1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * smsc47b397.c - Part of lm_sensors, Linux kernel modules
4 * for hardware monitoring
6 * Supports the SMSC LPC47B397-NC Super-I/O chip.
8 * Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
9 * Copyright (C) 2004 Utilitek Systems, Inc.
11 * derived in part from smsc47m1.c:
12 * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
13 * Copyright (C) 2004 Jean Delvare <jdelvare@suse.de>
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/ioport.h>
21 #include <linux/jiffies.h>
22 #include <linux/platform_device.h>
23 #include <linux/hwmon.h>
24 #include <linux/hwmon-sysfs.h>
25 #include <linux/err.h>
26 #include <linux/init.h>
27 #include <linux/mutex.h>
28 #include <linux/acpi.h>
31 static unsigned short force_id
;
32 module_param(force_id
, ushort
, 0);
33 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
35 static struct platform_device
*pdev
;
37 #define DRVNAME "smsc47b397"
39 /* Super-I/0 registers and commands */
41 #define REG 0x2e /* The register to read/write */
42 #define VAL 0x2f /* The value to read/write */
44 static inline void superio_outb(int reg
, int val
)
50 static inline int superio_inb(int reg
)
56 /* select superio logical device */
57 static inline void superio_select(int ld
)
59 superio_outb(0x07, ld
);
62 static inline int superio_enter(void)
64 if (!request_muxed_region(REG
, 2, DRVNAME
))
71 static inline void superio_exit(void)
74 release_region(REG
, 2);
77 #define SUPERIO_REG_DEVID 0x20
78 #define SUPERIO_REG_DEVREV 0x21
79 #define SUPERIO_REG_BASE_MSB 0x60
80 #define SUPERIO_REG_BASE_LSB 0x61
81 #define SUPERIO_REG_LD8 0x08
83 #define SMSC_EXTENT 0x02
86 static u8 smsc47b397_reg_temp
[] = {0x25, 0x26, 0x27, 0x80};
87 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
90 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
91 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
93 struct smsc47b397_data
{
97 struct mutex update_lock
;
98 unsigned long last_updated
; /* in jiffies */
101 /* register values */
106 static int smsc47b397_read_value(struct smsc47b397_data
*data
, u8 reg
)
110 mutex_lock(&data
->lock
);
111 outb(reg
, data
->addr
);
112 res
= inb_p(data
->addr
+ 1);
113 mutex_unlock(&data
->lock
);
117 static struct smsc47b397_data
*smsc47b397_update_device(struct device
*dev
)
119 struct smsc47b397_data
*data
= dev_get_drvdata(dev
);
122 mutex_lock(&data
->update_lock
);
124 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
125 dev_dbg(dev
, "starting device update...\n");
127 /* 4 temperature inputs, 4 fan inputs */
128 for (i
= 0; i
< 4; i
++) {
129 data
->temp
[i
] = smsc47b397_read_value(data
,
130 SMSC47B397_REG_TEMP(i
));
132 /* must read LSB first */
133 data
->fan
[i
] = smsc47b397_read_value(data
,
134 SMSC47B397_REG_FAN_LSB(i
));
135 data
->fan
[i
] |= smsc47b397_read_value(data
,
136 SMSC47B397_REG_FAN_MSB(i
)) << 8;
139 data
->last_updated
= jiffies
;
142 dev_dbg(dev
, "... device update complete\n");
145 mutex_unlock(&data
->update_lock
);
151 * TEMP: 0.001C/bit (-128C to +127C)
152 * REG: 1C/bit, two's complement
154 static int temp_from_reg(u8 reg
)
156 return (s8
)reg
* 1000;
159 static ssize_t
temp_show(struct device
*dev
, struct device_attribute
*devattr
,
162 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
163 struct smsc47b397_data
*data
= smsc47b397_update_device(dev
);
164 return sprintf(buf
, "%d\n", temp_from_reg(data
->temp
[attr
->index
]));
167 static SENSOR_DEVICE_ATTR_RO(temp1_input
, temp
, 0);
168 static SENSOR_DEVICE_ATTR_RO(temp2_input
, temp
, 1);
169 static SENSOR_DEVICE_ATTR_RO(temp3_input
, temp
, 2);
170 static SENSOR_DEVICE_ATTR_RO(temp4_input
, temp
, 3);
174 * REG: count of 90kHz pulses / revolution
176 static int fan_from_reg(u16 reg
)
178 if (reg
== 0 || reg
== 0xffff)
180 return 90000 * 60 / reg
;
183 static ssize_t
fan_show(struct device
*dev
, struct device_attribute
*devattr
,
186 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
187 struct smsc47b397_data
*data
= smsc47b397_update_device(dev
);
188 return sprintf(buf
, "%d\n", fan_from_reg(data
->fan
[attr
->index
]));
190 static SENSOR_DEVICE_ATTR_RO(fan1_input
, fan
, 0);
191 static SENSOR_DEVICE_ATTR_RO(fan2_input
, fan
, 1);
192 static SENSOR_DEVICE_ATTR_RO(fan3_input
, fan
, 2);
193 static SENSOR_DEVICE_ATTR_RO(fan4_input
, fan
, 3);
195 static struct attribute
*smsc47b397_attrs
[] = {
196 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
197 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
198 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
199 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
200 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
201 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
202 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
203 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
208 ATTRIBUTE_GROUPS(smsc47b397
);
210 static int smsc47b397_probe(struct platform_device
*pdev
);
212 static struct platform_driver smsc47b397_driver
= {
216 .probe
= smsc47b397_probe
,
219 static int smsc47b397_probe(struct platform_device
*pdev
)
221 struct device
*dev
= &pdev
->dev
;
222 struct smsc47b397_data
*data
;
223 struct device
*hwmon_dev
;
224 struct resource
*res
;
226 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
227 if (!devm_request_region(dev
, res
->start
, SMSC_EXTENT
,
228 smsc47b397_driver
.driver
.name
)) {
229 dev_err(dev
, "Region 0x%lx-0x%lx already in use!\n",
230 (unsigned long)res
->start
,
231 (unsigned long)res
->start
+ SMSC_EXTENT
- 1);
235 data
= devm_kzalloc(dev
, sizeof(struct smsc47b397_data
), GFP_KERNEL
);
239 data
->addr
= res
->start
;
240 mutex_init(&data
->lock
);
241 mutex_init(&data
->update_lock
);
243 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, "smsc47b397",
246 return PTR_ERR_OR_ZERO(hwmon_dev
);
249 static int __init
smsc47b397_device_add(unsigned short address
)
251 struct resource res
= {
253 .end
= address
+ SMSC_EXTENT
- 1,
255 .flags
= IORESOURCE_IO
,
259 err
= acpi_check_resource_conflict(&res
);
263 pdev
= platform_device_alloc(DRVNAME
, address
);
266 pr_err("Device allocation failed\n");
270 err
= platform_device_add_resources(pdev
, &res
, 1);
272 pr_err("Device resource addition failed (%d)\n", err
);
273 goto exit_device_put
;
276 err
= platform_device_add(pdev
);
278 pr_err("Device addition failed (%d)\n", err
);
279 goto exit_device_put
;
285 platform_device_put(pdev
);
290 static int __init
smsc47b397_find(void)
297 err
= superio_enter();
301 id
= force_id
? force_id
: superio_inb(SUPERIO_REG_DEVID
);
308 name
= "LPC47B397-NC";
319 rev
= superio_inb(SUPERIO_REG_DEVREV
);
321 superio_select(SUPERIO_REG_LD8
);
322 addr
= (superio_inb(SUPERIO_REG_BASE_MSB
) << 8)
323 | superio_inb(SUPERIO_REG_BASE_LSB
);
325 pr_info("found SMSC %s (base address 0x%04x, revision %u)\n",
332 static int __init
smsc47b397_init(void)
334 unsigned short address
;
337 ret
= smsc47b397_find();
342 ret
= platform_driver_register(&smsc47b397_driver
);
346 /* Sets global pdev as a side effect */
347 ret
= smsc47b397_device_add(address
);
354 platform_driver_unregister(&smsc47b397_driver
);
359 static void __exit
smsc47b397_exit(void)
361 platform_device_unregister(pdev
);
362 platform_driver_unregister(&smsc47b397_driver
);
365 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
366 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
367 MODULE_LICENSE("GPL");
369 module_init(smsc47b397_init
);
370 module_exit(smsc47b397_exit
);