wm8903.c 54 KB

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