wm2000.c 20 KB

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