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