wm8903.c 57 KB

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