Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 2 | /* tmp401.c |
| 3 | * |
| 4 | * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com> |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 5 | * Preliminary tmp411 support by: |
| 6 | * Gabriel Konat, Sander Leget, Wouter Willems |
| 7 | * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de> |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 8 | * |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 9 | * Cleanup and support for TMP431 and TMP432 by Guenter Roeck |
| 10 | * Copyright (c) 2013 Guenter Roeck <linux@roeck-us.net> |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC. |
| 15 | * |
| 16 | * Note this IC is in some aspect similar to the LM90, but it has quite a |
| 17 | * few differences too, for example the local temp has a higher resolution |
| 18 | * and thus has 16 bits registers for its value and limit instead of 8 bits. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
Guenter Roeck | 947e927 | 2013-03-27 08:48:03 -0700 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/jiffies.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/hwmon.h> |
| 28 | #include <linux/hwmon-sysfs.h> |
| 29 | #include <linux/err.h> |
| 30 | #include <linux/mutex.h> |
| 31 | #include <linux/sysfs.h> |
| 32 | |
| 33 | /* Addresses to scan */ |
Guenter Roeck | 9aecac0 | 2015-05-27 16:11:48 -0700 | [diff] [blame] | 34 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 0x4d, |
Guenter Roeck | 907a6d5 | 2014-12-05 10:15:03 -0800 | [diff] [blame] | 35 | 0x4e, 0x4f, I2C_CLIENT_END }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 36 | |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 37 | enum chips { tmp401, tmp411, tmp431, tmp432, tmp435, tmp461 }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * The TMP401 registers, note some registers have different addresses for |
| 41 | * reading and writing |
| 42 | */ |
| 43 | #define TMP401_STATUS 0x02 |
| 44 | #define TMP401_CONFIG_READ 0x03 |
| 45 | #define TMP401_CONFIG_WRITE 0x09 |
| 46 | #define TMP401_CONVERSION_RATE_READ 0x04 |
| 47 | #define TMP401_CONVERSION_RATE_WRITE 0x0A |
| 48 | #define TMP401_TEMP_CRIT_HYST 0x21 |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 49 | #define TMP401_MANUFACTURER_ID_REG 0xFE |
| 50 | #define TMP401_DEVICE_ID_REG 0xFF |
| 51 | |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 52 | static const u8 TMP401_TEMP_MSB_READ[7][2] = { |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 53 | { 0x00, 0x01 }, /* temp */ |
| 54 | { 0x06, 0x08 }, /* low limit */ |
| 55 | { 0x05, 0x07 }, /* high limit */ |
| 56 | { 0x20, 0x19 }, /* therm (crit) limit */ |
| 57 | { 0x30, 0x34 }, /* lowest */ |
| 58 | { 0x32, 0x36 }, /* highest */ |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 59 | { 0, 0x11 }, /* offset */ |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 60 | }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 61 | |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 62 | static const u8 TMP401_TEMP_MSB_WRITE[7][2] = { |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 63 | { 0, 0 }, /* temp (unused) */ |
| 64 | { 0x0C, 0x0E }, /* low limit */ |
| 65 | { 0x0B, 0x0D }, /* high limit */ |
| 66 | { 0x20, 0x19 }, /* therm (crit) limit */ |
| 67 | { 0x30, 0x34 }, /* lowest */ |
| 68 | { 0x32, 0x36 }, /* highest */ |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 69 | { 0, 0x11 }, /* offset */ |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 72 | static const u8 TMP432_TEMP_MSB_READ[4][3] = { |
| 73 | { 0x00, 0x01, 0x23 }, /* temp */ |
| 74 | { 0x06, 0x08, 0x16 }, /* low limit */ |
| 75 | { 0x05, 0x07, 0x15 }, /* high limit */ |
| 76 | { 0x20, 0x19, 0x1A }, /* therm (crit) limit */ |
| 77 | }; |
| 78 | |
| 79 | static const u8 TMP432_TEMP_MSB_WRITE[4][3] = { |
| 80 | { 0, 0, 0 }, /* temp - unused */ |
| 81 | { 0x0C, 0x0E, 0x16 }, /* low limit */ |
| 82 | { 0x0B, 0x0D, 0x15 }, /* high limit */ |
| 83 | { 0x20, 0x19, 0x1A }, /* therm (crit) limit */ |
| 84 | }; |
| 85 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 86 | /* [0] = fault, [1] = low, [2] = high, [3] = therm/crit */ |
| 87 | static const u8 TMP432_STATUS_REG[] = { |
| 88 | 0x1b, 0x36, 0x35, 0x37 }; |
| 89 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 90 | /* Flags */ |
Guenter Roeck | 947e927 | 2013-03-27 08:48:03 -0700 | [diff] [blame] | 91 | #define TMP401_CONFIG_RANGE BIT(2) |
| 92 | #define TMP401_CONFIG_SHUTDOWN BIT(6) |
| 93 | #define TMP401_STATUS_LOCAL_CRIT BIT(0) |
| 94 | #define TMP401_STATUS_REMOTE_CRIT BIT(1) |
| 95 | #define TMP401_STATUS_REMOTE_OPEN BIT(2) |
| 96 | #define TMP401_STATUS_REMOTE_LOW BIT(3) |
| 97 | #define TMP401_STATUS_REMOTE_HIGH BIT(4) |
| 98 | #define TMP401_STATUS_LOCAL_LOW BIT(5) |
| 99 | #define TMP401_STATUS_LOCAL_HIGH BIT(6) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 100 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 101 | /* On TMP432, each status has its own register */ |
| 102 | #define TMP432_STATUS_LOCAL BIT(0) |
| 103 | #define TMP432_STATUS_REMOTE1 BIT(1) |
| 104 | #define TMP432_STATUS_REMOTE2 BIT(2) |
| 105 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 106 | /* Manufacturer / Device ID's */ |
| 107 | #define TMP401_MANUFACTURER_ID 0x55 |
| 108 | #define TMP401_DEVICE_ID 0x11 |
Guenter Roeck | 4ce5b1f | 2013-03-29 17:56:07 -0700 | [diff] [blame] | 109 | #define TMP411A_DEVICE_ID 0x12 |
| 110 | #define TMP411B_DEVICE_ID 0x13 |
| 111 | #define TMP411C_DEVICE_ID 0x10 |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 112 | #define TMP431_DEVICE_ID 0x31 |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 113 | #define TMP432_DEVICE_ID 0x32 |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 114 | #define TMP435_DEVICE_ID 0x35 |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 115 | |
| 116 | /* |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 117 | * Driver data (common to all clients) |
| 118 | */ |
| 119 | |
| 120 | static const struct i2c_device_id tmp401_id[] = { |
| 121 | { "tmp401", tmp401 }, |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 122 | { "tmp411", tmp411 }, |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 123 | { "tmp431", tmp431 }, |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 124 | { "tmp432", tmp432 }, |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 125 | { "tmp435", tmp435 }, |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 126 | { "tmp461", tmp461 }, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 127 | { } |
| 128 | }; |
| 129 | MODULE_DEVICE_TABLE(i2c, tmp401_id); |
| 130 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Client data (each client gets its own) |
| 133 | */ |
| 134 | |
| 135 | struct tmp401_data { |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 136 | struct i2c_client *client; |
| 137 | const struct attribute_group *groups[3]; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 138 | struct mutex update_lock; |
| 139 | char valid; /* zero until following fields are valid */ |
| 140 | unsigned long last_updated; /* in jiffies */ |
Jean Delvare | dc71afe | 2010-03-05 22:17:26 +0100 | [diff] [blame] | 141 | enum chips kind; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 142 | |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 143 | unsigned int update_interval; /* in milliseconds */ |
| 144 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 145 | /* register values */ |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 146 | u8 status[4]; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 147 | u8 config; |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 148 | u16 temp[7][3]; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 149 | u8 temp_crit_hyst; |
| 150 | }; |
| 151 | |
| 152 | /* |
| 153 | * Sysfs attr show / store functions |
| 154 | */ |
| 155 | |
| 156 | static int tmp401_register_to_temp(u16 reg, u8 config) |
| 157 | { |
| 158 | int temp = reg; |
| 159 | |
| 160 | if (config & TMP401_CONFIG_RANGE) |
| 161 | temp -= 64 * 256; |
| 162 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 163 | return DIV_ROUND_CLOSEST(temp * 125, 32); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 166 | 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] | 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 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 174 | return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 177 | static int tmp401_update_device_reg16(struct i2c_client *client, |
| 178 | struct tmp401_data *data) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 179 | { |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 180 | int i, j, val; |
| 181 | int num_regs = data->kind == tmp411 ? 6 : 4; |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 182 | int num_sensors = data->kind == tmp432 ? 3 : 2; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 183 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 184 | for (i = 0; i < num_sensors; i++) { /* local / r1 / r2 */ |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 185 | for (j = 0; j < num_regs; j++) { /* temp / low / ... */ |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 186 | u8 regaddr; |
Jeroen De Wachter | 24333ac | 2017-01-09 18:55:00 +0100 | [diff] [blame] | 187 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 188 | regaddr = data->kind == tmp432 ? |
| 189 | TMP432_TEMP_MSB_READ[j][i] : |
| 190 | TMP401_TEMP_MSB_READ[j][i]; |
Jeroen De Wachter | 24333ac | 2017-01-09 18:55:00 +0100 | [diff] [blame] | 191 | if (j == 3) { /* crit is msb only */ |
| 192 | val = i2c_smbus_read_byte_data(client, regaddr); |
| 193 | } else { |
| 194 | val = i2c_smbus_read_word_swapped(client, |
| 195 | regaddr); |
| 196 | } |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 197 | if (val < 0) |
| 198 | return val; |
Jeroen De Wachter | 24333ac | 2017-01-09 18:55:00 +0100 | [diff] [blame] | 199 | |
| 200 | data->temp[j][i] = j == 3 ? val << 8 : val; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 201 | } |
| 202 | } |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 203 | return 0; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static struct tmp401_data *tmp401_update_device(struct device *dev) |
| 207 | { |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 208 | struct tmp401_data *data = dev_get_drvdata(dev); |
| 209 | struct i2c_client *client = data->client; |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 210 | struct tmp401_data *ret = data; |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 211 | int i, val; |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 212 | unsigned long next_update; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 213 | |
| 214 | mutex_lock(&data->update_lock); |
| 215 | |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 216 | next_update = data->last_updated + |
Jean Delvare | 4e2284d | 2013-05-19 16:57:30 +0200 | [diff] [blame] | 217 | msecs_to_jiffies(data->update_interval); |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 218 | if (time_after(jiffies, next_update) || !data->valid) { |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 219 | if (data->kind != tmp432) { |
| 220 | /* |
| 221 | * The driver uses the TMP432 status format internally. |
| 222 | * Convert status to TMP432 format for other chips. |
| 223 | */ |
| 224 | val = i2c_smbus_read_byte_data(client, TMP401_STATUS); |
| 225 | if (val < 0) { |
| 226 | ret = ERR_PTR(val); |
| 227 | goto abort; |
| 228 | } |
| 229 | data->status[0] = |
| 230 | (val & TMP401_STATUS_REMOTE_OPEN) >> 1; |
| 231 | data->status[1] = |
| 232 | ((val & TMP401_STATUS_REMOTE_LOW) >> 2) | |
| 233 | ((val & TMP401_STATUS_LOCAL_LOW) >> 5); |
| 234 | data->status[2] = |
| 235 | ((val & TMP401_STATUS_REMOTE_HIGH) >> 3) | |
| 236 | ((val & TMP401_STATUS_LOCAL_HIGH) >> 6); |
| 237 | data->status[3] = val & (TMP401_STATUS_LOCAL_CRIT |
| 238 | | TMP401_STATUS_REMOTE_CRIT); |
| 239 | } else { |
| 240 | for (i = 0; i < ARRAY_SIZE(data->status); i++) { |
| 241 | val = i2c_smbus_read_byte_data(client, |
| 242 | TMP432_STATUS_REG[i]); |
| 243 | if (val < 0) { |
| 244 | ret = ERR_PTR(val); |
| 245 | goto abort; |
| 246 | } |
| 247 | data->status[i] = val; |
| 248 | } |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 249 | } |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 250 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 251 | val = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); |
| 252 | if (val < 0) { |
| 253 | ret = ERR_PTR(val); |
| 254 | goto abort; |
| 255 | } |
| 256 | data->config = val; |
| 257 | val = tmp401_update_device_reg16(client, data); |
| 258 | if (val < 0) { |
| 259 | ret = ERR_PTR(val); |
| 260 | goto abort; |
| 261 | } |
| 262 | val = i2c_smbus_read_byte_data(client, TMP401_TEMP_CRIT_HYST); |
| 263 | if (val < 0) { |
| 264 | ret = ERR_PTR(val); |
| 265 | goto abort; |
| 266 | } |
| 267 | data->temp_crit_hyst = val; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 268 | |
| 269 | data->last_updated = jiffies; |
| 270 | data->valid = 1; |
| 271 | } |
| 272 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 273 | abort: |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 274 | mutex_unlock(&data->update_lock); |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 275 | return ret; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 278 | static ssize_t temp_show(struct device *dev, struct device_attribute *devattr, |
| 279 | char *buf) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 280 | { |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 281 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 282 | int index = to_sensor_dev_attr_2(devattr)->index; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 283 | struct tmp401_data *data = tmp401_update_device(dev); |
| 284 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 285 | if (IS_ERR(data)) |
| 286 | return PTR_ERR(data); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 287 | |
| 288 | return sprintf(buf, "%d\n", |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 289 | tmp401_register_to_temp(data->temp[nr][index], data->config)); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 292 | static ssize_t temp_crit_hyst_show(struct device *dev, |
| 293 | struct device_attribute *devattr, |
| 294 | char *buf) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 295 | { |
| 296 | int temp, index = to_sensor_dev_attr(devattr)->index; |
| 297 | struct tmp401_data *data = tmp401_update_device(dev); |
| 298 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 299 | if (IS_ERR(data)) |
| 300 | return PTR_ERR(data); |
| 301 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 302 | mutex_lock(&data->update_lock); |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 303 | temp = tmp401_register_to_temp(data->temp[3][index], data->config); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 304 | temp -= data->temp_crit_hyst * 1000; |
| 305 | mutex_unlock(&data->update_lock); |
| 306 | |
| 307 | return sprintf(buf, "%d\n", temp); |
| 308 | } |
| 309 | |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 310 | static ssize_t status_show(struct device *dev, |
| 311 | struct device_attribute *devattr, char *buf) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 312 | { |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 313 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 314 | int mask = to_sensor_dev_attr_2(devattr)->index; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 315 | struct tmp401_data *data = tmp401_update_device(dev); |
| 316 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 317 | if (IS_ERR(data)) |
| 318 | return PTR_ERR(data); |
| 319 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 320 | return sprintf(buf, "%d\n", !!(data->status[nr] & mask)); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 323 | static ssize_t temp_store(struct device *dev, |
| 324 | struct device_attribute *devattr, const char *buf, |
| 325 | size_t count) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 326 | { |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 327 | int nr = to_sensor_dev_attr_2(devattr)->nr; |
| 328 | int index = to_sensor_dev_attr_2(devattr)->index; |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 329 | struct tmp401_data *data = dev_get_drvdata(dev); |
| 330 | struct i2c_client *client = data->client; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 331 | long val; |
| 332 | u16 reg; |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 333 | u8 regaddr; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 334 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 335 | if (kstrtol(buf, 10, &val)) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 336 | return -EINVAL; |
| 337 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 338 | 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] | 339 | |
| 340 | mutex_lock(&data->update_lock); |
| 341 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 342 | regaddr = data->kind == tmp432 ? TMP432_TEMP_MSB_WRITE[nr][index] |
| 343 | : TMP401_TEMP_MSB_WRITE[nr][index]; |
Jeroen De Wachter | 24333ac | 2017-01-09 18:55:00 +0100 | [diff] [blame] | 344 | if (nr == 3) { /* crit is msb only */ |
| 345 | i2c_smbus_write_byte_data(client, regaddr, reg >> 8); |
| 346 | } else { |
| 347 | /* Hardware expects big endian data --> use _swapped */ |
| 348 | i2c_smbus_write_word_swapped(client, regaddr, reg); |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 349 | } |
| 350 | data->temp[nr][index] = reg; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 351 | |
| 352 | mutex_unlock(&data->update_lock); |
| 353 | |
| 354 | return count; |
| 355 | } |
| 356 | |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 357 | static ssize_t temp_crit_hyst_store(struct device *dev, |
| 358 | struct device_attribute *devattr, |
| 359 | const char *buf, size_t count) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 360 | { |
| 361 | int temp, index = to_sensor_dev_attr(devattr)->index; |
| 362 | struct tmp401_data *data = tmp401_update_device(dev); |
| 363 | long val; |
| 364 | u8 reg; |
| 365 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 366 | if (IS_ERR(data)) |
| 367 | return PTR_ERR(data); |
| 368 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 369 | if (kstrtol(buf, 10, &val)) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 370 | return -EINVAL; |
| 371 | |
| 372 | if (data->config & TMP401_CONFIG_RANGE) |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 373 | val = clamp_val(val, -64000, 191000); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 374 | else |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 375 | val = clamp_val(val, 0, 127000); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 376 | |
| 377 | mutex_lock(&data->update_lock); |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 378 | temp = tmp401_register_to_temp(data->temp[3][index], data->config); |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 379 | val = clamp_val(val, temp - 255000, temp); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 380 | reg = ((temp - val) + 500) / 1000; |
| 381 | |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 382 | i2c_smbus_write_byte_data(data->client, TMP401_TEMP_CRIT_HYST, |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 383 | reg); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 384 | |
| 385 | data->temp_crit_hyst = reg; |
| 386 | |
| 387 | mutex_unlock(&data->update_lock); |
| 388 | |
| 389 | return count; |
| 390 | } |
| 391 | |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 392 | /* |
| 393 | * Resets the historical measurements of minimum and maximum temperatures. |
| 394 | * This is done by writing any value to any of the minimum/maximum registers |
| 395 | * (0x30-0x37). |
| 396 | */ |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 397 | static ssize_t reset_temp_history_store(struct device *dev, |
| 398 | struct device_attribute *devattr, |
| 399 | const char *buf, size_t count) |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 400 | { |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 401 | struct tmp401_data *data = dev_get_drvdata(dev); |
| 402 | struct i2c_client *client = data->client; |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 403 | long val; |
| 404 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 405 | if (kstrtol(buf, 10, &val)) |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 406 | return -EINVAL; |
| 407 | |
| 408 | if (val != 1) { |
Guenter Roeck | b55f375 | 2013-01-10 10:01:24 -0800 | [diff] [blame] | 409 | dev_err(dev, |
| 410 | "temp_reset_history value %ld not supported. Use 1 to reset the history!\n", |
| 411 | val); |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 412 | return -EINVAL; |
| 413 | } |
Guenter Roeck | 8eb6d90 | 2013-04-14 04:39:46 -0700 | [diff] [blame] | 414 | mutex_lock(&data->update_lock); |
| 415 | i2c_smbus_write_byte_data(client, TMP401_TEMP_MSB_WRITE[5][0], val); |
| 416 | data->valid = 0; |
| 417 | mutex_unlock(&data->update_lock); |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 418 | |
| 419 | return count; |
| 420 | } |
| 421 | |
Julia Lawall | 5343aed | 2016-12-22 13:05:35 +0100 | [diff] [blame] | 422 | static ssize_t update_interval_show(struct device *dev, |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 423 | struct device_attribute *attr, char *buf) |
| 424 | { |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 425 | struct tmp401_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 426 | |
| 427 | return sprintf(buf, "%u\n", data->update_interval); |
| 428 | } |
| 429 | |
Julia Lawall | 5343aed | 2016-12-22 13:05:35 +0100 | [diff] [blame] | 430 | static ssize_t update_interval_store(struct device *dev, |
| 431 | struct device_attribute *attr, |
| 432 | const char *buf, size_t count) |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 433 | { |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 434 | struct tmp401_data *data = dev_get_drvdata(dev); |
| 435 | struct i2c_client *client = data->client; |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 436 | unsigned long val; |
| 437 | int err, rate; |
| 438 | |
| 439 | err = kstrtoul(buf, 10, &val); |
| 440 | if (err) |
| 441 | return err; |
| 442 | |
| 443 | /* |
| 444 | * For valid rates, interval can be calculated as |
| 445 | * interval = (1 << (7 - rate)) * 125; |
| 446 | * Rounded rate is therefore |
| 447 | * rate = 7 - __fls(interval * 4 / (125 * 3)); |
| 448 | * Use clamp_val() to avoid overflows, and to ensure valid input |
| 449 | * for __fls. |
| 450 | */ |
| 451 | val = clamp_val(val, 125, 16000); |
| 452 | rate = 7 - __fls(val * 4 / (125 * 3)); |
| 453 | mutex_lock(&data->update_lock); |
| 454 | i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, rate); |
| 455 | data->update_interval = (1 << (7 - rate)) * 125; |
| 456 | mutex_unlock(&data->update_lock); |
| 457 | |
| 458 | return count; |
| 459 | } |
| 460 | |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 461 | static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0); |
| 462 | static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 1, 0); |
| 463 | static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 2, 0); |
| 464 | static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 3, 0); |
| 465 | static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_crit_hyst, 0); |
| 466 | static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, status, 1, |
| 467 | TMP432_STATUS_LOCAL); |
| 468 | static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, status, 2, |
| 469 | TMP432_STATUS_LOCAL); |
| 470 | static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, status, 3, |
| 471 | TMP432_STATUS_LOCAL); |
| 472 | static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1); |
| 473 | static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 1, 1); |
| 474 | static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 2, 1); |
| 475 | static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 3, 1); |
| 476 | static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, temp_crit_hyst, 1); |
| 477 | static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, status, 0, TMP432_STATUS_REMOTE1); |
| 478 | static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, status, 1, |
| 479 | TMP432_STATUS_REMOTE1); |
| 480 | static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, status, 2, |
| 481 | TMP432_STATUS_REMOTE1); |
| 482 | static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, status, 3, |
| 483 | TMP432_STATUS_REMOTE1); |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 484 | |
Julia Lawall | 5343aed | 2016-12-22 13:05:35 +0100 | [diff] [blame] | 485 | static DEVICE_ATTR_RW(update_interval); |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 486 | |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 487 | static struct attribute *tmp401_attributes[] = { |
| 488 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 489 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 490 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 491 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
| 492 | &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr, |
| 493 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 494 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 495 | &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr, |
| 496 | |
| 497 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 498 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 499 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 500 | &sensor_dev_attr_temp2_crit.dev_attr.attr, |
| 501 | &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr, |
| 502 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
| 503 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 504 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 505 | &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr, |
| 506 | |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 507 | &dev_attr_update_interval.attr, |
| 508 | |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 509 | NULL |
| 510 | }; |
| 511 | |
| 512 | static const struct attribute_group tmp401_group = { |
| 513 | .attrs = tmp401_attributes, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | /* |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 517 | * Additional features of the TMP411 chip. |
| 518 | * The TMP411 stores the minimum and maximum |
| 519 | * temperature measured since power-on, chip-reset, or |
| 520 | * minimum and maximum register reset for both the local |
| 521 | * and remote channels. |
| 522 | */ |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 523 | static SENSOR_DEVICE_ATTR_2_RO(temp1_lowest, temp, 4, 0); |
| 524 | static SENSOR_DEVICE_ATTR_2_RO(temp1_highest, temp, 5, 0); |
| 525 | static SENSOR_DEVICE_ATTR_2_RO(temp2_lowest, temp, 4, 1); |
| 526 | static SENSOR_DEVICE_ATTR_2_RO(temp2_highest, temp, 5, 1); |
| 527 | static SENSOR_DEVICE_ATTR_WO(temp_reset_history, reset_temp_history, 0); |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 528 | |
| 529 | static struct attribute *tmp411_attributes[] = { |
| 530 | &sensor_dev_attr_temp1_highest.dev_attr.attr, |
| 531 | &sensor_dev_attr_temp1_lowest.dev_attr.attr, |
| 532 | &sensor_dev_attr_temp2_highest.dev_attr.attr, |
| 533 | &sensor_dev_attr_temp2_lowest.dev_attr.attr, |
| 534 | &sensor_dev_attr_temp_reset_history.dev_attr.attr, |
| 535 | NULL |
| 536 | }; |
| 537 | |
| 538 | static const struct attribute_group tmp411_group = { |
| 539 | .attrs = tmp411_attributes, |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 540 | }; |
| 541 | |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 542 | static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0, 2); |
| 543 | static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 1, 2); |
| 544 | static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, 2); |
| 545 | static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 3, 2); |
| 546 | static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, temp_crit_hyst, 2); |
| 547 | static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, status, 0, TMP432_STATUS_REMOTE2); |
| 548 | static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, status, 1, |
| 549 | TMP432_STATUS_REMOTE2); |
| 550 | static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, status, 2, |
| 551 | TMP432_STATUS_REMOTE2); |
| 552 | static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, status, 3, |
| 553 | TMP432_STATUS_REMOTE2); |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 554 | |
| 555 | static struct attribute *tmp432_attributes[] = { |
| 556 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 557 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 558 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 559 | &sensor_dev_attr_temp3_crit.dev_attr.attr, |
| 560 | &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr, |
| 561 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
| 562 | &sensor_dev_attr_temp3_max_alarm.dev_attr.attr, |
| 563 | &sensor_dev_attr_temp3_min_alarm.dev_attr.attr, |
| 564 | &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr, |
| 565 | |
| 566 | NULL |
| 567 | }; |
| 568 | |
| 569 | static const struct attribute_group tmp432_group = { |
| 570 | .attrs = tmp432_attributes, |
| 571 | }; |
| 572 | |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 573 | /* |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 574 | * Additional features of the TMP461 chip. |
| 575 | * The TMP461 temperature offset for the remote channel. |
| 576 | */ |
Guenter Roeck | e36917f | 2018-12-06 10:59:49 -0800 | [diff] [blame] | 577 | static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, 6, 1); |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 578 | |
| 579 | static struct attribute *tmp461_attributes[] = { |
| 580 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 581 | NULL |
| 582 | }; |
| 583 | |
| 584 | static const struct attribute_group tmp461_group = { |
| 585 | .attrs = tmp461_attributes, |
| 586 | }; |
| 587 | |
| 588 | /* |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 589 | * Begin non sysfs callback code (aka Real code) |
| 590 | */ |
| 591 | |
Bartosz Golaszewski | 90652ef | 2014-12-04 17:45:53 +0100 | [diff] [blame] | 592 | static int tmp401_init_client(struct tmp401_data *data, |
| 593 | struct i2c_client *client) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 594 | { |
Bartosz Golaszewski | 90652ef | 2014-12-04 17:45:53 +0100 | [diff] [blame] | 595 | int config, config_orig, status = 0; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 596 | |
| 597 | /* Set the conversion rate to 2 Hz */ |
| 598 | i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5); |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 599 | data->update_interval = 500; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 600 | |
| 601 | /* Start conversions (disable shutdown if necessary) */ |
| 602 | config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); |
Bartosz Golaszewski | 90652ef | 2014-12-04 17:45:53 +0100 | [diff] [blame] | 603 | if (config < 0) |
| 604 | return config; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 605 | |
| 606 | config_orig = config; |
| 607 | config &= ~TMP401_CONFIG_SHUTDOWN; |
| 608 | |
| 609 | if (config != config_orig) |
Bartosz Golaszewski | 90652ef | 2014-12-04 17:45:53 +0100 | [diff] [blame] | 610 | status = i2c_smbus_write_byte_data(client, |
| 611 | TMP401_CONFIG_WRITE, |
| 612 | config); |
| 613 | |
| 614 | return status; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 617 | static int tmp401_detect(struct i2c_client *client, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 618 | struct i2c_board_info *info) |
| 619 | { |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 620 | enum chips kind; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 621 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 622 | u8 reg; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 623 | |
| 624 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 625 | return -ENODEV; |
| 626 | |
| 627 | /* Detect and identify the chip */ |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 628 | reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG); |
| 629 | if (reg != TMP401_MANUFACTURER_ID) |
| 630 | return -ENODEV; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 631 | |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 632 | reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 633 | |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 634 | switch (reg) { |
| 635 | case TMP401_DEVICE_ID: |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 636 | if (client->addr != 0x4c) |
| 637 | return -ENODEV; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 638 | kind = tmp401; |
| 639 | break; |
Guenter Roeck | 4ce5b1f | 2013-03-29 17:56:07 -0700 | [diff] [blame] | 640 | case TMP411A_DEVICE_ID: |
| 641 | if (client->addr != 0x4c) |
| 642 | return -ENODEV; |
| 643 | kind = tmp411; |
| 644 | break; |
| 645 | case TMP411B_DEVICE_ID: |
| 646 | if (client->addr != 0x4d) |
| 647 | return -ENODEV; |
| 648 | kind = tmp411; |
| 649 | break; |
| 650 | case TMP411C_DEVICE_ID: |
| 651 | if (client->addr != 0x4e) |
| 652 | return -ENODEV; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 653 | kind = tmp411; |
| 654 | break; |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 655 | case TMP431_DEVICE_ID: |
Guenter Roeck | 907a6d5 | 2014-12-05 10:15:03 -0800 | [diff] [blame] | 656 | if (client->addr != 0x4c && client->addr != 0x4d) |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 657 | return -ENODEV; |
| 658 | kind = tmp431; |
| 659 | break; |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 660 | case TMP432_DEVICE_ID: |
Guenter Roeck | 907a6d5 | 2014-12-05 10:15:03 -0800 | [diff] [blame] | 661 | if (client->addr != 0x4c && client->addr != 0x4d) |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 662 | return -ENODEV; |
| 663 | kind = tmp432; |
| 664 | break; |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 665 | case TMP435_DEVICE_ID: |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 666 | kind = tmp435; |
| 667 | break; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 668 | default: |
| 669 | return -ENODEV; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 670 | } |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 671 | |
| 672 | reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); |
| 673 | if (reg & 0x1b) |
| 674 | return -ENODEV; |
| 675 | |
| 676 | reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ); |
| 677 | /* Datasheet says: 0x1-0x6 */ |
| 678 | if (reg > 15) |
| 679 | return -ENODEV; |
| 680 | |
Jean Delvare | dc71afe | 2010-03-05 22:17:26 +0100 | [diff] [blame] | 681 | strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 686 | static int tmp401_probe(struct i2c_client *client) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 687 | { |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 688 | static const char * const names[] = { |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 689 | "TMP401", "TMP411", "TMP431", "TMP432", "TMP435", "TMP461" |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 690 | }; |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 691 | struct device *dev = &client->dev; |
| 692 | struct device *hwmon_dev; |
| 693 | struct tmp401_data *data; |
Bartosz Golaszewski | 90652ef | 2014-12-04 17:45:53 +0100 | [diff] [blame] | 694 | int groups = 0, status; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 695 | |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 696 | data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 697 | if (!data) |
| 698 | return -ENOMEM; |
| 699 | |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 700 | data->client = client; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 701 | mutex_init(&data->update_lock); |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 702 | data->kind = i2c_match_id(tmp401_id, client)->driver_data; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 703 | |
| 704 | /* Initialize the TMP401 chip */ |
Bartosz Golaszewski | 90652ef | 2014-12-04 17:45:53 +0100 | [diff] [blame] | 705 | status = tmp401_init_client(data, client); |
| 706 | if (status < 0) |
| 707 | return status; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 708 | |
| 709 | /* Register sysfs hooks */ |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 710 | data->groups[groups++] = &tmp401_group; |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 711 | |
| 712 | /* Register additional tmp411 sysfs hooks */ |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 713 | if (data->kind == tmp411) |
| 714 | data->groups[groups++] = &tmp411_group; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 715 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 716 | /* Register additional tmp432 sysfs hooks */ |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 717 | if (data->kind == tmp432) |
| 718 | data->groups[groups++] = &tmp432_group; |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 719 | |
Andrew F. Davis | c0a68601 | 2016-06-08 12:00:54 -0500 | [diff] [blame] | 720 | if (data->kind == tmp461) |
| 721 | data->groups[groups++] = &tmp461_group; |
| 722 | |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 723 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
| 724 | data, data->groups); |
| 725 | if (IS_ERR(hwmon_dev)) |
| 726 | return PTR_ERR(hwmon_dev); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 727 | |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 728 | dev_info(dev, "Detected TI %s chip\n", names[data->kind]); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 729 | |
| 730 | return 0; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 733 | static struct i2c_driver tmp401_driver = { |
| 734 | .class = I2C_CLASS_HWMON, |
| 735 | .driver = { |
| 736 | .name = "tmp401", |
| 737 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 738 | .probe_new = tmp401_probe, |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 739 | .id_table = tmp401_id, |
| 740 | .detect = tmp401_detect, |
| 741 | .address_list = normal_i2c, |
| 742 | }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 743 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 744 | module_i2c_driver(tmp401_driver); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 745 | |
| 746 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
| 747 | MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver"); |
| 748 | MODULE_LICENSE("GPL"); |