summaryrefslogtreecommitdiff
path: root/drivers/power/regulator/palmas_regulator.c
blob: 78909f701894fbcd6c47808ef63e9f6604a29aff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2016
 * Texas Instruments Incorporated, <www.ti.com>
 *
 * Keerthy <j-keerthy@ti.com>
 */

#include <common.h>
#include <fdtdec.h>
#include <errno.h>
#include <dm.h>
#include <i2c.h>
#include <power/pmic.h>
#include <power/regulator.h>
#include <power/palmas.h>

#define	REGULATOR_ON		0x1
#define	REGULATOR_OFF		0x0

#define	SMPS_MODE_MASK		0x3
#define	SMPS_MODE_SHIFT		0x0
#define	LDO_MODE_MASK		0x1
#define	LDO_MODE_SHIFT		0x0

static const char palmas_smps_ctrl[][PALMAS_SMPS_NUM] = {
	{0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c},
	{0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38},
	{0x20, 0x24, 0x2c, 0x30, 0x38},
};

static const char palmas_smps_volt[][PALMAS_SMPS_NUM] = {
	{0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b, 0x3c},
	{0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b},
	{0x23, 0x27, 0x2f, 0x33, 0x3B}
};

static const char palmas_ldo_ctrl[][PALMAS_LDO_NUM] = {
	{0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64},
	{0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64},
	{0x50, 0x52, 0x54, 0x5e, 0x62}
};

static const char palmas_ldo_volt[][PALMAS_LDO_NUM] = {
	{0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65},
	{0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65},
	{0x51, 0x53, 0x55, 0x5f, 0x63}
};

static int palmas_smps_enable(struct udevice *dev, int op, bool *enable)
{
	int ret;
	unsigned int adr;
	struct dm_regulator_uclass_platdata *uc_pdata;

	uc_pdata = dev_get_uclass_platdata(dev);
	adr = uc_pdata->ctrl_reg;

	ret = pmic_reg_read(dev->parent, adr);
		if (ret < 0)
			return ret;

	if (op == PMIC_OP_GET) {
		ret &= PALMAS_SMPS_STATUS_MASK;

		if (ret)
			*enable = true;
		else
			*enable = false;

		return 0;
	} else if (op == PMIC_OP_SET) {
		if (*enable)
			ret |= PALMAS_SMPS_MODE_MASK;
		else
			ret &= ~(PALMAS_SMPS_MODE_MASK);

		ret = pmic_reg_write(dev->parent, adr, ret);
		if (ret)
			return ret;
	}

	return 0;
}

static int palmas_smps_volt2hex(int uV)
{
	if (uV > PALMAS_LDO_VOLT_MAX)
		return -EINVAL;

	if (uV > 1650000)
		return (uV - 1000000) / 20000 + 0x6;

	if (uV == 500000)
		return 0x6;
	else
		return 0x6 + ((uV - 500000) / 10000);
}

static int palmas_smps_hex2volt(int hex, bool range)
{
	unsigned int uV = 0;

	if (hex > PALMAS_SMPS_VOLT_MAX_HEX)
		return -EINVAL;

	if (hex < 0x7)
		uV = 500000;
	else
		uV = 500000 + (hex - 0x6) * 10000;

	if (range)
		uV *= 2;

	return uV;
}

static int palmas_smps_val(struct udevice *dev, int op, int *uV)
{
	unsigned int hex, adr;
	int ret;
	bool range;
	struct dm_regulator_uclass_platdata *uc_pdata;

	uc_pdata = dev_get_uclass_platdata(dev);

	if (op == PMIC_OP_GET)
		*uV = 0;

	adr = uc_pdata->volt_reg;

	ret = pmic_reg_read(dev->parent, adr);
	if (ret < 0)
		return ret;

	if (op == PMIC_OP_GET) {
		if (ret & PALMAS_SMPS_RANGE_MASK)
			range =  true;
		else
			range = false;

		ret &= PALMAS_SMPS_VOLT_MASK;
		ret = palmas_smps_hex2volt(ret, range);
		if (ret < 0)
			return ret;
		*uV = ret;

		return 0;
	}

	hex = palmas_smps_volt2hex(*uV);
	if (hex < 0)
		return hex;

	ret &= ~PALMAS_SMPS_VOLT_MASK;
	ret |= hex;
	if (*uV > 1650000)
		ret |= PALMAS_SMPS_RANGE_MASK;

	return pmic_reg_write(dev->parent, adr, ret);
}

