ak4642.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. #define AK4642_VERSION "0.0.1"
  31. #define PW_MGMT1 0x00
  32. #define PW_MGMT2 0x01
  33. #define SG_SL1 0x02
  34. #define SG_SL2 0x03
  35. #define MD_CTL1 0x04
  36. #define MD_CTL2 0x05
  37. #define TIMER 0x06
  38. #define ALC_CTL1 0x07
  39. #define ALC_CTL2 0x08
  40. #define L_IVC 0x09
  41. #define L_DVC 0x0a
  42. #define ALC_CTL3 0x0b
  43. #define R_IVC 0x0c
  44. #define R_DVC 0x0d
  45. #define MD_CTL3 0x0e
  46. #define MD_CTL4 0x0f
  47. #define PW_MGMT3 0x10
  48. #define DF_S 0x11
  49. #define FIL3_0 0x12
  50. #define FIL3_1 0x13
  51. #define FIL3_2 0x14
  52. #define FIL3_3 0x15
  53. #define EQ_0 0x16
  54. #define EQ_1 0x17
  55. #define EQ_2 0x18
  56. #define EQ_3 0x19
  57. #define EQ_4 0x1a
  58. #define EQ_5 0x1b
  59. #define FIL1_0 0x1c
  60. #define FIL1_1 0x1d
  61. #define FIL1_2 0x1e
  62. #define FIL1_3 0x1f
  63. #define PW_MGMT4 0x20
  64. #define MD_CTL5 0x21
  65. #define LO_MS 0x22
  66. #define HP_MS 0x23
  67. #define SPK_MS 0x24
  68. #define AK4642_CACHEREGNUM 0x25
  69. /* PW_MGMT2 */
  70. #define HPMTN (1 << 6)
  71. #define PMHPL (1 << 5)
  72. #define PMHPR (1 << 4)
  73. #define MS (1 << 3) /* master/slave select */
  74. #define MCKO (1 << 1)
  75. #define PMPLL (1 << 0)
  76. #define PMHP_MASK (PMHPL | PMHPR)
  77. #define PMHP PMHP_MASK
  78. /* MD_CTL1 */
  79. #define PLL3 (1 << 7)
  80. #define PLL2 (1 << 6)
  81. #define PLL1 (1 << 5)
  82. #define PLL0 (1 << 4)
  83. #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
  84. #define BCKO_MASK (1 << 3)
  85. #define BCKO_64 BCKO_MASK
  86. /* MD_CTL2 */
  87. #define FS0 (1 << 0)
  88. #define FS1 (1 << 1)
  89. #define FS2 (1 << 2)
  90. #define FS3 (1 << 5)
  91. #define FS_MASK (FS0 | FS1 | FS2 | FS3)
  92. /*
  93. * Playback Volume (table 39)
  94. *
  95. * max : 0x00 : +12.0 dB
  96. * ( 0.5 dB step )
  97. * min : 0xFE : -115.0 dB
  98. * mute: 0xFF
  99. */
  100. static const DECLARE_TLV_DB_SCALE(out_tlv, -11500, 50, 1);
  101. static const struct snd_kcontrol_new ak4642_snd_controls[] = {
  102. SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
  103. 0, 0xFF, 1, out_tlv),
  104. };
  105. /* codec private data */
  106. struct ak4642_priv {
  107. unsigned int sysclk;
  108. enum snd_soc_control_type control_type;
  109. void *control_data;
  110. };
  111. /*
  112. * ak4642 register cache
  113. */
  114. static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
  115. 0x0000, 0x0000, 0x0001, 0x0000,
  116. 0x0002, 0x0000, 0x0000, 0x0000,
  117. 0x00e1, 0x00e1, 0x0018, 0x0000,
  118. 0x00e1, 0x0018, 0x0011, 0x0008,
  119. 0x0000, 0x0000, 0x0000, 0x0000,
  120. 0x0000, 0x0000, 0x0000, 0x0000,
  121. 0x0000, 0x0000, 0x0000, 0x0000,
  122. 0x0000, 0x0000, 0x0000, 0x0000,
  123. 0x0000, 0x0000, 0x0000, 0x0000,
  124. 0x0000,
  125. };
  126. /*
  127. * read ak4642 register cache
  128. */
  129. static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
  130. unsigned int reg)
  131. {
  132. u16 *cache = codec->reg_cache;
  133. if (reg >= AK4642_CACHEREGNUM)
  134. return -1;
  135. return cache[reg];
  136. }
  137. /*
  138. * write ak4642 register cache
  139. */
  140. static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
  141. u16 reg, unsigned int value)
  142. {
  143. u16 *cache = codec->reg_cache;
  144. if (reg >= AK4642_CACHEREGNUM)
  145. return;
  146. cache[reg] = value;
  147. }
  148. /*
  149. * write to the AK4642 register space
  150. */
  151. static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
  152. unsigned int value)
  153. {
  154. u8 data[2];
  155. /* data is
  156. * D15..D8 AK4642 register offset
  157. * D7...D0 register data
  158. */
  159. data[0] = reg & 0xff;
  160. data[1] = value & 0xff;
  161. if (codec->hw_write(codec->control_data, data, 2) == 2) {
  162. ak4642_write_reg_cache(codec, reg, value);
  163. return 0;
  164. } else
  165. return -EIO;
  166. }
  167. static int ak4642_sync(struct snd_soc_codec *codec)
  168. {
  169. u16 *cache = codec->reg_cache;
  170. int i, r = 0;
  171. for (i = 0; i < AK4642_CACHEREGNUM; i++)
  172. r |= ak4642_write(codec, i, cache[i]);
  173. return r;
  174. };
  175. static int ak4642_dai_startup(struct snd_pcm_substream *substream,
  176. struct snd_soc_dai *dai)
  177. {
  178. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  179. struct snd_soc_codec *codec = dai->codec;
  180. if (is_play) {
  181. /*
  182. * start headphone output
  183. *
  184. * PLL, Master Mode
  185. * Audio I/F Format :MSB justified (ADC & DAC)
  186. * Bass Boost Level : Middle
  187. *
  188. * This operation came from example code of
  189. * "ASAHI KASEI AK4642" (japanese) manual p97.
  190. */
  191. ak4642_write(codec, 0x0f, 0x09);
  192. ak4642_write(codec, 0x0e, 0x19);
  193. ak4642_write(codec, 0x09, 0x91);
  194. ak4642_write(codec, 0x0c, 0x91);
  195. ak4642_write(codec, 0x00, 0x64);
  196. snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
  197. snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
  198. } else {
  199. /*
  200. * start stereo input
  201. *
  202. * PLL Master Mode
  203. * Audio I/F Format:MSB justified (ADC & DAC)
  204. * Pre MIC AMP:+20dB
  205. * MIC Power On
  206. * ALC setting:Refer to Table 35
  207. * ALC bit=“1”
  208. *
  209. * This operation came from example code of
  210. * "ASAHI KASEI AK4642" (japanese) manual p94.
  211. */
  212. ak4642_write(codec, 0x02, 0x05);
  213. ak4642_write(codec, 0x06, 0x3c);
  214. ak4642_write(codec, 0x08, 0xe1);
  215. ak4642_write(codec, 0x0b, 0x00);
  216. ak4642_write(codec, 0x07, 0x21);
  217. ak4642_write(codec, 0x00, 0x41);
  218. ak4642_write(codec, 0x10, 0x01);
  219. }
  220. return 0;
  221. }
  222. static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
  223. struct snd_soc_dai *dai)
  224. {
  225. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  226. struct snd_soc_codec *codec = dai->codec;
  227. if (is_play) {
  228. /* stop headphone output */
  229. snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
  230. snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
  231. ak4642_write(codec, 0x00, 0x40);
  232. ak4642_write(codec, 0x0e, 0x11);
  233. ak4642_write(codec, 0x0f, 0x08);
  234. } else {
  235. /* stop stereo input */
  236. ak4642_write(codec, 0x00, 0x40);
  237. ak4642_write(codec, 0x10, 0x00);
  238. ak4642_write(codec, 0x07, 0x01);
  239. }
  240. }
  241. static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
  242. int clk_id, unsigned int freq, int dir)
  243. {
  244. struct snd_soc_codec *codec = codec_dai->codec;
  245. u8 pll;
  246. switch (freq) {
  247. case 11289600:
  248. pll = PLL2;
  249. break;
  250. case 12288000:
  251. pll = PLL2 | PLL0;
  252. break;
  253. case 12000000:
  254. pll = PLL2 | PLL1;
  255. break;
  256. case 24000000:
  257. pll = PLL2 | PLL1 | PLL0;
  258. break;
  259. case 13500000:
  260. pll = PLL3 | PLL2;
  261. break;
  262. case 27000000:
  263. pll = PLL3 | PLL2 | PLL0;
  264. break;
  265. default:
  266. return -EINVAL;
  267. }
  268. snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
  269. return 0;
  270. }
  271. static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  272. {
  273. struct snd_soc_codec *codec = dai->codec;
  274. u8 data;
  275. u8 bcko;
  276. data = MCKO | PMPLL; /* use MCKO */
  277. bcko = 0;
  278. /* set master/slave audio interface */
  279. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  280. case SND_SOC_DAIFMT_CBM_CFM:
  281. data |= MS;
  282. bcko = BCKO_64;
  283. break;
  284. case SND_SOC_DAIFMT_CBS_CFS:
  285. break;
  286. default:
  287. return -EINVAL;
  288. }
  289. snd_soc_update_bits(codec, PW_MGMT2, MS, data);
  290. snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
  291. return 0;
  292. }
  293. static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
  294. struct snd_pcm_hw_params *params,
  295. struct snd_soc_dai *dai)
  296. {
  297. struct snd_soc_codec *codec = dai->codec;
  298. u8 rate;
  299. switch (params_rate(params)) {
  300. case 7350:
  301. rate = FS2;
  302. break;
  303. case 8000:
  304. rate = 0;
  305. break;
  306. case 11025:
  307. rate = FS2 | FS0;
  308. break;
  309. case 12000:
  310. rate = FS0;
  311. break;
  312. case 14700:
  313. rate = FS2 | FS1;
  314. break;
  315. case 16000:
  316. rate = FS1;
  317. break;
  318. case 22050:
  319. rate = FS2 | FS1 | FS0;
  320. break;
  321. case 24000:
  322. rate = FS1 | FS0;
  323. break;
  324. case 29400:
  325. rate = FS3 | FS2 | FS1;
  326. break;
  327. case 32000:
  328. rate = FS3 | FS1;
  329. break;
  330. case 44100:
  331. rate = FS3 | FS2 | FS1 | FS0;
  332. break;
  333. case 48000:
  334. rate = FS3 | FS1 | FS0;
  335. break;
  336. default:
  337. return -EINVAL;
  338. break;
  339. }
  340. snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
  341. return 0;
  342. }
  343. static struct snd_soc_dai_ops ak4642_dai_ops = {
  344. .startup = ak4642_dai_startup,
  345. .shutdown = ak4642_dai_shutdown,
  346. .set_sysclk = ak4642_dai_set_sysclk,
  347. .set_fmt = ak4642_dai_set_fmt,
  348. .hw_params = ak4642_dai_hw_params,
  349. };
  350. static struct snd_soc_dai_driver ak4642_dai = {
  351. .name = "ak4642-hifi",
  352. .playback = {
  353. .stream_name = "Playback",
  354. .channels_min = 1,
  355. .channels_max = 2,
  356. .rates = SNDRV_PCM_RATE_8000_48000,
  357. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  358. .capture = {
  359. .stream_name = "Capture",
  360. .channels_min = 1,
  361. .channels_max = 2,
  362. .rates = SNDRV_PCM_RATE_8000_48000,
  363. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  364. .ops = &ak4642_dai_ops,
  365. .symmetric_rates = 1,
  366. };
  367. static int ak4642_resume(struct snd_soc_codec *codec)
  368. {
  369. ak4642_sync(codec);
  370. return 0;
  371. }
  372. static int ak4642_probe(struct snd_soc_codec *codec)
  373. {
  374. struct ak4642_priv *ak4642 = snd_soc_codec_get_drvdata(codec);
  375. dev_info(codec->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
  376. codec->hw_write = (hw_write_t)i2c_master_send;
  377. codec->control_data = ak4642->control_data;
  378. return 0;
  379. }
  380. static struct snd_soc_codec_driver soc_codec_dev_ak4642 = {
  381. .probe = ak4642_probe,
  382. .resume = ak4642_resume,
  383. .read = ak4642_read_reg_cache,
  384. .write = ak4642_write,
  385. .reg_cache_size = ARRAY_SIZE(ak4642_reg),
  386. .reg_word_size = sizeof(u8),
  387. .reg_cache_default = ak4642_reg,
  388. };
  389. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  390. static __devinit int ak4642_i2c_probe(struct i2c_client *i2c,
  391. const struct i2c_device_id *id)
  392. {
  393. struct ak4642_priv *ak4642;
  394. int ret;
  395. ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
  396. if (ak4642 == NULL)
  397. return -ENOMEM;
  398. i2c_set_clientdata(i2c, ak4642);
  399. ak4642->control_data = i2c;
  400. ak4642->control_type = SND_SOC_I2C;
  401. ret = snd_soc_register_codec(&i2c->dev,
  402. &soc_codec_dev_ak4642, &ak4642_dai, 1);
  403. if (ret < 0)
  404. kfree(ak4642);
  405. return ret;
  406. }
  407. static __devexit int ak4642_i2c_remove(struct i2c_client *client)
  408. {
  409. snd_soc_unregister_codec(&client->dev);
  410. kfree(i2c_get_clientdata(client));
  411. return 0;
  412. }
  413. static const struct i2c_device_id ak4642_i2c_id[] = {
  414. { "ak4642", 0 },
  415. { "ak4643", 0 },
  416. { }
  417. };
  418. MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
  419. static struct i2c_driver ak4642_i2c_driver = {
  420. .driver = {
  421. .name = "ak4642-codec",
  422. .owner = THIS_MODULE,
  423. },
  424. .probe = ak4642_i2c_probe,
  425. .remove = __devexit_p(ak4642_i2c_remove),
  426. .id_table = ak4642_i2c_id,
  427. };
  428. #endif
  429. static int __init ak4642_modinit(void)
  430. {
  431. int ret = 0;
  432. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  433. ret = i2c_add_driver(&ak4642_i2c_driver);
  434. #endif
  435. return ret;
  436. }
  437. module_init(ak4642_modinit);
  438. static void __exit ak4642_exit(void)
  439. {
  440. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  441. i2c_del_driver(&ak4642_i2c_driver);
  442. #endif
  443. }
  444. module_exit(ak4642_exit);
  445. MODULE_DESCRIPTION("Soc AK4642 driver");
  446. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  447. MODULE_LICENSE("GPL");