0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/module.h>
0010 #include <linux/init.h>
0011 #include <linux/i2c.h>
0012 #include <linux/err.h>
0013 #include <linux/gpio.h>
0014 #include <linux/slab.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/mutex.h>
0017 #include <linux/of.h>
0018 #include <linux/of_gpio.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/regulator/driver.h>
0021 #include <linux/regulator/of_regulator.h>
0022 #include <linux/mfd/max8998.h>
0023 #include <linux/mfd/max8998-private.h>
0024
0025 struct max8998_data {
0026 struct device *dev;
0027 struct max8998_dev *iodev;
0028 int num_regulators;
0029 u8 buck1_vol[4];
0030 u8 buck2_vol[2];
0031 unsigned int buck1_idx;
0032
0033 unsigned int buck2_idx;
0034 };
0035
0036 static const unsigned int charger_current_table[] = {
0037 90000, 380000, 475000, 550000, 570000, 600000, 700000, 800000,
0038 };
0039
0040 static int max8998_get_enable_register(struct regulator_dev *rdev,
0041 int *reg, int *shift)
0042 {
0043 int ldo = rdev_get_id(rdev);
0044
0045 switch (ldo) {
0046 case MAX8998_LDO2 ... MAX8998_LDO5:
0047 *reg = MAX8998_REG_ONOFF1;
0048 *shift = 3 - (ldo - MAX8998_LDO2);
0049 break;
0050 case MAX8998_LDO6 ... MAX8998_LDO13:
0051 *reg = MAX8998_REG_ONOFF2;
0052 *shift = 7 - (ldo - MAX8998_LDO6);
0053 break;
0054 case MAX8998_LDO14 ... MAX8998_LDO17:
0055 *reg = MAX8998_REG_ONOFF3;
0056 *shift = 7 - (ldo - MAX8998_LDO14);
0057 break;
0058 case MAX8998_BUCK1 ... MAX8998_BUCK4:
0059 *reg = MAX8998_REG_ONOFF1;
0060 *shift = 7 - (ldo - MAX8998_BUCK1);
0061 break;
0062 case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
0063 *reg = MAX8998_REG_ONOFF4;
0064 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
0065 break;
0066 case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
0067 *reg = MAX8998_REG_CHGR2;
0068 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
0069 break;
0070 case MAX8998_CHARGER:
0071 *reg = MAX8998_REG_CHGR2;
0072 *shift = 0;
0073 break;
0074 default:
0075 return -EINVAL;
0076 }
0077
0078 return 0;
0079 }
0080
0081 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
0082 {
0083 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0084 struct i2c_client *i2c = max8998->iodev->i2c;
0085 int ret, reg, shift = 8;
0086 u8 val;
0087
0088 ret = max8998_get_enable_register(rdev, ®, &shift);
0089 if (ret)
0090 return ret;
0091
0092 ret = max8998_read_reg(i2c, reg, &val);
0093 if (ret)
0094 return ret;
0095
0096 return val & (1 << shift);
0097 }
0098
0099 static int max8998_ldo_is_enabled_inverted(struct regulator_dev *rdev)
0100 {
0101 return (!max8998_ldo_is_enabled(rdev));
0102 }
0103
0104 static int max8998_ldo_enable(struct regulator_dev *rdev)
0105 {
0106 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0107 struct i2c_client *i2c = max8998->iodev->i2c;
0108 int reg, shift = 8, ret;
0109
0110 ret = max8998_get_enable_register(rdev, ®, &shift);
0111 if (ret)
0112 return ret;
0113
0114 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
0115 }
0116
0117 static int max8998_ldo_disable(struct regulator_dev *rdev)
0118 {
0119 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0120 struct i2c_client *i2c = max8998->iodev->i2c;
0121 int reg, shift = 8, ret;
0122
0123 ret = max8998_get_enable_register(rdev, ®, &shift);
0124 if (ret)
0125 return ret;
0126
0127 return max8998_update_reg(i2c, reg, 0, 1<<shift);
0128 }
0129
0130 static int max8998_get_voltage_register(struct regulator_dev *rdev,
0131 int *_reg, int *_shift, int *_mask)
0132 {
0133 int ldo = rdev_get_id(rdev);
0134 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0135 int reg, shift = 0, mask = 0xff;
0136
0137 switch (ldo) {
0138 case MAX8998_LDO2 ... MAX8998_LDO3:
0139 reg = MAX8998_REG_LDO2_LDO3;
0140 mask = 0xf;
0141 if (ldo == MAX8998_LDO2)
0142 shift = 4;
0143 else
0144 shift = 0;
0145 break;
0146 case MAX8998_LDO4 ... MAX8998_LDO7:
0147 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
0148 break;
0149 case MAX8998_LDO8 ... MAX8998_LDO9:
0150 reg = MAX8998_REG_LDO8_LDO9;
0151 mask = 0xf;
0152 if (ldo == MAX8998_LDO8)
0153 shift = 4;
0154 else
0155 shift = 0;
0156 break;
0157 case MAX8998_LDO10 ... MAX8998_LDO11:
0158 reg = MAX8998_REG_LDO10_LDO11;
0159 if (ldo == MAX8998_LDO10) {
0160 shift = 5;
0161 mask = 0x7;
0162 } else {
0163 shift = 0;
0164 mask = 0x1f;
0165 }
0166 break;
0167 case MAX8998_LDO12 ... MAX8998_LDO17:
0168 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
0169 break;
0170 case MAX8998_BUCK1:
0171 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
0172 break;
0173 case MAX8998_BUCK2:
0174 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
0175 break;
0176 case MAX8998_BUCK3:
0177 reg = MAX8998_REG_BUCK3;
0178 break;
0179 case MAX8998_BUCK4:
0180 reg = MAX8998_REG_BUCK4;
0181 break;
0182 default:
0183 return -EINVAL;
0184 }
0185
0186 *_reg = reg;
0187 *_shift = shift;
0188 *_mask = mask;
0189
0190 return 0;
0191 }
0192
0193 static int max8998_get_voltage_sel(struct regulator_dev *rdev)
0194 {
0195 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0196 struct i2c_client *i2c = max8998->iodev->i2c;
0197 int reg, shift = 0, mask, ret;
0198 u8 val;
0199
0200 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask);
0201 if (ret)
0202 return ret;
0203
0204 ret = max8998_read_reg(i2c, reg, &val);
0205 if (ret)
0206 return ret;
0207
0208 val >>= shift;
0209 val &= mask;
0210
0211 return val;
0212 }
0213
0214 static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev,
0215 unsigned selector)
0216 {
0217 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0218 struct i2c_client *i2c = max8998->iodev->i2c;
0219 int reg, shift = 0, mask, ret;
0220
0221 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask);
0222 if (ret)
0223 return ret;
0224
0225 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift);
0226
0227 return ret;
0228 }
0229
0230 static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
0231 {
0232 gpio_set_value(gpio1, v & 0x1);
0233 gpio_set_value(gpio2, (v >> 1) & 0x1);
0234 }
0235
0236 static inline void buck2_gpio_set(int gpio, int v)
0237 {
0238 gpio_set_value(gpio, v & 0x1);
0239 }
0240
0241 static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev,
0242 unsigned selector)
0243 {
0244 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0245 struct max8998_platform_data *pdata = max8998->iodev->pdata;
0246 struct i2c_client *i2c = max8998->iodev->i2c;
0247 int buck = rdev_get_id(rdev);
0248 int reg, shift = 0, mask, ret, j;
0249 static u8 buck1_last_val;
0250
0251 ret = max8998_get_voltage_register(rdev, ®, &shift, &mask);
0252 if (ret)
0253 return ret;
0254
0255 switch (buck) {
0256 case MAX8998_BUCK1:
0257 dev_dbg(max8998->dev,
0258 "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n"
0259 "buck1_vol3:%d, buck1_vol4:%d\n",
0260 selector, max8998->buck1_vol[0], max8998->buck1_vol[1],
0261 max8998->buck1_vol[2], max8998->buck1_vol[3]);
0262
0263 if (gpio_is_valid(pdata->buck1_set1) &&
0264 gpio_is_valid(pdata->buck1_set2)) {
0265
0266
0267
0268 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
0269 if (max8998->buck1_vol[j] == selector) {
0270 max8998->buck1_idx = j;
0271 buck1_gpio_set(pdata->buck1_set1,
0272 pdata->buck1_set2, j);
0273 goto buck1_exit;
0274 }
0275 }
0276
0277 if (pdata->buck_voltage_lock)
0278 return -EINVAL;
0279
0280
0281 max8998->buck1_idx = (buck1_last_val % 2) + 2;
0282 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
0283 max8998->buck1_idx);
0284 max8998->buck1_vol[max8998->buck1_idx] = selector;
0285 ret = max8998_get_voltage_register(rdev, ®,
0286 &shift,
0287 &mask);
0288 ret = max8998_write_reg(i2c, reg, selector);
0289 buck1_gpio_set(pdata->buck1_set1,
0290 pdata->buck1_set2, max8998->buck1_idx);
0291 buck1_last_val++;
0292 buck1_exit:
0293 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
0294 i2c->name, gpio_get_value(pdata->buck1_set1),
0295 gpio_get_value(pdata->buck1_set2));
0296 break;
0297 } else {
0298 ret = max8998_write_reg(i2c, reg, selector);
0299 }
0300 break;
0301
0302 case MAX8998_BUCK2:
0303 dev_dbg(max8998->dev,
0304 "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n",
0305 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]);
0306 if (gpio_is_valid(pdata->buck2_set3)) {
0307
0308
0309
0310 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
0311 if (max8998->buck2_vol[j] == selector) {
0312 max8998->buck2_idx = j;
0313 buck2_gpio_set(pdata->buck2_set3, j);
0314 goto buck2_exit;
0315 }
0316 }
0317
0318 if (pdata->buck_voltage_lock)
0319 return -EINVAL;
0320
0321 max8998_get_voltage_register(rdev,
0322 ®, &shift, &mask);
0323 ret = max8998_write_reg(i2c, reg, selector);
0324 max8998->buck2_vol[max8998->buck2_idx] = selector;
0325 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
0326 buck2_exit:
0327 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
0328 gpio_get_value(pdata->buck2_set3));
0329 } else {
0330 ret = max8998_write_reg(i2c, reg, selector);
0331 }
0332 break;
0333
0334 case MAX8998_BUCK3:
0335 case MAX8998_BUCK4:
0336 ret = max8998_update_reg(i2c, reg, selector<<shift,
0337 mask<<shift);
0338 break;
0339 }
0340
0341 return ret;
0342 }
0343
0344 static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev,
0345 unsigned int old_selector,
0346 unsigned int new_selector)
0347 {
0348 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0349 struct i2c_client *i2c = max8998->iodev->i2c;
0350 int buck = rdev_get_id(rdev);
0351 u8 val = 0;
0352 int difference, ret;
0353
0354 if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4)
0355 return -EINVAL;
0356
0357
0358 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
0359 if (ret)
0360 return ret;
0361
0362
0363
0364 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
0365 return 0;
0366
0367 difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000;
0368 if (difference > 0)
0369 return DIV_ROUND_UP(difference, (val & 0x0f) + 1);
0370
0371 return 0;
0372 }
0373
0374 static int max8998_set_current_limit(struct regulator_dev *rdev,
0375 int min_uA, int max_uA)
0376 {
0377 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0378 struct i2c_client *i2c = max8998->iodev->i2c;
0379 unsigned int n_currents = rdev->desc->n_current_limits;
0380 int i, sel = -1;
0381
0382 if (n_currents == 0)
0383 return -EINVAL;
0384
0385 if (rdev->desc->curr_table) {
0386 const unsigned int *curr_table = rdev->desc->curr_table;
0387 bool ascend = curr_table[n_currents - 1] > curr_table[0];
0388
0389
0390 if (ascend) {
0391 for (i = n_currents - 1; i >= 0; i--) {
0392 if (min_uA <= curr_table[i] &&
0393 curr_table[i] <= max_uA) {
0394 sel = i;
0395 break;
0396 }
0397 }
0398 } else {
0399 for (i = 0; i < n_currents; i++) {
0400 if (min_uA <= curr_table[i] &&
0401 curr_table[i] <= max_uA) {
0402 sel = i;
0403 break;
0404 }
0405 }
0406 }
0407 }
0408
0409 if (sel < 0)
0410 return -EINVAL;
0411
0412 sel <<= ffs(rdev->desc->csel_mask) - 1;
0413
0414 return max8998_update_reg(i2c, rdev->desc->csel_reg,
0415 sel, rdev->desc->csel_mask);
0416 }
0417
0418 static int max8998_get_current_limit(struct regulator_dev *rdev)
0419 {
0420 struct max8998_data *max8998 = rdev_get_drvdata(rdev);
0421 struct i2c_client *i2c = max8998->iodev->i2c;
0422 u8 val;
0423 int ret;
0424
0425 ret = max8998_read_reg(i2c, rdev->desc->csel_reg, &val);
0426 if (ret != 0)
0427 return ret;
0428
0429 val &= rdev->desc->csel_mask;
0430 val >>= ffs(rdev->desc->csel_mask) - 1;
0431
0432 if (rdev->desc->curr_table) {
0433 if (val >= rdev->desc->n_current_limits)
0434 return -EINVAL;
0435
0436 return rdev->desc->curr_table[val];
0437 }
0438
0439 return -EINVAL;
0440 }
0441
0442 static const struct regulator_ops max8998_ldo_ops = {
0443 .list_voltage = regulator_list_voltage_linear,
0444 .map_voltage = regulator_map_voltage_linear,
0445 .is_enabled = max8998_ldo_is_enabled,
0446 .enable = max8998_ldo_enable,
0447 .disable = max8998_ldo_disable,
0448 .get_voltage_sel = max8998_get_voltage_sel,
0449 .set_voltage_sel = max8998_set_voltage_ldo_sel,
0450 };
0451
0452 static const struct regulator_ops max8998_buck_ops = {
0453 .list_voltage = regulator_list_voltage_linear,
0454 .map_voltage = regulator_map_voltage_linear,
0455 .is_enabled = max8998_ldo_is_enabled,
0456 .enable = max8998_ldo_enable,
0457 .disable = max8998_ldo_disable,
0458 .get_voltage_sel = max8998_get_voltage_sel,
0459 .set_voltage_sel = max8998_set_voltage_buck_sel,
0460 .set_voltage_time_sel = max8998_set_voltage_buck_time_sel,
0461 };
0462
0463 static const struct regulator_ops max8998_charger_ops = {
0464 .set_current_limit = max8998_set_current_limit,
0465 .get_current_limit = max8998_get_current_limit,
0466 .is_enabled = max8998_ldo_is_enabled_inverted,
0467
0468 .enable = max8998_ldo_disable,
0469 .disable = max8998_ldo_enable,
0470 };
0471
0472 static const struct regulator_ops max8998_others_ops = {
0473 .is_enabled = max8998_ldo_is_enabled,
0474 .enable = max8998_ldo_enable,
0475 .disable = max8998_ldo_disable,
0476 };
0477
0478 #define MAX8998_LINEAR_REG(_name, _ops, _min, _step, _max) \
0479 { \
0480 .name = #_name, \
0481 .id = MAX8998_##_name, \
0482 .ops = _ops, \
0483 .min_uV = (_min), \
0484 .uV_step = (_step), \
0485 .n_voltages = ((_max) - (_min)) / (_step) + 1, \
0486 .type = REGULATOR_VOLTAGE, \
0487 .owner = THIS_MODULE, \
0488 }
0489
0490 #define MAX8998_CURRENT_REG(_name, _ops, _table, _reg, _mask) \
0491 { \
0492 .name = #_name, \
0493 .id = MAX8998_##_name, \
0494 .ops = _ops, \
0495 .curr_table = _table, \
0496 .n_current_limits = ARRAY_SIZE(_table), \
0497 .csel_reg = _reg, \
0498 .csel_mask = _mask, \
0499 .type = REGULATOR_CURRENT, \
0500 .owner = THIS_MODULE, \
0501 }
0502
0503 #define MAX8998_OTHERS_REG(_name, _id) \
0504 { \
0505 .name = #_name, \
0506 .id = _id, \
0507 .ops = &max8998_others_ops, \
0508 .type = REGULATOR_VOLTAGE, \
0509 .owner = THIS_MODULE, \
0510 }
0511
0512 static const struct regulator_desc regulators[] = {
0513 MAX8998_LINEAR_REG(LDO2, &max8998_ldo_ops, 800000, 50000, 1300000),
0514 MAX8998_LINEAR_REG(LDO3, &max8998_ldo_ops, 800000, 50000, 1300000),
0515 MAX8998_LINEAR_REG(LDO4, &max8998_ldo_ops, 1600000, 100000, 3600000),
0516 MAX8998_LINEAR_REG(LDO5, &max8998_ldo_ops, 1600000, 100000, 3600000),
0517 MAX8998_LINEAR_REG(LDO6, &max8998_ldo_ops, 1600000, 100000, 3600000),
0518 MAX8998_LINEAR_REG(LDO7, &max8998_ldo_ops, 1600000, 100000, 3600000),
0519 MAX8998_LINEAR_REG(LDO8, &max8998_ldo_ops, 3000000, 100000, 3600000),
0520 MAX8998_LINEAR_REG(LDO9, &max8998_ldo_ops, 2800000, 100000, 3100000),
0521 MAX8998_LINEAR_REG(LDO10, &max8998_ldo_ops, 950000, 50000, 1300000),
0522 MAX8998_LINEAR_REG(LDO11, &max8998_ldo_ops, 1600000, 100000, 3600000),
0523 MAX8998_LINEAR_REG(LDO12, &max8998_ldo_ops, 800000, 100000, 3300000),
0524 MAX8998_LINEAR_REG(LDO13, &max8998_ldo_ops, 800000, 100000, 3300000),
0525 MAX8998_LINEAR_REG(LDO14, &max8998_ldo_ops, 1200000, 100000, 3300000),
0526 MAX8998_LINEAR_REG(LDO15, &max8998_ldo_ops, 1200000, 100000, 3300000),
0527 MAX8998_LINEAR_REG(LDO16, &max8998_ldo_ops, 1600000, 100000, 3600000),
0528 MAX8998_LINEAR_REG(LDO17, &max8998_ldo_ops, 1600000, 100000, 3600000),
0529 MAX8998_LINEAR_REG(BUCK1, &max8998_buck_ops, 750000, 25000, 1525000),
0530 MAX8998_LINEAR_REG(BUCK2, &max8998_buck_ops, 750000, 25000, 1525000),
0531 MAX8998_LINEAR_REG(BUCK3, &max8998_buck_ops, 1600000, 100000, 3600000),
0532 MAX8998_LINEAR_REG(BUCK4, &max8998_buck_ops, 800000, 100000, 2300000),
0533 MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP),
0534 MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP),
0535 MAX8998_OTHERS_REG(ENVICHG, MAX8998_ENVICHG),
0536 MAX8998_OTHERS_REG(ESAFEOUT1, MAX8998_ESAFEOUT1),
0537 MAX8998_OTHERS_REG(ESAFEOUT2, MAX8998_ESAFEOUT2),
0538 MAX8998_CURRENT_REG(CHARGER, &max8998_charger_ops,
0539 charger_current_table, MAX8998_REG_CHGR1, 0x7),
0540 };
0541
0542 static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev,
0543 struct max8998_platform_data *pdata,
0544 struct device_node *pmic_np)
0545 {
0546 int gpio;
0547
0548 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0);
0549 if (!gpio_is_valid(gpio)) {
0550 dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio);
0551 return -EINVAL;
0552 }
0553 pdata->buck1_set1 = gpio;
0554
0555 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1);
0556 if (!gpio_is_valid(gpio)) {
0557 dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio);
0558 return -EINVAL;
0559 }
0560 pdata->buck1_set2 = gpio;
0561
0562 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0);
0563 if (!gpio_is_valid(gpio)) {
0564 dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio);
0565 return -EINVAL;
0566 }
0567 pdata->buck2_set3 = gpio;
0568
0569 return 0;
0570 }
0571
0572 static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev,
0573 struct max8998_platform_data *pdata)
0574 {
0575 struct device_node *pmic_np = iodev->dev->of_node;
0576 struct device_node *regulators_np, *reg_np;
0577 struct max8998_regulator_data *rdata;
0578 unsigned int i;
0579 int ret;
0580
0581 regulators_np = of_get_child_by_name(pmic_np, "regulators");
0582 if (!regulators_np) {
0583 dev_err(iodev->dev, "could not find regulators sub-node\n");
0584 return -EINVAL;
0585 }
0586
0587
0588 pdata->num_regulators = of_get_child_count(regulators_np);
0589
0590 rdata = devm_kcalloc(iodev->dev,
0591 pdata->num_regulators, sizeof(*rdata),
0592 GFP_KERNEL);
0593 if (!rdata) {
0594 of_node_put(regulators_np);
0595 return -ENOMEM;
0596 }
0597
0598 pdata->regulators = rdata;
0599 for (i = 0; i < ARRAY_SIZE(regulators); ++i) {
0600 reg_np = of_get_child_by_name(regulators_np,
0601 regulators[i].name);
0602 if (!reg_np)
0603 continue;
0604
0605 rdata->id = regulators[i].id;
0606 rdata->initdata = of_get_regulator_init_data(iodev->dev,
0607 reg_np,
0608 ®ulators[i]);
0609 rdata->reg_node = reg_np;
0610 ++rdata;
0611 }
0612 pdata->num_regulators = rdata - pdata->regulators;
0613
0614 of_node_put(reg_np);
0615 of_node_put(regulators_np);
0616
0617 ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
0618 if (ret)
0619 return -EINVAL;
0620
0621 if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL))
0622 pdata->buck_voltage_lock = true;
0623
0624 ret = of_property_read_u32(pmic_np,
0625 "max8998,pmic-buck1-default-dvs-idx",
0626 &pdata->buck1_default_idx);
0627 if (!ret && pdata->buck1_default_idx >= 4) {
0628 pdata->buck1_default_idx = 0;
0629 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
0630 }
0631
0632 ret = of_property_read_u32(pmic_np,
0633 "max8998,pmic-buck2-default-dvs-idx",
0634 &pdata->buck2_default_idx);
0635 if (!ret && pdata->buck2_default_idx >= 2) {
0636 pdata->buck2_default_idx = 0;
0637 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n");
0638 }
0639
0640 ret = of_property_read_u32_array(pmic_np,
0641 "max8998,pmic-buck1-dvs-voltage",
0642 pdata->buck1_voltage,
0643 ARRAY_SIZE(pdata->buck1_voltage));
0644 if (ret) {
0645 dev_err(iodev->dev, "buck1 voltages not specified\n");
0646 return -EINVAL;
0647 }
0648
0649 ret = of_property_read_u32_array(pmic_np,
0650 "max8998,pmic-buck2-dvs-voltage",
0651 pdata->buck2_voltage,
0652 ARRAY_SIZE(pdata->buck2_voltage));
0653 if (ret) {
0654 dev_err(iodev->dev, "buck2 voltages not specified\n");
0655 return -EINVAL;
0656 }
0657
0658 return 0;
0659 }
0660
0661 static int max8998_pmic_probe(struct platform_device *pdev)
0662 {
0663 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
0664 struct max8998_platform_data *pdata = iodev->pdata;
0665 struct regulator_config config = { };
0666 struct regulator_dev *rdev;
0667 struct max8998_data *max8998;
0668 struct i2c_client *i2c;
0669 int i, ret;
0670 unsigned int v;
0671
0672 if (!pdata) {
0673 dev_err(pdev->dev.parent, "No platform init data supplied\n");
0674 return -ENODEV;
0675 }
0676
0677 if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) {
0678 ret = max8998_pmic_dt_parse_pdata(iodev, pdata);
0679 if (ret)
0680 return ret;
0681 }
0682
0683 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data),
0684 GFP_KERNEL);
0685 if (!max8998)
0686 return -ENOMEM;
0687
0688 max8998->dev = &pdev->dev;
0689 max8998->iodev = iodev;
0690 max8998->num_regulators = pdata->num_regulators;
0691 platform_set_drvdata(pdev, max8998);
0692 i2c = max8998->iodev->i2c;
0693
0694 max8998->buck1_idx = pdata->buck1_default_idx;
0695 max8998->buck2_idx = pdata->buck2_default_idx;
0696
0697
0698
0699
0700
0701
0702 if (gpio_is_valid(pdata->buck1_set1) &&
0703 gpio_is_valid(pdata->buck1_set2)) {
0704
0705 if (!pdata->buck1_set1) {
0706 dev_err(&pdev->dev,
0707 "MAX8998 SET1 GPIO defined as 0 !\n");
0708 WARN_ON(!pdata->buck1_set1);
0709 return -EIO;
0710 }
0711
0712 if (!pdata->buck1_set2) {
0713 dev_err(&pdev->dev,
0714 "MAX8998 SET2 GPIO defined as 0 !\n");
0715 WARN_ON(!pdata->buck1_set2);
0716 return -EIO;
0717 }
0718
0719 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
0720 gpio_direction_output(pdata->buck1_set1,
0721 max8998->buck1_idx & 0x1);
0722
0723
0724 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
0725 gpio_direction_output(pdata->buck1_set2,
0726 (max8998->buck1_idx >> 1) & 0x1);
0727
0728
0729 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) {
0730 int index = MAX8998_BUCK1 - MAX8998_LDO2;
0731
0732 i = 0;
0733 while (regulators[index].min_uV +
0734 regulators[index].uV_step * i
0735 < pdata->buck1_voltage[v])
0736 i++;
0737
0738 max8998->buck1_vol[v] = i;
0739 ret = max8998_write_reg(i2c,
0740 MAX8998_REG_BUCK1_VOLTAGE1 + v, i);
0741 if (ret)
0742 return ret;
0743 }
0744 }
0745
0746 if (gpio_is_valid(pdata->buck2_set3)) {
0747
0748 if (!pdata->buck2_set3) {
0749 dev_err(&pdev->dev,
0750 "MAX8998 SET3 GPIO defined as 0 !\n");
0751 WARN_ON(!pdata->buck2_set3);
0752 return -EIO;
0753 }
0754 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
0755 gpio_direction_output(pdata->buck2_set3,
0756 max8998->buck2_idx & 0x1);
0757
0758
0759 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) {
0760 int index = MAX8998_BUCK2 - MAX8998_LDO2;
0761
0762 i = 0;
0763 while (regulators[index].min_uV +
0764 regulators[index].uV_step * i
0765 < pdata->buck2_voltage[v])
0766 i++;
0767
0768 max8998->buck2_vol[v] = i;
0769 ret = max8998_write_reg(i2c,
0770 MAX8998_REG_BUCK2_VOLTAGE1 + v, i);
0771 if (ret)
0772 return ret;
0773 }
0774 }
0775
0776 for (i = 0; i < pdata->num_regulators; i++) {
0777 int index = pdata->regulators[i].id - MAX8998_LDO2;
0778
0779 config.dev = max8998->dev;
0780 config.of_node = pdata->regulators[i].reg_node;
0781 config.init_data = pdata->regulators[i].initdata;
0782 config.driver_data = max8998;
0783
0784 rdev = devm_regulator_register(&pdev->dev, ®ulators[index],
0785 &config);
0786 if (IS_ERR(rdev)) {
0787 ret = PTR_ERR(rdev);
0788 dev_err(max8998->dev, "regulator %s init failed (%d)\n",
0789 regulators[index].name, ret);
0790 return ret;
0791 }
0792 }
0793
0794 return 0;
0795 }
0796
0797 static const struct platform_device_id max8998_pmic_id[] = {
0798 { "max8998-pmic", TYPE_MAX8998 },
0799 { "lp3974-pmic", TYPE_LP3974 },
0800 { }
0801 };
0802 MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
0803
0804 static struct platform_driver max8998_pmic_driver = {
0805 .driver = {
0806 .name = "max8998-pmic",
0807 },
0808 .probe = max8998_pmic_probe,
0809 .id_table = max8998_pmic_id,
0810 };
0811
0812 static int __init max8998_pmic_init(void)
0813 {
0814 return platform_driver_register(&max8998_pmic_driver);
0815 }
0816 subsys_initcall(max8998_pmic_init);
0817
0818 static void __exit max8998_pmic_cleanup(void)
0819 {
0820 platform_driver_unregister(&max8998_pmic_driver);
0821 }
0822 module_exit(max8998_pmic_cleanup);
0823
0824 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
0825 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
0826 MODULE_LICENSE("GPL");