1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/bitops.h>
4 #include <linux/delay.h>
5 #include <linux/gpio/consumer.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/regmap.h>
10 #include <linux/regulator/driver.h>
11 #include <linux/regulator/of_regulator.h>
13 #define RT6245_VIRT_OCLIMIT 0x00
14 #define RT6245_VIRT_OTLEVEL 0x01
15 #define RT6245_VIRT_PGDLYTIME 0x02
16 #define RT6245_VIRT_SLEWRATE 0x03
17 #define RT6245_VIRT_SWFREQ 0x04
18 #define RT6245_VIRT_VOUT 0x05
20 #define RT6245_VOUT_MASK GENMASK(6, 0)
21 #define RT6245_SLEW_MASK GENMASK(2, 0)
22 #define RT6245_CHKSUM_MASK BIT(7)
23 #define RT6245_CODE_MASK GENMASK(6, 0)
25 /* HW Enable + Soft start time */
26 #define RT6245_ENTIME_IN_US 5000
28 #define RT6245_VOUT_MINUV 437500
29 #define RT6245_VOUT_MAXUV 1387500
30 #define RT6245_VOUT_STEPUV 12500
31 #define RT6245_NUM_VOUT ((RT6245_VOUT_MAXUV - RT6245_VOUT_MINUV) / RT6245_VOUT_STEPUV + 1)
34 struct gpio_desc *enable_gpio;
38 static int rt6245_enable(struct regulator_dev *rdev)
40 struct rt6245_priv *priv = rdev_get_drvdata(rdev);
41 struct regmap *regmap = rdev_get_regmap(rdev);
44 if (!priv->enable_gpio)
47 gpiod_direction_output(priv->enable_gpio, 1);
48 usleep_range(RT6245_ENTIME_IN_US, RT6245_ENTIME_IN_US + 1000);
50 regcache_cache_only(regmap, false);
51 ret = regcache_sync(regmap);
55 priv->enable_state = true;
59 static int rt6245_disable(struct regulator_dev *rdev)
61 struct rt6245_priv *priv = rdev_get_drvdata(rdev);
62 struct regmap *regmap = rdev_get_regmap(rdev);
64 if (!priv->enable_gpio)
67 regcache_cache_only(regmap, true);
68 regcache_mark_dirty(regmap);
70 gpiod_direction_output(priv->enable_gpio, 0);
72 priv->enable_state = false;
76 static int rt6245_is_enabled(struct regulator_dev *rdev)
78 struct rt6245_priv *priv = rdev_get_drvdata(rdev);
80 return priv->enable_state ? 1 : 0;
83 static const struct regulator_ops rt6245_regulator_ops = {
84 .list_voltage = regulator_list_voltage_linear,
85 .set_voltage_sel = regulator_set_voltage_sel_regmap,
86 .get_voltage_sel = regulator_get_voltage_sel_regmap,
87 .set_ramp_delay = regulator_set_ramp_delay_regmap,
88 .enable = rt6245_enable,
89 .disable = rt6245_disable,
90 .is_enabled = rt6245_is_enabled,
93 /* ramp delay dividend is 12500 uV/uS, and divisor from 1 to 8 */
94 static const unsigned int rt6245_ramp_delay_table[] = {
95 12500, 6250, 4167, 3125, 2500, 2083, 1786, 1562
98 static const struct regulator_desc rt6245_regulator_desc = {
99 .name = "rt6245-regulator",
100 .ops = &rt6245_regulator_ops,
101 .type = REGULATOR_VOLTAGE,
102 .min_uV = RT6245_VOUT_MINUV,
103 .uV_step = RT6245_VOUT_STEPUV,
104 .n_voltages = RT6245_NUM_VOUT,
105 .ramp_delay_table = rt6245_ramp_delay_table,
106 .n_ramp_values = ARRAY_SIZE(rt6245_ramp_delay_table),
107 .owner = THIS_MODULE,
108 .vsel_reg = RT6245_VIRT_VOUT,
109 .vsel_mask = RT6245_VOUT_MASK,
110 .ramp_reg = RT6245_VIRT_SLEWRATE,
111 .ramp_mask = RT6245_SLEW_MASK,
114 static int rt6245_init_device_properties(struct device *dev)
120 { "richtek,oc-level-select", RT6245_VIRT_OCLIMIT },
121 { "richtek,ot-level-select", RT6245_VIRT_OTLEVEL },
122 { "richtek,pgdly-time-select", RT6245_VIRT_PGDLYTIME },
123 { "richtek,switch-freq-select", RT6245_VIRT_SWFREQ }
125 struct regmap *regmap = dev_get_regmap(dev, NULL);
129 for (i = 0; i < ARRAY_SIZE(rt6245_props); i++) {
130 ret = device_property_read_u8(dev, rt6245_props[i].name, &propval);
134 ret = regmap_write(regmap, rt6245_props[i].reg, propval);
136 dev_err(dev, "Fail to apply [%s:%d]\n", rt6245_props[i].name, propval);
144 static int rt6245_reg_write(void *context, unsigned int reg, unsigned int val)
146 struct i2c_client *i2c = context;
147 static const u8 func_base[] = { 0x6F, 0x73, 0x78, 0x61, 0x7C, 0 };
148 unsigned int code, bit_count;
150 code = func_base[reg];
153 /* xor checksum for bit 6 to 0 */
154 bit_count = hweight8(code & RT6245_CODE_MASK);
156 code |= RT6245_CHKSUM_MASK;
158 code &= ~RT6245_CHKSUM_MASK;
160 return i2c_smbus_write_byte(i2c, code);
163 static const struct reg_default rt6245_reg_defaults[] = {
164 /* Default over current 14A */
165 { RT6245_VIRT_OCLIMIT, 2 },
166 /* Default over temperature 150'c */
167 { RT6245_VIRT_OTLEVEL, 0 },
168 /* Default power good delay time 10us */
169 { RT6245_VIRT_PGDLYTIME, 1 },
170 /* Default slewrate 12.5mV/uS */
171 { RT6245_VIRT_SLEWRATE, 0 },
172 /* Default switch frequency 800KHz */
173 { RT6245_VIRT_SWFREQ, 1 },
174 /* Default voltage 750mV */
175 { RT6245_VIRT_VOUT, 0x19 }
178 static const struct regmap_config rt6245_regmap_config = {
181 .max_register = RT6245_VIRT_VOUT,
182 .cache_type = REGCACHE_FLAT,
183 .reg_defaults = rt6245_reg_defaults,
184 .num_reg_defaults = ARRAY_SIZE(rt6245_reg_defaults),
185 .reg_write = rt6245_reg_write,
188 static int rt6245_probe(struct i2c_client *i2c)
190 struct rt6245_priv *priv;
191 struct regmap *regmap;
192 struct regulator_config regulator_cfg = {};
193 struct regulator_dev *rdev;
196 priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
200 priv->enable_state = true;
202 priv->enable_gpio = devm_gpiod_get_optional(&i2c->dev, "enable", GPIOD_OUT_HIGH);
203 if (IS_ERR(priv->enable_gpio)) {
204 dev_err(&i2c->dev, "Failed to get 'enable' gpio\n");
205 return PTR_ERR(priv->enable_gpio);
208 usleep_range(RT6245_ENTIME_IN_US, RT6245_ENTIME_IN_US + 1000);
210 regmap = devm_regmap_init(&i2c->dev, NULL, i2c, &rt6245_regmap_config);
211 if (IS_ERR(regmap)) {
212 dev_err(&i2c->dev, "Failed to initialize the regmap\n");
213 return PTR_ERR(regmap);
216 ret = rt6245_init_device_properties(&i2c->dev);
218 dev_err(&i2c->dev, "Failed to initialize device properties\n");
222 regulator_cfg.dev = &i2c->dev;
223 regulator_cfg.of_node = i2c->dev.of_node;
224 regulator_cfg.regmap = regmap;
225 regulator_cfg.driver_data = priv;
226 regulator_cfg.init_data = of_get_regulator_init_data(&i2c->dev, i2c->dev.of_node,
227 &rt6245_regulator_desc);
228 rdev = devm_regulator_register(&i2c->dev, &rt6245_regulator_desc, ®ulator_cfg);
230 dev_err(&i2c->dev, "Failed to register regulator\n");
231 return PTR_ERR(rdev);
237 static const struct of_device_id __maybe_unused rt6245_of_match_table[] = {
238 { .compatible = "richtek,rt6245", },
241 MODULE_DEVICE_TABLE(of, rt6245_of_match_table);
243 static struct i2c_driver rt6245_driver = {
246 .of_match_table = rt6245_of_match_table,
248 .probe_new = rt6245_probe,
250 module_i2c_driver(rt6245_driver);
252 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
253 MODULE_DESCRIPTION("Richtek RT6245 Regulator Driver");
254 MODULE_LICENSE("GPL v2");