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>
31 #include <linux/jiffies.h>
33 MODULE_LICENSE("GPL");
35 /* Addresses to scan */
36 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
38 /* Insmod parameters */
39 enum chips { thmc50, adm1022 };
41 static unsigned short adm1022_temp3[16];
42 static unsigned int adm1022_temp3_num;
43 module_param_array(adm1022_temp3, ushort, &adm1022_temp3_num, 0);
44 MODULE_PARM_DESC(adm1022_temp3,
45 "List of adapter,address pairs to enable 3rd temperature (ADM1022 only)");
47 /* Many THMC50 constants specified below */
49 /* The THMC50 registers */
50 #define THMC50_REG_CONF 0x40
51 #define THMC50_REG_COMPANY_ID 0x3E
52 #define THMC50_REG_DIE_CODE 0x3F
53 #define THMC50_REG_ANALOG_OUT 0x19
55 * The mirror status register cannot be used as
56 * reading it does not clear alarms.
58 #define THMC50_REG_INTR 0x41
60 static const u8 THMC50_REG_TEMP[] = { 0x27, 0x26, 0x20 };
61 static const u8 THMC50_REG_TEMP_MIN[] = { 0x3A, 0x38, 0x2C };
62 static const u8 THMC50_REG_TEMP_MAX[] = { 0x39, 0x37, 0x2B };
63 static const u8 THMC50_REG_TEMP_CRITICAL[] = { 0x13, 0x14, 0x14 };
64 static const u8 THMC50_REG_TEMP_DEFAULT[] = { 0x17, 0x18, 0x18 };
66 #define THMC50_REG_CONF_nFANOFF 0x20
67 #define THMC50_REG_CONF_PROGRAMMED 0x08
69 /* Each client has this additional data */
71 struct i2c_client *client;
72 const struct attribute_group *groups[3];
74 struct mutex update_lock;
76 unsigned long last_updated; /* In jiffies */
77 char has_temp3; /* !=0 if it is ADM1022 in temp3 mode */
78 char valid; /* !=0 if following fields are valid */
89 static struct thmc50_data *thmc50_update_device(struct device *dev)
91 struct thmc50_data *data = dev_get_drvdata(dev);
92 struct i2c_client *client = data->client;
93 int timeout = HZ / 5 + (data->type == thmc50 ? HZ : 0);
95 mutex_lock(&data->update_lock);
97 if (time_after(jiffies, data->last_updated + timeout)
100 int temps = data->has_temp3 ? 3 : 2;
102 int prog = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
104 prog &= THMC50_REG_CONF_PROGRAMMED;
106 for (i = 0; i < temps; i++) {
107 data->temp_input[i] = i2c_smbus_read_byte_data(client,
109 data->temp_max[i] = i2c_smbus_read_byte_data(client,
110 THMC50_REG_TEMP_MAX[i]);
111 data->temp_min[i] = i2c_smbus_read_byte_data(client,
112 THMC50_REG_TEMP_MIN[i]);
113 data->temp_critical[i] =
114 i2c_smbus_read_byte_data(client,
115 prog ? THMC50_REG_TEMP_CRITICAL[i]
116 : THMC50_REG_TEMP_DEFAULT[i]);
119 i2c_smbus_read_byte_data(client, THMC50_REG_ANALOG_OUT);
121 i2c_smbus_read_byte_data(client, THMC50_REG_INTR);
122 data->last_updated = jiffies;
126 mutex_unlock(&data->update_lock);
131 static ssize_t analog_out_show(struct device *dev,
132 struct device_attribute *attr, char *buf)
134 struct thmc50_data *data = thmc50_update_device(dev);
135 return sprintf(buf, "%d\n", data->analog_out);
138 static ssize_t analog_out_store(struct device *dev,
139 struct device_attribute *attr,
140 const char *buf, size_t count)
142 struct thmc50_data *data = dev_get_drvdata(dev);
143 struct i2c_client *client = data->client;
148 err = kstrtoul(buf, 10, &tmp);
152 mutex_lock(&data->update_lock);
153 data->analog_out = clamp_val(tmp, 0, 255);
154 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
157 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
158 if (data->analog_out == 0)
159 config &= ~THMC50_REG_CONF_nFANOFF;
161 config |= THMC50_REG_CONF_nFANOFF;
162 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
164 mutex_unlock(&data->update_lock);
168 /* There is only one PWM mode = DC */
169 static ssize_t pwm_mode_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 return sprintf(buf, "0\n");
176 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
179 int nr = to_sensor_dev_attr(attr)->index;
180 struct thmc50_data *data = thmc50_update_device(dev);
181 return sprintf(buf, "%d\n", data->temp_input[nr] * 1000);
184 static ssize_t temp_min_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
187 int nr = to_sensor_dev_attr(attr)->index;
188 struct thmc50_data *data = thmc50_update_device(dev);
189 return sprintf(buf, "%d\n", data->temp_min[nr] * 1000);
192 static ssize_t temp_min_store(struct device *dev,
193 struct device_attribute *attr, const char *buf,
196 int nr = to_sensor_dev_attr(attr)->index;
197 struct thmc50_data *data = dev_get_drvdata(dev);
198 struct i2c_client *client = data->client;
202 err = kstrtol(buf, 10, &val);
206 mutex_lock(&data->update_lock);
207 data->temp_min[nr] = clamp_val(val / 1000, -128, 127);
208 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MIN[nr],
210 mutex_unlock(&data->update_lock);
214 static ssize_t temp_max_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 int nr = to_sensor_dev_attr(attr)->index;
218 struct thmc50_data *data = thmc50_update_device(dev);
219 return sprintf(buf, "%d\n", data->temp_max[nr] * 1000);
222 static ssize_t temp_max_store(struct device *dev,
223 struct device_attribute *attr, const char *buf,
226 int nr = to_sensor_dev_attr(attr)->index;
227 struct thmc50_data *data = dev_get_drvdata(dev);
228 struct i2c_client *client = data->client;
232 err = kstrtol(buf, 10, &val);
236 mutex_lock(&data->update_lock);
237 data->temp_max[nr] = clamp_val(val / 1000, -128, 127);
238 i2c_smbus_write_byte_data(client, THMC50_REG_TEMP_MAX[nr],
240 mutex_unlock(&data->update_lock);
244 static ssize_t temp_critical_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 int nr = to_sensor_dev_attr(attr)->index;
248 struct thmc50_data *data = thmc50_update_device(dev);
249 return sprintf(buf, "%d\n", data->temp_critical[nr] * 1000);
252 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
255 int index = to_sensor_dev_attr(attr)->index;
256 struct thmc50_data *data = thmc50_update_device(dev);
258 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
261 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
262 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
263 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
264 static SENSOR_DEVICE_ATTR_RO(temp1_crit, temp_critical, 0);
265 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
266 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
267 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
268 static SENSOR_DEVICE_ATTR_RO(temp2_crit, temp_critical, 1);
269 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
270 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
271 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
272 static SENSOR_DEVICE_ATTR_RO(temp3_crit, temp_critical, 2);
274 static SENSOR_DEVICE_ATTR_RO(temp1_alarm, alarm, 0);
275 static SENSOR_DEVICE_ATTR_RO(temp2_alarm, alarm, 5);
276 static SENSOR_DEVICE_ATTR_RO(temp3_alarm, alarm, 1);
277 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 7);
278 static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2);
280 static SENSOR_DEVICE_ATTR_RW(pwm1, analog_out, 0);
281 static SENSOR_DEVICE_ATTR_RO(pwm1_mode, pwm_mode, 0);
283 static struct attribute *thmc50_attributes[] = {
284 &sensor_dev_attr_temp1_max.dev_attr.attr,
285 &sensor_dev_attr_temp1_min.dev_attr.attr,
286 &sensor_dev_attr_temp1_input.dev_attr.attr,
287 &sensor_dev_attr_temp1_crit.dev_attr.attr,
288 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
289 &sensor_dev_attr_temp2_max.dev_attr.attr,
290 &sensor_dev_attr_temp2_min.dev_attr.attr,
291 &sensor_dev_attr_temp2_input.dev_attr.attr,
292 &sensor_dev_attr_temp2_crit.dev_attr.attr,
293 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
294 &sensor_dev_attr_temp2_fault.dev_attr.attr,
295 &sensor_dev_attr_pwm1.dev_attr.attr,
296 &sensor_dev_attr_pwm1_mode.dev_attr.attr,
300 static const struct attribute_group thmc50_group = {
301 .attrs = thmc50_attributes,
304 /* for ADM1022 3rd temperature mode */
305 static struct attribute *temp3_attributes[] = {
306 &sensor_dev_attr_temp3_max.dev_attr.attr,
307 &sensor_dev_attr_temp3_min.dev_attr.attr,
308 &sensor_dev_attr_temp3_input.dev_attr.attr,
309 &sensor_dev_attr_temp3_crit.dev_attr.attr,
310 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
311 &sensor_dev_attr_temp3_fault.dev_attr.attr,
315 static const struct attribute_group temp3_group = {
316 .attrs = temp3_attributes,
319 /* Return 0 if detection is successful, -ENODEV otherwise */
320 static int thmc50_detect(struct i2c_client *client,
321 struct i2c_board_info *info)
326 struct i2c_adapter *adapter = client->adapter;
327 const char *type_name;
329 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
330 pr_debug("thmc50: detect failed, smbus byte data not supported!\n");
334 pr_debug("thmc50: Probing for THMC50 at 0x%2X on bus %d\n",
335 client->addr, i2c_adapter_id(client->adapter));
337 company = i2c_smbus_read_byte_data(client, THMC50_REG_COMPANY_ID);
338 revision = i2c_smbus_read_byte_data(client, THMC50_REG_DIE_CODE);
339 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
340 if (revision < 0xc0 || (config & 0x10))
343 if (company == 0x41) {
344 int id = i2c_adapter_id(client->adapter);
347 type_name = "adm1022";
348 for (i = 0; i + 1 < adm1022_temp3_num; i += 2)
349 if (adm1022_temp3[i] == id &&
350 adm1022_temp3[i + 1] == client->addr) {
351 /* enable 2nd remote temp */
353 i2c_smbus_write_byte_data(client,
358 } else if (company == 0x49) {
359 type_name = "thmc50";
361 pr_debug("thmc50: Detection of THMC50/ADM1022 failed\n");
365 pr_debug("thmc50: Detected %s (version %x, revision %x)\n",
366 type_name, (revision >> 4) - 0xc, revision & 0xf);
368 strlcpy(info->type, type_name, I2C_NAME_SIZE);
373 static void thmc50_init_client(struct thmc50_data *data)
375 struct i2c_client *client = data->client;
378 data->analog_out = i2c_smbus_read_byte_data(client,
379 THMC50_REG_ANALOG_OUT);
380 /* set up to at least 1 */
381 if (data->analog_out == 0) {
382 data->analog_out = 1;
383 i2c_smbus_write_byte_data(client, THMC50_REG_ANALOG_OUT,
386 config = i2c_smbus_read_byte_data(client, THMC50_REG_CONF);
387 config |= 0x1; /* start the chip if it is in standby mode */
388 if (data->type == adm1022 && (config & (1 << 7)))
390 i2c_smbus_write_byte_data(client, THMC50_REG_CONF, config);
393 static int thmc50_probe(struct i2c_client *client,
394 const struct i2c_device_id *id)
396 struct device *dev = &client->dev;
397 struct thmc50_data *data;
398 struct device *hwmon_dev;
401 data = devm_kzalloc(dev, sizeof(struct thmc50_data), GFP_KERNEL);
405 data->client = client;
406 data->type = id->driver_data;
407 mutex_init(&data->update_lock);
409 thmc50_init_client(data);
412 data->groups[idx++] = &thmc50_group;
414 /* Register additional ADM1022 sysfs hooks */
416 data->groups[idx++] = &temp3_group;
418 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
420 return PTR_ERR_OR_ZERO(hwmon_dev);
423 static const struct i2c_device_id thmc50_id[] = {
424 { "adm1022", adm1022 },
425 { "thmc50", thmc50 },
428 MODULE_DEVICE_TABLE(i2c, thmc50_id);
430 static struct i2c_driver thmc50_driver = {
431 .class = I2C_CLASS_HWMON,
435 .probe = thmc50_probe,
436 .id_table = thmc50_id,
437 .detect = thmc50_detect,
438 .address_list = normal_i2c,
441 module_i2c_driver(thmc50_driver);
443 MODULE_AUTHOR("Krzysztof Helt <krzysztof.h1@wp.pl>");
444 MODULE_DESCRIPTION("THMC50 driver");