wm2000.c 22 KB

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