Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
  3  *
  4  * Copyright (C) 2006 Corentin LABBE <corentin.labbe@geomatys.fr>
  5  *
  6  * Based on LM83 Driver by Jean Delvare <khali@linux-fr.org>
  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  * Insmod parameters
 48  */
 49 
 50 I2C_CLIENT_INSMOD_1(adm1029);
 51 
 52 /*
 53  * The ADM1029 registers
 54  * Manufacturer ID is 0x41 for Analog Devices
 55  */
 56 
 57 #define ADM1029_REG_MAN_ID                      0x0D
 58 #define ADM1029_REG_CHIP_ID                     0x0E
 59 #define ADM1029_REG_CONFIG                      0x01
 60 #define ADM1029_REG_NB_FAN_SUPPORT              0x02
 61 
 62 #define ADM1029_REG_TEMP_DEVICES_INSTALLED      0x06
 63 
 64 #define ADM1029_REG_LOCAL_TEMP                  0xA0
 65 #define ADM1029_REG_REMOTE1_TEMP                0xA1
 66 #define ADM1029_REG_REMOTE2_TEMP                0xA2
 67 
 68 #define ADM1029_REG_LOCAL_TEMP_HIGH             0x90
 69 #define ADM1029_REG_REMOTE1_TEMP_HIGH           0x91
 70 #define ADM1029_REG_REMOTE2_TEMP_HIGH           0x92
 71 
 72 #define ADM1029_REG_LOCAL_TEMP_LOW              0x98
 73 #define ADM1029_REG_REMOTE1_TEMP_LOW            0x99
 74 #define ADM1029_REG_REMOTE2_TEMP_LOW            0x9A
 75 
 76 #define ADM1029_REG_FAN1                        0x70
 77 #define ADM1029_REG_FAN2                        0x71
 78 
 79 #define ADM1029_REG_FAN1_MIN                    0x78
 80 #define ADM1029_REG_FAN2_MIN                    0x79
 81 
 82 #define ADM1029_REG_FAN1_CONFIG                 0x68
 83 #define ADM1029_REG_FAN2_CONFIG                 0x69
 84 
 85 #define TEMP_FROM_REG(val)      ((val) * 1000)
 86 
 87 #define DIV_FROM_REG(val)       ( 1 << (((val) >> 6) - 1))
 88 
 89 /* Registers to be checked by adm1029_update_device() */
 90 static const u8 ADM1029_REG_TEMP[] = {
 91         ADM1029_REG_LOCAL_TEMP,
 92         ADM1029_REG_REMOTE1_TEMP,
 93         ADM1029_REG_REMOTE2_TEMP,
 94         ADM1029_REG_LOCAL_TEMP_HIGH,
 95         ADM1029_REG_REMOTE1_TEMP_HIGH,
 96         ADM1029_REG_REMOTE2_TEMP_HIGH,
 97         ADM1029_REG_LOCAL_TEMP_LOW,
 98         ADM1029_REG_REMOTE1_TEMP_LOW,
 99         ADM1029_REG_REMOTE2_TEMP_LOW,
100 };
101 
102 static const u8 ADM1029_REG_FAN[] = {
103         ADM1029_REG_FAN1,
104         ADM1029_REG_FAN2,
105         ADM1029_REG_FAN1_MIN,
106         ADM1029_REG_FAN2_MIN,
107 };
108 
109 static const u8 ADM1029_REG_FAN_DIV[] = {
110         ADM1029_REG_FAN1_CONFIG,
111         ADM1029_REG_FAN2_CONFIG,
112 };
113 
114 /*
115  * Functions declaration
116  */
117 
118 static int adm1029_probe(struct i2c_client *client,
119                          const struct i2c_device_id *id);
120 static int adm1029_detect(struct i2c_client *client, int kind,
121                           struct i2c_board_info *info);
122 static int adm1029_remove(struct i2c_client *client);
123 static struct adm1029_data *adm1029_update_device(struct device *dev);
124 static int adm1029_init_client(struct i2c_client *client);
125 
126 /*
127  * Driver data (common to all clients)
128  */
129 
130 static const struct i2c_device_id adm1029_id[] = {
131         { "adm1029", adm1029 },
132         { }
133 };
134 MODULE_DEVICE_TABLE(i2c, adm1029_id);
135 
136 static struct i2c_driver adm1029_driver = {
137         .class          = I2C_CLASS_HWMON,
138         .driver = {
139                 .name = "adm1029",
140         },
141         .probe          = adm1029_probe,
142         .remove         = adm1029_remove,
143         .id_table       = adm1029_id,
144         .detect         = adm1029_detect,
145         .address_data   = &addr_data,
146 };
147 
148 /*
149  * Client data (each client gets its own)
150  */
151 
152 struct adm1029_data {
153         struct device *hwmon_dev;
154         struct mutex update_lock;
155         char valid;             /* zero until following fields are valid */
156         unsigned long last_updated;     /* in jiffies */
157 
158         /* registers values, signed for temperature, unsigned for other stuff */
159         s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)];
160         u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)];
161         u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)];
162 };
163 
164 /*
165  * Sysfs stuff
166  */
167 
168 static ssize_t
169 show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
170 {
171         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
172         struct adm1029_data *data = adm1029_update_device(dev);
173         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
174 }
175 
176 static ssize_t
177 show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
178 {
179         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
180         struct adm1029_data *data = adm1029_update_device(dev);
181         u16 val;
182         if (data->fan[attr->index] == 0
183             || (data->fan_div[attr->index] & 0xC0) == 0
184             || data->fan[attr->index] == 255) {
185                 return sprintf(buf, "\n");
186         }
187 
188         val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
189             / data->fan[attr->index];
190         return sprintf(buf, "%d\n", val);
191 }
192 
193 static ssize_t
194 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
195 {
196         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
197         struct adm1029_data *data = adm1029_update_device(dev);
198         if ((data->fan_div[attr->index] & 0xC0) == 0)
199                 return sprintf(buf, "\n");
200         return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
201 }
202 
203 static ssize_t set_fan_div(struct device *dev,
204             struct device_attribute *devattr, const char *buf, size_t count)
205 {
206         struct i2c_client *client = to_i2c_client(dev);
207         struct adm1029_data *data = i2c_get_clientdata(client);
208         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
209         long val = simple_strtol(buf, NULL, 10);
210         u8 reg;
211 
212         mutex_lock(&data->update_lock);
213 
214         /*Read actual config */
215         reg = i2c_smbus_read_byte_data(client,
216                                        ADM1029_REG_FAN_DIV[attr->index]);
217 
218         switch (val) {
219         case 1:
220                 val = 1;
221                 break;
222         case 2:
223                 val = 2;
224                 break;
225         case 4:
226                 val = 3;
227                 break;
228         default:
229                 mutex_unlock(&data->update_lock);
230                 dev_err(&client->dev, "fan_div value %ld not "
231                         "supported. Choose one of 1, 2 or 4!\n", val);
232                 return -EINVAL;
233         }
234         /* Update the value */
235         reg = (reg & 0x3F) | (val << 6);
236 
237         /* Write value */
238         i2c_smbus_write_byte_data(client,
239                                   ADM1029_REG_FAN_DIV[attr->index], reg);
240         mutex_unlock(&data->update_lock);
241 
242         return count;
243 }
244 
245 /*
246 Access rights on sysfs, S_IRUGO stand for Is Readable by User, Group and Others
247                         S_IWUSR stand for Is Writable by User
248 */
249 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
250 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
251 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
252 
253 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3);
254 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4);
255 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5);
256 
257 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6);
258 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7);
259 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8);
260 
261 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
262 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
263 
264 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2);
265 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3);
266 
267 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
268                           show_fan_div, set_fan_div, 0);
269 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR,
270                           show_fan_div, set_fan_div, 1);
271 
272 static struct attribute *adm1029_attributes[] = {
273         &sensor_dev_attr_temp1_input.dev_attr.attr,
274         &sensor_dev_attr_temp1_min.dev_attr.attr,
275         &sensor_dev_attr_temp1_max.dev_attr.attr,
276         &sensor_dev_attr_temp2_input.dev_attr.attr,
277         &sensor_dev_attr_temp2_min.dev_attr.attr,
278         &sensor_dev_attr_temp2_max.dev_attr.attr,
279         &sensor_dev_attr_temp3_input.dev_attr.attr,
280         &sensor_dev_attr_temp3_min.dev_attr.attr,
281         &sensor_dev_attr_temp3_max.dev_attr.attr,
282         &sensor_dev_attr_fan1_input.dev_attr.attr,
283         &sensor_dev_attr_fan2_input.dev_attr.attr,
284         &sensor_dev_attr_fan1_min.dev_attr.attr,
285         &sensor_dev_attr_fan2_min.dev_attr.attr,
286         &sensor_dev_attr_fan1_div.dev_attr.attr,
287         &sensor_dev_attr_fan2_div.dev_attr.attr,
288         NULL
289 };
290 
291 static const struct attribute_group adm1029_group = {
292         .attrs = adm1029_attributes,
293 };
294 
295 /*
296  * Real code
297  */
298 
299 /* Return 0 if detection is successful, -ENODEV otherwise */
300 static int adm1029_detect(struct i2c_client *client, int kind,
301                           struct i2c_board_info *info)
302 {
303         struct i2c_adapter *adapter = client->adapter;
304 
305         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
306                 return -ENODEV;
307 
308         /* Now we do the detection and identification. A negative kind
309          * means that the driver was loaded with no force parameter
310          * (default), so we must both detect and identify the chip
311          * (actually there is only one possible kind of chip for now, adm1029).
312          * A zero kind means that the driver was loaded with the force
313          * parameter, the detection step shall be skipped. A positive kind
314          * means that the driver was loaded with the force parameter and a
315          * given kind of chip is requested, so both the detection and the
316          * identification steps are skipped. */
317 
318         /* Default to an adm1029 if forced */
319         if (kind == 0)
320                 kind = adm1029;
321 
322         /* ADM1029 doesn't have CHIP ID, check just MAN ID
323          * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED,
324          * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values
325          * documented
326          */
327 
328         if (kind <= 0) {        /* identification */
329                 u8 man_id, chip_id, temp_devices_installed, nb_fan_support;
330 
331                 man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID);
332                 chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID);
333                 temp_devices_installed = i2c_smbus_read_byte_data(client,
334                                         ADM1029_REG_TEMP_DEVICES_INSTALLED);
335                 nb_fan_support = i2c_smbus_read_byte_data(client,
336                                                 ADM1029_REG_NB_FAN_SUPPORT);
337                 /* 0x41 is Analog Devices */
338                 if (man_id == 0x41 && (temp_devices_installed & 0xf9) == 0x01
339                     && nb_fan_support == 0x03) {
340                         if ((chip_id & 0xF0) == 0x00) {
341                                 kind = adm1029;
342                         } else {
343                                 /* There are no "official" CHIP ID, so actually
344                                  * we use Major/Minor revision for that */
345                                 printk(KERN_INFO
346                                        "adm1029: Unknown major revision %x, "
347                                        "please let us know\n", chip_id);
348                         }
349                 }
350 
351                 if (kind <= 0) {        /* identification failed */
352                         pr_debug("adm1029: Unsupported chip (man_id=0x%02X, "
353                                  "chip_id=0x%02X)\n", man_id, chip_id);
354                         return -ENODEV;
355                 }
356         }
357         strlcpy(info->type, "adm1029", I2C_NAME_SIZE);
358 
359         return 0;
360 }
361 
362 static int adm1029_probe(struct i2c_client *client,
363                          const struct i2c_device_id *id)
364 {
365         struct adm1029_data *data;
366         int err;
367 
368         data = kzalloc(sizeof(struct adm1029_data), GFP_KERNEL);
369         if (!data) {
370                 err = -ENOMEM;
371                 goto exit;
372         }
373 
374         i2c_set_clientdata(client, data);
375         mutex_init(&data->update_lock);
376 
377         /*
378          * Initialize the ADM1029 chip
379          * Check config register
380          */
381         if (adm1029_init_client(client) == 0) {
382                 err = -ENODEV;
383                 goto exit_free;
384         }
385 
386         /* Register sysfs hooks */
387         if ((err = sysfs_create_group(&client->dev.kobj, &adm1029_group)))
388                 goto exit_free;
389 
390         data->hwmon_dev = hwmon_device_register(&client->dev);
391         if (IS_ERR(data->hwmon_dev)) {
392                 err = PTR_ERR(data->hwmon_dev);
393                 goto exit_remove_files;
394         }
395 
396         return 0;
397 
398  exit_remove_files:
399         sysfs_remove_group(&client->dev.kobj, &adm1029_group);
400  exit_free:
401         kfree(data);
402  exit:
403         return err;
404 }
405 
406 static int adm1029_init_client(struct i2c_client *client)
407 {
408         u8 config;
409         config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
410         if ((config & 0x10) == 0) {
411                 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG,
412                                           config | 0x10);
413         }
414         /* recheck config */
415         config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG);
416         if ((config & 0x10) == 0) {
417                 dev_err(&client->dev, "Initialization failed!\n");
418                 return 0;
419         }
420         return 1;
421 }
422 
423 static int adm1029_remove(struct i2c_client *client)
424 {
425         struct adm1029_data *data = i2c_get_clientdata(client);
426 
427         hwmon_device_unregister(data->hwmon_dev);
428         sysfs_remove_group(&client->dev.kobj, &adm1029_group);
429 
430         kfree(data);
431         return 0;
432 }
433 
434 /*
435 function that update the status of the chips (temperature for exemple)
436 */
437 static struct adm1029_data *adm1029_update_device(struct device *dev)
438 {
439         struct i2c_client *client = to_i2c_client(dev);
440         struct adm1029_data *data = i2c_get_clientdata(client);
441 
442         mutex_lock(&data->update_lock);
443         /*
444          * Use the "cache" Luke, don't recheck values
445          * if there are already checked not a long time later
446          */
447         if (time_after(jiffies, data->last_updated + HZ * 2)
448          || !data->valid) {
449                 int nr;
450 
451                 dev_dbg(&client->dev, "Updating adm1029 data\n");
452 
453                 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) {
454                         data->temp[nr] =
455                             i2c_smbus_read_byte_data(client,
456                                                      ADM1029_REG_TEMP[nr]);
457                 }
458                 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) {
459                         data->fan[nr] =
460                             i2c_smbus_read_byte_data(client,
461                                                      ADM1029_REG_FAN[nr]);
462                 }
463                 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) {
464                         data->fan_div[nr] =
465                             i2c_smbus_read_byte_data(client,
466                                                      ADM1029_REG_FAN_DIV[nr]);
467                 }
468 
469                 data->last_updated = jiffies;
470                 data->valid = 1;
471         }
472 
473         mutex_unlock(&data->update_lock);
474 
475         return data;
476 }
477 
478 /*
479         Common module stuff
480 */
481 static int __init sensors_adm1029_init(void)
482 {
483 
484         return i2c_add_driver(&adm1029_driver);
485 }
486 
487 static void __exit sensors_adm1029_exit(void)
488 {
489 
490         i2c_del_driver(&adm1029_driver);
491 }
492 
493 MODULE_AUTHOR("Corentin LABBE <corentin.labbe@geomatys.fr>");
494 MODULE_DESCRIPTION("adm1029 driver");
495 MODULE_LICENSE("GPL v2");
496 
497 module_init(sensors_adm1029_init);
498 module_exit(sensors_adm1029_exit);
499 
  This page was automatically generated by the LXR engine.