static int palmas_ldo_bypass_enable(struct udevice *dev, bool enabled)
{
	int type = dev_get_driver_data(dev_get_parent(dev));
	struct dm_regulator_uclass_platdata *p;
	unsigned int adr;
	int reg;

	if (type == TPS65917) {
		/* bypass available only on LDO1 and LDO2 */
		if (dev->driver_data > 2)
			return -ENOTSUPP;
	} else if (type == TPS659038) {
		/* bypass available only on LDO9 */
		if (dev->driver_data != 9)
			return -ENOTSUPP;
	}

	p = dev_get_uclass_platdata(dev);
	adr = p->ctrl_reg;

	reg = pmic_reg_read(dev->parent, adr);
	if (reg < 0)
		return reg;

	if (enabled)
		reg |= PALMAS_LDO_BYPASS_EN;
	else
		reg &= ~PALMAS_LDO_BYPASS_EN;

	return pmic_reg_write(dev->parent, adr, reg);
}

static int palmas_ldo_enable(struct udevice *dev, int op, bool *enable)
{
	int ret;
	unsigned int adr;
	struct dm_regulator_uclass_platdata *uc_pdata;

	uc_pdata = dev_get_uclass_platdata(dev);
	adr = uc_pdata->ctrl_reg;

	ret = pmic_reg_read(dev->parent, adr);
		if (ret < 0)
			return ret;

	if (op == PMIC_OP_GET) {
		ret &= PALMAS_LDO_STATUS_MASK;

		if (ret)
			*enable = true;
		else
			*enable = false;

		return 0;
	} else if (op == PMIC_OP_SET) {
		if (*enable)
			ret |= PALMAS_LDO_MODE_MASK;
		else
			ret &= ~(PALMAS_LDO_MODE_MASK);

		ret = pmic_reg_write(dev->parent, adr, ret);
		if (ret)
			return ret;

		ret = palmas_ldo_bypass_enable(dev, false);
		if (ret && (ret != -ENOTSUPP))
			return ret;
	}

	return 0;
}

static int palmas_ldo_volt2hex(int uV)
{
	if (uV > PALMAS_LDO_VOLT_MAX)
		return -EINVAL;

	return (uV - 850000) / 50000;
}

static int palmas_ldo_hex2volt(int hex)
{
	if (hex > PALMAS_LDO_VOLT_MAX_HEX)
		return -EINVAL;

	if (!hex)
		return 0;

	return (hex * 50000) + 850000;
}

static int palmas_ldo_val(struct udevice *dev, int op, int *uV)
{
	unsigned int hex, adr;
	int ret;

	struct dm_regulator_uclass_platdata *uc_pdata;

	if (op == PMIC_OP_GET)
		*uV = 0;

	uc_pdata = dev_get_uclass_platdata(dev);

	adr = uc_pdata->volt_reg;

	ret = pmic_reg_read(dev->parent, adr);
	if (ret < 0)
		return ret;

	if (op == PMIC_OP_GET) {
		ret &= PALMAS_LDO_VOLT_MASK;
		ret = palmas_ldo_hex2volt(ret);
		if (ret < 0)
			return ret;
		*uV = ret;
		return 0;
	}

	hex = palmas_ldo_volt2hex(*uV);
	if (hex < 0)
		return hex;

	ret &= ~PALMAS_LDO_VOLT_MASK;
	ret |= hex;
	if (*uV > 1650000)
		ret |= 0x80;

	return pmic_reg_write(dev->parent, adr, ret);
}

static int palmas_ldo_probe(struct udevice *dev)
{
	struct dm_regulator_uclass_platdata *uc_pdata;
	struct udevice *parent;

	uc_pdata = dev_get_uclass_platdata(dev);

	parent = dev_get_parent(dev);
	int type = dev_get_driver_data(parent);

	uc_pdata->type = REGULATOR_TYPE_LDO;

	if (dev->driver_data) {
		u8 idx = dev->driver_data - 1;
		uc_pdata->ctrl_reg = palmas_ldo_ctrl[type][idx];
		uc_pdata->volt_reg = palmas_ldo_volt[type][idx];
	} else {
		/* check for ldoln and ldousb cases */
		if (!strcmp("ldoln", dev->name)) {
			uc_pdata->ctrl_reg = palmas_ldo_ctrl[type][9];
			uc_pdata->volt_reg = palmas_ldo_volt[type][9];
		} else if (!strcmp("ldousb", dev->name)) {
			uc_pdata->ctrl_reg = palmas_ldo_ctrl[type][10];
			uc_pdata->volt_reg = palmas_ldo_volt[type][10];
		}
	}

	return 0;
}

