forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
via-pmu.c
2594 lines (2265 loc) · 61.7 KB
/
via-pmu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Device driver for the via-pmu on Apple Powermacs.
*
* The VIA (versatile interface adapter) interfaces to the PMU,
* a 6805 microprocessor core whose primary function is to control
* battery charging and system power on the PowerBook 3400 and 2400.
* The PMU also controls the ADB (Apple Desktop Bus) which connects
* to the keyboard and mouse, as well as the non-volatile RAM
* and the RTC (real time clock) chip.
*
* Copyright (C) 1998 Paul Mackerras and Fabio Riccardi.
* Copyright (C) 2001-2002 Benjamin Herrenschmidt
* Copyright (C) 2006-2007 Johannes Berg
*
* THIS DRIVER IS BECOMING A TOTAL MESS !
* - Cleanup atomically disabling reply to PMU events after
* a sleep or a freq. switch
*
*/
#include <stdarg.h>
#include <linux/mutex.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/miscdevice.h>
#include <linux/blkdev.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/adb.h>
#include <linux/pmu.h>
#include <linux/cuda.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/pm.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/syscore_ops.h>
#include <linux/freezer.h>
#include <linux/syscalls.h>
#include <linux/suspend.h>
#include <linux/cpu.h>
#include <linux/compat.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <asm/prom.h>
#include <asm/machdep.h>
#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/sections.h>
#include <asm/irq.h>
#include <asm/pmac_feature.h>
#include <asm/pmac_pfunc.h>
#include <asm/pmac_low_i2c.h>
#include <asm/uaccess.h>
#include <asm/mmu_context.h>
#include <asm/cputable.h>
#include <asm/time.h>
#include <asm/backlight.h>
#include "via-pmu-event.h"
/* Some compile options */
#undef DEBUG_SLEEP
/* Misc minor number allocated for /dev/pmu */
#define PMU_MINOR 154
/* How many iterations between battery polls */
#define BATTERY_POLLING_COUNT 2
static DEFINE_MUTEX(pmu_info_proc_mutex);
static volatile unsigned char __iomem *via;
/* VIA registers - spaced 0x200 bytes apart */
#define RS 0x200 /* skip between registers */
#define B 0 /* B-side data */
#define A RS /* A-side data */
#define DIRB (2*RS) /* B-side direction (1=output) */
#define DIRA (3*RS) /* A-side direction (1=output) */
#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
#define SR (10*RS) /* Shift register */
#define ACR (11*RS) /* Auxiliary control register */
#define PCR (12*RS) /* Peripheral control register */
#define IFR (13*RS) /* Interrupt flag register */
#define IER (14*RS) /* Interrupt enable register */
#define ANH (15*RS) /* A-side data, no handshake */
/* Bits in B data register: both active low */
#define TACK 0x08 /* Transfer acknowledge (input) */
#define TREQ 0x10 /* Transfer request (output) */
/* Bits in ACR */
#define SR_CTRL 0x1c /* Shift register control bits */
#define SR_EXT 0x0c /* Shift on external clock */
#define SR_OUT 0x10 /* Shift out if 1 */
/* Bits in IFR and IER */
#define IER_SET 0x80 /* set bits in IER */
#define IER_CLR 0 /* clear bits in IER */
#define SR_INT 0x04 /* Shift register full/empty */
#define CB2_INT 0x08
#define CB1_INT 0x10 /* transition on CB1 input */
static volatile enum pmu_state {
idle,
sending,
intack,
reading,
reading_intr,
locked,
} pmu_state;
static volatile enum int_data_state {
int_data_empty,
int_data_fill,
int_data_ready,
int_data_flush
} int_data_state[2] = { int_data_empty, int_data_empty };
static struct adb_request *current_req;
static struct adb_request *last_req;
static struct adb_request *req_awaiting_reply;
static unsigned char interrupt_data[2][32];
static int interrupt_data_len[2];
static int int_data_last;
static unsigned char *reply_ptr;
static int data_index;
static int data_len;
static volatile int adb_int_pending;
static volatile int disable_poll;
static struct device_node *vias;
static int pmu_kind = PMU_UNKNOWN;
static int pmu_fully_inited;
static int pmu_has_adb;
static struct device_node *gpio_node;
static unsigned char __iomem *gpio_reg;
static int gpio_irq = NO_IRQ;
static int gpio_irq_enabled = -1;
static volatile int pmu_suspended;
static spinlock_t pmu_lock;
static u8 pmu_intr_mask;
static int pmu_version;
static int drop_interrupts;
#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
static int option_lid_wakeup = 1;
#endif /* CONFIG_SUSPEND && CONFIG_PPC32 */
static unsigned long async_req_locks;
static unsigned int pmu_irq_stats[11];
static struct proc_dir_entry *proc_pmu_root;
static struct proc_dir_entry *proc_pmu_info;
static struct proc_dir_entry *proc_pmu_irqstats;
static struct proc_dir_entry *proc_pmu_options;
static int option_server_mode;
int pmu_battery_count;
int pmu_cur_battery;
unsigned int pmu_power_flags = PMU_PWR_AC_PRESENT;
struct pmu_battery_info pmu_batteries[PMU_MAX_BATTERIES];
static int query_batt_timer = BATTERY_POLLING_COUNT;
static struct adb_request batt_req;
static struct proc_dir_entry *proc_pmu_batt[PMU_MAX_BATTERIES];
int __fake_sleep;
int asleep;
#ifdef CONFIG_ADB
static int adb_dev_map;
static int pmu_adb_flags;
static int pmu_probe(void);
static int pmu_init(void);
static int pmu_send_request(struct adb_request *req, int sync);
static int pmu_adb_autopoll(int devs);
static int pmu_adb_reset_bus(void);
#endif /* CONFIG_ADB */
static int init_pmu(void);
static void pmu_start(void);
static irqreturn_t via_pmu_interrupt(int irq, void *arg);
static irqreturn_t gpio1_interrupt(int irq, void *arg);
static const struct file_operations pmu_info_proc_fops;
static const struct file_operations pmu_irqstats_proc_fops;
static void pmu_pass_intr(unsigned char *data, int len);
static const struct file_operations pmu_battery_proc_fops;
static const struct file_operations pmu_options_proc_fops;
#ifdef CONFIG_ADB
struct adb_driver via_pmu_driver = {
"PMU",
pmu_probe,
pmu_init,
pmu_send_request,
pmu_adb_autopoll,
pmu_poll_adb,
pmu_adb_reset_bus
};
#endif /* CONFIG_ADB */
extern void low_sleep_handler(void);
extern void enable_kernel_altivec(void);
extern void enable_kernel_fp(void);
#ifdef DEBUG_SLEEP
int pmu_polled_request(struct adb_request *req);
void pmu_blink(int n);
#endif
/*
* This table indicates for each PMU opcode:
* - the number of data bytes to be sent with the command, or -1
* if a length byte should be sent,
* - the number of response bytes which the PMU will return, or
* -1 if it will send a length byte.
*/
static const s8 pmu_data_len[256][2] = {
/* 0 1 2 3 4 5 6 7 */
/*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
/*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
/*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
/*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
/*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
/*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
/*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
/*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
/*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
/*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
/*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
/*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
/*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
/*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
/*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
/*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
/*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
/*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
/*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
/*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
/*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
};
static char *pbook_type[] = {
"Unknown PowerBook",
"PowerBook 2400/3400/3500(G3)",
"PowerBook G3 Series",
"1999 PowerBook G3",
"Core99"
};
int __init find_via_pmu(void)
{
u64 taddr;
const u32 *reg;
if (via != 0)
return 1;
vias = of_find_node_by_name(NULL, "via-pmu");
if (vias == NULL)
return 0;
reg = of_get_property(vias, "reg", NULL);
if (reg == NULL) {
printk(KERN_ERR "via-pmu: No \"reg\" property !\n");
goto fail;
}
taddr = of_translate_address(vias, reg);
if (taddr == OF_BAD_ADDR) {
printk(KERN_ERR "via-pmu: Can't translate address !\n");
goto fail;
}
spin_lock_init(&pmu_lock);
pmu_has_adb = 1;
pmu_intr_mask = PMU_INT_PCEJECT |
PMU_INT_SNDBRT |
PMU_INT_ADB |
PMU_INT_TICK;
if (vias->parent->name && ((strcmp(vias->parent->name, "ohare") == 0)
|| of_device_is_compatible(vias->parent, "ohare")))
pmu_kind = PMU_OHARE_BASED;
else if (of_device_is_compatible(vias->parent, "paddington"))
pmu_kind = PMU_PADDINGTON_BASED;
else if (of_device_is_compatible(vias->parent, "heathrow"))
pmu_kind = PMU_HEATHROW_BASED;
else if (of_device_is_compatible(vias->parent, "Keylargo")
|| of_device_is_compatible(vias->parent, "K2-Keylargo")) {
struct device_node *gpiop;
struct device_node *adbp;
u64 gaddr = OF_BAD_ADDR;
pmu_kind = PMU_KEYLARGO_BASED;
adbp = of_find_node_by_type(NULL, "adb");
pmu_has_adb = (adbp != NULL);
of_node_put(adbp);
pmu_intr_mask = PMU_INT_PCEJECT |
PMU_INT_SNDBRT |
PMU_INT_ADB |
PMU_INT_TICK |
PMU_INT_ENVIRONMENT;
gpiop = of_find_node_by_name(NULL, "gpio");
if (gpiop) {
reg = of_get_property(gpiop, "reg", NULL);
if (reg)
gaddr = of_translate_address(gpiop, reg);
if (gaddr != OF_BAD_ADDR)
gpio_reg = ioremap(gaddr, 0x10);
}
if (gpio_reg == NULL) {
printk(KERN_ERR "via-pmu: Can't find GPIO reg !\n");
goto fail_gpio;
}
} else
pmu_kind = PMU_UNKNOWN;
via = ioremap(taddr, 0x2000);
if (via == NULL) {
printk(KERN_ERR "via-pmu: Can't map address !\n");
goto fail;
}
out_8(&via[IER], IER_CLR | 0x7f); /* disable all intrs */
out_8(&via[IFR], 0x7f); /* clear IFR */
pmu_state = idle;
if (!init_pmu()) {
via = NULL;
return 0;
}
printk(KERN_INFO "PMU driver v%d initialized for %s, firmware: %02x\n",
PMU_DRIVER_VERSION, pbook_type[pmu_kind], pmu_version);
sys_ctrler = SYS_CTRLER_PMU;
return 1;
fail:
of_node_put(vias);
iounmap(gpio_reg);
gpio_reg = NULL;
fail_gpio:
vias = NULL;
return 0;
}
#ifdef CONFIG_ADB
static int pmu_probe(void)
{
return vias == NULL? -ENODEV: 0;
}
static int __init pmu_init(void)
{
if (vias == NULL)
return -ENODEV;
return 0;
}
#endif /* CONFIG_ADB */
/*
* We can't wait until pmu_init gets called, that happens too late.
* It happens after IDE and SCSI initialization, which can take a few
* seconds, and by that time the PMU could have given up on us and
* turned us off.
* Thus this is called with arch_initcall rather than device_initcall.
*/
static int __init via_pmu_start(void)
{
unsigned int irq;
if (vias == NULL)
return -ENODEV;
batt_req.complete = 1;
irq = irq_of_parse_and_map(vias, 0);
if (irq == NO_IRQ) {
printk(KERN_ERR "via-pmu: can't map interrupt\n");
return -ENODEV;
}
/* We set IRQF_NO_SUSPEND because we don't want the interrupt
* to be disabled between the 2 passes of driver suspend, we
* control our own disabling for that one
*/
if (request_irq(irq, via_pmu_interrupt, IRQF_NO_SUSPEND,
"VIA-PMU", (void *)0)) {
printk(KERN_ERR "via-pmu: can't request irq %d\n", irq);
return -ENODEV;
}
if (pmu_kind == PMU_KEYLARGO_BASED) {
gpio_node = of_find_node_by_name(NULL, "extint-gpio1");
if (gpio_node == NULL)
gpio_node = of_find_node_by_name(NULL,
"pmu-interrupt");
if (gpio_node)
gpio_irq = irq_of_parse_and_map(gpio_node, 0);
if (gpio_irq != NO_IRQ) {
if (request_irq(gpio_irq, gpio1_interrupt, IRQF_TIMER,
"GPIO1 ADB", (void *)0))
printk(KERN_ERR "pmu: can't get irq %d"
" (GPIO1)\n", gpio_irq);
else
gpio_irq_enabled = 1;
}
}
/* Enable interrupts */
out_8(&via[IER], IER_SET | SR_INT | CB1_INT);
pmu_fully_inited = 1;
/* Make sure PMU settle down before continuing. This is _very_ important
* since the IDE probe may shut interrupts down for quite a bit of time. If
* a PMU communication is pending while this happens, the PMU may timeout
* Not that on Core99 machines, the PMU keeps sending us environement
* messages, we should find a way to either fix IDE or make it call
* pmu_suspend() before masking interrupts. This can also happens while
* scolling with some fbdevs.
*/
do {
pmu_poll();
} while (pmu_state != idle);
return 0;
}
arch_initcall(via_pmu_start);
/*
* This has to be done after pci_init, which is a subsys_initcall.
*/
static int __init via_pmu_dev_init(void)
{
if (vias == NULL)
return -ENODEV;
#ifdef CONFIG_PMAC_BACKLIGHT
/* Initialize backlight */
pmu_backlight_init();
#endif
#ifdef CONFIG_PPC32
if (of_machine_is_compatible("AAPL,3400/2400") ||
of_machine_is_compatible("AAPL,3500")) {
int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
NULL, PMAC_MB_INFO_MODEL, 0);
pmu_battery_count = 1;
if (mb == PMAC_TYPE_COMET)
pmu_batteries[0].flags |= PMU_BATT_TYPE_COMET;
else
pmu_batteries[0].flags |= PMU_BATT_TYPE_HOOPER;
} else if (of_machine_is_compatible("AAPL,PowerBook1998") ||
of_machine_is_compatible("PowerBook1,1")) {
pmu_battery_count = 2;
pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
} else {
struct device_node* prim =
of_find_node_by_name(NULL, "power-mgt");
const u32 *prim_info = NULL;
if (prim)
prim_info = of_get_property(prim, "prim-info", NULL);
if (prim_info) {
/* Other stuffs here yet unknown */
pmu_battery_count = (prim_info[6] >> 16) & 0xff;
pmu_batteries[0].flags |= PMU_BATT_TYPE_SMART;
if (pmu_battery_count > 1)
pmu_batteries[1].flags |= PMU_BATT_TYPE_SMART;
}
of_node_put(prim);
}
#endif /* CONFIG_PPC32 */
/* Create /proc/pmu */
proc_pmu_root = proc_mkdir("pmu", NULL);
if (proc_pmu_root) {
long i;
for (i=0; i<pmu_battery_count; i++) {
char title[16];
sprintf(title, "battery_%ld", i);
proc_pmu_batt[i] = proc_create_data(title, 0, proc_pmu_root,
&pmu_battery_proc_fops, (void *)i);
}
proc_pmu_info = proc_create("info", 0, proc_pmu_root, &pmu_info_proc_fops);
proc_pmu_irqstats = proc_create("interrupts", 0, proc_pmu_root,
&pmu_irqstats_proc_fops);
proc_pmu_options = proc_create("options", 0600, proc_pmu_root,
&pmu_options_proc_fops);
}
return 0;
}
device_initcall(via_pmu_dev_init);
static int
init_pmu(void)
{
int timeout;
struct adb_request req;
out_8(&via[B], via[B] | TREQ); /* negate TREQ */
out_8(&via[DIRB], (via[DIRB] | TREQ) & ~TACK); /* TACK in, TREQ out */
pmu_request(&req, NULL, 2, PMU_SET_INTR_MASK, pmu_intr_mask);
timeout = 100000;
while (!req.complete) {
if (--timeout < 0) {
printk(KERN_ERR "init_pmu: no response from PMU\n");
return 0;
}
udelay(10);
pmu_poll();
}
/* ack all pending interrupts */
timeout = 100000;
interrupt_data[0][0] = 1;
while (interrupt_data[0][0] || pmu_state != idle) {
if (--timeout < 0) {
printk(KERN_ERR "init_pmu: timed out acking intrs\n");
return 0;
}
if (pmu_state == idle)
adb_int_pending = 1;
via_pmu_interrupt(0, NULL);
udelay(10);
}
/* Tell PMU we are ready. */
if (pmu_kind == PMU_KEYLARGO_BASED) {
pmu_request(&req, NULL, 2, PMU_SYSTEM_READY, 2);
while (!req.complete)
pmu_poll();
}
/* Read PMU version */
pmu_request(&req, NULL, 1, PMU_GET_VERSION);
pmu_wait_complete(&req);
if (req.reply_len > 0)
pmu_version = req.reply[0];
/* Read server mode setting */
if (pmu_kind == PMU_KEYLARGO_BASED) {
pmu_request(&req, NULL, 2, PMU_POWER_EVENTS,
PMU_PWR_GET_POWERUP_EVENTS);
pmu_wait_complete(&req);
if (req.reply_len == 2) {
if (req.reply[1] & PMU_PWR_WAKEUP_AC_INSERT)
option_server_mode = 1;
printk(KERN_INFO "via-pmu: Server Mode is %s\n",
option_server_mode ? "enabled" : "disabled");
}
}
return 1;
}
int
pmu_get_model(void)
{
return pmu_kind;
}
static void pmu_set_server_mode(int server_mode)
{
struct adb_request req;
if (pmu_kind != PMU_KEYLARGO_BASED)
return;
option_server_mode = server_mode;
pmu_request(&req, NULL, 2, PMU_POWER_EVENTS, PMU_PWR_GET_POWERUP_EVENTS);
pmu_wait_complete(&req);
if (req.reply_len < 2)
return;
if (server_mode)
pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
PMU_PWR_SET_POWERUP_EVENTS,
req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
else
pmu_request(&req, NULL, 4, PMU_POWER_EVENTS,
PMU_PWR_CLR_POWERUP_EVENTS,
req.reply[0], PMU_PWR_WAKEUP_AC_INSERT);
pmu_wait_complete(&req);
}
/* This new version of the code for 2400/3400/3500 powerbooks
* is inspired from the implementation in gkrellm-pmu
*/
static void
done_battery_state_ohare(struct adb_request* req)
{
/* format:
* [0] : flags
* 0x01 : AC indicator
* 0x02 : charging
* 0x04 : battery exist
* 0x08 :
* 0x10 :
* 0x20 : full charged
* 0x40 : pcharge reset
* 0x80 : battery exist
*
* [1][2] : battery voltage
* [3] : CPU temperature
* [4] : battery temperature
* [5] : current
* [6][7] : pcharge
* --tkoba
*/
unsigned int bat_flags = PMU_BATT_TYPE_HOOPER;
long pcharge, charge, vb, vmax, lmax;
long vmax_charging, vmax_charged;
long amperage, voltage, time, max;
int mb = pmac_call_feature(PMAC_FTR_GET_MB_INFO,
NULL, PMAC_MB_INFO_MODEL, 0);
if (req->reply[0] & 0x01)
pmu_power_flags |= PMU_PWR_AC_PRESENT;
else
pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
if (mb == PMAC_TYPE_COMET) {
vmax_charged = 189;
vmax_charging = 213;
lmax = 6500;
} else {
vmax_charged = 330;
vmax_charging = 330;
lmax = 6500;
}
vmax = vmax_charged;
/* If battery installed */
if (req->reply[0] & 0x04) {
bat_flags |= PMU_BATT_PRESENT;
if (req->reply[0] & 0x02)
bat_flags |= PMU_BATT_CHARGING;
vb = (req->reply[1] << 8) | req->reply[2];
voltage = (vb * 265 + 72665) / 10;
amperage = req->reply[5];
if ((req->reply[0] & 0x01) == 0) {
if (amperage > 200)
vb += ((amperage - 200) * 15)/100;
} else if (req->reply[0] & 0x02) {
vb = (vb * 97) / 100;
vmax = vmax_charging;
}
charge = (100 * vb) / vmax;
if (req->reply[0] & 0x40) {
pcharge = (req->reply[6] << 8) + req->reply[7];
if (pcharge > lmax)
pcharge = lmax;
pcharge *= 100;
pcharge = 100 - pcharge / lmax;
if (pcharge < charge)
charge = pcharge;
}
if (amperage > 0)
time = (charge * 16440) / amperage;
else
time = 0;
max = 100;
amperage = -amperage;
} else
charge = max = amperage = voltage = time = 0;
pmu_batteries[pmu_cur_battery].flags = bat_flags;
pmu_batteries[pmu_cur_battery].charge = charge;
pmu_batteries[pmu_cur_battery].max_charge = max;
pmu_batteries[pmu_cur_battery].amperage = amperage;
pmu_batteries[pmu_cur_battery].voltage = voltage;
pmu_batteries[pmu_cur_battery].time_remaining = time;
clear_bit(0, &async_req_locks);
}
static void
done_battery_state_smart(struct adb_request* req)
{
/* format:
* [0] : format of this structure (known: 3,4,5)
* [1] : flags
*
* format 3 & 4:
*
* [2] : charge
* [3] : max charge
* [4] : current
* [5] : voltage
*
* format 5:
*
* [2][3] : charge
* [4][5] : max charge
* [6][7] : current
* [8][9] : voltage
*/
unsigned int bat_flags = PMU_BATT_TYPE_SMART;
int amperage;
unsigned int capa, max, voltage;
if (req->reply[1] & 0x01)
pmu_power_flags |= PMU_PWR_AC_PRESENT;
else
pmu_power_flags &= ~PMU_PWR_AC_PRESENT;
capa = max = amperage = voltage = 0;
if (req->reply[1] & 0x04) {
bat_flags |= PMU_BATT_PRESENT;
switch(req->reply[0]) {
case 3:
case 4: capa = req->reply[2];
max = req->reply[3];
amperage = *((signed char *)&req->reply[4]);
voltage = req->reply[5];
break;
case 5: capa = (req->reply[2] << 8) | req->reply[3];
max = (req->reply[4] << 8) | req->reply[5];
amperage = *((signed short *)&req->reply[6]);
voltage = (req->reply[8] << 8) | req->reply[9];
break;
default:
pr_warn("pmu.c: unrecognized battery info, "
"len: %d, %4ph\n", req->reply_len,
req->reply);
break;
}
}
if ((req->reply[1] & 0x01) && (amperage > 0))
bat_flags |= PMU_BATT_CHARGING;
pmu_batteries[pmu_cur_battery].flags = bat_flags;
pmu_batteries[pmu_cur_battery].charge = capa;
pmu_batteries[pmu_cur_battery].max_charge = max;
pmu_batteries[pmu_cur_battery].amperage = amperage;
pmu_batteries[pmu_cur_battery].voltage = voltage;
if (amperage) {
if ((req->reply[1] & 0x01) && (amperage > 0))
pmu_batteries[pmu_cur_battery].time_remaining
= ((max-capa) * 3600) / amperage;
else
pmu_batteries[pmu_cur_battery].time_remaining
= (capa * 3600) / (-amperage);
} else
pmu_batteries[pmu_cur_battery].time_remaining = 0;
pmu_cur_battery = (pmu_cur_battery + 1) % pmu_battery_count;
clear_bit(0, &async_req_locks);
}
static void
query_battery_state(void)
{
if (test_and_set_bit(0, &async_req_locks))
return;
if (pmu_kind == PMU_OHARE_BASED)
pmu_request(&batt_req, done_battery_state_ohare,
1, PMU_BATTERY_STATE);
else
pmu_request(&batt_req, done_battery_state_smart,
2, PMU_SMART_BATTERY_STATE, pmu_cur_battery+1);
}
static int pmu_info_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "PMU driver version : %d\n", PMU_DRIVER_VERSION);
seq_printf(m, "PMU firmware version : %02x\n", pmu_version);
seq_printf(m, "AC Power : %d\n",
((pmu_power_flags & PMU_PWR_AC_PRESENT) != 0) || pmu_battery_count == 0);
seq_printf(m, "Battery count : %d\n", pmu_battery_count);
return 0;
}
static int pmu_info_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, pmu_info_proc_show, NULL);
}
static const struct file_operations pmu_info_proc_fops = {
.owner = THIS_MODULE,
.open = pmu_info_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int pmu_irqstats_proc_show(struct seq_file *m, void *v)
{
int i;
static const char *irq_names[] = {
"Total CB1 triggered events",
"Total GPIO1 triggered events",
"PC-Card eject button",
"Sound/Brightness button",
"ADB message",
"Battery state change",
"Environment interrupt",
"Tick timer",
"Ghost interrupt (zero len)",
"Empty interrupt (empty mask)",
"Max irqs in a row"
};
for (i=0; i<11; i++) {
seq_printf(m, " %2u: %10u (%s)\n",
i, pmu_irq_stats[i], irq_names[i]);
}
return 0;
}
static int pmu_irqstats_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, pmu_irqstats_proc_show, NULL);
}
static const struct file_operations pmu_irqstats_proc_fops = {
.owner = THIS_MODULE,
.open = pmu_irqstats_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int pmu_battery_proc_show(struct seq_file *m, void *v)
{
long batnum = (long)m->private;
seq_putc(m, '\n');
seq_printf(m, "flags : %08x\n", pmu_batteries[batnum].flags);
seq_printf(m, "charge : %d\n", pmu_batteries[batnum].charge);
seq_printf(m, "max_charge : %d\n", pmu_batteries[batnum].max_charge);
seq_printf(m, "current : %d\n", pmu_batteries[batnum].amperage);
seq_printf(m, "voltage : %d\n", pmu_batteries[batnum].voltage);
seq_printf(m, "time rem. : %d\n", pmu_batteries[batnum].time_remaining);
return 0;
}
static int pmu_battery_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, pmu_battery_proc_show, PDE_DATA(inode));
}
static const struct file_operations pmu_battery_proc_fops = {
.owner = THIS_MODULE,
.open = pmu_battery_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int pmu_options_proc_show(struct seq_file *m, void *v)
{
#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
if (pmu_kind == PMU_KEYLARGO_BASED &&
pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
seq_printf(m, "lid_wakeup=%d\n", option_lid_wakeup);
#endif
if (pmu_kind == PMU_KEYLARGO_BASED)
seq_printf(m, "server_mode=%d\n", option_server_mode);
return 0;
}
static int pmu_options_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, pmu_options_proc_show, NULL);
}
static ssize_t pmu_options_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
char tmp[33];
char *label, *val;
size_t fcount = count;
if (!count)
return -EINVAL;
if (count > 32)
count = 32;
if (copy_from_user(tmp, buffer, count))
return -EFAULT;
tmp[count] = 0;
label = tmp;
while(*label == ' ')
label++;
val = label;
while(*val && (*val != '=')) {
if (*val == ' ')
*val = 0;
val++;
}
if ((*val) == 0)
return -EINVAL;
*(val++) = 0;
while(*val == ' ')
val++;
#if defined(CONFIG_SUSPEND) && defined(CONFIG_PPC32)
if (pmu_kind == PMU_KEYLARGO_BASED &&
pmac_call_feature(PMAC_FTR_SLEEP_STATE,NULL,0,-1) >= 0)
if (!strcmp(label, "lid_wakeup"))
option_lid_wakeup = ((*val) == '1');
#endif
if (pmu_kind == PMU_KEYLARGO_BASED && !strcmp(label, "server_mode")) {
int new_value;
new_value = ((*val) == '1');
if (new_value != option_server_mode)
pmu_set_server_mode(new_value);
}
return fcount;
}
static const struct file_operations pmu_options_proc_fops = {
.owner = THIS_MODULE,
.open = pmu_options_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
.write = pmu_options_proc_write,
};
#ifdef CONFIG_ADB
/* Send an ADB command */
static int pmu_send_request(struct adb_request *req, int sync)
{
int i, ret;
if ((vias == NULL) || (!pmu_fully_inited)) {
req->complete = 1;
return -ENXIO;
}
ret = -EINVAL;
switch (req->data[0]) {
case PMU_PACKET:
for (i = 0; i < req->nbytes - 1; ++i)
req->data[i] = req->data[i+1];
--req->nbytes;
if (pmu_data_len[req->data[0]][1] != 0) {
req->reply[0] = ADB_RET_OK;
req->reply_len = 1;
} else
req->reply_len = 0;
ret = pmu_queue_request(req);
break;
case CUDA_PACKET:
switch (req->data[1]) {
case CUDA_GET_TIME:
if (req->nbytes != 2)
break;
req->data[0] = PMU_READ_RTC;
req->nbytes = 1;
req->reply_len = 3;
req->reply[0] = CUDA_PACKET;
req->reply[1] = 0;
req->reply[2] = CUDA_GET_TIME;
ret = pmu_queue_request(req);
break;
case CUDA_SET_TIME:
if (req->nbytes != 6)
break;
req->data[0] = PMU_SET_RTC;
req->nbytes = 5;
for (i = 1; i <= 4; ++i)