xref: /OK3568_Linux_fs/kernel/drivers/regulator/lp8752.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * LP8752 High Performance Power Management Unit : System Interface Driver
3  *
4  * Author: zhangqing <zhangqing@rock-chips.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11 
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/err.h>
16 #include <linux/irq.h>
17 #include <linux/interrupt.h>
18 #include <linux/gpio.h>
19 #include <linux/regmap.h>
20 #include <linux/uaccess.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/of_regulator.h>
23 #include <linux/regulator/machine.h>
24 
25 #define LP8752_CTRL_BUCK0		0x02
26 #define LP8752_CTRL_BUCK1		0x04
27 #define LP8752_CTRL_BUCK2		0x06
28 #define LP8752_CTRL_BUCK3		0x08
29 
30 #define LP8752_VOUT_BUCK0		0x0a
31 #define LP8752_VOUT_BUCK1		0x0c
32 #define LP8752_VOUT_BUCK2		0x0e
33 #define LP8752_VOUT_BUCK3		0x10
34 
35 #define LP8752_BUCK_VSEL_MASK		0xff
36 #define LP8752_REG_MAX			0x2f
37 
38 enum lp8752_bucks {
39 	LP8752_BUCK0 = 0,
40 	LP8752_BUCK1,
41 	LP8752_BUCK2,
42 	LP8752_BUCK3,
43 	LP8752_BUCK_MAX,
44 };
45 
46 static const struct linear_range lp8752_buck_voltage_ranges[] = {
47 	REGULATOR_LINEAR_RANGE(500000, 0, 23, 10000),
48 	REGULATOR_LINEAR_RANGE(735000, 24, 157, 5000),
49 	REGULATOR_LINEAR_RANGE(1420000, 158, 255, 20000),
50 };
51 
52 struct lp8752_platform_data {
53 	int nphase;
54 	struct device_node *of_node[LP8752_BUCK_MAX];
55 	struct regulator_desc desc;
56 	struct regulator_init_data *buck_data[LP8752_BUCK_MAX];
57 };
58 
59 struct lp8752_mphase {
60 	int nreg;
61 	int buck_id[LP8752_BUCK_MAX];
62 };
63 
64 struct lp8752_chip {
65 	int nphase;
66 	struct device *dev;
67 	struct regmap *regmap;
68 	struct lp8752_platform_data *pdata;
69 	struct regulator_dev *rdev[LP8752_BUCK_MAX];
70 };
71 
lp8752_buck_set_mode(struct regulator_dev * rdev,unsigned int mode)72 static int lp8752_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
73 {
74 	struct lp8752_chip *pchip = rdev_get_drvdata(rdev);
75 	u8 msk = BIT(1);
76 	int ret;
77 
78 	switch (mode) {
79 	case REGULATOR_MODE_FAST:
80 		/* forced pwm mode */
81 		ret = regmap_update_bits(pchip->regmap,
82 					 rdev->desc->enable_reg,
83 					 msk, 0x1);
84 		break;
85 	case REGULATOR_MODE_NORMAL:
86 		/* automatic pwm/pfm mode */
87 		ret = regmap_update_bits(pchip->regmap,
88 					 rdev->desc->enable_reg,
89 					 msk, 0x0);
90 		break;
91 	default:
92 		dev_err(pchip->dev, "error:lp8752 only support auto and pwm mode\n");
93 		ret = -EINVAL;
94 		break;
95 	}
96 
97 	return ret;
98 }
99 
lp8752_buck_get_mode(struct regulator_dev * rdev)100 static unsigned int lp8752_buck_get_mode(struct regulator_dev *rdev)
101 {
102 	struct lp8752_chip *pchip = rdev_get_drvdata(rdev);
103 	int ret;
104 	unsigned int reg;
105 
106 	ret = regmap_read(pchip->regmap, rdev->desc->enable_reg, &reg);
107 	if (ret < 0) {
108 		dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
109 		return ret;
110 	}
111 
112 	return (reg & BIT(1)) ?  REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
113 }
114 
115 static struct regulator_ops lp8752_buck_ops = {
116 	.map_voltage = regulator_map_voltage_linear_range,
117 	.list_voltage = regulator_list_voltage_linear_range,
118 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
119 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
120 	.set_voltage_time_sel = regulator_set_voltage_time_sel,
121 	.enable = regulator_enable_regmap,
122 	.disable = regulator_disable_regmap,
123 	.is_enabled = regulator_is_enabled_regmap,
124 	.set_mode = lp8752_buck_set_mode,
125 	.get_mode = lp8752_buck_get_mode,
126 };
127 
128 #define lp8752_rail(_id) "lp8752_buck"#_id
129 #define vin(_id) "vin"#_id
130 
131 static const struct lp8752_mphase mphase_buck[] = {
132 	{ 1, { LP8752_BUCK0} },
133 	{ 2, { LP8752_BUCK0, LP8752_BUCK3 } },
134 	{ 3, { LP8752_BUCK0, LP8752_BUCK2, LP8752_BUCK3 } },
135 	{ 4, { LP8752_BUCK0, LP8752_BUCK1, LP8752_BUCK2, LP8752_BUCK3 } },
136 };
137 
138 static struct of_regulator_match lp8752_reg_matches[] = {
139 	{ .name = "lp8752_buck0", .driver_data = (void *)0 },
140 	{ .name = "lp8752_buck1", .driver_data = (void *)1 },
141 	{ .name = "lp8752_buck2", .driver_data = (void *)2 },
142 	{ .name = "lp8752_buck3", .driver_data = (void *)3 },
143 };
144 
lp8752_init_data(struct lp8752_chip * pchip)145 static int lp8752_init_data(struct lp8752_chip *pchip)
146 {
147 	int icnt, buck_id, count;
148 	struct lp8752_platform_data *pdata = pchip->pdata;
149 	struct device_node *regs, *lp8752_np;
150 
151 	lp8752_np = of_node_get(pchip->dev->of_node);
152 	if (!lp8752_np) {
153 		dev_err(pchip->dev, "Failed to find device node\n");
154 		return -ENODEV;
155 	}
156 
157 	regs = of_find_node_by_name(lp8752_np, "regulators");
158 	if (!regs)
159 		return -ENODEV;
160 
161 	count = of_regulator_match(pchip->dev, regs, lp8752_reg_matches,
162 				   LP8752_BUCK_MAX);
163 	of_node_put(regs);
164 	if ((count <= 0) || (count > LP8752_BUCK_MAX))
165 		return -ENODEV;
166 
167 	pchip->nphase = (count - 1) & 0xf;
168 
169 	/* set default data based on multi-phase config */
170 	for (icnt = 0; icnt < mphase_buck[pchip->nphase].nreg; icnt++) {
171 		buck_id = mphase_buck[pchip->nphase].buck_id[icnt];
172 		pdata->buck_data[buck_id] =
173 		lp8752_reg_matches[buck_id].init_data;
174 		pdata->of_node[buck_id] = lp8752_reg_matches[buck_id].of_node;
175 	}
176 
177 	return 0;
178 }
179 
180 #define lp8752_buck_desc(_id)\
181 {\
182 	.name = lp8752_rail(_id),\
183 	.id   = LP8752_BUCK##_id,\
184 	.supply_name = vin(_id),\
185 	.ops  = &lp8752_buck_ops,\
186 	.n_voltages = LP8752_BUCK_VSEL_MASK + 1,\
187 	.linear_ranges = lp8752_buck_voltage_ranges,\
188 	.n_linear_ranges = ARRAY_SIZE(lp8752_buck_voltage_ranges),\
189 	.type = REGULATOR_VOLTAGE,\
190 	.enable_reg = LP8752_CTRL_BUCK##_id,\
191 	.enable_mask = BIT(7),\
192 	.vsel_reg = LP8752_VOUT_BUCK##_id,\
193 	.vsel_mask = LP8752_BUCK_VSEL_MASK,\
194 	.enable_time = 400,\
195 	.owner = THIS_MODULE,\
196 }
197 
198 static struct regulator_desc lp8752_reg_desc[] = {
199 	lp8752_buck_desc(0),
200 	lp8752_buck_desc(1),
201 	lp8752_buck_desc(2),
202 	lp8752_buck_desc(3),
203 };
204 
lp8752_regulator_init(struct lp8752_chip * pchip)205 static int lp8752_regulator_init(struct lp8752_chip *pchip)
206 {
207 	int ret, icnt, buck_id;
208 	struct lp8752_platform_data *pdata = pchip->pdata;
209 	struct regulator_config rconfig = { };
210 
211 	rconfig.regmap = pchip->regmap;
212 	rconfig.dev = pchip->dev;
213 	rconfig.driver_data = pchip;
214 
215 	for (icnt = 0; icnt < mphase_buck[pchip->nphase].nreg; icnt++) {
216 		buck_id = mphase_buck[pchip->nphase].buck_id[icnt];
217 		rconfig.init_data = pdata->buck_data[buck_id];
218 		rconfig.of_node = pdata->of_node[buck_id];
219 		pchip->rdev[buck_id] =
220 		devm_regulator_register(pchip->dev,
221 					&lp8752_reg_desc[buck_id],
222 					&rconfig);
223 		if (IS_ERR(pchip->rdev[buck_id])) {
224 			ret = PTR_ERR(pchip->rdev[buck_id]);
225 			pchip->rdev[buck_id] = NULL;
226 			dev_err(pchip->dev, "regulator init failed: buck %d\n",
227 				buck_id);
228 			return ret;
229 		}
230 	}
231 
232 	return 0;
233 }
234 
235 static const struct regmap_config lp8752_regmap = {
236 	.reg_bits = 8,
237 	.val_bits = 8,
238 	.max_register = LP8752_REG_MAX,
239 };
240 
lp8752_probe(struct i2c_client * client,const struct i2c_device_id * id)241 static int lp8752_probe(struct i2c_client *client,
242 			const struct i2c_device_id *id)
243 {
244 	int ret;
245 	struct lp8752_chip *pchip;
246 	struct lp8752_platform_data *pdata = dev_get_platdata(&client->dev);
247 
248 	pchip = devm_kzalloc(&client->dev,
249 			     sizeof(struct lp8752_chip), GFP_KERNEL);
250 	if (!pchip)
251 		return -ENOMEM;
252 
253 	pchip->dev = &client->dev;
254 	pchip->regmap = devm_regmap_init_i2c(client, &lp8752_regmap);
255 	if (IS_ERR(pchip->regmap)) {
256 		ret = PTR_ERR(pchip->regmap);
257 		dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
258 		return ret;
259 	}
260 	i2c_set_clientdata(client, pchip);
261 
262 	ret = regmap_update_bits(pchip->regmap,
263 				 LP8752_CTRL_BUCK0,
264 				 (1 << 0), 0);
265 	ret = regmap_update_bits(pchip->regmap,
266 				 LP8752_CTRL_BUCK2,
267 				 (1 << 0), 0);
268 
269 	if (!pdata) {
270 		pchip->pdata = devm_kzalloc(pchip->dev,
271 					    sizeof(struct lp8752_platform_data),
272 					    GFP_KERNEL);
273 		if (!pchip->pdata)
274 			return -ENOMEM;
275 
276 		ret = lp8752_init_data(pchip);
277 		if (ret < 0) {
278 			dev_err(&client->dev, "fail to initialize chip\n");
279 			return ret;
280 		}
281 	} else {
282 		pchip->pdata = pdata;
283 		pchip->nphase = pdata->nphase;
284 	}
285 
286 	lp8752_regulator_init(pchip);
287 
288 	return 0;
289 }
290 
291 static const struct of_device_id lp8752_of_match[] = {
292 	{ .compatible = "ti,lp8752", },
293 	{},
294 };
295 MODULE_DEVICE_TABLE(of, lp8752_of_match);
296 
297 static const struct i2c_device_id lp8752_id[] = {
298 	{"lp8752", 0},
299 	{}
300 };
301 MODULE_DEVICE_TABLE(i2c, lp8752_id);
302 
303 static struct i2c_driver lp8752_i2c_driver = {
304 	.probe = lp8752_probe,
305 	.id_table = lp8752_id,
306 	.driver = {
307 		.name = "lp8752",
308 		.of_match_table = of_match_ptr(lp8752_of_match),
309 	},
310 };
311 
312 module_i2c_driver(lp8752_i2c_driver);
313 
314 MODULE_DESCRIPTION("Texas Instruments lp8752 driver");
315 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
316 MODULE_LICENSE("GPL v2");
317