wm8903.c 52 KB

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