wm8903.c 65 KB

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