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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
/*	$NetBSD: zapm.c,v 1.14 2021/08/07 16:19:08 thorpej Exp $	*/
/*	$OpenBSD: zaurus_apm.c,v 1.13 2006/12/12 23:14:28 dim Exp $	*/

/*
 * Copyright (c) 2005 Uwe Stuehler <uwe@bsdx.de>
 *
 * 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/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: zapm.c,v 1.14 2021/08/07 16:19:08 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/callout.h>
#include <sys/selinfo.h> /* XXX: for apm_softc that is exposed here */
#include <sys/device.h>

#include <dev/hpc/apm/apmvar.h>

#include <arm/xscale/pxa2x0reg.h>
#include <arm/xscale/pxa2x0var.h>
#include <arm/xscale/pxa2x0cpu.h>
#include <arm/xscale/pxa2x0_gpio.h>

#include <machine/config_hook.h>

#include <zaurus/dev/scoopvar.h>
#include <zaurus/dev/zsspvar.h>
#include <zaurus/zaurus/zaurus_reg.h>
#include <zaurus/zaurus/zaurus_var.h>

#ifdef APMDEBUG
#define DPRINTF(x)	printf x
#else
#define	DPRINTF(x)	do { } while (/*CONSTCOND*/0)
#endif

struct zapm_softc {
	device_t sc_dev;
	void *sc_apmdev;
	kmutex_t sc_mtx;

	struct callout sc_cyclic_poll;
	struct callout sc_discharge_poll;
	struct timeval sc_lastbattchk;
	volatile int suspended;
	volatile int charging;
	volatile int discharging;
	int battery_volt;
	int battery_full_cnt;

	/* GPIO pin */
	int sc_ac_detect_pin;
	int sc_batt_cover_pin;
	int sc_charge_comp_pin;

	/* machine-independent part */
	volatile u_int events;
	volatile int power_state;
	volatile int battery_state;
	volatile int ac_state;
	config_hook_tag sc_standby_hook;
	config_hook_tag sc_suspend_hook;
	config_hook_tag sc_battery_hook;
	config_hook_tag sc_ac_hook;
	int battery_life;
	int minutes_left;
};

static int	zapm_match(device_t, cfdata_t, void *);
static void	zapm_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(zapm, sizeof(struct zapm_softc),
    zapm_match, zapm_attach, NULL, NULL);

static int	zapm_hook(void *, int, long, void *);
static void     zapm_disconnect(void *);
static void     zapm_enable(void *, int);
static int      zapm_set_powstate(void *, u_int, u_int);
static int      zapm_get_powstat(void *, u_int, struct apm_power_info *);
static int      zapm_get_event(void *, u_int *, u_int *);
static void     zapm_cpu_busy(void *);
static void     zapm_cpu_idle(void *);
static void     zapm_get_capabilities(void *, u_int *, u_int *);

static struct apm_accessops zapm_accessops = {
	zapm_disconnect,
	zapm_enable,
	zapm_set_powstate,
	zapm_get_powstat,
	zapm_get_event,
	zapm_cpu_busy,
	zapm_cpu_idle,
	zapm_get_capabilities,
};

static int	zapm_acintr(void *);
static int	zapm_bcintr(void *);
static void	zapm_cyclic(void *);
static void	zapm_poll(void *);
static void	zapm_poll1(void *, int);

/* battery-related GPIO pins */
#define GPIO_AC_IN_C3000	115	/* 0=AC connected */
#define GPIO_CHRG_CO_C3000	101	/* 1=battery full */
#define GPIO_BATT_COVER_C3000	90	/* 0=unlocked */

/* Cyclic timer value */
#define	CYCLIC_TIME	(60 * hz)	/* 60s */

static int
zapm_match(device_t parent, cfdata_t cf, void *aux)
{

	if (ZAURUS_ISC1000 || ZAURUS_ISC3000)
		return 1;
	return 0;
}

