wm8731.c 18 KB

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