Merge branch 'for-5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/wq
[sfrench/cifs-2.6.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 #include <linux/interrupt.h>
34
35 #include "adt7x10.h"
36
37 /*
38  * ADT7X10 status
39  */
40 #define ADT7X10_STAT_T_LOW              (1 << 4)
41 #define ADT7X10_STAT_T_HIGH             (1 << 5)
42 #define ADT7X10_STAT_T_CRIT             (1 << 6)
43 #define ADT7X10_STAT_NOT_RDY            (1 << 7)
44
45 /*
46  * ADT7X10 config
47  */
48 #define ADT7X10_FAULT_QUEUE_MASK        (1 << 0 | 1 << 1)
49 #define ADT7X10_CT_POLARITY             (1 << 2)
50 #define ADT7X10_INT_POLARITY            (1 << 3)
51 #define ADT7X10_EVENT_MODE              (1 << 4)
52 #define ADT7X10_MODE_MASK               (1 << 5 | 1 << 6)
53 #define ADT7X10_FULL                    (0 << 5 | 0 << 6)
54 #define ADT7X10_PD                      (1 << 5 | 1 << 6)
55 #define ADT7X10_RESOLUTION              (1 << 7)
56
57 /*
58  * ADT7X10 masks
59  */
60 #define ADT7X10_T13_VALUE_MASK          0xFFF8
61 #define ADT7X10_T_HYST_MASK             0xF
62
63 /* straight from the datasheet */
64 #define ADT7X10_TEMP_MIN (-55000)
65 #define ADT7X10_TEMP_MAX 150000
66
67 /* Each client has this additional data */
68 struct adt7x10_data {
69         const struct adt7x10_ops *ops;
70         const char              *name;
71         struct device           *hwmon_dev;
72         struct mutex            update_lock;
73         u8                      config;
74         u8                      oldconfig;
75         bool                    valid;          /* true if registers valid */
76         unsigned long           last_updated;   /* In jiffies */
77         s16                     temp[4];        /* Register values,
78                                                    0 = input
79                                                    1 = high
80                                                    2 = low
81                                                    3 = critical */
82         u8                      hyst;           /* hysteresis offset */
83 };
84
85 static int adt7x10_read_byte(struct device *dev, u8 reg)
86 {
87         struct adt7x10_data *d = dev_get_drvdata(dev);
88         return d->ops->read_byte(dev, reg);
89 }
90
91 static int adt7x10_write_byte(struct device *dev, u8 reg, u8 data)
92 {
93         struct adt7x10_data *d = dev_get_drvdata(dev);
94         return d->ops->write_byte(dev, reg, data);
95 }
96
97 static int adt7x10_read_word(struct device *dev, u8 reg)
98 {
99         struct adt7x10_data *d = dev_get_drvdata(dev);
100         return d->ops->read_word(dev, reg);
101 }
102
103 static int adt7x10_write_word(struct device *dev, u8 reg, u16 data)
104 {
105         struct adt7x10_data *d = dev_get_drvdata(dev);
106         return d->ops->write_word(dev, reg, data);
107 }
108
109 static const u8 ADT7X10_REG_TEMP[4] = {
110         ADT7X10_TEMPERATURE,            /* input */
111         ADT7X10_T_ALARM_HIGH,           /* high */
112         ADT7X10_T_ALARM_LOW,            /* low */
113         ADT7X10_T_CRIT,                 /* critical */
114 };
115
116 static irqreturn_t adt7x10_irq_handler(int irq, void *private)
117 {
118         struct device *dev = private;
119         int status;
120
121         status = adt7x10_read_byte(dev, ADT7X10_STATUS);
122         if (status < 0)
123                 return IRQ_HANDLED;
124
125         if (status & ADT7X10_STAT_T_HIGH)
126                 sysfs_notify(&dev->kobj, NULL, "temp1_max_alarm");
127         if (status & ADT7X10_STAT_T_LOW)
128                 sysfs_notify(&dev->kobj, NULL, "temp1_min_alarm");
129         if (status & ADT7X10_STAT_T_CRIT)
130                 sysfs_notify(&dev->kobj, NULL, "temp1_crit_alarm");
131
132         return IRQ_HANDLED;
133 }
134
135 static int adt7x10_temp_ready(struct device *dev)
136 {
137         int i, status;
138
139         for (i = 0; i < 6; i++) {
140                 status = adt7x10_read_byte(dev, ADT7X10_STATUS);
141                 if (status < 0)
142                         return status;
143                 if (!(status & ADT7X10_STAT_NOT_RDY))
144                         return 0;
145                 msleep(60);
146         }
147         return -ETIMEDOUT;
148 }
149
150 static int adt7x10_update_temp(struct device *dev)
151 {
152         struct adt7x10_data *data = dev_get_drvdata(dev);
153         int ret = 0;
154
155         mutex_lock(&data->update_lock);
156
157         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
158             || !data->valid) {
159                 int temp;
160
161                 dev_dbg(dev, "Starting update\n");
162
163                 ret = adt7x10_temp_ready(dev); /* check for new value */
164                 if (ret)
165                         goto abort;
166
167                 temp = adt7x10_read_word(dev, ADT7X10_REG_TEMP[0]);
168                 if (temp < 0) {
169                         ret = temp;
170                         dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
171                                 ADT7X10_REG_TEMP[0], ret);
172                         goto abort;
173                 }
174                 data->temp[0] = temp;
175                 data->last_updated = jiffies;
176                 data->valid = true;
177         }
178
179 abort:
180         mutex_unlock(&data->update_lock);
181         return ret;
182 }
183
184 static int adt7x10_fill_cache(struct device *dev)
185 {
186         struct adt7x10_data *data = dev_get_drvdata(dev);
187         int ret;
188         int i;
189
190         for (i = 1; i < ARRAY_SIZE(data->temp); i++) {
191                 ret = adt7x10_read_word(dev, ADT7X10_REG_TEMP[i]);
192                 if (ret < 0) {
193                         dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
194                                 ADT7X10_REG_TEMP[i], ret);
195                         return ret;
196                 }
197                 data->temp[i] = ret;
198         }
199
200         ret = adt7x10_read_byte(dev, ADT7X10_T_HYST);
201         if (ret < 0) {
202                 dev_dbg(dev, "Failed to read value: reg %d, error %d\n",
203                                 ADT7X10_T_HYST, ret);
204                 return ret;
205         }
206         data->hyst = ret;
207
208         return 0;
209 }
210
211 static s16 ADT7X10_TEMP_TO_REG(long temp)
212 {
213         return DIV_ROUND_CLOSEST(clamp_val(temp, ADT7X10_TEMP_MIN,
214                                                ADT7X10_TEMP_MAX) * 128, 1000);
215 }
216
217 static int ADT7X10_REG_TO_TEMP(struct adt7x10_data *data, s16 reg)
218 {
219         /* in 13 bit mode, bits 0-2 are status flags - mask them out */
220         if (!(data->config & ADT7X10_RESOLUTION))
221                 reg &= ADT7X10_T13_VALUE_MASK;
222         /*
223          * temperature is stored in twos complement format, in steps of
224          * 1/128°C
225          */
226         return DIV_ROUND_CLOSEST(reg * 1000, 128);
227 }
228
229 /*-----------------------------------------------------------------------*/
230
231 /* sysfs attributes for hwmon */
232
233 static ssize_t adt7x10_temp_show(struct device *dev,
234                                  struct device_attribute *da, char *buf)
235 {
236         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
237         struct adt7x10_data *data = dev_get_drvdata(dev);
238
239
240         if (attr->index == 0) {
241                 int ret;
242
243                 ret = adt7x10_update_temp(dev);
244                 if (ret)
245                         return ret;
246         }
247
248         return sprintf(buf, "%d\n", ADT7X10_REG_TO_TEMP(data,
249                        data->temp[attr->index]));
250 }
251
252 static ssize_t adt7x10_temp_store(struct device *dev,
253                                   struct device_attribute *da,
254                                   const char *buf, size_t count)
255 {
256         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
257         struct adt7x10_data *data = dev_get_drvdata(dev);
258         int nr = attr->index;
259         long temp;
260         int ret;
261
262         ret = kstrtol(buf, 10, &temp);
263         if (ret)
264                 return ret;
265
266         mutex_lock(&data->update_lock);
267         data->temp[nr] = ADT7X10_TEMP_TO_REG(temp);
268         ret = adt7x10_write_word(dev, ADT7X10_REG_TEMP[nr], data->temp[nr]);
269         if (ret)
270                 count = ret;
271         mutex_unlock(&data->update_lock);
272         return count;
273 }
274
275 static ssize_t adt7x10_t_hyst_show(struct device *dev,
276                                    struct device_attribute *da, char *buf)
277 {
278         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
279         struct adt7x10_data *data = dev_get_drvdata(dev);
280         int nr = attr->index;
281         int hyst;
282
283         hyst = (data->hyst & ADT7X10_T_HYST_MASK) * 1000;
284
285         /*
286          * hysteresis is stored as a 4 bit offset in the device, convert it
287          * to an absolute value
288          */
289         if (nr == 2)    /* min has positive offset, others have negative */
290                 hyst = -hyst;
291         return sprintf(buf, "%d\n",
292                        ADT7X10_REG_TO_TEMP(data, data->temp[nr]) - hyst);
293 }
294
295 static ssize_t adt7x10_t_hyst_store(struct device *dev,
296                                     struct device_attribute *da,
297                                     const char *buf, size_t count)
298 {
299         struct adt7x10_data *data = dev_get_drvdata(dev);
300         int limit, ret;
301         long hyst;
302
303         ret = kstrtol(buf, 10, &hyst);
304         if (ret)
305                 return ret;
306         /* convert absolute hysteresis value to a 4 bit delta value */
307         limit = ADT7X10_REG_TO_TEMP(data, data->temp[1]);
308         hyst = clamp_val(hyst, ADT7X10_TEMP_MIN, ADT7X10_TEMP_MAX);
309         data->hyst = clamp_val(DIV_ROUND_CLOSEST(limit - hyst, 1000),
310                                    0, ADT7X10_T_HYST_MASK);
311         ret = adt7x10_write_byte(dev, ADT7X10_T_HYST, data->hyst);
312         if (ret)
313                 return ret;
314
315         return count;
316 }
317
318 static ssize_t adt7x10_alarm_show(struct device *dev,
319                                   struct device_attribute *da, char *buf)
320 {
321         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
322         int ret;
323
324         ret = adt7x10_read_byte(dev, ADT7X10_STATUS);
325         if (ret < 0)
326                 return ret;
327
328         return sprintf(buf, "%d\n", !!(ret & attr->index));
329 }
330
331 static ssize_t name_show(struct device *dev, struct device_attribute *da,
332                          char *buf)
333 {
334         struct adt7x10_data *data = dev_get_drvdata(dev);
335
336         return sprintf(buf, "%s\n", data->name);
337 }
338
339 static SENSOR_DEVICE_ATTR_RO(temp1_input, adt7x10_temp, 0);
340 static SENSOR_DEVICE_ATTR_RW(temp1_max, adt7x10_temp, 1);
341 static SENSOR_DEVICE_ATTR_RW(temp1_min, adt7x10_temp, 2);
342 static SENSOR_DEVICE_ATTR_RW(temp1_crit, adt7x10_temp, 3);
343 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, adt7x10_t_hyst, 1);
344 static SENSOR_DEVICE_ATTR_RO(temp1_min_hyst, adt7x10_t_hyst, 2);
345 static SENSOR_DEVICE_ATTR_RO(temp1_crit_hyst, adt7x10_t_hyst, 3);
346 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, adt7x10_alarm,
347                              ADT7X10_STAT_T_LOW);
348 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, adt7x10_alarm,
349                              ADT7X10_STAT_T_HIGH);
350 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, adt7x10_alarm,
351                              ADT7X10_STAT_T_CRIT);
352 static DEVICE_ATTR_RO(name);
353
354 static struct attribute *adt7x10_attributes[] = {
355         &sensor_dev_attr_temp1_input.dev_attr.attr,
356         &sensor_dev_attr_temp1_max.dev_attr.attr,
357         &sensor_dev_attr_temp1_min.dev_attr.attr,
358         &sensor_dev_attr_temp1_crit.dev_attr.attr,
359         &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
360         &sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
361         &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
362         &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
363         &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
364         &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
365         NULL
366 };
367
368 static const struct attribute_group adt7x10_group = {
369         .attrs = adt7x10_attributes,
370 };
371
372 int adt7x10_probe(struct device *dev, const char *name, int irq,
373                   const struct adt7x10_ops *ops)
374 {
375         struct adt7x10_data *data;
376         int ret;
377
378         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
379         if (!data)
380                 return -ENOMEM;
381
382         data->ops = ops;
383         data->name = name;
384
385         dev_set_drvdata(dev, data);
386         mutex_init(&data->update_lock);
387
388         /* configure as specified */
389         ret = adt7x10_read_byte(dev, ADT7X10_CONFIG);
390         if (ret < 0) {
391                 dev_dbg(dev, "Can't read config? %d\n", ret);
392                 return ret;
393         }
394         data->oldconfig = ret;
395
396         /*
397          * Set to 16 bit resolution, continous conversion and comparator mode.
398          */
399         data->config = data->oldconfig;
400         data->config &= ~(ADT7X10_MODE_MASK | ADT7X10_CT_POLARITY |
401                         ADT7X10_INT_POLARITY);
402         data->config |= ADT7X10_FULL | ADT7X10_RESOLUTION | ADT7X10_EVENT_MODE;
403
404         if (data->config != data->oldconfig) {
405                 ret = adt7x10_write_byte(dev, ADT7X10_CONFIG, data->config);
406                 if (ret)
407                         return ret;
408         }
409         dev_dbg(dev, "Config %02x\n", data->config);
410
411         ret = adt7x10_fill_cache(dev);
412         if (ret)
413                 goto exit_restore;
414
415         /* Register sysfs hooks */
416         ret = sysfs_create_group(&dev->kobj, &adt7x10_group);
417         if (ret)
418                 goto exit_restore;
419
420         /*
421          * The I2C device will already have it's own 'name' attribute, but for
422          * the SPI device we need to register it. name will only be non NULL if
423          * the device doesn't register the 'name' attribute on its own.
424          */
425         if (name) {
426                 ret = device_create_file(dev, &dev_attr_name);
427                 if (ret)
428                         goto exit_remove;
429         }
430
431         data->hwmon_dev = hwmon_device_register(dev);
432         if (IS_ERR(data->hwmon_dev)) {
433                 ret = PTR_ERR(data->hwmon_dev);
434                 goto exit_remove_name;
435         }
436
437         if (irq > 0) {
438                 ret = request_threaded_irq(irq, NULL, adt7x10_irq_handler,
439                                 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
440                                 dev_name(dev), dev);
441                 if (ret)
442                         goto exit_hwmon_device_unregister;
443         }
444
445         return 0;
446
447 exit_hwmon_device_unregister:
448         hwmon_device_unregister(data->hwmon_dev);
449 exit_remove_name:
450         if (name)
451                 device_remove_file(dev, &dev_attr_name);
452 exit_remove:
453         sysfs_remove_group(&dev->kobj, &adt7x10_group);
454 exit_restore:
455         adt7x10_write_byte(dev, ADT7X10_CONFIG, data->oldconfig);
456         return ret;
457 }
458 EXPORT_SYMBOL_GPL(adt7x10_probe);
459
460 int adt7x10_remove(struct device *dev, int irq)
461 {
462         struct adt7x10_data *data = dev_get_drvdata(dev);
463
464         if (irq > 0)
465                 free_irq(irq, dev);
466
467         hwmon_device_unregister(data->hwmon_dev);
468         if (data->name)
469                 device_remove_file(dev, &dev_attr_name);
470         sysfs_remove_group(&dev->kobj, &adt7x10_group);
471         if (data->oldconfig != data->config)
472                 adt7x10_write_byte(dev, ADT7X10_CONFIG, data->oldconfig);
473         return 0;
474 }
475 EXPORT_SYMBOL_GPL(adt7x10_remove);
476
477 #ifdef CONFIG_PM_SLEEP
478
479 static int adt7x10_suspend(struct device *dev)
480 {
481         struct adt7x10_data *data = dev_get_drvdata(dev);
482
483         return adt7x10_write_byte(dev, ADT7X10_CONFIG,
484                 data->config | ADT7X10_PD);
485 }
486
487 static int adt7x10_resume(struct device *dev)
488 {
489         struct adt7x10_data *data = dev_get_drvdata(dev);
490
491         return adt7x10_write_byte(dev, ADT7X10_CONFIG, data->config);
492 }
493
494 SIMPLE_DEV_PM_OPS(adt7x10_dev_pm_ops, adt7x10_suspend, adt7x10_resume);
495 EXPORT_SYMBOL_GPL(adt7x10_dev_pm_ops);
496
497 #endif /* CONFIG_PM_SLEEP */
498
499 MODULE_AUTHOR("Hartmut Knaack");
500 MODULE_DESCRIPTION("ADT7410/ADT7420, ADT7310/ADT7320 common code");
501 MODULE_LICENSE("GPL");