wm8993.c 45 KB

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