wm8731.c 19 KB

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