Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 2 | /* |
| 3 | * LP8755 High Performance Power Management Unit : System Interface Driver |
| 4 | * (based on rev. 0.26) |
| 5 | * Copyright 2012 Texas Instruments |
| 6 | * |
| 7 | * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com> |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/irq.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/gpio.h> |
| 17 | #include <linux/regmap.h> |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/regulator/driver.h> |
| 20 | #include <linux/regulator/machine.h> |
| 21 | #include <linux/platform_data/lp8755.h> |
| 22 | |
| 23 | #define LP8755_REG_BUCK0 0x00 |
| 24 | #define LP8755_REG_BUCK1 0x03 |
| 25 | #define LP8755_REG_BUCK2 0x04 |
| 26 | #define LP8755_REG_BUCK3 0x01 |
| 27 | #define LP8755_REG_BUCK4 0x05 |
| 28 | #define LP8755_REG_BUCK5 0x02 |
| 29 | #define LP8755_REG_MAX 0xFF |
| 30 | |
| 31 | #define LP8755_BUCK_EN_M BIT(7) |
| 32 | #define LP8755_BUCK_LINEAR_OUT_MAX 0x76 |
| 33 | #define LP8755_BUCK_VOUT_M 0x7F |
| 34 | |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 35 | struct lp8755_mphase { |
| 36 | int nreg; |
| 37 | int buck_num[LP8755_BUCK_MAX]; |
| 38 | }; |
| 39 | |
| 40 | struct lp8755_chip { |
| 41 | struct device *dev; |
| 42 | struct regmap *regmap; |
| 43 | struct lp8755_platform_data *pdata; |
| 44 | |
| 45 | int irq; |
| 46 | unsigned int irqmask; |
| 47 | |
| 48 | int mphase; |
| 49 | struct regulator_dev *rdev[LP8755_BUCK_MAX]; |
| 50 | }; |
| 51 | |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 52 | static int lp8755_buck_enable_time(struct regulator_dev *rdev) |
| 53 | { |
| 54 | int ret; |
| 55 | unsigned int regval; |
| 56 | enum lp8755_bucks id = rdev_get_id(rdev); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 57 | |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 58 | ret = regmap_read(rdev->regmap, 0x12 + id, ®val); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 59 | if (ret < 0) { |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 60 | dev_err(&rdev->dev, "i2c access error %s\n", __func__); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 61 | return ret; |
| 62 | } |
| 63 | return (regval & 0xff) * 100; |
| 64 | } |
| 65 | |
| 66 | static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 67 | { |
| 68 | int ret; |
| 69 | unsigned int regbval = 0x0; |
| 70 | enum lp8755_bucks id = rdev_get_id(rdev); |
| 71 | struct lp8755_chip *pchip = rdev_get_drvdata(rdev); |
| 72 | |
| 73 | switch (mode) { |
| 74 | case REGULATOR_MODE_FAST: |
| 75 | /* forced pwm mode */ |
| 76 | regbval = (0x01 << id); |
| 77 | break; |
| 78 | case REGULATOR_MODE_NORMAL: |
| 79 | /* enable automatic pwm/pfm mode */ |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 80 | ret = regmap_update_bits(rdev->regmap, 0x08 + id, 0x20, 0x00); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 81 | if (ret < 0) |
| 82 | goto err_i2c; |
| 83 | break; |
| 84 | case REGULATOR_MODE_IDLE: |
| 85 | /* enable automatic pwm/pfm/lppfm mode */ |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 86 | ret = regmap_update_bits(rdev->regmap, 0x08 + id, 0x20, 0x20); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 87 | if (ret < 0) |
| 88 | goto err_i2c; |
| 89 | |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 90 | ret = regmap_update_bits(rdev->regmap, 0x10, 0x01, 0x01); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 91 | if (ret < 0) |
| 92 | goto err_i2c; |
| 93 | break; |
| 94 | default: |
| 95 | dev_err(pchip->dev, "Not supported buck mode %s\n", __func__); |
| 96 | /* forced pwm mode */ |
| 97 | regbval = (0x01 << id); |
| 98 | } |
| 99 | |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 100 | ret = regmap_update_bits(rdev->regmap, 0x06, 0x01 << id, regbval); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 101 | if (ret < 0) |
| 102 | goto err_i2c; |
| 103 | return ret; |
| 104 | err_i2c: |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 105 | dev_err(&rdev->dev, "i2c access error %s\n", __func__); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev) |
| 110 | { |
| 111 | int ret; |
| 112 | unsigned int regval; |
| 113 | enum lp8755_bucks id = rdev_get_id(rdev); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 114 | |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 115 | ret = regmap_read(rdev->regmap, 0x06, ®val); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 116 | if (ret < 0) |
| 117 | goto err_i2c; |
| 118 | |
| 119 | /* mode fast means forced pwm mode */ |
| 120 | if (regval & (0x01 << id)) |
| 121 | return REGULATOR_MODE_FAST; |
| 122 | |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 123 | ret = regmap_read(rdev->regmap, 0x08 + id, ®val); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 124 | if (ret < 0) |
| 125 | goto err_i2c; |
| 126 | |
| 127 | /* mode idle means automatic pwm/pfm/lppfm mode */ |
| 128 | if (regval & 0x20) |
| 129 | return REGULATOR_MODE_IDLE; |
| 130 | |
| 131 | /* mode normal means automatic pwm/pfm mode */ |
| 132 | return REGULATOR_MODE_NORMAL; |
| 133 | |
| 134 | err_i2c: |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 135 | dev_err(&rdev->dev, "i2c access error %s\n", __func__); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int lp8755_buck_set_ramp(struct regulator_dev *rdev, int ramp) |
| 140 | { |
| 141 | int ret; |
| 142 | unsigned int regval = 0x00; |
| 143 | enum lp8755_bucks id = rdev_get_id(rdev); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 144 | |
| 145 | /* uV/us */ |
| 146 | switch (ramp) { |
| 147 | case 0 ... 230: |
| 148 | regval = 0x07; |
| 149 | break; |
| 150 | case 231 ... 470: |
| 151 | regval = 0x06; |
| 152 | break; |
| 153 | case 471 ... 940: |
| 154 | regval = 0x05; |
| 155 | break; |
| 156 | case 941 ... 1900: |
| 157 | regval = 0x04; |
| 158 | break; |
| 159 | case 1901 ... 3800: |
| 160 | regval = 0x03; |
| 161 | break; |
| 162 | case 3801 ... 7500: |
| 163 | regval = 0x02; |
| 164 | break; |
| 165 | case 7501 ... 15000: |
| 166 | regval = 0x01; |
| 167 | break; |
| 168 | case 15001 ... 30000: |
| 169 | regval = 0x00; |
| 170 | break; |
| 171 | default: |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 172 | dev_err(&rdev->dev, |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 173 | "Not supported ramp value %d %s\n", ramp, __func__); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 177 | ret = regmap_update_bits(rdev->regmap, 0x07 + id, 0x07, regval); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 178 | if (ret < 0) |
| 179 | goto err_i2c; |
| 180 | return ret; |
| 181 | err_i2c: |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 182 | dev_err(&rdev->dev, "i2c access error %s\n", __func__); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 183 | return ret; |
| 184 | } |
| 185 | |
Bhumika Goyal | d42797a | 2017-01-28 19:41:30 +0530 | [diff] [blame] | 186 | static const struct regulator_ops lp8755_buck_ops = { |
Mark Brown | 56a942e | 2013-07-02 23:57:35 +0100 | [diff] [blame] | 187 | .map_voltage = regulator_map_voltage_linear, |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 188 | .list_voltage = regulator_list_voltage_linear, |
| 189 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 190 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 191 | .enable = regulator_enable_regmap, |
| 192 | .disable = regulator_disable_regmap, |
| 193 | .is_enabled = regulator_is_enabled_regmap, |
| 194 | .enable_time = lp8755_buck_enable_time, |
| 195 | .set_mode = lp8755_buck_set_mode, |
| 196 | .get_mode = lp8755_buck_get_mode, |
| 197 | .set_ramp_delay = lp8755_buck_set_ramp, |
| 198 | }; |
| 199 | |
| 200 | #define lp8755_rail(_id) "lp8755_buck"#_id |
| 201 | #define lp8755_buck_init(_id)\ |
| 202 | {\ |
| 203 | .constraints = {\ |
| 204 | .name = lp8755_rail(_id),\ |
| 205 | .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\ |
| 206 | .min_uV = 500000,\ |
| 207 | .max_uV = 1675000,\ |
| 208 | },\ |
| 209 | } |
| 210 | |
| 211 | static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = { |
Axel Lin | 510799e | 2013-01-07 10:28:31 +0800 | [diff] [blame] | 212 | [LP8755_BUCK0] = lp8755_buck_init(0), |
| 213 | [LP8755_BUCK1] = lp8755_buck_init(1), |
| 214 | [LP8755_BUCK2] = lp8755_buck_init(2), |
| 215 | [LP8755_BUCK3] = lp8755_buck_init(3), |
| 216 | [LP8755_BUCK4] = lp8755_buck_init(4), |
| 217 | [LP8755_BUCK5] = lp8755_buck_init(5), |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = { |
Axel Lin | 510799e | 2013-01-07 10:28:31 +0800 | [diff] [blame] | 221 | { 3, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK5 } }, |
| 222 | { 6, { LP8755_BUCK0, LP8755_BUCK1, LP8755_BUCK2, LP8755_BUCK3, |
| 223 | LP8755_BUCK4, LP8755_BUCK5 } }, |
| 224 | { 5, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK4, |
| 225 | LP8755_BUCK5} }, |
| 226 | { 4, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK4, LP8755_BUCK5} }, |
| 227 | { 3, { LP8755_BUCK0, LP8755_BUCK4, LP8755_BUCK5} }, |
| 228 | { 2, { LP8755_BUCK0, LP8755_BUCK5} }, |
| 229 | { 1, { LP8755_BUCK0} }, |
| 230 | { 2, { LP8755_BUCK0, LP8755_BUCK3} }, |
| 231 | { 4, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK5} }, |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | static int lp8755_init_data(struct lp8755_chip *pchip) |
| 235 | { |
| 236 | unsigned int regval; |
| 237 | int ret, icnt, buck_num; |
| 238 | struct lp8755_platform_data *pdata = pchip->pdata; |
| 239 | |
| 240 | /* read back muti-phase configuration */ |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 241 | ret = regmap_read(pchip->regmap, 0x3D, ®val); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 242 | if (ret < 0) |
| 243 | goto out_i2c_error; |
Axel Lin | cad877e | 2012-12-26 11:56:37 +0800 | [diff] [blame] | 244 | pchip->mphase = regval & 0x0F; |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 245 | |
| 246 | /* set default data based on multi-phase config */ |
| 247 | for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) { |
| 248 | buck_num = mphase_buck[pchip->mphase].buck_num[icnt]; |
| 249 | pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num]; |
| 250 | } |
| 251 | return ret; |
| 252 | |
| 253 | out_i2c_error: |
Colin Ian King | 80aec6f | 2017-05-10 09:30:20 +0100 | [diff] [blame] | 254 | dev_err(pchip->dev, "i2c access error %s\n", __func__); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | #define lp8755_buck_desc(_id)\ |
| 259 | {\ |
| 260 | .name = lp8755_rail(_id),\ |
| 261 | .id = LP8755_BUCK##_id,\ |
| 262 | .ops = &lp8755_buck_ops,\ |
| 263 | .n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\ |
| 264 | .uV_step = 10000,\ |
| 265 | .min_uV = 500000,\ |
| 266 | .type = REGULATOR_VOLTAGE,\ |
| 267 | .owner = THIS_MODULE,\ |
| 268 | .enable_reg = LP8755_REG_BUCK##_id,\ |
| 269 | .enable_mask = LP8755_BUCK_EN_M,\ |
| 270 | .vsel_reg = LP8755_REG_BUCK##_id,\ |
| 271 | .vsel_mask = LP8755_BUCK_VOUT_M,\ |
| 272 | } |
| 273 | |
Axel Lin | 367e90d | 2019-01-26 11:39:04 +0800 | [diff] [blame] | 274 | static const struct regulator_desc lp8755_regulators[] = { |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 275 | lp8755_buck_desc(0), |
| 276 | lp8755_buck_desc(1), |
| 277 | lp8755_buck_desc(2), |
| 278 | lp8755_buck_desc(3), |
| 279 | lp8755_buck_desc(4), |
| 280 | lp8755_buck_desc(5), |
| 281 | }; |
| 282 | |
| 283 | static int lp8755_regulator_init(struct lp8755_chip *pchip) |
| 284 | { |
| 285 | int ret, icnt, buck_num; |
| 286 | struct lp8755_platform_data *pdata = pchip->pdata; |
| 287 | struct regulator_config rconfig = { }; |
| 288 | |
| 289 | rconfig.regmap = pchip->regmap; |
| 290 | rconfig.dev = pchip->dev; |
| 291 | rconfig.driver_data = pchip; |
| 292 | |
| 293 | for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) { |
| 294 | buck_num = mphase_buck[pchip->mphase].buck_num[icnt]; |
| 295 | rconfig.init_data = pdata->buck_data[buck_num]; |
| 296 | rconfig.of_node = pchip->dev->of_node; |
| 297 | pchip->rdev[buck_num] = |
Himangi Saraogi | 5713525 | 2014-07-07 21:12:14 +0530 | [diff] [blame] | 298 | devm_regulator_register(pchip->dev, |
| 299 | &lp8755_regulators[buck_num], &rconfig); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 300 | if (IS_ERR(pchip->rdev[buck_num])) { |
| 301 | ret = PTR_ERR(pchip->rdev[buck_num]); |
Axel Lin | a1a41ab | 2012-12-25 10:06:20 +0800 | [diff] [blame] | 302 | pchip->rdev[buck_num] = NULL; |
| 303 | dev_err(pchip->dev, "regulator init failed: buck %d\n", |
| 304 | buck_num); |
Himangi Saraogi | 5713525 | 2014-07-07 21:12:14 +0530 | [diff] [blame] | 305 | return ret; |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | return 0; |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static irqreturn_t lp8755_irq_handler(int irq, void *data) |
| 313 | { |
| 314 | int ret, icnt; |
| 315 | unsigned int flag0, flag1; |
| 316 | struct lp8755_chip *pchip = data; |
| 317 | |
| 318 | /* read flag0 register */ |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 319 | ret = regmap_read(pchip->regmap, 0x0D, &flag0); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 320 | if (ret < 0) |
| 321 | goto err_i2c; |
| 322 | /* clear flag register to pull up int. pin */ |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 323 | ret = regmap_write(pchip->regmap, 0x0D, 0x00); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 324 | if (ret < 0) |
| 325 | goto err_i2c; |
| 326 | |
| 327 | /* sent power fault detection event to specific regulator */ |
Axel Lin | 1200c60 | 2013-01-26 13:19:47 +0800 | [diff] [blame] | 328 | for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++) |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 329 | if ((flag0 & (0x4 << icnt)) |
| 330 | && (pchip->irqmask & (0x04 << icnt)) |
Steve Twiss | 89b2758c | 2019-02-26 15:32:18 +0000 | [diff] [blame] | 331 | && (pchip->rdev[icnt] != NULL)) { |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 332 | regulator_notifier_call_chain(pchip->rdev[icnt], |
| 333 | LP8755_EVENT_PWR_FAULT, |
| 334 | NULL); |
Steve Twiss | 89b2758c | 2019-02-26 15:32:18 +0000 | [diff] [blame] | 335 | } |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 336 | |
| 337 | /* read flag1 register */ |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 338 | ret = regmap_read(pchip->regmap, 0x0E, &flag1); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 339 | if (ret < 0) |
| 340 | goto err_i2c; |
| 341 | /* clear flag register to pull up int. pin */ |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 342 | ret = regmap_write(pchip->regmap, 0x0E, 0x00); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 343 | if (ret < 0) |
| 344 | goto err_i2c; |
| 345 | |
Krzysztof Kozlowski | 48f1b4e | 2019-01-08 13:12:33 +0100 | [diff] [blame] | 346 | /* send OCP event to all regulator devices */ |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 347 | if ((flag1 & 0x01) && (pchip->irqmask & 0x01)) |
| 348 | for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++) |
Steve Twiss | 89b2758c | 2019-02-26 15:32:18 +0000 | [diff] [blame] | 349 | if (pchip->rdev[icnt] != NULL) { |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 350 | regulator_notifier_call_chain(pchip->rdev[icnt], |
| 351 | LP8755_EVENT_OCP, |
| 352 | NULL); |
Steve Twiss | 89b2758c | 2019-02-26 15:32:18 +0000 | [diff] [blame] | 353 | } |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 354 | |
Krzysztof Kozlowski | 48f1b4e | 2019-01-08 13:12:33 +0100 | [diff] [blame] | 355 | /* send OVP event to all regulator devices */ |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 356 | if ((flag1 & 0x02) && (pchip->irqmask & 0x02)) |
| 357 | for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++) |
Steve Twiss | 89b2758c | 2019-02-26 15:32:18 +0000 | [diff] [blame] | 358 | if (pchip->rdev[icnt] != NULL) { |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 359 | regulator_notifier_call_chain(pchip->rdev[icnt], |
| 360 | LP8755_EVENT_OVP, |
| 361 | NULL); |
Steve Twiss | 89b2758c | 2019-02-26 15:32:18 +0000 | [diff] [blame] | 362 | } |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 363 | return IRQ_HANDLED; |
| 364 | |
| 365 | err_i2c: |
Colin Ian King | 80aec6f | 2017-05-10 09:30:20 +0100 | [diff] [blame] | 366 | dev_err(pchip->dev, "i2c access error %s\n", __func__); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 367 | return IRQ_NONE; |
| 368 | } |
| 369 | |
| 370 | static int lp8755_int_config(struct lp8755_chip *pchip) |
| 371 | { |
| 372 | int ret; |
| 373 | unsigned int regval; |
| 374 | |
| 375 | if (pchip->irq == 0) { |
| 376 | dev_warn(pchip->dev, "not use interrupt : %s\n", __func__); |
| 377 | return 0; |
| 378 | } |
| 379 | |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 380 | ret = regmap_read(pchip->regmap, 0x0F, ®val); |
Axel Lin | 840499a | 2015-05-31 14:06:46 +0800 | [diff] [blame] | 381 | if (ret < 0) { |
Colin Ian King | 80aec6f | 2017-05-10 09:30:20 +0100 | [diff] [blame] | 382 | dev_err(pchip->dev, "i2c access error %s\n", __func__); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 383 | return ret; |
Axel Lin | 840499a | 2015-05-31 14:06:46 +0800 | [diff] [blame] | 384 | } |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 385 | |
Axel Lin | 840499a | 2015-05-31 14:06:46 +0800 | [diff] [blame] | 386 | pchip->irqmask = regval; |
| 387 | return devm_request_threaded_irq(pchip->dev, pchip->irq, NULL, |
| 388 | lp8755_irq_handler, |
| 389 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 390 | "lp8755-irq", pchip); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static const struct regmap_config lp8755_regmap = { |
| 394 | .reg_bits = 8, |
| 395 | .val_bits = 8, |
| 396 | .max_register = LP8755_REG_MAX, |
| 397 | }; |
| 398 | |
| 399 | static int lp8755_probe(struct i2c_client *client, |
| 400 | const struct i2c_device_id *id) |
| 401 | { |
| 402 | int ret, icnt; |
| 403 | struct lp8755_chip *pchip; |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 404 | struct lp8755_platform_data *pdata = dev_get_platdata(&client->dev); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 405 | |
| 406 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { |
| 407 | dev_err(&client->dev, "i2c functionality check fail.\n"); |
| 408 | return -EOPNOTSUPP; |
| 409 | } |
| 410 | |
| 411 | pchip = devm_kzalloc(&client->dev, |
| 412 | sizeof(struct lp8755_chip), GFP_KERNEL); |
| 413 | if (!pchip) |
| 414 | return -ENOMEM; |
| 415 | |
| 416 | pchip->dev = &client->dev; |
| 417 | pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap); |
| 418 | if (IS_ERR(pchip->regmap)) { |
| 419 | ret = PTR_ERR(pchip->regmap); |
| 420 | dev_err(&client->dev, "fail to allocate regmap %d\n", ret); |
| 421 | return ret; |
| 422 | } |
| 423 | i2c_set_clientdata(client, pchip); |
| 424 | |
| 425 | if (pdata != NULL) { |
| 426 | pchip->pdata = pdata; |
| 427 | pchip->mphase = pdata->mphase; |
| 428 | } else { |
| 429 | pchip->pdata = devm_kzalloc(pchip->dev, |
| 430 | sizeof(struct lp8755_platform_data), |
| 431 | GFP_KERNEL); |
| 432 | if (!pchip->pdata) |
| 433 | return -ENOMEM; |
| 434 | ret = lp8755_init_data(pchip); |
Axel Lin | 240a529 | 2013-01-12 14:58:35 +0800 | [diff] [blame] | 435 | if (ret < 0) { |
| 436 | dev_err(&client->dev, "fail to initialize chip\n"); |
| 437 | return ret; |
| 438 | } |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | ret = lp8755_regulator_init(pchip); |
Axel Lin | 240a529 | 2013-01-12 14:58:35 +0800 | [diff] [blame] | 442 | if (ret < 0) { |
| 443 | dev_err(&client->dev, "fail to initialize regulators\n"); |
Himangi Saraogi | 5713525 | 2014-07-07 21:12:14 +0530 | [diff] [blame] | 444 | goto err; |
Axel Lin | 240a529 | 2013-01-12 14:58:35 +0800 | [diff] [blame] | 445 | } |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 446 | |
| 447 | pchip->irq = client->irq; |
| 448 | ret = lp8755_int_config(pchip); |
Axel Lin | 240a529 | 2013-01-12 14:58:35 +0800 | [diff] [blame] | 449 | if (ret < 0) { |
| 450 | dev_err(&client->dev, "fail to irq config\n"); |
Himangi Saraogi | 5713525 | 2014-07-07 21:12:14 +0530 | [diff] [blame] | 451 | goto err; |
Axel Lin | 240a529 | 2013-01-12 14:58:35 +0800 | [diff] [blame] | 452 | } |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 453 | |
| 454 | return ret; |
| 455 | |
Himangi Saraogi | 5713525 | 2014-07-07 21:12:14 +0530 | [diff] [blame] | 456 | err: |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 457 | /* output disable */ |
Axel Lin | 1200c60 | 2013-01-26 13:19:47 +0800 | [diff] [blame] | 458 | for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++) |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 459 | regmap_write(pchip->regmap, icnt, 0x00); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 460 | |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | static int lp8755_remove(struct i2c_client *client) |
| 465 | { |
| 466 | int icnt; |
| 467 | struct lp8755_chip *pchip = i2c_get_clientdata(client); |
| 468 | |
Axel Lin | 1200c60 | 2013-01-26 13:19:47 +0800 | [diff] [blame] | 469 | for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++) |
Axel Lin | 4cf1273 | 2020-08-02 17:06:38 +0800 | [diff] [blame] | 470 | regmap_write(pchip->regmap, icnt, 0x00); |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 471 | |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static const struct i2c_device_id lp8755_id[] = { |
| 476 | {LP8755_NAME, 0}, |
| 477 | {} |
| 478 | }; |
| 479 | |
| 480 | MODULE_DEVICE_TABLE(i2c, lp8755_id); |
| 481 | |
| 482 | static struct i2c_driver lp8755_i2c_driver = { |
| 483 | .driver = { |
| 484 | .name = LP8755_NAME, |
| 485 | }, |
| 486 | .probe = lp8755_probe, |
Axel Lin | a1a41ab | 2012-12-25 10:06:20 +0800 | [diff] [blame] | 487 | .remove = lp8755_remove, |
Daniel Jeong | b59320c | 2012-12-17 10:24:06 +0900 | [diff] [blame] | 488 | .id_table = lp8755_id, |
| 489 | }; |
| 490 | |
| 491 | static int __init lp8755_init(void) |
| 492 | { |
| 493 | return i2c_add_driver(&lp8755_i2c_driver); |
| 494 | } |
| 495 | |
| 496 | subsys_initcall(lp8755_init); |
| 497 | |
| 498 | static void __exit lp8755_exit(void) |
| 499 | { |
| 500 | i2c_del_driver(&lp8755_i2c_driver); |
| 501 | } |
| 502 | |
| 503 | module_exit(lp8755_exit); |
| 504 | |
| 505 | MODULE_DESCRIPTION("Texas Instruments lp8755 driver"); |
| 506 | MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>"); |
| 507 | MODULE_LICENSE("GPL v2"); |