blob: 9dc210b55e69b19f05dc2cf0f0666e8a0cc1a384 [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
21#include <linux/module.h>
22#include <linux/init.h>
Guenter Roeck947e9272013-03-27 08:48:03 -070023#include <linux/bitops.h>
Hans de Goedeab2b79d2009-06-15 18:39:46 +020024#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 Roeck9aecac02015-05-27 16:11:48 -070034static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4c, 0x4d,
Guenter Roeck907a6d52014-12-05 10:15:03 -080035 0x4e, 0x4f, I2C_CLIENT_END };
Hans de Goedeab2b79d2009-06-15 18:39:46 +020036
Andrew F. Davisc0a686012016-06-08 12:00:54 -050037enum chips { tmp401, tmp411, tmp431, tmp432, tmp435, tmp461 };
Hans de Goedeab2b79d2009-06-15 18:39:46 +020038
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 Goedeab2b79d2009-06-15 18:39:46 +020049#define TMP401_MANUFACTURER_ID_REG 0xFE
50#define TMP401_DEVICE_ID_REG 0xFF
51
Andrew F. Davisc0a686012016-06-08 12:00:54 -050052static const u8 TMP401_TEMP_MSB_READ[7][2] = {
Guenter Roeck14f2a662013-03-27 21:23:10 -070053 { 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. Davisc0a686012016-06-08 12:00:54 -050059 { 0, 0x11 }, /* offset */
Guenter Roeck14f2a662013-03-27 21:23:10 -070060};
Hans de Goedeab2b79d2009-06-15 18:39:46 +020061
Andrew F. Davisc0a686012016-06-08 12:00:54 -050062static const u8 TMP401_TEMP_MSB_WRITE[7][2] = {
Guenter Roeck14f2a662013-03-27 21:23:10 -070063 { 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. Davisc0a686012016-06-08 12:00:54 -050069 { 0, 0x11 }, /* offset */
Guenter Roeck14f2a662013-03-27 21:23:10 -070070};
71
Guenter Roeck29dd3b62013-03-28 01:36:44 -070072static 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
79static 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 Roeck29dd3b62013-03-28 01:36:44 -070086/* [0] = fault, [1] = low, [2] = high, [3] = therm/crit */
87static const u8 TMP432_STATUS_REG[] = {
88 0x1b, 0x36, 0x35, 0x37 };
89
Hans de Goedeab2b79d2009-06-15 18:39:46 +020090/* Flags */
Guenter Roeck947e9272013-03-27 08:48:03 -070091#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 Goedeab2b79d2009-06-15 18:39:46 +0200100
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700101/* 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 Goedeab2b79d2009-06-15 18:39:46 +0200106/* Manufacturer / Device ID's */
107#define TMP401_MANUFACTURER_ID 0x55
108#define TMP401_DEVICE_ID 0x11
Guenter Roeck4ce5b1f2013-03-29 17:56:07 -0700109#define TMP411A_DEVICE_ID 0x12
110#define TMP411B_DEVICE_ID 0x13
111#define TMP411C_DEVICE_ID 0x10
Guenter Roecka1fac922013-03-15 12:55:08 -0700112#define TMP431_DEVICE_ID 0x31
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700113#define TMP432_DEVICE_ID 0x32
Patrick Titiano06adbae2014-12-04 17:45:51 +0100114#define TMP435_DEVICE_ID 0x35
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200115
116/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200117 * Driver data (common to all clients)
118 */
119
120static const struct i2c_device_id tmp401_id[] = {
121 { "tmp401", tmp401 },
Andre Prendelfce0758f2009-06-15 18:39:47 +0200122 { "tmp411", tmp411 },
Guenter Roecka1fac922013-03-15 12:55:08 -0700123 { "tmp431", tmp431 },
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700124 { "tmp432", tmp432 },
Patrick Titiano06adbae2014-12-04 17:45:51 +0100125 { "tmp435", tmp435 },
Andrew F. Davisc0a686012016-06-08 12:00:54 -0500126 { "tmp461", tmp461 },
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200127 { }
128};
129MODULE_DEVICE_TABLE(i2c, tmp401_id);
130
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200131/*
132 * Client data (each client gets its own)
133 */
134
135struct tmp401_data {
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700136 struct i2c_client *client;
137 const struct attribute_group *groups[3];
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200138 struct mutex update_lock;
139 char valid; /* zero until following fields are valid */
140 unsigned long last_updated; /* in jiffies */
Jean Delvaredc71afe2010-03-05 22:17:26 +0100141 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200142
Guenter Roeck0846e302013-03-28 02:03:10 -0700143 unsigned int update_interval; /* in milliseconds */
144
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200145 /* register values */
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700146 u8 status[4];
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200147 u8 config;
Andrew F. Davisc0a686012016-06-08 12:00:54 -0500148 u16 temp[7][3];
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200149 u8 temp_crit_hyst;
150};
151
152/*
153 * Sysfs attr show / store functions
154 */
155
156static 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 Roeck14f2a662013-03-27 21:23:10 -0700163 return DIV_ROUND_CLOSEST(temp * 125, 32);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200164}
165
Guenter Roeck14f2a662013-03-27 21:23:10 -0700166static u16 tmp401_temp_to_register(long temp, u8 config, int zbits)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200167{
168 if (config & TMP401_CONFIG_RANGE) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800169 temp = clamp_val(temp, -64000, 191000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200170 temp += 64000;
171 } else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800172 temp = clamp_val(temp, 0, 127000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200173
Guenter Roeck14f2a662013-03-27 21:23:10 -0700174 return DIV_ROUND_CLOSEST(temp * (1 << (8 - zbits)), 1000) << zbits;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200175}
176
Guenter Roeck14f2a662013-03-27 21:23:10 -0700177static int tmp401_update_device_reg16(struct i2c_client *client,
178 struct tmp401_data *data)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200179{
Guenter Roeck14f2a662013-03-27 21:23:10 -0700180 int i, j, val;
181 int num_regs = data->kind == tmp411 ? 6 : 4;
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700182 int num_sensors = data->kind == tmp432 ? 3 : 2;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200183
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700184 for (i = 0; i < num_sensors; i++) { /* local / r1 / r2 */
Guenter Roeck14f2a662013-03-27 21:23:10 -0700185 for (j = 0; j < num_regs; j++) { /* temp / low / ... */
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700186 u8 regaddr;
Jeroen De Wachter24333ac2017-01-09 18:55:00 +0100187
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700188 regaddr = data->kind == tmp432 ?
189 TMP432_TEMP_MSB_READ[j][i] :
190 TMP401_TEMP_MSB_READ[j][i];
Jeroen De Wachter24333ac2017-01-09 18:55:00 +0100191 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 Roeck14f2a662013-03-27 21:23:10 -0700197 if (val < 0)
198 return val;
Jeroen De Wachter24333ac2017-01-09 18:55:00 +0100199
200 data->temp[j][i] = j == 3 ? val << 8 : val;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200201 }
202 }
Guenter Roeck14f2a662013-03-27 21:23:10 -0700203 return 0;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200204}
205
206static struct tmp401_data *tmp401_update_device(struct device *dev)
207{
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700208 struct tmp401_data *data = dev_get_drvdata(dev);
209 struct i2c_client *client = data->client;
Guenter Roeck14f2a662013-03-27 21:23:10 -0700210 struct tmp401_data *ret = data;
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700211 int i, val;
Guenter Roeck0846e302013-03-28 02:03:10 -0700212 unsigned long next_update;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200213
214 mutex_lock(&data->update_lock);
215
Guenter Roeck0846e302013-03-28 02:03:10 -0700216 next_update = data->last_updated +
Jean Delvare4e2284d2013-05-19 16:57:30 +0200217 msecs_to_jiffies(data->update_interval);
Guenter Roeck0846e302013-03-28 02:03:10 -0700218 if (time_after(jiffies, next_update) || !data->valid) {
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700219 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 Roeck14f2a662013-03-27 21:23:10 -0700249 }
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700250
Guenter Roeck14f2a662013-03-27 21:23:10 -0700251 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 Prendelea63c2b2010-05-27 19:58:49 +0200268
269 data->last_updated = jiffies;
270 data->valid = 1;
271 }
272
Guenter Roeck14f2a662013-03-27 21:23:10 -0700273abort:
Andre Prendelea63c2b2010-05-27 19:58:49 +0200274 mutex_unlock(&data->update_lock);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700275 return ret;
Andre Prendelea63c2b2010-05-27 19:58:49 +0200276}
277
Guenter Roecke36917f2018-12-06 10:59:49 -0800278static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
279 char *buf)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200280{
Guenter Roeck14f2a662013-03-27 21:23:10 -0700281 int nr = to_sensor_dev_attr_2(devattr)->nr;
282 int index = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200283 struct tmp401_data *data = tmp401_update_device(dev);
284
Guenter Roeck14f2a662013-03-27 21:23:10 -0700285 if (IS_ERR(data))
286 return PTR_ERR(data);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200287
288 return sprintf(buf, "%d\n",
Guenter Roeck14f2a662013-03-27 21:23:10 -0700289 tmp401_register_to_temp(data->temp[nr][index], data->config));
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200290}
291
Guenter Roecke36917f2018-12-06 10:59:49 -0800292static ssize_t temp_crit_hyst_show(struct device *dev,
293 struct device_attribute *devattr,
294 char *buf)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200295{
296 int temp, index = to_sensor_dev_attr(devattr)->index;
297 struct tmp401_data *data = tmp401_update_device(dev);
298
Guenter Roeck14f2a662013-03-27 21:23:10 -0700299 if (IS_ERR(data))
300 return PTR_ERR(data);
301
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200302 mutex_lock(&data->update_lock);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700303 temp = tmp401_register_to_temp(data->temp[3][index], data->config);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200304 temp -= data->temp_crit_hyst * 1000;
305 mutex_unlock(&data->update_lock);
306
307 return sprintf(buf, "%d\n", temp);
308}
309
Guenter Roecke36917f2018-12-06 10:59:49 -0800310static ssize_t status_show(struct device *dev,
311 struct device_attribute *devattr, char *buf)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200312{
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700313 int nr = to_sensor_dev_attr_2(devattr)->nr;
314 int mask = to_sensor_dev_attr_2(devattr)->index;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200315 struct tmp401_data *data = tmp401_update_device(dev);
316
Guenter Roeck14f2a662013-03-27 21:23:10 -0700317 if (IS_ERR(data))
318 return PTR_ERR(data);
319
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700320 return sprintf(buf, "%d\n", !!(data->status[nr] & mask));
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200321}
322
Guenter Roecke36917f2018-12-06 10:59:49 -0800323static ssize_t temp_store(struct device *dev,
324 struct device_attribute *devattr, const char *buf,
325 size_t count)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200326{
Guenter Roeck14f2a662013-03-27 21:23:10 -0700327 int nr = to_sensor_dev_attr_2(devattr)->nr;
328 int index = to_sensor_dev_attr_2(devattr)->index;
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700329 struct tmp401_data *data = dev_get_drvdata(dev);
330 struct i2c_client *client = data->client;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200331 long val;
332 u16 reg;
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700333 u8 regaddr;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200334
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100335 if (kstrtol(buf, 10, &val))
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200336 return -EINVAL;
337
Guenter Roeck14f2a662013-03-27 21:23:10 -0700338 reg = tmp401_temp_to_register(val, data->config, nr == 3 ? 8 : 4);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200339
340 mutex_lock(&data->update_lock);
341
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700342 regaddr = data->kind == tmp432 ? TMP432_TEMP_MSB_WRITE[nr][index]
343 : TMP401_TEMP_MSB_WRITE[nr][index];
Jeroen De Wachter24333ac2017-01-09 18:55:00 +0100344 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 Roeck14f2a662013-03-27 21:23:10 -0700349 }
350 data->temp[nr][index] = reg;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200351
352 mutex_unlock(&data->update_lock);
353
354 return count;
355}
356
Guenter Roecke36917f2018-12-06 10:59:49 -0800357static ssize_t temp_crit_hyst_store(struct device *dev,
358 struct device_attribute *devattr,
359 const char *buf, size_t count)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200360{
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 Roeck14f2a662013-03-27 21:23:10 -0700366 if (IS_ERR(data))
367 return PTR_ERR(data);
368
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100369 if (kstrtol(buf, 10, &val))
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200370 return -EINVAL;
371
372 if (data->config & TMP401_CONFIG_RANGE)
Guenter Roeck2a844c12013-01-09 08:09:34 -0800373 val = clamp_val(val, -64000, 191000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200374 else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800375 val = clamp_val(val, 0, 127000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200376
377 mutex_lock(&data->update_lock);
Guenter Roeck14f2a662013-03-27 21:23:10 -0700378 temp = tmp401_register_to_temp(data->temp[3][index], data->config);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800379 val = clamp_val(val, temp - 255000, temp);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200380 reg = ((temp - val) + 500) / 1000;
381
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700382 i2c_smbus_write_byte_data(data->client, TMP401_TEMP_CRIT_HYST,
Guenter Roeck14f2a662013-03-27 21:23:10 -0700383 reg);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200384
385 data->temp_crit_hyst = reg;
386
387 mutex_unlock(&data->update_lock);
388
389 return count;
390}
391
Andre Prendelfce0758f2009-06-15 18:39:47 +0200392/*
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 Roecke36917f2018-12-06 10:59:49 -0800397static ssize_t reset_temp_history_store(struct device *dev,
398 struct device_attribute *devattr,
399 const char *buf, size_t count)
Andre Prendelfce0758f2009-06-15 18:39:47 +0200400{
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700401 struct tmp401_data *data = dev_get_drvdata(dev);
402 struct i2c_client *client = data->client;
Andre Prendelfce0758f2009-06-15 18:39:47 +0200403 long val;
404
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100405 if (kstrtol(buf, 10, &val))
Andre Prendelfce0758f2009-06-15 18:39:47 +0200406 return -EINVAL;
407
408 if (val != 1) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800409 dev_err(dev,
410 "temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
411 val);
Andre Prendelfce0758f2009-06-15 18:39:47 +0200412 return -EINVAL;
413 }
Guenter Roeck8eb6d902013-04-14 04:39:46 -0700414 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 Prendelfce0758f2009-06-15 18:39:47 +0200418
419 return count;
420}
421
Julia Lawall5343aed2016-12-22 13:05:35 +0100422static ssize_t update_interval_show(struct device *dev,
Guenter Roeck0846e302013-03-28 02:03:10 -0700423 struct device_attribute *attr, char *buf)
424{
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700425 struct tmp401_data *data = dev_get_drvdata(dev);
Guenter Roeck0846e302013-03-28 02:03:10 -0700426
427 return sprintf(buf, "%u\n", data->update_interval);
428}
429
Julia Lawall5343aed2016-12-22 13:05:35 +0100430static ssize_t update_interval_store(struct device *dev,
431 struct device_attribute *attr,
432 const char *buf, size_t count)
Guenter Roeck0846e302013-03-28 02:03:10 -0700433{
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700434 struct tmp401_data *data = dev_get_drvdata(dev);
435 struct i2c_client *client = data->client;
Guenter Roeck0846e302013-03-28 02:03:10 -0700436 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 Roecke36917f2018-12-06 10:59:49 -0800461static SENSOR_DEVICE_ATTR_2_RO(temp1_input, temp, 0, 0);
462static SENSOR_DEVICE_ATTR_2_RW(temp1_min, temp, 1, 0);
463static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 2, 0);
464static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 3, 0);
465static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp_crit_hyst, 0);
466static SENSOR_DEVICE_ATTR_2_RO(temp1_min_alarm, status, 1,
467 TMP432_STATUS_LOCAL);
468static SENSOR_DEVICE_ATTR_2_RO(temp1_max_alarm, status, 2,
469 TMP432_STATUS_LOCAL);
470static SENSOR_DEVICE_ATTR_2_RO(temp1_crit_alarm, status, 3,
471 TMP432_STATUS_LOCAL);
472static SENSOR_DEVICE_ATTR_2_RO(temp2_input, temp, 0, 1);
473static SENSOR_DEVICE_ATTR_2_RW(temp2_min, temp, 1, 1);
474static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 2, 1);
475static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 3, 1);
476static SENSOR_DEVICE_ATTR_RO(temp2_crit_hyst, temp_crit_hyst, 1);
477static SENSOR_DEVICE_ATTR_2_RO(temp2_fault, status, 0, TMP432_STATUS_REMOTE1);
478static SENSOR_DEVICE_ATTR_2_RO(temp2_min_alarm, status, 1,
479 TMP432_STATUS_REMOTE1);
480static SENSOR_DEVICE_ATTR_2_RO(temp2_max_alarm, status, 2,
481 TMP432_STATUS_REMOTE1);
482static SENSOR_DEVICE_ATTR_2_RO(temp2_crit_alarm, status, 3,
483 TMP432_STATUS_REMOTE1);
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700484
Julia Lawall5343aed2016-12-22 13:05:35 +0100485static DEVICE_ATTR_RW(update_interval);
Guenter Roeck0846e302013-03-28 02:03:10 -0700486
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700487static 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 Roeck0846e302013-03-28 02:03:10 -0700507 &dev_attr_update_interval.attr,
508
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700509 NULL
510};
511
512static const struct attribute_group tmp401_group = {
513 .attrs = tmp401_attributes,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200514};
515
516/*
Andre Prendelfce0758f2009-06-15 18:39:47 +0200517 * 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 Roecke36917f2018-12-06 10:59:49 -0800523static SENSOR_DEVICE_ATTR_2_RO(temp1_lowest, temp, 4, 0);
524static SENSOR_DEVICE_ATTR_2_RO(temp1_highest, temp, 5, 0);
525static SENSOR_DEVICE_ATTR_2_RO(temp2_lowest, temp, 4, 1);
526static SENSOR_DEVICE_ATTR_2_RO(temp2_highest, temp, 5, 1);
527static SENSOR_DEVICE_ATTR_WO(temp_reset_history, reset_temp_history, 0);
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700528
529static 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
538static const struct attribute_group tmp411_group = {
539 .attrs = tmp411_attributes,
Andre Prendelfce0758f2009-06-15 18:39:47 +0200540};
541
Guenter Roecke36917f2018-12-06 10:59:49 -0800542static SENSOR_DEVICE_ATTR_2_RO(temp3_input, temp, 0, 2);
543static SENSOR_DEVICE_ATTR_2_RW(temp3_min, temp, 1, 2);
544static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, 2);
545static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 3, 2);
546static SENSOR_DEVICE_ATTR_RO(temp3_crit_hyst, temp_crit_hyst, 2);
547static SENSOR_DEVICE_ATTR_2_RO(temp3_fault, status, 0, TMP432_STATUS_REMOTE2);
548static SENSOR_DEVICE_ATTR_2_RO(temp3_min_alarm, status, 1,
549 TMP432_STATUS_REMOTE2);
550static SENSOR_DEVICE_ATTR_2_RO(temp3_max_alarm, status, 2,
551 TMP432_STATUS_REMOTE2);
552static SENSOR_DEVICE_ATTR_2_RO(temp3_crit_alarm, status, 3,
553 TMP432_STATUS_REMOTE2);
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700554
555static 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
569static const struct attribute_group tmp432_group = {
570 .attrs = tmp432_attributes,
571};
572
Andre Prendelfce0758f2009-06-15 18:39:47 +0200573/*
Andrew F. Davisc0a686012016-06-08 12:00:54 -0500574 * Additional features of the TMP461 chip.
575 * The TMP461 temperature offset for the remote channel.
576 */
Guenter Roecke36917f2018-12-06 10:59:49 -0800577static SENSOR_DEVICE_ATTR_2_RW(temp2_offset, temp, 6, 1);
Andrew F. Davisc0a686012016-06-08 12:00:54 -0500578
579static struct attribute *tmp461_attributes[] = {
580 &sensor_dev_attr_temp2_offset.dev_attr.attr,
581 NULL
582};
583
584static const struct attribute_group tmp461_group = {
585 .attrs = tmp461_attributes,
586};
587
588/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200589 * Begin non sysfs callback code (aka Real code)
590 */
591
Bartosz Golaszewski90652ef2014-12-04 17:45:53 +0100592static int tmp401_init_client(struct tmp401_data *data,
593 struct i2c_client *client)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200594{
Bartosz Golaszewski90652ef2014-12-04 17:45:53 +0100595 int config, config_orig, status = 0;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200596
597 /* Set the conversion rate to 2 Hz */
598 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
Guenter Roeck0846e302013-03-28 02:03:10 -0700599 data->update_interval = 500;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200600
601 /* Start conversions (disable shutdown if necessary) */
602 config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
Bartosz Golaszewski90652ef2014-12-04 17:45:53 +0100603 if (config < 0)
604 return config;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200605
606 config_orig = config;
607 config &= ~TMP401_CONFIG_SHUTDOWN;
608
609 if (config != config_orig)
Bartosz Golaszewski90652ef2014-12-04 17:45:53 +0100610 status = i2c_smbus_write_byte_data(client,
611 TMP401_CONFIG_WRITE,
612 config);
613
614 return status;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200615}
616
Jean Delvare310ec792009-12-14 21:17:23 +0100617static int tmp401_detect(struct i2c_client *client,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200618 struct i2c_board_info *info)
619{
Jean Delvaredbe73c82009-12-09 20:35:54 +0100620 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200621 struct i2c_adapter *adapter = client->adapter;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100622 u8 reg;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200623
624 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
625 return -ENODEV;
626
627 /* Detect and identify the chip */
Jean Delvaredbe73c82009-12-09 20:35:54 +0100628 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
629 if (reg != TMP401_MANUFACTURER_ID)
630 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200631
Jean Delvaredbe73c82009-12-09 20:35:54 +0100632 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200633
Jean Delvaredbe73c82009-12-09 20:35:54 +0100634 switch (reg) {
635 case TMP401_DEVICE_ID:
Guenter Roecka1fac922013-03-15 12:55:08 -0700636 if (client->addr != 0x4c)
637 return -ENODEV;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100638 kind = tmp401;
639 break;
Guenter Roeck4ce5b1f2013-03-29 17:56:07 -0700640 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 Delvaredbe73c82009-12-09 20:35:54 +0100653 kind = tmp411;
654 break;
Guenter Roecka1fac922013-03-15 12:55:08 -0700655 case TMP431_DEVICE_ID:
Guenter Roeck907a6d52014-12-05 10:15:03 -0800656 if (client->addr != 0x4c && client->addr != 0x4d)
Guenter Roecka1fac922013-03-15 12:55:08 -0700657 return -ENODEV;
658 kind = tmp431;
659 break;
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700660 case TMP432_DEVICE_ID:
Guenter Roeck907a6d52014-12-05 10:15:03 -0800661 if (client->addr != 0x4c && client->addr != 0x4d)
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700662 return -ENODEV;
663 kind = tmp432;
664 break;
Patrick Titiano06adbae2014-12-04 17:45:51 +0100665 case TMP435_DEVICE_ID:
Patrick Titiano06adbae2014-12-04 17:45:51 +0100666 kind = tmp435;
667 break;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100668 default:
669 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200670 }
Jean Delvaredbe73c82009-12-09 20:35:54 +0100671
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 Delvaredc71afe2010-03-05 22:17:26 +0100681 strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200682
683 return 0;
684}
685
Stephen Kitt67487032020-08-13 18:02:22 +0200686static int tmp401_probe(struct i2c_client *client)
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200687{
Patrick Titiano06adbae2014-12-04 17:45:51 +0100688 static const char * const names[] = {
Andrew F. Davisc0a686012016-06-08 12:00:54 -0500689 "TMP401", "TMP411", "TMP431", "TMP432", "TMP435", "TMP461"
Patrick Titiano06adbae2014-12-04 17:45:51 +0100690 };
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700691 struct device *dev = &client->dev;
692 struct device *hwmon_dev;
693 struct tmp401_data *data;
Bartosz Golaszewski90652ef2014-12-04 17:45:53 +0100694 int groups = 0, status;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200695
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700696 data = devm_kzalloc(dev, sizeof(struct tmp401_data), GFP_KERNEL);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200697 if (!data)
698 return -ENOMEM;
699
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700700 data->client = client;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200701 mutex_init(&data->update_lock);
Stephen Kitt67487032020-08-13 18:02:22 +0200702 data->kind = i2c_match_id(tmp401_id, client)->driver_data;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200703
704 /* Initialize the TMP401 chip */
Bartosz Golaszewski90652ef2014-12-04 17:45:53 +0100705 status = tmp401_init_client(data, client);
706 if (status < 0)
707 return status;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200708
709 /* Register sysfs hooks */
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700710 data->groups[groups++] = &tmp401_group;
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700711
712 /* Register additional tmp411 sysfs hooks */
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700713 if (data->kind == tmp411)
714 data->groups[groups++] = &tmp411_group;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200715
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700716 /* Register additional tmp432 sysfs hooks */
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700717 if (data->kind == tmp432)
718 data->groups[groups++] = &tmp432_group;
Guenter Roeck29dd3b62013-03-28 01:36:44 -0700719
Andrew F. Davisc0a686012016-06-08 12:00:54 -0500720 if (data->kind == tmp461)
721 data->groups[groups++] = &tmp461_group;
722
Guenter Roeckf3643ac2013-09-04 16:24:09 -0700723 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 Goedeab2b79d2009-06-15 18:39:46 +0200727
Guenter Roeckb4e665c2013-03-27 08:58:46 -0700728 dev_info(dev, "Detected TI %s chip\n", names[data->kind]);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200729
730 return 0;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200731}
732
Andre Prendelea63c2b2010-05-27 19:58:49 +0200733static struct i2c_driver tmp401_driver = {
734 .class = I2C_CLASS_HWMON,
735 .driver = {
736 .name = "tmp401",
737 },
Stephen Kitt67487032020-08-13 18:02:22 +0200738 .probe_new = tmp401_probe,
Andre Prendelea63c2b2010-05-27 19:58:49 +0200739 .id_table = tmp401_id,
740 .detect = tmp401_detect,
741 .address_list = normal_i2c,
742};
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200743
Axel Linf0967ee2012-01-20 15:38:18 +0800744module_i2c_driver(tmp401_driver);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200745
746MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
747MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
748MODULE_LICENSE("GPL");