wm8994-core.c 19 KB

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