wm8903.c 56 KB

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