2 * Device driver for the via-pmu on Apple Powermacs.
4 * The VIA (versatile interface adapter) interfaces to the PMU,
5 * a 6805 microprocessor core whose primary function is to control
6 * battery charging and system power on the PowerBook 3400 and 2400.
7 * The PMU also controls the ADB (Apple Desktop Bus) which connects
8 * to the keyboard and mouse, as well as the non-volatile RAM
9 * and the RTC (real time clock) chip.
11 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
12 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
14 * THIS DRIVER IS BECOMING A TOTAL MESS !
15 * - Cleanup atomically disabling reply to PMU events after
16 * a sleep or a freq. switch
17 * - Move sleep code out of here to pmac_pm, merge into new
18 * common PM infrastructure
19 * - Move backlight code out as well
20 * - Save/Restore PCI space properly
24 #include <linux/config.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/miscdevice.h>
31 #include <linux/blkdev.h>
32 #include <linux/pci.h>
33 #include <linux/slab.h>
34 #include <linux/poll.h>
35 #include <linux/adb.h>
36 #include <linux/pmu.h>
37 #include <linux/cuda.h>
38 #include <linux/smp_lock.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
42 #include <linux/proc_fs.h>
43 #include <linux/init.h>
44 #include <linux/interrupt.h>
45 #include <linux/device.h>
46 #include <linux/sysdev.h>
47 #include <linux/suspend.h>
48 #include <linux/syscalls.h>
49 #include <linux/cpu.h>
51 #include <asm/machdep.h>
53 #include <asm/pgtable.h>
54 #include <asm/system.h>
55 #include <asm/sections.h>
57 #include <asm/pmac_feature.h>
58 #include <asm/pmac_pfunc.h>
59 #include <asm/pmac_low_i2c.h>
60 #include <asm/uaccess.h>
61 #include <asm/mmu_context.h>
62 #include <asm/cputable.h>
64 #ifdef CONFIG_PMAC_BACKLIGHT
65 #include <asm/backlight.h>
69 #include <asm/open_pic.h>
72 /* Some compile options */
73 #undef SUSPEND_USES_PMU
75 #undef HACKED_PCI_SAVE
77 /* Misc minor number allocated for /dev/pmu */
80 /* How many iterations between battery polls */
81 #define BATTERY_POLLING_COUNT 2
83 static volatile unsigned char __iomem *via;
85 /* VIA registers - spaced 0x200 bytes apart */
86 #define RS 0x200 /* skip between registers */
87 #define B 0 /* B-side data */
88 #define A RS /* A-side data */
89 #define DIRB (2*RS) /* B-side direction (1=output) */
90 #define DIRA (3*RS) /* A-side direction (1=output) */
91 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
92 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
93 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
94 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
95 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
96 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
97 #define SR (10*RS) /* Shift register */
98 #define ACR (11*RS) /* Auxiliary control register */
99 #define PCR (12*RS) /* Peripheral control register */
100 #define IFR (13*RS) /* Interrupt flag register */
101 #define IER (14*RS) /* Interrupt enable register */
102 #define ANH (15*RS) /* A-side data, no handshake */
104 /* Bits in B data register: both active low */
105 #define TACK 0x08 /* Transfer acknowledge (input) */
106 #define TREQ 0x10 /* Transfer request (output) */
109 #define SR_CTRL 0x1c /* Shift register control bits */
110 #define SR_EXT 0x0c /* Shift on external clock */
111 #define SR_OUT 0x10 /* Shift out if 1 */
113 /* Bits in IFR and IER */
114 #define IER_SET 0x80 /* set bits in IER */
115 #define IER_CLR 0 /* clear bits in IER */
116 #define SR_INT 0x04 /* Shift register full/empty */
118 #define CB1_INT 0x10 /* transition on CB1 input */
120 static volatile enum pmu_state {
129 static volatile enum int_data_state {
134 } int_data_state[2] = { int_data_empty, int_data_empty };
136 static struct adb_request *current_req;
137 static struct adb_request *last_req;
138 static struct adb_request *req_awaiting_reply;
139 static unsigned char interrupt_data[2][32];
140 static int interrupt_data_len[2];
141 static int int_data_last;
142 static unsigned char *reply_ptr;
143 static int data_index;
145 static volatile int adb_int_pending;
146 static volatile int disable_poll;
147 static struct adb_request bright_req_1, bright_req_2;
148 static struct device_node *vias;
149 static int pmu_kind = PMU_UNKNOWN;
150 static int pmu_fully_inited = 0;
151 static int pmu_has_adb;
152 static struct device_node *gpio_node;
153 static unsigned char __iomem *gpio_reg = NULL;
154 static int gpio_irq = -1;
155 static int gpio_irq_enabled = -1;
156 static volatile int pmu_suspended = 0;
157 static spinlock_t pmu_lock;
158 static u8 pmu_intr_mask;
159 static int pmu_version;
160 static int drop_interrupts;
161 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
162 static int option_lid_wakeup = 1;
163 #endif /* CONFIG_PM && CONFIG_PPC32 */
164 #if (defined(CONFIG_PM)&&defined(CONFIG_PPC32))||defined(CONFIG_PMAC_BACKLIGHT)
165 static int sleep_in_progress;
167 static unsigned long async_req_locks;
168 static unsigned int pmu_irq_stats[11];
170 static struct proc_dir_entry *proc_pmu_root;
171 static struct proc_dir_entry *proc_pmu_info;
172 static struct proc_dir_entry *proc_pmu_irqstats;
173 static struct proc_dir_entry *proc_pmu_options;
174 static int option_server_mode;
176 int pmu_battery_count;
178 unsigned int pmu_power_flags;
179 struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
180 static int query_batt_timer = BATTERY_POLLING_COUNT;
181 static struct adb_request batt_req;
182 static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
184 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
185 extern int disable_kernel_backlight;
186 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
190 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
193 static int adb_dev_map = 0;
194 static int pmu_adb_flags;
196 static int pmu_probe(void);
197 static int pmu_init(void);
198 static int pmu_send_request(struct adb_request *req, int sync);
199 static int pmu_adb_autopoll(int devs);
200 static int pmu_adb_reset_bus(void);
201 #endif /* CONFIG_ADB */
203 static int init_pmu(void);
204 static void pmu_start(void);
205 static irqreturn_t via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs);
206 static irqreturn_t gpio1_interrupt(int irq, void *arg, struct pt_regs *regs);
207 static int proc_get_info(char *page, char **start, off_t off,
208 int count, int *eof, void *data);
209 static int proc_get_irqstats(char *page, char **start, off_t off,
210 int count, int *eof, void *data);
211 #ifdef CONFIG_PMAC_BACKLIGHT
212 static int pmu_set_backlight_level(int level, void* data);
213 static int pmu_set_backlight_enable(int on, int level, void* data);
214 #endif /* CONFIG_PMAC_BACKLIGHT */
215 static void pmu_pass_intr(unsigned char *data, int len);
216 static int proc_get_batt(char *page, char **start, off_t off,
217 int count, int *eof, void *data);
218 static int proc_read_options(char *page, char **start, off_t off,
219 int count, int *eof, void *data);
220 static int proc_write_options(struct file *file, const char __user *buffer,
221 unsigned long count, void *data);
224 struct adb_driver via_pmu_driver = {
233 #endif /* CONFIG_ADB */
235 extern void low_sleep_handler(void);
236 extern void enable_kernel_altivec(void);
237 extern void enable_kernel_fp(void);
240 int pmu_polled_request(struct adb_request *req);
241 int pmu_wink(struct adb_request *req);
245 * This table indicates for each PMU opcode:
246 * - the number of data bytes to be sent with the command, or -1
247 * if a length byte should be sent,
248 * - the number of response bytes which the PMU will return, or
249 * -1 if it will send a length byte.
251 static const s8 pmu_data_len[256][2] = {
252 /* 0 1 2 3 4 5 6 7 */
253 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
255 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
257 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
258 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
259 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
260 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
261 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
263 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
264 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
265 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
266 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
267 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
268 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
269 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
270 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
271 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
272 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
273 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
274 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
275 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
276 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
277 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
278 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
279 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
280 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
281 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
282 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
283 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
284 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
287 static char *pbook_type[] = {
289 "PowerBook 2400/3400/3500(G3)",
290 "PowerBook G3 Series",
295 #ifdef CONFIG_PMAC_BACKLIGHT
296 static struct backlight_controller pmu_backlight_controller = {
297 pmu_set_backlight_enable,
298 pmu_set_backlight_level
300 #endif /* CONFIG_PMAC_BACKLIGHT */
302 int __init find_via_pmu(void)
309 vias = of_find_node_by_name(NULL, "via-pmu");
313 reg = (u32 *)get_property(vias, "reg", NULL);
315 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
318 taddr = of_translate_address(vias, reg);
319 if (taddr == OF_BAD_ADDR) {
320 printk(KERN_ERR "via-pmu: Can't translate address !\n");
324 spin_lock_init(&pmu_lock);
328 pmu_intr_mask = PMU_INT_PCEJECT |
333 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
334 || device_is_compatible(vias->parent, "ohare")))
335 pmu_kind = PMU_OHARE_BASED;
336 else if (device_is_compatible(vias->parent, "paddington"))
337 pmu_kind = PMU_PADDINGTON_BASED;
338 else if (device_is_compatible(vias->parent, "heathrow"))
339 pmu_kind = PMU_HEATHROW_BASED;
340 else if (device_is_compatible(vias->parent, "Keylargo")
341 || device_is_compatible(vias->parent, "K2-Keylargo")) {
342 struct device_node *gpiop;
343 u64 gaddr = OF_BAD_ADDR;
345 pmu_kind = PMU_KEYLARGO_BASED;
346 pmu_has_adb = (find_type_devices("adb") != NULL);
347 pmu_intr_mask = PMU_INT_PCEJECT |
353 gpiop = of_find_node_by_name(NULL, "gpio");
355 reg = (u32 *)get_property(gpiop, "reg", NULL);
357 gaddr = of_translate_address(gpiop, reg);
358 if (gaddr != OF_BAD_ADDR)
359 gpio_reg = ioremap(gaddr, 0x10);
361 if (gpio_reg == NULL)
362 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
364 pmu_kind = PMU_UNKNOWN;
366 via = ioremap(taddr, 0x2000);
368 printk(KERN_ERR "via-pmu: Can't map address !\n");
372 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
373 out_8(&via[IFR], 0x7f); /* clear IFR */
382 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
383 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
385 sys_ctrler = SYS_CTRLER_PMU;
395 static int pmu_probe(void)
397 return vias == NULL? -ENODEV: 0;
400 static int __init pmu_init(void)
406 #endif /* CONFIG_ADB */
409 * We can't wait until pmu_init gets called, that happens too late.
410 * It happens after IDE and SCSI initialization, which can take a few
411 * seconds, and by that time the PMU could have given up on us and
413 * Thus this is called with arch_initcall rather than device_initcall.
415 static int __init via_pmu_start(void)
420 bright_req_1.complete = 1;
421 bright_req_2.complete = 1;
422 batt_req.complete = 1;
424 #ifndef CONFIG_PPC_MERGE
425 if (pmu_kind == PMU_KEYLARGO_BASED)
426 openpic_set_irq_priority(vias->intrs[0].line,
427 OPENPIC_PRIORITY_DEFAULT + 1);
430 if (request_irq(vias->intrs[0].line, via_pmu_interrupt, 0, "VIA-PMU",
432 printk(KERN_ERR "VIA-PMU: can't get irq %d\n",
433 vias->intrs[0].line);
437 if (pmu_kind == PMU_KEYLARGO_BASED) {
438 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
439 if (gpio_node == NULL)
440 gpio_node = of_find_node_by_name(NULL,
442 if (gpio_node && gpio_node->n_intrs > 0)
443 gpio_irq = gpio_node->intrs[0].line;
445 if (gpio_irq != -1) {
446 if (request_irq(gpio_irq, gpio1_interrupt, 0,
447 "GPIO1 ADB", (void *)0))
448 printk(KERN_ERR "pmu: can't get irq %d"
449 " (GPIO1)\n", gpio_irq);
451 gpio_irq_enabled = 1;
455 /* Enable interrupts */
456 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
458 pmu_fully_inited = 1;
460 /* Make sure PMU settle down before continuing. This is _very_ important
461 * since the IDE probe may shut interrupts down for quite a bit of time. If
462 * a PMU communication is pending while this happens, the PMU may timeout
463 * Not that on Core99 machines, the PMU keeps sending us environement
464 * messages, we should find a way to either fix IDE or make it call
465 * pmu_suspend() before masking interrupts. This can also happens while
466 * scolling with some fbdevs.
470 } while (pmu_state != idle);
475 arch_initcall(via_pmu_start);
478 * This has to be done after pci_init, which is a subsys_initcall.
480 static int __init via_pmu_dev_init(void)
485 #ifdef CONFIG_PMAC_BACKLIGHT
486 /* Enable backlight */
487 register_backlight_controller(&pmu_backlight_controller, NULL, "pmu");
488 #endif /* CONFIG_PMAC_BACKLIGHT */
491 if (machine_is_compatible("AAPL,3400/2400") ||
492 machine_is_compatible("AAPL,3500")) {
493 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
494 NULL, PMAC_MB_INFO_MODEL, 0);
495 pmu_battery_count = 1;
496 if (mb == PMAC_TYPE_COMET)
497 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
499 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
500 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
501 machine_is_compatible("PowerBook1,1")) {
502 pmu_battery_count = 2;
503 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
504 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
506 struct device_node* prim = find_devices("power-mgt");
507 u32 *prim_info = NULL;
509 prim_info = (u32 *)get_property(prim, "prim-info", NULL);
511 /* Other stuffs here yet unknown */
512 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
513 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
514 if (pmu_battery_count > 1)
515 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
518 #endif /* CONFIG_PPC32 */
520 /* Create /proc/pmu */
521 proc_pmu_root = proc_mkdir("pmu", NULL);
525 for (i=0; i<pmu_battery_count; i++) {
527 sprintf(title, "battery_%ld", i);
528 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
529 proc_get_batt, (void *)i);
532 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
533 proc_get_info, NULL);
534 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
535 proc_get_irqstats, NULL);
536 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
537 if (proc_pmu_options) {
538 proc_pmu_options->nlink = 1;
539 proc_pmu_options->read_proc = proc_read_options;
540 proc_pmu_options->write_proc = proc_write_options;
546 device_initcall(via_pmu_dev_init);
552 struct adb_request req;
554 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
555 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
557 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
559 while (!req.complete) {
561 printk(KERN_ERR "init_pmu: no response from PMU\n");
568 /* ack all pending interrupts */
570 interrupt_data[0][0] = 1;
571 while (interrupt_data[0][0] || pmu_state != idle) {
573 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
576 if (pmu_state == idle)
578 via_pmu_interrupt(0, NULL, NULL);
582 /* Tell PMU we are ready. */
583 if (pmu_kind == PMU_KEYLARGO_BASED) {
584 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
585 while (!req.complete)
589 /* Read PMU version */
590 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
591 pmu_wait_complete(&req);
592 if (req.reply_len > 0)
593 pmu_version = req.reply[0];
595 /* Read server mode setting */
596 if (pmu_kind == PMU_KEYLARGO_BASED) {
597 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
598 PMU_PWR_GET_POWERUP_EVENTS);
599 pmu_wait_complete(&req);
600 if (req.reply_len == 2) {
601 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
602 option_server_mode = 1;
603 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
604 option_server_mode ? "enabled" : "disabled");
616 static void pmu_set_server_mode(int server_mode)
618 struct adb_request req;
620 if (pmu_kind != PMU_KEYLARGO_BASED)
623 option_server_mode = server_mode;
624 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
625 pmu_wait_complete(&req);
626 if (req.reply_len < 2)
629 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
630 PMU_PWR_SET_POWERUP_EVENTS,
631 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
633 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
634 PMU_PWR_CLR_POWERUP_EVENTS,
635 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
636 pmu_wait_complete(&req);
639 /* This new version of the code for 2400/3400/3500 powerbooks
640 * is inspired from the implementation in gkrellm-pmu
643 done_battery_state_ohare(struct adb_request* req)
647 * 0x01 : AC indicator
649 * 0x04 : battery exist
652 * 0x20 : full charged
653 * 0x40 : pcharge reset
654 * 0x80 : battery exist
656 * [1][2] : battery voltage
657 * [3] : CPU temperature
658 * [4] : battery temperature
663 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
664 long pcharge, charge, vb, vmax, lmax;
665 long vmax_charging, vmax_charged;
666 long amperage, voltage, time, max;
667 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
668 NULL, PMAC_MB_INFO_MODEL, 0);
670 if (req->reply[0] & 0x01)
671 pmu_power_flags |= PMU_PWR_AC_PRESENT;
673 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
675 if (mb == PMAC_TYPE_COMET) {
686 /* If battery installed */
687 if (req->reply[0] & 0x04) {
688 bat_flags |= PMU_BATT_PRESENT;
689 if (req->reply[0] & 0x02)
690 bat_flags |= PMU_BATT_CHARGING;
691 vb = (req->reply[1] << 8) | req->reply[2];
692 voltage = (vb * 265 + 72665) / 10;
693 amperage = req->reply[5];
694 if ((req->reply[0] & 0x01) == 0) {
696 vb += ((amperage - 200) * 15)/100;
697 } else if (req->reply[0] & 0x02) {
698 vb = (vb * 97) / 100;
699 vmax = vmax_charging;
701 charge = (100 * vb) / vmax;
702 if (req->reply[0] & 0x40) {
703 pcharge = (req->reply[6] << 8) + req->reply[7];
707 pcharge = 100 - pcharge / lmax;
708 if (pcharge < charge)
712 time = (charge * 16440) / amperage;
716 amperage = -amperage;
718 charge = max = amperage = voltage = time = 0;
720 pmu_batteries[pmu_cur_battery].flags = bat_flags;
721 pmu_batteries[pmu_cur_battery].charge = charge;
722 pmu_batteries[pmu_cur_battery].max_charge = max;
723 pmu_batteries[pmu_cur_battery].amperage = amperage;
724 pmu_batteries[pmu_cur_battery].voltage = voltage;
725 pmu_batteries[pmu_cur_battery].time_remaining = time;
727 clear_bit(0, &async_req_locks);
731 done_battery_state_smart(struct adb_request* req)
734 * [0] : format of this structure (known: 3,4,5)
747 * [4][5] : max charge
752 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
754 unsigned int capa, max, voltage;
756 if (req->reply[1] & 0x01)
757 pmu_power_flags |= PMU_PWR_AC_PRESENT;
759 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
762 capa = max = amperage = voltage = 0;
764 if (req->reply[1] & 0x04) {
765 bat_flags |= PMU_BATT_PRESENT;
766 switch(req->reply[0]) {
768 case 4: capa = req->reply[2];
770 amperage = *((signed char *)&req->reply[4]);
771 voltage = req->reply[5];
773 case 5: capa = (req->reply[2] << 8) | req->reply[3];
774 max = (req->reply[4] << 8) | req->reply[5];
775 amperage = *((signed short *)&req->reply[6]);
776 voltage = (req->reply[8] << 8) | req->reply[9];
779 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
780 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
785 if ((req->reply[1] & 0x01) && (amperage > 0))
786 bat_flags |= PMU_BATT_CHARGING;
788 pmu_batteries[pmu_cur_battery].flags = bat_flags;
789 pmu_batteries[pmu_cur_battery].charge = capa;
790 pmu_batteries[pmu_cur_battery].max_charge = max;
791 pmu_batteries[pmu_cur_battery].amperage = amperage;
792 pmu_batteries[pmu_cur_battery].voltage = voltage;
794 if ((req->reply[1] & 0x01) && (amperage > 0))
795 pmu_batteries[pmu_cur_battery].time_remaining
796 = ((max-capa) * 3600) / amperage;
798 pmu_batteries[pmu_cur_battery].time_remaining
799 = (capa * 3600) / (-amperage);
801 pmu_batteries[pmu_cur_battery].time_remaining = 0;
803 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
805 clear_bit(0, &async_req_locks);
809 query_battery_state(void)
811 if (test_and_set_bit(0, &async_req_locks))
813 if (pmu_kind == PMU_OHARE_BASED)
814 pmu_request(&batt_req, done_battery_state_ohare,
815 1, PMU_BATTERY_STATE);
817 pmu_request(&batt_req, done_battery_state_smart,
818 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
822 proc_get_info(char *page, char **start, off_t off,
823 int count, int *eof, void *data)
827 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
828 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
829 p += sprintf(p, "AC Power : %d\n",
830 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
831 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
837 proc_get_irqstats(char *page, char **start, off_t off,
838 int count, int *eof, void *data)
842 static const char *irq_names[] = {
843 "Total CB1 triggered events",
844 "Total GPIO1 triggered events",
845 "PC-Card eject button",
846 "Sound/Brightness button",
848 "Battery state change",
849 "Environment interrupt",
851 "Ghost interrupt (zero len)",
852 "Empty interrupt (empty mask)",
856 for (i=0; i<11; i++) {
857 p += sprintf(p, " %2u: %10u (%s)\n",
858 i, pmu_irq_stats[i], irq_names[i]);
864 proc_get_batt(char *page, char **start, off_t off,
865 int count, int *eof, void *data)
867 long batnum = (long)data;
870 p += sprintf(p, "\n");
871 p += sprintf(p, "flags : %08x\n",
872 pmu_batteries[batnum].flags);
873 p += sprintf(p, "charge : %d\n",
874 pmu_batteries[batnum].charge);
875 p += sprintf(p, "max_charge : %d\n",
876 pmu_batteries[batnum].max_charge);
877 p += sprintf(p, "current : %d\n",
878 pmu_batteries[batnum].amperage);
879 p += sprintf(p, "voltage : %d\n",
880 pmu_batteries[batnum].voltage);
881 p += sprintf(p, "time rem. : %d\n",
882 pmu_batteries[batnum].time_remaining);
888 proc_read_options(char *page, char **start, off_t off,
889 int count, int *eof, void *data)
893 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
894 if (pmu_kind == PMU_KEYLARGO_BASED &&
895 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
896 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
898 if (pmu_kind == PMU_KEYLARGO_BASED)
899 p += sprintf(p, "server_mode=%d\n", option_server_mode);
905 proc_write_options(struct file *file, const char __user *buffer,
906 unsigned long count, void *data)
910 unsigned long fcount = count;
916 if (copy_from_user(tmp, buffer, count))
924 while(*val && (*val != '=')) {
934 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
935 if (pmu_kind == PMU_KEYLARGO_BASED &&
936 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
937 if (!strcmp(label, "lid_wakeup"))
938 option_lid_wakeup = ((*val) == '1');
940 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
942 new_value = ((*val) == '1');
943 if (new_value != option_server_mode)
944 pmu_set_server_mode(new_value);
950 /* Send an ADB command */
952 pmu_send_request(struct adb_request *req, int sync)
956 if ((vias == NULL) || (!pmu_fully_inited)) {
963 switch (req->data[0]) {
965 for (i = 0; i < req->nbytes - 1; ++i)
966 req->data[i] = req->data[i+1];
968 if (pmu_data_len[req->data[0]][1] != 0) {
969 req->reply[0] = ADB_RET_OK;
973 ret = pmu_queue_request(req);
976 switch (req->data[1]) {
978 if (req->nbytes != 2)
980 req->data[0] = PMU_READ_RTC;
983 req->reply[0] = CUDA_PACKET;
985 req->reply[2] = CUDA_GET_TIME;
986 ret = pmu_queue_request(req);
989 if (req->nbytes != 6)
991 req->data[0] = PMU_SET_RTC;
993 for (i = 1; i <= 4; ++i)
994 req->data[i] = req->data[i+1];
996 req->reply[0] = CUDA_PACKET;
998 req->reply[2] = CUDA_SET_TIME;
999 ret = pmu_queue_request(req);
1006 for (i = req->nbytes - 1; i > 1; --i)
1007 req->data[i+2] = req->data[i];
1008 req->data[3] = req->nbytes - 2;
1009 req->data[2] = pmu_adb_flags;
1010 /*req->data[1] = req->data[1];*/
1011 req->data[0] = PMU_ADB_CMD;
1013 req->reply_expected = 1;
1015 ret = pmu_queue_request(req);
1024 while (!req->complete)
1030 /* Enable/disable autopolling */
1032 pmu_adb_autopoll(int devs)
1034 struct adb_request req;
1036 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1041 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1042 adb_dev_map >> 8, adb_dev_map);
1045 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1048 while (!req.complete)
1053 /* Reset the ADB bus */
1055 pmu_adb_reset_bus(void)
1057 struct adb_request req;
1058 int save_autopoll = adb_dev_map;
1060 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1063 /* anyone got a better idea?? */
1064 pmu_adb_autopoll(0);
1068 req.data[0] = PMU_ADB_CMD;
1070 req.data[2] = ADB_BUSRESET;
1074 req.reply_expected = 1;
1075 if (pmu_queue_request(&req) != 0) {
1076 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1079 pmu_wait_complete(&req);
1081 if (save_autopoll != 0)
1082 pmu_adb_autopoll(save_autopoll);
1086 #endif /* CONFIG_ADB */
1088 /* Construct and send a pmu request */
1090 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1099 if (nbytes < 0 || nbytes > 32) {
1100 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1104 req->nbytes = nbytes;
1106 va_start(list, nbytes);
1107 for (i = 0; i < nbytes; ++i)
1108 req->data[i] = va_arg(list, int);
1111 req->reply_expected = 0;
1112 return pmu_queue_request(req);
1116 pmu_queue_request(struct adb_request *req)
1118 unsigned long flags;
1125 if (req->nbytes <= 0) {
1129 nsend = pmu_data_len[req->data[0]][0];
1130 if (nsend >= 0 && req->nbytes != nsend + 1) {
1139 spin_lock_irqsave(&pmu_lock, flags);
1140 if (current_req != 0) {
1141 last_req->next = req;
1146 if (pmu_state == idle)
1149 spin_unlock_irqrestore(&pmu_lock, flags);
1157 /* Sightly increased the delay, I had one occurrence of the message
1161 while ((in_8(&via[B]) & TACK) == 0) {
1162 if (--timeout < 0) {
1163 printk(KERN_ERR "PMU not responding (!ack)\n");
1170 /* New PMU seems to be very sensitive to those timings, so we make sure
1171 * PCI is flushed immediately */
1175 volatile unsigned char __iomem *v = via;
1177 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1179 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1186 volatile unsigned char __iomem *v = via;
1188 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1189 in_8(&v[SR]); /* resets SR */
1190 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1195 pmu_done(struct adb_request *req)
1197 void (*done)(struct adb_request *) = req->done;
1200 /* Here, we assume that if the request has a done member, the
1201 * struct request will survive to setting req->complete to 1
1210 struct adb_request *req;
1212 /* assert pmu_state == idle */
1213 /* get the packet to send */
1215 if (req == 0 || pmu_state != idle
1216 || (/*req->reply_expected && */req_awaiting_reply))
1219 pmu_state = sending;
1221 data_len = pmu_data_len[req->data[0]][0];
1223 /* Sounds safer to make sure ACK is high before writing. This helped
1224 * kill a problem with ADB and some iBooks
1227 /* set the shift register to shift out and send a byte */
1228 send_byte(req->data[0]);
1238 via_pmu_interrupt(0, NULL, NULL);
1248 /* Kicks ADB read when PMU is suspended */
1249 adb_int_pending = 1;
1251 via_pmu_interrupt(0, NULL, NULL);
1252 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1253 || req_awaiting_reply));
1257 pmu_wait_complete(struct adb_request *req)
1261 while((pmu_state != idle && pmu_state != locked) || !req->complete)
1262 via_pmu_interrupt(0, NULL, NULL);
1265 /* This function loops until the PMU is idle and prevents it from
1266 * anwsering to ADB interrupts. pmu_request can still be called.
1267 * This is done to avoid spurrious shutdowns when we know we'll have
1268 * interrupts switched off for a long time
1273 unsigned long flags;
1274 #ifdef SUSPEND_USES_PMU
1275 struct adb_request *req;
1280 spin_lock_irqsave(&pmu_lock, flags);
1282 if (pmu_suspended > 1) {
1283 spin_unlock_irqrestore(&pmu_lock, flags);
1288 spin_unlock_irqrestore(&pmu_lock, flags);
1289 if (req_awaiting_reply)
1290 adb_int_pending = 1;
1291 via_pmu_interrupt(0, NULL, NULL);
1292 spin_lock_irqsave(&pmu_lock, flags);
1293 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
1294 #ifdef SUSPEND_USES_PMU
1295 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1296 spin_unlock_irqrestore(&pmu_lock, flags);
1297 while(!req.complete)
1299 #else /* SUSPEND_USES_PMU */
1301 disable_irq_nosync(gpio_irq);
1302 out_8(&via[IER], CB1_INT | IER_CLR);
1303 spin_unlock_irqrestore(&pmu_lock, flags);
1304 #endif /* SUSPEND_USES_PMU */
1313 unsigned long flags;
1315 if (!via || (pmu_suspended < 1))
1318 spin_lock_irqsave(&pmu_lock, flags);
1320 if (pmu_suspended > 0) {
1321 spin_unlock_irqrestore(&pmu_lock, flags);
1324 adb_int_pending = 1;
1325 #ifdef SUSPEND_USES_PMU
1326 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1327 spin_unlock_irqrestore(&pmu_lock, flags);
1328 while(!req.complete)
1330 #else /* SUSPEND_USES_PMU */
1332 enable_irq(gpio_irq);
1333 out_8(&via[IER], CB1_INT | IER_SET);
1334 spin_unlock_irqrestore(&pmu_lock, flags);
1336 #endif /* SUSPEND_USES_PMU */
1339 /* Interrupt data could be the result data from an ADB cmd */
1341 pmu_handle_data(unsigned char *data, int len, struct pt_regs *regs)
1343 unsigned char ints, pirq;
1347 if (drop_interrupts || len < 1) {
1348 adb_int_pending = 0;
1353 /* Get PMU interrupt mask */
1356 /* Record zero interrupts for stats */
1360 /* Hack to deal with ADB autopoll flag */
1361 if (ints & PMU_INT_ADB)
1362 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1367 if (i > pmu_irq_stats[10])
1368 pmu_irq_stats[10] = i;
1372 for (pirq = 0; pirq < 8; pirq++)
1373 if (ints & (1 << pirq))
1375 pmu_irq_stats[pirq]++;
1377 ints &= ~(1 << pirq);
1379 /* Note: for some reason, we get an interrupt with len=1,
1380 * data[0]==0 after each normal ADB interrupt, at least
1381 * on the Pismo. Still investigating... --BenH
1383 if ((1 << pirq) & PMU_INT_ADB) {
1384 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1385 struct adb_request *req = req_awaiting_reply;
1387 printk(KERN_ERR "PMU: extra ADB reply\n");
1390 req_awaiting_reply = NULL;
1394 memcpy(req->reply, data + 1, len - 1);
1395 req->reply_len = len - 1;
1399 if (len == 4 && data[1] == 0x2c) {
1400 extern int xmon_wants_key, xmon_adb_keycode;
1401 if (xmon_wants_key) {
1402 xmon_adb_keycode = data[2];
1408 * XXX On the [23]400 the PMU gives us an up
1409 * event for keycodes 0x74 or 0x75 when the PC
1410 * card eject buttons are released, so we
1411 * ignore those events.
1413 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1414 && data[1] == 0x2c && data[3] == 0xff
1415 && (data[2] & ~1) == 0xf4))
1416 adb_input(data+1, len-1, regs, 1);
1417 #endif /* CONFIG_ADB */
1420 /* Sound/brightness button pressed */
1421 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1422 #ifdef CONFIG_PMAC_BACKLIGHT
1424 #ifdef CONFIG_INPUT_ADBHID
1425 if (!disable_kernel_backlight)
1426 #endif /* CONFIG_INPUT_ADBHID */
1427 set_backlight_level(data[1] >> 4);
1428 #endif /* CONFIG_PMAC_BACKLIGHT */
1430 /* Tick interrupt */
1431 else if ((1 << pirq) & PMU_INT_TICK) {
1432 /* Environement or tick interrupt, query batteries */
1433 if (pmu_battery_count) {
1434 if ((--query_batt_timer) == 0) {
1435 query_battery_state();
1436 query_batt_timer = BATTERY_POLLING_COUNT;
1440 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1441 if (pmu_battery_count)
1442 query_battery_state();
1443 pmu_pass_intr(data, len);
1445 pmu_pass_intr(data, len);
1450 static struct adb_request*
1451 pmu_sr_intr(struct pt_regs *regs)
1453 struct adb_request *req;
1456 if (via[B] & TREQ) {
1457 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1458 out_8(&via[IFR], SR_INT);
1461 /* The ack may not yet be low when we get the interrupt */
1462 while ((in_8(&via[B]) & TACK) != 0)
1465 /* if reading grab the byte, and reset the interrupt */
1466 if (pmu_state == reading || pmu_state == reading_intr)
1467 bite = in_8(&via[SR]);
1469 /* reset TREQ and wait for TACK to go high */
1470 out_8(&via[B], in_8(&via[B]) | TREQ);
1473 switch (pmu_state) {
1477 data_len = req->nbytes - 1;
1478 send_byte(data_len);
1481 if (data_index <= data_len) {
1482 send_byte(req->data[data_index++]);
1486 data_len = pmu_data_len[req->data[0]][1];
1487 if (data_len == 0) {
1489 current_req = req->next;
1490 if (req->reply_expected)
1491 req_awaiting_reply = req;
1495 pmu_state = reading;
1497 reply_ptr = req->reply + req->reply_len;
1505 pmu_state = reading_intr;
1506 reply_ptr = interrupt_data[int_data_last];
1508 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1509 enable_irq(gpio_irq);
1510 gpio_irq_enabled = 1;
1516 if (data_len == -1) {
1519 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1520 } else if (data_index < 32) {
1521 reply_ptr[data_index++] = bite;
1523 if (data_index < data_len) {
1528 if (pmu_state == reading_intr) {
1530 int_data_state[int_data_last] = int_data_ready;
1531 interrupt_data_len[int_data_last] = data_len;
1535 * For PMU sleep and freq change requests, we lock the
1536 * PMU until it's explicitely unlocked. This avoids any
1537 * spurrious event polling getting in
1539 current_req = req->next;
1540 req->reply_len += data_index;
1541 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1550 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1557 via_pmu_interrupt(int irq, void *arg, struct pt_regs *regs)
1559 unsigned long flags;
1563 struct adb_request *req = NULL;
1566 /* This is a bit brutal, we can probably do better */
1567 spin_lock_irqsave(&pmu_lock, flags);
1571 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1575 if (++nloop > 1000) {
1576 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1577 "intr=%x, ier=%x pmu_state=%d\n",
1578 intr, in_8(&via[IER]), pmu_state);
1581 out_8(&via[IFR], intr);
1582 if (intr & CB1_INT) {
1583 adb_int_pending = 1;
1586 if (intr & SR_INT) {
1587 req = pmu_sr_intr(regs);
1594 if (pmu_state == idle) {
1595 if (adb_int_pending) {
1596 if (int_data_state[0] == int_data_empty)
1598 else if (int_data_state[1] == int_data_empty)
1603 int_data_state[int_data_last] = int_data_fill;
1604 /* Sounds safer to make sure ACK is high before writing.
1605 * This helped kill a problem with ADB and some iBooks
1608 send_byte(PMU_INT_ACK);
1609 adb_int_pending = 0;
1610 } else if (current_req)
1614 /* Mark the oldest buffer for flushing */
1615 if (int_data_state[!int_data_last] == int_data_ready) {
1616 int_data_state[!int_data_last] = int_data_flush;
1617 int_data = !int_data_last;
1618 } else if (int_data_state[int_data_last] == int_data_ready) {
1619 int_data_state[int_data_last] = int_data_flush;
1620 int_data = int_data_last;
1623 spin_unlock_irqrestore(&pmu_lock, flags);
1625 /* Deal with completed PMU requests outside of the lock */
1631 /* Deal with interrupt datas outside of the lock */
1632 if (int_data >= 0) {
1633 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data], regs);
1634 spin_lock_irqsave(&pmu_lock, flags);
1636 int_data_state[int_data] = int_data_empty;
1641 return IRQ_RETVAL(handled);
1647 unsigned long flags;
1649 spin_lock_irqsave(&pmu_lock, flags);
1650 if (pmu_state == locked)
1652 adb_int_pending = 1;
1653 spin_unlock_irqrestore(&pmu_lock, flags);
1658 gpio1_interrupt(int irq, void *arg, struct pt_regs *regs)
1660 unsigned long flags;
1662 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1663 spin_lock_irqsave(&pmu_lock, flags);
1664 if (gpio_irq_enabled > 0) {
1665 disable_irq_nosync(gpio_irq);
1666 gpio_irq_enabled = 0;
1669 adb_int_pending = 1;
1670 spin_unlock_irqrestore(&pmu_lock, flags);
1671 via_pmu_interrupt(0, NULL, NULL);
1677 #ifdef CONFIG_PMAC_BACKLIGHT
1678 static int backlight_to_bright[] = {
1679 0x7f, 0x46, 0x42, 0x3e, 0x3a, 0x36, 0x32, 0x2e,
1680 0x2a, 0x26, 0x22, 0x1e, 0x1a, 0x16, 0x12, 0x0e
1684 pmu_set_backlight_enable(int on, int level, void* data)
1686 struct adb_request req;
1692 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
1693 backlight_to_bright[level]);
1694 pmu_wait_complete(&req);
1696 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1697 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
1698 pmu_wait_complete(&req);
1704 pmu_bright_complete(struct adb_request *req)
1706 if (req == &bright_req_1)
1707 clear_bit(1, &async_req_locks);
1708 if (req == &bright_req_2)
1709 clear_bit(2, &async_req_locks);
1713 pmu_set_backlight_level(int level, void* data)
1718 if (test_and_set_bit(1, &async_req_locks))
1720 pmu_request(&bright_req_1, pmu_bright_complete, 2, PMU_BACKLIGHT_BRIGHT,
1721 backlight_to_bright[level]);
1722 if (test_and_set_bit(2, &async_req_locks))
1724 pmu_request(&bright_req_2, pmu_bright_complete, 2, PMU_POWER_CTRL,
1725 PMU_POW_BACKLIGHT | (level > BACKLIGHT_OFF ?
1726 PMU_POW_ON : PMU_POW_OFF));
1730 #endif /* CONFIG_PMAC_BACKLIGHT */
1733 pmu_enable_irled(int on)
1735 struct adb_request req;
1739 if (pmu_kind == PMU_KEYLARGO_BASED)
1742 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1743 (on ? PMU_POW_ON : PMU_POW_OFF));
1744 pmu_wait_complete(&req);
1750 struct adb_request req;
1755 local_irq_disable();
1757 drop_interrupts = 1;
1759 if (pmu_kind != PMU_KEYLARGO_BASED) {
1760 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1762 while(!req.complete)
1766 pmu_request(&req, NULL, 1, PMU_RESET);
1767 pmu_wait_complete(&req);
1775 struct adb_request req;
1780 local_irq_disable();
1782 drop_interrupts = 1;
1784 if (pmu_kind != PMU_KEYLARGO_BASED) {
1785 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1787 pmu_wait_complete(&req);
1789 /* Disable server mode on shutdown or we'll just
1792 pmu_set_server_mode(0);
1795 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1796 'M', 'A', 'T', 'T');
1797 pmu_wait_complete(&req);
1810 static LIST_HEAD(sleep_notifiers);
1813 pmu_register_sleep_notifier(struct pmu_sleep_notifier *n)
1815 struct list_head *list;
1816 struct pmu_sleep_notifier *notifier;
1818 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1819 list = list->next) {
1820 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1821 if (n->priority > notifier->priority)
1824 __list_add(&n->list, list->prev, list);
1827 EXPORT_SYMBOL(pmu_register_sleep_notifier);
1830 pmu_unregister_sleep_notifier(struct pmu_sleep_notifier* n)
1832 if (n->list.next == 0)
1835 n->list.next = NULL;
1838 EXPORT_SYMBOL(pmu_unregister_sleep_notifier);
1839 #endif /* CONFIG_PM */
1841 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
1843 /* Sleep is broadcast last-to-first */
1845 broadcast_sleep(int when, int fallback)
1847 int ret = PBOOK_SLEEP_OK;
1848 struct list_head *list;
1849 struct pmu_sleep_notifier *notifier;
1851 for (list = sleep_notifiers.prev; list != &sleep_notifiers;
1852 list = list->prev) {
1853 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1854 ret = notifier->notifier_call(notifier, when);
1855 if (ret != PBOOK_SLEEP_OK) {
1856 printk(KERN_DEBUG "sleep %d rejected by %p (%p)\n",
1857 when, notifier, notifier->notifier_call);
1858 for (; list != &sleep_notifiers; list = list->next) {
1859 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1860 notifier->notifier_call(notifier, fallback);
1868 /* Wake is broadcast first-to-last */
1870 broadcast_wake(void)
1872 int ret = PBOOK_SLEEP_OK;
1873 struct list_head *list;
1874 struct pmu_sleep_notifier *notifier;
1876 for (list = sleep_notifiers.next; list != &sleep_notifiers;
1877 list = list->next) {
1878 notifier = list_entry(list, struct pmu_sleep_notifier, list);
1879 notifier->notifier_call(notifier, PBOOK_WAKE);
1885 * This struct is used to store config register values for
1886 * PCI devices which may get powered off when we sleep.
1888 static struct pci_save {
1889 #ifndef HACKED_PCI_SAVE
1898 static int pbook_npci_saves;
1901 pbook_alloc_pci_save(void)
1904 struct pci_dev *pd = NULL;
1907 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1912 pbook_pci_saves = (struct pci_save *)
1913 kmalloc(npci * sizeof(struct pci_save), GFP_KERNEL);
1914 pbook_npci_saves = npci;
1918 pbook_free_pci_save(void)
1920 if (pbook_pci_saves == NULL)
1922 kfree(pbook_pci_saves);
1923 pbook_pci_saves = NULL;
1924 pbook_npci_saves = 0;
1928 pbook_pci_save(void)
1930 struct pci_save *ps = pbook_pci_saves;
1931 struct pci_dev *pd = NULL;
1932 int npci = pbook_npci_saves;
1937 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1940 #ifndef HACKED_PCI_SAVE
1941 pci_read_config_word(pd, PCI_COMMAND, &ps->command);
1942 pci_read_config_word(pd, PCI_CACHE_LINE_SIZE, &ps->cache_lat);
1943 pci_read_config_word(pd, PCI_INTERRUPT_LINE, &ps->intr);
1944 pci_read_config_dword(pd, PCI_ROM_ADDRESS, &ps->rom_address);
1948 pci_read_config_dword(pd, i<<4, &ps->config[i]);
1954 /* For this to work, we must take care of a few things: If gmac was enabled
1955 * during boot, it will be in the pci dev list. If it's disabled at this point
1956 * (and it will probably be), then you can't access it's config space.
1959 pbook_pci_restore(void)
1962 struct pci_save *ps = pbook_pci_saves - 1;
1963 struct pci_dev *pd = NULL;
1964 int npci = pbook_npci_saves;
1967 while ((pd = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, pd)) != NULL) {
1968 #ifdef HACKED_PCI_SAVE
1974 pci_write_config_dword(pd, i<<4, ps->config[i]);
1975 pci_write_config_dword(pd, 4, ps->config[1]);
1980 if (ps->command == 0)
1982 pci_read_config_word(pd, PCI_COMMAND, &cmd);
1983 if ((ps->command & ~cmd) == 0)
1985 switch (pd->hdr_type) {
1986 case PCI_HEADER_TYPE_NORMAL:
1987 for (j = 0; j < 6; ++j)
1988 pci_write_config_dword(pd,
1989 PCI_BASE_ADDRESS_0 + j*4,
1990 pd->resource[j].start);
1991 pci_write_config_dword(pd, PCI_ROM_ADDRESS,
1993 pci_write_config_word(pd, PCI_CACHE_LINE_SIZE,
1995 pci_write_config_word(pd, PCI_INTERRUPT_LINE,
1997 pci_write_config_word(pd, PCI_COMMAND, ps->command);
2005 /* N.B. This doesn't work on the 3400 */
2009 struct adb_request req;
2011 memset(&req, 0, sizeof(req));
2013 for (; n > 0; --n) {
2020 req.reply[0] = ADB_RET_OK;
2022 req.reply_expected = 0;
2023 pmu_polled_request(&req);
2031 req.reply[0] = ADB_RET_OK;
2033 req.reply_expected = 0;
2034 pmu_polled_request(&req);
2042 * Put the powerbook to sleep.
2045 static u32 save_via[8];
2048 save_via_state(void)
2050 save_via[0] = in_8(&via[ANH]);
2051 save_via[1] = in_8(&via[DIRA]);
2052 save_via[2] = in_8(&via[B]);
2053 save_via[3] = in_8(&via[DIRB]);
2054 save_via[4] = in_8(&via[PCR]);
2055 save_via[5] = in_8(&via[ACR]);
2056 save_via[6] = in_8(&via[T1CL]);
2057 save_via[7] = in_8(&via[T1CH]);
2060 restore_via_state(void)
2062 out_8(&via[ANH], save_via[0]);
2063 out_8(&via[DIRA], save_via[1]);
2064 out_8(&via[B], save_via[2]);
2065 out_8(&via[DIRB], save_via[3]);
2066 out_8(&via[PCR], save_via[4]);
2067 out_8(&via[ACR], save_via[5]);
2068 out_8(&via[T1CL], save_via[6]);
2069 out_8(&via[T1CH], save_via[7]);
2070 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
2071 out_8(&via[IFR], 0x7f); /* clear IFR */
2072 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
2076 pmac_suspend_devices(void)
2080 pm_prepare_console();
2082 /* Notify old-style device drivers & userland */
2083 ret = broadcast_sleep(PBOOK_SLEEP_REQUEST, PBOOK_SLEEP_REJECT);
2084 if (ret != PBOOK_SLEEP_OK) {
2085 printk(KERN_ERR "Sleep rejected by drivers\n");
2089 /* Sync the disks. */
2090 /* XXX It would be nice to have some way to ensure that
2091 * nobody is dirtying any new buffers while we wait. That
2092 * could be achieved using the refrigerator for processes
2097 /* Sleep can fail now. May not be very robust but useful for debugging */
2098 ret = broadcast_sleep(PBOOK_SLEEP_NOW, PBOOK_WAKE);
2099 if (ret != PBOOK_SLEEP_OK) {
2100 printk(KERN_ERR "Driver sleep failed\n");
2104 /* Send suspend call to devices, hold the device core's dpm_sem */
2105 ret = device_suspend(PMSG_SUSPEND);
2108 printk(KERN_ERR "Driver sleep failed\n");
2112 /* Call platform functions marked "on sleep" */
2113 pmac_pfunc_i2c_suspend();
2114 pmac_pfunc_base_suspend();
2116 /* Stop preemption */
2119 /* Make sure the decrementer won't interrupt us */
2120 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2121 /* Make sure any pending DEC interrupt occurring while we did
2122 * the above didn't re-enable the DEC */
2124 asm volatile("mtdec %0" : : "r" (0x7fffffff));
2126 /* We can now disable MSR_EE. This code of course works properly only
2127 * on UP machines... For SMP, if we ever implement sleep, we'll have to
2128 * stop the "other" CPUs way before we do all that stuff.
2130 local_irq_disable();
2132 /* Broadcast power down irq
2133 * This isn't that useful in most cases (only directly wired devices can
2134 * use this but still... This will take care of sysdev's as well, so
2135 * we exit from here with local irqs disabled and PIC off.
2137 ret = device_power_down(PMSG_SUSPEND);
2139 wakeup_decrementer();
2144 printk(KERN_ERR "Driver powerdown failed\n");
2148 /* Wait for completion of async backlight requests */
2149 while (!bright_req_1.complete || !bright_req_2.complete ||
2153 /* Giveup the lazy FPU & vec so we don't have to back them
2154 * up from the low level code
2158 #ifdef CONFIG_ALTIVEC
2159 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2160 enable_kernel_altivec();
2161 #endif /* CONFIG_ALTIVEC */
2167 pmac_wakeup_devices(void)
2171 /* Power back up system devices (including the PIC) */
2174 /* Force a poll of ADB interrupts */
2175 adb_int_pending = 1;
2176 via_pmu_interrupt(0, NULL, NULL);
2178 /* Restart jiffies & scheduling */
2179 wakeup_decrementer();
2181 /* Re-enable local CPU interrupts */
2186 /* Call platform functions marked "on wake" */
2187 pmac_pfunc_base_resume();
2188 pmac_pfunc_i2c_resume();
2190 /* Resume devices */
2193 /* Notify old style drivers */
2196 pm_restore_console();
2201 #define GRACKLE_PM (1<<7)
2202 #define GRACKLE_DOZE (1<<5)
2203 #define GRACKLE_NAP (1<<4)
2204 #define GRACKLE_SLEEP (1<<3)
2206 static int powerbook_sleep_grackle(void)
2208 unsigned long save_l2cr;
2209 unsigned short pmcr1;
2210 struct adb_request req;
2212 struct pci_dev *grackle;
2214 grackle = pci_find_slot(0, 0);
2218 ret = pmac_suspend_devices();
2220 printk(KERN_ERR "Sleep rejected by devices\n");
2224 /* Turn off various things. Darwin does some retry tests here... */
2225 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
2226 pmu_wait_complete(&req);
2227 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2228 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2229 pmu_wait_complete(&req);
2231 /* For 750, save backside cache setting and disable it */
2232 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2234 if (!__fake_sleep) {
2235 /* Ask the PMU to put us to sleep */
2236 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2237 pmu_wait_complete(&req);
2240 /* The VIA is supposed not to be restored correctly*/
2242 /* We shut down some HW */
2243 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2245 pci_read_config_word(grackle, 0x70, &pmcr1);
2246 /* Apparently, MacOS uses NAP mode for Grackle ??? */
2247 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
2248 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
2249 pci_write_config_word(grackle, 0x70, pmcr1);
2251 /* Call low-level ASM sleep handler */
2255 low_sleep_handler();
2257 /* We're awake again, stop grackle PM */
2258 pci_read_config_word(grackle, 0x70, &pmcr1);
2259 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
2260 pci_write_config_word(grackle, 0x70, pmcr1);
2262 /* Make sure the PMU is idle */
2263 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2264 restore_via_state();
2266 /* Restore L2 cache */
2267 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2268 _set_L2CR(save_l2cr);
2270 /* Restore userland MMU context */
2271 set_context(current->active_mm->context.id, current->active_mm->pgd);
2273 /* Power things up */
2275 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2276 pmu_wait_complete(&req);
2277 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
2278 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
2279 pmu_wait_complete(&req);
2280 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
2281 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
2282 pmu_wait_complete(&req);
2284 pmac_wakeup_devices();
2290 powerbook_sleep_Core99(void)
2292 unsigned long save_l2cr;
2293 unsigned long save_l3cr;
2294 struct adb_request req;
2297 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
2298 printk(KERN_ERR "Sleep mode not supported on this machine\n");
2302 if (num_online_cpus() > 1 || cpu_is_offline(0))
2305 ret = pmac_suspend_devices();
2307 printk(KERN_ERR "Sleep rejected by devices\n");
2311 /* Stop environment and ADB interrupts */
2312 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
2313 pmu_wait_complete(&req);
2315 /* Tell PMU what events will wake us up */
2316 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
2318 pmu_wait_complete(&req);
2319 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
2320 0, PMU_PWR_WAKEUP_KEY |
2321 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
2322 pmu_wait_complete(&req);
2324 /* Save the state of the L2 and L3 caches */
2325 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
2326 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
2328 if (!__fake_sleep) {
2329 /* Ask the PMU to put us to sleep */
2330 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2331 pmu_wait_complete(&req);
2334 /* The VIA is supposed not to be restored correctly*/
2337 /* Shut down various ASICs. There's a chance that we can no longer
2338 * talk to the PMU after this, so I moved it to _after_ sending the
2339 * sleep command to it. Still need to be checked.
2341 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
2343 /* Call low-level ASM sleep handler */
2347 low_sleep_handler();
2349 /* Restore Apple core ASICs state */
2350 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
2353 restore_via_state();
2355 /* tweak LPJ before cpufreq is there */
2356 loops_per_jiffy *= 2;
2359 pmac_call_early_video_resume();
2361 /* Restore L2 cache */
2362 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
2363 _set_L2CR(save_l2cr);
2364 /* Restore L3 cache */
2365 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
2366 _set_L3CR(save_l3cr);
2368 /* Restore userland MMU context */
2369 set_context(current->active_mm->context.id, current->active_mm->pgd);
2371 /* Tell PMU we are ready */
2373 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2374 pmu_wait_complete(&req);
2375 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
2376 pmu_wait_complete(&req);
2378 /* Restore LPJ, cpufreq will adjust the cpu frequency */
2379 loops_per_jiffy /= 2;
2381 pmac_wakeup_devices();
2386 #define PB3400_MEM_CTRL 0xf8000000
2387 #define PB3400_MEM_CTRL_SLEEP 0x70
2390 powerbook_sleep_3400(void)
2395 struct adb_request sleep_req;
2396 void __iomem *mem_ctrl;
2397 unsigned int __iomem *mem_ctrl_sleep;
2399 /* first map in the memory controller registers */
2400 mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
2401 if (mem_ctrl == NULL) {
2402 printk("powerbook_sleep_3400: ioremap failed\n");
2405 mem_ctrl_sleep = mem_ctrl + PB3400_MEM_CTRL_SLEEP;
2407 /* Allocate room for PCI save */
2408 pbook_alloc_pci_save();
2410 ret = pmac_suspend_devices();
2412 pbook_free_pci_save();
2413 printk(KERN_ERR "Sleep rejected by devices\n");
2417 /* Save the state of PCI config space for some slots */
2420 /* Set the memory controller to keep the memory refreshed
2421 while we're asleep */
2422 for (i = 0x403f; i >= 0x4000; --i) {
2423 out_be32(mem_ctrl_sleep, i);
2425 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
2431 /* Ask the PMU to put us to sleep */
2432 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
2433 while (!sleep_req.complete)
2436 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
2438 /* displacement-flush the L2 cache - necessary? */
2439 for (p = KERNELBASE; p < KERNELBASE + 0x100000; p += 0x1000)
2440 i = *(volatile int *)p;
2443 /* Put the CPU into sleep mode */
2444 hid0 = mfspr(SPRN_HID0);
2445 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
2446 mtspr(SPRN_HID0, hid0);
2447 mtmsr(mfmsr() | MSR_POW | MSR_EE);
2450 /* OK, we're awake again, start restoring things */
2451 out_be32(mem_ctrl_sleep, 0x3f);
2452 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
2453 pbook_pci_restore();
2456 /* wait for the PMU interrupt sequence to complete */
2460 pmac_wakeup_devices();
2461 pbook_free_pci_save();
2467 #endif /* CONFIG_PM && CONFIG_PPC32 */
2470 * Support for /dev/pmu device
2472 #define RB_SIZE 0x10
2473 struct pmu_private {
2474 struct list_head list;
2479 unsigned char data[16];
2481 wait_queue_head_t wait;
2483 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2484 int backlight_locker;
2485 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2488 static LIST_HEAD(all_pmu_pvt);
2489 static DEFINE_SPINLOCK(all_pvt_lock);
2492 pmu_pass_intr(unsigned char *data, int len)
2494 struct pmu_private *pp;
2495 struct list_head *list;
2497 unsigned long flags;
2499 if (len > sizeof(pp->rb_buf[0].data))
2500 len = sizeof(pp->rb_buf[0].data);
2501 spin_lock_irqsave(&all_pvt_lock, flags);
2502 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2503 pp = list_entry(list, struct pmu_private, list);
2504 spin_lock(&pp->lock);
2508 if (i != pp->rb_get) {
2509 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2511 memcpy(rp->data, data, len);
2513 wake_up_interruptible(&pp->wait);
2515 spin_unlock(&pp->lock);
2517 spin_unlock_irqrestore(&all_pvt_lock, flags);
2521 pmu_open(struct inode *inode, struct file *file)
2523 struct pmu_private *pp;
2524 unsigned long flags;
2526 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2529 pp->rb_get = pp->rb_put = 0;
2530 spin_lock_init(&pp->lock);
2531 init_waitqueue_head(&pp->wait);
2532 spin_lock_irqsave(&all_pvt_lock, flags);
2533 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2534 pp->backlight_locker = 0;
2535 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2536 list_add(&pp->list, &all_pmu_pvt);
2537 spin_unlock_irqrestore(&all_pvt_lock, flags);
2538 file->private_data = pp;
2543 pmu_read(struct file *file, char __user *buf,
2544 size_t count, loff_t *ppos)
2546 struct pmu_private *pp = file->private_data;
2547 DECLARE_WAITQUEUE(wait, current);
2548 unsigned long flags;
2551 if (count < 1 || pp == 0)
2553 if (!access_ok(VERIFY_WRITE, buf, count))
2556 spin_lock_irqsave(&pp->lock, flags);
2557 add_wait_queue(&pp->wait, &wait);
2558 current->state = TASK_INTERRUPTIBLE;
2562 if (pp->rb_get != pp->rb_put) {
2564 struct rb_entry *rp = &pp->rb_buf[i];
2566 spin_unlock_irqrestore(&pp->lock, flags);
2569 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2573 spin_lock_irqsave(&pp->lock, flags);
2578 if (file->f_flags & O_NONBLOCK)
2581 if (signal_pending(current))
2583 spin_unlock_irqrestore(&pp->lock, flags);
2585 spin_lock_irqsave(&pp->lock, flags);
2587 current->state = TASK_RUNNING;
2588 remove_wait_queue(&pp->wait, &wait);
2589 spin_unlock_irqrestore(&pp->lock, flags);
2595 pmu_write(struct file *file, const char __user *buf,
2596 size_t count, loff_t *ppos)
2602 pmu_fpoll(struct file *filp, poll_table *wait)
2604 struct pmu_private *pp = filp->private_data;
2605 unsigned int mask = 0;
2606 unsigned long flags;
2610 poll_wait(filp, &pp->wait, wait);
2611 spin_lock_irqsave(&pp->lock, flags);
2612 if (pp->rb_get != pp->rb_put)
2614 spin_unlock_irqrestore(&pp->lock, flags);
2619 pmu_release(struct inode *inode, struct file *file)
2621 struct pmu_private *pp = file->private_data;
2622 unsigned long flags;
2626 file->private_data = NULL;
2627 spin_lock_irqsave(&all_pvt_lock, flags);
2628 list_del(&pp->list);
2629 spin_unlock_irqrestore(&all_pvt_lock, flags);
2630 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2631 if (pp->backlight_locker) {
2632 spin_lock_irqsave(&pmu_lock, flags);
2633 disable_kernel_backlight--;
2634 spin_unlock_irqrestore(&pmu_lock, flags);
2636 #endif /* defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT) */
2644 pmu_ioctl(struct inode * inode, struct file *filp,
2645 u_int cmd, u_long arg)
2647 __u32 __user *argp = (__u32 __user *)arg;
2648 int error = -EINVAL;
2651 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2653 if (!capable(CAP_SYS_ADMIN))
2655 if (sleep_in_progress)
2657 sleep_in_progress = 1;
2659 case PMU_OHARE_BASED:
2660 error = powerbook_sleep_3400();
2662 case PMU_HEATHROW_BASED:
2663 case PMU_PADDINGTON_BASED:
2664 error = powerbook_sleep_grackle();
2666 case PMU_KEYLARGO_BASED:
2667 error = powerbook_sleep_Core99();
2672 sleep_in_progress = 0;
2674 case PMU_IOC_CAN_SLEEP:
2675 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0)
2676 return put_user(0, argp);
2678 return put_user(1, argp);
2679 #endif /* CONFIG_PM && CONFIG_PPC32 */
2681 #ifdef CONFIG_PMAC_BACKLIGHT
2682 /* Backlight should have its own device or go via
2685 case PMU_IOC_GET_BACKLIGHT:
2686 if (sleep_in_progress)
2688 error = get_backlight_level();
2691 return put_user(error, argp);
2692 case PMU_IOC_SET_BACKLIGHT:
2695 if (sleep_in_progress)
2697 error = get_user(value, argp);
2699 error = set_backlight_level(value);
2702 #ifdef CONFIG_INPUT_ADBHID
2703 case PMU_IOC_GRAB_BACKLIGHT: {
2704 struct pmu_private *pp = filp->private_data;
2705 unsigned long flags;
2707 if (pp->backlight_locker)
2709 pp->backlight_locker = 1;
2710 spin_lock_irqsave(&pmu_lock, flags);
2711 disable_kernel_backlight++;
2712 spin_unlock_irqrestore(&pmu_lock, flags);
2715 #endif /* CONFIG_INPUT_ADBHID */
2716 #endif /* CONFIG_PMAC_BACKLIGHT */
2717 case PMU_IOC_GET_MODEL:
2718 return put_user(pmu_kind, argp);
2719 case PMU_IOC_HAS_ADB:
2720 return put_user(pmu_has_adb, argp);
2725 static struct file_operations pmu_device_fops = {
2731 .release = pmu_release,
2734 static struct miscdevice pmu_device = {
2735 PMU_MINOR, "pmu", &pmu_device_fops
2738 static int pmu_device_init(void)
2742 if (misc_register(&pmu_device) < 0)
2743 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
2746 device_initcall(pmu_device_init);
2751 polled_handshake(volatile unsigned char __iomem *via)
2753 via[B] &= ~TREQ; eieio();
2754 while ((via[B] & TACK) != 0)
2756 via[B] |= TREQ; eieio();
2757 while ((via[B] & TACK) == 0)
2762 polled_send_byte(volatile unsigned char __iomem *via, int x)
2764 via[ACR] |= SR_OUT | SR_EXT; eieio();
2765 via[SR] = x; eieio();
2766 polled_handshake(via);
2770 polled_recv_byte(volatile unsigned char __iomem *via)
2774 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2775 x = via[SR]; eieio();
2776 polled_handshake(via);
2777 x = via[SR]; eieio();
2782 pmu_polled_request(struct adb_request *req)
2784 unsigned long flags;
2786 volatile unsigned char __iomem *v = via;
2790 l = pmu_data_len[c][0];
2791 if (l >= 0 && req->nbytes != l + 1)
2794 local_irq_save(flags);
2795 while (pmu_state != idle)
2798 while ((via[B] & TACK) == 0)
2800 polled_send_byte(v, c);
2802 l = req->nbytes - 1;
2803 polled_send_byte(v, l);
2805 for (i = 1; i <= l; ++i)
2806 polled_send_byte(v, req->data[i]);
2808 l = pmu_data_len[c][1];
2810 l = polled_recv_byte(v);
2811 for (i = 0; i < l; ++i)
2812 req->reply[i + req->reply_len] = polled_recv_byte(v);
2817 local_irq_restore(flags);
2820 #endif /* DEBUG_SLEEP */
2823 /* FIXME: This is a temporary set of callbacks to enable us
2824 * to do suspend-to-disk.
2827 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2829 static int pmu_sys_suspended = 0;
2831 static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
2833 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
2836 /* Suspend PMU event interrupts */
2839 pmu_sys_suspended = 1;
2843 static int pmu_sys_resume(struct sys_device *sysdev)
2845 struct adb_request req;
2847 if (!pmu_sys_suspended)
2850 /* Tell PMU we are ready */
2851 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2852 pmu_wait_complete(&req);
2854 /* Resume PMU event interrupts */
2857 pmu_sys_suspended = 0;
2862 #endif /* CONFIG_PM && CONFIG_PPC32 */
2864 static struct sysdev_class pmu_sysclass = {
2865 set_kset_name("pmu"),
2868 static struct sys_device device_pmu = {
2870 .cls = &pmu_sysclass,
2873 static struct sysdev_driver driver_pmu = {
2874 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2875 .suspend = &pmu_sys_suspend,
2876 .resume = &pmu_sys_resume,
2877 #endif /* CONFIG_PM && CONFIG_PPC32 */
2880 static int __init init_pmu_sysfs(void)
2884 rc = sysdev_class_register(&pmu_sysclass);
2886 printk(KERN_ERR "Failed registering PMU sys class\n");
2889 rc = sysdev_register(&device_pmu);
2891 printk(KERN_ERR "Failed registering PMU sys device\n");
2894 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2896 printk(KERN_ERR "Failed registering PMU sys driver\n");
2902 subsys_initcall(init_pmu_sysfs);
2904 EXPORT_SYMBOL(pmu_request);
2905 EXPORT_SYMBOL(pmu_queue_request);
2906 EXPORT_SYMBOL(pmu_poll);
2907 EXPORT_SYMBOL(pmu_poll_adb);
2908 EXPORT_SYMBOL(pmu_wait_complete);
2909 EXPORT_SYMBOL(pmu_suspend);
2910 EXPORT_SYMBOL(pmu_resume);
2911 EXPORT_SYMBOL(pmu_unlock);
2912 #if defined(CONFIG_PM) && defined(CONFIG_PPC32)
2913 EXPORT_SYMBOL(pmu_enable_irled);
2914 EXPORT_SYMBOL(pmu_battery_count);
2915 EXPORT_SYMBOL(pmu_batteries);
2916 EXPORT_SYMBOL(pmu_power_flags);
2917 #endif /* CONFIG_PM && CONFIG_PPC32 */