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 | */ |
Mark Brown | 1aa20d2 | 2013-08-31 11:42:46 +0100 | [diff] [blame] | 390 | static void wm831x_buckv_dvs_init(struct platform_device *pdev, |
| 391 | struct wm831x_dcdc *dcdc, |
| 392 | struct wm831x_buckv_pdata *pdata) |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 393 | { |
| 394 | struct wm831x *wm831x = dcdc->wm831x; |
| 395 | int ret; |
| 396 | u16 ctrl; |
| 397 | |
| 398 | if (!pdata || !pdata->dvs_gpio) |
| 399 | return; |
| 400 | |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 401 | /* gpiolib won't let us read the GPIO status so pick the higher |
| 402 | * of the two existing voltages so we take it as platform data. |
| 403 | */ |
| 404 | dcdc->dvs_gpio_state = pdata->dvs_init_state; |
| 405 | |
Mark Brown | 1aa20d2 | 2013-08-31 11:42:46 +0100 | [diff] [blame] | 406 | ret = devm_gpio_request_one(&pdev->dev, pdata->dvs_gpio, |
| 407 | dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0, |
| 408 | "DCDC DVS"); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 409 | if (ret < 0) { |
Mark Brown | 053fa1b | 2012-05-07 18:19:08 +0100 | [diff] [blame] | 410 | dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n", |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 411 | dcdc->name, ret); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | |
| 415 | dcdc->dvs_gpio = pdata->dvs_gpio; |
Mark Brown | b47ba9fd | 2011-07-25 22:20:32 +0100 | [diff] [blame] | 416 | |
| 417 | switch (pdata->dvs_control_src) { |
| 418 | case 1: |
| 419 | ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT; |
| 420 | break; |
| 421 | case 2: |
| 422 | ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT; |
| 423 | break; |
| 424 | default: |
| 425 | dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n", |
| 426 | pdata->dvs_control_src, dcdc->name); |
| 427 | return; |
| 428 | } |
| 429 | |
Mark Brown | c439b8f | 2011-07-25 22:20:33 +0100 | [diff] [blame] | 430 | /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL |
| 431 | * to make bootstrapping a bit smoother. |
| 432 | */ |
| 433 | if (!dcdc->dvs_vsel) { |
| 434 | ret = wm831x_set_bits(wm831x, |
| 435 | dcdc->base + WM831X_DCDC_DVS_CONTROL, |
| 436 | WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel); |
| 437 | if (ret == 0) |
| 438 | dcdc->dvs_vsel = dcdc->on_vsel; |
| 439 | else |
| 440 | dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n", |
| 441 | ret); |
| 442 | } |
| 443 | |
Mark Brown | b47ba9fd | 2011-07-25 22:20:32 +0100 | [diff] [blame] | 444 | ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL, |
| 445 | WM831X_DC1_DVS_SRC_MASK, ctrl); |
| 446 | if (ret < 0) { |
| 447 | dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n", |
| 448 | dcdc->name, ret); |
| 449 | } |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 452 | static int wm831x_buckv_probe(struct platform_device *pdev) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 453 | { |
| 454 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 455 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 456 | struct regulator_config config = { }; |
Mark Brown | 137a635 | 2011-07-25 22:20:29 +0100 | [diff] [blame] | 457 | int id; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 458 | struct wm831x_dcdc *dcdc; |
| 459 | struct resource *res; |
| 460 | int ret, irq; |
| 461 | |
Mark Brown | 137a635 | 2011-07-25 22:20:29 +0100 | [diff] [blame] | 462 | if (pdata && pdata->wm831x_num) |
| 463 | id = (pdata->wm831x_num * 10) + 1; |
| 464 | else |
| 465 | id = 0; |
| 466 | id = pdev->id - id; |
| 467 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 468 | dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); |
| 469 | |
Mark Brown | fded2f4 | 2011-12-15 02:11:14 +0800 | [diff] [blame] | 470 | dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), |
| 471 | GFP_KERNEL); |
Sachin Kamat | 5730aa5 | 2014-02-20 14:23:20 +0530 | [diff] [blame] | 472 | if (!dcdc) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 473 | return -ENOMEM; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 474 | |
| 475 | dcdc->wm831x = wm831x; |
| 476 | |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 477 | res = platform_get_resource(pdev, IORESOURCE_REG, 0); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 478 | if (res == NULL) { |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 479 | dev_err(&pdev->dev, "No REG resource\n"); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 480 | ret = -EINVAL; |
| 481 | goto err; |
| 482 | } |
| 483 | dcdc->base = res->start; |
| 484 | |
| 485 | snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); |
| 486 | dcdc->desc.name = dcdc->name; |
Mark Brown | 82caa97 | 2012-03-28 21:40:42 +0100 | [diff] [blame] | 487 | |
| 488 | snprintf(dcdc->supply_name, sizeof(dcdc->supply_name), |
| 489 | "DC%dVDD", id + 1); |
| 490 | dcdc->desc.supply_name = dcdc->supply_name; |
| 491 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 492 | dcdc->desc.id = id; |
| 493 | dcdc->desc.type = REGULATOR_VOLTAGE; |
| 494 | dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1; |
| 495 | dcdc->desc.ops = &wm831x_buckv_ops; |
| 496 | dcdc->desc.owner = THIS_MODULE; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 497 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
| 498 | dcdc->desc.enable_mask = 1 << id; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 499 | |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 500 | ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG); |
| 501 | if (ret < 0) { |
| 502 | dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret); |
| 503 | goto err; |
| 504 | } |
| 505 | dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK; |
| 506 | |
Mark Brown | a1b81dd | 2011-07-25 22:20:30 +0100 | [diff] [blame] | 507 | ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 508 | if (ret < 0) { |
| 509 | dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret); |
| 510 | goto err; |
| 511 | } |
| 512 | dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK; |
| 513 | |
Mark Brown | f0b067d | 2012-05-14 11:37:09 +0100 | [diff] [blame] | 514 | if (pdata && pdata->dcdc[id]) |
Mark Brown | 1aa20d2 | 2013-08-31 11:42:46 +0100 | [diff] [blame] | 515 | wm831x_buckv_dvs_init(pdev, dcdc, |
| 516 | pdata->dcdc[id]->driver_data); |
Mark Brown | e24a04c | 2009-09-22 08:47:11 -0700 | [diff] [blame] | 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 | |
Mark Brown | d73b4cb | 2013-08-31 11:58:40 +0100 | [diff] [blame] | 524 | dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc, |
| 525 | &config); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 526 | if (IS_ERR(dcdc->regulator)) { |
| 527 | ret = PTR_ERR(dcdc->regulator); |
| 528 | dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", |
| 529 | id + 1, ret); |
| 530 | goto err; |
| 531 | } |
| 532 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 533 | irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); |
Mark Brown | b0c4c0c | 2013-08-31 11:47:39 +0100 | [diff] [blame] | 534 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 535 | wm831x_dcdc_uv_irq, |
Fabio Estevam | 2945473 | 2015-06-01 22:33:53 -0300 | [diff] [blame] | 536 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 537 | dcdc->name, dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 538 | if (ret != 0) { |
| 539 | dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", |
| 540 | irq, ret); |
Mark Brown | d73b4cb | 2013-08-31 11:58:40 +0100 | [diff] [blame] | 541 | goto err; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 542 | } |
| 543 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 544 | irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC")); |
Mark Brown | b0c4c0c | 2013-08-31 11:47:39 +0100 | [diff] [blame] | 545 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 546 | wm831x_dcdc_oc_irq, |
Fabio Estevam | 2945473 | 2015-06-01 22:33:53 -0300 | [diff] [blame] | 547 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 548 | dcdc->name, dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 549 | if (ret != 0) { |
| 550 | dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n", |
| 551 | irq, ret); |
Mark Brown | d73b4cb | 2013-08-31 11:58:40 +0100 | [diff] [blame] | 552 | goto err; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | platform_set_drvdata(pdev, dcdc); |
| 556 | |
| 557 | return 0; |
| 558 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 559 | err: |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 560 | return ret; |
| 561 | } |
| 562 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 563 | static struct platform_driver wm831x_buckv_driver = { |
| 564 | .probe = wm831x_buckv_probe, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 565 | .driver = { |
| 566 | .name = "wm831x-buckv", |
| 567 | }, |
| 568 | }; |
| 569 | |
| 570 | /* |
| 571 | * BUCKP specifics |
| 572 | */ |
| 573 | |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 574 | 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] | 575 | { |
| 576 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
| 577 | struct wm831x *wm831x = dcdc->wm831x; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 578 | u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 579 | int sel; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 580 | |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 581 | sel = regulator_map_voltage_linear(rdev, uV, uV); |
| 582 | if (sel < 0) |
| 583 | return sel; |
| 584 | |
| 585 | return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 588 | static struct regulator_ops wm831x_buckp_ops = { |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 589 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 817436e | 2012-04-15 11:55:34 +0100 | [diff] [blame] | 590 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
Axel Lin | c70ad9d | 2012-06-13 20:27:14 +0800 | [diff] [blame] | 591 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | d580cb5 | 2012-06-13 20:27:58 +0800 | [diff] [blame] | 592 | .map_voltage = regulator_map_voltage_linear, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 593 | .set_suspend_voltage = wm831x_buckp_set_suspend_voltage, |
| 594 | |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 595 | .is_enabled = regulator_is_enabled_regmap, |
| 596 | .enable = regulator_enable_regmap, |
| 597 | .disable = regulator_disable_regmap, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 598 | .get_status = wm831x_dcdc_get_status, |
| 599 | .get_mode = wm831x_dcdc_get_mode, |
| 600 | .set_mode = wm831x_dcdc_set_mode, |
| 601 | .set_suspend_mode = wm831x_dcdc_set_suspend_mode, |
| 602 | }; |
| 603 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 604 | static int wm831x_buckp_probe(struct platform_device *pdev) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 605 | { |
| 606 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 607 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 608 | struct regulator_config config = { }; |
Mark Brown | 137a635 | 2011-07-25 22:20:29 +0100 | [diff] [blame] | 609 | int id; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 610 | struct wm831x_dcdc *dcdc; |
| 611 | struct resource *res; |
| 612 | int ret, irq; |
| 613 | |
Mark Brown | 137a635 | 2011-07-25 22:20:29 +0100 | [diff] [blame] | 614 | if (pdata && pdata->wm831x_num) |
| 615 | id = (pdata->wm831x_num * 10) + 1; |
| 616 | else |
| 617 | id = 0; |
| 618 | id = pdev->id - id; |
| 619 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 620 | dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); |
| 621 | |
Mark Brown | fded2f4 | 2011-12-15 02:11:14 +0800 | [diff] [blame] | 622 | dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), |
| 623 | GFP_KERNEL); |
Sachin Kamat | 5730aa5 | 2014-02-20 14:23:20 +0530 | [diff] [blame] | 624 | if (!dcdc) |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 625 | return -ENOMEM; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 626 | |
| 627 | dcdc->wm831x = wm831x; |
| 628 | |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 629 | res = platform_get_resource(pdev, IORESOURCE_REG, 0); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 630 | if (res == NULL) { |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 631 | dev_err(&pdev->dev, "No REG resource\n"); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 632 | ret = -EINVAL; |
| 633 | goto err; |
| 634 | } |
| 635 | dcdc->base = res->start; |
| 636 | |
| 637 | snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); |
| 638 | dcdc->desc.name = dcdc->name; |
Mark Brown | 82caa97 | 2012-03-28 21:40:42 +0100 | [diff] [blame] | 639 | |
| 640 | snprintf(dcdc->supply_name, sizeof(dcdc->supply_name), |
| 641 | "DC%dVDD", id + 1); |
| 642 | dcdc->desc.supply_name = dcdc->supply_name; |
| 643 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 644 | dcdc->desc.id = id; |
| 645 | dcdc->desc.type = REGULATOR_VOLTAGE; |
| 646 | dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1; |
| 647 | dcdc->desc.ops = &wm831x_buckp_ops; |
| 648 | dcdc->desc.owner = THIS_MODULE; |
Mark Brown | 817436e | 2012-04-15 11:55:34 +0100 | [diff] [blame] | 649 | dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG; |
| 650 | dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 651 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
| 652 | dcdc->desc.enable_mask = 1 << id; |
Axel Lin | c70ad9d | 2012-06-13 20:27:14 +0800 | [diff] [blame] | 653 | dcdc->desc.min_uV = 850000; |
| 654 | dcdc->desc.uV_step = 25000; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 655 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 656 | config.dev = pdev->dev.parent; |
Mark Brown | b7ca878 | 2012-05-10 00:41:02 +0100 | [diff] [blame] | 657 | if (pdata) |
| 658 | config.init_data = pdata->dcdc[id]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 659 | config.driver_data = dcdc; |
Mark Brown | 817436e | 2012-04-15 11:55:34 +0100 | [diff] [blame] | 660 | config.regmap = wm831x->regmap; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 661 | |
Mark Brown | d73b4cb | 2013-08-31 11:58:40 +0100 | [diff] [blame] | 662 | dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc, |
| 663 | &config); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 664 | if (IS_ERR(dcdc->regulator)) { |
| 665 | ret = PTR_ERR(dcdc->regulator); |
| 666 | dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", |
| 667 | id + 1, ret); |
| 668 | goto err; |
| 669 | } |
| 670 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 671 | irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); |
Mark Brown | b0c4c0c | 2013-08-31 11:47:39 +0100 | [diff] [blame] | 672 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 673 | wm831x_dcdc_uv_irq, |
Fabio Estevam | 2945473 | 2015-06-01 22:33:53 -0300 | [diff] [blame] | 674 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 675 | dcdc->name, dcdc); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 676 | if (ret != 0) { |
| 677 | dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", |
| 678 | irq, ret); |
Mark Brown | d73b4cb | 2013-08-31 11:58:40 +0100 | [diff] [blame] | 679 | goto err; |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | platform_set_drvdata(pdev, dcdc); |
| 683 | |
| 684 | return 0; |
| 685 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 686 | err: |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 687 | return ret; |
| 688 | } |
| 689 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 690 | static struct platform_driver wm831x_buckp_driver = { |
| 691 | .probe = wm831x_buckp_probe, |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 692 | .driver = { |
| 693 | .name = "wm831x-buckp", |
| 694 | }, |
| 695 | }; |
| 696 | |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 697 | /* |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 698 | * DCDC boost convertors |
| 699 | */ |
| 700 | |
| 701 | static int wm831x_boostp_get_status(struct regulator_dev *rdev) |
| 702 | { |
| 703 | struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); |
| 704 | struct wm831x *wm831x = dcdc->wm831x; |
| 705 | int ret; |
| 706 | |
| 707 | /* First, check for errors */ |
| 708 | ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS); |
| 709 | if (ret < 0) |
| 710 | return ret; |
| 711 | |
| 712 | if (ret & (1 << rdev_get_id(rdev))) { |
| 713 | dev_dbg(wm831x->dev, "DCDC%d under voltage\n", |
| 714 | rdev_get_id(rdev) + 1); |
| 715 | return REGULATOR_STATUS_ERROR; |
| 716 | } |
| 717 | |
| 718 | /* Is the regulator on? */ |
| 719 | ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS); |
| 720 | if (ret < 0) |
| 721 | return ret; |
| 722 | if (ret & (1 << rdev_get_id(rdev))) |
| 723 | return REGULATOR_STATUS_ON; |
| 724 | else |
| 725 | return REGULATOR_STATUS_OFF; |
| 726 | } |
| 727 | |
| 728 | static struct regulator_ops wm831x_boostp_ops = { |
| 729 | .get_status = wm831x_boostp_get_status, |
| 730 | |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 731 | .is_enabled = regulator_is_enabled_regmap, |
| 732 | .enable = regulator_enable_regmap, |
| 733 | .disable = regulator_disable_regmap, |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 734 | }; |
| 735 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 736 | static int wm831x_boostp_probe(struct platform_device *pdev) |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 737 | { |
| 738 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 739 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 740 | struct regulator_config config = { }; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 741 | int id = pdev->id % ARRAY_SIZE(pdata->dcdc); |
| 742 | struct wm831x_dcdc *dcdc; |
| 743 | struct resource *res; |
| 744 | int ret, irq; |
| 745 | |
| 746 | dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); |
| 747 | |
| 748 | if (pdata == NULL || pdata->dcdc[id] == NULL) |
| 749 | return -ENODEV; |
| 750 | |
Axel Lin | 4c60165 | 2012-03-23 17:00:01 +0800 | [diff] [blame] | 751 | dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL); |
Sachin Kamat | 5730aa5 | 2014-02-20 14:23:20 +0530 | [diff] [blame] | 752 | if (!dcdc) |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 753 | return -ENOMEM; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 754 | |
| 755 | dcdc->wm831x = wm831x; |
| 756 | |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 757 | res = platform_get_resource(pdev, IORESOURCE_REG, 0); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 758 | if (res == NULL) { |
Mark Brown | 5656098 | 2012-08-07 19:42:47 +0100 | [diff] [blame] | 759 | dev_err(&pdev->dev, "No REG resource\n"); |
Fabio Estevam | 477b2ba | 2013-12-30 18:03:24 -0200 | [diff] [blame] | 760 | return -EINVAL; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 761 | } |
| 762 | dcdc->base = res->start; |
| 763 | |
| 764 | snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); |
| 765 | dcdc->desc.name = dcdc->name; |
| 766 | dcdc->desc.id = id; |
| 767 | dcdc->desc.type = REGULATOR_VOLTAGE; |
| 768 | dcdc->desc.ops = &wm831x_boostp_ops; |
| 769 | dcdc->desc.owner = THIS_MODULE; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 770 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
| 771 | dcdc->desc.enable_mask = 1 << id; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 772 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 773 | config.dev = pdev->dev.parent; |
Mark Brown | f0b067d | 2012-05-14 11:37:09 +0100 | [diff] [blame] | 774 | if (pdata) |
| 775 | config.init_data = pdata->dcdc[id]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 776 | config.driver_data = dcdc; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 777 | config.regmap = wm831x->regmap; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 778 | |
Mark Brown | d73b4cb | 2013-08-31 11:58:40 +0100 | [diff] [blame] | 779 | dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc, |
| 780 | &config); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 781 | if (IS_ERR(dcdc->regulator)) { |
| 782 | ret = PTR_ERR(dcdc->regulator); |
| 783 | dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", |
| 784 | id + 1, ret); |
Fabio Estevam | 477b2ba | 2013-12-30 18:03:24 -0200 | [diff] [blame] | 785 | return ret; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Mark Brown | cd99758 | 2012-05-14 23:14:24 +0200 | [diff] [blame] | 788 | irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); |
Mark Brown | b0c4c0c | 2013-08-31 11:47:39 +0100 | [diff] [blame] | 789 | ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, |
| 790 | wm831x_dcdc_uv_irq, |
Fabio Estevam | 2945473 | 2015-06-01 22:33:53 -0300 | [diff] [blame] | 791 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 792 | dcdc->name, |
Mark Brown | b0c4c0c | 2013-08-31 11:47:39 +0100 | [diff] [blame] | 793 | dcdc); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 794 | if (ret != 0) { |
| 795 | dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", |
| 796 | irq, ret); |
Fabio Estevam | 477b2ba | 2013-12-30 18:03:24 -0200 | [diff] [blame] | 797 | return ret; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | platform_set_drvdata(pdev, dcdc); |
| 801 | |
| 802 | return 0; |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 803 | } |
| 804 | |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 805 | static struct platform_driver wm831x_boostp_driver = { |
| 806 | .probe = wm831x_boostp_probe, |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 807 | .driver = { |
| 808 | .name = "wm831x-boostp", |
| 809 | }, |
| 810 | }; |
| 811 | |
| 812 | /* |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 813 | * External Power Enable |
| 814 | * |
| 815 | * These aren't actually DCDCs but look like them in hardware so share |
| 816 | * code. |
| 817 | */ |
| 818 | |
| 819 | #define WM831X_EPE_BASE 6 |
| 820 | |
| 821 | static struct regulator_ops wm831x_epe_ops = { |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 822 | .is_enabled = regulator_is_enabled_regmap, |
| 823 | .enable = regulator_enable_regmap, |
| 824 | .disable = regulator_disable_regmap, |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 825 | .get_status = wm831x_dcdc_get_status, |
| 826 | }; |
| 827 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 828 | static int wm831x_epe_probe(struct platform_device *pdev) |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 829 | { |
| 830 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 831 | struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 832 | struct regulator_config config = { }; |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 833 | int id = pdev->id % ARRAY_SIZE(pdata->epe); |
| 834 | struct wm831x_dcdc *dcdc; |
| 835 | int ret; |
| 836 | |
| 837 | dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1); |
| 838 | |
Axel Lin | 4c60165 | 2012-03-23 17:00:01 +0800 | [diff] [blame] | 839 | dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL); |
Sachin Kamat | 5730aa5 | 2014-02-20 14:23:20 +0530 | [diff] [blame] | 840 | if (!dcdc) |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 841 | return -ENOMEM; |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 842 | |
| 843 | dcdc->wm831x = wm831x; |
| 844 | |
| 845 | /* For current parts this is correct; probably need to revisit |
| 846 | * in future. |
| 847 | */ |
| 848 | snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1); |
| 849 | dcdc->desc.name = dcdc->name; |
| 850 | dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */ |
| 851 | dcdc->desc.ops = &wm831x_epe_ops; |
| 852 | dcdc->desc.type = REGULATOR_VOLTAGE; |
| 853 | dcdc->desc.owner = THIS_MODULE; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 854 | dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; |
| 855 | dcdc->desc.enable_mask = 1 << dcdc->desc.id; |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 856 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 857 | config.dev = pdev->dev.parent; |
Mark Brown | b7ca878 | 2012-05-10 00:41:02 +0100 | [diff] [blame] | 858 | if (pdata) |
| 859 | config.init_data = pdata->epe[id]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 860 | config.driver_data = dcdc; |
Mark Brown | 3d138fc | 2012-04-15 12:38:42 +0100 | [diff] [blame] | 861 | config.regmap = wm831x->regmap; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 862 | |
Mark Brown | d73b4cb | 2013-08-31 11:58:40 +0100 | [diff] [blame] | 863 | dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc, |
| 864 | &config); |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 865 | if (IS_ERR(dcdc->regulator)) { |
| 866 | ret = PTR_ERR(dcdc->regulator); |
| 867 | dev_err(wm831x->dev, "Failed to register EPE%d: %d\n", |
| 868 | id + 1, ret); |
| 869 | goto err; |
| 870 | } |
| 871 | |
| 872 | platform_set_drvdata(pdev, dcdc); |
| 873 | |
| 874 | return 0; |
| 875 | |
| 876 | err: |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 877 | return ret; |
| 878 | } |
| 879 | |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 880 | static struct platform_driver wm831x_epe_driver = { |
| 881 | .probe = wm831x_epe_probe, |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 882 | .driver = { |
| 883 | .name = "wm831x-epe", |
| 884 | }, |
| 885 | }; |
| 886 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 887 | static int __init wm831x_dcdc_init(void) |
| 888 | { |
| 889 | int ret; |
| 890 | ret = platform_driver_register(&wm831x_buckv_driver); |
| 891 | if (ret != 0) |
| 892 | pr_err("Failed to register WM831x BUCKV driver: %d\n", ret); |
| 893 | |
| 894 | ret = platform_driver_register(&wm831x_buckp_driver); |
| 895 | if (ret != 0) |
| 896 | pr_err("Failed to register WM831x BUCKP driver: %d\n", ret); |
| 897 | |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 898 | ret = platform_driver_register(&wm831x_boostp_driver); |
| 899 | if (ret != 0) |
| 900 | pr_err("Failed to register WM831x BOOST driver: %d\n", ret); |
| 901 | |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 902 | ret = platform_driver_register(&wm831x_epe_driver); |
| 903 | if (ret != 0) |
| 904 | pr_err("Failed to register WM831x EPE driver: %d\n", ret); |
| 905 | |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 906 | return 0; |
| 907 | } |
| 908 | subsys_initcall(wm831x_dcdc_init); |
| 909 | |
| 910 | static void __exit wm831x_dcdc_exit(void) |
| 911 | { |
Mark Brown | 8267a9b | 2009-07-28 15:22:23 +0100 | [diff] [blame] | 912 | platform_driver_unregister(&wm831x_epe_driver); |
Mark Brown | 1304850 | 2009-07-28 15:23:16 +0100 | [diff] [blame] | 913 | platform_driver_unregister(&wm831x_boostp_driver); |
Mark Brown | e4ee831 | 2009-07-28 15:21:49 +0100 | [diff] [blame] | 914 | platform_driver_unregister(&wm831x_buckp_driver); |
| 915 | platform_driver_unregister(&wm831x_buckv_driver); |
| 916 | } |
| 917 | module_exit(wm831x_dcdc_exit); |
| 918 | |
| 919 | /* Module information */ |
| 920 | MODULE_AUTHOR("Mark Brown"); |
| 921 | MODULE_DESCRIPTION("WM831x DC-DC convertor driver"); |
| 922 | MODULE_LICENSE("GPL"); |
| 923 | MODULE_ALIAS("platform:wm831x-buckv"); |
| 924 | MODULE_ALIAS("platform:wm831x-buckp"); |
Axel Lin | 43f1f21 | 2012-11-30 20:39:53 +0800 | [diff] [blame] | 925 | MODULE_ALIAS("platform:wm831x-boostp"); |
Mark Brown | 24b4315 | 2011-07-25 22:20:31 +0100 | [diff] [blame] | 926 | MODULE_ALIAS("platform:wm831x-epe"); |