static void
zapm_attach(device_t parent, device_t self, void *aux)
{
	struct zapm_softc *sc = device_private(self);
	struct apmdev_attach_args aaa;

	sc->sc_dev = self;

	aprint_normal(": pseudo power management module\n");
	aprint_naive("\n");

	/* machine-depent part */
	callout_init(&sc->sc_cyclic_poll, 0);
	callout_setfunc(&sc->sc_cyclic_poll, zapm_cyclic, sc);
	callout_init(&sc->sc_discharge_poll, 0);
	callout_setfunc(&sc->sc_discharge_poll, zapm_poll, sc);
	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);

	if (ZAURUS_ISC1000 || ZAURUS_ISC3000) {
		sc->sc_ac_detect_pin = GPIO_AC_IN_C3000;
		sc->sc_batt_cover_pin = GPIO_BATT_COVER_C3000;
		sc->sc_charge_comp_pin = GPIO_CHRG_CO_C3000;
	} else {
		/* XXX */
		return;
	}

	pxa2x0_gpio_set_function(sc->sc_ac_detect_pin, GPIO_IN);
	pxa2x0_gpio_set_function(sc->sc_charge_comp_pin, GPIO_IN);
	pxa2x0_gpio_set_function(sc->sc_batt_cover_pin, GPIO_IN);

	(void)pxa2x0_gpio_intr_establish(sc->sc_ac_detect_pin,
	    IST_EDGE_BOTH, IPL_BIO, zapm_acintr, sc);
	(void)pxa2x0_gpio_intr_establish(sc->sc_charge_comp_pin,
	    IST_EDGE_BOTH, IPL_BIO, zapm_bcintr, sc);

	/* machine-independent part */
	sc->events = 0;
	sc->power_state = APM_SYS_READY;
	sc->battery_state = APM_BATT_FLAG_UNKNOWN;
	sc->ac_state = APM_AC_UNKNOWN;
	sc->battery_life = APM_BATT_LIFE_UNKNOWN;
	sc->minutes_left = 0;
	sc->sc_standby_hook = config_hook(CONFIG_HOOK_PMEVENT,
					  CONFIG_HOOK_PMEVENT_STANDBYREQ,
					  CONFIG_HOOK_EXCLUSIVE,
					  zapm_hook, sc);
	sc->sc_suspend_hook = config_hook(CONFIG_HOOK_PMEVENT,
					  CONFIG_HOOK_PMEVENT_SUSPENDREQ,
					  CONFIG_HOOK_EXCLUSIVE,
					  zapm_hook, sc);

	sc->sc_battery_hook = config_hook(CONFIG_HOOK_PMEVENT,
					  CONFIG_HOOK_PMEVENT_BATTERY,
					  CONFIG_HOOK_SHARE,
					  zapm_hook, sc);

	sc->sc_ac_hook = config_hook(CONFIG_HOOK_PMEVENT,
				     CONFIG_HOOK_PMEVENT_AC,
				     CONFIG_HOOK_SHARE,
				     zapm_hook, sc);

	aaa.accessops = &zapm_accessops;
	aaa.accesscookie = sc;
	aaa.apm_detail = 0x0102;

	sc->sc_apmdev = config_found(self, &aaa, apmprint, CFARGS_NONE);
	if (sc->sc_apmdev != NULL) {
		zapm_poll1(sc, 0);
		callout_schedule(&sc->sc_cyclic_poll, CYCLIC_TIME);
	}
}

