wm2000.c 23 KB

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