1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/kthread.h>
25#include <linux/delay.h>
26#include <linux/ioport.h>
27#include <linux/miscdevice.h>
28#include <linux/kmod.h>
29#include <linux/reboot.h>
30
31#include <asm/ebus.h>
32#include <asm/uaccess.h>
33#include <asm/envctrl.h>
34#include <asm/io.h>
35
36#define ENVCTRL_MINOR 162
37
38#define PCF8584_ADDRESS 0x55
39
40#define CONTROL_PIN 0x80
41#define CONTROL_ES0 0x40
42#define CONTROL_ES1 0x20
43#define CONTROL_ES2 0x10
44#define CONTROL_ENI 0x08
45#define CONTROL_STA 0x04
46#define CONTROL_STO 0x02
47#define CONTROL_ACK 0x01
48
49#define STATUS_PIN 0x80
50#define STATUS_STS 0x20
51#define STATUS_BER 0x10
52#define STATUS_LRB 0x08
53#define STATUS_AD0 0x08
54#define STATUS_AAB 0x04
55#define STATUS_LAB 0x02
56#define STATUS_BB 0x01
57
58
59
60
61#define BUS_CLK_90 0x00
62#define BUS_CLK_45 0x01
63#define BUS_CLK_11 0x02
64#define BUS_CLK_1_5 0x03
65
66#define CLK_3 0x00
67#define CLK_4_43 0x10
68#define CLK_6 0x14
69#define CLK_8 0x18
70#define CLK_12 0x1c
71
72#define OBD_SEND_START 0xc5
73#define OBD_SEND_STOP 0xc3
74
75
76
77
78#define PCF8584_MAX_CHANNELS 8
79#define PCF8584_GLOBALADDR_TYPE 6
80#define PCF8584_FANSTAT_TYPE 3
81#define PCF8584_VOLTAGE_TYPE 2
82#define PCF8584_TEMP_TYPE 1
83
84
85
86
87#define ENVCTRL_NOMON 0
88#define ENVCTRL_CPUTEMP_MON 1
89#define ENVCTRL_CPUVOLTAGE_MON 2
90#define ENVCTRL_FANSTAT_MON 3
91#define ENVCTRL_ETHERTEMP_MON 4
92
93#define ENVCTRL_VOLTAGESTAT_MON 5
94#define ENVCTRL_MTHRBDTEMP_MON 6
95#define ENVCTRL_SCSITEMP_MON 7
96#define ENVCTRL_GLOBALADDR_MON 8
97
98
99
100
101#define I2C_ADC 0
102#define I2C_GPIO 1
103
104
105
106
107
108#define ENVCTRL_TRANSLATE_NO 0
109#define ENVCTRL_TRANSLATE_PARTIAL 1
110#define ENVCTRL_TRANSLATE_COMBINED 2
111#define ENVCTRL_TRANSLATE_FULL 3
112#define ENVCTRL_TRANSLATE_SCALE 4
113
114
115#define ENVCTRL_MAX_CPU 4
116#define CHANNEL_DESC_SZ 256
117
118
119#define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
120#define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
121
122
123
124
125#define ENVCTRL_CPCI_IGNORED_NODE 0x70
126
127#define PCF8584_DATA 0x00
128#define PCF8584_CSR 0x01
129
130
131
132
133struct pcf8584_channel {
134 unsigned char chnl_no;
135 unsigned char io_direction;
136 unsigned char type;
137 unsigned char last;
138};
139
140
141
142
143struct pcf8584_tblprop {
144 unsigned int type;
145 unsigned int scale;
146 unsigned int offset;
147 unsigned int size;
148};
149
150
151struct i2c_child_t {
152
153 unsigned char i2ctype;
154 unsigned long addr;
155 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
156
157
158 unsigned int total_chnls;
159 unsigned char fan_mask;
160 unsigned char voltage_mask;
161 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
162
163
164 unsigned int total_tbls;
165 char *tables;
166 char chnls_desc[CHANNEL_DESC_SZ];
167 char mon_type[PCF8584_MAX_CHANNELS];
168};
169
170static void __iomem *i2c;
171static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
172static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
173static unsigned int warning_temperature = 0;
174static unsigned int shutdown_temperature = 0;
175static char read_cpu;
176
177
178static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
179
180
181
182
183
184static void envtrl_i2c_test_pin(void)
185{
186 int limit = 1000000;
187
188 while (--limit > 0) {
189 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
190 break;
191 udelay(1);
192 }
193
194 if (limit <= 0)
195 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
196}
197
198
199
200
201static void envctrl_i2c_test_bb(void)
202{
203 int limit = 1000000;
204
205 while (--limit > 0) {
206
207 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
208 break;
209 udelay(1);
210 }
211
212 if (limit <= 0)
213 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
214}
215
216
217
218
219static int envctrl_i2c_read_addr(unsigned char addr)
220{
221 envctrl_i2c_test_bb();
222
223
224 writeb(addr + 1, i2c + PCF8584_DATA);
225
226 envctrl_i2c_test_bb();
227
228 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
229
230
231 envtrl_i2c_test_pin();
232
233
234 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
235 return readb(i2c + PCF8584_DATA);
236 } else {
237 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
238 return 0;
239 }
240}
241
242
243
244
245static void envctrl_i2c_write_addr(unsigned char addr)
246{
247 envctrl_i2c_test_bb();
248 writeb(addr, i2c + PCF8584_DATA);
249
250
251 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
252}
253
254
255
256
257
258static unsigned char envctrl_i2c_read_data(void)
259{
260 envtrl_i2c_test_pin();
261 writeb(CONTROL_ES0, i2c + PCF8584_CSR);
262 return readb(i2c + PCF8584_DATA);
263}
264
265
266
267
268static void envctrl_i2c_write_data(unsigned char port)
269{
270 envtrl_i2c_test_pin();
271 writeb(port, i2c + PCF8584_DATA);
272}
273
274
275
276
277static void envctrl_i2c_stop(void)
278{
279 envtrl_i2c_test_pin();
280 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
281}
282
283
284
285
286static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
287{
288
289 envctrl_i2c_write_addr(addr);
290
291
292 envctrl_i2c_write_data(port);
293 envctrl_i2c_stop();
294
295
296 envctrl_i2c_read_addr(addr);
297
298
299 envctrl_i2c_read_data();
300 envctrl_i2c_stop();
301
302 return readb(i2c + PCF8584_DATA);
303}
304
305
306
307
308static unsigned char envctrl_i2c_read_8574(unsigned char addr)
309{
310 unsigned char rd;
311
312 envctrl_i2c_read_addr(addr);
313
314
315 rd = envctrl_i2c_read_data();
316 envctrl_i2c_stop();
317 return rd;
318}
319
320
321
322
323
324static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
325 int scale, char *tbl, char *bufdata)
326{
327 int len = 0;
328
329 switch (translate_type) {
330 case ENVCTRL_TRANSLATE_NO:
331
332 len = 1;
333 bufdata[0] = data;
334 break;
335
336 case ENVCTRL_TRANSLATE_FULL:
337
338 len = 1;
339 bufdata[0] = tbl[data];
340 break;
341
342 case ENVCTRL_TRANSLATE_SCALE:
343
344 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
345 len = strlen(bufdata);
346 bufdata[len - 1] = bufdata[len - 2];
347 bufdata[len - 2] = '.';
348 break;
349
350 default:
351 break;
352 };
353
354 return len;
355}
356
357
358
359
360static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
361 char mon_type, unsigned char *bufdata)
362{
363 unsigned char data;
364 int i;
365 char *tbl, j = -1;
366
367
368 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
369 if (pchild->mon_type[i] == mon_type) {
370 if (++j == cpu) {
371 break;
372 }
373 }
374 }
375
376 if (j != cpu)
377 return 0;
378
379
380 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
381 (unsigned char)pchild->chnl_array[i].chnl_no);
382
383
384 tbl = pchild->tables + pchild->tblprop_array[i].offset;
385
386 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
387 pchild->tblprop_array[i].scale,
388 tbl, bufdata);
389}
390
391
392
393
394
395static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
396 char mon_type, unsigned char *bufdata)
397{
398 unsigned char data;
399 int i;
400 char *tbl = NULL;
401
402 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
403 if (pchild->mon_type[i] == mon_type)
404 break;
405 }
406
407 if (i >= PCF8584_MAX_CHANNELS)
408 return 0;
409
410
411 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
412 (unsigned char)pchild->chnl_array[i].chnl_no);
413
414
415 tbl = pchild->tables + pchild->tblprop_array[i].offset;
416
417 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
418 pchild->tblprop_array[i].scale,
419 tbl, bufdata);
420}
421
422
423
424
425static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
426 unsigned char data,
427 char *bufdata)
428{
429 unsigned char tmp, ret = 0;
430 int i, j = 0;
431
432 tmp = data & pchild->fan_mask;
433
434 if (tmp == pchild->fan_mask) {
435
436 ret = ENVCTRL_ALL_FANS_GOOD;
437 } else if (tmp == 0) {
438
439 ret = ENVCTRL_ALL_FANS_BAD;
440 } else {
441
442
443
444
445
446
447 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
448 if (pchild->fan_mask & chnls_mask[i]) {
449 if (!(chnls_mask[i] & tmp))
450 ret |= chnls_mask[j];
451
452 j++;
453 }
454 }
455 }
456
457 bufdata[0] = ret;
458 return 1;
459}
460
461
462
463
464static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
465 unsigned char data,
466 char *bufdata)
467{
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
483 return 1;
484}
485
486
487
488
489static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
490 unsigned char data,
491 char *bufdata)
492{
493 unsigned char tmp, ret = 0;
494 int i, j = 0;
495
496 tmp = data & pchild->voltage_mask;
497
498
499 if (tmp == pchild->voltage_mask) {
500
501 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
502 } else if (tmp == 0) {
503
504 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
505 } else {
506
507 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
508 if (pchild->voltage_mask & chnls_mask[i]) {
509 j++;
510
511
512 if (!(chnls_mask[i] & tmp))
513 break;
514 }
515 }
516
517
518
519
520
521 if (j == 1)
522 ret = ENVCTRL_VOLTAGE_BAD;
523 else
524 ret = ENVCTRL_POWERSUPPLY_BAD;
525 }
526
527 bufdata[0] = ret;
528 return 1;
529}
530
531
532
533
534static ssize_t
535envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
536{
537 struct i2c_child_t *pchild;
538 unsigned char data[10];
539 int ret = 0;
540
541
542
543
544
545
546 switch ((int)(long)file->private_data) {
547 case ENVCTRL_RD_WARNING_TEMPERATURE:
548 if (warning_temperature == 0)
549 return 0;
550
551 data[0] = (unsigned char)(warning_temperature);
552 ret = 1;
553 if (copy_to_user(buf, data, ret))
554 ret = -EFAULT;
555 break;
556
557 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
558 if (shutdown_temperature == 0)
559 return 0;
560
561 data[0] = (unsigned char)(shutdown_temperature);
562 ret = 1;
563 if (copy_to_user(buf, data, ret))
564 ret = -EFAULT;
565 break;
566
567 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
568 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
569 return 0;
570 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
571 if (copy_to_user(buf, data, ret))
572 ret = -EFAULT;
573 break;
574
575 case ENVCTRL_RD_CPU_TEMPERATURE:
576 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
577 return 0;
578 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
579
580
581 if (copy_to_user(buf, data, ret))
582 ret = -EFAULT;
583 break;
584
585 case ENVCTRL_RD_CPU_VOLTAGE:
586 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
587 return 0;
588 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
589
590
591 if (copy_to_user(buf, data, ret))
592 ret = -EFAULT;
593 break;
594
595 case ENVCTRL_RD_SCSI_TEMPERATURE:
596 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
597 return 0;
598 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
599 if (copy_to_user(buf, data, ret))
600 ret = -EFAULT;
601 break;
602
603 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
604 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
605 return 0;
606 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
607 if (copy_to_user(buf, data, ret))
608 ret = -EFAULT;
609 break;
610
611 case ENVCTRL_RD_FAN_STATUS:
612 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
613 return 0;
614 data[0] = envctrl_i2c_read_8574(pchild->addr);
615 ret = envctrl_i2c_fan_status(pchild,data[0], data);
616 if (copy_to_user(buf, data, ret))
617 ret = -EFAULT;
618 break;
619
620 case ENVCTRL_RD_GLOBALADDRESS:
621 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
622 return 0;
623 data[0] = envctrl_i2c_read_8574(pchild->addr);
624 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
625 if (copy_to_user(buf, data, ret))
626 ret = -EFAULT;
627 break;
628
629 case ENVCTRL_RD_VOLTAGE_STATUS:
630 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
631
632 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
633 return 0;
634 data[0] = envctrl_i2c_read_8574(pchild->addr);
635 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
636 if (copy_to_user(buf, data, ret))
637 ret = -EFAULT;
638 break;
639
640 default:
641 break;
642
643 };
644
645 return ret;
646}
647
648
649
650
651static long
652envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
653{
654 char __user *infobuf;
655
656 switch (cmd) {
657 case ENVCTRL_RD_WARNING_TEMPERATURE:
658 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
659 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
660 case ENVCTRL_RD_FAN_STATUS:
661 case ENVCTRL_RD_VOLTAGE_STATUS:
662 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
663 case ENVCTRL_RD_SCSI_TEMPERATURE:
664 case ENVCTRL_RD_GLOBALADDRESS:
665 file->private_data = (void *)(long)cmd;
666 break;
667
668 case ENVCTRL_RD_CPU_TEMPERATURE:
669 case ENVCTRL_RD_CPU_VOLTAGE:
670
671
672
673 infobuf = (char __user *) arg;
674 if (infobuf == NULL) {
675 read_cpu = 0;
676 }else {
677 get_user(read_cpu, infobuf);
678 }
679
680
681 file->private_data = (void *)(long)cmd;
682 break;
683
684 default:
685 return -EINVAL;
686 };
687
688 return 0;
689}
690
691
692
693
694static int
695envctrl_open(struct inode *inode, struct file *file)
696{
697 file->private_data = NULL;
698 return 0;
699}
700
701
702
703
704static int
705envctrl_release(struct inode *inode, struct file *file)
706{
707 return 0;
708}
709
710static const struct file_operations envctrl_fops = {
711 .owner = THIS_MODULE,
712 .read = envctrl_read,
713 .unlocked_ioctl = envctrl_ioctl,
714#ifdef CONFIG_COMPAT
715 .compat_ioctl = envctrl_ioctl,
716#endif
717 .open = envctrl_open,
718 .release = envctrl_release,
719};
720
721static struct miscdevice envctrl_dev = {
722 ENVCTRL_MINOR,
723 "envctrl",
724 &envctrl_fops
725};
726
727
728
729
730static void envctrl_set_mon(struct i2c_child_t *pchild,
731 const char *chnl_desc,
732 int chnl_no)
733{
734
735
736
737
738 if (!(strcmp(chnl_desc,"temp,cpu")) ||
739 !(strcmp(chnl_desc,"temp,cpu0")) ||
740 !(strcmp(chnl_desc,"temp,cpu1")) ||
741 !(strcmp(chnl_desc,"temp,cpu2")) ||
742 !(strcmp(chnl_desc,"temp,cpu3")))
743 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
744
745 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
746 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
747 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
748 !(strcmp(chnl_desc,"vddcore,cpu3")))
749 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
750
751 if (!(strcmp(chnl_desc,"temp,motherboard")))
752 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
753
754 if (!(strcmp(chnl_desc,"temp,scsi")))
755 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
756
757 if (!(strcmp(chnl_desc,"temp,ethernet")))
758 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
759}
760
761
762
763
764
765static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
766{
767 int i = 0, len;
768 const char *pos;
769 const unsigned int *pval;
770
771
772 pos = of_get_property(dp, "channels-description", &len);
773
774 while (len > 0) {
775 int l = strlen(pos) + 1;
776 envctrl_set_mon(pchild, pos, i++);
777 len -= l;
778 pos += l;
779 }
780
781
782 pval = of_get_property(dp, "warning-temp", NULL);
783 if (pval)
784 warning_temperature = *pval;
785
786 pval = of_get_property(dp, "shutdown-temp", NULL);
787 if (pval)
788 shutdown_temperature = *pval;
789}
790
791
792
793
794static void envctrl_init_fanstat(struct i2c_child_t *pchild)
795{
796 int i;
797
798
799 for (i = 0; i < pchild->total_chnls; i++)
800 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
801
802
803
804
805 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
806}
807
808
809
810
811static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
812{
813 int i;
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828 for (i = 0; i < pchild->total_chnls; i++) {
829 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
830 pchild->voltage_mask |= chnls_mask[i];
831 }
832 }
833
834
835
836
837
838 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
839}
840
841
842static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
843{
844 int i;
845
846
847 for (i = 0; i < pchild->total_chnls; i++)
848 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
849
850
851
852
853 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
854}
855
856
857
858
859static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
860 struct i2c_child_t *pchild)
861{
862 int len, i, tbls_size = 0;
863 struct device_node *dp = edev_child->prom_node;
864 const void *pval;
865
866
867 pval = of_get_property(dp, "reg", &len);
868 memcpy(&pchild->addr, pval, len);
869
870
871 pval = of_get_property(dp, "translation", &len);
872 if (pval && len > 0) {
873 memcpy(pchild->tblprop_array, pval, len);
874 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
875 for (i = 0; i < pchild->total_tbls; i++) {
876 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
877 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
878 }
879 }
880
881 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
882 if (pchild->tables == NULL){
883 printk("envctrl: Failed to allocate table.\n");
884 return;
885 }
886 pval = of_get_property(dp, "tables", &len);
887 if (!pval || len <= 0) {
888 printk("envctrl: Failed to get table.\n");
889 return;
890 }
891 memcpy(pchild->tables, pval, len);
892 }
893
894
895
896
897
898
899
900
901 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
902 struct device_node *root_node;
903 int len;
904
905 root_node = of_find_node_by_path("/");
906 if (!strcmp(root_node->name, "SUNW,UltraSPARC-IIi-cEngine")) {
907 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
908 pchild->mon_type[len] = ENVCTRL_NOMON;
909 }
910 return;
911 }
912 }
913
914
915 pval = of_get_property(dp, "channels-in-use", &len);
916 memcpy(pchild->chnl_array, pval, len);
917 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
918
919 for (i = 0; i < pchild->total_chnls; i++) {
920 switch (pchild->chnl_array[i].type) {
921 case PCF8584_TEMP_TYPE:
922 envctrl_init_adc(pchild, dp);
923 break;
924
925 case PCF8584_GLOBALADDR_TYPE:
926 envctrl_init_globaladdr(pchild);
927 i = pchild->total_chnls;
928 break;
929
930 case PCF8584_FANSTAT_TYPE:
931 envctrl_init_fanstat(pchild);
932 i = pchild->total_chnls;
933 break;
934
935 case PCF8584_VOLTAGE_TYPE:
936 if (pchild->i2ctype == I2C_ADC) {
937 envctrl_init_adc(pchild,dp);
938 } else {
939 envctrl_init_voltage_status(pchild);
940 }
941 i = pchild->total_chnls;
942 break;
943
944 default:
945 break;
946 };
947 }
948}
949
950
951
952
953static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
954{
955 int i, j;
956
957 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
958 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
959 if (i2c_childlist[i].mon_type[j] == mon_type) {
960 return (struct i2c_child_t *)(&(i2c_childlist[i]));
961 }
962 }
963 }
964 return NULL;
965}
966
967static void envctrl_do_shutdown(void)
968{
969 static int inprog = 0;
970 int ret;
971
972 if (inprog != 0)
973 return;
974
975 inprog = 1;
976 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
977 ret = orderly_poweroff(true);
978 if (ret < 0) {
979 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
980 inprog = 0;
981 }
982}
983
984static struct task_struct *kenvctrld_task;
985
986static int kenvctrld(void *__unused)
987{
988 int poll_interval;
989 int whichcpu;
990 char tempbuf[10];
991 struct i2c_child_t *cputemp;
992
993 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
994 printk(KERN_ERR
995 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
996 return -ENODEV;
997 }
998
999 poll_interval = 5000;
1000
1001 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1002 for (;;) {
1003 msleep_interruptible(poll_interval);
1004
1005 if (kthread_should_stop())
1006 break;
1007
1008 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1009 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1010 ENVCTRL_CPUTEMP_MON,
1011 tempbuf)) {
1012 if (tempbuf[0] >= shutdown_temperature) {
1013 printk(KERN_CRIT
1014 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1015 "shutdown threshold %i C\n",
1016 current->comm, whichcpu,
1017 tempbuf[0], shutdown_temperature);
1018 envctrl_do_shutdown();
1019 }
1020 }
1021 }
1022 }
1023 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1024 return 0;
1025}
1026
1027static int __init envctrl_init(void)
1028{
1029 struct linux_ebus *ebus = NULL;
1030 struct linux_ebus_device *edev = NULL;
1031 struct linux_ebus_child *edev_child = NULL;
1032 int err, i = 0;
1033
1034 for_each_ebus(ebus) {
1035 for_each_ebusdev(edev, ebus) {
1036 if (!strcmp(edev->prom_node->name, "bbc")) {
1037
1038
1039
1040 return -ENODEV;
1041 }
1042 }
1043 }
1044
1045
1046
1047
1048 for_each_ebus(ebus) {
1049 for_each_ebusdev(edev, ebus) {
1050 if (!strcmp(edev->prom_node->name, "i2c")) {
1051 i2c = ioremap(edev->resource[0].start, 0x2);
1052 for_each_edevchild(edev, edev_child) {
1053 if (!strcmp("gpio", edev_child->prom_node->name)) {
1054 i2c_childlist[i].i2ctype = I2C_GPIO;
1055 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1056 }
1057 if (!strcmp("adc", edev_child->prom_node->name)) {
1058 i2c_childlist[i].i2ctype = I2C_ADC;
1059 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1060 }
1061 }
1062 goto done;
1063 }
1064 }
1065 }
1066
1067done:
1068 if (!edev) {
1069 printk("envctrl: I2C device not found.\n");
1070 return -ENODEV;
1071 }
1072
1073
1074 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1075 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1076
1077
1078 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1079 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1080
1081
1082 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1083 udelay(200);
1084
1085
1086 err = misc_register(&envctrl_dev);
1087 if (err) {
1088 printk("envctrl: Unable to get misc minor %d\n",
1089 envctrl_dev.minor);
1090 goto out_iounmap;
1091 }
1092
1093
1094
1095
1096
1097 printk("envctrl: initialized ");
1098 for (--i; i >= 0; --i) {
1099 printk("[%s 0x%lx]%s",
1100 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1101 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1102 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1103 }
1104
1105 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1106 if (IS_ERR(kenvctrld_task)) {
1107 err = PTR_ERR(kenvctrld_task);
1108 goto out_deregister;
1109 }
1110
1111 return 0;
1112
1113out_deregister:
1114 misc_deregister(&envctrl_dev);
1115out_iounmap:
1116 iounmap(i2c);
1117 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1118 kfree(i2c_childlist[i].tables);
1119
1120 return err;
1121}
1122
1123static void __exit envctrl_cleanup(void)
1124{
1125 int i;
1126
1127 kthread_stop(kenvctrld_task);
1128
1129 iounmap(i2c);
1130 misc_deregister(&envctrl_dev);
1131
1132 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1133 kfree(i2c_childlist[i].tables);
1134}
1135
1136module_init(envctrl_init);
1137module_exit(envctrl_cleanup);
1138MODULE_LICENSE("GPL");
1139