Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/* envctrl.c: Temperature and Fan monitoring on Machines providing it.
3 *
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
14 *
15 * EB - Added support for CP1500 Global Address and PS/Voltage monitoring.
16 * Eric Brower <ebrower@usa.net>
17 *
18 * DB - Audit every copy_to_user in envctrl_read.
19 * Daniele Bellucci <bellucda@tiscali.it>
20 */
21
22#include <linux/module.h>
23#include <linux/kthread.h>
24#include <linux/delay.h>
25#include <linux/ioport.h>
26#include <linux/miscdevice.h>
27#include <linux/kmod.h>
28#include <linux/reboot.h>
29#include <linux/slab.h>
30#include <linux/of.h>
31#include <linux/platform_device.h>
32
33#include <linux/uaccess.h>
34#include <asm/envctrl.h>
35#include <asm/io.h>
36
37#define DRIVER_NAME "envctrl"
38#define PFX DRIVER_NAME ": "
39
40#define PCF8584_ADDRESS 0x55
41
42#define CONTROL_PIN 0x80
43#define CONTROL_ES0 0x40
44#define CONTROL_ES1 0x20
45#define CONTROL_ES2 0x10
46#define CONTROL_ENI 0x08
47#define CONTROL_STA 0x04
48#define CONTROL_STO 0x02
49#define CONTROL_ACK 0x01
50
51#define STATUS_PIN 0x80
52#define STATUS_STS 0x20
53#define STATUS_BER 0x10
54#define STATUS_LRB 0x08
55#define STATUS_AD0 0x08
56#define STATUS_AAB 0x04
57#define STATUS_LAB 0x02
58#define STATUS_BB 0x01
59
60/*
61 * CLK Mode Register.
62 */
63#define BUS_CLK_90 0x00
64#define BUS_CLK_45 0x01
65#define BUS_CLK_11 0x02
66#define BUS_CLK_1_5 0x03
67
68#define CLK_3 0x00
69#define CLK_4_43 0x10
70#define CLK_6 0x14
71#define CLK_8 0x18
72#define CLK_12 0x1c
73
74#define OBD_SEND_START 0xc5 /* value to generate I2c_bus START condition */
75#define OBD_SEND_STOP 0xc3 /* value to generate I2c_bus STOP condition */
76
77/* Monitor type of i2c child device.
78 * Firmware definitions.
79 */
80#define PCF8584_MAX_CHANNELS 8
81#define PCF8584_GLOBALADDR_TYPE 6 /* global address monitor */
82#define PCF8584_FANSTAT_TYPE 3 /* fan status monitor */
83#define PCF8584_VOLTAGE_TYPE 2 /* voltage monitor */
84#define PCF8584_TEMP_TYPE 1 /* temperature monitor*/
85
86/* Monitor type of i2c child device.
87 * Driver definitions.
88 */
89#define ENVCTRL_NOMON 0
90#define ENVCTRL_CPUTEMP_MON 1 /* cpu temperature monitor */
91#define ENVCTRL_CPUVOLTAGE_MON 2 /* voltage monitor */
92#define ENVCTRL_FANSTAT_MON 3 /* fan status monitor */
93#define ENVCTRL_ETHERTEMP_MON 4 /* ethernet temperature */
94 /* monitor */
95#define ENVCTRL_VOLTAGESTAT_MON 5 /* voltage status monitor */
96#define ENVCTRL_MTHRBDTEMP_MON 6 /* motherboard temperature */
97#define ENVCTRL_SCSITEMP_MON 7 /* scsi temperature */
98#define ENVCTRL_GLOBALADDR_MON 8 /* global address */
99
100/* Child device type.
101 * Driver definitions.
102 */
103#define I2C_ADC 0 /* pcf8591 */
104#define I2C_GPIO 1 /* pcf8571 */
105
106/* Data read from child device may need to decode
107 * through a data table and a scale.
108 * Translation type as defined by firmware.
109 */
110#define ENVCTRL_TRANSLATE_NO 0
111#define ENVCTRL_TRANSLATE_PARTIAL 1
112#define ENVCTRL_TRANSLATE_COMBINED 2
113#define ENVCTRL_TRANSLATE_FULL 3 /* table[data] */
114#define ENVCTRL_TRANSLATE_SCALE 4 /* table[data]/scale */
115
116/* Driver miscellaneous definitions. */
117#define ENVCTRL_MAX_CPU 4
118#define CHANNEL_DESC_SZ 256
119
120/* Mask values for combined GlobalAddress/PowerStatus node */
121#define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
122#define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
123
124/* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
125 * (see envctrl_init_i2c_child)
126 */
127#define ENVCTRL_CPCI_IGNORED_NODE 0x70
128
129#define PCF8584_DATA 0x00
130#define PCF8584_CSR 0x01
131
132/* Each child device can be monitored by up to PCF8584_MAX_CHANNELS.
133 * Property of a port or channel as defined by the firmware.
134 */
135struct pcf8584_channel {
136 unsigned char chnl_no;
137 unsigned char io_direction;
138 unsigned char type;
139 unsigned char last;
140};
141
142/* Each child device may have one or more tables of bytes to help decode
143 * data. Table property as defined by the firmware.
144 */
145struct pcf8584_tblprop {
146 unsigned int type;
147 unsigned int scale;
148 unsigned int offset; /* offset from the beginning of the table */
149 unsigned int size;
150};
151
152/* i2c child */
153struct i2c_child_t {
154 /* Either ADC or GPIO. */
155 unsigned char i2ctype;
156 unsigned long addr;
157 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
158
159 /* Channel info. */
160 unsigned int total_chnls; /* Number of monitor channels. */
161 unsigned char fan_mask; /* Byte mask for fan status channels. */
162 unsigned char voltage_mask; /* Byte mask for voltage status channels. */
163 struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];
164
165 /* Properties of all monitor channels. */
166 unsigned int total_tbls; /* Number of monitor tables. */
167 char *tables; /* Pointer to table(s). */
168 char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */
169 char mon_type[PCF8584_MAX_CHANNELS];
170};
171
172static void __iomem *i2c;
173static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
174static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
175static unsigned int warning_temperature = 0;
176static unsigned int shutdown_temperature = 0;
177static char read_cpu;
178
179/* Forward declarations. */
180static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
181
182/* Function Description: Test the PIN bit (Pending Interrupt Not)
183 * to test when serial transmission is completed .
184 * Return : None.
185 */
186static void envtrl_i2c_test_pin(void)
187{
188 int limit = 1000000;
189
190 while (--limit > 0) {
191 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
192 break;
193 udelay(1);
194 }
195
196 if (limit <= 0)
197 printk(KERN_INFO PFX "Pin status will not clear.\n");
198}
199
200/* Function Description: Test busy bit.
201 * Return : None.
202 */
203static void envctrl_i2c_test_bb(void)
204{
205 int limit = 1000000;
206
207 while (--limit > 0) {
208 /* Busy bit 0 means busy. */
209 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
210 break;
211 udelay(1);
212 }
213
214 if (limit <= 0)
215 printk(KERN_INFO PFX "Busy bit will not clear.\n");
216}
217
218/* Function Description: Send the address for a read access.
219 * Return : 0 if not acknowledged, otherwise acknowledged.
220 */
221static int envctrl_i2c_read_addr(unsigned char addr)
222{
223 envctrl_i2c_test_bb();
224
225 /* Load address. */
226 writeb(addr + 1, i2c + PCF8584_DATA);
227
228 envctrl_i2c_test_bb();
229
230 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
231
232 /* Wait for PIN. */
233 envtrl_i2c_test_pin();
234
235 /* CSR 0 means acknowledged. */
236 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
237 return readb(i2c + PCF8584_DATA);
238 } else {
239 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
240 return 0;
241 }
242}
243
244/* Function Description: Send the address for write mode.
245 * Return : None.
246 */
247static void envctrl_i2c_write_addr(unsigned char addr)
248{
249 envctrl_i2c_test_bb();
250 writeb(addr, i2c + PCF8584_DATA);
251
252 /* Generate Start condition. */
253 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
254}
255
256/* Function Description: Read 1 byte of data from addr
257 * set by envctrl_i2c_read_addr()
258 * Return : Data from address set by envctrl_i2c_read_addr().
259 */
260static unsigned char envctrl_i2c_read_data(void)
261{
262 envtrl_i2c_test_pin();
263 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
264 return readb(i2c + PCF8584_DATA);
265}
266
267/* Function Description: Instruct the device which port to read data from.
268 * Return : None.
269 */
270static void envctrl_i2c_write_data(unsigned char port)
271{
272 envtrl_i2c_test_pin();
273 writeb(port, i2c + PCF8584_DATA);
274}
275
276/* Function Description: Generate Stop condition after last byte is sent.
277 * Return : None.
278 */
279static void envctrl_i2c_stop(void)
280{
281 envtrl_i2c_test_pin();
282 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
283}
284
285/* Function Description: Read adc device.
286 * Return : Data at address and port.
287 */
288static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
289{
290 /* Send address. */
291 envctrl_i2c_write_addr(addr);
292
293 /* Setup port to read. */
294 envctrl_i2c_write_data(port);
295 envctrl_i2c_stop();
296
297 /* Read port. */
298 envctrl_i2c_read_addr(addr);
299
300 /* Do a single byte read and send stop. */
301 envctrl_i2c_read_data();
302 envctrl_i2c_stop();
303
304 return readb(i2c + PCF8584_DATA);
305}
306
307/* Function Description: Read gpio device.
308 * Return : Data at address.
309 */
310static unsigned char envctrl_i2c_read_8574(unsigned char addr)
311{
312 unsigned char rd;
313
314 envctrl_i2c_read_addr(addr);
315
316 /* Do a single byte read and send stop. */
317 rd = envctrl_i2c_read_data();
318 envctrl_i2c_stop();
319 return rd;
320}
321
322/* Function Description: Decode data read from an adc device using firmware
323 * table.
324 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
325 */
326static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
327 int scale, char *tbl, char *bufdata)
328{
329 int len = 0;
330
331 switch (translate_type) {
332 case ENVCTRL_TRANSLATE_NO:
333 /* No decode necessary. */
334 len = 1;
335 bufdata[0] = data;
336 break;
337
338 case ENVCTRL_TRANSLATE_FULL:
339 /* Decode this way: data = table[data]. */
340 len = 1;
341 bufdata[0] = tbl[data];
342 break;
343
344 case ENVCTRL_TRANSLATE_SCALE:
345 /* Decode this way: data = table[data]/scale */
346 sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));
347 len = strlen(bufdata);
348 bufdata[len - 1] = bufdata[len - 2];
349 bufdata[len - 2] = '.';
350 break;
351
352 default:
353 break;
354 }
355
356 return len;
357}
358
359/* Function Description: Read cpu-related data such as cpu temperature, voltage.
360 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
361 */
362static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
363 char mon_type, unsigned char *bufdata)
364{
365 unsigned char data;
366 int i, j = -1;
367 char *tbl;
368
369 /* Find the right monitor type and channel. */
370 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
371 if (pchild->mon_type[i] == mon_type) {
372 if (++j == cpu) {
373 break;
374 }
375 }
376 }
377
378 if (j != cpu)
379 return 0;
380
381 /* Read data from address and port. */
382 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
383 (unsigned char)pchild->chnl_array[i].chnl_no);
384
385 /* Find decoding table. */
386 tbl = pchild->tables + pchild->tblprop_array[i].offset;
387
388 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
389 pchild->tblprop_array[i].scale,
390 tbl, bufdata);
391}
392
393/* Function Description: Read noncpu-related data such as motherboard
394 * temperature.
395 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
396 */
397static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
398 char mon_type, unsigned char *bufdata)
399{
400 unsigned char data;
401 int i;
402 char *tbl = NULL;
403
404 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
405 if (pchild->mon_type[i] == mon_type)
406 break;
407 }
408
409 if (i >= PCF8584_MAX_CHANNELS)
410 return 0;
411
412 /* Read data from address and port. */
413 data = envctrl_i2c_read_8591((unsigned char)pchild->addr,
414 (unsigned char)pchild->chnl_array[i].chnl_no);
415
416 /* Find decoding table. */
417 tbl = pchild->tables + pchild->tblprop_array[i].offset;
418
419 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
420 pchild->tblprop_array[i].scale,
421 tbl, bufdata);
422}
423
424/* Function Description: Read fan status.
425 * Return : Always 1 byte. Status stored in bufdata.
426 */
427static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
428 unsigned char data,
429 char *bufdata)
430{
431 unsigned char tmp, ret = 0;
432 int i, j = 0;
433
434 tmp = data & pchild->fan_mask;
435
436 if (tmp == pchild->fan_mask) {
437 /* All bits are on. All fans are functioning. */
438 ret = ENVCTRL_ALL_FANS_GOOD;
439 } else if (tmp == 0) {
440 /* No bits are on. No fans are functioning. */
441 ret = ENVCTRL_ALL_FANS_BAD;
442 } else {
443 /* Go through all channels, mark 'on' the matched bits.
444 * Notice that fan_mask may have discontiguous bits but
445 * return mask are always contiguous. For example if we
446 * monitor 4 fans at channels 0,1,2,4, the return mask
447 * should be 00010000 if only fan at channel 4 is working.
448 */
449 for (i = 0; i < PCF8584_MAX_CHANNELS;i++) {
450 if (pchild->fan_mask & chnls_mask[i]) {
451 if (!(chnls_mask[i] & tmp))
452 ret |= chnls_mask[j];
453
454 j++;
455 }
456 }
457 }
458
459 bufdata[0] = ret;
460 return 1;
461}
462
463/* Function Description: Read global addressing line.
464 * Return : Always 1 byte. Status stored in bufdata.
465 */
466static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
467 unsigned char data,
468 char *bufdata)
469{
470 /* Translatation table is not necessary, as global
471 * addr is the integer value of the GA# bits.
472 *
473 * NOTE: MSB is documented as zero, but I see it as '1' always....
474 *
475 * -----------------------------------------------
476 * | 0 | FAL | DEG | GA4 | GA3 | GA2 | GA1 | GA0 |
477 * -----------------------------------------------
478 * GA0 - GA4 integer value of Global Address (backplane slot#)
479 * DEG 0 = cPCI Power supply output is starting to degrade
480 * 1 = cPCI Power supply output is OK
481 * FAL 0 = cPCI Power supply has failed
482 * 1 = cPCI Power supply output is OK
483 */
484 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
485 return 1;
486}
487
488/* Function Description: Read standard voltage and power supply status.
489 * Return : Always 1 byte. Status stored in bufdata.
490 */
491static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
492 unsigned char data,
493 char *bufdata)
494{
495 unsigned char tmp, ret = 0;
496 int i, j = 0;
497
498 tmp = data & pchild->voltage_mask;
499
500 /* Two channels are used to monitor voltage and power supply. */
501 if (tmp == pchild->voltage_mask) {
502 /* All bits are on. Voltage and power supply are okay. */
503 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_GOOD;
504 } else if (tmp == 0) {
505 /* All bits are off. Voltage and power supply are bad */
506 ret = ENVCTRL_VOLTAGE_POWERSUPPLY_BAD;
507 } else {
508 /* Either voltage or power supply has problem. */
509 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
510 if (pchild->voltage_mask & chnls_mask[i]) {
511 j++;
512
513 /* Break out when there is a mismatch. */
514 if (!(chnls_mask[i] & tmp))
515 break;
516 }
517 }
518
519 /* Make a wish that hardware will always use the
520 * first channel for voltage and the second for
521 * power supply.
522 */
523 if (j == 1)
524 ret = ENVCTRL_VOLTAGE_BAD;
525 else
526 ret = ENVCTRL_POWERSUPPLY_BAD;
527 }
528
529 bufdata[0] = ret;
530 return 1;
531}
532
533/* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
534 * Return: Number of read bytes. 0 for error.
535 */
536static ssize_t
537envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
538{
539 struct i2c_child_t *pchild;
540 unsigned char data[10];
541 int ret = 0;
542
543 /* Get the type of read as decided in ioctl() call.
544 * Find the appropriate i2c child.
545 * Get the data and put back to the user buffer.
546 */
547
548 switch ((int)(long)file->private_data) {
549 case ENVCTRL_RD_WARNING_TEMPERATURE:
550 if (warning_temperature == 0)
551 return 0;
552
553 data[0] = (unsigned char)(warning_temperature);
554 ret = 1;
555 if (copy_to_user(buf, data, ret))
556 ret = -EFAULT;
557 break;
558
559 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
560 if (shutdown_temperature == 0)
561 return 0;
562
563 data[0] = (unsigned char)(shutdown_temperature);
564 ret = 1;
565 if (copy_to_user(buf, data, ret))
566 ret = -EFAULT;
567 break;
568
569 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
570 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
571 return 0;
572 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
573 if (copy_to_user(buf, data, ret))
574 ret = -EFAULT;
575 break;
576
577 case ENVCTRL_RD_CPU_TEMPERATURE:
578 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
579 return 0;
580 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
581
582 /* Reset cpu to the default cpu0. */
583 if (copy_to_user(buf, data, ret))
584 ret = -EFAULT;
585 break;
586
587 case ENVCTRL_RD_CPU_VOLTAGE:
588 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
589 return 0;
590 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
591
592 /* Reset cpu to the default cpu0. */
593 if (copy_to_user(buf, data, ret))
594 ret = -EFAULT;
595 break;
596
597 case ENVCTRL_RD_SCSI_TEMPERATURE:
598 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
599 return 0;
600 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
601 if (copy_to_user(buf, data, ret))
602 ret = -EFAULT;
603 break;
604
605 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
606 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
607 return 0;
608 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
609 if (copy_to_user(buf, data, ret))
610 ret = -EFAULT;
611 break;
612
613 case ENVCTRL_RD_FAN_STATUS:
614 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
615 return 0;
616 data[0] = envctrl_i2c_read_8574(pchild->addr);
617 ret = envctrl_i2c_fan_status(pchild,data[0], data);
618 if (copy_to_user(buf, data, ret))
619 ret = -EFAULT;
620 break;
621
622 case ENVCTRL_RD_GLOBALADDRESS:
623 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
624 return 0;
625 data[0] = envctrl_i2c_read_8574(pchild->addr);
626 ret = envctrl_i2c_globaladdr(pchild, data[0], data);
627 if (copy_to_user(buf, data, ret))
628 ret = -EFAULT;
629 break;
630
631 case ENVCTRL_RD_VOLTAGE_STATUS:
632 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_VOLTAGESTAT_MON)))
633 /* If voltage monitor not present, check for CPCI equivalent */
634 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
635 return 0;
636 data[0] = envctrl_i2c_read_8574(pchild->addr);
637 ret = envctrl_i2c_voltage_status(pchild, data[0], data);
638 if (copy_to_user(buf, data, ret))
639 ret = -EFAULT;
640 break;
641
642 default:
643 break;
644
645 }
646
647 return ret;
648}
649
650/* Function Description: Command what to read. Mapped to user ioctl().
651 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
652 */
653static long
654envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
655{
656 char __user *infobuf;
657
658 switch (cmd) {
659 case ENVCTRL_RD_WARNING_TEMPERATURE:
660 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
661 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
662 case ENVCTRL_RD_FAN_STATUS:
663 case ENVCTRL_RD_VOLTAGE_STATUS:
664 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
665 case ENVCTRL_RD_SCSI_TEMPERATURE:
666 case ENVCTRL_RD_GLOBALADDRESS:
667 file->private_data = (void *)(long)cmd;
668 break;
669
670 case ENVCTRL_RD_CPU_TEMPERATURE:
671 case ENVCTRL_RD_CPU_VOLTAGE:
672 /* Check to see if application passes in any cpu number,
673 * the default is cpu0.
674 */
675 infobuf = (char __user *) arg;
676 if (infobuf == NULL) {
677 read_cpu = 0;
678 }else {
679 get_user(read_cpu, infobuf);
680 }
681
682 /* Save the command for use when reading. */
683 file->private_data = (void *)(long)cmd;
684 break;
685
686 default:
687 return -EINVAL;
688 }
689
690 return 0;
691}
692
693/* Function Description: open device. Mapped to user open().
694 * Return: Always 0.
695 */
696static int
697envctrl_open(struct inode *inode, struct file *file)
698{
699 file->private_data = NULL;
700 return 0;
701}
702
703/* Function Description: Open device. Mapped to user close().
704 * Return: Always 0.
705 */
706static int
707envctrl_release(struct inode *inode, struct file *file)
708{
709 return 0;
710}
711
712static const struct file_operations envctrl_fops = {
713 .owner = THIS_MODULE,
714 .read = envctrl_read,
715 .unlocked_ioctl = envctrl_ioctl,
716 .compat_ioctl = compat_ptr_ioctl,
717 .open = envctrl_open,
718 .release = envctrl_release,
719 .llseek = noop_llseek,
720};
721
722static struct miscdevice envctrl_dev = {
723 ENVCTRL_MINOR,
724 "envctrl",
725 &envctrl_fops
726};
727
728/* Function Description: Set monitor type based on firmware description.
729 * Return: None.
730 */
731static void envctrl_set_mon(struct i2c_child_t *pchild,
732 const char *chnl_desc,
733 int chnl_no)
734{
735 /* Firmware only has temperature type. It does not distinguish
736 * different kinds of temperatures. We use channel description
737 * to disinguish them.
738 */
739 if (!(strcmp(chnl_desc,"temp,cpu")) ||
740 !(strcmp(chnl_desc,"temp,cpu0")) ||
741 !(strcmp(chnl_desc,"temp,cpu1")) ||
742 !(strcmp(chnl_desc,"temp,cpu2")) ||
743 !(strcmp(chnl_desc,"temp,cpu3")))
744 pchild->mon_type[chnl_no] = ENVCTRL_CPUTEMP_MON;
745
746 if (!(strcmp(chnl_desc,"vddcore,cpu0")) ||
747 !(strcmp(chnl_desc,"vddcore,cpu1")) ||
748 !(strcmp(chnl_desc,"vddcore,cpu2")) ||
749 !(strcmp(chnl_desc,"vddcore,cpu3")))
750 pchild->mon_type[chnl_no] = ENVCTRL_CPUVOLTAGE_MON;
751
752 if (!(strcmp(chnl_desc,"temp,motherboard")))
753 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
754
755 if (!(strcmp(chnl_desc,"temp,scsi")))
756 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
757
758 if (!(strcmp(chnl_desc,"temp,ethernet")))
759 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
760}
761
762/* Function Description: Initialize monitor channel with channel desc,
763 * decoding tables, monitor type, optional properties.
764 * Return: None.
765 */
766static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
767{
768 int i = 0, len;
769 const char *pos;
770 const unsigned int *pval;
771
772 /* Firmware describe channels into a stream separated by a '\0'. */
773 pos = of_get_property(dp, "channels-description", &len);
774
775 while (len > 0) {
776 int l = strlen(pos) + 1;
777 envctrl_set_mon(pchild, pos, i++);
778 len -= l;
779 pos += l;
780 }
781
782 /* Get optional properties. */
783 pval = of_get_property(dp, "warning-temp", NULL);
784 if (pval)
785 warning_temperature = *pval;
786
787 pval = of_get_property(dp, "shutdown-temp", NULL);
788 if (pval)
789 shutdown_temperature = *pval;
790}
791
792/* Function Description: Initialize child device monitoring fan status.
793 * Return: None.
794 */
795static void envctrl_init_fanstat(struct i2c_child_t *pchild)
796{
797 int i;
798
799 /* Go through all channels and set up the mask. */
800 for (i = 0; i < pchild->total_chnls; i++)
801 pchild->fan_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
802
803 /* We only need to know if this child has fan status monitored.
804 * We don't care which channels since we have the mask already.
805 */
806 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
807}
808
809/* Function Description: Initialize child device for global addressing line.
810 * Return: None.
811 */
812static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
813{
814 int i;
815
816 /* Voltage/PowerSupply monitoring is piggybacked
817 * with Global Address on CompactPCI. See comments
818 * within envctrl_i2c_globaladdr for bit assignments.
819 *
820 * The mask is created here by assigning mask bits to each
821 * bit position that represents PCF8584_VOLTAGE_TYPE data.
822 * Channel numbers are not consecutive within the globaladdr
823 * node (why?), so we use the actual counter value as chnls_mask
824 * index instead of the chnl_array[x].chnl_no value.
825 *
826 * NOTE: This loop could be replaced with a constant representing
827 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
828 */
829 for (i = 0; i < pchild->total_chnls; i++) {
830 if (PCF8584_VOLTAGE_TYPE == pchild->chnl_array[i].type) {
831 pchild->voltage_mask |= chnls_mask[i];
832 }
833 }
834
835 /* We only need to know if this child has global addressing
836 * line monitored. We don't care which channels since we know
837 * the mask already (ENVCTRL_GLOBALADDR_ADDR_MASK).
838 */
839 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
840}
841
842/* Initialize child device monitoring voltage status. */
843static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
844{
845 int i;
846
847 /* Go through all channels and set up the mask. */
848 for (i = 0; i < pchild->total_chnls; i++)
849 pchild->voltage_mask |= chnls_mask[(pchild->chnl_array[i]).chnl_no];
850
851 /* We only need to know if this child has voltage status monitored.
852 * We don't care which channels since we have the mask already.
853 */
854 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
855}
856
857/* Function Description: Initialize i2c child device.
858 * Return: None.
859 */
860static void envctrl_init_i2c_child(struct device_node *dp,
861 struct i2c_child_t *pchild)
862{
863 int len, i, tbls_size = 0;
864 const void *pval;
865
866 /* Get device address. */
867 pval = of_get_property(dp, "reg", &len);
868 memcpy(&pchild->addr, pval, len);
869
870 /* Get tables property. Read firmware temperature tables. */
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(KERN_ERR PFX "Failed to allocate table.\n");
884 return;
885 }
886 pval = of_get_property(dp, "tables", &len);
887 if (!pval || len <= 0) {
888 printk(KERN_ERR PFX "Failed to get table.\n");
889 return;
890 }
891 memcpy(pchild->tables, pval, len);
892 }
893
894 /* SPARCengine ASM Reference Manual (ref. SMI doc 805-7581-04)
895 * sections 2.5, 3.5, 4.5 state node 0x70 for CP1400/1500 is
896 * "For Factory Use Only."
897 *
898 * We ignore the node on these platforms by assigning the
899 * 'NULL' monitor type.
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 (of_node_name_eq(root_node, "SUNW,UltraSPARC-IIi-cEngine")) {
907 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
908 pchild->mon_type[len] = ENVCTRL_NOMON;
909 }
910 of_node_put(root_node);
911 return;
912 }
913 of_node_put(root_node);
914 }
915
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);
920
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);
925 break;
926
927 case PCF8584_GLOBALADDR_TYPE:
928 envctrl_init_globaladdr(pchild);
929 i = pchild->total_chnls;
930 break;
931
932 case PCF8584_FANSTAT_TYPE:
933 envctrl_init_fanstat(pchild);
934 i = pchild->total_chnls;
935 break;
936
937 case PCF8584_VOLTAGE_TYPE:
938 if (pchild->i2ctype == I2C_ADC) {
939 envctrl_init_adc(pchild,dp);
940 } else {
941 envctrl_init_voltage_status(pchild);
942 }
943 i = pchild->total_chnls;
944 break;
945
946 default:
947 break;
948 }
949 }
950}
951
952/* Function Description: Search the child device list for a device.
953 * Return : The i2c child if found. NULL otherwise.
954 */
955static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
956{
957 int i, j;
958
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]));
963 }
964 }
965 }
966 return NULL;
967}
968
969static void envctrl_do_shutdown(void)
970{
971 static int inprog = 0;
972
973 if (inprog != 0)
974 return;
975
976 inprog = 1;
977 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
978 orderly_poweroff(true);
979}
980
981static struct task_struct *kenvctrld_task;
982
983static int kenvctrld(void *__unused)
984{
985 int poll_interval;
986 int whichcpu;
987 char tempbuf[10];
988 struct i2c_child_t *cputemp;
989
990 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
991 printk(KERN_ERR PFX
992 "kenvctrld unable to monitor CPU temp-- exiting\n");
993 return -ENODEV;
994 }
995
996 poll_interval = 5000; /* TODO env_mon_interval */
997
998 printk(KERN_INFO PFX "%s starting...\n", current->comm);
999 for (;;) {
1000 msleep_interruptible(poll_interval);
1001
1002 if (kthread_should_stop())
1003 break;
1004
1005 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1006 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1007 ENVCTRL_CPUTEMP_MON,
1008 tempbuf)) {
1009 if (tempbuf[0] >= shutdown_temperature) {
1010 printk(KERN_CRIT
1011 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1012 "shutdown threshold %i C\n",
1013 current->comm, whichcpu,
1014 tempbuf[0], shutdown_temperature);
1015 envctrl_do_shutdown();
1016 }
1017 }
1018 }
1019 }
1020 printk(KERN_INFO PFX "%s exiting...\n", current->comm);
1021 return 0;
1022}
1023
1024static int envctrl_probe(struct platform_device *op)
1025{
1026 struct device_node *dp;
1027 int index, err;
1028
1029 if (i2c)
1030 return -EINVAL;
1031
1032 i2c = of_ioremap(&op->resource[0], 0, 0x2, DRIVER_NAME);
1033 if (!i2c)
1034 return -ENOMEM;
1035
1036 index = 0;
1037 dp = op->dev.of_node->child;
1038 while (dp) {
1039 if (of_node_name_eq(dp, "gpio")) {
1040 i2c_childlist[index].i2ctype = I2C_GPIO;
1041 envctrl_init_i2c_child(dp, &(i2c_childlist[index++]));
1042 } else if (of_node_name_eq(dp, "adc")) {
1043 i2c_childlist[index].i2ctype = I2C_ADC;
1044 envctrl_init_i2c_child(dp, &(i2c_childlist[index++]));
1045 }
1046
1047 dp = dp->sibling;
1048 }
1049
1050 /* Set device address. */
1051 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1052 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1053
1054 /* Set system clock and SCL frequencies. */
1055 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1056 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1057
1058 /* Enable serial interface. */
1059 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1060 udelay(200);
1061
1062 /* Register the device as a minor miscellaneous device. */
1063 err = misc_register(&envctrl_dev);
1064 if (err) {
1065 printk(KERN_ERR PFX "Unable to get misc minor %d\n",
1066 envctrl_dev.minor);
1067 goto out_iounmap;
1068 }
1069
1070 /* Note above traversal routine post-incremented 'i' to accommodate
1071 * a next child device, so we decrement before reverse-traversal of
1072 * child devices.
1073 */
1074 printk(KERN_INFO PFX "Initialized ");
1075 for (--index; index >= 0; --index) {
1076 printk("[%s 0x%lx]%s",
1077 (I2C_ADC == i2c_childlist[index].i2ctype) ? "adc" :
1078 ((I2C_GPIO == i2c_childlist[index].i2ctype) ? "gpio" : "unknown"),
1079 i2c_childlist[index].addr, (0 == index) ? "\n" : " ");
1080 }
1081
1082 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1083 if (IS_ERR(kenvctrld_task)) {
1084 err = PTR_ERR(kenvctrld_task);
1085 goto out_deregister;
1086 }
1087
1088 return 0;
1089
1090out_deregister:
1091 misc_deregister(&envctrl_dev);
1092out_iounmap:
1093 of_iounmap(&op->resource[0], i2c, 0x2);
1094 for (index = 0; index < ENVCTRL_MAX_CPU * 2; index++)
1095 kfree(i2c_childlist[index].tables);
1096
1097 return err;
1098}
1099
1100static int envctrl_remove(struct platform_device *op)
1101{
1102 int index;
1103
1104 kthread_stop(kenvctrld_task);
1105
1106 of_iounmap(&op->resource[0], i2c, 0x2);
1107 misc_deregister(&envctrl_dev);
1108
1109 for (index = 0; index < ENVCTRL_MAX_CPU * 2; index++)
1110 kfree(i2c_childlist[index].tables);
1111
1112 return 0;
1113}
1114
1115static const struct of_device_id envctrl_match[] = {
1116 {
1117 .name = "i2c",
1118 .compatible = "i2cpcf,8584",
1119 },
1120 {},
1121};
1122MODULE_DEVICE_TABLE(of, envctrl_match);
1123
1124static struct platform_driver envctrl_driver = {
1125 .driver = {
1126 .name = DRIVER_NAME,
1127 .of_match_table = envctrl_match,
1128 },
1129 .probe = envctrl_probe,
1130 .remove = envctrl_remove,
1131};
1132
1133module_platform_driver(envctrl_driver);
1134
1135MODULE_LICENSE("GPL");
1/* envctrl.c: Temperature and Fan monitoring on Machines providing it.
2 *
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
13 *
14 * EB - Added support for CP1500 Global Address and PS/Voltage monitoring.
15 * Eric Brower <ebrower@usa.net>
16 *
17 * DB - Audit every copy_to_user in envctrl_read.
18 * Daniele Bellucci <bellucda@tiscali.it>
19 */
20
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>
29#include <linux/of.h>
30#include <linux/of_device.h>
31
32#include <linux/uaccess.h>
33#include <asm/envctrl.h>
34#include <asm/io.h>
35
36#define DRIVER_NAME "envctrl"
37#define PFX DRIVER_NAME ": "
38
39#define ENVCTRL_MINOR 162
40
41#define PCF8584_ADDRESS 0x55
42
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
51
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
60
61/*
62 * CLK Mode Register.
63 */
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
68
69#define CLK_3 0x00
70#define CLK_4_43 0x10
71#define CLK_6 0x14
72#define CLK_8 0x18
73#define CLK_12 0x1c
74
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 */
77
78/* Monitor type of i2c child device.
79 * Firmware definitions.
80 */
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*/
86
87/* Monitor type of i2c child device.
88 * Driver definitions.
89 */
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 */
95 /* monitor */
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 */
100
101/* Child device type.
102 * Driver definitions.
103 */
104#define I2C_ADC 0 /* pcf8591 */
105#define I2C_GPIO 1 /* pcf8571 */
106
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.
110 */
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 */
116
117/* Driver miscellaneous definitions. */
118#define ENVCTRL_MAX_CPU 4
119#define CHANNEL_DESC_SZ 256
120
121/* Mask values for combined GlobalAddress/PowerStatus node */
122#define ENVCTRL_GLOBALADDR_ADDR_MASK 0x1F
123#define ENVCTRL_GLOBALADDR_PSTAT_MASK 0x60
124
125/* Node 0x70 ignored on CompactPCI CP1400/1500 platforms
126 * (see envctrl_init_i2c_child)
127 */
128#define ENVCTRL_CPCI_IGNORED_NODE 0x70
129
130#define PCF8584_DATA 0x00
131#define PCF8584_CSR 0x01
132
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.
135 */
136struct pcf8584_channel {
137 unsigned char chnl_no;
138 unsigned char io_direction;
139 unsigned char type;
140 unsigned char last;
141};
142
143/* Each child device may have one or more tables of bytes to help decode
144 * data. Table property as defined by the firmware.
145 */
146struct pcf8584_tblprop {
147 unsigned int type;
148 unsigned int scale;
149 unsigned int offset; /* offset from the beginning of the table */
150 unsigned int size;
151};
152
153/* i2c child */
154struct i2c_child_t {
155 /* Either ADC or GPIO. */
156 unsigned char i2ctype;
157 unsigned long addr;
158 struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];
159
160 /* Channel info. */
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];
165
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];
171};
172
173static void __iomem *i2c;
174static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];
175static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
176static unsigned int warning_temperature = 0;
177static unsigned int shutdown_temperature = 0;
178static char read_cpu;
179
180/* Forward declarations. */
181static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);
182
183/* Function Description: Test the PIN bit (Pending Interrupt Not)
184 * to test when serial transmission is completed .
185 * Return : None.
186 */
187static void envtrl_i2c_test_pin(void)
188{
189 int limit = 1000000;
190
191 while (--limit > 0) {
192 if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN))
193 break;
194 udelay(1);
195 }
196
197 if (limit <= 0)
198 printk(KERN_INFO PFX "Pin status will not clear.\n");
199}
200
201/* Function Description: Test busy bit.
202 * Return : None.
203 */
204static void envctrl_i2c_test_bb(void)
205{
206 int limit = 1000000;
207
208 while (--limit > 0) {
209 /* Busy bit 0 means busy. */
210 if (readb(i2c + PCF8584_CSR) & STATUS_BB)
211 break;
212 udelay(1);
213 }
214
215 if (limit <= 0)
216 printk(KERN_INFO PFX "Busy bit will not clear.\n");
217}
218
219/* Function Description: Send the address for a read access.
220 * Return : 0 if not acknowledged, otherwise acknowledged.
221 */
222static int envctrl_i2c_read_addr(unsigned char addr)
223{
224 envctrl_i2c_test_bb();
225
226 /* Load address. */
227 writeb(addr + 1, i2c + PCF8584_DATA);
228
229 envctrl_i2c_test_bb();
230
231 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
232
233 /* Wait for PIN. */
234 envtrl_i2c_test_pin();
235
236 /* CSR 0 means acknowledged. */
237 if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {
238 return readb(i2c + PCF8584_DATA);
239 } else {
240 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
241 return 0;
242 }
243}
244
245/* Function Description: Send the address for write mode.
246 * Return : None.
247 */
248static void envctrl_i2c_write_addr(unsigned char addr)
249{
250 envctrl_i2c_test_bb();
251 writeb(addr, i2c + PCF8584_DATA);
252
253 /* Generate Start condition. */
254 writeb(OBD_SEND_START, i2c + PCF8584_CSR);
255}
256
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().
260 */
261static unsigned char envctrl_i2c_read_data(void)
262{
263 envtrl_i2c_test_pin();
264 writeb(CONTROL_ES0, i2c + PCF8584_CSR); /* Send neg ack. */
265 return readb(i2c + PCF8584_DATA);
266}
267
268/* Function Description: Instruct the device which port to read data from.
269 * Return : None.
270 */
271static void envctrl_i2c_write_data(unsigned char port)
272{
273 envtrl_i2c_test_pin();
274 writeb(port, i2c + PCF8584_DATA);
275}
276
277/* Function Description: Generate Stop condition after last byte is sent.
278 * Return : None.
279 */
280static void envctrl_i2c_stop(void)
281{
282 envtrl_i2c_test_pin();
283 writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);
284}
285
286/* Function Description: Read adc device.
287 * Return : Data at address and port.
288 */
289static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port)
290{
291 /* Send address. */
292 envctrl_i2c_write_addr(addr);
293
294 /* Setup port to read. */
295 envctrl_i2c_write_data(port);
296 envctrl_i2c_stop();
297
298 /* Read port. */
299 envctrl_i2c_read_addr(addr);
300
301 /* Do a single byte read and send stop. */
302 envctrl_i2c_read_data();
303 envctrl_i2c_stop();
304
305 return readb(i2c + PCF8584_DATA);
306}
307
308/* Function Description: Read gpio device.
309 * Return : Data at address.
310 */
311static unsigned char envctrl_i2c_read_8574(unsigned char addr)
312{
313 unsigned char rd;
314
315 envctrl_i2c_read_addr(addr);
316
317 /* Do a single byte read and send stop. */
318 rd = envctrl_i2c_read_data();
319 envctrl_i2c_stop();
320 return rd;
321}
322
323/* Function Description: Decode data read from an adc device using firmware
324 * table.
325 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
326 */
327static int envctrl_i2c_data_translate(unsigned char data, int translate_type,
328 int scale, char *tbl, char *bufdata)
329{
330 int len = 0;
331
332 switch (translate_type) {
333 case ENVCTRL_TRANSLATE_NO:
334 /* No decode necessary. */
335 len = 1;
336 bufdata[0] = data;
337 break;
338
339 case ENVCTRL_TRANSLATE_FULL:
340 /* Decode this way: data = table[data]. */
341 len = 1;
342 bufdata[0] = tbl[data];
343 break;
344
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] = '.';
351 break;
352
353 default:
354 break;
355 }
356
357 return len;
358}
359
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.
362 */
363static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,
364 char mon_type, unsigned char *bufdata)
365{
366 unsigned char data;
367 int i;
368 char *tbl, j = -1;
369
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) {
373 if (++j == cpu) {
374 break;
375 }
376 }
377 }
378
379 if (j != cpu)
380 return 0;
381
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);
385
386 /* Find decoding table. */
387 tbl = pchild->tables + pchild->tblprop_array[i].offset;
388
389 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
390 pchild->tblprop_array[i].scale,
391 tbl, bufdata);
392}
393
394/* Function Description: Read noncpu-related data such as motherboard
395 * temperature.
396 * Return: Number of read bytes. Data is stored in bufdata in ascii format.
397 */
398static int envctrl_read_noncpu_info(struct i2c_child_t *pchild,
399 char mon_type, unsigned char *bufdata)
400{
401 unsigned char data;
402 int i;
403 char *tbl = NULL;
404
405 for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {
406 if (pchild->mon_type[i] == mon_type)
407 break;
408 }
409
410 if (i >= PCF8584_MAX_CHANNELS)
411 return 0;
412
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);
416
417 /* Find decoding table. */
418 tbl = pchild->tables + pchild->tblprop_array[i].offset;
419
420 return envctrl_i2c_data_translate(data, pchild->tblprop_array[i].type,
421 pchild->tblprop_array[i].scale,
422 tbl, bufdata);
423}
424
425/* Function Description: Read fan status.
426 * Return : Always 1 byte. Status stored in bufdata.
427 */
428static int envctrl_i2c_fan_status(struct i2c_child_t *pchild,
429 unsigned char data,
430 char *bufdata)
431{
432 unsigned char tmp, ret = 0;
433 int i, j = 0;
434
435 tmp = data & pchild->fan_mask;
436
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;
443 } else {
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.
449 */
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];
454
455 j++;
456 }
457 }
458 }
459
460 bufdata[0] = ret;
461 return 1;
462}
463
464/* Function Description: Read global addressing line.
465 * Return : Always 1 byte. Status stored in bufdata.
466 */
467static int envctrl_i2c_globaladdr(struct i2c_child_t *pchild,
468 unsigned char data,
469 char *bufdata)
470{
471 /* Translatation table is not necessary, as global
472 * addr is the integer value of the GA# bits.
473 *
474 * NOTE: MSB is documented as zero, but I see it as '1' always....
475 *
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
484 */
485 bufdata[0] = (data & ENVCTRL_GLOBALADDR_ADDR_MASK);
486 return 1;
487}
488
489/* Function Description: Read standard voltage and power supply status.
490 * Return : Always 1 byte. Status stored in bufdata.
491 */
492static unsigned char envctrl_i2c_voltage_status(struct i2c_child_t *pchild,
493 unsigned char data,
494 char *bufdata)
495{
496 unsigned char tmp, ret = 0;
497 int i, j = 0;
498
499 tmp = data & pchild->voltage_mask;
500
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;
508 } else {
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]) {
512 j++;
513
514 /* Break out when there is a mismatch. */
515 if (!(chnls_mask[i] & tmp))
516 break;
517 }
518 }
519
520 /* Make a wish that hardware will always use the
521 * first channel for voltage and the second for
522 * power supply.
523 */
524 if (j == 1)
525 ret = ENVCTRL_VOLTAGE_BAD;
526 else
527 ret = ENVCTRL_POWERSUPPLY_BAD;
528 }
529
530 bufdata[0] = ret;
531 return 1;
532}
533
534/* Function Description: Read a byte from /dev/envctrl. Mapped to user read().
535 * Return: Number of read bytes. 0 for error.
536 */
537static ssize_t
538envctrl_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
539{
540 struct i2c_child_t *pchild;
541 unsigned char data[10];
542 int ret = 0;
543
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.
547 */
548
549 switch ((int)(long)file->private_data) {
550 case ENVCTRL_RD_WARNING_TEMPERATURE:
551 if (warning_temperature == 0)
552 return 0;
553
554 data[0] = (unsigned char)(warning_temperature);
555 ret = 1;
556 if (copy_to_user(buf, data, ret))
557 ret = -EFAULT;
558 break;
559
560 case ENVCTRL_RD_SHUTDOWN_TEMPERATURE:
561 if (shutdown_temperature == 0)
562 return 0;
563
564 data[0] = (unsigned char)(shutdown_temperature);
565 ret = 1;
566 if (copy_to_user(buf, data, ret))
567 ret = -EFAULT;
568 break;
569
570 case ENVCTRL_RD_MTHRBD_TEMPERATURE:
571 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_MTHRBDTEMP_MON)))
572 return 0;
573 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_MTHRBDTEMP_MON, data);
574 if (copy_to_user(buf, data, ret))
575 ret = -EFAULT;
576 break;
577
578 case ENVCTRL_RD_CPU_TEMPERATURE:
579 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON)))
580 return 0;
581 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUTEMP_MON, data);
582
583 /* Reset cpu to the default cpu0. */
584 if (copy_to_user(buf, data, ret))
585 ret = -EFAULT;
586 break;
587
588 case ENVCTRL_RD_CPU_VOLTAGE:
589 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_CPUVOLTAGE_MON)))
590 return 0;
591 ret = envctrl_read_cpu_info(read_cpu, pchild, ENVCTRL_CPUVOLTAGE_MON, data);
592
593 /* Reset cpu to the default cpu0. */
594 if (copy_to_user(buf, data, ret))
595 ret = -EFAULT;
596 break;
597
598 case ENVCTRL_RD_SCSI_TEMPERATURE:
599 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_SCSITEMP_MON)))
600 return 0;
601 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_SCSITEMP_MON, data);
602 if (copy_to_user(buf, data, ret))
603 ret = -EFAULT;
604 break;
605
606 case ENVCTRL_RD_ETHERNET_TEMPERATURE:
607 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_ETHERTEMP_MON)))
608 return 0;
609 ret = envctrl_read_noncpu_info(pchild, ENVCTRL_ETHERTEMP_MON, data);
610 if (copy_to_user(buf, data, ret))
611 ret = -EFAULT;
612 break;
613
614 case ENVCTRL_RD_FAN_STATUS:
615 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_FANSTAT_MON)))
616 return 0;
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))
620 ret = -EFAULT;
621 break;
622
623 case ENVCTRL_RD_GLOBALADDRESS:
624 if (!(pchild = envctrl_get_i2c_child(ENVCTRL_GLOBALADDR_MON)))
625 return 0;
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))
629 ret = -EFAULT;
630 break;
631
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)))
636 return 0;
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))
640 ret = -EFAULT;
641 break;
642
643 default:
644 break;
645
646 }
647
648 return ret;
649}
650
651/* Function Description: Command what to read. Mapped to user ioctl().
652 * Return: Gives 0 for implemented commands, -EINVAL otherwise.
653 */
654static long
655envctrl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
656{
657 char __user *infobuf;
658
659 switch (cmd) {
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;
669 break;
670
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.
675 */
676 infobuf = (char __user *) arg;
677 if (infobuf == NULL) {
678 read_cpu = 0;
679 }else {
680 get_user(read_cpu, infobuf);
681 }
682
683 /* Save the command for use when reading. */
684 file->private_data = (void *)(long)cmd;
685 break;
686
687 default:
688 return -EINVAL;
689 }
690
691 return 0;
692}
693
694/* Function Description: open device. Mapped to user open().
695 * Return: Always 0.
696 */
697static int
698envctrl_open(struct inode *inode, struct file *file)
699{
700 file->private_data = NULL;
701 return 0;
702}
703
704/* Function Description: Open device. Mapped to user close().
705 * Return: Always 0.
706 */
707static int
708envctrl_release(struct inode *inode, struct file *file)
709{
710 return 0;
711}
712
713static const struct file_operations envctrl_fops = {
714 .owner = THIS_MODULE,
715 .read = envctrl_read,
716 .unlocked_ioctl = envctrl_ioctl,
717#ifdef CONFIG_COMPAT
718 .compat_ioctl = envctrl_ioctl,
719#endif
720 .open = envctrl_open,
721 .release = envctrl_release,
722 .llseek = noop_llseek,
723};
724
725static struct miscdevice envctrl_dev = {
726 ENVCTRL_MINOR,
727 "envctrl",
728 &envctrl_fops
729};
730
731/* Function Description: Set monitor type based on firmware description.
732 * Return: None.
733 */
734static void envctrl_set_mon(struct i2c_child_t *pchild,
735 const char *chnl_desc,
736 int chnl_no)
737{
738 /* Firmware only has temperature type. It does not distinguish
739 * different kinds of temperatures. We use channel description
740 * to disinguish them.
741 */
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;
748
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;
754
755 if (!(strcmp(chnl_desc,"temp,motherboard")))
756 pchild->mon_type[chnl_no] = ENVCTRL_MTHRBDTEMP_MON;
757
758 if (!(strcmp(chnl_desc,"temp,scsi")))
759 pchild->mon_type[chnl_no] = ENVCTRL_SCSITEMP_MON;
760
761 if (!(strcmp(chnl_desc,"temp,ethernet")))
762 pchild->mon_type[chnl_no] = ENVCTRL_ETHERTEMP_MON;
763}
764
765/* Function Description: Initialize monitor channel with channel desc,
766 * decoding tables, monitor type, optional properties.
767 * Return: None.
768 */
769static void envctrl_init_adc(struct i2c_child_t *pchild, struct device_node *dp)
770{
771 int i = 0, len;
772 const char *pos;
773 const unsigned int *pval;
774
775 /* Firmware describe channels into a stream separated by a '\0'. */
776 pos = of_get_property(dp, "channels-description", &len);
777
778 while (len > 0) {
779 int l = strlen(pos) + 1;
780 envctrl_set_mon(pchild, pos, i++);
781 len -= l;
782 pos += l;
783 }
784
785 /* Get optional properties. */
786 pval = of_get_property(dp, "warning-temp", NULL);
787 if (pval)
788 warning_temperature = *pval;
789
790 pval = of_get_property(dp, "shutdown-temp", NULL);
791 if (pval)
792 shutdown_temperature = *pval;
793}
794
795/* Function Description: Initialize child device monitoring fan status.
796 * Return: None.
797 */
798static void envctrl_init_fanstat(struct i2c_child_t *pchild)
799{
800 int i;
801
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];
805
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.
808 */
809 pchild->mon_type[0] = ENVCTRL_FANSTAT_MON;
810}
811
812/* Function Description: Initialize child device for global addressing line.
813 * Return: None.
814 */
815static void envctrl_init_globaladdr(struct i2c_child_t *pchild)
816{
817 int i;
818
819 /* Voltage/PowerSupply monitoring is piggybacked
820 * with Global Address on CompactPCI. See comments
821 * within envctrl_i2c_globaladdr for bit assignments.
822 *
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.
828 *
829 * NOTE: This loop could be replaced with a constant representing
830 * a mask of bits 5&6 (ENVCTRL_GLOBALADDR_PSTAT_MASK).
831 */
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];
835 }
836 }
837
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).
841 */
842 pchild->mon_type[0] = ENVCTRL_GLOBALADDR_MON;
843}
844
845/* Initialize child device monitoring voltage status. */
846static void envctrl_init_voltage_status(struct i2c_child_t *pchild)
847{
848 int i;
849
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];
853
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.
856 */
857 pchild->mon_type[0] = ENVCTRL_VOLTAGESTAT_MON;
858}
859
860/* Function Description: Initialize i2c child device.
861 * Return: None.
862 */
863static void envctrl_init_i2c_child(struct device_node *dp,
864 struct i2c_child_t *pchild)
865{
866 int len, i, tbls_size = 0;
867 const void *pval;
868
869 /* Get device address. */
870 pval = of_get_property(dp, "reg", &len);
871 memcpy(&pchild->addr, pval, len);
872
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;
881 }
882 }
883
884 pchild->tables = kmalloc(tbls_size, GFP_KERNEL);
885 if (pchild->tables == NULL){
886 printk(KERN_ERR PFX "Failed to allocate table.\n");
887 return;
888 }
889 pval = of_get_property(dp, "tables", &len);
890 if (!pval || len <= 0) {
891 printk(KERN_ERR PFX "Failed to get table.\n");
892 return;
893 }
894 memcpy(pchild->tables, pval, len);
895 }
896
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."
900 *
901 * We ignore the node on these platforms by assigning the
902 * 'NULL' monitor type.
903 */
904 if (ENVCTRL_CPCI_IGNORED_NODE == pchild->addr) {
905 struct device_node *root_node;
906 int len;
907
908 root_node = of_find_node_by_path("/");
909 if (!strcmp(root_node->name, "SUNW,UltraSPARC-IIi-cEngine")) {
910 for (len = 0; len < PCF8584_MAX_CHANNELS; ++len) {
911 pchild->mon_type[len] = ENVCTRL_NOMON;
912 }
913 return;
914 }
915 }
916
917 /* Get the monitor channels. */
918 pval = of_get_property(dp, "channels-in-use", &len);
919 memcpy(pchild->chnl_array, pval, len);
920 pchild->total_chnls = len / sizeof(struct pcf8584_channel);
921
922 for (i = 0; i < pchild->total_chnls; i++) {
923 switch (pchild->chnl_array[i].type) {
924 case PCF8584_TEMP_TYPE:
925 envctrl_init_adc(pchild, dp);
926 break;
927
928 case PCF8584_GLOBALADDR_TYPE:
929 envctrl_init_globaladdr(pchild);
930 i = pchild->total_chnls;
931 break;
932
933 case PCF8584_FANSTAT_TYPE:
934 envctrl_init_fanstat(pchild);
935 i = pchild->total_chnls;
936 break;
937
938 case PCF8584_VOLTAGE_TYPE:
939 if (pchild->i2ctype == I2C_ADC) {
940 envctrl_init_adc(pchild,dp);
941 } else {
942 envctrl_init_voltage_status(pchild);
943 }
944 i = pchild->total_chnls;
945 break;
946
947 default:
948 break;
949 }
950 }
951}
952
953/* Function Description: Search the child device list for a device.
954 * Return : The i2c child if found. NULL otherwise.
955 */
956static struct i2c_child_t *envctrl_get_i2c_child(unsigned char mon_type)
957{
958 int i, j;
959
960 for (i = 0; i < ENVCTRL_MAX_CPU*2; i++) {
961 for (j = 0; j < PCF8584_MAX_CHANNELS; j++) {
962 if (i2c_childlist[i].mon_type[j] == mon_type) {
963 return (struct i2c_child_t *)(&(i2c_childlist[i]));
964 }
965 }
966 }
967 return NULL;
968}
969
970static void envctrl_do_shutdown(void)
971{
972 static int inprog = 0;
973
974 if (inprog != 0)
975 return;
976
977 inprog = 1;
978 printk(KERN_CRIT "kenvctrld: WARNING: Shutting down the system now.\n");
979 orderly_poweroff(true);
980}
981
982static struct task_struct *kenvctrld_task;
983
984static int kenvctrld(void *__unused)
985{
986 int poll_interval;
987 int whichcpu;
988 char tempbuf[10];
989 struct i2c_child_t *cputemp;
990
991 if (NULL == (cputemp = envctrl_get_i2c_child(ENVCTRL_CPUTEMP_MON))) {
992 printk(KERN_ERR PFX
993 "kenvctrld unable to monitor CPU temp-- exiting\n");
994 return -ENODEV;
995 }
996
997 poll_interval = 5000; /* TODO env_mon_interval */
998
999 printk(KERN_INFO PFX "%s starting...\n", current->comm);
1000 for (;;) {
1001 msleep_interruptible(poll_interval);
1002
1003 if (kthread_should_stop())
1004 break;
1005
1006 for (whichcpu = 0; whichcpu < ENVCTRL_MAX_CPU; ++whichcpu) {
1007 if (0 < envctrl_read_cpu_info(whichcpu, cputemp,
1008 ENVCTRL_CPUTEMP_MON,
1009 tempbuf)) {
1010 if (tempbuf[0] >= shutdown_temperature) {
1011 printk(KERN_CRIT
1012 "%s: WARNING: CPU%i temperature %i C meets or exceeds "\
1013 "shutdown threshold %i C\n",
1014 current->comm, whichcpu,
1015 tempbuf[0], shutdown_temperature);
1016 envctrl_do_shutdown();
1017 }
1018 }
1019 }
1020 }
1021 printk(KERN_INFO PFX "%s exiting...\n", current->comm);
1022 return 0;
1023}
1024
1025static int envctrl_probe(struct platform_device *op)
1026{
1027 struct device_node *dp;
1028 int index, err;
1029
1030 if (i2c)
1031 return -EINVAL;
1032
1033 i2c = of_ioremap(&op->resource[0], 0, 0x2, DRIVER_NAME);
1034 if (!i2c)
1035 return -ENOMEM;
1036
1037 index = 0;
1038 dp = op->dev.of_node->child;
1039 while (dp) {
1040 if (!strcmp(dp->name, "gpio")) {
1041 i2c_childlist[index].i2ctype = I2C_GPIO;
1042 envctrl_init_i2c_child(dp, &(i2c_childlist[index++]));
1043 } else if (!strcmp(dp->name, "adc")) {
1044 i2c_childlist[index].i2ctype = I2C_ADC;
1045 envctrl_init_i2c_child(dp, &(i2c_childlist[index++]));
1046 }
1047
1048 dp = dp->sibling;
1049 }
1050
1051 /* Set device address. */
1052 writeb(CONTROL_PIN, i2c + PCF8584_CSR);
1053 writeb(PCF8584_ADDRESS, i2c + PCF8584_DATA);
1054
1055 /* Set system clock and SCL frequencies. */
1056 writeb(CONTROL_PIN | CONTROL_ES1, i2c + PCF8584_CSR);
1057 writeb(CLK_4_43 | BUS_CLK_90, i2c + PCF8584_DATA);
1058
1059 /* Enable serial interface. */
1060 writeb(CONTROL_PIN | CONTROL_ES0 | CONTROL_ACK, i2c + PCF8584_CSR);
1061 udelay(200);
1062
1063 /* Register the device as a minor miscellaneous device. */
1064 err = misc_register(&envctrl_dev);
1065 if (err) {
1066 printk(KERN_ERR PFX "Unable to get misc minor %d\n",
1067 envctrl_dev.minor);
1068 goto out_iounmap;
1069 }
1070
1071 /* Note above traversal routine post-incremented 'i' to accommodate
1072 * a next child device, so we decrement before reverse-traversal of
1073 * child devices.
1074 */
1075 printk(KERN_INFO PFX "Initialized ");
1076 for (--index; index >= 0; --index) {
1077 printk("[%s 0x%lx]%s",
1078 (I2C_ADC == i2c_childlist[index].i2ctype) ? "adc" :
1079 ((I2C_GPIO == i2c_childlist[index].i2ctype) ? "gpio" : "unknown"),
1080 i2c_childlist[index].addr, (0 == index) ? "\n" : " ");
1081 }
1082
1083 kenvctrld_task = kthread_run(kenvctrld, NULL, "kenvctrld");
1084 if (IS_ERR(kenvctrld_task)) {
1085 err = PTR_ERR(kenvctrld_task);
1086 goto out_deregister;
1087 }
1088
1089 return 0;
1090
1091out_deregister:
1092 misc_deregister(&envctrl_dev);
1093out_iounmap:
1094 of_iounmap(&op->resource[0], i2c, 0x2);
1095 for (index = 0; index < ENVCTRL_MAX_CPU * 2; index++)
1096 kfree(i2c_childlist[index].tables);
1097
1098 return err;
1099}
1100
1101static int envctrl_remove(struct platform_device *op)
1102{
1103 int index;
1104
1105 kthread_stop(kenvctrld_task);
1106
1107 of_iounmap(&op->resource[0], i2c, 0x2);
1108 misc_deregister(&envctrl_dev);
1109
1110 for (index = 0; index < ENVCTRL_MAX_CPU * 2; index++)
1111 kfree(i2c_childlist[index].tables);
1112
1113 return 0;
1114}
1115
1116static const struct of_device_id envctrl_match[] = {
1117 {
1118 .name = "i2c",
1119 .compatible = "i2cpcf,8584",
1120 },
1121 {},
1122};
1123MODULE_DEVICE_TABLE(of, envctrl_match);
1124
1125static struct platform_driver envctrl_driver = {
1126 .driver = {
1127 .name = DRIVER_NAME,
1128 .of_match_table = envctrl_match,
1129 },
1130 .probe = envctrl_probe,
1131 .remove = envctrl_remove,
1132};
1133
1134module_platform_driver(envctrl_driver);
1135
1136MODULE_LICENSE("GPL");