wm8350.c 49 KB

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