^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // sy8106a-regulator.c - Regulator device driver for SY8106A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright (C) 2016 Ondřej Jirman <megous@megous.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // Copyright (c) 2017-2018 Icenowy Zheng <icenowy@aosc.io>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/regulator/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/regulator/of_regulator.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define SY8106A_REG_VOUT1_SEL 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define SY8106A_REG_VOUT_COM 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define SY8106A_REG_VOUT1_SEL_MASK 0x7f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define SY8106A_DISABLE_REG BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * The I2C controlled voltage will only work when this bit is set; otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * it will behave like a fixed regulator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define SY8106A_GO_BIT BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static const struct regmap_config sy8106a_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static const struct regulator_ops sy8106a_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .set_voltage_sel = regulator_set_voltage_sel_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .set_voltage_time_sel = regulator_set_voltage_time_sel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .get_voltage_sel = regulator_get_voltage_sel_regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .list_voltage = regulator_list_voltage_linear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* Enabling/disabling the regulator is not yet implemented */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* Default limits measured in millivolts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define SY8106A_MIN_MV 680
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define SY8106A_MAX_MV 1950
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define SY8106A_STEP_MV 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static const struct regulator_desc sy8106a_reg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) .name = "SY8106A",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .id = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) .ops = &sy8106a_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) .type = REGULATOR_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .n_voltages = ((SY8106A_MAX_MV - SY8106A_MIN_MV) / SY8106A_STEP_MV) + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .min_uV = (SY8106A_MIN_MV * 1000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .uV_step = (SY8106A_STEP_MV * 1000),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .vsel_reg = SY8106A_REG_VOUT1_SEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .vsel_mask = SY8106A_REG_VOUT1_SEL_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * This ramp_delay is a conservative default value which works on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * H3/H5 boards VDD-CPUX situations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .ramp_delay = 200,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * I2C driver interface functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int sy8106a_i2c_probe(struct i2c_client *i2c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct device *dev = &i2c->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct regulator_dev *rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct regulator_config config = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned int reg, vsel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u32 fixed_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) error = of_property_read_u32(dev->of_node, "silergy,fixed-microvolt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) &fixed_voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (fixed_voltage < SY8106A_MIN_MV * 1000 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) fixed_voltage > SY8106A_MAX_MV * 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) regmap = devm_regmap_init_i2c(i2c, &sy8106a_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (IS_ERR(regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) error = PTR_ERR(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) dev_err(dev, "Failed to allocate register map: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) config.dev = &i2c->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) config.regmap = regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) config.of_node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) config.init_data = of_get_regulator_init_data(dev, dev->of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) &sy8106a_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!config.init_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* Ensure GO_BIT is enabled when probing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) error = regmap_read(regmap, SY8106A_REG_VOUT1_SEL, ®);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (!(reg & SY8106A_GO_BIT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) vsel = (fixed_voltage / 1000 - SY8106A_MIN_MV) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) SY8106A_STEP_MV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) error = regmap_write(regmap, SY8106A_REG_VOUT1_SEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) vsel | SY8106A_GO_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* Probe regulator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) rdev = devm_regulator_register(&i2c->dev, &sy8106a_reg, &config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (IS_ERR(rdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) error = PTR_ERR(rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dev_err(&i2c->dev, "Failed to register SY8106A regulator: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static const struct of_device_id __maybe_unused sy8106a_i2c_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) { .compatible = "silergy,sy8106a" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) MODULE_DEVICE_TABLE(of, sy8106a_i2c_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static const struct i2c_device_id sy8106a_i2c_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) { "sy8106a", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) MODULE_DEVICE_TABLE(i2c, sy8106a_i2c_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static struct i2c_driver sy8106a_regulator_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .name = "sy8106a",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) .of_match_table = of_match_ptr(sy8106a_i2c_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .probe_new = sy8106a_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .id_table = sy8106a_i2c_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) module_i2c_driver(sy8106a_regulator_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) MODULE_AUTHOR("Ondřej Jirman <megous@megous.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) MODULE_AUTHOR("Icenowy Zheng <icenowy@aosc.io>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) MODULE_DESCRIPTION("Regulator device driver for Silergy SY8106A");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) MODULE_LICENSE("GPL");