Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/* $NetBSD: as3722.c,v 1.24 2021/08/07 16:19:11 thorpej Exp $ */

/*-
 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "opt_fdt.h"

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: as3722.c,v 1.24 2021/08/07 16:19:11 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kmem.h>
#include <sys/wdog.h>

#include <dev/clock_subr.h>

#include <dev/sysmon/sysmonvar.h>

#include <dev/i2c/i2cvar.h>
#include <dev/i2c/as3722.h>

#ifdef FDT
#include <dev/fdt/fdtvar.h>
#endif

#define	AS3722_I2C_ADDR			0x40

#define AS3722_START_YEAR		2000

#define AS3722_SD0_VOLTAGE_REG		0x00

#define AS3722_SD4_VOLTAGE_REG		0x04

#define AS3722_GPIO0_CTRL_REG		0x08
#define AS3722_GPIO0_CTRL_INVERT	__BIT(7)
#define AS3722_GPIO0_CTRL_IOSF		__BITS(6,3)
#define AS3722_GPIO0_CTRL_IOSF_GPIO	0
#define AS3722_GPIO0_CTRL_IOSF_WATCHDOG	9
#define AS3722_GPIO0_CTRL_MODE		__BITS(2,0)
#define AS3722_GPIO0_CTRL_MODE_PULLDOWN	5

#define AS3722_LDO6_VOLTAGE_REG		0x16

#define AS3722_RESET_CTRL_REG		0x36
#define AS3722_RESET_CTRL_POWER_OFF	__BIT(1)
#define AS3722_RESET_CTRL_FORCE_RESET	__BIT(0)

#define AS3722_WATCHDOG_CTRL_REG	0x38
#define AS3722_WATCHDOG_CTRL_MODE	__BITS(2,1)
#define AS3722_WATCHDOG_CTRL_ON		__BIT(0)

#define AS3722_WATCHDOG_TIMER_REG	0x46
#define AS3722_WATCHDOG_TIMER_TIMER	__BITS(6,0)

#define AS3722_WATCHDOG_SIGNAL_REG	0x48
#define AS3722_WATCHDOG_SIGNAL_PWM_DIV	__BITS(7,6)
#define AS3722_WATCHDOG_SIGNAL_SW_SIG	__BIT(0)

#define AS3722_SDCONTROL_REG		0x4d
#define AS3722_SDCONTROL_SD4_ENABLE	__BIT(4)

#define AS3722_LDOCONTROL0_REG		0x4e

#define AS3722_RTC_CONTROL_REG		0x60
#define AS3722_RTC_CONTROL_RTC_ON	__BIT(2)

#define AS3722_RTC_SECOND_REG		0x61
#define AS3722_RTC_MINUTE_REG		0x62
#define AS3722_RTC_HOUR_REG		0x63
#define AS3722_RTC_DAY_REG		0x64
#define AS3722_RTC_MONTH_REG		0x65
#define AS3722_RTC_YEAR_REG		0x66
#define AS3722_RTC_ACCESS_REG		0x6f

#define AS3722_ASIC_ID1_REG		0x90
#define AS3722_ASIC_ID2_REG		0x91

#define AS3722_FUSE7_REG		0xa7
#define AS3722_FUSE7_SD0_V_MINUS_200MV	__BIT(4)

struct as3722_softc {
	device_t	sc_dev;
	i2c_tag_t	sc_i2c;
	i2c_addr_t	sc_addr;
	int		sc_phandle;
	int		sc_flags;
#define AS3722_FLAG_SD0_V_MINUS_200MV 0x01

	struct sysmon_wdog sc_smw;
	struct todr_chip_handle sc_todr;
};

#ifdef FDT
static int	as3722reg_set_voltage_sd0(device_t, u_int, u_int);
static int	as3722reg_get_voltage_sd0(device_t, u_int *);
static int	as3722reg_set_voltage_sd4(device_t, u_int, u_int);
static int	as3722reg_get_voltage_sd4(device_t, u_int *);
static int	as3722reg_set_voltage_ldo(device_t, u_int, u_int);
static int	as3722reg_get_voltage_ldo(device_t, u_int *);

static const struct as3722regdef {
	const char	*name;
	u_int		vsel_reg;
	u_int		vsel_mask;
	u_int		enable_reg;
	u_int		enable_mask;
	int		(*set)(device_t, u_int, u_int);
	int		(*get)(device_t, u_int *);
} as3722regdefs[] = {
	{ .name = "sd0",
	  .vsel_reg = AS3722_SD0_VOLTAGE_REG,
	  .vsel_mask = 0x7f,
	  .set = as3722reg_set_voltage_sd0,
	  .get = as3722reg_get_voltage_sd0 },
	{ .name = "sd4",
	  .vsel_reg = AS3722_SD4_VOLTAGE_REG,
	  .vsel_mask = 0x7f,
	  .enable_reg = AS3722_SDCONTROL_REG,
	  .enable_mask = AS3722_SDCONTROL_SD4_ENABLE,
	  .set = as3722reg_set_voltage_sd4,
	  .get = as3722reg_get_voltage_sd4 },
	{ .name = "ldo6",
	  .vsel_reg = AS3722_LDO6_VOLTAGE_REG,
	  .vsel_mask = 0x7f,
	  .enable_reg = AS3722_LDOCONTROL0_REG,
	  .enable_mask = 0x40,
	  .set = as3722reg_set_voltage_ldo,
	  .get = as3722reg_get_voltage_ldo },
};

struct as3722reg_softc {
	device_t	sc_dev;
	int		sc_phandle;
	const struct as3722regdef *sc_regdef;
};

struct as3722reg_attach_args {
	const struct as3722regdef *reg_def;
	int		reg_phandle;
};
#endif

#define AS3722_WATCHDOG_DEFAULT_PERIOD	10

static int	as3722_match(device_t, cfdata_t, void *);
static void	as3722_attach(device_t, device_t, void *);

static void	as3722_wdt_attach(struct as3722_softc *);
static int	as3722_wdt_setmode(struct sysmon_wdog *);
static int	as3722_wdt_tickle(struct sysmon_wdog *);

static void	as3722_rtc_attach(struct as3722_softc *);
static int	as3722_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
static int	as3722_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);

#ifdef FDT
static void	as3722_regulator_attach(struct as3722_softc *);
static int	as3722reg_match(device_t, cfdata_t, void *);
static void	as3722reg_attach(device_t, device_t, void *);

static int	as3722reg_acquire(device_t);
static void	as3722reg_release(device_t);
static int	as3722reg_enable(device_t, bool);
static int	as3722reg_set_voltage(device_t, u_int, u_int);
static int	as3722reg_get_voltage(device_t, u_int *);

static struct fdtbus_regulator_controller_func as3722reg_funcs = {
	.acquire = as3722reg_acquire,
	.release = as3722reg_release,
	.enable = as3722reg_enable,
	.set_voltage = as3722reg_set_voltage,
	.get_voltage = as3722reg_get_voltage,
};

static void	as3722_power_reset(device_t);
static void	as3722_power_poweroff(device_t);

static struct fdtbus_power_controller_func as3722power_funcs = {
	.reset = as3722_power_reset,
	.poweroff = as3722_power_poweroff,
};
#endif

static int	as3722_read(struct as3722_softc *, uint8_t, uint8_t *, int);
static int	as3722_write(struct as3722_softc *, uint8_t, uint8_t, int);
static int	as3722_set_clear(struct as3722_softc *, uint8_t, uint8_t,
				 uint8_t, int);

CFATTACH_DECL_NEW(as3722pmic, sizeof(struct as3722_softc),
    as3722_match, as3722_attach, NULL, NULL);

#ifdef FDT
CFATTACH_DECL_NEW(as3722reg, sizeof(struct as3722reg_softc),
    as3722reg_match, as3722reg_attach, NULL, NULL);
#endif

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "ams,as3722" },
	DEVICE_COMPAT_EOL
};

static int
as3722_match(device_t parent, cfdata_t match, void *aux)
{
	struct i2c_attach_args *ia = aux;
	uint8_t reg, id1;
	int error, match_result;

	if (iic_use_direct_match(ia, match, compat_data, &match_result))
		return match_result;
	
	if (ia->ia_addr != AS3722_I2C_ADDR)
		return 0;
	
	iic_acquire_bus(ia->ia_tag, 0);
	reg = AS3722_ASIC_ID1_REG;
	error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
	    &reg, 1, &id1, 1, 0);
	iic_release_bus(ia->ia_tag, 0);

	if (error == 0 && id1 == 0x0c)
		return I2C_MATCH_ADDRESS_AND_PROBE;

	return 0;
}

static void
as3722_attach(device_t parent, device_t self, void *aux)
{
	struct as3722_softc * const sc = device_private(self);
	struct i2c_attach_args *ia = aux;

	sc->sc_dev = self;
	sc->sc_i2c = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;
	sc->sc_phandle = ia->ia_cookie;

	aprint_naive("\n");
	aprint_normal(": AMS AS3722\n");

	as3722_wdt_attach(sc);
	as3722_rtc_attach(sc);
#ifdef FDT
	as3722_regulator_attach(sc);

	fdtbus_register_power_controller(self, sc->sc_phandle,
	    &as3722power_funcs);
#endif
}

static void
as3722_wdt_attach(struct as3722_softc *sc)
{
	int error;

	iic_acquire_bus(sc->sc_i2c, 0);
	error = as3722_write(sc, AS3722_GPIO0_CTRL_REG,
	    __SHIFTIN(AS3722_GPIO0_CTRL_IOSF_GPIO,
		      AS3722_GPIO0_CTRL_IOSF) |
	    __SHIFTIN(AS3722_GPIO0_CTRL_MODE_PULLDOWN,
		      AS3722_GPIO0_CTRL_MODE),
	    0);
	error += as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
	    __SHIFTIN(1, AS3722_WATCHDOG_CTRL_MODE), 0, 0);
	iic_release_bus(sc->sc_i2c, 0);

	if (error) {
		aprint_error_dev(sc->sc_dev, "couldn't setup watchdog\n");
		return;
	}

	sc->sc_smw.smw_name = device_xname(sc->sc_dev);
	sc->sc_smw.smw_cookie = sc;
	sc->sc_smw.smw_setmode = as3722_wdt_setmode;
	sc->sc_smw.smw_tickle = as3722_wdt_tickle;
	sc->sc_smw.smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;

	aprint_normal_dev(sc->sc_dev, "default watchdog period is %u seconds\n",
	    sc->sc_smw.smw_period);

	if (sysmon_wdog_register(&sc->sc_smw) != 0)
		aprint_error_dev(sc->sc_dev, "couldn't register with sysmon\n");
}

static void
as3722_rtc_attach(struct as3722_softc *sc)
{
	int error;

	iic_acquire_bus(sc->sc_i2c, 0);
	error = as3722_set_clear(sc, AS3722_RTC_CONTROL_REG,
	    AS3722_RTC_CONTROL_RTC_ON, 0, 0);
	iic_release_bus(sc->sc_i2c, 0);

	if (error) {
		aprint_error_dev(sc->sc_dev, "couldn't setup RTC\n");
		return;
	}

	sc->sc_todr.todr_gettime_ymdhms = as3722_rtc_gettime;
	sc->sc_todr.todr_settime_ymdhms = as3722_rtc_settime;
	sc->sc_todr.cookie = sc;
#ifdef FDT
	fdtbus_todr_attach(sc->sc_dev, sc->sc_phandle, &sc->sc_todr);
#else
	todr_attach(&sc->sc_todr);
#endif
}

static int
as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
{
	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
	    &reg, 1, val, 1, flags);
}

static int
as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
{
	uint8_t buf[2] = { reg, val };
	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
	    NULL, 0, buf, 2, flags);
}

static int
as3722_set_clear(struct as3722_softc *sc, uint8_t reg, uint8_t set,
    uint8_t clr, int flags)
{
	uint8_t old, new;
	int error;

	error = as3722_read(sc, reg, &old, flags);
	if (error) {
		return error;
	}
	new = set | (old & ~clr);

	return as3722_write(sc, reg, new, flags);
}

static int
as3722_wdt_setmode(struct sysmon_wdog *smw)
{
	struct as3722_softc * const sc = smw->smw_cookie;
	int error;

	const int flags = 0;

	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
		iic_acquire_bus(sc->sc_i2c, flags);
		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
		    0, AS3722_WATCHDOG_CTRL_ON, flags);
		iic_release_bus(sc->sc_i2c, flags);
		return error;
	}

	if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
		smw->smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
	}
	if (smw->smw_period < 1 || smw->smw_period > 128) {
		return EINVAL;
	}
	sc->sc_smw.smw_period = smw->smw_period;

	iic_acquire_bus(sc->sc_i2c, flags);
	error = as3722_set_clear(sc, AS3722_WATCHDOG_TIMER_REG,
	    __SHIFTIN(sc->sc_smw.smw_period - 1, AS3722_WATCHDOG_TIMER_TIMER),
	    AS3722_WATCHDOG_TIMER_TIMER, flags);
	if (error == 0) {
		error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
		    AS3722_WATCHDOG_CTRL_ON, 0, flags);
	}
	iic_release_bus(sc->sc_i2c, flags);

	return error;
}

static int
as3722_wdt_tickle(struct sysmon_wdog *smw)
{
	struct as3722_softc * const sc = smw->smw_cookie;
	int error;

	const int flags = 0;

	iic_acquire_bus(sc->sc_i2c, flags);
	error = as3722_set_clear(sc, AS3722_WATCHDOG_SIGNAL_REG,
	    AS3722_WATCHDOG_SIGNAL_SW_SIG, 0, flags);
	iic_release_bus(sc->sc_i2c, flags);

	return error;
}

static int
as3722_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
{
	struct as3722_softc * const sc = tch->cookie;
	uint8_t buf[6];
	int error = 0;

	const int flags = 0;

	iic_acquire_bus(sc->sc_i2c, flags);
	error += as3722_read(sc, AS3722_RTC_SECOND_REG, &buf[0], flags);
	error += as3722_read(sc, AS3722_RTC_MINUTE_REG, &buf[1], flags);
	error += as3722_read(sc, AS3722_RTC_HOUR_REG, &buf[2], flags);
	error += as3722_read(sc, AS3722_RTC_DAY_REG, &buf[3], flags);
	error += as3722_read(sc, AS3722_RTC_MONTH_REG, &buf[4], flags);
	error += as3722_read(sc, AS3722_RTC_YEAR_REG, &buf[5], flags);
	iic_release_bus(sc->sc_i2c, flags);

	if (error)
		return error;

	dt->dt_sec = bcdtobin(buf[0] & 0x7f);
	dt->dt_min = bcdtobin(buf[1] & 0x7f);
	dt->dt_hour = bcdtobin(buf[2] & 0x3f);
	dt->dt_day = bcdtobin(buf[3] & 0x3f);
	dt->dt_mon = bcdtobin(buf[4] & 0x1f) - 1;
	dt->dt_year = AS3722_START_YEAR + bcdtobin(buf[5] & 0x7f);
	dt->dt_wday = 0;

	return 0;
}

static int
as3722_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
{
	struct as3722_softc * const sc = tch->cookie;
	uint8_t buf[6];
	int error = 0;

	if (dt->dt_year < AS3722_START_YEAR)
		return EINVAL;

	buf[0] = bintobcd(dt->dt_sec) & 0x7f;
	buf[1] = bintobcd(dt->dt_min) & 0x7f;
	buf[2] = bintobcd(dt->dt_hour) & 0x3f;
	buf[3] = bintobcd(dt->dt_day) & 0x3f;
	buf[4] = bintobcd(dt->dt_mon + 1) & 0x1f;
	buf[5] = bintobcd(dt->dt_year - AS3722_START_YEAR) & 0x7f;

	const int flags = 0;

	iic_acquire_bus(sc->sc_i2c, flags);
	error += as3722_write(sc, AS3722_RTC_SECOND_REG, buf[0], flags);
	error += as3722_write(sc, AS3722_RTC_MINUTE_REG, buf[1], flags);
	error += as3722_write(sc, AS3722_RTC_HOUR_REG, buf[2], flags);
	error += as3722_write(sc, AS3722_RTC_DAY_REG, buf[3], flags);
	error += as3722_write(sc, AS3722_RTC_MONTH_REG, buf[4], flags);
	error += as3722_write(sc, AS3722_RTC_YEAR_REG, buf[5], flags);
	iic_release_bus(sc->sc_i2c, flags);

	return error;
}

#ifdef FDT
static void
as3722_regulator_attach(struct as3722_softc *sc)
{
	struct as3722reg_attach_args raa;
	int phandle, child;
	int error;
	const int flags = 0;
	uint8_t tmp;

	iic_acquire_bus(sc->sc_i2c, flags);
	error = as3722_read(sc, AS3722_FUSE7_REG, &tmp, flags);
	iic_release_bus(sc->sc_i2c, flags);
	if (error != 0) {
		aprint_error_dev(sc->sc_dev, "failed to read Fuse7: %d\n", error);
		return;
	}

	if (tmp & AS3722_FUSE7_SD0_V_MINUS_200MV)
		sc->sc_flags |= AS3722_FLAG_SD0_V_MINUS_200MV;

	phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
	if (phandle <= 0)
		return;

	for (int i = 0; i < __arraycount(as3722regdefs); i++) {
		const struct as3722regdef *regdef = &as3722regdefs[i];
		child = of_find_firstchild_byname(phandle, regdef->name);
		if (child <= 0)
			continue;
		raa.reg_def = regdef;
		raa.reg_phandle = child;
		config_found(sc->sc_dev, &raa, NULL, CFARGS_NONE);
	}
}

static int
as3722reg_match(device_t parent, cfdata_t match, void *aux)
{
	return 1;
}

static void
as3722reg_attach(device_t parent, device_t self, void *aux)
{
	struct as3722reg_softc *sc = device_private(self);
	struct as3722reg_attach_args *raa = aux;
	char *name = NULL;
	int len;

	sc->sc_dev = self;
	sc->sc_phandle = raa->reg_phandle;
	sc->sc_regdef = raa->reg_def;

	fdtbus_register_regulator_controller(self, sc->sc_phandle,
	    &as3722reg_funcs);

	len = OF_getproplen(sc->sc_phandle, "regulator-name");
	if (len > 0) {
		name = kmem_zalloc(len, KM_SLEEP);
		OF_getprop(sc->sc_phandle, "regulator-name", name, len);
	}

	aprint_naive("\n");
	if (name)
		aprint_normal(": %s\n", name);
	else
		aprint_normal("\n");

	if (name)
		kmem_free(name, len);
}

static int
as3722reg_acquire(device_t dev)
{
	return 0;
}

static void
as3722reg_release(device_t dev)
{
}

static int
as3722reg_enable(device_t dev, bool enable)
{
	struct as3722reg_softc *sc = device_private(dev);
	struct as3722_softc *asc = device_private(device_parent(dev));
	const struct as3722regdef *regdef = sc->sc_regdef;
	const int flags = 0;
	int error;

	if (!regdef->enable_mask)
		return enable ? 0 : EINVAL;

	iic_acquire_bus(asc->sc_i2c, flags);
	if (enable)
		error = as3722_set_clear(asc, regdef->enable_reg,
		    regdef->enable_mask, 0, flags);
	else
		error = as3722_set_clear(asc, regdef->enable_reg,
		    0, regdef->enable_mask, flags);
	iic_release_bus(asc->sc_i2c, flags);

	return error;
}

static int
as3722reg_set_voltage_ldo(device_t dev, u_int min_uvol, u_int max_uvol)
{
	struct as3722reg_softc *sc = device_private(dev);
	struct as3722_softc *asc = device_private(device_parent(dev));
	const struct as3722regdef *regdef = sc->sc_regdef;
	const int flags = 0;
	uint8_t set_v = 0x00;
	u_int uvol;
	int error;

	for (uint8_t v = 0x01; v <= 0x24; v++) {
		uvol = 800000 + (v * 25000);
		if (uvol >= min_uvol && uvol <= max_uvol) {
			set_v = v;
			goto done;
		}
	}
	for (uint8_t v = 0x40; v <= 0x7f; v++) {
		uvol = 1725000 + ((v - 0x40) * 25000);
		if (uvol >= min_uvol && uvol <= max_uvol) {
			set_v = v;
			goto done;
		}
	}
	if (set_v == 0)
		return ERANGE;

done:
	iic_acquire_bus(asc->sc_i2c, flags);
	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
	    regdef->vsel_mask, flags);
	iic_release_bus(asc->sc_i2c, flags);

	return error;
}

static int
as3722reg_get_voltage_ldo(device_t dev, u_int *puvol)
{
	struct as3722reg_softc *sc = device_private(dev);
	struct as3722_softc *asc = device_private(device_parent(dev));
	const struct as3722regdef *regdef = sc->sc_regdef;
	const int flags = 0;
	uint8_t v;
	int error;

	iic_acquire_bus(asc->sc_i2c, flags);
	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
	iic_release_bus(asc->sc_i2c, flags);
	if (error != 0)
		return error;

	v &= regdef->vsel_mask;

	if (v == 0)
		*puvol = 0;	/* LDO off */
	else if (v >= 0x01 && v <= 0x24)
		*puvol = 800000 + (v * 25000);
	else if (v >= 0x40 && v <= 0x7f)
		*puvol = 1725000 + ((v - 0x40) * 25000);
	else
		return EINVAL;

	return 0;
}

