PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/sound/soc/codecs/ak4104.c

https://bitbucket.org/abioy/linux
C | 354 lines | 266 code | 63 blank | 25 comment | 22 complexity | 27567844d64f6c47f0ec482f961acdb4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * AK4104 ALSA SoC (ASoC) driver
  3. *
  4. * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <sound/core.h>
  14. #include <sound/soc.h>
  15. #include <sound/initval.h>
  16. #include <linux/spi/spi.h>
  17. #include <sound/asoundef.h>
  18. #include "ak4104.h"
  19. /* AK4104 registers addresses */
  20. #define AK4104_REG_CONTROL1 0x00
  21. #define AK4104_REG_RESERVED 0x01
  22. #define AK4104_REG_CONTROL2 0x02
  23. #define AK4104_REG_TX 0x03
  24. #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04)
  25. #define AK4104_NUM_REGS 10
  26. #define AK4104_REG_MASK 0x1f
  27. #define AK4104_READ 0xc0
  28. #define AK4104_WRITE 0xe0
  29. #define AK4104_RESERVED_VAL 0x5b
  30. /* Bit masks for AK4104 registers */
  31. #define AK4104_CONTROL1_RSTN (1 << 0)
  32. #define AK4104_CONTROL1_PW (1 << 1)
  33. #define AK4104_CONTROL1_DIF0 (1 << 2)
  34. #define AK4104_CONTROL1_DIF1 (1 << 3)
  35. #define AK4104_CONTROL2_SEL0 (1 << 0)
  36. #define AK4104_CONTROL2_SEL1 (1 << 1)
  37. #define AK4104_CONTROL2_MODE (1 << 2)
  38. #define AK4104_TX_TXE (1 << 0)
  39. #define AK4104_TX_V (1 << 1)
  40. #define DRV_NAME "ak4104"
  41. struct ak4104_private {
  42. struct snd_soc_codec codec;
  43. u8 reg_cache[AK4104_NUM_REGS];
  44. };
  45. static int ak4104_fill_cache(struct snd_soc_codec *codec)
  46. {
  47. int i;
  48. u8 *reg_cache = codec->reg_cache;
  49. struct spi_device *spi = codec->control_data;
  50. for (i = 0; i < codec->reg_cache_size; i++) {
  51. int ret = spi_w8r8(spi, i | AK4104_READ);
  52. if (ret < 0) {
  53. dev_err(&spi->dev, "SPI write failure\n");
  54. return ret;
  55. }
  56. reg_cache[i] = ret;
  57. }
  58. return 0;
  59. }
  60. static unsigned int ak4104_read_reg_cache(struct snd_soc_codec *codec,
  61. unsigned int reg)
  62. {
  63. u8 *reg_cache = codec->reg_cache;
  64. if (reg >= codec->reg_cache_size)
  65. return -EINVAL;
  66. return reg_cache[reg];
  67. }
  68. static int ak4104_spi_write(struct snd_soc_codec *codec, unsigned int reg,
  69. unsigned int value)
  70. {
  71. u8 *cache = codec->reg_cache;
  72. struct spi_device *spi = codec->control_data;
  73. if (reg >= codec->reg_cache_size)
  74. return -EINVAL;
  75. /* only write to the hardware if value has changed */
  76. if (cache[reg] != value) {
  77. u8 tmp[2] = { (reg & AK4104_REG_MASK) | AK4104_WRITE, value };
  78. if (spi_write(spi, tmp, sizeof(tmp))) {
  79. dev_err(&spi->dev, "SPI write failed\n");
  80. return -EIO;
  81. }
  82. cache[reg] = value;
  83. }
  84. return 0;
  85. }
  86. static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
  87. unsigned int format)
  88. {
  89. struct snd_soc_codec *codec = codec_dai->codec;
  90. int val = 0;
  91. val = ak4104_read_reg_cache(codec, AK4104_REG_CONTROL1);
  92. if (val < 0)
  93. return val;
  94. val &= ~(AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1);
  95. /* set DAI format */
  96. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  97. case SND_SOC_DAIFMT_RIGHT_J:
  98. break;
  99. case SND_SOC_DAIFMT_LEFT_J:
  100. val |= AK4104_CONTROL1_DIF0;
  101. break;
  102. case SND_SOC_DAIFMT_I2S:
  103. val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1;
  104. break;
  105. default:
  106. dev_err(codec->dev, "invalid dai format\n");
  107. return -EINVAL;
  108. }
  109. /* This device can only be slave */
  110. if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  111. return -EINVAL;
  112. return ak4104_spi_write(codec, AK4104_REG_CONTROL1, val);
  113. }
  114. static int ak4104_hw_params(struct snd_pcm_substream *substream,
  115. struct snd_pcm_hw_params *params,
  116. struct snd_soc_dai *dai)
  117. {
  118. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  119. struct snd_soc_device *socdev = rtd->socdev;
  120. struct snd_soc_codec *codec = socdev->card->codec;
  121. int val = 0;
  122. /* set the IEC958 bits: consumer mode, no copyright bit */
  123. val |= IEC958_AES0_CON_NOT_COPYRIGHT;
  124. ak4104_spi_write(codec, AK4104_REG_CHN_STATUS(0), val);
  125. val = 0;
  126. switch (params_rate(params)) {
  127. case 44100:
  128. val |= IEC958_AES3_CON_FS_44100;
  129. break;
  130. case 48000:
  131. val |= IEC958_AES3_CON_FS_48000;
  132. break;
  133. case 32000:
  134. val |= IEC958_AES3_CON_FS_32000;
  135. break;
  136. default:
  137. dev_err(codec->dev, "unsupported sampling rate\n");
  138. return -EINVAL;
  139. }
  140. return ak4104_spi_write(codec, AK4104_REG_CHN_STATUS(3), val);
  141. }
  142. static struct snd_soc_dai_ops ak4101_dai_ops = {
  143. .hw_params = ak4104_hw_params,
  144. .set_fmt = ak4104_set_dai_fmt,
  145. };
  146. struct snd_soc_dai ak4104_dai = {
  147. .name = DRV_NAME,
  148. .playback = {
  149. .stream_name = "Playback",
  150. .channels_min = 2,
  151. .channels_max = 2,
  152. .rates = SNDRV_PCM_RATE_8000_192000,
  153. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  154. SNDRV_PCM_FMTBIT_S24_3LE |
  155. SNDRV_PCM_FMTBIT_S24_LE
  156. },
  157. .ops = &ak4101_dai_ops,
  158. };
  159. static struct snd_soc_codec *ak4104_codec;
  160. static int ak4104_spi_probe(struct spi_device *spi)
  161. {
  162. struct snd_soc_codec *codec;
  163. struct ak4104_private *ak4104;
  164. int ret, val;
  165. spi->bits_per_word = 8;
  166. spi->mode = SPI_MODE_0;
  167. ret = spi_setup(spi);
  168. if (ret < 0)
  169. return ret;
  170. ak4104 = kzalloc(sizeof(struct ak4104_private), GFP_KERNEL);
  171. if (!ak4104) {
  172. dev_err(&spi->dev, "could not allocate codec\n");
  173. return -ENOMEM;
  174. }
  175. codec = &ak4104->codec;
  176. mutex_init(&codec->mutex);
  177. INIT_LIST_HEAD(&codec->dapm_widgets);
  178. INIT_LIST_HEAD(&codec->dapm_paths);
  179. codec->dev = &spi->dev;
  180. codec->name = DRV_NAME;
  181. codec->owner = THIS_MODULE;
  182. codec->dai = &ak4104_dai;
  183. codec->num_dai = 1;
  184. codec->private_data = ak4104;
  185. codec->control_data = spi;
  186. codec->reg_cache = ak4104->reg_cache;
  187. codec->reg_cache_size = AK4104_NUM_REGS;
  188. /* read all regs and fill the cache */
  189. ret = ak4104_fill_cache(codec);
  190. if (ret < 0) {
  191. dev_err(&spi->dev, "failed to fill register cache\n");
  192. return ret;
  193. }
  194. /* read the 'reserved' register - according to the datasheet, it
  195. * should contain 0x5b. Not a good way to verify the presence of
  196. * the device, but there is no hardware ID register. */
  197. if (ak4104_read_reg_cache(codec, AK4104_REG_RESERVED) !=
  198. AK4104_RESERVED_VAL) {
  199. ret = -ENODEV;
  200. goto error_free_codec;
  201. }
  202. /* set power-up and non-reset bits */
  203. val = ak4104_read_reg_cache(codec, AK4104_REG_CONTROL1);
  204. val |= AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN;
  205. ret = ak4104_spi_write(codec, AK4104_REG_CONTROL1, val);
  206. if (ret < 0)
  207. goto error_free_codec;
  208. /* enable transmitter */
  209. val = ak4104_read_reg_cache(codec, AK4104_REG_TX);
  210. val |= AK4104_TX_TXE;
  211. ret = ak4104_spi_write(codec, AK4104_REG_TX, val);
  212. if (ret < 0)
  213. goto error_free_codec;
  214. ak4104_codec = codec;
  215. ret = snd_soc_register_dai(&ak4104_dai);
  216. if (ret < 0) {
  217. dev_err(&spi->dev, "failed to register DAI\n");
  218. goto error_free_codec;
  219. }
  220. spi_set_drvdata(spi, ak4104);
  221. dev_info(&spi->dev, "SPI device initialized\n");
  222. return 0;
  223. error_free_codec:
  224. kfree(ak4104);
  225. ak4104_dai.dev = NULL;
  226. return ret;
  227. }
  228. static int __devexit ak4104_spi_remove(struct spi_device *spi)
  229. {
  230. int ret, val;
  231. struct ak4104_private *ak4104 = spi_get_drvdata(spi);
  232. val = ak4104_read_reg_cache(&ak4104->codec, AK4104_REG_CONTROL1);
  233. if (val < 0)
  234. return val;
  235. /* clear power-up and non-reset bits */
  236. val &= ~(AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
  237. ret = ak4104_spi_write(&ak4104->codec, AK4104_REG_CONTROL1, val);
  238. if (ret < 0)
  239. return ret;
  240. ak4104_codec = NULL;
  241. kfree(ak4104);
  242. return 0;
  243. }
  244. static int ak4104_probe(struct platform_device *pdev)
  245. {
  246. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  247. struct snd_soc_codec *codec = ak4104_codec;
  248. int ret;
  249. /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
  250. socdev->card->codec = codec;
  251. /* Register PCMs */
  252. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  253. if (ret < 0) {
  254. dev_err(codec->dev, "failed to create pcms\n");
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. static int ak4104_remove(struct platform_device *pdev)
  260. {
  261. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  262. snd_soc_free_pcms(socdev);
  263. return 0;
  264. };
  265. struct snd_soc_codec_device soc_codec_device_ak4104 = {
  266. .probe = ak4104_probe,
  267. .remove = ak4104_remove
  268. };
  269. EXPORT_SYMBOL_GPL(soc_codec_device_ak4104);
  270. static struct spi_driver ak4104_spi_driver = {
  271. .driver = {
  272. .name = DRV_NAME,
  273. .owner = THIS_MODULE,
  274. },
  275. .probe = ak4104_spi_probe,
  276. .remove = __devexit_p(ak4104_spi_remove),
  277. };
  278. static int __init ak4104_init(void)
  279. {
  280. pr_info("Asahi Kasei AK4104 ALSA SoC Codec Driver\n");
  281. return spi_register_driver(&ak4104_spi_driver);
  282. }
  283. module_init(ak4104_init);
  284. static void __exit ak4104_exit(void)
  285. {
  286. spi_unregister_driver(&ak4104_spi_driver);
  287. }
  288. module_exit(ak4104_exit);
  289. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  290. MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
  291. MODULE_LICENSE("GPL");