wm8994-core.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * wm8994-core.c -- Device access for Wolfson WM8994
  3. *
  4. * Copyright 2009 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 it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/i2c.h>
  18. #include <linux/err.h>
  19. #include <linux/delay.h>
  20. #include <linux/mfd/core.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/regmap.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/regulator/machine.h>
  28. #include <linux/mfd/wm8994/core.h>
  29. #include <linux/mfd/wm8994/pdata.h>
  30. #include <linux/mfd/wm8994/registers.h>
  31. #include "wm8994.h"
  32. /**
  33. * wm8994_reg_read: Read a single WM8994 register.
  34. *
  35. * @wm8994: Device to read from.
  36. * @reg: Register to read.
  37. */
  38. int wm8994_reg_read(struct wm8994 *wm8994, unsigned short reg)
  39. {
  40. unsigned int val;
  41. int ret;
  42. ret = regmap_read(wm8994->regmap, reg, &val);
  43. if (ret < 0)
  44. return ret;
  45. else
  46. return val;
  47. }
  48. EXPORT_SYMBOL_GPL(wm8994_reg_read);
  49. /**
  50. * wm8994_bulk_read: Read multiple WM8994 registers
  51. *
  52. * @wm8994: Device to read from
  53. * @reg: First register
  54. * @count: Number of registers
  55. * @buf: Buffer to fill. The data will be returned big endian.
  56. */
  57. int wm8994_bulk_read(struct wm8994 *wm8994, unsigned short reg,
  58. int count, u16 *buf)
  59. {
  60. return regmap_bulk_read(wm8994->regmap, reg, buf, count);
  61. }
  62. /**
  63. * wm8994_reg_write: Write a single WM8994 register.
  64. *
  65. * @wm8994: Device to write to.
  66. * @reg: Register to write to.
  67. * @val: Value to write.
  68. */
  69. int wm8994_reg_write(struct wm8994 *wm8994, unsigned short reg,
  70. unsigned short val)
  71. {
  72. return regmap_write(wm8994->regmap, reg, val);
  73. }
  74. EXPORT_SYMBOL_GPL(wm8994_reg_write);
  75. /**
  76. * wm8994_bulk_write: Write multiple WM8994 registers
  77. *
  78. * @wm8994: Device to write to
  79. * @reg: First register
  80. * @count: Number of registers
  81. * @buf: Buffer to write from. Data must be big-endian formatted.
  82. */
  83. int wm8994_bulk_write(struct wm8994 *wm8994, unsigned short reg,
  84. int count, const u16 *buf)
  85. {
  86. return regmap_raw_write(wm8994->regmap, reg, buf, count * sizeof(u16));
  87. }
  88. EXPORT_SYMBOL_GPL(wm8994_bulk_write);
  89. /**
  90. * wm8994_set_bits: Set the value of a bitfield in a WM8994 register
  91. *
  92. * @wm8994: Device to write to.
  93. * @reg: Register to write to.
  94. * @mask: Mask of bits to set.
  95. * @val: Value to set (unshifted)
  96. */
  97. int wm8994_set_bits(struct wm8994 *wm8994, unsigned short reg,
  98. unsigned short mask, unsigned short val)
  99. {
  100. return regmap_update_bits(wm8994->regmap, reg, mask, val);
  101. }
  102. EXPORT_SYMBOL_GPL(wm8994_set_bits);
  103. static struct mfd_cell wm8994_regulator_devs[] = {
  104. {
  105. .name = "wm8994-ldo",
  106. .id = 1,
  107. .pm_runtime_no_callbacks = true,
  108. },
  109. {
  110. .name = "wm8994-ldo",
  111. .id = 2,
  112. .pm_runtime_no_callbacks = true,
  113. },
  114. };
  115. static struct resource wm8994_codec_resources[] = {
  116. {
  117. .start = WM8994_IRQ_TEMP_SHUT,
  118. .end = WM8994_IRQ_TEMP_WARN,
  119. .flags = IORESOURCE_IRQ,
  120. },
  121. };
  122. static struct resource wm8994_gpio_resources[] = {
  123. {
  124. .start = WM8994_IRQ_GPIO(1),
  125. .end = WM8994_IRQ_GPIO(11),
  126. .flags = IORESOURCE_IRQ,
  127. },
  128. };
  129. static struct mfd_cell wm8994_devs[] = {
  130. {
  131. .name = "wm8994-codec",
  132. .num_resources = ARRAY_SIZE(wm8994_codec_resources),
  133. .resources = wm8994_codec_resources,
  134. },
  135. {
  136. .name = "wm8994-gpio",
  137. .num_resources = ARRAY_SIZE(wm8994_gpio_resources),
  138. .resources = wm8994_gpio_resources,
  139. .pm_runtime_no_callbacks = true,
  140. },
  141. };
  142. /*
  143. * Supplies for the main bulk of CODEC; the LDO supplies are ignored
  144. * and should be handled via the standard regulator API supply
  145. * management.
  146. */
  147. static const char *wm1811_main_supplies[] = {
  148. "DBVDD1",
  149. "DBVDD2",
  150. "DBVDD3",
  151. "DCVDD",
  152. "AVDD1",
  153. "AVDD2",
  154. "CPVDD",
  155. "SPKVDD1",
  156. "SPKVDD2",
  157. };
  158. static const char *wm8994_main_supplies[] = {
  159. "DBVDD",
  160. "DCVDD",
  161. "AVDD1",
  162. "AVDD2",
  163. "CPVDD",
  164. "SPKVDD1",
  165. "SPKVDD2",
  166. };
  167. static const char *wm8958_main_supplies[] = {
  168. "DBVDD1",
  169. "DBVDD2",
  170. "DBVDD3",
  171. "DCVDD",
  172. "AVDD1",
  173. "AVDD2",
  174. "CPVDD",
  175. "SPKVDD1",
  176. "SPKVDD2",
  177. };
  178. #ifdef CONFIG_PM_RUNTIME
  179. static int wm8994_suspend(struct device *dev)
  180. {
  181. struct wm8994 *wm8994 = dev_get_drvdata(dev);
  182. int ret;
  183. /* Don't actually go through with the suspend if the CODEC is
  184. * still active for accessory detect. */
  185. switch (wm8994->type) {
  186. case WM8958:
  187. case WM1811:
  188. ret = wm8994_reg_read(wm8994, WM8958_MIC_DETECT_1);
  189. if (ret < 0) {
  190. dev_err(dev, "Failed to read power status: %d\n", ret);
  191. } else if (ret & WM8958_MICD_ENA) {
  192. dev_dbg(dev, "CODEC still active, ignoring suspend\n");
  193. return 0;
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. /* Disable LDO pulldowns while the device is suspended if we
  200. * don't know that something will be driving them. */
  201. if (!wm8994->ldo_ena_always_driven)
  202. wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
  203. WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD,
  204. WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD);
  205. /* Explicitly put the device into reset in case regulators
  206. * don't get disabled in order to ensure consistent restart.
  207. */
  208. wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET,
  209. wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET));
  210. regcache_mark_dirty(wm8994->regmap);
  211. /* Restore GPIO registers to prevent problems with mismatched
  212. * pin configurations.
  213. */
  214. ret = regcache_sync_region(wm8994->regmap, WM8994_GPIO_1,
  215. WM8994_GPIO_11);
  216. if (ret != 0)
  217. dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
  218. /* In case one of the GPIOs is used as a wake input. */
  219. ret = regcache_sync_region(wm8994->regmap,
  220. WM8994_INTERRUPT_STATUS_1_MASK,
  221. WM8994_INTERRUPT_STATUS_1_MASK);
  222. if (ret != 0)
  223. dev_err(dev, "Failed to restore interrupt mask: %d\n", ret);
  224. regcache_cache_only(wm8994->regmap, true);
  225. wm8994->suspended = true;
  226. ret = regulator_bulk_disable(wm8994->num_supplies,
  227. wm8994->supplies);
  228. if (ret != 0) {
  229. dev_err(dev, "Failed to disable supplies: %d\n", ret);
  230. return ret;
  231. }
  232. return 0;
  233. }
  234. static int wm8994_resume(struct device *dev)
  235. {
  236. struct wm8994 *wm8994 = dev_get_drvdata(dev);
  237. int ret;
  238. /* We may have lied to the PM core about suspending */
  239. if (!wm8994->suspended)
  240. return 0;
  241. ret = regulator_bulk_enable(wm8994->num_supplies,
  242. wm8994->supplies);
  243. if (ret != 0) {
  244. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  245. return ret;
  246. }
  247. regcache_cache_only(wm8994->regmap, false);
  248. ret = regcache_sync(wm8994->regmap);
  249. if (ret != 0) {
  250. dev_err(dev, "Failed to restore register map: %d\n", ret);
  251. goto err_enable;
  252. }
  253. /* Disable LDO pulldowns while the device is active */
  254. wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
  255. WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD,
  256. 0);
  257. wm8994->suspended = false;
  258. return 0;
  259. err_enable:
  260. regulator_bulk_disable(wm8994->num_supplies, wm8994->supplies);
  261. return ret;
  262. }
  263. #endif
  264. #ifdef CONFIG_REGULATOR
  265. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  266. {
  267. struct wm8994_ldo_pdata *ldo_pdata;
  268. if (!pdata)
  269. return 0;
  270. ldo_pdata = &pdata->ldo[ldo];
  271. if (!ldo_pdata->init_data)
  272. return 0;
  273. return ldo_pdata->init_data->num_consumer_supplies != 0;
  274. }
  275. #else
  276. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  277. {
  278. return 0;
  279. }
  280. #endif
  281. static const struct reg_default wm8994_revc_patch[] = {
  282. { 0x102, 0x3 },
  283. { 0x56, 0x3 },
  284. { 0x817, 0x0 },
  285. { 0x102, 0x0 },
  286. };
  287. static const struct reg_default wm8958_reva_patch[] = {
  288. { 0x102, 0x3 },
  289. { 0xcb, 0x81 },
  290. { 0x817, 0x0 },
  291. { 0x102, 0x0 },
  292. };
  293. static const struct reg_default wm1811_reva_patch[] = {
  294. { 0x102, 0x3 },
  295. { 0x56, 0xc07 },
  296. { 0x5d, 0x7e },
  297. { 0x5e, 0x0 },
  298. { 0x102, 0x0 },
  299. };
  300. #ifdef CONFIG_OF
  301. static int wm8994_set_pdata_from_of(struct wm8994 *wm8994)
  302. {
  303. struct device_node *np = wm8994->dev->of_node;
  304. struct wm8994_pdata *pdata = &wm8994->pdata;
  305. int i;
  306. if (!np)
  307. return 0;
  308. if (of_property_read_u32_array(np, "wlf,gpio-cfg", pdata->gpio_defaults,
  309. ARRAY_SIZE(pdata->gpio_defaults)) >= 0) {
  310. for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
  311. if (wm8994->pdata.gpio_defaults[i] == 0)
  312. pdata->gpio_defaults[i]
  313. = WM8994_CONFIGURE_GPIO;
  314. }
  315. }
  316. of_property_read_u32_array(np, "wlf,micbias-cfg", pdata->micbias,
  317. ARRAY_SIZE(pdata->micbias));
  318. pdata->lineout1_diff = true;
  319. pdata->lineout2_diff = true;
  320. if (of_find_property(np, "wlf,lineout1-se", NULL))
  321. pdata->lineout1_diff = false;
  322. if (of_find_property(np, "wlf,lineout2-se", NULL))
  323. pdata->lineout2_diff = false;
  324. if (of_find_property(np, "wlf,lineout1-feedback", NULL))
  325. pdata->lineout1fb = true;
  326. if (of_find_property(np, "wlf,lineout2-feedback", NULL))
  327. pdata->lineout2fb = true;
  328. if (of_find_property(np, "wlf,ldoena-always-driven", NULL))
  329. pdata->lineout2fb = true;
  330. pdata->ldo[0].enable = of_get_named_gpio(np, "wlf,ldo1ena", 0);
  331. if (pdata->ldo[0].enable < 0)
  332. pdata->ldo[0].enable = 0;
  333. pdata->ldo[1].enable = of_get_named_gpio(np, "wlf,ldo2ena", 0);
  334. if (pdata->ldo[1].enable < 0)
  335. pdata->ldo[1].enable = 0;
  336. return 0;
  337. }
  338. #else
  339. static int wm8994_set_pdata_from_of(struct wm8994 *wm8994)
  340. {
  341. return 0;
  342. }
  343. #endif
  344. /*
  345. * Instantiate the generic non-control parts of the device.
  346. */
  347. static int wm8994_device_init(struct wm8994 *wm8994, int irq)
  348. {
  349. struct wm8994_pdata *pdata;
  350. struct regmap_config *regmap_config;
  351. const struct reg_default *regmap_patch = NULL;
  352. const char *devname;
  353. int ret, i, patch_regs = 0;
  354. int pulls = 0;
  355. if (dev_get_platdata(wm8994->dev)) {
  356. pdata = dev_get_platdata(wm8994->dev);
  357. wm8994->pdata = *pdata;
  358. }
  359. pdata = &wm8994->pdata;
  360. ret = wm8994_set_pdata_from_of(wm8994);
  361. if (ret != 0)
  362. return ret;
  363. dev_set_drvdata(wm8994->dev, wm8994);
  364. /* Add the on-chip regulators first for bootstrapping */
  365. ret = mfd_add_devices(wm8994->dev, -1,
  366. wm8994_regulator_devs,
  367. ARRAY_SIZE(wm8994_regulator_devs),
  368. NULL, 0, NULL);
  369. if (ret != 0) {
  370. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  371. goto err;
  372. }
  373. switch (wm8994->type) {
  374. case WM1811:
  375. wm8994->num_supplies = ARRAY_SIZE(wm1811_main_supplies);
  376. break;
  377. case WM8994:
  378. wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies);
  379. break;
  380. case WM8958:
  381. wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies);
  382. break;
  383. default:
  384. BUG();
  385. goto err;
  386. }
  387. wm8994->supplies = devm_kzalloc(wm8994->dev,
  388. sizeof(struct regulator_bulk_data) *
  389. wm8994->num_supplies, GFP_KERNEL);
  390. if (!wm8994->supplies) {
  391. ret = -ENOMEM;
  392. goto err;
  393. }
  394. switch (wm8994->type) {
  395. case WM1811:
  396. for (i = 0; i < ARRAY_SIZE(wm1811_main_supplies); i++)
  397. wm8994->supplies[i].supply = wm1811_main_supplies[i];
  398. break;
  399. case WM8994:
  400. for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++)
  401. wm8994->supplies[i].supply = wm8994_main_supplies[i];
  402. break;
  403. case WM8958:
  404. for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++)
  405. wm8994->supplies[i].supply = wm8958_main_supplies[i];
  406. break;
  407. default:
  408. BUG();
  409. goto err;
  410. }
  411. ret = devm_regulator_bulk_get(wm8994->dev, wm8994->num_supplies,
  412. wm8994->supplies);
  413. if (ret != 0) {
  414. dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
  415. goto err;
  416. }
  417. ret = regulator_bulk_enable(wm8994->num_supplies,
  418. wm8994->supplies);
  419. if (ret != 0) {
  420. dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
  421. goto err;
  422. }
  423. ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET);
  424. if (ret < 0) {
  425. dev_err(wm8994->dev, "Failed to read ID register\n");
  426. goto err_enable;
  427. }
  428. switch (ret) {
  429. case 0x1811:
  430. devname = "WM1811";
  431. if (wm8994->type != WM1811)
  432. dev_warn(wm8994->dev, "Device registered as type %d\n",
  433. wm8994->type);
  434. wm8994->type = WM1811;
  435. break;
  436. case 0x8994:
  437. devname = "WM8994";
  438. if (wm8994->type != WM8994)
  439. dev_warn(wm8994->dev, "Device registered as type %d\n",
  440. wm8994->type);
  441. wm8994->type = WM8994;
  442. break;
  443. case 0x8958:
  444. devname = "WM8958";
  445. if (wm8994->type != WM8958)
  446. dev_warn(wm8994->dev, "Device registered as type %d\n",
  447. wm8994->type);
  448. wm8994->type = WM8958;
  449. break;
  450. default:
  451. dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n",
  452. ret);
  453. ret = -EINVAL;
  454. goto err_enable;
  455. }
  456. ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION);
  457. if (ret < 0) {
  458. dev_err(wm8994->dev, "Failed to read revision register: %d\n",
  459. ret);
  460. goto err_enable;
  461. }
  462. wm8994->revision = ret & WM8994_CHIP_REV_MASK;
  463. wm8994->cust_id = (ret & WM8994_CUST_ID_MASK) >> WM8994_CUST_ID_SHIFT;
  464. switch (wm8994->type) {
  465. case WM8994:
  466. switch (wm8994->revision) {
  467. case 0:
  468. case 1:
  469. dev_warn(wm8994->dev,
  470. "revision %c not fully supported\n",
  471. 'A' + wm8994->revision);
  472. break;
  473. case 2:
  474. case 3:
  475. default:
  476. regmap_patch = wm8994_revc_patch;
  477. patch_regs = ARRAY_SIZE(wm8994_revc_patch);
  478. break;
  479. }
  480. break;
  481. case WM8958:
  482. switch (wm8994->revision) {
  483. case 0:
  484. regmap_patch = wm8958_reva_patch;
  485. patch_regs = ARRAY_SIZE(wm8958_reva_patch);
  486. break;
  487. default:
  488. break;
  489. }
  490. break;
  491. case WM1811:
  492. /* Revision C did not change the relevant layer */
  493. if (wm8994->revision > 1)
  494. wm8994->revision++;
  495. regmap_patch = wm1811_reva_patch;
  496. patch_regs = ARRAY_SIZE(wm1811_reva_patch);
  497. break;
  498. default:
  499. break;
  500. }
  501. dev_info(wm8994->dev, "%s revision %c CUST_ID %02x\n", devname,
  502. 'A' + wm8994->revision, wm8994->cust_id);
  503. switch (wm8994->type) {
  504. case WM1811:
  505. regmap_config = &wm1811_regmap_config;
  506. break;
  507. case WM8994:
  508. regmap_config = &wm8994_regmap_config;
  509. break;
  510. case WM8958:
  511. regmap_config = &wm8958_regmap_config;
  512. break;
  513. default:
  514. dev_err(wm8994->dev, "Unknown device type %d\n", wm8994->type);
  515. return -EINVAL;
  516. }
  517. ret = regmap_reinit_cache(wm8994->regmap, regmap_config);
  518. if (ret != 0) {
  519. dev_err(wm8994->dev, "Failed to reinit register cache: %d\n",
  520. ret);
  521. return ret;
  522. }
  523. /* Explicitly put the device into reset in case regulators
  524. * don't get disabled in order to ensure we know the device
  525. * state.
  526. */
  527. ret = wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET,
  528. wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET));
  529. if (ret != 0) {
  530. dev_err(wm8994->dev, "Failed to reset device: %d\n", ret);
  531. return ret;
  532. }
  533. if (regmap_patch) {
  534. ret = regmap_register_patch(wm8994->regmap, regmap_patch,
  535. patch_regs);
  536. if (ret != 0) {
  537. dev_err(wm8994->dev, "Failed to register patch: %d\n",
  538. ret);
  539. goto err;
  540. }
  541. }
  542. wm8994->irq_base = pdata->irq_base;
  543. wm8994->gpio_base = pdata->gpio_base;
  544. /* GPIO configuration is only applied if it's non-zero */
  545. for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
  546. if (pdata->gpio_defaults[i]) {
  547. wm8994_set_bits(wm8994, WM8994_GPIO_1 + i,
  548. 0xffff, pdata->gpio_defaults[i]);
  549. }
  550. }
  551. wm8994->ldo_ena_always_driven = pdata->ldo_ena_always_driven;
  552. if (pdata->spkmode_pu)
  553. pulls |= WM8994_SPKMODE_PU;
  554. /* Disable unneeded pulls */
  555. wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
  556. WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD |
  557. WM8994_SPKMODE_PU | WM8994_CSNADDR_PD,
  558. pulls);
  559. /* In some system designs where the regulators are not in use,
  560. * we can achieve a small reduction in leakage currents by
  561. * floating LDO outputs. This bit makes no difference if the
  562. * LDOs are enabled, it only affects cases where the LDOs were
  563. * in operation and are then disabled.
  564. */
  565. for (i = 0; i < WM8994_NUM_LDO_REGS; i++) {
  566. if (wm8994_ldo_in_use(pdata, i))
  567. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  568. WM8994_LDO1_DISCH, WM8994_LDO1_DISCH);
  569. else
  570. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  571. WM8994_LDO1_DISCH, 0);
  572. }
  573. wm8994_irq_init(wm8994);
  574. ret = mfd_add_devices(wm8994->dev, -1,
  575. wm8994_devs, ARRAY_SIZE(wm8994_devs),
  576. NULL, 0, NULL);
  577. if (ret != 0) {
  578. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  579. goto err_irq;
  580. }
  581. pm_runtime_enable(wm8994->dev);
  582. pm_runtime_idle(wm8994->dev);
  583. return 0;
  584. err_irq:
  585. wm8994_irq_exit(wm8994);
  586. err_enable:
  587. regulator_bulk_disable(wm8994->num_supplies,
  588. wm8994->supplies);
  589. err:
  590. mfd_remove_devices(wm8994->dev);
  591. return ret;
  592. }
  593. static void wm8994_device_exit(struct wm8994 *wm8994)
  594. {
  595. pm_runtime_disable(wm8994->dev);
  596. mfd_remove_devices(wm8994->dev);
  597. wm8994_irq_exit(wm8994);
  598. regulator_bulk_disable(wm8994->num_supplies,
  599. wm8994->supplies);
  600. }
  601. static const struct of_device_id wm8994_of_match[] = {
  602. { .compatible = "wlf,wm1811", .data = (void *)WM1811 },
  603. { .compatible = "wlf,wm8994", .data = (void *)WM8994 },
  604. { .compatible = "wlf,wm8958", .data = (void *)WM8958 },
  605. { }
  606. };
  607. MODULE_DEVICE_TABLE(of, wm8994_of_match);
  608. static int wm8994_i2c_probe(struct i2c_client *i2c,
  609. const struct i2c_device_id *id)
  610. {
  611. const struct of_device_id *of_id;
  612. struct wm8994 *wm8994;
  613. int ret;
  614. wm8994 = devm_kzalloc(&i2c->dev, sizeof(struct wm8994), GFP_KERNEL);
  615. if (wm8994 == NULL)
  616. return -ENOMEM;
  617. i2c_set_clientdata(i2c, wm8994);
  618. wm8994->dev = &i2c->dev;
  619. wm8994->irq = i2c->irq;
  620. if (i2c->dev.of_node) {
  621. of_id = of_match_device(wm8994_of_match, &i2c->dev);
  622. if (of_id)
  623. wm8994->type = (int)of_id->data;
  624. } else {
  625. wm8994->type = id->driver_data;
  626. }
  627. wm8994->regmap = devm_regmap_init_i2c(i2c, &wm8994_base_regmap_config);
  628. if (IS_ERR(wm8994->regmap)) {
  629. ret = PTR_ERR(wm8994->regmap);
  630. dev_err(wm8994->dev, "Failed to allocate register map: %d\n",
  631. ret);
  632. return ret;
  633. }
  634. return wm8994_device_init(wm8994, i2c->irq);
  635. }
  636. static int wm8994_i2c_remove(struct i2c_client *i2c)
  637. {
  638. struct wm8994 *wm8994 = i2c_get_clientdata(i2c);
  639. wm8994_device_exit(wm8994);
  640. return 0;
  641. }
  642. static const struct i2c_device_id wm8994_i2c_id[] = {
  643. { "wm1811", WM1811 },
  644. { "wm1811a", WM1811 },
  645. { "wm8994", WM8994 },
  646. { "wm8958", WM8958 },
  647. { }
  648. };
  649. MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);
  650. static const struct dev_pm_ops wm8994_pm_ops = {
  651. SET_RUNTIME_PM_OPS(wm8994_suspend, wm8994_resume, NULL)
  652. };
  653. static struct i2c_driver wm8994_i2c_driver = {
  654. .driver = {
  655. .name = "wm8994",
  656. .owner = THIS_MODULE,
  657. .pm = &wm8994_pm_ops,
  658. .of_match_table = of_match_ptr(wm8994_of_match),
  659. },
  660. .probe = wm8994_i2c_probe,
  661. .remove = wm8994_i2c_remove,
  662. .id_table = wm8994_i2c_id,
  663. };
  664. module_i2c_driver(wm8994_i2c_driver);
  665. MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
  666. MODULE_LICENSE("GPL");
  667. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");