wm8903.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  1. /*
  2. * wm8903.c -- WM8903 ALSA SoC Audio driver
  3. *
  4. * Copyright 2008 Wolfson Microelectronics
  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. * TODO:
  13. * - TDM mode configuration.
  14. * - Mic detect.
  15. * - Digital microphone support.
  16. * - Interrupt support (mic detect and sequencer).
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/pm.h>
  23. #include <linux/i2c.h>
  24. #include <linux/platform_device.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/tlv.h>
  29. #include <sound/soc.h>
  30. #include <sound/soc-dapm.h>
  31. #include <sound/initval.h>
  32. #include "wm8903.h"
  33. /* Register defaults at reset */
  34. static u16 wm8903_reg_defaults[] = {
  35. 0x8903, /* R0 - SW Reset and ID */
  36. 0x0000, /* R1 - Revision Number */
  37. 0x0000, /* R2 */
  38. 0x0000, /* R3 */
  39. 0x0018, /* R4 - Bias Control 0 */
  40. 0x0000, /* R5 - VMID Control 0 */
  41. 0x0000, /* R6 - Mic Bias Control 0 */
  42. 0x0000, /* R7 */
  43. 0x0001, /* R8 - Analogue DAC 0 */
  44. 0x0000, /* R9 */
  45. 0x0001, /* R10 - Analogue ADC 0 */
  46. 0x0000, /* R11 */
  47. 0x0000, /* R12 - Power Management 0 */
  48. 0x0000, /* R13 - Power Management 1 */
  49. 0x0000, /* R14 - Power Management 2 */
  50. 0x0000, /* R15 - Power Management 3 */
  51. 0x0000, /* R16 - Power Management 4 */
  52. 0x0000, /* R17 - Power Management 5 */
  53. 0x0000, /* R18 - Power Management 6 */
  54. 0x0000, /* R19 */
  55. 0x0400, /* R20 - Clock Rates 0 */
  56. 0x0D07, /* R21 - Clock Rates 1 */
  57. 0x0000, /* R22 - Clock Rates 2 */
  58. 0x0000, /* R23 */
  59. 0x0050, /* R24 - Audio Interface 0 */
  60. 0x0242, /* R25 - Audio Interface 1 */
  61. 0x0008, /* R26 - Audio Interface 2 */
  62. 0x0022, /* R27 - Audio Interface 3 */
  63. 0x0000, /* R28 */
  64. 0x0000, /* R29 */
  65. 0x00C0, /* R30 - DAC Digital Volume Left */
  66. 0x00C0, /* R31 - DAC Digital Volume Right */
  67. 0x0000, /* R32 - DAC Digital 0 */
  68. 0x0000, /* R33 - DAC Digital 1 */
  69. 0x0000, /* R34 */
  70. 0x0000, /* R35 */
  71. 0x00C0, /* R36 - ADC Digital Volume Left */
  72. 0x00C0, /* R37 - ADC Digital Volume Right */
  73. 0x0000, /* R38 - ADC Digital 0 */
  74. 0x0073, /* R39 - Digital Microphone 0 */
  75. 0x09BF, /* R40 - DRC 0 */
  76. 0x3241, /* R41 - DRC 1 */
  77. 0x0020, /* R42 - DRC 2 */
  78. 0x0000, /* R43 - DRC 3 */
  79. 0x0085, /* R44 - Analogue Left Input 0 */
  80. 0x0085, /* R45 - Analogue Right Input 0 */
  81. 0x0044, /* R46 - Analogue Left Input 1 */
  82. 0x0044, /* R47 - Analogue Right Input 1 */
  83. 0x0000, /* R48 */
  84. 0x0000, /* R49 */
  85. 0x0008, /* R50 - Analogue Left Mix 0 */
  86. 0x0004, /* R51 - Analogue Right Mix 0 */
  87. 0x0000, /* R52 - Analogue Spk Mix Left 0 */
  88. 0x0000, /* R53 - Analogue Spk Mix Left 1 */
  89. 0x0000, /* R54 - Analogue Spk Mix Right 0 */
  90. 0x0000, /* R55 - Analogue Spk Mix Right 1 */
  91. 0x0000, /* R56 */
  92. 0x002D, /* R57 - Analogue OUT1 Left */
  93. 0x002D, /* R58 - Analogue OUT1 Right */
  94. 0x0039, /* R59 - Analogue OUT2 Left */
  95. 0x0039, /* R60 - Analogue OUT2 Right */
  96. 0x0100, /* R61 */
  97. 0x0139, /* R62 - Analogue OUT3 Left */
  98. 0x0139, /* R63 - Analogue OUT3 Right */
  99. 0x0000, /* R64 */
  100. 0x0000, /* R65 - Analogue SPK Output Control 0 */
  101. 0x0000, /* R66 */
  102. 0x0010, /* R67 - DC Servo 0 */
  103. 0x0100, /* R68 */
  104. 0x00A4, /* R69 - DC Servo 2 */
  105. 0x0807, /* R70 */
  106. 0x0000, /* R71 */
  107. 0x0000, /* R72 */
  108. 0x0000, /* R73 */
  109. 0x0000, /* R74 */
  110. 0x0000, /* R75 */
  111. 0x0000, /* R76 */
  112. 0x0000, /* R77 */
  113. 0x0000, /* R78 */
  114. 0x000E, /* R79 */
  115. 0x0000, /* R80 */
  116. 0x0000, /* R81 */
  117. 0x0000, /* R82 */
  118. 0x0000, /* R83 */
  119. 0x0000, /* R84 */
  120. 0x0000, /* R85 */
  121. 0x0000, /* R86 */
  122. 0x0006, /* R87 */
  123. 0x0000, /* R88 */
  124. 0x0000, /* R89 */
  125. 0x0000, /* R90 - Analogue HP 0 */
  126. 0x0060, /* R91 */
  127. 0x0000, /* R92 */
  128. 0x0000, /* R93 */
  129. 0x0000, /* R94 - Analogue Lineout 0 */
  130. 0x0060, /* R95 */
  131. 0x0000, /* R96 */
  132. 0x0000, /* R97 */
  133. 0x0000, /* R98 - Charge Pump 0 */
  134. 0x1F25, /* R99 */
  135. 0x2B19, /* R100 */
  136. 0x01C0, /* R101 */
  137. 0x01EF, /* R102 */
  138. 0x2B00, /* R103 */
  139. 0x0000, /* R104 - Class W 0 */
  140. 0x01C0, /* R105 */
  141. 0x1C10, /* R106 */
  142. 0x0000, /* R107 */
  143. 0x0000, /* R108 - Write Sequencer 0 */
  144. 0x0000, /* R109 - Write Sequencer 1 */
  145. 0x0000, /* R110 - Write Sequencer 2 */
  146. 0x0000, /* R111 - Write Sequencer 3 */
  147. 0x0000, /* R112 - Write Sequencer 4 */
  148. 0x0000, /* R113 */
  149. 0x0000, /* R114 - Control Interface */
  150. 0x0000, /* R115 */
  151. 0x00A8, /* R116 - GPIO Control 1 */
  152. 0x00A8, /* R117 - GPIO Control 2 */
  153. 0x00A8, /* R118 - GPIO Control 3 */
  154. 0x0220, /* R119 - GPIO Control 4 */
  155. 0x01A0, /* R120 - GPIO Control 5 */
  156. 0x0000, /* R121 - Interrupt Status 1 */
  157. 0xFFFF, /* R122 - Interrupt Status 1 Mask */
  158. 0x0000, /* R123 - Interrupt Polarity 1 */
  159. 0x0000, /* R124 */
  160. 0x0003, /* R125 */
  161. 0x0000, /* R126 - Interrupt Control */
  162. 0x0000, /* R127 */
  163. 0x0005, /* R128 */
  164. 0x0000, /* R129 - Control Interface Test 1 */
  165. 0x0000, /* R130 */
  166. 0x0000, /* R131 */
  167. 0x0000, /* R132 */
  168. 0x0000, /* R133 */
  169. 0x0000, /* R134 */
  170. 0x03FF, /* R135 */
  171. 0x0007, /* R136 */
  172. 0x0040, /* R137 */
  173. 0x0000, /* R138 */
  174. 0x0000, /* R139 */
  175. 0x0000, /* R140 */
  176. 0x0000, /* R141 */
  177. 0x0000, /* R142 */
  178. 0x0000, /* R143 */
  179. 0x0000, /* R144 */
  180. 0x0000, /* R145 */
  181. 0x0000, /* R146 */
  182. 0x0000, /* R147 */
  183. 0x4000, /* R148 */
  184. 0x6810, /* R149 - Charge Pump Test 1 */
  185. 0x0004, /* R150 */
  186. 0x0000, /* R151 */
  187. 0x0000, /* R152 */
  188. 0x0000, /* R153 */
  189. 0x0000, /* R154 */
  190. 0x0000, /* R155 */
  191. 0x0000, /* R156 */
  192. 0x0000, /* R157 */
  193. 0x0000, /* R158 */
  194. 0x0000, /* R159 */
  195. 0x0000, /* R160 */
  196. 0x0000, /* R161 */
  197. 0x0000, /* R162 */
  198. 0x0000, /* R163 */
  199. 0x0028, /* R164 - Clock Rate Test 4 */
  200. 0x0004, /* R165 */
  201. 0x0000, /* R166 */
  202. 0x0060, /* R167 */
  203. 0x0000, /* R168 */
  204. 0x0000, /* R169 */
  205. 0x0000, /* R170 */
  206. 0x0000, /* R171 */
  207. 0x0000, /* R172 - Analogue Output Bias 0 */
  208. };
  209. struct wm8903_priv {
  210. struct snd_soc_codec codec;
  211. u16 reg_cache[ARRAY_SIZE(wm8903_reg_defaults)];
  212. int sysclk;
  213. /* Reference counts */
  214. int class_w_users;
  215. int playback_active;
  216. int capture_active;
  217. struct snd_pcm_substream *master_substream;
  218. struct snd_pcm_substream *slave_substream;
  219. };
  220. static int wm8903_volatile_register(unsigned int reg)
  221. {
  222. switch (reg) {
  223. case WM8903_SW_RESET_AND_ID:
  224. case WM8903_REVISION_NUMBER:
  225. case WM8903_INTERRUPT_STATUS_1:
  226. case WM8903_WRITE_SEQUENCER_4:
  227. return 1;
  228. default:
  229. return 0;
  230. }
  231. }
  232. static int wm8903_run_sequence(struct snd_soc_codec *codec, unsigned int start)
  233. {
  234. u16 reg[5];
  235. struct i2c_client *i2c = codec->control_data;
  236. BUG_ON(start > 48);
  237. /* Enable the sequencer */
  238. reg[0] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_0);
  239. reg[0] |= WM8903_WSEQ_ENA;
  240. snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, reg[0]);
  241. dev_dbg(&i2c->dev, "Starting sequence at %d\n", start);
  242. snd_soc_write(codec, WM8903_WRITE_SEQUENCER_3,
  243. start | WM8903_WSEQ_START);
  244. /* Wait for it to complete. If we have the interrupt wired up then
  245. * we could block waiting for an interrupt, though polling may still
  246. * be desirable for diagnostic purposes.
  247. */
  248. do {
  249. msleep(10);
  250. reg[4] = snd_soc_read(codec, WM8903_WRITE_SEQUENCER_4);
  251. } while (reg[4] & WM8903_WSEQ_BUSY);
  252. dev_dbg(&i2c->dev, "Sequence complete\n");
  253. /* Disable the sequencer again */
  254. snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0,
  255. reg[0] & ~WM8903_WSEQ_ENA);
  256. return 0;
  257. }
  258. static void wm8903_sync_reg_cache(struct snd_soc_codec *codec, u16 *cache)
  259. {
  260. int i;
  261. /* There really ought to be something better we can do here :/ */
  262. for (i = 0; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
  263. cache[i] = codec->hw_read(codec, i);
  264. }
  265. static void wm8903_reset(struct snd_soc_codec *codec)
  266. {
  267. snd_soc_write(codec, WM8903_SW_RESET_AND_ID, 0);
  268. memcpy(codec->reg_cache, wm8903_reg_defaults,
  269. sizeof(wm8903_reg_defaults));
  270. }
  271. #define WM8903_OUTPUT_SHORT 0x8
  272. #define WM8903_OUTPUT_OUT 0x4
  273. #define WM8903_OUTPUT_INT 0x2
  274. #define WM8903_OUTPUT_IN 0x1
  275. static int wm8903_cp_event(struct snd_soc_dapm_widget *w,
  276. struct snd_kcontrol *kcontrol, int event)
  277. {
  278. WARN_ON(event != SND_SOC_DAPM_POST_PMU);
  279. mdelay(4);
  280. return 0;
  281. }
  282. /*
  283. * Event for headphone and line out amplifier power changes. Special
  284. * power up/down sequences are required in order to maximise pop/click
  285. * performance.
  286. */
  287. static int wm8903_output_event(struct snd_soc_dapm_widget *w,
  288. struct snd_kcontrol *kcontrol, int event)
  289. {
  290. struct snd_soc_codec *codec = w->codec;
  291. u16 val;
  292. u16 reg;
  293. u16 dcs_reg;
  294. u16 dcs_bit;
  295. int shift;
  296. switch (w->reg) {
  297. case WM8903_POWER_MANAGEMENT_2:
  298. reg = WM8903_ANALOGUE_HP_0;
  299. dcs_bit = 0 + w->shift;
  300. break;
  301. case WM8903_POWER_MANAGEMENT_3:
  302. reg = WM8903_ANALOGUE_LINEOUT_0;
  303. dcs_bit = 2 + w->shift;
  304. break;
  305. default:
  306. BUG();
  307. return -EINVAL; /* Spurious warning from some compilers */
  308. }
  309. switch (w->shift) {
  310. case 0:
  311. shift = 0;
  312. break;
  313. case 1:
  314. shift = 4;
  315. break;
  316. default:
  317. BUG();
  318. return -EINVAL; /* Spurious warning from some compilers */
  319. }
  320. if (event & SND_SOC_DAPM_PRE_PMU) {
  321. val = snd_soc_read(codec, reg);
  322. /* Short the output */
  323. val &= ~(WM8903_OUTPUT_SHORT << shift);
  324. snd_soc_write(codec, reg, val);
  325. }
  326. if (event & SND_SOC_DAPM_POST_PMU) {
  327. val = snd_soc_read(codec, reg);
  328. val |= (WM8903_OUTPUT_IN << shift);
  329. snd_soc_write(codec, reg, val);
  330. val |= (WM8903_OUTPUT_INT << shift);
  331. snd_soc_write(codec, reg, val);
  332. /* Turn on the output ENA_OUTP */
  333. val |= (WM8903_OUTPUT_OUT << shift);
  334. snd_soc_write(codec, reg, val);
  335. /* Enable the DC servo */
  336. dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
  337. dcs_reg |= dcs_bit;
  338. snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
  339. /* Remove the short */
  340. val |= (WM8903_OUTPUT_SHORT << shift);
  341. snd_soc_write(codec, reg, val);
  342. }
  343. if (event & SND_SOC_DAPM_PRE_PMD) {
  344. val = snd_soc_read(codec, reg);
  345. /* Short the output */
  346. val &= ~(WM8903_OUTPUT_SHORT << shift);
  347. snd_soc_write(codec, reg, val);
  348. /* Disable the DC servo */
  349. dcs_reg = snd_soc_read(codec, WM8903_DC_SERVO_0);
  350. dcs_reg &= ~dcs_bit;
  351. snd_soc_write(codec, WM8903_DC_SERVO_0, dcs_reg);
  352. /* Then disable the intermediate and output stages */
  353. val &= ~((WM8903_OUTPUT_OUT | WM8903_OUTPUT_INT |
  354. WM8903_OUTPUT_IN) << shift);
  355. snd_soc_write(codec, reg, val);
  356. }
  357. return 0;
  358. }
  359. /*
  360. * When used with DAC outputs only the WM8903 charge pump supports
  361. * operation in class W mode, providing very low power consumption
  362. * when used with digital sources. Enable and disable this mode
  363. * automatically depending on the mixer configuration.
  364. *
  365. * All the relevant controls are simple switches.
  366. */
  367. static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
  368. struct snd_ctl_elem_value *ucontrol)
  369. {
  370. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  371. struct snd_soc_codec *codec = widget->codec;
  372. struct wm8903_priv *wm8903 = codec->private_data;
  373. struct i2c_client *i2c = codec->control_data;
  374. u16 reg;
  375. int ret;
  376. reg = snd_soc_read(codec, WM8903_CLASS_W_0);
  377. /* Turn it off if we're about to enable bypass */
  378. if (ucontrol->value.integer.value[0]) {
  379. if (wm8903->class_w_users == 0) {
  380. dev_dbg(&i2c->dev, "Disabling Class W\n");
  381. snd_soc_write(codec, WM8903_CLASS_W_0, reg &
  382. ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
  383. }
  384. wm8903->class_w_users++;
  385. }
  386. /* Implement the change */
  387. ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
  388. /* If we've just disabled the last bypass path turn Class W on */
  389. if (!ucontrol->value.integer.value[0]) {
  390. if (wm8903->class_w_users == 1) {
  391. dev_dbg(&i2c->dev, "Enabling Class W\n");
  392. snd_soc_write(codec, WM8903_CLASS_W_0, reg |
  393. WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
  394. }
  395. wm8903->class_w_users--;
  396. }
  397. dev_dbg(&i2c->dev, "Bypass use count now %d\n",
  398. wm8903->class_w_users);
  399. return ret;
  400. }
  401. #define SOC_DAPM_SINGLE_W(xname, reg, shift, max, invert) \
  402. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  403. .info = snd_soc_info_volsw, \
  404. .get = snd_soc_dapm_get_volsw, .put = wm8903_class_w_put, \
  405. .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
  406. /* ALSA can only do steps of .01dB */
  407. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  408. static const DECLARE_TLV_DB_SCALE(digital_sidetone_tlv, -3600, 300, 0);
  409. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  410. static const DECLARE_TLV_DB_SCALE(drc_tlv_thresh, 0, 75, 0);
  411. static const DECLARE_TLV_DB_SCALE(drc_tlv_amp, -2250, 75, 0);
  412. static const DECLARE_TLV_DB_SCALE(drc_tlv_min, 0, 600, 0);
  413. static const DECLARE_TLV_DB_SCALE(drc_tlv_max, 1200, 600, 0);
  414. static const DECLARE_TLV_DB_SCALE(drc_tlv_startup, -300, 50, 0);
  415. static const char *drc_slope_text[] = {
  416. "1", "1/2", "1/4", "1/8", "1/16", "0"
  417. };
  418. static const struct soc_enum drc_slope_r0 =
  419. SOC_ENUM_SINGLE(WM8903_DRC_2, 3, 6, drc_slope_text);
  420. static const struct soc_enum drc_slope_r1 =
  421. SOC_ENUM_SINGLE(WM8903_DRC_2, 0, 6, drc_slope_text);
  422. static const char *drc_attack_text[] = {
  423. "instantaneous",
  424. "363us", "762us", "1.45ms", "2.9ms", "5.8ms", "11.6ms", "23.2ms",
  425. "46.4ms", "92.8ms", "185.6ms"
  426. };
  427. static const struct soc_enum drc_attack =
  428. SOC_ENUM_SINGLE(WM8903_DRC_1, 12, 11, drc_attack_text);
  429. static const char *drc_decay_text[] = {
  430. "186ms", "372ms", "743ms", "1.49s", "2.97s", "5.94s", "11.89s",
  431. "23.87s", "47.56s"
  432. };
  433. static const struct soc_enum drc_decay =
  434. SOC_ENUM_SINGLE(WM8903_DRC_1, 8, 9, drc_decay_text);
  435. static const char *drc_ff_delay_text[] = {
  436. "5 samples", "9 samples"
  437. };
  438. static const struct soc_enum drc_ff_delay =
  439. SOC_ENUM_SINGLE(WM8903_DRC_0, 5, 2, drc_ff_delay_text);
  440. static const char *drc_qr_decay_text[] = {
  441. "0.725ms", "1.45ms", "5.8ms"
  442. };
  443. static const struct soc_enum drc_qr_decay =
  444. SOC_ENUM_SINGLE(WM8903_DRC_1, 4, 3, drc_qr_decay_text);
  445. static const char *drc_smoothing_text[] = {
  446. "Low", "Medium", "High"
  447. };
  448. static const struct soc_enum drc_smoothing =
  449. SOC_ENUM_SINGLE(WM8903_DRC_0, 11, 3, drc_smoothing_text);
  450. static const char *soft_mute_text[] = {
  451. "Fast (fs/2)", "Slow (fs/32)"
  452. };
  453. static const struct soc_enum soft_mute =
  454. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 10, 2, soft_mute_text);
  455. static const char *mute_mode_text[] = {
  456. "Hard", "Soft"
  457. };
  458. static const struct soc_enum mute_mode =
  459. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 9, 2, mute_mode_text);
  460. static const char *dac_deemphasis_text[] = {
  461. "Disabled", "32kHz", "44.1kHz", "48kHz"
  462. };
  463. static const struct soc_enum dac_deemphasis =
  464. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 1, 4, dac_deemphasis_text);
  465. static const char *companding_text[] = {
  466. "ulaw", "alaw"
  467. };
  468. static const struct soc_enum dac_companding =
  469. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 0, 2, companding_text);
  470. static const struct soc_enum adc_companding =
  471. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 2, 2, companding_text);
  472. static const char *input_mode_text[] = {
  473. "Single-Ended", "Differential Line", "Differential Mic"
  474. };
  475. static const struct soc_enum linput_mode_enum =
  476. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
  477. static const struct soc_enum rinput_mode_enum =
  478. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
  479. static const char *linput_mux_text[] = {
  480. "IN1L", "IN2L", "IN3L"
  481. };
  482. static const struct soc_enum linput_enum =
  483. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 2, 3, linput_mux_text);
  484. static const struct soc_enum linput_inv_enum =
  485. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 4, 3, linput_mux_text);
  486. static const char *rinput_mux_text[] = {
  487. "IN1R", "IN2R", "IN3R"
  488. };
  489. static const struct soc_enum rinput_enum =
  490. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 2, 3, rinput_mux_text);
  491. static const struct soc_enum rinput_inv_enum =
  492. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 4, 3, rinput_mux_text);
  493. static const char *sidetone_text[] = {
  494. "None", "Left", "Right"
  495. };
  496. static const struct soc_enum lsidetone_enum =
  497. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 2, 3, sidetone_text);
  498. static const struct soc_enum rsidetone_enum =
  499. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 0, 3, sidetone_text);
  500. static const struct snd_kcontrol_new wm8903_snd_controls[] = {
  501. /* Input PGAs - No TLV since the scale depends on PGA mode */
  502. SOC_SINGLE("Left Input PGA Switch", WM8903_ANALOGUE_LEFT_INPUT_0,
  503. 7, 1, 1),
  504. SOC_SINGLE("Left Input PGA Volume", WM8903_ANALOGUE_LEFT_INPUT_0,
  505. 0, 31, 0),
  506. SOC_SINGLE("Left Input PGA Common Mode Switch", WM8903_ANALOGUE_LEFT_INPUT_1,
  507. 6, 1, 0),
  508. SOC_SINGLE("Right Input PGA Switch", WM8903_ANALOGUE_RIGHT_INPUT_0,
  509. 7, 1, 1),
  510. SOC_SINGLE("Right Input PGA Volume", WM8903_ANALOGUE_RIGHT_INPUT_0,
  511. 0, 31, 0),
  512. SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
  513. 6, 1, 0),
  514. /* ADCs */
  515. SOC_SINGLE("DRC Switch", WM8903_DRC_0, 15, 1, 0),
  516. SOC_ENUM("DRC Compressor Slope R0", drc_slope_r0),
  517. SOC_ENUM("DRC Compressor Slope R1", drc_slope_r1),
  518. SOC_SINGLE_TLV("DRC Compressor Threashold Volume", WM8903_DRC_3, 5, 124, 1,
  519. drc_tlv_thresh),
  520. SOC_SINGLE_TLV("DRC Volume", WM8903_DRC_3, 0, 30, 1, drc_tlv_amp),
  521. SOC_SINGLE_TLV("DRC Minimum Gain Volume", WM8903_DRC_1, 2, 3, 1, drc_tlv_min),
  522. SOC_SINGLE_TLV("DRC Maximum Gain Volume", WM8903_DRC_1, 0, 3, 0, drc_tlv_max),
  523. SOC_ENUM("DRC Attack Rate", drc_attack),
  524. SOC_ENUM("DRC Decay Rate", drc_decay),
  525. SOC_ENUM("DRC FF Delay", drc_ff_delay),
  526. SOC_SINGLE("DRC Anticlip Switch", WM8903_DRC_0, 1, 1, 0),
  527. SOC_SINGLE("DRC QR Switch", WM8903_DRC_0, 2, 1, 0),
  528. SOC_SINGLE_TLV("DRC QR Threashold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
  529. SOC_ENUM("DRC QR Decay Rate", drc_qr_decay),
  530. SOC_SINGLE("DRC Smoothing Switch", WM8903_DRC_0, 3, 1, 0),
  531. SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8903_DRC_0, 0, 1, 0),
  532. SOC_ENUM("DRC Smoothing Threashold", drc_smoothing),
  533. SOC_SINGLE_TLV("DRC Startup Volume", WM8903_DRC_0, 6, 18, 0, drc_tlv_startup),
  534. SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
  535. WM8903_ADC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
  536. SOC_ENUM("ADC Companding Mode", adc_companding),
  537. SOC_SINGLE("ADC Companding Switch", WM8903_AUDIO_INTERFACE_0, 3, 1, 0),
  538. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8903_DAC_DIGITAL_0, 4, 8,
  539. 12, 0, digital_sidetone_tlv),
  540. /* DAC */
  541. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
  542. WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
  543. SOC_ENUM("DAC Soft Mute Rate", soft_mute),
  544. SOC_ENUM("DAC Mute Mode", mute_mode),
  545. SOC_SINGLE("DAC Mono Switch", WM8903_DAC_DIGITAL_1, 12, 1, 0),
  546. SOC_ENUM("DAC De-emphasis", dac_deemphasis),
  547. SOC_ENUM("DAC Companding Mode", dac_companding),
  548. SOC_SINGLE("DAC Companding Switch", WM8903_AUDIO_INTERFACE_0, 1, 1, 0),
  549. /* Headphones */
  550. SOC_DOUBLE_R("Headphone Switch",
  551. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  552. 8, 1, 1),
  553. SOC_DOUBLE_R("Headphone ZC Switch",
  554. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  555. 6, 1, 0),
  556. SOC_DOUBLE_R_TLV("Headphone Volume",
  557. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  558. 0, 63, 0, out_tlv),
  559. /* Line out */
  560. SOC_DOUBLE_R("Line Out Switch",
  561. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  562. 8, 1, 1),
  563. SOC_DOUBLE_R("Line Out ZC Switch",
  564. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  565. 6, 1, 0),
  566. SOC_DOUBLE_R_TLV("Line Out Volume",
  567. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  568. 0, 63, 0, out_tlv),
  569. /* Speaker */
  570. SOC_DOUBLE_R("Speaker Switch",
  571. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 8, 1, 1),
  572. SOC_DOUBLE_R("Speaker ZC Switch",
  573. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 6, 1, 0),
  574. SOC_DOUBLE_R_TLV("Speaker Volume",
  575. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT,
  576. 0, 63, 0, out_tlv),
  577. };
  578. static const struct snd_kcontrol_new linput_mode_mux =
  579. SOC_DAPM_ENUM("Left Input Mode Mux", linput_mode_enum);
  580. static const struct snd_kcontrol_new rinput_mode_mux =
  581. SOC_DAPM_ENUM("Right Input Mode Mux", rinput_mode_enum);
  582. static const struct snd_kcontrol_new linput_mux =
  583. SOC_DAPM_ENUM("Left Input Mux", linput_enum);
  584. static const struct snd_kcontrol_new linput_inv_mux =
  585. SOC_DAPM_ENUM("Left Inverting Input Mux", linput_inv_enum);
  586. static const struct snd_kcontrol_new rinput_mux =
  587. SOC_DAPM_ENUM("Right Input Mux", rinput_enum);
  588. static const struct snd_kcontrol_new rinput_inv_mux =
  589. SOC_DAPM_ENUM("Right Inverting Input Mux", rinput_inv_enum);
  590. static const struct snd_kcontrol_new lsidetone_mux =
  591. SOC_DAPM_ENUM("DACL Sidetone Mux", lsidetone_enum);
  592. static const struct snd_kcontrol_new rsidetone_mux =
  593. SOC_DAPM_ENUM("DACR Sidetone Mux", rsidetone_enum);
  594. static const struct snd_kcontrol_new left_output_mixer[] = {
  595. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_LEFT_MIX_0, 3, 1, 0),
  596. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_LEFT_MIX_0, 2, 1, 0),
  597. SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
  598. SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 0, 1, 0),
  599. };
  600. static const struct snd_kcontrol_new right_output_mixer[] = {
  601. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 3, 1, 0),
  602. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 2, 1, 0),
  603. SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
  604. SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 0, 1, 0),
  605. };
  606. static const struct snd_kcontrol_new left_speaker_mixer[] = {
  607. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 3, 1, 0),
  608. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 2, 1, 0),
  609. SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 1, 1, 0),
  610. SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0,
  611. 0, 1, 0),
  612. };
  613. static const struct snd_kcontrol_new right_speaker_mixer[] = {
  614. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 3, 1, 0),
  615. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 2, 1, 0),
  616. SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
  617. 1, 1, 0),
  618. SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
  619. 0, 1, 0),
  620. };
  621. static const struct snd_soc_dapm_widget wm8903_dapm_widgets[] = {
  622. SND_SOC_DAPM_INPUT("IN1L"),
  623. SND_SOC_DAPM_INPUT("IN1R"),
  624. SND_SOC_DAPM_INPUT("IN2L"),
  625. SND_SOC_DAPM_INPUT("IN2R"),
  626. SND_SOC_DAPM_INPUT("IN3L"),
  627. SND_SOC_DAPM_INPUT("IN3R"),
  628. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  629. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  630. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  631. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  632. SND_SOC_DAPM_OUTPUT("LOP"),
  633. SND_SOC_DAPM_OUTPUT("LON"),
  634. SND_SOC_DAPM_OUTPUT("ROP"),
  635. SND_SOC_DAPM_OUTPUT("RON"),
  636. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8903_MIC_BIAS_CONTROL_0, 0, 0),
  637. SND_SOC_DAPM_MUX("Left Input Mux", SND_SOC_NOPM, 0, 0, &linput_mux),
  638. SND_SOC_DAPM_MUX("Left Input Inverting Mux", SND_SOC_NOPM, 0, 0,
  639. &linput_inv_mux),
  640. SND_SOC_DAPM_MUX("Left Input Mode Mux", SND_SOC_NOPM, 0, 0, &linput_mode_mux),
  641. SND_SOC_DAPM_MUX("Right Input Mux", SND_SOC_NOPM, 0, 0, &rinput_mux),
  642. SND_SOC_DAPM_MUX("Right Input Inverting Mux", SND_SOC_NOPM, 0, 0,
  643. &rinput_inv_mux),
  644. SND_SOC_DAPM_MUX("Right Input Mode Mux", SND_SOC_NOPM, 0, 0, &rinput_mode_mux),
  645. SND_SOC_DAPM_PGA("Left Input PGA", WM8903_POWER_MANAGEMENT_0, 1, 0, NULL, 0),
  646. SND_SOC_DAPM_PGA("Right Input PGA", WM8903_POWER_MANAGEMENT_0, 0, 0, NULL, 0),
  647. SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8903_POWER_MANAGEMENT_6, 1, 0),
  648. SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8903_POWER_MANAGEMENT_6, 0, 0),
  649. SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &lsidetone_mux),
  650. SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &rsidetone_mux),
  651. SND_SOC_DAPM_DAC("DACL", "Left Playback", WM8903_POWER_MANAGEMENT_6, 3, 0),
  652. SND_SOC_DAPM_DAC("DACR", "Right Playback", WM8903_POWER_MANAGEMENT_6, 2, 0),
  653. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8903_POWER_MANAGEMENT_1, 1, 0,
  654. left_output_mixer, ARRAY_SIZE(left_output_mixer)),
  655. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8903_POWER_MANAGEMENT_1, 0, 0,
  656. right_output_mixer, ARRAY_SIZE(right_output_mixer)),
  657. SND_SOC_DAPM_MIXER("Left Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 1, 0,
  658. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
  659. SND_SOC_DAPM_MIXER("Right Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 0, 0,
  660. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
  661. SND_SOC_DAPM_PGA_E("Left Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
  662. 1, 0, NULL, 0, wm8903_output_event,
  663. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  664. SND_SOC_DAPM_PRE_PMD),
  665. SND_SOC_DAPM_PGA_E("Right Headphone Output PGA", WM8903_POWER_MANAGEMENT_2,
  666. 0, 0, NULL, 0, wm8903_output_event,
  667. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  668. SND_SOC_DAPM_PRE_PMD),
  669. SND_SOC_DAPM_PGA_E("Left Line Output PGA", WM8903_POWER_MANAGEMENT_3, 1, 0,
  670. NULL, 0, wm8903_output_event,
  671. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  672. SND_SOC_DAPM_PRE_PMD),
  673. SND_SOC_DAPM_PGA_E("Right Line Output PGA", WM8903_POWER_MANAGEMENT_3, 0, 0,
  674. NULL, 0, wm8903_output_event,
  675. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  676. SND_SOC_DAPM_PRE_PMD),
  677. SND_SOC_DAPM_PGA("Left Speaker PGA", WM8903_POWER_MANAGEMENT_5, 1, 0,
  678. NULL, 0),
  679. SND_SOC_DAPM_PGA("Right Speaker PGA", WM8903_POWER_MANAGEMENT_5, 0, 0,
  680. NULL, 0),
  681. SND_SOC_DAPM_SUPPLY("Charge Pump", WM8903_CHARGE_PUMP_0, 0, 0,
  682. wm8903_cp_event, SND_SOC_DAPM_POST_PMU),
  683. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8903_CLOCK_RATES_2, 1, 0, NULL, 0),
  684. };
  685. static const struct snd_soc_dapm_route intercon[] = {
  686. { "Left Input Mux", "IN1L", "IN1L" },
  687. { "Left Input Mux", "IN2L", "IN2L" },
  688. { "Left Input Mux", "IN3L", "IN3L" },
  689. { "Left Input Inverting Mux", "IN1L", "IN1L" },
  690. { "Left Input Inverting Mux", "IN2L", "IN2L" },
  691. { "Left Input Inverting Mux", "IN3L", "IN3L" },
  692. { "Right Input Mux", "IN1R", "IN1R" },
  693. { "Right Input Mux", "IN2R", "IN2R" },
  694. { "Right Input Mux", "IN3R", "IN3R" },
  695. { "Right Input Inverting Mux", "IN1R", "IN1R" },
  696. { "Right Input Inverting Mux", "IN2R", "IN2R" },
  697. { "Right Input Inverting Mux", "IN3R", "IN3R" },
  698. { "Left Input Mode Mux", "Single-Ended", "Left Input Inverting Mux" },
  699. { "Left Input Mode Mux", "Differential Line",
  700. "Left Input Mux" },
  701. { "Left Input Mode Mux", "Differential Line",
  702. "Left Input Inverting Mux" },
  703. { "Left Input Mode Mux", "Differential Mic",
  704. "Left Input Mux" },
  705. { "Left Input Mode Mux", "Differential Mic",
  706. "Left Input Inverting Mux" },
  707. { "Right Input Mode Mux", "Single-Ended",
  708. "Right Input Inverting Mux" },
  709. { "Right Input Mode Mux", "Differential Line",
  710. "Right Input Mux" },
  711. { "Right Input Mode Mux", "Differential Line",
  712. "Right Input Inverting Mux" },
  713. { "Right Input Mode Mux", "Differential Mic",
  714. "Right Input Mux" },
  715. { "Right Input Mode Mux", "Differential Mic",
  716. "Right Input Inverting Mux" },
  717. { "Left Input PGA", NULL, "Left Input Mode Mux" },
  718. { "Right Input PGA", NULL, "Right Input Mode Mux" },
  719. { "ADCL", NULL, "Left Input PGA" },
  720. { "ADCL", NULL, "CLK_DSP" },
  721. { "ADCR", NULL, "Right Input PGA" },
  722. { "ADCR", NULL, "CLK_DSP" },
  723. { "DACL Sidetone", "Left", "ADCL" },
  724. { "DACL Sidetone", "Right", "ADCR" },
  725. { "DACR Sidetone", "Left", "ADCL" },
  726. { "DACR Sidetone", "Right", "ADCR" },
  727. { "DACL", NULL, "DACL Sidetone" },
  728. { "DACL", NULL, "CLK_DSP" },
  729. { "DACR", NULL, "DACR Sidetone" },
  730. { "DACR", NULL, "CLK_DSP" },
  731. { "Left Output Mixer", "Left Bypass Switch", "Left Input PGA" },
  732. { "Left Output Mixer", "Right Bypass Switch", "Right Input PGA" },
  733. { "Left Output Mixer", "DACL Switch", "DACL" },
  734. { "Left Output Mixer", "DACR Switch", "DACR" },
  735. { "Right Output Mixer", "Left Bypass Switch", "Left Input PGA" },
  736. { "Right Output Mixer", "Right Bypass Switch", "Right Input PGA" },
  737. { "Right Output Mixer", "DACL Switch", "DACL" },
  738. { "Right Output Mixer", "DACR Switch", "DACR" },
  739. { "Left Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
  740. { "Left Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
  741. { "Left Speaker Mixer", "DACL Switch", "DACL" },
  742. { "Left Speaker Mixer", "DACR Switch", "DACR" },
  743. { "Right Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
  744. { "Right Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
  745. { "Right Speaker Mixer", "DACL Switch", "DACL" },
  746. { "Right Speaker Mixer", "DACR Switch", "DACR" },
  747. { "Left Line Output PGA", NULL, "Left Output Mixer" },
  748. { "Right Line Output PGA", NULL, "Right Output Mixer" },
  749. { "Left Headphone Output PGA", NULL, "Left Output Mixer" },
  750. { "Right Headphone Output PGA", NULL, "Right Output Mixer" },
  751. { "Left Speaker PGA", NULL, "Left Speaker Mixer" },
  752. { "Right Speaker PGA", NULL, "Right Speaker Mixer" },
  753. { "HPOUTL", NULL, "Left Headphone Output PGA" },
  754. { "HPOUTR", NULL, "Right Headphone Output PGA" },
  755. { "LINEOUTL", NULL, "Left Line Output PGA" },
  756. { "LINEOUTR", NULL, "Right Line Output PGA" },
  757. { "LOP", NULL, "Left Speaker PGA" },
  758. { "LON", NULL, "Left Speaker PGA" },
  759. { "ROP", NULL, "Right Speaker PGA" },
  760. { "RON", NULL, "Right Speaker PGA" },
  761. { "Left Headphone Output PGA", NULL, "Charge Pump" },
  762. { "Right Headphone Output PGA", NULL, "Charge Pump" },
  763. { "Left Line Output PGA", NULL, "Charge Pump" },
  764. { "Right Line Output PGA", NULL, "Charge Pump" },
  765. };
  766. static int wm8903_add_widgets(struct snd_soc_codec *codec)
  767. {
  768. snd_soc_dapm_new_controls(codec, wm8903_dapm_widgets,
  769. ARRAY_SIZE(wm8903_dapm_widgets));
  770. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  771. snd_soc_dapm_new_widgets(codec);
  772. return 0;
  773. }
  774. static int wm8903_set_bias_level(struct snd_soc_codec *codec,
  775. enum snd_soc_bias_level level)
  776. {
  777. struct i2c_client *i2c = codec->control_data;
  778. u16 reg, reg2;
  779. switch (level) {
  780. case SND_SOC_BIAS_ON:
  781. case SND_SOC_BIAS_PREPARE:
  782. reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
  783. reg &= ~(WM8903_VMID_RES_MASK);
  784. reg |= WM8903_VMID_RES_50K;
  785. snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
  786. break;
  787. case SND_SOC_BIAS_STANDBY:
  788. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  789. snd_soc_write(codec, WM8903_CLOCK_RATES_2,
  790. WM8903_CLK_SYS_ENA);
  791. /* Change DC servo dither level in startup sequence */
  792. snd_soc_write(codec, WM8903_WRITE_SEQUENCER_0, 0x11);
  793. snd_soc_write(codec, WM8903_WRITE_SEQUENCER_1, 0x1257);
  794. snd_soc_write(codec, WM8903_WRITE_SEQUENCER_2, 0x2);
  795. wm8903_run_sequence(codec, 0);
  796. wm8903_sync_reg_cache(codec, codec->reg_cache);
  797. /* Enable low impedence charge pump output */
  798. reg = snd_soc_read(codec,
  799. WM8903_CONTROL_INTERFACE_TEST_1);
  800. snd_soc_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
  801. reg | WM8903_TEST_KEY);
  802. reg2 = snd_soc_read(codec, WM8903_CHARGE_PUMP_TEST_1);
  803. snd_soc_write(codec, WM8903_CHARGE_PUMP_TEST_1,
  804. reg2 | WM8903_CP_SW_KELVIN_MODE_MASK);
  805. snd_soc_write(codec, WM8903_CONTROL_INTERFACE_TEST_1,
  806. reg);
  807. /* By default no bypass paths are enabled so
  808. * enable Class W support.
  809. */
  810. dev_dbg(&i2c->dev, "Enabling Class W\n");
  811. snd_soc_write(codec, WM8903_CLASS_W_0, reg |
  812. WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
  813. }
  814. reg = snd_soc_read(codec, WM8903_VMID_CONTROL_0);
  815. reg &= ~(WM8903_VMID_RES_MASK);
  816. reg |= WM8903_VMID_RES_250K;
  817. snd_soc_write(codec, WM8903_VMID_CONTROL_0, reg);
  818. break;
  819. case SND_SOC_BIAS_OFF:
  820. wm8903_run_sequence(codec, 32);
  821. reg = snd_soc_read(codec, WM8903_CLOCK_RATES_2);
  822. reg &= ~WM8903_CLK_SYS_ENA;
  823. snd_soc_write(codec, WM8903_CLOCK_RATES_2, reg);
  824. break;
  825. }
  826. codec->bias_level = level;
  827. return 0;
  828. }
  829. static int wm8903_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  830. int clk_id, unsigned int freq, int dir)
  831. {
  832. struct snd_soc_codec *codec = codec_dai->codec;
  833. struct wm8903_priv *wm8903 = codec->private_data;
  834. wm8903->sysclk = freq;
  835. return 0;
  836. }
  837. static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
  838. unsigned int fmt)
  839. {
  840. struct snd_soc_codec *codec = codec_dai->codec;
  841. u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
  842. aif1 &= ~(WM8903_LRCLK_DIR | WM8903_BCLK_DIR | WM8903_AIF_FMT_MASK |
  843. WM8903_AIF_LRCLK_INV | WM8903_AIF_BCLK_INV);
  844. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  845. case SND_SOC_DAIFMT_CBS_CFS:
  846. break;
  847. case SND_SOC_DAIFMT_CBS_CFM:
  848. aif1 |= WM8903_LRCLK_DIR;
  849. break;
  850. case SND_SOC_DAIFMT_CBM_CFM:
  851. aif1 |= WM8903_LRCLK_DIR | WM8903_BCLK_DIR;
  852. break;
  853. case SND_SOC_DAIFMT_CBM_CFS:
  854. aif1 |= WM8903_BCLK_DIR;
  855. break;
  856. default:
  857. return -EINVAL;
  858. }
  859. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  860. case SND_SOC_DAIFMT_DSP_A:
  861. aif1 |= 0x3;
  862. break;
  863. case SND_SOC_DAIFMT_DSP_B:
  864. aif1 |= 0x3 | WM8903_AIF_LRCLK_INV;
  865. break;
  866. case SND_SOC_DAIFMT_I2S:
  867. aif1 |= 0x2;
  868. break;
  869. case SND_SOC_DAIFMT_RIGHT_J:
  870. aif1 |= 0x1;
  871. break;
  872. case SND_SOC_DAIFMT_LEFT_J:
  873. break;
  874. default:
  875. return -EINVAL;
  876. }
  877. /* Clock inversion */
  878. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  879. case SND_SOC_DAIFMT_DSP_A:
  880. case SND_SOC_DAIFMT_DSP_B:
  881. /* frame inversion not valid for DSP modes */
  882. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  883. case SND_SOC_DAIFMT_NB_NF:
  884. break;
  885. case SND_SOC_DAIFMT_IB_NF:
  886. aif1 |= WM8903_AIF_BCLK_INV;
  887. break;
  888. default:
  889. return -EINVAL;
  890. }
  891. break;
  892. case SND_SOC_DAIFMT_I2S:
  893. case SND_SOC_DAIFMT_RIGHT_J:
  894. case SND_SOC_DAIFMT_LEFT_J:
  895. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  896. case SND_SOC_DAIFMT_NB_NF:
  897. break;
  898. case SND_SOC_DAIFMT_IB_IF:
  899. aif1 |= WM8903_AIF_BCLK_INV | WM8903_AIF_LRCLK_INV;
  900. break;
  901. case SND_SOC_DAIFMT_IB_NF:
  902. aif1 |= WM8903_AIF_BCLK_INV;
  903. break;
  904. case SND_SOC_DAIFMT_NB_IF:
  905. aif1 |= WM8903_AIF_LRCLK_INV;
  906. break;
  907. default:
  908. return -EINVAL;
  909. }
  910. break;
  911. default:
  912. return -EINVAL;
  913. }
  914. snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
  915. return 0;
  916. }
  917. static int wm8903_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  918. {
  919. struct snd_soc_codec *codec = codec_dai->codec;
  920. u16 reg;
  921. reg = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
  922. if (mute)
  923. reg |= WM8903_DAC_MUTE;
  924. else
  925. reg &= ~WM8903_DAC_MUTE;
  926. snd_soc_write(codec, WM8903_DAC_DIGITAL_1, reg);
  927. return 0;
  928. }
  929. /* Lookup table for CLK_SYS/fs ratio. 256fs or more is recommended
  930. * for optimal performance so we list the lower rates first and match
  931. * on the last match we find. */
  932. static struct {
  933. int div;
  934. int rate;
  935. int mode;
  936. int mclk_div;
  937. } clk_sys_ratios[] = {
  938. { 64, 0x0, 0x0, 1 },
  939. { 68, 0x0, 0x1, 1 },
  940. { 125, 0x0, 0x2, 1 },
  941. { 128, 0x1, 0x0, 1 },
  942. { 136, 0x1, 0x1, 1 },
  943. { 192, 0x2, 0x0, 1 },
  944. { 204, 0x2, 0x1, 1 },
  945. { 64, 0x0, 0x0, 2 },
  946. { 68, 0x0, 0x1, 2 },
  947. { 125, 0x0, 0x2, 2 },
  948. { 128, 0x1, 0x0, 2 },
  949. { 136, 0x1, 0x1, 2 },
  950. { 192, 0x2, 0x0, 2 },
  951. { 204, 0x2, 0x1, 2 },
  952. { 250, 0x2, 0x2, 1 },
  953. { 256, 0x3, 0x0, 1 },
  954. { 272, 0x3, 0x1, 1 },
  955. { 384, 0x4, 0x0, 1 },
  956. { 408, 0x4, 0x1, 1 },
  957. { 375, 0x4, 0x2, 1 },
  958. { 512, 0x5, 0x0, 1 },
  959. { 544, 0x5, 0x1, 1 },
  960. { 500, 0x5, 0x2, 1 },
  961. { 768, 0x6, 0x0, 1 },
  962. { 816, 0x6, 0x1, 1 },
  963. { 750, 0x6, 0x2, 1 },
  964. { 1024, 0x7, 0x0, 1 },
  965. { 1088, 0x7, 0x1, 1 },
  966. { 1000, 0x7, 0x2, 1 },
  967. { 1408, 0x8, 0x0, 1 },
  968. { 1496, 0x8, 0x1, 1 },
  969. { 1536, 0x9, 0x0, 1 },
  970. { 1632, 0x9, 0x1, 1 },
  971. { 1500, 0x9, 0x2, 1 },
  972. { 250, 0x2, 0x2, 2 },
  973. { 256, 0x3, 0x0, 2 },
  974. { 272, 0x3, 0x1, 2 },
  975. { 384, 0x4, 0x0, 2 },
  976. { 408, 0x4, 0x1, 2 },
  977. { 375, 0x4, 0x2, 2 },
  978. { 512, 0x5, 0x0, 2 },
  979. { 544, 0x5, 0x1, 2 },
  980. { 500, 0x5, 0x2, 2 },
  981. { 768, 0x6, 0x0, 2 },
  982. { 816, 0x6, 0x1, 2 },
  983. { 750, 0x6, 0x2, 2 },
  984. { 1024, 0x7, 0x0, 2 },
  985. { 1088, 0x7, 0x1, 2 },
  986. { 1000, 0x7, 0x2, 2 },
  987. { 1408, 0x8, 0x0, 2 },
  988. { 1496, 0x8, 0x1, 2 },
  989. { 1536, 0x9, 0x0, 2 },
  990. { 1632, 0x9, 0x1, 2 },
  991. { 1500, 0x9, 0x2, 2 },
  992. };
  993. /* CLK_SYS/BCLK ratios - multiplied by 10 due to .5s */
  994. static struct {
  995. int ratio;
  996. int div;
  997. } bclk_divs[] = {
  998. { 10, 0 },
  999. { 20, 2 },
  1000. { 30, 3 },
  1001. { 40, 4 },
  1002. { 50, 5 },
  1003. { 60, 7 },
  1004. { 80, 8 },
  1005. { 100, 9 },
  1006. { 120, 11 },
  1007. { 160, 12 },
  1008. { 200, 13 },
  1009. { 220, 14 },
  1010. { 240, 15 },
  1011. { 300, 17 },
  1012. { 320, 18 },
  1013. { 440, 19 },
  1014. { 480, 20 },
  1015. };
  1016. /* Sample rates for DSP */
  1017. static struct {
  1018. int rate;
  1019. int value;
  1020. } sample_rates[] = {
  1021. { 8000, 0 },
  1022. { 11025, 1 },
  1023. { 12000, 2 },
  1024. { 16000, 3 },
  1025. { 22050, 4 },
  1026. { 24000, 5 },
  1027. { 32000, 6 },
  1028. { 44100, 7 },
  1029. { 48000, 8 },
  1030. { 88200, 9 },
  1031. { 96000, 10 },
  1032. { 0, 0 },
  1033. };
  1034. static int wm8903_startup(struct snd_pcm_substream *substream,
  1035. struct snd_soc_dai *dai)
  1036. {
  1037. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1038. struct snd_soc_device *socdev = rtd->socdev;
  1039. struct snd_soc_codec *codec = socdev->card->codec;
  1040. struct wm8903_priv *wm8903 = codec->private_data;
  1041. struct i2c_client *i2c = codec->control_data;
  1042. struct snd_pcm_runtime *master_runtime;
  1043. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1044. wm8903->playback_active++;
  1045. else
  1046. wm8903->capture_active++;
  1047. /* The DAI has shared clocks so if we already have a playback or
  1048. * capture going then constrain this substream to match it.
  1049. */
  1050. if (wm8903->master_substream) {
  1051. master_runtime = wm8903->master_substream->runtime;
  1052. dev_dbg(&i2c->dev, "Constraining to %d bits\n",
  1053. master_runtime->sample_bits);
  1054. snd_pcm_hw_constraint_minmax(substream->runtime,
  1055. SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1056. master_runtime->sample_bits,
  1057. master_runtime->sample_bits);
  1058. wm8903->slave_substream = substream;
  1059. } else
  1060. wm8903->master_substream = substream;
  1061. return 0;
  1062. }
  1063. static void wm8903_shutdown(struct snd_pcm_substream *substream,
  1064. struct snd_soc_dai *dai)
  1065. {
  1066. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1067. struct snd_soc_device *socdev = rtd->socdev;
  1068. struct snd_soc_codec *codec = socdev->card->codec;
  1069. struct wm8903_priv *wm8903 = codec->private_data;
  1070. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1071. wm8903->playback_active--;
  1072. else
  1073. wm8903->capture_active--;
  1074. if (wm8903->master_substream == substream)
  1075. wm8903->master_substream = wm8903->slave_substream;
  1076. wm8903->slave_substream = NULL;
  1077. }
  1078. static int wm8903_hw_params(struct snd_pcm_substream *substream,
  1079. struct snd_pcm_hw_params *params,
  1080. struct snd_soc_dai *dai)
  1081. {
  1082. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1083. struct snd_soc_device *socdev = rtd->socdev;
  1084. struct snd_soc_codec *codec = socdev->card->codec;
  1085. struct wm8903_priv *wm8903 = codec->private_data;
  1086. struct i2c_client *i2c = codec->control_data;
  1087. int fs = params_rate(params);
  1088. int bclk;
  1089. int bclk_div;
  1090. int i;
  1091. int dsp_config;
  1092. int clk_config;
  1093. int best_val;
  1094. int cur_val;
  1095. int clk_sys;
  1096. u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
  1097. u16 aif2 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_2);
  1098. u16 aif3 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_3);
  1099. u16 clock0 = snd_soc_read(codec, WM8903_CLOCK_RATES_0);
  1100. u16 clock1 = snd_soc_read(codec, WM8903_CLOCK_RATES_1);
  1101. u16 dac_digital1 = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
  1102. if (substream == wm8903->slave_substream) {
  1103. dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
  1104. return 0;
  1105. }
  1106. /* Enable sloping stopband filter for low sample rates */
  1107. if (fs <= 24000)
  1108. dac_digital1 |= WM8903_DAC_SB_FILT;
  1109. else
  1110. dac_digital1 &= ~WM8903_DAC_SB_FILT;
  1111. /* Configure sample rate logic for DSP - choose nearest rate */
  1112. dsp_config = 0;
  1113. best_val = abs(sample_rates[dsp_config].rate - fs);
  1114. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1115. cur_val = abs(sample_rates[i].rate - fs);
  1116. if (cur_val <= best_val) {
  1117. dsp_config = i;
  1118. best_val = cur_val;
  1119. }
  1120. }
  1121. /* Constraints should stop us hitting this but let's make sure */
  1122. if (wm8903->capture_active)
  1123. switch (sample_rates[dsp_config].rate) {
  1124. case 88200:
  1125. case 96000:
  1126. dev_err(&i2c->dev, "%dHz unsupported by ADC\n",
  1127. fs);
  1128. return -EINVAL;
  1129. default:
  1130. break;
  1131. }
  1132. dev_dbg(&i2c->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
  1133. clock1 &= ~WM8903_SAMPLE_RATE_MASK;
  1134. clock1 |= sample_rates[dsp_config].value;
  1135. aif1 &= ~WM8903_AIF_WL_MASK;
  1136. bclk = 2 * fs;
  1137. switch (params_format(params)) {
  1138. case SNDRV_PCM_FORMAT_S16_LE:
  1139. bclk *= 16;
  1140. break;
  1141. case SNDRV_PCM_FORMAT_S20_3LE:
  1142. bclk *= 20;
  1143. aif1 |= 0x4;
  1144. break;
  1145. case SNDRV_PCM_FORMAT_S24_LE:
  1146. bclk *= 24;
  1147. aif1 |= 0x8;
  1148. break;
  1149. case SNDRV_PCM_FORMAT_S32_LE:
  1150. bclk *= 32;
  1151. aif1 |= 0xc;
  1152. break;
  1153. default:
  1154. return -EINVAL;
  1155. }
  1156. dev_dbg(&i2c->dev, "MCLK = %dHz, target sample rate = %dHz\n",
  1157. wm8903->sysclk, fs);
  1158. /* We may not have an MCLK which allows us to generate exactly
  1159. * the clock we want, particularly with USB derived inputs, so
  1160. * approximate.
  1161. */
  1162. clk_config = 0;
  1163. best_val = abs((wm8903->sysclk /
  1164. (clk_sys_ratios[0].mclk_div *
  1165. clk_sys_ratios[0].div)) - fs);
  1166. for (i = 1; i < ARRAY_SIZE(clk_sys_ratios); i++) {
  1167. cur_val = abs((wm8903->sysclk /
  1168. (clk_sys_ratios[i].mclk_div *
  1169. clk_sys_ratios[i].div)) - fs);
  1170. if (cur_val <= best_val) {
  1171. clk_config = i;
  1172. best_val = cur_val;
  1173. }
  1174. }
  1175. if (clk_sys_ratios[clk_config].mclk_div == 2) {
  1176. clock0 |= WM8903_MCLKDIV2;
  1177. clk_sys = wm8903->sysclk / 2;
  1178. } else {
  1179. clock0 &= ~WM8903_MCLKDIV2;
  1180. clk_sys = wm8903->sysclk;
  1181. }
  1182. clock1 &= ~(WM8903_CLK_SYS_RATE_MASK |
  1183. WM8903_CLK_SYS_MODE_MASK);
  1184. clock1 |= clk_sys_ratios[clk_config].rate << WM8903_CLK_SYS_RATE_SHIFT;
  1185. clock1 |= clk_sys_ratios[clk_config].mode << WM8903_CLK_SYS_MODE_SHIFT;
  1186. dev_dbg(&i2c->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
  1187. clk_sys_ratios[clk_config].rate,
  1188. clk_sys_ratios[clk_config].mode,
  1189. clk_sys_ratios[clk_config].div);
  1190. dev_dbg(&i2c->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
  1191. /* We may not get quite the right frequency if using
  1192. * approximate clocks so look for the closest match that is
  1193. * higher than the target (we need to ensure that there enough
  1194. * BCLKs to clock out the samples).
  1195. */
  1196. bclk_div = 0;
  1197. best_val = ((clk_sys * 10) / bclk_divs[0].ratio) - bclk;
  1198. i = 1;
  1199. while (i < ARRAY_SIZE(bclk_divs)) {
  1200. cur_val = ((clk_sys * 10) / bclk_divs[i].ratio) - bclk;
  1201. if (cur_val < 0) /* BCLK table is sorted */
  1202. break;
  1203. bclk_div = i;
  1204. best_val = cur_val;
  1205. i++;
  1206. }
  1207. aif2 &= ~WM8903_BCLK_DIV_MASK;
  1208. aif3 &= ~WM8903_LRCLK_RATE_MASK;
  1209. dev_dbg(&i2c->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
  1210. bclk_divs[bclk_div].ratio / 10, bclk,
  1211. (clk_sys * 10) / bclk_divs[bclk_div].ratio);
  1212. aif2 |= bclk_divs[bclk_div].div;
  1213. aif3 |= bclk / fs;
  1214. snd_soc_write(codec, WM8903_CLOCK_RATES_0, clock0);
  1215. snd_soc_write(codec, WM8903_CLOCK_RATES_1, clock1);
  1216. snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
  1217. snd_soc_write(codec, WM8903_AUDIO_INTERFACE_2, aif2);
  1218. snd_soc_write(codec, WM8903_AUDIO_INTERFACE_3, aif3);
  1219. snd_soc_write(codec, WM8903_DAC_DIGITAL_1, dac_digital1);
  1220. return 0;
  1221. }
  1222. #define WM8903_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
  1223. SNDRV_PCM_RATE_11025 | \
  1224. SNDRV_PCM_RATE_16000 | \
  1225. SNDRV_PCM_RATE_22050 | \
  1226. SNDRV_PCM_RATE_32000 | \
  1227. SNDRV_PCM_RATE_44100 | \
  1228. SNDRV_PCM_RATE_48000 | \
  1229. SNDRV_PCM_RATE_88200 | \
  1230. SNDRV_PCM_RATE_96000)
  1231. #define WM8903_CAPTURE_RATES (SNDRV_PCM_RATE_8000 |\
  1232. SNDRV_PCM_RATE_11025 | \
  1233. SNDRV_PCM_RATE_16000 | \
  1234. SNDRV_PCM_RATE_22050 | \
  1235. SNDRV_PCM_RATE_32000 | \
  1236. SNDRV_PCM_RATE_44100 | \
  1237. SNDRV_PCM_RATE_48000)
  1238. #define WM8903_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  1239. SNDRV_PCM_FMTBIT_S20_3LE |\
  1240. SNDRV_PCM_FMTBIT_S24_LE)
  1241. static struct snd_soc_dai_ops wm8903_dai_ops = {
  1242. .startup = wm8903_startup,
  1243. .shutdown = wm8903_shutdown,
  1244. .hw_params = wm8903_hw_params,
  1245. .digital_mute = wm8903_digital_mute,
  1246. .set_fmt = wm8903_set_dai_fmt,
  1247. .set_sysclk = wm8903_set_dai_sysclk,
  1248. };
  1249. struct snd_soc_dai wm8903_dai = {
  1250. .name = "WM8903",
  1251. .playback = {
  1252. .stream_name = "Playback",
  1253. .channels_min = 2,
  1254. .channels_max = 2,
  1255. .rates = WM8903_PLAYBACK_RATES,
  1256. .formats = WM8903_FORMATS,
  1257. },
  1258. .capture = {
  1259. .stream_name = "Capture",
  1260. .channels_min = 2,
  1261. .channels_max = 2,
  1262. .rates = WM8903_CAPTURE_RATES,
  1263. .formats = WM8903_FORMATS,
  1264. },
  1265. .ops = &wm8903_dai_ops,
  1266. .symmetric_rates = 1,
  1267. };
  1268. EXPORT_SYMBOL_GPL(wm8903_dai);
  1269. static int wm8903_suspend(struct platform_device *pdev, pm_message_t state)
  1270. {
  1271. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1272. struct snd_soc_codec *codec = socdev->card->codec;
  1273. wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1274. return 0;
  1275. }
  1276. static int wm8903_resume(struct platform_device *pdev)
  1277. {
  1278. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1279. struct snd_soc_codec *codec = socdev->card->codec;
  1280. struct i2c_client *i2c = codec->control_data;
  1281. int i;
  1282. u16 *reg_cache = codec->reg_cache;
  1283. u16 *tmp_cache = kmemdup(codec->reg_cache, sizeof(wm8903_reg_defaults),
  1284. GFP_KERNEL);
  1285. /* Bring the codec back up to standby first to minimise pop/clicks */
  1286. wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1287. wm8903_set_bias_level(codec, codec->suspend_bias_level);
  1288. /* Sync back everything else */
  1289. if (tmp_cache) {
  1290. for (i = 2; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
  1291. if (tmp_cache[i] != reg_cache[i])
  1292. snd_soc_write(codec, i, tmp_cache[i]);
  1293. } else {
  1294. dev_err(&i2c->dev, "Failed to allocate temporary cache\n");
  1295. }
  1296. return 0;
  1297. }
  1298. static struct snd_soc_codec *wm8903_codec;
  1299. static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
  1300. const struct i2c_device_id *id)
  1301. {
  1302. struct wm8903_priv *wm8903;
  1303. struct snd_soc_codec *codec;
  1304. int ret;
  1305. u16 val;
  1306. wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
  1307. if (wm8903 == NULL)
  1308. return -ENOMEM;
  1309. codec = &wm8903->codec;
  1310. mutex_init(&codec->mutex);
  1311. INIT_LIST_HEAD(&codec->dapm_widgets);
  1312. INIT_LIST_HEAD(&codec->dapm_paths);
  1313. codec->dev = &i2c->dev;
  1314. codec->name = "WM8903";
  1315. codec->owner = THIS_MODULE;
  1316. codec->bias_level = SND_SOC_BIAS_OFF;
  1317. codec->set_bias_level = wm8903_set_bias_level;
  1318. codec->dai = &wm8903_dai;
  1319. codec->num_dai = 1;
  1320. codec->reg_cache_size = ARRAY_SIZE(wm8903->reg_cache);
  1321. codec->reg_cache = &wm8903->reg_cache[0];
  1322. codec->private_data = wm8903;
  1323. codec->volatile_register = wm8903_volatile_register;
  1324. i2c_set_clientdata(i2c, codec);
  1325. codec->control_data = i2c;
  1326. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  1327. if (ret != 0) {
  1328. dev_err(&i2c->dev, "Failed to set cache I/O: %d\n", ret);
  1329. goto err;
  1330. }
  1331. val = snd_soc_read(codec, WM8903_SW_RESET_AND_ID);
  1332. if (val != wm8903_reg_defaults[WM8903_SW_RESET_AND_ID]) {
  1333. dev_err(&i2c->dev,
  1334. "Device with ID register %x is not a WM8903\n", val);
  1335. return -ENODEV;
  1336. }
  1337. val = snd_soc_read(codec, WM8903_REVISION_NUMBER);
  1338. dev_info(&i2c->dev, "WM8903 revision %d\n",
  1339. val & WM8903_CHIP_REV_MASK);
  1340. wm8903_reset(codec);
  1341. /* power on device */
  1342. wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1343. /* Latch volume update bits */
  1344. val = snd_soc_read(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT);
  1345. val |= WM8903_ADCVU;
  1346. snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT, val);
  1347. snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_RIGHT, val);
  1348. val = snd_soc_read(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT);
  1349. val |= WM8903_DACVU;
  1350. snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT, val);
  1351. snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_RIGHT, val);
  1352. val = snd_soc_read(codec, WM8903_ANALOGUE_OUT1_LEFT);
  1353. val |= WM8903_HPOUTVU;
  1354. snd_soc_write(codec, WM8903_ANALOGUE_OUT1_LEFT, val);
  1355. snd_soc_write(codec, WM8903_ANALOGUE_OUT1_RIGHT, val);
  1356. val = snd_soc_read(codec, WM8903_ANALOGUE_OUT2_LEFT);
  1357. val |= WM8903_LINEOUTVU;
  1358. snd_soc_write(codec, WM8903_ANALOGUE_OUT2_LEFT, val);
  1359. snd_soc_write(codec, WM8903_ANALOGUE_OUT2_RIGHT, val);
  1360. val = snd_soc_read(codec, WM8903_ANALOGUE_OUT3_LEFT);
  1361. val |= WM8903_SPKVU;
  1362. snd_soc_write(codec, WM8903_ANALOGUE_OUT3_LEFT, val);
  1363. snd_soc_write(codec, WM8903_ANALOGUE_OUT3_RIGHT, val);
  1364. /* Enable DAC soft mute by default */
  1365. val = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
  1366. val |= WM8903_DAC_MUTEMODE;
  1367. snd_soc_write(codec, WM8903_DAC_DIGITAL_1, val);
  1368. wm8903_dai.dev = &i2c->dev;
  1369. wm8903_codec = codec;
  1370. ret = snd_soc_register_codec(codec);
  1371. if (ret != 0) {
  1372. dev_err(&i2c->dev, "Failed to register codec: %d\n", ret);
  1373. goto err;
  1374. }
  1375. ret = snd_soc_register_dai(&wm8903_dai);
  1376. if (ret != 0) {
  1377. dev_err(&i2c->dev, "Failed to register DAI: %d\n", ret);
  1378. goto err_codec;
  1379. }
  1380. return ret;
  1381. err_codec:
  1382. snd_soc_unregister_codec(codec);
  1383. err:
  1384. wm8903_codec = NULL;
  1385. kfree(wm8903);
  1386. return ret;
  1387. }
  1388. static __devexit int wm8903_i2c_remove(struct i2c_client *client)
  1389. {
  1390. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  1391. snd_soc_unregister_dai(&wm8903_dai);
  1392. snd_soc_unregister_codec(codec);
  1393. wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1394. kfree(codec->private_data);
  1395. wm8903_codec = NULL;
  1396. wm8903_dai.dev = NULL;
  1397. return 0;
  1398. }
  1399. #ifdef CONFIG_PM
  1400. static int wm8903_i2c_suspend(struct i2c_client *client, pm_message_t msg)
  1401. {
  1402. return snd_soc_suspend_device(&client->dev);
  1403. }
  1404. static int wm8903_i2c_resume(struct i2c_client *client)
  1405. {
  1406. return snd_soc_resume_device(&client->dev);
  1407. }
  1408. #else
  1409. #define wm8903_i2c_suspend NULL
  1410. #define wm8903_i2c_resume NULL
  1411. #endif
  1412. /* i2c codec control layer */
  1413. static const struct i2c_device_id wm8903_i2c_id[] = {
  1414. { "wm8903", 0 },
  1415. { }
  1416. };
  1417. MODULE_DEVICE_TABLE(i2c, wm8903_i2c_id);
  1418. static struct i2c_driver wm8903_i2c_driver = {
  1419. .driver = {
  1420. .name = "WM8903",
  1421. .owner = THIS_MODULE,
  1422. },
  1423. .probe = wm8903_i2c_probe,
  1424. .remove = __devexit_p(wm8903_i2c_remove),
  1425. .suspend = wm8903_i2c_suspend,
  1426. .resume = wm8903_i2c_resume,
  1427. .id_table = wm8903_i2c_id,
  1428. };
  1429. static int wm8903_probe(struct platform_device *pdev)
  1430. {
  1431. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1432. int ret = 0;
  1433. if (!wm8903_codec) {
  1434. dev_err(&pdev->dev, "I2C device not yet probed\n");
  1435. goto err;
  1436. }
  1437. socdev->card->codec = wm8903_codec;
  1438. /* register pcms */
  1439. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  1440. if (ret < 0) {
  1441. dev_err(&pdev->dev, "failed to create pcms\n");
  1442. goto err;
  1443. }
  1444. snd_soc_add_controls(socdev->card->codec, wm8903_snd_controls,
  1445. ARRAY_SIZE(wm8903_snd_controls));
  1446. wm8903_add_widgets(socdev->card->codec);
  1447. ret = snd_soc_init_card(socdev);
  1448. if (ret < 0) {
  1449. dev_err(&pdev->dev, "wm8903: failed to register card\n");
  1450. goto card_err;
  1451. }
  1452. return ret;
  1453. card_err:
  1454. snd_soc_free_pcms(socdev);
  1455. snd_soc_dapm_free(socdev);
  1456. err:
  1457. return ret;
  1458. }
  1459. /* power down chip */
  1460. static int wm8903_remove(struct platform_device *pdev)
  1461. {
  1462. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1463. struct snd_soc_codec *codec = socdev->card->codec;
  1464. if (codec->control_data)
  1465. wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1466. snd_soc_free_pcms(socdev);
  1467. snd_soc_dapm_free(socdev);
  1468. return 0;
  1469. }
  1470. struct snd_soc_codec_device soc_codec_dev_wm8903 = {
  1471. .probe = wm8903_probe,
  1472. .remove = wm8903_remove,
  1473. .suspend = wm8903_suspend,
  1474. .resume = wm8903_resume,
  1475. };
  1476. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8903);
  1477. static int __init wm8903_modinit(void)
  1478. {
  1479. return i2c_add_driver(&wm8903_i2c_driver);
  1480. }
  1481. module_init(wm8903_modinit);
  1482. static void __exit wm8903_exit(void)
  1483. {
  1484. i2c_del_driver(&wm8903_i2c_driver);
  1485. }
  1486. module_exit(wm8903_exit);
  1487. MODULE_DESCRIPTION("ASoC WM8903 driver");
  1488. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.cm>");
  1489. MODULE_LICENSE("GPL");