static int
as3722reg_set_voltage_sd0(device_t dev, u_int min_uvol, u_int max_uvol)
{
	struct as3722reg_softc *sc = device_private(dev);
	struct as3722_softc *asc = device_private(device_parent(dev));
	const struct as3722regdef *regdef = sc->sc_regdef;
	const int flags = 0;
	uint8_t set_v = 0x00;
	u_int uvol;
	int error;

	if (asc->sc_flags & AS3722_FLAG_SD0_V_MINUS_200MV) {
		for (uint8_t v = 0x01; v <= 0x6e; v++) {
			uvol = 400000 + (v * 10000);
			if (uvol >= min_uvol && uvol <= max_uvol) {
				set_v = v;
				goto done;
			}
		}
	} else {
		for (uint8_t v = 0x01; v <= 0x5a; v++) {
			uvol = 600000 + (v * 10000);
			if (uvol >= min_uvol && uvol <= max_uvol) {
				set_v = v;
				goto done;
			}
		}
	}
	if (set_v == 0)
		return ERANGE;

done:
	iic_acquire_bus(asc->sc_i2c, flags);
	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
	    regdef->vsel_mask, flags);
	iic_release_bus(asc->sc_i2c, flags);

	return error;
}

static int
as3722reg_get_voltage_sd0(device_t dev, u_int *puvol)
{
	struct as3722reg_softc *sc = device_private(dev);
	struct as3722_softc *asc = device_private(device_parent(dev));
	const struct as3722regdef *regdef = sc->sc_regdef;
	const int flags = 0;
	uint8_t v;
	int error;

	iic_acquire_bus(asc->sc_i2c, flags);
	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
	iic_release_bus(asc->sc_i2c, flags);
	if (error != 0)
		return error;

	v &= regdef->vsel_mask;

	if (v == 0) {
		*puvol = 0;	/* DC/DC powered down */
		return 0;
	}
	if (asc->sc_flags & AS3722_FLAG_SD0_V_MINUS_200MV) {
		if (v >= 0x01 && v <= 0x6e) {
			*puvol = 400000 + (v * 10000);
			return 0;
		}
	} else {
		if (v >= 0x01 && v <= 0x5a) {
			*puvol = 600000 + (v * 10000);
			return 0;
		}
	}

	return EINVAL;
}

