xref: /OK3568_Linux_fs/kernel/drivers/regulator/wl2868c-regulator.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2021 Rockchip Electronics Co. Ltd.
4  *
5  * Author: Shunqing Chen <csq@rock-chips.com>
6  */
7 
8 #include <linux/err.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/of_gpio.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/machine.h>
18 #include <linux/regulator/of_regulator.h>
19 
20 #define WL2868C_DEVICE_D1		0x00
21 #define WL2868C_DEVICE_D2		0x01
22 #define WL2868C_DISCHARGE_RESISTORS	0x02
23 #define WL2868C_LDO1_VOUT		0x03
24 #define WL2868C_LDO2_VOUT		0x04
25 #define WL2868C_LDO3_VOUT		0x05
26 #define WL2868C_LDO4_VOUT		0x06
27 #define WL2868C_LDO5_VOUT		0x07
28 #define WL2868C_LDO6_VOUT		0x08
29 #define WL2868C_LDO7_VOUT		0x09
30 #define WL2868C_LDO1_LDO2_SEQ		0x0a
31 #define WL2868C_LDO3_LDO4_SEQ		0x0b
32 #define WL2868C_LDO5_LDO6_SEQ		0x0c
33 #define WL2868C_LDO7_SEQ		0x0d
34 #define WL2868C_LDO_EN			0x0e
35 #define WL2868C_SEQ_STATUS		0x0f
36 #define WL2868C_LDO1_STATUS		0x10
37 #define WL2868C_LDO1_OCP_CTL		0x11
38 #define WL2868C_LDO2_STATUS		0x12
39 #define WL2868C_LDO2_OCP_CTL		0x13
40 #define WL2868C_LDO3_STATUS		0x14
41 #define WL2868C_LDO3_OCP_CTL		0x15
42 #define WL2868C_LDO4_STATUS		0x16
43 #define WL2868C_LDO4_OCP_CTL		0x17
44 #define WL2868C_LDO5_STATUS		0x18
45 #define WL2868C_LDO5_OCP_CTL		0x19
46 #define WL2868C_LDO6_STATUS		0x1a
47 #define WL2868C_LDO6_OCP_CTL		0x1b
48 #define WL2868C_LDO7_STATUS		0x1c
49 #define WL2868C_LDO7_OCP_CTL		0x1d
50 #define WL2868C_REPROGRAMMABLE_I2C_ADDR	0x1e
51 #define RESERVED_1			0x1f
52 #define INT_LATCHED_CLR			0x20
53 #define INT_EN_SET			0x21
54 #define INT_LATCHED_STS			0x22
55 #define INT_PENDING_STS			0x23
56 #define UVLO_CTL			0x24
57 #define RESERVED_2			0x25
58 
59 #define WL2868C_VSEL_MASK		0xff
60 
61 enum wl2868c_regulators {
62 	WL2868C_REGULATOR_LDO1 = 0,
63 	WL2868C_REGULATOR_LDO2,
64 	WL2868C_REGULATOR_LDO3,
65 	WL2868C_REGULATOR_LDO4,
66 	WL2868C_REGULATOR_LDO5,
67 	WL2868C_REGULATOR_LDO6,
68 	WL2868C_REGULATOR_LDO7,
69 	WL2868C_MAX_REGULATORS,
70 };
71 
72 struct wl2868c {
73 	struct device *dev;
74 	struct regmap *regmap;
75 	struct regulator_dev *rdev;
76 	struct gpio_desc *reset_gpio;
77 	int min_dropout_uv;
78 	int ldo_vout[7];
79 	int ldo_en;
80 };
81 
82 static const struct regulator_ops wl2868c_reg_ops = {
83 	.list_voltage		= regulator_list_voltage_linear,
84 	.map_voltage		= regulator_map_voltage_linear,
85 	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
86 	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
87 	.enable			= regulator_enable_regmap,
88 	.disable		= regulator_disable_regmap,
89 	.is_enabled		= regulator_is_enabled_regmap,
90 };
91 
92 #define WL2868C_DESC(_id, _match, _supply, _min, _max, _step, _vreg,	\
93 	_vmask, _ereg, _emask, _enval, _disval)		\
94 	{								\
95 		.name		= (_match),				\
96 		.supply_name	= (_supply),				\
97 		.of_match	= of_match_ptr(_match),			\
98 		.regulators_node = of_match_ptr("regulators"),		\
99 		.type		= REGULATOR_VOLTAGE,			\
100 		.id		= (_id),				\
101 		.n_voltages	= (((_max) - (_min)) / (_step) + 1),	\
102 		.owner		= THIS_MODULE,				\
103 		.min_uV		= (_min) * 1000,			\
104 		.uV_step	= (_step) * 1000,			\
105 		.vsel_reg	= (_vreg),				\
106 		.vsel_mask	= (_vmask),				\
107 		.enable_reg	= (_ereg),				\
108 		.enable_mask	= (_emask),				\
109 		.enable_val     = (_enval),				\
110 		.disable_val     = (_disval),				\
111 		.ops		= &wl2868c_reg_ops,			\
112 	}
113 
114 static const struct regulator_desc wl2868c_reg[] = {
115 	WL2868C_DESC(WL2868C_REGULATOR_LDO1, "WL_LDO1", "ldo1", 496, 2536, 8,
116 		     WL2868C_LDO1_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(0), BIT(0), 0),
117 	WL2868C_DESC(WL2868C_REGULATOR_LDO2, "WL_LDO2", "ldo2", 496, 2536, 8,
118 		     WL2868C_LDO2_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(1), BIT(1), 0),
119 	WL2868C_DESC(WL2868C_REGULATOR_LDO3, "WL_LDO3", "ldo3", 1504, 3544, 8,
120 		     WL2868C_LDO3_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(2), BIT(2), 0),
121 	WL2868C_DESC(WL2868C_REGULATOR_LDO4, "WL_LDO4", "ldo4", 1504, 3544, 8,
122 		     WL2868C_LDO4_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(3), BIT(3), 0),
123 	WL2868C_DESC(WL2868C_REGULATOR_LDO5, "WL_LDO5", "ldo5", 1504, 3544, 8,
124 		     WL2868C_LDO5_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(4), BIT(4), 0),
125 	WL2868C_DESC(WL2868C_REGULATOR_LDO6, "WL_LDO6", "ldo6", 1504, 3544, 8,
126 		     WL2868C_LDO6_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(5), BIT(5), 0),
127 	WL2868C_DESC(WL2868C_REGULATOR_LDO7, "WL_LDO7", "ldo7", 1504, 3544, 8,
128 		     WL2868C_LDO7_VOUT, WL2868C_VSEL_MASK, WL2868C_LDO_EN, BIT(6), BIT(6), 0),
129 };
130 
131 static const struct regmap_range wl2868c_writeable_ranges[] = {
132 	regmap_reg_range(WL2868C_DISCHARGE_RESISTORS, WL2868C_SEQ_STATUS),
133 	regmap_reg_range(WL2868C_LDO1_OCP_CTL, WL2868C_LDO1_OCP_CTL),
134 	regmap_reg_range(WL2868C_LDO2_OCP_CTL, WL2868C_LDO2_OCP_CTL),
135 	regmap_reg_range(WL2868C_LDO3_OCP_CTL, WL2868C_LDO3_OCP_CTL),
136 	regmap_reg_range(WL2868C_LDO4_OCP_CTL, WL2868C_LDO4_OCP_CTL),
137 	regmap_reg_range(WL2868C_LDO5_OCP_CTL, WL2868C_LDO5_OCP_CTL),
138 	regmap_reg_range(WL2868C_LDO6_OCP_CTL, WL2868C_LDO6_OCP_CTL),
139 	regmap_reg_range(WL2868C_LDO7_OCP_CTL, WL2868C_REPROGRAMMABLE_I2C_ADDR),
140 	regmap_reg_range(INT_LATCHED_CLR, INT_LATCHED_CLR),
141 	regmap_reg_range(INT_EN_SET, INT_EN_SET),
142 	regmap_reg_range(UVLO_CTL, UVLO_CTL),
143 };
144 
145 static const struct regmap_range wl2868c_readable_ranges[] = {
146 	regmap_reg_range(WL2868C_DEVICE_D1, RESERVED_2),
147 };
148 
149 static const struct regmap_range wl2868c_volatile_ranges[] = {
150 	regmap_reg_range(WL2868C_DISCHARGE_RESISTORS, WL2868C_SEQ_STATUS),
151 	regmap_reg_range(WL2868C_LDO1_OCP_CTL, WL2868C_LDO1_OCP_CTL),
152 	regmap_reg_range(WL2868C_LDO2_OCP_CTL, WL2868C_LDO2_OCP_CTL),
153 	regmap_reg_range(WL2868C_LDO3_OCP_CTL, WL2868C_LDO3_OCP_CTL),
154 	regmap_reg_range(WL2868C_LDO4_OCP_CTL, WL2868C_LDO4_OCP_CTL),
155 	regmap_reg_range(WL2868C_LDO5_OCP_CTL, WL2868C_LDO5_OCP_CTL),
156 	regmap_reg_range(WL2868C_LDO6_OCP_CTL, WL2868C_LDO6_OCP_CTL),
157 	regmap_reg_range(WL2868C_LDO7_OCP_CTL, WL2868C_REPROGRAMMABLE_I2C_ADDR),
158 	regmap_reg_range(INT_LATCHED_CLR, INT_LATCHED_CLR),
159 	regmap_reg_range(INT_EN_SET, INT_EN_SET),
160 	regmap_reg_range(UVLO_CTL, UVLO_CTL),
161 };
162 
163 static const struct regmap_access_table wl2868c_writeable_table = {
164 	.yes_ranges   = wl2868c_writeable_ranges,
165 	.n_yes_ranges = ARRAY_SIZE(wl2868c_writeable_ranges),
166 };
167 
168 static const struct regmap_access_table wl2868c_readable_table = {
169 	.yes_ranges   = wl2868c_readable_ranges,
170 	.n_yes_ranges = ARRAY_SIZE(wl2868c_readable_ranges),
171 };
172 
173 static const struct regmap_access_table wl2868c_volatile_table = {
174 	.yes_ranges   = wl2868c_volatile_ranges,
175 	.n_yes_ranges = ARRAY_SIZE(wl2868c_volatile_ranges),
176 };
177 
178 static const struct regmap_config wl2868c_regmap_config = {
179 	.reg_bits = 8,
180 	.val_bits = 8,
181 	.max_register = RESERVED_2,
182 	.wr_table = &wl2868c_writeable_table,
183 	.rd_table = &wl2868c_readable_table,
184 	.cache_type = REGCACHE_RBTREE,
185 	.volatile_table = &wl2868c_volatile_table,
186 };
187 
wl2868c_reset(struct wl2868c * wl2868c)188 static void wl2868c_reset(struct wl2868c *wl2868c)
189 {
190 	gpiod_set_value_cansleep(wl2868c->reset_gpio, 0);
191 	usleep_range(10000, 11000);
192 	gpiod_set_value_cansleep(wl2868c->reset_gpio, 1);
193 	usleep_range(10000, 11000);
194 	gpiod_set_value_cansleep(wl2868c->reset_gpio, 0);
195 	usleep_range(10000, 11000);
196 }
197 
wl2868c_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)198 static int wl2868c_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
199 {
200 	struct device *dev = &client->dev;
201 	struct regulator_config config = {};
202 	struct regulator_dev *rdev;
203 	const struct regulator_desc *regulators;
204 	struct wl2868c *wl2868c;
205 	int ret, i;
206 
207 	wl2868c = devm_kzalloc(dev, sizeof(struct wl2868c), GFP_KERNEL);
208 	if (!wl2868c)
209 		return -ENOMEM;
210 
211 	wl2868c->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
212 	if (IS_ERR(wl2868c->reset_gpio)) {
213 		ret = PTR_ERR(wl2868c->reset_gpio);
214 		dev_err(dev, "failed to request reset GPIO: %d\n", ret);
215 		return ret;
216 	}
217 
218 	wl2868c_reset(wl2868c);
219 
220 	i2c_set_clientdata(client, wl2868c);
221 	wl2868c->dev = dev;
222 	wl2868c->regmap = devm_regmap_init_i2c(client, &wl2868c_regmap_config);
223 	if (IS_ERR(wl2868c->regmap)) {
224 		ret = PTR_ERR(wl2868c->regmap);
225 		dev_err(dev, "Failed to allocate register map: %d\n", ret);
226 		return ret;
227 	}
228 
229 	config.dev = &client->dev;
230 	config.regmap = wl2868c->regmap;
231 	regulators = wl2868c_reg;
232 	/* Instantiate the regulators */
233 	for (i = 0; i < WL2868C_MAX_REGULATORS; i++) {
234 		rdev = devm_regulator_register(&client->dev,
235 					       &regulators[i], &config);
236 		if (IS_ERR(rdev)) {
237 			dev_err(&client->dev,
238 				"failed to register %d regulator\n", i);
239 			return PTR_ERR(rdev);
240 		}
241 	}
242 
243 	return 0;
244 }
245 
wl2868c_regulator_shutdown(struct i2c_client * client)246 static void wl2868c_regulator_shutdown(struct i2c_client *client)
247 {
248 	struct wl2868c *wl2868c = i2c_get_clientdata(client);
249 
250 	if (system_state == SYSTEM_POWER_OFF)
251 		regmap_write(wl2868c->regmap, WL2868C_LDO_EN, 0x80);
252 }
253 
wl2868c_suspend(struct device * dev)254 static int __maybe_unused wl2868c_suspend(struct device *dev)
255 {
256 	struct i2c_client *client = to_i2c_client(dev);
257 	struct wl2868c *wl2868c = i2c_get_clientdata(client);
258 	int i;
259 
260 	regmap_read(wl2868c->regmap, WL2868C_LDO_EN, &wl2868c->ldo_en);
261 	for (i = 0; i < ARRAY_SIZE(wl2868c->ldo_vout); i++)
262 		regmap_read(wl2868c->regmap, WL2868C_LDO1_VOUT + i,
263 			    &wl2868c->ldo_vout[i]);
264 
265 	return 0;
266 }
267 
wl2868c_resume(struct device * dev)268 static int __maybe_unused wl2868c_resume(struct device *dev)
269 {
270 	struct i2c_client *client = to_i2c_client(dev);
271 	struct wl2868c *wl2868c = i2c_get_clientdata(client);
272 	int i;
273 
274 	wl2868c_reset(wl2868c);
275 	for (i = 0; i < ARRAY_SIZE(wl2868c->ldo_vout); i++)
276 		regmap_write(wl2868c->regmap, WL2868C_LDO1_VOUT + i,
277 			     wl2868c->ldo_vout[i]);
278 	regmap_write(wl2868c->regmap, WL2868C_LDO_EN, wl2868c->ldo_en);
279 
280 	return 0;
281 }
282 
283 static SIMPLE_DEV_PM_OPS(wl2868c_pm_ops, wl2868c_suspend, wl2868c_resume);
284 
285 static const struct i2c_device_id wl2868c_i2c_id[] = {
286 	{ "wl2868c", 0 },
287 	{ }
288 };
289 
290 MODULE_DEVICE_TABLE(i2c, wl2868c_i2c_id);
291 
292 static const struct of_device_id wl2868c_of_match[] = {
293 	{ .compatible = "willsemi,wl2868c" },
294 	{}
295 };
296 MODULE_DEVICE_TABLE(of, wl2868c_of_match);
297 
298 static struct i2c_driver wl2868c_i2c_driver = {
299 	.driver = {
300 		.name = "wl2868c",
301 		.of_match_table = of_match_ptr(wl2868c_of_match),
302 		.pm = &wl2868c_pm_ops,
303 	},
304 	.id_table = wl2868c_i2c_id,
305 	.probe	= wl2868c_i2c_probe,
306 	.shutdown = wl2868c_regulator_shutdown,
307 };
308 
309 module_i2c_driver(wl2868c_i2c_driver);
310 
311 MODULE_DESCRIPTION("WL2868C regulator driver");
312 MODULE_AUTHOR("Shunqing Chen <csq@rock-chips.com>");
313 MODULE_LICENSE("GPL");
314