static int
zapm_hook(void *v, int type, long id, void *msg)
{
	struct zapm_softc *sc = (struct zapm_softc *)v;
	int charge;
	int message;
	int s;

	if (type != CONFIG_HOOK_PMEVENT)
		return 1;

	if (CONFIG_HOOK_VALUEP(msg))
		message = (int)msg;
	else
		message = *(int *)msg;

	s = splhigh();

	switch (id) {
	case CONFIG_HOOK_PMEVENT_STANDBYREQ:
		if (sc->power_state != APM_SYS_STANDBY) {
			sc->events |= (1 << APM_USER_STANDBY_REQ);
		} else {
			sc->events |= (1 << APM_NORMAL_RESUME);
		}
		break;
	case CONFIG_HOOK_PMEVENT_SUSPENDREQ:
		if (sc->power_state != APM_SYS_SUSPEND) {
			DPRINTF(("zapm: suspend request\n"));
			sc->events |= (1 << APM_USER_SUSPEND_REQ);
		} else {
			sc->events |= (1 << APM_NORMAL_RESUME);
		}
		break;
	case CONFIG_HOOK_PMEVENT_BATTERY:
		switch (message) {
		case CONFIG_HOOK_BATT_CRITICAL:
			DPRINTF(("zapm: battery state critical\n"));
			charge = sc->battery_state & APM_BATT_FLAG_CHARGING;
			sc->battery_state = APM_BATT_FLAG_CRITICAL;
			sc->battery_state |= charge;
			sc->battery_life = 0;
			break;
		case CONFIG_HOOK_BATT_LOW:
			DPRINTF(("zapm: battery state low\n"));
			charge = sc->battery_state & APM_BATT_FLAG_CHARGING;
			sc->battery_state = APM_BATT_FLAG_LOW;
			sc->battery_state |= charge;
			break;
		case CONFIG_HOOK_BATT_HIGH:
			DPRINTF(("zapm: battery state high\n"));
			charge = sc->battery_state & APM_BATT_FLAG_CHARGING;
			sc->battery_state = APM_BATT_FLAG_HIGH;
			sc->battery_state |= charge;
			break;
		case CONFIG_HOOK_BATT_10P:
			DPRINTF(("zapm: battery life 10%%\n"));
			sc->battery_life = 10;
			break;
		case CONFIG_HOOK_BATT_20P:
			DPRINTF(("zapm: battery life 20%%\n"));
			sc->battery_life = 20;
			break;
		case CONFIG_HOOK_BATT_30P:
			DPRINTF(("zapm: battery life 30%%\n"));
			sc->battery_life = 30;
			break;
		case CONFIG_HOOK_BATT_40P:
			DPRINTF(("zapm: battery life 40%%\n"));
			sc->battery_life = 40;
			break;
		case CONFIG_HOOK_BATT_50P:
			DPRINTF(("zapm: battery life 50%%\n"));
			sc->battery_life = 50;
			break;
		case CONFIG_HOOK_BATT_60P:
			DPRINTF(("zapm: battery life 60%%\n"));
			sc->battery_life = 60;
			break;
		case CONFIG_HOOK_BATT_70P:
			DPRINTF(("zapm: battery life 70%%\n"));
			sc->battery_life = 70;
			break;
		case CONFIG_HOOK_BATT_80P:
			DPRINTF(("zapm: battery life 80%%\n"));
			sc->battery_life = 80;
			break;
		case CONFIG_HOOK_BATT_90P:
			DPRINTF(("zapm: battery life 90%%\n"));
			sc->battery_life = 90;
			break;
		case CONFIG_HOOK_BATT_100P:
			DPRINTF(("zapm: battery life 100%%\n"));
			sc->battery_life = 100;
			break;
		case CONFIG_HOOK_BATT_UNKNOWN:
			DPRINTF(("zapm: battery state unknown\n"));
			sc->battery_state = APM_BATT_FLAG_UNKNOWN;
			sc->battery_life = APM_BATT_LIFE_UNKNOWN;
			break;
		case CONFIG_HOOK_BATT_NO_SYSTEM_BATTERY:
			DPRINTF(("zapm: battery state no system battery?\n"));
			sc->battery_state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
			sc->battery_life = APM_BATT_LIFE_UNKNOWN;
			break;
		}
		break;
	case CONFIG_HOOK_PMEVENT_AC:
		switch (message) {
		case CONFIG_HOOK_AC_OFF:
			DPRINTF(("zapm: ac not connected\n"));
			sc->battery_state &= ~APM_BATT_FLAG_CHARGING;
			sc->ac_state = APM_AC_OFF;
			break;
		case CONFIG_HOOK_AC_ON_CHARGE:
			DPRINTF(("zapm: charging\n"));
			sc->battery_state |= APM_BATT_FLAG_CHARGING;
			sc->ac_state = APM_AC_ON;
			break;
		case CONFIG_HOOK_AC_ON_NOCHARGE:
			DPRINTF(("zapm: ac connected\n"));
			sc->battery_state &= ~APM_BATT_FLAG_CHARGING;
			sc->ac_state = APM_AC_ON;
			break;
		case CONFIG_HOOK_AC_UNKNOWN:
			sc->ac_state = APM_AC_UNKNOWN;
			break;
		}
		break;
	}

	splx(s);

	return 0;
}

