ak4104.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /* 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-codec"
  40. struct ak4104_private {
  41. enum snd_soc_control_type control_type;
  42. void *control_data;
  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->driver->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->driver->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->driver->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_codec *codec = rtd->codec;
  119. int val = 0;
  120. /* set the IEC958 bits: consumer mode, no copyright bit */
  121. val |= IEC958_AES0_CON_NOT_COPYRIGHT;
  122. ak4104_spi_write(codec, AK4104_REG_CHN_STATUS(0), val);
  123. val = 0;
  124. switch (params_rate(params)) {
  125. case 44100:
  126. val |= IEC958_AES3_CON_FS_44100;
  127. break;
  128. case 48000:
  129. val |= IEC958_AES3_CON_FS_48000;
  130. break;
  131. case 32000:
  132. val |= IEC958_AES3_CON_FS_32000;
  133. break;
  134. default:
  135. dev_err(codec->dev, "unsupported sampling rate\n");
  136. return -EINVAL;
  137. }
  138. return ak4104_spi_write(codec, AK4104_REG_CHN_STATUS(3), val);
  139. }
  140. static struct snd_soc_dai_ops ak4101_dai_ops = {
  141. .hw_params = ak4104_hw_params,
  142. .set_fmt = ak4104_set_dai_fmt,
  143. };
  144. static struct snd_soc_dai_driver ak4104_dai = {
  145. .name = "ak4104-hifi",
  146. .playback = {
  147. .stream_name = "Playback",
  148. .channels_min = 2,
  149. .channels_max = 2,
  150. .rates = SNDRV_PCM_RATE_8000_192000,
  151. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  152. SNDRV_PCM_FMTBIT_S24_3LE |
  153. SNDRV_PCM_FMTBIT_S24_LE
  154. },
  155. .ops = &ak4101_dai_ops,
  156. };
  157. static int ak4104_probe(struct snd_soc_codec *codec)
  158. {
  159. struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
  160. int ret, val;
  161. codec->control_data = ak4104->control_data;
  162. /* read all regs and fill the cache */
  163. ret = ak4104_fill_cache(codec);
  164. if (ret < 0) {
  165. dev_err(codec->dev, "failed to fill register cache\n");
  166. return ret;
  167. }
  168. /* read the 'reserved' register - according to the datasheet, it
  169. * should contain 0x5b. Not a good way to verify the presence of
  170. * the device, but there is no hardware ID register. */
  171. if (ak4104_read_reg_cache(codec, AK4104_REG_RESERVED) !=
  172. AK4104_RESERVED_VAL)
  173. return -ENODEV;
  174. /* set power-up and non-reset bits */
  175. val = ak4104_read_reg_cache(codec, AK4104_REG_CONTROL1);
  176. val |= AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN;
  177. ret = ak4104_spi_write(codec, AK4104_REG_CONTROL1, val);
  178. if (ret < 0)
  179. return ret;
  180. /* enable transmitter */
  181. val = ak4104_read_reg_cache(codec, AK4104_REG_TX);
  182. val |= AK4104_TX_TXE;
  183. ret = ak4104_spi_write(codec, AK4104_REG_TX, val);
  184. if (ret < 0)
  185. return ret;
  186. dev_info(codec->dev, "SPI device initialized\n");
  187. return 0;
  188. }
  189. static int ak4104_remove(struct snd_soc_codec *codec)
  190. {
  191. int val, ret;
  192. val = ak4104_read_reg_cache(codec, AK4104_REG_CONTROL1);
  193. if (val < 0)
  194. return val;
  195. /* clear power-up and non-reset bits */
  196. val &= ~(AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
  197. ret = ak4104_spi_write(codec, AK4104_REG_CONTROL1, val);
  198. return ret;
  199. }
  200. static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
  201. .probe = ak4104_probe,
  202. .remove = ak4104_remove,
  203. .reg_cache_size = AK4104_NUM_REGS,
  204. .reg_word_size = sizeof(u16),
  205. };
  206. static int ak4104_spi_probe(struct spi_device *spi)
  207. {
  208. struct ak4104_private *ak4104;
  209. int ret;
  210. spi->bits_per_word = 8;
  211. spi->mode = SPI_MODE_0;
  212. ret = spi_setup(spi);
  213. if (ret < 0)
  214. return ret;
  215. ak4104 = kzalloc(sizeof(struct ak4104_private), GFP_KERNEL);
  216. if (ak4104 == NULL)
  217. return -ENOMEM;
  218. ak4104->control_data = spi;
  219. ak4104->control_type = SND_SOC_SPI;
  220. spi_set_drvdata(spi, ak4104);
  221. ret = snd_soc_register_codec(&spi->dev,
  222. &soc_codec_device_ak4104, &ak4104_dai, 1);
  223. if (ret < 0)
  224. kfree(ak4104);
  225. return ret;
  226. }
  227. static int __devexit ak4104_spi_remove(struct spi_device *spi)
  228. {
  229. snd_soc_unregister_codec(&spi->dev);
  230. kfree(spi_get_drvdata(spi));
  231. return 0;
  232. }
  233. static struct spi_driver ak4104_spi_driver = {
  234. .driver = {
  235. .name = DRV_NAME,
  236. .owner = THIS_MODULE,
  237. },
  238. .probe = ak4104_spi_probe,
  239. .remove = __devexit_p(ak4104_spi_remove),
  240. };
  241. static int __init ak4104_init(void)
  242. {
  243. return spi_register_driver(&ak4104_spi_driver);
  244. }
  245. module_init(ak4104_init);
  246. static void __exit ak4104_exit(void)
  247. {
  248. spi_unregister_driver(&ak4104_spi_driver);
  249. }
  250. module_exit(ak4104_exit);
  251. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  252. MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
  253. MODULE_LICENSE("GPL");