1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 2018 Samsung Electronics
4  *  Jaehoon Chung <jh80.chung@samsung.com>
5  */
6 
7 #include <common.h>
8 #include <fdtdec.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <i2c.h>
12 #include <power/pmic.h>
13 #include <power/regulator.h>
14 #include <power/s2mps11.h>
15 
16 #define MODE(_id, _val, _name) { \
17 	.id = _id, \
18 	.register_value = _val, \
19 	.name = _name, \
20 }
21 
22 /* BUCK : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 */
23 static struct dm_regulator_mode s2mps11_buck_modes[] = {
24 	MODE(OP_OFF, S2MPS11_BUCK_MODE_OFF, "OFF"),
25 	MODE(OP_STANDBY, S2MPS11_BUCK_MODE_STANDBY, "ON/OFF"),
26 	MODE(OP_ON, S2MPS11_BUCK_MODE_STANDBY, "ON"),
27 };
28 
29 static struct dm_regulator_mode s2mps11_ldo_modes[] = {
30 	MODE(OP_OFF, S2MPS11_LDO_MODE_OFF, "OFF"),
31 	MODE(OP_STANDBY, S2MPS11_LDO_MODE_STANDBY, "ON/OFF"),
32 	MODE(OP_STANDBY_LPM, S2MPS11_LDO_MODE_STANDBY_LPM, "ON/LPM"),
33 	MODE(OP_ON, S2MPS11_LDO_MODE_ON, "ON"),
34 };
35 
36 static const char s2mps11_buck_ctrl[] = {
37 	0xff, 0x25, 0x27, 0x29, 0x2b, 0x2d, 0x33, 0x35, 0x37, 0x39, 0x3b
38 };
39 
40 static const char s2mps11_buck_out[] = {
41 	0xff, 0x26, 0x28, 0x2a, 0x2c, 0x2f, 0x34, 0x36, 0x38, 0x3a, 0x3c
42 };
43 
44 static int s2mps11_buck_hex2volt(int buck, int hex)
45 {
46 	unsigned int uV = 0;
47 
48 	if (hex < 0)
49 		goto bad;
50 
51 	switch (buck) {
52 	case 7:
53 	case 8:
54 	case 10:
55 		if (hex > S2MPS11_BUCK7_8_10_VOLT_MAX_HEX)
56 			goto bad;
57 
58 		uV = hex * S2MPS11_BUCK_HSTEP + S2MPS11_BUCK_UV_HMIN;
59 		break;
60 	case 9:
61 		if (hex > S2MPS11_BUCK9_VOLT_MAX_HEX)
62 			goto bad;
63 		uV = hex * S2MPS11_BUCK9_STEP * 2 + S2MPS11_BUCK9_UV_MIN;
64 		break;
65 	default:
66 		if (buck == 5 && hex > S2MPS11_BUCK5_VOLT_MAX_HEX)
67 			goto bad;
68 		else if (buck != 5 && hex > S2MPS11_BUCK_VOLT_MAX_HEX)
69 			goto bad;
70 
71 		uV = hex * S2MPS11_BUCK_LSTEP + S2MPS11_BUCK_UV_MIN;
72 		break;
73 	}
74 
75 	return uV;
76 bad:
77 	pr_err("Value: %#x is wrong for BUCK%d", hex, buck);
78 	return -EINVAL;
79 }
80 
81 static int s2mps11_buck_volt2hex(int buck, int uV)
82 {
83 	int hex;
84 
85 	switch (buck) {
86 	case 7:
87 	case 8:
88 	case 10:
89 		hex = (uV - S2MPS11_BUCK_UV_HMIN) / S2MPS11_BUCK_HSTEP;
90 		if (hex > S2MPS11_BUCK7_8_10_VOLT_MAX_HEX)
91 			goto bad;
92 
93 		break;
94 	case 9:
95 		hex = (uV - S2MPS11_BUCK9_UV_MIN) / S2MPS11_BUCK9_STEP;
96 		if (hex > S2MPS11_BUCK9_VOLT_MAX_HEX)
97 			goto bad;
98 		break;
99 	default:
100 		hex = (uV - S2MPS11_BUCK_UV_MIN) / S2MPS11_BUCK_LSTEP;
101 		if (buck == 5 && hex > S2MPS11_BUCK5_VOLT_MAX_HEX)
102 			goto bad;
103 		else if (buck != 5 && hex > S2MPS11_BUCK_VOLT_MAX_HEX)
104 			goto bad;
105 		break;
106 	};
107 
108 	if (hex >= 0)
109 		return hex;
110 
111 bad:
112 	pr_err("Value: %d uV is wrong for BUCK%d", uV, buck);
113 	return -EINVAL;
114 }
115 
116 static int s2mps11_buck_val(struct udevice *dev, int op, int *uV)
117 {
118 	int hex, buck, ret;
119 	u32 mask, addr;
120 	u8 val;
121 
122 	buck = dev->driver_data;
123 	if (buck < 1 || buck > S2MPS11_BUCK_NUM) {
124 		pr_err("Wrong buck number: %d\n", buck);
125 		return -EINVAL;
126 	}
127 
128 	if (op == PMIC_OP_GET)
129 		*uV = 0;
130 
131 	addr = s2mps11_buck_out[buck];
132 
133 	switch (buck) {
134 	case 9:
135 		mask = S2MPS11_BUCK9_VOLT_MASK;
136 		break;
137 	default:
138 		mask = S2MPS11_BUCK_VOLT_MASK;
139 		break;
140 	}
141 
142 	ret = pmic_read(dev->parent, addr, &val, 1);
143 	if (ret)
144 		return ret;
145 
146 	if (op == PMIC_OP_GET) {
147 		val &= mask;
148 		ret = s2mps11_buck_hex2volt(buck, val);
149 		if (ret < 0)
150 			return ret;
151 		*uV = ret;
152 		return 0;
153 	}
154 
155 	hex = s2mps11_buck_volt2hex(buck, *uV);
156 	if (hex < 0)
157 		return hex;
158 
159 	val &= ~mask;
160 	val |= hex;
161 	ret = pmic_write(dev->parent, addr, &val, 1);
162 
163 	return ret;
164 }
165 
166 static int s2mps11_buck_mode(struct udevice *dev, int op, int *opmode)
167 {
168 	unsigned int addr, mode;
169 	unsigned char val;
170 	int buck, ret;
171 
172 	buck = dev->driver_data;
173 	if (buck < 1 || buck > S2MPS11_BUCK_NUM) {
174 		pr_err("Wrong buck number: %d\n", buck);
175 		return -EINVAL;
176 	}
177 
178 	addr = s2mps11_buck_ctrl[buck];
179 
180 	ret = pmic_read(dev->parent, addr, &val, 1);
181 	if (ret)
182 		return ret;
183 
184 	if (op == PMIC_OP_GET) {
185 		val &= (S2MPS11_BUCK_MODE_MASK << S2MPS11_BUCK_MODE_SHIFT);
186 		switch (val) {
187 		case S2MPS11_BUCK_MODE_OFF:
188 			*opmode = OP_OFF;
189 			break;
190 		case S2MPS11_BUCK_MODE_STANDBY:
191 			*opmode = OP_STANDBY;
192 			break;
193 		case S2MPS11_BUCK_MODE_ON:
194 			*opmode = OP_ON;
195 			break;
196 		default:
197 			return -EINVAL;
198 		}
199 		return 0;
200 	}
201 
202 	switch (*opmode) {
203 	case OP_OFF:
204 		mode = S2MPS11_BUCK_MODE_OFF;
205 		break;
206 	case OP_STANDBY:
207 		mode = S2MPS11_BUCK_MODE_STANDBY;
208 		break;
209 	case OP_ON:
210 		mode = S2MPS11_BUCK_MODE_ON;
211 		break;
212 	default:
213 		pr_err("Wrong mode: %d for buck: %d\n", *opmode, buck);
214 		return -EINVAL;
215 	}
216 
217 	val &= ~(S2MPS11_BUCK_MODE_MASK << S2MPS11_BUCK_MODE_SHIFT);
218 	val |= mode;
219 	ret = pmic_write(dev->parent, addr, &val, 1);
220 
221 	return ret;
222 }
223 
224 static int s2mps11_buck_enable(struct udevice *dev, int op, bool *enable)
225 {
226 	int ret, on_off;
227 
228 	if (op == PMIC_OP_GET) {
229 		ret = s2mps11_buck_mode(dev, op, &on_off);
230 		if (ret)
231 			return ret;
232 		switch (on_off) {
233 		case OP_OFF:
234 			*enable = false;
235 			break;
236 		case OP_ON:
237 			*enable = true;
238 			break;
239 		default:
240 			return -EINVAL;
241 		}
242 	} else if (op == PMIC_OP_SET) {
243 		if (*enable)
244 			on_off = OP_ON;
245 		else
246 			on_off = OP_OFF;
247 
248 		ret = s2mps11_buck_mode(dev, op, &on_off);
249 		if (ret)
250 			return ret;
251 	}
252 
253 	return 0;
254 }
255 
256 static int buck_get_value(struct udevice *dev)
257 {
258 	int uV;
259 	int ret;
260 
261 	ret = s2mps11_buck_val(dev, PMIC_OP_GET, &uV);
262 	if (ret)
263 		return ret;
264 	return uV;
265 }
266 
267 static int buck_set_value(struct udevice *dev, int uV)
268 {
269 	return s2mps11_buck_val(dev, PMIC_OP_SET, &uV);
270 }
271 
272 static int buck_get_enable(struct udevice *dev)
273 {
274 	bool enable = false;
275 	int ret;
276 
277 	ret = s2mps11_buck_enable(dev, PMIC_OP_GET, &enable);
278 	if (ret)
279 		return ret;
280 	return enable;
281 }
282 
283 static int buck_set_enable(struct udevice *dev, bool enable)
284 {
285 	return s2mps11_buck_enable(dev, PMIC_OP_SET, &enable);
286 }
287 
288 static int buck_get_mode(struct udevice *dev)
289 {
290 	int mode;
291 	int ret;
292 
293 	ret = s2mps11_buck_mode(dev, PMIC_OP_GET, &mode);
294 	if (ret)
295 		return ret;
296 
297 	return mode;
298 }
299 
300 static int buck_set_mode(struct udevice *dev, int mode)
301 {
302 	return s2mps11_buck_mode(dev, PMIC_OP_SET, &mode);
303 }
304 
305 static int s2mps11_buck_probe(struct udevice *dev)
306 {
307 	struct dm_regulator_uclass_platdata *uc_pdata;
308 
309 	uc_pdata = dev_get_uclass_platdata(dev);
310 
311 	uc_pdata->type = REGULATOR_TYPE_BUCK;
312 	uc_pdata->mode = s2mps11_buck_modes;
313 	uc_pdata->mode_count = ARRAY_SIZE(s2mps11_buck_modes);
314 
315 	return 0;
316 }
317 
318 static const struct dm_regulator_ops s2mps11_buck_ops = {
319 	.get_value	= buck_get_value,
320 	.set_value	= buck_set_value,
321 	.get_enable	= buck_get_enable,
322 	.set_enable	= buck_set_enable,
323 	.get_mode	= buck_get_mode,
324 	.set_mode	= buck_set_mode,
325 };
326 
327 U_BOOT_DRIVER(s2mps11_buck) = {
328 	.name = S2MPS11_BUCK_DRIVER,
329 	.id = UCLASS_REGULATOR,
330 	.ops = &s2mps11_buck_ops,
331 	.probe = s2mps11_buck_probe,
332 };
333 
334 static int s2mps11_ldo_hex2volt(int ldo, int hex)
335 {
336 	unsigned int uV = 0;
337 
338 	if (hex > S2MPS11_LDO_VOLT_MAX_HEX) {
339 		pr_err("Value: %#x is wrong for LDO%d", hex, ldo);
340 		return -EINVAL;
341 	}
342 
343 	switch (ldo) {
344 	case 1:
345 	case 6:
346 	case 11:
347 	case 22:
348 	case 23:
349 	case 27:
350 	case 35:
351 		uV = hex * S2MPS11_LDO_STEP + S2MPS11_LDO_UV_MIN;
352 		break;
353 	default:
354 		uV = hex * S2MPS11_LDO_STEP * 2 + S2MPS11_LDO_UV_MIN;
355 		break;
356 	}
357 
358 	return uV;
359 }
360 
361 static int s2mps11_ldo_volt2hex(int ldo, int uV)
362 {
363 	int hex = 0;
364 
365 	switch (ldo) {
366 	case 1:
367 	case 6:
368 	case 11:
369 	case 22:
370 	case 23:
371 	case 27:
372 	case 35:
373 		hex = (uV - S2MPS11_LDO_UV_MIN) / S2MPS11_LDO_STEP;
374 		break;
375 	default:
376 		hex = (uV - S2MPS11_LDO_UV_MIN) / (S2MPS11_LDO_STEP * 2);
377 		break;
378 	}
379 
380 	if (hex >= 0 && hex <= S2MPS11_LDO_VOLT_MAX_HEX)
381 		return hex;
382 
383 	pr_err("Value: %d uV is wrong for LDO%d", uV, ldo);
384 	return -EINVAL;
385 
386 	return 0;
387 }
388 
389 static int s2mps11_ldo_val(struct udevice *dev, int op, int *uV)
390 {
391 	unsigned int addr;
392 	unsigned char val;
393 	int hex, ldo, ret;
394 
395 	ldo = dev->driver_data;
396 	if (ldo < 1 || ldo > S2MPS11_LDO_NUM) {
397 		pr_err("Wrong ldo number: %d\n", ldo);
398 		return -EINVAL;
399 	}
400 
401 	addr = S2MPS11_REG_L1CTRL + ldo - 1;
402 
403 	ret = pmic_read(dev->parent, addr, &val, 1);
404 	if (ret)
405 		return ret;
406 
407 	if (op == PMIC_OP_GET) {
408 		*uV = 0;
409 		val &= S2MPS11_LDO_VOLT_MASK;
410 		ret = s2mps11_ldo_hex2volt(ldo, val);
411 		if (ret < 0)
412 			return ret;
413 
414 		*uV = ret;
415 		return 0;
416 	}
417 
418 	hex = s2mps11_ldo_volt2hex(ldo, *uV);
419 	if (hex < 0)
420 		return hex;
421 
422 	val &= ~S2MPS11_LDO_VOLT_MASK;
423 	val |= hex;
424 	ret = pmic_write(dev->parent, addr, &val, 1);
425 
426 	return ret;
427 }
428 
429 static int s2mps11_ldo_mode(struct udevice *dev, int op, int *opmode)
430 {
431 	unsigned int addr, mode;
432 	unsigned char val;
433 	int ldo, ret;
434 
435 	ldo = dev->driver_data;
436 	if (ldo < 1 || ldo > S2MPS11_LDO_NUM) {
437 		pr_err("Wrong ldo number: %d\n", ldo);
438 		return -EINVAL;
439 	}
440 	addr = S2MPS11_REG_L1CTRL + ldo - 1;
441 
442 	ret = pmic_read(dev->parent, addr, &val, 1);
443 	if (ret)
444 		return ret;
445 
446 	if (op == PMIC_OP_GET) {
447 		val &= (S2MPS11_LDO_MODE_MASK << S2MPS11_LDO_MODE_SHIFT);
448 		switch (val) {
449 		case S2MPS11_LDO_MODE_OFF:
450 			*opmode = OP_OFF;
451 			break;
452 		case S2MPS11_LDO_MODE_STANDBY:
453 			*opmode = OP_STANDBY;
454 			break;
455 		case S2MPS11_LDO_MODE_STANDBY_LPM:
456 			*opmode = OP_STANDBY_LPM;
457 			break;
458 		case S2MPS11_LDO_MODE_ON:
459 			*opmode = OP_ON;
460 			break;
461 		default:
462 			return -EINVAL;
463 		}
464 		return 0;
465 	}
466 
467 	switch (*opmode) {
468 	case OP_OFF:
469 		mode = S2MPS11_LDO_MODE_OFF;
470 		break;
471 	case OP_STANDBY:
472 		mode = S2MPS11_LDO_MODE_STANDBY;
473 		break;
474 	case OP_STANDBY_LPM:
475 		mode = S2MPS11_LDO_MODE_STANDBY_LPM;
476 		break;
477 	case OP_ON:
478 		mode = S2MPS11_LDO_MODE_ON;
479 		break;
480 	default:
481 		pr_err("Wrong mode: %d for ldo: %d\n", *opmode, ldo);
482 		return -EINVAL;
483 	}
484 
485 	val &= ~(S2MPS11_LDO_MODE_MASK << S2MPS11_LDO_MODE_SHIFT);
486 	val |= mode;
487 	ret = pmic_write(dev->parent, addr, &val, 1);
488 
489 	return ret;
490 }
491 
492 static int s2mps11_ldo_enable(struct udevice *dev, int op, bool *enable)
493 {
494 	int ret, on_off;
495 
496 	if (op == PMIC_OP_GET) {
497 		ret = s2mps11_ldo_mode(dev, op, &on_off);
498 		if (ret)
499 			return ret;
500 		switch (on_off) {
501 		case OP_OFF:
502 			*enable = false;
503 			break;
504 		case OP_ON:
505 			*enable = true;
506 			break;
507 		default:
508 			return -EINVAL;
509 		}
510 	} else if (op == PMIC_OP_SET) {
511 		if (*enable)
512 			on_off = OP_ON;
513 		else
514 			on_off = OP_OFF;
515 
516 		ret = s2mps11_ldo_mode(dev, op, &on_off);
517 		if (ret)
518 			return ret;
519 	}
520 
521 	return 0;
522 }
523 
524 static int ldo_get_value(struct udevice *dev)
525 {
526 	int uV;
527 	int ret;
528 
529 	ret = s2mps11_ldo_val(dev, PMIC_OP_GET, &uV);
530 	if (ret)
531 		return ret;
532 
533 	return uV;
534 }
535 
536 static int ldo_set_value(struct udevice *dev, int uV)
537 {
538 	return s2mps11_ldo_val(dev, PMIC_OP_SET, &uV);
539 }
540 
541 static int ldo_get_enable(struct udevice *dev)
542 {
543 	bool enable = false;
544 	int ret;
545 
546 	ret = s2mps11_ldo_enable(dev, PMIC_OP_GET, &enable);
547 	if (ret)
548 		return ret;
549 	return enable;
550 }
551 
552 static int ldo_set_enable(struct udevice *dev, bool enable)
553 {
554 	int ret;
555 
556 	ret = s2mps11_ldo_enable(dev, PMIC_OP_SET, &enable);
557 	if (ret)
558 		return ret;
559 
560 	/* Wait the "enable delay" for voltage to start to rise */
561 	udelay(15);
562 
563 	return 0;
564 }
565 
566 static int ldo_get_mode(struct udevice *dev)
567 {
568 	int mode, ret;
569 
570 	ret = s2mps11_ldo_mode(dev, PMIC_OP_GET, &mode);
571 	if (ret)
572 		return ret;
573 	return mode;
574 }
575 
576 static int ldo_set_mode(struct udevice *dev, int mode)
577 {
578 	return s2mps11_ldo_mode(dev, PMIC_OP_SET, &mode);
579 }
580 
581 static int s2mps11_ldo_probe(struct udevice *dev)
582 {
583 	struct dm_regulator_uclass_platdata *uc_pdata;
584 
585 	uc_pdata = dev_get_uclass_platdata(dev);
586 	uc_pdata->type = REGULATOR_TYPE_LDO;
587 	uc_pdata->mode = s2mps11_ldo_modes;
588 	uc_pdata->mode_count = ARRAY_SIZE(s2mps11_ldo_modes);
589 
590 	return 0;
591 }
592 
593 static const struct dm_regulator_ops s2mps11_ldo_ops = {
594 	.get_value	= ldo_get_value,
595 	.set_value	= ldo_set_value,
596 	.get_enable	= ldo_get_enable,
597 	.set_enable	= ldo_set_enable,
598 	.get_mode	= ldo_get_mode,
599 	.set_mode	= ldo_set_mode,
600 };
601 
602 U_BOOT_DRIVER(s2mps11_ldo) = {
603 	.name = S2MPS11_LDO_DRIVER,
604 	.id = UCLASS_REGULATOR,
605 	.ops = &s2mps11_ldo_ops,
606 	.probe = s2mps11_ldo_probe,
607 };
608