Merge branch 'parisc-4.9-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[cascardo/linux.git] / drivers / hwmon / adm1029.c
1 /*
2  * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3  *
4  * Copyright (C) 2006 Corentin LABBE <clabbe.montjoie@gmail.com>
5  *
6  * Based on LM83 Driver by Jean Delvare <jdelvare@suse.de>
7  *
8  * Give only processor, motherboard temperatures and fan tachs
9  * Very rare chip please let me know if you use it
10  *
11  * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf
12  *
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation version 2 of the License
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  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/jiffies.h>
32 #include <linux/i2c.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/hwmon.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
37
38 /*
39  * Addresses to scan
40  */
41
42 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
43                                                 0x2e, 0x2f, I2C_CLIENT_END
44 };
45
46 /*
47  * The ADM1029 registers
48  * Manufacturer ID is 0x41 for Analog Devices
49  */
50
51 #define ADM1029_REG_MAN_ID                      0x0D
52 #define ADM1029_REG_CHIP_ID                     0x0E
53 #define ADM1029_REG_CONFIG                      0x01
54 #define ADM1029_REG_NB_FAN_SUPPORT              0x02
55
56 #define ADM1029_REG_TEMP_DEVICES_INSTALLED      0x06
57
58 #define ADM1029_REG_LOCAL_TEMP                  0xA0
59 #define ADM1029_REG_REMOTE1_TEMP                0xA1
60 #define ADM1029_REG_REMOTE2_TEMP                0xA2
61
62 #define ADM1029_REG_LOCAL_TEMP_HIGH             0x90
63 #define ADM1029_REG_REMOTE1_TEMP_HIGH           0x91
64 #define ADM1029_REG_REMOTE2_TEMP_HIGH           0x92
65
66 #define ADM1029_REG_LOCAL_TEMP_LOW              0x98
67 #define ADM1029_REG_REMOTE1_TEMP_LOW            0x99
68 #define ADM1029_REG_REMOTE2_TEMP_LOW            0x9A
69
70 #define ADM1029_REG_FAN1                        0x70
71 #define ADM1029_REG_FAN2                        0x71
72
73 #define ADM1029_REG_FAN1_MIN                    0x78
74 #define ADM1029_REG_FAN2_MIN                    0x79
75
76 #define ADM1029_REG_FAN1_CONFIG                 0x68
77 #define ADM1029_REG_FAN2_CONFIG                 0x69
78
79 #define TEMP_FROM_REG(val)      ((val) * 1000)
80
81 #define DIV_FROM_REG(val)       (1 << (((val) >> 6) - 1))
82
83 /* Registers to be checked by adm1029_update_device() */
84 static const u8 ADM1029_REG_TEMP[] = {
85         ADM1029_REG_LOCAL_TEMP,
86         ADM1029_REG_REMOTE1_TEMP,
87         ADM1029_REG_REMOTE2_TEMP,
88         ADM1029_REG_LOCAL_TEMP_HIGH,
89         ADM1029_REG_REMOTE1_TEMP_HIGH,
90         ADM1029_REG_REMOTE2_TEMP_HIGH,
91         ADM1029_REG_LOCAL_TEMP_LOW,
92         ADM1029_REG_REMOTE1_TEMP_LOW,
93         ADM1029_REG_REMOTE2_TEMP_LOW,
94 };
95
96 static const u8 ADM1029_REG_FAN[] = {
97         ADM1029_REG_FAN1,
98         ADM1029_REG_FAN2,
99         ADM1029_REG_FAN1_MIN,
100         ADM1029_REG_FAN2_MIN,
101 };
102
103 static const u8 ADM1029_REG_FAN_DIV[] = {
104         ADM1029_REG_FAN1_CONFIG,
105         ADM1029_REG_FAN2_CONFIG,
106 };
107
108 /*
109  * Client data (each client gets its own)
110  */
111
112 struct adm1029_data {
113         struct i2c_client *client;
114         struct mutex update_lock;
115         char valid;             /* zero until following fields are valid */
116         unsigned long last_updated;     /* in jiffies */
117
118         /* registers values, signed for temperature, unsigned for other stuff */
119         s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
120         u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
121         u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
122 };
123
124 /*
125  * function that update the status of the chips (temperature for example)
126  */
127 static struct adm1029_data *adm1029_update_device(struct device *dev)
128 {
129         struct adm1029_data *data = dev_get_drvdata(dev);
130         struct i2c_client *client = data->client;
131
132         mutex_lock(&data->update_lock);
133         /*
134          * Use the "cache" Luke, don't recheck values
135          * if there are already checked not a long time later
136          */
137         if (time_after(jiffies, data->last_updated + HZ * 2)
138          || !data->valid) {
139                 int nr;
140
141                 dev_dbg(&client->dev, "Updating adm1029 data\n");
142
143                 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
144                         data->temp[nr] =
145                             i2c_smbus_read_byte_data(client,
146                                                      ADM1029_REG_TEMP[nr]);
147                 }
148                 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
149                         data->fan[nr] =
150                             i2c_smbus_read_byte_data(client,
151                                                      ADM1029_REG_FAN[nr]);
152                 }
153                 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
154                         data->fan_div[nr] =
155                             i2c_smbus_read_byte_data(client,
156                                                      ADM1029_REG_FAN_DIV[nr]);
157                 }
158
159                 data->last_updated = jiffies;
160                 data->valid = 1;
161         }
162
163         mutex_unlock(&data->update_lock);
164
165         return data;
166 }
167
168 /*
169  * Sysfs stuff
170  */
171
172 static ssize_t
173 show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
174 {
175         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
176         struct adm1029_data *data = adm1029_update_device(dev);
177         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
178 }
179
180 static ssize_t
181 show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
182 {
183         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
184         struct adm1029_data *data = adm1029_update_device(dev);
185         u16 val;
186         if (data->fan[attr->index] == 0
187             || (data->fan_div[attr->index] & 0xC0) == 0
188             || data->fan[attr->index] == 255) {
189                 return sprintf(buf, "0\n");
190         }
191
192         val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
193             / data->fan[attr->index];
194         return sprintf(buf, "%d\n", val);
195 }
196
197 static ssize_t
198 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
199 {
200         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
201         struct adm1029_data *data = adm1029_update_device(dev);
202         if ((data->fan_div[attr->index] & 0xC0) == 0)
203                 return sprintf(buf, "0\n");
204         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
205 }
206
207 static ssize_t set_fan_div(struct device *dev,
208             struct device_attribute *devattr, const char *buf, size_t count)
209 {
210         struct adm1029_data *data = dev_get_drvdata(dev);
211         struct i2c_client *client = data->client;
212         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
213         u8 reg;
214         long val;
215         int ret = kstrtol(buf, 10, &val);
216         if (ret < 0)
217                 return ret;
218
219         mutex_lock(&data->update_lock);
220
221         /*Read actual config */
222         reg = i2c_smbus_read_byte_data(client,
223                                        ADM1029_REG_FAN_DIV[attr->index]);
224
225         switch (val) {
226         case 1:
227                 val = 1;
228                 break;
229         case 2:
230                 val = 2;
231                 break;
232         case 4:
233                 val = 3;
234                 break;
235         default:
236                 mutex_unlock(&data->update_lock);
237                 dev_err(&client->dev,
238                         "fan_div value %ld not supported. Choose one of 1, 2 or 4!\n",
239                         val);
240                 return -EINVAL;
241         }
242         /* Update the value */
243         reg = (reg & 0x3F) | (val << 6);
244
245         /* Update the cache */
246         data->fan_div[attr->index] = reg;
247
248         /* Write value */
249         i2c_smbus_write_byte_data(client,
250                                   ADM1029_REG_FAN_DIV[attr->index], reg);
251         mutex_unlock(&data->update_lock);
252
253         return count;
254 }
255
256 /*
257  * Access rights on sysfs. S_IRUGO: Is Readable by User, Group and Others
258  *                         S_IWUSR: Is Writable by User.
259  */
260 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
261 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
262 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
263
264 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
265 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
266 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
267
268 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
269 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
270 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
271
272 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
273 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
274
275 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
276 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
277
278 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
279                           show_fan_div, set_fan_div, 0);
280 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
281                           show_fan_div, set_fan_div, 1);
282
283 static struct attribute *adm1029_attrs[] = {
284         &sensor_dev_attr_temp1_input.dev_attr.attr,
285         &sensor_dev_attr_temp1_min.dev_attr.attr,
286         &sensor_dev_attr_temp1_max.dev_attr.attr,
287         &sensor_dev_attr_temp2_input.dev_attr.attr,
288         &sensor_dev_attr_temp2_min.dev_attr.attr,
289         &sensor_dev_attr_temp2_max.dev_attr.attr,
290         &sensor_dev_attr_temp3_input.dev_attr.attr,
291         &sensor_dev_attr_temp3_min.dev_attr.attr,
292         &sensor_dev_attr_temp3_max.dev_attr.attr,
293         &sensor_dev_attr_fan1_input.dev_attr.attr,
294         &sensor_dev_attr_fan2_input.dev_attr.attr,
295         &sensor_dev_attr_fan1_min.dev_attr.attr,
296         &sensor_dev_attr_fan2_min.dev_attr.attr,
297         &sensor_dev_attr_fan1_div.dev_attr.attr,
298         &sensor_dev_attr_fan2_div.dev_attr.attr,
299         NULL
300 };
301
302 ATTRIBUTE_GROUPS(adm1029);
303
304 /*
305  * Real code
306  */
307
308 /* Return 0 if detection is successful, -ENODEV otherwise */
309 static int adm1029_detect(struct i2c_client *client,
310                           struct i2c_board_info *info)
311 {
312         struct i2c_adapter *adapter = client->adapter;
313         u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
314
315         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
316                 return -ENODEV;
317
318         /*
319          * ADM1029 doesn't have CHIP ID, check just MAN ID
320          * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
321          * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
322          * documented
323          */
324
325         man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
326         chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
327         temp_devices_installed = i2c_smbus_read_byte_data(client,
328                                         ADM1029_REG_TEMP_DEVICES_INSTALLED);
329         nb_fan_support = i2c_smbus_read_byte_data(client,
330                                                 ADM1029_REG_NB_FAN_SUPPORT);
331         /* 0x41 is Analog Devices */
332         if (man_id != 0x41 || (temp_devices_installed & 0xf9) != 0x01
333             || nb_fan_support != 0x03)
334                 return -ENODEV;
335
336         if ((chip_id & 0xF0) != 0x00) {
337                 /*
338                  * There are no "official" CHIP ID, so actually
339                  * we use Major/Minor revision for that
340                  */
341                 pr_info("Unknown major revision %x, please let us know\n",
342                         chip_id);
343                 return -ENODEV;
344         }
345
346         strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
347
348         return 0;
349 }
350
351 static int adm1029_init_client(struct i2c_client *client)
352 {
353         u8 config;
354
355         config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
356         if ((config & 0x10) == 0) {
357                 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
358                                           config | 0x10);
359         }
360         /* recheck config */
361         config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
362         if ((config & 0x10) == 0) {
363                 dev_err(&client->dev, "Initialization failed!\n");
364                 return 0;
365         }
366         return 1;
367 }
368
369 static int adm1029_probe(struct i2c_client *client,
370                          const struct i2c_device_id *id)
371 {
372         struct device *dev = &client->dev;
373         struct adm1029_data *data;
374         struct device *hwmon_dev;
375
376         data = devm_kzalloc(dev, sizeof(struct adm1029_data), GFP_KERNEL);
377         if (!data)
378                 return -ENOMEM;
379
380         data->client = client;
381         mutex_init(&data->update_lock);
382
383         /*
384          * Initialize the ADM1029 chip
385          * Check config register
386          */
387         if (adm1029_init_client(client) == 0)
388                 return -ENODEV;
389
390         hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
391                                                            data,
392                                                            adm1029_groups);
393         return PTR_ERR_OR_ZERO(hwmon_dev);
394 }
395
396 static const struct i2c_device_id adm1029_id[] = {
397         { "adm1029", 0 },
398         { }
399 };
400 MODULE_DEVICE_TABLE(i2c, adm1029_id);
401
402 static struct i2c_driver adm1029_driver = {
403         .class          = I2C_CLASS_HWMON,
404         .driver = {
405                 .name = "adm1029",
406         },
407         .probe          = adm1029_probe,
408         .id_table       = adm1029_id,
409         .detect         = adm1029_detect,
410         .address_list   = normal_i2c,
411 };
412
413 module_i2c_driver(adm1029_driver);
414
415 MODULE_AUTHOR("Corentin LABBE <clabbe.montjoie@gmail.com>");
416 MODULE_DESCRIPTION("adm1029 driver");
417 MODULE_LICENSE("GPL v2");