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