wm2000.c 20 KB

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