wm2000.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. * wm2000.c -- WM2000 ALSA Soc Audio driver
  3. *
  4. * Copyright 2008-2010 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * The download image for the WM2000 will be requested as
  13. * 'wm2000_anc.bin' by default (overridable via platform data) at
  14. * runtime and is expected to be in flat binary format. This is
  15. * generated by Wolfson configuration tools and includes
  16. * system-specific callibration information. If supplied as a
  17. * sequence of ASCII-encoded hexidecimal bytes this can be converted
  18. * into a flat binary with a command such as this on the command line:
  19. *
  20. * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }'
  21. * < file > wm2000_anc.bin
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/kernel.h>
  26. #include <linux/init.h>
  27. #include <linux/firmware.h>
  28. #include <linux/delay.h>
  29. #include <linux/pm.h>
  30. #include <linux/i2c.h>
  31. #include <linux/regmap.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/slab.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include <sound/initval.h>
  39. #include <sound/tlv.h>
  40. #include <sound/wm2000.h>
  41. #include "wm2000.h"
  42. enum wm2000_anc_mode {
  43. ANC_ACTIVE = 0,
  44. ANC_BYPASS = 1,
  45. ANC_STANDBY = 2,
  46. ANC_OFF = 3,
  47. };
  48. struct wm2000_priv {
  49. struct i2c_client *i2c;
  50. struct regmap *regmap;
  51. enum wm2000_anc_mode anc_mode;
  52. unsigned int anc_active:1;
  53. unsigned int anc_eng_ena:1;
  54. unsigned int spk_ena:1;
  55. unsigned int mclk_div:1;
  56. unsigned int speech_clarity:1;
  57. int anc_download_size;
  58. char *anc_download;
  59. };
  60. static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
  61. unsigned int value)
  62. {
  63. struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
  64. return regmap_write(wm2000->regmap, reg, value);
  65. }
  66. static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
  67. {
  68. struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c);
  69. unsigned int val;
  70. int ret;
  71. ret = regmap_read(wm2000->regmap, r, &val);
  72. if (ret < 0)
  73. return -1;
  74. return val;
  75. }
  76. static void wm2000_reset(struct wm2000_priv *wm2000)
  77. {
  78. struct i2c_client *i2c = wm2000->i2c;
  79. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
  80. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
  81. wm2000_write(i2c, WM2000_REG_ID1, 0);
  82. wm2000->anc_mode = ANC_OFF;
  83. }
  84. static int wm2000_poll_bit(struct i2c_client *i2c,
  85. unsigned int reg, u8 mask, int timeout)
  86. {
  87. int val;
  88. val = wm2000_read(i2c, reg);
  89. while (!(val & mask) && --timeout) {
  90. msleep(1);
  91. val = wm2000_read(i2c, reg);
  92. }
  93. if (timeout == 0)
  94. return 0;
  95. else
  96. return 1;
  97. }
  98. static int wm2000_power_up(struct i2c_client *i2c, int analogue)
  99. {
  100. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  101. int ret, timeout;
  102. BUG_ON(wm2000->anc_mode != ANC_OFF);
  103. dev_dbg(&i2c->dev, "Beginning power up\n");
  104. if (!wm2000->mclk_div) {
  105. dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
  106. wm2000_write(i2c, WM2000_REG_SYS_CTL2,
  107. WM2000_MCLK_DIV2_ENA_CLR);
  108. } else {
  109. dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
  110. wm2000_write(i2c, WM2000_REG_SYS_CTL2,
  111. WM2000_MCLK_DIV2_ENA_SET);
  112. }
  113. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
  114. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
  115. /* Wait for ANC engine to become ready */
  116. if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
  117. WM2000_ANC_ENG_IDLE, 1)) {
  118. dev_err(&i2c->dev, "ANC engine failed to reset\n");
  119. return -ETIMEDOUT;
  120. }
  121. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  122. WM2000_STATUS_BOOT_COMPLETE, 1)) {
  123. dev_err(&i2c->dev, "ANC engine failed to initialise\n");
  124. return -ETIMEDOUT;
  125. }
  126. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
  127. /* Open code download of the data since it is the only bulk
  128. * write we do. */
  129. dev_dbg(&i2c->dev, "Downloading %d bytes\n",
  130. wm2000->anc_download_size - 2);
  131. ret = i2c_master_send(i2c, wm2000->anc_download,
  132. wm2000->anc_download_size);
  133. if (ret < 0) {
  134. dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
  135. return ret;
  136. }
  137. if (ret != wm2000->anc_download_size) {
  138. dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
  139. ret, wm2000->anc_download_size);
  140. return -EIO;
  141. }
  142. dev_dbg(&i2c->dev, "Download complete\n");
  143. if (analogue) {
  144. timeout = 248;
  145. wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
  146. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  147. WM2000_MODE_ANA_SEQ_INCLUDE |
  148. WM2000_MODE_MOUSE_ENABLE |
  149. WM2000_MODE_THERMAL_ENABLE);
  150. } else {
  151. timeout = 10;
  152. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  153. WM2000_MODE_MOUSE_ENABLE |
  154. WM2000_MODE_THERMAL_ENABLE);
  155. }
  156. ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY);
  157. if (wm2000->speech_clarity)
  158. ret |= WM2000_SPEECH_CLARITY;
  159. else
  160. ret &= ~WM2000_SPEECH_CLARITY;
  161. wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret);
  162. wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
  163. wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
  164. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
  165. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  166. WM2000_STATUS_MOUSE_ACTIVE, timeout)) {
  167. dev_err(&i2c->dev, "Timed out waiting for device after %dms\n",
  168. timeout * 10);
  169. return -ETIMEDOUT;
  170. }
  171. dev_dbg(&i2c->dev, "ANC active\n");
  172. if (analogue)
  173. dev_dbg(&i2c->dev, "Analogue active\n");
  174. wm2000->anc_mode = ANC_ACTIVE;
  175. return 0;
  176. }
  177. static int wm2000_power_down(struct i2c_client *i2c, int analogue)
  178. {
  179. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  180. int timeout;
  181. if (analogue) {
  182. timeout = 248;
  183. wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
  184. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  185. WM2000_MODE_ANA_SEQ_INCLUDE |
  186. WM2000_MODE_POWER_DOWN);
  187. } else {
  188. timeout = 10;
  189. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  190. WM2000_MODE_POWER_DOWN);
  191. }
  192. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  193. WM2000_STATUS_POWER_DOWN_COMPLETE, timeout)) {
  194. dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
  195. return -ETIMEDOUT;
  196. }
  197. if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
  198. WM2000_ANC_ENG_IDLE, 1)) {
  199. dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
  200. return -ETIMEDOUT;
  201. }
  202. dev_dbg(&i2c->dev, "powered off\n");
  203. wm2000->anc_mode = ANC_OFF;
  204. return 0;
  205. }
  206. static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
  207. {
  208. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  209. BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
  210. if (analogue) {
  211. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  212. WM2000_MODE_ANA_SEQ_INCLUDE |
  213. WM2000_MODE_THERMAL_ENABLE |
  214. WM2000_MODE_BYPASS_ENTRY);
  215. } else {
  216. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  217. WM2000_MODE_THERMAL_ENABLE |
  218. WM2000_MODE_BYPASS_ENTRY);
  219. }
  220. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  221. WM2000_STATUS_ANC_DISABLED, 10)) {
  222. dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
  223. return -ETIMEDOUT;
  224. }
  225. if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
  226. WM2000_ANC_ENG_IDLE, 1)) {
  227. dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
  228. return -ETIMEDOUT;
  229. }
  230. wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
  231. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
  232. wm2000->anc_mode = ANC_BYPASS;
  233. dev_dbg(&i2c->dev, "bypass enabled\n");
  234. return 0;
  235. }
  236. static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
  237. {
  238. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  239. BUG_ON(wm2000->anc_mode != ANC_BYPASS);
  240. wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
  241. if (analogue) {
  242. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  243. WM2000_MODE_ANA_SEQ_INCLUDE |
  244. WM2000_MODE_MOUSE_ENABLE |
  245. WM2000_MODE_THERMAL_ENABLE);
  246. } else {
  247. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  248. WM2000_MODE_MOUSE_ENABLE |
  249. WM2000_MODE_THERMAL_ENABLE);
  250. }
  251. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
  252. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
  253. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  254. WM2000_STATUS_MOUSE_ACTIVE, 10)) {
  255. dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
  256. return -ETIMEDOUT;
  257. }
  258. wm2000->anc_mode = ANC_ACTIVE;
  259. dev_dbg(&i2c->dev, "MOUSE active\n");
  260. return 0;
  261. }
  262. static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
  263. {
  264. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  265. int timeout;
  266. BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
  267. if (analogue) {
  268. timeout = 248;
  269. wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
  270. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  271. WM2000_MODE_ANA_SEQ_INCLUDE |
  272. WM2000_MODE_THERMAL_ENABLE |
  273. WM2000_MODE_STANDBY_ENTRY);
  274. } else {
  275. timeout = 10;
  276. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  277. WM2000_MODE_THERMAL_ENABLE |
  278. WM2000_MODE_STANDBY_ENTRY);
  279. }
  280. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  281. WM2000_STATUS_ANC_DISABLED, timeout)) {
  282. dev_err(&i2c->dev,
  283. "Timed out waiting for ANC disable after 1ms\n");
  284. return -ETIMEDOUT;
  285. }
  286. if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE,
  287. 1)) {
  288. dev_err(&i2c->dev,
  289. "Timed out waiting for standby after %dms\n",
  290. timeout * 10);
  291. return -ETIMEDOUT;
  292. }
  293. wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
  294. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
  295. wm2000->anc_mode = ANC_STANDBY;
  296. dev_dbg(&i2c->dev, "standby\n");
  297. if (analogue)
  298. dev_dbg(&i2c->dev, "Analogue disabled\n");
  299. return 0;
  300. }
  301. static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
  302. {
  303. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  304. int timeout;
  305. BUG_ON(wm2000->anc_mode != ANC_STANDBY);
  306. wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
  307. if (analogue) {
  308. timeout = 248;
  309. wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
  310. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  311. WM2000_MODE_ANA_SEQ_INCLUDE |
  312. WM2000_MODE_THERMAL_ENABLE |
  313. WM2000_MODE_MOUSE_ENABLE);
  314. } else {
  315. timeout = 10;
  316. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  317. WM2000_MODE_THERMAL_ENABLE |
  318. WM2000_MODE_MOUSE_ENABLE);
  319. }
  320. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
  321. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
  322. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  323. WM2000_STATUS_MOUSE_ACTIVE, timeout)) {
  324. dev_err(&i2c->dev, "Timed out waiting for MOUSE after %dms\n",
  325. timeout * 10);
  326. return -ETIMEDOUT;
  327. }
  328. wm2000->anc_mode = ANC_ACTIVE;
  329. dev_dbg(&i2c->dev, "MOUSE active\n");
  330. if (analogue)
  331. dev_dbg(&i2c->dev, "Analogue enabled\n");
  332. return 0;
  333. }
  334. typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
  335. static struct {
  336. enum wm2000_anc_mode source;
  337. enum wm2000_anc_mode dest;
  338. int analogue;
  339. wm2000_mode_fn step[2];
  340. } anc_transitions[] = {
  341. {
  342. .source = ANC_OFF,
  343. .dest = ANC_ACTIVE,
  344. .analogue = 1,
  345. .step = {
  346. wm2000_power_up,
  347. },
  348. },
  349. {
  350. .source = ANC_OFF,
  351. .dest = ANC_STANDBY,
  352. .step = {
  353. wm2000_power_up,
  354. wm2000_enter_standby,
  355. },
  356. },
  357. {
  358. .source = ANC_OFF,
  359. .dest = ANC_BYPASS,
  360. .analogue = 1,
  361. .step = {
  362. wm2000_power_up,
  363. wm2000_enter_bypass,
  364. },
  365. },
  366. {
  367. .source = ANC_ACTIVE,
  368. .dest = ANC_BYPASS,
  369. .analogue = 1,
  370. .step = {
  371. wm2000_enter_bypass,
  372. },
  373. },
  374. {
  375. .source = ANC_ACTIVE,
  376. .dest = ANC_STANDBY,
  377. .analogue = 1,
  378. .step = {
  379. wm2000_enter_standby,
  380. },
  381. },
  382. {
  383. .source = ANC_ACTIVE,
  384. .dest = ANC_OFF,
  385. .analogue = 1,
  386. .step = {
  387. wm2000_power_down,
  388. },
  389. },
  390. {
  391. .source = ANC_BYPASS,
  392. .dest = ANC_ACTIVE,
  393. .analogue = 1,
  394. .step = {
  395. wm2000_exit_bypass,
  396. },
  397. },
  398. {
  399. .source = ANC_BYPASS,
  400. .dest = ANC_STANDBY,
  401. .analogue = 1,
  402. .step = {
  403. wm2000_exit_bypass,
  404. wm2000_enter_standby,
  405. },
  406. },
  407. {
  408. .source = ANC_BYPASS,
  409. .dest = ANC_OFF,
  410. .step = {
  411. wm2000_exit_bypass,
  412. wm2000_power_down,
  413. },
  414. },
  415. {
  416. .source = ANC_STANDBY,
  417. .dest = ANC_ACTIVE,
  418. .analogue = 1,
  419. .step = {
  420. wm2000_exit_standby,
  421. },
  422. },
  423. {
  424. .source = ANC_STANDBY,
  425. .dest = ANC_BYPASS,
  426. .analogue = 1,
  427. .step = {
  428. wm2000_exit_standby,
  429. wm2000_enter_bypass,
  430. },
  431. },
  432. {
  433. .source = ANC_STANDBY,
  434. .dest = ANC_OFF,
  435. .step = {
  436. wm2000_exit_standby,
  437. wm2000_power_down,
  438. },
  439. },
  440. };
  441. static int wm2000_anc_transition(struct wm2000_priv *wm2000,
  442. enum wm2000_anc_mode mode)
  443. {
  444. struct i2c_client *i2c = wm2000->i2c;
  445. int i, j;
  446. int ret;
  447. if (wm2000->anc_mode == mode)
  448. return 0;
  449. for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
  450. if (anc_transitions[i].source == wm2000->anc_mode &&
  451. anc_transitions[i].dest == mode)
  452. break;
  453. if (i == ARRAY_SIZE(anc_transitions)) {
  454. dev_err(&i2c->dev, "No transition for %d->%d\n",
  455. wm2000->anc_mode, mode);
  456. return -EINVAL;
  457. }
  458. for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
  459. if (!anc_transitions[i].step[j])
  460. break;
  461. ret = anc_transitions[i].step[j](i2c,
  462. anc_transitions[i].analogue);
  463. if (ret != 0)
  464. return ret;
  465. }
  466. return 0;
  467. }
  468. static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
  469. {
  470. struct i2c_client *i2c = wm2000->i2c;
  471. enum wm2000_anc_mode mode;
  472. if (wm2000->anc_eng_ena && wm2000->spk_ena)
  473. if (wm2000->anc_active)
  474. mode = ANC_ACTIVE;
  475. else
  476. mode = ANC_BYPASS;
  477. else
  478. mode = ANC_STANDBY;
  479. dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
  480. mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
  481. wm2000->anc_active);
  482. return wm2000_anc_transition(wm2000, mode);
  483. }
  484. static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
  485. struct snd_ctl_elem_value *ucontrol)
  486. {
  487. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  488. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  489. ucontrol->value.integer.value[0] = wm2000->anc_active;
  490. return 0;
  491. }
  492. static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
  493. struct snd_ctl_elem_value *ucontrol)
  494. {
  495. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  496. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  497. int anc_active = ucontrol->value.integer.value[0];
  498. if (anc_active > 1)
  499. return -EINVAL;
  500. wm2000->anc_active = anc_active;
  501. return wm2000_anc_set_mode(wm2000);
  502. }
  503. static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
  504. struct snd_ctl_elem_value *ucontrol)
  505. {
  506. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  507. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  508. ucontrol->value.integer.value[0] = wm2000->spk_ena;
  509. return 0;
  510. }
  511. static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
  512. struct snd_ctl_elem_value *ucontrol)
  513. {
  514. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  515. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  516. int val = ucontrol->value.integer.value[0];
  517. if (val > 1)
  518. return -EINVAL;
  519. wm2000->spk_ena = val;
  520. return wm2000_anc_set_mode(wm2000);
  521. }
  522. static const struct snd_kcontrol_new wm2000_controls[] = {
  523. SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
  524. wm2000_anc_mode_get,
  525. wm2000_anc_mode_put),
  526. SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
  527. wm2000_speaker_get,
  528. wm2000_speaker_put),
  529. };
  530. static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
  531. struct snd_kcontrol *kcontrol, int event)
  532. {
  533. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  534. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  535. if (SND_SOC_DAPM_EVENT_ON(event))
  536. wm2000->anc_eng_ena = 1;
  537. if (SND_SOC_DAPM_EVENT_OFF(event))
  538. wm2000->anc_eng_ena = 0;
  539. return wm2000_anc_set_mode(wm2000);
  540. }
  541. static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
  542. /* Externally visible pins */
  543. SND_SOC_DAPM_OUTPUT("SPKN"),
  544. SND_SOC_DAPM_OUTPUT("SPKP"),
  545. SND_SOC_DAPM_INPUT("LINN"),
  546. SND_SOC_DAPM_INPUT("LINP"),
  547. SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
  548. wm2000_anc_power_event,
  549. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  550. };
  551. /* Target, Path, Source */
  552. static const struct snd_soc_dapm_route wm2000_audio_map[] = {
  553. { "SPKN", NULL, "ANC Engine" },
  554. { "SPKP", NULL, "ANC Engine" },
  555. { "ANC Engine", NULL, "LINN" },
  556. { "ANC Engine", NULL, "LINP" },
  557. };
  558. #ifdef CONFIG_PM
  559. static int wm2000_suspend(struct snd_soc_codec *codec)
  560. {
  561. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  562. return wm2000_anc_transition(wm2000, ANC_OFF);
  563. }
  564. static int wm2000_resume(struct snd_soc_codec *codec)
  565. {
  566. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  567. return wm2000_anc_set_mode(wm2000);
  568. }
  569. #else
  570. #define wm2000_suspend NULL
  571. #define wm2000_resume NULL
  572. #endif
  573. static const struct regmap_config wm2000_regmap = {
  574. .reg_bits = 16,
  575. .val_bits = 8,
  576. };
  577. static int wm2000_probe(struct snd_soc_codec *codec)
  578. {
  579. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  580. /* This will trigger a transition to standby mode by default */
  581. wm2000_anc_set_mode(wm2000);
  582. return 0;
  583. }
  584. static int wm2000_remove(struct snd_soc_codec *codec)
  585. {
  586. struct wm2000_priv *wm2000 = dev_get_drvdata(codec->dev);
  587. return wm2000_anc_transition(wm2000, ANC_OFF);
  588. }
  589. static struct snd_soc_codec_driver soc_codec_dev_wm2000 = {
  590. .probe = wm2000_probe,
  591. .remove = wm2000_remove,
  592. .suspend = wm2000_suspend,
  593. .resume = wm2000_resume,
  594. .dapm_widgets = wm2000_dapm_widgets,
  595. .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets),
  596. .dapm_routes = wm2000_audio_map,
  597. .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map),
  598. .controls = wm2000_controls,
  599. .num_controls = ARRAY_SIZE(wm2000_controls),
  600. };
  601. static int __devinit wm2000_i2c_probe(struct i2c_client *i2c,
  602. const struct i2c_device_id *i2c_id)
  603. {
  604. struct wm2000_priv *wm2000;
  605. struct wm2000_platform_data *pdata;
  606. const char *filename;
  607. const struct firmware *fw = NULL;
  608. int ret;
  609. int reg;
  610. u16 id;
  611. wm2000 = devm_kzalloc(&i2c->dev, sizeof(struct wm2000_priv),
  612. GFP_KERNEL);
  613. if (wm2000 == NULL) {
  614. dev_err(&i2c->dev, "Unable to allocate private data\n");
  615. return -ENOMEM;
  616. }
  617. dev_set_drvdata(&i2c->dev, wm2000);
  618. wm2000->regmap = regmap_init_i2c(i2c, &wm2000_regmap);
  619. if (IS_ERR(wm2000->regmap)) {
  620. ret = PTR_ERR(wm2000->regmap);
  621. dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
  622. ret);
  623. goto out;
  624. }
  625. /* Verify that this is a WM2000 */
  626. reg = wm2000_read(i2c, WM2000_REG_ID1);
  627. id = reg << 8;
  628. reg = wm2000_read(i2c, WM2000_REG_ID2);
  629. id |= reg & 0xff;
  630. if (id != 0x2000) {
  631. dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id);
  632. ret = -ENODEV;
  633. goto out_regmap_exit;
  634. }
  635. reg = wm2000_read(i2c, WM2000_REG_REVISON);
  636. dev_info(&i2c->dev, "revision %c\n", reg + 'A');
  637. filename = "wm2000_anc.bin";
  638. pdata = dev_get_platdata(&i2c->dev);
  639. if (pdata) {
  640. wm2000->mclk_div = pdata->mclkdiv2;
  641. wm2000->speech_clarity = !pdata->speech_enh_disable;
  642. if (pdata->download_file)
  643. filename = pdata->download_file;
  644. }
  645. ret = request_firmware(&fw, filename, &i2c->dev);
  646. if (ret != 0) {
  647. dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret);
  648. goto out_regmap_exit;
  649. }
  650. /* Pre-cook the concatenation of the register address onto the image */
  651. wm2000->anc_download_size = fw->size + 2;
  652. wm2000->anc_download = devm_kzalloc(&i2c->dev,
  653. wm2000->anc_download_size,
  654. GFP_KERNEL);
  655. if (wm2000->anc_download == NULL) {
  656. dev_err(&i2c->dev, "Out of memory\n");
  657. ret = -ENOMEM;
  658. goto out_regmap_exit;
  659. }
  660. wm2000->anc_download[0] = 0x80;
  661. wm2000->anc_download[1] = 0x00;
  662. memcpy(wm2000->anc_download + 2, fw->data, fw->size);
  663. wm2000->anc_eng_ena = 1;
  664. wm2000->anc_active = 1;
  665. wm2000->spk_ena = 1;
  666. wm2000->i2c = i2c;
  667. wm2000_reset(wm2000);
  668. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm2000, NULL, 0);
  669. if (!ret)
  670. goto out;
  671. out_regmap_exit:
  672. regmap_exit(wm2000->regmap);
  673. out:
  674. release_firmware(fw);
  675. return ret;
  676. }
  677. static __devexit int wm2000_i2c_remove(struct i2c_client *i2c)
  678. {
  679. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  680. snd_soc_unregister_codec(&i2c->dev);
  681. regmap_exit(wm2000->regmap);
  682. return 0;
  683. }
  684. static const struct i2c_device_id wm2000_i2c_id[] = {
  685. { "wm2000", 0 },
  686. { }
  687. };
  688. MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id);
  689. static struct i2c_driver wm2000_i2c_driver = {
  690. .driver = {
  691. .name = "wm2000",
  692. .owner = THIS_MODULE,
  693. },
  694. .probe = wm2000_i2c_probe,
  695. .remove = __devexit_p(wm2000_i2c_remove),
  696. .id_table = wm2000_i2c_id,
  697. };
  698. static int __init wm2000_init(void)
  699. {
  700. return i2c_add_driver(&wm2000_i2c_driver);
  701. }
  702. module_init(wm2000_init);
  703. static void __exit wm2000_exit(void)
  704. {
  705. i2c_del_driver(&wm2000_i2c_driver);
  706. }
  707. module_exit(wm2000_exit);
  708. MODULE_DESCRIPTION("ASoC WM2000 driver");
  709. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
  710. MODULE_LICENSE("GPL");