blob: a13b30e8d8d8f982f71ff5e22f42f1347e7f55ad [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
Jean Delvaree5e9f442009-12-14 21:17:27 +010045enum chips { tmp401, tmp411 };
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
Andre Prendelfce07582009-06-15 18:39:47 +020092#define TMP411_DEVICE_ID 0x12
Hans de Goedeab2b79d2009-06-15 18:39:46 +020093
94/*
95 * Functions declarations
96 */
97
98static int tmp401_probe(struct i2c_client *client,
99 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100100static int tmp401_detect(struct i2c_client *client,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200101 struct i2c_board_info *info);
102static int tmp401_remove(struct i2c_client *client);
103static struct tmp401_data *tmp401_update_device(struct device *dev);
104
105/*
106 * Driver data (common to all clients)
107 */
108
109static const struct i2c_device_id tmp401_id[] = {
110 { "tmp401", tmp401 },
Andre Prendelfce07582009-06-15 18:39:47 +0200111 { "tmp411", tmp411 },
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200112 { }
113};
114MODULE_DEVICE_TABLE(i2c, tmp401_id);
115
116static struct i2c_driver tmp401_driver = {
117 .class = I2C_CLASS_HWMON,
118 .driver = {
119 .name = "tmp401",
120 },
121 .probe = tmp401_probe,
122 .remove = tmp401_remove,
123 .id_table = tmp401_id,
124 .detect = tmp401_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100125 .address_list = normal_i2c,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200126};
127
128/*
129 * Client data (each client gets its own)
130 */
131
132struct tmp401_data {
133 struct device *hwmon_dev;
134 struct mutex update_lock;
135 char valid; /* zero until following fields are valid */
136 unsigned long last_updated; /* in jiffies */
Andre Prendelfce07582009-06-15 18:39:47 +0200137 int kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200138
139 /* register values */
140 u8 status;
141 u8 config;
142 u16 temp[2];
143 u16 temp_low[2];
144 u16 temp_high[2];
145 u8 temp_crit[2];
146 u8 temp_crit_hyst;
Andre Prendelfce07582009-06-15 18:39:47 +0200147 u16 temp_lowest[2];
148 u16 temp_highest[2];
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200149};
150
151/*
152 * Sysfs attr show / store functions
153 */
154
155static int tmp401_register_to_temp(u16 reg, u8 config)
156{
157 int temp = reg;
158
159 if (config & TMP401_CONFIG_RANGE)
160 temp -= 64 * 256;
161
162 return (temp * 625 + 80) / 160;
163}
164
165static u16 tmp401_temp_to_register(long temp, u8 config)
166{
167 if (config & TMP401_CONFIG_RANGE) {
168 temp = SENSORS_LIMIT(temp, -64000, 191000);
169 temp += 64000;
170 } else
171 temp = SENSORS_LIMIT(temp, 0, 127000);
172
173 return (temp * 160 + 312) / 625;
174}
175
176static int tmp401_crit_register_to_temp(u8 reg, u8 config)
177{
178 int temp = reg;
179
180 if (config & TMP401_CONFIG_RANGE)
181 temp -= 64;
182
183 return temp * 1000;
184}
185
186static u8 tmp401_crit_temp_to_register(long temp, u8 config)
187{
188 if (config & TMP401_CONFIG_RANGE) {
189 temp = SENSORS_LIMIT(temp, -64000, 191000);
190 temp += 64000;
191 } else
192 temp = SENSORS_LIMIT(temp, 0, 127000);
193
194 return (temp + 500) / 1000;
195}
196
197static ssize_t show_temp_value(struct device *dev,
198 struct device_attribute *devattr, char *buf)
199{
200 int index = to_sensor_dev_attr(devattr)->index;
201 struct tmp401_data *data = tmp401_update_device(dev);
202
203 return sprintf(buf, "%d\n",
204 tmp401_register_to_temp(data->temp[index], data->config));
205}
206
207static ssize_t show_temp_min(struct device *dev,
208 struct device_attribute *devattr, char *buf)
209{
210 int index = to_sensor_dev_attr(devattr)->index;
211 struct tmp401_data *data = tmp401_update_device(dev);
212
213 return sprintf(buf, "%d\n",
214 tmp401_register_to_temp(data->temp_low[index], data->config));
215}
216
217static ssize_t show_temp_max(struct device *dev,
218 struct device_attribute *devattr, char *buf)
219{
220 int index = to_sensor_dev_attr(devattr)->index;
221 struct tmp401_data *data = tmp401_update_device(dev);
222
223 return sprintf(buf, "%d\n",
224 tmp401_register_to_temp(data->temp_high[index], data->config));
225}
226
227static ssize_t show_temp_crit(struct device *dev,
228 struct device_attribute *devattr, char *buf)
229{
230 int index = to_sensor_dev_attr(devattr)->index;
231 struct tmp401_data *data = tmp401_update_device(dev);
232
233 return sprintf(buf, "%d\n",
234 tmp401_crit_register_to_temp(data->temp_crit[index],
235 data->config));
236}
237
238static ssize_t show_temp_crit_hyst(struct device *dev,
239 struct device_attribute *devattr, char *buf)
240{
241 int temp, index = to_sensor_dev_attr(devattr)->index;
242 struct tmp401_data *data = tmp401_update_device(dev);
243
244 mutex_lock(&data->update_lock);
245 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
246 data->config);
247 temp -= data->temp_crit_hyst * 1000;
248 mutex_unlock(&data->update_lock);
249
250 return sprintf(buf, "%d\n", temp);
251}
252
Andre Prendelfce07582009-06-15 18:39:47 +0200253static ssize_t show_temp_lowest(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_lowest[index],
261 data->config));
262}
263
264static ssize_t show_temp_highest(struct device *dev,
265 struct device_attribute *devattr, char *buf)
266{
267 int index = to_sensor_dev_attr(devattr)->index;
268 struct tmp401_data *data = tmp401_update_device(dev);
269
270 return sprintf(buf, "%d\n",
271 tmp401_register_to_temp(data->temp_highest[index],
272 data->config));
273}
274
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200275static ssize_t show_status(struct device *dev,
276 struct device_attribute *devattr, char *buf)
277{
278 int mask = to_sensor_dev_attr(devattr)->index;
279 struct tmp401_data *data = tmp401_update_device(dev);
280
281 if (data->status & mask)
282 return sprintf(buf, "1\n");
283 else
284 return sprintf(buf, "0\n");
285}
286
287static ssize_t store_temp_min(struct device *dev, struct device_attribute
288 *devattr, const char *buf, size_t count)
289{
290 int index = to_sensor_dev_attr(devattr)->index;
291 struct tmp401_data *data = tmp401_update_device(dev);
292 long val;
293 u16 reg;
294
295 if (strict_strtol(buf, 10, &val))
296 return -EINVAL;
297
298 reg = tmp401_temp_to_register(val, data->config);
299
300 mutex_lock(&data->update_lock);
301
302 i2c_smbus_write_byte_data(to_i2c_client(dev),
303 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8);
304 i2c_smbus_write_byte_data(to_i2c_client(dev),
305 TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF);
306
307 data->temp_low[index] = reg;
308
309 mutex_unlock(&data->update_lock);
310
311 return count;
312}
313
314static ssize_t store_temp_max(struct device *dev, struct device_attribute
315 *devattr, const char *buf, size_t count)
316{
317 int index = to_sensor_dev_attr(devattr)->index;
318 struct tmp401_data *data = tmp401_update_device(dev);
319 long val;
320 u16 reg;
321
322 if (strict_strtol(buf, 10, &val))
323 return -EINVAL;
324
325 reg = tmp401_temp_to_register(val, data->config);
326
327 mutex_lock(&data->update_lock);
328
329 i2c_smbus_write_byte_data(to_i2c_client(dev),
330 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8);
331 i2c_smbus_write_byte_data(to_i2c_client(dev),
332 TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF);
333
334 data->temp_high[index] = reg;
335
336 mutex_unlock(&data->update_lock);
337
338 return count;
339}
340
341static ssize_t store_temp_crit(struct device *dev, struct device_attribute
342 *devattr, const char *buf, size_t count)
343{
344 int index = to_sensor_dev_attr(devattr)->index;
345 struct tmp401_data *data = tmp401_update_device(dev);
346 long val;
347 u8 reg;
348
349 if (strict_strtol(buf, 10, &val))
350 return -EINVAL;
351
352 reg = tmp401_crit_temp_to_register(val, data->config);
353
354 mutex_lock(&data->update_lock);
355
356 i2c_smbus_write_byte_data(to_i2c_client(dev),
357 TMP401_TEMP_CRIT_LIMIT[index], reg);
358
359 data->temp_crit[index] = reg;
360
361 mutex_unlock(&data->update_lock);
362
363 return count;
364}
365
366static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
367 *devattr, const char *buf, size_t count)
368{
369 int temp, index = to_sensor_dev_attr(devattr)->index;
370 struct tmp401_data *data = tmp401_update_device(dev);
371 long val;
372 u8 reg;
373
374 if (strict_strtol(buf, 10, &val))
375 return -EINVAL;
376
377 if (data->config & TMP401_CONFIG_RANGE)
378 val = SENSORS_LIMIT(val, -64000, 191000);
379 else
380 val = SENSORS_LIMIT(val, 0, 127000);
381
382 mutex_lock(&data->update_lock);
383 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
384 data->config);
385 val = SENSORS_LIMIT(val, temp - 255000, temp);
386 reg = ((temp - val) + 500) / 1000;
387
388 i2c_smbus_write_byte_data(to_i2c_client(dev),
389 TMP401_TEMP_CRIT_HYST, reg);
390
391 data->temp_crit_hyst = reg;
392
393 mutex_unlock(&data->update_lock);
394
395 return count;
396}
397
Andre Prendelfce07582009-06-15 18:39:47 +0200398/*
399 * Resets the historical measurements of minimum and maximum temperatures.
400 * This is done by writing any value to any of the minimum/maximum registers
401 * (0x30-0x37).
402 */
403static ssize_t reset_temp_history(struct device *dev,
404 struct device_attribute *devattr, const char *buf, size_t count)
405{
406 long val;
407
408 if (strict_strtol(buf, 10, &val))
409 return -EINVAL;
410
411 if (val != 1) {
412 dev_err(dev, "temp_reset_history value %ld not"
413 " supported. Use 1 to reset the history!\n", val);
414 return -EINVAL;
415 }
416 i2c_smbus_write_byte_data(to_i2c_client(dev),
417 TMP411_TEMP_LOWEST_MSB[0], val);
418
419 return count;
420}
421
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200422static struct sensor_device_attribute tmp401_attr[] = {
423 SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
424 SENSOR_ATTR(temp1_min, 0644, show_temp_min, store_temp_min, 0),
425 SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
426 SENSOR_ATTR(temp1_crit, 0644, show_temp_crit, store_temp_crit, 0),
427 SENSOR_ATTR(temp1_crit_hyst, 0644, show_temp_crit_hyst,
428 store_temp_crit_hyst, 0),
429 SENSOR_ATTR(temp1_min_alarm, 0444, show_status, NULL,
430 TMP401_STATUS_LOCAL_LOW),
431 SENSOR_ATTR(temp1_max_alarm, 0444, show_status, NULL,
432 TMP401_STATUS_LOCAL_HIGH),
433 SENSOR_ATTR(temp1_crit_alarm, 0444, show_status, NULL,
434 TMP401_STATUS_LOCAL_CRIT),
435 SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
436 SENSOR_ATTR(temp2_min, 0644, show_temp_min, store_temp_min, 1),
437 SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
438 SENSOR_ATTR(temp2_crit, 0644, show_temp_crit, store_temp_crit, 1),
439 SENSOR_ATTR(temp2_crit_hyst, 0444, show_temp_crit_hyst, NULL, 1),
440 SENSOR_ATTR(temp2_fault, 0444, show_status, NULL,
441 TMP401_STATUS_REMOTE_OPEN),
442 SENSOR_ATTR(temp2_min_alarm, 0444, show_status, NULL,
443 TMP401_STATUS_REMOTE_LOW),
444 SENSOR_ATTR(temp2_max_alarm, 0444, show_status, NULL,
445 TMP401_STATUS_REMOTE_HIGH),
446 SENSOR_ATTR(temp2_crit_alarm, 0444, show_status, NULL,
447 TMP401_STATUS_REMOTE_CRIT),
448};
449
450/*
Andre Prendelfce07582009-06-15 18:39:47 +0200451 * Additional features of the TMP411 chip.
452 * The TMP411 stores the minimum and maximum
453 * temperature measured since power-on, chip-reset, or
454 * minimum and maximum register reset for both the local
455 * and remote channels.
456 */
457static struct sensor_device_attribute tmp411_attr[] = {
458 SENSOR_ATTR(temp1_highest, 0444, show_temp_highest, NULL, 0),
459 SENSOR_ATTR(temp1_lowest, 0444, show_temp_lowest, NULL, 0),
460 SENSOR_ATTR(temp2_highest, 0444, show_temp_highest, NULL, 1),
461 SENSOR_ATTR(temp2_lowest, 0444, show_temp_lowest, NULL, 1),
462 SENSOR_ATTR(temp_reset_history, 0200, NULL, reset_temp_history, 0),
463};
464
465/*
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200466 * Begin non sysfs callback code (aka Real code)
467 */
468
469static void tmp401_init_client(struct i2c_client *client)
470{
471 int config, config_orig;
472
473 /* Set the conversion rate to 2 Hz */
474 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
475
476 /* Start conversions (disable shutdown if necessary) */
477 config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
478 if (config < 0) {
479 dev_warn(&client->dev, "Initialization failed!\n");
480 return;
481 }
482
483 config_orig = config;
484 config &= ~TMP401_CONFIG_SHUTDOWN;
485
486 if (config != config_orig)
487 i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
488}
489
Jean Delvare310ec792009-12-14 21:17:23 +0100490static int tmp401_detect(struct i2c_client *client,
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200491 struct i2c_board_info *info)
492{
Jean Delvaredbe73c82009-12-09 20:35:54 +0100493 enum chips kind;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200494 struct i2c_adapter *adapter = client->adapter;
Jean Delvaredbe73c82009-12-09 20:35:54 +0100495 u8 reg;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200496
497 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
498 return -ENODEV;
499
500 /* Detect and identify the chip */
Jean Delvaredbe73c82009-12-09 20:35:54 +0100501 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
502 if (reg != TMP401_MANUFACTURER_ID)
503 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200504
Jean Delvaredbe73c82009-12-09 20:35:54 +0100505 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200506
Jean Delvaredbe73c82009-12-09 20:35:54 +0100507 switch (reg) {
508 case TMP401_DEVICE_ID:
509 kind = tmp401;
510 break;
511 case TMP411_DEVICE_ID:
512 kind = tmp411;
513 break;
514 default:
515 return -ENODEV;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200516 }
Jean Delvaredbe73c82009-12-09 20:35:54 +0100517
518 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
519 if (reg & 0x1b)
520 return -ENODEV;
521
522 reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
523 /* Datasheet says: 0x1-0x6 */
524 if (reg > 15)
525 return -ENODEV;
526
Andre Prendelfce07582009-06-15 18:39:47 +0200527 strlcpy(info->type, tmp401_id[kind - 1].name, I2C_NAME_SIZE);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200528
529 return 0;
530}
531
532static int tmp401_probe(struct i2c_client *client,
533 const struct i2c_device_id *id)
534{
535 int i, err = 0;
536 struct tmp401_data *data;
Andre Prendelfce07582009-06-15 18:39:47 +0200537 const char *names[] = { "TMP401", "TMP411" };
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200538
539 data = kzalloc(sizeof(struct tmp401_data), GFP_KERNEL);
540 if (!data)
541 return -ENOMEM;
542
543 i2c_set_clientdata(client, data);
544 mutex_init(&data->update_lock);
Andre Prendelfce07582009-06-15 18:39:47 +0200545 data->kind = id->driver_data;
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200546
547 /* Initialize the TMP401 chip */
548 tmp401_init_client(client);
549
550 /* Register sysfs hooks */
551 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) {
552 err = device_create_file(&client->dev,
553 &tmp401_attr[i].dev_attr);
554 if (err)
555 goto exit_remove;
556 }
557
Andre Prendelfce07582009-06-15 18:39:47 +0200558 /* Register aditional tmp411 sysfs hooks */
559 if (data->kind == tmp411) {
560 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) {
561 err = device_create_file(&client->dev,
562 &tmp411_attr[i].dev_attr);
563 if (err)
564 goto exit_remove;
565 }
566 }
567
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200568 data->hwmon_dev = hwmon_device_register(&client->dev);
569 if (IS_ERR(data->hwmon_dev)) {
570 err = PTR_ERR(data->hwmon_dev);
571 data->hwmon_dev = NULL;
572 goto exit_remove;
573 }
574
Andre Prendelfce07582009-06-15 18:39:47 +0200575 dev_info(&client->dev, "Detected TI %s chip\n",
576 names[data->kind - 1]);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200577
578 return 0;
579
580exit_remove:
581 tmp401_remove(client); /* will also free data for us */
582 return err;
583}
584
585static int tmp401_remove(struct i2c_client *client)
586{
587 struct tmp401_data *data = i2c_get_clientdata(client);
588 int i;
589
590 if (data->hwmon_dev)
591 hwmon_device_unregister(data->hwmon_dev);
592
593 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++)
594 device_remove_file(&client->dev, &tmp401_attr[i].dev_attr);
595
Andre Prendelfce07582009-06-15 18:39:47 +0200596 if (data->kind == tmp411) {
597 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++)
598 device_remove_file(&client->dev,
599 &tmp411_attr[i].dev_attr);
600 }
601
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200602 kfree(data);
603 return 0;
604}
605
Andre Prendelfce07582009-06-15 18:39:47 +0200606static struct tmp401_data *tmp401_update_device_reg16(
607 struct i2c_client *client, struct tmp401_data *data)
608{
609 int i;
610
611 for (i = 0; i < 2; i++) {
612 /*
613 * High byte must be read first immediately followed
614 * by the low byte
615 */
616 data->temp[i] = i2c_smbus_read_byte_data(client,
617 TMP401_TEMP_MSB[i]) << 8;
618 data->temp[i] |= i2c_smbus_read_byte_data(client,
619 TMP401_TEMP_LSB[i]);
620 data->temp_low[i] = i2c_smbus_read_byte_data(client,
621 TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8;
622 data->temp_low[i] |= i2c_smbus_read_byte_data(client,
623 TMP401_TEMP_LOW_LIMIT_LSB[i]);
624 data->temp_high[i] = i2c_smbus_read_byte_data(client,
625 TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8;
626 data->temp_high[i] |= i2c_smbus_read_byte_data(client,
627 TMP401_TEMP_HIGH_LIMIT_LSB[i]);
628 data->temp_crit[i] = i2c_smbus_read_byte_data(client,
629 TMP401_TEMP_CRIT_LIMIT[i]);
630
631 if (data->kind == tmp411) {
632 data->temp_lowest[i] = i2c_smbus_read_byte_data(client,
633 TMP411_TEMP_LOWEST_MSB[i]) << 8;
634 data->temp_lowest[i] |= i2c_smbus_read_byte_data(
635 client, TMP411_TEMP_LOWEST_LSB[i]);
636
637 data->temp_highest[i] = i2c_smbus_read_byte_data(
638 client, TMP411_TEMP_HIGHEST_MSB[i]) << 8;
639 data->temp_highest[i] |= i2c_smbus_read_byte_data(
640 client, TMP411_TEMP_HIGHEST_LSB[i]);
641 }
642 }
643 return data;
644}
645
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200646static struct tmp401_data *tmp401_update_device(struct device *dev)
647{
648 struct i2c_client *client = to_i2c_client(dev);
649 struct tmp401_data *data = i2c_get_clientdata(client);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200650
651 mutex_lock(&data->update_lock);
652
653 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
654 data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS);
655 data->config = i2c_smbus_read_byte_data(client,
656 TMP401_CONFIG_READ);
Andre Prendelfce07582009-06-15 18:39:47 +0200657 tmp401_update_device_reg16(client, data);
Hans de Goedeab2b79d2009-06-15 18:39:46 +0200658
659 data->temp_crit_hyst = i2c_smbus_read_byte_data(client,
660 TMP401_TEMP_CRIT_HYST);
661
662 data->last_updated = jiffies;
663 data->valid = 1;
664 }
665
666 mutex_unlock(&data->update_lock);
667
668 return data;
669}
670
671static int __init tmp401_init(void)
672{
673 return i2c_add_driver(&tmp401_driver);
674}
675
676static void __exit tmp401_exit(void)
677{
678 i2c_del_driver(&tmp401_driver);
679}
680
681MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
682MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
683MODULE_LICENSE("GPL");
684
685module_init(tmp401_init);
686module_exit(tmp401_exit);