misc: nct: error handling for no regulator
[linux-2.6.git] / drivers / misc / nct1008.c
1 /*
2  * drivers/misc/nct1008.c
3  *
4  * Driver for NCT1008, temperature monitoring device from ON Semiconductors
5  *
6  * Copyright (c) 2010-2013, NVIDIA CORPORATION. All rights reserved.
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, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/err.h>
29 #include <linux/gpio.h>
30 #include <linux/device.h>
31 #include <linux/nct1008.h>
32 #include <linux/delay.h>
33 #include <linux/thermal.h>
34 #include <linux/regulator/consumer.h>
35
36 /* Register Addresses */
37 #define LOCAL_TEMP_RD                   0x00
38 #define EXT_TEMP_RD_HI                  0x01
39 #define EXT_TEMP_RD_LO                  0x10
40 #define STATUS_RD                       0x02
41 #define CONFIG_RD                       0x03
42
43 #define LOCAL_TEMP_HI_LIMIT_RD          0x05
44 #define LOCAL_TEMP_LO_LIMIT_RD          0x06
45
46 #define EXT_TEMP_HI_LIMIT_HI_BYTE_RD    0x07
47 #define EXT_TEMP_LO_LIMIT_HI_BYTE_RD    0x08
48
49 #define CONFIG_WR                       0x09
50 #define CONV_RATE_WR                    0x0A
51 #define LOCAL_TEMP_HI_LIMIT_WR          0x0B
52 #define LOCAL_TEMP_LO_LIMIT_WR          0x0C
53 #define EXT_TEMP_HI_LIMIT_HI_BYTE_WR    0x0D
54 #define EXT_TEMP_LO_LIMIT_HI_BYTE_WR    0x0E
55 #define ONE_SHOT                        0x0F
56 #define OFFSET_WR                       0x11
57 #define OFFSET_QUARTER_WR               0x12
58 #define EXT_THERM_LIMIT_WR              0x19
59 #define LOCAL_THERM_LIMIT_WR            0x20
60 #define THERM_HYSTERESIS_WR             0x21
61
62 /* Configuration Register Bits */
63 #define EXTENDED_RANGE_BIT              BIT(2)
64 #define THERM2_BIT                      BIT(5)
65 #define STANDBY_BIT                     BIT(6)
66 #define ALERT_BIT                       BIT(7)
67
68 /* Max Temperature Measurements */
69 #define EXTENDED_RANGE_OFFSET           64U
70 #define STANDARD_RANGE_MAX              127U
71 #define EXTENDED_RANGE_MAX              (150U + EXTENDED_RANGE_OFFSET)
72
73 #define NCT1008_MIN_TEMP -64
74 #define NCT1008_MAX_TEMP 191
75
76 #define MAX_STR_PRINT 50
77
78 #define MAX_CONV_TIME_ONESHOT_MS (52)
79 #define CELSIUS_TO_MILLICELSIUS(x) ((x)*1000)
80 #define MILLICELSIUS_TO_CELSIUS(x) ((x)/1000)
81
82 #define POWER_ON_DELAY 20 /*ms*/
83
84 struct nct1008_data {
85         struct workqueue_struct *workqueue;
86         struct work_struct work;
87         struct i2c_client *client;
88         struct nct1008_platform_data plat_data;
89         struct mutex mutex;
90         struct dentry *dent;
91         u8 config;
92         enum nct1008_chip chip;
93         struct regulator *nct_reg;
94         long current_lo_limit;
95         long current_hi_limit;
96         int conv_period_ms;
97         long etemp;
98         int shutdown_complete;
99
100         struct thermal_zone_device *nct_int;
101         struct thermal_zone_device *nct_ext;
102 };
103
104 static const struct i2c_device_id nct1008_id[] = {
105         { "nct1008", NCT1008 },
106         { "nct72", NCT72},
107         { "nct218", NCT218 },
108         {}
109 };
110
111 static int conv_period_ms_table[] =
112         {16000, 8000, 4000, 2000, 1000, 500, 250, 125, 63, 32, 16};
113
114 static inline s16 value_to_temperature(bool extended, u8 value)
115 {
116         return extended ? (s16)(value - EXTENDED_RANGE_OFFSET) : (s16)value;
117 }
118
119 static inline u8 temperature_to_value(bool extended, s16 temp)
120 {
121         return extended ? (u8)(temp + EXTENDED_RANGE_OFFSET) : (u8)temp;
122 }
123
124 static int nct1008_write_reg(struct i2c_client *client, u8 reg, u16 value)
125 {
126         int ret = 0;
127         struct nct1008_data *data = i2c_get_clientdata(client);
128
129         mutex_lock(&data->mutex);
130         if (data && data->shutdown_complete) {
131                 mutex_unlock(&data->mutex);
132                 return -ENODEV;
133         }
134
135         ret = i2c_smbus_write_byte_data(client, reg, value);
136         mutex_unlock(&data->mutex);
137
138         if (ret < 0)
139                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
140
141         return ret;
142 }
143
144 static int nct1008_read_reg(struct i2c_client *client, u8 reg)
145 {
146         int ret = 0;
147         struct nct1008_data *data = i2c_get_clientdata(client);
148         mutex_lock(&data->mutex);
149         if (data && data->shutdown_complete) {
150                 mutex_unlock(&data->mutex);
151                 return -ENODEV;
152         }
153
154         ret = i2c_smbus_read_byte_data(client, reg);
155         mutex_unlock(&data->mutex);
156
157         if (ret < 0)
158                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
159
160         return ret;
161 }
162
163 static int nct1008_get_temp(struct device *dev, long *etemp, long *itemp)
164 {
165         struct i2c_client *client = to_i2c_client(dev);
166         struct nct1008_platform_data *pdata = client->dev.platform_data;
167         s16 temp_local;
168         u8 temp_ext_lo;
169         s16 temp_ext_hi;
170         long temp_ext_milli;
171         long temp_local_milli;
172         u8 value;
173
174         /* Read Local Temp */
175         if (itemp) {
176                 value = nct1008_read_reg(client, LOCAL_TEMP_RD);
177                 if (value < 0)
178                         goto error;
179                 temp_local = value_to_temperature(pdata->ext_range, value);
180                 temp_local_milli = CELSIUS_TO_MILLICELSIUS(temp_local);
181
182                 *itemp = temp_local_milli;
183         }
184
185         /* Read External Temp */
186         if (etemp) {
187                 value = nct1008_read_reg(client, EXT_TEMP_RD_LO);
188                 if (value < 0)
189                         goto error;
190                 temp_ext_lo = (value >> 6);
191
192                 value = nct1008_read_reg(client, EXT_TEMP_RD_HI);
193                 if (value < 0)
194                         goto error;
195                 temp_ext_hi = value_to_temperature(pdata->ext_range, value);
196
197                 temp_ext_milli = CELSIUS_TO_MILLICELSIUS(temp_ext_hi) +
198                                         temp_ext_lo * 250;
199
200                 *etemp = temp_ext_milli;
201         }
202
203         return 0;
204 error:
205         dev_err(&client->dev, "\n error in file=: %s %s() line=%d: "
206                 "error=%d ", __FILE__, __func__, __LINE__, value);
207         return value;
208 }
209
210 static ssize_t nct1008_show_temp(struct device *dev,
211         struct device_attribute *attr, char *buf)
212 {
213         struct i2c_client *client = to_i2c_client(dev);
214         struct nct1008_platform_data *pdata = client->dev.platform_data;
215         s16 temp1 = 0;
216         s16 temp = 0;
217         u8 temp2 = 0;
218         int value = 0;
219
220         if (!dev || !buf || !attr)
221                 return -EINVAL;
222
223         value = nct1008_read_reg(client, LOCAL_TEMP_RD);
224         if (value < 0)
225                 goto error;
226         temp1 = value_to_temperature(pdata->ext_range, value);
227
228         value = nct1008_read_reg(client, EXT_TEMP_RD_LO);
229         if (value < 0)
230                 goto error;
231         temp2 = (value >> 6);
232         value = nct1008_read_reg(client, EXT_TEMP_RD_HI);
233         if (value < 0)
234                 goto error;
235         temp = value_to_temperature(pdata->ext_range, value);
236
237         return snprintf(buf, MAX_STR_PRINT, "%d %d.%d\n",
238                 temp1, temp, temp2 * 25);
239
240 error:
241         return snprintf(buf, MAX_STR_PRINT,
242                 "Error read local/ext temperature\n");
243 }
244
245 static ssize_t nct1008_show_temp_overheat(struct device *dev,
246                                 struct device_attribute *attr,
247                                 char *buf)
248 {
249         struct i2c_client *client = to_i2c_client(dev);
250         struct nct1008_platform_data *pdata = client->dev.platform_data;
251         int value;
252         s16 temp, temp2;
253
254         /* Local temperature h/w shutdown limit */
255         value = nct1008_read_reg(client, LOCAL_THERM_LIMIT_WR);
256         if (value < 0)
257                 goto error;
258         temp = value_to_temperature(pdata->ext_range, value);
259
260         /* External temperature h/w shutdown limit */
261         value = nct1008_read_reg(client, EXT_THERM_LIMIT_WR);
262         if (value < 0)
263                 goto error;
264         temp2 = value_to_temperature(pdata->ext_range, value);
265
266         return snprintf(buf, MAX_STR_PRINT, "%d %d\n", temp, temp2);
267 error:
268         dev_err(dev, "%s: failed to read temperature-overheat "
269                 "\n", __func__);
270         return snprintf(buf, MAX_STR_PRINT, " Rd overheat Error\n");
271 }
272
273 static ssize_t nct1008_set_temp_overheat(struct device *dev,
274                         struct device_attribute *attr,
275                         const char *buf, size_t count)
276 {
277         long int num;
278         int err;
279         u8 temp;
280         long currTemp;
281         struct i2c_client *client = to_i2c_client(dev);
282         struct nct1008_platform_data *pdata = client->dev.platform_data;
283         char bufTemp[MAX_STR_PRINT];
284         char bufOverheat[MAX_STR_PRINT];
285         unsigned int ret;
286
287         if (strict_strtol(buf, 0, &num)) {
288                 dev_err(dev, "\n file: %s, line=%d return %s() ", __FILE__,
289                         __LINE__, __func__);
290                 return -EINVAL;
291         }
292         if (((int)num < NCT1008_MIN_TEMP) || ((int)num >= NCT1008_MAX_TEMP)) {
293                 dev_err(dev, "\n file: %s, line=%d return %s() ", __FILE__,
294                         __LINE__, __func__);
295                 return -EINVAL;
296         }
297         /* check for system power down */
298         err = nct1008_get_temp(dev, &currTemp, NULL);
299         if (err)
300                 goto error;
301
302         currTemp = MILLICELSIUS_TO_CELSIUS(currTemp);
303
304         if (currTemp >= (int)num) {
305                 ret = nct1008_show_temp(dev, attr, bufTemp);
306                 ret = nct1008_show_temp_overheat(dev, attr, bufOverheat);
307                 dev_err(dev, "\nCurrent temp: %s ", bufTemp);
308                 dev_err(dev, "\nOld overheat limit: %s ", bufOverheat);
309                 dev_err(dev, "\nReset from overheat: curr temp=%ld, "
310                         "new overheat temp=%d\n\n", currTemp, (int)num);
311         }
312
313         /* External temperature h/w shutdown limit */
314         temp = temperature_to_value(pdata->ext_range, (s16)num);
315         err = nct1008_write_reg(client, EXT_THERM_LIMIT_WR, temp);
316         if (err < 0)
317                 goto error;
318
319         /* Local temperature h/w shutdown limit */
320         temp = temperature_to_value(pdata->ext_range, (s16)num);
321         err = nct1008_write_reg(client, LOCAL_THERM_LIMIT_WR, temp);
322         if (err < 0)
323                 goto error;
324         return count;
325 error:
326         dev_err(dev, " %s: failed to set temperature-overheat\n", __func__);
327         return err;
328 }
329
330 static ssize_t nct1008_show_temp_alert(struct device *dev,
331                                 struct device_attribute *attr,
332                                 char *buf)
333 {
334         struct i2c_client *client = to_i2c_client(dev);
335         struct nct1008_platform_data *pdata = client->dev.platform_data;
336         int value;
337         s16 temp_hi, temp_lo;
338         /* External Temperature Throttling hi-limit */
339         value = nct1008_read_reg(client, EXT_TEMP_HI_LIMIT_HI_BYTE_RD);
340         if (value < 0)
341                 goto error;
342         temp_hi = value_to_temperature(pdata->ext_range, value);
343
344         /* External Temperature Throttling lo-limit */
345         value = nct1008_read_reg(client, EXT_TEMP_LO_LIMIT_HI_BYTE_RD);
346         if (value < 0)
347                 goto error;
348         temp_lo = value_to_temperature(pdata->ext_range, value);
349
350         return snprintf(buf, MAX_STR_PRINT, "lo:%d hi:%d\n", temp_lo, temp_hi);
351 error:
352         dev_err(dev, "%s: failed to read temperature-alert\n", __func__);
353         return snprintf(buf, MAX_STR_PRINT, " Rd alert Error\n");
354 }
355
356 static ssize_t nct1008_set_temp_alert(struct device *dev,
357                         struct device_attribute *attr,
358                         const char *buf, size_t count)
359 {
360         long int num;
361         int value;
362         int err;
363         struct i2c_client *client = to_i2c_client(dev);
364         struct nct1008_platform_data *pdata = client->dev.platform_data;
365
366         if (strict_strtol(buf, 0, &num)) {
367                 dev_err(dev, "\n file: %s, line=%d return %s() ", __FILE__,
368                         __LINE__, __func__);
369                 return -EINVAL;
370         }
371         if (((int)num < NCT1008_MIN_TEMP) || ((int)num >= NCT1008_MAX_TEMP)) {
372                 dev_err(dev, "\n file: %s, line=%d return %s() ", __FILE__,
373                         __LINE__, __func__);
374                 return -EINVAL;
375         }
376
377         /* External Temperature Throttling limit */
378         value = temperature_to_value(pdata->ext_range, (s16)num);
379         err = nct1008_write_reg(client, EXT_TEMP_HI_LIMIT_HI_BYTE_WR, value);
380         if (err < 0)
381                 goto error;
382
383         /* Local Temperature Throttling limit */
384         err = nct1008_write_reg(client, LOCAL_TEMP_HI_LIMIT_WR, value);
385         if (err < 0)
386                 goto error;
387
388         return count;
389 error:
390         dev_err(dev, "%s: failed to set temperature-alert "
391                 "\n", __func__);
392         return err;
393 }
394
395 static ssize_t nct1008_show_ext_temp(struct device *dev,
396         struct device_attribute *attr, char *buf)
397 {
398         struct i2c_client *client = to_i2c_client(dev);
399         struct nct1008_platform_data *pdata = client->dev.platform_data;
400         s16 temp_value;
401         int data = 0;
402         int data_lo;
403
404         if (!dev || !buf || !attr)
405                 return -EINVAL;
406
407         /* When reading the full external temperature value, read the
408          * LSB first. This causes the MSB to be locked (that is, the
409          * ADC does not write to it) until it is read */
410         data_lo = nct1008_read_reg(client, EXT_TEMP_RD_LO);
411         if (data_lo < 0) {
412                 dev_err(&client->dev, "%s: failed to read "
413                         "ext_temperature, i2c error=%d\n", __func__, data_lo);
414                 goto error;
415         }
416
417         data = nct1008_read_reg(client, EXT_TEMP_RD_HI);
418         if (data < 0) {
419                 dev_err(&client->dev, "%s: failed to read "
420                         "ext_temperature, i2c error=%d\n", __func__, data);
421                 goto error;
422         }
423
424         temp_value = value_to_temperature(pdata->ext_range, data);
425
426         return snprintf(buf, MAX_STR_PRINT, "%d.%d\n", temp_value,
427                 (25 * (data_lo >> 6)));
428 error:
429         return snprintf(buf, MAX_STR_PRINT, "Error read ext temperature\n");
430 }
431
432 static DEVICE_ATTR(temperature, S_IRUGO, nct1008_show_temp, NULL);
433 static DEVICE_ATTR(temperature_overheat, (S_IRUGO | (S_IWUSR | S_IWGRP)),
434                 nct1008_show_temp_overheat, nct1008_set_temp_overheat);
435 static DEVICE_ATTR(temperature_alert, (S_IRUGO | (S_IWUSR | S_IWGRP)),
436                 nct1008_show_temp_alert, nct1008_set_temp_alert);
437 static DEVICE_ATTR(ext_temperature, S_IRUGO, nct1008_show_ext_temp, NULL);
438
439 static struct attribute *nct1008_attributes[] = {
440         &dev_attr_temperature.attr,
441         &dev_attr_temperature_overheat.attr,
442         &dev_attr_temperature_alert.attr,
443         &dev_attr_ext_temperature.attr,
444         NULL
445 };
446
447 static const struct attribute_group nct1008_attr_group = {
448         .attrs = nct1008_attributes,
449 };
450
451 static int nct1008_thermal_set_limits(struct nct1008_data *data,
452                                       long lo_limit_milli,
453                                       long hi_limit_milli)
454 {
455         int err;
456         u8 value;
457         bool extended_range = data->plat_data.ext_range;
458         long lo_limit = MILLICELSIUS_TO_CELSIUS(lo_limit_milli);
459         long hi_limit = MILLICELSIUS_TO_CELSIUS(hi_limit_milli);
460
461         if (lo_limit >= hi_limit)
462                 return -EINVAL;
463
464         if (data->current_lo_limit != lo_limit) {
465                 value = temperature_to_value(extended_range, lo_limit);
466                 pr_debug("%s: set lo_limit %ld\n", __func__, lo_limit);
467                 err = nct1008_write_reg(data->client,
468                                 EXT_TEMP_LO_LIMIT_HI_BYTE_WR, value);
469                 if (err)
470                         return err;
471
472                 data->current_lo_limit = lo_limit;
473         }
474
475         if (data->current_hi_limit != hi_limit) {
476                 value = temperature_to_value(extended_range, hi_limit);
477                 pr_debug("%s: set hi_limit %ld\n", __func__, hi_limit);
478                 err = nct1008_write_reg(data->client,
479                                 EXT_TEMP_HI_LIMIT_HI_BYTE_WR, value);
480                 if (err)
481                         return err;
482
483                 data->current_hi_limit = hi_limit;
484         }
485
486         return 0;
487 }
488
489 #ifdef CONFIG_THERMAL
490 static void nct1008_update(struct nct1008_data *data)
491 {
492         struct thermal_zone_device *thz = data->nct_ext;
493         long low_temp = 0, high_temp = NCT1008_MAX_TEMP * 1000;
494         struct thermal_trip_info *trip_state;
495         long temp, trip_temp, hysteresis_temp;
496         int count;
497         enum events type = 0;
498
499         if (!thz)
500                 return;
501
502         thermal_zone_device_update(thz);
503
504         thz->ops->get_temp(thz, &temp);
505
506         for (count = 0; count < thz->trips; count++) {
507                 trip_state = &data->plat_data.trips[count];
508                 trip_temp = trip_state->trip_temp;
509                 hysteresis_temp = trip_temp - trip_state->hysteresis;
510                 if ((trip_state->trip_type == THERMAL_TRIP_PASSIVE) &&
511                     !trip_state->tripped)
512                         hysteresis_temp = trip_temp;
513
514                 if ((trip_temp >= temp) && (trip_temp < high_temp)) {
515                         high_temp = trip_temp;
516                         type = THERMAL_AUX1;
517                 }
518
519                 if ((hysteresis_temp < temp) && (hysteresis_temp > low_temp)) {
520                         low_temp = hysteresis_temp;
521                         type = THERMAL_AUX0;
522                 }
523         }
524
525         thermal_generate_netlink_event(thz->id, type);
526         nct1008_thermal_set_limits(data, low_temp, high_temp);
527 }
528
529 static int nct1008_ext_get_temp(struct thermal_zone_device *thz,
530                                         unsigned long *temp)
531 {
532         struct nct1008_data *data = thz->devdata;
533         struct i2c_client *client = data->client;
534         struct nct1008_platform_data *pdata = client->dev.platform_data;
535         s16 temp_ext_hi;
536         s16 temp_ext_lo;
537         long temp_ext_milli;
538         u8 value;
539
540         /* Read External Temp */
541         value = nct1008_read_reg(client, EXT_TEMP_RD_LO);
542         if (value < 0)
543                 return -1;
544         temp_ext_lo = (value >> 6);
545
546         value = nct1008_read_reg(client, EXT_TEMP_RD_HI);
547         if (value < 0)
548                 return -1;
549         temp_ext_hi = value_to_temperature(pdata->ext_range, value);
550
551         temp_ext_milli = CELSIUS_TO_MILLICELSIUS(temp_ext_hi) +
552                          temp_ext_lo * 250;
553         *temp = temp_ext_milli;
554         data->etemp = temp_ext_milli;
555
556         return 0;
557 }
558
559 static int nct1008_ext_bind(struct thermal_zone_device *thz,
560                             struct thermal_cooling_device *cdev)
561 {
562         struct nct1008_data *data = thz->devdata;
563         int i;
564         bool bind = false;
565
566         for (i = 0; i < data->plat_data.num_trips; i++) {
567                 if (!strcmp(data->plat_data.trips[i].cdev_type, cdev->type)) {
568                         thermal_zone_bind_cooling_device(thz, i, cdev,
569                                         data->plat_data.trips[i].upper,
570                                         data->plat_data.trips[i].lower);
571                         bind = true;
572                 }
573         }
574
575         if (bind)
576                 nct1008_update(data);
577
578         return 0;
579 }
580
581 static int nct1008_ext_unbind(struct thermal_zone_device *thz,
582                               struct thermal_cooling_device *cdev)
583 {
584         struct nct1008_data *data = thz->devdata;
585         int i;
586
587         for (i = 0; i < data->plat_data.num_trips; i++) {
588                 if (!strcmp(data->plat_data.trips[i].cdev_type, cdev->type))
589                         thermal_zone_unbind_cooling_device(thz, i, cdev);
590         }
591         return 0;
592 }
593
594 static int nct1008_ext_get_trip_temp(struct thermal_zone_device *thz,
595                                      int trip,
596                                      unsigned long *temp)
597 {
598         struct nct1008_data *data = thz->devdata;
599         struct thermal_trip_info *trip_state = &data->plat_data.trips[trip];
600
601         *temp = trip_state->trip_temp;
602
603         if (trip_state->trip_type != THERMAL_TRIP_PASSIVE)
604                 return 0;
605
606         if (thz->temperature >= *temp) {
607                 trip_state->tripped = true;
608         } else if (trip_state->tripped) {
609                 *temp -= trip_state->hysteresis;
610                 if (thz->temperature < *temp)
611                         trip_state->tripped = false;
612         }
613
614         return 0;
615 }
616
617 static int nct1008_ext_set_trip_temp(struct thermal_zone_device *thz,
618                                      int trip,
619                                      unsigned long temp)
620 {
621         struct nct1008_data *data = thz->devdata;
622
623         data->plat_data.trips[trip].trip_temp = temp;
624         nct1008_update(data);
625         return 0;
626 }
627
628 static int nct1008_ext_get_trip_type(struct thermal_zone_device *thz,
629                                      int trip,
630                                      enum thermal_trip_type *type)
631 {
632         struct nct1008_data *data = thz->devdata;
633
634         *type = data->plat_data.trips[trip].trip_type;
635         return 0;
636 }
637
638 static int nct1008_ext_get_trend(struct thermal_zone_device *thz,
639                                  int trip,
640                                  enum thermal_trend *trend)
641 {
642         struct nct1008_data *data = thz->devdata;
643         struct thermal_trip_info *trip_state;
644
645         trip_state = &data->plat_data.trips[trip];
646
647         switch (trip_state->trip_type) {
648         case THERMAL_TRIP_ACTIVE:
649                 /* aggressive active cooling */
650                 *trend = THERMAL_TREND_RAISING;
651                 break;
652         case THERMAL_TRIP_PASSIVE:
653                 if (data->etemp > trip_state->trip_temp)
654                         *trend = THERMAL_TREND_RAISING;
655                 else
656                         *trend = THERMAL_TREND_DROPPING;
657                 break;
658         default:
659                 return -EINVAL;
660         }
661
662         return 0;
663 }
664
665 static int nct1008_int_get_temp(struct thermal_zone_device *thz,
666                                 unsigned long *temp)
667 {
668         struct nct1008_data *data = thz->devdata;
669         struct i2c_client *client = data->client;
670         struct nct1008_platform_data *pdata = client->dev.platform_data;
671         s16 temp_local;
672         long temp_local_milli;
673         u8 value;
674
675         /* Read Local Temp */
676         value = nct1008_read_reg(client, LOCAL_TEMP_RD);
677         if (value < 0)
678                 return -1;
679         temp_local = value_to_temperature(pdata->ext_range, value);
680
681         temp_local_milli = CELSIUS_TO_MILLICELSIUS(temp_local);
682         *temp = temp_local_milli;
683
684         return 0;
685 }
686
687 static int nct1008_int_bind(struct thermal_zone_device *thz,
688                             struct thermal_cooling_device *cdev)
689 {
690         return 0;
691 }
692
693 static int nct1008_int_get_trip_temp(struct thermal_zone_device *thz,
694                                      int trip,
695                                      unsigned long *temp)
696 {
697         return -1;
698 }
699
700 static int nct1008_int_get_trip_type(struct thermal_zone_device *thz,
701                                      int trip,
702                                      enum thermal_trip_type *type)
703 {
704         return -1;
705 }
706
707 static struct thermal_zone_device_ops nct_int_ops = {
708         .get_temp = nct1008_int_get_temp,
709         .bind = nct1008_int_bind,
710         .unbind = nct1008_int_bind,
711         .get_trip_type = nct1008_int_get_trip_type,
712         .get_trip_temp = nct1008_int_get_trip_temp,
713 };
714
715 static struct thermal_zone_device_ops nct_ext_ops = {
716         .get_temp = nct1008_ext_get_temp,
717         .bind = nct1008_ext_bind,
718         .unbind = nct1008_ext_unbind,
719         .get_trip_type = nct1008_ext_get_trip_type,
720         .get_trip_temp = nct1008_ext_get_trip_temp,
721         .set_trip_temp = nct1008_ext_set_trip_temp,
722         .get_trend = nct1008_ext_get_trend,
723 };
724 #else
725 static void nct1008_update(struct nct1008_data *data)
726 {
727 }
728 #endif /* CONFIG_THERMAL */
729
730 #ifdef CONFIG_DEBUG_FS
731 #include <linux/debugfs.h>
732 #include <linux/seq_file.h>
733 static void print_reg(const char *reg_name, struct seq_file *s,
734                 int offset)
735 {
736         struct nct1008_data *nct_data = s->private;
737         int ret;
738
739         ret = nct1008_read_reg(nct_data->client, offset);
740         if (ret >= 0)
741                 seq_printf(s, "Reg %s Addr = 0x%02x Reg 0x%02x "
742                 "Value 0x%02x\n", reg_name,
743                 nct_data->client->addr,
744                         offset, ret);
745         else
746                 seq_printf(s, "%s: line=%d, i2c read error=%d\n",
747                 __func__, __LINE__, ret);
748 }
749
750 static int dbg_nct1008_show(struct seq_file *s, void *unused)
751 {
752         seq_printf(s, "NCT Thermal Sensor Registers\n");
753         seq_printf(s, "------------------\n");
754         print_reg("Local Temp Value    ",     s, 0x00);
755         print_reg("Ext Temp Value Hi   ",     s, 0x01);
756         print_reg("Status              ",     s, 0x02);
757         print_reg("Configuration       ",     s, 0x03);
758         print_reg("Conversion Rate     ",     s, 0x04);
759         print_reg("Local Temp Hi Limit ",     s, 0x05);
760         print_reg("Local Temp Lo Limit ",     s, 0x06);
761         print_reg("Ext Temp Hi Limit Hi",     s, 0x07);
762         print_reg("Ext Temp Hi Limit Lo",     s, 0x13);
763         print_reg("Ext Temp Lo Limit Hi",     s, 0x08);
764         print_reg("Ext Temp Lo Limit Lo",     s, 0x14);
765         print_reg("Ext Temp Value Lo   ",     s, 0x10);
766         print_reg("Ext Temp Offset Hi  ",     s, 0x11);
767         print_reg("Ext Temp Offset Lo  ",     s, 0x12);
768         print_reg("Ext THERM Limit     ",     s, 0x19);
769         print_reg("Local THERM Limit   ",     s, 0x20);
770         print_reg("THERM Hysteresis    ",     s, 0x21);
771         print_reg("Consecutive ALERT   ",     s, 0x22);
772         return 0;
773 }
774
775 static int dbg_nct1008_open(struct inode *inode, struct file *file)
776 {
777         return single_open(file, dbg_nct1008_show, inode->i_private);
778 }
779
780 static const struct file_operations debug_fops = {
781         .open           = dbg_nct1008_open,
782         .read           = seq_read,
783         .llseek         = seq_lseek,
784         .release        = single_release,
785 };
786
787 static int nct1008_debuginit(struct nct1008_data *nct)
788 {
789         int err = 0;
790         struct dentry *d;
791         char *name = nct1008_id[nct->chip].name;
792
793         /* create debugfs by selecting chipid */
794         d = debugfs_create_file(name, S_IRUGO, NULL,
795                 (void *)nct, &debug_fops);
796
797         if ((!d) || IS_ERR(d)) {
798                 dev_err(&nct->client->dev, "Error: %s debugfs_create_file"
799                         " returned an error\n", __func__);
800                 err = -ENOENT;
801                 goto end;
802         }
803         if (d == ERR_PTR(-ENODEV)) {
804                 dev_err(&nct->client->dev, "Error: %s debugfs not supported "
805                         "error=-ENODEV\n", __func__);
806                 err = -ENODEV;
807         } else {
808                 nct->dent = d;
809         }
810 end:
811         return err;
812 }
813 #else
814 static int nct1008_debuginit(struct nct1008_data *nct)
815 {
816         return 0;
817 }
818 #endif /* CONFIG_DEBUG_FS */
819
820 static int nct1008_enable(struct i2c_client *client)
821 {
822         struct nct1008_data *data = i2c_get_clientdata(client);
823         int err;
824
825         err = nct1008_write_reg(client, CONFIG_WR, data->config);
826         if (err < 0)
827                 dev_err(&client->dev, "%s, line=%d, i2c write error=%d\n",
828                 __func__, __LINE__, err);
829         return err;
830 }
831
832 static int nct1008_disable(struct i2c_client *client)
833 {
834         struct nct1008_data *data = i2c_get_clientdata(client);
835         int err;
836
837         err = nct1008_write_reg(client, CONFIG_WR,
838                                 data->config | STANDBY_BIT);
839         if (err < 0)
840                 dev_err(&client->dev, "%s, line=%d, i2c write error=%d\n",
841                 __func__, __LINE__, err);
842         return err;
843 }
844
845 static int nct1008_within_limits(struct nct1008_data *data)
846 {
847         int intr_status;
848
849         intr_status = nct1008_read_reg(data->client, STATUS_RD);
850         if (intr_status < 0)
851                 return intr_status;
852
853         return !(intr_status & (BIT(3) | BIT(4)));
854 }
855
856 static void nct1008_work_func(struct work_struct *work)
857 {
858         struct nct1008_data *data = container_of(work, struct nct1008_data,
859                                                 work);
860         int err;
861         struct timespec ts;
862
863         err = nct1008_disable(data->client);
864         if (err == -ENODEV)
865                 return;
866
867         if (!nct1008_within_limits(data))
868                 nct1008_update(data);
869
870         /* Initiate one-shot conversion */
871         nct1008_write_reg(data->client, ONE_SHOT, 0x1);
872
873         /* Give hardware necessary time to finish conversion */
874         ts = ns_to_timespec(MAX_CONV_TIME_ONESHOT_MS * 1000 * 1000);
875         hrtimer_nanosleep(&ts, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
876
877         nct1008_read_reg(data->client, STATUS_RD);
878
879         nct1008_enable(data->client);
880
881         enable_irq(data->client->irq);
882 }
883
884 static irqreturn_t nct1008_irq(int irq, void *dev_id)
885 {
886         struct nct1008_data *data = dev_id;
887
888         disable_irq_nosync(irq);
889         queue_work(data->workqueue, &data->work);
890
891         return IRQ_HANDLED;
892 }
893
894 static void nct1008_power_control(struct nct1008_data *data, bool is_enable)
895 {
896         int ret;
897         char *name = nct1008_id[data->chip].name;
898
899         if (!data->nct_reg) {
900                 data->nct_reg = regulator_get(&data->client->dev, "vdd");
901                 if (IS_ERR_OR_NULL(data->nct_reg)) {
902                         if (PTR_ERR(data->nct_reg) == -ENODEV)
903                                 dev_info(&data->client->dev,
904                                         "no regulator found for vdd."
905                                         " Assuming vdd is always powered");
906                         else
907                                 dev_warn(&data->client->dev, "Error [%ld] in "
908                                         "getting the regulator handle for"
909                                         " vdd\n", PTR_ERR(data->nct_reg));
910                         data->nct_reg = NULL;
911                         return;
912                 }
913         }
914         if (is_enable) {
915                 ret = regulator_enable(data->nct_reg);
916                 msleep(POWER_ON_DELAY);
917         } else {
918                 ret = regulator_disable(data->nct_reg);
919         }
920
921         if (ret < 0)
922                 dev_err(&data->client->dev, "Error in %s rail vdd_%s, "
923                         "error %d\n", (is_enable) ? "enabling" : "disabling",
924                         name,
925                         ret);
926         else
927                 dev_info(&data->client->dev, "success in %s rail vdd_nct%s\n",
928                         (is_enable) ? "enabling" : "disabling",
929                         name);
930
931
932 }
933
934 static int nct1008_configure_sensor(struct nct1008_data *data)
935 {
936         struct i2c_client *client = data->client;
937         struct nct1008_platform_data *pdata = client->dev.platform_data;
938         u8 value;
939         s16 temp;
940         u8 temp2;
941         int err;
942
943         if (!pdata || !pdata->supported_hwrev)
944                 return -ENODEV;
945
946         /* Initially place in Standby */
947         err = nct1008_write_reg(client, CONFIG_WR, STANDBY_BIT);
948         if (err)
949                 goto error;
950
951         /* External temperature h/w shutdown limit */
952         value = temperature_to_value(pdata->ext_range,
953                                         pdata->shutdown_ext_limit);
954         err = nct1008_write_reg(client, EXT_THERM_LIMIT_WR, value);
955         if (err)
956                 goto error;
957
958         /* Local temperature h/w shutdown limit */
959         value = temperature_to_value(pdata->ext_range,
960                                         pdata->shutdown_local_limit);
961         err = nct1008_write_reg(client, LOCAL_THERM_LIMIT_WR, value);
962         if (err)
963                 goto error;
964
965         /* set extended range mode if needed */
966         if (pdata->ext_range)
967                 data->config |= EXTENDED_RANGE_BIT;
968         data->config &= ~(THERM2_BIT | ALERT_BIT);
969
970         err = nct1008_write_reg(client, CONFIG_WR, data->config);
971         if (err)
972                 goto error;
973
974         /* Temperature conversion rate */
975         err = nct1008_write_reg(client, CONV_RATE_WR, pdata->conv_rate);
976         if (err)
977                 goto error;
978
979         data->conv_period_ms = conv_period_ms_table[pdata->conv_rate];
980
981         /* Setup local hi and lo limits */
982         err = nct1008_write_reg(client,
983                 LOCAL_TEMP_HI_LIMIT_WR, NCT1008_MAX_TEMP);
984         if (err)
985                 goto error;
986
987         err = nct1008_write_reg(client, LOCAL_TEMP_LO_LIMIT_WR, 0);
988         if (err)
989                 goto error;
990
991         /* Setup external hi and lo limits */
992         err = nct1008_write_reg(client, EXT_TEMP_LO_LIMIT_HI_BYTE_WR, 0);
993         if (err)
994                 goto error;
995         err = nct1008_write_reg(client, EXT_TEMP_HI_LIMIT_HI_BYTE_WR,
996                         NCT1008_MAX_TEMP);
997         if (err)
998                 goto error;
999
1000         /* read initial temperature */
1001         value = nct1008_read_reg(client, LOCAL_TEMP_RD);
1002         if (value < 0) {
1003                 err = value;
1004                 goto error;
1005         }
1006         temp = value_to_temperature(pdata->ext_range, value);
1007         dev_dbg(&client->dev, "\n initial local temp = %d ", temp);
1008
1009         value = nct1008_read_reg(client, EXT_TEMP_RD_LO);
1010         if (value < 0) {
1011                 err = value;
1012                 goto error;
1013         }
1014         temp2 = (value >> 6);
1015         value = nct1008_read_reg(client, EXT_TEMP_RD_HI);
1016         if (value < 0) {
1017                 err = value;
1018                 goto error;
1019         }
1020         temp = value_to_temperature(pdata->ext_range, value);
1021
1022         if (temp2 > 0)
1023                 dev_dbg(&client->dev, "\n initial ext temp = %d.%d deg",
1024                                 temp, temp2 * 25);
1025         else
1026                 dev_dbg(&client->dev, "\n initial ext temp = %d.0 deg", temp);
1027
1028         /* Remote channel offset */
1029         err = nct1008_write_reg(client, OFFSET_WR, pdata->offset / 4);
1030         if (err < 0)
1031                 goto error;
1032
1033         /* Remote channel offset fraction (quarters) */
1034         err = nct1008_write_reg(client, OFFSET_QUARTER_WR,
1035                                         (pdata->offset % 4) << 6);
1036         if (err < 0)
1037                 goto error;
1038
1039         /* Reset current hi/lo limit values with register values */
1040         value = nct1008_read_reg(data->client, EXT_TEMP_LO_LIMIT_HI_BYTE_RD);
1041         if (value < 0) {
1042                 err = value;
1043                 goto error;
1044         }
1045         data->current_lo_limit = value_to_temperature(pdata->ext_range, value);
1046
1047         value = nct1008_read_reg(data->client, EXT_TEMP_HI_LIMIT_HI_BYTE_RD);
1048         if (value < 0) {
1049                 err = value;
1050                 goto error;
1051         }
1052         data->current_hi_limit = value_to_temperature(pdata->ext_range, value);
1053
1054         return 0;
1055 error:
1056         dev_err(&client->dev, "\n exit %s, err=%d ", __func__, err);
1057         return err;
1058 }
1059
1060 static int __devinit nct1008_configure_irq(struct nct1008_data *data)
1061 {
1062         char *name = nct1008_id[data->chip].name;
1063
1064         data->workqueue = create_singlethread_workqueue(name);
1065
1066         INIT_WORK(&data->work, nct1008_work_func);
1067
1068         if (data->client->irq < 0)
1069                 return 0;
1070         else
1071                 return request_irq(data->client->irq, nct1008_irq,
1072                         IRQF_TRIGGER_LOW,
1073                         name,
1074                         data);
1075 }
1076
1077 /*
1078  * Manufacturer(OnSemi) recommended sequence for
1079  * Extended Range mode is as follows
1080  * 1. Place in Standby
1081  * 2. Scale the THERM and ALERT limits
1082  *      appropriately(for Extended Range mode).
1083  * 3. Enable Extended Range mode.
1084  *      ALERT mask/THERM2 mode may be done here
1085  *      as these are not critical
1086  * 4. Set Conversion Rate as required
1087  * 5. Take device out of Standby
1088  */
1089
1090 /*
1091  * function nct1008_probe takes care of initial configuration
1092  */
1093 static int __devinit nct1008_probe(struct i2c_client *client,
1094                                 const struct i2c_device_id *id)
1095 {
1096         struct nct1008_data *data;
1097         int err;
1098         int i;
1099         int mask = 0;
1100         char nct_int_name[THERMAL_NAME_LENGTH];
1101         char nct_ext_name[THERMAL_NAME_LENGTH];
1102
1103         data = kzalloc(sizeof(struct nct1008_data), GFP_KERNEL);
1104         if (!data)
1105                 return -ENOMEM;
1106
1107         data->client = client;
1108         data->chip = id->driver_data;
1109         memcpy(&data->plat_data, client->dev.platform_data,
1110                 sizeof(struct nct1008_platform_data));
1111         i2c_set_clientdata(client, data);
1112         mutex_init(&data->mutex);
1113
1114         nct1008_power_control(data, true);
1115         if (!data->nct_reg) {
1116                 /* power up failure */
1117                 err = -EIO;
1118                 goto cleanup;
1119         }
1120         /* extended range recommended steps 1 through 4 taken care
1121          * in nct1008_configure_sensor function */
1122         err = nct1008_configure_sensor(data);   /* sensor is in standby */
1123         if (err < 0) {
1124                 dev_err(&client->dev, "\n error file: %s : %s(), line=%d ",
1125                         __FILE__, __func__, __LINE__);
1126                 goto error;
1127         }
1128
1129         err = nct1008_configure_irq(data);
1130         if (err < 0) {
1131                 dev_err(&client->dev, "\n error file: %s : %s(), line=%d ",
1132                         __FILE__, __func__, __LINE__);
1133                 goto error;
1134         }
1135         dev_info(&client->dev, "%s: initialized\n", __func__);
1136
1137         /* extended range recommended step 5 is in nct1008_enable function */
1138         err = nct1008_enable(client);           /* sensor is running */
1139         if (err < 0) {
1140                 dev_err(&client->dev, "Error: %s, line=%d, error=%d\n",
1141                         __func__, __LINE__, err);
1142                 goto error;
1143         }
1144
1145         /* register sysfs hooks */
1146         err = sysfs_create_group(&client->dev.kobj, &nct1008_attr_group);
1147         if (err < 0) {
1148                 dev_err(&client->dev, "\n sysfs create err=%d ", err);
1149                 goto error;
1150         }
1151
1152         err = nct1008_debuginit(data);
1153         if (err < 0)
1154                 err = 0; /* without debugfs we may continue */
1155
1156 #ifdef CONFIG_THERMAL
1157         for (i = 0; i < data->plat_data.num_trips; i++)
1158                 mask |= (1 << i);
1159
1160         if (data->plat_data.loc_name) {
1161                 strcpy(nct_int_name, "Tboard_");
1162                 strcpy(nct_ext_name, "Tdiode_");
1163                 strncat(nct_int_name, data->plat_data.loc_name,
1164                         (THERMAL_NAME_LENGTH - strlen("Tboard_")) - 1);
1165                 strncat(nct_ext_name, data->plat_data.loc_name,
1166                         (THERMAL_NAME_LENGTH - strlen("Tdiode_")) - 1);
1167         } else {
1168                 strcpy(nct_int_name, "Tboard");
1169                 strcpy(nct_ext_name, "Tdiode");
1170         }
1171
1172         data->nct_int = thermal_zone_device_register(nct_int_name,
1173                                                 0,
1174                                                 0x0,
1175                                                 data,
1176                                                 &nct_int_ops,
1177                                                 NULL,
1178                                                 2000,
1179                                                 0);
1180         if (IS_ERR_OR_NULL(data->nct_int))
1181                 goto error;
1182
1183         data->nct_ext = thermal_zone_device_register(nct_ext_name,
1184                                         data->plat_data.num_trips,
1185                                         mask,
1186                                         data,
1187                                         &nct_ext_ops,
1188                                         data->plat_data.tzp,
1189                                         data->plat_data.passive_delay,
1190                                         0);
1191         if (IS_ERR_OR_NULL(data->nct_ext)) {
1192                 thermal_zone_device_unregister(data->nct_int);
1193                 data->nct_int = NULL;
1194                 goto error;
1195         }
1196
1197         nct1008_update(data);
1198 #endif
1199         return 0;
1200
1201 error:
1202         dev_err(&client->dev, "\n exit %s, err=%d ", __func__, err);
1203         nct1008_power_control(data, false);
1204 cleanup:
1205         mutex_destroy(&data->mutex);
1206         if (data->nct_reg)
1207                 regulator_put(data->nct_reg);
1208         kfree(data);
1209         return err;
1210 }
1211
1212 static int __devexit nct1008_remove(struct i2c_client *client)
1213 {
1214         struct nct1008_data *data = i2c_get_clientdata(client);
1215
1216         if (data->dent)
1217                 debugfs_remove(data->dent);
1218
1219         free_irq(data->client->irq, data);
1220         cancel_work_sync(&data->work);
1221         sysfs_remove_group(&client->dev.kobj, &nct1008_attr_group);
1222         nct1008_power_control(data, false);
1223         if (data->nct_reg)
1224                 regulator_put(data->nct_reg);
1225         mutex_destroy(&data->mutex);
1226         kfree(data);
1227
1228         return 0;
1229 }
1230
1231 static void nct1008_shutdown(struct i2c_client *client)
1232 {
1233         struct nct1008_data *data = i2c_get_clientdata(client);
1234         if (client->irq)
1235                 disable_irq(client->irq);
1236
1237         cancel_work_sync(&data->work);
1238
1239         mutex_lock(&data->mutex);
1240         data->shutdown_complete = 1;
1241         mutex_unlock(&data->mutex);
1242 }
1243
1244 #ifdef CONFIG_PM_SLEEP
1245 static int nct1008_suspend_powerdown(struct device *dev)
1246 {
1247         struct i2c_client *client = to_i2c_client(dev);
1248         int err;
1249         struct nct1008_data *data = i2c_get_clientdata(client);
1250
1251         disable_irq(client->irq);
1252         err = nct1008_disable(client);
1253         nct1008_power_control(data, false);
1254         return err;
1255 }
1256
1257 static int nct1008_suspend_wakeup(struct device *dev)
1258 {
1259         struct i2c_client *client = to_i2c_client(dev);
1260         int err;
1261         struct nct1008_data *data = i2c_get_clientdata(client);
1262         long ext_temp;
1263
1264         err = nct1008_get_temp(dev, &ext_temp, 0);
1265         if (err)
1266                 goto error;
1267
1268         if (ext_temp > data->plat_data.suspend_ext_limit_lo)
1269                 err = nct1008_thermal_set_limits(data,
1270                         data->plat_data.suspend_ext_limit_lo,
1271                         NCT1008_MAX_TEMP * 1000);
1272         else
1273                 err = nct1008_thermal_set_limits(data,
1274                         NCT1008_MIN_TEMP * 1000,
1275                         data->plat_data.suspend_ext_limit_hi);
1276
1277         if (err)
1278                 goto error;
1279
1280         /* Enable NCT wake */
1281         err = enable_irq_wake(client->irq);
1282         if (err)
1283                 dev_err(&client->dev, "Error: %s, error=%d. failed to enable NCT "
1284                                 "wakeup\n", __func__, err);
1285
1286
1287         return err;
1288
1289 error:
1290         dev_err(&client->dev, "\n error in file=: %s %s() line=%d: "
1291                 "error=%d. Can't set correct LP1 alarm limits or set wakeup irq, "
1292                 "shutting down device", __FILE__, __func__, __LINE__, err);
1293
1294         return nct1008_suspend_powerdown(dev);
1295 }
1296
1297 static int nct1008_suspend(struct device *dev)
1298 {
1299         struct i2c_client *client = to_i2c_client(dev);
1300         struct nct1008_data *data = i2c_get_clientdata(client);
1301
1302         if (data->plat_data.suspend_with_wakeup &&
1303                 data->plat_data.suspend_with_wakeup())
1304                 return nct1008_suspend_wakeup(dev);
1305         else
1306                 return nct1008_suspend_powerdown(dev);
1307 }
1308
1309
1310 static int nct1008_resume_wakeup(struct device *dev)
1311 {
1312         int err = 0;
1313         struct i2c_client *client = to_i2c_client(dev);
1314
1315         err = disable_irq_wake(client->irq);
1316         if (err) {
1317                 dev_err(&client->dev, "Error: %s, error=%d. failed to disable NCT "
1318                                 "wakeup\n", __func__, err);
1319                 return err;
1320         }
1321
1322         /* NCT wasn't powered down, so IRQ is still enabled. */
1323         /* Disable it before calling update */
1324         disable_irq(client->irq);
1325
1326         return err;
1327 }
1328
1329 static int nct1008_resume_powerdown(struct device *dev)
1330 {
1331         struct i2c_client *client = to_i2c_client(dev);
1332         int err = 0;
1333         struct nct1008_data *data = i2c_get_clientdata(client);
1334
1335         nct1008_power_control(data, true);
1336         nct1008_configure_sensor(data);
1337         err = nct1008_enable(client);
1338         if (err < 0) {
1339                 dev_err(&client->dev, "Error: %s, error=%d\n",
1340                         __func__, err);
1341                 return err;
1342         }
1343
1344         return err;
1345 }
1346
1347 static int nct1008_resume(struct device *dev)
1348 {
1349         struct i2c_client *client = to_i2c_client(dev);
1350         int err;
1351         struct nct1008_data *data = i2c_get_clientdata(client);
1352
1353         if (data->plat_data.suspend_with_wakeup &&
1354                 data->plat_data.suspend_with_wakeup())
1355                 err = nct1008_resume_wakeup(dev);
1356         else
1357                 err = nct1008_resume_powerdown(dev);
1358
1359         if (err)
1360                 return err;
1361
1362         nct1008_update(data);
1363         enable_irq(client->irq);
1364
1365         return 0;
1366 }
1367
1368 static const struct dev_pm_ops nct1008_pm_ops = {
1369         .suspend        = nct1008_suspend,
1370         .resume         = nct1008_resume,
1371 };
1372
1373 #endif
1374
1375 MODULE_DEVICE_TABLE(i2c, nct1008_id);
1376
1377 static struct i2c_driver nct1008_driver = {
1378         .driver = {
1379                 .name   = "nct_thermal",
1380 #ifdef CONFIG_PM_SLEEP
1381                 .pm = &nct1008_pm_ops,
1382 #endif
1383         },
1384         .probe          = nct1008_probe,
1385         .remove         = __devexit_p(nct1008_remove),
1386         .id_table       = nct1008_id,
1387         .shutdown       = nct1008_shutdown,
1388 };
1389
1390 static int __init nct1008_init(void)
1391 {
1392         return i2c_add_driver(&nct1008_driver);
1393 }
1394
1395 static void __exit nct1008_exit(void)
1396 {
1397         i2c_del_driver(&nct1008_driver);
1398 }
1399
1400 MODULE_DESCRIPTION("Temperature sensor driver for OnSemi NCT1008/NCT72");
1401 MODULE_LICENSE("GPL");
1402
1403 module_init(nct1008_init);
1404 module_exit(nct1008_exit);