wm8993.c 43 KB

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