1 /* envctrl.c: Temperature and Fan monitoring on Machines providing it.
3 * Copyright (C) 1998 Eddie C. Dost (ecd@skynet.be)
4 * Copyright (C) 2000 Vinh Truong (vinh.truong@eng.sun.com)
5 * VT - The implementation is to support Sun Microelectronics (SME) platform
6 * environment monitoring. SME platforms use pcf8584 as the i2c bus
7 * controller to access pcf8591 (8-bit A/D and D/A converter) and
8 * pcf8571 (256 x 8-bit static low-voltage RAM with I2C-bus interface).
9 * At board level, it follows SME Firmware I2C Specification. Reference:
10 * http://www-eu2.semiconductors.com/pip/PCF8584P
11 * http://www-eu2.semiconductors.com/pip/PCF8574AP
12 * http://www-eu2.semiconductors.com/pip/PCF8591P
14 * EB - Added support for CP1500 Global Address and PS/Voltage monitoring.
15 * Eric Brower <ebrower@usa.net>
17 * DB - Audit every copy_to_user in envctrl_read.
18 * Daniele Bellucci <bellucda@tiscali.it>
21 #include <linux/module.h>
22 #include <linux/kthread.h>
23 #include <linux/delay.h>
24 #include <linux/ioport.h>
25 #include <linux/miscdevice.h>
26 #include <linux/kmod.h>
27 #include <linux/reboot.h>
28 #include <linux/slab.h>
30 #include <linux/of_device.h>
32 #include <linux/uaccess.h>
33 #include <asm/envctrl.h>
36 #define DRIVER_NAME "envctrl"
37 #define PFX DRIVER_NAME ": "
39 #define ENVCTRL_MINOR 162
41 #define PCF8584_ADDRESS 0x55
43 #define CONTROL_PIN 0x80
44 #define CONTROL_ES0 0x40
45 #define CONTROL_ES1 0x20
46 #define CONTROL_ES2 0x10
47 #define CONTROL_ENI 0x08
48 #define CONTROL_STA 0x04
49 #define CONTROL_STO 0x02
50 #define CONTROL_ACK 0x01
52 #define STATUS_PIN 0x80
53 #define STATUS_STS 0x20
54 #define STATUS_BER 0x10
55 #define STATUS_LRB 0x08
56 #define STATUS_AD0 0x08
57 #define STATUS_AAB 0x04
58 #define STATUS_LAB 0x02
59 #define STATUS_BB 0x01
64 #define BUS_CLK_90 0x00
65 #define BUS_CLK_45 0x01
66 #define BUS_CLK_11 0x02
67 #define BUS_CLK_1_5 0x03
75 #define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
76 #define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
78 /* Monitor type of i2c child device.
79 * Firmware definitions.
81 #define PCF8584_MAX_CHANNELS 8
82 #define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
83 #define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
84 #define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
85 #define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
87 /* Monitor type of i2c child device.
90 #define ENVCTRL_NOMON 0
91 #define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
92 #define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
93 #define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
94 #define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperature */
96 #define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
97 #define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
98 #define ENVCTRL_SCSITEMP_MON 7 /* scsi temperature */
99 #define ENVCTRL_GLOBALADDR_MON 8 /* global address */
101 /* Child device type.
102 * Driver definitions.
104 #define I2C_ADC 0 /* pcf8591 */
105 #define I2C_GPIO 1 /* pcf8571 */
107 /* Data read from child device may need to decode
108 * through a data table and a scale.
109 * Translation type as defined by firmware.
111 #define ENVCTRL_TRANSLATE_NO 0
112 #define ENVCTRL_TRANSLATE_PARTIAL 1
113 #define ENVCTRL_TRANSLATE_COMBINED 2
114 #define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
115 #define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
117 /* Driver miscellaneous definitions. */
118 #define ENVCTRL_MAX_CPU 4
119 #define CHANNEL_DESC_SZ 256
121 /* Mask values for combined GlobalAddress/PowerStatus node */
122 #define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
123 #define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
125 /* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
126 * (see envctrl_init_i2c_child)
128 #define ENVCTRL_CPCI_IGNORED_NODE 0x70
130 #define PCF8584_DATA 0x00
131 #define PCF8584_CSR 0x01
133 /* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
134 * Property of a port or channel as defined by the firmware.
136 struct pcf8584_channel {
137 unsigned char chnl_no;
138 unsigned char io_direction;
143 /* Each child device may have one or more tables of bytes to help decode
144 * data. Table property as defined by the firmware.
146 struct pcf8584_tblprop {
149 unsigned int offset; /* offset from the beginning of the table */
155 /* Either ADC or GPIO. */
156 unsigned char i2ctype;
158 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
161 unsigned int total_chnls; /* Number of monitor channels. */
162 unsigned char fan_mask; /* Byte mask for fan status channels. */
163 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
164 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
166 /* Properties of all monitor channels. */
167 unsigned int total_tbls; /* Number of monitor tables. */
168 char *tables; /* Pointer to table(s). */
169 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
170 char mon_type[PCF8584_MAX_CHANNELS];
173 static void __iomem *i2c;
174 static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
175 static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
176 static unsigned int warning_temperature = 0;
177 static unsigned int shutdown_temperature = 0;
178 static char read_cpu;
180 /* Forward declarations. */
181 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
183 /* Function Description: Test the PIN bit (Pending Interrupt Not)
184 * to test when serial transmission is completed .
187 static void envtrl_i2c_test_pin(void)
191 while (--limit > 0) {
192 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
198 printk(KERN_INFO PFX "Pin status will not clear.\n");
201 /* Function Description: Test busy bit.
204 static void envctrl_i2c_test_bb(void)
208 while (--limit > 0) {
209 /* Busy bit 0 means busy. */
210 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
216 printk(KERN_INFO PFX "Busy bit will not clear.\n");
219 /* Function Description: Send the address for a read access.
220 * Return : 0 if not acknowledged, otherwise acknowledged.
222 static int envctrl_i2c_read_addr(unsigned char addr)
224 envctrl_i2c_test_bb();
227 writeb(addr + 1, i2c + PCF8584_DATA);
229 envctrl_i2c_test_bb();
231 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
234 envtrl_i2c_test_pin();
236 /* CSR 0 means acknowledged. */
237 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
238 return readb(i2c + PCF8584_DATA);
240 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
245 /* Function Description: Send the address for write mode.
248 static void envctrl_i2c_write_addr(unsigned char addr)
250 envctrl_i2c_test_bb();
251 writeb(addr, i2c + PCF8584_DATA);
253 /* Generate Start condition. */
254 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
257 /* Function Description: Read 1 byte of data from addr
258 * set by envctrl_i2c_read_addr()
259 * Return : Data from address set by envctrl_i2c_read_addr().
261 static unsigned char envctrl_i2c_read_data(void)
263 envtrl_i2c_test_pin();
264 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
265 return readb(i2c + PCF8584_DATA);
268 /* Function Description: Instruct the device which port to read data from.
271 static void envctrl_i2c_write_data(unsigned char port)
273 envtrl_i2c_test_pin();
274 writeb(port, i2c + PCF8584_DATA);
277 /* Function Description: Generate Stop condition after last byte is sent.
280 static void envctrl_i2c_stop(void)
282 envtrl_i2c_test_pin();
283 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
286 /* Function Description: Read adc device.
287 * Return : Data at address and port.
289 static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
292 envctrl_i2c_write_addr(addr);
294 /* Setup port to read. */
295 envctrl_i2c_write_data(port);
299 envctrl_i2c_read_addr(addr);
301 /* Do a single byte read and send stop. */
302 envctrl_i2c_read_data();
305 return readb(i2c + PCF8584_DATA);
308 /* Function Description: Read gpio device.
309 * Return : Data at address.
311 static unsigned char envctrl_i2c_read_8574(unsigned char addr)
315 envctrl_i2c_read_addr(addr);
317 /* Do a single byte read and send stop. */
318 rd = envctrl_i2c_read_data();
323 /* Function Description: Decode data read from an adc device using firmware
325 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
327 static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
328 int scale, char *tbl, char *bufdata)
332 switch (translate_type) {
333 case ENVCTRL_TRANSLATE_NO:
334 /* No decode necessary. */
339 case ENVCTRL_TRANSLATE_FULL:
340 /* Decode this way: data = table[data]. */
342 bufdata[0] = tbl[data];
345 case ENVCTRL_TRANSLATE_SCALE:
346 /* Decode this way: data = table[data]/scale */
347 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
348 len = strlen(bufdata);
349 bufdata[len - 1] = bufdata[len - 2];
350 bufdata[len - 2] = '.';
360 /* Function Description: Read cpu-related data such as cpu temperature, voltage.
361 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
363 static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
364 char mon_type, unsigned char *bufdata)
370 /* Find the right monitor type and channel. */
371 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
372 if (pchild->mon_type[i] == mon_type) {
382 /* Read data from address and port. */
383 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
384 (unsigned char)pchild->chnl_array[i].chnl_no);
386 /* Find decoding table. */
387 tbl = pchild->tables + pchild->tblprop_array[i].offset;
389 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
390 pchild->tblprop_array[i].scale,
394 /* Function Description: Read noncpu-related data such as motherboard
396 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
398 static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
399 char mon_type, unsigned char *bufdata)
405 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
406 if (pchild->mon_type[i] == mon_type)
410 if (i >= PCF8584_MAX_CHANNELS)
413 /* Read data from address and port. */
414 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
415 (unsigned char)pchild->chnl_array[i].chnl_no);
417 /* Find decoding table. */
418 tbl = pchild->tables + pchild->tblprop_array[i].offset;
420 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
421 pchild->tblprop_array[i].scale,
425 /* Function Description: Read fan status.
426 * Return : Always 1 byte. Status stored in bufdata.
428 static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
432 unsigned char tmp, ret = 0;
435 tmp = data & pchild->fan_mask;
437 if (tmp == pchild->fan_mask) {
438 /* All bits are on. All fans are functioning. */
439 ret = ENVCTRL_ALL_FANS_GOOD;
440 } else if (tmp == 0) {
441 /* No bits are on. No fans are functioning. */
442 ret = ENVCTRL_ALL_FANS_BAD;
444 /* Go through all channels, mark 'on' the matched bits.
445 * Notice that fan_mask may have discontiguous bits but
446 * return mask are always contiguous. For example if we
447 * monitor 4 fans at channels 0,1,2,4, the return mask
448 * should be 00010000 if only fan at channel 4 is working.
450 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
451 if (pchild->fan_mask & chnls_mask[i]) {
452 if (!(chnls_mask[i] & tmp))
453 ret |= chnls_mask[j];
464 /* Function Description: Read global addressing line.
465 * Return : Always 1 byte. Status stored in bufdata.
467 static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
471 /* Translatation table is not necessary, as global
472 * addr is the integer value of the GA# bits.
474 * NOTE: MSB is documented as zero, but I see it as '1' always....
476 * -----------------------------------------------
477 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
478 * -----------------------------------------------
479 * GA0 - GA4 integer value of Global Address (backplane slot#)
480 * DEG 0 = cPCI Power supply output is starting to degrade
481 * 1 = cPCI Power supply output is OK
482 * FAL 0 = cPCI Power supply has failed
483 * 1 = cPCI Power supply output is OK
485 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
489 /* Function Description: Read standard voltage and power supply status.
490 * Return : Always 1 byte. Status stored in bufdata.
492 static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
496 unsigned char tmp, ret = 0;
499 tmp = data & pchild->voltage_mask;
501 /* Two channels are used to monitor voltage and power supply. */
502 if (tmp == pchild->voltage_mask) {
503 /* All bits are on. Voltage and power supply are okay. */
504 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
505 } else if (tmp == 0) {
506 /* All bits are off. Voltage and power supply are bad */
507 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
509 /* Either voltage or power supply has problem. */
510 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
511 if (pchild->voltage_mask & chnls_mask[i]) {
514 /* Break out when there is a mismatch. */
515 if (!(chnls_mask[i] & tmp))
520 /* Make a wish that hardware will always use the
521 * first channel for voltage and the second for
525 ret = ENVCTRL_VOLTAGE_BAD;
527 ret = ENVCTRL_POWERSUPPLY_BAD;
534 /* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
535 * Return: Number of read bytes. 0 for error.
538 envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
540 struct i2c_child_t *pchild;
541 unsigned char data[10];
544 /* Get the type of read as decided in ioctl() call.
545 * Find the appropriate i2c child.
546 * Get the data and put back to the user buffer.
549 switch ((int)(long)file->private_data) {
550 case ENVCTRL_RD_WARNING_TEMPERATURE:
551 if (warning_temperature == 0)
554 data[0] = (unsigned char)(warning_temperature);
556 if (copy_to_user(buf, data, ret))
560 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
561 if (shutdown_temperature == 0)
564 data[0] = (unsigned char)(shutdown_temperature);
566 if (copy_to_user(buf, data, ret))
570 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
571 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
573 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
574 if (copy_to_user(buf, data, ret))
578 case ENVCTRL_RD_CPU_TEMPERATURE:
579 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
581 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
583 /* Reset cpu to the default cpu0. */
584 if (copy_to_user(buf, data, ret))
588 case ENVCTRL_RD_CPU_VOLTAGE:
589 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
591 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
593 /* Reset cpu to the default cpu0. */
594 if (copy_to_user(buf, data, ret))
598 case ENVCTRL_RD_SCSI_TEMPERATURE:
599 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
601 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
602 if (copy_to_user(buf, data, ret))
606 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
607 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
609 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
610 if (copy_to_user(buf, data, ret))
614 case ENVCTRL_RD_FAN_STATUS:
615 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
617 data[0] = envctrl_i2c_read_8574(pchild->addr);
618 ret = envctrl_i2c_fan_status(pchild,data[0], data);
619 if (copy_to_user(buf, data, ret))
623 case ENVCTRL_RD_GLOBALADDRESS:
624 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
626 data[0] = envctrl_i2c_read_8574(pchild->addr);
627 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
628 if (copy_to_user(buf, data, ret))
632 case ENVCTRL_RD_VOLTAGE_STATUS:
633 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
634 /* If voltage monitor not present, check for CPCI equivalent */
635 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
637 data[0] = envctrl_i2c_read_8574(pchild->addr);
638 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
639 if (copy_to_user(buf, data, ret))
651 /* Function Description: Command what to read. Mapped to user ioctl().
652 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
655 envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
657 char __user *infobuf;
660 case ENVCTRL_RD_WARNING_TEMPERATURE:
661 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
662 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
663 case ENVCTRL_RD_FAN_STATUS:
664 case ENVCTRL_RD_VOLTAGE_STATUS:
665 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
666 case ENVCTRL_RD_SCSI_TEMPERATURE:
667 case ENVCTRL_RD_GLOBALADDRESS:
668 file->private_data = (void *)(long)cmd;
671 case ENVCTRL_RD_CPU_TEMPERATURE:
672 case ENVCTRL_RD_CPU_VOLTAGE:
673 /* Check to see if application passes in any cpu number,
674 * the default is cpu0.
676 infobuf = (char __user *) arg;
677 if (infobuf == NULL) {
680 get_user(read_cpu, infobuf);
683 /* Save the command for use when reading. */
684 file->private_data = (void *)(long)cmd;
694 /* Function Description: open device. Mapped to user open().
698 envctrl_open(struct inode *inode, struct file *file)
700 file->private_data = NULL;
704 /* Function Description: Open device. Mapped to user close().
708 envctrl_release(struct inode *inode, struct file *file)
713 static const struct file_operations envctrl_fops = {
714 .owner = THIS_MODULE,
715 .read = envctrl_read,
716 .unlocked_ioctl = envctrl_ioctl,
718 .compat_ioctl = envctrl_ioctl,
720 .open = envctrl_open,
721 .release = envctrl_release,
722 .llseek = noop_llseek,
725 static struct miscdevice envctrl_dev = {
731 /* Function Description: Set monitor type based on firmware description.
734 static void envctrl_set_mon(struct i2c_child_t *pchild,
735 const char *chnl_desc,
738 /* Firmware only has temperature type. It does not distinguish
739 * different kinds of temperatures. We use channel description
740 * to disinguish them.
742 if (!(strcmp(chnl_desc,"temp,cpu")) ||
743 !(strcmp(chnl_desc,"temp,cpu0")) ||
744 !(strcmp(chnl_desc,"temp,cpu1")) ||
745 !(strcmp(chnl_desc,"temp,cpu2")) ||
746 !(strcmp(chnl_desc,"temp,cpu3")))
747 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
749 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
750 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
751 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
752 !(strcmp(chnl_desc,"vddcore,cpu3")))
753 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
755 if (!(strcmp(chnl_desc,"temp,motherboard")))
756 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
758 if (!(strcmp(chnl_desc,"temp,scsi")))
759 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
761 if (!(strcmp(chnl_desc,"temp,ethernet")))
762 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
765 /* Function Description: Initialize monitor channel with channel desc,
766 * decoding tables, monitor type, optional properties.
769 static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
773 const unsigned int *pval;
775 /* Firmware describe channels into a stream separated by a '\0'. */
776 pos = of_get_property(dp, "channels-description", &len);
779 int l = strlen(pos) + 1;
780 envctrl_set_mon(pchild, pos, i++);
785 /* Get optional properties. */
786 pval = of_get_property(dp, "warning-temp", NULL);
788 warning_temperature = *pval;
790 pval = of_get_property(dp, "shutdown-temp", NULL);
792 shutdown_temperature = *pval;
795 /* Function Description: Initialize child device monitoring fan status.
798 static void envctrl_init_fanstat(struct i2c_child_t *pchild)
802 /* Go through all channels and set up the mask. */
803 for (i = 0; i < pchild->total_chnls; i++)
804 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
806 /* We only need to know if this child has fan status monitored.
807 * We don't care which channels since we have the mask already.
809 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
812 /* Function Description: Initialize child device for global addressing line.
815 static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
819 /* Voltage/PowerSupply monitoring is piggybacked
820 * with Global Address on CompactPCI. See comments
821 * within envctrl_i2c_globaladdr for bit assignments.
823 * The mask is created here by assigning mask bits to each
824 * bit position that represents PCF8584_VOLTAGE_TYPE data.
825 * Channel numbers are not consecutive within the globaladdr
826 * node (why?), so we use the actual counter value as chnls_mask
827 * index instead of the chnl_array[x].chnl_no value.
829 * NOTE: This loop could be replaced with a constant representing
830 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
832 for (i = 0; i < pchild->total_chnls; i++) {
833 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
834 pchild->voltage_mask |= chnls_mask[i];
838 /* We only need to know if this child has global addressing
839 * line monitored. We don't care which channels since we know
840 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
842 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
845 /* Initialize child device monitoring voltage status. */
846 static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
850 /* Go through all channels and set up the mask. */
851 for (i = 0; i < pchild->total_chnls; i++)
852 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
854 /* We only need to know if this child has voltage status monitored.
855 * We don't care which channels since we have the mask already.
857 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
860 /* Function Description: Initialize i2c child device.
863 static void envctrl_init_i2c_child(struct device_node *dp,
864 struct i2c_child_t *pchild)
866 int len, i, tbls_size = 0;
869 /* Get device address. */
870 pval = of_get_property(dp, "reg", &len);
871 memcpy(&pchild->addr, pval, len);
873 /* Get tables property. Read firmware temperature tables. */
874 pval = of_get_property(dp, "translation", &len);
875 if (pval && len > 0) {
876 memcpy(pchild->tblprop_array, pval, len);
877 pchild->total_tbls = len / sizeof(struct pcf8584_tblprop);
878 for (i = 0; i < pchild->total_tbls; i++) {
879 if ((pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset) > tbls_size) {
880 tbls_size = pchild->tblprop_array[i].size + pchild->tblprop_array[i].offset;
884 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
885 if (pchild->tables == NULL){
886 printk(KERN_ERR PFX "Failed to allocate table.\n");
889 pval = of_get_property(dp, "tables", &len);
890 if (!pval || len <= 0) {
891 printk(KERN_ERR PFX "Failed to get table.\n");
894 memcpy(pchild->tables, pval, len);
897 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
898 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
899 * "For Factory Use Only."
901 * We ignore the node on these platforms by assigning the
902 * 'NULL' monitor type.
904 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
905 struct device_node *root_node;
908 root_node = of_find_node_by_path("/");
909 if (of_node_name_eq(root_node, "SUNW,UltraSPARC-IIi-cEngine")) {
910 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
911 pchild->mon_type[len] = ENVCTRL_NOMON;
913 of_node_put(root_node);
916 of_node_put(root_node);
919 /* Get the monitor channels. */
920 pval = of_get_property(dp, "channels-in-use", &len);
921 memcpy(pchild->chnl_array, pval, len);
922 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
924 for (i = 0; i < pchild->total_chnls; i++) {
925 switch (pchild->chnl_array[i].type) {
926 case PCF8584_TEMP_TYPE:
927 envctrl_init_adc(pchild, dp);
930 case PCF8584_GLOBALADDR_TYPE:
931 envctrl_init_globaladdr(pchild);
932 i = pchild->total_chnls;
935 case PCF8584_FANSTAT_TYPE:
936 envctrl_init_fanstat(pchild);
937 i = pchild->total_chnls;
940 case PCF8584_VOLTAGE_TYPE:
941 if (pchild->i2ctype == I2C_ADC) {
942 envctrl_init_adc(pchild,dp);
944 envctrl_init_voltage_status(pchild);
946 i = pchild->total_chnls;
955 /* Function Description: Search the child device list for a device.
956 * Return : The i2c child if found. NULL otherwise.
958 static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
962 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
963 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
964 if (i2c_childlist[i].mon_type[j] == mon_type) {
965 return (struct i2c_child_t *)(&(i2c_childlist[i]));
972 static void envctrl_do_shutdown(void)
974 static int inprog = 0;
980 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
981 orderly_poweroff(true);
984 static struct task_struct *kenvctrld_task;
986 static int kenvctrld(void *__unused)
991 struct i2c_child_t *cputemp;
993 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
995 "kenvctrld unable to monitor CPU temp-- exiting\n");
999 poll_interval = 5000; /* TODO env_mon_interval */
1001 printk(KERN_INFO PFX "%s starting...\n", current->comm);
1003 msleep_interruptible(poll_interval);
1005 if (kthread_should_stop())
1008 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1009 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1010 ENVCTRL_CPUTEMP_MON,
1012 if (tempbuf[0] >= shutdown_temperature) {
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();
1023 printk(KERN_INFO PFX "%s exiting...\n", current->comm);
1027 static int envctrl_probe(struct platform_device *op)
1029 struct device_node *dp;
1035 i2c = of_ioremap(&op->resource[0], 0, 0x2, DRIVER_NAME);
1040 dp = op->dev.of_node->child;
1042 if (of_node_name_eq(dp, "gpio")) {
1043 i2c_childlist[index].i2ctype = I2C_GPIO;
1044 envctrl_init_i2c_child(dp, &(i2c_childlist[index++]));
1045 } else if (of_node_name_eq(dp, "adc")) {
1046 i2c_childlist[index].i2ctype = I2C_ADC;
1047 envctrl_init_i2c_child(dp, &(i2c_childlist[index++]));
1053 /* Set device address. */
1054 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1055 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1057 /* Set system clock and SCL frequencies. */
1058 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1059 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1061 /* Enable serial interface. */
1062 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1065 /* Register the device as a minor miscellaneous device. */
1066 err = misc_register(&envctrl_dev);
1068 printk(KERN_ERR PFX "Unable to get misc minor %d\n",
1073 /* Note above traversal routine post-incremented 'i' to accommodate
1074 * a next child device, so we decrement before reverse-traversal of
1077 printk(KERN_INFO PFX "Initialized ");
1078 for (--index; index >= 0; --index) {
1079 printk("[%s 0x%lx]%s",
1080 (I2C_ADC == i2c_childlist[index].i2ctype) ? "adc" :
1081 ((I2C_GPIO == i2c_childlist[index].i2ctype) ? "gpio" : "unknown"),
1082 i2c_childlist[index].addr, (0 == index) ? "\n" : " ");
1085 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1086 if (IS_ERR(kenvctrld_task)) {
1087 err = PTR_ERR(kenvctrld_task);
1088 goto out_deregister;
1094 misc_deregister(&envctrl_dev);
1096 of_iounmap(&op->resource[0], i2c, 0x2);
1097 for (index = 0; index < ENVCTRL_MAX_CPU * 2; index++)
1098 kfree(i2c_childlist[index].tables);
1103 static int envctrl_remove(struct platform_device *op)
1107 kthread_stop(kenvctrld_task);
1109 of_iounmap(&op->resource[0], i2c, 0x2);
1110 misc_deregister(&envctrl_dev);
1112 for (index = 0; index < ENVCTRL_MAX_CPU * 2; index++)
1113 kfree(i2c_childlist[index].tables);
1118 static const struct of_device_id envctrl_match[] = {
1121 .compatible = "i2cpcf,8584",
1125 MODULE_DEVICE_TABLE(of, envctrl_match);
1127 static struct platform_driver envctrl_driver = {
1129 .name = DRIVER_NAME,
1130 .of_match_table = envctrl_match,
1132 .probe = envctrl_probe,
1133 .remove = envctrl_remove,
1136 module_platform_driver(envctrl_driver);
1138 MODULE_LICENSE("GPL");