summaryrefslogtreecommitdiffstats
path: root/drivers/power/pmic/pmic_tps65090.c
blob: c5b396610ac3ecec91c22ffeaf600c723299fb9b (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
/*
 * Copyright (c) 2012 The Chromium OS Authors.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <errno.h>
#include <fdtdec.h>
#include <i2c.h>
#include <power/pmic.h>
#include <power/tps65090_pmic.h>

DECLARE_GLOBAL_DATA_PTR;

#define TPS65090_NAME "TPS65090_PMIC"

/* TPS65090 register addresses */
enum {
	REG_IRQ1 = 0,
	REG_CG_CTRL0 = 4,
	REG_CG_STATUS1 = 0xa,
	REG_FET1_CTRL = 0x0f,
	REG_FET2_CTRL,
	REG_FET3_CTRL,
	REG_FET4_CTRL,
	REG_FET5_CTRL,
	REG_FET6_CTRL,
	REG_FET7_CTRL,
	TPS65090_NUM_REGS,
};

enum {
	IRQ1_VBATG = 1 << 3,
	CG_CTRL0_ENC_MASK	= 0x01,

	MAX_FET_NUM	= 7,
	MAX_CTRL_READ_TRIES = 5,

	/* TPS65090 FET_CTRL register values */
	FET_CTRL_TOFET		= 1 << 7,  /* Timeout, startup, overload */
	FET_CTRL_PGFET		= 1 << 4,  /* Power good for FET status */
	FET_CTRL_WAIT		= 3 << 2,  /* Overcurrent timeout max */
	FET_CTRL_ADENFET	= 1 << 1,  /* Enable output auto discharge */
	FET_CTRL_ENFET		= 1 << 0,  /* Enable FET */
};

/**
 * Checks for a valid FET number
 *
 * @param fet_id	FET number to check
 * @return 0 if ok, -EINVAL if FET value is out of range
 */
static int tps65090_check_fet(unsigned int fet_id)
{
	if (fet_id == 0 || fet_id > MAX_FET_NUM) {
		debug("parameter fet_id is out of range, %u not in 1 ~ %u\n",
		      fet_id, MAX_FET_NUM);
		return -EINVAL;
	}

	return 0;
}

/**
 * Set the power state for a FET
 *
 * @param pmic		pmic structure for the tps65090
 * @param fet_id	Fet number to set (1..MAX_FET_NUM)
 * @param set		1 to power on FET, 0 to power off
 * @return -EIO if we got a comms error, -EAGAIN if the FET failed to
 * change state. If all is ok, returns 0.
 */
static int tps65090_fet_set(struct pmic *pmic, int fet_id, bool set)
{
	int retry;
	u32 reg, value;

	value = FET_CTRL_ADENFET | FET_CTRL_WAIT;
	if (set)
		value |= FET_CTRL_ENFET;

	if (pmic_reg_write(pmic, REG_FET1_CTRL + fet_id - 1, value))
		return -EIO;

	/* Try reading until we get a result */
	for (retry = 0; retry < MAX_CTRL_READ_TRIES; retry++) {
		if (pmic_reg_read(pmic, REG_FET1_CTRL + fet_id - 1, &reg))
			return -EIO;

		/* Check that the fet went into the expected state */
		if (!!(reg & FET_CTRL_PGFET) == set)
			return 0;

		/* If we got a timeout, there is no point in waiting longer */
		if (reg & FET_CTRL_TOFET)
			break;

		mdelay(1);
	}

	debug("FET %d: Power good should have set to %d but reg=%#02x\n",
	      fet_id, set, reg);
	return -EAGAIN;
}

int tps65090_fet_enable(unsigned int fet_id)
{
	struct pmic *pmic;
	ulong start;
	int loops;
	int ret;

	ret = tps65090_check_fet(fet_id);
	if (ret)
		return ret;

	pmic = pmic_get(TPS65090_NAME);
	if (!pmic)
		return -EACCES;

	start = get_timer(0);
	for (loops = 0;; loops++) {
		ret = tps65090_fet_set(pmic, fet_id, true);
		if (!ret)
			break;

		if (get_timer(start) > 100)
			break;

		/* Turn it off and try again until we time out */
		tps65090_fet_set(pmic, fet_id, false);
	}

	if (ret)
		debug("%s: FET%d failed to power on: time=%lums, loops=%d\n",
		      __func__, fet_id, get_timer(start), loops);
	else if (loops)
		debug("%s: FET%d powered on after %lums, loops=%d\n",
		      __func__, fet_id, get_timer(start), loops);

	/*
	 * Unfortunately, there are some conditions where the power
	 * good bit will be 0, but the fet still comes up. One such
	 * case occurs with the lcd backlight. We'll just return 0 here
	 * and assume that the fet will eventually come up.
	 */
	if (ret == -EAGAIN)
		ret = 0;

	return ret;
}

int tps65090_fet_disable(unsigned int fet_id)
{
	struct pmic *pmic;
	int ret;

	ret = tps65090_check_fet(fet_id);
	if (ret)
		return ret;

	pmic = pmic_get(TPS65090_NAME);
	if (!pmic)
		return -EACCES;
	ret = tps65090_fet_set(pmic, fet_id, false);

	return ret;
}

int tps65090_fet_is_enabled(unsigned int fet_id)
{
	struct pmic *pmic;
	u32 reg;
	int ret;

	ret = tps65090_check_fet(fet_id);
	if (ret)
		return ret;

	pmic = pmic_get(TPS65090_NAME);
	if (!pmic)
		return -ENODEV;
	ret = pmic_reg_read(pmic, REG_FET1_CTRL + fet_id - 1, &reg);
	if (ret) {
		debug("fail to read FET%u_CTRL register over I2C", fet_id);
		return -EIO;
	}

	return reg & FET_CTRL_ENFET;
}

int tps65090_get_charging(void)
{
	struct pmic *pmic;
	u32 val;
	int ret;

	pmic = pmic_get(TPS65090_NAME);
	if (!pmic)
		return -EACCES;

	ret = pmic_reg_read(pmic, REG_CG_CTRL0, &val);
	if (ret)
		return ret;

	return !!(val & CG_CTRL0_ENC_MASK);
}

static int tps65090_charger_state(struct pmic *pmic, int state,
				  int current)
{
	u32 val;
	int ret;

	ret = pmic_reg_read(pmic, REG_CG_CTRL0, &val);
	if (!ret) {
		if (state == PMIC_CHARGER_ENABLE)
			val |= CG_CTRL0_ENC_MASK;
		else
			val &= ~CG_CTRL0_ENC_MASK;
		ret = pmic_reg_write(pmic, REG_CG_CTRL0, val);
	}
	if (ret) {
		debug("%s: Failed to read/write register\n", __func__);
		return ret;
	}

	return 0;
}

int tps65090_get_status(void)
{
	struct pmic *pmic;
	u32 val;
	int ret;

	pmic = pmic_get(TPS65090_NAME);
	if (!pmic)
		return -EACCES;

	ret = pmic_reg_read(pmic, REG_CG_STATUS1, &val);
	if (ret)
		return ret;

	return val;
}

static int tps65090_charger_bat_present(struct pmic *pmic)
{
	u32 val;
	int ret;

	ret = pmic_reg_read(pmic, REG_IRQ1, &val);
	if (ret)
		return ret;

	return !!(val & IRQ1_VBATG);
}

static struct power_chrg power_chrg_pmic_ops = {
	.chrg_bat_present = tps65090_charger_bat_present,
	.chrg_state = tps65090_charger_state,
};

int tps65090_init(void)
{
	struct pmic *p;
	int bus;
	int addr;
	const void *blob = gd->fdt_blob;
	int node, parent;

	node = fdtdec_next_compatible(blob, 0, COMPAT_TI_TPS65090);
	if (node < 0) {
		debug("PMIC: No node for PMIC Chip in device tree\n");
		debug("node = %d\n", node);
		return -ENODEV;
	}

	parent = fdt_parent_offset(blob, node);
	if (parent < 0) {
		debug("%s: Cannot find node parent\n", __func__);
		return -EINVAL;
	}

	bus = i2c_get_bus_num_fdt(parent);
	if (p->bus < 0) {
		debug("%s: Cannot find I2C bus\n", __func__);
		return -ENOENT;
	}
	addr = fdtdec_get_int(blob, node, "reg", TPS65090_I2C_ADDR);
	p = pmic_alloc();
	if (!p) {
		printf("%s: POWER allocation error!\n", __func__);
		return -ENOMEM;
	}

	p->name = TPS65090_NAME;
	p->bus = bus;
	p->interface = PMIC_I2C;
	p->number_of_regs = TPS65090_NUM_REGS;
	p->hw.i2c.addr = addr;
	p->hw.i2c.tx_num = 1;
	p->chrg = &power_chrg_pmic_ops;

	puts("TPS65090 PMIC init\n");

	return 0;
}
OpenPOWER on IntegriCloud