ak4642.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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/delay.h>
  25. #include <linux/i2c.h>
  26. #include <linux/slab.h>
  27. #include <linux/of_device.h>
  28. #include <linux/module.h>
  29. #include <sound/soc.h>
  30. #include <sound/initval.h>
  31. #include <sound/tlv.h>
  32. #define PW_MGMT1 0x00
  33. #define PW_MGMT2 0x01
  34. #define SG_SL1 0x02
  35. #define SG_SL2 0x03
  36. #define MD_CTL1 0x04
  37. #define MD_CTL2 0x05
  38. #define TIMER 0x06
  39. #define ALC_CTL1 0x07
  40. #define ALC_CTL2 0x08
  41. #define L_IVC 0x09
  42. #define L_DVC 0x0a
  43. #define ALC_CTL3 0x0b
  44. #define R_IVC 0x0c
  45. #define R_DVC 0x0d
  46. #define MD_CTL3 0x0e
  47. #define MD_CTL4 0x0f
  48. #define PW_MGMT3 0x10
  49. #define DF_S 0x11
  50. #define FIL3_0 0x12
  51. #define FIL3_1 0x13
  52. #define FIL3_2 0x14
  53. #define FIL3_3 0x15
  54. #define EQ_0 0x16
  55. #define EQ_1 0x17
  56. #define EQ_2 0x18
  57. #define EQ_3 0x19
  58. #define EQ_4 0x1a
  59. #define EQ_5 0x1b
  60. #define FIL1_0 0x1c
  61. #define FIL1_1 0x1d
  62. #define FIL1_2 0x1e
  63. #define FIL1_3 0x1f
  64. #define PW_MGMT4 0x20
  65. #define MD_CTL5 0x21
  66. #define LO_MS 0x22
  67. #define HP_MS 0x23
  68. #define SPK_MS 0x24
  69. /* PW_MGMT1*/
  70. #define PMVCM (1 << 6) /* VCOM Power Management */
  71. #define PMMIN (1 << 5) /* MIN Input Power Management */
  72. #define PMDAC (1 << 2) /* DAC Power Management */
  73. #define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
  74. /* PW_MGMT2 */
  75. #define HPMTN (1 << 6)
  76. #define PMHPL (1 << 5)
  77. #define PMHPR (1 << 4)
  78. #define MS (1 << 3) /* master/slave select */
  79. #define MCKO (1 << 1)
  80. #define PMPLL (1 << 0)
  81. #define PMHP_MASK (PMHPL | PMHPR)
  82. #define PMHP PMHP_MASK
  83. /* PW_MGMT3 */
  84. #define PMADR (1 << 0) /* MIC L / ADC R Power Management */
  85. /* SG_SL1 */
  86. #define MINS (1 << 6) /* Switch from MIN to Speaker */
  87. #define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */
  88. #define PMMP (1 << 2) /* MPWR pin Power Management */
  89. #define MGAIN0 (1 << 0) /* MIC amp gain*/
  90. /* TIMER */
  91. #define ZTM(param) ((param & 0x3) << 4) /* ALC Zoro Crossing TimeOut */
  92. #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
  93. /* ALC_CTL1 */
  94. #define ALC (1 << 5) /* ALC Enable */
  95. #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
  96. /* MD_CTL1 */
  97. #define PLL3 (1 << 7)
  98. #define PLL2 (1 << 6)
  99. #define PLL1 (1 << 5)
  100. #define PLL0 (1 << 4)
  101. #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
  102. #define BCKO_MASK (1 << 3)
  103. #define BCKO_64 BCKO_MASK
  104. #define DIF_MASK (3 << 0)
  105. #define DSP (0 << 0)
  106. #define RIGHT_J (1 << 0)
  107. #define LEFT_J (2 << 0)
  108. #define I2S (3 << 0)
  109. /* MD_CTL2 */
  110. #define FS0 (1 << 0)
  111. #define FS1 (1 << 1)
  112. #define FS2 (1 << 2)
  113. #define FS3 (1 << 5)
  114. #define FS_MASK (FS0 | FS1 | FS2 | FS3)
  115. /* MD_CTL3 */
  116. #define BST1 (1 << 3)
  117. /* MD_CTL4 */
  118. #define DACH (1 << 0)
  119. /*
  120. * Playback Volume (table 39)
  121. *
  122. * max : 0x00 : +12.0 dB
  123. * ( 0.5 dB step )
  124. * min : 0xFE : -115.0 dB
  125. * mute: 0xFF
  126. */
  127. static const DECLARE_TLV_DB_SCALE(out_tlv, -11550, 50, 1);
  128. static const struct snd_kcontrol_new ak4642_snd_controls[] = {
  129. SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
  130. 0, 0xFF, 1, out_tlv),
  131. };
  132. static const struct snd_kcontrol_new ak4642_headphone_control =
  133. SOC_DAPM_SINGLE("Switch", PW_MGMT2, 6, 1, 0);
  134. static const struct snd_kcontrol_new ak4642_lout_mixer_controls[] = {
  135. SOC_DAPM_SINGLE("DACL", SG_SL1, 4, 1, 0),
  136. };
  137. static const struct snd_soc_dapm_widget ak4642_dapm_widgets[] = {
  138. /* Outputs */
  139. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  140. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  141. SND_SOC_DAPM_OUTPUT("LINEOUT"),
  142. SND_SOC_DAPM_PGA("HPL Out", PW_MGMT2, 5, 0, NULL, 0),
  143. SND_SOC_DAPM_PGA("HPR Out", PW_MGMT2, 4, 0, NULL, 0),
  144. SND_SOC_DAPM_SWITCH("Headphone Enable", SND_SOC_NOPM, 0, 0,
  145. &ak4642_headphone_control),
  146. SND_SOC_DAPM_PGA("DACH", MD_CTL4, 0, 0, NULL, 0),
  147. SND_SOC_DAPM_MIXER("LINEOUT Mixer", PW_MGMT1, 3, 0,
  148. &ak4642_lout_mixer_controls[0],
  149. ARRAY_SIZE(ak4642_lout_mixer_controls)),
  150. /* DAC */
  151. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", PW_MGMT1, 2, 0),
  152. };
  153. static const struct snd_soc_dapm_route ak4642_intercon[] = {
  154. /* Outputs */
  155. {"HPOUTL", NULL, "HPL Out"},
  156. {"HPOUTR", NULL, "HPR Out"},
  157. {"LINEOUT", NULL, "LINEOUT Mixer"},
  158. {"HPL Out", NULL, "Headphone Enable"},
  159. {"HPR Out", NULL, "Headphone Enable"},
  160. {"Headphone Enable", "Switch", "DACH"},
  161. {"DACH", NULL, "DAC"},
  162. {"LINEOUT Mixer", "DACL", "DAC"},
  163. };
  164. /*
  165. * ak4642 register cache
  166. */
  167. static const u8 ak4642_reg[] = {
  168. 0x00, 0x00, 0x01, 0x00,
  169. 0x02, 0x00, 0x00, 0x00,
  170. 0xe1, 0xe1, 0x18, 0x00,
  171. 0xe1, 0x18, 0x11, 0x08,
  172. 0x00, 0x00, 0x00, 0x00,
  173. 0x00, 0x00, 0x00, 0x00,
  174. 0x00, 0x00, 0x00, 0x00,
  175. 0x00, 0x00, 0x00, 0x00,
  176. 0x00, 0x00, 0x00, 0x00,
  177. 0x00,
  178. };
  179. static const u8 ak4648_reg[] = {
  180. 0x00, 0x00, 0x01, 0x00,
  181. 0x02, 0x00, 0x00, 0x00,
  182. 0xe1, 0xe1, 0x18, 0x00,
  183. 0xe1, 0x18, 0x11, 0xb8,
  184. 0x00, 0x00, 0x00, 0x00,
  185. 0x00, 0x00, 0x00, 0x00,
  186. 0x00, 0x00, 0x00, 0x00,
  187. 0x00, 0x00, 0x00, 0x00,
  188. 0x00, 0x00, 0x00, 0x00,
  189. 0x00, 0x88, 0x88, 0x08,
  190. };
  191. static int ak4642_dai_startup(struct snd_pcm_substream *substream,
  192. struct snd_soc_dai *dai)
  193. {
  194. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  195. struct snd_soc_codec *codec = dai->codec;
  196. if (is_play) {
  197. /*
  198. * start headphone output
  199. *
  200. * PLL, Master Mode
  201. * Audio I/F Format :MSB justified (ADC & DAC)
  202. * Bass Boost Level : Middle
  203. *
  204. * This operation came from example code of
  205. * "ASAHI KASEI AK4642" (japanese) manual p97.
  206. */
  207. snd_soc_write(codec, L_IVC, 0x91); /* volume */
  208. snd_soc_write(codec, R_IVC, 0x91); /* volume */
  209. } else {
  210. /*
  211. * start stereo input
  212. *
  213. * PLL Master Mode
  214. * Audio I/F Format:MSB justified (ADC & DAC)
  215. * Pre MIC AMP:+20dB
  216. * MIC Power On
  217. * ALC setting:Refer to Table 35
  218. * ALC bit=“1”
  219. *
  220. * This operation came from example code of
  221. * "ASAHI KASEI AK4642" (japanese) manual p94.
  222. */
  223. snd_soc_write(codec, SG_SL1, PMMP | MGAIN0);
  224. snd_soc_write(codec, TIMER, ZTM(0x3) | WTM(0x3));
  225. snd_soc_write(codec, ALC_CTL1, ALC | LMTH0);
  226. snd_soc_update_bits(codec, PW_MGMT1, PMADL, PMADL);
  227. snd_soc_update_bits(codec, PW_MGMT3, PMADR, PMADR);
  228. }
  229. return 0;
  230. }
  231. static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
  232. struct snd_soc_dai *dai)
  233. {
  234. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  235. struct snd_soc_codec *codec = dai->codec;
  236. if (is_play) {
  237. } else {
  238. /* stop stereo input */
  239. snd_soc_update_bits(codec, PW_MGMT1, PMADL, 0);
  240. snd_soc_update_bits(codec, PW_MGMT3, PMADR, 0);
  241. snd_soc_update_bits(codec, ALC_CTL1, ALC, 0);
  242. }
  243. }
  244. static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
  245. int clk_id, unsigned int freq, int dir)
  246. {
  247. struct snd_soc_codec *codec = codec_dai->codec;
  248. u8 pll;
  249. switch (freq) {
  250. case 11289600:
  251. pll = PLL2;
  252. break;
  253. case 12288000:
  254. pll = PLL2 | PLL0;
  255. break;
  256. case 12000000:
  257. pll = PLL2 | PLL1;
  258. break;
  259. case 24000000:
  260. pll = PLL2 | PLL1 | PLL0;
  261. break;
  262. case 13500000:
  263. pll = PLL3 | PLL2;
  264. break;
  265. case 27000000:
  266. pll = PLL3 | PLL2 | PLL0;
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
  272. return 0;
  273. }
  274. static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  275. {
  276. struct snd_soc_codec *codec = dai->codec;
  277. u8 data;
  278. u8 bcko;
  279. data = MCKO | PMPLL; /* use MCKO */
  280. bcko = 0;
  281. /* set master/slave audio interface */
  282. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  283. case SND_SOC_DAIFMT_CBM_CFM:
  284. data |= MS;
  285. bcko = BCKO_64;
  286. break;
  287. case SND_SOC_DAIFMT_CBS_CFS:
  288. break;
  289. default:
  290. return -EINVAL;
  291. }
  292. snd_soc_update_bits(codec, PW_MGMT2, MS | MCKO | PMPLL, data);
  293. snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
  294. /* format type */
  295. data = 0;
  296. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  297. case SND_SOC_DAIFMT_LEFT_J:
  298. data = LEFT_J;
  299. break;
  300. case SND_SOC_DAIFMT_I2S:
  301. data = I2S;
  302. break;
  303. /* FIXME
  304. * Please add RIGHT_J / DSP support here
  305. */
  306. default:
  307. return -EINVAL;
  308. break;
  309. }
  310. snd_soc_update_bits(codec, MD_CTL1, DIF_MASK, data);
  311. return 0;
  312. }
  313. static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
  314. struct snd_pcm_hw_params *params,
  315. struct snd_soc_dai *dai)
  316. {
  317. struct snd_soc_codec *codec = dai->codec;
  318. u8 rate;
  319. switch (params_rate(params)) {
  320. case 7350:
  321. rate = FS2;
  322. break;
  323. case 8000:
  324. rate = 0;
  325. break;
  326. case 11025:
  327. rate = FS2 | FS0;
  328. break;
  329. case 12000:
  330. rate = FS0;
  331. break;
  332. case 14700:
  333. rate = FS2 | FS1;
  334. break;
  335. case 16000:
  336. rate = FS1;
  337. break;
  338. case 22050:
  339. rate = FS2 | FS1 | FS0;
  340. break;
  341. case 24000:
  342. rate = FS1 | FS0;
  343. break;
  344. case 29400:
  345. rate = FS3 | FS2 | FS1;
  346. break;
  347. case 32000:
  348. rate = FS3 | FS1;
  349. break;
  350. case 44100:
  351. rate = FS3 | FS2 | FS1 | FS0;
  352. break;
  353. case 48000:
  354. rate = FS3 | FS1 | FS0;
  355. break;
  356. default:
  357. return -EINVAL;
  358. break;
  359. }
  360. snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
  361. return 0;
  362. }
  363. static int ak4642_set_bias_level(struct snd_soc_codec *codec,
  364. enum snd_soc_bias_level level)
  365. {
  366. switch (level) {
  367. case SND_SOC_BIAS_OFF:
  368. snd_soc_write(codec, PW_MGMT1, 0x00);
  369. break;
  370. default:
  371. snd_soc_update_bits(codec, PW_MGMT1, PMVCM, PMVCM);
  372. break;
  373. }
  374. codec->dapm.bias_level = level;
  375. return 0;
  376. }
  377. static const struct snd_soc_dai_ops ak4642_dai_ops = {
  378. .startup = ak4642_dai_startup,
  379. .shutdown = ak4642_dai_shutdown,
  380. .set_sysclk = ak4642_dai_set_sysclk,
  381. .set_fmt = ak4642_dai_set_fmt,
  382. .hw_params = ak4642_dai_hw_params,
  383. };
  384. static struct snd_soc_dai_driver ak4642_dai = {
  385. .name = "ak4642-hifi",
  386. .playback = {
  387. .stream_name = "Playback",
  388. .channels_min = 1,
  389. .channels_max = 2,
  390. .rates = SNDRV_PCM_RATE_8000_48000,
  391. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  392. .capture = {
  393. .stream_name = "Capture",
  394. .channels_min = 1,
  395. .channels_max = 2,
  396. .rates = SNDRV_PCM_RATE_8000_48000,
  397. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  398. .ops = &ak4642_dai_ops,
  399. .symmetric_rates = 1,
  400. };
  401. static int ak4642_resume(struct snd_soc_codec *codec)
  402. {
  403. snd_soc_cache_sync(codec);
  404. return 0;
  405. }
  406. static int ak4642_probe(struct snd_soc_codec *codec)
  407. {
  408. int ret;
  409. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_I2C);
  410. if (ret < 0) {
  411. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  412. return ret;
  413. }
  414. snd_soc_add_codec_controls(codec, ak4642_snd_controls,
  415. ARRAY_SIZE(ak4642_snd_controls));
  416. ak4642_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  417. return 0;
  418. }
  419. static int ak4642_remove(struct snd_soc_codec *codec)
  420. {
  421. ak4642_set_bias_level(codec, SND_SOC_BIAS_OFF);
  422. return 0;
  423. }
  424. static struct snd_soc_codec_driver soc_codec_dev_ak4642 = {
  425. .probe = ak4642_probe,
  426. .remove = ak4642_remove,
  427. .resume = ak4642_resume,
  428. .set_bias_level = ak4642_set_bias_level,
  429. .reg_cache_default = ak4642_reg, /* ak4642 reg */
  430. .reg_cache_size = ARRAY_SIZE(ak4642_reg), /* ak4642 reg */
  431. .reg_word_size = sizeof(u8),
  432. .dapm_widgets = ak4642_dapm_widgets,
  433. .num_dapm_widgets = ARRAY_SIZE(ak4642_dapm_widgets),
  434. .dapm_routes = ak4642_intercon,
  435. .num_dapm_routes = ARRAY_SIZE(ak4642_intercon),
  436. };
  437. static struct snd_soc_codec_driver soc_codec_dev_ak4648 = {
  438. .probe = ak4642_probe,
  439. .remove = ak4642_remove,
  440. .resume = ak4642_resume,
  441. .set_bias_level = ak4642_set_bias_level,
  442. .reg_cache_default = ak4648_reg, /* ak4648 reg */
  443. .reg_cache_size = ARRAY_SIZE(ak4648_reg), /* ak4648 reg */
  444. .reg_word_size = sizeof(u8),
  445. .dapm_widgets = ak4642_dapm_widgets,
  446. .num_dapm_widgets = ARRAY_SIZE(ak4642_dapm_widgets),
  447. .dapm_routes = ak4642_intercon,
  448. .num_dapm_routes = ARRAY_SIZE(ak4642_intercon),
  449. };
  450. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  451. static struct of_device_id ak4642_of_match[];
  452. static int ak4642_i2c_probe(struct i2c_client *i2c,
  453. const struct i2c_device_id *id)
  454. {
  455. struct device_node *np = i2c->dev.of_node;
  456. const struct snd_soc_codec_driver *driver;
  457. driver = NULL;
  458. if (np) {
  459. const struct of_device_id *of_id;
  460. of_id = of_match_device(ak4642_of_match, &i2c->dev);
  461. if (of_id)
  462. driver = of_id->data;
  463. } else {
  464. driver = (struct snd_soc_codec_driver *)id->driver_data;
  465. }
  466. if (!driver) {
  467. dev_err(&i2c->dev, "no driver\n");
  468. return -EINVAL;
  469. }
  470. return snd_soc_register_codec(&i2c->dev,
  471. driver, &ak4642_dai, 1);
  472. }
  473. static int ak4642_i2c_remove(struct i2c_client *client)
  474. {
  475. snd_soc_unregister_codec(&client->dev);
  476. return 0;
  477. }
  478. static struct of_device_id ak4642_of_match[] = {
  479. { .compatible = "asahi-kasei,ak4642", .data = &soc_codec_dev_ak4642},
  480. { .compatible = "asahi-kasei,ak4643", .data = &soc_codec_dev_ak4642},
  481. { .compatible = "asahi-kasei,ak4648", .data = &soc_codec_dev_ak4648},
  482. {},
  483. };
  484. MODULE_DEVICE_TABLE(of, ak4642_of_match);
  485. static const struct i2c_device_id ak4642_i2c_id[] = {
  486. { "ak4642", (kernel_ulong_t)&soc_codec_dev_ak4642 },
  487. { "ak4643", (kernel_ulong_t)&soc_codec_dev_ak4642 },
  488. { "ak4648", (kernel_ulong_t)&soc_codec_dev_ak4648 },
  489. { }
  490. };
  491. MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
  492. static struct i2c_driver ak4642_i2c_driver = {
  493. .driver = {
  494. .name = "ak4642-codec",
  495. .owner = THIS_MODULE,
  496. .of_match_table = ak4642_of_match,
  497. },
  498. .probe = ak4642_i2c_probe,
  499. .remove = ak4642_i2c_remove,
  500. .id_table = ak4642_i2c_id,
  501. };
  502. #endif
  503. static int __init ak4642_modinit(void)
  504. {
  505. int ret = 0;
  506. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  507. ret = i2c_add_driver(&ak4642_i2c_driver);
  508. #endif
  509. return ret;
  510. }
  511. module_init(ak4642_modinit);
  512. static void __exit ak4642_exit(void)
  513. {
  514. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  515. i2c_del_driver(&ak4642_i2c_driver);
  516. #endif
  517. }
  518. module_exit(ak4642_exit);
  519. MODULE_DESCRIPTION("Soc AK4642 driver");
  520. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  521. MODULE_LICENSE("GPL");