Lines Matching +full:i2c +full:- +full:gpio
1 // SPDX-License-Identifier: GPL-2.0+
3 // max8998.c - Voltage regulator driver for the Maxim 8998
5 // Copyright (C) 2009-2010 Samsung Electronics
11 #include <linux/i2c.h>
13 #include <linux/gpio.h>
23 #include <linux/mfd/max8998-private.h>
48 *shift = 3 - (ldo - MAX8998_LDO2); in max8998_get_enable_register()
52 *shift = 7 - (ldo - MAX8998_LDO6); in max8998_get_enable_register()
56 *shift = 7 - (ldo - MAX8998_LDO14); in max8998_get_enable_register()
60 *shift = 7 - (ldo - MAX8998_BUCK1); in max8998_get_enable_register()
64 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP); in max8998_get_enable_register()
68 *shift = 7 - (ldo - MAX8998_ESAFEOUT1); 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() local
92 ret = max8998_read_reg(i2c, reg, &val); in max8998_ldo_is_enabled()
107 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_ldo_enable() local
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() local
127 return max8998_update_reg(i2c, reg, 0, 1<<shift); in max8998_ldo_disable()
147 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4); 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()
183 return -EINVAL; in max8998_get_voltage_register()
196 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_get_voltage_sel() local
204 ret = max8998_read_reg(i2c, reg, &val); in max8998_get_voltage_sel()
218 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_set_voltage_ldo_sel() local
225 ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift); in max8998_set_voltage_ldo_sel()
236 static inline void buck2_gpio_set(int gpio, int v) in buck2_gpio_set() argument
238 gpio_set_value(gpio, v & 0x1); in buck2_gpio_set()
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() local
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()
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()
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()
349 struct i2c_client *i2c = max8998->iodev->i2c; in max8998_set_voltage_buck_time_sel() local
355 return -EINVAL; in max8998_set_voltage_buck_time_sel()
358 ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val); 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() local
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() local
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),
546 int gpio; in max8998_pmic_dt_parse_dvs_gpio() local
548 gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0); in max8998_pmic_dt_parse_dvs_gpio()
549 if (!gpio_is_valid(gpio)) { 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()
556 if (!gpio_is_valid(gpio)) { 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()
563 if (!gpio_is_valid(gpio)) { 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 if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL)) in max8998_pmic_dt_parse_pdata()
622 pdata->buck_voltage_lock = true; in max8998_pmic_dt_parse_pdata()
625 "max8998,pmic-buck1-default-dvs-idx", in max8998_pmic_dt_parse_pdata()
626 &pdata->buck1_default_idx); in max8998_pmic_dt_parse_pdata()
627 if (!ret && pdata->buck1_default_idx >= 4) { in max8998_pmic_dt_parse_pdata()
628 pdata->buck1_default_idx = 0; in max8998_pmic_dt_parse_pdata()
629 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n"); in max8998_pmic_dt_parse_pdata()
633 "max8998,pmic-buck2-default-dvs-idx", in max8998_pmic_dt_parse_pdata()
634 &pdata->buck2_default_idx); in max8998_pmic_dt_parse_pdata()
635 if (!ret && pdata->buck2_default_idx >= 2) { in max8998_pmic_dt_parse_pdata()
636 pdata->buck2_default_idx = 0; in max8998_pmic_dt_parse_pdata()
637 dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n"); in max8998_pmic_dt_parse_pdata()
641 "max8998,pmic-buck1-dvs-voltage", in max8998_pmic_dt_parse_pdata()
642 pdata->buck1_voltage, in max8998_pmic_dt_parse_pdata()
643 ARRAY_SIZE(pdata->buck1_voltage)); in max8998_pmic_dt_parse_pdata()
645 dev_err(iodev->dev, "buck1 voltages not specified\n"); in max8998_pmic_dt_parse_pdata()
646 return -EINVAL; in max8998_pmic_dt_parse_pdata()
650 "max8998,pmic-buck2-dvs-voltage", in max8998_pmic_dt_parse_pdata()
651 pdata->buck2_voltage, in max8998_pmic_dt_parse_pdata()
652 ARRAY_SIZE(pdata->buck2_voltage)); in max8998_pmic_dt_parse_pdata()
654 dev_err(iodev->dev, "buck2 voltages not specified\n"); in max8998_pmic_dt_parse_pdata()
655 return -EINVAL; in max8998_pmic_dt_parse_pdata()
663 struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent); in max8998_pmic_probe()
664 struct max8998_platform_data *pdata = iodev->pdata; in max8998_pmic_probe()
668 struct i2c_client *i2c; in max8998_pmic_probe() local
673 dev_err(pdev->dev.parent, "No platform init data supplied\n"); in max8998_pmic_probe()
674 return -ENODEV; in max8998_pmic_probe()
677 if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) { in max8998_pmic_probe()
683 max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data), in max8998_pmic_probe()
686 return -ENOMEM; in max8998_pmic_probe()
688 max8998->dev = &pdev->dev; in max8998_pmic_probe()
689 max8998->iodev = iodev; in max8998_pmic_probe()
690 max8998->num_regulators = pdata->num_regulators; in max8998_pmic_probe()
692 i2c = max8998->iodev->i2c; in max8998_pmic_probe()
694 max8998->buck1_idx = pdata->buck1_default_idx; in max8998_pmic_probe()
695 max8998->buck2_idx = pdata->buck2_default_idx; in max8998_pmic_probe()
698 /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */ in max8998_pmic_probe()
702 if (gpio_is_valid(pdata->buck1_set1) && in max8998_pmic_probe()
703 gpio_is_valid(pdata->buck1_set2)) { in max8998_pmic_probe()
705 if (!pdata->buck1_set1) { in max8998_pmic_probe()
706 dev_err(&pdev->dev, in max8998_pmic_probe()
707 "MAX8998 SET1 GPIO defined as 0 !\n"); in max8998_pmic_probe()
708 WARN_ON(!pdata->buck1_set1); in max8998_pmic_probe()
709 return -EIO; in max8998_pmic_probe()
712 if (!pdata->buck1_set2) { in max8998_pmic_probe()
713 dev_err(&pdev->dev, in max8998_pmic_probe()
714 "MAX8998 SET2 GPIO defined as 0 !\n"); in max8998_pmic_probe()
715 WARN_ON(!pdata->buck1_set2); in max8998_pmic_probe()
716 return -EIO; in max8998_pmic_probe()
719 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1"); in max8998_pmic_probe()
720 gpio_direction_output(pdata->buck1_set1, in max8998_pmic_probe()
721 max8998->buck1_idx & 0x1); in max8998_pmic_probe()
724 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2"); in max8998_pmic_probe()
725 gpio_direction_output(pdata->buck1_set2, in max8998_pmic_probe()
726 (max8998->buck1_idx >> 1) & 0x1); in max8998_pmic_probe()
729 for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) { in max8998_pmic_probe()
730 int index = MAX8998_BUCK1 - MAX8998_LDO2; in max8998_pmic_probe()
735 < pdata->buck1_voltage[v]) in max8998_pmic_probe()
738 max8998->buck1_vol[v] = i; in max8998_pmic_probe()
739 ret = max8998_write_reg(i2c, in max8998_pmic_probe()
746 if (gpio_is_valid(pdata->buck2_set3)) { in max8998_pmic_probe()
748 if (!pdata->buck2_set3) { in max8998_pmic_probe()
749 dev_err(&pdev->dev, in max8998_pmic_probe()
750 "MAX8998 SET3 GPIO defined as 0 !\n"); in max8998_pmic_probe()
751 WARN_ON(!pdata->buck2_set3); in max8998_pmic_probe()
752 return -EIO; in max8998_pmic_probe()
754 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3"); in max8998_pmic_probe()
755 gpio_direction_output(pdata->buck2_set3, in max8998_pmic_probe()
756 max8998->buck2_idx & 0x1); in max8998_pmic_probe()
759 for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) { in max8998_pmic_probe()
760 int index = MAX8998_BUCK2 - MAX8998_LDO2; in max8998_pmic_probe()
765 < pdata->buck2_voltage[v]) in max8998_pmic_probe()
768 max8998->buck2_vol[v] = i; in max8998_pmic_probe()
769 ret = max8998_write_reg(i2c, in max8998_pmic_probe()
776 for (i = 0; i < pdata->num_regulators; i++) { in max8998_pmic_probe()
777 int index = pdata->regulators[i].id - MAX8998_LDO2; in max8998_pmic_probe()
779 config.dev = max8998->dev; in max8998_pmic_probe()
780 config.of_node = pdata->regulators[i].reg_node; in max8998_pmic_probe()
781 config.init_data = pdata->regulators[i].initdata; in max8998_pmic_probe()
784 rdev = devm_regulator_register(&pdev->dev, ®ulators[index], in max8998_pmic_probe()
788 dev_err(max8998->dev, "regulator %s init failed (%d)\n", in max8998_pmic_probe()
798 { "max8998-pmic", TYPE_MAX8998 },
799 { "lp3974-pmic", TYPE_LP3974 },
806 .name = "max8998-pmic",