Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | vt8231.c - Part of lm_sensors, Linux kernel modules |
| 3 | for hardware monitoring |
| 4 | |
Roger Lucas | af86576 | 2008-02-13 07:52:06 -0500 | [diff] [blame^] | 5 | Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk> |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 6 | Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com> |
| 7 | Aaron M. Marsh <amarsh@sdf.lonestar.org> |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | /* Supports VIA VT8231 South Bridge embedded sensors |
| 25 | */ |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/pci.h> |
| 31 | #include <linux/jiffies.h> |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 33 | #include <linux/hwmon.h> |
| 34 | #include <linux/hwmon-sysfs.h> |
| 35 | #include <linux/hwmon-vid.h> |
| 36 | #include <linux/err.h> |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 37 | #include <linux/mutex.h> |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 38 | #include <asm/io.h> |
| 39 | |
| 40 | static int force_addr; |
| 41 | module_param(force_addr, int, 0); |
| 42 | MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors"); |
| 43 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 44 | static struct platform_device *pdev; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 45 | |
| 46 | #define VT8231_EXTENT 0x80 |
| 47 | #define VT8231_BASE_REG 0x70 |
| 48 | #define VT8231_ENABLE_REG 0x74 |
| 49 | |
| 50 | /* The VT8231 registers |
| 51 | |
| 52 | The reset value for the input channel configuration is used (Reg 0x4A=0x07) |
| 53 | which sets the selected inputs marked with '*' below if multiple options are |
| 54 | possible: |
| 55 | |
| 56 | Voltage Mode Temperature Mode |
| 57 | Sensor Linux Id Linux Id VIA Id |
| 58 | -------- -------- -------- ------ |
| 59 | CPU Diode N/A temp1 0 |
| 60 | UIC1 in0 temp2 * 1 |
| 61 | UIC2 in1 * temp3 2 |
| 62 | UIC3 in2 * temp4 3 |
| 63 | UIC4 in3 * temp5 4 |
| 64 | UIC5 in4 * temp6 5 |
| 65 | 3.3V in5 N/A |
| 66 | |
| 67 | Note that the BIOS may set the configuration register to a different value |
| 68 | to match the motherboard configuration. |
| 69 | */ |
| 70 | |
| 71 | /* fans numbered 0-1 */ |
| 72 | #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr)) |
| 73 | #define VT8231_REG_FAN(nr) (0x29 + (nr)) |
| 74 | |
| 75 | /* Voltage inputs numbered 0-5 */ |
| 76 | |
| 77 | static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 }; |
| 78 | static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 }; |
| 79 | static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 }; |
| 80 | |
| 81 | /* Temperatures are numbered 1-6 according to the Linux kernel specification. |
| 82 | ** |
| 83 | ** In the VIA datasheet, however, the temperatures are numbered from zero. |
| 84 | ** Since it is important that this driver can easily be compared to the VIA |
| 85 | ** datasheet, we will use the VIA numbering within this driver and map the |
| 86 | ** kernel sysfs device name to the VIA number in the sysfs callback. |
| 87 | */ |
| 88 | |
| 89 | #define VT8231_REG_TEMP_LOW01 0x49 |
| 90 | #define VT8231_REG_TEMP_LOW25 0x4d |
| 91 | |
| 92 | static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 }; |
| 93 | static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 }; |
| 94 | static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 }; |
| 95 | |
| 96 | #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210) |
| 97 | #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210) |
| 98 | #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200) |
| 99 | |
| 100 | #define VT8231_REG_CONFIG 0x40 |
| 101 | #define VT8231_REG_ALARM1 0x41 |
| 102 | #define VT8231_REG_ALARM2 0x42 |
| 103 | #define VT8231_REG_FANDIV 0x47 |
| 104 | #define VT8231_REG_UCH_CONFIG 0x4a |
| 105 | #define VT8231_REG_TEMP1_CONFIG 0x4b |
| 106 | #define VT8231_REG_TEMP2_CONFIG 0x4c |
| 107 | |
| 108 | /* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux |
| 109 | ** numbering |
| 110 | */ |
| 111 | #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \ |
| 112 | ((ch_config) >> ((i)+1)) & 0x01) |
| 113 | /* voltages 0-5 */ |
| 114 | #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \ |
| 115 | !(((ch_config) >> ((i)+2)) & 0x01)) |
| 116 | |
| 117 | #define DIV_FROM_REG(val) (1 << (val)) |
| 118 | |
| 119 | /* NB The values returned here are NOT temperatures. The calibration curves |
| 120 | ** for the thermistor curves are board-specific and must go in the |
| 121 | ** sensors.conf file. Temperature sensors are actually ten bits, but the |
| 122 | ** VIA datasheet only considers the 8 MSBs obtained from the regtemp[] |
| 123 | ** register. The temperature value returned should have a magnitude of 3, |
| 124 | ** so we use the VIA scaling as the "true" scaling and use the remaining 2 |
| 125 | ** LSBs as fractional precision. |
| 126 | ** |
| 127 | ** All the on-chip hardware temperature comparisons for the alarms are only |
| 128 | ** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits |
| 129 | ** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are |
| 130 | ** ignored. |
| 131 | */ |
| 132 | |
| 133 | /******** FAN RPM CONVERSIONS ******** |
| 134 | ** This chip saturates back at 0, not at 255 like many the other chips. |
| 135 | ** So, 0 means 0 RPM |
| 136 | */ |
| 137 | static inline u8 FAN_TO_REG(long rpm, int div) |
| 138 | { |
| 139 | if (rpm == 0) |
| 140 | return 0; |
| 141 | return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255); |
| 142 | } |
| 143 | |
| 144 | #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div))) |
| 145 | |
| 146 | struct vt8231_data { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 147 | unsigned short addr; |
| 148 | const char *name; |
| 149 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 150 | struct mutex update_lock; |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 151 | struct device *hwmon_dev; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 152 | char valid; /* !=0 if following fields are valid */ |
| 153 | unsigned long last_updated; /* In jiffies */ |
| 154 | |
| 155 | u8 in[6]; /* Register value */ |
| 156 | u8 in_max[6]; /* Register value */ |
| 157 | u8 in_min[6]; /* Register value */ |
| 158 | u16 temp[6]; /* Register value 10 bit, right aligned */ |
| 159 | u8 temp_max[6]; /* Register value */ |
| 160 | u8 temp_min[6]; /* Register value */ |
| 161 | u8 fan[2]; /* Register value */ |
| 162 | u8 fan_min[2]; /* Register value */ |
| 163 | u8 fan_div[2]; /* Register encoding, shifted right */ |
| 164 | u16 alarms; /* Register encoding */ |
| 165 | u8 uch_config; |
| 166 | }; |
| 167 | |
| 168 | static struct pci_dev *s_bridge; |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 169 | static int vt8231_probe(struct platform_device *pdev); |
Jean Delvare | d054612 | 2007-07-22 12:09:48 +0200 | [diff] [blame] | 170 | static int __devexit vt8231_remove(struct platform_device *pdev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 171 | static struct vt8231_data *vt8231_update_device(struct device *dev); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 172 | static void vt8231_init_device(struct vt8231_data *data); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 173 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 174 | static inline int vt8231_read_value(struct vt8231_data *data, u8 reg) |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 175 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 176 | return inb_p(data->addr + reg); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 179 | static inline void vt8231_write_value(struct vt8231_data *data, u8 reg, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 180 | u8 value) |
| 181 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 182 | outb_p(value, data->addr + reg); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* following are the sysfs callback functions */ |
| 186 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
| 187 | char *buf) |
| 188 | { |
| 189 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 190 | int nr = sensor_attr->index; |
| 191 | struct vt8231_data *data = vt8231_update_device(dev); |
| 192 | |
| 193 | return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958); |
| 194 | } |
| 195 | |
| 196 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
| 197 | char *buf) |
| 198 | { |
| 199 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 200 | int nr = sensor_attr->index; |
| 201 | struct vt8231_data *data = vt8231_update_device(dev); |
| 202 | |
| 203 | return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958); |
| 204 | } |
| 205 | |
| 206 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
| 207 | char *buf) |
| 208 | { |
| 209 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 210 | int nr = sensor_attr->index; |
| 211 | struct vt8231_data *data = vt8231_update_device(dev); |
| 212 | |
| 213 | return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958)); |
| 214 | } |
| 215 | |
| 216 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
| 217 | const char *buf, size_t count) |
| 218 | { |
| 219 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 220 | int nr = sensor_attr->index; |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 221 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 222 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 223 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 224 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 225 | data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 226 | vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 227 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 228 | return count; |
| 229 | } |
| 230 | |
| 231 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
| 232 | const char *buf, size_t count) |
| 233 | { |
| 234 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 235 | int nr = sensor_attr->index; |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 236 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 237 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 238 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 239 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 240 | data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 241 | vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 242 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 243 | return count; |
| 244 | } |
| 245 | |
| 246 | /* Special case for input 5 as this has 3.3V scaling built into the chip */ |
| 247 | static ssize_t show_in5(struct device *dev, struct device_attribute *attr, |
| 248 | char *buf) |
| 249 | { |
| 250 | struct vt8231_data *data = vt8231_update_device(dev); |
| 251 | |
| 252 | return sprintf(buf, "%d\n", |
| 253 | (((data->in[5] - 3) * 10000 * 54) / (958 * 34))); |
| 254 | } |
| 255 | |
| 256 | static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr, |
| 257 | char *buf) |
| 258 | { |
| 259 | struct vt8231_data *data = vt8231_update_device(dev); |
| 260 | |
| 261 | return sprintf(buf, "%d\n", |
| 262 | (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34))); |
| 263 | } |
| 264 | |
| 265 | static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr, |
| 266 | char *buf) |
| 267 | { |
| 268 | struct vt8231_data *data = vt8231_update_device(dev); |
| 269 | |
| 270 | return sprintf(buf, "%d\n", |
| 271 | (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34))); |
| 272 | } |
| 273 | |
| 274 | static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr, |
| 275 | const char *buf, size_t count) |
| 276 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 277 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 278 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 279 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 280 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 281 | data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3, |
| 282 | 0, 255); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 283 | vt8231_write_value(data, regvoltmin[5], data->in_min[5]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 284 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 285 | return count; |
| 286 | } |
| 287 | |
| 288 | static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr, |
| 289 | const char *buf, size_t count) |
| 290 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 291 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 292 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 293 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 294 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 295 | data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3, |
| 296 | 0, 255); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 297 | vt8231_write_value(data, regvoltmax[5], data->in_max[5]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 298 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 299 | return count; |
| 300 | } |
| 301 | |
| 302 | #define define_voltage_sysfs(offset) \ |
| 303 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 304 | show_in, NULL, offset); \ |
| 305 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 306 | show_in_min, set_in_min, offset); \ |
| 307 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 308 | show_in_max, set_in_max, offset) |
| 309 | |
| 310 | define_voltage_sysfs(0); |
| 311 | define_voltage_sysfs(1); |
| 312 | define_voltage_sysfs(2); |
| 313 | define_voltage_sysfs(3); |
| 314 | define_voltage_sysfs(4); |
| 315 | |
| 316 | static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL); |
| 317 | static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min); |
| 318 | static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max); |
| 319 | |
| 320 | /* Temperatures */ |
| 321 | static ssize_t show_temp0(struct device *dev, struct device_attribute *attr, |
| 322 | char *buf) |
| 323 | { |
| 324 | struct vt8231_data *data = vt8231_update_device(dev); |
| 325 | return sprintf(buf, "%d\n", data->temp[0] * 250); |
| 326 | } |
| 327 | |
| 328 | static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr, |
| 329 | char *buf) |
| 330 | { |
| 331 | struct vt8231_data *data = vt8231_update_device(dev); |
| 332 | return sprintf(buf, "%d\n", data->temp_max[0] * 1000); |
| 333 | } |
| 334 | |
| 335 | static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr, |
| 336 | char *buf) |
| 337 | { |
| 338 | struct vt8231_data *data = vt8231_update_device(dev); |
| 339 | return sprintf(buf, "%d\n", data->temp_min[0] * 1000); |
| 340 | } |
| 341 | |
| 342 | static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr, |
| 343 | const char *buf, size_t count) |
| 344 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 345 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 346 | int val = simple_strtol(buf, NULL, 10); |
| 347 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 348 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 349 | data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 350 | vt8231_write_value(data, regtempmax[0], data->temp_max[0]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 351 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 352 | return count; |
| 353 | } |
| 354 | static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr, |
| 355 | const char *buf, size_t count) |
| 356 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 357 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 358 | int val = simple_strtol(buf, NULL, 10); |
| 359 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 360 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 361 | data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 362 | vt8231_write_value(data, regtempmin[0], data->temp_min[0]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 363 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 364 | return count; |
| 365 | } |
| 366 | |
| 367 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 368 | char *buf) |
| 369 | { |
| 370 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 371 | int nr = sensor_attr->index; |
| 372 | struct vt8231_data *data = vt8231_update_device(dev); |
| 373 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); |
| 374 | } |
| 375 | |
| 376 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
| 377 | char *buf) |
| 378 | { |
| 379 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 380 | int nr = sensor_attr->index; |
| 381 | struct vt8231_data *data = vt8231_update_device(dev); |
| 382 | return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr])); |
| 383 | } |
| 384 | |
| 385 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
| 386 | char *buf) |
| 387 | { |
| 388 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 389 | int nr = sensor_attr->index; |
| 390 | struct vt8231_data *data = vt8231_update_device(dev); |
| 391 | return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr])); |
| 392 | } |
| 393 | |
| 394 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
| 395 | const char *buf, size_t count) |
| 396 | { |
| 397 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 398 | int nr = sensor_attr->index; |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 399 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 400 | int val = simple_strtol(buf, NULL, 10); |
| 401 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 402 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 403 | data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 404 | vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 405 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 406 | return count; |
| 407 | } |
| 408 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
| 409 | const char *buf, size_t count) |
| 410 | { |
| 411 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 412 | int nr = sensor_attr->index; |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 413 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 414 | int val = simple_strtol(buf, NULL, 10); |
| 415 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 416 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 417 | data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 418 | vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 419 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 420 | return count; |
| 421 | } |
| 422 | |
| 423 | /* Note that these map the Linux temperature sensor numbering (1-6) to the VIA |
| 424 | ** temperature sensor numbering (0-5) |
| 425 | */ |
| 426 | #define define_temperature_sysfs(offset) \ |
| 427 | static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \ |
| 428 | show_temp, NULL, offset - 1); \ |
| 429 | static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \ |
| 430 | show_temp_max, set_temp_max, offset - 1); \ |
Jean Delvare | e3efa5a | 2006-02-05 23:11:16 +0100 | [diff] [blame] | 431 | static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \ |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 432 | show_temp_min, set_temp_min, offset - 1) |
| 433 | |
| 434 | static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL); |
| 435 | static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max); |
Jean Delvare | e3efa5a | 2006-02-05 23:11:16 +0100 | [diff] [blame] | 436 | static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 437 | |
| 438 | define_temperature_sysfs(2); |
| 439 | define_temperature_sysfs(3); |
| 440 | define_temperature_sysfs(4); |
| 441 | define_temperature_sysfs(5); |
| 442 | define_temperature_sysfs(6); |
| 443 | |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 444 | /* Fans */ |
| 445 | static ssize_t show_fan(struct device *dev, struct device_attribute *attr, |
| 446 | char *buf) |
| 447 | { |
| 448 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 449 | int nr = sensor_attr->index; |
| 450 | struct vt8231_data *data = vt8231_update_device(dev); |
| 451 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr], |
| 452 | DIV_FROM_REG(data->fan_div[nr]))); |
| 453 | } |
| 454 | |
| 455 | static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr, |
| 456 | char *buf) |
| 457 | { |
| 458 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 459 | int nr = sensor_attr->index; |
| 460 | struct vt8231_data *data = vt8231_update_device(dev); |
| 461 | return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr], |
| 462 | DIV_FROM_REG(data->fan_div[nr]))); |
| 463 | } |
| 464 | |
| 465 | static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr, |
| 466 | char *buf) |
| 467 | { |
| 468 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 469 | int nr = sensor_attr->index; |
| 470 | struct vt8231_data *data = vt8231_update_device(dev); |
| 471 | return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr])); |
| 472 | } |
| 473 | |
| 474 | static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr, |
| 475 | const char *buf, size_t count) |
| 476 | { |
| 477 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 478 | int nr = sensor_attr->index; |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 479 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 480 | int val = simple_strtoul(buf, NULL, 10); |
| 481 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 482 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 483 | data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr])); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 484 | vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 485 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 486 | return count; |
| 487 | } |
| 488 | |
| 489 | static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr, |
| 490 | const char *buf, size_t count) |
| 491 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 492 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 493 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 494 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 495 | int nr = sensor_attr->index; |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 496 | int old = vt8231_read_value(data, VT8231_REG_FANDIV); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 497 | long min = FAN_FROM_REG(data->fan_min[nr], |
| 498 | DIV_FROM_REG(data->fan_div[nr])); |
| 499 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 500 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 501 | switch (val) { |
| 502 | case 1: data->fan_div[nr] = 0; break; |
| 503 | case 2: data->fan_div[nr] = 1; break; |
| 504 | case 4: data->fan_div[nr] = 2; break; |
| 505 | case 8: data->fan_div[nr] = 3; break; |
| 506 | default: |
Joe Perches | b20ff13 | 2007-11-19 17:48:07 -0800 | [diff] [blame] | 507 | dev_err(dev, "fan_div value %ld not supported. " |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 508 | "Choose one of 1, 2, 4 or 8!\n", val); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 509 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | /* Correct the fan minimum speed */ |
| 514 | data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr])); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 515 | vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 516 | |
| 517 | old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 518 | vt8231_write_value(data, VT8231_REG_FANDIV, old); |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 519 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 520 | return count; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | #define define_fan_sysfs(offset) \ |
| 525 | static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \ |
| 526 | show_fan, NULL, offset - 1); \ |
| 527 | static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \ |
| 528 | show_fan_div, set_fan_div, offset - 1); \ |
| 529 | static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \ |
| 530 | show_fan_min, set_fan_min, offset - 1) |
| 531 | |
| 532 | define_fan_sysfs(1); |
| 533 | define_fan_sysfs(2); |
| 534 | |
| 535 | /* Alarms */ |
| 536 | static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, |
| 537 | char *buf) |
| 538 | { |
| 539 | struct vt8231_data *data = vt8231_update_device(dev); |
| 540 | return sprintf(buf, "%d\n", data->alarms); |
| 541 | } |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 542 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
| 543 | |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 544 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 545 | char *buf) |
| 546 | { |
| 547 | int bitnr = to_sensor_dev_attr(attr)->index; |
| 548 | struct vt8231_data *data = vt8231_update_device(dev); |
| 549 | return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1); |
| 550 | } |
| 551 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 552 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 553 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 554 | static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 555 | static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 556 | static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 557 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 11); |
| 558 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0); |
| 559 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 1); |
| 560 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 561 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8); |
| 562 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 2); |
| 563 | static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 564 | static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7); |
| 565 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 566 | static ssize_t show_name(struct device *dev, struct device_attribute |
| 567 | *devattr, char *buf) |
| 568 | { |
| 569 | struct vt8231_data *data = dev_get_drvdata(dev); |
| 570 | return sprintf(buf, "%s\n", data->name); |
| 571 | } |
| 572 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 573 | |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 574 | static struct attribute *vt8231_attributes_temps[6][5] = { |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 575 | { |
| 576 | &dev_attr_temp1_input.attr, |
| 577 | &dev_attr_temp1_max_hyst.attr, |
| 578 | &dev_attr_temp1_max.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 579 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 580 | NULL |
| 581 | }, { |
| 582 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 583 | &sensor_dev_attr_temp2_max_hyst.dev_attr.attr, |
| 584 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 585 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 586 | NULL |
| 587 | }, { |
| 588 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 589 | &sensor_dev_attr_temp3_max_hyst.dev_attr.attr, |
| 590 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 591 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 592 | NULL |
| 593 | }, { |
| 594 | &sensor_dev_attr_temp4_input.dev_attr.attr, |
| 595 | &sensor_dev_attr_temp4_max_hyst.dev_attr.attr, |
| 596 | &sensor_dev_attr_temp4_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 597 | &sensor_dev_attr_temp4_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 598 | NULL |
| 599 | }, { |
| 600 | &sensor_dev_attr_temp5_input.dev_attr.attr, |
| 601 | &sensor_dev_attr_temp5_max_hyst.dev_attr.attr, |
| 602 | &sensor_dev_attr_temp5_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 603 | &sensor_dev_attr_temp5_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 604 | NULL |
| 605 | }, { |
| 606 | &sensor_dev_attr_temp6_input.dev_attr.attr, |
| 607 | &sensor_dev_attr_temp6_max_hyst.dev_attr.attr, |
| 608 | &sensor_dev_attr_temp6_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 609 | &sensor_dev_attr_temp6_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 610 | NULL |
| 611 | } |
| 612 | }; |
| 613 | |
| 614 | static const struct attribute_group vt8231_group_temps[6] = { |
| 615 | { .attrs = vt8231_attributes_temps[0] }, |
| 616 | { .attrs = vt8231_attributes_temps[1] }, |
| 617 | { .attrs = vt8231_attributes_temps[2] }, |
| 618 | { .attrs = vt8231_attributes_temps[3] }, |
| 619 | { .attrs = vt8231_attributes_temps[4] }, |
| 620 | { .attrs = vt8231_attributes_temps[5] }, |
| 621 | }; |
| 622 | |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 623 | static struct attribute *vt8231_attributes_volts[6][5] = { |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 624 | { |
| 625 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 626 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 627 | &sensor_dev_attr_in0_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 628 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 629 | NULL |
| 630 | }, { |
| 631 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 632 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 633 | &sensor_dev_attr_in1_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 634 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 635 | NULL |
| 636 | }, { |
| 637 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 638 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 639 | &sensor_dev_attr_in2_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 640 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 641 | NULL |
| 642 | }, { |
| 643 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 644 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 645 | &sensor_dev_attr_in3_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 646 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 647 | NULL |
| 648 | }, { |
| 649 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 650 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 651 | &sensor_dev_attr_in4_max.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 652 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 653 | NULL |
| 654 | }, { |
| 655 | &dev_attr_in5_input.attr, |
| 656 | &dev_attr_in5_min.attr, |
| 657 | &dev_attr_in5_max.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 658 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 659 | NULL |
| 660 | } |
| 661 | }; |
| 662 | |
| 663 | static const struct attribute_group vt8231_group_volts[6] = { |
| 664 | { .attrs = vt8231_attributes_volts[0] }, |
| 665 | { .attrs = vt8231_attributes_volts[1] }, |
| 666 | { .attrs = vt8231_attributes_volts[2] }, |
| 667 | { .attrs = vt8231_attributes_volts[3] }, |
| 668 | { .attrs = vt8231_attributes_volts[4] }, |
| 669 | { .attrs = vt8231_attributes_volts[5] }, |
| 670 | }; |
| 671 | |
| 672 | static struct attribute *vt8231_attributes[] = { |
| 673 | &sensor_dev_attr_fan1_input.dev_attr.attr, |
| 674 | &sensor_dev_attr_fan2_input.dev_attr.attr, |
| 675 | &sensor_dev_attr_fan1_min.dev_attr.attr, |
| 676 | &sensor_dev_attr_fan2_min.dev_attr.attr, |
| 677 | &sensor_dev_attr_fan1_div.dev_attr.attr, |
| 678 | &sensor_dev_attr_fan2_div.dev_attr.attr, |
Jean Delvare | 2d1374c | 2008-01-06 15:46:02 +0100 | [diff] [blame] | 679 | &sensor_dev_attr_fan1_alarm.dev_attr.attr, |
| 680 | &sensor_dev_attr_fan2_alarm.dev_attr.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 681 | &dev_attr_alarms.attr, |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 682 | &dev_attr_name.attr, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 683 | NULL |
| 684 | }; |
| 685 | |
| 686 | static const struct attribute_group vt8231_group = { |
| 687 | .attrs = vt8231_attributes, |
| 688 | }; |
| 689 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 690 | static struct platform_driver vt8231_driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 691 | .driver = { |
Jean Delvare | 8721884 | 2006-09-03 22:36:14 +0200 | [diff] [blame] | 692 | .owner = THIS_MODULE, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 693 | .name = "vt8231", |
| 694 | }, |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 695 | .probe = vt8231_probe, |
| 696 | .remove = __devexit_p(vt8231_remove), |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | static struct pci_device_id vt8231_pci_ids[] = { |
| 700 | { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) }, |
| 701 | { 0, } |
| 702 | }; |
| 703 | |
| 704 | MODULE_DEVICE_TABLE(pci, vt8231_pci_ids); |
| 705 | |
| 706 | static int __devinit vt8231_pci_probe(struct pci_dev *dev, |
| 707 | const struct pci_device_id *id); |
| 708 | |
| 709 | static struct pci_driver vt8231_pci_driver = { |
| 710 | .name = "vt8231", |
| 711 | .id_table = vt8231_pci_ids, |
| 712 | .probe = vt8231_pci_probe, |
| 713 | }; |
| 714 | |
Mark M. Hoffman | a022fef | 2007-10-14 15:00:24 -0400 | [diff] [blame] | 715 | static int vt8231_probe(struct platform_device *pdev) |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 716 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 717 | struct resource *res; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 718 | struct vt8231_data *data; |
| 719 | int err = 0, i; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 720 | |
| 721 | /* Reserve the ISA region */ |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 722 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 723 | if (!request_region(res->start, VT8231_EXTENT, |
| 724 | vt8231_driver.driver.name)) { |
| 725 | dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n", |
| 726 | (unsigned long)res->start, (unsigned long)res->end); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 727 | return -ENODEV; |
| 728 | } |
| 729 | |
| 730 | if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) { |
| 731 | err = -ENOMEM; |
| 732 | goto exit_release; |
| 733 | } |
| 734 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 735 | platform_set_drvdata(pdev, data); |
| 736 | data->addr = res->start; |
| 737 | data->name = "vt8231"; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 738 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 739 | mutex_init(&data->update_lock); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 740 | vt8231_init_device(data); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 741 | |
| 742 | /* Register sysfs hooks */ |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 743 | if ((err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group))) |
| 744 | goto exit_free; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 745 | |
| 746 | /* Must update device information to find out the config field */ |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 747 | data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 748 | |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 749 | for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) { |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 750 | if (ISTEMP(i, data->uch_config)) { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 751 | if ((err = sysfs_create_group(&pdev->dev.kobj, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 752 | &vt8231_group_temps[i]))) |
| 753 | goto exit_remove_files; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 757 | for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) { |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 758 | if (ISVOLT(i, data->uch_config)) { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 759 | if ((err = sysfs_create_group(&pdev->dev.kobj, |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 760 | &vt8231_group_volts[i]))) |
| 761 | goto exit_remove_files; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 765 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
| 766 | if (IS_ERR(data->hwmon_dev)) { |
| 767 | err = PTR_ERR(data->hwmon_dev); |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 768 | goto exit_remove_files; |
| 769 | } |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 770 | return 0; |
| 771 | |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 772 | exit_remove_files: |
| 773 | for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 774 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]); |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 775 | |
| 776 | for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 777 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]); |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 778 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 779 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group); |
| 780 | |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 781 | exit_free: |
Jean Delvare | 04a6217 | 2007-06-12 13:57:19 +0200 | [diff] [blame] | 782 | platform_set_drvdata(pdev, NULL); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 783 | kfree(data); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 784 | |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 785 | exit_release: |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 786 | release_region(res->start, VT8231_EXTENT); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 787 | return err; |
| 788 | } |
| 789 | |
Jean Delvare | d054612 | 2007-07-22 12:09:48 +0200 | [diff] [blame] | 790 | static int __devexit vt8231_remove(struct platform_device *pdev) |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 791 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 792 | struct vt8231_data *data = platform_get_drvdata(pdev); |
| 793 | int i; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 794 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 795 | hwmon_device_unregister(data->hwmon_dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 796 | |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 797 | for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 798 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]); |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 799 | |
| 800 | for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 801 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]); |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 802 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 803 | sysfs_remove_group(&pdev->dev.kobj, &vt8231_group); |
Roger Lucas | cbeeb5b | 2006-09-24 21:21:46 +0200 | [diff] [blame] | 804 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 805 | release_region(data->addr, VT8231_EXTENT); |
| 806 | platform_set_drvdata(pdev, NULL); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 807 | kfree(data); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 808 | return 0; |
| 809 | } |
| 810 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 811 | static void vt8231_init_device(struct vt8231_data *data) |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 812 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 813 | vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0); |
| 814 | vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | static struct vt8231_data *vt8231_update_device(struct device *dev) |
| 818 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 819 | struct vt8231_data *data = dev_get_drvdata(dev); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 820 | int i; |
| 821 | u16 low; |
| 822 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 823 | mutex_lock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 824 | |
| 825 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 826 | || !data->valid) { |
| 827 | for (i = 0; i < 6; i++) { |
| 828 | if (ISVOLT(i, data->uch_config)) { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 829 | data->in[i] = vt8231_read_value(data, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 830 | regvolt[i]); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 831 | data->in_min[i] = vt8231_read_value(data, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 832 | regvoltmin[i]); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 833 | data->in_max[i] = vt8231_read_value(data, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 834 | regvoltmax[i]); |
| 835 | } |
| 836 | } |
| 837 | for (i = 0; i < 2; i++) { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 838 | data->fan[i] = vt8231_read_value(data, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 839 | VT8231_REG_FAN(i)); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 840 | data->fan_min[i] = vt8231_read_value(data, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 841 | VT8231_REG_FAN_MIN(i)); |
| 842 | } |
| 843 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 844 | low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 845 | low = (low >> 6) | ((low & 0x30) >> 2) |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 846 | | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 847 | for (i = 0; i < 6; i++) { |
| 848 | if (ISTEMP(i, data->uch_config)) { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 849 | data->temp[i] = (vt8231_read_value(data, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 850 | regtemp[i]) << 2) |
| 851 | | ((low >> (2 * i)) & 0x03); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 852 | data->temp_max[i] = vt8231_read_value(data, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 853 | regtempmax[i]); |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 854 | data->temp_min[i] = vt8231_read_value(data, |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 855 | regtempmin[i]); |
| 856 | } |
| 857 | } |
| 858 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 859 | i = vt8231_read_value(data, VT8231_REG_FANDIV); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 860 | data->fan_div[0] = (i >> 4) & 0x03; |
| 861 | data->fan_div[1] = i >> 6; |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 862 | data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) | |
| 863 | (vt8231_read_value(data, VT8231_REG_ALARM2) << 8); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 864 | |
| 865 | /* Set alarm flags correctly */ |
| 866 | if (!data->fan[0] && data->fan_min[0]) { |
| 867 | data->alarms |= 0x40; |
| 868 | } else if (data->fan[0] && !data->fan_min[0]) { |
| 869 | data->alarms &= ~0x40; |
| 870 | } |
| 871 | |
| 872 | if (!data->fan[1] && data->fan_min[1]) { |
| 873 | data->alarms |= 0x80; |
| 874 | } else if (data->fan[1] && !data->fan_min[1]) { |
| 875 | data->alarms &= ~0x80; |
| 876 | } |
| 877 | |
| 878 | data->last_updated = jiffies; |
| 879 | data->valid = 1; |
| 880 | } |
| 881 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 882 | mutex_unlock(&data->update_lock); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 883 | |
| 884 | return data; |
| 885 | } |
| 886 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 887 | static int __devinit vt8231_device_add(unsigned short address) |
| 888 | { |
| 889 | struct resource res = { |
| 890 | .start = address, |
| 891 | .end = address + VT8231_EXTENT - 1, |
| 892 | .name = "vt8231", |
| 893 | .flags = IORESOURCE_IO, |
| 894 | }; |
| 895 | int err; |
| 896 | |
| 897 | pdev = platform_device_alloc("vt8231", address); |
| 898 | if (!pdev) { |
| 899 | err = -ENOMEM; |
| 900 | printk(KERN_ERR "vt8231: Device allocation failed\n"); |
| 901 | goto exit; |
| 902 | } |
| 903 | |
| 904 | err = platform_device_add_resources(pdev, &res, 1); |
| 905 | if (err) { |
| 906 | printk(KERN_ERR "vt8231: Device resource addition failed " |
| 907 | "(%d)\n", err); |
| 908 | goto exit_device_put; |
| 909 | } |
| 910 | |
| 911 | err = platform_device_add(pdev); |
| 912 | if (err) { |
| 913 | printk(KERN_ERR "vt8231: Device addition failed (%d)\n", |
| 914 | err); |
| 915 | goto exit_device_put; |
| 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | |
| 920 | exit_device_put: |
| 921 | platform_device_put(pdev); |
| 922 | exit: |
| 923 | return err; |
| 924 | } |
| 925 | |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 926 | static int __devinit vt8231_pci_probe(struct pci_dev *dev, |
| 927 | const struct pci_device_id *id) |
| 928 | { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 929 | u16 address, val; |
| 930 | if (force_addr) { |
| 931 | address = force_addr & 0xff00; |
| 932 | dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", |
| 933 | address); |
| 934 | |
| 935 | if (PCIBIOS_SUCCESSFUL != |
| 936 | pci_write_config_word(dev, VT8231_BASE_REG, address | 1)) |
| 937 | return -ENODEV; |
| 938 | } |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 939 | |
| 940 | if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG, |
| 941 | &val)) |
| 942 | return -ENODEV; |
| 943 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 944 | address = val & ~(VT8231_EXTENT - 1); |
| 945 | if (address == 0) { |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 946 | dev_err(&dev->dev, "base address not set -\ |
| 947 | upgrade BIOS or use force_addr=0xaddr\n"); |
| 948 | return -ENODEV; |
| 949 | } |
| 950 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 951 | if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG, |
| 952 | &val)) |
| 953 | return -ENODEV; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 954 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 955 | if (!(val & 0x0001)) { |
| 956 | dev_warn(&dev->dev, "enabling sensors\n"); |
| 957 | if (PCIBIOS_SUCCESSFUL != |
| 958 | pci_write_config_word(dev, VT8231_ENABLE_REG, |
| 959 | val | 0x0001)) |
| 960 | return -ENODEV; |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 961 | } |
| 962 | |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 963 | if (platform_driver_register(&vt8231_driver)) |
| 964 | goto exit; |
| 965 | |
| 966 | /* Sets global pdev as a side effect */ |
| 967 | if (vt8231_device_add(address)) |
| 968 | goto exit_unregister; |
| 969 | |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 970 | /* Always return failure here. This is to allow other drivers to bind |
| 971 | * to this pci device. We don't really want to have control over the |
| 972 | * pci device, we only wanted to read as few register values from it. |
| 973 | */ |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 974 | |
| 975 | /* We do, however, mark ourselves as using the PCI device to stop it |
| 976 | getting unloaded. */ |
| 977 | s_bridge = pci_dev_get(dev); |
| 978 | return -ENODEV; |
| 979 | |
| 980 | exit_unregister: |
| 981 | platform_driver_unregister(&vt8231_driver); |
| 982 | exit: |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 983 | return -ENODEV; |
| 984 | } |
| 985 | |
| 986 | static int __init sm_vt8231_init(void) |
| 987 | { |
Richard Knutsson | 93b4768 | 2005-11-30 01:00:35 +0100 | [diff] [blame] | 988 | return pci_register_driver(&vt8231_pci_driver); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static void __exit sm_vt8231_exit(void) |
| 992 | { |
| 993 | pci_unregister_driver(&vt8231_pci_driver); |
| 994 | if (s_bridge != NULL) { |
Roger Lucas | ec5e1a4 | 2007-06-12 21:04:08 +0200 | [diff] [blame] | 995 | platform_device_unregister(pdev); |
| 996 | platform_driver_unregister(&vt8231_driver); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 997 | pci_dev_put(s_bridge); |
| 998 | s_bridge = NULL; |
| 999 | } |
| 1000 | } |
| 1001 | |
Roger Lucas | af86576 | 2008-02-13 07:52:06 -0500 | [diff] [blame^] | 1002 | MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>"); |
Roger Lucas | 1de9e37 | 2005-11-26 20:20:05 +0100 | [diff] [blame] | 1003 | MODULE_DESCRIPTION("VT8231 sensors"); |
| 1004 | MODULE_LICENSE("GPL"); |
| 1005 | |
| 1006 | module_init(sm_vt8231_init); |
| 1007 | module_exit(sm_vt8231_exit); |