ak4104.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 <linux/spi/spi.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <sound/asoundef.h>
  18. #include <sound/core.h>
  19. #include <sound/soc.h>
  20. #include <sound/initval.h>
  21. /* AK4104 registers addresses */
  22. #define AK4104_REG_CONTROL1 0x00
  23. #define AK4104_REG_RESERVED 0x01
  24. #define AK4104_REG_CONTROL2 0x02
  25. #define AK4104_REG_TX 0x03
  26. #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04)
  27. #define AK4104_NUM_REGS 10
  28. #define AK4104_REG_MASK 0x1f
  29. #define AK4104_READ 0xc0
  30. #define AK4104_WRITE 0xe0
  31. #define AK4104_RESERVED_VAL 0x5b
  32. /* Bit masks for AK4104 registers */
  33. #define AK4104_CONTROL1_RSTN (1 << 0)
  34. #define AK4104_CONTROL1_PW (1 << 1)
  35. #define AK4104_CONTROL1_DIF0 (1 << 2)
  36. #define AK4104_CONTROL1_DIF1 (1 << 3)
  37. #define AK4104_CONTROL2_SEL0 (1 << 0)
  38. #define AK4104_CONTROL2_SEL1 (1 << 1)
  39. #define AK4104_CONTROL2_MODE (1 << 2)
  40. #define AK4104_TX_TXE (1 << 0)
  41. #define AK4104_TX_V (1 << 1)
  42. struct ak4104_private {
  43. struct regmap *regmap;
  44. struct regulator *regulator;
  45. };
  46. static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = {
  47. SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX, AK4104_TX_TXE, 0, NULL, 0),
  48. SND_SOC_DAPM_OUTPUT("TX"),
  49. };
  50. static const struct snd_soc_dapm_route ak4104_dapm_routes[] = {
  51. { "TXE", NULL, "Playback" },
  52. { "TX", NULL, "TXE" },
  53. };
  54. static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
  55. unsigned int format)
  56. {
  57. struct snd_soc_codec *codec = codec_dai->codec;
  58. struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
  59. int val = 0;
  60. int ret;
  61. /* set DAI format */
  62. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  63. case SND_SOC_DAIFMT_RIGHT_J:
  64. break;
  65. case SND_SOC_DAIFMT_LEFT_J:
  66. val |= AK4104_CONTROL1_DIF0;
  67. break;
  68. case SND_SOC_DAIFMT_I2S:
  69. val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1;
  70. break;
  71. default:
  72. dev_err(codec->dev, "invalid dai format\n");
  73. return -EINVAL;
  74. }
  75. /* This device can only be slave */
  76. if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  77. return -EINVAL;
  78. ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
  79. AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
  80. val);
  81. if (ret < 0)
  82. return ret;
  83. return 0;
  84. }
  85. static int ak4104_hw_params(struct snd_pcm_substream *substream,
  86. struct snd_pcm_hw_params *params,
  87. struct snd_soc_dai *dai)
  88. {
  89. struct snd_soc_codec *codec = dai->codec;
  90. struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
  91. int ret, val = 0;
  92. /* set the IEC958 bits: consumer mode, no copyright bit */
  93. val |= IEC958_AES0_CON_NOT_COPYRIGHT;
  94. regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
  95. val = 0;
  96. switch (params_rate(params)) {
  97. case 22050:
  98. val |= IEC958_AES3_CON_FS_22050;
  99. break;
  100. case 24000:
  101. val |= IEC958_AES3_CON_FS_24000;
  102. break;
  103. case 32000:
  104. val |= IEC958_AES3_CON_FS_32000;
  105. break;
  106. case 44100:
  107. val |= IEC958_AES3_CON_FS_44100;
  108. break;
  109. case 48000:
  110. val |= IEC958_AES3_CON_FS_48000;
  111. break;
  112. case 88200:
  113. val |= IEC958_AES3_CON_FS_88200;
  114. break;
  115. case 96000:
  116. val |= IEC958_AES3_CON_FS_96000;
  117. break;
  118. case 176400:
  119. val |= IEC958_AES3_CON_FS_176400;
  120. break;
  121. case 192000:
  122. val |= IEC958_AES3_CON_FS_192000;
  123. break;
  124. default:
  125. dev_err(codec->dev, "unsupported sampling rate\n");
  126. return -EINVAL;
  127. }
  128. ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
  129. if (ret < 0)
  130. return ret;
  131. return 0;
  132. }
  133. static const struct snd_soc_dai_ops ak4101_dai_ops = {
  134. .hw_params = ak4104_hw_params,
  135. .set_fmt = ak4104_set_dai_fmt,
  136. };
  137. static struct snd_soc_dai_driver ak4104_dai = {
  138. .name = "ak4104-hifi",
  139. .playback = {
  140. .stream_name = "Playback",
  141. .channels_min = 2,
  142. .channels_max = 2,
  143. .rates = SNDRV_PCM_RATE_8000_192000,
  144. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  145. SNDRV_PCM_FMTBIT_S24_3LE |
  146. SNDRV_PCM_FMTBIT_S24_LE
  147. },
  148. .ops = &ak4101_dai_ops,
  149. };
  150. static int ak4104_probe(struct snd_soc_codec *codec)
  151. {
  152. struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
  153. int ret;
  154. ret = regulator_enable(ak4104->regulator);
  155. if (ret < 0) {
  156. dev_err(codec->dev, "Unable to enable regulator: %d\n", ret);
  157. return ret;
  158. }
  159. /* set power-up and non-reset bits */
  160. ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
  161. AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
  162. AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
  163. if (ret < 0)
  164. goto exit_disable_regulator;
  165. /* enable transmitter */
  166. ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
  167. AK4104_TX_TXE, AK4104_TX_TXE);
  168. if (ret < 0)
  169. goto exit_disable_regulator;
  170. return 0;
  171. exit_disable_regulator:
  172. regulator_disable(ak4104->regulator);
  173. return ret;
  174. }
  175. static int ak4104_remove(struct snd_soc_codec *codec)
  176. {
  177. struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
  178. regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
  179. AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
  180. regulator_disable(ak4104->regulator);
  181. return 0;
  182. }
  183. #ifdef CONFIG_PM
  184. static int ak4104_soc_suspend(struct snd_soc_codec *codec)
  185. {
  186. struct ak4104_private *priv = snd_soc_codec_get_drvdata(codec);
  187. regulator_disable(priv->regulator);
  188. return 0;
  189. }
  190. static int ak4104_soc_resume(struct snd_soc_codec *codec)
  191. {
  192. struct ak4104_private *priv = snd_soc_codec_get_drvdata(codec);
  193. int ret;
  194. ret = regulator_enable(priv->regulator);
  195. if (ret < 0)
  196. return ret;
  197. return 0;
  198. }
  199. #else
  200. #define ak4104_soc_suspend NULL
  201. #define ak4104_soc_resume NULL
  202. #endif /* CONFIG_PM */
  203. static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
  204. .probe = ak4104_probe,
  205. .remove = ak4104_remove,
  206. .suspend = ak4104_soc_suspend,
  207. .resume = ak4104_soc_resume,
  208. .dapm_widgets = ak4104_dapm_widgets,
  209. .num_dapm_widgets = ARRAY_SIZE(ak4104_dapm_widgets),
  210. .dapm_routes = ak4104_dapm_routes,
  211. .num_dapm_routes = ARRAY_SIZE(ak4104_dapm_routes),
  212. };
  213. static const struct regmap_config ak4104_regmap = {
  214. .reg_bits = 8,
  215. .val_bits = 8,
  216. .max_register = AK4104_NUM_REGS - 1,
  217. .read_flag_mask = AK4104_READ,
  218. .write_flag_mask = AK4104_WRITE,
  219. .cache_type = REGCACHE_RBTREE,
  220. };
  221. static int ak4104_spi_probe(struct spi_device *spi)
  222. {
  223. struct device_node *np = spi->dev.of_node;
  224. struct ak4104_private *ak4104;
  225. unsigned int val;
  226. int ret;
  227. spi->bits_per_word = 8;
  228. spi->mode = SPI_MODE_0;
  229. ret = spi_setup(spi);
  230. if (ret < 0)
  231. return ret;
  232. ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
  233. GFP_KERNEL);
  234. if (ak4104 == NULL)
  235. return -ENOMEM;
  236. ak4104->regulator = devm_regulator_get(&spi->dev, "vdd");
  237. if (IS_ERR(ak4104->regulator)) {
  238. ret = PTR_ERR(ak4104->regulator);
  239. dev_err(&spi->dev, "Unable to get Vdd regulator: %d\n", ret);
  240. return ret;
  241. }
  242. ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
  243. if (IS_ERR(ak4104->regmap)) {
  244. ret = PTR_ERR(ak4104->regmap);
  245. return ret;
  246. }
  247. if (np) {
  248. enum of_gpio_flags flags;
  249. int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
  250. if (gpio_is_valid(gpio)) {
  251. ret = devm_gpio_request_one(&spi->dev, gpio,
  252. flags & OF_GPIO_ACTIVE_LOW ?
  253. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
  254. "ak4104 reset");
  255. if (ret < 0)
  256. return ret;
  257. }
  258. }
  259. /* read the 'reserved' register - according to the datasheet, it
  260. * should contain 0x5b. Not a good way to verify the presence of
  261. * the device, but there is no hardware ID register. */
  262. ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
  263. if (ret != 0)
  264. return ret;
  265. if (val != AK4104_RESERVED_VAL)
  266. return -ENODEV;
  267. spi_set_drvdata(spi, ak4104);
  268. ret = snd_soc_register_codec(&spi->dev,
  269. &soc_codec_device_ak4104, &ak4104_dai, 1);
  270. return ret;
  271. }
  272. static int ak4104_spi_remove(struct spi_device *spi)
  273. {
  274. snd_soc_unregister_codec(&spi->dev);
  275. return 0;
  276. }
  277. static const struct of_device_id ak4104_of_match[] = {
  278. { .compatible = "asahi-kasei,ak4104", },
  279. { }
  280. };
  281. MODULE_DEVICE_TABLE(of, ak4104_of_match);
  282. static const struct spi_device_id ak4104_id_table[] = {
  283. { "ak4104", 0 },
  284. { }
  285. };
  286. MODULE_DEVICE_TABLE(spi, ak4104_id_table);
  287. static struct spi_driver ak4104_spi_driver = {
  288. .driver = {
  289. .name = "ak4104",
  290. .owner = THIS_MODULE,
  291. .of_match_table = ak4104_of_match,
  292. },
  293. .id_table = ak4104_id_table,
  294. .probe = ak4104_spi_probe,
  295. .remove = ak4104_spi_remove,
  296. };
  297. module_spi_driver(ak4104_spi_driver);
  298. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  299. MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
  300. MODULE_LICENSE("GPL");