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 const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
37 /* Insmod parameters */
38 enum chips { thmc50, adm1022 };
40 static unsigned short adm1022_temp3[16];
41 static unsigned int adm1022_temp3_num;
42 module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0);
43 MODULE_PARM_DESC(adm1022_temp3, "List of adapter,address pairs "
44 "to enable 3rd temperature (ADM1022 only)");
46 /* Many THMC50 constants specified below */
48 /* The THMC50 registers */
49 #define THMC50_REG_CONF 0x40
50 #define THMC50_REG_COMPANY_ID 0x3E
51 #define THMC50_REG_DIE_CODE 0x3F
52 #define THMC50_REG_ANALOG_OUT 0x19
54 * The mirror status register cannot be used as
55 * reading it does not clear alarms.
57 #define THMC50_REG_INTR 0x41
59 static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
60 static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
61 static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
62 static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
63 static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
65 #define THMC50_REG_CONF_nFANOFF 0x20
66 #define THMC50_REG_CONF_PROGRAMMED 0x08
68 /* Each client has this additional data */
70 struct device *hwmon_dev;
72 struct mutex update_lock;
74 unsigned long last_updated; /* In jiffies */
75 char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */
76 char valid; /* !=0 if following fields are valid */
87 static int thmc50_detect(struct i2c_client *client,
88 struct i2c_board_info *info);
89 static int thmc50_probe(struct i2c_client *client,
90 const struct i2c_device_id *id);
91 static int thmc50_remove(struct i2c_client *client);
92 static void thmc50_init_client(struct i2c_client *client);
93 static struct thmc50_data *thmc50_update_device(struct device *dev);
95 static const struct i2c_device_id thmc50_id[] = {
96 { "adm1022", adm1022 },
100 MODULE_DEVICE_TABLE(i2c, thmc50_id);
102 static struct i2c_driver thmc50_driver = {
103 .class = I2C_CLASS_HWMON,
107 .probe = thmc50_probe,
108 .remove = thmc50_remove,
109 .id_table = thmc50_id,
110 .detect = thmc50_detect,
111 .address_list = normal_i2c,
114 static ssize_t show_analog_out(struct device *dev,
115 struct device_attribute *attr, char *buf)
117 struct thmc50_data *data = thmc50_update_device(dev);
118 return sprintf(buf, "%d\n", data->analog_out);
121 static ssize_t set_analog_out(struct device *dev,
122 struct device_attribute *attr,
123 const char *buf, size_t count)
125 struct i2c_client *client = to_i2c_client(dev);
126 struct thmc50_data *data = i2c_get_clientdata(client);
131 err = kstrtoul(buf, 10, &tmp);
135 mutex_lock(&data->update_lock);
136 data->analog_out = SENSORS_LIMIT(tmp, 0, 255);
137 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
140 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
141 if (data->analog_out == 0)
142 config &= ~THMC50_REG_CONF_nFANOFF;
144 config |= THMC50_REG_CONF_nFANOFF;
145 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
147 mutex_unlock(&data->update_lock);
151 /* There is only one PWM mode = DC */
152 static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
155 return sprintf(buf, "0\n");
159 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
162 int nr = to_sensor_dev_attr(attr)->index;
163 struct thmc50_data *data = thmc50_update_device(dev);
164 return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
167 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
170 int nr = to_sensor_dev_attr(attr)->index;
171 struct thmc50_data *data = thmc50_update_device(dev);
172 return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
175 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
176 const char *buf, size_t count)
178 int nr = to_sensor_dev_attr(attr)->index;
179 struct i2c_client *client = to_i2c_client(dev);
180 struct thmc50_data *data = i2c_get_clientdata(client);
184 err = kstrtol(buf, 10, &val);
188 mutex_lock(&data->update_lock);
189 data->temp_min[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
190 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
192 mutex_unlock(&data->update_lock);
196 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
199 int nr = to_sensor_dev_attr(attr)->index;
200 struct thmc50_data *data = thmc50_update_device(dev);
201 return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
204 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
205 const char *buf, size_t count)
207 int nr = to_sensor_dev_attr(attr)->index;
208 struct i2c_client *client = to_i2c_client(dev);
209 struct thmc50_data *data = i2c_get_clientdata(client);
213 err = kstrtol(buf, 10, &val);
217 mutex_lock(&data->update_lock);
218 data->temp_max[nr] = SENSORS_LIMIT(val / 1000, -128, 127);
219 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
221 mutex_unlock(&data->update_lock);
225 static ssize_t show_temp_critical(struct device *dev,
226 struct device_attribute *attr,
229 int nr = to_sensor_dev_attr(attr)->index;
230 struct thmc50_data *data = thmc50_update_device(dev);
231 return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000);
234 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
237 int index = to_sensor_dev_attr(attr)->index;
238 struct thmc50_data *data = thmc50_update_device(dev);
240 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
243 #define temp_reg(offset) \
244 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
246 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
247 show_temp_min, set_temp_min, offset - 1); \
248 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
249 show_temp_max, set_temp_max, offset - 1); \
250 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO, \
251 show_temp_critical, NULL, offset - 1);
257 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0);
258 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
259 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
260 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 7);
261 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
263 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_analog_out,
265 static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
267 static struct attribute *thmc50_attributes[] = {
268 &sensor_dev_attr_temp1_max.dev_attr.attr,
269 &sensor_dev_attr_temp1_min.dev_attr.attr,
270 &sensor_dev_attr_temp1_input.dev_attr.attr,
271 &sensor_dev_attr_temp1_crit.dev_attr.attr,
272 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
273 &sensor_dev_attr_temp2_max.dev_attr.attr,
274 &sensor_dev_attr_temp2_min.dev_attr.attr,
275 &sensor_dev_attr_temp2_input.dev_attr.attr,
276 &sensor_dev_attr_temp2_crit.dev_attr.attr,
277 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
278 &sensor_dev_attr_temp2_fault.dev_attr.attr,
279 &sensor_dev_attr_pwm1.dev_attr.attr,
280 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
284 static const struct attribute_group thmc50_group = {
285 .attrs = thmc50_attributes,
288 /* for ADM1022 3rd temperature mode */
289 static struct attribute *temp3_attributes[] = {
290 &sensor_dev_attr_temp3_max.dev_attr.attr,
291 &sensor_dev_attr_temp3_min.dev_attr.attr,
292 &sensor_dev_attr_temp3_input.dev_attr.attr,
293 &sensor_dev_attr_temp3_crit.dev_attr.attr,
294 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
295 &sensor_dev_attr_temp3_fault.dev_attr.attr,
299 static const struct attribute_group temp3_group = {
300 .attrs = temp3_attributes,
303 /* Return 0 if detection is successful, -ENODEV otherwise */
304 static int thmc50_detect(struct i2c_client *client,
305 struct i2c_board_info *info)
310 struct i2c_adapter *adapter = client->adapter;
311 const char *type_name;
313 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
314 pr_debug("thmc50: detect failed, "
315 "smbus byte data not supported!\n");
319 pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
320 client->addr, i2c_adapter_id(client->adapter));
322 company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
323 revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
324 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
325 if (revision < 0xc0 || (config & 0x10))
328 if (company == 0x41) {
329 int id = i2c_adapter_id(client->adapter);
332 type_name = "adm1022";
333 for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
334 if (adm1022_temp3[i] == id &&
335 adm1022_temp3[i + 1] == client->addr) {
336 /* enable 2nd remote temp */
338 i2c_smbus_write_byte_data(client,
343 } else if (company == 0x49) {
344 type_name = "thmc50";
346 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
350 pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
351 type_name, (revision >> 4) - 0xc, revision & 0xf);
353 strlcpy(info->type, type_name, I2C_NAME_SIZE);
358 static int thmc50_probe(struct i2c_client *client,
359 const struct i2c_device_id *id)
361 struct thmc50_data *data;
364 data = devm_kzalloc(&client->dev, sizeof(struct thmc50_data),
369 i2c_set_clientdata(client, data);
370 data->type = id->driver_data;
371 mutex_init(&data->update_lock);
373 thmc50_init_client(client);
375 /* Register sysfs hooks */
376 err = sysfs_create_group(&client->dev.kobj, &thmc50_group);
380 /* Register ADM1022 sysfs hooks */
381 if (data->has_temp3) {
382 err = sysfs_create_group(&client->dev.kobj, &temp3_group);
384 goto exit_remove_sysfs_thmc50;
387 /* Register a new directory entry with module sensors */
388 data->hwmon_dev = hwmon_device_register(&client->dev);
389 if (IS_ERR(data->hwmon_dev)) {
390 err = PTR_ERR(data->hwmon_dev);
391 goto exit_remove_sysfs;
398 sysfs_remove_group(&client->dev.kobj, &temp3_group);
399 exit_remove_sysfs_thmc50:
400 sysfs_remove_group(&client->dev.kobj, &thmc50_group);
404 static int thmc50_remove(struct i2c_client *client)
406 struct thmc50_data *data = i2c_get_clientdata(client);
408 hwmon_device_unregister(data->hwmon_dev);
409 sysfs_remove_group(&client->dev.kobj, &thmc50_group);
411 sysfs_remove_group(&client->dev.kobj, &temp3_group);
416 static void thmc50_init_client(struct i2c_client *client)
418 struct thmc50_data *data = i2c_get_clientdata(client);
421 data->analog_out = i2c_smbus_read_byte_data(client,
422 THMC50_REG_ANALOG_OUT);
423 /* set up to at least 1 */
424 if (data->analog_out == 0) {
425 data->analog_out = 1;
426 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
429 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
430 config |= 0x1; /* start the chip if it is in standby mode */
431 if (data->type == adm1022 && (config & (1 << 7)))
433 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
436 static struct thmc50_data *thmc50_update_device(struct device *dev)
438 struct i2c_client *client = to_i2c_client(dev);
439 struct thmc50_data *data = i2c_get_clientdata(client);
440 int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
442 mutex_lock(&data->update_lock);
444 if (time_after(jiffies, data->last_updated + timeout)
447 int temps = data->has_temp3 ? 3 : 2;
449 int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
451 prog &= THMC50_REG_CONF_PROGRAMMED;
453 for (i = 0; i < temps; i++) {
454 data->temp_input[i] = i2c_smbus_read_byte_data(client,
456 data->temp_max[i] = i2c_smbus_read_byte_data(client,
457 THMC50_REG_TEMP_MAX[i]);
458 data->temp_min[i] = i2c_smbus_read_byte_data(client,
459 THMC50_REG_TEMP_MIN[i]);
460 data->temp_critical[i] =
461 i2c_smbus_read_byte_data(client,
462 prog ? THMC50_REG_TEMP_CRITICAL[i]
463 : THMC50_REG_TEMP_DEFAULT[i]);
466 i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
468 i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
469 data->last_updated = jiffies;
473 mutex_unlock(&data->update_lock);
478 module_i2c_driver(thmc50_driver);
480 MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
481 MODULE_DESCRIPTION("THMC50 driver");