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