ak4613.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * ak4613.c -- Asahi Kasei ALSA Soc Audio driver
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corporation
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * Based on ak4642.c by Kuninori Morimoto
  8. * Based on wm8731.c by Richard Purdie
  9. * Based on ak4535.c by Richard Purdie
  10. * Based on wm8753.c by Liam Girdwood
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/i2c.h>
  18. #include <linux/slab.h>
  19. #include <linux/of_device.h>
  20. #include <linux/module.h>
  21. #include <linux/regmap.h>
  22. #include <sound/soc.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/tlv.h>
  25. #define PW_MGMT1 0x00 /* Power Management 1 */
  26. #define PW_MGMT2 0x01 /* Power Management 2 */
  27. #define PW_MGMT3 0x02 /* Power Management 3 */
  28. #define CTRL1 0x03 /* Control 1 */
  29. #define CTRL2 0x04 /* Control 2 */
  30. #define DEMP1 0x05 /* De-emphasis1 */
  31. #define DEMP2 0x06 /* De-emphasis2 */
  32. #define OFD 0x07 /* Overflow Detect */
  33. #define ZRD 0x08 /* Zero Detect */
  34. #define ICTRL 0x09 /* Input Control */
  35. #define OCTRL 0x0a /* Output Control */
  36. #define LOUT1 0x0b /* LOUT1 Volume Control */
  37. #define ROUT1 0x0c /* ROUT1 Volume Control */
  38. #define LOUT2 0x0d /* LOUT2 Volume Control */
  39. #define ROUT2 0x0e /* ROUT2 Volume Control */
  40. #define LOUT3 0x0f /* LOUT3 Volume Control */
  41. #define ROUT3 0x10 /* ROUT3 Volume Control */
  42. #define LOUT4 0x11 /* LOUT4 Volume Control */
  43. #define ROUT4 0x12 /* ROUT4 Volume Control */
  44. #define LOUT5 0x13 /* LOUT5 Volume Control */
  45. #define ROUT5 0x14 /* ROUT5 Volume Control */
  46. #define LOUT6 0x15 /* LOUT6 Volume Control */
  47. #define ROUT6 0x16 /* ROUT6 Volume Control */
  48. /* PW_MGMT1 */
  49. #define RSTN BIT(0)
  50. #define PMDAC BIT(1)
  51. #define PMADC BIT(2)
  52. #define PMVR BIT(3)
  53. /* PW_MGMT2 */
  54. #define PMAD_ALL 0x7
  55. /* PW_MGMT3 */
  56. #define PMDA_ALL 0x3f
  57. /* CTRL1 */
  58. #define DIF0 BIT(3)
  59. #define DIF1 BIT(4)
  60. #define DIF2 BIT(5)
  61. #define TDM0 BIT(6)
  62. #define TDM1 BIT(7)
  63. #define NO_FMT (0xff)
  64. #define FMT_MASK (0xf8)
  65. /* CTRL2 */
  66. #define DFS_MASK (3 << 2)
  67. #define DFS_NORMAL_SPEED (0 << 2)
  68. #define DFS_DOUBLE_SPEED (1 << 2)
  69. #define DFS_QUAD_SPEED (2 << 2)
  70. struct ak4613_formats {
  71. unsigned int width;
  72. unsigned int fmt;
  73. };
  74. struct ak4613_interface {
  75. struct ak4613_formats capture;
  76. struct ak4613_formats playback;
  77. };
  78. struct ak4613_priv {
  79. struct mutex lock;
  80. const struct ak4613_interface *iface;
  81. unsigned int fmt;
  82. u8 oc;
  83. u8 ic;
  84. int cnt;
  85. };
  86. /*
  87. * Playback Volume
  88. *
  89. * max : 0x00 : 0 dB
  90. * ( 0.5 dB step )
  91. * min : 0xFE : -127.0 dB
  92. * mute: 0xFF
  93. */
  94. static const DECLARE_TLV_DB_SCALE(out_tlv, -12750, 50, 1);
  95. static const struct snd_kcontrol_new ak4613_snd_controls[] = {
  96. SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1, ROUT1,
  97. 0, 0xFF, 1, out_tlv),
  98. SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2, ROUT2,
  99. 0, 0xFF, 1, out_tlv),
  100. SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3, ROUT3,
  101. 0, 0xFF, 1, out_tlv),
  102. SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4, ROUT4,
  103. 0, 0xFF, 1, out_tlv),
  104. SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5, ROUT5,
  105. 0, 0xFF, 1, out_tlv),
  106. SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6, ROUT6,
  107. 0, 0xFF, 1, out_tlv),
  108. };
  109. static const struct reg_default ak4613_reg[] = {
  110. { 0x0, 0x0f }, { 0x1, 0x07 }, { 0x2, 0x3f }, { 0x3, 0x20 },
  111. { 0x4, 0x20 }, { 0x5, 0x55 }, { 0x6, 0x05 }, { 0x7, 0x07 },
  112. { 0x8, 0x0f }, { 0x9, 0x07 }, { 0xa, 0x3f }, { 0xb, 0x00 },
  113. { 0xc, 0x00 }, { 0xd, 0x00 }, { 0xe, 0x00 }, { 0xf, 0x00 },
  114. { 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
  115. { 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
  116. };
  117. #define AUDIO_IFACE_TO_VAL(fmts) ((fmts - ak4613_iface) << 3)
  118. #define AUDIO_IFACE(b, fmt) { b, SND_SOC_DAIFMT_##fmt }
  119. static const struct ak4613_interface ak4613_iface[] = {
  120. /* capture */ /* playback */
  121. [0] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(16, RIGHT_J) },
  122. [1] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(20, RIGHT_J) },
  123. [2] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(24, RIGHT_J) },
  124. [3] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(24, LEFT_J) },
  125. [4] = { AUDIO_IFACE(24, I2S), AUDIO_IFACE(24, I2S) },
  126. };
  127. static const struct regmap_config ak4613_regmap_cfg = {
  128. .reg_bits = 8,
  129. .val_bits = 8,
  130. .max_register = 0x16,
  131. .reg_defaults = ak4613_reg,
  132. .num_reg_defaults = ARRAY_SIZE(ak4613_reg),
  133. .cache_type = REGCACHE_RBTREE,
  134. };
  135. static const struct of_device_id ak4613_of_match[] = {
  136. { .compatible = "asahi-kasei,ak4613", .data = &ak4613_regmap_cfg },
  137. {},
  138. };
  139. MODULE_DEVICE_TABLE(of, ak4613_of_match);
  140. static const struct i2c_device_id ak4613_i2c_id[] = {
  141. { "ak4613", (kernel_ulong_t)&ak4613_regmap_cfg },
  142. { }
  143. };
  144. MODULE_DEVICE_TABLE(i2c, ak4613_i2c_id);
  145. static const struct snd_soc_dapm_widget ak4613_dapm_widgets[] = {
  146. /* Outputs */
  147. SND_SOC_DAPM_OUTPUT("LOUT1"),
  148. SND_SOC_DAPM_OUTPUT("LOUT2"),
  149. SND_SOC_DAPM_OUTPUT("LOUT3"),
  150. SND_SOC_DAPM_OUTPUT("LOUT4"),
  151. SND_SOC_DAPM_OUTPUT("LOUT5"),
  152. SND_SOC_DAPM_OUTPUT("LOUT6"),
  153. SND_SOC_DAPM_OUTPUT("ROUT1"),
  154. SND_SOC_DAPM_OUTPUT("ROUT2"),
  155. SND_SOC_DAPM_OUTPUT("ROUT3"),
  156. SND_SOC_DAPM_OUTPUT("ROUT4"),
  157. SND_SOC_DAPM_OUTPUT("ROUT5"),
  158. SND_SOC_DAPM_OUTPUT("ROUT6"),
  159. /* Inputs */
  160. SND_SOC_DAPM_INPUT("LIN1"),
  161. SND_SOC_DAPM_INPUT("LIN2"),
  162. SND_SOC_DAPM_INPUT("RIN1"),
  163. SND_SOC_DAPM_INPUT("RIN2"),
  164. /* DAC */
  165. SND_SOC_DAPM_DAC("DAC1", NULL, PW_MGMT3, 0, 0),
  166. SND_SOC_DAPM_DAC("DAC2", NULL, PW_MGMT3, 1, 0),
  167. SND_SOC_DAPM_DAC("DAC3", NULL, PW_MGMT3, 2, 0),
  168. SND_SOC_DAPM_DAC("DAC4", NULL, PW_MGMT3, 3, 0),
  169. SND_SOC_DAPM_DAC("DAC5", NULL, PW_MGMT3, 4, 0),
  170. SND_SOC_DAPM_DAC("DAC6", NULL, PW_MGMT3, 5, 0),
  171. /* ADC */
  172. SND_SOC_DAPM_ADC("ADC1", NULL, PW_MGMT2, 0, 0),
  173. SND_SOC_DAPM_ADC("ADC2", NULL, PW_MGMT2, 1, 0),
  174. };
  175. static const struct snd_soc_dapm_route ak4613_intercon[] = {
  176. {"LOUT1", NULL, "DAC1"},
  177. {"LOUT2", NULL, "DAC2"},
  178. {"LOUT3", NULL, "DAC3"},
  179. {"LOUT4", NULL, "DAC4"},
  180. {"LOUT5", NULL, "DAC5"},
  181. {"LOUT6", NULL, "DAC6"},
  182. {"ROUT1", NULL, "DAC1"},
  183. {"ROUT2", NULL, "DAC2"},
  184. {"ROUT3", NULL, "DAC3"},
  185. {"ROUT4", NULL, "DAC4"},
  186. {"ROUT5", NULL, "DAC5"},
  187. {"ROUT6", NULL, "DAC6"},
  188. {"DAC1", NULL, "Playback"},
  189. {"DAC2", NULL, "Playback"},
  190. {"DAC3", NULL, "Playback"},
  191. {"DAC4", NULL, "Playback"},
  192. {"DAC5", NULL, "Playback"},
  193. {"DAC6", NULL, "Playback"},
  194. {"Capture", NULL, "ADC1"},
  195. {"Capture", NULL, "ADC2"},
  196. {"ADC1", NULL, "LIN1"},
  197. {"ADC2", NULL, "LIN2"},
  198. {"ADC1", NULL, "RIN1"},
  199. {"ADC2", NULL, "RIN2"},
  200. };
  201. static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
  202. struct snd_soc_dai *dai)
  203. {
  204. struct snd_soc_codec *codec = dai->codec;
  205. struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
  206. struct device *dev = codec->dev;
  207. mutex_lock(&priv->lock);
  208. priv->cnt--;
  209. if (priv->cnt < 0) {
  210. dev_err(dev, "unexpected counter error\n");
  211. priv->cnt = 0;
  212. }
  213. if (!priv->cnt)
  214. priv->iface = NULL;
  215. mutex_unlock(&priv->lock);
  216. }
  217. static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  218. {
  219. struct snd_soc_codec *codec = dai->codec;
  220. struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
  221. fmt &= SND_SOC_DAIFMT_FORMAT_MASK;
  222. switch (fmt) {
  223. case SND_SOC_DAIFMT_RIGHT_J:
  224. case SND_SOC_DAIFMT_LEFT_J:
  225. case SND_SOC_DAIFMT_I2S:
  226. priv->fmt = fmt;
  227. break;
  228. default:
  229. return -EINVAL;
  230. }
  231. return 0;
  232. }
  233. static bool ak4613_dai_fmt_matching(const struct ak4613_interface *iface,
  234. int is_play,
  235. unsigned int fmt, unsigned int width)
  236. {
  237. const struct ak4613_formats *fmts;
  238. fmts = (is_play) ? &iface->playback : &iface->capture;
  239. if (fmts->fmt != fmt)
  240. return false;
  241. if (fmt == SND_SOC_DAIFMT_RIGHT_J) {
  242. if (fmts->width != width)
  243. return false;
  244. } else {
  245. if (fmts->width < width)
  246. return false;
  247. }
  248. return true;
  249. }
  250. static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
  251. struct snd_pcm_hw_params *params,
  252. struct snd_soc_dai *dai)
  253. {
  254. struct snd_soc_codec *codec = dai->codec;
  255. struct ak4613_priv *priv = snd_soc_codec_get_drvdata(codec);
  256. const struct ak4613_interface *iface;
  257. struct device *dev = codec->dev;
  258. unsigned int width = params_width(params);
  259. unsigned int fmt = priv->fmt;
  260. unsigned int rate;
  261. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  262. int i, ret;
  263. u8 fmt_ctrl, ctrl2;
  264. rate = params_rate(params);
  265. switch (rate) {
  266. case 32000:
  267. case 44100:
  268. case 48000:
  269. ctrl2 = DFS_NORMAL_SPEED;
  270. break;
  271. case 88200:
  272. case 96000:
  273. ctrl2 = DFS_DOUBLE_SPEED;
  274. break;
  275. case 176400:
  276. case 192000:
  277. ctrl2 = DFS_QUAD_SPEED;
  278. break;
  279. default:
  280. return -EINVAL;
  281. }
  282. /*
  283. * FIXME
  284. *
  285. * It doesn't support TDM at this point
  286. */
  287. fmt_ctrl = NO_FMT;
  288. ret = -EINVAL;
  289. iface = NULL;
  290. mutex_lock(&priv->lock);
  291. if (priv->iface) {
  292. if (ak4613_dai_fmt_matching(priv->iface, is_play, fmt, width))
  293. iface = priv->iface;
  294. } else {
  295. for (i = ARRAY_SIZE(ak4613_iface); i >= 0; i--) {
  296. if (!ak4613_dai_fmt_matching(ak4613_iface + i,
  297. is_play,
  298. fmt, width))
  299. continue;
  300. iface = ak4613_iface + i;
  301. break;
  302. }
  303. }
  304. if ((priv->iface == NULL) ||
  305. (priv->iface == iface)) {
  306. priv->iface = iface;
  307. priv->cnt++;
  308. ret = 0;
  309. }
  310. mutex_unlock(&priv->lock);
  311. if (ret < 0)
  312. goto hw_params_end;
  313. fmt_ctrl = AUDIO_IFACE_TO_VAL(iface);
  314. snd_soc_update_bits(codec, CTRL1, FMT_MASK, fmt_ctrl);
  315. snd_soc_update_bits(codec, CTRL2, DFS_MASK, ctrl2);
  316. snd_soc_write(codec, ICTRL, priv->ic);
  317. snd_soc_write(codec, OCTRL, priv->oc);
  318. hw_params_end:
  319. if (ret < 0)
  320. dev_warn(dev, "unsupported data width/format combination\n");
  321. return ret;
  322. }
  323. static int ak4613_set_bias_level(struct snd_soc_codec *codec,
  324. enum snd_soc_bias_level level)
  325. {
  326. u8 mgmt1 = 0;
  327. switch (level) {
  328. case SND_SOC_BIAS_ON:
  329. mgmt1 |= RSTN;
  330. /* fall through */
  331. case SND_SOC_BIAS_PREPARE:
  332. mgmt1 |= PMADC | PMDAC;
  333. /* fall through */
  334. case SND_SOC_BIAS_STANDBY:
  335. mgmt1 |= PMVR;
  336. /* fall through */
  337. case SND_SOC_BIAS_OFF:
  338. default:
  339. break;
  340. }
  341. snd_soc_write(codec, PW_MGMT1, mgmt1);
  342. return 0;
  343. }
  344. static const struct snd_soc_dai_ops ak4613_dai_ops = {
  345. .shutdown = ak4613_dai_shutdown,
  346. .set_fmt = ak4613_dai_set_fmt,
  347. .hw_params = ak4613_dai_hw_params,
  348. };
  349. #define AK4613_PCM_RATE (SNDRV_PCM_RATE_32000 |\
  350. SNDRV_PCM_RATE_44100 |\
  351. SNDRV_PCM_RATE_48000 |\
  352. SNDRV_PCM_RATE_64000 |\
  353. SNDRV_PCM_RATE_88200 |\
  354. SNDRV_PCM_RATE_96000 |\
  355. SNDRV_PCM_RATE_176400 |\
  356. SNDRV_PCM_RATE_192000)
  357. #define AK4613_PCM_FMTBIT (SNDRV_PCM_FMTBIT_S16_LE |\
  358. SNDRV_PCM_FMTBIT_S24_LE)
  359. static struct snd_soc_dai_driver ak4613_dai = {
  360. .name = "ak4613-hifi",
  361. .playback = {
  362. .stream_name = "Playback",
  363. .channels_min = 2,
  364. .channels_max = 2,
  365. .rates = AK4613_PCM_RATE,
  366. .formats = AK4613_PCM_FMTBIT,
  367. },
  368. .capture = {
  369. .stream_name = "Capture",
  370. .channels_min = 2,
  371. .channels_max = 2,
  372. .rates = AK4613_PCM_RATE,
  373. .formats = AK4613_PCM_FMTBIT,
  374. },
  375. .ops = &ak4613_dai_ops,
  376. .symmetric_rates = 1,
  377. };
  378. static int ak4613_suspend(struct snd_soc_codec *codec)
  379. {
  380. struct regmap *regmap = dev_get_regmap(codec->dev, NULL);
  381. regcache_cache_only(regmap, true);
  382. regcache_mark_dirty(regmap);
  383. return 0;
  384. }
  385. static int ak4613_resume(struct snd_soc_codec *codec)
  386. {
  387. struct regmap *regmap = dev_get_regmap(codec->dev, NULL);
  388. regcache_cache_only(regmap, false);
  389. return regcache_sync(regmap);
  390. }
  391. static struct snd_soc_codec_driver soc_codec_dev_ak4613 = {
  392. .suspend = ak4613_suspend,
  393. .resume = ak4613_resume,
  394. .set_bias_level = ak4613_set_bias_level,
  395. .component_driver = {
  396. .controls = ak4613_snd_controls,
  397. .num_controls = ARRAY_SIZE(ak4613_snd_controls),
  398. .dapm_widgets = ak4613_dapm_widgets,
  399. .num_dapm_widgets = ARRAY_SIZE(ak4613_dapm_widgets),
  400. .dapm_routes = ak4613_intercon,
  401. .num_dapm_routes = ARRAY_SIZE(ak4613_intercon),
  402. },
  403. };
  404. static void ak4613_parse_of(struct ak4613_priv *priv,
  405. struct device *dev)
  406. {
  407. struct device_node *np = dev->of_node;
  408. char prop[32];
  409. int i;
  410. /* Input 1 - 2 */
  411. for (i = 0; i < 2; i++) {
  412. snprintf(prop, sizeof(prop), "asahi-kasei,in%d-single-end", i + 1);
  413. if (!of_get_property(np, prop, NULL))
  414. priv->ic |= 1 << i;
  415. }
  416. /* Output 1 - 6 */
  417. for (i = 0; i < 6; i++) {
  418. snprintf(prop, sizeof(prop), "asahi-kasei,out%d-single-end", i + 1);
  419. if (!of_get_property(np, prop, NULL))
  420. priv->oc |= 1 << i;
  421. }
  422. }
  423. static int ak4613_i2c_probe(struct i2c_client *i2c,
  424. const struct i2c_device_id *id)
  425. {
  426. struct device *dev = &i2c->dev;
  427. struct device_node *np = dev->of_node;
  428. const struct regmap_config *regmap_cfg;
  429. struct regmap *regmap;
  430. struct ak4613_priv *priv;
  431. regmap_cfg = NULL;
  432. if (np) {
  433. const struct of_device_id *of_id;
  434. of_id = of_match_device(ak4613_of_match, dev);
  435. if (of_id)
  436. regmap_cfg = of_id->data;
  437. } else {
  438. regmap_cfg = (const struct regmap_config *)id->driver_data;
  439. }
  440. if (!regmap_cfg)
  441. return -EINVAL;
  442. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  443. if (!priv)
  444. return -ENOMEM;
  445. ak4613_parse_of(priv, dev);
  446. priv->iface = NULL;
  447. priv->cnt = 0;
  448. mutex_init(&priv->lock);
  449. i2c_set_clientdata(i2c, priv);
  450. regmap = devm_regmap_init_i2c(i2c, regmap_cfg);
  451. if (IS_ERR(regmap))
  452. return PTR_ERR(regmap);
  453. return snd_soc_register_codec(dev, &soc_codec_dev_ak4613,
  454. &ak4613_dai, 1);
  455. }
  456. static int ak4613_i2c_remove(struct i2c_client *client)
  457. {
  458. snd_soc_unregister_codec(&client->dev);
  459. return 0;
  460. }
  461. static struct i2c_driver ak4613_i2c_driver = {
  462. .driver = {
  463. .name = "ak4613-codec",
  464. .of_match_table = ak4613_of_match,
  465. },
  466. .probe = ak4613_i2c_probe,
  467. .remove = ak4613_i2c_remove,
  468. .id_table = ak4613_i2c_id,
  469. };
  470. module_i2c_driver(ak4613_i2c_driver);
  471. MODULE_DESCRIPTION("Soc AK4613 driver");
  472. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  473. MODULE_LICENSE("GPL v2");