ak4104.c 8.4 KB

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