blob: 641be1f7f9cd1226e1c469bcb0d72892b939ce9e [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Guenter Roeck8fda79e2012-01-14 22:21:41 -08003 * sis5595.c - Part of lm_sensors, Linux kernel modules
4 * for hardware monitoring
5 *
6 * Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
7 * Kyösti Mälkki <kmalkki@cc.hut.fi>, and
8 * Mark D. Studebaker <mdsxyz123@yahoo.com>
9 * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
Jean Delvare7c81c60f2014-01-29 20:40:08 +010010 * the help of Jean Delvare <jdelvare@suse.de>
Guenter Roeck8fda79e2012-01-14 22:21:41 -080011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13/*
Guenter Roeck8fda79e2012-01-14 22:21:41 -080014 * SiS southbridge has a LM78-like chip integrated on the same IC.
15 * This driver is a customized copy of lm78.c
16 *
17 * Supports following revisions:
18 * Version PCI ID PCI Revision
19 * 1 1039/0008 AF or less
20 * 2 1039/0008 B0 or greater
21 *
22 * Note: these chips contain a 0008 device which is incompatible with the
23 * 5595. We recognize these by the presence of the listed
24 * "blacklist" PCI ID and refuse to load.
25 *
26 * NOT SUPPORTED PCI ID BLACKLIST PCI ID
27 * 540 0008 0540
28 * 550 0008 0550
29 * 5513 0008 5511
30 * 5581 0008 5597
31 * 5582 0008 5597
32 * 5597 0008 5597
33 * 5598 0008 5597/5598
34 * 630 0008 0630
35 * 645 0008 0645
36 * 730 0008 0730
37 * 735 0008 0735
38 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Uwe Kleine-König847a3b02022-09-22 09:48:59 +020040#define DRIVER_NAME "sis5595"
Joe Perches4b2515d2010-10-20 06:51:47 +000041#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/module.h>
44#include <linux/slab.h>
45#include <linux/ioport.h>
46#include <linux/pci.h>
Jean Delvare17e7dc42007-06-09 10:11:16 -040047#include <linux/platform_device.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040048#include <linux/hwmon.h>
Jean Delvare1f5f48d2007-06-09 10:11:16 -040049#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040050#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/init.h>
Dominik Hacklff324092005-05-16 18:12:18 +020052#include <linux/jiffies.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010053#include <linux/mutex.h>
Jean Delvarea5ebe662006-09-24 21:24:46 +020054#include <linux/sysfs.h>
Jean Delvareb9acb642009-01-07 16:37:35 +010055#include <linux/acpi.h>
H Hartley Sweeten6055fae2009-09-15 17:18:13 +020056#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Guenter Roeck8fda79e2012-01-14 22:21:41 -080058/*
59 * If force_addr is set to anything different from 0, we forcibly enable
60 * the device at the given address.
61 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static u16 force_addr;
63module_param(force_addr, ushort, 0);
64MODULE_PARM_DESC(force_addr,
65 "Initialize the base address of the sensors");
66
Jean Delvare17e7dc42007-06-09 10:11:16 -040067static struct platform_device *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* Many SIS5595 constants specified below */
70
71/* Length of ISA address segment */
72#define SIS5595_EXTENT 8
73/* PCI Config Registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SIS5595_BASE_REG 0x68
75#define SIS5595_PIN_REG 0x7A
76#define SIS5595_ENABLE_REG 0x7B
77
78/* Where are the ISA address/data registers relative to the base address */
79#define SIS5595_ADDR_REG_OFFSET 5
80#define SIS5595_DATA_REG_OFFSET 6
81
82/* The SIS5595 registers */
83#define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
84#define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
85#define SIS5595_REG_IN(nr) (0x20 + (nr))
86
87#define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
88#define SIS5595_REG_FAN(nr) (0x28 + (nr))
89
Guenter Roeck8fda79e2012-01-14 22:21:41 -080090/*
91 * On the first version of the chip, the temp registers are separate.
92 * On the second version,
93 * TEMP pin is shared with IN4, configured in PCI register 0x7A.
94 * The registers are the same as well.
95 * OVER and HYST are really MAX and MIN.
96 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#define REV2MIN 0xb0
Guenter Roeck8fda79e2012-01-14 22:21:41 -080099#define SIS5595_REG_TEMP (((data->revision) >= REV2MIN) ? \
100 SIS5595_REG_IN(4) : 0x27)
101#define SIS5595_REG_TEMP_OVER (((data->revision) >= REV2MIN) ? \
102 SIS5595_REG_IN_MAX(4) : 0x39)
103#define SIS5595_REG_TEMP_HYST (((data->revision) >= REV2MIN) ? \
104 SIS5595_REG_IN_MIN(4) : 0x3a)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#define SIS5595_REG_CONFIG 0x40
107#define SIS5595_REG_ALARM1 0x41
108#define SIS5595_REG_ALARM2 0x42
109#define SIS5595_REG_FANDIV 0x47
110
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800111/*
112 * Conversions. Limit checking is only done on the TO_REG
113 * variants.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800116/*
117 * IN: mV, (0V to 4.08V)
118 * REG: 16mV/bit
119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static inline u8 IN_TO_REG(unsigned long val)
121{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800122 unsigned long nval = clamp_val(val, 0, 4080);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return (nval + 8) / 16;
124}
125#define IN_FROM_REG(val) ((val) * 16)
126
127static inline u8 FAN_TO_REG(long rpm, int div)
128{
129 if (rpm <= 0)
130 return 255;
Dan Carpenter3806b452013-12-12 08:05:33 +0100131 if (rpm > 1350000)
132 return 1;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800133 return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
136static inline int FAN_FROM_REG(u8 val, int div)
137{
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800138 return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800141/*
142 * TEMP: mC (-54.12C to +157.53C)
143 * REG: 0.83C/bit + 52.12, two's complement
144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static inline int TEMP_FROM_REG(s8 val)
146{
147 return val * 830 + 52120;
148}
Axel Lincc336542014-07-31 22:27:04 +0800149static inline s8 TEMP_TO_REG(long val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800151 int nval = clamp_val(val, -54120, 157530) ;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800152 return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800155/*
156 * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
157 * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static inline u8 DIV_TO_REG(int val)
160{
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800161 return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163#define DIV_FROM_REG(val) (1 << (val))
164
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800165/*
166 * For each registered chip, we need to keep some data in memory.
167 * The structure is dynamically allocated.
168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169struct sis5595_data {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400170 unsigned short addr;
171 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700172 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100173 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100175 struct mutex update_lock;
Paul Fertser952a11c2021-09-24 22:52:02 +0300176 bool valid; /* true if following fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned long last_updated; /* In jiffies */
178 char maxins; /* == 3 if temp enabled, otherwise == 4 */
179 u8 revision; /* Reg. value */
180
181 u8 in[5]; /* Register value */
182 u8 in_max[5]; /* Register value */
183 u8 in_min[5]; /* Register value */
184 u8 fan[2]; /* Register value */
185 u8 fan_min[2]; /* Register value */
186 s8 temp; /* Register value */
187 s8 temp_over; /* Register value */
188 s8 temp_hyst; /* Register value */
189 u8 fan_div[2]; /* Register encoding, shifted right */
190 u16 alarms; /* Register encoding, combined */
191};
192
193static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
194
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200195/* ISA access must be locked explicitly. */
196static int sis5595_read_value(struct sis5595_data *data, u8 reg)
197{
198 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200200 mutex_lock(&data->lock);
201 outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
202 res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
203 mutex_unlock(&data->lock);
204 return res;
205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200207static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
208{
209 mutex_lock(&data->lock);
210 outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
211 outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
212 mutex_unlock(&data->lock);
213}
214
215static struct sis5595_data *sis5595_update_device(struct device *dev)
216{
217 struct sis5595_data *data = dev_get_drvdata(dev);
218 int i;
219
220 mutex_lock(&data->update_lock);
221
222 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
223 || !data->valid) {
224
225 for (i = 0; i <= data->maxins; i++) {
226 data->in[i] =
227 sis5595_read_value(data, SIS5595_REG_IN(i));
228 data->in_min[i] =
229 sis5595_read_value(data,
230 SIS5595_REG_IN_MIN(i));
231 data->in_max[i] =
232 sis5595_read_value(data,
233 SIS5595_REG_IN_MAX(i));
234 }
235 for (i = 0; i < 2; i++) {
236 data->fan[i] =
237 sis5595_read_value(data, SIS5595_REG_FAN(i));
238 data->fan_min[i] =
239 sis5595_read_value(data,
240 SIS5595_REG_FAN_MIN(i));
241 }
242 if (data->maxins == 3) {
243 data->temp =
244 sis5595_read_value(data, SIS5595_REG_TEMP);
245 data->temp_over =
246 sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
247 data->temp_hyst =
248 sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
249 }
250 i = sis5595_read_value(data, SIS5595_REG_FANDIV);
251 data->fan_div[0] = (i >> 4) & 0x03;
252 data->fan_div[1] = i >> 6;
253 data->alarms =
254 sis5595_read_value(data, SIS5595_REG_ALARM1) |
255 (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
256 data->last_updated = jiffies;
257 data->valid = true;
258 }
259
260 mutex_unlock(&data->update_lock);
261
262 return data;
263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265/* 4 Voltages */
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800266static ssize_t in_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400267 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400270 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
271 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
273}
274
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800275static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400276 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400279 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
280 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
282}
283
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800284static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400285 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400288 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
289 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
291}
292
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800293static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
294 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400296 struct sis5595_data *data = dev_get_drvdata(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400297 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
298 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800299 unsigned long val;
300 int err;
301
302 err = kstrtoul(buf, 10, &val);
303 if (err)
304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100306 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 data->in_min[nr] = IN_TO_REG(val);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400308 sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100309 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return count;
311}
312
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800313static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
314 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400316 struct sis5595_data *data = dev_get_drvdata(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400317 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
318 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800319 unsigned long val;
320 int err;
321
322 err = kstrtoul(buf, 10, &val);
323 if (err)
324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100326 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 data->in_max[nr] = IN_TO_REG(val);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400328 sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100329 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return count;
331}
332
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800333static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
334static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
335static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
336static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
337static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
338static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
339static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
340static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
341static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
342static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
343static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
344static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
345static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
346static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
347static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349/* Temperature */
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100350static ssize_t temp1_input_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct sis5595_data *data = sis5595_update_device(dev);
354 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
355}
356
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100357static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800358 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct sis5595_data *data = sis5595_update_device(dev);
361 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
362}
363
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100364static ssize_t temp1_max_store(struct device *dev,
365 struct device_attribute *attr, const char *buf,
366 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400368 struct sis5595_data *data = dev_get_drvdata(dev);
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800369 long val;
370 int err;
371
372 err = kstrtol(buf, 10, &val);
373 if (err)
374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100376 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 data->temp_over = TEMP_TO_REG(val);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400378 sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100379 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return count;
381}
382
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100383static ssize_t temp1_max_hyst_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct sis5595_data *data = sis5595_update_device(dev);
387 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
388}
389
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100390static ssize_t temp1_max_hyst_store(struct device *dev,
391 struct device_attribute *attr,
392 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400394 struct sis5595_data *data = dev_get_drvdata(dev);
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800395 long val;
396 int err;
397
398 err = kstrtol(buf, 10, &val);
399 if (err)
400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100402 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 data->temp_hyst = TEMP_TO_REG(val);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400404 sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100405 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return count;
407}
408
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100409static DEVICE_ATTR_RO(temp1_input);
410static DEVICE_ATTR_RW(temp1_max);
411static DEVICE_ATTR_RW(temp1_max_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/* 2 Fans */
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800414static ssize_t fan_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400415 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400418 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
419 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800421 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800424static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400425 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400428 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
429 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800430 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
431 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800434static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
435 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400437 struct sis5595_data *data = dev_get_drvdata(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400438 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
439 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800440 unsigned long val;
441 int err;
442
443 err = kstrtoul(buf, 10, &val);
444 if (err)
445 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100447 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare17e7dc42007-06-09 10:11:16 -0400449 sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100450 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return count;
452}
453
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800454static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400455 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
457 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400458 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
459 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800460 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800463/*
464 * Note: we save and restore the fan minimum here, because its value is
465 * determined in part by the fan divisor. This follows the principle of
466 * least surprise; the user doesn't expect the fan minimum to change just
467 * because the divisor changed.
468 */
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800469static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
470 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400472 struct sis5595_data *data = dev_get_drvdata(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400473 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
474 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 unsigned long min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int reg;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800477 unsigned long val;
478 int err;
479
480 err = kstrtoul(buf, 10, &val);
481 if (err)
482 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100484 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 min = FAN_FROM_REG(data->fan_min[nr],
486 DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare17e7dc42007-06-09 10:11:16 -0400487 reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 switch (val) {
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800490 case 1:
491 data->fan_div[nr] = 0;
492 break;
493 case 2:
494 data->fan_div[nr] = 1;
495 break;
496 case 4:
497 data->fan_div[nr] = 2;
498 break;
499 case 8:
500 data->fan_div[nr] = 3;
501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 default:
Guenter Roeckb55f3752013-01-10 10:01:24 -0800503 dev_err(dev,
504 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
505 val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100506 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return -EINVAL;
508 }
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 switch (nr) {
511 case 0:
512 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
513 break;
514 case 1:
515 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
516 break;
517 }
Jean Delvare17e7dc42007-06-09 10:11:16 -0400518 sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 data->fan_min[nr] =
520 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare17e7dc42007-06-09 10:11:16 -0400521 sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100522 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return count;
524}
525
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800526static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
527static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
528static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
529static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
530static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
531static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/* Alarms */
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100534static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800535 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct sis5595_data *data = sis5595_update_device(dev);
538 return sprintf(buf, "%d\n", data->alarms);
539}
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100540static DEVICE_ATTR_RO(alarms);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200541
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800542static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
Ivo Manca5c726b32007-10-15 20:50:53 +0200543 char *buf)
544{
545 struct sis5595_data *data = sis5595_update_device(dev);
546 int nr = to_sensor_dev_attr(da)->index;
547 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
548}
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800549static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
550static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
551static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
552static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
553static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 15);
554static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
555static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
556static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 15);
Ivo Manca5c726b32007-10-15 20:50:53 +0200557
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100558static ssize_t name_show(struct device *dev, struct device_attribute *attr,
Jean Delvare17e7dc42007-06-09 10:11:16 -0400559 char *buf)
560{
561 struct sis5595_data *data = dev_get_drvdata(dev);
562 return sprintf(buf, "%s\n", data->name);
563}
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100564static DEVICE_ATTR_RO(name);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400565
Jean Delvarea5ebe662006-09-24 21:24:46 +0200566static struct attribute *sis5595_attributes[] = {
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400567 &sensor_dev_attr_in0_input.dev_attr.attr,
568 &sensor_dev_attr_in0_min.dev_attr.attr,
569 &sensor_dev_attr_in0_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200570 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400571 &sensor_dev_attr_in1_input.dev_attr.attr,
572 &sensor_dev_attr_in1_min.dev_attr.attr,
573 &sensor_dev_attr_in1_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200574 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400575 &sensor_dev_attr_in2_input.dev_attr.attr,
576 &sensor_dev_attr_in2_min.dev_attr.attr,
577 &sensor_dev_attr_in2_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200578 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400579 &sensor_dev_attr_in3_input.dev_attr.attr,
580 &sensor_dev_attr_in3_min.dev_attr.attr,
581 &sensor_dev_attr_in3_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200582 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200583
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400584 &sensor_dev_attr_fan1_input.dev_attr.attr,
585 &sensor_dev_attr_fan1_min.dev_attr.attr,
586 &sensor_dev_attr_fan1_div.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200587 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400588 &sensor_dev_attr_fan2_input.dev_attr.attr,
589 &sensor_dev_attr_fan2_min.dev_attr.attr,
590 &sensor_dev_attr_fan2_div.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200591 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200592
593 &dev_attr_alarms.attr,
Jean Delvare17e7dc42007-06-09 10:11:16 -0400594 &dev_attr_name.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200595 NULL
596};
597
598static const struct attribute_group sis5595_group = {
599 .attrs = sis5595_attributes,
600};
601
Ivo Manca76e63862007-10-15 13:27:13 +0200602static struct attribute *sis5595_attributes_in4[] = {
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400603 &sensor_dev_attr_in4_input.dev_attr.attr,
604 &sensor_dev_attr_in4_min.dev_attr.attr,
605 &sensor_dev_attr_in4_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200606 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Ivo Manca76e63862007-10-15 13:27:13 +0200607 NULL
608};
Jean Delvarea5ebe662006-09-24 21:24:46 +0200609
Ivo Manca76e63862007-10-15 13:27:13 +0200610static const struct attribute_group sis5595_group_in4 = {
611 .attrs = sis5595_attributes_in4,
612};
613
614static struct attribute *sis5595_attributes_temp1[] = {
Jean Delvarea5ebe662006-09-24 21:24:46 +0200615 &dev_attr_temp1_input.attr,
616 &dev_attr_temp1_max.attr,
617 &dev_attr_temp1_max_hyst.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200618 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200619 NULL
620};
621
Ivo Manca76e63862007-10-15 13:27:13 +0200622static const struct attribute_group sis5595_group_temp1 = {
623 .attrs = sis5595_attributes_temp1,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200624};
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800625
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200626/* Called when we have found a new SIS5595. */
627static void sis5595_init_device(struct sis5595_data *data)
628{
629 u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
630 if (!(config & 0x01))
631 sis5595_write_value(data, SIS5595_REG_CONFIG,
632 (config & 0xf7) | 0x01);
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/* This is called when the module is loaded */
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500636static int sis5595_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 int err = 0;
639 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct sis5595_data *data;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400641 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /* Reserve the ISA region */
Jean Delvare17e7dc42007-06-09 10:11:16 -0400645 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck5d224ad2012-06-02 11:20:23 -0700646 if (!devm_request_region(&pdev->dev, res->start, SIS5595_EXTENT,
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200647 DRIVER_NAME))
Guenter Roeck5d224ad2012-06-02 11:20:23 -0700648 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Guenter Roeck5d224ad2012-06-02 11:20:23 -0700650 data = devm_kzalloc(&pdev->dev, sizeof(struct sis5595_data),
651 GFP_KERNEL);
652 if (!data)
653 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100655 mutex_init(&data->lock);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400656 mutex_init(&data->update_lock);
657 data->addr = res->start;
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200658 data->name = DRIVER_NAME;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400659 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800661 /*
662 * Check revision and pin registers to determine whether 4 or 5 voltages
663 */
Auke Kok7b6d1f02007-08-27 16:17:01 -0700664 data->revision = s_bridge->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* 4 voltages, 1 temp */
666 data->maxins = 3;
667 if (data->revision >= REV2MIN) {
668 pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
669 if (!(val & 0x80))
670 /* 5 voltages, no temps */
671 data->maxins = 4;
672 }
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* Initialize the SIS5595 chip */
Jean Delvare17e7dc42007-06-09 10:11:16 -0400675 sis5595_init_device(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /* A few vars need to be filled upon startup */
678 for (i = 0; i < 2; i++) {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400679 data->fan_min[i] = sis5595_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 SIS5595_REG_FAN_MIN(i));
681 }
682
683 /* Register sysfs hooks */
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800684 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
685 if (err)
Guenter Roeck5d224ad2012-06-02 11:20:23 -0700686 return err;
Jean Delvarea5ebe662006-09-24 21:24:46 +0200687 if (data->maxins == 4) {
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800688 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
689 if (err)
Jean Delvarea5ebe662006-09-24 21:24:46 +0200690 goto exit_remove_files;
691 } else {
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800692 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
693 if (err)
Jean Delvarea5ebe662006-09-24 21:24:46 +0200694 goto exit_remove_files;
695 }
696
Tony Jones1beeffe2007-08-20 13:46:20 -0700697 data->hwmon_dev = hwmon_device_register(&pdev->dev);
698 if (IS_ERR(data->hwmon_dev)) {
699 err = PTR_ERR(data->hwmon_dev);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200700 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400704
Jean Delvarea5ebe662006-09-24 21:24:46 +0200705exit_remove_files:
Jean Delvare17e7dc42007-06-09 10:11:16 -0400706 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
Ivo Manca76e63862007-10-15 13:27:13 +0200707 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
708 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return err;
710}
711
Uwe Kleine-König39797752023-09-18 10:59:43 +0200712static void sis5595_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400714 struct sis5595_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Tony Jones1beeffe2007-08-20 13:46:20 -0700716 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400717 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
Ivo Manca76e63862007-10-15 13:27:13 +0200718 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
719 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Jingoo Hancd9bb052013-12-03 07:10:29 +0000722static const struct pci_device_id sis5595_pci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
724 { 0, }
725};
726
727MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
728
Bill Pembertona5977242012-11-19 13:24:37 -0500729static int blacklist[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 PCI_DEVICE_ID_SI_540,
731 PCI_DEVICE_ID_SI_550,
732 PCI_DEVICE_ID_SI_630,
733 PCI_DEVICE_ID_SI_645,
734 PCI_DEVICE_ID_SI_730,
735 PCI_DEVICE_ID_SI_735,
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800736 PCI_DEVICE_ID_SI_5511, /*
737 * 5513 chip has the 0008 device but
738 * that ID shows up in other chips so we
739 * use the 5511 ID for recognition
740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 PCI_DEVICE_ID_SI_5597,
742 PCI_DEVICE_ID_SI_5598,
743 0 };
744
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500745static int sis5595_device_add(unsigned short address)
Jean Delvare17e7dc42007-06-09 10:11:16 -0400746{
747 struct resource res = {
748 .start = address,
749 .end = address + SIS5595_EXTENT - 1,
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200750 .name = DRIVER_NAME,
Jean Delvare17e7dc42007-06-09 10:11:16 -0400751 .flags = IORESOURCE_IO,
752 };
753 int err;
754
Jean Delvareb9acb642009-01-07 16:37:35 +0100755 err = acpi_check_resource_conflict(&res);
756 if (err)
757 goto exit;
758
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200759 pdev = platform_device_alloc(DRIVER_NAME, address);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400760 if (!pdev) {
761 err = -ENOMEM;
Joe Perches4b2515d2010-10-20 06:51:47 +0000762 pr_err("Device allocation failed\n");
Jean Delvare17e7dc42007-06-09 10:11:16 -0400763 goto exit;
764 }
765
766 err = platform_device_add_resources(pdev, &res, 1);
767 if (err) {
Joe Perches4b2515d2010-10-20 06:51:47 +0000768 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400769 goto exit_device_put;
770 }
771
772 err = platform_device_add(pdev);
773 if (err) {
Joe Perches4b2515d2010-10-20 06:51:47 +0000774 pr_err("Device addition failed (%d)\n", err);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400775 goto exit_device_put;
776 }
777
778 return 0;
779
780exit_device_put:
781 platform_device_put(pdev);
782exit:
783 return err;
784}
785
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200786static struct platform_driver sis5595_driver = {
787 .driver = {
788 .name = DRIVER_NAME,
789 },
790 .probe = sis5595_probe,
Uwe Kleine-König39797752023-09-18 10:59:43 +0200791 .remove_new = sis5595_remove,
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200792};
793
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500794static int sis5595_pci_probe(struct pci_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 const struct pci_device_id *id)
796{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400797 u16 address;
798 u8 enable;
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300799 int *i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 for (i = blacklist; *i != 0; i++) {
Mark M. Hoffman5460a9d2007-10-14 14:57:35 -0400802 struct pci_dev *d;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800803 d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
804 if (d) {
805 dev_err(&d->dev,
806 "Looked for SIS5595 but found unsupported device %.4x\n",
807 *i);
Mark M. Hoffman5460a9d2007-10-14 14:57:35 -0400808 pci_dev_put(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return -ENODEV;
810 }
811 }
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800812
Jean Delvare17e7dc42007-06-09 10:11:16 -0400813 force_addr &= ~(SIS5595_EXTENT - 1);
814 if (force_addr) {
815 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
816 pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
817 }
818
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300819 err = pci_read_config_word(dev, SIS5595_BASE_REG, &address);
820 if (err != PCIBIOS_SUCCESSFUL) {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400821 dev_err(&dev->dev, "Failed to read ISA address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return -ENODEV;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400823 }
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800824
Jean Delvare17e7dc42007-06-09 10:11:16 -0400825 address &= ~(SIS5595_EXTENT - 1);
826 if (!address) {
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800827 dev_err(&dev->dev,
828 "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return -ENODEV;
830 }
Jean Delvare17e7dc42007-06-09 10:11:16 -0400831 if (force_addr && address != force_addr) {
832 /* doesn't work for some chips? */
833 dev_err(&dev->dev, "Failed to force ISA address\n");
834 return -ENODEV;
835 }
836
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300837 err = pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable);
838 if (err != PCIBIOS_SUCCESSFUL) {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400839 dev_err(&dev->dev, "Failed to read enable register\n");
840 return -ENODEV;
841 }
842 if (!(enable & 0x80)) {
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300843 err = pci_write_config_byte(dev, SIS5595_ENABLE_REG, enable | 0x80);
844 if (err != PCIBIOS_SUCCESSFUL)
845 goto enable_fail;
846
847 err = pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable);
848 if (err != PCIBIOS_SUCCESSFUL)
849 goto enable_fail;
850
851 /* doesn't work for some chips! */
852 if (!(enable & 0x80))
853 goto enable_fail;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400854 }
855
856 if (platform_driver_register(&sis5595_driver)) {
857 dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
858 goto exit;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 s_bridge = pci_dev_get(dev);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400862 /* Sets global pdev as a side effect */
863 if (sis5595_device_add(address))
864 goto exit_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800866 /*
867 * Always return failure here. This is to allow other drivers to bind
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * to this pci device. We don't really want to have control over the
869 * pci device, we only wanted to read as few register values from it.
870 */
871 return -ENODEV;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400872
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300873enable_fail:
874 dev_err(&dev->dev, "Failed to enable HWM device\n");
875 goto exit;
876
Jean Delvare17e7dc42007-06-09 10:11:16 -0400877exit_unregister:
878 pci_dev_put(dev);
879 platform_driver_unregister(&sis5595_driver);
880exit:
881 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884static struct pci_driver sis5595_pci_driver = {
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200885 .name = DRIVER_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 .id_table = sis5595_pci_ids,
887 .probe = sis5595_pci_probe,
888};
889
890static int __init sm_sis5595_init(void)
891{
892 return pci_register_driver(&sis5595_pci_driver);
893}
894
895static void __exit sm_sis5595_exit(void)
896{
897 pci_unregister_driver(&sis5595_pci_driver);
898 if (s_bridge != NULL) {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400899 platform_device_unregister(pdev);
900 platform_driver_unregister(&sis5595_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 pci_dev_put(s_bridge);
902 s_bridge = NULL;
903 }
904}
905
906MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
907MODULE_DESCRIPTION("SiS 5595 Sensor device");
908MODULE_LICENSE("GPL");
909
910module_init(sm_sis5595_init);
911module_exit(sm_sis5595_exit);