wm8903.c 51 KB

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