wm8994-core.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 (eg, for audio passthrough from CP. */
  185. ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_1);
  186. if (ret < 0) {
  187. dev_err(dev, "Failed to read power status: %d\n", ret);
  188. } else if (ret & WM8994_VMID_SEL_MASK) {
  189. dev_dbg(dev, "CODEC still active, ignoring suspend\n");
  190. return 0;
  191. }
  192. ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_4);
  193. if (ret < 0) {
  194. dev_err(dev, "Failed to read power status: %d\n", ret);
  195. } else if (ret & (WM8994_AIF2ADCL_ENA | WM8994_AIF2ADCR_ENA |
  196. WM8994_AIF1ADC2L_ENA | WM8994_AIF1ADC2R_ENA |
  197. WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC1R_ENA)) {
  198. dev_dbg(dev, "CODEC still active, ignoring suspend\n");
  199. return 0;
  200. }
  201. ret = wm8994_reg_read(wm8994, WM8994_POWER_MANAGEMENT_5);
  202. if (ret < 0) {
  203. dev_err(dev, "Failed to read power status: %d\n", ret);
  204. } else if (ret & (WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA |
  205. WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA |
  206. WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA)) {
  207. dev_dbg(dev, "CODEC still active, ignoring suspend\n");
  208. return 0;
  209. }
  210. switch (wm8994->type) {
  211. case WM8958:
  212. case WM1811:
  213. ret = wm8994_reg_read(wm8994, WM8958_MIC_DETECT_1);
  214. if (ret < 0) {
  215. dev_err(dev, "Failed to read power status: %d\n", ret);
  216. } else if (ret & WM8958_MICD_ENA) {
  217. dev_dbg(dev, "CODEC still active, ignoring suspend\n");
  218. return 0;
  219. }
  220. break;
  221. default:
  222. break;
  223. }
  224. /* Disable LDO pulldowns while the device is suspended if we
  225. * don't know that something will be driving them. */
  226. if (!wm8994->ldo_ena_always_driven)
  227. wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
  228. WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD,
  229. WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD);
  230. /* Explicitly put the device into reset in case regulators
  231. * don't get disabled in order to ensure consistent restart.
  232. */
  233. wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET,
  234. wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET));
  235. regcache_mark_dirty(wm8994->regmap);
  236. /* Restore GPIO registers to prevent problems with mismatched
  237. * pin configurations.
  238. */
  239. ret = regcache_sync_region(wm8994->regmap, WM8994_GPIO_1,
  240. WM8994_GPIO_11);
  241. if (ret != 0)
  242. dev_err(dev, "Failed to restore GPIO registers: %d\n", ret);
  243. /* In case one of the GPIOs is used as a wake input. */
  244. ret = regcache_sync_region(wm8994->regmap,
  245. WM8994_INTERRUPT_STATUS_1_MASK,
  246. WM8994_INTERRUPT_STATUS_1_MASK);
  247. if (ret != 0)
  248. dev_err(dev, "Failed to restore interrupt mask: %d\n", ret);
  249. regcache_cache_only(wm8994->regmap, true);
  250. wm8994->suspended = true;
  251. ret = regulator_bulk_disable(wm8994->num_supplies,
  252. wm8994->supplies);
  253. if (ret != 0) {
  254. dev_err(dev, "Failed to disable supplies: %d\n", ret);
  255. return ret;
  256. }
  257. return 0;
  258. }
  259. static int wm8994_resume(struct device *dev)
  260. {
  261. struct wm8994 *wm8994 = dev_get_drvdata(dev);
  262. int ret;
  263. /* We may have lied to the PM core about suspending */
  264. if (!wm8994->suspended)
  265. return 0;
  266. ret = regulator_bulk_enable(wm8994->num_supplies,
  267. wm8994->supplies);
  268. if (ret != 0) {
  269. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  270. return ret;
  271. }
  272. regcache_cache_only(wm8994->regmap, false);
  273. ret = regcache_sync(wm8994->regmap);
  274. if (ret != 0) {
  275. dev_err(dev, "Failed to restore register map: %d\n", ret);
  276. goto err_enable;
  277. }
  278. /* Disable LDO pulldowns while the device is active */
  279. wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
  280. WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD,
  281. 0);
  282. wm8994->suspended = false;
  283. return 0;
  284. err_enable:
  285. regulator_bulk_disable(wm8994->num_supplies, wm8994->supplies);
  286. return ret;
  287. }
  288. #endif
  289. #ifdef CONFIG_REGULATOR
  290. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  291. {
  292. struct wm8994_ldo_pdata *ldo_pdata;
  293. if (!pdata)
  294. return 0;
  295. ldo_pdata = &pdata->ldo[ldo];
  296. if (!ldo_pdata->init_data)
  297. return 0;
  298. return ldo_pdata->init_data->num_consumer_supplies != 0;
  299. }
  300. #else
  301. static int wm8994_ldo_in_use(struct wm8994_pdata *pdata, int ldo)
  302. {
  303. return 0;
  304. }
  305. #endif
  306. static const struct reg_default wm8994_revc_patch[] = {
  307. { 0x102, 0x3 },
  308. { 0x56, 0x3 },
  309. { 0x817, 0x0 },
  310. { 0x102, 0x0 },
  311. };
  312. static const struct reg_default wm8958_reva_patch[] = {
  313. { 0x102, 0x3 },
  314. { 0xcb, 0x81 },
  315. { 0x817, 0x0 },
  316. { 0x102, 0x0 },
  317. };
  318. static const struct reg_default wm1811_reva_patch[] = {
  319. { 0x102, 0x3 },
  320. { 0x56, 0xc07 },
  321. { 0x5d, 0x7e },
  322. { 0x5e, 0x0 },
  323. { 0x102, 0x0 },
  324. };
  325. #ifdef CONFIG_OF
  326. static int wm8994_set_pdata_from_of(struct wm8994 *wm8994)
  327. {
  328. struct device_node *np = wm8994->dev->of_node;
  329. struct wm8994_pdata *pdata = &wm8994->pdata;
  330. int i;
  331. if (!np)
  332. return 0;
  333. if (of_property_read_u32_array(np, "wlf,gpio-cfg", pdata->gpio_defaults,
  334. ARRAY_SIZE(pdata->gpio_defaults)) >= 0) {
  335. for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
  336. if (wm8994->pdata.gpio_defaults[i] == 0)
  337. pdata->gpio_defaults[i]
  338. = WM8994_CONFIGURE_GPIO;
  339. }
  340. }
  341. of_property_read_u32_array(np, "wlf,micbias-cfg", pdata->micbias,
  342. ARRAY_SIZE(pdata->micbias));
  343. pdata->lineout1_diff = true;
  344. pdata->lineout2_diff = true;
  345. if (of_find_property(np, "wlf,lineout1-se", NULL))
  346. pdata->lineout1_diff = false;
  347. if (of_find_property(np, "wlf,lineout2-se", NULL))
  348. pdata->lineout2_diff = false;
  349. if (of_find_property(np, "wlf,lineout1-feedback", NULL))
  350. pdata->lineout1fb = true;
  351. if (of_find_property(np, "wlf,lineout2-feedback", NULL))
  352. pdata->lineout2fb = true;
  353. if (of_find_property(np, "wlf,ldoena-always-driven", NULL))
  354. pdata->lineout2fb = true;
  355. pdata->ldo[0].enable = of_get_named_gpio(np, "wlf,ldo1ena", 0);
  356. if (pdata->ldo[0].enable < 0)
  357. pdata->ldo[0].enable = 0;
  358. pdata->ldo[1].enable = of_get_named_gpio(np, "wlf,ldo2ena", 0);
  359. if (pdata->ldo[1].enable < 0)
  360. pdata->ldo[1].enable = 0;
  361. return 0;
  362. }
  363. #else
  364. static int wm8994_set_pdata_from_of(struct wm8994 *wm8994)
  365. {
  366. return 0;
  367. }
  368. #endif
  369. /*
  370. * Instantiate the generic non-control parts of the device.
  371. */
  372. static int wm8994_device_init(struct wm8994 *wm8994, int irq)
  373. {
  374. struct wm8994_pdata *pdata;
  375. struct regmap_config *regmap_config;
  376. const struct reg_default *regmap_patch = NULL;
  377. const char *devname;
  378. int ret, i, patch_regs = 0;
  379. int pulls = 0;
  380. if (dev_get_platdata(wm8994->dev)) {
  381. pdata = dev_get_platdata(wm8994->dev);
  382. wm8994->pdata = *pdata;
  383. }
  384. pdata = &wm8994->pdata;
  385. ret = wm8994_set_pdata_from_of(wm8994);
  386. if (ret != 0)
  387. return ret;
  388. dev_set_drvdata(wm8994->dev, wm8994);
  389. /* Add the on-chip regulators first for bootstrapping */
  390. ret = mfd_add_devices(wm8994->dev, -1,
  391. wm8994_regulator_devs,
  392. ARRAY_SIZE(wm8994_regulator_devs),
  393. NULL, 0, NULL);
  394. if (ret != 0) {
  395. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  396. goto err;
  397. }
  398. switch (wm8994->type) {
  399. case WM1811:
  400. wm8994->num_supplies = ARRAY_SIZE(wm1811_main_supplies);
  401. break;
  402. case WM8994:
  403. wm8994->num_supplies = ARRAY_SIZE(wm8994_main_supplies);
  404. break;
  405. case WM8958:
  406. wm8994->num_supplies = ARRAY_SIZE(wm8958_main_supplies);
  407. break;
  408. default:
  409. BUG();
  410. goto err;
  411. }
  412. wm8994->supplies = devm_kzalloc(wm8994->dev,
  413. sizeof(struct regulator_bulk_data) *
  414. wm8994->num_supplies, GFP_KERNEL);
  415. if (!wm8994->supplies) {
  416. ret = -ENOMEM;
  417. goto err;
  418. }
  419. switch (wm8994->type) {
  420. case WM1811:
  421. for (i = 0; i < ARRAY_SIZE(wm1811_main_supplies); i++)
  422. wm8994->supplies[i].supply = wm1811_main_supplies[i];
  423. break;
  424. case WM8994:
  425. for (i = 0; i < ARRAY_SIZE(wm8994_main_supplies); i++)
  426. wm8994->supplies[i].supply = wm8994_main_supplies[i];
  427. break;
  428. case WM8958:
  429. for (i = 0; i < ARRAY_SIZE(wm8958_main_supplies); i++)
  430. wm8994->supplies[i].supply = wm8958_main_supplies[i];
  431. break;
  432. default:
  433. BUG();
  434. goto err;
  435. }
  436. ret = devm_regulator_bulk_get(wm8994->dev, wm8994->num_supplies,
  437. wm8994->supplies);
  438. if (ret != 0) {
  439. dev_err(wm8994->dev, "Failed to get supplies: %d\n", ret);
  440. goto err;
  441. }
  442. ret = regulator_bulk_enable(wm8994->num_supplies,
  443. wm8994->supplies);
  444. if (ret != 0) {
  445. dev_err(wm8994->dev, "Failed to enable supplies: %d\n", ret);
  446. goto err;
  447. }
  448. ret = wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET);
  449. if (ret < 0) {
  450. dev_err(wm8994->dev, "Failed to read ID register\n");
  451. goto err_enable;
  452. }
  453. switch (ret) {
  454. case 0x1811:
  455. devname = "WM1811";
  456. if (wm8994->type != WM1811)
  457. dev_warn(wm8994->dev, "Device registered as type %d\n",
  458. wm8994->type);
  459. wm8994->type = WM1811;
  460. break;
  461. case 0x8994:
  462. devname = "WM8994";
  463. if (wm8994->type != WM8994)
  464. dev_warn(wm8994->dev, "Device registered as type %d\n",
  465. wm8994->type);
  466. wm8994->type = WM8994;
  467. break;
  468. case 0x8958:
  469. devname = "WM8958";
  470. if (wm8994->type != WM8958)
  471. dev_warn(wm8994->dev, "Device registered as type %d\n",
  472. wm8994->type);
  473. wm8994->type = WM8958;
  474. break;
  475. default:
  476. dev_err(wm8994->dev, "Device is not a WM8994, ID is %x\n",
  477. ret);
  478. ret = -EINVAL;
  479. goto err_enable;
  480. }
  481. ret = wm8994_reg_read(wm8994, WM8994_CHIP_REVISION);
  482. if (ret < 0) {
  483. dev_err(wm8994->dev, "Failed to read revision register: %d\n",
  484. ret);
  485. goto err_enable;
  486. }
  487. wm8994->revision = ret & WM8994_CHIP_REV_MASK;
  488. wm8994->cust_id = (ret & WM8994_CUST_ID_MASK) >> WM8994_CUST_ID_SHIFT;
  489. switch (wm8994->type) {
  490. case WM8994:
  491. switch (wm8994->revision) {
  492. case 0:
  493. case 1:
  494. dev_warn(wm8994->dev,
  495. "revision %c not fully supported\n",
  496. 'A' + wm8994->revision);
  497. break;
  498. case 2:
  499. case 3:
  500. default:
  501. regmap_patch = wm8994_revc_patch;
  502. patch_regs = ARRAY_SIZE(wm8994_revc_patch);
  503. break;
  504. }
  505. break;
  506. case WM8958:
  507. switch (wm8994->revision) {
  508. case 0:
  509. regmap_patch = wm8958_reva_patch;
  510. patch_regs = ARRAY_SIZE(wm8958_reva_patch);
  511. break;
  512. default:
  513. break;
  514. }
  515. break;
  516. case WM1811:
  517. /* Revision C did not change the relevant layer */
  518. if (wm8994->revision > 1)
  519. wm8994->revision++;
  520. regmap_patch = wm1811_reva_patch;
  521. patch_regs = ARRAY_SIZE(wm1811_reva_patch);
  522. break;
  523. default:
  524. break;
  525. }
  526. dev_info(wm8994->dev, "%s revision %c CUST_ID %02x\n", devname,
  527. 'A' + wm8994->revision, wm8994->cust_id);
  528. switch (wm8994->type) {
  529. case WM1811:
  530. regmap_config = &wm1811_regmap_config;
  531. break;
  532. case WM8994:
  533. regmap_config = &wm8994_regmap_config;
  534. break;
  535. case WM8958:
  536. regmap_config = &wm8958_regmap_config;
  537. break;
  538. default:
  539. dev_err(wm8994->dev, "Unknown device type %d\n", wm8994->type);
  540. return -EINVAL;
  541. }
  542. ret = regmap_reinit_cache(wm8994->regmap, regmap_config);
  543. if (ret != 0) {
  544. dev_err(wm8994->dev, "Failed to reinit register cache: %d\n",
  545. ret);
  546. return ret;
  547. }
  548. /* Explicitly put the device into reset in case regulators
  549. * don't get disabled in order to ensure we know the device
  550. * state.
  551. */
  552. ret = wm8994_reg_write(wm8994, WM8994_SOFTWARE_RESET,
  553. wm8994_reg_read(wm8994, WM8994_SOFTWARE_RESET));
  554. if (ret != 0) {
  555. dev_err(wm8994->dev, "Failed to reset device: %d\n", ret);
  556. return ret;
  557. }
  558. if (regmap_patch) {
  559. ret = regmap_register_patch(wm8994->regmap, regmap_patch,
  560. patch_regs);
  561. if (ret != 0) {
  562. dev_err(wm8994->dev, "Failed to register patch: %d\n",
  563. ret);
  564. goto err;
  565. }
  566. }
  567. wm8994->irq_base = pdata->irq_base;
  568. wm8994->gpio_base = pdata->gpio_base;
  569. /* GPIO configuration is only applied if it's non-zero */
  570. for (i = 0; i < ARRAY_SIZE(pdata->gpio_defaults); i++) {
  571. if (pdata->gpio_defaults[i]) {
  572. wm8994_set_bits(wm8994, WM8994_GPIO_1 + i,
  573. 0xffff, pdata->gpio_defaults[i]);
  574. }
  575. }
  576. wm8994->ldo_ena_always_driven = pdata->ldo_ena_always_driven;
  577. if (pdata->spkmode_pu)
  578. pulls |= WM8994_SPKMODE_PU;
  579. /* Disable unneeded pulls */
  580. wm8994_set_bits(wm8994, WM8994_PULL_CONTROL_2,
  581. WM8994_LDO1ENA_PD | WM8994_LDO2ENA_PD |
  582. WM8994_SPKMODE_PU | WM8994_CSNADDR_PD,
  583. pulls);
  584. /* In some system designs where the regulators are not in use,
  585. * we can achieve a small reduction in leakage currents by
  586. * floating LDO outputs. This bit makes no difference if the
  587. * LDOs are enabled, it only affects cases where the LDOs were
  588. * in operation and are then disabled.
  589. */
  590. for (i = 0; i < WM8994_NUM_LDO_REGS; i++) {
  591. if (wm8994_ldo_in_use(pdata, i))
  592. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  593. WM8994_LDO1_DISCH, WM8994_LDO1_DISCH);
  594. else
  595. wm8994_set_bits(wm8994, WM8994_LDO_1 + i,
  596. WM8994_LDO1_DISCH, 0);
  597. }
  598. wm8994_irq_init(wm8994);
  599. ret = mfd_add_devices(wm8994->dev, -1,
  600. wm8994_devs, ARRAY_SIZE(wm8994_devs),
  601. NULL, 0, NULL);
  602. if (ret != 0) {
  603. dev_err(wm8994->dev, "Failed to add children: %d\n", ret);
  604. goto err_irq;
  605. }
  606. pm_runtime_enable(wm8994->dev);
  607. pm_runtime_idle(wm8994->dev);
  608. return 0;
  609. err_irq:
  610. wm8994_irq_exit(wm8994);
  611. err_enable:
  612. regulator_bulk_disable(wm8994->num_supplies,
  613. wm8994->supplies);
  614. err:
  615. mfd_remove_devices(wm8994->dev);
  616. return ret;
  617. }
  618. static void wm8994_device_exit(struct wm8994 *wm8994)
  619. {
  620. pm_runtime_disable(wm8994->dev);
  621. mfd_remove_devices(wm8994->dev);
  622. wm8994_irq_exit(wm8994);
  623. regulator_bulk_disable(wm8994->num_supplies,
  624. wm8994->supplies);
  625. }
  626. static const struct of_device_id wm8994_of_match[] = {
  627. { .compatible = "wlf,wm1811", .data = (void *)WM1811 },
  628. { .compatible = "wlf,wm8994", .data = (void *)WM8994 },
  629. { .compatible = "wlf,wm8958", .data = (void *)WM8958 },
  630. { }
  631. };
  632. MODULE_DEVICE_TABLE(of, wm8994_of_match);
  633. static int wm8994_i2c_probe(struct i2c_client *i2c,
  634. const struct i2c_device_id *id)
  635. {
  636. const struct of_device_id *of_id;
  637. struct wm8994 *wm8994;
  638. int ret;
  639. wm8994 = devm_kzalloc(&i2c->dev, sizeof(struct wm8994), GFP_KERNEL);
  640. if (wm8994 == NULL)
  641. return -ENOMEM;
  642. i2c_set_clientdata(i2c, wm8994);
  643. wm8994->dev = &i2c->dev;
  644. wm8994->irq = i2c->irq;
  645. if (i2c->dev.of_node) {
  646. of_id = of_match_device(wm8994_of_match, &i2c->dev);
  647. if (of_id)
  648. wm8994->type = (int)of_id->data;
  649. } else {
  650. wm8994->type = id->driver_data;
  651. }
  652. wm8994->regmap = devm_regmap_init_i2c(i2c, &wm8994_base_regmap_config);
  653. if (IS_ERR(wm8994->regmap)) {
  654. ret = PTR_ERR(wm8994->regmap);
  655. dev_err(wm8994->dev, "Failed to allocate register map: %d\n",
  656. ret);
  657. return ret;
  658. }
  659. return wm8994_device_init(wm8994, i2c->irq);
  660. }
  661. static int wm8994_i2c_remove(struct i2c_client *i2c)
  662. {
  663. struct wm8994 *wm8994 = i2c_get_clientdata(i2c);
  664. wm8994_device_exit(wm8994);
  665. return 0;
  666. }
  667. static const struct i2c_device_id wm8994_i2c_id[] = {
  668. { "wm1811", WM1811 },
  669. { "wm1811a", WM1811 },
  670. { "wm8994", WM8994 },
  671. { "wm8958", WM8958 },
  672. { }
  673. };
  674. MODULE_DEVICE_TABLE(i2c, wm8994_i2c_id);
  675. static const struct dev_pm_ops wm8994_pm_ops = {
  676. SET_RUNTIME_PM_OPS(wm8994_suspend, wm8994_resume, NULL)
  677. };
  678. static struct i2c_driver wm8994_i2c_driver = {
  679. .driver = {
  680. .name = "wm8994",
  681. .owner = THIS_MODULE,
  682. .pm = &wm8994_pm_ops,
  683. .of_match_table = of_match_ptr(wm8994_of_match),
  684. },
  685. .probe = wm8994_i2c_probe,
  686. .remove = wm8994_i2c_remove,
  687. .id_table = wm8994_i2c_id,
  688. };
  689. module_i2c_driver(wm8994_i2c_driver);
  690. MODULE_DESCRIPTION("Core support for the WM8994 audio CODEC");
  691. MODULE_LICENSE("GPL");
  692. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");