Blame view
drivers/hwmon/lm92.c
13.1 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 |
/* * lm92 - Hardware monitoring driver |
910e8dcf1 hwmon: (lm92) Con... |
3 |
* Copyright (C) 2005-2008 Jean Delvare <khali@linux-fr.org> |
1da177e4c Linux-2.6.12-rc2 |
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
* * Based on the lm90 driver, with some ideas taken from the lm_sensors * lm92 driver as well. * * The LM92 is a sensor chip made by National Semiconductor. It reports * its own temperature with a 0.0625 deg resolution and a 0.33 deg * accuracy. Complete datasheet can be obtained from National's website * at: * http://www.national.com/pf/LM/LM92.html * * This driver also supports the MAX6635 sensor chip made by Maxim. * This chip is compatible with the LM92, but has a lesser accuracy * (1.0 deg). Complete datasheet can be obtained from Maxim's website * at: * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3074 * * Since the LM92 was the first chipset supported by this driver, most * comments will refer to this chipset, but are actually general and * concern all supported chipsets, unless mentioned otherwise. * * Support could easily be added for the National Semiconductor LM76 * and Maxim MAX6633 and MAX6634 chips, which are mostly compatible * with the LM92. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <linux/module.h> #include <linux/init.h> #include <linux/slab.h> #include <linux/i2c.h> |
943b0830c [PATCH] I2C hwmon... |
47 |
#include <linux/hwmon.h> |
6cb59e915 hwmon: (lm92) Add... |
48 |
#include <linux/hwmon-sysfs.h> |
943b0830c [PATCH] I2C hwmon... |
49 |
#include <linux/err.h> |
9a61bf630 [PATCH] hwmon: Se... |
50 |
#include <linux/mutex.h> |
1da177e4c Linux-2.6.12-rc2 |
51 52 53 |
/* The LM92 and MAX6635 have 2 two-state pins for address selection, resulting in 4 possible addresses. */ |
25e9c86d5 hwmon: normal_i2c... |
54 55 |
static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, I2C_CLIENT_END }; |
1da177e4c Linux-2.6.12-rc2 |
56 |
|
1da177e4c Linux-2.6.12-rc2 |
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
/* The LM92 registers */ #define LM92_REG_CONFIG 0x01 /* 8-bit, RW */ #define LM92_REG_TEMP 0x00 /* 16-bit, RO */ #define LM92_REG_TEMP_HYST 0x02 /* 16-bit, RW */ #define LM92_REG_TEMP_CRIT 0x03 /* 16-bit, RW */ #define LM92_REG_TEMP_LOW 0x04 /* 16-bit, RW */ #define LM92_REG_TEMP_HIGH 0x05 /* 16-bit, RW */ #define LM92_REG_MAN_ID 0x07 /* 16-bit, RO, LM92 only */ /* The LM92 uses signed 13-bit values with LSB = 0.0625 degree Celsius, left-justified in 16-bit registers. No rounding is done, with such a resolution it's just not worth it. Note that the MAX6635 doesn't make use of the 4 lower bits for limits (i.e. effective resolution for limits is 1 degree Celsius). */ static inline int TEMP_FROM_REG(s16 reg) { return reg / 8 * 625 / 10; } static inline s16 TEMP_TO_REG(int val) { if (val <= -60000) return -60000 * 10 / 625 * 8; if (val >= 160000) return 160000 * 10 / 625 * 8; return val * 10 / 625 * 8; } /* Alarm flags are stored in the 3 LSB of the temperature register */ static inline u8 ALARMS_FROM_REG(s16 reg) { return reg & 0x0007; } /* Driver data (common to all clients) */ static struct i2c_driver lm92_driver; /* Client data (each client gets its own) */ struct lm92_data { |
1beeffe43 hwmon: Convert fr... |
96 |
struct device *hwmon_dev; |
9a61bf630 [PATCH] hwmon: Se... |
97 |
struct mutex update_lock; |
1da177e4c Linux-2.6.12-rc2 |
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
char valid; /* zero until following fields are valid */ unsigned long last_updated; /* in jiffies */ /* registers values */ s16 temp1_input, temp1_crit, temp1_min, temp1_max, temp1_hyst; }; /* * Sysfs attributes and callback functions */ static struct lm92_data *lm92_update_device(struct device *dev) { struct i2c_client *client = to_i2c_client(dev); struct lm92_data *data = i2c_get_clientdata(client); |
9a61bf630 [PATCH] hwmon: Se... |
114 |
mutex_lock(&data->update_lock); |
1da177e4c Linux-2.6.12-rc2 |
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { dev_dbg(&client->dev, "Updating lm92 data "); data->temp1_input = swab16(i2c_smbus_read_word_data(client, LM92_REG_TEMP)); data->temp1_hyst = swab16(i2c_smbus_read_word_data(client, LM92_REG_TEMP_HYST)); data->temp1_crit = swab16(i2c_smbus_read_word_data(client, LM92_REG_TEMP_CRIT)); data->temp1_min = swab16(i2c_smbus_read_word_data(client, LM92_REG_TEMP_LOW)); data->temp1_max = swab16(i2c_smbus_read_word_data(client, LM92_REG_TEMP_HIGH)); data->last_updated = jiffies; data->valid = 1; } |
9a61bf630 [PATCH] hwmon: Se... |
134 |
mutex_unlock(&data->update_lock); |
1da177e4c Linux-2.6.12-rc2 |
135 136 137 138 139 |
return data; } #define show_temp(value) \ |
8627f9ba5 [PATCH] Driver Co... |
140 |
static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \ |
1da177e4c Linux-2.6.12-rc2 |
141 142 143 144 145 146 147 148 149 150 151 |
{ \ struct lm92_data *data = lm92_update_device(dev); \ return sprintf(buf, "%d ", TEMP_FROM_REG(data->value)); \ } show_temp(temp1_input); show_temp(temp1_crit); show_temp(temp1_min); show_temp(temp1_max); #define set_temp(value, reg) \ |
8627f9ba5 [PATCH] Driver Co... |
152 |
static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, \ |
1da177e4c Linux-2.6.12-rc2 |
153 154 155 156 157 158 |
size_t count) \ { \ struct i2c_client *client = to_i2c_client(dev); \ struct lm92_data *data = i2c_get_clientdata(client); \ long val = simple_strtol(buf, NULL, 10); \ \ |
9a61bf630 [PATCH] hwmon: Se... |
159 |
mutex_lock(&data->update_lock); \ |
1da177e4c Linux-2.6.12-rc2 |
160 161 |
data->value = TEMP_TO_REG(val); \ i2c_smbus_write_word_data(client, reg, swab16(data->value)); \ |
9a61bf630 [PATCH] hwmon: Se... |
162 |
mutex_unlock(&data->update_lock); \ |
1da177e4c Linux-2.6.12-rc2 |
163 164 165 166 167 |
return count; \ } set_temp(temp1_crit, LM92_REG_TEMP_CRIT); set_temp(temp1_min, LM92_REG_TEMP_LOW); set_temp(temp1_max, LM92_REG_TEMP_HIGH); |
8627f9ba5 [PATCH] Driver Co... |
168 |
static ssize_t show_temp1_crit_hyst(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4c Linux-2.6.12-rc2 |
169 170 171 172 173 174 |
{ struct lm92_data *data = lm92_update_device(dev); return sprintf(buf, "%d ", TEMP_FROM_REG(data->temp1_crit) - TEMP_FROM_REG(data->temp1_hyst)); } |
8627f9ba5 [PATCH] Driver Co... |
175 |
static ssize_t show_temp1_max_hyst(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4c Linux-2.6.12-rc2 |
176 177 178 179 180 181 |
{ struct lm92_data *data = lm92_update_device(dev); return sprintf(buf, "%d ", TEMP_FROM_REG(data->temp1_max) - TEMP_FROM_REG(data->temp1_hyst)); } |
8627f9ba5 [PATCH] Driver Co... |
182 |
static ssize_t show_temp1_min_hyst(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4c Linux-2.6.12-rc2 |
183 184 185 186 187 188 |
{ struct lm92_data *data = lm92_update_device(dev); return sprintf(buf, "%d ", TEMP_FROM_REG(data->temp1_min) + TEMP_FROM_REG(data->temp1_hyst)); } |
8627f9ba5 [PATCH] Driver Co... |
189 |
static ssize_t set_temp1_crit_hyst(struct device *dev, struct device_attribute *attr, const char *buf, |
1da177e4c Linux-2.6.12-rc2 |
190 191 192 193 194 |
size_t count) { struct i2c_client *client = to_i2c_client(dev); struct lm92_data *data = i2c_get_clientdata(client); long val = simple_strtol(buf, NULL, 10); |
9a61bf630 [PATCH] hwmon: Se... |
195 |
mutex_lock(&data->update_lock); |
1da177e4c Linux-2.6.12-rc2 |
196 197 198 |
data->temp1_hyst = TEMP_FROM_REG(data->temp1_crit) - val; i2c_smbus_write_word_data(client, LM92_REG_TEMP_HYST, swab16(TEMP_TO_REG(data->temp1_hyst))); |
9a61bf630 [PATCH] hwmon: Se... |
199 |
mutex_unlock(&data->update_lock); |
1da177e4c Linux-2.6.12-rc2 |
200 201 |
return count; } |
8627f9ba5 [PATCH] Driver Co... |
202 |
static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4c Linux-2.6.12-rc2 |
203 204 205 206 207 |
{ struct lm92_data *data = lm92_update_device(dev); return sprintf(buf, "%d ", ALARMS_FROM_REG(data->temp1_input)); } |
6cb59e915 hwmon: (lm92) Add... |
208 209 210 211 212 213 214 215 |
static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, char *buf) { int bitnr = to_sensor_dev_attr(attr)->index; struct lm92_data *data = lm92_update_device(dev); return sprintf(buf, "%d ", (data->temp1_input >> bitnr) & 1); } |
1da177e4c Linux-2.6.12-rc2 |
216 217 218 219 220 221 222 223 224 225 226 227 |
static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1_input, NULL); static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp1_crit, set_temp1_crit); static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp1_crit_hyst, set_temp1_crit_hyst); static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp1_min, set_temp1_min); static DEVICE_ATTR(temp1_min_hyst, S_IRUGO, show_temp1_min_hyst, NULL); static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp1_max, set_temp1_max); static DEVICE_ATTR(temp1_max_hyst, S_IRUGO, show_temp1_max_hyst, NULL); static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
6cb59e915 hwmon: (lm92) Add... |
228 229 230 |
static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2); static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0); static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1); |
1da177e4c Linux-2.6.12-rc2 |
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
/* * Detection and registration */ static void lm92_init_client(struct i2c_client *client) { u8 config; /* Start the conversions if needed */ config = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG); if (config & 0x01) i2c_smbus_write_byte_data(client, LM92_REG_CONFIG, config & 0xFE); } /* The MAX6635 has no identification register, so we have to use tricks to identify it reliably. This is somewhat slow. Note that we do NOT rely on the 2 MSB of the configuration register always reading 0, as suggested by the datasheet, because it was once reported not to be true. */ static int max6635_check(struct i2c_client *client) { u16 temp_low, temp_high, temp_hyst, temp_crit; u8 conf; int i; /* No manufacturer ID register, so a read from this address will always return the last read value. */ temp_low = i2c_smbus_read_word_data(client, LM92_REG_TEMP_LOW); if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_low) return 0; temp_high = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HIGH); if (i2c_smbus_read_word_data(client, LM92_REG_MAN_ID) != temp_high) return 0; /* Limits are stored as integer values (signed, 9-bit). */ if ((temp_low & 0x7f00) || (temp_high & 0x7f00)) return 0; temp_hyst = i2c_smbus_read_word_data(client, LM92_REG_TEMP_HYST); temp_crit = i2c_smbus_read_word_data(client, LM92_REG_TEMP_CRIT); if ((temp_hyst & 0x7f00) || (temp_crit & 0x7f00)) return 0; /* Registers addresses were found to cycle over 16-byte boundaries. We don't test all registers with all offsets so as to save some reads and time, but this should still be sufficient to dismiss non-MAX6635 chips. */ conf = i2c_smbus_read_byte_data(client, LM92_REG_CONFIG); for (i=16; i<96; i*=2) { if (temp_hyst != i2c_smbus_read_word_data(client, LM92_REG_TEMP_HYST + i - 16) || temp_crit != i2c_smbus_read_word_data(client, LM92_REG_TEMP_CRIT + i) || temp_low != i2c_smbus_read_word_data(client, LM92_REG_TEMP_LOW + i + 16) || temp_high != i2c_smbus_read_word_data(client, LM92_REG_TEMP_HIGH + i + 32) || conf != i2c_smbus_read_byte_data(client, LM92_REG_CONFIG + i)) return 0; } return 1; } |
0501a3816 hwmon: Fix unchec... |
297 298 299 300 301 302 303 304 305 |
static struct attribute *lm92_attributes[] = { &dev_attr_temp1_input.attr, &dev_attr_temp1_crit.attr, &dev_attr_temp1_crit_hyst.attr, &dev_attr_temp1_min.attr, &dev_attr_temp1_min_hyst.attr, &dev_attr_temp1_max.attr, &dev_attr_temp1_max_hyst.attr, &dev_attr_alarms.attr, |
6cb59e915 hwmon: (lm92) Add... |
306 307 308 |
&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
0501a3816 hwmon: Fix unchec... |
309 310 311 312 313 314 |
NULL }; static const struct attribute_group lm92_group = { .attrs = lm92_attributes, }; |
910e8dcf1 hwmon: (lm92) Con... |
315 |
/* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec7921 i2c: Drop the kin... |
316 |
static int lm92_detect(struct i2c_client *new_client, |
910e8dcf1 hwmon: (lm92) Con... |
317 |
struct i2c_board_info *info) |
1da177e4c Linux-2.6.12-rc2 |
318 |
{ |
910e8dcf1 hwmon: (lm92) Con... |
319 |
struct i2c_adapter *adapter = new_client->adapter; |
52df6440a hwmon: Clean up d... |
320 321 |
u8 config; u16 man_id; |
1da177e4c Linux-2.6.12-rc2 |
322 323 324 |
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) |
910e8dcf1 hwmon: (lm92) Con... |
325 |
return -ENODEV; |
1da177e4c Linux-2.6.12-rc2 |
326 |
|
52df6440a hwmon: Clean up d... |
327 328 329 330 331 332 333 334 335 336 337 |
config = i2c_smbus_read_byte_data(new_client, LM92_REG_CONFIG); man_id = i2c_smbus_read_word_data(new_client, LM92_REG_MAN_ID); if ((config & 0xe0) == 0x00 && man_id == 0x0180) pr_info("lm92: Found National Semiconductor LM92 chip "); else if (max6635_check(new_client)) pr_info("lm92: Found Maxim MAX6635 chip "); else return -ENODEV; |
1da177e4c Linux-2.6.12-rc2 |
338 |
|
910e8dcf1 hwmon: (lm92) Con... |
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
strlcpy(info->type, "lm92", I2C_NAME_SIZE); return 0; } static int lm92_probe(struct i2c_client *new_client, const struct i2c_device_id *id) { struct lm92_data *data; int err; data = kzalloc(sizeof(struct lm92_data), GFP_KERNEL); if (!data) { err = -ENOMEM; goto exit; } i2c_set_clientdata(new_client, data); |
1da177e4c Linux-2.6.12-rc2 |
357 |
data->valid = 0; |
9a61bf630 [PATCH] hwmon: Se... |
358 |
mutex_init(&data->update_lock); |
1da177e4c Linux-2.6.12-rc2 |
359 |
|
1da177e4c Linux-2.6.12-rc2 |
360 361 362 363 |
/* Initialize the chipset */ lm92_init_client(new_client); /* Register sysfs hooks */ |
0501a3816 hwmon: Fix unchec... |
364 |
if ((err = sysfs_create_group(&new_client->dev.kobj, &lm92_group))) |
910e8dcf1 hwmon: (lm92) Con... |
365 |
goto exit_free; |
0501a3816 hwmon: Fix unchec... |
366 |
|
1beeffe43 hwmon: Convert fr... |
367 368 369 |
data->hwmon_dev = hwmon_device_register(&new_client->dev); if (IS_ERR(data->hwmon_dev)) { err = PTR_ERR(data->hwmon_dev); |
0501a3816 hwmon: Fix unchec... |
370 |
goto exit_remove; |
943b0830c [PATCH] I2C hwmon... |
371 |
} |
1da177e4c Linux-2.6.12-rc2 |
372 |
return 0; |
0501a3816 hwmon: Fix unchec... |
373 374 |
exit_remove: sysfs_remove_group(&new_client->dev.kobj, &lm92_group); |
1da177e4c Linux-2.6.12-rc2 |
375 376 377 378 379 |
exit_free: kfree(data); exit: return err; } |
910e8dcf1 hwmon: (lm92) Con... |
380 |
static int lm92_remove(struct i2c_client *client) |
1da177e4c Linux-2.6.12-rc2 |
381 |
{ |
943b0830c [PATCH] I2C hwmon... |
382 |
struct lm92_data *data = i2c_get_clientdata(client); |
1da177e4c Linux-2.6.12-rc2 |
383 |
|
1beeffe43 hwmon: Convert fr... |
384 |
hwmon_device_unregister(data->hwmon_dev); |
0501a3816 hwmon: Fix unchec... |
385 |
sysfs_remove_group(&client->dev.kobj, &lm92_group); |
943b0830c [PATCH] I2C hwmon... |
386 |
|
943b0830c [PATCH] I2C hwmon... |
387 |
kfree(data); |
1da177e4c Linux-2.6.12-rc2 |
388 389 390 391 392 393 394 |
return 0; } /* * Module and driver stuff */ |
910e8dcf1 hwmon: (lm92) Con... |
395 |
static const struct i2c_device_id lm92_id[] = { |
1f86df49d i2c: Drop I2C_CLI... |
396 |
{ "lm92", 0 }, |
910e8dcf1 hwmon: (lm92) Con... |
397 398 399 400 |
/* max6635 could be added here */ { } }; MODULE_DEVICE_TABLE(i2c, lm92_id); |
1da177e4c Linux-2.6.12-rc2 |
401 |
static struct i2c_driver lm92_driver = { |
910e8dcf1 hwmon: (lm92) Con... |
402 |
.class = I2C_CLASS_HWMON, |
cdaf79349 [PATCH] i2c: Drop... |
403 |
.driver = { |
cdaf79349 [PATCH] i2c: Drop... |
404 405 |
.name = "lm92", }, |
910e8dcf1 hwmon: (lm92) Con... |
406 407 408 409 |
.probe = lm92_probe, .remove = lm92_remove, .id_table = lm92_id, .detect = lm92_detect, |
c3813d6af i2c: Get rid of s... |
410 |
.address_list = normal_i2c, |
1da177e4c Linux-2.6.12-rc2 |
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
}; static int __init sensors_lm92_init(void) { return i2c_add_driver(&lm92_driver); } static void __exit sensors_lm92_exit(void) { i2c_del_driver(&lm92_driver); } MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>"); MODULE_DESCRIPTION("LM92/MAX6635 driver"); MODULE_LICENSE("GPL"); module_init(sensors_lm92_init); module_exit(sensors_lm92_exit); |