summaryrefslogtreecommitdiffstats
path: root/drivers/power/regulator/tps65910_regulator.c
blob: 171afd34a5f1db65b1e3af965e806e91ebaaa8b0 (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) EETS GmbH, 2017, Felix Brack <f.brack@eets.ch>
 */

#include <common.h>
#include <dm.h>
#include <log.h>
#include <power/pmic.h>
#include <power/regulator.h>
#include <power/tps65910_pmic.h>

#define VOUT_CHOICE_COUNT 4

/*
 * struct regulator_props - Properties of a LDO and VIO SMPS regulator
 *
 * All of these regulators allow setting one out of four output voltages.
 * These output voltages are only achievable when supplying the regulator
 * with a minimum input voltage.
 *
 * @vin_min[]: minimum supply input voltage in uV required to achieve the
 *             corresponding vout[] voltage
 * @vout[]:    regulator output voltage in uV
 * @reg:       I2C register used to set regulator voltage
 */
struct regulator_props {
	int vin_min[VOUT_CHOICE_COUNT];
	int vout[VOUT_CHOICE_COUNT];
	int reg;
};

static const struct regulator_props ldo_props_vdig1 = {
	.vin_min = { 1700000, 2100000, 2700000, 3200000 },
	.vout = { 1200000, 1500000, 1800000, 2700000 },
	.reg = TPS65910_REG_VDIG1
};

static const struct regulator_props ldo_props_vdig2 = {
	.vin_min = { 1700000, 1700000, 1700000, 2700000 },
	.vout = { 1000000, 1100000, 1200000, 1800000 },
	.reg = TPS65910_REG_VDIG2
};

static const struct regulator_props ldo_props_vpll = {
	.vin_min = { 2700000, 2700000, 2700000, 3000000 },
	.vout = { 1000000, 1100000, 1800000, 2500000 },
	.reg = TPS65910_REG_VPLL
};

static const struct regulator_props ldo_props_vdac = {
	.vin_min = { 2700000, 3000000, 3200000, 3200000 },
	.vout = { 1800000, 2600000, 2800000, 2850000 },
	.reg = TPS65910_REG_VDAC
};

static const struct regulator_props ldo_props_vaux1 = {
	.vin_min = { 2700000, 3200000, 3200000, 3200000 },
	.vout = { 1800000, 2500000, 2800000, 2850000 },
	.reg = TPS65910_REG_VAUX1
};

static const struct regulator_props ldo_props_vaux2 = {
	.vin_min = { 2700000, 3200000, 3200000, 3600000 },
	.vout = { 1800000, 2800000, 2900000, 3300000 },
	.reg = TPS65910_REG_VAUX2
};

static const struct regulator_props ldo_props_vaux33 = {
	.vin_min = { 2700000, 2700000, 3200000, 3600000 },
	.vout = { 1800000, 2000000, 2800000, 3300000 },
	.reg = TPS65910_REG_VAUX33
};

static const struct regulator_props ldo_props_vmmc = {
	.vin_min = { 2700000, 3200000, 3200000, 3600000 },
	.vout = { 1800000, 2800000, 3000000, 3300000 },
	.reg = TPS65910_REG_VMMC
};

static const struct regulator_props smps_props_vio = {
	.vin_min = { 3200000, 3200000, 4000000, 4400000 },
	.vout = { 1500000, 1800000, 2500000, 3300000 },
	.reg = TPS65910_REG_VIO
};

/* lookup table of control registers indexed by regulator unit number */
static const int ctrl_regs[] = {
	TPS65910_REG_VRTC,
	TPS65910_REG_VIO,
	TPS65910_REG_VDD1,
	TPS65910_REG_VDD2,
	TPS65910_REG_VDD3,
	TPS65910_REG_VDIG1,
	TPS65910_REG_VDIG2,
	TPS65910_REG_VPLL,
	TPS65910_REG_VDAC,
	TPS65910_REG_VAUX1,
	TPS65910_REG_VAUX2,
	TPS65910_REG_VAUX33,
	TPS65910_REG_VMMC
};

/* supply names as used in DT */
static const char * const supply_names[] = {
	"vccio-supply",
	"vcc1-supply",
	"vcc2-supply",
	"vcc3-supply",
	"vcc4-supply",
	"vcc5-supply",
	"vcc6-supply",
	"vcc7-supply"
};

