ak5558.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Audio driver for AK5558 ADC
  4. //
  5. // Copyright (C) 2015 Asahi Kasei Microdevices Corporation
  6. // Copyright 2018 NXP
  7. #include <linux/delay.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/i2c.h>
  10. #include <linux/module.h>
  11. #include <linux/pm_runtime.h>
  12. #include <linux/regmap.h>
  13. #include <linux/regulator/consumer.h>
  14. #include <linux/slab.h>
  15. #include <sound/initval.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include <sound/soc-dapm.h>
  20. #include <sound/tlv.h>
  21. #include "ak5558.h"
  22. #define AK5558_NUM_SUPPLIES 2
  23. static const char *ak5558_supply_names[AK5558_NUM_SUPPLIES] = {
  24. "DVDD",
  25. "AVDD",
  26. };
  27. /* AK5558 Codec Private Data */
  28. struct ak5558_priv {
  29. struct regulator_bulk_data supplies[AK5558_NUM_SUPPLIES];
  30. struct snd_soc_component component;
  31. struct regmap *regmap;
  32. struct i2c_client *i2c;
  33. struct gpio_desc *reset_gpiod; /* Reset & Power down GPIO */
  34. int slots;
  35. int slot_width;
  36. };
  37. /* ak5558 register cache & default register settings */
  38. static const struct reg_default ak5558_reg[] = {
  39. { 0x0, 0xFF }, /* 0x00 AK5558_00_POWER_MANAGEMENT1 */
  40. { 0x1, 0x01 }, /* 0x01 AK5558_01_POWER_MANAGEMENT2 */
  41. { 0x2, 0x01 }, /* 0x02 AK5558_02_CONTROL1 */
  42. { 0x3, 0x00 }, /* 0x03 AK5558_03_CONTROL2 */
  43. { 0x4, 0x00 }, /* 0x04 AK5558_04_CONTROL3 */
  44. { 0x5, 0x00 } /* 0x05 AK5558_05_DSD */
  45. };
  46. static const char * const mono_texts[] = {
  47. "8 Slot", "2 Slot", "4 Slot", "1 Slot",
  48. };
  49. static const struct soc_enum ak5558_mono_enum[] = {
  50. SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
  51. ARRAY_SIZE(mono_texts), mono_texts),
  52. };
  53. static const char * const digfil_texts[] = {
  54. "Sharp Roll-Off", "Show Roll-Off",
  55. "Short Delay Sharp Roll-Off", "Short Delay Show Roll-Off",
  56. };
  57. static const struct soc_enum ak5558_adcset_enum[] = {
  58. SOC_ENUM_SINGLE(AK5558_04_CONTROL3, 0,
  59. ARRAY_SIZE(digfil_texts), digfil_texts),
  60. };
  61. static const struct snd_kcontrol_new ak5558_snd_controls[] = {
  62. SOC_ENUM("AK5558 Monaural Mode", ak5558_mono_enum[0]),
  63. SOC_ENUM("AK5558 Digital Filter", ak5558_adcset_enum[0]),
  64. };
  65. static const struct snd_soc_dapm_widget ak5558_dapm_widgets[] = {
  66. /* Analog Input */
  67. SND_SOC_DAPM_INPUT("AIN1"),
  68. SND_SOC_DAPM_INPUT("AIN2"),
  69. SND_SOC_DAPM_INPUT("AIN3"),
  70. SND_SOC_DAPM_INPUT("AIN4"),
  71. SND_SOC_DAPM_INPUT("AIN5"),
  72. SND_SOC_DAPM_INPUT("AIN6"),
  73. SND_SOC_DAPM_INPUT("AIN7"),
  74. SND_SOC_DAPM_INPUT("AIN8"),
  75. SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
  76. SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),
  77. SND_SOC_DAPM_ADC("ADC Ch3", NULL, AK5558_00_POWER_MANAGEMENT1, 2, 0),
  78. SND_SOC_DAPM_ADC("ADC Ch4", NULL, AK5558_00_POWER_MANAGEMENT1, 3, 0),
  79. SND_SOC_DAPM_ADC("ADC Ch5", NULL, AK5558_00_POWER_MANAGEMENT1, 4, 0),
  80. SND_SOC_DAPM_ADC("ADC Ch6", NULL, AK5558_00_POWER_MANAGEMENT1, 5, 0),
  81. SND_SOC_DAPM_ADC("ADC Ch7", NULL, AK5558_00_POWER_MANAGEMENT1, 6, 0),
  82. SND_SOC_DAPM_ADC("ADC Ch8", NULL, AK5558_00_POWER_MANAGEMENT1, 7, 0),
  83. SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
  84. };
  85. static const struct snd_soc_dapm_route ak5558_intercon[] = {
  86. {"ADC Ch1", NULL, "AIN1"},
  87. {"SDTO", NULL, "ADC Ch1"},
  88. {"ADC Ch2", NULL, "AIN2"},
  89. {"SDTO", NULL, "ADC Ch2"},
  90. {"ADC Ch3", NULL, "AIN3"},
  91. {"SDTO", NULL, "ADC Ch3"},
  92. {"ADC Ch4", NULL, "AIN4"},
  93. {"SDTO", NULL, "ADC Ch4"},
  94. {"ADC Ch5", NULL, "AIN5"},
  95. {"SDTO", NULL, "ADC Ch5"},
  96. {"ADC Ch6", NULL, "AIN6"},
  97. {"SDTO", NULL, "ADC Ch6"},
  98. {"ADC Ch7", NULL, "AIN7"},
  99. {"SDTO", NULL, "ADC Ch7"},
  100. {"ADC Ch8", NULL, "AIN8"},
  101. {"SDTO", NULL, "ADC Ch8"},
  102. };
  103. static int ak5558_set_mcki(struct snd_soc_component *component)
  104. {
  105. return snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_CKS,
  106. AK5558_CKS_AUTO);
  107. }
  108. static int ak5558_hw_params(struct snd_pcm_substream *substream,
  109. struct snd_pcm_hw_params *params,
  110. struct snd_soc_dai *dai)
  111. {
  112. struct snd_soc_component *component = dai->component;
  113. struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
  114. u8 bits;
  115. int pcm_width = max(params_physical_width(params), ak5558->slot_width);
  116. switch (pcm_width) {
  117. case 16:
  118. bits = AK5558_DIF_24BIT_MODE;
  119. break;
  120. case 32:
  121. bits = AK5558_DIF_32BIT_MODE;
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_BITS, bits);
  127. return 0;
  128. }
  129. static int ak5558_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  130. {
  131. struct snd_soc_component *component = dai->component;
  132. u8 format;
  133. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  134. case SND_SOC_DAIFMT_CBS_CFS:
  135. break;
  136. case SND_SOC_DAIFMT_CBM_CFM:
  137. break;
  138. case SND_SOC_DAIFMT_CBS_CFM:
  139. case SND_SOC_DAIFMT_CBM_CFS:
  140. default:
  141. dev_err(dai->dev, "Clock mode unsupported");
  142. return -EINVAL;
  143. }
  144. /* set master/slave audio interface */
  145. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  146. case SND_SOC_DAIFMT_I2S:
  147. format = AK5558_DIF_I2S_MODE;
  148. break;
  149. case SND_SOC_DAIFMT_LEFT_J:
  150. format = AK5558_DIF_MSB_MODE;
  151. break;
  152. case SND_SOC_DAIFMT_DSP_B:
  153. format = AK5558_DIF_MSB_MODE;
  154. break;
  155. default:
  156. return -EINVAL;
  157. }
  158. snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_DIF, format);
  159. return 0;
  160. }
  161. static int ak5558_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  162. unsigned int rx_mask, int slots,
  163. int slot_width)
  164. {
  165. struct snd_soc_component *component = dai->component;
  166. struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
  167. int tdm_mode;
  168. ak5558->slots = slots;
  169. ak5558->slot_width = slot_width;
  170. switch (slots * slot_width) {
  171. case 128:
  172. tdm_mode = AK5558_MODE_TDM128;
  173. break;
  174. case 256:
  175. tdm_mode = AK5558_MODE_TDM256;
  176. break;
  177. case 512:
  178. tdm_mode = AK5558_MODE_TDM512;
  179. break;
  180. default:
  181. tdm_mode = AK5558_MODE_NORMAL;
  182. break;
  183. }
  184. snd_soc_component_update_bits(component, AK5558_03_CONTROL2, AK5558_MODE_BITS,
  185. tdm_mode);
  186. return 0;
  187. }
  188. #define AK5558_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  189. SNDRV_PCM_FMTBIT_S24_LE |\
  190. SNDRV_PCM_FMTBIT_S32_LE)
  191. static const unsigned int ak5558_rates[] = {
  192. 8000, 11025, 16000, 22050,
  193. 32000, 44100, 48000, 88200,
  194. 96000, 176400, 192000, 352800,
  195. 384000, 705600, 768000, 1411200,
  196. 2822400,
  197. };
  198. static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints = {
  199. .count = ARRAY_SIZE(ak5558_rates),
  200. .list = ak5558_rates,
  201. };
  202. static int ak5558_startup(struct snd_pcm_substream *substream,
  203. struct snd_soc_dai *dai)
  204. {
  205. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  206. SNDRV_PCM_HW_PARAM_RATE,
  207. &ak5558_rate_constraints);
  208. }
  209. static const struct snd_soc_dai_ops ak5558_dai_ops = {
  210. .startup = ak5558_startup,
  211. .hw_params = ak5558_hw_params,
  212. .set_fmt = ak5558_set_dai_fmt,
  213. .set_tdm_slot = ak5558_set_tdm_slot,
  214. };
  215. static struct snd_soc_dai_driver ak5558_dai = {
  216. .name = "ak5558-aif",
  217. .capture = {
  218. .stream_name = "Capture",
  219. .channels_min = 1,
  220. .channels_max = 8,
  221. .rates = SNDRV_PCM_RATE_KNOT,
  222. .formats = AK5558_FORMATS,
  223. },
  224. .ops = &ak5558_dai_ops,
  225. };
  226. static void ak5558_power_off(struct ak5558_priv *ak5558)
  227. {
  228. if (!ak5558->reset_gpiod)
  229. return;
  230. gpiod_set_value_cansleep(ak5558->reset_gpiod, 1);
  231. usleep_range(1000, 2000);
  232. }
  233. static void ak5558_power_on(struct ak5558_priv *ak5558)
  234. {
  235. if (!ak5558->reset_gpiod)
  236. return;
  237. gpiod_set_value_cansleep(ak5558->reset_gpiod, 0);
  238. usleep_range(1000, 2000);
  239. }
  240. static int ak5558_probe(struct snd_soc_component *component)
  241. {
  242. struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
  243. ak5558_power_on(ak5558);
  244. return ak5558_set_mcki(component);
  245. }
  246. static void ak5558_remove(struct snd_soc_component *component)
  247. {
  248. struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
  249. ak5558_power_off(ak5558);
  250. }
  251. static int __maybe_unused ak5558_runtime_suspend(struct device *dev)
  252. {
  253. struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
  254. regcache_cache_only(ak5558->regmap, true);
  255. ak5558_power_off(ak5558);
  256. regulator_bulk_disable(ARRAY_SIZE(ak5558->supplies),
  257. ak5558->supplies);
  258. return 0;
  259. }
  260. static int __maybe_unused ak5558_runtime_resume(struct device *dev)
  261. {
  262. struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
  263. int ret;
  264. ret = regulator_bulk_enable(ARRAY_SIZE(ak5558->supplies),
  265. ak5558->supplies);
  266. if (ret != 0) {
  267. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  268. return ret;
  269. }
  270. ak5558_power_off(ak5558);
  271. ak5558_power_on(ak5558);
  272. regcache_cache_only(ak5558->regmap, false);
  273. regcache_mark_dirty(ak5558->regmap);
  274. return regcache_sync(ak5558->regmap);
  275. }
  276. static const struct dev_pm_ops ak5558_pm = {
  277. SET_RUNTIME_PM_OPS(ak5558_runtime_suspend, ak5558_runtime_resume, NULL)
  278. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  279. pm_runtime_force_resume)
  280. };
  281. static const struct snd_soc_component_driver soc_codec_dev_ak5558 = {
  282. .probe = ak5558_probe,
  283. .remove = ak5558_remove,
  284. .controls = ak5558_snd_controls,
  285. .num_controls = ARRAY_SIZE(ak5558_snd_controls),
  286. .dapm_widgets = ak5558_dapm_widgets,
  287. .num_dapm_widgets = ARRAY_SIZE(ak5558_dapm_widgets),
  288. .dapm_routes = ak5558_intercon,
  289. .num_dapm_routes = ARRAY_SIZE(ak5558_intercon),
  290. .idle_bias_on = 1,
  291. .use_pmdown_time = 1,
  292. .endianness = 1,
  293. .non_legacy_dai_naming = 1,
  294. };
  295. static const struct regmap_config ak5558_regmap = {
  296. .reg_bits = 8,
  297. .val_bits = 8,
  298. .max_register = AK5558_05_DSD,
  299. .reg_defaults = ak5558_reg,
  300. .num_reg_defaults = ARRAY_SIZE(ak5558_reg),
  301. .cache_type = REGCACHE_RBTREE,
  302. };
  303. static int ak5558_i2c_probe(struct i2c_client *i2c)
  304. {
  305. struct ak5558_priv *ak5558;
  306. int ret = 0;
  307. int i;
  308. ak5558 = devm_kzalloc(&i2c->dev, sizeof(*ak5558), GFP_KERNEL);
  309. if (!ak5558)
  310. return -ENOMEM;
  311. ak5558->regmap = devm_regmap_init_i2c(i2c, &ak5558_regmap);
  312. if (IS_ERR(ak5558->regmap))
  313. return PTR_ERR(ak5558->regmap);
  314. i2c_set_clientdata(i2c, ak5558);
  315. ak5558->i2c = i2c;
  316. ak5558->reset_gpiod = devm_gpiod_get_optional(&i2c->dev, "reset",
  317. GPIOD_OUT_LOW);
  318. if (IS_ERR(ak5558->reset_gpiod))
  319. return PTR_ERR(ak5558->reset_gpiod);
  320. for (i = 0; i < ARRAY_SIZE(ak5558->supplies); i++)
  321. ak5558->supplies[i].supply = ak5558_supply_names[i];
  322. ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(ak5558->supplies),
  323. ak5558->supplies);
  324. if (ret != 0) {
  325. dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
  326. return ret;
  327. }
  328. ret = devm_snd_soc_register_component(&i2c->dev,
  329. &soc_codec_dev_ak5558,
  330. &ak5558_dai, 1);
  331. if (ret)
  332. return ret;
  333. pm_runtime_enable(&i2c->dev);
  334. regcache_cache_only(ak5558->regmap, true);
  335. return 0;
  336. }
  337. static int ak5558_i2c_remove(struct i2c_client *i2c)
  338. {
  339. pm_runtime_disable(&i2c->dev);
  340. return 0;
  341. }
  342. static const struct of_device_id ak5558_i2c_dt_ids[] = {
  343. { .compatible = "asahi-kasei,ak5558"},
  344. { }
  345. };
  346. MODULE_DEVICE_TABLE(of, ak5558_i2c_dt_ids);
  347. static struct i2c_driver ak5558_i2c_driver = {
  348. .driver = {
  349. .name = "ak5558",
  350. .of_match_table = of_match_ptr(ak5558_i2c_dt_ids),
  351. .pm = &ak5558_pm,
  352. },
  353. .probe_new = ak5558_i2c_probe,
  354. .remove = ak5558_i2c_remove,
  355. };
  356. module_i2c_driver(ak5558_i2c_driver);
  357. MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
  358. MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
  359. MODULE_DESCRIPTION("ASoC AK5558 ADC driver");
  360. MODULE_LICENSE("GPL v2");