blob: ed086491cc9af9090daee76907c1b1caccb90f5c [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 */
43static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
44
45/* Insmod parameters */
Andre Prendelfce07582009-06-15 18:39:47 +020046I2C_CLIENT_INSMOD_2(tmp401, tmp411);
Hans de Goedeab2b79d2009-06-15 18:39:46 +020047
48/*
49 * The TMP401 registers, note some registers have different addresses for
50 * reading and writing
51 */
52#define TMP401_STATUS 0x02
53#define TMP401_CONFIG_READ 0x03
54#define TMP401_CONFIG_WRITE 0x09
55#define TMP401_CONVERSION_RATE_READ 0x04
56#define TMP401_CONVERSION_RATE_WRITE 0x0A
57#define TMP401_TEMP_CRIT_HYST 0x21
58#define TMP401_CONSECUTIVE_ALERT 0x22
59#define TMP401_MANUFACTURER_ID_REG 0xFE
60#define TMP401_DEVICE_ID_REG 0xFF
Andre Prendelfce07582009-06-15 18:39:47 +020061#define TMP411_N_FACTOR_REG 0x18
Hans de Goedeab2b79d2009-06-15 18:39:46 +020062
63static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 };
64static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 };
65static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2] = { 0x06, 0x08 };
66static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2] = { 0x0C, 0x0E };
67static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2] = { 0x17, 0x14 };
68static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2] = { 0x05, 0x07 };
69static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2] = { 0x0B, 0x0D };
70static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 };
71/* These are called the THERM limit / hysteresis / mask in the datasheet */
72static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 };
73
Andre Prendelfce07582009-06-15 18:39:47 +020074static const u8 TMP411_TEMP_LOWEST_MSB[2] = { 0x30, 0x34 };
75static const u8 TMP411_TEMP_LOWEST_LSB[2] = { 0x31, 0x35 };
76static const u8 TMP411_TEMP_HIGHEST_MSB[2] = { 0x32, 0x36 };
77static const u8 TMP411_TEMP_HIGHEST_LSB[2] = { 0x33, 0x37 };
78
Hans de Goedeab2b79d2009-06-15 18:39:46 +020079/* Flags */
80#define TMP401_CONFIG_RANGE 0x04
81#define TMP401_CONFIG_SHUTDOWN 0x40
82#define TMP401_STATUS_LOCAL_CRIT 0x01
83#define TMP401_STATUS_REMOTE_CRIT 0x02
84#define TMP401_STATUS_REMOTE_OPEN 0x04
85#define TMP401_STATUS_REMOTE_LOW 0x08
86#define TMP401_STATUS_REMOTE_HIGH 0x10
87#define TMP401_STATUS_LOCAL_LOW 0x20
88#define TMP401_STATUS_LOCAL_HIGH 0x40
89
90/* Manufacturer / Device ID's */
91#define TMP401_MANUFACTURER_ID 0x55
92#define TMP401_DEVICE_ID 0x11
Andre Prendelfce07582009-06-15 18:39:47 +020093#define TMP411_DEVICE_ID 0x12
Hans de Goedeab2b79d2009-06-15 18:39:46 +020094
95/*
96 * Functions declarations
97 */
98
99static int tmp401_probe(struct i2c_client *client,
100 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100101static int tmp401_detect(struct i2c_client *client,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200102 struct i2c_board_info *info);
103static int tmp401_remove(struct i2c_client *client);
104static struct tmp401_data *tmp401_update_device(struct device *dev);
105
106/*
107 * Driver data (common to all clients)
108 */
109
110static const struct i2c_device_id tmp401_id[] = {
111 { "tmp401", tmp401 },
Andre Prendelfce07582009-06-15 18:39:47 +0200112 { "tmp411", tmp411 },
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200113 { }
114};
115MODULE_DEVICE_TABLE(i2c, tmp401_id);
116
117static struct i2c_driver tmp401_driver = {
118 .class = I2C_CLASS_HWMON,
119 .driver = {
120 .name = "tmp401",
121 },
122 .probe = tmp401_probe,
123 .remove = tmp401_remove,
124 .id_table = tmp401_id,
125 .detect = tmp401_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100126 .address_list = normal_i2c,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200127};
128
129/*
130 * Client data (each client gets its own)
131 */
132
133struct tmp401_data {
134 struct device *hwmon_dev;
135 struct mutex update_lock;
136 char valid; /* zero until following fields are valid */
137 unsigned long last_updated; /* in jiffies */
Andre Prendelfce07582009-06-15 18:39:47 +0200138 int kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200139
140 /* register values */
141 u8 status;
142 u8 config;
143 u16 temp[2];
144 u16 temp_low[2];
145 u16 temp_high[2];
146 u8 temp_crit[2];
147 u8 temp_crit_hyst;
Andre Prendelfce07582009-06-15 18:39:47 +0200148 u16 temp_lowest[2];
149 u16 temp_highest[2];
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200150};
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
163 return (temp * 625 + 80) / 160;
164}
165
166static u16 tmp401_temp_to_register(long temp, u8 config)
167{
168 if (config & TMP401_CONFIG_RANGE) {
169 temp = SENSORS_LIMIT(temp, -64000, 191000);
170 temp += 64000;
171 } else
172 temp = SENSORS_LIMIT(temp, 0, 127000);
173
174 return (temp * 160 + 312) / 625;
175}
176
177static int tmp401_crit_register_to_temp(u8 reg, u8 config)
178{
179 int temp = reg;
180
181 if (config & TMP401_CONFIG_RANGE)
182 temp -= 64;
183
184 return temp * 1000;
185}
186
187static u8 tmp401_crit_temp_to_register(long temp, u8 config)
188{
189 if (config & TMP401_CONFIG_RANGE) {
190 temp = SENSORS_LIMIT(temp, -64000, 191000);
191 temp += 64000;
192 } else
193 temp = SENSORS_LIMIT(temp, 0, 127000);
194
195 return (temp + 500) / 1000;
196}
197
198static ssize_t show_temp_value(struct device *dev,
199 struct device_attribute *devattr, char *buf)
200{
201 int index = to_sensor_dev_attr(devattr)->index;
202 struct tmp401_data *data = tmp401_update_device(dev);
203
204 return sprintf(buf, "%d\n",
205 tmp401_register_to_temp(data->temp[index], data->config));
206}
207
208static ssize_t show_temp_min(struct device *dev,
209 struct device_attribute *devattr, char *buf)
210{
211 int index = to_sensor_dev_attr(devattr)->index;
212 struct tmp401_data *data = tmp401_update_device(dev);
213
214 return sprintf(buf, "%d\n",
215 tmp401_register_to_temp(data->temp_low[index], data->config));
216}
217
218static ssize_t show_temp_max(struct device *dev,
219 struct device_attribute *devattr, char *buf)
220{
221 int index = to_sensor_dev_attr(devattr)->index;
222 struct tmp401_data *data = tmp401_update_device(dev);
223
224 return sprintf(buf, "%d\n",
225 tmp401_register_to_temp(data->temp_high[index], data->config));
226}
227
228static ssize_t show_temp_crit(struct device *dev,
229 struct device_attribute *devattr, char *buf)
230{
231 int index = to_sensor_dev_attr(devattr)->index;
232 struct tmp401_data *data = tmp401_update_device(dev);
233
234 return sprintf(buf, "%d\n",
235 tmp401_crit_register_to_temp(data->temp_crit[index],
236 data->config));
237}
238
239static ssize_t show_temp_crit_hyst(struct device *dev,
240 struct device_attribute *devattr, char *buf)
241{
242 int temp, index = to_sensor_dev_attr(devattr)->index;
243 struct tmp401_data *data = tmp401_update_device(dev);
244
245 mutex_lock(&data->update_lock);
246 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
247 data->config);
248 temp -= data->temp_crit_hyst * 1000;
249 mutex_unlock(&data->update_lock);
250
251 return sprintf(buf, "%d\n", temp);
252}
253
Andre Prendelfce07582009-06-15 18:39:47 +0200254static ssize_t show_temp_lowest(struct device *dev,
255 struct device_attribute *devattr, char *buf)
256{
257 int index = to_sensor_dev_attr(devattr)->index;
258 struct tmp401_data *data = tmp401_update_device(dev);
259
260 return sprintf(buf, "%d\n",
261 tmp401_register_to_temp(data->temp_lowest[index],
262 data->config));
263}
264
265static ssize_t show_temp_highest(struct device *dev,
266 struct device_attribute *devattr, char *buf)
267{
268 int index = to_sensor_dev_attr(devattr)->index;
269 struct tmp401_data *data = tmp401_update_device(dev);
270
271 return sprintf(buf, "%d\n",
272 tmp401_register_to_temp(data->temp_highest[index],
273 data->config));
274}
275
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200276static ssize_t show_status(struct device *dev,
277 struct device_attribute *devattr, char *buf)
278{
279 int mask = to_sensor_dev_attr(devattr)->index;
280 struct tmp401_data *data = tmp401_update_device(dev);
281
282 if (data->status & mask)
283 return sprintf(buf, "1\n");
284 else
285 return sprintf(buf, "0\n");
286}
287
288static ssize_t store_temp_min(struct device *dev, struct device_attribute
289 *devattr, const char *buf, size_t count)
290{
291 int index = to_sensor_dev_attr(devattr)->index;
292 struct tmp401_data *data = tmp401_update_device(dev);
293 long val;
294 u16 reg;
295
296 if (strict_strtol(buf, 10, &val))
297 return -EINVAL;
298
299 reg = tmp401_temp_to_register(val, data->config);
300
301 mutex_lock(&data->update_lock);
302
303 i2c_smbus_write_byte_data(to_i2c_client(dev),
304 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8);
305 i2c_smbus_write_byte_data(to_i2c_client(dev),
306 TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF);
307
308 data->temp_low[index] = reg;
309
310 mutex_unlock(&data->update_lock);
311
312 return count;
313}
314
315static ssize_t store_temp_max(struct device *dev, struct device_attribute
316 *devattr, const char *buf, size_t count)
317{
318 int index = to_sensor_dev_attr(devattr)->index;
319 struct tmp401_data *data = tmp401_update_device(dev);
320 long val;
321 u16 reg;
322
323 if (strict_strtol(buf, 10, &val))
324 return -EINVAL;
325
326 reg = tmp401_temp_to_register(val, data->config);
327
328 mutex_lock(&data->update_lock);
329
330 i2c_smbus_write_byte_data(to_i2c_client(dev),
331 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8);
332 i2c_smbus_write_byte_data(to_i2c_client(dev),
333 TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF);
334
335 data->temp_high[index] = reg;
336
337 mutex_unlock(&data->update_lock);
338
339 return count;
340}
341
342static ssize_t store_temp_crit(struct device *dev, struct device_attribute
343 *devattr, const char *buf, size_t count)
344{
345 int index = to_sensor_dev_attr(devattr)->index;
346 struct tmp401_data *data = tmp401_update_device(dev);
347 long val;
348 u8 reg;
349
350 if (strict_strtol(buf, 10, &val))
351 return -EINVAL;
352
353 reg = tmp401_crit_temp_to_register(val, data->config);
354
355 mutex_lock(&data->update_lock);
356
357 i2c_smbus_write_byte_data(to_i2c_client(dev),
358 TMP401_TEMP_CRIT_LIMIT[index], reg);
359
360 data->temp_crit[index] = reg;
361
362 mutex_unlock(&data->update_lock);
363
364 return count;
365}
366
367static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
368 *devattr, const char *buf, size_t count)
369{
370 int temp, index = to_sensor_dev_attr(devattr)->index;
371 struct tmp401_data *data = tmp401_update_device(dev);
372 long val;
373 u8 reg;
374
375 if (strict_strtol(buf, 10, &val))
376 return -EINVAL;
377
378 if (data->config & TMP401_CONFIG_RANGE)
379 val = SENSORS_LIMIT(val, -64000, 191000);
380 else
381 val = SENSORS_LIMIT(val, 0, 127000);
382
383 mutex_lock(&data->update_lock);
384 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
385 data->config);
386 val = SENSORS_LIMIT(val, temp - 255000, temp);
387 reg = ((temp - val) + 500) / 1000;
388
389 i2c_smbus_write_byte_data(to_i2c_client(dev),
390 TMP401_TEMP_CRIT_HYST, reg);
391
392 data->temp_crit_hyst = reg;
393
394 mutex_unlock(&data->update_lock);
395
396 return count;
397}
398
Andre Prendelfce07582009-06-15 18:39:47 +0200399/*
400 * Resets the historical measurements of minimum and maximum temperatures.
401 * This is done by writing any value to any of the minimum/maximum registers
402 * (0x30-0x37).
403 */
404static ssize_t reset_temp_history(struct device *dev,
405 struct device_attribute *devattr, const char *buf, size_t count)
406{
407 long val;
408
409 if (strict_strtol(buf, 10, &val))
410 return -EINVAL;
411
412 if (val != 1) {
413 dev_err(dev, "temp_reset_history value %ld not"
414 " supported. Use 1 to reset the history!\n", val);
415 return -EINVAL;
416 }
417 i2c_smbus_write_byte_data(to_i2c_client(dev),
418 TMP411_TEMP_LOWEST_MSB[0], val);
419
420 return count;
421}
422
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200423static struct sensor_device_attribute tmp401_attr[] = {
424 SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
425 SENSOR_ATTR(temp1_min, 0644, show_temp_min, store_temp_min, 0),
426 SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
427 SENSOR_ATTR(temp1_crit, 0644, show_temp_crit, store_temp_crit, 0),
428 SENSOR_ATTR(temp1_crit_hyst, 0644, show_temp_crit_hyst,
429 store_temp_crit_hyst, 0),
430 SENSOR_ATTR(temp1_min_alarm, 0444, show_status, NULL,
431 TMP401_STATUS_LOCAL_LOW),
432 SENSOR_ATTR(temp1_max_alarm, 0444, show_status, NULL,
433 TMP401_STATUS_LOCAL_HIGH),
434 SENSOR_ATTR(temp1_crit_alarm, 0444, show_status, NULL,
435 TMP401_STATUS_LOCAL_CRIT),
436 SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
437 SENSOR_ATTR(temp2_min, 0644, show_temp_min, store_temp_min, 1),
438 SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
439 SENSOR_ATTR(temp2_crit, 0644, show_temp_crit, store_temp_crit, 1),
440 SENSOR_ATTR(temp2_crit_hyst, 0444, show_temp_crit_hyst, NULL, 1),
441 SENSOR_ATTR(temp2_fault, 0444, show_status, NULL,
442 TMP401_STATUS_REMOTE_OPEN),
443 SENSOR_ATTR(temp2_min_alarm, 0444, show_status, NULL,
444 TMP401_STATUS_REMOTE_LOW),
445 SENSOR_ATTR(temp2_max_alarm, 0444, show_status, NULL,
446 TMP401_STATUS_REMOTE_HIGH),
447 SENSOR_ATTR(temp2_crit_alarm, 0444, show_status, NULL,
448 TMP401_STATUS_REMOTE_CRIT),
449};
450
451/*
Andre Prendelfce07582009-06-15 18:39:47 +0200452 * Additional features of the TMP411 chip.
453 * The TMP411 stores the minimum and maximum
454 * temperature measured since power-on, chip-reset, or
455 * minimum and maximum register reset for both the local
456 * and remote channels.
457 */
458static struct sensor_device_attribute tmp411_attr[] = {
459 SENSOR_ATTR(temp1_highest, 0444, show_temp_highest, NULL, 0),
460 SENSOR_ATTR(temp1_lowest, 0444, show_temp_lowest, NULL, 0),
461 SENSOR_ATTR(temp2_highest, 0444, show_temp_highest, NULL, 1),
462 SENSOR_ATTR(temp2_lowest, 0444, show_temp_lowest, NULL, 1),
463 SENSOR_ATTR(temp_reset_history, 0200, NULL, reset_temp_history, 0),
464};
465
466/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200467 * Begin non sysfs callback code (aka Real code)
468 */
469
470static void tmp401_init_client(struct i2c_client *client)
471{
472 int config, config_orig;
473
474 /* Set the conversion rate to 2 Hz */
475 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
476
477 /* Start conversions (disable shutdown if necessary) */
478 config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
479 if (config < 0) {
480 dev_warn(&client->dev, "Initialization failed!\n");
481 return;
482 }
483
484 config_orig = config;
485 config &= ~TMP401_CONFIG_SHUTDOWN;
486
487 if (config != config_orig)
488 i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
489}
490
Jean Delvare310ec792009-12-14 21:17:23 +0100491static int tmp401_detect(struct i2c_client *client,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200492 struct i2c_board_info *info)
493{
Jean Delvaredbe73c82009-12-09 20:35:54 +0100494 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200495 struct i2c_adapter *adapter = client->adapter;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100496 u8 reg;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200497
498 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
499 return -ENODEV;
500
501 /* Detect and identify the chip */
Jean Delvaredbe73c82009-12-09 20:35:54 +0100502 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
503 if (reg != TMP401_MANUFACTURER_ID)
504 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200505
Jean Delvaredbe73c82009-12-09 20:35:54 +0100506 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200507
Jean Delvaredbe73c82009-12-09 20:35:54 +0100508 switch (reg) {
509 case TMP401_DEVICE_ID:
510 kind = tmp401;
511 break;
512 case TMP411_DEVICE_ID:
513 kind = tmp411;
514 break;
515 default:
516 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200517 }
Jean Delvaredbe73c82009-12-09 20:35:54 +0100518
519 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
520 if (reg & 0x1b)
521 return -ENODEV;
522
523 reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
524 /* Datasheet says: 0x1-0x6 */
525 if (reg > 15)
526 return -ENODEV;
527
Andre Prendelfce07582009-06-15 18:39:47 +0200528 strlcpy(info->type, tmp401_id[kind - 1].name, I2C_NAME_SIZE);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200529
530 return 0;
531}
532
533static int tmp401_probe(struct i2c_client *client,
534 const struct i2c_device_id *id)
535{
536 int i, err = 0;
537 struct tmp401_data *data;
Andre Prendelfce07582009-06-15 18:39:47 +0200538 const char *names[] = { "TMP401", "TMP411" };
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200539
540 data = kzalloc(sizeof(struct tmp401_data), GFP_KERNEL);
541 if (!data)
542 return -ENOMEM;
543
544 i2c_set_clientdata(client, data);
545 mutex_init(&data->update_lock);
Andre Prendelfce07582009-06-15 18:39:47 +0200546 data->kind = id->driver_data;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200547
548 /* Initialize the TMP401 chip */
549 tmp401_init_client(client);
550
551 /* Register sysfs hooks */
552 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) {
553 err = device_create_file(&client->dev,
554 &tmp401_attr[i].dev_attr);
555 if (err)
556 goto exit_remove;
557 }
558
Andre Prendelfce07582009-06-15 18:39:47 +0200559 /* Register aditional tmp411 sysfs hooks */
560 if (data->kind == tmp411) {
561 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) {
562 err = device_create_file(&client->dev,
563 &tmp411_attr[i].dev_attr);
564 if (err)
565 goto exit_remove;
566 }
567 }
568
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200569 data->hwmon_dev = hwmon_device_register(&client->dev);
570 if (IS_ERR(data->hwmon_dev)) {
571 err = PTR_ERR(data->hwmon_dev);
572 data->hwmon_dev = NULL;
573 goto exit_remove;
574 }
575
Andre Prendelfce07582009-06-15 18:39:47 +0200576 dev_info(&client->dev, "Detected TI %s chip\n",
577 names[data->kind - 1]);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200578
579 return 0;
580
581exit_remove:
582 tmp401_remove(client); /* will also free data for us */
583 return err;
584}
585
586static int tmp401_remove(struct i2c_client *client)
587{
588 struct tmp401_data *data = i2c_get_clientdata(client);
589 int i;
590
591 if (data->hwmon_dev)
592 hwmon_device_unregister(data->hwmon_dev);
593
594 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++)
595 device_remove_file(&client->dev, &tmp401_attr[i].dev_attr);
596
Andre Prendelfce07582009-06-15 18:39:47 +0200597 if (data->kind == tmp411) {
598 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++)
599 device_remove_file(&client->dev,
600 &tmp411_attr[i].dev_attr);
601 }
602
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200603 kfree(data);
604 return 0;
605}
606
Andre Prendelfce07582009-06-15 18:39:47 +0200607static struct tmp401_data *tmp401_update_device_reg16(
608 struct i2c_client *client, struct tmp401_data *data)
609{
610 int i;
611
612 for (i = 0; i < 2; i++) {
613 /*
614 * High byte must be read first immediately followed
615 * by the low byte
616 */
617 data->temp[i] = i2c_smbus_read_byte_data(client,
618 TMP401_TEMP_MSB[i]) << 8;
619 data->temp[i] |= i2c_smbus_read_byte_data(client,
620 TMP401_TEMP_LSB[i]);
621 data->temp_low[i] = i2c_smbus_read_byte_data(client,
622 TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8;
623 data->temp_low[i] |= i2c_smbus_read_byte_data(client,
624 TMP401_TEMP_LOW_LIMIT_LSB[i]);
625 data->temp_high[i] = i2c_smbus_read_byte_data(client,
626 TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8;
627 data->temp_high[i] |= i2c_smbus_read_byte_data(client,
628 TMP401_TEMP_HIGH_LIMIT_LSB[i]);
629 data->temp_crit[i] = i2c_smbus_read_byte_data(client,
630 TMP401_TEMP_CRIT_LIMIT[i]);
631
632 if (data->kind == tmp411) {
633 data->temp_lowest[i] = i2c_smbus_read_byte_data(client,
634 TMP411_TEMP_LOWEST_MSB[i]) << 8;
635 data->temp_lowest[i] |= i2c_smbus_read_byte_data(
636 client, TMP411_TEMP_LOWEST_LSB[i]);
637
638 data->temp_highest[i] = i2c_smbus_read_byte_data(
639 client, TMP411_TEMP_HIGHEST_MSB[i]) << 8;
640 data->temp_highest[i] |= i2c_smbus_read_byte_data(
641 client, TMP411_TEMP_HIGHEST_LSB[i]);
642 }
643 }
644 return data;
645}
646
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200647static struct tmp401_data *tmp401_update_device(struct device *dev)
648{
649 struct i2c_client *client = to_i2c_client(dev);
650 struct tmp401_data *data = i2c_get_clientdata(client);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200651
652 mutex_lock(&data->update_lock);
653
654 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
655 data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS);
656 data->config = i2c_smbus_read_byte_data(client,
657 TMP401_CONFIG_READ);
Andre Prendelfce07582009-06-15 18:39:47 +0200658 tmp401_update_device_reg16(client, data);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200659
660 data->temp_crit_hyst = i2c_smbus_read_byte_data(client,
661 TMP401_TEMP_CRIT_HYST);
662
663 data->last_updated = jiffies;
664 data->valid = 1;
665 }
666
667 mutex_unlock(&data->update_lock);
668
669 return data;
670}
671
672static int __init tmp401_init(void)
673{
674 return i2c_add_driver(&tmp401_driver);
675}
676
677static void __exit tmp401_exit(void)
678{
679 i2c_del_driver(&tmp401_driver);
680}
681
682MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
683MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
684MODULE_LICENSE("GPL");
685
686module_init(tmp401_init);
687module_exit(tmp401_exit);