static void
zapm_disconnect(void *v)
{
#if 0
	struct zapm_softc *sc = (struct zapm_softc *)v;
#endif
}

static void
zapm_enable(void *v, int onoff)
{
#if 0
	struct zapm_softc *sc = (struct zapm_softc *)v;
#endif
}

static int
zapm_set_powstate(void *v, u_int devid, u_int powstat)
{
	struct zapm_softc *sc = (struct zapm_softc *)v;

	if (devid != APM_DEV_ALLDEVS)
		return APM_ERR_UNRECOG_DEV;

	switch (powstat) {
	case APM_SYS_READY:
		DPRINTF(("zapm: set power state READY\n"));
		sc->power_state = APM_SYS_READY;
		break;
	case APM_SYS_STANDBY:
		DPRINTF(("zapm: set power state STANDBY\n"));
		/* XXX */
		DPRINTF(("zapm: resume\n"));
		break;
	case APM_SYS_SUSPEND:
		DPRINTF(("zapm: set power state SUSPEND...\n"));
		/* XXX */
		DPRINTF(("zapm: resume\n"));
		break;
	case APM_SYS_OFF:
		DPRINTF(("zapm: set power state OFF\n"));
		sc->power_state = APM_SYS_OFF;
		break;
	case APM_LASTREQ_INPROG:
		/*DPRINTF(("zapm: set power state INPROG\n"));*/
		break;
	case APM_LASTREQ_REJECTED:
		DPRINTF(("zapm: set power state REJECTED\n"));
		break;
	}

	return 0;
}

static int
zapm_get_powstat(void *v, u_int batteryid, struct apm_power_info *pinfo)
{
	struct zapm_softc *sc = (struct zapm_softc *)v;
	int val;

	if (config_hook_call(CONFIG_HOOK_GET,
			     CONFIG_HOOK_ACADAPTER, &val) != -1)
		pinfo->ac_state = val;
	else
		pinfo->ac_state = sc->ac_state;
	DPRINTF(("zapm: pinfo->ac_state: %d\n", pinfo->ac_state));

	if (config_hook_call(CONFIG_HOOK_GET,
			     CONFIG_HOOK_CHARGE, &val) != -1)
		pinfo->battery_state = val;
	else {
		DPRINTF(("zapm: sc->battery_state: %#x\n", sc->battery_state));
		if (sc->battery_state & APM_BATT_FLAG_CHARGING)
			pinfo->battery_flags = APM_BATT_FLAG_CHARGING;
		else if (sc->battery_state & APM_BATT_FLAG_CRITICAL)
			pinfo->battery_flags = APM_BATT_FLAG_CRITICAL;
		else if (sc->battery_state & APM_BATT_FLAG_LOW)
			pinfo->battery_flags = APM_BATT_FLAG_LOW;
		else if (sc->battery_state & APM_BATT_FLAG_HIGH)
			pinfo->battery_flags = APM_BATT_FLAG_HIGH;
		else
			pinfo->battery_flags = APM_BATT_FLAG_UNKNOWN;
	}
	DPRINTF(("zapm: pinfo->battery_flags: %#x\n", pinfo->battery_flags));

	if (config_hook_call(CONFIG_HOOK_GET,
			     CONFIG_HOOK_BATTERYVAL, &val) != -1)
		pinfo->battery_life = val;
	else
		pinfo->battery_life = sc->battery_life;
	DPRINTF(("zapm: pinfo->battery_life: %d\n", pinfo->battery_life));

	return 0;
}

