j6x0pwr.c revision 1.3 1 1.3 uwe /* $NetBSD: j6x0pwr.c,v 1.3 2003/10/22 23:52:46 uwe Exp $ */
2 1.1 uwe
3 1.1 uwe /*
4 1.1 uwe * Copyright (c) 2003 Valeriy E. Ushakov
5 1.1 uwe * All rights reserved.
6 1.1 uwe *
7 1.1 uwe * Redistribution and use in source and binary forms, with or without
8 1.1 uwe * modification, are permitted provided that the following conditions
9 1.1 uwe * are met:
10 1.1 uwe * 1. Redistributions of source code must retain the above copyright
11 1.1 uwe * notice, this list of conditions and the following disclaimer.
12 1.1 uwe * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 uwe * notice, this list of conditions and the following disclaimer in the
14 1.1 uwe * documentation and/or other materials provided with the distribution.
15 1.1 uwe * 3. The name of the author may not be used to endorse or promote products
16 1.1 uwe * derived from this software without specific prior written permission
17 1.1 uwe *
18 1.1 uwe * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 uwe * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 uwe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 uwe * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 uwe * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 uwe * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 uwe * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 uwe * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 uwe * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 uwe * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 uwe */
29 1.2 uwe
30 1.2 uwe #include <sys/cdefs.h>
31 1.3 uwe __KERNEL_RCSID(0, "$NetBSD: j6x0pwr.c,v 1.3 2003/10/22 23:52:46 uwe Exp $");
32 1.1 uwe
33 1.1 uwe #include <sys/param.h>
34 1.1 uwe #include <sys/kernel.h>
35 1.1 uwe #include <sys/device.h>
36 1.1 uwe #include <sys/malloc.h>
37 1.1 uwe #include <sys/systm.h>
38 1.1 uwe #include <sys/callout.h>
39 1.1 uwe #ifdef GPROF
40 1.1 uwe #include <sys/gmon.h>
41 1.1 uwe #endif
42 1.1 uwe
43 1.3 uwe #include <machine/platid.h>
44 1.3 uwe #include <machine/platid_mask.h>
45 1.3 uwe
46 1.1 uwe #include <sh3/exception.h>
47 1.1 uwe #include <sh3/intcreg.h>
48 1.1 uwe #include <sh3/pfcreg.h>
49 1.1 uwe
50 1.3 uwe #include <sh3/dev/adcvar.h>
51 1.1 uwe
52 1.1 uwe
53 1.1 uwe /* SH7709_PGDR bits pertinent to Jornada 6x0 power */
54 1.1 uwe #define PGDR_MAIN_BATTERY_OUT 0x04
55 1.1 uwe #define PGDR_LID_OPEN 0x01
56 1.1 uwe
57 1.1 uwe /* A/D covnerter channels to get power stats from */
58 1.1 uwe #define ADC_CHANNEL_BATTERY 3
59 1.1 uwe #define ADC_CHANNEL_BACKUP 4
60 1.1 uwe #define ADC_CHANNEL_CHARGE 5
61 1.1 uwe
62 1.1 uwe /* warn that main battery is low after drops below this value */
63 1.1 uwe #define J6X0PWR_BATTERY_WARNING_THRESHOLD 200
64 1.1 uwe
65 1.1 uwe
66 1.1 uwe struct j6x0pwr_softc {
67 1.1 uwe struct device sc_dev;
68 1.1 uwe
69 1.1 uwe struct callout sc_poll_ch;
70 1.1 uwe };
71 1.1 uwe
72 1.1 uwe static int j6x0pwr_match(struct device *, struct cfdata *, void *);
73 1.1 uwe static void j6x0pwr_attach(struct device *, struct device *, void *);
74 1.1 uwe
75 1.1 uwe CFATTACH_DECL(j6x0pwr, sizeof(struct j6x0pwr_softc),
76 1.1 uwe j6x0pwr_match, j6x0pwr_attach, NULL, NULL);
77 1.1 uwe
78 1.1 uwe
79 1.1 uwe static int j6x0pwr_intr(void *);
80 1.1 uwe static void j6x0pwr_poll_callout(void *);
81 1.1 uwe
82 1.1 uwe
83 1.1 uwe static int
84 1.1 uwe j6x0pwr_match(struct device *parent, struct cfdata *cfp, void *aux)
85 1.1 uwe {
86 1.1 uwe
87 1.1 uwe /*
88 1.1 uwe * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
89 1.1 uwe * Is 620 wired similarly?
90 1.1 uwe */
91 1.1 uwe if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
92 1.1 uwe return (0);
93 1.1 uwe
94 1.1 uwe if (strcmp(cfp->cf_name, "j6x0pwr") != 0)
95 1.1 uwe return (0);
96 1.1 uwe
97 1.1 uwe return (1);
98 1.1 uwe }
99 1.1 uwe
100 1.1 uwe
101 1.1 uwe static void
102 1.1 uwe j6x0pwr_attach(struct device *parent, struct device *self, void *aux)
103 1.1 uwe {
104 1.1 uwe struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
105 1.1 uwe
106 1.1 uwe intc_intr_establish(SH7709_INTEVT2_IRQ0, IST_EDGE, IPL_TTY,
107 1.1 uwe j6x0pwr_intr, sc);
108 1.1 uwe
109 1.1 uwe callout_init(&sc->sc_poll_ch);
110 1.1 uwe callout_reset(&sc->sc_poll_ch, 5 * hz,
111 1.1 uwe j6x0pwr_poll_callout, sc);
112 1.1 uwe
113 1.1 uwe printf("\n");
114 1.1 uwe }
115 1.1 uwe
116 1.1 uwe
117 1.1 uwe /*
118 1.1 uwe * Triggered when the On/Off button is pressed or the lid is closed.
119 1.1 uwe * The state of the lid is reflected in the bit PGDR[2].
120 1.1 uwe * Closing the lid can trigger several consecutive interrupts.
121 1.1 uwe *
122 1.1 uwe * XXX: Since we don't put the machine to sleep, I have no idea how
123 1.1 uwe * wakeup interrupt(s) look like. Need to revisit when software
124 1.1 uwe * suspend is added to the kernel (which we need to support ACPI).
125 1.1 uwe */
126 1.1 uwe static int
127 1.1 uwe j6x0pwr_intr(void *self)
128 1.1 uwe {
129 1.1 uwe struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
130 1.1 uwe uint8_t irr0;
131 1.1 uwe uint8_t pgdr;
132 1.1 uwe
133 1.1 uwe irr0 = _reg_read_1(SH7709_IRR0);
134 1.1 uwe if ((irr0 & IRR0_IRQ0) == 0) {
135 1.1 uwe #ifdef DIAGNOSTIC
136 1.1 uwe printf_nolog("%s: irr0=0x%02x?\n", sc->sc_dev.dv_xname, irr0);
137 1.1 uwe #endif
138 1.1 uwe return (0);
139 1.1 uwe }
140 1.1 uwe
141 1.1 uwe /* clear the interrupt */
142 1.1 uwe _reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ0);
143 1.1 uwe
144 1.1 uwe pgdr = _reg_read_1(SH7709_PGDR);
145 1.1 uwe if ((pgdr & PGDR_LID_OPEN) == 0)
146 1.1 uwe printf("%s: lid closed\n", sc->sc_dev.dv_xname);
147 1.1 uwe else
148 1.1 uwe printf("%s: ON/OFF\n", sc->sc_dev.dv_xname);
149 1.1 uwe
150 1.1 uwe return (1);
151 1.1 uwe }
152 1.1 uwe
153 1.1 uwe
154 1.1 uwe volatile int j6x0pwr_poll_verbose = 0; /* XXX: tweak from ddb */
155 1.1 uwe
156 1.1 uwe static void
157 1.1 uwe j6x0pwr_poll_callout(void *self)
158 1.1 uwe {
159 1.1 uwe struct j6x0pwr_softc *sc = (struct j6x0pwr_softc *)self;
160 1.1 uwe int battery, backup, charging;
161 1.1 uwe uint8_t pgdr;
162 1.1 uwe int s;
163 1.1 uwe
164 1.1 uwe pgdr = _reg_read_1(SH7709_PGDR);
165 1.1 uwe
166 1.3 uwe /* just check main battery charge if not verbose and battery is in */
167 1.1 uwe if (!j6x0pwr_poll_verbose) {
168 1.1 uwe if (pgdr & PGDR_MAIN_BATTERY_OUT)
169 1.1 uwe goto reschedule;
170 1.1 uwe else {
171 1.1 uwe s = spltty();
172 1.1 uwe battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
173 1.1 uwe splx(s);
174 1.1 uwe goto check_battery;
175 1.1 uwe }
176 1.1 uwe }
177 1.1 uwe
178 1.1 uwe s = spltty();
179 1.1 uwe battery = adc_sample_channel(ADC_CHANNEL_BATTERY);
180 1.1 uwe splx(s);
181 1.1 uwe
182 1.1 uwe s = spltty();
183 1.1 uwe backup = adc_sample_channel(ADC_CHANNEL_BACKUP);
184 1.1 uwe splx(s);
185 1.1 uwe
186 1.1 uwe s = spltty();
187 1.1 uwe charging = adc_sample_channel(ADC_CHANNEL_CHARGE);
188 1.1 uwe splx(s);
189 1.1 uwe
190 1.1 uwe printf_nolog("%s: main=", sc->sc_dev.dv_xname);
191 1.1 uwe if (pgdr & PGDR_MAIN_BATTERY_OUT)
192 1.1 uwe printf_nolog("<no>");
193 1.1 uwe else
194 1.1 uwe printf_nolog("%-4d", battery);
195 1.1 uwe
196 1.1 uwe printf_nolog(" bkup=%-4d", backup);
197 1.1 uwe
198 1.1 uwe /*
199 1.1 uwe * When main battery is being charged, ADC_CHANNEL_CHARGE
200 1.1 uwe * samples at almost zero. It samples at 2^10 otherwise.
201 1.1 uwe */
202 1.1 uwe if (charging < 8)
203 1.1 uwe printf_nolog("charging");
204 1.1 uwe
205 1.1 uwe printf_nolog("\n");
206 1.1 uwe
207 1.1 uwe check_battery:
208 1.1 uwe if (!(pgdr & PGDR_MAIN_BATTERY_OUT)
209 1.1 uwe && (battery < J6X0PWR_BATTERY_WARNING_THRESHOLD))
210 1.1 uwe printf("%s: WARNING: main battery %d is low!\n",
211 1.1 uwe sc->sc_dev.dv_xname, battery);
212 1.1 uwe
213 1.1 uwe reschedule:
214 1.1 uwe callout_schedule(&sc->sc_poll_ch, 5*hz);
215 1.1 uwe }
216