wm8350.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. /*
  2. * wm8350.c -- WM8350 ALSA SoC audio driver
  3. *
  4. * Copyright (C) 2007-12 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  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/slab.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mfd/wm8350/audio.h>
  20. #include <linux/mfd/wm8350/core.h>
  21. #include <linux/regulator/consumer.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/initval.h>
  27. #include <sound/tlv.h>
  28. #include <trace/events/asoc.h>
  29. #include "wm8350.h"
  30. #define WM8350_OUTn_0dB 0x39
  31. #define WM8350_RAMP_NONE 0
  32. #define WM8350_RAMP_UP 1
  33. #define WM8350_RAMP_DOWN 2
  34. /* We only include the analogue supplies here; the digital supplies
  35. * need to be available well before this driver can be probed.
  36. */
  37. static const char *supply_names[] = {
  38. "AVDD",
  39. "HPVDD",
  40. };
  41. struct wm8350_output {
  42. u16 active;
  43. u16 left_vol;
  44. u16 right_vol;
  45. u16 ramp;
  46. u16 mute;
  47. };
  48. struct wm8350_jack_data {
  49. struct snd_soc_jack *jack;
  50. struct delayed_work work;
  51. int report;
  52. int short_report;
  53. };
  54. struct wm8350_data {
  55. struct wm8350 *wm8350;
  56. struct wm8350_output out1;
  57. struct wm8350_output out2;
  58. struct wm8350_jack_data hpl;
  59. struct wm8350_jack_data hpr;
  60. struct wm8350_jack_data mic;
  61. struct regulator_bulk_data supplies[ARRAY_SIZE(supply_names)];
  62. int fll_freq_out;
  63. int fll_freq_in;
  64. struct delayed_work pga_work;
  65. };
  66. /*
  67. * Ramp OUT1 PGA volume to minimise pops at stream startup and shutdown.
  68. */
  69. static inline int wm8350_out1_ramp_step(struct wm8350_data *wm8350_data)
  70. {
  71. struct wm8350_output *out1 = &wm8350_data->out1;
  72. struct wm8350 *wm8350 = wm8350_data->wm8350;
  73. int left_complete = 0, right_complete = 0;
  74. u16 reg, val;
  75. /* left channel */
  76. reg = wm8350_reg_read(wm8350, WM8350_LOUT1_VOLUME);
  77. val = (reg & WM8350_OUT1L_VOL_MASK) >> WM8350_OUT1L_VOL_SHIFT;
  78. if (out1->ramp == WM8350_RAMP_UP) {
  79. /* ramp step up */
  80. if (val < out1->left_vol) {
  81. val++;
  82. reg &= ~WM8350_OUT1L_VOL_MASK;
  83. wm8350_reg_write(wm8350, WM8350_LOUT1_VOLUME,
  84. reg | (val << WM8350_OUT1L_VOL_SHIFT));
  85. } else
  86. left_complete = 1;
  87. } else if (out1->ramp == WM8350_RAMP_DOWN) {
  88. /* ramp step down */
  89. if (val > 0) {
  90. val--;
  91. reg &= ~WM8350_OUT1L_VOL_MASK;
  92. wm8350_reg_write(wm8350, WM8350_LOUT1_VOLUME,
  93. reg | (val << WM8350_OUT1L_VOL_SHIFT));
  94. } else
  95. left_complete = 1;
  96. } else
  97. return 1;
  98. /* right channel */
  99. reg = wm8350_reg_read(wm8350, WM8350_ROUT1_VOLUME);
  100. val = (reg & WM8350_OUT1R_VOL_MASK) >> WM8350_OUT1R_VOL_SHIFT;
  101. if (out1->ramp == WM8350_RAMP_UP) {
  102. /* ramp step up */
  103. if (val < out1->right_vol) {
  104. val++;
  105. reg &= ~WM8350_OUT1R_VOL_MASK;
  106. wm8350_reg_write(wm8350, WM8350_ROUT1_VOLUME,
  107. reg | (val << WM8350_OUT1R_VOL_SHIFT));
  108. } else
  109. right_complete = 1;
  110. } else if (out1->ramp == WM8350_RAMP_DOWN) {
  111. /* ramp step down */
  112. if (val > 0) {
  113. val--;
  114. reg &= ~WM8350_OUT1R_VOL_MASK;
  115. wm8350_reg_write(wm8350, WM8350_ROUT1_VOLUME,
  116. reg | (val << WM8350_OUT1R_VOL_SHIFT));
  117. } else
  118. right_complete = 1;
  119. }
  120. /* only hit the update bit if either volume has changed this step */
  121. if (!left_complete || !right_complete)
  122. wm8350_set_bits(wm8350, WM8350_LOUT1_VOLUME, WM8350_OUT1_VU);
  123. return left_complete & right_complete;
  124. }
  125. /*
  126. * Ramp OUT2 PGA volume to minimise pops at stream startup and shutdown.
  127. */
  128. static inline int wm8350_out2_ramp_step(struct wm8350_data *wm8350_data)
  129. {
  130. struct wm8350_output *out2 = &wm8350_data->out2;
  131. struct wm8350 *wm8350 = wm8350_data->wm8350;
  132. int left_complete = 0, right_complete = 0;
  133. u16 reg, val;
  134. /* left channel */
  135. reg = wm8350_reg_read(wm8350, WM8350_LOUT2_VOLUME);
  136. val = (reg & WM8350_OUT2L_VOL_MASK) >> WM8350_OUT1L_VOL_SHIFT;
  137. if (out2->ramp == WM8350_RAMP_UP) {
  138. /* ramp step up */
  139. if (val < out2->left_vol) {
  140. val++;
  141. reg &= ~WM8350_OUT2L_VOL_MASK;
  142. wm8350_reg_write(wm8350, WM8350_LOUT2_VOLUME,
  143. reg | (val << WM8350_OUT1L_VOL_SHIFT));
  144. } else
  145. left_complete = 1;
  146. } else if (out2->ramp == WM8350_RAMP_DOWN) {
  147. /* ramp step down */
  148. if (val > 0) {
  149. val--;
  150. reg &= ~WM8350_OUT2L_VOL_MASK;
  151. wm8350_reg_write(wm8350, WM8350_LOUT2_VOLUME,
  152. reg | (val << WM8350_OUT1L_VOL_SHIFT));
  153. } else
  154. left_complete = 1;
  155. } else
  156. return 1;
  157. /* right channel */
  158. reg = wm8350_reg_read(wm8350, WM8350_ROUT2_VOLUME);
  159. val = (reg & WM8350_OUT2R_VOL_MASK) >> WM8350_OUT1R_VOL_SHIFT;
  160. if (out2->ramp == WM8350_RAMP_UP) {
  161. /* ramp step up */
  162. if (val < out2->right_vol) {
  163. val++;
  164. reg &= ~WM8350_OUT2R_VOL_MASK;
  165. wm8350_reg_write(wm8350, WM8350_ROUT2_VOLUME,
  166. reg | (val << WM8350_OUT1R_VOL_SHIFT));
  167. } else
  168. right_complete = 1;
  169. } else if (out2->ramp == WM8350_RAMP_DOWN) {
  170. /* ramp step down */
  171. if (val > 0) {
  172. val--;
  173. reg &= ~WM8350_OUT2R_VOL_MASK;
  174. wm8350_reg_write(wm8350, WM8350_ROUT2_VOLUME,
  175. reg | (val << WM8350_OUT1R_VOL_SHIFT));
  176. } else
  177. right_complete = 1;
  178. }
  179. /* only hit the update bit if either volume has changed this step */
  180. if (!left_complete || !right_complete)
  181. wm8350_set_bits(wm8350, WM8350_LOUT2_VOLUME, WM8350_OUT2_VU);
  182. return left_complete & right_complete;
  183. }
  184. /*
  185. * This work ramps both output PGAs at stream start/stop time to
  186. * minimise pop associated with DAPM power switching.
  187. * It's best to enable Zero Cross when ramping occurs to minimise any
  188. * zipper noises.
  189. */
  190. static void wm8350_pga_work(struct work_struct *work)
  191. {
  192. struct wm8350_data *wm8350_data =
  193. container_of(work, struct wm8350_data, pga_work.work);
  194. struct wm8350_output *out1 = &wm8350_data->out1,
  195. *out2 = &wm8350_data->out2;
  196. int i, out1_complete, out2_complete;
  197. /* do we need to ramp at all ? */
  198. if (out1->ramp == WM8350_RAMP_NONE && out2->ramp == WM8350_RAMP_NONE)
  199. return;
  200. /* PGA volumes have 6 bits of resolution to ramp */
  201. for (i = 0; i <= 63; i++) {
  202. out1_complete = 1, out2_complete = 1;
  203. if (out1->ramp != WM8350_RAMP_NONE)
  204. out1_complete = wm8350_out1_ramp_step(wm8350_data);
  205. if (out2->ramp != WM8350_RAMP_NONE)
  206. out2_complete = wm8350_out2_ramp_step(wm8350_data);
  207. /* ramp finished ? */
  208. if (out1_complete && out2_complete)
  209. break;
  210. /* we need to delay longer on the up ramp */
  211. if (out1->ramp == WM8350_RAMP_UP ||
  212. out2->ramp == WM8350_RAMP_UP) {
  213. /* delay is longer over 0dB as increases are larger */
  214. if (i >= WM8350_OUTn_0dB)
  215. schedule_timeout_interruptible(msecs_to_jiffies
  216. (2));
  217. else
  218. schedule_timeout_interruptible(msecs_to_jiffies
  219. (1));
  220. } else
  221. udelay(50); /* doesn't matter if we delay longer */
  222. }
  223. out1->ramp = WM8350_RAMP_NONE;
  224. out2->ramp = WM8350_RAMP_NONE;
  225. }
  226. /*
  227. * WM8350 Controls
  228. */
  229. static int pga_event(struct snd_soc_dapm_widget *w,
  230. struct snd_kcontrol *kcontrol, int event)
  231. {
  232. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  233. struct wm8350_data *wm8350_data = snd_soc_codec_get_drvdata(codec);
  234. struct wm8350_output *out;
  235. switch (w->shift) {
  236. case 0:
  237. case 1:
  238. out = &wm8350_data->out1;
  239. break;
  240. case 2:
  241. case 3:
  242. out = &wm8350_data->out2;
  243. break;
  244. default:
  245. WARN(1, "Invalid shift %d\n", w->shift);
  246. return -1;
  247. }
  248. switch (event) {
  249. case SND_SOC_DAPM_POST_PMU:
  250. out->ramp = WM8350_RAMP_UP;
  251. out->active = 1;
  252. schedule_delayed_work(&wm8350_data->pga_work,
  253. msecs_to_jiffies(1));
  254. break;
  255. case SND_SOC_DAPM_PRE_PMD:
  256. out->ramp = WM8350_RAMP_DOWN;
  257. out->active = 0;
  258. schedule_delayed_work(&wm8350_data->pga_work,
  259. msecs_to_jiffies(1));
  260. break;
  261. }
  262. return 0;
  263. }
  264. static int wm8350_put_volsw_2r_vu(struct snd_kcontrol *kcontrol,
  265. struct snd_ctl_elem_value *ucontrol)
  266. {
  267. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  268. struct wm8350_data *wm8350_priv = snd_soc_codec_get_drvdata(codec);
  269. struct wm8350_output *out = NULL;
  270. struct soc_mixer_control *mc =
  271. (struct soc_mixer_control *)kcontrol->private_value;
  272. int ret;
  273. unsigned int reg = mc->reg;
  274. u16 val;
  275. /* For OUT1 and OUT2 we shadow the values and only actually write
  276. * them out when active in order to ensure the amplifier comes on
  277. * as quietly as possible. */
  278. switch (reg) {
  279. case WM8350_LOUT1_VOLUME:
  280. out = &wm8350_priv->out1;
  281. break;
  282. case WM8350_LOUT2_VOLUME:
  283. out = &wm8350_priv->out2;
  284. break;
  285. default:
  286. break;
  287. }
  288. if (out) {
  289. out->left_vol = ucontrol->value.integer.value[0];
  290. out->right_vol = ucontrol->value.integer.value[1];
  291. if (!out->active)
  292. return 1;
  293. }
  294. ret = snd_soc_put_volsw(kcontrol, ucontrol);
  295. if (ret < 0)
  296. return ret;
  297. /* now hit the volume update bits (always bit 8) */
  298. val = snd_soc_read(codec, reg);
  299. snd_soc_write(codec, reg, val | WM8350_OUT1_VU);
  300. return 1;
  301. }
  302. static int wm8350_get_volsw_2r(struct snd_kcontrol *kcontrol,
  303. struct snd_ctl_elem_value *ucontrol)
  304. {
  305. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  306. struct wm8350_data *wm8350_priv = snd_soc_codec_get_drvdata(codec);
  307. struct wm8350_output *out1 = &wm8350_priv->out1;
  308. struct wm8350_output *out2 = &wm8350_priv->out2;
  309. struct soc_mixer_control *mc =
  310. (struct soc_mixer_control *)kcontrol->private_value;
  311. unsigned int reg = mc->reg;
  312. /* If these are cached registers use the cache */
  313. switch (reg) {
  314. case WM8350_LOUT1_VOLUME:
  315. ucontrol->value.integer.value[0] = out1->left_vol;
  316. ucontrol->value.integer.value[1] = out1->right_vol;
  317. return 0;
  318. case WM8350_LOUT2_VOLUME:
  319. ucontrol->value.integer.value[0] = out2->left_vol;
  320. ucontrol->value.integer.value[1] = out2->right_vol;
  321. return 0;
  322. default:
  323. break;
  324. }
  325. return snd_soc_get_volsw(kcontrol, ucontrol);
  326. }
  327. static const char *wm8350_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
  328. static const char *wm8350_pol[] = { "Normal", "Inv R", "Inv L", "Inv L & R" };
  329. static const char *wm8350_dacmutem[] = { "Normal", "Soft" };
  330. static const char *wm8350_dacmutes[] = { "Fast", "Slow" };
  331. static const char *wm8350_adcfilter[] = { "None", "High Pass" };
  332. static const char *wm8350_adchp[] = { "44.1kHz", "8kHz", "16kHz", "32kHz" };
  333. static const char *wm8350_lr[] = { "Left", "Right" };
  334. static const struct soc_enum wm8350_enum[] = {
  335. SOC_ENUM_SINGLE(WM8350_DAC_CONTROL, 4, 4, wm8350_deemp),
  336. SOC_ENUM_SINGLE(WM8350_DAC_CONTROL, 0, 4, wm8350_pol),
  337. SOC_ENUM_SINGLE(WM8350_DAC_MUTE_VOLUME, 14, 2, wm8350_dacmutem),
  338. SOC_ENUM_SINGLE(WM8350_DAC_MUTE_VOLUME, 13, 2, wm8350_dacmutes),
  339. SOC_ENUM_SINGLE(WM8350_ADC_CONTROL, 15, 2, wm8350_adcfilter),
  340. SOC_ENUM_SINGLE(WM8350_ADC_CONTROL, 8, 4, wm8350_adchp),
  341. SOC_ENUM_SINGLE(WM8350_ADC_CONTROL, 0, 4, wm8350_pol),
  342. SOC_ENUM_SINGLE(WM8350_INPUT_MIXER_VOLUME, 15, 2, wm8350_lr),
  343. };
  344. static DECLARE_TLV_DB_SCALE(pre_amp_tlv, -1200, 3525, 0);
  345. static DECLARE_TLV_DB_SCALE(out_pga_tlv, -5700, 600, 0);
  346. static DECLARE_TLV_DB_SCALE(dac_pcm_tlv, -7163, 36, 1);
  347. static DECLARE_TLV_DB_SCALE(adc_pcm_tlv, -12700, 50, 1);
  348. static DECLARE_TLV_DB_SCALE(out_mix_tlv, -1500, 300, 1);
  349. static const DECLARE_TLV_DB_RANGE(capture_sd_tlv,
  350. 0, 12, TLV_DB_SCALE_ITEM(-3600, 300, 1),
  351. 13, 15, TLV_DB_SCALE_ITEM(0, 0, 0)
  352. );
  353. static const struct snd_kcontrol_new wm8350_snd_controls[] = {
  354. SOC_ENUM("Playback Deemphasis", wm8350_enum[0]),
  355. SOC_ENUM("Playback DAC Inversion", wm8350_enum[1]),
  356. SOC_DOUBLE_R_EXT_TLV("Playback PCM Volume",
  357. WM8350_DAC_DIGITAL_VOLUME_L,
  358. WM8350_DAC_DIGITAL_VOLUME_R,
  359. 0, 255, 0, wm8350_get_volsw_2r,
  360. wm8350_put_volsw_2r_vu, dac_pcm_tlv),
  361. SOC_ENUM("Playback PCM Mute Function", wm8350_enum[2]),
  362. SOC_ENUM("Playback PCM Mute Speed", wm8350_enum[3]),
  363. SOC_ENUM("Capture PCM Filter", wm8350_enum[4]),
  364. SOC_ENUM("Capture PCM HP Filter", wm8350_enum[5]),
  365. SOC_ENUM("Capture ADC Inversion", wm8350_enum[6]),
  366. SOC_DOUBLE_R_EXT_TLV("Capture PCM Volume",
  367. WM8350_ADC_DIGITAL_VOLUME_L,
  368. WM8350_ADC_DIGITAL_VOLUME_R,
  369. 0, 255, 0, wm8350_get_volsw_2r,
  370. wm8350_put_volsw_2r_vu, adc_pcm_tlv),
  371. SOC_DOUBLE_TLV("Capture Sidetone Volume",
  372. WM8350_ADC_DIVIDER,
  373. 8, 4, 15, 1, capture_sd_tlv),
  374. SOC_DOUBLE_R_EXT_TLV("Capture Volume",
  375. WM8350_LEFT_INPUT_VOLUME,
  376. WM8350_RIGHT_INPUT_VOLUME,
  377. 2, 63, 0, wm8350_get_volsw_2r,
  378. wm8350_put_volsw_2r_vu, pre_amp_tlv),
  379. SOC_DOUBLE_R("Capture ZC Switch",
  380. WM8350_LEFT_INPUT_VOLUME,
  381. WM8350_RIGHT_INPUT_VOLUME, 13, 1, 0),
  382. SOC_SINGLE_TLV("Left Input Left Sidetone Volume",
  383. WM8350_OUTPUT_LEFT_MIXER_VOLUME, 1, 7, 0, out_mix_tlv),
  384. SOC_SINGLE_TLV("Left Input Right Sidetone Volume",
  385. WM8350_OUTPUT_LEFT_MIXER_VOLUME,
  386. 5, 7, 0, out_mix_tlv),
  387. SOC_SINGLE_TLV("Left Input Bypass Volume",
  388. WM8350_OUTPUT_LEFT_MIXER_VOLUME,
  389. 9, 7, 0, out_mix_tlv),
  390. SOC_SINGLE_TLV("Right Input Left Sidetone Volume",
  391. WM8350_OUTPUT_RIGHT_MIXER_VOLUME,
  392. 1, 7, 0, out_mix_tlv),
  393. SOC_SINGLE_TLV("Right Input Right Sidetone Volume",
  394. WM8350_OUTPUT_RIGHT_MIXER_VOLUME,
  395. 5, 7, 0, out_mix_tlv),
  396. SOC_SINGLE_TLV("Right Input Bypass Volume",
  397. WM8350_OUTPUT_RIGHT_MIXER_VOLUME,
  398. 13, 7, 0, out_mix_tlv),
  399. SOC_SINGLE("Left Input Mixer +20dB Switch",
  400. WM8350_INPUT_MIXER_VOLUME_L, 0, 1, 0),
  401. SOC_SINGLE("Right Input Mixer +20dB Switch",
  402. WM8350_INPUT_MIXER_VOLUME_R, 0, 1, 0),
  403. SOC_SINGLE_TLV("Out4 Capture Volume",
  404. WM8350_INPUT_MIXER_VOLUME,
  405. 1, 7, 0, out_mix_tlv),
  406. SOC_DOUBLE_R_EXT_TLV("Out1 Playback Volume",
  407. WM8350_LOUT1_VOLUME,
  408. WM8350_ROUT1_VOLUME,
  409. 2, 63, 0, wm8350_get_volsw_2r,
  410. wm8350_put_volsw_2r_vu, out_pga_tlv),
  411. SOC_DOUBLE_R("Out1 Playback ZC Switch",
  412. WM8350_LOUT1_VOLUME,
  413. WM8350_ROUT1_VOLUME, 13, 1, 0),
  414. SOC_DOUBLE_R_EXT_TLV("Out2 Playback Volume",
  415. WM8350_LOUT2_VOLUME,
  416. WM8350_ROUT2_VOLUME,
  417. 2, 63, 0, wm8350_get_volsw_2r,
  418. wm8350_put_volsw_2r_vu, out_pga_tlv),
  419. SOC_DOUBLE_R("Out2 Playback ZC Switch", WM8350_LOUT2_VOLUME,
  420. WM8350_ROUT2_VOLUME, 13, 1, 0),
  421. SOC_SINGLE("Out2 Right Invert Switch", WM8350_ROUT2_VOLUME, 10, 1, 0),
  422. SOC_SINGLE_TLV("Out2 Beep Volume", WM8350_BEEP_VOLUME,
  423. 5, 7, 0, out_mix_tlv),
  424. SOC_DOUBLE_R("Out1 Playback Switch",
  425. WM8350_LOUT1_VOLUME,
  426. WM8350_ROUT1_VOLUME,
  427. 14, 1, 1),
  428. SOC_DOUBLE_R("Out2 Playback Switch",
  429. WM8350_LOUT2_VOLUME,
  430. WM8350_ROUT2_VOLUME,
  431. 14, 1, 1),
  432. };
  433. /*
  434. * DAPM Controls
  435. */
  436. /* Left Playback Mixer */
  437. static const struct snd_kcontrol_new wm8350_left_play_mixer_controls[] = {
  438. SOC_DAPM_SINGLE("Playback Switch",
  439. WM8350_LEFT_MIXER_CONTROL, 11, 1, 0),
  440. SOC_DAPM_SINGLE("Left Bypass Switch",
  441. WM8350_LEFT_MIXER_CONTROL, 2, 1, 0),
  442. SOC_DAPM_SINGLE("Right Playback Switch",
  443. WM8350_LEFT_MIXER_CONTROL, 12, 1, 0),
  444. SOC_DAPM_SINGLE("Left Sidetone Switch",
  445. WM8350_LEFT_MIXER_CONTROL, 0, 1, 0),
  446. SOC_DAPM_SINGLE("Right Sidetone Switch",
  447. WM8350_LEFT_MIXER_CONTROL, 1, 1, 0),
  448. };
  449. /* Right Playback Mixer */
  450. static const struct snd_kcontrol_new wm8350_right_play_mixer_controls[] = {
  451. SOC_DAPM_SINGLE("Playback Switch",
  452. WM8350_RIGHT_MIXER_CONTROL, 12, 1, 0),
  453. SOC_DAPM_SINGLE("Right Bypass Switch",
  454. WM8350_RIGHT_MIXER_CONTROL, 3, 1, 0),
  455. SOC_DAPM_SINGLE("Left Playback Switch",
  456. WM8350_RIGHT_MIXER_CONTROL, 11, 1, 0),
  457. SOC_DAPM_SINGLE("Left Sidetone Switch",
  458. WM8350_RIGHT_MIXER_CONTROL, 0, 1, 0),
  459. SOC_DAPM_SINGLE("Right Sidetone Switch",
  460. WM8350_RIGHT_MIXER_CONTROL, 1, 1, 0),
  461. };
  462. /* Out4 Mixer */
  463. static const struct snd_kcontrol_new wm8350_out4_mixer_controls[] = {
  464. SOC_DAPM_SINGLE("Right Playback Switch",
  465. WM8350_OUT4_MIXER_CONTROL, 12, 1, 0),
  466. SOC_DAPM_SINGLE("Left Playback Switch",
  467. WM8350_OUT4_MIXER_CONTROL, 11, 1, 0),
  468. SOC_DAPM_SINGLE("Right Capture Switch",
  469. WM8350_OUT4_MIXER_CONTROL, 9, 1, 0),
  470. SOC_DAPM_SINGLE("Out3 Playback Switch",
  471. WM8350_OUT4_MIXER_CONTROL, 2, 1, 0),
  472. SOC_DAPM_SINGLE("Right Mixer Switch",
  473. WM8350_OUT4_MIXER_CONTROL, 1, 1, 0),
  474. SOC_DAPM_SINGLE("Left Mixer Switch",
  475. WM8350_OUT4_MIXER_CONTROL, 0, 1, 0),
  476. };
  477. /* Out3 Mixer */
  478. static const struct snd_kcontrol_new wm8350_out3_mixer_controls[] = {
  479. SOC_DAPM_SINGLE("Left Playback Switch",
  480. WM8350_OUT3_MIXER_CONTROL, 11, 1, 0),
  481. SOC_DAPM_SINGLE("Left Capture Switch",
  482. WM8350_OUT3_MIXER_CONTROL, 8, 1, 0),
  483. SOC_DAPM_SINGLE("Out4 Playback Switch",
  484. WM8350_OUT3_MIXER_CONTROL, 3, 1, 0),
  485. SOC_DAPM_SINGLE("Left Mixer Switch",
  486. WM8350_OUT3_MIXER_CONTROL, 0, 1, 0),
  487. };
  488. /* Left Input Mixer */
  489. static const struct snd_kcontrol_new wm8350_left_capt_mixer_controls[] = {
  490. SOC_DAPM_SINGLE_TLV("L2 Capture Volume",
  491. WM8350_INPUT_MIXER_VOLUME_L, 1, 7, 0, out_mix_tlv),
  492. SOC_DAPM_SINGLE_TLV("L3 Capture Volume",
  493. WM8350_INPUT_MIXER_VOLUME_L, 9, 7, 0, out_mix_tlv),
  494. SOC_DAPM_SINGLE("PGA Capture Switch",
  495. WM8350_LEFT_INPUT_VOLUME, 14, 1, 1),
  496. };
  497. /* Right Input Mixer */
  498. static const struct snd_kcontrol_new wm8350_right_capt_mixer_controls[] = {
  499. SOC_DAPM_SINGLE_TLV("L2 Capture Volume",
  500. WM8350_INPUT_MIXER_VOLUME_R, 5, 7, 0, out_mix_tlv),
  501. SOC_DAPM_SINGLE_TLV("L3 Capture Volume",
  502. WM8350_INPUT_MIXER_VOLUME_R, 13, 7, 0, out_mix_tlv),
  503. SOC_DAPM_SINGLE("PGA Capture Switch",
  504. WM8350_RIGHT_INPUT_VOLUME, 14, 1, 1),
  505. };
  506. /* Left Mic Mixer */
  507. static const struct snd_kcontrol_new wm8350_left_mic_mixer_controls[] = {
  508. SOC_DAPM_SINGLE("INN Capture Switch", WM8350_INPUT_CONTROL, 1, 1, 0),
  509. SOC_DAPM_SINGLE("INP Capture Switch", WM8350_INPUT_CONTROL, 0, 1, 0),
  510. SOC_DAPM_SINGLE("IN2 Capture Switch", WM8350_INPUT_CONTROL, 2, 1, 0),
  511. };
  512. /* Right Mic Mixer */
  513. static const struct snd_kcontrol_new wm8350_right_mic_mixer_controls[] = {
  514. SOC_DAPM_SINGLE("INN Capture Switch", WM8350_INPUT_CONTROL, 9, 1, 0),
  515. SOC_DAPM_SINGLE("INP Capture Switch", WM8350_INPUT_CONTROL, 8, 1, 0),
  516. SOC_DAPM_SINGLE("IN2 Capture Switch", WM8350_INPUT_CONTROL, 10, 1, 0),
  517. };
  518. /* Beep Switch */
  519. static const struct snd_kcontrol_new wm8350_beep_switch_controls =
  520. SOC_DAPM_SINGLE("Switch", WM8350_BEEP_VOLUME, 15, 1, 1);
  521. /* Out4 Capture Mux */
  522. static const struct snd_kcontrol_new wm8350_out4_capture_controls =
  523. SOC_DAPM_ENUM("Route", wm8350_enum[7]);
  524. static const struct snd_soc_dapm_widget wm8350_dapm_widgets[] = {
  525. SND_SOC_DAPM_PGA("IN3R PGA", WM8350_POWER_MGMT_2, 11, 0, NULL, 0),
  526. SND_SOC_DAPM_PGA("IN3L PGA", WM8350_POWER_MGMT_2, 10, 0, NULL, 0),
  527. SND_SOC_DAPM_PGA_E("Right Out2 PGA", WM8350_POWER_MGMT_3, 3, 0, NULL,
  528. 0, pga_event,
  529. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  530. SND_SOC_DAPM_PGA_E("Left Out2 PGA", WM8350_POWER_MGMT_3, 2, 0, NULL, 0,
  531. pga_event,
  532. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  533. SND_SOC_DAPM_PGA_E("Right Out1 PGA", WM8350_POWER_MGMT_3, 1, 0, NULL,
  534. 0, pga_event,
  535. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  536. SND_SOC_DAPM_PGA_E("Left Out1 PGA", WM8350_POWER_MGMT_3, 0, 0, NULL, 0,
  537. pga_event,
  538. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  539. SND_SOC_DAPM_MIXER("Right Capture Mixer", WM8350_POWER_MGMT_2,
  540. 7, 0, &wm8350_right_capt_mixer_controls[0],
  541. ARRAY_SIZE(wm8350_right_capt_mixer_controls)),
  542. SND_SOC_DAPM_MIXER("Left Capture Mixer", WM8350_POWER_MGMT_2,
  543. 6, 0, &wm8350_left_capt_mixer_controls[0],
  544. ARRAY_SIZE(wm8350_left_capt_mixer_controls)),
  545. SND_SOC_DAPM_MIXER("Out4 Mixer", WM8350_POWER_MGMT_2, 5, 0,
  546. &wm8350_out4_mixer_controls[0],
  547. ARRAY_SIZE(wm8350_out4_mixer_controls)),
  548. SND_SOC_DAPM_MIXER("Out3 Mixer", WM8350_POWER_MGMT_2, 4, 0,
  549. &wm8350_out3_mixer_controls[0],
  550. ARRAY_SIZE(wm8350_out3_mixer_controls)),
  551. SND_SOC_DAPM_MIXER("Right Playback Mixer", WM8350_POWER_MGMT_2, 1, 0,
  552. &wm8350_right_play_mixer_controls[0],
  553. ARRAY_SIZE(wm8350_right_play_mixer_controls)),
  554. SND_SOC_DAPM_MIXER("Left Playback Mixer", WM8350_POWER_MGMT_2, 0, 0,
  555. &wm8350_left_play_mixer_controls[0],
  556. ARRAY_SIZE(wm8350_left_play_mixer_controls)),
  557. SND_SOC_DAPM_MIXER("Left Mic Mixer", WM8350_POWER_MGMT_2, 8, 0,
  558. &wm8350_left_mic_mixer_controls[0],
  559. ARRAY_SIZE(wm8350_left_mic_mixer_controls)),
  560. SND_SOC_DAPM_MIXER("Right Mic Mixer", WM8350_POWER_MGMT_2, 9, 0,
  561. &wm8350_right_mic_mixer_controls[0],
  562. ARRAY_SIZE(wm8350_right_mic_mixer_controls)),
  563. /* virtual mixer for Beep and Out2R */
  564. SND_SOC_DAPM_MIXER("Out2 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
  565. SND_SOC_DAPM_SWITCH("Beep", WM8350_POWER_MGMT_3, 7, 0,
  566. &wm8350_beep_switch_controls),
  567. SND_SOC_DAPM_ADC("Right ADC", "Right Capture",
  568. WM8350_POWER_MGMT_4, 3, 0),
  569. SND_SOC_DAPM_ADC("Left ADC", "Left Capture",
  570. WM8350_POWER_MGMT_4, 2, 0),
  571. SND_SOC_DAPM_DAC("Right DAC", "Right Playback",
  572. WM8350_POWER_MGMT_4, 5, 0),
  573. SND_SOC_DAPM_DAC("Left DAC", "Left Playback",
  574. WM8350_POWER_MGMT_4, 4, 0),
  575. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8350_POWER_MGMT_1, 4, 0),
  576. SND_SOC_DAPM_MUX("Out4 Capture Channel", SND_SOC_NOPM, 0, 0,
  577. &wm8350_out4_capture_controls),
  578. SND_SOC_DAPM_OUTPUT("OUT1R"),
  579. SND_SOC_DAPM_OUTPUT("OUT1L"),
  580. SND_SOC_DAPM_OUTPUT("OUT2R"),
  581. SND_SOC_DAPM_OUTPUT("OUT2L"),
  582. SND_SOC_DAPM_OUTPUT("OUT3"),
  583. SND_SOC_DAPM_OUTPUT("OUT4"),
  584. SND_SOC_DAPM_INPUT("IN1RN"),
  585. SND_SOC_DAPM_INPUT("IN1RP"),
  586. SND_SOC_DAPM_INPUT("IN2R"),
  587. SND_SOC_DAPM_INPUT("IN1LP"),
  588. SND_SOC_DAPM_INPUT("IN1LN"),
  589. SND_SOC_DAPM_INPUT("IN2L"),
  590. SND_SOC_DAPM_INPUT("IN3R"),
  591. SND_SOC_DAPM_INPUT("IN3L"),
  592. };
  593. static const struct snd_soc_dapm_route wm8350_dapm_routes[] = {
  594. /* left playback mixer */
  595. {"Left Playback Mixer", "Playback Switch", "Left DAC"},
  596. {"Left Playback Mixer", "Left Bypass Switch", "IN3L PGA"},
  597. {"Left Playback Mixer", "Right Playback Switch", "Right DAC"},
  598. {"Left Playback Mixer", "Left Sidetone Switch", "Left Mic Mixer"},
  599. {"Left Playback Mixer", "Right Sidetone Switch", "Right Mic Mixer"},
  600. /* right playback mixer */
  601. {"Right Playback Mixer", "Playback Switch", "Right DAC"},
  602. {"Right Playback Mixer", "Right Bypass Switch", "IN3R PGA"},
  603. {"Right Playback Mixer", "Left Playback Switch", "Left DAC"},
  604. {"Right Playback Mixer", "Left Sidetone Switch", "Left Mic Mixer"},
  605. {"Right Playback Mixer", "Right Sidetone Switch", "Right Mic Mixer"},
  606. /* out4 playback mixer */
  607. {"Out4 Mixer", "Right Playback Switch", "Right DAC"},
  608. {"Out4 Mixer", "Left Playback Switch", "Left DAC"},
  609. {"Out4 Mixer", "Right Capture Switch", "Right Capture Mixer"},
  610. {"Out4 Mixer", "Out3 Playback Switch", "Out3 Mixer"},
  611. {"Out4 Mixer", "Right Mixer Switch", "Right Playback Mixer"},
  612. {"Out4 Mixer", "Left Mixer Switch", "Left Playback Mixer"},
  613. {"OUT4", NULL, "Out4 Mixer"},
  614. /* out3 playback mixer */
  615. {"Out3 Mixer", "Left Playback Switch", "Left DAC"},
  616. {"Out3 Mixer", "Left Capture Switch", "Left Capture Mixer"},
  617. {"Out3 Mixer", "Left Mixer Switch", "Left Playback Mixer"},
  618. {"Out3 Mixer", "Out4 Playback Switch", "Out4 Mixer"},
  619. {"OUT3", NULL, "Out3 Mixer"},
  620. /* out2 */
  621. {"Right Out2 PGA", NULL, "Right Playback Mixer"},
  622. {"Left Out2 PGA", NULL, "Left Playback Mixer"},
  623. {"OUT2L", NULL, "Left Out2 PGA"},
  624. {"OUT2R", NULL, "Right Out2 PGA"},
  625. /* out1 */
  626. {"Right Out1 PGA", NULL, "Right Playback Mixer"},
  627. {"Left Out1 PGA", NULL, "Left Playback Mixer"},
  628. {"OUT1L", NULL, "Left Out1 PGA"},
  629. {"OUT1R", NULL, "Right Out1 PGA"},
  630. /* ADCs */
  631. {"Left ADC", NULL, "Left Capture Mixer"},
  632. {"Right ADC", NULL, "Right Capture Mixer"},
  633. /* Left capture mixer */
  634. {"Left Capture Mixer", "L2 Capture Volume", "IN2L"},
  635. {"Left Capture Mixer", "L3 Capture Volume", "IN3L PGA"},
  636. {"Left Capture Mixer", "PGA Capture Switch", "Left Mic Mixer"},
  637. {"Left Capture Mixer", NULL, "Out4 Capture Channel"},
  638. /* Right capture mixer */
  639. {"Right Capture Mixer", "L2 Capture Volume", "IN2R"},
  640. {"Right Capture Mixer", "L3 Capture Volume", "IN3R PGA"},
  641. {"Right Capture Mixer", "PGA Capture Switch", "Right Mic Mixer"},
  642. {"Right Capture Mixer", NULL, "Out4 Capture Channel"},
  643. /* L3 Inputs */
  644. {"IN3L PGA", NULL, "IN3L"},
  645. {"IN3R PGA", NULL, "IN3R"},
  646. /* Left Mic mixer */
  647. {"Left Mic Mixer", "INN Capture Switch", "IN1LN"},
  648. {"Left Mic Mixer", "INP Capture Switch", "IN1LP"},
  649. {"Left Mic Mixer", "IN2 Capture Switch", "IN2L"},
  650. /* Right Mic mixer */
  651. {"Right Mic Mixer", "INN Capture Switch", "IN1RN"},
  652. {"Right Mic Mixer", "INP Capture Switch", "IN1RP"},
  653. {"Right Mic Mixer", "IN2 Capture Switch", "IN2R"},
  654. /* out 4 capture */
  655. {"Out4 Capture Channel", NULL, "Out4 Mixer"},
  656. /* Beep */
  657. {"Beep", NULL, "IN3R PGA"},
  658. };
  659. static int wm8350_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  660. int clk_id, unsigned int freq, int dir)
  661. {
  662. struct snd_soc_codec *codec = codec_dai->codec;
  663. struct wm8350_data *wm8350_data = snd_soc_codec_get_drvdata(codec);
  664. struct wm8350 *wm8350 = wm8350_data->wm8350;
  665. u16 fll_4;
  666. switch (clk_id) {
  667. case WM8350_MCLK_SEL_MCLK:
  668. wm8350_clear_bits(wm8350, WM8350_CLOCK_CONTROL_1,
  669. WM8350_MCLK_SEL);
  670. break;
  671. case WM8350_MCLK_SEL_PLL_MCLK:
  672. case WM8350_MCLK_SEL_PLL_DAC:
  673. case WM8350_MCLK_SEL_PLL_ADC:
  674. case WM8350_MCLK_SEL_PLL_32K:
  675. wm8350_set_bits(wm8350, WM8350_CLOCK_CONTROL_1,
  676. WM8350_MCLK_SEL);
  677. fll_4 = snd_soc_read(codec, WM8350_FLL_CONTROL_4) &
  678. ~WM8350_FLL_CLK_SRC_MASK;
  679. snd_soc_write(codec, WM8350_FLL_CONTROL_4, fll_4 | clk_id);
  680. break;
  681. }
  682. /* MCLK direction */
  683. if (dir == SND_SOC_CLOCK_OUT)
  684. wm8350_set_bits(wm8350, WM8350_CLOCK_CONTROL_2,
  685. WM8350_MCLK_DIR);
  686. else
  687. wm8350_clear_bits(wm8350, WM8350_CLOCK_CONTROL_2,
  688. WM8350_MCLK_DIR);
  689. return 0;
  690. }
  691. static int wm8350_set_clkdiv(struct snd_soc_dai *codec_dai, int div_id, int div)
  692. {
  693. struct snd_soc_codec *codec = codec_dai->codec;
  694. u16 val;
  695. switch (div_id) {
  696. case WM8350_ADC_CLKDIV:
  697. val = snd_soc_read(codec, WM8350_ADC_DIVIDER) &
  698. ~WM8350_ADC_CLKDIV_MASK;
  699. snd_soc_write(codec, WM8350_ADC_DIVIDER, val | div);
  700. break;
  701. case WM8350_DAC_CLKDIV:
  702. val = snd_soc_read(codec, WM8350_DAC_CLOCK_CONTROL) &
  703. ~WM8350_DAC_CLKDIV_MASK;
  704. snd_soc_write(codec, WM8350_DAC_CLOCK_CONTROL, val | div);
  705. break;
  706. case WM8350_BCLK_CLKDIV:
  707. val = snd_soc_read(codec, WM8350_CLOCK_CONTROL_1) &
  708. ~WM8350_BCLK_DIV_MASK;
  709. snd_soc_write(codec, WM8350_CLOCK_CONTROL_1, val | div);
  710. break;
  711. case WM8350_OPCLK_CLKDIV:
  712. val = snd_soc_read(codec, WM8350_CLOCK_CONTROL_1) &
  713. ~WM8350_OPCLK_DIV_MASK;
  714. snd_soc_write(codec, WM8350_CLOCK_CONTROL_1, val | div);
  715. break;
  716. case WM8350_SYS_CLKDIV:
  717. val = snd_soc_read(codec, WM8350_CLOCK_CONTROL_1) &
  718. ~WM8350_MCLK_DIV_MASK;
  719. snd_soc_write(codec, WM8350_CLOCK_CONTROL_1, val | div);
  720. break;
  721. case WM8350_DACLR_CLKDIV:
  722. val = snd_soc_read(codec, WM8350_DAC_LR_RATE) &
  723. ~WM8350_DACLRC_RATE_MASK;
  724. snd_soc_write(codec, WM8350_DAC_LR_RATE, val | div);
  725. break;
  726. case WM8350_ADCLR_CLKDIV:
  727. val = snd_soc_read(codec, WM8350_ADC_LR_RATE) &
  728. ~WM8350_ADCLRC_RATE_MASK;
  729. snd_soc_write(codec, WM8350_ADC_LR_RATE, val | div);
  730. break;
  731. default:
  732. return -EINVAL;
  733. }
  734. return 0;
  735. }
  736. static int wm8350_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  737. {
  738. struct snd_soc_codec *codec = codec_dai->codec;
  739. u16 iface = snd_soc_read(codec, WM8350_AI_FORMATING) &
  740. ~(WM8350_AIF_BCLK_INV | WM8350_AIF_LRCLK_INV | WM8350_AIF_FMT_MASK);
  741. u16 master = snd_soc_read(codec, WM8350_AI_DAC_CONTROL) &
  742. ~WM8350_BCLK_MSTR;
  743. u16 dac_lrc = snd_soc_read(codec, WM8350_DAC_LR_RATE) &
  744. ~WM8350_DACLRC_ENA;
  745. u16 adc_lrc = snd_soc_read(codec, WM8350_ADC_LR_RATE) &
  746. ~WM8350_ADCLRC_ENA;
  747. /* set master/slave audio interface */
  748. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  749. case SND_SOC_DAIFMT_CBM_CFM:
  750. master |= WM8350_BCLK_MSTR;
  751. dac_lrc |= WM8350_DACLRC_ENA;
  752. adc_lrc |= WM8350_ADCLRC_ENA;
  753. break;
  754. case SND_SOC_DAIFMT_CBS_CFS:
  755. break;
  756. default:
  757. return -EINVAL;
  758. }
  759. /* interface format */
  760. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  761. case SND_SOC_DAIFMT_I2S:
  762. iface |= 0x2 << 8;
  763. break;
  764. case SND_SOC_DAIFMT_RIGHT_J:
  765. break;
  766. case SND_SOC_DAIFMT_LEFT_J:
  767. iface |= 0x1 << 8;
  768. break;
  769. case SND_SOC_DAIFMT_DSP_A:
  770. iface |= 0x3 << 8;
  771. break;
  772. case SND_SOC_DAIFMT_DSP_B:
  773. iface |= 0x3 << 8 | WM8350_AIF_LRCLK_INV;
  774. break;
  775. default:
  776. return -EINVAL;
  777. }
  778. /* clock inversion */
  779. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  780. case SND_SOC_DAIFMT_NB_NF:
  781. break;
  782. case SND_SOC_DAIFMT_IB_IF:
  783. iface |= WM8350_AIF_LRCLK_INV | WM8350_AIF_BCLK_INV;
  784. break;
  785. case SND_SOC_DAIFMT_IB_NF:
  786. iface |= WM8350_AIF_BCLK_INV;
  787. break;
  788. case SND_SOC_DAIFMT_NB_IF:
  789. iface |= WM8350_AIF_LRCLK_INV;
  790. break;
  791. default:
  792. return -EINVAL;
  793. }
  794. snd_soc_write(codec, WM8350_AI_FORMATING, iface);
  795. snd_soc_write(codec, WM8350_AI_DAC_CONTROL, master);
  796. snd_soc_write(codec, WM8350_DAC_LR_RATE, dac_lrc);
  797. snd_soc_write(codec, WM8350_ADC_LR_RATE, adc_lrc);
  798. return 0;
  799. }
  800. static int wm8350_pcm_hw_params(struct snd_pcm_substream *substream,
  801. struct snd_pcm_hw_params *params,
  802. struct snd_soc_dai *codec_dai)
  803. {
  804. struct snd_soc_codec *codec = codec_dai->codec;
  805. struct wm8350_data *wm8350_data = snd_soc_codec_get_drvdata(codec);
  806. struct wm8350 *wm8350 = wm8350_data->wm8350;
  807. u16 iface = snd_soc_read(codec, WM8350_AI_FORMATING) &
  808. ~WM8350_AIF_WL_MASK;
  809. /* bit size */
  810. switch (params_width(params)) {
  811. case 16:
  812. break;
  813. case 20:
  814. iface |= 0x1 << 10;
  815. break;
  816. case 24:
  817. iface |= 0x2 << 10;
  818. break;
  819. case 32:
  820. iface |= 0x3 << 10;
  821. break;
  822. }
  823. snd_soc_write(codec, WM8350_AI_FORMATING, iface);
  824. /* The sloping stopband filter is recommended for use with
  825. * lower sample rates to improve performance.
  826. */
  827. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  828. if (params_rate(params) < 24000)
  829. wm8350_set_bits(wm8350, WM8350_DAC_MUTE_VOLUME,
  830. WM8350_DAC_SB_FILT);
  831. else
  832. wm8350_clear_bits(wm8350, WM8350_DAC_MUTE_VOLUME,
  833. WM8350_DAC_SB_FILT);
  834. }
  835. return 0;
  836. }
  837. static int wm8350_mute(struct snd_soc_dai *dai, int mute)
  838. {
  839. struct snd_soc_codec *codec = dai->codec;
  840. unsigned int val;
  841. if (mute)
  842. val = WM8350_DAC_MUTE_ENA;
  843. else
  844. val = 0;
  845. snd_soc_update_bits(codec, WM8350_DAC_MUTE, WM8350_DAC_MUTE_ENA, val);
  846. return 0;
  847. }
  848. /* FLL divisors */
  849. struct _fll_div {
  850. int div; /* FLL_OUTDIV */
  851. int n;
  852. int k;
  853. int ratio; /* FLL_FRATIO */
  854. };
  855. /* The size in bits of the fll divide multiplied by 10
  856. * to allow rounding later */
  857. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  858. static inline int fll_factors(struct _fll_div *fll_div, unsigned int input,
  859. unsigned int output)
  860. {
  861. u64 Kpart;
  862. unsigned int t1, t2, K, Nmod;
  863. if (output >= 2815250 && output <= 3125000)
  864. fll_div->div = 0x4;
  865. else if (output >= 5625000 && output <= 6250000)
  866. fll_div->div = 0x3;
  867. else if (output >= 11250000 && output <= 12500000)
  868. fll_div->div = 0x2;
  869. else if (output >= 22500000 && output <= 25000000)
  870. fll_div->div = 0x1;
  871. else {
  872. printk(KERN_ERR "wm8350: fll freq %d out of range\n", output);
  873. return -EINVAL;
  874. }
  875. if (input > 48000)
  876. fll_div->ratio = 1;
  877. else
  878. fll_div->ratio = 8;
  879. t1 = output * (1 << (fll_div->div + 1));
  880. t2 = input * fll_div->ratio;
  881. fll_div->n = t1 / t2;
  882. Nmod = t1 % t2;
  883. if (Nmod) {
  884. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  885. do_div(Kpart, t2);
  886. K = Kpart & 0xFFFFFFFF;
  887. /* Check if we need to round */
  888. if ((K % 10) >= 5)
  889. K += 5;
  890. /* Move down to proper range now rounding is done */
  891. K /= 10;
  892. fll_div->k = K;
  893. } else
  894. fll_div->k = 0;
  895. return 0;
  896. }
  897. static int wm8350_set_fll(struct snd_soc_dai *codec_dai,
  898. int pll_id, int source, unsigned int freq_in,
  899. unsigned int freq_out)
  900. {
  901. struct snd_soc_codec *codec = codec_dai->codec;
  902. struct wm8350_data *priv = snd_soc_codec_get_drvdata(codec);
  903. struct wm8350 *wm8350 = priv->wm8350;
  904. struct _fll_div fll_div;
  905. int ret = 0;
  906. u16 fll_1, fll_4;
  907. if (freq_in == priv->fll_freq_in && freq_out == priv->fll_freq_out)
  908. return 0;
  909. /* power down FLL - we need to do this for reconfiguration */
  910. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_4,
  911. WM8350_FLL_ENA | WM8350_FLL_OSC_ENA);
  912. if (freq_out == 0 || freq_in == 0)
  913. return ret;
  914. ret = fll_factors(&fll_div, freq_in, freq_out);
  915. if (ret < 0)
  916. return ret;
  917. dev_dbg(wm8350->dev,
  918. "FLL in %u FLL out %u N 0x%x K 0x%x div %d ratio %d",
  919. freq_in, freq_out, fll_div.n, fll_div.k, fll_div.div,
  920. fll_div.ratio);
  921. /* set up N.K & dividers */
  922. fll_1 = snd_soc_read(codec, WM8350_FLL_CONTROL_1) &
  923. ~(WM8350_FLL_OUTDIV_MASK | WM8350_FLL_RSP_RATE_MASK | 0xc000);
  924. snd_soc_write(codec, WM8350_FLL_CONTROL_1,
  925. fll_1 | (fll_div.div << 8) | 0x50);
  926. snd_soc_write(codec, WM8350_FLL_CONTROL_2,
  927. (fll_div.ratio << 11) | (fll_div.
  928. n & WM8350_FLL_N_MASK));
  929. snd_soc_write(codec, WM8350_FLL_CONTROL_3, fll_div.k);
  930. fll_4 = snd_soc_read(codec, WM8350_FLL_CONTROL_4) &
  931. ~(WM8350_FLL_FRAC | WM8350_FLL_SLOW_LOCK_REF);
  932. snd_soc_write(codec, WM8350_FLL_CONTROL_4,
  933. fll_4 | (fll_div.k ? WM8350_FLL_FRAC : 0) |
  934. (fll_div.ratio == 8 ? WM8350_FLL_SLOW_LOCK_REF : 0));
  935. /* power FLL on */
  936. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_4, WM8350_FLL_OSC_ENA);
  937. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_4, WM8350_FLL_ENA);
  938. priv->fll_freq_out = freq_out;
  939. priv->fll_freq_in = freq_in;
  940. return 0;
  941. }
  942. static int wm8350_set_bias_level(struct snd_soc_codec *codec,
  943. enum snd_soc_bias_level level)
  944. {
  945. struct wm8350_data *priv = snd_soc_codec_get_drvdata(codec);
  946. struct wm8350 *wm8350 = priv->wm8350;
  947. struct wm8350_audio_platform_data *platform =
  948. wm8350->codec.platform_data;
  949. u16 pm1;
  950. int ret;
  951. switch (level) {
  952. case SND_SOC_BIAS_ON:
  953. pm1 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_1) &
  954. ~(WM8350_VMID_MASK | WM8350_CODEC_ISEL_MASK);
  955. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1,
  956. pm1 | WM8350_VMID_50K |
  957. platform->codec_current_on << 14);
  958. break;
  959. case SND_SOC_BIAS_PREPARE:
  960. pm1 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_1);
  961. pm1 &= ~WM8350_VMID_MASK;
  962. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1,
  963. pm1 | WM8350_VMID_50K);
  964. break;
  965. case SND_SOC_BIAS_STANDBY:
  966. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  967. ret = regulator_bulk_enable(ARRAY_SIZE(priv->supplies),
  968. priv->supplies);
  969. if (ret != 0)
  970. return ret;
  971. /* Enable the system clock */
  972. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_4,
  973. WM8350_SYSCLK_ENA);
  974. /* mute DAC & outputs */
  975. wm8350_set_bits(wm8350, WM8350_DAC_MUTE,
  976. WM8350_DAC_MUTE_ENA);
  977. /* discharge cap memory */
  978. wm8350_reg_write(wm8350, WM8350_ANTI_POP_CONTROL,
  979. platform->dis_out1 |
  980. (platform->dis_out2 << 2) |
  981. (platform->dis_out3 << 4) |
  982. (platform->dis_out4 << 6));
  983. /* wait for discharge */
  984. schedule_timeout_interruptible(msecs_to_jiffies
  985. (platform->
  986. cap_discharge_msecs));
  987. /* enable antipop */
  988. wm8350_reg_write(wm8350, WM8350_ANTI_POP_CONTROL,
  989. (platform->vmid_s_curve << 8));
  990. /* ramp up vmid */
  991. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1,
  992. (platform->
  993. codec_current_charge << 14) |
  994. WM8350_VMID_5K | WM8350_VMIDEN |
  995. WM8350_VBUFEN);
  996. /* wait for vmid */
  997. schedule_timeout_interruptible(msecs_to_jiffies
  998. (platform->
  999. vmid_charge_msecs));
  1000. /* turn on vmid 300k */
  1001. pm1 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_1) &
  1002. ~(WM8350_VMID_MASK | WM8350_CODEC_ISEL_MASK);
  1003. pm1 |= WM8350_VMID_300K |
  1004. (platform->codec_current_standby << 14);
  1005. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1,
  1006. pm1);
  1007. /* enable analogue bias */
  1008. pm1 |= WM8350_BIASEN;
  1009. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1, pm1);
  1010. /* disable antipop */
  1011. wm8350_reg_write(wm8350, WM8350_ANTI_POP_CONTROL, 0);
  1012. } else {
  1013. /* turn on vmid 300k and reduce current */
  1014. pm1 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_1) &
  1015. ~(WM8350_VMID_MASK | WM8350_CODEC_ISEL_MASK);
  1016. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1,
  1017. pm1 | WM8350_VMID_300K |
  1018. (platform->
  1019. codec_current_standby << 14));
  1020. }
  1021. break;
  1022. case SND_SOC_BIAS_OFF:
  1023. /* mute DAC & enable outputs */
  1024. wm8350_set_bits(wm8350, WM8350_DAC_MUTE, WM8350_DAC_MUTE_ENA);
  1025. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_3,
  1026. WM8350_OUT1L_ENA | WM8350_OUT1R_ENA |
  1027. WM8350_OUT2L_ENA | WM8350_OUT2R_ENA);
  1028. /* enable anti pop S curve */
  1029. wm8350_reg_write(wm8350, WM8350_ANTI_POP_CONTROL,
  1030. (platform->vmid_s_curve << 8));
  1031. /* turn off vmid */
  1032. pm1 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_1) &
  1033. ~WM8350_VMIDEN;
  1034. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1, pm1);
  1035. /* wait */
  1036. schedule_timeout_interruptible(msecs_to_jiffies
  1037. (platform->
  1038. vmid_discharge_msecs));
  1039. wm8350_reg_write(wm8350, WM8350_ANTI_POP_CONTROL,
  1040. (platform->vmid_s_curve << 8) |
  1041. platform->dis_out1 |
  1042. (platform->dis_out2 << 2) |
  1043. (platform->dis_out3 << 4) |
  1044. (platform->dis_out4 << 6));
  1045. /* turn off VBuf and drain */
  1046. pm1 = wm8350_reg_read(wm8350, WM8350_POWER_MGMT_1) &
  1047. ~(WM8350_VBUFEN | WM8350_VMID_MASK);
  1048. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1,
  1049. pm1 | WM8350_OUTPUT_DRAIN_EN);
  1050. /* wait */
  1051. schedule_timeout_interruptible(msecs_to_jiffies
  1052. (platform->drain_msecs));
  1053. pm1 &= ~WM8350_BIASEN;
  1054. wm8350_reg_write(wm8350, WM8350_POWER_MGMT_1, pm1);
  1055. /* disable anti-pop */
  1056. wm8350_reg_write(wm8350, WM8350_ANTI_POP_CONTROL, 0);
  1057. wm8350_clear_bits(wm8350, WM8350_LOUT1_VOLUME,
  1058. WM8350_OUT1L_ENA);
  1059. wm8350_clear_bits(wm8350, WM8350_ROUT1_VOLUME,
  1060. WM8350_OUT1R_ENA);
  1061. wm8350_clear_bits(wm8350, WM8350_LOUT2_VOLUME,
  1062. WM8350_OUT2L_ENA);
  1063. wm8350_clear_bits(wm8350, WM8350_ROUT2_VOLUME,
  1064. WM8350_OUT2R_ENA);
  1065. /* disable clock gen */
  1066. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_4,
  1067. WM8350_SYSCLK_ENA);
  1068. regulator_bulk_disable(ARRAY_SIZE(priv->supplies),
  1069. priv->supplies);
  1070. break;
  1071. }
  1072. return 0;
  1073. }
  1074. static void wm8350_hp_work(struct wm8350_data *priv,
  1075. struct wm8350_jack_data *jack,
  1076. u16 mask)
  1077. {
  1078. struct wm8350 *wm8350 = priv->wm8350;
  1079. u16 reg;
  1080. int report;
  1081. reg = wm8350_reg_read(wm8350, WM8350_JACK_PIN_STATUS);
  1082. if (reg & mask)
  1083. report = jack->report;
  1084. else
  1085. report = 0;
  1086. snd_soc_jack_report(jack->jack, report, jack->report);
  1087. }
  1088. static void wm8350_hpl_work(struct work_struct *work)
  1089. {
  1090. struct wm8350_data *priv =
  1091. container_of(work, struct wm8350_data, hpl.work.work);
  1092. wm8350_hp_work(priv, &priv->hpl, WM8350_JACK_L_LVL);
  1093. }
  1094. static void wm8350_hpr_work(struct work_struct *work)
  1095. {
  1096. struct wm8350_data *priv =
  1097. container_of(work, struct wm8350_data, hpr.work.work);
  1098. wm8350_hp_work(priv, &priv->hpr, WM8350_JACK_R_LVL);
  1099. }
  1100. static irqreturn_t wm8350_hpl_jack_handler(int irq, void *data)
  1101. {
  1102. struct wm8350_data *priv = data;
  1103. struct wm8350 *wm8350 = priv->wm8350;
  1104. #ifndef CONFIG_SND_SOC_WM8350_MODULE
  1105. trace_snd_soc_jack_irq("WM8350 HPL");
  1106. #endif
  1107. if (device_may_wakeup(wm8350->dev))
  1108. pm_wakeup_event(wm8350->dev, 250);
  1109. queue_delayed_work(system_power_efficient_wq,
  1110. &priv->hpl.work, msecs_to_jiffies(200));
  1111. return IRQ_HANDLED;
  1112. }
  1113. static irqreturn_t wm8350_hpr_jack_handler(int irq, void *data)
  1114. {
  1115. struct wm8350_data *priv = data;
  1116. struct wm8350 *wm8350 = priv->wm8350;
  1117. #ifndef CONFIG_SND_SOC_WM8350_MODULE
  1118. trace_snd_soc_jack_irq("WM8350 HPR");
  1119. #endif
  1120. if (device_may_wakeup(wm8350->dev))
  1121. pm_wakeup_event(wm8350->dev, 250);
  1122. queue_delayed_work(system_power_efficient_wq,
  1123. &priv->hpr.work, msecs_to_jiffies(200));
  1124. return IRQ_HANDLED;
  1125. }
  1126. /**
  1127. * wm8350_hp_jack_detect - Enable headphone jack detection.
  1128. *
  1129. * @codec: WM8350 codec
  1130. * @which: left or right jack detect signal
  1131. * @jack: jack to report detection events on
  1132. * @report: value to report
  1133. *
  1134. * Enables the headphone jack detection of the WM8350. If no report
  1135. * is specified then detection is disabled.
  1136. */
  1137. int wm8350_hp_jack_detect(struct snd_soc_codec *codec, enum wm8350_jack which,
  1138. struct snd_soc_jack *jack, int report)
  1139. {
  1140. struct wm8350_data *priv = snd_soc_codec_get_drvdata(codec);
  1141. struct wm8350 *wm8350 = priv->wm8350;
  1142. int ena;
  1143. switch (which) {
  1144. case WM8350_JDL:
  1145. priv->hpl.jack = jack;
  1146. priv->hpl.report = report;
  1147. ena = WM8350_JDL_ENA;
  1148. break;
  1149. case WM8350_JDR:
  1150. priv->hpr.jack = jack;
  1151. priv->hpr.report = report;
  1152. ena = WM8350_JDR_ENA;
  1153. break;
  1154. default:
  1155. return -EINVAL;
  1156. }
  1157. if (report) {
  1158. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_4, WM8350_TOCLK_ENA);
  1159. wm8350_set_bits(wm8350, WM8350_JACK_DETECT, ena);
  1160. } else {
  1161. wm8350_clear_bits(wm8350, WM8350_JACK_DETECT, ena);
  1162. }
  1163. /* Sync status */
  1164. switch (which) {
  1165. case WM8350_JDL:
  1166. wm8350_hpl_jack_handler(0, priv);
  1167. break;
  1168. case WM8350_JDR:
  1169. wm8350_hpr_jack_handler(0, priv);
  1170. break;
  1171. }
  1172. return 0;
  1173. }
  1174. EXPORT_SYMBOL_GPL(wm8350_hp_jack_detect);
  1175. static irqreturn_t wm8350_mic_handler(int irq, void *data)
  1176. {
  1177. struct wm8350_data *priv = data;
  1178. struct wm8350 *wm8350 = priv->wm8350;
  1179. u16 reg;
  1180. int report = 0;
  1181. #ifndef CONFIG_SND_SOC_WM8350_MODULE
  1182. trace_snd_soc_jack_irq("WM8350 mic");
  1183. #endif
  1184. reg = wm8350_reg_read(wm8350, WM8350_JACK_PIN_STATUS);
  1185. if (reg & WM8350_JACK_MICSCD_LVL)
  1186. report |= priv->mic.short_report;
  1187. if (reg & WM8350_JACK_MICSD_LVL)
  1188. report |= priv->mic.report;
  1189. snd_soc_jack_report(priv->mic.jack, report,
  1190. priv->mic.report | priv->mic.short_report);
  1191. return IRQ_HANDLED;
  1192. }
  1193. /**
  1194. * wm8350_mic_jack_detect - Enable microphone jack detection.
  1195. *
  1196. * @codec: WM8350 codec
  1197. * @jack: jack to report detection events on
  1198. * @detect_report: value to report when presence detected
  1199. * @short_report: value to report when microphone short detected
  1200. *
  1201. * Enables the microphone jack detection of the WM8350. If both reports
  1202. * are specified as zero then detection is disabled.
  1203. */
  1204. int wm8350_mic_jack_detect(struct snd_soc_codec *codec,
  1205. struct snd_soc_jack *jack,
  1206. int detect_report, int short_report)
  1207. {
  1208. struct wm8350_data *priv = snd_soc_codec_get_drvdata(codec);
  1209. struct wm8350 *wm8350 = priv->wm8350;
  1210. priv->mic.jack = jack;
  1211. priv->mic.report = detect_report;
  1212. priv->mic.short_report = short_report;
  1213. if (detect_report || short_report) {
  1214. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_4, WM8350_TOCLK_ENA);
  1215. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_1,
  1216. WM8350_MIC_DET_ENA);
  1217. } else {
  1218. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_1,
  1219. WM8350_MIC_DET_ENA);
  1220. }
  1221. return 0;
  1222. }
  1223. EXPORT_SYMBOL_GPL(wm8350_mic_jack_detect);
  1224. #define WM8350_RATES (SNDRV_PCM_RATE_8000_96000)
  1225. #define WM8350_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  1226. SNDRV_PCM_FMTBIT_S20_3LE |\
  1227. SNDRV_PCM_FMTBIT_S24_LE)
  1228. static const struct snd_soc_dai_ops wm8350_dai_ops = {
  1229. .hw_params = wm8350_pcm_hw_params,
  1230. .digital_mute = wm8350_mute,
  1231. .set_fmt = wm8350_set_dai_fmt,
  1232. .set_sysclk = wm8350_set_dai_sysclk,
  1233. .set_pll = wm8350_set_fll,
  1234. .set_clkdiv = wm8350_set_clkdiv,
  1235. };
  1236. static struct snd_soc_dai_driver wm8350_dai = {
  1237. .name = "wm8350-hifi",
  1238. .playback = {
  1239. .stream_name = "Playback",
  1240. .channels_min = 1,
  1241. .channels_max = 2,
  1242. .rates = WM8350_RATES,
  1243. .formats = WM8350_FORMATS,
  1244. },
  1245. .capture = {
  1246. .stream_name = "Capture",
  1247. .channels_min = 1,
  1248. .channels_max = 2,
  1249. .rates = WM8350_RATES,
  1250. .formats = WM8350_FORMATS,
  1251. },
  1252. .ops = &wm8350_dai_ops,
  1253. };
  1254. static int wm8350_codec_probe(struct snd_soc_codec *codec)
  1255. {
  1256. struct wm8350 *wm8350 = dev_get_platdata(codec->dev);
  1257. struct wm8350_data *priv;
  1258. struct wm8350_output *out1;
  1259. struct wm8350_output *out2;
  1260. int ret, i;
  1261. if (wm8350->codec.platform_data == NULL) {
  1262. dev_err(codec->dev, "No audio platform data supplied\n");
  1263. return -EINVAL;
  1264. }
  1265. priv = devm_kzalloc(codec->dev, sizeof(struct wm8350_data),
  1266. GFP_KERNEL);
  1267. if (priv == NULL)
  1268. return -ENOMEM;
  1269. snd_soc_codec_set_drvdata(codec, priv);
  1270. priv->wm8350 = wm8350;
  1271. for (i = 0; i < ARRAY_SIZE(supply_names); i++)
  1272. priv->supplies[i].supply = supply_names[i];
  1273. ret = devm_regulator_bulk_get(wm8350->dev, ARRAY_SIZE(priv->supplies),
  1274. priv->supplies);
  1275. if (ret != 0)
  1276. return ret;
  1277. /* Put the codec into reset if it wasn't already */
  1278. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_5, WM8350_CODEC_ENA);
  1279. INIT_DELAYED_WORK(&priv->pga_work, wm8350_pga_work);
  1280. INIT_DELAYED_WORK(&priv->hpl.work, wm8350_hpl_work);
  1281. INIT_DELAYED_WORK(&priv->hpr.work, wm8350_hpr_work);
  1282. /* Enable the codec */
  1283. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5, WM8350_CODEC_ENA);
  1284. /* Enable robust clocking mode in ADC */
  1285. snd_soc_write(codec, WM8350_SECURITY, 0xa7);
  1286. snd_soc_write(codec, 0xde, 0x13);
  1287. snd_soc_write(codec, WM8350_SECURITY, 0);
  1288. /* read OUT1 & OUT2 volumes */
  1289. out1 = &priv->out1;
  1290. out2 = &priv->out2;
  1291. out1->left_vol = (wm8350_reg_read(wm8350, WM8350_LOUT1_VOLUME) &
  1292. WM8350_OUT1L_VOL_MASK) >> WM8350_OUT1L_VOL_SHIFT;
  1293. out1->right_vol = (wm8350_reg_read(wm8350, WM8350_ROUT1_VOLUME) &
  1294. WM8350_OUT1R_VOL_MASK) >> WM8350_OUT1R_VOL_SHIFT;
  1295. out2->left_vol = (wm8350_reg_read(wm8350, WM8350_LOUT2_VOLUME) &
  1296. WM8350_OUT2L_VOL_MASK) >> WM8350_OUT1L_VOL_SHIFT;
  1297. out2->right_vol = (wm8350_reg_read(wm8350, WM8350_ROUT2_VOLUME) &
  1298. WM8350_OUT2R_VOL_MASK) >> WM8350_OUT1R_VOL_SHIFT;
  1299. wm8350_reg_write(wm8350, WM8350_LOUT1_VOLUME, 0);
  1300. wm8350_reg_write(wm8350, WM8350_ROUT1_VOLUME, 0);
  1301. wm8350_reg_write(wm8350, WM8350_LOUT2_VOLUME, 0);
  1302. wm8350_reg_write(wm8350, WM8350_ROUT2_VOLUME, 0);
  1303. /* Latch VU bits & mute */
  1304. wm8350_set_bits(wm8350, WM8350_LOUT1_VOLUME,
  1305. WM8350_OUT1_VU | WM8350_OUT1L_MUTE);
  1306. wm8350_set_bits(wm8350, WM8350_LOUT2_VOLUME,
  1307. WM8350_OUT2_VU | WM8350_OUT2L_MUTE);
  1308. wm8350_set_bits(wm8350, WM8350_ROUT1_VOLUME,
  1309. WM8350_OUT1_VU | WM8350_OUT1R_MUTE);
  1310. wm8350_set_bits(wm8350, WM8350_ROUT2_VOLUME,
  1311. WM8350_OUT2_VU | WM8350_OUT2R_MUTE);
  1312. /* Make sure AIF tristating is disabled by default */
  1313. wm8350_clear_bits(wm8350, WM8350_AI_FORMATING, WM8350_AIF_TRI);
  1314. /* Make sure we've got a sane companding setup too */
  1315. wm8350_clear_bits(wm8350, WM8350_ADC_DAC_COMP,
  1316. WM8350_DAC_COMP | WM8350_LOOPBACK);
  1317. /* Make sure jack detect is disabled to start off with */
  1318. wm8350_clear_bits(wm8350, WM8350_JACK_DETECT,
  1319. WM8350_JDL_ENA | WM8350_JDR_ENA);
  1320. wm8350_register_irq(wm8350, WM8350_IRQ_CODEC_JCK_DET_L,
  1321. wm8350_hpl_jack_handler, 0, "Left jack detect",
  1322. priv);
  1323. wm8350_register_irq(wm8350, WM8350_IRQ_CODEC_JCK_DET_R,
  1324. wm8350_hpr_jack_handler, 0, "Right jack detect",
  1325. priv);
  1326. wm8350_register_irq(wm8350, WM8350_IRQ_CODEC_MICSCD,
  1327. wm8350_mic_handler, 0, "Microphone short", priv);
  1328. wm8350_register_irq(wm8350, WM8350_IRQ_CODEC_MICD,
  1329. wm8350_mic_handler, 0, "Microphone detect", priv);
  1330. return 0;
  1331. }
  1332. static int wm8350_codec_remove(struct snd_soc_codec *codec)
  1333. {
  1334. struct wm8350_data *priv = snd_soc_codec_get_drvdata(codec);
  1335. struct wm8350 *wm8350 = dev_get_platdata(codec->dev);
  1336. wm8350_clear_bits(wm8350, WM8350_JACK_DETECT,
  1337. WM8350_JDL_ENA | WM8350_JDR_ENA);
  1338. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_4, WM8350_TOCLK_ENA);
  1339. wm8350_free_irq(wm8350, WM8350_IRQ_CODEC_MICD, priv);
  1340. wm8350_free_irq(wm8350, WM8350_IRQ_CODEC_MICSCD, priv);
  1341. wm8350_free_irq(wm8350, WM8350_IRQ_CODEC_JCK_DET_L, priv);
  1342. wm8350_free_irq(wm8350, WM8350_IRQ_CODEC_JCK_DET_R, priv);
  1343. priv->hpl.jack = NULL;
  1344. priv->hpr.jack = NULL;
  1345. priv->mic.jack = NULL;
  1346. cancel_delayed_work_sync(&priv->hpl.work);
  1347. cancel_delayed_work_sync(&priv->hpr.work);
  1348. /* if there was any work waiting then we run it now and
  1349. * wait for its completion */
  1350. flush_delayed_work(&priv->pga_work);
  1351. wm8350_clear_bits(wm8350, WM8350_POWER_MGMT_5, WM8350_CODEC_ENA);
  1352. return 0;
  1353. }
  1354. static struct regmap *wm8350_get_regmap(struct device *dev)
  1355. {
  1356. struct wm8350 *wm8350 = dev_get_platdata(dev);
  1357. return wm8350->regmap;
  1358. }
  1359. static const struct snd_soc_codec_driver soc_codec_dev_wm8350 = {
  1360. .probe = wm8350_codec_probe,
  1361. .remove = wm8350_codec_remove,
  1362. .get_regmap = wm8350_get_regmap,
  1363. .set_bias_level = wm8350_set_bias_level,
  1364. .suspend_bias_off = true,
  1365. .component_driver = {
  1366. .controls = wm8350_snd_controls,
  1367. .num_controls = ARRAY_SIZE(wm8350_snd_controls),
  1368. .dapm_widgets = wm8350_dapm_widgets,
  1369. .num_dapm_widgets = ARRAY_SIZE(wm8350_dapm_widgets),
  1370. .dapm_routes = wm8350_dapm_routes,
  1371. .num_dapm_routes = ARRAY_SIZE(wm8350_dapm_routes),
  1372. },
  1373. };
  1374. static int wm8350_probe(struct platform_device *pdev)
  1375. {
  1376. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8350,
  1377. &wm8350_dai, 1);
  1378. }
  1379. static int wm8350_remove(struct platform_device *pdev)
  1380. {
  1381. snd_soc_unregister_codec(&pdev->dev);
  1382. return 0;
  1383. }
  1384. static struct platform_driver wm8350_codec_driver = {
  1385. .driver = {
  1386. .name = "wm8350-codec",
  1387. },
  1388. .probe = wm8350_probe,
  1389. .remove = wm8350_remove,
  1390. };
  1391. module_platform_driver(wm8350_codec_driver);
  1392. MODULE_DESCRIPTION("ASoC WM8350 driver");
  1393. MODULE_AUTHOR("Liam Girdwood");
  1394. MODULE_LICENSE("GPL");
  1395. MODULE_ALIAS("platform:wm8350-codec");