blob: b86d9df7105d107878f3618b5c7122452b97b6da [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans de Goedeab2b79d2009-06-15 18:39:46 +02002/* tmp401.c
3 *
4 * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
Andre Prendelfce0758f2009-06-15 18:39:47 +02005 * Preliminary tmp411 support by:
6 * Gabriel Konat, Sander Leget, Wouter Willems
7 * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
Hans de Goedeab2b79d2009-06-15 18:39:46 +02008 *
Guenter Roeck29dd3b62013-03-28 01:36:44 -07009 * Cleanup and support for TMP431 and TMP432 by Guenter Roeck
10 * Copyright (c) 2013 Guenter Roeck <linux@roeck-us.net>
Hans de Goedeab2b79d2009-06-15 18:39:46 +020011 */
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 Roeck947e9272013-03-27 08:48:03 -070021#include <linux/bitops.h>
Guenter Roeckca53e762021-10-16 10:51:27 -070022#include <linux/err.h>
Hans de Goedeab2b79d2009-06-15 18:39:46 +020023#include <linux/i2c.h>
24#include <linux/hwmon.h>
Guenter Roeckca53e762021-10-16 10:51:27 -070025#include <linux/init.h>
26#include <linux/module.h>
Hans de Goedeab2b79d2009-06-15 18:39:46 +020027#include <linux/mutex.h>
Guenter Roeck50152fb2021-10-16 14:56:29 -070028#include <linux/regmap.h>
Guenter Roeckca53e762021-10-16 10:51:27 -070029#include <linux/slab.h>
Hans de Goedeab2b79d2009-06-15 18:39:46 +020030
31/* Addresses to scan */
Guenter Roeck9aecac02015-05-27 16:11:48 -070032static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 0x4d,
Guenter Roeck907a6d52014-12-05 10:15:03 -080033 0x4e, 0x4f, I2C_CLIENT_END };
Hans de Goedeab2b79d2009-06-15 18:39:46 +020034
Guenter Roeck38d9f062021-10-15 17:34:25 -070035enum chips { tmp401, tmp411, tmp431, tmp432, tmp435 };
Hans de Goedeab2b79d2009-06-15 18:39:46 +020036
37/*
38 * The TMP401 registers, note some registers have different addresses for
39 * reading and writing
40 */
41#define TMP401_STATUS 0x02
Guenter Roeckff300b72021-11-09 13:18:26 -080042#define TMP401_CONFIG 0x03
43#define TMP401_CONVERSION_RATE 0x04
Hans de Goedeab2b79d2009-06-15 18:39:46 +020044#define TMP401_TEMP_CRIT_HYST 0x21
Hans de Goedeab2b79d2009-06-15 18:39:46 +020045#define TMP401_MANUFACTURER_ID_REG 0xFE
46#define TMP401_DEVICE_ID_REG 0xFF
47
Guenter Roeckff300b72021-11-09 13:18:26 -080048static const u8 TMP401_TEMP_MSB[7][3] = {
Guenter Roeck29dd3b62013-03-28 01:36:44 -070049 { 0x00, 0x01, 0x23 }, /* temp */
50 { 0x06, 0x08, 0x16 }, /* low limit */
51 { 0x05, 0x07, 0x15 }, /* high limit */
Guenter Roeckbcb31e62021-10-17 07:47:43 -070052 { 0x20, 0x19, 0x1a }, /* therm (crit) limit */
53 { 0x30, 0x34, 0x00 }, /* lowest */
Guenter Roeckca53e762021-10-16 10:51:27 -070054 { 0x32, 0xf6, 0x00 }, /* highest */
Guenter Roeck29dd3b62013-03-28 01:36:44 -070055};
56
Guenter Roeck29dd3b62013-03-28 01:36:44 -070057/* [0] = fault, [1] = low, [2] = high, [3] = therm/crit */
58static const u8 TMP432_STATUS_REG[] = {
59 0x1b, 0x36, 0x35, 0x37 };
60
Hans de Goedeab2b79d2009-06-15 18:39:46 +020061/* Flags */
Guenter Roeck947e9272013-03-27 08:48:03 -070062#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 Goedeab2b79d2009-06-15 18:39:46 +020071
Guenter Roeck29dd3b62013-03-28 01:36:44 -070072/* 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 Goedeab2b79d2009-06-15 18:39:46 +020077/* Manufacturer / Device ID's */
78#define TMP401_MANUFACTURER_ID 0x55
79#define TMP401_DEVICE_ID 0x11
Guenter Roeck4ce5b1f2013-03-29 17:56:07 -070080#define TMP411A_DEVICE_ID 0x12
81#define TMP411B_DEVICE_ID 0x13
82#define TMP411C_DEVICE_ID 0x10
Guenter Roecka1fac922013-03-15 12:55:08 -070083#define TMP431_DEVICE_ID 0x31
Guenter Roeck29dd3b62013-03-28 01:36:44 -070084#define TMP432_DEVICE_ID 0x32
Patrick Titiano06adbae2014-12-04 17:45:51 +010085#define TMP435_DEVICE_ID 0x35
Hans de Goedeab2b79d2009-06-15 18:39:46 +020086
87/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +020088 * Driver data (common to all clients)
89 */
90
91static const struct i2c_device_id tmp401_id[] = {
92 { "tmp401", tmp401 },
Andre Prendelfce0758f2009-06-15 18:39:47 +020093 { "tmp411", tmp411 },
Guenter Roecka1fac922013-03-15 12:55:08 -070094 { "tmp431", tmp431 },
Guenter Roeck29dd3b62013-03-28 01:36:44 -070095 { "tmp432", tmp432 },
Patrick Titiano06adbae2014-12-04 17:45:51 +010096 { "tmp435", tmp435 },
Hans de Goedeab2b79d2009-06-15 18:39:46 +020097 { }
98};
99MODULE_DEVICE_TABLE(i2c, tmp401_id);
100
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200101/*
102 * Client data (each client gets its own)
103 */
104
105struct tmp401_data {
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700106 struct i2c_client *client;
Guenter Roeck50152fb2021-10-16 14:56:29 -0700107 struct regmap *regmap;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200108 struct mutex update_lock;
Jean Delvaredc71afe2010-03-05 22:17:26 +0100109 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200110
Guenter Roeckca53e762021-10-16 10:51:27 -0700111 bool extended_range;
Guenter Roeck0846e302013-03-28 02:03:10 -0700112
Guenter Roeckca53e762021-10-16 10:51:27 -0700113 /* 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 Goedeab2b79d2009-06-15 18:39:46 +0200120};
121
Guenter Roeck50152fb2021-10-16 14:56:29 -0700122/* regmap */
123
124static 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
141static 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
226static 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 Roeckff300b72021-11-09 13:18:26 -0800232 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 Roeck50152fb2021-10-16 14:56:29 -0700238 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 Roeckff300b72021-11-09 13:18:26 -0800245 case TMP401_CONVERSION_RATE:
246 case TMP401_CONFIG:
247 reg += 6; /* adjust for register write address */
248 fallthrough;
Guenter Roeck50152fb2021-10-16 14:56:29 -0700249 default:
250 return i2c_smbus_write_byte_data(client, reg, val);
251 }
252}
253
254static 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 Roeckca53e762021-10-16 10:51:27 -0700265static int tmp401_register_to_temp(u16 reg, bool extended)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200266{
267 int temp = reg;
268
Guenter Roeckca53e762021-10-16 10:51:27 -0700269 if (extended)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200270 temp -= 64 * 256;
271
Guenter Roeck14f2a662013-03-27 21:23:10 -0700272 return DIV_ROUND_CLOSEST(temp * 125, 32);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200273}
274
Guenter Roeckca53e762021-10-16 10:51:27 -0700275static u16 tmp401_temp_to_register(long temp, bool extended, int zbits)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200276{
Guenter Roeckca53e762021-10-16 10:51:27 -0700277 if (extended) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800278 temp = clamp_val(temp, -64000, 191000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200279 temp += 64000;
Guenter Roeckca53e762021-10-16 10:51:27 -0700280 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800281 temp = clamp_val(temp, 0, 127000);
Guenter Roeckca53e762021-10-16 10:51:27 -0700282 }
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200283
Guenter Roeck14f2a662013-03-27 21:23:10 -0700284 return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200285}
286
Guenter Roeck50152fb2021-10-16 14:56:29 -0700287/* hwmon API functions */
Guenter Roeckca53e762021-10-16 10:51:27 -0700288
289static 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
298static 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
305static int tmp401_temp_read(struct device *dev, u32 attr, int channel, long *val)
306{
307 struct tmp401_data *data = dev_get_drvdata(dev);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700308 struct regmap *regmap = data->regmap;
309 unsigned int regval;
310 int reg, ret;
Guenter Roeckca53e762021-10-16 10:51:27 -0700311
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 Roeckff300b72021-11-09 13:18:26 -0800319 reg = TMP401_TEMP_MSB[tmp401_temp_reg_index[attr]][channel];
Guenter Roeck50152fb2021-10-16 14:56:29 -0700320 ret = regmap_read(regmap, reg, &regval);
321 if (ret < 0)
322 return ret;
Guenter Roeckca53e762021-10-16 10:51:27 -0700323 *val = tmp401_register_to_temp(regval, data->extended_range);
324 break;
325 case hwmon_temp_crit_hyst:
326 mutex_lock(&data->update_lock);
Guenter Roeckff300b72021-11-09 13:18:26 -0800327 reg = TMP401_TEMP_MSB[3][channel];
Guenter Roeck50152fb2021-10-16 14:56:29 -0700328 ret = regmap_read(regmap, reg, &regval);
329 if (ret < 0)
Guenter Roeckca53e762021-10-16 10:51:27 -0700330 goto unlock;
331 *val = tmp401_register_to_temp(regval, data->extended_range);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700332 ret = regmap_read(regmap, TMP401_TEMP_CRIT_HYST, &regval);
333 if (ret < 0)
Guenter Roeckca53e762021-10-16 10:51:27 -0700334 goto unlock;
335 *val -= regval * 1000;
Guenter Roeckca53e762021-10-16 10:51:27 -0700336unlock:
337 mutex_unlock(&data->update_lock);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700338 if (ret < 0)
339 return ret;
Guenter Roeckca53e762021-10-16 10:51:27 -0700340 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 Roeck50152fb2021-10-16 14:56:29 -0700346 ret = regmap_read(regmap, reg, &regval);
347 if (ret < 0)
348 return ret;
Guenter Roeckca53e762021-10-16 10:51:27 -0700349 *val = !!(regval & BIT(channel));
350 break;
351 default:
352 return -EOPNOTSUPP;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200353 }
Guenter Roeck14f2a662013-03-27 21:23:10 -0700354 return 0;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200355}
356
Guenter Roeckca53e762021-10-16 10:51:27 -0700357static int tmp401_temp_write(struct device *dev, u32 attr, int channel,
358 long val)
Andre Prendelea63c2b2010-05-27 19:58:49 +0200359{
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700360 struct tmp401_data *data = dev_get_drvdata(dev);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700361 struct regmap *regmap = data->regmap;
362 unsigned int regval;
363 int reg, ret, temp;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200364
365 mutex_lock(&data->update_lock);
Guenter Roeckca53e762021-10-16 10:51:27 -0700366 switch (attr) {
367 case hwmon_temp_min:
368 case hwmon_temp_max:
369 case hwmon_temp_crit:
Guenter Roeckff300b72021-11-09 13:18:26 -0800370 reg = TMP401_TEMP_MSB[tmp401_temp_reg_index[attr]][channel];
Guenter Roeckca53e762021-10-16 10:51:27 -0700371 regval = tmp401_temp_to_register(val, data->extended_range,
372 attr == hwmon_temp_crit ? 8 : 4);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700373 ret = regmap_write(regmap, reg, regval);
Guenter Roeckca53e762021-10-16 10:51:27 -0700374 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 Prendelea63c2b2010-05-27 19:58:49 +0200380
Guenter Roeckff300b72021-11-09 13:18:26 -0800381 reg = TMP401_TEMP_MSB[3][channel];
Guenter Roeck50152fb2021-10-16 14:56:29 -0700382 ret = regmap_read(regmap, reg, &regval);
Guenter Roeckca53e762021-10-16 10:51:27 -0700383 if (ret < 0)
384 break;
Guenter Roeck50152fb2021-10-16 14:56:29 -0700385 temp = tmp401_register_to_temp(regval, data->extended_range);
Guenter Roeckca53e762021-10-16 10:51:27 -0700386 val = clamp_val(val, temp - 255000, temp);
387 regval = ((temp - val) + 500) / 1000;
Guenter Roeck50152fb2021-10-16 14:56:29 -0700388 ret = regmap_write(regmap, TMP401_TEMP_CRIT_HYST, regval);
Guenter Roeckca53e762021-10-16 10:51:27 -0700389 break;
390 default:
391 ret = -EOPNOTSUPP;
392 break;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200393 }
Andre Prendelea63c2b2010-05-27 19:58:49 +0200394 mutex_unlock(&data->update_lock);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700395 return ret;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200396}
397
Guenter Roeckca53e762021-10-16 10:51:27 -0700398static int tmp401_chip_read(struct device *dev, u32 attr, int channel, long *val)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200399{
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700400 struct tmp401_data *data = dev_get_drvdata(dev);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700401 u32 regval;
402 int ret;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200403
Guenter Roeckca53e762021-10-16 10:51:27 -0700404 switch (attr) {
405 case hwmon_chip_update_interval:
Guenter Roeckff300b72021-11-09 13:18:26 -0800406 ret = regmap_read(data->regmap, TMP401_CONVERSION_RATE, &regval);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700407 if (ret < 0)
408 return ret;
Guenter Roeckca53e762021-10-16 10:51:27 -0700409 *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 Roeck14f2a662013-03-27 21:23:10 -0700416 }
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200417
Guenter Roeckca53e762021-10-16 10:51:27 -0700418 return 0;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200419}
420
Guenter Roeck50152fb2021-10-16 14:56:29 -0700421static int tmp401_set_convrate(struct regmap *regmap, long val)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200422{
Guenter Roeckff300b72021-11-09 13:18:26 -0800423 int rate;
Guenter Roeck0846e302013-03-28 02:03:10 -0700424
Guenter Roeck0846e302013-03-28 02:03:10 -0700425 /*
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 Roeckff300b72021-11-09 13:18:26 -0800435 return regmap_write(regmap, TMP401_CONVERSION_RATE, rate);
Guenter Roeck0846e302013-03-28 02:03:10 -0700436}
437
Guenter Roeckca53e762021-10-16 10:51:27 -0700438static int tmp401_chip_write(struct device *dev, u32 attr, int channel, long val)
439{
440 struct tmp401_data *data = dev_get_drvdata(dev);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700441 struct regmap *regmap = data->regmap;
Guenter Roeckca53e762021-10-16 10:51:27 -0700442 int err;
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700443
Guenter Roeckca53e762021-10-16 10:51:27 -0700444 mutex_lock(&data->update_lock);
445 switch (attr) {
446 case hwmon_chip_update_interval:
Guenter Roeck50152fb2021-10-16 14:56:29 -0700447 err = tmp401_set_convrate(regmap, val);
Guenter Roeckca53e762021-10-16 10:51:27 -0700448 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 Roeck50152fb2021-10-16 14:56:29 -0700458 err = regmap_write(regmap, 0x30, 0);
Guenter Roeckca53e762021-10-16 10:51:27 -0700459 break;
460 default:
461 err = -EOPNOTSUPP;
462 break;
463 }
464 mutex_unlock(&data->update_lock);
Guenter Roeck0846e302013-03-28 02:03:10 -0700465
Guenter Roeckca53e762021-10-16 10:51:27 -0700466 return err;
467}
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700468
Guenter Roeckca53e762021-10-16 10:51:27 -0700469static 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 Roeckb4e665c2013-03-27 08:58:46 -0700481
Guenter Roeckca53e762021-10-16 10:51:27 -0700482static 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 Roeck0846e302013-03-28 02:03:10 -0700494
Guenter Roeckca53e762021-10-16 10:51:27 -0700495static 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
533static const struct hwmon_ops tmp401_ops = {
534 .is_visible = tmp401_is_visible,
535 .read = tmp401_read,
536 .write = tmp401_write,
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700537};
538
Guenter Roeck50152fb2021-10-16 14:56:29 -0700539/* chip initialization, detect, probe */
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200540
Guenter Roeck50152fb2021-10-16 14:56:29 -0700541static 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 Roeckff300b72021-11-09 13:18:26 -0800548 ret = regmap_write(regmap, TMP401_CONVERSION_RATE, 5);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700549 if (ret < 0)
550 return ret;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200551
552 /* Start conversions (disable shutdown if necessary) */
Guenter Roeckff300b72021-11-09 13:18:26 -0800553 ret = regmap_read(regmap, TMP401_CONFIG, &config);
Guenter Roeck50152fb2021-10-16 14:56:29 -0700554 if (ret < 0)
555 return ret;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200556
557 config_orig = config;
558 config &= ~TMP401_CONFIG_SHUTDOWN;
559
Guenter Roeckca53e762021-10-16 10:51:27 -0700560 data->extended_range = !!(config & TMP401_CONFIG_RANGE);
561
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200562 if (config != config_orig)
Guenter Roeckff300b72021-11-09 13:18:26 -0800563 ret = regmap_write(regmap, TMP401_CONFIG, config);
Bartosz Golaszewski90652ef2014-12-04 17:45:53 +0100564
Guenter Roeck50152fb2021-10-16 14:56:29 -0700565 return ret;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200566}
567
Jean Delvare310ec792009-12-14 21:17:23 +0100568static int tmp401_detect(struct i2c_client *client,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200569 struct i2c_board_info *info)
570{
Jean Delvaredbe73c82009-12-09 20:35:54 +0100571 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200572 struct i2c_adapter *adapter = client->adapter;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100573 u8 reg;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200574
575 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
576 return -ENODEV;
577
578 /* Detect and identify the chip */
Jean Delvaredbe73c82009-12-09 20:35:54 +0100579 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
580 if (reg != TMP401_MANUFACTURER_ID)
581 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200582
Jean Delvaredbe73c82009-12-09 20:35:54 +0100583 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200584
Jean Delvaredbe73c82009-12-09 20:35:54 +0100585 switch (reg) {
586 case TMP401_DEVICE_ID:
Guenter Roecka1fac922013-03-15 12:55:08 -0700587 if (client->addr != 0x4c)
588 return -ENODEV;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100589 kind = tmp401;
590 break;
Guenter Roeck4ce5b1f2013-03-29 17:56:07 -0700591 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 Delvaredbe73c82009-12-09 20:35:54 +0100604 kind = tmp411;
605 break;
Guenter Roecka1fac922013-03-15 12:55:08 -0700606 case TMP431_DEVICE_ID:
Guenter Roeck907a6d52014-12-05 10:15:03 -0800607 if (client->addr != 0x4c && client->addr != 0x4d)
Guenter Roecka1fac922013-03-15 12:55:08 -0700608 return -ENODEV;
609 kind = tmp431;
610 break;
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700611 case TMP432_DEVICE_ID:
Guenter Roeck907a6d52014-12-05 10:15:03 -0800612 if (client->addr != 0x4c && client->addr != 0x4d)
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700613 return -ENODEV;
614 kind = tmp432;
615 break;
Patrick Titiano06adbae2014-12-04 17:45:51 +0100616 case TMP435_DEVICE_ID:
Patrick Titiano06adbae2014-12-04 17:45:51 +0100617 kind = tmp435;
618 break;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100619 default:
620 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200621 }
Jean Delvaredbe73c82009-12-09 20:35:54 +0100622
Guenter Roeckff300b72021-11-09 13:18:26 -0800623 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG);
Jean Delvaredbe73c82009-12-09 20:35:54 +0100624 if (reg & 0x1b)
625 return -ENODEV;
626
Guenter Roeckff300b72021-11-09 13:18:26 -0800627 reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE);
Jean Delvaredbe73c82009-12-09 20:35:54 +0100628 /* Datasheet says: 0x1-0x6 */
629 if (reg > 15)
630 return -ENODEV;
631
Jean Delvaredc71afe2010-03-05 22:17:26 +0100632 strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200633
634 return 0;
635}
636
Stephen Kitt67487032020-08-13 18:02:22 +0200637static int tmp401_probe(struct i2c_client *client)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200638{
Patrick Titiano06adbae2014-12-04 17:45:51 +0100639 static const char * const names[] = {
Guenter Roeck38d9f062021-10-15 17:34:25 -0700640 "TMP401", "TMP411", "TMP431", "TMP432", "TMP435"
Patrick Titiano06adbae2014-12-04 17:45:51 +0100641 };
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700642 struct device *dev = &client->dev;
Guenter Roeckca53e762021-10-16 10:51:27 -0700643 struct hwmon_channel_info *info;
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700644 struct device *hwmon_dev;
645 struct tmp401_data *data;
Guenter Roeckca53e762021-10-16 10:51:27 -0700646 int status;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200647
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700648 data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200649 if (!data)
650 return -ENOMEM;
651
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700652 data->client = client;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200653 mutex_init(&data->update_lock);
Stephen Kitt67487032020-08-13 18:02:22 +0200654 data->kind = i2c_match_id(tmp401_id, client)->driver_data;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200655
Guenter Roeck50152fb2021-10-16 14:56:29 -0700656 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 Roeckca53e762021-10-16 10:51:27 -0700660 /* 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 Goedeab2b79d2009-06-15 18:39:46 +0200696 /* Initialize the TMP401 chip */
Guenter Roeck50152fb2021-10-16 14:56:29 -0700697 status = tmp401_init_client(data);
Bartosz Golaszewski90652ef2014-12-04 17:45:53 +0100698 if (status < 0)
699 return status;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200700
Guenter Roeckca53e762021-10-16 10:51:27 -0700701 hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, data,
702 &data->chip, NULL);
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700703 if (IS_ERR(hwmon_dev))
704 return PTR_ERR(hwmon_dev);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200705
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700706 dev_info(dev, "Detected TI %s chip\n", names[data->kind]);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200707
708 return 0;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200709}
710
Andre Prendelea63c2b2010-05-27 19:58:49 +0200711static struct i2c_driver tmp401_driver = {
712 .class = I2C_CLASS_HWMON,
713 .driver = {
714 .name = "tmp401",
715 },
Stephen Kitt67487032020-08-13 18:02:22 +0200716 .probe_new = tmp401_probe,
Andre Prendelea63c2b2010-05-27 19:58:49 +0200717 .id_table = tmp401_id,
718 .detect = tmp401_detect,
719 .address_list = normal_i2c,
720};
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200721
Axel Linf0967ee2012-01-20 15:38:18 +0800722module_i2c_driver(tmp401_driver);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200723
724MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
725MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
726MODULE_LICENSE("GPL");