blob: be71a3e17ccad503e87586318f846d39ae2eaf7c [file] [log] [blame]
Hans de Goedeab2b79d2009-06-15 18:39:46 +02001/* tmp401.c
2 *
3 * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
Andre Prendelfce07582009-06-15 18:39:47 +02004 * Preliminary tmp411 support by:
5 * Gabriel Konat, Sander Leget, Wouter Willems
6 * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
Hans de Goedeab2b79d2009-06-15 18:39:46 +02007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23/*
24 * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC.
25 *
26 * Note this IC is in some aspect similar to the LM90, but it has quite a
27 * few differences too, for example the local temp has a higher resolution
28 * and thus has 16 bits registers for its value and limit instead of 8 bits.
29 */
30
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/jiffies.h>
35#include <linux/i2c.h>
36#include <linux/hwmon.h>
37#include <linux/hwmon-sysfs.h>
38#include <linux/err.h>
39#include <linux/mutex.h>
40#include <linux/sysfs.h>
41
42/* Addresses to scan */
Guenter Roecka1fac922013-03-15 12:55:08 -070043static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Hans de Goedeab2b79d2009-06-15 18:39:46 +020044
Guenter Roecka1fac922013-03-15 12:55:08 -070045enum chips { tmp401, tmp411, tmp431 };
Hans de Goedeab2b79d2009-06-15 18:39:46 +020046
47/*
48 * The TMP401 registers, note some registers have different addresses for
49 * reading and writing
50 */
51#define TMP401_STATUS 0x02
52#define TMP401_CONFIG_READ 0x03
53#define TMP401_CONFIG_WRITE 0x09
54#define TMP401_CONVERSION_RATE_READ 0x04
55#define TMP401_CONVERSION_RATE_WRITE 0x0A
56#define TMP401_TEMP_CRIT_HYST 0x21
57#define TMP401_CONSECUTIVE_ALERT 0x22
58#define TMP401_MANUFACTURER_ID_REG 0xFE
59#define TMP401_DEVICE_ID_REG 0xFF
Andre Prendelfce07582009-06-15 18:39:47 +020060#define TMP411_N_FACTOR_REG 0x18
Hans de Goedeab2b79d2009-06-15 18:39:46 +020061
62static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 };
63static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 };
64static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2] = { 0x06, 0x08 };
65static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2] = { 0x0C, 0x0E };
66static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2] = { 0x17, 0x14 };
67static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2] = { 0x05, 0x07 };
68static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2] = { 0x0B, 0x0D };
69static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 };
70/* These are called the THERM limit / hysteresis / mask in the datasheet */
71static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 };
72
Andre Prendelfce07582009-06-15 18:39:47 +020073static const u8 TMP411_TEMP_LOWEST_MSB[2] = { 0x30, 0x34 };
74static const u8 TMP411_TEMP_LOWEST_LSB[2] = { 0x31, 0x35 };
75static const u8 TMP411_TEMP_HIGHEST_MSB[2] = { 0x32, 0x36 };
76static const u8 TMP411_TEMP_HIGHEST_LSB[2] = { 0x33, 0x37 };
77
Hans de Goedeab2b79d2009-06-15 18:39:46 +020078/* Flags */
79#define TMP401_CONFIG_RANGE 0x04
80#define TMP401_CONFIG_SHUTDOWN 0x40
81#define TMP401_STATUS_LOCAL_CRIT 0x01
82#define TMP401_STATUS_REMOTE_CRIT 0x02
83#define TMP401_STATUS_REMOTE_OPEN 0x04
84#define TMP401_STATUS_REMOTE_LOW 0x08
85#define TMP401_STATUS_REMOTE_HIGH 0x10
86#define TMP401_STATUS_LOCAL_LOW 0x20
87#define TMP401_STATUS_LOCAL_HIGH 0x40
88
89/* Manufacturer / Device ID's */
90#define TMP401_MANUFACTURER_ID 0x55
91#define TMP401_DEVICE_ID 0x11
Guenter Roeck4ce5b1f2013-03-29 17:56:07 -070092#define TMP411A_DEVICE_ID 0x12
93#define TMP411B_DEVICE_ID 0x13
94#define TMP411C_DEVICE_ID 0x10
Guenter Roecka1fac922013-03-15 12:55:08 -070095#define TMP431_DEVICE_ID 0x31
Hans de Goedeab2b79d2009-06-15 18:39:46 +020096
97/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +020098 * Driver data (common to all clients)
99 */
100
101static const struct i2c_device_id tmp401_id[] = {
102 { "tmp401", tmp401 },
Andre Prendelfce07582009-06-15 18:39:47 +0200103 { "tmp411", tmp411 },
Guenter Roecka1fac922013-03-15 12:55:08 -0700104 { "tmp431", tmp431 },
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200105 { }
106};
107MODULE_DEVICE_TABLE(i2c, tmp401_id);
108
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200109/*
110 * Client data (each client gets its own)
111 */
112
113struct tmp401_data {
114 struct device *hwmon_dev;
115 struct mutex update_lock;
116 char valid; /* zero until following fields are valid */
117 unsigned long last_updated; /* in jiffies */
Jean Delvaredc71afe2010-03-05 22:17:26 +0100118 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200119
120 /* register values */
121 u8 status;
122 u8 config;
123 u16 temp[2];
124 u16 temp_low[2];
125 u16 temp_high[2];
126 u8 temp_crit[2];
127 u8 temp_crit_hyst;
Andre Prendelfce07582009-06-15 18:39:47 +0200128 u16 temp_lowest[2];
129 u16 temp_highest[2];
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200130};
131
132/*
133 * Sysfs attr show / store functions
134 */
135
136static int tmp401_register_to_temp(u16 reg, u8 config)
137{
138 int temp = reg;
139
140 if (config & TMP401_CONFIG_RANGE)
141 temp -= 64 * 256;
142
143 return (temp * 625 + 80) / 160;
144}
145
146static u16 tmp401_temp_to_register(long temp, u8 config)
147{
148 if (config & TMP401_CONFIG_RANGE) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800149 temp = clamp_val(temp, -64000, 191000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200150 temp += 64000;
151 } else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800152 temp = clamp_val(temp, 0, 127000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200153
154 return (temp * 160 + 312) / 625;
155}
156
157static int tmp401_crit_register_to_temp(u8 reg, u8 config)
158{
159 int temp = reg;
160
161 if (config & TMP401_CONFIG_RANGE)
162 temp -= 64;
163
164 return temp * 1000;
165}
166
167static u8 tmp401_crit_temp_to_register(long temp, u8 config)
168{
169 if (config & TMP401_CONFIG_RANGE) {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800170 temp = clamp_val(temp, -64000, 191000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200171 temp += 64000;
172 } else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800173 temp = clamp_val(temp, 0, 127000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200174
175 return (temp + 500) / 1000;
176}
177
Andre Prendelea63c2b2010-05-27 19:58:49 +0200178static struct tmp401_data *tmp401_update_device_reg16(
179 struct i2c_client *client, struct tmp401_data *data)
180{
181 int i;
182
183 for (i = 0; i < 2; i++) {
184 /*
185 * High byte must be read first immediately followed
186 * by the low byte
187 */
188 data->temp[i] = i2c_smbus_read_byte_data(client,
189 TMP401_TEMP_MSB[i]) << 8;
190 data->temp[i] |= i2c_smbus_read_byte_data(client,
191 TMP401_TEMP_LSB[i]);
192 data->temp_low[i] = i2c_smbus_read_byte_data(client,
193 TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8;
194 data->temp_low[i] |= i2c_smbus_read_byte_data(client,
195 TMP401_TEMP_LOW_LIMIT_LSB[i]);
196 data->temp_high[i] = i2c_smbus_read_byte_data(client,
197 TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8;
198 data->temp_high[i] |= i2c_smbus_read_byte_data(client,
199 TMP401_TEMP_HIGH_LIMIT_LSB[i]);
200 data->temp_crit[i] = i2c_smbus_read_byte_data(client,
201 TMP401_TEMP_CRIT_LIMIT[i]);
202
203 if (data->kind == tmp411) {
204 data->temp_lowest[i] = i2c_smbus_read_byte_data(client,
205 TMP411_TEMP_LOWEST_MSB[i]) << 8;
206 data->temp_lowest[i] |= i2c_smbus_read_byte_data(
207 client, TMP411_TEMP_LOWEST_LSB[i]);
208
209 data->temp_highest[i] = i2c_smbus_read_byte_data(
210 client, TMP411_TEMP_HIGHEST_MSB[i]) << 8;
211 data->temp_highest[i] |= i2c_smbus_read_byte_data(
212 client, TMP411_TEMP_HIGHEST_LSB[i]);
213 }
214 }
215 return data;
216}
217
218static struct tmp401_data *tmp401_update_device(struct device *dev)
219{
220 struct i2c_client *client = to_i2c_client(dev);
221 struct tmp401_data *data = i2c_get_clientdata(client);
222
223 mutex_lock(&data->update_lock);
224
225 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
226 data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS);
227 data->config = i2c_smbus_read_byte_data(client,
228 TMP401_CONFIG_READ);
229 tmp401_update_device_reg16(client, data);
230
231 data->temp_crit_hyst = i2c_smbus_read_byte_data(client,
232 TMP401_TEMP_CRIT_HYST);
233
234 data->last_updated = jiffies;
235 data->valid = 1;
236 }
237
238 mutex_unlock(&data->update_lock);
239
240 return data;
241}
242
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200243static ssize_t show_temp_value(struct device *dev,
244 struct device_attribute *devattr, char *buf)
245{
246 int index = to_sensor_dev_attr(devattr)->index;
247 struct tmp401_data *data = tmp401_update_device(dev);
248
249 return sprintf(buf, "%d\n",
250 tmp401_register_to_temp(data->temp[index], data->config));
251}
252
253static ssize_t show_temp_min(struct device *dev,
254 struct device_attribute *devattr, char *buf)
255{
256 int index = to_sensor_dev_attr(devattr)->index;
257 struct tmp401_data *data = tmp401_update_device(dev);
258
259 return sprintf(buf, "%d\n",
260 tmp401_register_to_temp(data->temp_low[index], data->config));
261}
262
263static ssize_t show_temp_max(struct device *dev,
264 struct device_attribute *devattr, char *buf)
265{
266 int index = to_sensor_dev_attr(devattr)->index;
267 struct tmp401_data *data = tmp401_update_device(dev);
268
269 return sprintf(buf, "%d\n",
270 tmp401_register_to_temp(data->temp_high[index], data->config));
271}
272
273static ssize_t show_temp_crit(struct device *dev,
274 struct device_attribute *devattr, char *buf)
275{
276 int index = to_sensor_dev_attr(devattr)->index;
277 struct tmp401_data *data = tmp401_update_device(dev);
278
279 return sprintf(buf, "%d\n",
280 tmp401_crit_register_to_temp(data->temp_crit[index],
281 data->config));
282}
283
284static ssize_t show_temp_crit_hyst(struct device *dev,
285 struct device_attribute *devattr, char *buf)
286{
287 int temp, index = to_sensor_dev_attr(devattr)->index;
288 struct tmp401_data *data = tmp401_update_device(dev);
289
290 mutex_lock(&data->update_lock);
291 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
292 data->config);
293 temp -= data->temp_crit_hyst * 1000;
294 mutex_unlock(&data->update_lock);
295
296 return sprintf(buf, "%d\n", temp);
297}
298
Andre Prendelfce07582009-06-15 18:39:47 +0200299static ssize_t show_temp_lowest(struct device *dev,
300 struct device_attribute *devattr, char *buf)
301{
302 int index = to_sensor_dev_attr(devattr)->index;
303 struct tmp401_data *data = tmp401_update_device(dev);
304
305 return sprintf(buf, "%d\n",
306 tmp401_register_to_temp(data->temp_lowest[index],
307 data->config));
308}
309
310static ssize_t show_temp_highest(struct device *dev,
311 struct device_attribute *devattr, char *buf)
312{
313 int index = to_sensor_dev_attr(devattr)->index;
314 struct tmp401_data *data = tmp401_update_device(dev);
315
316 return sprintf(buf, "%d\n",
317 tmp401_register_to_temp(data->temp_highest[index],
318 data->config));
319}
320
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200321static ssize_t show_status(struct device *dev,
322 struct device_attribute *devattr, char *buf)
323{
324 int mask = to_sensor_dev_attr(devattr)->index;
325 struct tmp401_data *data = tmp401_update_device(dev);
326
327 if (data->status & mask)
328 return sprintf(buf, "1\n");
329 else
330 return sprintf(buf, "0\n");
331}
332
333static ssize_t store_temp_min(struct device *dev, struct device_attribute
334 *devattr, const char *buf, size_t count)
335{
336 int index = to_sensor_dev_attr(devattr)->index;
337 struct tmp401_data *data = tmp401_update_device(dev);
338 long val;
339 u16 reg;
340
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100341 if (kstrtol(buf, 10, &val))
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200342 return -EINVAL;
343
344 reg = tmp401_temp_to_register(val, data->config);
345
346 mutex_lock(&data->update_lock);
347
348 i2c_smbus_write_byte_data(to_i2c_client(dev),
349 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8);
350 i2c_smbus_write_byte_data(to_i2c_client(dev),
351 TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF);
352
353 data->temp_low[index] = reg;
354
355 mutex_unlock(&data->update_lock);
356
357 return count;
358}
359
360static ssize_t store_temp_max(struct device *dev, struct device_attribute
361 *devattr, const char *buf, size_t count)
362{
363 int index = to_sensor_dev_attr(devattr)->index;
364 struct tmp401_data *data = tmp401_update_device(dev);
365 long val;
366 u16 reg;
367
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100368 if (kstrtol(buf, 10, &val))
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200369 return -EINVAL;
370
371 reg = tmp401_temp_to_register(val, data->config);
372
373 mutex_lock(&data->update_lock);
374
375 i2c_smbus_write_byte_data(to_i2c_client(dev),
376 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8);
377 i2c_smbus_write_byte_data(to_i2c_client(dev),
378 TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF);
379
380 data->temp_high[index] = reg;
381
382 mutex_unlock(&data->update_lock);
383
384 return count;
385}
386
387static ssize_t store_temp_crit(struct device *dev, struct device_attribute
388 *devattr, const char *buf, size_t count)
389{
390 int index = to_sensor_dev_attr(devattr)->index;
391 struct tmp401_data *data = tmp401_update_device(dev);
392 long val;
393 u8 reg;
394
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100395 if (kstrtol(buf, 10, &val))
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200396 return -EINVAL;
397
398 reg = tmp401_crit_temp_to_register(val, data->config);
399
400 mutex_lock(&data->update_lock);
401
402 i2c_smbus_write_byte_data(to_i2c_client(dev),
403 TMP401_TEMP_CRIT_LIMIT[index], reg);
404
405 data->temp_crit[index] = reg;
406
407 mutex_unlock(&data->update_lock);
408
409 return count;
410}
411
412static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
413 *devattr, const char *buf, size_t count)
414{
415 int temp, index = to_sensor_dev_attr(devattr)->index;
416 struct tmp401_data *data = tmp401_update_device(dev);
417 long val;
418 u8 reg;
419
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100420 if (kstrtol(buf, 10, &val))
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200421 return -EINVAL;
422
423 if (data->config & TMP401_CONFIG_RANGE)
Guenter Roeck2a844c12013-01-09 08:09:34 -0800424 val = clamp_val(val, -64000, 191000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200425 else
Guenter Roeck2a844c12013-01-09 08:09:34 -0800426 val = clamp_val(val, 0, 127000);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200427
428 mutex_lock(&data->update_lock);
429 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
430 data->config);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800431 val = clamp_val(val, temp - 255000, temp);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200432 reg = ((temp - val) + 500) / 1000;
433
434 i2c_smbus_write_byte_data(to_i2c_client(dev),
435 TMP401_TEMP_CRIT_HYST, reg);
436
437 data->temp_crit_hyst = reg;
438
439 mutex_unlock(&data->update_lock);
440
441 return count;
442}
443
Andre Prendelfce07582009-06-15 18:39:47 +0200444/*
445 * Resets the historical measurements of minimum and maximum temperatures.
446 * This is done by writing any value to any of the minimum/maximum registers
447 * (0x30-0x37).
448 */
449static ssize_t reset_temp_history(struct device *dev,
450 struct device_attribute *devattr, const char *buf, size_t count)
451{
452 long val;
453
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100454 if (kstrtol(buf, 10, &val))
Andre Prendelfce07582009-06-15 18:39:47 +0200455 return -EINVAL;
456
457 if (val != 1) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800458 dev_err(dev,
459 "temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
460 val);
Andre Prendelfce07582009-06-15 18:39:47 +0200461 return -EINVAL;
462 }
463 i2c_smbus_write_byte_data(to_i2c_client(dev),
464 TMP411_TEMP_LOWEST_MSB[0], val);
465
466 return count;
467}
468
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200469static struct sensor_device_attribute tmp401_attr[] = {
Andre Prendel2b76d802010-05-27 19:58:48 +0200470 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0),
471 SENSOR_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
472 store_temp_min, 0),
473 SENSOR_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
474 store_temp_max, 0),
475 SENSOR_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit,
476 store_temp_crit, 0),
477 SENSOR_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_crit_hyst,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200478 store_temp_crit_hyst, 0),
Andre Prendel2b76d802010-05-27 19:58:48 +0200479 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_status, NULL,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200480 TMP401_STATUS_LOCAL_LOW),
Andre Prendel2b76d802010-05-27 19:58:48 +0200481 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_status, NULL,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200482 TMP401_STATUS_LOCAL_HIGH),
Andre Prendel2b76d802010-05-27 19:58:48 +0200483 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_status, NULL,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200484 TMP401_STATUS_LOCAL_CRIT),
Andre Prendel2b76d802010-05-27 19:58:48 +0200485 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1),
486 SENSOR_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
487 store_temp_min, 1),
488 SENSOR_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
489 store_temp_max, 1),
490 SENSOR_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit,
491 store_temp_crit, 1),
492 SENSOR_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 1),
493 SENSOR_ATTR(temp2_fault, S_IRUGO, show_status, NULL,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200494 TMP401_STATUS_REMOTE_OPEN),
Andre Prendel2b76d802010-05-27 19:58:48 +0200495 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_status, NULL,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200496 TMP401_STATUS_REMOTE_LOW),
Andre Prendel2b76d802010-05-27 19:58:48 +0200497 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_status, NULL,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200498 TMP401_STATUS_REMOTE_HIGH),
Andre Prendel2b76d802010-05-27 19:58:48 +0200499 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_status, NULL,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200500 TMP401_STATUS_REMOTE_CRIT),
501};
502
503/*
Andre Prendelfce07582009-06-15 18:39:47 +0200504 * Additional features of the TMP411 chip.
505 * The TMP411 stores the minimum and maximum
506 * temperature measured since power-on, chip-reset, or
507 * minimum and maximum register reset for both the local
508 * and remote channels.
509 */
510static struct sensor_device_attribute tmp411_attr[] = {
Andre Prendel2b76d802010-05-27 19:58:48 +0200511 SENSOR_ATTR(temp1_highest, S_IRUGO, show_temp_highest, NULL, 0),
512 SENSOR_ATTR(temp1_lowest, S_IRUGO, show_temp_lowest, NULL, 0),
513 SENSOR_ATTR(temp2_highest, S_IRUGO, show_temp_highest, NULL, 1),
514 SENSOR_ATTR(temp2_lowest, S_IRUGO, show_temp_lowest, NULL, 1),
515 SENSOR_ATTR(temp_reset_history, S_IWUSR, NULL, reset_temp_history, 0),
Andre Prendelfce07582009-06-15 18:39:47 +0200516};
517
518/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200519 * Begin non sysfs callback code (aka Real code)
520 */
521
522static void tmp401_init_client(struct i2c_client *client)
523{
524 int config, config_orig;
525
526 /* Set the conversion rate to 2 Hz */
527 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
528
529 /* Start conversions (disable shutdown if necessary) */
530 config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
531 if (config < 0) {
532 dev_warn(&client->dev, "Initialization failed!\n");
533 return;
534 }
535
536 config_orig = config;
537 config &= ~TMP401_CONFIG_SHUTDOWN;
538
539 if (config != config_orig)
540 i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
541}
542
Jean Delvare310ec792009-12-14 21:17:23 +0100543static int tmp401_detect(struct i2c_client *client,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200544 struct i2c_board_info *info)
545{
Jean Delvaredbe73c82009-12-09 20:35:54 +0100546 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200547 struct i2c_adapter *adapter = client->adapter;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100548 u8 reg;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200549
550 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
551 return -ENODEV;
552
553 /* Detect and identify the chip */
Jean Delvaredbe73c82009-12-09 20:35:54 +0100554 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
555 if (reg != TMP401_MANUFACTURER_ID)
556 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200557
Jean Delvaredbe73c82009-12-09 20:35:54 +0100558 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200559
Jean Delvaredbe73c82009-12-09 20:35:54 +0100560 switch (reg) {
561 case TMP401_DEVICE_ID:
Guenter Roecka1fac922013-03-15 12:55:08 -0700562 if (client->addr != 0x4c)
563 return -ENODEV;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100564 kind = tmp401;
565 break;
Guenter Roeck4ce5b1f2013-03-29 17:56:07 -0700566 case TMP411A_DEVICE_ID:
567 if (client->addr != 0x4c)
568 return -ENODEV;
569 kind = tmp411;
570 break;
571 case TMP411B_DEVICE_ID:
572 if (client->addr != 0x4d)
573 return -ENODEV;
574 kind = tmp411;
575 break;
576 case TMP411C_DEVICE_ID:
577 if (client->addr != 0x4e)
578 return -ENODEV;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100579 kind = tmp411;
580 break;
Guenter Roecka1fac922013-03-15 12:55:08 -0700581 case TMP431_DEVICE_ID:
582 if (client->addr == 0x4e)
583 return -ENODEV;
584 kind = tmp431;
585 break;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100586 default:
587 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200588 }
Jean Delvaredbe73c82009-12-09 20:35:54 +0100589
590 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
591 if (reg & 0x1b)
592 return -ENODEV;
593
594 reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
595 /* Datasheet says: 0x1-0x6 */
596 if (reg > 15)
597 return -ENODEV;
598
Jean Delvaredc71afe2010-03-05 22:17:26 +0100599 strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200600
601 return 0;
602}
603
Andre Prendelea63c2b2010-05-27 19:58:49 +0200604static int tmp401_remove(struct i2c_client *client)
605{
606 struct tmp401_data *data = i2c_get_clientdata(client);
607 int i;
608
609 if (data->hwmon_dev)
610 hwmon_device_unregister(data->hwmon_dev);
611
612 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++)
613 device_remove_file(&client->dev, &tmp401_attr[i].dev_attr);
614
615 if (data->kind == tmp411) {
616 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++)
617 device_remove_file(&client->dev,
618 &tmp411_attr[i].dev_attr);
619 }
620
Andre Prendelea63c2b2010-05-27 19:58:49 +0200621 return 0;
622}
623
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200624static int tmp401_probe(struct i2c_client *client,
625 const struct i2c_device_id *id)
626{
627 int i, err = 0;
628 struct tmp401_data *data;
Guenter Roecka1fac922013-03-15 12:55:08 -0700629 const char *names[] = { "TMP401", "TMP411", "TMP431" };
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200630
Guenter Roeck0df9fc72012-06-02 11:35:53 -0700631 data = devm_kzalloc(&client->dev, sizeof(struct tmp401_data),
632 GFP_KERNEL);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200633 if (!data)
634 return -ENOMEM;
635
636 i2c_set_clientdata(client, data);
637 mutex_init(&data->update_lock);
Andre Prendelfce07582009-06-15 18:39:47 +0200638 data->kind = id->driver_data;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200639
640 /* Initialize the TMP401 chip */
641 tmp401_init_client(client);
642
643 /* Register sysfs hooks */
644 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) {
645 err = device_create_file(&client->dev,
646 &tmp401_attr[i].dev_attr);
647 if (err)
648 goto exit_remove;
649 }
650
Justin P. Mattocka80581d2012-02-11 05:55:58 -0800651 /* Register additional tmp411 sysfs hooks */
Andre Prendelfce07582009-06-15 18:39:47 +0200652 if (data->kind == tmp411) {
653 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) {
654 err = device_create_file(&client->dev,
655 &tmp411_attr[i].dev_attr);
656 if (err)
657 goto exit_remove;
658 }
659 }
660
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200661 data->hwmon_dev = hwmon_device_register(&client->dev);
662 if (IS_ERR(data->hwmon_dev)) {
663 err = PTR_ERR(data->hwmon_dev);
664 data->hwmon_dev = NULL;
665 goto exit_remove;
666 }
667
Jean Delvaredc71afe2010-03-05 22:17:26 +0100668 dev_info(&client->dev, "Detected TI %s chip\n", names[data->kind]);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200669
670 return 0;
671
672exit_remove:
Guenter Roeck0df9fc72012-06-02 11:35:53 -0700673 tmp401_remove(client);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200674 return err;
675}
676
Andre Prendelea63c2b2010-05-27 19:58:49 +0200677static struct i2c_driver tmp401_driver = {
678 .class = I2C_CLASS_HWMON,
679 .driver = {
680 .name = "tmp401",
681 },
682 .probe = tmp401_probe,
683 .remove = tmp401_remove,
684 .id_table = tmp401_id,
685 .detect = tmp401_detect,
686 .address_list = normal_i2c,
687};
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200688
Axel Linf0967ee2012-01-20 15:38:18 +0800689module_i2c_driver(tmp401_driver);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200690
691MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
692MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
693MODULE_LICENSE("GPL");