static int
as3722reg_set_voltage_sd4(device_t dev, u_int min_uvol, u_int max_uvol)
{
	struct as3722reg_softc *sc = device_private(dev);
	struct as3722_softc *asc = device_private(device_parent(dev));
	const struct as3722regdef *regdef = sc->sc_regdef;
	const int flags = 0;
	uint8_t set_v = 0x00;
	u_int uvol;
	int error;


	for (uint8_t v = 0x01; v <= 0x40; v++) {
		uvol = 600000 + (v * 12500);
		if (uvol >= min_uvol && uvol <= max_uvol) {
			set_v = v;
			goto done;
		}
	}
	for (uint8_t v = 0x41; v <= 0x70; v++) {
		uvol = 1400000 + ((v - 0x40) * 25000);
		if (uvol >= min_uvol && uvol <= max_uvol) {
			set_v = v;
			goto done;
		}
	}
	for (uint8_t v = 0x71; v <= 0x7f; v++) {
		uvol = 2600000 + ((v - 0x70) * 50000);
		if (uvol >= min_uvol && uvol <= max_uvol) {
			set_v = v;
			goto done;
		}
	}
	if (set_v == 0)
		return ERANGE;

done:
	iic_acquire_bus(asc->sc_i2c, flags);
	error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
	    regdef->vsel_mask, flags);
	iic_release_bus(asc->sc_i2c, flags);

	return error;
}

