Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 1 | /* tmp401.c |
| 2 | * |
| 3 | * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com> |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 4 | * Preliminary tmp411 support by: |
| 5 | * Gabriel Konat, Sander Leget, Wouter Willems |
| 6 | * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de> |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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. |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC. |
| 25 | * |
| 26 | * Note this IC is in some aspect similar to the LM90, but it has quite a |
| 27 | * few differences too, for example the local temp has a higher resolution |
| 28 | * and thus has 16 bits registers for its value and limit instead of 8 bits. |
| 29 | */ |
| 30 | |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/jiffies.h> |
| 35 | #include <linux/i2c.h> |
| 36 | #include <linux/hwmon.h> |
| 37 | #include <linux/hwmon-sysfs.h> |
| 38 | #include <linux/err.h> |
| 39 | #include <linux/mutex.h> |
| 40 | #include <linux/sysfs.h> |
| 41 | |
| 42 | /* Addresses to scan */ |
| 43 | static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END }; |
| 44 | |
Jean Delvare | e5e9f44 | 2009-12-14 21:17:27 +0100 | [diff] [blame^] | 45 | enum chips { tmp401, tmp411 }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * The TMP401 registers, note some registers have different addresses for |
| 49 | * reading and writing |
| 50 | */ |
| 51 | #define TMP401_STATUS 0x02 |
| 52 | #define TMP401_CONFIG_READ 0x03 |
| 53 | #define TMP401_CONFIG_WRITE 0x09 |
| 54 | #define TMP401_CONVERSION_RATE_READ 0x04 |
| 55 | #define TMP401_CONVERSION_RATE_WRITE 0x0A |
| 56 | #define TMP401_TEMP_CRIT_HYST 0x21 |
| 57 | #define TMP401_CONSECUTIVE_ALERT 0x22 |
| 58 | #define TMP401_MANUFACTURER_ID_REG 0xFE |
| 59 | #define TMP401_DEVICE_ID_REG 0xFF |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 60 | #define TMP411_N_FACTOR_REG 0x18 |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 61 | |
| 62 | static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 }; |
| 63 | static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 }; |
| 64 | static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2] = { 0x06, 0x08 }; |
| 65 | static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2] = { 0x0C, 0x0E }; |
| 66 | static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2] = { 0x17, 0x14 }; |
| 67 | static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2] = { 0x05, 0x07 }; |
| 68 | static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2] = { 0x0B, 0x0D }; |
| 69 | static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 }; |
| 70 | /* These are called the THERM limit / hysteresis / mask in the datasheet */ |
| 71 | static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 }; |
| 72 | |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 73 | static const u8 TMP411_TEMP_LOWEST_MSB[2] = { 0x30, 0x34 }; |
| 74 | static const u8 TMP411_TEMP_LOWEST_LSB[2] = { 0x31, 0x35 }; |
| 75 | static const u8 TMP411_TEMP_HIGHEST_MSB[2] = { 0x32, 0x36 }; |
| 76 | static const u8 TMP411_TEMP_HIGHEST_LSB[2] = { 0x33, 0x37 }; |
| 77 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 78 | /* Flags */ |
| 79 | #define TMP401_CONFIG_RANGE 0x04 |
| 80 | #define TMP401_CONFIG_SHUTDOWN 0x40 |
| 81 | #define TMP401_STATUS_LOCAL_CRIT 0x01 |
| 82 | #define TMP401_STATUS_REMOTE_CRIT 0x02 |
| 83 | #define TMP401_STATUS_REMOTE_OPEN 0x04 |
| 84 | #define TMP401_STATUS_REMOTE_LOW 0x08 |
| 85 | #define TMP401_STATUS_REMOTE_HIGH 0x10 |
| 86 | #define TMP401_STATUS_LOCAL_LOW 0x20 |
| 87 | #define TMP401_STATUS_LOCAL_HIGH 0x40 |
| 88 | |
| 89 | /* Manufacturer / Device ID's */ |
| 90 | #define TMP401_MANUFACTURER_ID 0x55 |
| 91 | #define TMP401_DEVICE_ID 0x11 |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 92 | #define TMP411_DEVICE_ID 0x12 |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Functions declarations |
| 96 | */ |
| 97 | |
| 98 | static int tmp401_probe(struct i2c_client *client, |
| 99 | const struct i2c_device_id *id); |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 100 | static int tmp401_detect(struct i2c_client *client, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 101 | struct i2c_board_info *info); |
| 102 | static int tmp401_remove(struct i2c_client *client); |
| 103 | static struct tmp401_data *tmp401_update_device(struct device *dev); |
| 104 | |
| 105 | /* |
| 106 | * Driver data (common to all clients) |
| 107 | */ |
| 108 | |
| 109 | static const struct i2c_device_id tmp401_id[] = { |
| 110 | { "tmp401", tmp401 }, |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 111 | { "tmp411", tmp411 }, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 112 | { } |
| 113 | }; |
| 114 | MODULE_DEVICE_TABLE(i2c, tmp401_id); |
| 115 | |
| 116 | static struct i2c_driver tmp401_driver = { |
| 117 | .class = I2C_CLASS_HWMON, |
| 118 | .driver = { |
| 119 | .name = "tmp401", |
| 120 | }, |
| 121 | .probe = tmp401_probe, |
| 122 | .remove = tmp401_remove, |
| 123 | .id_table = tmp401_id, |
| 124 | .detect = tmp401_detect, |
Jean Delvare | c3813d6 | 2009-12-14 21:17:25 +0100 | [diff] [blame] | 125 | .address_list = normal_i2c, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | /* |
| 129 | * Client data (each client gets its own) |
| 130 | */ |
| 131 | |
| 132 | struct tmp401_data { |
| 133 | struct device *hwmon_dev; |
| 134 | struct mutex update_lock; |
| 135 | char valid; /* zero until following fields are valid */ |
| 136 | unsigned long last_updated; /* in jiffies */ |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 137 | int kind; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 138 | |
| 139 | /* register values */ |
| 140 | u8 status; |
| 141 | u8 config; |
| 142 | u16 temp[2]; |
| 143 | u16 temp_low[2]; |
| 144 | u16 temp_high[2]; |
| 145 | u8 temp_crit[2]; |
| 146 | u8 temp_crit_hyst; |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 147 | u16 temp_lowest[2]; |
| 148 | u16 temp_highest[2]; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | /* |
| 152 | * Sysfs attr show / store functions |
| 153 | */ |
| 154 | |
| 155 | static int tmp401_register_to_temp(u16 reg, u8 config) |
| 156 | { |
| 157 | int temp = reg; |
| 158 | |
| 159 | if (config & TMP401_CONFIG_RANGE) |
| 160 | temp -= 64 * 256; |
| 161 | |
| 162 | return (temp * 625 + 80) / 160; |
| 163 | } |
| 164 | |
| 165 | static u16 tmp401_temp_to_register(long temp, u8 config) |
| 166 | { |
| 167 | if (config & TMP401_CONFIG_RANGE) { |
| 168 | temp = SENSORS_LIMIT(temp, -64000, 191000); |
| 169 | temp += 64000; |
| 170 | } else |
| 171 | temp = SENSORS_LIMIT(temp, 0, 127000); |
| 172 | |
| 173 | return (temp * 160 + 312) / 625; |
| 174 | } |
| 175 | |
| 176 | static int tmp401_crit_register_to_temp(u8 reg, u8 config) |
| 177 | { |
| 178 | int temp = reg; |
| 179 | |
| 180 | if (config & TMP401_CONFIG_RANGE) |
| 181 | temp -= 64; |
| 182 | |
| 183 | return temp * 1000; |
| 184 | } |
| 185 | |
| 186 | static u8 tmp401_crit_temp_to_register(long temp, u8 config) |
| 187 | { |
| 188 | if (config & TMP401_CONFIG_RANGE) { |
| 189 | temp = SENSORS_LIMIT(temp, -64000, 191000); |
| 190 | temp += 64000; |
| 191 | } else |
| 192 | temp = SENSORS_LIMIT(temp, 0, 127000); |
| 193 | |
| 194 | return (temp + 500) / 1000; |
| 195 | } |
| 196 | |
| 197 | static ssize_t show_temp_value(struct device *dev, |
| 198 | struct device_attribute *devattr, char *buf) |
| 199 | { |
| 200 | int index = to_sensor_dev_attr(devattr)->index; |
| 201 | struct tmp401_data *data = tmp401_update_device(dev); |
| 202 | |
| 203 | return sprintf(buf, "%d\n", |
| 204 | tmp401_register_to_temp(data->temp[index], data->config)); |
| 205 | } |
| 206 | |
| 207 | static ssize_t show_temp_min(struct device *dev, |
| 208 | struct device_attribute *devattr, char *buf) |
| 209 | { |
| 210 | int index = to_sensor_dev_attr(devattr)->index; |
| 211 | struct tmp401_data *data = tmp401_update_device(dev); |
| 212 | |
| 213 | return sprintf(buf, "%d\n", |
| 214 | tmp401_register_to_temp(data->temp_low[index], data->config)); |
| 215 | } |
| 216 | |
| 217 | static ssize_t show_temp_max(struct device *dev, |
| 218 | struct device_attribute *devattr, char *buf) |
| 219 | { |
| 220 | int index = to_sensor_dev_attr(devattr)->index; |
| 221 | struct tmp401_data *data = tmp401_update_device(dev); |
| 222 | |
| 223 | return sprintf(buf, "%d\n", |
| 224 | tmp401_register_to_temp(data->temp_high[index], data->config)); |
| 225 | } |
| 226 | |
| 227 | static ssize_t show_temp_crit(struct device *dev, |
| 228 | struct device_attribute *devattr, char *buf) |
| 229 | { |
| 230 | int index = to_sensor_dev_attr(devattr)->index; |
| 231 | struct tmp401_data *data = tmp401_update_device(dev); |
| 232 | |
| 233 | return sprintf(buf, "%d\n", |
| 234 | tmp401_crit_register_to_temp(data->temp_crit[index], |
| 235 | data->config)); |
| 236 | } |
| 237 | |
| 238 | static ssize_t show_temp_crit_hyst(struct device *dev, |
| 239 | struct device_attribute *devattr, char *buf) |
| 240 | { |
| 241 | int temp, index = to_sensor_dev_attr(devattr)->index; |
| 242 | struct tmp401_data *data = tmp401_update_device(dev); |
| 243 | |
| 244 | mutex_lock(&data->update_lock); |
| 245 | temp = tmp401_crit_register_to_temp(data->temp_crit[index], |
| 246 | data->config); |
| 247 | temp -= data->temp_crit_hyst * 1000; |
| 248 | mutex_unlock(&data->update_lock); |
| 249 | |
| 250 | return sprintf(buf, "%d\n", temp); |
| 251 | } |
| 252 | |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 253 | static ssize_t show_temp_lowest(struct device *dev, |
| 254 | struct device_attribute *devattr, char *buf) |
| 255 | { |
| 256 | int index = to_sensor_dev_attr(devattr)->index; |
| 257 | struct tmp401_data *data = tmp401_update_device(dev); |
| 258 | |
| 259 | return sprintf(buf, "%d\n", |
| 260 | tmp401_register_to_temp(data->temp_lowest[index], |
| 261 | data->config)); |
| 262 | } |
| 263 | |
| 264 | static ssize_t show_temp_highest(struct device *dev, |
| 265 | struct device_attribute *devattr, char *buf) |
| 266 | { |
| 267 | int index = to_sensor_dev_attr(devattr)->index; |
| 268 | struct tmp401_data *data = tmp401_update_device(dev); |
| 269 | |
| 270 | return sprintf(buf, "%d\n", |
| 271 | tmp401_register_to_temp(data->temp_highest[index], |
| 272 | data->config)); |
| 273 | } |
| 274 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 275 | static ssize_t show_status(struct device *dev, |
| 276 | struct device_attribute *devattr, char *buf) |
| 277 | { |
| 278 | int mask = to_sensor_dev_attr(devattr)->index; |
| 279 | struct tmp401_data *data = tmp401_update_device(dev); |
| 280 | |
| 281 | if (data->status & mask) |
| 282 | return sprintf(buf, "1\n"); |
| 283 | else |
| 284 | return sprintf(buf, "0\n"); |
| 285 | } |
| 286 | |
| 287 | static ssize_t store_temp_min(struct device *dev, struct device_attribute |
| 288 | *devattr, const char *buf, size_t count) |
| 289 | { |
| 290 | int index = to_sensor_dev_attr(devattr)->index; |
| 291 | struct tmp401_data *data = tmp401_update_device(dev); |
| 292 | long val; |
| 293 | u16 reg; |
| 294 | |
| 295 | if (strict_strtol(buf, 10, &val)) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | reg = tmp401_temp_to_register(val, data->config); |
| 299 | |
| 300 | mutex_lock(&data->update_lock); |
| 301 | |
| 302 | i2c_smbus_write_byte_data(to_i2c_client(dev), |
| 303 | TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8); |
| 304 | i2c_smbus_write_byte_data(to_i2c_client(dev), |
| 305 | TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF); |
| 306 | |
| 307 | data->temp_low[index] = reg; |
| 308 | |
| 309 | mutex_unlock(&data->update_lock); |
| 310 | |
| 311 | return count; |
| 312 | } |
| 313 | |
| 314 | static ssize_t store_temp_max(struct device *dev, struct device_attribute |
| 315 | *devattr, const char *buf, size_t count) |
| 316 | { |
| 317 | int index = to_sensor_dev_attr(devattr)->index; |
| 318 | struct tmp401_data *data = tmp401_update_device(dev); |
| 319 | long val; |
| 320 | u16 reg; |
| 321 | |
| 322 | if (strict_strtol(buf, 10, &val)) |
| 323 | return -EINVAL; |
| 324 | |
| 325 | reg = tmp401_temp_to_register(val, data->config); |
| 326 | |
| 327 | mutex_lock(&data->update_lock); |
| 328 | |
| 329 | i2c_smbus_write_byte_data(to_i2c_client(dev), |
| 330 | TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8); |
| 331 | i2c_smbus_write_byte_data(to_i2c_client(dev), |
| 332 | TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF); |
| 333 | |
| 334 | data->temp_high[index] = reg; |
| 335 | |
| 336 | mutex_unlock(&data->update_lock); |
| 337 | |
| 338 | return count; |
| 339 | } |
| 340 | |
| 341 | static ssize_t store_temp_crit(struct device *dev, struct device_attribute |
| 342 | *devattr, const char *buf, size_t count) |
| 343 | { |
| 344 | int index = to_sensor_dev_attr(devattr)->index; |
| 345 | struct tmp401_data *data = tmp401_update_device(dev); |
| 346 | long val; |
| 347 | u8 reg; |
| 348 | |
| 349 | if (strict_strtol(buf, 10, &val)) |
| 350 | return -EINVAL; |
| 351 | |
| 352 | reg = tmp401_crit_temp_to_register(val, data->config); |
| 353 | |
| 354 | mutex_lock(&data->update_lock); |
| 355 | |
| 356 | i2c_smbus_write_byte_data(to_i2c_client(dev), |
| 357 | TMP401_TEMP_CRIT_LIMIT[index], reg); |
| 358 | |
| 359 | data->temp_crit[index] = reg; |
| 360 | |
| 361 | mutex_unlock(&data->update_lock); |
| 362 | |
| 363 | return count; |
| 364 | } |
| 365 | |
| 366 | static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute |
| 367 | *devattr, const char *buf, size_t count) |
| 368 | { |
| 369 | int temp, index = to_sensor_dev_attr(devattr)->index; |
| 370 | struct tmp401_data *data = tmp401_update_device(dev); |
| 371 | long val; |
| 372 | u8 reg; |
| 373 | |
| 374 | if (strict_strtol(buf, 10, &val)) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | if (data->config & TMP401_CONFIG_RANGE) |
| 378 | val = SENSORS_LIMIT(val, -64000, 191000); |
| 379 | else |
| 380 | val = SENSORS_LIMIT(val, 0, 127000); |
| 381 | |
| 382 | mutex_lock(&data->update_lock); |
| 383 | temp = tmp401_crit_register_to_temp(data->temp_crit[index], |
| 384 | data->config); |
| 385 | val = SENSORS_LIMIT(val, temp - 255000, temp); |
| 386 | reg = ((temp - val) + 500) / 1000; |
| 387 | |
| 388 | i2c_smbus_write_byte_data(to_i2c_client(dev), |
| 389 | TMP401_TEMP_CRIT_HYST, reg); |
| 390 | |
| 391 | data->temp_crit_hyst = reg; |
| 392 | |
| 393 | mutex_unlock(&data->update_lock); |
| 394 | |
| 395 | return count; |
| 396 | } |
| 397 | |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 398 | /* |
| 399 | * Resets the historical measurements of minimum and maximum temperatures. |
| 400 | * This is done by writing any value to any of the minimum/maximum registers |
| 401 | * (0x30-0x37). |
| 402 | */ |
| 403 | static ssize_t reset_temp_history(struct device *dev, |
| 404 | struct device_attribute *devattr, const char *buf, size_t count) |
| 405 | { |
| 406 | long val; |
| 407 | |
| 408 | if (strict_strtol(buf, 10, &val)) |
| 409 | return -EINVAL; |
| 410 | |
| 411 | if (val != 1) { |
| 412 | dev_err(dev, "temp_reset_history value %ld not" |
| 413 | " supported. Use 1 to reset the history!\n", val); |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | i2c_smbus_write_byte_data(to_i2c_client(dev), |
| 417 | TMP411_TEMP_LOWEST_MSB[0], val); |
| 418 | |
| 419 | return count; |
| 420 | } |
| 421 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 422 | static struct sensor_device_attribute tmp401_attr[] = { |
| 423 | SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0), |
| 424 | SENSOR_ATTR(temp1_min, 0644, show_temp_min, store_temp_min, 0), |
| 425 | SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0), |
| 426 | SENSOR_ATTR(temp1_crit, 0644, show_temp_crit, store_temp_crit, 0), |
| 427 | SENSOR_ATTR(temp1_crit_hyst, 0644, show_temp_crit_hyst, |
| 428 | store_temp_crit_hyst, 0), |
| 429 | SENSOR_ATTR(temp1_min_alarm, 0444, show_status, NULL, |
| 430 | TMP401_STATUS_LOCAL_LOW), |
| 431 | SENSOR_ATTR(temp1_max_alarm, 0444, show_status, NULL, |
| 432 | TMP401_STATUS_LOCAL_HIGH), |
| 433 | SENSOR_ATTR(temp1_crit_alarm, 0444, show_status, NULL, |
| 434 | TMP401_STATUS_LOCAL_CRIT), |
| 435 | SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1), |
| 436 | SENSOR_ATTR(temp2_min, 0644, show_temp_min, store_temp_min, 1), |
| 437 | SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1), |
| 438 | SENSOR_ATTR(temp2_crit, 0644, show_temp_crit, store_temp_crit, 1), |
| 439 | SENSOR_ATTR(temp2_crit_hyst, 0444, show_temp_crit_hyst, NULL, 1), |
| 440 | SENSOR_ATTR(temp2_fault, 0444, show_status, NULL, |
| 441 | TMP401_STATUS_REMOTE_OPEN), |
| 442 | SENSOR_ATTR(temp2_min_alarm, 0444, show_status, NULL, |
| 443 | TMP401_STATUS_REMOTE_LOW), |
| 444 | SENSOR_ATTR(temp2_max_alarm, 0444, show_status, NULL, |
| 445 | TMP401_STATUS_REMOTE_HIGH), |
| 446 | SENSOR_ATTR(temp2_crit_alarm, 0444, show_status, NULL, |
| 447 | TMP401_STATUS_REMOTE_CRIT), |
| 448 | }; |
| 449 | |
| 450 | /* |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 451 | * Additional features of the TMP411 chip. |
| 452 | * The TMP411 stores the minimum and maximum |
| 453 | * temperature measured since power-on, chip-reset, or |
| 454 | * minimum and maximum register reset for both the local |
| 455 | * and remote channels. |
| 456 | */ |
| 457 | static struct sensor_device_attribute tmp411_attr[] = { |
| 458 | SENSOR_ATTR(temp1_highest, 0444, show_temp_highest, NULL, 0), |
| 459 | SENSOR_ATTR(temp1_lowest, 0444, show_temp_lowest, NULL, 0), |
| 460 | SENSOR_ATTR(temp2_highest, 0444, show_temp_highest, NULL, 1), |
| 461 | SENSOR_ATTR(temp2_lowest, 0444, show_temp_lowest, NULL, 1), |
| 462 | SENSOR_ATTR(temp_reset_history, 0200, NULL, reset_temp_history, 0), |
| 463 | }; |
| 464 | |
| 465 | /* |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 466 | * Begin non sysfs callback code (aka Real code) |
| 467 | */ |
| 468 | |
| 469 | static void tmp401_init_client(struct i2c_client *client) |
| 470 | { |
| 471 | int config, config_orig; |
| 472 | |
| 473 | /* Set the conversion rate to 2 Hz */ |
| 474 | i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5); |
| 475 | |
| 476 | /* Start conversions (disable shutdown if necessary) */ |
| 477 | config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); |
| 478 | if (config < 0) { |
| 479 | dev_warn(&client->dev, "Initialization failed!\n"); |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | config_orig = config; |
| 484 | config &= ~TMP401_CONFIG_SHUTDOWN; |
| 485 | |
| 486 | if (config != config_orig) |
| 487 | i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config); |
| 488 | } |
| 489 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 490 | static int tmp401_detect(struct i2c_client *client, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 491 | struct i2c_board_info *info) |
| 492 | { |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 493 | enum chips kind; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 494 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 495 | u8 reg; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 496 | |
| 497 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 498 | return -ENODEV; |
| 499 | |
| 500 | /* Detect and identify the chip */ |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 501 | reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG); |
| 502 | if (reg != TMP401_MANUFACTURER_ID) |
| 503 | return -ENODEV; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 504 | |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 505 | reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 506 | |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 507 | switch (reg) { |
| 508 | case TMP401_DEVICE_ID: |
| 509 | kind = tmp401; |
| 510 | break; |
| 511 | case TMP411_DEVICE_ID: |
| 512 | kind = tmp411; |
| 513 | break; |
| 514 | default: |
| 515 | return -ENODEV; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 516 | } |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 517 | |
| 518 | reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); |
| 519 | if (reg & 0x1b) |
| 520 | return -ENODEV; |
| 521 | |
| 522 | reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ); |
| 523 | /* Datasheet says: 0x1-0x6 */ |
| 524 | if (reg > 15) |
| 525 | return -ENODEV; |
| 526 | |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 527 | strlcpy(info->type, tmp401_id[kind - 1].name, I2C_NAME_SIZE); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int tmp401_probe(struct i2c_client *client, |
| 533 | const struct i2c_device_id *id) |
| 534 | { |
| 535 | int i, err = 0; |
| 536 | struct tmp401_data *data; |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 537 | const char *names[] = { "TMP401", "TMP411" }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 538 | |
| 539 | data = kzalloc(sizeof(struct tmp401_data), GFP_KERNEL); |
| 540 | if (!data) |
| 541 | return -ENOMEM; |
| 542 | |
| 543 | i2c_set_clientdata(client, data); |
| 544 | mutex_init(&data->update_lock); |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 545 | data->kind = id->driver_data; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 546 | |
| 547 | /* Initialize the TMP401 chip */ |
| 548 | tmp401_init_client(client); |
| 549 | |
| 550 | /* Register sysfs hooks */ |
| 551 | for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) { |
| 552 | err = device_create_file(&client->dev, |
| 553 | &tmp401_attr[i].dev_attr); |
| 554 | if (err) |
| 555 | goto exit_remove; |
| 556 | } |
| 557 | |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 558 | /* Register aditional tmp411 sysfs hooks */ |
| 559 | if (data->kind == tmp411) { |
| 560 | for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) { |
| 561 | err = device_create_file(&client->dev, |
| 562 | &tmp411_attr[i].dev_attr); |
| 563 | if (err) |
| 564 | goto exit_remove; |
| 565 | } |
| 566 | } |
| 567 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 568 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 569 | if (IS_ERR(data->hwmon_dev)) { |
| 570 | err = PTR_ERR(data->hwmon_dev); |
| 571 | data->hwmon_dev = NULL; |
| 572 | goto exit_remove; |
| 573 | } |
| 574 | |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 575 | dev_info(&client->dev, "Detected TI %s chip\n", |
| 576 | names[data->kind - 1]); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 577 | |
| 578 | return 0; |
| 579 | |
| 580 | exit_remove: |
| 581 | tmp401_remove(client); /* will also free data for us */ |
| 582 | return err; |
| 583 | } |
| 584 | |
| 585 | static int tmp401_remove(struct i2c_client *client) |
| 586 | { |
| 587 | struct tmp401_data *data = i2c_get_clientdata(client); |
| 588 | int i; |
| 589 | |
| 590 | if (data->hwmon_dev) |
| 591 | hwmon_device_unregister(data->hwmon_dev); |
| 592 | |
| 593 | for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) |
| 594 | device_remove_file(&client->dev, &tmp401_attr[i].dev_attr); |
| 595 | |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 596 | if (data->kind == tmp411) { |
| 597 | for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) |
| 598 | device_remove_file(&client->dev, |
| 599 | &tmp411_attr[i].dev_attr); |
| 600 | } |
| 601 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 602 | kfree(data); |
| 603 | return 0; |
| 604 | } |
| 605 | |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 606 | static struct tmp401_data *tmp401_update_device_reg16( |
| 607 | struct i2c_client *client, struct tmp401_data *data) |
| 608 | { |
| 609 | int i; |
| 610 | |
| 611 | for (i = 0; i < 2; i++) { |
| 612 | /* |
| 613 | * High byte must be read first immediately followed |
| 614 | * by the low byte |
| 615 | */ |
| 616 | data->temp[i] = i2c_smbus_read_byte_data(client, |
| 617 | TMP401_TEMP_MSB[i]) << 8; |
| 618 | data->temp[i] |= i2c_smbus_read_byte_data(client, |
| 619 | TMP401_TEMP_LSB[i]); |
| 620 | data->temp_low[i] = i2c_smbus_read_byte_data(client, |
| 621 | TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8; |
| 622 | data->temp_low[i] |= i2c_smbus_read_byte_data(client, |
| 623 | TMP401_TEMP_LOW_LIMIT_LSB[i]); |
| 624 | data->temp_high[i] = i2c_smbus_read_byte_data(client, |
| 625 | TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8; |
| 626 | data->temp_high[i] |= i2c_smbus_read_byte_data(client, |
| 627 | TMP401_TEMP_HIGH_LIMIT_LSB[i]); |
| 628 | data->temp_crit[i] = i2c_smbus_read_byte_data(client, |
| 629 | TMP401_TEMP_CRIT_LIMIT[i]); |
| 630 | |
| 631 | if (data->kind == tmp411) { |
| 632 | data->temp_lowest[i] = i2c_smbus_read_byte_data(client, |
| 633 | TMP411_TEMP_LOWEST_MSB[i]) << 8; |
| 634 | data->temp_lowest[i] |= i2c_smbus_read_byte_data( |
| 635 | client, TMP411_TEMP_LOWEST_LSB[i]); |
| 636 | |
| 637 | data->temp_highest[i] = i2c_smbus_read_byte_data( |
| 638 | client, TMP411_TEMP_HIGHEST_MSB[i]) << 8; |
| 639 | data->temp_highest[i] |= i2c_smbus_read_byte_data( |
| 640 | client, TMP411_TEMP_HIGHEST_LSB[i]); |
| 641 | } |
| 642 | } |
| 643 | return data; |
| 644 | } |
| 645 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 646 | static struct tmp401_data *tmp401_update_device(struct device *dev) |
| 647 | { |
| 648 | struct i2c_client *client = to_i2c_client(dev); |
| 649 | struct tmp401_data *data = i2c_get_clientdata(client); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 650 | |
| 651 | mutex_lock(&data->update_lock); |
| 652 | |
| 653 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
| 654 | data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS); |
| 655 | data->config = i2c_smbus_read_byte_data(client, |
| 656 | TMP401_CONFIG_READ); |
Andre Prendel | fce0758 | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 657 | tmp401_update_device_reg16(client, data); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 658 | |
| 659 | data->temp_crit_hyst = i2c_smbus_read_byte_data(client, |
| 660 | TMP401_TEMP_CRIT_HYST); |
| 661 | |
| 662 | data->last_updated = jiffies; |
| 663 | data->valid = 1; |
| 664 | } |
| 665 | |
| 666 | mutex_unlock(&data->update_lock); |
| 667 | |
| 668 | return data; |
| 669 | } |
| 670 | |
| 671 | static int __init tmp401_init(void) |
| 672 | { |
| 673 | return i2c_add_driver(&tmp401_driver); |
| 674 | } |
| 675 | |
| 676 | static void __exit tmp401_exit(void) |
| 677 | { |
| 678 | i2c_del_driver(&tmp401_driver); |
| 679 | } |
| 680 | |
| 681 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
| 682 | MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver"); |
| 683 | MODULE_LICENSE("GPL"); |
| 684 | |
| 685 | module_init(tmp401_init); |
| 686 | module_exit(tmp401_exit); |