static int
zapm_get_event(void *v, u_int *event_type, u_int *event_info)
{
	struct zapm_softc *sc = (struct zapm_softc *)v;
	u_int ev;
	int s;

	s = splhigh();
	for (ev = APM_STANDBY_REQ; ev <= APM_CAP_CHANGE; ev++) {
		if (sc->events & (1 << ev)) {
			sc->events &= ~(1 << ev);
			*event_type = ev;
			if (*event_type == APM_NORMAL_RESUME ||
			    *event_type == APM_CRIT_RESUME) {
				/* pccard power off in the suspend state */
				*event_info = 1;
				sc->power_state = APM_SYS_READY;
			} else {
				*event_info = 0;
			}
			splx(s);

			return 0;
		}
	}
	splx(s);

	return APM_ERR_NOEVENTS;
}

static void
zapm_cpu_busy(void *v)
{
#if 0
	struct zapm_softc *sc = (struct zapm_softc *)v;
#endif
}

static void
zapm_cpu_idle(void *v)
{
#if 0
	struct zapm_softc *sc = (struct zapm_softc *)v;
#endif
}

static void
zapm_get_capabilities(void *v, u_int *numbatts, u_int *capflags)
{
#if 0
	struct zapm_softc *sc = (struct zapm_softc *)v;
#endif

	*numbatts = 1;
	*capflags = 0 /* | APM_GLOBAL_STANDBY | APM_GLOBAL_SUSPEND */;
}

/*-----------------------------------------------------------------------------
 * zaurus depent part
 */
/* MAX1111 command word */
#define MAXCTRL_PD0		(1<<0)
#define MAXCTRL_PD1		(1<<1)
#define MAXCTRL_SGL		(1<<2)
#define MAXCTRL_UNI		(1<<3)
#define MAXCTRL_SEL_SHIFT	4
#define MAXCTRL_STR		(1<<7)

/* MAX1111 ADC channels */
#define	BATT_THM		2
#define	BATT_AD			4
#define JK_VAD			6

/*
 * Battery-specific information
 */
struct battery_threshold {
	int	percent;
	int	value;
	int	state;
};

struct battery_info {
	const struct battery_threshold *bi_thres;
};

