hwmon: Add driver for LM95234
[deliverable/linux.git] / drivers / hwmon / tmp401.c
CommitLineData
ab2b79d5
HG
1/* tmp401.c
2 *
3 * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
fce0758f
AP
4 * Preliminary tmp411 support by:
5 * Gabriel Konat, Sander Leget, Wouter Willems
6 * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
ab2b79d5
HG
7 *
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 */
a1fac92b 43static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
ab2b79d5 44
a1fac92b 45enum chips { tmp401, tmp411, tmp431 };
ab2b79d5
HG
46
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
fce0758f 60#define TMP411_N_FACTOR_REG 0x18
ab2b79d5
HG
61
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
fce0758f
AP
73static 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
ab2b79d5
HG
78/* 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
fce0758f 92#define TMP411_DEVICE_ID 0x12
a1fac92b 93#define TMP431_DEVICE_ID 0x31
ab2b79d5 94
ab2b79d5
HG
95/*
96 * Driver data (common to all clients)
97 */
98
99static const struct i2c_device_id tmp401_id[] = {
100 { "tmp401", tmp401 },
fce0758f 101 { "tmp411", tmp411 },
a1fac92b 102 { "tmp431", tmp431 },
ab2b79d5
HG
103 { }
104};
105MODULE_DEVICE_TABLE(i2c, tmp401_id);
106
ab2b79d5
HG
107/*
108 * Client data (each client gets its own)
109 */
110
111struct tmp401_data {
112 struct device *hwmon_dev;
113 struct mutex update_lock;
114 char valid; /* zero until following fields are valid */
115 unsigned long last_updated; /* in jiffies */
dc71afe5 116 enum chips kind;
ab2b79d5
HG
117
118 /* register values */
119 u8 status;
120 u8 config;
121 u16 temp[2];
122 u16 temp_low[2];
123 u16 temp_high[2];
124 u8 temp_crit[2];
125 u8 temp_crit_hyst;
fce0758f
AP
126 u16 temp_lowest[2];
127 u16 temp_highest[2];
ab2b79d5
HG
128};
129
130/*
131 * Sysfs attr show / store functions
132 */
133
134static int tmp401_register_to_temp(u16 reg, u8 config)
135{
136 int temp = reg;
137
138 if (config & TMP401_CONFIG_RANGE)
139 temp -= 64 * 256;
140
141 return (temp * 625 + 80) / 160;
142}
143
144static u16 tmp401_temp_to_register(long temp, u8 config)
145{
146 if (config & TMP401_CONFIG_RANGE) {
2a844c14 147 temp = clamp_val(temp, -64000, 191000);
ab2b79d5
HG
148 temp += 64000;
149 } else
2a844c14 150 temp = clamp_val(temp, 0, 127000);
ab2b79d5
HG
151
152 return (temp * 160 + 312) / 625;
153}
154
155static int tmp401_crit_register_to_temp(u8 reg, u8 config)
156{
157 int temp = reg;
158
159 if (config & TMP401_CONFIG_RANGE)
160 temp -= 64;
161
162 return temp * 1000;
163}
164
165static u8 tmp401_crit_temp_to_register(long temp, u8 config)
166{
167 if (config & TMP401_CONFIG_RANGE) {
2a844c14 168 temp = clamp_val(temp, -64000, 191000);
ab2b79d5
HG
169 temp += 64000;
170 } else
2a844c14 171 temp = clamp_val(temp, 0, 127000);
ab2b79d5
HG
172
173 return (temp + 500) / 1000;
174}
175
ea63c2b9
AP
176static struct tmp401_data *tmp401_update_device_reg16(
177 struct i2c_client *client, struct tmp401_data *data)
178{
179 int i;
180
181 for (i = 0; i < 2; i++) {
182 /*
183 * High byte must be read first immediately followed
184 * by the low byte
185 */
186 data->temp[i] = i2c_smbus_read_byte_data(client,
187 TMP401_TEMP_MSB[i]) << 8;
188 data->temp[i] |= i2c_smbus_read_byte_data(client,
189 TMP401_TEMP_LSB[i]);
190 data->temp_low[i] = i2c_smbus_read_byte_data(client,
191 TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8;
192 data->temp_low[i] |= i2c_smbus_read_byte_data(client,
193 TMP401_TEMP_LOW_LIMIT_LSB[i]);
194 data->temp_high[i] = i2c_smbus_read_byte_data(client,
195 TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8;
196 data->temp_high[i] |= i2c_smbus_read_byte_data(client,
197 TMP401_TEMP_HIGH_LIMIT_LSB[i]);
198 data->temp_crit[i] = i2c_smbus_read_byte_data(client,
199 TMP401_TEMP_CRIT_LIMIT[i]);
200
201 if (data->kind == tmp411) {
202 data->temp_lowest[i] = i2c_smbus_read_byte_data(client,
203 TMP411_TEMP_LOWEST_MSB[i]) << 8;
204 data->temp_lowest[i] |= i2c_smbus_read_byte_data(
205 client, TMP411_TEMP_LOWEST_LSB[i]);
206
207 data->temp_highest[i] = i2c_smbus_read_byte_data(
208 client, TMP411_TEMP_HIGHEST_MSB[i]) << 8;
209 data->temp_highest[i] |= i2c_smbus_read_byte_data(
210 client, TMP411_TEMP_HIGHEST_LSB[i]);
211 }
212 }
213 return data;
214}
215
216static struct tmp401_data *tmp401_update_device(struct device *dev)
217{
218 struct i2c_client *client = to_i2c_client(dev);
219 struct tmp401_data *data = i2c_get_clientdata(client);
220
221 mutex_lock(&data->update_lock);
222
223 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
224 data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS);
225 data->config = i2c_smbus_read_byte_data(client,
226 TMP401_CONFIG_READ);
227 tmp401_update_device_reg16(client, data);
228
229 data->temp_crit_hyst = i2c_smbus_read_byte_data(client,
230 TMP401_TEMP_CRIT_HYST);
231
232 data->last_updated = jiffies;
233 data->valid = 1;
234 }
235
236 mutex_unlock(&data->update_lock);
237
238 return data;
239}
240
ab2b79d5
HG
241static ssize_t show_temp_value(struct device *dev,
242 struct device_attribute *devattr, char *buf)
243{
244 int index = to_sensor_dev_attr(devattr)->index;
245 struct tmp401_data *data = tmp401_update_device(dev);
246
247 return sprintf(buf, "%d\n",
248 tmp401_register_to_temp(data->temp[index], data->config));
249}
250
251static ssize_t show_temp_min(struct device *dev,
252 struct device_attribute *devattr, char *buf)
253{
254 int index = to_sensor_dev_attr(devattr)->index;
255 struct tmp401_data *data = tmp401_update_device(dev);
256
257 return sprintf(buf, "%d\n",
258 tmp401_register_to_temp(data->temp_low[index], data->config));
259}
260
261static ssize_t show_temp_max(struct device *dev,
262 struct device_attribute *devattr, char *buf)
263{
264 int index = to_sensor_dev_attr(devattr)->index;
265 struct tmp401_data *data = tmp401_update_device(dev);
266
267 return sprintf(buf, "%d\n",
268 tmp401_register_to_temp(data->temp_high[index], data->config));
269}
270
271static ssize_t show_temp_crit(struct device *dev,
272 struct device_attribute *devattr, char *buf)
273{
274 int index = to_sensor_dev_attr(devattr)->index;
275 struct tmp401_data *data = tmp401_update_device(dev);
276
277 return sprintf(buf, "%d\n",
278 tmp401_crit_register_to_temp(data->temp_crit[index],
279 data->config));
280}
281
282static ssize_t show_temp_crit_hyst(struct device *dev,
283 struct device_attribute *devattr, char *buf)
284{
285 int temp, index = to_sensor_dev_attr(devattr)->index;
286 struct tmp401_data *data = tmp401_update_device(dev);
287
288 mutex_lock(&data->update_lock);
289 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
290 data->config);
291 temp -= data->temp_crit_hyst * 1000;
292 mutex_unlock(&data->update_lock);
293
294 return sprintf(buf, "%d\n", temp);
295}
296
fce0758f
AP
297static ssize_t show_temp_lowest(struct device *dev,
298 struct device_attribute *devattr, char *buf)
299{
300 int index = to_sensor_dev_attr(devattr)->index;
301 struct tmp401_data *data = tmp401_update_device(dev);
302
303 return sprintf(buf, "%d\n",
304 tmp401_register_to_temp(data->temp_lowest[index],
305 data->config));
306}
307
308static ssize_t show_temp_highest(struct device *dev,
309 struct device_attribute *devattr, char *buf)
310{
311 int index = to_sensor_dev_attr(devattr)->index;
312 struct tmp401_data *data = tmp401_update_device(dev);
313
314 return sprintf(buf, "%d\n",
315 tmp401_register_to_temp(data->temp_highest[index],
316 data->config));
317}
318
ab2b79d5
HG
319static ssize_t show_status(struct device *dev,
320 struct device_attribute *devattr, char *buf)
321{
322 int mask = to_sensor_dev_attr(devattr)->index;
323 struct tmp401_data *data = tmp401_update_device(dev);
324
325 if (data->status & mask)
326 return sprintf(buf, "1\n");
327 else
328 return sprintf(buf, "0\n");
329}
330
331static ssize_t store_temp_min(struct device *dev, struct device_attribute
332 *devattr, const char *buf, size_t count)
333{
334 int index = to_sensor_dev_attr(devattr)->index;
335 struct tmp401_data *data = tmp401_update_device(dev);
336 long val;
337 u16 reg;
338
179c4fdb 339 if (kstrtol(buf, 10, &val))
ab2b79d5
HG
340 return -EINVAL;
341
342 reg = tmp401_temp_to_register(val, data->config);
343
344 mutex_lock(&data->update_lock);
345
346 i2c_smbus_write_byte_data(to_i2c_client(dev),
347 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8);
348 i2c_smbus_write_byte_data(to_i2c_client(dev),
349 TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF);
350
351 data->temp_low[index] = reg;
352
353 mutex_unlock(&data->update_lock);
354
355 return count;
356}
357
358static ssize_t store_temp_max(struct device *dev, struct device_attribute
359 *devattr, const char *buf, size_t count)
360{
361 int index = to_sensor_dev_attr(devattr)->index;
362 struct tmp401_data *data = tmp401_update_device(dev);
363 long val;
364 u16 reg;
365
179c4fdb 366 if (kstrtol(buf, 10, &val))
ab2b79d5
HG
367 return -EINVAL;
368
369 reg = tmp401_temp_to_register(val, data->config);
370
371 mutex_lock(&data->update_lock);
372
373 i2c_smbus_write_byte_data(to_i2c_client(dev),
374 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8);
375 i2c_smbus_write_byte_data(to_i2c_client(dev),
376 TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF);
377
378 data->temp_high[index] = reg;
379
380 mutex_unlock(&data->update_lock);
381
382 return count;
383}
384
385static ssize_t store_temp_crit(struct device *dev, struct device_attribute
386 *devattr, const char *buf, size_t count)
387{
388 int index = to_sensor_dev_attr(devattr)->index;
389 struct tmp401_data *data = tmp401_update_device(dev);
390 long val;
391 u8 reg;
392
179c4fdb 393 if (kstrtol(buf, 10, &val))
ab2b79d5
HG
394 return -EINVAL;
395
396 reg = tmp401_crit_temp_to_register(val, data->config);
397
398 mutex_lock(&data->update_lock);
399
400 i2c_smbus_write_byte_data(to_i2c_client(dev),
401 TMP401_TEMP_CRIT_LIMIT[index], reg);
402
403 data->temp_crit[index] = reg;
404
405 mutex_unlock(&data->update_lock);
406
407 return count;
408}
409
410static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
411 *devattr, const char *buf, size_t count)
412{
413 int temp, index = to_sensor_dev_attr(devattr)->index;
414 struct tmp401_data *data = tmp401_update_device(dev);
415 long val;
416 u8 reg;
417
179c4fdb 418 if (kstrtol(buf, 10, &val))
ab2b79d5
HG
419 return -EINVAL;
420
421 if (data->config & TMP401_CONFIG_RANGE)
2a844c14 422 val = clamp_val(val, -64000, 191000);
ab2b79d5 423 else
2a844c14 424 val = clamp_val(val, 0, 127000);
ab2b79d5
HG
425
426 mutex_lock(&data->update_lock);
427 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
428 data->config);
2a844c14 429 val = clamp_val(val, temp - 255000, temp);
ab2b79d5
HG
430 reg = ((temp - val) + 500) / 1000;
431
432 i2c_smbus_write_byte_data(to_i2c_client(dev),
433 TMP401_TEMP_CRIT_HYST, reg);
434
435 data->temp_crit_hyst = reg;
436
437 mutex_unlock(&data->update_lock);
438
439 return count;
440}
441
fce0758f
AP
442/*
443 * Resets the historical measurements of minimum and maximum temperatures.
444 * This is done by writing any value to any of the minimum/maximum registers
445 * (0x30-0x37).
446 */
447static ssize_t reset_temp_history(struct device *dev,
448 struct device_attribute *devattr, const char *buf, size_t count)
449{
450 long val;
451
179c4fdb 452 if (kstrtol(buf, 10, &val))
fce0758f
AP
453 return -EINVAL;
454
455 if (val != 1) {
b55f3757
GR
456 dev_err(dev,
457 "temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
458 val);
fce0758f
AP
459 return -EINVAL;
460 }
461 i2c_smbus_write_byte_data(to_i2c_client(dev),
462 TMP411_TEMP_LOWEST_MSB[0], val);
463
464 return count;
465}
466
ab2b79d5 467static struct sensor_device_attribute tmp401_attr[] = {
2b76d80a
AP
468 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0),
469 SENSOR_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
470 store_temp_min, 0),
471 SENSOR_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
472 store_temp_max, 0),
473 SENSOR_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit,
474 store_temp_crit, 0),
475 SENSOR_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_crit_hyst,
ab2b79d5 476 store_temp_crit_hyst, 0),
2b76d80a 477 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_status, NULL,
ab2b79d5 478 TMP401_STATUS_LOCAL_LOW),
2b76d80a 479 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_status, NULL,
ab2b79d5 480 TMP401_STATUS_LOCAL_HIGH),
2b76d80a 481 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_status, NULL,
ab2b79d5 482 TMP401_STATUS_LOCAL_CRIT),
2b76d80a
AP
483 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1),
484 SENSOR_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
485 store_temp_min, 1),
486 SENSOR_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
487 store_temp_max, 1),
488 SENSOR_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit,
489 store_temp_crit, 1),
490 SENSOR_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 1),
491 SENSOR_ATTR(temp2_fault, S_IRUGO, show_status, NULL,
ab2b79d5 492 TMP401_STATUS_REMOTE_OPEN),
2b76d80a 493 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_status, NULL,
ab2b79d5 494 TMP401_STATUS_REMOTE_LOW),
2b76d80a 495 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_status, NULL,
ab2b79d5 496 TMP401_STATUS_REMOTE_HIGH),
2b76d80a 497 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_status, NULL,
ab2b79d5
HG
498 TMP401_STATUS_REMOTE_CRIT),
499};
500
fce0758f
AP
501/*
502 * Additional features of the TMP411 chip.
503 * The TMP411 stores the minimum and maximum
504 * temperature measured since power-on, chip-reset, or
505 * minimum and maximum register reset for both the local
506 * and remote channels.
507 */
508static struct sensor_device_attribute tmp411_attr[] = {
2b76d80a
AP
509 SENSOR_ATTR(temp1_highest, S_IRUGO, show_temp_highest, NULL, 0),
510 SENSOR_ATTR(temp1_lowest, S_IRUGO, show_temp_lowest, NULL, 0),
511 SENSOR_ATTR(temp2_highest, S_IRUGO, show_temp_highest, NULL, 1),
512 SENSOR_ATTR(temp2_lowest, S_IRUGO, show_temp_lowest, NULL, 1),
513 SENSOR_ATTR(temp_reset_history, S_IWUSR, NULL, reset_temp_history, 0),
fce0758f
AP
514};
515
ab2b79d5
HG
516/*
517 * Begin non sysfs callback code (aka Real code)
518 */
519
520static void tmp401_init_client(struct i2c_client *client)
521{
522 int config, config_orig;
523
524 /* Set the conversion rate to 2 Hz */
525 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
526
527 /* Start conversions (disable shutdown if necessary) */
528 config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
529 if (config < 0) {
530 dev_warn(&client->dev, "Initialization failed!\n");
531 return;
532 }
533
534 config_orig = config;
535 config &= ~TMP401_CONFIG_SHUTDOWN;
536
537 if (config != config_orig)
538 i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
539}
540
310ec792 541static int tmp401_detect(struct i2c_client *client,
ab2b79d5
HG
542 struct i2c_board_info *info)
543{
dbe73c8f 544 enum chips kind;
ab2b79d5 545 struct i2c_adapter *adapter = client->adapter;
dbe73c8f 546 u8 reg;
ab2b79d5
HG
547
548 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
549 return -ENODEV;
550
551 /* Detect and identify the chip */
dbe73c8f
JD
552 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
553 if (reg != TMP401_MANUFACTURER_ID)
554 return -ENODEV;
ab2b79d5 555
dbe73c8f 556 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
ab2b79d5 557
dbe73c8f
JD
558 switch (reg) {
559 case TMP401_DEVICE_ID:
a1fac92b
GR
560 if (client->addr != 0x4c)
561 return -ENODEV;
dbe73c8f
JD
562 kind = tmp401;
563 break;
564 case TMP411_DEVICE_ID:
565 kind = tmp411;
566 break;
a1fac92b
GR
567 case TMP431_DEVICE_ID:
568 if (client->addr == 0x4e)
569 return -ENODEV;
570 kind = tmp431;
571 break;
dbe73c8f
JD
572 default:
573 return -ENODEV;
ab2b79d5 574 }
dbe73c8f
JD
575
576 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
577 if (reg & 0x1b)
578 return -ENODEV;
579
580 reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
581 /* Datasheet says: 0x1-0x6 */
582 if (reg > 15)
583 return -ENODEV;
584
dc71afe5 585 strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
ab2b79d5
HG
586
587 return 0;
588}
589
ea63c2b9
AP
590static int tmp401_remove(struct i2c_client *client)
591{
592 struct tmp401_data *data = i2c_get_clientdata(client);
593 int i;
594
595 if (data->hwmon_dev)
596 hwmon_device_unregister(data->hwmon_dev);
597
598 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++)
599 device_remove_file(&client->dev, &tmp401_attr[i].dev_attr);
600
601 if (data->kind == tmp411) {
602 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++)
603 device_remove_file(&client->dev,
604 &tmp411_attr[i].dev_attr);
605 }
606
ea63c2b9
AP
607 return 0;
608}
609
ab2b79d5
HG
610static int tmp401_probe(struct i2c_client *client,
611 const struct i2c_device_id *id)
612{
613 int i, err = 0;
614 struct tmp401_data *data;
a1fac92b 615 const char *names[] = { "TMP401", "TMP411", "TMP431" };
ab2b79d5 616
0df9fc7b
GR
617 data = devm_kzalloc(&client->dev, sizeof(struct tmp401_data),
618 GFP_KERNEL);
ab2b79d5
HG
619 if (!data)
620 return -ENOMEM;
621
622 i2c_set_clientdata(client, data);
623 mutex_init(&data->update_lock);
fce0758f 624 data->kind = id->driver_data;
ab2b79d5
HG
625
626 /* Initialize the TMP401 chip */
627 tmp401_init_client(client);
628
629 /* Register sysfs hooks */
630 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) {
631 err = device_create_file(&client->dev,
632 &tmp401_attr[i].dev_attr);
633 if (err)
634 goto exit_remove;
635 }
636
a80581d0 637 /* Register additional tmp411 sysfs hooks */
fce0758f
AP
638 if (data->kind == tmp411) {
639 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) {
640 err = device_create_file(&client->dev,
641 &tmp411_attr[i].dev_attr);
642 if (err)
643 goto exit_remove;
644 }
645 }
646
ab2b79d5
HG
647 data->hwmon_dev = hwmon_device_register(&client->dev);
648 if (IS_ERR(data->hwmon_dev)) {
649 err = PTR_ERR(data->hwmon_dev);
650 data->hwmon_dev = NULL;
651 goto exit_remove;
652 }
653
dc71afe5 654 dev_info(&client->dev, "Detected TI %s chip\n", names[data->kind]);
ab2b79d5
HG
655
656 return 0;
657
658exit_remove:
0df9fc7b 659 tmp401_remove(client);
ab2b79d5
HG
660 return err;
661}
662
ea63c2b9
AP
663static struct i2c_driver tmp401_driver = {
664 .class = I2C_CLASS_HWMON,
665 .driver = {
666 .name = "tmp401",
667 },
668 .probe = tmp401_probe,
669 .remove = tmp401_remove,
670 .id_table = tmp401_id,
671 .detect = tmp401_detect,
672 .address_list = normal_i2c,
673};
ab2b79d5 674
f0967eea 675module_i2c_driver(tmp401_driver);
ab2b79d5
HG
676
677MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
678MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
679MODULE_LICENSE("GPL");
This page took 0.306887 seconds and 5 git commands to generate.