wm8993.c 44 KB

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