wm2000.c 23 KB

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