ak4613.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // ak4613.c -- Asahi Kasei ALSA Soc Audio driver
  4. //
  5. // Copyright (C) 2015 Renesas Electronics Corporation
  6. // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. //
  8. // Based on ak4642.c by Kuninori Morimoto
  9. // Based on wm8731.c by Richard Purdie
  10. // Based on ak4535.c by Richard Purdie
  11. // Based on wm8753.c by Liam Girdwood
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/i2c.h>
  15. #include <linux/slab.h>
  16. #include <linux/of_device.h>
  17. #include <linux/module.h>
  18. #include <linux/regmap.h>
  19. #include <sound/soc.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/tlv.h>
  22. #define PW_MGMT1 0x00 /* Power Management 1 */
  23. #define PW_MGMT2 0x01 /* Power Management 2 */
  24. #define PW_MGMT3 0x02 /* Power Management 3 */
  25. #define CTRL1 0x03 /* Control 1 */
  26. #define CTRL2 0x04 /* Control 2 */
  27. #define DEMP1 0x05 /* De-emphasis1 */
  28. #define DEMP2 0x06 /* De-emphasis2 */
  29. #define OFD 0x07 /* Overflow Detect */
  30. #define ZRD 0x08 /* Zero Detect */
  31. #define ICTRL 0x09 /* Input Control */
  32. #define OCTRL 0x0a /* Output Control */
  33. #define LOUT1 0x0b /* LOUT1 Volume Control */
  34. #define ROUT1 0x0c /* ROUT1 Volume Control */
  35. #define LOUT2 0x0d /* LOUT2 Volume Control */
  36. #define ROUT2 0x0e /* ROUT2 Volume Control */
  37. #define LOUT3 0x0f /* LOUT3 Volume Control */
  38. #define ROUT3 0x10 /* ROUT3 Volume Control */
  39. #define LOUT4 0x11 /* LOUT4 Volume Control */
  40. #define ROUT4 0x12 /* ROUT4 Volume Control */
  41. #define LOUT5 0x13 /* LOUT5 Volume Control */
  42. #define ROUT5 0x14 /* ROUT5 Volume Control */
  43. #define LOUT6 0x15 /* LOUT6 Volume Control */
  44. #define ROUT6 0x16 /* ROUT6 Volume Control */
  45. /* PW_MGMT1 */
  46. #define RSTN BIT(0)
  47. #define PMDAC BIT(1)
  48. #define PMADC BIT(2)
  49. #define PMVR BIT(3)
  50. /* PW_MGMT2 */
  51. #define PMAD_ALL 0x7
  52. /* PW_MGMT3 */
  53. #define PMDA_ALL 0x3f
  54. /* CTRL1 */
  55. #define DIF0 BIT(3)
  56. #define DIF1 BIT(4)
  57. #define DIF2 BIT(5)
  58. #define TDM0 BIT(6)
  59. #define TDM1 BIT(7)
  60. #define NO_FMT (0xff)
  61. #define FMT_MASK (0xf8)
  62. /* CTRL2 */
  63. #define DFS_MASK (3 << 2)
  64. #define DFS_NORMAL_SPEED (0 << 2)
  65. #define DFS_DOUBLE_SPEED (1 << 2)
  66. #define DFS_QUAD_SPEED (2 << 2)
  67. /* ICTRL */
  68. #define ICTRL_MASK (0x3)
  69. /* OCTRL */
  70. #define OCTRL_MASK (0x3F)
  71. struct ak4613_formats {
  72. unsigned int width;
  73. unsigned int fmt;
  74. };
  75. struct ak4613_interface {
  76. struct ak4613_formats capture;
  77. struct ak4613_formats playback;
  78. };
  79. struct ak4613_priv {
  80. struct mutex lock;
  81. const struct ak4613_interface *iface;
  82. struct snd_pcm_hw_constraint_list constraint;
  83. struct work_struct dummy_write_work;
  84. struct snd_soc_component *component;
  85. unsigned int rate;
  86. unsigned int sysclk;
  87. unsigned int fmt;
  88. u8 oc;
  89. u8 ic;
  90. int cnt;
  91. };
  92. /*
  93. * Playback Volume
  94. *
  95. * max : 0x00 : 0 dB
  96. * ( 0.5 dB step )
  97. * min : 0xFE : -127.0 dB
  98. * mute: 0xFF
  99. */
  100. static const DECLARE_TLV_DB_SCALE(out_tlv, -12750, 50, 1);
  101. static const struct snd_kcontrol_new ak4613_snd_controls[] = {
  102. SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1, ROUT1,
  103. 0, 0xFF, 1, out_tlv),
  104. SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2, ROUT2,
  105. 0, 0xFF, 1, out_tlv),
  106. SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3, ROUT3,
  107. 0, 0xFF, 1, out_tlv),
  108. SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4, ROUT4,
  109. 0, 0xFF, 1, out_tlv),
  110. SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5, ROUT5,
  111. 0, 0xFF, 1, out_tlv),
  112. SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6, ROUT6,
  113. 0, 0xFF, 1, out_tlv),
  114. };
  115. static const struct reg_default ak4613_reg[] = {
  116. { 0x0, 0x0f }, { 0x1, 0x07 }, { 0x2, 0x3f }, { 0x3, 0x20 },
  117. { 0x4, 0x20 }, { 0x5, 0x55 }, { 0x6, 0x05 }, { 0x7, 0x07 },
  118. { 0x8, 0x0f }, { 0x9, 0x07 }, { 0xa, 0x3f }, { 0xb, 0x00 },
  119. { 0xc, 0x00 }, { 0xd, 0x00 }, { 0xe, 0x00 }, { 0xf, 0x00 },
  120. { 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
  121. { 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
  122. };
  123. #define AUDIO_IFACE_TO_VAL(fmts) ((fmts - ak4613_iface) << 3)
  124. #define AUDIO_IFACE(b, fmt) { b, SND_SOC_DAIFMT_##fmt }
  125. static const struct ak4613_interface ak4613_iface[] = {
  126. /* capture */ /* playback */
  127. /* [0] - [2] are not supported */
  128. [3] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(24, LEFT_J) },
  129. [4] = { AUDIO_IFACE(24, I2S), AUDIO_IFACE(24, I2S) },
  130. };
  131. static const struct regmap_config ak4613_regmap_cfg = {
  132. .reg_bits = 8,
  133. .val_bits = 8,
  134. .max_register = 0x16,
  135. .reg_defaults = ak4613_reg,
  136. .num_reg_defaults = ARRAY_SIZE(ak4613_reg),
  137. .cache_type = REGCACHE_RBTREE,
  138. };
  139. static const struct of_device_id ak4613_of_match[] = {
  140. { .compatible = "asahi-kasei,ak4613", .data = &ak4613_regmap_cfg },
  141. {},
  142. };
  143. MODULE_DEVICE_TABLE(of, ak4613_of_match);
  144. static const struct i2c_device_id ak4613_i2c_id[] = {
  145. { "ak4613", (kernel_ulong_t)&ak4613_regmap_cfg },
  146. { }
  147. };
  148. MODULE_DEVICE_TABLE(i2c, ak4613_i2c_id);
  149. static const struct snd_soc_dapm_widget ak4613_dapm_widgets[] = {
  150. /* Outputs */
  151. SND_SOC_DAPM_OUTPUT("LOUT1"),
  152. SND_SOC_DAPM_OUTPUT("LOUT2"),
  153. SND_SOC_DAPM_OUTPUT("LOUT3"),
  154. SND_SOC_DAPM_OUTPUT("LOUT4"),
  155. SND_SOC_DAPM_OUTPUT("LOUT5"),
  156. SND_SOC_DAPM_OUTPUT("LOUT6"),
  157. SND_SOC_DAPM_OUTPUT("ROUT1"),
  158. SND_SOC_DAPM_OUTPUT("ROUT2"),
  159. SND_SOC_DAPM_OUTPUT("ROUT3"),
  160. SND_SOC_DAPM_OUTPUT("ROUT4"),
  161. SND_SOC_DAPM_OUTPUT("ROUT5"),
  162. SND_SOC_DAPM_OUTPUT("ROUT6"),
  163. /* Inputs */
  164. SND_SOC_DAPM_INPUT("LIN1"),
  165. SND_SOC_DAPM_INPUT("LIN2"),
  166. SND_SOC_DAPM_INPUT("RIN1"),
  167. SND_SOC_DAPM_INPUT("RIN2"),
  168. /* DAC */
  169. SND_SOC_DAPM_DAC("DAC1", NULL, PW_MGMT3, 0, 0),
  170. SND_SOC_DAPM_DAC("DAC2", NULL, PW_MGMT3, 1, 0),
  171. SND_SOC_DAPM_DAC("DAC3", NULL, PW_MGMT3, 2, 0),
  172. SND_SOC_DAPM_DAC("DAC4", NULL, PW_MGMT3, 3, 0),
  173. SND_SOC_DAPM_DAC("DAC5", NULL, PW_MGMT3, 4, 0),
  174. SND_SOC_DAPM_DAC("DAC6", NULL, PW_MGMT3, 5, 0),
  175. /* ADC */
  176. SND_SOC_DAPM_ADC("ADC1", NULL, PW_MGMT2, 0, 0),
  177. SND_SOC_DAPM_ADC("ADC2", NULL, PW_MGMT2, 1, 0),
  178. };
  179. static const struct snd_soc_dapm_route ak4613_intercon[] = {
  180. {"LOUT1", NULL, "DAC1"},
  181. {"LOUT2", NULL, "DAC2"},
  182. {"LOUT3", NULL, "DAC3"},
  183. {"LOUT4", NULL, "DAC4"},
  184. {"LOUT5", NULL, "DAC5"},
  185. {"LOUT6", NULL, "DAC6"},
  186. {"ROUT1", NULL, "DAC1"},
  187. {"ROUT2", NULL, "DAC2"},
  188. {"ROUT3", NULL, "DAC3"},
  189. {"ROUT4", NULL, "DAC4"},
  190. {"ROUT5", NULL, "DAC5"},
  191. {"ROUT6", NULL, "DAC6"},
  192. {"DAC1", NULL, "Playback"},
  193. {"DAC2", NULL, "Playback"},
  194. {"DAC3", NULL, "Playback"},
  195. {"DAC4", NULL, "Playback"},
  196. {"DAC5", NULL, "Playback"},
  197. {"DAC6", NULL, "Playback"},
  198. {"Capture", NULL, "ADC1"},
  199. {"Capture", NULL, "ADC2"},
  200. {"ADC1", NULL, "LIN1"},
  201. {"ADC2", NULL, "LIN2"},
  202. {"ADC1", NULL, "RIN1"},
  203. {"ADC2", NULL, "RIN2"},
  204. };
  205. static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
  206. struct snd_soc_dai *dai)
  207. {
  208. struct snd_soc_component *component = dai->component;
  209. struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
  210. struct device *dev = component->dev;
  211. mutex_lock(&priv->lock);
  212. priv->cnt--;
  213. if (priv->cnt < 0) {
  214. dev_err(dev, "unexpected counter error\n");
  215. priv->cnt = 0;
  216. }
  217. if (!priv->cnt)
  218. priv->iface = NULL;
  219. mutex_unlock(&priv->lock);
  220. }
  221. static void ak4613_hw_constraints(struct ak4613_priv *priv,
  222. struct snd_pcm_runtime *runtime)
  223. {
  224. static const unsigned int ak4613_rates[] = {
  225. 32000,
  226. 44100,
  227. 48000,
  228. 64000,
  229. 88200,
  230. 96000,
  231. 176400,
  232. 192000,
  233. };
  234. struct snd_pcm_hw_constraint_list *constraint = &priv->constraint;
  235. unsigned int fs;
  236. int i;
  237. constraint->list = ak4613_rates;
  238. constraint->mask = 0;
  239. constraint->count = 0;
  240. /*
  241. * Slave Mode
  242. * Normal: [32kHz, 48kHz] : 256fs,384fs or 512fs
  243. * Double: [64kHz, 96kHz] : 256fs
  244. * Quad : [128kHz,192kHz]: 128fs
  245. *
  246. * Master mode
  247. * Normal: [32kHz, 48kHz] : 256fs or 512fs
  248. * Double: [64kHz, 96kHz] : 256fs
  249. * Quad : [128kHz,192kHz]: 128fs
  250. */
  251. for (i = 0; i < ARRAY_SIZE(ak4613_rates); i++) {
  252. /* minimum fs on each range */
  253. fs = (ak4613_rates[i] <= 96000) ? 256 : 128;
  254. if (priv->sysclk >= ak4613_rates[i] * fs)
  255. constraint->count = i + 1;
  256. }
  257. snd_pcm_hw_constraint_list(runtime, 0,
  258. SNDRV_PCM_HW_PARAM_RATE, constraint);
  259. }
  260. static int ak4613_dai_startup(struct snd_pcm_substream *substream,
  261. struct snd_soc_dai *dai)
  262. {
  263. struct snd_soc_component *component = dai->component;
  264. struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
  265. priv->cnt++;
  266. ak4613_hw_constraints(priv, substream->runtime);
  267. return 0;
  268. }
  269. static int ak4613_dai_set_sysclk(struct snd_soc_dai *codec_dai,
  270. int clk_id, unsigned int freq, int dir)
  271. {
  272. struct snd_soc_component *component = codec_dai->component;
  273. struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
  274. priv->sysclk = freq;
  275. return 0;
  276. }
  277. static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  278. {
  279. struct snd_soc_component *component = dai->component;
  280. struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
  281. fmt &= SND_SOC_DAIFMT_FORMAT_MASK;
  282. switch (fmt) {
  283. case SND_SOC_DAIFMT_LEFT_J:
  284. case SND_SOC_DAIFMT_I2S:
  285. priv->fmt = fmt;
  286. break;
  287. default:
  288. return -EINVAL;
  289. }
  290. return 0;
  291. }
  292. static bool ak4613_dai_fmt_matching(const struct ak4613_interface *iface,
  293. int is_play,
  294. unsigned int fmt, unsigned int width)
  295. {
  296. const struct ak4613_formats *fmts;
  297. fmts = (is_play) ? &iface->playback : &iface->capture;
  298. if (fmts->fmt != fmt)
  299. return false;
  300. if (fmts->width != width)
  301. return false;
  302. return true;
  303. }
  304. static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
  305. struct snd_pcm_hw_params *params,
  306. struct snd_soc_dai *dai)
  307. {
  308. struct snd_soc_component *component = dai->component;
  309. struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
  310. const struct ak4613_interface *iface;
  311. struct device *dev = component->dev;
  312. unsigned int width = params_width(params);
  313. unsigned int fmt = priv->fmt;
  314. unsigned int rate;
  315. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  316. int i, ret;
  317. u8 fmt_ctrl, ctrl2;
  318. rate = params_rate(params);
  319. switch (rate) {
  320. case 32000:
  321. case 44100:
  322. case 48000:
  323. ctrl2 = DFS_NORMAL_SPEED;
  324. break;
  325. case 64000:
  326. case 88200:
  327. case 96000:
  328. ctrl2 = DFS_DOUBLE_SPEED;
  329. break;
  330. case 176400:
  331. case 192000:
  332. ctrl2 = DFS_QUAD_SPEED;
  333. break;
  334. default:
  335. return -EINVAL;
  336. }
  337. priv->rate = rate;
  338. /*
  339. * FIXME
  340. *
  341. * It doesn't support TDM at this point
  342. */
  343. fmt_ctrl = NO_FMT;
  344. ret = -EINVAL;
  345. iface = NULL;
  346. mutex_lock(&priv->lock);
  347. if (priv->iface) {
  348. if (ak4613_dai_fmt_matching(priv->iface, is_play, fmt, width))
  349. iface = priv->iface;
  350. } else {
  351. for (i = ARRAY_SIZE(ak4613_iface) - 1; i >= 0; i--) {
  352. if (!ak4613_dai_fmt_matching(ak4613_iface + i,
  353. is_play,
  354. fmt, width))
  355. continue;
  356. iface = ak4613_iface + i;
  357. break;
  358. }
  359. }
  360. if ((priv->iface == NULL) ||
  361. (priv->iface == iface)) {
  362. priv->iface = iface;
  363. ret = 0;
  364. }
  365. mutex_unlock(&priv->lock);
  366. if (ret < 0)
  367. goto hw_params_end;
  368. fmt_ctrl = AUDIO_IFACE_TO_VAL(iface);
  369. snd_soc_component_update_bits(component, CTRL1, FMT_MASK, fmt_ctrl);
  370. snd_soc_component_update_bits(component, CTRL2, DFS_MASK, ctrl2);
  371. snd_soc_component_update_bits(component, ICTRL, ICTRL_MASK, priv->ic);
  372. snd_soc_component_update_bits(component, OCTRL, OCTRL_MASK, priv->oc);
  373. hw_params_end:
  374. if (ret < 0)
  375. dev_warn(dev, "unsupported data width/format combination\n");
  376. return ret;
  377. }
  378. static int ak4613_set_bias_level(struct snd_soc_component *component,
  379. enum snd_soc_bias_level level)
  380. {
  381. u8 mgmt1 = 0;
  382. switch (level) {
  383. case SND_SOC_BIAS_ON:
  384. mgmt1 |= RSTN;
  385. fallthrough;
  386. case SND_SOC_BIAS_PREPARE:
  387. mgmt1 |= PMADC | PMDAC;
  388. fallthrough;
  389. case SND_SOC_BIAS_STANDBY:
  390. mgmt1 |= PMVR;
  391. fallthrough;
  392. case SND_SOC_BIAS_OFF:
  393. default:
  394. break;
  395. }
  396. snd_soc_component_write(component, PW_MGMT1, mgmt1);
  397. return 0;
  398. }
  399. static void ak4613_dummy_write(struct work_struct *work)
  400. {
  401. struct ak4613_priv *priv = container_of(work,
  402. struct ak4613_priv,
  403. dummy_write_work);
  404. struct snd_soc_component *component = priv->component;
  405. unsigned int mgmt1;
  406. unsigned int mgmt3;
  407. /*
  408. * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
  409. *
  410. * Note
  411. *
  412. * To avoid extra delay, we want to avoid preemption here,
  413. * but we can't. Because it uses I2C access which is using IRQ
  414. * and sleep. Thus, delay might be more than 5 LR clocks
  415. * see also
  416. * ak4613_dai_trigger()
  417. */
  418. udelay(5000000 / priv->rate);
  419. mgmt1 = snd_soc_component_read(component, PW_MGMT1);
  420. mgmt3 = snd_soc_component_read(component, PW_MGMT3);
  421. snd_soc_component_write(component, PW_MGMT1, mgmt1);
  422. snd_soc_component_write(component, PW_MGMT3, mgmt3);
  423. }
  424. static int ak4613_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  425. struct snd_soc_dai *dai)
  426. {
  427. struct snd_soc_component *component = dai->component;
  428. struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
  429. /*
  430. * FIXME
  431. *
  432. * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
  433. * from Power Down Release. Otherwise, Playback volume will be 0dB.
  434. * To avoid complex multiple delay/dummy_write method from
  435. * ak4613_set_bias_level() / SND_SOC_DAPM_DAC_E("DACx", ...),
  436. * call it once here.
  437. *
  438. * But, unfortunately, we can't "write" here because here is atomic
  439. * context (It uses I2C access for writing).
  440. * Thus, use schedule_work() to switching to normal context
  441. * immediately.
  442. *
  443. * Note
  444. *
  445. * Calling ak4613_dummy_write() function might be delayed.
  446. * In such case, ak4613 volume might be temporarily 0dB when
  447. * beggining of playback.
  448. * see also
  449. * ak4613_dummy_write()
  450. */
  451. if ((cmd != SNDRV_PCM_TRIGGER_START) &&
  452. (cmd != SNDRV_PCM_TRIGGER_RESUME))
  453. return 0;
  454. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  455. return 0;
  456. priv->component = component;
  457. schedule_work(&priv->dummy_write_work);
  458. return 0;
  459. }
  460. static const struct snd_soc_dai_ops ak4613_dai_ops = {
  461. .startup = ak4613_dai_startup,
  462. .shutdown = ak4613_dai_shutdown,
  463. .set_sysclk = ak4613_dai_set_sysclk,
  464. .set_fmt = ak4613_dai_set_fmt,
  465. .trigger = ak4613_dai_trigger,
  466. .hw_params = ak4613_dai_hw_params,
  467. };
  468. #define AK4613_PCM_RATE (SNDRV_PCM_RATE_32000 |\
  469. SNDRV_PCM_RATE_44100 |\
  470. SNDRV_PCM_RATE_48000 |\
  471. SNDRV_PCM_RATE_64000 |\
  472. SNDRV_PCM_RATE_88200 |\
  473. SNDRV_PCM_RATE_96000 |\
  474. SNDRV_PCM_RATE_176400 |\
  475. SNDRV_PCM_RATE_192000)
  476. #define AK4613_PCM_FMTBIT (SNDRV_PCM_FMTBIT_S24_LE)
  477. static struct snd_soc_dai_driver ak4613_dai = {
  478. .name = "ak4613-hifi",
  479. .playback = {
  480. .stream_name = "Playback",
  481. .channels_min = 2,
  482. .channels_max = 2,
  483. .rates = AK4613_PCM_RATE,
  484. .formats = AK4613_PCM_FMTBIT,
  485. },
  486. .capture = {
  487. .stream_name = "Capture",
  488. .channels_min = 2,
  489. .channels_max = 2,
  490. .rates = AK4613_PCM_RATE,
  491. .formats = AK4613_PCM_FMTBIT,
  492. },
  493. .ops = &ak4613_dai_ops,
  494. .symmetric_rates = 1,
  495. };
  496. static int ak4613_suspend(struct snd_soc_component *component)
  497. {
  498. struct regmap *regmap = dev_get_regmap(component->dev, NULL);
  499. regcache_cache_only(regmap, true);
  500. regcache_mark_dirty(regmap);
  501. return 0;
  502. }
  503. static int ak4613_resume(struct snd_soc_component *component)
  504. {
  505. struct regmap *regmap = dev_get_regmap(component->dev, NULL);
  506. regcache_cache_only(regmap, false);
  507. return regcache_sync(regmap);
  508. }
  509. static const struct snd_soc_component_driver soc_component_dev_ak4613 = {
  510. .suspend = ak4613_suspend,
  511. .resume = ak4613_resume,
  512. .set_bias_level = ak4613_set_bias_level,
  513. .controls = ak4613_snd_controls,
  514. .num_controls = ARRAY_SIZE(ak4613_snd_controls),
  515. .dapm_widgets = ak4613_dapm_widgets,
  516. .num_dapm_widgets = ARRAY_SIZE(ak4613_dapm_widgets),
  517. .dapm_routes = ak4613_intercon,
  518. .num_dapm_routes = ARRAY_SIZE(ak4613_intercon),
  519. .idle_bias_on = 1,
  520. .endianness = 1,
  521. .non_legacy_dai_naming = 1,
  522. };
  523. static void ak4613_parse_of(struct ak4613_priv *priv,
  524. struct device *dev)
  525. {
  526. struct device_node *np = dev->of_node;
  527. char prop[32];
  528. int i;
  529. /* Input 1 - 2 */
  530. for (i = 0; i < 2; i++) {
  531. snprintf(prop, sizeof(prop), "asahi-kasei,in%d-single-end", i + 1);
  532. if (!of_get_property(np, prop, NULL))
  533. priv->ic |= 1 << i;
  534. }
  535. /* Output 1 - 6 */
  536. for (i = 0; i < 6; i++) {
  537. snprintf(prop, sizeof(prop), "asahi-kasei,out%d-single-end", i + 1);
  538. if (!of_get_property(np, prop, NULL))
  539. priv->oc |= 1 << i;
  540. }
  541. }
  542. static int ak4613_i2c_probe(struct i2c_client *i2c,
  543. const struct i2c_device_id *id)
  544. {
  545. struct device *dev = &i2c->dev;
  546. struct device_node *np = dev->of_node;
  547. const struct regmap_config *regmap_cfg;
  548. struct regmap *regmap;
  549. struct ak4613_priv *priv;
  550. regmap_cfg = NULL;
  551. if (np) {
  552. const struct of_device_id *of_id;
  553. of_id = of_match_device(ak4613_of_match, dev);
  554. if (of_id)
  555. regmap_cfg = of_id->data;
  556. } else {
  557. regmap_cfg = (const struct regmap_config *)id->driver_data;
  558. }
  559. if (!regmap_cfg)
  560. return -EINVAL;
  561. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  562. if (!priv)
  563. return -ENOMEM;
  564. ak4613_parse_of(priv, dev);
  565. priv->iface = NULL;
  566. priv->cnt = 0;
  567. priv->sysclk = 0;
  568. INIT_WORK(&priv->dummy_write_work, ak4613_dummy_write);
  569. mutex_init(&priv->lock);
  570. i2c_set_clientdata(i2c, priv);
  571. regmap = devm_regmap_init_i2c(i2c, regmap_cfg);
  572. if (IS_ERR(regmap))
  573. return PTR_ERR(regmap);
  574. return devm_snd_soc_register_component(dev, &soc_component_dev_ak4613,
  575. &ak4613_dai, 1);
  576. }
  577. static int ak4613_i2c_remove(struct i2c_client *client)
  578. {
  579. return 0;
  580. }
  581. static struct i2c_driver ak4613_i2c_driver = {
  582. .driver = {
  583. .name = "ak4613-codec",
  584. .of_match_table = ak4613_of_match,
  585. },
  586. .probe = ak4613_i2c_probe,
  587. .remove = ak4613_i2c_remove,
  588. .id_table = ak4613_i2c_id,
  589. };
  590. module_i2c_driver(ak4613_i2c_driver);
  591. MODULE_DESCRIPTION("Soc AK4613 driver");
  592. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  593. MODULE_LICENSE("GPL v2");