wm8961.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*
  2. * wm8961.c -- WM8961 ALSA SoC Audio driver
  3. *
  4. * Author: Mark Brown
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Currently unimplemented features:
  11. * - ALC
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/i2c.h>
  19. #include <linux/slab.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/initval.h>
  25. #include <sound/tlv.h>
  26. #include "wm8961.h"
  27. #define WM8961_MAX_REGISTER 0xFC
  28. static u16 wm8961_reg_defaults[] = {
  29. 0x009F, /* R0 - Left Input volume */
  30. 0x009F, /* R1 - Right Input volume */
  31. 0x0000, /* R2 - LOUT1 volume */
  32. 0x0000, /* R3 - ROUT1 volume */
  33. 0x0020, /* R4 - Clocking1 */
  34. 0x0008, /* R5 - ADC & DAC Control 1 */
  35. 0x0000, /* R6 - ADC & DAC Control 2 */
  36. 0x000A, /* R7 - Audio Interface 0 */
  37. 0x01F4, /* R8 - Clocking2 */
  38. 0x0000, /* R9 - Audio Interface 1 */
  39. 0x00FF, /* R10 - Left DAC volume */
  40. 0x00FF, /* R11 - Right DAC volume */
  41. 0x0000, /* R12 */
  42. 0x0000, /* R13 */
  43. 0x0040, /* R14 - Audio Interface 2 */
  44. 0x0000, /* R15 - Software Reset */
  45. 0x0000, /* R16 */
  46. 0x007B, /* R17 - ALC1 */
  47. 0x0000, /* R18 - ALC2 */
  48. 0x0032, /* R19 - ALC3 */
  49. 0x0000, /* R20 - Noise Gate */
  50. 0x00C0, /* R21 - Left ADC volume */
  51. 0x00C0, /* R22 - Right ADC volume */
  52. 0x0120, /* R23 - Additional control(1) */
  53. 0x0000, /* R24 - Additional control(2) */
  54. 0x0000, /* R25 - Pwr Mgmt (1) */
  55. 0x0000, /* R26 - Pwr Mgmt (2) */
  56. 0x0000, /* R27 - Additional Control (3) */
  57. 0x0000, /* R28 - Anti-pop */
  58. 0x0000, /* R29 */
  59. 0x005F, /* R30 - Clocking 3 */
  60. 0x0000, /* R31 */
  61. 0x0000, /* R32 - ADCL signal path */
  62. 0x0000, /* R33 - ADCR signal path */
  63. 0x0000, /* R34 */
  64. 0x0000, /* R35 */
  65. 0x0000, /* R36 */
  66. 0x0000, /* R37 */
  67. 0x0000, /* R38 */
  68. 0x0000, /* R39 */
  69. 0x0000, /* R40 - LOUT2 volume */
  70. 0x0000, /* R41 - ROUT2 volume */
  71. 0x0000, /* R42 */
  72. 0x0000, /* R43 */
  73. 0x0000, /* R44 */
  74. 0x0000, /* R45 */
  75. 0x0000, /* R46 */
  76. 0x0000, /* R47 - Pwr Mgmt (3) */
  77. 0x0023, /* R48 - Additional Control (4) */
  78. 0x0000, /* R49 - Class D Control 1 */
  79. 0x0000, /* R50 */
  80. 0x0003, /* R51 - Class D Control 2 */
  81. 0x0000, /* R52 */
  82. 0x0000, /* R53 */
  83. 0x0000, /* R54 */
  84. 0x0000, /* R55 */
  85. 0x0106, /* R56 - Clocking 4 */
  86. 0x0000, /* R57 - DSP Sidetone 0 */
  87. 0x0000, /* R58 - DSP Sidetone 1 */
  88. 0x0000, /* R59 */
  89. 0x0000, /* R60 - DC Servo 0 */
  90. 0x0000, /* R61 - DC Servo 1 */
  91. 0x0000, /* R62 */
  92. 0x015E, /* R63 - DC Servo 3 */
  93. 0x0010, /* R64 */
  94. 0x0010, /* R65 - DC Servo 5 */
  95. 0x0000, /* R66 */
  96. 0x0001, /* R67 */
  97. 0x0003, /* R68 - Analogue PGA Bias */
  98. 0x0000, /* R69 - Analogue HP 0 */
  99. 0x0060, /* R70 */
  100. 0x01FB, /* R71 - Analogue HP 2 */
  101. 0x0000, /* R72 - Charge Pump 1 */
  102. 0x0065, /* R73 */
  103. 0x005F, /* R74 */
  104. 0x0059, /* R75 */
  105. 0x006B, /* R76 */
  106. 0x0038, /* R77 */
  107. 0x000C, /* R78 */
  108. 0x000A, /* R79 */
  109. 0x006B, /* R80 */
  110. 0x0000, /* R81 */
  111. 0x0000, /* R82 - Charge Pump B */
  112. 0x0087, /* R83 */
  113. 0x0000, /* R84 */
  114. 0x005C, /* R85 */
  115. 0x0000, /* R86 */
  116. 0x0000, /* R87 - Write Sequencer 1 */
  117. 0x0000, /* R88 - Write Sequencer 2 */
  118. 0x0000, /* R89 - Write Sequencer 3 */
  119. 0x0000, /* R90 - Write Sequencer 4 */
  120. 0x0000, /* R91 - Write Sequencer 5 */
  121. 0x0000, /* R92 - Write Sequencer 6 */
  122. 0x0000, /* R93 - Write Sequencer 7 */
  123. 0x0000, /* R94 */
  124. 0x0000, /* R95 */
  125. 0x0000, /* R96 */
  126. 0x0000, /* R97 */
  127. 0x0000, /* R98 */
  128. 0x0000, /* R99 */
  129. 0x0000, /* R100 */
  130. 0x0000, /* R101 */
  131. 0x0000, /* R102 */
  132. 0x0000, /* R103 */
  133. 0x0000, /* R104 */
  134. 0x0000, /* R105 */
  135. 0x0000, /* R106 */
  136. 0x0000, /* R107 */
  137. 0x0000, /* R108 */
  138. 0x0000, /* R109 */
  139. 0x0000, /* R110 */
  140. 0x0000, /* R111 */
  141. 0x0000, /* R112 */
  142. 0x0000, /* R113 */
  143. 0x0000, /* R114 */
  144. 0x0000, /* R115 */
  145. 0x0000, /* R116 */
  146. 0x0000, /* R117 */
  147. 0x0000, /* R118 */
  148. 0x0000, /* R119 */
  149. 0x0000, /* R120 */
  150. 0x0000, /* R121 */
  151. 0x0000, /* R122 */
  152. 0x0000, /* R123 */
  153. 0x0000, /* R124 */
  154. 0x0000, /* R125 */
  155. 0x0000, /* R126 */
  156. 0x0000, /* R127 */
  157. 0x0000, /* R128 */
  158. 0x0000, /* R129 */
  159. 0x0000, /* R130 */
  160. 0x0000, /* R131 */
  161. 0x0000, /* R132 */
  162. 0x0000, /* R133 */
  163. 0x0000, /* R134 */
  164. 0x0000, /* R135 */
  165. 0x0000, /* R136 */
  166. 0x0000, /* R137 */
  167. 0x0000, /* R138 */
  168. 0x0000, /* R139 */
  169. 0x0000, /* R140 */
  170. 0x0000, /* R141 */
  171. 0x0000, /* R142 */
  172. 0x0000, /* R143 */
  173. 0x0000, /* R144 */
  174. 0x0000, /* R145 */
  175. 0x0000, /* R146 */
  176. 0x0000, /* R147 */
  177. 0x0000, /* R148 */
  178. 0x0000, /* R149 */
  179. 0x0000, /* R150 */
  180. 0x0000, /* R151 */
  181. 0x0000, /* R152 */
  182. 0x0000, /* R153 */
  183. 0x0000, /* R154 */
  184. 0x0000, /* R155 */
  185. 0x0000, /* R156 */
  186. 0x0000, /* R157 */
  187. 0x0000, /* R158 */
  188. 0x0000, /* R159 */
  189. 0x0000, /* R160 */
  190. 0x0000, /* R161 */
  191. 0x0000, /* R162 */
  192. 0x0000, /* R163 */
  193. 0x0000, /* R164 */
  194. 0x0000, /* R165 */
  195. 0x0000, /* R166 */
  196. 0x0000, /* R167 */
  197. 0x0000, /* R168 */
  198. 0x0000, /* R169 */
  199. 0x0000, /* R170 */
  200. 0x0000, /* R171 */
  201. 0x0000, /* R172 */
  202. 0x0000, /* R173 */
  203. 0x0000, /* R174 */
  204. 0x0000, /* R175 */
  205. 0x0000, /* R176 */
  206. 0x0000, /* R177 */
  207. 0x0000, /* R178 */
  208. 0x0000, /* R179 */
  209. 0x0000, /* R180 */
  210. 0x0000, /* R181 */
  211. 0x0000, /* R182 */
  212. 0x0000, /* R183 */
  213. 0x0000, /* R184 */
  214. 0x0000, /* R185 */
  215. 0x0000, /* R186 */
  216. 0x0000, /* R187 */
  217. 0x0000, /* R188 */
  218. 0x0000, /* R189 */
  219. 0x0000, /* R190 */
  220. 0x0000, /* R191 */
  221. 0x0000, /* R192 */
  222. 0x0000, /* R193 */
  223. 0x0000, /* R194 */
  224. 0x0000, /* R195 */
  225. 0x0030, /* R196 */
  226. 0x0006, /* R197 */
  227. 0x0000, /* R198 */
  228. 0x0060, /* R199 */
  229. 0x0000, /* R200 */
  230. 0x003F, /* R201 */
  231. 0x0000, /* R202 */
  232. 0x0000, /* R203 */
  233. 0x0000, /* R204 */
  234. 0x0001, /* R205 */
  235. 0x0000, /* R206 */
  236. 0x0181, /* R207 */
  237. 0x0005, /* R208 */
  238. 0x0008, /* R209 */
  239. 0x0008, /* R210 */
  240. 0x0000, /* R211 */
  241. 0x013B, /* R212 */
  242. 0x0000, /* R213 */
  243. 0x0000, /* R214 */
  244. 0x0000, /* R215 */
  245. 0x0000, /* R216 */
  246. 0x0070, /* R217 */
  247. 0x0000, /* R218 */
  248. 0x0000, /* R219 */
  249. 0x0000, /* R220 */
  250. 0x0000, /* R221 */
  251. 0x0000, /* R222 */
  252. 0x0003, /* R223 */
  253. 0x0000, /* R224 */
  254. 0x0000, /* R225 */
  255. 0x0001, /* R226 */
  256. 0x0008, /* R227 */
  257. 0x0000, /* R228 */
  258. 0x0000, /* R229 */
  259. 0x0000, /* R230 */
  260. 0x0000, /* R231 */
  261. 0x0004, /* R232 */
  262. 0x0000, /* R233 */
  263. 0x0000, /* R234 */
  264. 0x0000, /* R235 */
  265. 0x0000, /* R236 */
  266. 0x0000, /* R237 */
  267. 0x0080, /* R238 */
  268. 0x0000, /* R239 */
  269. 0x0000, /* R240 */
  270. 0x0000, /* R241 */
  271. 0x0000, /* R242 */
  272. 0x0000, /* R243 */
  273. 0x0000, /* R244 */
  274. 0x0052, /* R245 */
  275. 0x0110, /* R246 */
  276. 0x0040, /* R247 */
  277. 0x0000, /* R248 */
  278. 0x0030, /* R249 */
  279. 0x0000, /* R250 */
  280. 0x0000, /* R251 */
  281. 0x0001, /* R252 - General test 1 */
  282. };
  283. struct wm8961_priv {
  284. enum snd_soc_control_type control_type;
  285. int sysclk;
  286. };
  287. static int wm8961_volatile_register(struct snd_soc_codec *codec, unsigned int reg)
  288. {
  289. switch (reg) {
  290. case WM8961_SOFTWARE_RESET:
  291. case WM8961_WRITE_SEQUENCER_7:
  292. case WM8961_DC_SERVO_1:
  293. return 1;
  294. default:
  295. return 0;
  296. }
  297. }
  298. static int wm8961_reset(struct snd_soc_codec *codec)
  299. {
  300. return snd_soc_write(codec, WM8961_SOFTWARE_RESET, 0);
  301. }
  302. /*
  303. * The headphone output supports special anti-pop sequences giving
  304. * silent power up and power down.
  305. */
  306. static int wm8961_hp_event(struct snd_soc_dapm_widget *w,
  307. struct snd_kcontrol *kcontrol, int event)
  308. {
  309. struct snd_soc_codec *codec = w->codec;
  310. u16 hp_reg = snd_soc_read(codec, WM8961_ANALOGUE_HP_0);
  311. u16 cp_reg = snd_soc_read(codec, WM8961_CHARGE_PUMP_1);
  312. u16 pwr_reg = snd_soc_read(codec, WM8961_PWR_MGMT_2);
  313. u16 dcs_reg = snd_soc_read(codec, WM8961_DC_SERVO_1);
  314. int timeout = 500;
  315. if (event & SND_SOC_DAPM_POST_PMU) {
  316. /* Make sure the output is shorted */
  317. hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
  318. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  319. /* Enable the charge pump */
  320. cp_reg |= WM8961_CP_ENA;
  321. snd_soc_write(codec, WM8961_CHARGE_PUMP_1, cp_reg);
  322. mdelay(5);
  323. /* Enable the PGA */
  324. pwr_reg |= WM8961_LOUT1_PGA | WM8961_ROUT1_PGA;
  325. snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
  326. /* Enable the amplifier */
  327. hp_reg |= WM8961_HPR_ENA | WM8961_HPL_ENA;
  328. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  329. /* Second stage enable */
  330. hp_reg |= WM8961_HPR_ENA_DLY | WM8961_HPL_ENA_DLY;
  331. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  332. /* Enable the DC servo & trigger startup */
  333. dcs_reg |=
  334. WM8961_DCS_ENA_CHAN_HPR | WM8961_DCS_TRIG_STARTUP_HPR |
  335. WM8961_DCS_ENA_CHAN_HPL | WM8961_DCS_TRIG_STARTUP_HPL;
  336. dev_dbg(codec->dev, "Enabling DC servo\n");
  337. snd_soc_write(codec, WM8961_DC_SERVO_1, dcs_reg);
  338. do {
  339. msleep(1);
  340. dcs_reg = snd_soc_read(codec, WM8961_DC_SERVO_1);
  341. } while (--timeout &&
  342. dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
  343. WM8961_DCS_TRIG_STARTUP_HPL));
  344. if (dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
  345. WM8961_DCS_TRIG_STARTUP_HPL))
  346. dev_err(codec->dev, "DC servo timed out\n");
  347. else
  348. dev_dbg(codec->dev, "DC servo startup complete\n");
  349. /* Enable the output stage */
  350. hp_reg |= WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP;
  351. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  352. /* Remove the short on the output stage */
  353. hp_reg |= WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT;
  354. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  355. }
  356. if (event & SND_SOC_DAPM_PRE_PMD) {
  357. /* Short the output */
  358. hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
  359. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  360. /* Disable the output stage */
  361. hp_reg &= ~(WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP);
  362. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  363. /* Disable DC offset cancellation */
  364. dcs_reg &= ~(WM8961_DCS_ENA_CHAN_HPR |
  365. WM8961_DCS_ENA_CHAN_HPL);
  366. snd_soc_write(codec, WM8961_DC_SERVO_1, dcs_reg);
  367. /* Finish up */
  368. hp_reg &= ~(WM8961_HPR_ENA_DLY | WM8961_HPR_ENA |
  369. WM8961_HPL_ENA_DLY | WM8961_HPL_ENA);
  370. snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
  371. /* Disable the PGA */
  372. pwr_reg &= ~(WM8961_LOUT1_PGA | WM8961_ROUT1_PGA);
  373. snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
  374. /* Disable the charge pump */
  375. dev_dbg(codec->dev, "Disabling charge pump\n");
  376. snd_soc_write(codec, WM8961_CHARGE_PUMP_1,
  377. cp_reg & ~WM8961_CP_ENA);
  378. }
  379. return 0;
  380. }
  381. static int wm8961_spk_event(struct snd_soc_dapm_widget *w,
  382. struct snd_kcontrol *kcontrol, int event)
  383. {
  384. struct snd_soc_codec *codec = w->codec;
  385. u16 pwr_reg = snd_soc_read(codec, WM8961_PWR_MGMT_2);
  386. u16 spk_reg = snd_soc_read(codec, WM8961_CLASS_D_CONTROL_1);
  387. if (event & SND_SOC_DAPM_POST_PMU) {
  388. /* Enable the PGA */
  389. pwr_reg |= WM8961_SPKL_PGA | WM8961_SPKR_PGA;
  390. snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
  391. /* Enable the amplifier */
  392. spk_reg |= WM8961_SPKL_ENA | WM8961_SPKR_ENA;
  393. snd_soc_write(codec, WM8961_CLASS_D_CONTROL_1, spk_reg);
  394. }
  395. if (event & SND_SOC_DAPM_PRE_PMD) {
  396. /* Disable the amplifier */
  397. spk_reg &= ~(WM8961_SPKL_ENA | WM8961_SPKR_ENA);
  398. snd_soc_write(codec, WM8961_CLASS_D_CONTROL_1, spk_reg);
  399. /* Disable the PGA */
  400. pwr_reg &= ~(WM8961_SPKL_PGA | WM8961_SPKR_PGA);
  401. snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
  402. }
  403. return 0;
  404. }
  405. static const char *adc_hpf_text[] = {
  406. "Hi-fi", "Voice 1", "Voice 2", "Voice 3",
  407. };
  408. static const struct soc_enum adc_hpf =
  409. SOC_ENUM_SINGLE(WM8961_ADC_DAC_CONTROL_2, 7, 4, adc_hpf_text);
  410. static const char *dac_deemph_text[] = {
  411. "None", "32kHz", "44.1kHz", "48kHz",
  412. };
  413. static const struct soc_enum dac_deemph =
  414. SOC_ENUM_SINGLE(WM8961_ADC_DAC_CONTROL_1, 1, 4, dac_deemph_text);
  415. static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
  416. static const DECLARE_TLV_DB_SCALE(hp_sec_tlv, -700, 100, 0);
  417. static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);
  418. static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
  419. static unsigned int boost_tlv[] = {
  420. TLV_DB_RANGE_HEAD(4),
  421. 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
  422. 1, 1, TLV_DB_SCALE_ITEM(13, 0, 0),
  423. 2, 2, TLV_DB_SCALE_ITEM(20, 0, 0),
  424. 3, 3, TLV_DB_SCALE_ITEM(29, 0, 0),
  425. };
  426. static const DECLARE_TLV_DB_SCALE(pga_tlv, -2325, 75, 0);
  427. static const struct snd_kcontrol_new wm8961_snd_controls[] = {
  428. SOC_DOUBLE_R_TLV("Headphone Volume", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
  429. 0, 127, 0, out_tlv),
  430. SOC_DOUBLE_TLV("Headphone Secondary Volume", WM8961_ANALOGUE_HP_2,
  431. 6, 3, 7, 0, hp_sec_tlv),
  432. SOC_DOUBLE_R("Headphone ZC Switch", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
  433. 7, 1, 0),
  434. SOC_DOUBLE_R_TLV("Speaker Volume", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
  435. 0, 127, 0, out_tlv),
  436. SOC_DOUBLE_R("Speaker ZC Switch", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
  437. 7, 1, 0),
  438. SOC_SINGLE("Speaker AC Gain", WM8961_CLASS_D_CONTROL_2, 0, 7, 0),
  439. SOC_SINGLE("DAC x128 OSR Switch", WM8961_ADC_DAC_CONTROL_2, 0, 1, 0),
  440. SOC_ENUM("DAC Deemphasis", dac_deemph),
  441. SOC_SINGLE("DAC Soft Mute Switch", WM8961_ADC_DAC_CONTROL_2, 3, 1, 0),
  442. SOC_DOUBLE_R_TLV("Sidetone Volume", WM8961_DSP_SIDETONE_0,
  443. WM8961_DSP_SIDETONE_1, 4, 12, 0, sidetone_tlv),
  444. SOC_SINGLE("ADC High Pass Filter Switch", WM8961_ADC_DAC_CONTROL_1, 0, 1, 0),
  445. SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
  446. SOC_DOUBLE_R_TLV("Capture Volume",
  447. WM8961_LEFT_ADC_VOLUME, WM8961_RIGHT_ADC_VOLUME,
  448. 1, 119, 0, adc_tlv),
  449. SOC_DOUBLE_R_TLV("Capture Boost Volume",
  450. WM8961_ADCL_SIGNAL_PATH, WM8961_ADCR_SIGNAL_PATH,
  451. 4, 3, 0, boost_tlv),
  452. SOC_DOUBLE_R_TLV("Capture PGA Volume",
  453. WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
  454. 0, 62, 0, pga_tlv),
  455. SOC_DOUBLE_R("Capture PGA ZC Switch",
  456. WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
  457. 6, 1, 1),
  458. SOC_DOUBLE_R("Capture PGA Switch",
  459. WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
  460. 7, 1, 1),
  461. };
  462. static const char *sidetone_text[] = {
  463. "None", "Left", "Right"
  464. };
  465. static const struct soc_enum dacl_sidetone =
  466. SOC_ENUM_SINGLE(WM8961_DSP_SIDETONE_0, 2, 3, sidetone_text);
  467. static const struct soc_enum dacr_sidetone =
  468. SOC_ENUM_SINGLE(WM8961_DSP_SIDETONE_1, 2, 3, sidetone_text);
  469. static const struct snd_kcontrol_new dacl_mux =
  470. SOC_DAPM_ENUM("DACL Sidetone", dacl_sidetone);
  471. static const struct snd_kcontrol_new dacr_mux =
  472. SOC_DAPM_ENUM("DACR Sidetone", dacr_sidetone);
  473. static const struct snd_soc_dapm_widget wm8961_dapm_widgets[] = {
  474. SND_SOC_DAPM_INPUT("LINPUT"),
  475. SND_SOC_DAPM_INPUT("RINPUT"),
  476. SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8961_CLOCKING2, 4, 0, NULL, 0),
  477. SND_SOC_DAPM_PGA("Left Input", WM8961_PWR_MGMT_1, 5, 0, NULL, 0),
  478. SND_SOC_DAPM_PGA("Right Input", WM8961_PWR_MGMT_1, 4, 0, NULL, 0),
  479. SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", WM8961_PWR_MGMT_1, 3, 0),
  480. SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", WM8961_PWR_MGMT_1, 2, 0),
  481. SND_SOC_DAPM_SUPPLY("MICBIAS", WM8961_PWR_MGMT_1, 1, 0, NULL, 0),
  482. SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &dacl_mux),
  483. SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &dacr_mux),
  484. SND_SOC_DAPM_DAC("DACL", "HiFi Playback", WM8961_PWR_MGMT_2, 8, 0),
  485. SND_SOC_DAPM_DAC("DACR", "HiFi Playback", WM8961_PWR_MGMT_2, 7, 0),
  486. /* Handle as a mono path for DCS */
  487. SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM,
  488. 4, 0, NULL, 0, wm8961_hp_event,
  489. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  490. SND_SOC_DAPM_PGA_E("Speaker Output", SND_SOC_NOPM,
  491. 4, 0, NULL, 0, wm8961_spk_event,
  492. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  493. SND_SOC_DAPM_OUTPUT("HP_L"),
  494. SND_SOC_DAPM_OUTPUT("HP_R"),
  495. SND_SOC_DAPM_OUTPUT("SPK_LN"),
  496. SND_SOC_DAPM_OUTPUT("SPK_LP"),
  497. SND_SOC_DAPM_OUTPUT("SPK_RN"),
  498. SND_SOC_DAPM_OUTPUT("SPK_RP"),
  499. };
  500. static const struct snd_soc_dapm_route audio_paths[] = {
  501. { "DACL", NULL, "CLK_DSP" },
  502. { "DACL", NULL, "DACL Sidetone" },
  503. { "DACR", NULL, "CLK_DSP" },
  504. { "DACR", NULL, "DACR Sidetone" },
  505. { "DACL Sidetone", "Left", "ADCL" },
  506. { "DACL Sidetone", "Right", "ADCR" },
  507. { "DACR Sidetone", "Left", "ADCL" },
  508. { "DACR Sidetone", "Right", "ADCR" },
  509. { "HP_L", NULL, "Headphone Output" },
  510. { "HP_R", NULL, "Headphone Output" },
  511. { "Headphone Output", NULL, "DACL" },
  512. { "Headphone Output", NULL, "DACR" },
  513. { "SPK_LN", NULL, "Speaker Output" },
  514. { "SPK_LP", NULL, "Speaker Output" },
  515. { "SPK_RN", NULL, "Speaker Output" },
  516. { "SPK_RP", NULL, "Speaker Output" },
  517. { "Speaker Output", NULL, "DACL" },
  518. { "Speaker Output", NULL, "DACR" },
  519. { "ADCL", NULL, "Left Input" },
  520. { "ADCL", NULL, "CLK_DSP" },
  521. { "ADCR", NULL, "Right Input" },
  522. { "ADCR", NULL, "CLK_DSP" },
  523. { "Left Input", NULL, "LINPUT" },
  524. { "Right Input", NULL, "RINPUT" },
  525. };
  526. /* Values for CLK_SYS_RATE */
  527. static struct {
  528. int ratio;
  529. u16 val;
  530. } wm8961_clk_sys_ratio[] = {
  531. { 64, 0 },
  532. { 128, 1 },
  533. { 192, 2 },
  534. { 256, 3 },
  535. { 384, 4 },
  536. { 512, 5 },
  537. { 768, 6 },
  538. { 1024, 7 },
  539. { 1408, 8 },
  540. { 1536, 9 },
  541. };
  542. /* Values for SAMPLE_RATE */
  543. static struct {
  544. int rate;
  545. u16 val;
  546. } wm8961_srate[] = {
  547. { 48000, 0 },
  548. { 44100, 0 },
  549. { 32000, 1 },
  550. { 22050, 2 },
  551. { 24000, 2 },
  552. { 16000, 3 },
  553. { 11250, 4 },
  554. { 12000, 4 },
  555. { 8000, 5 },
  556. };
  557. static int wm8961_hw_params(struct snd_pcm_substream *substream,
  558. struct snd_pcm_hw_params *params,
  559. struct snd_soc_dai *dai)
  560. {
  561. struct snd_soc_codec *codec = dai->codec;
  562. struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
  563. int i, best, target, fs;
  564. u16 reg;
  565. fs = params_rate(params);
  566. if (!wm8961->sysclk) {
  567. dev_err(codec->dev, "MCLK has not been specified\n");
  568. return -EINVAL;
  569. }
  570. /* Find the closest sample rate for the filters */
  571. best = 0;
  572. for (i = 0; i < ARRAY_SIZE(wm8961_srate); i++) {
  573. if (abs(wm8961_srate[i].rate - fs) <
  574. abs(wm8961_srate[best].rate - fs))
  575. best = i;
  576. }
  577. reg = snd_soc_read(codec, WM8961_ADDITIONAL_CONTROL_3);
  578. reg &= ~WM8961_SAMPLE_RATE_MASK;
  579. reg |= wm8961_srate[best].val;
  580. snd_soc_write(codec, WM8961_ADDITIONAL_CONTROL_3, reg);
  581. dev_dbg(codec->dev, "Selected SRATE %dHz for %dHz\n",
  582. wm8961_srate[best].rate, fs);
  583. /* Select a CLK_SYS/fs ratio equal to or higher than required */
  584. target = wm8961->sysclk / fs;
  585. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && target < 64) {
  586. dev_err(codec->dev,
  587. "SYSCLK must be at least 64*fs for DAC\n");
  588. return -EINVAL;
  589. }
  590. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && target < 256) {
  591. dev_err(codec->dev,
  592. "SYSCLK must be at least 256*fs for ADC\n");
  593. return -EINVAL;
  594. }
  595. for (i = 0; i < ARRAY_SIZE(wm8961_clk_sys_ratio); i++) {
  596. if (wm8961_clk_sys_ratio[i].ratio >= target)
  597. break;
  598. }
  599. if (i == ARRAY_SIZE(wm8961_clk_sys_ratio)) {
  600. dev_err(codec->dev, "Unable to generate CLK_SYS_RATE\n");
  601. return -EINVAL;
  602. }
  603. dev_dbg(codec->dev, "Selected CLK_SYS_RATE of %d for %d/%d=%d\n",
  604. wm8961_clk_sys_ratio[i].ratio, wm8961->sysclk, fs,
  605. wm8961->sysclk / fs);
  606. reg = snd_soc_read(codec, WM8961_CLOCKING_4);
  607. reg &= ~WM8961_CLK_SYS_RATE_MASK;
  608. reg |= wm8961_clk_sys_ratio[i].val << WM8961_CLK_SYS_RATE_SHIFT;
  609. snd_soc_write(codec, WM8961_CLOCKING_4, reg);
  610. reg = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_0);
  611. reg &= ~WM8961_WL_MASK;
  612. switch (params_format(params)) {
  613. case SNDRV_PCM_FORMAT_S16_LE:
  614. break;
  615. case SNDRV_PCM_FORMAT_S20_3LE:
  616. reg |= 1 << WM8961_WL_SHIFT;
  617. break;
  618. case SNDRV_PCM_FORMAT_S24_LE:
  619. reg |= 2 << WM8961_WL_SHIFT;
  620. break;
  621. case SNDRV_PCM_FORMAT_S32_LE:
  622. reg |= 3 << WM8961_WL_SHIFT;
  623. break;
  624. default:
  625. return -EINVAL;
  626. }
  627. snd_soc_write(codec, WM8961_AUDIO_INTERFACE_0, reg);
  628. /* Sloping stop-band filter is recommended for <= 24kHz */
  629. reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_2);
  630. if (fs <= 24000)
  631. reg |= WM8961_DACSLOPE;
  632. else
  633. reg &= ~WM8961_DACSLOPE;
  634. snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_2, reg);
  635. return 0;
  636. }
  637. static int wm8961_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  638. unsigned int freq,
  639. int dir)
  640. {
  641. struct snd_soc_codec *codec = dai->codec;
  642. struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
  643. u16 reg = snd_soc_read(codec, WM8961_CLOCKING1);
  644. if (freq > 33000000) {
  645. dev_err(codec->dev, "MCLK must be <33MHz\n");
  646. return -EINVAL;
  647. }
  648. if (freq > 16500000) {
  649. dev_dbg(codec->dev, "Using MCLK/2 for %dHz MCLK\n", freq);
  650. reg |= WM8961_MCLKDIV;
  651. freq /= 2;
  652. } else {
  653. dev_dbg(codec->dev, "Using MCLK/1 for %dHz MCLK\n", freq);
  654. reg &= ~WM8961_MCLKDIV;
  655. }
  656. snd_soc_write(codec, WM8961_CLOCKING1, reg);
  657. wm8961->sysclk = freq;
  658. return 0;
  659. }
  660. static int wm8961_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  661. {
  662. struct snd_soc_codec *codec = dai->codec;
  663. u16 aif = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_0);
  664. aif &= ~(WM8961_BCLKINV | WM8961_LRP |
  665. WM8961_MS | WM8961_FORMAT_MASK);
  666. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  667. case SND_SOC_DAIFMT_CBM_CFM:
  668. aif |= WM8961_MS;
  669. break;
  670. case SND_SOC_DAIFMT_CBS_CFS:
  671. break;
  672. default:
  673. return -EINVAL;
  674. }
  675. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  676. case SND_SOC_DAIFMT_RIGHT_J:
  677. break;
  678. case SND_SOC_DAIFMT_LEFT_J:
  679. aif |= 1;
  680. break;
  681. case SND_SOC_DAIFMT_I2S:
  682. aif |= 2;
  683. break;
  684. case SND_SOC_DAIFMT_DSP_B:
  685. aif |= WM8961_LRP;
  686. case SND_SOC_DAIFMT_DSP_A:
  687. aif |= 3;
  688. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  689. case SND_SOC_DAIFMT_NB_NF:
  690. case SND_SOC_DAIFMT_IB_NF:
  691. break;
  692. default:
  693. return -EINVAL;
  694. }
  695. break;
  696. default:
  697. return -EINVAL;
  698. }
  699. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  700. case SND_SOC_DAIFMT_NB_NF:
  701. break;
  702. case SND_SOC_DAIFMT_NB_IF:
  703. aif |= WM8961_LRP;
  704. break;
  705. case SND_SOC_DAIFMT_IB_NF:
  706. aif |= WM8961_BCLKINV;
  707. break;
  708. case SND_SOC_DAIFMT_IB_IF:
  709. aif |= WM8961_BCLKINV | WM8961_LRP;
  710. break;
  711. default:
  712. return -EINVAL;
  713. }
  714. return snd_soc_write(codec, WM8961_AUDIO_INTERFACE_0, aif);
  715. }
  716. static int wm8961_set_tristate(struct snd_soc_dai *dai, int tristate)
  717. {
  718. struct snd_soc_codec *codec = dai->codec;
  719. u16 reg = snd_soc_read(codec, WM8961_ADDITIONAL_CONTROL_2);
  720. if (tristate)
  721. reg |= WM8961_TRIS;
  722. else
  723. reg &= ~WM8961_TRIS;
  724. return snd_soc_write(codec, WM8961_ADDITIONAL_CONTROL_2, reg);
  725. }
  726. static int wm8961_digital_mute(struct snd_soc_dai *dai, int mute)
  727. {
  728. struct snd_soc_codec *codec = dai->codec;
  729. u16 reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_1);
  730. if (mute)
  731. reg |= WM8961_DACMU;
  732. else
  733. reg &= ~WM8961_DACMU;
  734. msleep(17);
  735. return snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_1, reg);
  736. }
  737. static int wm8961_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
  738. {
  739. struct snd_soc_codec *codec = dai->codec;
  740. u16 reg;
  741. switch (div_id) {
  742. case WM8961_BCLK:
  743. reg = snd_soc_read(codec, WM8961_CLOCKING2);
  744. reg &= ~WM8961_BCLKDIV_MASK;
  745. reg |= div;
  746. snd_soc_write(codec, WM8961_CLOCKING2, reg);
  747. break;
  748. case WM8961_LRCLK:
  749. reg = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_2);
  750. reg &= ~WM8961_LRCLK_RATE_MASK;
  751. reg |= div;
  752. snd_soc_write(codec, WM8961_AUDIO_INTERFACE_2, reg);
  753. break;
  754. default:
  755. return -EINVAL;
  756. }
  757. return 0;
  758. }
  759. static int wm8961_set_bias_level(struct snd_soc_codec *codec,
  760. enum snd_soc_bias_level level)
  761. {
  762. u16 reg;
  763. /* This is all slightly unusual since we have no bypass paths
  764. * and the output amplifier structure means we can just slam
  765. * the biases straight up rather than having to ramp them
  766. * slowly.
  767. */
  768. switch (level) {
  769. case SND_SOC_BIAS_ON:
  770. break;
  771. case SND_SOC_BIAS_PREPARE:
  772. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
  773. /* Enable bias generation */
  774. reg = snd_soc_read(codec, WM8961_ANTI_POP);
  775. reg |= WM8961_BUFIOEN | WM8961_BUFDCOPEN;
  776. snd_soc_write(codec, WM8961_ANTI_POP, reg);
  777. /* VMID=2*50k, VREF */
  778. reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
  779. reg &= ~WM8961_VMIDSEL_MASK;
  780. reg |= (1 << WM8961_VMIDSEL_SHIFT) | WM8961_VREF;
  781. snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
  782. }
  783. break;
  784. case SND_SOC_BIAS_STANDBY:
  785. if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
  786. /* VREF off */
  787. reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
  788. reg &= ~WM8961_VREF;
  789. snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
  790. /* Bias generation off */
  791. reg = snd_soc_read(codec, WM8961_ANTI_POP);
  792. reg &= ~(WM8961_BUFIOEN | WM8961_BUFDCOPEN);
  793. snd_soc_write(codec, WM8961_ANTI_POP, reg);
  794. /* VMID off */
  795. reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
  796. reg &= ~WM8961_VMIDSEL_MASK;
  797. snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
  798. }
  799. break;
  800. case SND_SOC_BIAS_OFF:
  801. break;
  802. }
  803. codec->dapm.bias_level = level;
  804. return 0;
  805. }
  806. #define WM8961_RATES SNDRV_PCM_RATE_8000_48000
  807. #define WM8961_FORMATS \
  808. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  809. SNDRV_PCM_FMTBIT_S24_LE)
  810. static const struct snd_soc_dai_ops wm8961_dai_ops = {
  811. .hw_params = wm8961_hw_params,
  812. .set_sysclk = wm8961_set_sysclk,
  813. .set_fmt = wm8961_set_fmt,
  814. .digital_mute = wm8961_digital_mute,
  815. .set_tristate = wm8961_set_tristate,
  816. .set_clkdiv = wm8961_set_clkdiv,
  817. };
  818. static struct snd_soc_dai_driver wm8961_dai = {
  819. .name = "wm8961-hifi",
  820. .playback = {
  821. .stream_name = "HiFi Playback",
  822. .channels_min = 1,
  823. .channels_max = 2,
  824. .rates = WM8961_RATES,
  825. .formats = WM8961_FORMATS,},
  826. .capture = {
  827. .stream_name = "HiFi Capture",
  828. .channels_min = 1,
  829. .channels_max = 2,
  830. .rates = WM8961_RATES,
  831. .formats = WM8961_FORMATS,},
  832. .ops = &wm8961_dai_ops,
  833. };
  834. static int wm8961_probe(struct snd_soc_codec *codec)
  835. {
  836. struct snd_soc_dapm_context *dapm = &codec->dapm;
  837. int ret = 0;
  838. u16 reg;
  839. ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
  840. if (ret != 0) {
  841. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  842. return ret;
  843. }
  844. reg = snd_soc_read(codec, WM8961_SOFTWARE_RESET);
  845. if (reg != 0x1801) {
  846. dev_err(codec->dev, "Device is not a WM8961: ID=0x%x\n", reg);
  847. return -EINVAL;
  848. }
  849. /* This isn't volatile - readback doesn't correspond to write */
  850. codec->cache_bypass = 1;
  851. reg = snd_soc_read(codec, WM8961_RIGHT_INPUT_VOLUME);
  852. codec->cache_bypass = 0;
  853. dev_info(codec->dev, "WM8961 family %d revision %c\n",
  854. (reg & WM8961_DEVICE_ID_MASK) >> WM8961_DEVICE_ID_SHIFT,
  855. ((reg & WM8961_CHIP_REV_MASK) >> WM8961_CHIP_REV_SHIFT)
  856. + 'A');
  857. ret = wm8961_reset(codec);
  858. if (ret < 0) {
  859. dev_err(codec->dev, "Failed to issue reset\n");
  860. return ret;
  861. }
  862. /* Enable class W */
  863. reg = snd_soc_read(codec, WM8961_CHARGE_PUMP_B);
  864. reg |= WM8961_CP_DYN_PWR_MASK;
  865. snd_soc_write(codec, WM8961_CHARGE_PUMP_B, reg);
  866. /* Latch volume update bits (right channel only, we always
  867. * write both out) and default ZC on. */
  868. reg = snd_soc_read(codec, WM8961_ROUT1_VOLUME);
  869. snd_soc_write(codec, WM8961_ROUT1_VOLUME,
  870. reg | WM8961_LO1ZC | WM8961_OUT1VU);
  871. snd_soc_write(codec, WM8961_LOUT1_VOLUME, reg | WM8961_LO1ZC);
  872. reg = snd_soc_read(codec, WM8961_ROUT2_VOLUME);
  873. snd_soc_write(codec, WM8961_ROUT2_VOLUME,
  874. reg | WM8961_SPKRZC | WM8961_SPKVU);
  875. snd_soc_write(codec, WM8961_LOUT2_VOLUME, reg | WM8961_SPKLZC);
  876. reg = snd_soc_read(codec, WM8961_RIGHT_ADC_VOLUME);
  877. snd_soc_write(codec, WM8961_RIGHT_ADC_VOLUME, reg | WM8961_ADCVU);
  878. reg = snd_soc_read(codec, WM8961_RIGHT_INPUT_VOLUME);
  879. snd_soc_write(codec, WM8961_RIGHT_INPUT_VOLUME, reg | WM8961_IPVU);
  880. /* Use soft mute by default */
  881. reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_2);
  882. reg |= WM8961_DACSMM;
  883. snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_2, reg);
  884. /* Use automatic clocking mode by default; for now this is all
  885. * we support.
  886. */
  887. reg = snd_soc_read(codec, WM8961_CLOCKING_3);
  888. reg &= ~WM8961_MANUAL_MODE;
  889. snd_soc_write(codec, WM8961_CLOCKING_3, reg);
  890. wm8961_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  891. snd_soc_add_codec_controls(codec, wm8961_snd_controls,
  892. ARRAY_SIZE(wm8961_snd_controls));
  893. snd_soc_dapm_new_controls(dapm, wm8961_dapm_widgets,
  894. ARRAY_SIZE(wm8961_dapm_widgets));
  895. snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
  896. return 0;
  897. }
  898. static int wm8961_remove(struct snd_soc_codec *codec)
  899. {
  900. wm8961_set_bias_level(codec, SND_SOC_BIAS_OFF);
  901. return 0;
  902. }
  903. #ifdef CONFIG_PM
  904. static int wm8961_suspend(struct snd_soc_codec *codec)
  905. {
  906. wm8961_set_bias_level(codec, SND_SOC_BIAS_OFF);
  907. return 0;
  908. }
  909. static int wm8961_resume(struct snd_soc_codec *codec)
  910. {
  911. snd_soc_cache_sync(codec);
  912. wm8961_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  913. return 0;
  914. }
  915. #else
  916. #define wm8961_suspend NULL
  917. #define wm8961_resume NULL
  918. #endif
  919. static struct snd_soc_codec_driver soc_codec_dev_wm8961 = {
  920. .probe = wm8961_probe,
  921. .remove = wm8961_remove,
  922. .suspend = wm8961_suspend,
  923. .resume = wm8961_resume,
  924. .set_bias_level = wm8961_set_bias_level,
  925. .reg_cache_size = ARRAY_SIZE(wm8961_reg_defaults),
  926. .reg_word_size = sizeof(u16),
  927. .reg_cache_default = wm8961_reg_defaults,
  928. .volatile_register = wm8961_volatile_register,
  929. };
  930. static __devinit int wm8961_i2c_probe(struct i2c_client *i2c,
  931. const struct i2c_device_id *id)
  932. {
  933. struct wm8961_priv *wm8961;
  934. int ret;
  935. wm8961 = devm_kzalloc(&i2c->dev, sizeof(struct wm8961_priv),
  936. GFP_KERNEL);
  937. if (wm8961 == NULL)
  938. return -ENOMEM;
  939. i2c_set_clientdata(i2c, wm8961);
  940. ret = snd_soc_register_codec(&i2c->dev,
  941. &soc_codec_dev_wm8961, &wm8961_dai, 1);
  942. return ret;
  943. }
  944. static __devexit int wm8961_i2c_remove(struct i2c_client *client)
  945. {
  946. snd_soc_unregister_codec(&client->dev);
  947. return 0;
  948. }
  949. static const struct i2c_device_id wm8961_i2c_id[] = {
  950. { "wm8961", 0 },
  951. { }
  952. };
  953. MODULE_DEVICE_TABLE(i2c, wm8961_i2c_id);
  954. static struct i2c_driver wm8961_i2c_driver = {
  955. .driver = {
  956. .name = "wm8961",
  957. .owner = THIS_MODULE,
  958. },
  959. .probe = wm8961_i2c_probe,
  960. .remove = __devexit_p(wm8961_i2c_remove),
  961. .id_table = wm8961_i2c_id,
  962. };
  963. static int __init wm8961_modinit(void)
  964. {
  965. int ret = 0;
  966. ret = i2c_add_driver(&wm8961_i2c_driver);
  967. if (ret != 0) {
  968. printk(KERN_ERR "Failed to register wm8961 I2C driver: %d\n",
  969. ret);
  970. }
  971. return ret;
  972. }
  973. module_init(wm8961_modinit);
  974. static void __exit wm8961_exit(void)
  975. {
  976. i2c_del_driver(&wm8961_i2c_driver);
  977. }
  978. module_exit(wm8961_exit);
  979. MODULE_DESCRIPTION("ASoC WM8961 driver");
  980. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  981. MODULE_LICENSE("GPL");