xref: /linux/drivers/hwmon/smsc47b397.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
1 /*
2     smsc47b397.c - Part of lm_sensors, Linux kernel modules
3 			for hardware monitoring
4 
5     Supports the SMSC LPC47B397-NC Super-I/O chip.
6 
7     Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
8 	Copyright (C) 2004 Utilitek Systems, Inc.
9 
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>
13 
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.
18 
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.
23 
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.
27 */
28 
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/ioport.h>
32 #include <linux/jiffies.h>
33 #include <linux/i2c.h>
34 #include <linux/i2c-isa.h>
35 #include <linux/hwmon.h>
36 #include <linux/err.h>
37 #include <linux/init.h>
38 #include <linux/mutex.h>
39 #include <asm/io.h>
40 
41 /* Address is autodetected, there is no default value */
42 static unsigned short address;
43 
44 /* Super-I/0 registers and commands */
45 
46 #define	REG	0x2e	/* The register to read/write */
47 #define	VAL	0x2f	/* The value to read/write */
48 
49 static inline void superio_outb(int reg, int val)
50 {
51 	outb(reg, REG);
52 	outb(val, VAL);
53 }
54 
55 static inline int superio_inb(int reg)
56 {
57 	outb(reg, REG);
58 	return inb(VAL);
59 }
60 
61 /* select superio logical device */
62 static inline void superio_select(int ld)
63 {
64 	superio_outb(0x07, ld);
65 }
66 
67 static inline void superio_enter(void)
68 {
69 	outb(0x55, REG);
70 }
71 
72 static inline void superio_exit(void)
73 {
74 	outb(0xAA, REG);
75 }
76 
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
82 
83 #define SMSC_EXTENT		0x02
84 
85 /* 0 <= nr <= 3 */
86 static u8 smsc47b397_reg_temp[] = {0x25, 0x26, 0x27, 0x80};
87 #define SMSC47B397_REG_TEMP(nr)	(smsc47b397_reg_temp[(nr)])
88 
89 /* 0 <= nr <= 3 */
90 #define SMSC47B397_REG_FAN_LSB(nr) (0x28 + 2 * (nr))
91 #define SMSC47B397_REG_FAN_MSB(nr) (0x29 + 2 * (nr))
92 
93 struct smsc47b397_data {
94 	struct i2c_client client;
95 	struct class_device *class_dev;
96 	struct mutex lock;
97 
98 	struct mutex update_lock;
99 	unsigned long last_updated; /* in jiffies */
100 	int valid;
101 
102 	/* register values */
103 	u16 fan[4];
104 	u8 temp[4];
105 };
106 
107 static int smsc47b397_read_value(struct i2c_client *client, u8 reg)
108 {
109 	struct smsc47b397_data *data = i2c_get_clientdata(client);
110 	int res;
111 
112 	mutex_lock(&data->lock);
113 	outb(reg, client->addr);
114 	res = inb_p(client->addr + 1);
115 	mutex_unlock(&data->lock);
116 	return res;
117 }
118 
119 static struct smsc47b397_data *smsc47b397_update_device(struct device *dev)
120 {
121  	struct i2c_client *client = to_i2c_client(dev);
122 	struct smsc47b397_data *data = i2c_get_clientdata(client);
123 	int i;
124 
125 	mutex_lock(&data->update_lock);
126 
127 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
128 		dev_dbg(&client->dev, "starting device update...\n");
129 
130 		/* 4 temperature inputs, 4 fan inputs */
131 		for (i = 0; i < 4; i++) {
132 			data->temp[i] = smsc47b397_read_value(client,
133 					SMSC47B397_REG_TEMP(i));
134 
135 			/* must read LSB first */
136 			data->fan[i]  = smsc47b397_read_value(client,
137 					SMSC47B397_REG_FAN_LSB(i));
138 			data->fan[i] |= smsc47b397_read_value(client,
139 					SMSC47B397_REG_FAN_MSB(i)) << 8;
140 		}
141 
142 		data->last_updated = jiffies;
143 		data->valid = 1;
144 
145 		dev_dbg(&client->dev, "... device update complete\n");
146 	}
147 
148 	mutex_unlock(&data->update_lock);
149 
150 	return data;
151 }
152 
153 /* TEMP: 0.001C/bit (-128C to +127C)
154    REG: 1C/bit, two's complement */
155 static int temp_from_reg(u8 reg)
156 {
157 	return (s8)reg * 1000;
158 }
159 
160 /* 0 <= nr <= 3 */
161 static ssize_t show_temp(struct device *dev, char *buf, int nr)
162 {
163 	struct smsc47b397_data *data = smsc47b397_update_device(dev);
164 	return sprintf(buf, "%d\n", temp_from_reg(data->temp[nr]));
165 }
166 
167 #define sysfs_temp(num) \
168 static ssize_t show_temp##num(struct device *dev, struct device_attribute *attr, char *buf) \
169 { \
170 	return show_temp(dev, buf, num-1); \
171 } \
172 static DEVICE_ATTR(temp##num##_input, S_IRUGO, show_temp##num, NULL)
173 
174 sysfs_temp(1);
175 sysfs_temp(2);
176 sysfs_temp(3);
177 sysfs_temp(4);
178 
179 #define device_create_file_temp(client, num) \
180 	device_create_file(&client->dev, &dev_attr_temp##num##_input)
181 
182 /* FAN: 1 RPM/bit
183    REG: count of 90kHz pulses / revolution */
184 static int fan_from_reg(u16 reg)
185 {
186 	return 90000 * 60 / reg;
187 }
188 
189 /* 0 <= nr <= 3 */
190 static ssize_t show_fan(struct device *dev, char *buf, int nr)
191 {
192         struct smsc47b397_data *data = smsc47b397_update_device(dev);
193         return sprintf(buf, "%d\n", fan_from_reg(data->fan[nr]));
194 }
195 
196 #define sysfs_fan(num) \
197 static ssize_t show_fan##num(struct device *dev, struct device_attribute *attr, char *buf) \
198 { \
199 	return show_fan(dev, buf, num-1); \
200 } \
201 static DEVICE_ATTR(fan##num##_input, S_IRUGO, show_fan##num, NULL)
202 
203 sysfs_fan(1);
204 sysfs_fan(2);
205 sysfs_fan(3);
206 sysfs_fan(4);
207 
208 #define device_create_file_fan(client, num) \
209 	device_create_file(&client->dev, &dev_attr_fan##num##_input)
210 
211 static int smsc47b397_detach_client(struct i2c_client *client)
212 {
213 	struct smsc47b397_data *data = i2c_get_clientdata(client);
214 	int err;
215 
216 	hwmon_device_unregister(data->class_dev);
217 
218 	if ((err = i2c_detach_client(client)))
219 		return err;
220 
221 	release_region(client->addr, SMSC_EXTENT);
222 	kfree(data);
223 
224 	return 0;
225 }
226 
227 static int smsc47b397_detect(struct i2c_adapter *adapter);
228 
229 static struct i2c_driver smsc47b397_driver = {
230 	.driver = {
231 		.name	= "smsc47b397",
232 	},
233 	.attach_adapter	= smsc47b397_detect,
234 	.detach_client	= smsc47b397_detach_client,
235 };
236 
237 static int smsc47b397_detect(struct i2c_adapter *adapter)
238 {
239 	struct i2c_client *new_client;
240 	struct smsc47b397_data *data;
241 	int err = 0;
242 
243 	if (!request_region(address, SMSC_EXTENT,
244 			    smsc47b397_driver.driver.name)) {
245 		dev_err(&adapter->dev, "Region 0x%x already in use!\n",
246 			address);
247 		return -EBUSY;
248 	}
249 
250 	if (!(data = kzalloc(sizeof(struct smsc47b397_data), GFP_KERNEL))) {
251 		err = -ENOMEM;
252 		goto error_release;
253 	}
254 
255 	new_client = &data->client;
256 	i2c_set_clientdata(new_client, data);
257 	new_client->addr = address;
258 	mutex_init(&data->lock);
259 	new_client->adapter = adapter;
260 	new_client->driver = &smsc47b397_driver;
261 	new_client->flags = 0;
262 
263 	strlcpy(new_client->name, "smsc47b397", I2C_NAME_SIZE);
264 
265 	mutex_init(&data->update_lock);
266 
267 	if ((err = i2c_attach_client(new_client)))
268 		goto error_free;
269 
270 	data->class_dev = hwmon_device_register(&new_client->dev);
271 	if (IS_ERR(data->class_dev)) {
272 		err = PTR_ERR(data->class_dev);
273 		goto error_detach;
274 	}
275 
276 	device_create_file_temp(new_client, 1);
277 	device_create_file_temp(new_client, 2);
278 	device_create_file_temp(new_client, 3);
279 	device_create_file_temp(new_client, 4);
280 
281 	device_create_file_fan(new_client, 1);
282 	device_create_file_fan(new_client, 2);
283 	device_create_file_fan(new_client, 3);
284 	device_create_file_fan(new_client, 4);
285 
286 	return 0;
287 
288 error_detach:
289 	i2c_detach_client(new_client);
290 error_free:
291 	kfree(data);
292 error_release:
293 	release_region(address, SMSC_EXTENT);
294 	return err;
295 }
296 
297 static int __init smsc47b397_find(unsigned short *addr)
298 {
299 	u8 id, rev;
300 
301 	superio_enter();
302 	id = superio_inb(SUPERIO_REG_DEVID);
303 
304 	if ((id != 0x6f) && (id != 0x81)) {
305 		superio_exit();
306 		return -ENODEV;
307 	}
308 
309 	rev = superio_inb(SUPERIO_REG_DEVREV);
310 
311 	superio_select(SUPERIO_REG_LD8);
312 	*addr = (superio_inb(SUPERIO_REG_BASE_MSB) << 8)
313 		 |  superio_inb(SUPERIO_REG_BASE_LSB);
314 
315 	printk(KERN_INFO "smsc47b397: found SMSC %s "
316 		"(base address 0x%04x, revision %u)\n",
317 		id == 0x81 ? "SCH5307-NS" : "LPC47B397-NC", *addr, rev);
318 
319 	superio_exit();
320 	return 0;
321 }
322 
323 static int __init smsc47b397_init(void)
324 {
325 	int ret;
326 
327 	if ((ret = smsc47b397_find(&address)))
328 		return ret;
329 
330 	return i2c_isa_add_driver(&smsc47b397_driver);
331 }
332 
333 static void __exit smsc47b397_exit(void)
334 {
335 	i2c_isa_del_driver(&smsc47b397_driver);
336 }
337 
338 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
339 MODULE_DESCRIPTION("SMSC LPC47B397 driver");
340 MODULE_LICENSE("GPL");
341 
342 module_init(smsc47b397_init);
343 module_exit(smsc47b397_exit);
344