hwmon: (lm75) Tune resolution and sample time per chip
[firefly-linux-kernel-4.4.55.git] / drivers / hwmon / lm75.c
1 /*
2  * lm75.c - Part of lm_sensors, Linux kernel modules for hardware
3  *       monitoring
4  * Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include "lm75.h"
31
32
33 /*
34  * This driver handles the LM75 and compatible digital temperature sensors.
35  */
36
37 enum lm75_type {                /* keep sorted in alphabetical order */
38         adt75,
39         ds1775,
40         ds75,
41         lm75,
42         lm75a,
43         max6625,
44         max6626,
45         mcp980x,
46         stds75,
47         tcn75,
48         tmp100,
49         tmp101,
50         tmp105,
51         tmp175,
52         tmp275,
53         tmp75,
54 };
55
56 /* Addresses scanned */
57 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
58                                         0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
59
60
61 /* The LM75 registers */
62 #define LM75_REG_CONF           0x01
63 static const u8 LM75_REG_TEMP[3] = {
64         0x00,           /* input */
65         0x03,           /* max */
66         0x02,           /* hyst */
67 };
68
69 /* Each client has this additional data */
70 struct lm75_data {
71         struct device           *hwmon_dev;
72         struct mutex            update_lock;
73         u8                      orig_conf;
74         u8                      resolution;     /* In bits, between 9 and 12 */
75         u8                      resolution_limits;
76         char                    valid;          /* !=0 if registers are valid */
77         unsigned long           last_updated;   /* In jiffies */
78         unsigned long           sample_time;    /* In jiffies */
79         s16                     temp[3];        /* Register values,
80                                                    0 = input
81                                                    1 = max
82                                                    2 = hyst */
83 };
84
85 static int lm75_read_value(struct i2c_client *client, u8 reg);
86 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
87 static struct lm75_data *lm75_update_device(struct device *dev);
88
89
90 /*-----------------------------------------------------------------------*/
91
92 /* sysfs attributes for hwmon */
93
94 static ssize_t show_temp(struct device *dev, struct device_attribute *da,
95                          char *buf)
96 {
97         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
98         struct lm75_data *data = lm75_update_device(dev);
99         long temp;
100
101         if (IS_ERR(data))
102                 return PTR_ERR(data);
103
104         temp = ((data->temp[attr->index] >> (16 - data->resolution)) * 1000)
105                >> (data->resolution - 8);
106
107         return sprintf(buf, "%ld\n", temp);
108 }
109
110 static ssize_t set_temp(struct device *dev, struct device_attribute *da,
111                         const char *buf, size_t count)
112 {
113         struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
114         struct i2c_client *client = to_i2c_client(dev);
115         struct lm75_data *data = i2c_get_clientdata(client);
116         int nr = attr->index;
117         long temp;
118         int error;
119         u8 resolution;
120
121         error = kstrtol(buf, 10, &temp);
122         if (error)
123                 return error;
124
125         /*
126          * Resolution of limit registers is assumed to be the same as the
127          * temperature input register resolution unless given explicitly.
128          */
129         if (attr->index && data->resolution_limits)
130                 resolution = data->resolution_limits;
131         else
132                 resolution = data->resolution;
133
134         mutex_lock(&data->update_lock);
135         temp = clamp_val(temp, LM75_TEMP_MIN, LM75_TEMP_MAX);
136         data->temp[nr] = DIV_ROUND_CLOSEST(temp  << (resolution - 8),
137                                            1000) << (16 - resolution);
138         lm75_write_value(client, LM75_REG_TEMP[nr], data->temp[nr]);
139         mutex_unlock(&data->update_lock);
140         return count;
141 }
142
143 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
144                         show_temp, set_temp, 1);
145 static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
146                         show_temp, set_temp, 2);
147 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
148
149 static struct attribute *lm75_attributes[] = {
150         &sensor_dev_attr_temp1_input.dev_attr.attr,
151         &sensor_dev_attr_temp1_max.dev_attr.attr,
152         &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
153
154         NULL
155 };
156
157 static const struct attribute_group lm75_group = {
158         .attrs = lm75_attributes,
159 };
160
161 /*-----------------------------------------------------------------------*/
162
163 /* device probe and removal */
164
165 static int
166 lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
167 {
168         struct lm75_data *data;
169         int status;
170         u8 set_mask, clr_mask;
171         int new;
172         enum lm75_type kind = id->driver_data;
173
174         if (!i2c_check_functionality(client->adapter,
175                         I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
176                 return -EIO;
177
178         data = devm_kzalloc(&client->dev, sizeof(struct lm75_data), GFP_KERNEL);
179         if (!data)
180                 return -ENOMEM;
181
182         i2c_set_clientdata(client, data);
183         mutex_init(&data->update_lock);
184
185         /* Set to LM75 resolution (9 bits, 1/2 degree C) and range.
186          * Then tweak to be more precise when appropriate.
187          */
188         set_mask = 0;
189         clr_mask = LM75_SHUTDOWN;               /* continuous conversions */
190
191         switch (kind) {
192         case adt75:
193                 clr_mask |= 1 << 5;             /* not one-shot mode */
194                 data->resolution = 12;
195                 data->sample_time = HZ / 8;
196                 break;
197         case ds1775:
198         case ds75:
199         case stds75:
200                 clr_mask |= 3 << 5;
201                 set_mask |= 2 << 5;             /* 11-bit mode */
202                 data->resolution = 11;
203                 data->sample_time = HZ;
204                 break;
205         case lm75:
206         case lm75a:
207                 data->resolution = 9;
208                 data->sample_time = HZ / 2;
209                 break;
210         case max6625:
211                 data->resolution = 9;
212                 data->sample_time = HZ / 4;
213                 break;
214         case max6626:
215                 data->resolution = 12;
216                 data->resolution_limits = 9;
217                 data->sample_time = HZ / 4;
218                 break;
219         case tcn75:
220                 data->resolution = 9;
221                 data->sample_time = HZ / 8;
222                 break;
223         case mcp980x:
224                 data->resolution_limits = 9;
225                 /* fall through */
226         case tmp100:
227         case tmp101:
228                 set_mask |= 3 << 5;             /* 12-bit mode */
229                 data->resolution = 12;
230                 data->sample_time = HZ;
231                 clr_mask |= 1 << 7;             /* not one-shot mode */
232                 break;
233         case tmp105:
234         case tmp175:
235         case tmp275:
236         case tmp75:
237                 set_mask |= 3 << 5;             /* 12-bit mode */
238                 clr_mask |= 1 << 7;             /* not one-shot mode */
239                 data->resolution = 12;
240                 data->sample_time = HZ / 2;
241                 break;
242         }
243
244         /* configure as specified */
245         status = lm75_read_value(client, LM75_REG_CONF);
246         if (status < 0) {
247                 dev_dbg(&client->dev, "Can't read config? %d\n", status);
248                 return status;
249         }
250         data->orig_conf = status;
251         new = status & ~clr_mask;
252         new |= set_mask;
253         if (status != new)
254                 lm75_write_value(client, LM75_REG_CONF, new);
255         dev_dbg(&client->dev, "Config %02x\n", new);
256
257         /* Register sysfs hooks */
258         status = sysfs_create_group(&client->dev.kobj, &lm75_group);
259         if (status)
260                 return status;
261
262         data->hwmon_dev = hwmon_device_register(&client->dev);
263         if (IS_ERR(data->hwmon_dev)) {
264                 status = PTR_ERR(data->hwmon_dev);
265                 goto exit_remove;
266         }
267
268         dev_info(&client->dev, "%s: sensor '%s'\n",
269                  dev_name(data->hwmon_dev), client->name);
270
271         return 0;
272
273 exit_remove:
274         sysfs_remove_group(&client->dev.kobj, &lm75_group);
275         return status;
276 }
277
278 static int lm75_remove(struct i2c_client *client)
279 {
280         struct lm75_data *data = i2c_get_clientdata(client);
281
282         hwmon_device_unregister(data->hwmon_dev);
283         sysfs_remove_group(&client->dev.kobj, &lm75_group);
284         lm75_write_value(client, LM75_REG_CONF, data->orig_conf);
285         return 0;
286 }
287
288 static const struct i2c_device_id lm75_ids[] = {
289         { "adt75", adt75, },
290         { "ds1775", ds1775, },
291         { "ds75", ds75, },
292         { "lm75", lm75, },
293         { "lm75a", lm75a, },
294         { "max6625", max6625, },
295         { "max6626", max6626, },
296         { "mcp980x", mcp980x, },
297         { "stds75", stds75, },
298         { "tcn75", tcn75, },
299         { "tmp100", tmp100, },
300         { "tmp101", tmp101, },
301         { "tmp105", tmp105, },
302         { "tmp175", tmp175, },
303         { "tmp275", tmp275, },
304         { "tmp75", tmp75, },
305         { /* LIST END */ }
306 };
307 MODULE_DEVICE_TABLE(i2c, lm75_ids);
308
309 #define LM75A_ID 0xA1
310
311 /* Return 0 if detection is successful, -ENODEV otherwise */
312 static int lm75_detect(struct i2c_client *new_client,
313                        struct i2c_board_info *info)
314 {
315         struct i2c_adapter *adapter = new_client->adapter;
316         int i;
317         int conf, hyst, os;
318         bool is_lm75a = 0;
319
320         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
321                                      I2C_FUNC_SMBUS_WORD_DATA))
322                 return -ENODEV;
323
324         /*
325          * Now, we do the remaining detection. There is no identification-
326          * dedicated register so we have to rely on several tricks:
327          * unused bits, registers cycling over 8-address boundaries,
328          * addresses 0x04-0x07 returning the last read value.
329          * The cycling+unused addresses combination is not tested,
330          * since it would significantly slow the detection down and would
331          * hardly add any value.
332          *
333          * The National Semiconductor LM75A is different than earlier
334          * LM75s.  It has an ID byte of 0xaX (where X is the chip
335          * revision, with 1 being the only revision in existence) in
336          * register 7, and unused registers return 0xff rather than the
337          * last read value.
338          *
339          * Note that this function only detects the original National
340          * Semiconductor LM75 and the LM75A. Clones from other vendors
341          * aren't detected, on purpose, because they are typically never
342          * found on PC hardware. They are found on embedded designs where
343          * they can be instantiated explicitly so detection is not needed.
344          * The absence of identification registers on all these clones
345          * would make their exhaustive detection very difficult and weak,
346          * and odds are that the driver would bind to unsupported devices.
347          */
348
349         /* Unused bits */
350         conf = i2c_smbus_read_byte_data(new_client, 1);
351         if (conf & 0xe0)
352                 return -ENODEV;
353
354         /* First check for LM75A */
355         if (i2c_smbus_read_byte_data(new_client, 7) == LM75A_ID) {
356                 /* LM75A returns 0xff on unused registers so
357                    just to be sure we check for that too. */
358                 if (i2c_smbus_read_byte_data(new_client, 4) != 0xff
359                  || i2c_smbus_read_byte_data(new_client, 5) != 0xff
360                  || i2c_smbus_read_byte_data(new_client, 6) != 0xff)
361                         return -ENODEV;
362                 is_lm75a = 1;
363                 hyst = i2c_smbus_read_byte_data(new_client, 2);
364                 os = i2c_smbus_read_byte_data(new_client, 3);
365         } else { /* Traditional style LM75 detection */
366                 /* Unused addresses */
367                 hyst = i2c_smbus_read_byte_data(new_client, 2);
368                 if (i2c_smbus_read_byte_data(new_client, 4) != hyst
369                  || i2c_smbus_read_byte_data(new_client, 5) != hyst
370                  || i2c_smbus_read_byte_data(new_client, 6) != hyst
371                  || i2c_smbus_read_byte_data(new_client, 7) != hyst)
372                         return -ENODEV;
373                 os = i2c_smbus_read_byte_data(new_client, 3);
374                 if (i2c_smbus_read_byte_data(new_client, 4) != os
375                  || i2c_smbus_read_byte_data(new_client, 5) != os
376                  || i2c_smbus_read_byte_data(new_client, 6) != os
377                  || i2c_smbus_read_byte_data(new_client, 7) != os)
378                         return -ENODEV;
379         }
380
381         /* Addresses cycling */
382         for (i = 8; i <= 248; i += 40) {
383                 if (i2c_smbus_read_byte_data(new_client, i + 1) != conf
384                  || i2c_smbus_read_byte_data(new_client, i + 2) != hyst
385                  || i2c_smbus_read_byte_data(new_client, i + 3) != os)
386                         return -ENODEV;
387                 if (is_lm75a && i2c_smbus_read_byte_data(new_client, i + 7)
388                                 != LM75A_ID)
389                         return -ENODEV;
390         }
391
392         strlcpy(info->type, is_lm75a ? "lm75a" : "lm75", I2C_NAME_SIZE);
393
394         return 0;
395 }
396
397 #ifdef CONFIG_PM
398 static int lm75_suspend(struct device *dev)
399 {
400         int status;
401         struct i2c_client *client = to_i2c_client(dev);
402         status = lm75_read_value(client, LM75_REG_CONF);
403         if (status < 0) {
404                 dev_dbg(&client->dev, "Can't read config? %d\n", status);
405                 return status;
406         }
407         status = status | LM75_SHUTDOWN;
408         lm75_write_value(client, LM75_REG_CONF, status);
409         return 0;
410 }
411
412 static int lm75_resume(struct device *dev)
413 {
414         int status;
415         struct i2c_client *client = to_i2c_client(dev);
416         status = lm75_read_value(client, LM75_REG_CONF);
417         if (status < 0) {
418                 dev_dbg(&client->dev, "Can't read config? %d\n", status);
419                 return status;
420         }
421         status = status & ~LM75_SHUTDOWN;
422         lm75_write_value(client, LM75_REG_CONF, status);
423         return 0;
424 }
425
426 static const struct dev_pm_ops lm75_dev_pm_ops = {
427         .suspend        = lm75_suspend,
428         .resume         = lm75_resume,
429 };
430 #define LM75_DEV_PM_OPS (&lm75_dev_pm_ops)
431 #else
432 #define LM75_DEV_PM_OPS NULL
433 #endif /* CONFIG_PM */
434
435 static struct i2c_driver lm75_driver = {
436         .class          = I2C_CLASS_HWMON,
437         .driver = {
438                 .name   = "lm75",
439                 .pm     = LM75_DEV_PM_OPS,
440         },
441         .probe          = lm75_probe,
442         .remove         = lm75_remove,
443         .id_table       = lm75_ids,
444         .detect         = lm75_detect,
445         .address_list   = normal_i2c,
446 };
447
448 /*-----------------------------------------------------------------------*/
449
450 /* register access */
451
452 /*
453  * All registers are word-sized, except for the configuration register.
454  * LM75 uses a high-byte first convention, which is exactly opposite to
455  * the SMBus standard.
456  */
457 static int lm75_read_value(struct i2c_client *client, u8 reg)
458 {
459         if (reg == LM75_REG_CONF)
460                 return i2c_smbus_read_byte_data(client, reg);
461         else
462                 return i2c_smbus_read_word_swapped(client, reg);
463 }
464
465 static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
466 {
467         if (reg == LM75_REG_CONF)
468                 return i2c_smbus_write_byte_data(client, reg, value);
469         else
470                 return i2c_smbus_write_word_swapped(client, reg, value);
471 }
472
473 static struct lm75_data *lm75_update_device(struct device *dev)
474 {
475         struct i2c_client *client = to_i2c_client(dev);
476         struct lm75_data *data = i2c_get_clientdata(client);
477         struct lm75_data *ret = data;
478
479         mutex_lock(&data->update_lock);
480
481         if (time_after(jiffies, data->last_updated + data->sample_time)
482             || !data->valid) {
483                 int i;
484                 dev_dbg(&client->dev, "Starting lm75 update\n");
485
486                 for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
487                         int status;
488
489                         status = lm75_read_value(client, LM75_REG_TEMP[i]);
490                         if (unlikely(status < 0)) {
491                                 dev_dbg(dev,
492                                         "LM75: Failed to read value: reg %d, error %d\n",
493                                         LM75_REG_TEMP[i], status);
494                                 ret = ERR_PTR(status);
495                                 data->valid = 0;
496                                 goto abort;
497                         }
498                         data->temp[i] = status;
499                 }
500                 data->last_updated = jiffies;
501                 data->valid = 1;
502         }
503
504 abort:
505         mutex_unlock(&data->update_lock);
506         return ret;
507 }
508
509 module_i2c_driver(lm75_driver);
510
511 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
512 MODULE_DESCRIPTION("LM75 driver");
513 MODULE_LICENSE("GPL");