wm8731.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * wm8731.c -- WM8731 ALSA SoC Audio driver
  3. *
  4. * Copyright 2005 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <richard@openedhand.com>
  7. *
  8. * Based on wm8753.c by Liam Girdwood
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spi/spi.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/initval.h>
  28. #include "wm8731.h"
  29. static struct snd_soc_codec *wm8731_codec;
  30. struct snd_soc_codec_device soc_codec_dev_wm8731;
  31. /* codec private data */
  32. struct wm8731_priv {
  33. struct snd_soc_codec codec;
  34. u16 reg_cache[WM8731_CACHEREGNUM];
  35. unsigned int sysclk;
  36. };
  37. #ifdef CONFIG_SPI_MASTER
  38. static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
  39. #endif
  40. /*
  41. * wm8731 register cache
  42. * We can't read the WM8731 register space when we are
  43. * using 2 wire for device control, so we cache them instead.
  44. * There is no point in caching the reset register
  45. */
  46. static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
  47. 0x0097, 0x0097, 0x0079, 0x0079,
  48. 0x000a, 0x0008, 0x009f, 0x000a,
  49. 0x0000, 0x0000
  50. };
  51. #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
  52. static const char *wm8731_input_select[] = {"Line In", "Mic"};
  53. static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  54. static const struct soc_enum wm8731_enum[] = {
  55. SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
  56. SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
  57. };
  58. static const struct snd_kcontrol_new wm8731_snd_controls[] = {
  59. SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
  60. 0, 127, 0),
  61. SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
  62. 7, 1, 0),
  63. SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
  64. SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
  65. SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
  66. SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
  67. SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
  68. SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
  69. SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
  70. SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
  71. };
  72. /* Output Mixer */
  73. static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
  74. SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
  75. SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
  76. SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
  77. };
  78. /* Input mux */
  79. static const struct snd_kcontrol_new wm8731_input_mux_controls =
  80. SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
  81. static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
  82. SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
  83. &wm8731_output_mixer_controls[0],
  84. ARRAY_SIZE(wm8731_output_mixer_controls)),
  85. SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
  86. SND_SOC_DAPM_OUTPUT("LOUT"),
  87. SND_SOC_DAPM_OUTPUT("LHPOUT"),
  88. SND_SOC_DAPM_OUTPUT("ROUT"),
  89. SND_SOC_DAPM_OUTPUT("RHPOUT"),
  90. SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
  91. SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
  92. SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
  93. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
  94. SND_SOC_DAPM_INPUT("MICIN"),
  95. SND_SOC_DAPM_INPUT("RLINEIN"),
  96. SND_SOC_DAPM_INPUT("LLINEIN"),
  97. };
  98. static const struct snd_soc_dapm_route intercon[] = {
  99. /* output mixer */
  100. {"Output Mixer", "Line Bypass Switch", "Line Input"},
  101. {"Output Mixer", "HiFi Playback Switch", "DAC"},
  102. {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
  103. /* outputs */
  104. {"RHPOUT", NULL, "Output Mixer"},
  105. {"ROUT", NULL, "Output Mixer"},
  106. {"LHPOUT", NULL, "Output Mixer"},
  107. {"LOUT", NULL, "Output Mixer"},
  108. /* input mux */
  109. {"Input Mux", "Line In", "Line Input"},
  110. {"Input Mux", "Mic", "Mic Bias"},
  111. {"ADC", NULL, "Input Mux"},
  112. /* inputs */
  113. {"Line Input", NULL, "LLINEIN"},
  114. {"Line Input", NULL, "RLINEIN"},
  115. {"Mic Bias", NULL, "MICIN"},
  116. };
  117. static int wm8731_add_widgets(struct snd_soc_codec *codec)
  118. {
  119. snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
  120. ARRAY_SIZE(wm8731_dapm_widgets));
  121. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  122. snd_soc_dapm_new_widgets(codec);
  123. return 0;
  124. }
  125. struct _coeff_div {
  126. u32 mclk;
  127. u32 rate;
  128. u16 fs;
  129. u8 sr:4;
  130. u8 bosr:1;
  131. u8 usb:1;
  132. };
  133. /* codec mclk clock divider coefficients */
  134. static const struct _coeff_div coeff_div[] = {
  135. /* 48k */
  136. {12288000, 48000, 256, 0x0, 0x0, 0x0},
  137. {18432000, 48000, 384, 0x0, 0x1, 0x0},
  138. {12000000, 48000, 250, 0x0, 0x0, 0x1},
  139. /* 32k */
  140. {12288000, 32000, 384, 0x6, 0x0, 0x0},
  141. {18432000, 32000, 576, 0x6, 0x1, 0x0},
  142. {12000000, 32000, 375, 0x6, 0x0, 0x1},
  143. /* 8k */
  144. {12288000, 8000, 1536, 0x3, 0x0, 0x0},
  145. {18432000, 8000, 2304, 0x3, 0x1, 0x0},
  146. {11289600, 8000, 1408, 0xb, 0x0, 0x0},
  147. {16934400, 8000, 2112, 0xb, 0x1, 0x0},
  148. {12000000, 8000, 1500, 0x3, 0x0, 0x1},
  149. /* 96k */
  150. {12288000, 96000, 128, 0x7, 0x0, 0x0},
  151. {18432000, 96000, 192, 0x7, 0x1, 0x0},
  152. {12000000, 96000, 125, 0x7, 0x0, 0x1},
  153. /* 44.1k */
  154. {11289600, 44100, 256, 0x8, 0x0, 0x0},
  155. {16934400, 44100, 384, 0x8, 0x1, 0x0},
  156. {12000000, 44100, 272, 0x8, 0x1, 0x1},
  157. /* 88.2k */
  158. {11289600, 88200, 128, 0xf, 0x0, 0x0},
  159. {16934400, 88200, 192, 0xf, 0x1, 0x0},
  160. {12000000, 88200, 136, 0xf, 0x1, 0x1},
  161. };
  162. static inline int get_coeff(int mclk, int rate)
  163. {
  164. int i;
  165. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  166. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  167. return i;
  168. }
  169. return 0;
  170. }
  171. static int wm8731_hw_params(struct snd_pcm_substream *substream,
  172. struct snd_pcm_hw_params *params,
  173. struct snd_soc_dai *dai)
  174. {
  175. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  176. struct snd_soc_device *socdev = rtd->socdev;
  177. struct snd_soc_codec *codec = socdev->card->codec;
  178. struct wm8731_priv *wm8731 = codec->private_data;
  179. u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
  180. int i = get_coeff(wm8731->sysclk, params_rate(params));
  181. u16 srate = (coeff_div[i].sr << 2) |
  182. (coeff_div[i].bosr << 1) | coeff_div[i].usb;
  183. snd_soc_write(codec, WM8731_SRATE, srate);
  184. /* bit size */
  185. switch (params_format(params)) {
  186. case SNDRV_PCM_FORMAT_S16_LE:
  187. break;
  188. case SNDRV_PCM_FORMAT_S20_3LE:
  189. iface |= 0x0004;
  190. break;
  191. case SNDRV_PCM_FORMAT_S24_LE:
  192. iface |= 0x0008;
  193. break;
  194. }
  195. snd_soc_write(codec, WM8731_IFACE, iface);
  196. return 0;
  197. }
  198. static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
  199. struct snd_soc_dai *dai)
  200. {
  201. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  202. struct snd_soc_device *socdev = rtd->socdev;
  203. struct snd_soc_codec *codec = socdev->card->codec;
  204. /* set active */
  205. snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
  206. return 0;
  207. }
  208. static void wm8731_shutdown(struct snd_pcm_substream *substream,
  209. struct snd_soc_dai *dai)
  210. {
  211. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  212. struct snd_soc_device *socdev = rtd->socdev;
  213. struct snd_soc_codec *codec = socdev->card->codec;
  214. /* deactivate */
  215. if (!codec->active) {
  216. udelay(50);
  217. snd_soc_write(codec, WM8731_ACTIVE, 0x0);
  218. }
  219. }
  220. static int wm8731_mute(struct snd_soc_dai *dai, int mute)
  221. {
  222. struct snd_soc_codec *codec = dai->codec;
  223. u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
  224. if (mute)
  225. snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
  226. else
  227. snd_soc_write(codec, WM8731_APDIGI, mute_reg);
  228. return 0;
  229. }
  230. static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  231. int clk_id, unsigned int freq, int dir)
  232. {
  233. struct snd_soc_codec *codec = codec_dai->codec;
  234. struct wm8731_priv *wm8731 = codec->private_data;
  235. switch (freq) {
  236. case 11289600:
  237. case 12000000:
  238. case 12288000:
  239. case 16934400:
  240. case 18432000:
  241. wm8731->sysclk = freq;
  242. return 0;
  243. }
  244. return -EINVAL;
  245. }
  246. static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
  247. unsigned int fmt)
  248. {
  249. struct snd_soc_codec *codec = codec_dai->codec;
  250. u16 iface = 0;
  251. /* set master/slave audio interface */
  252. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  253. case SND_SOC_DAIFMT_CBM_CFM:
  254. iface |= 0x0040;
  255. break;
  256. case SND_SOC_DAIFMT_CBS_CFS:
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. /* interface format */
  262. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  263. case SND_SOC_DAIFMT_I2S:
  264. iface |= 0x0002;
  265. break;
  266. case SND_SOC_DAIFMT_RIGHT_J:
  267. break;
  268. case SND_SOC_DAIFMT_LEFT_J:
  269. iface |= 0x0001;
  270. break;
  271. case SND_SOC_DAIFMT_DSP_A:
  272. iface |= 0x0003;
  273. break;
  274. case SND_SOC_DAIFMT_DSP_B:
  275. iface |= 0x0013;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. /* clock inversion */
  281. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  282. case SND_SOC_DAIFMT_NB_NF:
  283. break;
  284. case SND_SOC_DAIFMT_IB_IF:
  285. iface |= 0x0090;
  286. break;
  287. case SND_SOC_DAIFMT_IB_NF:
  288. iface |= 0x0080;
  289. break;
  290. case SND_SOC_DAIFMT_NB_IF:
  291. iface |= 0x0010;
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. /* set iface */
  297. snd_soc_write(codec, WM8731_IFACE, iface);
  298. return 0;
  299. }
  300. static int wm8731_set_bias_level(struct snd_soc_codec *codec,
  301. enum snd_soc_bias_level level)
  302. {
  303. u16 reg;
  304. switch (level) {
  305. case SND_SOC_BIAS_ON:
  306. break;
  307. case SND_SOC_BIAS_PREPARE:
  308. break;
  309. case SND_SOC_BIAS_STANDBY:
  310. /* Clear PWROFF, gate CLKOUT, everything else as-is */
  311. reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
  312. snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
  313. break;
  314. case SND_SOC_BIAS_OFF:
  315. snd_soc_write(codec, WM8731_ACTIVE, 0x0);
  316. snd_soc_write(codec, WM8731_PWR, 0xffff);
  317. break;
  318. }
  319. codec->bias_level = level;
  320. return 0;
  321. }
  322. #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  323. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  324. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  325. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
  326. SNDRV_PCM_RATE_96000)
  327. #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  328. SNDRV_PCM_FMTBIT_S24_LE)
  329. static struct snd_soc_dai_ops wm8731_dai_ops = {
  330. .prepare = wm8731_pcm_prepare,
  331. .hw_params = wm8731_hw_params,
  332. .shutdown = wm8731_shutdown,
  333. .digital_mute = wm8731_mute,
  334. .set_sysclk = wm8731_set_dai_sysclk,
  335. .set_fmt = wm8731_set_dai_fmt,
  336. };
  337. struct snd_soc_dai wm8731_dai = {
  338. .name = "WM8731",
  339. .playback = {
  340. .stream_name = "Playback",
  341. .channels_min = 1,
  342. .channels_max = 2,
  343. .rates = WM8731_RATES,
  344. .formats = WM8731_FORMATS,},
  345. .capture = {
  346. .stream_name = "Capture",
  347. .channels_min = 1,
  348. .channels_max = 2,
  349. .rates = WM8731_RATES,
  350. .formats = WM8731_FORMATS,},
  351. .ops = &wm8731_dai_ops,
  352. };
  353. EXPORT_SYMBOL_GPL(wm8731_dai);
  354. #ifdef CONFIG_PM
  355. static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
  356. {
  357. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  358. struct snd_soc_codec *codec = socdev->card->codec;
  359. snd_soc_write(codec, WM8731_ACTIVE, 0x0);
  360. wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
  361. return 0;
  362. }
  363. static int wm8731_resume(struct platform_device *pdev)
  364. {
  365. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  366. struct snd_soc_codec *codec = socdev->card->codec;
  367. int i;
  368. u8 data[2];
  369. u16 *cache = codec->reg_cache;
  370. /* Sync reg_cache with the hardware */
  371. for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
  372. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  373. data[1] = cache[i] & 0x00ff;
  374. codec->hw_write(codec->control_data, data, 2);
  375. }
  376. wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  377. wm8731_set_bias_level(codec, codec->suspend_bias_level);
  378. return 0;
  379. }
  380. #else
  381. #define wm8731_suspend NULL
  382. #define wm8731_resume NULL
  383. #endif
  384. static int wm8731_probe(struct platform_device *pdev)
  385. {
  386. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  387. struct snd_soc_codec *codec;
  388. int ret = 0;
  389. if (wm8731_codec == NULL) {
  390. dev_err(&pdev->dev, "Codec device not registered\n");
  391. return -ENODEV;
  392. }
  393. socdev->card->codec = wm8731_codec;
  394. codec = wm8731_codec;
  395. /* register pcms */
  396. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  397. if (ret < 0) {
  398. dev_err(codec->dev, "failed to create pcms: %d\n", ret);
  399. goto pcm_err;
  400. }
  401. snd_soc_add_controls(codec, wm8731_snd_controls,
  402. ARRAY_SIZE(wm8731_snd_controls));
  403. wm8731_add_widgets(codec);
  404. ret = snd_soc_init_card(socdev);
  405. if (ret < 0) {
  406. dev_err(codec->dev, "failed to register card: %d\n", ret);
  407. goto card_err;
  408. }
  409. return ret;
  410. card_err:
  411. snd_soc_free_pcms(socdev);
  412. snd_soc_dapm_free(socdev);
  413. pcm_err:
  414. return ret;
  415. }
  416. /* power down chip */
  417. static int wm8731_remove(struct platform_device *pdev)
  418. {
  419. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  420. snd_soc_free_pcms(socdev);
  421. snd_soc_dapm_free(socdev);
  422. return 0;
  423. }
  424. struct snd_soc_codec_device soc_codec_dev_wm8731 = {
  425. .probe = wm8731_probe,
  426. .remove = wm8731_remove,
  427. .suspend = wm8731_suspend,
  428. .resume = wm8731_resume,
  429. };
  430. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
  431. static int wm8731_register(struct wm8731_priv *wm8731)
  432. {
  433. int ret;
  434. struct snd_soc_codec *codec = &wm8731->codec;
  435. if (wm8731_codec) {
  436. dev_err(codec->dev, "Another WM8731 is registered\n");
  437. ret = -EINVAL;
  438. goto err;
  439. }
  440. mutex_init(&codec->mutex);
  441. INIT_LIST_HEAD(&codec->dapm_widgets);
  442. INIT_LIST_HEAD(&codec->dapm_paths);
  443. codec->private_data = wm8731;
  444. codec->name = "WM8731";
  445. codec->owner = THIS_MODULE;
  446. codec->bias_level = SND_SOC_BIAS_OFF;
  447. codec->set_bias_level = wm8731_set_bias_level;
  448. codec->dai = &wm8731_dai;
  449. codec->num_dai = 1;
  450. codec->reg_cache_size = WM8731_CACHEREGNUM;
  451. codec->reg_cache = &wm8731->reg_cache;
  452. memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg));
  453. ret = snd_soc_codec_set_cache_io(codec, 7, 9);
  454. if (ret < 0) {
  455. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  456. goto err;
  457. }
  458. ret = wm8731_reset(codec);
  459. if (ret < 0) {
  460. dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
  461. goto err;
  462. }
  463. wm8731_dai.dev = codec->dev;
  464. wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  465. /* Latch the update bits */
  466. snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
  467. snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
  468. snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
  469. snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
  470. /* Disable bypass path by default */
  471. snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
  472. wm8731_codec = codec;
  473. ret = snd_soc_register_codec(codec);
  474. if (ret != 0) {
  475. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  476. goto err;
  477. }
  478. ret = snd_soc_register_dai(&wm8731_dai);
  479. if (ret != 0) {
  480. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  481. snd_soc_unregister_codec(codec);
  482. goto err_codec;
  483. }
  484. return 0;
  485. err_codec:
  486. snd_soc_unregister_codec(codec);
  487. err:
  488. kfree(wm8731);
  489. return ret;
  490. }
  491. static void wm8731_unregister(struct wm8731_priv *wm8731)
  492. {
  493. wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF);
  494. snd_soc_unregister_dai(&wm8731_dai);
  495. snd_soc_unregister_codec(&wm8731->codec);
  496. kfree(wm8731);
  497. wm8731_codec = NULL;
  498. }
  499. #if defined(CONFIG_SPI_MASTER)
  500. static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
  501. {
  502. struct spi_transfer t;
  503. struct spi_message m;
  504. u8 msg[2];
  505. if (len <= 0)
  506. return 0;
  507. msg[0] = data[0];
  508. msg[1] = data[1];
  509. spi_message_init(&m);
  510. memset(&t, 0, (sizeof t));
  511. t.tx_buf = &msg[0];
  512. t.len = len;
  513. spi_message_add_tail(&t, &m);
  514. spi_sync(spi, &m);
  515. return len;
  516. }
  517. static int __devinit wm8731_spi_probe(struct spi_device *spi)
  518. {
  519. struct snd_soc_codec *codec;
  520. struct wm8731_priv *wm8731;
  521. wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
  522. if (wm8731 == NULL)
  523. return -ENOMEM;
  524. codec = &wm8731->codec;
  525. codec->control_data = spi;
  526. codec->hw_write = (hw_write_t)wm8731_spi_write;
  527. codec->dev = &spi->dev;
  528. dev_set_drvdata(&spi->dev, wm8731);
  529. return wm8731_register(wm8731);
  530. }
  531. static int __devexit wm8731_spi_remove(struct spi_device *spi)
  532. {
  533. struct wm8731_priv *wm8731 = dev_get_drvdata(&spi->dev);
  534. wm8731_unregister(wm8731);
  535. return 0;
  536. }
  537. #ifdef CONFIG_PM
  538. static int wm8731_spi_suspend(struct spi_device *spi, pm_message_t msg)
  539. {
  540. return snd_soc_suspend_device(&spi->dev);
  541. }
  542. static int wm8731_spi_resume(struct spi_device *spi)
  543. {
  544. return snd_soc_resume_device(&spi->dev);
  545. }
  546. #else
  547. #define wm8731_spi_suspend NULL
  548. #define wm8731_spi_resume NULL
  549. #endif
  550. static struct spi_driver wm8731_spi_driver = {
  551. .driver = {
  552. .name = "wm8731",
  553. .bus = &spi_bus_type,
  554. .owner = THIS_MODULE,
  555. },
  556. .probe = wm8731_spi_probe,
  557. .suspend = wm8731_spi_suspend,
  558. .resume = wm8731_spi_resume,
  559. .remove = __devexit_p(wm8731_spi_remove),
  560. };
  561. #endif /* CONFIG_SPI_MASTER */
  562. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  563. static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
  564. const struct i2c_device_id *id)
  565. {
  566. struct wm8731_priv *wm8731;
  567. struct snd_soc_codec *codec;
  568. wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
  569. if (wm8731 == NULL)
  570. return -ENOMEM;
  571. codec = &wm8731->codec;
  572. codec->hw_write = (hw_write_t)i2c_master_send;
  573. i2c_set_clientdata(i2c, wm8731);
  574. codec->control_data = i2c;
  575. codec->dev = &i2c->dev;
  576. return wm8731_register(wm8731);
  577. }
  578. static __devexit int wm8731_i2c_remove(struct i2c_client *client)
  579. {
  580. struct wm8731_priv *wm8731 = i2c_get_clientdata(client);
  581. wm8731_unregister(wm8731);
  582. return 0;
  583. }
  584. #ifdef CONFIG_PM
  585. static int wm8731_i2c_suspend(struct i2c_client *i2c, pm_message_t msg)
  586. {
  587. return snd_soc_suspend_device(&i2c->dev);
  588. }
  589. static int wm8731_i2c_resume(struct i2c_client *i2c)
  590. {
  591. return snd_soc_resume_device(&i2c->dev);
  592. }
  593. #else
  594. #define wm8731_i2c_suspend NULL
  595. #define wm8731_i2c_resume NULL
  596. #endif
  597. static const struct i2c_device_id wm8731_i2c_id[] = {
  598. { "wm8731", 0 },
  599. { }
  600. };
  601. MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
  602. static struct i2c_driver wm8731_i2c_driver = {
  603. .driver = {
  604. .name = "WM8731 I2C Codec",
  605. .owner = THIS_MODULE,
  606. },
  607. .probe = wm8731_i2c_probe,
  608. .remove = __devexit_p(wm8731_i2c_remove),
  609. .suspend = wm8731_i2c_suspend,
  610. .resume = wm8731_i2c_resume,
  611. .id_table = wm8731_i2c_id,
  612. };
  613. #endif
  614. static int __init wm8731_modinit(void)
  615. {
  616. int ret;
  617. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  618. ret = i2c_add_driver(&wm8731_i2c_driver);
  619. if (ret != 0) {
  620. printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
  621. ret);
  622. }
  623. #endif
  624. #if defined(CONFIG_SPI_MASTER)
  625. ret = spi_register_driver(&wm8731_spi_driver);
  626. if (ret != 0) {
  627. printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
  628. ret);
  629. }
  630. #endif
  631. return 0;
  632. }
  633. module_init(wm8731_modinit);
  634. static void __exit wm8731_exit(void)
  635. {
  636. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  637. i2c_del_driver(&wm8731_i2c_driver);
  638. #endif
  639. #if defined(CONFIG_SPI_MASTER)
  640. spi_unregister_driver(&wm8731_spi_driver);
  641. #endif
  642. }
  643. module_exit(wm8731_exit);
  644. MODULE_DESCRIPTION("ASoC WM8731 driver");
  645. MODULE_AUTHOR("Richard Purdie");
  646. MODULE_LICENSE("GPL");