hwmon: (tmp401) Fix device detection for TMP411B and TMP411C
[platform/kernel/linux-rpi.git] / drivers / hwmon / tmp401.c
1 /* tmp401.c
2  *
3  * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
4  * Preliminary tmp411 support by:
5  * Gabriel Konat, Sander Leget, Wouter Willems
6  * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de>
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 */
43 static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
44
45 enum chips { tmp401, tmp411, tmp431 };
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
60 #define TMP411_N_FACTOR_REG                     0x18
61
62 static const u8 TMP401_TEMP_MSB[2]                      = { 0x00, 0x01 };
63 static const u8 TMP401_TEMP_LSB[2]                      = { 0x15, 0x10 };
64 static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2]       = { 0x06, 0x08 };
65 static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2]      = { 0x0C, 0x0E };
66 static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2]            = { 0x17, 0x14 };
67 static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2]      = { 0x05, 0x07 };
68 static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2]     = { 0x0B, 0x0D };
69 static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2]           = { 0x16, 0x13 };
70 /* These are called the THERM limit / hysteresis / mask in the datasheet */
71 static const u8 TMP401_TEMP_CRIT_LIMIT[2]               = { 0x20, 0x19 };
72
73 static const u8 TMP411_TEMP_LOWEST_MSB[2]               = { 0x30, 0x34 };
74 static const u8 TMP411_TEMP_LOWEST_LSB[2]               = { 0x31, 0x35 };
75 static const u8 TMP411_TEMP_HIGHEST_MSB[2]              = { 0x32, 0x36 };
76 static const u8 TMP411_TEMP_HIGHEST_LSB[2]              = { 0x33, 0x37 };
77
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
92 #define TMP411A_DEVICE_ID                       0x12
93 #define TMP411B_DEVICE_ID                       0x13
94 #define TMP411C_DEVICE_ID                       0x10
95 #define TMP431_DEVICE_ID                        0x31
96
97 /*
98  * Driver data (common to all clients)
99  */
100
101 static const struct i2c_device_id tmp401_id[] = {
102         { "tmp401", tmp401 },
103         { "tmp411", tmp411 },
104         { "tmp431", tmp431 },
105         { }
106 };
107 MODULE_DEVICE_TABLE(i2c, tmp401_id);
108
109 /*
110  * Client data (each client gets its own)
111  */
112
113 struct 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 */
118         enum chips kind;
119
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;
128         u16 temp_lowest[2];
129         u16 temp_highest[2];
130 };
131
132 /*
133  * Sysfs attr show / store functions
134  */
135
136 static 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
146 static u16 tmp401_temp_to_register(long temp, u8 config)
147 {
148         if (config & TMP401_CONFIG_RANGE) {
149                 temp = clamp_val(temp, -64000, 191000);
150                 temp += 64000;
151         } else
152                 temp = clamp_val(temp, 0, 127000);
153
154         return (temp * 160 + 312) / 625;
155 }
156
157 static 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
167 static u8 tmp401_crit_temp_to_register(long temp, u8 config)
168 {
169         if (config & TMP401_CONFIG_RANGE) {
170                 temp = clamp_val(temp, -64000, 191000);
171                 temp += 64000;
172         } else
173                 temp = clamp_val(temp, 0, 127000);
174
175         return (temp + 500) / 1000;
176 }
177
178 static 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
218 static 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
243 static 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
253 static 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
263 static 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
273 static 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
284 static 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
299 static 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
310 static 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
321 static 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
333 static 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
341         if (kstrtol(buf, 10, &val))
342                 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
360 static 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
368         if (kstrtol(buf, 10, &val))
369                 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
387 static 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
395         if (kstrtol(buf, 10, &val))
396                 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
412 static 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
420         if (kstrtol(buf, 10, &val))
421                 return -EINVAL;
422
423         if (data->config & TMP401_CONFIG_RANGE)
424                 val = clamp_val(val, -64000, 191000);
425         else
426                 val = clamp_val(val, 0, 127000);
427
428         mutex_lock(&data->update_lock);
429         temp = tmp401_crit_register_to_temp(data->temp_crit[index],
430                                                 data->config);
431         val = clamp_val(val, temp - 255000, temp);
432         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
444 /*
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  */
449 static ssize_t reset_temp_history(struct device *dev,
450         struct device_attribute *devattr, const char *buf, size_t count)
451 {
452         long val;
453
454         if (kstrtol(buf, 10, &val))
455                 return -EINVAL;
456
457         if (val != 1) {
458                 dev_err(dev,
459                         "temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
460                         val);
461                 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
469 static struct sensor_device_attribute tmp401_attr[] = {
470         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,
478                     store_temp_crit_hyst, 0),
479         SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_status, NULL,
480                     TMP401_STATUS_LOCAL_LOW),
481         SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_status, NULL,
482                     TMP401_STATUS_LOCAL_HIGH),
483         SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_status, NULL,
484                     TMP401_STATUS_LOCAL_CRIT),
485         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,
494                     TMP401_STATUS_REMOTE_OPEN),
495         SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_status, NULL,
496                     TMP401_STATUS_REMOTE_LOW),
497         SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_status, NULL,
498                     TMP401_STATUS_REMOTE_HIGH),
499         SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_status, NULL,
500                     TMP401_STATUS_REMOTE_CRIT),
501 };
502
503 /*
504  * 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  */
510 static struct sensor_device_attribute tmp411_attr[] = {
511         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),
516 };
517
518 /*
519  * Begin non sysfs callback code (aka Real code)
520  */
521
522 static 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
543 static int tmp401_detect(struct i2c_client *client,
544                          struct i2c_board_info *info)
545 {
546         enum chips kind;
547         struct i2c_adapter *adapter = client->adapter;
548         u8 reg;
549
550         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
551                 return -ENODEV;
552
553         /* Detect and identify the chip */
554         reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
555         if (reg != TMP401_MANUFACTURER_ID)
556                 return -ENODEV;
557
558         reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
559
560         switch (reg) {
561         case TMP401_DEVICE_ID:
562                 if (client->addr != 0x4c)
563                         return -ENODEV;
564                 kind = tmp401;
565                 break;
566         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;
579                 kind = tmp411;
580                 break;
581         case TMP431_DEVICE_ID:
582                 if (client->addr == 0x4e)
583                         return -ENODEV;
584                 kind = tmp431;
585                 break;
586         default:
587                 return -ENODEV;
588         }
589
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
599         strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
600
601         return 0;
602 }
603
604 static 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
621         return 0;
622 }
623
624 static 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;
629         const char *names[] = { "TMP401", "TMP411", "TMP431" };
630
631         data = devm_kzalloc(&client->dev, sizeof(struct tmp401_data),
632                             GFP_KERNEL);
633         if (!data)
634                 return -ENOMEM;
635
636         i2c_set_clientdata(client, data);
637         mutex_init(&data->update_lock);
638         data->kind = id->driver_data;
639
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
651         /* Register additional tmp411 sysfs hooks */
652         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
661         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
668         dev_info(&client->dev, "Detected TI %s chip\n", names[data->kind]);
669
670         return 0;
671
672 exit_remove:
673         tmp401_remove(client);
674         return err;
675 }
676
677 static 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 };
688
689 module_i2c_driver(tmp401_driver);
690
691 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
692 MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
693 MODULE_LICENSE("GPL");