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 | |
Axel Lin | b5fb77e | 2012-06-13 20:29:17 +0800 | [diff] [blame] | 218 | static int wm831x_buckv_map_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) |
Axel Lin | 1a67993 | 2012-11-30 13:19:33 +0800 | [diff] [blame] | 226 | vsel = DIV_ROUND_UP(min_uV - 600000, 12500) + 8; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 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 | |
Axel Lin | b5fb77e | 2012-06-13 20:29:17 +0800 | [diff] [blame] | 254 | static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev, |
| 255 | unsigned vsel) |
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; |
Axel Lin | b5fb77e | 2012-06-13 20:29:17 +0800 | [diff] [blame] | 261 | int ret; |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 262 | |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 263 | /* If this value is already set then do a GPIO update if we can */ |
| 264 | if (dcdc->dvs_gpio && dcdc->on_vsel == vsel) |
| 265 | return wm831x_buckv_set_dvs(rdev, 0); |
| 266 | |
| 267 | if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel) |
| 268 | return wm831x_buckv_set_dvs(rdev, 1); |
| 269 | |
| 270 | /* Always set the ON status to the minimum voltage */ |
| 271 | ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel); |
| 272 | if (ret < 0) |
| 273 | return ret; |
| 274 | dcdc->on_vsel = vsel; |
| 275 | |
| 276 | if (!dcdc->dvs_gpio) |
| 277 | return ret; |
| 278 | |
| 279 | /* Kick the voltage transition now */ |
| 280 | ret = wm831x_buckv_set_dvs(rdev, 0); |
| 281 | if (ret < 0) |
| 282 | return ret; |
| 283 | |
Mark Brown | 88cda60 | 2011-07-25 22:20:34 +0100 | [diff] [blame] | 284 | /* |
| 285 | * If this VSEL is higher than the last one we've seen then |
| 286 | * remember it as the DVS VSEL. This is optimised for CPUfreq |
| 287 | * usage where we want to get to the highest voltage very |
| 288 | * quickly. |
| 289 | */ |
| 290 | if (vsel > dcdc->dvs_vsel) { |
| 291 | ret = wm831x_set_bits(wm831x, dvs_reg, |
| 292 | WM831X_DC1_DVS_VSEL_MASK, |
Mark Brown | 13ae633 | 2012-11-20 10:02:06 +0900 | [diff] [blame] | 293 | vsel); |
Mark Brown | 88cda60 | 2011-07-25 22:20:34 +0100 | [diff] [blame] | 294 | if (ret == 0) |
| 295 | dcdc->dvs_vsel = vsel; |
| 296 | else |
| 297 | dev_warn(wm831x->dev, |
| 298 | "Failed to set DCDC DVS VSEL: %d\n", ret); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 301 | return 0; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev, |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 305 | int uV) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 306 | { |
| 307 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 308 | struct wm831x *wm831x = dcdc->wm831x; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 309 | u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 310 | int vsel; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 311 | |
Axel Lin | b5fb77e | 2012-06-13 20:29:17 +0800 | [diff] [blame] | 312 | vsel = wm831x_buckv_map_voltage(rdev, uV, uV); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 313 | if (vsel < 0) |
| 314 | return vsel; |
| 315 | |
| 316 | return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Mark Brown | afb8bb8 | 2010-12-10 17:28:08 +0000 | [diff] [blame] | 319 | static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 320 | { |
| 321 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 322 | |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 323 | if (dcdc->dvs_gpio && dcdc->dvs_gpio_state) |
Mark Brown | afb8bb8 | 2010-12-10 17:28:08 +0000 | [diff] [blame] | 324 | return dcdc->dvs_vsel; |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 325 | else |
Mark Brown | afb8bb8 | 2010-12-10 17:28:08 +0000 | [diff] [blame] | 326 | return dcdc->on_vsel; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* Current limit options */ |
| 330 | static u16 wm831x_dcdc_ilim[] = { |
| 331 | 125, 250, 375, 500, 625, 750, 875, 1000 |
| 332 | }; |
| 333 | |
| 334 | static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev, |
| 335 | int min_uA, int max_uA) |
| 336 | { |
| 337 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
| 338 | struct wm831x *wm831x = dcdc->wm831x; |
| 339 | u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; |
| 340 | int i; |
| 341 | |
Axel Lin | 422294d | 2012-08-08 20:20:29 +0800 | [diff] [blame] | 342 | for (i = ARRAY_SIZE(wm831x_dcdc_ilim) - 1; i >= 0; i--) { |
Axel Lin | a171e78 | 2012-03-27 15:17:26 +0800 | [diff] [blame] | 343 | if ((min_uA <= wm831x_dcdc_ilim[i]) && |
| 344 | (wm831x_dcdc_ilim[i] <= max_uA)) |
Axel Lin | 422294d | 2012-08-08 20:20:29 +0800 | [diff] [blame] | 345 | return wm831x_set_bits(wm831x, reg, |
| 346 | WM831X_DC1_HC_THR_MASK, |
| 347 | i << WM831X_DC1_HC_THR_SHIFT); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 348 | } |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 349 | |
Axel Lin | 422294d | 2012-08-08 20:20:29 +0800 | [diff] [blame] | 350 | return -EINVAL; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev) |
| 354 | { |
| 355 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
| 356 | struct wm831x *wm831x = dcdc->wm831x; |
| 357 | u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; |
| 358 | int val; |
| 359 | |
| 360 | val = wm831x_reg_read(wm831x, reg); |
| 361 | if (val < 0) |
| 362 | return val; |
| 363 | |
Axel Lin | 09bf14b | 2012-03-15 15:53:05 +0800 | [diff] [blame] | 364 | val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT; |
| 365 | return wm831x_dcdc_ilim[val]; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static struct regulator_ops wm831x_buckv_ops = { |
Axel Lin | b5fb77e | 2012-06-13 20:29:17 +0800 | [diff] [blame] | 369 | .set_voltage_sel = wm831x_buckv_set_voltage_sel, |
Mark Brown | afb8bb8 | 2010-12-10 17:28:08 +0000 | [diff] [blame] | 370 | .get_voltage_sel = wm831x_buckv_get_voltage_sel, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 371 | .list_voltage = wm831x_buckv_list_voltage, |
Axel Lin | b5fb77e | 2012-06-13 20:29:17 +0800 | [diff] [blame] | 372 | .map_voltage = wm831x_buckv_map_voltage, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 373 | .set_suspend_voltage = wm831x_buckv_set_suspend_voltage, |
| 374 | .set_current_limit = wm831x_buckv_set_current_limit, |
| 375 | .get_current_limit = wm831x_buckv_get_current_limit, |
| 376 | |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 377 | .is_enabled = regulator_is_enabled_regmap, |
| 378 | .enable = regulator_enable_regmap, |
| 379 | .disable = regulator_disable_regmap, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 380 | .get_status = wm831x_dcdc_get_status, |
| 381 | .get_mode = wm831x_dcdc_get_mode, |
| 382 | .set_mode = wm831x_dcdc_set_mode, |
| 383 | .set_suspend_mode = wm831x_dcdc_set_suspend_mode, |
| 384 | }; |
| 385 | |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 386 | /* |
| 387 | * Set up DVS control. We just log errors since we can still run |
| 388 | * (with reduced performance) if we fail. |
| 389 | */ |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 390 | static void wm831x_buckv_dvs_init(struct wm831x_dcdc *dcdc, |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 391 | struct wm831x_buckv_pdata *pdata) |
| 392 | { |
| 393 | struct wm831x *wm831x = dcdc->wm831x; |
| 394 | int ret; |
| 395 | u16 ctrl; |
| 396 | |
| 397 | if (!pdata || !pdata->dvs_gpio) |
| 398 | return; |
| 399 | |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 400 | /* gpiolib won't let us read the GPIO status so pick the higher |
| 401 | * of the two existing voltages so we take it as platform data. |
| 402 | */ |
| 403 | dcdc->dvs_gpio_state = pdata->dvs_init_state; |
| 404 | |
Mark Brown | 053fa1b | 2012-05-07 18:19:08 +0100 | [diff] [blame] | 405 | ret = gpio_request_one(pdata->dvs_gpio, |
| 406 | dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0, |
| 407 | "DCDC DVS"); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 408 | if (ret < 0) { |
Mark Brown | 053fa1b | 2012-05-07 18:19:08 +0100 | [diff] [blame] | 409 | dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n", |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 410 | dcdc->name, ret); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | |
| 414 | dcdc->dvs_gpio = pdata->dvs_gpio; |
Mark Brown | b47ba9fd | 2011-07-25 22:20:32 +0100 | [diff] [blame] | 415 | |
| 416 | switch (pdata->dvs_control_src) { |
| 417 | case 1: |
| 418 | ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT; |
| 419 | break; |
| 420 | case 2: |
| 421 | ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT; |
| 422 | break; |
| 423 | default: |
| 424 | dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n", |
| 425 | pdata->dvs_control_src, dcdc->name); |
| 426 | return; |
| 427 | } |
| 428 | |
Mark Brown | c439b8f | 2011-07-25 22:20:33 +0100 | [diff] [blame] | 429 | /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL |
| 430 | * to make bootstrapping a bit smoother. |
| 431 | */ |
| 432 | if (!dcdc->dvs_vsel) { |
| 433 | ret = wm831x_set_bits(wm831x, |
| 434 | dcdc->base + WM831X_DCDC_DVS_CONTROL, |
| 435 | WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel); |
| 436 | if (ret == 0) |
| 437 | dcdc->dvs_vsel = dcdc->on_vsel; |
| 438 | else |
| 439 | dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n", |
| 440 | ret); |
| 441 | } |
| 442 | |
Mark Brown | b47ba9fd | 2011-07-25 22:20:32 +0100 | [diff] [blame] | 443 | ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL, |
| 444 | WM831X_DC1_DVS_SRC_MASK, ctrl); |
| 445 | if (ret < 0) { |
| 446 | dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n", |
| 447 | dcdc->name, ret); |
| 448 | } |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 451 | static int wm831x_buckv_probe(struct platform_device *pdev) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 452 | { |
| 453 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 454 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 455 | struct regulator_config config = { }; |
Mark Brown | 137a635 | 2011-07-25 22:20:29 +0100 | [diff] [blame] | 456 | int id; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 457 | struct wm831x_dcdc *dcdc; |
| 458 | struct resource *res; |
| 459 | int ret, irq; |
| 460 | |
Mark Brown | 137a635 | 2011-07-25 22:20:29 +0100 | [diff] [blame] | 461 | if (pdata && pdata->wm831x_num) |
| 462 | id = (pdata->wm831x_num * 10) + 1; |
| 463 | else |
| 464 | id = 0; |
| 465 | id = pdev->id - id; |
| 466 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 467 | dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); |
| 468 | |
Mark Brown | fded2f4 | 2011-12-15 02:11:14 +0800 | [diff] [blame] | 469 | dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), |
| 470 | GFP_KERNEL); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 471 | if (dcdc == NULL) { |
| 472 | dev_err(&pdev->dev, "Unable to allocate private data\n"); |
| 473 | return -ENOMEM; |
| 474 | } |
| 475 | |
| 476 | dcdc->wm831x = wm831x; |
| 477 | |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 478 | res = platform_get_resource(pdev, IORESOURCE_REG, 0); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 479 | if (res == NULL) { |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 480 | dev_err(&pdev->dev, "No REG resource\n"); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 481 | ret = -EINVAL; |
| 482 | goto err; |
| 483 | } |
| 484 | dcdc->base = res->start; |
| 485 | |
| 486 | snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); |
| 487 | dcdc->desc.name = dcdc->name; |
Mark Brown | 82caa97 | 2012-03-28 21:40:42 +0100 | [diff] [blame] | 488 | |
| 489 | snprintf(dcdc->supply_name, sizeof(dcdc->supply_name), |
| 490 | "DC%dVDD", id + 1); |
| 491 | dcdc->desc.supply_name = dcdc->supply_name; |
| 492 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 493 | dcdc->desc.id = id; |
| 494 | dcdc->desc.type = REGULATOR_VOLTAGE; |
| 495 | dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1; |
| 496 | dcdc->desc.ops = &wm831x_buckv_ops; |
| 497 | dcdc->desc.owner = THIS_MODULE; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 498 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
| 499 | dcdc->desc.enable_mask = 1 << id; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 500 | |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 501 | ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG); |
| 502 | if (ret < 0) { |
| 503 | dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret); |
| 504 | goto err; |
| 505 | } |
| 506 | dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK; |
| 507 | |
Mark Brown | a1b81dd | 2011-07-25 22:20:30 +0100 | [diff] [blame] | 508 | ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 509 | if (ret < 0) { |
| 510 | dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret); |
| 511 | goto err; |
| 512 | } |
| 513 | dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK; |
| 514 | |
Mark Brown | f0b067d | 2012-05-14 11:37:09 +0100 | [diff] [blame] | 515 | if (pdata && pdata->dcdc[id]) |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 516 | wm831x_buckv_dvs_init(dcdc, pdata->dcdc[id]->driver_data); |
| 517 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 518 | config.dev = pdev->dev.parent; |
Mark Brown | b7ca878 | 2012-05-10 00:41:02 +0100 | [diff] [blame] | 519 | if (pdata) |
| 520 | config.init_data = pdata->dcdc[id]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 521 | config.driver_data = dcdc; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 522 | config.regmap = wm831x->regmap; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 523 | |
| 524 | dcdc->regulator = regulator_register(&dcdc->desc, &config); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 525 | if (IS_ERR(dcdc->regulator)) { |
| 526 | ret = PTR_ERR(dcdc->regulator); |
| 527 | dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", |
| 528 | id + 1, ret); |
| 529 | goto err; |
| 530 | } |
| 531 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 532 | irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); |
Mark Brown | dfda9c2 | 2011-03-01 16:50:43 +0000 | [diff] [blame] | 533 | ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq, |
| 534 | IRQF_TRIGGER_RISING, dcdc->name, dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 535 | if (ret != 0) { |
| 536 | dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", |
| 537 | irq, ret); |
| 538 | goto err_regulator; |
| 539 | } |
| 540 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 541 | irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC")); |
Mark Brown | dfda9c2 | 2011-03-01 16:50:43 +0000 | [diff] [blame] | 542 | ret = request_threaded_irq(irq, NULL, wm831x_dcdc_oc_irq, |
| 543 | IRQF_TRIGGER_RISING, dcdc->name, dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 544 | if (ret != 0) { |
| 545 | dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n", |
| 546 | irq, ret); |
| 547 | goto err_uv; |
| 548 | } |
| 549 | |
| 550 | platform_set_drvdata(pdev, dcdc); |
| 551 | |
| 552 | return 0; |
| 553 | |
| 554 | err_uv: |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 555 | free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")), |
| 556 | dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 557 | err_regulator: |
| 558 | regulator_unregister(dcdc->regulator); |
| 559 | err: |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 560 | if (dcdc->dvs_gpio) |
| 561 | gpio_free(dcdc->dvs_gpio); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 562 | return ret; |
| 563 | } |
| 564 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 565 | static int wm831x_buckv_remove(struct platform_device *pdev) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 566 | { |
| 567 | struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 568 | struct wm831x *wm831x = dcdc->wm831x; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 569 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 570 | free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC")), |
| 571 | dcdc); |
| 572 | free_irq(wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")), |
| 573 | dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 574 | regulator_unregister(dcdc->regulator); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 575 | if (dcdc->dvs_gpio) |
| 576 | gpio_free(dcdc->dvs_gpio); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static struct platform_driver wm831x_buckv_driver = { |
| 582 | .probe = wm831x_buckv_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 583 | .remove = wm831x_buckv_remove, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 584 | .driver = { |
| 585 | .name = "wm831x-buckv", |
Dmitry Torokhov | eb66d56 | 2010-02-23 23:38:39 -0800 | [diff] [blame] | 586 | .owner = THIS_MODULE, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 587 | }, |
| 588 | }; |
| 589 | |
| 590 | /* |
| 591 | * BUCKP specifics |
| 592 | */ |
| 593 | |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 594 | static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 595 | { |
| 596 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
| 597 | struct wm831x *wm831x = dcdc->wm831x; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 598 | u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 599 | int sel; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 600 | |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 601 | sel = regulator_map_voltage_linear(rdev, uV, uV); |
| 602 | if (sel < 0) |
| 603 | return sel; |
| 604 | |
| 605 | return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 606 | } |
| 607 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 608 | static struct regulator_ops wm831x_buckp_ops = { |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 609 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 817436e | 2012-04-15 11:55:34 +0100 | [diff] [blame] | 610 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
Axel Lin | c70ad9d | 2012-06-13 20:27:14 +0800 | [diff] [blame] | 611 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 612 | .map_voltage = regulator_map_voltage_linear, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 613 | .set_suspend_voltage = wm831x_buckp_set_suspend_voltage, |
| 614 | |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 615 | .is_enabled = regulator_is_enabled_regmap, |
| 616 | .enable = regulator_enable_regmap, |
| 617 | .disable = regulator_disable_regmap, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 618 | .get_status = wm831x_dcdc_get_status, |
| 619 | .get_mode = wm831x_dcdc_get_mode, |
| 620 | .set_mode = wm831x_dcdc_set_mode, |
| 621 | .set_suspend_mode = wm831x_dcdc_set_suspend_mode, |
| 622 | }; |
| 623 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 624 | static int wm831x_buckp_probe(struct platform_device *pdev) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 625 | { |
| 626 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 627 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 628 | struct regulator_config config = { }; |
Mark Brown | 137a635 | 2011-07-25 22:20:29 +0100 | [diff] [blame] | 629 | int id; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 630 | struct wm831x_dcdc *dcdc; |
| 631 | struct resource *res; |
| 632 | int ret, irq; |
| 633 | |
Mark Brown | 137a635 | 2011-07-25 22:20:29 +0100 | [diff] [blame] | 634 | if (pdata && pdata->wm831x_num) |
| 635 | id = (pdata->wm831x_num * 10) + 1; |
| 636 | else |
| 637 | id = 0; |
| 638 | id = pdev->id - id; |
| 639 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 640 | dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); |
| 641 | |
Mark Brown | fded2f4 | 2011-12-15 02:11:14 +0800 | [diff] [blame] | 642 | dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), |
| 643 | GFP_KERNEL); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 644 | if (dcdc == NULL) { |
| 645 | dev_err(&pdev->dev, "Unable to allocate private data\n"); |
| 646 | return -ENOMEM; |
| 647 | } |
| 648 | |
| 649 | dcdc->wm831x = wm831x; |
| 650 | |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 651 | res = platform_get_resource(pdev, IORESOURCE_REG, 0); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 652 | if (res == NULL) { |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 653 | dev_err(&pdev->dev, "No REG resource\n"); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 654 | ret = -EINVAL; |
| 655 | goto err; |
| 656 | } |
| 657 | dcdc->base = res->start; |
| 658 | |
| 659 | snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); |
| 660 | dcdc->desc.name = dcdc->name; |
Mark Brown | 82caa97 | 2012-03-28 21:40:42 +0100 | [diff] [blame] | 661 | |
| 662 | snprintf(dcdc->supply_name, sizeof(dcdc->supply_name), |
| 663 | "DC%dVDD", id + 1); |
| 664 | dcdc->desc.supply_name = dcdc->supply_name; |
| 665 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 666 | dcdc->desc.id = id; |
| 667 | dcdc->desc.type = REGULATOR_VOLTAGE; |
| 668 | dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1; |
| 669 | dcdc->desc.ops = &wm831x_buckp_ops; |
| 670 | dcdc->desc.owner = THIS_MODULE; |
Mark Brown | 817436e | 2012-04-15 11:55:34 +0100 | [diff] [blame] | 671 | dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG; |
| 672 | dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 673 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
| 674 | dcdc->desc.enable_mask = 1 << id; |
Axel Lin | c70ad9d | 2012-06-13 20:27:14 +0800 | [diff] [blame] | 675 | dcdc->desc.min_uV = 850000; |
| 676 | dcdc->desc.uV_step = 25000; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 677 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 678 | config.dev = pdev->dev.parent; |
Mark Brown | b7ca878 | 2012-05-10 00:41:02 +0100 | [diff] [blame] | 679 | if (pdata) |
| 680 | config.init_data = pdata->dcdc[id]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 681 | config.driver_data = dcdc; |
Mark Brown | 817436e | 2012-04-15 11:55:34 +0100 | [diff] [blame] | 682 | config.regmap = wm831x->regmap; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 683 | |
| 684 | dcdc->regulator = regulator_register(&dcdc->desc, &config); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 685 | if (IS_ERR(dcdc->regulator)) { |
| 686 | ret = PTR_ERR(dcdc->regulator); |
| 687 | dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", |
| 688 | id + 1, ret); |
| 689 | goto err; |
| 690 | } |
| 691 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 692 | irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); |
Mark Brown | dfda9c2 | 2011-03-01 16:50:43 +0000 | [diff] [blame] | 693 | ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq, |
| 694 | IRQF_TRIGGER_RISING, dcdc->name, dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 695 | if (ret != 0) { |
| 696 | dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", |
| 697 | irq, ret); |
| 698 | goto err_regulator; |
| 699 | } |
| 700 | |
| 701 | platform_set_drvdata(pdev, dcdc); |
| 702 | |
| 703 | return 0; |
| 704 | |
| 705 | err_regulator: |
| 706 | regulator_unregister(dcdc->regulator); |
| 707 | err: |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 708 | return ret; |
| 709 | } |
| 710 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 711 | static int wm831x_buckp_remove(struct platform_device *pdev) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 712 | { |
| 713 | struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 714 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 715 | free_irq(wm831x_irq(dcdc->wm831x, platform_get_irq_byname(pdev, "UV")), |
| 716 | dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 717 | regulator_unregister(dcdc->regulator); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static struct platform_driver wm831x_buckp_driver = { |
| 723 | .probe = wm831x_buckp_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 724 | .remove = wm831x_buckp_remove, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 725 | .driver = { |
| 726 | .name = "wm831x-buckp", |
Dmitry Torokhov | eb66d56 | 2010-02-23 23:38:39 -0800 | [diff] [blame] | 727 | .owner = THIS_MODULE, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 728 | }, |
| 729 | }; |
| 730 | |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 731 | /* |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 732 | * DCDC boost convertors |
| 733 | */ |
| 734 | |
| 735 | static int wm831x_boostp_get_status(struct regulator_dev *rdev) |
| 736 | { |
| 737 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
| 738 | struct wm831x *wm831x = dcdc->wm831x; |
| 739 | int ret; |
| 740 | |
| 741 | /* First, check for errors */ |
| 742 | ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS); |
| 743 | if (ret < 0) |
| 744 | return ret; |
| 745 | |
| 746 | if (ret & (1 << rdev_get_id(rdev))) { |
| 747 | dev_dbg(wm831x->dev, "DCDC%d under voltage\n", |
| 748 | rdev_get_id(rdev) + 1); |
| 749 | return REGULATOR_STATUS_ERROR; |
| 750 | } |
| 751 | |
| 752 | /* Is the regulator on? */ |
| 753 | ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS); |
| 754 | if (ret < 0) |
| 755 | return ret; |
| 756 | if (ret & (1 << rdev_get_id(rdev))) |
| 757 | return REGULATOR_STATUS_ON; |
| 758 | else |
| 759 | return REGULATOR_STATUS_OFF; |
| 760 | } |
| 761 | |
| 762 | static struct regulator_ops wm831x_boostp_ops = { |
| 763 | .get_status = wm831x_boostp_get_status, |
| 764 | |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 765 | .is_enabled = regulator_is_enabled_regmap, |
| 766 | .enable = regulator_enable_regmap, |
| 767 | .disable = regulator_disable_regmap, |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 768 | }; |
| 769 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 770 | static int wm831x_boostp_probe(struct platform_device *pdev) |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 771 | { |
| 772 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 773 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 774 | struct regulator_config config = { }; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 775 | int id = pdev->id % ARRAY_SIZE(pdata->dcdc); |
| 776 | struct wm831x_dcdc *dcdc; |
| 777 | struct resource *res; |
| 778 | int ret, irq; |
| 779 | |
| 780 | dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); |
| 781 | |
| 782 | if (pdata == NULL || pdata->dcdc[id] == NULL) |
| 783 | return -ENODEV; |
| 784 | |
Axel Lin | 4c60165 | 2012-03-23 17:00:01 +0800 | [diff] [blame] | 785 | dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 786 | if (dcdc == NULL) { |
| 787 | dev_err(&pdev->dev, "Unable to allocate private data\n"); |
| 788 | return -ENOMEM; |
| 789 | } |
| 790 | |
| 791 | dcdc->wm831x = wm831x; |
| 792 | |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 793 | res = platform_get_resource(pdev, IORESOURCE_REG, 0); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 794 | if (res == NULL) { |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 795 | dev_err(&pdev->dev, "No REG resource\n"); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 796 | ret = -EINVAL; |
| 797 | goto err; |
| 798 | } |
| 799 | dcdc->base = res->start; |
| 800 | |
| 801 | snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); |
| 802 | dcdc->desc.name = dcdc->name; |
| 803 | dcdc->desc.id = id; |
| 804 | dcdc->desc.type = REGULATOR_VOLTAGE; |
| 805 | dcdc->desc.ops = &wm831x_boostp_ops; |
| 806 | dcdc->desc.owner = THIS_MODULE; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 807 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
| 808 | dcdc->desc.enable_mask = 1 << id; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 809 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 810 | config.dev = pdev->dev.parent; |
Mark Brown | f0b067d | 2012-05-14 11:37:09 +0100 | [diff] [blame] | 811 | if (pdata) |
| 812 | config.init_data = pdata->dcdc[id]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 813 | config.driver_data = dcdc; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 814 | config.regmap = wm831x->regmap; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 815 | |
| 816 | dcdc->regulator = regulator_register(&dcdc->desc, &config); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 817 | if (IS_ERR(dcdc->regulator)) { |
| 818 | ret = PTR_ERR(dcdc->regulator); |
| 819 | dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", |
| 820 | id + 1, ret); |
| 821 | goto err; |
| 822 | } |
| 823 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 824 | irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); |
Mark Brown | dfda9c2 | 2011-03-01 16:50:43 +0000 | [diff] [blame] | 825 | ret = request_threaded_irq(irq, NULL, wm831x_dcdc_uv_irq, |
| 826 | IRQF_TRIGGER_RISING, dcdc->name, |
| 827 | dcdc); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 828 | if (ret != 0) { |
| 829 | dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", |
| 830 | irq, ret); |
| 831 | goto err_regulator; |
| 832 | } |
| 833 | |
| 834 | platform_set_drvdata(pdev, dcdc); |
| 835 | |
| 836 | return 0; |
| 837 | |
| 838 | err_regulator: |
| 839 | regulator_unregister(dcdc->regulator); |
| 840 | err: |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 841 | return ret; |
| 842 | } |
| 843 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 844 | static int wm831x_boostp_remove(struct platform_device *pdev) |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 845 | { |
| 846 | struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 847 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 848 | free_irq(wm831x_irq(dcdc->wm831x, platform_get_irq_byname(pdev, "UV")), |
| 849 | dcdc); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 850 | regulator_unregister(dcdc->regulator); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static struct platform_driver wm831x_boostp_driver = { |
| 856 | .probe = wm831x_boostp_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 857 | .remove = wm831x_boostp_remove, |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 858 | .driver = { |
| 859 | .name = "wm831x-boostp", |
Dmitry Torokhov | eb66d56 | 2010-02-23 23:38:39 -0800 | [diff] [blame] | 860 | .owner = THIS_MODULE, |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 861 | }, |
| 862 | }; |
| 863 | |
| 864 | /* |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 865 | * External Power Enable |
| 866 | * |
| 867 | * These aren't actually DCDCs but look like them in hardware so share |
| 868 | * code. |
| 869 | */ |
| 870 | |
| 871 | #define WM831X_EPE_BASE 6 |
| 872 | |
| 873 | static struct regulator_ops wm831x_epe_ops = { |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 874 | .is_enabled = regulator_is_enabled_regmap, |
| 875 | .enable = regulator_enable_regmap, |
| 876 | .disable = regulator_disable_regmap, |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 877 | .get_status = wm831x_dcdc_get_status, |
| 878 | }; |
| 879 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 880 | static int wm831x_epe_probe(struct platform_device *pdev) |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 881 | { |
| 882 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 883 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 884 | struct regulator_config config = { }; |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 885 | int id = pdev->id % ARRAY_SIZE(pdata->epe); |
| 886 | struct wm831x_dcdc *dcdc; |
| 887 | int ret; |
| 888 | |
| 889 | dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1); |
| 890 | |
Axel Lin | 4c60165 | 2012-03-23 17:00:01 +0800 | [diff] [blame] | 891 | dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL); |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 892 | if (dcdc == NULL) { |
| 893 | dev_err(&pdev->dev, "Unable to allocate private data\n"); |
| 894 | return -ENOMEM; |
| 895 | } |
| 896 | |
| 897 | dcdc->wm831x = wm831x; |
| 898 | |
| 899 | /* For current parts this is correct; probably need to revisit |
| 900 | * in future. |
| 901 | */ |
| 902 | snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1); |
| 903 | dcdc->desc.name = dcdc->name; |
| 904 | dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */ |
| 905 | dcdc->desc.ops = &wm831x_epe_ops; |
| 906 | dcdc->desc.type = REGULATOR_VOLTAGE; |
| 907 | dcdc->desc.owner = THIS_MODULE; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 908 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
| 909 | dcdc->desc.enable_mask = 1 << dcdc->desc.id; |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 910 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 911 | config.dev = pdev->dev.parent; |
Mark Brown | b7ca878 | 2012-05-10 00:41:02 +0100 | [diff] [blame] | 912 | if (pdata) |
| 913 | config.init_data = pdata->epe[id]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 914 | config.driver_data = dcdc; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 915 | config.regmap = wm831x->regmap; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 916 | |
| 917 | dcdc->regulator = regulator_register(&dcdc->desc, &config); |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 918 | if (IS_ERR(dcdc->regulator)) { |
| 919 | ret = PTR_ERR(dcdc->regulator); |
| 920 | dev_err(wm831x->dev, "Failed to register EPE%d: %d\n", |
| 921 | id + 1, ret); |
| 922 | goto err; |
| 923 | } |
| 924 | |
| 925 | platform_set_drvdata(pdev, dcdc); |
| 926 | |
| 927 | return 0; |
| 928 | |
| 929 | err: |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 930 | return ret; |
| 931 | } |
| 932 | |
Bill Pemberton | 8dc995f | 2012-11-19 13:26:10 -0500 | [diff] [blame] | 933 | static int wm831x_epe_remove(struct platform_device *pdev) |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 934 | { |
| 935 | struct wm831x_dcdc *dcdc = platform_get_drvdata(pdev); |
| 936 | |
| 937 | regulator_unregister(dcdc->regulator); |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | static struct platform_driver wm831x_epe_driver = { |
| 943 | .probe = wm831x_epe_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame] | 944 | .remove = wm831x_epe_remove, |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 945 | .driver = { |
| 946 | .name = "wm831x-epe", |
Dmitry Torokhov | eb66d56 | 2010-02-23 23:38:39 -0800 | [diff] [blame] | 947 | .owner = THIS_MODULE, |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 948 | }, |
| 949 | }; |
| 950 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 951 | static int __init wm831x_dcdc_init(void) |
| 952 | { |
| 953 | int ret; |
| 954 | ret = platform_driver_register(&wm831x_buckv_driver); |
| 955 | if (ret != 0) |
| 956 | pr_err("Failed to register WM831x BUCKV driver: %d\n", ret); |
| 957 | |
| 958 | ret = platform_driver_register(&wm831x_buckp_driver); |
| 959 | if (ret != 0) |
| 960 | pr_err("Failed to register WM831x BUCKP driver: %d\n", ret); |
| 961 | |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 962 | ret = platform_driver_register(&wm831x_boostp_driver); |
| 963 | if (ret != 0) |
| 964 | pr_err("Failed to register WM831x BOOST driver: %d\n", ret); |
| 965 | |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 966 | ret = platform_driver_register(&wm831x_epe_driver); |
| 967 | if (ret != 0) |
| 968 | pr_err("Failed to register WM831x EPE driver: %d\n", ret); |
| 969 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 970 | return 0; |
| 971 | } |
| 972 | subsys_initcall(wm831x_dcdc_init); |
| 973 | |
| 974 | static void __exit wm831x_dcdc_exit(void) |
| 975 | { |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 976 | platform_driver_unregister(&wm831x_epe_driver); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 977 | platform_driver_unregister(&wm831x_boostp_driver); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 978 | platform_driver_unregister(&wm831x_buckp_driver); |
| 979 | platform_driver_unregister(&wm831x_buckv_driver); |
| 980 | } |
| 981 | module_exit(wm831x_dcdc_exit); |
| 982 | |
| 983 | /* Module information */ |
| 984 | MODULE_AUTHOR("Mark Brown"); |
| 985 | MODULE_DESCRIPTION("WM831x DC-DC convertor driver"); |
| 986 | MODULE_LICENSE("GPL"); |
| 987 | MODULE_ALIAS("platform:wm831x-buckv"); |
| 988 | MODULE_ALIAS("platform:wm831x-buckp"); |
Axel Lin | 43f1f21 | 2012-11-30 20:39:53 +0800 | [diff] [blame] | 989 | MODULE_ALIAS("platform:wm831x-boostp"); |
Mark Brown | 24b4315 | 2011-07-25 22:20:31 +0100 | [diff] [blame] | 990 | MODULE_ALIAS("platform:wm831x-epe"); |