wm8993.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  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 <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/tlv.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.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. u16 reg_cache[WM8993_REGISTER_COUNT];
  221. struct regulator_bulk_data supplies[WM8993_NUM_SUPPLIES];
  222. struct wm8993_platform_data pdata;
  223. struct snd_soc_codec codec;
  224. int master;
  225. int sysclk_source;
  226. int tdm_slots;
  227. int tdm_width;
  228. unsigned int mclk_rate;
  229. unsigned int sysclk_rate;
  230. unsigned int fs;
  231. unsigned int bclk;
  232. int class_w_users;
  233. unsigned int fll_fref;
  234. unsigned int fll_fout;
  235. int fll_src;
  236. };
  237. static int wm8993_volatile(unsigned int reg)
  238. {
  239. switch (reg) {
  240. case WM8993_SOFTWARE_RESET:
  241. case WM8993_DC_SERVO_0:
  242. case WM8993_DC_SERVO_READBACK_0:
  243. case WM8993_DC_SERVO_READBACK_1:
  244. case WM8993_DC_SERVO_READBACK_2:
  245. return 1;
  246. default:
  247. return 0;
  248. }
  249. }
  250. struct _fll_div {
  251. u16 fll_fratio;
  252. u16 fll_outdiv;
  253. u16 fll_clk_ref_div;
  254. u16 n;
  255. u16 k;
  256. };
  257. /* The size in bits of the FLL divide multiplied by 10
  258. * to allow rounding later */
  259. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  260. static struct {
  261. unsigned int min;
  262. unsigned int max;
  263. u16 fll_fratio;
  264. int ratio;
  265. } fll_fratios[] = {
  266. { 0, 64000, 4, 16 },
  267. { 64000, 128000, 3, 8 },
  268. { 128000, 256000, 2, 4 },
  269. { 256000, 1000000, 1, 2 },
  270. { 1000000, 13500000, 0, 1 },
  271. };
  272. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  273. unsigned int Fout)
  274. {
  275. u64 Kpart;
  276. unsigned int K, Ndiv, Nmod, target;
  277. unsigned int div;
  278. int i;
  279. /* Fref must be <=13.5MHz */
  280. div = 1;
  281. fll_div->fll_clk_ref_div = 0;
  282. while ((Fref / div) > 13500000) {
  283. div *= 2;
  284. fll_div->fll_clk_ref_div++;
  285. if (div > 8) {
  286. pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
  287. Fref);
  288. return -EINVAL;
  289. }
  290. }
  291. pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
  292. /* Apply the division for our remaining calculations */
  293. Fref /= div;
  294. /* Fvco should be 90-100MHz; don't check the upper bound */
  295. div = 0;
  296. target = Fout * 2;
  297. while (target < 90000000) {
  298. div++;
  299. target *= 2;
  300. if (div > 7) {
  301. pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
  302. Fout);
  303. return -EINVAL;
  304. }
  305. }
  306. fll_div->fll_outdiv = div;
  307. pr_debug("Fvco=%dHz\n", target);
  308. /* Find an appropraite FLL_FRATIO and factor it out of the target */
  309. for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
  310. if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
  311. fll_div->fll_fratio = fll_fratios[i].fll_fratio;
  312. target /= fll_fratios[i].ratio;
  313. break;
  314. }
  315. }
  316. if (i == ARRAY_SIZE(fll_fratios)) {
  317. pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
  318. return -EINVAL;
  319. }
  320. /* Now, calculate N.K */
  321. Ndiv = target / Fref;
  322. fll_div->n = Ndiv;
  323. Nmod = target % Fref;
  324. pr_debug("Nmod=%d\n", Nmod);
  325. /* Calculate fractional part - scale up so we can round. */
  326. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  327. do_div(Kpart, Fref);
  328. K = Kpart & 0xFFFFFFFF;
  329. if ((K % 10) >= 5)
  330. K += 5;
  331. /* Move down to proper range now rounding is done */
  332. fll_div->k = K / 10;
  333. pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
  334. fll_div->n, fll_div->k,
  335. fll_div->fll_fratio, fll_div->fll_outdiv,
  336. fll_div->fll_clk_ref_div);
  337. return 0;
  338. }
  339. static int wm8993_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  340. unsigned int Fref, unsigned int Fout)
  341. {
  342. struct snd_soc_codec *codec = dai->codec;
  343. struct wm8993_priv *wm8993 = codec->private_data;
  344. u16 reg1, reg4, reg5;
  345. struct _fll_div fll_div;
  346. int ret;
  347. /* Any change? */
  348. if (Fref == wm8993->fll_fref && Fout == wm8993->fll_fout)
  349. return 0;
  350. /* Disable the FLL */
  351. if (Fout == 0) {
  352. dev_dbg(codec->dev, "FLL disabled\n");
  353. wm8993->fll_fref = 0;
  354. wm8993->fll_fout = 0;
  355. reg1 = snd_soc_read(codec, WM8993_FLL_CONTROL_1);
  356. reg1 &= ~WM8993_FLL_ENA;
  357. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  358. return 0;
  359. }
  360. ret = fll_factors(&fll_div, Fref, Fout);
  361. if (ret != 0)
  362. return ret;
  363. reg5 = snd_soc_read(codec, WM8993_FLL_CONTROL_5);
  364. reg5 &= ~WM8993_FLL_CLK_SRC_MASK;
  365. switch (fll_id) {
  366. case WM8993_FLL_MCLK:
  367. break;
  368. case WM8993_FLL_LRCLK:
  369. reg5 |= 1;
  370. break;
  371. case WM8993_FLL_BCLK:
  372. reg5 |= 2;
  373. break;
  374. default:
  375. dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
  376. return -EINVAL;
  377. }
  378. /* Any FLL configuration change requires that the FLL be
  379. * disabled first. */
  380. reg1 = snd_soc_read(codec, WM8993_FLL_CONTROL_1);
  381. reg1 &= ~WM8993_FLL_ENA;
  382. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  383. /* Apply the configuration */
  384. if (fll_div.k)
  385. reg1 |= WM8993_FLL_FRAC_MASK;
  386. else
  387. reg1 &= ~WM8993_FLL_FRAC_MASK;
  388. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1);
  389. snd_soc_write(codec, WM8993_FLL_CONTROL_2,
  390. (fll_div.fll_outdiv << WM8993_FLL_OUTDIV_SHIFT) |
  391. (fll_div.fll_fratio << WM8993_FLL_FRATIO_SHIFT));
  392. snd_soc_write(codec, WM8993_FLL_CONTROL_3, fll_div.k);
  393. reg4 = snd_soc_read(codec, WM8993_FLL_CONTROL_4);
  394. reg4 &= ~WM8993_FLL_N_MASK;
  395. reg4 |= fll_div.n << WM8993_FLL_N_SHIFT;
  396. snd_soc_write(codec, WM8993_FLL_CONTROL_4, reg4);
  397. reg5 &= ~WM8993_FLL_CLK_REF_DIV_MASK;
  398. reg5 |= fll_div.fll_clk_ref_div << WM8993_FLL_CLK_REF_DIV_SHIFT;
  399. snd_soc_write(codec, WM8993_FLL_CONTROL_5, reg5);
  400. /* Enable the FLL */
  401. snd_soc_write(codec, WM8993_FLL_CONTROL_1, reg1 | WM8993_FLL_ENA);
  402. dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
  403. wm8993->fll_fref = Fref;
  404. wm8993->fll_fout = Fout;
  405. wm8993->fll_src = source;
  406. return 0;
  407. }
  408. static int configure_clock(struct snd_soc_codec *codec)
  409. {
  410. struct wm8993_priv *wm8993 = codec->private_data;
  411. unsigned int reg;
  412. /* This should be done on init() for bypass paths */
  413. switch (wm8993->sysclk_source) {
  414. case WM8993_SYSCLK_MCLK:
  415. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8993->mclk_rate);
  416. reg = snd_soc_read(codec, WM8993_CLOCKING_2);
  417. reg &= ~(WM8993_MCLK_DIV | WM8993_SYSCLK_SRC);
  418. if (wm8993->mclk_rate > 13500000) {
  419. reg |= WM8993_MCLK_DIV;
  420. wm8993->sysclk_rate = wm8993->mclk_rate / 2;
  421. } else {
  422. reg &= ~WM8993_MCLK_DIV;
  423. wm8993->sysclk_rate = wm8993->mclk_rate;
  424. }
  425. snd_soc_write(codec, WM8993_CLOCKING_2, reg);
  426. break;
  427. case WM8993_SYSCLK_FLL:
  428. dev_dbg(codec->dev, "Using %dHz FLL clock\n",
  429. wm8993->fll_fout);
  430. reg = snd_soc_read(codec, WM8993_CLOCKING_2);
  431. reg |= WM8993_SYSCLK_SRC;
  432. if (wm8993->fll_fout > 13500000) {
  433. reg |= WM8993_MCLK_DIV;
  434. wm8993->sysclk_rate = wm8993->fll_fout / 2;
  435. } else {
  436. reg &= ~WM8993_MCLK_DIV;
  437. wm8993->sysclk_rate = wm8993->fll_fout;
  438. }
  439. snd_soc_write(codec, WM8993_CLOCKING_2, reg);
  440. break;
  441. default:
  442. dev_err(codec->dev, "System clock not configured\n");
  443. return -EINVAL;
  444. }
  445. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8993->sysclk_rate);
  446. return 0;
  447. }
  448. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  449. static const DECLARE_TLV_DB_SCALE(drc_comp_threash, -4500, 75, 0);
  450. static const DECLARE_TLV_DB_SCALE(drc_comp_amp, -2250, 75, 0);
  451. static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
  452. static const unsigned int drc_max_tlv[] = {
  453. TLV_DB_RANGE_HEAD(4),
  454. 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
  455. 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0),
  456. };
  457. static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
  458. static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -1800, 300, 0);
  459. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  460. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  461. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  462. static const char *dac_deemph_text[] = {
  463. "None",
  464. "32kHz",
  465. "44.1kHz",
  466. "48kHz",
  467. };
  468. static const struct soc_enum dac_deemph =
  469. SOC_ENUM_SINGLE(WM8993_DAC_CTRL, 4, 4, dac_deemph_text);
  470. static const char *adc_hpf_text[] = {
  471. "Hi-Fi",
  472. "Voice 1",
  473. "Voice 2",
  474. "Voice 3",
  475. };
  476. static const struct soc_enum adc_hpf =
  477. SOC_ENUM_SINGLE(WM8993_ADC_CTRL, 5, 4, adc_hpf_text);
  478. static const char *drc_path_text[] = {
  479. "ADC",
  480. "DAC"
  481. };
  482. static const struct soc_enum drc_path =
  483. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 14, 2, drc_path_text);
  484. static const char *drc_r0_text[] = {
  485. "1",
  486. "1/2",
  487. "1/4",
  488. "1/8",
  489. "1/16",
  490. "0",
  491. };
  492. static const struct soc_enum drc_r0 =
  493. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 8, 6, drc_r0_text);
  494. static const char *drc_r1_text[] = {
  495. "1",
  496. "1/2",
  497. "1/4",
  498. "1/8",
  499. "0",
  500. };
  501. static const struct soc_enum drc_r1 =
  502. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_4, 13, 5, drc_r1_text);
  503. static const char *drc_attack_text[] = {
  504. "Reserved",
  505. "181us",
  506. "363us",
  507. "726us",
  508. "1.45ms",
  509. "2.9ms",
  510. "5.8ms",
  511. "11.6ms",
  512. "23.2ms",
  513. "46.4ms",
  514. "92.8ms",
  515. "185.6ms",
  516. };
  517. static const struct soc_enum drc_attack =
  518. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 12, 12, drc_attack_text);
  519. static const char *drc_decay_text[] = {
  520. "186ms",
  521. "372ms",
  522. "743ms",
  523. "1.49s",
  524. "2.97ms",
  525. "5.94ms",
  526. "11.89ms",
  527. "23.78ms",
  528. "47.56ms",
  529. };
  530. static const struct soc_enum drc_decay =
  531. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 8, 9, drc_decay_text);
  532. static const char *drc_ff_text[] = {
  533. "5 samples",
  534. "9 samples",
  535. };
  536. static const struct soc_enum drc_ff =
  537. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 7, 2, drc_ff_text);
  538. static const char *drc_qr_rate_text[] = {
  539. "0.725ms",
  540. "1.45ms",
  541. "5.8ms",
  542. };
  543. static const struct soc_enum drc_qr_rate =
  544. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 0, 3, drc_qr_rate_text);
  545. static const char *drc_smooth_text[] = {
  546. "Low",
  547. "Medium",
  548. "High",
  549. };
  550. static const struct soc_enum drc_smooth =
  551. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 4, 3, drc_smooth_text);
  552. static const struct snd_kcontrol_new wm8993_snd_controls[] = {
  553. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8993_DIGITAL_SIDE_TONE,
  554. 5, 9, 12, 0, sidetone_tlv),
  555. SOC_SINGLE("DRC Switch", WM8993_DRC_CONTROL_1, 15, 1, 0),
  556. SOC_ENUM("DRC Path", drc_path),
  557. SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8993_DRC_CONTROL_2,
  558. 2, 60, 1, drc_comp_threash),
  559. SOC_SINGLE_TLV("DRC Compressor Amplitude Volume", WM8993_DRC_CONTROL_3,
  560. 11, 30, 1, drc_comp_amp),
  561. SOC_ENUM("DRC R0", drc_r0),
  562. SOC_ENUM("DRC R1", drc_r1),
  563. SOC_SINGLE_TLV("DRC Minimum Volume", WM8993_DRC_CONTROL_1, 2, 3, 1,
  564. drc_min_tlv),
  565. SOC_SINGLE_TLV("DRC Maximum Volume", WM8993_DRC_CONTROL_1, 0, 3, 0,
  566. drc_max_tlv),
  567. SOC_ENUM("DRC Attack Rate", drc_attack),
  568. SOC_ENUM("DRC Decay Rate", drc_decay),
  569. SOC_ENUM("DRC FF Delay", drc_ff),
  570. SOC_SINGLE("DRC Anti-clip Switch", WM8993_DRC_CONTROL_1, 9, 1, 0),
  571. SOC_SINGLE("DRC Quick Release Switch", WM8993_DRC_CONTROL_1, 10, 1, 0),
  572. SOC_SINGLE_TLV("DRC Quick Release Volume", WM8993_DRC_CONTROL_3, 2, 3, 0,
  573. drc_qr_tlv),
  574. SOC_ENUM("DRC Quick Release Rate", drc_qr_rate),
  575. SOC_SINGLE("DRC Smoothing Switch", WM8993_DRC_CONTROL_1, 11, 1, 0),
  576. SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8993_DRC_CONTROL_1, 8, 1, 0),
  577. SOC_ENUM("DRC Smoothing Hysteresis Threshold", drc_smooth),
  578. SOC_SINGLE_TLV("DRC Startup Volume", WM8993_DRC_CONTROL_4, 8, 18, 0,
  579. drc_startup_tlv),
  580. SOC_SINGLE("EQ Switch", WM8993_EQ1, 0, 1, 0),
  581. SOC_DOUBLE_R_TLV("Capture Volume", WM8993_LEFT_ADC_DIGITAL_VOLUME,
  582. WM8993_RIGHT_ADC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
  583. SOC_SINGLE("ADC High Pass Filter Switch", WM8993_ADC_CTRL, 8, 1, 0),
  584. SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
  585. SOC_DOUBLE_R_TLV("Playback Volume", WM8993_LEFT_DAC_DIGITAL_VOLUME,
  586. WM8993_RIGHT_DAC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
  587. SOC_SINGLE_TLV("Playback Boost Volume", WM8993_AUDIO_INTERFACE_2, 10, 3, 0,
  588. dac_boost_tlv),
  589. SOC_ENUM("DAC Deemphasis", dac_deemph),
  590. SOC_SINGLE_TLV("SPKL DAC Volume", WM8993_SPKMIXL_ATTENUATION,
  591. 2, 1, 1, wm_hubs_spkmix_tlv),
  592. SOC_SINGLE_TLV("SPKR DAC Volume", WM8993_SPKMIXR_ATTENUATION,
  593. 2, 1, 1, wm_hubs_spkmix_tlv),
  594. };
  595. static const struct snd_kcontrol_new wm8993_eq_controls[] = {
  596. SOC_SINGLE_TLV("EQ1 Volume", WM8993_EQ2, 0, 24, 0, eq_tlv),
  597. SOC_SINGLE_TLV("EQ2 Volume", WM8993_EQ3, 0, 24, 0, eq_tlv),
  598. SOC_SINGLE_TLV("EQ3 Volume", WM8993_EQ4, 0, 24, 0, eq_tlv),
  599. SOC_SINGLE_TLV("EQ4 Volume", WM8993_EQ5, 0, 24, 0, eq_tlv),
  600. SOC_SINGLE_TLV("EQ5 Volume", WM8993_EQ6, 0, 24, 0, eq_tlv),
  601. };
  602. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  603. struct snd_kcontrol *kcontrol, int event)
  604. {
  605. struct snd_soc_codec *codec = w->codec;
  606. switch (event) {
  607. case SND_SOC_DAPM_PRE_PMU:
  608. return configure_clock(codec);
  609. case SND_SOC_DAPM_POST_PMD:
  610. break;
  611. }
  612. return 0;
  613. }
  614. /*
  615. * When used with DAC outputs only the WM8993 charge pump supports
  616. * operation in class W mode, providing very low power consumption
  617. * when used with digital sources. Enable and disable this mode
  618. * automatically depending on the mixer configuration.
  619. *
  620. * Currently the only supported paths are the direct DAC->headphone
  621. * paths (which provide minimum power consumption anyway).
  622. */
  623. static int class_w_put(struct snd_kcontrol *kcontrol,
  624. struct snd_ctl_elem_value *ucontrol)
  625. {
  626. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  627. struct snd_soc_codec *codec = widget->codec;
  628. struct wm8993_priv *wm8993 = codec->private_data;
  629. int ret;
  630. /* Turn it off if we're using the main output mixer */
  631. if (ucontrol->value.integer.value[0] == 0) {
  632. if (wm8993->class_w_users == 0) {
  633. dev_dbg(codec->dev, "Disabling Class W\n");
  634. snd_soc_update_bits(codec, WM8993_CLASS_W_0,
  635. WM8993_CP_DYN_FREQ |
  636. WM8993_CP_DYN_V,
  637. 0);
  638. }
  639. wm8993->class_w_users++;
  640. }
  641. /* Implement the change */
  642. ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  643. /* Enable it if we're using the direct DAC path */
  644. if (ucontrol->value.integer.value[0] == 1) {
  645. if (wm8993->class_w_users == 1) {
  646. dev_dbg(codec->dev, "Enabling Class W\n");
  647. snd_soc_update_bits(codec, WM8993_CLASS_W_0,
  648. WM8993_CP_DYN_FREQ |
  649. WM8993_CP_DYN_V,
  650. WM8993_CP_DYN_FREQ |
  651. WM8993_CP_DYN_V);
  652. }
  653. wm8993->class_w_users--;
  654. }
  655. dev_dbg(codec->dev, "Indirect DAC use count now %d\n",
  656. wm8993->class_w_users);
  657. return ret;
  658. }
  659. #define SOC_DAPM_ENUM_W(xname, xenum) \
  660. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  661. .info = snd_soc_info_enum_double, \
  662. .get = snd_soc_dapm_get_enum_double, \
  663. .put = class_w_put, \
  664. .private_value = (unsigned long)&xenum }
  665. static const char *hp_mux_text[] = {
  666. "Mixer",
  667. "DAC",
  668. };
  669. static const struct soc_enum hpl_enum =
  670. SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER1, 8, 2, hp_mux_text);
  671. static const struct snd_kcontrol_new hpl_mux =
  672. SOC_DAPM_ENUM_W("Left Headphone Mux", hpl_enum);
  673. static const struct soc_enum hpr_enum =
  674. SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER2, 8, 2, hp_mux_text);
  675. static const struct snd_kcontrol_new hpr_mux =
  676. SOC_DAPM_ENUM_W("Right Headphone Mux", hpr_enum);
  677. static const struct snd_kcontrol_new left_speaker_mixer[] = {
  678. SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 7, 1, 0),
  679. SOC_DAPM_SINGLE("IN1LP Switch", WM8993_SPEAKER_MIXER, 5, 1, 0),
  680. SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 3, 1, 0),
  681. SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
  682. };
  683. static const struct snd_kcontrol_new right_speaker_mixer[] = {
  684. SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
  685. SOC_DAPM_SINGLE("IN1RP Switch", WM8993_SPEAKER_MIXER, 4, 1, 0),
  686. SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 2, 1, 0),
  687. SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 0, 1, 0),
  688. };
  689. static const char *aif_text[] = {
  690. "Left", "Right"
  691. };
  692. static const struct soc_enum aifoutl_enum =
  693. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 15, 2, aif_text);
  694. static const struct snd_kcontrol_new aifoutl_mux =
  695. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  696. static const struct soc_enum aifoutr_enum =
  697. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 14, 2, aif_text);
  698. static const struct snd_kcontrol_new aifoutr_mux =
  699. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  700. static const struct soc_enum aifinl_enum =
  701. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 15, 2, aif_text);
  702. static const struct snd_kcontrol_new aifinl_mux =
  703. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  704. static const struct soc_enum aifinr_enum =
  705. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 14, 2, aif_text);
  706. static const struct snd_kcontrol_new aifinr_mux =
  707. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  708. static const char *sidetone_text[] = {
  709. "None", "Left", "Right"
  710. };
  711. static const struct soc_enum sidetonel_enum =
  712. SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 2, 3, sidetone_text);
  713. static const struct snd_kcontrol_new sidetonel_mux =
  714. SOC_DAPM_ENUM("Left Sidetone", sidetonel_enum);
  715. static const struct soc_enum sidetoner_enum =
  716. SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 0, 3, sidetone_text);
  717. static const struct snd_kcontrol_new sidetoner_mux =
  718. SOC_DAPM_ENUM("Right Sidetone", sidetoner_enum);
  719. static const struct snd_soc_dapm_widget wm8993_dapm_widgets[] = {
  720. SND_SOC_DAPM_SUPPLY("CLK_SYS", WM8993_BUS_CONTROL_1, 1, 0, clk_sys_event,
  721. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  722. SND_SOC_DAPM_SUPPLY("TOCLK", WM8993_CLOCKING_1, 14, 0, NULL, 0),
  723. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8993_CLOCKING_3, 0, 0, NULL, 0),
  724. SND_SOC_DAPM_ADC("ADCL", NULL, WM8993_POWER_MANAGEMENT_2, 1, 0),
  725. SND_SOC_DAPM_ADC("ADCR", NULL, WM8993_POWER_MANAGEMENT_2, 0, 0),
  726. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  727. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  728. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  729. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  730. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  731. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  732. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  733. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  734. SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &sidetonel_mux),
  735. SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &sidetoner_mux),
  736. SND_SOC_DAPM_DAC("DACL", NULL, WM8993_POWER_MANAGEMENT_3, 1, 0),
  737. SND_SOC_DAPM_DAC("DACR", NULL, WM8993_POWER_MANAGEMENT_3, 0, 0),
  738. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  739. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  740. SND_SOC_DAPM_MIXER("SPKL", WM8993_POWER_MANAGEMENT_3, 8, 0,
  741. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
  742. SND_SOC_DAPM_MIXER("SPKR", WM8993_POWER_MANAGEMENT_3, 9, 0,
  743. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
  744. };
  745. static const struct snd_soc_dapm_route routes[] = {
  746. { "ADCL", NULL, "CLK_SYS" },
  747. { "ADCL", NULL, "CLK_DSP" },
  748. { "ADCR", NULL, "CLK_SYS" },
  749. { "ADCR", NULL, "CLK_DSP" },
  750. { "AIFOUTL Mux", "Left", "ADCL" },
  751. { "AIFOUTL Mux", "Right", "ADCR" },
  752. { "AIFOUTR Mux", "Left", "ADCL" },
  753. { "AIFOUTR Mux", "Right", "ADCR" },
  754. { "AIFOUTL", NULL, "AIFOUTL Mux" },
  755. { "AIFOUTR", NULL, "AIFOUTR Mux" },
  756. { "DACL Mux", "Left", "AIFINL" },
  757. { "DACL Mux", "Right", "AIFINR" },
  758. { "DACR Mux", "Left", "AIFINL" },
  759. { "DACR Mux", "Right", "AIFINR" },
  760. { "DACL Sidetone", "Left", "ADCL" },
  761. { "DACL Sidetone", "Right", "ADCR" },
  762. { "DACR Sidetone", "Left", "ADCL" },
  763. { "DACR Sidetone", "Right", "ADCR" },
  764. { "DACL", NULL, "CLK_SYS" },
  765. { "DACL", NULL, "CLK_DSP" },
  766. { "DACL", NULL, "DACL Mux" },
  767. { "DACL", NULL, "DACL Sidetone" },
  768. { "DACR", NULL, "CLK_SYS" },
  769. { "DACR", NULL, "CLK_DSP" },
  770. { "DACR", NULL, "DACR Mux" },
  771. { "DACR", NULL, "DACR Sidetone" },
  772. { "Left Output Mixer", "DAC Switch", "DACL" },
  773. { "Right Output Mixer", "DAC Switch", "DACR" },
  774. { "Left Output PGA", NULL, "CLK_SYS" },
  775. { "Right Output PGA", NULL, "CLK_SYS" },
  776. { "SPKL", "DAC Switch", "DACL" },
  777. { "SPKL", NULL, "CLK_SYS" },
  778. { "SPKR", "DAC Switch", "DACR" },
  779. { "SPKR", NULL, "CLK_SYS" },
  780. { "Left Headphone Mux", "DAC", "DACL" },
  781. { "Right Headphone Mux", "DAC", "DACR" },
  782. };
  783. static int wm8993_set_bias_level(struct snd_soc_codec *codec,
  784. enum snd_soc_bias_level level)
  785. {
  786. struct wm8993_priv *wm8993 = codec->private_data;
  787. switch (level) {
  788. case SND_SOC_BIAS_ON:
  789. case SND_SOC_BIAS_PREPARE:
  790. /* VMID=2*40k */
  791. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  792. WM8993_VMID_SEL_MASK, 0x2);
  793. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
  794. WM8993_TSHUT_ENA, WM8993_TSHUT_ENA);
  795. break;
  796. case SND_SOC_BIAS_STANDBY:
  797. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  798. /* Tune DC servo configuration */
  799. snd_soc_write(codec, 0x44, 3);
  800. snd_soc_write(codec, 0x56, 3);
  801. snd_soc_write(codec, 0x44, 0);
  802. /* Bring up VMID with fast soft start */
  803. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  804. WM8993_STARTUP_BIAS_ENA |
  805. WM8993_VMID_BUF_ENA |
  806. WM8993_VMID_RAMP_MASK |
  807. WM8993_BIAS_SRC,
  808. WM8993_STARTUP_BIAS_ENA |
  809. WM8993_VMID_BUF_ENA |
  810. WM8993_VMID_RAMP_MASK |
  811. WM8993_BIAS_SRC);
  812. /* If either line output is single ended we
  813. * need the VMID buffer */
  814. if (!wm8993->pdata.lineout1_diff ||
  815. !wm8993->pdata.lineout2_diff)
  816. snd_soc_update_bits(codec, WM8993_ANTIPOP1,
  817. WM8993_LINEOUT_VMID_BUF_ENA,
  818. WM8993_LINEOUT_VMID_BUF_ENA);
  819. /* VMID=2*40k */
  820. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  821. WM8993_VMID_SEL_MASK |
  822. WM8993_BIAS_ENA,
  823. WM8993_BIAS_ENA | 0x2);
  824. msleep(32);
  825. /* Switch to normal bias */
  826. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  827. WM8993_BIAS_SRC |
  828. WM8993_STARTUP_BIAS_ENA, 0);
  829. }
  830. /* VMID=2*240k */
  831. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  832. WM8993_VMID_SEL_MASK, 0x4);
  833. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
  834. WM8993_TSHUT_ENA, 0);
  835. break;
  836. case SND_SOC_BIAS_OFF:
  837. snd_soc_update_bits(codec, WM8993_ANTIPOP1,
  838. WM8993_LINEOUT_VMID_BUF_ENA, 0);
  839. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  840. WM8993_VMID_SEL_MASK | WM8993_BIAS_ENA,
  841. 0);
  842. break;
  843. }
  844. codec->bias_level = level;
  845. return 0;
  846. }
  847. static int wm8993_set_sysclk(struct snd_soc_dai *codec_dai,
  848. int clk_id, unsigned int freq, int dir)
  849. {
  850. struct snd_soc_codec *codec = codec_dai->codec;
  851. struct wm8993_priv *wm8993 = codec->private_data;
  852. switch (clk_id) {
  853. case WM8993_SYSCLK_MCLK:
  854. wm8993->mclk_rate = freq;
  855. case WM8993_SYSCLK_FLL:
  856. wm8993->sysclk_source = clk_id;
  857. break;
  858. default:
  859. return -EINVAL;
  860. }
  861. return 0;
  862. }
  863. static int wm8993_set_dai_fmt(struct snd_soc_dai *dai,
  864. unsigned int fmt)
  865. {
  866. struct snd_soc_codec *codec = dai->codec;
  867. struct wm8993_priv *wm8993 = codec->private_data;
  868. unsigned int aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
  869. unsigned int aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
  870. aif1 &= ~(WM8993_BCLK_DIR | WM8993_AIF_BCLK_INV |
  871. WM8993_AIF_LRCLK_INV | WM8993_AIF_FMT_MASK);
  872. aif4 &= ~WM8993_LRCLK_DIR;
  873. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  874. case SND_SOC_DAIFMT_CBS_CFS:
  875. wm8993->master = 0;
  876. break;
  877. case SND_SOC_DAIFMT_CBS_CFM:
  878. aif4 |= WM8993_LRCLK_DIR;
  879. wm8993->master = 1;
  880. break;
  881. case SND_SOC_DAIFMT_CBM_CFS:
  882. aif1 |= WM8993_BCLK_DIR;
  883. wm8993->master = 1;
  884. break;
  885. case SND_SOC_DAIFMT_CBM_CFM:
  886. aif1 |= WM8993_BCLK_DIR;
  887. aif4 |= WM8993_LRCLK_DIR;
  888. wm8993->master = 1;
  889. break;
  890. default:
  891. return -EINVAL;
  892. }
  893. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  894. case SND_SOC_DAIFMT_DSP_B:
  895. aif1 |= WM8993_AIF_LRCLK_INV;
  896. case SND_SOC_DAIFMT_DSP_A:
  897. aif1 |= 0x18;
  898. break;
  899. case SND_SOC_DAIFMT_I2S:
  900. aif1 |= 0x10;
  901. break;
  902. case SND_SOC_DAIFMT_RIGHT_J:
  903. break;
  904. case SND_SOC_DAIFMT_LEFT_J:
  905. aif1 |= 0x8;
  906. break;
  907. default:
  908. return -EINVAL;
  909. }
  910. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  911. case SND_SOC_DAIFMT_DSP_A:
  912. case SND_SOC_DAIFMT_DSP_B:
  913. /* frame inversion not valid for DSP modes */
  914. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  915. case SND_SOC_DAIFMT_NB_NF:
  916. break;
  917. case SND_SOC_DAIFMT_IB_NF:
  918. aif1 |= WM8993_AIF_BCLK_INV;
  919. break;
  920. default:
  921. return -EINVAL;
  922. }
  923. break;
  924. case SND_SOC_DAIFMT_I2S:
  925. case SND_SOC_DAIFMT_RIGHT_J:
  926. case SND_SOC_DAIFMT_LEFT_J:
  927. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  928. case SND_SOC_DAIFMT_NB_NF:
  929. break;
  930. case SND_SOC_DAIFMT_IB_IF:
  931. aif1 |= WM8993_AIF_BCLK_INV | WM8993_AIF_LRCLK_INV;
  932. break;
  933. case SND_SOC_DAIFMT_IB_NF:
  934. aif1 |= WM8993_AIF_BCLK_INV;
  935. break;
  936. case SND_SOC_DAIFMT_NB_IF:
  937. aif1 |= WM8993_AIF_LRCLK_INV;
  938. break;
  939. default:
  940. return -EINVAL;
  941. }
  942. break;
  943. default:
  944. return -EINVAL;
  945. }
  946. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
  947. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
  948. return 0;
  949. }
  950. static int wm8993_hw_params(struct snd_pcm_substream *substream,
  951. struct snd_pcm_hw_params *params,
  952. struct snd_soc_dai *dai)
  953. {
  954. struct snd_soc_codec *codec = dai->codec;
  955. struct wm8993_priv *wm8993 = codec->private_data;
  956. int ret, i, best, best_val, cur_val;
  957. unsigned int clocking1, clocking3, aif1, aif4;
  958. clocking1 = snd_soc_read(codec, WM8993_CLOCKING_1);
  959. clocking1 &= ~WM8993_BCLK_DIV_MASK;
  960. clocking3 = snd_soc_read(codec, WM8993_CLOCKING_3);
  961. clocking3 &= ~(WM8993_CLK_SYS_RATE_MASK | WM8993_SAMPLE_RATE_MASK);
  962. aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
  963. aif1 &= ~WM8993_AIF_WL_MASK;
  964. aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
  965. aif4 &= ~WM8993_LRCLK_RATE_MASK;
  966. /* What BCLK do we need? */
  967. wm8993->fs = params_rate(params);
  968. wm8993->bclk = 2 * wm8993->fs;
  969. if (wm8993->tdm_slots) {
  970. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  971. wm8993->tdm_slots, wm8993->tdm_width);
  972. wm8993->bclk *= wm8993->tdm_width * wm8993->tdm_slots;
  973. } else {
  974. switch (params_format(params)) {
  975. case SNDRV_PCM_FORMAT_S16_LE:
  976. wm8993->bclk *= 16;
  977. break;
  978. case SNDRV_PCM_FORMAT_S20_3LE:
  979. wm8993->bclk *= 20;
  980. aif1 |= 0x8;
  981. break;
  982. case SNDRV_PCM_FORMAT_S24_LE:
  983. wm8993->bclk *= 24;
  984. aif1 |= 0x10;
  985. break;
  986. case SNDRV_PCM_FORMAT_S32_LE:
  987. wm8993->bclk *= 32;
  988. aif1 |= 0x18;
  989. break;
  990. default:
  991. return -EINVAL;
  992. }
  993. }
  994. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8993->bclk);
  995. ret = configure_clock(codec);
  996. if (ret != 0)
  997. return ret;
  998. /* Select nearest CLK_SYS_RATE */
  999. best = 0;
  1000. best_val = abs((wm8993->sysclk_rate / clk_sys_rates[0].ratio)
  1001. - wm8993->fs);
  1002. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1003. cur_val = abs((wm8993->sysclk_rate /
  1004. clk_sys_rates[i].ratio) - wm8993->fs);;
  1005. if (cur_val < best_val) {
  1006. best = i;
  1007. best_val = cur_val;
  1008. }
  1009. }
  1010. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1011. clk_sys_rates[best].ratio);
  1012. clocking3 |= (clk_sys_rates[best].clk_sys_rate
  1013. << WM8993_CLK_SYS_RATE_SHIFT);
  1014. /* SAMPLE_RATE */
  1015. best = 0;
  1016. best_val = abs(wm8993->fs - sample_rates[0].rate);
  1017. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1018. /* Closest match */
  1019. cur_val = abs(wm8993->fs - sample_rates[i].rate);
  1020. if (cur_val < best_val) {
  1021. best = i;
  1022. best_val = cur_val;
  1023. }
  1024. }
  1025. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1026. sample_rates[best].rate);
  1027. clocking3 |= (sample_rates[best].sample_rate
  1028. << WM8993_SAMPLE_RATE_SHIFT);
  1029. /* BCLK_DIV */
  1030. best = 0;
  1031. best_val = INT_MAX;
  1032. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1033. cur_val = ((wm8993->sysclk_rate * 10) / bclk_divs[i].div)
  1034. - wm8993->bclk;
  1035. if (cur_val < 0) /* Table is sorted */
  1036. break;
  1037. if (cur_val < best_val) {
  1038. best = i;
  1039. best_val = cur_val;
  1040. }
  1041. }
  1042. wm8993->bclk = (wm8993->sysclk_rate * 10) / bclk_divs[best].div;
  1043. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1044. bclk_divs[best].div, wm8993->bclk);
  1045. clocking1 |= bclk_divs[best].bclk_div << WM8993_BCLK_DIV_SHIFT;
  1046. /* LRCLK is a simple fraction of BCLK */
  1047. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8993->bclk / wm8993->fs);
  1048. aif4 |= wm8993->bclk / wm8993->fs;
  1049. snd_soc_write(codec, WM8993_CLOCKING_1, clocking1);
  1050. snd_soc_write(codec, WM8993_CLOCKING_3, clocking3);
  1051. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
  1052. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
  1053. /* ReTune Mobile? */
  1054. if (wm8993->pdata.num_retune_configs) {
  1055. u16 eq1 = snd_soc_read(codec, WM8993_EQ1);
  1056. struct wm8993_retune_mobile_setting *s;
  1057. best = 0;
  1058. best_val = abs(wm8993->pdata.retune_configs[0].rate
  1059. - wm8993->fs);
  1060. for (i = 0; i < wm8993->pdata.num_retune_configs; i++) {
  1061. cur_val = abs(wm8993->pdata.retune_configs[i].rate
  1062. - wm8993->fs);
  1063. if (cur_val < best_val) {
  1064. best_val = cur_val;
  1065. best = i;
  1066. }
  1067. }
  1068. s = &wm8993->pdata.retune_configs[best];
  1069. dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
  1070. s->name, s->rate);
  1071. /* Disable EQ while we reconfigure */
  1072. snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, 0);
  1073. for (i = 1; i < ARRAY_SIZE(s->config); i++)
  1074. snd_soc_write(codec, WM8993_EQ1 + i, s->config[i]);
  1075. snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, eq1);
  1076. }
  1077. return 0;
  1078. }
  1079. static int wm8993_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1080. {
  1081. struct snd_soc_codec *codec = codec_dai->codec;
  1082. unsigned int reg;
  1083. reg = snd_soc_read(codec, WM8993_DAC_CTRL);
  1084. if (mute)
  1085. reg |= WM8993_DAC_MUTE;
  1086. else
  1087. reg &= ~WM8993_DAC_MUTE;
  1088. snd_soc_write(codec, WM8993_DAC_CTRL, reg);
  1089. return 0;
  1090. }
  1091. static int wm8993_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1092. unsigned int rx_mask, int slots, int slot_width)
  1093. {
  1094. struct snd_soc_codec *codec = dai->codec;
  1095. struct wm8993_priv *wm8993 = codec->private_data;
  1096. int aif1 = 0;
  1097. int aif2 = 0;
  1098. /* Don't need to validate anything if we're turning off TDM */
  1099. if (slots == 0) {
  1100. wm8993->tdm_slots = 0;
  1101. goto out;
  1102. }
  1103. /* Note that we allow configurations we can't handle ourselves -
  1104. * for example, we can generate clocks for slots 2 and up even if
  1105. * we can't use those slots ourselves.
  1106. */
  1107. aif1 |= WM8993_AIFADC_TDM;
  1108. aif2 |= WM8993_AIFDAC_TDM;
  1109. switch (rx_mask) {
  1110. case 3:
  1111. break;
  1112. case 0xc:
  1113. aif1 |= WM8993_AIFADC_TDM_CHAN;
  1114. break;
  1115. default:
  1116. return -EINVAL;
  1117. }
  1118. switch (tx_mask) {
  1119. case 3:
  1120. break;
  1121. case 0xc:
  1122. aif2 |= WM8993_AIFDAC_TDM_CHAN;
  1123. break;
  1124. default:
  1125. return -EINVAL;
  1126. }
  1127. out:
  1128. wm8993->tdm_width = slot_width;
  1129. wm8993->tdm_slots = slots / 2;
  1130. snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_1,
  1131. WM8993_AIFADC_TDM | WM8993_AIFADC_TDM_CHAN, aif1);
  1132. snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_2,
  1133. WM8993_AIFDAC_TDM | WM8993_AIFDAC_TDM_CHAN, aif2);
  1134. return 0;
  1135. }
  1136. static struct snd_soc_dai_ops wm8993_ops = {
  1137. .set_sysclk = wm8993_set_sysclk,
  1138. .set_fmt = wm8993_set_dai_fmt,
  1139. .hw_params = wm8993_hw_params,
  1140. .digital_mute = wm8993_digital_mute,
  1141. .set_pll = wm8993_set_fll,
  1142. .set_tdm_slot = wm8993_set_tdm_slot,
  1143. };
  1144. #define WM8993_RATES SNDRV_PCM_RATE_8000_48000
  1145. #define WM8993_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  1146. SNDRV_PCM_FMTBIT_S20_3LE |\
  1147. SNDRV_PCM_FMTBIT_S24_LE |\
  1148. SNDRV_PCM_FMTBIT_S32_LE)
  1149. struct snd_soc_dai wm8993_dai = {
  1150. .name = "WM8993",
  1151. .playback = {
  1152. .stream_name = "Playback",
  1153. .channels_min = 1,
  1154. .channels_max = 2,
  1155. .rates = WM8993_RATES,
  1156. .formats = WM8993_FORMATS,
  1157. },
  1158. .capture = {
  1159. .stream_name = "Capture",
  1160. .channels_min = 1,
  1161. .channels_max = 2,
  1162. .rates = WM8993_RATES,
  1163. .formats = WM8993_FORMATS,
  1164. },
  1165. .ops = &wm8993_ops,
  1166. .symmetric_rates = 1,
  1167. };
  1168. EXPORT_SYMBOL_GPL(wm8993_dai);
  1169. static struct snd_soc_codec *wm8993_codec;
  1170. static int wm8993_probe(struct platform_device *pdev)
  1171. {
  1172. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1173. struct snd_soc_codec *codec;
  1174. struct wm8993_priv *wm8993;
  1175. int ret = 0;
  1176. if (!wm8993_codec) {
  1177. dev_err(&pdev->dev, "I2C device not yet probed\n");
  1178. goto err;
  1179. }
  1180. socdev->card->codec = wm8993_codec;
  1181. codec = wm8993_codec;
  1182. wm8993 = codec->private_data;
  1183. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  1184. if (ret < 0) {
  1185. dev_err(codec->dev, "failed to create pcms\n");
  1186. goto err;
  1187. }
  1188. snd_soc_add_controls(codec, wm8993_snd_controls,
  1189. ARRAY_SIZE(wm8993_snd_controls));
  1190. if (wm8993->pdata.num_retune_configs != 0) {
  1191. dev_dbg(codec->dev, "Using ReTune Mobile\n");
  1192. } else {
  1193. dev_dbg(codec->dev, "No ReTune Mobile, using normal EQ\n");
  1194. snd_soc_add_controls(codec, wm8993_eq_controls,
  1195. ARRAY_SIZE(wm8993_eq_controls));
  1196. }
  1197. snd_soc_dapm_new_controls(codec, wm8993_dapm_widgets,
  1198. ARRAY_SIZE(wm8993_dapm_widgets));
  1199. wm_hubs_add_analogue_controls(codec);
  1200. snd_soc_dapm_add_routes(codec, routes, ARRAY_SIZE(routes));
  1201. wm_hubs_add_analogue_routes(codec, wm8993->pdata.lineout1_diff,
  1202. wm8993->pdata.lineout2_diff);
  1203. return ret;
  1204. err:
  1205. return ret;
  1206. }
  1207. static int wm8993_remove(struct platform_device *pdev)
  1208. {
  1209. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1210. snd_soc_free_pcms(socdev);
  1211. snd_soc_dapm_free(socdev);
  1212. return 0;
  1213. }
  1214. #ifdef CONFIG_PM
  1215. static int wm8993_suspend(struct platform_device *pdev, pm_message_t state)
  1216. {
  1217. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1218. struct snd_soc_codec *codec = socdev->card->codec;
  1219. struct wm8993_priv *wm8993 = codec->private_data;
  1220. int fll_fout = wm8993->fll_fout;
  1221. int fll_fref = wm8993->fll_fref;
  1222. int ret;
  1223. /* Stop the FLL in an orderly fashion */
  1224. ret = wm8993_set_fll(codec->dai, 0, 0, 0, 0);
  1225. if (ret != 0) {
  1226. dev_err(&pdev->dev, "Failed to stop FLL\n");
  1227. return ret;
  1228. }
  1229. wm8993->fll_fout = fll_fout;
  1230. wm8993->fll_fref = fll_fref;
  1231. wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1232. return 0;
  1233. }
  1234. static int wm8993_resume(struct platform_device *pdev)
  1235. {
  1236. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1237. struct snd_soc_codec *codec = socdev->card->codec;
  1238. struct wm8993_priv *wm8993 = codec->private_data;
  1239. u16 *cache = wm8993->reg_cache;
  1240. int i, ret;
  1241. /* Restore the register settings */
  1242. for (i = 1; i < WM8993_MAX_REGISTER; i++) {
  1243. if (cache[i] == wm8993_reg_defaults[i])
  1244. continue;
  1245. snd_soc_write(codec, i, cache[i]);
  1246. }
  1247. wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1248. /* Restart the FLL? */
  1249. if (wm8993->fll_fout) {
  1250. int fll_fout = wm8993->fll_fout;
  1251. int fll_fref = wm8993->fll_fref;
  1252. wm8993->fll_fref = 0;
  1253. wm8993->fll_fout = 0;
  1254. ret = wm8993_set_fll(codec->dai, 0, wm8993->fll_src,
  1255. fll_fref, fll_fout);
  1256. if (ret != 0)
  1257. dev_err(codec->dev, "Failed to restart FLL\n");
  1258. }
  1259. return 0;
  1260. }
  1261. #else
  1262. #define wm8993_suspend NULL
  1263. #define wm8993_resume NULL
  1264. #endif
  1265. struct snd_soc_codec_device soc_codec_dev_wm8993 = {
  1266. .probe = wm8993_probe,
  1267. .remove = wm8993_remove,
  1268. .suspend = wm8993_suspend,
  1269. .resume = wm8993_resume,
  1270. };
  1271. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8993);
  1272. static int wm8993_i2c_probe(struct i2c_client *i2c,
  1273. const struct i2c_device_id *id)
  1274. {
  1275. struct wm8993_priv *wm8993;
  1276. struct snd_soc_codec *codec;
  1277. unsigned int val;
  1278. int ret;
  1279. int i;
  1280. if (wm8993_codec) {
  1281. dev_err(&i2c->dev, "A WM8993 is already registered\n");
  1282. return -EINVAL;
  1283. }
  1284. wm8993 = kzalloc(sizeof(struct wm8993_priv), GFP_KERNEL);
  1285. if (wm8993 == NULL)
  1286. return -ENOMEM;
  1287. codec = &wm8993->codec;
  1288. if (i2c->dev.platform_data)
  1289. memcpy(&wm8993->pdata, i2c->dev.platform_data,
  1290. sizeof(wm8993->pdata));
  1291. mutex_init(&codec->mutex);
  1292. INIT_LIST_HEAD(&codec->dapm_widgets);
  1293. INIT_LIST_HEAD(&codec->dapm_paths);
  1294. codec->name = "WM8993";
  1295. codec->volatile_register = wm8993_volatile;
  1296. codec->reg_cache = wm8993->reg_cache;
  1297. codec->reg_cache_size = ARRAY_SIZE(wm8993->reg_cache);
  1298. codec->bias_level = SND_SOC_BIAS_OFF;
  1299. codec->set_bias_level = wm8993_set_bias_level;
  1300. codec->dai = &wm8993_dai;
  1301. codec->num_dai = 1;
  1302. codec->private_data = wm8993;
  1303. wm8993->hubs_data.hp_startup_mode = 1;
  1304. wm8993->hubs_data.dcs_codes = -2;
  1305. memcpy(wm8993->reg_cache, wm8993_reg_defaults,
  1306. sizeof(wm8993->reg_cache));
  1307. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  1308. if (ret != 0) {
  1309. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1310. goto err;
  1311. }
  1312. i2c_set_clientdata(i2c, wm8993);
  1313. codec->control_data = i2c;
  1314. wm8993_codec = codec;
  1315. codec->dev = &i2c->dev;
  1316. for (i = 0; i < ARRAY_SIZE(wm8993->supplies); i++)
  1317. wm8993->supplies[i].supply = wm8993_supply_names[i];
  1318. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8993->supplies),
  1319. wm8993->supplies);
  1320. if (ret != 0) {
  1321. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1322. goto err;
  1323. }
  1324. ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
  1325. wm8993->supplies);
  1326. if (ret != 0) {
  1327. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  1328. goto err_get;
  1329. }
  1330. val = snd_soc_read(codec, WM8993_SOFTWARE_RESET);
  1331. if (val != wm8993_reg_defaults[WM8993_SOFTWARE_RESET]) {
  1332. dev_err(codec->dev, "Invalid ID register value %x\n", val);
  1333. ret = -EINVAL;
  1334. goto err_enable;
  1335. }
  1336. ret = snd_soc_write(codec, WM8993_SOFTWARE_RESET, 0xffff);
  1337. if (ret != 0)
  1338. goto err_enable;
  1339. /* By default we're using the output mixers */
  1340. wm8993->class_w_users = 2;
  1341. /* Latch volume update bits and default ZC on */
  1342. snd_soc_update_bits(codec, WM8993_RIGHT_DAC_DIGITAL_VOLUME,
  1343. WM8993_DAC_VU, WM8993_DAC_VU);
  1344. snd_soc_update_bits(codec, WM8993_RIGHT_ADC_DIGITAL_VOLUME,
  1345. WM8993_ADC_VU, WM8993_ADC_VU);
  1346. /* Manualy manage the HPOUT sequencing for independent stereo
  1347. * control. */
  1348. snd_soc_update_bits(codec, WM8993_ANALOGUE_HP_0,
  1349. WM8993_HPOUT1_AUTO_PU, 0);
  1350. /* Use automatic clock configuration */
  1351. snd_soc_update_bits(codec, WM8993_CLOCKING_4, WM8993_SR_MODE, 0);
  1352. wm_hubs_handle_analogue_pdata(codec, wm8993->pdata.lineout1_diff,
  1353. wm8993->pdata.lineout2_diff,
  1354. wm8993->pdata.lineout1fb,
  1355. wm8993->pdata.lineout2fb,
  1356. wm8993->pdata.jd_scthr,
  1357. wm8993->pdata.jd_thr,
  1358. wm8993->pdata.micbias1_lvl,
  1359. wm8993->pdata.micbias2_lvl);
  1360. ret = wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1361. if (ret != 0)
  1362. goto err_enable;
  1363. wm8993_dai.dev = codec->dev;
  1364. ret = snd_soc_register_dai(&wm8993_dai);
  1365. if (ret != 0)
  1366. goto err_bias;
  1367. ret = snd_soc_register_codec(codec);
  1368. return 0;
  1369. err_bias:
  1370. wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1371. err_enable:
  1372. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1373. err_get:
  1374. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1375. err:
  1376. wm8993_codec = NULL;
  1377. kfree(wm8993);
  1378. return ret;
  1379. }
  1380. static int wm8993_i2c_remove(struct i2c_client *client)
  1381. {
  1382. struct wm8993_priv *wm8993 = i2c_get_clientdata(client);
  1383. snd_soc_unregister_codec(&wm8993->codec);
  1384. snd_soc_unregister_dai(&wm8993_dai);
  1385. wm8993_set_bias_level(&wm8993->codec, SND_SOC_BIAS_OFF);
  1386. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1387. kfree(wm8993);
  1388. return 0;
  1389. }
  1390. static const struct i2c_device_id wm8993_i2c_id[] = {
  1391. { "wm8993", 0 },
  1392. { }
  1393. };
  1394. MODULE_DEVICE_TABLE(i2c, wm8993_i2c_id);
  1395. static struct i2c_driver wm8993_i2c_driver = {
  1396. .driver = {
  1397. .name = "WM8993",
  1398. .owner = THIS_MODULE,
  1399. },
  1400. .probe = wm8993_i2c_probe,
  1401. .remove = wm8993_i2c_remove,
  1402. .id_table = wm8993_i2c_id,
  1403. };
  1404. static int __init wm8993_modinit(void)
  1405. {
  1406. int ret;
  1407. ret = i2c_add_driver(&wm8993_i2c_driver);
  1408. if (ret != 0)
  1409. pr_err("WM8993: Unable to register I2C driver: %d\n", ret);
  1410. return ret;
  1411. }
  1412. module_init(wm8993_modinit);
  1413. static void __exit wm8993_exit(void)
  1414. {
  1415. i2c_del_driver(&wm8993_i2c_driver);
  1416. }
  1417. module_exit(wm8993_exit);
  1418. MODULE_DESCRIPTION("ASoC WM8993 driver");
  1419. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  1420. MODULE_LICENSE("GPL");