wm8737.c 19 KB

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