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