wm2000.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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/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. enum wm2000_anc_mode anc_mode;
  51. unsigned int anc_active:1;
  52. unsigned int anc_eng_ena:1;
  53. unsigned int spk_ena:1;
  54. unsigned int mclk_div:1;
  55. unsigned int speech_clarity:1;
  56. int anc_download_size;
  57. char *anc_download;
  58. };
  59. static struct i2c_client *wm2000_i2c;
  60. static int wm2000_write(struct i2c_client *i2c, unsigned int reg,
  61. unsigned int value)
  62. {
  63. u8 data[3];
  64. int ret;
  65. data[0] = (reg >> 8) & 0xff;
  66. data[1] = reg & 0xff;
  67. data[2] = value & 0xff;
  68. dev_vdbg(&i2c->dev, "write %x = %x\n", reg, value);
  69. ret = i2c_master_send(i2c, data, 3);
  70. if (ret == 3)
  71. return 0;
  72. if (ret < 0)
  73. return ret;
  74. else
  75. return -EIO;
  76. }
  77. static unsigned int wm2000_read(struct i2c_client *i2c, unsigned int r)
  78. {
  79. struct i2c_msg xfer[2];
  80. u8 reg[2];
  81. u8 data;
  82. int ret;
  83. /* Write register */
  84. reg[0] = (r >> 8) & 0xff;
  85. reg[1] = r & 0xff;
  86. xfer[0].addr = i2c->addr;
  87. xfer[0].flags = 0;
  88. xfer[0].len = sizeof(reg);
  89. xfer[0].buf = &reg[0];
  90. /* Read data */
  91. xfer[1].addr = i2c->addr;
  92. xfer[1].flags = I2C_M_RD;
  93. xfer[1].len = 1;
  94. xfer[1].buf = &data;
  95. ret = i2c_transfer(i2c->adapter, xfer, 2);
  96. if (ret != 2) {
  97. dev_err(&i2c->dev, "i2c_transfer() returned %d\n", ret);
  98. return 0;
  99. }
  100. dev_vdbg(&i2c->dev, "read %x from %x\n", data, r);
  101. return data;
  102. }
  103. static void wm2000_reset(struct wm2000_priv *wm2000)
  104. {
  105. struct i2c_client *i2c = wm2000->i2c;
  106. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
  107. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
  108. wm2000_write(i2c, WM2000_REG_ID1, 0);
  109. wm2000->anc_mode = ANC_OFF;
  110. }
  111. static int wm2000_poll_bit(struct i2c_client *i2c,
  112. unsigned int reg, u8 mask, int timeout)
  113. {
  114. int val;
  115. val = wm2000_read(i2c, reg);
  116. while (!(val & mask) && --timeout) {
  117. msleep(1);
  118. val = wm2000_read(i2c, reg);
  119. }
  120. if (timeout == 0)
  121. return 0;
  122. else
  123. return 1;
  124. }
  125. static int wm2000_power_up(struct i2c_client *i2c, int analogue)
  126. {
  127. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  128. int ret, timeout;
  129. BUG_ON(wm2000->anc_mode != ANC_OFF);
  130. dev_dbg(&i2c->dev, "Beginning power up\n");
  131. if (!wm2000->mclk_div) {
  132. dev_dbg(&i2c->dev, "Disabling MCLK divider\n");
  133. wm2000_write(i2c, WM2000_REG_SYS_CTL2,
  134. WM2000_MCLK_DIV2_ENA_CLR);
  135. } else {
  136. dev_dbg(&i2c->dev, "Enabling MCLK divider\n");
  137. wm2000_write(i2c, WM2000_REG_SYS_CTL2,
  138. WM2000_MCLK_DIV2_ENA_SET);
  139. }
  140. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR);
  141. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET);
  142. /* Wait for ANC engine to become ready */
  143. if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
  144. WM2000_ANC_ENG_IDLE, 1)) {
  145. dev_err(&i2c->dev, "ANC engine failed to reset\n");
  146. return -ETIMEDOUT;
  147. }
  148. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  149. WM2000_STATUS_BOOT_COMPLETE, 1)) {
  150. dev_err(&i2c->dev, "ANC engine failed to initialise\n");
  151. return -ETIMEDOUT;
  152. }
  153. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
  154. /* Open code download of the data since it is the only bulk
  155. * write we do. */
  156. dev_dbg(&i2c->dev, "Downloading %d bytes\n",
  157. wm2000->anc_download_size - 2);
  158. ret = i2c_master_send(i2c, wm2000->anc_download,
  159. wm2000->anc_download_size);
  160. if (ret < 0) {
  161. dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret);
  162. return ret;
  163. }
  164. if (ret != wm2000->anc_download_size) {
  165. dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n",
  166. ret, wm2000->anc_download_size);
  167. return -EIO;
  168. }
  169. dev_dbg(&i2c->dev, "Download complete\n");
  170. if (analogue) {
  171. timeout = 248;
  172. wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
  173. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  174. WM2000_MODE_ANA_SEQ_INCLUDE |
  175. WM2000_MODE_MOUSE_ENABLE |
  176. WM2000_MODE_THERMAL_ENABLE);
  177. } else {
  178. timeout = 10;
  179. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  180. WM2000_MODE_MOUSE_ENABLE |
  181. WM2000_MODE_THERMAL_ENABLE);
  182. }
  183. ret = wm2000_read(i2c, WM2000_REG_SPEECH_CLARITY);
  184. if (wm2000->speech_clarity)
  185. ret &= ~WM2000_SPEECH_CLARITY;
  186. else
  187. ret |= WM2000_SPEECH_CLARITY;
  188. wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, ret);
  189. wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33);
  190. wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02);
  191. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
  192. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  193. WM2000_STATUS_MOUSE_ACTIVE, timeout)) {
  194. dev_err(&i2c->dev, "Timed out waiting for device after %dms\n",
  195. timeout * 10);
  196. return -ETIMEDOUT;
  197. }
  198. dev_dbg(&i2c->dev, "ANC active\n");
  199. if (analogue)
  200. dev_dbg(&i2c->dev, "Analogue active\n");
  201. wm2000->anc_mode = ANC_ACTIVE;
  202. return 0;
  203. }
  204. static int wm2000_power_down(struct i2c_client *i2c, int analogue)
  205. {
  206. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  207. int timeout;
  208. if (analogue) {
  209. timeout = 248;
  210. wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
  211. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  212. WM2000_MODE_ANA_SEQ_INCLUDE |
  213. WM2000_MODE_POWER_DOWN);
  214. } else {
  215. timeout = 10;
  216. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  217. WM2000_MODE_POWER_DOWN);
  218. }
  219. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  220. WM2000_STATUS_POWER_DOWN_COMPLETE, timeout)) {
  221. dev_err(&i2c->dev, "Timeout waiting for ANC power down\n");
  222. return -ETIMEDOUT;
  223. }
  224. if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
  225. WM2000_ANC_ENG_IDLE, 1)) {
  226. dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
  227. return -ETIMEDOUT;
  228. }
  229. dev_dbg(&i2c->dev, "powered off\n");
  230. wm2000->anc_mode = ANC_OFF;
  231. return 0;
  232. }
  233. static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue)
  234. {
  235. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  236. BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
  237. if (analogue) {
  238. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  239. WM2000_MODE_ANA_SEQ_INCLUDE |
  240. WM2000_MODE_THERMAL_ENABLE |
  241. WM2000_MODE_BYPASS_ENTRY);
  242. } else {
  243. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  244. WM2000_MODE_THERMAL_ENABLE |
  245. WM2000_MODE_BYPASS_ENTRY);
  246. }
  247. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  248. WM2000_STATUS_ANC_DISABLED, 10)) {
  249. dev_err(&i2c->dev, "Timeout waiting for ANC disable\n");
  250. return -ETIMEDOUT;
  251. }
  252. if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT,
  253. WM2000_ANC_ENG_IDLE, 1)) {
  254. dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n");
  255. return -ETIMEDOUT;
  256. }
  257. wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
  258. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
  259. wm2000->anc_mode = ANC_BYPASS;
  260. dev_dbg(&i2c->dev, "bypass enabled\n");
  261. return 0;
  262. }
  263. static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue)
  264. {
  265. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  266. BUG_ON(wm2000->anc_mode != ANC_BYPASS);
  267. wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
  268. if (analogue) {
  269. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  270. WM2000_MODE_ANA_SEQ_INCLUDE |
  271. WM2000_MODE_MOUSE_ENABLE |
  272. WM2000_MODE_THERMAL_ENABLE);
  273. } else {
  274. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  275. WM2000_MODE_MOUSE_ENABLE |
  276. WM2000_MODE_THERMAL_ENABLE);
  277. }
  278. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
  279. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
  280. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  281. WM2000_STATUS_MOUSE_ACTIVE, 10)) {
  282. dev_err(&i2c->dev, "Timed out waiting for MOUSE\n");
  283. return -ETIMEDOUT;
  284. }
  285. wm2000->anc_mode = ANC_ACTIVE;
  286. dev_dbg(&i2c->dev, "MOUSE active\n");
  287. return 0;
  288. }
  289. static int wm2000_enter_standby(struct i2c_client *i2c, int analogue)
  290. {
  291. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  292. int timeout;
  293. BUG_ON(wm2000->anc_mode != ANC_ACTIVE);
  294. if (analogue) {
  295. timeout = 248;
  296. wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, timeout / 4);
  297. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  298. WM2000_MODE_ANA_SEQ_INCLUDE |
  299. WM2000_MODE_THERMAL_ENABLE |
  300. WM2000_MODE_STANDBY_ENTRY);
  301. } else {
  302. timeout = 10;
  303. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  304. WM2000_MODE_THERMAL_ENABLE |
  305. WM2000_MODE_STANDBY_ENTRY);
  306. }
  307. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  308. WM2000_STATUS_ANC_DISABLED, timeout)) {
  309. dev_err(&i2c->dev,
  310. "Timed out waiting for ANC disable after 1ms\n");
  311. return -ETIMEDOUT;
  312. }
  313. if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE,
  314. 1)) {
  315. dev_err(&i2c->dev,
  316. "Timed out waiting for standby after %dms\n",
  317. timeout * 10);
  318. return -ETIMEDOUT;
  319. }
  320. wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY);
  321. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR);
  322. wm2000->anc_mode = ANC_STANDBY;
  323. dev_dbg(&i2c->dev, "standby\n");
  324. if (analogue)
  325. dev_dbg(&i2c->dev, "Analogue disabled\n");
  326. return 0;
  327. }
  328. static int wm2000_exit_standby(struct i2c_client *i2c, int analogue)
  329. {
  330. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  331. int timeout;
  332. BUG_ON(wm2000->anc_mode != ANC_STANDBY);
  333. wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0);
  334. if (analogue) {
  335. timeout = 248;
  336. wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, timeout / 4);
  337. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  338. WM2000_MODE_ANA_SEQ_INCLUDE |
  339. WM2000_MODE_THERMAL_ENABLE |
  340. WM2000_MODE_MOUSE_ENABLE);
  341. } else {
  342. timeout = 10;
  343. wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL,
  344. WM2000_MODE_THERMAL_ENABLE |
  345. WM2000_MODE_MOUSE_ENABLE);
  346. }
  347. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET);
  348. wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR);
  349. if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS,
  350. WM2000_STATUS_MOUSE_ACTIVE, timeout)) {
  351. dev_err(&i2c->dev, "Timed out waiting for MOUSE after %dms\n",
  352. timeout * 10);
  353. return -ETIMEDOUT;
  354. }
  355. wm2000->anc_mode = ANC_ACTIVE;
  356. dev_dbg(&i2c->dev, "MOUSE active\n");
  357. if (analogue)
  358. dev_dbg(&i2c->dev, "Analogue enabled\n");
  359. return 0;
  360. }
  361. typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue);
  362. static struct {
  363. enum wm2000_anc_mode source;
  364. enum wm2000_anc_mode dest;
  365. int analogue;
  366. wm2000_mode_fn step[2];
  367. } anc_transitions[] = {
  368. {
  369. .source = ANC_OFF,
  370. .dest = ANC_ACTIVE,
  371. .analogue = 1,
  372. .step = {
  373. wm2000_power_up,
  374. },
  375. },
  376. {
  377. .source = ANC_OFF,
  378. .dest = ANC_STANDBY,
  379. .step = {
  380. wm2000_power_up,
  381. wm2000_enter_standby,
  382. },
  383. },
  384. {
  385. .source = ANC_OFF,
  386. .dest = ANC_BYPASS,
  387. .analogue = 1,
  388. .step = {
  389. wm2000_power_up,
  390. wm2000_enter_bypass,
  391. },
  392. },
  393. {
  394. .source = ANC_ACTIVE,
  395. .dest = ANC_BYPASS,
  396. .analogue = 1,
  397. .step = {
  398. wm2000_enter_bypass,
  399. },
  400. },
  401. {
  402. .source = ANC_ACTIVE,
  403. .dest = ANC_STANDBY,
  404. .analogue = 1,
  405. .step = {
  406. wm2000_enter_standby,
  407. },
  408. },
  409. {
  410. .source = ANC_ACTIVE,
  411. .dest = ANC_OFF,
  412. .analogue = 1,
  413. .step = {
  414. wm2000_power_down,
  415. },
  416. },
  417. {
  418. .source = ANC_BYPASS,
  419. .dest = ANC_ACTIVE,
  420. .analogue = 1,
  421. .step = {
  422. wm2000_exit_bypass,
  423. },
  424. },
  425. {
  426. .source = ANC_BYPASS,
  427. .dest = ANC_STANDBY,
  428. .analogue = 1,
  429. .step = {
  430. wm2000_exit_bypass,
  431. wm2000_enter_standby,
  432. },
  433. },
  434. {
  435. .source = ANC_BYPASS,
  436. .dest = ANC_OFF,
  437. .step = {
  438. wm2000_exit_bypass,
  439. wm2000_power_down,
  440. },
  441. },
  442. {
  443. .source = ANC_STANDBY,
  444. .dest = ANC_ACTIVE,
  445. .analogue = 1,
  446. .step = {
  447. wm2000_exit_standby,
  448. },
  449. },
  450. {
  451. .source = ANC_STANDBY,
  452. .dest = ANC_BYPASS,
  453. .analogue = 1,
  454. .step = {
  455. wm2000_exit_standby,
  456. wm2000_enter_bypass,
  457. },
  458. },
  459. {
  460. .source = ANC_STANDBY,
  461. .dest = ANC_OFF,
  462. .step = {
  463. wm2000_exit_standby,
  464. wm2000_power_down,
  465. },
  466. },
  467. };
  468. static int wm2000_anc_transition(struct wm2000_priv *wm2000,
  469. enum wm2000_anc_mode mode)
  470. {
  471. struct i2c_client *i2c = wm2000->i2c;
  472. int i, j;
  473. int ret;
  474. if (wm2000->anc_mode == mode)
  475. return 0;
  476. for (i = 0; i < ARRAY_SIZE(anc_transitions); i++)
  477. if (anc_transitions[i].source == wm2000->anc_mode &&
  478. anc_transitions[i].dest == mode)
  479. break;
  480. if (i == ARRAY_SIZE(anc_transitions)) {
  481. dev_err(&i2c->dev, "No transition for %d->%d\n",
  482. wm2000->anc_mode, mode);
  483. return -EINVAL;
  484. }
  485. for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) {
  486. if (!anc_transitions[i].step[j])
  487. break;
  488. ret = anc_transitions[i].step[j](i2c,
  489. anc_transitions[i].analogue);
  490. if (ret != 0)
  491. return ret;
  492. }
  493. return 0;
  494. }
  495. static int wm2000_anc_set_mode(struct wm2000_priv *wm2000)
  496. {
  497. struct i2c_client *i2c = wm2000->i2c;
  498. enum wm2000_anc_mode mode;
  499. if (wm2000->anc_eng_ena && wm2000->spk_ena)
  500. if (wm2000->anc_active)
  501. mode = ANC_ACTIVE;
  502. else
  503. mode = ANC_BYPASS;
  504. else
  505. mode = ANC_STANDBY;
  506. dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n",
  507. mode, wm2000->anc_eng_ena, !wm2000->spk_ena,
  508. wm2000->anc_active);
  509. return wm2000_anc_transition(wm2000, mode);
  510. }
  511. static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol,
  512. struct snd_ctl_elem_value *ucontrol)
  513. {
  514. struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
  515. ucontrol->value.enumerated.item[0] = wm2000->anc_active;
  516. return 0;
  517. }
  518. static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol,
  519. struct snd_ctl_elem_value *ucontrol)
  520. {
  521. struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
  522. int anc_active = ucontrol->value.enumerated.item[0];
  523. if (anc_active > 1)
  524. return -EINVAL;
  525. wm2000->anc_active = anc_active;
  526. return wm2000_anc_set_mode(wm2000);
  527. }
  528. static int wm2000_speaker_get(struct snd_kcontrol *kcontrol,
  529. struct snd_ctl_elem_value *ucontrol)
  530. {
  531. struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
  532. ucontrol->value.enumerated.item[0] = wm2000->spk_ena;
  533. return 0;
  534. }
  535. static int wm2000_speaker_put(struct snd_kcontrol *kcontrol,
  536. struct snd_ctl_elem_value *ucontrol)
  537. {
  538. struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
  539. int val = ucontrol->value.enumerated.item[0];
  540. if (val > 1)
  541. return -EINVAL;
  542. wm2000->spk_ena = val;
  543. return wm2000_anc_set_mode(wm2000);
  544. }
  545. static const struct snd_kcontrol_new wm2000_controls[] = {
  546. SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0,
  547. wm2000_anc_mode_get,
  548. wm2000_anc_mode_put),
  549. SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0,
  550. wm2000_speaker_get,
  551. wm2000_speaker_put),
  552. };
  553. static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w,
  554. struct snd_kcontrol *kcontrol, int event)
  555. {
  556. struct wm2000_priv *wm2000 = dev_get_drvdata(&wm2000_i2c->dev);
  557. if (SND_SOC_DAPM_EVENT_ON(event))
  558. wm2000->anc_eng_ena = 1;
  559. if (SND_SOC_DAPM_EVENT_OFF(event))
  560. wm2000->anc_eng_ena = 0;
  561. return wm2000_anc_set_mode(wm2000);
  562. }
  563. static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = {
  564. /* Externally visible pins */
  565. SND_SOC_DAPM_OUTPUT("WM2000 SPKN"),
  566. SND_SOC_DAPM_OUTPUT("WM2000 SPKP"),
  567. SND_SOC_DAPM_INPUT("WM2000 LINN"),
  568. SND_SOC_DAPM_INPUT("WM2000 LINP"),
  569. SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0,
  570. wm2000_anc_power_event,
  571. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  572. };
  573. /* Target, Path, Source */
  574. static const struct snd_soc_dapm_route audio_map[] = {
  575. { "WM2000 SPKN", NULL, "ANC Engine" },
  576. { "WM2000 SPKP", NULL, "ANC Engine" },
  577. { "ANC Engine", NULL, "WM2000 LINN" },
  578. { "ANC Engine", NULL, "WM2000 LINP" },
  579. };
  580. /* Called from the machine driver */
  581. int wm2000_add_controls(struct snd_soc_codec *codec)
  582. {
  583. struct snd_soc_dapm_context *dapm = &codec->dapm;
  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(dapm, wm2000_dapm_widgets,
  590. ARRAY_SIZE(wm2000_dapm_widgets));
  591. if (ret < 0)
  592. return ret;
  593. ret = snd_soc_dapm_add_routes(dapm, 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 device *dev)
  687. {
  688. struct i2c_client *i2c = to_i2c_client(dev);
  689. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  690. return wm2000_anc_transition(wm2000, ANC_OFF);
  691. }
  692. static int wm2000_i2c_resume(struct device *dev)
  693. {
  694. struct i2c_client *i2c = to_i2c_client(dev);
  695. struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev);
  696. return wm2000_anc_set_mode(wm2000);
  697. }
  698. #endif
  699. static SIMPLE_DEV_PM_OPS(wm2000_pm, wm2000_i2c_suspend, wm2000_i2c_resume);
  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. .pm = &wm2000_pm,
  710. },
  711. .probe = wm2000_i2c_probe,
  712. .remove = __devexit_p(wm2000_i2c_remove),
  713. .shutdown = wm2000_i2c_shutdown,
  714. .id_table = wm2000_i2c_id,
  715. };
  716. static int __init wm2000_init(void)
  717. {
  718. return i2c_add_driver(&wm2000_i2c_driver);
  719. }
  720. module_init(wm2000_init);
  721. static void __exit wm2000_exit(void)
  722. {
  723. i2c_del_driver(&wm2000_i2c_driver);
  724. }
  725. module_exit(wm2000_exit);
  726. MODULE_DESCRIPTION("ASoC WM2000 driver");
  727. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
  728. MODULE_LICENSE("GPL");