Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / max6650.c
blob247bc14eb77b2d29ec0f97433604c956482270a6
1 /*
2 * max6650.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring.
5 * (C) 2007 by Hans J. Koch <hjk@linutronix.de>
7 * based on code written by John Morris <john.morris@spirentcom.com>
8 * Copyright (c) 2003 Spirent Communications
9 * and Claus Gindhart <claus.gindhart@kontron.com>
11 * This module has only been tested with the MAX6650 chip. It should
12 * also work with the MAX6651. It does not distinguish max6650 and max6651
13 * chips.
15 * Tha datasheet was last seen at:
17 * http://pdfserv.maxim-ic.com/en/ds/MAX6650-MAX6651.pdf
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/jiffies.h>
38 #include <linux/i2c.h>
39 #include <linux/hwmon.h>
40 #include <linux/hwmon-sysfs.h>
41 #include <linux/err.h>
44 * Addresses to scan. There are four disjoint possibilities, by pin config.
47 <<<<<<< HEAD:drivers/hwmon/max6650.c
48 static unsigned short normal_i2c[] = {0x1b, 0x1f, 0x48, 0x4b, I2C_CLIENT_END};
49 =======
50 static const unsigned short normal_i2c[] = {0x1b, 0x1f, 0x48, 0x4b,
51 I2C_CLIENT_END};
52 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/max6650.c
55 * Insmod parameters
58 /* fan_voltage: 5=5V fan, 12=12V fan, 0=don't change */
59 static int fan_voltage;
60 /* prescaler: Possible values are 1, 2, 4, 8, 16 or 0 for don't change */
61 static int prescaler;
62 /* clock: The clock frequency of the chip the driver should assume */
63 static int clock = 254000;
65 module_param(fan_voltage, int, S_IRUGO);
66 module_param(prescaler, int, S_IRUGO);
67 module_param(clock, int, S_IRUGO);
69 I2C_CLIENT_INSMOD_1(max6650);
72 * MAX 6650/6651 registers
75 #define MAX6650_REG_SPEED 0x00
76 #define MAX6650_REG_CONFIG 0x02
77 #define MAX6650_REG_GPIO_DEF 0x04
78 #define MAX6650_REG_DAC 0x06
79 #define MAX6650_REG_ALARM_EN 0x08
80 #define MAX6650_REG_ALARM 0x0A
81 #define MAX6650_REG_TACH0 0x0C
82 #define MAX6650_REG_TACH1 0x0E
83 #define MAX6650_REG_TACH2 0x10
84 #define MAX6650_REG_TACH3 0x12
85 #define MAX6650_REG_GPIO_STAT 0x14
86 #define MAX6650_REG_COUNT 0x16
89 * Config register bits
92 #define MAX6650_CFG_V12 0x08
93 #define MAX6650_CFG_PRESCALER_MASK 0x07
94 #define MAX6650_CFG_PRESCALER_2 0x01
95 #define MAX6650_CFG_PRESCALER_4 0x02
96 #define MAX6650_CFG_PRESCALER_8 0x03
97 #define MAX6650_CFG_PRESCALER_16 0x04
98 #define MAX6650_CFG_MODE_MASK 0x30
99 #define MAX6650_CFG_MODE_ON 0x00
100 #define MAX6650_CFG_MODE_OFF 0x10
101 #define MAX6650_CFG_MODE_CLOSED_LOOP 0x20
102 #define MAX6650_CFG_MODE_OPEN_LOOP 0x30
103 #define MAX6650_COUNT_MASK 0x03
105 /* Minimum and maximum values of the FAN-RPM */
106 #define FAN_RPM_MIN 240
107 #define FAN_RPM_MAX 30000
109 #define DIV_FROM_REG(reg) (1 << (reg & 7))
111 static int max6650_attach_adapter(struct i2c_adapter *adapter);
112 static int max6650_detect(struct i2c_adapter *adapter, int address, int kind);
113 static int max6650_init_client(struct i2c_client *client);
114 static int max6650_detach_client(struct i2c_client *client);
115 static struct max6650_data *max6650_update_device(struct device *dev);
118 * Driver data (common to all clients)
121 static struct i2c_driver max6650_driver = {
122 .driver = {
123 .name = "max6650",
125 .attach_adapter = max6650_attach_adapter,
126 .detach_client = max6650_detach_client,
130 * Client data (each client gets its own)
133 struct max6650_data
135 struct i2c_client client;
136 struct device *hwmon_dev;
137 struct mutex update_lock;
138 char valid; /* zero until following fields are valid */
139 unsigned long last_updated; /* in jiffies */
141 /* register values */
142 u8 speed;
143 u8 config;
144 u8 tach[4];
145 u8 count;
146 u8 dac;
149 static ssize_t get_fan(struct device *dev, struct device_attribute *devattr,
150 char *buf)
152 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
153 struct max6650_data *data = max6650_update_device(dev);
154 int rpm;
157 * Calculation details:
159 * Each tachometer counts over an interval given by the "count"
160 * register (0.25, 0.5, 1 or 2 seconds). This module assumes
161 * that the fans produce two pulses per revolution (this seems
162 * to be the most common).
165 rpm = ((data->tach[attr->index] * 120) / DIV_FROM_REG(data->count));
166 return sprintf(buf, "%d\n", rpm);
170 * Set the fan speed to the specified RPM (or read back the RPM setting).
171 * This works in closed loop mode only. Use pwm1 for open loop speed setting.
173 * The MAX6650/1 will automatically control fan speed when in closed loop
174 * mode.
176 * Assumptions:
178 * 1) The MAX6650/1 internal 254kHz clock frequency is set correctly. Use
179 * the clock module parameter if you need to fine tune this.
181 * 2) The prescaler (low three bits of the config register) has already
182 * been set to an appropriate value. Use the prescaler module parameter
183 * if your BIOS doesn't initialize the chip properly.
185 * The relevant equations are given on pages 21 and 22 of the datasheet.
187 * From the datasheet, the relevant equation when in regulation is:
189 * [fCLK / (128 x (KTACH + 1))] = 2 x FanSpeed / KSCALE
191 * where:
193 * fCLK is the oscillator frequency (either the 254kHz internal
194 * oscillator or the externally applied clock)
196 * KTACH is the value in the speed register
198 * FanSpeed is the speed of the fan in rps
200 * KSCALE is the prescaler value (1, 2, 4, 8, or 16)
202 * When reading, we need to solve for FanSpeed. When writing, we need to
203 * solve for KTACH.
205 * Note: this tachometer is completely separate from the tachometers
206 * used to measure the fan speeds. Only one fan's speed (fan1) is
207 * controlled.
210 static ssize_t get_target(struct device *dev, struct device_attribute *devattr,
211 char *buf)
213 struct max6650_data *data = max6650_update_device(dev);
214 int kscale, ktach, rpm;
217 * Use the datasheet equation:
219 * FanSpeed = KSCALE x fCLK / [256 x (KTACH + 1)]
221 * then multiply by 60 to give rpm.
224 kscale = DIV_FROM_REG(data->config);
225 ktach = data->speed;
226 rpm = 60 * kscale * clock / (256 * (ktach + 1));
227 return sprintf(buf, "%d\n", rpm);
230 static ssize_t set_target(struct device *dev, struct device_attribute *devattr,
231 const char *buf, size_t count)
233 struct i2c_client *client = to_i2c_client(dev);
234 struct max6650_data *data = i2c_get_clientdata(client);
235 int rpm = simple_strtoul(buf, NULL, 10);
236 int kscale, ktach;
238 rpm = SENSORS_LIMIT(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
241 * Divide the required speed by 60 to get from rpm to rps, then
242 * use the datasheet equation:
244 * KTACH = [(fCLK x KSCALE) / (256 x FanSpeed)] - 1
247 mutex_lock(&data->update_lock);
249 kscale = DIV_FROM_REG(data->config);
250 ktach = ((clock * kscale) / (256 * rpm / 60)) - 1;
251 if (ktach < 0)
252 ktach = 0;
253 if (ktach > 255)
254 ktach = 255;
255 data->speed = ktach;
257 i2c_smbus_write_byte_data(client, MAX6650_REG_SPEED, data->speed);
259 mutex_unlock(&data->update_lock);
261 return count;
265 * Get/set the fan speed in open loop mode using pwm1 sysfs file.
266 * Speed is given as a relative value from 0 to 255, where 255 is maximum
267 * speed. Note that this is done by writing directly to the chip's DAC,
268 * it won't change the closed loop speed set by fan1_target.
269 * Also note that due to rounding errors it is possible that you don't read
270 * back exactly the value you have set.
273 static ssize_t get_pwm(struct device *dev, struct device_attribute *devattr,
274 char *buf)
276 int pwm;
277 struct max6650_data *data = max6650_update_device(dev);
279 /* Useful range for dac is 0-180 for 12V fans and 0-76 for 5V fans.
280 Lower DAC values mean higher speeds. */
281 if (data->config & MAX6650_CFG_V12)
282 pwm = 255 - (255 * (int)data->dac)/180;
283 else
284 pwm = 255 - (255 * (int)data->dac)/76;
286 if (pwm < 0)
287 pwm = 0;
289 return sprintf(buf, "%d\n", pwm);
292 static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
293 const char *buf, size_t count)
295 struct i2c_client *client = to_i2c_client(dev);
296 struct max6650_data *data = i2c_get_clientdata(client);
297 int pwm = simple_strtoul(buf, NULL, 10);
299 pwm = SENSORS_LIMIT(pwm, 0, 255);
301 mutex_lock(&data->update_lock);
303 if (data->config & MAX6650_CFG_V12)
304 data->dac = 180 - (180 * pwm)/255;
305 else
306 data->dac = 76 - (76 * pwm)/255;
308 i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac);
310 mutex_unlock(&data->update_lock);
312 return count;
316 * Get/Set controller mode:
317 * Possible values:
318 * 0 = Fan always on
319 * 1 = Open loop, Voltage is set according to speed, not regulated.
320 * 2 = Closed loop, RPM for all fans regulated by fan1 tachometer
323 static ssize_t get_enable(struct device *dev, struct device_attribute *devattr,
324 char *buf)
326 struct max6650_data *data = max6650_update_device(dev);
327 int mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4;
328 int sysfs_modes[4] = {0, 1, 2, 1};
330 return sprintf(buf, "%d\n", sysfs_modes[mode]);
333 static ssize_t set_enable(struct device *dev, struct device_attribute *devattr,
334 const char *buf, size_t count)
336 struct i2c_client *client = to_i2c_client(dev);
337 struct max6650_data *data = i2c_get_clientdata(client);
338 int mode = simple_strtoul(buf, NULL, 10);
339 int max6650_modes[3] = {0, 3, 2};
341 if ((mode < 0)||(mode > 2)) {
342 dev_err(&client->dev,
343 "illegal value for pwm1_enable (%d)\n", mode);
344 return -EINVAL;
347 mutex_lock(&data->update_lock);
349 data->config = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
350 data->config = (data->config & ~MAX6650_CFG_MODE_MASK)
351 | (max6650_modes[mode] << 4);
353 i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, data->config);
355 mutex_unlock(&data->update_lock);
357 return count;
361 * Read/write functions for fan1_div sysfs file. The MAX6650 has no such
362 * divider. We handle this by converting between divider and counttime:
364 * (counttime == k) <==> (divider == 2^k), k = 0, 1, 2, or 3
366 * Lower values of k allow to connect a faster fan without the risk of
367 * counter overflow. The price is lower resolution. You can also set counttime
368 * using the module parameter. Note that the module parameter "prescaler" also
369 * influences the behaviour. Unfortunately, there's no sysfs attribute
370 * defined for that. See the data sheet for details.
373 static ssize_t get_div(struct device *dev, struct device_attribute *devattr,
374 char *buf)
376 struct max6650_data *data = max6650_update_device(dev);
378 return sprintf(buf, "%d\n", DIV_FROM_REG(data->count));
381 static ssize_t set_div(struct device *dev, struct device_attribute *devattr,
382 const char *buf, size_t count)
384 struct i2c_client *client = to_i2c_client(dev);
385 struct max6650_data *data = i2c_get_clientdata(client);
386 int div = simple_strtoul(buf, NULL, 10);
388 mutex_lock(&data->update_lock);
389 switch (div) {
390 case 1:
391 data->count = 0;
392 break;
393 case 2:
394 data->count = 1;
395 break;
396 case 4:
397 data->count = 2;
398 break;
399 case 8:
400 data->count = 3;
401 break;
402 default:
403 dev_err(&client->dev,
404 "illegal value for fan divider (%d)\n", div);
405 return -EINVAL;
408 i2c_smbus_write_byte_data(client, MAX6650_REG_COUNT, data->count);
409 mutex_unlock(&data->update_lock);
411 return count;
414 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
415 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
416 static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
417 static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
418 static DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO, get_target, set_target);
419 static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, get_div, set_div);
420 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, get_enable, set_enable);
421 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm);
424 static struct attribute *max6650_attrs[] = {
425 &sensor_dev_attr_fan1_input.dev_attr.attr,
426 &sensor_dev_attr_fan2_input.dev_attr.attr,
427 &sensor_dev_attr_fan3_input.dev_attr.attr,
428 &sensor_dev_attr_fan4_input.dev_attr.attr,
429 &dev_attr_fan1_target.attr,
430 &dev_attr_fan1_div.attr,
431 &dev_attr_pwm1_enable.attr,
432 &dev_attr_pwm1.attr,
433 NULL
436 static struct attribute_group max6650_attr_grp = {
437 .attrs = max6650_attrs,
441 * Real code
444 static int max6650_attach_adapter(struct i2c_adapter *adapter)
446 if (!(adapter->class & I2C_CLASS_HWMON)) {
447 dev_dbg(&adapter->dev,
448 "FATAL: max6650_attach_adapter class HWMON not set\n");
449 return 0;
452 return i2c_probe(adapter, &addr_data, max6650_detect);
456 * The following function does more than just detection. If detection
457 * succeeds, it also registers the new chip.
460 static int max6650_detect(struct i2c_adapter *adapter, int address, int kind)
462 struct i2c_client *client;
463 struct max6650_data *data;
464 int err = -ENODEV;
466 dev_dbg(&adapter->dev, "max6650_detect called, kind = %d\n", kind);
468 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
469 dev_dbg(&adapter->dev, "max6650: I2C bus doesn't support "
470 "byte read mode, skipping.\n");
471 return 0;
474 if (!(data = kzalloc(sizeof(struct max6650_data), GFP_KERNEL))) {
475 dev_err(&adapter->dev, "max6650: out of memory.\n");
476 return -ENOMEM;
479 client = &data->client;
480 i2c_set_clientdata(client, data);
481 client->addr = address;
482 client->adapter = adapter;
483 client->driver = &max6650_driver;
486 * Now we do the remaining detection. A negative kind means that
487 * the driver was loaded with no force parameter (default), so we
488 * must both detect and identify the chip (actually there is only
489 * one possible kind of chip for now, max6650). A zero kind means that
490 * the driver was loaded with the force parameter, the detection
491 * step shall be skipped. A positive kind means that the driver
492 * was loaded with the force parameter and a given kind of chip is
493 * requested, so both the detection and the identification steps
494 * are skipped.
496 * Currently I can find no way to distinguish between a MAX6650 and
497 * a MAX6651. This driver has only been tried on the former.
500 if ((kind < 0) &&
501 ( (i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG) & 0xC0)
502 ||(i2c_smbus_read_byte_data(client, MAX6650_REG_GPIO_STAT) & 0xE0)
503 ||(i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN) & 0xE0)
504 ||(i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM) & 0xE0)
505 ||(i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT) & 0xFC))) {
506 dev_dbg(&adapter->dev,
507 "max6650: detection failed at 0x%02x.\n", address);
508 goto err_free;
511 dev_info(&adapter->dev, "max6650: chip found at 0x%02x.\n", address);
513 strlcpy(client->name, "max6650", I2C_NAME_SIZE);
514 mutex_init(&data->update_lock);
516 if ((err = i2c_attach_client(client))) {
517 dev_err(&adapter->dev, "max6650: failed to attach client.\n");
518 goto err_free;
522 * Initialize the max6650 chip
524 if (max6650_init_client(client))
525 goto err_detach;
527 err = sysfs_create_group(&client->dev.kobj, &max6650_attr_grp);
528 if (err)
529 goto err_detach;
531 data->hwmon_dev = hwmon_device_register(&client->dev);
532 if (!IS_ERR(data->hwmon_dev))
533 return 0;
535 err = PTR_ERR(data->hwmon_dev);
536 dev_err(&client->dev, "error registering hwmon device.\n");
537 sysfs_remove_group(&client->dev.kobj, &max6650_attr_grp);
538 err_detach:
539 i2c_detach_client(client);
540 err_free:
541 kfree(data);
542 return err;
545 static int max6650_detach_client(struct i2c_client *client)
547 struct max6650_data *data = i2c_get_clientdata(client);
548 int err;
550 sysfs_remove_group(&client->dev.kobj, &max6650_attr_grp);
551 hwmon_device_unregister(data->hwmon_dev);
552 err = i2c_detach_client(client);
553 if (!err)
554 kfree(data);
555 return err;
558 static int max6650_init_client(struct i2c_client *client)
560 struct max6650_data *data = i2c_get_clientdata(client);
561 int config;
562 int err = -EIO;
564 config = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
566 if (config < 0) {
567 dev_err(&client->dev, "Error reading config, aborting.\n");
568 return err;
571 switch (fan_voltage) {
572 case 0:
573 break;
574 case 5:
575 config &= ~MAX6650_CFG_V12;
576 break;
577 case 12:
578 config |= MAX6650_CFG_V12;
579 break;
580 default:
581 dev_err(&client->dev,
582 "illegal value for fan_voltage (%d)\n",
583 fan_voltage);
586 dev_info(&client->dev, "Fan voltage is set to %dV.\n",
587 (config & MAX6650_CFG_V12) ? 12 : 5);
589 switch (prescaler) {
590 case 0:
591 break;
592 case 1:
593 config &= ~MAX6650_CFG_PRESCALER_MASK;
594 break;
595 case 2:
596 config = (config & ~MAX6650_CFG_PRESCALER_MASK)
597 | MAX6650_CFG_PRESCALER_2;
598 break;
599 case 4:
600 config = (config & ~MAX6650_CFG_PRESCALER_MASK)
601 | MAX6650_CFG_PRESCALER_4;
602 break;
603 case 8:
604 config = (config & ~MAX6650_CFG_PRESCALER_MASK)
605 | MAX6650_CFG_PRESCALER_8;
606 break;
607 case 16:
608 config = (config & ~MAX6650_CFG_PRESCALER_MASK)
609 | MAX6650_CFG_PRESCALER_16;
610 break;
611 default:
612 dev_err(&client->dev,
613 "illegal value for prescaler (%d)\n",
614 prescaler);
617 dev_info(&client->dev, "Prescaler is set to %d.\n",
618 1 << (config & MAX6650_CFG_PRESCALER_MASK));
620 /* If mode is set to "full off", we change it to "open loop" and
621 * set DAC to 255, which has the same effect. We do this because
622 * there's no "full off" mode defined in hwmon specifcations.
625 if ((config & MAX6650_CFG_MODE_MASK) == MAX6650_CFG_MODE_OFF) {
626 dev_dbg(&client->dev, "Change mode to open loop, full off.\n");
627 config = (config & ~MAX6650_CFG_MODE_MASK)
628 | MAX6650_CFG_MODE_OPEN_LOOP;
629 if (i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, 255)) {
630 dev_err(&client->dev, "DAC write error, aborting.\n");
631 return err;
635 if (i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, config)) {
636 dev_err(&client->dev, "Config write error, aborting.\n");
637 return err;
640 data->config = config;
641 data->count = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT);
643 return 0;
646 static const u8 tach_reg[] = {
647 MAX6650_REG_TACH0,
648 MAX6650_REG_TACH1,
649 MAX6650_REG_TACH2,
650 MAX6650_REG_TACH3,
653 static struct max6650_data *max6650_update_device(struct device *dev)
655 int i;
656 struct i2c_client *client = to_i2c_client(dev);
657 struct max6650_data *data = i2c_get_clientdata(client);
659 mutex_lock(&data->update_lock);
661 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
662 data->speed = i2c_smbus_read_byte_data(client,
663 MAX6650_REG_SPEED);
664 data->config = i2c_smbus_read_byte_data(client,
665 MAX6650_REG_CONFIG);
666 for (i = 0; i < 4; i++) {
667 data->tach[i] = i2c_smbus_read_byte_data(client,
668 tach_reg[i]);
670 data->count = i2c_smbus_read_byte_data(client,
671 MAX6650_REG_COUNT);
672 data->dac = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC);
674 data->last_updated = jiffies;
675 data->valid = 1;
678 mutex_unlock(&data->update_lock);
680 return data;
683 static int __init sensors_max6650_init(void)
685 return i2c_add_driver(&max6650_driver);
688 static void __exit sensors_max6650_exit(void)
690 i2c_del_driver(&max6650_driver);
693 MODULE_AUTHOR("Hans J. Koch");
694 MODULE_DESCRIPTION("MAX6650 sensor driver");
695 MODULE_LICENSE("GPL");
697 module_init(sensors_max6650_init);
698 module_exit(sensors_max6650_exit);