]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/hwmon/lm77.c
hwmon: (lm77) Rearrange code to no longer require forward declarations
[linux-beck.git] / drivers / hwmon / lm77.c
1 /*
2  * lm77.c - Part of lm_sensors, Linux kernel modules for hardware
3  *          monitoring
4  *
5  * Copyright (c) 2004  Andras BALI <drewie@freemail.hu>
6  *
7  * Heavily based on lm75.c by Frodo Looijaard <frodol@dds.nl>.  The LM77
8  * is a temperature sensor and thermal window comparator with 0.5 deg
9  * resolution made by National Semiconductor.  Complete datasheet can be
10  * obtained at their site:
11  *      http://www.national.com/pf/LM/LM77.html
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
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/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33
34 /* Addresses to scan */
35 static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
36                                                 I2C_CLIENT_END };
37
38 /* The LM77 registers */
39 #define LM77_REG_TEMP           0x00
40 #define LM77_REG_CONF           0x01
41 #define LM77_REG_TEMP_HYST      0x02
42 #define LM77_REG_TEMP_CRIT      0x03
43 #define LM77_REG_TEMP_MIN       0x04
44 #define LM77_REG_TEMP_MAX       0x05
45
46 /* Each client has this additional data */
47 struct lm77_data {
48         struct device           *hwmon_dev;
49         struct mutex            update_lock;
50         char                    valid;
51         unsigned long           last_updated;   /* In jiffies */
52         int                     temp_input;     /* Temperatures */
53         int                     temp_crit;
54         int                     temp_min;
55         int                     temp_max;
56         int                     temp_hyst;
57         u8                      alarms;
58 };
59
60 /* straight from the datasheet */
61 #define LM77_TEMP_MIN (-55000)
62 #define LM77_TEMP_MAX 125000
63
64 /*
65  * In the temperature registers, the low 3 bits are not part of the
66  * temperature values; they are the status bits.
67  */
68 static inline s16 LM77_TEMP_TO_REG(int temp)
69 {
70         int ntemp = clamp_val(temp, LM77_TEMP_MIN, LM77_TEMP_MAX);
71         return (ntemp / 500) * 8;
72 }
73
74 static inline int LM77_TEMP_FROM_REG(s16 reg)
75 {
76         return (reg / 8) * 500;
77 }
78
79 /*
80  * All registers are word-sized, except for the configuration register.
81  * The LM77 uses the high-byte first convention.
82  */
83 static u16 lm77_read_value(struct i2c_client *client, u8 reg)
84 {
85         if (reg == LM77_REG_CONF)
86                 return i2c_smbus_read_byte_data(client, reg);
87         else
88                 return i2c_smbus_read_word_swapped(client, reg);
89 }
90
91 static int lm77_write_value(struct i2c_client *client, u8 reg, u16 value)
92 {
93         if (reg == LM77_REG_CONF)
94                 return i2c_smbus_write_byte_data(client, reg, value);
95         else
96                 return i2c_smbus_write_word_swapped(client, reg, value);
97 }
98
99 static struct lm77_data *lm77_update_device(struct device *dev)
100 {
101         struct i2c_client *client = to_i2c_client(dev);
102         struct lm77_data *data = i2c_get_clientdata(client);
103
104         mutex_lock(&data->update_lock);
105
106         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
107             || !data->valid) {
108                 dev_dbg(&client->dev, "Starting lm77 update\n");
109                 data->temp_input =
110                         LM77_TEMP_FROM_REG(lm77_read_value(client,
111                                                            LM77_REG_TEMP));
112                 data->temp_hyst =
113                         LM77_TEMP_FROM_REG(lm77_read_value(client,
114                                                            LM77_REG_TEMP_HYST));
115                 data->temp_crit =
116                         LM77_TEMP_FROM_REG(lm77_read_value(client,
117                                                            LM77_REG_TEMP_CRIT));
118                 data->temp_min =
119                         LM77_TEMP_FROM_REG(lm77_read_value(client,
120                                                            LM77_REG_TEMP_MIN));
121                 data->temp_max =
122                         LM77_TEMP_FROM_REG(lm77_read_value(client,
123                                                            LM77_REG_TEMP_MAX));
124                 data->alarms =
125                         lm77_read_value(client, LM77_REG_TEMP) & 0x0007;
126                 data->last_updated = jiffies;
127                 data->valid = 1;
128         }
129
130         mutex_unlock(&data->update_lock);
131
132         return data;
133 }
134
135 /* sysfs stuff */
136
137 /* read routines for temperature limits */
138 #define show(value)     \
139 static ssize_t show_##value(struct device *dev,                 \
140                             struct device_attribute *attr,      \
141                             char *buf)                          \
142 {                                                               \
143         struct lm77_data *data = lm77_update_device(dev);       \
144         return sprintf(buf, "%d\n", data->value);               \
145 }
146
147 show(temp_input);
148 show(temp_crit);
149 show(temp_min);
150 show(temp_max);
151
152 /* read routines for hysteresis values */
153 static ssize_t show_temp_crit_hyst(struct device *dev,
154                                    struct device_attribute *attr, char *buf)
155 {
156         struct lm77_data *data = lm77_update_device(dev);
157         return sprintf(buf, "%d\n", data->temp_crit - data->temp_hyst);
158 }
159 static ssize_t show_temp_min_hyst(struct device *dev,
160                                   struct device_attribute *attr, char *buf)
161 {
162         struct lm77_data *data = lm77_update_device(dev);
163         return sprintf(buf, "%d\n", data->temp_min + data->temp_hyst);
164 }
165 static ssize_t show_temp_max_hyst(struct device *dev,
166                                   struct device_attribute *attr, char *buf)
167 {
168         struct lm77_data *data = lm77_update_device(dev);
169         return sprintf(buf, "%d\n", data->temp_max - data->temp_hyst);
170 }
171
172 /* write routines */
173 #define set(value, reg) \
174 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, \
175                            const char *buf, size_t count)               \
176 {                                                                       \
177         struct i2c_client *client = to_i2c_client(dev);                 \
178         struct lm77_data *data = i2c_get_clientdata(client);            \
179         long val;                                                       \
180         int err = kstrtol(buf, 10, &val);                               \
181         if (err)                                                        \
182                 return err;                                             \
183                                                                         \
184         mutex_lock(&data->update_lock);                                 \
185         data->value = val;                                              \
186         lm77_write_value(client, reg, LM77_TEMP_TO_REG(data->value));   \
187         mutex_unlock(&data->update_lock);                               \
188         return count;                                                   \
189 }
190
191 set(temp_min, LM77_REG_TEMP_MIN);
192 set(temp_max, LM77_REG_TEMP_MAX);
193
194 /*
195  * hysteresis is stored as a relative value on the chip, so it has to be
196  * converted first
197  */
198 static ssize_t set_temp_crit_hyst(struct device *dev,
199                                   struct device_attribute *attr,
200                                   const char *buf, size_t count)
201 {
202         struct i2c_client *client = to_i2c_client(dev);
203         struct lm77_data *data = i2c_get_clientdata(client);
204         unsigned long val;
205         int err;
206
207         err = kstrtoul(buf, 10, &val);
208         if (err)
209                 return err;
210
211         mutex_lock(&data->update_lock);
212         data->temp_hyst = data->temp_crit - val;
213         lm77_write_value(client, LM77_REG_TEMP_HYST,
214                          LM77_TEMP_TO_REG(data->temp_hyst));
215         mutex_unlock(&data->update_lock);
216         return count;
217 }
218
219 /* preserve hysteresis when setting T_crit */
220 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
221                              const char *buf, size_t count)
222 {
223         struct i2c_client *client = to_i2c_client(dev);
224         struct lm77_data *data = i2c_get_clientdata(client);
225         int oldcrithyst;
226         unsigned long val;
227         int err;
228
229         err = kstrtoul(buf, 10, &val);
230         if (err)
231                 return err;
232
233         mutex_lock(&data->update_lock);
234         oldcrithyst = data->temp_crit - data->temp_hyst;
235         data->temp_crit = val;
236         data->temp_hyst = data->temp_crit - oldcrithyst;
237         lm77_write_value(client, LM77_REG_TEMP_CRIT,
238                          LM77_TEMP_TO_REG(data->temp_crit));
239         lm77_write_value(client, LM77_REG_TEMP_HYST,
240                          LM77_TEMP_TO_REG(data->temp_hyst));
241         mutex_unlock(&data->update_lock);
242         return count;
243 }
244
245 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
246                           char *buf)
247 {
248         int bitnr = to_sensor_dev_attr(attr)->index;
249         struct lm77_data *data = lm77_update_device(dev);
250         return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
251 }
252
253 static DEVICE_ATTR(temp1_input, S_IRUGO,
254                    show_temp_input, NULL);
255 static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
256                    show_temp_crit, set_temp_crit);
257 static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
258                    show_temp_min, set_temp_min);
259 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
260                    show_temp_max, set_temp_max);
261
262 static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
263                    show_temp_crit_hyst, set_temp_crit_hyst);
264 static DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
265                    show_temp_min_hyst, NULL);
266 static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
267                    show_temp_max_hyst, NULL);
268
269 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 2);
270 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 0);
271 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 1);
272
273 static struct attribute *lm77_attributes[] = {
274         &dev_attr_temp1_input.attr,
275         &dev_attr_temp1_crit.attr,
276         &dev_attr_temp1_min.attr,
277         &dev_attr_temp1_max.attr,
278         &dev_attr_temp1_crit_hyst.attr,
279         &dev_attr_temp1_min_hyst.attr,
280         &dev_attr_temp1_max_hyst.attr,
281         &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
282         &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
283         &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
284         NULL
285 };
286
287 static const struct attribute_group lm77_group = {
288         .attrs = lm77_attributes,
289 };
290
291 /* Return 0 if detection is successful, -ENODEV otherwise */
292 static int lm77_detect(struct i2c_client *client, struct i2c_board_info *info)
293 {
294         struct i2c_adapter *adapter = client->adapter;
295         int i, cur, conf, hyst, crit, min, max;
296
297         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
298                                      I2C_FUNC_SMBUS_WORD_DATA))
299                 return -ENODEV;
300
301         /*
302          * Here comes the remaining detection.  Since the LM77 has no
303          * register dedicated to identification, we have to rely on the
304          * following tricks:
305          *
306          * 1. the high 4 bits represent the sign and thus they should
307          *    always be the same
308          * 2. the high 3 bits are unused in the configuration register
309          * 3. addresses 0x06 and 0x07 return the last read value
310          * 4. registers cycling over 8-address boundaries
311          *
312          * Word-sized registers are high-byte first.
313          */
314
315         /* addresses cycling */
316         cur = i2c_smbus_read_word_data(client, 0);
317         conf = i2c_smbus_read_byte_data(client, 1);
318         hyst = i2c_smbus_read_word_data(client, 2);
319         crit = i2c_smbus_read_word_data(client, 3);
320         min = i2c_smbus_read_word_data(client, 4);
321         max = i2c_smbus_read_word_data(client, 5);
322         for (i = 8; i <= 0xff; i += 8) {
323                 if (i2c_smbus_read_byte_data(client, i + 1) != conf
324                  || i2c_smbus_read_word_data(client, i + 2) != hyst
325                  || i2c_smbus_read_word_data(client, i + 3) != crit
326                  || i2c_smbus_read_word_data(client, i + 4) != min
327                  || i2c_smbus_read_word_data(client, i + 5) != max)
328                         return -ENODEV;
329         }
330
331         /* sign bits */
332         if (((cur & 0x00f0) != 0xf0 && (cur & 0x00f0) != 0x0)
333          || ((hyst & 0x00f0) != 0xf0 && (hyst & 0x00f0) != 0x0)
334          || ((crit & 0x00f0) != 0xf0 && (crit & 0x00f0) != 0x0)
335          || ((min & 0x00f0) != 0xf0 && (min & 0x00f0) != 0x0)
336          || ((max & 0x00f0) != 0xf0 && (max & 0x00f0) != 0x0))
337                 return -ENODEV;
338
339         /* unused bits */
340         if (conf & 0xe0)
341                 return -ENODEV;
342
343         /* 0x06 and 0x07 return the last read value */
344         cur = i2c_smbus_read_word_data(client, 0);
345         if (i2c_smbus_read_word_data(client, 6) != cur
346          || i2c_smbus_read_word_data(client, 7) != cur)
347                 return -ENODEV;
348         hyst = i2c_smbus_read_word_data(client, 2);
349         if (i2c_smbus_read_word_data(client, 6) != hyst
350          || i2c_smbus_read_word_data(client, 7) != hyst)
351                 return -ENODEV;
352         min = i2c_smbus_read_word_data(client, 4);
353         if (i2c_smbus_read_word_data(client, 6) != min
354          || i2c_smbus_read_word_data(client, 7) != min)
355                 return -ENODEV;
356
357         strlcpy(info->type, "lm77", I2C_NAME_SIZE);
358
359         return 0;
360 }
361
362 static void lm77_init_client(struct i2c_client *client)
363 {
364         /* Initialize the LM77 chip - turn off shutdown mode */
365         int conf = lm77_read_value(client, LM77_REG_CONF);
366         if (conf & 1)
367                 lm77_write_value(client, LM77_REG_CONF, conf & 0xfe);
368 }
369
370 static int lm77_probe(struct i2c_client *client, const struct i2c_device_id *id)
371 {
372         struct device *dev = &client->dev;
373         struct lm77_data *data;
374         int err;
375
376         data = devm_kzalloc(dev, sizeof(struct lm77_data), GFP_KERNEL);
377         if (!data)
378                 return -ENOMEM;
379
380         i2c_set_clientdata(client, data);
381         mutex_init(&data->update_lock);
382
383         /* Initialize the LM77 chip */
384         lm77_init_client(client);
385
386         /* Register sysfs hooks */
387         err = sysfs_create_group(&dev->kobj, &lm77_group);
388         if (err)
389                 return err;
390
391         data->hwmon_dev = hwmon_device_register(dev);
392         if (IS_ERR(data->hwmon_dev)) {
393                 err = PTR_ERR(data->hwmon_dev);
394                 goto exit_remove;
395         }
396
397         return 0;
398
399 exit_remove:
400         sysfs_remove_group(&dev->kobj, &lm77_group);
401         return err;
402 }
403
404 static int lm77_remove(struct i2c_client *client)
405 {
406         struct lm77_data *data = i2c_get_clientdata(client);
407         hwmon_device_unregister(data->hwmon_dev);
408         sysfs_remove_group(&client->dev.kobj, &lm77_group);
409         return 0;
410 }
411
412 static const struct i2c_device_id lm77_id[] = {
413         { "lm77", 0 },
414         { }
415 };
416 MODULE_DEVICE_TABLE(i2c, lm77_id);
417
418 /* This is the driver that will be inserted */
419 static struct i2c_driver lm77_driver = {
420         .class          = I2C_CLASS_HWMON,
421         .driver = {
422                 .name   = "lm77",
423         },
424         .probe          = lm77_probe,
425         .remove         = lm77_remove,
426         .id_table       = lm77_id,
427         .detect         = lm77_detect,
428         .address_list   = normal_i2c,
429 };
430
431 module_i2c_driver(lm77_driver);
432
433 MODULE_AUTHOR("Andras BALI <drewie@freemail.hu>");
434 MODULE_DESCRIPTION("LM77 driver");
435 MODULE_LICENSE("GPL");