summaryrefslogtreecommitdiffstats
path: root/src/drivers/xpowers/axp209/axp209.c
blob: f36c506a2df378d981d687619378420761d9fff4 (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
/*
 * Driver for X-Powers AXP 209 Power Management Unit
 *
 * Despite axp209_read/write only working on a byte at a time, there is no such
 * limitation in the AXP209.
 *
 * Copyright (C) 2013  Alexandru Gagniuc <mr.nuke.me@gmail.com>
 * Subject to the GNU GPL v2, or (at your option) any later version.
 */

#include "axp209.h"
#include "chip.h"

#include <console/console.h>
#include <device/device.h>
#include <device/i2c.h>

/* Hide these definitions from the rest of the source, so keep them here */
enum registers {
	REG_POWER_STATUS	= 0x00,
	REG_POWER_MODE		= 0x01,
	REG_OTG_VBUS		= 0x02,
	REG_CHIP_ID		= 0x03,
	REG_CHIP_PWROUT_CTL	= 0x12,
	REG_DCDC2_VOLTAGE	= 0x23,
	REG_DCDC2_LDO3_CTL	= 0x25,
	REG_DCDC3_VOLTAGE	= 0x27,
	REG_LDO24_VOLTAGE	= 0x28,
	REG_LDO3_VOLTAGE	= 0x29,
	REG_VBUS_IPSOUT		= 0x30,
	REG_PWROFF_VOLTAGE	= 0x31,
	REG_SHTDWN_SETTING	= 0x32,
};

/* REG_LDO24_VOLTAGE definitions */
#define REG_LDO24_VOLTAGE_LDO2_MASK	(0xf << 4)
#define REG_LDO24_VOLTAGE_LDO2_VAL(x)	((x << 4) & REG_LDO24_VOLTAGE_LDO2_MASK)
#define REG_LDO24_VOLTAGE_LDO4_MASK	(0xf << 0)
#define REG_LDO24_VOLTAGE_LDO4_VAL(x)	((x << 0) & REG_LDO24_VOLTAGE_LDO4_MASK)

/*
 * Read and write accessors. We only work on one register at a time, but there
 * is no limitation on the AXP209 as to how many registers we may read or write
 * in one transaction.
 * These return the number of bytes read/written, or an error code. In this
 * case, they return 1 on success, or an error code otherwise. This is done to
 * work with I²C drivers that return either 0 on success or the number of bytes
 * actually transferred.
 */
static int axp209_read(u8 bus, u8 reg, u8 *val)
{
	if (i2c_readb(bus, AXP209_I2C_ADDR, reg, val) < 0)
		return CB_ERR;
	return 1;
}

static int axp209_write(u8 bus, u8 reg, u8 val)
{
	if (i2c_writeb(bus, AXP209_I2C_ADDR, reg, val) < 0)
		return CB_ERR;
	return 1;
}

/**
 * \brief Identify and initialize an AXP209 on the I²C bus
 *
 * @param[in] bus I²C bus to which the AXP209 is connected
 * @return CB_SUCCES on if an AXP209 is found, or an error code otherwise.
 */
enum cb_err axp209_init(u8 bus)
{
	u8 id;

	if (axp209_read(bus, REG_CHIP_ID, &id) != 1)
		return CB_ERR;

	/* From U-Boot code : Low 4 bits is chip version */
	if ((id & 0x0f) != 0x1) {
		printk(BIOS_ERR, "[axp209] ID 0x%x does not match\n", id);
		return CB_ERR;
	}

	return CB_SUCCESS;
}

/**
 * \brief Configure the output voltage of DC-DC2 converter
 *
 * If the requested voltage is not available, the next lowest voltage will
 * be applied.
 * Valid values are between 700mV and 2275mV
 *
 * @param[in] millivolts voltage in mV units.
 * @param[in] bus I²C bus to which the AXP209 is connected
 * @return CB_SUCCES on success,
 *	   CB_ERR_ARG if voltage is out of range, or an error code otherwise.
 */
enum cb_err axp209_set_dcdc2_voltage(u8 bus, u16 millivolts)
{
	u8 val;

	if (millivolts < 700 || millivolts > 2275)
		return CB_ERR_ARG;

	val = (millivolts - 700) / 25;

	if (axp209_write(bus, REG_DCDC2_VOLTAGE, val) != 1)
		return CB_ERR;

	return CB_SUCCESS;
}

/**
 * \brief Configure the output voltage of DC-DC3 converter
 *
 * If the requested voltage is not available, the next lowest voltage will
 * be applied.
 * Valid values are between 700mV and 3500mV
 *
 * @param[in] millivolts voltage in mV units.
 * @param[in] bus I²C bus to which the AXP209 is connected
 * @return CB_SUCCES on success,
 *	   CB_ERR_ARG if voltage is out of range, or an error code otherwise.
 */
enum cb_err axp209_set_dcdc3_voltage(u8 bus, u16 millivolts)
{
	u8 val;

	if (millivolts < 700 || millivolts > 3500)
		return CB_ERR_ARG;

	val = (millivolts - 700) / 25;

	if (axp209_write(bus, REG_DCDC3_VOLTAGE, val) != 1)
		return CB_ERR;

	return CB_SUCCESS;
}

/**
 * \brief Configure the output voltage of LDO2 regulator
 *
 * If the requested voltage is not available, the next lowest voltage will
 * be applied.
 * Valid values are between 700mV and 3300mV
 *
 * @param[in] millivolts voltage in mV units.
 * @param[in] bus I²C bus to which the AXP209 is connected
 * @return CB_SUCCES on success,
 *	   CB_ERR_ARG if voltage is out of range, or an error code otherwise.
 */
enum cb_err axp209_set_ldo2_voltage(u8 bus, u16 millivolts)
{
	u8 reg8, val;

	if (millivolts < 1800 || millivolts > 3300)
		return CB_ERR_ARG;

	/* Try to read the register first, and stop here on error */
	if (axp209_read(bus, REG_LDO24_VOLTAGE, &reg8) != 1)
		return CB_ERR;

	val = (millivolts - 1800) / 100;
	reg8 &= ~REG_LDO24_VOLTAGE_LDO2_MASK;
	reg8 |= REG_LDO24_VOLTAGE_LDO2_VAL(val);

	if (axp209_write(bus, REG_LDO24_VOLTAGE, reg8) != 1)
		return CB_ERR;

	return CB_SUCCESS;
}

/**
 * \brief Configure the output voltage of LDO4 regulator
 *
 * If the requested voltage is not available, the next lowest voltage will
 * be applied.
 * Valid values are between 700mV and 3500mV. Datasheet lists maximum voltage at
 * 2250mV, but hardware samples go as high as 3500mV.
 *
 * @param[in] millivolts voltage in mV units.
 * @param[in] bus I²C bus to which the AXP209 is connected
 * @return CB_SUCCES on success,
 *	   CB_ERR_ARG if voltage is out of range, or an error code otherwise.
 */
enum cb_err axp209_set_ldo3_voltage(u8 bus, u16 millivolts)
{
	u8 val;

	/* Datasheet lists 2250 max, but PMU will output up to 3500mV */
	if (millivolts < 700 || millivolts > 3500)
		return CB_ERR_ARG;

	val = (millivolts - 700) / 25;

	if (axp209_write(bus, REG_LDO3_VOLTAGE, val) != 1)
		return CB_ERR;

	return CB_SUCCESS;
}

/**
 * \brief Configure the output voltage of DC-DC2 converter
 *
 * If the requested voltage is not available, the next lowest voltage will
 * be applied.
 * Valid values are between 1250V and 3300mV
 *
 * @param[in] millivolts voltage in mV units.
 * @param[in] bus I²C bus to which the AXP209 is connected
 * @return CB_SUCCES on success,
 *	   CB_ERR_ARG if voltage is out of range, or an error code otherwise.
 */
enum cb_err axp209_set_ldo4_voltage(u8 bus, u16 millivolts)
{
	u8 reg8, val;

	if (millivolts < 1250 || millivolts > 3300)
		return CB_ERR_ARG;

	/* Try to read the register first, and stop here on error */
	if (axp209_read(bus, REG_LDO24_VOLTAGE, &reg8) != 1)
		return CB_ERR;

	if (millivolts <= 2000)
		val = (millivolts - 1200) / 100;
	else if (millivolts <= 2700)
		val = 9 + (millivolts - 2500) / 100;
	else if (millivolts <= 2800)
		val = 11;
	else
		val = 12 + (millivolts - 3000) / 100;

	reg8 &= ~REG_LDO24_VOLTAGE_LDO4_MASK;
	reg8 |= REG_LDO24_VOLTAGE_LDO4_VAL(val);

	if (axp209_write(bus, REG_LDO24_VOLTAGE, reg8) != 1)
		return CB_ERR;

	return CB_SUCCESS;
}

static const struct {
	enum cb_err (*set_voltage) (u8 bus, u16 mv);
	const char *name;
} vtable[] = { {
		.set_voltage = axp209_set_dcdc2_voltage,
		.name = "DCDC2",
	}, {
		.set_voltage = axp209_set_dcdc3_voltage,
		.name = "DCDC3",
	}, {
		.set_voltage = axp209_set_ldo2_voltage,
		.name = "LDO2",
	}, {
		.set_voltage = axp209_set_ldo3_voltage,
		.name = "LDO3",
	}, {
		.set_voltage = axp209_set_ldo4_voltage,
		.name = "LDO4",
	}
};

static enum cb_err set_rail(u8 bus, int idx, u16 mv)
{
	enum cb_err err;
	const char *name = vtable[idx].name;

	/* If voltage isn't specified, don't touch the rail */
	if (mv == 0) {
		printk(BIOS_DEBUG, "[axp209] Skipping %s configuration\n",
		       name);
		return CB_SUCCESS;
	}

	if ((err = vtable[idx].set_voltage(bus, mv) != CB_SUCCESS)) {
		printk(BIOS_ERR, "[axp209] Failed to set %s to %u mv\n",
		       name, mv);
		return err;
	}

	return CB_SUCCESS;
}

/**
 * \brief Configure all voltage rails
 *
 * Configure all converters and regulators from devicetree config. If any of the
 * voltages are not declared (i.e. are zero), the respective rail will not be
 * reconfigured, and retain its powerup voltage.
 *
 * @param[in] cfg pointer to @ref drivers_xpowers_axp209_config structure
 * @param[in] bus I²C bus to which the AXP209 is connected
 * @return CB_SUCCES on success, or an error code otherwise.
 */
enum cb_err axp209_set_voltages(u8 bus, const struct
				drivers_xpowers_axp209_config *cfg)
{
	enum cb_err err;

	/* Don't worry about what the error is. Console prints that */
	err = set_rail(bus, 0, cfg->dcdc2_voltage_mv);
	err |= set_rail(bus, 1, cfg->dcdc3_voltage_mv);
	err |= set_rail(bus, 2, cfg->ldo2_voltage_mv);
	err |= set_rail(bus, 3, cfg->ldo3_voltage_mv);
	err |= set_rail(bus, 4, cfg->ldo4_voltage_mv);

	if (err != CB_SUCCESS)
		return CB_ERR;

	return CB_SUCCESS;
}

/*
 * Usually, the AXP209 is enabled and configured in romstage, so there is no
 * need for a full ramstage driver. Hence .enable_dev is NULL.
 */
#ifndef __PRE_RAM__
struct chip_operations drivers_xpowers_axp209_config = {
	CHIP_NAME("X-Powers AXP 209 Power Management Unit")
	.enable_dev = NULL,
};
#endif /* __PRE_RAM__ */