Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * max8998.c - Voltage regulator driver for the Maxim 8998 |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Samsung Electronics |
| 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 6 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/mutex.h> |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 31 | #include <linux/delay.h> |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/regulator/driver.h> |
| 34 | #include <linux/mfd/max8998.h> |
| 35 | #include <linux/mfd/max8998-private.h> |
| 36 | |
| 37 | struct max8998_data { |
| 38 | struct device *dev; |
| 39 | struct max8998_dev *iodev; |
| 40 | int num_regulators; |
| 41 | struct regulator_dev **rdev; |
Lukasz Majewski | 889cd5a | 2010-09-27 14:32:25 +0200 | [diff] [blame] | 42 | u8 buck1_vol[4]; /* voltages for selection */ |
| 43 | u8 buck2_vol[2]; |
| 44 | unsigned int buck1_idx; /* index to last changed voltage */ |
| 45 | /* value in a set */ |
| 46 | unsigned int buck2_idx; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | struct voltage_map_desc { |
| 50 | int min; |
| 51 | int max; |
| 52 | int step; |
| 53 | }; |
| 54 | |
| 55 | /* Voltage maps */ |
| 56 | static const struct voltage_map_desc ldo23_voltage_map_desc = { |
| 57 | .min = 800, .step = 50, .max = 1300, |
| 58 | }; |
| 59 | static const struct voltage_map_desc ldo456711_voltage_map_desc = { |
| 60 | .min = 1600, .step = 100, .max = 3600, |
| 61 | }; |
| 62 | static const struct voltage_map_desc ldo8_voltage_map_desc = { |
| 63 | .min = 3000, .step = 100, .max = 3600, |
| 64 | }; |
| 65 | static const struct voltage_map_desc ldo9_voltage_map_desc = { |
| 66 | .min = 2800, .step = 100, .max = 3100, |
| 67 | }; |
| 68 | static const struct voltage_map_desc ldo10_voltage_map_desc = { |
| 69 | .min = 950, .step = 50, .max = 1300, |
| 70 | }; |
| 71 | static const struct voltage_map_desc ldo1213_voltage_map_desc = { |
| 72 | .min = 800, .step = 100, .max = 3300, |
| 73 | }; |
| 74 | static const struct voltage_map_desc ldo1415_voltage_map_desc = { |
| 75 | .min = 1200, .step = 100, .max = 3300, |
| 76 | }; |
| 77 | static const struct voltage_map_desc ldo1617_voltage_map_desc = { |
| 78 | .min = 1600, .step = 100, .max = 3600, |
| 79 | }; |
| 80 | static const struct voltage_map_desc buck12_voltage_map_desc = { |
| 81 | .min = 750, .step = 25, .max = 1525, |
| 82 | }; |
| 83 | static const struct voltage_map_desc buck3_voltage_map_desc = { |
| 84 | .min = 1600, .step = 100, .max = 3600, |
| 85 | }; |
| 86 | static const struct voltage_map_desc buck4_voltage_map_desc = { |
| 87 | .min = 800, .step = 100, .max = 2300, |
| 88 | }; |
| 89 | |
| 90 | static const struct voltage_map_desc *ldo_voltage_map[] = { |
| 91 | NULL, |
| 92 | NULL, |
| 93 | &ldo23_voltage_map_desc, /* LDO2 */ |
| 94 | &ldo23_voltage_map_desc, /* LDO3 */ |
| 95 | &ldo456711_voltage_map_desc, /* LDO4 */ |
| 96 | &ldo456711_voltage_map_desc, /* LDO5 */ |
| 97 | &ldo456711_voltage_map_desc, /* LDO6 */ |
| 98 | &ldo456711_voltage_map_desc, /* LDO7 */ |
| 99 | &ldo8_voltage_map_desc, /* LDO8 */ |
| 100 | &ldo9_voltage_map_desc, /* LDO9 */ |
| 101 | &ldo10_voltage_map_desc, /* LDO10 */ |
| 102 | &ldo456711_voltage_map_desc, /* LDO11 */ |
| 103 | &ldo1213_voltage_map_desc, /* LDO12 */ |
| 104 | &ldo1213_voltage_map_desc, /* LDO13 */ |
| 105 | &ldo1415_voltage_map_desc, /* LDO14 */ |
| 106 | &ldo1415_voltage_map_desc, /* LDO15 */ |
| 107 | &ldo1617_voltage_map_desc, /* LDO16 */ |
| 108 | &ldo1617_voltage_map_desc, /* LDO17 */ |
| 109 | &buck12_voltage_map_desc, /* BUCK1 */ |
| 110 | &buck12_voltage_map_desc, /* BUCK2 */ |
| 111 | &buck3_voltage_map_desc, /* BUCK3 */ |
| 112 | &buck4_voltage_map_desc, /* BUCK4 */ |
| 113 | }; |
| 114 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 115 | static int max8998_list_voltage(struct regulator_dev *rdev, |
| 116 | unsigned int selector) |
| 117 | { |
| 118 | const struct voltage_map_desc *desc; |
Axel Lin | 7b94791 | 2012-03-01 09:27:29 +0800 | [diff] [blame] | 119 | int ldo = rdev_get_id(rdev); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 120 | int val; |
| 121 | |
Axel Lin | 1b69d8e | 2010-08-04 14:34:10 +0800 | [diff] [blame] | 122 | if (ldo >= ARRAY_SIZE(ldo_voltage_map)) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 123 | return -EINVAL; |
| 124 | |
| 125 | desc = ldo_voltage_map[ldo]; |
| 126 | if (desc == NULL) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | val = desc->min + desc->step * selector; |
| 130 | if (val > desc->max) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | return val * 1000; |
| 134 | } |
| 135 | |
| 136 | static int max8998_get_enable_register(struct regulator_dev *rdev, |
| 137 | int *reg, int *shift) |
| 138 | { |
Axel Lin | 7b94791 | 2012-03-01 09:27:29 +0800 | [diff] [blame] | 139 | int ldo = rdev_get_id(rdev); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 140 | |
| 141 | switch (ldo) { |
| 142 | case MAX8998_LDO2 ... MAX8998_LDO5: |
| 143 | *reg = MAX8998_REG_ONOFF1; |
| 144 | *shift = 3 - (ldo - MAX8998_LDO2); |
| 145 | break; |
| 146 | case MAX8998_LDO6 ... MAX8998_LDO13: |
| 147 | *reg = MAX8998_REG_ONOFF2; |
| 148 | *shift = 7 - (ldo - MAX8998_LDO6); |
| 149 | break; |
| 150 | case MAX8998_LDO14 ... MAX8998_LDO17: |
| 151 | *reg = MAX8998_REG_ONOFF3; |
| 152 | *shift = 7 - (ldo - MAX8998_LDO14); |
| 153 | break; |
| 154 | case MAX8998_BUCK1 ... MAX8998_BUCK4: |
| 155 | *reg = MAX8998_REG_ONOFF1; |
| 156 | *shift = 7 - (ldo - MAX8998_BUCK1); |
| 157 | break; |
| 158 | case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG: |
| 159 | *reg = MAX8998_REG_ONOFF4; |
| 160 | *shift = 7 - (ldo - MAX8998_EN32KHZ_AP); |
| 161 | break; |
| 162 | case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2: |
| 163 | *reg = MAX8998_REG_CHGR2; |
| 164 | *shift = 7 - (ldo - MAX8998_ESAFEOUT1); |
| 165 | break; |
| 166 | default: |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int max8998_ldo_is_enabled(struct regulator_dev *rdev) |
| 174 | { |
| 175 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 176 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 177 | int ret, reg, shift = 8; |
| 178 | u8 val; |
| 179 | |
| 180 | ret = max8998_get_enable_register(rdev, ®, &shift); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 184 | ret = max8998_read_reg(i2c, reg, &val); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 185 | if (ret) |
| 186 | return ret; |
| 187 | |
| 188 | return val & (1 << shift); |
| 189 | } |
| 190 | |
| 191 | static int max8998_ldo_enable(struct regulator_dev *rdev) |
| 192 | { |
| 193 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 194 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 195 | int reg, shift = 8, ret; |
| 196 | |
| 197 | ret = max8998_get_enable_register(rdev, ®, &shift); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 201 | return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static int max8998_ldo_disable(struct regulator_dev *rdev) |
| 205 | { |
| 206 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 207 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 208 | int reg, shift = 8, ret; |
| 209 | |
| 210 | ret = max8998_get_enable_register(rdev, ®, &shift); |
| 211 | if (ret) |
| 212 | return ret; |
| 213 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 214 | return max8998_update_reg(i2c, reg, 0, 1<<shift); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static int max8998_get_voltage_register(struct regulator_dev *rdev, |
| 218 | int *_reg, int *_shift, int *_mask) |
| 219 | { |
Axel Lin | 7b94791 | 2012-03-01 09:27:29 +0800 | [diff] [blame] | 220 | int ldo = rdev_get_id(rdev); |
Lukasz Majewski | 889cd5a | 2010-09-27 14:32:25 +0200 | [diff] [blame] | 221 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 222 | int reg, shift = 0, mask = 0xff; |
| 223 | |
| 224 | switch (ldo) { |
| 225 | case MAX8998_LDO2 ... MAX8998_LDO3: |
| 226 | reg = MAX8998_REG_LDO2_LDO3; |
| 227 | mask = 0xf; |
| 228 | if (ldo == MAX8998_LDO2) |
| 229 | shift = 4; |
| 230 | else |
| 231 | shift = 0; |
| 232 | break; |
| 233 | case MAX8998_LDO4 ... MAX8998_LDO7: |
| 234 | reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4); |
| 235 | break; |
| 236 | case MAX8998_LDO8 ... MAX8998_LDO9: |
| 237 | reg = MAX8998_REG_LDO8_LDO9; |
| 238 | mask = 0xf; |
| 239 | if (ldo == MAX8998_LDO8) |
| 240 | shift = 4; |
| 241 | else |
| 242 | shift = 0; |
| 243 | break; |
| 244 | case MAX8998_LDO10 ... MAX8998_LDO11: |
| 245 | reg = MAX8998_REG_LDO10_LDO11; |
| 246 | if (ldo == MAX8998_LDO10) { |
| 247 | shift = 5; |
| 248 | mask = 0x7; |
| 249 | } else { |
| 250 | shift = 0; |
| 251 | mask = 0x1f; |
| 252 | } |
| 253 | break; |
| 254 | case MAX8998_LDO12 ... MAX8998_LDO17: |
| 255 | reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12); |
| 256 | break; |
| 257 | case MAX8998_BUCK1: |
Lukasz Majewski | 889cd5a | 2010-09-27 14:32:25 +0200 | [diff] [blame] | 258 | reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 259 | break; |
| 260 | case MAX8998_BUCK2: |
Lukasz Majewski | 889cd5a | 2010-09-27 14:32:25 +0200 | [diff] [blame] | 261 | reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 262 | break; |
| 263 | case MAX8998_BUCK3: |
| 264 | reg = MAX8998_REG_BUCK3; |
| 265 | break; |
| 266 | case MAX8998_BUCK4: |
| 267 | reg = MAX8998_REG_BUCK4; |
| 268 | break; |
| 269 | default: |
| 270 | return -EINVAL; |
| 271 | } |
| 272 | |
| 273 | *_reg = reg; |
| 274 | *_shift = shift; |
| 275 | *_mask = mask; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 280 | static int max8998_get_voltage_sel(struct regulator_dev *rdev) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 281 | { |
| 282 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 283 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 284 | int reg, shift = 0, mask, ret; |
| 285 | u8 val; |
| 286 | |
| 287 | ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); |
| 288 | if (ret) |
| 289 | return ret; |
| 290 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 291 | ret = max8998_read_reg(i2c, reg, &val); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 292 | if (ret) |
| 293 | return ret; |
| 294 | |
| 295 | val >>= shift; |
| 296 | val &= mask; |
| 297 | |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 298 | return val; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 301 | static int max8998_set_voltage_ldo(struct regulator_dev *rdev, |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 302 | int min_uV, int max_uV, unsigned *selector) |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 303 | { |
| 304 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 305 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 306 | int min_vol = min_uV / 1000, max_vol = max_uV / 1000; |
| 307 | const struct voltage_map_desc *desc; |
Axel Lin | 7b94791 | 2012-03-01 09:27:29 +0800 | [diff] [blame] | 308 | int ldo = rdev_get_id(rdev); |
Axel Lin | 2e42a7d | 2012-04-10 14:21:01 +0800 | [diff] [blame] | 309 | int reg, shift = 0, mask, ret, i; |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 310 | |
| 311 | if (ldo >= ARRAY_SIZE(ldo_voltage_map)) |
| 312 | return -EINVAL; |
| 313 | |
| 314 | desc = ldo_voltage_map[ldo]; |
| 315 | if (desc == NULL) |
| 316 | return -EINVAL; |
| 317 | |
| 318 | if (max_vol < desc->min || min_vol > desc->max) |
| 319 | return -EINVAL; |
| 320 | |
Axel Lin | 2e42a7d | 2012-04-10 14:21:01 +0800 | [diff] [blame] | 321 | if (min_vol < desc->min) |
| 322 | min_vol = desc->min; |
| 323 | |
| 324 | i = DIV_ROUND_UP(min_vol - desc->min, desc->step); |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 325 | |
| 326 | if (desc->min + desc->step*i > max_vol) |
| 327 | return -EINVAL; |
| 328 | |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 329 | *selector = i; |
| 330 | |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 331 | ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); |
| 332 | if (ret) |
| 333 | return ret; |
| 334 | |
| 335 | ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift); |
| 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 340 | static inline void buck1_gpio_set(int gpio1, int gpio2, int v) |
| 341 | { |
| 342 | gpio_set_value(gpio1, v & 0x1); |
| 343 | gpio_set_value(gpio2, (v >> 1) & 0x1); |
| 344 | } |
| 345 | |
| 346 | static inline void buck2_gpio_set(int gpio, int v) |
| 347 | { |
| 348 | gpio_set_value(gpio, v & 0x1); |
| 349 | } |
| 350 | |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 351 | static int max8998_set_voltage_buck(struct regulator_dev *rdev, |
Mark Brown | f4d6adf | 2010-11-19 12:07:44 +0000 | [diff] [blame] | 352 | int min_uV, int max_uV, unsigned *selector) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 353 | { |
| 354 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 355 | struct max8998_platform_data *pdata = |
| 356 | dev_get_platdata(max8998->iodev->dev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 357 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 358 | int min_vol = min_uV / 1000, max_vol = max_uV / 1000; |
| 359 | const struct voltage_map_desc *desc; |
Axel Lin | 7b94791 | 2012-03-01 09:27:29 +0800 | [diff] [blame] | 360 | int buck = rdev_get_id(rdev); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 361 | int reg, shift = 0, mask, ret; |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame^] | 362 | int i, j, previous_sel; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 363 | static u8 buck1_last_val; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 364 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 365 | if (buck >= ARRAY_SIZE(ldo_voltage_map)) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 366 | return -EINVAL; |
| 367 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 368 | desc = ldo_voltage_map[buck]; |
| 369 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 370 | if (desc == NULL) |
| 371 | return -EINVAL; |
| 372 | |
| 373 | if (max_vol < desc->min || min_vol > desc->max) |
| 374 | return -EINVAL; |
| 375 | |
Axel Lin | 2e42a7d | 2012-04-10 14:21:01 +0800 | [diff] [blame] | 376 | if (min_vol < desc->min) |
| 377 | min_vol = desc->min; |
| 378 | |
| 379 | i = DIV_ROUND_UP(min_vol - desc->min, desc->step); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 380 | |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 381 | if (desc->min + desc->step*i > max_vol) |
| 382 | return -EINVAL; |
| 383 | |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 384 | *selector = i; |
| 385 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 386 | ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); |
| 387 | if (ret) |
| 388 | return ret; |
| 389 | |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 390 | previous_sel = max8998_get_voltage_sel(rdev); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 391 | |
| 392 | /* Check if voltage needs to be changed */ |
| 393 | /* if previous_voltage equal new voltage, return */ |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 394 | if (previous_sel == i) { |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 395 | dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n", |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 396 | max8998_list_voltage(rdev, previous_sel), |
| 397 | max8998_list_voltage(rdev, i)); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | switch (buck) { |
| 402 | case MAX8998_BUCK1: |
| 403 | dev_dbg(max8998->dev, |
Joe Perches | 85ee7a1 | 2011-04-23 20:38:19 -0700 | [diff] [blame] | 404 | "BUCK1, i:%d, buck1_vol1:%d, buck1_vol2:%d\n" |
| 405 | "buck1_vol3:%d, buck1_vol4:%d\n", |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 406 | i, max8998->buck1_vol[0], max8998->buck1_vol[1], |
| 407 | max8998->buck1_vol[2], max8998->buck1_vol[3]); |
| 408 | |
| 409 | if (gpio_is_valid(pdata->buck1_set1) && |
| 410 | gpio_is_valid(pdata->buck1_set2)) { |
| 411 | |
| 412 | /* check if requested voltage */ |
| 413 | /* value is already defined */ |
| 414 | for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) { |
| 415 | if (max8998->buck1_vol[j] == i) { |
| 416 | max8998->buck1_idx = j; |
| 417 | buck1_gpio_set(pdata->buck1_set1, |
| 418 | pdata->buck1_set2, j); |
| 419 | goto buck1_exit; |
| 420 | } |
| 421 | } |
| 422 | |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 423 | if (pdata->buck_voltage_lock) |
| 424 | return -EINVAL; |
| 425 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 426 | /* no predefine regulator found */ |
| 427 | max8998->buck1_idx = (buck1_last_val % 2) + 2; |
| 428 | dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n", |
| 429 | max8998->buck1_idx); |
| 430 | max8998->buck1_vol[max8998->buck1_idx] = i; |
| 431 | ret = max8998_get_voltage_register(rdev, ®, |
| 432 | &shift, |
| 433 | &mask); |
| 434 | ret = max8998_write_reg(i2c, reg, i); |
| 435 | buck1_gpio_set(pdata->buck1_set1, |
| 436 | pdata->buck1_set2, max8998->buck1_idx); |
| 437 | buck1_last_val++; |
| 438 | buck1_exit: |
| 439 | dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n", |
| 440 | i2c->name, gpio_get_value(pdata->buck1_set1), |
| 441 | gpio_get_value(pdata->buck1_set2)); |
| 442 | break; |
| 443 | } else { |
| 444 | ret = max8998_write_reg(i2c, reg, i); |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 445 | } |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 446 | break; |
| 447 | |
| 448 | case MAX8998_BUCK2: |
| 449 | dev_dbg(max8998->dev, |
| 450 | "BUCK2, i:%d buck2_vol1:%d, buck2_vol2:%d\n" |
| 451 | , i, max8998->buck2_vol[0], max8998->buck2_vol[1]); |
| 452 | if (gpio_is_valid(pdata->buck2_set3)) { |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 453 | |
| 454 | /* check if requested voltage */ |
| 455 | /* value is already defined */ |
| 456 | for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) { |
| 457 | if (max8998->buck2_vol[j] == i) { |
| 458 | max8998->buck2_idx = j; |
| 459 | buck2_gpio_set(pdata->buck2_set3, j); |
| 460 | goto buck2_exit; |
| 461 | } |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 462 | } |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 463 | |
| 464 | if (pdata->buck_voltage_lock) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | max8998_get_voltage_register(rdev, |
| 468 | ®, &shift, &mask); |
| 469 | ret = max8998_write_reg(i2c, reg, i); |
| 470 | max8998->buck2_vol[max8998->buck2_idx] = i; |
| 471 | buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx); |
| 472 | buck2_exit: |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 473 | dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name, |
| 474 | gpio_get_value(pdata->buck2_set3)); |
| 475 | } else { |
| 476 | ret = max8998_write_reg(i2c, reg, i); |
| 477 | } |
| 478 | break; |
| 479 | |
| 480 | case MAX8998_BUCK3: |
| 481 | case MAX8998_BUCK4: |
| 482 | ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift); |
| 483 | break; |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame^] | 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev, |
| 490 | unsigned int old_selector, |
| 491 | unsigned int new_selector) |
| 492 | { |
| 493 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 494 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 495 | const struct voltage_map_desc *desc; |
| 496 | int buck = rdev_get_id(rdev); |
| 497 | u8 val = 0; |
| 498 | int difference, ret; |
| 499 | |
| 500 | if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4) |
| 501 | return -EINVAL; |
| 502 | |
| 503 | desc = ldo_voltage_map[buck]; |
| 504 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 505 | /* Voltage stabilization */ |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame^] | 506 | ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val); |
| 507 | if (ret) |
| 508 | return ret; |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 509 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 510 | /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */ |
| 511 | /* MAX8998 has ENRAMP bit implemented, so test it*/ |
| 512 | if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP)) |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame^] | 513 | return 0; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 514 | |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame^] | 515 | difference = (new_selector - old_selector) * desc->step; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 516 | if (difference > 0) |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame^] | 517 | return difference / ((val & 0x0f) + 1); |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 518 | |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame^] | 519 | return 0; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static struct regulator_ops max8998_ldo_ops = { |
| 523 | .list_voltage = max8998_list_voltage, |
| 524 | .is_enabled = max8998_ldo_is_enabled, |
| 525 | .enable = max8998_ldo_enable, |
| 526 | .disable = max8998_ldo_disable, |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 527 | .get_voltage_sel = max8998_get_voltage_sel, |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 528 | .set_voltage = max8998_set_voltage_ldo, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 529 | .set_suspend_enable = max8998_ldo_enable, |
| 530 | .set_suspend_disable = max8998_ldo_disable, |
| 531 | }; |
| 532 | |
| 533 | static struct regulator_ops max8998_buck_ops = { |
| 534 | .list_voltage = max8998_list_voltage, |
| 535 | .is_enabled = max8998_ldo_is_enabled, |
| 536 | .enable = max8998_ldo_enable, |
| 537 | .disable = max8998_ldo_disable, |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 538 | .get_voltage_sel = max8998_get_voltage_sel, |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 539 | .set_voltage = max8998_set_voltage_buck, |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame^] | 540 | .set_voltage_time_sel = max8998_set_voltage_buck_time_sel, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 541 | .set_suspend_enable = max8998_ldo_enable, |
| 542 | .set_suspend_disable = max8998_ldo_disable, |
| 543 | }; |
| 544 | |
| 545 | static struct regulator_ops max8998_others_ops = { |
| 546 | .is_enabled = max8998_ldo_is_enabled, |
| 547 | .enable = max8998_ldo_enable, |
| 548 | .disable = max8998_ldo_disable, |
| 549 | .set_suspend_enable = max8998_ldo_enable, |
| 550 | .set_suspend_disable = max8998_ldo_disable, |
| 551 | }; |
| 552 | |
| 553 | static struct regulator_desc regulators[] = { |
| 554 | { |
| 555 | .name = "LDO2", |
| 556 | .id = MAX8998_LDO2, |
| 557 | .ops = &max8998_ldo_ops, |
| 558 | .type = REGULATOR_VOLTAGE, |
| 559 | .owner = THIS_MODULE, |
| 560 | }, { |
| 561 | .name = "LDO3", |
| 562 | .id = MAX8998_LDO3, |
| 563 | .ops = &max8998_ldo_ops, |
| 564 | .type = REGULATOR_VOLTAGE, |
| 565 | .owner = THIS_MODULE, |
| 566 | }, { |
| 567 | .name = "LDO4", |
| 568 | .id = MAX8998_LDO4, |
| 569 | .ops = &max8998_ldo_ops, |
| 570 | .type = REGULATOR_VOLTAGE, |
| 571 | .owner = THIS_MODULE, |
| 572 | }, { |
| 573 | .name = "LDO5", |
| 574 | .id = MAX8998_LDO5, |
| 575 | .ops = &max8998_ldo_ops, |
| 576 | .type = REGULATOR_VOLTAGE, |
| 577 | .owner = THIS_MODULE, |
| 578 | }, { |
| 579 | .name = "LDO6", |
| 580 | .id = MAX8998_LDO6, |
| 581 | .ops = &max8998_ldo_ops, |
| 582 | .type = REGULATOR_VOLTAGE, |
| 583 | .owner = THIS_MODULE, |
| 584 | }, { |
| 585 | .name = "LDO7", |
| 586 | .id = MAX8998_LDO7, |
| 587 | .ops = &max8998_ldo_ops, |
| 588 | .type = REGULATOR_VOLTAGE, |
| 589 | .owner = THIS_MODULE, |
| 590 | }, { |
| 591 | .name = "LDO8", |
| 592 | .id = MAX8998_LDO8, |
| 593 | .ops = &max8998_ldo_ops, |
| 594 | .type = REGULATOR_VOLTAGE, |
| 595 | .owner = THIS_MODULE, |
| 596 | }, { |
| 597 | .name = "LDO9", |
| 598 | .id = MAX8998_LDO9, |
| 599 | .ops = &max8998_ldo_ops, |
| 600 | .type = REGULATOR_VOLTAGE, |
| 601 | .owner = THIS_MODULE, |
| 602 | }, { |
| 603 | .name = "LDO10", |
| 604 | .id = MAX8998_LDO10, |
| 605 | .ops = &max8998_ldo_ops, |
| 606 | .type = REGULATOR_VOLTAGE, |
| 607 | .owner = THIS_MODULE, |
| 608 | }, { |
| 609 | .name = "LDO11", |
| 610 | .id = MAX8998_LDO11, |
| 611 | .ops = &max8998_ldo_ops, |
| 612 | .type = REGULATOR_VOLTAGE, |
| 613 | .owner = THIS_MODULE, |
| 614 | }, { |
| 615 | .name = "LDO12", |
| 616 | .id = MAX8998_LDO12, |
| 617 | .ops = &max8998_ldo_ops, |
| 618 | .type = REGULATOR_VOLTAGE, |
| 619 | .owner = THIS_MODULE, |
| 620 | }, { |
| 621 | .name = "LDO13", |
| 622 | .id = MAX8998_LDO13, |
| 623 | .ops = &max8998_ldo_ops, |
| 624 | .type = REGULATOR_VOLTAGE, |
| 625 | .owner = THIS_MODULE, |
| 626 | }, { |
| 627 | .name = "LDO14", |
| 628 | .id = MAX8998_LDO14, |
| 629 | .ops = &max8998_ldo_ops, |
| 630 | .type = REGULATOR_VOLTAGE, |
| 631 | .owner = THIS_MODULE, |
| 632 | }, { |
| 633 | .name = "LDO15", |
| 634 | .id = MAX8998_LDO15, |
| 635 | .ops = &max8998_ldo_ops, |
| 636 | .type = REGULATOR_VOLTAGE, |
| 637 | .owner = THIS_MODULE, |
| 638 | }, { |
| 639 | .name = "LDO16", |
| 640 | .id = MAX8998_LDO16, |
| 641 | .ops = &max8998_ldo_ops, |
| 642 | .type = REGULATOR_VOLTAGE, |
| 643 | .owner = THIS_MODULE, |
| 644 | }, { |
| 645 | .name = "LDO17", |
| 646 | .id = MAX8998_LDO17, |
| 647 | .ops = &max8998_ldo_ops, |
| 648 | .type = REGULATOR_VOLTAGE, |
| 649 | .owner = THIS_MODULE, |
| 650 | }, { |
| 651 | .name = "BUCK1", |
| 652 | .id = MAX8998_BUCK1, |
| 653 | .ops = &max8998_buck_ops, |
| 654 | .type = REGULATOR_VOLTAGE, |
| 655 | .owner = THIS_MODULE, |
| 656 | }, { |
| 657 | .name = "BUCK2", |
| 658 | .id = MAX8998_BUCK2, |
| 659 | .ops = &max8998_buck_ops, |
| 660 | .type = REGULATOR_VOLTAGE, |
| 661 | .owner = THIS_MODULE, |
| 662 | }, { |
| 663 | .name = "BUCK3", |
| 664 | .id = MAX8998_BUCK3, |
| 665 | .ops = &max8998_buck_ops, |
| 666 | .type = REGULATOR_VOLTAGE, |
| 667 | .owner = THIS_MODULE, |
| 668 | }, { |
| 669 | .name = "BUCK4", |
| 670 | .id = MAX8998_BUCK4, |
| 671 | .ops = &max8998_buck_ops, |
| 672 | .type = REGULATOR_VOLTAGE, |
| 673 | .owner = THIS_MODULE, |
| 674 | }, { |
| 675 | .name = "EN32KHz AP", |
| 676 | .id = MAX8998_EN32KHZ_AP, |
| 677 | .ops = &max8998_others_ops, |
| 678 | .type = REGULATOR_VOLTAGE, |
| 679 | .owner = THIS_MODULE, |
| 680 | }, { |
| 681 | .name = "EN32KHz CP", |
| 682 | .id = MAX8998_EN32KHZ_CP, |
| 683 | .ops = &max8998_others_ops, |
| 684 | .type = REGULATOR_VOLTAGE, |
| 685 | .owner = THIS_MODULE, |
| 686 | }, { |
| 687 | .name = "ENVICHG", |
| 688 | .id = MAX8998_ENVICHG, |
| 689 | .ops = &max8998_others_ops, |
| 690 | .type = REGULATOR_VOLTAGE, |
| 691 | .owner = THIS_MODULE, |
| 692 | }, { |
| 693 | .name = "ESAFEOUT1", |
| 694 | .id = MAX8998_ESAFEOUT1, |
| 695 | .ops = &max8998_others_ops, |
| 696 | .type = REGULATOR_VOLTAGE, |
| 697 | .owner = THIS_MODULE, |
| 698 | }, { |
| 699 | .name = "ESAFEOUT2", |
| 700 | .id = MAX8998_ESAFEOUT2, |
| 701 | .ops = &max8998_others_ops, |
| 702 | .type = REGULATOR_VOLTAGE, |
| 703 | .owner = THIS_MODULE, |
| 704 | } |
| 705 | }; |
| 706 | |
| 707 | static __devinit int max8998_pmic_probe(struct platform_device *pdev) |
| 708 | { |
| 709 | struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
| 710 | struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev); |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 711 | struct regulator_config config = { }; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 712 | struct regulator_dev **rdev; |
| 713 | struct max8998_data *max8998; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 714 | struct i2c_client *i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 715 | int i, ret, size; |
| 716 | |
| 717 | if (!pdata) { |
| 718 | dev_err(pdev->dev.parent, "No platform init data supplied\n"); |
| 719 | return -ENODEV; |
| 720 | } |
| 721 | |
| 722 | max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL); |
| 723 | if (!max8998) |
| 724 | return -ENOMEM; |
| 725 | |
Axel Lin | 11fa0d1 | 2010-08-19 10:29:19 +0800 | [diff] [blame] | 726 | size = sizeof(struct regulator_dev *) * pdata->num_regulators; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 727 | max8998->rdev = kzalloc(size, GFP_KERNEL); |
| 728 | if (!max8998->rdev) { |
| 729 | kfree(max8998); |
| 730 | return -ENOMEM; |
| 731 | } |
| 732 | |
| 733 | rdev = max8998->rdev; |
Axel Lin | 747cc85 | 2010-08-27 16:37:34 +0800 | [diff] [blame] | 734 | max8998->dev = &pdev->dev; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 735 | max8998->iodev = iodev; |
Axel Lin | c356cbc | 2010-08-22 15:26:49 +0800 | [diff] [blame] | 736 | max8998->num_regulators = pdata->num_regulators; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 737 | platform_set_drvdata(pdev, max8998); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 738 | i2c = max8998->iodev->i2c; |
| 739 | |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 740 | max8998->buck1_idx = pdata->buck1_default_idx; |
| 741 | max8998->buck2_idx = pdata->buck2_default_idx; |
| 742 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 743 | /* NOTE: */ |
| 744 | /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */ |
| 745 | /* will be displayed */ |
| 746 | |
| 747 | /* Check if MAX8998 voltage selection GPIOs are defined */ |
| 748 | if (gpio_is_valid(pdata->buck1_set1) && |
| 749 | gpio_is_valid(pdata->buck1_set2)) { |
| 750 | /* Check if SET1 is not equal to 0 */ |
| 751 | if (!pdata->buck1_set1) { |
| 752 | printk(KERN_ERR "MAX8998 SET1 GPIO defined as 0 !\n"); |
| 753 | WARN_ON(!pdata->buck1_set1); |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 754 | ret = -EIO; |
| 755 | goto err_free_mem; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 756 | } |
| 757 | /* Check if SET2 is not equal to 0 */ |
| 758 | if (!pdata->buck1_set2) { |
| 759 | printk(KERN_ERR "MAX8998 SET2 GPIO defined as 0 !\n"); |
| 760 | WARN_ON(!pdata->buck1_set2); |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 761 | ret = -EIO; |
| 762 | goto err_free_mem; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1"); |
| 766 | gpio_direction_output(pdata->buck1_set1, |
| 767 | max8998->buck1_idx & 0x1); |
| 768 | |
| 769 | |
| 770 | gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2"); |
| 771 | gpio_direction_output(pdata->buck1_set2, |
| 772 | (max8998->buck1_idx >> 1) & 0x1); |
| 773 | /* Set predefined value for BUCK1 register 1 */ |
| 774 | i = 0; |
| 775 | while (buck12_voltage_map_desc.min + |
| 776 | buck12_voltage_map_desc.step*i |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 777 | < (pdata->buck1_voltage1 / 1000)) |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 778 | i++; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 779 | max8998->buck1_vol[0] = i; |
| 780 | ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i); |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 781 | if (ret) |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 782 | goto err_free_mem; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 783 | |
| 784 | /* Set predefined value for BUCK1 register 2 */ |
| 785 | i = 0; |
| 786 | while (buck12_voltage_map_desc.min + |
| 787 | buck12_voltage_map_desc.step*i |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 788 | < (pdata->buck1_voltage2 / 1000)) |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 789 | i++; |
| 790 | |
| 791 | max8998->buck1_vol[1] = i; |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 792 | ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i); |
| 793 | if (ret) |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 794 | goto err_free_mem; |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 795 | |
| 796 | /* Set predefined value for BUCK1 register 3 */ |
| 797 | i = 0; |
| 798 | while (buck12_voltage_map_desc.min + |
| 799 | buck12_voltage_map_desc.step*i |
| 800 | < (pdata->buck1_voltage3 / 1000)) |
| 801 | i++; |
| 802 | |
| 803 | max8998->buck1_vol[2] = i; |
| 804 | ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE3, i); |
| 805 | if (ret) |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 806 | goto err_free_mem; |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 807 | |
| 808 | /* Set predefined value for BUCK1 register 4 */ |
| 809 | i = 0; |
| 810 | while (buck12_voltage_map_desc.min + |
| 811 | buck12_voltage_map_desc.step*i |
| 812 | < (pdata->buck1_voltage4 / 1000)) |
| 813 | i++; |
| 814 | |
| 815 | max8998->buck1_vol[3] = i; |
| 816 | ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE4, i); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 817 | if (ret) |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 818 | goto err_free_mem; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 819 | |
| 820 | } |
| 821 | |
| 822 | if (gpio_is_valid(pdata->buck2_set3)) { |
| 823 | /* Check if SET3 is not equal to 0 */ |
| 824 | if (!pdata->buck2_set3) { |
| 825 | printk(KERN_ERR "MAX8998 SET3 GPIO defined as 0 !\n"); |
| 826 | WARN_ON(!pdata->buck2_set3); |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 827 | ret = -EIO; |
| 828 | goto err_free_mem; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 829 | } |
| 830 | gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3"); |
| 831 | gpio_direction_output(pdata->buck2_set3, |
| 832 | max8998->buck2_idx & 0x1); |
| 833 | |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 834 | /* BUCK2 register 1 */ |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 835 | i = 0; |
| 836 | while (buck12_voltage_map_desc.min + |
| 837 | buck12_voltage_map_desc.step*i |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 838 | < (pdata->buck2_voltage1 / 1000)) |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 839 | i++; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 840 | max8998->buck2_vol[0] = i; |
| 841 | ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i); |
| 842 | if (ret) |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 843 | goto err_free_mem; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 844 | |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 845 | /* BUCK2 register 2 */ |
| 846 | i = 0; |
| 847 | while (buck12_voltage_map_desc.min + |
| 848 | buck12_voltage_map_desc.step*i |
| 849 | < (pdata->buck2_voltage2 / 1000)) |
| 850 | i++; |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 851 | max8998->buck2_vol[1] = i; |
| 852 | ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE2, i); |
| 853 | if (ret) |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 854 | goto err_free_mem; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 855 | } |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 856 | |
| 857 | for (i = 0; i < pdata->num_regulators; i++) { |
| 858 | const struct voltage_map_desc *desc; |
| 859 | int id = pdata->regulators[i].id; |
| 860 | int index = id - MAX8998_LDO2; |
| 861 | |
| 862 | desc = ldo_voltage_map[id]; |
| 863 | if (desc && regulators[index].ops != &max8998_others_ops) { |
| 864 | int count = (desc->max - desc->min) / desc->step + 1; |
| 865 | regulators[index].n_voltages = count; |
| 866 | } |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 867 | |
| 868 | config.dev = max8998->dev; |
| 869 | config.init_data = pdata->regulators[i].initdata; |
| 870 | config.driver_data = max8998; |
| 871 | |
| 872 | rdev[i] = regulator_register(®ulators[index], &config); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 873 | if (IS_ERR(rdev[i])) { |
| 874 | ret = PTR_ERR(rdev[i]); |
| 875 | dev_err(max8998->dev, "regulator init failed\n"); |
| 876 | rdev[i] = NULL; |
| 877 | goto err; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | |
| 882 | return 0; |
| 883 | err: |
Axel Lin | 11fa0d1 | 2010-08-19 10:29:19 +0800 | [diff] [blame] | 884 | for (i = 0; i < max8998->num_regulators; i++) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 885 | if (rdev[i]) |
| 886 | regulator_unregister(rdev[i]); |
| 887 | |
Axel Lin | 6be6e96 | 2011-05-13 14:54:06 +0800 | [diff] [blame] | 888 | err_free_mem: |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 889 | kfree(max8998->rdev); |
| 890 | kfree(max8998); |
| 891 | |
| 892 | return ret; |
| 893 | } |
| 894 | |
| 895 | static int __devexit max8998_pmic_remove(struct platform_device *pdev) |
| 896 | { |
| 897 | struct max8998_data *max8998 = platform_get_drvdata(pdev); |
| 898 | struct regulator_dev **rdev = max8998->rdev; |
| 899 | int i; |
| 900 | |
Axel Lin | 11fa0d1 | 2010-08-19 10:29:19 +0800 | [diff] [blame] | 901 | for (i = 0; i < max8998->num_regulators; i++) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 902 | if (rdev[i]) |
| 903 | regulator_unregister(rdev[i]); |
| 904 | |
| 905 | kfree(max8998->rdev); |
| 906 | kfree(max8998); |
| 907 | |
| 908 | return 0; |
| 909 | } |
| 910 | |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 911 | static const struct platform_device_id max8998_pmic_id[] = { |
| 912 | { "max8998-pmic", TYPE_MAX8998 }, |
| 913 | { "lp3974-pmic", TYPE_LP3974 }, |
| 914 | { } |
| 915 | }; |
Axel Lin | a51b907 | 2011-03-26 23:28:42 +0800 | [diff] [blame] | 916 | MODULE_DEVICE_TABLE(platform, max8998_pmic_id); |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 917 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 918 | static struct platform_driver max8998_pmic_driver = { |
| 919 | .driver = { |
| 920 | .name = "max8998-pmic", |
| 921 | .owner = THIS_MODULE, |
| 922 | }, |
| 923 | .probe = max8998_pmic_probe, |
| 924 | .remove = __devexit_p(max8998_pmic_remove), |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 925 | .id_table = max8998_pmic_id, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 926 | }; |
| 927 | |
| 928 | static int __init max8998_pmic_init(void) |
| 929 | { |
| 930 | return platform_driver_register(&max8998_pmic_driver); |
| 931 | } |
| 932 | subsys_initcall(max8998_pmic_init); |
| 933 | |
| 934 | static void __exit max8998_pmic_cleanup(void) |
| 935 | { |
| 936 | platform_driver_unregister(&max8998_pmic_driver); |
| 937 | } |
| 938 | module_exit(max8998_pmic_cleanup); |
| 939 | |
| 940 | MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver"); |
| 941 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 942 | MODULE_LICENSE("GPL"); |