static int
as3722reg_get_voltage_sd4(device_t dev, u_int *puvol)
{
	struct as3722reg_softc *sc = device_private(dev);
	struct as3722_softc *asc = device_private(device_parent(dev));
	const struct as3722regdef *regdef = sc->sc_regdef;
	const int flags = 0;
	uint8_t v;
	int error;

	iic_acquire_bus(asc->sc_i2c, flags);
	error = as3722_read(asc, regdef->vsel_reg, &v, flags);
	iic_release_bus(asc->sc_i2c, flags);
	if (error != 0)
		return error;

	v &= regdef->vsel_mask;

	if (v == 0)
		*puvol = 0;	/* DC/DC powered down */
	else if (v >= 0x01 && v <= 0x40)
		*puvol = 600000 + (v * 12500);
	else if (v >= 0x41 && v <= 0x70)
		*puvol = 1400000 + (v - 0x40) * 25000;
	else if (v >= 0x71 && v <= 0x7f)
		*puvol = 2600000 + (v - 0x70) * 50000;
	else
		return EINVAL;

	return 0;
}

static int
as3722reg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
{
	struct as3722reg_softc *sc = device_private(dev);
	const struct as3722regdef *regdef = sc->sc_regdef;

	return regdef->set(dev, min_uvol, max_uvol);
}

