ak4104.c 8.3 KB

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