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