/* lookup table of regulator supplies indexed by regulator unit number */
static const int regulator_supplies[] = {
	TPS65910_SUPPLY_VCC7,
	TPS65910_SUPPLY_VCCIO,
	TPS65910_SUPPLY_VCC1,
	TPS65910_SUPPLY_VCC2,
	TPS65910_SUPPLY_VCC7,
	TPS65910_SUPPLY_VCC6,
	TPS65910_SUPPLY_VCC6,
	TPS65910_SUPPLY_VCC5,
	TPS65910_SUPPLY_VCC5,
	TPS65910_SUPPLY_VCC4,
	TPS65910_SUPPLY_VCC4,
	TPS65910_SUPPLY_VCC3,
	TPS65910_SUPPLY_VCC3
};

static int get_ctrl_reg_from_unit_addr(const uint unit_addr)
{
	if (unit_addr < ARRAY_SIZE(ctrl_regs))
		return ctrl_regs[unit_addr];
	return -ENXIO;
}

static int tps65910_regulator_get_value(struct udevice *dev,
					const struct regulator_props *rgp)
{
	int sel, val, vout;
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);
	int vin = pdata->supply;

	val = pmic_reg_read(dev->parent, rgp->reg);
	if (val < 0)
		return val;
	sel = (val & TPS65910_SEL_MASK) >> 2;
	vout = (vin >= *(rgp->vin_min + sel)) ? *(rgp->vout + sel) : 0;
	vout = ((val & TPS65910_SUPPLY_STATE_MASK) == 1) ? vout : 0;

	return vout;
}

static int tps65910_ldo_get_value(struct udevice *dev)
{
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);
	int vin;

	if (!pdata)
		return 0;
	vin = pdata->supply;

	switch (pdata->unit) {
	case TPS65910_UNIT_VRTC:
		/* VRTC is fixed and can't be turned off */
		return (vin >= 2500000) ? 1830000 : 0;
	case TPS65910_UNIT_VDIG1:
		return tps65910_regulator_get_value(dev, &ldo_props_vdig1);
	case TPS65910_UNIT_VDIG2:
		return tps65910_regulator_get_value(dev, &ldo_props_vdig2);
	case TPS65910_UNIT_VPLL:
		return tps65910_regulator_get_value(dev, &ldo_props_vpll);
	case TPS65910_UNIT_VDAC:
		return tps65910_regulator_get_value(dev, &ldo_props_vdac);
	case TPS65910_UNIT_VAUX1:
		return tps65910_regulator_get_value(dev, &ldo_props_vaux1);
	case TPS65910_UNIT_VAUX2:
		return tps65910_regulator_get_value(dev, &ldo_props_vaux2);
	case TPS65910_UNIT_VAUX33:
		return tps65910_regulator_get_value(dev, &ldo_props_vaux33);
	case TPS65910_UNIT_VMMC:
		return tps65910_regulator_get_value(dev, &ldo_props_vmmc);
	default:
		return 0;
	}
}

static int tps65910_regulator_set_value(struct udevice *dev,
					const struct regulator_props *ldo,
					int uV)
{
	int val;
	int sel = 0;
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);

	do {
		/* we only allow exact voltage matches */
		if (uV == *(ldo->vout + sel))
			break;
	} while (++sel < VOUT_CHOICE_COUNT);
	if (sel == VOUT_CHOICE_COUNT)
		return -EINVAL;
	if (pdata->supply < *(ldo->vin_min + sel))
		return -EINVAL;

	val = pmic_reg_read(dev->parent, ldo->reg);
	if (val < 0)
		return val;
	val &= ~TPS65910_SEL_MASK;
	val |= sel << 2;
	return pmic_reg_write(dev->parent, ldo->reg, val);
}

