1 /* $Id: envctrl.c,v 1.25 2002/01/15 09:01:26 davem Exp $
2 * envctrl.c: Temperature and Fan monitoring on Machines providing it.
4 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 2000 Vinh Truong (vinh.truong@eng.sun.com)
6 * VT - The implementation is to support Sun Microelectronics (SME) platform
7 * environment monitoring. SME platforms use pcf8584 as the i2c bus
8 * controller to access pcf8591 (8-bit A/D and D/A converter) and
9 * pcf8571 (256 x 8-bit static low-voltage RAM with I2C-bus interface).
10 * At board level, it follows SME Firmware I2C Specification. Reference:
11 * http://www-eu2.semiconductors.com/pip/PCF8584P
12 * http://www-eu2.semiconductors.com/pip/PCF8574AP
13 * http://www-eu2.semiconductors.com/pip/PCF8591P
15 * EB - Added support for CP1500 Global Address and PS/Voltage monitoring.
16 * Eric Brower <ebrower@usa.net>
18 * DB - Audit every copy_to_user in envctrl_read.
19 * Daniele Bellucci <bellucda@tiscali.it>
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 #include <linux/smp_lock.h>
33 #include <asm/uaccess.h>
34 #include <asm/envctrl.h>
37 #define ENVCTRL_MINOR 162
39 #define PCF8584_ADDRESS 0x55
41 #define CONTROL_PIN 0x80
42 #define CONTROL_ES0 0x40
43 #define CONTROL_ES1 0x20
44 #define CONTROL_ES2 0x10
45 #define CONTROL_ENI 0x08
46 #define CONTROL_STA 0x04
47 #define CONTROL_STO 0x02
48 #define CONTROL_ACK 0x01
50 #define STATUS_PIN 0x80
51 #define STATUS_STS 0x20
52 #define STATUS_BER 0x10
53 #define STATUS_LRB 0x08
54 #define STATUS_AD0 0x08
55 #define STATUS_AAB 0x04
56 #define STATUS_LAB 0x02
57 #define STATUS_BB 0x01
62 #define BUS_CLK_90 0x00
63 #define BUS_CLK_45 0x01
64 #define BUS_CLK_11 0x02
65 #define BUS_CLK_1_5 0x03
73 #define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
74 #define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
76 /* Monitor type of i2c child device.
77 * Firmware definitions.
79 #define PCF8584_MAX_CHANNELS 8
80 #define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
81 #define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
82 #define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
83 #define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
85 /* Monitor type of i2c child device.
88 #define ENVCTRL_NOMON 0
89 #define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
90 #define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
91 #define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
92 #define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperarture */
94 #define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
95 #define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
96 #define ENVCTRL_SCSITEMP_MON 7 /* scsi temperarture */
97 #define ENVCTRL_GLOBALADDR_MON 8 /* global address */
100 * Driver definitions.
102 #define I2C_ADC 0 /* pcf8591 */
103 #define I2C_GPIO 1 /* pcf8571 */
105 /* Data read from child device may need to decode
106 * through a data table and a scale.
107 * Translation type as defined by firmware.
109 #define ENVCTRL_TRANSLATE_NO 0
110 #define ENVCTRL_TRANSLATE_PARTIAL 1
111 #define ENVCTRL_TRANSLATE_COMBINED 2
112 #define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
113 #define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
115 /* Driver miscellaneous definitions. */
116 #define ENVCTRL_MAX_CPU 4
117 #define CHANNEL_DESC_SZ 256
119 /* Mask values for combined GlobalAddress/PowerStatus node */
120 #define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
121 #define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
123 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
124 * (see envctrl_init_i2c_child)
126 #define ENVCTRL_CPCI_IGNORED_NODE 0x70
128 #define PCF8584_DATA 0x00
129 #define PCF8584_CSR 0x01
131 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
132 * Property of a port or channel as defined by the firmware.
134 struct pcf8584_channel {
135 unsigned char chnl_no;
136 unsigned char io_direction;
141 /* Each child device may have one or more tables of bytes to help decode
142 * data. Table property as defined by the firmware.
144 struct pcf8584_tblprop {
147 unsigned int offset; /* offset from the beginning of the table */
153 /* Either ADC or GPIO. */
154 unsigned char i2ctype;
156 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
159 unsigned int total_chnls; /* Number of monitor channels. */
160 unsigned char fan_mask; /* Byte mask for fan status channels. */
161 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
162 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
164 /* Properties of all monitor channels. */
165 unsigned int total_tbls; /* Number of monitor tables. */
166 char *tables; /* Pointer to table(s). */
167 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
168 char mon_type[PCF8584_MAX_CHANNELS];
171 static void __iomem *i2c;
172 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
173 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
174 static unsigned int warning_temperature = 0;
175 static unsigned int shutdown_temperature = 0;
176 static char read_cpu;
178 /* Forward declarations. */
179 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
181 /* Function Description: Test the PIN bit (Pending Interrupt Not)
182 * to test when serial transmission is completed .
185 static void envtrl_i2c_test_pin(void)
189 while (--limit > 0) {
190 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
196 printk(KERN_INFO "envctrl: Pin status will not clear.\n");
199 /* Function Description: Test busy bit.
202 static void envctrl_i2c_test_bb(void)
206 while (--limit > 0) {
207 /* Busy bit 0 means busy. */
208 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
214 printk(KERN_INFO "envctrl: Busy bit will not clear.\n");
217 /* Function Description: Send the address for a read access.
218 * Return : 0 if not acknowledged, otherwise acknowledged.
220 static int envctrl_i2c_read_addr(unsigned char addr)
222 envctrl_i2c_test_bb();
225 writeb(addr + 1, i2c + PCF8584_DATA);
227 envctrl_i2c_test_bb();
229 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
232 envtrl_i2c_test_pin();
234 /* CSR 0 means acknowledged. */
235 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
236 return readb(i2c + PCF8584_DATA);
238 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
243 /* Function Description: Send the address for write mode.
246 static void envctrl_i2c_write_addr(unsigned char addr)
248 envctrl_i2c_test_bb();
249 writeb(addr, i2c + PCF8584_DATA);
251 /* Generate Start condition. */
252 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
255 /* Function Description: Read 1 byte of data from addr
256 * set by envctrl_i2c_read_addr()
257 * Return : Data from address set by envctrl_i2c_read_addr().
259 static unsigned char envctrl_i2c_read_data(void)
261 envtrl_i2c_test_pin();
262 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
263 return readb(i2c + PCF8584_DATA);
266 /* Function Description: Instruct the device which port to read data from.
269 static void envctrl_i2c_write_data(unsigned char port)
271 envtrl_i2c_test_pin();
272 writeb(port, i2c + PCF8584_DATA);
275 /* Function Description: Generate Stop condition after last byte is sent.
278 static void envctrl_i2c_stop(void)
280 envtrl_i2c_test_pin();
281 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
284 /* Function Description: Read adc device.
285 * Return : Data at address and port.
287 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
290 envctrl_i2c_write_addr(addr);
292 /* Setup port to read. */
293 envctrl_i2c_write_data(port);
297 envctrl_i2c_read_addr(addr);
299 /* Do a single byte read and send stop. */
300 envctrl_i2c_read_data();
303 return readb(i2c + PCF8584_DATA);
306 /* Function Description: Read gpio device.
307 * Return : Data at address.
309 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
313 envctrl_i2c_read_addr(addr);
315 /* Do a single byte read and send stop. */
316 rd = envctrl_i2c_read_data();
321 /* Function Description: Decode data read from an adc device using firmware
323 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
325 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
326 int scale, char *tbl, char *bufdata)
330 switch (translate_type) {
331 case ENVCTRL_TRANSLATE_NO:
332 /* No decode necessary. */
337 case ENVCTRL_TRANSLATE_FULL:
338 /* Decode this way: data = table[data]. */
340 bufdata[0] = tbl[data];
343 case ENVCTRL_TRANSLATE_SCALE:
344 /* Decode this way: data = table[data]/scale */
345 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
346 len = strlen(bufdata);
347 bufdata[len - 1] = bufdata[len - 2];
348 bufdata[len - 2] = '.';
358 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
359 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
361 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
362 char mon_type, unsigned char *bufdata)
368 /* Find the right monitor type and channel. */
369 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
370 if (pchild->mon_type[i] == mon_type) {
380 /* Read data from address and port. */
381 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
382 (unsigned char)pchild->chnl_array[i].chnl_no);
384 /* Find decoding table. */
385 tbl = pchild->tables + pchild->tblprop_array[i].offset;
387 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
388 pchild->tblprop_array[i].scale,
392 /* Function Description: Read noncpu-related data such as motherboard
394 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
396 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
397 char mon_type, unsigned char *bufdata)
403 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
404 if (pchild->mon_type[i] == mon_type)
408 if (i >= PCF8584_MAX_CHANNELS)
411 /* Read data from address and port. */
412 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
413 (unsigned char)pchild->chnl_array[i].chnl_no);
415 /* Find decoding table. */
416 tbl = pchild->tables + pchild->tblprop_array[i].offset;
418 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
419 pchild->tblprop_array[i].scale,
423 /* Function Description: Read fan status.
424 * Return : Always 1 byte. Status stored in bufdata.
426 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
430 unsigned char tmp, ret = 0;
433 tmp = data & pchild->fan_mask;
435 if (tmp == pchild->fan_mask) {
436 /* All bits are on. All fans are functioning. */
437 ret = ENVCTRL_ALL_FANS_GOOD;
438 } else if (tmp == 0) {
439 /* No bits are on. No fans are functioning. */
440 ret = ENVCTRL_ALL_FANS_BAD;
442 /* Go through all channels, mark 'on' the matched bits.
443 * Notice that fan_mask may have discontiguous bits but
444 * return mask are always contiguous. For example if we
445 * monitor 4 fans at channels 0,1,2,4, the return mask
446 * should be 00010000 if only fan at channel 4 is working.
448 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
449 if (pchild->fan_mask & chnls_mask[i]) {
450 if (!(chnls_mask[i] & tmp))
451 ret |= chnls_mask[j];
462 /* Function Description: Read global addressing line.
463 * Return : Always 1 byte. Status stored in bufdata.
465 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
469 /* Translatation table is not necessary, as global
470 * addr is the integer value of the GA# bits.
472 * NOTE: MSB is documented as zero, but I see it as '1' always....
474 * -----------------------------------------------
475 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
476 * -----------------------------------------------
477 * GA0 - GA4 integer value of Global Address (backplane slot#)
478 * DEG 0 = cPCI Power supply output is starting to degrade
479 * 1 = cPCI Power supply output is OK
480 * FAL 0 = cPCI Power supply has failed
481 * 1 = cPCI Power supply output is OK
483 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
487 /* Function Description: Read standard voltage and power supply status.
488 * Return : Always 1 byte. Status stored in bufdata.
490 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
494 unsigned char tmp, ret = 0;
497 tmp = data & pchild->voltage_mask;
499 /* Two channels are used to monitor voltage and power supply. */
500 if (tmp == pchild->voltage_mask) {
501 /* All bits are on. Voltage and power supply are okay. */
502 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
503 } else if (tmp == 0) {
504 /* All bits are off. Voltage and power supply are bad */
505 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
507 /* Either voltage or power supply has problem. */
508 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
509 if (pchild->voltage_mask & chnls_mask[i]) {
512 /* Break out when there is a mismatch. */
513 if (!(chnls_mask[i] & tmp))
518 /* Make a wish that hardware will always use the
519 * first channel for voltage and the second for
523 ret = ENVCTRL_VOLTAGE_BAD;
525 ret = ENVCTRL_POWERSUPPLY_BAD;
532 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
533 * Return: Number of read bytes. 0 for error.
536 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
538 struct i2c_child_t *pchild;
539 unsigned char data[10];
542 /* Get the type of read as decided in ioctl() call.
543 * Find the appropriate i2c child.
544 * Get the data and put back to the user buffer.
547 switch ((int)(long)file->private_data) {
548 case ENVCTRL_RD_WARNING_TEMPERATURE:
549 if (warning_temperature == 0)
552 data[0] = (unsigned char)(warning_temperature);
554 if (copy_to_user(buf, data, ret))
558 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
559 if (shutdown_temperature == 0)
562 data[0] = (unsigned char)(shutdown_temperature);
564 if (copy_to_user(buf, data, ret))
568 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
569 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
571 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
572 if (copy_to_user(buf, data, ret))
576 case ENVCTRL_RD_CPU_TEMPERATURE:
577 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
579 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
581 /* Reset cpu to the default cpu0. */
582 if (copy_to_user(buf, data, ret))
586 case ENVCTRL_RD_CPU_VOLTAGE:
587 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
589 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
591 /* Reset cpu to the default cpu0. */
592 if (copy_to_user(buf, data, ret))
596 case ENVCTRL_RD_SCSI_TEMPERATURE:
597 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
599 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
600 if (copy_to_user(buf, data, ret))
604 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
605 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
607 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
608 if (copy_to_user(buf, data, ret))
612 case ENVCTRL_RD_FAN_STATUS:
613 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
615 data[0] = envctrl_i2c_read_8574(pchild->addr);
616 ret = envctrl_i2c_fan_status(pchild,data[0], data);
617 if (copy_to_user(buf, data, ret))
621 case ENVCTRL_RD_GLOBALADDRESS:
622 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
624 data[0] = envctrl_i2c_read_8574(pchild->addr);
625 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
626 if (copy_to_user(buf, data, ret))
630 case ENVCTRL_RD_VOLTAGE_STATUS:
631 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
632 /* If voltage monitor not present, check for CPCI equivalent */
633 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
635 data[0] = envctrl_i2c_read_8574(pchild->addr);
636 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
637 if (copy_to_user(buf, data, ret))
649 /* Function Description: Command what to read. Mapped to user ioctl().
650 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
653 envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
655 char __user *infobuf;
658 case ENVCTRL_RD_WARNING_TEMPERATURE:
659 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
660 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
661 case ENVCTRL_RD_FAN_STATUS:
662 case ENVCTRL_RD_VOLTAGE_STATUS:
663 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
664 case ENVCTRL_RD_SCSI_TEMPERATURE:
665 case ENVCTRL_RD_GLOBALADDRESS:
666 file->private_data = (void *)(long)cmd;
669 case ENVCTRL_RD_CPU_TEMPERATURE:
670 case ENVCTRL_RD_CPU_VOLTAGE:
671 /* Check to see if application passes in any cpu number,
672 * the default is cpu0.
674 infobuf = (char __user *) arg;
675 if (infobuf == NULL) {
678 get_user(read_cpu, infobuf);
681 /* Save the command for use when reading. */
682 file->private_data = (void *)(long)cmd;
692 /* Function Description: open device. Mapped to user open().
696 envctrl_open(struct inode *inode, struct file *file)
699 file->private_data = NULL;
703 /* Function Description: Open device. Mapped to user close().
707 envctrl_release(struct inode *inode, struct file *file)
712 static const struct file_operations envctrl_fops = {
713 .owner = THIS_MODULE,
714 .read = envctrl_read,
715 .unlocked_ioctl = envctrl_ioctl,
717 .compat_ioctl = envctrl_ioctl,
719 .open = envctrl_open,
720 .release = envctrl_release,
723 static struct miscdevice envctrl_dev = {
729 /* Function Description: Set monitor type based on firmware description.
732 static void envctrl_set_mon(struct i2c_child_t *pchild,
733 const char *chnl_desc,
736 /* Firmware only has temperature type. It does not distinguish
737 * different kinds of temperatures. We use channel description
738 * to disinguish them.
740 if (!(strcmp(chnl_desc,"temp,cpu")) ||
741 !(strcmp(chnl_desc,"temp,cpu0")) ||
742 !(strcmp(chnl_desc,"temp,cpu1")) ||
743 !(strcmp(chnl_desc,"temp,cpu2")) ||
744 !(strcmp(chnl_desc,"temp,cpu3")))
745 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
747 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
748 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
749 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
750 !(strcmp(chnl_desc,"vddcore,cpu3")))
751 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
753 if (!(strcmp(chnl_desc,"temp,motherboard")))
754 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
756 if (!(strcmp(chnl_desc,"temp,scsi")))
757 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
759 if (!(strcmp(chnl_desc,"temp,ethernet")))
760 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
763 /* Function Description: Initialize monitor channel with channel desc,
764 * decoding tables, monitor type, optional properties.
767 static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
771 const unsigned int *pval;
773 /* Firmware describe channels into a stream separated by a '\0'. */
774 pos = of_get_property(dp, "channels-description", &len);
777 int l = strlen(pos) + 1;
778 envctrl_set_mon(pchild, pos, i++);
783 /* Get optional properties. */
784 pval = of_get_property(dp, "warning-temp", NULL);
786 warning_temperature = *pval;
788 pval = of_get_property(dp, "shutdown-temp", NULL);
790 shutdown_temperature = *pval;
793 /* Function Description: Initialize child device monitoring fan status.
796 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
800 /* Go through all channels and set up the mask. */
801 for (i = 0; i < pchild->total_chnls; i++)
802 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
804 /* We only need to know if this child has fan status monitored.
805 * We don't care which channels since we have the mask already.
807 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
810 /* Function Description: Initialize child device for global addressing line.
813 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
817 /* Voltage/PowerSupply monitoring is piggybacked
818 * with Global Address on CompactPCI. See comments
819 * within envctrl_i2c_globaladdr for bit assignments.
821 * The mask is created here by assigning mask bits to each
822 * bit position that represents PCF8584_VOLTAGE_TYPE data.
823 * Channel numbers are not consecutive within the globaladdr
824 * node (why?), so we use the actual counter value as chnls_mask
825 * index instead of the chnl_array[x].chnl_no value.
827 * NOTE: This loop could be replaced with a constant representing
828 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
830 for (i = 0; i < pchild->total_chnls; i++) {
831 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
832 pchild->voltage_mask |= chnls_mask[i];
836 /* We only need to know if this child has global addressing
837 * line monitored. We don't care which channels since we know
838 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
840 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
843 /* Initialize child device monitoring voltage status. */
844 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
848 /* Go through all channels and set up the mask. */
849 for (i = 0; i < pchild->total_chnls; i++)
850 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
852 /* We only need to know if this child has voltage status monitored.
853 * We don't care which channels since we have the mask already.
855 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
858 /* Function Description: Initialize i2c child device.
861 static void envctrl_init_i2c_child(struct linux_ebus_child *edev_child,
862 struct i2c_child_t *pchild)
864 int len, i, tbls_size = 0;
865 struct device_node *dp = edev_child->prom_node;
868 /* Get device address. */
869 pval = of_get_property(dp, "reg", &len);
870 memcpy(&pchild->addr, pval, len);
872 /* Get tables property. Read firmware temperature tables. */
873 pval = of_get_property(dp, "translation", &len);
874 if (pval && len > 0) {
875 memcpy(pchild->tblprop_array, pval, len);
876 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
877 for (i = 0; i < pchild->total_tbls; i++) {
878 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
879 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
883 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
884 if (pchild->tables == NULL){
885 printk("envctrl: Failed to allocate table.\n");
888 pval = of_get_property(dp, "tables", &len);
889 if (!pval || len <= 0) {
890 printk("envctrl: Failed to get table.\n");
893 memcpy(pchild->tables, pval, len);
896 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
897 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
898 * "For Factory Use Only."
900 * We ignore the node on these platforms by assigning the
901 * 'NULL' monitor type.
903 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
904 struct device_node *root_node;
907 root_node = of_find_node_by_path("/");
908 if (!strcmp(root_node->name, "SUNW,UltraSPARC-IIi-cEngine")) {
909 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
910 pchild->mon_type[len] = ENVCTRL_NOMON;
916 /* Get the monitor channels. */
917 pval = of_get_property(dp, "channels-in-use", &len);
918 memcpy(pchild->chnl_array, pval, len);
919 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
921 for (i = 0; i < pchild->total_chnls; i++) {
922 switch (pchild->chnl_array[i].type) {
923 case PCF8584_TEMP_TYPE:
924 envctrl_init_adc(pchild, dp);
927 case PCF8584_GLOBALADDR_TYPE:
928 envctrl_init_globaladdr(pchild);
929 i = pchild->total_chnls;
932 case PCF8584_FANSTAT_TYPE:
933 envctrl_init_fanstat(pchild);
934 i = pchild->total_chnls;
937 case PCF8584_VOLTAGE_TYPE:
938 if (pchild->i2ctype == I2C_ADC) {
939 envctrl_init_adc(pchild,dp);
941 envctrl_init_voltage_status(pchild);
943 i = pchild->total_chnls;
952 /* Function Description: Search the child device list for a device.
953 * Return : The i2c child if found. NULL otherwise.
955 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
959 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
960 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
961 if (i2c_childlist[i].mon_type[j] == mon_type) {
962 return (struct i2c_child_t *)(&(i2c_childlist[i]));
969 static void envctrl_do_shutdown(void)
971 static int inprog = 0;
978 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
979 ret = orderly_poweroff(true);
981 printk(KERN_CRIT "kenvctrld: WARNING: system shutdown failed!\n");
982 inprog = 0; /* unlikely to succeed, but we could try again */
986 static struct task_struct *kenvctrld_task;
988 static int kenvctrld(void *__unused)
993 struct i2c_child_t *cputemp;
995 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
997 "envctrl: kenvctrld unable to monitor CPU temp-- exiting\n");
1001 poll_interval = 5000; /* TODO env_mon_interval */
1003 printk(KERN_INFO "envctrl: %s starting...\n", current->comm);
1005 msleep_interruptible(poll_interval);
1007 if (kthread_should_stop())
1010 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1011 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1012 ENVCTRL_CPUTEMP_MON,
1014 if (tempbuf[0] >= shutdown_temperature) {
1016 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1017 "shutdown threshold %i C\n",
1018 current->comm, whichcpu,
1019 tempbuf[0], shutdown_temperature);
1020 envctrl_do_shutdown();
1025 printk(KERN_INFO "envctrl: %s exiting...\n", current->comm);
1029 static int __init envctrl_init(void)
1031 struct linux_ebus *ebus = NULL;
1032 struct linux_ebus_device *edev = NULL;
1033 struct linux_ebus_child *edev_child = NULL;
1036 for_each_ebus(ebus) {
1037 for_each_ebusdev(edev, ebus) {
1038 if (!strcmp(edev->prom_node->name, "bbc")) {
1039 /* If we find a boot-bus controller node,
1040 * then this envctrl driver is not for us.
1047 /* Traverse through ebus and ebus device list for i2c device and
1048 * adc and gpio nodes.
1050 for_each_ebus(ebus) {
1051 for_each_ebusdev(edev, ebus) {
1052 if (!strcmp(edev->prom_node->name, "i2c")) {
1053 i2c = ioremap(edev->resource[0].start, 0x2);
1054 for_each_edevchild(edev, edev_child) {
1055 if (!strcmp("gpio", edev_child->prom_node->name)) {
1056 i2c_childlist[i].i2ctype = I2C_GPIO;
1057 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1059 if (!strcmp("adc", edev_child->prom_node->name)) {
1060 i2c_childlist[i].i2ctype = I2C_ADC;
1061 envctrl_init_i2c_child(edev_child, &(i2c_childlist[i++]));
1071 printk("envctrl: I2C device not found.\n");
1075 /* Set device address. */
1076 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1077 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1079 /* Set system clock and SCL frequencies. */
1080 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1081 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1083 /* Enable serial interface. */
1084 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1087 /* Register the device as a minor miscellaneous device. */
1088 err = misc_register(&envctrl_dev);
1090 printk("envctrl: Unable to get misc minor %d\n",
1095 /* Note above traversal routine post-incremented 'i' to accommodate
1096 * a next child device, so we decrement before reverse-traversal of
1099 printk("envctrl: initialized ");
1100 for (--i; i >= 0; --i) {
1101 printk("[%s 0x%lx]%s",
1102 (I2C_ADC == i2c_childlist[i].i2ctype) ? ("adc") :
1103 ((I2C_GPIO == i2c_childlist[i].i2ctype) ? ("gpio") : ("unknown")),
1104 i2c_childlist[i].addr, (0 == i) ? ("\n") : (" "));
1107 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1108 if (IS_ERR(kenvctrld_task)) {
1109 err = PTR_ERR(kenvctrld_task);
1110 goto out_deregister;
1116 misc_deregister(&envctrl_dev);
1119 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1120 kfree(i2c_childlist[i].tables);
1125 static void __exit envctrl_cleanup(void)
1129 kthread_stop(kenvctrld_task);
1132 misc_deregister(&envctrl_dev);
1134 for (i = 0; i < ENVCTRL_MAX_CPU * 2; i++)
1135 kfree(i2c_childlist[i].tables);
1138 module_init(envctrl_init);
1139 module_exit(envctrl_cleanup);
1140 MODULE_LICENSE("GPL");