]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/hwmon/sis5595.c
x86/msr-index: Cleanup bit defines
[mirror_ubuntu-bionic-kernel.git] / drivers / hwmon / sis5595.c
CommitLineData
1da177e4 1/*
8fda79ec
GR
2 * sis5595.c - Part of lm_sensors, Linux kernel modules
3 * for hardware monitoring
4 *
5 * Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
6 * Kyösti Mälkki <kmalkki@cc.hut.fi>, and
7 * Mark D. Studebaker <mdsxyz123@yahoo.com>
8 * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
7c81c60f 9 * the help of Jean Delvare <jdelvare@suse.de>
8fda79ec
GR
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
1da177e4
LT
25
26/*
8fda79ec
GR
27 * SiS southbridge has a LM78-like chip integrated on the same IC.
28 * This driver is a customized copy of lm78.c
29 *
30 * Supports following revisions:
31 * Version PCI ID PCI Revision
32 * 1 1039/0008 AF or less
33 * 2 1039/0008 B0 or greater
34 *
35 * Note: these chips contain a 0008 device which is incompatible with the
36 * 5595. We recognize these by the presence of the listed
37 * "blacklist" PCI ID and refuse to load.
38 *
39 * NOT SUPPORTED PCI ID BLACKLIST PCI ID
40 * 540 0008 0540
41 * 550 0008 0550
42 * 5513 0008 5511
43 * 5581 0008 5597
44 * 5582 0008 5597
45 * 5597 0008 5597
46 * 5598 0008 5597/5598
47 * 630 0008 0630
48 * 645 0008 0645
49 * 730 0008 0730
50 * 735 0008 0735
51 */
1da177e4 52
4b2515db
JP
53#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
1da177e4
LT
55#include <linux/module.h>
56#include <linux/slab.h>
57#include <linux/ioport.h>
58#include <linux/pci.h>
17e7dc43 59#include <linux/platform_device.h>
943b0830 60#include <linux/hwmon.h>
1f5f48dd 61#include <linux/hwmon-sysfs.h>
943b0830 62#include <linux/err.h>
1da177e4 63#include <linux/init.h>
ff324094 64#include <linux/jiffies.h>
9a61bf63 65#include <linux/mutex.h>
a5ebe668 66#include <linux/sysfs.h>
b9acb64a 67#include <linux/acpi.h>
6055fae8 68#include <linux/io.h>
1da177e4
LT
69
70
8fda79ec
GR
71/*
72 * If force_addr is set to anything different from 0, we forcibly enable
73 * the device at the given address.
74 */
1da177e4
LT
75static u16 force_addr;
76module_param(force_addr, ushort, 0);
77MODULE_PARM_DESC(force_addr,
78 "Initialize the base address of the sensors");
79
17e7dc43 80static struct platform_device *pdev;
1da177e4
LT
81
82/* Many SIS5595 constants specified below */
83
84/* Length of ISA address segment */
85#define SIS5595_EXTENT 8
86/* PCI Config Registers */
1da177e4
LT
87#define SIS5595_BASE_REG 0x68
88#define SIS5595_PIN_REG 0x7A
89#define SIS5595_ENABLE_REG 0x7B
90
91/* Where are the ISA address/data registers relative to the base address */
92#define SIS5595_ADDR_REG_OFFSET 5
93#define SIS5595_DATA_REG_OFFSET 6
94
95/* The SIS5595 registers */
96#define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
97#define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
98#define SIS5595_REG_IN(nr) (0x20 + (nr))
99
100#define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
101#define SIS5595_REG_FAN(nr) (0x28 + (nr))
102
8fda79ec
GR
103/*
104 * On the first version of the chip, the temp registers are separate.
105 * On the second version,
106 * TEMP pin is shared with IN4, configured in PCI register 0x7A.
107 * The registers are the same as well.
108 * OVER and HYST are really MAX and MIN.
109 */
1da177e4
LT
110
111#define REV2MIN 0xb0
8fda79ec
GR
112#define SIS5595_REG_TEMP (((data->revision) >= REV2MIN) ? \
113 SIS5595_REG_IN(4) : 0x27)
114#define SIS5595_REG_TEMP_OVER (((data->revision) >= REV2MIN) ? \
115 SIS5595_REG_IN_MAX(4) : 0x39)
116#define SIS5595_REG_TEMP_HYST (((data->revision) >= REV2MIN) ? \
117 SIS5595_REG_IN_MIN(4) : 0x3a)
1da177e4
LT
118
119#define SIS5595_REG_CONFIG 0x40
120#define SIS5595_REG_ALARM1 0x41
121#define SIS5595_REG_ALARM2 0x42
122#define SIS5595_REG_FANDIV 0x47
123
8fda79ec
GR
124/*
125 * Conversions. Limit checking is only done on the TO_REG
126 * variants.
127 */
1da177e4 128
8fda79ec
GR
129/*
130 * IN: mV, (0V to 4.08V)
131 * REG: 16mV/bit
132 */
1da177e4
LT
133static inline u8 IN_TO_REG(unsigned long val)
134{
2a844c14 135 unsigned long nval = clamp_val(val, 0, 4080);
1da177e4
LT
136 return (nval + 8) / 16;
137}
138#define IN_FROM_REG(val) ((val) * 16)
139
140static inline u8 FAN_TO_REG(long rpm, int div)
141{
142 if (rpm <= 0)
143 return 255;
3806b45b
DC
144 if (rpm > 1350000)
145 return 1;
2a844c14 146 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
1da177e4
LT
147}
148
149static inline int FAN_FROM_REG(u8 val, int div)
150{
8fda79ec 151 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
1da177e4
LT
152}
153
8fda79ec
GR
154/*
155 * TEMP: mC (-54.12C to +157.53C)
156 * REG: 0.83C/bit + 52.12, two's complement
157 */
1da177e4
LT
158static inline int TEMP_FROM_REG(s8 val)
159{
160 return val * 830 + 52120;
161}
cc336546 162static inline s8 TEMP_TO_REG(long val)
1da177e4 163{
2a844c14 164 int nval = clamp_val(val, -54120, 157530) ;
8fda79ec 165 return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
1da177e4
LT
166}
167
8fda79ec
GR
168/*
169 * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
170 * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
171 */
1da177e4
LT
172static inline u8 DIV_TO_REG(int val)
173{
8fda79ec 174 return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
1da177e4
LT
175}
176#define DIV_FROM_REG(val) (1 << (val))
177
8fda79ec
GR
178/*
179 * For each registered chip, we need to keep some data in memory.
180 * The structure is dynamically allocated.
181 */
1da177e4 182struct sis5595_data {
17e7dc43
JD
183 unsigned short addr;
184 const char *name;
1beeffe4 185 struct device *hwmon_dev;
9a61bf63 186 struct mutex lock;
1da177e4 187
9a61bf63 188 struct mutex update_lock;
1da177e4
LT
189 char valid; /* !=0 if following fields are valid */
190 unsigned long last_updated; /* In jiffies */
191 char maxins; /* == 3 if temp enabled, otherwise == 4 */
192 u8 revision; /* Reg. value */
193
194 u8 in[5]; /* Register value */
195 u8 in_max[5]; /* Register value */
196 u8 in_min[5]; /* Register value */
197 u8 fan[2]; /* Register value */
198 u8 fan_min[2]; /* Register value */
199 s8 temp; /* Register value */
200 s8 temp_over; /* Register value */
201 s8 temp_hyst; /* Register value */
202 u8 fan_div[2]; /* Register encoding, shifted right */
203 u16 alarms; /* Register encoding, combined */
204};
205
206static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
207
17e7dc43 208static int sis5595_probe(struct platform_device *pdev);
281dfd0b 209static int sis5595_remove(struct platform_device *pdev);
1da177e4 210
17e7dc43
JD
211static int sis5595_read_value(struct sis5595_data *data, u8 reg);
212static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value);
1da177e4 213static struct sis5595_data *sis5595_update_device(struct device *dev);
17e7dc43 214static void sis5595_init_device(struct sis5595_data *data);
1da177e4 215
17e7dc43 216static struct platform_driver sis5595_driver = {
cdaf7934 217 .driver = {
cdaf7934
LR
218 .name = "sis5595",
219 },
17e7dc43 220 .probe = sis5595_probe,
9e5e9b7a 221 .remove = sis5595_remove,
1da177e4
LT
222};
223
224/* 4 Voltages */
1f5f48dd
JD
225static ssize_t show_in(struct device *dev, struct device_attribute *da,
226 char *buf)
1da177e4
LT
227{
228 struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dd
JD
229 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
230 int nr = attr->index;
1da177e4
LT
231 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
232}
233
1f5f48dd
JD
234static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
235 char *buf)
1da177e4
LT
236{
237 struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dd
JD
238 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
239 int nr = attr->index;
1da177e4
LT
240 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
241}
242
1f5f48dd
JD
243static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
244 char *buf)
1da177e4
LT
245{
246 struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dd
JD
247 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
248 int nr = attr->index;
1da177e4
LT
249 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
250}
251
1f5f48dd
JD
252static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
253 const char *buf, size_t count)
1da177e4 254{
17e7dc43 255 struct sis5595_data *data = dev_get_drvdata(dev);
1f5f48dd
JD
256 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
257 int nr = attr->index;
8fda79ec
GR
258 unsigned long val;
259 int err;
260
261 err = kstrtoul(buf, 10, &val);
262 if (err)
263 return err;
1da177e4 264
9a61bf63 265 mutex_lock(&data->update_lock);
1da177e4 266 data->in_min[nr] = IN_TO_REG(val);
17e7dc43 267 sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
9a61bf63 268 mutex_unlock(&data->update_lock);
1da177e4
LT
269 return count;
270}
271
1f5f48dd
JD
272static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
273 const char *buf, size_t count)
1da177e4 274{
17e7dc43 275 struct sis5595_data *data = dev_get_drvdata(dev);
1f5f48dd
JD
276 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
277 int nr = attr->index;
8fda79ec
GR
278 unsigned long val;
279 int err;
280
281 err = kstrtoul(buf, 10, &val);
282 if (err)
283 return err;
1da177e4 284
9a61bf63 285 mutex_lock(&data->update_lock);
1da177e4 286 data->in_max[nr] = IN_TO_REG(val);
17e7dc43 287 sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
9a61bf63 288 mutex_unlock(&data->update_lock);
1da177e4
LT
289 return count;
290}
291
292#define show_in_offset(offset) \
1f5f48dd
JD
293static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
294 show_in, NULL, offset); \
295static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
296 show_in_min, set_in_min, offset); \
297static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
298 show_in_max, set_in_max, offset);
1da177e4
LT
299
300show_in_offset(0);
301show_in_offset(1);
302show_in_offset(2);
303show_in_offset(3);
304show_in_offset(4);
305
306/* Temperature */
feaf8bd4
JL
307static ssize_t temp1_input_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
1da177e4
LT
309{
310 struct sis5595_data *data = sis5595_update_device(dev);
311 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
312}
313
feaf8bd4 314static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
8fda79ec 315 char *buf)
1da177e4
LT
316{
317 struct sis5595_data *data = sis5595_update_device(dev);
318 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
319}
320
feaf8bd4
JL
321static ssize_t temp1_max_store(struct device *dev,
322 struct device_attribute *attr, const char *buf,
323 size_t count)
1da177e4 324{
17e7dc43 325 struct sis5595_data *data = dev_get_drvdata(dev);
8fda79ec
GR
326 long val;
327 int err;
328
329 err = kstrtol(buf, 10, &val);
330 if (err)
331 return err;
1da177e4 332
9a61bf63 333 mutex_lock(&data->update_lock);
1da177e4 334 data->temp_over = TEMP_TO_REG(val);
17e7dc43 335 sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
9a61bf63 336 mutex_unlock(&data->update_lock);
1da177e4
LT
337 return count;
338}
339
feaf8bd4
JL
340static ssize_t temp1_max_hyst_show(struct device *dev,
341 struct device_attribute *attr, char *buf)
1da177e4
LT
342{
343 struct sis5595_data *data = sis5595_update_device(dev);
344 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
345}
346
feaf8bd4
JL
347static ssize_t temp1_max_hyst_store(struct device *dev,
348 struct device_attribute *attr,
349 const char *buf, size_t count)
1da177e4 350{
17e7dc43 351 struct sis5595_data *data = dev_get_drvdata(dev);
8fda79ec
GR
352 long val;
353 int err;
354
355 err = kstrtol(buf, 10, &val);
356 if (err)
357 return err;
1da177e4 358
9a61bf63 359 mutex_lock(&data->update_lock);
1da177e4 360 data->temp_hyst = TEMP_TO_REG(val);
17e7dc43 361 sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
9a61bf63 362 mutex_unlock(&data->update_lock);
1da177e4
LT
363 return count;
364}
365
feaf8bd4
JL
366static DEVICE_ATTR_RO(temp1_input);
367static DEVICE_ATTR_RW(temp1_max);
368static DEVICE_ATTR_RW(temp1_max_hyst);
1da177e4
LT
369
370/* 2 Fans */
1f5f48dd
JD
371static ssize_t show_fan(struct device *dev, struct device_attribute *da,
372 char *buf)
1da177e4
LT
373{
374 struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dd
JD
375 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
376 int nr = attr->index;
1da177e4 377 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
8fda79ec 378 DIV_FROM_REG(data->fan_div[nr])));
1da177e4
LT
379}
380
1f5f48dd
JD
381static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
382 char *buf)
1da177e4
LT
383{
384 struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dd
JD
385 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
386 int nr = attr->index;
8fda79ec
GR
387 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
388 DIV_FROM_REG(data->fan_div[nr])));
1da177e4
LT
389}
390
1f5f48dd
JD
391static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
392 const char *buf, size_t count)
1da177e4 393{
17e7dc43 394 struct sis5595_data *data = dev_get_drvdata(dev);
1f5f48dd
JD
395 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
396 int nr = attr->index;
8fda79ec
GR
397 unsigned long val;
398 int err;
399
400 err = kstrtoul(buf, 10, &val);
401 if (err)
402 return err;
1da177e4 403
9a61bf63 404 mutex_lock(&data->update_lock);
1da177e4 405 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
17e7dc43 406 sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 407 mutex_unlock(&data->update_lock);
1da177e4
LT
408 return count;
409}
410
1f5f48dd
JD
411static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
412 char *buf)
1da177e4
LT
413{
414 struct sis5595_data *data = sis5595_update_device(dev);
1f5f48dd
JD
415 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
416 int nr = attr->index;
8fda79ec 417 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
1da177e4
LT
418}
419
8fda79ec
GR
420/*
421 * Note: we save and restore the fan minimum here, because its value is
422 * determined in part by the fan divisor. This follows the principle of
423 * least surprise; the user doesn't expect the fan minimum to change just
424 * because the divisor changed.
425 */
1f5f48dd
JD
426static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
427 const char *buf, size_t count)
1da177e4 428{
17e7dc43 429 struct sis5595_data *data = dev_get_drvdata(dev);
1f5f48dd
JD
430 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
431 int nr = attr->index;
1da177e4 432 unsigned long min;
1da177e4 433 int reg;
8fda79ec
GR
434 unsigned long val;
435 int err;
436
437 err = kstrtoul(buf, 10, &val);
438 if (err)
439 return err;
1da177e4 440
9a61bf63 441 mutex_lock(&data->update_lock);
1da177e4
LT
442 min = FAN_FROM_REG(data->fan_min[nr],
443 DIV_FROM_REG(data->fan_div[nr]));
17e7dc43 444 reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
1da177e4
LT
445
446 switch (val) {
8fda79ec
GR
447 case 1:
448 data->fan_div[nr] = 0;
449 break;
450 case 2:
451 data->fan_div[nr] = 1;
452 break;
453 case 4:
454 data->fan_div[nr] = 2;
455 break;
456 case 8:
457 data->fan_div[nr] = 3;
458 break;
1da177e4 459 default:
b55f3757
GR
460 dev_err(dev,
461 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
462 val);
9a61bf63 463 mutex_unlock(&data->update_lock);
1da177e4
LT
464 return -EINVAL;
465 }
8fda79ec 466
1da177e4
LT
467 switch (nr) {
468 case 0:
469 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
470 break;
471 case 1:
472 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
473 break;
474 }
17e7dc43 475 sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
1da177e4
LT
476 data->fan_min[nr] =
477 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
17e7dc43 478 sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
9a61bf63 479 mutex_unlock(&data->update_lock);
1da177e4
LT
480 return count;
481}
482
483#define show_fan_offset(offset) \
1f5f48dd
JD
484static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
485 show_fan, NULL, offset - 1); \
486static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
487 show_fan_min, set_fan_min, offset - 1); \
488static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
489 show_fan_div, set_fan_div, offset - 1);
1da177e4
LT
490
491show_fan_offset(1);
492show_fan_offset(2);
493
1da177e4 494/* Alarms */
feaf8bd4 495static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
8fda79ec 496 char *buf)
1da177e4
LT
497{
498 struct sis5595_data *data = sis5595_update_device(dev);
499 return sprintf(buf, "%d\n", data->alarms);
500}
feaf8bd4 501static DEVICE_ATTR_RO(alarms);
a5ebe668 502
5c726b3b
IM
503static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
504 char *buf)
505{
506 struct sis5595_data *data = sis5595_update_device(dev);
507 int nr = to_sensor_dev_attr(da)->index;
508 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
509}
510static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
511static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
512static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
513static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
514static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 15);
515static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
516static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
517static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 15);
518
feaf8bd4 519static ssize_t name_show(struct device *dev, struct device_attribute *attr,
17e7dc43
JD
520 char *buf)
521{
522 struct sis5595_data *data = dev_get_drvdata(dev);
523 return sprintf(buf, "%s\n", data->name);
524}
feaf8bd4 525static DEVICE_ATTR_RO(name);
17e7dc43 526
a5ebe668 527static struct attribute *sis5595_attributes[] = {
1f5f48dd
JD
528 &sensor_dev_attr_in0_input.dev_attr.attr,
529 &sensor_dev_attr_in0_min.dev_attr.attr,
530 &sensor_dev_attr_in0_max.dev_attr.attr,
5c726b3b 531 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1f5f48dd
JD
532 &sensor_dev_attr_in1_input.dev_attr.attr,
533 &sensor_dev_attr_in1_min.dev_attr.attr,
534 &sensor_dev_attr_in1_max.dev_attr.attr,
5c726b3b 535 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1f5f48dd
JD
536 &sensor_dev_attr_in2_input.dev_attr.attr,
537 &sensor_dev_attr_in2_min.dev_attr.attr,
538 &sensor_dev_attr_in2_max.dev_attr.attr,
5c726b3b 539 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1f5f48dd
JD
540 &sensor_dev_attr_in3_input.dev_attr.attr,
541 &sensor_dev_attr_in3_min.dev_attr.attr,
542 &sensor_dev_attr_in3_max.dev_attr.attr,
5c726b3b 543 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1f5f48dd
JD
544
545 &sensor_dev_attr_fan1_input.dev_attr.attr,
546 &sensor_dev_attr_fan1_min.dev_attr.attr,
547 &sensor_dev_attr_fan1_div.dev_attr.attr,
5c726b3b 548 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1f5f48dd
JD
549 &sensor_dev_attr_fan2_input.dev_attr.attr,
550 &sensor_dev_attr_fan2_min.dev_attr.attr,
551 &sensor_dev_attr_fan2_div.dev_attr.attr,
5c726b3b 552 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
a5ebe668
JD
553
554 &dev_attr_alarms.attr,
17e7dc43 555 &dev_attr_name.attr,
a5ebe668
JD
556 NULL
557};
558
559static const struct attribute_group sis5595_group = {
560 .attrs = sis5595_attributes,
561};
562
76e63860 563static struct attribute *sis5595_attributes_in4[] = {
1f5f48dd
JD
564 &sensor_dev_attr_in4_input.dev_attr.attr,
565 &sensor_dev_attr_in4_min.dev_attr.attr,
566 &sensor_dev_attr_in4_max.dev_attr.attr,
5c726b3b 567 &sensor_dev_attr_in4_alarm.dev_attr.attr,
76e63860
IM
568 NULL
569};
570
571static const struct attribute_group sis5595_group_in4 = {
572 .attrs = sis5595_attributes_in4,
573};
a5ebe668 574
76e63860 575static struct attribute *sis5595_attributes_temp1[] = {
a5ebe668
JD
576 &dev_attr_temp1_input.attr,
577 &dev_attr_temp1_max.attr,
578 &dev_attr_temp1_max_hyst.attr,
5c726b3b 579 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
a5ebe668
JD
580 NULL
581};
582
76e63860
IM
583static const struct attribute_group sis5595_group_temp1 = {
584 .attrs = sis5595_attributes_temp1,
a5ebe668 585};
8fda79ec 586
1da177e4 587/* This is called when the module is loaded */
6c931ae1 588static int sis5595_probe(struct platform_device *pdev)
1da177e4
LT
589{
590 int err = 0;
591 int i;
1da177e4 592 struct sis5595_data *data;
17e7dc43 593 struct resource *res;
1da177e4 594 char val;
1da177e4 595
1da177e4 596 /* Reserve the ISA region */
17e7dc43 597 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
5d224ade
GR
598 if (!devm_request_region(&pdev->dev, res->start, SIS5595_EXTENT,
599 sis5595_driver.driver.name))
600 return -EBUSY;
1da177e4 601
5d224ade
GR
602 data = devm_kzalloc(&pdev->dev, sizeof(struct sis5595_data),
603 GFP_KERNEL);
604 if (!data)
605 return -ENOMEM;
1da177e4 606
9a61bf63 607 mutex_init(&data->lock);
17e7dc43
JD
608 mutex_init(&data->update_lock);
609 data->addr = res->start;
610 data->name = "sis5595";
611 platform_set_drvdata(pdev, data);
1da177e4 612
8fda79ec
GR
613 /*
614 * Check revision and pin registers to determine whether 4 or 5 voltages
615 */
7b6d1f04 616 data->revision = s_bridge->revision;
1da177e4
LT
617 /* 4 voltages, 1 temp */
618 data->maxins = 3;
619 if (data->revision >= REV2MIN) {
620 pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
621 if (!(val & 0x80))
622 /* 5 voltages, no temps */
623 data->maxins = 4;
624 }
8fda79ec 625
1da177e4 626 /* Initialize the SIS5595 chip */
17e7dc43 627 sis5595_init_device(data);
1da177e4
LT
628
629 /* A few vars need to be filled upon startup */
630 for (i = 0; i < 2; i++) {
17e7dc43 631 data->fan_min[i] = sis5595_read_value(data,
1da177e4
LT
632 SIS5595_REG_FAN_MIN(i));
633 }
634
635 /* Register sysfs hooks */
8fda79ec
GR
636 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
637 if (err)
5d224ade 638 return err;
a5ebe668 639 if (data->maxins == 4) {
8fda79ec
GR
640 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
641 if (err)
a5ebe668
JD
642 goto exit_remove_files;
643 } else {
8fda79ec
GR
644 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
645 if (err)
a5ebe668
JD
646 goto exit_remove_files;
647 }
648
1beeffe4
TJ
649 data->hwmon_dev = hwmon_device_register(&pdev->dev);
650 if (IS_ERR(data->hwmon_dev)) {
651 err = PTR_ERR(data->hwmon_dev);
a5ebe668 652 goto exit_remove_files;
943b0830
MH
653 }
654
1da177e4 655 return 0;
943b0830 656
a5ebe668 657exit_remove_files:
17e7dc43 658 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
76e63860
IM
659 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
660 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
1da177e4
LT
661 return err;
662}
663
281dfd0b 664static int sis5595_remove(struct platform_device *pdev)
1da177e4 665{
17e7dc43 666 struct sis5595_data *data = platform_get_drvdata(pdev);
1da177e4 667
1beeffe4 668 hwmon_device_unregister(data->hwmon_dev);
17e7dc43 669 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
76e63860
IM
670 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
671 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
1da177e4 672
1da177e4
LT
673 return 0;
674}
675
676
677/* ISA access must be locked explicitly. */
17e7dc43 678static int sis5595_read_value(struct sis5595_data *data, u8 reg)
1da177e4
LT
679{
680 int res;
681
9a61bf63 682 mutex_lock(&data->lock);
17e7dc43
JD
683 outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
684 res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
9a61bf63 685 mutex_unlock(&data->lock);
1da177e4
LT
686 return res;
687}
688
17e7dc43 689static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
1da177e4 690{
9a61bf63 691 mutex_lock(&data->lock);
17e7dc43
JD
692 outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
693 outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
9a61bf63 694 mutex_unlock(&data->lock);
1da177e4
LT
695}
696
697/* Called when we have found a new SIS5595. */
6c931ae1 698static void sis5595_init_device(struct sis5595_data *data)
1da177e4 699{
17e7dc43 700 u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
1da177e4 701 if (!(config & 0x01))
17e7dc43 702 sis5595_write_value(data, SIS5595_REG_CONFIG,
1da177e4
LT
703 (config & 0xf7) | 0x01);
704}
705
706static struct sis5595_data *sis5595_update_device(struct device *dev)
707{
17e7dc43 708 struct sis5595_data *data = dev_get_drvdata(dev);
1da177e4
LT
709 int i;
710
9a61bf63 711 mutex_lock(&data->update_lock);
1da177e4
LT
712
713 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
714 || !data->valid) {
715
716 for (i = 0; i <= data->maxins; i++) {
717 data->in[i] =
17e7dc43 718 sis5595_read_value(data, SIS5595_REG_IN(i));
1da177e4 719 data->in_min[i] =
17e7dc43 720 sis5595_read_value(data,
1da177e4
LT
721 SIS5595_REG_IN_MIN(i));
722 data->in_max[i] =
17e7dc43 723 sis5595_read_value(data,
1da177e4
LT
724 SIS5595_REG_IN_MAX(i));
725 }
726 for (i = 0; i < 2; i++) {
727 data->fan[i] =
17e7dc43 728 sis5595_read_value(data, SIS5595_REG_FAN(i));
1da177e4 729 data->fan_min[i] =
17e7dc43 730 sis5595_read_value(data,
1da177e4
LT
731 SIS5595_REG_FAN_MIN(i));
732 }
733 if (data->maxins == 3) {
734 data->temp =
17e7dc43 735 sis5595_read_value(data, SIS5595_REG_TEMP);
1da177e4 736 data->temp_over =
17e7dc43 737 sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
1da177e4 738 data->temp_hyst =
17e7dc43 739 sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
1da177e4 740 }
17e7dc43 741 i = sis5595_read_value(data, SIS5595_REG_FANDIV);
1da177e4
LT
742 data->fan_div[0] = (i >> 4) & 0x03;
743 data->fan_div[1] = i >> 6;
744 data->alarms =
17e7dc43
JD
745 sis5595_read_value(data, SIS5595_REG_ALARM1) |
746 (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
1da177e4
LT
747 data->last_updated = jiffies;
748 data->valid = 1;
749 }
750
9a61bf63 751 mutex_unlock(&data->update_lock);
1da177e4
LT
752
753 return data;
754}
755
cd9bb056 756static const struct pci_device_id sis5595_pci_ids[] = {
1da177e4
LT
757 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
758 { 0, }
759};
760
761MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
762
a5977246 763static int blacklist[] = {
1da177e4
LT
764 PCI_DEVICE_ID_SI_540,
765 PCI_DEVICE_ID_SI_550,
766 PCI_DEVICE_ID_SI_630,
767 PCI_DEVICE_ID_SI_645,
768 PCI_DEVICE_ID_SI_730,
769 PCI_DEVICE_ID_SI_735,
8fda79ec
GR
770 PCI_DEVICE_ID_SI_5511, /*
771 * 5513 chip has the 0008 device but
772 * that ID shows up in other chips so we
773 * use the 5511 ID for recognition
774 */
1da177e4
LT
775 PCI_DEVICE_ID_SI_5597,
776 PCI_DEVICE_ID_SI_5598,
777 0 };
778
6c931ae1 779static int sis5595_device_add(unsigned short address)
17e7dc43
JD
780{
781 struct resource res = {
782 .start = address,
783 .end = address + SIS5595_EXTENT - 1,
784 .name = "sis5595",
785 .flags = IORESOURCE_IO,
786 };
787 int err;
788
b9acb64a
JD
789 err = acpi_check_resource_conflict(&res);
790 if (err)
791 goto exit;
792
17e7dc43
JD
793 pdev = platform_device_alloc("sis5595", address);
794 if (!pdev) {
795 err = -ENOMEM;
4b2515db 796 pr_err("Device allocation failed\n");
17e7dc43
JD
797 goto exit;
798 }
799
800 err = platform_device_add_resources(pdev, &res, 1);
801 if (err) {
4b2515db 802 pr_err("Device resource addition failed (%d)\n", err);
17e7dc43
JD
803 goto exit_device_put;
804 }
805
806 err = platform_device_add(pdev);
807 if (err) {
4b2515db 808 pr_err("Device addition failed (%d)\n", err);
17e7dc43
JD
809 goto exit_device_put;
810 }
811
812 return 0;
813
814exit_device_put:
815 platform_device_put(pdev);
816exit:
817 return err;
818}
819
6c931ae1 820static int sis5595_pci_probe(struct pci_dev *dev,
1da177e4
LT
821 const struct pci_device_id *id)
822{
17e7dc43
JD
823 u16 address;
824 u8 enable;
1da177e4 825 int *i;
1da177e4
LT
826
827 for (i = blacklist; *i != 0; i++) {
5460a9d0 828 struct pci_dev *d;
8fda79ec
GR
829 d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
830 if (d) {
831 dev_err(&d->dev,
832 "Looked for SIS5595 but found unsupported device %.4x\n",
833 *i);
5460a9d0 834 pci_dev_put(d);
1da177e4
LT
835 return -ENODEV;
836 }
837 }
8fda79ec 838
17e7dc43
JD
839 force_addr &= ~(SIS5595_EXTENT - 1);
840 if (force_addr) {
841 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
842 pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
843 }
844
1da177e4 845 if (PCIBIOS_SUCCESSFUL !=
17e7dc43
JD
846 pci_read_config_word(dev, SIS5595_BASE_REG, &address)) {
847 dev_err(&dev->dev, "Failed to read ISA address\n");
1da177e4 848 return -ENODEV;
17e7dc43 849 }
8fda79ec 850
17e7dc43
JD
851 address &= ~(SIS5595_EXTENT - 1);
852 if (!address) {
8fda79ec
GR
853 dev_err(&dev->dev,
854 "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
1da177e4 855 return -ENODEV;
1da177e4 856 }
17e7dc43
JD
857 if (force_addr && address != force_addr) {
858 /* doesn't work for some chips? */
859 dev_err(&dev->dev, "Failed to force ISA address\n");
860 return -ENODEV;
861 }
1da177e4 862
17e7dc43
JD
863 if (PCIBIOS_SUCCESSFUL !=
864 pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable)) {
865 dev_err(&dev->dev, "Failed to read enable register\n");
866 return -ENODEV;
867 }
868 if (!(enable & 0x80)) {
869 if ((PCIBIOS_SUCCESSFUL !=
870 pci_write_config_byte(dev, SIS5595_ENABLE_REG,
871 enable | 0x80))
872 || (PCIBIOS_SUCCESSFUL !=
873 pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable))
874 || (!(enable & 0x80))) {
875 /* doesn't work for some chips! */
876 dev_err(&dev->dev, "Failed to enable HWM device\n");
877 return -ENODEV;
878 }
1da177e4
LT
879 }
880
17e7dc43
JD
881 if (platform_driver_register(&sis5595_driver)) {
882 dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
883 goto exit;
884 }
885
886 s_bridge = pci_dev_get(dev);
887 /* Sets global pdev as a side effect */
888 if (sis5595_device_add(address))
889 goto exit_unregister;
890
8fda79ec
GR
891 /*
892 * Always return failure here. This is to allow other drivers to bind
1da177e4
LT
893 * to this pci device. We don't really want to have control over the
894 * pci device, we only wanted to read as few register values from it.
895 */
896 return -ENODEV;
17e7dc43
JD
897
898exit_unregister:
899 pci_dev_put(dev);
900 platform_driver_unregister(&sis5595_driver);
901exit:
902 return -ENODEV;
1da177e4
LT
903}
904
905static struct pci_driver sis5595_pci_driver = {
906 .name = "sis5595",
907 .id_table = sis5595_pci_ids,
908 .probe = sis5595_pci_probe,
909};
910
911static int __init sm_sis5595_init(void)
912{
913 return pci_register_driver(&sis5595_pci_driver);
914}
915
916static void __exit sm_sis5595_exit(void)
917{
918 pci_unregister_driver(&sis5595_pci_driver);
919 if (s_bridge != NULL) {
17e7dc43
JD
920 platform_device_unregister(pdev);
921 platform_driver_unregister(&sis5595_driver);
1da177e4
LT
922 pci_dev_put(s_bridge);
923 s_bridge = NULL;
924 }
925}
926
927MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
928MODULE_DESCRIPTION("SiS 5595 Sensor device");
929MODULE_LICENSE("GPL");
930
931module_init(sm_sis5595_init);
932module_exit(sm_sis5595_exit);