static const struct battery_threshold zaurus_battery_life_c3000[] = {
	{ 100,	212,	CONFIG_HOOK_BATT_HIGH		},
	{  98,	212,	CONFIG_HOOK_BATT_HIGH		},
	{  95,	211,	CONFIG_HOOK_BATT_HIGH		},
	{  93,	210,	CONFIG_HOOK_BATT_HIGH		},
	{  90,	209,	CONFIG_HOOK_BATT_HIGH		},
	{  88,	208,	CONFIG_HOOK_BATT_HIGH		},
	{  85,	207,	CONFIG_HOOK_BATT_HIGH		},
	{  83,	206,	CONFIG_HOOK_BATT_HIGH		},
	{  80,	205,	CONFIG_HOOK_BATT_HIGH		},
	{  78,	204,	CONFIG_HOOK_BATT_HIGH		},
	{  75,	203,	CONFIG_HOOK_BATT_HIGH		},
	{  73,	202,	CONFIG_HOOK_BATT_HIGH		},
	{  70,	201,	CONFIG_HOOK_BATT_HIGH		},
	{  68,	200,	CONFIG_HOOK_BATT_HIGH		},
	{  65,	199,	CONFIG_HOOK_BATT_HIGH		},
	{  63,	198,	CONFIG_HOOK_BATT_HIGH		},
	{  60,	197,	CONFIG_HOOK_BATT_HIGH		},
	{  58,	196,	CONFIG_HOOK_BATT_HIGH		},
	{  55,	195,	CONFIG_HOOK_BATT_HIGH		},
	{  53,	194,	CONFIG_HOOK_BATT_HIGH		},
	{  50,	193,	CONFIG_HOOK_BATT_HIGH		},
	{  48,	192,	CONFIG_HOOK_BATT_HIGH		},
	{  45,	192,	CONFIG_HOOK_BATT_HIGH		},
	{  43,	191,	CONFIG_HOOK_BATT_HIGH		},
	{  40,	191,	CONFIG_HOOK_BATT_HIGH		},
	{  38,	190,	CONFIG_HOOK_BATT_HIGH		},
	{  35,	190,	CONFIG_HOOK_BATT_HIGH		},
	{  33,	189,	CONFIG_HOOK_BATT_HIGH		},
	{  30,	188,	CONFIG_HOOK_BATT_HIGH		},
	{  28,	187,	CONFIG_HOOK_BATT_LOW		},
	{  25,	186,	CONFIG_HOOK_BATT_LOW		},
	{  23,	185,	CONFIG_HOOK_BATT_LOW		},
	{  20,	184,	CONFIG_HOOK_BATT_LOW		},
	{  18,	183,	CONFIG_HOOK_BATT_LOW		},
	{  15,	182,	CONFIG_HOOK_BATT_LOW		},
	{  13,	181,	CONFIG_HOOK_BATT_LOW		},
	{  10,	180,	CONFIG_HOOK_BATT_LOW		},
	{   8,	179,	CONFIG_HOOK_BATT_LOW		},
	{   5,	178,	CONFIG_HOOK_BATT_LOW		},
	{   0,	  0,	CONFIG_HOOK_BATT_CRITICAL	}
};

static const struct battery_info zaurus_battery_c3000 = {
	zaurus_battery_life_c3000
};

static const struct battery_info *zaurus_main_battery = &zaurus_battery_c3000;

/* Restart charging this many times before accepting BATT_FULL. */
#define	MIN_BATT_FULL		2

/* Discharge 100 ms before reading the voltage if AC is connected. */
#define	DISCHARGE_TIMEOUT	(hz / 10)

/* Check battery voltage and "kick charging" every minute. */
static const struct timeval zapm_battchkrate = { 60, 0 };

static int	zapm_get_ac_state(struct zapm_softc *);
static int	zapm_get_battery_compartment_state(struct zapm_softc *);
static int	zapm_get_charge_complete_state(struct zapm_softc *);
static void	zapm_set_charging(struct zapm_softc *, int);
static int	zapm_charge_complete(struct zapm_softc *);
static int	max1111_adc_value_avg(int chan, int pause);
static int	zapm_get_battery_volt(void);
static int	zapm_battery_state(int volt);
static int	zapm_battery_life(int volt);

static int
zapm_acintr(void *v)
{

	zapm_poll1(v, 1);

	return 1;
}

static int
zapm_bcintr(void *v)
{

	zapm_poll1(v, 1);

	return 1;
}

static void
zapm_cyclic(void *v)
{
	struct zapm_softc *sc = (struct zapm_softc *)v;

	zapm_poll1(sc, 1);

	callout_schedule(&sc->sc_cyclic_poll, CYCLIC_TIME);
}

static void
zapm_poll(void *v)
{

	zapm_poll1(v, 1);
}

static int
zapm_get_ac_state(struct zapm_softc *sc)
{

	if (!pxa2x0_gpio_get_bit(sc->sc_ac_detect_pin))
		return APM_AC_ON;
	return APM_AC_OFF;
}

static int
zapm_get_battery_compartment_state(struct zapm_softc *sc)
{

	return pxa2x0_gpio_get_bit(sc->sc_batt_cover_pin);
}

static int
zapm_get_charge_complete_state(struct zapm_softc *sc)
{

	return pxa2x0_gpio_get_bit(sc->sc_charge_comp_pin);
}

