2 thmc50.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (C) 2007 Krzysztof Helt <krzysztof.h1@wp.pl>
5 Based on 2.4 driver by Frodo Looijaard <frodol@dds.nl> and
6 Philip Edelbrock <phil@netroedge.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/hwmon.h>
28 #include <linux/hwmon-sysfs.h>
29 #include <linux/err.h>
30 #include <linux/mutex.h>
32 MODULE_LICENSE("GPL");
34 /* Addresses to scan */
35 static unsigned short normal_i2c
[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
};
37 /* Insmod parameters */
38 I2C_CLIENT_INSMOD_2(thmc50
, adm1022
);
39 I2C_CLIENT_MODULE_PARM(adm1022_temp3
, "List of adapter,address pairs "
40 "to enable 3rd temperature (ADM1022 only)");
42 /* Many THMC50 constants specified below */
44 /* The THMC50 registers */
45 #define THMC50_REG_CONF 0x40
46 #define THMC50_REG_COMPANY_ID 0x3E
47 #define THMC50_REG_DIE_CODE 0x3F
48 #define THMC50_REG_ANALOG_OUT 0x19
50 * The mirror status register cannot be used as
51 * reading it does not clear alarms.
53 #define THMC50_REG_INTR 0x41
55 const static u8 THMC50_REG_TEMP
[] = { 0x27, 0x26, 0x20 };
56 const static u8 THMC50_REG_TEMP_MIN
[] = { 0x3A, 0x38, 0x2C };
57 const static u8 THMC50_REG_TEMP_MAX
[] = { 0x39, 0x37, 0x2B };
59 #define THMC50_REG_CONF_nFANOFF 0x20
61 /* Each client has this additional data */
63 struct i2c_client client
;
64 struct device
*hwmon_dev
;
66 struct mutex update_lock
;
68 unsigned long last_updated
; /* In jiffies */
69 char has_temp3
; /* !=0 if it is ADM1022 in temp3 mode */
70 char valid
; /* !=0 if following fields are valid */
80 static int thmc50_attach_adapter(struct i2c_adapter
*adapter
);
81 static int thmc50_detach_client(struct i2c_client
*client
);
82 static void thmc50_init_client(struct i2c_client
*client
);
83 static struct thmc50_data
*thmc50_update_device(struct device
*dev
);
85 static struct i2c_driver thmc50_driver
= {
89 .attach_adapter
= thmc50_attach_adapter
,
90 .detach_client
= thmc50_detach_client
,
93 static ssize_t
show_analog_out(struct device
*dev
,
94 struct device_attribute
*attr
, char *buf
)
96 struct thmc50_data
*data
= thmc50_update_device(dev
);
97 return sprintf(buf
, "%d\n", data
->analog_out
);
100 static ssize_t
set_analog_out(struct device
*dev
,
101 struct device_attribute
*attr
,
102 const char *buf
, size_t count
)
104 struct i2c_client
*client
= to_i2c_client(dev
);
105 struct thmc50_data
*data
= i2c_get_clientdata(client
);
106 int tmp
= simple_strtoul(buf
, NULL
, 10);
109 mutex_lock(&data
->update_lock
);
110 data
->analog_out
= SENSORS_LIMIT(tmp
, 0, 255);
111 i2c_smbus_write_byte_data(client
, THMC50_REG_ANALOG_OUT
,
114 config
= i2c_smbus_read_byte_data(client
, THMC50_REG_CONF
);
115 if (data
->analog_out
== 0)
116 config
&= ~THMC50_REG_CONF_nFANOFF
;
118 config
|= THMC50_REG_CONF_nFANOFF
;
119 i2c_smbus_write_byte_data(client
, THMC50_REG_CONF
, config
);
121 mutex_unlock(&data
->update_lock
);
125 /* There is only one PWM mode = DC */
126 static ssize_t
show_pwm_mode(struct device
*dev
, struct device_attribute
*attr
,
129 return sprintf(buf
, "0\n");
133 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
136 int nr
= to_sensor_dev_attr(attr
)->index
;
137 struct thmc50_data
*data
= thmc50_update_device(dev
);
138 return sprintf(buf
, "%d\n", data
->temp_input
[nr
] * 1000);
141 static ssize_t
show_temp_min(struct device
*dev
, struct device_attribute
*attr
,
144 int nr
= to_sensor_dev_attr(attr
)->index
;
145 struct thmc50_data
*data
= thmc50_update_device(dev
);
146 return sprintf(buf
, "%d\n", data
->temp_min
[nr
] * 1000);
149 static ssize_t
set_temp_min(struct device
*dev
, struct device_attribute
*attr
,
150 const char *buf
, size_t count
)
152 int nr
= to_sensor_dev_attr(attr
)->index
;
153 struct i2c_client
*client
= to_i2c_client(dev
);
154 struct thmc50_data
*data
= i2c_get_clientdata(client
);
155 int val
= simple_strtol(buf
, NULL
, 10);
157 mutex_lock(&data
->update_lock
);
158 data
->temp_min
[nr
] = SENSORS_LIMIT(val
/ 1000, -128, 127);
159 i2c_smbus_write_byte_data(client
, THMC50_REG_TEMP_MIN
[nr
],
161 mutex_unlock(&data
->update_lock
);
165 static ssize_t
show_temp_max(struct device
*dev
, struct device_attribute
*attr
,
168 int nr
= to_sensor_dev_attr(attr
)->index
;
169 struct thmc50_data
*data
= thmc50_update_device(dev
);
170 return sprintf(buf
, "%d\n", data
->temp_max
[nr
] * 1000);
173 static ssize_t
set_temp_max(struct device
*dev
, struct device_attribute
*attr
,
174 const char *buf
, size_t count
)
176 int nr
= to_sensor_dev_attr(attr
)->index
;
177 struct i2c_client
*client
= to_i2c_client(dev
);
178 struct thmc50_data
*data
= i2c_get_clientdata(client
);
179 int val
= simple_strtol(buf
, NULL
, 10);
181 mutex_lock(&data
->update_lock
);
182 data
->temp_max
[nr
] = SENSORS_LIMIT(val
/ 1000, -128, 127);
183 i2c_smbus_write_byte_data(client
, THMC50_REG_TEMP_MAX
[nr
],
185 mutex_unlock(&data
->update_lock
);
189 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
192 int index
= to_sensor_dev_attr(attr
)->index
;
193 struct thmc50_data
*data
= thmc50_update_device(dev
);
195 return sprintf(buf
, "%u\n", (data
->alarms
>> index
) & 1);
198 #define temp_reg(offset) \
199 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
201 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
202 show_temp_min, set_temp_min, offset - 1); \
203 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
204 show_temp_max, set_temp_max, offset - 1);
210 static SENSOR_DEVICE_ATTR(temp1_alarm
, S_IRUGO
, show_alarm
, NULL
, 0);
211 static SENSOR_DEVICE_ATTR(temp2_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
212 static SENSOR_DEVICE_ATTR(temp3_alarm
, S_IRUGO
, show_alarm
, NULL
, 1);
213 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 7);
214 static SENSOR_DEVICE_ATTR(temp3_fault
, S_IRUGO
, show_alarm
, NULL
, 2);
216 static SENSOR_DEVICE_ATTR(pwm1
, S_IRUGO
| S_IWUSR
, show_analog_out
,
218 static SENSOR_DEVICE_ATTR(pwm1_mode
, S_IRUGO
, show_pwm_mode
, NULL
, 0);
220 static struct attribute
*thmc50_attributes
[] = {
221 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
222 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
223 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
224 &sensor_dev_attr_temp1_alarm
.dev_attr
.attr
,
225 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
226 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
227 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
228 &sensor_dev_attr_temp2_alarm
.dev_attr
.attr
,
229 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
230 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
231 &sensor_dev_attr_pwm1_mode
.dev_attr
.attr
,
235 static const struct attribute_group thmc50_group
= {
236 .attrs
= thmc50_attributes
,
239 /* for ADM1022 3rd temperature mode */
240 static struct attribute
*temp3_attributes
[] = {
241 &sensor_dev_attr_temp3_max
.dev_attr
.attr
,
242 &sensor_dev_attr_temp3_min
.dev_attr
.attr
,
243 &sensor_dev_attr_temp3_input
.dev_attr
.attr
,
244 &sensor_dev_attr_temp3_alarm
.dev_attr
.attr
,
245 &sensor_dev_attr_temp3_fault
.dev_attr
.attr
,
249 static const struct attribute_group temp3_group
= {
250 .attrs
= temp3_attributes
,
253 static int thmc50_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
258 struct i2c_client
*client
;
259 struct thmc50_data
*data
;
262 const char *type_name
;
264 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
265 pr_debug("thmc50: detect failed, "
266 "smbus byte data not supported!\n");
270 /* OK. For now, we presume we have a valid client. We now create the
271 client structure, even though we cannot fill it completely yet.
272 But it allows us to access thmc50 registers. */
273 if (!(data
= kzalloc(sizeof(struct thmc50_data
), GFP_KERNEL
))) {
274 pr_debug("thmc50: detect failed, kzalloc failed!\n");
279 client
= &data
->client
;
280 i2c_set_clientdata(client
, data
);
281 client
->addr
= address
;
282 client
->adapter
= adapter
;
283 client
->driver
= &thmc50_driver
;
286 pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
287 client
->addr
, i2c_adapter_id(client
->adapter
));
289 /* Now, we do the remaining detection. */
290 company
= i2c_smbus_read_byte_data(client
, THMC50_REG_COMPANY_ID
);
291 revision
= i2c_smbus_read_byte_data(client
, THMC50_REG_DIE_CODE
);
292 config
= i2c_smbus_read_byte_data(client
, THMC50_REG_CONF
);
298 if (revision
>= 0xc0 && ((config
& 0x10) == 0)) {
299 if (company
== 0x49) {
302 } else if (company
== 0x41) {
308 if (err
== -ENODEV
) {
309 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
314 if (kind
== adm1022
) {
315 int id
= i2c_adapter_id(client
->adapter
);
318 type_name
= "adm1022";
319 data
->has_temp3
= (config
>> 7) & 1; /* config MSB */
320 for (i
= 0; i
+ 1 < adm1022_temp3_num
; i
+= 2)
321 if (adm1022_temp3
[i
] == id
&&
322 adm1022_temp3
[i
+ 1] == address
) {
323 /* enable 2nd remote temp */
328 type_name
= "thmc50";
330 pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
331 type_name
, (revision
>> 4) - 0xc, revision
& 0xf);
333 /* Fill in the remaining client fields & put it into the global list */
334 strlcpy(client
->name
, type_name
, I2C_NAME_SIZE
);
335 mutex_init(&data
->update_lock
);
337 /* Tell the I2C layer a new client has arrived */
338 if ((err
= i2c_attach_client(client
)))
341 thmc50_init_client(client
);
343 /* Register sysfs hooks */
344 if ((err
= sysfs_create_group(&client
->dev
.kobj
, &thmc50_group
)))
347 /* Register ADM1022 sysfs hooks */
349 if ((err
= sysfs_create_group(&client
->dev
.kobj
,
351 goto exit_remove_sysfs_thmc50
;
353 /* Register a new directory entry with module sensors */
354 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
355 if (IS_ERR(data
->hwmon_dev
)) {
356 err
= PTR_ERR(data
->hwmon_dev
);
357 goto exit_remove_sysfs
;
364 sysfs_remove_group(&client
->dev
.kobj
, &temp3_group
);
365 exit_remove_sysfs_thmc50
:
366 sysfs_remove_group(&client
->dev
.kobj
, &thmc50_group
);
368 i2c_detach_client(client
);
375 static int thmc50_attach_adapter(struct i2c_adapter
*adapter
)
377 if (!(adapter
->class & I2C_CLASS_HWMON
))
379 return i2c_probe(adapter
, &addr_data
, thmc50_detect
);
382 static int thmc50_detach_client(struct i2c_client
*client
)
384 struct thmc50_data
*data
= i2c_get_clientdata(client
);
387 hwmon_device_unregister(data
->hwmon_dev
);
388 sysfs_remove_group(&client
->dev
.kobj
, &thmc50_group
);
390 sysfs_remove_group(&client
->dev
.kobj
, &temp3_group
);
392 if ((err
= i2c_detach_client(client
)))
400 static void thmc50_init_client(struct i2c_client
*client
)
402 struct thmc50_data
*data
= i2c_get_clientdata(client
);
405 data
->analog_out
= i2c_smbus_read_byte_data(client
,
406 THMC50_REG_ANALOG_OUT
);
407 /* set up to at least 1 */
408 if (data
->analog_out
== 0) {
409 data
->analog_out
= 1;
410 i2c_smbus_write_byte_data(client
, THMC50_REG_ANALOG_OUT
,
413 config
= i2c_smbus_read_byte_data(client
, THMC50_REG_CONF
);
414 config
|= 0x1; /* start the chip if it is in standby mode */
416 config
|= 0x80; /* enable 2nd remote temp */
417 i2c_smbus_write_byte_data(client
, THMC50_REG_CONF
, config
);
420 static struct thmc50_data
*thmc50_update_device(struct device
*dev
)
422 struct i2c_client
*client
= to_i2c_client(dev
);
423 struct thmc50_data
*data
= i2c_get_clientdata(client
);
424 int timeout
= HZ
/ 5 + (data
->type
== thmc50
? HZ
: 0);
426 mutex_lock(&data
->update_lock
);
428 if (time_after(jiffies
, data
->last_updated
+ timeout
)
431 int temps
= data
->has_temp3
? 3 : 2;
433 for (i
= 0; i
< temps
; i
++) {
434 data
->temp_input
[i
] = i2c_smbus_read_byte_data(client
,
436 data
->temp_max
[i
] = i2c_smbus_read_byte_data(client
,
437 THMC50_REG_TEMP_MAX
[i
]);
438 data
->temp_min
[i
] = i2c_smbus_read_byte_data(client
,
439 THMC50_REG_TEMP_MIN
[i
]);
442 i2c_smbus_read_byte_data(client
, THMC50_REG_ANALOG_OUT
);
444 i2c_smbus_read_byte_data(client
, THMC50_REG_INTR
);
445 data
->last_updated
= jiffies
;
449 mutex_unlock(&data
->update_lock
);
454 static int __init
sm_thmc50_init(void)
456 return i2c_add_driver(&thmc50_driver
);
459 static void __exit
sm_thmc50_exit(void)
461 i2c_del_driver(&thmc50_driver
);
464 MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
465 MODULE_DESCRIPTION("THMC50 driver");
467 module_init(sm_thmc50_init
);
468 module_exit(sm_thmc50_exit
);