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