1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Regulator driver for National Semiconductors LP3971 PMIC chip
4 *
5 * Copyright (C) 2009 Samsung Electronics
6 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
7 *
8 * Based on wm8350.c
9 */
10
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>
19
20 struct lp3971 {
21 struct device *dev;
22 struct mutex io_lock;
23 struct i2c_client *i2c;
24 };
25
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);
28
29 #define LP3971_SYS_CONTROL1_REG 0x07
30
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
35
36 #define LP3971_BUCK_VOL_ENABLE_REG 0x10
37 #define LP3971_BUCK_VOL_CHANGE_REG 0x20
38
39 /* Voltage control registers shift:
40 LP3971_BUCK1 -> 0
41 LP3971_BUCK2 -> 4
42 LP3971_BUCK3 -> 6
43 */
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
48
49 #define LP3971_BUCK1_BASE 0x23
50 #define LP3971_BUCK2_BASE 0x29
51 #define LP3971_BUCK3_BASE 0x32
52
53 static const int buck_base_addr[] = {
54 LP3971_BUCK1_BASE,
55 LP3971_BUCK2_BASE,
56 LP3971_BUCK3_BASE,
57 };
58
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)
61
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,
67 };
68
69 #define BUCK_TARGET_VOL_MASK 0x3f
70
71 #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
72
73 #define LP3971_LDO_ENABLE_REG 0x12
74 #define LP3971_LDO_VOL_CONTR_BASE 0x39
75
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
82 */
83 #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
84
85 /* Voltage control registers shift:
86 LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
87 LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
88 LP3971_LDO5 -> 0
89 */
90 #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
91 #define LDO_VOL_CONTR_MASK 0x0f
92
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,
96 };
97
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,
101 };
102
103 #define LDO_VOL_MIN_IDX 0x00
104 #define LDO_VOL_MAX_IDX 0x0f
105
lp3971_ldo_is_enabled(struct regulator_dev * dev)106 static int lp3971_ldo_is_enabled(struct regulator_dev *dev)
107 {
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;
112
113 val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG);
114 return (val & mask) != 0;
115 }
116
lp3971_ldo_enable(struct regulator_dev * dev)117 static int lp3971_ldo_enable(struct regulator_dev *dev)
118 {
119 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
120 int ldo = rdev_get_id(dev) - LP3971_LDO1;
121 u16 mask = 1 << (1 + ldo);
122
123 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask);
124 }
125
lp3971_ldo_disable(struct regulator_dev * dev)126 static int lp3971_ldo_disable(struct regulator_dev *dev)
127 {
128 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
129 int ldo = rdev_get_id(dev) - LP3971_LDO1;
130 u16 mask = 1 << (1 + ldo);
131
132 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0);
133 }
134
lp3971_ldo_get_voltage_sel(struct regulator_dev * dev)135 static int lp3971_ldo_get_voltage_sel(struct regulator_dev *dev)
136 {
137 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
138 int ldo = rdev_get_id(dev) - LP3971_LDO1;
139 u16 val, reg;
140
141 reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo));
142 val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK;
143
144 return val;
145 }
146
lp3971_ldo_set_voltage_sel(struct regulator_dev * dev,unsigned int selector)147 static int lp3971_ldo_set_voltage_sel(struct regulator_dev *dev,
148 unsigned int selector)
149 {
150 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
151 int ldo = rdev_get_id(dev) - LP3971_LDO1;
152
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));
156 }
157
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,
166 };
167
lp3971_dcdc_is_enabled(struct regulator_dev * dev)168 static int lp3971_dcdc_is_enabled(struct regulator_dev *dev)
169 {
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;
174
175 val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG);
176 return (val & mask) != 0;
177 }
178
lp3971_dcdc_enable(struct regulator_dev * dev)179 static int lp3971_dcdc_enable(struct regulator_dev *dev)
180 {
181 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
182 int buck = rdev_get_id(dev) - LP3971_DCDC1;
183 u16 mask = 1 << (buck * 2);
184
185 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask);
186 }
187
lp3971_dcdc_disable(struct regulator_dev * dev)188 static int lp3971_dcdc_disable(struct regulator_dev *dev)
189 {
190 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
191 int buck = rdev_get_id(dev) - LP3971_DCDC1;
192 u16 mask = 1 << (buck * 2);
193
194 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0);
195 }
196
lp3971_dcdc_get_voltage_sel(struct regulator_dev * dev)197 static int lp3971_dcdc_get_voltage_sel(struct regulator_dev *dev)
198 {
199 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
200 int buck = rdev_get_id(dev) - LP3971_DCDC1;
201 u16 reg;
202
203 reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck));
204 reg &= BUCK_TARGET_VOL_MASK;
205
206 return reg;
207 }
208
lp3971_dcdc_set_voltage_sel(struct regulator_dev * dev,unsigned int selector)209 static int lp3971_dcdc_set_voltage_sel(struct regulator_dev *dev,
210 unsigned int selector)
211 {
212 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
213 int buck = rdev_get_id(dev) - LP3971_DCDC1;
214 int ret;
215
216 ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck),
217 BUCK_TARGET_VOL_MASK, selector);
218 if (ret)
219 return ret;
220
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;
226
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));
230 }
231
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,
240 };
241
242 static const struct regulator_desc regulators[] = {
243 {
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,
251 },
252 {
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,
260 },
261 {
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,
269 },
270 {
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,
278 },
279 {
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,
287 },
288 {
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,
296 },
297 {
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,
305 },
306 {
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,
314 },
315 };
316
lp3971_i2c_read(struct i2c_client * i2c,char reg,int count,u16 * dest)317 static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count,
318 u16 *dest)
319 {
320 int ret;
321
322 if (count != 1)
323 return -EIO;
324 ret = i2c_smbus_read_byte_data(i2c, reg);
325 if (ret < 0)
326 return ret;
327
328 *dest = ret;
329 return 0;
330 }
331
lp3971_i2c_write(struct i2c_client * i2c,char reg,int count,const u16 * src)332 static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count,
333 const u16 *src)
334 {
335 if (count != 1)
336 return -EIO;
337 return i2c_smbus_write_byte_data(i2c, reg, *src);
338 }
339
lp3971_reg_read(struct lp3971 * lp3971,u8 reg)340 static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg)
341 {
342 u16 val = 0;
343
344 mutex_lock(&lp3971->io_lock);
345
346 lp3971_i2c_read(lp3971->i2c, reg, 1, &val);
347
348 dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
349 (unsigned)val&0xff);
350
351 mutex_unlock(&lp3971->io_lock);
352
353 return val & 0xff;
354 }
355
lp3971_set_bits(struct lp3971 * lp3971,u8 reg,u16 mask,u16 val)356 static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val)
357 {
358 u16 tmp;
359 int ret;
360
361 mutex_lock(&lp3971->io_lock);
362
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);
369 }
370 mutex_unlock(&lp3971->io_lock);
371
372 return ret;
373 }
374
setup_regulators(struct lp3971 * lp3971,struct lp3971_platform_data * pdata)375 static int setup_regulators(struct lp3971 *lp3971,
376 struct lp3971_platform_data *pdata)
377 {
378 int i, err;
379
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;
385
386 config.dev = lp3971->dev;
387 config.init_data = reg->initdata;
388 config.driver_data = lp3971;
389
390 rdev = devm_regulator_register(lp3971->dev,
391 ®ulators[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;
397 }
398 }
399
400 return 0;
401 }
402
lp3971_i2c_probe(struct i2c_client * i2c)403 static int lp3971_i2c_probe(struct i2c_client *i2c)
404 {
405 struct lp3971 *lp3971;
406 struct lp3971_platform_data *pdata = dev_get_platdata(&i2c->dev);
407 int ret;
408 u16 val;
409
410 if (!pdata) {
411 dev_dbg(&i2c->dev, "No platform init data supplied\n");
412 return -ENODEV;
413 }
414
415 lp3971 = devm_kzalloc(&i2c->dev, sizeof(struct lp3971), GFP_KERNEL);
416 if (lp3971 == NULL)
417 return -ENOMEM;
418
419 lp3971->i2c = i2c;
420 lp3971->dev = &i2c->dev;
421
422 mutex_init(&lp3971->io_lock);
423
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;
431 }
432
433 ret = setup_regulators(lp3971, pdata);
434 if (ret < 0)
435 return ret;
436
437 i2c_set_clientdata(i2c, lp3971);
438 return 0;
439 }
440
441 static const struct i2c_device_id lp3971_i2c_id[] = {
442 { "lp3971", 0 },
443 { }
444 };
445 MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id);
446
447 static struct i2c_driver lp3971_i2c_driver = {
448 .driver = {
449 .name = "LP3971",
450 },
451 .probe_new = lp3971_i2c_probe,
452 .id_table = lp3971_i2c_id,
453 };
454
455 module_i2c_driver(lp3971_i2c_driver);
456
457 MODULE_LICENSE("GPL");
458 MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
459 MODULE_DESCRIPTION("LP3971 PMIC driver");
460