1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/bitops.h>
4 #include <linux/delay.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/i2c.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>
12 
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
19 
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)
24 
25 /* HW Enable + Soft start time */
26 #define RT6245_ENTIME_IN_US	5000
27 
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)
32 
33 struct rt6245_priv {
34 	struct gpio_desc *enable_gpio;
35 	bool enable_state;
36 };
37 
38 static int rt6245_enable(struct regulator_dev *rdev)
39 {
40 	struct rt6245_priv *priv = rdev_get_drvdata(rdev);
41 	struct regmap *regmap = rdev_get_regmap(rdev);
42 	int ret;
43 
44 	if (!priv->enable_gpio)
45 		return 0;
46 
47 	gpiod_direction_output(priv->enable_gpio, 1);
48 	usleep_range(RT6245_ENTIME_IN_US, RT6245_ENTIME_IN_US + 1000);
49 
50 	regcache_cache_only(regmap, false);
51 	ret = regcache_sync(regmap);
52 	if (ret)
53 		return ret;
54 
55 	priv->enable_state = true;
56 	return 0;
57 }
58 
59 static int rt6245_disable(struct regulator_dev *rdev)
60 {
61 	struct rt6245_priv *priv = rdev_get_drvdata(rdev);
62 	struct regmap *regmap = rdev_get_regmap(rdev);
63 
64 	if (!priv->enable_gpio)
65 		return -EINVAL;
66 
67 	regcache_cache_only(regmap, true);
68 	regcache_mark_dirty(regmap);
69 
70 	gpiod_direction_output(priv->enable_gpio, 0);
71 
72 	priv->enable_state = false;
73 	return 0;
74 }
75 
76 static int rt6245_is_enabled(struct regulator_dev *rdev)
77 {
78 	struct rt6245_priv *priv = rdev_get_drvdata(rdev);
79 
80 	return priv->enable_state ? 1 : 0;
81 }
82 
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,
91 };
92 
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
96 };
97 
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,
112 };
113 
114 static int rt6245_init_device_properties(struct device *dev)
115 {
116 	const struct {
117 		const char *name;
118 		unsigned int reg;
119 	} rt6245_props[] = {
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 }
124 	};
125 	struct regmap *regmap = dev_get_regmap(dev, NULL);
126 	u8 propval;
127 	int i, ret;
128 
129 	for (i = 0; i < ARRAY_SIZE(rt6245_props); i++) {
130 		ret = device_property_read_u8(dev, rt6245_props[i].name, &propval);
131 		if (ret)
132 			continue;
133 
134 		ret = regmap_write(regmap, rt6245_props[i].reg, propval);
135 		if (ret) {
136 			dev_err(dev, "Fail to apply [%s:%d]\n", rt6245_props[i].name, propval);
137 			return ret;
138 		}
139 	}
140 
141 	return 0;
142 }
143 
144 static int rt6245_reg_write(void *context, unsigned int reg, unsigned int val)
145 {
146 	struct i2c_client *i2c = context;
147 	const u8 func_base[] = { 0x6F, 0x73, 0x78, 0x61, 0x7C, 0 };
148 	unsigned int code, bit_count;
149 
150 	code = func_base[reg];
151 	code += val;
152 
153 	/* xor checksum for bit 6 to 0 */
154 	bit_count = hweight8(code & RT6245_CODE_MASK);
155 	if (bit_count % 2)
156 		code |= RT6245_CHKSUM_MASK;
157 	else
158 		code &= ~RT6245_CHKSUM_MASK;
159 
160 	return i2c_smbus_write_byte(i2c, code);
161 }
162 
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 }
176 };
177 
178 static const struct regmap_config rt6245_regmap_config = {
179 	.reg_bits = 8,
180 	.val_bits = 8,
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,
186 };
187 
188 static int rt6245_probe(struct i2c_client *i2c)
189 {
190 	struct rt6245_priv *priv;
191 	struct regmap *regmap;
192 	struct regulator_config regulator_cfg = {};
193 	struct regulator_dev *rdev;
194 	int ret;
195 
196 	priv = devm_kzalloc(&i2c->dev, sizeof(*priv), GFP_KERNEL);
197 	if (!priv)
198 		return -ENOMEM;
199 
200 	priv->enable_state = true;
201 
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);
206 	}
207 
208 	usleep_range(RT6245_ENTIME_IN_US, RT6245_ENTIME_IN_US + 1000);
209 
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);
214 	}
215 
216 	ret = rt6245_init_device_properties(&i2c->dev);
217 	if (ret) {
218 		dev_err(&i2c->dev, "Failed to initialize device properties\n");
219 		return ret;
220 	}
221 
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, &regulator_cfg);
229 	if (IS_ERR(rdev)) {
230 		dev_err(&i2c->dev, "Failed to register regulator\n");
231 		return PTR_ERR(rdev);
232 	}
233 
234 	return 0;
235 }
236 
237 static const struct of_device_id __maybe_unused rt6245_of_match_table[] = {
238 	{ .compatible = "richtek,rt6245", },
239 	{}
240 };
241 MODULE_DEVICE_TABLE(of, rt6245_of_match_table);
242 
243 static struct i2c_driver rt6245_driver = {
244 	.driver = {
245 		.name = "rt6245",
246 		.of_match_table = rt6245_of_match_table,
247 	},
248 	.probe_new = rt6245_probe,
249 };
250 module_i2c_driver(rt6245_driver);
251 
252 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
253 MODULE_DESCRIPTION("Richtek RT6245 Regulator Driver");
254 MODULE_LICENSE("GPL v2");
255