ak4642.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/pm.h>
  28. #include <linux/i2c.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <sound/core.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/soc.h>
  35. #include <sound/soc-dapm.h>
  36. #include <sound/initval.h>
  37. #include "ak4642.h"
  38. #define AK4642_VERSION "0.0.1"
  39. #define PW_MGMT1 0x00
  40. #define PW_MGMT2 0x01
  41. #define SG_SL1 0x02
  42. #define SG_SL2 0x03
  43. #define MD_CTL1 0x04
  44. #define MD_CTL2 0x05
  45. #define TIMER 0x06
  46. #define ALC_CTL1 0x07
  47. #define ALC_CTL2 0x08
  48. #define L_IVC 0x09
  49. #define L_DVC 0x0a
  50. #define ALC_CTL3 0x0b
  51. #define R_IVC 0x0c
  52. #define R_DVC 0x0d
  53. #define MD_CTL3 0x0e
  54. #define MD_CTL4 0x0f
  55. #define PW_MGMT3 0x10
  56. #define DF_S 0x11
  57. #define FIL3_0 0x12
  58. #define FIL3_1 0x13
  59. #define FIL3_2 0x14
  60. #define FIL3_3 0x15
  61. #define EQ_0 0x16
  62. #define EQ_1 0x17
  63. #define EQ_2 0x18
  64. #define EQ_3 0x19
  65. #define EQ_4 0x1a
  66. #define EQ_5 0x1b
  67. #define FIL1_0 0x1c
  68. #define FIL1_1 0x1d
  69. #define FIL1_2 0x1e
  70. #define FIL1_3 0x1f
  71. #define PW_MGMT4 0x20
  72. #define MD_CTL5 0x21
  73. #define LO_MS 0x22
  74. #define HP_MS 0x23
  75. #define SPK_MS 0x24
  76. #define AK4642_CACHEREGNUM 0x25
  77. struct snd_soc_codec_device soc_codec_dev_ak4642;
  78. /* codec private data */
  79. struct ak4642_priv {
  80. struct snd_soc_codec codec;
  81. unsigned int sysclk;
  82. };
  83. static struct snd_soc_codec *ak4642_codec;
  84. /*
  85. * ak4642 register cache
  86. */
  87. static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
  88. 0x0000, 0x0000, 0x0001, 0x0000,
  89. 0x0002, 0x0000, 0x0000, 0x0000,
  90. 0x00e1, 0x00e1, 0x0018, 0x0000,
  91. 0x00e1, 0x0018, 0x0011, 0x0008,
  92. 0x0000, 0x0000, 0x0000, 0x0000,
  93. 0x0000, 0x0000, 0x0000, 0x0000,
  94. 0x0000, 0x0000, 0x0000, 0x0000,
  95. 0x0000, 0x0000, 0x0000, 0x0000,
  96. 0x0000, 0x0000, 0x0000, 0x0000,
  97. 0x0000,
  98. };
  99. /*
  100. * read ak4642 register cache
  101. */
  102. static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
  103. unsigned int reg)
  104. {
  105. u16 *cache = codec->reg_cache;
  106. if (reg >= AK4642_CACHEREGNUM)
  107. return -1;
  108. return cache[reg];
  109. }
  110. /*
  111. * write ak4642 register cache
  112. */
  113. static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
  114. u16 reg, unsigned int value)
  115. {
  116. u16 *cache = codec->reg_cache;
  117. if (reg >= AK4642_CACHEREGNUM)
  118. return;
  119. cache[reg] = value;
  120. }
  121. /*
  122. * write to the AK4642 register space
  123. */
  124. static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
  125. unsigned int value)
  126. {
  127. u8 data[2];
  128. /* data is
  129. * D15..D8 AK4642 register offset
  130. * D7...D0 register data
  131. */
  132. data[0] = reg & 0xff;
  133. data[1] = value & 0xff;
  134. if (codec->hw_write(codec->control_data, data, 2) == 2) {
  135. ak4642_write_reg_cache(codec, reg, value);
  136. return 0;
  137. } else
  138. return -EIO;
  139. }
  140. static int ak4642_sync(struct snd_soc_codec *codec)
  141. {
  142. u16 *cache = codec->reg_cache;
  143. int i, r = 0;
  144. for (i = 0; i < AK4642_CACHEREGNUM; i++)
  145. r |= ak4642_write(codec, i, cache[i]);
  146. return r;
  147. };
  148. static int ak4642_dai_startup(struct snd_pcm_substream *substream,
  149. struct snd_soc_dai *dai)
  150. {
  151. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  152. struct snd_soc_codec *codec = dai->codec;
  153. if (is_play) {
  154. /*
  155. * start headphone output
  156. *
  157. * PLL, Master Mode
  158. * Audio I/F Format :MSB justified (ADC & DAC)
  159. * Sampling Frequency: 44.1kHz
  160. * Digital Volume: −8dB
  161. * Bass Boost Level : Middle
  162. *
  163. * This operation came from example code of
  164. * "ASAHI KASEI AK4642" (japanese) manual p97.
  165. *
  166. * Example code use 0x39, 0x79 value for 0x01 address,
  167. * But we need MCKO (0x02) bit now
  168. */
  169. ak4642_write(codec, 0x05, 0x27);
  170. ak4642_write(codec, 0x0f, 0x09);
  171. ak4642_write(codec, 0x0e, 0x19);
  172. ak4642_write(codec, 0x09, 0x91);
  173. ak4642_write(codec, 0x0c, 0x91);
  174. ak4642_write(codec, 0x0a, 0x28);
  175. ak4642_write(codec, 0x0d, 0x28);
  176. ak4642_write(codec, 0x00, 0x64);
  177. ak4642_write(codec, 0x01, 0x3b); /* + MCKO bit */
  178. ak4642_write(codec, 0x01, 0x7b); /* + MCKO bit */
  179. } else {
  180. /*
  181. * start stereo input
  182. *
  183. * PLL Master Mode
  184. * Audio I/F Format:MSB justified (ADC & DAC)
  185. * Sampling Frequency:44.1kHz
  186. * Pre MIC AMP:+20dB
  187. * MIC Power On
  188. * ALC setting:Refer to Table 35
  189. * ALC bit=“1”
  190. *
  191. * This operation came from example code of
  192. * "ASAHI KASEI AK4642" (japanese) manual p94.
  193. */
  194. ak4642_write(codec, 0x05, 0x27);
  195. ak4642_write(codec, 0x02, 0x05);
  196. ak4642_write(codec, 0x06, 0x3c);
  197. ak4642_write(codec, 0x08, 0xe1);
  198. ak4642_write(codec, 0x0b, 0x00);
  199. ak4642_write(codec, 0x07, 0x21);
  200. ak4642_write(codec, 0x00, 0x41);
  201. ak4642_write(codec, 0x10, 0x01);
  202. }
  203. return 0;
  204. }
  205. static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
  206. struct snd_soc_dai *dai)
  207. {
  208. int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  209. struct snd_soc_codec *codec = dai->codec;
  210. if (is_play) {
  211. /* stop headphone output */
  212. ak4642_write(codec, 0x01, 0x3b);
  213. ak4642_write(codec, 0x01, 0x0b);
  214. ak4642_write(codec, 0x00, 0x40);
  215. ak4642_write(codec, 0x0e, 0x11);
  216. ak4642_write(codec, 0x0f, 0x08);
  217. } else {
  218. /* stop stereo input */
  219. ak4642_write(codec, 0x00, 0x40);
  220. ak4642_write(codec, 0x10, 0x00);
  221. ak4642_write(codec, 0x07, 0x01);
  222. }
  223. }
  224. static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
  225. int clk_id, unsigned int freq, int dir)
  226. {
  227. struct snd_soc_codec *codec = codec_dai->codec;
  228. struct ak4642_priv *ak4642 = codec->private_data;
  229. ak4642->sysclk = freq;
  230. return 0;
  231. }
  232. static struct snd_soc_dai_ops ak4642_dai_ops = {
  233. .startup = ak4642_dai_startup,
  234. .shutdown = ak4642_dai_shutdown,
  235. .set_sysclk = ak4642_dai_set_sysclk,
  236. };
  237. struct snd_soc_dai ak4642_dai = {
  238. .name = "AK4642",
  239. .playback = {
  240. .stream_name = "Playback",
  241. .channels_min = 1,
  242. .channels_max = 2,
  243. .rates = SNDRV_PCM_RATE_8000_48000,
  244. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  245. .capture = {
  246. .stream_name = "Capture",
  247. .channels_min = 1,
  248. .channels_max = 2,
  249. .rates = SNDRV_PCM_RATE_8000_48000,
  250. .formats = SNDRV_PCM_FMTBIT_S16_LE },
  251. .ops = &ak4642_dai_ops,
  252. };
  253. EXPORT_SYMBOL_GPL(ak4642_dai);
  254. static int ak4642_resume(struct platform_device *pdev)
  255. {
  256. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  257. struct snd_soc_codec *codec = socdev->card->codec;
  258. ak4642_sync(codec);
  259. return 0;
  260. }
  261. /*
  262. * initialise the AK4642 driver
  263. * register the mixer and dsp interfaces with the kernel
  264. */
  265. static int ak4642_init(struct ak4642_priv *ak4642)
  266. {
  267. struct snd_soc_codec *codec = &ak4642->codec;
  268. int ret = 0;
  269. if (ak4642_codec) {
  270. dev_err(codec->dev, "Another ak4642 is registered\n");
  271. return -EINVAL;
  272. }
  273. mutex_init(&codec->mutex);
  274. INIT_LIST_HEAD(&codec->dapm_widgets);
  275. INIT_LIST_HEAD(&codec->dapm_paths);
  276. codec->private_data = ak4642;
  277. codec->name = "AK4642";
  278. codec->owner = THIS_MODULE;
  279. codec->read = ak4642_read_reg_cache;
  280. codec->write = ak4642_write;
  281. codec->dai = &ak4642_dai;
  282. codec->num_dai = 1;
  283. codec->hw_write = (hw_write_t)i2c_master_send;
  284. codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
  285. codec->reg_cache = kmemdup(ak4642_reg,
  286. sizeof(ak4642_reg), GFP_KERNEL);
  287. if (!codec->reg_cache)
  288. return -ENOMEM;
  289. ak4642_dai.dev = codec->dev;
  290. ak4642_codec = codec;
  291. ret = snd_soc_register_codec(codec);
  292. if (ret) {
  293. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  294. goto reg_cache_err;
  295. }
  296. ret = snd_soc_register_dai(&ak4642_dai);
  297. if (ret) {
  298. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  299. snd_soc_unregister_codec(codec);
  300. goto reg_cache_err;
  301. }
  302. /*
  303. * clock setting
  304. *
  305. * Audio I/F Format: MSB justified (ADC & DAC)
  306. * BICK frequency at Master Mode: 64fs
  307. * Input Master Clock Select at PLL Mode: 11.2896MHz
  308. * MCKO: Enable
  309. * Sampling Frequency: 44.1kHz
  310. *
  311. * This operation came from example code of
  312. * "ASAHI KASEI AK4642" (japanese) manual p89.
  313. *
  314. * please fix-me
  315. */
  316. ak4642_write(codec, 0x01, 0x08);
  317. ak4642_write(codec, 0x04, 0x4a);
  318. ak4642_write(codec, 0x05, 0x27);
  319. ak4642_write(codec, 0x00, 0x40);
  320. ak4642_write(codec, 0x01, 0x0b);
  321. return ret;
  322. reg_cache_err:
  323. kfree(codec->reg_cache);
  324. codec->reg_cache = NULL;
  325. return ret;
  326. }
  327. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  328. static int ak4642_i2c_probe(struct i2c_client *i2c,
  329. const struct i2c_device_id *id)
  330. {
  331. struct ak4642_priv *ak4642;
  332. struct snd_soc_codec *codec;
  333. int ret;
  334. ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
  335. if (!ak4642)
  336. return -ENOMEM;
  337. codec = &ak4642->codec;
  338. codec->dev = &i2c->dev;
  339. i2c_set_clientdata(i2c, ak4642);
  340. codec->control_data = i2c;
  341. ret = ak4642_init(ak4642);
  342. if (ret < 0)
  343. printk(KERN_ERR "failed to initialise AK4642\n");
  344. return ret;
  345. }
  346. static int ak4642_i2c_remove(struct i2c_client *client)
  347. {
  348. struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
  349. snd_soc_unregister_dai(&ak4642_dai);
  350. snd_soc_unregister_codec(&ak4642->codec);
  351. kfree(ak4642->codec.reg_cache);
  352. kfree(ak4642);
  353. ak4642_codec = NULL;
  354. return 0;
  355. }
  356. static const struct i2c_device_id ak4642_i2c_id[] = {
  357. { "ak4642", 0 },
  358. { "ak4643", 0 },
  359. { }
  360. };
  361. MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
  362. static struct i2c_driver ak4642_i2c_driver = {
  363. .driver = {
  364. .name = "AK4642 I2C Codec",
  365. .owner = THIS_MODULE,
  366. },
  367. .probe = ak4642_i2c_probe,
  368. .remove = ak4642_i2c_remove,
  369. .id_table = ak4642_i2c_id,
  370. };
  371. #endif
  372. static int ak4642_probe(struct platform_device *pdev)
  373. {
  374. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  375. int ret;
  376. if (!ak4642_codec) {
  377. dev_err(&pdev->dev, "Codec device not registered\n");
  378. return -ENODEV;
  379. }
  380. socdev->card->codec = ak4642_codec;
  381. /* register pcms */
  382. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  383. if (ret < 0) {
  384. printk(KERN_ERR "ak4642: failed to create pcms\n");
  385. goto pcm_err;
  386. }
  387. dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
  388. return ret;
  389. pcm_err:
  390. return ret;
  391. }
  392. /* power down chip */
  393. static int ak4642_remove(struct platform_device *pdev)
  394. {
  395. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  396. snd_soc_free_pcms(socdev);
  397. snd_soc_dapm_free(socdev);
  398. return 0;
  399. }
  400. struct snd_soc_codec_device soc_codec_dev_ak4642 = {
  401. .probe = ak4642_probe,
  402. .remove = ak4642_remove,
  403. .resume = ak4642_resume,
  404. };
  405. EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
  406. static int __init ak4642_modinit(void)
  407. {
  408. int ret = 0;
  409. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  410. ret = i2c_add_driver(&ak4642_i2c_driver);
  411. #endif
  412. return ret;
  413. }
  414. module_init(ak4642_modinit);
  415. static void __exit ak4642_exit(void)
  416. {
  417. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  418. i2c_del_driver(&ak4642_i2c_driver);
  419. #endif
  420. }
  421. module_exit(ak4642_exit);
  422. MODULE_DESCRIPTION("Soc AK4642 driver");
  423. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  424. MODULE_LICENSE("GPL");