wm8903.c 65 KB

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