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