wm8903.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128
  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. int dcs_pending;
  220. int dcs_cache[4];
  221. /* Reference count */
  222. int class_w_users;
  223. struct snd_soc_jack *mic_jack;
  224. int mic_det;
  225. int mic_short;
  226. int mic_last_report;
  227. int mic_delay;
  228. #ifdef CONFIG_GPIOLIB
  229. struct gpio_chip gpio_chip;
  230. #endif
  231. };
  232. static int wm8903_volatile_register(struct snd_soc_codec *codec, unsigned int reg)
  233. {
  234. switch (reg) {
  235. case WM8903_SW_RESET_AND_ID:
  236. case WM8903_REVISION_NUMBER:
  237. case WM8903_INTERRUPT_STATUS_1:
  238. case WM8903_WRITE_SEQUENCER_4:
  239. case WM8903_DC_SERVO_READBACK_1:
  240. case WM8903_DC_SERVO_READBACK_2:
  241. case WM8903_DC_SERVO_READBACK_3:
  242. case WM8903_DC_SERVO_READBACK_4:
  243. return 1;
  244. default:
  245. return 0;
  246. }
  247. }
  248. static void wm8903_reset(struct snd_soc_codec *codec)
  249. {
  250. snd_soc_write(codec, WM8903_SW_RESET_AND_ID, 0);
  251. memcpy(codec->reg_cache, wm8903_reg_defaults,
  252. sizeof(wm8903_reg_defaults));
  253. }
  254. static int wm8903_cp_event(struct snd_soc_dapm_widget *w,
  255. struct snd_kcontrol *kcontrol, int event)
  256. {
  257. WARN_ON(event != SND_SOC_DAPM_POST_PMU);
  258. mdelay(4);
  259. return 0;
  260. }
  261. static int wm8903_dcs_event(struct snd_soc_dapm_widget *w,
  262. struct snd_kcontrol *kcontrol, int event)
  263. {
  264. struct snd_soc_codec *codec = w->codec;
  265. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  266. switch (event) {
  267. case SND_SOC_DAPM_POST_PMU:
  268. wm8903->dcs_pending |= 1 << w->shift;
  269. break;
  270. case SND_SOC_DAPM_PRE_PMD:
  271. snd_soc_update_bits(codec, WM8903_DC_SERVO_0,
  272. 1 << w->shift, 0);
  273. break;
  274. }
  275. return 0;
  276. }
  277. #define WM8903_DCS_MODE_WRITE_STOP 0
  278. #define WM8903_DCS_MODE_START_STOP 2
  279. static void wm8903_seq_notifier(struct snd_soc_dapm_context *dapm,
  280. enum snd_soc_dapm_type event, int subseq)
  281. {
  282. struct snd_soc_codec *codec = container_of(dapm,
  283. struct snd_soc_codec, dapm);
  284. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  285. int dcs_mode = WM8903_DCS_MODE_WRITE_STOP;
  286. int i, val;
  287. /* Complete any pending DC servo starts */
  288. if (wm8903->dcs_pending) {
  289. dev_dbg(codec->dev, "Starting DC servo for %x\n",
  290. wm8903->dcs_pending);
  291. /* If we've no cached values then we need to do startup */
  292. for (i = 0; i < ARRAY_SIZE(wm8903->dcs_cache); i++) {
  293. if (!(wm8903->dcs_pending & (1 << i)))
  294. continue;
  295. if (wm8903->dcs_cache[i]) {
  296. dev_dbg(codec->dev,
  297. "Restore DC servo %d value %x\n",
  298. 3 - i, wm8903->dcs_cache[i]);
  299. snd_soc_write(codec, WM8903_DC_SERVO_4 + i,
  300. wm8903->dcs_cache[i] & 0xff);
  301. } else {
  302. dev_dbg(codec->dev,
  303. "Calibrate DC servo %d\n", 3 - i);
  304. dcs_mode = WM8903_DCS_MODE_START_STOP;
  305. }
  306. }
  307. /* Don't trust the cache for analogue */
  308. if (wm8903->class_w_users)
  309. dcs_mode = WM8903_DCS_MODE_START_STOP;
  310. snd_soc_update_bits(codec, WM8903_DC_SERVO_2,
  311. WM8903_DCS_MODE_MASK, dcs_mode);
  312. snd_soc_update_bits(codec, WM8903_DC_SERVO_0,
  313. WM8903_DCS_ENA_MASK, wm8903->dcs_pending);
  314. switch (dcs_mode) {
  315. case WM8903_DCS_MODE_WRITE_STOP:
  316. break;
  317. case WM8903_DCS_MODE_START_STOP:
  318. msleep(270);
  319. /* Cache the measured offsets for digital */
  320. if (wm8903->class_w_users)
  321. break;
  322. for (i = 0; i < ARRAY_SIZE(wm8903->dcs_cache); i++) {
  323. if (!(wm8903->dcs_pending & (1 << i)))
  324. continue;
  325. val = snd_soc_read(codec,
  326. WM8903_DC_SERVO_READBACK_1 + i);
  327. dev_dbg(codec->dev, "DC servo %d: %x\n",
  328. 3 - i, val);
  329. wm8903->dcs_cache[i] = val;
  330. }
  331. break;
  332. default:
  333. pr_warn("DCS mode %d delay not set\n", dcs_mode);
  334. break;
  335. }
  336. wm8903->dcs_pending = 0;
  337. }
  338. }
  339. /*
  340. * When used with DAC outputs only the WM8903 charge pump supports
  341. * operation in class W mode, providing very low power consumption
  342. * when used with digital sources. Enable and disable this mode
  343. * automatically depending on the mixer configuration.
  344. *
  345. * All the relevant controls are simple switches.
  346. */
  347. static int wm8903_class_w_put(struct snd_kcontrol *kcontrol,
  348. struct snd_ctl_elem_value *ucontrol)
  349. {
  350. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  351. struct snd_soc_codec *codec = widget->codec;
  352. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  353. u16 reg;
  354. int ret;
  355. reg = snd_soc_read(codec, WM8903_CLASS_W_0);
  356. /* Turn it off if we're about to enable bypass */
  357. if (ucontrol->value.integer.value[0]) {
  358. if (wm8903->class_w_users == 0) {
  359. dev_dbg(codec->dev, "Disabling Class W\n");
  360. snd_soc_write(codec, WM8903_CLASS_W_0, reg &
  361. ~(WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V));
  362. }
  363. wm8903->class_w_users++;
  364. }
  365. /* Implement the change */
  366. ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
  367. /* If we've just disabled the last bypass path turn Class W on */
  368. if (!ucontrol->value.integer.value[0]) {
  369. if (wm8903->class_w_users == 1) {
  370. dev_dbg(codec->dev, "Enabling Class W\n");
  371. snd_soc_write(codec, WM8903_CLASS_W_0, reg |
  372. WM8903_CP_DYN_FREQ | WM8903_CP_DYN_V);
  373. }
  374. wm8903->class_w_users--;
  375. }
  376. dev_dbg(codec->dev, "Bypass use count now %d\n",
  377. wm8903->class_w_users);
  378. return ret;
  379. }
  380. #define SOC_DAPM_SINGLE_W(xname, reg, shift, max, invert) \
  381. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  382. .info = snd_soc_info_volsw, \
  383. .get = snd_soc_dapm_get_volsw, .put = wm8903_class_w_put, \
  384. .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert) }
  385. static int wm8903_deemph[] = { 0, 32000, 44100, 48000 };
  386. static int wm8903_set_deemph(struct snd_soc_codec *codec)
  387. {
  388. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  389. int val, i, best;
  390. /* If we're using deemphasis select the nearest available sample
  391. * rate.
  392. */
  393. if (wm8903->deemph) {
  394. best = 1;
  395. for (i = 2; i < ARRAY_SIZE(wm8903_deemph); i++) {
  396. if (abs(wm8903_deemph[i] - wm8903->fs) <
  397. abs(wm8903_deemph[best] - wm8903->fs))
  398. best = i;
  399. }
  400. val = best << WM8903_DEEMPH_SHIFT;
  401. } else {
  402. best = 0;
  403. val = 0;
  404. }
  405. dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n",
  406. best, wm8903_deemph[best]);
  407. return snd_soc_update_bits(codec, WM8903_DAC_DIGITAL_1,
  408. WM8903_DEEMPH_MASK, val);
  409. }
  410. static int wm8903_get_deemph(struct snd_kcontrol *kcontrol,
  411. struct snd_ctl_elem_value *ucontrol)
  412. {
  413. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  414. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  415. ucontrol->value.enumerated.item[0] = wm8903->deemph;
  416. return 0;
  417. }
  418. static int wm8903_put_deemph(struct snd_kcontrol *kcontrol,
  419. struct snd_ctl_elem_value *ucontrol)
  420. {
  421. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  422. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  423. int deemph = ucontrol->value.enumerated.item[0];
  424. int ret = 0;
  425. if (deemph > 1)
  426. return -EINVAL;
  427. mutex_lock(&codec->mutex);
  428. if (wm8903->deemph != deemph) {
  429. wm8903->deemph = deemph;
  430. wm8903_set_deemph(codec);
  431. ret = 1;
  432. }
  433. mutex_unlock(&codec->mutex);
  434. return ret;
  435. }
  436. /* ALSA can only do steps of .01dB */
  437. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  438. static const DECLARE_TLV_DB_SCALE(digital_sidetone_tlv, -3600, 300, 0);
  439. static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
  440. static const DECLARE_TLV_DB_SCALE(drc_tlv_thresh, 0, 75, 0);
  441. static const DECLARE_TLV_DB_SCALE(drc_tlv_amp, -2250, 75, 0);
  442. static const DECLARE_TLV_DB_SCALE(drc_tlv_min, 0, 600, 0);
  443. static const DECLARE_TLV_DB_SCALE(drc_tlv_max, 1200, 600, 0);
  444. static const DECLARE_TLV_DB_SCALE(drc_tlv_startup, -300, 50, 0);
  445. static const char *hpf_mode_text[] = {
  446. "Hi-fi", "Voice 1", "Voice 2", "Voice 3"
  447. };
  448. static const struct soc_enum hpf_mode =
  449. SOC_ENUM_SINGLE(WM8903_ADC_DIGITAL_0, 5, 4, hpf_mode_text);
  450. static const char *osr_text[] = {
  451. "Low power", "High performance"
  452. };
  453. static const struct soc_enum adc_osr =
  454. SOC_ENUM_SINGLE(WM8903_ANALOGUE_ADC_0, 0, 2, osr_text);
  455. static const struct soc_enum dac_osr =
  456. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 0, 2, osr_text);
  457. static const char *drc_slope_text[] = {
  458. "1", "1/2", "1/4", "1/8", "1/16", "0"
  459. };
  460. static const struct soc_enum drc_slope_r0 =
  461. SOC_ENUM_SINGLE(WM8903_DRC_2, 3, 6, drc_slope_text);
  462. static const struct soc_enum drc_slope_r1 =
  463. SOC_ENUM_SINGLE(WM8903_DRC_2, 0, 6, drc_slope_text);
  464. static const char *drc_attack_text[] = {
  465. "instantaneous",
  466. "363us", "762us", "1.45ms", "2.9ms", "5.8ms", "11.6ms", "23.2ms",
  467. "46.4ms", "92.8ms", "185.6ms"
  468. };
  469. static const struct soc_enum drc_attack =
  470. SOC_ENUM_SINGLE(WM8903_DRC_1, 12, 11, drc_attack_text);
  471. static const char *drc_decay_text[] = {
  472. "186ms", "372ms", "743ms", "1.49s", "2.97s", "5.94s", "11.89s",
  473. "23.87s", "47.56s"
  474. };
  475. static const struct soc_enum drc_decay =
  476. SOC_ENUM_SINGLE(WM8903_DRC_1, 8, 9, drc_decay_text);
  477. static const char *drc_ff_delay_text[] = {
  478. "5 samples", "9 samples"
  479. };
  480. static const struct soc_enum drc_ff_delay =
  481. SOC_ENUM_SINGLE(WM8903_DRC_0, 5, 2, drc_ff_delay_text);
  482. static const char *drc_qr_decay_text[] = {
  483. "0.725ms", "1.45ms", "5.8ms"
  484. };
  485. static const struct soc_enum drc_qr_decay =
  486. SOC_ENUM_SINGLE(WM8903_DRC_1, 4, 3, drc_qr_decay_text);
  487. static const char *drc_smoothing_text[] = {
  488. "Low", "Medium", "High"
  489. };
  490. static const struct soc_enum drc_smoothing =
  491. SOC_ENUM_SINGLE(WM8903_DRC_0, 11, 3, drc_smoothing_text);
  492. static const char *soft_mute_text[] = {
  493. "Fast (fs/2)", "Slow (fs/32)"
  494. };
  495. static const struct soc_enum soft_mute =
  496. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 10, 2, soft_mute_text);
  497. static const char *mute_mode_text[] = {
  498. "Hard", "Soft"
  499. };
  500. static const struct soc_enum mute_mode =
  501. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_1, 9, 2, mute_mode_text);
  502. static const char *companding_text[] = {
  503. "ulaw", "alaw"
  504. };
  505. static const struct soc_enum dac_companding =
  506. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 0, 2, companding_text);
  507. static const struct soc_enum adc_companding =
  508. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 2, 2, companding_text);
  509. static const char *input_mode_text[] = {
  510. "Single-Ended", "Differential Line", "Differential Mic"
  511. };
  512. static const struct soc_enum linput_mode_enum =
  513. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
  514. static const struct soc_enum rinput_mode_enum =
  515. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
  516. static const char *linput_mux_text[] = {
  517. "IN1L", "IN2L", "IN3L"
  518. };
  519. static const struct soc_enum linput_enum =
  520. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 2, 3, linput_mux_text);
  521. static const struct soc_enum linput_inv_enum =
  522. SOC_ENUM_SINGLE(WM8903_ANALOGUE_LEFT_INPUT_1, 4, 3, linput_mux_text);
  523. static const char *rinput_mux_text[] = {
  524. "IN1R", "IN2R", "IN3R"
  525. };
  526. static const struct soc_enum rinput_enum =
  527. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 2, 3, rinput_mux_text);
  528. static const struct soc_enum rinput_inv_enum =
  529. SOC_ENUM_SINGLE(WM8903_ANALOGUE_RIGHT_INPUT_1, 4, 3, rinput_mux_text);
  530. static const char *sidetone_text[] = {
  531. "None", "Left", "Right"
  532. };
  533. static const struct soc_enum lsidetone_enum =
  534. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 2, 3, sidetone_text);
  535. static const struct soc_enum rsidetone_enum =
  536. SOC_ENUM_SINGLE(WM8903_DAC_DIGITAL_0, 0, 3, sidetone_text);
  537. static const char *aif_text[] = {
  538. "Left", "Right"
  539. };
  540. static const struct soc_enum lcapture_enum =
  541. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 7, 2, aif_text);
  542. static const struct soc_enum rcapture_enum =
  543. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 6, 2, aif_text);
  544. static const struct soc_enum lplay_enum =
  545. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 5, 2, aif_text);
  546. static const struct soc_enum rplay_enum =
  547. SOC_ENUM_SINGLE(WM8903_AUDIO_INTERFACE_0, 4, 2, aif_text);
  548. static const struct snd_kcontrol_new wm8903_snd_controls[] = {
  549. /* Input PGAs - No TLV since the scale depends on PGA mode */
  550. SOC_SINGLE("Left Input PGA Switch", WM8903_ANALOGUE_LEFT_INPUT_0,
  551. 7, 1, 1),
  552. SOC_SINGLE("Left Input PGA Volume", WM8903_ANALOGUE_LEFT_INPUT_0,
  553. 0, 31, 0),
  554. SOC_SINGLE("Left Input PGA Common Mode Switch", WM8903_ANALOGUE_LEFT_INPUT_1,
  555. 6, 1, 0),
  556. SOC_SINGLE("Right Input PGA Switch", WM8903_ANALOGUE_RIGHT_INPUT_0,
  557. 7, 1, 1),
  558. SOC_SINGLE("Right Input PGA Volume", WM8903_ANALOGUE_RIGHT_INPUT_0,
  559. 0, 31, 0),
  560. SOC_SINGLE("Right Input PGA Common Mode Switch", WM8903_ANALOGUE_RIGHT_INPUT_1,
  561. 6, 1, 0),
  562. /* ADCs */
  563. SOC_ENUM("ADC OSR", adc_osr),
  564. SOC_SINGLE("HPF Switch", WM8903_ADC_DIGITAL_0, 4, 1, 0),
  565. SOC_ENUM("HPF Mode", hpf_mode),
  566. SOC_SINGLE("DRC Switch", WM8903_DRC_0, 15, 1, 0),
  567. SOC_ENUM("DRC Compressor Slope R0", drc_slope_r0),
  568. SOC_ENUM("DRC Compressor Slope R1", drc_slope_r1),
  569. SOC_SINGLE_TLV("DRC Compressor Threshold Volume", WM8903_DRC_3, 5, 124, 1,
  570. drc_tlv_thresh),
  571. SOC_SINGLE_TLV("DRC Volume", WM8903_DRC_3, 0, 30, 1, drc_tlv_amp),
  572. SOC_SINGLE_TLV("DRC Minimum Gain Volume", WM8903_DRC_1, 2, 3, 1, drc_tlv_min),
  573. SOC_SINGLE_TLV("DRC Maximum Gain Volume", WM8903_DRC_1, 0, 3, 0, drc_tlv_max),
  574. SOC_ENUM("DRC Attack Rate", drc_attack),
  575. SOC_ENUM("DRC Decay Rate", drc_decay),
  576. SOC_ENUM("DRC FF Delay", drc_ff_delay),
  577. SOC_SINGLE("DRC Anticlip Switch", WM8903_DRC_0, 1, 1, 0),
  578. SOC_SINGLE("DRC QR Switch", WM8903_DRC_0, 2, 1, 0),
  579. SOC_SINGLE_TLV("DRC QR Threshold Volume", WM8903_DRC_0, 6, 3, 0, drc_tlv_max),
  580. SOC_ENUM("DRC QR Decay Rate", drc_qr_decay),
  581. SOC_SINGLE("DRC Smoothing Switch", WM8903_DRC_0, 3, 1, 0),
  582. SOC_SINGLE("DRC Smoothing Hysteresis Switch", WM8903_DRC_0, 0, 1, 0),
  583. SOC_ENUM("DRC Smoothing Threshold", drc_smoothing),
  584. SOC_SINGLE_TLV("DRC Startup Volume", WM8903_DRC_0, 6, 18, 0, drc_tlv_startup),
  585. SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8903_ADC_DIGITAL_VOLUME_LEFT,
  586. WM8903_ADC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
  587. SOC_ENUM("ADC Companding Mode", adc_companding),
  588. SOC_SINGLE("ADC Companding Switch", WM8903_AUDIO_INTERFACE_0, 3, 1, 0),
  589. SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8903_DAC_DIGITAL_0, 4, 8,
  590. 12, 0, digital_sidetone_tlv),
  591. /* DAC */
  592. SOC_ENUM("DAC OSR", dac_osr),
  593. SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8903_DAC_DIGITAL_VOLUME_LEFT,
  594. WM8903_DAC_DIGITAL_VOLUME_RIGHT, 1, 120, 0, digital_tlv),
  595. SOC_ENUM("DAC Soft Mute Rate", soft_mute),
  596. SOC_ENUM("DAC Mute Mode", mute_mode),
  597. SOC_SINGLE("DAC Mono Switch", WM8903_DAC_DIGITAL_1, 12, 1, 0),
  598. SOC_ENUM("DAC Companding Mode", dac_companding),
  599. SOC_SINGLE("DAC Companding Switch", WM8903_AUDIO_INTERFACE_0, 1, 1, 0),
  600. SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
  601. wm8903_get_deemph, wm8903_put_deemph),
  602. /* Headphones */
  603. SOC_DOUBLE_R("Headphone Switch",
  604. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  605. 8, 1, 1),
  606. SOC_DOUBLE_R("Headphone ZC Switch",
  607. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  608. 6, 1, 0),
  609. SOC_DOUBLE_R_TLV("Headphone Volume",
  610. WM8903_ANALOGUE_OUT1_LEFT, WM8903_ANALOGUE_OUT1_RIGHT,
  611. 0, 63, 0, out_tlv),
  612. /* Line out */
  613. SOC_DOUBLE_R("Line Out Switch",
  614. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  615. 8, 1, 1),
  616. SOC_DOUBLE_R("Line Out ZC Switch",
  617. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  618. 6, 1, 0),
  619. SOC_DOUBLE_R_TLV("Line Out Volume",
  620. WM8903_ANALOGUE_OUT2_LEFT, WM8903_ANALOGUE_OUT2_RIGHT,
  621. 0, 63, 0, out_tlv),
  622. /* Speaker */
  623. SOC_DOUBLE_R("Speaker Switch",
  624. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 8, 1, 1),
  625. SOC_DOUBLE_R("Speaker ZC Switch",
  626. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT, 6, 1, 0),
  627. SOC_DOUBLE_R_TLV("Speaker Volume",
  628. WM8903_ANALOGUE_OUT3_LEFT, WM8903_ANALOGUE_OUT3_RIGHT,
  629. 0, 63, 0, out_tlv),
  630. };
  631. static const struct snd_kcontrol_new linput_mode_mux =
  632. SOC_DAPM_ENUM("Left Input Mode Mux", linput_mode_enum);
  633. static const struct snd_kcontrol_new rinput_mode_mux =
  634. SOC_DAPM_ENUM("Right Input Mode Mux", rinput_mode_enum);
  635. static const struct snd_kcontrol_new linput_mux =
  636. SOC_DAPM_ENUM("Left Input Mux", linput_enum);
  637. static const struct snd_kcontrol_new linput_inv_mux =
  638. SOC_DAPM_ENUM("Left Inverting Input Mux", linput_inv_enum);
  639. static const struct snd_kcontrol_new rinput_mux =
  640. SOC_DAPM_ENUM("Right Input Mux", rinput_enum);
  641. static const struct snd_kcontrol_new rinput_inv_mux =
  642. SOC_DAPM_ENUM("Right Inverting Input Mux", rinput_inv_enum);
  643. static const struct snd_kcontrol_new lsidetone_mux =
  644. SOC_DAPM_ENUM("DACL Sidetone Mux", lsidetone_enum);
  645. static const struct snd_kcontrol_new rsidetone_mux =
  646. SOC_DAPM_ENUM("DACR Sidetone Mux", rsidetone_enum);
  647. static const struct snd_kcontrol_new lcapture_mux =
  648. SOC_DAPM_ENUM("Left Capture Mux", lcapture_enum);
  649. static const struct snd_kcontrol_new rcapture_mux =
  650. SOC_DAPM_ENUM("Right Capture Mux", rcapture_enum);
  651. static const struct snd_kcontrol_new lplay_mux =
  652. SOC_DAPM_ENUM("Left Playback Mux", lplay_enum);
  653. static const struct snd_kcontrol_new rplay_mux =
  654. SOC_DAPM_ENUM("Right Playback Mux", rplay_enum);
  655. static const struct snd_kcontrol_new left_output_mixer[] = {
  656. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_LEFT_MIX_0, 3, 1, 0),
  657. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_LEFT_MIX_0, 2, 1, 0),
  658. SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 1, 1, 0),
  659. SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_LEFT_MIX_0, 0, 1, 0),
  660. };
  661. static const struct snd_kcontrol_new right_output_mixer[] = {
  662. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 3, 1, 0),
  663. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 2, 1, 0),
  664. SOC_DAPM_SINGLE_W("Left Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 1, 1, 0),
  665. SOC_DAPM_SINGLE_W("Right Bypass Switch", WM8903_ANALOGUE_RIGHT_MIX_0, 0, 1, 0),
  666. };
  667. static const struct snd_kcontrol_new left_speaker_mixer[] = {
  668. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 3, 1, 0),
  669. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 2, 1, 0),
  670. SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0, 1, 1, 0),
  671. SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_LEFT_0,
  672. 0, 1, 0),
  673. };
  674. static const struct snd_kcontrol_new right_speaker_mixer[] = {
  675. SOC_DAPM_SINGLE("DACL Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 3, 1, 0),
  676. SOC_DAPM_SINGLE("DACR Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0, 2, 1, 0),
  677. SOC_DAPM_SINGLE("Left Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
  678. 1, 1, 0),
  679. SOC_DAPM_SINGLE("Right Bypass Switch", WM8903_ANALOGUE_SPK_MIX_RIGHT_0,
  680. 0, 1, 0),
  681. };
  682. static const struct snd_soc_dapm_widget wm8903_dapm_widgets[] = {
  683. SND_SOC_DAPM_INPUT("IN1L"),
  684. SND_SOC_DAPM_INPUT("IN1R"),
  685. SND_SOC_DAPM_INPUT("IN2L"),
  686. SND_SOC_DAPM_INPUT("IN2R"),
  687. SND_SOC_DAPM_INPUT("IN3L"),
  688. SND_SOC_DAPM_INPUT("IN3R"),
  689. SND_SOC_DAPM_OUTPUT("HPOUTL"),
  690. SND_SOC_DAPM_OUTPUT("HPOUTR"),
  691. SND_SOC_DAPM_OUTPUT("LINEOUTL"),
  692. SND_SOC_DAPM_OUTPUT("LINEOUTR"),
  693. SND_SOC_DAPM_OUTPUT("LOP"),
  694. SND_SOC_DAPM_OUTPUT("LON"),
  695. SND_SOC_DAPM_OUTPUT("ROP"),
  696. SND_SOC_DAPM_OUTPUT("RON"),
  697. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8903_MIC_BIAS_CONTROL_0, 0, 0),
  698. SND_SOC_DAPM_MUX("Left Input Mux", SND_SOC_NOPM, 0, 0, &linput_mux),
  699. SND_SOC_DAPM_MUX("Left Input Inverting Mux", SND_SOC_NOPM, 0, 0,
  700. &linput_inv_mux),
  701. SND_SOC_DAPM_MUX("Left Input Mode Mux", SND_SOC_NOPM, 0, 0, &linput_mode_mux),
  702. SND_SOC_DAPM_MUX("Right Input Mux", SND_SOC_NOPM, 0, 0, &rinput_mux),
  703. SND_SOC_DAPM_MUX("Right Input Inverting Mux", SND_SOC_NOPM, 0, 0,
  704. &rinput_inv_mux),
  705. SND_SOC_DAPM_MUX("Right Input Mode Mux", SND_SOC_NOPM, 0, 0, &rinput_mode_mux),
  706. SND_SOC_DAPM_PGA("Left Input PGA", WM8903_POWER_MANAGEMENT_0, 1, 0, NULL, 0),
  707. SND_SOC_DAPM_PGA("Right Input PGA", WM8903_POWER_MANAGEMENT_0, 0, 0, NULL, 0),
  708. SND_SOC_DAPM_ADC("ADCL", NULL, WM8903_POWER_MANAGEMENT_6, 1, 0),
  709. SND_SOC_DAPM_ADC("ADCR", NULL, WM8903_POWER_MANAGEMENT_6, 0, 0),
  710. SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lcapture_mux),
  711. SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rcapture_mux),
  712. SND_SOC_DAPM_AIF_OUT("AIFTXL", "Left HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
  713. SND_SOC_DAPM_AIF_OUT("AIFTXR", "Right HiFi Capture", 0, SND_SOC_NOPM, 0, 0),
  714. SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &lsidetone_mux),
  715. SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &rsidetone_mux),
  716. SND_SOC_DAPM_AIF_IN("AIFRXL", "Left Playback", 0, SND_SOC_NOPM, 0, 0),
  717. SND_SOC_DAPM_AIF_IN("AIFRXR", "Right Playback", 0, SND_SOC_NOPM, 0, 0),
  718. SND_SOC_DAPM_MUX("Left Playback Mux", SND_SOC_NOPM, 0, 0, &lplay_mux),
  719. SND_SOC_DAPM_MUX("Right Playback Mux", SND_SOC_NOPM, 0, 0, &rplay_mux),
  720. SND_SOC_DAPM_DAC("DACL", NULL, WM8903_POWER_MANAGEMENT_6, 3, 0),
  721. SND_SOC_DAPM_DAC("DACR", NULL, WM8903_POWER_MANAGEMENT_6, 2, 0),
  722. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8903_POWER_MANAGEMENT_1, 1, 0,
  723. left_output_mixer, ARRAY_SIZE(left_output_mixer)),
  724. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8903_POWER_MANAGEMENT_1, 0, 0,
  725. right_output_mixer, ARRAY_SIZE(right_output_mixer)),
  726. SND_SOC_DAPM_MIXER("Left Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 1, 0,
  727. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
  728. SND_SOC_DAPM_MIXER("Right Speaker Mixer", WM8903_POWER_MANAGEMENT_4, 0, 0,
  729. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
  730. SND_SOC_DAPM_PGA_S("Left Headphone Output PGA", 0, WM8903_POWER_MANAGEMENT_2,
  731. 1, 0, NULL, 0),
  732. SND_SOC_DAPM_PGA_S("Right Headphone Output PGA", 0, WM8903_POWER_MANAGEMENT_2,
  733. 0, 0, NULL, 0),
  734. SND_SOC_DAPM_PGA_S("Left Line Output PGA", 0, WM8903_POWER_MANAGEMENT_3, 1, 0,
  735. NULL, 0),
  736. SND_SOC_DAPM_PGA_S("Right Line Output PGA", 0, WM8903_POWER_MANAGEMENT_3, 0, 0,
  737. NULL, 0),
  738. SND_SOC_DAPM_PGA_S("HPL_RMV_SHORT", 4, WM8903_ANALOGUE_HP_0, 7, 0, NULL, 0),
  739. SND_SOC_DAPM_PGA_S("HPL_ENA_OUTP", 3, WM8903_ANALOGUE_HP_0, 6, 0, NULL, 0),
  740. SND_SOC_DAPM_PGA_S("HPL_ENA_DLY", 2, WM8903_ANALOGUE_HP_0, 5, 0, NULL, 0),
  741. SND_SOC_DAPM_PGA_S("HPL_ENA", 1, WM8903_ANALOGUE_HP_0, 4, 0, NULL, 0),
  742. SND_SOC_DAPM_PGA_S("HPR_RMV_SHORT", 4, WM8903_ANALOGUE_HP_0, 3, 0, NULL, 0),
  743. SND_SOC_DAPM_PGA_S("HPR_ENA_OUTP", 3, WM8903_ANALOGUE_HP_0, 2, 0, NULL, 0),
  744. SND_SOC_DAPM_PGA_S("HPR_ENA_DLY", 2, WM8903_ANALOGUE_HP_0, 1, 0, NULL, 0),
  745. SND_SOC_DAPM_PGA_S("HPR_ENA", 1, WM8903_ANALOGUE_HP_0, 0, 0, NULL, 0),
  746. SND_SOC_DAPM_PGA_S("LINEOUTL_RMV_SHORT", 4, WM8903_ANALOGUE_LINEOUT_0, 7, 0,
  747. NULL, 0),
  748. SND_SOC_DAPM_PGA_S("LINEOUTL_ENA_OUTP", 3, WM8903_ANALOGUE_LINEOUT_0, 6, 0,
  749. NULL, 0),
  750. SND_SOC_DAPM_PGA_S("LINEOUTL_ENA_DLY", 2, WM8903_ANALOGUE_LINEOUT_0, 5, 0,
  751. NULL, 0),
  752. SND_SOC_DAPM_PGA_S("LINEOUTL_ENA", 1, WM8903_ANALOGUE_LINEOUT_0, 4, 0,
  753. NULL, 0),
  754. SND_SOC_DAPM_PGA_S("LINEOUTR_RMV_SHORT", 4, WM8903_ANALOGUE_LINEOUT_0, 3, 0,
  755. NULL, 0),
  756. SND_SOC_DAPM_PGA_S("LINEOUTR_ENA_OUTP", 3, WM8903_ANALOGUE_LINEOUT_0, 2, 0,
  757. NULL, 0),
  758. SND_SOC_DAPM_PGA_S("LINEOUTR_ENA_DLY", 2, WM8903_ANALOGUE_LINEOUT_0, 1, 0,
  759. NULL, 0),
  760. SND_SOC_DAPM_PGA_S("LINEOUTR_ENA", 1, WM8903_ANALOGUE_LINEOUT_0, 0, 0,
  761. NULL, 0),
  762. SND_SOC_DAPM_SUPPLY("DCS Master", WM8903_DC_SERVO_0, 4, 0, NULL, 0),
  763. SND_SOC_DAPM_PGA_S("HPL_DCS", 3, SND_SOC_NOPM, 3, 0, wm8903_dcs_event,
  764. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  765. SND_SOC_DAPM_PGA_S("HPR_DCS", 3, SND_SOC_NOPM, 2, 0, wm8903_dcs_event,
  766. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  767. SND_SOC_DAPM_PGA_S("LINEOUTL_DCS", 3, SND_SOC_NOPM, 1, 0, wm8903_dcs_event,
  768. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  769. SND_SOC_DAPM_PGA_S("LINEOUTR_DCS", 3, SND_SOC_NOPM, 0, 0, wm8903_dcs_event,
  770. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  771. SND_SOC_DAPM_PGA("Left Speaker PGA", WM8903_POWER_MANAGEMENT_5, 1, 0,
  772. NULL, 0),
  773. SND_SOC_DAPM_PGA("Right Speaker PGA", WM8903_POWER_MANAGEMENT_5, 0, 0,
  774. NULL, 0),
  775. SND_SOC_DAPM_SUPPLY("Charge Pump", WM8903_CHARGE_PUMP_0, 0, 0,
  776. wm8903_cp_event, SND_SOC_DAPM_POST_PMU),
  777. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8903_CLOCK_RATES_2, 1, 0, NULL, 0),
  778. SND_SOC_DAPM_SUPPLY("CLK_SYS", WM8903_CLOCK_RATES_2, 2, 0, NULL, 0),
  779. };
  780. static const struct snd_soc_dapm_route intercon[] = {
  781. { "CLK_DSP", NULL, "CLK_SYS" },
  782. { "Mic Bias", NULL, "CLK_SYS" },
  783. { "HPL_DCS", NULL, "CLK_SYS" },
  784. { "HPR_DCS", NULL, "CLK_SYS" },
  785. { "LINEOUTL_DCS", NULL, "CLK_SYS" },
  786. { "LINEOUTR_DCS", NULL, "CLK_SYS" },
  787. { "Left Input Mux", "IN1L", "IN1L" },
  788. { "Left Input Mux", "IN2L", "IN2L" },
  789. { "Left Input Mux", "IN3L", "IN3L" },
  790. { "Left Input Inverting Mux", "IN1L", "IN1L" },
  791. { "Left Input Inverting Mux", "IN2L", "IN2L" },
  792. { "Left Input Inverting Mux", "IN3L", "IN3L" },
  793. { "Right Input Mux", "IN1R", "IN1R" },
  794. { "Right Input Mux", "IN2R", "IN2R" },
  795. { "Right Input Mux", "IN3R", "IN3R" },
  796. { "Right Input Inverting Mux", "IN1R", "IN1R" },
  797. { "Right Input Inverting Mux", "IN2R", "IN2R" },
  798. { "Right Input Inverting Mux", "IN3R", "IN3R" },
  799. { "Left Input Mode Mux", "Single-Ended", "Left Input Inverting Mux" },
  800. { "Left Input Mode Mux", "Differential Line",
  801. "Left Input Mux" },
  802. { "Left Input Mode Mux", "Differential Line",
  803. "Left Input Inverting Mux" },
  804. { "Left Input Mode Mux", "Differential Mic",
  805. "Left Input Mux" },
  806. { "Left Input Mode Mux", "Differential Mic",
  807. "Left Input Inverting Mux" },
  808. { "Right Input Mode Mux", "Single-Ended",
  809. "Right Input Inverting Mux" },
  810. { "Right Input Mode Mux", "Differential Line",
  811. "Right Input Mux" },
  812. { "Right Input Mode Mux", "Differential Line",
  813. "Right Input Inverting Mux" },
  814. { "Right Input Mode Mux", "Differential Mic",
  815. "Right Input Mux" },
  816. { "Right Input Mode Mux", "Differential Mic",
  817. "Right Input Inverting Mux" },
  818. { "Left Input PGA", NULL, "Left Input Mode Mux" },
  819. { "Right Input PGA", NULL, "Right Input Mode Mux" },
  820. { "Left Capture Mux", "Left", "ADCL" },
  821. { "Left Capture Mux", "Right", "ADCR" },
  822. { "Right Capture Mux", "Left", "ADCL" },
  823. { "Right Capture Mux", "Right", "ADCR" },
  824. { "AIFTXL", NULL, "Left Capture Mux" },
  825. { "AIFTXR", NULL, "Right Capture Mux" },
  826. { "ADCL", NULL, "Left Input PGA" },
  827. { "ADCL", NULL, "CLK_DSP" },
  828. { "ADCR", NULL, "Right Input PGA" },
  829. { "ADCR", NULL, "CLK_DSP" },
  830. { "Left Playback Mux", "Left", "AIFRXL" },
  831. { "Left Playback Mux", "Right", "AIFRXR" },
  832. { "Right Playback Mux", "Left", "AIFRXL" },
  833. { "Right Playback Mux", "Right", "AIFRXR" },
  834. { "DACL Sidetone", "Left", "ADCL" },
  835. { "DACL Sidetone", "Right", "ADCR" },
  836. { "DACR Sidetone", "Left", "ADCL" },
  837. { "DACR Sidetone", "Right", "ADCR" },
  838. { "DACL", NULL, "Left Playback Mux" },
  839. { "DACL", NULL, "DACL Sidetone" },
  840. { "DACL", NULL, "CLK_DSP" },
  841. { "DACR", NULL, "Right Playback Mux" },
  842. { "DACR", NULL, "DACR Sidetone" },
  843. { "DACR", NULL, "CLK_DSP" },
  844. { "Left Output Mixer", "Left Bypass Switch", "Left Input PGA" },
  845. { "Left Output Mixer", "Right Bypass Switch", "Right Input PGA" },
  846. { "Left Output Mixer", "DACL Switch", "DACL" },
  847. { "Left Output Mixer", "DACR Switch", "DACR" },
  848. { "Right Output Mixer", "Left Bypass Switch", "Left Input PGA" },
  849. { "Right Output Mixer", "Right Bypass Switch", "Right Input PGA" },
  850. { "Right Output Mixer", "DACL Switch", "DACL" },
  851. { "Right Output Mixer", "DACR Switch", "DACR" },
  852. { "Left Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
  853. { "Left Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
  854. { "Left Speaker Mixer", "DACL Switch", "DACL" },
  855. { "Left Speaker Mixer", "DACR Switch", "DACR" },
  856. { "Right Speaker Mixer", "Left Bypass Switch", "Left Input PGA" },
  857. { "Right Speaker Mixer", "Right Bypass Switch", "Right Input PGA" },
  858. { "Right Speaker Mixer", "DACL Switch", "DACL" },
  859. { "Right Speaker Mixer", "DACR Switch", "DACR" },
  860. { "Left Line Output PGA", NULL, "Left Output Mixer" },
  861. { "Right Line Output PGA", NULL, "Right Output Mixer" },
  862. { "Left Headphone Output PGA", NULL, "Left Output Mixer" },
  863. { "Right Headphone Output PGA", NULL, "Right Output Mixer" },
  864. { "Left Speaker PGA", NULL, "Left Speaker Mixer" },
  865. { "Right Speaker PGA", NULL, "Right Speaker Mixer" },
  866. { "HPL_ENA", NULL, "Left Headphone Output PGA" },
  867. { "HPR_ENA", NULL, "Right Headphone Output PGA" },
  868. { "HPL_ENA_DLY", NULL, "HPL_ENA" },
  869. { "HPR_ENA_DLY", NULL, "HPR_ENA" },
  870. { "LINEOUTL_ENA", NULL, "Left Line Output PGA" },
  871. { "LINEOUTR_ENA", NULL, "Right Line Output PGA" },
  872. { "LINEOUTL_ENA_DLY", NULL, "LINEOUTL_ENA" },
  873. { "LINEOUTR_ENA_DLY", NULL, "LINEOUTR_ENA" },
  874. { "HPL_DCS", NULL, "DCS Master" },
  875. { "HPR_DCS", NULL, "DCS Master" },
  876. { "LINEOUTL_DCS", NULL, "DCS Master" },
  877. { "LINEOUTR_DCS", NULL, "DCS Master" },
  878. { "HPL_DCS", NULL, "HPL_ENA_DLY" },
  879. { "HPR_DCS", NULL, "HPR_ENA_DLY" },
  880. { "LINEOUTL_DCS", NULL, "LINEOUTL_ENA_DLY" },
  881. { "LINEOUTR_DCS", NULL, "LINEOUTR_ENA_DLY" },
  882. { "HPL_ENA_OUTP", NULL, "HPL_DCS" },
  883. { "HPR_ENA_OUTP", NULL, "HPR_DCS" },
  884. { "LINEOUTL_ENA_OUTP", NULL, "LINEOUTL_DCS" },
  885. { "LINEOUTR_ENA_OUTP", NULL, "LINEOUTR_DCS" },
  886. { "HPL_RMV_SHORT", NULL, "HPL_ENA_OUTP" },
  887. { "HPR_RMV_SHORT", NULL, "HPR_ENA_OUTP" },
  888. { "LINEOUTL_RMV_SHORT", NULL, "LINEOUTL_ENA_OUTP" },
  889. { "LINEOUTR_RMV_SHORT", NULL, "LINEOUTR_ENA_OUTP" },
  890. { "HPOUTL", NULL, "HPL_RMV_SHORT" },
  891. { "HPOUTR", NULL, "HPR_RMV_SHORT" },
  892. { "LINEOUTL", NULL, "LINEOUTL_RMV_SHORT" },
  893. { "LINEOUTR", NULL, "LINEOUTR_RMV_SHORT" },
  894. { "LOP", NULL, "Left Speaker PGA" },
  895. { "LON", NULL, "Left Speaker PGA" },
  896. { "ROP", NULL, "Right Speaker PGA" },
  897. { "RON", NULL, "Right Speaker PGA" },
  898. { "Left Headphone Output PGA", NULL, "Charge Pump" },
  899. { "Right Headphone Output PGA", NULL, "Charge Pump" },
  900. { "Left Line Output PGA", NULL, "Charge Pump" },
  901. { "Right Line Output PGA", NULL, "Charge Pump" },
  902. };
  903. static int wm8903_add_widgets(struct snd_soc_codec *codec)
  904. {
  905. struct snd_soc_dapm_context *dapm = &codec->dapm;
  906. snd_soc_dapm_new_controls(dapm, wm8903_dapm_widgets,
  907. ARRAY_SIZE(wm8903_dapm_widgets));
  908. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  909. return 0;
  910. }
  911. static int wm8903_set_bias_level(struct snd_soc_codec *codec,
  912. enum snd_soc_bias_level level)
  913. {
  914. switch (level) {
  915. case SND_SOC_BIAS_ON:
  916. break;
  917. case SND_SOC_BIAS_PREPARE:
  918. snd_soc_update_bits(codec, WM8903_VMID_CONTROL_0,
  919. WM8903_VMID_RES_MASK,
  920. WM8903_VMID_RES_50K);
  921. break;
  922. case SND_SOC_BIAS_STANDBY:
  923. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  924. snd_soc_update_bits(codec, WM8903_BIAS_CONTROL_0,
  925. WM8903_POBCTRL | WM8903_ISEL_MASK |
  926. WM8903_STARTUP_BIAS_ENA |
  927. WM8903_BIAS_ENA,
  928. WM8903_POBCTRL |
  929. (2 << WM8903_ISEL_SHIFT) |
  930. WM8903_STARTUP_BIAS_ENA);
  931. snd_soc_update_bits(codec,
  932. WM8903_ANALOGUE_SPK_OUTPUT_CONTROL_0,
  933. WM8903_SPK_DISCHARGE,
  934. WM8903_SPK_DISCHARGE);
  935. msleep(33);
  936. snd_soc_update_bits(codec, WM8903_POWER_MANAGEMENT_5,
  937. WM8903_SPKL_ENA | WM8903_SPKR_ENA,
  938. WM8903_SPKL_ENA | WM8903_SPKR_ENA);
  939. snd_soc_update_bits(codec,
  940. WM8903_ANALOGUE_SPK_OUTPUT_CONTROL_0,
  941. WM8903_SPK_DISCHARGE, 0);
  942. snd_soc_update_bits(codec, WM8903_VMID_CONTROL_0,
  943. WM8903_VMID_TIE_ENA |
  944. WM8903_BUFIO_ENA |
  945. WM8903_VMID_IO_ENA |
  946. WM8903_VMID_SOFT_MASK |
  947. WM8903_VMID_RES_MASK |
  948. WM8903_VMID_BUF_ENA,
  949. WM8903_VMID_TIE_ENA |
  950. WM8903_BUFIO_ENA |
  951. WM8903_VMID_IO_ENA |
  952. (2 << WM8903_VMID_SOFT_SHIFT) |
  953. WM8903_VMID_RES_250K |
  954. WM8903_VMID_BUF_ENA);
  955. msleep(129);
  956. snd_soc_update_bits(codec, WM8903_POWER_MANAGEMENT_5,
  957. WM8903_SPKL_ENA | WM8903_SPKR_ENA,
  958. 0);
  959. snd_soc_update_bits(codec, WM8903_VMID_CONTROL_0,
  960. WM8903_VMID_SOFT_MASK, 0);
  961. snd_soc_update_bits(codec, WM8903_VMID_CONTROL_0,
  962. WM8903_VMID_RES_MASK,
  963. WM8903_VMID_RES_50K);
  964. snd_soc_update_bits(codec, WM8903_BIAS_CONTROL_0,
  965. WM8903_BIAS_ENA | WM8903_POBCTRL,
  966. WM8903_BIAS_ENA);
  967. /* By default no bypass paths are enabled so
  968. * enable Class W support.
  969. */
  970. dev_dbg(codec->dev, "Enabling Class W\n");
  971. snd_soc_update_bits(codec, WM8903_CLASS_W_0,
  972. WM8903_CP_DYN_FREQ |
  973. WM8903_CP_DYN_V,
  974. WM8903_CP_DYN_FREQ |
  975. WM8903_CP_DYN_V);
  976. }
  977. snd_soc_update_bits(codec, WM8903_VMID_CONTROL_0,
  978. WM8903_VMID_RES_MASK,
  979. WM8903_VMID_RES_250K);
  980. break;
  981. case SND_SOC_BIAS_OFF:
  982. snd_soc_update_bits(codec, WM8903_BIAS_CONTROL_0,
  983. WM8903_BIAS_ENA, 0);
  984. snd_soc_update_bits(codec, WM8903_VMID_CONTROL_0,
  985. WM8903_VMID_SOFT_MASK,
  986. 2 << WM8903_VMID_SOFT_SHIFT);
  987. snd_soc_update_bits(codec, WM8903_VMID_CONTROL_0,
  988. WM8903_VMID_BUF_ENA, 0);
  989. msleep(290);
  990. snd_soc_update_bits(codec, WM8903_VMID_CONTROL_0,
  991. WM8903_VMID_TIE_ENA | WM8903_BUFIO_ENA |
  992. WM8903_VMID_IO_ENA | WM8903_VMID_RES_MASK |
  993. WM8903_VMID_SOFT_MASK |
  994. WM8903_VMID_BUF_ENA, 0);
  995. snd_soc_update_bits(codec, WM8903_BIAS_CONTROL_0,
  996. WM8903_STARTUP_BIAS_ENA, 0);
  997. break;
  998. }
  999. codec->dapm.bias_level = level;
  1000. return 0;
  1001. }
  1002. static int wm8903_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  1003. int clk_id, unsigned int freq, int dir)
  1004. {
  1005. struct snd_soc_codec *codec = codec_dai->codec;
  1006. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  1007. wm8903->sysclk = freq;
  1008. return 0;
  1009. }
  1010. static int wm8903_set_dai_fmt(struct snd_soc_dai *codec_dai,
  1011. unsigned int fmt)
  1012. {
  1013. struct snd_soc_codec *codec = codec_dai->codec;
  1014. u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
  1015. aif1 &= ~(WM8903_LRCLK_DIR | WM8903_BCLK_DIR | WM8903_AIF_FMT_MASK |
  1016. WM8903_AIF_LRCLK_INV | WM8903_AIF_BCLK_INV);
  1017. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1018. case SND_SOC_DAIFMT_CBS_CFS:
  1019. break;
  1020. case SND_SOC_DAIFMT_CBS_CFM:
  1021. aif1 |= WM8903_LRCLK_DIR;
  1022. break;
  1023. case SND_SOC_DAIFMT_CBM_CFM:
  1024. aif1 |= WM8903_LRCLK_DIR | WM8903_BCLK_DIR;
  1025. break;
  1026. case SND_SOC_DAIFMT_CBM_CFS:
  1027. aif1 |= WM8903_BCLK_DIR;
  1028. break;
  1029. default:
  1030. return -EINVAL;
  1031. }
  1032. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1033. case SND_SOC_DAIFMT_DSP_A:
  1034. aif1 |= 0x3;
  1035. break;
  1036. case SND_SOC_DAIFMT_DSP_B:
  1037. aif1 |= 0x3 | WM8903_AIF_LRCLK_INV;
  1038. break;
  1039. case SND_SOC_DAIFMT_I2S:
  1040. aif1 |= 0x2;
  1041. break;
  1042. case SND_SOC_DAIFMT_RIGHT_J:
  1043. aif1 |= 0x1;
  1044. break;
  1045. case SND_SOC_DAIFMT_LEFT_J:
  1046. break;
  1047. default:
  1048. return -EINVAL;
  1049. }
  1050. /* Clock inversion */
  1051. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1052. case SND_SOC_DAIFMT_DSP_A:
  1053. case SND_SOC_DAIFMT_DSP_B:
  1054. /* frame inversion not valid for DSP modes */
  1055. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1056. case SND_SOC_DAIFMT_NB_NF:
  1057. break;
  1058. case SND_SOC_DAIFMT_IB_NF:
  1059. aif1 |= WM8903_AIF_BCLK_INV;
  1060. break;
  1061. default:
  1062. return -EINVAL;
  1063. }
  1064. break;
  1065. case SND_SOC_DAIFMT_I2S:
  1066. case SND_SOC_DAIFMT_RIGHT_J:
  1067. case SND_SOC_DAIFMT_LEFT_J:
  1068. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  1069. case SND_SOC_DAIFMT_NB_NF:
  1070. break;
  1071. case SND_SOC_DAIFMT_IB_IF:
  1072. aif1 |= WM8903_AIF_BCLK_INV | WM8903_AIF_LRCLK_INV;
  1073. break;
  1074. case SND_SOC_DAIFMT_IB_NF:
  1075. aif1 |= WM8903_AIF_BCLK_INV;
  1076. break;
  1077. case SND_SOC_DAIFMT_NB_IF:
  1078. aif1 |= WM8903_AIF_LRCLK_INV;
  1079. break;
  1080. default:
  1081. return -EINVAL;
  1082. }
  1083. break;
  1084. default:
  1085. return -EINVAL;
  1086. }
  1087. snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
  1088. return 0;
  1089. }
  1090. static int wm8903_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  1091. {
  1092. struct snd_soc_codec *codec = codec_dai->codec;
  1093. u16 reg;
  1094. reg = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
  1095. if (mute)
  1096. reg |= WM8903_DAC_MUTE;
  1097. else
  1098. reg &= ~WM8903_DAC_MUTE;
  1099. snd_soc_write(codec, WM8903_DAC_DIGITAL_1, reg);
  1100. return 0;
  1101. }
  1102. /* Lookup table for CLK_SYS/fs ratio. 256fs or more is recommended
  1103. * for optimal performance so we list the lower rates first and match
  1104. * on the last match we find. */
  1105. static struct {
  1106. int div;
  1107. int rate;
  1108. int mode;
  1109. int mclk_div;
  1110. } clk_sys_ratios[] = {
  1111. { 64, 0x0, 0x0, 1 },
  1112. { 68, 0x0, 0x1, 1 },
  1113. { 125, 0x0, 0x2, 1 },
  1114. { 128, 0x1, 0x0, 1 },
  1115. { 136, 0x1, 0x1, 1 },
  1116. { 192, 0x2, 0x0, 1 },
  1117. { 204, 0x2, 0x1, 1 },
  1118. { 64, 0x0, 0x0, 2 },
  1119. { 68, 0x0, 0x1, 2 },
  1120. { 125, 0x0, 0x2, 2 },
  1121. { 128, 0x1, 0x0, 2 },
  1122. { 136, 0x1, 0x1, 2 },
  1123. { 192, 0x2, 0x0, 2 },
  1124. { 204, 0x2, 0x1, 2 },
  1125. { 250, 0x2, 0x2, 1 },
  1126. { 256, 0x3, 0x0, 1 },
  1127. { 272, 0x3, 0x1, 1 },
  1128. { 384, 0x4, 0x0, 1 },
  1129. { 408, 0x4, 0x1, 1 },
  1130. { 375, 0x4, 0x2, 1 },
  1131. { 512, 0x5, 0x0, 1 },
  1132. { 544, 0x5, 0x1, 1 },
  1133. { 500, 0x5, 0x2, 1 },
  1134. { 768, 0x6, 0x0, 1 },
  1135. { 816, 0x6, 0x1, 1 },
  1136. { 750, 0x6, 0x2, 1 },
  1137. { 1024, 0x7, 0x0, 1 },
  1138. { 1088, 0x7, 0x1, 1 },
  1139. { 1000, 0x7, 0x2, 1 },
  1140. { 1408, 0x8, 0x0, 1 },
  1141. { 1496, 0x8, 0x1, 1 },
  1142. { 1536, 0x9, 0x0, 1 },
  1143. { 1632, 0x9, 0x1, 1 },
  1144. { 1500, 0x9, 0x2, 1 },
  1145. { 250, 0x2, 0x2, 2 },
  1146. { 256, 0x3, 0x0, 2 },
  1147. { 272, 0x3, 0x1, 2 },
  1148. { 384, 0x4, 0x0, 2 },
  1149. { 408, 0x4, 0x1, 2 },
  1150. { 375, 0x4, 0x2, 2 },
  1151. { 512, 0x5, 0x0, 2 },
  1152. { 544, 0x5, 0x1, 2 },
  1153. { 500, 0x5, 0x2, 2 },
  1154. { 768, 0x6, 0x0, 2 },
  1155. { 816, 0x6, 0x1, 2 },
  1156. { 750, 0x6, 0x2, 2 },
  1157. { 1024, 0x7, 0x0, 2 },
  1158. { 1088, 0x7, 0x1, 2 },
  1159. { 1000, 0x7, 0x2, 2 },
  1160. { 1408, 0x8, 0x0, 2 },
  1161. { 1496, 0x8, 0x1, 2 },
  1162. { 1536, 0x9, 0x0, 2 },
  1163. { 1632, 0x9, 0x1, 2 },
  1164. { 1500, 0x9, 0x2, 2 },
  1165. };
  1166. /* CLK_SYS/BCLK ratios - multiplied by 10 due to .5s */
  1167. static struct {
  1168. int ratio;
  1169. int div;
  1170. } bclk_divs[] = {
  1171. { 10, 0 },
  1172. { 20, 2 },
  1173. { 30, 3 },
  1174. { 40, 4 },
  1175. { 50, 5 },
  1176. { 60, 7 },
  1177. { 80, 8 },
  1178. { 100, 9 },
  1179. { 120, 11 },
  1180. { 160, 12 },
  1181. { 200, 13 },
  1182. { 220, 14 },
  1183. { 240, 15 },
  1184. { 300, 17 },
  1185. { 320, 18 },
  1186. { 440, 19 },
  1187. { 480, 20 },
  1188. };
  1189. /* Sample rates for DSP */
  1190. static struct {
  1191. int rate;
  1192. int value;
  1193. } sample_rates[] = {
  1194. { 8000, 0 },
  1195. { 11025, 1 },
  1196. { 12000, 2 },
  1197. { 16000, 3 },
  1198. { 22050, 4 },
  1199. { 24000, 5 },
  1200. { 32000, 6 },
  1201. { 44100, 7 },
  1202. { 48000, 8 },
  1203. { 88200, 9 },
  1204. { 96000, 10 },
  1205. { 0, 0 },
  1206. };
  1207. static int wm8903_hw_params(struct snd_pcm_substream *substream,
  1208. struct snd_pcm_hw_params *params,
  1209. struct snd_soc_dai *dai)
  1210. {
  1211. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1212. struct snd_soc_codec *codec =rtd->codec;
  1213. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  1214. int fs = params_rate(params);
  1215. int bclk;
  1216. int bclk_div;
  1217. int i;
  1218. int dsp_config;
  1219. int clk_config;
  1220. int best_val;
  1221. int cur_val;
  1222. int clk_sys;
  1223. u16 aif1 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_1);
  1224. u16 aif2 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_2);
  1225. u16 aif3 = snd_soc_read(codec, WM8903_AUDIO_INTERFACE_3);
  1226. u16 clock0 = snd_soc_read(codec, WM8903_CLOCK_RATES_0);
  1227. u16 clock1 = snd_soc_read(codec, WM8903_CLOCK_RATES_1);
  1228. u16 dac_digital1 = snd_soc_read(codec, WM8903_DAC_DIGITAL_1);
  1229. /* Enable sloping stopband filter for low sample rates */
  1230. if (fs <= 24000)
  1231. dac_digital1 |= WM8903_DAC_SB_FILT;
  1232. else
  1233. dac_digital1 &= ~WM8903_DAC_SB_FILT;
  1234. /* Configure sample rate logic for DSP - choose nearest rate */
  1235. dsp_config = 0;
  1236. best_val = abs(sample_rates[dsp_config].rate - fs);
  1237. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  1238. cur_val = abs(sample_rates[i].rate - fs);
  1239. if (cur_val <= best_val) {
  1240. dsp_config = i;
  1241. best_val = cur_val;
  1242. }
  1243. }
  1244. dev_dbg(codec->dev, "DSP fs = %dHz\n", sample_rates[dsp_config].rate);
  1245. clock1 &= ~WM8903_SAMPLE_RATE_MASK;
  1246. clock1 |= sample_rates[dsp_config].value;
  1247. aif1 &= ~WM8903_AIF_WL_MASK;
  1248. bclk = 2 * fs;
  1249. switch (params_format(params)) {
  1250. case SNDRV_PCM_FORMAT_S16_LE:
  1251. bclk *= 16;
  1252. break;
  1253. case SNDRV_PCM_FORMAT_S20_3LE:
  1254. bclk *= 20;
  1255. aif1 |= 0x4;
  1256. break;
  1257. case SNDRV_PCM_FORMAT_S24_LE:
  1258. bclk *= 24;
  1259. aif1 |= 0x8;
  1260. break;
  1261. case SNDRV_PCM_FORMAT_S32_LE:
  1262. bclk *= 32;
  1263. aif1 |= 0xc;
  1264. break;
  1265. default:
  1266. return -EINVAL;
  1267. }
  1268. dev_dbg(codec->dev, "MCLK = %dHz, target sample rate = %dHz\n",
  1269. wm8903->sysclk, fs);
  1270. /* We may not have an MCLK which allows us to generate exactly
  1271. * the clock we want, particularly with USB derived inputs, so
  1272. * approximate.
  1273. */
  1274. clk_config = 0;
  1275. best_val = abs((wm8903->sysclk /
  1276. (clk_sys_ratios[0].mclk_div *
  1277. clk_sys_ratios[0].div)) - fs);
  1278. for (i = 1; i < ARRAY_SIZE(clk_sys_ratios); i++) {
  1279. cur_val = abs((wm8903->sysclk /
  1280. (clk_sys_ratios[i].mclk_div *
  1281. clk_sys_ratios[i].div)) - fs);
  1282. if (cur_val <= best_val) {
  1283. clk_config = i;
  1284. best_val = cur_val;
  1285. }
  1286. }
  1287. if (clk_sys_ratios[clk_config].mclk_div == 2) {
  1288. clock0 |= WM8903_MCLKDIV2;
  1289. clk_sys = wm8903->sysclk / 2;
  1290. } else {
  1291. clock0 &= ~WM8903_MCLKDIV2;
  1292. clk_sys = wm8903->sysclk;
  1293. }
  1294. clock1 &= ~(WM8903_CLK_SYS_RATE_MASK |
  1295. WM8903_CLK_SYS_MODE_MASK);
  1296. clock1 |= clk_sys_ratios[clk_config].rate << WM8903_CLK_SYS_RATE_SHIFT;
  1297. clock1 |= clk_sys_ratios[clk_config].mode << WM8903_CLK_SYS_MODE_SHIFT;
  1298. dev_dbg(codec->dev, "CLK_SYS_RATE=%x, CLK_SYS_MODE=%x div=%d\n",
  1299. clk_sys_ratios[clk_config].rate,
  1300. clk_sys_ratios[clk_config].mode,
  1301. clk_sys_ratios[clk_config].div);
  1302. dev_dbg(codec->dev, "Actual CLK_SYS = %dHz\n", clk_sys);
  1303. /* We may not get quite the right frequency if using
  1304. * approximate clocks so look for the closest match that is
  1305. * higher than the target (we need to ensure that there enough
  1306. * BCLKs to clock out the samples).
  1307. */
  1308. bclk_div = 0;
  1309. best_val = ((clk_sys * 10) / bclk_divs[0].ratio) - bclk;
  1310. i = 1;
  1311. while (i < ARRAY_SIZE(bclk_divs)) {
  1312. cur_val = ((clk_sys * 10) / bclk_divs[i].ratio) - bclk;
  1313. if (cur_val < 0) /* BCLK table is sorted */
  1314. break;
  1315. bclk_div = i;
  1316. best_val = cur_val;
  1317. i++;
  1318. }
  1319. aif2 &= ~WM8903_BCLK_DIV_MASK;
  1320. aif3 &= ~WM8903_LRCLK_RATE_MASK;
  1321. dev_dbg(codec->dev, "BCLK ratio %d for %dHz - actual BCLK = %dHz\n",
  1322. bclk_divs[bclk_div].ratio / 10, bclk,
  1323. (clk_sys * 10) / bclk_divs[bclk_div].ratio);
  1324. aif2 |= bclk_divs[bclk_div].div;
  1325. aif3 |= bclk / fs;
  1326. wm8903->fs = params_rate(params);
  1327. wm8903_set_deemph(codec);
  1328. snd_soc_write(codec, WM8903_CLOCK_RATES_0, clock0);
  1329. snd_soc_write(codec, WM8903_CLOCK_RATES_1, clock1);
  1330. snd_soc_write(codec, WM8903_AUDIO_INTERFACE_1, aif1);
  1331. snd_soc_write(codec, WM8903_AUDIO_INTERFACE_2, aif2);
  1332. snd_soc_write(codec, WM8903_AUDIO_INTERFACE_3, aif3);
  1333. snd_soc_write(codec, WM8903_DAC_DIGITAL_1, dac_digital1);
  1334. return 0;
  1335. }
  1336. /**
  1337. * wm8903_mic_detect - Enable microphone detection via the WM8903 IRQ
  1338. *
  1339. * @codec: WM8903 codec
  1340. * @jack: jack to report detection events on
  1341. * @det: value to report for presence detection
  1342. * @shrt: value to report for short detection
  1343. *
  1344. * Enable microphone detection via IRQ on the WM8903. If GPIOs are
  1345. * being used to bring out signals to the processor then only platform
  1346. * data configuration is needed for WM8903 and processor GPIOs should
  1347. * be configured using snd_soc_jack_add_gpios() instead.
  1348. *
  1349. * The current threasholds for detection should be configured using
  1350. * micdet_cfg in the platform data. Using this function will force on
  1351. * the microphone bias for the device.
  1352. */
  1353. int wm8903_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
  1354. int det, int shrt)
  1355. {
  1356. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  1357. int irq_mask = WM8903_MICDET_EINT | WM8903_MICSHRT_EINT;
  1358. dev_dbg(codec->dev, "Enabling microphone detection: %x %x\n",
  1359. det, shrt);
  1360. /* Store the configuration */
  1361. wm8903->mic_jack = jack;
  1362. wm8903->mic_det = det;
  1363. wm8903->mic_short = shrt;
  1364. /* Enable interrupts we've got a report configured for */
  1365. if (det)
  1366. irq_mask &= ~WM8903_MICDET_EINT;
  1367. if (shrt)
  1368. irq_mask &= ~WM8903_MICSHRT_EINT;
  1369. snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
  1370. WM8903_MICDET_EINT | WM8903_MICSHRT_EINT,
  1371. irq_mask);
  1372. if (det || shrt) {
  1373. /* Enable mic detection, this may not have been set through
  1374. * platform data (eg, if the defaults are OK). */
  1375. snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
  1376. WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
  1377. snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
  1378. WM8903_MICDET_ENA, WM8903_MICDET_ENA);
  1379. } else {
  1380. snd_soc_update_bits(codec, WM8903_MIC_BIAS_CONTROL_0,
  1381. WM8903_MICDET_ENA, 0);
  1382. }
  1383. return 0;
  1384. }
  1385. EXPORT_SYMBOL_GPL(wm8903_mic_detect);
  1386. static irqreturn_t wm8903_irq(int irq, void *data)
  1387. {
  1388. struct snd_soc_codec *codec = data;
  1389. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  1390. int mic_report;
  1391. int int_pol;
  1392. int int_val = 0;
  1393. int mask = ~snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1_MASK);
  1394. int_val = snd_soc_read(codec, WM8903_INTERRUPT_STATUS_1) & mask;
  1395. if (int_val & WM8903_WSEQ_BUSY_EINT) {
  1396. dev_warn(codec->dev, "Write sequencer done\n");
  1397. }
  1398. /*
  1399. * The rest is microphone jack detection. We need to manually
  1400. * invert the polarity of the interrupt after each event - to
  1401. * simplify the code keep track of the last state we reported
  1402. * and just invert the relevant bits in both the report and
  1403. * the polarity register.
  1404. */
  1405. mic_report = wm8903->mic_last_report;
  1406. int_pol = snd_soc_read(codec, WM8903_INTERRUPT_POLARITY_1);
  1407. #ifndef CONFIG_SND_SOC_WM8903_MODULE
  1408. if (int_val & (WM8903_MICSHRT_EINT | WM8903_MICDET_EINT))
  1409. trace_snd_soc_jack_irq(dev_name(codec->dev));
  1410. #endif
  1411. if (int_val & WM8903_MICSHRT_EINT) {
  1412. dev_dbg(codec->dev, "Microphone short (pol=%x)\n", int_pol);
  1413. mic_report ^= wm8903->mic_short;
  1414. int_pol ^= WM8903_MICSHRT_INV;
  1415. }
  1416. if (int_val & WM8903_MICDET_EINT) {
  1417. dev_dbg(codec->dev, "Microphone detect (pol=%x)\n", int_pol);
  1418. mic_report ^= wm8903->mic_det;
  1419. int_pol ^= WM8903_MICDET_INV;
  1420. msleep(wm8903->mic_delay);
  1421. }
  1422. snd_soc_update_bits(codec, WM8903_INTERRUPT_POLARITY_1,
  1423. WM8903_MICSHRT_INV | WM8903_MICDET_INV, int_pol);
  1424. snd_soc_jack_report(wm8903->mic_jack, mic_report,
  1425. wm8903->mic_short | wm8903->mic_det);
  1426. wm8903->mic_last_report = mic_report;
  1427. return IRQ_HANDLED;
  1428. }
  1429. #define WM8903_PLAYBACK_RATES (SNDRV_PCM_RATE_8000 |\
  1430. SNDRV_PCM_RATE_11025 | \
  1431. SNDRV_PCM_RATE_16000 | \
  1432. SNDRV_PCM_RATE_22050 | \
  1433. SNDRV_PCM_RATE_32000 | \
  1434. SNDRV_PCM_RATE_44100 | \
  1435. SNDRV_PCM_RATE_48000 | \
  1436. SNDRV_PCM_RATE_88200 | \
  1437. SNDRV_PCM_RATE_96000)
  1438. #define WM8903_CAPTURE_RATES (SNDRV_PCM_RATE_8000 |\
  1439. SNDRV_PCM_RATE_11025 | \
  1440. SNDRV_PCM_RATE_16000 | \
  1441. SNDRV_PCM_RATE_22050 | \
  1442. SNDRV_PCM_RATE_32000 | \
  1443. SNDRV_PCM_RATE_44100 | \
  1444. SNDRV_PCM_RATE_48000)
  1445. #define WM8903_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  1446. SNDRV_PCM_FMTBIT_S20_3LE |\
  1447. SNDRV_PCM_FMTBIT_S24_LE)
  1448. static struct snd_soc_dai_ops wm8903_dai_ops = {
  1449. .hw_params = wm8903_hw_params,
  1450. .digital_mute = wm8903_digital_mute,
  1451. .set_fmt = wm8903_set_dai_fmt,
  1452. .set_sysclk = wm8903_set_dai_sysclk,
  1453. };
  1454. static struct snd_soc_dai_driver wm8903_dai = {
  1455. .name = "wm8903-hifi",
  1456. .playback = {
  1457. .stream_name = "Playback",
  1458. .channels_min = 2,
  1459. .channels_max = 2,
  1460. .rates = WM8903_PLAYBACK_RATES,
  1461. .formats = WM8903_FORMATS,
  1462. },
  1463. .capture = {
  1464. .stream_name = "Capture",
  1465. .channels_min = 2,
  1466. .channels_max = 2,
  1467. .rates = WM8903_CAPTURE_RATES,
  1468. .formats = WM8903_FORMATS,
  1469. },
  1470. .ops = &wm8903_dai_ops,
  1471. .symmetric_rates = 1,
  1472. };
  1473. static int wm8903_suspend(struct snd_soc_codec *codec, pm_message_t state)
  1474. {
  1475. wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1476. return 0;
  1477. }
  1478. static int wm8903_resume(struct snd_soc_codec *codec)
  1479. {
  1480. int i;
  1481. u16 *reg_cache = codec->reg_cache;
  1482. u16 *tmp_cache = kmemdup(reg_cache, sizeof(wm8903_reg_defaults),
  1483. GFP_KERNEL);
  1484. /* Bring the codec back up to standby first to minimise pop/clicks */
  1485. wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1486. /* Sync back everything else */
  1487. if (tmp_cache) {
  1488. for (i = 2; i < ARRAY_SIZE(wm8903_reg_defaults); i++)
  1489. if (tmp_cache[i] != reg_cache[i])
  1490. snd_soc_write(codec, i, tmp_cache[i]);
  1491. kfree(tmp_cache);
  1492. } else {
  1493. dev_err(codec->dev, "Failed to allocate temporary cache\n");
  1494. }
  1495. return 0;
  1496. }
  1497. #ifdef CONFIG_GPIOLIB
  1498. static inline struct wm8903_priv *gpio_to_wm8903(struct gpio_chip *chip)
  1499. {
  1500. return container_of(chip, struct wm8903_priv, gpio_chip);
  1501. }
  1502. static int wm8903_gpio_request(struct gpio_chip *chip, unsigned offset)
  1503. {
  1504. if (offset >= WM8903_NUM_GPIO)
  1505. return -EINVAL;
  1506. return 0;
  1507. }
  1508. static int wm8903_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
  1509. {
  1510. struct wm8903_priv *wm8903 = gpio_to_wm8903(chip);
  1511. struct snd_soc_codec *codec = wm8903->codec;
  1512. unsigned int mask, val;
  1513. mask = WM8903_GP1_FN_MASK | WM8903_GP1_DIR_MASK;
  1514. val = (WM8903_GPn_FN_GPIO_INPUT << WM8903_GP1_FN_SHIFT) |
  1515. WM8903_GP1_DIR;
  1516. return snd_soc_update_bits(codec, WM8903_GPIO_CONTROL_1 + offset,
  1517. mask, val);
  1518. }
  1519. static int wm8903_gpio_get(struct gpio_chip *chip, unsigned offset)
  1520. {
  1521. struct wm8903_priv *wm8903 = gpio_to_wm8903(chip);
  1522. struct snd_soc_codec *codec = wm8903->codec;
  1523. int reg;
  1524. reg = snd_soc_read(codec, WM8903_GPIO_CONTROL_1 + offset);
  1525. return (reg & WM8903_GP1_LVL_MASK) >> WM8903_GP1_LVL_SHIFT;
  1526. }
  1527. static int wm8903_gpio_direction_out(struct gpio_chip *chip,
  1528. unsigned offset, int value)
  1529. {
  1530. struct wm8903_priv *wm8903 = gpio_to_wm8903(chip);
  1531. struct snd_soc_codec *codec = wm8903->codec;
  1532. unsigned int mask, val;
  1533. mask = WM8903_GP1_FN_MASK | WM8903_GP1_DIR_MASK | WM8903_GP1_LVL_MASK;
  1534. val = (WM8903_GPn_FN_GPIO_OUTPUT << WM8903_GP1_FN_SHIFT) |
  1535. (value << WM8903_GP2_LVL_SHIFT);
  1536. return snd_soc_update_bits(codec, WM8903_GPIO_CONTROL_1 + offset,
  1537. mask, val);
  1538. }
  1539. static void wm8903_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  1540. {
  1541. struct wm8903_priv *wm8903 = gpio_to_wm8903(chip);
  1542. struct snd_soc_codec *codec = wm8903->codec;
  1543. snd_soc_update_bits(codec, WM8903_GPIO_CONTROL_1 + offset,
  1544. WM8903_GP1_LVL_MASK,
  1545. !!value << WM8903_GP1_LVL_SHIFT);
  1546. }
  1547. static struct gpio_chip wm8903_template_chip = {
  1548. .label = "wm8903",
  1549. .owner = THIS_MODULE,
  1550. .request = wm8903_gpio_request,
  1551. .direction_input = wm8903_gpio_direction_in,
  1552. .get = wm8903_gpio_get,
  1553. .direction_output = wm8903_gpio_direction_out,
  1554. .set = wm8903_gpio_set,
  1555. .can_sleep = 1,
  1556. };
  1557. static void wm8903_init_gpio(struct snd_soc_codec *codec)
  1558. {
  1559. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  1560. struct wm8903_platform_data *pdata = dev_get_platdata(codec->dev);
  1561. int ret;
  1562. wm8903->gpio_chip = wm8903_template_chip;
  1563. wm8903->gpio_chip.ngpio = WM8903_NUM_GPIO;
  1564. wm8903->gpio_chip.dev = codec->dev;
  1565. if (pdata && pdata->gpio_base)
  1566. wm8903->gpio_chip.base = pdata->gpio_base;
  1567. else
  1568. wm8903->gpio_chip.base = -1;
  1569. ret = gpiochip_add(&wm8903->gpio_chip);
  1570. if (ret != 0)
  1571. dev_err(codec->dev, "Failed to add GPIOs: %d\n", ret);
  1572. }
  1573. static void wm8903_free_gpio(struct snd_soc_codec *codec)
  1574. {
  1575. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  1576. int ret;
  1577. ret = gpiochip_remove(&wm8903->gpio_chip);
  1578. if (ret != 0)
  1579. dev_err(codec->dev, "Failed to remove GPIOs: %d\n", ret);
  1580. }
  1581. #else
  1582. static void wm8903_init_gpio(struct snd_soc_codec *codec)
  1583. {
  1584. }
  1585. static void wm8903_free_gpio(struct snd_soc_codec *codec)
  1586. {
  1587. }
  1588. #endif
  1589. static int wm8903_probe(struct snd_soc_codec *codec)
  1590. {
  1591. struct wm8903_platform_data *pdata = dev_get_platdata(codec->dev);
  1592. struct wm8903_priv *wm8903 = snd_soc_codec_get_drvdata(codec);
  1593. int ret, i;
  1594. int trigger, irq_pol;
  1595. u16 val;
  1596. wm8903->codec = codec;
  1597. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  1598. if (ret != 0) {
  1599. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  1600. return ret;
  1601. }
  1602. val = snd_soc_read(codec, WM8903_SW_RESET_AND_ID);
  1603. if (val != wm8903_reg_defaults[WM8903_SW_RESET_AND_ID]) {
  1604. dev_err(codec->dev,
  1605. "Device with ID register %x is not a WM8903\n", val);
  1606. return -ENODEV;
  1607. }
  1608. val = snd_soc_read(codec, WM8903_REVISION_NUMBER);
  1609. dev_info(codec->dev, "WM8903 revision %c\n",
  1610. (val & WM8903_CHIP_REV_MASK) + 'A');
  1611. wm8903_reset(codec);
  1612. /* Set up GPIOs and microphone detection */
  1613. if (pdata) {
  1614. bool mic_gpio = false;
  1615. for (i = 0; i < ARRAY_SIZE(pdata->gpio_cfg); i++) {
  1616. if (pdata->gpio_cfg[i] == WM8903_GPIO_NO_CONFIG)
  1617. continue;
  1618. snd_soc_write(codec, WM8903_GPIO_CONTROL_1 + i,
  1619. pdata->gpio_cfg[i] & 0xffff);
  1620. val = (pdata->gpio_cfg[i] & WM8903_GP1_FN_MASK)
  1621. >> WM8903_GP1_FN_SHIFT;
  1622. switch (val) {
  1623. case WM8903_GPn_FN_MICBIAS_CURRENT_DETECT:
  1624. case WM8903_GPn_FN_MICBIAS_SHORT_DETECT:
  1625. mic_gpio = true;
  1626. break;
  1627. default:
  1628. break;
  1629. }
  1630. }
  1631. snd_soc_write(codec, WM8903_MIC_BIAS_CONTROL_0,
  1632. pdata->micdet_cfg);
  1633. /* Microphone detection needs the WSEQ clock */
  1634. if (pdata->micdet_cfg)
  1635. snd_soc_update_bits(codec, WM8903_WRITE_SEQUENCER_0,
  1636. WM8903_WSEQ_ENA, WM8903_WSEQ_ENA);
  1637. /* If microphone detection is enabled by pdata but
  1638. * detected via IRQ then interrupts can be lost before
  1639. * the machine driver has set up microphone detection
  1640. * IRQs as the IRQs are clear on read. The detection
  1641. * will be enabled when the machine driver configures.
  1642. */
  1643. WARN_ON(!mic_gpio && (pdata->micdet_cfg & WM8903_MICDET_ENA));
  1644. wm8903->mic_delay = pdata->micdet_delay;
  1645. }
  1646. if (wm8903->irq) {
  1647. if (pdata && pdata->irq_active_low) {
  1648. trigger = IRQF_TRIGGER_LOW;
  1649. irq_pol = WM8903_IRQ_POL;
  1650. } else {
  1651. trigger = IRQF_TRIGGER_HIGH;
  1652. irq_pol = 0;
  1653. }
  1654. snd_soc_update_bits(codec, WM8903_INTERRUPT_CONTROL,
  1655. WM8903_IRQ_POL, irq_pol);
  1656. ret = request_threaded_irq(wm8903->irq, NULL, wm8903_irq,
  1657. trigger | IRQF_ONESHOT,
  1658. "wm8903", codec);
  1659. if (ret != 0) {
  1660. dev_err(codec->dev, "Failed to request IRQ: %d\n",
  1661. ret);
  1662. return ret;
  1663. }
  1664. /* Enable write sequencer interrupts */
  1665. snd_soc_update_bits(codec, WM8903_INTERRUPT_STATUS_1_MASK,
  1666. WM8903_IM_WSEQ_BUSY_EINT, 0);
  1667. }
  1668. /* power on device */
  1669. wm8903_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1670. /* Latch volume update bits */
  1671. val = snd_soc_read(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT);
  1672. val |= WM8903_ADCVU;
  1673. snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_LEFT, val);
  1674. snd_soc_write(codec, WM8903_ADC_DIGITAL_VOLUME_RIGHT, val);
  1675. val = snd_soc_read(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT);
  1676. val |= WM8903_DACVU;
  1677. snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_LEFT, val);
  1678. snd_soc_write(codec, WM8903_DAC_DIGITAL_VOLUME_RIGHT, val);
  1679. val = snd_soc_read(codec, WM8903_ANALOGUE_OUT1_LEFT);
  1680. val |= WM8903_HPOUTVU;
  1681. snd_soc_write(codec, WM8903_ANALOGUE_OUT1_LEFT, val);
  1682. snd_soc_write(codec, WM8903_ANALOGUE_OUT1_RIGHT, val);
  1683. val = snd_soc_read(codec, WM8903_ANALOGUE_OUT2_LEFT);
  1684. val |= WM8903_LINEOUTVU;
  1685. snd_soc_write(codec, WM8903_ANALOGUE_OUT2_LEFT, val);
  1686. snd_soc_write(codec, WM8903_ANALOGUE_OUT2_RIGHT, val);
  1687. val = snd_soc_read(codec, WM8903_ANALOGUE_OUT3_LEFT);
  1688. val |= WM8903_SPKVU;
  1689. snd_soc_write(codec, WM8903_ANALOGUE_OUT3_LEFT, val);
  1690. snd_soc_write(codec, WM8903_ANALOGUE_OUT3_RIGHT, val);
  1691. /* Enable DAC soft mute by default */
  1692. snd_soc_update_bits(codec, WM8903_DAC_DIGITAL_1,
  1693. WM8903_DAC_MUTEMODE | WM8903_DAC_MUTE,
  1694. WM8903_DAC_MUTEMODE | WM8903_DAC_MUTE);
  1695. snd_soc_add_controls(codec, wm8903_snd_controls,
  1696. ARRAY_SIZE(wm8903_snd_controls));
  1697. wm8903_add_widgets(codec);
  1698. wm8903_init_gpio(codec);
  1699. return ret;
  1700. }
  1701. /* power down chip */
  1702. static int wm8903_remove(struct snd_soc_codec *codec)
  1703. {
  1704. wm8903_free_gpio(codec);
  1705. wm8903_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1706. return 0;
  1707. }
  1708. static struct snd_soc_codec_driver soc_codec_dev_wm8903 = {
  1709. .probe = wm8903_probe,
  1710. .remove = wm8903_remove,
  1711. .suspend = wm8903_suspend,
  1712. .resume = wm8903_resume,
  1713. .set_bias_level = wm8903_set_bias_level,
  1714. .reg_cache_size = ARRAY_SIZE(wm8903_reg_defaults),
  1715. .reg_word_size = sizeof(u16),
  1716. .reg_cache_default = wm8903_reg_defaults,
  1717. .volatile_register = wm8903_volatile_register,
  1718. .seq_notifier = wm8903_seq_notifier,
  1719. };
  1720. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1721. static __devinit int wm8903_i2c_probe(struct i2c_client *i2c,
  1722. const struct i2c_device_id *id)
  1723. {
  1724. struct wm8903_priv *wm8903;
  1725. int ret;
  1726. wm8903 = kzalloc(sizeof(struct wm8903_priv), GFP_KERNEL);
  1727. if (wm8903 == NULL)
  1728. return -ENOMEM;
  1729. i2c_set_clientdata(i2c, wm8903);
  1730. wm8903->irq = i2c->irq;
  1731. ret = snd_soc_register_codec(&i2c->dev,
  1732. &soc_codec_dev_wm8903, &wm8903_dai, 1);
  1733. if (ret < 0)
  1734. kfree(wm8903);
  1735. return ret;
  1736. }
  1737. static __devexit int wm8903_i2c_remove(struct i2c_client *client)
  1738. {
  1739. snd_soc_unregister_codec(&client->dev);
  1740. kfree(i2c_get_clientdata(client));
  1741. return 0;
  1742. }
  1743. static const struct i2c_device_id wm8903_i2c_id[] = {
  1744. { "wm8903", 0 },
  1745. { }
  1746. };
  1747. MODULE_DEVICE_TABLE(i2c, wm8903_i2c_id);
  1748. static struct i2c_driver wm8903_i2c_driver = {
  1749. .driver = {
  1750. .name = "wm8903",
  1751. .owner = THIS_MODULE,
  1752. },
  1753. .probe = wm8903_i2c_probe,
  1754. .remove = __devexit_p(wm8903_i2c_remove),
  1755. .id_table = wm8903_i2c_id,
  1756. };
  1757. #endif
  1758. static int __init wm8903_modinit(void)
  1759. {
  1760. int ret = 0;
  1761. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1762. ret = i2c_add_driver(&wm8903_i2c_driver);
  1763. if (ret != 0) {
  1764. printk(KERN_ERR "Failed to register wm8903 I2C driver: %d\n",
  1765. ret);
  1766. }
  1767. #endif
  1768. return ret;
  1769. }
  1770. module_init(wm8903_modinit);
  1771. static void __exit wm8903_exit(void)
  1772. {
  1773. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1774. i2c_del_driver(&wm8903_i2c_driver);
  1775. #endif
  1776. }
  1777. module_exit(wm8903_exit);
  1778. MODULE_DESCRIPTION("ASoC WM8903 driver");
  1779. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.cm>");
  1780. MODULE_LICENSE("GPL");