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