static void
zapm_set_charging(struct zapm_softc *sc, int enable)
{

	if (ZAURUS_ISC1000 || ZAURUS_ISC3000) {
		scoop_discharge_battery(0);
		scoop_charge_battery(enable, 0);
		scoop_led_set(SCOOP_LED_ORANGE, enable);
	}
}

/*
 * Return non-zero if the charge complete signal indicates that the
 * battery is fully charged.  Restart charging to clear this signal.
 */
static int
zapm_charge_complete(struct zapm_softc *sc)
{

	if (sc->charging && sc->battery_full_cnt < MIN_BATT_FULL) {
		if (zapm_get_charge_complete_state(sc)) {
			sc->battery_full_cnt++;
			if (sc->battery_full_cnt < MIN_BATT_FULL) {
				DPRINTF(("battery almost full\n"));
				zapm_set_charging(sc, 0);
				delay(15000);
				zapm_set_charging(sc, 1);
			}
		} else if (sc->battery_full_cnt > 0) {
			/* false alarm */
			sc->battery_full_cnt = 0;
			zapm_set_charging(sc, 0);
			delay(15000);
			zapm_set_charging(sc, 1);
		}
	}

	return (sc->battery_full_cnt >= MIN_BATT_FULL);
}

static int
max1111_adc_value(int chan)
{

	return ((int)zssp_ic_send(ZSSP_IC_MAX1111, MAXCTRL_PD0 |
	    MAXCTRL_PD1 | MAXCTRL_SGL | MAXCTRL_UNI |
	    (chan << MAXCTRL_SEL_SHIFT) | MAXCTRL_STR));
}

/* XXX simplify */
static int
max1111_adc_value_avg(int chan, int pause)
{
	int val[5];
	int sum;
	int minv, maxv, v;
	int i;

	DPRINTF(("max1111_adc_value_avg: chan = %d, pause = %d\n",
	    chan, pause));

	for (i = 0; i < 5; i++) {
		val[i] = max1111_adc_value(chan);
		if (i != 4)
			delay(pause * 1000);
		DPRINTF(("max1111_adc_value_avg: chan[%d] = %d\n", i, val[i]));
	}

	/* get max value */
	v = val[0];
	minv = 0;
	for (i = 1; i < 5; i++) {
		if (v < val[i]) {
			v = val[i];
			minv = i;
		}
	}

	/* get min value */
	v = val[4];
	maxv = 4;
	for (i = 3; i >= 0; i--) {
		if (v > val[i]) {
			v = val[i];
			maxv = i;
		}
	}

	DPRINTF(("max1111_adc_value_avg: minv = %d, maxv = %d\n", minv, maxv));
	sum = 0;
	for (i = 0; i < 5; i++) {
		if (i == minv || i == maxv)
			continue;
		sum += val[i];
	}

	DPRINTF(("max1111_adc_value_avg: sum = %d, sum / 3 = %d\n",
	    sum, sum / 3));

	return sum / 3;
}

static int
zapm_get_battery_volt(void)
{

	return max1111_adc_value_avg(BATT_AD, 10);
}

static int
zapm_battery_state(int volt)
{
	const struct battery_threshold *bthr;
	int i;

	bthr = zaurus_main_battery->bi_thres;

	for (i = 0; bthr[i].value > 0; i++)
		if (bthr[i].value <= volt)
			break;

	return bthr[i].state;
}

static int
zapm_battery_life(int volt)
{
	const struct battery_threshold *bthr;
	int i;

	bthr = zaurus_main_battery->bi_thres;

	for (i = 0; bthr[i].value > 0; i++)
		if (bthr[i].value <= volt)
			break;

	if (i == 0)
		return bthr[0].percent;

	return (bthr[i].percent +
	    ((volt - bthr[i].value) * 100) /
	    (bthr[i-1].value - bthr[i].value) *
	    (bthr[i-1].percent - bthr[i].percent) / 100);
}

/*
 * Poll power-management related GPIO inputs, update battery life
 * in softc, and/or control battery charging.
 */
