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