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 | |
Guenter Roeck | 947e927 | 2013-03-27 08:48:03 -0700 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 22 | #include <linux/err.h> |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 23 | #include <linux/i2c.h> |
| 24 | #include <linux/hwmon.h> |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 28 | #include <linux/regmap.h> |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 29 | #include <linux/slab.h> |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 30 | |
| 31 | /* Addresses to scan */ |
Guenter Roeck | 9aecac0 | 2015-05-27 16:11:48 -0700 | [diff] [blame] | 32 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 0x4d, |
Guenter Roeck | 907a6d5 | 2014-12-05 10:15:03 -0800 | [diff] [blame] | 33 | 0x4e, 0x4f, I2C_CLIENT_END }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 34 | |
Guenter Roeck | 38d9f06 | 2021-10-15 17:34:25 -0700 | [diff] [blame] | 35 | enum chips { tmp401, tmp411, tmp431, tmp432, tmp435 }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * The TMP401 registers, note some registers have different addresses for |
| 39 | * reading and writing |
| 40 | */ |
| 41 | #define TMP401_STATUS 0x02 |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 42 | #define TMP401_CONFIG 0x03 |
| 43 | #define TMP401_CONVERSION_RATE 0x04 |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 44 | #define TMP401_TEMP_CRIT_HYST 0x21 |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 45 | #define TMP401_MANUFACTURER_ID_REG 0xFE |
| 46 | #define TMP401_DEVICE_ID_REG 0xFF |
| 47 | |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 48 | static const u8 TMP401_TEMP_MSB[7][3] = { |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 49 | { 0x00, 0x01, 0x23 }, /* temp */ |
| 50 | { 0x06, 0x08, 0x16 }, /* low limit */ |
| 51 | { 0x05, 0x07, 0x15 }, /* high limit */ |
Guenter Roeck | bcb31e6 | 2021-10-17 07:47:43 -0700 | [diff] [blame] | 52 | { 0x20, 0x19, 0x1a }, /* therm (crit) limit */ |
| 53 | { 0x30, 0x34, 0x00 }, /* lowest */ |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 54 | { 0x32, 0xf6, 0x00 }, /* highest */ |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 57 | /* [0] = fault, [1] = low, [2] = high, [3] = therm/crit */ |
| 58 | static const u8 TMP432_STATUS_REG[] = { |
| 59 | 0x1b, 0x36, 0x35, 0x37 }; |
| 60 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 61 | /* Flags */ |
Guenter Roeck | 947e927 | 2013-03-27 08:48:03 -0700 | [diff] [blame] | 62 | #define TMP401_CONFIG_RANGE BIT(2) |
| 63 | #define TMP401_CONFIG_SHUTDOWN BIT(6) |
| 64 | #define TMP401_STATUS_LOCAL_CRIT BIT(0) |
| 65 | #define TMP401_STATUS_REMOTE_CRIT BIT(1) |
| 66 | #define TMP401_STATUS_REMOTE_OPEN BIT(2) |
| 67 | #define TMP401_STATUS_REMOTE_LOW BIT(3) |
| 68 | #define TMP401_STATUS_REMOTE_HIGH BIT(4) |
| 69 | #define TMP401_STATUS_LOCAL_LOW BIT(5) |
| 70 | #define TMP401_STATUS_LOCAL_HIGH BIT(6) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 71 | |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 72 | /* On TMP432, each status has its own register */ |
| 73 | #define TMP432_STATUS_LOCAL BIT(0) |
| 74 | #define TMP432_STATUS_REMOTE1 BIT(1) |
| 75 | #define TMP432_STATUS_REMOTE2 BIT(2) |
| 76 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 77 | /* Manufacturer / Device ID's */ |
| 78 | #define TMP401_MANUFACTURER_ID 0x55 |
| 79 | #define TMP401_DEVICE_ID 0x11 |
Guenter Roeck | 4ce5b1f | 2013-03-29 17:56:07 -0700 | [diff] [blame] | 80 | #define TMP411A_DEVICE_ID 0x12 |
| 81 | #define TMP411B_DEVICE_ID 0x13 |
| 82 | #define TMP411C_DEVICE_ID 0x10 |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 83 | #define TMP431_DEVICE_ID 0x31 |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 84 | #define TMP432_DEVICE_ID 0x32 |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 85 | #define TMP435_DEVICE_ID 0x35 |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 86 | |
| 87 | /* |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 88 | * Driver data (common to all clients) |
| 89 | */ |
| 90 | |
| 91 | static const struct i2c_device_id tmp401_id[] = { |
| 92 | { "tmp401", tmp401 }, |
Andre Prendel | fce0758f | 2009-06-15 18:39:47 +0200 | [diff] [blame] | 93 | { "tmp411", tmp411 }, |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 94 | { "tmp431", tmp431 }, |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 95 | { "tmp432", tmp432 }, |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 96 | { "tmp435", tmp435 }, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 97 | { } |
| 98 | }; |
| 99 | MODULE_DEVICE_TABLE(i2c, tmp401_id); |
| 100 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 101 | /* |
| 102 | * Client data (each client gets its own) |
| 103 | */ |
| 104 | |
| 105 | struct tmp401_data { |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 106 | struct i2c_client *client; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 107 | struct regmap *regmap; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 108 | struct mutex update_lock; |
Jean Delvare | dc71afe | 2010-03-05 22:17:26 +0100 | [diff] [blame] | 109 | enum chips kind; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 110 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 111 | bool extended_range; |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 112 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 113 | /* hwmon API configuration data */ |
| 114 | u32 chip_channel_config[4]; |
| 115 | struct hwmon_channel_info chip_info; |
| 116 | u32 temp_channel_config[4]; |
| 117 | struct hwmon_channel_info temp_info; |
| 118 | const struct hwmon_channel_info *info[3]; |
| 119 | struct hwmon_chip_info chip; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 122 | /* regmap */ |
| 123 | |
| 124 | static bool tmp401_regmap_is_volatile(struct device *dev, unsigned int reg) |
| 125 | { |
| 126 | switch (reg) { |
| 127 | case 0: /* local temp msb */ |
| 128 | case 1: /* remote temp msb */ |
| 129 | case 2: /* status */ |
| 130 | case 0x10: /* remote temp lsb */ |
| 131 | case 0x15: /* local temp lsb */ |
| 132 | case 0x1b: /* status (tmp432) */ |
| 133 | case 0x23 ... 0x24: /* remote temp 2 msb / lsb */ |
| 134 | case 0x30 ... 0x37: /* lowest/highest temp; status (tmp432) */ |
| 135 | return true; |
| 136 | default: |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static int tmp401_reg_read(void *context, unsigned int reg, unsigned int *val) |
| 142 | { |
| 143 | struct tmp401_data *data = context; |
| 144 | struct i2c_client *client = data->client; |
| 145 | int regval; |
| 146 | |
| 147 | switch (reg) { |
| 148 | case 0: /* local temp msb */ |
| 149 | case 1: /* remote temp msb */ |
| 150 | case 5: /* local temp high limit msb */ |
| 151 | case 6: /* local temp low limit msb */ |
| 152 | case 7: /* remote temp ligh limit msb */ |
| 153 | case 8: /* remote temp low limit msb */ |
| 154 | case 0x15: /* remote temp 2 high limit msb */ |
| 155 | case 0x16: /* remote temp 2 low limit msb */ |
| 156 | case 0x23: /* remote temp 2 msb */ |
| 157 | case 0x30: /* local temp minimum, tmp411 */ |
| 158 | case 0x32: /* local temp maximum, tmp411 */ |
| 159 | case 0x34: /* remote temp minimum, tmp411 */ |
| 160 | case 0xf6: /* remote temp maximum, tmp411 (really 0x36) */ |
| 161 | /* work around register overlap between TMP411 and TMP432 */ |
| 162 | if (reg == 0xf6) |
| 163 | reg = 0x36; |
| 164 | regval = i2c_smbus_read_word_swapped(client, reg); |
| 165 | if (regval < 0) |
| 166 | return regval; |
| 167 | *val = regval; |
| 168 | break; |
| 169 | case 0x19: /* critical limits, 8-bit registers */ |
| 170 | case 0x1a: |
| 171 | case 0x20: |
| 172 | regval = i2c_smbus_read_byte_data(client, reg); |
| 173 | if (regval < 0) |
| 174 | return regval; |
| 175 | *val = regval << 8; |
| 176 | break; |
| 177 | case 0x1b: |
| 178 | case 0x35 ... 0x37: |
| 179 | if (data->kind == tmp432) { |
| 180 | regval = i2c_smbus_read_byte_data(client, reg); |
| 181 | if (regval < 0) |
| 182 | return regval; |
| 183 | *val = regval; |
| 184 | break; |
| 185 | } |
| 186 | /* simulate TMP432 status registers */ |
| 187 | regval = i2c_smbus_read_byte_data(client, TMP401_STATUS); |
| 188 | if (regval < 0) |
| 189 | return regval; |
| 190 | *val = 0; |
| 191 | switch (reg) { |
| 192 | case 0x1b: /* open / fault */ |
| 193 | if (regval & TMP401_STATUS_REMOTE_OPEN) |
| 194 | *val |= BIT(1); |
| 195 | break; |
| 196 | case 0x35: /* high limit */ |
| 197 | if (regval & TMP401_STATUS_LOCAL_HIGH) |
| 198 | *val |= BIT(0); |
| 199 | if (regval & TMP401_STATUS_REMOTE_HIGH) |
| 200 | *val |= BIT(1); |
| 201 | break; |
| 202 | case 0x36: /* low limit */ |
| 203 | if (regval & TMP401_STATUS_LOCAL_LOW) |
| 204 | *val |= BIT(0); |
| 205 | if (regval & TMP401_STATUS_REMOTE_LOW) |
| 206 | *val |= BIT(1); |
| 207 | break; |
| 208 | case 0x37: /* therm / crit limit */ |
| 209 | if (regval & TMP401_STATUS_LOCAL_CRIT) |
| 210 | *val |= BIT(0); |
| 211 | if (regval & TMP401_STATUS_REMOTE_CRIT) |
| 212 | *val |= BIT(1); |
| 213 | break; |
| 214 | } |
| 215 | break; |
| 216 | default: |
| 217 | regval = i2c_smbus_read_byte_data(client, reg); |
| 218 | if (regval < 0) |
| 219 | return regval; |
| 220 | *val = regval; |
| 221 | break; |
| 222 | } |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int tmp401_reg_write(void *context, unsigned int reg, unsigned int val) |
| 227 | { |
| 228 | struct tmp401_data *data = context; |
| 229 | struct i2c_client *client = data->client; |
| 230 | |
| 231 | switch (reg) { |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 232 | case 0x05: /* local temp high limit msb */ |
| 233 | case 0x06: /* local temp low limit msb */ |
| 234 | case 0x07: /* remote temp ligh limit msb */ |
| 235 | case 0x08: /* remote temp low limit msb */ |
| 236 | reg += 6; /* adjust for register write address */ |
| 237 | fallthrough; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 238 | case 0x15: /* remote temp 2 high limit msb */ |
| 239 | case 0x16: /* remote temp 2 low limit msb */ |
| 240 | return i2c_smbus_write_word_swapped(client, reg, val); |
| 241 | case 0x19: /* critical limits, 8-bit registers */ |
| 242 | case 0x1a: |
| 243 | case 0x20: |
| 244 | return i2c_smbus_write_byte_data(client, reg, val >> 8); |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 245 | case TMP401_CONVERSION_RATE: |
| 246 | case TMP401_CONFIG: |
| 247 | reg += 6; /* adjust for register write address */ |
| 248 | fallthrough; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 249 | default: |
| 250 | return i2c_smbus_write_byte_data(client, reg, val); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static const struct regmap_config tmp401_regmap_config = { |
| 255 | .reg_bits = 8, |
| 256 | .val_bits = 16, |
| 257 | .cache_type = REGCACHE_RBTREE, |
| 258 | .volatile_reg = tmp401_regmap_is_volatile, |
| 259 | .reg_read = tmp401_reg_read, |
| 260 | .reg_write = tmp401_reg_write, |
| 261 | }; |
| 262 | |
| 263 | /* temperature conversion */ |
| 264 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 265 | static int tmp401_register_to_temp(u16 reg, bool extended) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 266 | { |
| 267 | int temp = reg; |
| 268 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 269 | if (extended) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 270 | temp -= 64 * 256; |
| 271 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 272 | return DIV_ROUND_CLOSEST(temp * 125, 32); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 275 | static u16 tmp401_temp_to_register(long temp, bool extended, int zbits) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 276 | { |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 277 | if (extended) { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 278 | temp = clamp_val(temp, -64000, 191000); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 279 | temp += 64000; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 280 | } else { |
Guenter Roeck | 2a844c1 | 2013-01-09 08:09:34 -0800 | [diff] [blame] | 281 | temp = clamp_val(temp, 0, 127000); |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 282 | } |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 283 | |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 284 | return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 287 | /* hwmon API functions */ |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 288 | |
| 289 | static const u8 tmp401_temp_reg_index[] = { |
| 290 | [hwmon_temp_input] = 0, |
| 291 | [hwmon_temp_min] = 1, |
| 292 | [hwmon_temp_max] = 2, |
| 293 | [hwmon_temp_crit] = 3, |
| 294 | [hwmon_temp_lowest] = 4, |
| 295 | [hwmon_temp_highest] = 5, |
| 296 | }; |
| 297 | |
| 298 | static const u8 tmp401_status_reg_index[] = { |
| 299 | [hwmon_temp_fault] = 0, |
| 300 | [hwmon_temp_min_alarm] = 1, |
| 301 | [hwmon_temp_max_alarm] = 2, |
| 302 | [hwmon_temp_crit_alarm] = 3, |
| 303 | }; |
| 304 | |
| 305 | static int tmp401_temp_read(struct device *dev, u32 attr, int channel, long *val) |
| 306 | { |
| 307 | struct tmp401_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 308 | struct regmap *regmap = data->regmap; |
| 309 | unsigned int regval; |
| 310 | int reg, ret; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 311 | |
| 312 | switch (attr) { |
| 313 | case hwmon_temp_input: |
| 314 | case hwmon_temp_min: |
| 315 | case hwmon_temp_max: |
| 316 | case hwmon_temp_crit: |
| 317 | case hwmon_temp_lowest: |
| 318 | case hwmon_temp_highest: |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 319 | reg = TMP401_TEMP_MSB[tmp401_temp_reg_index[attr]][channel]; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 320 | ret = regmap_read(regmap, reg, ®val); |
| 321 | if (ret < 0) |
| 322 | return ret; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 323 | *val = tmp401_register_to_temp(regval, data->extended_range); |
| 324 | break; |
| 325 | case hwmon_temp_crit_hyst: |
| 326 | mutex_lock(&data->update_lock); |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 327 | reg = TMP401_TEMP_MSB[3][channel]; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 328 | ret = regmap_read(regmap, reg, ®val); |
| 329 | if (ret < 0) |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 330 | goto unlock; |
| 331 | *val = tmp401_register_to_temp(regval, data->extended_range); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 332 | ret = regmap_read(regmap, TMP401_TEMP_CRIT_HYST, ®val); |
| 333 | if (ret < 0) |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 334 | goto unlock; |
| 335 | *val -= regval * 1000; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 336 | unlock: |
| 337 | mutex_unlock(&data->update_lock); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 338 | if (ret < 0) |
| 339 | return ret; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 340 | break; |
| 341 | case hwmon_temp_fault: |
| 342 | case hwmon_temp_min_alarm: |
| 343 | case hwmon_temp_max_alarm: |
| 344 | case hwmon_temp_crit_alarm: |
| 345 | reg = TMP432_STATUS_REG[tmp401_status_reg_index[attr]]; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 346 | ret = regmap_read(regmap, reg, ®val); |
| 347 | if (ret < 0) |
| 348 | return ret; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 349 | *val = !!(regval & BIT(channel)); |
| 350 | break; |
| 351 | default: |
| 352 | return -EOPNOTSUPP; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 353 | } |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 354 | return 0; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 355 | } |
| 356 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 357 | static int tmp401_temp_write(struct device *dev, u32 attr, int channel, |
| 358 | long val) |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 359 | { |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 360 | struct tmp401_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 361 | struct regmap *regmap = data->regmap; |
| 362 | unsigned int regval; |
| 363 | int reg, ret, temp; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 364 | |
| 365 | mutex_lock(&data->update_lock); |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 366 | switch (attr) { |
| 367 | case hwmon_temp_min: |
| 368 | case hwmon_temp_max: |
| 369 | case hwmon_temp_crit: |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 370 | reg = TMP401_TEMP_MSB[tmp401_temp_reg_index[attr]][channel]; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 371 | regval = tmp401_temp_to_register(val, data->extended_range, |
| 372 | attr == hwmon_temp_crit ? 8 : 4); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 373 | ret = regmap_write(regmap, reg, regval); |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 374 | break; |
| 375 | case hwmon_temp_crit_hyst: |
| 376 | if (data->extended_range) |
| 377 | val = clamp_val(val, -64000, 191000); |
| 378 | else |
| 379 | val = clamp_val(val, 0, 127000); |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 380 | |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 381 | reg = TMP401_TEMP_MSB[3][channel]; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 382 | ret = regmap_read(regmap, reg, ®val); |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 383 | if (ret < 0) |
| 384 | break; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 385 | temp = tmp401_register_to_temp(regval, data->extended_range); |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 386 | val = clamp_val(val, temp - 255000, temp); |
| 387 | regval = ((temp - val) + 500) / 1000; |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 388 | ret = regmap_write(regmap, TMP401_TEMP_CRIT_HYST, regval); |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 389 | break; |
| 390 | default: |
| 391 | ret = -EOPNOTSUPP; |
| 392 | break; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 393 | } |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 394 | mutex_unlock(&data->update_lock); |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 395 | return ret; |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 398 | static int tmp401_chip_read(struct device *dev, u32 attr, int channel, long *val) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 399 | { |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 400 | struct tmp401_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 401 | u32 regval; |
| 402 | int ret; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 403 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 404 | switch (attr) { |
| 405 | case hwmon_chip_update_interval: |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 406 | ret = regmap_read(data->regmap, TMP401_CONVERSION_RATE, ®val); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 407 | if (ret < 0) |
| 408 | return ret; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 409 | *val = (1 << (7 - regval)) * 125; |
| 410 | break; |
| 411 | case hwmon_chip_temp_reset_history: |
| 412 | *val = 0; |
| 413 | break; |
| 414 | default: |
| 415 | return -EOPNOTSUPP; |
Guenter Roeck | 14f2a66 | 2013-03-27 21:23:10 -0700 | [diff] [blame] | 416 | } |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 417 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 418 | return 0; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 421 | static int tmp401_set_convrate(struct regmap *regmap, long val) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 422 | { |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 423 | int rate; |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 424 | |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 425 | /* |
| 426 | * For valid rates, interval can be calculated as |
| 427 | * interval = (1 << (7 - rate)) * 125; |
| 428 | * Rounded rate is therefore |
| 429 | * rate = 7 - __fls(interval * 4 / (125 * 3)); |
| 430 | * Use clamp_val() to avoid overflows, and to ensure valid input |
| 431 | * for __fls. |
| 432 | */ |
| 433 | val = clamp_val(val, 125, 16000); |
| 434 | rate = 7 - __fls(val * 4 / (125 * 3)); |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 435 | return regmap_write(regmap, TMP401_CONVERSION_RATE, rate); |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 438 | static int tmp401_chip_write(struct device *dev, u32 attr, int channel, long val) |
| 439 | { |
| 440 | struct tmp401_data *data = dev_get_drvdata(dev); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 441 | struct regmap *regmap = data->regmap; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 442 | int err; |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 443 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 444 | mutex_lock(&data->update_lock); |
| 445 | switch (attr) { |
| 446 | case hwmon_chip_update_interval: |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 447 | err = tmp401_set_convrate(regmap, val); |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 448 | break; |
| 449 | case hwmon_chip_temp_reset_history: |
| 450 | if (val != 1) { |
| 451 | err = -EINVAL; |
| 452 | break; |
| 453 | } |
| 454 | /* |
| 455 | * Reset history by writing any value to any of the |
| 456 | * minimum/maximum registers (0x30-0x37). |
| 457 | */ |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 458 | err = regmap_write(regmap, 0x30, 0); |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 459 | break; |
| 460 | default: |
| 461 | err = -EOPNOTSUPP; |
| 462 | break; |
| 463 | } |
| 464 | mutex_unlock(&data->update_lock); |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 465 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 466 | return err; |
| 467 | } |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 468 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 469 | static int tmp401_read(struct device *dev, enum hwmon_sensor_types type, |
| 470 | u32 attr, int channel, long *val) |
| 471 | { |
| 472 | switch (type) { |
| 473 | case hwmon_chip: |
| 474 | return tmp401_chip_read(dev, attr, channel, val); |
| 475 | case hwmon_temp: |
| 476 | return tmp401_temp_read(dev, attr, channel, val); |
| 477 | default: |
| 478 | return -EOPNOTSUPP; |
| 479 | } |
| 480 | } |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 481 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 482 | static int tmp401_write(struct device *dev, enum hwmon_sensor_types type, |
| 483 | u32 attr, int channel, long val) |
| 484 | { |
| 485 | switch (type) { |
| 486 | case hwmon_chip: |
| 487 | return tmp401_chip_write(dev, attr, channel, val); |
| 488 | case hwmon_temp: |
| 489 | return tmp401_temp_write(dev, attr, channel, val); |
| 490 | default: |
| 491 | return -EOPNOTSUPP; |
| 492 | } |
| 493 | } |
Guenter Roeck | 0846e30 | 2013-03-28 02:03:10 -0700 | [diff] [blame] | 494 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 495 | static umode_t tmp401_is_visible(const void *data, enum hwmon_sensor_types type, |
| 496 | u32 attr, int channel) |
| 497 | { |
| 498 | switch (type) { |
| 499 | case hwmon_chip: |
| 500 | switch (attr) { |
| 501 | case hwmon_chip_update_interval: |
| 502 | case hwmon_chip_temp_reset_history: |
| 503 | return 0644; |
| 504 | default: |
| 505 | break; |
| 506 | } |
| 507 | break; |
| 508 | case hwmon_temp: |
| 509 | switch (attr) { |
| 510 | case hwmon_temp_input: |
| 511 | case hwmon_temp_min_alarm: |
| 512 | case hwmon_temp_max_alarm: |
| 513 | case hwmon_temp_crit_alarm: |
| 514 | case hwmon_temp_fault: |
| 515 | case hwmon_temp_lowest: |
| 516 | case hwmon_temp_highest: |
| 517 | return 0444; |
| 518 | case hwmon_temp_min: |
| 519 | case hwmon_temp_max: |
| 520 | case hwmon_temp_crit: |
| 521 | case hwmon_temp_crit_hyst: |
| 522 | return 0644; |
| 523 | default: |
| 524 | break; |
| 525 | } |
| 526 | break; |
| 527 | default: |
| 528 | break; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static const struct hwmon_ops tmp401_ops = { |
| 534 | .is_visible = tmp401_is_visible, |
| 535 | .read = tmp401_read, |
| 536 | .write = tmp401_write, |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 537 | }; |
| 538 | |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 539 | /* chip initialization, detect, probe */ |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 540 | |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 541 | static int tmp401_init_client(struct tmp401_data *data) |
| 542 | { |
| 543 | struct regmap *regmap = data->regmap; |
| 544 | u32 config, config_orig; |
| 545 | int ret; |
| 546 | |
| 547 | /* Set conversion rate to 2 Hz */ |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 548 | ret = regmap_write(regmap, TMP401_CONVERSION_RATE, 5); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 549 | if (ret < 0) |
| 550 | return ret; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 551 | |
| 552 | /* Start conversions (disable shutdown if necessary) */ |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 553 | ret = regmap_read(regmap, TMP401_CONFIG, &config); |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 554 | if (ret < 0) |
| 555 | return ret; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 556 | |
| 557 | config_orig = config; |
| 558 | config &= ~TMP401_CONFIG_SHUTDOWN; |
| 559 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 560 | data->extended_range = !!(config & TMP401_CONFIG_RANGE); |
| 561 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 562 | if (config != config_orig) |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 563 | ret = regmap_write(regmap, TMP401_CONFIG, config); |
Bartosz Golaszewski | 90652ef | 2014-12-04 17:45:53 +0100 | [diff] [blame] | 564 | |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 565 | return ret; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Jean Delvare | 310ec79 | 2009-12-14 21:17:23 +0100 | [diff] [blame] | 568 | static int tmp401_detect(struct i2c_client *client, |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 569 | struct i2c_board_info *info) |
| 570 | { |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 571 | enum chips kind; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 572 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 573 | u8 reg; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 574 | |
| 575 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
| 576 | return -ENODEV; |
| 577 | |
| 578 | /* Detect and identify the chip */ |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 579 | reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG); |
| 580 | if (reg != TMP401_MANUFACTURER_ID) |
| 581 | return -ENODEV; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 582 | |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 583 | reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 584 | |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 585 | switch (reg) { |
| 586 | case TMP401_DEVICE_ID: |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 587 | if (client->addr != 0x4c) |
| 588 | return -ENODEV; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 589 | kind = tmp401; |
| 590 | break; |
Guenter Roeck | 4ce5b1f | 2013-03-29 17:56:07 -0700 | [diff] [blame] | 591 | case TMP411A_DEVICE_ID: |
| 592 | if (client->addr != 0x4c) |
| 593 | return -ENODEV; |
| 594 | kind = tmp411; |
| 595 | break; |
| 596 | case TMP411B_DEVICE_ID: |
| 597 | if (client->addr != 0x4d) |
| 598 | return -ENODEV; |
| 599 | kind = tmp411; |
| 600 | break; |
| 601 | case TMP411C_DEVICE_ID: |
| 602 | if (client->addr != 0x4e) |
| 603 | return -ENODEV; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 604 | kind = tmp411; |
| 605 | break; |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 606 | case TMP431_DEVICE_ID: |
Guenter Roeck | 907a6d5 | 2014-12-05 10:15:03 -0800 | [diff] [blame] | 607 | if (client->addr != 0x4c && client->addr != 0x4d) |
Guenter Roeck | a1fac92 | 2013-03-15 12:55:08 -0700 | [diff] [blame] | 608 | return -ENODEV; |
| 609 | kind = tmp431; |
| 610 | break; |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 611 | case TMP432_DEVICE_ID: |
Guenter Roeck | 907a6d5 | 2014-12-05 10:15:03 -0800 | [diff] [blame] | 612 | if (client->addr != 0x4c && client->addr != 0x4d) |
Guenter Roeck | 29dd3b6 | 2013-03-28 01:36:44 -0700 | [diff] [blame] | 613 | return -ENODEV; |
| 614 | kind = tmp432; |
| 615 | break; |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 616 | case TMP435_DEVICE_ID: |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 617 | kind = tmp435; |
| 618 | break; |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 619 | default: |
| 620 | return -ENODEV; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 621 | } |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 622 | |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 623 | reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG); |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 624 | if (reg & 0x1b) |
| 625 | return -ENODEV; |
| 626 | |
Guenter Roeck | ff300b7 | 2021-11-09 13:18:26 -0800 | [diff] [blame] | 627 | reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE); |
Jean Delvare | dbe73c8 | 2009-12-09 20:35:54 +0100 | [diff] [blame] | 628 | /* Datasheet says: 0x1-0x6 */ |
| 629 | if (reg > 15) |
| 630 | return -ENODEV; |
| 631 | |
Jean Delvare | dc71afe | 2010-03-05 22:17:26 +0100 | [diff] [blame] | 632 | strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 637 | static int tmp401_probe(struct i2c_client *client) |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 638 | { |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 639 | static const char * const names[] = { |
Guenter Roeck | 38d9f06 | 2021-10-15 17:34:25 -0700 | [diff] [blame] | 640 | "TMP401", "TMP411", "TMP431", "TMP432", "TMP435" |
Patrick Titiano | 06adbae | 2014-12-04 17:45:51 +0100 | [diff] [blame] | 641 | }; |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 642 | struct device *dev = &client->dev; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 643 | struct hwmon_channel_info *info; |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 644 | struct device *hwmon_dev; |
| 645 | struct tmp401_data *data; |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 646 | int status; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 647 | |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 648 | data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 649 | if (!data) |
| 650 | return -ENOMEM; |
| 651 | |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 652 | data->client = client; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 653 | mutex_init(&data->update_lock); |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 654 | data->kind = i2c_match_id(tmp401_id, client)->driver_data; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 655 | |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 656 | data->regmap = devm_regmap_init(dev, NULL, data, &tmp401_regmap_config); |
| 657 | if (IS_ERR(data->regmap)) |
| 658 | return PTR_ERR(data->regmap); |
| 659 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 660 | /* initialize configuration data */ |
| 661 | data->chip.ops = &tmp401_ops; |
| 662 | data->chip.info = data->info; |
| 663 | |
| 664 | data->info[0] = &data->chip_info; |
| 665 | data->info[1] = &data->temp_info; |
| 666 | |
| 667 | info = &data->chip_info; |
| 668 | info->type = hwmon_chip; |
| 669 | info->config = data->chip_channel_config; |
| 670 | |
| 671 | data->chip_channel_config[0] = HWMON_C_UPDATE_INTERVAL; |
| 672 | |
| 673 | info = &data->temp_info; |
| 674 | info->type = hwmon_temp; |
| 675 | info->config = data->temp_channel_config; |
| 676 | |
| 677 | data->temp_channel_config[0] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | |
| 678 | HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM | |
| 679 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM; |
| 680 | data->temp_channel_config[1] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | |
| 681 | HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM | |
| 682 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT; |
| 683 | |
| 684 | if (data->kind == tmp411) { |
| 685 | data->temp_channel_config[0] |= HWMON_T_HIGHEST | HWMON_T_LOWEST; |
| 686 | data->temp_channel_config[1] |= HWMON_T_HIGHEST | HWMON_T_LOWEST; |
| 687 | data->chip_channel_config[0] |= HWMON_C_TEMP_RESET_HISTORY; |
| 688 | } |
| 689 | |
| 690 | if (data->kind == tmp432) { |
| 691 | data->temp_channel_config[2] = HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | |
| 692 | HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_MIN_ALARM | |
| 693 | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM | HWMON_T_FAULT; |
| 694 | } |
| 695 | |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 696 | /* Initialize the TMP401 chip */ |
Guenter Roeck | 50152fb | 2021-10-16 14:56:29 -0700 | [diff] [blame] | 697 | status = tmp401_init_client(data); |
Bartosz Golaszewski | 90652ef | 2014-12-04 17:45:53 +0100 | [diff] [blame] | 698 | if (status < 0) |
| 699 | return status; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 700 | |
Guenter Roeck | ca53e76 | 2021-10-16 10:51:27 -0700 | [diff] [blame] | 701 | hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data, |
| 702 | &data->chip, NULL); |
Guenter Roeck | f3643ac | 2013-09-04 16:24:09 -0700 | [diff] [blame] | 703 | if (IS_ERR(hwmon_dev)) |
| 704 | return PTR_ERR(hwmon_dev); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 705 | |
Guenter Roeck | b4e665c | 2013-03-27 08:58:46 -0700 | [diff] [blame] | 706 | dev_info(dev, "Detected TI %s chip\n", names[data->kind]); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 707 | |
| 708 | return 0; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 709 | } |
| 710 | |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 711 | static struct i2c_driver tmp401_driver = { |
| 712 | .class = I2C_CLASS_HWMON, |
| 713 | .driver = { |
| 714 | .name = "tmp401", |
| 715 | }, |
Stephen Kitt | 6748703 | 2020-08-13 18:02:22 +0200 | [diff] [blame] | 716 | .probe_new = tmp401_probe, |
Andre Prendel | ea63c2b | 2010-05-27 19:58:49 +0200 | [diff] [blame] | 717 | .id_table = tmp401_id, |
| 718 | .detect = tmp401_detect, |
| 719 | .address_list = normal_i2c, |
| 720 | }; |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 721 | |
Axel Lin | f0967ee | 2012-01-20 15:38:18 +0800 | [diff] [blame] | 722 | module_i2c_driver(tmp401_driver); |
Hans de Goede | ab2b79d | 2009-06-15 18:39:46 +0200 | [diff] [blame] | 723 | |
| 724 | MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>"); |
| 725 | MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver"); |
| 726 | MODULE_LICENSE("GPL"); |