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/smp_lock.h>
37 #include <linux/module.h>
38 #include <linux/spinlock.h>
40 #include <linux/proc_fs.h>
41 #include <linux/init.h>
42 #include <linux/interrupt.h>
43 #include <linux/device.h>
44 #include <linux/sysdev.h>
45 #include <linux/suspend.h>
46 #include <linux/syscalls.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 */
67 #undef SUSPEND_USES_PMU
69 #undef HACKED_PCI_SAVE
71 /* Misc minor number allocated for /dev/pmu */
74 /* How many iterations between battery polls */
75 #define BATTERY_POLLING_COUNT 2
77 static volatile unsigned char __iomem *via;
79 /* VIA registers - spaced 0x200 bytes apart */
80 #define RS 0x200 /* skip between registers */
81 #define B 0 /* B-side data */
82 #define A RS /* A-side data */
83 #define DIRB (2*RS) /* B-side direction (1=output) */
84 #define DIRA (3*RS) /* A-side direction (1=output) */
85 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
86 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
87 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
88 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
89 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
90 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
91 #define SR (10*RS) /* Shift register */
92 #define ACR (11*RS) /* Auxiliary control register */
93 #define PCR (12*RS) /* Peripheral control register */
94 #define IFR (13*RS) /* Interrupt flag register */
95 #define IER (14*RS) /* Interrupt enable register */
96 #define ANH (15*RS) /* A-side data, no handshake */
98 /* Bits in B data register: both active low */
99 #define TACK 0x08 /* Transfer acknowledge (input) */
100 #define TREQ 0x10 /* Transfer request (output) */
103 #define SR_CTRL 0x1c /* Shift register control bits */
104 #define SR_EXT 0x0c /* Shift on external clock */
105 #define SR_OUT 0x10 /* Shift out if 1 */
107 /* Bits in IFR and IER */
108 #define IER_SET 0x80 /* set bits in IER */
109 #define IER_CLR 0 /* clear bits in IER */
110 #define SR_INT 0x04 /* Shift register full/empty */
112 #define CB1_INT 0x10 /* transition on CB1 input */
114 static volatile enum pmu_state {
123 static volatile enum int_data_state {
128 } int_data_state[2] = { int_data_empty, int_data_empty };
130 static struct adb_request *current_req;
131 static struct adb_request *last_req;
132 static struct adb_request *req_awaiting_reply;
133 static unsigned char interrupt_data[2][32];
134 static int interrupt_data_len[2];
135 static int int_data_last;
136 static unsigned char *reply_ptr;
137 static int data_index;
139 static volatile int adb_int_pending;
140 static volatile int disable_poll;
141 static struct device_node *vias;
142 static int pmu_kind = PMU_UNKNOWN;
143 static int pmu_fully_inited = 0;
144 static int pmu_has_adb;
145 static struct device_node *gpio_node;
146 static unsigned char __iomem *gpio_reg = NULL;
147 static int gpio_irq = NO_IRQ;
148 static int gpio_irq_enabled = -1;
149 static volatile int pmu_suspended = 0;
150 static spinlock_t pmu_lock;
151 static u8 pmu_intr_mask;
152 static int pmu_version;
153 static int drop_interrupts;
154 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
155 static int option_lid_wakeup = 1;
156 #endif /* CONFIG_PM && CONFIG_PPC32 */
157 #if (defined(CONFIG_PM)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT_LEGACY)
158 static int sleep_in_progress;
160 static unsigned long async_req_locks;
161 static unsigned int pmu_irq_stats[11];
163 static struct proc_dir_entry *proc_pmu_root;
164 static struct proc_dir_entry *proc_pmu_info;
165 static struct proc_dir_entry *proc_pmu_irqstats;
166 static struct proc_dir_entry *proc_pmu_options;
167 static int option_server_mode;
169 int pmu_battery_count;
171 unsigned int pmu_power_flags;
172 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
173 static int query_batt_timer = BATTERY_POLLING_COUNT;
174 static struct adb_request batt_req;
175 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
179 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
182 static int adb_dev_map = 0;
183 static int pmu_adb_flags;
185 static int pmu_probe(void);
186 static int pmu_init(void);
187 static int pmu_send_request(struct adb_request *req, int sync);
188 static int pmu_adb_autopoll(int devs);
189 static int pmu_adb_reset_bus(void);
190 #endif /* CONFIG_ADB */
192 static int init_pmu(void);
193 static void pmu_start(void);
194 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
195 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
196 static int proc_get_info(char *page, char **start, off_t off,
197 int count, int *eof, void *data);
198 static int proc_get_irqstats(char *page, char **start, off_t off,
199 int count, int *eof, void *data);
200 static void pmu_pass_intr(unsigned char *data, int len);
201 static int proc_get_batt(char *page, char **start, off_t off,
202 int count, int *eof, void *data);
203 static int proc_read_options(char *page, char **start, off_t off,
204 int count, int *eof, void *data);
205 static int proc_write_options(struct file *file, const char __user *buffer,
206 unsigned long count, void *data);
209 struct adb_driver via_pmu_driver = {
218 #endif /* CONFIG_ADB */
220 extern void low_sleep_handler(void);
221 extern void enable_kernel_altivec(void);
222 extern void enable_kernel_fp(void);
225 int pmu_polled_request(struct adb_request *req);
226 int pmu_wink(struct adb_request *req);
230 * This table indicates for each PMU opcode:
231 * - the number of data bytes to be sent with the command, or -1
232 * if a length byte should be sent,
233 * - the number of response bytes which the PMU will return, or
234 * -1 if it will send a length byte.
236 static const s8 pmu_data_len[256][2] = {
237 /* 0 1 2 3 4 5 6 7 */
238 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
239 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
240 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
241 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
242 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
243 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
244 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
245 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
246 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
247 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
248 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
249 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
250 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
251 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
252 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
253 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
254 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
255 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
256 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
257 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
258 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
259 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
260 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
261 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
262 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
263 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
264 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
265 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
266 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
267 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
268 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
269 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
272 static char *pbook_type[] = {
274 "PowerBook 2400/3400/3500(G3)",
275 "PowerBook G3 Series",
280 int __init find_via_pmu(void)
287 vias = of_find_node_by_name(NULL, "via-pmu");
291 reg = (u32 *)get_property(vias, "reg", NULL);
293 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
296 taddr = of_translate_address(vias, reg);
297 if (taddr == OF_BAD_ADDR) {
298 printk(KERN_ERR "via-pmu: Can't translate address !\n");
302 spin_lock_init(&pmu_lock);
306 pmu_intr_mask = PMU_INT_PCEJECT |
311 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
312 || device_is_compatible(vias->parent, "ohare")))
313 pmu_kind = PMU_OHARE_BASED;
314 else if (device_is_compatible(vias->parent, "paddington"))
315 pmu_kind = PMU_PADDINGTON_BASED;
316 else if (device_is_compatible(vias->parent, "heathrow"))
317 pmu_kind = PMU_HEATHROW_BASED;
318 else if (device_is_compatible(vias->parent, "Keylargo")
319 || device_is_compatible(vias->parent, "K2-Keylargo")) {
320 struct device_node *gpiop;
321 u64 gaddr = OF_BAD_ADDR;
323 pmu_kind = PMU_KEYLARGO_BASED;
324 pmu_has_adb = (find_type_devices("adb") != NULL);
325 pmu_intr_mask = PMU_INT_PCEJECT |
331 gpiop = of_find_node_by_name(NULL, "gpio");
333 reg = (u32 *)get_property(gpiop, "reg", NULL);
335 gaddr = of_translate_address(gpiop, reg);
336 if (gaddr != OF_BAD_ADDR)
337 gpio_reg = ioremap(gaddr, 0x10);
339 if (gpio_reg == NULL)
340 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
342 pmu_kind = PMU_UNKNOWN;
344 via = ioremap(taddr, 0x2000);
346 printk(KERN_ERR "via-pmu: Can't map address !\n");
350 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
351 out_8(&via[IFR], 0x7f); /* clear IFR */
360 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
361 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
363 sys_ctrler = SYS_CTRLER_PMU;
373 static int pmu_probe(void)
375 return vias == NULL? -ENODEV: 0;
378 static int __init pmu_init(void)
384 #endif /* CONFIG_ADB */
387 * We can't wait until pmu_init gets called, that happens too late.
388 * It happens after IDE and SCSI initialization, which can take a few
389 * seconds, and by that time the PMU could have given up on us and
391 * Thus this is called with arch_initcall rather than device_initcall.
393 static int __init via_pmu_start(void)
400 batt_req.complete = 1;
402 irq = irq_of_parse_and_map(vias, 0);
404 printk(KERN_ERR "via-pmu: can't map interruptn");
407 if (request_irq(irq, via_pmu_interrupt, 0, "VIA-PMU", (void *)0)) {
408 printk(KERN_ERR "via-pmu: can't request irq %d\n", irq);
412 if (pmu_kind == PMU_KEYLARGO_BASED) {
413 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
414 if (gpio_node == NULL)
415 gpio_node = of_find_node_by_name(NULL,
418 gpio_irq = irq_of_parse_and_map(gpio_node, 0);
420 if (gpio_irq != NO_IRQ) {
421 if (request_irq(gpio_irq, gpio1_interrupt, 0,
422 "GPIO1 ADB", (void *)0))
423 printk(KERN_ERR "pmu: can't get irq %d"
424 " (GPIO1)\n", gpio_irq);
426 gpio_irq_enabled = 1;
430 /* Enable interrupts */
431 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
433 pmu_fully_inited = 1;
435 /* Make sure PMU settle down before continuing. This is _very_ important
436 * since the IDE probe may shut interrupts down for quite a bit of time. If
437 * a PMU communication is pending while this happens, the PMU may timeout
438 * Not that on Core99 machines, the PMU keeps sending us environement
439 * messages, we should find a way to either fix IDE or make it call
440 * pmu_suspend() before masking interrupts. This can also happens while
441 * scolling with some fbdevs.
445 } while (pmu_state != idle);
450 arch_initcall(via_pmu_start);
453 * This has to be done after pci_init, which is a subsys_initcall.
455 static int __init via_pmu_dev_init(void)
460 #ifdef CONFIG_PMAC_BACKLIGHT
461 /* Initialize backlight */
462 pmu_backlight_init();
466 if (machine_is_compatible("AAPL,3400/2400") ||
467 machine_is_compatible("AAPL,3500")) {
468 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
469 NULL, PMAC_MB_INFO_MODEL, 0);
470 pmu_battery_count = 1;
471 if (mb == PMAC_TYPE_COMET)
472 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
474 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
475 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
476 machine_is_compatible("PowerBook1,1")) {
477 pmu_battery_count = 2;
478 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
479 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
481 struct device_node* prim = find_devices("power-mgt");
482 u32 *prim_info = NULL;
484 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
486 /* Other stuffs here yet unknown */
487 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
488 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
489 if (pmu_battery_count > 1)
490 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
493 #endif /* CONFIG_PPC32 */
495 /* Create /proc/pmu */
496 proc_pmu_root = proc_mkdir("pmu", NULL);
500 for (i=0; i<pmu_battery_count; i++) {
502 sprintf(title, "battery_%ld", i);
503 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
504 proc_get_batt, (void *)i);
507 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
508 proc_get_info, NULL);
509 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
510 proc_get_irqstats, NULL);
511 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
512 if (proc_pmu_options) {
513 proc_pmu_options->nlink = 1;
514 proc_pmu_options->read_proc = proc_read_options;
515 proc_pmu_options->write_proc = proc_write_options;
521 device_initcall(via_pmu_dev_init);
527 struct adb_request req;
529 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
530 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
532 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
534 while (!req.complete) {
536 printk(KERN_ERR "init_pmu: no response from PMU\n");
543 /* ack all pending interrupts */
545 interrupt_data[0][0] = 1;
546 while (interrupt_data[0][0] || pmu_state != idle) {
548 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
551 if (pmu_state == idle)
553 via_pmu_interrupt(0, NULL, NULL);
557 /* Tell PMU we are ready. */
558 if (pmu_kind == PMU_KEYLARGO_BASED) {
559 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
560 while (!req.complete)
564 /* Read PMU version */
565 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
566 pmu_wait_complete(&req);
567 if (req.reply_len > 0)
568 pmu_version = req.reply[0];
570 /* Read server mode setting */
571 if (pmu_kind == PMU_KEYLARGO_BASED) {
572 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
573 PMU_PWR_GET_POWERUP_EVENTS);
574 pmu_wait_complete(&req);
575 if (req.reply_len == 2) {
576 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
577 option_server_mode = 1;
578 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
579 option_server_mode ? "enabled" : "disabled");
591 static void pmu_set_server_mode(int server_mode)
593 struct adb_request req;
595 if (pmu_kind != PMU_KEYLARGO_BASED)
598 option_server_mode = server_mode;
599 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
600 pmu_wait_complete(&req);
601 if (req.reply_len < 2)
604 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
605 PMU_PWR_SET_POWERUP_EVENTS,
606 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
608 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
609 PMU_PWR_CLR_POWERUP_EVENTS,
610 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
611 pmu_wait_complete(&req);
614 /* This new version of the code for 2400/3400/3500 powerbooks
615 * is inspired from the implementation in gkrellm-pmu
618 done_battery_state_ohare(struct adb_request* req)
622 * 0x01 : AC indicator
624 * 0x04 : battery exist
627 * 0x20 : full charged
628 * 0x40 : pcharge reset
629 * 0x80 : battery exist
631 * [1][2] : battery voltage
632 * [3] : CPU temperature
633 * [4] : battery temperature
638 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
639 long pcharge, charge, vb, vmax, lmax;
640 long vmax_charging, vmax_charged;
641 long amperage, voltage, time, max;
642 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
643 NULL, PMAC_MB_INFO_MODEL, 0);
645 if (req->reply[0] & 0x01)
646 pmu_power_flags |= PMU_PWR_AC_PRESENT;
648 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
650 if (mb == PMAC_TYPE_COMET) {
661 /* If battery installed */
662 if (req->reply[0] & 0x04) {
663 bat_flags |= PMU_BATT_PRESENT;
664 if (req->reply[0] & 0x02)
665 bat_flags |= PMU_BATT_CHARGING;
666 vb = (req->reply[1] << 8) | req->reply[2];
667 voltage = (vb * 265 + 72665) / 10;
668 amperage = req->reply[5];
669 if ((req->reply[0] & 0x01) == 0) {
671 vb += ((amperage - 200) * 15)/100;
672 } else if (req->reply[0] & 0x02) {
673 vb = (vb * 97) / 100;
674 vmax = vmax_charging;
676 charge = (100 * vb) / vmax;
677 if (req->reply[0] & 0x40) {
678 pcharge = (req->reply[6] << 8) + req->reply[7];
682 pcharge = 100 - pcharge / lmax;
683 if (pcharge < charge)
687 time = (charge * 16440) / amperage;
691 amperage = -amperage;
693 charge = max = amperage = voltage = time = 0;
695 pmu_batteries[pmu_cur_battery].flags = bat_flags;
696 pmu_batteries[pmu_cur_battery].charge = charge;
697 pmu_batteries[pmu_cur_battery].max_charge = max;
698 pmu_batteries[pmu_cur_battery].amperage = amperage;
699 pmu_batteries[pmu_cur_battery].voltage = voltage;
700 pmu_batteries[pmu_cur_battery].time_remaining = time;
702 clear_bit(0, &async_req_locks);
706 done_battery_state_smart(struct adb_request* req)
709 * [0] : format of this structure (known: 3,4,5)
722 * [4][5] : max charge
727 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
729 unsigned int capa, max, voltage;
731 if (req->reply[1] & 0x01)
732 pmu_power_flags |= PMU_PWR_AC_PRESENT;
734 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
737 capa = max = amperage = voltage = 0;
739 if (req->reply[1] & 0x04) {
740 bat_flags |= PMU_BATT_PRESENT;
741 switch(req->reply[0]) {
743 case 4: capa = req->reply[2];
745 amperage = *((signed char *)&req->reply[4]);
746 voltage = req->reply[5];
748 case 5: capa = (req->reply[2] << 8) | req->reply[3];
749 max = (req->reply[4] << 8) | req->reply[5];
750 amperage = *((signed short *)&req->reply[6]);
751 voltage = (req->reply[8] << 8) | req->reply[9];
754 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
755 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
760 if ((req->reply[1] & 0x01) && (amperage > 0))
761 bat_flags |= PMU_BATT_CHARGING;
763 pmu_batteries[pmu_cur_battery].flags = bat_flags;
764 pmu_batteries[pmu_cur_battery].charge = capa;
765 pmu_batteries[pmu_cur_battery].max_charge = max;
766 pmu_batteries[pmu_cur_battery].amperage = amperage;
767 pmu_batteries[pmu_cur_battery].voltage = voltage;
769 if ((req->reply[1] & 0x01) && (amperage > 0))
770 pmu_batteries[pmu_cur_battery].time_remaining
771 = ((max-capa) * 3600) / amperage;
773 pmu_batteries[pmu_cur_battery].time_remaining
774 = (capa * 3600) / (-amperage);
776 pmu_batteries[pmu_cur_battery].time_remaining = 0;
778 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
780 clear_bit(0, &async_req_locks);
784 query_battery_state(void)
786 if (test_and_set_bit(0, &async_req_locks))
788 if (pmu_kind == PMU_OHARE_BASED)
789 pmu_request(&batt_req, done_battery_state_ohare,
790 1, PMU_BATTERY_STATE);
792 pmu_request(&batt_req, done_battery_state_smart,
793 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
797 proc_get_info(char *page, char **start, off_t off,
798 int count, int *eof, void *data)
802 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
803 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
804 p += sprintf(p, "AC Power : %d\n",
805 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
806 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
812 proc_get_irqstats(char *page, char **start, off_t off,
813 int count, int *eof, void *data)
817 static const char *irq_names[] = {
818 "Total CB1 triggered events",
819 "Total GPIO1 triggered events",
820 "PC-Card eject button",
821 "Sound/Brightness button",
823 "Battery state change",
824 "Environment interrupt",
826 "Ghost interrupt (zero len)",
827 "Empty interrupt (empty mask)",
831 for (i=0; i<11; i++) {
832 p += sprintf(p, " %2u: %10u (%s)\n",
833 i, pmu_irq_stats[i], irq_names[i]);
839 proc_get_batt(char *page, char **start, off_t off,
840 int count, int *eof, void *data)
842 long batnum = (long)data;
845 p += sprintf(p, "\n");
846 p += sprintf(p, "flags : %08x\n",
847 pmu_batteries[batnum].flags);
848 p += sprintf(p, "charge : %d\n",
849 pmu_batteries[batnum].charge);
850 p += sprintf(p, "max_charge : %d\n",
851 pmu_batteries[batnum].max_charge);
852 p += sprintf(p, "current : %d\n",
853 pmu_batteries[batnum].amperage);
854 p += sprintf(p, "voltage : %d\n",
855 pmu_batteries[batnum].voltage);
856 p += sprintf(p, "time rem. : %d\n",
857 pmu_batteries[batnum].time_remaining);
863 proc_read_options(char *page, char **start, off_t off,
864 int count, int *eof, void *data)
868 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
869 if (pmu_kind == PMU_KEYLARGO_BASED &&
870 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
871 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
873 if (pmu_kind == PMU_KEYLARGO_BASED)
874 p += sprintf(p, "server_mode=%d\n", option_server_mode);
880 proc_write_options(struct file *file, const char __user *buffer,
881 unsigned long count, void *data)
885 unsigned long fcount = count;
891 if (copy_from_user(tmp, buffer, count))
899 while(*val && (*val != '=')) {
909 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
910 if (pmu_kind == PMU_KEYLARGO_BASED &&
911 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
912 if (!strcmp(label, "lid_wakeup"))
913 option_lid_wakeup = ((*val) == '1');
915 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
917 new_value = ((*val) == '1');
918 if (new_value != option_server_mode)
919 pmu_set_server_mode(new_value);
925 /* Send an ADB command */
927 pmu_send_request(struct adb_request *req, int sync)
931 if ((vias == NULL) || (!pmu_fully_inited)) {
938 switch (req->data[0]) {
940 for (i = 0; i < req->nbytes - 1; ++i)
941 req->data[i] = req->data[i+1];
943 if (pmu_data_len[req->data[0]][1] != 0) {
944 req->reply[0] = ADB_RET_OK;
948 ret = pmu_queue_request(req);
951 switch (req->data[1]) {
953 if (req->nbytes != 2)
955 req->data[0] = PMU_READ_RTC;
958 req->reply[0] = CUDA_PACKET;
960 req->reply[2] = CUDA_GET_TIME;
961 ret = pmu_queue_request(req);
964 if (req->nbytes != 6)
966 req->data[0] = PMU_SET_RTC;
968 for (i = 1; i <= 4; ++i)
969 req->data[i] = req->data[i+1];
971 req->reply[0] = CUDA_PACKET;
973 req->reply[2] = CUDA_SET_TIME;
974 ret = pmu_queue_request(req);
981 for (i = req->nbytes - 1; i > 1; --i)
982 req->data[i+2] = req->data[i];
983 req->data[3] = req->nbytes - 2;
984 req->data[2] = pmu_adb_flags;
985 /*req->data[1] = req->data[1];*/
986 req->data[0] = PMU_ADB_CMD;
988 req->reply_expected = 1;
990 ret = pmu_queue_request(req);
999 while (!req->complete)
1005 /* Enable/disable autopolling */
1007 pmu_adb_autopoll(int devs)
1009 struct adb_request req;
1011 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1016 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1017 adb_dev_map >> 8, adb_dev_map);
1020 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1023 while (!req.complete)
1028 /* Reset the ADB bus */
1030 pmu_adb_reset_bus(void)
1032 struct adb_request req;
1033 int save_autopoll = adb_dev_map;
1035 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1038 /* anyone got a better idea?? */
1039 pmu_adb_autopoll(0);
1043 req.data[0] = PMU_ADB_CMD;
1045 req.data[2] = ADB_BUSRESET;
1049 req.reply_expected = 1;
1050 if (pmu_queue_request(&req) != 0) {
1051 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1054 pmu_wait_complete(&req);
1056 if (save_autopoll != 0)
1057 pmu_adb_autopoll(save_autopoll);
1061 #endif /* CONFIG_ADB */
1063 /* Construct and send a pmu request */
1065 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1074 if (nbytes < 0 || nbytes > 32) {
1075 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1079 req->nbytes = nbytes;
1081 va_start(list, nbytes);
1082 for (i = 0; i < nbytes; ++i)
1083 req->data[i] = va_arg(list, int);
1086 req->reply_expected = 0;
1087 return pmu_queue_request(req);
1091 pmu_queue_request(struct adb_request *req)
1093 unsigned long flags;
1100 if (req->nbytes <= 0) {
1104 nsend = pmu_data_len[req->data[0]][0];
1105 if (nsend >= 0 && req->nbytes != nsend + 1) {
1114 spin_lock_irqsave(&pmu_lock, flags);
1115 if (current_req != 0) {
1116 last_req->next = req;
1121 if (pmu_state == idle)
1124 spin_unlock_irqrestore(&pmu_lock, flags);
1132 /* Sightly increased the delay, I had one occurrence of the message
1136 while ((in_8(&via[B]) & TACK) == 0) {
1137 if (--timeout < 0) {
1138 printk(KERN_ERR "PMU not responding (!ack)\n");
1145 /* New PMU seems to be very sensitive to those timings, so we make sure
1146 * PCI is flushed immediately */
1150 volatile unsigned char __iomem *v = via;
1152 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1154 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1161 volatile unsigned char __iomem *v = via;
1163 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1164 in_8(&v[SR]); /* resets SR */
1165 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1170 pmu_done(struct adb_request *req)
1172 void (*done)(struct adb_request *) = req->done;
1175 /* Here, we assume that if the request has a done member, the
1176 * struct request will survive to setting req->complete to 1
1185 struct adb_request *req;
1187 /* assert pmu_state == idle */
1188 /* get the packet to send */
1190 if (req == 0 || pmu_state != idle
1191 || (/*req->reply_expected && */req_awaiting_reply))
1194 pmu_state = sending;
1196 data_len = pmu_data_len[req->data[0]][0];
1198 /* Sounds safer to make sure ACK is high before writing. This helped
1199 * kill a problem with ADB and some iBooks
1202 /* set the shift register to shift out and send a byte */
1203 send_byte(req->data[0]);
1213 via_pmu_interrupt(0, NULL, NULL);
1223 /* Kicks ADB read when PMU is suspended */
1224 adb_int_pending = 1;
1226 via_pmu_interrupt(0, NULL, NULL);
1227 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1228 || req_awaiting_reply));
1232 pmu_wait_complete(struct adb_request *req)
1236 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1237 via_pmu_interrupt(0, NULL, NULL);
1240 /* This function loops until the PMU is idle and prevents it from
1241 * anwsering to ADB interrupts. pmu_request can still be called.
1242 * This is done to avoid spurrious shutdowns when we know we'll have
1243 * interrupts switched off for a long time
1248 unsigned long flags;
1249 #ifdef SUSPEND_USES_PMU
1250 struct adb_request *req;
1255 spin_lock_irqsave(&pmu_lock, flags);
1257 if (pmu_suspended > 1) {
1258 spin_unlock_irqrestore(&pmu_lock, flags);
1263 spin_unlock_irqrestore(&pmu_lock, flags);
1264 if (req_awaiting_reply)
1265 adb_int_pending = 1;
1266 via_pmu_interrupt(0, NULL, NULL);
1267 spin_lock_irqsave(&pmu_lock, flags);
1268 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1269 #ifdef SUSPEND_USES_PMU
1270 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1271 spin_unlock_irqrestore(&pmu_lock, flags);
1272 while(!req.complete)
1274 #else /* SUSPEND_USES_PMU */
1276 disable_irq_nosync(gpio_irq);
1277 out_8(&via[IER], CB1_INT | IER_CLR);
1278 spin_unlock_irqrestore(&pmu_lock, flags);
1279 #endif /* SUSPEND_USES_PMU */
1288 unsigned long flags;
1290 if (!via || (pmu_suspended < 1))
1293 spin_lock_irqsave(&pmu_lock, flags);
1295 if (pmu_suspended > 0) {
1296 spin_unlock_irqrestore(&pmu_lock, flags);
1299 adb_int_pending = 1;
1300 #ifdef SUSPEND_USES_PMU
1301 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1302 spin_unlock_irqrestore(&pmu_lock, flags);
1303 while(!req.complete)
1305 #else /* SUSPEND_USES_PMU */
1307 enable_irq(gpio_irq);
1308 out_8(&via[IER], CB1_INT | IER_SET);
1309 spin_unlock_irqrestore(&pmu_lock, flags);
1311 #endif /* SUSPEND_USES_PMU */
1314 /* Interrupt data could be the result data from an ADB cmd */
1316 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1318 unsigned char ints, pirq;
1322 if (drop_interrupts || len < 1) {
1323 adb_int_pending = 0;
1328 /* Get PMU interrupt mask */
1331 /* Record zero interrupts for stats */
1335 /* Hack to deal with ADB autopoll flag */
1336 if (ints & PMU_INT_ADB)
1337 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1342 if (i > pmu_irq_stats[10])
1343 pmu_irq_stats[10] = i;
1347 for (pirq = 0; pirq < 8; pirq++)
1348 if (ints & (1 << pirq))
1350 pmu_irq_stats[pirq]++;
1352 ints &= ~(1 << pirq);
1354 /* Note: for some reason, we get an interrupt with len=1,
1355 * data[0]==0 after each normal ADB interrupt, at least
1356 * on the Pismo. Still investigating... --BenH
1358 if ((1 << pirq) & PMU_INT_ADB) {
1359 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1360 struct adb_request *req = req_awaiting_reply;
1362 printk(KERN_ERR "PMU: extra ADB reply\n");
1365 req_awaiting_reply = NULL;
1369 memcpy(req->reply, data + 1, len - 1);
1370 req->reply_len = len - 1;
1374 if (len == 4 && data[1] == 0x2c) {
1375 extern int xmon_wants_key, xmon_adb_keycode;
1376 if (xmon_wants_key) {
1377 xmon_adb_keycode = data[2];
1383 * XXX On the [23]400 the PMU gives us an up
1384 * event for keycodes 0x74 or 0x75 when the PC
1385 * card eject buttons are released, so we
1386 * ignore those events.
1388 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1389 && data[1] == 0x2c && data[3] == 0xff
1390 && (data[2] & ~1) == 0xf4))
1391 adb_input(data+1, len-1, regs, 1);
1392 #endif /* CONFIG_ADB */
1395 /* Sound/brightness button pressed */
1396 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1397 #ifdef CONFIG_PMAC_BACKLIGHT
1399 pmac_backlight_set_legacy_brightness_pmu(data[1] >> 4);
1402 /* Tick interrupt */
1403 else if ((1 << pirq) & PMU_INT_TICK) {
1404 /* Environement or tick interrupt, query batteries */
1405 if (pmu_battery_count) {
1406 if ((--query_batt_timer) == 0) {
1407 query_battery_state();
1408 query_batt_timer = BATTERY_POLLING_COUNT;
1412 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1413 if (pmu_battery_count)
1414 query_battery_state();
1415 pmu_pass_intr(data, len);
1416 /* len == 6 is probably a bad check. But how do I
1417 * know what PMU versions send what events here? */
1419 via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
1420 via_pmu_event(PMU_EVT_LID, data[1]&1);
1423 pmu_pass_intr(data, len);
1428 static struct adb_request*
1429 pmu_sr_intr(struct pt_regs *regs)
1431 struct adb_request *req;
1434 if (via[B] & TREQ) {
1435 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1436 out_8(&via[IFR], SR_INT);
1439 /* The ack may not yet be low when we get the interrupt */
1440 while ((in_8(&via[B]) & TACK) != 0)
1443 /* if reading grab the byte, and reset the interrupt */
1444 if (pmu_state == reading || pmu_state == reading_intr)
1445 bite = in_8(&via[SR]);
1447 /* reset TREQ and wait for TACK to go high */
1448 out_8(&via[B], in_8(&via[B]) | TREQ);
1451 switch (pmu_state) {
1455 data_len = req->nbytes - 1;
1456 send_byte(data_len);
1459 if (data_index <= data_len) {
1460 send_byte(req->data[data_index++]);
1464 data_len = pmu_data_len[req->data[0]][1];
1465 if (data_len == 0) {
1467 current_req = req->next;
1468 if (req->reply_expected)
1469 req_awaiting_reply = req;
1473 pmu_state = reading;
1475 reply_ptr = req->reply + req->reply_len;
1483 pmu_state = reading_intr;
1484 reply_ptr = interrupt_data[int_data_last];
1486 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1487 enable_irq(gpio_irq);
1488 gpio_irq_enabled = 1;
1494 if (data_len == -1) {
1497 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1498 } else if (data_index < 32) {
1499 reply_ptr[data_index++] = bite;
1501 if (data_index < data_len) {
1506 if (pmu_state == reading_intr) {
1508 int_data_state[int_data_last] = int_data_ready;
1509 interrupt_data_len[int_data_last] = data_len;
1513 * For PMU sleep and freq change requests, we lock the
1514 * PMU until it's explicitely unlocked. This avoids any
1515 * spurrious event polling getting in
1517 current_req = req->next;
1518 req->reply_len += data_index;
1519 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1528 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1535 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1537 unsigned long flags;
1541 struct adb_request *req = NULL;
1544 /* This is a bit brutal, we can probably do better */
1545 spin_lock_irqsave(&pmu_lock, flags);
1549 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1553 if (++nloop > 1000) {
1554 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1555 "intr=%x, ier=%x pmu_state=%d\n",
1556 intr, in_8(&via[IER]), pmu_state);
1559 out_8(&via[IFR], intr);
1560 if (intr & CB1_INT) {
1561 adb_int_pending = 1;
1564 if (intr & SR_INT) {
1565 req = pmu_sr_intr(regs);
1572 if (pmu_state == idle) {
1573 if (adb_int_pending) {
1574 if (int_data_state[0] == int_data_empty)
1576 else if (int_data_state[1] == int_data_empty)
1581 int_data_state[int_data_last] = int_data_fill;
1582 /* Sounds safer to make sure ACK is high before writing.
1583 * This helped kill a problem with ADB and some iBooks
1586 send_byte(PMU_INT_ACK);
1587 adb_int_pending = 0;
1588 } else if (current_req)
1592 /* Mark the oldest buffer for flushing */
1593 if (int_data_state[!int_data_last] == int_data_ready) {
1594 int_data_state[!int_data_last] = int_data_flush;
1595 int_data = !int_data_last;
1596 } else if (int_data_state[int_data_last] == int_data_ready) {
1597 int_data_state[int_data_last] = int_data_flush;
1598 int_data = int_data_last;
1601 spin_unlock_irqrestore(&pmu_lock, flags);
1603 /* Deal with completed PMU requests outside of the lock */
1609 /* Deal with interrupt datas outside of the lock */
1610 if (int_data >= 0) {
1611 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1612 spin_lock_irqsave(&pmu_lock, flags);
1614 int_data_state[int_data] = int_data_empty;
1619 return IRQ_RETVAL(handled);
1625 unsigned long flags;
1627 spin_lock_irqsave(&pmu_lock, flags);
1628 if (pmu_state == locked)
1630 adb_int_pending = 1;
1631 spin_unlock_irqrestore(&pmu_lock, flags);
1636 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1638 unsigned long flags;
1640 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1641 spin_lock_irqsave(&pmu_lock, flags);
1642 if (gpio_irq_enabled > 0) {
1643 disable_irq_nosync(gpio_irq);
1644 gpio_irq_enabled = 0;
1647 adb_int_pending = 1;
1648 spin_unlock_irqrestore(&pmu_lock, flags);
1649 via_pmu_interrupt(0, NULL, NULL);
1656 pmu_enable_irled(int on)
1658 struct adb_request req;
1662 if (pmu_kind == PMU_KEYLARGO_BASED)
1665 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1666 (on ? PMU_POW_ON : PMU_POW_OFF));
1667 pmu_wait_complete(&req);
1673 struct adb_request req;
1678 local_irq_disable();
1680 drop_interrupts = 1;
1682 if (pmu_kind != PMU_KEYLARGO_BASED) {
1683 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1685 while(!req.complete)
1689 pmu_request(&req, NULL, 1, PMU_RESET);
1690 pmu_wait_complete(&req);
1698 struct adb_request req;
1703 local_irq_disable();
1705 drop_interrupts = 1;
1707 if (pmu_kind != PMU_KEYLARGO_BASED) {
1708 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1710 pmu_wait_complete(&req);
1712 /* Disable server mode on shutdown or we'll just
1715 pmu_set_server_mode(0);
1718 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1719 'M', 'A', 'T', 'T');
1720 pmu_wait_complete(&req);
1733 static LIST_HEAD(sleep_notifiers);
1736 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1738 struct list_head *list;
1739 struct pmu_sleep_notifier *notifier;
1741 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1742 list = list->next) {
1743 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1744 if (n->priority > notifier->priority)
1747 __list_add(&n->list, list->prev, list);
1750 EXPORT_SYMBOL(pmu_register_sleep_notifier);
1753 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1755 if (n->list.next == 0)
1758 n->list.next = NULL;
1761 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
1762 #endif /* CONFIG_PM */
1764 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1766 /* Sleep is broadcast last-to-first */
1768 broadcast_sleep(int when, int fallback)
1770 int ret = PBOOK_SLEEP_OK;
1771 struct list_head *list;
1772 struct pmu_sleep_notifier *notifier;
1774 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1775 list = list->prev) {
1776 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1777 ret = notifier->notifier_call(notifier, when);
1778 if (ret != PBOOK_SLEEP_OK) {
1779 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
1780 when, notifier, notifier->notifier_call);
1781 for (; list != &sleep_notifiers; list = list->next) {
1782 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1783 notifier->notifier_call(notifier, fallback);
1791 /* Wake is broadcast first-to-last */
1793 broadcast_wake(void)
1795 int ret = PBOOK_SLEEP_OK;
1796 struct list_head *list;
1797 struct pmu_sleep_notifier *notifier;
1799 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1800 list = list->next) {
1801 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1802 notifier->notifier_call(notifier, PBOOK_WAKE);
1808 * This struct is used to store config register values for
1809 * PCI devices which may get powered off when we sleep.
1811 static struct pci_save {
1812 #ifndef HACKED_PCI_SAVE
1821 static int pbook_npci_saves;
1824 pbook_alloc_pci_save(void)
1827 struct pci_dev *pd = NULL;
1830 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1835 pbook_pci_saves = (struct pci_save *)
1836 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
1837 pbook_npci_saves = npci;
1841 pbook_free_pci_save(void)
1843 if (pbook_pci_saves == NULL)
1845 kfree(pbook_pci_saves);
1846 pbook_pci_saves = NULL;
1847 pbook_npci_saves = 0;
1851 pbook_pci_save(void)
1853 struct pci_save *ps = pbook_pci_saves;
1854 struct pci_dev *pd = NULL;
1855 int npci = pbook_npci_saves;
1860 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1863 #ifndef HACKED_PCI_SAVE
1864 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1865 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1866 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1867 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1871 pci_read_config_dword(pd, i<<4, &ps->config[i]);
1877 /* For this to work, we must take care of a few things: If gmac was enabled
1878 * during boot, it will be in the pci dev list. If it's disabled at this point
1879 * (and it will probably be), then you can't access it's config space.
1882 pbook_pci_restore(void)
1885 struct pci_save *ps = pbook_pci_saves - 1;
1886 struct pci_dev *pd = NULL;
1887 int npci = pbook_npci_saves;
1890 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1891 #ifdef HACKED_PCI_SAVE
1897 pci_write_config_dword(pd, i<<4, ps->config[i]);
1898 pci_write_config_dword(pd, 4, ps->config[1]);
1903 if (ps->command == 0)
1905 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1906 if ((ps->command & ~cmd) == 0)
1908 switch (pd->hdr_type) {
1909 case PCI_HEADER_TYPE_NORMAL:
1910 for (j = 0; j < 6; ++j)
1911 pci_write_config_dword(pd,
1912 PCI_BASE_ADDRESS_0 + j*4,
1913 pd->resource[j].start);
1914 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1916 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1918 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1920 pci_write_config_word(pd, PCI_COMMAND, ps->command);
1928 /* N.B. This doesn't work on the 3400 */
1932 struct adb_request req;
1934 memset(&req, 0, sizeof(req));
1936 for (; n > 0; --n) {
1943 req.reply[0] = ADB_RET_OK;
1945 req.reply_expected = 0;
1946 pmu_polled_request(&req);
1954 req.reply[0] = ADB_RET_OK;
1956 req.reply_expected = 0;
1957 pmu_polled_request(&req);
1965 * Put the powerbook to sleep.
1968 static u32 save_via[8];
1971 save_via_state(void)
1973 save_via[0] = in_8(&via[ANH]);
1974 save_via[1] = in_8(&via[DIRA]);
1975 save_via[2] = in_8(&via[B]);
1976 save_via[3] = in_8(&via[DIRB]);
1977 save_via[4] = in_8(&via[PCR]);
1978 save_via[5] = in_8(&via[ACR]);
1979 save_via[6] = in_8(&via[T1CL]);
1980 save_via[7] = in_8(&via[T1CH]);
1983 restore_via_state(void)
1985 out_8(&via[ANH], save_via[0]);
1986 out_8(&via[DIRA], save_via[1]);
1987 out_8(&via[B], save_via[2]);
1988 out_8(&via[DIRB], save_via[3]);
1989 out_8(&via[PCR], save_via[4]);
1990 out_8(&via[ACR], save_via[5]);
1991 out_8(&via[T1CL], save_via[6]);
1992 out_8(&via[T1CH], save_via[7]);
1993 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
1994 out_8(&via[IFR], 0x7f); /* clear IFR */
1995 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
1998 extern void pmu_backlight_set_sleep(int sleep);
2001 pmac_suspend_devices(void)
2005 pm_prepare_console();
2007 /* Notify old-style device drivers & userland */
2008 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2009 if (ret != PBOOK_SLEEP_OK) {
2010 printk(KERN_ERR "Sleep rejected by drivers\n");
2014 /* Sync the disks. */
2015 /* XXX It would be nice to have some way to ensure that
2016 * nobody is dirtying any new buffers while we wait. That
2017 * could be achieved using the refrigerator for processes
2022 /* Sleep can fail now. May not be very robust but useful for debugging */
2023 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2024 if (ret != PBOOK_SLEEP_OK) {
2025 printk(KERN_ERR "Driver sleep failed\n");
2029 /* Send suspend call to devices, hold the device core's dpm_sem */
2030 ret = device_suspend(PMSG_SUSPEND);
2033 printk(KERN_ERR "Driver sleep failed\n");
2037 #ifdef CONFIG_PMAC_BACKLIGHT
2038 /* Tell backlight code not to muck around with the chip anymore */
2039 pmu_backlight_set_sleep(1);
2042 /* Call platform functions marked "on sleep" */
2043 pmac_pfunc_i2c_suspend();
2044 pmac_pfunc_base_suspend();
2046 /* Stop preemption */
2049 /* Make sure the decrementer won't interrupt us */
2050 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2051 /* Make sure any pending DEC interrupt occurring while we did
2052 * the above didn't re-enable the DEC */
2054 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2056 /* We can now disable MSR_EE. This code of course works properly only
2057 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2058 * stop the "other" CPUs way before we do all that stuff.
2060 local_irq_disable();
2062 /* Broadcast power down irq
2063 * This isn't that useful in most cases (only directly wired devices can
2064 * use this but still... This will take care of sysdev's as well, so
2065 * we exit from here with local irqs disabled and PIC off.
2067 ret = device_power_down(PMSG_SUSPEND);
2069 wakeup_decrementer();
2074 printk(KERN_ERR "Driver powerdown failed\n");
2078 /* Wait for completion of async requests */
2079 while (!batt_req.complete)
2082 /* Giveup the lazy FPU & vec so we don't have to back them
2083 * up from the low level code
2087 #ifdef CONFIG_ALTIVEC
2088 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2089 enable_kernel_altivec();
2090 #endif /* CONFIG_ALTIVEC */
2096 pmac_wakeup_devices(void)
2100 #ifdef CONFIG_PMAC_BACKLIGHT
2101 /* Tell backlight code it can use the chip again */
2102 pmu_backlight_set_sleep(0);
2105 /* Power back up system devices (including the PIC) */
2108 /* Force a poll of ADB interrupts */
2109 adb_int_pending = 1;
2110 via_pmu_interrupt(0, NULL, NULL);
2112 /* Restart jiffies & scheduling */
2113 wakeup_decrementer();
2115 /* Re-enable local CPU interrupts */
2120 /* Call platform functions marked "on wake" */
2121 pmac_pfunc_base_resume();
2122 pmac_pfunc_i2c_resume();
2124 /* Resume devices */
2127 /* Notify old style drivers */
2130 pm_restore_console();
2135 #define GRACKLE_PM (1<<7)
2136 #define GRACKLE_DOZE (1<<5)
2137 #define GRACKLE_NAP (1<<4)
2138 #define GRACKLE_SLEEP (1<<3)
2140 static int powerbook_sleep_grackle(void)
2142 unsigned long save_l2cr;
2143 unsigned short pmcr1;
2144 struct adb_request req;
2146 struct pci_dev *grackle;
2148 grackle = pci_find_slot(0, 0);
2152 ret = pmac_suspend_devices();
2154 printk(KERN_ERR "Sleep rejected by devices\n");
2158 /* Turn off various things. Darwin does some retry tests here... */
2159 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2160 pmu_wait_complete(&req);
2161 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2162 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2163 pmu_wait_complete(&req);
2165 /* For 750, save backside cache setting and disable it */
2166 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2168 if (!__fake_sleep) {
2169 /* Ask the PMU to put us to sleep */
2170 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2171 pmu_wait_complete(&req);
2174 /* The VIA is supposed not to be restored correctly*/
2176 /* We shut down some HW */
2177 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2179 pci_read_config_word(grackle, 0x70, &pmcr1);
2180 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2181 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2182 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2183 pci_write_config_word(grackle, 0x70, pmcr1);
2185 /* Call low-level ASM sleep handler */
2189 low_sleep_handler();
2191 /* We're awake again, stop grackle PM */
2192 pci_read_config_word(grackle, 0x70, &pmcr1);
2193 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2194 pci_write_config_word(grackle, 0x70, pmcr1);
2196 /* Make sure the PMU is idle */
2197 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2198 restore_via_state();
2200 /* Restore L2 cache */
2201 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2202 _set_L2CR(save_l2cr);
2204 /* Restore userland MMU context */
2205 set_context(current->active_mm->context.id, current->active_mm->pgd);
2207 /* Power things up */
2209 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2210 pmu_wait_complete(&req);
2211 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2212 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2213 pmu_wait_complete(&req);
2214 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2215 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2216 pmu_wait_complete(&req);
2218 pmac_wakeup_devices();
2224 powerbook_sleep_Core99(void)
2226 unsigned long save_l2cr;
2227 unsigned long save_l3cr;
2228 struct adb_request req;
2231 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2232 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2236 if (num_online_cpus() > 1 || cpu_is_offline(0))
2239 ret = pmac_suspend_devices();
2241 printk(KERN_ERR "Sleep rejected by devices\n");
2245 /* Stop environment and ADB interrupts */
2246 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2247 pmu_wait_complete(&req);
2249 /* Tell PMU what events will wake us up */
2250 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2252 pmu_wait_complete(&req);
2253 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2254 0, PMU_PWR_WAKEUP_KEY |
2255 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2256 pmu_wait_complete(&req);
2258 /* Save the state of the L2 and L3 caches */
2259 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2260 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2262 if (!__fake_sleep) {
2263 /* Ask the PMU to put us to sleep */
2264 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2265 pmu_wait_complete(&req);
2268 /* The VIA is supposed not to be restored correctly*/
2271 /* Shut down various ASICs. There's a chance that we can no longer
2272 * talk to the PMU after this, so I moved it to _after_ sending the
2273 * sleep command to it. Still need to be checked.
2275 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2277 /* Call low-level ASM sleep handler */
2281 low_sleep_handler();
2283 /* Restore Apple core ASICs state */
2284 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2287 restore_via_state();
2289 /* tweak LPJ before cpufreq is there */
2290 loops_per_jiffy *= 2;
2293 pmac_call_early_video_resume();
2295 /* Restore L2 cache */
2296 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2297 _set_L2CR(save_l2cr);
2298 /* Restore L3 cache */
2299 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2300 _set_L3CR(save_l3cr);
2302 /* Restore userland MMU context */
2303 set_context(current->active_mm->context.id, current->active_mm->pgd);
2305 /* Tell PMU we are ready */
2307 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2308 pmu_wait_complete(&req);
2309 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2310 pmu_wait_complete(&req);
2312 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2313 loops_per_jiffy /= 2;
2315 pmac_wakeup_devices();
2320 #define PB3400_MEM_CTRL 0xf8000000
2321 #define PB3400_MEM_CTRL_SLEEP 0x70
2324 powerbook_sleep_3400(void)
2329 struct adb_request sleep_req;
2330 void __iomem *mem_ctrl;
2331 unsigned int __iomem *mem_ctrl_sleep;
2333 /* first map in the memory controller registers */
2334 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2335 if (mem_ctrl == NULL) {
2336 printk("powerbook_sleep_3400: ioremap failed\n");
2339 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2341 /* Allocate room for PCI save */
2342 pbook_alloc_pci_save();
2344 ret = pmac_suspend_devices();
2346 pbook_free_pci_save();
2347 printk(KERN_ERR "Sleep rejected by devices\n");
2351 /* Save the state of PCI config space for some slots */
2354 /* Set the memory controller to keep the memory refreshed
2355 while we're asleep */
2356 for (i = 0x403f; i >= 0x4000; --i) {
2357 out_be32(mem_ctrl_sleep, i);
2359 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2365 /* Ask the PMU to put us to sleep */
2366 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2367 while (!sleep_req.complete)
2370 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2372 /* displacement-flush the L2 cache - necessary? */
2373 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2374 i = *(volatile int *)p;
2377 /* Put the CPU into sleep mode */
2378 hid0 = mfspr(SPRN_HID0);
2379 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2380 mtspr(SPRN_HID0, hid0);
2381 mtmsr(mfmsr() | MSR_POW | MSR_EE);
2384 /* OK, we're awake again, start restoring things */
2385 out_be32(mem_ctrl_sleep, 0x3f);
2386 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2387 pbook_pci_restore();
2390 /* wait for the PMU interrupt sequence to complete */
2394 pmac_wakeup_devices();
2395 pbook_free_pci_save();
2401 #endif /* CONFIG_PM && CONFIG_PPC32 */
2404 * Support for /dev/pmu device
2406 #define RB_SIZE 0x10
2407 struct pmu_private {
2408 struct list_head list;
2413 unsigned char data[16];
2415 wait_queue_head_t wait;
2417 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2418 int backlight_locker;
2422 static LIST_HEAD(all_pmu_pvt);
2423 static DEFINE_SPINLOCK(all_pvt_lock);
2426 pmu_pass_intr(unsigned char *data, int len)
2428 struct pmu_private *pp;
2429 struct list_head *list;
2431 unsigned long flags;
2433 if (len > sizeof(pp->rb_buf[0].data))
2434 len = sizeof(pp->rb_buf[0].data);
2435 spin_lock_irqsave(&all_pvt_lock, flags);
2436 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2437 pp = list_entry(list, struct pmu_private, list);
2438 spin_lock(&pp->lock);
2442 if (i != pp->rb_get) {
2443 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2445 memcpy(rp->data, data, len);
2447 wake_up_interruptible(&pp->wait);
2449 spin_unlock(&pp->lock);
2451 spin_unlock_irqrestore(&all_pvt_lock, flags);
2455 pmu_open(struct inode *inode, struct file *file)
2457 struct pmu_private *pp;
2458 unsigned long flags;
2460 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2463 pp->rb_get = pp->rb_put = 0;
2464 spin_lock_init(&pp->lock);
2465 init_waitqueue_head(&pp->wait);
2466 spin_lock_irqsave(&all_pvt_lock, flags);
2467 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2468 pp->backlight_locker = 0;
2470 list_add(&pp->list, &all_pmu_pvt);
2471 spin_unlock_irqrestore(&all_pvt_lock, flags);
2472 file->private_data = pp;
2477 pmu_read(struct file *file, char __user *buf,
2478 size_t count, loff_t *ppos)
2480 struct pmu_private *pp = file->private_data;
2481 DECLARE_WAITQUEUE(wait, current);
2482 unsigned long flags;
2485 if (count < 1 || pp == 0)
2487 if (!access_ok(VERIFY_WRITE, buf, count))
2490 spin_lock_irqsave(&pp->lock, flags);
2491 add_wait_queue(&pp->wait, &wait);
2492 current->state = TASK_INTERRUPTIBLE;
2496 if (pp->rb_get != pp->rb_put) {
2498 struct rb_entry *rp = &pp->rb_buf[i];
2500 spin_unlock_irqrestore(&pp->lock, flags);
2503 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2507 spin_lock_irqsave(&pp->lock, flags);
2512 if (file->f_flags & O_NONBLOCK)
2515 if (signal_pending(current))
2517 spin_unlock_irqrestore(&pp->lock, flags);
2519 spin_lock_irqsave(&pp->lock, flags);
2521 current->state = TASK_RUNNING;
2522 remove_wait_queue(&pp->wait, &wait);
2523 spin_unlock_irqrestore(&pp->lock, flags);
2529 pmu_write(struct file *file, const char __user *buf,
2530 size_t count, loff_t *ppos)
2536 pmu_fpoll(struct file *filp, poll_table *wait)
2538 struct pmu_private *pp = filp->private_data;
2539 unsigned int mask = 0;
2540 unsigned long flags;
2544 poll_wait(filp, &pp->wait, wait);
2545 spin_lock_irqsave(&pp->lock, flags);
2546 if (pp->rb_get != pp->rb_put)
2548 spin_unlock_irqrestore(&pp->lock, flags);
2553 pmu_release(struct inode *inode, struct file *file)
2555 struct pmu_private *pp = file->private_data;
2556 unsigned long flags;
2560 file->private_data = NULL;
2561 spin_lock_irqsave(&all_pvt_lock, flags);
2562 list_del(&pp->list);
2563 spin_unlock_irqrestore(&all_pvt_lock, flags);
2565 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2566 if (pp->backlight_locker)
2567 pmac_backlight_enable();
2577 pmu_ioctl(struct inode * inode, struct file *filp,
2578 u_int cmd, u_long arg)
2580 __u32 __user *argp = (__u32 __user *)arg;
2581 int error = -EINVAL;
2584 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2586 if (!capable(CAP_SYS_ADMIN))
2588 if (sleep_in_progress)
2590 sleep_in_progress = 1;
2592 case PMU_OHARE_BASED:
2593 error = powerbook_sleep_3400();
2595 case PMU_HEATHROW_BASED:
2596 case PMU_PADDINGTON_BASED:
2597 error = powerbook_sleep_grackle();
2599 case PMU_KEYLARGO_BASED:
2600 error = powerbook_sleep_Core99();
2605 sleep_in_progress = 0;
2607 case PMU_IOC_CAN_SLEEP:
2608 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2609 return put_user(0, argp);
2611 return put_user(1, argp);
2612 #endif /* CONFIG_PM && CONFIG_PPC32 */
2614 #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2615 /* Compatibility ioctl's for backlight */
2616 case PMU_IOC_GET_BACKLIGHT:
2620 if (sleep_in_progress)
2623 brightness = pmac_backlight_get_legacy_brightness();
2627 return put_user(brightness, argp);
2630 case PMU_IOC_SET_BACKLIGHT:
2634 if (sleep_in_progress)
2637 error = get_user(brightness, argp);
2641 return pmac_backlight_set_legacy_brightness(brightness);
2643 #ifdef CONFIG_INPUT_ADBHID
2644 case PMU_IOC_GRAB_BACKLIGHT: {
2645 struct pmu_private *pp = filp->private_data;
2647 if (pp->backlight_locker)
2650 pp->backlight_locker = 1;
2651 pmac_backlight_disable();
2655 #endif /* CONFIG_INPUT_ADBHID */
2656 #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
2658 case PMU_IOC_GET_MODEL:
2659 return put_user(pmu_kind, argp);
2660 case PMU_IOC_HAS_ADB:
2661 return put_user(pmu_has_adb, argp);
2666 static struct file_operations pmu_device_fops = {
2672 .release = pmu_release,
2675 static struct miscdevice pmu_device = {
2676 PMU_MINOR, "pmu", &pmu_device_fops
2679 static int pmu_device_init(void)
2683 if (misc_register(&pmu_device) < 0)
2684 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2687 device_initcall(pmu_device_init);
2692 polled_handshake(volatile unsigned char __iomem *via)
2694 via[B] &= ~TREQ; eieio();
2695 while ((via[B] & TACK) != 0)
2697 via[B] |= TREQ; eieio();
2698 while ((via[B] & TACK) == 0)
2703 polled_send_byte(volatile unsigned char __iomem *via, int x)
2705 via[ACR] |= SR_OUT | SR_EXT; eieio();
2706 via[SR] = x; eieio();
2707 polled_handshake(via);
2711 polled_recv_byte(volatile unsigned char __iomem *via)
2715 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2716 x = via[SR]; eieio();
2717 polled_handshake(via);
2718 x = via[SR]; eieio();
2723 pmu_polled_request(struct adb_request *req)
2725 unsigned long flags;
2727 volatile unsigned char __iomem *v = via;
2731 l = pmu_data_len[c][0];
2732 if (l >= 0 && req->nbytes != l + 1)
2735 local_irq_save(flags);
2736 while (pmu_state != idle)
2739 while ((via[B] & TACK) == 0)
2741 polled_send_byte(v, c);
2743 l = req->nbytes - 1;
2744 polled_send_byte(v, l);
2746 for (i = 1; i <= l; ++i)
2747 polled_send_byte(v, req->data[i]);
2749 l = pmu_data_len[c][1];
2751 l = polled_recv_byte(v);
2752 for (i = 0; i < l; ++i)
2753 req->reply[i + req->reply_len] = polled_recv_byte(v);
2758 local_irq_restore(flags);
2761 #endif /* DEBUG_SLEEP */
2764 /* FIXME: This is a temporary set of callbacks to enable us
2765 * to do suspend-to-disk.
2768 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2770 static int pmu_sys_suspended = 0;
2772 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
2774 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
2777 /* Suspend PMU event interrupts */
2780 pmu_sys_suspended = 1;
2784 static int pmu_sys_resume(struct sys_device *sysdev)
2786 struct adb_request req;
2788 if (!pmu_sys_suspended)
2791 /* Tell PMU we are ready */
2792 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2793 pmu_wait_complete(&req);
2795 /* Resume PMU event interrupts */
2798 pmu_sys_suspended = 0;
2803 #endif /* CONFIG_PM && CONFIG_PPC32 */
2805 static struct sysdev_class pmu_sysclass = {
2806 set_kset_name("pmu"),
2809 static struct sys_device device_pmu = {
2811 .cls = &pmu_sysclass,
2814 static struct sysdev_driver driver_pmu = {
2815 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2816 .suspend = &pmu_sys_suspend,
2817 .resume = &pmu_sys_resume,
2818 #endif /* CONFIG_PM && CONFIG_PPC32 */
2821 static int __init init_pmu_sysfs(void)
2825 rc = sysdev_class_register(&pmu_sysclass);
2827 printk(KERN_ERR "Failed registering PMU sys class\n");
2830 rc = sysdev_register(&device_pmu);
2832 printk(KERN_ERR "Failed registering PMU sys device\n");
2835 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2837 printk(KERN_ERR "Failed registering PMU sys driver\n");
2843 subsys_initcall(init_pmu_sysfs);
2845 EXPORT_SYMBOL(pmu_request);
2846 EXPORT_SYMBOL(pmu_queue_request);
2847 EXPORT_SYMBOL(pmu_poll);
2848 EXPORT_SYMBOL(pmu_poll_adb);
2849 EXPORT_SYMBOL(pmu_wait_complete);
2850 EXPORT_SYMBOL(pmu_suspend);
2851 EXPORT_SYMBOL(pmu_resume);
2852 EXPORT_SYMBOL(pmu_unlock);
2853 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2854 EXPORT_SYMBOL(pmu_enable_irled);
2855 EXPORT_SYMBOL(pmu_battery_count);
2856 EXPORT_SYMBOL(pmu_batteries);
2857 EXPORT_SYMBOL(pmu_power_flags);
2858 #endif /* CONFIG_PM && CONFIG_PPC32 */