2 smsc47b397.c - Part of lm_sensors, Linux kernel modules
3 for hardware monitoring
5 Supports the SMSC LPC47B397-NC Super-I/O chip.
7 Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
8 Copyright (C) 2004 Utilitek Systems, Inc.
10 derived in part from smsc47m1.c:
11 Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
12 Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/ioport.h>
32 #include <linux/jiffies.h>
33 #include <linux/platform_device.h>
34 #include <linux/hwmon.h>
35 #include <linux/hwmon-sysfs.h>
36 #include <linux/err.h>
37 #include <linux/init.h>
38 #include <linux/mutex.h>
39 #include <linux/acpi.h>
42 static unsigned short force_id
;
43 module_param(force_id
, ushort
, 0);
44 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
46 static struct platform_device
*pdev
;
48 #define DRVNAME "smsc47b397"
50 /* Super-I/0 registers and commands */
52 #define REG 0x2e /* The register to read/write */
53 #define VAL 0x2f /* The value to read/write */
55 static inline void superio_outb(int reg
, int val
)
61 static inline int superio_inb(int reg
)
67 /* select superio logical device */
68 static inline void superio_select(int ld
)
70 superio_outb(0x07, ld
);
73 static inline void superio_enter(void)
78 static inline void superio_exit(void)
83 #define SUPERIO_REG_DEVID 0x20
84 #define SUPERIO_REG_DEVREV 0x21
85 #define SUPERIO_REG_BASE_MSB 0x60
86 #define SUPERIO_REG_BASE_LSB 0x61
87 #define SUPERIO_REG_LD8 0x08
89 #define SMSC_EXTENT 0x02
92 static u8 smsc47b397_reg_temp
[] = {0x25, 0x26, 0x27, 0x80};
93 #define SMSC47B397_REG_TEMP(nr) (smsc47b397_reg_temp[(nr)])
96 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
97 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
99 struct smsc47b397_data
{
102 struct device
*hwmon_dev
;
105 struct mutex update_lock
;
106 unsigned long last_updated
; /* in jiffies */
109 /* register values */
114 static int smsc47b397_read_value(struct smsc47b397_data
* data
, u8 reg
)
118 mutex_lock(&data
->lock
);
119 outb(reg
, data
->addr
);
120 res
= inb_p(data
->addr
+ 1);
121 mutex_unlock(&data
->lock
);
125 static struct smsc47b397_data
*smsc47b397_update_device(struct device
*dev
)
127 struct smsc47b397_data
*data
= dev_get_drvdata(dev
);
130 mutex_lock(&data
->update_lock
);
132 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
133 dev_dbg(dev
, "starting device update...\n");
135 /* 4 temperature inputs, 4 fan inputs */
136 for (i
= 0; i
< 4; i
++) {
137 data
->temp
[i
] = smsc47b397_read_value(data
,
138 SMSC47B397_REG_TEMP(i
));
140 /* must read LSB first */
141 data
->fan
[i
] = smsc47b397_read_value(data
,
142 SMSC47B397_REG_FAN_LSB(i
));
143 data
->fan
[i
] |= smsc47b397_read_value(data
,
144 SMSC47B397_REG_FAN_MSB(i
)) << 8;
147 data
->last_updated
= jiffies
;
150 dev_dbg(dev
, "... device update complete\n");
153 mutex_unlock(&data
->update_lock
);
158 /* TEMP: 0.001C/bit (-128C to +127C)
159 REG: 1C/bit, two's complement */
160 static int temp_from_reg(u8 reg
)
162 return (s8
)reg
* 1000;
165 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
168 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
169 struct smsc47b397_data
*data
= smsc47b397_update_device(dev
);
170 return sprintf(buf
, "%d\n", temp_from_reg(data
->temp
[attr
->index
]));
173 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0);
174 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp
, NULL
, 1);
175 static SENSOR_DEVICE_ATTR(temp3_input
, S_IRUGO
, show_temp
, NULL
, 2);
176 static SENSOR_DEVICE_ATTR(temp4_input
, S_IRUGO
, show_temp
, NULL
, 3);
179 REG: count of 90kHz pulses / revolution */
180 static int fan_from_reg(u16 reg
)
182 if (reg
== 0 || reg
== 0xffff)
184 return 90000 * 60 / reg
;
187 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
190 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
191 struct smsc47b397_data
*data
= smsc47b397_update_device(dev
);
192 return sprintf(buf
, "%d\n", fan_from_reg(data
->fan
[attr
->index
]));
194 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, show_fan
, NULL
, 0);
195 static SENSOR_DEVICE_ATTR(fan2_input
, S_IRUGO
, show_fan
, NULL
, 1);
196 static SENSOR_DEVICE_ATTR(fan3_input
, S_IRUGO
, show_fan
, NULL
, 2);
197 static SENSOR_DEVICE_ATTR(fan4_input
, S_IRUGO
, show_fan
, NULL
, 3);
199 static ssize_t
show_name(struct device
*dev
, struct device_attribute
202 struct smsc47b397_data
*data
= dev_get_drvdata(dev
);
203 return sprintf(buf
, "%s\n", data
->name
);
205 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
207 static struct attribute
*smsc47b397_attributes
[] = {
208 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
209 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
210 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
211 &sensor_dev_attr_temp4_input
.dev_attr
.attr
,
212 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
213 &sensor_dev_attr_fan2_input
.dev_attr
.attr
,
214 &sensor_dev_attr_fan3_input
.dev_attr
.attr
,
215 &sensor_dev_attr_fan4_input
.dev_attr
.attr
,
221 static const struct attribute_group smsc47b397_group
= {
222 .attrs
= smsc47b397_attributes
,
225 static int __devexit
smsc47b397_remove(struct platform_device
*pdev
)
227 struct smsc47b397_data
*data
= platform_get_drvdata(pdev
);
228 struct resource
*res
;
230 hwmon_device_unregister(data
->hwmon_dev
);
231 sysfs_remove_group(&pdev
->dev
.kobj
, &smsc47b397_group
);
232 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
233 release_region(res
->start
, SMSC_EXTENT
);
239 static int smsc47b397_probe(struct platform_device
*pdev
);
241 static struct platform_driver smsc47b397_driver
= {
243 .owner
= THIS_MODULE
,
246 .probe
= smsc47b397_probe
,
247 .remove
= __devexit_p(smsc47b397_remove
),
250 static int __devinit
smsc47b397_probe(struct platform_device
*pdev
)
252 struct device
*dev
= &pdev
->dev
;
253 struct smsc47b397_data
*data
;
254 struct resource
*res
;
257 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
258 if (!request_region(res
->start
, SMSC_EXTENT
,
259 smsc47b397_driver
.driver
.name
)) {
260 dev_err(dev
, "Region 0x%lx-0x%lx already in use!\n",
261 (unsigned long)res
->start
,
262 (unsigned long)res
->start
+ SMSC_EXTENT
- 1);
266 if (!(data
= kzalloc(sizeof(struct smsc47b397_data
), GFP_KERNEL
))) {
271 data
->addr
= res
->start
;
272 data
->name
= "smsc47b397";
273 mutex_init(&data
->lock
);
274 mutex_init(&data
->update_lock
);
275 platform_set_drvdata(pdev
, data
);
277 if ((err
= sysfs_create_group(&dev
->kobj
, &smsc47b397_group
)))
280 data
->hwmon_dev
= hwmon_device_register(dev
);
281 if (IS_ERR(data
->hwmon_dev
)) {
282 err
= PTR_ERR(data
->hwmon_dev
);
289 sysfs_remove_group(&dev
->kobj
, &smsc47b397_group
);
293 release_region(res
->start
, SMSC_EXTENT
);
297 static int __init
smsc47b397_device_add(unsigned short address
)
299 struct resource res
= {
301 .end
= address
+ SMSC_EXTENT
- 1,
303 .flags
= IORESOURCE_IO
,
307 err
= acpi_check_resource_conflict(&res
);
311 pdev
= platform_device_alloc(DRVNAME
, address
);
314 printk(KERN_ERR DRVNAME
": Device allocation failed\n");
318 err
= platform_device_add_resources(pdev
, &res
, 1);
320 printk(KERN_ERR DRVNAME
": Device resource addition failed "
322 goto exit_device_put
;
325 err
= platform_device_add(pdev
);
327 printk(KERN_ERR DRVNAME
": Device addition failed (%d)\n",
329 goto exit_device_put
;
335 platform_device_put(pdev
);
340 static int __init
smsc47b397_find(unsigned short *addr
)
346 id
= force_id
? force_id
: superio_inb(SUPERIO_REG_DEVID
);
353 name
= "LPC47B397-NC";
364 rev
= superio_inb(SUPERIO_REG_DEVREV
);
366 superio_select(SUPERIO_REG_LD8
);
367 *addr
= (superio_inb(SUPERIO_REG_BASE_MSB
) << 8)
368 | superio_inb(SUPERIO_REG_BASE_LSB
);
370 printk(KERN_INFO DRVNAME
": found SMSC %s "
371 "(base address 0x%04x, revision %u)\n",
378 static int __init
smsc47b397_init(void)
380 unsigned short address
;
383 if ((ret
= smsc47b397_find(&address
)))
386 ret
= platform_driver_register(&smsc47b397_driver
);
390 /* Sets global pdev as a side effect */
391 ret
= smsc47b397_device_add(address
);
398 platform_driver_unregister(&smsc47b397_driver
);
403 static void __exit
smsc47b397_exit(void)
405 platform_device_unregister(pdev
);
406 platform_driver_unregister(&smsc47b397_driver
);
409 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
410 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
411 MODULE_LICENSE("GPL");
413 module_init(smsc47b397_init
);
414 module_exit(smsc47b397_exit
);