wm8903.c 62 KB

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