wm8731.c 18 KB

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