Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | it87.c - Part of lm_sensors, Linux kernel modules for hardware |
| 3 | monitoring. |
| 4 | |
Jean Delvare | 9174999 | 2005-10-08 00:10:00 +0200 | [diff] [blame] | 5 | Supports: IT8705F Super I/O chip w/LPC interface |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 6 | IT8712F Super I/O chip w/LPC interface |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 7 | IT8716F Super I/O chip w/LPC interface |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 8 | IT8718F Super I/O chip w/LPC interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | Sis950 A clone of the IT8705F |
| 10 | |
| 11 | Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com> |
Jean Delvare | b19367c | 2006-08-28 14:39:26 +0200 | [diff] [blame] | 12 | Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or modify |
| 15 | it under the terms of the GNU General Public License as published by |
| 16 | the Free Software Foundation; either version 2 of the License, or |
| 17 | (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | */ |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/jiffies.h> |
| 33 | #include <linux/i2c.h> |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 34 | #include <linux/i2c-isa.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 35 | #include <linux/hwmon.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 36 | #include <linux/hwmon-sysfs.h> |
| 37 | #include <linux/hwmon-vid.h> |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 38 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 40 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/io.h> |
| 42 | |
| 43 | |
Jean Delvare | 9174999 | 2005-10-08 00:10:00 +0200 | [diff] [blame] | 44 | static unsigned short isa_address; |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 45 | enum chips { it87, it8712, it8716, it8718 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #define REG 0x2e /* The register to read/write */ |
| 48 | #define DEV 0x07 /* Register: Logical device select */ |
| 49 | #define VAL 0x2f /* The value to read/write */ |
| 50 | #define PME 0x04 /* The device with the fan registers in it */ |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 51 | #define GPIO 0x07 /* The device with the IT8718F VID value in it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #define DEVID 0x20 /* Register: Device ID */ |
| 53 | #define DEVREV 0x22 /* Register: Device Revision */ |
| 54 | |
| 55 | static inline int |
| 56 | superio_inb(int reg) |
| 57 | { |
| 58 | outb(reg, REG); |
| 59 | return inb(VAL); |
| 60 | } |
| 61 | |
| 62 | static int superio_inw(int reg) |
| 63 | { |
| 64 | int val; |
| 65 | outb(reg++, REG); |
| 66 | val = inb(VAL) << 8; |
| 67 | outb(reg, REG); |
| 68 | val |= inb(VAL); |
| 69 | return val; |
| 70 | } |
| 71 | |
| 72 | static inline void |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 73 | superio_select(int ldn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
| 75 | outb(DEV, REG); |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 76 | outb(ldn, VAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static inline void |
| 80 | superio_enter(void) |
| 81 | { |
| 82 | outb(0x87, REG); |
| 83 | outb(0x01, REG); |
| 84 | outb(0x55, REG); |
| 85 | outb(0x55, REG); |
| 86 | } |
| 87 | |
| 88 | static inline void |
| 89 | superio_exit(void) |
| 90 | { |
| 91 | outb(0x02, REG); |
| 92 | outb(0x02, VAL); |
| 93 | } |
| 94 | |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 95 | /* Logical device 4 registers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | #define IT8712F_DEVID 0x8712 |
| 97 | #define IT8705F_DEVID 0x8705 |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 98 | #define IT8716F_DEVID 0x8716 |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 99 | #define IT8718F_DEVID 0x8718 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | #define IT87_ACT_REG 0x30 |
| 101 | #define IT87_BASE_REG 0x60 |
| 102 | |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 103 | /* Logical device 7 registers (IT8712F and later) */ |
| 104 | #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */ |
| 105 | #define IT87_SIO_VID_REG 0xfc /* VID value */ |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* Update battery voltage after every reading if true */ |
| 108 | static int update_vbat; |
| 109 | |
| 110 | /* Not all BIOSes properly configure the PWM registers */ |
| 111 | static int fix_pwm_polarity; |
| 112 | |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 113 | /* Values read from Super-I/O config space */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | static u16 chip_type; |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 115 | static u8 vid_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | /* Many IT87 constants specified below */ |
| 118 | |
| 119 | /* Length of ISA address segment */ |
| 120 | #define IT87_EXTENT 8 |
| 121 | |
| 122 | /* Where are the ISA address/data registers relative to the base address */ |
| 123 | #define IT87_ADDR_REG_OFFSET 5 |
| 124 | #define IT87_DATA_REG_OFFSET 6 |
| 125 | |
| 126 | /*----- The IT87 registers -----*/ |
| 127 | |
| 128 | #define IT87_REG_CONFIG 0x00 |
| 129 | |
| 130 | #define IT87_REG_ALARM1 0x01 |
| 131 | #define IT87_REG_ALARM2 0x02 |
| 132 | #define IT87_REG_ALARM3 0x03 |
| 133 | |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 134 | /* The IT8718F has the VID value in a different register, in Super-I/O |
| 135 | configuration space. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | #define IT87_REG_VID 0x0a |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 137 | /* Warning: register 0x0b is used for something completely different in |
| 138 | new chips/revisions. I suspect only 16-bit tachometer mode will work |
| 139 | for these. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | #define IT87_REG_FAN_DIV 0x0b |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 141 | #define IT87_REG_FAN_16BIT 0x0c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */ |
| 144 | |
| 145 | #define IT87_REG_FAN(nr) (0x0d + (nr)) |
| 146 | #define IT87_REG_FAN_MIN(nr) (0x10 + (nr)) |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 147 | #define IT87_REG_FANX(nr) (0x18 + (nr)) |
| 148 | #define IT87_REG_FANX_MIN(nr) (0x1b + (nr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | #define IT87_REG_FAN_MAIN_CTRL 0x13 |
| 150 | #define IT87_REG_FAN_CTL 0x14 |
| 151 | #define IT87_REG_PWM(nr) (0x15 + (nr)) |
| 152 | |
| 153 | #define IT87_REG_VIN(nr) (0x20 + (nr)) |
| 154 | #define IT87_REG_TEMP(nr) (0x29 + (nr)) |
| 155 | |
| 156 | #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2) |
| 157 | #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2) |
| 158 | #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2) |
| 159 | #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2) |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | #define IT87_REG_VIN_ENABLE 0x50 |
| 162 | #define IT87_REG_TEMP_ENABLE 0x51 |
| 163 | |
| 164 | #define IT87_REG_CHIPID 0x58 |
| 165 | |
| 166 | #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255)) |
| 167 | #define IN_FROM_REG(val) ((val) * 16) |
| 168 | |
| 169 | static inline u8 FAN_TO_REG(long rpm, int div) |
| 170 | { |
| 171 | if (rpm == 0) |
| 172 | return 255; |
| 173 | rpm = SENSORS_LIMIT(rpm, 1, 1000000); |
| 174 | return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, |
| 175 | 254); |
| 176 | } |
| 177 | |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 178 | static inline u16 FAN16_TO_REG(long rpm) |
| 179 | { |
| 180 | if (rpm == 0) |
| 181 | return 0xffff; |
| 182 | return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe); |
| 183 | } |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div))) |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 186 | /* The divider is fixed to 2 in 16-bit mode */ |
| 187 | #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\ |
| 190 | ((val)+500)/1000),-128,127)) |
| 191 | #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000) |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | #define PWM_TO_REG(val) ((val) >> 1) |
| 194 | #define PWM_FROM_REG(val) (((val)&0x7f) << 1) |
| 195 | |
| 196 | static int DIV_TO_REG(int val) |
| 197 | { |
| 198 | int answer = 0; |
Jean Delvare | b9e349f | 2006-08-28 14:26:22 +0200 | [diff] [blame] | 199 | while (answer < 7 && (val >>= 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | answer++; |
| 201 | return answer; |
| 202 | } |
| 203 | #define DIV_FROM_REG(val) (1 << (val)) |
| 204 | |
Jean Delvare | f8d0c19 | 2007-02-14 21:15:02 +0100 | [diff] [blame] | 205 | static const unsigned int pwm_freq[8] = { |
| 206 | 48000000 / 128, |
| 207 | 24000000 / 128, |
| 208 | 12000000 / 128, |
| 209 | 8000000 / 128, |
| 210 | 6000000 / 128, |
| 211 | 3000000 / 128, |
| 212 | 1500000 / 128, |
| 213 | 750000 / 128, |
| 214 | }; |
| 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Jean Delvare | ed6bafb | 2007-02-14 21:15:03 +0100 | [diff] [blame] | 217 | /* For each registered chip, we need to keep some data in memory. |
| 218 | The structure is dynamically allocated. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | struct it87_data { |
| 220 | struct i2c_client client; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 221 | struct class_device *class_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | enum chips type; |
| 223 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 224 | struct mutex update_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | char valid; /* !=0 if following fields are valid */ |
| 226 | unsigned long last_updated; /* In jiffies */ |
| 227 | |
| 228 | u8 in[9]; /* Register value */ |
Jean Delvare | 3543a53 | 2006-08-28 14:27:25 +0200 | [diff] [blame] | 229 | u8 in_max[8]; /* Register value */ |
| 230 | u8 in_min[8]; /* Register value */ |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 231 | u8 has_fan; /* Bitfield, fans enabled */ |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 232 | u16 fan[3]; /* Register values, possibly combined */ |
| 233 | u16 fan_min[3]; /* Register values, possibly combined */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | u8 temp[3]; /* Register value */ |
| 235 | u8 temp_high[3]; /* Register value */ |
| 236 | u8 temp_low[3]; /* Register value */ |
| 237 | u8 sensor; /* Register value */ |
| 238 | u8 fan_div[3]; /* Register encoding, shifted right */ |
| 239 | u8 vid; /* Register encoding, combined */ |
Jean Delvare | a7be58a | 2005-12-18 16:40:14 +0100 | [diff] [blame] | 240 | u8 vrm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | u32 alarms; /* Register encoding, combined */ |
| 242 | u8 fan_main_ctrl; /* Register value */ |
Jean Delvare | f8d0c19 | 2007-02-14 21:15:02 +0100 | [diff] [blame] | 243 | u8 fan_ctl; /* Register value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | u8 manual_pwm_ctl[3]; /* manual PWM value set by user */ |
| 245 | }; |
| 246 | |
| 247 | |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 248 | static int it87_detect(struct i2c_adapter *adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | static int it87_detach_client(struct i2c_client *client); |
| 250 | |
Darren Jenkins | f6c27fc | 2006-02-27 23:14:58 +0100 | [diff] [blame] | 251 | static int it87_read_value(struct i2c_client *client, u8 reg); |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 252 | static void it87_write_value(struct i2c_client *client, u8 reg, u8 value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | static struct it87_data *it87_update_device(struct device *dev); |
| 254 | static int it87_check_pwm(struct i2c_client *client); |
| 255 | static void it87_init_client(struct i2c_client *client, struct it87_data *data); |
| 256 | |
| 257 | |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 258 | static struct i2c_driver it87_isa_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 259 | .driver = { |
Jean Delvare | 8721884 | 2006-09-03 22:36:14 +0200 | [diff] [blame] | 260 | .owner = THIS_MODULE, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 261 | .name = "it87-isa", |
| 262 | }, |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 263 | .attach_adapter = it87_detect, |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 264 | .detach_client = it87_detach_client, |
| 265 | }; |
| 266 | |
| 267 | |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 268 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
| 269 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 271 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 272 | int nr = sensor_attr->index; |
| 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | struct it87_data *data = it87_update_device(dev); |
| 275 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr])); |
| 276 | } |
| 277 | |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 278 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
| 279 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 281 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 282 | int nr = sensor_attr->index; |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | struct it87_data *data = it87_update_device(dev); |
| 285 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr])); |
| 286 | } |
| 287 | |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 288 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
| 289 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 291 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 292 | int nr = sensor_attr->index; |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | struct it87_data *data = it87_update_device(dev); |
| 295 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr])); |
| 296 | } |
| 297 | |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 298 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
| 299 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 301 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 302 | int nr = sensor_attr->index; |
| 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | struct i2c_client *client = to_i2c_client(dev); |
| 305 | struct it87_data *data = i2c_get_clientdata(client); |
| 306 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 307 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 308 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | data->in_min[nr] = IN_TO_REG(val); |
| 310 | it87_write_value(client, IT87_REG_VIN_MIN(nr), |
| 311 | data->in_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 312 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return count; |
| 314 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 315 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
| 316 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 318 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 319 | int nr = sensor_attr->index; |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | struct i2c_client *client = to_i2c_client(dev); |
| 322 | struct it87_data *data = i2c_get_clientdata(client); |
| 323 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 324 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 325 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | data->in_max[nr] = IN_TO_REG(val); |
| 327 | it87_write_value(client, IT87_REG_VIN_MAX(nr), |
| 328 | data->in_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 329 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return count; |
| 331 | } |
| 332 | |
| 333 | #define show_in_offset(offset) \ |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 334 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 335 | show_in, NULL, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | #define limit_in_offset(offset) \ |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 338 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 339 | show_in_min, set_in_min, offset); \ |
| 340 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 341 | show_in_max, set_in_max, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | show_in_offset(0); |
| 344 | limit_in_offset(0); |
| 345 | show_in_offset(1); |
| 346 | limit_in_offset(1); |
| 347 | show_in_offset(2); |
| 348 | limit_in_offset(2); |
| 349 | show_in_offset(3); |
| 350 | limit_in_offset(3); |
| 351 | show_in_offset(4); |
| 352 | limit_in_offset(4); |
| 353 | show_in_offset(5); |
| 354 | limit_in_offset(5); |
| 355 | show_in_offset(6); |
| 356 | limit_in_offset(6); |
| 357 | show_in_offset(7); |
| 358 | limit_in_offset(7); |
| 359 | show_in_offset(8); |
| 360 | |
| 361 | /* 3 temperatures */ |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 362 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 363 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 365 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 366 | int nr = sensor_attr->index; |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | struct it87_data *data = it87_update_device(dev); |
| 369 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); |
| 370 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 371 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
| 372 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 374 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 375 | int nr = sensor_attr->index; |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | struct it87_data *data = it87_update_device(dev); |
| 378 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr])); |
| 379 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 380 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
| 381 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 383 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 384 | int nr = sensor_attr->index; |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | struct it87_data *data = it87_update_device(dev); |
| 387 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr])); |
| 388 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 389 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
| 390 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 392 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 393 | int nr = sensor_attr->index; |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | struct i2c_client *client = to_i2c_client(dev); |
| 396 | struct it87_data *data = i2c_get_clientdata(client); |
| 397 | int val = simple_strtol(buf, NULL, 10); |
| 398 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 399 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | data->temp_high[nr] = TEMP_TO_REG(val); |
| 401 | it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 402 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | return count; |
| 404 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 405 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
| 406 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 408 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 409 | int nr = sensor_attr->index; |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | struct i2c_client *client = to_i2c_client(dev); |
| 412 | struct it87_data *data = i2c_get_clientdata(client); |
| 413 | int val = simple_strtol(buf, NULL, 10); |
| 414 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 415 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | data->temp_low[nr] = TEMP_TO_REG(val); |
| 417 | it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 418 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | return count; |
| 420 | } |
| 421 | #define show_temp_offset(offset) \ |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 422 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
| 423 | show_temp, NULL, offset - 1); \ |
| 424 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 425 | show_temp_max, set_temp_max, offset - 1); \ |
| 426 | static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \ |
| 427 | show_temp_min, set_temp_min, offset - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | show_temp_offset(1); |
| 430 | show_temp_offset(2); |
| 431 | show_temp_offset(3); |
| 432 | |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 433 | static ssize_t show_sensor(struct device *dev, struct device_attribute *attr, |
| 434 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 436 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 437 | int nr = sensor_attr->index; |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | struct it87_data *data = it87_update_device(dev); |
| 440 | u8 reg = data->sensor; /* In case the value is updated while we use it */ |
| 441 | |
| 442 | if (reg & (1 << nr)) |
| 443 | return sprintf(buf, "3\n"); /* thermal diode */ |
| 444 | if (reg & (8 << nr)) |
| 445 | return sprintf(buf, "2\n"); /* thermistor */ |
| 446 | return sprintf(buf, "0\n"); /* disabled */ |
| 447 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 448 | static ssize_t set_sensor(struct device *dev, struct device_attribute *attr, |
| 449 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 451 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 452 | int nr = sensor_attr->index; |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | struct i2c_client *client = to_i2c_client(dev); |
| 455 | struct it87_data *data = i2c_get_clientdata(client); |
| 456 | int val = simple_strtol(buf, NULL, 10); |
| 457 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 458 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | data->sensor &= ~(1 << nr); |
| 461 | data->sensor &= ~(8 << nr); |
| 462 | /* 3 = thermal diode; 2 = thermistor; 0 = disabled */ |
| 463 | if (val == 3) |
| 464 | data->sensor |= 1 << nr; |
| 465 | else if (val == 2) |
| 466 | data->sensor |= 8 << nr; |
| 467 | else if (val != 0) { |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 468 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | return -EINVAL; |
| 470 | } |
| 471 | it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 472 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | return count; |
| 474 | } |
| 475 | #define show_sensor_offset(offset) \ |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 476 | static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \ |
| 477 | show_sensor, set_sensor, offset - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | show_sensor_offset(1); |
| 480 | show_sensor_offset(2); |
| 481 | show_sensor_offset(3); |
| 482 | |
| 483 | /* 3 Fans */ |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 484 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
| 485 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 487 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 488 | int nr = sensor_attr->index; |
| 489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | struct it87_data *data = it87_update_device(dev); |
| 491 | return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr], |
| 492 | DIV_FROM_REG(data->fan_div[nr]))); |
| 493 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 494 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
| 495 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 497 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 498 | int nr = sensor_attr->index; |
| 499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | struct it87_data *data = it87_update_device(dev); |
| 501 | return sprintf(buf,"%d\n", |
| 502 | FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]))); |
| 503 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 504 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
| 505 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 507 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 508 | int nr = sensor_attr->index; |
| 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | struct it87_data *data = it87_update_device(dev); |
| 511 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
| 512 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 513 | static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, |
| 514 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 516 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 517 | int nr = sensor_attr->index; |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | struct it87_data *data = it87_update_device(dev); |
| 520 | return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0); |
| 521 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 522 | static ssize_t show_pwm(struct device *dev, struct device_attribute *attr, |
| 523 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 525 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 526 | int nr = sensor_attr->index; |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct it87_data *data = it87_update_device(dev); |
| 529 | return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]); |
| 530 | } |
Jean Delvare | f8d0c19 | 2007-02-14 21:15:02 +0100 | [diff] [blame] | 531 | static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr, |
| 532 | char *buf) |
| 533 | { |
| 534 | struct it87_data *data = it87_update_device(dev); |
| 535 | int index = (data->fan_ctl >> 4) & 0x07; |
| 536 | |
| 537 | return sprintf(buf, "%u\n", pwm_freq[index]); |
| 538 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 539 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 540 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 542 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 543 | int nr = sensor_attr->index; |
| 544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | struct i2c_client *client = to_i2c_client(dev); |
| 546 | struct it87_data *data = i2c_get_clientdata(client); |
| 547 | int val = simple_strtol(buf, NULL, 10); |
Jean Delvare | 7f999aa | 2007-02-14 21:15:03 +0100 | [diff] [blame] | 548 | u8 reg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 550 | mutex_lock(&data->update_lock); |
Jean Delvare | 7f999aa | 2007-02-14 21:15:03 +0100 | [diff] [blame] | 551 | reg = it87_read_value(client, IT87_REG_FAN_DIV); |
Jean Delvare | 07eab46 | 2005-11-23 15:44:31 -0800 | [diff] [blame] | 552 | switch (nr) { |
| 553 | case 0: data->fan_div[nr] = reg & 0x07; break; |
| 554 | case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break; |
| 555 | case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break; |
| 556 | } |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
| 559 | it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 560 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return count; |
| 562 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 563 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
| 564 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 566 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 567 | int nr = sensor_attr->index; |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | struct i2c_client *client = to_i2c_client(dev); |
| 570 | struct it87_data *data = i2c_get_clientdata(client); |
Jean Delvare | b9e349f | 2006-08-28 14:26:22 +0200 | [diff] [blame] | 571 | unsigned long val = simple_strtoul(buf, NULL, 10); |
Jean Delvare | 8ab4ec3 | 2006-08-28 14:35:46 +0200 | [diff] [blame] | 572 | int min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | u8 old; |
| 574 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 575 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | old = it87_read_value(client, IT87_REG_FAN_DIV); |
| 577 | |
Jean Delvare | 8ab4ec3 | 2006-08-28 14:35:46 +0200 | [diff] [blame] | 578 | /* Save fan min limit */ |
| 579 | min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
| 581 | switch (nr) { |
| 582 | case 0: |
| 583 | case 1: |
| 584 | data->fan_div[nr] = DIV_TO_REG(val); |
| 585 | break; |
| 586 | case 2: |
| 587 | if (val < 8) |
| 588 | data->fan_div[nr] = 1; |
| 589 | else |
| 590 | data->fan_div[nr] = 3; |
| 591 | } |
| 592 | val = old & 0x80; |
| 593 | val |= (data->fan_div[0] & 0x07); |
| 594 | val |= (data->fan_div[1] & 0x07) << 3; |
| 595 | if (data->fan_div[2] == 3) |
| 596 | val |= 0x1 << 6; |
| 597 | it87_write_value(client, IT87_REG_FAN_DIV, val); |
| 598 | |
Jean Delvare | 8ab4ec3 | 2006-08-28 14:35:46 +0200 | [diff] [blame] | 599 | /* Restore fan min limit */ |
| 600 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
| 601 | it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]); |
| 602 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 603 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | return count; |
| 605 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 606 | static ssize_t set_pwm_enable(struct device *dev, |
| 607 | struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 609 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 610 | int nr = sensor_attr->index; |
| 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | struct i2c_client *client = to_i2c_client(dev); |
| 613 | struct it87_data *data = i2c_get_clientdata(client); |
| 614 | int val = simple_strtol(buf, NULL, 10); |
| 615 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 616 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
| 618 | if (val == 0) { |
| 619 | int tmp; |
| 620 | /* make sure the fan is on when in on/off mode */ |
| 621 | tmp = it87_read_value(client, IT87_REG_FAN_CTL); |
| 622 | it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr)); |
| 623 | /* set on/off mode */ |
| 624 | data->fan_main_ctrl &= ~(1 << nr); |
| 625 | it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); |
| 626 | } else if (val == 1) { |
| 627 | /* set SmartGuardian mode */ |
| 628 | data->fan_main_ctrl |= (1 << nr); |
| 629 | it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); |
| 630 | /* set saved pwm value, clear FAN_CTLX PWM mode bit */ |
| 631 | it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr])); |
| 632 | } else { |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 633 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | return -EINVAL; |
| 635 | } |
| 636 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 637 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | return count; |
| 639 | } |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 640 | static ssize_t set_pwm(struct device *dev, struct device_attribute *attr, |
| 641 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 643 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 644 | int nr = sensor_attr->index; |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | struct i2c_client *client = to_i2c_client(dev); |
| 647 | struct it87_data *data = i2c_get_clientdata(client); |
| 648 | int val = simple_strtol(buf, NULL, 10); |
| 649 | |
| 650 | if (val < 0 || val > 255) |
| 651 | return -EINVAL; |
| 652 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 653 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | data->manual_pwm_ctl[nr] = val; |
| 655 | if (data->fan_main_ctrl & (1 << nr)) |
| 656 | it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr])); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 657 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | return count; |
| 659 | } |
Jean Delvare | f8d0c19 | 2007-02-14 21:15:02 +0100 | [diff] [blame] | 660 | static ssize_t set_pwm_freq(struct device *dev, |
| 661 | struct device_attribute *attr, const char *buf, size_t count) |
| 662 | { |
| 663 | struct i2c_client *client = to_i2c_client(dev); |
| 664 | struct it87_data *data = i2c_get_clientdata(client); |
| 665 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 666 | int i; |
| 667 | |
| 668 | /* Search for the nearest available frequency */ |
| 669 | for (i = 0; i < 7; i++) { |
| 670 | if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2) |
| 671 | break; |
| 672 | } |
| 673 | |
| 674 | mutex_lock(&data->update_lock); |
| 675 | data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL) & 0x8f; |
| 676 | data->fan_ctl |= i << 4; |
| 677 | it87_write_value(client, IT87_REG_FAN_CTL, data->fan_ctl); |
| 678 | mutex_unlock(&data->update_lock); |
| 679 | |
| 680 | return count; |
| 681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 683 | #define show_fan_offset(offset) \ |
| 684 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 685 | show_fan, NULL, offset - 1); \ |
| 686 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 687 | show_fan_min, set_fan_min, offset - 1); \ |
| 688 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 689 | show_fan_div, set_fan_div, offset - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
| 691 | show_fan_offset(1); |
| 692 | show_fan_offset(2); |
| 693 | show_fan_offset(3); |
| 694 | |
| 695 | #define show_pwm_offset(offset) \ |
Jean Delvare | 20ad93d | 2005-06-05 11:53:25 +0200 | [diff] [blame] | 696 | static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \ |
| 697 | show_pwm_enable, set_pwm_enable, offset - 1); \ |
| 698 | static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \ |
Jean Delvare | f8d0c19 | 2007-02-14 21:15:02 +0100 | [diff] [blame] | 699 | show_pwm, set_pwm, offset - 1); \ |
| 700 | static DEVICE_ATTR(pwm##offset##_freq, \ |
| 701 | (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \ |
| 702 | show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
| 704 | show_pwm_offset(1); |
| 705 | show_pwm_offset(2); |
| 706 | show_pwm_offset(3); |
| 707 | |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 708 | /* A different set of callbacks for 16-bit fans */ |
| 709 | static ssize_t show_fan16(struct device *dev, struct device_attribute *attr, |
| 710 | char *buf) |
| 711 | { |
| 712 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 713 | int nr = sensor_attr->index; |
| 714 | struct it87_data *data = it87_update_device(dev); |
| 715 | return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr])); |
| 716 | } |
| 717 | |
| 718 | static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr, |
| 719 | char *buf) |
| 720 | { |
| 721 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 722 | int nr = sensor_attr->index; |
| 723 | struct it87_data *data = it87_update_device(dev); |
| 724 | return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr])); |
| 725 | } |
| 726 | |
| 727 | static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr, |
| 728 | const char *buf, size_t count) |
| 729 | { |
| 730 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 731 | int nr = sensor_attr->index; |
| 732 | struct i2c_client *client = to_i2c_client(dev); |
| 733 | struct it87_data *data = i2c_get_clientdata(client); |
| 734 | int val = simple_strtol(buf, NULL, 10); |
| 735 | |
| 736 | mutex_lock(&data->update_lock); |
| 737 | data->fan_min[nr] = FAN16_TO_REG(val); |
| 738 | it87_write_value(client, IT87_REG_FAN_MIN(nr), |
| 739 | data->fan_min[nr] & 0xff); |
| 740 | it87_write_value(client, IT87_REG_FANX_MIN(nr), |
| 741 | data->fan_min[nr] >> 8); |
| 742 | mutex_unlock(&data->update_lock); |
| 743 | return count; |
| 744 | } |
| 745 | |
| 746 | /* We want to use the same sysfs file names as 8-bit fans, but we need |
| 747 | different variable names, so we have to use SENSOR_ATTR instead of |
| 748 | SENSOR_DEVICE_ATTR. */ |
| 749 | #define show_fan16_offset(offset) \ |
| 750 | static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \ |
| 751 | = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \ |
| 752 | show_fan16, NULL, offset - 1); \ |
| 753 | static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \ |
| 754 | = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 755 | show_fan16_min, set_fan16_min, offset - 1) |
| 756 | |
| 757 | show_fan16_offset(1); |
| 758 | show_fan16_offset(2); |
| 759 | show_fan16_offset(3); |
| 760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | /* Alarms */ |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 762 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
| 764 | struct it87_data *data = it87_update_device(dev); |
Jean Delvare | 68188ba | 2005-05-16 18:52:38 +0200 | [diff] [blame] | 765 | return sprintf(buf, "%u\n", data->alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
Jean Delvare | 1d66c64 | 2005-04-18 21:16:59 -0700 | [diff] [blame] | 767 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | static ssize_t |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 770 | show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | { |
| 772 | struct it87_data *data = it87_update_device(dev); |
Jean Delvare | a7be58a | 2005-12-18 16:40:14 +0100 | [diff] [blame] | 773 | return sprintf(buf, "%u\n", data->vrm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
| 775 | static ssize_t |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 776 | store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
| 778 | struct i2c_client *client = to_i2c_client(dev); |
| 779 | struct it87_data *data = i2c_get_clientdata(client); |
| 780 | u32 val; |
| 781 | |
| 782 | val = simple_strtoul(buf, NULL, 10); |
| 783 | data->vrm = val; |
| 784 | |
| 785 | return count; |
| 786 | } |
| 787 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
| 789 | static ssize_t |
Yani Ioannou | 30f7429 | 2005-05-17 06:41:35 -0400 | [diff] [blame] | 790 | show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
| 792 | struct it87_data *data = it87_update_device(dev); |
| 793 | return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm)); |
| 794 | } |
| 795 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL); |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 796 | |
| 797 | static struct attribute *it87_attributes[] = { |
| 798 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 799 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 800 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 801 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 802 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 803 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 804 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 805 | &sensor_dev_attr_in7_input.dev_attr.attr, |
| 806 | &sensor_dev_attr_in8_input.dev_attr.attr, |
| 807 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 808 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 809 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 810 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 811 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 812 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 813 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 814 | &sensor_dev_attr_in7_min.dev_attr.attr, |
| 815 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 816 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 817 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 818 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 819 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 820 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 821 | &sensor_dev_attr_in6_max.dev_attr.attr, |
| 822 | &sensor_dev_attr_in7_max.dev_attr.attr, |
| 823 | |
| 824 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 825 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 826 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 827 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 828 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 829 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 830 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 831 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 832 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 833 | &sensor_dev_attr_temp1_type.dev_attr.attr, |
| 834 | &sensor_dev_attr_temp2_type.dev_attr.attr, |
| 835 | &sensor_dev_attr_temp3_type.dev_attr.attr, |
| 836 | |
| 837 | &dev_attr_alarms.attr, |
| 838 | NULL |
| 839 | }; |
| 840 | |
| 841 | static const struct attribute_group it87_group = { |
| 842 | .attrs = it87_attributes, |
| 843 | }; |
| 844 | |
| 845 | static struct attribute *it87_attributes_opt[] = { |
| 846 | &sensor_dev_attr_fan1_input16.dev_attr.attr, |
| 847 | &sensor_dev_attr_fan1_min16.dev_attr.attr, |
| 848 | &sensor_dev_attr_fan2_input16.dev_attr.attr, |
| 849 | &sensor_dev_attr_fan2_min16.dev_attr.attr, |
| 850 | &sensor_dev_attr_fan3_input16.dev_attr.attr, |
| 851 | &sensor_dev_attr_fan3_min16.dev_attr.attr, |
| 852 | |
| 853 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 854 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 855 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 856 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 857 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 858 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
| 859 | &sensor_dev_attr_fan3_input.dev_attr.attr, |
| 860 | &sensor_dev_attr_fan3_min.dev_attr.attr, |
| 861 | &sensor_dev_attr_fan3_div.dev_attr.attr, |
| 862 | |
| 863 | &sensor_dev_attr_pwm1_enable.dev_attr.attr, |
| 864 | &sensor_dev_attr_pwm2_enable.dev_attr.attr, |
| 865 | &sensor_dev_attr_pwm3_enable.dev_attr.attr, |
| 866 | &sensor_dev_attr_pwm1.dev_attr.attr, |
| 867 | &sensor_dev_attr_pwm2.dev_attr.attr, |
| 868 | &sensor_dev_attr_pwm3.dev_attr.attr, |
| 869 | |
| 870 | &dev_attr_vrm.attr, |
| 871 | &dev_attr_cpu0_vid.attr, |
| 872 | NULL |
| 873 | }; |
| 874 | |
| 875 | static const struct attribute_group it87_group_opt = { |
| 876 | .attrs = it87_attributes_opt, |
| 877 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
Jean Delvare | 2d8672c | 2005-07-19 23:56:35 +0200 | [diff] [blame] | 879 | /* SuperIO detection - will change isa_address if a chip is found */ |
Jean Delvare | 9174999 | 2005-10-08 00:10:00 +0200 | [diff] [blame] | 880 | static int __init it87_find(unsigned short *address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
| 882 | int err = -ENODEV; |
| 883 | |
| 884 | superio_enter(); |
| 885 | chip_type = superio_inw(DEVID); |
| 886 | if (chip_type != IT8712F_DEVID |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 887 | && chip_type != IT8716F_DEVID |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 888 | && chip_type != IT8718F_DEVID |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | && chip_type != IT8705F_DEVID) |
| 890 | goto exit; |
| 891 | |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 892 | superio_select(PME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | if (!(superio_inb(IT87_ACT_REG) & 0x01)) { |
| 894 | pr_info("it87: Device not activated, skipping\n"); |
| 895 | goto exit; |
| 896 | } |
| 897 | |
| 898 | *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1); |
| 899 | if (*address == 0) { |
| 900 | pr_info("it87: Base address not set, skipping\n"); |
| 901 | goto exit; |
| 902 | } |
| 903 | |
| 904 | err = 0; |
| 905 | pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n", |
| 906 | chip_type, *address, superio_inb(DEVREV) & 0x0f); |
| 907 | |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 908 | /* Read GPIO config and VID value from LDN 7 (GPIO) */ |
| 909 | if (chip_type != IT8705F_DEVID) { |
| 910 | int reg; |
| 911 | |
| 912 | superio_select(GPIO); |
| 913 | if (chip_type == it8718) |
| 914 | vid_value = superio_inb(IT87_SIO_VID_REG); |
| 915 | |
| 916 | reg = superio_inb(IT87_SIO_PINX2_REG); |
| 917 | if (reg & (1 << 0)) |
| 918 | pr_info("it87: in3 is VCC (+5V)\n"); |
| 919 | if (reg & (1 << 1)) |
| 920 | pr_info("it87: in7 is VCCH (+5V Stand-By)\n"); |
| 921 | } |
| 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | exit: |
| 924 | superio_exit(); |
| 925 | return err; |
| 926 | } |
| 927 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 928 | /* This function is called by i2c_probe */ |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 929 | static int it87_detect(struct i2c_adapter *adapter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | struct i2c_client *new_client; |
| 932 | struct it87_data *data; |
| 933 | int err = 0; |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 934 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | int enable_pwm_interface; |
| 936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | /* Reserve the ISA region */ |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 938 | if (!request_region(isa_address, IT87_EXTENT, |
| 939 | it87_isa_driver.driver.name)){ |
| 940 | err = -EBUSY; |
| 941 | goto ERROR0; |
| 942 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
Deepak Saxena | ba9c2e8 | 2005-10-17 23:08:32 +0200 | [diff] [blame] | 944 | if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | err = -ENOMEM; |
| 946 | goto ERROR1; |
| 947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | new_client = &data->client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | i2c_set_clientdata(new_client, data); |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 951 | new_client->addr = isa_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | new_client->adapter = adapter; |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 953 | new_client->driver = &it87_isa_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
| 955 | /* Now, we do the remaining detection. */ |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 956 | if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80) |
| 957 | || it87_read_value(new_client, IT87_REG_CHIPID) != 0x90) { |
| 958 | err = -ENODEV; |
| 959 | goto ERROR2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* Determine the chip type. */ |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 963 | switch (chip_type) { |
| 964 | case IT8712F_DEVID: |
| 965 | data->type = it8712; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | name = "it8712"; |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 967 | break; |
| 968 | case IT8716F_DEVID: |
| 969 | data->type = it8716; |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 970 | name = "it8716"; |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 971 | break; |
| 972 | case IT8718F_DEVID: |
| 973 | data->type = it8718; |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 974 | name = "it8718"; |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 975 | break; |
| 976 | default: |
| 977 | data->type = it87; |
| 978 | name = "it87"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /* Fill in the remaining client fields and put it into the global list */ |
| 982 | strlcpy(new_client->name, name, I2C_NAME_SIZE); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 983 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
| 985 | /* Tell the I2C layer a new client has arrived */ |
| 986 | if ((err = i2c_attach_client(new_client))) |
| 987 | goto ERROR2; |
| 988 | |
| 989 | /* Check PWM configuration */ |
| 990 | enable_pwm_interface = it87_check_pwm(new_client); |
| 991 | |
| 992 | /* Initialize the IT87 chip */ |
| 993 | it87_init_client(new_client, data); |
| 994 | |
| 995 | /* Register sysfs hooks */ |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 996 | if ((err = sysfs_create_group(&new_client->dev.kobj, &it87_group))) |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 997 | goto ERROR3; |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 998 | |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 999 | /* Do not create fan files for disabled fans */ |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 1000 | if (data->type == it8716 || data->type == it8718) { |
| 1001 | /* 16-bit tachometers */ |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1002 | if (data->has_fan & (1 << 0)) { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1003 | if ((err = device_create_file(&new_client->dev, |
| 1004 | &sensor_dev_attr_fan1_input16.dev_attr)) |
| 1005 | || (err = device_create_file(&new_client->dev, |
| 1006 | &sensor_dev_attr_fan1_min16.dev_attr))) |
| 1007 | goto ERROR4; |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1008 | } |
| 1009 | if (data->has_fan & (1 << 1)) { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1010 | if ((err = device_create_file(&new_client->dev, |
| 1011 | &sensor_dev_attr_fan2_input16.dev_attr)) |
| 1012 | || (err = device_create_file(&new_client->dev, |
| 1013 | &sensor_dev_attr_fan2_min16.dev_attr))) |
| 1014 | goto ERROR4; |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1015 | } |
| 1016 | if (data->has_fan & (1 << 2)) { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1017 | if ((err = device_create_file(&new_client->dev, |
| 1018 | &sensor_dev_attr_fan3_input16.dev_attr)) |
| 1019 | || (err = device_create_file(&new_client->dev, |
| 1020 | &sensor_dev_attr_fan3_min16.dev_attr))) |
| 1021 | goto ERROR4; |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1022 | } |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1023 | } else { |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 1024 | /* 8-bit tachometers with clock divider */ |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1025 | if (data->has_fan & (1 << 0)) { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1026 | if ((err = device_create_file(&new_client->dev, |
| 1027 | &sensor_dev_attr_fan1_input.dev_attr)) |
| 1028 | || (err = device_create_file(&new_client->dev, |
| 1029 | &sensor_dev_attr_fan1_min.dev_attr)) |
| 1030 | || (err = device_create_file(&new_client->dev, |
| 1031 | &sensor_dev_attr_fan1_div.dev_attr))) |
| 1032 | goto ERROR4; |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1033 | } |
| 1034 | if (data->has_fan & (1 << 1)) { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1035 | if ((err = device_create_file(&new_client->dev, |
| 1036 | &sensor_dev_attr_fan2_input.dev_attr)) |
| 1037 | || (err = device_create_file(&new_client->dev, |
| 1038 | &sensor_dev_attr_fan2_min.dev_attr)) |
| 1039 | || (err = device_create_file(&new_client->dev, |
| 1040 | &sensor_dev_attr_fan2_div.dev_attr))) |
| 1041 | goto ERROR4; |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1042 | } |
| 1043 | if (data->has_fan & (1 << 2)) { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1044 | if ((err = device_create_file(&new_client->dev, |
| 1045 | &sensor_dev_attr_fan3_input.dev_attr)) |
| 1046 | || (err = device_create_file(&new_client->dev, |
| 1047 | &sensor_dev_attr_fan3_min.dev_attr)) |
| 1048 | || (err = device_create_file(&new_client->dev, |
| 1049 | &sensor_dev_attr_fan3_div.dev_attr))) |
| 1050 | goto ERROR4; |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1051 | } |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | if (enable_pwm_interface) { |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1055 | if ((err = device_create_file(&new_client->dev, |
| 1056 | &sensor_dev_attr_pwm1_enable.dev_attr)) |
| 1057 | || (err = device_create_file(&new_client->dev, |
| 1058 | &sensor_dev_attr_pwm2_enable.dev_attr)) |
| 1059 | || (err = device_create_file(&new_client->dev, |
| 1060 | &sensor_dev_attr_pwm3_enable.dev_attr)) |
| 1061 | || (err = device_create_file(&new_client->dev, |
| 1062 | &sensor_dev_attr_pwm1.dev_attr)) |
| 1063 | || (err = device_create_file(&new_client->dev, |
| 1064 | &sensor_dev_attr_pwm2.dev_attr)) |
| 1065 | || (err = device_create_file(&new_client->dev, |
Jean Delvare | f8d0c19 | 2007-02-14 21:15:02 +0100 | [diff] [blame] | 1066 | &sensor_dev_attr_pwm3.dev_attr)) |
| 1067 | || (err = device_create_file(&new_client->dev, |
| 1068 | &dev_attr_pwm1_freq)) |
| 1069 | || (err = device_create_file(&new_client->dev, |
| 1070 | &dev_attr_pwm2_freq)) |
| 1071 | || (err = device_create_file(&new_client->dev, |
| 1072 | &dev_attr_pwm3_freq))) |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1073 | goto ERROR4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 1076 | if (data->type == it8712 || data->type == it8716 |
| 1077 | || data->type == it8718) { |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 1078 | data->vrm = vid_which_vrm(); |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 1079 | /* VID reading from Super-I/O config space if available */ |
| 1080 | data->vid = vid_value; |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1081 | if ((err = device_create_file(&new_client->dev, |
| 1082 | &dev_attr_vrm)) |
| 1083 | || (err = device_create_file(&new_client->dev, |
| 1084 | &dev_attr_cpu0_vid))) |
| 1085 | goto ERROR4; |
| 1086 | } |
| 1087 | |
| 1088 | data->class_dev = hwmon_device_register(&new_client->dev); |
| 1089 | if (IS_ERR(data->class_dev)) { |
| 1090 | err = PTR_ERR(data->class_dev); |
| 1091 | goto ERROR4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | return 0; |
| 1095 | |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1096 | ERROR4: |
| 1097 | sysfs_remove_group(&new_client->dev.kobj, &it87_group); |
| 1098 | sysfs_remove_group(&new_client->dev.kobj, &it87_group_opt); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1099 | ERROR3: |
| 1100 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | ERROR2: |
| 1102 | kfree(data); |
| 1103 | ERROR1: |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1104 | release_region(isa_address, IT87_EXTENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | ERROR0: |
| 1106 | return err; |
| 1107 | } |
| 1108 | |
| 1109 | static int it87_detach_client(struct i2c_client *client) |
| 1110 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1111 | struct it87_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | int err; |
| 1113 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1114 | hwmon_device_unregister(data->class_dev); |
Jean Delvare | 87808be | 2006-09-24 21:17:13 +0200 | [diff] [blame] | 1115 | sysfs_remove_group(&client->dev.kobj, &it87_group); |
| 1116 | sysfs_remove_group(&client->dev.kobj, &it87_group_opt); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1117 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 1118 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1121 | release_region(client->addr, IT87_EXTENT); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 1122 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
Jean Delvare | 7f999aa | 2007-02-14 21:15:03 +0100 | [diff] [blame] | 1127 | /* Must be called with data->update_lock held, except during initialization. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks, |
| 1129 | would slow down the IT87 access and should not be necessary. */ |
| 1130 | static int it87_read_value(struct i2c_client *client, u8 reg) |
| 1131 | { |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1132 | outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET); |
Jean Delvare | 7f999aa | 2007-02-14 21:15:03 +0100 | [diff] [blame] | 1133 | return inb_p(client->addr + IT87_DATA_REG_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Jean Delvare | 7f999aa | 2007-02-14 21:15:03 +0100 | [diff] [blame] | 1136 | /* Must be called with data->update_lock held, except during initialization. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks, |
| 1138 | would slow down the IT87 access and should not be necessary. */ |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1139 | static void it87_write_value(struct i2c_client *client, u8 reg, u8 value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | { |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1141 | outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET); |
| 1142 | outb_p(value, client->addr + IT87_DATA_REG_OFFSET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /* Return 1 if and only if the PWM interface is safe to use */ |
| 1146 | static int it87_check_pwm(struct i2c_client *client) |
| 1147 | { |
| 1148 | /* Some BIOSes fail to correctly configure the IT87 fans. All fans off |
| 1149 | * and polarity set to active low is sign that this is the case so we |
| 1150 | * disable pwm control to protect the user. */ |
| 1151 | int tmp = it87_read_value(client, IT87_REG_FAN_CTL); |
| 1152 | if ((tmp & 0x87) == 0) { |
| 1153 | if (fix_pwm_polarity) { |
| 1154 | /* The user asks us to attempt a chip reconfiguration. |
| 1155 | * This means switching to active high polarity and |
| 1156 | * inverting all fan speed values. */ |
| 1157 | int i; |
| 1158 | u8 pwm[3]; |
| 1159 | |
| 1160 | for (i = 0; i < 3; i++) |
| 1161 | pwm[i] = it87_read_value(client, |
| 1162 | IT87_REG_PWM(i)); |
| 1163 | |
| 1164 | /* If any fan is in automatic pwm mode, the polarity |
| 1165 | * might be correct, as suspicious as it seems, so we |
| 1166 | * better don't change anything (but still disable the |
| 1167 | * PWM interface). */ |
| 1168 | if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) { |
| 1169 | dev_info(&client->dev, "Reconfiguring PWM to " |
| 1170 | "active high polarity\n"); |
| 1171 | it87_write_value(client, IT87_REG_FAN_CTL, |
| 1172 | tmp | 0x87); |
| 1173 | for (i = 0; i < 3; i++) |
| 1174 | it87_write_value(client, |
| 1175 | IT87_REG_PWM(i), |
| 1176 | 0x7f & ~pwm[i]); |
| 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | dev_info(&client->dev, "PWM configuration is " |
| 1181 | "too broken to be fixed\n"); |
| 1182 | } |
| 1183 | |
| 1184 | dev_info(&client->dev, "Detected broken BIOS " |
| 1185 | "defaults, disabling PWM interface\n"); |
| 1186 | return 0; |
| 1187 | } else if (fix_pwm_polarity) { |
| 1188 | dev_info(&client->dev, "PWM configuration looks " |
| 1189 | "sane, won't touch\n"); |
| 1190 | } |
| 1191 | |
| 1192 | return 1; |
| 1193 | } |
| 1194 | |
| 1195 | /* Called when we have found a new IT87. */ |
| 1196 | static void it87_init_client(struct i2c_client *client, struct it87_data *data) |
| 1197 | { |
| 1198 | int tmp, i; |
| 1199 | |
| 1200 | /* initialize to sane defaults: |
| 1201 | * - if the chip is in manual pwm mode, this will be overwritten with |
| 1202 | * the actual settings on the chip (so in this case, initialization |
| 1203 | * is not needed) |
| 1204 | * - if in automatic or on/off mode, we could switch to manual mode, |
| 1205 | * read the registers and set manual_pwm_ctl accordingly, but currently |
| 1206 | * this is not implemented, so we initialize to something sane */ |
| 1207 | for (i = 0; i < 3; i++) { |
| 1208 | data->manual_pwm_ctl[i] = 0xff; |
| 1209 | } |
| 1210 | |
Jean Delvare | c5df9b7 | 2006-08-28 14:37:54 +0200 | [diff] [blame] | 1211 | /* Some chips seem to have default value 0xff for all limit |
| 1212 | * registers. For low voltage limits it makes no sense and triggers |
| 1213 | * alarms, so change to 0 instead. For high temperature limits, it |
| 1214 | * means -1 degree C, which surprisingly doesn't trigger an alarm, |
| 1215 | * but is still confusing, so change to 127 degrees C. */ |
| 1216 | for (i = 0; i < 8; i++) { |
| 1217 | tmp = it87_read_value(client, IT87_REG_VIN_MIN(i)); |
| 1218 | if (tmp == 0xff) |
| 1219 | it87_write_value(client, IT87_REG_VIN_MIN(i), 0); |
| 1220 | } |
| 1221 | for (i = 0; i < 3; i++) { |
| 1222 | tmp = it87_read_value(client, IT87_REG_TEMP_HIGH(i)); |
| 1223 | if (tmp == 0xff) |
| 1224 | it87_write_value(client, IT87_REG_TEMP_HIGH(i), 127); |
| 1225 | } |
| 1226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | /* Check if temperature channnels are reset manually or by some reason */ |
| 1228 | tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE); |
| 1229 | if ((tmp & 0x3f) == 0) { |
| 1230 | /* Temp1,Temp3=thermistor; Temp2=thermal diode */ |
| 1231 | tmp = (tmp & 0xc0) | 0x2a; |
| 1232 | it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp); |
| 1233 | } |
| 1234 | data->sensor = tmp; |
| 1235 | |
| 1236 | /* Check if voltage monitors are reset manually or by some reason */ |
| 1237 | tmp = it87_read_value(client, IT87_REG_VIN_ENABLE); |
| 1238 | if ((tmp & 0xff) == 0) { |
| 1239 | /* Enable all voltage monitors */ |
| 1240 | it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff); |
| 1241 | } |
| 1242 | |
| 1243 | /* Check if tachometers are reset manually or by some reason */ |
| 1244 | data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL); |
| 1245 | if ((data->fan_main_ctrl & 0x70) == 0) { |
| 1246 | /* Enable all fan tachometers */ |
| 1247 | data->fan_main_ctrl |= 0x70; |
| 1248 | it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl); |
| 1249 | } |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1250 | data->has_fan = (data->fan_main_ctrl >> 4) & 0x07; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1252 | /* Set tachometers to 16-bit mode if needed */ |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 1253 | if (data->type == it8716 || data->type == it8718) { |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1254 | tmp = it87_read_value(client, IT87_REG_FAN_16BIT); |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1255 | if (~tmp & 0x07 & data->has_fan) { |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1256 | dev_dbg(&client->dev, |
| 1257 | "Setting fan1-3 to 16-bit mode\n"); |
| 1258 | it87_write_value(client, IT87_REG_FAN_16BIT, |
| 1259 | tmp | 0x07); |
| 1260 | } |
| 1261 | } |
| 1262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | /* Set current fan mode registers and the default settings for the |
| 1264 | * other mode registers */ |
| 1265 | for (i = 0; i < 3; i++) { |
| 1266 | if (data->fan_main_ctrl & (1 << i)) { |
| 1267 | /* pwm mode */ |
| 1268 | tmp = it87_read_value(client, IT87_REG_PWM(i)); |
| 1269 | if (tmp & 0x80) { |
| 1270 | /* automatic pwm - not yet implemented, but |
| 1271 | * leave the settings made by the BIOS alone |
| 1272 | * until a change is requested via the sysfs |
| 1273 | * interface */ |
| 1274 | } else { |
| 1275 | /* manual pwm */ |
| 1276 | data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp); |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | /* Start monitoring */ |
| 1282 | it87_write_value(client, IT87_REG_CONFIG, |
| 1283 | (it87_read_value(client, IT87_REG_CONFIG) & 0x36) |
| 1284 | | (update_vbat ? 0x41 : 0x01)); |
| 1285 | } |
| 1286 | |
| 1287 | static struct it87_data *it87_update_device(struct device *dev) |
| 1288 | { |
| 1289 | struct i2c_client *client = to_i2c_client(dev); |
| 1290 | struct it87_data *data = i2c_get_clientdata(client); |
| 1291 | int i; |
| 1292 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1293 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
| 1295 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 1296 | || !data->valid) { |
| 1297 | |
| 1298 | if (update_vbat) { |
| 1299 | /* Cleared after each update, so reenable. Value |
| 1300 | returned by this read will be previous value */ |
| 1301 | it87_write_value(client, IT87_REG_CONFIG, |
| 1302 | it87_read_value(client, IT87_REG_CONFIG) | 0x40); |
| 1303 | } |
| 1304 | for (i = 0; i <= 7; i++) { |
| 1305 | data->in[i] = |
| 1306 | it87_read_value(client, IT87_REG_VIN(i)); |
| 1307 | data->in_min[i] = |
| 1308 | it87_read_value(client, IT87_REG_VIN_MIN(i)); |
| 1309 | data->in_max[i] = |
| 1310 | it87_read_value(client, IT87_REG_VIN_MAX(i)); |
| 1311 | } |
Jean Delvare | 3543a53 | 2006-08-28 14:27:25 +0200 | [diff] [blame] | 1312 | /* in8 (battery) has no limit registers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | data->in[8] = |
| 1314 | it87_read_value(client, IT87_REG_VIN(8)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
| 1316 | for (i = 0; i < 3; i++) { |
Jean Delvare | 9060f8b | 2006-08-28 14:24:17 +0200 | [diff] [blame] | 1317 | /* Skip disabled fans */ |
| 1318 | if (!(data->has_fan & (1 << i))) |
| 1319 | continue; |
| 1320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | data->fan_min[i] = |
| 1322 | it87_read_value(client, IT87_REG_FAN_MIN(i)); |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1323 | data->fan[i] = it87_read_value(client, |
| 1324 | IT87_REG_FAN(i)); |
| 1325 | /* Add high byte if in 16-bit mode */ |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 1326 | if (data->type == it8716 || data->type == it8718) { |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1327 | data->fan[i] |= it87_read_value(client, |
| 1328 | IT87_REG_FANX(i)) << 8; |
| 1329 | data->fan_min[i] |= it87_read_value(client, |
| 1330 | IT87_REG_FANX_MIN(i)) << 8; |
| 1331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | } |
| 1333 | for (i = 0; i < 3; i++) { |
| 1334 | data->temp[i] = |
| 1335 | it87_read_value(client, IT87_REG_TEMP(i)); |
| 1336 | data->temp_high[i] = |
| 1337 | it87_read_value(client, IT87_REG_TEMP_HIGH(i)); |
| 1338 | data->temp_low[i] = |
| 1339 | it87_read_value(client, IT87_REG_TEMP_LOW(i)); |
| 1340 | } |
| 1341 | |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1342 | /* Newer chips don't have clock dividers */ |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 1343 | if ((data->has_fan & 0x07) && data->type != it8716 |
| 1344 | && data->type != it8718) { |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1345 | i = it87_read_value(client, IT87_REG_FAN_DIV); |
| 1346 | data->fan_div[0] = i & 0x07; |
| 1347 | data->fan_div[1] = (i >> 3) & 0x07; |
| 1348 | data->fan_div[2] = (i & 0x40) ? 3 : 1; |
| 1349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | data->alarms = |
| 1352 | it87_read_value(client, IT87_REG_ALARM1) | |
| 1353 | (it87_read_value(client, IT87_REG_ALARM2) << 8) | |
| 1354 | (it87_read_value(client, IT87_REG_ALARM3) << 16); |
| 1355 | data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL); |
Jean Delvare | f8d0c19 | 2007-02-14 21:15:02 +0100 | [diff] [blame] | 1356 | data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | |
| 1358 | data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE); |
| 1359 | /* The 8705 does not have VID capability */ |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1360 | if (data->type == it8712 || data->type == it8716) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | data->vid = it87_read_value(client, IT87_REG_VID); |
Jean Delvare | 17d648b | 2006-08-28 14:23:46 +0200 | [diff] [blame] | 1362 | /* The older IT8712F revisions had only 5 VID pins, |
| 1363 | but we assume it is always safe to read 6 bits. */ |
| 1364 | data->vid &= 0x3f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | } |
| 1366 | data->last_updated = jiffies; |
| 1367 | data->valid = 1; |
| 1368 | } |
| 1369 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 1370 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
| 1372 | return data; |
| 1373 | } |
| 1374 | |
| 1375 | static int __init sm_it87_init(void) |
| 1376 | { |
Jean Delvare | 9174999 | 2005-10-08 00:10:00 +0200 | [diff] [blame] | 1377 | int res; |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1378 | |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1379 | if ((res = it87_find(&isa_address))) |
Jean Delvare | fde0950 | 2005-07-19 23:51:07 +0200 | [diff] [blame] | 1380 | return res; |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1381 | return i2c_isa_add_driver(&it87_isa_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | static void __exit sm_it87_exit(void) |
| 1385 | { |
Jean Delvare | 8e9afcb | 2006-12-12 18:18:28 +0100 | [diff] [blame] | 1386 | i2c_isa_del_driver(&it87_isa_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | |
Jean Delvare | b19367c | 2006-08-28 14:39:26 +0200 | [diff] [blame] | 1390 | MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>, " |
| 1391 | "Jean Delvare <khali@linux-fr.org>"); |
Jean Delvare | 87673dd | 2006-08-28 14:37:19 +0200 | [diff] [blame] | 1392 | MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F, SiS950 driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | module_param(update_vbat, bool, 0); |
| 1394 | MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value"); |
| 1395 | module_param(fix_pwm_polarity, bool, 0); |
| 1396 | MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)"); |
| 1397 | MODULE_LICENSE("GPL"); |
| 1398 | |
| 1399 | module_init(sm_it87_init); |
| 1400 | module_exit(sm_it87_exit); |