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