blob: 6f308a4757ee60dd6aa8acd4881745efc4eb4521 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Device driver for the via-pmu on Apple Powermacs.
3 *
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.
10 *
11 * Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
12 * Copyright (C) 2001-2002 Benjamin Herrenschmidt
Johannes Bergf91266e2007-12-12 01:25:59 +110013 * Copyright (C) 2006-2007 Johannes Berg
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * THIS DRIVER IS BECOMING A TOTAL MESS !
16 * - Cleanup atomically disabling reply to PMU events after
17 * a sleep or a freq. switch
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 */
20#include <stdarg.h>
Arnd Bergmannffe83732008-05-20 19:16:58 +020021#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/module.h>
36#include <linux/spinlock.h>
37#include <linux/pm.h>
38#include <linux/proc_fs.h>
39#include <linux/init.h>
40#include <linux/interrupt.h>
41#include <linux/device.h>
42#include <linux/sysdev.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080043#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/syscalls.h>
Dave Jones6002f542007-01-05 16:36:18 -080045#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/cpu.h>
47#include <asm/prom.h>
48#include <asm/machdep.h>
49#include <asm/io.h>
50#include <asm/pgtable.h>
51#include <asm/system.h>
52#include <asm/sections.h>
53#include <asm/irq.h>
54#include <asm/pmac_feature.h>
Benjamin Herrenschmidt5b9ca522006-01-07 11:41:02 +110055#include <asm/pmac_pfunc.h>
56#include <asm/pmac_low_i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/uaccess.h>
58#include <asm/mmu_context.h>
59#include <asm/cputable.h>
60#include <asm/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/backlight.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Johannes Berg9e8e30a2006-06-26 01:49:55 -040063#include "via-pmu-event.h"
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Some compile options */
Johannes Bergf91266e2007-12-12 01:25:59 +110066#undef DEBUG_SLEEP
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Misc minor number allocated for /dev/pmu */
69#define PMU_MINOR 154
70
71/* How many iterations between battery polls */
72#define BATTERY_POLLING_COUNT 2
73
74static volatile unsigned char __iomem *via;
75
76/* VIA registers - spaced 0x200 bytes apart */
77#define RS 0x200 /* skip between registers */
78#define B 0 /* B-side data */
79#define A RS /* A-side data */
80#define DIRB (2*RS) /* B-side direction (1=output) */
81#define DIRA (3*RS) /* A-side direction (1=output) */
82#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
83#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
84#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
85#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
86#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
87#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
88#define SR (10*RS) /* Shift register */
89#define ACR (11*RS) /* Auxiliary control register */
90#define PCR (12*RS) /* Peripheral control register */
91#define IFR (13*RS) /* Interrupt flag register */
92#define IER (14*RS) /* Interrupt enable register */
93#define ANH (15*RS) /* A-side data, no handshake */
94
95/* Bits in B data register: both active low */
96#define TACK 0x08 /* Transfer acknowledge (input) */
97#define TREQ 0x10 /* Transfer request (output) */
98
99/* Bits in ACR */
100#define SR_CTRL 0x1c /* Shift register control bits */
101#define SR_EXT 0x0c /* Shift on external clock */
102#define SR_OUT 0x10 /* Shift out if 1 */
103
104/* Bits in IFR and IER */
105#define IER_SET 0x80 /* set bits in IER */
106#define IER_CLR 0 /* clear bits in IER */
107#define SR_INT 0x04 /* Shift register full/empty */
108#define CB2_INT 0x08
109#define CB1_INT 0x10 /* transition on CB1 input */
110
111static volatile enum pmu_state {
112 idle,
113 sending,
114 intack,
115 reading,
116 reading_intr,
117 locked,
118} pmu_state;
119
120static volatile enum int_data_state {
121 int_data_empty,
122 int_data_fill,
123 int_data_ready,
124 int_data_flush
125} int_data_state[2] = { int_data_empty, int_data_empty };
126
127static struct adb_request *current_req;
128static struct adb_request *last_req;
129static struct adb_request *req_awaiting_reply;
130static unsigned char interrupt_data[2][32];
131static int interrupt_data_len[2];
132static int int_data_last;
133static unsigned char *reply_ptr;
134static int data_index;
135static int data_len;
136static volatile int adb_int_pending;
137static volatile int disable_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct device_node *vias;
139static int pmu_kind = PMU_UNKNOWN;
Olaf Hering87275852007-02-10 21:35:12 +0100140static int pmu_fully_inited;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static int pmu_has_adb;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100142static struct device_node *gpio_node;
Olaf Hering87275852007-02-10 21:35:12 +0100143static unsigned char __iomem *gpio_reg;
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000144static int gpio_irq = NO_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static int gpio_irq_enabled = -1;
Olaf Hering87275852007-02-10 21:35:12 +0100146static volatile int pmu_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static spinlock_t pmu_lock;
148static u8 pmu_intr_mask;
149static int pmu_version;
150static int drop_interrupts;
Johannes Bergf91266e2007-12-12 01:25:59 +1100151#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int option_lid_wakeup = 1;
Johannes Bergf91266e2007-12-12 01:25:59 +1100153#endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static unsigned long async_req_locks;
155static unsigned int pmu_irq_stats[11];
156
157static struct proc_dir_entry *proc_pmu_root;
158static struct proc_dir_entry *proc_pmu_info;
159static struct proc_dir_entry *proc_pmu_irqstats;
160static struct proc_dir_entry *proc_pmu_options;
161static int option_server_mode;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163int pmu_battery_count;
164int pmu_cur_battery;
Olaf Heringa334bdb2007-02-10 21:40:00 +0100165unsigned int pmu_power_flags = PMU_PWR_AC_PRESENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
167static int query_batt_timer = BATTERY_POLLING_COUNT;
168static struct adb_request batt_req;
169static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171int __fake_sleep;
172int asleep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174#ifdef CONFIG_ADB
Olaf Hering87275852007-02-10 21:35:12 +0100175static int adb_dev_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static int pmu_adb_flags;
177
178static int pmu_probe(void);
179static int pmu_init(void);
180static int pmu_send_request(struct adb_request *req, int sync);
181static int pmu_adb_autopoll(int devs);
182static int pmu_adb_reset_bus(void);
183#endif /* CONFIG_ADB */
184
185static int init_pmu(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static void pmu_start(void);
David Howells7d12e782006-10-05 14:55:46 +0100187static irqreturn_t via_pmu_interrupt(int irq, void *arg);
188static irqreturn_t gpio1_interrupt(int irq, void *arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int proc_get_info(char *page, char **start, off_t off,
190 int count, int *eof, void *data);
191static int proc_get_irqstats(char *page, char **start, off_t off,
192 int count, int *eof, void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static void pmu_pass_intr(unsigned char *data, int len);
194static int proc_get_batt(char *page, char **start, off_t off,
195 int count, int *eof, void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static int proc_read_options(char *page, char **start, off_t off,
197 int count, int *eof, void *data);
198static int proc_write_options(struct file *file, const char __user *buffer,
199 unsigned long count, void *data);
200
201#ifdef CONFIG_ADB
202struct adb_driver via_pmu_driver = {
203 "PMU",
204 pmu_probe,
205 pmu_init,
206 pmu_send_request,
207 pmu_adb_autopoll,
208 pmu_poll_adb,
209 pmu_adb_reset_bus
210};
211#endif /* CONFIG_ADB */
212
213extern void low_sleep_handler(void);
214extern void enable_kernel_altivec(void);
215extern void enable_kernel_fp(void);
216
217#ifdef DEBUG_SLEEP
218int pmu_polled_request(struct adb_request *req);
Johannes Bergf91266e2007-12-12 01:25:59 +1100219void pmu_blink(int n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#endif
221
222/*
223 * This table indicates for each PMU opcode:
224 * - the number of data bytes to be sent with the command, or -1
225 * if a length byte should be sent,
226 * - the number of response bytes which the PMU will return, or
227 * -1 if it will send a length byte.
228 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500229static const s8 pmu_data_len[256][2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* 0 1 2 3 4 5 6 7 */
231/*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
232/*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
233/*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
234/*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
235/*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
236/*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
237/*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
238/*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
239/*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
240/*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
241/*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
242/*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
243/*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
244/*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
245/*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
246/*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
247/*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
248/*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
249/*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
250/*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
251/*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
252/*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
253/*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
254/*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
255/*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
256/*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
257/*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
258/*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
259/*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
260/*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
261/*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
262/*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
263};
264
265static char *pbook_type[] = {
266 "Unknown PowerBook",
267 "PowerBook 2400/3400/3500(G3)",
268 "PowerBook G3 Series",
269 "1999 PowerBook G3",
270 "Core99"
271};
272
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100273int __init find_via_pmu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100275 u64 taddr;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000276 const u32 *reg;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (via != 0)
279 return 1;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100280 vias = of_find_node_by_name(NULL, "via-pmu");
281 if (vias == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Stephen Rothwell01b27262007-04-27 13:41:15 +1000284 reg = of_get_property(vias, "reg", NULL);
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100285 if (reg == NULL) {
286 printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
287 goto fail;
288 }
289 taddr = of_translate_address(vias, reg);
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +1100290 if (taddr == OF_BAD_ADDR) {
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100291 printk(KERN_ERR "via-pmu: Can't translate address !\n");
292 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
295 spin_lock_init(&pmu_lock);
296
297 pmu_has_adb = 1;
298
299 pmu_intr_mask = PMU_INT_PCEJECT |
300 PMU_INT_SNDBRT |
301 PMU_INT_ADB |
302 PMU_INT_TICK;
303
304 if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000305 || of_device_is_compatible(vias->parent, "ohare")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 pmu_kind = PMU_OHARE_BASED;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000307 else if (of_device_is_compatible(vias->parent, "paddington"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 pmu_kind = PMU_PADDINGTON_BASED;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000309 else if (of_device_is_compatible(vias->parent, "heathrow"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 pmu_kind = PMU_HEATHROW_BASED;
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000311 else if (of_device_is_compatible(vias->parent, "Keylargo")
312 || of_device_is_compatible(vias->parent, "K2-Keylargo")) {
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100313 struct device_node *gpiop;
Stephen Rothwell1658ab62007-04-24 13:51:59 +1000314 struct device_node *adbp;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100315 u64 gaddr = OF_BAD_ADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 pmu_kind = PMU_KEYLARGO_BASED;
Stephen Rothwell1658ab62007-04-24 13:51:59 +1000318 adbp = of_find_node_by_type(NULL, "adb");
319 pmu_has_adb = (adbp != NULL);
320 of_node_put(adbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 pmu_intr_mask = PMU_INT_PCEJECT |
322 PMU_INT_SNDBRT |
323 PMU_INT_ADB |
324 PMU_INT_TICK |
325 PMU_INT_ENVIRONMENT;
326
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100327 gpiop = of_find_node_by_name(NULL, "gpio");
328 if (gpiop) {
Stephen Rothwell01b27262007-04-27 13:41:15 +1000329 reg = of_get_property(gpiop, "reg", NULL);
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100330 if (reg)
331 gaddr = of_translate_address(gpiop, reg);
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100332 if (gaddr != OF_BAD_ADDR)
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100333 gpio_reg = ioremap(gaddr, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Olaf Hering61e37ca2006-09-26 22:28:36 +0200335 if (gpio_reg == NULL) {
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100336 printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
Olaf Hering61e37ca2006-09-26 22:28:36 +0200337 goto fail_gpio;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 } else
340 pmu_kind = PMU_UNKNOWN;
341
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100342 via = ioremap(taddr, 0x2000);
343 if (via == NULL) {
344 printk(KERN_ERR "via-pmu: Can't map address !\n");
345 goto fail;
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
349 out_8(&via[IFR], 0x7f); /* clear IFR */
350
351 pmu_state = idle;
352
353 if (!init_pmu()) {
354 via = NULL;
355 return 0;
356 }
357
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +1100358 printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
360
361 sys_ctrler = SYS_CTRLER_PMU;
362
363 return 1;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100364 fail:
365 of_node_put(vias);
Olaf Hering61e37ca2006-09-26 22:28:36 +0200366 iounmap(gpio_reg);
367 gpio_reg = NULL;
368 fail_gpio:
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100369 vias = NULL;
370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373#ifdef CONFIG_ADB
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100374static int pmu_probe(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 return vias == NULL? -ENODEV: 0;
377}
378
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100379static int __init pmu_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 if (vias == NULL)
382 return -ENODEV;
383 return 0;
384}
385#endif /* CONFIG_ADB */
386
387/*
388 * We can't wait until pmu_init gets called, that happens too late.
389 * It happens after IDE and SCSI initialization, which can take a few
390 * seconds, and by that time the PMU could have given up on us and
391 * turned us off.
392 * Thus this is called with arch_initcall rather than device_initcall.
393 */
394static int __init via_pmu_start(void)
395{
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000396 unsigned int irq;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (vias == NULL)
399 return -ENODEV;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 batt_req.complete = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000403 irq = irq_of_parse_and_map(vias, 0);
404 if (irq == NO_IRQ) {
Michael Buesch7b52b442007-08-13 06:38:34 +1000405 printk(KERN_ERR "via-pmu: can't map interrupt\n");
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000406 return -ENODEV;
407 }
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +0000408 /* We set IRQF_TIMER because we don't want the interrupt to be disabled
409 * between the 2 passes of driver suspend, we control our own disabling
410 * for that one
411 */
412 if (request_irq(irq, via_pmu_interrupt, IRQF_TIMER, "VIA-PMU", (void *)0)) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000413 printk(KERN_ERR "via-pmu: can't request irq %d\n", irq);
414 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100417 if (pmu_kind == PMU_KEYLARGO_BASED) {
418 gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
419 if (gpio_node == NULL)
420 gpio_node = of_find_node_by_name(NULL,
421 "pmu-interrupt");
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000422 if (gpio_node)
423 gpio_irq = irq_of_parse_and_map(gpio_node, 0);
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100424
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000425 if (gpio_irq != NO_IRQ) {
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +0000426 if (request_irq(gpio_irq, gpio1_interrupt, IRQF_TIMER,
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100427 "GPIO1 ADB", (void *)0))
428 printk(KERN_ERR "pmu: can't get irq %d"
429 " (GPIO1)\n", gpio_irq);
430 else
431 gpio_irq_enabled = 1;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
435 /* Enable interrupts */
436 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
437
438 pmu_fully_inited = 1;
439
440 /* Make sure PMU settle down before continuing. This is _very_ important
441 * since the IDE probe may shut interrupts down for quite a bit of time. If
442 * a PMU communication is pending while this happens, the PMU may timeout
443 * Not that on Core99 machines, the PMU keeps sending us environement
444 * messages, we should find a way to either fix IDE or make it call
445 * pmu_suspend() before masking interrupts. This can also happens while
446 * scolling with some fbdevs.
447 */
448 do {
449 pmu_poll();
450 } while (pmu_state != idle);
451
452 return 0;
453}
454
455arch_initcall(via_pmu_start);
456
457/*
458 * This has to be done after pci_init, which is a subsys_initcall.
459 */
460static int __init via_pmu_dev_init(void)
461{
462 if (vias == NULL)
463 return -ENODEV;
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#ifdef CONFIG_PMAC_BACKLIGHT
Michael Hanselmann5474c122006-06-25 05:47:08 -0700466 /* Initialize backlight */
Michael Hanselmann4b755992006-07-30 03:04:19 -0700467 pmu_backlight_init();
Michael Hanselmann5474c122006-06-25 05:47:08 -0700468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700470#ifdef CONFIG_PPC32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (machine_is_compatible("AAPL,3400/2400") ||
472 machine_is_compatible("AAPL,3500")) {
473 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
474 NULL, PMAC_MB_INFO_MODEL, 0);
475 pmu_battery_count = 1;
476 if (mb == PMAC_TYPE_COMET)
477 pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
478 else
479 pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
480 } else if (machine_is_compatible("AAPL,PowerBook1998") ||
481 machine_is_compatible("PowerBook1,1")) {
482 pmu_battery_count = 2;
483 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
484 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
485 } else {
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000486 struct device_node* prim =
487 of_find_node_by_name(NULL, "power-mgt");
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000488 const u32 *prim_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (prim)
Stephen Rothwell01b27262007-04-27 13:41:15 +1000490 prim_info = of_get_property(prim, "prim-info", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (prim_info) {
492 /* Other stuffs here yet unknown */
493 pmu_battery_count = (prim_info[6] >> 16) & 0xff;
494 pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
495 if (pmu_battery_count > 1)
496 pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
497 }
Stephen Rothwell30686ba2007-04-24 13:53:04 +1000498 of_node_put(prim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700500#endif /* CONFIG_PPC32 */
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* Create /proc/pmu */
503 proc_pmu_root = proc_mkdir("pmu", NULL);
504 if (proc_pmu_root) {
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700505 long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 for (i=0; i<pmu_battery_count; i++) {
508 char title[16];
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700509 sprintf(title, "battery_%ld", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 proc_pmu_batt[i] = create_proc_read_entry(title, 0, proc_pmu_root,
511 proc_get_batt, (void *)i);
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 proc_pmu_info = create_proc_read_entry("info", 0, proc_pmu_root,
515 proc_get_info, NULL);
516 proc_pmu_irqstats = create_proc_read_entry("interrupts", 0, proc_pmu_root,
517 proc_get_irqstats, NULL);
518 proc_pmu_options = create_proc_entry("options", 0600, proc_pmu_root);
519 if (proc_pmu_options) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 proc_pmu_options->read_proc = proc_read_options;
521 proc_pmu_options->write_proc = proc_write_options;
522 }
523 }
524 return 0;
525}
526
527device_initcall(via_pmu_dev_init);
528
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500529static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530init_pmu(void)
531{
532 int timeout;
533 struct adb_request req;
534
535 out_8(&via[B], via[B] | TREQ); /* negate TREQ */
536 out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
537
538 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
539 timeout = 100000;
540 while (!req.complete) {
541 if (--timeout < 0) {
542 printk(KERN_ERR "init_pmu: no response from PMU\n");
543 return 0;
544 }
545 udelay(10);
546 pmu_poll();
547 }
548
549 /* ack all pending interrupts */
550 timeout = 100000;
551 interrupt_data[0][0] = 1;
552 while (interrupt_data[0][0] || pmu_state != idle) {
553 if (--timeout < 0) {
554 printk(KERN_ERR "init_pmu: timed out acking intrs\n");
555 return 0;
556 }
557 if (pmu_state == idle)
558 adb_int_pending = 1;
David Howells7d12e782006-10-05 14:55:46 +0100559 via_pmu_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 udelay(10);
561 }
562
563 /* Tell PMU we are ready. */
564 if (pmu_kind == PMU_KEYLARGO_BASED) {
565 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
566 while (!req.complete)
567 pmu_poll();
568 }
569
570 /* Read PMU version */
571 pmu_request(&req, NULL, 1, PMU_GET_VERSION);
572 pmu_wait_complete(&req);
573 if (req.reply_len > 0)
574 pmu_version = req.reply[0];
575
576 /* Read server mode setting */
577 if (pmu_kind == PMU_KEYLARGO_BASED) {
578 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
579 PMU_PWR_GET_POWERUP_EVENTS);
580 pmu_wait_complete(&req);
581 if (req.reply_len == 2) {
582 if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
583 option_server_mode = 1;
584 printk(KERN_INFO "via-pmu: Server Mode is %s\n",
585 option_server_mode ? "enabled" : "disabled");
586 }
587 }
588 return 1;
589}
590
591int
592pmu_get_model(void)
593{
594 return pmu_kind;
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597static void pmu_set_server_mode(int server_mode)
598{
599 struct adb_request req;
600
601 if (pmu_kind != PMU_KEYLARGO_BASED)
602 return;
603
604 option_server_mode = server_mode;
605 pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
606 pmu_wait_complete(&req);
607 if (req.reply_len < 2)
608 return;
609 if (server_mode)
610 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
611 PMU_PWR_SET_POWERUP_EVENTS,
612 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
613 else
614 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
615 PMU_PWR_CLR_POWERUP_EVENTS,
616 req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
617 pmu_wait_complete(&req);
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/* This new version of the code for 2400/3400/3500 powerbooks
621 * is inspired from the implementation in gkrellm-pmu
622 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500623static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624done_battery_state_ohare(struct adb_request* req)
625{
626 /* format:
627 * [0] : flags
628 * 0x01 : AC indicator
629 * 0x02 : charging
630 * 0x04 : battery exist
631 * 0x08 :
632 * 0x10 :
633 * 0x20 : full charged
634 * 0x40 : pcharge reset
635 * 0x80 : battery exist
636 *
637 * [1][2] : battery voltage
638 * [3] : CPU temperature
639 * [4] : battery temperature
640 * [5] : current
641 * [6][7] : pcharge
642 * --tkoba
643 */
644 unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
645 long pcharge, charge, vb, vmax, lmax;
646 long vmax_charging, vmax_charged;
647 long amperage, voltage, time, max;
648 int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
649 NULL, PMAC_MB_INFO_MODEL, 0);
650
651 if (req->reply[0] & 0x01)
652 pmu_power_flags |= PMU_PWR_AC_PRESENT;
653 else
654 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
655
656 if (mb == PMAC_TYPE_COMET) {
657 vmax_charged = 189;
658 vmax_charging = 213;
659 lmax = 6500;
660 } else {
661 vmax_charged = 330;
662 vmax_charging = 330;
663 lmax = 6500;
664 }
665 vmax = vmax_charged;
666
667 /* If battery installed */
668 if (req->reply[0] & 0x04) {
669 bat_flags |= PMU_BATT_PRESENT;
670 if (req->reply[0] & 0x02)
671 bat_flags |= PMU_BATT_CHARGING;
672 vb = (req->reply[1] << 8) | req->reply[2];
673 voltage = (vb * 265 + 72665) / 10;
674 amperage = req->reply[5];
675 if ((req->reply[0] & 0x01) == 0) {
676 if (amperage > 200)
677 vb += ((amperage - 200) * 15)/100;
678 } else if (req->reply[0] & 0x02) {
679 vb = (vb * 97) / 100;
680 vmax = vmax_charging;
681 }
682 charge = (100 * vb) / vmax;
683 if (req->reply[0] & 0x40) {
684 pcharge = (req->reply[6] << 8) + req->reply[7];
685 if (pcharge > lmax)
686 pcharge = lmax;
687 pcharge *= 100;
688 pcharge = 100 - pcharge / lmax;
689 if (pcharge < charge)
690 charge = pcharge;
691 }
692 if (amperage > 0)
693 time = (charge * 16440) / amperage;
694 else
695 time = 0;
696 max = 100;
697 amperage = -amperage;
698 } else
699 charge = max = amperage = voltage = time = 0;
700
701 pmu_batteries[pmu_cur_battery].flags = bat_flags;
702 pmu_batteries[pmu_cur_battery].charge = charge;
703 pmu_batteries[pmu_cur_battery].max_charge = max;
704 pmu_batteries[pmu_cur_battery].amperage = amperage;
705 pmu_batteries[pmu_cur_battery].voltage = voltage;
706 pmu_batteries[pmu_cur_battery].time_remaining = time;
707
708 clear_bit(0, &async_req_locks);
709}
710
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500711static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712done_battery_state_smart(struct adb_request* req)
713{
714 /* format:
715 * [0] : format of this structure (known: 3,4,5)
716 * [1] : flags
717 *
718 * format 3 & 4:
719 *
720 * [2] : charge
721 * [3] : max charge
722 * [4] : current
723 * [5] : voltage
724 *
725 * format 5:
726 *
727 * [2][3] : charge
728 * [4][5] : max charge
729 * [6][7] : current
730 * [8][9] : voltage
731 */
732
733 unsigned int bat_flags = PMU_BATT_TYPE_SMART;
734 int amperage;
735 unsigned int capa, max, voltage;
736
737 if (req->reply[1] & 0x01)
738 pmu_power_flags |= PMU_PWR_AC_PRESENT;
739 else
740 pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
741
742
743 capa = max = amperage = voltage = 0;
744
745 if (req->reply[1] & 0x04) {
746 bat_flags |= PMU_BATT_PRESENT;
747 switch(req->reply[0]) {
748 case 3:
749 case 4: capa = req->reply[2];
750 max = req->reply[3];
751 amperage = *((signed char *)&req->reply[4]);
752 voltage = req->reply[5];
753 break;
754 case 5: capa = (req->reply[2] << 8) | req->reply[3];
755 max = (req->reply[4] << 8) | req->reply[5];
756 amperage = *((signed short *)&req->reply[6]);
757 voltage = (req->reply[8] << 8) | req->reply[9];
758 break;
759 default:
760 printk(KERN_WARNING "pmu.c : unrecognized battery info, len: %d, %02x %02x %02x %02x\n",
761 req->reply_len, req->reply[0], req->reply[1], req->reply[2], req->reply[3]);
762 break;
763 }
764 }
765
766 if ((req->reply[1] & 0x01) && (amperage > 0))
767 bat_flags |= PMU_BATT_CHARGING;
768
769 pmu_batteries[pmu_cur_battery].flags = bat_flags;
770 pmu_batteries[pmu_cur_battery].charge = capa;
771 pmu_batteries[pmu_cur_battery].max_charge = max;
772 pmu_batteries[pmu_cur_battery].amperage = amperage;
773 pmu_batteries[pmu_cur_battery].voltage = voltage;
774 if (amperage) {
775 if ((req->reply[1] & 0x01) && (amperage > 0))
776 pmu_batteries[pmu_cur_battery].time_remaining
777 = ((max-capa) * 3600) / amperage;
778 else
779 pmu_batteries[pmu_cur_battery].time_remaining
780 = (capa * 3600) / (-amperage);
781 } else
782 pmu_batteries[pmu_cur_battery].time_remaining = 0;
783
784 pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
785
786 clear_bit(0, &async_req_locks);
787}
788
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500789static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790query_battery_state(void)
791{
792 if (test_and_set_bit(0, &async_req_locks))
793 return;
794 if (pmu_kind == PMU_OHARE_BASED)
795 pmu_request(&batt_req, done_battery_state_ohare,
796 1, PMU_BATTERY_STATE);
797 else
798 pmu_request(&batt_req, done_battery_state_smart,
799 2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
800}
801
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500802static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803proc_get_info(char *page, char **start, off_t off,
804 int count, int *eof, void *data)
805{
806 char* p = page;
807
808 p += sprintf(p, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
809 p += sprintf(p, "PMU firmware version : %02x\n", pmu_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 p += sprintf(p, "AC Power : %d\n",
Benjamin Herrenschmidt63e1fd42006-03-13 21:20:42 -0800811 ((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 p += sprintf(p, "Battery count : %d\n", pmu_battery_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 return p - page;
815}
816
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500817static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818proc_get_irqstats(char *page, char **start, off_t off,
819 int count, int *eof, void *data)
820{
821 int i;
822 char* p = page;
823 static const char *irq_names[] = {
824 "Total CB1 triggered events",
825 "Total GPIO1 triggered events",
826 "PC-Card eject button",
827 "Sound/Brightness button",
828 "ADB message",
829 "Battery state change",
830 "Environment interrupt",
831 "Tick timer",
832 "Ghost interrupt (zero len)",
833 "Empty interrupt (empty mask)",
834 "Max irqs in a row"
835 };
836
837 for (i=0; i<11; i++) {
838 p += sprintf(p, " %2u: %10u (%s)\n",
839 i, pmu_irq_stats[i], irq_names[i]);
840 }
841 return p - page;
842}
843
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500844static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845proc_get_batt(char *page, char **start, off_t off,
846 int count, int *eof, void *data)
847{
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700848 long batnum = (long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 char *p = page;
850
851 p += sprintf(p, "\n");
852 p += sprintf(p, "flags : %08x\n",
853 pmu_batteries[batnum].flags);
854 p += sprintf(p, "charge : %d\n",
855 pmu_batteries[batnum].charge);
856 p += sprintf(p, "max_charge : %d\n",
857 pmu_batteries[batnum].max_charge);
858 p += sprintf(p, "current : %d\n",
859 pmu_batteries[batnum].amperage);
860 p += sprintf(p, "voltage : %d\n",
861 pmu_batteries[batnum].voltage);
862 p += sprintf(p, "time rem. : %d\n",
863 pmu_batteries[batnum].time_remaining);
864
865 return p - page;
866}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500868static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869proc_read_options(char *page, char **start, off_t off,
870 int count, int *eof, void *data)
871{
872 char *p = page;
873
Johannes Bergf91266e2007-12-12 01:25:59 +1100874#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (pmu_kind == PMU_KEYLARGO_BASED &&
876 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
877 p += sprintf(p, "lid_wakeup=%d\n", option_lid_wakeup);
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700878#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (pmu_kind == PMU_KEYLARGO_BASED)
880 p += sprintf(p, "server_mode=%d\n", option_server_mode);
881
882 return p - page;
883}
884
Jon Loeligeraacaf9b2005-09-17 10:36:54 -0500885static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886proc_write_options(struct file *file, const char __user *buffer,
887 unsigned long count, void *data)
888{
889 char tmp[33];
890 char *label, *val;
891 unsigned long fcount = count;
892
893 if (!count)
894 return -EINVAL;
895 if (count > 32)
896 count = 32;
897 if (copy_from_user(tmp, buffer, count))
898 return -EFAULT;
899 tmp[count] = 0;
900
901 label = tmp;
902 while(*label == ' ')
903 label++;
904 val = label;
905 while(*val && (*val != '=')) {
906 if (*val == ' ')
907 *val = 0;
908 val++;
909 }
910 if ((*val) == 0)
911 return -EINVAL;
912 *(val++) = 0;
913 while(*val == ' ')
914 val++;
Johannes Bergf91266e2007-12-12 01:25:59 +1100915#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (pmu_kind == PMU_KEYLARGO_BASED &&
917 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
918 if (!strcmp(label, "lid_wakeup"))
919 option_lid_wakeup = ((*val) == '1');
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -0700920#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
922 int new_value;
923 new_value = ((*val) == '1');
924 if (new_value != option_server_mode)
925 pmu_set_server_mode(new_value);
926 }
927 return fcount;
928}
929
930#ifdef CONFIG_ADB
931/* Send an ADB command */
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +0000932static int pmu_send_request(struct adb_request *req, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 int i, ret;
935
936 if ((vias == NULL) || (!pmu_fully_inited)) {
937 req->complete = 1;
938 return -ENXIO;
939 }
940
941 ret = -EINVAL;
942
943 switch (req->data[0]) {
944 case PMU_PACKET:
945 for (i = 0; i < req->nbytes - 1; ++i)
946 req->data[i] = req->data[i+1];
947 --req->nbytes;
948 if (pmu_data_len[req->data[0]][1] != 0) {
949 req->reply[0] = ADB_RET_OK;
950 req->reply_len = 1;
951 } else
952 req->reply_len = 0;
953 ret = pmu_queue_request(req);
954 break;
955 case CUDA_PACKET:
956 switch (req->data[1]) {
957 case CUDA_GET_TIME:
958 if (req->nbytes != 2)
959 break;
960 req->data[0] = PMU_READ_RTC;
961 req->nbytes = 1;
962 req->reply_len = 3;
963 req->reply[0] = CUDA_PACKET;
964 req->reply[1] = 0;
965 req->reply[2] = CUDA_GET_TIME;
966 ret = pmu_queue_request(req);
967 break;
968 case CUDA_SET_TIME:
969 if (req->nbytes != 6)
970 break;
971 req->data[0] = PMU_SET_RTC;
972 req->nbytes = 5;
973 for (i = 1; i <= 4; ++i)
974 req->data[i] = req->data[i+1];
975 req->reply_len = 3;
976 req->reply[0] = CUDA_PACKET;
977 req->reply[1] = 0;
978 req->reply[2] = CUDA_SET_TIME;
979 ret = pmu_queue_request(req);
980 break;
981 }
982 break;
983 case ADB_PACKET:
984 if (!pmu_has_adb)
985 return -ENXIO;
986 for (i = req->nbytes - 1; i > 1; --i)
987 req->data[i+2] = req->data[i];
988 req->data[3] = req->nbytes - 2;
989 req->data[2] = pmu_adb_flags;
990 /*req->data[1] = req->data[1];*/
991 req->data[0] = PMU_ADB_CMD;
992 req->nbytes += 2;
993 req->reply_expected = 1;
994 req->reply_len = 0;
995 ret = pmu_queue_request(req);
996 break;
997 }
998 if (ret) {
999 req->complete = 1;
1000 return ret;
1001 }
1002
1003 if (sync)
1004 while (!req->complete)
1005 pmu_poll();
1006
1007 return 0;
1008}
1009
1010/* Enable/disable autopolling */
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +00001011static int __pmu_adb_autopoll(int devs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 struct adb_request req;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (devs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
1017 adb_dev_map >> 8, adb_dev_map);
1018 pmu_adb_flags = 2;
1019 } else {
1020 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
1021 pmu_adb_flags = 0;
1022 }
1023 while (!req.complete)
1024 pmu_poll();
1025 return 0;
1026}
1027
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +00001028static int pmu_adb_autopoll(int devs)
1029{
1030 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1031 return -ENXIO;
1032
1033 adb_dev_map = devs;
1034 return __pmu_adb_autopoll(devs);
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/* Reset the ADB bus */
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +00001038static int pmu_adb_reset_bus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct adb_request req;
1041 int save_autopoll = adb_dev_map;
1042
1043 if ((vias == NULL) || (!pmu_fully_inited) || !pmu_has_adb)
1044 return -ENXIO;
1045
1046 /* anyone got a better idea?? */
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +00001047 __pmu_adb_autopoll(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +00001049 req.nbytes = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 req.done = NULL;
1051 req.data[0] = PMU_ADB_CMD;
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +00001052 req.data[1] = ADB_BUSRESET;
1053 req.data[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 req.data[3] = 0;
1055 req.data[4] = 0;
1056 req.reply_len = 0;
1057 req.reply_expected = 1;
1058 if (pmu_queue_request(&req) != 0) {
1059 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
1060 return -EIO;
1061 }
1062 pmu_wait_complete(&req);
1063
1064 if (save_autopoll != 0)
Benjamin Herrenschmidt11a50872009-10-09 11:27:54 +00001065 __pmu_adb_autopoll(save_autopoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 return 0;
1068}
1069#endif /* CONFIG_ADB */
1070
1071/* Construct and send a pmu request */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001072int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
1074 int nbytes, ...)
1075{
1076 va_list list;
1077 int i;
1078
1079 if (vias == NULL)
1080 return -ENXIO;
1081
1082 if (nbytes < 0 || nbytes > 32) {
1083 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
1084 req->complete = 1;
1085 return -EINVAL;
1086 }
1087 req->nbytes = nbytes;
1088 req->done = done;
1089 va_start(list, nbytes);
1090 for (i = 0; i < nbytes; ++i)
1091 req->data[i] = va_arg(list, int);
1092 va_end(list);
1093 req->reply_len = 0;
1094 req->reply_expected = 0;
1095 return pmu_queue_request(req);
1096}
1097
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001098int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099pmu_queue_request(struct adb_request *req)
1100{
1101 unsigned long flags;
1102 int nsend;
1103
1104 if (via == NULL) {
1105 req->complete = 1;
1106 return -ENXIO;
1107 }
1108 if (req->nbytes <= 0) {
1109 req->complete = 1;
1110 return 0;
1111 }
1112 nsend = pmu_data_len[req->data[0]][0];
1113 if (nsend >= 0 && req->nbytes != nsend + 1) {
1114 req->complete = 1;
1115 return -EINVAL;
1116 }
1117
1118 req->next = NULL;
1119 req->sent = 0;
1120 req->complete = 0;
1121
1122 spin_lock_irqsave(&pmu_lock, flags);
1123 if (current_req != 0) {
1124 last_req->next = req;
1125 last_req = req;
1126 } else {
1127 current_req = req;
1128 last_req = req;
1129 if (pmu_state == idle)
1130 pmu_start();
1131 }
1132 spin_unlock_irqrestore(&pmu_lock, flags);
1133
1134 return 0;
1135}
1136
1137static inline void
1138wait_for_ack(void)
1139{
1140 /* Sightly increased the delay, I had one occurrence of the message
1141 * reported
1142 */
1143 int timeout = 4000;
1144 while ((in_8(&via[B]) & TACK) == 0) {
1145 if (--timeout < 0) {
1146 printk(KERN_ERR "PMU not responding (!ack)\n");
1147 return;
1148 }
1149 udelay(10);
1150 }
1151}
1152
1153/* New PMU seems to be very sensitive to those timings, so we make sure
1154 * PCI is flushed immediately */
1155static inline void
1156send_byte(int x)
1157{
1158 volatile unsigned char __iomem *v = via;
1159
1160 out_8(&v[ACR], in_8(&v[ACR]) | SR_OUT | SR_EXT);
1161 out_8(&v[SR], x);
1162 out_8(&v[B], in_8(&v[B]) & ~TREQ); /* assert TREQ */
1163 (void)in_8(&v[B]);
1164}
1165
1166static inline void
1167recv_byte(void)
1168{
1169 volatile unsigned char __iomem *v = via;
1170
1171 out_8(&v[ACR], (in_8(&v[ACR]) & ~SR_OUT) | SR_EXT);
1172 in_8(&v[SR]); /* resets SR */
1173 out_8(&v[B], in_8(&v[B]) & ~TREQ);
1174 (void)in_8(&v[B]);
1175}
1176
1177static inline void
1178pmu_done(struct adb_request *req)
1179{
1180 void (*done)(struct adb_request *) = req->done;
1181 mb();
1182 req->complete = 1;
1183 /* Here, we assume that if the request has a done member, the
1184 * struct request will survive to setting req->complete to 1
1185 */
1186 if (done)
1187 (*done)(req);
1188}
1189
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001190static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191pmu_start(void)
1192{
1193 struct adb_request *req;
1194
1195 /* assert pmu_state == idle */
1196 /* get the packet to send */
1197 req = current_req;
1198 if (req == 0 || pmu_state != idle
1199 || (/*req->reply_expected && */req_awaiting_reply))
1200 return;
1201
1202 pmu_state = sending;
1203 data_index = 1;
1204 data_len = pmu_data_len[req->data[0]][0];
1205
1206 /* Sounds safer to make sure ACK is high before writing. This helped
1207 * kill a problem with ADB and some iBooks
1208 */
1209 wait_for_ack();
1210 /* set the shift register to shift out and send a byte */
1211 send_byte(req->data[0]);
1212}
1213
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001214void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215pmu_poll(void)
1216{
1217 if (!via)
1218 return;
1219 if (disable_poll)
1220 return;
David Howells7d12e782006-10-05 14:55:46 +01001221 via_pmu_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001224void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225pmu_poll_adb(void)
1226{
1227 if (!via)
1228 return;
1229 if (disable_poll)
1230 return;
1231 /* Kicks ADB read when PMU is suspended */
1232 adb_int_pending = 1;
1233 do {
David Howells7d12e782006-10-05 14:55:46 +01001234 via_pmu_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 } while (pmu_suspended && (adb_int_pending || pmu_state != idle
1236 || req_awaiting_reply));
1237}
1238
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001239void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240pmu_wait_complete(struct adb_request *req)
1241{
1242 if (!via)
1243 return;
1244 while((pmu_state != idle && pmu_state != locked) || !req->complete)
David Howells7d12e782006-10-05 14:55:46 +01001245 via_pmu_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248/* This function loops until the PMU is idle and prevents it from
1249 * anwsering to ADB interrupts. pmu_request can still be called.
1250 * This is done to avoid spurrious shutdowns when we know we'll have
1251 * interrupts switched off for a long time
1252 */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001253void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254pmu_suspend(void)
1255{
1256 unsigned long flags;
Johannes Berg1b0e9d42007-11-14 06:08:32 +11001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (!via)
1259 return;
1260
1261 spin_lock_irqsave(&pmu_lock, flags);
1262 pmu_suspended++;
1263 if (pmu_suspended > 1) {
1264 spin_unlock_irqrestore(&pmu_lock, flags);
1265 return;
1266 }
1267
1268 do {
1269 spin_unlock_irqrestore(&pmu_lock, flags);
1270 if (req_awaiting_reply)
1271 adb_int_pending = 1;
David Howells7d12e782006-10-05 14:55:46 +01001272 via_pmu_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 spin_lock_irqsave(&pmu_lock, flags);
1274 if (!adb_int_pending && pmu_state == idle && !req_awaiting_reply) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (gpio_irq >= 0)
1276 disable_irq_nosync(gpio_irq);
1277 out_8(&via[IER], CB1_INT | IER_CLR);
1278 spin_unlock_irqrestore(&pmu_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 }
1281 } while (1);
1282}
1283
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001284void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285pmu_resume(void)
1286{
1287 unsigned long flags;
1288
1289 if (!via || (pmu_suspended < 1))
1290 return;
1291
1292 spin_lock_irqsave(&pmu_lock, flags);
1293 pmu_suspended--;
1294 if (pmu_suspended > 0) {
1295 spin_unlock_irqrestore(&pmu_lock, flags);
1296 return;
1297 }
1298 adb_int_pending = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (gpio_irq >= 0)
1300 enable_irq(gpio_irq);
1301 out_8(&via[IER], CB1_INT | IER_SET);
1302 spin_unlock_irqrestore(&pmu_lock, flags);
1303 pmu_poll();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306/* Interrupt data could be the result data from an ADB cmd */
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001307static void
David Howells7d12e782006-10-05 14:55:46 +01001308pmu_handle_data(unsigned char *data, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
1310 unsigned char ints, pirq;
1311 int i = 0;
1312
1313 asleep = 0;
1314 if (drop_interrupts || len < 1) {
1315 adb_int_pending = 0;
1316 pmu_irq_stats[8]++;
1317 return;
1318 }
1319
1320 /* Get PMU interrupt mask */
1321 ints = data[0];
1322
1323 /* Record zero interrupts for stats */
1324 if (ints == 0)
1325 pmu_irq_stats[9]++;
1326
1327 /* Hack to deal with ADB autopoll flag */
1328 if (ints & PMU_INT_ADB)
1329 ints &= ~(PMU_INT_ADB_AUTO | PMU_INT_AUTO_SRQ_POLL);
1330
1331next:
1332
1333 if (ints == 0) {
1334 if (i > pmu_irq_stats[10])
1335 pmu_irq_stats[10] = i;
1336 return;
1337 }
1338
1339 for (pirq = 0; pirq < 8; pirq++)
1340 if (ints & (1 << pirq))
1341 break;
1342 pmu_irq_stats[pirq]++;
1343 i++;
1344 ints &= ~(1 << pirq);
1345
1346 /* Note: for some reason, we get an interrupt with len=1,
1347 * data[0]==0 after each normal ADB interrupt, at least
1348 * on the Pismo. Still investigating... --BenH
1349 */
1350 if ((1 << pirq) & PMU_INT_ADB) {
1351 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
1352 struct adb_request *req = req_awaiting_reply;
1353 if (req == 0) {
1354 printk(KERN_ERR "PMU: extra ADB reply\n");
1355 return;
1356 }
1357 req_awaiting_reply = NULL;
1358 if (len <= 2)
1359 req->reply_len = 0;
1360 else {
1361 memcpy(req->reply, data + 1, len - 1);
1362 req->reply_len = len - 1;
1363 }
1364 pmu_done(req);
1365 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (len == 4 && data[1] == 0x2c) {
1367 extern int xmon_wants_key, xmon_adb_keycode;
1368 if (xmon_wants_key) {
1369 xmon_adb_keycode = data[2];
1370 return;
1371 }
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373#ifdef CONFIG_ADB
1374 /*
1375 * XXX On the [23]400 the PMU gives us an up
1376 * event for keycodes 0x74 or 0x75 when the PC
1377 * card eject buttons are released, so we
1378 * ignore those events.
1379 */
1380 if (!(pmu_kind == PMU_OHARE_BASED && len == 4
1381 && data[1] == 0x2c && data[3] == 0xff
1382 && (data[2] & ~1) == 0xf4))
David Howells7d12e782006-10-05 14:55:46 +01001383 adb_input(data+1, len-1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384#endif /* CONFIG_ADB */
1385 }
1386 }
1387 /* Sound/brightness button pressed */
1388 else if ((1 << pirq) & PMU_INT_SNDBRT) {
1389#ifdef CONFIG_PMAC_BACKLIGHT
1390 if (len == 3)
Michael Hanselmann4b755992006-07-30 03:04:19 -07001391 pmac_backlight_set_legacy_brightness_pmu(data[1] >> 4);
1392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394 /* Tick interrupt */
1395 else if ((1 << pirq) & PMU_INT_TICK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* Environement or tick interrupt, query batteries */
1397 if (pmu_battery_count) {
1398 if ((--query_batt_timer) == 0) {
1399 query_battery_state();
1400 query_batt_timer = BATTERY_POLLING_COUNT;
1401 }
1402 }
1403 }
1404 else if ((1 << pirq) & PMU_INT_ENVIRONMENT) {
1405 if (pmu_battery_count)
1406 query_battery_state();
1407 pmu_pass_intr(data, len);
Johannes Berg9e8e30a2006-06-26 01:49:55 -04001408 /* len == 6 is probably a bad check. But how do I
1409 * know what PMU versions send what events here? */
1410 if (len == 6) {
1411 via_pmu_event(PMU_EVT_POWER, !!(data[1]&8));
1412 via_pmu_event(PMU_EVT_LID, data[1]&1);
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 } else {
1415 pmu_pass_intr(data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 goto next;
1418}
1419
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001420static struct adb_request*
David Howells7d12e782006-10-05 14:55:46 +01001421pmu_sr_intr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct adb_request *req;
1424 int bite = 0;
1425
1426 if (via[B] & TREQ) {
1427 printk(KERN_ERR "PMU: spurious SR intr (%x)\n", via[B]);
1428 out_8(&via[IFR], SR_INT);
1429 return NULL;
1430 }
1431 /* The ack may not yet be low when we get the interrupt */
1432 while ((in_8(&via[B]) & TACK) != 0)
1433 ;
1434
1435 /* if reading grab the byte, and reset the interrupt */
1436 if (pmu_state == reading || pmu_state == reading_intr)
1437 bite = in_8(&via[SR]);
1438
1439 /* reset TREQ and wait for TACK to go high */
1440 out_8(&via[B], in_8(&via[B]) | TREQ);
1441 wait_for_ack();
1442
1443 switch (pmu_state) {
1444 case sending:
1445 req = current_req;
1446 if (data_len < 0) {
1447 data_len = req->nbytes - 1;
1448 send_byte(data_len);
1449 break;
1450 }
1451 if (data_index <= data_len) {
1452 send_byte(req->data[data_index++]);
1453 break;
1454 }
1455 req->sent = 1;
1456 data_len = pmu_data_len[req->data[0]][1];
1457 if (data_len == 0) {
1458 pmu_state = idle;
1459 current_req = req->next;
1460 if (req->reply_expected)
1461 req_awaiting_reply = req;
1462 else
1463 return req;
1464 } else {
1465 pmu_state = reading;
1466 data_index = 0;
1467 reply_ptr = req->reply + req->reply_len;
1468 recv_byte();
1469 }
1470 break;
1471
1472 case intack:
1473 data_index = 0;
1474 data_len = -1;
1475 pmu_state = reading_intr;
1476 reply_ptr = interrupt_data[int_data_last];
1477 recv_byte();
1478 if (gpio_irq >= 0 && !gpio_irq_enabled) {
1479 enable_irq(gpio_irq);
1480 gpio_irq_enabled = 1;
1481 }
1482 break;
1483
1484 case reading:
1485 case reading_intr:
1486 if (data_len == -1) {
1487 data_len = bite;
1488 if (bite > 32)
1489 printk(KERN_ERR "PMU: bad reply len %d\n", bite);
1490 } else if (data_index < 32) {
1491 reply_ptr[data_index++] = bite;
1492 }
1493 if (data_index < data_len) {
1494 recv_byte();
1495 break;
1496 }
1497
1498 if (pmu_state == reading_intr) {
1499 pmu_state = idle;
1500 int_data_state[int_data_last] = int_data_ready;
1501 interrupt_data_len[int_data_last] = data_len;
1502 } else {
1503 req = current_req;
1504 /*
1505 * For PMU sleep and freq change requests, we lock the
Jean Delvarec03983a2007-10-19 23:22:55 +02001506 * PMU until it's explicitly unlocked. This avoids any
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * spurrious event polling getting in
1508 */
1509 current_req = req->next;
1510 req->reply_len += data_index;
1511 if (req->data[0] == PMU_SLEEP || req->data[0] == PMU_CPU_SPEED)
1512 pmu_state = locked;
1513 else
1514 pmu_state = idle;
1515 return req;
1516 }
1517 break;
1518
1519 default:
1520 printk(KERN_ERR "via_pmu_interrupt: unknown state %d?\n",
1521 pmu_state);
1522 }
1523 return NULL;
1524}
1525
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001526static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001527via_pmu_interrupt(int irq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 unsigned long flags;
1530 int intr;
1531 int nloop = 0;
1532 int int_data = -1;
1533 struct adb_request *req = NULL;
1534 int handled = 0;
1535
1536 /* This is a bit brutal, we can probably do better */
1537 spin_lock_irqsave(&pmu_lock, flags);
1538 ++disable_poll;
1539
1540 for (;;) {
1541 intr = in_8(&via[IFR]) & (SR_INT | CB1_INT);
1542 if (intr == 0)
1543 break;
1544 handled = 1;
1545 if (++nloop > 1000) {
1546 printk(KERN_DEBUG "PMU: stuck in intr loop, "
1547 "intr=%x, ier=%x pmu_state=%d\n",
1548 intr, in_8(&via[IER]), pmu_state);
1549 break;
1550 }
1551 out_8(&via[IFR], intr);
1552 if (intr & CB1_INT) {
1553 adb_int_pending = 1;
1554 pmu_irq_stats[0]++;
1555 }
1556 if (intr & SR_INT) {
David Howells7d12e782006-10-05 14:55:46 +01001557 req = pmu_sr_intr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (req)
1559 break;
1560 }
1561 }
1562
1563recheck:
1564 if (pmu_state == idle) {
1565 if (adb_int_pending) {
1566 if (int_data_state[0] == int_data_empty)
1567 int_data_last = 0;
1568 else if (int_data_state[1] == int_data_empty)
1569 int_data_last = 1;
1570 else
1571 goto no_free_slot;
1572 pmu_state = intack;
1573 int_data_state[int_data_last] = int_data_fill;
1574 /* Sounds safer to make sure ACK is high before writing.
1575 * This helped kill a problem with ADB and some iBooks
1576 */
1577 wait_for_ack();
1578 send_byte(PMU_INT_ACK);
1579 adb_int_pending = 0;
1580 } else if (current_req)
1581 pmu_start();
1582 }
1583no_free_slot:
1584 /* Mark the oldest buffer for flushing */
1585 if (int_data_state[!int_data_last] == int_data_ready) {
1586 int_data_state[!int_data_last] = int_data_flush;
1587 int_data = !int_data_last;
1588 } else if (int_data_state[int_data_last] == int_data_ready) {
1589 int_data_state[int_data_last] = int_data_flush;
1590 int_data = int_data_last;
1591 }
1592 --disable_poll;
1593 spin_unlock_irqrestore(&pmu_lock, flags);
1594
1595 /* Deal with completed PMU requests outside of the lock */
1596 if (req) {
1597 pmu_done(req);
1598 req = NULL;
1599 }
1600
1601 /* Deal with interrupt datas outside of the lock */
1602 if (int_data >= 0) {
David Howells7d12e782006-10-05 14:55:46 +01001603 pmu_handle_data(interrupt_data[int_data], interrupt_data_len[int_data]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 spin_lock_irqsave(&pmu_lock, flags);
1605 ++disable_poll;
1606 int_data_state[int_data] = int_data_empty;
1607 int_data = -1;
1608 goto recheck;
1609 }
1610
1611 return IRQ_RETVAL(handled);
1612}
1613
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001614void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615pmu_unlock(void)
1616{
1617 unsigned long flags;
1618
1619 spin_lock_irqsave(&pmu_lock, flags);
1620 if (pmu_state == locked)
1621 pmu_state = idle;
1622 adb_int_pending = 1;
1623 spin_unlock_irqrestore(&pmu_lock, flags);
1624}
1625
1626
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001627static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001628gpio1_interrupt(int irq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
1630 unsigned long flags;
1631
1632 if ((in_8(gpio_reg + 0x9) & 0x02) == 0) {
1633 spin_lock_irqsave(&pmu_lock, flags);
1634 if (gpio_irq_enabled > 0) {
1635 disable_irq_nosync(gpio_irq);
1636 gpio_irq_enabled = 0;
1637 }
1638 pmu_irq_stats[1]++;
1639 adb_int_pending = 1;
1640 spin_unlock_irqrestore(&pmu_lock, flags);
David Howells7d12e782006-10-05 14:55:46 +01001641 via_pmu_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return IRQ_HANDLED;
1643 }
1644 return IRQ_NONE;
1645}
1646
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001647void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648pmu_enable_irled(int on)
1649{
1650 struct adb_request req;
1651
1652 if (vias == NULL)
1653 return ;
1654 if (pmu_kind == PMU_KEYLARGO_BASED)
1655 return ;
1656
1657 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
1658 (on ? PMU_POW_ON : PMU_POW_OFF));
1659 pmu_wait_complete(&req);
1660}
1661
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001662void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663pmu_restart(void)
1664{
1665 struct adb_request req;
1666
1667 if (via == NULL)
1668 return;
1669
1670 local_irq_disable();
1671
1672 drop_interrupts = 1;
1673
1674 if (pmu_kind != PMU_KEYLARGO_BASED) {
1675 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1676 PMU_INT_TICK );
1677 while(!req.complete)
1678 pmu_poll();
1679 }
1680
1681 pmu_request(&req, NULL, 1, PMU_RESET);
1682 pmu_wait_complete(&req);
1683 for (;;)
1684 ;
1685}
1686
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001687void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688pmu_shutdown(void)
1689{
1690 struct adb_request req;
1691
1692 if (via == NULL)
1693 return;
1694
1695 local_irq_disable();
1696
1697 drop_interrupts = 1;
1698
1699 if (pmu_kind != PMU_KEYLARGO_BASED) {
1700 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB |
1701 PMU_INT_TICK );
1702 pmu_wait_complete(&req);
1703 } else {
1704 /* Disable server mode on shutdown or we'll just
1705 * wake up again
1706 */
1707 pmu_set_server_mode(0);
1708 }
1709
1710 pmu_request(&req, NULL, 5, PMU_SHUTDOWN,
1711 'M', 'A', 'T', 'T');
1712 pmu_wait_complete(&req);
1713 for (;;)
1714 ;
1715}
1716
1717int
1718pmu_present(void)
1719{
1720 return via != 0;
1721}
1722
Johannes Bergf91266e2007-12-12 01:25:59 +11001723#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724/*
1725 * Put the powerbook to sleep.
1726 */
1727
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001728static u32 save_via[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001730static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731save_via_state(void)
1732{
1733 save_via[0] = in_8(&via[ANH]);
1734 save_via[1] = in_8(&via[DIRA]);
1735 save_via[2] = in_8(&via[B]);
1736 save_via[3] = in_8(&via[DIRB]);
1737 save_via[4] = in_8(&via[PCR]);
1738 save_via[5] = in_8(&via[ACR]);
1739 save_via[6] = in_8(&via[T1CL]);
1740 save_via[7] = in_8(&via[T1CH]);
1741}
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001742static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743restore_via_state(void)
1744{
1745 out_8(&via[ANH], save_via[0]);
1746 out_8(&via[DIRA], save_via[1]);
1747 out_8(&via[B], save_via[2]);
1748 out_8(&via[DIRB], save_via[3]);
1749 out_8(&via[PCR], save_via[4]);
1750 out_8(&via[ACR], save_via[5]);
1751 out_8(&via[T1CL], save_via[6]);
1752 out_8(&via[T1CH], save_via[7]);
1753 out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
1754 out_8(&via[IFR], 0x7f); /* clear IFR */
1755 out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
1756}
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758#define GRACKLE_PM (1<<7)
1759#define GRACKLE_DOZE (1<<5)
1760#define GRACKLE_NAP (1<<4)
1761#define GRACKLE_SLEEP (1<<3)
1762
Olaf Hering3bea6312006-03-21 23:00:05 -08001763static int powerbook_sleep_grackle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
1765 unsigned long save_l2cr;
1766 unsigned short pmcr1;
1767 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct pci_dev *grackle;
1769
Alan Coxc78f8302007-04-23 14:56:01 +01001770 grackle = pci_get_bus_and_slot(0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (!grackle)
1772 return -ENODEV;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* Turn off various things. Darwin does some retry tests here... */
1775 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0, PMU_POW0_OFF|PMU_POW0_HARD_DRIVE);
1776 pmu_wait_complete(&req);
1777 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1778 PMU_POW_OFF|PMU_POW_BACKLIGHT|PMU_POW_IRLED|PMU_POW_MEDIABAY);
1779 pmu_wait_complete(&req);
1780
1781 /* For 750, save backside cache setting and disable it */
1782 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
1783
1784 if (!__fake_sleep) {
1785 /* Ask the PMU to put us to sleep */
1786 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
1787 pmu_wait_complete(&req);
1788 }
1789
1790 /* The VIA is supposed not to be restored correctly*/
1791 save_via_state();
1792 /* We shut down some HW */
1793 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,1);
1794
1795 pci_read_config_word(grackle, 0x70, &pmcr1);
1796 /* Apparently, MacOS uses NAP mode for Grackle ??? */
1797 pmcr1 &= ~(GRACKLE_DOZE|GRACKLE_SLEEP);
1798 pmcr1 |= GRACKLE_PM|GRACKLE_NAP;
1799 pci_write_config_word(grackle, 0x70, pmcr1);
1800
1801 /* Call low-level ASM sleep handler */
1802 if (__fake_sleep)
1803 mdelay(5000);
1804 else
1805 low_sleep_handler();
1806
1807 /* We're awake again, stop grackle PM */
1808 pci_read_config_word(grackle, 0x70, &pmcr1);
1809 pmcr1 &= ~(GRACKLE_PM|GRACKLE_DOZE|GRACKLE_SLEEP|GRACKLE_NAP);
1810 pci_write_config_word(grackle, 0x70, pmcr1);
1811
Alan Coxc78f8302007-04-23 14:56:01 +01001812 pci_dev_put(grackle);
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* Make sure the PMU is idle */
1815 pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,0);
1816 restore_via_state();
1817
1818 /* Restore L2 cache */
1819 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
1820 _set_L2CR(save_l2cr);
1821
1822 /* Restore userland MMU context */
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +00001823 switch_mmu_context(NULL, current->active_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /* Power things up */
1826 pmu_unlock();
1827 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1828 pmu_wait_complete(&req);
1829 pmu_request(&req, NULL, 2, PMU_POWER_CTRL0,
1830 PMU_POW0_ON|PMU_POW0_HARD_DRIVE);
1831 pmu_wait_complete(&req);
1832 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
1833 PMU_POW_ON|PMU_POW_BACKLIGHT|PMU_POW_CHARGER|PMU_POW_IRLED|PMU_POW_MEDIABAY);
1834 pmu_wait_complete(&req);
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return 0;
1837}
1838
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05001839static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840powerbook_sleep_Core99(void)
1841{
1842 unsigned long save_l2cr;
1843 unsigned long save_l3cr;
1844 struct adb_request req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) < 0) {
1847 printk(KERN_ERR "Sleep mode not supported on this machine\n");
1848 return -ENOSYS;
1849 }
1850
1851 if (num_online_cpus() > 1 || cpu_is_offline(0))
1852 return -EAGAIN;
1853
Benjamin Herrenschmidtb16eeb42005-05-27 12:53:02 -07001854 /* Stop environment and ADB interrupts */
1855 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, 0);
1856 pmu_wait_complete(&req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 /* Tell PMU what events will wake us up */
1859 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_CLR_WAKEUP_EVENTS,
1860 0xff, 0xff);
1861 pmu_wait_complete(&req);
1862 pmu_request(&req, NULL, 4, PMU_POWER_EVENTS, PMU_PWR_SET_WAKEUP_EVENTS,
1863 0, PMU_PWR_WAKEUP_KEY |
1864 (option_lid_wakeup ? PMU_PWR_WAKEUP_LID_OPEN : 0));
1865 pmu_wait_complete(&req);
1866
1867 /* Save the state of the L2 and L3 caches */
1868 save_l3cr = _get_L3CR(); /* (returns -1 if not available) */
1869 save_l2cr = _get_L2CR(); /* (returns -1 if not available) */
1870
1871 if (!__fake_sleep) {
1872 /* Ask the PMU to put us to sleep */
1873 pmu_request(&req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
1874 pmu_wait_complete(&req);
1875 }
1876
1877 /* The VIA is supposed not to be restored correctly*/
1878 save_via_state();
1879
1880 /* Shut down various ASICs. There's a chance that we can no longer
1881 * talk to the PMU after this, so I moved it to _after_ sending the
1882 * sleep command to it. Still need to be checked.
1883 */
1884 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
1885
1886 /* Call low-level ASM sleep handler */
1887 if (__fake_sleep)
1888 mdelay(5000);
1889 else
1890 low_sleep_handler();
1891
1892 /* Restore Apple core ASICs state */
1893 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
1894
1895 /* Restore VIA */
1896 restore_via_state();
1897
Benjamin Herrenschmidt0086b5e2005-06-10 14:19:02 +10001898 /* tweak LPJ before cpufreq is there */
1899 loops_per_jiffy *= 2;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* Restore video */
1902 pmac_call_early_video_resume();
1903
1904 /* Restore L2 cache */
1905 if (save_l2cr != 0xffffffff && (save_l2cr & L2CR_L2E) != 0)
1906 _set_L2CR(save_l2cr);
1907 /* Restore L3 cache */
1908 if (save_l3cr != 0xffffffff && (save_l3cr & L3CR_L3E) != 0)
1909 _set_L3CR(save_l3cr);
1910
1911 /* Restore userland MMU context */
Benjamin Herrenschmidt5e696612008-12-18 19:13:24 +00001912 switch_mmu_context(NULL, current->active_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /* Tell PMU we are ready */
1915 pmu_unlock();
1916 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
1917 pmu_wait_complete(&req);
1918 pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
1919 pmu_wait_complete(&req);
1920
Benjamin Herrenschmidt0086b5e2005-06-10 14:19:02 +10001921 /* Restore LPJ, cpufreq will adjust the cpu frequency */
1922 loops_per_jiffy /= 2;
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926
1927#define PB3400_MEM_CTRL 0xf8000000
1928#define PB3400_MEM_CTRL_SLEEP 0x70
1929
Paul Mackerras887ef352007-12-19 22:45:31 +11001930static void __iomem *pb3400_mem_ctrl;
1931
1932static void powerbook_sleep_init_3400(void)
1933{
1934 /* map in the memory controller registers */
1935 pb3400_mem_ctrl = ioremap(PB3400_MEM_CTRL, 0x100);
1936 if (pb3400_mem_ctrl == NULL)
1937 printk(KERN_WARNING "ioremap failed: sleep won't be possible");
1938}
1939
1940static int powerbook_sleep_3400(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Johannes Bergf91266e2007-12-12 01:25:59 +11001942 int i, x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 unsigned int hid0;
Paul Mackerras887ef352007-12-19 22:45:31 +11001944 unsigned long msr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 struct adb_request sleep_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 unsigned int __iomem *mem_ctrl_sleep;
1947
Paul Mackerras887ef352007-12-19 22:45:31 +11001948 if (pb3400_mem_ctrl == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return -ENOMEM;
Paul Mackerras887ef352007-12-19 22:45:31 +11001950 mem_ctrl_sleep = pb3400_mem_ctrl + PB3400_MEM_CTRL_SLEEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /* Set the memory controller to keep the memory refreshed
1953 while we're asleep */
1954 for (i = 0x403f; i >= 0x4000; --i) {
1955 out_be32(mem_ctrl_sleep, i);
1956 do {
1957 x = (in_be32(mem_ctrl_sleep) >> 16) & 0x3ff;
1958 } while (x == 0);
1959 if (x >= 0x100)
1960 break;
1961 }
1962
1963 /* Ask the PMU to put us to sleep */
1964 pmu_request(&sleep_req, NULL, 5, PMU_SLEEP, 'M', 'A', 'T', 'T');
Paul Mackerras887ef352007-12-19 22:45:31 +11001965 pmu_wait_complete(&sleep_req);
1966 pmu_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Paul Mackerras887ef352007-12-19 22:45:31 +11001968 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 asleep = 1;
1971
1972 /* Put the CPU into sleep mode */
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001973 hid0 = mfspr(SPRN_HID0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 hid0 = (hid0 & ~(HID0_NAP | HID0_DOZE)) | HID0_SLEEP;
Benjamin Herrenschmidt21fe3302005-11-07 16:41:59 +11001975 mtspr(SPRN_HID0, hid0);
Paul Mackerras887ef352007-12-19 22:45:31 +11001976 local_irq_enable();
1977 msr = mfmsr() | MSR_POW;
1978 while (asleep) {
1979 mb();
1980 mtmsr(msr);
1981 isync();
1982 }
1983 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* OK, we're awake again, start restoring things */
1986 out_be32(mem_ctrl_sleep, 0x3f);
Paul Mackerras887ef352007-12-19 22:45:31 +11001987 pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return 0;
1990}
1991
Johannes Bergf91266e2007-12-12 01:25:59 +11001992#endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994/*
1995 * Support for /dev/pmu device
1996 */
1997#define RB_SIZE 0x10
1998struct pmu_private {
1999 struct list_head list;
2000 int rb_get;
2001 int rb_put;
2002 struct rb_entry {
2003 unsigned short len;
2004 unsigned char data[16];
2005 } rb_buf[RB_SIZE];
2006 wait_queue_head_t wait;
2007 spinlock_t lock;
2008#if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2009 int backlight_locker;
Michael Hanselmann4b755992006-07-30 03:04:19 -07002010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011};
2012
2013static LIST_HEAD(all_pmu_pvt);
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002014static DEFINE_SPINLOCK(all_pvt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002016static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017pmu_pass_intr(unsigned char *data, int len)
2018{
2019 struct pmu_private *pp;
2020 struct list_head *list;
2021 int i;
2022 unsigned long flags;
2023
2024 if (len > sizeof(pp->rb_buf[0].data))
2025 len = sizeof(pp->rb_buf[0].data);
2026 spin_lock_irqsave(&all_pvt_lock, flags);
2027 for (list = &all_pmu_pvt; (list = list->next) != &all_pmu_pvt; ) {
2028 pp = list_entry(list, struct pmu_private, list);
2029 spin_lock(&pp->lock);
2030 i = pp->rb_put + 1;
2031 if (i >= RB_SIZE)
2032 i = 0;
2033 if (i != pp->rb_get) {
2034 struct rb_entry *rp = &pp->rb_buf[pp->rb_put];
2035 rp->len = len;
2036 memcpy(rp->data, data, len);
2037 pp->rb_put = i;
2038 wake_up_interruptible(&pp->wait);
2039 }
2040 spin_unlock(&pp->lock);
2041 }
2042 spin_unlock_irqrestore(&all_pvt_lock, flags);
2043}
2044
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002045static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046pmu_open(struct inode *inode, struct file *file)
2047{
2048 struct pmu_private *pp;
2049 unsigned long flags;
2050
2051 pp = kmalloc(sizeof(struct pmu_private), GFP_KERNEL);
2052 if (pp == 0)
2053 return -ENOMEM;
2054 pp->rb_get = pp->rb_put = 0;
2055 spin_lock_init(&pp->lock);
2056 init_waitqueue_head(&pp->wait);
Arnd Bergmannffe83732008-05-20 19:16:58 +02002057 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 spin_lock_irqsave(&all_pvt_lock, flags);
2059#if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
2060 pp->backlight_locker = 0;
Michael Hanselmann4b755992006-07-30 03:04:19 -07002061#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 list_add(&pp->list, &all_pmu_pvt);
2063 spin_unlock_irqrestore(&all_pvt_lock, flags);
2064 file->private_data = pp;
Arnd Bergmannffe83732008-05-20 19:16:58 +02002065 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return 0;
2067}
2068
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002069static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070pmu_read(struct file *file, char __user *buf,
2071 size_t count, loff_t *ppos)
2072{
2073 struct pmu_private *pp = file->private_data;
2074 DECLARE_WAITQUEUE(wait, current);
2075 unsigned long flags;
2076 int ret = 0;
2077
2078 if (count < 1 || pp == 0)
2079 return -EINVAL;
2080 if (!access_ok(VERIFY_WRITE, buf, count))
2081 return -EFAULT;
2082
2083 spin_lock_irqsave(&pp->lock, flags);
2084 add_wait_queue(&pp->wait, &wait);
2085 current->state = TASK_INTERRUPTIBLE;
2086
2087 for (;;) {
2088 ret = -EAGAIN;
2089 if (pp->rb_get != pp->rb_put) {
2090 int i = pp->rb_get;
2091 struct rb_entry *rp = &pp->rb_buf[i];
2092 ret = rp->len;
2093 spin_unlock_irqrestore(&pp->lock, flags);
2094 if (ret > count)
2095 ret = count;
2096 if (ret > 0 && copy_to_user(buf, rp->data, ret))
2097 ret = -EFAULT;
2098 if (++i >= RB_SIZE)
2099 i = 0;
2100 spin_lock_irqsave(&pp->lock, flags);
2101 pp->rb_get = i;
2102 }
2103 if (ret >= 0)
2104 break;
2105 if (file->f_flags & O_NONBLOCK)
2106 break;
2107 ret = -ERESTARTSYS;
2108 if (signal_pending(current))
2109 break;
2110 spin_unlock_irqrestore(&pp->lock, flags);
2111 schedule();
2112 spin_lock_irqsave(&pp->lock, flags);
2113 }
2114 current->state = TASK_RUNNING;
2115 remove_wait_queue(&pp->wait, &wait);
2116 spin_unlock_irqrestore(&pp->lock, flags);
2117
2118 return ret;
2119}
2120
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002121static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122pmu_write(struct file *file, const char __user *buf,
2123 size_t count, loff_t *ppos)
2124{
2125 return 0;
2126}
2127
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002128static unsigned int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129pmu_fpoll(struct file *filp, poll_table *wait)
2130{
2131 struct pmu_private *pp = filp->private_data;
2132 unsigned int mask = 0;
2133 unsigned long flags;
2134
2135 if (pp == 0)
2136 return 0;
2137 poll_wait(filp, &pp->wait, wait);
2138 spin_lock_irqsave(&pp->lock, flags);
2139 if (pp->rb_get != pp->rb_put)
2140 mask |= POLLIN;
2141 spin_unlock_irqrestore(&pp->lock, flags);
2142 return mask;
2143}
2144
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002145static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146pmu_release(struct inode *inode, struct file *file)
2147{
2148 struct pmu_private *pp = file->private_data;
2149 unsigned long flags;
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (pp != 0) {
2152 file->private_data = NULL;
2153 spin_lock_irqsave(&all_pvt_lock, flags);
2154 list_del(&pp->list);
2155 spin_unlock_irqrestore(&all_pvt_lock, flags);
Michael Hanselmann4b755992006-07-30 03:04:19 -07002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157#if defined(CONFIG_INPUT_ADBHID) && defined(CONFIG_PMAC_BACKLIGHT)
Michael Hanselmann4b755992006-07-30 03:04:19 -07002158 if (pp->backlight_locker)
2159 pmac_backlight_enable();
2160#endif
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 kfree(pp);
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 return 0;
2165}
2166
Johannes Bergf91266e2007-12-12 01:25:59 +11002167#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
Scott Wood7ac5dde2007-12-13 04:35:19 +11002168static void pmac_suspend_disable_irqs(void)
Johannes Bergf91266e2007-12-12 01:25:59 +11002169{
Johannes Bergf91266e2007-12-12 01:25:59 +11002170 /* Call platform functions marked "on sleep" */
2171 pmac_pfunc_i2c_suspend();
2172 pmac_pfunc_base_suspend();
Johannes Bergf91266e2007-12-12 01:25:59 +11002173}
2174
2175static int powerbook_sleep(suspend_state_t state)
2176{
2177 int error = 0;
2178
2179 /* Wait for completion of async requests */
2180 while (!batt_req.complete)
2181 pmu_poll();
2182
2183 /* Giveup the lazy FPU & vec so we don't have to back them
2184 * up from the low level code
2185 */
2186 enable_kernel_fp();
2187
2188#ifdef CONFIG_ALTIVEC
2189 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2190 enable_kernel_altivec();
2191#endif /* CONFIG_ALTIVEC */
2192
2193 switch (pmu_kind) {
2194 case PMU_OHARE_BASED:
2195 error = powerbook_sleep_3400();
2196 break;
2197 case PMU_HEATHROW_BASED:
2198 case PMU_PADDINGTON_BASED:
2199 error = powerbook_sleep_grackle();
2200 break;
2201 case PMU_KEYLARGO_BASED:
2202 error = powerbook_sleep_Core99();
2203 break;
2204 default:
2205 return -ENOSYS;
2206 }
2207
2208 if (error)
2209 return error;
2210
2211 mdelay(100);
2212
Johannes Bergf91266e2007-12-12 01:25:59 +11002213 return 0;
2214}
2215
Scott Wood7ac5dde2007-12-13 04:35:19 +11002216static void pmac_suspend_enable_irqs(void)
Johannes Bergf91266e2007-12-12 01:25:59 +11002217{
2218 /* Force a poll of ADB interrupts */
2219 adb_int_pending = 1;
2220 via_pmu_interrupt(0, NULL);
2221
Johannes Bergf91266e2007-12-12 01:25:59 +11002222 mdelay(10);
Johannes Bergf91266e2007-12-12 01:25:59 +11002223
2224 /* Call platform functions marked "on wake" */
2225 pmac_pfunc_base_resume();
2226 pmac_pfunc_i2c_resume();
2227}
2228
2229static int pmu_sleep_valid(suspend_state_t state)
2230{
2231 return state == PM_SUSPEND_MEM
2232 && (pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, -1) >= 0);
2233}
2234
2235static struct platform_suspend_ops pmu_pm_ops = {
2236 .enter = powerbook_sleep,
2237 .valid = pmu_sleep_valid,
2238};
2239
2240static int register_pmu_pm_ops(void)
2241{
Scott Wood7ac5dde2007-12-13 04:35:19 +11002242 if (pmu_kind == PMU_OHARE_BASED)
2243 powerbook_sleep_init_3400();
2244 ppc_md.suspend_disable_irqs = pmac_suspend_disable_irqs;
2245 ppc_md.suspend_enable_irqs = pmac_suspend_enable_irqs;
Johannes Bergf91266e2007-12-12 01:25:59 +11002246 suspend_set_ops(&pmu_pm_ops);
2247
2248 return 0;
2249}
2250
2251device_initcall(register_pmu_pm_ops);
2252#endif
2253
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002254static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255pmu_ioctl(struct inode * inode, struct file *filp,
2256 u_int cmd, u_long arg)
2257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 __u32 __user *argp = (__u32 __user *)arg;
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07002259 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 switch (cmd) {
2262 case PMU_IOC_SLEEP:
2263 if (!capable(CAP_SYS_ADMIN))
2264 return -EACCES;
Johannes Bergf91266e2007-12-12 01:25:59 +11002265 return pm_suspend(PM_SUSPEND_MEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 case PMU_IOC_CAN_SLEEP:
Johannes Bergf91266e2007-12-12 01:25:59 +11002267 if (pmac_call_feature(PMAC_FTR_SLEEP_STATE, NULL, 0, -1) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 return put_user(0, argp);
2269 else
2270 return put_user(1, argp);
2271
Michael Hanselmann5474c122006-06-25 05:47:08 -07002272#ifdef CONFIG_PMAC_BACKLIGHT_LEGACY
2273 /* Compatibility ioctl's for backlight */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 case PMU_IOC_GET_BACKLIGHT:
Michael Hanselmann5474c122006-06-25 05:47:08 -07002275 {
2276 int brightness;
2277
Michael Hanselmann5474c122006-06-25 05:47:08 -07002278 brightness = pmac_backlight_get_legacy_brightness();
2279 if (brightness < 0)
2280 return brightness;
2281 else
2282 return put_user(brightness, argp);
2283
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 case PMU_IOC_SET_BACKLIGHT:
2286 {
Michael Hanselmann5474c122006-06-25 05:47:08 -07002287 int brightness;
2288
Michael Hanselmann5474c122006-06-25 05:47:08 -07002289 error = get_user(brightness, argp);
2290 if (error)
2291 return error;
2292
2293 return pmac_backlight_set_legacy_brightness(brightness);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295#ifdef CONFIG_INPUT_ADBHID
2296 case PMU_IOC_GRAB_BACKLIGHT: {
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07002297 struct pmu_private *pp = filp->private_data;
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (pp->backlight_locker)
2300 return 0;
Michael Hanselmann4b755992006-07-30 03:04:19 -07002301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 pp->backlight_locker = 1;
Michael Hanselmann4b755992006-07-30 03:04:19 -07002303 pmac_backlight_disable();
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return 0;
2306 }
2307#endif /* CONFIG_INPUT_ADBHID */
Michael Hanselmann5474c122006-06-25 05:47:08 -07002308#endif /* CONFIG_PMAC_BACKLIGHT_LEGACY */
Michael Hanselmann4b755992006-07-30 03:04:19 -07002309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 case PMU_IOC_GET_MODEL:
2311 return put_user(pmu_kind, argp);
2312 case PMU_IOC_HAS_ADB:
2313 return put_user(pmu_has_adb, argp);
2314 }
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07002315 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Arjan van de Venfa027c22007-02-12 00:55:33 -08002318static const struct file_operations pmu_device_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 .read = pmu_read,
2320 .write = pmu_write,
2321 .poll = pmu_fpoll,
2322 .ioctl = pmu_ioctl,
2323 .open = pmu_open,
2324 .release = pmu_release,
2325};
2326
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002327static struct miscdevice pmu_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 PMU_MINOR, "pmu", &pmu_device_fops
2329};
2330
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07002331static int pmu_device_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 if (!via)
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07002334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (misc_register(&pmu_device) < 0)
2336 printk(KERN_ERR "via-pmu: cannot register misc device.\n");
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07002337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
Benjamin Herrenschmidt8c870932005-06-27 14:36:34 -07002339device_initcall(pmu_device_init);
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342#ifdef DEBUG_SLEEP
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002343static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344polled_handshake(volatile unsigned char __iomem *via)
2345{
2346 via[B] &= ~TREQ; eieio();
2347 while ((via[B] & TACK) != 0)
2348 ;
2349 via[B] |= TREQ; eieio();
2350 while ((via[B] & TACK) == 0)
2351 ;
2352}
2353
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002354static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355polled_send_byte(volatile unsigned char __iomem *via, int x)
2356{
2357 via[ACR] |= SR_OUT | SR_EXT; eieio();
2358 via[SR] = x; eieio();
2359 polled_handshake(via);
2360}
2361
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002362static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363polled_recv_byte(volatile unsigned char __iomem *via)
2364{
2365 int x;
2366
2367 via[ACR] = (via[ACR] & ~SR_OUT) | SR_EXT; eieio();
2368 x = via[SR]; eieio();
2369 polled_handshake(via);
2370 x = via[SR]; eieio();
2371 return x;
2372}
2373
Jon Loeligeraacaf9b2005-09-17 10:36:54 -05002374int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375pmu_polled_request(struct adb_request *req)
2376{
2377 unsigned long flags;
2378 int i, l, c;
2379 volatile unsigned char __iomem *v = via;
2380
2381 req->complete = 1;
2382 c = req->data[0];
2383 l = pmu_data_len[c][0];
2384 if (l >= 0 && req->nbytes != l + 1)
2385 return -EINVAL;
2386
2387 local_irq_save(flags);
2388 while (pmu_state != idle)
2389 pmu_poll();
2390
2391 while ((via[B] & TACK) == 0)
2392 ;
2393 polled_send_byte(v, c);
2394 if (l < 0) {
2395 l = req->nbytes - 1;
2396 polled_send_byte(v, l);
2397 }
2398 for (i = 1; i <= l; ++i)
2399 polled_send_byte(v, req->data[i]);
2400
2401 l = pmu_data_len[c][1];
2402 if (l < 0)
2403 l = polled_recv_byte(v);
2404 for (i = 0; i < l; ++i)
2405 req->reply[i + req->reply_len] = polled_recv_byte(v);
2406
2407 if (req->done)
2408 (*req->done)(req);
2409
2410 local_irq_restore(flags);
2411 return 0;
2412}
Johannes Bergf91266e2007-12-12 01:25:59 +11002413
2414/* N.B. This doesn't work on the 3400 */
2415void pmu_blink(int n)
2416{
2417 struct adb_request req;
2418
2419 memset(&req, 0, sizeof(req));
2420
2421 for (; n > 0; --n) {
2422 req.nbytes = 4;
2423 req.done = NULL;
2424 req.data[0] = 0xee;
2425 req.data[1] = 4;
2426 req.data[2] = 0;
2427 req.data[3] = 1;
2428 req.reply[0] = ADB_RET_OK;
2429 req.reply_len = 1;
2430 req.reply_expected = 0;
2431 pmu_polled_request(&req);
2432 mdelay(50);
2433 req.nbytes = 4;
2434 req.done = NULL;
2435 req.data[0] = 0xee;
2436 req.data[1] = 4;
2437 req.data[2] = 0;
2438 req.data[3] = 0;
2439 req.reply[0] = ADB_RET_OK;
2440 req.reply_len = 1;
2441 req.reply_expected = 0;
2442 pmu_polled_request(&req);
2443 mdelay(50);
2444 }
2445 mdelay(50);
2446}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447#endif /* DEBUG_SLEEP */
2448
Johannes Bergf91266e2007-12-12 01:25:59 +11002449#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
Johannes Bergf5965752007-05-08 01:08:00 +10002450int pmu_sys_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Pavel Machek3bfffd92005-04-16 15:25:37 -07002452static int pmu_sys_suspend(struct sys_device *sysdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
Pavel Machekca078ba2005-09-03 15:56:57 -07002454 if (state.event != PM_EVENT_SUSPEND || pmu_sys_suspended)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return 0;
2456
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +11002457 /* Suspend PMU event interrupts */\
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 pmu_suspend();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 pmu_sys_suspended = 1;
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +11002460
2461#ifdef CONFIG_PMAC_BACKLIGHT
2462 /* Tell backlight code not to muck around with the chip anymore */
2463 pmu_backlight_set_sleep(1);
2464#endif
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return 0;
2467}
2468
2469static int pmu_sys_resume(struct sys_device *sysdev)
2470{
2471 struct adb_request req;
2472
2473 if (!pmu_sys_suspended)
2474 return 0;
2475
2476 /* Tell PMU we are ready */
2477 pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
2478 pmu_wait_complete(&req);
2479
Benjamin Herrenschmidt0094f2c2007-12-20 15:00:21 +11002480#ifdef CONFIG_PMAC_BACKLIGHT
2481 /* Tell backlight code it can use the chip again */
2482 pmu_backlight_set_sleep(0);
2483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 /* Resume PMU event interrupts */
2485 pmu_resume();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 pmu_sys_suspended = 0;
2487
2488 return 0;
2489}
2490
Johannes Bergf91266e2007-12-12 01:25:59 +11002491#endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493static struct sysdev_class pmu_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002494 .name = "pmu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495};
2496
2497static struct sys_device device_pmu = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 .cls = &pmu_sysclass,
2499};
2500
2501static struct sysdev_driver driver_pmu = {
Johannes Bergf91266e2007-12-12 01:25:59 +11002502#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 .suspend = &pmu_sys_suspend,
2504 .resume = &pmu_sys_resume,
Johannes Bergf91266e2007-12-12 01:25:59 +11002505#endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506};
2507
2508static int __init init_pmu_sysfs(void)
2509{
2510 int rc;
2511
2512 rc = sysdev_class_register(&pmu_sysclass);
2513 if (rc) {
2514 printk(KERN_ERR "Failed registering PMU sys class\n");
2515 return -ENODEV;
2516 }
2517 rc = sysdev_register(&device_pmu);
2518 if (rc) {
2519 printk(KERN_ERR "Failed registering PMU sys device\n");
2520 return -ENODEV;
2521 }
2522 rc = sysdev_driver_register(&pmu_sysclass, &driver_pmu);
2523 if (rc) {
2524 printk(KERN_ERR "Failed registering PMU sys driver\n");
2525 return -ENODEV;
2526 }
2527 return 0;
2528}
2529
2530subsys_initcall(init_pmu_sysfs);
2531
2532EXPORT_SYMBOL(pmu_request);
Benjamin Herrenschmidt730745a2006-01-07 11:30:44 +11002533EXPORT_SYMBOL(pmu_queue_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534EXPORT_SYMBOL(pmu_poll);
2535EXPORT_SYMBOL(pmu_poll_adb);
2536EXPORT_SYMBOL(pmu_wait_complete);
2537EXPORT_SYMBOL(pmu_suspend);
2538EXPORT_SYMBOL(pmu_resume);
2539EXPORT_SYMBOL(pmu_unlock);
Guido Guenther620a2452008-03-09 06:20:17 +11002540#if defined(CONFIG_PPC32)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541EXPORT_SYMBOL(pmu_enable_irled);
2542EXPORT_SYMBOL(pmu_battery_count);
2543EXPORT_SYMBOL(pmu_batteries);
2544EXPORT_SYMBOL(pmu_power_flags);
Johannes Bergf91266e2007-12-12 01:25:59 +11002545#endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546