ak4104.c 8.7 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 <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_22050 | SNDRV_PCM_RATE_32000 |
  144. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
  145. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
  146. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
  147. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  148. SNDRV_PCM_FMTBIT_S24_3LE |
  149. SNDRV_PCM_FMTBIT_S24_LE
  150. },
  151. .ops = &ak4101_dai_ops,
  152. };
  153. static int ak4104_probe(struct snd_soc_codec *codec)
  154. {
  155. struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
  156. int ret;
  157. ret = regulator_enable(ak4104->regulator);
  158. if (ret < 0) {
  159. dev_err(codec->dev, "Unable to enable regulator: %d\n", ret);
  160. return ret;
  161. }
  162. /* set power-up and non-reset bits */
  163. ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
  164. AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
  165. AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
  166. if (ret < 0)
  167. goto exit_disable_regulator;
  168. /* enable transmitter */
  169. ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
  170. AK4104_TX_TXE, AK4104_TX_TXE);
  171. if (ret < 0)
  172. goto exit_disable_regulator;
  173. return 0;
  174. exit_disable_regulator:
  175. regulator_disable(ak4104->regulator);
  176. return ret;
  177. }
  178. static int ak4104_remove(struct snd_soc_codec *codec)
  179. {
  180. struct ak4104_private *ak4104 = snd_soc_codec_get_drvdata(codec);
  181. regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
  182. AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
  183. regulator_disable(ak4104->regulator);
  184. return 0;
  185. }
  186. #ifdef CONFIG_PM
  187. static int ak4104_soc_suspend(struct snd_soc_codec *codec)
  188. {
  189. struct ak4104_private *priv = snd_soc_codec_get_drvdata(codec);
  190. regulator_disable(priv->regulator);
  191. return 0;
  192. }
  193. static int ak4104_soc_resume(struct snd_soc_codec *codec)
  194. {
  195. struct ak4104_private *priv = snd_soc_codec_get_drvdata(codec);
  196. int ret;
  197. ret = regulator_enable(priv->regulator);
  198. if (ret < 0)
  199. return ret;
  200. return 0;
  201. }
  202. #else
  203. #define ak4104_soc_suspend NULL
  204. #define ak4104_soc_resume NULL
  205. #endif /* CONFIG_PM */
  206. static struct snd_soc_codec_driver soc_codec_device_ak4104 = {
  207. .probe = ak4104_probe,
  208. .remove = ak4104_remove,
  209. .suspend = ak4104_soc_suspend,
  210. .resume = ak4104_soc_resume,
  211. .component_driver = {
  212. .dapm_widgets = ak4104_dapm_widgets,
  213. .num_dapm_widgets = ARRAY_SIZE(ak4104_dapm_widgets),
  214. .dapm_routes = ak4104_dapm_routes,
  215. .num_dapm_routes = ARRAY_SIZE(ak4104_dapm_routes),
  216. }
  217. };
  218. static const struct regmap_config ak4104_regmap = {
  219. .reg_bits = 8,
  220. .val_bits = 8,
  221. .max_register = AK4104_NUM_REGS - 1,
  222. .read_flag_mask = AK4104_READ,
  223. .write_flag_mask = AK4104_WRITE,
  224. .cache_type = REGCACHE_RBTREE,
  225. };
  226. static int ak4104_spi_probe(struct spi_device *spi)
  227. {
  228. struct device_node *np = spi->dev.of_node;
  229. struct ak4104_private *ak4104;
  230. unsigned int val;
  231. int ret;
  232. spi->bits_per_word = 8;
  233. spi->mode = SPI_MODE_0;
  234. ret = spi_setup(spi);
  235. if (ret < 0)
  236. return ret;
  237. ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
  238. GFP_KERNEL);
  239. if (ak4104 == NULL)
  240. return -ENOMEM;
  241. ak4104->regulator = devm_regulator_get(&spi->dev, "vdd");
  242. if (IS_ERR(ak4104->regulator)) {
  243. ret = PTR_ERR(ak4104->regulator);
  244. dev_err(&spi->dev, "Unable to get Vdd regulator: %d\n", ret);
  245. return ret;
  246. }
  247. ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
  248. if (IS_ERR(ak4104->regmap)) {
  249. ret = PTR_ERR(ak4104->regmap);
  250. return ret;
  251. }
  252. if (np) {
  253. enum of_gpio_flags flags;
  254. int gpio = of_get_named_gpio_flags(np, "reset-gpio", 0, &flags);
  255. if (gpio_is_valid(gpio)) {
  256. ret = devm_gpio_request_one(&spi->dev, gpio,
  257. flags & OF_GPIO_ACTIVE_LOW ?
  258. GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
  259. "ak4104 reset");
  260. if (ret < 0)
  261. return ret;
  262. }
  263. }
  264. /* read the 'reserved' register - according to the datasheet, it
  265. * should contain 0x5b. Not a good way to verify the presence of
  266. * the device, but there is no hardware ID register. */
  267. ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
  268. if (ret != 0)
  269. return ret;
  270. if (val != AK4104_RESERVED_VAL)
  271. return -ENODEV;
  272. spi_set_drvdata(spi, ak4104);
  273. ret = snd_soc_register_codec(&spi->dev,
  274. &soc_codec_device_ak4104, &ak4104_dai, 1);
  275. return ret;
  276. }
  277. static int ak4104_spi_remove(struct spi_device *spi)
  278. {
  279. snd_soc_unregister_codec(&spi->dev);
  280. return 0;
  281. }
  282. static const struct of_device_id ak4104_of_match[] = {
  283. { .compatible = "asahi-kasei,ak4104", },
  284. { }
  285. };
  286. MODULE_DEVICE_TABLE(of, ak4104_of_match);
  287. static const struct spi_device_id ak4104_id_table[] = {
  288. { "ak4104", 0 },
  289. { }
  290. };
  291. MODULE_DEVICE_TABLE(spi, ak4104_id_table);
  292. static struct spi_driver ak4104_spi_driver = {
  293. .driver = {
  294. .name = "ak4104",
  295. .of_match_table = ak4104_of_match,
  296. },
  297. .id_table = ak4104_id_table,
  298. .probe = ak4104_spi_probe,
  299. .remove = ak4104_spi_remove,
  300. };
  301. module_spi_driver(ak4104_spi_driver);
  302. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  303. MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
  304. MODULE_LICENSE("GPL");