blob: e73b1522f3cefaa8b0fa8f318ca7d4a0eeb3e13e [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/*
Krzysztof Kozlowski5b1d7a02024-02-25 21:28:41 +0100156 * FAN DIV: 1, 2, 4, or 8
157 * REG: 0, 1, 2, or 3 (respectively)
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define DIV_FROM_REG(val) (1 << (val))
160
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800161/*
162 * For each registered chip, we need to keep some data in memory.
163 * The structure is dynamically allocated.
164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165struct sis5595_data {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400166 unsigned short addr;
167 const char *name;
Tony Jones1beeffe2007-08-20 13:46:20 -0700168 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100169 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100171 struct mutex update_lock;
Paul Fertser952a11c2021-09-24 22:52:02 +0300172 bool valid; /* true if following fields are valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned long last_updated; /* In jiffies */
174 char maxins; /* == 3 if temp enabled, otherwise == 4 */
175 u8 revision; /* Reg. value */
176
177 u8 in[5]; /* Register value */
178 u8 in_max[5]; /* Register value */
179 u8 in_min[5]; /* Register value */
180 u8 fan[2]; /* Register value */
181 u8 fan_min[2]; /* Register value */
182 s8 temp; /* Register value */
183 s8 temp_over; /* Register value */
184 s8 temp_hyst; /* Register value */
185 u8 fan_div[2]; /* Register encoding, shifted right */
186 u16 alarms; /* Register encoding, combined */
187};
188
189static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
190
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200191/* ISA access must be locked explicitly. */
192static int sis5595_read_value(struct sis5595_data *data, u8 reg)
193{
194 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200196 mutex_lock(&data->lock);
197 outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
198 res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
199 mutex_unlock(&data->lock);
200 return res;
201}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200203static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
204{
205 mutex_lock(&data->lock);
206 outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
207 outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
208 mutex_unlock(&data->lock);
209}
210
211static struct sis5595_data *sis5595_update_device(struct device *dev)
212{
213 struct sis5595_data *data = dev_get_drvdata(dev);
214 int i;
215
216 mutex_lock(&data->update_lock);
217
218 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
219 || !data->valid) {
220
221 for (i = 0; i <= data->maxins; i++) {
222 data->in[i] =
223 sis5595_read_value(data, SIS5595_REG_IN(i));
224 data->in_min[i] =
225 sis5595_read_value(data,
226 SIS5595_REG_IN_MIN(i));
227 data->in_max[i] =
228 sis5595_read_value(data,
229 SIS5595_REG_IN_MAX(i));
230 }
231 for (i = 0; i < 2; i++) {
232 data->fan[i] =
233 sis5595_read_value(data, SIS5595_REG_FAN(i));
234 data->fan_min[i] =
235 sis5595_read_value(data,
236 SIS5595_REG_FAN_MIN(i));
237 }
238 if (data->maxins == 3) {
239 data->temp =
240 sis5595_read_value(data, SIS5595_REG_TEMP);
241 data->temp_over =
242 sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
243 data->temp_hyst =
244 sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
245 }
246 i = sis5595_read_value(data, SIS5595_REG_FANDIV);
247 data->fan_div[0] = (i >> 4) & 0x03;
248 data->fan_div[1] = i >> 6;
249 data->alarms =
250 sis5595_read_value(data, SIS5595_REG_ALARM1) |
251 (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
252 data->last_updated = jiffies;
253 data->valid = true;
254 }
255
256 mutex_unlock(&data->update_lock);
257
258 return data;
259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261/* 4 Voltages */
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800262static ssize_t in_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400263 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400266 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
267 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
269}
270
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800271static ssize_t in_min_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400272 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400275 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
276 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
278}
279
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800280static ssize_t in_max_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400281 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400284 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
285 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
287}
288
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800289static ssize_t in_min_store(struct device *dev, struct device_attribute *da,
290 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400292 struct sis5595_data *data = dev_get_drvdata(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400293 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
294 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800295 unsigned long val;
296 int err;
297
298 err = kstrtoul(buf, 10, &val);
299 if (err)
300 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100302 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 data->in_min[nr] = IN_TO_REG(val);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400304 sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100305 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return count;
307}
308
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800309static ssize_t in_max_store(struct device *dev, struct device_attribute *da,
310 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400312 struct sis5595_data *data = dev_get_drvdata(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400313 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
314 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800315 unsigned long val;
316 int err;
317
318 err = kstrtoul(buf, 10, &val);
319 if (err)
320 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100322 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 data->in_max[nr] = IN_TO_REG(val);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400324 sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100325 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return count;
327}
328
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800329static SENSOR_DEVICE_ATTR_RO(in0_input, in, 0);
330static SENSOR_DEVICE_ATTR_RW(in0_min, in_min, 0);
331static SENSOR_DEVICE_ATTR_RW(in0_max, in_max, 0);
332static SENSOR_DEVICE_ATTR_RO(in1_input, in, 1);
333static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 1);
334static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 1);
335static SENSOR_DEVICE_ATTR_RO(in2_input, in, 2);
336static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 2);
337static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 2);
338static SENSOR_DEVICE_ATTR_RO(in3_input, in, 3);
339static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 3);
340static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 3);
341static SENSOR_DEVICE_ATTR_RO(in4_input, in, 4);
342static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 4);
343static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345/* Temperature */
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100346static ssize_t temp1_input_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct sis5595_data *data = sis5595_update_device(dev);
350 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
351}
352
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100353static ssize_t temp1_max_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800354 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct sis5595_data *data = sis5595_update_device(dev);
357 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
358}
359
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100360static ssize_t temp1_max_store(struct device *dev,
361 struct device_attribute *attr, const char *buf,
362 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400364 struct sis5595_data *data = dev_get_drvdata(dev);
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800365 long val;
366 int err;
367
368 err = kstrtol(buf, 10, &val);
369 if (err)
370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100372 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 data->temp_over = TEMP_TO_REG(val);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400374 sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100375 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return count;
377}
378
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100379static ssize_t temp1_max_hyst_show(struct device *dev,
380 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct sis5595_data *data = sis5595_update_device(dev);
383 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
384}
385
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100386static ssize_t temp1_max_hyst_store(struct device *dev,
387 struct device_attribute *attr,
388 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400390 struct sis5595_data *data = dev_get_drvdata(dev);
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800391 long val;
392 int err;
393
394 err = kstrtol(buf, 10, &val);
395 if (err)
396 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100398 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 data->temp_hyst = TEMP_TO_REG(val);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400400 sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100401 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return count;
403}
404
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100405static DEVICE_ATTR_RO(temp1_input);
406static DEVICE_ATTR_RW(temp1_max);
407static DEVICE_ATTR_RW(temp1_max_hyst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409/* 2 Fans */
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800410static ssize_t fan_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400411 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400414 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
415 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800417 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800420static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400421 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400424 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
425 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800426 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
427 DIV_FROM_REG(data->fan_div[nr])));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800430static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
431 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400433 struct sis5595_data *data = dev_get_drvdata(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400434 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
435 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800436 unsigned long val;
437 int err;
438
439 err = kstrtoul(buf, 10, &val);
440 if (err)
441 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100443 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare17e7dc42007-06-09 10:11:16 -0400445 sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100446 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return count;
448}
449
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800450static ssize_t fan_div_show(struct device *dev, struct device_attribute *da,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400451 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct sis5595_data *data = sis5595_update_device(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400454 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
455 int nr = attr->index;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800456 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800459/*
460 * Note: we save and restore the fan minimum here, because its value is
461 * determined in part by the fan divisor. This follows the principle of
462 * least surprise; the user doesn't expect the fan minimum to change just
463 * because the divisor changed.
464 */
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800465static ssize_t fan_div_store(struct device *dev, struct device_attribute *da,
466 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400468 struct sis5595_data *data = dev_get_drvdata(dev);
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400469 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
470 int nr = attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 unsigned long min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int reg;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800473 unsigned long val;
474 int err;
475
476 err = kstrtoul(buf, 10, &val);
477 if (err)
478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100480 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 min = FAN_FROM_REG(data->fan_min[nr],
482 DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare17e7dc42007-06-09 10:11:16 -0400483 reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 switch (val) {
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800486 case 1:
487 data->fan_div[nr] = 0;
488 break;
489 case 2:
490 data->fan_div[nr] = 1;
491 break;
492 case 4:
493 data->fan_div[nr] = 2;
494 break;
495 case 8:
496 data->fan_div[nr] = 3;
497 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 default:
Guenter Roeckb55f3752013-01-10 10:01:24 -0800499 dev_err(dev,
500 "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
501 val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100502 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -EINVAL;
504 }
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 switch (nr) {
507 case 0:
508 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
509 break;
510 case 1:
511 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
512 break;
513 }
Jean Delvare17e7dc42007-06-09 10:11:16 -0400514 sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 data->fan_min[nr] =
516 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
Jean Delvare17e7dc42007-06-09 10:11:16 -0400517 sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100518 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return count;
520}
521
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800522static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
523static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
524static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
525static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
526static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
527static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/* Alarms */
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100530static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800531 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct sis5595_data *data = sis5595_update_device(dev);
534 return sprintf(buf, "%d\n", data->alarms);
535}
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100536static DEVICE_ATTR_RO(alarms);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200537
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800538static ssize_t alarm_show(struct device *dev, struct device_attribute *da,
Ivo Manca5c726b32007-10-15 20:50:53 +0200539 char *buf)
540{
541 struct sis5595_data *data = sis5595_update_device(dev);
542 int nr = to_sensor_dev_attr(da)->index;
543 return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
544}
Guenter Roeck60a9c3f2019-01-22 14:28:50 -0800545static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
546static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
547static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
548static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
549static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 15);
550static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 6);
551static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 7);
552static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 15);
Ivo Manca5c726b32007-10-15 20:50:53 +0200553
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100554static ssize_t name_show(struct device *dev, struct device_attribute *attr,
Jean Delvare17e7dc42007-06-09 10:11:16 -0400555 char *buf)
556{
557 struct sis5595_data *data = dev_get_drvdata(dev);
558 return sprintf(buf, "%s\n", data->name);
559}
Julia Lawallfeaf8bd2016-12-22 13:05:05 +0100560static DEVICE_ATTR_RO(name);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400561
Jean Delvarea5ebe662006-09-24 21:24:46 +0200562static struct attribute *sis5595_attributes[] = {
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400563 &sensor_dev_attr_in0_input.dev_attr.attr,
564 &sensor_dev_attr_in0_min.dev_attr.attr,
565 &sensor_dev_attr_in0_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200566 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400567 &sensor_dev_attr_in1_input.dev_attr.attr,
568 &sensor_dev_attr_in1_min.dev_attr.attr,
569 &sensor_dev_attr_in1_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200570 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400571 &sensor_dev_attr_in2_input.dev_attr.attr,
572 &sensor_dev_attr_in2_min.dev_attr.attr,
573 &sensor_dev_attr_in2_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200574 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400575 &sensor_dev_attr_in3_input.dev_attr.attr,
576 &sensor_dev_attr_in3_min.dev_attr.attr,
577 &sensor_dev_attr_in3_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200578 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200579
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400580 &sensor_dev_attr_fan1_input.dev_attr.attr,
581 &sensor_dev_attr_fan1_min.dev_attr.attr,
582 &sensor_dev_attr_fan1_div.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200583 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400584 &sensor_dev_attr_fan2_input.dev_attr.attr,
585 &sensor_dev_attr_fan2_min.dev_attr.attr,
586 &sensor_dev_attr_fan2_div.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200587 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200588
589 &dev_attr_alarms.attr,
Jean Delvare17e7dc42007-06-09 10:11:16 -0400590 &dev_attr_name.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200591 NULL
592};
593
594static const struct attribute_group sis5595_group = {
595 .attrs = sis5595_attributes,
596};
597
Ivo Manca76e63862007-10-15 13:27:13 +0200598static struct attribute *sis5595_attributes_in4[] = {
Jean Delvare1f5f48d2007-06-09 10:11:16 -0400599 &sensor_dev_attr_in4_input.dev_attr.attr,
600 &sensor_dev_attr_in4_min.dev_attr.attr,
601 &sensor_dev_attr_in4_max.dev_attr.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200602 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Ivo Manca76e63862007-10-15 13:27:13 +0200603 NULL
604};
Jean Delvarea5ebe662006-09-24 21:24:46 +0200605
Ivo Manca76e63862007-10-15 13:27:13 +0200606static const struct attribute_group sis5595_group_in4 = {
607 .attrs = sis5595_attributes_in4,
608};
609
610static struct attribute *sis5595_attributes_temp1[] = {
Jean Delvarea5ebe662006-09-24 21:24:46 +0200611 &dev_attr_temp1_input.attr,
612 &dev_attr_temp1_max.attr,
613 &dev_attr_temp1_max_hyst.attr,
Ivo Manca5c726b32007-10-15 20:50:53 +0200614 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200615 NULL
616};
617
Ivo Manca76e63862007-10-15 13:27:13 +0200618static const struct attribute_group sis5595_group_temp1 = {
619 .attrs = sis5595_attributes_temp1,
Jean Delvarea5ebe662006-09-24 21:24:46 +0200620};
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800621
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200622/* Called when we have found a new SIS5595. */
623static void sis5595_init_device(struct sis5595_data *data)
624{
625 u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
626 if (!(config & 0x01))
627 sis5595_write_value(data, SIS5595_REG_CONFIG,
628 (config & 0xf7) | 0x01);
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/* This is called when the module is loaded */
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500632static int sis5595_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 int err = 0;
635 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct sis5595_data *data;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400637 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 char val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /* Reserve the ISA region */
Jean Delvare17e7dc42007-06-09 10:11:16 -0400641 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
Guenter Roeck5d224ad2012-06-02 11:20:23 -0700642 if (!devm_request_region(&pdev->dev, res->start, SIS5595_EXTENT,
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200643 DRIVER_NAME))
Guenter Roeck5d224ad2012-06-02 11:20:23 -0700644 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Guenter Roeck5d224ad2012-06-02 11:20:23 -0700646 data = devm_kzalloc(&pdev->dev, sizeof(struct sis5595_data),
647 GFP_KERNEL);
648 if (!data)
649 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100651 mutex_init(&data->lock);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400652 mutex_init(&data->update_lock);
653 data->addr = res->start;
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200654 data->name = DRIVER_NAME;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400655 platform_set_drvdata(pdev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800657 /*
658 * Check revision and pin registers to determine whether 4 or 5 voltages
659 */
Auke Kok7b6d1f02007-08-27 16:17:01 -0700660 data->revision = s_bridge->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* 4 voltages, 1 temp */
662 data->maxins = 3;
663 if (data->revision >= REV2MIN) {
664 pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
665 if (!(val & 0x80))
666 /* 5 voltages, no temps */
667 data->maxins = 4;
668 }
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* Initialize the SIS5595 chip */
Jean Delvare17e7dc42007-06-09 10:11:16 -0400671 sis5595_init_device(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 /* A few vars need to be filled upon startup */
674 for (i = 0; i < 2; i++) {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400675 data->fan_min[i] = sis5595_read_value(data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 SIS5595_REG_FAN_MIN(i));
677 }
678
679 /* Register sysfs hooks */
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800680 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
681 if (err)
Guenter Roeck5d224ad2012-06-02 11:20:23 -0700682 return err;
Jean Delvarea5ebe662006-09-24 21:24:46 +0200683 if (data->maxins == 4) {
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800684 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
685 if (err)
Jean Delvarea5ebe662006-09-24 21:24:46 +0200686 goto exit_remove_files;
687 } else {
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800688 err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
689 if (err)
Jean Delvarea5ebe662006-09-24 21:24:46 +0200690 goto exit_remove_files;
691 }
692
Tony Jones1beeffe2007-08-20 13:46:20 -0700693 data->hwmon_dev = hwmon_device_register(&pdev->dev);
694 if (IS_ERR(data->hwmon_dev)) {
695 err = PTR_ERR(data->hwmon_dev);
Jean Delvarea5ebe662006-09-24 21:24:46 +0200696 goto exit_remove_files;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400697 }
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400700
Jean Delvarea5ebe662006-09-24 21:24:46 +0200701exit_remove_files:
Jean Delvare17e7dc42007-06-09 10:11:16 -0400702 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
Ivo Manca76e63862007-10-15 13:27:13 +0200703 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
704 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return err;
706}
707
Uwe Kleine-König39797752023-09-18 10:59:43 +0200708static void sis5595_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400710 struct sis5595_data *data = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Tony Jones1beeffe2007-08-20 13:46:20 -0700712 hwmon_device_unregister(data->hwmon_dev);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400713 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
Ivo Manca76e63862007-10-15 13:27:13 +0200714 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
715 sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Jingoo Hancd9bb052013-12-03 07:10:29 +0000718static const struct pci_device_id sis5595_pci_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
720 { 0, }
721};
722
723MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
724
Bill Pembertona5977242012-11-19 13:24:37 -0500725static int blacklist[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 PCI_DEVICE_ID_SI_540,
727 PCI_DEVICE_ID_SI_550,
728 PCI_DEVICE_ID_SI_630,
729 PCI_DEVICE_ID_SI_645,
730 PCI_DEVICE_ID_SI_730,
731 PCI_DEVICE_ID_SI_735,
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800732 PCI_DEVICE_ID_SI_5511, /*
733 * 5513 chip has the 0008 device but
734 * that ID shows up in other chips so we
735 * use the 5511 ID for recognition
736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 PCI_DEVICE_ID_SI_5597,
738 PCI_DEVICE_ID_SI_5598,
739 0 };
740
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500741static int sis5595_device_add(unsigned short address)
Jean Delvare17e7dc42007-06-09 10:11:16 -0400742{
743 struct resource res = {
744 .start = address,
745 .end = address + SIS5595_EXTENT - 1,
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200746 .name = DRIVER_NAME,
Jean Delvare17e7dc42007-06-09 10:11:16 -0400747 .flags = IORESOURCE_IO,
748 };
749 int err;
750
Jean Delvareb9acb642009-01-07 16:37:35 +0100751 err = acpi_check_resource_conflict(&res);
752 if (err)
753 goto exit;
754
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200755 pdev = platform_device_alloc(DRIVER_NAME, address);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400756 if (!pdev) {
757 err = -ENOMEM;
Joe Perches4b2515d2010-10-20 06:51:47 +0000758 pr_err("Device allocation failed\n");
Jean Delvare17e7dc42007-06-09 10:11:16 -0400759 goto exit;
760 }
761
762 err = platform_device_add_resources(pdev, &res, 1);
763 if (err) {
Joe Perches4b2515d2010-10-20 06:51:47 +0000764 pr_err("Device resource addition failed (%d)\n", err);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400765 goto exit_device_put;
766 }
767
768 err = platform_device_add(pdev);
769 if (err) {
Joe Perches4b2515d2010-10-20 06:51:47 +0000770 pr_err("Device addition failed (%d)\n", err);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400771 goto exit_device_put;
772 }
773
774 return 0;
775
776exit_device_put:
777 platform_device_put(pdev);
778exit:
779 return err;
780}
781
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200782static struct platform_driver sis5595_driver = {
783 .driver = {
784 .name = DRIVER_NAME,
785 },
786 .probe = sis5595_probe,
Uwe Kleine-König39797752023-09-18 10:59:43 +0200787 .remove_new = sis5595_remove,
Uwe Kleine-König1b2f9b12022-09-22 09:49:00 +0200788};
789
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500790static int sis5595_pci_probe(struct pci_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 const struct pci_device_id *id)
792{
Jean Delvare17e7dc42007-06-09 10:11:16 -0400793 u16 address;
794 u8 enable;
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300795 int *i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 for (i = blacklist; *i != 0; i++) {
Mark M. Hoffman5460a9d2007-10-14 14:57:35 -0400798 struct pci_dev *d;
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800799 d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
800 if (d) {
801 dev_err(&d->dev,
802 "Looked for SIS5595 but found unsupported device %.4x\n",
803 *i);
Mark M. Hoffman5460a9d2007-10-14 14:57:35 -0400804 pci_dev_put(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -ENODEV;
806 }
807 }
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800808
Jean Delvare17e7dc42007-06-09 10:11:16 -0400809 force_addr &= ~(SIS5595_EXTENT - 1);
810 if (force_addr) {
811 dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
812 pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
813 }
814
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300815 err = pci_read_config_word(dev, SIS5595_BASE_REG, &address);
816 if (err != PCIBIOS_SUCCESSFUL) {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400817 dev_err(&dev->dev, "Failed to read ISA address\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return -ENODEV;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400819 }
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800820
Jean Delvare17e7dc42007-06-09 10:11:16 -0400821 address &= ~(SIS5595_EXTENT - 1);
822 if (!address) {
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800823 dev_err(&dev->dev,
824 "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return -ENODEV;
826 }
Jean Delvare17e7dc42007-06-09 10:11:16 -0400827 if (force_addr && address != force_addr) {
828 /* doesn't work for some chips? */
829 dev_err(&dev->dev, "Failed to force ISA address\n");
830 return -ENODEV;
831 }
832
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300833 err = pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable);
834 if (err != PCIBIOS_SUCCESSFUL) {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400835 dev_err(&dev->dev, "Failed to read enable register\n");
836 return -ENODEV;
837 }
838 if (!(enable & 0x80)) {
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300839 err = pci_write_config_byte(dev, SIS5595_ENABLE_REG, enable | 0x80);
840 if (err != PCIBIOS_SUCCESSFUL)
841 goto enable_fail;
842
843 err = pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable);
844 if (err != PCIBIOS_SUCCESSFUL)
845 goto enable_fail;
846
847 /* doesn't work for some chips! */
848 if (!(enable & 0x80))
849 goto enable_fail;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400850 }
851
852 if (platform_driver_register(&sis5595_driver)) {
853 dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
854 goto exit;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 s_bridge = pci_dev_get(dev);
Jean Delvare17e7dc42007-06-09 10:11:16 -0400858 /* Sets global pdev as a side effect */
859 if (sis5595_device_add(address))
860 goto exit_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Guenter Roeck8fda79e2012-01-14 22:21:41 -0800862 /*
863 * Always return failure here. This is to allow other drivers to bind
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 * to this pci device. We don't really want to have control over the
865 * pci device, we only wanted to read as few register values from it.
866 */
867 return -ENODEV;
Jean Delvare17e7dc42007-06-09 10:11:16 -0400868
Ilpo Järvinena1f38982023-08-24 16:28:31 +0300869enable_fail:
870 dev_err(&dev->dev, "Failed to enable HWM device\n");
871 goto exit;
872
Jean Delvare17e7dc42007-06-09 10:11:16 -0400873exit_unregister:
874 pci_dev_put(dev);
875 platform_driver_unregister(&sis5595_driver);
876exit:
877 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880static struct pci_driver sis5595_pci_driver = {
Uwe Kleine-König847a3b02022-09-22 09:48:59 +0200881 .name = DRIVER_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 .id_table = sis5595_pci_ids,
883 .probe = sis5595_pci_probe,
884};
885
886static int __init sm_sis5595_init(void)
887{
888 return pci_register_driver(&sis5595_pci_driver);
889}
890
891static void __exit sm_sis5595_exit(void)
892{
893 pci_unregister_driver(&sis5595_pci_driver);
894 if (s_bridge != NULL) {
Jean Delvare17e7dc42007-06-09 10:11:16 -0400895 platform_device_unregister(pdev);
896 platform_driver_unregister(&sis5595_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 pci_dev_put(s_bridge);
898 s_bridge = NULL;
899 }
900}
901
902MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
903MODULE_DESCRIPTION("SiS 5595 Sensor device");
904MODULE_LICENSE("GPL");
905
906module_init(sm_sis5595_init);
907module_exit(sm_sis5595_exit);