84b3dfc1c1cdd1070fcac8b5f915767aa643d3bd
[firefly-linux-kernel-4.4.55.git] / drivers / hwmon / adt7x10.c
1 /*
2  * adt7x10.c - Part of lm_sensors, Linux kernel modules for hardware
3  *       monitoring
4  * This driver handles the ADT7410 and compatible digital temperature sensors.
5  * Hartmut Knaack <knaack.h@gmx.de> 2012-07-22
6  * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
7  * and adt7410.c from iio-staging by Sonic Zhang <sonic.zhang@analog.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/hwmon.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/err.h>
31 #include <linux/mutex.h>
32 #include <linux/delay.h>
33
34 #include "adt7x10.h"
35
36 /*
37  * ADT7X10 status
38  */
39 #define ADT7X10_STAT_T_LOW              (1 << 4)
40 #define ADT7X10_STAT_T_HIGH             (1 << 5)
41 #define ADT7X10_STAT_T_CRIT             (1 << 6)
42 #define ADT7X10_STAT_NOT_RDY            (1 << 7)
43
44 /*
45  * ADT7X10 config
46  */
47 #define ADT7X10_FAULT_QUEUE_MASK        (1 << 0 | 1 << 1)
48 #define ADT7X10_CT_POLARITY             (1 << 2)
49 #define ADT7X10_INT_POLARITY            (1 << 3)
50 #define ADT7X10_EVENT_MODE              (1 << 4)
51 #define ADT7X10_MODE_MASK               (1 << 5 | 1 << 6)
52 #define ADT7X10_FULL                    (0 << 5 | 0 << 6)
53 #define ADT7X10_PD                      (1 << 5 | 1 << 6)
54 #define ADT7X10_RESOLUTION              (1 << 7)
55
56 /*
57  * ADT7X10 masks
58  */
59 #define ADT7X10_T13_VALUE_MASK          0xFFF8
60 #define ADT7X10_T_HYST_MASK             0xF
61
62 /* straight from the datasheet */
63 #define ADT7X10_TEMP_MIN (-55000)
64 #define ADT7X10_TEMP_MAX 150000
65
66 /* Each client has this additional data */
67 struct adt7x10_data {
68         const struct adt7x10_ops *ops;
69         const char              *name;
70         struct device           *hwmon_dev;
71         struct mutex            update_lock;
72         u8                      config;
73         u8                      oldconfig;
74         bool                    valid;          /* true if registers valid */
75         unsigned long           last_updated;   /* In jiffies */
76         s16                     temp[4];        /* Register values,
77                                                    0 = input
78                                                    1 = high
79                                                    2 = low
80                                                    3 = critical */
81         u8                      hyst;           /* hysteresis offset */
82 };
83
84 static int adt7x10_read_byte(struct device *dev, u8 reg)
85 {
86         struct adt7x10_data *d = dev_get_drvdata(dev);
87         return d->ops->read_byte(dev, reg);
88 }
89
90 static int adt7x10_write_byte(struct device *dev, u8 reg, u8 data)
91 {
92         struct adt7x10_data *d = dev_get_drvdata(dev);
93         return d->ops->write_byte(dev, reg, data);
94 }
95
96 static int adt7x10_read_word(struct device *dev, u8 reg)
97 {
98         struct adt7x10_data *d = dev_get_drvdata(dev);
99         return d->ops->read_word(dev, reg);
100 }
101
102 static int adt7x10_write_word(struct device *dev, u8 reg, u16 data)
103 {
104         struct adt7x10_data *d = dev_get_drvdata(dev);
105         return d->ops->write_word(dev, reg, data);
106 }
107
108 static const u8 ADT7X10_REG_TEMP[4] = {
109         ADT7X10_TEMPERATURE,            /* input */
110         ADT7X10_T_ALARM_HIGH,           /* high */
111         ADT7X10_T_ALARM_LOW,            /* low */
112         ADT7X10_T_CRIT,                 /* critical */
113 };
114
115 static int adt7x10_temp_ready(struct device *dev)
116 {
117         int i, status;
118
119         for (i = 0; i < 6; i++) {
120                 status = adt7x10_read_byte(dev, ADT7X10_STATUS);
121                 if (status < 0)
122                         return status;
123                 if (!(status & ADT7X10_STAT_NOT_RDY))
124                         return 0;
125                 msleep(60);
126         }
127         return -ETIMEDOUT;
128 }
129
130 static int adt7x10_update_temp(struct device *dev)
131 {
132         struct adt7x10_data *data = dev_get_drvdata(dev);
133         int ret = 0;
134
135         mutex_lock(&data->update_lock);
136
137         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
138             || !data->valid) {
139                 int temp;
140
141                 dev_dbg(dev, "Starting update\n");
142
143                 ret = adt7x10_temp_ready(dev); /* check for new value */
144                 if (ret)
145                         goto abort;
146
147                 temp = adt7x10_read_word(dev, ADT7X10_REG_TEMP[0]);
148                 if (temp < 0) {
149                         ret = temp;
150                         dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
151                                 ADT7X10_REG_TEMP[0], ret);
152                         goto abort;
153                 }
154                 data->temp[0] = temp;
155                 data->last_updated = jiffies;
156                 data->valid = true;
157         }
158
159 abort:
160         mutex_unlock(&data->update_lock);
161         return ret;
162 }
163
164 static int adt7x10_fill_cache(struct device *dev)
165 {
166         struct adt7x10_data *data = dev_get_drvdata(dev);
167         int ret;
168         int i;
169
170         for (i = 1; i < ARRAY_SIZE(data->temp); i++) {
171                 ret = adt7x10_read_word(dev, ADT7X10_REG_TEMP[i]);
172                 if (ret < 0) {
173                         dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
174                                 ADT7X10_REG_TEMP[i], ret);
175                         return ret;
176                 }
177                 data->temp[i] = ret;
178         }
179
180         ret = adt7x10_read_byte(dev, ADT7X10_T_HYST);
181         if (ret < 0) {
182                 dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
183                                 ADT7X10_T_HYST, ret);
184                 return ret;
185         }
186         data->hyst = ret;
187
188         return 0;
189 }
190
191 static s16 ADT7X10_TEMP_TO_REG(long temp)
192 {
193         return DIV_ROUND_CLOSEST(clamp_val(temp, ADT7X10_TEMP_MIN,
194                                                ADT7X10_TEMP_MAX) * 128, 1000);
195 }
196
197 static int ADT7X10_REG_TO_TEMP(struct adt7x10_data *data, s16 reg)
198 {
199         /* in 13 bit mode, bits 0-2 are status flags - mask them out */
200         if (!(data->config & ADT7X10_RESOLUTION))
201                 reg &= ADT7X10_T13_VALUE_MASK;
202         /*
203          * temperature is stored in twos complement format, in steps of
204          * 1/128°C
205          */
206         return DIV_ROUND_CLOSEST(reg * 1000, 128);
207 }
208
209 /*-----------------------------------------------------------------------*/
210
211 /* sysfs attributes for hwmon */
212
213 static ssize_t adt7x10_show_temp(struct device *dev,
214                                  struct device_attribute *da,
215                                  char *buf)
216 {
217         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
218         struct adt7x10_data *data = dev_get_drvdata(dev);
219
220
221         if (attr->index == 0) {
222                 int ret;
223
224                 ret = adt7x10_update_temp(dev);
225                 if (ret)
226                         return ret;
227         }
228
229         return sprintf(buf, "%d\n", ADT7X10_REG_TO_TEMP(data,
230                        data->temp[attr->index]));
231 }
232
233 static ssize_t adt7x10_set_temp(struct device *dev,
234                                 struct device_attribute *da,
235                                 const char *buf, size_t count)
236 {
237         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
238         struct adt7x10_data *data = dev_get_drvdata(dev);
239         int nr = attr->index;
240         long temp;
241         int ret;
242
243         ret = kstrtol(buf, 10, &temp);
244         if (ret)
245                 return ret;
246
247         mutex_lock(&data->update_lock);
248         data->temp[nr] = ADT7X10_TEMP_TO_REG(temp);
249         ret = adt7x10_write_word(dev, ADT7X10_REG_TEMP[nr], data->temp[nr]);
250         if (ret)
251                 count = ret;
252         mutex_unlock(&data->update_lock);
253         return count;
254 }
255
256 static ssize_t adt7x10_show_t_hyst(struct device *dev,
257                                    struct device_attribute *da,
258                                    char *buf)
259 {
260         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
261         struct adt7x10_data *data = dev_get_drvdata(dev);
262         int nr = attr->index;
263         int hyst;
264
265         hyst = (data->hyst & ADT7X10_T_HYST_MASK) * 1000;
266
267         /*
268          * hysteresis is stored as a 4 bit offset in the device, convert it
269          * to an absolute value
270          */
271         if (nr == 2)    /* min has positive offset, others have negative */
272                 hyst = -hyst;
273         return sprintf(buf, "%d\n",
274                        ADT7X10_REG_TO_TEMP(data, data->temp[nr]) - hyst);
275 }
276
277 static ssize_t adt7x10_set_t_hyst(struct device *dev,
278                                   struct device_attribute *da,
279                                   const char *buf, size_t count)
280 {
281         struct adt7x10_data *data = dev_get_drvdata(dev);
282         int limit, ret;
283         long hyst;
284
285         ret = kstrtol(buf, 10, &hyst);
286         if (ret)
287                 return ret;
288         /* convert absolute hysteresis value to a 4 bit delta value */
289         limit = ADT7X10_REG_TO_TEMP(data, data->temp[1]);
290         hyst = clamp_val(hyst, ADT7X10_TEMP_MIN, ADT7X10_TEMP_MAX);
291         data->hyst = clamp_val(DIV_ROUND_CLOSEST(limit - hyst, 1000),
292                                    0, ADT7X10_T_HYST_MASK);
293         ret = adt7x10_write_byte(dev, ADT7X10_T_HYST, data->hyst);
294         if (ret)
295                 return ret;
296
297         return count;
298 }
299
300 static ssize_t adt7x10_show_alarm(struct device *dev,
301                                   struct device_attribute *da,
302                                   char *buf)
303 {
304         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
305         int ret;
306
307         ret = adt7x10_read_byte(dev, ADT7X10_STATUS);
308         if (ret < 0)
309                 return ret;
310
311         return sprintf(buf, "%d\n", !!(ret & attr->index));
312 }
313
314 static ssize_t adt7x10_show_name(struct device *dev,
315                                  struct device_attribute *da,
316                                  char *buf)
317 {
318         struct adt7x10_data *data = dev_get_drvdata(dev);
319
320         return sprintf(buf, "%s\n", data->name);
321 }
322
323 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7x10_show_temp, NULL, 0);
324 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
325                           adt7x10_show_temp, adt7x10_set_temp, 1);
326 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
327                           adt7x10_show_temp, adt7x10_set_temp, 2);
328 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
329                           adt7x10_show_temp, adt7x10_set_temp, 3);
330 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
331                           adt7x10_show_t_hyst, adt7x10_set_t_hyst, 1);
332 static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
333                           adt7x10_show_t_hyst, NULL, 2);
334 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO,
335                           adt7x10_show_t_hyst, NULL, 3);
336 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, adt7x10_show_alarm,
337                           NULL, ADT7X10_STAT_T_LOW);
338 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adt7x10_show_alarm,
339                           NULL, ADT7X10_STAT_T_HIGH);
340 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, adt7x10_show_alarm,
341                           NULL, ADT7X10_STAT_T_CRIT);
342 static DEVICE_ATTR(name, S_IRUGO, adt7x10_show_name, NULL);
343
344 static struct attribute *adt7x10_attributes[] = {
345         &sensor_dev_attr_temp1_input.dev_attr.attr,
346         &sensor_dev_attr_temp1_max.dev_attr.attr,
347         &sensor_dev_attr_temp1_min.dev_attr.attr,
348         &sensor_dev_attr_temp1_crit.dev_attr.attr,
349         &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
350         &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
351         &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
352         &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
353         &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
354         &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
355         NULL
356 };
357
358 static const struct attribute_group adt7x10_group = {
359         .attrs = adt7x10_attributes,
360 };
361
362 int adt7x10_probe(struct device *dev, const char *name,
363                   const struct adt7x10_ops *ops)
364 {
365         struct adt7x10_data *data;
366         int ret;
367
368         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
369         if (!data)
370                 return -ENOMEM;
371
372         data->ops = ops;
373         data->name = name;
374
375         dev_set_drvdata(dev, data);
376         mutex_init(&data->update_lock);
377
378         /* configure as specified */
379         ret = adt7x10_read_byte(dev, ADT7X10_CONFIG);
380         if (ret < 0) {
381                 dev_dbg(dev, "Can't read config? %d\n", ret);
382                 return ret;
383         }
384         data->oldconfig = ret;
385
386         /*
387          * Set to 16 bit resolution, continous conversion and comparator mode.
388          */
389         data->config = data->oldconfig;
390         data->config &= ~ADT7X10_MODE_MASK;
391         data->config |= ADT7X10_FULL | ADT7X10_RESOLUTION | ADT7X10_EVENT_MODE;
392         if (data->config != data->oldconfig) {
393                 ret = adt7x10_write_byte(dev, ADT7X10_CONFIG, data->config);
394                 if (ret)
395                         return ret;
396         }
397         dev_dbg(dev, "Config %02x\n", data->config);
398
399         ret = adt7x10_fill_cache(dev);
400         if (ret)
401                 goto exit_restore;
402
403         /* Register sysfs hooks */
404         ret = sysfs_create_group(&dev->kobj, &adt7x10_group);
405         if (ret)
406                 goto exit_restore;
407
408         /*
409          * The I2C device will already have it's own 'name' attribute, but for
410          * the SPI device we need to register it. name will only be non NULL if
411          * the device doesn't register the 'name' attribute on its own.
412          */
413         if (name) {
414                 ret = device_create_file(dev, &dev_attr_name);
415                 if (ret)
416                         goto exit_remove;
417         }
418
419         data->hwmon_dev = hwmon_device_register(dev);
420         if (IS_ERR(data->hwmon_dev)) {
421                 ret = PTR_ERR(data->hwmon_dev);
422                 goto exit_remove_name;
423         }
424
425         return 0;
426
427 exit_remove_name:
428         if (name)
429                 device_remove_file(dev, &dev_attr_name);
430 exit_remove:
431         sysfs_remove_group(&dev->kobj, &adt7x10_group);
432 exit_restore:
433         adt7x10_write_byte(dev, ADT7X10_CONFIG, data->oldconfig);
434         return ret;
435 }
436 EXPORT_SYMBOL_GPL(adt7x10_probe);
437
438 int adt7x10_remove(struct device *dev)
439 {
440         struct adt7x10_data *data = dev_get_drvdata(dev);
441
442         hwmon_device_unregister(data->hwmon_dev);
443         if (data->name)
444                 device_remove_file(dev, &dev_attr_name);
445         sysfs_remove_group(&dev->kobj, &adt7x10_group);
446         if (data->oldconfig != data->config)
447                 adt7x10_write_byte(dev, ADT7X10_CONFIG, data->oldconfig);
448         return 0;
449 }
450 EXPORT_SYMBOL_GPL(adt7x10_remove);
451
452 #ifdef CONFIG_PM_SLEEP
453
454 static int adt7x10_suspend(struct device *dev)
455 {
456         struct adt7x10_data *data = dev_get_drvdata(dev);
457
458         return adt7x10_write_byte(dev, ADT7X10_CONFIG,
459                 data->config | ADT7X10_PD);
460 }
461
462 static int adt7x10_resume(struct device *dev)
463 {
464         struct adt7x10_data *data = dev_get_drvdata(dev);
465
466         return adt7x10_write_byte(dev, ADT7X10_CONFIG, data->config);
467 }
468
469 SIMPLE_DEV_PM_OPS(adt7x10_dev_pm_ops, adt7x10_suspend, adt7x10_resume);
470 EXPORT_SYMBOL_GPL(adt7x10_dev_pm_ops);
471
472 #endif /* CONFIG_PM_SLEEP */
473
474 MODULE_AUTHOR("Hartmut Knaack");
475 MODULE_DESCRIPTION("ADT7410/ADT7420, ADT7310/ADT7320 common code");
476 MODULE_LICENSE("GPL");