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