summaryrefslogtreecommitdiffstats
path: root/drivers/mfd/axp20x.c
blob: 666b9ea98caeb5bbf16fa874cfc010439210674a (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * MFD core driver for the X-Powers' Power Management ICs
 *
 * AXP20x typically comprises an adaptive USB-Compatible PWM charger, BUCK DC-DC
 * converters, LDOs, multiple 12-bit ADCs of voltage, current and temperature
 * as well as configurable GPIOs.
 *
 * This file contains the interface independent core functions.
 *
 * Copyright (C) 2014 Carlo Caione
 *
 * Author: Carlo Caione <carlo@caione.org>
 */

#include <common.h>
#include <linux/bitops.h>
#include <clock.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/mfd/axp20x.h>
#include <linux/mfd/core.h>
#include <module.h>
#include <of.h>
#include <of_device.h>
#include <linux/regmap.h>
#include <regulator.h>

#define AXP20X_OFF	BIT(7)

#define AXP806_REG_ADDR_EXT_ADDR_MASTER_MODE	0
#define AXP806_REG_ADDR_EXT_ADDR_SLAVE_MODE	BIT(4)

static const char * const axp20x_model_names[] = {
	"AXP152",
	"AXP202",
	"AXP209",
	"AXP221",
	"AXP223",
	"AXP288",
	"AXP313A",
	"AXP803",
	"AXP806",
	"AXP809",
	"AXP813",
};

static const struct regmap_config axp152_regmap_config = {
	.reg_bits	= 8,
	.val_bits	= 8,
	.max_register	= AXP152_PWM1_DUTY_CYCLE,
};

static const struct regmap_config axp20x_regmap_config = {
	.reg_bits	= 8,
	.val_bits	= 8,
	.max_register	= AXP20X_OCV(AXP20X_OCV_MAX),
};

static const struct regmap_config axp22x_regmap_config = {
	.reg_bits	= 8,
	.val_bits	= 8,
	.max_register	= AXP22X_BATLOW_THRES1,
};

static const struct regmap_config axp288_regmap_config = {
	.reg_bits	= 8,
	.val_bits	= 8,
	.max_register	= AXP288_FG_TUNE5,
};

static const struct regmap_config axp313a_regmap_config = {
	.reg_bits	= 8,
	.val_bits	= 8,
	.max_register	= AXP313A_POK_CONTROL,
};

static const struct regmap_config axp806_regmap_config = {
	.reg_bits	= 8,
	.val_bits	= 8,
	.max_register	= AXP806_REG_ADDR_EXT,
};

static const struct mfd_cell axp20x_cells[] = {
	{
		.name		= "axp20x-gpio",
		/* .of_compatible	= "x-powers,axp209-gpio", */
	}, {
		.name		= "axp20x-pek",
	}, {
		.name		= "axp20x-regulator",
	}, {
		.name		= "axp20x-adc",
		/* .of_compatible	= "x-powers,axp209-adc", */
	}, {
		.name		= "axp20x-battery-power-supply",
		/* .of_compatible	= "x-powers,axp209-battery-power-supply", */
	}, {
		.name		= "axp20x-ac-power-supply",
		/* .of_compatible	= "x-powers,axp202-ac-power-supply", */
	}, {
		.name		= "axp20x-usb-power-supply",
		/* .of_compatible	= "x-powers,axp202-usb-power-supply", */
	},
};

static const struct mfd_cell axp221_cells[] = {
	{
		.name		= "axp221-pek",
	}, {
		.name		= "axp20x-regulator",
	}, {
		.name		= "axp22x-adc",
		/* .of_compatible	= "x-powers,axp221-adc", */
	}, {
		.name		= "axp20x-ac-power-supply",
		/* .of_compatible	= "x-powers,axp221-ac-power-supply", */
	}, {
		.name		= "axp20x-battery-power-supply",
		/* .of_compatible	= "x-powers,axp221-battery-power-supply", */
	}, {
		.name		= "axp20x-usb-power-supply",
		/* .of_compatible	= "x-powers,axp221-usb-power-supply", */
	},
};

static const struct mfd_cell axp223_cells[] = {
	{
		.name		= "axp221-pek",
	}, {
		.name		= "axp22x-adc",
		/* .of_compatible	= "x-powers,axp221-adc", */
	}, {
		.name		= "axp20x-battery-power-supply",
		/* .of_compatible	= "x-powers,axp221-battery-power-supply", */
	}, {
		.name		= "axp20x-regulator",
	}, {
		.name		= "axp20x-ac-power-supply",
		/* .of_compatible	= "x-powers,axp221-ac-power-supply", */
	}, {
		.name		= "axp20x-usb-power-supply",
		/* .of_compatible	= "x-powers,axp223-usb-power-supply", */
	},
};

static const struct mfd_cell axp152_cells[] = {
	{
		.name		= "axp20x-pek",
	},
	{
		.name		= "axp20x-regulator",
	},
};

static const struct mfd_cell axp288_cells[] = {
	{
		.name		= "axp288_adc",
	}, {
		.name		= "axp288_extcon",
	}, {
		.name		= "axp288_charger",
	}, {
		.name		= "axp221-pek",
	}, {
		.name		= "axp288_pmic_acpi",
	},
};

static const struct mfd_cell axp313a_cells[] = {
	{
		.name		= "axp313a-regulator"
	},
};


static const struct mfd_cell axp803_cells[] = {
	{
		.name		= "axp221-pek",
	}, {
		.name		= "axp20x-gpio",
		/* .of_compatible	= "x-powers,axp813-gpio", */
	}, {
		.name		= "axp813-adc",
		/* .of_compatible	= "x-powers,axp813-adc", */
	}, {
		.name		= "axp20x-battery-power-supply",
		/* .of_compatible	= "x-powers,axp813-battery-power-supply", */
	}, {
		.name		= "axp20x-ac-power-supply",
		/* .of_compatible	= "x-powers,axp813-ac-power-supply", */
	}, {
		.name		= "axp20x-usb-power-supply",
		/* .of_compatible	= "x-powers,axp813-usb-power-supply", */
	},
	{	.name		= "axp20x-regulator" },
};

static const struct mfd_cell axp806_cells[] = {
	{
		.name		= "axp20x-regulator",
	},
};

static const struct mfd_cell axp809_cells[] = {
	{
		.name		= "axp221-pek",
	}, {
		.name		= "axp20x-regulator",
	},
};

static const struct mfd_cell axp813_cells[] = {
	{
		.name		= "axp221-pek",
	}, {
		.name		= "axp20x-regulator",
	}, {
		.name		= "axp20x-gpio",
		/* .of_compatible	= "x-powers,axp813-gpio", */
	}, {
		.name		= "axp813-adc",
		/* .of_compatible	= "x-powers,axp813-adc", */
	}, {
		.name		= "axp20x-battery-power-supply",
		/* .of_compatible	= "x-powers,axp813-battery-power-supply", */
	}, {
		.name		= "axp20x-ac-power-supply",
		/* .of_compatible	= "x-powers,axp813-ac-power-supply", */
	}, {
		.name		= "axp20x-usb-power-supply",
		/* .of_compatible	= "x-powers,axp813-usb-power-supply", */
	},
};

static void axp20x_power_off(struct poweroff_handler *handler)
{
	struct axp20x_dev *axp20x = container_of(handler, struct axp20x_dev, poweroff);

	regmap_write(axp20x->regmap, AXP20X_OFF_CTRL, AXP20X_OFF);

	shutdown_barebox();

	/* Give capacitors etc. time to drain to avoid kernel panic msg. */
	mdelay(500);
	hang();
}

int axp20x_match_device(struct axp20x_dev *axp20x)
{
	struct device *dev = axp20x->dev;
	const struct of_device_id *of_id;

	of_id = of_match_device(dev->driver->of_compatible, dev);
	if (!of_id) {
		dev_err(dev, "Unable to match OF ID\n");
		return -ENODEV;
	}
	axp20x->variant = (long)of_id->data;

	switch (axp20x->variant) {
	case AXP152_ID:
		axp20x->nr_cells = ARRAY_SIZE(axp152_cells);
		axp20x->cells = axp152_cells;
		axp20x->regmap_cfg = &axp152_regmap_config;
		break;
	case AXP202_ID:
	case AXP209_ID:
		axp20x->nr_cells = ARRAY_SIZE(axp20x_cells);
		axp20x->cells = axp20x_cells;
		axp20x->regmap_cfg = &axp20x_regmap_config;
		break;
	case AXP221_ID:
		axp20x->nr_cells = ARRAY_SIZE(axp221_cells);
		axp20x->cells = axp221_cells;
		axp20x->regmap_cfg = &axp22x_regmap_config;
		break;
	case AXP223_ID:
		axp20x->nr_cells = ARRAY_SIZE(axp223_cells);
		axp20x->cells = axp223_cells;
		axp20x->regmap_cfg = &axp22x_regmap_config;
		break;
	case AXP288_ID:
		axp20x->cells = axp288_cells;
		axp20x->nr_cells = ARRAY_SIZE(axp288_cells);
		axp20x->regmap_cfg = &axp288_regmap_config;
		break;
	case AXP313A_ID:
		axp20x->cells = axp313a_cells;
		axp20x->nr_cells = ARRAY_SIZE(axp313a_cells);
		axp20x->regmap_cfg = &axp313a_regmap_config;
		break;
	case AXP803_ID:
		axp20x->nr_cells = ARRAY_SIZE(axp803_cells);
		axp20x->cells = axp803_cells;
		axp20x->regmap_cfg = &axp288_regmap_config;
		break;
	case AXP806_ID:
		/*
		 * Don't register the power key part if in slave mode or
		 * if there is no interrupt line.
		 */
		axp20x->nr_cells = ARRAY_SIZE(axp806_cells);
		axp20x->cells = axp806_cells;
		axp20x->regmap_cfg = &axp806_regmap_config;
		break;
	case AXP809_ID:
		axp20x->nr_cells = ARRAY_SIZE(axp809_cells);
		axp20x->cells = axp809_cells;
		axp20x->regmap_cfg = &axp22x_regmap_config;
		break;
	case AXP813_ID:
		axp20x->nr_cells = ARRAY_SIZE(axp813_cells);
		axp20x->cells = axp813_cells;
		axp20x->regmap_cfg = &axp288_regmap_config;
		break;
	default:
		dev_err(dev, "unsupported AXP20X ID %lu\n", axp20x->variant);
		return -EINVAL;
	}
	dev_info(dev, "AXP20x variant %s found\n",
		 axp20x_model_names[axp20x->variant]);

	return 0;
}
EXPORT_SYMBOL(axp20x_match_device);

int axp20x_device_probe(struct axp20x_dev *axp20x)
{
	int ret;

	/*
	 * The AXP806 supports either master/standalone or slave mode.
	 * Slave mode allows sharing the serial bus, even with multiple
	 * AXP806 which all have the same hardware address.
	 *
	 * This is done with extra "serial interface address extension",
	 * or AXP806_BUS_ADDR_EXT, and "register address extension", or
	 * AXP806_REG_ADDR_EXT, registers. The former is read-only, with
	 * 1 bit customizable at the factory, and 1 bit depending on the
	 * state of an external pin. The latter is writable. The device
	 * will only respond to operations to its other registers when
	 * the these device addressing bits (in the upper 4 bits of the
	 * registers) match.
	 *
	 * By default we support an AXP806 chained to an AXP809 in slave
	 * mode. Boards which use an AXP806 in master mode can set the
	 * property "x-powers,master-mode" to override the default.
	 */
	if (axp20x->variant == AXP806_ID) {
		if (of_property_read_bool(axp20x->dev->of_node,
					  "x-powers,master-mode") ||
		    of_property_read_bool(axp20x->dev->of_node,
					  "x-powers,self-working-mode"))
			regmap_write(axp20x->regmap, AXP806_REG_ADDR_EXT,
				     AXP806_REG_ADDR_EXT_ADDR_MASTER_MODE);
		else
			regmap_write(axp20x->regmap, AXP806_REG_ADDR_EXT,
				     AXP806_REG_ADDR_EXT_ADDR_SLAVE_MODE);
	}

	axp20x->dev->priv = axp20x;

	ret = mfd_add_devices(axp20x->dev, axp20x->cells, axp20x->nr_cells);
	if (ret)
		return dev_err_probe(axp20x->dev, ret, "failed to add MFD devices\n");


	axp20x->poweroff.name = "axp20x-poweroff";
	axp20x->poweroff.poweroff = axp20x_power_off;
	axp20x->poweroff.priority = 200;

	if (!(axp20x->variant == AXP288_ID) || (axp20x->variant == AXP313A_ID))
		poweroff_handler_register(&axp20x->poweroff);

	return 0;
}
EXPORT_SYMBOL(axp20x_device_probe);

MODULE_DESCRIPTION("PMIC MFD core driver for AXP20X");
MODULE_AUTHOR("Carlo Caione <carlo@caione.org>");
MODULE_LICENSE("GPL");