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