wm8728.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * wm8728.c -- WM8728 ALSA SoC Audio driver
  3. *
  4. * Copyright 2008 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/i2c.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spi/spi.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dapm.h>
  25. #include <sound/initval.h>
  26. #include <sound/tlv.h>
  27. #include "wm8728.h"
  28. struct snd_soc_codec_device soc_codec_dev_wm8728;
  29. /*
  30. * We can't read the WM8728 register space so we cache them instead.
  31. * Note that the defaults here aren't the physical defaults, we latch
  32. * the volume update bits, mute the output and enable infinite zero
  33. * detect.
  34. */
  35. static const u16 wm8728_reg_defaults[] = {
  36. 0x1ff,
  37. 0x1ff,
  38. 0x001,
  39. 0x100,
  40. };
  41. static inline unsigned int wm8728_read_reg_cache(struct snd_soc_codec *codec,
  42. unsigned int reg)
  43. {
  44. u16 *cache = codec->reg_cache;
  45. BUG_ON(reg > ARRAY_SIZE(wm8728_reg_defaults));
  46. return cache[reg];
  47. }
  48. static inline void wm8728_write_reg_cache(struct snd_soc_codec *codec,
  49. u16 reg, unsigned int value)
  50. {
  51. u16 *cache = codec->reg_cache;
  52. BUG_ON(reg > ARRAY_SIZE(wm8728_reg_defaults));
  53. cache[reg] = value;
  54. }
  55. /*
  56. * write to the WM8728 register space
  57. */
  58. static int wm8728_write(struct snd_soc_codec *codec, unsigned int reg,
  59. unsigned int value)
  60. {
  61. u8 data[2];
  62. /* data is
  63. * D15..D9 WM8728 register offset
  64. * D8...D0 register data
  65. */
  66. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  67. data[1] = value & 0x00ff;
  68. wm8728_write_reg_cache(codec, reg, value);
  69. if (codec->hw_write(codec->control_data, data, 2) == 2)
  70. return 0;
  71. else
  72. return -EIO;
  73. }
  74. static const DECLARE_TLV_DB_SCALE(wm8728_tlv, -12750, 50, 1);
  75. static const struct snd_kcontrol_new wm8728_snd_controls[] = {
  76. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8728_DACLVOL, WM8728_DACRVOL,
  77. 0, 255, 0, wm8728_tlv),
  78. SOC_SINGLE("Deemphasis", WM8728_DACCTL, 1, 1, 0),
  79. };
  80. /*
  81. * DAPM controls.
  82. */
  83. static const struct snd_soc_dapm_widget wm8728_dapm_widgets[] = {
  84. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0),
  85. SND_SOC_DAPM_OUTPUT("VOUTL"),
  86. SND_SOC_DAPM_OUTPUT("VOUTR"),
  87. };
  88. static const struct snd_soc_dapm_route intercon[] = {
  89. {"VOUTL", NULL, "DAC"},
  90. {"VOUTR", NULL, "DAC"},
  91. };
  92. static int wm8728_add_widgets(struct snd_soc_codec *codec)
  93. {
  94. snd_soc_dapm_new_controls(codec, wm8728_dapm_widgets,
  95. ARRAY_SIZE(wm8728_dapm_widgets));
  96. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  97. snd_soc_dapm_new_widgets(codec);
  98. return 0;
  99. }
  100. static int wm8728_mute(struct snd_soc_dai *dai, int mute)
  101. {
  102. struct snd_soc_codec *codec = dai->codec;
  103. u16 mute_reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  104. if (mute)
  105. wm8728_write(codec, WM8728_DACCTL, mute_reg | 1);
  106. else
  107. wm8728_write(codec, WM8728_DACCTL, mute_reg & ~1);
  108. return 0;
  109. }
  110. static int wm8728_hw_params(struct snd_pcm_substream *substream,
  111. struct snd_pcm_hw_params *params,
  112. struct snd_soc_dai *dai)
  113. {
  114. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  115. struct snd_soc_device *socdev = rtd->socdev;
  116. struct snd_soc_codec *codec = socdev->codec;
  117. u16 dac = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  118. dac &= ~0x18;
  119. switch (params_format(params)) {
  120. case SNDRV_PCM_FORMAT_S16_LE:
  121. break;
  122. case SNDRV_PCM_FORMAT_S20_3LE:
  123. dac |= 0x10;
  124. break;
  125. case SNDRV_PCM_FORMAT_S24_LE:
  126. dac |= 0x08;
  127. break;
  128. default:
  129. return -EINVAL;
  130. }
  131. wm8728_write(codec, WM8728_DACCTL, dac);
  132. return 0;
  133. }
  134. static int wm8728_set_dai_fmt(struct snd_soc_dai *codec_dai,
  135. unsigned int fmt)
  136. {
  137. struct snd_soc_codec *codec = codec_dai->codec;
  138. u16 iface = wm8728_read_reg_cache(codec, WM8728_IFCTL);
  139. /* Currently only I2S is supported by the driver, though the
  140. * hardware is more flexible.
  141. */
  142. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  143. case SND_SOC_DAIFMT_I2S:
  144. iface |= 1;
  145. break;
  146. default:
  147. return -EINVAL;
  148. }
  149. /* The hardware only support full slave mode */
  150. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  151. case SND_SOC_DAIFMT_CBS_CFS:
  152. break;
  153. default:
  154. return -EINVAL;
  155. }
  156. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  157. case SND_SOC_DAIFMT_NB_NF:
  158. iface &= ~0x22;
  159. break;
  160. case SND_SOC_DAIFMT_IB_NF:
  161. iface |= 0x20;
  162. iface &= ~0x02;
  163. break;
  164. case SND_SOC_DAIFMT_NB_IF:
  165. iface |= 0x02;
  166. iface &= ~0x20;
  167. break;
  168. case SND_SOC_DAIFMT_IB_IF:
  169. iface |= 0x22;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. wm8728_write(codec, WM8728_IFCTL, iface);
  175. return 0;
  176. }
  177. static int wm8728_set_bias_level(struct snd_soc_codec *codec,
  178. enum snd_soc_bias_level level)
  179. {
  180. u16 reg;
  181. int i;
  182. switch (level) {
  183. case SND_SOC_BIAS_ON:
  184. case SND_SOC_BIAS_PREPARE:
  185. case SND_SOC_BIAS_STANDBY:
  186. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  187. /* Power everything up... */
  188. reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  189. wm8728_write(codec, WM8728_DACCTL, reg & ~0x4);
  190. /* ..then sync in the register cache. */
  191. for (i = 0; i < ARRAY_SIZE(wm8728_reg_defaults); i++)
  192. wm8728_write(codec, i,
  193. wm8728_read_reg_cache(codec, i));
  194. }
  195. break;
  196. case SND_SOC_BIAS_OFF:
  197. reg = wm8728_read_reg_cache(codec, WM8728_DACCTL);
  198. wm8728_write(codec, WM8728_DACCTL, reg | 0x4);
  199. break;
  200. }
  201. codec->bias_level = level;
  202. return 0;
  203. }
  204. #define WM8728_RATES (SNDRV_PCM_RATE_8000_192000)
  205. #define WM8728_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  206. SNDRV_PCM_FMTBIT_S24_LE)
  207. struct snd_soc_dai wm8728_dai = {
  208. .name = "WM8728",
  209. .playback = {
  210. .stream_name = "Playback",
  211. .channels_min = 2,
  212. .channels_max = 2,
  213. .rates = WM8728_RATES,
  214. .formats = WM8728_FORMATS,
  215. },
  216. .ops = {
  217. .hw_params = wm8728_hw_params,
  218. .digital_mute = wm8728_mute,
  219. .set_fmt = wm8728_set_dai_fmt,
  220. }
  221. };
  222. EXPORT_SYMBOL_GPL(wm8728_dai);
  223. static int wm8728_suspend(struct platform_device *pdev, pm_message_t state)
  224. {
  225. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  226. struct snd_soc_codec *codec = socdev->codec;
  227. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  228. return 0;
  229. }
  230. static int wm8728_resume(struct platform_device *pdev)
  231. {
  232. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  233. struct snd_soc_codec *codec = socdev->codec;
  234. wm8728_set_bias_level(codec, codec->suspend_bias_level);
  235. return 0;
  236. }
  237. /*
  238. * initialise the WM8728 driver
  239. * register the mixer and dsp interfaces with the kernel
  240. */
  241. static int wm8728_init(struct snd_soc_device *socdev)
  242. {
  243. struct snd_soc_codec *codec = socdev->codec;
  244. int ret = 0;
  245. codec->name = "WM8728";
  246. codec->owner = THIS_MODULE;
  247. codec->read = wm8728_read_reg_cache;
  248. codec->write = wm8728_write;
  249. codec->set_bias_level = wm8728_set_bias_level;
  250. codec->dai = &wm8728_dai;
  251. codec->num_dai = 1;
  252. codec->bias_level = SND_SOC_BIAS_OFF;
  253. codec->reg_cache_size = ARRAY_SIZE(wm8728_reg_defaults);
  254. codec->reg_cache = kmemdup(wm8728_reg_defaults,
  255. sizeof(wm8728_reg_defaults),
  256. GFP_KERNEL);
  257. if (codec->reg_cache == NULL)
  258. return -ENOMEM;
  259. /* register pcms */
  260. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  261. if (ret < 0) {
  262. printk(KERN_ERR "wm8728: failed to create pcms\n");
  263. goto pcm_err;
  264. }
  265. /* power on device */
  266. wm8728_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  267. snd_soc_add_controls(codec, wm8728_snd_controls,
  268. ARRAY_SIZE(wm8728_snd_controls));
  269. wm8728_add_widgets(codec);
  270. ret = snd_soc_init_card(socdev);
  271. if (ret < 0) {
  272. printk(KERN_ERR "wm8728: failed to register card\n");
  273. goto card_err;
  274. }
  275. return ret;
  276. card_err:
  277. snd_soc_free_pcms(socdev);
  278. snd_soc_dapm_free(socdev);
  279. pcm_err:
  280. kfree(codec->reg_cache);
  281. return ret;
  282. }
  283. static struct snd_soc_device *wm8728_socdev;
  284. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  285. /*
  286. * WM8728 2 wire address is determined by GPIO5
  287. * state during powerup.
  288. * low = 0x1a
  289. * high = 0x1b
  290. */
  291. static int wm8728_i2c_probe(struct i2c_client *i2c,
  292. const struct i2c_device_id *id)
  293. {
  294. struct snd_soc_device *socdev = wm8728_socdev;
  295. struct snd_soc_codec *codec = socdev->codec;
  296. int ret;
  297. i2c_set_clientdata(i2c, codec);
  298. codec->control_data = i2c;
  299. ret = wm8728_init(socdev);
  300. if (ret < 0)
  301. pr_err("failed to initialise WM8728\n");
  302. return ret;
  303. }
  304. static int wm8728_i2c_remove(struct i2c_client *client)
  305. {
  306. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  307. kfree(codec->reg_cache);
  308. return 0;
  309. }
  310. static const struct i2c_device_id wm8728_i2c_id[] = {
  311. { "wm8728", 0 },
  312. { }
  313. };
  314. MODULE_DEVICE_TABLE(i2c, wm8728_i2c_id);
  315. static struct i2c_driver wm8728_i2c_driver = {
  316. .driver = {
  317. .name = "WM8728 I2C Codec",
  318. .owner = THIS_MODULE,
  319. },
  320. .probe = wm8728_i2c_probe,
  321. .remove = wm8728_i2c_remove,
  322. .id_table = wm8728_i2c_id,
  323. };
  324. static int wm8728_add_i2c_device(struct platform_device *pdev,
  325. const struct wm8728_setup_data *setup)
  326. {
  327. struct i2c_board_info info;
  328. struct i2c_adapter *adapter;
  329. struct i2c_client *client;
  330. int ret;
  331. ret = i2c_add_driver(&wm8728_i2c_driver);
  332. if (ret != 0) {
  333. dev_err(&pdev->dev, "can't add i2c driver\n");
  334. return ret;
  335. }
  336. memset(&info, 0, sizeof(struct i2c_board_info));
  337. info.addr = setup->i2c_address;
  338. strlcpy(info.type, "wm8728", I2C_NAME_SIZE);
  339. adapter = i2c_get_adapter(setup->i2c_bus);
  340. if (!adapter) {
  341. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  342. setup->i2c_bus);
  343. goto err_driver;
  344. }
  345. client = i2c_new_device(adapter, &info);
  346. i2c_put_adapter(adapter);
  347. if (!client) {
  348. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  349. (unsigned int)info.addr);
  350. goto err_driver;
  351. }
  352. return 0;
  353. err_driver:
  354. i2c_del_driver(&wm8728_i2c_driver);
  355. return -ENODEV;
  356. }
  357. #endif
  358. #if defined(CONFIG_SPI_MASTER)
  359. static int __devinit wm8728_spi_probe(struct spi_device *spi)
  360. {
  361. struct snd_soc_device *socdev = wm8728_socdev;
  362. struct snd_soc_codec *codec = socdev->codec;
  363. int ret;
  364. codec->control_data = spi;
  365. ret = wm8728_init(socdev);
  366. if (ret < 0)
  367. dev_err(&spi->dev, "failed to initialise WM8728\n");
  368. return ret;
  369. }
  370. static int __devexit wm8728_spi_remove(struct spi_device *spi)
  371. {
  372. return 0;
  373. }
  374. static struct spi_driver wm8728_spi_driver = {
  375. .driver = {
  376. .name = "wm8728",
  377. .bus = &spi_bus_type,
  378. .owner = THIS_MODULE,
  379. },
  380. .probe = wm8728_spi_probe,
  381. .remove = __devexit_p(wm8728_spi_remove),
  382. };
  383. static int wm8728_spi_write(struct spi_device *spi, const char *data, int len)
  384. {
  385. struct spi_transfer t;
  386. struct spi_message m;
  387. u8 msg[2];
  388. if (len <= 0)
  389. return 0;
  390. msg[0] = data[0];
  391. msg[1] = data[1];
  392. spi_message_init(&m);
  393. memset(&t, 0, (sizeof t));
  394. t.tx_buf = &msg[0];
  395. t.len = len;
  396. spi_message_add_tail(&t, &m);
  397. spi_sync(spi, &m);
  398. return len;
  399. }
  400. #endif /* CONFIG_SPI_MASTER */
  401. static int wm8728_probe(struct platform_device *pdev)
  402. {
  403. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  404. struct wm8728_setup_data *setup;
  405. struct snd_soc_codec *codec;
  406. int ret = 0;
  407. setup = socdev->codec_data;
  408. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  409. if (codec == NULL)
  410. return -ENOMEM;
  411. socdev->codec = codec;
  412. mutex_init(&codec->mutex);
  413. INIT_LIST_HEAD(&codec->dapm_widgets);
  414. INIT_LIST_HEAD(&codec->dapm_paths);
  415. wm8728_socdev = socdev;
  416. ret = -ENODEV;
  417. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  418. if (setup->i2c_address) {
  419. codec->hw_write = (hw_write_t)i2c_master_send;
  420. ret = wm8728_add_i2c_device(pdev, setup);
  421. }
  422. #endif
  423. #if defined(CONFIG_SPI_MASTER)
  424. if (setup->spi) {
  425. codec->hw_write = (hw_write_t)wm8728_spi_write;
  426. ret = spi_register_driver(&wm8728_spi_driver);
  427. if (ret != 0)
  428. printk(KERN_ERR "can't add spi driver");
  429. }
  430. #endif
  431. if (ret != 0)
  432. kfree(codec);
  433. return ret;
  434. }
  435. /* power down chip */
  436. static int wm8728_remove(struct platform_device *pdev)
  437. {
  438. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  439. struct snd_soc_codec *codec = socdev->codec;
  440. if (codec->control_data)
  441. wm8728_set_bias_level(codec, SND_SOC_BIAS_OFF);
  442. snd_soc_free_pcms(socdev);
  443. snd_soc_dapm_free(socdev);
  444. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  445. i2c_unregister_device(codec->control_data);
  446. i2c_del_driver(&wm8728_i2c_driver);
  447. #endif
  448. #if defined(CONFIG_SPI_MASTER)
  449. spi_unregister_driver(&wm8728_spi_driver);
  450. #endif
  451. kfree(codec);
  452. return 0;
  453. }
  454. struct snd_soc_codec_device soc_codec_dev_wm8728 = {
  455. .probe = wm8728_probe,
  456. .remove = wm8728_remove,
  457. .suspend = wm8728_suspend,
  458. .resume = wm8728_resume,
  459. };
  460. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8728);
  461. static int __init wm8728_modinit(void)
  462. {
  463. return snd_soc_register_dai(&wm8728_dai);
  464. }
  465. module_init(wm8728_modinit);
  466. static void __exit wm8728_exit(void)
  467. {
  468. snd_soc_unregister_dai(&wm8728_dai);
  469. }
  470. module_exit(wm8728_exit);
  471. MODULE_DESCRIPTION("ASoC WM8728 driver");
  472. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  473. MODULE_LICENSE("GPL");