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