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