wm831x-dcdc.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * wm831x-dcdc.c -- DC-DC buck convertor driver for the WM831x series
  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. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/bitops.h>
  17. #include <linux/err.h>
  18. #include <linux/i2c.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/gpio.h>
  23. #include <linux/slab.h>
  24. #include <linux/mfd/wm831x/core.h>
  25. #include <linux/mfd/wm831x/regulator.h>
  26. #include <linux/mfd/wm831x/pdata.h>
  27. #define WM831X_BUCKV_MAX_SELECTOR 0x68
  28. #define WM831X_BUCKP_MAX_SELECTOR 0x66
  29. #define WM831X_DCDC_MODE_FAST 0
  30. #define WM831X_DCDC_MODE_NORMAL 1
  31. #define WM831X_DCDC_MODE_IDLE 2
  32. #define WM831X_DCDC_MODE_STANDBY 3
  33. #define WM831X_DCDC_MAX_NAME 6
  34. /* Register offsets in control block */
  35. #define WM831X_DCDC_CONTROL_1 0
  36. #define WM831X_DCDC_CONTROL_2 1
  37. #define WM831X_DCDC_ON_CONFIG 2
  38. #define WM831X_DCDC_SLEEP_CONTROL 3
  39. #define WM831X_DCDC_DVS_CONTROL 4
  40. /*
  41. * Shared
  42. */
  43. struct wm831x_dcdc {
  44. char name[WM831X_DCDC_MAX_NAME];
  45. struct regulator_desc desc;
  46. int base;
  47. struct wm831x *wm831x;
  48. struct regulator_dev *regulator;
  49. int dvs_gpio;
  50. int dvs_gpio_state;
  51. int on_vsel;
  52. int dvs_vsel;
  53. };
  54. static int wm831x_dcdc_is_enabled(struct regulator_dev *rdev)
  55. {
  56. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  57. struct wm831x *wm831x = dcdc->wm831x;
  58. int mask = 1 << rdev_get_id(rdev);
  59. int reg;
  60. reg = wm831x_reg_read(wm831x, WM831X_DCDC_ENABLE);
  61. if (reg < 0)
  62. return reg;
  63. if (reg & mask)
  64. return 1;
  65. else
  66. return 0;
  67. }
  68. static int wm831x_dcdc_enable(struct regulator_dev *rdev)
  69. {
  70. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  71. struct wm831x *wm831x = dcdc->wm831x;
  72. int mask = 1 << rdev_get_id(rdev);
  73. return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, mask);
  74. }
  75. static int wm831x_dcdc_disable(struct regulator_dev *rdev)
  76. {
  77. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  78. struct wm831x *wm831x = dcdc->wm831x;
  79. int mask = 1 << rdev_get_id(rdev);
  80. return wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, mask, 0);
  81. }
  82. static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev)
  83. {
  84. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  85. struct wm831x *wm831x = dcdc->wm831x;
  86. u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
  87. int val;
  88. val = wm831x_reg_read(wm831x, reg);
  89. if (val < 0)
  90. return val;
  91. val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT;
  92. switch (val) {
  93. case WM831X_DCDC_MODE_FAST:
  94. return REGULATOR_MODE_FAST;
  95. case WM831X_DCDC_MODE_NORMAL:
  96. return REGULATOR_MODE_NORMAL;
  97. case WM831X_DCDC_MODE_STANDBY:
  98. return REGULATOR_MODE_STANDBY;
  99. case WM831X_DCDC_MODE_IDLE:
  100. return REGULATOR_MODE_IDLE;
  101. default:
  102. BUG();
  103. return -EINVAL;
  104. }
  105. }
  106. static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg,
  107. unsigned int mode)
  108. {
  109. int val;
  110. switch (mode) {
  111. case REGULATOR_MODE_FAST:
  112. val = WM831X_DCDC_MODE_FAST;
  113. break;
  114. case REGULATOR_MODE_NORMAL:
  115. val = WM831X_DCDC_MODE_NORMAL;
  116. break;
  117. case REGULATOR_MODE_STANDBY:
  118. val = WM831X_DCDC_MODE_STANDBY;
  119. break;
  120. case REGULATOR_MODE_IDLE:
  121. val = WM831X_DCDC_MODE_IDLE;
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK,
  127. val << WM831X_DC1_ON_MODE_SHIFT);
  128. }
  129. static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode)
  130. {
  131. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  132. struct wm831x *wm831x = dcdc->wm831x;
  133. u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
  134. return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
  135. }
  136. static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev,
  137. unsigned int mode)
  138. {
  139. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  140. struct wm831x *wm831x = dcdc->wm831x;
  141. u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
  142. return wm831x_dcdc_set_mode_int(wm831x, reg, mode);
  143. }
  144. static int wm831x_dcdc_get_status(struct regulator_dev *rdev)
  145. {
  146. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  147. struct wm831x *wm831x = dcdc->wm831x;
  148. int ret;
  149. /* First, check for errors */
  150. ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
  151. if (ret < 0)
  152. return ret;
  153. if (ret & (1 << rdev_get_id(rdev))) {
  154. dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
  155. rdev_get_id(rdev) + 1);
  156. return REGULATOR_STATUS_ERROR;
  157. }
  158. /* DCDC1 and DCDC2 can additionally detect high voltage/current */
  159. if (rdev_get_id(rdev) < 2) {
  160. if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) {
  161. dev_dbg(wm831x->dev, "DCDC%d over voltage\n",
  162. rdev_get_id(rdev) + 1);
  163. return REGULATOR_STATUS_ERROR;
  164. }
  165. if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) {
  166. dev_dbg(wm831x->dev, "DCDC%d over current\n",
  167. rdev_get_id(rdev) + 1);
  168. return REGULATOR_STATUS_ERROR;
  169. }
  170. }
  171. /* Is the regulator on? */
  172. ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
  173. if (ret < 0)
  174. return ret;
  175. if (!(ret & (1 << rdev_get_id(rdev))))
  176. return REGULATOR_STATUS_OFF;
  177. /* TODO: When we handle hardware control modes so we can report the
  178. * current mode. */
  179. return REGULATOR_STATUS_ON;
  180. }
  181. static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data)
  182. {
  183. struct wm831x_dcdc *dcdc = data;
  184. regulator_notifier_call_chain(dcdc->regulator,
  185. REGULATOR_EVENT_UNDER_VOLTAGE,
  186. NULL);
  187. return IRQ_HANDLED;
  188. }
  189. static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data)
  190. {
  191. struct wm831x_dcdc *dcdc = data;
  192. regulator_notifier_call_chain(dcdc->regulator,
  193. REGULATOR_EVENT_OVER_CURRENT,
  194. NULL);
  195. return IRQ_HANDLED;
  196. }
  197. /*
  198. * BUCKV specifics
  199. */
  200. static int wm831x_buckv_list_voltage(struct regulator_dev *rdev,
  201. unsigned selector)
  202. {
  203. if (selector <= 0x8)
  204. return 600000;
  205. if (selector <= WM831X_BUCKV_MAX_SELECTOR)
  206. return 600000 + ((selector - 0x8) * 12500);
  207. return -EINVAL;
  208. }
  209. static int wm831x_buckv_select_min_voltage(struct regulator_dev *rdev,
  210. int min_uV, int max_uV)
  211. {
  212. u16 vsel;
  213. if (min_uV < 600000)
  214. vsel = 0;
  215. else if (min_uV <= 1800000)
  216. vsel = ((min_uV - 600000) / 12500) + 8;
  217. else
  218. return -EINVAL;
  219. if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV)
  220. return -EINVAL;
  221. return vsel;
  222. }
  223. static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
  224. {
  225. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  226. if (state == dcdc->dvs_gpio_state)
  227. return 0;
  228. dcdc->dvs_gpio_state = state;
  229. gpio_set_value(dcdc->dvs_gpio, state);
  230. /* Should wait for DVS state change to be asserted if we have
  231. * a GPIO for it, for now assume the device is configured
  232. * for the fastest possible transition.
  233. */
  234. return 0;
  235. }
  236. static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
  237. int min_uV, int max_uV, unsigned *selector)
  238. {
  239. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  240. struct wm831x *wm831x = dcdc->wm831x;
  241. int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
  242. int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
  243. int vsel, ret;
  244. vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
  245. if (vsel < 0)
  246. return vsel;
  247. *selector = vsel;
  248. /* If this value is already set then do a GPIO update if we can */
  249. if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
  250. return wm831x_buckv_set_dvs(rdev, 0);
  251. if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
  252. return wm831x_buckv_set_dvs(rdev, 1);
  253. /* Always set the ON status to the minimum voltage */
  254. ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
  255. if (ret < 0)
  256. return ret;
  257. dcdc->on_vsel = vsel;
  258. if (!dcdc->dvs_gpio)
  259. return ret;
  260. /* Kick the voltage transition now */
  261. ret = wm831x_buckv_set_dvs(rdev, 0);
  262. if (ret < 0)
  263. return ret;
  264. /*
  265. * If this VSEL is higher than the last one we've seen then
  266. * remember it as the DVS VSEL. This is optimised for CPUfreq
  267. * usage where we want to get to the highest voltage very
  268. * quickly.
  269. */
  270. if (vsel > dcdc->dvs_vsel) {
  271. ret = wm831x_set_bits(wm831x, dvs_reg,
  272. WM831X_DC1_DVS_VSEL_MASK,
  273. vsel);
  274. if (ret == 0)
  275. dcdc->dvs_vsel = vsel;
  276. else
  277. dev_warn(wm831x->dev,
  278. "Failed to set DCDC DVS VSEL: %d\n", ret);
  279. }
  280. return 0;
  281. }
  282. static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
  283. int uV)
  284. {
  285. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  286. struct wm831x *wm831x = dcdc->wm831x;
  287. u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
  288. int vsel;
  289. vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
  290. if (vsel < 0)
  291. return vsel;
  292. return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
  293. }
  294. static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
  295. {
  296. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  297. if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
  298. return dcdc->dvs_vsel;
  299. else
  300. return dcdc->on_vsel;
  301. }
  302. /* Current limit options */
  303. static const unsigned int wm831x_dcdc_ilim[] = {
  304. 125000, 250000, 375000, 500000, 625000, 750000, 875000, 1000000
  305. };
  306. static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
  307. int min_uA, int max_uA)
  308. {
  309. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  310. struct wm831x *wm831x = dcdc->wm831x;
  311. u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
  312. int i;
  313. for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
  314. if ((min_uA <= wm831x_dcdc_ilim[i]) &&
  315. (wm831x_dcdc_ilim[i] <= max_uA))
  316. break;
  317. }
  318. if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
  319. return -EINVAL;
  320. return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK,
  321. i << WM831X_DC1_HC_THR_SHIFT);
  322. }
  323. static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
  324. {
  325. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  326. struct wm831x *wm831x = dcdc->wm831x;
  327. u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
  328. int val;
  329. val = wm831x_reg_read(wm831x, reg);
  330. if (val < 0)
  331. return val;
  332. val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT;
  333. return wm831x_dcdc_ilim[val];
  334. }
  335. static struct regulator_ops wm831x_buckv_ops = {
  336. .set_voltage = wm831x_buckv_set_voltage,
  337. .get_voltage_sel = wm831x_buckv_get_voltage_sel,
  338. .list_voltage = wm831x_buckv_list_voltage,
  339. .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
  340. .set_current_limit = wm831x_buckv_set_current_limit,
  341. .get_current_limit = wm831x_buckv_get_current_limit,
  342. .is_enabled = wm831x_dcdc_is_enabled,
  343. .enable = wm831x_dcdc_enable,
  344. .disable = wm831x_dcdc_disable,
  345. .get_status = wm831x_dcdc_get_status,
  346. .get_mode = wm831x_dcdc_get_mode,
  347. .set_mode = wm831x_dcdc_set_mode,
  348. .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
  349. };
  350. /*
  351. * Set up DVS control. We just log errors since we can still run
  352. * (with reduced performance) if we fail.
  353. */
  354. static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
  355. struct wm831x_buckv_pdata *pdata)
  356. {
  357. struct wm831x *wm831x = dcdc->wm831x;
  358. int ret;
  359. u16 ctrl;
  360. if (!pdata || !pdata->dvs_gpio)
  361. return;
  362. ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
  363. if (ret < 0) {
  364. dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
  365. dcdc->name, ret);
  366. return;
  367. }
  368. /* gpiolib won't let us read the GPIO status so pick the higher
  369. * of the two existing voltages so we take it as platform data.
  370. */
  371. dcdc->dvs_gpio_state = pdata->dvs_init_state;
  372. ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
  373. if (ret < 0) {
  374. dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
  375. dcdc->name, ret);
  376. gpio_free(pdata->dvs_gpio);
  377. return;
  378. }
  379. dcdc->dvs_gpio = pdata->dvs_gpio;
  380. switch (pdata->dvs_control_src) {
  381. case 1:
  382. ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
  383. break;
  384. case 2:
  385. ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
  386. break;
  387. default:
  388. dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
  389. pdata->dvs_control_src, dcdc->name);
  390. return;
  391. }
  392. /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL
  393. * to make bootstrapping a bit smoother.
  394. */
  395. if (!dcdc->dvs_vsel) {
  396. ret = wm831x_set_bits(wm831x,
  397. dcdc->base + WM831X_DCDC_DVS_CONTROL,
  398. WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel);
  399. if (ret == 0)
  400. dcdc->dvs_vsel = dcdc->on_vsel;
  401. else
  402. dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n",
  403. ret);
  404. }
  405. ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
  406. WM831X_DC1_DVS_SRC_MASK, ctrl);
  407. if (ret < 0) {
  408. dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
  409. dcdc->name, ret);
  410. }
  411. }
  412. static __devinit int wm831x_buckv_probe(struct platform_device *pdev)
  413. {
  414. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  415. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  416. int id;
  417. struct wm831x_dcdc *dcdc;
  418. struct resource *res;
  419. int ret, irq;
  420. if (pdata && pdata->wm831x_num)
  421. id = (pdata->wm831x_num * 10) + 1;
  422. else
  423. id = 0;
  424. id = pdev->id - id;
  425. dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
  426. if (pdata == NULL || pdata->dcdc[id] == NULL)
  427. return -ENODEV;
  428. dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
  429. GFP_KERNEL);
  430. if (dcdc == NULL) {
  431. dev_err(&pdev->dev, "Unable to allocate private data\n");
  432. return -ENOMEM;
  433. }
  434. dcdc->wm831x = wm831x;
  435. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  436. if (res == NULL) {
  437. dev_err(&pdev->dev, "No I/O resource\n");
  438. ret = -EINVAL;
  439. goto err;
  440. }
  441. dcdc->base = res->start;
  442. snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
  443. dcdc->desc.name = dcdc->name;
  444. dcdc->desc.id = id;
  445. dcdc->desc.type = REGULATOR_VOLTAGE;
  446. dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
  447. dcdc->desc.ops = &wm831x_buckv_ops;
  448. dcdc->desc.owner = THIS_MODULE;
  449. ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
  450. if (ret < 0) {
  451. dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
  452. goto err;
  453. }
  454. dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
  455. ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL);
  456. if (ret < 0) {
  457. dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
  458. goto err;
  459. }
  460. dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
  461. if (pdata->dcdc[id])
  462. wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
  463. dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
  464. pdata->dcdc[id], dcdc, NULL);
  465. if (IS_ERR(dcdc->regulator)) {
  466. ret = PTR_ERR(dcdc->regulator);
  467. dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
  468. id + 1, ret);
  469. goto err;
  470. }
  471. irq = platform_get_irq_byname(pdev, "UV");
  472. ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
  473. IRQF_TRIGGER_RISING, dcdc->name, dcdc);
  474. if (ret != 0) {
  475. dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
  476. irq, ret);
  477. goto err_regulator;
  478. }
  479. irq = platform_get_irq_byname(pdev, "HC");
  480. ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
  481. IRQF_TRIGGER_RISING, dcdc->name, dcdc);
  482. if (ret != 0) {
  483. dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
  484. irq, ret);
  485. goto err_uv;
  486. }
  487. platform_set_drvdata(pdev, dcdc);
  488. return 0;
  489. err_uv:
  490. free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
  491. err_regulator:
  492. regulator_unregister(dcdc->regulator);
  493. err:
  494. if (dcdc->dvs_gpio)
  495. gpio_free(dcdc->dvs_gpio);
  496. return ret;
  497. }
  498. static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
  499. {
  500. struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
  501. platform_set_drvdata(pdev, NULL);
  502. free_irq(platform_get_irq_byname(pdev, "HC"), dcdc);
  503. free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
  504. regulator_unregister(dcdc->regulator);
  505. if (dcdc->dvs_gpio)
  506. gpio_free(dcdc->dvs_gpio);
  507. return 0;
  508. }
  509. static struct platform_driver wm831x_buckv_driver = {
  510. .probe = wm831x_buckv_probe,
  511. .remove = __devexit_p(wm831x_buckv_remove),
  512. .driver = {
  513. .name = "wm831x-buckv",
  514. .owner = THIS_MODULE,
  515. },
  516. };
  517. /*
  518. * BUCKP specifics
  519. */
  520. static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
  521. unsigned selector)
  522. {
  523. if (selector <= WM831X_BUCKP_MAX_SELECTOR)
  524. return 850000 + (selector * 25000);
  525. else
  526. return -EINVAL;
  527. }
  528. static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
  529. int min_uV, int max_uV, int *selector)
  530. {
  531. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  532. struct wm831x *wm831x = dcdc->wm831x;
  533. u16 vsel;
  534. if (min_uV <= 34000000)
  535. vsel = (min_uV - 850000) / 25000;
  536. else
  537. return -EINVAL;
  538. if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
  539. return -EINVAL;
  540. *selector = vsel;
  541. return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
  542. }
  543. static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
  544. int min_uV, int max_uV,
  545. unsigned *selector)
  546. {
  547. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  548. u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
  549. return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV,
  550. selector);
  551. }
  552. static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
  553. int uV)
  554. {
  555. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  556. u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
  557. unsigned selector;
  558. return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector);
  559. }
  560. static int wm831x_buckp_get_voltage_sel(struct regulator_dev *rdev)
  561. {
  562. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  563. struct wm831x *wm831x = dcdc->wm831x;
  564. u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
  565. int val;
  566. val = wm831x_reg_read(wm831x, reg);
  567. if (val < 0)
  568. return val;
  569. return val & WM831X_DC3_ON_VSEL_MASK;
  570. }
  571. static struct regulator_ops wm831x_buckp_ops = {
  572. .set_voltage = wm831x_buckp_set_voltage,
  573. .get_voltage_sel = wm831x_buckp_get_voltage_sel,
  574. .list_voltage = wm831x_buckp_list_voltage,
  575. .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
  576. .is_enabled = wm831x_dcdc_is_enabled,
  577. .enable = wm831x_dcdc_enable,
  578. .disable = wm831x_dcdc_disable,
  579. .get_status = wm831x_dcdc_get_status,
  580. .get_mode = wm831x_dcdc_get_mode,
  581. .set_mode = wm831x_dcdc_set_mode,
  582. .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
  583. };
  584. static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
  585. {
  586. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  587. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  588. int id;
  589. struct wm831x_dcdc *dcdc;
  590. struct resource *res;
  591. int ret, irq;
  592. if (pdata && pdata->wm831x_num)
  593. id = (pdata->wm831x_num * 10) + 1;
  594. else
  595. id = 0;
  596. id = pdev->id - id;
  597. dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
  598. if (pdata == NULL || pdata->dcdc[id] == NULL)
  599. return -ENODEV;
  600. dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc),
  601. GFP_KERNEL);
  602. if (dcdc == NULL) {
  603. dev_err(&pdev->dev, "Unable to allocate private data\n");
  604. return -ENOMEM;
  605. }
  606. dcdc->wm831x = wm831x;
  607. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  608. if (res == NULL) {
  609. dev_err(&pdev->dev, "No I/O resource\n");
  610. ret = -EINVAL;
  611. goto err;
  612. }
  613. dcdc->base = res->start;
  614. snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
  615. dcdc->desc.name = dcdc->name;
  616. dcdc->desc.id = id;
  617. dcdc->desc.type = REGULATOR_VOLTAGE;
  618. dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
  619. dcdc->desc.ops = &wm831x_buckp_ops;
  620. dcdc->desc.owner = THIS_MODULE;
  621. dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
  622. pdata->dcdc[id], dcdc, NULL);
  623. if (IS_ERR(dcdc->regulator)) {
  624. ret = PTR_ERR(dcdc->regulator);
  625. dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
  626. id + 1, ret);
  627. goto err;
  628. }
  629. irq = platform_get_irq_byname(pdev, "UV");
  630. ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
  631. IRQF_TRIGGER_RISING, dcdc->name, dcdc);
  632. if (ret != 0) {
  633. dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
  634. irq, ret);
  635. goto err_regulator;
  636. }
  637. platform_set_drvdata(pdev, dcdc);
  638. return 0;
  639. err_regulator:
  640. regulator_unregister(dcdc->regulator);
  641. err:
  642. return ret;
  643. }
  644. static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
  645. {
  646. struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
  647. platform_set_drvdata(pdev, NULL);
  648. free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
  649. regulator_unregister(dcdc->regulator);
  650. return 0;
  651. }
  652. static struct platform_driver wm831x_buckp_driver = {
  653. .probe = wm831x_buckp_probe,
  654. .remove = __devexit_p(wm831x_buckp_remove),
  655. .driver = {
  656. .name = "wm831x-buckp",
  657. .owner = THIS_MODULE,
  658. },
  659. };
  660. /*
  661. * DCDC boost convertors
  662. */
  663. static int wm831x_boostp_get_status(struct regulator_dev *rdev)
  664. {
  665. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  666. struct wm831x *wm831x = dcdc->wm831x;
  667. int ret;
  668. /* First, check for errors */
  669. ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
  670. if (ret < 0)
  671. return ret;
  672. if (ret & (1 << rdev_get_id(rdev))) {
  673. dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
  674. rdev_get_id(rdev) + 1);
  675. return REGULATOR_STATUS_ERROR;
  676. }
  677. /* Is the regulator on? */
  678. ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
  679. if (ret < 0)
  680. return ret;
  681. if (ret & (1 << rdev_get_id(rdev)))
  682. return REGULATOR_STATUS_ON;
  683. else
  684. return REGULATOR_STATUS_OFF;
  685. }
  686. static struct regulator_ops wm831x_boostp_ops = {
  687. .get_status = wm831x_boostp_get_status,
  688. .is_enabled = wm831x_dcdc_is_enabled,
  689. .enable = wm831x_dcdc_enable,
  690. .disable = wm831x_dcdc_disable,
  691. };
  692. static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
  693. {
  694. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  695. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  696. int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
  697. struct wm831x_dcdc *dcdc;
  698. struct resource *res;
  699. int ret, irq;
  700. dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
  701. if (pdata == NULL || pdata->dcdc[id] == NULL)
  702. return -ENODEV;
  703. dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
  704. if (dcdc == NULL) {
  705. dev_err(&pdev->dev, "Unable to allocate private data\n");
  706. return -ENOMEM;
  707. }
  708. dcdc->wm831x = wm831x;
  709. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  710. if (res == NULL) {
  711. dev_err(&pdev->dev, "No I/O resource\n");
  712. ret = -EINVAL;
  713. goto err;
  714. }
  715. dcdc->base = res->start;
  716. snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
  717. dcdc->desc.name = dcdc->name;
  718. dcdc->desc.id = id;
  719. dcdc->desc.type = REGULATOR_VOLTAGE;
  720. dcdc->desc.ops = &wm831x_boostp_ops;
  721. dcdc->desc.owner = THIS_MODULE;
  722. dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
  723. pdata->dcdc[id], dcdc, NULL);
  724. if (IS_ERR(dcdc->regulator)) {
  725. ret = PTR_ERR(dcdc->regulator);
  726. dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
  727. id + 1, ret);
  728. goto err;
  729. }
  730. irq = platform_get_irq_byname(pdev, "UV");
  731. ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
  732. IRQF_TRIGGER_RISING, dcdc->name,
  733. dcdc);
  734. if (ret != 0) {
  735. dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
  736. irq, ret);
  737. goto err_regulator;
  738. }
  739. platform_set_drvdata(pdev, dcdc);
  740. return 0;
  741. err_regulator:
  742. regulator_unregister(dcdc->regulator);
  743. err:
  744. kfree(dcdc);
  745. return ret;
  746. }
  747. static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
  748. {
  749. struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
  750. platform_set_drvdata(pdev, NULL);
  751. free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
  752. regulator_unregister(dcdc->regulator);
  753. kfree(dcdc);
  754. return 0;
  755. }
  756. static struct platform_driver wm831x_boostp_driver = {
  757. .probe = wm831x_boostp_probe,
  758. .remove = __devexit_p(wm831x_boostp_remove),
  759. .driver = {
  760. .name = "wm831x-boostp",
  761. .owner = THIS_MODULE,
  762. },
  763. };
  764. /*
  765. * External Power Enable
  766. *
  767. * These aren't actually DCDCs but look like them in hardware so share
  768. * code.
  769. */
  770. #define WM831X_EPE_BASE 6
  771. static struct regulator_ops wm831x_epe_ops = {
  772. .is_enabled = wm831x_dcdc_is_enabled,
  773. .enable = wm831x_dcdc_enable,
  774. .disable = wm831x_dcdc_disable,
  775. .get_status = wm831x_dcdc_get_status,
  776. };
  777. static __devinit int wm831x_epe_probe(struct platform_device *pdev)
  778. {
  779. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  780. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  781. int id = pdev->id % ARRAY_SIZE(pdata->epe);
  782. struct wm831x_dcdc *dcdc;
  783. int ret;
  784. dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
  785. if (pdata == NULL || pdata->epe[id] == NULL)
  786. return -ENODEV;
  787. dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
  788. if (dcdc == NULL) {
  789. dev_err(&pdev->dev, "Unable to allocate private data\n");
  790. return -ENOMEM;
  791. }
  792. dcdc->wm831x = wm831x;
  793. /* For current parts this is correct; probably need to revisit
  794. * in future.
  795. */
  796. snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
  797. dcdc->desc.name = dcdc->name;
  798. dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
  799. dcdc->desc.ops = &wm831x_epe_ops;
  800. dcdc->desc.type = REGULATOR_VOLTAGE;
  801. dcdc->desc.owner = THIS_MODULE;
  802. dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
  803. pdata->epe[id], dcdc, NULL);
  804. if (IS_ERR(dcdc->regulator)) {
  805. ret = PTR_ERR(dcdc->regulator);
  806. dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
  807. id + 1, ret);
  808. goto err;
  809. }
  810. platform_set_drvdata(pdev, dcdc);
  811. return 0;
  812. err:
  813. kfree(dcdc);
  814. return ret;
  815. }
  816. static __devexit int wm831x_epe_remove(struct platform_device *pdev)
  817. {
  818. struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
  819. platform_set_drvdata(pdev, NULL);
  820. regulator_unregister(dcdc->regulator);
  821. kfree(dcdc);
  822. return 0;
  823. }
  824. static struct platform_driver wm831x_epe_driver = {
  825. .probe = wm831x_epe_probe,
  826. .remove = __devexit_p(wm831x_epe_remove),
  827. .driver = {
  828. .name = "wm831x-epe",
  829. .owner = THIS_MODULE,
  830. },
  831. };
  832. static int __init wm831x_dcdc_init(void)
  833. {
  834. int ret;
  835. ret = platform_driver_register(&wm831x_buckv_driver);
  836. if (ret != 0)
  837. pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
  838. ret = platform_driver_register(&wm831x_buckp_driver);
  839. if (ret != 0)
  840. pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
  841. ret = platform_driver_register(&wm831x_boostp_driver);
  842. if (ret != 0)
  843. pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
  844. ret = platform_driver_register(&wm831x_epe_driver);
  845. if (ret != 0)
  846. pr_err("Failed to register WM831x EPE driver: %d\n", ret);
  847. return 0;
  848. }
  849. subsys_initcall(wm831x_dcdc_init);
  850. static void __exit wm831x_dcdc_exit(void)
  851. {
  852. platform_driver_unregister(&wm831x_epe_driver);
  853. platform_driver_unregister(&wm831x_boostp_driver);
  854. platform_driver_unregister(&wm831x_buckp_driver);
  855. platform_driver_unregister(&wm831x_buckv_driver);
  856. }
  857. module_exit(wm831x_dcdc_exit);
  858. /* Module information */
  859. MODULE_AUTHOR("Mark Brown");
  860. MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
  861. MODULE_LICENSE("GPL");
  862. MODULE_ALIAS("platform:wm831x-buckv");
  863. MODULE_ALIAS("platform:wm831x-buckp");
  864. MODULE_ALIAS("platform:wm831x-epe");