Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / regulator / lp3971.c
blob8be252f81b091c88282123d37c13a52a5007927e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Regulator driver for National Semiconductors LP3971 PMIC chip
5 * Copyright (C) 2009 Samsung Electronics
6 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
8 * Based on wm8350.c
9 */
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>
20 struct lp3971 {
21 struct device *dev;
22 struct mutex io_lock;
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:
40 LP3971_BUCK1 -> 0
41 LP3971_BUCK2 -> 4
42 LP3971_BUCK3 -> 6
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[] = {
54 LP3971_BUCK1_BASE,
55 LP3971_BUCK2_BASE,
56 LP3971_BUCK3_BASE,
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,
66 3000000, 3300000,
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
88 LP3971_LDO5 -> 0
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);
111 u16 val;
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;
139 u16 val, reg;
141 reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo));
142 val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK;
144 return val;
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);
173 u16 val;
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;
201 u16 reg;
203 reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck));
204 reg &= BUCK_TARGET_VOL_MASK;
206 return reg;
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;
214 int ret;
216 ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck),
217 BUCK_TARGET_VOL_MASK, selector);
218 if (ret)
219 return ret;
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));
224 if (ret)
225 return ret;
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[] = {
244 .name = "LDO1",
245 .id = LP3971_LDO1,
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,
253 .name = "LDO2",
254 .id = LP3971_LDO2,
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,
262 .name = "LDO3",
263 .id = LP3971_LDO3,
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,
271 .name = "LDO4",
272 .id = LP3971_LDO4,
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,
280 .name = "LDO5",
281 .id = LP3971_LDO5,
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,
289 .name = "DCDC1",
290 .id = LP3971_DCDC1,
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,
298 .name = "DCDC2",
299 .id = LP3971_DCDC2,
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,
307 .name = "DCDC3",
308 .id = LP3971_DCDC3,
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,
318 u16 *dest)
320 int ret;
322 if (count != 1)
323 return -EIO;
324 ret = i2c_smbus_read_byte_data(i2c, reg);
325 if (ret < 0)
326 return ret;
328 *dest = ret;
329 return 0;
332 static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count,
333 const u16 *src)
335 if (count != 1)
336 return -EIO;
337 return i2c_smbus_write_byte_data(i2c, reg, *src);
340 static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg)
342 u16 val = 0;
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,
349 (unsigned)val&0xff);
351 mutex_unlock(&lp3971->io_lock);
353 return val & 0xff;
356 static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val)
358 u16 tmp;
359 int ret;
361 mutex_lock(&lp3971->io_lock);
363 ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp);
364 if (ret == 0) {
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,
368 (unsigned)val&0xff);
370 mutex_unlock(&lp3971->io_lock);
372 return ret;
375 static int setup_regulators(struct lp3971 *lp3971,
376 struct lp3971_platform_data *pdata)
378 int i, err;
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 &regulators[reg->id], &config);
392 if (IS_ERR(rdev)) {
393 err = PTR_ERR(rdev);
394 dev_err(lp3971->dev, "regulator init failed: %d\n",
395 err);
396 return err;
400 return 0;
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);
407 int ret;
408 u16 val;
410 if (!pdata) {
411 dev_dbg(&i2c->dev, "No platform init data supplied\n");
412 return -ENODEV;
415 lp3971 = devm_kzalloc(&i2c->dev, sizeof(struct lp3971), GFP_KERNEL);
416 if (lp3971 == NULL)
417 return -ENOMEM;
419 lp3971->i2c = i2c;
420 lp3971->dev = &i2c->dev;
422 mutex_init(&lp3971->io_lock);
424 /* Detect LP3971 */
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)
427 ret = -ENODEV;
428 if (ret < 0) {
429 dev_err(&i2c->dev, "failed to detect device\n");
430 return ret;
433 ret = setup_regulators(lp3971, pdata);
434 if (ret < 0)
435 return ret;
437 i2c_set_clientdata(i2c, lp3971);
438 return 0;
441 static const struct i2c_device_id lp3971_i2c_id[] = {
442 { "lp3971", 0 },
445 MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id);
447 static struct i2c_driver lp3971_i2c_driver = {
448 .driver = {
449 .name = "LP3971",
451 .probe_new = lp3971_i2c_probe,
452 .id_table = lp3971_i2c_id,
455 module_i2c_driver(lp3971_i2c_driver);
457 MODULE_LICENSE("GPL");
458 MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
459 MODULE_DESCRIPTION("LP3971 PMIC driver");