Lines Matching +full:da9121 +full:- +full:regulator

1 // SPDX-License-Identifier: GPL-2.0-only
3 // DA9121 Single-channel dual-phase 10A buck converter
7 // DA9130 Single-channel dual-phase 10A buck converter (Automotive)
8 // DA9217 Single-channel dual-phase 6A buck converter
9 // DA9122 Dual-channel single-phase 5A buck converter
10 // DA9131 Dual-channel single-phase 5A buck converter (Automotive)
11 // DA9220 Dual-channel single-phase 3A buck converter
12 // DA9132 Dual-channel single-phase 3A buck converter (Automotive)
19 #include <linux/regulator/of_regulator.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/regulator/driver.h>
26 #include <linux/regulator/da9121.h>
30 #include "da9121-regulator.h"
33 struct da9121 { struct
136 int buck_id; /* 0=core, 1/2-buck */
142 char *warn; /* if NULL, notify - otherwise dev_warn this string */
165 * re-occur. When this driver starts, it is expected that these will have
166 * self-cleared for when the IRQs are enabled, so these should never be seen.
190 struct da9121 *chip = rdev_get_drvdata(rdev); in da9121_get_current_limit()
193 variant_parameters[chip->variant_id].current_range; in da9121_get_current_limit()
197 ret = regmap_read(chip->regmap, da9121_current_field[id].reg, &val); in da9121_get_current_limit()
199 dev_err(chip->dev, "Cannot read BUCK register: %d\n", ret); in da9121_get_current_limit()
203 if (val < range->reg_min) { in da9121_get_current_limit()
204 ret = -EACCES; in da9121_get_current_limit()
208 if (val > range->reg_max) { in da9121_get_current_limit()
209 ret = -EINVAL; in da9121_get_current_limit()
213 return range->val_min + (range->val_stp * (val - range->reg_min)); in da9121_get_current_limit()
222 struct da9121 *chip = rdev_get_drvdata(rdev); in da9121_ceiling_selector()
224 variant_parameters[chip->variant_id].current_range; in da9121_ceiling_selector()
230 if (range->val_min > max || range->val_max < min) { in da9121_ceiling_selector()
231 dev_err(chip->dev, in da9121_ceiling_selector()
232 "Requested current out of regulator capability\n"); in da9121_ceiling_selector()
233 ret = -EINVAL; in da9121_ceiling_selector()
237 level = range->val_max; in da9121_ceiling_selector()
238 for (i = range->reg_max; i >= range->reg_min; i--) { in da9121_ceiling_selector()
243 level -= range->val_stp; in da9121_ceiling_selector()
247 dev_err(chip->dev, in da9121_ceiling_selector()
249 ret = -EINVAL; in da9121_ceiling_selector()
261 struct da9121 *chip = rdev_get_drvdata(rdev); in da9121_set_current_limit()
264 variant_parameters[chip->variant_id].current_range; in da9121_set_current_limit()
268 if (min_ua < range->val_min || in da9121_set_current_limit()
269 max_ua > range->val_max) { in da9121_set_current_limit()
270 ret = -EINVAL; in da9121_set_current_limit()
274 if (rdev->desc->ops->is_enabled(rdev)) { in da9121_set_current_limit()
275 ret = -EBUSY; in da9121_set_current_limit()
283 ret = regmap_update_bits(chip->regmap, in da9121_set_current_limit()
288 dev_err(chip->dev, "Cannot update BUCK current limit, err: %d\n", ret); in da9121_set_current_limit()
312 struct da9121 *chip = rdev_get_drvdata(rdev); in da9121_buck_set_mode()
330 return -EINVAL; in da9121_buck_set_mode()
333 return regmap_update_bits(chip->regmap, in da9121_buck_set_mode()
341 struct da9121 *chip = rdev_get_drvdata(rdev); in da9121_buck_get_mode()
346 ret = regmap_read(chip->regmap, da9121_mode_field[id].reg, &val); in da9121_buck_get_mode()
348 dev_err(chip->dev, "Cannot read BUCK register: %d\n", ret); in da9121_buck_get_mode()
349 return -EINVAL; in da9121_buck_get_mode()
354 return -EINVAL; in da9121_buck_get_mode()
381 struct da9121 *chip = config->driver_data; in da9121_of_parse_cb()
385 if (chip->pdata == NULL) { in da9121_of_parse_cb()
386 pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL); in da9121_of_parse_cb()
388 return -ENOMEM; in da9121_of_parse_cb()
390 pdata = chip->pdata; in da9121_of_parse_cb()
393 pdata->num_buck++; in da9121_of_parse_cb()
395 if (pdata->num_buck > variant_parameters[chip->variant_id].num_bucks) { in da9121_of_parse_cb()
396 dev_err(chip->dev, "Error: excessive regulators for device\n"); in da9121_of_parse_cb()
397 return -ENODEV; in da9121_of_parse_cb()
403 "da9121-enable"); in da9121_of_parse_cb()
405 config->ena_gpiod = ena_gpiod; in da9121_of_parse_cb()
407 if (variant_parameters[chip->variant_id].num_bucks == 2) { in da9121_of_parse_cb()
412 if (of_property_read_u32(da9121_matches[pdata->num_buck-1].of_node, in da9121_of_parse_cb()
413 "dlg,ripple-cancel", &ripple_cancel)) { in da9121_of_parse_cb()
414 if (pdata->num_buck > 1) in da9121_of_parse_cb()
419 ret = regmap_update_bits(chip->regmap, ripple_reg, in da9121_of_parse_cb()
423 dev_err(chip->dev, "Cannot set ripple mode, err: %d\n", ret); in da9121_of_parse_cb()
434 #define DA9121_N_VOLTAGES (((DA9121_MAX_MV - DA9121_MIN_MV) / DA9121_STEP_MV) \
439 .name = "da9121",
567 #define DA914X_N_VOLTAGES (((DA914X_MAX_MV - DA914X_MIN_MV) / DA914X_STEP_MV) \
622 struct da9121 *chip = container_of(work, struct da9121, work.work); in da9121_status_poll_on()
629 ret = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_STATUS_0, status, 2); in da9121_status_poll_on()
631 dev_err(chip->dev, in da9121_status_poll_on()
638 * and the respective state cleared - then flagged to unmask for next IRQ. in da9121_status_poll_on()
642 int reg_idx = item->reg_index; in da9121_status_poll_on()
643 bool relevant = (item->buck_id <= variant_parameters[chip->variant_id].num_bucks); in da9121_status_poll_on()
644 bool supported = (item->warn == NULL); in da9121_status_poll_on()
645 bool persisting = (chip->persistent[reg_idx] & item->event_bit); in da9121_status_poll_on()
646 bool now_cleared = !(status[reg_idx] & item->status_bit); in da9121_status_poll_on()
649 clear[reg_idx] |= item->mask_bit; in da9121_status_poll_on()
650 chip->persistent[reg_idx] &= ~item->event_bit; in da9121_status_poll_on()
659 ret = regmap_update_bits(chip->regmap, reg, mbit, 0); in da9121_status_poll_on()
661 dev_err(chip->dev, in da9121_status_poll_on()
669 if (chip->persistent[0] | chip->persistent[1]) { in da9121_status_poll_on()
670 delay = msecs_to_jiffies(chip->passive_delay); in da9121_status_poll_on()
671 queue_delayed_work(system_freezable_wq, &chip->work, delay); in da9121_status_poll_on()
680 struct da9121 *chip = data; in da9121_irq_handler()
689 err = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_EVENT_0, event, 3); in da9121_irq_handler()
691 dev_err(chip->dev, "Failed to read EVENT registers %d\n", err); in da9121_irq_handler()
696 err = regmap_bulk_read(chip->regmap, DA9121_REG_SYS_MASK_0, mask, 3); in da9121_irq_handler()
698 dev_err(chip->dev, in da9121_irq_handler()
704 rdev = chip->rdev[DA9121_IDX_BUCK1]; in da9121_irq_handler()
708 * or a warning given for unexpected events - those from device POR, and in da9121_irq_handler()
713 int reg_idx = item->reg_index; in da9121_irq_handler()
714 bool relevant = (item->buck_id <= variant_parameters[chip->variant_id].num_bucks); in da9121_irq_handler()
715 bool enabled = !(mask[reg_idx] & item->mask_bit); in da9121_irq_handler()
716 bool active = (event[reg_idx] & item->event_bit); in da9121_irq_handler()
717 bool notify = (item->warn == NULL); in da9121_irq_handler()
721 chip->persistent[reg_idx] |= item->event_bit; in da9121_irq_handler()
722 regulator_notifier_call_chain(rdev, item->notification, NULL); in da9121_irq_handler()
724 dev_warn(chip->dev, item->warn); in da9121_irq_handler()
725 handled[reg_idx] |= item->event_bit; in da9121_irq_handler()
733 dev_warn(chip->dev, in da9121_irq_handler()
745 err = regmap_update_bits(chip->regmap, reg, mbit, mbit); in da9121_irq_handler()
747 dev_err(chip->dev, in da9121_irq_handler()
758 err = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_EVENT_0, handled, 3); in da9121_irq_handler()
760 dev_err(chip->dev, "Fail to write EVENTs %d\n", err); in da9121_irq_handler()
766 queue_delayed_work(system_freezable_wq, &chip->work, 0); in da9121_irq_handler()
771 static int da9121_set_regulator_config(struct da9121 *chip) in da9121_set_regulator_config()
774 unsigned int max_matches = variant_parameters[chip->variant_id].num_bucks; in da9121_set_regulator_config()
780 local_da9121_regulators[chip->variant_id][i]; in da9121_set_regulator_config()
782 config.dev = chip->dev; in da9121_set_regulator_config()
784 config.regmap = chip->regmap; in da9121_set_regulator_config()
786 chip->rdev[i] = devm_regulator_register(chip->dev, in da9121_set_regulator_config()
788 if (IS_ERR(chip->rdev[i])) { in da9121_set_regulator_config()
789 dev_err(chip->dev, "Failed to register regulator %s, %d/%d\n", in da9121_set_regulator_config()
790 regl_desc->name, (i+1), max_matches); in da9121_set_regulator_config()
791 ret = PTR_ERR(chip->rdev[i]); in da9121_set_regulator_config()
800 /* DA9121 chip register model */
868 /* DA9121 regmap config for 1 channel variants */
879 /* DA9121 regmap config for 2 channel variants */
890 static int da9121_check_device_type(struct i2c_client *i2c, struct da9121 *chip) in da9121_check_device_type()
899 ret = regmap_read(chip->regmap, DA9121_REG_OTP_DEVICE_ID, &device_id); in da9121_check_device_type()
901 dev_err(chip->dev, "Cannot read device ID: %d\n", ret); in da9121_check_device_type()
905 ret = regmap_read(chip->regmap, DA9121_REG_OTP_VARIANT_ID, &variant_id); in da9121_check_device_type()
907 dev_err(chip->dev, "Cannot read variant ID: %d\n", ret); in da9121_check_device_type()
912 dev_err(chip->dev, "Invalid device ID: 0x%02x\n", device_id); in da9121_check_device_type()
913 ret = -ENODEV; in da9121_check_device_type()
919 switch (chip->subvariant_id) { in da9121_check_device_type()
921 type = "DA9121"; in da9121_check_device_type()
954 switch (chip->subvariant_id) { in da9121_check_device_type()
969 dev_info(chip->dev, in da9121_check_device_type()
970 "Device detected (device-ID: 0x%02X, var-ID: 0x%02X, %s)\n", in da9121_check_device_type()
974 dev_err(chip->dev, "Device tree configuration does not match detected device.\n"); in da9121_check_device_type()
975 ret = -EINVAL; in da9121_check_device_type()
986 dev_err(chip->dev, in da9121_check_device_type()
988 ret = -EINVAL; in da9121_check_device_type()
995 struct da9121 *chip) in da9121_assign_chip_model()
1000 chip->dev = &i2c->dev; in da9121_assign_chip_model()
1002 /* Use configured subtype to select the regulator descriptor index and in da9121_assign_chip_model()
1005 switch (chip->subvariant_id) { in da9121_assign_chip_model()
1008 chip->variant_id = DA9121_TYPE_DA9121_DA9130; in da9121_assign_chip_model()
1012 chip->variant_id = DA9121_TYPE_DA9217; in da9121_assign_chip_model()
1017 chip->variant_id = DA9121_TYPE_DA9122_DA9131; in da9121_assign_chip_model()
1022 chip->variant_id = DA9121_TYPE_DA9220_DA9132; in da9121_assign_chip_model()
1026 chip->variant_id = DA9121_TYPE_DA9141; in da9121_assign_chip_model()
1030 chip->variant_id = DA9121_TYPE_DA9142; in da9121_assign_chip_model()
1034 return -EINVAL; in da9121_assign_chip_model()
1038 da9121_matches[0].desc = local_da9121_regulators[chip->variant_id][0]; in da9121_assign_chip_model()
1039 da9121_matches[1].desc = local_da9121_regulators[chip->variant_id][1]; in da9121_assign_chip_model()
1041 chip->regmap = devm_regmap_init_i2c(i2c, regmap); in da9121_assign_chip_model()
1042 if (IS_ERR(chip->regmap)) { in da9121_assign_chip_model()
1043 ret = PTR_ERR(chip->regmap); in da9121_assign_chip_model()
1044 dev_err(chip->dev, "Failed to configure a register map: %d\n", in da9121_assign_chip_model()
1055 struct da9121 *chip) in da9121_config_irq()
1061 chip->chip_irq = i2c->irq; in da9121_config_irq()
1063 if (chip->chip_irq != 0) { in da9121_config_irq()
1064 if (!of_property_read_u32(chip->dev->of_node, in da9121_config_irq()
1065 "dlg,irq-polling-delay-passive-ms", in da9121_config_irq()
1069 dev_warn(chip->dev, in da9121_config_irq()
1070 "Out-of-range polling period %d ms\n", in da9121_config_irq()
1076 chip->passive_delay = p_delay; in da9121_config_irq()
1078 ret = request_threaded_irq(chip->chip_irq, NULL, in da9121_config_irq()
1081 "da9121", chip); in da9121_config_irq()
1083 dev_err(chip->dev, "Failed IRQ request: %d\n", in da9121_config_irq()
1084 chip->chip_irq); in da9121_config_irq()
1088 ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4); in da9121_config_irq()
1090 dev_err(chip->dev, "Failed to set IRQ masks: %d\n", in da9121_config_irq()
1095 INIT_DELAYED_WORK(&chip->work, da9121_status_poll_on); in da9121_config_irq()
1096 dev_info(chip->dev, "Interrupt polling period set at %d ms\n", in da9121_config_irq()
1097 chip->passive_delay); in da9121_config_irq()
1102 free_irq(chip->chip_irq, chip); in da9121_config_irq()
1107 { .compatible = "dlg,da9121", .data = (void *) DA9121_SUBTYPE_DA9121 },
1126 return -EINVAL; in da9121_of_get_id()
1128 return (uintptr_t)id->data; in da9121_of_get_id()
1133 struct da9121 *chip; in da9121_i2c_probe()
1137 chip = devm_kzalloc(&i2c->dev, sizeof(struct da9121), GFP_KERNEL); in da9121_i2c_probe()
1139 ret = -ENOMEM; in da9121_i2c_probe()
1143 chip->pdata = i2c->dev.platform_data; in da9121_i2c_probe()
1144 chip->subvariant_id = da9121_of_get_id(&i2c->dev); in da9121_i2c_probe()
1150 ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4); in da9121_i2c_probe()
1152 dev_err(chip->dev, "Failed to set IRQ masks: %d\n", ret); in da9121_i2c_probe()
1168 struct da9121 *chip = i2c_get_clientdata(i2c); in da9121_i2c_remove()
1172 free_irq(chip->chip_irq, chip); in da9121_i2c_remove()
1173 cancel_delayed_work_sync(&chip->work); in da9121_i2c_remove()
1175 ret = regmap_bulk_write(chip->regmap, DA9121_REG_SYS_MASK_0, mask_all, 4); in da9121_i2c_remove()
1177 dev_err(chip->dev, "Failed to set IRQ masks: %d\n", ret); in da9121_i2c_remove()
1181 {"da9121", DA9121_TYPE_DA9121_DA9130},
1196 .name = "da9121",