2 * LP8752 High Performance Power Management Unit : System Interface Driver
4 * Author: zhangqing <zhangqing@rock-chips.com>
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.
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>
25 #define LP8752_CTRL_BUCK0 0x02
26 #define LP8752_CTRL_BUCK1 0x04
27 #define LP8752_CTRL_BUCK2 0x06
28 #define LP8752_CTRL_BUCK3 0x08
30 #define LP8752_VOUT_BUCK0 0x0a
31 #define LP8752_VOUT_BUCK1 0x0c
32 #define LP8752_VOUT_BUCK2 0x0e
33 #define LP8752_VOUT_BUCK3 0x10
35 #define LP8752_BUCK_VSEL_MASK 0xff
36 #define LP8752_REG_MAX 0x2f
46 static const struct regulator_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),
52 struct lp8752_platform_data {
54 struct device_node *of_node[LP8752_BUCK_MAX];
55 struct regulator_desc desc;
56 struct regulator_init_data *buck_data[LP8752_BUCK_MAX];
59 struct lp8752_mphase {
61 int buck_id[LP8752_BUCK_MAX];
67 struct regmap *regmap;
68 struct lp8752_platform_data *pdata;
69 struct regulator_dev *rdev[LP8752_BUCK_MAX];
72 static int lp8752_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
74 struct lp8752_chip *pchip = rdev_get_drvdata(rdev);
79 case REGULATOR_MODE_FAST:
81 ret = regmap_update_bits(pchip->regmap,
82 rdev->desc->enable_reg,
84 case REGULATOR_MODE_NORMAL:
85 /* automatic pwm/pfm mode */
86 ret = regmap_update_bits(pchip->regmap,
87 rdev->desc->enable_reg,
96 static unsigned int lp8752_buck_get_mode(struct regulator_dev *rdev)
98 struct lp8752_chip *pchip = rdev_get_drvdata(rdev);
102 ret = regmap_read(pchip->regmap, rdev->desc->enable_reg, ®);
104 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
108 return (reg & BIT(1)) ? REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
111 static struct regulator_ops lp8752_buck_ops = {
112 .map_voltage = regulator_map_voltage_linear_range,
113 .list_voltage = regulator_list_voltage_linear_range,
114 .set_voltage_sel = regulator_set_voltage_sel_regmap,
115 .get_voltage_sel = regulator_get_voltage_sel_regmap,
116 .enable = regulator_enable_regmap,
117 .disable = regulator_disable_regmap,
118 .is_enabled = regulator_is_enabled_regmap,
119 .set_mode = lp8752_buck_set_mode,
120 .get_mode = lp8752_buck_get_mode,
123 #define lp8752_rail(_id) "lp8752_buck"#_id
124 #define vin(_id) "vin"#_id
126 static const struct lp8752_mphase mphase_buck[] = {
127 { 1, { LP8752_BUCK0} },
128 { 2, { LP8752_BUCK0, LP8752_BUCK3 } },
129 { 3, { LP8752_BUCK0, LP8752_BUCK2, LP8752_BUCK3 } },
130 { 4, { LP8752_BUCK0, LP8752_BUCK1, LP8752_BUCK2, LP8752_BUCK3 } },
133 static struct of_regulator_match lp8752_reg_matches[] = {
134 { .name = "lp8752_buck0", .driver_data = (void *)0 },
135 { .name = "lp8752_buck1", .driver_data = (void *)1 },
136 { .name = "lp8752_buck2", .driver_data = (void *)2 },
137 { .name = "lp8752_buck3", .driver_data = (void *)3 },
140 static int lp8752_init_data(struct lp8752_chip *pchip)
142 int icnt, buck_id, count;
143 struct lp8752_platform_data *pdata = pchip->pdata;
144 struct device_node *regs, *lp8752_np;
146 lp8752_np = of_node_get(pchip->dev->of_node);
148 dev_err(pchip->dev, "Failed to find device node\n");
152 regs = of_find_node_by_name(lp8752_np, "regulators");
156 count = of_regulator_match(pchip->dev, regs, lp8752_reg_matches,
159 if ((count <= 0) || (count > LP8752_BUCK_MAX))
162 pchip->nphase = (count - 1) & 0xf;
164 /* set default data based on multi-phase config */
165 for (icnt = 0; icnt < mphase_buck[pchip->nphase].nreg; icnt++) {
166 buck_id = mphase_buck[pchip->nphase].buck_id[icnt];
167 pdata->buck_data[buck_id] =
168 lp8752_reg_matches[buck_id].init_data;
169 pdata->of_node[buck_id] = lp8752_reg_matches[buck_id].of_node;
175 #define lp8752_buck_desc(_id)\
177 .name = lp8752_rail(_id),\
178 .id = LP8752_BUCK##_id,\
179 .supply_name = vin(_id),\
180 .ops = &lp8752_buck_ops,\
181 .n_voltages = LP8752_BUCK_VSEL_MASK + 1,\
182 .linear_ranges = lp8752_buck_voltage_ranges,\
183 .n_linear_ranges = ARRAY_SIZE(lp8752_buck_voltage_ranges),\
184 .type = REGULATOR_VOLTAGE,\
185 .enable_reg = LP8752_CTRL_BUCK##_id,\
186 .enable_mask = BIT(7),\
187 .vsel_reg = LP8752_VOUT_BUCK##_id,\
188 .vsel_mask = LP8752_BUCK_VSEL_MASK,\
190 .owner = THIS_MODULE,\
193 static struct regulator_desc lp8752_reg_desc[] = {
200 static int lp8752_regulator_init(struct lp8752_chip *pchip)
202 int ret, icnt, buck_id;
203 struct lp8752_platform_data *pdata = pchip->pdata;
204 struct regulator_config rconfig = { };
206 rconfig.regmap = pchip->regmap;
207 rconfig.dev = pchip->dev;
208 rconfig.driver_data = pchip;
210 for (icnt = 0; icnt < mphase_buck[pchip->nphase].nreg; icnt++) {
211 buck_id = mphase_buck[pchip->nphase].buck_id[icnt];
212 rconfig.init_data = pdata->buck_data[buck_id];
213 rconfig.of_node = pdata->of_node[buck_id];
214 pchip->rdev[buck_id] =
215 devm_regulator_register(pchip->dev,
216 &lp8752_reg_desc[buck_id],
218 if (IS_ERR(pchip->rdev[buck_id])) {
219 ret = PTR_ERR(pchip->rdev[buck_id]);
220 pchip->rdev[buck_id] = NULL;
221 dev_err(pchip->dev, "regulator init failed: buck %d\n",
230 static const struct regmap_config lp8752_regmap = {
233 .max_register = LP8752_REG_MAX,
236 static int lp8752_probe(struct i2c_client *client,
237 const struct i2c_device_id *id)
240 struct lp8752_chip *pchip;
241 struct lp8752_platform_data *pdata = dev_get_platdata(&client->dev);
243 pchip = devm_kzalloc(&client->dev,
244 sizeof(struct lp8752_chip), GFP_KERNEL);
248 pchip->dev = &client->dev;
249 pchip->regmap = devm_regmap_init_i2c(client, &lp8752_regmap);
250 if (IS_ERR(pchip->regmap)) {
251 ret = PTR_ERR(pchip->regmap);
252 dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
255 i2c_set_clientdata(client, pchip);
257 ret = regmap_update_bits(pchip->regmap,
260 ret = regmap_update_bits(pchip->regmap,
265 pchip->pdata = devm_kzalloc(pchip->dev,
266 sizeof(struct lp8752_platform_data),
271 ret = lp8752_init_data(pchip);
273 dev_err(&client->dev, "fail to initialize chip\n");
277 pchip->pdata = pdata;
278 pchip->nphase = pdata->nphase;
281 lp8752_regulator_init(pchip);
286 static const struct of_device_id lp8752_of_match[] = {
287 { .compatible = "ti,lp8752", },
290 MODULE_DEVICE_TABLE(of, lp8752_of_match);
292 static const struct i2c_device_id lp8752_id[] = {
296 MODULE_DEVICE_TABLE(i2c, lp8752_id);
298 static struct i2c_driver lp8752_i2c_driver = {
299 .probe = lp8752_probe,
300 .id_table = lp8752_id,
303 .of_match_table = of_match_ptr(lp8752_of_match),
307 module_i2c_driver(lp8752_i2c_driver);
309 MODULE_DESCRIPTION("Texas Instruments lp8752 driver");
310 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
311 MODULE_LICENSE("GPL v2");