wm831x-dcdc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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_select_max_voltage(struct regulator_dev *rdev,
  224. int min_uV, int max_uV)
  225. {
  226. u16 vsel;
  227. if (max_uV < 600000 || max_uV > 1800000)
  228. return -EINVAL;
  229. vsel = ((max_uV - 600000) / 12500) + 8;
  230. if (wm831x_buckv_list_voltage(rdev, vsel) < min_uV ||
  231. wm831x_buckv_list_voltage(rdev, vsel) < max_uV)
  232. return -EINVAL;
  233. return vsel;
  234. }
  235. static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state)
  236. {
  237. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  238. if (state == dcdc->dvs_gpio_state)
  239. return 0;
  240. dcdc->dvs_gpio_state = state;
  241. gpio_set_value(dcdc->dvs_gpio, state);
  242. /* Should wait for DVS state change to be asserted if we have
  243. * a GPIO for it, for now assume the device is configured
  244. * for the fastest possible transition.
  245. */
  246. return 0;
  247. }
  248. static int wm831x_buckv_set_voltage(struct regulator_dev *rdev,
  249. int min_uV, int max_uV, unsigned *selector)
  250. {
  251. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  252. struct wm831x *wm831x = dcdc->wm831x;
  253. int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
  254. int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL;
  255. int vsel, ret;
  256. vsel = wm831x_buckv_select_min_voltage(rdev, min_uV, max_uV);
  257. if (vsel < 0)
  258. return vsel;
  259. *selector = vsel;
  260. /* If this value is already set then do a GPIO update if we can */
  261. if (dcdc->dvs_gpio && dcdc->on_vsel == vsel)
  262. return wm831x_buckv_set_dvs(rdev, 0);
  263. if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel)
  264. return wm831x_buckv_set_dvs(rdev, 1);
  265. /* Always set the ON status to the minimum voltage */
  266. ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel);
  267. if (ret < 0)
  268. return ret;
  269. dcdc->on_vsel = vsel;
  270. if (!dcdc->dvs_gpio)
  271. return ret;
  272. /* Kick the voltage transition now */
  273. ret = wm831x_buckv_set_dvs(rdev, 0);
  274. if (ret < 0)
  275. return ret;
  276. /* Set the high voltage as the DVS voltage. This is optimised
  277. * for CPUfreq usage, most processors will keep the maximum
  278. * voltage constant and lower the minimum with the frequency. */
  279. vsel = wm831x_buckv_select_max_voltage(rdev, min_uV, max_uV);
  280. if (vsel < 0) {
  281. /* This should never happen - at worst the same vsel
  282. * should be chosen */
  283. WARN_ON(vsel < 0);
  284. return 0;
  285. }
  286. /* Don't bother if it's the same VSEL we're already using */
  287. if (vsel == dcdc->on_vsel)
  288. return 0;
  289. ret = wm831x_set_bits(wm831x, dvs_reg, WM831X_DC1_DVS_VSEL_MASK, vsel);
  290. if (ret == 0)
  291. dcdc->dvs_vsel = vsel;
  292. else
  293. dev_warn(wm831x->dev, "Failed to set DCDC DVS VSEL: %d\n",
  294. ret);
  295. return 0;
  296. }
  297. static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev,
  298. int uV)
  299. {
  300. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  301. struct wm831x *wm831x = dcdc->wm831x;
  302. u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
  303. int vsel;
  304. vsel = wm831x_buckv_select_min_voltage(rdev, uV, uV);
  305. if (vsel < 0)
  306. return vsel;
  307. return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel);
  308. }
  309. static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev)
  310. {
  311. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  312. if (dcdc->dvs_gpio && dcdc->dvs_gpio_state)
  313. return dcdc->dvs_vsel;
  314. else
  315. return dcdc->on_vsel;
  316. }
  317. /* Current limit options */
  318. static u16 wm831x_dcdc_ilim[] = {
  319. 125, 250, 375, 500, 625, 750, 875, 1000
  320. };
  321. static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev,
  322. int min_uA, int max_uA)
  323. {
  324. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  325. struct wm831x *wm831x = dcdc->wm831x;
  326. u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
  327. int i;
  328. for (i = 0; i < ARRAY_SIZE(wm831x_dcdc_ilim); i++) {
  329. if (max_uA <= wm831x_dcdc_ilim[i])
  330. break;
  331. }
  332. if (i == ARRAY_SIZE(wm831x_dcdc_ilim))
  333. return -EINVAL;
  334. return wm831x_set_bits(wm831x, reg, WM831X_DC1_HC_THR_MASK, i);
  335. }
  336. static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev)
  337. {
  338. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  339. struct wm831x *wm831x = dcdc->wm831x;
  340. u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2;
  341. int val;
  342. val = wm831x_reg_read(wm831x, reg);
  343. if (val < 0)
  344. return val;
  345. return wm831x_dcdc_ilim[val & WM831X_DC1_HC_THR_MASK];
  346. }
  347. static struct regulator_ops wm831x_buckv_ops = {
  348. .set_voltage = wm831x_buckv_set_voltage,
  349. .get_voltage_sel = wm831x_buckv_get_voltage_sel,
  350. .list_voltage = wm831x_buckv_list_voltage,
  351. .set_suspend_voltage = wm831x_buckv_set_suspend_voltage,
  352. .set_current_limit = wm831x_buckv_set_current_limit,
  353. .get_current_limit = wm831x_buckv_get_current_limit,
  354. .is_enabled = wm831x_dcdc_is_enabled,
  355. .enable = wm831x_dcdc_enable,
  356. .disable = wm831x_dcdc_disable,
  357. .get_status = wm831x_dcdc_get_status,
  358. .get_mode = wm831x_dcdc_get_mode,
  359. .set_mode = wm831x_dcdc_set_mode,
  360. .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
  361. };
  362. /*
  363. * Set up DVS control. We just log errors since we can still run
  364. * (with reduced performance) if we fail.
  365. */
  366. static __devinit void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc,
  367. struct wm831x_buckv_pdata *pdata)
  368. {
  369. struct wm831x *wm831x = dcdc->wm831x;
  370. int ret;
  371. u16 ctrl;
  372. if (!pdata || !pdata->dvs_gpio)
  373. return;
  374. switch (pdata->dvs_control_src) {
  375. case 1:
  376. ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT;
  377. break;
  378. case 2:
  379. ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT;
  380. break;
  381. default:
  382. dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n",
  383. pdata->dvs_control_src, dcdc->name);
  384. return;
  385. }
  386. ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL,
  387. WM831X_DC1_DVS_SRC_MASK, ctrl);
  388. if (ret < 0) {
  389. dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n",
  390. dcdc->name, ret);
  391. return;
  392. }
  393. ret = gpio_request(pdata->dvs_gpio, "DCDC DVS");
  394. if (ret < 0) {
  395. dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n",
  396. dcdc->name, ret);
  397. return;
  398. }
  399. /* gpiolib won't let us read the GPIO status so pick the higher
  400. * of the two existing voltages so we take it as platform data.
  401. */
  402. dcdc->dvs_gpio_state = pdata->dvs_init_state;
  403. ret = gpio_direction_output(pdata->dvs_gpio, dcdc->dvs_gpio_state);
  404. if (ret < 0) {
  405. dev_err(wm831x->dev, "Failed to enable %s DVS GPIO: %d\n",
  406. dcdc->name, ret);
  407. gpio_free(pdata->dvs_gpio);
  408. return;
  409. }
  410. dcdc->dvs_gpio = pdata->dvs_gpio;
  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 = pdev->id % ARRAY_SIZE(pdata->dcdc);
  417. struct wm831x_dcdc *dcdc;
  418. struct resource *res;
  419. int ret, irq;
  420. dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
  421. if (pdata == NULL || pdata->dcdc[id] == NULL)
  422. return -ENODEV;
  423. dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
  424. if (dcdc == NULL) {
  425. dev_err(&pdev->dev, "Unable to allocate private data\n");
  426. return -ENOMEM;
  427. }
  428. dcdc->wm831x = wm831x;
  429. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  430. if (res == NULL) {
  431. dev_err(&pdev->dev, "No I/O resource\n");
  432. ret = -EINVAL;
  433. goto err;
  434. }
  435. dcdc->base = res->start;
  436. snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
  437. dcdc->desc.name = dcdc->name;
  438. dcdc->desc.id = id;
  439. dcdc->desc.type = REGULATOR_VOLTAGE;
  440. dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1;
  441. dcdc->desc.ops = &wm831x_buckv_ops;
  442. dcdc->desc.owner = THIS_MODULE;
  443. ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
  444. if (ret < 0) {
  445. dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret);
  446. goto err;
  447. }
  448. dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK;
  449. ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG);
  450. if (ret < 0) {
  451. dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret);
  452. goto err;
  453. }
  454. dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK;
  455. if (pdata->dcdc[id])
  456. wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data);
  457. dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
  458. pdata->dcdc[id], dcdc);
  459. if (IS_ERR(dcdc->regulator)) {
  460. ret = PTR_ERR(dcdc->regulator);
  461. dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
  462. id + 1, ret);
  463. goto err;
  464. }
  465. irq = platform_get_irq_byname(pdev, "UV");
  466. ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
  467. IRQF_TRIGGER_RISING, dcdc->name, dcdc);
  468. if (ret != 0) {
  469. dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
  470. irq, ret);
  471. goto err_regulator;
  472. }
  473. irq = platform_get_irq_byname(pdev, "HC");
  474. ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq,
  475. IRQF_TRIGGER_RISING, dcdc->name, dcdc);
  476. if (ret != 0) {
  477. dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n",
  478. irq, ret);
  479. goto err_uv;
  480. }
  481. platform_set_drvdata(pdev, dcdc);
  482. return 0;
  483. err_uv:
  484. free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
  485. err_regulator:
  486. regulator_unregister(dcdc->regulator);
  487. err:
  488. if (dcdc->dvs_gpio)
  489. gpio_free(dcdc->dvs_gpio);
  490. kfree(dcdc);
  491. return ret;
  492. }
  493. static __devexit int wm831x_buckv_remove(struct platform_device *pdev)
  494. {
  495. struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
  496. platform_set_drvdata(pdev, NULL);
  497. free_irq(platform_get_irq_byname(pdev, "HC"), dcdc);
  498. free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
  499. regulator_unregister(dcdc->regulator);
  500. if (dcdc->dvs_gpio)
  501. gpio_free(dcdc->dvs_gpio);
  502. kfree(dcdc);
  503. return 0;
  504. }
  505. static struct platform_driver wm831x_buckv_driver = {
  506. .probe = wm831x_buckv_probe,
  507. .remove = __devexit_p(wm831x_buckv_remove),
  508. .driver = {
  509. .name = "wm831x-buckv",
  510. .owner = THIS_MODULE,
  511. },
  512. };
  513. /*
  514. * BUCKP specifics
  515. */
  516. static int wm831x_buckp_list_voltage(struct regulator_dev *rdev,
  517. unsigned selector)
  518. {
  519. if (selector <= WM831X_BUCKP_MAX_SELECTOR)
  520. return 850000 + (selector * 25000);
  521. else
  522. return -EINVAL;
  523. }
  524. static int wm831x_buckp_set_voltage_int(struct regulator_dev *rdev, int reg,
  525. int min_uV, int max_uV, int *selector)
  526. {
  527. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  528. struct wm831x *wm831x = dcdc->wm831x;
  529. u16 vsel;
  530. if (min_uV <= 34000000)
  531. vsel = (min_uV - 850000) / 25000;
  532. else
  533. return -EINVAL;
  534. if (wm831x_buckp_list_voltage(rdev, vsel) > max_uV)
  535. return -EINVAL;
  536. *selector = vsel;
  537. return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, vsel);
  538. }
  539. static int wm831x_buckp_set_voltage(struct regulator_dev *rdev,
  540. int min_uV, int max_uV,
  541. unsigned *selector)
  542. {
  543. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  544. u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
  545. return wm831x_buckp_set_voltage_int(rdev, reg, min_uV, max_uV,
  546. selector);
  547. }
  548. static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev,
  549. int uV)
  550. {
  551. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  552. u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL;
  553. unsigned selector;
  554. return wm831x_buckp_set_voltage_int(rdev, reg, uV, uV, &selector);
  555. }
  556. static int wm831x_buckp_get_voltage_sel(struct regulator_dev *rdev)
  557. {
  558. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  559. struct wm831x *wm831x = dcdc->wm831x;
  560. u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG;
  561. int val;
  562. val = wm831x_reg_read(wm831x, reg);
  563. if (val < 0)
  564. return val;
  565. return val & WM831X_DC3_ON_VSEL_MASK;
  566. }
  567. static struct regulator_ops wm831x_buckp_ops = {
  568. .set_voltage = wm831x_buckp_set_voltage,
  569. .get_voltage_sel = wm831x_buckp_get_voltage_sel,
  570. .list_voltage = wm831x_buckp_list_voltage,
  571. .set_suspend_voltage = wm831x_buckp_set_suspend_voltage,
  572. .is_enabled = wm831x_dcdc_is_enabled,
  573. .enable = wm831x_dcdc_enable,
  574. .disable = wm831x_dcdc_disable,
  575. .get_status = wm831x_dcdc_get_status,
  576. .get_mode = wm831x_dcdc_get_mode,
  577. .set_mode = wm831x_dcdc_set_mode,
  578. .set_suspend_mode = wm831x_dcdc_set_suspend_mode,
  579. };
  580. static __devinit int wm831x_buckp_probe(struct platform_device *pdev)
  581. {
  582. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  583. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  584. int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
  585. struct wm831x_dcdc *dcdc;
  586. struct resource *res;
  587. int ret, irq;
  588. dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
  589. if (pdata == NULL || pdata->dcdc[id] == NULL)
  590. return -ENODEV;
  591. dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
  592. if (dcdc == NULL) {
  593. dev_err(&pdev->dev, "Unable to allocate private data\n");
  594. return -ENOMEM;
  595. }
  596. dcdc->wm831x = wm831x;
  597. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  598. if (res == NULL) {
  599. dev_err(&pdev->dev, "No I/O resource\n");
  600. ret = -EINVAL;
  601. goto err;
  602. }
  603. dcdc->base = res->start;
  604. snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
  605. dcdc->desc.name = dcdc->name;
  606. dcdc->desc.id = id;
  607. dcdc->desc.type = REGULATOR_VOLTAGE;
  608. dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1;
  609. dcdc->desc.ops = &wm831x_buckp_ops;
  610. dcdc->desc.owner = THIS_MODULE;
  611. dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
  612. pdata->dcdc[id], dcdc);
  613. if (IS_ERR(dcdc->regulator)) {
  614. ret = PTR_ERR(dcdc->regulator);
  615. dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
  616. id + 1, ret);
  617. goto err;
  618. }
  619. irq = platform_get_irq_byname(pdev, "UV");
  620. ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
  621. IRQF_TRIGGER_RISING, dcdc->name, dcdc);
  622. if (ret != 0) {
  623. dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
  624. irq, ret);
  625. goto err_regulator;
  626. }
  627. platform_set_drvdata(pdev, dcdc);
  628. return 0;
  629. err_regulator:
  630. regulator_unregister(dcdc->regulator);
  631. err:
  632. kfree(dcdc);
  633. return ret;
  634. }
  635. static __devexit int wm831x_buckp_remove(struct platform_device *pdev)
  636. {
  637. struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
  638. platform_set_drvdata(pdev, NULL);
  639. free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
  640. regulator_unregister(dcdc->regulator);
  641. kfree(dcdc);
  642. return 0;
  643. }
  644. static struct platform_driver wm831x_buckp_driver = {
  645. .probe = wm831x_buckp_probe,
  646. .remove = __devexit_p(wm831x_buckp_remove),
  647. .driver = {
  648. .name = "wm831x-buckp",
  649. .owner = THIS_MODULE,
  650. },
  651. };
  652. /*
  653. * DCDC boost convertors
  654. */
  655. static int wm831x_boostp_get_status(struct regulator_dev *rdev)
  656. {
  657. struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev);
  658. struct wm831x *wm831x = dcdc->wm831x;
  659. int ret;
  660. /* First, check for errors */
  661. ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS);
  662. if (ret < 0)
  663. return ret;
  664. if (ret & (1 << rdev_get_id(rdev))) {
  665. dev_dbg(wm831x->dev, "DCDC%d under voltage\n",
  666. rdev_get_id(rdev) + 1);
  667. return REGULATOR_STATUS_ERROR;
  668. }
  669. /* Is the regulator on? */
  670. ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS);
  671. if (ret < 0)
  672. return ret;
  673. if (ret & (1 << rdev_get_id(rdev)))
  674. return REGULATOR_STATUS_ON;
  675. else
  676. return REGULATOR_STATUS_OFF;
  677. }
  678. static struct regulator_ops wm831x_boostp_ops = {
  679. .get_status = wm831x_boostp_get_status,
  680. .is_enabled = wm831x_dcdc_is_enabled,
  681. .enable = wm831x_dcdc_enable,
  682. .disable = wm831x_dcdc_disable,
  683. };
  684. static __devinit int wm831x_boostp_probe(struct platform_device *pdev)
  685. {
  686. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  687. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  688. int id = pdev->id % ARRAY_SIZE(pdata->dcdc);
  689. struct wm831x_dcdc *dcdc;
  690. struct resource *res;
  691. int ret, irq;
  692. dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1);
  693. if (pdata == NULL || pdata->dcdc[id] == NULL)
  694. return -ENODEV;
  695. dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
  696. if (dcdc == NULL) {
  697. dev_err(&pdev->dev, "Unable to allocate private data\n");
  698. return -ENOMEM;
  699. }
  700. dcdc->wm831x = wm831x;
  701. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  702. if (res == NULL) {
  703. dev_err(&pdev->dev, "No I/O resource\n");
  704. ret = -EINVAL;
  705. goto err;
  706. }
  707. dcdc->base = res->start;
  708. snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1);
  709. dcdc->desc.name = dcdc->name;
  710. dcdc->desc.id = id;
  711. dcdc->desc.type = REGULATOR_VOLTAGE;
  712. dcdc->desc.ops = &wm831x_boostp_ops;
  713. dcdc->desc.owner = THIS_MODULE;
  714. dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
  715. pdata->dcdc[id], dcdc);
  716. if (IS_ERR(dcdc->regulator)) {
  717. ret = PTR_ERR(dcdc->regulator);
  718. dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n",
  719. id + 1, ret);
  720. goto err;
  721. }
  722. irq = platform_get_irq_byname(pdev, "UV");
  723. ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq,
  724. IRQF_TRIGGER_RISING, dcdc->name,
  725. dcdc);
  726. if (ret != 0) {
  727. dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n",
  728. irq, ret);
  729. goto err_regulator;
  730. }
  731. platform_set_drvdata(pdev, dcdc);
  732. return 0;
  733. err_regulator:
  734. regulator_unregister(dcdc->regulator);
  735. err:
  736. kfree(dcdc);
  737. return ret;
  738. }
  739. static __devexit int wm831x_boostp_remove(struct platform_device *pdev)
  740. {
  741. struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
  742. platform_set_drvdata(pdev, NULL);
  743. free_irq(platform_get_irq_byname(pdev, "UV"), dcdc);
  744. regulator_unregister(dcdc->regulator);
  745. kfree(dcdc);
  746. return 0;
  747. }
  748. static struct platform_driver wm831x_boostp_driver = {
  749. .probe = wm831x_boostp_probe,
  750. .remove = __devexit_p(wm831x_boostp_remove),
  751. .driver = {
  752. .name = "wm831x-boostp",
  753. .owner = THIS_MODULE,
  754. },
  755. };
  756. /*
  757. * External Power Enable
  758. *
  759. * These aren't actually DCDCs but look like them in hardware so share
  760. * code.
  761. */
  762. #define WM831X_EPE_BASE 6
  763. static struct regulator_ops wm831x_epe_ops = {
  764. .is_enabled = wm831x_dcdc_is_enabled,
  765. .enable = wm831x_dcdc_enable,
  766. .disable = wm831x_dcdc_disable,
  767. .get_status = wm831x_dcdc_get_status,
  768. };
  769. static __devinit int wm831x_epe_probe(struct platform_device *pdev)
  770. {
  771. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  772. struct wm831x_pdata *pdata = wm831x->dev->platform_data;
  773. int id = pdev->id % ARRAY_SIZE(pdata->epe);
  774. struct wm831x_dcdc *dcdc;
  775. int ret;
  776. dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1);
  777. if (pdata == NULL || pdata->epe[id] == NULL)
  778. return -ENODEV;
  779. dcdc = kzalloc(sizeof(struct wm831x_dcdc), GFP_KERNEL);
  780. if (dcdc == NULL) {
  781. dev_err(&pdev->dev, "Unable to allocate private data\n");
  782. return -ENOMEM;
  783. }
  784. dcdc->wm831x = wm831x;
  785. /* For current parts this is correct; probably need to revisit
  786. * in future.
  787. */
  788. snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1);
  789. dcdc->desc.name = dcdc->name;
  790. dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */
  791. dcdc->desc.ops = &wm831x_epe_ops;
  792. dcdc->desc.type = REGULATOR_VOLTAGE;
  793. dcdc->desc.owner = THIS_MODULE;
  794. dcdc->regulator = regulator_register(&dcdc->desc, &pdev->dev,
  795. pdata->epe[id], dcdc);
  796. if (IS_ERR(dcdc->regulator)) {
  797. ret = PTR_ERR(dcdc->regulator);
  798. dev_err(wm831x->dev, "Failed to register EPE%d: %d\n",
  799. id + 1, ret);
  800. goto err;
  801. }
  802. platform_set_drvdata(pdev, dcdc);
  803. return 0;
  804. err:
  805. kfree(dcdc);
  806. return ret;
  807. }
  808. static __devexit int wm831x_epe_remove(struct platform_device *pdev)
  809. {
  810. struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev);
  811. platform_set_drvdata(pdev, NULL);
  812. regulator_unregister(dcdc->regulator);
  813. kfree(dcdc);
  814. return 0;
  815. }
  816. static struct platform_driver wm831x_epe_driver = {
  817. .probe = wm831x_epe_probe,
  818. .remove = __devexit_p(wm831x_epe_remove),
  819. .driver = {
  820. .name = "wm831x-epe",
  821. .owner = THIS_MODULE,
  822. },
  823. };
  824. static int __init wm831x_dcdc_init(void)
  825. {
  826. int ret;
  827. ret = platform_driver_register(&wm831x_buckv_driver);
  828. if (ret != 0)
  829. pr_err("Failed to register WM831x BUCKV driver: %d\n", ret);
  830. ret = platform_driver_register(&wm831x_buckp_driver);
  831. if (ret != 0)
  832. pr_err("Failed to register WM831x BUCKP driver: %d\n", ret);
  833. ret = platform_driver_register(&wm831x_boostp_driver);
  834. if (ret != 0)
  835. pr_err("Failed to register WM831x BOOST driver: %d\n", ret);
  836. ret = platform_driver_register(&wm831x_epe_driver);
  837. if (ret != 0)
  838. pr_err("Failed to register WM831x EPE driver: %d\n", ret);
  839. return 0;
  840. }
  841. subsys_initcall(wm831x_dcdc_init);
  842. static void __exit wm831x_dcdc_exit(void)
  843. {
  844. platform_driver_unregister(&wm831x_epe_driver);
  845. platform_driver_unregister(&wm831x_boostp_driver);
  846. platform_driver_unregister(&wm831x_buckp_driver);
  847. platform_driver_unregister(&wm831x_buckv_driver);
  848. }
  849. module_exit(wm831x_dcdc_exit);
  850. /* Module information */
  851. MODULE_AUTHOR("Mark Brown");
  852. MODULE_DESCRIPTION("WM831x DC-DC convertor driver");
  853. MODULE_LICENSE("GPL");
  854. MODULE_ALIAS("platform:wm831x-buckv");
  855. MODULE_ALIAS("platform:wm831x-buckp");