wm8994-core.c 19 KB

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