Krzysztof Kozlowski | 5e9384c | 2018-08-07 18:18:25 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // max8997.c - Regulator driver for the Maxim 8997/8966 |
| 4 | // |
| 5 | // Copyright (C) 2011 Samsung Electronics |
| 6 | // MyungJoo Ham <myungjoo.ham@samsung.com> |
| 7 | // |
| 8 | // This driver is based on max8998.c |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 9 | |
| 10 | #include <linux/bug.h> |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 11 | #include <linux/err.h> |
| 12 | #include <linux/gpio.h> |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 13 | #include <linux/of_gpio.h> |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Paul Gortmaker | 65602c3 | 2011-07-17 16:28:23 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/regulator/driver.h> |
| 18 | #include <linux/regulator/machine.h> |
| 19 | #include <linux/mfd/max8997.h> |
| 20 | #include <linux/mfd/max8997-private.h> |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 21 | #include <linux/regulator/of_regulator.h> |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 22 | |
| 23 | struct max8997_data { |
| 24 | struct device *dev; |
| 25 | struct max8997_dev *iodev; |
| 26 | int num_regulators; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 27 | int ramp_delay; /* in mV/us */ |
| 28 | |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 29 | bool buck1_gpiodvs; |
| 30 | bool buck2_gpiodvs; |
| 31 | bool buck5_gpiodvs; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 32 | u8 buck1_vol[8]; |
| 33 | u8 buck2_vol[8]; |
| 34 | u8 buck5_vol[8]; |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 35 | int buck125_gpios[3]; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 36 | int buck125_gpioindex; |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 37 | bool ignore_gpiodvs_side_effect; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 38 | |
| 39 | u8 saved_states[MAX8997_REG_MAX]; |
| 40 | }; |
| 41 | |
Axel Lin | b79ca05 | 2012-12-26 16:30:44 +0800 | [diff] [blame] | 42 | static const unsigned int safeoutvolt[] = { |
| 43 | 4850000, |
| 44 | 4900000, |
| 45 | 4950000, |
| 46 | 3300000, |
| 47 | }; |
| 48 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 49 | static inline void max8997_set_gpio(struct max8997_data *max8997) |
| 50 | { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 51 | int set3 = (max8997->buck125_gpioindex) & 0x1; |
| 52 | int set2 = ((max8997->buck125_gpioindex) >> 1) & 0x1; |
| 53 | int set1 = ((max8997->buck125_gpioindex) >> 2) & 0x1; |
| 54 | |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 55 | gpio_set_value(max8997->buck125_gpios[0], set1); |
| 56 | gpio_set_value(max8997->buck125_gpios[1], set2); |
| 57 | gpio_set_value(max8997->buck125_gpios[2], set3); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | struct voltage_map_desc { |
| 61 | int min; |
| 62 | int max; |
| 63 | int step; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 64 | }; |
| 65 | |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 66 | /* Voltage maps in uV */ |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 67 | static const struct voltage_map_desc ldo_voltage_map_desc = { |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 68 | .min = 800000, .max = 3950000, .step = 50000, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 69 | }; /* LDO1 ~ 18, 21 all */ |
| 70 | |
| 71 | static const struct voltage_map_desc buck1245_voltage_map_desc = { |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 72 | .min = 650000, .max = 2225000, .step = 25000, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 73 | }; /* Buck1, 2, 4, 5 */ |
| 74 | |
| 75 | static const struct voltage_map_desc buck37_voltage_map_desc = { |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 76 | .min = 750000, .max = 3900000, .step = 50000, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 77 | }; /* Buck3, 7 */ |
| 78 | |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 79 | /* current map in uA */ |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 80 | static const struct voltage_map_desc charger_current_map_desc = { |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 81 | .min = 200000, .max = 950000, .step = 50000, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static const struct voltage_map_desc topoff_current_map_desc = { |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 85 | .min = 50000, .max = 200000, .step = 10000, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static const struct voltage_map_desc *reg_voltage_map[] = { |
| 89 | [MAX8997_LDO1] = &ldo_voltage_map_desc, |
| 90 | [MAX8997_LDO2] = &ldo_voltage_map_desc, |
| 91 | [MAX8997_LDO3] = &ldo_voltage_map_desc, |
| 92 | [MAX8997_LDO4] = &ldo_voltage_map_desc, |
| 93 | [MAX8997_LDO5] = &ldo_voltage_map_desc, |
| 94 | [MAX8997_LDO6] = &ldo_voltage_map_desc, |
| 95 | [MAX8997_LDO7] = &ldo_voltage_map_desc, |
| 96 | [MAX8997_LDO8] = &ldo_voltage_map_desc, |
| 97 | [MAX8997_LDO9] = &ldo_voltage_map_desc, |
| 98 | [MAX8997_LDO10] = &ldo_voltage_map_desc, |
| 99 | [MAX8997_LDO11] = &ldo_voltage_map_desc, |
| 100 | [MAX8997_LDO12] = &ldo_voltage_map_desc, |
| 101 | [MAX8997_LDO13] = &ldo_voltage_map_desc, |
| 102 | [MAX8997_LDO14] = &ldo_voltage_map_desc, |
| 103 | [MAX8997_LDO15] = &ldo_voltage_map_desc, |
| 104 | [MAX8997_LDO16] = &ldo_voltage_map_desc, |
| 105 | [MAX8997_LDO17] = &ldo_voltage_map_desc, |
| 106 | [MAX8997_LDO18] = &ldo_voltage_map_desc, |
| 107 | [MAX8997_LDO21] = &ldo_voltage_map_desc, |
| 108 | [MAX8997_BUCK1] = &buck1245_voltage_map_desc, |
| 109 | [MAX8997_BUCK2] = &buck1245_voltage_map_desc, |
| 110 | [MAX8997_BUCK3] = &buck37_voltage_map_desc, |
| 111 | [MAX8997_BUCK4] = &buck1245_voltage_map_desc, |
| 112 | [MAX8997_BUCK5] = &buck1245_voltage_map_desc, |
| 113 | [MAX8997_BUCK6] = NULL, |
| 114 | [MAX8997_BUCK7] = &buck37_voltage_map_desc, |
| 115 | [MAX8997_EN32KHZ_AP] = NULL, |
| 116 | [MAX8997_EN32KHZ_CP] = NULL, |
| 117 | [MAX8997_ENVICHG] = NULL, |
| 118 | [MAX8997_ESAFEOUT1] = NULL, |
| 119 | [MAX8997_ESAFEOUT2] = NULL, |
| 120 | [MAX8997_CHARGER_CV] = NULL, |
| 121 | [MAX8997_CHARGER] = &charger_current_map_desc, |
| 122 | [MAX8997_CHARGER_TOPOFF] = &topoff_current_map_desc, |
| 123 | }; |
| 124 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 125 | static int max8997_list_voltage_charger_cv(struct regulator_dev *rdev, |
| 126 | unsigned int selector) |
| 127 | { |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 128 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 129 | |
| 130 | if (rid != MAX8997_CHARGER_CV) |
| 131 | goto err; |
| 132 | |
| 133 | switch (selector) { |
| 134 | case 0x00: |
| 135 | return 4200000; |
| 136 | case 0x01 ... 0x0E: |
| 137 | return 4000000 + 20000 * (selector - 0x01); |
| 138 | case 0x0F: |
| 139 | return 4350000; |
| 140 | default: |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | err: |
| 144 | return -EINVAL; |
| 145 | } |
| 146 | |
| 147 | static int max8997_list_voltage(struct regulator_dev *rdev, |
| 148 | unsigned int selector) |
| 149 | { |
| 150 | const struct voltage_map_desc *desc; |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 151 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 152 | int val; |
| 153 | |
Dan Carpenter | 1aa1b91 | 2018-07-06 11:37:18 +0300 | [diff] [blame] | 154 | if (rid < 0 || rid >= ARRAY_SIZE(reg_voltage_map)) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 155 | return -EINVAL; |
| 156 | |
| 157 | desc = reg_voltage_map[rid]; |
| 158 | if (desc == NULL) |
| 159 | return -EINVAL; |
| 160 | |
| 161 | val = desc->min + desc->step * selector; |
| 162 | if (val > desc->max) |
| 163 | return -EINVAL; |
| 164 | |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 165 | return val; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static int max8997_get_enable_register(struct regulator_dev *rdev, |
| 169 | int *reg, int *mask, int *pattern) |
| 170 | { |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 171 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 172 | |
| 173 | switch (rid) { |
| 174 | case MAX8997_LDO1 ... MAX8997_LDO21: |
| 175 | *reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1); |
| 176 | *mask = 0xC0; |
| 177 | *pattern = 0xC0; |
| 178 | break; |
| 179 | case MAX8997_BUCK1: |
| 180 | *reg = MAX8997_REG_BUCK1CTRL; |
| 181 | *mask = 0x01; |
| 182 | *pattern = 0x01; |
| 183 | break; |
| 184 | case MAX8997_BUCK2: |
| 185 | *reg = MAX8997_REG_BUCK2CTRL; |
| 186 | *mask = 0x01; |
| 187 | *pattern = 0x01; |
| 188 | break; |
| 189 | case MAX8997_BUCK3: |
| 190 | *reg = MAX8997_REG_BUCK3CTRL; |
| 191 | *mask = 0x01; |
| 192 | *pattern = 0x01; |
| 193 | break; |
| 194 | case MAX8997_BUCK4: |
| 195 | *reg = MAX8997_REG_BUCK4CTRL; |
| 196 | *mask = 0x01; |
| 197 | *pattern = 0x01; |
| 198 | break; |
| 199 | case MAX8997_BUCK5: |
| 200 | *reg = MAX8997_REG_BUCK5CTRL; |
| 201 | *mask = 0x01; |
| 202 | *pattern = 0x01; |
| 203 | break; |
| 204 | case MAX8997_BUCK6: |
| 205 | *reg = MAX8997_REG_BUCK6CTRL; |
| 206 | *mask = 0x01; |
| 207 | *pattern = 0x01; |
| 208 | break; |
| 209 | case MAX8997_BUCK7: |
| 210 | *reg = MAX8997_REG_BUCK7CTRL; |
| 211 | *mask = 0x01; |
| 212 | *pattern = 0x01; |
| 213 | break; |
| 214 | case MAX8997_EN32KHZ_AP ... MAX8997_EN32KHZ_CP: |
| 215 | *reg = MAX8997_REG_MAINCON1; |
| 216 | *mask = 0x01 << (rid - MAX8997_EN32KHZ_AP); |
| 217 | *pattern = 0x01 << (rid - MAX8997_EN32KHZ_AP); |
| 218 | break; |
| 219 | case MAX8997_ENVICHG: |
| 220 | *reg = MAX8997_REG_MBCCTRL1; |
| 221 | *mask = 0x80; |
| 222 | *pattern = 0x80; |
| 223 | break; |
| 224 | case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2: |
| 225 | *reg = MAX8997_REG_SAFEOUTCTRL; |
| 226 | *mask = 0x40 << (rid - MAX8997_ESAFEOUT1); |
| 227 | *pattern = 0x40 << (rid - MAX8997_ESAFEOUT1); |
| 228 | break; |
| 229 | case MAX8997_CHARGER: |
| 230 | *reg = MAX8997_REG_MBCCTRL2; |
| 231 | *mask = 0x40; |
| 232 | *pattern = 0x40; |
| 233 | break; |
| 234 | default: |
| 235 | /* Not controllable or not exists */ |
| 236 | return -EINVAL; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static int max8997_reg_is_enabled(struct regulator_dev *rdev) |
| 243 | { |
| 244 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
| 245 | struct i2c_client *i2c = max8997->iodev->i2c; |
| 246 | int ret, reg, mask, pattern; |
| 247 | u8 val; |
| 248 | |
| 249 | ret = max8997_get_enable_register(rdev, ®, &mask, &pattern); |
Axel Lin | c245c08 | 2012-04-17 23:48:27 +0800 | [diff] [blame] | 250 | if (ret) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 251 | return ret; |
| 252 | |
| 253 | ret = max8997_read_reg(i2c, reg, &val); |
| 254 | if (ret) |
| 255 | return ret; |
| 256 | |
| 257 | return (val & mask) == pattern; |
| 258 | } |
| 259 | |
| 260 | static int max8997_reg_enable(struct regulator_dev *rdev) |
| 261 | { |
| 262 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
| 263 | struct i2c_client *i2c = max8997->iodev->i2c; |
| 264 | int ret, reg, mask, pattern; |
| 265 | |
| 266 | ret = max8997_get_enable_register(rdev, ®, &mask, &pattern); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | |
| 270 | return max8997_update_reg(i2c, reg, pattern, mask); |
| 271 | } |
| 272 | |
| 273 | static int max8997_reg_disable(struct regulator_dev *rdev) |
| 274 | { |
| 275 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
| 276 | struct i2c_client *i2c = max8997->iodev->i2c; |
| 277 | int ret, reg, mask, pattern; |
| 278 | |
| 279 | ret = max8997_get_enable_register(rdev, ®, &mask, &pattern); |
| 280 | if (ret) |
| 281 | return ret; |
| 282 | |
| 283 | return max8997_update_reg(i2c, reg, ~pattern, mask); |
| 284 | } |
| 285 | |
| 286 | static int max8997_get_voltage_register(struct regulator_dev *rdev, |
| 287 | int *_reg, int *_shift, int *_mask) |
| 288 | { |
Axel Lin | 6f43c38 | 2012-03-13 11:22:41 +0800 | [diff] [blame] | 289 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 290 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 291 | int reg, shift = 0, mask = 0x3f; |
| 292 | |
| 293 | switch (rid) { |
| 294 | case MAX8997_LDO1 ... MAX8997_LDO21: |
| 295 | reg = MAX8997_REG_LDO1CTRL + (rid - MAX8997_LDO1); |
| 296 | break; |
| 297 | case MAX8997_BUCK1: |
| 298 | reg = MAX8997_REG_BUCK1DVS1; |
Axel Lin | 6f43c38 | 2012-03-13 11:22:41 +0800 | [diff] [blame] | 299 | if (max8997->buck1_gpiodvs) |
| 300 | reg += max8997->buck125_gpioindex; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 301 | break; |
| 302 | case MAX8997_BUCK2: |
| 303 | reg = MAX8997_REG_BUCK2DVS1; |
Axel Lin | 6f43c38 | 2012-03-13 11:22:41 +0800 | [diff] [blame] | 304 | if (max8997->buck2_gpiodvs) |
| 305 | reg += max8997->buck125_gpioindex; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 306 | break; |
| 307 | case MAX8997_BUCK3: |
| 308 | reg = MAX8997_REG_BUCK3DVS; |
| 309 | break; |
| 310 | case MAX8997_BUCK4: |
| 311 | reg = MAX8997_REG_BUCK4DVS; |
| 312 | break; |
| 313 | case MAX8997_BUCK5: |
| 314 | reg = MAX8997_REG_BUCK5DVS1; |
Axel Lin | 6f43c38 | 2012-03-13 11:22:41 +0800 | [diff] [blame] | 315 | if (max8997->buck5_gpiodvs) |
| 316 | reg += max8997->buck125_gpioindex; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 317 | break; |
| 318 | case MAX8997_BUCK7: |
| 319 | reg = MAX8997_REG_BUCK7DVS; |
| 320 | break; |
| 321 | case MAX8997_ESAFEOUT1 ... MAX8997_ESAFEOUT2: |
| 322 | reg = MAX8997_REG_SAFEOUTCTRL; |
| 323 | shift = (rid == MAX8997_ESAFEOUT2) ? 2 : 0; |
| 324 | mask = 0x3; |
| 325 | break; |
| 326 | case MAX8997_CHARGER_CV: |
| 327 | reg = MAX8997_REG_MBCCTRL3; |
| 328 | shift = 0; |
| 329 | mask = 0xf; |
| 330 | break; |
| 331 | case MAX8997_CHARGER: |
| 332 | reg = MAX8997_REG_MBCCTRL4; |
| 333 | shift = 0; |
| 334 | mask = 0xf; |
| 335 | break; |
| 336 | case MAX8997_CHARGER_TOPOFF: |
| 337 | reg = MAX8997_REG_MBCCTRL5; |
| 338 | shift = 0; |
| 339 | mask = 0xf; |
| 340 | break; |
| 341 | default: |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | |
| 345 | *_reg = reg; |
| 346 | *_shift = shift; |
| 347 | *_mask = mask; |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 352 | static int max8997_get_voltage_sel(struct regulator_dev *rdev) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 353 | { |
| 354 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 355 | struct i2c_client *i2c = max8997->iodev->i2c; |
| 356 | int reg, shift, mask, ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 357 | u8 val; |
| 358 | |
| 359 | ret = max8997_get_voltage_register(rdev, ®, &shift, &mask); |
| 360 | if (ret) |
| 361 | return ret; |
| 362 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 363 | ret = max8997_read_reg(i2c, reg, &val); |
| 364 | if (ret) |
| 365 | return ret; |
| 366 | |
| 367 | val >>= shift; |
| 368 | val &= mask; |
| 369 | |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 370 | return val; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static inline int max8997_get_voltage_proper_val( |
| 374 | const struct voltage_map_desc *desc, |
| 375 | int min_vol, int max_vol) |
| 376 | { |
Axel Lin | 2358b77 | 2012-04-10 14:20:03 +0800 | [diff] [blame] | 377 | int i; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 378 | |
| 379 | if (desc == NULL) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | if (max_vol < desc->min || min_vol > desc->max) |
| 383 | return -EINVAL; |
| 384 | |
Axel Lin | 2358b77 | 2012-04-10 14:20:03 +0800 | [diff] [blame] | 385 | if (min_vol < desc->min) |
| 386 | min_vol = desc->min; |
| 387 | |
| 388 | i = DIV_ROUND_UP(min_vol - desc->min, desc->step); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 389 | |
| 390 | if (desc->min + desc->step * i > max_vol) |
| 391 | return -EINVAL; |
| 392 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 393 | return i; |
| 394 | } |
| 395 | |
| 396 | static int max8997_set_voltage_charger_cv(struct regulator_dev *rdev, |
| 397 | int min_uV, int max_uV, unsigned *selector) |
| 398 | { |
| 399 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
| 400 | struct i2c_client *i2c = max8997->iodev->i2c; |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 401 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 402 | int lb, ub; |
| 403 | int reg, shift = 0, mask, ret = 0; |
| 404 | u8 val = 0x0; |
| 405 | |
| 406 | if (rid != MAX8997_CHARGER_CV) |
| 407 | return -EINVAL; |
| 408 | |
| 409 | ret = max8997_get_voltage_register(rdev, ®, &shift, &mask); |
| 410 | if (ret) |
| 411 | return ret; |
| 412 | |
| 413 | if (max_uV < 4000000 || min_uV > 4350000) |
| 414 | return -EINVAL; |
| 415 | |
MyungJoo Ham | f74521c | 2017-05-08 05:45:44 +0000 | [diff] [blame] | 416 | if (min_uV <= 4000000) |
| 417 | val = 0x1; |
| 418 | else if (min_uV <= 4200000 && max_uV >= 4200000) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 419 | val = 0x0; |
| 420 | else { |
| 421 | lb = (min_uV - 4000001) / 20000 + 2; |
| 422 | ub = (max_uV - 4000000) / 20000 + 1; |
| 423 | |
| 424 | if (lb > ub) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | if (lb < 0xf) |
| 428 | val = lb; |
| 429 | else { |
| 430 | if (ub >= 0xf) |
| 431 | val = 0xf; |
| 432 | else |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | *selector = val; |
| 438 | |
| 439 | ret = max8997_update_reg(i2c, reg, val << shift, mask); |
| 440 | |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * For LDO1 ~ LDO21, BUCK1~5, BUCK7, CHARGER, CHARGER_TOPOFF |
| 446 | * BUCK1, 2, and 5 are available if they are not controlled by gpio |
| 447 | */ |
| 448 | static int max8997_set_voltage_ldobuck(struct regulator_dev *rdev, |
| 449 | int min_uV, int max_uV, unsigned *selector) |
| 450 | { |
| 451 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
| 452 | struct i2c_client *i2c = max8997->iodev->i2c; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 453 | const struct voltage_map_desc *desc; |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 454 | int rid = rdev_get_id(rdev); |
Axel Lin | 62bc4d4 | 2012-04-11 22:58:03 +0800 | [diff] [blame] | 455 | int i, reg, shift, mask, ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 456 | |
| 457 | switch (rid) { |
| 458 | case MAX8997_LDO1 ... MAX8997_LDO21: |
| 459 | break; |
| 460 | case MAX8997_BUCK1 ... MAX8997_BUCK5: |
| 461 | break; |
| 462 | case MAX8997_BUCK6: |
| 463 | return -EINVAL; |
| 464 | case MAX8997_BUCK7: |
| 465 | break; |
| 466 | case MAX8997_CHARGER: |
| 467 | break; |
| 468 | case MAX8997_CHARGER_TOPOFF: |
| 469 | break; |
| 470 | default: |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
| 474 | desc = reg_voltage_map[rid]; |
| 475 | |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 476 | i = max8997_get_voltage_proper_val(desc, min_uV, max_uV); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 477 | if (i < 0) |
| 478 | return i; |
| 479 | |
| 480 | ret = max8997_get_voltage_register(rdev, ®, &shift, &mask); |
| 481 | if (ret) |
| 482 | return ret; |
| 483 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 484 | ret = max8997_update_reg(i2c, reg, i << shift, mask << shift); |
| 485 | *selector = i; |
| 486 | |
Axel Lin | 62bc4d4 | 2012-04-11 22:58:03 +0800 | [diff] [blame] | 487 | return ret; |
| 488 | } |
| 489 | |
Axel Lin | 24bd410 | 2012-12-26 16:28:39 +0800 | [diff] [blame] | 490 | static int max8997_set_voltage_buck_time_sel(struct regulator_dev *rdev, |
Axel Lin | 62bc4d4 | 2012-04-11 22:58:03 +0800 | [diff] [blame] | 491 | unsigned int old_selector, |
| 492 | unsigned int new_selector) |
| 493 | { |
| 494 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
| 495 | int rid = rdev_get_id(rdev); |
| 496 | const struct voltage_map_desc *desc = reg_voltage_map[rid]; |
| 497 | |
| 498 | /* Delay is required only if the voltage is increasing */ |
| 499 | if (old_selector >= new_selector) |
| 500 | return 0; |
| 501 | |
| 502 | /* No need to delay if gpio_dvs_mode */ |
| 503 | switch (rid) { |
| 504 | case MAX8997_BUCK1: |
| 505 | if (max8997->buck1_gpiodvs) |
| 506 | return 0; |
| 507 | break; |
| 508 | case MAX8997_BUCK2: |
| 509 | if (max8997->buck2_gpiodvs) |
| 510 | return 0; |
| 511 | break; |
| 512 | case MAX8997_BUCK5: |
| 513 | if (max8997->buck5_gpiodvs) |
| 514 | return 0; |
| 515 | break; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 516 | } |
| 517 | |
Axel Lin | 62bc4d4 | 2012-04-11 22:58:03 +0800 | [diff] [blame] | 518 | switch (rid) { |
| 519 | case MAX8997_BUCK1: |
| 520 | case MAX8997_BUCK2: |
| 521 | case MAX8997_BUCK4: |
| 522 | case MAX8997_BUCK5: |
| 523 | return DIV_ROUND_UP(desc->step * (new_selector - old_selector), |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 524 | max8997->ramp_delay * 1000); |
Axel Lin | 62bc4d4 | 2012-04-11 22:58:03 +0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | return 0; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Assess the damage on the voltage setting of BUCK1,2,5 by the change. |
| 532 | * |
| 533 | * When GPIO-DVS mode is used for multiple bucks, changing the voltage value |
| 534 | * of one of the bucks may affect that of another buck, which is the side |
| 535 | * effect of the change (set_voltage). This function examines the GPIO-DVS |
| 536 | * configurations and checks whether such side-effect exists. |
| 537 | */ |
| 538 | static int max8997_assess_side_effect(struct regulator_dev *rdev, |
| 539 | u8 new_val, int *best) |
| 540 | { |
| 541 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 542 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 543 | u8 *buckx_val[3]; |
| 544 | bool buckx_gpiodvs[3]; |
| 545 | int side_effect[8]; |
| 546 | int min_side_effect = INT_MAX; |
| 547 | int i; |
| 548 | |
| 549 | *best = -1; |
| 550 | |
| 551 | switch (rid) { |
| 552 | case MAX8997_BUCK1: |
| 553 | rid = 0; |
| 554 | break; |
| 555 | case MAX8997_BUCK2: |
| 556 | rid = 1; |
| 557 | break; |
| 558 | case MAX8997_BUCK5: |
| 559 | rid = 2; |
| 560 | break; |
| 561 | default: |
| 562 | return -EINVAL; |
| 563 | } |
| 564 | |
| 565 | buckx_val[0] = max8997->buck1_vol; |
| 566 | buckx_val[1] = max8997->buck2_vol; |
| 567 | buckx_val[2] = max8997->buck5_vol; |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 568 | buckx_gpiodvs[0] = max8997->buck1_gpiodvs; |
| 569 | buckx_gpiodvs[1] = max8997->buck2_gpiodvs; |
| 570 | buckx_gpiodvs[2] = max8997->buck5_gpiodvs; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 571 | |
| 572 | for (i = 0; i < 8; i++) { |
| 573 | int others; |
| 574 | |
| 575 | if (new_val != (buckx_val[rid])[i]) { |
| 576 | side_effect[i] = -1; |
| 577 | continue; |
| 578 | } |
| 579 | |
| 580 | side_effect[i] = 0; |
| 581 | for (others = 0; others < 3; others++) { |
| 582 | int diff; |
| 583 | |
| 584 | if (others == rid) |
| 585 | continue; |
| 586 | if (buckx_gpiodvs[others] == false) |
| 587 | continue; /* Not affected */ |
| 588 | diff = (buckx_val[others])[i] - |
| 589 | (buckx_val[others])[max8997->buck125_gpioindex]; |
| 590 | if (diff > 0) |
| 591 | side_effect[i] += diff; |
| 592 | else if (diff < 0) |
| 593 | side_effect[i] -= diff; |
| 594 | } |
| 595 | if (side_effect[i] == 0) { |
| 596 | *best = i; |
| 597 | return 0; /* NO SIDE EFFECT! Use This! */ |
| 598 | } |
| 599 | if (side_effect[i] < min_side_effect) { |
| 600 | min_side_effect = side_effect[i]; |
| 601 | *best = i; |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | if (*best == -1) |
| 606 | return -EINVAL; |
| 607 | |
| 608 | return side_effect[*best]; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * For Buck 1 ~ 5 and 7. If it is not controlled by GPIO, this calls |
| 613 | * max8997_set_voltage_ldobuck to do the job. |
| 614 | */ |
| 615 | static int max8997_set_voltage_buck(struct regulator_dev *rdev, |
| 616 | int min_uV, int max_uV, unsigned *selector) |
| 617 | { |
| 618 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 619 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 620 | const struct voltage_map_desc *desc; |
| 621 | int new_val, new_idx, damage, tmp_val, tmp_idx, tmp_dmg; |
| 622 | bool gpio_dvs_mode = false; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 623 | |
| 624 | if (rid < MAX8997_BUCK1 || rid > MAX8997_BUCK7) |
| 625 | return -EINVAL; |
| 626 | |
| 627 | switch (rid) { |
| 628 | case MAX8997_BUCK1: |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 629 | if (max8997->buck1_gpiodvs) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 630 | gpio_dvs_mode = true; |
| 631 | break; |
| 632 | case MAX8997_BUCK2: |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 633 | if (max8997->buck2_gpiodvs) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 634 | gpio_dvs_mode = true; |
| 635 | break; |
| 636 | case MAX8997_BUCK5: |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 637 | if (max8997->buck5_gpiodvs) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 638 | gpio_dvs_mode = true; |
| 639 | break; |
| 640 | } |
| 641 | |
| 642 | if (!gpio_dvs_mode) |
| 643 | return max8997_set_voltage_ldobuck(rdev, min_uV, max_uV, |
| 644 | selector); |
| 645 | |
| 646 | desc = reg_voltage_map[rid]; |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 647 | new_val = max8997_get_voltage_proper_val(desc, min_uV, max_uV); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 648 | if (new_val < 0) |
| 649 | return new_val; |
| 650 | |
| 651 | tmp_dmg = INT_MAX; |
| 652 | tmp_idx = -1; |
| 653 | tmp_val = -1; |
| 654 | do { |
| 655 | damage = max8997_assess_side_effect(rdev, new_val, &new_idx); |
| 656 | if (damage == 0) |
| 657 | goto out; |
| 658 | |
| 659 | if (tmp_dmg > damage) { |
| 660 | tmp_idx = new_idx; |
| 661 | tmp_val = new_val; |
| 662 | tmp_dmg = damage; |
| 663 | } |
| 664 | |
| 665 | new_val++; |
Axel Lin | f55205f | 2012-04-11 20:53:58 +0800 | [diff] [blame] | 666 | } while (desc->min + desc->step * new_val <= desc->max); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 667 | |
| 668 | new_idx = tmp_idx; |
| 669 | new_val = tmp_val; |
| 670 | |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 671 | if (max8997->ignore_gpiodvs_side_effect == false) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 672 | return -EINVAL; |
| 673 | |
Jingoo Han | c4e3b14 | 2013-10-14 17:49:11 +0900 | [diff] [blame] | 674 | dev_warn(&rdev->dev, |
| 675 | "MAX8997 GPIO-DVS Side Effect Warning: GPIO SET: %d -> %d\n", |
| 676 | max8997->buck125_gpioindex, tmp_idx); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 677 | |
| 678 | out: |
| 679 | if (new_idx < 0 || new_val < 0) |
| 680 | return -EINVAL; |
| 681 | |
| 682 | max8997->buck125_gpioindex = new_idx; |
| 683 | max8997_set_gpio(max8997); |
| 684 | *selector = new_val; |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 689 | /* For SAFEOUT1 and SAFEOUT2 */ |
Axel Lin | b79ca05 | 2012-12-26 16:30:44 +0800 | [diff] [blame] | 690 | static int max8997_set_voltage_safeout_sel(struct regulator_dev *rdev, |
| 691 | unsigned selector) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 692 | { |
| 693 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
| 694 | struct i2c_client *i2c = max8997->iodev->i2c; |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 695 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 696 | int reg, shift = 0, mask, ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 697 | |
| 698 | if (rid != MAX8997_ESAFEOUT1 && rid != MAX8997_ESAFEOUT2) |
| 699 | return -EINVAL; |
| 700 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 701 | ret = max8997_get_voltage_register(rdev, ®, &shift, &mask); |
| 702 | if (ret) |
| 703 | return ret; |
| 704 | |
Axel Lin | b79ca05 | 2012-12-26 16:30:44 +0800 | [diff] [blame] | 705 | return max8997_update_reg(i2c, reg, selector << shift, mask << shift); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 706 | } |
| 707 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 708 | static int max8997_reg_disable_suspend(struct regulator_dev *rdev) |
| 709 | { |
| 710 | struct max8997_data *max8997 = rdev_get_drvdata(rdev); |
| 711 | struct i2c_client *i2c = max8997->iodev->i2c; |
| 712 | int ret, reg, mask, pattern; |
Axel Lin | b3e1348 | 2012-03-01 09:26:27 +0800 | [diff] [blame] | 713 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 714 | |
| 715 | ret = max8997_get_enable_register(rdev, ®, &mask, &pattern); |
| 716 | if (ret) |
| 717 | return ret; |
| 718 | |
| 719 | max8997_read_reg(i2c, reg, &max8997->saved_states[rid]); |
| 720 | |
| 721 | if (rid == MAX8997_LDO1 || |
| 722 | rid == MAX8997_LDO10 || |
| 723 | rid == MAX8997_LDO21) { |
| 724 | dev_dbg(&rdev->dev, "Conditional Power-Off for %s\n", |
| 725 | rdev->desc->name); |
| 726 | return max8997_update_reg(i2c, reg, 0x40, mask); |
| 727 | } |
| 728 | |
| 729 | dev_dbg(&rdev->dev, "Full Power-Off for %s (%xh -> %xh)\n", |
| 730 | rdev->desc->name, max8997->saved_states[rid] & mask, |
| 731 | (~pattern) & mask); |
| 732 | return max8997_update_reg(i2c, reg, ~pattern, mask); |
| 733 | } |
| 734 | |
Rikard Falkeborn | 9ed84d2 | 2020-06-18 00:32:47 +0200 | [diff] [blame] | 735 | static const struct regulator_ops max8997_ldo_ops = { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 736 | .list_voltage = max8997_list_voltage, |
| 737 | .is_enabled = max8997_reg_is_enabled, |
| 738 | .enable = max8997_reg_enable, |
| 739 | .disable = max8997_reg_disable, |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 740 | .get_voltage_sel = max8997_get_voltage_sel, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 741 | .set_voltage = max8997_set_voltage_ldobuck, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 742 | .set_suspend_disable = max8997_reg_disable_suspend, |
| 743 | }; |
| 744 | |
Rikard Falkeborn | 9ed84d2 | 2020-06-18 00:32:47 +0200 | [diff] [blame] | 745 | static const struct regulator_ops max8997_buck_ops = { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 746 | .list_voltage = max8997_list_voltage, |
| 747 | .is_enabled = max8997_reg_is_enabled, |
| 748 | .enable = max8997_reg_enable, |
| 749 | .disable = max8997_reg_disable, |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 750 | .get_voltage_sel = max8997_get_voltage_sel, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 751 | .set_voltage = max8997_set_voltage_buck, |
Axel Lin | 24bd410 | 2012-12-26 16:28:39 +0800 | [diff] [blame] | 752 | .set_voltage_time_sel = max8997_set_voltage_buck_time_sel, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 753 | .set_suspend_disable = max8997_reg_disable_suspend, |
| 754 | }; |
| 755 | |
Rikard Falkeborn | 9ed84d2 | 2020-06-18 00:32:47 +0200 | [diff] [blame] | 756 | static const struct regulator_ops max8997_fixedvolt_ops = { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 757 | .list_voltage = max8997_list_voltage, |
| 758 | .is_enabled = max8997_reg_is_enabled, |
| 759 | .enable = max8997_reg_enable, |
| 760 | .disable = max8997_reg_disable, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 761 | .set_suspend_disable = max8997_reg_disable_suspend, |
| 762 | }; |
| 763 | |
Rikard Falkeborn | 9ed84d2 | 2020-06-18 00:32:47 +0200 | [diff] [blame] | 764 | static const struct regulator_ops max8997_safeout_ops = { |
Axel Lin | b79ca05 | 2012-12-26 16:30:44 +0800 | [diff] [blame] | 765 | .list_voltage = regulator_list_voltage_table, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 766 | .is_enabled = max8997_reg_is_enabled, |
| 767 | .enable = max8997_reg_enable, |
| 768 | .disable = max8997_reg_disable, |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 769 | .get_voltage_sel = max8997_get_voltage_sel, |
Axel Lin | b79ca05 | 2012-12-26 16:30:44 +0800 | [diff] [blame] | 770 | .set_voltage_sel = max8997_set_voltage_safeout_sel, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 771 | .set_suspend_disable = max8997_reg_disable_suspend, |
| 772 | }; |
| 773 | |
Rikard Falkeborn | 9ed84d2 | 2020-06-18 00:32:47 +0200 | [diff] [blame] | 774 | static const struct regulator_ops max8997_fixedstate_ops = { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 775 | .list_voltage = max8997_list_voltage_charger_cv, |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 776 | .get_voltage_sel = max8997_get_voltage_sel, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 777 | .set_voltage = max8997_set_voltage_charger_cv, |
| 778 | }; |
| 779 | |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 780 | static int max8997_set_current_limit(struct regulator_dev *rdev, |
| 781 | int min_uA, int max_uA) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 782 | { |
| 783 | unsigned dummy; |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 784 | int rid = rdev_get_id(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 785 | |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 786 | if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF) |
| 787 | return -EINVAL; |
| 788 | |
| 789 | /* Reuse max8997_set_voltage_ldobuck to set current_limit. */ |
| 790 | return max8997_set_voltage_ldobuck(rdev, min_uA, max_uA, &dummy); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 791 | } |
| 792 | |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 793 | static int max8997_get_current_limit(struct regulator_dev *rdev) |
| 794 | { |
| 795 | int sel, rid = rdev_get_id(rdev); |
| 796 | |
| 797 | if (rid != MAX8997_CHARGER && rid != MAX8997_CHARGER_TOPOFF) |
| 798 | return -EINVAL; |
| 799 | |
| 800 | sel = max8997_get_voltage_sel(rdev); |
| 801 | if (sel < 0) |
| 802 | return sel; |
| 803 | |
| 804 | /* Reuse max8997_list_voltage to get current_limit. */ |
| 805 | return max8997_list_voltage(rdev, sel); |
| 806 | } |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 807 | |
Rikard Falkeborn | 9ed84d2 | 2020-06-18 00:32:47 +0200 | [diff] [blame] | 808 | static const struct regulator_ops max8997_charger_ops = { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 809 | .is_enabled = max8997_reg_is_enabled, |
| 810 | .enable = max8997_reg_enable, |
| 811 | .disable = max8997_reg_disable, |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 812 | .get_current_limit = max8997_get_current_limit, |
| 813 | .set_current_limit = max8997_set_current_limit, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 814 | }; |
| 815 | |
Rikard Falkeborn | 9ed84d2 | 2020-06-18 00:32:47 +0200 | [diff] [blame] | 816 | static const struct regulator_ops max8997_charger_fixedstate_ops = { |
Axel Lin | 9e96b3a | 2012-04-11 22:56:11 +0800 | [diff] [blame] | 817 | .get_current_limit = max8997_get_current_limit, |
| 818 | .set_current_limit = max8997_set_current_limit, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 819 | }; |
| 820 | |
Axel Lin | 4533f80 | 2012-03-13 14:53:58 +0800 | [diff] [blame] | 821 | #define MAX8997_VOLTAGE_REGULATOR(_name, _ops) {\ |
| 822 | .name = #_name, \ |
| 823 | .id = MAX8997_##_name, \ |
| 824 | .ops = &_ops, \ |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 825 | .type = REGULATOR_VOLTAGE, \ |
| 826 | .owner = THIS_MODULE, \ |
| 827 | } |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 828 | |
Axel Lin | 4533f80 | 2012-03-13 14:53:58 +0800 | [diff] [blame] | 829 | #define MAX8997_CURRENT_REGULATOR(_name, _ops) {\ |
| 830 | .name = #_name, \ |
| 831 | .id = MAX8997_##_name, \ |
| 832 | .ops = &_ops, \ |
| 833 | .type = REGULATOR_CURRENT, \ |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 834 | .owner = THIS_MODULE, \ |
| 835 | } |
| 836 | |
| 837 | static struct regulator_desc regulators[] = { |
Axel Lin | 4533f80 | 2012-03-13 14:53:58 +0800 | [diff] [blame] | 838 | MAX8997_VOLTAGE_REGULATOR(LDO1, max8997_ldo_ops), |
| 839 | MAX8997_VOLTAGE_REGULATOR(LDO2, max8997_ldo_ops), |
| 840 | MAX8997_VOLTAGE_REGULATOR(LDO3, max8997_ldo_ops), |
| 841 | MAX8997_VOLTAGE_REGULATOR(LDO4, max8997_ldo_ops), |
| 842 | MAX8997_VOLTAGE_REGULATOR(LDO5, max8997_ldo_ops), |
| 843 | MAX8997_VOLTAGE_REGULATOR(LDO6, max8997_ldo_ops), |
| 844 | MAX8997_VOLTAGE_REGULATOR(LDO7, max8997_ldo_ops), |
| 845 | MAX8997_VOLTAGE_REGULATOR(LDO8, max8997_ldo_ops), |
| 846 | MAX8997_VOLTAGE_REGULATOR(LDO9, max8997_ldo_ops), |
| 847 | MAX8997_VOLTAGE_REGULATOR(LDO10, max8997_ldo_ops), |
| 848 | MAX8997_VOLTAGE_REGULATOR(LDO11, max8997_ldo_ops), |
| 849 | MAX8997_VOLTAGE_REGULATOR(LDO12, max8997_ldo_ops), |
| 850 | MAX8997_VOLTAGE_REGULATOR(LDO13, max8997_ldo_ops), |
| 851 | MAX8997_VOLTAGE_REGULATOR(LDO14, max8997_ldo_ops), |
| 852 | MAX8997_VOLTAGE_REGULATOR(LDO15, max8997_ldo_ops), |
| 853 | MAX8997_VOLTAGE_REGULATOR(LDO16, max8997_ldo_ops), |
| 854 | MAX8997_VOLTAGE_REGULATOR(LDO17, max8997_ldo_ops), |
| 855 | MAX8997_VOLTAGE_REGULATOR(LDO18, max8997_ldo_ops), |
| 856 | MAX8997_VOLTAGE_REGULATOR(LDO21, max8997_ldo_ops), |
| 857 | MAX8997_VOLTAGE_REGULATOR(BUCK1, max8997_buck_ops), |
| 858 | MAX8997_VOLTAGE_REGULATOR(BUCK2, max8997_buck_ops), |
| 859 | MAX8997_VOLTAGE_REGULATOR(BUCK3, max8997_buck_ops), |
| 860 | MAX8997_VOLTAGE_REGULATOR(BUCK4, max8997_buck_ops), |
| 861 | MAX8997_VOLTAGE_REGULATOR(BUCK5, max8997_buck_ops), |
| 862 | MAX8997_VOLTAGE_REGULATOR(BUCK6, max8997_fixedvolt_ops), |
| 863 | MAX8997_VOLTAGE_REGULATOR(BUCK7, max8997_buck_ops), |
| 864 | MAX8997_VOLTAGE_REGULATOR(EN32KHZ_AP, max8997_fixedvolt_ops), |
| 865 | MAX8997_VOLTAGE_REGULATOR(EN32KHZ_CP, max8997_fixedvolt_ops), |
| 866 | MAX8997_VOLTAGE_REGULATOR(ENVICHG, max8997_fixedvolt_ops), |
| 867 | MAX8997_VOLTAGE_REGULATOR(ESAFEOUT1, max8997_safeout_ops), |
| 868 | MAX8997_VOLTAGE_REGULATOR(ESAFEOUT2, max8997_safeout_ops), |
| 869 | MAX8997_VOLTAGE_REGULATOR(CHARGER_CV, max8997_fixedstate_ops), |
| 870 | MAX8997_CURRENT_REGULATOR(CHARGER, max8997_charger_ops), |
| 871 | MAX8997_CURRENT_REGULATOR(CHARGER_TOPOFF, |
| 872 | max8997_charger_fixedstate_ops), |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 873 | }; |
| 874 | |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 875 | #ifdef CONFIG_OF |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 876 | static int max8997_pmic_dt_parse_dvs_gpio(struct platform_device *pdev, |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 877 | struct max8997_platform_data *pdata, |
| 878 | struct device_node *pmic_np) |
| 879 | { |
| 880 | int i, gpio; |
| 881 | |
| 882 | for (i = 0; i < 3; i++) { |
| 883 | gpio = of_get_named_gpio(pmic_np, |
| 884 | "max8997,pmic-buck125-dvs-gpios", i); |
| 885 | if (!gpio_is_valid(gpio)) { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 886 | dev_err(&pdev->dev, "invalid gpio[%d]: %d\n", i, gpio); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 887 | return -EINVAL; |
| 888 | } |
| 889 | pdata->buck125_gpios[i] = gpio; |
| 890 | } |
| 891 | return 0; |
| 892 | } |
| 893 | |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 894 | static int max8997_pmic_dt_parse_pdata(struct platform_device *pdev, |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 895 | struct max8997_platform_data *pdata) |
| 896 | { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 897 | struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 898 | struct device_node *pmic_np, *regulators_np, *reg_np; |
| 899 | struct max8997_regulator_data *rdata; |
| 900 | unsigned int i, dvs_voltage_nr = 1, ret; |
| 901 | |
Guodong Xu | b8b27a4 | 2014-09-10 11:50:39 +0800 | [diff] [blame] | 902 | pmic_np = iodev->dev->of_node; |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 903 | if (!pmic_np) { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 904 | dev_err(&pdev->dev, "could not find pmic sub-node\n"); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 905 | return -ENODEV; |
| 906 | } |
| 907 | |
Sachin Kamat | c0a3536 | 2014-02-14 17:19:52 +0530 | [diff] [blame] | 908 | regulators_np = of_get_child_by_name(pmic_np, "regulators"); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 909 | if (!regulators_np) { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 910 | dev_err(&pdev->dev, "could not find regulators sub-node\n"); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 911 | return -EINVAL; |
| 912 | } |
| 913 | |
| 914 | /* count the number of regulators to be supported in pmic */ |
Axel Lin | ee70b25 | 2013-01-30 20:52:49 +0800 | [diff] [blame] | 915 | pdata->num_regulators = of_get_child_count(regulators_np); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 916 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 917 | rdata = devm_kcalloc(&pdev->dev, |
| 918 | pdata->num_regulators, sizeof(*rdata), |
| 919 | GFP_KERNEL); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 920 | if (!rdata) { |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 921 | of_node_put(regulators_np); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 922 | return -ENOMEM; |
| 923 | } |
| 924 | |
| 925 | pdata->regulators = rdata; |
| 926 | for_each_child_of_node(regulators_np, reg_np) { |
| 927 | for (i = 0; i < ARRAY_SIZE(regulators); i++) |
Rob Herring | c32569e | 2018-12-05 13:50:37 -0600 | [diff] [blame] | 928 | if (of_node_name_eq(reg_np, regulators[i].name)) |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 929 | break; |
| 930 | |
| 931 | if (i == ARRAY_SIZE(regulators)) { |
Rob Herring | 0c9721a | 2018-08-27 20:52:42 -0500 | [diff] [blame] | 932 | dev_warn(&pdev->dev, "don't know how to configure regulator %pOFn\n", |
| 933 | reg_np); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 934 | continue; |
| 935 | } |
| 936 | |
| 937 | rdata->id = i; |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 938 | rdata->initdata = of_get_regulator_init_data(&pdev->dev, |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 939 | reg_np, |
| 940 | ®ulators[i]); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 941 | rdata->reg_node = reg_np; |
| 942 | rdata++; |
| 943 | } |
Axel Lin | c92f5dd | 2013-01-27 21:16:56 +0800 | [diff] [blame] | 944 | of_node_put(regulators_np); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 945 | |
| 946 | if (of_get_property(pmic_np, "max8997,pmic-buck1-uses-gpio-dvs", NULL)) |
| 947 | pdata->buck1_gpiodvs = true; |
| 948 | |
| 949 | if (of_get_property(pmic_np, "max8997,pmic-buck2-uses-gpio-dvs", NULL)) |
| 950 | pdata->buck2_gpiodvs = true; |
| 951 | |
| 952 | if (of_get_property(pmic_np, "max8997,pmic-buck5-uses-gpio-dvs", NULL)) |
| 953 | pdata->buck5_gpiodvs = true; |
| 954 | |
| 955 | if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs || |
| 956 | pdata->buck5_gpiodvs) { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 957 | ret = max8997_pmic_dt_parse_dvs_gpio(pdev, pdata, pmic_np); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 958 | if (ret) |
| 959 | return -EINVAL; |
| 960 | |
| 961 | if (of_property_read_u32(pmic_np, |
| 962 | "max8997,pmic-buck125-default-dvs-idx", |
| 963 | &pdata->buck125_default_idx)) { |
| 964 | pdata->buck125_default_idx = 0; |
| 965 | } else { |
| 966 | if (pdata->buck125_default_idx >= 8) { |
| 967 | pdata->buck125_default_idx = 0; |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 968 | dev_info(&pdev->dev, "invalid value for default dvs index, using 0 instead\n"); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
| 972 | if (of_get_property(pmic_np, |
| 973 | "max8997,pmic-ignore-gpiodvs-side-effect", NULL)) |
| 974 | pdata->ignore_gpiodvs_side_effect = true; |
| 975 | |
| 976 | dvs_voltage_nr = 8; |
| 977 | } |
| 978 | |
| 979 | if (of_property_read_u32_array(pmic_np, |
| 980 | "max8997,pmic-buck1-dvs-voltage", |
| 981 | pdata->buck1_voltage, dvs_voltage_nr)) { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 982 | dev_err(&pdev->dev, "buck1 voltages not specified\n"); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 983 | return -EINVAL; |
| 984 | } |
| 985 | |
| 986 | if (of_property_read_u32_array(pmic_np, |
| 987 | "max8997,pmic-buck2-dvs-voltage", |
| 988 | pdata->buck2_voltage, dvs_voltage_nr)) { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 989 | dev_err(&pdev->dev, "buck2 voltages not specified\n"); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 990 | return -EINVAL; |
| 991 | } |
| 992 | |
| 993 | if (of_property_read_u32_array(pmic_np, |
| 994 | "max8997,pmic-buck5-dvs-voltage", |
| 995 | pdata->buck5_voltage, dvs_voltage_nr)) { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 996 | dev_err(&pdev->dev, "buck5 voltages not specified\n"); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 997 | return -EINVAL; |
| 998 | } |
| 999 | |
| 1000 | return 0; |
| 1001 | } |
| 1002 | #else |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 1003 | static int max8997_pmic_dt_parse_pdata(struct platform_device *pdev, |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 1004 | struct max8997_platform_data *pdata) |
| 1005 | { |
| 1006 | return 0; |
| 1007 | } |
| 1008 | #endif /* CONFIG_OF */ |
| 1009 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 1010 | static int max8997_pmic_probe(struct platform_device *pdev) |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1011 | { |
| 1012 | struct max8997_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 1013 | struct max8997_platform_data *pdata = iodev->pdata; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 1014 | struct regulator_config config = { }; |
Axel Lin | ad78bba | 2014-03-09 15:29:31 +0800 | [diff] [blame] | 1015 | struct regulator_dev *rdev; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1016 | struct max8997_data *max8997; |
| 1017 | struct i2c_client *i2c; |
Axel Lin | ad78bba | 2014-03-09 15:29:31 +0800 | [diff] [blame] | 1018 | int i, ret, nr_dvs; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1019 | u8 max_buck1 = 0, max_buck2 = 0, max_buck5 = 0; |
| 1020 | |
Axel Lin | a2f0730 | 2013-04-13 22:05:43 +0800 | [diff] [blame] | 1021 | if (!pdata) { |
| 1022 | dev_err(&pdev->dev, "No platform init data supplied.\n"); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1023 | return -ENODEV; |
| 1024 | } |
| 1025 | |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 1026 | if (iodev->dev->of_node) { |
Axel Lin | b4895e2 | 2013-01-25 10:26:32 +0800 | [diff] [blame] | 1027 | ret = max8997_pmic_dt_parse_pdata(pdev, pdata); |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 1028 | if (ret) |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
Axel Lin | 8ae5767 | 2012-04-19 09:44:13 +0800 | [diff] [blame] | 1032 | max8997 = devm_kzalloc(&pdev->dev, sizeof(struct max8997_data), |
| 1033 | GFP_KERNEL); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1034 | if (!max8997) |
| 1035 | return -ENOMEM; |
| 1036 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1037 | max8997->dev = &pdev->dev; |
| 1038 | max8997->iodev = iodev; |
| 1039 | max8997->num_regulators = pdata->num_regulators; |
| 1040 | platform_set_drvdata(pdev, max8997); |
| 1041 | i2c = max8997->iodev->i2c; |
| 1042 | |
| 1043 | max8997->buck125_gpioindex = pdata->buck125_default_idx; |
MyungJoo Ham | 6e0414a | 2011-06-20 17:30:17 +0900 | [diff] [blame] | 1044 | max8997->buck1_gpiodvs = pdata->buck1_gpiodvs; |
| 1045 | max8997->buck2_gpiodvs = pdata->buck2_gpiodvs; |
| 1046 | max8997->buck5_gpiodvs = pdata->buck5_gpiodvs; |
| 1047 | memcpy(max8997->buck125_gpios, pdata->buck125_gpios, sizeof(int) * 3); |
| 1048 | max8997->ignore_gpiodvs_side_effect = pdata->ignore_gpiodvs_side_effect; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1049 | |
Thomas Abraham | 068a8c8 | 2012-11-23 13:33:14 +0530 | [diff] [blame] | 1050 | nr_dvs = (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs || |
| 1051 | pdata->buck5_gpiodvs) ? 8 : 1; |
| 1052 | |
| 1053 | for (i = 0; i < nr_dvs; i++) { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1054 | max8997->buck1_vol[i] = ret = |
| 1055 | max8997_get_voltage_proper_val( |
| 1056 | &buck1245_voltage_map_desc, |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 1057 | pdata->buck1_voltage[i], |
| 1058 | pdata->buck1_voltage[i] + |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1059 | buck1245_voltage_map_desc.step); |
| 1060 | if (ret < 0) |
Sachin Kamat | 4177f95 | 2013-09-04 11:08:01 +0530 | [diff] [blame] | 1061 | return ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1062 | |
| 1063 | max8997->buck2_vol[i] = ret = |
| 1064 | max8997_get_voltage_proper_val( |
| 1065 | &buck1245_voltage_map_desc, |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 1066 | pdata->buck2_voltage[i], |
| 1067 | pdata->buck2_voltage[i] + |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1068 | buck1245_voltage_map_desc.step); |
| 1069 | if (ret < 0) |
Sachin Kamat | 4177f95 | 2013-09-04 11:08:01 +0530 | [diff] [blame] | 1070 | return ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1071 | |
| 1072 | max8997->buck5_vol[i] = ret = |
| 1073 | max8997_get_voltage_proper_val( |
| 1074 | &buck1245_voltage_map_desc, |
Axel Lin | bc3b775 | 2012-12-28 17:09:03 +0800 | [diff] [blame] | 1075 | pdata->buck5_voltage[i], |
| 1076 | pdata->buck5_voltage[i] + |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1077 | buck1245_voltage_map_desc.step); |
| 1078 | if (ret < 0) |
Sachin Kamat | 4177f95 | 2013-09-04 11:08:01 +0530 | [diff] [blame] | 1079 | return ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1080 | |
| 1081 | if (max_buck1 < max8997->buck1_vol[i]) |
| 1082 | max_buck1 = max8997->buck1_vol[i]; |
| 1083 | if (max_buck2 < max8997->buck2_vol[i]) |
| 1084 | max_buck2 = max8997->buck2_vol[i]; |
| 1085 | if (max_buck5 < max8997->buck5_vol[i]) |
| 1086 | max_buck5 = max8997->buck5_vol[i]; |
| 1087 | } |
| 1088 | |
| 1089 | /* For the safety, set max voltage before setting up */ |
| 1090 | for (i = 0; i < 8; i++) { |
Axel Lin | ecb9c4f | 2011-05-16 18:20:34 +0800 | [diff] [blame] | 1091 | max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1092 | max_buck1, 0x3f); |
Axel Lin | ecb9c4f | 2011-05-16 18:20:34 +0800 | [diff] [blame] | 1093 | max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1094 | max_buck2, 0x3f); |
Axel Lin | ecb9c4f | 2011-05-16 18:20:34 +0800 | [diff] [blame] | 1095 | max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1096 | max_buck5, 0x3f); |
| 1097 | } |
| 1098 | |
Thomas Abraham | 11ec7bf0 | 2012-11-23 13:33:13 +0530 | [diff] [blame] | 1099 | /* Initialize all the DVS related BUCK registers */ |
Thomas Abraham | 068a8c8 | 2012-11-23 13:33:14 +0530 | [diff] [blame] | 1100 | for (i = 0; i < nr_dvs; i++) { |
Thomas Abraham | 11ec7bf0 | 2012-11-23 13:33:13 +0530 | [diff] [blame] | 1101 | max8997_update_reg(i2c, MAX8997_REG_BUCK1DVS1 + i, |
| 1102 | max8997->buck1_vol[i], |
| 1103 | 0x3f); |
| 1104 | max8997_update_reg(i2c, MAX8997_REG_BUCK2DVS1 + i, |
| 1105 | max8997->buck2_vol[i], |
| 1106 | 0x3f); |
| 1107 | max8997_update_reg(i2c, MAX8997_REG_BUCK5DVS1 + i, |
| 1108 | max8997->buck5_vol[i], |
| 1109 | 0x3f); |
| 1110 | } |
| 1111 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1112 | /* |
| 1113 | * If buck 1, 2, and 5 do not care DVS GPIO settings, ignore them. |
| 1114 | * If at least one of them cares, set gpios. |
| 1115 | */ |
| 1116 | if (pdata->buck1_gpiodvs || pdata->buck2_gpiodvs || |
| 1117 | pdata->buck5_gpiodvs) { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1118 | |
| 1119 | if (!gpio_is_valid(pdata->buck125_gpios[0]) || |
| 1120 | !gpio_is_valid(pdata->buck125_gpios[1]) || |
| 1121 | !gpio_is_valid(pdata->buck125_gpios[2])) { |
| 1122 | dev_err(&pdev->dev, "GPIO NOT VALID\n"); |
Sachin Kamat | 4177f95 | 2013-09-04 11:08:01 +0530 | [diff] [blame] | 1123 | return -EINVAL; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1124 | } |
| 1125 | |
Axel Lin | 8fa25ed | 2012-07-05 23:06:57 +0800 | [diff] [blame] | 1126 | ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[0], |
| 1127 | "MAX8997 SET1"); |
| 1128 | if (ret) |
Sachin Kamat | 4177f95 | 2013-09-04 11:08:01 +0530 | [diff] [blame] | 1129 | return ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1130 | |
Axel Lin | 8fa25ed | 2012-07-05 23:06:57 +0800 | [diff] [blame] | 1131 | ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[1], |
| 1132 | "MAX8997 SET2"); |
| 1133 | if (ret) |
Sachin Kamat | 4177f95 | 2013-09-04 11:08:01 +0530 | [diff] [blame] | 1134 | return ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1135 | |
Axel Lin | 8fa25ed | 2012-07-05 23:06:57 +0800 | [diff] [blame] | 1136 | ret = devm_gpio_request(&pdev->dev, pdata->buck125_gpios[2], |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1137 | "MAX8997 SET3"); |
Axel Lin | 8fa25ed | 2012-07-05 23:06:57 +0800 | [diff] [blame] | 1138 | if (ret) |
Sachin Kamat | 4177f95 | 2013-09-04 11:08:01 +0530 | [diff] [blame] | 1139 | return ret; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1140 | |
| 1141 | gpio_direction_output(pdata->buck125_gpios[0], |
| 1142 | (max8997->buck125_gpioindex >> 2) |
| 1143 | & 0x1); /* SET1 */ |
| 1144 | gpio_direction_output(pdata->buck125_gpios[1], |
| 1145 | (max8997->buck125_gpioindex >> 1) |
| 1146 | & 0x1); /* SET2 */ |
| 1147 | gpio_direction_output(pdata->buck125_gpios[2], |
| 1148 | (max8997->buck125_gpioindex >> 0) |
| 1149 | & 0x1); /* SET3 */ |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /* DVS-GPIO disabled */ |
| 1153 | max8997_update_reg(i2c, MAX8997_REG_BUCK1CTRL, (pdata->buck1_gpiodvs) ? |
| 1154 | (1 << 1) : (0 << 1), 1 << 1); |
| 1155 | max8997_update_reg(i2c, MAX8997_REG_BUCK2CTRL, (pdata->buck2_gpiodvs) ? |
| 1156 | (1 << 1) : (0 << 1), 1 << 1); |
| 1157 | max8997_update_reg(i2c, MAX8997_REG_BUCK5CTRL, (pdata->buck5_gpiodvs) ? |
| 1158 | (1 << 1) : (0 << 1), 1 << 1); |
| 1159 | |
Tushar Behera | dbb48e7 | 2011-06-21 09:08:59 +0530 | [diff] [blame] | 1160 | /* Misc Settings */ |
| 1161 | max8997->ramp_delay = 10; /* set 10mV/us, which is the default */ |
| 1162 | max8997_write_reg(i2c, MAX8997_REG_BUCKRAMP, (0xf << 4) | 0x9); |
| 1163 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1164 | for (i = 0; i < pdata->num_regulators; i++) { |
| 1165 | const struct voltage_map_desc *desc; |
| 1166 | int id = pdata->regulators[i].id; |
| 1167 | |
| 1168 | desc = reg_voltage_map[id]; |
Axel Lin | b79ca05 | 2012-12-26 16:30:44 +0800 | [diff] [blame] | 1169 | if (desc) { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1170 | regulators[id].n_voltages = |
| 1171 | (desc->max - desc->min) / desc->step + 1; |
Axel Lin | b79ca05 | 2012-12-26 16:30:44 +0800 | [diff] [blame] | 1172 | } else if (id == MAX8997_ESAFEOUT1 || id == MAX8997_ESAFEOUT2) { |
| 1173 | regulators[id].volt_table = safeoutvolt; |
| 1174 | regulators[id].n_voltages = ARRAY_SIZE(safeoutvolt); |
| 1175 | } else if (id == MAX8997_CHARGER_CV) { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1176 | regulators[id].n_voltages = 16; |
Axel Lin | b79ca05 | 2012-12-26 16:30:44 +0800 | [diff] [blame] | 1177 | } |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1178 | |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 1179 | config.dev = max8997->dev; |
| 1180 | config.init_data = pdata->regulators[i].initdata; |
| 1181 | config.driver_data = max8997; |
Thomas Abraham | 77b71b3 | 2012-11-27 14:04:32 +0530 | [diff] [blame] | 1182 | config.of_node = pdata->regulators[i].reg_node; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 1183 | |
Axel Lin | ad78bba | 2014-03-09 15:29:31 +0800 | [diff] [blame] | 1184 | rdev = devm_regulator_register(&pdev->dev, ®ulators[id], |
| 1185 | &config); |
| 1186 | if (IS_ERR(rdev)) { |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1187 | dev_err(max8997->dev, "regulator init failed for %d\n", |
| 1188 | id); |
Axel Lin | ad78bba | 2014-03-09 15:29:31 +0800 | [diff] [blame] | 1189 | return PTR_ERR(rdev); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1193 | return 0; |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | static const struct platform_device_id max8997_pmic_id[] = { |
| 1197 | { "max8997-pmic", 0}, |
| 1198 | { }, |
| 1199 | }; |
Axel Lin | a51b907 | 2011-03-26 23:28:42 +0800 | [diff] [blame] | 1200 | MODULE_DEVICE_TABLE(platform, max8997_pmic_id); |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1201 | |
| 1202 | static struct platform_driver max8997_pmic_driver = { |
| 1203 | .driver = { |
| 1204 | .name = "max8997-pmic", |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1205 | }, |
| 1206 | .probe = max8997_pmic_probe, |
MyungJoo Ham | bd6ca2c | 2011-03-11 11:34:44 +0900 | [diff] [blame] | 1207 | .id_table = max8997_pmic_id, |
| 1208 | }; |
| 1209 | |
| 1210 | static int __init max8997_pmic_init(void) |
| 1211 | { |
| 1212 | return platform_driver_register(&max8997_pmic_driver); |
| 1213 | } |
| 1214 | subsys_initcall(max8997_pmic_init); |
| 1215 | |
| 1216 | static void __exit max8997_pmic_cleanup(void) |
| 1217 | { |
| 1218 | platform_driver_unregister(&max8997_pmic_driver); |
| 1219 | } |
| 1220 | module_exit(max8997_pmic_cleanup); |
| 1221 | |
| 1222 | MODULE_DESCRIPTION("MAXIM 8997/8966 Regulator Driver"); |
| 1223 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 1224 | MODULE_LICENSE("GPL"); |