12796a01cSAlec Li // SPDX-License-Identifier: GPL-2.0
22796a01cSAlec Li //
32796a01cSAlec Li // AWINIC AW37503 Regulator Driver
42796a01cSAlec Li //
52796a01cSAlec Li // Copyright (C) 2023 awinic. All Rights Reserved
62796a01cSAlec Li //
72796a01cSAlec Li // Author: <like@awinic.com>
82796a01cSAlec Li
92796a01cSAlec Li #include <linux/err.h>
102796a01cSAlec Li #include <linux/gpio/consumer.h>
112796a01cSAlec Li #include <linux/i2c.h>
122796a01cSAlec Li #include <linux/module.h>
132796a01cSAlec Li #include <linux/regmap.h>
142796a01cSAlec Li #include <linux/regulator/driver.h>
152796a01cSAlec Li #include <linux/regulator/machine.h>
162796a01cSAlec Li
172796a01cSAlec Li #define AW37503_REG_VPOS 0x00
182796a01cSAlec Li #define AW37503_REG_VNEG 0x01
192796a01cSAlec Li #define AW37503_REG_APPS 0x03
202796a01cSAlec Li #define AW37503_REG_CONTROL 0x04
212796a01cSAlec Li #define AW37503_REG_WPRTEN 0x21
222796a01cSAlec Li
232796a01cSAlec Li #define AW37503_VOUT_MASK 0x1F
242796a01cSAlec Li #define AW37503_VOUT_N_VOLTAGE 0x15
252796a01cSAlec Li #define AW37503_VOUT_VMIN 4000000
262796a01cSAlec Li #define AW37503_VOUT_VMAX 6000000
272796a01cSAlec Li #define AW37503_VOUT_STEP 100000
282796a01cSAlec Li
292796a01cSAlec Li #define AW37503_REG_APPS_DIS_VPOS BIT(1)
302796a01cSAlec Li #define AW37503_REG_APPS_DIS_VNEG BIT(0)
312796a01cSAlec Li
322796a01cSAlec Li #define AW37503_REGULATOR_ID_VPOS 0
332796a01cSAlec Li #define AW37503_REGULATOR_ID_VNEG 1
342796a01cSAlec Li #define AW37503_MAX_REGULATORS 2
352796a01cSAlec Li
362796a01cSAlec Li struct aw37503_reg_pdata {
372796a01cSAlec Li struct gpio_desc *en_gpiod;
382796a01cSAlec Li int ena_gpio_state;
392796a01cSAlec Li };
402796a01cSAlec Li
412796a01cSAlec Li struct aw37503_regulator {
422796a01cSAlec Li struct device *dev;
432796a01cSAlec Li struct aw37503_reg_pdata reg_pdata[AW37503_MAX_REGULATORS];
442796a01cSAlec Li };
452796a01cSAlec Li
aw37503_regulator_enable(struct regulator_dev * rdev)462796a01cSAlec Li static int aw37503_regulator_enable(struct regulator_dev *rdev)
472796a01cSAlec Li {
482796a01cSAlec Li struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
492796a01cSAlec Li int id = rdev_get_id(rdev);
502796a01cSAlec Li struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
512796a01cSAlec Li int ret;
522796a01cSAlec Li
532796a01cSAlec Li if (!IS_ERR(rpdata->en_gpiod)) {
542796a01cSAlec Li gpiod_set_value_cansleep(rpdata->en_gpiod, 1);
552796a01cSAlec Li rpdata->ena_gpio_state = 1;
562796a01cSAlec Li }
572796a01cSAlec Li
582796a01cSAlec Li /* Hardware automatically enable discharge bit in enable */
592796a01cSAlec Li if (rdev->constraints->active_discharge ==
602796a01cSAlec Li REGULATOR_ACTIVE_DISCHARGE_DISABLE) {
612796a01cSAlec Li ret = regulator_set_active_discharge_regmap(rdev, false);
622796a01cSAlec Li if (ret < 0) {
632796a01cSAlec Li dev_err(chip->dev, "Failed to disable active discharge: %d\n",
642796a01cSAlec Li ret);
652796a01cSAlec Li return ret;
662796a01cSAlec Li }
672796a01cSAlec Li }
682796a01cSAlec Li
692796a01cSAlec Li return 0;
702796a01cSAlec Li }
712796a01cSAlec Li
aw37503_regulator_disable(struct regulator_dev * rdev)722796a01cSAlec Li static int aw37503_regulator_disable(struct regulator_dev *rdev)
732796a01cSAlec Li {
742796a01cSAlec Li struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
752796a01cSAlec Li int id = rdev_get_id(rdev);
762796a01cSAlec Li struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
772796a01cSAlec Li
782796a01cSAlec Li if (!IS_ERR(rpdata->en_gpiod)) {
792796a01cSAlec Li gpiod_set_value_cansleep(rpdata->en_gpiod, 0);
802796a01cSAlec Li rpdata->ena_gpio_state = 0;
812796a01cSAlec Li }
822796a01cSAlec Li
832796a01cSAlec Li return 0;
842796a01cSAlec Li }
852796a01cSAlec Li
aw37503_regulator_is_enabled(struct regulator_dev * rdev)862796a01cSAlec Li static int aw37503_regulator_is_enabled(struct regulator_dev *rdev)
872796a01cSAlec Li {
882796a01cSAlec Li struct aw37503_regulator *chip = rdev_get_drvdata(rdev);
892796a01cSAlec Li int id = rdev_get_id(rdev);
902796a01cSAlec Li struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[id];
912796a01cSAlec Li
922796a01cSAlec Li if (!IS_ERR(rpdata->en_gpiod))
932796a01cSAlec Li return rpdata->ena_gpio_state;
942796a01cSAlec Li
952796a01cSAlec Li return 1;
962796a01cSAlec Li }
972796a01cSAlec Li
982796a01cSAlec Li static const struct regulator_ops aw37503_regulator_ops = {
992796a01cSAlec Li .enable = aw37503_regulator_enable,
1002796a01cSAlec Li .disable = aw37503_regulator_disable,
1012796a01cSAlec Li .is_enabled = aw37503_regulator_is_enabled,
1022796a01cSAlec Li .list_voltage = regulator_list_voltage_linear,
1032796a01cSAlec Li .map_voltage = regulator_map_voltage_linear,
1042796a01cSAlec Li .get_voltage_sel = regulator_get_voltage_sel_regmap,
1052796a01cSAlec Li .set_voltage_sel = regulator_set_voltage_sel_regmap,
1062796a01cSAlec Li .set_active_discharge = regulator_set_active_discharge_regmap,
1072796a01cSAlec Li };
1082796a01cSAlec Li
aw37503_of_parse_cb(struct device_node * np,const struct regulator_desc * desc,struct regulator_config * config)1092796a01cSAlec Li static int aw37503_of_parse_cb(struct device_node *np,
1102796a01cSAlec Li const struct regulator_desc *desc,
1112796a01cSAlec Li struct regulator_config *config)
1122796a01cSAlec Li {
1132796a01cSAlec Li struct aw37503_regulator *chip = config->driver_data;
1142796a01cSAlec Li struct aw37503_reg_pdata *rpdata = &chip->reg_pdata[desc->id];
1152796a01cSAlec Li int ret;
1162796a01cSAlec Li
1172796a01cSAlec Li rpdata->en_gpiod = devm_fwnode_gpiod_get(chip->dev, of_fwnode_handle(np),
1182796a01cSAlec Li "enable", GPIOD_OUT_LOW,
1192796a01cSAlec Li "enable");
1202796a01cSAlec Li
1212796a01cSAlec Li if (IS_ERR(rpdata->en_gpiod)) {
1222796a01cSAlec Li ret = PTR_ERR(rpdata->en_gpiod);
1232796a01cSAlec Li
1242796a01cSAlec Li /* Ignore the error other than probe defer */
1252796a01cSAlec Li if (ret == -EPROBE_DEFER)
1262796a01cSAlec Li return ret;
1272796a01cSAlec Li return 0;
1282796a01cSAlec Li }
1292796a01cSAlec Li
1302796a01cSAlec Li return 0;
1312796a01cSAlec Li }
1322796a01cSAlec Li
1332796a01cSAlec Li #define AW37503_REGULATOR_DESC(_id, _name) \
1342796a01cSAlec Li [AW37503_REGULATOR_ID_##_id] = { \
1352796a01cSAlec Li .name = "aw37503-"#_name, \
1362796a01cSAlec Li .supply_name = "vin", \
1372796a01cSAlec Li .id = AW37503_REGULATOR_ID_##_id, \
1382796a01cSAlec Li .of_match = of_match_ptr(#_name), \
1392796a01cSAlec Li .of_parse_cb = aw37503_of_parse_cb, \
1402796a01cSAlec Li .ops = &aw37503_regulator_ops, \
1412796a01cSAlec Li .n_voltages = AW37503_VOUT_N_VOLTAGE, \
1422796a01cSAlec Li .min_uV = AW37503_VOUT_VMIN, \
1432796a01cSAlec Li .uV_step = AW37503_VOUT_STEP, \
1442796a01cSAlec Li .enable_time = 500, \
1452796a01cSAlec Li .vsel_mask = AW37503_VOUT_MASK, \
1462796a01cSAlec Li .vsel_reg = AW37503_REG_##_id, \
1472796a01cSAlec Li .active_discharge_off = 0, \
1482796a01cSAlec Li .active_discharge_on = AW37503_REG_APPS_DIS_##_id, \
1492796a01cSAlec Li .active_discharge_mask = AW37503_REG_APPS_DIS_##_id, \
1502796a01cSAlec Li .active_discharge_reg = AW37503_REG_APPS, \
1512796a01cSAlec Li .type = REGULATOR_VOLTAGE, \
1522796a01cSAlec Li .owner = THIS_MODULE, \
1532796a01cSAlec Li }
1542796a01cSAlec Li
1552796a01cSAlec Li static const struct regulator_desc aw_regs_desc[AW37503_MAX_REGULATORS] = {
1562796a01cSAlec Li AW37503_REGULATOR_DESC(VPOS, outp),
1572796a01cSAlec Li AW37503_REGULATOR_DESC(VNEG, outn),
1582796a01cSAlec Li };
1592796a01cSAlec Li
1602796a01cSAlec Li static const struct regmap_range aw37503_no_reg_ranges[] = {
1612796a01cSAlec Li regmap_reg_range(AW37503_REG_CONTROL + 1,
1622796a01cSAlec Li AW37503_REG_WPRTEN - 1),
1632796a01cSAlec Li };
1642796a01cSAlec Li
1652796a01cSAlec Li static const struct regmap_access_table aw37503_no_reg_table = {
1662796a01cSAlec Li .no_ranges = aw37503_no_reg_ranges,
1672796a01cSAlec Li .n_no_ranges = ARRAY_SIZE(aw37503_no_reg_ranges),
1682796a01cSAlec Li };
1692796a01cSAlec Li
1702796a01cSAlec Li static const struct regmap_config aw37503_regmap_config = {
1712796a01cSAlec Li .reg_bits = 8,
1722796a01cSAlec Li .val_bits = 8,
1732796a01cSAlec Li .max_register = AW37503_REG_WPRTEN,
1742796a01cSAlec Li .rd_table = &aw37503_no_reg_table,
1752796a01cSAlec Li .wr_table = &aw37503_no_reg_table,
1762796a01cSAlec Li };
1772796a01cSAlec Li
aw37503_probe(struct i2c_client * client)1782796a01cSAlec Li static int aw37503_probe(struct i2c_client *client)
1792796a01cSAlec Li {
1802796a01cSAlec Li struct device *dev = &client->dev;
1812796a01cSAlec Li struct aw37503_regulator *chip;
1822796a01cSAlec Li struct regulator_dev *rdev;
1832796a01cSAlec Li struct regmap *regmap;
1842796a01cSAlec Li struct regulator_config config = { };
1852796a01cSAlec Li int id;
1862796a01cSAlec Li
1872796a01cSAlec Li chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
1882796a01cSAlec Li if (!chip)
1892796a01cSAlec Li return -ENOMEM;
1902796a01cSAlec Li
1912796a01cSAlec Li regmap = devm_regmap_init_i2c(client, &aw37503_regmap_config);
1922796a01cSAlec Li if (IS_ERR(regmap))
1932796a01cSAlec Li return dev_err_probe(dev, PTR_ERR(regmap),
1942796a01cSAlec Li "Failed to init regmap\n");
1952796a01cSAlec Li
1962796a01cSAlec Li i2c_set_clientdata(client, chip);
1972796a01cSAlec Li chip->dev = dev;
1982796a01cSAlec Li
1992796a01cSAlec Li config.regmap = regmap;
2002796a01cSAlec Li config.dev = dev;
2012796a01cSAlec Li config.driver_data = chip;
2022796a01cSAlec Li
2032796a01cSAlec Li for (id = 0; id < AW37503_MAX_REGULATORS; ++id) {
2042796a01cSAlec Li rdev = devm_regulator_register(dev, &aw_regs_desc[id],
2052796a01cSAlec Li &config);
2062796a01cSAlec Li if (IS_ERR(rdev))
2072796a01cSAlec Li return dev_err_probe(dev, PTR_ERR(rdev),
2082796a01cSAlec Li "Failed to register regulator %s\n",
2092796a01cSAlec Li aw_regs_desc[id].name);
2102796a01cSAlec Li }
2112796a01cSAlec Li return 0;
2122796a01cSAlec Li }
2132796a01cSAlec Li
2142796a01cSAlec Li static const struct i2c_device_id aw37503_id[] = {
2152796a01cSAlec Li {.name = "aw37503",},
2162796a01cSAlec Li {},
2172796a01cSAlec Li };
2182796a01cSAlec Li MODULE_DEVICE_TABLE(i2c, aw37503_id);
2192796a01cSAlec Li
2202796a01cSAlec Li static const struct of_device_id aw37503_of_match[] = {
2212796a01cSAlec Li {.compatible = "awinic,aw37503",},
2222796a01cSAlec Li { /* Sentinel */ },
2232796a01cSAlec Li };
2242796a01cSAlec Li
2252796a01cSAlec Li MODULE_DEVICE_TABLE(of, aw37503_of_match);
2262796a01cSAlec Li
2272796a01cSAlec Li static struct i2c_driver aw37503_i2c_driver = {
2282796a01cSAlec Li .driver = {
2292796a01cSAlec Li .name = "aw37503",
2302796a01cSAlec Li .of_match_table = aw37503_of_match,
2312796a01cSAlec Li },
232*21cc7f81SUwe Kleine-König .probe = aw37503_probe,
2332796a01cSAlec Li .id_table = aw37503_id,
2342796a01cSAlec Li };
2352796a01cSAlec Li
2362796a01cSAlec Li module_i2c_driver(aw37503_i2c_driver);
2372796a01cSAlec Li
2382796a01cSAlec Li MODULE_DESCRIPTION("aw37503 regulator driver");
2392796a01cSAlec Li MODULE_AUTHOR("Alec Li <like@awinic.com>");
2402796a01cSAlec Li MODULE_LICENSE("GPL");
241