ak4642.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 not tested.
  21. * AK4643 is tested.
  22. */
  23. #include <linux/delay.h>
  24. #include <linux/i2c.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <sound/soc-dapm.h>
  28. #include <sound/initval.h>
  29. #include <sound/tlv.h>
  30. #include "ak4642.h"
  31. #define AK4642_VERSION "0.0.1"
  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. #define AK4642_CACHEREGNUM 0x25
  70. /* PW_MGMT2 */
  71. #define HPMTN (1 << 6)
  72. #define PMHPL (1 << 5)
  73. #define PMHPR (1 << 4)
  74. #define MS (1 << 3) /* master/slave select */
  75. #define MCKO (1 << 1)
  76. #define PMPLL (1 << 0)
  77. #define PMHP_MASK (PMHPL | PMHPR)
  78. #define PMHP PMHP_MASK
  79. /* MD_CTL1 */
  80. #define PLL3 (1 << 7)
  81. #define PLL2 (1 << 6)
  82. #define PLL1 (1 << 5)
  83. #define PLL0 (1 << 4)
  84. #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
  85. #define BCKO_MASK (1 << 3)
  86. #define BCKO_64 BCKO_MASK
  87. /* MD_CTL2 */
  88. #define FS0 (1 << 0)
  89. #define FS1 (1 << 1)
  90. #define FS2 (1 << 2)
  91. #define FS3 (1 << 5)
  92. #define FS_MASK (FS0 | FS1 | FS2 | FS3)
  93. struct snd_soc_codec_device soc_codec_dev_ak4642;
  94. /*
  95. * Playback Volume (table 39)
  96. *
  97. * max : 0x00 : +12.0 dB
  98. * ( 0.5 dB step )
  99. * min : 0xFE : -115.0 dB
  100. * mute: 0xFF
  101. */
  102. static const DECLARE_TLV_DB_SCALE(out_tlv, -11500, 50, 1);
  103. static const struct snd_kcontrol_new ak4642_snd_controls[] = {
  104. SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
  105. 0, 0xFF, 1, out_tlv),
  106. };
  107. /* codec private data */
  108. struct ak4642_priv {
  109. struct snd_soc_codec codec;
  110. };
  111. static struct snd_soc_codec *ak4642_codec;
  112. /*
  113. * ak4642 register cache
  114. */
  115. static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
  116. 0x0000, 0x0000, 0x0001, 0x0000,
  117. 0x0002, 0x0000, 0x0000, 0x0000,
  118. 0x00e1, 0x00e1, 0x0018, 0x0000,
  119. 0x00e1, 0x0018, 0x0011, 0x0008,
  120. 0x0000, 0x0000, 0x0000, 0x0000,
  121. 0x0000, 0x0000, 0x0000, 0x0000,
  122. 0x0000, 0x0000, 0x0000, 0x0000,
  123. 0x0000, 0x0000, 0x0000, 0x0000,
  124. 0x0000, 0x0000, 0x0000, 0x0000,
  125. 0x0000,
  126. };
  127. /*
  128. * read ak4642 register cache
  129. */
  130. static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
  131. unsigned int reg)
  132. {
  133. u16 *cache = codec->reg_cache;
  134. if (reg >= AK4642_CACHEREGNUM)
  135. return -1;
  136. return cache[reg];
  137. }
  138. /*
  139. * write ak4642 register cache
  140. */
  141. static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
  142. u16 reg, unsigned int value)
  143. {
  144. u16 *cache = codec->reg_cache;
  145. if (reg >= AK4642_CACHEREGNUM)
  146. return;
  147. cache[reg] = value;
  148. }
  149. /*
  150. * write to the AK4642 register space
  151. */
  152. static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
  153. unsigned int value)
  154. {
  155. u8 data[2];
  156. /* data is
  157. * D15..D8 AK4642 register offset
  158. * D7...D0 register data
  159. */
  160. data[0] = reg & 0xff;
  161. data[1] = value & 0xff;
  162. if (codec->hw_write(codec->control_data, data, 2) == 2) {
  163. ak4642_write_reg_cache(codec, reg, value);
  164. return 0;
  165. } else
  166. return -EIO;
  167. }
  168. static int ak4642_sync(struct snd_soc_codec *codec)
  169. {
  170. u16 *cache = codec->reg_cache;
  171. int i, r = 0;
  172. for (i = 0; i < AK4642_CACHEREGNUM; i++)
  173. r |= ak4642_write(codec, i, cache[i]);
  174. return r;
  175. };
  176. static int ak4642_dai_startup(struct snd_pcm_substream *substream,
  177. struct snd_soc_dai *dai)
  178. {
  179. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  180. struct snd_soc_codec *codec = dai->codec;
  181. if (is_play) {
  182. /*
  183. * start headphone output
  184. *
  185. * PLL, Master Mode
  186. * Audio I/F Format :MSB justified (ADC & DAC)
  187. * Bass Boost Level : Middle
  188. *
  189. * This operation came from example code of
  190. * "ASAHI KASEI AK4642" (japanese) manual p97.
  191. */
  192. ak4642_write(codec, 0x0f, 0x09);
  193. ak4642_write(codec, 0x0e, 0x19);
  194. ak4642_write(codec, 0x09, 0x91);
  195. ak4642_write(codec, 0x0c, 0x91);
  196. ak4642_write(codec, 0x00, 0x64);
  197. snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
  198. snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
  199. } else {
  200. /*
  201. * start stereo input
  202. *
  203. * PLL Master Mode
  204. * Audio I/F Format:MSB justified (ADC & DAC)
  205. * Pre MIC AMP:+20dB
  206. * MIC Power On
  207. * ALC setting:Refer to Table 35
  208. * ALC bit=“1”
  209. *
  210. * This operation came from example code of
  211. * "ASAHI KASEI AK4642" (japanese) manual p94.
  212. */
  213. ak4642_write(codec, 0x02, 0x05);
  214. ak4642_write(codec, 0x06, 0x3c);
  215. ak4642_write(codec, 0x08, 0xe1);
  216. ak4642_write(codec, 0x0b, 0x00);
  217. ak4642_write(codec, 0x07, 0x21);
  218. ak4642_write(codec, 0x00, 0x41);
  219. ak4642_write(codec, 0x10, 0x01);
  220. }
  221. return 0;
  222. }
  223. static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
  224. struct snd_soc_dai *dai)
  225. {
  226. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  227. struct snd_soc_codec *codec = dai->codec;
  228. if (is_play) {
  229. /* stop headphone output */
  230. snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
  231. snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
  232. ak4642_write(codec, 0x00, 0x40);
  233. ak4642_write(codec, 0x0e, 0x11);
  234. ak4642_write(codec, 0x0f, 0x08);
  235. } else {
  236. /* stop stereo input */
  237. ak4642_write(codec, 0x00, 0x40);
  238. ak4642_write(codec, 0x10, 0x00);
  239. ak4642_write(codec, 0x07, 0x01);
  240. }
  241. }
  242. static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
  243. int clk_id, unsigned int freq, int dir)
  244. {
  245. struct snd_soc_codec *codec = codec_dai->codec;
  246. u8 pll;
  247. switch (freq) {
  248. case 11289600:
  249. pll = PLL2;
  250. break;
  251. case 12288000:
  252. pll = PLL2 | PLL0;
  253. break;
  254. case 12000000:
  255. pll = PLL2 | PLL1;
  256. break;
  257. case 24000000:
  258. pll = PLL2 | PLL1 | PLL0;
  259. break;
  260. case 13500000:
  261. pll = PLL3 | PLL2;
  262. break;
  263. case 27000000:
  264. pll = PLL3 | PLL2 | PLL0;
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
  270. return 0;
  271. }
  272. static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  273. {
  274. struct snd_soc_codec *codec = dai->codec;
  275. u8 data;
  276. u8 bcko;
  277. data = MCKO | PMPLL; /* use MCKO */
  278. bcko = 0;
  279. /* set master/slave audio interface */
  280. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  281. case SND_SOC_DAIFMT_CBM_CFM:
  282. data |= MS;
  283. bcko = BCKO_64;
  284. break;
  285. case SND_SOC_DAIFMT_CBS_CFS:
  286. break;
  287. default:
  288. return -EINVAL;
  289. }
  290. snd_soc_update_bits(codec, PW_MGMT2, MS, data);
  291. snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
  292. return 0;
  293. }
  294. static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
  295. struct snd_pcm_hw_params *params,
  296. struct snd_soc_dai *dai)
  297. {
  298. struct snd_soc_codec *codec = dai->codec;
  299. u8 rate;
  300. switch (params_rate(params)) {
  301. case 7350:
  302. rate = FS2;
  303. break;
  304. case 8000:
  305. rate = 0;
  306. break;
  307. case 11025:
  308. rate = FS2 | FS0;
  309. break;
  310. case 12000:
  311. rate = FS0;
  312. break;
  313. case 14700:
  314. rate = FS2 | FS1;
  315. break;
  316. case 16000:
  317. rate = FS1;
  318. break;
  319. case 22050:
  320. rate = FS2 | FS1 | FS0;
  321. break;
  322. case 24000:
  323. rate = FS1 | FS0;
  324. break;
  325. case 29400:
  326. rate = FS3 | FS2 | FS1;
  327. break;
  328. case 32000:
  329. rate = FS3 | FS1;
  330. break;
  331. case 44100:
  332. rate = FS3 | FS2 | FS1 | FS0;
  333. break;
  334. case 48000:
  335. rate = FS3 | FS1 | FS0;
  336. break;
  337. default:
  338. return -EINVAL;
  339. break;
  340. }
  341. snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
  342. return 0;
  343. }
  344. static struct snd_soc_dai_ops ak4642_dai_ops = {
  345. .startup = ak4642_dai_startup,
  346. .shutdown = ak4642_dai_shutdown,
  347. .set_sysclk = ak4642_dai_set_sysclk,
  348. .set_fmt = ak4642_dai_set_fmt,
  349. .hw_params = ak4642_dai_hw_params,
  350. };
  351. struct snd_soc_dai ak4642_dai = {
  352. .name = "AK4642",
  353. .playback = {
  354. .stream_name = "Playback",
  355. .channels_min = 1,
  356. .channels_max = 2,
  357. .rates = SNDRV_PCM_RATE_8000_48000,
  358. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  359. .capture = {
  360. .stream_name = "Capture",
  361. .channels_min = 1,
  362. .channels_max = 2,
  363. .rates = SNDRV_PCM_RATE_8000_48000,
  364. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  365. .ops = &ak4642_dai_ops,
  366. .symmetric_rates = 1,
  367. };
  368. EXPORT_SYMBOL_GPL(ak4642_dai);
  369. static int ak4642_resume(struct platform_device *pdev)
  370. {
  371. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  372. struct snd_soc_codec *codec = socdev->card->codec;
  373. ak4642_sync(codec);
  374. return 0;
  375. }
  376. /*
  377. * initialise the AK4642 driver
  378. * register the mixer and dsp interfaces with the kernel
  379. */
  380. static int ak4642_init(struct ak4642_priv *ak4642)
  381. {
  382. struct snd_soc_codec *codec = &ak4642->codec;
  383. int ret = 0;
  384. if (ak4642_codec) {
  385. dev_err(codec->dev, "Another ak4642 is registered\n");
  386. return -EINVAL;
  387. }
  388. mutex_init(&codec->mutex);
  389. INIT_LIST_HEAD(&codec->dapm_widgets);
  390. INIT_LIST_HEAD(&codec->dapm_paths);
  391. snd_soc_codec_set_drvdata(codec, ak4642);
  392. codec->name = "AK4642";
  393. codec->owner = THIS_MODULE;
  394. codec->read = ak4642_read_reg_cache;
  395. codec->write = ak4642_write;
  396. codec->dai = &ak4642_dai;
  397. codec->num_dai = 1;
  398. codec->hw_write = (hw_write_t)i2c_master_send;
  399. codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
  400. codec->reg_cache = kmemdup(ak4642_reg,
  401. sizeof(ak4642_reg), GFP_KERNEL);
  402. if (!codec->reg_cache)
  403. return -ENOMEM;
  404. ak4642_dai.dev = codec->dev;
  405. ak4642_codec = codec;
  406. ret = snd_soc_register_codec(codec);
  407. if (ret) {
  408. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  409. goto reg_cache_err;
  410. }
  411. ret = snd_soc_register_dai(&ak4642_dai);
  412. if (ret) {
  413. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  414. snd_soc_unregister_codec(codec);
  415. goto reg_cache_err;
  416. }
  417. return ret;
  418. reg_cache_err:
  419. kfree(codec->reg_cache);
  420. codec->reg_cache = NULL;
  421. return ret;
  422. }
  423. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  424. static int ak4642_i2c_probe(struct i2c_client *i2c,
  425. const struct i2c_device_id *id)
  426. {
  427. struct ak4642_priv *ak4642;
  428. struct snd_soc_codec *codec;
  429. int ret;
  430. ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
  431. if (!ak4642)
  432. return -ENOMEM;
  433. codec = &ak4642->codec;
  434. codec->dev = &i2c->dev;
  435. i2c_set_clientdata(i2c, ak4642);
  436. codec->control_data = i2c;
  437. ret = ak4642_init(ak4642);
  438. if (ret < 0) {
  439. printk(KERN_ERR "failed to initialise AK4642\n");
  440. kfree(ak4642);
  441. }
  442. return ret;
  443. }
  444. static int ak4642_i2c_remove(struct i2c_client *client)
  445. {
  446. struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
  447. snd_soc_unregister_dai(&ak4642_dai);
  448. snd_soc_unregister_codec(&ak4642->codec);
  449. kfree(ak4642->codec.reg_cache);
  450. kfree(ak4642);
  451. ak4642_codec = NULL;
  452. return 0;
  453. }
  454. static const struct i2c_device_id ak4642_i2c_id[] = {
  455. { "ak4642", 0 },
  456. { "ak4643", 0 },
  457. { }
  458. };
  459. MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
  460. static struct i2c_driver ak4642_i2c_driver = {
  461. .driver = {
  462. .name = "AK4642 I2C Codec",
  463. .owner = THIS_MODULE,
  464. },
  465. .probe = ak4642_i2c_probe,
  466. .remove = ak4642_i2c_remove,
  467. .id_table = ak4642_i2c_id,
  468. };
  469. #endif
  470. static int ak4642_probe(struct platform_device *pdev)
  471. {
  472. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  473. int ret;
  474. if (!ak4642_codec) {
  475. dev_err(&pdev->dev, "Codec device not registered\n");
  476. return -ENODEV;
  477. }
  478. socdev->card->codec = ak4642_codec;
  479. /* register pcms */
  480. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  481. if (ret < 0) {
  482. printk(KERN_ERR "ak4642: failed to create pcms\n");
  483. goto pcm_err;
  484. }
  485. snd_soc_add_controls(ak4642_codec, ak4642_snd_controls,
  486. ARRAY_SIZE(ak4642_snd_controls));
  487. dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
  488. return ret;
  489. pcm_err:
  490. return ret;
  491. }
  492. /* power down chip */
  493. static int ak4642_remove(struct platform_device *pdev)
  494. {
  495. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  496. snd_soc_free_pcms(socdev);
  497. snd_soc_dapm_free(socdev);
  498. return 0;
  499. }
  500. struct snd_soc_codec_device soc_codec_dev_ak4642 = {
  501. .probe = ak4642_probe,
  502. .remove = ak4642_remove,
  503. .resume = ak4642_resume,
  504. };
  505. EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
  506. static int __init ak4642_modinit(void)
  507. {
  508. int ret = 0;
  509. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  510. ret = i2c_add_driver(&ak4642_i2c_driver);
  511. #endif
  512. return ret;
  513. }
  514. module_init(ak4642_modinit);
  515. static void __exit ak4642_exit(void)
  516. {
  517. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  518. i2c_del_driver(&ak4642_i2c_driver);
  519. #endif
  520. }
  521. module_exit(ak4642_exit);
  522. MODULE_DESCRIPTION("Soc AK4642 driver");
  523. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  524. MODULE_LICENSE("GPL");