1 // SPDX-License-Identifier: GPL-2.0-only
3 * Regulator driver for National Semiconductors LP3971 PMIC chip
5 * Copyright (C) 2009 Samsung Electronics
6 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
11 #include <linux/bug.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/regulator/lp3971.h>
18 #include <linux/slab.h>
23 struct i2c_client *i2c;
26 static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg);
27 static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val);
29 #define LP3971_SYS_CONTROL1_REG 0x07
31 /* System control register 1 initial value,
32 bits 4 and 5 are EPROM programmable */
33 #define SYS_CONTROL1_INIT_VAL 0x40
34 #define SYS_CONTROL1_INIT_MASK 0xCF
36 #define LP3971_BUCK_VOL_ENABLE_REG 0x10
37 #define LP3971_BUCK_VOL_CHANGE_REG 0x20
39 /* Voltage control registers shift:
44 #define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01))
45 #define BUCK_VOL_CHANGE_FLAG_GO 0x01
46 #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
47 #define BUCK_VOL_CHANGE_FLAG_MASK 0x03
49 #define LP3971_BUCK1_BASE 0x23
50 #define LP3971_BUCK2_BASE 0x29
51 #define LP3971_BUCK3_BASE 0x32
53 static const int buck_base_addr[] = {
59 #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
60 #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
62 static const unsigned int buck_voltage_map[] = {
63 0, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
64 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
65 1550000, 1600000, 1650000, 1700000, 1800000, 1900000, 2500000, 2800000,
69 #define BUCK_TARGET_VOL_MASK 0x3f
71 #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
73 #define LP3971_LDO_ENABLE_REG 0x12
74 #define LP3971_LDO_VOL_CONTR_BASE 0x39
76 /* Voltage control registers:
77 LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
78 LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
79 LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
80 LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
81 LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
83 #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
85 /* Voltage control registers shift:
86 LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
87 LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
90 #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
91 #define LDO_VOL_CONTR_MASK 0x0f
93 static const unsigned int ldo45_voltage_map[] = {
94 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000,
95 1400000, 1500000, 1800000, 1900000, 2500000, 2800000, 3000000, 3300000,
98 static const unsigned int ldo123_voltage_map[] = {
99 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
100 2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
103 #define LDO_VOL_MIN_IDX 0x00
104 #define LDO_VOL_MAX_IDX 0x0f
106 static int lp3971_ldo_is_enabled(struct regulator_dev *dev)
108 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
109 int ldo = rdev_get_id(dev) - LP3971_LDO1;
110 u16 mask = 1 << (1 + ldo);
113 val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG);
114 return (val & mask) != 0;
117 static int lp3971_ldo_enable(struct regulator_dev *dev)
119 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
120 int ldo = rdev_get_id(dev) - LP3971_LDO1;
121 u16 mask = 1 << (1 + ldo);
123 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask);
126 static int lp3971_ldo_disable(struct regulator_dev *dev)
128 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
129 int ldo = rdev_get_id(dev) - LP3971_LDO1;
130 u16 mask = 1 << (1 + ldo);
132 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0);
135 static int lp3971_ldo_get_voltage_sel(struct regulator_dev *dev)
137 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
138 int ldo = rdev_get_id(dev) - LP3971_LDO1;
141 reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo));
142 val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK;
147 static int lp3971_ldo_set_voltage_sel(struct regulator_dev *dev,
148 unsigned int selector)
150 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
151 int ldo = rdev_get_id(dev) - LP3971_LDO1;
153 return lp3971_set_bits(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo),
154 LDO_VOL_CONTR_MASK << LDO_VOL_CONTR_SHIFT(ldo),
155 selector << LDO_VOL_CONTR_SHIFT(ldo));
158 static const struct regulator_ops lp3971_ldo_ops = {
159 .list_voltage = regulator_list_voltage_table,
160 .map_voltage = regulator_map_voltage_ascend,
161 .is_enabled = lp3971_ldo_is_enabled,
162 .enable = lp3971_ldo_enable,
163 .disable = lp3971_ldo_disable,
164 .get_voltage_sel = lp3971_ldo_get_voltage_sel,
165 .set_voltage_sel = lp3971_ldo_set_voltage_sel,
168 static int lp3971_dcdc_is_enabled(struct regulator_dev *dev)
170 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
171 int buck = rdev_get_id(dev) - LP3971_DCDC1;
172 u16 mask = 1 << (buck * 2);
175 val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG);
176 return (val & mask) != 0;
179 static int lp3971_dcdc_enable(struct regulator_dev *dev)
181 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
182 int buck = rdev_get_id(dev) - LP3971_DCDC1;
183 u16 mask = 1 << (buck * 2);
185 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask);
188 static int lp3971_dcdc_disable(struct regulator_dev *dev)
190 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
191 int buck = rdev_get_id(dev) - LP3971_DCDC1;
192 u16 mask = 1 << (buck * 2);
194 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0);
197 static int lp3971_dcdc_get_voltage_sel(struct regulator_dev *dev)
199 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
200 int buck = rdev_get_id(dev) - LP3971_DCDC1;
203 reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck));
204 reg &= BUCK_TARGET_VOL_MASK;
209 static int lp3971_dcdc_set_voltage_sel(struct regulator_dev *dev,
210 unsigned int selector)
212 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
213 int buck = rdev_get_id(dev) - LP3971_DCDC1;
216 ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck),
217 BUCK_TARGET_VOL_MASK, selector);
221 ret = lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
222 BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
223 BUCK_VOL_CHANGE_FLAG_GO << BUCK_VOL_CHANGE_SHIFT(buck));
227 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
228 BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
229 0 << BUCK_VOL_CHANGE_SHIFT(buck));
232 static const struct regulator_ops lp3971_dcdc_ops = {
233 .list_voltage = regulator_list_voltage_table,
234 .map_voltage = regulator_map_voltage_ascend,
235 .is_enabled = lp3971_dcdc_is_enabled,
236 .enable = lp3971_dcdc_enable,
237 .disable = lp3971_dcdc_disable,
238 .get_voltage_sel = lp3971_dcdc_get_voltage_sel,
239 .set_voltage_sel = lp3971_dcdc_set_voltage_sel,
242 static const struct regulator_desc regulators[] = {
246 .ops = &lp3971_ldo_ops,
247 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
248 .volt_table = ldo123_voltage_map,
249 .type = REGULATOR_VOLTAGE,
250 .owner = THIS_MODULE,
255 .ops = &lp3971_ldo_ops,
256 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
257 .volt_table = ldo123_voltage_map,
258 .type = REGULATOR_VOLTAGE,
259 .owner = THIS_MODULE,
264 .ops = &lp3971_ldo_ops,
265 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
266 .volt_table = ldo123_voltage_map,
267 .type = REGULATOR_VOLTAGE,
268 .owner = THIS_MODULE,
273 .ops = &lp3971_ldo_ops,
274 .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
275 .volt_table = ldo45_voltage_map,
276 .type = REGULATOR_VOLTAGE,
277 .owner = THIS_MODULE,
282 .ops = &lp3971_ldo_ops,
283 .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
284 .volt_table = ldo45_voltage_map,
285 .type = REGULATOR_VOLTAGE,
286 .owner = THIS_MODULE,
291 .ops = &lp3971_dcdc_ops,
292 .n_voltages = ARRAY_SIZE(buck_voltage_map),
293 .volt_table = buck_voltage_map,
294 .type = REGULATOR_VOLTAGE,
295 .owner = THIS_MODULE,
300 .ops = &lp3971_dcdc_ops,
301 .n_voltages = ARRAY_SIZE(buck_voltage_map),
302 .volt_table = buck_voltage_map,
303 .type = REGULATOR_VOLTAGE,
304 .owner = THIS_MODULE,
309 .ops = &lp3971_dcdc_ops,
310 .n_voltages = ARRAY_SIZE(buck_voltage_map),
311 .volt_table = buck_voltage_map,
312 .type = REGULATOR_VOLTAGE,
313 .owner = THIS_MODULE,
317 static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count,
324 ret = i2c_smbus_read_byte_data(i2c, reg);
332 static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count,
337 return i2c_smbus_write_byte_data(i2c, reg, *src);
340 static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg)
344 mutex_lock(&lp3971->io_lock);
346 lp3971_i2c_read(lp3971->i2c, reg, 1, &val);
348 dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
351 mutex_unlock(&lp3971->io_lock);
356 static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val)
361 mutex_lock(&lp3971->io_lock);
363 ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp);
365 tmp = (tmp & ~mask) | val;
366 ret = lp3971_i2c_write(lp3971->i2c, reg, 1, &tmp);
367 dev_dbg(lp3971->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
370 mutex_unlock(&lp3971->io_lock);
375 static int setup_regulators(struct lp3971 *lp3971,
376 struct lp3971_platform_data *pdata)
380 /* Instantiate the regulators */
381 for (i = 0; i < pdata->num_regulators; i++) {
382 struct regulator_config config = { };
383 struct lp3971_regulator_subdev *reg = &pdata->regulators[i];
384 struct regulator_dev *rdev;
386 config.dev = lp3971->dev;
387 config.init_data = reg->initdata;
388 config.driver_data = lp3971;
390 rdev = devm_regulator_register(lp3971->dev,
391 ®ulators[reg->id], &config);
394 dev_err(lp3971->dev, "regulator init failed: %d\n",
403 static int lp3971_i2c_probe(struct i2c_client *i2c)
405 struct lp3971 *lp3971;
406 struct lp3971_platform_data *pdata = dev_get_platdata(&i2c->dev);
411 dev_dbg(&i2c->dev, "No platform init data supplied\n");
415 lp3971 = devm_kzalloc(&i2c->dev, sizeof(struct lp3971), GFP_KERNEL);
420 lp3971->dev = &i2c->dev;
422 mutex_init(&lp3971->io_lock);
425 ret = lp3971_i2c_read(i2c, LP3971_SYS_CONTROL1_REG, 1, &val);
426 if (ret == 0 && (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL)
429 dev_err(&i2c->dev, "failed to detect device\n");
433 ret = setup_regulators(lp3971, pdata);
437 i2c_set_clientdata(i2c, lp3971);
441 static const struct i2c_device_id lp3971_i2c_id[] = {
445 MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id);
447 static struct i2c_driver lp3971_i2c_driver = {
450 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
452 .probe = lp3971_i2c_probe,
453 .id_table = lp3971_i2c_id,
456 module_i2c_driver(lp3971_i2c_driver);
458 MODULE_LICENSE("GPL");
459 MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
460 MODULE_DESCRIPTION("LP3971 PMIC driver");