1e4ee831fSMark Brown /* 2e4ee831fSMark Brown * wm831x-dcdc.c -- DC-DC buck convertor driver for the WM831x series 3e4ee831fSMark Brown * 4e4ee831fSMark Brown * Copyright 2009 Wolfson Microelectronics PLC. 5e4ee831fSMark Brown * 6e4ee831fSMark Brown * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> 7e4ee831fSMark Brown * 8e4ee831fSMark Brown * This program is free software; you can redistribute it and/or modify it 9e4ee831fSMark Brown * under the terms of the GNU General Public License as published by the 10e4ee831fSMark Brown * Free Software Foundation; either version 2 of the License, or (at your 11e4ee831fSMark Brown * option) any later version. 12e4ee831fSMark Brown */ 13e4ee831fSMark Brown 14e4ee831fSMark Brown #include <linux/module.h> 15e4ee831fSMark Brown #include <linux/moduleparam.h> 16e4ee831fSMark Brown #include <linux/init.h> 17e4ee831fSMark Brown #include <linux/bitops.h> 18e4ee831fSMark Brown #include <linux/err.h> 19e4ee831fSMark Brown #include <linux/i2c.h> 20e4ee831fSMark Brown #include <linux/platform_device.h> 21e4ee831fSMark Brown #include <linux/regulator/driver.h> 22e24a04c4SMark Brown #include <linux/regulator/machine.h> 23e24a04c4SMark Brown #include <linux/gpio.h> 245a0e3ad6STejun Heo #include <linux/slab.h> 25e4ee831fSMark Brown 26e4ee831fSMark Brown #include <linux/mfd/wm831x/core.h> 27e4ee831fSMark Brown #include <linux/mfd/wm831x/regulator.h> 28e4ee831fSMark Brown #include <linux/mfd/wm831x/pdata.h> 29e4ee831fSMark Brown 30e4ee831fSMark Brown #define WM831X_BUCKV_MAX_SELECTOR 0x68 31e4ee831fSMark Brown #define WM831X_BUCKP_MAX_SELECTOR 0x66 32e4ee831fSMark Brown 33e4ee831fSMark Brown #define WM831X_DCDC_MODE_FAST 0 34e4ee831fSMark Brown #define WM831X_DCDC_MODE_NORMAL 1 35e4ee831fSMark Brown #define WM831X_DCDC_MODE_IDLE 2 36e4ee831fSMark Brown #define WM831X_DCDC_MODE_STANDBY 3 37e4ee831fSMark Brown 3882caa978SMark Brown #define WM831X_DCDC_MAX_NAME 9 39e4ee831fSMark Brown 40e4ee831fSMark Brown /* Register offsets in control block */ 41e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_1 0 42e4ee831fSMark Brown #define WM831X_DCDC_CONTROL_2 1 43e4ee831fSMark Brown #define WM831X_DCDC_ON_CONFIG 2 44e4ee831fSMark Brown #define WM831X_DCDC_SLEEP_CONTROL 3 45e24a04c4SMark Brown #define WM831X_DCDC_DVS_CONTROL 4 46e4ee831fSMark Brown 47e4ee831fSMark Brown /* 48e4ee831fSMark Brown * Shared 49e4ee831fSMark Brown */ 50e4ee831fSMark Brown 51e4ee831fSMark Brown struct wm831x_dcdc { 52e4ee831fSMark Brown char name[WM831X_DCDC_MAX_NAME]; 5382caa978SMark Brown char supply_name[WM831X_DCDC_MAX_NAME]; 54e4ee831fSMark Brown struct regulator_desc desc; 55e4ee831fSMark Brown int base; 56e4ee831fSMark Brown struct wm831x *wm831x; 57e4ee831fSMark Brown struct regulator_dev *regulator; 58e24a04c4SMark Brown int dvs_gpio; 59e24a04c4SMark Brown int dvs_gpio_state; 60e24a04c4SMark Brown int on_vsel; 61e24a04c4SMark Brown int dvs_vsel; 62e4ee831fSMark Brown }; 63e4ee831fSMark Brown 64e4ee831fSMark Brown static unsigned int wm831x_dcdc_get_mode(struct regulator_dev *rdev) 65e4ee831fSMark Brown 66e4ee831fSMark Brown { 67e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 68e4ee831fSMark Brown struct wm831x *wm831x = dcdc->wm831x; 69e4ee831fSMark Brown u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 70e4ee831fSMark Brown int val; 71e4ee831fSMark Brown 72e4ee831fSMark Brown val = wm831x_reg_read(wm831x, reg); 73e4ee831fSMark Brown if (val < 0) 74e4ee831fSMark Brown return val; 75e4ee831fSMark Brown 76e4ee831fSMark Brown val = (val & WM831X_DC1_ON_MODE_MASK) >> WM831X_DC1_ON_MODE_SHIFT; 77e4ee831fSMark Brown 78e4ee831fSMark Brown switch (val) { 79e4ee831fSMark Brown case WM831X_DCDC_MODE_FAST: 80e4ee831fSMark Brown return REGULATOR_MODE_FAST; 81e4ee831fSMark Brown case WM831X_DCDC_MODE_NORMAL: 82e4ee831fSMark Brown return REGULATOR_MODE_NORMAL; 83e4ee831fSMark Brown case WM831X_DCDC_MODE_STANDBY: 84e4ee831fSMark Brown return REGULATOR_MODE_STANDBY; 85e4ee831fSMark Brown case WM831X_DCDC_MODE_IDLE: 86e4ee831fSMark Brown return REGULATOR_MODE_IDLE; 87e4ee831fSMark Brown default: 88e4ee831fSMark Brown BUG(); 899ee291a4SMark Brown return -EINVAL; 90e4ee831fSMark Brown } 91e4ee831fSMark Brown } 92e4ee831fSMark Brown 93e4ee831fSMark Brown static int wm831x_dcdc_set_mode_int(struct wm831x *wm831x, int reg, 94e4ee831fSMark Brown unsigned int mode) 95e4ee831fSMark Brown { 96e4ee831fSMark Brown int val; 97e4ee831fSMark Brown 98e4ee831fSMark Brown switch (mode) { 99e4ee831fSMark Brown case REGULATOR_MODE_FAST: 100e4ee831fSMark Brown val = WM831X_DCDC_MODE_FAST; 101e4ee831fSMark Brown break; 102e4ee831fSMark Brown case REGULATOR_MODE_NORMAL: 103e4ee831fSMark Brown val = WM831X_DCDC_MODE_NORMAL; 104e4ee831fSMark Brown break; 105e4ee831fSMark Brown case REGULATOR_MODE_STANDBY: 106e4ee831fSMark Brown val = WM831X_DCDC_MODE_STANDBY; 107e4ee831fSMark Brown break; 108e4ee831fSMark Brown case REGULATOR_MODE_IDLE: 109e4ee831fSMark Brown val = WM831X_DCDC_MODE_IDLE; 110e4ee831fSMark Brown break; 111e4ee831fSMark Brown default: 112e4ee831fSMark Brown return -EINVAL; 113e4ee831fSMark Brown } 114e4ee831fSMark Brown 115e4ee831fSMark Brown return wm831x_set_bits(wm831x, reg, WM831X_DC1_ON_MODE_MASK, 116e4ee831fSMark Brown val << WM831X_DC1_ON_MODE_SHIFT); 117e4ee831fSMark Brown } 118e4ee831fSMark Brown 119e4ee831fSMark Brown static int wm831x_dcdc_set_mode(struct regulator_dev *rdev, unsigned int mode) 120e4ee831fSMark Brown { 121e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 122e4ee831fSMark Brown struct wm831x *wm831x = dcdc->wm831x; 123e4ee831fSMark Brown u16 reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 124e4ee831fSMark Brown 125e4ee831fSMark Brown return wm831x_dcdc_set_mode_int(wm831x, reg, mode); 126e4ee831fSMark Brown } 127e4ee831fSMark Brown 128e4ee831fSMark Brown static int wm831x_dcdc_set_suspend_mode(struct regulator_dev *rdev, 129e4ee831fSMark Brown unsigned int mode) 130e4ee831fSMark Brown { 131e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 132e4ee831fSMark Brown struct wm831x *wm831x = dcdc->wm831x; 133e4ee831fSMark Brown u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; 134e4ee831fSMark Brown 135e4ee831fSMark Brown return wm831x_dcdc_set_mode_int(wm831x, reg, mode); 136e4ee831fSMark Brown } 137e4ee831fSMark Brown 138e4ee831fSMark Brown static int wm831x_dcdc_get_status(struct regulator_dev *rdev) 139e4ee831fSMark Brown { 140e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 141e4ee831fSMark Brown struct wm831x *wm831x = dcdc->wm831x; 142e4ee831fSMark Brown int ret; 143e4ee831fSMark Brown 144e4ee831fSMark Brown /* First, check for errors */ 145e4ee831fSMark Brown ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS); 146e4ee831fSMark Brown if (ret < 0) 147e4ee831fSMark Brown return ret; 148e4ee831fSMark Brown 149e4ee831fSMark Brown if (ret & (1 << rdev_get_id(rdev))) { 150e4ee831fSMark Brown dev_dbg(wm831x->dev, "DCDC%d under voltage\n", 151e4ee831fSMark Brown rdev_get_id(rdev) + 1); 152e4ee831fSMark Brown return REGULATOR_STATUS_ERROR; 153e4ee831fSMark Brown } 154e4ee831fSMark Brown 155e4ee831fSMark Brown /* DCDC1 and DCDC2 can additionally detect high voltage/current */ 156e4ee831fSMark Brown if (rdev_get_id(rdev) < 2) { 157e4ee831fSMark Brown if (ret & (WM831X_DC1_OV_STS << rdev_get_id(rdev))) { 158e4ee831fSMark Brown dev_dbg(wm831x->dev, "DCDC%d over voltage\n", 159e4ee831fSMark Brown rdev_get_id(rdev) + 1); 160e4ee831fSMark Brown return REGULATOR_STATUS_ERROR; 161e4ee831fSMark Brown } 162e4ee831fSMark Brown 163e4ee831fSMark Brown if (ret & (WM831X_DC1_HC_STS << rdev_get_id(rdev))) { 164e4ee831fSMark Brown dev_dbg(wm831x->dev, "DCDC%d over current\n", 165e4ee831fSMark Brown rdev_get_id(rdev) + 1); 166e4ee831fSMark Brown return REGULATOR_STATUS_ERROR; 167e4ee831fSMark Brown } 168e4ee831fSMark Brown } 169e4ee831fSMark Brown 170e4ee831fSMark Brown /* Is the regulator on? */ 171e4ee831fSMark Brown ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS); 172e4ee831fSMark Brown if (ret < 0) 173e4ee831fSMark Brown return ret; 174e4ee831fSMark Brown if (!(ret & (1 << rdev_get_id(rdev)))) 175e4ee831fSMark Brown return REGULATOR_STATUS_OFF; 176e4ee831fSMark Brown 177e4ee831fSMark Brown /* TODO: When we handle hardware control modes so we can report the 178e4ee831fSMark Brown * current mode. */ 179e4ee831fSMark Brown return REGULATOR_STATUS_ON; 180e4ee831fSMark Brown } 181e4ee831fSMark Brown 182e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_uv_irq(int irq, void *data) 183e4ee831fSMark Brown { 184e4ee831fSMark Brown struct wm831x_dcdc *dcdc = data; 185e4ee831fSMark Brown 186e4ee831fSMark Brown regulator_notifier_call_chain(dcdc->regulator, 187e4ee831fSMark Brown REGULATOR_EVENT_UNDER_VOLTAGE, 188e4ee831fSMark Brown NULL); 189e4ee831fSMark Brown 190e4ee831fSMark Brown return IRQ_HANDLED; 191e4ee831fSMark Brown } 192e4ee831fSMark Brown 193e4ee831fSMark Brown static irqreturn_t wm831x_dcdc_oc_irq(int irq, void *data) 194e4ee831fSMark Brown { 195e4ee831fSMark Brown struct wm831x_dcdc *dcdc = data; 196e4ee831fSMark Brown 197e4ee831fSMark Brown regulator_notifier_call_chain(dcdc->regulator, 198e4ee831fSMark Brown REGULATOR_EVENT_OVER_CURRENT, 199e4ee831fSMark Brown NULL); 200e4ee831fSMark Brown 201e4ee831fSMark Brown return IRQ_HANDLED; 202e4ee831fSMark Brown } 203e4ee831fSMark Brown 204e4ee831fSMark Brown /* 205e4ee831fSMark Brown * BUCKV specifics 206e4ee831fSMark Brown */ 207e4ee831fSMark Brown 208e4ee831fSMark Brown static int wm831x_buckv_list_voltage(struct regulator_dev *rdev, 209e4ee831fSMark Brown unsigned selector) 210e4ee831fSMark Brown { 211e4ee831fSMark Brown if (selector <= 0x8) 212e4ee831fSMark Brown return 600000; 213e4ee831fSMark Brown if (selector <= WM831X_BUCKV_MAX_SELECTOR) 214e4ee831fSMark Brown return 600000 + ((selector - 0x8) * 12500); 215e4ee831fSMark Brown return -EINVAL; 216e4ee831fSMark Brown } 217e4ee831fSMark Brown 218b5fb77e0SAxel Lin static int wm831x_buckv_map_voltage(struct regulator_dev *rdev, 219e4ee831fSMark Brown int min_uV, int max_uV) 220e4ee831fSMark Brown { 221e4ee831fSMark Brown u16 vsel; 222e4ee831fSMark Brown 223e4ee831fSMark Brown if (min_uV < 600000) 224e4ee831fSMark Brown vsel = 0; 225e4ee831fSMark Brown else if (min_uV <= 1800000) 2261a679930SAxel Lin vsel = DIV_ROUND_UP(min_uV - 600000, 12500) + 8; 227e4ee831fSMark Brown else 228e4ee831fSMark Brown return -EINVAL; 229e4ee831fSMark Brown 230e4ee831fSMark Brown if (wm831x_buckv_list_voltage(rdev, vsel) > max_uV) 231e4ee831fSMark Brown return -EINVAL; 232e4ee831fSMark Brown 233e24a04c4SMark Brown return vsel; 234e24a04c4SMark Brown } 235e24a04c4SMark Brown 236e24a04c4SMark Brown static int wm831x_buckv_set_dvs(struct regulator_dev *rdev, int state) 237e24a04c4SMark Brown { 238e24a04c4SMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 239e24a04c4SMark Brown 240e24a04c4SMark Brown if (state == dcdc->dvs_gpio_state) 241e24a04c4SMark Brown return 0; 242e24a04c4SMark Brown 243e24a04c4SMark Brown dcdc->dvs_gpio_state = state; 244e24a04c4SMark Brown gpio_set_value(dcdc->dvs_gpio, state); 245e24a04c4SMark Brown 246e24a04c4SMark Brown /* Should wait for DVS state change to be asserted if we have 247e24a04c4SMark Brown * a GPIO for it, for now assume the device is configured 248e24a04c4SMark Brown * for the fastest possible transition. 249e24a04c4SMark Brown */ 250e24a04c4SMark Brown 251e24a04c4SMark Brown return 0; 252e4ee831fSMark Brown } 253e4ee831fSMark Brown 254b5fb77e0SAxel Lin static int wm831x_buckv_set_voltage_sel(struct regulator_dev *rdev, 255b5fb77e0SAxel Lin unsigned vsel) 256e4ee831fSMark Brown { 257e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 258e24a04c4SMark Brown struct wm831x *wm831x = dcdc->wm831x; 259e24a04c4SMark Brown int on_reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 260e24a04c4SMark Brown int dvs_reg = dcdc->base + WM831X_DCDC_DVS_CONTROL; 261b5fb77e0SAxel Lin int ret; 2623a93f2a9SMark Brown 263e24a04c4SMark Brown /* If this value is already set then do a GPIO update if we can */ 264e24a04c4SMark Brown if (dcdc->dvs_gpio && dcdc->on_vsel == vsel) 265e24a04c4SMark Brown return wm831x_buckv_set_dvs(rdev, 0); 266e24a04c4SMark Brown 267e24a04c4SMark Brown if (dcdc->dvs_gpio && dcdc->dvs_vsel == vsel) 268e24a04c4SMark Brown return wm831x_buckv_set_dvs(rdev, 1); 269e24a04c4SMark Brown 270e24a04c4SMark Brown /* Always set the ON status to the minimum voltage */ 271e24a04c4SMark Brown ret = wm831x_set_bits(wm831x, on_reg, WM831X_DC1_ON_VSEL_MASK, vsel); 272e24a04c4SMark Brown if (ret < 0) 273e24a04c4SMark Brown return ret; 274e24a04c4SMark Brown dcdc->on_vsel = vsel; 275e24a04c4SMark Brown 276e24a04c4SMark Brown if (!dcdc->dvs_gpio) 277e24a04c4SMark Brown return ret; 278e24a04c4SMark Brown 279e24a04c4SMark Brown /* Kick the voltage transition now */ 280e24a04c4SMark Brown ret = wm831x_buckv_set_dvs(rdev, 0); 281e24a04c4SMark Brown if (ret < 0) 282e24a04c4SMark Brown return ret; 283e24a04c4SMark Brown 28488cda60eSMark Brown /* 28588cda60eSMark Brown * If this VSEL is higher than the last one we've seen then 28688cda60eSMark Brown * remember it as the DVS VSEL. This is optimised for CPUfreq 28788cda60eSMark Brown * usage where we want to get to the highest voltage very 28888cda60eSMark Brown * quickly. 28988cda60eSMark Brown */ 29088cda60eSMark Brown if (vsel > dcdc->dvs_vsel) { 29188cda60eSMark Brown ret = wm831x_set_bits(wm831x, dvs_reg, 29288cda60eSMark Brown WM831X_DC1_DVS_VSEL_MASK, 29313ae633cSMark Brown vsel); 294e24a04c4SMark Brown if (ret == 0) 295e24a04c4SMark Brown dcdc->dvs_vsel = vsel; 296e24a04c4SMark Brown else 29788cda60eSMark Brown dev_warn(wm831x->dev, 29888cda60eSMark Brown "Failed to set DCDC DVS VSEL: %d\n", ret); 29988cda60eSMark Brown } 300e24a04c4SMark Brown 301e24a04c4SMark Brown return 0; 302e4ee831fSMark Brown } 303e4ee831fSMark Brown 304e4ee831fSMark Brown static int wm831x_buckv_set_suspend_voltage(struct regulator_dev *rdev, 305e4ee831fSMark Brown int uV) 306e4ee831fSMark Brown { 307e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 308e24a04c4SMark Brown struct wm831x *wm831x = dcdc->wm831x; 309e4ee831fSMark Brown u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; 310e24a04c4SMark Brown int vsel; 311e4ee831fSMark Brown 312b5fb77e0SAxel Lin vsel = wm831x_buckv_map_voltage(rdev, uV, uV); 313e24a04c4SMark Brown if (vsel < 0) 314e24a04c4SMark Brown return vsel; 315e24a04c4SMark Brown 316e24a04c4SMark Brown return wm831x_set_bits(wm831x, reg, WM831X_DC1_SLP_VSEL_MASK, vsel); 317e4ee831fSMark Brown } 318e4ee831fSMark Brown 319afb8bb80SMark Brown static int wm831x_buckv_get_voltage_sel(struct regulator_dev *rdev) 320e4ee831fSMark Brown { 321e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 322e4ee831fSMark Brown 323e24a04c4SMark Brown if (dcdc->dvs_gpio && dcdc->dvs_gpio_state) 324afb8bb80SMark Brown return dcdc->dvs_vsel; 325e24a04c4SMark Brown else 326afb8bb80SMark Brown return dcdc->on_vsel; 327e4ee831fSMark Brown } 328e4ee831fSMark Brown 329e4ee831fSMark Brown /* Current limit options */ 330e4ee831fSMark Brown static u16 wm831x_dcdc_ilim[] = { 331e4ee831fSMark Brown 125, 250, 375, 500, 625, 750, 875, 1000 332e4ee831fSMark Brown }; 333e4ee831fSMark Brown 334e4ee831fSMark Brown static int wm831x_buckv_set_current_limit(struct regulator_dev *rdev, 335e4ee831fSMark Brown int min_uA, int max_uA) 336e4ee831fSMark Brown { 337e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 338e4ee831fSMark Brown struct wm831x *wm831x = dcdc->wm831x; 339e4ee831fSMark Brown u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; 340e4ee831fSMark Brown int i; 341e4ee831fSMark Brown 342422294deSAxel Lin for (i = ARRAY_SIZE(wm831x_dcdc_ilim) - 1; i >= 0; i--) { 343a171e782SAxel Lin if ((min_uA <= wm831x_dcdc_ilim[i]) && 344a171e782SAxel Lin (wm831x_dcdc_ilim[i] <= max_uA)) 345422294deSAxel Lin return wm831x_set_bits(wm831x, reg, 346422294deSAxel Lin WM831X_DC1_HC_THR_MASK, 34709bf14b9SAxel Lin i << WM831X_DC1_HC_THR_SHIFT); 348e4ee831fSMark Brown } 349e4ee831fSMark Brown 350422294deSAxel Lin return -EINVAL; 351422294deSAxel Lin } 352422294deSAxel Lin 353e4ee831fSMark Brown static int wm831x_buckv_get_current_limit(struct regulator_dev *rdev) 354e4ee831fSMark Brown { 355e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 356e4ee831fSMark Brown struct wm831x *wm831x = dcdc->wm831x; 357e4ee831fSMark Brown u16 reg = dcdc->base + WM831X_DCDC_CONTROL_2; 358e4ee831fSMark Brown int val; 359e4ee831fSMark Brown 360e4ee831fSMark Brown val = wm831x_reg_read(wm831x, reg); 361e4ee831fSMark Brown if (val < 0) 362e4ee831fSMark Brown return val; 363e4ee831fSMark Brown 36409bf14b9SAxel Lin val = (val & WM831X_DC1_HC_THR_MASK) >> WM831X_DC1_HC_THR_SHIFT; 36509bf14b9SAxel Lin return wm831x_dcdc_ilim[val]; 366e4ee831fSMark Brown } 367e4ee831fSMark Brown 368b0d6dd3bSJulia Lawall static const struct regulator_ops wm831x_buckv_ops = { 369b5fb77e0SAxel Lin .set_voltage_sel = wm831x_buckv_set_voltage_sel, 370afb8bb80SMark Brown .get_voltage_sel = wm831x_buckv_get_voltage_sel, 371e4ee831fSMark Brown .list_voltage = wm831x_buckv_list_voltage, 372b5fb77e0SAxel Lin .map_voltage = wm831x_buckv_map_voltage, 373e4ee831fSMark Brown .set_suspend_voltage = wm831x_buckv_set_suspend_voltage, 374e4ee831fSMark Brown .set_current_limit = wm831x_buckv_set_current_limit, 375e4ee831fSMark Brown .get_current_limit = wm831x_buckv_get_current_limit, 376e4ee831fSMark Brown 3773d138fccSMark Brown .is_enabled = regulator_is_enabled_regmap, 3783d138fccSMark Brown .enable = regulator_enable_regmap, 3793d138fccSMark Brown .disable = regulator_disable_regmap, 380e4ee831fSMark Brown .get_status = wm831x_dcdc_get_status, 381e4ee831fSMark Brown .get_mode = wm831x_dcdc_get_mode, 382e4ee831fSMark Brown .set_mode = wm831x_dcdc_set_mode, 383e4ee831fSMark Brown .set_suspend_mode = wm831x_dcdc_set_suspend_mode, 384e4ee831fSMark Brown }; 385e4ee831fSMark Brown 386e24a04c4SMark Brown /* 387e24a04c4SMark Brown * Set up DVS control. We just log errors since we can still run 388e24a04c4SMark Brown * (with reduced performance) if we fail. 389e24a04c4SMark Brown */ 3901aa20d27SMark Brown static void wm831x_buckv_dvs_init(struct platform_device *pdev, 3911aa20d27SMark Brown struct wm831x_dcdc *dcdc, 392e24a04c4SMark Brown struct wm831x_buckv_pdata *pdata) 393e24a04c4SMark Brown { 394e24a04c4SMark Brown struct wm831x *wm831x = dcdc->wm831x; 395e24a04c4SMark Brown int ret; 396e24a04c4SMark Brown u16 ctrl; 397e24a04c4SMark Brown 398e24a04c4SMark Brown if (!pdata || !pdata->dvs_gpio) 399e24a04c4SMark Brown return; 400e24a04c4SMark Brown 401e24a04c4SMark Brown /* gpiolib won't let us read the GPIO status so pick the higher 402e24a04c4SMark Brown * of the two existing voltages so we take it as platform data. 403e24a04c4SMark Brown */ 404e24a04c4SMark Brown dcdc->dvs_gpio_state = pdata->dvs_init_state; 405e24a04c4SMark Brown 4061aa20d27SMark Brown ret = devm_gpio_request_one(&pdev->dev, pdata->dvs_gpio, 407053fa1b7SMark Brown dcdc->dvs_gpio_state ? GPIOF_INIT_HIGH : 0, 408053fa1b7SMark Brown "DCDC DVS"); 409e24a04c4SMark Brown if (ret < 0) { 410053fa1b7SMark Brown dev_err(wm831x->dev, "Failed to get %s DVS GPIO: %d\n", 411e24a04c4SMark Brown dcdc->name, ret); 412e24a04c4SMark Brown return; 413e24a04c4SMark Brown } 414e24a04c4SMark Brown 415e24a04c4SMark Brown dcdc->dvs_gpio = pdata->dvs_gpio; 416b47ba9fdSMark Brown 417b47ba9fdSMark Brown switch (pdata->dvs_control_src) { 418b47ba9fdSMark Brown case 1: 419b47ba9fdSMark Brown ctrl = 2 << WM831X_DC1_DVS_SRC_SHIFT; 420b47ba9fdSMark Brown break; 421b47ba9fdSMark Brown case 2: 422b47ba9fdSMark Brown ctrl = 3 << WM831X_DC1_DVS_SRC_SHIFT; 423b47ba9fdSMark Brown break; 424b47ba9fdSMark Brown default: 425b47ba9fdSMark Brown dev_err(wm831x->dev, "Invalid DVS control source %d for %s\n", 426b47ba9fdSMark Brown pdata->dvs_control_src, dcdc->name); 427b47ba9fdSMark Brown return; 428b47ba9fdSMark Brown } 429b47ba9fdSMark Brown 430c439b8f4SMark Brown /* If DVS_VSEL is set to the minimum value then raise it to ON_VSEL 431c439b8f4SMark Brown * to make bootstrapping a bit smoother. 432c439b8f4SMark Brown */ 433c439b8f4SMark Brown if (!dcdc->dvs_vsel) { 434c439b8f4SMark Brown ret = wm831x_set_bits(wm831x, 435c439b8f4SMark Brown dcdc->base + WM831X_DCDC_DVS_CONTROL, 436c439b8f4SMark Brown WM831X_DC1_DVS_VSEL_MASK, dcdc->on_vsel); 437c439b8f4SMark Brown if (ret == 0) 438c439b8f4SMark Brown dcdc->dvs_vsel = dcdc->on_vsel; 439c439b8f4SMark Brown else 440c439b8f4SMark Brown dev_warn(wm831x->dev, "Failed to set DVS_VSEL: %d\n", 441c439b8f4SMark Brown ret); 442c439b8f4SMark Brown } 443c439b8f4SMark Brown 444b47ba9fdSMark Brown ret = wm831x_set_bits(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL, 445b47ba9fdSMark Brown WM831X_DC1_DVS_SRC_MASK, ctrl); 446b47ba9fdSMark Brown if (ret < 0) { 447b47ba9fdSMark Brown dev_err(wm831x->dev, "Failed to set %s DVS source: %d\n", 448b47ba9fdSMark Brown dcdc->name, ret); 449b47ba9fdSMark Brown } 450e24a04c4SMark Brown } 451e24a04c4SMark Brown 452a5023574SBill Pemberton static int wm831x_buckv_probe(struct platform_device *pdev) 453e4ee831fSMark Brown { 454e4ee831fSMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 455dff91d0bSJingoo Han struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); 456c172708dSMark Brown struct regulator_config config = { }; 457137a6354SMark Brown int id; 458e4ee831fSMark Brown struct wm831x_dcdc *dcdc; 459e4ee831fSMark Brown struct resource *res; 460e4ee831fSMark Brown int ret, irq; 461e4ee831fSMark Brown 462137a6354SMark Brown if (pdata && pdata->wm831x_num) 463137a6354SMark Brown id = (pdata->wm831x_num * 10) + 1; 464137a6354SMark Brown else 465137a6354SMark Brown id = 0; 466137a6354SMark Brown id = pdev->id - id; 467137a6354SMark Brown 468e4ee831fSMark Brown dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 469e4ee831fSMark Brown 470fded2f4fSMark Brown dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), 471fded2f4fSMark Brown GFP_KERNEL); 4725730aa57SSachin Kamat if (!dcdc) 473e4ee831fSMark Brown return -ENOMEM; 474e4ee831fSMark Brown 475e4ee831fSMark Brown dcdc->wm831x = wm831x; 476e4ee831fSMark Brown 4775656098eSMark Brown res = platform_get_resource(pdev, IORESOURCE_REG, 0); 478e4ee831fSMark Brown if (res == NULL) { 4795656098eSMark Brown dev_err(&pdev->dev, "No REG resource\n"); 480e4ee831fSMark Brown ret = -EINVAL; 481e4ee831fSMark Brown goto err; 482e4ee831fSMark Brown } 483e4ee831fSMark Brown dcdc->base = res->start; 484e4ee831fSMark Brown 485e4ee831fSMark Brown snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 486e4ee831fSMark Brown dcdc->desc.name = dcdc->name; 48782caa978SMark Brown 48882caa978SMark Brown snprintf(dcdc->supply_name, sizeof(dcdc->supply_name), 48982caa978SMark Brown "DC%dVDD", id + 1); 49082caa978SMark Brown dcdc->desc.supply_name = dcdc->supply_name; 49182caa978SMark Brown 492e4ee831fSMark Brown dcdc->desc.id = id; 493e4ee831fSMark Brown dcdc->desc.type = REGULATOR_VOLTAGE; 494e4ee831fSMark Brown dcdc->desc.n_voltages = WM831X_BUCKV_MAX_SELECTOR + 1; 495e4ee831fSMark Brown dcdc->desc.ops = &wm831x_buckv_ops; 496e4ee831fSMark Brown dcdc->desc.owner = THIS_MODULE; 4973d138fccSMark Brown dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; 4983d138fccSMark Brown dcdc->desc.enable_mask = 1 << id; 499e4ee831fSMark Brown 500e24a04c4SMark Brown ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_ON_CONFIG); 501e24a04c4SMark Brown if (ret < 0) { 502e24a04c4SMark Brown dev_err(wm831x->dev, "Failed to read ON VSEL: %d\n", ret); 503e24a04c4SMark Brown goto err; 504e24a04c4SMark Brown } 505e24a04c4SMark Brown dcdc->on_vsel = ret & WM831X_DC1_ON_VSEL_MASK; 506e24a04c4SMark Brown 507a1b81dd3SMark Brown ret = wm831x_reg_read(wm831x, dcdc->base + WM831X_DCDC_DVS_CONTROL); 508e24a04c4SMark Brown if (ret < 0) { 509e24a04c4SMark Brown dev_err(wm831x->dev, "Failed to read DVS VSEL: %d\n", ret); 510e24a04c4SMark Brown goto err; 511e24a04c4SMark Brown } 512e24a04c4SMark Brown dcdc->dvs_vsel = ret & WM831X_DC1_DVS_VSEL_MASK; 513e24a04c4SMark Brown 514f0b067d9SMark Brown if (pdata && pdata->dcdc[id]) 5151aa20d27SMark Brown wm831x_buckv_dvs_init(pdev, dcdc, 5161aa20d27SMark Brown pdata->dcdc[id]->driver_data); 517e24a04c4SMark Brown 518c172708dSMark Brown config.dev = pdev->dev.parent; 519b7ca8788SMark Brown if (pdata) 520c172708dSMark Brown config.init_data = pdata->dcdc[id]; 521c172708dSMark Brown config.driver_data = dcdc; 5223d138fccSMark Brown config.regmap = wm831x->regmap; 523c172708dSMark Brown 524d73b4cb7SMark Brown dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc, 525d73b4cb7SMark Brown &config); 526e4ee831fSMark Brown if (IS_ERR(dcdc->regulator)) { 527e4ee831fSMark Brown ret = PTR_ERR(dcdc->regulator); 528e4ee831fSMark Brown dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 529e4ee831fSMark Brown id + 1, ret); 530e4ee831fSMark Brown goto err; 531e4ee831fSMark Brown } 532e4ee831fSMark Brown 533cd99758bSMark Brown irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); 534b0c4c0c6SMark Brown ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 535b0c4c0c6SMark Brown wm831x_dcdc_uv_irq, 53629454738SFabio Estevam IRQF_TRIGGER_RISING | IRQF_ONESHOT, 53729454738SFabio Estevam dcdc->name, dcdc); 538e4ee831fSMark Brown if (ret != 0) { 539e4ee831fSMark Brown dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 540e4ee831fSMark Brown irq, ret); 541d73b4cb7SMark Brown goto err; 542e4ee831fSMark Brown } 543e4ee831fSMark Brown 544cd99758bSMark Brown irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "HC")); 545b0c4c0c6SMark Brown ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 546b0c4c0c6SMark Brown wm831x_dcdc_oc_irq, 54729454738SFabio Estevam IRQF_TRIGGER_RISING | IRQF_ONESHOT, 54829454738SFabio Estevam dcdc->name, dcdc); 549e4ee831fSMark Brown if (ret != 0) { 550e4ee831fSMark Brown dev_err(&pdev->dev, "Failed to request HC IRQ %d: %d\n", 551e4ee831fSMark Brown irq, ret); 552d73b4cb7SMark Brown goto err; 553e4ee831fSMark Brown } 554e4ee831fSMark Brown 555e4ee831fSMark Brown platform_set_drvdata(pdev, dcdc); 556e4ee831fSMark Brown 557e4ee831fSMark Brown return 0; 558e4ee831fSMark Brown 559e4ee831fSMark Brown err: 560e4ee831fSMark Brown return ret; 561e4ee831fSMark Brown } 562e4ee831fSMark Brown 563e4ee831fSMark Brown static struct platform_driver wm831x_buckv_driver = { 564e4ee831fSMark Brown .probe = wm831x_buckv_probe, 565e4ee831fSMark Brown .driver = { 566e4ee831fSMark Brown .name = "wm831x-buckv", 567e4ee831fSMark Brown }, 568e4ee831fSMark Brown }; 569e4ee831fSMark Brown 570e4ee831fSMark Brown /* 571e4ee831fSMark Brown * BUCKP specifics 572e4ee831fSMark Brown */ 573e4ee831fSMark Brown 574d580cb5eSAxel Lin static int wm831x_buckp_set_suspend_voltage(struct regulator_dev *rdev, int uV) 575e4ee831fSMark Brown { 576e4ee831fSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 577e4ee831fSMark Brown struct wm831x *wm831x = dcdc->wm831x; 578e4ee831fSMark Brown u16 reg = dcdc->base + WM831X_DCDC_SLEEP_CONTROL; 579d580cb5eSAxel Lin int sel; 580e4ee831fSMark Brown 581d580cb5eSAxel Lin sel = regulator_map_voltage_linear(rdev, uV, uV); 582d580cb5eSAxel Lin if (sel < 0) 583d580cb5eSAxel Lin return sel; 584d580cb5eSAxel Lin 585d580cb5eSAxel Lin return wm831x_set_bits(wm831x, reg, WM831X_DC3_ON_VSEL_MASK, sel); 586e4ee831fSMark Brown } 587e4ee831fSMark Brown 588b0d6dd3bSJulia Lawall static const struct regulator_ops wm831x_buckp_ops = { 589d580cb5eSAxel Lin .set_voltage_sel = regulator_set_voltage_sel_regmap, 590817436e7SMark Brown .get_voltage_sel = regulator_get_voltage_sel_regmap, 591c70ad9dcSAxel Lin .list_voltage = regulator_list_voltage_linear, 592d580cb5eSAxel Lin .map_voltage = regulator_map_voltage_linear, 593e4ee831fSMark Brown .set_suspend_voltage = wm831x_buckp_set_suspend_voltage, 594e4ee831fSMark Brown 5953d138fccSMark Brown .is_enabled = regulator_is_enabled_regmap, 5963d138fccSMark Brown .enable = regulator_enable_regmap, 5973d138fccSMark Brown .disable = regulator_disable_regmap, 598e4ee831fSMark Brown .get_status = wm831x_dcdc_get_status, 599e4ee831fSMark Brown .get_mode = wm831x_dcdc_get_mode, 600e4ee831fSMark Brown .set_mode = wm831x_dcdc_set_mode, 601e4ee831fSMark Brown .set_suspend_mode = wm831x_dcdc_set_suspend_mode, 602e4ee831fSMark Brown }; 603e4ee831fSMark Brown 604a5023574SBill Pemberton static int wm831x_buckp_probe(struct platform_device *pdev) 605e4ee831fSMark Brown { 606e4ee831fSMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 607dff91d0bSJingoo Han struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); 608c172708dSMark Brown struct regulator_config config = { }; 609137a6354SMark Brown int id; 610e4ee831fSMark Brown struct wm831x_dcdc *dcdc; 611e4ee831fSMark Brown struct resource *res; 612e4ee831fSMark Brown int ret, irq; 613e4ee831fSMark Brown 614137a6354SMark Brown if (pdata && pdata->wm831x_num) 615137a6354SMark Brown id = (pdata->wm831x_num * 10) + 1; 616137a6354SMark Brown else 617137a6354SMark Brown id = 0; 618137a6354SMark Brown id = pdev->id - id; 619137a6354SMark Brown 620e4ee831fSMark Brown dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 621e4ee831fSMark Brown 622fded2f4fSMark Brown dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), 623fded2f4fSMark Brown GFP_KERNEL); 6245730aa57SSachin Kamat if (!dcdc) 625e4ee831fSMark Brown return -ENOMEM; 626e4ee831fSMark Brown 627e4ee831fSMark Brown dcdc->wm831x = wm831x; 628e4ee831fSMark Brown 6295656098eSMark Brown res = platform_get_resource(pdev, IORESOURCE_REG, 0); 630e4ee831fSMark Brown if (res == NULL) { 6315656098eSMark Brown dev_err(&pdev->dev, "No REG resource\n"); 632e4ee831fSMark Brown ret = -EINVAL; 633e4ee831fSMark Brown goto err; 634e4ee831fSMark Brown } 635e4ee831fSMark Brown dcdc->base = res->start; 636e4ee831fSMark Brown 637e4ee831fSMark Brown snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 638e4ee831fSMark Brown dcdc->desc.name = dcdc->name; 63982caa978SMark Brown 64082caa978SMark Brown snprintf(dcdc->supply_name, sizeof(dcdc->supply_name), 64182caa978SMark Brown "DC%dVDD", id + 1); 64282caa978SMark Brown dcdc->desc.supply_name = dcdc->supply_name; 64382caa978SMark Brown 644e4ee831fSMark Brown dcdc->desc.id = id; 645e4ee831fSMark Brown dcdc->desc.type = REGULATOR_VOLTAGE; 646e4ee831fSMark Brown dcdc->desc.n_voltages = WM831X_BUCKP_MAX_SELECTOR + 1; 647e4ee831fSMark Brown dcdc->desc.ops = &wm831x_buckp_ops; 648e4ee831fSMark Brown dcdc->desc.owner = THIS_MODULE; 649817436e7SMark Brown dcdc->desc.vsel_reg = dcdc->base + WM831X_DCDC_ON_CONFIG; 650817436e7SMark Brown dcdc->desc.vsel_mask = WM831X_DC3_ON_VSEL_MASK; 6513d138fccSMark Brown dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; 6523d138fccSMark Brown dcdc->desc.enable_mask = 1 << id; 653c70ad9dcSAxel Lin dcdc->desc.min_uV = 850000; 654c70ad9dcSAxel Lin dcdc->desc.uV_step = 25000; 655e4ee831fSMark Brown 656c172708dSMark Brown config.dev = pdev->dev.parent; 657b7ca8788SMark Brown if (pdata) 658c172708dSMark Brown config.init_data = pdata->dcdc[id]; 659c172708dSMark Brown config.driver_data = dcdc; 660817436e7SMark Brown config.regmap = wm831x->regmap; 661c172708dSMark Brown 662d73b4cb7SMark Brown dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc, 663d73b4cb7SMark Brown &config); 664e4ee831fSMark Brown if (IS_ERR(dcdc->regulator)) { 665e4ee831fSMark Brown ret = PTR_ERR(dcdc->regulator); 666e4ee831fSMark Brown dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 667e4ee831fSMark Brown id + 1, ret); 668e4ee831fSMark Brown goto err; 669e4ee831fSMark Brown } 670e4ee831fSMark Brown 671cd99758bSMark Brown irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); 672b0c4c0c6SMark Brown ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 673b0c4c0c6SMark Brown wm831x_dcdc_uv_irq, 67429454738SFabio Estevam IRQF_TRIGGER_RISING | IRQF_ONESHOT, 67529454738SFabio Estevam dcdc->name, dcdc); 676e4ee831fSMark Brown if (ret != 0) { 677e4ee831fSMark Brown dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 678e4ee831fSMark Brown irq, ret); 679d73b4cb7SMark Brown goto err; 680e4ee831fSMark Brown } 681e4ee831fSMark Brown 682e4ee831fSMark Brown platform_set_drvdata(pdev, dcdc); 683e4ee831fSMark Brown 684e4ee831fSMark Brown return 0; 685e4ee831fSMark Brown 686e4ee831fSMark Brown err: 687e4ee831fSMark Brown return ret; 688e4ee831fSMark Brown } 689e4ee831fSMark Brown 690e4ee831fSMark Brown static struct platform_driver wm831x_buckp_driver = { 691e4ee831fSMark Brown .probe = wm831x_buckp_probe, 692e4ee831fSMark Brown .driver = { 693e4ee831fSMark Brown .name = "wm831x-buckp", 694e4ee831fSMark Brown }, 695e4ee831fSMark Brown }; 696e4ee831fSMark Brown 6978267a9baSMark Brown /* 6981304850dSMark Brown * DCDC boost convertors 6991304850dSMark Brown */ 7001304850dSMark Brown 7011304850dSMark Brown static int wm831x_boostp_get_status(struct regulator_dev *rdev) 7021304850dSMark Brown { 7031304850dSMark Brown struct wm831x_dcdc *dcdc = rdev_get_drvdata(rdev); 7041304850dSMark Brown struct wm831x *wm831x = dcdc->wm831x; 7051304850dSMark Brown int ret; 7061304850dSMark Brown 7071304850dSMark Brown /* First, check for errors */ 7081304850dSMark Brown ret = wm831x_reg_read(wm831x, WM831X_DCDC_UV_STATUS); 7091304850dSMark Brown if (ret < 0) 7101304850dSMark Brown return ret; 7111304850dSMark Brown 7121304850dSMark Brown if (ret & (1 << rdev_get_id(rdev))) { 7131304850dSMark Brown dev_dbg(wm831x->dev, "DCDC%d under voltage\n", 7141304850dSMark Brown rdev_get_id(rdev) + 1); 7151304850dSMark Brown return REGULATOR_STATUS_ERROR; 7161304850dSMark Brown } 7171304850dSMark Brown 7181304850dSMark Brown /* Is the regulator on? */ 7191304850dSMark Brown ret = wm831x_reg_read(wm831x, WM831X_DCDC_STATUS); 7201304850dSMark Brown if (ret < 0) 7211304850dSMark Brown return ret; 7221304850dSMark Brown if (ret & (1 << rdev_get_id(rdev))) 7231304850dSMark Brown return REGULATOR_STATUS_ON; 7241304850dSMark Brown else 7251304850dSMark Brown return REGULATOR_STATUS_OFF; 7261304850dSMark Brown } 7271304850dSMark Brown 728b0d6dd3bSJulia Lawall static const struct regulator_ops wm831x_boostp_ops = { 7291304850dSMark Brown .get_status = wm831x_boostp_get_status, 7301304850dSMark Brown 7313d138fccSMark Brown .is_enabled = regulator_is_enabled_regmap, 7323d138fccSMark Brown .enable = regulator_enable_regmap, 7333d138fccSMark Brown .disable = regulator_disable_regmap, 7341304850dSMark Brown }; 7351304850dSMark Brown 736a5023574SBill Pemberton static int wm831x_boostp_probe(struct platform_device *pdev) 7371304850dSMark Brown { 7381304850dSMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 739dff91d0bSJingoo Han struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); 740c172708dSMark Brown struct regulator_config config = { }; 7411304850dSMark Brown int id = pdev->id % ARRAY_SIZE(pdata->dcdc); 7421304850dSMark Brown struct wm831x_dcdc *dcdc; 7431304850dSMark Brown struct resource *res; 7441304850dSMark Brown int ret, irq; 7451304850dSMark Brown 7461304850dSMark Brown dev_dbg(&pdev->dev, "Probing DCDC%d\n", id + 1); 7471304850dSMark Brown 7481304850dSMark Brown if (pdata == NULL || pdata->dcdc[id] == NULL) 7491304850dSMark Brown return -ENODEV; 7501304850dSMark Brown 7514c60165dSAxel Lin dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL); 7525730aa57SSachin Kamat if (!dcdc) 7531304850dSMark Brown return -ENOMEM; 7541304850dSMark Brown 7551304850dSMark Brown dcdc->wm831x = wm831x; 7561304850dSMark Brown 7575656098eSMark Brown res = platform_get_resource(pdev, IORESOURCE_REG, 0); 7581304850dSMark Brown if (res == NULL) { 7595656098eSMark Brown dev_err(&pdev->dev, "No REG resource\n"); 760477b2bacSFabio Estevam return -EINVAL; 7611304850dSMark Brown } 7621304850dSMark Brown dcdc->base = res->start; 7631304850dSMark Brown 7641304850dSMark Brown snprintf(dcdc->name, sizeof(dcdc->name), "DCDC%d", id + 1); 7651304850dSMark Brown dcdc->desc.name = dcdc->name; 7661304850dSMark Brown dcdc->desc.id = id; 7671304850dSMark Brown dcdc->desc.type = REGULATOR_VOLTAGE; 7681304850dSMark Brown dcdc->desc.ops = &wm831x_boostp_ops; 7691304850dSMark Brown dcdc->desc.owner = THIS_MODULE; 7703d138fccSMark Brown dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; 7713d138fccSMark Brown dcdc->desc.enable_mask = 1 << id; 7721304850dSMark Brown 773c172708dSMark Brown config.dev = pdev->dev.parent; 774f0b067d9SMark Brown if (pdata) 775c172708dSMark Brown config.init_data = pdata->dcdc[id]; 776c172708dSMark Brown config.driver_data = dcdc; 7773d138fccSMark Brown config.regmap = wm831x->regmap; 778c172708dSMark Brown 779d73b4cb7SMark Brown dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc, 780d73b4cb7SMark Brown &config); 7811304850dSMark Brown if (IS_ERR(dcdc->regulator)) { 7821304850dSMark Brown ret = PTR_ERR(dcdc->regulator); 7831304850dSMark Brown dev_err(wm831x->dev, "Failed to register DCDC%d: %d\n", 7841304850dSMark Brown id + 1, ret); 785477b2bacSFabio Estevam return ret; 7861304850dSMark Brown } 7871304850dSMark Brown 788cd99758bSMark Brown irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "UV")); 789b0c4c0c6SMark Brown ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 790b0c4c0c6SMark Brown wm831x_dcdc_uv_irq, 79129454738SFabio Estevam IRQF_TRIGGER_RISING | IRQF_ONESHOT, 79229454738SFabio Estevam dcdc->name, 7931304850dSMark Brown dcdc); 7941304850dSMark Brown if (ret != 0) { 7951304850dSMark Brown dev_err(&pdev->dev, "Failed to request UV IRQ %d: %d\n", 7961304850dSMark Brown irq, ret); 797477b2bacSFabio Estevam return ret; 7981304850dSMark Brown } 7991304850dSMark Brown 8001304850dSMark Brown platform_set_drvdata(pdev, dcdc); 8011304850dSMark Brown 8021304850dSMark Brown return 0; 8031304850dSMark Brown } 8041304850dSMark Brown 8051304850dSMark Brown static struct platform_driver wm831x_boostp_driver = { 8061304850dSMark Brown .probe = wm831x_boostp_probe, 8071304850dSMark Brown .driver = { 8081304850dSMark Brown .name = "wm831x-boostp", 8091304850dSMark Brown }, 8101304850dSMark Brown }; 8111304850dSMark Brown 8121304850dSMark Brown /* 8138267a9baSMark Brown * External Power Enable 8148267a9baSMark Brown * 8158267a9baSMark Brown * These aren't actually DCDCs but look like them in hardware so share 8168267a9baSMark Brown * code. 8178267a9baSMark Brown */ 8188267a9baSMark Brown 8198267a9baSMark Brown #define WM831X_EPE_BASE 6 8208267a9baSMark Brown 821b0d6dd3bSJulia Lawall static const struct regulator_ops wm831x_epe_ops = { 8223d138fccSMark Brown .is_enabled = regulator_is_enabled_regmap, 8233d138fccSMark Brown .enable = regulator_enable_regmap, 8243d138fccSMark Brown .disable = regulator_disable_regmap, 8258267a9baSMark Brown .get_status = wm831x_dcdc_get_status, 8268267a9baSMark Brown }; 8278267a9baSMark Brown 828a5023574SBill Pemberton static int wm831x_epe_probe(struct platform_device *pdev) 8298267a9baSMark Brown { 8308267a9baSMark Brown struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); 831dff91d0bSJingoo Han struct wm831x_pdata *pdata = dev_get_platdata(wm831x->dev); 832c172708dSMark Brown struct regulator_config config = { }; 8338267a9baSMark Brown int id = pdev->id % ARRAY_SIZE(pdata->epe); 8348267a9baSMark Brown struct wm831x_dcdc *dcdc; 8358267a9baSMark Brown int ret; 8368267a9baSMark Brown 8378267a9baSMark Brown dev_dbg(&pdev->dev, "Probing EPE%d\n", id + 1); 8388267a9baSMark Brown 8394c60165dSAxel Lin dcdc = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_dcdc), GFP_KERNEL); 8405730aa57SSachin Kamat if (!dcdc) 8418267a9baSMark Brown return -ENOMEM; 8428267a9baSMark Brown 8438267a9baSMark Brown dcdc->wm831x = wm831x; 8448267a9baSMark Brown 8458267a9baSMark Brown /* For current parts this is correct; probably need to revisit 8468267a9baSMark Brown * in future. 8478267a9baSMark Brown */ 8488267a9baSMark Brown snprintf(dcdc->name, sizeof(dcdc->name), "EPE%d", id + 1); 8498267a9baSMark Brown dcdc->desc.name = dcdc->name; 8508267a9baSMark Brown dcdc->desc.id = id + WM831X_EPE_BASE; /* Offset in DCDC registers */ 8518267a9baSMark Brown dcdc->desc.ops = &wm831x_epe_ops; 8528267a9baSMark Brown dcdc->desc.type = REGULATOR_VOLTAGE; 8538267a9baSMark Brown dcdc->desc.owner = THIS_MODULE; 8543d138fccSMark Brown dcdc->desc.enable_reg = WM831X_DCDC_ENABLE; 8553d138fccSMark Brown dcdc->desc.enable_mask = 1 << dcdc->desc.id; 8568267a9baSMark Brown 857c172708dSMark Brown config.dev = pdev->dev.parent; 858b7ca8788SMark Brown if (pdata) 859c172708dSMark Brown config.init_data = pdata->epe[id]; 860c172708dSMark Brown config.driver_data = dcdc; 8613d138fccSMark Brown config.regmap = wm831x->regmap; 862c172708dSMark Brown 863d73b4cb7SMark Brown dcdc->regulator = devm_regulator_register(&pdev->dev, &dcdc->desc, 864d73b4cb7SMark Brown &config); 8658267a9baSMark Brown if (IS_ERR(dcdc->regulator)) { 8668267a9baSMark Brown ret = PTR_ERR(dcdc->regulator); 8678267a9baSMark Brown dev_err(wm831x->dev, "Failed to register EPE%d: %d\n", 8688267a9baSMark Brown id + 1, ret); 8698267a9baSMark Brown goto err; 8708267a9baSMark Brown } 8718267a9baSMark Brown 8728267a9baSMark Brown platform_set_drvdata(pdev, dcdc); 8738267a9baSMark Brown 8748267a9baSMark Brown return 0; 8758267a9baSMark Brown 8768267a9baSMark Brown err: 8778267a9baSMark Brown return ret; 8788267a9baSMark Brown } 8798267a9baSMark Brown 8808267a9baSMark Brown static struct platform_driver wm831x_epe_driver = { 8818267a9baSMark Brown .probe = wm831x_epe_probe, 8828267a9baSMark Brown .driver = { 8838267a9baSMark Brown .name = "wm831x-epe", 8848267a9baSMark Brown }, 8858267a9baSMark Brown }; 8868267a9baSMark Brown 887e4ee831fSMark Brown static int __init wm831x_dcdc_init(void) 888e4ee831fSMark Brown { 889e4ee831fSMark Brown int ret; 890e4ee831fSMark Brown ret = platform_driver_register(&wm831x_buckv_driver); 891e4ee831fSMark Brown if (ret != 0) 892e4ee831fSMark Brown pr_err("Failed to register WM831x BUCKV driver: %d\n", ret); 893e4ee831fSMark Brown 894e4ee831fSMark Brown ret = platform_driver_register(&wm831x_buckp_driver); 895e4ee831fSMark Brown if (ret != 0) 896e4ee831fSMark Brown pr_err("Failed to register WM831x BUCKP driver: %d\n", ret); 897e4ee831fSMark Brown 8981304850dSMark Brown ret = platform_driver_register(&wm831x_boostp_driver); 8991304850dSMark Brown if (ret != 0) 9001304850dSMark Brown pr_err("Failed to register WM831x BOOST driver: %d\n", ret); 9011304850dSMark Brown 9028267a9baSMark Brown ret = platform_driver_register(&wm831x_epe_driver); 9038267a9baSMark Brown if (ret != 0) 9048267a9baSMark Brown pr_err("Failed to register WM831x EPE driver: %d\n", ret); 9058267a9baSMark Brown 906e4ee831fSMark Brown return 0; 907e4ee831fSMark Brown } 908e4ee831fSMark Brown subsys_initcall(wm831x_dcdc_init); 909e4ee831fSMark Brown 910e4ee831fSMark Brown static void __exit wm831x_dcdc_exit(void) 911e4ee831fSMark Brown { 9128267a9baSMark Brown platform_driver_unregister(&wm831x_epe_driver); 9131304850dSMark Brown platform_driver_unregister(&wm831x_boostp_driver); 914e4ee831fSMark Brown platform_driver_unregister(&wm831x_buckp_driver); 915e4ee831fSMark Brown platform_driver_unregister(&wm831x_buckv_driver); 916e4ee831fSMark Brown } 917e4ee831fSMark Brown module_exit(wm831x_dcdc_exit); 918e4ee831fSMark Brown 919e4ee831fSMark Brown /* Module information */ 920e4ee831fSMark Brown MODULE_AUTHOR("Mark Brown"); 921e4ee831fSMark Brown MODULE_DESCRIPTION("WM831x DC-DC convertor driver"); 922e4ee831fSMark Brown MODULE_LICENSE("GPL"); 923e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckv"); 924e4ee831fSMark Brown MODULE_ALIAS("platform:wm831x-buckp"); 92543f1f216SAxel Lin MODULE_ALIAS("platform:wm831x-boostp"); 92624b43150SMark Brown MODULE_ALIAS("platform:wm831x-epe"); 927