static int tps65910_ldo_set_value(struct udevice *dev, int uV)
{
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);
	int vin = pdata->supply;

	switch (pdata->unit) {
	case TPS65910_UNIT_VRTC:
		/* VRTC is fixed to 1.83V and can't be turned off */
		if (vin < 2500000)
			return -EINVAL;
		return 0;
	case TPS65910_UNIT_VDIG1:
		return tps65910_regulator_set_value(dev, &ldo_props_vdig1, uV);
	case TPS65910_UNIT_VDIG2:
		return tps65910_regulator_set_value(dev, &ldo_props_vdig2, uV);
	case TPS65910_UNIT_VPLL:
		return tps65910_regulator_set_value(dev, &ldo_props_vpll, uV);
	case TPS65910_UNIT_VDAC:
		return tps65910_regulator_set_value(dev, &ldo_props_vdac, uV);
	case TPS65910_UNIT_VAUX1:
		return tps65910_regulator_set_value(dev, &ldo_props_vaux1, uV);
	case TPS65910_UNIT_VAUX2:
		return tps65910_regulator_set_value(dev, &ldo_props_vaux2, uV);
	case TPS65910_UNIT_VAUX33:
		return tps65910_regulator_set_value(dev, &ldo_props_vaux33, uV);
	case TPS65910_UNIT_VMMC:
		return tps65910_regulator_set_value(dev, &ldo_props_vmmc, uV);
	default:
		return 0;
	}
}

static int tps65910_get_enable(struct udevice *dev)
{
	int reg, val;
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);

	reg = get_ctrl_reg_from_unit_addr(pdata->unit);
	if (reg < 0)
		return reg;

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

	/* bits 1:0 of regulator control register define state */
	return ((val & TPS65910_SUPPLY_STATE_MASK) == 1);
}

static int tps65910_set_enable(struct udevice *dev, bool enable)
{
	int reg;
	uint clr, set;
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);

	reg = get_ctrl_reg_from_unit_addr(pdata->unit);
	if (reg < 0)
		return reg;

	if (enable) {
		clr = TPS65910_SUPPLY_STATE_MASK & ~TPS65910_SUPPLY_STATE_ON;
		set = TPS65910_SUPPLY_STATE_MASK & TPS65910_SUPPLY_STATE_ON;
	} else {
		clr = TPS65910_SUPPLY_STATE_MASK & ~TPS65910_SUPPLY_STATE_OFF;
		set = TPS65910_SUPPLY_STATE_MASK & TPS65910_SUPPLY_STATE_OFF;
	}
	return pmic_clrsetbits(dev->parent, reg, clr, set);
}

static int buck_get_vdd1_vdd2_value(struct udevice *dev, int reg_vdd)
{
	int gain;
	int val = pmic_reg_read(dev, reg_vdd);

	if (val < 0)
		return val;
	gain = (val & TPS65910_GAIN_SEL_MASK) >> 6;
	gain = (gain == 0) ? 1 : gain;
	val = pmic_reg_read(dev, reg_vdd + 1);
	if (val < 0)
		return val;
	if (val & TPS65910_VDD_SR_MASK)
		/* use smart reflex value instead */
		val = pmic_reg_read(dev, reg_vdd + 2);
	if (val < 0)
		return val;
	return (562500 + (val & TPS65910_VDD_SEL_MASK) * 12500) * gain;
}

static int tps65910_buck_get_value(struct udevice *dev)
{
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);

	switch (pdata->unit) {
	case TPS65910_UNIT_VIO:
		return tps65910_regulator_get_value(dev, &smps_props_vio);
	case TPS65910_UNIT_VDD1:
		return buck_get_vdd1_vdd2_value(dev->parent, TPS65910_REG_VDD1);
	case TPS65910_UNIT_VDD2:
		return buck_get_vdd1_vdd2_value(dev->parent, TPS65910_REG_VDD2);
	default:
		return 0;
	}
}

