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