Lines Matching +full:reg +full:- +full:shift

1 // SPDX-License-Identifier: GPL-2.0+
3 // max8998.c - Voltage regulator driver for the Maxim 8998
5 // Copyright (C) 2009-2010 Samsung Electronics
23 #include <linux/mfd/max8998-private.h>
41 int *reg, int *shift) in max8998_get_enable_register() argument
47 *reg = MAX8998_REG_ONOFF1; in max8998_get_enable_register()
48 *shift = 3 - (ldo - MAX8998_LDO2); in max8998_get_enable_register()
51 *reg = MAX8998_REG_ONOFF2; in max8998_get_enable_register()
52 *shift = 7 - (ldo - MAX8998_LDO6); in max8998_get_enable_register()
55 *reg = MAX8998_REG_ONOFF3; in max8998_get_enable_register()
56 *shift = 7 - (ldo - MAX8998_LDO14); in max8998_get_enable_register()
59 *reg = MAX8998_REG_ONOFF1; in max8998_get_enable_register()
60 *shift = 7 - (ldo - MAX8998_BUCK1); in max8998_get_enable_register()
63 *reg = MAX8998_REG_ONOFF4; in max8998_get_enable_register()
64 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP); in max8998_get_enable_register()
67 *reg = MAX8998_REG_CHGR2; in max8998_get_enable_register()
68 *shift = 7 - (ldo - MAX8998_ESAFEOUT1); in max8998_get_enable_register()
71 *reg = MAX8998_REG_CHGR2; in max8998_get_enable_register()
72 *shift = 0; in max8998_get_enable_register()
75 return -EINVAL; in max8998_get_enable_register()
84 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_ldo_is_enabled()
85 int ret, reg, shift = 8; in max8998_ldo_is_enabled() local
88 ret = max8998_get_enable_register(rdev, &reg, &shift); in max8998_ldo_is_enabled()
92 ret = max8998_read_reg(i2c, reg, &val); in max8998_ldo_is_enabled()
96 return val & (1 << shift); in max8998_ldo_is_enabled()
107 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_ldo_enable()
108 int reg, shift = 8, ret; in max8998_ldo_enable() local
110 ret = max8998_get_enable_register(rdev, &reg, &shift); in max8998_ldo_enable()
114 return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift); in max8998_ldo_enable()
120 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_ldo_disable()
121 int reg, shift = 8, ret; in max8998_ldo_disable() local
123 ret = max8998_get_enable_register(rdev, &reg, &shift); in max8998_ldo_disable()
127 return max8998_update_reg(i2c, reg, 0, 1<<shift); in max8998_ldo_disable()
135 int reg, shift = 0, mask = 0xff; in max8998_get_voltage_register() local
139 reg = MAX8998_REG_LDO2_LDO3; in max8998_get_voltage_register()
142 shift = 4; in max8998_get_voltage_register()
144 shift = 0; in max8998_get_voltage_register()
147 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4); in max8998_get_voltage_register()
150 reg = MAX8998_REG_LDO8_LDO9; in max8998_get_voltage_register()
153 shift = 4; in max8998_get_voltage_register()
155 shift = 0; in max8998_get_voltage_register()
158 reg = MAX8998_REG_LDO10_LDO11; in max8998_get_voltage_register()
160 shift = 5; in max8998_get_voltage_register()
163 shift = 0; in max8998_get_voltage_register()
168 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12); in max8998_get_voltage_register()
171 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx; in max8998_get_voltage_register()
174 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx; in max8998_get_voltage_register()
177 reg = MAX8998_REG_BUCK3; in max8998_get_voltage_register()
180 reg = MAX8998_REG_BUCK4; in max8998_get_voltage_register()
183 return -EINVAL; in max8998_get_voltage_register()
186 *_reg = reg; in max8998_get_voltage_register()
187 *_shift = shift; in max8998_get_voltage_register()
196 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_get_voltage_sel()
197 int reg, shift = 0, mask, ret; in max8998_get_voltage_sel() local
200 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask); in max8998_get_voltage_sel()
204 ret = max8998_read_reg(i2c, reg, &val); in max8998_get_voltage_sel()
208 val >>= shift; in max8998_get_voltage_sel()
218 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_set_voltage_ldo_sel()
219 int reg, shift = 0, mask, ret; in max8998_set_voltage_ldo_sel() local
221 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask); in max8998_set_voltage_ldo_sel()
225 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift); in max8998_set_voltage_ldo_sel()
245 struct max8998_platform_data *pdata = max8998->iodev->pdata; in max8998_set_voltage_buck_sel()
246 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_set_voltage_buck_sel()
248 int reg, shift = 0, mask, ret, j; in max8998_set_voltage_buck_sel() local
251 ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask); in max8998_set_voltage_buck_sel()
257 dev_dbg(max8998->dev, in max8998_set_voltage_buck_sel()
260 selector, max8998->buck1_vol[0], max8998->buck1_vol[1], in max8998_set_voltage_buck_sel()
261 max8998->buck1_vol[2], max8998->buck1_vol[3]); in max8998_set_voltage_buck_sel()
263 if (gpio_is_valid(pdata->buck1_set1) && in max8998_set_voltage_buck_sel()
264 gpio_is_valid(pdata->buck1_set2)) { in max8998_set_voltage_buck_sel()
268 for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) { in max8998_set_voltage_buck_sel()
269 if (max8998->buck1_vol[j] == selector) { in max8998_set_voltage_buck_sel()
270 max8998->buck1_idx = j; in max8998_set_voltage_buck_sel()
271 buck1_gpio_set(pdata->buck1_set1, in max8998_set_voltage_buck_sel()
272 pdata->buck1_set2, j); in max8998_set_voltage_buck_sel()
277 if (pdata->buck_voltage_lock) in max8998_set_voltage_buck_sel()
278 return -EINVAL; in max8998_set_voltage_buck_sel()
281 max8998->buck1_idx = (buck1_last_val % 2) + 2; in max8998_set_voltage_buck_sel()
282 dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n", in max8998_set_voltage_buck_sel()
283 max8998->buck1_idx); in max8998_set_voltage_buck_sel()
284 max8998->buck1_vol[max8998->buck1_idx] = selector; in max8998_set_voltage_buck_sel()
285 ret = max8998_get_voltage_register(rdev, &reg, in max8998_set_voltage_buck_sel()
286 &shift, in max8998_set_voltage_buck_sel()
288 ret = max8998_write_reg(i2c, reg, selector); in max8998_set_voltage_buck_sel()
289 buck1_gpio_set(pdata->buck1_set1, in max8998_set_voltage_buck_sel()
290 pdata->buck1_set2, max8998->buck1_idx); in max8998_set_voltage_buck_sel()
293 dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n", in max8998_set_voltage_buck_sel()
294 i2c->name, gpio_get_value(pdata->buck1_set1), in max8998_set_voltage_buck_sel()
295 gpio_get_value(pdata->buck1_set2)); in max8998_set_voltage_buck_sel()
298 ret = max8998_write_reg(i2c, reg, selector); in max8998_set_voltage_buck_sel()
303 dev_dbg(max8998->dev, in max8998_set_voltage_buck_sel()
305 selector, max8998->buck2_vol[0], max8998->buck2_vol[1]); in max8998_set_voltage_buck_sel()
306 if (gpio_is_valid(pdata->buck2_set3)) { in max8998_set_voltage_buck_sel()
310 for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) { in max8998_set_voltage_buck_sel()
311 if (max8998->buck2_vol[j] == selector) { in max8998_set_voltage_buck_sel()
312 max8998->buck2_idx = j; in max8998_set_voltage_buck_sel()
313 buck2_gpio_set(pdata->buck2_set3, j); in max8998_set_voltage_buck_sel()
318 if (pdata->buck_voltage_lock) in max8998_set_voltage_buck_sel()
319 return -EINVAL; in max8998_set_voltage_buck_sel()
322 &reg, &shift, &mask); in max8998_set_voltage_buck_sel()
323 ret = max8998_write_reg(i2c, reg, selector); in max8998_set_voltage_buck_sel()
324 max8998->buck2_vol[max8998->buck2_idx] = selector; in max8998_set_voltage_buck_sel()
325 buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx); in max8998_set_voltage_buck_sel()
327 dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name, in max8998_set_voltage_buck_sel()
328 gpio_get_value(pdata->buck2_set3)); in max8998_set_voltage_buck_sel()
330 ret = max8998_write_reg(i2c, reg, selector); in max8998_set_voltage_buck_sel()
336 ret = max8998_update_reg(i2c, reg, selector<<shift, in max8998_set_voltage_buck_sel()
337 mask<<shift); in max8998_set_voltage_buck_sel()
349 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_set_voltage_buck_time_sel()
355 return -EINVAL; in max8998_set_voltage_buck_time_sel()
362 /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */ in max8998_set_voltage_buck_time_sel()
364 if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP)) in max8998_set_voltage_buck_time_sel()
367 difference = (new_selector - old_selector) * rdev->desc->uV_step / 1000; in max8998_set_voltage_buck_time_sel()
378 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_set_current_limit()
379 unsigned int n_currents = rdev->desc->n_current_limits; in max8998_set_current_limit()
380 int i, sel = -1; in max8998_set_current_limit()
383 return -EINVAL; in max8998_set_current_limit()
385 if (rdev->desc->curr_table) { in max8998_set_current_limit()
386 const unsigned int *curr_table = rdev->desc->curr_table; in max8998_set_current_limit()
387 bool ascend = curr_table[n_currents - 1] > curr_table[0]; in max8998_set_current_limit()
391 for (i = n_currents - 1; i >= 0; i--) { in max8998_set_current_limit()
410 return -EINVAL; in max8998_set_current_limit()
412 sel <<= ffs(rdev->desc->csel_mask) - 1; in max8998_set_current_limit()
414 return max8998_update_reg(i2c, rdev->desc->csel_reg, in max8998_set_current_limit()
415 sel, rdev->desc->csel_mask); in max8998_set_current_limit()
421 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_get_current_limit()
425 ret = max8998_read_reg(i2c, rdev->desc->csel_reg, &val); in max8998_get_current_limit()
429 val &= rdev->desc->csel_mask; in max8998_get_current_limit()
430 val >>= ffs(rdev->desc->csel_mask) - 1; in max8998_get_current_limit()
432 if (rdev->desc->curr_table) { in max8998_get_current_limit()
433 if (val >= rdev->desc->n_current_limits) in max8998_get_current_limit()
434 return -EINVAL; in max8998_get_current_limit()
436 return rdev->desc->curr_table[val]; in max8998_get_current_limit()
439 return -EINVAL; in max8998_get_current_limit()
485 .n_voltages = ((_max) - (_min)) / (_step) + 1, \
533 MAX8998_OTHERS_REG(EN32KHz-AP, MAX8998_EN32KHZ_AP),
534 MAX8998_OTHERS_REG(EN32KHz-CP, MAX8998_EN32KHZ_CP),
548 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0); in max8998_pmic_dt_parse_dvs_gpio()
550 dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio); in max8998_pmic_dt_parse_dvs_gpio()
551 return -EINVAL; in max8998_pmic_dt_parse_dvs_gpio()
553 pdata->buck1_set1 = gpio; in max8998_pmic_dt_parse_dvs_gpio()
555 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1); in max8998_pmic_dt_parse_dvs_gpio()
557 dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio); in max8998_pmic_dt_parse_dvs_gpio()
558 return -EINVAL; in max8998_pmic_dt_parse_dvs_gpio()
560 pdata->buck1_set2 = gpio; in max8998_pmic_dt_parse_dvs_gpio()
562 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0); in max8998_pmic_dt_parse_dvs_gpio()
564 dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio); in max8998_pmic_dt_parse_dvs_gpio()
565 return -EINVAL; in max8998_pmic_dt_parse_dvs_gpio()
567 pdata->buck2_set3 = gpio; in max8998_pmic_dt_parse_dvs_gpio()
575 struct device_node *pmic_np = iodev->dev->of_node; in max8998_pmic_dt_parse_pdata()
583 dev_err(iodev->dev, "could not find regulators sub-node\n"); in max8998_pmic_dt_parse_pdata()
584 return -EINVAL; in max8998_pmic_dt_parse_pdata()
588 pdata->num_regulators = of_get_child_count(regulators_np); in max8998_pmic_dt_parse_pdata()
590 rdata = devm_kcalloc(iodev->dev, in max8998_pmic_dt_parse_pdata()
591 pdata->num_regulators, sizeof(*rdata), in max8998_pmic_dt_parse_pdata()
595 return -ENOMEM; in max8998_pmic_dt_parse_pdata()
598 pdata->regulators = rdata; in max8998_pmic_dt_parse_pdata()
605 rdata->id = regulators[i].id; in max8998_pmic_dt_parse_pdata()
606 rdata->initdata = of_get_regulator_init_data(iodev->dev, in max8998_pmic_dt_parse_pdata()
609 rdata->reg_node = reg_np; in max8998_pmic_dt_parse_pdata()
612 pdata->num_regulators = rdata - pdata->regulators; in max8998_pmic_dt_parse_pdata()
619 return -EINVAL; in max8998_pmic_dt_parse_pdata()
621 pdata->buck_voltage_lock = of_property_read_bool(pmic_np, "max8998,pmic-buck-voltage-lock"); in max8998_pmic_dt_parse_pdata()
624 "max8998,pmic-buck1-default-dvs-idx", in max8998_pmic_dt_parse_pdata()
625 &pdata->buck1_default_idx); in max8998_pmic_dt_parse_pdata()
626 if (!ret && pdata->buck1_default_idx >= 4) { in max8998_pmic_dt_parse_pdata()
627 pdata->buck1_default_idx = 0; in max8998_pmic_dt_parse_pdata()
628 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n"); in max8998_pmic_dt_parse_pdata()
632 "max8998,pmic-buck2-default-dvs-idx", in max8998_pmic_dt_parse_pdata()
633 &pdata->buck2_default_idx); in max8998_pmic_dt_parse_pdata()
634 if (!ret && pdata->buck2_default_idx >= 2) { in max8998_pmic_dt_parse_pdata()
635 pdata->buck2_default_idx = 0; in max8998_pmic_dt_parse_pdata()
636 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n"); in max8998_pmic_dt_parse_pdata()
640 "max8998,pmic-buck1-dvs-voltage", in max8998_pmic_dt_parse_pdata()
641 pdata->buck1_voltage, in max8998_pmic_dt_parse_pdata()
642 ARRAY_SIZE(pdata->buck1_voltage)); in max8998_pmic_dt_parse_pdata()
644 dev_err(iodev->dev, "buck1 voltages not specified\n"); in max8998_pmic_dt_parse_pdata()
645 return -EINVAL; in max8998_pmic_dt_parse_pdata()
649 "max8998,pmic-buck2-dvs-voltage", in max8998_pmic_dt_parse_pdata()
650 pdata->buck2_voltage, in max8998_pmic_dt_parse_pdata()
651 ARRAY_SIZE(pdata->buck2_voltage)); in max8998_pmic_dt_parse_pdata()
653 dev_err(iodev->dev, "buck2 voltages not specified\n"); in max8998_pmic_dt_parse_pdata()
654 return -EINVAL; in max8998_pmic_dt_parse_pdata()
662 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent); in max8998_pmic_probe()
663 struct max8998_platform_data *pdata = iodev->pdata; in max8998_pmic_probe()
672 dev_err(pdev->dev.parent, "No platform init data supplied\n"); in max8998_pmic_probe()
673 return -ENODEV; in max8998_pmic_probe()
676 if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) { in max8998_pmic_probe()
682 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data), in max8998_pmic_probe()
685 return -ENOMEM; in max8998_pmic_probe()
687 max8998->dev = &pdev->dev; in max8998_pmic_probe()
688 max8998->iodev = iodev; in max8998_pmic_probe()
689 max8998->num_regulators = pdata->num_regulators; in max8998_pmic_probe()
691 i2c = max8998->iodev->i2c; in max8998_pmic_probe()
693 max8998->buck1_idx = pdata->buck1_default_idx; in max8998_pmic_probe()
694 max8998->buck2_idx = pdata->buck2_default_idx; in max8998_pmic_probe()
697 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */ in max8998_pmic_probe()
701 if (gpio_is_valid(pdata->buck1_set1) && in max8998_pmic_probe()
702 gpio_is_valid(pdata->buck1_set2)) { in max8998_pmic_probe()
704 if (!pdata->buck1_set1) { in max8998_pmic_probe()
705 dev_err(&pdev->dev, in max8998_pmic_probe()
707 WARN_ON(!pdata->buck1_set1); in max8998_pmic_probe()
708 return -EIO; in max8998_pmic_probe()
711 if (!pdata->buck1_set2) { in max8998_pmic_probe()
712 dev_err(&pdev->dev, in max8998_pmic_probe()
714 WARN_ON(!pdata->buck1_set2); in max8998_pmic_probe()
715 return -EIO; in max8998_pmic_probe()
718 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1"); in max8998_pmic_probe()
719 gpio_direction_output(pdata->buck1_set1, in max8998_pmic_probe()
720 max8998->buck1_idx & 0x1); in max8998_pmic_probe()
723 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2"); in max8998_pmic_probe()
724 gpio_direction_output(pdata->buck1_set2, in max8998_pmic_probe()
725 (max8998->buck1_idx >> 1) & 0x1); in max8998_pmic_probe()
728 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) { in max8998_pmic_probe()
729 int index = MAX8998_BUCK1 - MAX8998_LDO2; in max8998_pmic_probe()
734 < pdata->buck1_voltage[v]) in max8998_pmic_probe()
737 max8998->buck1_vol[v] = i; in max8998_pmic_probe()
745 if (gpio_is_valid(pdata->buck2_set3)) { in max8998_pmic_probe()
747 if (!pdata->buck2_set3) { in max8998_pmic_probe()
748 dev_err(&pdev->dev, in max8998_pmic_probe()
750 WARN_ON(!pdata->buck2_set3); in max8998_pmic_probe()
751 return -EIO; in max8998_pmic_probe()
753 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3"); in max8998_pmic_probe()
754 gpio_direction_output(pdata->buck2_set3, in max8998_pmic_probe()
755 max8998->buck2_idx & 0x1); in max8998_pmic_probe()
758 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) { in max8998_pmic_probe()
759 int index = MAX8998_BUCK2 - MAX8998_LDO2; in max8998_pmic_probe()
764 < pdata->buck2_voltage[v]) in max8998_pmic_probe()
767 max8998->buck2_vol[v] = i; in max8998_pmic_probe()
775 for (i = 0; i < pdata->num_regulators; i++) { in max8998_pmic_probe()
776 int index = pdata->regulators[i].id - MAX8998_LDO2; in max8998_pmic_probe()
778 config.dev = max8998->dev; in max8998_pmic_probe()
779 config.of_node = pdata->regulators[i].reg_node; in max8998_pmic_probe()
780 config.init_data = pdata->regulators[i].initdata; in max8998_pmic_probe()
783 rdev = devm_regulator_register(&pdev->dev, &regulators[index], in max8998_pmic_probe()
787 dev_err(max8998->dev, "regulator %s init failed (%d)\n", in max8998_pmic_probe()
797 { "max8998-pmic", TYPE_MAX8998 },
798 { "lp3974-pmic", TYPE_LP3974 },
805 .name = "max8998-pmic",