blob: 73bc2ffc598d2cabf66664f34b43b9f9dbb2d9ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 lm78.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/jiffies.h>
25#include <linux/i2c.h>
Jean Delvarefde09502005-07-19 23:51:07 +020026#include <linux/i2c-isa.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040027#include <linux/hwmon.h>
Jean Delvare19f673e2005-07-31 22:12:09 +020028#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32
33/* Addresses to scan */
34static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24,
35 0x25, 0x26, 0x27, 0x28, 0x29,
36 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
37 0x2f, I2C_CLIENT_END };
Jean Delvare2d8672c2005-07-19 23:56:35 +020038static unsigned short isa_address = 0x290;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020041I2C_CLIENT_INSMOD_2(lm78, lm79);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* Many LM78 constants specified below */
44
45/* Length of ISA address segment */
46#define LM78_EXTENT 8
47
48/* Where are the ISA address/data registers relative to the base address */
49#define LM78_ADDR_REG_OFFSET 5
50#define LM78_DATA_REG_OFFSET 6
51
52/* The LM78 registers */
53#define LM78_REG_IN_MAX(nr) (0x2b + (nr) * 2)
54#define LM78_REG_IN_MIN(nr) (0x2c + (nr) * 2)
55#define LM78_REG_IN(nr) (0x20 + (nr))
56
57#define LM78_REG_FAN_MIN(nr) (0x3b + (nr))
58#define LM78_REG_FAN(nr) (0x28 + (nr))
59
60#define LM78_REG_TEMP 0x27
61#define LM78_REG_TEMP_OVER 0x39
62#define LM78_REG_TEMP_HYST 0x3a
63
64#define LM78_REG_ALARM1 0x41
65#define LM78_REG_ALARM2 0x42
66
67#define LM78_REG_VID_FANDIV 0x47
68
69#define LM78_REG_CONFIG 0x40
70#define LM78_REG_CHIPID 0x49
71#define LM78_REG_I2C_ADDR 0x48
72
73
74/* Conversions. Rounding and limit checking is only done on the TO_REG
75 variants. */
76
77/* IN: mV, (0V to 4.08V)
78 REG: 16mV/bit */
79static inline u8 IN_TO_REG(unsigned long val)
80{
81 unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
82 return (nval + 8) / 16;
83}
84#define IN_FROM_REG(val) ((val) * 16)
85
86static inline u8 FAN_TO_REG(long rpm, int div)
87{
88 if (rpm <= 0)
89 return 255;
90 return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
91}
92
93static inline int FAN_FROM_REG(u8 val, int div)
94{
95 return val==0 ? -1 : val==255 ? 0 : 1350000/(val*div);
96}
97
98/* TEMP: mC (-128C to +127C)
99 REG: 1C/bit, two's complement */
100static inline s8 TEMP_TO_REG(int val)
101{
102 int nval = SENSORS_LIMIT(val, -128000, 127000) ;
103 return nval<0 ? (nval-500)/1000 : (nval+500)/1000;
104}
105
106static inline int TEMP_FROM_REG(s8 val)
107{
108 return val * 1000;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define DIV_FROM_REG(val) (1 << (val))
112
113/* There are some complications in a module like this. First off, LM78 chips
114 may be both present on the SMBus and the ISA bus, and we have to handle
115 those cases separately at some places. Second, there might be several
116 LM78 chips available (well, actually, that is probably never done; but
117 it is a clean illustration of how to handle a case like that). Finally,
118 a specific chip may be attached to *both* ISA and SMBus, and we would
119 not like to detect it double. Fortunately, in the case of the LM78 at
120 least, a register tells us what SMBus address we are on, so that helps
121 a bit - except if there could be more than one SMBus. Groan. No solution
122 for this yet. */
123
124/* This module may seem overly long and complicated. In fact, it is not so
125 bad. Quite a lot of bookkeeping is done. A real driver can often cut
126 some corners. */
127
128/* For each registered LM78, we need to keep some data in memory. That
129 data is pointed to by lm78_list[NR]->data. The structure itself is
130 dynamically allocated, at the same time when a new lm78 client is
131 allocated. */
132struct lm78_data {
133 struct i2c_client client;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400134 struct class_device *class_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100135 struct mutex lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 enum chips type;
137
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100138 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 char valid; /* !=0 if following fields are valid */
140 unsigned long last_updated; /* In jiffies */
141
142 u8 in[7]; /* Register value */
143 u8 in_max[7]; /* Register value */
144 u8 in_min[7]; /* Register value */
145 u8 fan[3]; /* Register value */
146 u8 fan_min[3]; /* Register value */
147 s8 temp; /* Register value */
148 s8 temp_over; /* Register value */
149 s8 temp_hyst; /* Register value */
150 u8 fan_div[3]; /* Register encoding, shifted right */
151 u8 vid; /* Register encoding, combined */
152 u16 alarms; /* Register encoding, combined */
153};
154
155
156static int lm78_attach_adapter(struct i2c_adapter *adapter);
Jean Delvare2d8672c2005-07-19 23:56:35 +0200157static int lm78_isa_attach_adapter(struct i2c_adapter *adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int lm78_detect(struct i2c_adapter *adapter, int address, int kind);
159static int lm78_detach_client(struct i2c_client *client);
160
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100161static int lm78_read_value(struct i2c_client *client, u8 reg);
162static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static struct lm78_data *lm78_update_device(struct device *dev);
164static void lm78_init_client(struct i2c_client *client);
165
166
167static struct i2c_driver lm78_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100168 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100169 .name = "lm78",
170 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .id = I2C_DRIVERID_LM78,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .attach_adapter = lm78_attach_adapter,
173 .detach_client = lm78_detach_client,
174};
175
Jean Delvarefde09502005-07-19 23:51:07 +0200176static struct i2c_driver lm78_isa_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100177 .driver = {
Jean Delvare87218842006-09-03 22:36:14 +0200178 .owner = THIS_MODULE,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100179 .name = "lm78-isa",
180 },
Jean Delvare2d8672c2005-07-19 23:56:35 +0200181 .attach_adapter = lm78_isa_attach_adapter,
Jean Delvarefde09502005-07-19 23:51:07 +0200182 .detach_client = lm78_detach_client,
183};
184
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/* 7 Voltages */
187static ssize_t show_in(struct device *dev, char *buf, int nr)
188{
189 struct lm78_data *data = lm78_update_device(dev);
190 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
191}
192
193static ssize_t show_in_min(struct device *dev, char *buf, int nr)
194{
195 struct lm78_data *data = lm78_update_device(dev);
196 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
197}
198
199static ssize_t show_in_max(struct device *dev, char *buf, int nr)
200{
201 struct lm78_data *data = lm78_update_device(dev);
202 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
203}
204
205static ssize_t set_in_min(struct device *dev, const char *buf,
206 size_t count, int nr)
207{
208 struct i2c_client *client = to_i2c_client(dev);
209 struct lm78_data *data = i2c_get_clientdata(client);
210 unsigned long val = simple_strtoul(buf, NULL, 10);
211
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100212 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 data->in_min[nr] = IN_TO_REG(val);
214 lm78_write_value(client, LM78_REG_IN_MIN(nr), data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100215 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return count;
217}
218
219static ssize_t set_in_max(struct device *dev, const char *buf,
220 size_t count, int nr)
221{
222 struct i2c_client *client = to_i2c_client(dev);
223 struct lm78_data *data = i2c_get_clientdata(client);
224 unsigned long val = simple_strtoul(buf, NULL, 10);
225
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100226 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 data->in_max[nr] = IN_TO_REG(val);
228 lm78_write_value(client, LM78_REG_IN_MAX(nr), data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100229 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return count;
231}
232
233#define show_in_offset(offset) \
234static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400235 show_in##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{ \
237 return show_in(dev, buf, offset); \
238} \
239static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
240 show_in##offset, NULL); \
241static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400242 show_in##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{ \
244 return show_in_min(dev, buf, offset); \
245} \
246static ssize_t \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400247 show_in##offset##_max (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{ \
249 return show_in_max(dev, buf, offset); \
250} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400251static ssize_t set_in##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 const char *buf, size_t count) \
253{ \
254 return set_in_min(dev, buf, count, offset); \
255} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400256static ssize_t set_in##offset##_max (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 const char *buf, size_t count) \
258{ \
259 return set_in_max(dev, buf, count, offset); \
260} \
261static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
262 show_in##offset##_min, set_in##offset##_min); \
263static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
264 show_in##offset##_max, set_in##offset##_max);
265
266show_in_offset(0);
267show_in_offset(1);
268show_in_offset(2);
269show_in_offset(3);
270show_in_offset(4);
271show_in_offset(5);
272show_in_offset(6);
273
274/* Temperature */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400275static ssize_t show_temp(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct lm78_data *data = lm78_update_device(dev);
278 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
279}
280
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400281static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct lm78_data *data = lm78_update_device(dev);
284 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
285}
286
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400287static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct i2c_client *client = to_i2c_client(dev);
290 struct lm78_data *data = i2c_get_clientdata(client);
291 long val = simple_strtol(buf, NULL, 10);
292
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100293 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 data->temp_over = TEMP_TO_REG(val);
295 lm78_write_value(client, LM78_REG_TEMP_OVER, data->temp_over);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100296 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return count;
298}
299
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400300static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct lm78_data *data = lm78_update_device(dev);
303 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
304}
305
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400306static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct i2c_client *client = to_i2c_client(dev);
309 struct lm78_data *data = i2c_get_clientdata(client);
310 long val = simple_strtol(buf, NULL, 10);
311
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100312 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 data->temp_hyst = TEMP_TO_REG(val);
314 lm78_write_value(client, LM78_REG_TEMP_HYST, data->temp_hyst);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100315 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return count;
317}
318
319static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
320static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
321 show_temp_over, set_temp_over);
322static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
323 show_temp_hyst, set_temp_hyst);
324
325/* 3 Fans */
326static ssize_t show_fan(struct device *dev, char *buf, int nr)
327{
328 struct lm78_data *data = lm78_update_device(dev);
329 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
330 DIV_FROM_REG(data->fan_div[nr])) );
331}
332
333static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
334{
335 struct lm78_data *data = lm78_update_device(dev);
336 return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr],
337 DIV_FROM_REG(data->fan_div[nr])) );
338}
339
340static ssize_t set_fan_min(struct device *dev, const char *buf,
341 size_t count, int nr)
342{
343 struct i2c_client *client = to_i2c_client(dev);
344 struct lm78_data *data = i2c_get_clientdata(client);
345 unsigned long val = simple_strtoul(buf, NULL, 10);
346
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100347 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
349 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100350 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return count;
352}
353
354static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
355{
356 struct lm78_data *data = lm78_update_device(dev);
357 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]) );
358}
359
360/* Note: we save and restore the fan minimum here, because its value is
361 determined in part by the fan divisor. This follows the principle of
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200362 least surprise; the user doesn't expect the fan minimum to change just
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 because the divisor changed. */
364static ssize_t set_fan_div(struct device *dev, const char *buf,
365 size_t count, int nr)
366{
367 struct i2c_client *client = to_i2c_client(dev);
368 struct lm78_data *data = i2c_get_clientdata(client);
369 unsigned long val = simple_strtoul(buf, NULL, 10);
370 unsigned long min;
371 u8 reg;
372
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100373 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 min = FAN_FROM_REG(data->fan_min[nr],
375 DIV_FROM_REG(data->fan_div[nr]));
376
377 switch (val) {
378 case 1: data->fan_div[nr] = 0; break;
379 case 2: data->fan_div[nr] = 1; break;
380 case 4: data->fan_div[nr] = 2; break;
381 case 8: data->fan_div[nr] = 3; break;
382 default:
383 dev_err(&client->dev, "fan_div value %ld not "
384 "supported. Choose one of 1, 2, 4 or 8!\n", val);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100385 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -EINVAL;
387 }
388
389 reg = lm78_read_value(client, LM78_REG_VID_FANDIV);
390 switch (nr) {
391 case 0:
392 reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
393 break;
394 case 1:
395 reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
396 break;
397 }
398 lm78_write_value(client, LM78_REG_VID_FANDIV, reg);
399
400 data->fan_min[nr] =
401 FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
402 lm78_write_value(client, LM78_REG_FAN_MIN(nr), data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100403 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 return count;
406}
407
408#define show_fan_offset(offset) \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400409static ssize_t show_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{ \
411 return show_fan(dev, buf, offset - 1); \
412} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400413static ssize_t show_fan_##offset##_min (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{ \
415 return show_fan_min(dev, buf, offset - 1); \
416} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400417static ssize_t show_fan_##offset##_div (struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{ \
419 return show_fan_div(dev, buf, offset - 1); \
420} \
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400421static ssize_t set_fan_##offset##_min (struct device *dev, struct device_attribute *attr, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 const char *buf, size_t count) \
423{ \
424 return set_fan_min(dev, buf, count, offset - 1); \
425} \
426static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan_##offset, NULL);\
427static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
428 show_fan_##offset##_min, set_fan_##offset##_min);
429
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400430static ssize_t set_fan_1_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 size_t count)
432{
433 return set_fan_div(dev, buf, count, 0) ;
434}
435
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400436static ssize_t set_fan_2_div(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 size_t count)
438{
439 return set_fan_div(dev, buf, count, 1) ;
440}
441
442show_fan_offset(1);
443show_fan_offset(2);
444show_fan_offset(3);
445
446/* Fan 3 divisor is locked in H/W */
447static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
448 show_fan_1_div, set_fan_1_div);
449static DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
450 show_fan_2_div, set_fan_2_div);
451static DEVICE_ATTR(fan3_div, S_IRUGO, show_fan_3_div, NULL);
452
453/* VID */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400454static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct lm78_data *data = lm78_update_device(dev);
Jean Delvared0d3cd62005-11-23 15:44:26 -0800457 return sprintf(buf, "%d\n", vid_from_reg(data->vid, 82));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
460
461/* Alarms */
Yani Ioannou8627f9b2005-05-17 06:42:03 -0400462static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct lm78_data *data = lm78_update_device(dev);
465 return sprintf(buf, "%u\n", data->alarms);
466}
467static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
468
469/* This function is called when:
470 * lm78_driver is inserted (when this module is loaded), for each
471 available adapter
472 * when a new adapter is inserted (and lm78_driver is still present) */
473static int lm78_attach_adapter(struct i2c_adapter *adapter)
474{
475 if (!(adapter->class & I2C_CLASS_HWMON))
476 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200477 return i2c_probe(adapter, &addr_data, lm78_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Jean Delvare2d8672c2005-07-19 23:56:35 +0200480static int lm78_isa_attach_adapter(struct i2c_adapter *adapter)
481{
482 return lm78_detect(adapter, isa_address, -1);
483}
484
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200485static struct attribute *lm78_attributes[] = {
486 &dev_attr_in0_input.attr,
487 &dev_attr_in0_min.attr,
488 &dev_attr_in0_max.attr,
489 &dev_attr_in1_input.attr,
490 &dev_attr_in1_min.attr,
491 &dev_attr_in1_max.attr,
492 &dev_attr_in2_input.attr,
493 &dev_attr_in2_min.attr,
494 &dev_attr_in2_max.attr,
495 &dev_attr_in3_input.attr,
496 &dev_attr_in3_min.attr,
497 &dev_attr_in3_max.attr,
498 &dev_attr_in4_input.attr,
499 &dev_attr_in4_min.attr,
500 &dev_attr_in4_max.attr,
501 &dev_attr_in5_input.attr,
502 &dev_attr_in5_min.attr,
503 &dev_attr_in5_max.attr,
504 &dev_attr_in6_input.attr,
505 &dev_attr_in6_min.attr,
506 &dev_attr_in6_max.attr,
507 &dev_attr_temp1_input.attr,
508 &dev_attr_temp1_max.attr,
509 &dev_attr_temp1_max_hyst.attr,
510 &dev_attr_fan1_input.attr,
511 &dev_attr_fan1_min.attr,
512 &dev_attr_fan1_div.attr,
513 &dev_attr_fan2_input.attr,
514 &dev_attr_fan2_min.attr,
515 &dev_attr_fan2_div.attr,
516 &dev_attr_fan3_input.attr,
517 &dev_attr_fan3_min.attr,
518 &dev_attr_fan3_div.attr,
519 &dev_attr_alarms.attr,
520 &dev_attr_cpu0_vid.attr,
521
522 NULL
523};
524
525static const struct attribute_group lm78_group = {
526 .attrs = lm78_attributes,
527};
528
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200529/* This function is called by i2c_probe */
Ben Dooksd8d20612005-10-26 21:05:46 +0200530static int lm78_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 int i, err;
533 struct i2c_client *new_client;
534 struct lm78_data *data;
535 const char *client_name = "";
536 int is_isa = i2c_is_isa_adapter(adapter);
537
538 if (!is_isa &&
539 !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
540 err = -ENODEV;
541 goto ERROR0;
542 }
543
544 /* Reserve the ISA region */
545 if (is_isa)
Jean Delvarefde09502005-07-19 23:51:07 +0200546 if (!request_region(address, LM78_EXTENT,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100547 lm78_isa_driver.driver.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 err = -EBUSY;
549 goto ERROR0;
550 }
551
552 /* Probe whether there is anything available on this address. Already
553 done for SMBus clients */
554 if (kind < 0) {
555 if (is_isa) {
556
557#define REALLY_SLOW_IO
558 /* We need the timeouts for at least some LM78-like
559 chips. But only if we read 'undefined' registers. */
560 i = inb_p(address + 1);
561 if (inb_p(address + 2) != i) {
562 err = -ENODEV;
563 goto ERROR1;
564 }
565 if (inb_p(address + 3) != i) {
566 err = -ENODEV;
567 goto ERROR1;
568 }
569 if (inb_p(address + 7) != i) {
570 err = -ENODEV;
571 goto ERROR1;
572 }
573#undef REALLY_SLOW_IO
574
575 /* Let's just hope nothing breaks here */
576 i = inb_p(address + 5) & 0x7f;
577 outb_p(~i & 0x7f, address + 5);
578 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
579 outb_p(i, address + 5);
580 err = -ENODEV;
581 goto ERROR1;
582 }
583 }
584 }
585
586 /* OK. For now, we presume we have a valid client. We now create the
587 client structure, even though we cannot fill it completely yet.
588 But it allows us to access lm78_{read,write}_value. */
589
Deepak Saxenaba9c2e82005-10-17 23:08:32 +0200590 if (!(data = kzalloc(sizeof(struct lm78_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 err = -ENOMEM;
592 goto ERROR1;
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 new_client = &data->client;
596 if (is_isa)
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100597 mutex_init(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 i2c_set_clientdata(new_client, data);
599 new_client->addr = address;
600 new_client->adapter = adapter;
Jean Delvarefde09502005-07-19 23:51:07 +0200601 new_client->driver = is_isa ? &lm78_isa_driver : &lm78_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 new_client->flags = 0;
603
604 /* Now, we do the remaining detection. */
605 if (kind < 0) {
606 if (lm78_read_value(new_client, LM78_REG_CONFIG) & 0x80) {
607 err = -ENODEV;
608 goto ERROR2;
609 }
610 if (!is_isa && (lm78_read_value(
611 new_client, LM78_REG_I2C_ADDR) != address)) {
612 err = -ENODEV;
613 goto ERROR2;
614 }
615 }
616
617 /* Determine the chip type. */
618 if (kind <= 0) {
619 i = lm78_read_value(new_client, LM78_REG_CHIPID);
Jean Delvare27fe0482005-07-27 21:30:16 +0200620 if (i == 0x00 || i == 0x20 /* LM78 */
621 || i == 0x40) /* LM78-J */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 kind = lm78;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 else if ((i & 0xfe) == 0xc0)
624 kind = lm79;
625 else {
626 if (kind == 0)
627 dev_warn(&adapter->dev, "Ignoring 'force' "
628 "parameter for unknown chip at "
629 "adapter %d, address 0x%02x\n",
630 i2c_adapter_id(adapter), address);
631 err = -ENODEV;
632 goto ERROR2;
633 }
634 }
635
636 if (kind == lm78) {
637 client_name = "lm78";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 } else if (kind == lm79) {
639 client_name = "lm79";
640 }
641
642 /* Fill in the remaining client fields and put into the global list */
643 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
644 data->type = kind;
645
646 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100647 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /* Tell the I2C layer a new client has arrived */
650 if ((err = i2c_attach_client(new_client)))
651 goto ERROR2;
652
653 /* Initialize the LM78 chip */
654 lm78_init_client(new_client);
655
656 /* A few vars need to be filled upon startup */
657 for (i = 0; i < 3; i++) {
658 data->fan_min[i] = lm78_read_value(new_client,
659 LM78_REG_FAN_MIN(i));
660 }
661
662 /* Register sysfs hooks */
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200663 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm78_group)))
664 goto ERROR3;
665
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400666 data->class_dev = hwmon_device_register(&new_client->dev);
667 if (IS_ERR(data->class_dev)) {
668 err = PTR_ERR(data->class_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200669 goto ERROR4;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400670 }
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 0;
673
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200674ERROR4:
675 sysfs_remove_group(&new_client->dev.kobj, &lm78_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400676ERROR3:
677 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678ERROR2:
679 kfree(data);
680ERROR1:
681 if (is_isa)
682 release_region(address, LM78_EXTENT);
683ERROR0:
684 return err;
685}
686
687static int lm78_detach_client(struct i2c_client *client)
688{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400689 struct lm78_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int err;
691
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400692 hwmon_device_unregister(data->class_dev);
Mark M. Hoffmanc1685f62006-09-24 20:59:49 +0200693 sysfs_remove_group(&client->dev.kobj, &lm78_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400694
Jean Delvare7bef5592005-07-27 22:14:49 +0200695 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if(i2c_is_isa_client(client))
699 release_region(client->addr, LM78_EXTENT);
700
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400701 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 return 0;
704}
705
Steven Cole44bbe872005-05-03 18:21:25 -0600706/* The SMBus locks itself, but ISA access must be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 We don't want to lock the whole ISA bus, so we lock each client
708 separately.
709 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
710 would slow down the LM78 access and should not be necessary. */
711static int lm78_read_value(struct i2c_client *client, u8 reg)
712{
713 int res;
714 if (i2c_is_isa_client(client)) {
715 struct lm78_data *data = i2c_get_clientdata(client);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100716 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
718 res = inb_p(client->addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100719 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return res;
721 } else
722 return i2c_smbus_read_byte_data(client, reg);
723}
724
Steven Cole44bbe872005-05-03 18:21:25 -0600725/* The SMBus locks itself, but ISA access muse be locked explicitly!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 We don't want to lock the whole ISA bus, so we lock each client
727 separately.
728 We ignore the LM78 BUSY flag at this moment - it could lead to deadlocks,
729 would slow down the LM78 access and should not be necessary.
730 There are some ugly typecasts here, but the good new is - they should
731 nowhere else be necessary! */
732static int lm78_write_value(struct i2c_client *client, u8 reg, u8 value)
733{
734 if (i2c_is_isa_client(client)) {
735 struct lm78_data *data = i2c_get_clientdata(client);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100736 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 outb_p(reg, client->addr + LM78_ADDR_REG_OFFSET);
738 outb_p(value, client->addr + LM78_DATA_REG_OFFSET);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100739 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 0;
741 } else
742 return i2c_smbus_write_byte_data(client, reg, value);
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745static void lm78_init_client(struct i2c_client *client)
746{
747 u8 config = lm78_read_value(client, LM78_REG_CONFIG);
748
749 /* Start monitoring */
750 if (!(config & 0x01))
751 lm78_write_value(client, LM78_REG_CONFIG,
752 (config & 0xf7) | 0x01);
753}
754
755static struct lm78_data *lm78_update_device(struct device *dev)
756{
757 struct i2c_client *client = to_i2c_client(dev);
758 struct lm78_data *data = i2c_get_clientdata(client);
759 int i;
760
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100761 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
764 || !data->valid) {
765
766 dev_dbg(&client->dev, "Starting lm78 update\n");
767
768 for (i = 0; i <= 6; i++) {
769 data->in[i] =
770 lm78_read_value(client, LM78_REG_IN(i));
771 data->in_min[i] =
772 lm78_read_value(client, LM78_REG_IN_MIN(i));
773 data->in_max[i] =
774 lm78_read_value(client, LM78_REG_IN_MAX(i));
775 }
776 for (i = 0; i < 3; i++) {
777 data->fan[i] =
778 lm78_read_value(client, LM78_REG_FAN(i));
779 data->fan_min[i] =
780 lm78_read_value(client, LM78_REG_FAN_MIN(i));
781 }
782 data->temp = lm78_read_value(client, LM78_REG_TEMP);
783 data->temp_over =
784 lm78_read_value(client, LM78_REG_TEMP_OVER);
785 data->temp_hyst =
786 lm78_read_value(client, LM78_REG_TEMP_HYST);
787 i = lm78_read_value(client, LM78_REG_VID_FANDIV);
788 data->vid = i & 0x0f;
789 if (data->type == lm79)
790 data->vid |=
791 (lm78_read_value(client, LM78_REG_CHIPID) &
792 0x01) << 4;
793 else
794 data->vid |= 0x10;
795 data->fan_div[0] = (i >> 4) & 0x03;
796 data->fan_div[1] = i >> 6;
797 data->alarms = lm78_read_value(client, LM78_REG_ALARM1) +
798 (lm78_read_value(client, LM78_REG_ALARM2) << 8);
799 data->last_updated = jiffies;
800 data->valid = 1;
801
802 data->fan_div[2] = 1;
803 }
804
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100805 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 return data;
808}
809
810static int __init sm_lm78_init(void)
811{
Jean Delvarefde09502005-07-19 23:51:07 +0200812 int res;
813
814 res = i2c_add_driver(&lm78_driver);
815 if (res)
816 return res;
817
Jean Delvaree6938102006-10-13 16:56:28 +0200818 /* Don't exit if this one fails, we still want the I2C variants
819 to work! */
820 if (i2c_isa_add_driver(&lm78_isa_driver))
821 isa_address = 0;
Jean Delvarefde09502005-07-19 23:51:07 +0200822
823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826static void __exit sm_lm78_exit(void)
827{
Jean Delvaree6938102006-10-13 16:56:28 +0200828 if (isa_address)
829 i2c_isa_del_driver(&lm78_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 i2c_del_driver(&lm78_driver);
831}
832
833
834
835MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
Jean Delvare27fe0482005-07-27 21:30:16 +0200836MODULE_DESCRIPTION("LM78/LM79 driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837MODULE_LICENSE("GPL");
838
839module_init(sm_lm78_init);
840module_exit(sm_lm78_exit);