wm8731.c 20 KB

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