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/freezer.h>
46 #include <linux/syscalls.h>
47 #include <linux/suspend.h>
48 #include <linux/cpu.h>
50 #include <asm/machdep.h>
52 #include <asm/pgtable.h>
53 #include <asm/system.h>
54 #include <asm/sections.h>
56 #include <asm/pmac_feature.h>
57 #include <asm/pmac_pfunc.h>
58 #include <asm/pmac_low_i2c.h>
59 #include <asm/uaccess.h>
60 #include <asm/mmu_context.h>
61 #include <asm/cputable.h>
63 #include <asm/backlight.h>
65 #include "via-pmu-event.h"
67 /* Some compile options */
68 #undef SUSPEND_USES_PMU
70 #undef HACKED_PCI_SAVE
72 /* Misc minor number allocated for /dev/pmu */
75 /* How many iterations between battery polls */
76 #define BATTERY_POLLING_COUNT 2
78 static volatile unsigned char __iomem *via;
80 /* VIA registers - spaced 0x200 bytes apart */
81 #define RS 0x200 /* skip between registers */
82 #define B 0 /* B-side data */
83 #define A RS /* A-side data */
84 #define DIRB (2*RS) /* B-side direction (1=output) */
85 #define DIRA (3*RS) /* A-side direction (1=output) */
86 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
87 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
88 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
89 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
90 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
91 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
92 #define SR (10*RS) /* Shift register */
93 #define ACR (11*RS) /* Auxiliary control register */
94 #define PCR (12*RS) /* Peripheral control register */
95 #define IFR (13*RS) /* Interrupt flag register */
96 #define IER (14*RS) /* Interrupt enable register */
97 #define ANH (15*RS) /* A-side data, no handshake */
99 /* Bits in B data register: both active low */
100 #define TACK 0x08 /* Transfer acknowledge (input) */
101 #define TREQ 0x10 /* Transfer request (output) */
104 #define SR_CTRL 0x1c /* Shift register control bits */
105 #define SR_EXT 0x0c /* Shift on external clock */
106 #define SR_OUT 0x10 /* Shift out if 1 */
108 /* Bits in IFR and IER */
109 #define IER_SET 0x80 /* set bits in IER */
110 #define IER_CLR 0 /* clear bits in IER */
111 #define SR_INT 0x04 /* Shift register full/empty */
113 #define CB1_INT 0x10 /* transition on CB1 input */
115 static volatile enum pmu_state {
124 static volatile enum int_data_state {
129 } int_data_state[2] = { int_data_empty, int_data_empty };
131 static struct adb_request *current_req;
132 static struct adb_request *last_req;
133 static struct adb_request *req_awaiting_reply;
134 static unsigned char interrupt_data[2][32];
135 static int interrupt_data_len[2];
136 static int int_data_last;
137 static unsigned char *reply_ptr;
138 static int data_index;
140 static volatile int adb_int_pending;
141 static volatile int disable_poll;
142 static struct device_node *vias;
143 static int pmu_kind = PMU_UNKNOWN;
144 static int pmu_fully_inited;
145 static int pmu_has_adb;
146 static struct device_node *gpio_node;
147 static unsigned char __iomem *gpio_reg;
148 static int gpio_irq = NO_IRQ;
149 static int gpio_irq_enabled = -1;
150 static volatile int pmu_suspended;
151 static spinlock_t pmu_lock;
152 static u8 pmu_intr_mask;
153 static int pmu_version;
154 static int drop_interrupts;
155 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
156 static int option_lid_wakeup = 1;
157 #endif /* CONFIG_PM && CONFIG_PPC32 */
158 #if (defined(CONFIG_PM)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT_LEGACY)
159 static int sleep_in_progress;
161 static unsigned long async_req_locks;
162 static unsigned int pmu_irq_stats[11];
164 static struct proc_dir_entry *proc_pmu_root;
165 static struct proc_dir_entry *proc_pmu_info;
166 static struct proc_dir_entry *proc_pmu_irqstats;
167 static struct proc_dir_entry *proc_pmu_options;
168 static int option_server_mode;
170 int pmu_battery_count;
172 unsigned int pmu_power_flags = PMU_PWR_AC_PRESENT;
173 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
174 static int query_batt_timer = BATTERY_POLLING_COUNT;
175 static struct adb_request batt_req;
176 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
180 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
183 static int adb_dev_map;
184 static int pmu_adb_flags;
186 static int pmu_probe(void);
187 static int pmu_init(void);
188 static int pmu_send_request(struct adb_request *req, int sync);
189 static int pmu_adb_autopoll(int devs);
190 static int pmu_adb_reset_bus(void);
191 #endif /* CONFIG_ADB */
193 static int init_pmu(void);
194 static void pmu_start(void);
195 static irqreturn_t via_pmu_interrupt(int irq, void *arg);
196 static irqreturn_t gpio1_interrupt(int irq, void *arg);
197 static int proc_get_info(char *page, char **start, off_t off,
198 int count, int *eof, void *data);
199 static int proc_get_irqstats(char *page, char **start, off_t off,
200 int count, int *eof, void *data);
201 static void pmu_pass_intr(unsigned char *data, int len);
202 static int proc_get_batt(char *page, char **start, off_t off,
203 int count, int *eof, void *data);
204 static int proc_read_options(char *page, char **start, off_t off,
205 int count, int *eof, void *data);
206 static int proc_write_options(struct file *file, const char __user *buffer,
207 unsigned long count, void *data);
210 struct adb_driver via_pmu_driver = {
219 #endif /* CONFIG_ADB */
221 extern void low_sleep_handler(void);
222 extern void enable_kernel_altivec(void);
223 extern void enable_kernel_fp(void);
226 int pmu_polled_request(struct adb_request *req);
227 int pmu_wink(struct adb_request *req);
231 * This table indicates for each PMU opcode:
232 * - the number of data bytes to be sent with the command, or -1
233 * if a length byte should be sent,
234 * - the number of response bytes which the PMU will return, or
235 * -1 if it will send a length byte.
237 static const s8 pmu_data_len[256][2] = {
238 /* 0 1 2 3 4 5 6 7 */
239 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
240 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
241 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
242 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
243 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
244 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
245 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
246 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
247 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
248 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
249 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
250 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
251 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
253 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
255 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
257 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
259 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
260 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
261 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
263 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
264 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
265 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
267 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
268 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
269 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
270 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
273 static char *pbook_type[] = {
275 "PowerBook 2400/3400/3500(G3)",
276 "PowerBook G3 Series",
281 int __init find_via_pmu(void)
288 vias = of_find_node_by_name(NULL, "via-pmu");
292 reg = of_get_property(vias, "reg", NULL);
294 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
297 taddr = of_translate_address(vias, reg);
298 if (taddr == OF_BAD_ADDR) {
299 printk(KERN_ERR "via-pmu: Can't translate address !\n");
303 spin_lock_init(&pmu_lock);
307 pmu_intr_mask = PMU_INT_PCEJECT |
312 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
313 || device_is_compatible(vias->parent, "ohare")))
314 pmu_kind = PMU_OHARE_BASED;
315 else if (device_is_compatible(vias->parent, "paddington"))
316 pmu_kind = PMU_PADDINGTON_BASED;
317 else if (device_is_compatible(vias->parent, "heathrow"))
318 pmu_kind = PMU_HEATHROW_BASED;
319 else if (device_is_compatible(vias->parent, "Keylargo")
320 || device_is_compatible(vias->parent, "K2-Keylargo")) {
321 struct device_node *gpiop;
322 struct device_node *adbp;
323 u64 gaddr = OF_BAD_ADDR;
325 pmu_kind = PMU_KEYLARGO_BASED;
326 adbp = of_find_node_by_type(NULL, "adb");
327 pmu_has_adb = (adbp != NULL);
329 pmu_intr_mask = PMU_INT_PCEJECT |
335 gpiop = of_find_node_by_name(NULL, "gpio");
337 reg = of_get_property(gpiop, "reg", NULL);
339 gaddr = of_translate_address(gpiop, reg);
340 if (gaddr != OF_BAD_ADDR)
341 gpio_reg = ioremap(gaddr, 0x10);
343 if (gpio_reg == NULL) {
344 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
348 pmu_kind = PMU_UNKNOWN;
350 via = ioremap(taddr, 0x2000);
352 printk(KERN_ERR "via-pmu: Can't map address !\n");
356 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
357 out_8(&via[IFR], 0x7f); /* clear IFR */
366 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
367 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
369 sys_ctrler = SYS_CTRLER_PMU;
382 static int pmu_probe(void)
384 return vias == NULL? -ENODEV: 0;
387 static int __init pmu_init(void)
393 #endif /* CONFIG_ADB */
396 * We can't wait until pmu_init gets called, that happens too late.
397 * It happens after IDE and SCSI initialization, which can take a few
398 * seconds, and by that time the PMU could have given up on us and
400 * Thus this is called with arch_initcall rather than device_initcall.
402 static int __init via_pmu_start(void)
409 batt_req.complete = 1;
411 irq = irq_of_parse_and_map(vias, 0);
413 printk(KERN_ERR "via-pmu: can't map interruptn");
416 if (request_irq(irq, via_pmu_interrupt, 0, "VIA-PMU", (void *)0)) {
417 printk(KERN_ERR "via-pmu: can't request irq %d\n", irq);
421 if (pmu_kind == PMU_KEYLARGO_BASED) {
422 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
423 if (gpio_node == NULL)
424 gpio_node = of_find_node_by_name(NULL,
427 gpio_irq = irq_of_parse_and_map(gpio_node, 0);
429 if (gpio_irq != NO_IRQ) {
430 if (request_irq(gpio_irq, gpio1_interrupt, 0,
431 "GPIO1 ADB", (void *)0))
432 printk(KERN_ERR "pmu: can't get irq %d"
433 " (GPIO1)\n", gpio_irq);
435 gpio_irq_enabled = 1;
439 /* Enable interrupts */
440 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
442 pmu_fully_inited = 1;
444 /* Make sure PMU settle down before continuing. This is _very_ important
445 * since the IDE probe may shut interrupts down for quite a bit of time. If
446 * a PMU communication is pending while this happens, the PMU may timeout
447 * Not that on Core99 machines, the PMU keeps sending us environement
448 * messages, we should find a way to either fix IDE or make it call
449 * pmu_suspend() before masking interrupts. This can also happens while
450 * scolling with some fbdevs.
454 } while (pmu_state != idle);
459 arch_initcall(via_pmu_start);
462 * This has to be done after pci_init, which is a subsys_initcall.
464 static int __init via_pmu_dev_init(void)
469 #ifdef CONFIG_PMAC_BACKLIGHT
470 /* Initialize backlight */
471 pmu_backlight_init();
475 if (machine_is_compatible("AAPL,3400/2400") ||
476 machine_is_compatible("AAPL,3500")) {
477 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
478 NULL, PMAC_MB_INFO_MODEL, 0);
479 pmu_battery_count = 1;
480 if (mb == PMAC_TYPE_COMET)
481 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
483 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
484 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
485 machine_is_compatible("PowerBook1,1")) {
486 pmu_battery_count = 2;
487 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
488 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
490 struct device_node* prim = find_devices("power-mgt");
491 const u32 *prim_info = NULL;
493 prim_info = of_get_property(prim, "prim-info", NULL);
495 /* Other stuffs here yet unknown */
496 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
497 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
498 if (pmu_battery_count > 1)
499 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
502 #endif /* CONFIG_PPC32 */
504 /* Create /proc/pmu */
505 proc_pmu_root = proc_mkdir("pmu", NULL);
509 for (i=0; i<pmu_battery_count; i++) {
511 sprintf(title, "battery_%ld", i);
512 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
513 proc_get_batt, (void *)i);
516 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
517 proc_get_info, NULL);
518 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
519 proc_get_irqstats, NULL);
520 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
521 if (proc_pmu_options) {
522 proc_pmu_options->read_proc = proc_read_options;
523 proc_pmu_options->write_proc = proc_write_options;
529 device_initcall(via_pmu_dev_init);
535 struct adb_request req;
537 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
538 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
540 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
542 while (!req.complete) {
544 printk(KERN_ERR "init_pmu: no response from PMU\n");
551 /* ack all pending interrupts */
553 interrupt_data[0][0] = 1;
554 while (interrupt_data[0][0] || pmu_state != idle) {
556 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
559 if (pmu_state == idle)
561 via_pmu_interrupt(0, NULL);
565 /* Tell PMU we are ready. */
566 if (pmu_kind == PMU_KEYLARGO_BASED) {
567 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
568 while (!req.complete)
572 /* Read PMU version */
573 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
574 pmu_wait_complete(&req);
575 if (req.reply_len > 0)
576 pmu_version = req.reply[0];
578 /* Read server mode setting */
579 if (pmu_kind == PMU_KEYLARGO_BASED) {
580 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
581 PMU_PWR_GET_POWERUP_EVENTS);
582 pmu_wait_complete(&req);
583 if (req.reply_len == 2) {
584 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
585 option_server_mode = 1;
586 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
587 option_server_mode ? "enabled" : "disabled");
599 static void pmu_set_server_mode(int server_mode)
601 struct adb_request req;
603 if (pmu_kind != PMU_KEYLARGO_BASED)
606 option_server_mode = server_mode;
607 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
608 pmu_wait_complete(&req);
609 if (req.reply_len < 2)
612 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
613 PMU_PWR_SET_POWERUP_EVENTS,
614 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
616 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
617 PMU_PWR_CLR_POWERUP_EVENTS,
618 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
619 pmu_wait_complete(&req);
622 /* This new version of the code for 2400/3400/3500 powerbooks
623 * is inspired from the implementation in gkrellm-pmu
626 done_battery_state_ohare(struct adb_request* req)
630 * 0x01 : AC indicator
632 * 0x04 : battery exist
635 * 0x20 : full charged
636 * 0x40 : pcharge reset
637 * 0x80 : battery exist
639 * [1][2] : battery voltage
640 * [3] : CPU temperature
641 * [4] : battery temperature
646 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
647 long pcharge, charge, vb, vmax, lmax;
648 long vmax_charging, vmax_charged;
649 long amperage, voltage, time, max;
650 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
651 NULL, PMAC_MB_INFO_MODEL, 0);
653 if (req->reply[0] & 0x01)
654 pmu_power_flags |= PMU_PWR_AC_PRESENT;
656 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
658 if (mb == PMAC_TYPE_COMET) {
669 /* If battery installed */
670 if (req->reply[0] & 0x04) {
671 bat_flags |= PMU_BATT_PRESENT;
672 if (req->reply[0] & 0x02)
673 bat_flags |= PMU_BATT_CHARGING;
674 vb = (req->reply[1] << 8) | req->reply[2];
675 voltage = (vb * 265 + 72665) / 10;
676 amperage = req->reply[5];
677 if ((req->reply[0] & 0x01) == 0) {
679 vb += ((amperage - 200) * 15)/100;
680 } else if (req->reply[0] & 0x02) {
681 vb = (vb * 97) / 100;
682 vmax = vmax_charging;
684 charge = (100 * vb) / vmax;
685 if (req->reply[0] & 0x40) {
686 pcharge = (req->reply[6] << 8) + req->reply[7];
690 pcharge = 100 - pcharge / lmax;
691 if (pcharge < charge)
695 time = (charge * 16440) / amperage;
699 amperage = -amperage;
701 charge = max = amperage = voltage = time = 0;
703 pmu_batteries[pmu_cur_battery].flags = bat_flags;
704 pmu_batteries[pmu_cur_battery].charge = charge;
705 pmu_batteries[pmu_cur_battery].max_charge = max;
706 pmu_batteries[pmu_cur_battery].amperage = amperage;
707 pmu_batteries[pmu_cur_battery].voltage = voltage;
708 pmu_batteries[pmu_cur_battery].time_remaining = time;
710 clear_bit(0, &async_req_locks);
714 done_battery_state_smart(struct adb_request* req)
717 * [0] : format of this structure (known: 3,4,5)
730 * [4][5] : max charge
735 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
737 unsigned int capa, max, voltage;
739 if (req->reply[1] & 0x01)
740 pmu_power_flags |= PMU_PWR_AC_PRESENT;
742 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
745 capa = max = amperage = voltage = 0;
747 if (req->reply[1] & 0x04) {
748 bat_flags |= PMU_BATT_PRESENT;
749 switch(req->reply[0]) {
751 case 4: capa = req->reply[2];
753 amperage = *((signed char *)&req->reply[4]);
754 voltage = req->reply[5];
756 case 5: capa = (req->reply[2] << 8) | req->reply[3];
757 max = (req->reply[4] << 8) | req->reply[5];
758 amperage = *((signed short *)&req->reply[6]);
759 voltage = (req->reply[8] << 8) | req->reply[9];
762 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
763 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
768 if ((req->reply[1] & 0x01) && (amperage > 0))
769 bat_flags |= PMU_BATT_CHARGING;
771 pmu_batteries[pmu_cur_battery].flags = bat_flags;
772 pmu_batteries[pmu_cur_battery].charge = capa;
773 pmu_batteries[pmu_cur_battery].max_charge = max;
774 pmu_batteries[pmu_cur_battery].amperage = amperage;
775 pmu_batteries[pmu_cur_battery].voltage = voltage;
777 if ((req->reply[1] & 0x01) && (amperage > 0))
778 pmu_batteries[pmu_cur_battery].time_remaining
779 = ((max-capa) * 3600) / amperage;
781 pmu_batteries[pmu_cur_battery].time_remaining
782 = (capa * 3600) / (-amperage);
784 pmu_batteries[pmu_cur_battery].time_remaining = 0;
786 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
788 clear_bit(0, &async_req_locks);
792 query_battery_state(void)
794 if (test_and_set_bit(0, &async_req_locks))
796 if (pmu_kind == PMU_OHARE_BASED)
797 pmu_request(&batt_req, done_battery_state_ohare,
798 1, PMU_BATTERY_STATE);
800 pmu_request(&batt_req, done_battery_state_smart,
801 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
805 proc_get_info(char *page, char **start, off_t off,
806 int count, int *eof, void *data)
810 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
811 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
812 p += sprintf(p, "AC Power : %d\n",
813 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
814 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
820 proc_get_irqstats(char *page, char **start, off_t off,
821 int count, int *eof, void *data)
825 static const char *irq_names[] = {
826 "Total CB1 triggered events",
827 "Total GPIO1 triggered events",
828 "PC-Card eject button",
829 "Sound/Brightness button",
831 "Battery state change",
832 "Environment interrupt",
834 "Ghost interrupt (zero len)",
835 "Empty interrupt (empty mask)",
839 for (i=0; i<11; i++) {
840 p += sprintf(p, " %2u: %10u (%s)\n",
841 i, pmu_irq_stats[i], irq_names[i]);
847 proc_get_batt(char *page, char **start, off_t off,
848 int count, int *eof, void *data)
850 long batnum = (long)data;
853 p += sprintf(p, "\n");
854 p += sprintf(p, "flags : %08x\n",
855 pmu_batteries[batnum].flags);
856 p += sprintf(p, "charge : %d\n",
857 pmu_batteries[batnum].charge);
858 p += sprintf(p, "max_charge : %d\n",
859 pmu_batteries[batnum].max_charge);
860 p += sprintf(p, "current : %d\n",
861 pmu_batteries[batnum].amperage);
862 p += sprintf(p, "voltage : %d\n",
863 pmu_batteries[batnum].voltage);
864 p += sprintf(p, "time rem. : %d\n",
865 pmu_batteries[batnum].time_remaining);
871 proc_read_options(char *page, char **start, off_t off,
872 int count, int *eof, void *data)
876 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
877 if (pmu_kind == PMU_KEYLARGO_BASED &&
878 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
879 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
881 if (pmu_kind == PMU_KEYLARGO_BASED)
882 p += sprintf(p, "server_mode=%d\n", option_server_mode);
888 proc_write_options(struct file *file, const char __user *buffer,
889 unsigned long count, void *data)
893 unsigned long fcount = count;
899 if (copy_from_user(tmp, buffer, count))
907 while(*val && (*val != '=')) {
917 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
918 if (pmu_kind == PMU_KEYLARGO_BASED &&
919 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
920 if (!strcmp(label, "lid_wakeup"))
921 option_lid_wakeup = ((*val) == '1');
923 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
925 new_value = ((*val) == '1');
926 if (new_value != option_server_mode)
927 pmu_set_server_mode(new_value);
933 /* Send an ADB command */
935 pmu_send_request(struct adb_request *req, int sync)
939 if ((vias == NULL) || (!pmu_fully_inited)) {
946 switch (req->data[0]) {
948 for (i = 0; i < req->nbytes - 1; ++i)
949 req->data[i] = req->data[i+1];
951 if (pmu_data_len[req->data[0]][1] != 0) {
952 req->reply[0] = ADB_RET_OK;
956 ret = pmu_queue_request(req);
959 switch (req->data[1]) {
961 if (req->nbytes != 2)
963 req->data[0] = PMU_READ_RTC;
966 req->reply[0] = CUDA_PACKET;
968 req->reply[2] = CUDA_GET_TIME;
969 ret = pmu_queue_request(req);
972 if (req->nbytes != 6)
974 req->data[0] = PMU_SET_RTC;
976 for (i = 1; i <= 4; ++i)
977 req->data[i] = req->data[i+1];
979 req->reply[0] = CUDA_PACKET;
981 req->reply[2] = CUDA_SET_TIME;
982 ret = pmu_queue_request(req);
989 for (i = req->nbytes - 1; i > 1; --i)
990 req->data[i+2] = req->data[i];
991 req->data[3] = req->nbytes - 2;
992 req->data[2] = pmu_adb_flags;
993 /*req->data[1] = req->data[1];*/
994 req->data[0] = PMU_ADB_CMD;
996 req->reply_expected = 1;
998 ret = pmu_queue_request(req);
1007 while (!req->complete)
1013 /* Enable/disable autopolling */
1015 pmu_adb_autopoll(int devs)
1017 struct adb_request req;
1019 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1024 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1025 adb_dev_map >> 8, adb_dev_map);
1028 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1031 while (!req.complete)
1036 /* Reset the ADB bus */
1038 pmu_adb_reset_bus(void)
1040 struct adb_request req;
1041 int save_autopoll = adb_dev_map;
1043 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1046 /* anyone got a better idea?? */
1047 pmu_adb_autopoll(0);
1051 req.data[0] = PMU_ADB_CMD;
1053 req.data[2] = ADB_BUSRESET;
1057 req.reply_expected = 1;
1058 if (pmu_queue_request(&req) != 0) {
1059 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1062 pmu_wait_complete(&req);
1064 if (save_autopoll != 0)
1065 pmu_adb_autopoll(save_autopoll);
1069 #endif /* CONFIG_ADB */
1071 /* Construct and send a pmu request */
1073 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1082 if (nbytes < 0 || nbytes > 32) {
1083 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1087 req->nbytes = nbytes;
1089 va_start(list, nbytes);
1090 for (i = 0; i < nbytes; ++i)
1091 req->data[i] = va_arg(list, int);
1094 req->reply_expected = 0;
1095 return pmu_queue_request(req);
1099 pmu_queue_request(struct adb_request *req)
1101 unsigned long flags;
1108 if (req->nbytes <= 0) {
1112 nsend = pmu_data_len[req->data[0]][0];
1113 if (nsend >= 0 && req->nbytes != nsend + 1) {
1122 spin_lock_irqsave(&pmu_lock, flags);
1123 if (current_req != 0) {
1124 last_req->next = req;
1129 if (pmu_state == idle)
1132 spin_unlock_irqrestore(&pmu_lock, flags);
1140 /* Sightly increased the delay, I had one occurrence of the message
1144 while ((in_8(&via[B]) & TACK) == 0) {
1145 if (--timeout < 0) {
1146 printk(KERN_ERR "PMU not responding (!ack)\n");
1153 /* New PMU seems to be very sensitive to those timings, so we make sure
1154 * PCI is flushed immediately */
1158 volatile unsigned char __iomem *v = via;
1160 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1162 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1169 volatile unsigned char __iomem *v = via;
1171 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1172 in_8(&v[SR]); /* resets SR */
1173 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1178 pmu_done(struct adb_request *req)
1180 void (*done)(struct adb_request *) = req->done;
1183 /* Here, we assume that if the request has a done member, the
1184 * struct request will survive to setting req->complete to 1
1193 struct adb_request *req;
1195 /* assert pmu_state == idle */
1196 /* get the packet to send */
1198 if (req == 0 || pmu_state != idle
1199 || (/*req->reply_expected && */req_awaiting_reply))
1202 pmu_state = sending;
1204 data_len = pmu_data_len[req->data[0]][0];
1206 /* Sounds safer to make sure ACK is high before writing. This helped
1207 * kill a problem with ADB and some iBooks
1210 /* set the shift register to shift out and send a byte */
1211 send_byte(req->data[0]);
1221 via_pmu_interrupt(0, NULL);
1231 /* Kicks ADB read when PMU is suspended */
1232 adb_int_pending = 1;
1234 via_pmu_interrupt(0, NULL);
1235 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1236 || req_awaiting_reply));
1240 pmu_wait_complete(struct adb_request *req)
1244 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1245 via_pmu_interrupt(0, NULL);
1248 /* This function loops until the PMU is idle and prevents it from
1249 * anwsering to ADB interrupts. pmu_request can still be called.
1250 * This is done to avoid spurrious shutdowns when we know we'll have
1251 * interrupts switched off for a long time
1256 unsigned long flags;
1257 #ifdef SUSPEND_USES_PMU
1258 struct adb_request *req;
1263 spin_lock_irqsave(&pmu_lock, flags);
1265 if (pmu_suspended > 1) {
1266 spin_unlock_irqrestore(&pmu_lock, flags);
1271 spin_unlock_irqrestore(&pmu_lock, flags);
1272 if (req_awaiting_reply)
1273 adb_int_pending = 1;
1274 via_pmu_interrupt(0, NULL);
1275 spin_lock_irqsave(&pmu_lock, flags);
1276 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1277 #ifdef SUSPEND_USES_PMU
1278 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1279 spin_unlock_irqrestore(&pmu_lock, flags);
1280 while(!req.complete)
1282 #else /* SUSPEND_USES_PMU */
1284 disable_irq_nosync(gpio_irq);
1285 out_8(&via[IER], CB1_INT | IER_CLR);
1286 spin_unlock_irqrestore(&pmu_lock, flags);
1287 #endif /* SUSPEND_USES_PMU */
1296 unsigned long flags;
1298 if (!via || (pmu_suspended < 1))
1301 spin_lock_irqsave(&pmu_lock, flags);
1303 if (pmu_suspended > 0) {
1304 spin_unlock_irqrestore(&pmu_lock, flags);
1307 adb_int_pending = 1;
1308 #ifdef SUSPEND_USES_PMU
1309 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1310 spin_unlock_irqrestore(&pmu_lock, flags);
1311 while(!req.complete)
1313 #else /* SUSPEND_USES_PMU */
1315 enable_irq(gpio_irq);
1316 out_8(&via[IER], CB1_INT | IER_SET);
1317 spin_unlock_irqrestore(&pmu_lock, flags);
1319 #endif /* SUSPEND_USES_PMU */
1322 /* Interrupt data could be the result data from an ADB cmd */
1324 pmu_handle_data(unsigned char *data, int len)
1326 unsigned char ints, pirq;
1330 if (drop_interrupts || len < 1) {
1331 adb_int_pending = 0;
1336 /* Get PMU interrupt mask */
1339 /* Record zero interrupts for stats */
1343 /* Hack to deal with ADB autopoll flag */
1344 if (ints & PMU_INT_ADB)
1345 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1350 if (i > pmu_irq_stats[10])
1351 pmu_irq_stats[10] = i;
1355 for (pirq = 0; pirq < 8; pirq++)
1356 if (ints & (1 << pirq))
1358 pmu_irq_stats[pirq]++;
1360 ints &= ~(1 << pirq);
1362 /* Note: for some reason, we get an interrupt with len=1,
1363 * data[0]==0 after each normal ADB interrupt, at least
1364 * on the Pismo. Still investigating... --BenH
1366 if ((1 << pirq) & PMU_INT_ADB) {
1367 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1368 struct adb_request *req = req_awaiting_reply;
1370 printk(KERN_ERR "PMU: extra ADB reply\n");
1373 req_awaiting_reply = NULL;
1377 memcpy(req->reply, data + 1, len - 1);
1378 req->reply_len = len - 1;
1382 if (len == 4 && data[1] == 0x2c) {
1383 extern int xmon_wants_key, xmon_adb_keycode;
1384 if (xmon_wants_key) {
1385 xmon_adb_keycode = data[2];
1391 * XXX On the [23]400 the PMU gives us an up
1392 * event for keycodes 0x74 or 0x75 when the PC
1393 * card eject buttons are released, so we
1394 * ignore those events.
1396 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1397 && data[1] == 0x2c && data[3] == 0xff
1398 && (data[2] & ~1) == 0xf4))
1399 adb_input(data+1, len-1, 1);
1400 #endif /* CONFIG_ADB */
1403 /* Sound/brightness button pressed */
1404 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1405 #ifdef CONFIG_PMAC_BACKLIGHT
1407 pmac_backlight_set_legacy_brightness_pmu(data[1] >> 4);
1410 /* Tick interrupt */
1411 else if ((1 << pirq) & PMU_INT_TICK) {
1412 /* Environement or tick interrupt, query batteries */
1413 if (pmu_battery_count) {
1414 if ((--query_batt_timer) == 0) {
1415 query_battery_state();
1416 query_batt_timer = BATTERY_POLLING_COUNT;
1420 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1421 if (pmu_battery_count)
1422 query_battery_state();
1423 pmu_pass_intr(data, len);
1424 /* len == 6 is probably a bad check. But how do I
1425 * know what PMU versions send what events here? */
1427 via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
1428 via_pmu_event(PMU_EVT_LID, data[1]&1);
1431 pmu_pass_intr(data, len);
1436 static struct adb_request*
1439 struct adb_request *req;
1442 if (via[B] & TREQ) {
1443 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1444 out_8(&via[IFR], SR_INT);
1447 /* The ack may not yet be low when we get the interrupt */
1448 while ((in_8(&via[B]) & TACK) != 0)
1451 /* if reading grab the byte, and reset the interrupt */
1452 if (pmu_state == reading || pmu_state == reading_intr)
1453 bite = in_8(&via[SR]);
1455 /* reset TREQ and wait for TACK to go high */
1456 out_8(&via[B], in_8(&via[B]) | TREQ);
1459 switch (pmu_state) {
1463 data_len = req->nbytes - 1;
1464 send_byte(data_len);
1467 if (data_index <= data_len) {
1468 send_byte(req->data[data_index++]);
1472 data_len = pmu_data_len[req->data[0]][1];
1473 if (data_len == 0) {
1475 current_req = req->next;
1476 if (req->reply_expected)
1477 req_awaiting_reply = req;
1481 pmu_state = reading;
1483 reply_ptr = req->reply + req->reply_len;
1491 pmu_state = reading_intr;
1492 reply_ptr = interrupt_data[int_data_last];
1494 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1495 enable_irq(gpio_irq);
1496 gpio_irq_enabled = 1;
1502 if (data_len == -1) {
1505 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1506 } else if (data_index < 32) {
1507 reply_ptr[data_index++] = bite;
1509 if (data_index < data_len) {
1514 if (pmu_state == reading_intr) {
1516 int_data_state[int_data_last] = int_data_ready;
1517 interrupt_data_len[int_data_last] = data_len;
1521 * For PMU sleep and freq change requests, we lock the
1522 * PMU until it's explicitely unlocked. This avoids any
1523 * spurrious event polling getting in
1525 current_req = req->next;
1526 req->reply_len += data_index;
1527 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1536 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1543 via_pmu_interrupt(int irq, void *arg)
1545 unsigned long flags;
1549 struct adb_request *req = NULL;
1552 /* This is a bit brutal, we can probably do better */
1553 spin_lock_irqsave(&pmu_lock, flags);
1557 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1561 if (++nloop > 1000) {
1562 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1563 "intr=%x, ier=%x pmu_state=%d\n",
1564 intr, in_8(&via[IER]), pmu_state);
1567 out_8(&via[IFR], intr);
1568 if (intr & CB1_INT) {
1569 adb_int_pending = 1;
1572 if (intr & SR_INT) {
1573 req = pmu_sr_intr();
1580 if (pmu_state == idle) {
1581 if (adb_int_pending) {
1582 if (int_data_state[0] == int_data_empty)
1584 else if (int_data_state[1] == int_data_empty)
1589 int_data_state[int_data_last] = int_data_fill;
1590 /* Sounds safer to make sure ACK is high before writing.
1591 * This helped kill a problem with ADB and some iBooks
1594 send_byte(PMU_INT_ACK);
1595 adb_int_pending = 0;
1596 } else if (current_req)
1600 /* Mark the oldest buffer for flushing */
1601 if (int_data_state[!int_data_last] == int_data_ready) {
1602 int_data_state[!int_data_last] = int_data_flush;
1603 int_data = !int_data_last;
1604 } else if (int_data_state[int_data_last] == int_data_ready) {
1605 int_data_state[int_data_last] = int_data_flush;
1606 int_data = int_data_last;
1609 spin_unlock_irqrestore(&pmu_lock, flags);
1611 /* Deal with completed PMU requests outside of the lock */
1617 /* Deal with interrupt datas outside of the lock */
1618 if (int_data >= 0) {
1619 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data]);
1620 spin_lock_irqsave(&pmu_lock, flags);
1622 int_data_state[int_data] = int_data_empty;
1627 return IRQ_RETVAL(handled);
1633 unsigned long flags;
1635 spin_lock_irqsave(&pmu_lock, flags);
1636 if (pmu_state == locked)
1638 adb_int_pending = 1;
1639 spin_unlock_irqrestore(&pmu_lock, flags);
1644 gpio1_interrupt(int irq, void *arg)
1646 unsigned long flags;
1648 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1649 spin_lock_irqsave(&pmu_lock, flags);
1650 if (gpio_irq_enabled > 0) {
1651 disable_irq_nosync(gpio_irq);
1652 gpio_irq_enabled = 0;
1655 adb_int_pending = 1;
1656 spin_unlock_irqrestore(&pmu_lock, flags);
1657 via_pmu_interrupt(0, NULL);
1664 pmu_enable_irled(int on)
1666 struct adb_request req;
1670 if (pmu_kind == PMU_KEYLARGO_BASED)
1673 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1674 (on ? PMU_POW_ON : PMU_POW_OFF));
1675 pmu_wait_complete(&req);
1681 struct adb_request req;
1686 local_irq_disable();
1688 drop_interrupts = 1;
1690 if (pmu_kind != PMU_KEYLARGO_BASED) {
1691 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1693 while(!req.complete)
1697 pmu_request(&req, NULL, 1, PMU_RESET);
1698 pmu_wait_complete(&req);
1706 struct adb_request req;
1711 local_irq_disable();
1713 drop_interrupts = 1;
1715 if (pmu_kind != PMU_KEYLARGO_BASED) {
1716 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1718 pmu_wait_complete(&req);
1720 /* Disable server mode on shutdown or we'll just
1723 pmu_set_server_mode(0);
1726 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1727 'M', 'A', 'T', 'T');
1728 pmu_wait_complete(&req);
1741 static LIST_HEAD(sleep_notifiers);
1744 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1746 struct list_head *list;
1747 struct pmu_sleep_notifier *notifier;
1749 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1750 list = list->next) {
1751 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1752 if (n->priority > notifier->priority)
1755 __list_add(&n->list, list->prev, list);
1758 EXPORT_SYMBOL(pmu_register_sleep_notifier);
1761 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1763 if (n->list.next == 0)
1766 n->list.next = NULL;
1769 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
1770 #endif /* CONFIG_PM */
1772 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1774 /* Sleep is broadcast last-to-first */
1775 static void broadcast_sleep(int when)
1777 struct list_head *list;
1778 struct pmu_sleep_notifier *notifier;
1780 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1781 list = list->prev) {
1782 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1783 notifier->notifier_call(notifier, when);
1787 /* Wake is broadcast first-to-last */
1788 static void broadcast_wake(void)
1790 struct list_head *list;
1791 struct pmu_sleep_notifier *notifier;
1793 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1794 list = list->next) {
1795 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1796 notifier->notifier_call(notifier, PBOOK_WAKE);
1801 * This struct is used to store config register values for
1802 * PCI devices which may get powered off when we sleep.
1804 static struct pci_save {
1805 #ifndef HACKED_PCI_SAVE
1814 static int pbook_npci_saves;
1817 pbook_alloc_pci_save(void)
1820 struct pci_dev *pd = NULL;
1823 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1828 pbook_pci_saves = (struct pci_save *)
1829 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
1830 pbook_npci_saves = npci;
1834 pbook_free_pci_save(void)
1836 if (pbook_pci_saves == NULL)
1838 kfree(pbook_pci_saves);
1839 pbook_pci_saves = NULL;
1840 pbook_npci_saves = 0;
1844 pbook_pci_save(void)
1846 struct pci_save *ps = pbook_pci_saves;
1847 struct pci_dev *pd = NULL;
1848 int npci = pbook_npci_saves;
1853 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1858 #ifndef HACKED_PCI_SAVE
1859 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1860 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1861 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1862 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1866 pci_read_config_dword(pd, i<<4, &ps->config[i]);
1872 /* For this to work, we must take care of a few things: If gmac was enabled
1873 * during boot, it will be in the pci dev list. If it's disabled at this point
1874 * (and it will probably be), then you can't access it's config space.
1877 pbook_pci_restore(void)
1880 struct pci_save *ps = pbook_pci_saves - 1;
1881 struct pci_dev *pd = NULL;
1882 int npci = pbook_npci_saves;
1885 while ((pd = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1886 #ifdef HACKED_PCI_SAVE
1894 pci_write_config_dword(pd, i<<4, ps->config[i]);
1895 pci_write_config_dword(pd, 4, ps->config[1]);
1900 if (ps->command == 0)
1902 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1903 if ((ps->command & ~cmd) == 0)
1905 switch (pd->hdr_type) {
1906 case PCI_HEADER_TYPE_NORMAL:
1907 for (j = 0; j < 6; ++j)
1908 pci_write_config_dword(pd,
1909 PCI_BASE_ADDRESS_0 + j*4,
1910 pd->resource[j].start);
1911 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1913 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1915 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1917 pci_write_config_word(pd, PCI_COMMAND, ps->command);
1925 /* N.B. This doesn't work on the 3400 */
1929 struct adb_request req;
1931 memset(&req, 0, sizeof(req));
1933 for (; n > 0; --n) {
1940 req.reply[0] = ADB_RET_OK;
1942 req.reply_expected = 0;
1943 pmu_polled_request(&req);
1951 req.reply[0] = ADB_RET_OK;
1953 req.reply_expected = 0;
1954 pmu_polled_request(&req);
1962 * Put the powerbook to sleep.
1965 static u32 save_via[8];
1968 save_via_state(void)
1970 save_via[0] = in_8(&via[ANH]);
1971 save_via[1] = in_8(&via[DIRA]);
1972 save_via[2] = in_8(&via[B]);
1973 save_via[3] = in_8(&via[DIRB]);
1974 save_via[4] = in_8(&via[PCR]);
1975 save_via[5] = in_8(&via[ACR]);
1976 save_via[6] = in_8(&via[T1CL]);
1977 save_via[7] = in_8(&via[T1CH]);
1980 restore_via_state(void)
1982 out_8(&via[ANH], save_via[0]);
1983 out_8(&via[DIRA], save_via[1]);
1984 out_8(&via[B], save_via[2]);
1985 out_8(&via[DIRB], save_via[3]);
1986 out_8(&via[PCR], save_via[4]);
1987 out_8(&via[ACR], save_via[5]);
1988 out_8(&via[T1CL], save_via[6]);
1989 out_8(&via[T1CH], save_via[7]);
1990 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
1991 out_8(&via[IFR], 0x7f); /* clear IFR */
1992 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
1995 extern void pmu_backlight_set_sleep(int sleep);
1998 pmac_suspend_devices(void)
2002 pm_prepare_console();
2004 /* Notify old-style device drivers */
2005 broadcast_sleep(PBOOK_SLEEP_REQUEST);
2007 /* Sync the disks. */
2008 /* XXX It would be nice to have some way to ensure that
2009 * nobody is dirtying any new buffers while we wait. That
2010 * could be achieved using the refrigerator for processes
2015 broadcast_sleep(PBOOK_SLEEP_NOW);
2017 /* Send suspend call to devices, hold the device core's dpm_sem */
2018 ret = device_suspend(PMSG_SUSPEND);
2021 printk(KERN_ERR "Driver sleep failed\n");
2025 #ifdef CONFIG_PMAC_BACKLIGHT
2026 /* Tell backlight code not to muck around with the chip anymore */
2027 pmu_backlight_set_sleep(1);
2030 /* Call platform functions marked "on sleep" */
2031 pmac_pfunc_i2c_suspend();
2032 pmac_pfunc_base_suspend();
2034 /* Stop preemption */
2037 /* Make sure the decrementer won't interrupt us */
2038 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2039 /* Make sure any pending DEC interrupt occurring while we did
2040 * the above didn't re-enable the DEC */
2042 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2044 /* We can now disable MSR_EE. This code of course works properly only
2045 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2046 * stop the "other" CPUs way before we do all that stuff.
2048 local_irq_disable();
2050 /* Broadcast power down irq
2051 * This isn't that useful in most cases (only directly wired devices can
2052 * use this but still... This will take care of sysdev's as well, so
2053 * we exit from here with local irqs disabled and PIC off.
2055 ret = device_power_down(PMSG_SUSPEND);
2057 wakeup_decrementer();
2062 printk(KERN_ERR "Driver powerdown failed\n");
2066 /* Wait for completion of async requests */
2067 while (!batt_req.complete)
2070 /* Giveup the lazy FPU & vec so we don't have to back them
2071 * up from the low level code
2075 #ifdef CONFIG_ALTIVEC
2076 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2077 enable_kernel_altivec();
2078 #endif /* CONFIG_ALTIVEC */
2084 pmac_wakeup_devices(void)
2088 #ifdef CONFIG_PMAC_BACKLIGHT
2089 /* Tell backlight code it can use the chip again */
2090 pmu_backlight_set_sleep(0);
2093 /* Power back up system devices (including the PIC) */
2096 /* Force a poll of ADB interrupts */
2097 adb_int_pending = 1;
2098 via_pmu_interrupt(0, NULL);
2100 /* Restart jiffies & scheduling */
2101 wakeup_decrementer();
2103 /* Re-enable local CPU interrupts */
2108 /* Call platform functions marked "on wake" */
2109 pmac_pfunc_base_resume();
2110 pmac_pfunc_i2c_resume();
2112 /* Resume devices */
2115 /* Notify old style drivers */
2118 pm_restore_console();
2123 #define GRACKLE_PM (1<<7)
2124 #define GRACKLE_DOZE (1<<5)
2125 #define GRACKLE_NAP (1<<4)
2126 #define GRACKLE_SLEEP (1<<3)
2128 static int powerbook_sleep_grackle(void)
2130 unsigned long save_l2cr;
2131 unsigned short pmcr1;
2132 struct adb_request req;
2134 struct pci_dev *grackle;
2136 grackle = pci_find_slot(0, 0);
2140 ret = pmac_suspend_devices();
2142 printk(KERN_ERR "Sleep rejected by devices\n");
2146 /* Turn off various things. Darwin does some retry tests here... */
2147 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2148 pmu_wait_complete(&req);
2149 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2150 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2151 pmu_wait_complete(&req);
2153 /* For 750, save backside cache setting and disable it */
2154 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2156 if (!__fake_sleep) {
2157 /* Ask the PMU to put us to sleep */
2158 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2159 pmu_wait_complete(&req);
2162 /* The VIA is supposed not to be restored correctly*/
2164 /* We shut down some HW */
2165 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2167 pci_read_config_word(grackle, 0x70, &pmcr1);
2168 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2169 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2170 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2171 pci_write_config_word(grackle, 0x70, pmcr1);
2173 /* Call low-level ASM sleep handler */
2177 low_sleep_handler();
2179 /* We're awake again, stop grackle PM */
2180 pci_read_config_word(grackle, 0x70, &pmcr1);
2181 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2182 pci_write_config_word(grackle, 0x70, pmcr1);
2184 /* Make sure the PMU is idle */
2185 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2186 restore_via_state();
2188 /* Restore L2 cache */
2189 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2190 _set_L2CR(save_l2cr);
2192 /* Restore userland MMU context */
2193 set_context(current->active_mm->context.id, current->active_mm->pgd);
2195 /* Power things up */
2197 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2198 pmu_wait_complete(&req);
2199 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2200 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2201 pmu_wait_complete(&req);
2202 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2203 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2204 pmu_wait_complete(&req);
2206 pmac_wakeup_devices();
2212 powerbook_sleep_Core99(void)
2214 unsigned long save_l2cr;
2215 unsigned long save_l3cr;
2216 struct adb_request req;
2219 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2220 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2224 if (num_online_cpus() > 1 || cpu_is_offline(0))
2227 ret = pmac_suspend_devices();
2229 printk(KERN_ERR "Sleep rejected by devices\n");
2233 /* Stop environment and ADB interrupts */
2234 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2235 pmu_wait_complete(&req);
2237 /* Tell PMU what events will wake us up */
2238 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2240 pmu_wait_complete(&req);
2241 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2242 0, PMU_PWR_WAKEUP_KEY |
2243 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2244 pmu_wait_complete(&req);
2246 /* Save the state of the L2 and L3 caches */
2247 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2248 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2250 if (!__fake_sleep) {
2251 /* Ask the PMU to put us to sleep */
2252 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2253 pmu_wait_complete(&req);
2256 /* The VIA is supposed not to be restored correctly*/
2259 /* Shut down various ASICs. There's a chance that we can no longer
2260 * talk to the PMU after this, so I moved it to _after_ sending the
2261 * sleep command to it. Still need to be checked.
2263 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2265 /* Call low-level ASM sleep handler */
2269 low_sleep_handler();
2271 /* Restore Apple core ASICs state */
2272 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2275 restore_via_state();
2277 /* tweak LPJ before cpufreq is there */
2278 loops_per_jiffy *= 2;
2281 pmac_call_early_video_resume();
2283 /* Restore L2 cache */
2284 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2285 _set_L2CR(save_l2cr);
2286 /* Restore L3 cache */
2287 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2288 _set_L3CR(save_l3cr);
2290 /* Restore userland MMU context */
2291 set_context(current->active_mm->context.id, current->active_mm->pgd);
2293 /* Tell PMU we are ready */
2295 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2296 pmu_wait_complete(&req);
2297 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2298 pmu_wait_complete(&req);
2300 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2301 loops_per_jiffy /= 2;
2303 pmac_wakeup_devices();
2308 #define PB3400_MEM_CTRL 0xf8000000
2309 #define PB3400_MEM_CTRL_SLEEP 0x70
2312 powerbook_sleep_3400(void)
2317 struct adb_request sleep_req;
2318 void __iomem *mem_ctrl;
2319 unsigned int __iomem *mem_ctrl_sleep;
2321 /* first map in the memory controller registers */
2322 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2323 if (mem_ctrl == NULL) {
2324 printk("powerbook_sleep_3400: ioremap failed\n");
2327 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2329 /* Allocate room for PCI save */
2330 pbook_alloc_pci_save();
2332 ret = pmac_suspend_devices();
2334 pbook_free_pci_save();
2335 printk(KERN_ERR "Sleep rejected by devices\n");
2339 /* Save the state of PCI config space for some slots */
2342 /* Set the memory controller to keep the memory refreshed
2343 while we're asleep */
2344 for (i = 0x403f; i >= 0x4000; --i) {
2345 out_be32(mem_ctrl_sleep, i);
2347 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2353 /* Ask the PMU to put us to sleep */
2354 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2355 while (!sleep_req.complete)
2358 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2360 /* displacement-flush the L2 cache - necessary? */
2361 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2362 i = *(volatile int *)p;
2365 /* Put the CPU into sleep mode */
2366 hid0 = mfspr(SPRN_HID0);
2367 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2368 mtspr(SPRN_HID0, hid0);
2369 mtmsr(mfmsr() | MSR_POW | MSR_EE);
2372 /* OK, we're awake again, start restoring things */
2373 out_be32(mem_ctrl_sleep, 0x3f);
2374 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2375 pbook_pci_restore();
2378 /* wait for the PMU interrupt sequence to complete */
2382 pmac_wakeup_devices();
2383 pbook_free_pci_save();
2389 #endif /* CONFIG_PM && CONFIG_PPC32 */
2392 * Support for /dev/pmu device
2394 #define RB_SIZE 0x10
2395 struct pmu_private {
2396 struct list_head list;
2401 unsigned char data[16];
2403 wait_queue_head_t wait;
2405 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2406 int backlight_locker;
2410 static LIST_HEAD(all_pmu_pvt);
2411 static DEFINE_SPINLOCK(all_pvt_lock);
2414 pmu_pass_intr(unsigned char *data, int len)
2416 struct pmu_private *pp;
2417 struct list_head *list;
2419 unsigned long flags;
2421 if (len > sizeof(pp->rb_buf[0].data))
2422 len = sizeof(pp->rb_buf[0].data);
2423 spin_lock_irqsave(&all_pvt_lock, flags);
2424 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2425 pp = list_entry(list, struct pmu_private, list);
2426 spin_lock(&pp->lock);
2430 if (i != pp->rb_get) {
2431 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2433 memcpy(rp->data, data, len);
2435 wake_up_interruptible(&pp->wait);
2437 spin_unlock(&pp->lock);
2439 spin_unlock_irqrestore(&all_pvt_lock, flags);
2443 pmu_open(struct inode *inode, struct file *file)
2445 struct pmu_private *pp;
2446 unsigned long flags;
2448 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2451 pp->rb_get = pp->rb_put = 0;
2452 spin_lock_init(&pp->lock);
2453 init_waitqueue_head(&pp->wait);
2454 spin_lock_irqsave(&all_pvt_lock, flags);
2455 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2456 pp->backlight_locker = 0;
2458 list_add(&pp->list, &all_pmu_pvt);
2459 spin_unlock_irqrestore(&all_pvt_lock, flags);
2460 file->private_data = pp;
2465 pmu_read(struct file *file, char __user *buf,
2466 size_t count, loff_t *ppos)
2468 struct pmu_private *pp = file->private_data;
2469 DECLARE_WAITQUEUE(wait, current);
2470 unsigned long flags;
2473 if (count < 1 || pp == 0)
2475 if (!access_ok(VERIFY_WRITE, buf, count))
2478 spin_lock_irqsave(&pp->lock, flags);
2479 add_wait_queue(&pp->wait, &wait);
2480 current->state = TASK_INTERRUPTIBLE;
2484 if (pp->rb_get != pp->rb_put) {
2486 struct rb_entry *rp = &pp->rb_buf[i];
2488 spin_unlock_irqrestore(&pp->lock, flags);
2491 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2495 spin_lock_irqsave(&pp->lock, flags);
2500 if (file->f_flags & O_NONBLOCK)
2503 if (signal_pending(current))
2505 spin_unlock_irqrestore(&pp->lock, flags);
2507 spin_lock_irqsave(&pp->lock, flags);
2509 current->state = TASK_RUNNING;
2510 remove_wait_queue(&pp->wait, &wait);
2511 spin_unlock_irqrestore(&pp->lock, flags);
2517 pmu_write(struct file *file, const char __user *buf,
2518 size_t count, loff_t *ppos)
2524 pmu_fpoll(struct file *filp, poll_table *wait)
2526 struct pmu_private *pp = filp->private_data;
2527 unsigned int mask = 0;
2528 unsigned long flags;
2532 poll_wait(filp, &pp->wait, wait);
2533 spin_lock_irqsave(&pp->lock, flags);
2534 if (pp->rb_get != pp->rb_put)
2536 spin_unlock_irqrestore(&pp->lock, flags);
2541 pmu_release(struct inode *inode, struct file *file)
2543 struct pmu_private *pp = file->private_data;
2544 unsigned long flags;
2548 file->private_data = NULL;
2549 spin_lock_irqsave(&all_pvt_lock, flags);
2550 list_del(&pp->list);
2551 spin_unlock_irqrestore(&all_pvt_lock, flags);
2553 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2554 if (pp->backlight_locker)
2555 pmac_backlight_enable();
2565 pmu_ioctl(struct inode * inode, struct file *filp,
2566 u_int cmd, u_long arg)
2568 __u32 __user *argp = (__u32 __user *)arg;
2569 int error = -EINVAL;
2572 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2574 if (!capable(CAP_SYS_ADMIN))
2576 if (sleep_in_progress)
2578 sleep_in_progress = 1;
2580 case PMU_OHARE_BASED:
2581 error = powerbook_sleep_3400();
2583 case PMU_HEATHROW_BASED:
2584 case PMU_PADDINGTON_BASED:
2585 error = powerbook_sleep_grackle();
2587 case PMU_KEYLARGO_BASED:
2588 error = powerbook_sleep_Core99();
2593 sleep_in_progress = 0;
2595 case PMU_IOC_CAN_SLEEP:
2596 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2597 return put_user(0, argp);
2599 return put_user(1, argp);
2600 #endif /* CONFIG_PM && CONFIG_PPC32 */
2602 #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2603 /* Compatibility ioctl's for backlight */
2604 case PMU_IOC_GET_BACKLIGHT:
2608 if (sleep_in_progress)
2611 brightness = pmac_backlight_get_legacy_brightness();
2615 return put_user(brightness, argp);
2618 case PMU_IOC_SET_BACKLIGHT:
2622 if (sleep_in_progress)
2625 error = get_user(brightness, argp);
2629 return pmac_backlight_set_legacy_brightness(brightness);
2631 #ifdef CONFIG_INPUT_ADBHID
2632 case PMU_IOC_GRAB_BACKLIGHT: {
2633 struct pmu_private *pp = filp->private_data;
2635 if (pp->backlight_locker)
2638 pp->backlight_locker = 1;
2639 pmac_backlight_disable();
2643 #endif /* CONFIG_INPUT_ADBHID */
2644 #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
2646 case PMU_IOC_GET_MODEL:
2647 return put_user(pmu_kind, argp);
2648 case PMU_IOC_HAS_ADB:
2649 return put_user(pmu_has_adb, argp);
2654 static const struct file_operations pmu_device_fops = {
2660 .release = pmu_release,
2663 static struct miscdevice pmu_device = {
2664 PMU_MINOR, "pmu", &pmu_device_fops
2667 static int pmu_device_init(void)
2671 if (misc_register(&pmu_device) < 0)
2672 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2675 device_initcall(pmu_device_init);
2680 polled_handshake(volatile unsigned char __iomem *via)
2682 via[B] &= ~TREQ; eieio();
2683 while ((via[B] & TACK) != 0)
2685 via[B] |= TREQ; eieio();
2686 while ((via[B] & TACK) == 0)
2691 polled_send_byte(volatile unsigned char __iomem *via, int x)
2693 via[ACR] |= SR_OUT | SR_EXT; eieio();
2694 via[SR] = x; eieio();
2695 polled_handshake(via);
2699 polled_recv_byte(volatile unsigned char __iomem *via)
2703 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2704 x = via[SR]; eieio();
2705 polled_handshake(via);
2706 x = via[SR]; eieio();
2711 pmu_polled_request(struct adb_request *req)
2713 unsigned long flags;
2715 volatile unsigned char __iomem *v = via;
2719 l = pmu_data_len[c][0];
2720 if (l >= 0 && req->nbytes != l + 1)
2723 local_irq_save(flags);
2724 while (pmu_state != idle)
2727 while ((via[B] & TACK) == 0)
2729 polled_send_byte(v, c);
2731 l = req->nbytes - 1;
2732 polled_send_byte(v, l);
2734 for (i = 1; i <= l; ++i)
2735 polled_send_byte(v, req->data[i]);
2737 l = pmu_data_len[c][1];
2739 l = polled_recv_byte(v);
2740 for (i = 0; i < l; ++i)
2741 req->reply[i + req->reply_len] = polled_recv_byte(v);
2746 local_irq_restore(flags);
2749 #endif /* DEBUG_SLEEP */
2752 /* FIXME: This is a temporary set of callbacks to enable us
2753 * to do suspend-to-disk.
2756 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2758 static int pmu_sys_suspended;
2760 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
2762 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
2765 /* Suspend PMU event interrupts */
2768 pmu_sys_suspended = 1;
2772 static int pmu_sys_resume(struct sys_device *sysdev)
2774 struct adb_request req;
2776 if (!pmu_sys_suspended)
2779 /* Tell PMU we are ready */
2780 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2781 pmu_wait_complete(&req);
2783 /* Resume PMU event interrupts */
2786 pmu_sys_suspended = 0;
2791 #endif /* CONFIG_PM && CONFIG_PPC32 */
2793 static struct sysdev_class pmu_sysclass = {
2794 set_kset_name("pmu"),
2797 static struct sys_device device_pmu = {
2798 .cls = &pmu_sysclass,
2801 static struct sysdev_driver driver_pmu = {
2802 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2803 .suspend = &pmu_sys_suspend,
2804 .resume = &pmu_sys_resume,
2805 #endif /* CONFIG_PM && CONFIG_PPC32 */
2808 static int __init init_pmu_sysfs(void)
2812 rc = sysdev_class_register(&pmu_sysclass);
2814 printk(KERN_ERR "Failed registering PMU sys class\n");
2817 rc = sysdev_register(&device_pmu);
2819 printk(KERN_ERR "Failed registering PMU sys device\n");
2822 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2824 printk(KERN_ERR "Failed registering PMU sys driver\n");
2830 subsys_initcall(init_pmu_sysfs);
2832 EXPORT_SYMBOL(pmu_request);
2833 EXPORT_SYMBOL(pmu_queue_request);
2834 EXPORT_SYMBOL(pmu_poll);
2835 EXPORT_SYMBOL(pmu_poll_adb);
2836 EXPORT_SYMBOL(pmu_wait_complete);
2837 EXPORT_SYMBOL(pmu_suspend);
2838 EXPORT_SYMBOL(pmu_resume);
2839 EXPORT_SYMBOL(pmu_unlock);
2840 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2841 EXPORT_SYMBOL(pmu_enable_irled);
2842 EXPORT_SYMBOL(pmu_battery_count);
2843 EXPORT_SYMBOL(pmu_batteries);
2844 EXPORT_SYMBOL(pmu_power_flags);
2845 #endif /* CONFIG_PM && CONFIG_PPC32 */