wm8993.c 44 KB

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