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