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
13 * Copyright (C) 2006-2007 Johannes Berg
15 * THIS DRIVER IS BECOMING A TOTAL MESS !
16 * - Cleanup atomically disabling reply to PMU events after
17 * a sleep or a freq. switch
21 #include <linux/mutex.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/kernel.h>
25 #include <linux/delay.h>
26 #include <linux/sched.h>
27 #include <linux/miscdevice.h>
28 #include <linux/blkdev.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31 #include <linux/poll.h>
32 #include <linux/adb.h>
33 #include <linux/pmu.h>
34 #include <linux/cuda.h>
35 #include <linux/module.h>
36 #include <linux/spinlock.h>
38 #include <linux/proc_fs.h>
39 #include <linux/seq_file.h>
40 #include <linux/init.h>
41 #include <linux/interrupt.h>
42 #include <linux/device.h>
43 #include <linux/syscore_ops.h>
44 #include <linux/freezer.h>
45 #include <linux/syscalls.h>
46 #include <linux/suspend.h>
47 #include <linux/cpu.h>
48 #include <linux/compat.h>
49 #include <linux/of_address.h>
50 #include <linux/of_irq.h>
52 #include <asm/machdep.h>
54 #include <asm/pgtable.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 #include <asm/backlight.h>
66 #include "via-pmu-event.h"
68 /* Some compile options */
71 /* Misc minor number allocated for /dev/pmu */
74 /* How many iterations between battery polls */
75 #define BATTERY_POLLING_COUNT 2
77 static DEFINE_MUTEX(pmu_info_proc_mutex
);
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_SUSPEND) && defined(CONFIG_PPC32)
156 static int option_lid_wakeup
= 1;
157 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
158 static unsigned long async_req_locks
;
159 static unsigned int pmu_irq_stats
[11];
161 static struct proc_dir_entry
*proc_pmu_root
;
162 static struct proc_dir_entry
*proc_pmu_info
;
163 static struct proc_dir_entry
*proc_pmu_irqstats
;
164 static struct proc_dir_entry
*proc_pmu_options
;
165 static int option_server_mode
;
167 int pmu_battery_count
;
169 unsigned int pmu_power_flags
= PMU_PWR_AC_PRESENT
;
170 struct pmu_battery_info pmu_batteries
[PMU_MAX_BATTERIES
];
171 static int query_batt_timer
= BATTERY_POLLING_COUNT
;
172 static struct adb_request batt_req
;
173 static struct proc_dir_entry
*proc_pmu_batt
[PMU_MAX_BATTERIES
];
179 static int adb_dev_map
;
180 static int pmu_adb_flags
;
182 static int pmu_probe(void);
183 static int pmu_init(void);
184 static int pmu_send_request(struct adb_request
*req
, int sync
);
185 static int pmu_adb_autopoll(int devs
);
186 static int pmu_adb_reset_bus(void);
187 #endif /* CONFIG_ADB */
189 static int init_pmu(void);
190 static void pmu_start(void);
191 static irqreturn_t
via_pmu_interrupt(int irq
, void *arg
);
192 static irqreturn_t
gpio1_interrupt(int irq
, void *arg
);
193 static const struct file_operations pmu_info_proc_fops
;
194 static const struct file_operations pmu_irqstats_proc_fops
;
195 static void pmu_pass_intr(unsigned char *data
, int len
);
196 static const struct file_operations pmu_battery_proc_fops
;
197 static const struct file_operations pmu_options_proc_fops
;
200 struct adb_driver via_pmu_driver
= {
209 #endif /* CONFIG_ADB */
211 extern void low_sleep_handler(void);
212 extern void enable_kernel_altivec(void);
213 extern void enable_kernel_fp(void);
216 int pmu_polled_request(struct adb_request
*req
);
217 void pmu_blink(int n
);
221 * This table indicates for each PMU opcode:
222 * - the number of data bytes to be sent with the command, or -1
223 * if a length byte should be sent,
224 * - the number of response bytes which the PMU will return, or
225 * -1 if it will send a length byte.
227 static const s8 pmu_data_len
[256][2] = {
228 /* 0 1 2 3 4 5 6 7 */
229 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
230 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
231 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
232 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
233 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
234 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
235 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
236 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
237 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
238 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
239 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
240 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
241 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
242 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
243 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
244 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
245 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
246 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
247 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
248 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
249 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
250 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
251 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
252 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
253 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
255 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
257 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
258 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
259 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
260 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
263 static char *pbook_type
[] = {
265 "PowerBook 2400/3400/3500(G3)",
266 "PowerBook G3 Series",
271 int __init
find_via_pmu(void)
278 vias
= of_find_node_by_name(NULL
, "via-pmu");
282 reg
= of_get_property(vias
, "reg", NULL
);
284 printk(KERN_ERR
"via-pmu: No \"reg\" property !\n");
287 taddr
= of_translate_address(vias
, reg
);
288 if (taddr
== OF_BAD_ADDR
) {
289 printk(KERN_ERR
"via-pmu: Can't translate address !\n");
293 spin_lock_init(&pmu_lock
);
297 pmu_intr_mask
= PMU_INT_PCEJECT
|
302 if (vias
->parent
->name
&& ((strcmp(vias
->parent
->name
, "ohare") == 0)
303 || of_device_is_compatible(vias
->parent
, "ohare")))
304 pmu_kind
= PMU_OHARE_BASED
;
305 else if (of_device_is_compatible(vias
->parent
, "paddington"))
306 pmu_kind
= PMU_PADDINGTON_BASED
;
307 else if (of_device_is_compatible(vias
->parent
, "heathrow"))
308 pmu_kind
= PMU_HEATHROW_BASED
;
309 else if (of_device_is_compatible(vias
->parent
, "Keylargo")
310 || of_device_is_compatible(vias
->parent
, "K2-Keylargo")) {
311 struct device_node
*gpiop
;
312 struct device_node
*adbp
;
313 u64 gaddr
= OF_BAD_ADDR
;
315 pmu_kind
= PMU_KEYLARGO_BASED
;
316 adbp
= of_find_node_by_type(NULL
, "adb");
317 pmu_has_adb
= (adbp
!= NULL
);
319 pmu_intr_mask
= PMU_INT_PCEJECT
|
325 gpiop
= of_find_node_by_name(NULL
, "gpio");
327 reg
= of_get_property(gpiop
, "reg", NULL
);
329 gaddr
= of_translate_address(gpiop
, reg
);
330 if (gaddr
!= OF_BAD_ADDR
)
331 gpio_reg
= ioremap(gaddr
, 0x10);
333 if (gpio_reg
== NULL
) {
334 printk(KERN_ERR
"via-pmu: Can't find GPIO reg !\n");
338 pmu_kind
= PMU_UNKNOWN
;
340 via
= ioremap(taddr
, 0x2000);
342 printk(KERN_ERR
"via-pmu: Can't map address !\n");
346 out_8(&via
[IER
], IER_CLR
| 0x7f); /* disable all intrs */
347 out_8(&via
[IFR
], 0x7f); /* clear IFR */
356 printk(KERN_INFO
"PMU driver v%d initialized for %s, firmware: %02x\n",
357 PMU_DRIVER_VERSION
, pbook_type
[pmu_kind
], pmu_version
);
359 sys_ctrler
= SYS_CTRLER_PMU
;
372 static int pmu_probe(void)
374 return vias
== NULL
? -ENODEV
: 0;
377 static int __init
pmu_init(void)
383 #endif /* CONFIG_ADB */
386 * We can't wait until pmu_init gets called, that happens too late.
387 * It happens after IDE and SCSI initialization, which can take a few
388 * seconds, and by that time the PMU could have given up on us and
390 * Thus this is called with arch_initcall rather than device_initcall.
392 static int __init
via_pmu_start(void)
399 batt_req
.complete
= 1;
401 irq
= irq_of_parse_and_map(vias
, 0);
403 printk(KERN_ERR
"via-pmu: can't map interrupt\n");
406 /* We set IRQF_NO_SUSPEND because we don't want the interrupt
407 * to be disabled between the 2 passes of driver suspend, we
408 * control our own disabling for that one
410 if (request_irq(irq
, via_pmu_interrupt
, IRQF_NO_SUSPEND
,
411 "VIA-PMU", (void *)0)) {
412 printk(KERN_ERR
"via-pmu: can't request irq %d\n", irq
);
416 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
417 gpio_node
= of_find_node_by_name(NULL
, "extint-gpio1");
418 if (gpio_node
== NULL
)
419 gpio_node
= of_find_node_by_name(NULL
,
422 gpio_irq
= irq_of_parse_and_map(gpio_node
, 0);
424 if (gpio_irq
!= NO_IRQ
) {
425 if (request_irq(gpio_irq
, gpio1_interrupt
, IRQF_TIMER
,
426 "GPIO1 ADB", (void *)0))
427 printk(KERN_ERR
"pmu: can't get irq %d"
428 " (GPIO1)\n", gpio_irq
);
430 gpio_irq_enabled
= 1;
434 /* Enable interrupts */
435 out_8(&via
[IER
], IER_SET
| SR_INT
| CB1_INT
);
437 pmu_fully_inited
= 1;
439 /* Make sure PMU settle down before continuing. This is _very_ important
440 * since the IDE probe may shut interrupts down for quite a bit of time. If
441 * a PMU communication is pending while this happens, the PMU may timeout
442 * Not that on Core99 machines, the PMU keeps sending us environement
443 * messages, we should find a way to either fix IDE or make it call
444 * pmu_suspend() before masking interrupts. This can also happens while
445 * scolling with some fbdevs.
449 } while (pmu_state
!= idle
);
454 arch_initcall(via_pmu_start
);
457 * This has to be done after pci_init, which is a subsys_initcall.
459 static int __init
via_pmu_dev_init(void)
464 #ifdef CONFIG_PMAC_BACKLIGHT
465 /* Initialize backlight */
466 pmu_backlight_init();
470 if (of_machine_is_compatible("AAPL,3400/2400") ||
471 of_machine_is_compatible("AAPL,3500")) {
472 int mb
= pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
473 NULL
, PMAC_MB_INFO_MODEL
, 0);
474 pmu_battery_count
= 1;
475 if (mb
== PMAC_TYPE_COMET
)
476 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_COMET
;
478 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_HOOPER
;
479 } else if (of_machine_is_compatible("AAPL,PowerBook1998") ||
480 of_machine_is_compatible("PowerBook1,1")) {
481 pmu_battery_count
= 2;
482 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_SMART
;
483 pmu_batteries
[1].flags
|= PMU_BATT_TYPE_SMART
;
485 struct device_node
* prim
=
486 of_find_node_by_name(NULL
, "power-mgt");
487 const u32
*prim_info
= NULL
;
489 prim_info
= of_get_property(prim
, "prim-info", NULL
);
491 /* Other stuffs here yet unknown */
492 pmu_battery_count
= (prim_info
[6] >> 16) & 0xff;
493 pmu_batteries
[0].flags
|= PMU_BATT_TYPE_SMART
;
494 if (pmu_battery_count
> 1)
495 pmu_batteries
[1].flags
|= PMU_BATT_TYPE_SMART
;
499 #endif /* CONFIG_PPC32 */
501 /* Create /proc/pmu */
502 proc_pmu_root
= proc_mkdir("pmu", NULL
);
506 for (i
=0; i
<pmu_battery_count
; i
++) {
508 sprintf(title
, "battery_%ld", i
);
509 proc_pmu_batt
[i
] = proc_create_data(title
, 0, proc_pmu_root
,
510 &pmu_battery_proc_fops
, (void *)i
);
513 proc_pmu_info
= proc_create("info", 0, proc_pmu_root
, &pmu_info_proc_fops
);
514 proc_pmu_irqstats
= proc_create("interrupts", 0, proc_pmu_root
,
515 &pmu_irqstats_proc_fops
);
516 proc_pmu_options
= proc_create("options", 0600, proc_pmu_root
,
517 &pmu_options_proc_fops
);
522 device_initcall(via_pmu_dev_init
);
528 struct adb_request req
;
530 out_8(&via
[B
], via
[B
] | TREQ
); /* negate TREQ */
531 out_8(&via
[DIRB
], (via
[DIRB
] | TREQ
) & ~TACK
); /* TACK in, TREQ out */
533 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
535 while (!req
.complete
) {
537 printk(KERN_ERR
"init_pmu: no response from PMU\n");
544 /* ack all pending interrupts */
546 interrupt_data
[0][0] = 1;
547 while (interrupt_data
[0][0] || pmu_state
!= idle
) {
549 printk(KERN_ERR
"init_pmu: timed out acking intrs\n");
552 if (pmu_state
== idle
)
554 via_pmu_interrupt(0, NULL
);
558 /* Tell PMU we are ready. */
559 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
560 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
561 while (!req
.complete
)
565 /* Read PMU version */
566 pmu_request(&req
, NULL
, 1, PMU_GET_VERSION
);
567 pmu_wait_complete(&req
);
568 if (req
.reply_len
> 0)
569 pmu_version
= req
.reply
[0];
571 /* Read server mode setting */
572 if (pmu_kind
== PMU_KEYLARGO_BASED
) {
573 pmu_request(&req
, NULL
, 2, PMU_POWER_EVENTS
,
574 PMU_PWR_GET_POWERUP_EVENTS
);
575 pmu_wait_complete(&req
);
576 if (req
.reply_len
== 2) {
577 if (req
.reply
[1] & PMU_PWR_WAKEUP_AC_INSERT
)
578 option_server_mode
= 1;
579 printk(KERN_INFO
"via-pmu: Server Mode is %s\n",
580 option_server_mode
? "enabled" : "disabled");
592 static void pmu_set_server_mode(int server_mode
)
594 struct adb_request req
;
596 if (pmu_kind
!= PMU_KEYLARGO_BASED
)
599 option_server_mode
= server_mode
;
600 pmu_request(&req
, NULL
, 2, PMU_POWER_EVENTS
, PMU_PWR_GET_POWERUP_EVENTS
);
601 pmu_wait_complete(&req
);
602 if (req
.reply_len
< 2)
605 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
,
606 PMU_PWR_SET_POWERUP_EVENTS
,
607 req
.reply
[0], PMU_PWR_WAKEUP_AC_INSERT
);
609 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
,
610 PMU_PWR_CLR_POWERUP_EVENTS
,
611 req
.reply
[0], PMU_PWR_WAKEUP_AC_INSERT
);
612 pmu_wait_complete(&req
);
615 /* This new version of the code for 2400/3400/3500 powerbooks
616 * is inspired from the implementation in gkrellm-pmu
619 done_battery_state_ohare(struct adb_request
* req
)
623 * 0x01 : AC indicator
625 * 0x04 : battery exist
628 * 0x20 : full charged
629 * 0x40 : pcharge reset
630 * 0x80 : battery exist
632 * [1][2] : battery voltage
633 * [3] : CPU temperature
634 * [4] : battery temperature
639 unsigned int bat_flags
= PMU_BATT_TYPE_HOOPER
;
640 long pcharge
, charge
, vb
, vmax
, lmax
;
641 long vmax_charging
, vmax_charged
;
642 long amperage
, voltage
, time
, max
;
643 int mb
= pmac_call_feature(PMAC_FTR_GET_MB_INFO
,
644 NULL
, PMAC_MB_INFO_MODEL
, 0);
646 if (req
->reply
[0] & 0x01)
647 pmu_power_flags
|= PMU_PWR_AC_PRESENT
;
649 pmu_power_flags
&= ~PMU_PWR_AC_PRESENT
;
651 if (mb
== PMAC_TYPE_COMET
) {
662 /* If battery installed */
663 if (req
->reply
[0] & 0x04) {
664 bat_flags
|= PMU_BATT_PRESENT
;
665 if (req
->reply
[0] & 0x02)
666 bat_flags
|= PMU_BATT_CHARGING
;
667 vb
= (req
->reply
[1] << 8) | req
->reply
[2];
668 voltage
= (vb
* 265 + 72665) / 10;
669 amperage
= req
->reply
[5];
670 if ((req
->reply
[0] & 0x01) == 0) {
672 vb
+= ((amperage
- 200) * 15)/100;
673 } else if (req
->reply
[0] & 0x02) {
674 vb
= (vb
* 97) / 100;
675 vmax
= vmax_charging
;
677 charge
= (100 * vb
) / vmax
;
678 if (req
->reply
[0] & 0x40) {
679 pcharge
= (req
->reply
[6] << 8) + req
->reply
[7];
683 pcharge
= 100 - pcharge
/ lmax
;
684 if (pcharge
< charge
)
688 time
= (charge
* 16440) / amperage
;
692 amperage
= -amperage
;
694 charge
= max
= amperage
= voltage
= time
= 0;
696 pmu_batteries
[pmu_cur_battery
].flags
= bat_flags
;
697 pmu_batteries
[pmu_cur_battery
].charge
= charge
;
698 pmu_batteries
[pmu_cur_battery
].max_charge
= max
;
699 pmu_batteries
[pmu_cur_battery
].amperage
= amperage
;
700 pmu_batteries
[pmu_cur_battery
].voltage
= voltage
;
701 pmu_batteries
[pmu_cur_battery
].time_remaining
= time
;
703 clear_bit(0, &async_req_locks
);
707 done_battery_state_smart(struct adb_request
* req
)
710 * [0] : format of this structure (known: 3,4,5)
723 * [4][5] : max charge
728 unsigned int bat_flags
= PMU_BATT_TYPE_SMART
;
730 unsigned int capa
, max
, voltage
;
732 if (req
->reply
[1] & 0x01)
733 pmu_power_flags
|= PMU_PWR_AC_PRESENT
;
735 pmu_power_flags
&= ~PMU_PWR_AC_PRESENT
;
738 capa
= max
= amperage
= voltage
= 0;
740 if (req
->reply
[1] & 0x04) {
741 bat_flags
|= PMU_BATT_PRESENT
;
742 switch(req
->reply
[0]) {
744 case 4: capa
= req
->reply
[2];
746 amperage
= *((signed char *)&req
->reply
[4]);
747 voltage
= req
->reply
[5];
749 case 5: capa
= (req
->reply
[2] << 8) | req
->reply
[3];
750 max
= (req
->reply
[4] << 8) | req
->reply
[5];
751 amperage
= *((signed short *)&req
->reply
[6]);
752 voltage
= (req
->reply
[8] << 8) | req
->reply
[9];
755 pr_warn("pmu.c: unrecognized battery info, "
756 "len: %d, %4ph\n", req
->reply_len
,
762 if ((req
->reply
[1] & 0x01) && (amperage
> 0))
763 bat_flags
|= PMU_BATT_CHARGING
;
765 pmu_batteries
[pmu_cur_battery
].flags
= bat_flags
;
766 pmu_batteries
[pmu_cur_battery
].charge
= capa
;
767 pmu_batteries
[pmu_cur_battery
].max_charge
= max
;
768 pmu_batteries
[pmu_cur_battery
].amperage
= amperage
;
769 pmu_batteries
[pmu_cur_battery
].voltage
= voltage
;
771 if ((req
->reply
[1] & 0x01) && (amperage
> 0))
772 pmu_batteries
[pmu_cur_battery
].time_remaining
773 = ((max
-capa
) * 3600) / amperage
;
775 pmu_batteries
[pmu_cur_battery
].time_remaining
776 = (capa
* 3600) / (-amperage
);
778 pmu_batteries
[pmu_cur_battery
].time_remaining
= 0;
780 pmu_cur_battery
= (pmu_cur_battery
+ 1) % pmu_battery_count
;
782 clear_bit(0, &async_req_locks
);
786 query_battery_state(void)
788 if (test_and_set_bit(0, &async_req_locks
))
790 if (pmu_kind
== PMU_OHARE_BASED
)
791 pmu_request(&batt_req
, done_battery_state_ohare
,
792 1, PMU_BATTERY_STATE
);
794 pmu_request(&batt_req
, done_battery_state_smart
,
795 2, PMU_SMART_BATTERY_STATE
, pmu_cur_battery
+1);
798 static int pmu_info_proc_show(struct seq_file
*m
, void *v
)
800 seq_printf(m
, "PMU driver version : %d\n", PMU_DRIVER_VERSION
);
801 seq_printf(m
, "PMU firmware version : %02x\n", pmu_version
);
802 seq_printf(m
, "AC Power : %d\n",
803 ((pmu_power_flags
& PMU_PWR_AC_PRESENT
) != 0) || pmu_battery_count
== 0);
804 seq_printf(m
, "Battery count : %d\n", pmu_battery_count
);
809 static int pmu_info_proc_open(struct inode
*inode
, struct file
*file
)
811 return single_open(file
, pmu_info_proc_show
, NULL
);
814 static const struct file_operations pmu_info_proc_fops
= {
815 .owner
= THIS_MODULE
,
816 .open
= pmu_info_proc_open
,
819 .release
= single_release
,
822 static int pmu_irqstats_proc_show(struct seq_file
*m
, void *v
)
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 seq_printf(m
, " %2u: %10u (%s)\n",
841 i
, pmu_irq_stats
[i
], irq_names
[i
]);
846 static int pmu_irqstats_proc_open(struct inode
*inode
, struct file
*file
)
848 return single_open(file
, pmu_irqstats_proc_show
, NULL
);
851 static const struct file_operations pmu_irqstats_proc_fops
= {
852 .owner
= THIS_MODULE
,
853 .open
= pmu_irqstats_proc_open
,
856 .release
= single_release
,
859 static int pmu_battery_proc_show(struct seq_file
*m
, void *v
)
861 long batnum
= (long)m
->private;
864 seq_printf(m
, "flags : %08x\n", pmu_batteries
[batnum
].flags
);
865 seq_printf(m
, "charge : %d\n", pmu_batteries
[batnum
].charge
);
866 seq_printf(m
, "max_charge : %d\n", pmu_batteries
[batnum
].max_charge
);
867 seq_printf(m
, "current : %d\n", pmu_batteries
[batnum
].amperage
);
868 seq_printf(m
, "voltage : %d\n", pmu_batteries
[batnum
].voltage
);
869 seq_printf(m
, "time rem. : %d\n", pmu_batteries
[batnum
].time_remaining
);
873 static int pmu_battery_proc_open(struct inode
*inode
, struct file
*file
)
875 return single_open(file
, pmu_battery_proc_show
, PDE_DATA(inode
));
878 static const struct file_operations pmu_battery_proc_fops
= {
879 .owner
= THIS_MODULE
,
880 .open
= pmu_battery_proc_open
,
883 .release
= single_release
,
886 static int pmu_options_proc_show(struct seq_file
*m
, void *v
)
888 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
889 if (pmu_kind
== PMU_KEYLARGO_BASED
&&
890 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) >= 0)
891 seq_printf(m
, "lid_wakeup=%d\n", option_lid_wakeup
);
893 if (pmu_kind
== PMU_KEYLARGO_BASED
)
894 seq_printf(m
, "server_mode=%d\n", option_server_mode
);
899 static int pmu_options_proc_open(struct inode
*inode
, struct file
*file
)
901 return single_open(file
, pmu_options_proc_show
, NULL
);
904 static ssize_t
pmu_options_proc_write(struct file
*file
,
905 const char __user
*buffer
, size_t count
, loff_t
*pos
)
909 size_t fcount
= count
;
915 if (copy_from_user(tmp
, buffer
, count
))
923 while(*val
&& (*val
!= '=')) {
933 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
934 if (pmu_kind
== PMU_KEYLARGO_BASED
&&
935 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) >= 0)
936 if (!strcmp(label
, "lid_wakeup"))
937 option_lid_wakeup
= ((*val
) == '1');
939 if (pmu_kind
== PMU_KEYLARGO_BASED
&& !strcmp(label
, "server_mode")) {
941 new_value
= ((*val
) == '1');
942 if (new_value
!= option_server_mode
)
943 pmu_set_server_mode(new_value
);
948 static const struct file_operations pmu_options_proc_fops
= {
949 .owner
= THIS_MODULE
,
950 .open
= pmu_options_proc_open
,
953 .release
= single_release
,
954 .write
= pmu_options_proc_write
,
958 /* Send an ADB command */
959 static int pmu_send_request(struct adb_request
*req
, int sync
)
963 if ((vias
== NULL
) || (!pmu_fully_inited
)) {
970 switch (req
->data
[0]) {
972 for (i
= 0; i
< req
->nbytes
- 1; ++i
)
973 req
->data
[i
] = req
->data
[i
+1];
975 if (pmu_data_len
[req
->data
[0]][1] != 0) {
976 req
->reply
[0] = ADB_RET_OK
;
980 ret
= pmu_queue_request(req
);
983 switch (req
->data
[1]) {
985 if (req
->nbytes
!= 2)
987 req
->data
[0] = PMU_READ_RTC
;
990 req
->reply
[0] = CUDA_PACKET
;
992 req
->reply
[2] = CUDA_GET_TIME
;
993 ret
= pmu_queue_request(req
);
996 if (req
->nbytes
!= 6)
998 req
->data
[0] = PMU_SET_RTC
;
1000 for (i
= 1; i
<= 4; ++i
)
1001 req
->data
[i
] = req
->data
[i
+1];
1003 req
->reply
[0] = CUDA_PACKET
;
1005 req
->reply
[2] = CUDA_SET_TIME
;
1006 ret
= pmu_queue_request(req
);
1013 for (i
= req
->nbytes
- 1; i
> 1; --i
)
1014 req
->data
[i
+2] = req
->data
[i
];
1015 req
->data
[3] = req
->nbytes
- 2;
1016 req
->data
[2] = pmu_adb_flags
;
1017 /*req->data[1] = req->data[1];*/
1018 req
->data
[0] = PMU_ADB_CMD
;
1020 req
->reply_expected
= 1;
1022 ret
= pmu_queue_request(req
);
1031 while (!req
->complete
)
1037 /* Enable/disable autopolling */
1038 static int __pmu_adb_autopoll(int devs
)
1040 struct adb_request req
;
1043 pmu_request(&req
, NULL
, 5, PMU_ADB_CMD
, 0, 0x86,
1044 adb_dev_map
>> 8, adb_dev_map
);
1047 pmu_request(&req
, NULL
, 1, PMU_ADB_POLL_OFF
);
1050 while (!req
.complete
)
1055 static int pmu_adb_autopoll(int devs
)
1057 if ((vias
== NULL
) || (!pmu_fully_inited
) || !pmu_has_adb
)
1061 return __pmu_adb_autopoll(devs
);
1064 /* Reset the ADB bus */
1065 static int pmu_adb_reset_bus(void)
1067 struct adb_request req
;
1068 int save_autopoll
= adb_dev_map
;
1070 if ((vias
== NULL
) || (!pmu_fully_inited
) || !pmu_has_adb
)
1073 /* anyone got a better idea?? */
1074 __pmu_adb_autopoll(0);
1078 req
.data
[0] = PMU_ADB_CMD
;
1079 req
.data
[1] = ADB_BUSRESET
;
1084 req
.reply_expected
= 1;
1085 if (pmu_queue_request(&req
) != 0) {
1086 printk(KERN_ERR
"pmu_adb_reset_bus: pmu_queue_request failed\n");
1089 pmu_wait_complete(&req
);
1091 if (save_autopoll
!= 0)
1092 __pmu_adb_autopoll(save_autopoll
);
1096 #endif /* CONFIG_ADB */
1098 /* Construct and send a pmu request */
1100 pmu_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
1109 if (nbytes
< 0 || nbytes
> 32) {
1110 printk(KERN_ERR
"pmu_request: bad nbytes (%d)\n", nbytes
);
1114 req
->nbytes
= nbytes
;
1116 va_start(list
, nbytes
);
1117 for (i
= 0; i
< nbytes
; ++i
)
1118 req
->data
[i
] = va_arg(list
, int);
1121 req
->reply_expected
= 0;
1122 return pmu_queue_request(req
);
1126 pmu_queue_request(struct adb_request
*req
)
1128 unsigned long flags
;
1135 if (req
->nbytes
<= 0) {
1139 nsend
= pmu_data_len
[req
->data
[0]][0];
1140 if (nsend
>= 0 && req
->nbytes
!= nsend
+ 1) {
1149 spin_lock_irqsave(&pmu_lock
, flags
);
1150 if (current_req
!= 0) {
1151 last_req
->next
= req
;
1156 if (pmu_state
== idle
)
1159 spin_unlock_irqrestore(&pmu_lock
, flags
);
1167 /* Sightly increased the delay, I had one occurrence of the message
1171 while ((in_8(&via
[B
]) & TACK
) == 0) {
1172 if (--timeout
< 0) {
1173 printk(KERN_ERR
"PMU not responding (!ack)\n");
1180 /* New PMU seems to be very sensitive to those timings, so we make sure
1181 * PCI is flushed immediately */
1185 volatile unsigned char __iomem
*v
= via
;
1187 out_8(&v
[ACR
], in_8(&v
[ACR
]) | SR_OUT
| SR_EXT
);
1189 out_8(&v
[B
], in_8(&v
[B
]) & ~TREQ
); /* assert TREQ */
1196 volatile unsigned char __iomem
*v
= via
;
1198 out_8(&v
[ACR
], (in_8(&v
[ACR
]) & ~SR_OUT
) | SR_EXT
);
1199 in_8(&v
[SR
]); /* resets SR */
1200 out_8(&v
[B
], in_8(&v
[B
]) & ~TREQ
);
1205 pmu_done(struct adb_request
*req
)
1207 void (*done
)(struct adb_request
*) = req
->done
;
1210 /* Here, we assume that if the request has a done member, the
1211 * struct request will survive to setting req->complete to 1
1220 struct adb_request
*req
;
1222 /* assert pmu_state == idle */
1223 /* get the packet to send */
1225 if (req
== 0 || pmu_state
!= idle
1226 || (/*req->reply_expected && */req_awaiting_reply
))
1229 pmu_state
= sending
;
1231 data_len
= pmu_data_len
[req
->data
[0]][0];
1233 /* Sounds safer to make sure ACK is high before writing. This helped
1234 * kill a problem with ADB and some iBooks
1237 /* set the shift register to shift out and send a byte */
1238 send_byte(req
->data
[0]);
1248 via_pmu_interrupt(0, NULL
);
1258 /* Kicks ADB read when PMU is suspended */
1259 adb_int_pending
= 1;
1261 via_pmu_interrupt(0, NULL
);
1262 } while (pmu_suspended
&& (adb_int_pending
|| pmu_state
!= idle
1263 || req_awaiting_reply
));
1267 pmu_wait_complete(struct adb_request
*req
)
1271 while((pmu_state
!= idle
&& pmu_state
!= locked
) || !req
->complete
)
1272 via_pmu_interrupt(0, NULL
);
1275 /* This function loops until the PMU is idle and prevents it from
1276 * anwsering to ADB interrupts. pmu_request can still be called.
1277 * This is done to avoid spurrious shutdowns when we know we'll have
1278 * interrupts switched off for a long time
1283 unsigned long flags
;
1288 spin_lock_irqsave(&pmu_lock
, flags
);
1290 if (pmu_suspended
> 1) {
1291 spin_unlock_irqrestore(&pmu_lock
, flags
);
1296 spin_unlock_irqrestore(&pmu_lock
, flags
);
1297 if (req_awaiting_reply
)
1298 adb_int_pending
= 1;
1299 via_pmu_interrupt(0, NULL
);
1300 spin_lock_irqsave(&pmu_lock
, flags
);
1301 if (!adb_int_pending
&& pmu_state
== idle
&& !req_awaiting_reply
) {
1303 disable_irq_nosync(gpio_irq
);
1304 out_8(&via
[IER
], CB1_INT
| IER_CLR
);
1305 spin_unlock_irqrestore(&pmu_lock
, flags
);
1314 unsigned long flags
;
1316 if (!via
|| (pmu_suspended
< 1))
1319 spin_lock_irqsave(&pmu_lock
, flags
);
1321 if (pmu_suspended
> 0) {
1322 spin_unlock_irqrestore(&pmu_lock
, flags
);
1325 adb_int_pending
= 1;
1327 enable_irq(gpio_irq
);
1328 out_8(&via
[IER
], CB1_INT
| IER_SET
);
1329 spin_unlock_irqrestore(&pmu_lock
, flags
);
1333 /* Interrupt data could be the result data from an ADB cmd */
1335 pmu_handle_data(unsigned char *data
, int len
)
1337 unsigned char ints
, pirq
;
1341 if (drop_interrupts
|| len
< 1) {
1342 adb_int_pending
= 0;
1347 /* Get PMU interrupt mask */
1350 /* Record zero interrupts for stats */
1354 /* Hack to deal with ADB autopoll flag */
1355 if (ints
& PMU_INT_ADB
)
1356 ints
&= ~(PMU_INT_ADB_AUTO
| PMU_INT_AUTO_SRQ_POLL
);
1361 if (i
> pmu_irq_stats
[10])
1362 pmu_irq_stats
[10] = i
;
1366 for (pirq
= 0; pirq
< 8; pirq
++)
1367 if (ints
& (1 << pirq
))
1369 pmu_irq_stats
[pirq
]++;
1371 ints
&= ~(1 << pirq
);
1373 /* Note: for some reason, we get an interrupt with len=1,
1374 * data[0]==0 after each normal ADB interrupt, at least
1375 * on the Pismo. Still investigating... --BenH
1377 if ((1 << pirq
) & PMU_INT_ADB
) {
1378 if ((data
[0] & PMU_INT_ADB_AUTO
) == 0) {
1379 struct adb_request
*req
= req_awaiting_reply
;
1381 printk(KERN_ERR
"PMU: extra ADB reply\n");
1384 req_awaiting_reply
= NULL
;
1388 memcpy(req
->reply
, data
+ 1, len
- 1);
1389 req
->reply_len
= len
- 1;
1393 if (len
== 4 && data
[1] == 0x2c) {
1394 extern int xmon_wants_key
, xmon_adb_keycode
;
1395 if (xmon_wants_key
) {
1396 xmon_adb_keycode
= data
[2];
1402 * XXX On the [23]400 the PMU gives us an up
1403 * event for keycodes 0x74 or 0x75 when the PC
1404 * card eject buttons are released, so we
1405 * ignore those events.
1407 if (!(pmu_kind
== PMU_OHARE_BASED
&& len
== 4
1408 && data
[1] == 0x2c && data
[3] == 0xff
1409 && (data
[2] & ~1) == 0xf4))
1410 adb_input(data
+1, len
-1, 1);
1411 #endif /* CONFIG_ADB */
1414 /* Sound/brightness button pressed */
1415 else if ((1 << pirq
) & PMU_INT_SNDBRT
) {
1416 #ifdef CONFIG_PMAC_BACKLIGHT
1418 pmac_backlight_set_legacy_brightness_pmu(data
[1] >> 4);
1421 /* Tick interrupt */
1422 else if ((1 << pirq
) & PMU_INT_TICK
) {
1423 /* Environement or tick interrupt, query batteries */
1424 if (pmu_battery_count
) {
1425 if ((--query_batt_timer
) == 0) {
1426 query_battery_state();
1427 query_batt_timer
= BATTERY_POLLING_COUNT
;
1431 else if ((1 << pirq
) & PMU_INT_ENVIRONMENT
) {
1432 if (pmu_battery_count
)
1433 query_battery_state();
1434 pmu_pass_intr(data
, len
);
1435 /* len == 6 is probably a bad check. But how do I
1436 * know what PMU versions send what events here? */
1438 via_pmu_event(PMU_EVT_POWER
, !!(data
[1]&8));
1439 via_pmu_event(PMU_EVT_LID
, data
[1]&1);
1442 pmu_pass_intr(data
, len
);
1447 static struct adb_request
*
1450 struct adb_request
*req
;
1453 if (via
[B
] & TREQ
) {
1454 printk(KERN_ERR
"PMU: spurious SR intr (%x)\n", via
[B
]);
1455 out_8(&via
[IFR
], SR_INT
);
1458 /* The ack may not yet be low when we get the interrupt */
1459 while ((in_8(&via
[B
]) & TACK
) != 0)
1462 /* if reading grab the byte, and reset the interrupt */
1463 if (pmu_state
== reading
|| pmu_state
== reading_intr
)
1464 bite
= in_8(&via
[SR
]);
1466 /* reset TREQ and wait for TACK to go high */
1467 out_8(&via
[B
], in_8(&via
[B
]) | TREQ
);
1470 switch (pmu_state
) {
1474 data_len
= req
->nbytes
- 1;
1475 send_byte(data_len
);
1478 if (data_index
<= data_len
) {
1479 send_byte(req
->data
[data_index
++]);
1483 data_len
= pmu_data_len
[req
->data
[0]][1];
1484 if (data_len
== 0) {
1486 current_req
= req
->next
;
1487 if (req
->reply_expected
)
1488 req_awaiting_reply
= req
;
1492 pmu_state
= reading
;
1494 reply_ptr
= req
->reply
+ req
->reply_len
;
1502 pmu_state
= reading_intr
;
1503 reply_ptr
= interrupt_data
[int_data_last
];
1505 if (gpio_irq
>= 0 && !gpio_irq_enabled
) {
1506 enable_irq(gpio_irq
);
1507 gpio_irq_enabled
= 1;
1513 if (data_len
== -1) {
1516 printk(KERN_ERR
"PMU: bad reply len %d\n", bite
);
1517 } else if (data_index
< 32) {
1518 reply_ptr
[data_index
++] = bite
;
1520 if (data_index
< data_len
) {
1525 if (pmu_state
== reading_intr
) {
1527 int_data_state
[int_data_last
] = int_data_ready
;
1528 interrupt_data_len
[int_data_last
] = data_len
;
1532 * For PMU sleep and freq change requests, we lock the
1533 * PMU until it's explicitly unlocked. This avoids any
1534 * spurrious event polling getting in
1536 current_req
= req
->next
;
1537 req
->reply_len
+= data_index
;
1538 if (req
->data
[0] == PMU_SLEEP
|| req
->data
[0] == PMU_CPU_SPEED
)
1547 printk(KERN_ERR
"via_pmu_interrupt: unknown state %d?\n",
1554 via_pmu_interrupt(int irq
, void *arg
)
1556 unsigned long flags
;
1560 struct adb_request
*req
= NULL
;
1563 /* This is a bit brutal, we can probably do better */
1564 spin_lock_irqsave(&pmu_lock
, flags
);
1568 intr
= in_8(&via
[IFR
]) & (SR_INT
| CB1_INT
);
1572 if (++nloop
> 1000) {
1573 printk(KERN_DEBUG
"PMU: stuck in intr loop, "
1574 "intr=%x, ier=%x pmu_state=%d\n",
1575 intr
, in_8(&via
[IER
]), pmu_state
);
1578 out_8(&via
[IFR
], intr
);
1579 if (intr
& CB1_INT
) {
1580 adb_int_pending
= 1;
1583 if (intr
& SR_INT
) {
1584 req
= pmu_sr_intr();
1591 if (pmu_state
== idle
) {
1592 if (adb_int_pending
) {
1593 if (int_data_state
[0] == int_data_empty
)
1595 else if (int_data_state
[1] == int_data_empty
)
1600 int_data_state
[int_data_last
] = int_data_fill
;
1601 /* Sounds safer to make sure ACK is high before writing.
1602 * This helped kill a problem with ADB and some iBooks
1605 send_byte(PMU_INT_ACK
);
1606 adb_int_pending
= 0;
1607 } else if (current_req
)
1611 /* Mark the oldest buffer for flushing */
1612 if (int_data_state
[!int_data_last
] == int_data_ready
) {
1613 int_data_state
[!int_data_last
] = int_data_flush
;
1614 int_data
= !int_data_last
;
1615 } else 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
;
1620 spin_unlock_irqrestore(&pmu_lock
, flags
);
1622 /* Deal with completed PMU requests outside of the lock */
1628 /* Deal with interrupt datas outside of the lock */
1629 if (int_data
>= 0) {
1630 pmu_handle_data(interrupt_data
[int_data
], interrupt_data_len
[int_data
]);
1631 spin_lock_irqsave(&pmu_lock
, flags
);
1633 int_data_state
[int_data
] = int_data_empty
;
1638 return IRQ_RETVAL(handled
);
1644 unsigned long flags
;
1646 spin_lock_irqsave(&pmu_lock
, flags
);
1647 if (pmu_state
== locked
)
1649 adb_int_pending
= 1;
1650 spin_unlock_irqrestore(&pmu_lock
, flags
);
1655 gpio1_interrupt(int irq
, void *arg
)
1657 unsigned long flags
;
1659 if ((in_8(gpio_reg
+ 0x9) & 0x02) == 0) {
1660 spin_lock_irqsave(&pmu_lock
, flags
);
1661 if (gpio_irq_enabled
> 0) {
1662 disable_irq_nosync(gpio_irq
);
1663 gpio_irq_enabled
= 0;
1666 adb_int_pending
= 1;
1667 spin_unlock_irqrestore(&pmu_lock
, flags
);
1668 via_pmu_interrupt(0, NULL
);
1675 pmu_enable_irled(int on
)
1677 struct adb_request req
;
1681 if (pmu_kind
== PMU_KEYLARGO_BASED
)
1684 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
, PMU_POW_IRLED
|
1685 (on
? PMU_POW_ON
: PMU_POW_OFF
));
1686 pmu_wait_complete(&req
);
1692 struct adb_request req
;
1697 local_irq_disable();
1699 drop_interrupts
= 1;
1701 if (pmu_kind
!= PMU_KEYLARGO_BASED
) {
1702 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|
1704 while(!req
.complete
)
1708 pmu_request(&req
, NULL
, 1, PMU_RESET
);
1709 pmu_wait_complete(&req
);
1717 struct adb_request req
;
1722 local_irq_disable();
1724 drop_interrupts
= 1;
1726 if (pmu_kind
!= PMU_KEYLARGO_BASED
) {
1727 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
|
1729 pmu_wait_complete(&req
);
1731 /* Disable server mode on shutdown or we'll just
1734 pmu_set_server_mode(0);
1737 pmu_request(&req
, NULL
, 5, PMU_SHUTDOWN
,
1738 'M', 'A', 'T', 'T');
1739 pmu_wait_complete(&req
);
1750 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
1752 * Put the powerbook to sleep.
1755 static u32 save_via
[8];
1758 save_via_state(void)
1760 save_via
[0] = in_8(&via
[ANH
]);
1761 save_via
[1] = in_8(&via
[DIRA
]);
1762 save_via
[2] = in_8(&via
[B
]);
1763 save_via
[3] = in_8(&via
[DIRB
]);
1764 save_via
[4] = in_8(&via
[PCR
]);
1765 save_via
[5] = in_8(&via
[ACR
]);
1766 save_via
[6] = in_8(&via
[T1CL
]);
1767 save_via
[7] = in_8(&via
[T1CH
]);
1770 restore_via_state(void)
1772 out_8(&via
[ANH
], save_via
[0]);
1773 out_8(&via
[DIRA
], save_via
[1]);
1774 out_8(&via
[B
], save_via
[2]);
1775 out_8(&via
[DIRB
], save_via
[3]);
1776 out_8(&via
[PCR
], save_via
[4]);
1777 out_8(&via
[ACR
], save_via
[5]);
1778 out_8(&via
[T1CL
], save_via
[6]);
1779 out_8(&via
[T1CH
], save_via
[7]);
1780 out_8(&via
[IER
], IER_CLR
| 0x7f); /* disable all intrs */
1781 out_8(&via
[IFR
], 0x7f); /* clear IFR */
1782 out_8(&via
[IER
], IER_SET
| SR_INT
| CB1_INT
);
1785 #define GRACKLE_PM (1<<7)
1786 #define GRACKLE_DOZE (1<<5)
1787 #define GRACKLE_NAP (1<<4)
1788 #define GRACKLE_SLEEP (1<<3)
1790 static int powerbook_sleep_grackle(void)
1792 unsigned long save_l2cr
;
1793 unsigned short pmcr1
;
1794 struct adb_request req
;
1795 struct pci_dev
*grackle
;
1797 grackle
= pci_get_bus_and_slot(0, 0);
1801 /* Turn off various things. Darwin does some retry tests here... */
1802 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL0
, PMU_POW0_OFF
|PMU_POW0_HARD_DRIVE
);
1803 pmu_wait_complete(&req
);
1804 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
1805 PMU_POW_OFF
|PMU_POW_BACKLIGHT
|PMU_POW_IRLED
|PMU_POW_MEDIABAY
);
1806 pmu_wait_complete(&req
);
1808 /* For 750, save backside cache setting and disable it */
1809 save_l2cr
= _get_L2CR(); /* (returns -1 if not available) */
1811 if (!__fake_sleep
) {
1812 /* Ask the PMU to put us to sleep */
1813 pmu_request(&req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
1814 pmu_wait_complete(&req
);
1817 /* The VIA is supposed not to be restored correctly*/
1819 /* We shut down some HW */
1820 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,1);
1822 pci_read_config_word(grackle
, 0x70, &pmcr1
);
1823 /* Apparently, MacOS uses NAP mode for Grackle ??? */
1824 pmcr1
&= ~(GRACKLE_DOZE
|GRACKLE_SLEEP
);
1825 pmcr1
|= GRACKLE_PM
|GRACKLE_NAP
;
1826 pci_write_config_word(grackle
, 0x70, pmcr1
);
1828 /* Call low-level ASM sleep handler */
1832 low_sleep_handler();
1834 /* We're awake again, stop grackle PM */
1835 pci_read_config_word(grackle
, 0x70, &pmcr1
);
1836 pmcr1
&= ~(GRACKLE_PM
|GRACKLE_DOZE
|GRACKLE_SLEEP
|GRACKLE_NAP
);
1837 pci_write_config_word(grackle
, 0x70, pmcr1
);
1839 pci_dev_put(grackle
);
1841 /* Make sure the PMU is idle */
1842 pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,0);
1843 restore_via_state();
1845 /* Restore L2 cache */
1846 if (save_l2cr
!= 0xffffffff && (save_l2cr
& L2CR_L2E
) != 0)
1847 _set_L2CR(save_l2cr
);
1849 /* Restore userland MMU context */
1850 switch_mmu_context(NULL
, current
->active_mm
);
1852 /* Power things up */
1854 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
1855 pmu_wait_complete(&req
);
1856 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL0
,
1857 PMU_POW0_ON
|PMU_POW0_HARD_DRIVE
);
1858 pmu_wait_complete(&req
);
1859 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
1860 PMU_POW_ON
|PMU_POW_BACKLIGHT
|PMU_POW_CHARGER
|PMU_POW_IRLED
|PMU_POW_MEDIABAY
);
1861 pmu_wait_complete(&req
);
1867 powerbook_sleep_Core99(void)
1869 unsigned long save_l2cr
;
1870 unsigned long save_l3cr
;
1871 struct adb_request req
;
1873 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE
,NULL
,0,-1) < 0) {
1874 printk(KERN_ERR
"Sleep mode not supported on this machine\n");
1878 if (num_online_cpus() > 1 || cpu_is_offline(0))
1881 /* Stop environment and ADB interrupts */
1882 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, 0);
1883 pmu_wait_complete(&req
);
1885 /* Tell PMU what events will wake us up */
1886 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
, PMU_PWR_CLR_WAKEUP_EVENTS
,
1888 pmu_wait_complete(&req
);
1889 pmu_request(&req
, NULL
, 4, PMU_POWER_EVENTS
, PMU_PWR_SET_WAKEUP_EVENTS
,
1890 0, PMU_PWR_WAKEUP_KEY
|
1891 (option_lid_wakeup
? PMU_PWR_WAKEUP_LID_OPEN
: 0));
1892 pmu_wait_complete(&req
);
1894 /* Save the state of the L2 and L3 caches */
1895 save_l3cr
= _get_L3CR(); /* (returns -1 if not available) */
1896 save_l2cr
= _get_L2CR(); /* (returns -1 if not available) */
1898 if (!__fake_sleep
) {
1899 /* Ask the PMU to put us to sleep */
1900 pmu_request(&req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
1901 pmu_wait_complete(&req
);
1904 /* The VIA is supposed not to be restored correctly*/
1907 /* Shut down various ASICs. There's a chance that we can no longer
1908 * talk to the PMU after this, so I moved it to _after_ sending the
1909 * sleep command to it. Still need to be checked.
1911 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 1);
1913 /* Call low-level ASM sleep handler */
1917 low_sleep_handler();
1919 /* Restore Apple core ASICs state */
1920 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 0);
1923 restore_via_state();
1925 /* tweak LPJ before cpufreq is there */
1926 loops_per_jiffy
*= 2;
1929 pmac_call_early_video_resume();
1931 /* Restore L2 cache */
1932 if (save_l2cr
!= 0xffffffff && (save_l2cr
& L2CR_L2E
) != 0)
1933 _set_L2CR(save_l2cr
);
1934 /* Restore L3 cache */
1935 if (save_l3cr
!= 0xffffffff && (save_l3cr
& L3CR_L3E
) != 0)
1936 _set_L3CR(save_l3cr
);
1938 /* Restore userland MMU context */
1939 switch_mmu_context(NULL
, current
->active_mm
);
1941 /* Tell PMU we are ready */
1943 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
1944 pmu_wait_complete(&req
);
1945 pmu_request(&req
, NULL
, 2, PMU_SET_INTR_MASK
, pmu_intr_mask
);
1946 pmu_wait_complete(&req
);
1948 /* Restore LPJ, cpufreq will adjust the cpu frequency */
1949 loops_per_jiffy
/= 2;
1954 #define PB3400_MEM_CTRL 0xf8000000
1955 #define PB3400_MEM_CTRL_SLEEP 0x70
1957 static void __iomem
*pb3400_mem_ctrl
;
1959 static void powerbook_sleep_init_3400(void)
1961 /* map in the memory controller registers */
1962 pb3400_mem_ctrl
= ioremap(PB3400_MEM_CTRL
, 0x100);
1963 if (pb3400_mem_ctrl
== NULL
)
1964 printk(KERN_WARNING
"ioremap failed: sleep won't be possible");
1967 static int powerbook_sleep_3400(void)
1972 struct adb_request sleep_req
;
1973 unsigned int __iomem
*mem_ctrl_sleep
;
1975 if (pb3400_mem_ctrl
== NULL
)
1977 mem_ctrl_sleep
= pb3400_mem_ctrl
+ PB3400_MEM_CTRL_SLEEP
;
1979 /* Set the memory controller to keep the memory refreshed
1980 while we're asleep */
1981 for (i
= 0x403f; i
>= 0x4000; --i
) {
1982 out_be32(mem_ctrl_sleep
, i
);
1984 x
= (in_be32(mem_ctrl_sleep
) >> 16) & 0x3ff;
1990 /* Ask the PMU to put us to sleep */
1991 pmu_request(&sleep_req
, NULL
, 5, PMU_SLEEP
, 'M', 'A', 'T', 'T');
1992 pmu_wait_complete(&sleep_req
);
1995 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 1);
1999 /* Put the CPU into sleep mode */
2000 hid0
= mfspr(SPRN_HID0
);
2001 hid0
= (hid0
& ~(HID0_NAP
| HID0_DOZE
)) | HID0_SLEEP
;
2002 mtspr(SPRN_HID0
, hid0
);
2004 msr
= mfmsr() | MSR_POW
;
2010 local_irq_disable();
2012 /* OK, we're awake again, start restoring things */
2013 out_be32(mem_ctrl_sleep
, 0x3f);
2014 pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, 0);
2019 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
2022 * Support for /dev/pmu device
2024 #define RB_SIZE 0x10
2025 struct pmu_private
{
2026 struct list_head list
;
2031 unsigned char data
[16];
2033 wait_queue_head_t wait
;
2035 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2036 int backlight_locker
;
2040 static LIST_HEAD(all_pmu_pvt
);
2041 static DEFINE_SPINLOCK(all_pvt_lock
);
2044 pmu_pass_intr(unsigned char *data
, int len
)
2046 struct pmu_private
*pp
;
2047 struct list_head
*list
;
2049 unsigned long flags
;
2051 if (len
> sizeof(pp
->rb_buf
[0].data
))
2052 len
= sizeof(pp
->rb_buf
[0].data
);
2053 spin_lock_irqsave(&all_pvt_lock
, flags
);
2054 for (list
= &all_pmu_pvt
; (list
= list
->next
) != &all_pmu_pvt
; ) {
2055 pp
= list_entry(list
, struct pmu_private
, list
);
2056 spin_lock(&pp
->lock
);
2060 if (i
!= pp
->rb_get
) {
2061 struct rb_entry
*rp
= &pp
->rb_buf
[pp
->rb_put
];
2063 memcpy(rp
->data
, data
, len
);
2065 wake_up_interruptible(&pp
->wait
);
2067 spin_unlock(&pp
->lock
);
2069 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2073 pmu_open(struct inode
*inode
, struct file
*file
)
2075 struct pmu_private
*pp
;
2076 unsigned long flags
;
2078 pp
= kmalloc(sizeof(struct pmu_private
), GFP_KERNEL
);
2081 pp
->rb_get
= pp
->rb_put
= 0;
2082 spin_lock_init(&pp
->lock
);
2083 init_waitqueue_head(&pp
->wait
);
2084 mutex_lock(&pmu_info_proc_mutex
);
2085 spin_lock_irqsave(&all_pvt_lock
, flags
);
2086 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2087 pp
->backlight_locker
= 0;
2089 list_add(&pp
->list
, &all_pmu_pvt
);
2090 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2091 file
->private_data
= pp
;
2092 mutex_unlock(&pmu_info_proc_mutex
);
2097 pmu_read(struct file
*file
, char __user
*buf
,
2098 size_t count
, loff_t
*ppos
)
2100 struct pmu_private
*pp
= file
->private_data
;
2101 DECLARE_WAITQUEUE(wait
, current
);
2102 unsigned long flags
;
2105 if (count
< 1 || pp
== 0)
2107 if (!access_ok(VERIFY_WRITE
, buf
, count
))
2110 spin_lock_irqsave(&pp
->lock
, flags
);
2111 add_wait_queue(&pp
->wait
, &wait
);
2112 current
->state
= TASK_INTERRUPTIBLE
;
2116 if (pp
->rb_get
!= pp
->rb_put
) {
2118 struct rb_entry
*rp
= &pp
->rb_buf
[i
];
2120 spin_unlock_irqrestore(&pp
->lock
, flags
);
2123 if (ret
> 0 && copy_to_user(buf
, rp
->data
, ret
))
2127 spin_lock_irqsave(&pp
->lock
, flags
);
2132 if (file
->f_flags
& O_NONBLOCK
)
2135 if (signal_pending(current
))
2137 spin_unlock_irqrestore(&pp
->lock
, flags
);
2139 spin_lock_irqsave(&pp
->lock
, flags
);
2141 current
->state
= TASK_RUNNING
;
2142 remove_wait_queue(&pp
->wait
, &wait
);
2143 spin_unlock_irqrestore(&pp
->lock
, flags
);
2149 pmu_write(struct file
*file
, const char __user
*buf
,
2150 size_t count
, loff_t
*ppos
)
2156 pmu_fpoll(struct file
*filp
, poll_table
*wait
)
2158 struct pmu_private
*pp
= filp
->private_data
;
2159 unsigned int mask
= 0;
2160 unsigned long flags
;
2164 poll_wait(filp
, &pp
->wait
, wait
);
2165 spin_lock_irqsave(&pp
->lock
, flags
);
2166 if (pp
->rb_get
!= pp
->rb_put
)
2168 spin_unlock_irqrestore(&pp
->lock
, flags
);
2173 pmu_release(struct inode
*inode
, struct file
*file
)
2175 struct pmu_private
*pp
= file
->private_data
;
2176 unsigned long flags
;
2179 file
->private_data
= NULL
;
2180 spin_lock_irqsave(&all_pvt_lock
, flags
);
2181 list_del(&pp
->list
);
2182 spin_unlock_irqrestore(&all_pvt_lock
, flags
);
2184 #if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2185 if (pp
->backlight_locker
)
2186 pmac_backlight_enable();
2194 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
2195 static void pmac_suspend_disable_irqs(void)
2197 /* Call platform functions marked "on sleep" */
2198 pmac_pfunc_i2c_suspend();
2199 pmac_pfunc_base_suspend();
2202 static int powerbook_sleep(suspend_state_t state
)
2206 /* Wait for completion of async requests */
2207 while (!batt_req
.complete
)
2210 /* Giveup the lazy FPU & vec so we don't have to back them
2211 * up from the low level code
2215 #ifdef CONFIG_ALTIVEC
2216 if (cpu_has_feature(CPU_FTR_ALTIVEC
))
2217 enable_kernel_altivec();
2218 #endif /* CONFIG_ALTIVEC */
2221 case PMU_OHARE_BASED
:
2222 error
= powerbook_sleep_3400();
2224 case PMU_HEATHROW_BASED
:
2225 case PMU_PADDINGTON_BASED
:
2226 error
= powerbook_sleep_grackle();
2228 case PMU_KEYLARGO_BASED
:
2229 error
= powerbook_sleep_Core99();
2243 static void pmac_suspend_enable_irqs(void)
2245 /* Force a poll of ADB interrupts */
2246 adb_int_pending
= 1;
2247 via_pmu_interrupt(0, NULL
);
2251 /* Call platform functions marked "on wake" */
2252 pmac_pfunc_base_resume();
2253 pmac_pfunc_i2c_resume();
2256 static int pmu_sleep_valid(suspend_state_t state
)
2258 return state
== PM_SUSPEND_MEM
2259 && (pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, -1) >= 0);
2262 static const struct platform_suspend_ops pmu_pm_ops
= {
2263 .enter
= powerbook_sleep
,
2264 .valid
= pmu_sleep_valid
,
2267 static int register_pmu_pm_ops(void)
2269 if (pmu_kind
== PMU_OHARE_BASED
)
2270 powerbook_sleep_init_3400();
2271 ppc_md
.suspend_disable_irqs
= pmac_suspend_disable_irqs
;
2272 ppc_md
.suspend_enable_irqs
= pmac_suspend_enable_irqs
;
2273 suspend_set_ops(&pmu_pm_ops
);
2278 device_initcall(register_pmu_pm_ops
);
2281 static int pmu_ioctl(struct file
*filp
,
2282 u_int cmd
, u_long arg
)
2284 __u32 __user
*argp
= (__u32 __user
*)arg
;
2285 int error
= -EINVAL
;
2289 if (!capable(CAP_SYS_ADMIN
))
2291 return pm_suspend(PM_SUSPEND_MEM
);
2292 case PMU_IOC_CAN_SLEEP
:
2293 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE
, NULL
, 0, -1) < 0)
2294 return put_user(0, argp
);
2296 return put_user(1, argp
);
2298 #ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2299 /* Compatibility ioctl's for backlight */
2300 case PMU_IOC_GET_BACKLIGHT
:
2304 brightness
= pmac_backlight_get_legacy_brightness();
2308 return put_user(brightness
, argp
);
2311 case PMU_IOC_SET_BACKLIGHT
:
2315 error
= get_user(brightness
, argp
);
2319 return pmac_backlight_set_legacy_brightness(brightness
);
2321 #ifdef CONFIG_INPUT_ADBHID
2322 case PMU_IOC_GRAB_BACKLIGHT
: {
2323 struct pmu_private
*pp
= filp
->private_data
;
2325 if (pp
->backlight_locker
)
2328 pp
->backlight_locker
= 1;
2329 pmac_backlight_disable();
2333 #endif /* CONFIG_INPUT_ADBHID */
2334 #endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
2336 case PMU_IOC_GET_MODEL
:
2337 return put_user(pmu_kind
, argp
);
2338 case PMU_IOC_HAS_ADB
:
2339 return put_user(pmu_has_adb
, argp
);
2344 static long pmu_unlocked_ioctl(struct file
*filp
,
2345 u_int cmd
, u_long arg
)
2349 mutex_lock(&pmu_info_proc_mutex
);
2350 ret
= pmu_ioctl(filp
, cmd
, arg
);
2351 mutex_unlock(&pmu_info_proc_mutex
);
2356 #ifdef CONFIG_COMPAT
2357 #define PMU_IOC_GET_BACKLIGHT32 _IOR('B', 1, compat_size_t)
2358 #define PMU_IOC_SET_BACKLIGHT32 _IOW('B', 2, compat_size_t)
2359 #define PMU_IOC_GET_MODEL32 _IOR('B', 3, compat_size_t)
2360 #define PMU_IOC_HAS_ADB32 _IOR('B', 4, compat_size_t)
2361 #define PMU_IOC_CAN_SLEEP32 _IOR('B', 5, compat_size_t)
2362 #define PMU_IOC_GRAB_BACKLIGHT32 _IOR('B', 6, compat_size_t)
2364 static long compat_pmu_ioctl (struct file
*filp
, u_int cmd
, u_long arg
)
2369 case PMU_IOC_GET_BACKLIGHT32
:
2370 cmd
= PMU_IOC_GET_BACKLIGHT
;
2372 case PMU_IOC_SET_BACKLIGHT32
:
2373 cmd
= PMU_IOC_SET_BACKLIGHT
;
2375 case PMU_IOC_GET_MODEL32
:
2376 cmd
= PMU_IOC_GET_MODEL
;
2378 case PMU_IOC_HAS_ADB32
:
2379 cmd
= PMU_IOC_HAS_ADB
;
2381 case PMU_IOC_CAN_SLEEP32
:
2382 cmd
= PMU_IOC_CAN_SLEEP
;
2384 case PMU_IOC_GRAB_BACKLIGHT32
:
2385 cmd
= PMU_IOC_GRAB_BACKLIGHT
;
2388 return -ENOIOCTLCMD
;
2390 return pmu_unlocked_ioctl(filp
, cmd
, (unsigned long)compat_ptr(arg
));
2394 static const struct file_operations pmu_device_fops
= {
2398 .unlocked_ioctl
= pmu_unlocked_ioctl
,
2399 #ifdef CONFIG_COMPAT
2400 .compat_ioctl
= compat_pmu_ioctl
,
2403 .release
= pmu_release
,
2404 .llseek
= noop_llseek
,
2407 static struct miscdevice pmu_device
= {
2408 PMU_MINOR
, "pmu", &pmu_device_fops
2411 static int pmu_device_init(void)
2415 if (misc_register(&pmu_device
) < 0)
2416 printk(KERN_ERR
"via-pmu: cannot register misc device.\n");
2419 device_initcall(pmu_device_init
);
2424 polled_handshake(volatile unsigned char __iomem
*via
)
2426 via
[B
] &= ~TREQ
; eieio();
2427 while ((via
[B
] & TACK
) != 0)
2429 via
[B
] |= TREQ
; eieio();
2430 while ((via
[B
] & TACK
) == 0)
2435 polled_send_byte(volatile unsigned char __iomem
*via
, int x
)
2437 via
[ACR
] |= SR_OUT
| SR_EXT
; eieio();
2438 via
[SR
] = x
; eieio();
2439 polled_handshake(via
);
2443 polled_recv_byte(volatile unsigned char __iomem
*via
)
2447 via
[ACR
] = (via
[ACR
] & ~SR_OUT
) | SR_EXT
; eieio();
2448 x
= via
[SR
]; eieio();
2449 polled_handshake(via
);
2450 x
= via
[SR
]; eieio();
2455 pmu_polled_request(struct adb_request
*req
)
2457 unsigned long flags
;
2459 volatile unsigned char __iomem
*v
= via
;
2463 l
= pmu_data_len
[c
][0];
2464 if (l
>= 0 && req
->nbytes
!= l
+ 1)
2467 local_irq_save(flags
);
2468 while (pmu_state
!= idle
)
2471 while ((via
[B
] & TACK
) == 0)
2473 polled_send_byte(v
, c
);
2475 l
= req
->nbytes
- 1;
2476 polled_send_byte(v
, l
);
2478 for (i
= 1; i
<= l
; ++i
)
2479 polled_send_byte(v
, req
->data
[i
]);
2481 l
= pmu_data_len
[c
][1];
2483 l
= polled_recv_byte(v
);
2484 for (i
= 0; i
< l
; ++i
)
2485 req
->reply
[i
+ req
->reply_len
] = polled_recv_byte(v
);
2490 local_irq_restore(flags
);
2494 /* N.B. This doesn't work on the 3400 */
2495 void pmu_blink(int n
)
2497 struct adb_request req
;
2499 memset(&req
, 0, sizeof(req
));
2501 for (; n
> 0; --n
) {
2508 req
.reply
[0] = ADB_RET_OK
;
2510 req
.reply_expected
= 0;
2511 pmu_polled_request(&req
);
2519 req
.reply
[0] = ADB_RET_OK
;
2521 req
.reply_expected
= 0;
2522 pmu_polled_request(&req
);
2527 #endif /* DEBUG_SLEEP */
2529 #if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
2530 int pmu_sys_suspended
;
2532 static int pmu_syscore_suspend(void)
2534 /* Suspend PMU event interrupts */
2536 pmu_sys_suspended
= 1;
2538 #ifdef CONFIG_PMAC_BACKLIGHT
2539 /* Tell backlight code not to muck around with the chip anymore */
2540 pmu_backlight_set_sleep(1);
2546 static void pmu_syscore_resume(void)
2548 struct adb_request req
;
2550 if (!pmu_sys_suspended
)
2553 /* Tell PMU we are ready */
2554 pmu_request(&req
, NULL
, 2, PMU_SYSTEM_READY
, 2);
2555 pmu_wait_complete(&req
);
2557 #ifdef CONFIG_PMAC_BACKLIGHT
2558 /* Tell backlight code it can use the chip again */
2559 pmu_backlight_set_sleep(0);
2561 /* Resume PMU event interrupts */
2563 pmu_sys_suspended
= 0;
2566 static struct syscore_ops pmu_syscore_ops
= {
2567 .suspend
= pmu_syscore_suspend
,
2568 .resume
= pmu_syscore_resume
,
2571 static int pmu_syscore_register(void)
2573 register_syscore_ops(&pmu_syscore_ops
);
2577 subsys_initcall(pmu_syscore_register
);
2578 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
2580 EXPORT_SYMBOL(pmu_request
);
2581 EXPORT_SYMBOL(pmu_queue_request
);
2582 EXPORT_SYMBOL(pmu_poll
);
2583 EXPORT_SYMBOL(pmu_poll_adb
);
2584 EXPORT_SYMBOL(pmu_wait_complete
);
2585 EXPORT_SYMBOL(pmu_suspend
);
2586 EXPORT_SYMBOL(pmu_resume
);
2587 EXPORT_SYMBOL(pmu_unlock
);
2588 #if defined(CONFIG_PPC32)
2589 EXPORT_SYMBOL(pmu_enable_irled
);
2590 EXPORT_SYMBOL(pmu_battery_count
);
2591 EXPORT_SYMBOL(pmu_batteries
);
2592 EXPORT_SYMBOL(pmu_power_flags
);
2593 #endif /* CONFIG_SUSPEND && CONFIG_PPC32 */