wm8961.c 30 KB

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