static int
as3722reg_get_voltage(device_t dev, u_int *puvol)
{
	struct as3722reg_softc *sc = device_private(dev);
	const struct as3722regdef *regdef = sc->sc_regdef;

	return regdef->get(dev, puvol);
}

static void
as3722_power_reset(device_t dev)
{
	delay(1000000);
	as3722_reboot(dev);
}

static void
as3722_power_poweroff(device_t dev)
{
	delay(1000000);
	as3722_poweroff(dev);
}
#endif

int
as3722_poweroff(device_t dev)
{
	struct as3722_softc * const sc = device_private(dev);
	int error;

	error = iic_acquire_bus(sc->sc_i2c, 0);
	if (error == 0) {
		error = as3722_write(sc, AS3722_RESET_CTRL_REG,
		    AS3722_RESET_CTRL_POWER_OFF, 0);
		iic_release_bus(sc->sc_i2c, 0);
	}
	if (error) {
		device_printf(dev, "WARNING: unable to power off, error %d\n",
		    error);
	}

	return error;
}

int
as3722_reboot(device_t dev)
{
	struct as3722_softc * const sc = device_private(dev);
	int error;

	error = iic_acquire_bus(sc->sc_i2c, 0);
	if (error == 0) {
		error = as3722_write(sc, AS3722_RESET_CTRL_REG,
		    AS3722_RESET_CTRL_FORCE_RESET, 0);
		iic_release_bus(sc->sc_i2c, 0);
	}
	if (error) {
		device_printf(dev, "WARNING: unable to reboot, error %d\n",
		    error);
	}

	return error;
}