wm8993.c 45 KB

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