summaryrefslogtreecommitdiff
path: root/sys/dev/fdt/axppmic.c
blob: 697a5d7e4683cd5cb3366a2ed948f2ba55e24ba5 (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*	$OpenBSD: axppmic.c,v 1.6 2018/02/10 22:32:01 kettenis Exp $	*/
/*
 * Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/sensors.h>

#include <dev/i2c/i2cvar.h>
#include <dev/fdt/rsbvar.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_regulator.h>
#include <dev/ofw/fdt.h>

extern void (*powerdownfn)(void);

#define AXP209_SDR		0x32
#define  AXP209_SDR_SHUTDOWN	(1 << 7)
#define AXP209_ADC_EN1		0x82
#define  AXP209_ADC_EN1_ACIN	(3 << 4)
#define  AXP209_ADC_EN1_VBUS	(3 << 2)

#define AXP806_REG_ADDR_EXT			0xff
#define  AXP806_REG_ADDR_EXT_MASTER_MODE	(0 << 4)
#define  AXP806_REG_ADDR_EXT_SLAVE_MODE		(1 << 4)

/* Regulators for AXP209, AXP221, AXP806 and AXP809. */

struct axppmic_regdata {
	const char *name;
	uint8_t ereg, emask, eval, dval;
	uint8_t vreg, vmask;
	uint32_t base, delta;
	uint32_t base2, delta2;
};

struct axppmic_regdata axp209_regdata[] = {
	{ "dcdc2", 0x12, (1 << 4), (1 << 4), (0 << 4),
	  0x23, 0x3f, 700000, 25000 },
	{ "dcdc3", 0x12, (1 << 1), (1 << 1), (0 << 1),
	  0x27, 0x3f, 700000, 25000 },
	/* LDO1 can't be controlled */
	{ "ldo2", 0x12, (1 << 2), (1 << 2), (0 << 2),
	  0x28, 0xf0, 1800000, (100000 >> 4) },
	{ "ldo3", 0x12, (1 << 6), (1 << 6), (0 << 6),
	  0x29, 0x7f, 700000, 25000 },
	/* LDO4 voltage levels are complicated */
	{ "ldo5", 0x90, 0x07, 0x03, 0x07,
	  0x91, 0xf0, 1800000, (100000 >> 4) },
	{ NULL }
};

struct axppmic_regdata axp221_regdata[] = {
	{ "dcdc1", 0x10, (1 << 1), (1 << 1), (0 << 1),
	  0x21, 0x1f, 1600000, 100000 },
	{ "dcdc2", 0x10, (1 << 2), (1 << 2), (0 << 2),
	  0x22, 0x3f, 600000, 20000 },
	{ "dcdc3", 0x10, (1 << 3), (1 << 3), (0 << 3),
	  0x23, 0x3f, 600000, 20000 },
	{ "dcdc4", 0x10, (1 << 4), (1 << 4), (0 << 4),
	  0x24, 0x3f, 600000, 20000 },
	{ "dcdc5", 0x10, (1 << 5), (1 << 5), (0 << 5),
	  0x25, 0x1f, 1000000, 50000 },
	{ "dc1sw", 0x12, (1 << 7), (1 << 7), (0 << 7) },
	{ "dc5ldo", 0x10, (1 << 0), (1 << 0), (0 << 0),
	  0x1c, 0x07, 700000, 100000 },
	{ "aldo1", 0x10, (1 << 6), (1 << 6), (0 << 6),
	  0x28, 0x1f, 700000, 100000 },
	{ "aldo2", 0x10, (1 << 7), (1 << 7), (0 << 7),
	  0x29, 0x1f, 700000, 100000 },
	{ "aldo3", 0x13, (1 << 7), (1 << 7), (0 << 7),
	  0x2a, 0x1f, 700000, 100000 },
	{ "dldo1", 0x12, (1 << 3), (1 << 3), (0 << 3),
	  0x15, 0x1f, 700000, 100000 },
	{ "dldo2", 0x12, (1 << 4), (1 << 4), (0 << 4),
	  0x16, 0x1f, 700000, 100000 },
	{ "dldo3", 0x12, (1 << 5), (1 << 5), (0 << 5),
	  0x17, 0x1f, 700000, 100000 },
	{ "dldo4", 0x12, (1 << 6), (1 << 6), (0 << 6),
	  0x18, 0x1f, 700000, 100000 },
	{ "eldo1", 0x12, (1 << 0), (1 << 0), (0 << 0),
	  0x19, 0x1f, 700000, 100000 },
	{ "eldo2", 0x12, (1 << 1), (1 << 1), (0 << 1),
	  0x1a, 0x1f, 700000, 100000 },
	{ "eldo3", 0x12, (1 << 2), (1 << 2), (0 << 2),
	  0x1b, 0x1f, 700000, 100000 },
	{ "ldo_io0", 0x90, 0x07, 0x03, 0x04,
	  0x91, 0x1f, 700000, 100000 },
	{ "ldo_io1", 0x92, 0x07, 0x03, 0x04,
	  0x93, 0x1f, 700000, 100000 },
	{ NULL }
};

struct axppmic_regdata axp803_regdata[] = {
	{ "dcdc1", 0x10, (1 << 0), (1 << 0), (0 << 0),
	  0x20, 0x1f, 1600000, 100000 },
	{ "dcdc2", 0x10, (1 << 1), (1 << 1), (0 << 1),
	  0x21, 0x7f, 500000, 10000, 1220000, 20000 },
	{ "dcdc3", 0x10, (1 << 2), (1 << 2), (0 << 2),
	  0x22, 0x7f, 500000, 10000, 1220000, 20000 },
	{ "dcdc4", 0x10, (1 << 3), (1 << 3), (0 << 3),
	  0x23, 0x7f, 500000, 10000, 1220000, 20000 },
	{ "dcdc5", 0x10, (1 << 4), (1 << 4), (0 << 4),
	  0x24, 0x7f, 800000, 10000, 1140000, 20000 },
	{ "dcdc6", 0x10, (1 << 5), (1 << 5), (0 << 5),
	  0x25, 0x7f, 600000, 10000, 1120000, 20000 },
	{ "dc1sw", 0x12, (1 << 7), (1 << 7), (0 << 7) },
	{ "aldo1", 0x13, (1 << 5), (1 << 5), (0 << 5),
	  0x28, 0x1f, 700000, 100000 },
	{ "aldo2", 0x13, (1 << 6), (1 << 6), (0 << 6),
	  0x29, 0x1f, 700000, 100000 },
	{ "aldo3", 0x13, (1 << 7), (1 << 7), (0 << 7),
	  0x2a, 0x1f, 700000, 100000 },
	{ "dldo1", 0x12, (1 << 3), (1 << 3), (0 << 3),
	  0x15, 0x1f, 700000, 100000 },
	{ "dldo2", 0x12, (1 << 4), (1 << 4), (0 << 4),
	  0x16, 0x1f, 700000, 100000, 3400000, 200000 },
	{ "dldo3", 0x12, (1 << 5), (1 << 5), (0 << 5),
	  0x17, 0x1f, 700000, 100000 },
	{ "dldo4", 0x12, (1 << 6), (1 << 6), (0 << 6),
	  0x18, 0x1f, 700000, 100000 },
	{ "eldo1", 0x12, (1 << 0), (1 << 0), (0 << 0),
	  0x19, 0x1f, 700000, 50000 },
	{ "eldo2", 0x12, (1 << 1), (1 << 1), (0 << 1),
	  0x1a, 0x1f, 700000, 50000 },
	{ "eldo3", 0x12, (1 << 2), (1 << 2), (0 << 2),
	  0x1b, 0x1f, 700000, 50000 },
	{ "fldo1", 0x13, (1 << 2), (1 << 2), (0 << 2),
	  0x1c, 0x0f, 700000, 50000 },
	{ "fldo2", 0x13, (1 << 3), (1 << 3), (0 << 3),
	  0x1d, 0x0f, 700000, 50000 },
	{ "ldo-io0", 0x90, 0x07, 0x03, 0x04,
	  0x91, 0x1f, 700000, 100000 },
	{ "ldo-io1", 0x92, 0x07, 0x03, 0x04,
	  0x93, 0x1f, 700000, 100000 },
	{ NULL }
};

struct axppmic_regdata axp806_regdata[] = {
	{ "dcdca", 0x10, (1 << 0), (1 << 0), (0 << 0),
	  0x12, 0x7f, 600000, 10000, 1120000, 20000 },
	{ "dcdcb", 0x10, (1 << 1), (1 << 1), (0 << 1),
	  0x13, 0x1f, 1000000, 50000 },
	{ "dcdcc", 0x10, (1 << 2), (1 << 2), (0 << 2),
	  0x14, 0x7f, 600000, 10000, 1120000, 20000 },
	{ "dcdcd", 0x10, (1 << 3), (1 << 3), (0 << 3),
	  0x15, 0x3f, 600000, 20000, 1600000, 100000 },
	{ "dcdce", 0x10, (1 << 4), (1 << 4), (0 << 4),
	  0x16, 0x1f, 1100000, 100000 },
	{ "aldo1", 0x10, (1 << 5), (1 << 5), (0 << 5),
	  0x17, 0x1f, 700000, 100000 },
	{ "aldo2", 0x10, (1 << 6), (1 << 6), (0 << 6),
	  0x18, 0x1f, 700000, 100000 },
	{ "aldo3", 0x10, (1 << 7), (1 << 7), (0 << 7),
	  0x19, 0x1f, 700000, 100000 },
	{ "bldo1", 0x11, (1 << 0), (1 << 0), (0 << 0),
	  0x20, 0x0f, 700000, 100000 },
	{ "bldo2", 0x11, (1 << 1), (1 << 1), (0 << 1),
	  0x21, 0x0f, 700000, 100000 },
	{ "bldo3", 0x11, (1 << 2), (1 << 2), (0 << 2),
	  0x22, 0x0f, 700000, 100000 },
	{ "bldo4", 0x11, (1 << 3), (1 << 3), (0 << 3),
	  0x23, 0x0f, 700000, 100000 },
	{ "cldo1", 0x11, (1 << 4), (1 << 4), (0 << 4),
	  0x24, 0x1f, 700000, 100000 },
	{ "cldo2", 0x11, (1 << 5), (1 << 5), (0 << 5),
	  0x25, 0x1f, 700000, 100000, 3600000, 200000 },
	{ "cldo3", 0x11, (1 << 6), (1 << 6), (0 << 6),
	  0x26, 0x1f, 700000, 100000 },
	{ "sw", 0x11, (1 << 7), (1 << 7), (0 << 7) },
	{ NULL }
};

struct axppmic_regdata axp809_regdata[] = {
	{ "dcdc1", 0x10, (1 << 1), (1 << 1), (0 << 1),
	  0x21, 0x1f, 1600000, 100000 },
	{ "dcdc2", 0x10, (1 << 2), (1 << 2), (0 << 2),
	  0x22, 0x3f, 600000, 20000 },
	{ "dcdc3", 0x10, (1 << 3), (1 << 3), (0 << 3),
	  0x23, 0x3f, 600000, 20000 },
	{ "dcdc4", 0x10, (1 << 4), (1 << 4), (0 << 4),
	  0x24, 0x3f, 600000, 20000, 1800000, 100000 },
	{ "dcdc5", 0x10, (1 << 5), (1 << 5), (0 << 5),
	  0x25, 0x1f, 1000000, 50000 },
	{ "dc5ldo", 0x10, (1 << 0), (1 << 0), (0 << 0),
	  0x1c, 0x07, 700000, 100000 },
	{ "aldo1", 0x10, (1 << 6), (1 << 6), (0 << 6),
	  0x28, 0x1f, 700000, 100000 },
	{ "aldo2", 0x10, (1 << 7), (1 << 7), (0 << 7),
	  0x29, 0x1f, 700000, 100000 },
	{ "aldo3", 0x12, (1 << 5), (1 << 5), (0 << 5),
	  0x2a, 0x1f, 700000, 100000 },
	{ "dldo1", 0x12, (1 << 3), (1 << 3), (0 << 3),
	  0x15, 0x1f, 700000, 100000 },
	{ "dldo2", 0x12, (1 << 4), (1 << 4), (0 << 4),
	  0x16, 0x1f, 700000, 100000 },
	{ "eldo1", 0x12, (1 << 0), (1 << 0), (0 << 0),
	  0x19, 0x1f, 700000, 100000 },
	{ "eldo2", 0x12, (1 << 1), (1 << 1), (0 << 1),
	  0x1a, 0x1f, 700000, 100000 },
	{ "eldo3", 0x12, (1 << 2), (1 << 2), (0 << 2),
	  0x1b, 0x1f, 700000, 100000 },
	{ "ldo_io0", 0x90, 0x07, 0x03, 0x04,
	  0x91, 0x1f, 700000, 100000 },
	{ "ldo_io1", 0x92, 0x07, 0x03, 0x04,
	  0x93, 0x1f, 700000, 100000 },
	{ NULL }
};

/* Sensors for AXP209 and AXP221/AXP809. */

#define AXPPMIC_NSENSORS 8

struct axppmic_sensdata {
	const char *name;
	enum sensor_type type;
	uint8_t reg;
	uint64_t base, delta;
};

struct axppmic_sensdata axp209_sensdata[] = {
	{ "ACIN", SENSOR_INDICATOR, 0x00, (1 << 7), (1 << 6) },
	{ "VBUS", SENSOR_INDICATOR, 0x00, (1 << 5), (1 << 4) },
	{ "ACIN", SENSOR_VOLTS_DC, 0x56, 0, 1700 },
	{ "ACIN", SENSOR_AMPS, 0x58, 0, 625 },
	{ "VBUS", SENSOR_VOLTS_DC, 0x5a, 0, 1700 },
	{ "VBUS", SENSOR_AMPS, 0x5c, 0, 375 },
	{ "", SENSOR_TEMP, 0x5e, 128450000, 100000 },
	{ "APS", SENSOR_VOLTS_DC, 0x7e, 0, 1400 },
	{ NULL }
};

struct axppmic_sensdata axp221_sensdata[] = {
	{ "ACIN", SENSOR_INDICATOR, 0x00, (1 << 7), (1 << 6) },
	{ "VBUS", SENSOR_INDICATOR, 0x00, (1 << 5), (1 << 4) },
	{ "", SENSOR_TEMP, 0x56, 5450000, 105861 },
	{ NULL }
};

struct axppmic_sensdata axp803_sensdata[] = {
	{ "ACIN", SENSOR_INDICATOR, 0x00, (1 << 7), (1 << 6) },
	{ "VBUS", SENSOR_INDICATOR, 0x00, (1 << 5), (1 << 4) },
	{ "", SENSOR_TEMP, 0x56, 5450000, 106250 },
	{ NULL }
};

struct axppmic_device {
	const char *name;
	const char *chip;
	struct axppmic_regdata *regdata;
	struct axppmic_sensdata *sensdata;
};

struct axppmic_device axppmic_devices[] = {
	{ "x-powers,axp152", "AXP152" },
	{ "x-powers,axp209", "AXP209", axp209_regdata, axp209_sensdata },
	{ "x-powers,axp221", "AXP221", axp221_regdata, axp221_sensdata },
	{ "x-powers,axp223", "AXP223", axp221_regdata, axp221_sensdata },
	{ "x-powers,axp803", "AXP803", axp803_regdata, axp803_sensdata },
	{ "x-powers,axp806", "AXP806", axp806_regdata },
	{ "x-powers,axp809", "AXP809", axp809_regdata, axp221_sensdata }
};

const struct axppmic_device *
axppmic_lookup(const char *name)
{
	int i;

	for (i = 0; i < nitems(axppmic_devices); i++) {
		if (strcmp(name, axppmic_devices[i].name) == 0)
			return &axppmic_devices[i];
	}

	return NULL;
}

struct axppmic_softc {
	struct device	sc_dev;
	void		*sc_cookie;
	uint16_t 	sc_addr;

	uint8_t		(*sc_read)(struct axppmic_softc *, uint8_t);
	void		(*sc_write)(struct axppmic_softc *, uint8_t, uint8_t);
	struct axppmic_regdata *sc_regdata;
	struct axppmic_sensdata *sc_sensdata;

	struct ksensor	sc_sensor[AXPPMIC_NSENSORS];
	struct ksensordev sc_sensordev;
};

inline uint8_t
axppmic_read_reg(struct axppmic_softc *sc, uint8_t reg)
{
	return sc->sc_read(sc, reg);
}

inline void
axppmic_write_reg(struct axppmic_softc *sc, uint8_t reg, uint8_t value)
{
	sc->sc_write(sc, reg, value);
}

void	axppmic_attach_common(struct axppmic_softc *, const char *, int);

/* I2C interface */

int	axppmic_i2c_match(struct device *, void *, void *);
void	axppmic_i2c_attach(struct device *, struct device *, void *);

struct cfattach axppmic_ca = {
	sizeof(struct axppmic_softc), axppmic_i2c_match, axppmic_i2c_attach
};

struct cfdriver axppmic_cd = {
	NULL, "axppmic", DV_DULL
};

uint8_t	axppmic_i2c_read(struct axppmic_softc *, uint8_t);
void	axppmic_i2c_write(struct axppmic_softc *, uint8_t, uint8_t);

int
axppmic_i2c_match(struct device *parent, void *match, void *aux)
{
	struct i2c_attach_args *ia = aux;

	if (axppmic_lookup(ia->ia_name))
		return 1;
	return 0;
}

void
axppmic_i2c_attach(struct device *parent, struct device *self, void *aux)
{
	struct axppmic_softc *sc = (struct axppmic_softc *)self;
	struct i2c_attach_args *ia = aux;
	int node = *(int *)ia->ia_cookie;

	sc->sc_cookie = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;
	sc->sc_read = axppmic_i2c_read;
	sc->sc_write = axppmic_i2c_write;

	axppmic_attach_common(sc, ia->ia_name, node);
}

uint8_t
axppmic_i2c_read(struct axppmic_softc *sc, uint8_t reg)
{
	i2c_tag_t tag = sc->sc_cookie;
	int flags = cold ? I2C_F_POLL : 0;
	int error;
	uint8_t value;

	iic_acquire_bus(tag, flags);
	error = iic_smbus_read_byte(tag, sc->sc_addr, reg, &value, flags);
	iic_release_bus(tag, flags);
	if (error) {
		printf("%s: SMBus read byte from 0x%02x failed\n",
		    sc->sc_dev.dv_xname, reg);
		return 0xff;
	}

	return value;
}

void
axppmic_i2c_write(struct axppmic_softc *sc, uint8_t reg, uint8_t value)
{
	i2c_tag_t tag = sc->sc_cookie;
	int flags = cold ? I2C_F_POLL : 0;
	int error;

	iic_acquire_bus(tag, flags);
	error = iic_smbus_write_byte(tag, sc->sc_addr, reg, value, flags);
	iic_release_bus(tag, flags);
	if (error)
		printf("%s: SMBus write byte to 0x%02x failed\n",
		    sc->sc_dev.dv_xname, reg);
}

/* RSB interface */

int	axppmic_rsb_match(struct device *, void *, void *);
void	axppmic_rsb_attach(struct device *, struct device *, void *);

struct cfattach axppmic_rsb_ca = {
	sizeof(struct axppmic_softc), axppmic_rsb_match, axppmic_rsb_attach
};

struct cfdriver axppmic_rsb_cd = {
	NULL, "axppmic", DV_DULL
};

uint8_t	axppmic_rsb_read(struct axppmic_softc *, uint8_t);
void	axppmic_rsb_write(struct axppmic_softc *, uint8_t, uint8_t);

int
axppmic_rsb_match(struct device *parent, void *match, void *aux)
{
	struct rsb_attach_args *ra = aux;

	if (axppmic_lookup(ra->ra_name))
		return 1;
	return 0;
}

void
axppmic_rsb_attach(struct device *parent, struct device *self, void *aux)
{
	struct axppmic_softc *sc = (struct axppmic_softc *)self;
	struct rsb_attach_args *ra = aux;

	sc->sc_cookie = ra->ra_cookie;
	sc->sc_addr = ra->ra_rta;
	sc->sc_read = axppmic_rsb_read;
	sc->sc_write = axppmic_rsb_write;

	axppmic_attach_common(sc, ra->ra_name, ra->ra_node);
}

uint8_t
axppmic_rsb_read(struct axppmic_softc *sc, uint8_t reg)
{
	return rsb_read_1(sc->sc_cookie, sc->sc_addr, reg);
}

void
axppmic_rsb_write(struct axppmic_softc *sc, uint8_t reg, uint8_t value)
{
	rsb_write_1(sc->sc_cookie, sc->sc_addr, reg, value);
}

/* Common code */

void	axppmic_attach_regulators(struct axppmic_softc *, int);
void	axppmic_attach_sensors(struct axppmic_softc *);

struct axppmic_softc *axppmic_sc;
void	axp209_powerdown(void);

void
axppmic_attach_common(struct axppmic_softc *sc, const char *name, int node)
{
	const struct axppmic_device *device;

	device = axppmic_lookup(name);
	printf(": %s\n", device->chip);

	sc->sc_regdata = device->regdata;
	sc->sc_sensdata = device->sensdata;

	/* Switch AXP806 into master or slave mode. */
	if (strcmp(name, "x-powers,axp806") == 0) {
	    if (OF_getproplen(node, "x-powers,master-mode") == 0) {
			axppmic_write_reg(sc, AXP806_REG_ADDR_EXT,
			    AXP806_REG_ADDR_EXT_MASTER_MODE);
		} else {
			axppmic_write_reg(sc, AXP806_REG_ADDR_EXT,
			    AXP806_REG_ADDR_EXT_SLAVE_MODE);
		}
	}

	/* Enable data collecton on AXP209. */
	if (strcmp(name, "x-powers,axp209") == 0) {
		uint8_t reg;

		/* Turn on sampling of ACIN and VBUS voltage and current. */
		reg = axppmic_read_reg(sc, AXP209_ADC_EN1);
		reg |= AXP209_ADC_EN1_ACIN;
		reg |= AXP209_ADC_EN1_VBUS;
		axppmic_write_reg(sc, AXP209_ADC_EN1, reg);
	}

	if (sc->sc_regdata)
		axppmic_attach_regulators(sc, node);

	if (sc->sc_sensdata)
		axppmic_attach_sensors(sc);

#ifdef __armv7__
	if (strcmp(name, "x-powers,axp152") == 0 ||
	    strcmp(name, "x-powers,axp209") == 0) {
		axppmic_sc = sc;
		powerdownfn = axp209_powerdown;
	}
#endif
}

/* Regulators */

struct axppmic_regulator {
	struct axppmic_softc *ar_sc;

	uint8_t ar_ereg, ar_emask;
	uint8_t ar_eval, ar_dval;

	uint8_t ar_vreg, ar_vmask;
	uint32_t ar_base, ar_delta;
	uint32_t ar_base2, ar_delta2;

	struct regulator_device ar_rd;
};

void	axppmic_attach_regulator(struct axppmic_softc *, int);
uint32_t axppmic_get_voltage(void *);
int	axppmic_set_voltage(void *, uint32_t);
int	axppmic_enable(void *, int);

void
axppmic_attach_regulators(struct axppmic_softc *sc, int node)
{
	node = OF_getnodebyname(node, "regulators");
	if (node == 0)
		return;

	for (node = OF_child(node); node; node = OF_peer(node))
		axppmic_attach_regulator(sc, node);
}

void
axppmic_attach_regulator(struct axppmic_softc *sc, int node)
{
	struct axppmic_regulator *ar;
	char name[32];
	int i;

	name[0] = 0;
	OF_getprop(node, "name", name, sizeof(name));
	name[sizeof(name) - 1] = 0;
	for (i = 0; sc->sc_regdata[i].name; i++) {
		if (strcmp(sc->sc_regdata[i].name, name) == 0)
			break;
	}
	if (sc->sc_regdata[i].name == NULL)
		return;

	ar = malloc(sizeof(*ar), M_DEVBUF, M_WAITOK | M_ZERO);
	ar->ar_sc = sc;

	ar->ar_ereg = sc->sc_regdata[i].ereg;
	ar->ar_emask = sc->sc_regdata[i].emask;
	ar->ar_eval = sc->sc_regdata[i].eval;
	ar->ar_dval = sc->sc_regdata[i].dval;
	ar->ar_vreg = sc->sc_regdata[i].vreg;
	ar->ar_vmask = sc->sc_regdata[i].vmask;
	ar->ar_base = sc->sc_regdata[i].base;
	ar->ar_delta = sc->sc_regdata[i].delta;

	ar->ar_rd.rd_node = node;
	ar->ar_rd.rd_cookie = ar;
	ar->ar_rd.rd_get_voltage = axppmic_get_voltage;
	ar->ar_rd.rd_set_voltage = axppmic_set_voltage;
	ar->ar_rd.rd_enable = axppmic_enable;
	regulator_register(&ar->ar_rd);
}

uint32_t
axppmic_get_voltage(void *cookie)
{
	struct axppmic_regulator *ar = cookie;
	uint32_t voltage;
	uint8_t value;

	value = axppmic_read_reg(ar->ar_sc, ar->ar_vreg);
	value &= ar->ar_vmask;
	voltage = ar->ar_base + value * ar->ar_delta;
	if (ar->ar_base2 > 0 && voltage > ar->ar_base2) {
		value -= (ar->ar_base2 - ar->ar_base) / ar->ar_delta;
		voltage = ar->ar_base2 + value * ar->ar_delta2;
	}
	return voltage;
}

int
axppmic_set_voltage(void *cookie, uint32_t voltage)
{
	struct axppmic_regulator *ar = cookie;
	uint32_t value, reg;

	if (voltage < ar->ar_base)
		return EINVAL;
	value = (voltage - ar->ar_base) / ar->ar_delta;
	if (ar->ar_base2 > 0 && voltage > ar->ar_base2) {
		value = (ar->ar_base2 - ar->ar_base) / ar->ar_delta;
		value += (voltage - ar->ar_base2) / ar->ar_delta2;
	}
	if (value > ar->ar_vmask)
		return EINVAL;

	reg = axppmic_read_reg(ar->ar_sc, ar->ar_vreg);
	reg &= ar->ar_vmask;
	axppmic_write_reg(ar->ar_sc, ar->ar_vreg, reg | value);
	return 0;
}

int
axppmic_enable(void *cookie, int on)
{
	struct axppmic_regulator *ar = cookie;
	uint8_t reg;

	reg = axppmic_read_reg(ar->ar_sc, ar->ar_ereg);
	reg &= ~ar->ar_emask;
	if (on)
		reg |= ar->ar_eval;
	else
		reg |= ar->ar_dval;
	axppmic_write_reg(ar->ar_sc, ar->ar_ereg, reg);
	return 0;
}

/* Sensors */

void	axppmic_update_sensors(void *);
void	axppmic_update_indicator(struct axppmic_softc *, int);
void	axppmic_update_sensor(struct axppmic_softc *, int);

void
axppmic_attach_sensors(struct axppmic_softc *sc)
{
	int i;

	for (i = 0; sc->sc_sensdata[i].name; i++) {
		KASSERT(i < AXPPMIC_NSENSORS);

		sc->sc_sensor[i].type = sc->sc_sensdata[i].type;
		strlcpy(sc->sc_sensor[i].desc, sc->sc_sensdata[i].name,
		    sizeof(sc->sc_sensor[i].desc));
		sensor_attach(&sc->sc_sensordev, &sc->sc_sensor[i]);
	}

	axppmic_update_sensors(sc);
	if (sensor_task_register(sc, axppmic_update_sensors, 5) == NULL) {
		printf(", unable to register update task\n");
		return;
	}

	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
	    sizeof(sc->sc_sensordev.xname));
	sensordev_install(&sc->sc_sensordev);
}

