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/config.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/miscdevice.h>
31 #include <linux/blkdev.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/adb.h>
36 #include <linux/pmu.h>
37 #include <linux/cuda.h>
38 #include <linux/smp_lock.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
42 #include <linux/proc_fs.h>
43 #include <linux/init.h>
44 #include <linux/interrupt.h>
45 #include <linux/device.h>
46 #include <linux/sysdev.h>
47 #include <linux/suspend.h>
48 #include <linux/syscalls.h>
49 #include <linux/cpu.h>
51 #include <asm/machdep.h>
53 #include <asm/pgtable.h>
54 #include <asm/system.h>
55 #include <asm/sections.h>
57 #include <asm/pmac_feature.h>
58 #include <asm/pmac_pfunc.h>
59 #include <asm/pmac_low_i2c.h>
60 #include <asm/uaccess.h>
61 #include <asm/mmu_context.h>
62 #include <asm/cputable.h>
64 #ifdef CONFIG_PMAC_BACKLIGHT
65 #include <asm/backlight.h>
69 #include <asm/open_pic.h>
72 #include "via-pmu-event.h"
74 /* Some compile options */
75 #undef SUSPEND_USES_PMU
77 #undef HACKED_PCI_SAVE
79 /* Misc minor number allocated for /dev/pmu */
82 /* How many iterations between battery polls */
83 #define BATTERY_POLLING_COUNT 2
85 static volatile unsigned char __iomem *via;
87 /* VIA registers - spaced 0x200 bytes apart */
88 #define RS 0x200 /* skip between registers */
89 #define B 0 /* B-side data */
90 #define A RS /* A-side data */
91 #define DIRB (2*RS) /* B-side direction (1=output) */
92 #define DIRA (3*RS) /* A-side direction (1=output) */
93 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
94 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
95 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
96 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
97 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
98 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
99 #define SR (10*RS) /* Shift register */
100 #define ACR (11*RS) /* Auxiliary control register */
101 #define PCR (12*RS) /* Peripheral control register */
102 #define IFR (13*RS) /* Interrupt flag register */
103 #define IER (14*RS) /* Interrupt enable register */
104 #define ANH (15*RS) /* A-side data, no handshake */
106 /* Bits in B data register: both active low */
107 #define TACK 0x08 /* Transfer acknowledge (input) */
108 #define TREQ 0x10 /* Transfer request (output) */
111 #define SR_CTRL 0x1c /* Shift register control bits */
112 #define SR_EXT 0x0c /* Shift on external clock */
113 #define SR_OUT 0x10 /* Shift out if 1 */
115 /* Bits in IFR and IER */
116 #define IER_SET 0x80 /* set bits in IER */
117 #define IER_CLR 0 /* clear bits in IER */
118 #define SR_INT 0x04 /* Shift register full/empty */
120 #define CB1_INT 0x10 /* transition on CB1 input */
122 static volatile enum pmu_state {
131 static volatile enum int_data_state {
136 } int_data_state[2] = { int_data_empty, int_data_empty };
138 static struct adb_request *current_req;
139 static struct adb_request *last_req;
140 static struct adb_request *req_awaiting_reply;
141 static unsigned char interrupt_data[2][32];
142 static int interrupt_data_len[2];
143 static int int_data_last;
144 static unsigned char *reply_ptr;
145 static int data_index;
147 static volatile int adb_int_pending;
148 static volatile int disable_poll;
149 static struct device_node *vias;
150 static int pmu_kind = PMU_UNKNOWN;
151 static int pmu_fully_inited = 0;
152 static int pmu_has_adb;
153 static struct device_node *gpio_node;
154 static unsigned char __iomem *gpio_reg = NULL;
155 static int gpio_irq = -1;
156 static int gpio_irq_enabled = -1;
157 static volatile int pmu_suspended = 0;
158 static spinlock_t pmu_lock;
159 static u8 pmu_intr_mask;
160 static int pmu_version;
161 static int drop_interrupts;
162 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
163 static int option_lid_wakeup = 1;
164 #endif /* CONFIG_PM && CONFIG_PPC32 */
165 #if (defined(CONFIG_PM)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT_LEGACY)
166 static int sleep_in_progress;
168 static unsigned long async_req_locks;
169 static unsigned int pmu_irq_stats[11];
171 static struct proc_dir_entry *proc_pmu_root;
172 static struct proc_dir_entry *proc_pmu_info;
173 static struct proc_dir_entry *proc_pmu_irqstats;
174 static struct proc_dir_entry *proc_pmu_options;
175 static int option_server_mode;
177 int pmu_battery_count;
179 unsigned int pmu_power_flags;
180 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
181 static int query_batt_timer = BATTERY_POLLING_COUNT;
182 static struct adb_request batt_req;
183 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
185 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
186 extern int disable_kernel_backlight;
187 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
191 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
194 static int adb_dev_map = 0;
195 static int pmu_adb_flags;
197 static int pmu_probe(void);
198 static int pmu_init(void);
199 static int pmu_send_request(struct adb_request *req, int sync);
200 static int pmu_adb_autopoll(int devs);
201 static int pmu_adb_reset_bus(void);
202 #endif /* CONFIG_ADB */
204 static int init_pmu(void);
205 static void pmu_start(void);
206 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
207 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
208 static int proc_get_info(char *page, char **start, off_t off,
209 int count, int *eof, void *data);
210 static int proc_get_irqstats(char *page, char **start, off_t off,
211 int count, int *eof, void *data);
212 static void pmu_pass_intr(unsigned char *data, int len);
213 static int proc_get_batt(char *page, char **start, off_t off,
214 int count, int *eof, void *data);
215 static int proc_read_options(char *page, char **start, off_t off,
216 int count, int *eof, void *data);
217 static int proc_write_options(struct file *file, const char __user *buffer,
218 unsigned long count, void *data);
221 struct adb_driver via_pmu_driver = {
230 #endif /* CONFIG_ADB */
232 extern void low_sleep_handler(void);
233 extern void enable_kernel_altivec(void);
234 extern void enable_kernel_fp(void);
237 int pmu_polled_request(struct adb_request *req);
238 int pmu_wink(struct adb_request *req);
242 * This table indicates for each PMU opcode:
243 * - the number of data bytes to be sent with the command, or -1
244 * if a length byte should be sent,
245 * - the number of response bytes which the PMU will return, or
246 * -1 if it will send a length byte.
248 static const s8 pmu_data_len[256][2] = {
249 /* 0 1 2 3 4 5 6 7 */
250 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
251 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
252 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
253 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
254 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
255 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
256 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
257 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
258 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
259 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
260 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
261 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
262 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
263 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
264 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
265 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
266 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
267 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
268 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
269 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
270 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
271 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
272 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
273 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
274 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
275 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
276 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
277 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
278 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
279 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
280 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
281 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
284 static char *pbook_type[] = {
286 "PowerBook 2400/3400/3500(G3)",
287 "PowerBook G3 Series",
292 int __init find_via_pmu(void)
299 vias = of_find_node_by_name(NULL, "via-pmu");
303 reg = (u32 *)get_property(vias, "reg", NULL);
305 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
308 taddr = of_translate_address(vias, reg);
309 if (taddr == OF_BAD_ADDR) {
310 printk(KERN_ERR "via-pmu: Can't translate address !\n");
314 spin_lock_init(&pmu_lock);
318 pmu_intr_mask = PMU_INT_PCEJECT |
323 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
324 || device_is_compatible(vias->parent, "ohare")))
325 pmu_kind = PMU_OHARE_BASED;
326 else if (device_is_compatible(vias->parent, "paddington"))
327 pmu_kind = PMU_PADDINGTON_BASED;
328 else if (device_is_compatible(vias->parent, "heathrow"))
329 pmu_kind = PMU_HEATHROW_BASED;
330 else if (device_is_compatible(vias->parent, "Keylargo")
331 || device_is_compatible(vias->parent, "K2-Keylargo")) {
332 struct device_node *gpiop;
333 u64 gaddr = OF_BAD_ADDR;
335 pmu_kind = PMU_KEYLARGO_BASED;
336 pmu_has_adb = (find_type_devices("adb") != NULL);
337 pmu_intr_mask = PMU_INT_PCEJECT |
343 gpiop = of_find_node_by_name(NULL, "gpio");
345 reg = (u32 *)get_property(gpiop, "reg", NULL);
347 gaddr = of_translate_address(gpiop, reg);
348 if (gaddr != OF_BAD_ADDR)
349 gpio_reg = ioremap(gaddr, 0x10);
351 if (gpio_reg == NULL)
352 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
354 pmu_kind = PMU_UNKNOWN;
356 via = ioremap(taddr, 0x2000);
358 printk(KERN_ERR "via-pmu: Can't map address !\n");
362 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
363 out_8(&via[IFR], 0x7f); /* clear IFR */
372 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
373 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
375 sys_ctrler = SYS_CTRLER_PMU;
385 static int pmu_probe(void)
387 return vias == NULL? -ENODEV: 0;
390 static int __init pmu_init(void)
396 #endif /* CONFIG_ADB */
399 * We can't wait until pmu_init gets called, that happens too late.
400 * It happens after IDE and SCSI initialization, which can take a few
401 * seconds, and by that time the PMU could have given up on us and
403 * Thus this is called with arch_initcall rather than device_initcall.
405 static int __init via_pmu_start(void)
410 batt_req.complete = 1;
412 #ifndef CONFIG_PPC_MERGE
413 if (pmu_kind == PMU_KEYLARGO_BASED)
414 openpic_set_irq_priority(vias->intrs[0].line,
415 OPENPIC_PRIORITY_DEFAULT + 1);
418 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
420 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
421 vias->intrs[0].line);
425 if (pmu_kind == PMU_KEYLARGO_BASED) {
426 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
427 if (gpio_node == NULL)
428 gpio_node = of_find_node_by_name(NULL,
430 if (gpio_node && gpio_node->n_intrs > 0)
431 gpio_irq = gpio_node->intrs[0].line;
433 if (gpio_irq != -1) {
434 if (request_irq(gpio_irq, gpio1_interrupt, 0,
435 "GPIO1 ADB", (void *)0))
436 printk(KERN_ERR "pmu: can't get irq %d"
437 " (GPIO1)\n", gpio_irq);
439 gpio_irq_enabled = 1;
443 /* Enable interrupts */
444 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
446 pmu_fully_inited = 1;
448 /* Make sure PMU settle down before continuing. This is _very_ important
449 * since the IDE probe may shut interrupts down for quite a bit of time. If
450 * a PMU communication is pending while this happens, the PMU may timeout
451 * Not that on Core99 machines, the PMU keeps sending us environement
452 * messages, we should find a way to either fix IDE or make it call
453 * pmu_suspend() before masking interrupts. This can also happens while
454 * scolling with some fbdevs.
458 } while (pmu_state != idle);
463 arch_initcall(via_pmu_start);
466 * This has to be done after pci_init, which is a subsys_initcall.
468 static int __init via_pmu_dev_init(void)
473 #ifdef CONFIG_PMAC_BACKLIGHT
474 /* Initialize backlight */
475 pmu_backlight_init(vias);
479 if (machine_is_compatible("AAPL,3400/2400") ||
480 machine_is_compatible("AAPL,3500")) {
481 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
482 NULL, PMAC_MB_INFO_MODEL, 0);
483 pmu_battery_count = 1;
484 if (mb == PMAC_TYPE_COMET)
485 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
487 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
488 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
489 machine_is_compatible("PowerBook1,1")) {
490 pmu_battery_count = 2;
491 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
492 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
494 struct device_node* prim = find_devices("power-mgt");
495 u32 *prim_info = NULL;
497 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
499 /* Other stuffs here yet unknown */
500 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
501 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
502 if (pmu_battery_count > 1)
503 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
506 #endif /* CONFIG_PPC32 */
508 /* Create /proc/pmu */
509 proc_pmu_root = proc_mkdir("pmu", NULL);
513 for (i=0; i<pmu_battery_count; i++) {
515 sprintf(title, "battery_%ld", i);
516 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
517 proc_get_batt, (void *)i);
520 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
521 proc_get_info, NULL);
522 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
523 proc_get_irqstats, NULL);
524 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
525 if (proc_pmu_options) {
526 proc_pmu_options->nlink = 1;
527 proc_pmu_options->read_proc = proc_read_options;
528 proc_pmu_options->write_proc = proc_write_options;
534 device_initcall(via_pmu_dev_init);
540 struct adb_request req;
542 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
543 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
545 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
547 while (!req.complete) {
549 printk(KERN_ERR "init_pmu: no response from PMU\n");
556 /* ack all pending interrupts */
558 interrupt_data[0][0] = 1;
559 while (interrupt_data[0][0] || pmu_state != idle) {
561 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
564 if (pmu_state == idle)
566 via_pmu_interrupt(0, NULL, NULL);
570 /* Tell PMU we are ready. */
571 if (pmu_kind == PMU_KEYLARGO_BASED) {
572 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
573 while (!req.complete)
577 /* Read PMU version */
578 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
579 pmu_wait_complete(&req);
580 if (req.reply_len > 0)
581 pmu_version = req.reply[0];
583 /* Read server mode setting */
584 if (pmu_kind == PMU_KEYLARGO_BASED) {
585 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
586 PMU_PWR_GET_POWERUP_EVENTS);
587 pmu_wait_complete(&req);
588 if (req.reply_len == 2) {
589 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
590 option_server_mode = 1;
591 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
592 option_server_mode ? "enabled" : "disabled");
604 static void pmu_set_server_mode(int server_mode)
606 struct adb_request req;
608 if (pmu_kind != PMU_KEYLARGO_BASED)
611 option_server_mode = server_mode;
612 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
613 pmu_wait_complete(&req);
614 if (req.reply_len < 2)
617 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
618 PMU_PWR_SET_POWERUP_EVENTS,
619 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
621 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
622 PMU_PWR_CLR_POWERUP_EVENTS,
623 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
624 pmu_wait_complete(&req);
627 /* This new version of the code for 2400/3400/3500 powerbooks
628 * is inspired from the implementation in gkrellm-pmu
631 done_battery_state_ohare(struct adb_request* req)
635 * 0x01 : AC indicator
637 * 0x04 : battery exist
640 * 0x20 : full charged
641 * 0x40 : pcharge reset
642 * 0x80 : battery exist
644 * [1][2] : battery voltage
645 * [3] : CPU temperature
646 * [4] : battery temperature
651 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
652 long pcharge, charge, vb, vmax, lmax;
653 long vmax_charging, vmax_charged;
654 long amperage, voltage, time, max;
655 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
656 NULL, PMAC_MB_INFO_MODEL, 0);
658 if (req->reply[0] & 0x01)
659 pmu_power_flags |= PMU_PWR_AC_PRESENT;
661 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
663 if (mb == PMAC_TYPE_COMET) {
674 /* If battery installed */
675 if (req->reply[0] & 0x04) {
676 bat_flags |= PMU_BATT_PRESENT;
677 if (req->reply[0] & 0x02)
678 bat_flags |= PMU_BATT_CHARGING;
679 vb = (req->reply[1] << 8) | req->reply[2];
680 voltage = (vb * 265 + 72665) / 10;
681 amperage = req->reply[5];
682 if ((req->reply[0] & 0x01) == 0) {
684 vb += ((amperage - 200) * 15)/100;
685 } else if (req->reply[0] & 0x02) {
686 vb = (vb * 97) / 100;
687 vmax = vmax_charging;
689 charge = (100 * vb) / vmax;
690 if (req->reply[0] & 0x40) {
691 pcharge = (req->reply[6] << 8) + req->reply[7];
695 pcharge = 100 - pcharge / lmax;
696 if (pcharge < charge)
700 time = (charge * 16440) / amperage;
704 amperage = -amperage;
706 charge = max = amperage = voltage = time = 0;
708 pmu_batteries[pmu_cur_battery].flags = bat_flags;
709 pmu_batteries[pmu_cur_battery].charge = charge;
710 pmu_batteries[pmu_cur_battery].max_charge = max;
711 pmu_batteries[pmu_cur_battery].amperage = amperage;
712 pmu_batteries[pmu_cur_battery].voltage = voltage;
713 pmu_batteries[pmu_cur_battery].time_remaining = time;
715 clear_bit(0, &async_req_locks);
719 done_battery_state_smart(struct adb_request* req)
722 * [0] : format of this structure (known: 3,4,5)
735 * [4][5] : max charge
740 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
742 unsigned int capa, max, voltage;
744 if (req->reply[1] & 0x01)
745 pmu_power_flags |= PMU_PWR_AC_PRESENT;
747 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
750 capa = max = amperage = voltage = 0;
752 if (req->reply[1] & 0x04) {
753 bat_flags |= PMU_BATT_PRESENT;
754 switch(req->reply[0]) {
756 case 4: capa = req->reply[2];
758 amperage = *((signed char *)&req->reply[4]);
759 voltage = req->reply[5];
761 case 5: capa = (req->reply[2] << 8) | req->reply[3];
762 max = (req->reply[4] << 8) | req->reply[5];
763 amperage = *((signed short *)&req->reply[6]);
764 voltage = (req->reply[8] << 8) | req->reply[9];
767 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
768 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
773 if ((req->reply[1] & 0x01) && (amperage > 0))
774 bat_flags |= PMU_BATT_CHARGING;
776 pmu_batteries[pmu_cur_battery].flags = bat_flags;
777 pmu_batteries[pmu_cur_battery].charge = capa;
778 pmu_batteries[pmu_cur_battery].max_charge = max;
779 pmu_batteries[pmu_cur_battery].amperage = amperage;
780 pmu_batteries[pmu_cur_battery].voltage = voltage;
782 if ((req->reply[1] & 0x01) && (amperage > 0))
783 pmu_batteries[pmu_cur_battery].time_remaining
784 = ((max-capa) * 3600) / amperage;
786 pmu_batteries[pmu_cur_battery].time_remaining
787 = (capa * 3600) / (-amperage);
789 pmu_batteries[pmu_cur_battery].time_remaining = 0;
791 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
793 clear_bit(0, &async_req_locks);
797 query_battery_state(void)
799 if (test_and_set_bit(0, &async_req_locks))
801 if (pmu_kind == PMU_OHARE_BASED)
802 pmu_request(&batt_req, done_battery_state_ohare,
803 1, PMU_BATTERY_STATE);
805 pmu_request(&batt_req, done_battery_state_smart,
806 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
810 proc_get_info(char *page, char **start, off_t off,
811 int count, int *eof, void *data)
815 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
816 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
817 p += sprintf(p, "AC Power : %d\n",
818 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
819 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
825 proc_get_irqstats(char *page, char **start, off_t off,
826 int count, int *eof, void *data)
830 static const char *irq_names[] = {
831 "Total CB1 triggered events",
832 "Total GPIO1 triggered events",
833 "PC-Card eject button",
834 "Sound/Brightness button",
836 "Battery state change",
837 "Environment interrupt",
839 "Ghost interrupt (zero len)",
840 "Empty interrupt (empty mask)",
844 for (i=0; i<11; i++) {
845 p += sprintf(p, " %2u: %10u (%s)\n",
846 i, pmu_irq_stats[i], irq_names[i]);
852 proc_get_batt(char *page, char **start, off_t off,
853 int count, int *eof, void *data)
855 long batnum = (long)data;
858 p += sprintf(p, "\n");
859 p += sprintf(p, "flags : %08x\n",
860 pmu_batteries[batnum].flags);
861 p += sprintf(p, "charge : %d\n",
862 pmu_batteries[batnum].charge);
863 p += sprintf(p, "max_charge : %d\n",
864 pmu_batteries[batnum].max_charge);
865 p += sprintf(p, "current : %d\n",
866 pmu_batteries[batnum].amperage);
867 p += sprintf(p, "voltage : %d\n",
868 pmu_batteries[batnum].voltage);
869 p += sprintf(p, "time rem. : %d\n",
870 pmu_batteries[batnum].time_remaining);
876 proc_read_options(char *page, char **start, off_t off,
877 int count, int *eof, void *data)
881 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
882 if (pmu_kind == PMU_KEYLARGO_BASED &&
883 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
884 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
886 if (pmu_kind == PMU_KEYLARGO_BASED)
887 p += sprintf(p, "server_mode=%d\n", option_server_mode);
893 proc_write_options(struct file *file, const char __user *buffer,
894 unsigned long count, void *data)
898 unsigned long fcount = count;
904 if (copy_from_user(tmp, buffer, count))
912 while(*val && (*val != '=')) {
922 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
923 if (pmu_kind == PMU_KEYLARGO_BASED &&
924 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
925 if (!strcmp(label, "lid_wakeup"))
926 option_lid_wakeup = ((*val) == '1');
928 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
930 new_value = ((*val) == '1');
931 if (new_value != option_server_mode)
932 pmu_set_server_mode(new_value);
938 /* Send an ADB command */
940 pmu_send_request(struct adb_request *req, int sync)
944 if ((vias == NULL) || (!pmu_fully_inited)) {
951 switch (req->data[0]) {
953 for (i = 0; i < req->nbytes - 1; ++i)
954 req->data[i] = req->data[i+1];
956 if (pmu_data_len[req->data[0]][1] != 0) {
957 req->reply[0] = ADB_RET_OK;
961 ret = pmu_queue_request(req);
964 switch (req->data[1]) {
966 if (req->nbytes != 2)
968 req->data[0] = PMU_READ_RTC;
971 req->reply[0] = CUDA_PACKET;
973 req->reply[2] = CUDA_GET_TIME;
974 ret = pmu_queue_request(req);
977 if (req->nbytes != 6)
979 req->data[0] = PMU_SET_RTC;
981 for (i = 1; i <= 4; ++i)
982 req->data[i] = req->data[i+1];
984 req->reply[0] = CUDA_PACKET;
986 req->reply[2] = CUDA_SET_TIME;
987 ret = pmu_queue_request(req);
994 for (i = req->nbytes - 1; i > 1; --i)
995 req->data[i+2] = req->data[i];
996 req->data[3] = req->nbytes - 2;
997 req->data[2] = pmu_adb_flags;
998 /*req->data[1] = req->data[1];*/
999 req->data[0] = PMU_ADB_CMD;
1001 req->reply_expected = 1;
1003 ret = pmu_queue_request(req);
1012 while (!req->complete)
1018 /* Enable/disable autopolling */
1020 pmu_adb_autopoll(int devs)
1022 struct adb_request req;
1024 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1029 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1030 adb_dev_map >> 8, adb_dev_map);
1033 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1036 while (!req.complete)
1041 /* Reset the ADB bus */
1043 pmu_adb_reset_bus(void)
1045 struct adb_request req;
1046 int save_autopoll = adb_dev_map;
1048 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1051 /* anyone got a better idea?? */
1052 pmu_adb_autopoll(0);
1056 req.data[0] = PMU_ADB_CMD;
1058 req.data[2] = ADB_BUSRESET;
1062 req.reply_expected = 1;
1063 if (pmu_queue_request(&req) != 0) {
1064 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1067 pmu_wait_complete(&req);
1069 if (save_autopoll != 0)
1070 pmu_adb_autopoll(save_autopoll);
1074 #endif /* CONFIG_ADB */
1076 /* Construct and send a pmu request */
1078 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1087 if (nbytes < 0 || nbytes > 32) {
1088 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1092 req->nbytes = nbytes;
1094 va_start(list, nbytes);
1095 for (i = 0; i < nbytes; ++i)
1096 req->data[i] = va_arg(list, int);
1099 req->reply_expected = 0;
1100 return pmu_queue_request(req);
1104 pmu_queue_request(struct adb_request *req)
1106 unsigned long flags;
1113 if (req->nbytes <= 0) {
1117 nsend = pmu_data_len[req->data[0]][0];
1118 if (nsend >= 0 && req->nbytes != nsend + 1) {
1127 spin_lock_irqsave(&pmu_lock, flags);
1128 if (current_req != 0) {
1129 last_req->next = req;
1134 if (pmu_state == idle)
1137 spin_unlock_irqrestore(&pmu_lock, flags);
1145 /* Sightly increased the delay, I had one occurrence of the message
1149 while ((in_8(&via[B]) & TACK) == 0) {
1150 if (--timeout < 0) {
1151 printk(KERN_ERR "PMU not responding (!ack)\n");
1158 /* New PMU seems to be very sensitive to those timings, so we make sure
1159 * PCI is flushed immediately */
1163 volatile unsigned char __iomem *v = via;
1165 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1167 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1174 volatile unsigned char __iomem *v = via;
1176 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1177 in_8(&v[SR]); /* resets SR */
1178 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1183 pmu_done(struct adb_request *req)
1185 void (*done)(struct adb_request *) = req->done;
1188 /* Here, we assume that if the request has a done member, the
1189 * struct request will survive to setting req->complete to 1
1198 struct adb_request *req;
1200 /* assert pmu_state == idle */
1201 /* get the packet to send */
1203 if (req == 0 || pmu_state != idle
1204 || (/*req->reply_expected && */req_awaiting_reply))
1207 pmu_state = sending;
1209 data_len = pmu_data_len[req->data[0]][0];
1211 /* Sounds safer to make sure ACK is high before writing. This helped
1212 * kill a problem with ADB and some iBooks
1215 /* set the shift register to shift out and send a byte */
1216 send_byte(req->data[0]);
1226 via_pmu_interrupt(0, NULL, NULL);
1236 /* Kicks ADB read when PMU is suspended */
1237 adb_int_pending = 1;
1239 via_pmu_interrupt(0, NULL, NULL);
1240 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1241 || req_awaiting_reply));
1245 pmu_wait_complete(struct adb_request *req)
1249 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1250 via_pmu_interrupt(0, NULL, NULL);
1253 /* This function loops until the PMU is idle and prevents it from
1254 * anwsering to ADB interrupts. pmu_request can still be called.
1255 * This is done to avoid spurrious shutdowns when we know we'll have
1256 * interrupts switched off for a long time
1261 unsigned long flags;
1262 #ifdef SUSPEND_USES_PMU
1263 struct adb_request *req;
1268 spin_lock_irqsave(&pmu_lock, flags);
1270 if (pmu_suspended > 1) {
1271 spin_unlock_irqrestore(&pmu_lock, flags);
1276 spin_unlock_irqrestore(&pmu_lock, flags);
1277 if (req_awaiting_reply)
1278 adb_int_pending = 1;
1279 via_pmu_interrupt(0, NULL, NULL);
1280 spin_lock_irqsave(&pmu_lock, flags);
1281 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1282 #ifdef SUSPEND_USES_PMU
1283 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1284 spin_unlock_irqrestore(&pmu_lock, flags);
1285 while(!req.complete)
1287 #else /* SUSPEND_USES_PMU */
1289 disable_irq_nosync(gpio_irq);
1290 out_8(&via[IER], CB1_INT | IER_CLR);
1291 spin_unlock_irqrestore(&pmu_lock, flags);
1292 #endif /* SUSPEND_USES_PMU */
1301 unsigned long flags;
1303 if (!via || (pmu_suspended < 1))
1306 spin_lock_irqsave(&pmu_lock, flags);
1308 if (pmu_suspended > 0) {
1309 spin_unlock_irqrestore(&pmu_lock, flags);
1312 adb_int_pending = 1;
1313 #ifdef SUSPEND_USES_PMU
1314 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1315 spin_unlock_irqrestore(&pmu_lock, flags);
1316 while(!req.complete)
1318 #else /* SUSPEND_USES_PMU */
1320 enable_irq(gpio_irq);
1321 out_8(&via[IER], CB1_INT | IER_SET);
1322 spin_unlock_irqrestore(&pmu_lock, flags);
1324 #endif /* SUSPEND_USES_PMU */
1327 /* Interrupt data could be the result data from an ADB cmd */
1329 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1331 unsigned char ints, pirq;
1335 if (drop_interrupts || len < 1) {
1336 adb_int_pending = 0;
1341 /* Get PMU interrupt mask */
1344 /* Record zero interrupts for stats */
1348 /* Hack to deal with ADB autopoll flag */
1349 if (ints & PMU_INT_ADB)
1350 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1355 if (i > pmu_irq_stats[10])
1356 pmu_irq_stats[10] = i;
1360 for (pirq = 0; pirq < 8; pirq++)
1361 if (ints & (1 << pirq))
1363 pmu_irq_stats[pirq]++;
1365 ints &= ~(1 << pirq);
1367 /* Note: for some reason, we get an interrupt with len=1,
1368 * data[0]==0 after each normal ADB interrupt, at least
1369 * on the Pismo. Still investigating... --BenH
1371 if ((1 << pirq) & PMU_INT_ADB) {
1372 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1373 struct adb_request *req = req_awaiting_reply;
1375 printk(KERN_ERR "PMU: extra ADB reply\n");
1378 req_awaiting_reply = NULL;
1382 memcpy(req->reply, data + 1, len - 1);
1383 req->reply_len = len - 1;
1387 if (len == 4 && data[1] == 0x2c) {
1388 extern int xmon_wants_key, xmon_adb_keycode;
1389 if (xmon_wants_key) {
1390 xmon_adb_keycode = data[2];
1396 * XXX On the [23]400 the PMU gives us an up
1397 * event for keycodes 0x74 or 0x75 when the PC
1398 * card eject buttons are released, so we
1399 * ignore those events.
1401 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1402 && data[1] == 0x2c && data[3] == 0xff
1403 && (data[2] & ~1) == 0xf4))
1404 adb_input(data+1, len-1, regs, 1);
1405 #endif /* CONFIG_ADB */
1408 /* Sound/brightness button pressed */
1409 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1410 #ifdef CONFIG_PMAC_BACKLIGHT
1412 #ifdef CONFIG_INPUT_ADBHID
1413 if (!disable_kernel_backlight)
1414 #endif /* CONFIG_INPUT_ADBHID */
1415 pmac_backlight_set_legacy_brightness(data[1] >> 4);
1416 #endif /* CONFIG_PMAC_BACKLIGHT */
1418 /* Tick interrupt */
1419 else if ((1 << pirq) & PMU_INT_TICK) {
1420 /* Environement or tick interrupt, query batteries */
1421 if (pmu_battery_count) {
1422 if ((--query_batt_timer) == 0) {
1423 query_battery_state();
1424 query_batt_timer = BATTERY_POLLING_COUNT;
1428 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1429 if (pmu_battery_count)
1430 query_battery_state();
1431 pmu_pass_intr(data, len);
1432 /* len == 6 is probably a bad check. But how do I
1433 * know what PMU versions send what events here? */
1435 via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
1436 via_pmu_event(PMU_EVT_LID, data[1]&1);
1439 pmu_pass_intr(data, len);
1444 static struct adb_request*
1445 pmu_sr_intr(struct pt_regs *regs)
1447 struct adb_request *req;
1450 if (via[B] & TREQ) {
1451 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1452 out_8(&via[IFR], SR_INT);
1455 /* The ack may not yet be low when we get the interrupt */
1456 while ((in_8(&via[B]) & TACK) != 0)
1459 /* if reading grab the byte, and reset the interrupt */
1460 if (pmu_state == reading || pmu_state == reading_intr)
1461 bite = in_8(&via[SR]);
1463 /* reset TREQ and wait for TACK to go high */
1464 out_8(&via[B], in_8(&via[B]) | TREQ);
1467 switch (pmu_state) {
1471 data_len = req->nbytes - 1;
1472 send_byte(data_len);
1475 if (data_index <= data_len) {
1476 send_byte(req->data[data_index++]);
1480 data_len = pmu_data_len[req->data[0]][1];
1481 if (data_len == 0) {
1483 current_req = req->next;
1484 if (req->reply_expected)
1485 req_awaiting_reply = req;
1489 pmu_state = reading;
1491 reply_ptr = req->reply + req->reply_len;
1499 pmu_state = reading_intr;
1500 reply_ptr = interrupt_data[int_data_last];
1502 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1503 enable_irq(gpio_irq);
1504 gpio_irq_enabled = 1;
1510 if (data_len == -1) {
1513 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1514 } else if (data_index < 32) {
1515 reply_ptr[data_index++] = bite;
1517 if (data_index < data_len) {
1522 if (pmu_state == reading_intr) {
1524 int_data_state[int_data_last] = int_data_ready;
1525 interrupt_data_len[int_data_last] = data_len;
1529 * For PMU sleep and freq change requests, we lock the
1530 * PMU until it's explicitely unlocked. This avoids any
1531 * spurrious event polling getting in
1533 current_req = req->next;
1534 req->reply_len += data_index;
1535 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1544 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1551 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1553 unsigned long flags;
1557 struct adb_request *req = NULL;
1560 /* This is a bit brutal, we can probably do better */
1561 spin_lock_irqsave(&pmu_lock, flags);
1565 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1569 if (++nloop > 1000) {
1570 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1571 "intr=%x, ier=%x pmu_state=%d\n",
1572 intr, in_8(&via[IER]), pmu_state);
1575 out_8(&via[IFR], intr);
1576 if (intr & CB1_INT) {
1577 adb_int_pending = 1;
1580 if (intr & SR_INT) {
1581 req = pmu_sr_intr(regs);
1588 if (pmu_state == idle) {
1589 if (adb_int_pending) {
1590 if (int_data_state[0] == int_data_empty)
1592 else if (int_data_state[1] == int_data_empty)
1597 int_data_state[int_data_last] = int_data_fill;
1598 /* Sounds safer to make sure ACK is high before writing.
1599 * This helped kill a problem with ADB and some iBooks
1602 send_byte(PMU_INT_ACK);
1603 adb_int_pending = 0;
1604 } else if (current_req)
1608 /* Mark the oldest buffer for flushing */
1609 if (int_data_state[!int_data_last] == int_data_ready) {
1610 int_data_state[!int_data_last] = int_data_flush;
1611 int_data = !int_data_last;
1612 } else if (int_data_state[int_data_last] == int_data_ready) {
1613 int_data_state[int_data_last] = int_data_flush;
1614 int_data = int_data_last;
1617 spin_unlock_irqrestore(&pmu_lock, flags);
1619 /* Deal with completed PMU requests outside of the lock */
1625 /* Deal with interrupt datas outside of the lock */
1626 if (int_data >= 0) {
1627 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1628 spin_lock_irqsave(&pmu_lock, flags);
1630 int_data_state[int_data] = int_data_empty;
1635 return IRQ_RETVAL(handled);
1641 unsigned long flags;
1643 spin_lock_irqsave(&pmu_lock, flags);
1644 if (pmu_state == locked)
1646 adb_int_pending = 1;
1647 spin_unlock_irqrestore(&pmu_lock, flags);
1652 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1654 unsigned long flags;
1656 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1657 spin_lock_irqsave(&pmu_lock, flags);
1658 if (gpio_irq_enabled > 0) {
1659 disable_irq_nosync(gpio_irq);
1660 gpio_irq_enabled = 0;
1663 adb_int_pending = 1;
1664 spin_unlock_irqrestore(&pmu_lock, flags);
1665 via_pmu_interrupt(0, NULL, NULL);
1672 pmu_enable_irled(int on)
1674 struct adb_request req;
1678 if (pmu_kind == PMU_KEYLARGO_BASED)
1681 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1682 (on ? PMU_POW_ON : PMU_POW_OFF));
1683 pmu_wait_complete(&req);
1689 struct adb_request req;
1694 local_irq_disable();
1696 drop_interrupts = 1;
1698 if (pmu_kind != PMU_KEYLARGO_BASED) {
1699 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1701 while(!req.complete)
1705 pmu_request(&req, NULL, 1, PMU_RESET);
1706 pmu_wait_complete(&req);
1714 struct adb_request req;
1719 local_irq_disable();
1721 drop_interrupts = 1;
1723 if (pmu_kind != PMU_KEYLARGO_BASED) {
1724 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1726 pmu_wait_complete(&req);
1728 /* Disable server mode on shutdown or we'll just
1731 pmu_set_server_mode(0);
1734 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1735 'M', 'A', 'T', 'T');
1736 pmu_wait_complete(&req);
1749 static LIST_HEAD(sleep_notifiers);
1752 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1754 struct list_head *list;
1755 struct pmu_sleep_notifier *notifier;
1757 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1758 list = list->next) {
1759 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1760 if (n->priority > notifier->priority)
1763 __list_add(&n->list, list->prev, list);
1766 EXPORT_SYMBOL(pmu_register_sleep_notifier);
1769 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1771 if (n->list.next == 0)
1774 n->list.next = NULL;
1777 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
1778 #endif /* CONFIG_PM */
1780 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1782 /* Sleep is broadcast last-to-first */
1784 broadcast_sleep(int when, int fallback)
1786 int ret = PBOOK_SLEEP_OK;
1787 struct list_head *list;
1788 struct pmu_sleep_notifier *notifier;
1790 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1791 list = list->prev) {
1792 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1793 ret = notifier->notifier_call(notifier, when);
1794 if (ret != PBOOK_SLEEP_OK) {
1795 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
1796 when, notifier, notifier->notifier_call);
1797 for (; list != &sleep_notifiers; list = list->next) {
1798 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1799 notifier->notifier_call(notifier, fallback);
1807 /* Wake is broadcast first-to-last */
1809 broadcast_wake(void)
1811 int ret = PBOOK_SLEEP_OK;
1812 struct list_head *list;
1813 struct pmu_sleep_notifier *notifier;
1815 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1816 list = list->next) {
1817 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1818 notifier->notifier_call(notifier, PBOOK_WAKE);
1824 * This struct is used to store config register values for
1825 * PCI devices which may get powered off when we sleep.
1827 static struct pci_save {
1828 #ifndef HACKED_PCI_SAVE
1837 static int pbook_npci_saves;
1840 pbook_alloc_pci_save(void)
1843 struct pci_dev *pd = NULL;
1846 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1851 pbook_pci_saves = (struct pci_save *)
1852 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
1853 pbook_npci_saves = npci;
1857 pbook_free_pci_save(void)
1859 if (pbook_pci_saves == NULL)
1861 kfree(pbook_pci_saves);
1862 pbook_pci_saves = NULL;
1863 pbook_npci_saves = 0;
1867 pbook_pci_save(void)
1869 struct pci_save *ps = pbook_pci_saves;
1870 struct pci_dev *pd = NULL;
1871 int npci = pbook_npci_saves;
1876 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1879 #ifndef HACKED_PCI_SAVE
1880 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1881 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1882 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1883 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1887 pci_read_config_dword(pd, i<<4, &ps->config[i]);
1893 /* For this to work, we must take care of a few things: If gmac was enabled
1894 * during boot, it will be in the pci dev list. If it's disabled at this point
1895 * (and it will probably be), then you can't access it's config space.
1898 pbook_pci_restore(void)
1901 struct pci_save *ps = pbook_pci_saves - 1;
1902 struct pci_dev *pd = NULL;
1903 int npci = pbook_npci_saves;
1906 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1907 #ifdef HACKED_PCI_SAVE
1913 pci_write_config_dword(pd, i<<4, ps->config[i]);
1914 pci_write_config_dword(pd, 4, ps->config[1]);
1919 if (ps->command == 0)
1921 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1922 if ((ps->command & ~cmd) == 0)
1924 switch (pd->hdr_type) {
1925 case PCI_HEADER_TYPE_NORMAL:
1926 for (j = 0; j < 6; ++j)
1927 pci_write_config_dword(pd,
1928 PCI_BASE_ADDRESS_0 + j*4,
1929 pd->resource[j].start);
1930 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1932 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1934 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1936 pci_write_config_word(pd, PCI_COMMAND, ps->command);
1944 /* N.B. This doesn't work on the 3400 */
1948 struct adb_request req;
1950 memset(&req, 0, sizeof(req));
1952 for (; n > 0; --n) {
1959 req.reply[0] = ADB_RET_OK;
1961 req.reply_expected = 0;
1962 pmu_polled_request(&req);
1970 req.reply[0] = ADB_RET_OK;
1972 req.reply_expected = 0;
1973 pmu_polled_request(&req);
1981 * Put the powerbook to sleep.
1984 static u32 save_via[8];
1987 save_via_state(void)
1989 save_via[0] = in_8(&via[ANH]);
1990 save_via[1] = in_8(&via[DIRA]);
1991 save_via[2] = in_8(&via[B]);
1992 save_via[3] = in_8(&via[DIRB]);
1993 save_via[4] = in_8(&via[PCR]);
1994 save_via[5] = in_8(&via[ACR]);
1995 save_via[6] = in_8(&via[T1CL]);
1996 save_via[7] = in_8(&via[T1CH]);
1999 restore_via_state(void)
2001 out_8(&via[ANH], save_via[0]);
2002 out_8(&via[DIRA], save_via[1]);
2003 out_8(&via[B], save_via[2]);
2004 out_8(&via[DIRB], save_via[3]);
2005 out_8(&via[PCR], save_via[4]);
2006 out_8(&via[ACR], save_via[5]);
2007 out_8(&via[T1CL], save_via[6]);
2008 out_8(&via[T1CH], save_via[7]);
2009 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
2010 out_8(&via[IFR], 0x7f); /* clear IFR */
2011 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2015 pmac_suspend_devices(void)
2019 pm_prepare_console();
2021 /* Notify old-style device drivers & userland */
2022 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2023 if (ret != PBOOK_SLEEP_OK) {
2024 printk(KERN_ERR "Sleep rejected by drivers\n");
2028 /* Sync the disks. */
2029 /* XXX It would be nice to have some way to ensure that
2030 * nobody is dirtying any new buffers while we wait. That
2031 * could be achieved using the refrigerator for processes
2036 /* Sleep can fail now. May not be very robust but useful for debugging */
2037 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2038 if (ret != PBOOK_SLEEP_OK) {
2039 printk(KERN_ERR "Driver sleep failed\n");
2043 /* Send suspend call to devices, hold the device core's dpm_sem */
2044 ret = device_suspend(PMSG_SUSPEND);
2047 printk(KERN_ERR "Driver sleep failed\n");
2051 /* Call platform functions marked "on sleep" */
2052 pmac_pfunc_i2c_suspend();
2053 pmac_pfunc_base_suspend();
2055 /* Stop preemption */
2058 /* Make sure the decrementer won't interrupt us */
2059 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2060 /* Make sure any pending DEC interrupt occurring while we did
2061 * the above didn't re-enable the DEC */
2063 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2065 /* We can now disable MSR_EE. This code of course works properly only
2066 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2067 * stop the "other" CPUs way before we do all that stuff.
2069 local_irq_disable();
2071 /* Broadcast power down irq
2072 * This isn't that useful in most cases (only directly wired devices can
2073 * use this but still... This will take care of sysdev's as well, so
2074 * we exit from here with local irqs disabled and PIC off.
2076 ret = device_power_down(PMSG_SUSPEND);
2078 wakeup_decrementer();
2083 printk(KERN_ERR "Driver powerdown failed\n");
2087 /* Wait for completion of async requests */
2088 while (!batt_req.complete)
2091 /* Giveup the lazy FPU & vec so we don't have to back them
2092 * up from the low level code
2096 #ifdef CONFIG_ALTIVEC
2097 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2098 enable_kernel_altivec();
2099 #endif /* CONFIG_ALTIVEC */
2105 pmac_wakeup_devices(void)
2109 /* Power back up system devices (including the PIC) */
2112 /* Force a poll of ADB interrupts */
2113 adb_int_pending = 1;
2114 via_pmu_interrupt(0, NULL, NULL);
2116 /* Restart jiffies & scheduling */
2117 wakeup_decrementer();
2119 /* Re-enable local CPU interrupts */
2124 /* Call platform functions marked "on wake" */
2125 pmac_pfunc_base_resume();
2126 pmac_pfunc_i2c_resume();
2128 /* Resume devices */
2131 /* Notify old style drivers */
2134 pm_restore_console();
2139 #define GRACKLE_PM (1<<7)
2140 #define GRACKLE_DOZE (1<<5)
2141 #define GRACKLE_NAP (1<<4)
2142 #define GRACKLE_SLEEP (1<<3)
2144 static int powerbook_sleep_grackle(void)
2146 unsigned long save_l2cr;
2147 unsigned short pmcr1;
2148 struct adb_request req;
2150 struct pci_dev *grackle;
2152 grackle = pci_find_slot(0, 0);
2156 ret = pmac_suspend_devices();
2158 printk(KERN_ERR "Sleep rejected by devices\n");
2162 /* Turn off various things. Darwin does some retry tests here... */
2163 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2164 pmu_wait_complete(&req);
2165 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2166 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2167 pmu_wait_complete(&req);
2169 /* For 750, save backside cache setting and disable it */
2170 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2172 if (!__fake_sleep) {
2173 /* Ask the PMU to put us to sleep */
2174 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2175 pmu_wait_complete(&req);
2178 /* The VIA is supposed not to be restored correctly*/
2180 /* We shut down some HW */
2181 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2183 pci_read_config_word(grackle, 0x70, &pmcr1);
2184 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2185 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2186 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2187 pci_write_config_word(grackle, 0x70, pmcr1);
2189 /* Call low-level ASM sleep handler */
2193 low_sleep_handler();
2195 /* We're awake again, stop grackle PM */
2196 pci_read_config_word(grackle, 0x70, &pmcr1);
2197 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2198 pci_write_config_word(grackle, 0x70, pmcr1);
2200 /* Make sure the PMU is idle */
2201 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2202 restore_via_state();
2204 /* Restore L2 cache */
2205 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2206 _set_L2CR(save_l2cr);
2208 /* Restore userland MMU context */
2209 set_context(current->active_mm->context.id, current->active_mm->pgd);
2211 /* Power things up */
2213 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2214 pmu_wait_complete(&req);
2215 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2216 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2217 pmu_wait_complete(&req);
2218 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2219 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2220 pmu_wait_complete(&req);
2222 pmac_wakeup_devices();
2228 powerbook_sleep_Core99(void)
2230 unsigned long save_l2cr;
2231 unsigned long save_l3cr;
2232 struct adb_request req;
2235 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2236 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2240 if (num_online_cpus() > 1 || cpu_is_offline(0))
2243 ret = pmac_suspend_devices();
2245 printk(KERN_ERR "Sleep rejected by devices\n");
2249 /* Stop environment and ADB interrupts */
2250 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2251 pmu_wait_complete(&req);
2253 /* Tell PMU what events will wake us up */
2254 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2256 pmu_wait_complete(&req);
2257 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2258 0, PMU_PWR_WAKEUP_KEY |
2259 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2260 pmu_wait_complete(&req);
2262 /* Save the state of the L2 and L3 caches */
2263 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2264 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2266 if (!__fake_sleep) {
2267 /* Ask the PMU to put us to sleep */
2268 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2269 pmu_wait_complete(&req);
2272 /* The VIA is supposed not to be restored correctly*/
2275 /* Shut down various ASICs. There's a chance that we can no longer
2276 * talk to the PMU after this, so I moved it to _after_ sending the
2277 * sleep command to it. Still need to be checked.
2279 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2281 /* Call low-level ASM sleep handler */
2285 low_sleep_handler();
2287 /* Restore Apple core ASICs state */
2288 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2291 restore_via_state();
2293 /* tweak LPJ before cpufreq is there */
2294 loops_per_jiffy *= 2;
2297 pmac_call_early_video_resume();
2299 /* Restore L2 cache */
2300 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2301 _set_L2CR(save_l2cr);
2302 /* Restore L3 cache */
2303 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2304 _set_L3CR(save_l3cr);
2306 /* Restore userland MMU context */
2307 set_context(current->active_mm->context.id, current->active_mm->pgd);
2309 /* Tell PMU we are ready */
2311 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2312 pmu_wait_complete(&req);
2313 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2314 pmu_wait_complete(&req);
2316 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2317 loops_per_jiffy /= 2;
2319 pmac_wakeup_devices();
2324 #define PB3400_MEM_CTRL 0xf8000000
2325 #define PB3400_MEM_CTRL_SLEEP 0x70
2328 powerbook_sleep_3400(void)
2333 struct adb_request sleep_req;
2334 void __iomem *mem_ctrl;
2335 unsigned int __iomem *mem_ctrl_sleep;
2337 /* first map in the memory controller registers */
2338 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2339 if (mem_ctrl == NULL) {
2340 printk("powerbook_sleep_3400: ioremap failed\n");
2343 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2345 /* Allocate room for PCI save */
2346 pbook_alloc_pci_save();
2348 ret = pmac_suspend_devices();
2350 pbook_free_pci_save();
2351 printk(KERN_ERR "Sleep rejected by devices\n");
2355 /* Save the state of PCI config space for some slots */
2358 /* Set the memory controller to keep the memory refreshed
2359 while we're asleep */
2360 for (i = 0x403f; i >= 0x4000; --i) {
2361 out_be32(mem_ctrl_sleep, i);
2363 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2369 /* Ask the PMU to put us to sleep */
2370 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2371 while (!sleep_req.complete)
2374 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2376 /* displacement-flush the L2 cache - necessary? */
2377 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2378 i = *(volatile int *)p;
2381 /* Put the CPU into sleep mode */
2382 hid0 = mfspr(SPRN_HID0);
2383 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2384 mtspr(SPRN_HID0, hid0);
2385 mtmsr(mfmsr() | MSR_POW | MSR_EE);
2388 /* OK, we're awake again, start restoring things */
2389 out_be32(mem_ctrl_sleep, 0x3f);
2390 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2391 pbook_pci_restore();
2394 /* wait for the PMU interrupt sequence to complete */
2398 pmac_wakeup_devices();
2399 pbook_free_pci_save();
2405 #endif /* CONFIG_PM && CONFIG_PPC32 */
2408 * Support for /dev/pmu device
2410 #define RB_SIZE 0x10
2411 struct pmu_private {
2412 struct list_head list;
2417 unsigned char data[16];
2419 wait_queue_head_t wait;
2421 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2422 int backlight_locker;
2423 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2426 static LIST_HEAD(all_pmu_pvt);
2427 static DEFINE_SPINLOCK(all_pvt_lock);
2430 pmu_pass_intr(unsigned char *data, int len)
2432 struct pmu_private *pp;
2433 struct list_head *list;
2435 unsigned long flags;
2437 if (len > sizeof(pp->rb_buf[0].data))
2438 len = sizeof(pp->rb_buf[0].data);
2439 spin_lock_irqsave(&all_pvt_lock, flags);
2440 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2441 pp = list_entry(list, struct pmu_private, list);
2442 spin_lock(&pp->lock);
2446 if (i != pp->rb_get) {
2447 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2449 memcpy(rp->data, data, len);
2451 wake_up_interruptible(&pp->wait);
2453 spin_unlock(&pp->lock);
2455 spin_unlock_irqrestore(&all_pvt_lock, flags);
2459 pmu_open(struct inode *inode, struct file *file)
2461 struct pmu_private *pp;
2462 unsigned long flags;
2464 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2467 pp->rb_get = pp->rb_put = 0;
2468 spin_lock_init(&pp->lock);
2469 init_waitqueue_head(&pp->wait);
2470 spin_lock_irqsave(&all_pvt_lock, flags);
2471 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2472 pp->backlight_locker = 0;
2473 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2474 list_add(&pp->list, &all_pmu_pvt);
2475 spin_unlock_irqrestore(&all_pvt_lock, flags);
2476 file->private_data = pp;
2481 pmu_read(struct file *file, char __user *buf,
2482 size_t count, loff_t *ppos)
2484 struct pmu_private *pp = file->private_data;
2485 DECLARE_WAITQUEUE(wait, current);
2486 unsigned long flags;
2489 if (count < 1 || pp == 0)
2491 if (!access_ok(VERIFY_WRITE, buf, count))
2494 spin_lock_irqsave(&pp->lock, flags);
2495 add_wait_queue(&pp->wait, &wait);
2496 current->state = TASK_INTERRUPTIBLE;
2500 if (pp->rb_get != pp->rb_put) {
2502 struct rb_entry *rp = &pp->rb_buf[i];
2504 spin_unlock_irqrestore(&pp->lock, flags);
2507 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2511 spin_lock_irqsave(&pp->lock, flags);
2516 if (file->f_flags & O_NONBLOCK)
2519 if (signal_pending(current))
2521 spin_unlock_irqrestore(&pp->lock, flags);
2523 spin_lock_irqsave(&pp->lock, flags);
2525 current->state = TASK_RUNNING;
2526 remove_wait_queue(&pp->wait, &wait);
2527 spin_unlock_irqrestore(&pp->lock, flags);
2533 pmu_write(struct file *file, const char __user *buf,
2534 size_t count, loff_t *ppos)
2540 pmu_fpoll(struct file *filp, poll_table *wait)
2542 struct pmu_private *pp = filp->private_data;
2543 unsigned int mask = 0;
2544 unsigned long flags;
2548 poll_wait(filp, &pp->wait, wait);
2549 spin_lock_irqsave(&pp->lock, flags);
2550 if (pp->rb_get != pp->rb_put)
2552 spin_unlock_irqrestore(&pp->lock, flags);
2557 pmu_release(struct inode *inode, struct file *file)
2559 struct pmu_private *pp = file->private_data;
2560 unsigned long flags;
2564 file->private_data = NULL;
2565 spin_lock_irqsave(&all_pvt_lock, flags);
2566 list_del(&pp->list);
2567 spin_unlock_irqrestore(&all_pvt_lock, flags);
2568 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2569 if (pp->backlight_locker) {
2570 spin_lock_irqsave(&pmu_lock, flags);
2571 disable_kernel_backlight--;
2572 spin_unlock_irqrestore(&pmu_lock, flags);
2574 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2582 pmu_ioctl(struct inode * inode, struct file *filp,
2583 u_int cmd, u_long arg)
2585 __u32 __user *argp = (__u32 __user *)arg;
2586 int error = -EINVAL;
2589 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2591 if (!capable(CAP_SYS_ADMIN))
2593 if (sleep_in_progress)
2595 sleep_in_progress = 1;
2597 case PMU_OHARE_BASED:
2598 error = powerbook_sleep_3400();
2600 case PMU_HEATHROW_BASED:
2601 case PMU_PADDINGTON_BASED:
2602 error = powerbook_sleep_grackle();
2604 case PMU_KEYLARGO_BASED:
2605 error = powerbook_sleep_Core99();
2610 sleep_in_progress = 0;
2612 case PMU_IOC_CAN_SLEEP:
2613 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2614 return put_user(0, argp);
2616 return put_user(1, argp);
2617 #endif /* CONFIG_PM && CONFIG_PPC32 */
2619 #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2620 /* Compatibility ioctl's for backlight */
2621 case PMU_IOC_GET_BACKLIGHT:
2625 if (sleep_in_progress)
2628 brightness = pmac_backlight_get_legacy_brightness();
2632 return put_user(brightness, argp);
2635 case PMU_IOC_SET_BACKLIGHT:
2639 if (sleep_in_progress)
2642 error = get_user(brightness, argp);
2646 return pmac_backlight_set_legacy_brightness(brightness);
2648 #ifdef CONFIG_INPUT_ADBHID
2649 case PMU_IOC_GRAB_BACKLIGHT: {
2650 struct pmu_private *pp = filp->private_data;
2651 unsigned long flags;
2653 if (pp->backlight_locker)
2655 pp->backlight_locker = 1;
2656 spin_lock_irqsave(&pmu_lock, flags);
2657 disable_kernel_backlight++;
2658 spin_unlock_irqrestore(&pmu_lock, flags);
2661 #endif /* CONFIG_INPUT_ADBHID */
2662 #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
2663 case PMU_IOC_GET_MODEL:
2664 return put_user(pmu_kind, argp);
2665 case PMU_IOC_HAS_ADB:
2666 return put_user(pmu_has_adb, argp);
2671 static struct file_operations pmu_device_fops = {
2677 .release = pmu_release,
2680 static struct miscdevice pmu_device = {
2681 PMU_MINOR, "pmu", &pmu_device_fops
2684 static int pmu_device_init(void)
2688 if (misc_register(&pmu_device) < 0)
2689 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2692 device_initcall(pmu_device_init);
2697 polled_handshake(volatile unsigned char __iomem *via)
2699 via[B] &= ~TREQ; eieio();
2700 while ((via[B] & TACK) != 0)
2702 via[B] |= TREQ; eieio();
2703 while ((via[B] & TACK) == 0)
2708 polled_send_byte(volatile unsigned char __iomem *via, int x)
2710 via[ACR] |= SR_OUT | SR_EXT; eieio();
2711 via[SR] = x; eieio();
2712 polled_handshake(via);
2716 polled_recv_byte(volatile unsigned char __iomem *via)
2720 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2721 x = via[SR]; eieio();
2722 polled_handshake(via);
2723 x = via[SR]; eieio();
2728 pmu_polled_request(struct adb_request *req)
2730 unsigned long flags;
2732 volatile unsigned char __iomem *v = via;
2736 l = pmu_data_len[c][0];
2737 if (l >= 0 && req->nbytes != l + 1)
2740 local_irq_save(flags);
2741 while (pmu_state != idle)
2744 while ((via[B] & TACK) == 0)
2746 polled_send_byte(v, c);
2748 l = req->nbytes - 1;
2749 polled_send_byte(v, l);
2751 for (i = 1; i <= l; ++i)
2752 polled_send_byte(v, req->data[i]);
2754 l = pmu_data_len[c][1];
2756 l = polled_recv_byte(v);
2757 for (i = 0; i < l; ++i)
2758 req->reply[i + req->reply_len] = polled_recv_byte(v);
2763 local_irq_restore(flags);
2766 #endif /* DEBUG_SLEEP */
2769 /* FIXME: This is a temporary set of callbacks to enable us
2770 * to do suspend-to-disk.
2773 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2775 static int pmu_sys_suspended = 0;
2777 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
2779 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
2782 /* Suspend PMU event interrupts */
2785 pmu_sys_suspended = 1;
2789 static int pmu_sys_resume(struct sys_device *sysdev)
2791 struct adb_request req;
2793 if (!pmu_sys_suspended)
2796 /* Tell PMU we are ready */
2797 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2798 pmu_wait_complete(&req);
2800 /* Resume PMU event interrupts */
2803 pmu_sys_suspended = 0;
2808 #endif /* CONFIG_PM && CONFIG_PPC32 */
2810 static struct sysdev_class pmu_sysclass = {
2811 set_kset_name("pmu"),
2814 static struct sys_device device_pmu = {
2816 .cls = &pmu_sysclass,
2819 static struct sysdev_driver driver_pmu = {
2820 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2821 .suspend = &pmu_sys_suspend,
2822 .resume = &pmu_sys_resume,
2823 #endif /* CONFIG_PM && CONFIG_PPC32 */
2826 static int __init init_pmu_sysfs(void)
2830 rc = sysdev_class_register(&pmu_sysclass);
2832 printk(KERN_ERR "Failed registering PMU sys class\n");
2835 rc = sysdev_register(&device_pmu);
2837 printk(KERN_ERR "Failed registering PMU sys device\n");
2840 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2842 printk(KERN_ERR "Failed registering PMU sys driver\n");
2848 subsys_initcall(init_pmu_sysfs);
2850 EXPORT_SYMBOL(pmu_request);
2851 EXPORT_SYMBOL(pmu_queue_request);
2852 EXPORT_SYMBOL(pmu_poll);
2853 EXPORT_SYMBOL(pmu_poll_adb);
2854 EXPORT_SYMBOL(pmu_wait_complete);
2855 EXPORT_SYMBOL(pmu_suspend);
2856 EXPORT_SYMBOL(pmu_resume);
2857 EXPORT_SYMBOL(pmu_unlock);
2858 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2859 EXPORT_SYMBOL(pmu_enable_irled);
2860 EXPORT_SYMBOL(pmu_battery_count);
2861 EXPORT_SYMBOL(pmu_batteries);
2862 EXPORT_SYMBOL(pmu_power_flags);
2863 #endif /* CONFIG_PM && CONFIG_PPC32 */