ak4642.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver
  3. *
  4. * Copyright (C) 2009 Renesas Solutions Corp.
  5. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  6. *
  7. * Based on wm8731.c by Richard Purdie
  8. * Based on ak4535.c by Richard Purdie
  9. * Based on wm8753.c by Liam Girdwood
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. /* ** CAUTION **
  16. *
  17. * This is very simple driver.
  18. * It can use headphone output / stereo input only
  19. *
  20. * AK4642 is tested.
  21. * AK4643 is tested.
  22. * AK4648 is tested.
  23. */
  24. #include <linux/clk.h>
  25. #include <linux/clk-provider.h>
  26. #include <linux/delay.h>
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include <linux/of_device.h>
  30. #include <linux/module.h>
  31. #include <linux/regmap.h>
  32. #include <sound/soc.h>
  33. #include <sound/initval.h>
  34. #include <sound/tlv.h>
  35. #define PW_MGMT1 0x00
  36. #define PW_MGMT2 0x01
  37. #define SG_SL1 0x02
  38. #define SG_SL2 0x03
  39. #define MD_CTL1 0x04
  40. #define MD_CTL2 0x05
  41. #define TIMER 0x06
  42. #define ALC_CTL1 0x07
  43. #define ALC_CTL2 0x08
  44. #define L_IVC 0x09
  45. #define L_DVC 0x0a
  46. #define ALC_CTL3 0x0b
  47. #define R_IVC 0x0c
  48. #define R_DVC 0x0d
  49. #define MD_CTL3 0x0e
  50. #define MD_CTL4 0x0f
  51. #define PW_MGMT3 0x10
  52. #define DF_S 0x11
  53. #define FIL3_0 0x12
  54. #define FIL3_1 0x13
  55. #define FIL3_2 0x14
  56. #define FIL3_3 0x15
  57. #define EQ_0 0x16
  58. #define EQ_1 0x17
  59. #define EQ_2 0x18
  60. #define EQ_3 0x19
  61. #define EQ_4 0x1a
  62. #define EQ_5 0x1b
  63. #define FIL1_0 0x1c
  64. #define FIL1_1 0x1d
  65. #define FIL1_2 0x1e
  66. #define FIL1_3 0x1f /* The maximum valid register for ak4642 */
  67. #define PW_MGMT4 0x20
  68. #define MD_CTL5 0x21
  69. #define LO_MS 0x22
  70. #define HP_MS 0x23
  71. #define SPK_MS 0x24 /* The maximum valid register for ak4643 */
  72. #define EQ_FBEQAB 0x25
  73. #define EQ_FBEQCD 0x26
  74. #define EQ_FBEQE 0x27 /* The maximum valid register for ak4648 */
  75. /* PW_MGMT1*/
  76. #define PMVCM (1 << 6) /* VCOM Power Management */
  77. #define PMMIN (1 << 5) /* MIN Input Power Management */
  78. #define PMDAC (1 << 2) /* DAC Power Management */
  79. #define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
  80. /* PW_MGMT2 */
  81. #define HPMTN (1 << 6)
  82. #define PMHPL (1 << 5)
  83. #define PMHPR (1 << 4)
  84. #define MS (1 << 3) /* master/slave select */
  85. #define MCKO (1 << 1)
  86. #define PMPLL (1 << 0)
  87. #define PMHP_MASK (PMHPL | PMHPR)
  88. #define PMHP PMHP_MASK
  89. /* PW_MGMT3 */
  90. #define PMADR (1 << 0) /* MIC L / ADC R Power Management */
  91. /* SG_SL1 */
  92. #define MINS (1 << 6) /* Switch from MIN to Speaker */
  93. #define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */
  94. #define PMMP (1 << 2) /* MPWR pin Power Management */
  95. #define MGAIN0 (1 << 0) /* MIC amp gain*/
  96. /* SG_SL2 */
  97. #define LOPS (1 << 6) /* Stero Line-out Power Save Mode */
  98. /* TIMER */
  99. #define ZTM(param) ((param & 0x3) << 4) /* ALC Zero Crossing TimeOut */
  100. #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
  101. /* ALC_CTL1 */
  102. #define ALC (1 << 5) /* ALC Enable */
  103. #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
  104. /* MD_CTL1 */
  105. #define PLL3 (1 << 7)
  106. #define PLL2 (1 << 6)
  107. #define PLL1 (1 << 5)
  108. #define PLL0 (1 << 4)
  109. #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
  110. #define BCKO_MASK (1 << 3)
  111. #define BCKO_64 BCKO_MASK
  112. #define DIF_MASK (3 << 0)
  113. #define DSP (0 << 0)
  114. #define RIGHT_J (1 << 0)
  115. #define LEFT_J (2 << 0)
  116. #define I2S (3 << 0)
  117. /* MD_CTL2 */
  118. #define FSs(val) (((val & 0x7) << 0) | ((val & 0x8) << 2))
  119. #define PSs(val) ((val & 0x3) << 6)
  120. /* MD_CTL3 */
  121. #define BST1 (1 << 3)
  122. /* MD_CTL4 */
  123. #define DACH (1 << 0)
  124. struct ak4642_drvdata {
  125. const struct regmap_config *regmap_config;
  126. int extended_frequencies;
  127. };
  128. struct ak4642_priv {
  129. const struct ak4642_drvdata *drvdata;
  130. struct clk *mcko;
  131. };
  132. /*
  133. * Playback Volume (table 39)
  134. *
  135. * max : 0x00 : +12.0 dB
  136. * ( 0.5 dB step )
  137. * min : 0xFE : -115.0 dB
  138. * mute: 0xFF
  139. */
  140. static const DECLARE_TLV_DB_SCALE(out_tlv, -11550, 50, 1);
  141. static const struct snd_kcontrol_new ak4642_snd_controls[] = {
  142. SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
  143. 0, 0xFF, 1, out_tlv),
  144. SOC_SINGLE("ALC Capture Switch", ALC_CTL1, 5, 1, 0),
  145. SOC_SINGLE("ALC Capture ZC Switch", ALC_CTL1, 4, 1, 1),
  146. };
  147. static const struct snd_kcontrol_new ak4642_headphone_control =
  148. SOC_DAPM_SINGLE("Switch", PW_MGMT2, 6, 1, 0);
  149. static const struct snd_kcontrol_new ak4642_lout_mixer_controls[] = {
  150. SOC_DAPM_SINGLE("DACL", SG_SL1, 4, 1, 0),
  151. };
  152. /* event handlers */
  153. static int ak4642_lout_event(struct snd_soc_dapm_widget *w,
  154. struct snd_kcontrol *kcontrol, int event)
  155. {
  156. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  157. switch (event) {
  158. case SND_SOC_DAPM_PRE_PMD:
  159. case SND_SOC_DAPM_PRE_PMU:
  160. /* Power save mode ON */
  161. snd_soc_update_bits(codec, SG_SL2, LOPS, LOPS);
  162. break;
  163. case SND_SOC_DAPM_POST_PMU:
  164. case SND_SOC_DAPM_POST_PMD:
  165. /* Power save mode OFF */
  166. mdelay(300);
  167. snd_soc_update_bits(codec, SG_SL2, LOPS, 0);
  168. break;
  169. }
  170. return 0;
  171. }
  172. static const struct snd_soc_dapm_widget ak4642_dapm_widgets[] = {
  173. /* Outputs */
  174. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  175. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  176. SND_SOC_DAPM_OUTPUT("LINEOUT"),
  177. SND_SOC_DAPM_PGA("HPL Out", PW_MGMT2, 5, 0, NULL, 0),
  178. SND_SOC_DAPM_PGA("HPR Out", PW_MGMT2, 4, 0, NULL, 0),
  179. SND_SOC_DAPM_SWITCH("Headphone Enable", SND_SOC_NOPM, 0, 0,
  180. &ak4642_headphone_control),
  181. SND_SOC_DAPM_PGA("DACH", MD_CTL4, 0, 0, NULL, 0),
  182. SND_SOC_DAPM_MIXER_E("LINEOUT Mixer", PW_MGMT1, 3, 0,
  183. &ak4642_lout_mixer_controls[0],
  184. ARRAY_SIZE(ak4642_lout_mixer_controls),
  185. ak4642_lout_event,
  186. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  187. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  188. /* DAC */
  189. SND_SOC_DAPM_DAC("DAC", NULL, PW_MGMT1, 2, 0),
  190. };
  191. static const struct snd_soc_dapm_route ak4642_intercon[] = {
  192. /* Outputs */
  193. {"HPOUTL", NULL, "HPL Out"},
  194. {"HPOUTR", NULL, "HPR Out"},
  195. {"LINEOUT", NULL, "LINEOUT Mixer"},
  196. {"HPL Out", NULL, "Headphone Enable"},
  197. {"HPR Out", NULL, "Headphone Enable"},
  198. {"Headphone Enable", "Switch", "DACH"},
  199. {"DACH", NULL, "DAC"},
  200. {"LINEOUT Mixer", "DACL", "DAC"},
  201. { "DAC", NULL, "Playback" },
  202. };
  203. /*
  204. * ak4642 register cache
  205. */
  206. static const struct reg_default ak4643_reg[] = {
  207. { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
  208. { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
  209. { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
  210. { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0x08 },
  211. { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
  212. { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
  213. { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
  214. { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
  215. { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
  216. { 36, 0x00 },
  217. };
  218. /* The default settings for 0x0 ~ 0x1f registers are the same for ak4642
  219. and ak4643. So we reuse the ak4643 reg_default for ak4642.
  220. The valid registers for ak4642 are 0x0 ~ 0x1f which is a subset of ak4643,
  221. so define NUM_AK4642_REG_DEFAULTS for ak4642.
  222. */
  223. #define ak4642_reg ak4643_reg
  224. #define NUM_AK4642_REG_DEFAULTS (FIL1_3 + 1)
  225. static const struct reg_default ak4648_reg[] = {
  226. { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
  227. { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
  228. { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
  229. { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0xb8 },
  230. { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
  231. { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
  232. { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
  233. { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
  234. { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
  235. { 36, 0x00 }, { 37, 0x88 }, { 38, 0x88 }, { 39, 0x08 },
  236. };
  237. static int ak4642_dai_startup(struct snd_pcm_substream *substream,
  238. struct snd_soc_dai *dai)
  239. {
  240. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  241. struct snd_soc_codec *codec = dai->codec;
  242. if (is_play) {
  243. /*
  244. * start headphone output
  245. *
  246. * PLL, Master Mode
  247. * Audio I/F Format :MSB justified (ADC & DAC)
  248. * Bass Boost Level : Middle
  249. *
  250. * This operation came from example code of
  251. * "ASAHI KASEI AK4642" (japanese) manual p97.
  252. */
  253. snd_soc_write(codec, L_IVC, 0x91); /* volume */
  254. snd_soc_write(codec, R_IVC, 0x91); /* volume */
  255. } else {
  256. /*
  257. * start stereo input
  258. *
  259. * PLL Master Mode
  260. * Audio I/F Format:MSB justified (ADC & DAC)
  261. * Pre MIC AMP:+20dB
  262. * MIC Power On
  263. * ALC setting:Refer to Table 35
  264. * ALC bit=“1”
  265. *
  266. * This operation came from example code of
  267. * "ASAHI KASEI AK4642" (japanese) manual p94.
  268. */
  269. snd_soc_update_bits(codec, SG_SL1, PMMP | MGAIN0, PMMP | MGAIN0);
  270. snd_soc_write(codec, TIMER, ZTM(0x3) | WTM(0x3));
  271. snd_soc_write(codec, ALC_CTL1, ALC | LMTH0);
  272. snd_soc_update_bits(codec, PW_MGMT1, PMADL, PMADL);
  273. snd_soc_update_bits(codec, PW_MGMT3, PMADR, PMADR);
  274. }
  275. return 0;
  276. }
  277. static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
  278. struct snd_soc_dai *dai)
  279. {
  280. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  281. struct snd_soc_codec *codec = dai->codec;
  282. if (is_play) {
  283. } else {
  284. /* stop stereo input */
  285. snd_soc_update_bits(codec, PW_MGMT1, PMADL, 0);
  286. snd_soc_update_bits(codec, PW_MGMT3, PMADR, 0);
  287. snd_soc_update_bits(codec, ALC_CTL1, ALC, 0);
  288. }
  289. }
  290. static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
  291. int clk_id, unsigned int freq, int dir)
  292. {
  293. struct snd_soc_codec *codec = codec_dai->codec;
  294. struct ak4642_priv *priv = snd_soc_codec_get_drvdata(codec);
  295. u8 pll;
  296. int extended_freq = 0;
  297. switch (freq) {
  298. case 11289600:
  299. pll = PLL2;
  300. break;
  301. case 12288000:
  302. pll = PLL2 | PLL0;
  303. break;
  304. case 12000000:
  305. pll = PLL2 | PLL1;
  306. break;
  307. case 24000000:
  308. pll = PLL2 | PLL1 | PLL0;
  309. break;
  310. case 13500000:
  311. pll = PLL3 | PLL2;
  312. break;
  313. case 27000000:
  314. pll = PLL3 | PLL2 | PLL0;
  315. break;
  316. case 19200000:
  317. pll = PLL3;
  318. extended_freq = 1;
  319. break;
  320. case 13000000:
  321. pll = PLL3 | PLL2 | PLL1;
  322. extended_freq = 1;
  323. break;
  324. case 26000000:
  325. pll = PLL3 | PLL2 | PLL1 | PLL0;
  326. extended_freq = 1;
  327. break;
  328. default:
  329. return -EINVAL;
  330. }
  331. if (extended_freq && !priv->drvdata->extended_frequencies)
  332. return -EINVAL;
  333. snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
  334. return 0;
  335. }
  336. static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  337. {
  338. struct snd_soc_codec *codec = dai->codec;
  339. u8 data;
  340. u8 bcko;
  341. data = MCKO | PMPLL; /* use MCKO */
  342. bcko = 0;
  343. /* set master/slave audio interface */
  344. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  345. case SND_SOC_DAIFMT_CBM_CFM:
  346. data |= MS;
  347. bcko = BCKO_64;
  348. break;
  349. case SND_SOC_DAIFMT_CBS_CFS:
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. snd_soc_update_bits(codec, PW_MGMT2, MS | MCKO | PMPLL, data);
  355. snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
  356. /* format type */
  357. data = 0;
  358. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  359. case SND_SOC_DAIFMT_LEFT_J:
  360. data = LEFT_J;
  361. break;
  362. case SND_SOC_DAIFMT_I2S:
  363. data = I2S;
  364. break;
  365. /* FIXME
  366. * Please add RIGHT_J / DSP support here
  367. */
  368. default:
  369. return -EINVAL;
  370. }
  371. snd_soc_update_bits(codec, MD_CTL1, DIF_MASK, data);
  372. return 0;
  373. }
  374. static int ak4642_set_mcko(struct snd_soc_codec *codec,
  375. u32 frequency)
  376. {
  377. u32 fs_list[] = {
  378. [0] = 8000,
  379. [1] = 12000,
  380. [2] = 16000,
  381. [3] = 24000,
  382. [4] = 7350,
  383. [5] = 11025,
  384. [6] = 14700,
  385. [7] = 22050,
  386. [10] = 32000,
  387. [11] = 48000,
  388. [14] = 29400,
  389. [15] = 44100,
  390. };
  391. u32 ps_list[] = {
  392. [0] = 256,
  393. [1] = 128,
  394. [2] = 64,
  395. [3] = 32
  396. };
  397. int ps, fs;
  398. for (ps = 0; ps < ARRAY_SIZE(ps_list); ps++) {
  399. for (fs = 0; fs < ARRAY_SIZE(fs_list); fs++) {
  400. if (frequency == ps_list[ps] * fs_list[fs]) {
  401. snd_soc_write(codec, MD_CTL2,
  402. PSs(ps) | FSs(fs));
  403. return 0;
  404. }
  405. }
  406. }
  407. return 0;
  408. }
  409. static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
  410. struct snd_pcm_hw_params *params,
  411. struct snd_soc_dai *dai)
  412. {
  413. struct snd_soc_codec *codec = dai->codec;
  414. struct ak4642_priv *priv = snd_soc_codec_get_drvdata(codec);
  415. u32 rate = clk_get_rate(priv->mcko);
  416. if (!rate)
  417. rate = params_rate(params) * 256;
  418. return ak4642_set_mcko(codec, rate);
  419. }
  420. static int ak4642_set_bias_level(struct snd_soc_codec *codec,
  421. enum snd_soc_bias_level level)
  422. {
  423. switch (level) {
  424. case SND_SOC_BIAS_OFF:
  425. snd_soc_write(codec, PW_MGMT1, 0x00);
  426. break;
  427. default:
  428. snd_soc_update_bits(codec, PW_MGMT1, PMVCM, PMVCM);
  429. break;
  430. }
  431. return 0;
  432. }
  433. static const struct snd_soc_dai_ops ak4642_dai_ops = {
  434. .startup = ak4642_dai_startup,
  435. .shutdown = ak4642_dai_shutdown,
  436. .set_sysclk = ak4642_dai_set_sysclk,
  437. .set_fmt = ak4642_dai_set_fmt,
  438. .hw_params = ak4642_dai_hw_params,
  439. };
  440. static struct snd_soc_dai_driver ak4642_dai = {
  441. .name = "ak4642-hifi",
  442. .playback = {
  443. .stream_name = "Playback",
  444. .channels_min = 2,
  445. .channels_max = 2,
  446. .rates = SNDRV_PCM_RATE_8000_48000,
  447. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  448. .capture = {
  449. .stream_name = "Capture",
  450. .channels_min = 2,
  451. .channels_max = 2,
  452. .rates = SNDRV_PCM_RATE_8000_48000,
  453. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  454. .ops = &ak4642_dai_ops,
  455. .symmetric_rates = 1,
  456. };
  457. static int ak4642_suspend(struct snd_soc_codec *codec)
  458. {
  459. struct regmap *regmap = dev_get_regmap(codec->dev, NULL);
  460. regcache_cache_only(regmap, true);
  461. regcache_mark_dirty(regmap);
  462. return 0;
  463. }
  464. static int ak4642_resume(struct snd_soc_codec *codec)
  465. {
  466. struct regmap *regmap = dev_get_regmap(codec->dev, NULL);
  467. regcache_cache_only(regmap, false);
  468. regcache_sync(regmap);
  469. return 0;
  470. }
  471. static int ak4642_probe(struct snd_soc_codec *codec)
  472. {
  473. struct ak4642_priv *priv = snd_soc_codec_get_drvdata(codec);
  474. if (priv->mcko)
  475. ak4642_set_mcko(codec, clk_get_rate(priv->mcko));
  476. return 0;
  477. }
  478. static struct snd_soc_codec_driver soc_codec_dev_ak4642 = {
  479. .probe = ak4642_probe,
  480. .suspend = ak4642_suspend,
  481. .resume = ak4642_resume,
  482. .set_bias_level = ak4642_set_bias_level,
  483. .component_driver = {
  484. .controls = ak4642_snd_controls,
  485. .num_controls = ARRAY_SIZE(ak4642_snd_controls),
  486. .dapm_widgets = ak4642_dapm_widgets,
  487. .num_dapm_widgets = ARRAY_SIZE(ak4642_dapm_widgets),
  488. .dapm_routes = ak4642_intercon,
  489. .num_dapm_routes = ARRAY_SIZE(ak4642_intercon),
  490. },
  491. };
  492. static const struct regmap_config ak4642_regmap = {
  493. .reg_bits = 8,
  494. .val_bits = 8,
  495. .max_register = FIL1_3,
  496. .reg_defaults = ak4642_reg,
  497. .num_reg_defaults = NUM_AK4642_REG_DEFAULTS,
  498. .cache_type = REGCACHE_RBTREE,
  499. };
  500. static const struct regmap_config ak4643_regmap = {
  501. .reg_bits = 8,
  502. .val_bits = 8,
  503. .max_register = SPK_MS,
  504. .reg_defaults = ak4643_reg,
  505. .num_reg_defaults = ARRAY_SIZE(ak4643_reg),
  506. .cache_type = REGCACHE_RBTREE,
  507. };
  508. static const struct regmap_config ak4648_regmap = {
  509. .reg_bits = 8,
  510. .val_bits = 8,
  511. .max_register = EQ_FBEQE,
  512. .reg_defaults = ak4648_reg,
  513. .num_reg_defaults = ARRAY_SIZE(ak4648_reg),
  514. .cache_type = REGCACHE_RBTREE,
  515. };
  516. static const struct ak4642_drvdata ak4642_drvdata = {
  517. .regmap_config = &ak4642_regmap,
  518. };
  519. static const struct ak4642_drvdata ak4643_drvdata = {
  520. .regmap_config = &ak4643_regmap,
  521. };
  522. static const struct ak4642_drvdata ak4648_drvdata = {
  523. .regmap_config = &ak4648_regmap,
  524. .extended_frequencies = 1,
  525. };
  526. #ifdef CONFIG_COMMON_CLK
  527. static struct clk *ak4642_of_parse_mcko(struct device *dev)
  528. {
  529. struct device_node *np = dev->of_node;
  530. struct clk *clk;
  531. const char *clk_name = np->name;
  532. const char *parent_clk_name = NULL;
  533. u32 rate;
  534. if (of_property_read_u32(np, "clock-frequency", &rate))
  535. return NULL;
  536. if (of_property_read_bool(np, "clocks"))
  537. parent_clk_name = of_clk_get_parent_name(np, 0);
  538. of_property_read_string(np, "clock-output-names", &clk_name);
  539. clk = clk_register_fixed_rate(dev, clk_name, parent_clk_name, 0, rate);
  540. if (!IS_ERR(clk))
  541. of_clk_add_provider(np, of_clk_src_simple_get, clk);
  542. return clk;
  543. }
  544. #else
  545. #define ak4642_of_parse_mcko(d) 0
  546. #endif
  547. static const struct of_device_id ak4642_of_match[];
  548. static int ak4642_i2c_probe(struct i2c_client *i2c,
  549. const struct i2c_device_id *id)
  550. {
  551. struct device *dev = &i2c->dev;
  552. struct device_node *np = dev->of_node;
  553. const struct ak4642_drvdata *drvdata = NULL;
  554. struct regmap *regmap;
  555. struct ak4642_priv *priv;
  556. struct clk *mcko = NULL;
  557. if (np) {
  558. const struct of_device_id *of_id;
  559. mcko = ak4642_of_parse_mcko(dev);
  560. if (IS_ERR(mcko))
  561. mcko = NULL;
  562. of_id = of_match_device(ak4642_of_match, dev);
  563. if (of_id)
  564. drvdata = of_id->data;
  565. } else {
  566. drvdata = (const struct ak4642_drvdata *)id->driver_data;
  567. }
  568. if (!drvdata) {
  569. dev_err(dev, "Unknown device type\n");
  570. return -EINVAL;
  571. }
  572. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  573. if (!priv)
  574. return -ENOMEM;
  575. priv->drvdata = drvdata;
  576. priv->mcko = mcko;
  577. i2c_set_clientdata(i2c, priv);
  578. regmap = devm_regmap_init_i2c(i2c, drvdata->regmap_config);
  579. if (IS_ERR(regmap))
  580. return PTR_ERR(regmap);
  581. return snd_soc_register_codec(dev,
  582. &soc_codec_dev_ak4642, &ak4642_dai, 1);
  583. }
  584. static int ak4642_i2c_remove(struct i2c_client *client)
  585. {
  586. snd_soc_unregister_codec(&client->dev);
  587. return 0;
  588. }
  589. static const struct of_device_id ak4642_of_match[] = {
  590. { .compatible = "asahi-kasei,ak4642", .data = &ak4642_drvdata},
  591. { .compatible = "asahi-kasei,ak4643", .data = &ak4643_drvdata},
  592. { .compatible = "asahi-kasei,ak4648", .data = &ak4648_drvdata},
  593. {},
  594. };
  595. MODULE_DEVICE_TABLE(of, ak4642_of_match);
  596. static const struct i2c_device_id ak4642_i2c_id[] = {
  597. { "ak4642", (kernel_ulong_t)&ak4642_drvdata },
  598. { "ak4643", (kernel_ulong_t)&ak4643_drvdata },
  599. { "ak4648", (kernel_ulong_t)&ak4648_drvdata },
  600. { }
  601. };
  602. MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
  603. static struct i2c_driver ak4642_i2c_driver = {
  604. .driver = {
  605. .name = "ak4642-codec",
  606. .of_match_table = ak4642_of_match,
  607. },
  608. .probe = ak4642_i2c_probe,
  609. .remove = ak4642_i2c_remove,
  610. .id_table = ak4642_i2c_id,
  611. };
  612. module_i2c_driver(ak4642_i2c_driver);
  613. MODULE_DESCRIPTION("Soc AK4642 driver");
  614. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  615. MODULE_LICENSE("GPL");