wm8961.c 32 KB

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