wm8903.c 51 KB

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