static int ldo_get_value(struct udevice *dev)
{
	int uV;
	int ret;

	ret = palmas_ldo_val(dev, PMIC_OP_GET, &uV);
	if (ret)
		return ret;

	return uV;
}

static int ldo_set_value(struct udevice *dev, int uV)
{
	return palmas_ldo_val(dev, PMIC_OP_SET, &uV);
}

static int ldo_get_enable(struct udevice *dev)
{
	bool enable = false;
	int ret;

	ret = palmas_ldo_enable(dev, PMIC_OP_GET, &enable);
	if (ret)
		return ret;

	return enable;
}

static int ldo_set_enable(struct udevice *dev, bool enable)
{
	return palmas_ldo_enable(dev, PMIC_OP_SET, &enable);
}

static int palmas_smps_probe(struct udevice *dev)
{
	struct dm_regulator_uclass_platdata *uc_pdata;
	struct udevice *parent;
	int idx;

	uc_pdata = dev_get_uclass_platdata(dev);

	parent = dev_get_parent(dev);
	int type = dev_get_driver_data(parent);

	uc_pdata->type = REGULATOR_TYPE_BUCK;

	switch (type) {
	case PALMAS:
	case TPS659038:
		switch (dev->driver_data) {
		case 123:
		case 12:
			uc_pdata->ctrl_reg = palmas_smps_ctrl[type][0];
			uc_pdata->volt_reg = palmas_smps_volt[type][0];
			break;
		case 3:
			uc_pdata->ctrl_reg = palmas_smps_ctrl[type][1];
			uc_pdata->volt_reg = palmas_smps_volt[type][1];
			break;
		case 45:
			uc_pdata->ctrl_reg = palmas_smps_ctrl[type][2];
			uc_pdata->volt_reg = palmas_smps_volt[type][2];
			break;
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			idx = dev->driver_data - 3;
			uc_pdata->ctrl_reg = palmas_smps_ctrl[type][idx];
			uc_pdata->volt_reg = palmas_smps_volt[type][idx];
			break;

		default:
			printf("Wrong ID for regulator\n");
		}
		break;

	case TPS65917:
		switch (dev->driver_data) {
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			idx = dev->driver_data - 1;
			uc_pdata->ctrl_reg = palmas_smps_ctrl[type][idx];
			uc_pdata->volt_reg = palmas_smps_volt[type][idx];
			break;
		case 12:
			idx = 0;
			uc_pdata->ctrl_reg = palmas_smps_ctrl[type][idx];
			uc_pdata->volt_reg = palmas_smps_volt[type][idx];
			break;
		default:
			printf("Wrong ID for regulator\n");
		}
		break;

	default:
			printf("Invalid PMIC ID\n");
	}

	return 0;
}

static int smps_get_value(struct udevice *dev)
{
	int uV;
	int ret;

	ret = palmas_smps_val(dev, PMIC_OP_GET, &uV);
	if (ret)
		return ret;

	return uV;
}

static int smps_set_value(struct udevice *dev, int uV)
{
	return palmas_smps_val(dev, PMIC_OP_SET, &uV);
}

static int smps_get_enable(struct udevice *dev)
{
	bool enable = false;
	int ret;

	ret = palmas_smps_enable(dev, PMIC_OP_GET, &enable);
	if (ret)
		return ret;

	return enable;
}

static int smps_set_enable(struct udevice *dev, bool enable)
{
	return palmas_smps_enable(dev, PMIC_OP_SET, &enable);
}

static const struct dm_regulator_ops palmas_ldo_ops = {
	.get_value  = ldo_get_value,
	.set_value  = ldo_set_value,
	.get_enable = ldo_get_enable,
	.set_enable = ldo_set_enable,
};

U_BOOT_DRIVER(palmas_ldo) = {
	.name = PALMAS_LDO_DRIVER,
	.id = UCLASS_REGULATOR,
	.ops = &palmas_ldo_ops,
	.probe = palmas_ldo_probe,
};

static const struct dm_regulator_ops palmas_smps_ops = {
	.get_value  = smps_get_value,
	.set_value  = smps_set_value,
	.get_enable = smps_get_enable,
	.set_enable = smps_set_enable,
};

U_BOOT_DRIVER(palmas_smps) = {
	.name = PALMAS_SMPS_DRIVER,
	.id = UCLASS_REGULATOR,
	.ops = &palmas_smps_ops,
	.probe = palmas_smps_probe,
};