ak5558.c 13 KB

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