2 * Device driver for the via-pmu on Apple Powermacs.
4 * The VIA (versatile interface adapter) interfaces to the PMU,
5 * a 6805 microprocessor core whose primary function is to control
6 * battery charging and system power on the PowerBook 3400 and 2400.
7 * The PMU also controls the ADB (Apple Desktop Bus) which connects
8 * to the keyboard and mouse, as well as the non-volatile RAM
9 * and the RTC (real time clock) chip.
11 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
12 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
14 * THIS DRIVER IS BECOMING A TOTAL MESS !
15 * - Cleanup atomically disabling reply to PMU events after
16 * a sleep or a freq. switch
17 * - Move sleep code out of here to pmac_pm, merge into new
18 * common PM infrastructure
19 * - Move backlight code out as well
20 * - Save/Restore PCI space properly
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/sched.h>
29 #include <linux/miscdevice.h>
30 #include <linux/blkdev.h>
31 #include <linux/pci.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/adb.h>
35 #include <linux/pmu.h>
36 #include <linux/cuda.h>
37 #include <linux/smp_lock.h>
38 #include <linux/module.h>
39 #include <linux/spinlock.h>
41 #include <linux/proc_fs.h>
42 #include <linux/init.h>
43 #include <linux/interrupt.h>
44 #include <linux/device.h>
45 #include <linux/sysdev.h>
46 #include <linux/suspend.h>
47 #include <linux/syscalls.h>
48 #include <linux/cpu.h>
50 #include <asm/machdep.h>
52 #include <asm/pgtable.h>
53 #include <asm/system.h>
54 #include <asm/sections.h>
56 #include <asm/pmac_feature.h>
57 #include <asm/pmac_pfunc.h>
58 #include <asm/pmac_low_i2c.h>
59 #include <asm/uaccess.h>
60 #include <asm/mmu_context.h>
61 #include <asm/cputable.h>
63 #ifdef CONFIG_PMAC_BACKLIGHT
64 #include <asm/backlight.h>
68 #include <asm/open_pic.h>
71 #include "via-pmu-event.h"
73 /* Some compile options */
74 #undef SUSPEND_USES_PMU
76 #undef HACKED_PCI_SAVE
78 /* Misc minor number allocated for /dev/pmu */
81 /* How many iterations between battery polls */
82 #define BATTERY_POLLING_COUNT 2
84 static volatile unsigned char __iomem *via;
86 /* VIA registers - spaced 0x200 bytes apart */
87 #define RS 0x200 /* skip between registers */
88 #define B 0 /* B-side data */
89 #define A RS /* A-side data */
90 #define DIRB (2*RS) /* B-side direction (1=output) */
91 #define DIRA (3*RS) /* A-side direction (1=output) */
92 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
93 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
94 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
95 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
96 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
97 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
98 #define SR (10*RS) /* Shift register */
99 #define ACR (11*RS) /* Auxiliary control register */
100 #define PCR (12*RS) /* Peripheral control register */
101 #define IFR (13*RS) /* Interrupt flag register */
102 #define IER (14*RS) /* Interrupt enable register */
103 #define ANH (15*RS) /* A-side data, no handshake */
105 /* Bits in B data register: both active low */
106 #define TACK 0x08 /* Transfer acknowledge (input) */
107 #define TREQ 0x10 /* Transfer request (output) */
110 #define SR_CTRL 0x1c /* Shift register control bits */
111 #define SR_EXT 0x0c /* Shift on external clock */
112 #define SR_OUT 0x10 /* Shift out if 1 */
114 /* Bits in IFR and IER */
115 #define IER_SET 0x80 /* set bits in IER */
116 #define IER_CLR 0 /* clear bits in IER */
117 #define SR_INT 0x04 /* Shift register full/empty */
119 #define CB1_INT 0x10 /* transition on CB1 input */
121 static volatile enum pmu_state {
130 static volatile enum int_data_state {
135 } int_data_state[2] = { int_data_empty, int_data_empty };
137 static struct adb_request *current_req;
138 static struct adb_request *last_req;
139 static struct adb_request *req_awaiting_reply;
140 static unsigned char interrupt_data[2][32];
141 static int interrupt_data_len[2];
142 static int int_data_last;
143 static unsigned char *reply_ptr;
144 static int data_index;
146 static volatile int adb_int_pending;
147 static volatile int disable_poll;
148 static struct device_node *vias;
149 static int pmu_kind = PMU_UNKNOWN;
150 static int pmu_fully_inited = 0;
151 static int pmu_has_adb;
152 static struct device_node *gpio_node;
153 static unsigned char __iomem *gpio_reg = NULL;
154 static int gpio_irq = -1;
155 static int gpio_irq_enabled = -1;
156 static volatile int pmu_suspended = 0;
157 static spinlock_t pmu_lock;
158 static u8 pmu_intr_mask;
159 static int pmu_version;
160 static int drop_interrupts;
161 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
162 static int option_lid_wakeup = 1;
163 #endif /* CONFIG_PM && CONFIG_PPC32 */
164 #if (defined(CONFIG_PM)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT_LEGACY)
165 static int sleep_in_progress;
167 static unsigned long async_req_locks;
168 static unsigned int pmu_irq_stats[11];
170 static struct proc_dir_entry *proc_pmu_root;
171 static struct proc_dir_entry *proc_pmu_info;
172 static struct proc_dir_entry *proc_pmu_irqstats;
173 static struct proc_dir_entry *proc_pmu_options;
174 static int option_server_mode;
176 int pmu_battery_count;
178 unsigned int pmu_power_flags;
179 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
180 static int query_batt_timer = BATTERY_POLLING_COUNT;
181 static struct adb_request batt_req;
182 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
184 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
185 extern int disable_kernel_backlight;
186 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
190 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
193 static int adb_dev_map = 0;
194 static int pmu_adb_flags;
196 static int pmu_probe(void);
197 static int pmu_init(void);
198 static int pmu_send_request(struct adb_request *req, int sync);
199 static int pmu_adb_autopoll(int devs);
200 static int pmu_adb_reset_bus(void);
201 #endif /* CONFIG_ADB */
203 static int init_pmu(void);
204 static void pmu_start(void);
205 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
206 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
207 static int proc_get_info(char *page, char **start, off_t off,
208 int count, int *eof, void *data);
209 static int proc_get_irqstats(char *page, char **start, off_t off,
210 int count, int *eof, void *data);
211 static void pmu_pass_intr(unsigned char *data, int len);
212 static int proc_get_batt(char *page, char **start, off_t off,
213 int count, int *eof, void *data);
214 static int proc_read_options(char *page, char **start, off_t off,
215 int count, int *eof, void *data);
216 static int proc_write_options(struct file *file, const char __user *buffer,
217 unsigned long count, void *data);
220 struct adb_driver via_pmu_driver = {
229 #endif /* CONFIG_ADB */
231 extern void low_sleep_handler(void);
232 extern void enable_kernel_altivec(void);
233 extern void enable_kernel_fp(void);
236 int pmu_polled_request(struct adb_request *req);
237 int pmu_wink(struct adb_request *req);
241 * This table indicates for each PMU opcode:
242 * - the number of data bytes to be sent with the command, or -1
243 * if a length byte should be sent,
244 * - the number of response bytes which the PMU will return, or
245 * -1 if it will send a length byte.
247 static const s8 pmu_data_len[256][2] = {
248 /* 0 1 2 3 4 5 6 7 */
249 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
250 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
251 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
253 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
255 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
257 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
259 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
260 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
261 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
263 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
264 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
265 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
267 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
268 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
269 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
270 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
272 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
273 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
274 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
275 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
276 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
277 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
278 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
279 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
280 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
283 static char *pbook_type[] = {
285 "PowerBook 2400/3400/3500(G3)",
286 "PowerBook G3 Series",
291 int __init find_via_pmu(void)
298 vias = of_find_node_by_name(NULL, "via-pmu");
302 reg = (u32 *)get_property(vias, "reg", NULL);
304 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
307 taddr = of_translate_address(vias, reg);
308 if (taddr == OF_BAD_ADDR) {
309 printk(KERN_ERR "via-pmu: Can't translate address !\n");
313 spin_lock_init(&pmu_lock);
317 pmu_intr_mask = PMU_INT_PCEJECT |
322 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
323 || device_is_compatible(vias->parent, "ohare")))
324 pmu_kind = PMU_OHARE_BASED;
325 else if (device_is_compatible(vias->parent, "paddington"))
326 pmu_kind = PMU_PADDINGTON_BASED;
327 else if (device_is_compatible(vias->parent, "heathrow"))
328 pmu_kind = PMU_HEATHROW_BASED;
329 else if (device_is_compatible(vias->parent, "Keylargo")
330 || device_is_compatible(vias->parent, "K2-Keylargo")) {
331 struct device_node *gpiop;
332 u64 gaddr = OF_BAD_ADDR;
334 pmu_kind = PMU_KEYLARGO_BASED;
335 pmu_has_adb = (find_type_devices("adb") != NULL);
336 pmu_intr_mask = PMU_INT_PCEJECT |
342 gpiop = of_find_node_by_name(NULL, "gpio");
344 reg = (u32 *)get_property(gpiop, "reg", NULL);
346 gaddr = of_translate_address(gpiop, reg);
347 if (gaddr != OF_BAD_ADDR)
348 gpio_reg = ioremap(gaddr, 0x10);
350 if (gpio_reg == NULL)
351 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
353 pmu_kind = PMU_UNKNOWN;
355 via = ioremap(taddr, 0x2000);
357 printk(KERN_ERR "via-pmu: Can't map address !\n");
361 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
362 out_8(&via[IFR], 0x7f); /* clear IFR */
371 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
372 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
374 sys_ctrler = SYS_CTRLER_PMU;
384 static int pmu_probe(void)
386 return vias == NULL? -ENODEV: 0;
389 static int __init pmu_init(void)
395 #endif /* CONFIG_ADB */
398 * We can't wait until pmu_init gets called, that happens too late.
399 * It happens after IDE and SCSI initialization, which can take a few
400 * seconds, and by that time the PMU could have given up on us and
402 * Thus this is called with arch_initcall rather than device_initcall.
404 static int __init via_pmu_start(void)
409 batt_req.complete = 1;
411 #ifndef CONFIG_PPC_MERGE
412 if (pmu_kind == PMU_KEYLARGO_BASED)
413 openpic_set_irq_priority(vias->intrs[0].line,
414 OPENPIC_PRIORITY_DEFAULT + 1);
417 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
419 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
420 vias->intrs[0].line);
424 if (pmu_kind == PMU_KEYLARGO_BASED) {
425 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
426 if (gpio_node == NULL)
427 gpio_node = of_find_node_by_name(NULL,
429 if (gpio_node && gpio_node->n_intrs > 0)
430 gpio_irq = gpio_node->intrs[0].line;
432 if (gpio_irq != -1) {
433 if (request_irq(gpio_irq, gpio1_interrupt, 0,
434 "GPIO1 ADB", (void *)0))
435 printk(KERN_ERR "pmu: can't get irq %d"
436 " (GPIO1)\n", gpio_irq);
438 gpio_irq_enabled = 1;
442 /* Enable interrupts */
443 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
445 pmu_fully_inited = 1;
447 /* Make sure PMU settle down before continuing. This is _very_ important
448 * since the IDE probe may shut interrupts down for quite a bit of time. If
449 * a PMU communication is pending while this happens, the PMU may timeout
450 * Not that on Core99 machines, the PMU keeps sending us environement
451 * messages, we should find a way to either fix IDE or make it call
452 * pmu_suspend() before masking interrupts. This can also happens while
453 * scolling with some fbdevs.
457 } while (pmu_state != idle);
462 arch_initcall(via_pmu_start);
465 * This has to be done after pci_init, which is a subsys_initcall.
467 static int __init via_pmu_dev_init(void)
472 #ifdef CONFIG_PMAC_BACKLIGHT
473 /* Initialize backlight */
474 pmu_backlight_init(vias);
478 if (machine_is_compatible("AAPL,3400/2400") ||
479 machine_is_compatible("AAPL,3500")) {
480 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
481 NULL, PMAC_MB_INFO_MODEL, 0);
482 pmu_battery_count = 1;
483 if (mb == PMAC_TYPE_COMET)
484 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
486 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
487 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
488 machine_is_compatible("PowerBook1,1")) {
489 pmu_battery_count = 2;
490 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
491 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
493 struct device_node* prim = find_devices("power-mgt");
494 u32 *prim_info = NULL;
496 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
498 /* Other stuffs here yet unknown */
499 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
500 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
501 if (pmu_battery_count > 1)
502 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
505 #endif /* CONFIG_PPC32 */
507 /* Create /proc/pmu */
508 proc_pmu_root = proc_mkdir("pmu", NULL);
512 for (i=0; i<pmu_battery_count; i++) {
514 sprintf(title, "battery_%ld", i);
515 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
516 proc_get_batt, (void *)i);
519 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
520 proc_get_info, NULL);
521 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
522 proc_get_irqstats, NULL);
523 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
524 if (proc_pmu_options) {
525 proc_pmu_options->nlink = 1;
526 proc_pmu_options->read_proc = proc_read_options;
527 proc_pmu_options->write_proc = proc_write_options;
533 device_initcall(via_pmu_dev_init);
539 struct adb_request req;
541 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
542 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
544 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
546 while (!req.complete) {
548 printk(KERN_ERR "init_pmu: no response from PMU\n");
555 /* ack all pending interrupts */
557 interrupt_data[0][0] = 1;
558 while (interrupt_data[0][0] || pmu_state != idle) {
560 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
563 if (pmu_state == idle)
565 via_pmu_interrupt(0, NULL, NULL);
569 /* Tell PMU we are ready. */
570 if (pmu_kind == PMU_KEYLARGO_BASED) {
571 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
572 while (!req.complete)
576 /* Read PMU version */
577 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
578 pmu_wait_complete(&req);
579 if (req.reply_len > 0)
580 pmu_version = req.reply[0];
582 /* Read server mode setting */
583 if (pmu_kind == PMU_KEYLARGO_BASED) {
584 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
585 PMU_PWR_GET_POWERUP_EVENTS);
586 pmu_wait_complete(&req);
587 if (req.reply_len == 2) {
588 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
589 option_server_mode = 1;
590 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
591 option_server_mode ? "enabled" : "disabled");
603 static void pmu_set_server_mode(int server_mode)
605 struct adb_request req;
607 if (pmu_kind != PMU_KEYLARGO_BASED)
610 option_server_mode = server_mode;
611 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
612 pmu_wait_complete(&req);
613 if (req.reply_len < 2)
616 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
617 PMU_PWR_SET_POWERUP_EVENTS,
618 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
620 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
621 PMU_PWR_CLR_POWERUP_EVENTS,
622 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
623 pmu_wait_complete(&req);
626 /* This new version of the code for 2400/3400/3500 powerbooks
627 * is inspired from the implementation in gkrellm-pmu
630 done_battery_state_ohare(struct adb_request* req)
634 * 0x01 : AC indicator
636 * 0x04 : battery exist
639 * 0x20 : full charged
640 * 0x40 : pcharge reset
641 * 0x80 : battery exist
643 * [1][2] : battery voltage
644 * [3] : CPU temperature
645 * [4] : battery temperature
650 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
651 long pcharge, charge, vb, vmax, lmax;
652 long vmax_charging, vmax_charged;
653 long amperage, voltage, time, max;
654 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
655 NULL, PMAC_MB_INFO_MODEL, 0);
657 if (req->reply[0] & 0x01)
658 pmu_power_flags |= PMU_PWR_AC_PRESENT;
660 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
662 if (mb == PMAC_TYPE_COMET) {
673 /* If battery installed */
674 if (req->reply[0] & 0x04) {
675 bat_flags |= PMU_BATT_PRESENT;
676 if (req->reply[0] & 0x02)
677 bat_flags |= PMU_BATT_CHARGING;
678 vb = (req->reply[1] << 8) | req->reply[2];
679 voltage = (vb * 265 + 72665) / 10;
680 amperage = req->reply[5];
681 if ((req->reply[0] & 0x01) == 0) {
683 vb += ((amperage - 200) * 15)/100;
684 } else if (req->reply[0] & 0x02) {
685 vb = (vb * 97) / 100;
686 vmax = vmax_charging;
688 charge = (100 * vb) / vmax;
689 if (req->reply[0] & 0x40) {
690 pcharge = (req->reply[6] << 8) + req->reply[7];
694 pcharge = 100 - pcharge / lmax;
695 if (pcharge < charge)
699 time = (charge * 16440) / amperage;
703 amperage = -amperage;
705 charge = max = amperage = voltage = time = 0;
707 pmu_batteries[pmu_cur_battery].flags = bat_flags;
708 pmu_batteries[pmu_cur_battery].charge = charge;
709 pmu_batteries[pmu_cur_battery].max_charge = max;
710 pmu_batteries[pmu_cur_battery].amperage = amperage;
711 pmu_batteries[pmu_cur_battery].voltage = voltage;
712 pmu_batteries[pmu_cur_battery].time_remaining = time;
714 clear_bit(0, &async_req_locks);
718 done_battery_state_smart(struct adb_request* req)
721 * [0] : format of this structure (known: 3,4,5)
734 * [4][5] : max charge
739 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
741 unsigned int capa, max, voltage;
743 if (req->reply[1] & 0x01)
744 pmu_power_flags |= PMU_PWR_AC_PRESENT;
746 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
749 capa = max = amperage = voltage = 0;
751 if (req->reply[1] & 0x04) {
752 bat_flags |= PMU_BATT_PRESENT;
753 switch(req->reply[0]) {
755 case 4: capa = req->reply[2];
757 amperage = *((signed char *)&req->reply[4]);
758 voltage = req->reply[5];
760 case 5: capa = (req->reply[2] << 8) | req->reply[3];
761 max = (req->reply[4] << 8) | req->reply[5];
762 amperage = *((signed short *)&req->reply[6]);
763 voltage = (req->reply[8] << 8) | req->reply[9];
766 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
767 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
772 if ((req->reply[1] & 0x01) && (amperage > 0))
773 bat_flags |= PMU_BATT_CHARGING;
775 pmu_batteries[pmu_cur_battery].flags = bat_flags;
776 pmu_batteries[pmu_cur_battery].charge = capa;
777 pmu_batteries[pmu_cur_battery].max_charge = max;
778 pmu_batteries[pmu_cur_battery].amperage = amperage;
779 pmu_batteries[pmu_cur_battery].voltage = voltage;
781 if ((req->reply[1] & 0x01) && (amperage > 0))
782 pmu_batteries[pmu_cur_battery].time_remaining
783 = ((max-capa) * 3600) / amperage;
785 pmu_batteries[pmu_cur_battery].time_remaining
786 = (capa * 3600) / (-amperage);
788 pmu_batteries[pmu_cur_battery].time_remaining = 0;
790 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
792 clear_bit(0, &async_req_locks);
796 query_battery_state(void)
798 if (test_and_set_bit(0, &async_req_locks))
800 if (pmu_kind == PMU_OHARE_BASED)
801 pmu_request(&batt_req, done_battery_state_ohare,
802 1, PMU_BATTERY_STATE);
804 pmu_request(&batt_req, done_battery_state_smart,
805 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
809 proc_get_info(char *page, char **start, off_t off,
810 int count, int *eof, void *data)
814 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
815 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
816 p += sprintf(p, "AC Power : %d\n",
817 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
818 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
824 proc_get_irqstats(char *page, char **start, off_t off,
825 int count, int *eof, void *data)
829 static const char *irq_names[] = {
830 "Total CB1 triggered events",
831 "Total GPIO1 triggered events",
832 "PC-Card eject button",
833 "Sound/Brightness button",
835 "Battery state change",
836 "Environment interrupt",
838 "Ghost interrupt (zero len)",
839 "Empty interrupt (empty mask)",
843 for (i=0; i<11; i++) {
844 p += sprintf(p, " %2u: %10u (%s)\n",
845 i, pmu_irq_stats[i], irq_names[i]);
851 proc_get_batt(char *page, char **start, off_t off,
852 int count, int *eof, void *data)
854 long batnum = (long)data;
857 p += sprintf(p, "\n");
858 p += sprintf(p, "flags : %08x\n",
859 pmu_batteries[batnum].flags);
860 p += sprintf(p, "charge : %d\n",
861 pmu_batteries[batnum].charge);
862 p += sprintf(p, "max_charge : %d\n",
863 pmu_batteries[batnum].max_charge);
864 p += sprintf(p, "current : %d\n",
865 pmu_batteries[batnum].amperage);
866 p += sprintf(p, "voltage : %d\n",
867 pmu_batteries[batnum].voltage);
868 p += sprintf(p, "time rem. : %d\n",
869 pmu_batteries[batnum].time_remaining);
875 proc_read_options(char *page, char **start, off_t off,
876 int count, int *eof, void *data)
880 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
881 if (pmu_kind == PMU_KEYLARGO_BASED &&
882 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
883 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
885 if (pmu_kind == PMU_KEYLARGO_BASED)
886 p += sprintf(p, "server_mode=%d\n", option_server_mode);
892 proc_write_options(struct file *file, const char __user *buffer,
893 unsigned long count, void *data)
897 unsigned long fcount = count;
903 if (copy_from_user(tmp, buffer, count))
911 while(*val && (*val != '=')) {
921 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
922 if (pmu_kind == PMU_KEYLARGO_BASED &&
923 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
924 if (!strcmp(label, "lid_wakeup"))
925 option_lid_wakeup = ((*val) == '1');
927 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
929 new_value = ((*val) == '1');
930 if (new_value != option_server_mode)
931 pmu_set_server_mode(new_value);
937 /* Send an ADB command */
939 pmu_send_request(struct adb_request *req, int sync)
943 if ((vias == NULL) || (!pmu_fully_inited)) {
950 switch (req->data[0]) {
952 for (i = 0; i < req->nbytes - 1; ++i)
953 req->data[i] = req->data[i+1];
955 if (pmu_data_len[req->data[0]][1] != 0) {
956 req->reply[0] = ADB_RET_OK;
960 ret = pmu_queue_request(req);
963 switch (req->data[1]) {
965 if (req->nbytes != 2)
967 req->data[0] = PMU_READ_RTC;
970 req->reply[0] = CUDA_PACKET;
972 req->reply[2] = CUDA_GET_TIME;
973 ret = pmu_queue_request(req);
976 if (req->nbytes != 6)
978 req->data[0] = PMU_SET_RTC;
980 for (i = 1; i <= 4; ++i)
981 req->data[i] = req->data[i+1];
983 req->reply[0] = CUDA_PACKET;
985 req->reply[2] = CUDA_SET_TIME;
986 ret = pmu_queue_request(req);
993 for (i = req->nbytes - 1; i > 1; --i)
994 req->data[i+2] = req->data[i];
995 req->data[3] = req->nbytes - 2;
996 req->data[2] = pmu_adb_flags;
997 /*req->data[1] = req->data[1];*/
998 req->data[0] = PMU_ADB_CMD;
1000 req->reply_expected = 1;
1002 ret = pmu_queue_request(req);
1011 while (!req->complete)
1017 /* Enable/disable autopolling */
1019 pmu_adb_autopoll(int devs)
1021 struct adb_request req;
1023 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1028 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1029 adb_dev_map >> 8, adb_dev_map);
1032 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1035 while (!req.complete)
1040 /* Reset the ADB bus */
1042 pmu_adb_reset_bus(void)
1044 struct adb_request req;
1045 int save_autopoll = adb_dev_map;
1047 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1050 /* anyone got a better idea?? */
1051 pmu_adb_autopoll(0);
1055 req.data[0] = PMU_ADB_CMD;
1057 req.data[2] = ADB_BUSRESET;
1061 req.reply_expected = 1;
1062 if (pmu_queue_request(&req) != 0) {
1063 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1066 pmu_wait_complete(&req);
1068 if (save_autopoll != 0)
1069 pmu_adb_autopoll(save_autopoll);
1073 #endif /* CONFIG_ADB */
1075 /* Construct and send a pmu request */
1077 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1086 if (nbytes < 0 || nbytes > 32) {
1087 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1091 req->nbytes = nbytes;
1093 va_start(list, nbytes);
1094 for (i = 0; i < nbytes; ++i)
1095 req->data[i] = va_arg(list, int);
1098 req->reply_expected = 0;
1099 return pmu_queue_request(req);
1103 pmu_queue_request(struct adb_request *req)
1105 unsigned long flags;
1112 if (req->nbytes <= 0) {
1116 nsend = pmu_data_len[req->data[0]][0];
1117 if (nsend >= 0 && req->nbytes != nsend + 1) {
1126 spin_lock_irqsave(&pmu_lock, flags);
1127 if (current_req != 0) {
1128 last_req->next = req;
1133 if (pmu_state == idle)
1136 spin_unlock_irqrestore(&pmu_lock, flags);
1144 /* Sightly increased the delay, I had one occurrence of the message
1148 while ((in_8(&via[B]) & TACK) == 0) {
1149 if (--timeout < 0) {
1150 printk(KERN_ERR "PMU not responding (!ack)\n");
1157 /* New PMU seems to be very sensitive to those timings, so we make sure
1158 * PCI is flushed immediately */
1162 volatile unsigned char __iomem *v = via;
1164 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1166 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1173 volatile unsigned char __iomem *v = via;
1175 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1176 in_8(&v[SR]); /* resets SR */
1177 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1182 pmu_done(struct adb_request *req)
1184 void (*done)(struct adb_request *) = req->done;
1187 /* Here, we assume that if the request has a done member, the
1188 * struct request will survive to setting req->complete to 1
1197 struct adb_request *req;
1199 /* assert pmu_state == idle */
1200 /* get the packet to send */
1202 if (req == 0 || pmu_state != idle
1203 || (/*req->reply_expected && */req_awaiting_reply))
1206 pmu_state = sending;
1208 data_len = pmu_data_len[req->data[0]][0];
1210 /* Sounds safer to make sure ACK is high before writing. This helped
1211 * kill a problem with ADB and some iBooks
1214 /* set the shift register to shift out and send a byte */
1215 send_byte(req->data[0]);
1225 via_pmu_interrupt(0, NULL, NULL);
1235 /* Kicks ADB read when PMU is suspended */
1236 adb_int_pending = 1;
1238 via_pmu_interrupt(0, NULL, NULL);
1239 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1240 || req_awaiting_reply));
1244 pmu_wait_complete(struct adb_request *req)
1248 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1249 via_pmu_interrupt(0, NULL, NULL);
1252 /* This function loops until the PMU is idle and prevents it from
1253 * anwsering to ADB interrupts. pmu_request can still be called.
1254 * This is done to avoid spurrious shutdowns when we know we'll have
1255 * interrupts switched off for a long time
1260 unsigned long flags;
1261 #ifdef SUSPEND_USES_PMU
1262 struct adb_request *req;
1267 spin_lock_irqsave(&pmu_lock, flags);
1269 if (pmu_suspended > 1) {
1270 spin_unlock_irqrestore(&pmu_lock, flags);
1275 spin_unlock_irqrestore(&pmu_lock, flags);
1276 if (req_awaiting_reply)
1277 adb_int_pending = 1;
1278 via_pmu_interrupt(0, NULL, NULL);
1279 spin_lock_irqsave(&pmu_lock, flags);
1280 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1281 #ifdef SUSPEND_USES_PMU
1282 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1283 spin_unlock_irqrestore(&pmu_lock, flags);
1284 while(!req.complete)
1286 #else /* SUSPEND_USES_PMU */
1288 disable_irq_nosync(gpio_irq);
1289 out_8(&via[IER], CB1_INT | IER_CLR);
1290 spin_unlock_irqrestore(&pmu_lock, flags);
1291 #endif /* SUSPEND_USES_PMU */
1300 unsigned long flags;
1302 if (!via || (pmu_suspended < 1))
1305 spin_lock_irqsave(&pmu_lock, flags);
1307 if (pmu_suspended > 0) {
1308 spin_unlock_irqrestore(&pmu_lock, flags);
1311 adb_int_pending = 1;
1312 #ifdef SUSPEND_USES_PMU
1313 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1314 spin_unlock_irqrestore(&pmu_lock, flags);
1315 while(!req.complete)
1317 #else /* SUSPEND_USES_PMU */
1319 enable_irq(gpio_irq);
1320 out_8(&via[IER], CB1_INT | IER_SET);
1321 spin_unlock_irqrestore(&pmu_lock, flags);
1323 #endif /* SUSPEND_USES_PMU */
1326 /* Interrupt data could be the result data from an ADB cmd */
1328 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1330 unsigned char ints, pirq;
1334 if (drop_interrupts || len < 1) {
1335 adb_int_pending = 0;
1340 /* Get PMU interrupt mask */
1343 /* Record zero interrupts for stats */
1347 /* Hack to deal with ADB autopoll flag */
1348 if (ints & PMU_INT_ADB)
1349 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1354 if (i > pmu_irq_stats[10])
1355 pmu_irq_stats[10] = i;
1359 for (pirq = 0; pirq < 8; pirq++)
1360 if (ints & (1 << pirq))
1362 pmu_irq_stats[pirq]++;
1364 ints &= ~(1 << pirq);
1366 /* Note: for some reason, we get an interrupt with len=1,
1367 * data[0]==0 after each normal ADB interrupt, at least
1368 * on the Pismo. Still investigating... --BenH
1370 if ((1 << pirq) & PMU_INT_ADB) {
1371 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1372 struct adb_request *req = req_awaiting_reply;
1374 printk(KERN_ERR "PMU: extra ADB reply\n");
1377 req_awaiting_reply = NULL;
1381 memcpy(req->reply, data + 1, len - 1);
1382 req->reply_len = len - 1;
1386 if (len == 4 && data[1] == 0x2c) {
1387 extern int xmon_wants_key, xmon_adb_keycode;
1388 if (xmon_wants_key) {
1389 xmon_adb_keycode = data[2];
1395 * XXX On the [23]400 the PMU gives us an up
1396 * event for keycodes 0x74 or 0x75 when the PC
1397 * card eject buttons are released, so we
1398 * ignore those events.
1400 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1401 && data[1] == 0x2c && data[3] == 0xff
1402 && (data[2] & ~1) == 0xf4))
1403 adb_input(data+1, len-1, regs, 1);
1404 #endif /* CONFIG_ADB */
1407 /* Sound/brightness button pressed */
1408 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1409 #ifdef CONFIG_PMAC_BACKLIGHT
1411 #ifdef CONFIG_INPUT_ADBHID
1412 if (!disable_kernel_backlight)
1413 #endif /* CONFIG_INPUT_ADBHID */
1414 pmac_backlight_set_legacy_brightness(data[1] >> 4);
1415 #endif /* CONFIG_PMAC_BACKLIGHT */
1417 /* Tick interrupt */
1418 else if ((1 << pirq) & PMU_INT_TICK) {
1419 /* Environement or tick interrupt, query batteries */
1420 if (pmu_battery_count) {
1421 if ((--query_batt_timer) == 0) {
1422 query_battery_state();
1423 query_batt_timer = BATTERY_POLLING_COUNT;
1427 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1428 if (pmu_battery_count)
1429 query_battery_state();
1430 pmu_pass_intr(data, len);
1431 /* len == 6 is probably a bad check. But how do I
1432 * know what PMU versions send what events here? */
1434 via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
1435 via_pmu_event(PMU_EVT_LID, data[1]&1);
1438 pmu_pass_intr(data, len);
1443 static struct adb_request*
1444 pmu_sr_intr(struct pt_regs *regs)
1446 struct adb_request *req;
1449 if (via[B] & TREQ) {
1450 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1451 out_8(&via[IFR], SR_INT);
1454 /* The ack may not yet be low when we get the interrupt */
1455 while ((in_8(&via[B]) & TACK) != 0)
1458 /* if reading grab the byte, and reset the interrupt */
1459 if (pmu_state == reading || pmu_state == reading_intr)
1460 bite = in_8(&via[SR]);
1462 /* reset TREQ and wait for TACK to go high */
1463 out_8(&via[B], in_8(&via[B]) | TREQ);
1466 switch (pmu_state) {
1470 data_len = req->nbytes - 1;
1471 send_byte(data_len);
1474 if (data_index <= data_len) {
1475 send_byte(req->data[data_index++]);
1479 data_len = pmu_data_len[req->data[0]][1];
1480 if (data_len == 0) {
1482 current_req = req->next;
1483 if (req->reply_expected)
1484 req_awaiting_reply = req;
1488 pmu_state = reading;
1490 reply_ptr = req->reply + req->reply_len;
1498 pmu_state = reading_intr;
1499 reply_ptr = interrupt_data[int_data_last];
1501 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1502 enable_irq(gpio_irq);
1503 gpio_irq_enabled = 1;
1509 if (data_len == -1) {
1512 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1513 } else if (data_index < 32) {
1514 reply_ptr[data_index++] = bite;
1516 if (data_index < data_len) {
1521 if (pmu_state == reading_intr) {
1523 int_data_state[int_data_last] = int_data_ready;
1524 interrupt_data_len[int_data_last] = data_len;
1528 * For PMU sleep and freq change requests, we lock the
1529 * PMU until it's explicitely unlocked. This avoids any
1530 * spurrious event polling getting in
1532 current_req = req->next;
1533 req->reply_len += data_index;
1534 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1543 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1550 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1552 unsigned long flags;
1556 struct adb_request *req = NULL;
1559 /* This is a bit brutal, we can probably do better */
1560 spin_lock_irqsave(&pmu_lock, flags);
1564 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1568 if (++nloop > 1000) {
1569 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1570 "intr=%x, ier=%x pmu_state=%d\n",
1571 intr, in_8(&via[IER]), pmu_state);
1574 out_8(&via[IFR], intr);
1575 if (intr & CB1_INT) {
1576 adb_int_pending = 1;
1579 if (intr & SR_INT) {
1580 req = pmu_sr_intr(regs);
1587 if (pmu_state == idle) {
1588 if (adb_int_pending) {
1589 if (int_data_state[0] == int_data_empty)
1591 else if (int_data_state[1] == int_data_empty)
1596 int_data_state[int_data_last] = int_data_fill;
1597 /* Sounds safer to make sure ACK is high before writing.
1598 * This helped kill a problem with ADB and some iBooks
1601 send_byte(PMU_INT_ACK);
1602 adb_int_pending = 0;
1603 } else if (current_req)
1607 /* Mark the oldest buffer for flushing */
1608 if (int_data_state[!int_data_last] == int_data_ready) {
1609 int_data_state[!int_data_last] = int_data_flush;
1610 int_data = !int_data_last;
1611 } else if (int_data_state[int_data_last] == int_data_ready) {
1612 int_data_state[int_data_last] = int_data_flush;
1613 int_data = int_data_last;
1616 spin_unlock_irqrestore(&pmu_lock, flags);
1618 /* Deal with completed PMU requests outside of the lock */
1624 /* Deal with interrupt datas outside of the lock */
1625 if (int_data >= 0) {
1626 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1627 spin_lock_irqsave(&pmu_lock, flags);
1629 int_data_state[int_data] = int_data_empty;
1634 return IRQ_RETVAL(handled);
1640 unsigned long flags;
1642 spin_lock_irqsave(&pmu_lock, flags);
1643 if (pmu_state == locked)
1645 adb_int_pending = 1;
1646 spin_unlock_irqrestore(&pmu_lock, flags);
1651 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1653 unsigned long flags;
1655 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1656 spin_lock_irqsave(&pmu_lock, flags);
1657 if (gpio_irq_enabled > 0) {
1658 disable_irq_nosync(gpio_irq);
1659 gpio_irq_enabled = 0;
1662 adb_int_pending = 1;
1663 spin_unlock_irqrestore(&pmu_lock, flags);
1664 via_pmu_interrupt(0, NULL, NULL);
1671 pmu_enable_irled(int on)
1673 struct adb_request req;
1677 if (pmu_kind == PMU_KEYLARGO_BASED)
1680 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1681 (on ? PMU_POW_ON : PMU_POW_OFF));
1682 pmu_wait_complete(&req);
1688 struct adb_request req;
1693 local_irq_disable();
1695 drop_interrupts = 1;
1697 if (pmu_kind != PMU_KEYLARGO_BASED) {
1698 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1700 while(!req.complete)
1704 pmu_request(&req, NULL, 1, PMU_RESET);
1705 pmu_wait_complete(&req);
1713 struct adb_request req;
1718 local_irq_disable();
1720 drop_interrupts = 1;
1722 if (pmu_kind != PMU_KEYLARGO_BASED) {
1723 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1725 pmu_wait_complete(&req);
1727 /* Disable server mode on shutdown or we'll just
1730 pmu_set_server_mode(0);
1733 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1734 'M', 'A', 'T', 'T');
1735 pmu_wait_complete(&req);
1748 static LIST_HEAD(sleep_notifiers);
1751 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1753 struct list_head *list;
1754 struct pmu_sleep_notifier *notifier;
1756 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1757 list = list->next) {
1758 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1759 if (n->priority > notifier->priority)
1762 __list_add(&n->list, list->prev, list);
1765 EXPORT_SYMBOL(pmu_register_sleep_notifier);
1768 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1770 if (n->list.next == 0)
1773 n->list.next = NULL;
1776 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
1777 #endif /* CONFIG_PM */
1779 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1781 /* Sleep is broadcast last-to-first */
1783 broadcast_sleep(int when, int fallback)
1785 int ret = PBOOK_SLEEP_OK;
1786 struct list_head *list;
1787 struct pmu_sleep_notifier *notifier;
1789 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1790 list = list->prev) {
1791 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1792 ret = notifier->notifier_call(notifier, when);
1793 if (ret != PBOOK_SLEEP_OK) {
1794 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
1795 when, notifier, notifier->notifier_call);
1796 for (; list != &sleep_notifiers; list = list->next) {
1797 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1798 notifier->notifier_call(notifier, fallback);
1806 /* Wake is broadcast first-to-last */
1808 broadcast_wake(void)
1810 int ret = PBOOK_SLEEP_OK;
1811 struct list_head *list;
1812 struct pmu_sleep_notifier *notifier;
1814 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1815 list = list->next) {
1816 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1817 notifier->notifier_call(notifier, PBOOK_WAKE);
1823 * This struct is used to store config register values for
1824 * PCI devices which may get powered off when we sleep.
1826 static struct pci_save {
1827 #ifndef HACKED_PCI_SAVE
1836 static int pbook_npci_saves;
1839 pbook_alloc_pci_save(void)
1842 struct pci_dev *pd = NULL;
1845 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1850 pbook_pci_saves = (struct pci_save *)
1851 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
1852 pbook_npci_saves = npci;
1856 pbook_free_pci_save(void)
1858 if (pbook_pci_saves == NULL)
1860 kfree(pbook_pci_saves);
1861 pbook_pci_saves = NULL;
1862 pbook_npci_saves = 0;
1866 pbook_pci_save(void)
1868 struct pci_save *ps = pbook_pci_saves;
1869 struct pci_dev *pd = NULL;
1870 int npci = pbook_npci_saves;
1875 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1878 #ifndef HACKED_PCI_SAVE
1879 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1880 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1881 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1882 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1886 pci_read_config_dword(pd, i<<4, &ps->config[i]);
1892 /* For this to work, we must take care of a few things: If gmac was enabled
1893 * during boot, it will be in the pci dev list. If it's disabled at this point
1894 * (and it will probably be), then you can't access it's config space.
1897 pbook_pci_restore(void)
1900 struct pci_save *ps = pbook_pci_saves - 1;
1901 struct pci_dev *pd = NULL;
1902 int npci = pbook_npci_saves;
1905 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1906 #ifdef HACKED_PCI_SAVE
1912 pci_write_config_dword(pd, i<<4, ps->config[i]);
1913 pci_write_config_dword(pd, 4, ps->config[1]);
1918 if (ps->command == 0)
1920 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1921 if ((ps->command & ~cmd) == 0)
1923 switch (pd->hdr_type) {
1924 case PCI_HEADER_TYPE_NORMAL:
1925 for (j = 0; j < 6; ++j)
1926 pci_write_config_dword(pd,
1927 PCI_BASE_ADDRESS_0 + j*4,
1928 pd->resource[j].start);
1929 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1931 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1933 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1935 pci_write_config_word(pd, PCI_COMMAND, ps->command);
1943 /* N.B. This doesn't work on the 3400 */
1947 struct adb_request req;
1949 memset(&req, 0, sizeof(req));
1951 for (; n > 0; --n) {
1958 req.reply[0] = ADB_RET_OK;
1960 req.reply_expected = 0;
1961 pmu_polled_request(&req);
1969 req.reply[0] = ADB_RET_OK;
1971 req.reply_expected = 0;
1972 pmu_polled_request(&req);
1980 * Put the powerbook to sleep.
1983 static u32 save_via[8];
1986 save_via_state(void)
1988 save_via[0] = in_8(&via[ANH]);
1989 save_via[1] = in_8(&via[DIRA]);
1990 save_via[2] = in_8(&via[B]);
1991 save_via[3] = in_8(&via[DIRB]);
1992 save_via[4] = in_8(&via[PCR]);
1993 save_via[5] = in_8(&via[ACR]);
1994 save_via[6] = in_8(&via[T1CL]);
1995 save_via[7] = in_8(&via[T1CH]);
1998 restore_via_state(void)
2000 out_8(&via[ANH], save_via[0]);
2001 out_8(&via[DIRA], save_via[1]);
2002 out_8(&via[B], save_via[2]);
2003 out_8(&via[DIRB], save_via[3]);
2004 out_8(&via[PCR], save_via[4]);
2005 out_8(&via[ACR], save_via[5]);
2006 out_8(&via[T1CL], save_via[6]);
2007 out_8(&via[T1CH], save_via[7]);
2008 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
2009 out_8(&via[IFR], 0x7f); /* clear IFR */
2010 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2014 pmac_suspend_devices(void)
2018 pm_prepare_console();
2020 /* Notify old-style device drivers & userland */
2021 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2022 if (ret != PBOOK_SLEEP_OK) {
2023 printk(KERN_ERR "Sleep rejected by drivers\n");
2027 /* Sync the disks. */
2028 /* XXX It would be nice to have some way to ensure that
2029 * nobody is dirtying any new buffers while we wait. That
2030 * could be achieved using the refrigerator for processes
2035 /* Sleep can fail now. May not be very robust but useful for debugging */
2036 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2037 if (ret != PBOOK_SLEEP_OK) {
2038 printk(KERN_ERR "Driver sleep failed\n");
2042 /* Send suspend call to devices, hold the device core's dpm_sem */
2043 ret = device_suspend(PMSG_SUSPEND);
2046 printk(KERN_ERR "Driver sleep failed\n");
2050 /* Call platform functions marked "on sleep" */
2051 pmac_pfunc_i2c_suspend();
2052 pmac_pfunc_base_suspend();
2054 /* Stop preemption */
2057 /* Make sure the decrementer won't interrupt us */
2058 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2059 /* Make sure any pending DEC interrupt occurring while we did
2060 * the above didn't re-enable the DEC */
2062 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2064 /* We can now disable MSR_EE. This code of course works properly only
2065 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2066 * stop the "other" CPUs way before we do all that stuff.
2068 local_irq_disable();
2070 /* Broadcast power down irq
2071 * This isn't that useful in most cases (only directly wired devices can
2072 * use this but still... This will take care of sysdev's as well, so
2073 * we exit from here with local irqs disabled and PIC off.
2075 ret = device_power_down(PMSG_SUSPEND);
2077 wakeup_decrementer();
2082 printk(KERN_ERR "Driver powerdown failed\n");
2086 /* Wait for completion of async requests */
2087 while (!batt_req.complete)
2090 /* Giveup the lazy FPU & vec so we don't have to back them
2091 * up from the low level code
2095 #ifdef CONFIG_ALTIVEC
2096 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2097 enable_kernel_altivec();
2098 #endif /* CONFIG_ALTIVEC */
2104 pmac_wakeup_devices(void)
2108 /* Power back up system devices (including the PIC) */
2111 /* Force a poll of ADB interrupts */
2112 adb_int_pending = 1;
2113 via_pmu_interrupt(0, NULL, NULL);
2115 /* Restart jiffies & scheduling */
2116 wakeup_decrementer();
2118 /* Re-enable local CPU interrupts */
2123 /* Call platform functions marked "on wake" */
2124 pmac_pfunc_base_resume();
2125 pmac_pfunc_i2c_resume();
2127 /* Resume devices */
2130 /* Notify old style drivers */
2133 pm_restore_console();
2138 #define GRACKLE_PM (1<<7)
2139 #define GRACKLE_DOZE (1<<5)
2140 #define GRACKLE_NAP (1<<4)
2141 #define GRACKLE_SLEEP (1<<3)
2143 static int powerbook_sleep_grackle(void)
2145 unsigned long save_l2cr;
2146 unsigned short pmcr1;
2147 struct adb_request req;
2149 struct pci_dev *grackle;
2151 grackle = pci_find_slot(0, 0);
2155 ret = pmac_suspend_devices();
2157 printk(KERN_ERR "Sleep rejected by devices\n");
2161 /* Turn off various things. Darwin does some retry tests here... */
2162 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2163 pmu_wait_complete(&req);
2164 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2165 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2166 pmu_wait_complete(&req);
2168 /* For 750, save backside cache setting and disable it */
2169 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2171 if (!__fake_sleep) {
2172 /* Ask the PMU to put us to sleep */
2173 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2174 pmu_wait_complete(&req);
2177 /* The VIA is supposed not to be restored correctly*/
2179 /* We shut down some HW */
2180 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2182 pci_read_config_word(grackle, 0x70, &pmcr1);
2183 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2184 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2185 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2186 pci_write_config_word(grackle, 0x70, pmcr1);
2188 /* Call low-level ASM sleep handler */
2192 low_sleep_handler();
2194 /* We're awake again, stop grackle PM */
2195 pci_read_config_word(grackle, 0x70, &pmcr1);
2196 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2197 pci_write_config_word(grackle, 0x70, pmcr1);
2199 /* Make sure the PMU is idle */
2200 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2201 restore_via_state();
2203 /* Restore L2 cache */
2204 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2205 _set_L2CR(save_l2cr);
2207 /* Restore userland MMU context */
2208 set_context(current->active_mm->context.id, current->active_mm->pgd);
2210 /* Power things up */
2212 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2213 pmu_wait_complete(&req);
2214 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2215 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2216 pmu_wait_complete(&req);
2217 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2218 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2219 pmu_wait_complete(&req);
2221 pmac_wakeup_devices();
2227 powerbook_sleep_Core99(void)
2229 unsigned long save_l2cr;
2230 unsigned long save_l3cr;
2231 struct adb_request req;
2234 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2235 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2239 if (num_online_cpus() > 1 || cpu_is_offline(0))
2242 ret = pmac_suspend_devices();
2244 printk(KERN_ERR "Sleep rejected by devices\n");
2248 /* Stop environment and ADB interrupts */
2249 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2250 pmu_wait_complete(&req);
2252 /* Tell PMU what events will wake us up */
2253 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2255 pmu_wait_complete(&req);
2256 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2257 0, PMU_PWR_WAKEUP_KEY |
2258 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2259 pmu_wait_complete(&req);
2261 /* Save the state of the L2 and L3 caches */
2262 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2263 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2265 if (!__fake_sleep) {
2266 /* Ask the PMU to put us to sleep */
2267 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2268 pmu_wait_complete(&req);
2271 /* The VIA is supposed not to be restored correctly*/
2274 /* Shut down various ASICs. There's a chance that we can no longer
2275 * talk to the PMU after this, so I moved it to _after_ sending the
2276 * sleep command to it. Still need to be checked.
2278 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2280 /* Call low-level ASM sleep handler */
2284 low_sleep_handler();
2286 /* Restore Apple core ASICs state */
2287 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2290 restore_via_state();
2292 /* tweak LPJ before cpufreq is there */
2293 loops_per_jiffy *= 2;
2296 pmac_call_early_video_resume();
2298 /* Restore L2 cache */
2299 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2300 _set_L2CR(save_l2cr);
2301 /* Restore L3 cache */
2302 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2303 _set_L3CR(save_l3cr);
2305 /* Restore userland MMU context */
2306 set_context(current->active_mm->context.id, current->active_mm->pgd);
2308 /* Tell PMU we are ready */
2310 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2311 pmu_wait_complete(&req);
2312 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2313 pmu_wait_complete(&req);
2315 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2316 loops_per_jiffy /= 2;
2318 pmac_wakeup_devices();
2323 #define PB3400_MEM_CTRL 0xf8000000
2324 #define PB3400_MEM_CTRL_SLEEP 0x70
2327 powerbook_sleep_3400(void)
2332 struct adb_request sleep_req;
2333 void __iomem *mem_ctrl;
2334 unsigned int __iomem *mem_ctrl_sleep;
2336 /* first map in the memory controller registers */
2337 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2338 if (mem_ctrl == NULL) {
2339 printk("powerbook_sleep_3400: ioremap failed\n");
2342 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2344 /* Allocate room for PCI save */
2345 pbook_alloc_pci_save();
2347 ret = pmac_suspend_devices();
2349 pbook_free_pci_save();
2350 printk(KERN_ERR "Sleep rejected by devices\n");
2354 /* Save the state of PCI config space for some slots */
2357 /* Set the memory controller to keep the memory refreshed
2358 while we're asleep */
2359 for (i = 0x403f; i >= 0x4000; --i) {
2360 out_be32(mem_ctrl_sleep, i);
2362 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2368 /* Ask the PMU to put us to sleep */
2369 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2370 while (!sleep_req.complete)
2373 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2375 /* displacement-flush the L2 cache - necessary? */
2376 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2377 i = *(volatile int *)p;
2380 /* Put the CPU into sleep mode */
2381 hid0 = mfspr(SPRN_HID0);
2382 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2383 mtspr(SPRN_HID0, hid0);
2384 mtmsr(mfmsr() | MSR_POW | MSR_EE);
2387 /* OK, we're awake again, start restoring things */
2388 out_be32(mem_ctrl_sleep, 0x3f);
2389 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2390 pbook_pci_restore();
2393 /* wait for the PMU interrupt sequence to complete */
2397 pmac_wakeup_devices();
2398 pbook_free_pci_save();
2404 #endif /* CONFIG_PM && CONFIG_PPC32 */
2407 * Support for /dev/pmu device
2409 #define RB_SIZE 0x10
2410 struct pmu_private {
2411 struct list_head list;
2416 unsigned char data[16];
2418 wait_queue_head_t wait;
2420 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2421 int backlight_locker;
2422 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2425 static LIST_HEAD(all_pmu_pvt);
2426 static DEFINE_SPINLOCK(all_pvt_lock);
2429 pmu_pass_intr(unsigned char *data, int len)
2431 struct pmu_private *pp;
2432 struct list_head *list;
2434 unsigned long flags;
2436 if (len > sizeof(pp->rb_buf[0].data))
2437 len = sizeof(pp->rb_buf[0].data);
2438 spin_lock_irqsave(&all_pvt_lock, flags);
2439 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2440 pp = list_entry(list, struct pmu_private, list);
2441 spin_lock(&pp->lock);
2445 if (i != pp->rb_get) {
2446 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2448 memcpy(rp->data, data, len);
2450 wake_up_interruptible(&pp->wait);
2452 spin_unlock(&pp->lock);
2454 spin_unlock_irqrestore(&all_pvt_lock, flags);
2458 pmu_open(struct inode *inode, struct file *file)
2460 struct pmu_private *pp;
2461 unsigned long flags;
2463 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2466 pp->rb_get = pp->rb_put = 0;
2467 spin_lock_init(&pp->lock);
2468 init_waitqueue_head(&pp->wait);
2469 spin_lock_irqsave(&all_pvt_lock, flags);
2470 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2471 pp->backlight_locker = 0;
2472 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2473 list_add(&pp->list, &all_pmu_pvt);
2474 spin_unlock_irqrestore(&all_pvt_lock, flags);
2475 file->private_data = pp;
2480 pmu_read(struct file *file, char __user *buf,
2481 size_t count, loff_t *ppos)
2483 struct pmu_private *pp = file->private_data;
2484 DECLARE_WAITQUEUE(wait, current);
2485 unsigned long flags;
2488 if (count < 1 || pp == 0)
2490 if (!access_ok(VERIFY_WRITE, buf, count))
2493 spin_lock_irqsave(&pp->lock, flags);
2494 add_wait_queue(&pp->wait, &wait);
2495 current->state = TASK_INTERRUPTIBLE;
2499 if (pp->rb_get != pp->rb_put) {
2501 struct rb_entry *rp = &pp->rb_buf[i];
2503 spin_unlock_irqrestore(&pp->lock, flags);
2506 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2510 spin_lock_irqsave(&pp->lock, flags);
2515 if (file->f_flags & O_NONBLOCK)
2518 if (signal_pending(current))
2520 spin_unlock_irqrestore(&pp->lock, flags);
2522 spin_lock_irqsave(&pp->lock, flags);
2524 current->state = TASK_RUNNING;
2525 remove_wait_queue(&pp->wait, &wait);
2526 spin_unlock_irqrestore(&pp->lock, flags);
2532 pmu_write(struct file *file, const char __user *buf,
2533 size_t count, loff_t *ppos)
2539 pmu_fpoll(struct file *filp, poll_table *wait)
2541 struct pmu_private *pp = filp->private_data;
2542 unsigned int mask = 0;
2543 unsigned long flags;
2547 poll_wait(filp, &pp->wait, wait);
2548 spin_lock_irqsave(&pp->lock, flags);
2549 if (pp->rb_get != pp->rb_put)
2551 spin_unlock_irqrestore(&pp->lock, flags);
2556 pmu_release(struct inode *inode, struct file *file)
2558 struct pmu_private *pp = file->private_data;
2559 unsigned long flags;
2563 file->private_data = NULL;
2564 spin_lock_irqsave(&all_pvt_lock, flags);
2565 list_del(&pp->list);
2566 spin_unlock_irqrestore(&all_pvt_lock, flags);
2567 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2568 if (pp->backlight_locker) {
2569 spin_lock_irqsave(&pmu_lock, flags);
2570 disable_kernel_backlight--;
2571 spin_unlock_irqrestore(&pmu_lock, flags);
2573 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2581 pmu_ioctl(struct inode * inode, struct file *filp,
2582 u_int cmd, u_long arg)
2584 __u32 __user *argp = (__u32 __user *)arg;
2585 int error = -EINVAL;
2588 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2590 if (!capable(CAP_SYS_ADMIN))
2592 if (sleep_in_progress)
2594 sleep_in_progress = 1;
2596 case PMU_OHARE_BASED:
2597 error = powerbook_sleep_3400();
2599 case PMU_HEATHROW_BASED:
2600 case PMU_PADDINGTON_BASED:
2601 error = powerbook_sleep_grackle();
2603 case PMU_KEYLARGO_BASED:
2604 error = powerbook_sleep_Core99();
2609 sleep_in_progress = 0;
2611 case PMU_IOC_CAN_SLEEP:
2612 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2613 return put_user(0, argp);
2615 return put_user(1, argp);
2616 #endif /* CONFIG_PM && CONFIG_PPC32 */
2618 #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2619 /* Compatibility ioctl's for backlight */
2620 case PMU_IOC_GET_BACKLIGHT:
2624 if (sleep_in_progress)
2627 brightness = pmac_backlight_get_legacy_brightness();
2631 return put_user(brightness, argp);
2634 case PMU_IOC_SET_BACKLIGHT:
2638 if (sleep_in_progress)
2641 error = get_user(brightness, argp);
2645 return pmac_backlight_set_legacy_brightness(brightness);
2647 #ifdef CONFIG_INPUT_ADBHID
2648 case PMU_IOC_GRAB_BACKLIGHT: {
2649 struct pmu_private *pp = filp->private_data;
2650 unsigned long flags;
2652 if (pp->backlight_locker)
2654 pp->backlight_locker = 1;
2655 spin_lock_irqsave(&pmu_lock, flags);
2656 disable_kernel_backlight++;
2657 spin_unlock_irqrestore(&pmu_lock, flags);
2660 #endif /* CONFIG_INPUT_ADBHID */
2661 #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
2662 case PMU_IOC_GET_MODEL:
2663 return put_user(pmu_kind, argp);
2664 case PMU_IOC_HAS_ADB:
2665 return put_user(pmu_has_adb, argp);
2670 static struct file_operations pmu_device_fops = {
2676 .release = pmu_release,
2679 static struct miscdevice pmu_device = {
2680 PMU_MINOR, "pmu", &pmu_device_fops
2683 static int pmu_device_init(void)
2687 if (misc_register(&pmu_device) < 0)
2688 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2691 device_initcall(pmu_device_init);
2696 polled_handshake(volatile unsigned char __iomem *via)
2698 via[B] &= ~TREQ; eieio();
2699 while ((via[B] & TACK) != 0)
2701 via[B] |= TREQ; eieio();
2702 while ((via[B] & TACK) == 0)
2707 polled_send_byte(volatile unsigned char __iomem *via, int x)
2709 via[ACR] |= SR_OUT | SR_EXT; eieio();
2710 via[SR] = x; eieio();
2711 polled_handshake(via);
2715 polled_recv_byte(volatile unsigned char __iomem *via)
2719 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2720 x = via[SR]; eieio();
2721 polled_handshake(via);
2722 x = via[SR]; eieio();
2727 pmu_polled_request(struct adb_request *req)
2729 unsigned long flags;
2731 volatile unsigned char __iomem *v = via;
2735 l = pmu_data_len[c][0];
2736 if (l >= 0 && req->nbytes != l + 1)
2739 local_irq_save(flags);
2740 while (pmu_state != idle)
2743 while ((via[B] & TACK) == 0)
2745 polled_send_byte(v, c);
2747 l = req->nbytes - 1;
2748 polled_send_byte(v, l);
2750 for (i = 1; i <= l; ++i)
2751 polled_send_byte(v, req->data[i]);
2753 l = pmu_data_len[c][1];
2755 l = polled_recv_byte(v);
2756 for (i = 0; i < l; ++i)
2757 req->reply[i + req->reply_len] = polled_recv_byte(v);
2762 local_irq_restore(flags);
2765 #endif /* DEBUG_SLEEP */
2768 /* FIXME: This is a temporary set of callbacks to enable us
2769 * to do suspend-to-disk.
2772 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2774 static int pmu_sys_suspended = 0;
2776 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
2778 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
2781 /* Suspend PMU event interrupts */
2784 pmu_sys_suspended = 1;
2788 static int pmu_sys_resume(struct sys_device *sysdev)
2790 struct adb_request req;
2792 if (!pmu_sys_suspended)
2795 /* Tell PMU we are ready */
2796 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2797 pmu_wait_complete(&req);
2799 /* Resume PMU event interrupts */
2802 pmu_sys_suspended = 0;
2807 #endif /* CONFIG_PM && CONFIG_PPC32 */
2809 static struct sysdev_class pmu_sysclass = {
2810 set_kset_name("pmu"),
2813 static struct sys_device device_pmu = {
2815 .cls = &pmu_sysclass,
2818 static struct sysdev_driver driver_pmu = {
2819 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2820 .suspend = &pmu_sys_suspend,
2821 .resume = &pmu_sys_resume,
2822 #endif /* CONFIG_PM && CONFIG_PPC32 */
2825 static int __init init_pmu_sysfs(void)
2829 rc = sysdev_class_register(&pmu_sysclass);
2831 printk(KERN_ERR "Failed registering PMU sys class\n");
2834 rc = sysdev_register(&device_pmu);
2836 printk(KERN_ERR "Failed registering PMU sys device\n");
2839 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2841 printk(KERN_ERR "Failed registering PMU sys driver\n");
2847 subsys_initcall(init_pmu_sysfs);
2849 EXPORT_SYMBOL(pmu_request);
2850 EXPORT_SYMBOL(pmu_queue_request);
2851 EXPORT_SYMBOL(pmu_poll);
2852 EXPORT_SYMBOL(pmu_poll_adb);
2853 EXPORT_SYMBOL(pmu_wait_complete);
2854 EXPORT_SYMBOL(pmu_suspend);
2855 EXPORT_SYMBOL(pmu_resume);
2856 EXPORT_SYMBOL(pmu_unlock);
2857 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2858 EXPORT_SYMBOL(pmu_enable_irled);
2859 EXPORT_SYMBOL(pmu_battery_count);
2860 EXPORT_SYMBOL(pmu_batteries);
2861 EXPORT_SYMBOL(pmu_power_flags);
2862 #endif /* CONFIG_PM && CONFIG_PPC32 */