wm8993.c 44 KB

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