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