wm8903.c 52 KB

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