wm8737.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * wm8737.c -- WM8737 ALSA SoC Audio driver
  3. *
  4. * Copyright 2010 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/i2c.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/slab.h>
  22. #include <linux/of_device.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/soc-dapm.h>
  28. #include <sound/initval.h>
  29. #include <sound/tlv.h>
  30. #include "wm8737.h"
  31. #define WM8737_NUM_SUPPLIES 4
  32. static const char *wm8737_supply_names[WM8737_NUM_SUPPLIES] = {
  33. "DCVDD",
  34. "DBVDD",
  35. "AVDD",
  36. "MVDD",
  37. };
  38. /* codec private data */
  39. struct wm8737_priv {
  40. struct regmap *regmap;
  41. struct regulator_bulk_data supplies[WM8737_NUM_SUPPLIES];
  42. unsigned int mclk;
  43. };
  44. static const struct reg_default wm8737_reg_defaults[] = {
  45. { 0, 0x00C3 }, /* R0 - Left PGA volume */
  46. { 1, 0x00C3 }, /* R1 - Right PGA volume */
  47. { 2, 0x0007 }, /* R2 - AUDIO path L */
  48. { 3, 0x0007 }, /* R3 - AUDIO path R */
  49. { 4, 0x0000 }, /* R4 - 3D Enhance */
  50. { 5, 0x0000 }, /* R5 - ADC Control */
  51. { 6, 0x0000 }, /* R6 - Power Management */
  52. { 7, 0x000A }, /* R7 - Audio Format */
  53. { 8, 0x0000 }, /* R8 - Clocking */
  54. { 9, 0x000F }, /* R9 - MIC Preamp Control */
  55. { 10, 0x0003 }, /* R10 - Misc Bias Control */
  56. { 11, 0x0000 }, /* R11 - Noise Gate */
  57. { 12, 0x007C }, /* R12 - ALC1 */
  58. { 13, 0x0000 }, /* R13 - ALC2 */
  59. { 14, 0x0032 }, /* R14 - ALC3 */
  60. };
  61. static bool wm8737_volatile(struct device *dev, unsigned int reg)
  62. {
  63. switch (reg) {
  64. case WM8737_RESET:
  65. return true;
  66. default:
  67. return false;
  68. }
  69. }
  70. static int wm8737_reset(struct snd_soc_codec *codec)
  71. {
  72. return snd_soc_write(codec, WM8737_RESET, 0);
  73. }
  74. static const DECLARE_TLV_DB_RANGE(micboost_tlv,
  75. 0, 0, TLV_DB_SCALE_ITEM(1300, 0, 0),
  76. 1, 1, TLV_DB_SCALE_ITEM(1800, 0, 0),
  77. 2, 2, TLV_DB_SCALE_ITEM(2800, 0, 0),
  78. 3, 3, TLV_DB_SCALE_ITEM(3300, 0, 0)
  79. );
  80. static const DECLARE_TLV_DB_SCALE(pga_tlv, -9750, 50, 1);
  81. static const DECLARE_TLV_DB_SCALE(adc_tlv, -600, 600, 0);
  82. static const DECLARE_TLV_DB_SCALE(ng_tlv, -7800, 600, 0);
  83. static const DECLARE_TLV_DB_SCALE(alc_max_tlv, -1200, 600, 0);
  84. static const DECLARE_TLV_DB_SCALE(alc_target_tlv, -1800, 100, 0);
  85. static const char *micbias_enum_text[] = {
  86. "25%",
  87. "50%",
  88. "75%",
  89. "100%",
  90. };
  91. static SOC_ENUM_SINGLE_DECL(micbias_enum,
  92. WM8737_MIC_PREAMP_CONTROL, 0, micbias_enum_text);
  93. static const char *low_cutoff_text[] = {
  94. "Low", "High"
  95. };
  96. static SOC_ENUM_SINGLE_DECL(low_3d,
  97. WM8737_3D_ENHANCE, 6, low_cutoff_text);
  98. static const char *high_cutoff_text[] = {
  99. "High", "Low"
  100. };
  101. static SOC_ENUM_SINGLE_DECL(high_3d,
  102. WM8737_3D_ENHANCE, 5, high_cutoff_text);
  103. static const char *alc_fn_text[] = {
  104. "Disabled", "Right", "Left", "Stereo"
  105. };
  106. static SOC_ENUM_SINGLE_DECL(alc_fn,
  107. WM8737_ALC1, 7, alc_fn_text);
  108. static const char *alc_hold_text[] = {
  109. "0", "2.67ms", "5.33ms", "10.66ms", "21.32ms", "42.64ms", "85.28ms",
  110. "170.56ms", "341.12ms", "682.24ms", "1.364s", "2.728s", "5.458s",
  111. "10.916s", "21.832s", "43.691s"
  112. };
  113. static SOC_ENUM_SINGLE_DECL(alc_hold,
  114. WM8737_ALC2, 0, alc_hold_text);
  115. static const char *alc_atk_text[] = {
  116. "8.4ms", "16.8ms", "33.6ms", "67.2ms", "134.4ms", "268.8ms", "537.6ms",
  117. "1.075s", "2.15s", "4.3s", "8.6s"
  118. };
  119. static SOC_ENUM_SINGLE_DECL(alc_atk,
  120. WM8737_ALC3, 0, alc_atk_text);
  121. static const char *alc_dcy_text[] = {
  122. "33.6ms", "67.2ms", "134.4ms", "268.8ms", "537.6ms", "1.075s", "2.15s",
  123. "4.3s", "8.6s", "17.2s", "34.41s"
  124. };
  125. static SOC_ENUM_SINGLE_DECL(alc_dcy,
  126. WM8737_ALC3, 4, alc_dcy_text);
  127. static const struct snd_kcontrol_new wm8737_snd_controls[] = {
  128. SOC_DOUBLE_R_TLV("Mic Boost Volume", WM8737_AUDIO_PATH_L, WM8737_AUDIO_PATH_R,
  129. 6, 3, 0, micboost_tlv),
  130. SOC_DOUBLE_R("Mic Boost Switch", WM8737_AUDIO_PATH_L, WM8737_AUDIO_PATH_R,
  131. 4, 1, 0),
  132. SOC_DOUBLE("Mic ZC Switch", WM8737_AUDIO_PATH_L, WM8737_AUDIO_PATH_R,
  133. 3, 1, 0),
  134. SOC_DOUBLE_R_TLV("Capture Volume", WM8737_LEFT_PGA_VOLUME,
  135. WM8737_RIGHT_PGA_VOLUME, 0, 255, 0, pga_tlv),
  136. SOC_DOUBLE("Capture ZC Switch", WM8737_AUDIO_PATH_L, WM8737_AUDIO_PATH_R,
  137. 2, 1, 0),
  138. SOC_DOUBLE("INPUT1 DC Bias Switch", WM8737_MISC_BIAS_CONTROL, 0, 1, 1, 0),
  139. SOC_ENUM("Mic PGA Bias", micbias_enum),
  140. SOC_SINGLE("ADC Low Power Switch", WM8737_ADC_CONTROL, 2, 1, 0),
  141. SOC_SINGLE("High Pass Filter Switch", WM8737_ADC_CONTROL, 0, 1, 1),
  142. SOC_DOUBLE("Polarity Invert Switch", WM8737_ADC_CONTROL, 5, 6, 1, 0),
  143. SOC_SINGLE("3D Switch", WM8737_3D_ENHANCE, 0, 1, 0),
  144. SOC_SINGLE("3D Depth", WM8737_3D_ENHANCE, 1, 15, 0),
  145. SOC_ENUM("3D Low Cut-off", low_3d),
  146. SOC_ENUM("3D High Cut-off", low_3d),
  147. SOC_SINGLE_TLV("3D ADC Volume", WM8737_3D_ENHANCE, 7, 1, 1, adc_tlv),
  148. SOC_SINGLE("Noise Gate Switch", WM8737_NOISE_GATE, 0, 1, 0),
  149. SOC_SINGLE_TLV("Noise Gate Threshold Volume", WM8737_NOISE_GATE, 2, 7, 0,
  150. ng_tlv),
  151. SOC_ENUM("ALC", alc_fn),
  152. SOC_SINGLE_TLV("ALC Max Gain Volume", WM8737_ALC1, 4, 7, 0, alc_max_tlv),
  153. SOC_SINGLE_TLV("ALC Target Volume", WM8737_ALC1, 0, 15, 0, alc_target_tlv),
  154. SOC_ENUM("ALC Hold Time", alc_hold),
  155. SOC_SINGLE("ALC ZC Switch", WM8737_ALC2, 4, 1, 0),
  156. SOC_ENUM("ALC Attack Time", alc_atk),
  157. SOC_ENUM("ALC Decay Time", alc_dcy),
  158. };
  159. static const char *linsel_text[] = {
  160. "LINPUT1", "LINPUT2", "LINPUT3", "LINPUT1 DC",
  161. };
  162. static SOC_ENUM_SINGLE_DECL(linsel_enum,
  163. WM8737_AUDIO_PATH_L, 7, linsel_text);
  164. static const struct snd_kcontrol_new linsel_mux =
  165. SOC_DAPM_ENUM("LINSEL", linsel_enum);
  166. static const char *rinsel_text[] = {
  167. "RINPUT1", "RINPUT2", "RINPUT3", "RINPUT1 DC",
  168. };
  169. static SOC_ENUM_SINGLE_DECL(rinsel_enum,
  170. WM8737_AUDIO_PATH_R, 7, rinsel_text);
  171. static const struct snd_kcontrol_new rinsel_mux =
  172. SOC_DAPM_ENUM("RINSEL", rinsel_enum);
  173. static const char *bypass_text[] = {
  174. "Direct", "Preamp"
  175. };
  176. static SOC_ENUM_SINGLE_DECL(lbypass_enum,
  177. WM8737_MIC_PREAMP_CONTROL, 2, bypass_text);
  178. static const struct snd_kcontrol_new lbypass_mux =
  179. SOC_DAPM_ENUM("Left Bypass", lbypass_enum);
  180. static SOC_ENUM_SINGLE_DECL(rbypass_enum,
  181. WM8737_MIC_PREAMP_CONTROL, 3, bypass_text);
  182. static const struct snd_kcontrol_new rbypass_mux =
  183. SOC_DAPM_ENUM("Left Bypass", rbypass_enum);
  184. static const struct snd_soc_dapm_widget wm8737_dapm_widgets[] = {
  185. SND_SOC_DAPM_INPUT("LINPUT1"),
  186. SND_SOC_DAPM_INPUT("LINPUT2"),
  187. SND_SOC_DAPM_INPUT("LINPUT3"),
  188. SND_SOC_DAPM_INPUT("RINPUT1"),
  189. SND_SOC_DAPM_INPUT("RINPUT2"),
  190. SND_SOC_DAPM_INPUT("RINPUT3"),
  191. SND_SOC_DAPM_INPUT("LACIN"),
  192. SND_SOC_DAPM_INPUT("RACIN"),
  193. SND_SOC_DAPM_MUX("LINSEL", SND_SOC_NOPM, 0, 0, &linsel_mux),
  194. SND_SOC_DAPM_MUX("RINSEL", SND_SOC_NOPM, 0, 0, &rinsel_mux),
  195. SND_SOC_DAPM_MUX("Left Preamp Mux", SND_SOC_NOPM, 0, 0, &lbypass_mux),
  196. SND_SOC_DAPM_MUX("Right Preamp Mux", SND_SOC_NOPM, 0, 0, &rbypass_mux),
  197. SND_SOC_DAPM_PGA("PGAL", WM8737_POWER_MANAGEMENT, 5, 0, NULL, 0),
  198. SND_SOC_DAPM_PGA("PGAR", WM8737_POWER_MANAGEMENT, 4, 0, NULL, 0),
  199. SND_SOC_DAPM_DAC("ADCL", NULL, WM8737_POWER_MANAGEMENT, 3, 0),
  200. SND_SOC_DAPM_DAC("ADCR", NULL, WM8737_POWER_MANAGEMENT, 2, 0),
  201. SND_SOC_DAPM_AIF_OUT("AIF", "Capture", 0, WM8737_POWER_MANAGEMENT, 6, 0),
  202. };
  203. static const struct snd_soc_dapm_route intercon[] = {
  204. { "LINSEL", "LINPUT1", "LINPUT1" },
  205. { "LINSEL", "LINPUT2", "LINPUT2" },
  206. { "LINSEL", "LINPUT3", "LINPUT3" },
  207. { "LINSEL", "LINPUT1 DC", "LINPUT1" },
  208. { "RINSEL", "RINPUT1", "RINPUT1" },
  209. { "RINSEL", "RINPUT2", "RINPUT2" },
  210. { "RINSEL", "RINPUT3", "RINPUT3" },
  211. { "RINSEL", "RINPUT1 DC", "RINPUT1" },
  212. { "Left Preamp Mux", "Preamp", "LINSEL" },
  213. { "Left Preamp Mux", "Direct", "LACIN" },
  214. { "Right Preamp Mux", "Preamp", "RINSEL" },
  215. { "Right Preamp Mux", "Direct", "RACIN" },
  216. { "PGAL", NULL, "Left Preamp Mux" },
  217. { "PGAR", NULL, "Right Preamp Mux" },
  218. { "ADCL", NULL, "PGAL" },
  219. { "ADCR", NULL, "PGAR" },
  220. { "AIF", NULL, "ADCL" },
  221. { "AIF", NULL, "ADCR" },
  222. };
  223. /* codec mclk clock divider coefficients */
  224. static const struct {
  225. u32 mclk;
  226. u32 rate;
  227. u8 usb;
  228. u8 sr;
  229. } coeff_div[] = {
  230. { 12288000, 8000, 0, 0x4 },
  231. { 12288000, 12000, 0, 0x8 },
  232. { 12288000, 16000, 0, 0xa },
  233. { 12288000, 24000, 0, 0x1c },
  234. { 12288000, 32000, 0, 0xc },
  235. { 12288000, 48000, 0, 0 },
  236. { 12288000, 96000, 0, 0xe },
  237. { 11289600, 8000, 0, 0x14 },
  238. { 11289600, 11025, 0, 0x18 },
  239. { 11289600, 22050, 0, 0x1a },
  240. { 11289600, 44100, 0, 0x10 },
  241. { 11289600, 88200, 0, 0x1e },
  242. { 18432000, 8000, 0, 0x5 },
  243. { 18432000, 12000, 0, 0x9 },
  244. { 18432000, 16000, 0, 0xb },
  245. { 18432000, 24000, 0, 0x1b },
  246. { 18432000, 32000, 0, 0xd },
  247. { 18432000, 48000, 0, 0x1 },
  248. { 18432000, 96000, 0, 0x1f },
  249. { 16934400, 8000, 0, 0x15 },
  250. { 16934400, 11025, 0, 0x19 },
  251. { 16934400, 22050, 0, 0x1b },
  252. { 16934400, 44100, 0, 0x11 },
  253. { 16934400, 88200, 0, 0x1f },
  254. { 12000000, 8000, 1, 0x4 },
  255. { 12000000, 11025, 1, 0x19 },
  256. { 12000000, 12000, 1, 0x8 },
  257. { 12000000, 16000, 1, 0xa },
  258. { 12000000, 22050, 1, 0x1b },
  259. { 12000000, 24000, 1, 0x1c },
  260. { 12000000, 32000, 1, 0xc },
  261. { 12000000, 44100, 1, 0x11 },
  262. { 12000000, 48000, 1, 0x0 },
  263. { 12000000, 88200, 1, 0x1f },
  264. { 12000000, 96000, 1, 0xe },
  265. };
  266. static int wm8737_hw_params(struct snd_pcm_substream *substream,
  267. struct snd_pcm_hw_params *params,
  268. struct snd_soc_dai *dai)
  269. {
  270. struct snd_soc_codec *codec = dai->codec;
  271. struct wm8737_priv *wm8737 = snd_soc_codec_get_drvdata(codec);
  272. int i;
  273. u16 clocking = 0;
  274. u16 af = 0;
  275. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  276. if (coeff_div[i].rate != params_rate(params))
  277. continue;
  278. if (coeff_div[i].mclk == wm8737->mclk)
  279. break;
  280. if (coeff_div[i].mclk == wm8737->mclk * 2) {
  281. clocking |= WM8737_CLKDIV2;
  282. break;
  283. }
  284. }
  285. if (i == ARRAY_SIZE(coeff_div)) {
  286. dev_err(codec->dev, "%dHz MCLK can't support %dHz\n",
  287. wm8737->mclk, params_rate(params));
  288. return -EINVAL;
  289. }
  290. clocking |= coeff_div[i].usb | (coeff_div[i].sr << WM8737_SR_SHIFT);
  291. switch (params_width(params)) {
  292. case 16:
  293. break;
  294. case 20:
  295. af |= 0x8;
  296. break;
  297. case 24:
  298. af |= 0x10;
  299. break;
  300. case 32:
  301. af |= 0x18;
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. snd_soc_update_bits(codec, WM8737_AUDIO_FORMAT, WM8737_WL_MASK, af);
  307. snd_soc_update_bits(codec, WM8737_CLOCKING,
  308. WM8737_USB_MODE | WM8737_CLKDIV2 | WM8737_SR_MASK,
  309. clocking);
  310. return 0;
  311. }
  312. static int wm8737_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  313. int clk_id, unsigned int freq, int dir)
  314. {
  315. struct snd_soc_codec *codec = codec_dai->codec;
  316. struct wm8737_priv *wm8737 = snd_soc_codec_get_drvdata(codec);
  317. int i;
  318. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  319. if (freq == coeff_div[i].mclk ||
  320. freq == coeff_div[i].mclk * 2) {
  321. wm8737->mclk = freq;
  322. return 0;
  323. }
  324. }
  325. dev_err(codec->dev, "MCLK rate %dHz not supported\n", freq);
  326. return -EINVAL;
  327. }
  328. static int wm8737_set_dai_fmt(struct snd_soc_dai *codec_dai,
  329. unsigned int fmt)
  330. {
  331. struct snd_soc_codec *codec = codec_dai->codec;
  332. u16 af = 0;
  333. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  334. case SND_SOC_DAIFMT_CBM_CFM:
  335. af |= WM8737_MS;
  336. break;
  337. case SND_SOC_DAIFMT_CBS_CFS:
  338. break;
  339. default:
  340. return -EINVAL;
  341. }
  342. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  343. case SND_SOC_DAIFMT_I2S:
  344. af |= 0x2;
  345. break;
  346. case SND_SOC_DAIFMT_RIGHT_J:
  347. break;
  348. case SND_SOC_DAIFMT_LEFT_J:
  349. af |= 0x1;
  350. break;
  351. case SND_SOC_DAIFMT_DSP_A:
  352. af |= 0x3;
  353. break;
  354. case SND_SOC_DAIFMT_DSP_B:
  355. af |= 0x13;
  356. break;
  357. default:
  358. return -EINVAL;
  359. }
  360. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  361. case SND_SOC_DAIFMT_NB_NF:
  362. break;
  363. case SND_SOC_DAIFMT_NB_IF:
  364. af |= WM8737_LRP;
  365. break;
  366. default:
  367. return -EINVAL;
  368. }
  369. snd_soc_update_bits(codec, WM8737_AUDIO_FORMAT,
  370. WM8737_FORMAT_MASK | WM8737_LRP | WM8737_MS, af);
  371. return 0;
  372. }
  373. static int wm8737_set_bias_level(struct snd_soc_codec *codec,
  374. enum snd_soc_bias_level level)
  375. {
  376. struct wm8737_priv *wm8737 = snd_soc_codec_get_drvdata(codec);
  377. int ret;
  378. switch (level) {
  379. case SND_SOC_BIAS_ON:
  380. break;
  381. case SND_SOC_BIAS_PREPARE:
  382. /* VMID at 2*75k */
  383. snd_soc_update_bits(codec, WM8737_MISC_BIAS_CONTROL,
  384. WM8737_VMIDSEL_MASK, 0);
  385. break;
  386. case SND_SOC_BIAS_STANDBY:
  387. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  388. ret = regulator_bulk_enable(ARRAY_SIZE(wm8737->supplies),
  389. wm8737->supplies);
  390. if (ret != 0) {
  391. dev_err(codec->dev,
  392. "Failed to enable supplies: %d\n",
  393. ret);
  394. return ret;
  395. }
  396. regcache_sync(wm8737->regmap);
  397. /* Fast VMID ramp at 2*2.5k */
  398. snd_soc_update_bits(codec, WM8737_MISC_BIAS_CONTROL,
  399. WM8737_VMIDSEL_MASK,
  400. 2 << WM8737_VMIDSEL_SHIFT);
  401. /* Bring VMID up */
  402. snd_soc_update_bits(codec, WM8737_POWER_MANAGEMENT,
  403. WM8737_VMID_MASK |
  404. WM8737_VREF_MASK,
  405. WM8737_VMID_MASK |
  406. WM8737_VREF_MASK);
  407. msleep(500);
  408. }
  409. /* VMID at 2*300k */
  410. snd_soc_update_bits(codec, WM8737_MISC_BIAS_CONTROL,
  411. WM8737_VMIDSEL_MASK,
  412. 1 << WM8737_VMIDSEL_SHIFT);
  413. break;
  414. case SND_SOC_BIAS_OFF:
  415. snd_soc_update_bits(codec, WM8737_POWER_MANAGEMENT,
  416. WM8737_VMID_MASK | WM8737_VREF_MASK, 0);
  417. regulator_bulk_disable(ARRAY_SIZE(wm8737->supplies),
  418. wm8737->supplies);
  419. break;
  420. }
  421. return 0;
  422. }
  423. #define WM8737_RATES SNDRV_PCM_RATE_8000_96000
  424. #define WM8737_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  425. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  426. static const struct snd_soc_dai_ops wm8737_dai_ops = {
  427. .hw_params = wm8737_hw_params,
  428. .set_sysclk = wm8737_set_dai_sysclk,
  429. .set_fmt = wm8737_set_dai_fmt,
  430. };
  431. static struct snd_soc_dai_driver wm8737_dai = {
  432. .name = "wm8737",
  433. .capture = {
  434. .stream_name = "Capture",
  435. .channels_min = 2, /* Mono modes not yet supported */
  436. .channels_max = 2,
  437. .rates = WM8737_RATES,
  438. .formats = WM8737_FORMATS,
  439. },
  440. .ops = &wm8737_dai_ops,
  441. };
  442. static int wm8737_probe(struct snd_soc_codec *codec)
  443. {
  444. struct wm8737_priv *wm8737 = snd_soc_codec_get_drvdata(codec);
  445. int ret;
  446. ret = regulator_bulk_enable(ARRAY_SIZE(wm8737->supplies),
  447. wm8737->supplies);
  448. if (ret != 0) {
  449. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  450. goto err_get;
  451. }
  452. ret = wm8737_reset(codec);
  453. if (ret < 0) {
  454. dev_err(codec->dev, "Failed to issue reset\n");
  455. goto err_enable;
  456. }
  457. snd_soc_update_bits(codec, WM8737_LEFT_PGA_VOLUME, WM8737_LVU,
  458. WM8737_LVU);
  459. snd_soc_update_bits(codec, WM8737_RIGHT_PGA_VOLUME, WM8737_RVU,
  460. WM8737_RVU);
  461. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
  462. /* Bias level configuration will have done an extra enable */
  463. regulator_bulk_disable(ARRAY_SIZE(wm8737->supplies), wm8737->supplies);
  464. return 0;
  465. err_enable:
  466. regulator_bulk_disable(ARRAY_SIZE(wm8737->supplies), wm8737->supplies);
  467. err_get:
  468. return ret;
  469. }
  470. static const struct snd_soc_codec_driver soc_codec_dev_wm8737 = {
  471. .probe = wm8737_probe,
  472. .set_bias_level = wm8737_set_bias_level,
  473. .suspend_bias_off = true,
  474. .component_driver = {
  475. .controls = wm8737_snd_controls,
  476. .num_controls = ARRAY_SIZE(wm8737_snd_controls),
  477. .dapm_widgets = wm8737_dapm_widgets,
  478. .num_dapm_widgets = ARRAY_SIZE(wm8737_dapm_widgets),
  479. .dapm_routes = intercon,
  480. .num_dapm_routes = ARRAY_SIZE(intercon),
  481. },
  482. };
  483. static const struct of_device_id wm8737_of_match[] = {
  484. { .compatible = "wlf,wm8737", },
  485. { }
  486. };
  487. MODULE_DEVICE_TABLE(of, wm8737_of_match);
  488. static const struct regmap_config wm8737_regmap = {
  489. .reg_bits = 7,
  490. .val_bits = 9,
  491. .max_register = WM8737_MAX_REGISTER,
  492. .reg_defaults = wm8737_reg_defaults,
  493. .num_reg_defaults = ARRAY_SIZE(wm8737_reg_defaults),
  494. .cache_type = REGCACHE_RBTREE,
  495. .volatile_reg = wm8737_volatile,
  496. };
  497. #if IS_ENABLED(CONFIG_I2C)
  498. static int wm8737_i2c_probe(struct i2c_client *i2c,
  499. const struct i2c_device_id *id)
  500. {
  501. struct wm8737_priv *wm8737;
  502. int ret, i;
  503. wm8737 = devm_kzalloc(&i2c->dev, sizeof(struct wm8737_priv),
  504. GFP_KERNEL);
  505. if (wm8737 == NULL)
  506. return -ENOMEM;
  507. for (i = 0; i < ARRAY_SIZE(wm8737->supplies); i++)
  508. wm8737->supplies[i].supply = wm8737_supply_names[i];
  509. ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8737->supplies),
  510. wm8737->supplies);
  511. if (ret != 0) {
  512. dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
  513. return ret;
  514. }
  515. wm8737->regmap = devm_regmap_init_i2c(i2c, &wm8737_regmap);
  516. if (IS_ERR(wm8737->regmap))
  517. return PTR_ERR(wm8737->regmap);
  518. i2c_set_clientdata(i2c, wm8737);
  519. ret = snd_soc_register_codec(&i2c->dev,
  520. &soc_codec_dev_wm8737, &wm8737_dai, 1);
  521. return ret;
  522. }
  523. static int wm8737_i2c_remove(struct i2c_client *client)
  524. {
  525. snd_soc_unregister_codec(&client->dev);
  526. return 0;
  527. }
  528. static const struct i2c_device_id wm8737_i2c_id[] = {
  529. { "wm8737", 0 },
  530. { }
  531. };
  532. MODULE_DEVICE_TABLE(i2c, wm8737_i2c_id);
  533. static struct i2c_driver wm8737_i2c_driver = {
  534. .driver = {
  535. .name = "wm8737",
  536. .of_match_table = wm8737_of_match,
  537. },
  538. .probe = wm8737_i2c_probe,
  539. .remove = wm8737_i2c_remove,
  540. .id_table = wm8737_i2c_id,
  541. };
  542. #endif
  543. #if defined(CONFIG_SPI_MASTER)
  544. static int wm8737_spi_probe(struct spi_device *spi)
  545. {
  546. struct wm8737_priv *wm8737;
  547. int ret, i;
  548. wm8737 = devm_kzalloc(&spi->dev, sizeof(struct wm8737_priv),
  549. GFP_KERNEL);
  550. if (wm8737 == NULL)
  551. return -ENOMEM;
  552. for (i = 0; i < ARRAY_SIZE(wm8737->supplies); i++)
  553. wm8737->supplies[i].supply = wm8737_supply_names[i];
  554. ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8737->supplies),
  555. wm8737->supplies);
  556. if (ret != 0) {
  557. dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
  558. return ret;
  559. }
  560. wm8737->regmap = devm_regmap_init_spi(spi, &wm8737_regmap);
  561. if (IS_ERR(wm8737->regmap))
  562. return PTR_ERR(wm8737->regmap);
  563. spi_set_drvdata(spi, wm8737);
  564. ret = snd_soc_register_codec(&spi->dev,
  565. &soc_codec_dev_wm8737, &wm8737_dai, 1);
  566. return ret;
  567. }
  568. static int wm8737_spi_remove(struct spi_device *spi)
  569. {
  570. snd_soc_unregister_codec(&spi->dev);
  571. return 0;
  572. }
  573. static struct spi_driver wm8737_spi_driver = {
  574. .driver = {
  575. .name = "wm8737",
  576. .of_match_table = wm8737_of_match,
  577. },
  578. .probe = wm8737_spi_probe,
  579. .remove = wm8737_spi_remove,
  580. };
  581. #endif /* CONFIG_SPI_MASTER */
  582. static int __init wm8737_modinit(void)
  583. {
  584. int ret;
  585. #if IS_ENABLED(CONFIG_I2C)
  586. ret = i2c_add_driver(&wm8737_i2c_driver);
  587. if (ret != 0) {
  588. printk(KERN_ERR "Failed to register WM8737 I2C driver: %d\n",
  589. ret);
  590. }
  591. #endif
  592. #if defined(CONFIG_SPI_MASTER)
  593. ret = spi_register_driver(&wm8737_spi_driver);
  594. if (ret != 0) {
  595. printk(KERN_ERR "Failed to register WM8737 SPI driver: %d\n",
  596. ret);
  597. }
  598. #endif
  599. return 0;
  600. }
  601. module_init(wm8737_modinit);
  602. static void __exit wm8737_exit(void)
  603. {
  604. #if defined(CONFIG_SPI_MASTER)
  605. spi_unregister_driver(&wm8737_spi_driver);
  606. #endif
  607. #if IS_ENABLED(CONFIG_I2C)
  608. i2c_del_driver(&wm8737_i2c_driver);
  609. #endif
  610. }
  611. module_exit(wm8737_exit);
  612. MODULE_DESCRIPTION("ASoC WM8737 driver");
  613. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  614. MODULE_LICENSE("GPL");