ak4104.c 8.6 KB

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