wm8993.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  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. /* Both overestimates */
  401. if (Fref < 1000000)
  402. msleep(3);
  403. else
  404. msleep(1);
  405. dev_dbg(codec->dev, "FLL enabled at %dHz->%dHz\n", Fref, Fout);
  406. wm8993->fll_fref = Fref;
  407. wm8993->fll_fout = Fout;
  408. wm8993->fll_src = source;
  409. return 0;
  410. }
  411. static int wm8993_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
  412. unsigned int Fref, unsigned int Fout)
  413. {
  414. return _wm8993_set_fll(dai->codec, fll_id, source, Fref, Fout);
  415. }
  416. static int configure_clock(struct snd_soc_codec *codec)
  417. {
  418. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  419. unsigned int reg;
  420. /* This should be done on init() for bypass paths */
  421. switch (wm8993->sysclk_source) {
  422. case WM8993_SYSCLK_MCLK:
  423. dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8993->mclk_rate);
  424. reg = snd_soc_read(codec, WM8993_CLOCKING_2);
  425. reg &= ~(WM8993_MCLK_DIV | WM8993_SYSCLK_SRC);
  426. if (wm8993->mclk_rate > 13500000) {
  427. reg |= WM8993_MCLK_DIV;
  428. wm8993->sysclk_rate = wm8993->mclk_rate / 2;
  429. } else {
  430. reg &= ~WM8993_MCLK_DIV;
  431. wm8993->sysclk_rate = wm8993->mclk_rate;
  432. }
  433. snd_soc_write(codec, WM8993_CLOCKING_2, reg);
  434. break;
  435. case WM8993_SYSCLK_FLL:
  436. dev_dbg(codec->dev, "Using %dHz FLL clock\n",
  437. wm8993->fll_fout);
  438. reg = snd_soc_read(codec, WM8993_CLOCKING_2);
  439. reg |= WM8993_SYSCLK_SRC;
  440. if (wm8993->fll_fout > 13500000) {
  441. reg |= WM8993_MCLK_DIV;
  442. wm8993->sysclk_rate = wm8993->fll_fout / 2;
  443. } else {
  444. reg &= ~WM8993_MCLK_DIV;
  445. wm8993->sysclk_rate = wm8993->fll_fout;
  446. }
  447. snd_soc_write(codec, WM8993_CLOCKING_2, reg);
  448. break;
  449. default:
  450. dev_err(codec->dev, "System clock not configured\n");
  451. return -EINVAL;
  452. }
  453. dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8993->sysclk_rate);
  454. return 0;
  455. }
  456. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  457. static const DECLARE_TLV_DB_SCALE(drc_comp_threash, -4500, 75, 0);
  458. static const DECLARE_TLV_DB_SCALE(drc_comp_amp, -2250, 75, 0);
  459. static const DECLARE_TLV_DB_SCALE(drc_min_tlv, -1800, 600, 0);
  460. static const unsigned int drc_max_tlv[] = {
  461. TLV_DB_RANGE_HEAD(2),
  462. 0, 2, TLV_DB_SCALE_ITEM(1200, 600, 0),
  463. 3, 3, TLV_DB_SCALE_ITEM(3600, 0, 0),
  464. };
  465. static const DECLARE_TLV_DB_SCALE(drc_qr_tlv, 1200, 600, 0);
  466. static const DECLARE_TLV_DB_SCALE(drc_startup_tlv, -1800, 300, 0);
  467. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  468. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  469. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  470. static const char *dac_deemph_text[] = {
  471. "None",
  472. "32kHz",
  473. "44.1kHz",
  474. "48kHz",
  475. };
  476. static const struct soc_enum dac_deemph =
  477. SOC_ENUM_SINGLE(WM8993_DAC_CTRL, 4, 4, dac_deemph_text);
  478. static const char *adc_hpf_text[] = {
  479. "Hi-Fi",
  480. "Voice 1",
  481. "Voice 2",
  482. "Voice 3",
  483. };
  484. static const struct soc_enum adc_hpf =
  485. SOC_ENUM_SINGLE(WM8993_ADC_CTRL, 5, 4, adc_hpf_text);
  486. static const char *drc_path_text[] = {
  487. "ADC",
  488. "DAC"
  489. };
  490. static const struct soc_enum drc_path =
  491. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 14, 2, drc_path_text);
  492. static const char *drc_r0_text[] = {
  493. "1",
  494. "1/2",
  495. "1/4",
  496. "1/8",
  497. "1/16",
  498. "0",
  499. };
  500. static const struct soc_enum drc_r0 =
  501. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 8, 6, drc_r0_text);
  502. static const char *drc_r1_text[] = {
  503. "1",
  504. "1/2",
  505. "1/4",
  506. "1/8",
  507. "0",
  508. };
  509. static const struct soc_enum drc_r1 =
  510. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_4, 13, 5, drc_r1_text);
  511. static const char *drc_attack_text[] = {
  512. "Reserved",
  513. "181us",
  514. "363us",
  515. "726us",
  516. "1.45ms",
  517. "2.9ms",
  518. "5.8ms",
  519. "11.6ms",
  520. "23.2ms",
  521. "46.4ms",
  522. "92.8ms",
  523. "185.6ms",
  524. };
  525. static const struct soc_enum drc_attack =
  526. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 12, 12, drc_attack_text);
  527. static const char *drc_decay_text[] = {
  528. "186ms",
  529. "372ms",
  530. "743ms",
  531. "1.49s",
  532. "2.97ms",
  533. "5.94ms",
  534. "11.89ms",
  535. "23.78ms",
  536. "47.56ms",
  537. };
  538. static const struct soc_enum drc_decay =
  539. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_2, 8, 9, drc_decay_text);
  540. static const char *drc_ff_text[] = {
  541. "5 samples",
  542. "9 samples",
  543. };
  544. static const struct soc_enum drc_ff =
  545. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 7, 2, drc_ff_text);
  546. static const char *drc_qr_rate_text[] = {
  547. "0.725ms",
  548. "1.45ms",
  549. "5.8ms",
  550. };
  551. static const struct soc_enum drc_qr_rate =
  552. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_3, 0, 3, drc_qr_rate_text);
  553. static const char *drc_smooth_text[] = {
  554. "Low",
  555. "Medium",
  556. "High",
  557. };
  558. static const struct soc_enum drc_smooth =
  559. SOC_ENUM_SINGLE(WM8993_DRC_CONTROL_1, 4, 3, drc_smooth_text);
  560. static const struct snd_kcontrol_new wm8993_snd_controls[] = {
  561. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8993_DIGITAL_SIDE_TONE,
  562. 5, 9, 12, 0, sidetone_tlv),
  563. SOC_SINGLE("DRC Switch", WM8993_DRC_CONTROL_1, 15, 1, 0),
  564. SOC_ENUM("DRC Path", drc_path),
  565. SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8993_DRC_CONTROL_2,
  566. 2, 60, 1, drc_comp_threash),
  567. SOC_SINGLE_TLV("DRC Compressor Amplitude Volume", WM8993_DRC_CONTROL_3,
  568. 11, 30, 1, drc_comp_amp),
  569. SOC_ENUM("DRC R0", drc_r0),
  570. SOC_ENUM("DRC R1", drc_r1),
  571. SOC_SINGLE_TLV("DRC Minimum Volume", WM8993_DRC_CONTROL_1, 2, 3, 1,
  572. drc_min_tlv),
  573. SOC_SINGLE_TLV("DRC Maximum Volume", WM8993_DRC_CONTROL_1, 0, 3, 0,
  574. drc_max_tlv),
  575. SOC_ENUM("DRC Attack Rate", drc_attack),
  576. SOC_ENUM("DRC Decay Rate", drc_decay),
  577. SOC_ENUM("DRC FF Delay", drc_ff),
  578. SOC_SINGLE("DRC Anti-clip Switch", WM8993_DRC_CONTROL_1, 9, 1, 0),
  579. SOC_SINGLE("DRC Quick Release Switch", WM8993_DRC_CONTROL_1, 10, 1, 0),
  580. SOC_SINGLE_TLV("DRC Quick Release Volume", WM8993_DRC_CONTROL_3, 2, 3, 0,
  581. drc_qr_tlv),
  582. SOC_ENUM("DRC Quick Release Rate", drc_qr_rate),
  583. SOC_SINGLE("DRC Smoothing Switch", WM8993_DRC_CONTROL_1, 11, 1, 0),
  584. SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8993_DRC_CONTROL_1, 8, 1, 0),
  585. SOC_ENUM("DRC Smoothing Hysteresis Threshold", drc_smooth),
  586. SOC_SINGLE_TLV("DRC Startup Volume", WM8993_DRC_CONTROL_4, 8, 18, 0,
  587. drc_startup_tlv),
  588. SOC_SINGLE("EQ Switch", WM8993_EQ1, 0, 1, 0),
  589. SOC_DOUBLE_R_TLV("Capture Volume", WM8993_LEFT_ADC_DIGITAL_VOLUME,
  590. WM8993_RIGHT_ADC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
  591. SOC_SINGLE("ADC High Pass Filter Switch", WM8993_ADC_CTRL, 8, 1, 0),
  592. SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
  593. SOC_DOUBLE_R_TLV("Playback Volume", WM8993_LEFT_DAC_DIGITAL_VOLUME,
  594. WM8993_RIGHT_DAC_DIGITAL_VOLUME, 1, 96, 0, digital_tlv),
  595. SOC_SINGLE_TLV("Playback Boost Volume", WM8993_AUDIO_INTERFACE_2, 10, 3, 0,
  596. dac_boost_tlv),
  597. SOC_ENUM("DAC Deemphasis", dac_deemph),
  598. SOC_SINGLE_TLV("SPKL DAC Volume", WM8993_SPKMIXL_ATTENUATION,
  599. 2, 1, 1, wm_hubs_spkmix_tlv),
  600. SOC_SINGLE_TLV("SPKR DAC Volume", WM8993_SPKMIXR_ATTENUATION,
  601. 2, 1, 1, wm_hubs_spkmix_tlv),
  602. };
  603. static const struct snd_kcontrol_new wm8993_eq_controls[] = {
  604. SOC_SINGLE_TLV("EQ1 Volume", WM8993_EQ2, 0, 24, 0, eq_tlv),
  605. SOC_SINGLE_TLV("EQ2 Volume", WM8993_EQ3, 0, 24, 0, eq_tlv),
  606. SOC_SINGLE_TLV("EQ3 Volume", WM8993_EQ4, 0, 24, 0, eq_tlv),
  607. SOC_SINGLE_TLV("EQ4 Volume", WM8993_EQ5, 0, 24, 0, eq_tlv),
  608. SOC_SINGLE_TLV("EQ5 Volume", WM8993_EQ6, 0, 24, 0, eq_tlv),
  609. };
  610. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  611. struct snd_kcontrol *kcontrol, int event)
  612. {
  613. struct snd_soc_codec *codec = w->codec;
  614. switch (event) {
  615. case SND_SOC_DAPM_PRE_PMU:
  616. return configure_clock(codec);
  617. case SND_SOC_DAPM_POST_PMD:
  618. break;
  619. }
  620. return 0;
  621. }
  622. /*
  623. * When used with DAC outputs only the WM8993 charge pump supports
  624. * operation in class W mode, providing very low power consumption
  625. * when used with digital sources. Enable and disable this mode
  626. * automatically depending on the mixer configuration.
  627. *
  628. * Currently the only supported paths are the direct DAC->headphone
  629. * paths (which provide minimum power consumption anyway).
  630. */
  631. static int class_w_put(struct snd_kcontrol *kcontrol,
  632. struct snd_ctl_elem_value *ucontrol)
  633. {
  634. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  635. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  636. struct snd_soc_codec *codec = widget->codec;
  637. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  638. int ret;
  639. /* Turn it off if we're using the main output mixer */
  640. if (ucontrol->value.integer.value[0] == 0) {
  641. if (wm8993->class_w_users == 0) {
  642. dev_dbg(codec->dev, "Disabling Class W\n");
  643. snd_soc_update_bits(codec, WM8993_CLASS_W_0,
  644. WM8993_CP_DYN_FREQ |
  645. WM8993_CP_DYN_V,
  646. 0);
  647. }
  648. wm8993->class_w_users++;
  649. wm8993->hubs_data.class_w = true;
  650. }
  651. /* Implement the change */
  652. ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  653. /* Enable it if we're using the direct DAC path */
  654. if (ucontrol->value.integer.value[0] == 1) {
  655. if (wm8993->class_w_users == 1) {
  656. dev_dbg(codec->dev, "Enabling Class W\n");
  657. snd_soc_update_bits(codec, WM8993_CLASS_W_0,
  658. WM8993_CP_DYN_FREQ |
  659. WM8993_CP_DYN_V,
  660. WM8993_CP_DYN_FREQ |
  661. WM8993_CP_DYN_V);
  662. }
  663. wm8993->class_w_users--;
  664. wm8993->hubs_data.class_w = false;
  665. }
  666. dev_dbg(codec->dev, "Indirect DAC use count now %d\n",
  667. wm8993->class_w_users);
  668. return ret;
  669. }
  670. #define SOC_DAPM_ENUM_W(xname, xenum) \
  671. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  672. .info = snd_soc_info_enum_double, \
  673. .get = snd_soc_dapm_get_enum_double, \
  674. .put = class_w_put, \
  675. .private_value = (unsigned long)&xenum }
  676. static const char *hp_mux_text[] = {
  677. "Mixer",
  678. "DAC",
  679. };
  680. static const struct soc_enum hpl_enum =
  681. SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER1, 8, 2, hp_mux_text);
  682. static const struct snd_kcontrol_new hpl_mux =
  683. SOC_DAPM_ENUM_W("Left Headphone Mux", hpl_enum);
  684. static const struct soc_enum hpr_enum =
  685. SOC_ENUM_SINGLE(WM8993_OUTPUT_MIXER2, 8, 2, hp_mux_text);
  686. static const struct snd_kcontrol_new hpr_mux =
  687. SOC_DAPM_ENUM_W("Right Headphone Mux", hpr_enum);
  688. static const struct snd_kcontrol_new left_speaker_mixer[] = {
  689. SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 7, 1, 0),
  690. SOC_DAPM_SINGLE("IN1LP Switch", WM8993_SPEAKER_MIXER, 5, 1, 0),
  691. SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 3, 1, 0),
  692. SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
  693. };
  694. static const struct snd_kcontrol_new right_speaker_mixer[] = {
  695. SOC_DAPM_SINGLE("Input Switch", WM8993_SPEAKER_MIXER, 6, 1, 0),
  696. SOC_DAPM_SINGLE("IN1RP Switch", WM8993_SPEAKER_MIXER, 4, 1, 0),
  697. SOC_DAPM_SINGLE("Output Switch", WM8993_SPEAKER_MIXER, 2, 1, 0),
  698. SOC_DAPM_SINGLE("DAC Switch", WM8993_SPEAKER_MIXER, 0, 1, 0),
  699. };
  700. static const char *aif_text[] = {
  701. "Left", "Right"
  702. };
  703. static const struct soc_enum aifoutl_enum =
  704. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 15, 2, aif_text);
  705. static const struct snd_kcontrol_new aifoutl_mux =
  706. SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
  707. static const struct soc_enum aifoutr_enum =
  708. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_1, 14, 2, aif_text);
  709. static const struct snd_kcontrol_new aifoutr_mux =
  710. SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
  711. static const struct soc_enum aifinl_enum =
  712. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 15, 2, aif_text);
  713. static const struct snd_kcontrol_new aifinl_mux =
  714. SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
  715. static const struct soc_enum aifinr_enum =
  716. SOC_ENUM_SINGLE(WM8993_AUDIO_INTERFACE_2, 14, 2, aif_text);
  717. static const struct snd_kcontrol_new aifinr_mux =
  718. SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
  719. static const char *sidetone_text[] = {
  720. "None", "Left", "Right"
  721. };
  722. static const struct soc_enum sidetonel_enum =
  723. SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 2, 3, sidetone_text);
  724. static const struct snd_kcontrol_new sidetonel_mux =
  725. SOC_DAPM_ENUM("Left Sidetone", sidetonel_enum);
  726. static const struct soc_enum sidetoner_enum =
  727. SOC_ENUM_SINGLE(WM8993_DIGITAL_SIDE_TONE, 0, 3, sidetone_text);
  728. static const struct snd_kcontrol_new sidetoner_mux =
  729. SOC_DAPM_ENUM("Right Sidetone", sidetoner_enum);
  730. static const struct snd_soc_dapm_widget wm8993_dapm_widgets[] = {
  731. SND_SOC_DAPM_SUPPLY("CLK_SYS", WM8993_BUS_CONTROL_1, 1, 0, clk_sys_event,
  732. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  733. SND_SOC_DAPM_SUPPLY("TOCLK", WM8993_CLOCKING_1, 14, 0, NULL, 0),
  734. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8993_CLOCKING_3, 0, 0, NULL, 0),
  735. SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, NULL, 0),
  736. SND_SOC_DAPM_ADC("ADCL", NULL, WM8993_POWER_MANAGEMENT_2, 1, 0),
  737. SND_SOC_DAPM_ADC("ADCR", NULL, WM8993_POWER_MANAGEMENT_2, 0, 0),
  738. SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
  739. SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
  740. SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
  741. SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
  742. SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
  743. SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
  744. SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
  745. SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
  746. SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &sidetonel_mux),
  747. SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &sidetoner_mux),
  748. SND_SOC_DAPM_DAC("DACL", NULL, WM8993_POWER_MANAGEMENT_3, 1, 0),
  749. SND_SOC_DAPM_DAC("DACR", NULL, WM8993_POWER_MANAGEMENT_3, 0, 0),
  750. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
  751. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
  752. SND_SOC_DAPM_MIXER("SPKL", WM8993_POWER_MANAGEMENT_3, 8, 0,
  753. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
  754. SND_SOC_DAPM_MIXER("SPKR", WM8993_POWER_MANAGEMENT_3, 9, 0,
  755. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
  756. SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
  757. };
  758. static const struct snd_soc_dapm_route routes[] = {
  759. { "MICBIAS1", NULL, "VMID" },
  760. { "MICBIAS2", NULL, "VMID" },
  761. { "ADCL", NULL, "CLK_SYS" },
  762. { "ADCL", NULL, "CLK_DSP" },
  763. { "ADCR", NULL, "CLK_SYS" },
  764. { "ADCR", NULL, "CLK_DSP" },
  765. { "AIFOUTL Mux", "Left", "ADCL" },
  766. { "AIFOUTL Mux", "Right", "ADCR" },
  767. { "AIFOUTR Mux", "Left", "ADCL" },
  768. { "AIFOUTR Mux", "Right", "ADCR" },
  769. { "AIFOUTL", NULL, "AIFOUTL Mux" },
  770. { "AIFOUTR", NULL, "AIFOUTR Mux" },
  771. { "DACL Mux", "Left", "AIFINL" },
  772. { "DACL Mux", "Right", "AIFINR" },
  773. { "DACR Mux", "Left", "AIFINL" },
  774. { "DACR Mux", "Right", "AIFINR" },
  775. { "DACL Sidetone", "Left", "ADCL" },
  776. { "DACL Sidetone", "Right", "ADCR" },
  777. { "DACR Sidetone", "Left", "ADCL" },
  778. { "DACR Sidetone", "Right", "ADCR" },
  779. { "DACL", NULL, "CLK_SYS" },
  780. { "DACL", NULL, "CLK_DSP" },
  781. { "DACL", NULL, "DACL Mux" },
  782. { "DACL", NULL, "DACL Sidetone" },
  783. { "DACR", NULL, "CLK_SYS" },
  784. { "DACR", NULL, "CLK_DSP" },
  785. { "DACR", NULL, "DACR Mux" },
  786. { "DACR", NULL, "DACR Sidetone" },
  787. { "Left Output Mixer", "DAC Switch", "DACL" },
  788. { "Right Output Mixer", "DAC Switch", "DACR" },
  789. { "Left Output PGA", NULL, "CLK_SYS" },
  790. { "Right Output PGA", NULL, "CLK_SYS" },
  791. { "SPKL", "DAC Switch", "DACL" },
  792. { "SPKL", NULL, "CLK_SYS" },
  793. { "SPKR", "DAC Switch", "DACR" },
  794. { "SPKR", NULL, "CLK_SYS" },
  795. { "Left Headphone Mux", "DAC", "DACL" },
  796. { "Right Headphone Mux", "DAC", "DACR" },
  797. };
  798. static int wm8993_set_bias_level(struct snd_soc_codec *codec,
  799. enum snd_soc_bias_level level)
  800. {
  801. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  802. int ret;
  803. switch (level) {
  804. case SND_SOC_BIAS_ON:
  805. case SND_SOC_BIAS_PREPARE:
  806. /* VMID=2*40k */
  807. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  808. WM8993_VMID_SEL_MASK, 0x2);
  809. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
  810. WM8993_TSHUT_ENA, WM8993_TSHUT_ENA);
  811. break;
  812. case SND_SOC_BIAS_STANDBY:
  813. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  814. ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
  815. wm8993->supplies);
  816. if (ret != 0)
  817. return ret;
  818. snd_soc_cache_sync(codec);
  819. /* Tune DC servo configuration */
  820. snd_soc_write(codec, 0x44, 3);
  821. snd_soc_write(codec, 0x56, 3);
  822. snd_soc_write(codec, 0x44, 0);
  823. /* Bring up VMID with fast soft start */
  824. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  825. WM8993_STARTUP_BIAS_ENA |
  826. WM8993_VMID_BUF_ENA |
  827. WM8993_VMID_RAMP_MASK |
  828. WM8993_BIAS_SRC,
  829. WM8993_STARTUP_BIAS_ENA |
  830. WM8993_VMID_BUF_ENA |
  831. WM8993_VMID_RAMP_MASK |
  832. WM8993_BIAS_SRC);
  833. /* If either line output is single ended we
  834. * need the VMID buffer */
  835. if (!wm8993->pdata.lineout1_diff ||
  836. !wm8993->pdata.lineout2_diff)
  837. snd_soc_update_bits(codec, WM8993_ANTIPOP1,
  838. WM8993_LINEOUT_VMID_BUF_ENA,
  839. WM8993_LINEOUT_VMID_BUF_ENA);
  840. /* VMID=2*40k */
  841. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  842. WM8993_VMID_SEL_MASK |
  843. WM8993_BIAS_ENA,
  844. WM8993_BIAS_ENA | 0x2);
  845. msleep(32);
  846. /* Switch to normal bias */
  847. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  848. WM8993_BIAS_SRC |
  849. WM8993_STARTUP_BIAS_ENA, 0);
  850. }
  851. /* VMID=2*240k */
  852. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  853. WM8993_VMID_SEL_MASK, 0x4);
  854. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_2,
  855. WM8993_TSHUT_ENA, 0);
  856. break;
  857. case SND_SOC_BIAS_OFF:
  858. snd_soc_update_bits(codec, WM8993_ANTIPOP1,
  859. WM8993_LINEOUT_VMID_BUF_ENA, 0);
  860. snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
  861. WM8993_VMID_SEL_MASK | WM8993_BIAS_ENA,
  862. 0);
  863. snd_soc_update_bits(codec, WM8993_ANTIPOP2,
  864. WM8993_STARTUP_BIAS_ENA |
  865. WM8993_VMID_BUF_ENA |
  866. WM8993_VMID_RAMP_MASK |
  867. WM8993_BIAS_SRC, 0);
  868. #ifdef CONFIG_REGULATOR
  869. /* Post 2.6.34 we will be able to get a callback when
  870. * the regulators are disabled which we can use but
  871. * for now just assume that the power will be cut if
  872. * the regulator API is in use.
  873. */
  874. codec->cache_sync = 1;
  875. #endif
  876. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies),
  877. wm8993->supplies);
  878. break;
  879. }
  880. codec->dapm.bias_level = level;
  881. return 0;
  882. }
  883. static int wm8993_set_sysclk(struct snd_soc_dai *codec_dai,
  884. int clk_id, unsigned int freq, int dir)
  885. {
  886. struct snd_soc_codec *codec = codec_dai->codec;
  887. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  888. switch (clk_id) {
  889. case WM8993_SYSCLK_MCLK:
  890. wm8993->mclk_rate = freq;
  891. case WM8993_SYSCLK_FLL:
  892. wm8993->sysclk_source = clk_id;
  893. break;
  894. default:
  895. return -EINVAL;
  896. }
  897. return 0;
  898. }
  899. static int wm8993_set_dai_fmt(struct snd_soc_dai *dai,
  900. unsigned int fmt)
  901. {
  902. struct snd_soc_codec *codec = dai->codec;
  903. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  904. unsigned int aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
  905. unsigned int aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
  906. aif1 &= ~(WM8993_BCLK_DIR | WM8993_AIF_BCLK_INV |
  907. WM8993_AIF_LRCLK_INV | WM8993_AIF_FMT_MASK);
  908. aif4 &= ~WM8993_LRCLK_DIR;
  909. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  910. case SND_SOC_DAIFMT_CBS_CFS:
  911. wm8993->master = 0;
  912. break;
  913. case SND_SOC_DAIFMT_CBS_CFM:
  914. aif4 |= WM8993_LRCLK_DIR;
  915. wm8993->master = 1;
  916. break;
  917. case SND_SOC_DAIFMT_CBM_CFS:
  918. aif1 |= WM8993_BCLK_DIR;
  919. wm8993->master = 1;
  920. break;
  921. case SND_SOC_DAIFMT_CBM_CFM:
  922. aif1 |= WM8993_BCLK_DIR;
  923. aif4 |= WM8993_LRCLK_DIR;
  924. wm8993->master = 1;
  925. break;
  926. default:
  927. return -EINVAL;
  928. }
  929. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  930. case SND_SOC_DAIFMT_DSP_B:
  931. aif1 |= WM8993_AIF_LRCLK_INV;
  932. case SND_SOC_DAIFMT_DSP_A:
  933. aif1 |= 0x18;
  934. break;
  935. case SND_SOC_DAIFMT_I2S:
  936. aif1 |= 0x10;
  937. break;
  938. case SND_SOC_DAIFMT_RIGHT_J:
  939. break;
  940. case SND_SOC_DAIFMT_LEFT_J:
  941. aif1 |= 0x8;
  942. break;
  943. default:
  944. return -EINVAL;
  945. }
  946. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  947. case SND_SOC_DAIFMT_DSP_A:
  948. case SND_SOC_DAIFMT_DSP_B:
  949. /* frame inversion not valid for DSP modes */
  950. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  951. case SND_SOC_DAIFMT_NB_NF:
  952. break;
  953. case SND_SOC_DAIFMT_IB_NF:
  954. aif1 |= WM8993_AIF_BCLK_INV;
  955. break;
  956. default:
  957. return -EINVAL;
  958. }
  959. break;
  960. case SND_SOC_DAIFMT_I2S:
  961. case SND_SOC_DAIFMT_RIGHT_J:
  962. case SND_SOC_DAIFMT_LEFT_J:
  963. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  964. case SND_SOC_DAIFMT_NB_NF:
  965. break;
  966. case SND_SOC_DAIFMT_IB_IF:
  967. aif1 |= WM8993_AIF_BCLK_INV | WM8993_AIF_LRCLK_INV;
  968. break;
  969. case SND_SOC_DAIFMT_IB_NF:
  970. aif1 |= WM8993_AIF_BCLK_INV;
  971. break;
  972. case SND_SOC_DAIFMT_NB_IF:
  973. aif1 |= WM8993_AIF_LRCLK_INV;
  974. break;
  975. default:
  976. return -EINVAL;
  977. }
  978. break;
  979. default:
  980. return -EINVAL;
  981. }
  982. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
  983. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
  984. return 0;
  985. }
  986. static int wm8993_hw_params(struct snd_pcm_substream *substream,
  987. struct snd_pcm_hw_params *params,
  988. struct snd_soc_dai *dai)
  989. {
  990. struct snd_soc_codec *codec = dai->codec;
  991. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  992. int ret, i, best, best_val, cur_val;
  993. unsigned int clocking1, clocking3, aif1, aif4;
  994. clocking1 = snd_soc_read(codec, WM8993_CLOCKING_1);
  995. clocking1 &= ~WM8993_BCLK_DIV_MASK;
  996. clocking3 = snd_soc_read(codec, WM8993_CLOCKING_3);
  997. clocking3 &= ~(WM8993_CLK_SYS_RATE_MASK | WM8993_SAMPLE_RATE_MASK);
  998. aif1 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_1);
  999. aif1 &= ~WM8993_AIF_WL_MASK;
  1000. aif4 = snd_soc_read(codec, WM8993_AUDIO_INTERFACE_4);
  1001. aif4 &= ~WM8993_LRCLK_RATE_MASK;
  1002. /* What BCLK do we need? */
  1003. wm8993->fs = params_rate(params);
  1004. wm8993->bclk = 2 * wm8993->fs;
  1005. if (wm8993->tdm_slots) {
  1006. dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
  1007. wm8993->tdm_slots, wm8993->tdm_width);
  1008. wm8993->bclk *= wm8993->tdm_width * wm8993->tdm_slots;
  1009. } else {
  1010. switch (params_format(params)) {
  1011. case SNDRV_PCM_FORMAT_S16_LE:
  1012. wm8993->bclk *= 16;
  1013. break;
  1014. case SNDRV_PCM_FORMAT_S20_3LE:
  1015. wm8993->bclk *= 20;
  1016. aif1 |= 0x8;
  1017. break;
  1018. case SNDRV_PCM_FORMAT_S24_LE:
  1019. wm8993->bclk *= 24;
  1020. aif1 |= 0x10;
  1021. break;
  1022. case SNDRV_PCM_FORMAT_S32_LE:
  1023. wm8993->bclk *= 32;
  1024. aif1 |= 0x18;
  1025. break;
  1026. default:
  1027. return -EINVAL;
  1028. }
  1029. }
  1030. dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8993->bclk);
  1031. ret = configure_clock(codec);
  1032. if (ret != 0)
  1033. return ret;
  1034. /* Select nearest CLK_SYS_RATE */
  1035. best = 0;
  1036. best_val = abs((wm8993->sysclk_rate / clk_sys_rates[0].ratio)
  1037. - wm8993->fs);
  1038. for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
  1039. cur_val = abs((wm8993->sysclk_rate /
  1040. clk_sys_rates[i].ratio) - wm8993->fs);
  1041. if (cur_val < best_val) {
  1042. best = i;
  1043. best_val = cur_val;
  1044. }
  1045. }
  1046. dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
  1047. clk_sys_rates[best].ratio);
  1048. clocking3 |= (clk_sys_rates[best].clk_sys_rate
  1049. << WM8993_CLK_SYS_RATE_SHIFT);
  1050. /* SAMPLE_RATE */
  1051. best = 0;
  1052. best_val = abs(wm8993->fs - sample_rates[0].rate);
  1053. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1054. /* Closest match */
  1055. cur_val = abs(wm8993->fs - sample_rates[i].rate);
  1056. if (cur_val < best_val) {
  1057. best = i;
  1058. best_val = cur_val;
  1059. }
  1060. }
  1061. dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
  1062. sample_rates[best].rate);
  1063. clocking3 |= (sample_rates[best].sample_rate
  1064. << WM8993_SAMPLE_RATE_SHIFT);
  1065. /* BCLK_DIV */
  1066. best = 0;
  1067. best_val = INT_MAX;
  1068. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  1069. cur_val = ((wm8993->sysclk_rate * 10) / bclk_divs[i].div)
  1070. - wm8993->bclk;
  1071. if (cur_val < 0) /* Table is sorted */
  1072. break;
  1073. if (cur_val < best_val) {
  1074. best = i;
  1075. best_val = cur_val;
  1076. }
  1077. }
  1078. wm8993->bclk = (wm8993->sysclk_rate * 10) / bclk_divs[best].div;
  1079. dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
  1080. bclk_divs[best].div, wm8993->bclk);
  1081. clocking1 |= bclk_divs[best].bclk_div << WM8993_BCLK_DIV_SHIFT;
  1082. /* LRCLK is a simple fraction of BCLK */
  1083. dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8993->bclk / wm8993->fs);
  1084. aif4 |= wm8993->bclk / wm8993->fs;
  1085. snd_soc_write(codec, WM8993_CLOCKING_1, clocking1);
  1086. snd_soc_write(codec, WM8993_CLOCKING_3, clocking3);
  1087. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_1, aif1);
  1088. snd_soc_write(codec, WM8993_AUDIO_INTERFACE_4, aif4);
  1089. /* ReTune Mobile? */
  1090. if (wm8993->pdata.num_retune_configs) {
  1091. u16 eq1 = snd_soc_read(codec, WM8993_EQ1);
  1092. struct wm8993_retune_mobile_setting *s;
  1093. best = 0;
  1094. best_val = abs(wm8993->pdata.retune_configs[0].rate
  1095. - wm8993->fs);
  1096. for (i = 0; i < wm8993->pdata.num_retune_configs; i++) {
  1097. cur_val = abs(wm8993->pdata.retune_configs[i].rate
  1098. - wm8993->fs);
  1099. if (cur_val < best_val) {
  1100. best_val = cur_val;
  1101. best = i;
  1102. }
  1103. }
  1104. s = &wm8993->pdata.retune_configs[best];
  1105. dev_dbg(codec->dev, "ReTune Mobile %s tuned for %dHz\n",
  1106. s->name, s->rate);
  1107. /* Disable EQ while we reconfigure */
  1108. snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, 0);
  1109. for (i = 1; i < ARRAY_SIZE(s->config); i++)
  1110. snd_soc_write(codec, WM8993_EQ1 + i, s->config[i]);
  1111. snd_soc_update_bits(codec, WM8993_EQ1, WM8993_EQ_ENA, eq1);
  1112. }
  1113. return 0;
  1114. }
  1115. static int wm8993_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1116. {
  1117. struct snd_soc_codec *codec = codec_dai->codec;
  1118. unsigned int reg;
  1119. reg = snd_soc_read(codec, WM8993_DAC_CTRL);
  1120. if (mute)
  1121. reg |= WM8993_DAC_MUTE;
  1122. else
  1123. reg &= ~WM8993_DAC_MUTE;
  1124. snd_soc_write(codec, WM8993_DAC_CTRL, reg);
  1125. return 0;
  1126. }
  1127. static int wm8993_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  1128. unsigned int rx_mask, int slots, int slot_width)
  1129. {
  1130. struct snd_soc_codec *codec = dai->codec;
  1131. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1132. int aif1 = 0;
  1133. int aif2 = 0;
  1134. /* Don't need to validate anything if we're turning off TDM */
  1135. if (slots == 0) {
  1136. wm8993->tdm_slots = 0;
  1137. goto out;
  1138. }
  1139. /* Note that we allow configurations we can't handle ourselves -
  1140. * for example, we can generate clocks for slots 2 and up even if
  1141. * we can't use those slots ourselves.
  1142. */
  1143. aif1 |= WM8993_AIFADC_TDM;
  1144. aif2 |= WM8993_AIFDAC_TDM;
  1145. switch (rx_mask) {
  1146. case 3:
  1147. break;
  1148. case 0xc:
  1149. aif1 |= WM8993_AIFADC_TDM_CHAN;
  1150. break;
  1151. default:
  1152. return -EINVAL;
  1153. }
  1154. switch (tx_mask) {
  1155. case 3:
  1156. break;
  1157. case 0xc:
  1158. aif2 |= WM8993_AIFDAC_TDM_CHAN;
  1159. break;
  1160. default:
  1161. return -EINVAL;
  1162. }
  1163. out:
  1164. wm8993->tdm_width = slot_width;
  1165. wm8993->tdm_slots = slots / 2;
  1166. snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_1,
  1167. WM8993_AIFADC_TDM | WM8993_AIFADC_TDM_CHAN, aif1);
  1168. snd_soc_update_bits(codec, WM8993_AUDIO_INTERFACE_2,
  1169. WM8993_AIFDAC_TDM | WM8993_AIFDAC_TDM_CHAN, aif2);
  1170. return 0;
  1171. }
  1172. static const struct snd_soc_dai_ops wm8993_ops = {
  1173. .set_sysclk = wm8993_set_sysclk,
  1174. .set_fmt = wm8993_set_dai_fmt,
  1175. .hw_params = wm8993_hw_params,
  1176. .digital_mute = wm8993_digital_mute,
  1177. .set_pll = wm8993_set_fll,
  1178. .set_tdm_slot = wm8993_set_tdm_slot,
  1179. };
  1180. #define WM8993_RATES SNDRV_PCM_RATE_8000_48000
  1181. #define WM8993_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  1182. SNDRV_PCM_FMTBIT_S20_3LE |\
  1183. SNDRV_PCM_FMTBIT_S24_LE |\
  1184. SNDRV_PCM_FMTBIT_S32_LE)
  1185. static struct snd_soc_dai_driver wm8993_dai = {
  1186. .name = "wm8993-hifi",
  1187. .playback = {
  1188. .stream_name = "Playback",
  1189. .channels_min = 1,
  1190. .channels_max = 2,
  1191. .rates = WM8993_RATES,
  1192. .formats = WM8993_FORMATS,
  1193. },
  1194. .capture = {
  1195. .stream_name = "Capture",
  1196. .channels_min = 1,
  1197. .channels_max = 2,
  1198. .rates = WM8993_RATES,
  1199. .formats = WM8993_FORMATS,
  1200. },
  1201. .ops = &wm8993_ops,
  1202. .symmetric_rates = 1,
  1203. };
  1204. static int wm8993_probe(struct snd_soc_codec *codec)
  1205. {
  1206. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1207. struct snd_soc_dapm_context *dapm = &codec->dapm;
  1208. int ret, i, val;
  1209. wm8993->hubs_data.hp_startup_mode = 1;
  1210. wm8993->hubs_data.dcs_codes_l = -2;
  1211. wm8993->hubs_data.dcs_codes_r = -2;
  1212. wm8993->hubs_data.series_startup = 1;
  1213. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  1214. if (ret != 0) {
  1215. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1216. return ret;
  1217. }
  1218. for (i = 0; i < ARRAY_SIZE(wm8993->supplies); i++)
  1219. wm8993->supplies[i].supply = wm8993_supply_names[i];
  1220. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8993->supplies),
  1221. wm8993->supplies);
  1222. if (ret != 0) {
  1223. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1224. return ret;
  1225. }
  1226. ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
  1227. wm8993->supplies);
  1228. if (ret != 0) {
  1229. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  1230. goto err_get;
  1231. }
  1232. val = snd_soc_read(codec, WM8993_SOFTWARE_RESET);
  1233. if (val != wm8993_reg_defaults[WM8993_SOFTWARE_RESET]) {
  1234. dev_err(codec->dev, "Invalid ID register value %x\n", val);
  1235. ret = -EINVAL;
  1236. goto err_enable;
  1237. }
  1238. ret = snd_soc_write(codec, WM8993_SOFTWARE_RESET, 0xffff);
  1239. if (ret != 0)
  1240. goto err_enable;
  1241. codec->cache_only = 1;
  1242. /* By default we're using the output mixers */
  1243. wm8993->class_w_users = 2;
  1244. /* Latch volume update bits and default ZC on */
  1245. snd_soc_update_bits(codec, WM8993_RIGHT_DAC_DIGITAL_VOLUME,
  1246. WM8993_DAC_VU, WM8993_DAC_VU);
  1247. snd_soc_update_bits(codec, WM8993_RIGHT_ADC_DIGITAL_VOLUME,
  1248. WM8993_ADC_VU, WM8993_ADC_VU);
  1249. /* Manualy manage the HPOUT sequencing for independent stereo
  1250. * control. */
  1251. snd_soc_update_bits(codec, WM8993_ANALOGUE_HP_0,
  1252. WM8993_HPOUT1_AUTO_PU, 0);
  1253. /* Use automatic clock configuration */
  1254. snd_soc_update_bits(codec, WM8993_CLOCKING_4, WM8993_SR_MODE, 0);
  1255. wm_hubs_handle_analogue_pdata(codec, wm8993->pdata.lineout1_diff,
  1256. wm8993->pdata.lineout2_diff,
  1257. wm8993->pdata.lineout1fb,
  1258. wm8993->pdata.lineout2fb,
  1259. wm8993->pdata.jd_scthr,
  1260. wm8993->pdata.jd_thr,
  1261. wm8993->pdata.micbias1_lvl,
  1262. wm8993->pdata.micbias2_lvl);
  1263. ret = wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1264. if (ret != 0)
  1265. goto err_enable;
  1266. snd_soc_add_controls(codec, wm8993_snd_controls,
  1267. ARRAY_SIZE(wm8993_snd_controls));
  1268. if (wm8993->pdata.num_retune_configs != 0) {
  1269. dev_dbg(codec->dev, "Using ReTune Mobile\n");
  1270. } else {
  1271. dev_dbg(codec->dev, "No ReTune Mobile, using normal EQ\n");
  1272. snd_soc_add_controls(codec, wm8993_eq_controls,
  1273. ARRAY_SIZE(wm8993_eq_controls));
  1274. }
  1275. snd_soc_dapm_new_controls(dapm, wm8993_dapm_widgets,
  1276. ARRAY_SIZE(wm8993_dapm_widgets));
  1277. wm_hubs_add_analogue_controls(codec);
  1278. snd_soc_dapm_add_routes(dapm, routes, ARRAY_SIZE(routes));
  1279. wm_hubs_add_analogue_routes(codec, wm8993->pdata.lineout1_diff,
  1280. wm8993->pdata.lineout2_diff);
  1281. return 0;
  1282. err_enable:
  1283. regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1284. err_get:
  1285. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1286. return ret;
  1287. }
  1288. static int wm8993_remove(struct snd_soc_codec *codec)
  1289. {
  1290. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1291. wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1292. regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
  1293. return 0;
  1294. }
  1295. #ifdef CONFIG_PM
  1296. static int wm8993_suspend(struct snd_soc_codec *codec)
  1297. {
  1298. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1299. int fll_fout = wm8993->fll_fout;
  1300. int fll_fref = wm8993->fll_fref;
  1301. int ret;
  1302. /* Stop the FLL in an orderly fashion */
  1303. ret = _wm8993_set_fll(codec, 0, 0, 0, 0);
  1304. if (ret != 0) {
  1305. dev_err(codec->dev, "Failed to stop FLL\n");
  1306. return ret;
  1307. }
  1308. wm8993->fll_fout = fll_fout;
  1309. wm8993->fll_fref = fll_fref;
  1310. wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1311. return 0;
  1312. }
  1313. static int wm8993_resume(struct snd_soc_codec *codec)
  1314. {
  1315. struct wm8993_priv *wm8993 = snd_soc_codec_get_drvdata(codec);
  1316. int ret;
  1317. wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1318. /* Restart the FLL? */
  1319. if (wm8993->fll_fout) {
  1320. int fll_fout = wm8993->fll_fout;
  1321. int fll_fref = wm8993->fll_fref;
  1322. wm8993->fll_fref = 0;
  1323. wm8993->fll_fout = 0;
  1324. ret = _wm8993_set_fll(codec, 0, wm8993->fll_src,
  1325. fll_fref, fll_fout);
  1326. if (ret != 0)
  1327. dev_err(codec->dev, "Failed to restart FLL\n");
  1328. }
  1329. return 0;
  1330. }
  1331. #else
  1332. #define wm8993_suspend NULL
  1333. #define wm8993_resume NULL
  1334. #endif
  1335. static struct snd_soc_codec_driver soc_codec_dev_wm8993 = {
  1336. .probe = wm8993_probe,
  1337. .remove = wm8993_remove,
  1338. .suspend = wm8993_suspend,
  1339. .resume = wm8993_resume,
  1340. .set_bias_level = wm8993_set_bias_level,
  1341. .reg_cache_size = ARRAY_SIZE(wm8993_reg_defaults),
  1342. .reg_word_size = sizeof(u16),
  1343. .reg_cache_default = wm8993_reg_defaults,
  1344. .volatile_register = wm8993_volatile,
  1345. };
  1346. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1347. static __devinit int wm8993_i2c_probe(struct i2c_client *i2c,
  1348. const struct i2c_device_id *id)
  1349. {
  1350. struct wm8993_priv *wm8993;
  1351. int ret;
  1352. wm8993 = devm_kzalloc(&i2c->dev, sizeof(struct wm8993_priv),
  1353. GFP_KERNEL);
  1354. if (wm8993 == NULL)
  1355. return -ENOMEM;
  1356. i2c_set_clientdata(i2c, wm8993);
  1357. ret = snd_soc_register_codec(&i2c->dev,
  1358. &soc_codec_dev_wm8993, &wm8993_dai, 1);
  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",
  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");