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 * - Save/Restore PCI space properly
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/miscdevice.h>
29 #include <linux/blkdev.h>
30 #include <linux/pci.h>
31 #include <linux/slab.h>
32 #include <linux/poll.h>
33 #include <linux/adb.h>
34 #include <linux/pmu.h>
35 #include <linux/cuda.h>
36 #include <linux/module.h>
37 #include <linux/spinlock.h>
39 #include <linux/proc_fs.h>
40 #include <linux/init.h>
41 #include <linux/interrupt.h>
42 #include <linux/device.h>
43 #include <linux/sysdev.h>
44 #include <linux/freezer.h>
45 #include <linux/syscalls.h>
46 #include <linux/suspend.h>
47 #include <linux/cpu.h>
49 #include <asm/machdep.h>
51 #include <asm/pgtable.h>
52 #include <asm/system.h>
53 #include <asm/sections.h>
55 #include <asm/pmac_feature.h>
56 #include <asm/pmac_pfunc.h>
57 #include <asm/pmac_low_i2c.h>
58 #include <asm/uaccess.h>
59 #include <asm/mmu_context.h>
60 #include <asm/cputable.h>
62 #include <asm/backlight.h>
64 #include "via-pmu-event.h"
66 /* Some compile options */
69 /* Misc minor number allocated for /dev/pmu */
72 /* How many iterations between battery polls */
73 #define BATTERY_POLLING_COUNT 2
75 static volatile unsigned char __iomem *via;
77 /* VIA registers - spaced 0x200 bytes apart */
78 #define RS 0x200 /* skip between registers */
79 #define B 0 /* B-side data */
80 #define A RS /* A-side data */
81 #define DIRB (2*RS) /* B-side direction (1=output) */
82 #define DIRA (3*RS) /* A-side direction (1=output) */
83 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
84 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
85 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
86 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
87 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
88 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
89 #define SR (10*RS) /* Shift register */
90 #define ACR (11*RS) /* Auxiliary control register */
91 #define PCR (12*RS) /* Peripheral control register */
92 #define IFR (13*RS) /* Interrupt flag register */
93 #define IER (14*RS) /* Interrupt enable register */
94 #define ANH (15*RS) /* A-side data, no handshake */
96 /* Bits in B data register: both active low */
97 #define TACK 0x08 /* Transfer acknowledge (input) */
98 #define TREQ 0x10 /* Transfer request (output) */
101 #define SR_CTRL 0x1c /* Shift register control bits */
102 #define SR_EXT 0x0c /* Shift on external clock */
103 #define SR_OUT 0x10 /* Shift out if 1 */
105 /* Bits in IFR and IER */
106 #define IER_SET 0x80 /* set bits in IER */
107 #define IER_CLR 0 /* clear bits in IER */
108 #define SR_INT 0x04 /* Shift register full/empty */
110 #define CB1_INT 0x10 /* transition on CB1 input */
112 static volatile enum pmu_state {
121 static volatile enum int_data_state {
126 } int_data_state[2] = { int_data_empty, int_data_empty };
128 static struct adb_request *current_req;
129 static struct adb_request *last_req;
130 static struct adb_request *req_awaiting_reply;
131 static unsigned char interrupt_data[2][32];
132 static int interrupt_data_len[2];
133 static int int_data_last;
134 static unsigned char *reply_ptr;
135 static int data_index;
137 static volatile int adb_int_pending;
138 static volatile int disable_poll;
139 static struct device_node *vias;
140 static int pmu_kind = PMU_UNKNOWN;
141 static int pmu_fully_inited;
142 static int pmu_has_adb;
143 static struct device_node *gpio_node;
144 static unsigned char __iomem *gpio_reg;
145 static int gpio_irq = NO_IRQ;
146 static int gpio_irq_enabled = -1;
147 static volatile int pmu_suspended;
148 static spinlock_t pmu_lock;
149 static u8 pmu_intr_mask;
150 static int pmu_version;
151 static int drop_interrupts;
152 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PPC32)
153 static int option_lid_wakeup = 1;
154 #endif /* CONFIG_PM_SLEEP && CONFIG_PPC32 */
155 #if (defined(CONFIG_PM_SLEEP)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT_LEGACY)
156 static int sleep_in_progress;
158 static unsigned long async_req_locks;
159 static unsigned int pmu_irq_stats[11];
161 static struct proc_dir_entry *proc_pmu_root;
162 static struct proc_dir_entry *proc_pmu_info;
163 static struct proc_dir_entry *proc_pmu_irqstats;
164 static struct proc_dir_entry *proc_pmu_options;
165 static int option_server_mode;
167 int pmu_battery_count;
169 unsigned int pmu_power_flags = PMU_PWR_AC_PRESENT;
170 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
171 static int query_batt_timer = BATTERY_POLLING_COUNT;
172 static struct adb_request batt_req;
173 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
177 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
180 static int adb_dev_map;
181 static int pmu_adb_flags;
183 static int pmu_probe(void);
184 static int pmu_init(void);
185 static int pmu_send_request(struct adb_request *req, int sync);
186 static int pmu_adb_autopoll(int devs);
187 static int pmu_adb_reset_bus(void);
188 #endif /* CONFIG_ADB */
190 static int init_pmu(void);
191 static void pmu_start(void);
192 static irqreturn_t via_pmu_interrupt(int irq, void *arg);
193 static irqreturn_t gpio1_interrupt(int irq, void *arg);
194 static int proc_get_info(char *page, char **start, off_t off,
195 int count, int *eof, void *data);
196 static int proc_get_irqstats(char *page, char **start, off_t off,
197 int count, int *eof, void *data);
198 static void pmu_pass_intr(unsigned char *data, int len);
199 static int proc_get_batt(char *page, char **start, off_t off,
200 int count, int *eof, void *data);
201 static int proc_read_options(char *page, char **start, off_t off,
202 int count, int *eof, void *data);
203 static int proc_write_options(struct file *file, const char __user *buffer,
204 unsigned long count, void *data);
207 struct adb_driver via_pmu_driver = {
216 #endif /* CONFIG_ADB */
218 extern void low_sleep_handler(void);
219 extern void enable_kernel_altivec(void);
220 extern void enable_kernel_fp(void);
223 int pmu_polled_request(struct adb_request *req);
224 int pmu_wink(struct adb_request *req);
228 * This table indicates for each PMU opcode:
229 * - the number of data bytes to be sent with the command, or -1
230 * if a length byte should be sent,
231 * - the number of response bytes which the PMU will return, or
232 * -1 if it will send a length byte.
234 static const s8 pmu_data_len[256][2] = {
235 /* 0 1 2 3 4 5 6 7 */
236 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
237 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
238 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
239 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
240 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
241 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
242 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
243 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
244 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
245 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
246 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
247 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
248 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
249 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
250 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
251 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
252 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
253 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
254 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
255 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
256 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
257 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
258 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
259 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
260 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
261 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
262 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
263 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
264 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
265 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
266 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
267 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
270 static char *pbook_type[] = {
272 "PowerBook 2400/3400/3500(G3)",
273 "PowerBook G3 Series",
278 int __init find_via_pmu(void)
285 vias = of_find_node_by_name(NULL, "via-pmu");
289 reg = of_get_property(vias, "reg", NULL);
291 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
294 taddr = of_translate_address(vias, reg);
295 if (taddr == OF_BAD_ADDR) {
296 printk(KERN_ERR "via-pmu: Can't translate address !\n");
300 spin_lock_init(&pmu_lock);
304 pmu_intr_mask = PMU_INT_PCEJECT |
309 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
310 || of_device_is_compatible(vias->parent, "ohare")))
311 pmu_kind = PMU_OHARE_BASED;
312 else if (of_device_is_compatible(vias->parent, "paddington"))
313 pmu_kind = PMU_PADDINGTON_BASED;
314 else if (of_device_is_compatible(vias->parent, "heathrow"))
315 pmu_kind = PMU_HEATHROW_BASED;
316 else if (of_device_is_compatible(vias->parent, "Keylargo")
317 || of_device_is_compatible(vias->parent, "K2-Keylargo")) {
318 struct device_node *gpiop;
319 struct device_node *adbp;
320 u64 gaddr = OF_BAD_ADDR;
322 pmu_kind = PMU_KEYLARGO_BASED;
323 adbp = of_find_node_by_type(NULL, "adb");
324 pmu_has_adb = (adbp != NULL);
326 pmu_intr_mask = PMU_INT_PCEJECT |
332 gpiop = of_find_node_by_name(NULL, "gpio");
334 reg = of_get_property(gpiop, "reg", NULL);
336 gaddr = of_translate_address(gpiop, reg);
337 if (gaddr != OF_BAD_ADDR)
338 gpio_reg = ioremap(gaddr, 0x10);
340 if (gpio_reg == NULL) {
341 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
345 pmu_kind = PMU_UNKNOWN;
347 via = ioremap(taddr, 0x2000);
349 printk(KERN_ERR "via-pmu: Can't map address !\n");
353 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
354 out_8(&via[IFR], 0x7f); /* clear IFR */
363 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
364 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
366 sys_ctrler = SYS_CTRLER_PMU;
379 static int pmu_probe(void)
381 return vias == NULL? -ENODEV: 0;
384 static int __init pmu_init(void)
390 #endif /* CONFIG_ADB */
393 * We can't wait until pmu_init gets called, that happens too late.
394 * It happens after IDE and SCSI initialization, which can take a few
395 * seconds, and by that time the PMU could have given up on us and
397 * Thus this is called with arch_initcall rather than device_initcall.
399 static int __init via_pmu_start(void)
406 batt_req.complete = 1;
408 irq = irq_of_parse_and_map(vias, 0);
410 printk(KERN_ERR "via-pmu: can't map interrupt\n");
413 if (request_irq(irq, via_pmu_interrupt, 0, "VIA-PMU", (void *)0)) {
414 printk(KERN_ERR "via-pmu: can't request irq %d\n", irq);
418 if (pmu_kind == PMU_KEYLARGO_BASED) {
419 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
420 if (gpio_node == NULL)
421 gpio_node = of_find_node_by_name(NULL,
424 gpio_irq = irq_of_parse_and_map(gpio_node, 0);
426 if (gpio_irq != NO_IRQ) {
427 if (request_irq(gpio_irq, gpio1_interrupt, 0,
428 "GPIO1 ADB", (void *)0))
429 printk(KERN_ERR "pmu: can't get irq %d"
430 " (GPIO1)\n", gpio_irq);
432 gpio_irq_enabled = 1;
436 /* Enable interrupts */
437 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
439 pmu_fully_inited = 1;
441 /* Make sure PMU settle down before continuing. This is _very_ important
442 * since the IDE probe may shut interrupts down for quite a bit of time. If
443 * a PMU communication is pending while this happens, the PMU may timeout
444 * Not that on Core99 machines, the PMU keeps sending us environement
445 * messages, we should find a way to either fix IDE or make it call
446 * pmu_suspend() before masking interrupts. This can also happens while
447 * scolling with some fbdevs.
451 } while (pmu_state != idle);
456 arch_initcall(via_pmu_start);
459 * This has to be done after pci_init, which is a subsys_initcall.
461 static int __init via_pmu_dev_init(void)
466 #ifdef CONFIG_PMAC_BACKLIGHT
467 /* Initialize backlight */
468 pmu_backlight_init();
472 if (machine_is_compatible("AAPL,3400/2400") ||
473 machine_is_compatible("AAPL,3500")) {
474 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
475 NULL, PMAC_MB_INFO_MODEL, 0);
476 pmu_battery_count = 1;
477 if (mb == PMAC_TYPE_COMET)
478 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
480 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
481 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
482 machine_is_compatible("PowerBook1,1")) {
483 pmu_battery_count = 2;
484 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
485 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
487 struct device_node* prim =
488 of_find_node_by_name(NULL, "power-mgt");
489 const u32 *prim_info = NULL;
491 prim_info = of_get_property(prim, "prim-info", NULL);
493 /* Other stuffs here yet unknown */
494 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
495 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
496 if (pmu_battery_count > 1)
497 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
501 #endif /* CONFIG_PPC32 */
503 /* Create /proc/pmu */
504 proc_pmu_root = proc_mkdir("pmu", NULL);
508 for (i=0; i<pmu_battery_count; i++) {
510 sprintf(title, "battery_%ld", i);
511 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
512 proc_get_batt, (void *)i);
515 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
516 proc_get_info, NULL);
517 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
518 proc_get_irqstats, NULL);
519 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
520 if (proc_pmu_options) {
521 proc_pmu_options->read_proc = proc_read_options;
522 proc_pmu_options->write_proc = proc_write_options;
528 device_initcall(via_pmu_dev_init);
534 struct adb_request req;
536 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
537 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
539 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
541 while (!req.complete) {
543 printk(KERN_ERR "init_pmu: no response from PMU\n");
550 /* ack all pending interrupts */
552 interrupt_data[0][0] = 1;
553 while (interrupt_data[0][0] || pmu_state != idle) {
555 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
558 if (pmu_state == idle)
560 via_pmu_interrupt(0, NULL);
564 /* Tell PMU we are ready. */
565 if (pmu_kind == PMU_KEYLARGO_BASED) {
566 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
567 while (!req.complete)
571 /* Read PMU version */
572 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
573 pmu_wait_complete(&req);
574 if (req.reply_len > 0)
575 pmu_version = req.reply[0];
577 /* Read server mode setting */
578 if (pmu_kind == PMU_KEYLARGO_BASED) {
579 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
580 PMU_PWR_GET_POWERUP_EVENTS);
581 pmu_wait_complete(&req);
582 if (req.reply_len == 2) {
583 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
584 option_server_mode = 1;
585 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
586 option_server_mode ? "enabled" : "disabled");
598 static void pmu_set_server_mode(int server_mode)
600 struct adb_request req;
602 if (pmu_kind != PMU_KEYLARGO_BASED)
605 option_server_mode = server_mode;
606 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
607 pmu_wait_complete(&req);
608 if (req.reply_len < 2)
611 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
612 PMU_PWR_SET_POWERUP_EVENTS,
613 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
615 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
616 PMU_PWR_CLR_POWERUP_EVENTS,
617 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
618 pmu_wait_complete(&req);
621 /* This new version of the code for 2400/3400/3500 powerbooks
622 * is inspired from the implementation in gkrellm-pmu
625 done_battery_state_ohare(struct adb_request* req)
629 * 0x01 : AC indicator
631 * 0x04 : battery exist
634 * 0x20 : full charged
635 * 0x40 : pcharge reset
636 * 0x80 : battery exist
638 * [1][2] : battery voltage
639 * [3] : CPU temperature
640 * [4] : battery temperature
645 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
646 long pcharge, charge, vb, vmax, lmax;
647 long vmax_charging, vmax_charged;
648 long amperage, voltage, time, max;
649 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
650 NULL, PMAC_MB_INFO_MODEL, 0);
652 if (req->reply[0] & 0x01)
653 pmu_power_flags |= PMU_PWR_AC_PRESENT;
655 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
657 if (mb == PMAC_TYPE_COMET) {
668 /* If battery installed */
669 if (req->reply[0] & 0x04) {
670 bat_flags |= PMU_BATT_PRESENT;
671 if (req->reply[0] & 0x02)
672 bat_flags |= PMU_BATT_CHARGING;
673 vb = (req->reply[1] << 8) | req->reply[2];
674 voltage = (vb * 265 + 72665) / 10;
675 amperage = req->reply[5];
676 if ((req->reply[0] & 0x01) == 0) {
678 vb += ((amperage - 200) * 15)/100;
679 } else if (req->reply[0] & 0x02) {
680 vb = (vb * 97) / 100;
681 vmax = vmax_charging;
683 charge = (100 * vb) / vmax;
684 if (req->reply[0] & 0x40) {
685 pcharge = (req->reply[6] << 8) + req->reply[7];
689 pcharge = 100 - pcharge / lmax;
690 if (pcharge < charge)
694 time = (charge * 16440) / amperage;
698 amperage = -amperage;
700 charge = max = amperage = voltage = time = 0;
702 pmu_batteries[pmu_cur_battery].flags = bat_flags;
703 pmu_batteries[pmu_cur_battery].charge = charge;
704 pmu_batteries[pmu_cur_battery].max_charge = max;
705 pmu_batteries[pmu_cur_battery].amperage = amperage;
706 pmu_batteries[pmu_cur_battery].voltage = voltage;
707 pmu_batteries[pmu_cur_battery].time_remaining = time;
709 clear_bit(0, &async_req_locks);
713 done_battery_state_smart(struct adb_request* req)
716 * [0] : format of this structure (known: 3,4,5)
729 * [4][5] : max charge
734 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
736 unsigned int capa, max, voltage;
738 if (req->reply[1] & 0x01)
739 pmu_power_flags |= PMU_PWR_AC_PRESENT;
741 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
744 capa = max = amperage = voltage = 0;
746 if (req->reply[1] & 0x04) {
747 bat_flags |= PMU_BATT_PRESENT;
748 switch(req->reply[0]) {
750 case 4: capa = req->reply[2];
752 amperage = *((signed char *)&req->reply[4]);
753 voltage = req->reply[5];
755 case 5: capa = (req->reply[2] << 8) | req->reply[3];
756 max = (req->reply[4] << 8) | req->reply[5];
757 amperage = *((signed short *)&req->reply[6]);
758 voltage = (req->reply[8] << 8) | req->reply[9];
761 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
762 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
767 if ((req->reply[1] & 0x01) && (amperage > 0))
768 bat_flags |= PMU_BATT_CHARGING;
770 pmu_batteries[pmu_cur_battery].flags = bat_flags;
771 pmu_batteries[pmu_cur_battery].charge = capa;
772 pmu_batteries[pmu_cur_battery].max_charge = max;
773 pmu_batteries[pmu_cur_battery].amperage = amperage;
774 pmu_batteries[pmu_cur_battery].voltage = voltage;
776 if ((req->reply[1] & 0x01) && (amperage > 0))
777 pmu_batteries[pmu_cur_battery].time_remaining
778 = ((max-capa) * 3600) / amperage;
780 pmu_batteries[pmu_cur_battery].time_remaining
781 = (capa * 3600) / (-amperage);
783 pmu_batteries[pmu_cur_battery].time_remaining = 0;
785 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
787 clear_bit(0, &async_req_locks);
791 query_battery_state(void)
793 if (test_and_set_bit(0, &async_req_locks))
795 if (pmu_kind == PMU_OHARE_BASED)
796 pmu_request(&batt_req, done_battery_state_ohare,
797 1, PMU_BATTERY_STATE);
799 pmu_request(&batt_req, done_battery_state_smart,
800 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
804 proc_get_info(char *page, char **start, off_t off,
805 int count, int *eof, void *data)
809 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
810 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
811 p += sprintf(p, "AC Power : %d\n",
812 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
813 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
819 proc_get_irqstats(char *page, char **start, off_t off,
820 int count, int *eof, void *data)
824 static const char *irq_names[] = {
825 "Total CB1 triggered events",
826 "Total GPIO1 triggered events",
827 "PC-Card eject button",
828 "Sound/Brightness button",
830 "Battery state change",
831 "Environment interrupt",
833 "Ghost interrupt (zero len)",
834 "Empty interrupt (empty mask)",
838 for (i=0; i<11; i++) {
839 p += sprintf(p, " %2u: %10u (%s)\n",
840 i, pmu_irq_stats[i], irq_names[i]);
846 proc_get_batt(char *page, char **start, off_t off,
847 int count, int *eof, void *data)
849 long batnum = (long)data;
852 p += sprintf(p, "\n");
853 p += sprintf(p, "flags : %08x\n",
854 pmu_batteries[batnum].flags);
855 p += sprintf(p, "charge : %d\n",
856 pmu_batteries[batnum].charge);
857 p += sprintf(p, "max_charge : %d\n",
858 pmu_batteries[batnum].max_charge);
859 p += sprintf(p, "current : %d\n",
860 pmu_batteries[batnum].amperage);
861 p += sprintf(p, "voltage : %d\n",
862 pmu_batteries[batnum].voltage);
863 p += sprintf(p, "time rem. : %d\n",
864 pmu_batteries[batnum].time_remaining);
870 proc_read_options(char *page, char **start, off_t off,
871 int count, int *eof, void *data)
875 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PPC32)
876 if (pmu_kind == PMU_KEYLARGO_BASED &&
877 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
878 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
880 if (pmu_kind == PMU_KEYLARGO_BASED)
881 p += sprintf(p, "server_mode=%d\n", option_server_mode);
887 proc_write_options(struct file *file, const char __user *buffer,
888 unsigned long count, void *data)
892 unsigned long fcount = count;
898 if (copy_from_user(tmp, buffer, count))
906 while(*val && (*val != '=')) {
916 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PPC32)
917 if (pmu_kind == PMU_KEYLARGO_BASED &&
918 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
919 if (!strcmp(label, "lid_wakeup"))
920 option_lid_wakeup = ((*val) == '1');
922 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
924 new_value = ((*val) == '1');
925 if (new_value != option_server_mode)
926 pmu_set_server_mode(new_value);
932 /* Send an ADB command */
934 pmu_send_request(struct adb_request *req, int sync)
938 if ((vias == NULL) || (!pmu_fully_inited)) {
945 switch (req->data[0]) {
947 for (i = 0; i < req->nbytes - 1; ++i)
948 req->data[i] = req->data[i+1];
950 if (pmu_data_len[req->data[0]][1] != 0) {
951 req->reply[0] = ADB_RET_OK;
955 ret = pmu_queue_request(req);
958 switch (req->data[1]) {
960 if (req->nbytes != 2)
962 req->data[0] = PMU_READ_RTC;
965 req->reply[0] = CUDA_PACKET;
967 req->reply[2] = CUDA_GET_TIME;
968 ret = pmu_queue_request(req);
971 if (req->nbytes != 6)
973 req->data[0] = PMU_SET_RTC;
975 for (i = 1; i <= 4; ++i)
976 req->data[i] = req->data[i+1];
978 req->reply[0] = CUDA_PACKET;
980 req->reply[2] = CUDA_SET_TIME;
981 ret = pmu_queue_request(req);
988 for (i = req->nbytes - 1; i > 1; --i)
989 req->data[i+2] = req->data[i];
990 req->data[3] = req->nbytes - 2;
991 req->data[2] = pmu_adb_flags;
992 /*req->data[1] = req->data[1];*/
993 req->data[0] = PMU_ADB_CMD;
995 req->reply_expected = 1;
997 ret = pmu_queue_request(req);
1006 while (!req->complete)
1012 /* Enable/disable autopolling */
1014 pmu_adb_autopoll(int devs)
1016 struct adb_request req;
1018 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1023 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1024 adb_dev_map >> 8, adb_dev_map);
1027 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1030 while (!req.complete)
1035 /* Reset the ADB bus */
1037 pmu_adb_reset_bus(void)
1039 struct adb_request req;
1040 int save_autopoll = adb_dev_map;
1042 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1045 /* anyone got a better idea?? */
1046 pmu_adb_autopoll(0);
1050 req.data[0] = PMU_ADB_CMD;
1052 req.data[2] = ADB_BUSRESET;
1056 req.reply_expected = 1;
1057 if (pmu_queue_request(&req) != 0) {
1058 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1061 pmu_wait_complete(&req);
1063 if (save_autopoll != 0)
1064 pmu_adb_autopoll(save_autopoll);
1068 #endif /* CONFIG_ADB */
1070 /* Construct and send a pmu request */
1072 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1081 if (nbytes < 0 || nbytes > 32) {
1082 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1086 req->nbytes = nbytes;
1088 va_start(list, nbytes);
1089 for (i = 0; i < nbytes; ++i)
1090 req->data[i] = va_arg(list, int);
1093 req->reply_expected = 0;
1094 return pmu_queue_request(req);
1098 pmu_queue_request(struct adb_request *req)
1100 unsigned long flags;
1107 if (req->nbytes <= 0) {
1111 nsend = pmu_data_len[req->data[0]][0];
1112 if (nsend >= 0 && req->nbytes != nsend + 1) {
1121 spin_lock_irqsave(&pmu_lock, flags);
1122 if (current_req != 0) {
1123 last_req->next = req;
1128 if (pmu_state == idle)
1131 spin_unlock_irqrestore(&pmu_lock, flags);
1139 /* Sightly increased the delay, I had one occurrence of the message
1143 while ((in_8(&via[B]) & TACK) == 0) {
1144 if (--timeout < 0) {
1145 printk(KERN_ERR "PMU not responding (!ack)\n");
1152 /* New PMU seems to be very sensitive to those timings, so we make sure
1153 * PCI is flushed immediately */
1157 volatile unsigned char __iomem *v = via;
1159 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1161 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1168 volatile unsigned char __iomem *v = via;
1170 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1171 in_8(&v[SR]); /* resets SR */
1172 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1177 pmu_done(struct adb_request *req)
1179 void (*done)(struct adb_request *) = req->done;
1182 /* Here, we assume that if the request has a done member, the
1183 * struct request will survive to setting req->complete to 1
1192 struct adb_request *req;
1194 /* assert pmu_state == idle */
1195 /* get the packet to send */
1197 if (req == 0 || pmu_state != idle
1198 || (/*req->reply_expected && */req_awaiting_reply))
1201 pmu_state = sending;
1203 data_len = pmu_data_len[req->data[0]][0];
1205 /* Sounds safer to make sure ACK is high before writing. This helped
1206 * kill a problem with ADB and some iBooks
1209 /* set the shift register to shift out and send a byte */
1210 send_byte(req->data[0]);
1220 via_pmu_interrupt(0, NULL);
1230 /* Kicks ADB read when PMU is suspended */
1231 adb_int_pending = 1;
1233 via_pmu_interrupt(0, NULL);
1234 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1235 || req_awaiting_reply));
1239 pmu_wait_complete(struct adb_request *req)
1243 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1244 via_pmu_interrupt(0, NULL);
1247 /* This function loops until the PMU is idle and prevents it from
1248 * anwsering to ADB interrupts. pmu_request can still be called.
1249 * This is done to avoid spurrious shutdowns when we know we'll have
1250 * interrupts switched off for a long time
1255 unsigned long flags;
1260 spin_lock_irqsave(&pmu_lock, flags);
1262 if (pmu_suspended > 1) {
1263 spin_unlock_irqrestore(&pmu_lock, flags);
1268 spin_unlock_irqrestore(&pmu_lock, flags);
1269 if (req_awaiting_reply)
1270 adb_int_pending = 1;
1271 via_pmu_interrupt(0, NULL);
1272 spin_lock_irqsave(&pmu_lock, flags);
1273 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1275 disable_irq_nosync(gpio_irq);
1276 out_8(&via[IER], CB1_INT | IER_CLR);
1277 spin_unlock_irqrestore(&pmu_lock, flags);
1286 unsigned long flags;
1288 if (!via || (pmu_suspended < 1))
1291 spin_lock_irqsave(&pmu_lock, flags);
1293 if (pmu_suspended > 0) {
1294 spin_unlock_irqrestore(&pmu_lock, flags);
1297 adb_int_pending = 1;
1299 enable_irq(gpio_irq);
1300 out_8(&via[IER], CB1_INT | IER_SET);
1301 spin_unlock_irqrestore(&pmu_lock, flags);
1305 /* Interrupt data could be the result data from an ADB cmd */
1307 pmu_handle_data(unsigned char *data, int len)
1309 unsigned char ints, pirq;
1313 if (drop_interrupts || len < 1) {
1314 adb_int_pending = 0;
1319 /* Get PMU interrupt mask */
1322 /* Record zero interrupts for stats */
1326 /* Hack to deal with ADB autopoll flag */
1327 if (ints & PMU_INT_ADB)
1328 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1333 if (i > pmu_irq_stats[10])
1334 pmu_irq_stats[10] = i;
1338 for (pirq = 0; pirq < 8; pirq++)
1339 if (ints & (1 << pirq))
1341 pmu_irq_stats[pirq]++;
1343 ints &= ~(1 << pirq);
1345 /* Note: for some reason, we get an interrupt with len=1,
1346 * data[0]==0 after each normal ADB interrupt, at least
1347 * on the Pismo. Still investigating... --BenH
1349 if ((1 << pirq) & PMU_INT_ADB) {
1350 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1351 struct adb_request *req = req_awaiting_reply;
1353 printk(KERN_ERR "PMU: extra ADB reply\n");
1356 req_awaiting_reply = NULL;
1360 memcpy(req->reply, data + 1, len - 1);
1361 req->reply_len = len - 1;
1365 if (len == 4 && data[1] == 0x2c) {
1366 extern int xmon_wants_key, xmon_adb_keycode;
1367 if (xmon_wants_key) {
1368 xmon_adb_keycode = data[2];
1374 * XXX On the [23]400 the PMU gives us an up
1375 * event for keycodes 0x74 or 0x75 when the PC
1376 * card eject buttons are released, so we
1377 * ignore those events.
1379 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1380 && data[1] == 0x2c && data[3] == 0xff
1381 && (data[2] & ~1) == 0xf4))
1382 adb_input(data+1, len-1, 1);
1383 #endif /* CONFIG_ADB */
1386 /* Sound/brightness button pressed */
1387 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1388 #ifdef CONFIG_PMAC_BACKLIGHT
1390 pmac_backlight_set_legacy_brightness_pmu(data[1] >> 4);
1393 /* Tick interrupt */
1394 else if ((1 << pirq) & PMU_INT_TICK) {
1395 /* Environement or tick interrupt, query batteries */
1396 if (pmu_battery_count) {
1397 if ((--query_batt_timer) == 0) {
1398 query_battery_state();
1399 query_batt_timer = BATTERY_POLLING_COUNT;
1403 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1404 if (pmu_battery_count)
1405 query_battery_state();
1406 pmu_pass_intr(data, len);
1407 /* len == 6 is probably a bad check. But how do I
1408 * know what PMU versions send what events here? */
1410 via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
1411 via_pmu_event(PMU_EVT_LID, data[1]&1);
1414 pmu_pass_intr(data, len);
1419 static struct adb_request*
1422 struct adb_request *req;
1425 if (via[B] & TREQ) {
1426 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1427 out_8(&via[IFR], SR_INT);
1430 /* The ack may not yet be low when we get the interrupt */
1431 while ((in_8(&via[B]) & TACK) != 0)
1434 /* if reading grab the byte, and reset the interrupt */
1435 if (pmu_state == reading || pmu_state == reading_intr)
1436 bite = in_8(&via[SR]);
1438 /* reset TREQ and wait for TACK to go high */
1439 out_8(&via[B], in_8(&via[B]) | TREQ);
1442 switch (pmu_state) {
1446 data_len = req->nbytes - 1;
1447 send_byte(data_len);
1450 if (data_index <= data_len) {
1451 send_byte(req->data[data_index++]);
1455 data_len = pmu_data_len[req->data[0]][1];
1456 if (data_len == 0) {
1458 current_req = req->next;
1459 if (req->reply_expected)
1460 req_awaiting_reply = req;
1464 pmu_state = reading;
1466 reply_ptr = req->reply + req->reply_len;
1474 pmu_state = reading_intr;
1475 reply_ptr = interrupt_data[int_data_last];
1477 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1478 enable_irq(gpio_irq);
1479 gpio_irq_enabled = 1;
1485 if (data_len == -1) {
1488 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1489 } else if (data_index < 32) {
1490 reply_ptr[data_index++] = bite;
1492 if (data_index < data_len) {
1497 if (pmu_state == reading_intr) {
1499 int_data_state[int_data_last] = int_data_ready;
1500 interrupt_data_len[int_data_last] = data_len;
1504 * For PMU sleep and freq change requests, we lock the
1505 * PMU until it's explicitly unlocked. This avoids any
1506 * spurrious event polling getting in
1508 current_req = req->next;
1509 req->reply_len += data_index;
1510 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1519 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1526 via_pmu_interrupt(int irq, void *arg)
1528 unsigned long flags;
1532 struct adb_request *req = NULL;
1535 /* This is a bit brutal, we can probably do better */
1536 spin_lock_irqsave(&pmu_lock, flags);
1540 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1544 if (++nloop > 1000) {
1545 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1546 "intr=%x, ier=%x pmu_state=%d\n",
1547 intr, in_8(&via[IER]), pmu_state);
1550 out_8(&via[IFR], intr);
1551 if (intr & CB1_INT) {
1552 adb_int_pending = 1;
1555 if (intr & SR_INT) {
1556 req = pmu_sr_intr();
1563 if (pmu_state == idle) {
1564 if (adb_int_pending) {
1565 if (int_data_state[0] == int_data_empty)
1567 else if (int_data_state[1] == int_data_empty)
1572 int_data_state[int_data_last] = int_data_fill;
1573 /* Sounds safer to make sure ACK is high before writing.
1574 * This helped kill a problem with ADB and some iBooks
1577 send_byte(PMU_INT_ACK);
1578 adb_int_pending = 0;
1579 } else if (current_req)
1583 /* Mark the oldest buffer for flushing */
1584 if (int_data_state[!int_data_last] == int_data_ready) {
1585 int_data_state[!int_data_last] = int_data_flush;
1586 int_data = !int_data_last;
1587 } else if (int_data_state[int_data_last] == int_data_ready) {
1588 int_data_state[int_data_last] = int_data_flush;
1589 int_data = int_data_last;
1592 spin_unlock_irqrestore(&pmu_lock, flags);
1594 /* Deal with completed PMU requests outside of the lock */
1600 /* Deal with interrupt datas outside of the lock */
1601 if (int_data >= 0) {
1602 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data]);
1603 spin_lock_irqsave(&pmu_lock, flags);
1605 int_data_state[int_data] = int_data_empty;
1610 return IRQ_RETVAL(handled);
1616 unsigned long flags;
1618 spin_lock_irqsave(&pmu_lock, flags);
1619 if (pmu_state == locked)
1621 adb_int_pending = 1;
1622 spin_unlock_irqrestore(&pmu_lock, flags);
1627 gpio1_interrupt(int irq, void *arg)
1629 unsigned long flags;
1631 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1632 spin_lock_irqsave(&pmu_lock, flags);
1633 if (gpio_irq_enabled > 0) {
1634 disable_irq_nosync(gpio_irq);
1635 gpio_irq_enabled = 0;
1638 adb_int_pending = 1;
1639 spin_unlock_irqrestore(&pmu_lock, flags);
1640 via_pmu_interrupt(0, NULL);
1647 pmu_enable_irled(int on)
1649 struct adb_request req;
1653 if (pmu_kind == PMU_KEYLARGO_BASED)
1656 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1657 (on ? PMU_POW_ON : PMU_POW_OFF));
1658 pmu_wait_complete(&req);
1664 struct adb_request req;
1669 local_irq_disable();
1671 drop_interrupts = 1;
1673 if (pmu_kind != PMU_KEYLARGO_BASED) {
1674 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1676 while(!req.complete)
1680 pmu_request(&req, NULL, 1, PMU_RESET);
1681 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 pmu_wait_complete(&req);
1703 /* Disable server mode on shutdown or we'll just
1706 pmu_set_server_mode(0);
1709 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1710 'M', 'A', 'T', 'T');
1711 pmu_wait_complete(&req);
1722 #ifdef CONFIG_PM_SLEEP
1724 static LIST_HEAD(sleep_notifiers);
1727 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1729 struct list_head *list;
1730 struct pmu_sleep_notifier *notifier;
1732 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1733 list = list->next) {
1734 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1735 if (n->priority > notifier->priority)
1738 __list_add(&n->list, list->prev, list);
1741 EXPORT_SYMBOL(pmu_register_sleep_notifier);
1744 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1746 if (n->list.next == 0)
1749 n->list.next = NULL;
1752 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
1753 #endif /* CONFIG_PM_SLEEP */
1755 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PPC32)
1757 /* Sleep is broadcast last-to-first */
1758 static void broadcast_sleep(int when)
1760 struct list_head *list;
1761 struct pmu_sleep_notifier *notifier;
1763 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1764 list = list->prev) {
1765 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1766 notifier->notifier_call(notifier, when);
1770 /* Wake is broadcast first-to-last */
1771 static void broadcast_wake(void)
1773 struct list_head *list;
1774 struct pmu_sleep_notifier *notifier;
1776 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1777 list = list->next) {
1778 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1779 notifier->notifier_call(notifier, PBOOK_WAKE);
1784 * This struct is used to store config register values for
1785 * PCI devices which may get powered off when we sleep.
1787 static struct pci_save {
1793 static int pbook_npci_saves;
1796 pbook_alloc_pci_save(void)
1799 struct pci_dev *pd = NULL;
1802 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1807 pbook_pci_saves = (struct pci_save *)
1808 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
1809 pbook_npci_saves = npci;
1813 pbook_free_pci_save(void)
1815 if (pbook_pci_saves == NULL)
1817 kfree(pbook_pci_saves);
1818 pbook_pci_saves = NULL;
1819 pbook_npci_saves = 0;
1823 pbook_pci_save(void)
1825 struct pci_save *ps = pbook_pci_saves;
1826 struct pci_dev *pd = NULL;
1827 int npci = pbook_npci_saves;
1832 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1837 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1838 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1839 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1840 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1845 /* For this to work, we must take care of a few things: If gmac was enabled
1846 * during boot, it will be in the pci dev list. If it's disabled at this point
1847 * (and it will probably be), then you can't access it's config space.
1850 pbook_pci_restore(void)
1853 struct pci_save *ps = pbook_pci_saves - 1;
1854 struct pci_dev *pd = NULL;
1855 int npci = pbook_npci_saves;
1858 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1862 if (ps->command == 0)
1864 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1865 if ((ps->command & ~cmd) == 0)
1867 switch (pd->hdr_type) {
1868 case PCI_HEADER_TYPE_NORMAL:
1869 for (j = 0; j < 6; ++j)
1870 pci_write_config_dword(pd,
1871 PCI_BASE_ADDRESS_0 + j*4,
1872 pd->resource[j].start);
1873 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1875 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1877 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1879 pci_write_config_word(pd, PCI_COMMAND, ps->command);
1886 /* N.B. This doesn't work on the 3400 */
1890 struct adb_request req;
1892 memset(&req, 0, sizeof(req));
1894 for (; n > 0; --n) {
1901 req.reply[0] = ADB_RET_OK;
1903 req.reply_expected = 0;
1904 pmu_polled_request(&req);
1912 req.reply[0] = ADB_RET_OK;
1914 req.reply_expected = 0;
1915 pmu_polled_request(&req);
1923 * Put the powerbook to sleep.
1926 static u32 save_via[8];
1929 save_via_state(void)
1931 save_via[0] = in_8(&via[ANH]);
1932 save_via[1] = in_8(&via[DIRA]);
1933 save_via[2] = in_8(&via[B]);
1934 save_via[3] = in_8(&via[DIRB]);
1935 save_via[4] = in_8(&via[PCR]);
1936 save_via[5] = in_8(&via[ACR]);
1937 save_via[6] = in_8(&via[T1CL]);
1938 save_via[7] = in_8(&via[T1CH]);
1941 restore_via_state(void)
1943 out_8(&via[ANH], save_via[0]);
1944 out_8(&via[DIRA], save_via[1]);
1945 out_8(&via[B], save_via[2]);
1946 out_8(&via[DIRB], save_via[3]);
1947 out_8(&via[PCR], save_via[4]);
1948 out_8(&via[ACR], save_via[5]);
1949 out_8(&via[T1CL], save_via[6]);
1950 out_8(&via[T1CH], save_via[7]);
1951 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
1952 out_8(&via[IFR], 0x7f); /* clear IFR */
1953 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
1956 extern void pmu_backlight_set_sleep(int sleep);
1959 pmac_suspend_devices(void)
1963 pm_prepare_console();
1965 /* Notify old-style device drivers */
1966 broadcast_sleep(PBOOK_SLEEP_REQUEST);
1968 /* Sync the disks. */
1969 /* XXX It would be nice to have some way to ensure that
1970 * nobody is dirtying any new buffers while we wait. That
1971 * could be achieved using the refrigerator for processes
1976 broadcast_sleep(PBOOK_SLEEP_NOW);
1978 /* Send suspend call to devices, hold the device core's dpm_sem */
1979 ret = device_suspend(PMSG_SUSPEND);
1982 printk(KERN_ERR "Driver sleep failed\n");
1986 #ifdef CONFIG_PMAC_BACKLIGHT
1987 /* Tell backlight code not to muck around with the chip anymore */
1988 pmu_backlight_set_sleep(1);
1991 /* Call platform functions marked "on sleep" */
1992 pmac_pfunc_i2c_suspend();
1993 pmac_pfunc_base_suspend();
1995 /* Stop preemption */
1998 /* Make sure the decrementer won't interrupt us */
1999 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2000 /* Make sure any pending DEC interrupt occurring while we did
2001 * the above didn't re-enable the DEC */
2003 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2005 /* We can now disable MSR_EE. This code of course works properly only
2006 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2007 * stop the "other" CPUs way before we do all that stuff.
2009 local_irq_disable();
2011 /* Broadcast power down irq
2012 * This isn't that useful in most cases (only directly wired devices can
2013 * use this but still... This will take care of sysdev's as well, so
2014 * we exit from here with local irqs disabled and PIC off.
2016 ret = device_power_down(PMSG_SUSPEND);
2018 wakeup_decrementer();
2023 printk(KERN_ERR "Driver powerdown failed\n");
2027 /* Wait for completion of async requests */
2028 while (!batt_req.complete)
2031 /* Giveup the lazy FPU & vec so we don't have to back them
2032 * up from the low level code
2036 #ifdef CONFIG_ALTIVEC
2037 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2038 enable_kernel_altivec();
2039 #endif /* CONFIG_ALTIVEC */
2045 pmac_wakeup_devices(void)
2049 #ifdef CONFIG_PMAC_BACKLIGHT
2050 /* Tell backlight code it can use the chip again */
2051 pmu_backlight_set_sleep(0);
2054 /* Power back up system devices (including the PIC) */
2057 /* Force a poll of ADB interrupts */
2058 adb_int_pending = 1;
2059 via_pmu_interrupt(0, NULL);
2061 /* Restart jiffies & scheduling */
2062 wakeup_decrementer();
2064 /* Re-enable local CPU interrupts */
2069 /* Call platform functions marked "on wake" */
2070 pmac_pfunc_base_resume();
2071 pmac_pfunc_i2c_resume();
2073 /* Resume devices */
2076 /* Notify old style drivers */
2079 pm_restore_console();
2084 #define GRACKLE_PM (1<<7)
2085 #define GRACKLE_DOZE (1<<5)
2086 #define GRACKLE_NAP (1<<4)
2087 #define GRACKLE_SLEEP (1<<3)
2089 static int powerbook_sleep_grackle(void)
2091 unsigned long save_l2cr;
2092 unsigned short pmcr1;
2093 struct adb_request req;
2095 struct pci_dev *grackle;
2097 grackle = pci_get_bus_and_slot(0, 0);
2101 ret = pmac_suspend_devices();
2103 printk(KERN_ERR "Sleep rejected by devices\n");
2107 /* Turn off various things. Darwin does some retry tests here... */
2108 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2109 pmu_wait_complete(&req);
2110 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2111 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2112 pmu_wait_complete(&req);
2114 /* For 750, save backside cache setting and disable it */
2115 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2117 if (!__fake_sleep) {
2118 /* Ask the PMU to put us to sleep */
2119 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2120 pmu_wait_complete(&req);
2123 /* The VIA is supposed not to be restored correctly*/
2125 /* We shut down some HW */
2126 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2128 pci_read_config_word(grackle, 0x70, &pmcr1);
2129 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2130 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2131 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2132 pci_write_config_word(grackle, 0x70, pmcr1);
2134 /* Call low-level ASM sleep handler */
2138 low_sleep_handler();
2140 /* We're awake again, stop grackle PM */
2141 pci_read_config_word(grackle, 0x70, &pmcr1);
2142 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2143 pci_write_config_word(grackle, 0x70, pmcr1);
2145 pci_dev_put(grackle);
2147 /* Make sure the PMU is idle */
2148 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2149 restore_via_state();
2151 /* Restore L2 cache */
2152 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2153 _set_L2CR(save_l2cr);
2155 /* Restore userland MMU context */
2156 set_context(current->active_mm->context.id, current->active_mm->pgd);
2158 /* Power things up */
2160 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2161 pmu_wait_complete(&req);
2162 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2163 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2164 pmu_wait_complete(&req);
2165 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2166 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2167 pmu_wait_complete(&req);
2169 pmac_wakeup_devices();
2175 powerbook_sleep_Core99(void)
2177 unsigned long save_l2cr;
2178 unsigned long save_l3cr;
2179 struct adb_request req;
2182 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2183 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2187 if (num_online_cpus() > 1 || cpu_is_offline(0))
2190 ret = pmac_suspend_devices();
2192 printk(KERN_ERR "Sleep rejected by devices\n");
2196 /* Stop environment and ADB interrupts */
2197 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2198 pmu_wait_complete(&req);
2200 /* Tell PMU what events will wake us up */
2201 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2203 pmu_wait_complete(&req);
2204 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2205 0, PMU_PWR_WAKEUP_KEY |
2206 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2207 pmu_wait_complete(&req);
2209 /* Save the state of the L2 and L3 caches */
2210 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2211 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2213 if (!__fake_sleep) {
2214 /* Ask the PMU to put us to sleep */
2215 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2216 pmu_wait_complete(&req);
2219 /* The VIA is supposed not to be restored correctly*/
2222 /* Shut down various ASICs. There's a chance that we can no longer
2223 * talk to the PMU after this, so I moved it to _after_ sending the
2224 * sleep command to it. Still need to be checked.
2226 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2228 /* Call low-level ASM sleep handler */
2232 low_sleep_handler();
2234 /* Restore Apple core ASICs state */
2235 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2238 restore_via_state();
2240 /* tweak LPJ before cpufreq is there */
2241 loops_per_jiffy *= 2;
2244 pmac_call_early_video_resume();
2246 /* Restore L2 cache */
2247 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2248 _set_L2CR(save_l2cr);
2249 /* Restore L3 cache */
2250 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2251 _set_L3CR(save_l3cr);
2253 /* Restore userland MMU context */
2254 set_context(current->active_mm->context.id, current->active_mm->pgd);
2256 /* Tell PMU we are ready */
2258 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2259 pmu_wait_complete(&req);
2260 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2261 pmu_wait_complete(&req);
2263 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2264 loops_per_jiffy /= 2;
2266 pmac_wakeup_devices();
2271 #define PB3400_MEM_CTRL 0xf8000000
2272 #define PB3400_MEM_CTRL_SLEEP 0x70
2275 powerbook_sleep_3400(void)
2280 struct adb_request sleep_req;
2281 void __iomem *mem_ctrl;
2282 unsigned int __iomem *mem_ctrl_sleep;
2284 /* first map in the memory controller registers */
2285 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2286 if (mem_ctrl == NULL) {
2287 printk("powerbook_sleep_3400: ioremap failed\n");
2290 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2292 /* Allocate room for PCI save */
2293 pbook_alloc_pci_save();
2295 ret = pmac_suspend_devices();
2297 pbook_free_pci_save();
2298 printk(KERN_ERR "Sleep rejected by devices\n");
2302 /* Save the state of PCI config space for some slots */
2305 /* Set the memory controller to keep the memory refreshed
2306 while we're asleep */
2307 for (i = 0x403f; i >= 0x4000; --i) {
2308 out_be32(mem_ctrl_sleep, i);
2310 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2316 /* Ask the PMU to put us to sleep */
2317 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2318 while (!sleep_req.complete)
2321 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2323 /* displacement-flush the L2 cache - necessary? */
2324 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2325 i = *(volatile int *)p;
2328 /* Put the CPU into sleep mode */
2329 hid0 = mfspr(SPRN_HID0);
2330 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2331 mtspr(SPRN_HID0, hid0);
2332 mtmsr(mfmsr() | MSR_POW | MSR_EE);
2335 /* OK, we're awake again, start restoring things */
2336 out_be32(mem_ctrl_sleep, 0x3f);
2337 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2338 pbook_pci_restore();
2341 /* wait for the PMU interrupt sequence to complete */
2345 pmac_wakeup_devices();
2346 pbook_free_pci_save();
2352 #endif /* CONFIG_PM_SLEEP && CONFIG_PPC32 */
2355 * Support for /dev/pmu device
2357 #define RB_SIZE 0x10
2358 struct pmu_private {
2359 struct list_head list;
2364 unsigned char data[16];
2366 wait_queue_head_t wait;
2368 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2369 int backlight_locker;
2373 static LIST_HEAD(all_pmu_pvt);
2374 static DEFINE_SPINLOCK(all_pvt_lock);
2377 pmu_pass_intr(unsigned char *data, int len)
2379 struct pmu_private *pp;
2380 struct list_head *list;
2382 unsigned long flags;
2384 if (len > sizeof(pp->rb_buf[0].data))
2385 len = sizeof(pp->rb_buf[0].data);
2386 spin_lock_irqsave(&all_pvt_lock, flags);
2387 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2388 pp = list_entry(list, struct pmu_private, list);
2389 spin_lock(&pp->lock);
2393 if (i != pp->rb_get) {
2394 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2396 memcpy(rp->data, data, len);
2398 wake_up_interruptible(&pp->wait);
2400 spin_unlock(&pp->lock);
2402 spin_unlock_irqrestore(&all_pvt_lock, flags);
2406 pmu_open(struct inode *inode, struct file *file)
2408 struct pmu_private *pp;
2409 unsigned long flags;
2411 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2414 pp->rb_get = pp->rb_put = 0;
2415 spin_lock_init(&pp->lock);
2416 init_waitqueue_head(&pp->wait);
2417 spin_lock_irqsave(&all_pvt_lock, flags);
2418 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2419 pp->backlight_locker = 0;
2421 list_add(&pp->list, &all_pmu_pvt);
2422 spin_unlock_irqrestore(&all_pvt_lock, flags);
2423 file->private_data = pp;
2428 pmu_read(struct file *file, char __user *buf,
2429 size_t count, loff_t *ppos)
2431 struct pmu_private *pp = file->private_data;
2432 DECLARE_WAITQUEUE(wait, current);
2433 unsigned long flags;
2436 if (count < 1 || pp == 0)
2438 if (!access_ok(VERIFY_WRITE, buf, count))
2441 spin_lock_irqsave(&pp->lock, flags);
2442 add_wait_queue(&pp->wait, &wait);
2443 current->state = TASK_INTERRUPTIBLE;
2447 if (pp->rb_get != pp->rb_put) {
2449 struct rb_entry *rp = &pp->rb_buf[i];
2451 spin_unlock_irqrestore(&pp->lock, flags);
2454 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2458 spin_lock_irqsave(&pp->lock, flags);
2463 if (file->f_flags & O_NONBLOCK)
2466 if (signal_pending(current))
2468 spin_unlock_irqrestore(&pp->lock, flags);
2470 spin_lock_irqsave(&pp->lock, flags);
2472 current->state = TASK_RUNNING;
2473 remove_wait_queue(&pp->wait, &wait);
2474 spin_unlock_irqrestore(&pp->lock, flags);
2480 pmu_write(struct file *file, const char __user *buf,
2481 size_t count, loff_t *ppos)
2487 pmu_fpoll(struct file *filp, poll_table *wait)
2489 struct pmu_private *pp = filp->private_data;
2490 unsigned int mask = 0;
2491 unsigned long flags;
2495 poll_wait(filp, &pp->wait, wait);
2496 spin_lock_irqsave(&pp->lock, flags);
2497 if (pp->rb_get != pp->rb_put)
2499 spin_unlock_irqrestore(&pp->lock, flags);
2504 pmu_release(struct inode *inode, struct file *file)
2506 struct pmu_private *pp = file->private_data;
2507 unsigned long flags;
2510 file->private_data = NULL;
2511 spin_lock_irqsave(&all_pvt_lock, flags);
2512 list_del(&pp->list);
2513 spin_unlock_irqrestore(&all_pvt_lock, flags);
2515 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2516 if (pp->backlight_locker)
2517 pmac_backlight_enable();
2526 pmu_ioctl(struct inode * inode, struct file *filp,
2527 u_int cmd, u_long arg)
2529 __u32 __user *argp = (__u32 __user *)arg;
2530 int error = -EINVAL;
2533 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PPC32)
2535 if (!capable(CAP_SYS_ADMIN))
2537 if (sleep_in_progress)
2539 sleep_in_progress = 1;
2541 case PMU_OHARE_BASED:
2542 error = powerbook_sleep_3400();
2544 case PMU_HEATHROW_BASED:
2545 case PMU_PADDINGTON_BASED:
2546 error = powerbook_sleep_grackle();
2548 case PMU_KEYLARGO_BASED:
2549 error = powerbook_sleep_Core99();
2554 sleep_in_progress = 0;
2556 case PMU_IOC_CAN_SLEEP:
2557 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2558 return put_user(0, argp);
2560 return put_user(1, argp);
2561 #endif /* CONFIG_PM_SLEEP && CONFIG_PPC32 */
2563 #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2564 /* Compatibility ioctl's for backlight */
2565 case PMU_IOC_GET_BACKLIGHT:
2569 if (sleep_in_progress)
2572 brightness = pmac_backlight_get_legacy_brightness();
2576 return put_user(brightness, argp);
2579 case PMU_IOC_SET_BACKLIGHT:
2583 if (sleep_in_progress)
2586 error = get_user(brightness, argp);
2590 return pmac_backlight_set_legacy_brightness(brightness);
2592 #ifdef CONFIG_INPUT_ADBHID
2593 case PMU_IOC_GRAB_BACKLIGHT: {
2594 struct pmu_private *pp = filp->private_data;
2596 if (pp->backlight_locker)
2599 pp->backlight_locker = 1;
2600 pmac_backlight_disable();
2604 #endif /* CONFIG_INPUT_ADBHID */
2605 #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
2607 case PMU_IOC_GET_MODEL:
2608 return put_user(pmu_kind, argp);
2609 case PMU_IOC_HAS_ADB:
2610 return put_user(pmu_has_adb, argp);
2615 static const struct file_operations pmu_device_fops = {
2621 .release = pmu_release,
2624 static struct miscdevice pmu_device = {
2625 PMU_MINOR, "pmu", &pmu_device_fops
2628 static int pmu_device_init(void)
2632 if (misc_register(&pmu_device) < 0)
2633 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2636 device_initcall(pmu_device_init);
2641 polled_handshake(volatile unsigned char __iomem *via)
2643 via[B] &= ~TREQ; eieio();
2644 while ((via[B] & TACK) != 0)
2646 via[B] |= TREQ; eieio();
2647 while ((via[B] & TACK) == 0)
2652 polled_send_byte(volatile unsigned char __iomem *via, int x)
2654 via[ACR] |= SR_OUT | SR_EXT; eieio();
2655 via[SR] = x; eieio();
2656 polled_handshake(via);
2660 polled_recv_byte(volatile unsigned char __iomem *via)
2664 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2665 x = via[SR]; eieio();
2666 polled_handshake(via);
2667 x = via[SR]; eieio();
2672 pmu_polled_request(struct adb_request *req)
2674 unsigned long flags;
2676 volatile unsigned char __iomem *v = via;
2680 l = pmu_data_len[c][0];
2681 if (l >= 0 && req->nbytes != l + 1)
2684 local_irq_save(flags);
2685 while (pmu_state != idle)
2688 while ((via[B] & TACK) == 0)
2690 polled_send_byte(v, c);
2692 l = req->nbytes - 1;
2693 polled_send_byte(v, l);
2695 for (i = 1; i <= l; ++i)
2696 polled_send_byte(v, req->data[i]);
2698 l = pmu_data_len[c][1];
2700 l = polled_recv_byte(v);
2701 for (i = 0; i < l; ++i)
2702 req->reply[i + req->reply_len] = polled_recv_byte(v);
2707 local_irq_restore(flags);
2710 #endif /* DEBUG_SLEEP */
2713 /* FIXME: This is a temporary set of callbacks to enable us
2714 * to do suspend-to-disk.
2717 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PPC32)
2719 int pmu_sys_suspended;
2721 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
2723 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
2726 /* Suspend PMU event interrupts */
2729 pmu_sys_suspended = 1;
2733 static int pmu_sys_resume(struct sys_device *sysdev)
2735 struct adb_request req;
2737 if (!pmu_sys_suspended)
2740 /* Tell PMU we are ready */
2741 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2742 pmu_wait_complete(&req);
2744 /* Resume PMU event interrupts */
2747 pmu_sys_suspended = 0;
2752 #endif /* CONFIG_PM_SLEEP && CONFIG_PPC32 */
2754 static struct sysdev_class pmu_sysclass = {
2755 set_kset_name("pmu"),
2758 static struct sys_device device_pmu = {
2759 .cls = &pmu_sysclass,
2762 static struct sysdev_driver driver_pmu = {
2763 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PPC32)
2764 .suspend = &pmu_sys_suspend,
2765 .resume = &pmu_sys_resume,
2766 #endif /* CONFIG_PM_SLEEP && CONFIG_PPC32 */
2769 static int __init init_pmu_sysfs(void)
2773 rc = sysdev_class_register(&pmu_sysclass);
2775 printk(KERN_ERR "Failed registering PMU sys class\n");
2778 rc = sysdev_register(&device_pmu);
2780 printk(KERN_ERR "Failed registering PMU sys device\n");
2783 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2785 printk(KERN_ERR "Failed registering PMU sys driver\n");
2791 subsys_initcall(init_pmu_sysfs);
2793 EXPORT_SYMBOL(pmu_request);
2794 EXPORT_SYMBOL(pmu_queue_request);
2795 EXPORT_SYMBOL(pmu_poll);
2796 EXPORT_SYMBOL(pmu_poll_adb);
2797 EXPORT_SYMBOL(pmu_wait_complete);
2798 EXPORT_SYMBOL(pmu_suspend);
2799 EXPORT_SYMBOL(pmu_resume);
2800 EXPORT_SYMBOL(pmu_unlock);
2801 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PPC32)
2802 EXPORT_SYMBOL(pmu_enable_irled);
2803 EXPORT_SYMBOL(pmu_battery_count);
2804 EXPORT_SYMBOL(pmu_batteries);
2805 EXPORT_SYMBOL(pmu_power_flags);
2806 #endif /* CONFIG_PM_SLEEP && CONFIG_PPC32 */