misc: tegra-baseband: remove remote wakeup callback
[linux-3.10.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-2015, 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/of_gpio.h>
33 #include <linux/delay.h>
34 #include <linux/thermal.h>
35 #include <linux/regulator/consumer.h>
36
37 /* Register Addresses used in this module. */
38 #define LOC_TEMP_RD                  0x00
39 #define EXT_TEMP_HI_RD               0x01
40 #define STATUS_RD                    0x02
41 #define CONFIG_RD                    0x03
42 #define CONV_RATE_RD                 0x04
43 #define LOC_TEMP_HI_LIMIT_RD         0x05
44 #define LOC_TEMP_LO_LIMIT_RD         0x06
45 #define EXT_TEMP_HI_LIMIT_HI_BYTE_RD 0x07
46 #define EXT_TEMP_LO_LIMIT_HI_BYTE_RD 0x08
47 #define CONFIG_WR                    0x09
48 #define CONV_RATE_WR                 0x0A
49 #define LOC_TEMP_HI_LIMIT_WR         0x0B
50 #define LOC_TEMP_LO_LIMIT_WR         0x0C
51 #define EXT_TEMP_HI_LIMIT_HI_BYTE_WR 0x0D
52 #define EXT_TEMP_LO_LIMIT_HI_BYTE_WR 0x0E
53 #define ONE_SHOT                     0x0F
54 #define EXT_TEMP_LO_RD               0x10
55 #define OFFSET_WR                    0x11
56 #define OFFSET_QUARTER_WR            0x12
57 #define EXT_TEMP_HI_LIMIT_LO_BYTE    0x13
58 #define EXT_TEMP_LO_LIMIT_LO_BYTE    0x14
59 /* NOT USED                          0x15 */
60 /* NOT USED                          0x16 */
61 /* NOT USED                          0x17 */
62 /* NOT USED                          0x18 */
63 #define EXT_THERM_LIMIT_WR           0x19
64 /* NOT USED                          0x1A */
65 /* NOT USED                          0x1B */
66 /* NOT USED                          0x1C */
67 /* NOT USED                          0x1D */
68 /* NOT USED                          0x1E */
69 /* NOT USED                          0x1F */
70 #define LOC_THERM_LIMIT              0x20
71 #define THERM_HYSTERESIS             0x21
72 #define COSECUTIVE_ALERT             0x22
73
74 /* Set of register types that are sensor dependant. */
75 enum nct1008_sensor_reg_types {
76         TEMP_HI_LIMIT, TEMP_LO_LIMIT,
77         TEMP_HI_LIMIT_RD, TEMP_LO_LIMIT_RD,
78         TEMP_HI_LIMIT_WR, TEMP_LO_LIMIT_WR,
79         TEMP_RD_LO, TEMP_RD_HI,
80         TEMP_RD, TEMP_WR,
81         REGS_COUNT /* This has to be the last element! */
82 };
83
84 /* Mapping from register type on a given sensor to hardware specific address. */
85 static int nct1008_sensor_regs[SENSORS_COUNT][REGS_COUNT] = {
86         [LOC] = {
87                 [TEMP_HI_LIMIT_RD] = LOC_TEMP_HI_LIMIT_RD,
88                 [TEMP_HI_LIMIT_WR] = LOC_TEMP_HI_LIMIT_WR,
89                 [TEMP_LO_LIMIT_RD] = LOC_TEMP_LO_LIMIT_RD,
90                 [TEMP_LO_LIMIT_WR] = LOC_TEMP_LO_LIMIT_WR,
91         },
92         [EXT] = {
93                 [TEMP_HI_LIMIT_RD] = EXT_TEMP_HI_LIMIT_HI_BYTE_RD,
94                 [TEMP_HI_LIMIT_WR] = EXT_TEMP_HI_LIMIT_HI_BYTE_WR,
95                 [TEMP_LO_LIMIT_RD] = EXT_TEMP_LO_LIMIT_HI_BYTE_RD,
96                 [TEMP_LO_LIMIT_WR] = EXT_TEMP_LO_LIMIT_HI_BYTE_WR,
97                 [TEMP_RD_LO] = EXT_TEMP_LO_RD,
98                 [TEMP_RD_HI] = EXT_TEMP_HI_RD,
99         },
100 };
101
102 /* Accessor to the sensor specific registers. */
103 #define NCT_REG(x, y) nct1008_sensor_regs[x][y]
104
105 /* Configuration register bits. */
106 #define EXTENDED_RANGE_BIT BIT(2)
107 #define THERM2_BIT         BIT(5)
108 #define STANDBY_BIT        BIT(6)
109 #define ALERT_BIT          BIT(7)
110
111 /* Status register trip point bits. */
112 #define EXT_LO_BIT BIT(3) /* External Sensor has tripped 'temp <= LOW' */
113 #define EXT_HI_BIT BIT(4) /* External Sensor has tripped 'temp > HIGH' */
114 #define LOC_LO_BIT BIT(5) /* Local Sensor has tripped 'temp <= LOW' */
115 #define LOC_HI_BIT BIT(6) /* Local Sensor has tripped 'temp > HIGH' */
116
117 /* Constants.  */
118 #define EXTENDED_RANGE_OFFSET 64U
119 #define STANDARD_RANGE_MAX    127U
120 #define EXTENDED_RANGE_MAX   (150U + EXTENDED_RANGE_OFFSET)
121
122 #define NCT1008_MIN_TEMP       (-64)
123 #define NCT1008_MAX_TEMP         191
124 #define NCT1008_MAX_TEMP_MILLI   191750
125
126 #define MAX_STR_PRINT            50
127 #define MAX_CONV_TIME_ONESHOT_MS 52
128
129 #define CELSIUS_TO_MILLICELSIUS(x) ((x)*1000)
130 #define MILLICELSIUS_TO_CELSIUS(x) ((x)/1000)
131
132 struct nct1008_adjust_offset_table {
133         int temp;
134         int offset;
135 };
136
137 struct nct1008_sensor_data {
138         struct nct1008_adjust_offset_table offset_table[16];
139         struct thermal_zone_device *thz;
140         long current_hi_limit;
141         long current_lo_limit;
142         int temp;
143 };
144
145 struct nct1008_data {
146         struct workqueue_struct *workqueue;
147         struct work_struct work;
148         struct i2c_client *client;
149         struct nct1008_platform_data plat_data;
150         struct mutex mutex;
151         u8 config;
152         enum nct1008_chip chip;
153         struct regulator *nct_reg;
154         int conv_period_ms;
155         int nct_disabled;
156         int stop_workqueue;
157
158         struct nct1008_sensor_data sensors[SENSORS_COUNT];
159 };
160
161 static int conv_period_ms_table[] =
162         {16000, 8000, 4000, 2000, 1000, 500, 250, 125, 63, 32, 16};
163
164 static void nct1008_setup_shutdown_warning(struct nct1008_data *data);
165
166 static inline s16 value_to_temperature(bool extended, u8 value)
167 {
168         return extended ? (s16)(value - EXTENDED_RANGE_OFFSET) : (s16)value;
169 }
170
171 static inline u8 temperature_to_value(bool extended, s16 temp)
172 {
173         return extended ? (u8)(temp + EXTENDED_RANGE_OFFSET) : (u8)temp;
174 }
175
176 static int nct1008_write_reg(struct i2c_client *client, u8 reg, u16 value)
177 {
178         int ret = 0;
179         struct nct1008_data *data = i2c_get_clientdata(client);
180
181         mutex_lock(&data->mutex);
182         if (data && data->nct_disabled) {
183                 mutex_unlock(&data->mutex);
184                 return -ENODEV;
185         }
186
187         ret = i2c_smbus_write_byte_data(client, reg, value);
188         mutex_unlock(&data->mutex);
189
190         if (ret < 0)
191                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
192
193         return ret;
194 }
195
196 static int nct1008_read_reg(struct i2c_client *client, u8 reg)
197 {
198         int ret = 0;
199         struct nct1008_data *data = i2c_get_clientdata(client);
200         mutex_lock(&data->mutex);
201         if (data && data->nct_disabled) {
202                 mutex_unlock(&data->mutex);
203                 return -ENODEV;
204         }
205
206         ret = i2c_smbus_read_byte_data(client, reg);
207         mutex_unlock(&data->mutex);
208
209         if (ret < 0)
210                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
211
212         return ret;
213 }
214
215 static int nct1008_get_temp_common(int sensor,
216                                         struct nct1008_data *data,
217                                         long *temp)
218 {
219         struct i2c_client *client = data->client;
220         struct nct1008_platform_data *pdata = client->dev.platform_data;
221         struct nct1008_sensor_data *sensorp;
222         s16 temp_hi;
223         s16 temp_lo;
224         long temp_milli = 0;
225         int i, off = 0;
226         u8 value;
227         int ret;
228
229         if (!((sensor == EXT) || (sensor == LOC)))
230                 return -1;
231
232         /* Read External Temp */
233         if (sensor == EXT) {
234                 ret = nct1008_read_reg(client, NCT_REG(sensor, TEMP_RD_LO));
235                 if (ret < 0)
236                         return -1;
237                 else
238                         value = ret;
239
240                 temp_lo = (value >> 6);
241
242                 ret = nct1008_read_reg(client, EXT_TEMP_HI_RD);
243                 if (ret < 0)
244                         return -1;
245                 else
246                         value = ret;
247
248                 temp_hi = value_to_temperature(pdata->extended_range, value);
249                 temp_milli = CELSIUS_TO_MILLICELSIUS(temp_hi) + temp_lo * 250;
250
251                 sensorp = &data->sensors[sensor];
252                 for (i = 0; i < ARRAY_SIZE(sensorp->offset_table); i++) {
253                         if (temp_milli <
254                                 (sensorp->offset_table[i].temp * 1000)) {
255                                 off = sensorp->offset_table[i].offset * 1000;
256                                 break;
257                         }
258                 }
259
260                 temp_milli += off;
261
262         } else if (sensor == LOC) {
263                 value = nct1008_read_reg(client, LOC_TEMP_RD);
264                 if (value < 0)
265                         return -1;
266                 temp_hi = value_to_temperature(pdata->extended_range, value);
267                 temp_milli = CELSIUS_TO_MILLICELSIUS(temp_hi);
268         }
269
270         if (temp_milli > NCT1008_MAX_TEMP_MILLI)
271                 return -1;
272
273         *temp = temp_milli;
274         data->sensors[sensor].temp = temp_milli;
275
276         return 0;
277 }
278
279 static ssize_t nct1008_show_temp(struct device *dev,
280         struct device_attribute *attr, char *buf)
281 {
282         struct i2c_client *client = to_i2c_client(dev);
283         struct nct1008_platform_data *pdata = client->dev.platform_data;
284         s16 temp1 = 0;
285         s16 temp = 0;
286         u8 temp2 = 0;
287         int value = 0;
288
289         if (!dev || !buf || !attr)
290                 return -EINVAL;
291
292         value = nct1008_read_reg(client, LOC_TEMP_RD);
293         if (value < 0)
294                 goto error;
295         temp1 = value_to_temperature(pdata->extended_range, value);
296
297         value = nct1008_read_reg(client, EXT_TEMP_LO_RD);
298         if (value < 0)
299                 goto error;
300         temp2 = (value >> 6);
301         value = nct1008_read_reg(client, EXT_TEMP_HI_RD);
302         if (value < 0)
303                 goto error;
304         temp = value_to_temperature(pdata->extended_range, value);
305
306         return snprintf(buf, MAX_STR_PRINT, "%d %d.%d\n",
307                 temp1, temp, temp2 * 25);
308
309 error:
310         return snprintf(buf, MAX_STR_PRINT,
311                 "Error read local/ext temperature\n");
312 }
313
314 static ssize_t nct1008_show_temp_overheat(struct device *dev,
315                                 struct device_attribute *attr,
316                                 char *buf)
317 {
318         struct i2c_client *client = to_i2c_client(dev);
319         struct nct1008_platform_data *pdata = client->dev.platform_data;
320         int value;
321         s16 temp, temp2;
322
323         /* Local temperature h/w shutdown limit */
324         value = nct1008_read_reg(client, LOC_THERM_LIMIT);
325         if (value < 0)
326                 goto error;
327         temp = value_to_temperature(pdata->extended_range, value);
328
329         /* External temperature h/w shutdown limit */
330         value = nct1008_read_reg(client, EXT_THERM_LIMIT_WR);
331         if (value < 0)
332                 goto error;
333         temp2 = value_to_temperature(pdata->extended_range, value);
334
335         return snprintf(buf, MAX_STR_PRINT, "%d %d\n", temp, temp2);
336 error:
337         dev_err(dev, "%s: failed to read temperature-overheat "
338                 "\n", __func__);
339         return snprintf(buf, MAX_STR_PRINT, " Rd overheat Error\n");
340 }
341
342 static ssize_t nct1008_set_temp_overheat(struct device *dev,
343                         struct device_attribute *attr,
344                         const char *buf, size_t count)
345 {
346         long int num;
347         int err;
348         u8 temp;
349         long curr_temp;
350         struct i2c_client *client = to_i2c_client(dev);
351         struct nct1008_data *data = i2c_get_clientdata(client);
352         char bufTemp[MAX_STR_PRINT];
353         char bufOverheat[MAX_STR_PRINT];
354         unsigned int ret;
355
356         if (strict_strtol(buf, 0, &num)) {
357                 dev_err(dev, "\n file: %s, line=%d return %s() ", __FILE__,
358                         __LINE__, __func__);
359                 return -EINVAL;
360         }
361         if (((int)num < NCT1008_MIN_TEMP) || ((int)num >= NCT1008_MAX_TEMP)) {
362                 dev_err(dev, "\n file: %s, line=%d return %s() ", __FILE__,
363                         __LINE__, __func__);
364                 return -EINVAL;
365         }
366         /* check for system power down */
367         err = nct1008_get_temp_common(EXT, data, &curr_temp);
368         if (err)
369                 goto error;
370
371         curr_temp = MILLICELSIUS_TO_CELSIUS(curr_temp);
372
373         if (curr_temp >= (int)num) {
374                 ret = nct1008_show_temp(dev, attr, bufTemp);
375                 ret = nct1008_show_temp_overheat(dev, attr, bufOverheat);
376                 dev_err(dev, "\nCurrent temp: %s ", bufTemp);
377                 dev_err(dev, "\nOld overheat limit: %s ", bufOverheat);
378                 dev_err(dev, "\nReset from overheat: curr temp=%ld, new overheat temp=%d\n\n",
379                         curr_temp, (int)num);
380         }
381
382         /* External temperature h/w shutdown limit */
383         temp = temperature_to_value(data->plat_data.extended_range, (s16)num);
384         err = nct1008_write_reg(client, EXT_THERM_LIMIT_WR, temp);
385         if (err < 0)
386                 goto error;
387
388         /* Local temperature h/w shutdown limit */
389         temp = temperature_to_value(data->plat_data.extended_range, (s16)num);
390         err = nct1008_write_reg(client, LOC_THERM_LIMIT, temp);
391         if (err < 0)
392                 goto error;
393
394         data->plat_data.sensors[EXT].shutdown_limit = num;
395         if (!client->dev.of_node)
396                 nct1008_setup_shutdown_warning(data);
397
398         return count;
399 error:
400         dev_err(dev, " %s: failed to set temperature-overheat\n", __func__);
401         return err;
402 }
403
404 static ssize_t nct1008_show_temp_alert(struct device *dev,
405                                 struct device_attribute *attr,
406                                 char *buf)
407 {
408         struct i2c_client *client = to_i2c_client(dev);
409         struct nct1008_platform_data *pdata = client->dev.platform_data;
410         int value;
411         s16 temp_hi, temp_lo;
412         /* External Temperature Throttling hi-limit */
413         value = nct1008_read_reg(client, EXT_TEMP_HI_LIMIT_HI_BYTE_RD);
414         if (value < 0)
415                 goto error;
416         temp_hi = value_to_temperature(pdata->extended_range, value);
417
418         /* External Temperature Throttling lo-limit */
419         value = nct1008_read_reg(client, EXT_TEMP_LO_LIMIT_HI_BYTE_RD);
420         if (value < 0)
421                 goto error;
422         temp_lo = value_to_temperature(pdata->extended_range, value);
423
424         return snprintf(buf, MAX_STR_PRINT, "lo:%d hi:%d\n", temp_lo, temp_hi);
425 error:
426         dev_err(dev, "%s: failed to read temperature-alert\n", __func__);
427         return snprintf(buf, MAX_STR_PRINT, " Rd alert Error\n");
428 }
429
430 static ssize_t nct1008_set_temp_alert(struct device *dev,
431                         struct device_attribute *attr,
432                         const char *buf, size_t count)
433 {
434         long int num;
435         int value;
436         int err;
437         struct i2c_client *client = to_i2c_client(dev);
438         struct nct1008_platform_data *pdata = client->dev.platform_data;
439
440         if (strict_strtol(buf, 0, &num)) {
441                 dev_err(dev, "\n file: %s, line=%d return %s() ", __FILE__,
442                         __LINE__, __func__);
443                 return -EINVAL;
444         }
445         if (((int)num < NCT1008_MIN_TEMP) || ((int)num >= NCT1008_MAX_TEMP)) {
446                 dev_err(dev, "\n file: %s, line=%d return %s() ", __FILE__,
447                         __LINE__, __func__);
448                 return -EINVAL;
449         }
450
451         /* External Temperature Throttling limit */
452         value = temperature_to_value(pdata->extended_range, (s16)num);
453         err = nct1008_write_reg(client, EXT_TEMP_HI_LIMIT_HI_BYTE_WR, value);
454         if (err < 0)
455                 goto error;
456
457         /* Local Temperature Throttling limit */
458         err = nct1008_write_reg(client, LOC_TEMP_HI_LIMIT_WR, value);
459         if (err < 0)
460                 goto error;
461
462         return count;
463 error:
464         dev_err(dev, "%s: failed to set temperature-alert "
465                 "\n", __func__);
466         return err;
467 }
468
469 static ssize_t nct1008_show_sensor_temp(int sensor, struct device *dev,
470         struct device_attribute *attr, char *buf)
471 {
472         struct i2c_client *client = to_i2c_client(dev);
473         struct nct1008_platform_data *pdata = client->dev.platform_data;
474         s16 temp_value;
475         int data = 0;
476         int data_lo;
477
478         if (!dev || !buf || !attr)
479                 return -EINVAL;
480
481         /* When reading the full external temperature value, read the
482          * LSB first. This causes the MSB to be locked (that is, the
483          * ADC does not write to it) until it is read */
484         data_lo = nct1008_read_reg(client, NCT_REG(sensor, TEMP_RD_LO));
485         if (data_lo < 0) {
486                 dev_err(&client->dev, "%s: failed to read "
487                         "ext_temperature, i2c error=%d\n", __func__, data_lo);
488                 goto error;
489         }
490
491         data = nct1008_read_reg(client, NCT_REG(sensor, TEMP_RD_HI));
492         if (data < 0) {
493                 dev_err(&client->dev, "%s: failed to read "
494                         "ext_temperature, i2c error=%d\n", __func__, data);
495                 goto error;
496         }
497
498         temp_value = value_to_temperature(pdata->extended_range, data);
499
500         return snprintf(buf, MAX_STR_PRINT, "%d.%d\n", temp_value,
501                 (25 * (data_lo >> 6)));
502 error:
503         return snprintf(buf, MAX_STR_PRINT, "Error read ext temperature\n");
504 }
505
506 static ssize_t pr_reg(struct nct1008_data *nct, char *buf, int max_s,
507                         const char *reg_name, int offset)
508 {
509         int ret, sz = 0;
510
511         ret = nct1008_read_reg(nct->client, offset);
512         if (ret >= 0)
513                 sz += snprintf(buf + sz, PAGE_SIZE - sz,
514                                 "%20s  0x%02x  0x%02x  0x%02x\n",
515                                 reg_name, nct->client->addr, offset, ret);
516         else
517                 sz += snprintf(buf + sz, PAGE_SIZE - sz,
518                                 "%s: line=%d, i2c ** read error=%d **\n",
519                                 __func__, __LINE__, ret);
520         return sz;
521 }
522
523 static ssize_t nct1008_show_regs(struct device *dev,
524         struct device_attribute *attr, char *buf)
525 {
526         struct i2c_client *client = to_i2c_client(dev);
527         struct nct1008_data *nct = i2c_get_clientdata(client);
528         char *name = nct->chip == NCT72 ? "nct72" : "nct1008";
529         int sz = 0;
530
531         sz += snprintf(buf + sz, PAGE_SIZE - sz,
532                 "%s Registers\n", name);
533         sz += snprintf(buf + sz, PAGE_SIZE - sz,
534                 "---------------------------------------\n");
535         sz += snprintf(buf + sz, PAGE_SIZE - sz, "%20s  %4s  %4s  %s\n",
536                 "Register Name       ", "Addr", "Reg", "Value");
537         sz += snprintf(buf + sz, PAGE_SIZE - sz, "%20s  %4s  %4s  %s\n",
538                 "--------------------", "----", "----", "-----");
539         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
540                 "Status              ", STATUS_RD);
541         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
542                 "Configuration       ", CONFIG_RD);
543         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
544                 "Conversion Rate     ", CONV_RATE_RD);
545         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
546                 "Hysteresis          ", THERM_HYSTERESIS);
547         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
548                 "Consecutive Alert   ", COSECUTIVE_ALERT);
549         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
550                 "Local Temp Value    ", LOC_TEMP_RD);
551         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
552                 "Local Temp Hi Limit ", LOC_TEMP_HI_LIMIT_RD);
553         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
554                 "Local Temp Lo Limit ", LOC_TEMP_LO_LIMIT_RD);
555         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
556                 "Local Therm Limit   ", LOC_THERM_LIMIT);
557         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
558                 "Ext Temp Value Hi   ", EXT_TEMP_HI_RD);
559         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
560                 "Ext Temp Value Lo   ", EXT_TEMP_LO_RD);
561         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
562                 "Ext Temp Hi Limit Hi", EXT_TEMP_HI_LIMIT_HI_BYTE_RD);
563         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
564                 "Ext Temp Hi Limit Lo", EXT_TEMP_HI_LIMIT_LO_BYTE);
565         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
566                 "Ext Temp Lo Limit Hi", EXT_TEMP_LO_LIMIT_HI_BYTE_RD);
567         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
568                 "Ext Temp Lo Limit Lo", EXT_TEMP_LO_LIMIT_LO_BYTE);
569         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
570                 "Ext Temp Offset Hi  ", OFFSET_WR);
571         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
572                 "Ext Temp Offset Lo  ", OFFSET_QUARTER_WR);
573         sz += pr_reg(nct, buf+sz, PAGE_SIZE-sz,
574                 "Ext Therm Limit     ", EXT_THERM_LIMIT_WR);
575
576         return sz;
577 }
578
579 static ssize_t nct1008_set_offsets(struct device *dev,
580                         struct device_attribute *attr,
581                         const char *buf, size_t count)
582 {
583         struct i2c_client *client = to_i2c_client(dev);
584         struct nct1008_data *nct = i2c_get_clientdata(client);
585         int index, temp, off;
586         int rv = count;
587
588         strim((char *)buf);
589         sscanf(buf, "[%u] %u %d", &index, &temp, &off);
590
591         if (index >= ARRAY_SIZE(nct->sensors[EXT].offset_table)) {
592                 pr_info("%s: invalid index [%d]\n", __func__, index);
593                 rv = -EINVAL;
594         } else {
595                 nct->sensors[EXT].offset_table[index].temp = temp;
596                 nct->sensors[EXT].offset_table[index].offset = off;
597         }
598
599         return rv;
600 }
601
602 static ssize_t nct1008_show_offsets(struct device *dev,
603         struct device_attribute *attr, char *buf)
604 {
605         struct i2c_client *client = to_i2c_client(dev);
606         struct nct1008_data *nct = i2c_get_clientdata(client);
607         char *name = nct->chip == NCT72 ? "nct72" : "nct1008";
608         int i, sz = 0;
609
610         sz += snprintf(buf + sz, PAGE_SIZE - sz,
611                                 "%s offsets table\n", name);
612         sz += snprintf(buf + sz, PAGE_SIZE - sz,
613                                 "%2s  %4s  %s\n", " #", "temp", "offset");
614         sz += snprintf(buf + sz, PAGE_SIZE - sz,
615                                 "%2s  %4s  %s\n", "--", "----", "------");
616
617         for (i = 0; i < ARRAY_SIZE(nct->sensors[EXT].offset_table); i++)
618                 sz += snprintf(buf + sz, PAGE_SIZE - sz,
619                                 "%2d  %4d  %3d\n",
620                                 i, nct->sensors[EXT].offset_table[i].temp,
621                                 nct->sensors[EXT].offset_table[i].offset);
622         return sz;
623 }
624
625
626 /* This function is used by the system to show the temperature. */
627 static ssize_t nct1008_show_ext_temp(struct device *dev,
628                                         struct device_attribute *attr,
629                                         char *buf)
630 {
631         return nct1008_show_sensor_temp(EXT, dev, attr, buf);
632 }
633
634 static DEVICE_ATTR(temperature, S_IRUGO, nct1008_show_temp, NULL);
635 static DEVICE_ATTR(temperature_overheat, (S_IRUGO | (S_IWUSR | S_IWGRP)),
636                 nct1008_show_temp_overheat, nct1008_set_temp_overheat);
637 static DEVICE_ATTR(temperature_alert, (S_IRUGO | (S_IWUSR | S_IWGRP)),
638                 nct1008_show_temp_alert, nct1008_set_temp_alert);
639 static DEVICE_ATTR(ext_temperature, S_IRUGO, nct1008_show_ext_temp, NULL);
640 static DEVICE_ATTR(registers, S_IRUGO, nct1008_show_regs, NULL);
641 static DEVICE_ATTR(offsets, (S_IRUGO | (S_IWUSR | S_IWGRP)),
642                 nct1008_show_offsets, nct1008_set_offsets);
643
644 static struct attribute *nct1008_attributes[] = {
645         &dev_attr_temperature.attr,
646         &dev_attr_temperature_overheat.attr,
647         &dev_attr_temperature_alert.attr,
648         &dev_attr_ext_temperature.attr,
649         &dev_attr_registers.attr,
650         &dev_attr_offsets.attr,
651         NULL
652 };
653
654 static const struct attribute_group nct1008_attr_group = {
655         .attrs = nct1008_attributes,
656 };
657
658 static const unsigned long THERM_WARN_RANGE_HIGH_OFFSET = 3000;
659 static unsigned long nct1008_shutdown_warning_cur_state;
660 static long shutdown_warn_saved_temp;
661
662 static int nct1008_shutdown_warning_get_max_state(
663                                         struct thermal_cooling_device *cdev,
664                                         unsigned long *max_state)
665 {
666         /* A state for every 250mC */
667         *max_state = THERM_WARN_RANGE_HIGH_OFFSET / 250;
668         return 0;
669 }
670
671 static int nct1008_shutdown_warning_get_cur_state(
672                                         struct thermal_cooling_device *cdev,
673                                         unsigned long *cur_state)
674 {
675         struct nct1008_data *data = cdev->devdata;
676         long limit = data->plat_data.sensors[EXT].shutdown_limit * 1000;
677         long temp;
678
679         if (nct1008_get_temp_common(EXT, data, &temp))
680                 return -1;
681
682         if (temp >= (limit - THERM_WARN_RANGE_HIGH_OFFSET))
683                 *cur_state = nct1008_shutdown_warning_cur_state;
684         else
685                 *cur_state = 0;
686
687         return 0;
688 }
689
690 static int nct1008_shutdown_warning_set_cur_state(
691                                         struct thermal_cooling_device *cdev,
692                                         unsigned long cur_state)
693 {
694         struct nct1008_data *data = cdev->devdata;
695         long limit = data->plat_data.sensors[EXT].shutdown_limit * 1000;
696         long temp;
697
698         if (nct1008_get_temp_common(EXT, data, &temp))
699                 return -1;
700         else if (temp < 0)
701                 goto ret;
702
703         if ((temp >= (limit - THERM_WARN_RANGE_HIGH_OFFSET)) &&
704                 (temp != shutdown_warn_saved_temp)) {
705                 pr_warn("NCT%s: Warning: chip temperature (%ld.%02ldC) is %s SHUTDOWN limit (%c%ldC).\n",
706                         (data->chip == NCT72) ? "72" : "1008",
707                         temp / 1000, (temp % 1000) / 10,
708                         temp > limit ? "above" :
709                         temp == limit ? "at" : "near",
710                         temp > limit ? '>' : '<', limit / 1000);
711                 shutdown_warn_saved_temp = temp;
712         }
713
714  ret:
715         nct1008_shutdown_warning_cur_state = cur_state;
716         return 0;
717 }
718
719 static struct thermal_cooling_device_ops nct1008_shutdown_warning_ops = {
720         .get_max_state = nct1008_shutdown_warning_get_max_state,
721         .get_cur_state = nct1008_shutdown_warning_get_cur_state,
722         .set_cur_state = nct1008_shutdown_warning_set_cur_state,
723 };
724
725 static int nct1008_thermal_set_limits(int sensor,
726                                       struct nct1008_data *data,
727                                       long lo_limit_milli,
728                                       long hi_limit_milli)
729 {
730         int err;
731         u8 value;
732         bool extended_range = data->plat_data.extended_range;
733         long lo_limit = MILLICELSIUS_TO_CELSIUS(lo_limit_milli);
734         long hi_limit = MILLICELSIUS_TO_CELSIUS(hi_limit_milli);
735
736         if (lo_limit >= hi_limit)
737                 return -EINVAL;
738
739         if (data->sensors[sensor].current_lo_limit != lo_limit) {
740                 value = temperature_to_value(extended_range, lo_limit);
741                 pr_debug("%s: set lo_limit %ld\n", __func__, lo_limit);
742                 err = nct1008_write_reg(data->client,
743                                 NCT_REG(sensor, TEMP_LO_LIMIT_WR), value);
744                 if (err)
745                         return err;
746
747                 data->sensors[sensor].current_lo_limit = lo_limit;
748         }
749
750         if (data->sensors[sensor].current_hi_limit != hi_limit) {
751                 value = temperature_to_value(extended_range, hi_limit);
752                 pr_debug("%s: set hi_limit %ld\n", __func__, hi_limit);
753                 err = nct1008_write_reg(data->client,
754                                 NCT_REG(sensor, TEMP_HI_LIMIT_WR), value);
755                 if (err)
756                         return err;
757
758                 data->sensors[sensor].current_hi_limit = hi_limit;
759         }
760
761         if (sensor == LOC)
762                 pr_debug("NCT1008: LOC-sensor limits set to %ld - %ld\n",
763                         lo_limit, hi_limit);
764
765         return 0;
766 }
767
768 #ifdef CONFIG_THERMAL
769 static void nct1008_update(int sensor, struct nct1008_data *data)
770 {
771         struct thermal_zone_device *thz;
772         long low_temp, high_temp;
773         struct thermal_trip_info *trip_state;
774         long temp, trip_temp, hysteresis_temp;
775         int count;
776         enum thermal_trip_type trip_type;
777         low_temp = 0, high_temp = NCT1008_MAX_TEMP * 1000;
778         thz = data->sensors[sensor].thz;
779
780         if (!thz)
781                 return;
782
783         thermal_zone_device_update(thz);
784
785         temp = thz->temperature;
786
787         for (count = 0; count < thz->trips; count++) {
788                 trip_state = &data->plat_data.sensors[sensor].trips[count];
789                 thz->ops->get_trip_temp(thz, count, &trip_temp);
790                 thz->ops->get_trip_type(thz, count, &trip_type);
791                 thz->ops->get_trip_hyst(thz, count, &hysteresis_temp);
792                 hysteresis_temp = trip_temp - hysteresis_temp;
793                 if ((trip_type == THERMAL_TRIP_PASSIVE) &&
794                     !trip_state->tripped)
795                         hysteresis_temp = trip_temp;
796
797                 if ((trip_temp > temp) && (trip_temp < high_temp))
798                         high_temp = trip_temp;
799
800                 if ((hysteresis_temp < temp) && (hysteresis_temp > low_temp))
801                         low_temp = hysteresis_temp;
802         }
803
804         nct1008_thermal_set_limits(sensor, data, low_temp, high_temp);
805 }
806
807 static int nct1008_ext_get_temp(struct thermal_zone_device *thz, long *temp)
808 {
809         struct nct1008_data *data = thz->devdata;
810
811         return nct1008_get_temp_common(EXT, data, temp);
812 }
813
814 static int nct1008_ext_get_temp_as_sensor(void *data, long *temp)
815 {
816         return nct1008_get_temp_common(EXT, (struct nct1008_data *) data, temp);
817 }
818
819 static int nct1008_ext_bind(struct thermal_zone_device *thz,
820                             struct thermal_cooling_device *cdev)
821 {
822         struct nct1008_data *data = thz->devdata;
823         int i;
824         struct nct1008_sensor_platform_data *sensor;
825
826         sensor = &data->plat_data.sensors[EXT];
827
828         for (i = 0; i < sensor->num_trips; i++) {
829                 if (!strcmp(sensor->trips[i].cdev_type, cdev->type))
830                         thermal_zone_bind_cooling_device(thz, i, cdev,
831                                         sensor->trips[i].upper,
832                                         sensor->trips[i].lower);
833         }
834
835         return 0;
836 }
837
838
839 static int nct1008_unbind(int sensor,
840                                 struct thermal_zone_device *thz,
841                                 struct thermal_cooling_device *cdev)
842 {
843         struct nct1008_data *data = thz->devdata;
844         int i;
845
846         struct nct1008_sensor_platform_data *sensor_data;
847
848         sensor_data = &data->plat_data.sensors[sensor];
849
850         for (i = 0; i < sensor_data->num_trips; i++) {
851                 if (!strcmp(sensor_data->trips[i].cdev_type, cdev->type))
852                         thermal_zone_unbind_cooling_device(thz, i, cdev);
853         }
854         return 0;
855 }
856
857 /* Helper function that is called in order to unbind external sensor from the
858    cooling device. */
859 static inline int nct1008_ext_unbind(struct thermal_zone_device *thz,
860                                         struct thermal_cooling_device *cdev)
861 {
862         return nct1008_unbind(EXT, thz, cdev);
863 }
864
865 /* Helper function that is called in order to unbind local sensor from the
866    cooling device. */
867 static inline int nct1008_loc_unbind(struct thermal_zone_device *thz,
868                                         struct thermal_cooling_device *cdev)
869 {
870         return nct1008_unbind(LOC, thz, cdev);
871 }
872
873 /* This function reads the temperature value set for the given trip point. */
874 static int nct1008_get_trip_temp(int sensor,
875                                         struct thermal_zone_device *thz,
876                                         int trip, long *temp)
877 {
878         struct nct1008_data *data = thz->devdata;
879         struct thermal_trip_info *trip_state =
880                 &data->plat_data.sensors[sensor].trips[trip];
881
882         *temp = trip_state->trip_temp;
883
884         if (trip_state->trip_type != THERMAL_TRIP_PASSIVE)
885                 return 0;
886
887         if (thz->temperature >= *temp) {
888                 trip_state->tripped = true;
889         } else if (trip_state->tripped) {
890                 *temp -= trip_state->hysteresis;
891                 if (thz->temperature < *temp)
892                         trip_state->tripped = false;
893         }
894
895         return 0;
896 }
897
898 /* This function reads the temperature value set for the given trip point for
899    the local sensor. */
900 static inline int nct1008_loc_get_trip_temp(struct thermal_zone_device *thz,
901                                                 int trip, long *temp)
902 {
903         return nct1008_get_trip_temp(LOC, thz, trip, temp);
904 }
905
906 /* This function reads the temperature value set for the given trip point for
907         the remote sensor. */
908 static inline int nct1008_ext_get_trip_temp(struct thermal_zone_device *thz,
909                                                 int trip, long *temp)
910 {
911         return nct1008_get_trip_temp(EXT, thz, trip, temp);
912 }
913
914 /* This function allows setting trip point temperature for the sensor
915    specified. */
916 static int nct1008_set_trip_temp(int sensor,
917                                         struct thermal_zone_device *thz,
918                                         int trip, long temp)
919 {
920         struct nct1008_data *data = thz->devdata;
921
922         data->plat_data.sensors[sensor].trips[trip].trip_temp = temp;
923         nct1008_update(sensor, data);
924         return 0;
925 }
926
927 /* This function allows setting trip point temperature for the local sensor. */
928 static inline int nct1008_loc_set_trip_temp(struct thermal_zone_device *thz,
929                                                 int trip, long temp)
930 {
931         return nct1008_set_trip_temp(LOC, thz, trip, temp);
932 }
933
934 /* This function allows setting trip point temperature for the external
935  * sensor. */
936 static inline int nct1008_ext_set_trip_temp(struct thermal_zone_device *thz,
937                                                 int trip, long temp)
938 {
939         return nct1008_set_trip_temp(EXT, thz, trip, temp);
940 }
941
942 static int nct1008_loc_trip_update(void *of_data, int trip)
943 {
944         struct nct1008_data *data = (struct nct1008_data *)of_data;
945
946         nct1008_update(LOC, data);
947
948         return 0;
949 }
950
951 static int nct1008_ext_trip_update(void *of_data, int trip)
952 {
953         struct nct1008_data *data = (struct nct1008_data *)of_data;
954
955         nct1008_update(EXT, data);
956
957         return 0;
958 }
959
960 /* This function return the trip point type for the sensor specified. */
961 static int nct1008_get_trip_type(int sensor,
962                                         struct thermal_zone_device *thz,
963                                         int trip,
964                                         enum thermal_trip_type *type)
965 {
966         struct nct1008_data *data = thz->devdata;
967
968         *type = data->plat_data.sensors[sensor].trips[trip].trip_type;
969         return 0;
970 }
971
972 /* This function return the trip point type for the local sensor. */
973 static inline int nct1008_loc_get_trip_type(struct thermal_zone_device *thz,
974                                                 int trip,
975                                                 enum thermal_trip_type *type)
976 {
977         return nct1008_get_trip_type(LOC, thz, trip, type);
978 }
979
980 /* This function return the trip point type for the external sensor. */
981 static inline int nct1008_ext_get_trip_type(struct thermal_zone_device *thz,
982                                                 int trip,
983                                                 enum thermal_trip_type *type)
984 {
985         return nct1008_get_trip_type(EXT, thz, trip, type);
986 }
987
988 static int nct1008_get_trip_hyst(int sensor, struct thermal_zone_device *thz,
989                                                 int trip,
990                                                 long *hyst)
991 {
992         struct nct1008_data *data = thz->devdata;
993
994         *hyst = data->plat_data.sensors[sensor].trips[trip].hysteresis;
995         return 0;
996 }
997
998 static inline int  nct1008_loc_get_trip_hyst(struct thermal_zone_device *thz,
999                                                 int trip,
1000                                                 long *hyst)
1001 {
1002         return nct1008_get_trip_hyst(LOC, thz, trip, hyst);
1003 }
1004
1005 static inline int  nct1008_ext_get_trip_hyst(struct thermal_zone_device *thz,
1006                                                 int trip,
1007                                                 long *hyst)
1008 {
1009         return nct1008_get_trip_hyst(EXT, thz, trip, hyst);
1010 }
1011
1012 /* This function returns value of trend for the temperature change, depending
1013    on the trip point type. */
1014 static int nct1008_get_trend(struct thermal_zone_device *thz,
1015                                 int trip,
1016                                 enum thermal_trend *trend)
1017 {
1018         long trip_temp, trip_hyst;
1019         enum thermal_trip_type trip_type;
1020
1021         thz->ops->get_trip_temp(thz, trip, &trip_temp);
1022         thz->ops->get_trip_type(thz, trip, &trip_type);
1023         thz->ops->get_trip_hyst(thz, trip, &trip_hyst);
1024
1025         switch (trip_type) {
1026         case THERMAL_TRIP_ACTIVE:
1027                 if (thz->temperature >= trip_temp)
1028                         *trend = THERMAL_TREND_RAISING;
1029                 else
1030                         *trend = THERMAL_TREND_DROPPING;
1031                 break;
1032         case THERMAL_TRIP_PASSIVE:
1033                 if (thz->temperature > thz->last_temperature)
1034                         *trend = THERMAL_TREND_RAISING;
1035                 else if (thz->temperature < (thz->last_temperature - trip_hyst))
1036                         *trend = THERMAL_TREND_DROPPING;
1037                 else
1038                         *trend = THERMAL_TREND_STABLE;
1039                 break;
1040         default:
1041                 return -EINVAL;
1042         }
1043
1044         return 0;
1045 }
1046
1047 static int nct1008_get_trend_as_sensor(int sensor,
1048                                 struct nct1008_data *data,
1049                                         long *trend)
1050 {
1051         struct thermal_zone_device *thz = data->sensors[sensor].thz;
1052
1053         return thz->temperature - thz->last_temperature;
1054 }
1055
1056 /* Helper function to get trend for the local sensor. */
1057 static inline int nct1008_loc_get_trend_as_sensor(void *data,
1058                                                 long *trend)
1059 {
1060         return nct1008_get_trend_as_sensor(LOC,
1061                 (struct nct1008_data *) data, trend);
1062 }
1063
1064 static inline int nct1008_ext_get_trend_as_sensor(void *data,
1065                                                 long *trend)
1066 {
1067         return nct1008_get_trend_as_sensor
1068                 (EXT, (struct nct1008_data *) data, trend);
1069 }
1070
1071
1072 /* Helper function to get temperature of the local sensor. */
1073 static int nct1008_loc_get_temp(struct thermal_zone_device *thz, long *temp)
1074 {
1075         struct nct1008_data *data = thz->devdata;
1076
1077         return nct1008_get_temp_common(LOC, data, temp);
1078 }
1079
1080 static int nct1008_loc_get_temp_as_sensor(void *data, long *temp)
1081 {
1082         return nct1008_get_temp_common(LOC, (struct nct1008_data *) data, temp);
1083 }
1084 /* Helper function to bind local sensor with the cooling device specified. */
1085 static int nct1008_loc_bind(struct thermal_zone_device *thz,
1086                                 struct thermal_cooling_device *cdev)
1087 {
1088         struct nct1008_data *data = thz->devdata;
1089         int i;
1090         struct nct1008_sensor_platform_data *sensor_data;
1091
1092         pr_debug("NCT1008: LOC-sensor bind %s, %s attempt\n",
1093                  thz->type, cdev->type);
1094
1095         sensor_data = &data->plat_data.sensors[LOC];
1096
1097         for (i = 0; i < sensor_data->num_trips; i++) {
1098                 if (!strcmp(sensor_data->trips[i].cdev_type, cdev->type)) {
1099                         thermal_zone_bind_cooling_device(thz, i, cdev,
1100                                 sensor_data->trips[i].upper,
1101                                 sensor_data->trips[i].lower);
1102                         break;
1103                 }
1104         }
1105
1106         return 0;
1107 }
1108
1109 static struct thermal_zone_device_ops nct_loc_ops = {
1110         .get_temp = nct1008_loc_get_temp,
1111         .bind = nct1008_loc_bind,
1112         .unbind = nct1008_loc_unbind,
1113         .get_trip_type = nct1008_loc_get_trip_type,
1114         .get_trip_temp = nct1008_loc_get_trip_temp,
1115         .get_trip_hyst = nct1008_loc_get_trip_hyst,
1116         .set_trip_temp = nct1008_loc_set_trip_temp,
1117         .get_trend = nct1008_get_trend,
1118 };
1119
1120 static struct thermal_zone_device_ops nct_ext_ops = {
1121         .get_temp = nct1008_ext_get_temp,
1122         .bind = nct1008_ext_bind,
1123         .unbind = nct1008_ext_unbind,
1124         .get_trip_type = nct1008_ext_get_trip_type,
1125         .get_trip_temp = nct1008_ext_get_trip_temp,
1126         .get_trip_hyst = nct1008_ext_get_trip_hyst,
1127         .set_trip_temp = nct1008_ext_set_trip_temp,
1128         .get_trend = nct1008_get_trend,
1129 };
1130 #else
1131 static void nct1008_update(nct1008_sensors sensor, struct nct1008_data *data)
1132 {
1133 }
1134 #endif /* CONFIG_THERMAL */
1135
1136 static int nct1008_enable(struct i2c_client *client)
1137 {
1138         struct nct1008_data *data = i2c_get_clientdata(client);
1139         int err;
1140
1141         err = nct1008_write_reg(client, CONFIG_WR, data->config);
1142         if (err < 0)
1143                 dev_err(&client->dev, "%s, line=%d, i2c write error=%d\n",
1144                 __func__, __LINE__, err);
1145         return err;
1146 }
1147
1148 static int nct1008_disable(struct i2c_client *client)
1149 {
1150         struct nct1008_data *data = i2c_get_clientdata(client);
1151         int err;
1152
1153         err = nct1008_write_reg(client, CONFIG_WR,
1154                                 data->config | STANDBY_BIT);
1155         if (err < 0)
1156                 dev_err(&client->dev, "%s, line=%d, i2c write error=%d\n",
1157                 __func__, __LINE__, err);
1158         return err;
1159 }
1160
1161 static void nct1008_work_func(struct work_struct *work)
1162 {
1163         struct nct1008_data *data = container_of(work, struct nct1008_data,
1164                                                 work);
1165         int err;
1166         struct timespec ts;
1167         int intr_status;
1168
1169         mutex_lock(&data->mutex);
1170         if (data->stop_workqueue) {
1171                 mutex_unlock(&data->mutex);
1172                 return;
1173         }
1174         mutex_unlock(&data->mutex);
1175
1176         err = nct1008_disable(data->client);
1177         if (err == -ENODEV)
1178                 return;
1179
1180         intr_status = nct1008_read_reg(data->client, STATUS_RD);
1181         pr_debug("NCT1008: interruption (0x%08x)\n", intr_status);
1182
1183         if (intr_status & (LOC_LO_BIT | LOC_HI_BIT)) {
1184                 pr_debug("NCT1008: LOC-sensor is not within limits\n");
1185                 nct1008_update(LOC, data);
1186         }
1187
1188         if (intr_status & (EXT_LO_BIT | EXT_HI_BIT))
1189                 nct1008_update(EXT, data);
1190
1191         /* Initiate one-shot conversion */
1192         nct1008_write_reg(data->client, ONE_SHOT, 0x1);
1193
1194         /* Give hardware necessary time to finish conversion */
1195         ts = ns_to_timespec(MAX_CONV_TIME_ONESHOT_MS * 1000 * 1000);
1196         hrtimer_nanosleep(&ts, NULL, HRTIMER_MODE_REL, CLOCK_MONOTONIC);
1197
1198         nct1008_read_reg(data->client, STATUS_RD);
1199
1200         nct1008_enable(data->client);
1201
1202         enable_irq(data->client->irq);
1203 }
1204
1205 static irqreturn_t nct1008_irq(int irq, void *dev_id)
1206 {
1207         struct nct1008_data *data = dev_id;
1208
1209         disable_irq_nosync(irq);
1210         queue_work(data->workqueue, &data->work);
1211
1212         return IRQ_HANDLED;
1213 }
1214
1215 static void nct1008_power_control(struct nct1008_data *data, bool is_enable)
1216 {
1217         int ret;
1218
1219         mutex_lock(&data->mutex);
1220         if (!data->nct_reg) {
1221                 data->nct_reg = regulator_get(&data->client->dev, "vdd");
1222                 if (IS_ERR_OR_NULL(data->nct_reg)) {
1223                         if (PTR_ERR(data->nct_reg) == -ENODEV)
1224                                 dev_info(&data->client->dev,
1225                                         "no regulator found for vdd."
1226                                         " Assuming vdd is always powered");
1227                         else
1228                                 dev_warn(&data->client->dev, "Error [%ld] in "
1229                                         "getting the regulator handle for"
1230                                         " vdd\n", PTR_ERR(data->nct_reg));
1231                         data->nct_reg = NULL;
1232                         mutex_unlock(&data->mutex);
1233                         return;
1234                 }
1235         }
1236         if (is_enable)
1237                 ret = regulator_enable(data->nct_reg);
1238         else
1239                 ret = regulator_disable(data->nct_reg);
1240
1241         if (ret < 0)
1242                 dev_err(&data->client->dev, "Error in %s rail vdd_nct%s, "
1243                         "error %d\n", (is_enable) ? "enabling" : "disabling",
1244                         (data->chip == NCT72) ? "72" : "1008",
1245                         ret);
1246         else
1247                 dev_info(&data->client->dev, "success in %s rail vdd_nct%s\n",
1248                         (is_enable) ? "enabling" : "disabling",
1249                         (data->chip == NCT72) ? "72" : "1008");
1250         data->nct_disabled = !is_enable;
1251         mutex_unlock(&data->mutex);
1252 }
1253
1254 static void nct1008_setup_shutdown_warning(struct nct1008_data *data)
1255 {
1256         static struct thermal_cooling_device *cdev;
1257         long limit = data->plat_data.sensors[EXT].shutdown_limit * 1000;
1258         long warn_temp = limit - THERM_WARN_RANGE_HIGH_OFFSET;
1259         int i;
1260         struct nct1008_sensor_platform_data *sensor_data;
1261
1262         if (cdev)
1263                 thermal_cooling_device_unregister(cdev);
1264         cdev = thermal_cooling_device_register("shutdown_warning", data,
1265                                         &nct1008_shutdown_warning_ops);
1266         if (IS_ERR_OR_NULL(cdev)) {
1267                 cdev = NULL;
1268                 return;
1269         }
1270
1271         sensor_data = &data->plat_data.sensors[EXT];
1272
1273         for (i = 0; i < sensor_data->num_trips; i++) {
1274                 if (!strcmp(sensor_data->trips[i].cdev_type,
1275                                                 "shutdown_warning")) {
1276                         sensor_data->trips[i].trip_temp = warn_temp;
1277                         nct1008_update(EXT, data);
1278                         break;
1279                 }
1280         }
1281
1282         pr_info("NCT%s: Enabled overheat logging at %ld.%02ldC\n",
1283                         (data->chip == NCT72) ? "72" : "1008",
1284                         warn_temp / 1000, (warn_temp % 1000) / 10);
1285 }
1286
1287 static int nct1008_configure_sensor(struct nct1008_data *data)
1288 {
1289         struct i2c_client *client = data->client;
1290         struct nct1008_platform_data *pdata = client->dev.platform_data;
1291         u8 value;
1292         s16 temp;
1293         u8 temp2;
1294         int ret;
1295         bool ext_err = false;
1296
1297         if (!pdata || !pdata->supported_hwrev)
1298                 return -ENODEV;
1299
1300         ret = nct1008_read_reg(data->client, STATUS_RD);
1301         if (ret & BIT(2)) {
1302                 pr_info("NCT%s: ERR: remote sensor circuit is open (0x%02x)\n",
1303                         (data->chip == NCT72) ? "72" : "1008", ret);
1304                 ext_err = true; /* flag the error */
1305         }
1306
1307         /* Initially place in Standby */
1308         ret = nct1008_write_reg(client, CONFIG_WR, STANDBY_BIT);
1309         if (ret)
1310                 goto error;
1311
1312         /* Local temperature h/w shutdown limit */
1313         value = temperature_to_value(pdata->extended_range,
1314                                         pdata->sensors[LOC].shutdown_limit);
1315         ret = nct1008_write_reg(client, LOC_THERM_LIMIT, value);
1316         if (ret)
1317                 goto error;
1318
1319         /* set extended range mode if needed */
1320         if (pdata->extended_range)
1321                 data->config |= EXTENDED_RANGE_BIT;
1322         data->config &= ~(THERM2_BIT | ALERT_BIT);
1323
1324         ret = nct1008_write_reg(client, CONFIG_WR, data->config | STANDBY_BIT);
1325         if (ret)
1326                 goto error;
1327
1328         /* Temperature conversion rate */
1329         ret = nct1008_write_reg(client, CONV_RATE_WR, pdata->conv_rate);
1330         if (ret)
1331                 goto error;
1332
1333         data->conv_period_ms = conv_period_ms_table[pdata->conv_rate];
1334
1335         /* Setup local hi and lo limits. */
1336         ret = nct1008_write_reg(client, LOC_TEMP_HI_LIMIT_WR, NCT1008_MAX_TEMP);
1337         if (ret)
1338                 goto error;
1339
1340         ret = nct1008_write_reg(client, LOC_TEMP_LO_LIMIT_WR, 0);
1341         if (ret)
1342                 goto error;
1343
1344         /* read initial temperature */
1345         ret = nct1008_read_reg(client, LOC_TEMP_RD);
1346         if (ret < 0)
1347                 goto error;
1348         else
1349                 value = ret;
1350
1351         temp = value_to_temperature(pdata->extended_range, value);
1352         dev_dbg(&client->dev, "\n initial local temp = %d ", temp);
1353
1354         if (ext_err)
1355                 return ext_err; /* skip configuration of EXT sensor */
1356
1357         /* External temperature h/w shutdown limit. */
1358         value = temperature_to_value(pdata->extended_range,
1359                                         pdata->sensors[EXT].shutdown_limit);
1360         ret = nct1008_write_reg(client, EXT_THERM_LIMIT_WR, value);
1361         if (ret)
1362                 goto error;
1363
1364         /* Setup external hi and lo limits */
1365         ret = nct1008_write_reg(client, EXT_TEMP_LO_LIMIT_HI_BYTE_WR, 0);
1366         if (ret)
1367                 goto error;
1368         ret = nct1008_write_reg(client, EXT_TEMP_HI_LIMIT_HI_BYTE_WR,
1369                         NCT1008_MAX_TEMP);
1370         if (ret)
1371                 goto error;
1372
1373         ret = nct1008_read_reg(client, EXT_TEMP_LO_RD);
1374         if (ret < 0)
1375                 goto error;
1376         else
1377                 value = ret;
1378
1379         temp2 = (value >> 6);
1380         ret = nct1008_read_reg(client, EXT_TEMP_HI_RD);
1381         if (ret < 0)
1382                 goto error;
1383         else
1384                 value = ret;
1385
1386         temp = value_to_temperature(pdata->extended_range, value);
1387
1388         if (temp2 > 0)
1389                 dev_dbg(&client->dev, "\n initial ext temp = %d.%d deg",
1390                                 temp, temp2 * 25);
1391         else
1392                 dev_dbg(&client->dev, "\n initial ext temp = %d.0 deg", temp);
1393
1394         /* Remote channel offset */
1395         ret = nct1008_write_reg(client, OFFSET_WR, pdata->offset / 4);
1396         if (ret < 0)
1397                 goto error;
1398
1399         /* Remote channel offset fraction (quarters) */
1400         ret = nct1008_write_reg(client, OFFSET_QUARTER_WR,
1401                                         (pdata->offset % 4) << 6);
1402         if (ret < 0)
1403                 goto error;
1404
1405         /* Reset current hi/lo limit values with register values */
1406         ret = nct1008_read_reg(data->client, EXT_TEMP_LO_LIMIT_HI_BYTE_RD);
1407         if (ret < 0)
1408                 goto error;
1409         else
1410                 value = ret;
1411         data->sensors[EXT].current_lo_limit =
1412                 value_to_temperature(pdata->extended_range, value);
1413
1414         ret = nct1008_read_reg(data->client, EXT_TEMP_HI_LIMIT_HI_BYTE_RD);
1415         if (ret < 0)
1416                 goto error;
1417         else
1418                 value = ret;
1419
1420         data->sensors[EXT].current_hi_limit =
1421                 value_to_temperature(pdata->extended_range, value);
1422
1423         ret = nct1008_read_reg(data->client, LOC_TEMP_LO_LIMIT_RD);
1424         if (ret < 0)
1425                 goto error;
1426         else
1427                 value = ret;
1428
1429         data->sensors[LOC].current_lo_limit =
1430                 value_to_temperature(pdata->extended_range, value);
1431
1432         value = nct1008_read_reg(data->client, LOC_TEMP_HI_LIMIT_RD);
1433         if (ret < 0)
1434                 goto error;
1435         else
1436                 value = ret;
1437
1438         data->sensors[LOC].current_hi_limit =
1439                 value_to_temperature(pdata->extended_range, value);
1440
1441         if (!client->dev.of_node)
1442                 nct1008_setup_shutdown_warning(data);
1443
1444         return 0;
1445 error:
1446         dev_err(&client->dev, "\n exit %s, err=%d ", __func__, ret);
1447         return ret;
1448 }
1449
1450 static int nct1008_configure_irq(struct nct1008_data *data)
1451 {
1452         data->workqueue = create_singlethread_workqueue((data->chip == NCT72) \
1453                                                         ? "nct72" : "nct1008");
1454
1455         INIT_WORK(&data->work, nct1008_work_func);
1456
1457         if (data->client->irq < 0)
1458                 return 0;
1459         else
1460                 return request_irq(data->client->irq, nct1008_irq,
1461                         IRQF_TRIGGER_LOW,
1462                         (data->chip == NCT72) ? "nct72" : "nct1008",
1463                         data);
1464 }
1465
1466 static struct nct1008_platform_data *nct1008_dt_parse(struct i2c_client *client)
1467 {
1468         struct device_node *np = client->dev.of_node;
1469         struct device_node *child_sensor;
1470         struct nct1008_platform_data *pdata;
1471         int nct72_gpio;
1472         unsigned int proc, index = 0;
1473         if (!np) {
1474                 dev_err(&client->dev,
1475                         "Cannot found the DT node\n");
1476                 goto err_parse_dt;
1477         }
1478
1479         dev_info(&client->dev, "starting parse dt\n");
1480         pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1481         if (!pdata) {
1482                 dev_err(&client->dev,
1483                         "Parse DT fails at malloc pdata\n");
1484                 goto err_parse_dt;
1485         }
1486
1487         pdata->loc_name = of_get_property(np, "sensor-name", NULL);
1488         if (pdata->loc_name == NULL) {
1489                 dev_err(&client->dev,
1490                         "Cannot found the name\n");
1491                 goto err_parse_dt;
1492         }
1493
1494         if (client->irq == 0)
1495                 client->irq = -1;
1496
1497         if (of_property_read_u32(np, "conv-rate", &proc))
1498                 goto err_parse_dt;
1499         pdata->conv_rate = proc;
1500
1501         if (of_property_read_u32(np, "supported-hwrev", &proc))
1502                 goto err_parse_dt;
1503         pdata->supported_hwrev = (bool) proc;
1504
1505         if (of_property_read_u32(np, "extended-rage", &proc))
1506                 goto err_parse_dt;
1507         pdata->extended_range = (bool) proc;
1508
1509         if (of_property_read_u32(np, "offset", &proc))
1510                 goto err_parse_dt;
1511         pdata->offset =  proc;
1512
1513         if (of_property_read_bool(np, "temp-alert-gpio")) {
1514                 nct72_gpio = of_get_named_gpio(
1515                         np,  "temp-alert-gpio", 0);
1516                 if (gpio_request(nct72_gpio, "temp_alert") < 0)
1517                         dev_err(&client->dev,
1518                                 "%s gpio request error\n", __FILE__);
1519                 if (gpio_direction_input(nct72_gpio) < 0) {
1520                         dev_err(&client->dev,
1521                                 "%s gpio direction_input fail\n", __FILE__);
1522                         gpio_free(nct72_gpio);
1523                 }
1524         }
1525
1526         for_each_child_of_node(np, child_sensor) {
1527                 if (of_property_read_u32(child_sensor, "shutdown-limit", &proc))
1528                         goto err_parse_dt;
1529                 pdata->sensors[index].shutdown_limit = proc;
1530
1531                 proc = 0;
1532                 of_property_read_u32(child_sensor, "suspend_limit_hi", &proc);
1533                 pdata->sensors[index].suspend_limit_hi = proc;
1534                 of_property_read_u32(child_sensor, "suspend_limit_lo", &proc);
1535                 pdata->sensors[index].suspend_limit_lo = proc;
1536                 index++;
1537         }
1538
1539         dev_info(&client->dev, "success parsing dt\n");
1540         client->dev.platform_data = pdata;
1541         return pdata;
1542
1543 err_parse_dt:
1544         dev_err(&client->dev, "Parsing device tree data error.\n");
1545         return NULL;
1546 }
1547
1548 /*
1549  * Manufacturer(OnSemi) recommended sequence for
1550  * Extended Range mode is as follows
1551  * 1. Place in Standby
1552  * 2. Scale the THERM and ALERT limits
1553  *      appropriately(for Extended Range mode).
1554  * 3. Enable Extended Range mode.
1555  *      ALERT mask/THERM2 mode may be done here
1556  *      as these are not critical
1557  * 4. Set Conversion Rate as required
1558  * 5. Take device out of Standby
1559  */
1560
1561 /*
1562  * function nct1008_probe takes care of initial configuration
1563  */
1564 static int nct1008_probe(struct i2c_client *client,
1565                                 const struct i2c_device_id *id)
1566 {
1567         struct nct1008_data *data;
1568         struct nct1008_platform_data *pdata;
1569         struct thermal_zone_device *zone_device;
1570         int err;
1571         int i;
1572         u64 mask = 0;
1573         char nct_loc_name[THERMAL_NAME_LENGTH];
1574         char nct_ext_name[THERMAL_NAME_LENGTH];
1575         bool ext_err;
1576         struct nct1008_sensor_platform_data *sensor_data;
1577         struct thermal_of_sensor_ops loc_sops = {
1578                 .get_temp = nct1008_loc_get_temp_as_sensor,
1579                 .get_trend = nct1008_loc_get_trend_as_sensor,
1580                 .trip_update = nct1008_loc_trip_update,
1581         };
1582
1583         struct thermal_of_sensor_ops ext_sops = {
1584                 .get_temp = nct1008_ext_get_temp_as_sensor,
1585                 .get_trend = nct1008_ext_get_trend_as_sensor,
1586                 .trip_update = nct1008_ext_trip_update,
1587         };
1588
1589         if (client->dev.of_node) {
1590                 dev_info(&client->dev, "find device tree node, parsing dt\n");
1591                 pdata = nct1008_dt_parse(client);
1592                 if (IS_ERR(pdata)) {
1593                         err = PTR_ERR(pdata);
1594                         dev_err(&client->dev,
1595                                 "Parsing of node failed, %d\n", err);
1596                         return err;
1597                 }
1598         }
1599
1600         data = kzalloc(sizeof(struct nct1008_data), GFP_KERNEL);
1601         if (!data)
1602                 return -ENOMEM;
1603
1604         data->client = client;
1605         data->chip = id->driver_data;
1606         memcpy(&data->plat_data, client->dev.platform_data,
1607                 sizeof(struct nct1008_platform_data));
1608
1609         i2c_set_clientdata(client, data);
1610         mutex_init(&data->mutex);
1611
1612         nct1008_power_control(data, true);
1613         if (!data->nct_reg) {
1614                 /* power up failure */
1615                 err = -EIO;
1616                 goto cleanup;
1617         }
1618         /* extended range recommended steps 1 through 4 taken care
1619          * in nct1008_configure_sensor function */
1620         err = nct1008_configure_sensor(data);   /* sensor is in standby */
1621         ext_err = err;
1622         if (err < 0) {
1623                 dev_err(&client->dev, "\n error file: %s : %s(), line=%d ",
1624                         __FILE__, __func__, __LINE__);
1625                 goto error;
1626         }
1627
1628         err = nct1008_configure_irq(data);
1629         if (err < 0) {
1630                 dev_err(&client->dev, "\n error file: %s : %s(), line=%d ",
1631                         __FILE__, __func__, __LINE__);
1632                 goto error;
1633         }
1634         dev_info(&client->dev, "%s: initialized\n", __func__);
1635
1636         /* extended range recommended step 5 is in nct1008_enable function */
1637         err = nct1008_enable(client);           /* sensor is running */
1638         if (err < 0) {
1639                 dev_err(&client->dev, "Error: %s, line=%d, error=%d\n",
1640                         __func__, __LINE__, err);
1641                 goto error;
1642         }
1643
1644         /* register sysfs hooks */
1645         err = sysfs_create_group(&client->dev.kobj, &nct1008_attr_group);
1646         if (err < 0) {
1647                 dev_err(&client->dev, "\n sysfs create err=%d ", err);
1648                 goto error;
1649         }
1650
1651 #ifdef CONFIG_THERMAL
1652         if (data->plat_data.loc_name) {
1653                 strcpy(nct_loc_name, "Tboard_");
1654                 strcpy(nct_ext_name, "Tdiode_");
1655                 strncat(nct_loc_name, data->plat_data.loc_name,
1656                         (THERMAL_NAME_LENGTH - strlen("Tboard_")) - 1);
1657                 strncat(nct_ext_name, data->plat_data.loc_name,
1658                         (THERMAL_NAME_LENGTH - strlen("Tdiode_")) - 1);
1659         } else {
1660                 strcpy(nct_loc_name, "Tboard");
1661                 strcpy(nct_ext_name, "Tdiode");
1662         }
1663
1664         if (client->dev.of_node) {
1665                 /* Config for the Local sensor. */
1666                 zone_device =
1667                         thermal_zone_of_sensor_register2(&client->dev, LOC,
1668                                 data, &loc_sops);
1669
1670                 if (!IS_ERR_OR_NULL(zone_device))
1671                         data->sensors[LOC].thz = zone_device;
1672
1673                 /* register External sensor if connection is good  */
1674                 zone_device =
1675                         thermal_zone_of_sensor_register2(&client->dev, EXT,
1676                                         data, &ext_sops);
1677                 if (!IS_ERR_OR_NULL(zone_device))
1678                         data->sensors[EXT].thz = zone_device;
1679         } else {
1680                 sensor_data = &data->plat_data.sensors[LOC];
1681
1682                 /* Config for the Local sensor. */
1683                 mask = 0;
1684                 for (i = 0; i < sensor_data->num_trips; i++)
1685                         if (data->plat_data.sensors[LOC].trips[i].mask)
1686                                 mask |= 1ULL << i;
1687
1688                 data->sensors[LOC].thz =
1689                         thermal_zone_device_register(nct_loc_name,
1690                                                 sensor_data->num_trips,
1691                                                 mask,
1692                                                 data,
1693                                                 &nct_loc_ops,
1694                                                 sensor_data->tzp,
1695                                                 2000,
1696                                                 0);
1697
1698                 /* Config for the External sensor. */
1699                 mask = 0;
1700                 for (i = 0; i < data->plat_data.sensors[EXT].num_trips; i++)
1701                         if (data->plat_data.sensors[EXT].trips[i].mask > 0)
1702                                 mask |= 1ULL << i;
1703
1704                 /* register External sensor if connection is good  */
1705                 data->sensors[EXT].thz = ext_err ? NULL :
1706                         thermal_zone_device_register(nct_ext_name,
1707                                 data->plat_data.sensors[EXT].num_trips,
1708                                 mask,
1709                                 data,
1710                                 &nct_ext_ops,
1711                                 data->plat_data.sensors[EXT].tzp,
1712                                 data->plat_data.sensors[EXT].passive_delay,
1713                                 data->plat_data.sensors[EXT].polling_delay);
1714         }
1715
1716         if (!IS_ERR_OR_NULL(data->sensors[LOC].thz)) {
1717                 nct1008_update(LOC, data);
1718         }
1719
1720         if (!IS_ERR_OR_NULL(data->sensors[EXT].thz)) {
1721                 nct1008_update(EXT, data);
1722                 shutdown_warn_saved_temp = data->sensors[EXT].thz->temperature;
1723         }
1724
1725 #endif
1726         return 0;
1727
1728 error:
1729         dev_err(&client->dev, "\n exit %s, err=%d ", __func__, err);
1730         nct1008_power_control(data, false);
1731 cleanup:
1732         mutex_destroy(&data->mutex);
1733         if (data->nct_reg)
1734                 regulator_put(data->nct_reg);
1735         kfree(data);
1736         return err;
1737 }
1738
1739 static int nct1008_remove(struct i2c_client *client)
1740 {
1741         struct nct1008_data *data = i2c_get_clientdata(client);
1742
1743         mutex_lock(&data->mutex);
1744         data->stop_workqueue = 1;
1745         mutex_unlock(&data->mutex);
1746
1747         cancel_work_sync(&data->work);
1748         free_irq(data->client->irq, data);
1749         sysfs_remove_group(&client->dev.kobj, &nct1008_attr_group);
1750         nct1008_power_control(data, false);
1751
1752         if (data->nct_reg)
1753                 regulator_put(data->nct_reg);
1754
1755         mutex_destroy(&data->mutex);
1756         kfree(data);
1757
1758         return 0;
1759 }
1760
1761 static void nct1008_shutdown(struct i2c_client *client)
1762 {
1763         struct nct1008_data *data = i2c_get_clientdata(client);
1764
1765         mutex_lock(&data->mutex);
1766         data->stop_workqueue = 1;
1767         mutex_unlock(&data->mutex);
1768
1769         if (data->sensors[LOC].thz) {
1770                 if (client->dev.of_node)
1771                         thermal_zone_of_sensor_unregister
1772                                 (&(client->dev), data->sensors[LOC].thz);
1773                 else
1774                         thermal_zone_device_unregister(data->sensors[LOC].thz);
1775                 data->sensors[LOC].thz = NULL;
1776         }
1777         if (data->sensors[EXT].thz) {
1778                 if (client->dev.of_node)
1779                         thermal_zone_of_sensor_unregister
1780                                 (&(client->dev), data->sensors[EXT].thz);
1781                 else
1782                         thermal_zone_device_unregister(data->sensors[EXT].thz);
1783                 data->sensors[EXT].thz = NULL;
1784         }
1785
1786         cancel_work_sync(&data->work);
1787
1788         if (client->irq)
1789                 disable_irq(client->irq);
1790
1791         mutex_lock(&data->mutex);
1792         data->nct_disabled = 1;
1793         mutex_unlock(&data->mutex);
1794 }
1795
1796 #ifdef CONFIG_PM_SLEEP
1797 static int nct1008_suspend_powerdown(struct device *dev)
1798 {
1799         struct i2c_client *client = to_i2c_client(dev);
1800         int err;
1801         struct nct1008_data *data = i2c_get_clientdata(client);
1802
1803         mutex_lock(&data->mutex);
1804         data->stop_workqueue = 1;
1805         mutex_unlock(&data->mutex);
1806         cancel_work_sync(&data->work);
1807         disable_irq(client->irq);
1808         err = nct1008_disable(client);
1809         nct1008_power_control(data, false);
1810         return err;
1811 }
1812
1813 static int nct1008_suspend_wakeup(struct device *dev)
1814 {
1815         struct i2c_client *client = to_i2c_client(dev);
1816         int err;
1817         struct nct1008_data *data = i2c_get_clientdata(client);
1818         long temp;
1819         int sensor_nr;
1820         struct nct1008_sensor_platform_data *sensor_data;
1821
1822         for (sensor_nr = 0; sensor_nr < SENSORS_COUNT; sensor_nr++) {
1823                 sensor_data = &data->plat_data.sensors[sensor_nr];
1824
1825                 err = nct1008_get_temp_common(sensor_nr, data, &temp);
1826
1827                 if (err)
1828                         goto error;
1829
1830                 if (temp > sensor_data->suspend_limit_lo)
1831                         err = nct1008_thermal_set_limits(sensor_nr, data,
1832                                 sensor_data->suspend_limit_lo,
1833                                 NCT1008_MAX_TEMP * 1000);
1834                 else
1835                         err = nct1008_thermal_set_limits(sensor_nr, data,
1836                                 NCT1008_MIN_TEMP * 1000,
1837                                 sensor_data->suspend_limit_hi);
1838
1839                 if (err)
1840                         goto error;
1841         }
1842
1843         /* Enable NCT wake. */
1844         err = enable_irq_wake(client->irq);
1845         if (err)
1846                 dev_err(&client->dev, "Error: %s, error=%d. failed to enable NCT wakeup\n",
1847                         __func__, err);
1848         return err;
1849
1850 error:
1851         dev_err(&client->dev, "\n error in file=: %s %s() line=%d: "
1852                 "error=%d. Can't set correct LP1 alarm limits or set wakeup irq, "
1853                 "shutting down device", __FILE__, __func__, __LINE__, err);
1854
1855         return nct1008_suspend_powerdown(dev);
1856 }
1857
1858 static int nct1008_suspend(struct device *dev)
1859 {
1860         struct i2c_client *client = to_i2c_client(dev);
1861         struct nct1008_data *data = i2c_get_clientdata(client);
1862
1863         if (data->plat_data.suspend_with_wakeup &&
1864                 data->plat_data.suspend_with_wakeup())
1865                 return nct1008_suspend_wakeup(dev);
1866         else
1867                 return nct1008_suspend_powerdown(dev);
1868 }
1869
1870
1871 static int nct1008_resume_wakeup(struct device *dev)
1872 {
1873         int err = 0;
1874         struct i2c_client *client = to_i2c_client(dev);
1875
1876         err = disable_irq_wake(client->irq);
1877         if (err) {
1878                 dev_err(&client->dev, "Error: %s, error=%d. failed to disable NCT "
1879                                 "wakeup\n", __func__, err);
1880                 return err;
1881         }
1882
1883         /* NCT wasn't powered down, so IRQ is still enabled. */
1884         /* Disable it before calling update */
1885         disable_irq(client->irq);
1886
1887         return err;
1888 }
1889
1890 static int nct1008_resume_powerdown(struct device *dev)
1891 {
1892         struct i2c_client *client = to_i2c_client(dev);
1893         int err = 0;
1894         struct nct1008_data *data = i2c_get_clientdata(client);
1895
1896         nct1008_power_control(data, true);
1897         nct1008_configure_sensor(data);
1898         err = nct1008_enable(client);
1899         if (err < 0) {
1900                 dev_err(&client->dev, "Error: %s, error=%d\n",
1901                         __func__, err);
1902                 return err;
1903         }
1904
1905         return err;
1906 }
1907
1908 static int nct1008_resume(struct device *dev)
1909 {
1910         struct i2c_client *client = to_i2c_client(dev);
1911         int err;
1912         struct nct1008_data *data = i2c_get_clientdata(client);
1913
1914         if (data->plat_data.suspend_with_wakeup &&
1915                 data->plat_data.suspend_with_wakeup())
1916                 err = nct1008_resume_wakeup(dev);
1917         else
1918                 err = nct1008_resume_powerdown(dev);
1919
1920         if (err)
1921                 return err;
1922
1923         nct1008_update(LOC, data);
1924         nct1008_update(EXT, data);
1925         mutex_lock(&data->mutex);
1926         data->stop_workqueue = 0;
1927         mutex_unlock(&data->mutex);
1928         enable_irq(client->irq);
1929
1930         return 0;
1931 }
1932
1933 static const struct dev_pm_ops nct1008_pm_ops = {
1934         .suspend        = nct1008_suspend,
1935         .resume         = nct1008_resume,
1936 };
1937
1938 #endif
1939
1940 static const struct i2c_device_id nct1008_id[] = {
1941         { "nct1008", NCT1008 },
1942         { "nct72", NCT72},
1943         {}
1944 };
1945 MODULE_DEVICE_TABLE(i2c, nct1008_id);
1946
1947 static const struct of_device_id nct1008_of_match[] = {
1948         {.compatible = "onsemi,nct72", },
1949         { }
1950 };
1951
1952 static struct i2c_driver nct1008_driver = {
1953         .driver = {
1954                 .name   = "nct1008_nct72",
1955 #ifdef CONFIG_PM_SLEEP
1956                 .pm = &nct1008_pm_ops,
1957 #endif
1958                 .of_match_table = nct1008_of_match,
1959         },
1960         .probe          = nct1008_probe,
1961         .remove         = nct1008_remove,
1962         .id_table       = nct1008_id,
1963         .shutdown       = nct1008_shutdown,
1964 };
1965
1966 static int __init nct1008_sync_thz(struct device *dev, void *unused)
1967 {
1968         struct nct1008_data *data = dev_get_drvdata(dev);
1969         if (data->sensors[LOC].thz)
1970                 thermal_zone_device_update(data->sensors[LOC].thz);
1971         if (data->sensors[EXT].thz)
1972                 thermal_zone_device_update(data->sensors[EXT].thz);
1973         return 0;
1974 }
1975
1976 static int __init nct1008_sync(void)
1977 {
1978         return driver_for_each_device(
1979                 &nct1008_driver.driver, NULL, NULL, nct1008_sync_thz);
1980 }
1981 late_initcall_sync(nct1008_sync);
1982
1983 static int __init nct1008_init(void)
1984 {
1985         return i2c_add_driver(&nct1008_driver);
1986 }
1987
1988 static void __exit nct1008_exit(void)
1989 {
1990         i2c_del_driver(&nct1008_driver);
1991 }
1992
1993 MODULE_DESCRIPTION("Temperature sensor driver for OnSemi NCT1008/NCT72");
1994 MODULE_LICENSE("GPL");
1995
1996 module_init(nct1008_init);
1997 module_exit(nct1008_exit);