void
axppmic_update_sensors(void *arg)
{
	struct axppmic_softc *sc = arg;
	int i;

	for (i = 0; sc->sc_sensdata[i].name; i++) {
		if (sc->sc_sensdata[i].type == SENSOR_INDICATOR)
			axppmic_update_indicator(sc, i);
		else
			axppmic_update_sensor(sc, i);
	}
}

void
axppmic_update_indicator(struct axppmic_softc *sc, int i)
{
	uint8_t reg = sc->sc_sensdata[i].reg;
	uint8_t mask = sc->sc_sensdata[i].base;
	uint8_t mask_ok = sc->sc_sensdata[i].delta;
	uint8_t value;

	value = axppmic_read_reg(sc, reg);
	sc->sc_sensor[i].value = (value & mask) ? 1 : 0;
	if (value & mask) {
		sc->sc_sensor[i].status =
		    (value & mask_ok) ? SENSOR_S_OK : SENSOR_S_WARN;
	} else {
		sc->sc_sensor[i].status = SENSOR_S_UNSPEC;
	}
}

void
axppmic_update_sensor(struct axppmic_softc *sc, int i)
{
	uint8_t reg = sc->sc_sensdata[i].reg;
	uint64_t base = sc->sc_sensdata[i].base;
	uint64_t delta = sc->sc_sensdata[i].delta;
	uint16_t value;

	value = axppmic_read_reg(sc, reg);
	value = (value << 4) | axppmic_read_reg(sc, reg + 1);
	sc->sc_sensor[i].value = base + value * delta;
}

void
axp209_powerdown(void)
{
	axppmic_write_reg(axppmic_sc, AXP209_SDR, AXP209_SDR_SHUTDOWN);
}