wm8903.c 64 KB

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