static void
zapm_poll1(void *v, int do_suspend)
{
	struct zapm_softc *sc = (struct zapm_softc *)v;
	int ac_state;
	int bc_lock;
	int charging;
	int volt;

	if (!mutex_tryenter(&sc->sc_mtx))
		return;

	ac_state = zapm_get_ac_state(sc);
	bc_lock = zapm_get_battery_compartment_state(sc);

	/* Stop discharging. */
	if (sc->discharging) {
		sc->discharging = 0;
		charging = 0;
		volt = zapm_get_battery_volt();
		DPRINTF(("zapm_poll: discharge off volt %d\n", volt));
	} else {
		charging = sc->battery_state & APM_BATT_FLAG_CHARGING;
		volt = sc->battery_volt;
	}

	/* Start or stop charging as necessary. */
	if (ac_state && bc_lock) {
		int charge_completed = zapm_charge_complete(sc);
		if (charging) {
			if (charge_completed) {
				DPRINTF(("zapm_poll: battery is full\n"));
				charging = 0;
				zapm_set_charging(sc, 0);
			}
		} else if (!charge_completed) {
			charging = APM_BATT_FLAG_CHARGING;
			volt = zapm_get_battery_volt();
			zapm_set_charging(sc, 1);
			DPRINTF(("zapm_poll: start charging volt %d\n", volt));
		}
	} else {
		if (charging) {
			charging = 0;
			zapm_set_charging(sc, 0);
			timerclear(&sc->sc_lastbattchk);
			DPRINTF(("zapm_poll: stop charging\n"));
		}
		sc->battery_full_cnt = 0;
	}

	/*
	 * Restart charging once in a while.  Discharge a few milliseconds
	 * before updating the voltage in our softc if A/C is connected.
	 */
	if (bc_lock && ratecheck(&sc->sc_lastbattchk, &zapm_battchkrate)) {
		if (do_suspend && sc->suspended) {
			/* XXX */
#if 0
			DPRINTF(("zapm_poll: suspended %lu %lu\n",
			    sc->lastbattchk.tv_sec,
			    pxa2x0_rtc_getsecs()));
			if (charging) {
				zapm_set_charging(sc, 0);
				delay(15000);
				zapm_set_charging(sc, 1);
				pxa2x0_rtc_setalarm(pxa2x0_rtc_getsecs() +
				    zapm_battchkrate.tv_sec + 1);
			}
#endif
		} else if (ac_state && sc->battery_full_cnt == 0) {
			DPRINTF(("zapm_poll: discharge on\n"));
			if (charging)
				zapm_set_charging(sc, 0);
			sc->discharging = 1;
			if (ZAURUS_ISC1000 || ZAURUS_ISC3000)
				scoop_discharge_battery(1);
			callout_schedule(&sc->sc_discharge_poll,
			    DISCHARGE_TIMEOUT);
		} else if (!ac_state) {
			volt = zapm_get_battery_volt();
			DPRINTF(("zapm_poll: volt %d\n", volt));
		}
	}

	/* Update the cached power state in our softc. */
	if ((ac_state != sc->ac_state)
	 || (charging != (sc->battery_state & APM_BATT_FLAG_CHARGING))) {
		config_hook_call(CONFIG_HOOK_PMEVENT,
		    CONFIG_HOOK_PMEVENT_AC,
		    (void *)((ac_state == APM_AC_OFF)
		        ? CONFIG_HOOK_AC_OFF
		        : (charging ? CONFIG_HOOK_AC_ON_CHARGE
		                    : CONFIG_HOOK_AC_ON_NOCHARGE)));
	}
	if (volt != sc->battery_volt) {
		sc->battery_volt = volt;
		sc->battery_life = zapm_battery_life(volt);
		config_hook_call(CONFIG_HOOK_PMEVENT,
		    CONFIG_HOOK_PMEVENT_BATTERY,
		    (void *)zapm_battery_state(volt));
	}

	mutex_exit(&sc->sc_mtx);
}