static int buck_set_vdd1_vdd2_value(struct udevice *dev, int uV)
{
	int ret, reg_vdd, gain;
	int val;
	struct dm_regulator_uclass_platdata *uc_pdata;
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);

	switch (pdata->unit) {
	case TPS65910_UNIT_VDD1:
		reg_vdd = TPS65910_REG_VDD1;
		break;
	case TPS65910_UNIT_VDD2:
		reg_vdd = TPS65910_REG_VDD2;
		break;
	default:
		return -EINVAL;
	}
	uc_pdata = dev_get_uclass_platdata(dev);

	/* check setpoint is within limits */
	if (uV < uc_pdata->min_uV) {
		pr_err("voltage %duV for %s too low\n", uV, dev->name);
		return -EINVAL;
	}
	if (uV > uc_pdata->max_uV) {
		pr_err("voltage %duV for %s too high\n", uV, dev->name);
		return -EINVAL;
	}

	val = pmic_reg_read(dev->parent, reg_vdd);
	if (val < 0)
		return val;
	gain = (val & TPS65910_GAIN_SEL_MASK) >> 6;
	gain = (gain == 0) ? 1 : gain;
	val = ((uV / gain) - 562500) / 12500;
	if (val < TPS65910_VDD_SEL_MIN || val > TPS65910_VDD_SEL_MAX)
		/*
		 * Neither do we change the gain, nor do we allow shutdown or
		 * any approximate value (for now)
		 */
		return -EPERM;
	val &= TPS65910_VDD_SEL_MASK;
	ret = pmic_reg_write(dev->parent, reg_vdd + 1, val);
	if (ret)
		return ret;
	return 0;
}

static int tps65910_buck_set_value(struct udevice *dev, int uV)
{
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);

	if (pdata->unit == TPS65910_UNIT_VIO)
		return tps65910_regulator_set_value(dev, &smps_props_vio, uV);

	return buck_set_vdd1_vdd2_value(dev, uV);
}

static int tps65910_boost_get_value(struct udevice *dev)
{
	int vout;
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);

	vout = (pdata->supply >= 3000000) ? 5000000 : 0;
	return vout;
}

static int tps65910_regulator_ofdata_to_platdata(struct udevice *dev)
{
	struct udevice *supply;
	int ret;
	const char *supply_name;
	struct tps65910_regulator_pdata *pdata = dev_get_platdata(dev);

	pdata->unit = dev_get_driver_data(dev);
	if (pdata->unit > TPS65910_UNIT_VMMC)
		return -EINVAL;
	supply_name = supply_names[regulator_supplies[pdata->unit]];

	debug("Looking up supply power %s\n", supply_name);
	ret = device_get_supply_regulator(dev->parent, supply_name, &supply);
	if (ret) {
		debug("  missing supply power %s\n", supply_name);
		return ret;
	}
	pdata->supply = regulator_get_value(supply);
	if (pdata->supply < 0) {
		debug("  invalid supply voltage for regulator %s\n",
		      supply->name);
		return -EINVAL;
	}

	return 0;
}

static const struct dm_regulator_ops tps65910_boost_ops = {
	.get_value  = tps65910_boost_get_value,
	.get_enable = tps65910_get_enable,
	.set_enable = tps65910_set_enable,
};

U_BOOT_DRIVER(tps65910_boost) = {
	.name = TPS65910_BOOST_DRIVER,
	.id = UCLASS_REGULATOR,
	.ops = &tps65910_boost_ops,
	.platdata_auto_alloc_size = sizeof(struct tps65910_regulator_pdata),
	.ofdata_to_platdata = tps65910_regulator_ofdata_to_platdata,
};

static const struct dm_regulator_ops tps65910_buck_ops = {
	.get_value  = tps65910_buck_get_value,
	.set_value  = tps65910_buck_set_value,
	.get_enable = tps65910_get_enable,
	.set_enable = tps65910_set_enable,
};

U_BOOT_DRIVER(tps65910_buck) = {
	.name = TPS65910_BUCK_DRIVER,
	.id = UCLASS_REGULATOR,
	.ops = &tps65910_buck_ops,
	.platdata_auto_alloc_size = sizeof(struct tps65910_regulator_pdata),
	.ofdata_to_platdata = tps65910_regulator_ofdata_to_platdata,
};

static const struct dm_regulator_ops tps65910_ldo_ops = {
	.get_value  = tps65910_ldo_get_value,
	.set_value  = tps65910_ldo_set_value,
	.get_enable = tps65910_get_enable,
	.set_enable = tps65910_set_enable,
};

U_BOOT_DRIVER(tps65910_ldo) = {
	.name = TPS65910_LDO_DRIVER,
	.id = UCLASS_REGULATOR,
	.ops = &tps65910_ldo_ops,
	.platdata_auto_alloc_size = sizeof(struct tps65910_regulator_pdata),
	.ofdata_to_platdata = tps65910_regulator_ofdata_to_platdata,
};