wm8993.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. /*
  2. * wm8993.c -- WM8993 ALSA SoC audio driver
  3. *
  4. * Copyright 2009, 2010 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/pm.h>
  17. #include <linux/i2c.h>
  18. #include <linux/regulator/consumer.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/slab.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/tlv.h>
  25. #include <sound/soc.h>
  26. #include <sound/initval.h>
  27. #include <sound/wm8993.h>
  28. #include "wm8993.h"
  29. #include "wm_hubs.h"
  30. #define WM8993_NUM_SUPPLIES 6
  31. static const char *wm8993_supply_names[WM8993_NUM_SUPPLIES] = {
  32. "DCVDD",
  33. "DBVDD",
  34. "AVDD1",
  35. "AVDD2",
  36. "CPVDD",
  37. "SPKVDD",
  38. };
  39. static u16 wm8993_reg_defaults[WM8993_REGISTER_COUNT] = {
  40. 0x8993, /* R0 - Software Reset */
  41. 0x0000, /* R1 - Power Management (1) */
  42. 0x6000, /* R2 - Power Management (2) */
  43. 0x0000, /* R3 - Power Management (3) */
  44. 0x4050, /* R4 - Audio Interface (1) */
  45. 0x4000, /* R5 - Audio Interface (2) */
  46. 0x01C8, /* R6 - Clocking 1 */
  47. 0x0000, /* R7 - Clocking 2 */
  48. 0x0000, /* R8 - Audio Interface (3) */
  49. 0x0040, /* R9 - Audio Interface (4) */
  50. 0x0004, /* R10 - DAC CTRL */
  51. 0x00C0, /* R11 - Left DAC Digital Volume */
  52. 0x00C0, /* R12 - Right DAC Digital Volume */
  53. 0x0000, /* R13 - Digital Side Tone */
  54. 0x0300, /* R14 - ADC CTRL */
  55. 0x00C0, /* R15 - Left ADC Digital Volume */
  56. 0x00C0, /* R16 - Right ADC Digital Volume */
  57. 0x0000, /* R17 */
  58. 0x0000, /* R18 - GPIO CTRL 1 */
  59. 0x0010, /* R19 - GPIO1 */
  60. 0x0000, /* R20 - IRQ_DEBOUNCE */
  61. 0x0000, /* R21 */
  62. 0x8000, /* R22 - GPIOCTRL 2 */
  63. 0x0800, /* R23 - GPIO_POL */
  64. 0x008B, /* R24 - Left Line Input 1&2 Volume */
  65. 0x008B, /* R25 - Left Line Input 3&4 Volume */
  66. 0x008B, /* R26 - Right Line Input 1&2 Volume */
  67. 0x008B, /* R27 - Right Line Input 3&4 Volume */
  68. 0x006D, /* R28 - Left Output Volume */
  69. 0x006D, /* R29 - Right Output Volume */
  70. 0x0066, /* R30 - Line Outputs Volume */
  71. 0x0020, /* R31 - HPOUT2 Volume */
  72. 0x0079, /* R32 - Left OPGA Volume */
  73. 0x0079, /* R33 - Right OPGA Volume */
  74. 0x0003, /* R34 - SPKMIXL Attenuation */
  75. 0x0003, /* R35 - SPKMIXR Attenuation */
  76. 0x0011, /* R36 - SPKOUT Mixers */
  77. 0x0100, /* R37 - SPKOUT Boost */
  78. 0x0079, /* R38 - Speaker Volume Left */
  79. 0x0079, /* R39 - Speaker Volume Right */
  80. 0x0000, /* R40 - Input Mixer2 */
  81. 0x0000, /* R41 - Input Mixer3 */
  82. 0x0000, /* R42 - Input Mixer4 */
  83. 0x0000, /* R43 - Input Mixer5 */
  84. 0x0000, /* R44 - Input Mixer6 */
  85. 0x0000, /* R45 - Output Mixer1 */
  86. 0x0000, /* R46 - Output Mixer2 */
  87. 0x0000, /* R47 - Output Mixer3 */
  88. 0x0000, /* R48 - Output Mixer4 */
  89. 0x0000, /* R49 - Output Mixer5 */
  90. 0x0000, /* R50 - Output Mixer6 */
  91. 0x0000, /* R51 - HPOUT2 Mixer */
  92. 0x0000, /* R52 - Line Mixer1 */
  93. 0x0000, /* R53 - Line Mixer2 */
  94. 0x0000, /* R54 - Speaker Mixer */
  95. 0x0000, /* R55 - Additional Control */
  96. 0x0000, /* R56 - AntiPOP1 */
  97. 0x0000, /* R57 - AntiPOP2 */
  98. 0x0000, /* R58 - MICBIAS */
  99. 0x0000, /* R59 */
  100. 0x0000, /* R60 - FLL Control 1 */
  101. 0x0000, /* R61 - FLL Control 2 */
  102. 0x0000, /* R62 - FLL Control 3 */
  103. 0x2EE0, /* R63 - FLL Control 4 */
  104. 0x0002, /* R64 - FLL Control 5 */
  105. 0x2287, /* R65 - Clocking 3 */
  106. 0x025F, /* R66 - Clocking 4 */
  107. 0x0000, /* R67 - MW Slave Control */
  108. 0x0000, /* R68 */
  109. 0x0002, /* R69 - Bus Control 1 */
  110. 0x0000, /* R70 - Write Sequencer 0 */
  111. 0x0000, /* R71 - Write Sequencer 1 */
  112. 0x0000, /* R72 - Write Sequencer 2 */
  113. 0x0000, /* R73 - Write Sequencer 3 */
  114. 0x0000, /* R74 - Write Sequencer 4 */
  115. 0x0000, /* R75 - Write Sequencer 5 */
  116. 0x1F25, /* R76 - Charge Pump 1 */
  117. 0x0000, /* R77 */
  118. 0x0000, /* R78 */
  119. 0x0000, /* R79 */
  120. 0x0000, /* R80 */
  121. 0x0000, /* R81 - Class W 0 */
  122. 0x0000, /* R82 */
  123. 0x0000, /* R83 */
  124. 0x0000, /* R84 - DC Servo 0 */
  125. 0x054A, /* R85 - DC Servo 1 */
  126. 0x0000, /* R86 */
  127. 0x0000, /* R87 - DC Servo 3 */
  128. 0x0000, /* R88 - DC Servo Readback 0 */
  129. 0x0000, /* R89 - DC Servo Readback 1 */
  130. 0x0000, /* R90 - DC Servo Readback 2 */
  131. 0x0000, /* R91 */
  132. 0x0000, /* R92 */
  133. 0x0000, /* R93 */
  134. 0x0000, /* R94 */
  135. 0x0000, /* R95 */
  136. 0x0100, /* R96 - Analogue HP 0 */
  137. 0x0000, /* R97 */
  138. 0x0000, /* R98 - EQ1 */
  139. 0x000C, /* R99 - EQ2 */
  140. 0x000C, /* R100 - EQ3 */
  141. 0x000C, /* R101 - EQ4 */
  142. 0x000C, /* R102 - EQ5 */
  143. 0x000C, /* R103 - EQ6 */
  144. 0x0FCA, /* R104 - EQ7 */
  145. 0x0400, /* R105 - EQ8 */
  146. 0x00D8, /* R106 - EQ9 */
  147. 0x1EB5, /* R107 - EQ10 */
  148. 0xF145, /* R108 - EQ11 */
  149. 0x0B75, /* R109 - EQ12 */
  150. 0x01C5, /* R110 - EQ13 */
  151. 0x1C58, /* R111 - EQ14 */
  152. 0xF373, /* R112 - EQ15 */
  153. 0x0A54, /* R113 - EQ16 */
  154. 0x0558, /* R114 - EQ17 */
  155. 0x168E, /* R115 - EQ18 */
  156. 0xF829, /* R116 - EQ19 */
  157. 0x07AD, /* R117 - EQ20 */
  158. 0x1103, /* R118 - EQ21 */
  159. 0x0564, /* R119 - EQ22 */
  160. 0x0559, /* R120 - EQ23 */
  161. 0x4000, /* R121 - EQ24 */
  162. 0x0000, /* R122 - Digital Pulls */
  163. 0x0F08, /* R123 - DRC Control 1 */
  164. 0x0000, /* R124 - DRC Control 2 */
  165. 0x0080, /* R125 - DRC Control 3 */
  166. 0x0000, /* R126 - DRC Control 4 */
  167. };
  168. static struct {
  169. int ratio;
  170. int clk_sys_rate;
  171. } clk_sys_rates[] = {
  172. { 64, 0 },
  173. { 128, 1 },
  174. { 192, 2 },
  175. { 256, 3 },
  176. { 384, 4 },
  177. { 512, 5 },
  178. { 768, 6 },
  179. { 1024, 7 },
  180. { 1408, 8 },
  181. { 1536, 9 },
  182. };
  183. static struct {
  184. int rate;
  185. int sample_rate;
  186. } sample_rates[] = {
  187. { 8000, 0 },
  188. { 11025, 1 },
  189. { 12000, 1 },
  190. { 16000, 2 },
  191. { 22050, 3 },
  192. { 24000, 3 },
  193. { 32000, 4 },
  194. { 44100, 5 },
  195. { 48000, 5 },
  196. };
  197. static struct {
  198. int div; /* *10 due to .5s */
  199. int bclk_div;
  200. } bclk_divs[] = {
  201. { 10, 0 },
  202. { 15, 1 },
  203. { 20, 2 },
  204. { 30, 3 },
  205. { 40, 4 },
  206. { 55, 5 },
  207. { 60, 6 },
  208. { 80, 7 },
  209. { 110, 8 },
  210. { 120, 9 },
  211. { 160, 10 },
  212. { 220, 11 },
  213. { 240, 12 },
  214. { 320, 13 },
  215. { 440, 14 },
  216. { 480, 15 },
  217. };
  218. struct wm8993_priv {
  219. struct wm_hubs_data hubs_data;
  220. struct regulator_bulk_data supplies[WM8993_NUM_SUPPLIES];
  221. struct wm8993_platform_data pdata;
  222. enum snd_soc_control_type control_type;
  223. int master;
  224. int sysclk_source;
  225. int tdm_slots;
  226. int tdm_width;
  227. unsigned int mclk_rate;
  228. unsigned int sysclk_rate;
  229. unsigned int fs;
  230. unsigned int bclk;
  231. int class_w_users;
  232. unsigned int fll_fref;
  233. unsigned int fll_fout;
  234. int fll_src;
  235. };
  236. static int wm8993_volatile(struct snd_soc_codec *codec, unsigned int reg)
  237. {
  238. switch (reg) {
  239. case WM8993_SOFTWARE_RESET:
  240. case WM8993_DC_SERVO_0:
  241. case WM8993_DC_SERVO_READBACK_0:
  242. case WM8993_DC_SERVO_READBACK_1:
  243. case WM8993_DC_SERVO_READBACK_2:
  244. return 1;
  245. default:
  246. return 0;
  247. }
  248. }
  249. struct _fll_div {
  250. u16 fll_fratio;
  251. u16 fll_outdiv;
  252. u16 fll_clk_ref_div;
  253. u16 n;
  254. u16 k;
  255. };
  256. /* The size in bits of the FLL divide multiplied by 10
  257. * to allow rounding later */
  258. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  259. static struct {
  260. unsigned int min;
  261. unsigned int max;
  262. u16 fll_fratio;
  263. int ratio;
  264. } fll_fratios[] = {
  265. { 0, 64000, 4, 16 },
  266. { 64000, 128000, 3, 8 },
  267. { 128000, 256000, 2, 4 },
  268. { 256000, 1000000, 1, 2 },
  269. { 1000000, 13500000, 0, 1 },
  270. };
  271. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  272. unsigned int Fout)
  273. {
  274. u64 Kpart;
  275. unsigned int K, Ndiv, Nmod, target;
  276. unsigned int div;
  277. int i;
  278. /* Fref must be <=13.5MHz */
  279. div = 1;
  280. fll_div->fll_clk_ref_div = 0;
  281. while ((Fref / div) > 13500000) {
  282. div *= 2;
  283. fll_div->fll_clk_ref_div++;
  284. if (div > 8) {
  285. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  286. Fref);
  287. return -EINVAL;
  288. }
  289. }
  290. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  291. /* Apply the division for our remaining calculations */
  292. Fref /= div;
  293. /* Fvco should be 90-100MHz; don't check the upper bound */
  294. div = 0;
  295. target = Fout * 2;
  296. while (target < 90000000) {
  297. div++;
  298. target *= 2;
  299. if (div > 7) {
  300. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  301. Fout);
  302. return -EINVAL;
  303. }
  304. }
  305. fll_div->fll_outdiv = div;
  306. pr_debug("Fvco=%dHz\n", target);
  307. /* Find an appropriate FLL_FRATIO and factor it out of the target */
  308. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  309. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  310. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  311. target /= fll_fratios[i].ratio;
  312. break;
  313. }
  314. }
  315. if (i == ARRAY_SIZE(fll_fratios)) {
  316. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  317. return -EINVAL;
  318. }
  319. /* Now, calculate N.K */
  320. Ndiv = target / Fref;
  321. fll_div->n = Ndiv;
  322. Nmod = target % Fref;
  323. pr_debug("Nmod=%d\n", Nmod);
  324. /* Calculate fractional part - scale up so we can round. */
  325. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  326. do_div(Kpart, Fref);
  327. K = Kpart & 0xFFFFFFFF;
  328. if ((K % 10) >= 5)
  329. K += 5;
  330. /* Move down to proper range now rounding is done */
  331. fll_div->k = K / 10;
  332. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  333. fll_div->n, fll_div->k,
  334. fll_div->fll_fratio, fll_div->fll_outdiv,
  335. fll_div->fll_clk_ref_div);
  336. return 0;
  337. }
  338. static int _wm8993_set_fll(struct snd_soc_codec *codec, int fll_id, int source,
  339. unsigned int Fref, unsigned int Fout)
  340. {
  341. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  342. u16 reg1, reg4, reg5;
  343. struct _fll_div fll_div;
  344. int ret;
  345. /* Any change? */
  346. if (Fref == wm8993->fll_fref && Fout == wm8993->fll_fout)
  347. return 0;
  348. /* Disable the FLL */
  349. if (Fout == 0) {
  350. dev_dbg(codec->dev, "FLL disabled\n");
  351. wm8993->fll_fref = 0;
  352. wm8993->fll_fout = 0;
  353. reg1 = snd_soc_read(codec, WM8993_FLL_CONTROL_1);
  354. reg1 &= ~WM8993_FLL_ENA;
  355. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  356. return 0;
  357. }
  358. ret = fll_factors(&fll_div, Fref, Fout);
  359. if (ret != 0)
  360. return ret;
  361. reg5 = snd_soc_read(codec, WM8993_FLL_CONTROL_5);
  362. reg5 &= ~WM8993_FLL_CLK_SRC_MASK;
  363. switch (fll_id) {
  364. case WM8993_FLL_MCLK:
  365. break;
  366. case WM8993_FLL_LRCLK:
  367. reg5 |= 1;
  368. break;
  369. case WM8993_FLL_BCLK:
  370. reg5 |= 2;
  371. break;
  372. default:
  373. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  374. return -EINVAL;
  375. }
  376. /* Any FLL configuration change requires that the FLL be
  377. * disabled first. */
  378. reg1 = snd_soc_read(codec, WM8993_FLL_CONTROL_1);
  379. reg1 &= ~WM8993_FLL_ENA;
  380. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  381. /* Apply the configuration */
  382. if (fll_div.k)
  383. reg1 |= WM8993_FLL_FRAC_MASK;
  384. else
  385. reg1 &= ~WM8993_FLL_FRAC_MASK;
  386. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  387. snd_soc_write(codec, WM8993_FLL_CONTROL_2,
  388. (fll_div.fll_outdiv << WM8993_FLL_OUTDIV_SHIFT) |
  389. (fll_div.fll_fratio << WM8993_FLL_FRATIO_SHIFT));
  390. snd_soc_write(codec, WM8993_FLL_CONTROL_3, fll_div.k);
  391. reg4 = snd_soc_read(codec, WM8993_FLL_CONTROL_4);
  392. reg4 &= ~WM8993_FLL_N_MASK;
  393. reg4 |= fll_div.n << WM8993_FLL_N_SHIFT;
  394. snd_soc_write(codec, WM8993_FLL_CONTROL_4, reg4);
  395. reg5 &= ~WM8993_FLL_CLK_REF_DIV_MASK;
  396. reg5 |= fll_div.fll_clk_ref_div << WM8993_FLL_CLK_REF_DIV_SHIFT;
  397. snd_soc_write(codec, WM8993_FLL_CONTROL_5, reg5);
  398. /* Enable the FLL */
  399. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1 | WM8993_FLL_ENA);
  400. dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
  401. wm8993->fll_fref = Fref;
  402. wm8993->fll_fout = Fout;
  403. wm8993->fll_src = source;
  404. return 0;
  405. }
  406. static int wm8993_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  407. unsigned int Fref, unsigned int Fout)
  408. {
  409. return _wm8993_set_fll(dai->codec, fll_id, source, Fref, Fout);
  410. }
  411. static int configure_clock(struct snd_soc_codec *codec)
  412. {
  413. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  414. unsigned int reg;
  415. /* This should be done on init() for bypass paths */
  416. switch (wm8993->sysclk_source) {
  417. case WM8993_SYSCLK_MCLK:
  418. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8993->mclk_rate);
  419. reg = snd_soc_read(codec, WM8993_CLOCKING_2);
  420. reg &= ~(WM8993_MCLK_DIV | WM8993_SYSCLK_SRC);
  421. if (wm8993->mclk_rate > 13500000) {
  422. reg |= WM8993_MCLK_DIV;
  423. wm8993->sysclk_rate = wm8993->mclk_rate / 2;
  424. } else {
  425. reg &= ~WM8993_MCLK_DIV;
  426. wm8993->sysclk_rate = wm8993->mclk_rate;
  427. }
  428. snd_soc_write(codec, WM8993_CLOCKING_2, reg);
  429. break;
  430. case WM8993_SYSCLK_FLL:
  431. dev_dbg(codec->dev, "Using %dHz FLL clock\n",
  432. wm8993->fll_fout);
  433. reg = snd_soc_read(codec, WM8993_CLOCKING_2);
  434. reg |= WM8993_SYSCLK_SRC;
  435. if (wm8993->fll_fout > 13500000) {
  436. reg |= WM8993_MCLK_DIV;
  437. wm8993->sysclk_rate = wm8993->fll_fout / 2;
  438. } else {
  439. reg &= ~WM8993_MCLK_DIV;
  440. wm8993->sysclk_rate = wm8993->fll_fout;
  441. }
  442. snd_soc_write(codec, WM8993_CLOCKING_2, reg);
  443. break;
  444. default:
  445. dev_err(codec->dev, "System clock not configured\n");
  446. return -EINVAL;
  447. }
  448. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8993->sysclk_rate);
  449. return 0;
  450. }
  451. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  452. static const DECLARE_TLV_DB_SCALE(drc_comp_threash, -4500, 75, 0);
  453. static const DECLARE_TLV_DB_SCALE(drc_comp_amp, -2250, 75, 0);
  454. static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
  455. static const unsigned int drc_max_tlv[] = {
  456. TLV_DB_RANGE_HEAD(4),
  457. 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
  458. 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0),
  459. };
  460. static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
  461. static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -1800, 300, 0);
  462. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  463. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  464. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  465. static const char *dac_deemph_text[] = {
  466. "None",
  467. "32kHz",
  468. "44.1kHz",
  469. "48kHz",
  470. };
  471. static const struct soc_enum dac_deemph =
  472. SOC_ENUM_SINGLE(WM8993_DAC_CTRL, 4, 4, dac_deemph_text);
  473. static const char *adc_hpf_text[] = {
  474. "Hi-Fi",
  475. "Voice 1",
  476. "Voice 2",
  477. "Voice 3",
  478. };
  479. static const struct soc_enum adc_hpf =
  480. SOC_ENUM_SINGLE(WM8993_ADC_CTRL, 5, 4, adc_hpf_text);
  481. static const char *drc_path_text[] = {
  482. "ADC",
  483. "DAC"
  484. };
  485. static const struct soc_enum drc_path =
  486. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 14, 2, drc_path_text);
  487. static const char *drc_r0_text[] = {
  488. "1",
  489. "1/2",
  490. "1/4",
  491. "1/8",
  492. "1/16",
  493. "0",
  494. };
  495. static const struct soc_enum drc_r0 =
  496. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 8, 6, drc_r0_text);
  497. static const char *drc_r1_text[] = {
  498. "1",
  499. "1/2",
  500. "1/4",
  501. "1/8",
  502. "0",
  503. };
  504. static const struct soc_enum drc_r1 =
  505. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_4, 13, 5, drc_r1_text);
  506. static const char *drc_attack_text[] = {
  507. "Reserved",
  508. "181us",
  509. "363us",
  510. "726us",
  511. "1.45ms",
  512. "2.9ms",
  513. "5.8ms",
  514. "11.6ms",
  515. "23.2ms",
  516. "46.4ms",
  517. "92.8ms",
  518. "185.6ms",
  519. };
  520. static const struct soc_enum drc_attack =
  521. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 12, 12, drc_attack_text);
  522. static const char *drc_decay_text[] = {
  523. "186ms",
  524. "372ms",
  525. "743ms",
  526. "1.49s",
  527. "2.97ms",
  528. "5.94ms",
  529. "11.89ms",
  530. "23.78ms",
  531. "47.56ms",
  532. };
  533. static const struct soc_enum drc_decay =
  534. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 8, 9, drc_decay_text);
  535. static const char *drc_ff_text[] = {
  536. "5 samples",
  537. "9 samples",
  538. };
  539. static const struct soc_enum drc_ff =
  540. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 7, 2, drc_ff_text);
  541. static const char *drc_qr_rate_text[] = {
  542. "0.725ms",
  543. "1.45ms",
  544. "5.8ms",
  545. };
  546. static const struct soc_enum drc_qr_rate =
  547. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 0, 3, drc_qr_rate_text);
  548. static const char *drc_smooth_text[] = {
  549. "Low",
  550. "Medium",
  551. "High",
  552. };
  553. static const struct soc_enum drc_smooth =
  554. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 4, 3, drc_smooth_text);
  555. static const struct snd_kcontrol_new wm8993_snd_controls[] = {
  556. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8993_DIGITAL_SIDE_TONE,
  557. 5, 9, 12, 0, sidetone_tlv),
  558. SOC_SINGLE("DRC Switch", WM8993_DRC_CONTROL_1, 15, 1, 0),
  559. SOC_ENUM("DRC Path", drc_path),
  560. SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8993_DRC_CONTROL_2,
  561. 2, 60, 1, drc_comp_threash),
  562. SOC_SINGLE_TLV("DRC Compressor Amplitude Volume", WM8993_DRC_CONTROL_3,
  563. 11, 30, 1, drc_comp_amp),
  564. SOC_ENUM("DRC R0", drc_r0),
  565. SOC_ENUM("DRC R1", drc_r1),
  566. SOC_SINGLE_TLV("DRC Minimum Volume", WM8993_DRC_CONTROL_1, 2, 3, 1,
  567. drc_min_tlv),
  568. SOC_SINGLE_TLV("DRC Maximum Volume", WM8993_DRC_CONTROL_1, 0, 3, 0,
  569. drc_max_tlv),
  570. SOC_ENUM("DRC Attack Rate", drc_attack),
  571. SOC_ENUM("DRC Decay Rate", drc_decay),
  572. SOC_ENUM("DRC FF Delay", drc_ff),
  573. SOC_SINGLE("DRC Anti-clip Switch", WM8993_DRC_CONTROL_1, 9, 1, 0),
  574. SOC_SINGLE("DRC Quick Release Switch", WM8993_DRC_CONTROL_1, 10, 1, 0),
  575. SOC_SINGLE_TLV("DRC Quick Release Volume", WM8993_DRC_CONTROL_3, 2, 3, 0,
  576. drc_qr_tlv),
  577. SOC_ENUM("DRC Quick Release Rate", drc_qr_rate),
  578. SOC_SINGLE("DRC Smoothing Switch", WM8993_DRC_CONTROL_1, 11, 1, 0),
  579. SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8993_DRC_CONTROL_1, 8, 1, 0),
  580. SOC_ENUM("DRC Smoothing Hysteresis Threshold", drc_smooth),
  581. SOC_SINGLE_TLV("DRC Startup Volume", WM8993_DRC_CONTROL_4, 8, 18, 0,
  582. drc_startup_tlv),
  583. SOC_SINGLE("EQ Switch", WM8993_EQ1, 0, 1, 0),
  584. SOC_DOUBLE_R_TLV("Capture Volume", WM8993_LEFT_ADC_DIGITAL_VOLUME,
  585. WM8993_RIGHT_ADC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
  586. SOC_SINGLE("ADC High Pass Filter Switch", WM8993_ADC_CTRL, 8, 1, 0),
  587. SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
  588. SOC_DOUBLE_R_TLV("Playback Volume", WM8993_LEFT_DAC_DIGITAL_VOLUME,
  589. WM8993_RIGHT_DAC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
  590. SOC_SINGLE_TLV("Playback Boost Volume", WM8993_AUDIO_INTERFACE_2, 10, 3, 0,
  591. dac_boost_tlv),
  592. SOC_ENUM("DAC Deemphasis", dac_deemph),
  593. SOC_SINGLE_TLV("SPKL DAC Volume", WM8993_SPKMIXL_ATTENUATION,
  594. 2, 1, 1, wm_hubs_spkmix_tlv),
  595. SOC_SINGLE_TLV("SPKR DAC Volume", WM8993_SPKMIXR_ATTENUATION,
  596. 2, 1, 1, wm_hubs_spkmix_tlv),
  597. };
  598. static const struct snd_kcontrol_new wm8993_eq_controls[] = {
  599. SOC_SINGLE_TLV("EQ1 Volume", WM8993_EQ2, 0, 24, 0, eq_tlv),
  600. SOC_SINGLE_TLV("EQ2 Volume", WM8993_EQ3, 0, 24, 0, eq_tlv),
  601. SOC_SINGLE_TLV("EQ3 Volume", WM8993_EQ4, 0, 24, 0, eq_tlv),
  602. SOC_SINGLE_TLV("EQ4 Volume", WM8993_EQ5, 0, 24, 0, eq_tlv),
  603. SOC_SINGLE_TLV("EQ5 Volume", WM8993_EQ6, 0, 24, 0, eq_tlv),
  604. };
  605. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  606. struct snd_kcontrol *kcontrol, int event)
  607. {
  608. struct snd_soc_codec *codec = w->codec;
  609. switch (event) {
  610. case SND_SOC_DAPM_PRE_PMU:
  611. return configure_clock(codec);
  612. case SND_SOC_DAPM_POST_PMD:
  613. break;
  614. }
  615. return 0;
  616. }
  617. /*
  618. * When used with DAC outputs only the WM8993 charge pump supports
  619. * operation in class W mode, providing very low power consumption
  620. * when used with digital sources. Enable and disable this mode
  621. * automatically depending on the mixer configuration.
  622. *
  623. * Currently the only supported paths are the direct DAC->headphone
  624. * paths (which provide minimum power consumption anyway).
  625. */
  626. static int class_w_put(struct snd_kcontrol *kcontrol,
  627. struct snd_ctl_elem_value *ucontrol)
  628. {
  629. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  630. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  631. struct snd_soc_codec *codec = widget->codec;
  632. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  633. int ret;
  634. /* Turn it off if we're using the main output mixer */
  635. if (ucontrol->value.integer.value[0] == 0) {
  636. if (wm8993->class_w_users == 0) {
  637. dev_dbg(codec->dev, "Disabling Class W\n");
  638. snd_soc_update_bits(codec, WM8993_CLASS_W_0,
  639. WM8993_CP_DYN_FREQ |
  640. WM8993_CP_DYN_V,
  641. 0);
  642. }
  643. wm8993->class_w_users++;
  644. wm8993->hubs_data.class_w = true;
  645. }
  646. /* Implement the change */
  647. ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  648. /* Enable it if we're using the direct DAC path */
  649. if (ucontrol->value.integer.value[0] == 1) {
  650. if (wm8993->class_w_users == 1) {
  651. dev_dbg(codec->dev, "Enabling Class W\n");
  652. snd_soc_update_bits(codec, WM8993_CLASS_W_0,
  653. WM8993_CP_DYN_FREQ |
  654. WM8993_CP_DYN_V,
  655. WM8993_CP_DYN_FREQ |
  656. WM8993_CP_DYN_V);
  657. }
  658. wm8993->class_w_users--;
  659. wm8993->hubs_data.class_w = false;
  660. }
  661. dev_dbg(codec->dev, "Indirect DAC use count now %d\n",
  662. wm8993->class_w_users);
  663. return ret;
  664. }
  665. #define SOC_DAPM_ENUM_W(xname, xenum) \
  666. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  667. .info = snd_soc_info_enum_double, \
  668. .get = snd_soc_dapm_get_enum_double, \
  669. .put = class_w_put, \
  670. .private_value = (unsigned long)&xenum }
  671. static const char *hp_mux_text[] = {
  672. "Mixer",
  673. "DAC",
  674. };
  675. static const struct soc_enum hpl_enum =
  676. SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER1, 8, 2, hp_mux_text);
  677. static const struct snd_kcontrol_new hpl_mux =
  678. SOC_DAPM_ENUM_W("Left Headphone Mux", hpl_enum);
  679. static const struct soc_enum hpr_enum =
  680. SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER2, 8, 2, hp_mux_text);
  681. static const struct snd_kcontrol_new hpr_mux =
  682. SOC_DAPM_ENUM_W("Right Headphone Mux", hpr_enum);
  683. static const struct snd_kcontrol_new left_speaker_mixer[] = {
  684. SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 7, 1, 0),
  685. SOC_DAPM_SINGLE("IN1LP Switch", WM8993_SPEAKER_MIXER, 5, 1, 0),
  686. SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 3, 1, 0),
  687. SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
  688. };
  689. static const struct snd_kcontrol_new right_speaker_mixer[] = {
  690. SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
  691. SOC_DAPM_SINGLE("IN1RP Switch", WM8993_SPEAKER_MIXER, 4, 1, 0),
  692. SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 2, 1, 0),
  693. SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 0, 1, 0),
  694. };
  695. static const char *aif_text[] = {
  696. "Left", "Right"
  697. };
  698. static const struct soc_enum aifoutl_enum =
  699. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 15, 2, aif_text);
  700. static const struct snd_kcontrol_new aifoutl_mux =
  701. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  702. static const struct soc_enum aifoutr_enum =
  703. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 14, 2, aif_text);
  704. static const struct snd_kcontrol_new aifoutr_mux =
  705. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  706. static const struct soc_enum aifinl_enum =
  707. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 15, 2, aif_text);
  708. static const struct snd_kcontrol_new aifinl_mux =
  709. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  710. static const struct soc_enum aifinr_enum =
  711. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 14, 2, aif_text);
  712. static const struct snd_kcontrol_new aifinr_mux =
  713. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  714. static const char *sidetone_text[] = {
  715. "None", "Left", "Right"
  716. };
  717. static const struct soc_enum sidetonel_enum =
  718. SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 2, 3, sidetone_text);
  719. static const struct snd_kcontrol_new sidetonel_mux =
  720. SOC_DAPM_ENUM("Left Sidetone", sidetonel_enum);
  721. static const struct soc_enum sidetoner_enum =
  722. SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 0, 3, sidetone_text);
  723. static const struct snd_kcontrol_new sidetoner_mux =
  724. SOC_DAPM_ENUM("Right Sidetone", sidetoner_enum);
  725. static const struct snd_soc_dapm_widget wm8993_dapm_widgets[] = {
  726. SND_SOC_DAPM_SUPPLY("CLK_SYS", WM8993_BUS_CONTROL_1, 1, 0, clk_sys_event,
  727. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  728. SND_SOC_DAPM_SUPPLY("TOCLK", WM8993_CLOCKING_1, 14, 0, NULL, 0),
  729. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8993_CLOCKING_3, 0, 0, NULL, 0),
  730. SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, NULL, 0),
  731. SND_SOC_DAPM_ADC("ADCL", NULL, WM8993_POWER_MANAGEMENT_2, 1, 0),
  732. SND_SOC_DAPM_ADC("ADCR", NULL, WM8993_POWER_MANAGEMENT_2, 0, 0),
  733. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  734. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  735. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  736. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  737. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  738. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  739. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  740. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  741. SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &sidetonel_mux),
  742. SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &sidetoner_mux),
  743. SND_SOC_DAPM_DAC("DACL", NULL, WM8993_POWER_MANAGEMENT_3, 1, 0),
  744. SND_SOC_DAPM_DAC("DACR", NULL, WM8993_POWER_MANAGEMENT_3, 0, 0),
  745. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  746. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  747. SND_SOC_DAPM_MIXER("SPKL", WM8993_POWER_MANAGEMENT_3, 8, 0,
  748. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
  749. SND_SOC_DAPM_MIXER("SPKR", WM8993_POWER_MANAGEMENT_3, 9, 0,
  750. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
  751. SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
  752. };
  753. static const struct snd_soc_dapm_route routes[] = {
  754. { "MICBIAS1", NULL, "VMID" },
  755. { "MICBIAS2", NULL, "VMID" },
  756. { "ADCL", NULL, "CLK_SYS" },
  757. { "ADCL", NULL, "CLK_DSP" },
  758. { "ADCR", NULL, "CLK_SYS" },
  759. { "ADCR", NULL, "CLK_DSP" },
  760. { "AIFOUTL Mux", "Left", "ADCL" },
  761. { "AIFOUTL Mux", "Right", "ADCR" },
  762. { "AIFOUTR Mux", "Left", "ADCL" },
  763. { "AIFOUTR Mux", "Right", "ADCR" },
  764. { "AIFOUTL", NULL, "AIFOUTL Mux" },
  765. { "AIFOUTR", NULL, "AIFOUTR Mux" },
  766. { "DACL Mux", "Left", "AIFINL" },
  767. { "DACL Mux", "Right", "AIFINR" },
  768. { "DACR Mux", "Left", "AIFINL" },
  769. { "DACR Mux", "Right", "AIFINR" },
  770. { "DACL Sidetone", "Left", "ADCL" },
  771. { "DACL Sidetone", "Right", "ADCR" },
  772. { "DACR Sidetone", "Left", "ADCL" },
  773. { "DACR Sidetone", "Right", "ADCR" },
  774. { "DACL", NULL, "CLK_SYS" },
  775. { "DACL", NULL, "CLK_DSP" },
  776. { "DACL", NULL, "DACL Mux" },
  777. { "DACL", NULL, "DACL Sidetone" },
  778. { "DACR", NULL, "CLK_SYS" },
  779. { "DACR", NULL, "CLK_DSP" },
  780. { "DACR", NULL, "DACR Mux" },
  781. { "DACR", NULL, "DACR Sidetone" },
  782. { "Left Output Mixer", "DAC Switch", "DACL" },
  783. { "Right Output Mixer", "DAC Switch", "DACR" },
  784. { "Left Output PGA", NULL, "CLK_SYS" },
  785. { "Right Output PGA", NULL, "CLK_SYS" },
  786. { "SPKL", "DAC Switch", "DACL" },
  787. { "SPKL", NULL, "CLK_SYS" },
  788. { "SPKR", "DAC Switch", "DACR" },
  789. { "SPKR", NULL, "CLK_SYS" },
  790. { "Left Headphone Mux", "DAC", "DACL" },
  791. { "Right Headphone Mux", "DAC", "DACR" },
  792. };
  793. static void wm8993_cache_restore(struct snd_soc_codec *codec)
  794. {
  795. u16 *cache = codec->reg_cache;
  796. int i;
  797. if (!codec->cache_sync)
  798. return;
  799. /* Reenable hardware writes */
  800. codec->cache_only = 0;
  801. /* Restore the register settings */
  802. for (i = 1; i < WM8993_MAX_REGISTER; i++) {
  803. if (cache[i] == wm8993_reg_defaults[i])
  804. continue;
  805. snd_soc_write(codec, i, cache[i]);
  806. }
  807. /* We're in sync again */
  808. codec->cache_sync = 0;
  809. }
  810. static int wm8993_set_bias_level(struct snd_soc_codec *codec,
  811. enum snd_soc_bias_level level)
  812. {
  813. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  814. int ret;
  815. switch (level) {
  816. case SND_SOC_BIAS_ON:
  817. case SND_SOC_BIAS_PREPARE:
  818. /* VMID=2*40k */
  819. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  820. WM8993_VMID_SEL_MASK, 0x2);
  821. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
  822. WM8993_TSHUT_ENA, WM8993_TSHUT_ENA);
  823. break;
  824. case SND_SOC_BIAS_STANDBY:
  825. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  826. ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
  827. wm8993->supplies);
  828. if (ret != 0)
  829. return ret;
  830. wm8993_cache_restore(codec);
  831. /* Tune DC servo configuration */
  832. snd_soc_write(codec, 0x44, 3);
  833. snd_soc_write(codec, 0x56, 3);
  834. snd_soc_write(codec, 0x44, 0);
  835. /* Bring up VMID with fast soft start */
  836. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  837. WM8993_STARTUP_BIAS_ENA |
  838. WM8993_VMID_BUF_ENA |
  839. WM8993_VMID_RAMP_MASK |
  840. WM8993_BIAS_SRC,
  841. WM8993_STARTUP_BIAS_ENA |
  842. WM8993_VMID_BUF_ENA |
  843. WM8993_VMID_RAMP_MASK |
  844. WM8993_BIAS_SRC);
  845. /* If either line output is single ended we
  846. * need the VMID buffer */
  847. if (!wm8993->pdata.lineout1_diff ||
  848. !wm8993->pdata.lineout2_diff)
  849. snd_soc_update_bits(codec, WM8993_ANTIPOP1,
  850. WM8993_LINEOUT_VMID_BUF_ENA,
  851. WM8993_LINEOUT_VMID_BUF_ENA);
  852. /* VMID=2*40k */
  853. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  854. WM8993_VMID_SEL_MASK |
  855. WM8993_BIAS_ENA,
  856. WM8993_BIAS_ENA | 0x2);
  857. msleep(32);
  858. /* Switch to normal bias */
  859. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  860. WM8993_BIAS_SRC |
  861. WM8993_STARTUP_BIAS_ENA, 0);
  862. }
  863. /* VMID=2*240k */
  864. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  865. WM8993_VMID_SEL_MASK, 0x4);
  866. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
  867. WM8993_TSHUT_ENA, 0);
  868. break;
  869. case SND_SOC_BIAS_OFF:
  870. snd_soc_update_bits(codec, WM8993_ANTIPOP1,
  871. WM8993_LINEOUT_VMID_BUF_ENA, 0);
  872. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  873. WM8993_VMID_SEL_MASK | WM8993_BIAS_ENA,
  874. 0);
  875. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  876. WM8993_STARTUP_BIAS_ENA |
  877. WM8993_VMID_BUF_ENA |
  878. WM8993_VMID_RAMP_MASK |
  879. WM8993_BIAS_SRC, 0);
  880. #ifdef CONFIG_REGULATOR
  881. /* Post 2.6.34 we will be able to get a callback when
  882. * the regulators are disabled which we can use but
  883. * for now just assume that the power will be cut if
  884. * the regulator API is in use.
  885. */
  886. codec->cache_sync = 1;
  887. #endif
  888. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies),
  889. wm8993->supplies);
  890. break;
  891. }
  892. codec->dapm.bias_level = level;
  893. return 0;
  894. }
  895. static int wm8993_set_sysclk(struct snd_soc_dai *codec_dai,
  896. int clk_id, unsigned int freq, int dir)
  897. {
  898. struct snd_soc_codec *codec = codec_dai->codec;
  899. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  900. switch (clk_id) {
  901. case WM8993_SYSCLK_MCLK:
  902. wm8993->mclk_rate = freq;
  903. case WM8993_SYSCLK_FLL:
  904. wm8993->sysclk_source = clk_id;
  905. break;
  906. default:
  907. return -EINVAL;
  908. }
  909. return 0;
  910. }
  911. static int wm8993_set_dai_fmt(struct snd_soc_dai *dai,
  912. unsigned int fmt)
  913. {
  914. struct snd_soc_codec *codec = dai->codec;
  915. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  916. unsigned int aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
  917. unsigned int aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
  918. aif1 &= ~(WM8993_BCLK_DIR | WM8993_AIF_BCLK_INV |
  919. WM8993_AIF_LRCLK_INV | WM8993_AIF_FMT_MASK);
  920. aif4 &= ~WM8993_LRCLK_DIR;
  921. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  922. case SND_SOC_DAIFMT_CBS_CFS:
  923. wm8993->master = 0;
  924. break;
  925. case SND_SOC_DAIFMT_CBS_CFM:
  926. aif4 |= WM8993_LRCLK_DIR;
  927. wm8993->master = 1;
  928. break;
  929. case SND_SOC_DAIFMT_CBM_CFS:
  930. aif1 |= WM8993_BCLK_DIR;
  931. wm8993->master = 1;
  932. break;
  933. case SND_SOC_DAIFMT_CBM_CFM:
  934. aif1 |= WM8993_BCLK_DIR;
  935. aif4 |= WM8993_LRCLK_DIR;
  936. wm8993->master = 1;
  937. break;
  938. default:
  939. return -EINVAL;
  940. }
  941. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  942. case SND_SOC_DAIFMT_DSP_B:
  943. aif1 |= WM8993_AIF_LRCLK_INV;
  944. case SND_SOC_DAIFMT_DSP_A:
  945. aif1 |= 0x18;
  946. break;
  947. case SND_SOC_DAIFMT_I2S:
  948. aif1 |= 0x10;
  949. break;
  950. case SND_SOC_DAIFMT_RIGHT_J:
  951. break;
  952. case SND_SOC_DAIFMT_LEFT_J:
  953. aif1 |= 0x8;
  954. break;
  955. default:
  956. return -EINVAL;
  957. }
  958. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  959. case SND_SOC_DAIFMT_DSP_A:
  960. case SND_SOC_DAIFMT_DSP_B:
  961. /* frame inversion not valid for DSP modes */
  962. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  963. case SND_SOC_DAIFMT_NB_NF:
  964. break;
  965. case SND_SOC_DAIFMT_IB_NF:
  966. aif1 |= WM8993_AIF_BCLK_INV;
  967. break;
  968. default:
  969. return -EINVAL;
  970. }
  971. break;
  972. case SND_SOC_DAIFMT_I2S:
  973. case SND_SOC_DAIFMT_RIGHT_J:
  974. case SND_SOC_DAIFMT_LEFT_J:
  975. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  976. case SND_SOC_DAIFMT_NB_NF:
  977. break;
  978. case SND_SOC_DAIFMT_IB_IF:
  979. aif1 |= WM8993_AIF_BCLK_INV | WM8993_AIF_LRCLK_INV;
  980. break;
  981. case SND_SOC_DAIFMT_IB_NF:
  982. aif1 |= WM8993_AIF_BCLK_INV;
  983. break;
  984. case SND_SOC_DAIFMT_NB_IF:
  985. aif1 |= WM8993_AIF_LRCLK_INV;
  986. break;
  987. default:
  988. return -EINVAL;
  989. }
  990. break;
  991. default:
  992. return -EINVAL;
  993. }
  994. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
  995. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
  996. return 0;
  997. }
  998. static int wm8993_hw_params(struct snd_pcm_substream *substream,
  999. struct snd_pcm_hw_params *params,
  1000. struct snd_soc_dai *dai)
  1001. {
  1002. struct snd_soc_codec *codec = dai->codec;
  1003. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1004. int ret, i, best, best_val, cur_val;
  1005. unsigned int clocking1, clocking3, aif1, aif4;
  1006. clocking1 = snd_soc_read(codec, WM8993_CLOCKING_1);
  1007. clocking1 &= ~WM8993_BCLK_DIV_MASK;
  1008. clocking3 = snd_soc_read(codec, WM8993_CLOCKING_3);
  1009. clocking3 &= ~(WM8993_CLK_SYS_RATE_MASK | WM8993_SAMPLE_RATE_MASK);
  1010. aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
  1011. aif1 &= ~WM8993_AIF_WL_MASK;
  1012. aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
  1013. aif4 &= ~WM8993_LRCLK_RATE_MASK;
  1014. /* What BCLK do we need? */
  1015. wm8993->fs = params_rate(params);
  1016. wm8993->bclk = 2 * wm8993->fs;
  1017. if (wm8993->tdm_slots) {
  1018. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  1019. wm8993->tdm_slots, wm8993->tdm_width);
  1020. wm8993->bclk *= wm8993->tdm_width * wm8993->tdm_slots;
  1021. } else {
  1022. switch (params_format(params)) {
  1023. case SNDRV_PCM_FORMAT_S16_LE:
  1024. wm8993->bclk *= 16;
  1025. break;
  1026. case SNDRV_PCM_FORMAT_S20_3LE:
  1027. wm8993->bclk *= 20;
  1028. aif1 |= 0x8;
  1029. break;
  1030. case SNDRV_PCM_FORMAT_S24_LE:
  1031. wm8993->bclk *= 24;
  1032. aif1 |= 0x10;
  1033. break;
  1034. case SNDRV_PCM_FORMAT_S32_LE:
  1035. wm8993->bclk *= 32;
  1036. aif1 |= 0x18;
  1037. break;
  1038. default:
  1039. return -EINVAL;
  1040. }
  1041. }
  1042. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8993->bclk);
  1043. ret = configure_clock(codec);
  1044. if (ret != 0)
  1045. return ret;
  1046. /* Select nearest CLK_SYS_RATE */
  1047. best = 0;
  1048. best_val = abs((wm8993->sysclk_rate / clk_sys_rates[0].ratio)
  1049. - wm8993->fs);
  1050. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1051. cur_val = abs((wm8993->sysclk_rate /
  1052. clk_sys_rates[i].ratio) - wm8993->fs);
  1053. if (cur_val < best_val) {
  1054. best = i;
  1055. best_val = cur_val;
  1056. }
  1057. }
  1058. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1059. clk_sys_rates[best].ratio);
  1060. clocking3 |= (clk_sys_rates[best].clk_sys_rate
  1061. << WM8993_CLK_SYS_RATE_SHIFT);
  1062. /* SAMPLE_RATE */
  1063. best = 0;
  1064. best_val = abs(wm8993->fs - sample_rates[0].rate);
  1065. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1066. /* Closest match */
  1067. cur_val = abs(wm8993->fs - sample_rates[i].rate);
  1068. if (cur_val < best_val) {
  1069. best = i;
  1070. best_val = cur_val;
  1071. }
  1072. }
  1073. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1074. sample_rates[best].rate);
  1075. clocking3 |= (sample_rates[best].sample_rate
  1076. << WM8993_SAMPLE_RATE_SHIFT);
  1077. /* BCLK_DIV */
  1078. best = 0;
  1079. best_val = INT_MAX;
  1080. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1081. cur_val = ((wm8993->sysclk_rate * 10) / bclk_divs[i].div)
  1082. - wm8993->bclk;
  1083. if (cur_val < 0) /* Table is sorted */
  1084. break;
  1085. if (cur_val < best_val) {
  1086. best = i;
  1087. best_val = cur_val;
  1088. }
  1089. }
  1090. wm8993->bclk = (wm8993->sysclk_rate * 10) / bclk_divs[best].div;
  1091. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1092. bclk_divs[best].div, wm8993->bclk);
  1093. clocking1 |= bclk_divs[best].bclk_div << WM8993_BCLK_DIV_SHIFT;
  1094. /* LRCLK is a simple fraction of BCLK */
  1095. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8993->bclk / wm8993->fs);
  1096. aif4 |= wm8993->bclk / wm8993->fs;
  1097. snd_soc_write(codec, WM8993_CLOCKING_1, clocking1);
  1098. snd_soc_write(codec, WM8993_CLOCKING_3, clocking3);
  1099. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
  1100. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
  1101. /* ReTune Mobile? */
  1102. if (wm8993->pdata.num_retune_configs) {
  1103. u16 eq1 = snd_soc_read(codec, WM8993_EQ1);
  1104. struct wm8993_retune_mobile_setting *s;
  1105. best = 0;
  1106. best_val = abs(wm8993->pdata.retune_configs[0].rate
  1107. - wm8993->fs);
  1108. for (i = 0; i < wm8993->pdata.num_retune_configs; i++) {
  1109. cur_val = abs(wm8993->pdata.retune_configs[i].rate
  1110. - wm8993->fs);
  1111. if (cur_val < best_val) {
  1112. best_val = cur_val;
  1113. best = i;
  1114. }
  1115. }
  1116. s = &wm8993->pdata.retune_configs[best];
  1117. dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
  1118. s->name, s->rate);
  1119. /* Disable EQ while we reconfigure */
  1120. snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, 0);
  1121. for (i = 1; i < ARRAY_SIZE(s->config); i++)
  1122. snd_soc_write(codec, WM8993_EQ1 + i, s->config[i]);
  1123. snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, eq1);
  1124. }
  1125. return 0;
  1126. }
  1127. static int wm8993_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1128. {
  1129. struct snd_soc_codec *codec = codec_dai->codec;
  1130. unsigned int reg;
  1131. reg = snd_soc_read(codec, WM8993_DAC_CTRL);
  1132. if (mute)
  1133. reg |= WM8993_DAC_MUTE;
  1134. else
  1135. reg &= ~WM8993_DAC_MUTE;
  1136. snd_soc_write(codec, WM8993_DAC_CTRL, reg);
  1137. return 0;
  1138. }
  1139. static int wm8993_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1140. unsigned int rx_mask, int slots, int slot_width)
  1141. {
  1142. struct snd_soc_codec *codec = dai->codec;
  1143. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1144. int aif1 = 0;
  1145. int aif2 = 0;
  1146. /* Don't need to validate anything if we're turning off TDM */
  1147. if (slots == 0) {
  1148. wm8993->tdm_slots = 0;
  1149. goto out;
  1150. }
  1151. /* Note that we allow configurations we can't handle ourselves -
  1152. * for example, we can generate clocks for slots 2 and up even if
  1153. * we can't use those slots ourselves.
  1154. */
  1155. aif1 |= WM8993_AIFADC_TDM;
  1156. aif2 |= WM8993_AIFDAC_TDM;
  1157. switch (rx_mask) {
  1158. case 3:
  1159. break;
  1160. case 0xc:
  1161. aif1 |= WM8993_AIFADC_TDM_CHAN;
  1162. break;
  1163. default:
  1164. return -EINVAL;
  1165. }
  1166. switch (tx_mask) {
  1167. case 3:
  1168. break;
  1169. case 0xc:
  1170. aif2 |= WM8993_AIFDAC_TDM_CHAN;
  1171. break;
  1172. default:
  1173. return -EINVAL;
  1174. }
  1175. out:
  1176. wm8993->tdm_width = slot_width;
  1177. wm8993->tdm_slots = slots / 2;
  1178. snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_1,
  1179. WM8993_AIFADC_TDM | WM8993_AIFADC_TDM_CHAN, aif1);
  1180. snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_2,
  1181. WM8993_AIFDAC_TDM | WM8993_AIFDAC_TDM_CHAN, aif2);
  1182. return 0;
  1183. }
  1184. static struct snd_soc_dai_ops wm8993_ops = {
  1185. .set_sysclk = wm8993_set_sysclk,
  1186. .set_fmt = wm8993_set_dai_fmt,
  1187. .hw_params = wm8993_hw_params,
  1188. .digital_mute = wm8993_digital_mute,
  1189. .set_pll = wm8993_set_fll,
  1190. .set_tdm_slot = wm8993_set_tdm_slot,
  1191. };
  1192. #define WM8993_RATES SNDRV_PCM_RATE_8000_48000
  1193. #define WM8993_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  1194. SNDRV_PCM_FMTBIT_S20_3LE |\
  1195. SNDRV_PCM_FMTBIT_S24_LE |\
  1196. SNDRV_PCM_FMTBIT_S32_LE)
  1197. static struct snd_soc_dai_driver wm8993_dai = {
  1198. .name = "wm8993-hifi",
  1199. .playback = {
  1200. .stream_name = "Playback",
  1201. .channels_min = 1,
  1202. .channels_max = 2,
  1203. .rates = WM8993_RATES,
  1204. .formats = WM8993_FORMATS,
  1205. },
  1206. .capture = {
  1207. .stream_name = "Capture",
  1208. .channels_min = 1,
  1209. .channels_max = 2,
  1210. .rates = WM8993_RATES,
  1211. .formats = WM8993_FORMATS,
  1212. },
  1213. .ops = &wm8993_ops,
  1214. .symmetric_rates = 1,
  1215. };
  1216. static int wm8993_probe(struct snd_soc_codec *codec)
  1217. {
  1218. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1219. struct snd_soc_dapm_context *dapm = &codec->dapm;
  1220. int ret, i, val;
  1221. wm8993->hubs_data.hp_startup_mode = 1;
  1222. wm8993->hubs_data.dcs_codes_l = -2;
  1223. wm8993->hubs_data.dcs_codes_r = -2;
  1224. wm8993->hubs_data.series_startup = 1;
  1225. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  1226. if (ret != 0) {
  1227. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1228. return ret;
  1229. }
  1230. for (i = 0; i < ARRAY_SIZE(wm8993->supplies); i++)
  1231. wm8993->supplies[i].supply = wm8993_supply_names[i];
  1232. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8993->supplies),
  1233. wm8993->supplies);
  1234. if (ret != 0) {
  1235. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1236. return ret;
  1237. }
  1238. ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
  1239. wm8993->supplies);
  1240. if (ret != 0) {
  1241. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  1242. goto err_get;
  1243. }
  1244. val = snd_soc_read(codec, WM8993_SOFTWARE_RESET);
  1245. if (val != wm8993_reg_defaults[WM8993_SOFTWARE_RESET]) {
  1246. dev_err(codec->dev, "Invalid ID register value %x\n", val);
  1247. ret = -EINVAL;
  1248. goto err_enable;
  1249. }
  1250. ret = snd_soc_write(codec, WM8993_SOFTWARE_RESET, 0xffff);
  1251. if (ret != 0)
  1252. goto err_enable;
  1253. codec->cache_only = 1;
  1254. /* By default we're using the output mixers */
  1255. wm8993->class_w_users = 2;
  1256. /* Latch volume update bits and default ZC on */
  1257. snd_soc_update_bits(codec, WM8993_RIGHT_DAC_DIGITAL_VOLUME,
  1258. WM8993_DAC_VU, WM8993_DAC_VU);
  1259. snd_soc_update_bits(codec, WM8993_RIGHT_ADC_DIGITAL_VOLUME,
  1260. WM8993_ADC_VU, WM8993_ADC_VU);
  1261. /* Manualy manage the HPOUT sequencing for independent stereo
  1262. * control. */
  1263. snd_soc_update_bits(codec, WM8993_ANALOGUE_HP_0,
  1264. WM8993_HPOUT1_AUTO_PU, 0);
  1265. /* Use automatic clock configuration */
  1266. snd_soc_update_bits(codec, WM8993_CLOCKING_4, WM8993_SR_MODE, 0);
  1267. wm_hubs_handle_analogue_pdata(codec, wm8993->pdata.lineout1_diff,
  1268. wm8993->pdata.lineout2_diff,
  1269. wm8993->pdata.lineout1fb,
  1270. wm8993->pdata.lineout2fb,
  1271. wm8993->pdata.jd_scthr,
  1272. wm8993->pdata.jd_thr,
  1273. wm8993->pdata.micbias1_lvl,
  1274. wm8993->pdata.micbias2_lvl);
  1275. ret = wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1276. if (ret != 0)
  1277. goto err_enable;
  1278. snd_soc_add_controls(codec, wm8993_snd_controls,
  1279. ARRAY_SIZE(wm8993_snd_controls));
  1280. if (wm8993->pdata.num_retune_configs != 0) {
  1281. dev_dbg(codec->dev, "Using ReTune Mobile\n");
  1282. } else {
  1283. dev_dbg(codec->dev, "No ReTune Mobile, using normal EQ\n");
  1284. snd_soc_add_controls(codec, wm8993_eq_controls,
  1285. ARRAY_SIZE(wm8993_eq_controls));
  1286. }
  1287. snd_soc_dapm_new_controls(dapm, wm8993_dapm_widgets,
  1288. ARRAY_SIZE(wm8993_dapm_widgets));
  1289. wm_hubs_add_analogue_controls(codec);
  1290. snd_soc_dapm_add_routes(dapm, routes, ARRAY_SIZE(routes));
  1291. wm_hubs_add_analogue_routes(codec, wm8993->pdata.lineout1_diff,
  1292. wm8993->pdata.lineout2_diff);
  1293. return 0;
  1294. err_enable:
  1295. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1296. err_get:
  1297. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1298. return ret;
  1299. }
  1300. static int wm8993_remove(struct snd_soc_codec *codec)
  1301. {
  1302. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1303. wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1304. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1305. return 0;
  1306. }
  1307. #ifdef CONFIG_PM
  1308. static int wm8993_suspend(struct snd_soc_codec *codec, pm_message_t state)
  1309. {
  1310. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1311. int fll_fout = wm8993->fll_fout;
  1312. int fll_fref = wm8993->fll_fref;
  1313. int ret;
  1314. /* Stop the FLL in an orderly fashion */
  1315. ret = _wm8993_set_fll(codec, 0, 0, 0, 0);
  1316. if (ret != 0) {
  1317. dev_err(codec->dev, "Failed to stop FLL\n");
  1318. return ret;
  1319. }
  1320. wm8993->fll_fout = fll_fout;
  1321. wm8993->fll_fref = fll_fref;
  1322. wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1323. return 0;
  1324. }
  1325. static int wm8993_resume(struct snd_soc_codec *codec)
  1326. {
  1327. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1328. int ret;
  1329. wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1330. /* Restart the FLL? */
  1331. if (wm8993->fll_fout) {
  1332. int fll_fout = wm8993->fll_fout;
  1333. int fll_fref = wm8993->fll_fref;
  1334. wm8993->fll_fref = 0;
  1335. wm8993->fll_fout = 0;
  1336. ret = _wm8993_set_fll(codec, 0, wm8993->fll_src,
  1337. fll_fref, fll_fout);
  1338. if (ret != 0)
  1339. dev_err(codec->dev, "Failed to restart FLL\n");
  1340. }
  1341. return 0;
  1342. }
  1343. #else
  1344. #define wm8993_suspend NULL
  1345. #define wm8993_resume NULL
  1346. #endif
  1347. static struct snd_soc_codec_driver soc_codec_dev_wm8993 = {
  1348. .probe = wm8993_probe,
  1349. .remove = wm8993_remove,
  1350. .suspend = wm8993_suspend,
  1351. .resume = wm8993_resume,
  1352. .set_bias_level = wm8993_set_bias_level,
  1353. .reg_cache_size = ARRAY_SIZE(wm8993_reg_defaults),
  1354. .reg_word_size = sizeof(u16),
  1355. .reg_cache_default = wm8993_reg_defaults,
  1356. .volatile_register = wm8993_volatile,
  1357. };
  1358. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1359. static __devinit int wm8993_i2c_probe(struct i2c_client *i2c,
  1360. const struct i2c_device_id *id)
  1361. {
  1362. struct wm8993_priv *wm8993;
  1363. int ret;
  1364. wm8993 = kzalloc(sizeof(struct wm8993_priv), GFP_KERNEL);
  1365. if (wm8993 == NULL)
  1366. return -ENOMEM;
  1367. i2c_set_clientdata(i2c, wm8993);
  1368. ret = snd_soc_register_codec(&i2c->dev,
  1369. &soc_codec_dev_wm8993, &wm8993_dai, 1);
  1370. if (ret < 0)
  1371. kfree(wm8993);
  1372. return ret;
  1373. }
  1374. static __devexit int wm8993_i2c_remove(struct i2c_client *client)
  1375. {
  1376. snd_soc_unregister_codec(&client->dev);
  1377. kfree(i2c_get_clientdata(client));
  1378. return 0;
  1379. }
  1380. static const struct i2c_device_id wm8993_i2c_id[] = {
  1381. { "wm8993", 0 },
  1382. { }
  1383. };
  1384. MODULE_DEVICE_TABLE(i2c, wm8993_i2c_id);
  1385. static struct i2c_driver wm8993_i2c_driver = {
  1386. .driver = {
  1387. .name = "wm8993-codec",
  1388. .owner = THIS_MODULE,
  1389. },
  1390. .probe = wm8993_i2c_probe,
  1391. .remove = __devexit_p(wm8993_i2c_remove),
  1392. .id_table = wm8993_i2c_id,
  1393. };
  1394. #endif
  1395. static int __init wm8993_modinit(void)
  1396. {
  1397. int ret = 0;
  1398. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1399. ret = i2c_add_driver(&wm8993_i2c_driver);
  1400. if (ret != 0) {
  1401. pr_err("WM8993: Unable to register I2C driver: %d\n",
  1402. ret);
  1403. }
  1404. #endif
  1405. return ret;
  1406. }
  1407. module_init(wm8993_modinit);
  1408. static void __exit wm8993_exit(void)
  1409. {
  1410. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1411. i2c_del_driver(&wm8993_i2c_driver);
  1412. #endif
  1413. }
  1414. module_exit(wm8993_exit);
  1415. MODULE_DESCRIPTION("ASoC WM8993 driver");
  1416. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1417. MODULE_LICENSE("GPL");