2 amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
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.
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.
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.
22 #include <linux/kernel.h> /* Needed for KERN_INFO */
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/jiffies.h>
27 #include <linux/i2c.h>
28 #include <linux/hwmon.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <linux/err.h>
31 #include <linux/mutex.h>
38 static const unsigned short normal_i2c
[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
39 0x4c, 0x4d, 0x4e, I2C_CLIENT_END
};
47 static int pwminv
= 0; /*Inverted PWM output. */
48 module_param(pwminv
, int, S_IRUGO
);
50 static int init
= 1; /*Power-on initialization.*/
51 module_param(init
, int, S_IRUGO
);
54 I2C_CLIENT_INSMOD_1(amc6821
);
56 #define AMC6821_REG_DEV_ID 0x3D
57 #define AMC6821_REG_COMP_ID 0x3E
58 #define AMC6821_REG_CONF1 0x00
59 #define AMC6821_REG_CONF2 0x01
60 #define AMC6821_REG_CONF3 0x3F
61 #define AMC6821_REG_CONF4 0x04
62 #define AMC6821_REG_STAT1 0x02
63 #define AMC6821_REG_STAT2 0x03
64 #define AMC6821_REG_TDATA_LOW 0x08
65 #define AMC6821_REG_TDATA_HI 0x09
66 #define AMC6821_REG_LTEMP_HI 0x0A
67 #define AMC6821_REG_RTEMP_HI 0x0B
68 #define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
69 #define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
70 #define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
71 #define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
72 #define AMC6821_REG_LTEMP_CRIT 0x1B
73 #define AMC6821_REG_RTEMP_CRIT 0x1D
74 #define AMC6821_REG_PSV_TEMP 0x1C
75 #define AMC6821_REG_DCY 0x22
76 #define AMC6821_REG_LTEMP_FAN_CTRL 0x24
77 #define AMC6821_REG_RTEMP_FAN_CTRL 0x25
78 #define AMC6821_REG_DCY_LOW_TEMP 0x21
80 #define AMC6821_REG_TACH_LLIMITL 0x10
81 #define AMC6821_REG_TACH_LLIMITH 0x11
82 #define AMC6821_REG_TACH_HLIMITL 0x12
83 #define AMC6821_REG_TACH_HLIMITH 0x13
85 #define AMC6821_CONF1_START 0x01
86 #define AMC6821_CONF1_FAN_INT_EN 0x02
87 #define AMC6821_CONF1_FANIE 0x04
88 #define AMC6821_CONF1_PWMINV 0x08
89 #define AMC6821_CONF1_FAN_FAULT_EN 0x10
90 #define AMC6821_CONF1_FDRC0 0x20
91 #define AMC6821_CONF1_FDRC1 0x40
92 #define AMC6821_CONF1_THERMOVIE 0x80
94 #define AMC6821_CONF2_PWM_EN 0x01
95 #define AMC6821_CONF2_TACH_MODE 0x02
96 #define AMC6821_CONF2_TACH_EN 0x04
97 #define AMC6821_CONF2_RTFIE 0x08
98 #define AMC6821_CONF2_LTOIE 0x10
99 #define AMC6821_CONF2_RTOIE 0x20
100 #define AMC6821_CONF2_PSVIE 0x40
101 #define AMC6821_CONF2_RST 0x80
103 #define AMC6821_CONF3_THERM_FAN_EN 0x80
104 #define AMC6821_CONF3_REV_MASK 0x0F
106 #define AMC6821_CONF4_OVREN 0x10
107 #define AMC6821_CONF4_TACH_FAST 0x20
108 #define AMC6821_CONF4_PSPR 0x40
109 #define AMC6821_CONF4_MODE 0x80
111 #define AMC6821_STAT1_RPM_ALARM 0x01
112 #define AMC6821_STAT1_FANS 0x02
113 #define AMC6821_STAT1_RTH 0x04
114 #define AMC6821_STAT1_RTL 0x08
115 #define AMC6821_STAT1_R_THERM 0x10
116 #define AMC6821_STAT1_RTF 0x20
117 #define AMC6821_STAT1_LTH 0x40
118 #define AMC6821_STAT1_LTL 0x80
120 #define AMC6821_STAT2_RTC 0x08
121 #define AMC6821_STAT2_LTC 0x10
122 #define AMC6821_STAT2_LPSV 0x20
123 #define AMC6821_STAT2_L_THERM 0x40
124 #define AMC6821_STAT2_THERM_IN 0x80
126 enum {IDX_TEMP1_INPUT
= 0, IDX_TEMP1_MIN
, IDX_TEMP1_MAX
,
127 IDX_TEMP1_CRIT
, IDX_TEMP2_INPUT
, IDX_TEMP2_MIN
,
128 IDX_TEMP2_MAX
, IDX_TEMP2_CRIT
,
131 static const u8 temp_reg
[] = {AMC6821_REG_LTEMP_HI
,
132 AMC6821_REG_LTEMP_LIMIT_MIN
,
133 AMC6821_REG_LTEMP_LIMIT_MAX
,
134 AMC6821_REG_LTEMP_CRIT
,
135 AMC6821_REG_RTEMP_HI
,
136 AMC6821_REG_RTEMP_LIMIT_MIN
,
137 AMC6821_REG_RTEMP_LIMIT_MAX
,
138 AMC6821_REG_RTEMP_CRIT
, };
140 enum {IDX_FAN1_INPUT
= 0, IDX_FAN1_MIN
, IDX_FAN1_MAX
,
143 static const u8 fan_reg_low
[] = {AMC6821_REG_TDATA_LOW
,
144 AMC6821_REG_TACH_LLIMITL
,
145 AMC6821_REG_TACH_HLIMITL
, };
148 static const u8 fan_reg_hi
[] = {AMC6821_REG_TDATA_HI
,
149 AMC6821_REG_TACH_LLIMITH
,
150 AMC6821_REG_TACH_HLIMITH
, };
152 static int amc6821_probe(
153 struct i2c_client
*client
,
154 const struct i2c_device_id
*id
);
155 static int amc6821_detect(
156 struct i2c_client
*client
,
158 struct i2c_board_info
*info
);
159 static int amc6821_init_client(struct i2c_client
*client
);
160 static int amc6821_remove(struct i2c_client
*client
);
161 static struct amc6821_data
*amc6821_update_device(struct device
*dev
);
164 * Driver data (common to all clients)
167 static const struct i2c_device_id amc6821_id
[] = {
168 { "amc6821", amc6821
},
172 MODULE_DEVICE_TABLE(i2c
, amc6821_id
);
174 static struct i2c_driver amc6821_driver
= {
175 .class = I2C_CLASS_HWMON
,
179 .probe
= amc6821_probe
,
180 .remove
= amc6821_remove
,
181 .id_table
= amc6821_id
,
182 .detect
= amc6821_detect
,
183 .address_data
= &addr_data
,
188 * Client data (each client gets its own)
191 struct amc6821_data
{
192 struct device
*hwmon_dev
;
193 struct mutex update_lock
;
194 char valid
; /* zero until following fields are valid */
195 unsigned long last_updated
; /* in jiffies */
197 /* register values */
198 int temp
[TEMP_IDX_LEN
];
200 u16 fan
[FAN1_IDX_LEN
];
204 u8 temp1_auto_point_temp
[3];
205 u8 temp2_auto_point_temp
[3];
206 u8 pwm1_auto_point_pwm
[3];
208 u8 pwm1_auto_channels_temp
;
215 static ssize_t
get_temp(
217 struct device_attribute
*devattr
,
220 struct amc6821_data
*data
= amc6821_update_device(dev
);
221 int ix
= to_sensor_dev_attr(devattr
)->index
;
223 return sprintf(buf
, "%d\n", data
->temp
[ix
] * 1000);
228 static ssize_t
set_temp(
230 struct device_attribute
*attr
,
234 struct i2c_client
*client
= to_i2c_client(dev
);
235 struct amc6821_data
*data
= i2c_get_clientdata(client
);
236 int ix
= to_sensor_dev_attr(attr
)->index
;
239 int ret
= strict_strtol(buf
, 10, &val
);
242 val
= SENSORS_LIMIT(val
/ 1000, -128, 127);
244 mutex_lock(&data
->update_lock
);
245 data
->temp
[ix
] = val
;
246 if (i2c_smbus_write_byte_data(client
, temp_reg
[ix
], data
->temp
[ix
])) {
247 dev_err(&client
->dev
, "Register write error, aborting.\n");
250 mutex_unlock(&data
->update_lock
);
257 static ssize_t
get_temp_alarm(
259 struct device_attribute
*devattr
,
262 struct amc6821_data
*data
= amc6821_update_device(dev
);
263 int ix
= to_sensor_dev_attr(devattr
)->index
;
268 flag
= data
->stat1
& AMC6821_STAT1_LTL
;
271 flag
= data
->stat1
& AMC6821_STAT1_LTH
;
274 flag
= data
->stat2
& AMC6821_STAT2_LTC
;
277 flag
= data
->stat1
& AMC6821_STAT1_RTL
;
280 flag
= data
->stat1
& AMC6821_STAT1_RTH
;
283 flag
= data
->stat2
& AMC6821_STAT2_RTC
;
286 dev_dbg(dev
, "Unknown attr->index (%d).\n", ix
);
290 return sprintf(buf
, "1");
292 return sprintf(buf
, "0");
298 static ssize_t
get_temp2_fault(
300 struct device_attribute
*devattr
,
303 struct amc6821_data
*data
= amc6821_update_device(dev
);
304 if (data
->stat1
& AMC6821_STAT1_RTF
)
305 return sprintf(buf
, "1");
307 return sprintf(buf
, "0");
310 static ssize_t
get_pwm1(
312 struct device_attribute
*devattr
,
315 struct amc6821_data
*data
= amc6821_update_device(dev
);
316 return sprintf(buf
, "%d\n", data
->pwm1
);
319 static ssize_t
set_pwm1(
321 struct device_attribute
*devattr
,
325 struct i2c_client
*client
= to_i2c_client(dev
);
326 struct amc6821_data
*data
= i2c_get_clientdata(client
);
328 int ret
= strict_strtol(buf
, 10, &val
);
332 mutex_lock(&data
->update_lock
);
333 data
->pwm1
= SENSORS_LIMIT(val
, 0, 255);
334 i2c_smbus_write_byte_data(client
, AMC6821_REG_DCY
, data
->pwm1
);
335 mutex_unlock(&data
->update_lock
);
339 static ssize_t
get_pwm1_enable(
341 struct device_attribute
*devattr
,
344 struct amc6821_data
*data
= amc6821_update_device(dev
);
345 return sprintf(buf
, "%d\n", data
->pwm1_enable
);
348 static ssize_t
set_pwm1_enable(
350 struct device_attribute
*attr
,
354 struct i2c_client
*client
= to_i2c_client(dev
);
355 struct amc6821_data
*data
= i2c_get_clientdata(client
);
357 int config
= strict_strtol(buf
, 10, &val
);
361 config
= i2c_smbus_read_byte_data(client
, AMC6821_REG_CONF1
);
363 dev_err(&client
->dev
,
364 "Error reading configuration register, aborting.\n");
370 config
&= ~AMC6821_CONF1_FDRC0
;
371 config
&= ~AMC6821_CONF1_FDRC1
;
374 config
&= ~AMC6821_CONF1_FDRC0
;
375 config
|= AMC6821_CONF1_FDRC1
;
378 config
|= AMC6821_CONF1_FDRC0
;
379 config
|= AMC6821_CONF1_FDRC1
;
384 mutex_lock(&data
->update_lock
);
385 if (i2c_smbus_write_byte_data(client
, AMC6821_REG_CONF1
, config
)) {
386 dev_err(&client
->dev
,
387 "Configuration register write error, aborting.\n");
390 mutex_unlock(&data
->update_lock
);
395 static ssize_t
get_pwm1_auto_channels_temp(
397 struct device_attribute
*devattr
,
400 struct amc6821_data
*data
= amc6821_update_device(dev
);
401 return sprintf(buf
, "%d\n", data
->pwm1_auto_channels_temp
);
405 static ssize_t
get_temp_auto_point_temp(
407 struct device_attribute
*devattr
,
410 int ix
= to_sensor_dev_attr_2(devattr
)->index
;
411 int nr
= to_sensor_dev_attr_2(devattr
)->nr
;
412 struct amc6821_data
*data
= amc6821_update_device(dev
);
415 return sprintf(buf
, "%d\n",
416 data
->temp1_auto_point_temp
[ix
] * 1000);
419 return sprintf(buf
, "%d\n",
420 data
->temp2_auto_point_temp
[ix
] * 1000);
423 dev_dbg(dev
, "Unknown attr->nr (%d).\n", nr
);
429 static ssize_t
get_pwm1_auto_point_pwm(
431 struct device_attribute
*devattr
,
434 int ix
= to_sensor_dev_attr(devattr
)->index
;
435 struct amc6821_data
*data
= amc6821_update_device(dev
);
436 return sprintf(buf
, "%d\n", data
->pwm1_auto_point_pwm
[ix
]);
440 static inline ssize_t
set_slope_register(struct i2c_client
*client
,
448 dt
= ptemp
[2]-ptemp
[1];
449 for (tmp
= 4; tmp
> 0; tmp
--) {
450 if (dt
* (0x20 >> tmp
) >= dpwm
)
453 tmp
|= (ptemp
[1] & 0x7C) << 1;
454 if (i2c_smbus_write_byte_data(client
,
456 dev_err(&client
->dev
, "Register write error, aborting.\n");
464 static ssize_t
set_temp_auto_point_temp(
466 struct device_attribute
*attr
,
470 struct i2c_client
*client
= to_i2c_client(dev
);
471 struct amc6821_data
*data
= amc6821_update_device(dev
);
472 int ix
= to_sensor_dev_attr_2(attr
)->index
;
473 int nr
= to_sensor_dev_attr_2(attr
)->nr
;
478 int ret
= strict_strtol(buf
, 10, &val
);
484 ptemp
= data
->temp1_auto_point_temp
;
485 reg
= AMC6821_REG_LTEMP_FAN_CTRL
;
488 ptemp
= data
->temp2_auto_point_temp
;
489 reg
= AMC6821_REG_RTEMP_FAN_CTRL
;
492 dev_dbg(dev
, "Unknown attr->nr (%d).\n", nr
);
497 mutex_lock(&data
->update_lock
);
500 ptemp
[0] = SENSORS_LIMIT(val
/ 1000, 0,
501 data
->temp1_auto_point_temp
[1]);
502 ptemp
[0] = SENSORS_LIMIT(ptemp
[0], 0,
503 data
->temp2_auto_point_temp
[1]);
504 ptemp
[0] = SENSORS_LIMIT(ptemp
[0], 0, 63);
505 if (i2c_smbus_write_byte_data(
507 AMC6821_REG_PSV_TEMP
,
509 dev_err(&client
->dev
,
510 "Register write error, aborting.\n");
516 ptemp
[1] = SENSORS_LIMIT(
518 (ptemp
[0] & 0x7C) + 4,
521 ptemp
[2] = SENSORS_LIMIT(
522 ptemp
[2], ptemp
[1] + 1,
526 ptemp
[2] = SENSORS_LIMIT(
532 dev_dbg(dev
, "Unknown attr->index (%d).\n", ix
);
536 dpwm
= data
->pwm1_auto_point_pwm
[2] - data
->pwm1_auto_point_pwm
[1];
537 if (set_slope_register(client
, reg
, dpwm
, ptemp
))
541 mutex_unlock(&data
->update_lock
);
547 static ssize_t
set_pwm1_auto_point_pwm(
549 struct device_attribute
*attr
,
553 struct i2c_client
*client
= to_i2c_client(dev
);
554 struct amc6821_data
*data
= i2c_get_clientdata(client
);
557 int ret
= strict_strtol(buf
, 10, &val
);
561 mutex_lock(&data
->update_lock
);
562 data
->pwm1_auto_point_pwm
[1] = SENSORS_LIMIT(val
, 0, 254);
563 if (i2c_smbus_write_byte_data(client
, AMC6821_REG_DCY_LOW_TEMP
,
564 data
->pwm1_auto_point_pwm
[1])) {
565 dev_err(&client
->dev
, "Register write error, aborting.\n");
569 dpwm
= data
->pwm1_auto_point_pwm
[2] - data
->pwm1_auto_point_pwm
[1];
570 if (set_slope_register(client
, AMC6821_REG_LTEMP_FAN_CTRL
, dpwm
,
571 data
->temp1_auto_point_temp
)) {
575 if (set_slope_register(client
, AMC6821_REG_RTEMP_FAN_CTRL
, dpwm
,
576 data
->temp2_auto_point_temp
)) {
583 mutex_unlock(&data
->update_lock
);
587 static ssize_t
get_fan(
589 struct device_attribute
*devattr
,
592 struct amc6821_data
*data
= amc6821_update_device(dev
);
593 int ix
= to_sensor_dev_attr(devattr
)->index
;
594 if (0 == data
->fan
[ix
])
595 return sprintf(buf
, "0");
596 return sprintf(buf
, "%d\n", (int)(6000000 / data
->fan
[ix
]));
601 static ssize_t
get_fan1_fault(
603 struct device_attribute
*devattr
,
606 struct amc6821_data
*data
= amc6821_update_device(dev
);
607 if (data
->stat1
& AMC6821_STAT1_FANS
)
608 return sprintf(buf
, "1");
610 return sprintf(buf
, "0");
615 static ssize_t
set_fan(
617 struct device_attribute
*attr
,
618 const char *buf
, size_t count
)
620 struct i2c_client
*client
= to_i2c_client(dev
);
621 struct amc6821_data
*data
= i2c_get_clientdata(client
);
623 int ix
= to_sensor_dev_attr(attr
)->index
;
624 int ret
= strict_strtol(buf
, 10, &val
);
627 val
= 1 > val
? 0xFFFF : 6000000/val
;
629 mutex_lock(&data
->update_lock
);
630 data
->fan
[ix
] = (u16
) SENSORS_LIMIT(val
, 1, 0xFFFF);
631 if (i2c_smbus_write_byte_data(client
, fan_reg_low
[ix
],
632 data
->fan
[ix
] & 0xFF)) {
633 dev_err(&client
->dev
, "Register write error, aborting.\n");
637 if (i2c_smbus_write_byte_data(client
,
638 fan_reg_hi
[ix
], data
->fan
[ix
] >> 8)) {
639 dev_err(&client
->dev
, "Register write error, aborting.\n");
643 mutex_unlock(&data
->update_lock
);
649 static ssize_t
get_fan1_div(
651 struct device_attribute
*devattr
,
654 struct amc6821_data
*data
= amc6821_update_device(dev
);
655 return sprintf(buf
, "%d\n", data
->fan1_div
);
658 static ssize_t
set_fan1_div(
660 struct device_attribute
*attr
,
661 const char *buf
, size_t count
)
663 struct i2c_client
*client
= to_i2c_client(dev
);
664 struct amc6821_data
*data
= i2c_get_clientdata(client
);
666 int config
= strict_strtol(buf
, 10, &val
);
670 config
= i2c_smbus_read_byte_data(client
, AMC6821_REG_CONF4
);
672 dev_err(&client
->dev
,
673 "Error reading configuration register, aborting.\n");
676 mutex_lock(&data
->update_lock
);
679 config
&= ~AMC6821_CONF4_PSPR
;
683 config
|= AMC6821_CONF4_PSPR
;
687 mutex_unlock(&data
->update_lock
);
691 if (i2c_smbus_write_byte_data(client
, AMC6821_REG_CONF4
, config
)) {
692 dev_err(&client
->dev
,
693 "Configuration register write error, aborting.\n");
697 mutex_unlock(&data
->update_lock
);
703 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
,
704 get_temp
, NULL
, IDX_TEMP1_INPUT
);
705 static SENSOR_DEVICE_ATTR(temp1_min
, S_IRUGO
| S_IWUSR
, get_temp
,
706 set_temp
, IDX_TEMP1_MIN
);
707 static SENSOR_DEVICE_ATTR(temp1_max
, S_IRUGO
| S_IWUSR
, get_temp
,
708 set_temp
, IDX_TEMP1_MAX
);
709 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IRUGO
| S_IWUSR
, get_temp
,
710 set_temp
, IDX_TEMP1_CRIT
);
711 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
,
712 get_temp_alarm
, NULL
, IDX_TEMP1_MIN
);
713 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
,
714 get_temp_alarm
, NULL
, IDX_TEMP1_MAX
);
715 static SENSOR_DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
,
716 get_temp_alarm
, NULL
, IDX_TEMP1_CRIT
);
717 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
| S_IWUSR
,
718 get_temp
, NULL
, IDX_TEMP2_INPUT
);
719 static SENSOR_DEVICE_ATTR(temp2_min
, S_IRUGO
| S_IWUSR
, get_temp
,
720 set_temp
, IDX_TEMP2_MIN
);
721 static SENSOR_DEVICE_ATTR(temp2_max
, S_IRUGO
| S_IWUSR
, get_temp
,
722 set_temp
, IDX_TEMP2_MAX
);
723 static SENSOR_DEVICE_ATTR(temp2_crit
, S_IRUGO
| S_IWUSR
, get_temp
,
724 set_temp
, IDX_TEMP2_CRIT
);
725 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
,
726 get_temp2_fault
, NULL
, 0);
727 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
,
728 get_temp_alarm
, NULL
, IDX_TEMP2_MIN
);
729 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
,
730 get_temp_alarm
, NULL
, IDX_TEMP2_MAX
);
731 static SENSOR_DEVICE_ATTR(temp2_crit_alarm
, S_IRUGO
,
732 get_temp_alarm
, NULL
, IDX_TEMP2_CRIT
);
733 static SENSOR_DEVICE_ATTR(fan1_input
, S_IRUGO
, get_fan
, NULL
, IDX_FAN1_INPUT
);
734 static SENSOR_DEVICE_ATTR(fan1_min
, S_IRUGO
| S_IWUSR
,
735 get_fan
, set_fan
, IDX_FAN1_MIN
);
736 static SENSOR_DEVICE_ATTR(fan1_max
, S_IRUGO
| S_IWUSR
,
737 get_fan
, set_fan
, IDX_FAN1_MAX
);
738 static SENSOR_DEVICE_ATTR(fan1_fault
, S_IRUGO
, get_fan1_fault
, NULL
, 0);
739 static SENSOR_DEVICE_ATTR(fan1_div
, S_IRUGO
| S_IWUSR
,
740 get_fan1_div
, set_fan1_div
, 0);
742 static SENSOR_DEVICE_ATTR(pwm1
, S_IWUSR
| S_IRUGO
, get_pwm1
, set_pwm1
, 0);
743 static SENSOR_DEVICE_ATTR(pwm1_enable
, S_IWUSR
| S_IRUGO
,
744 get_pwm1_enable
, set_pwm1_enable
, 0);
745 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm
, S_IRUGO
,
746 get_pwm1_auto_point_pwm
, NULL
, 0);
747 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm
, S_IWUSR
| S_IRUGO
,
748 get_pwm1_auto_point_pwm
, set_pwm1_auto_point_pwm
, 1);
749 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm
, S_IRUGO
,
750 get_pwm1_auto_point_pwm
, NULL
, 2);
751 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp
, S_IRUGO
,
752 get_pwm1_auto_channels_temp
, NULL
, 0);
753 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp
, S_IRUGO
,
754 get_temp_auto_point_temp
, NULL
, 1, 0);
755 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp
, S_IWUSR
| S_IRUGO
,
756 get_temp_auto_point_temp
, set_temp_auto_point_temp
, 1, 1);
757 static SENSOR_DEVICE_ATTR_2(temp1_auto_point3_temp
, S_IWUSR
| S_IRUGO
,
758 get_temp_auto_point_temp
, set_temp_auto_point_temp
, 1, 2);
760 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp
, S_IWUSR
| S_IRUGO
,
761 get_temp_auto_point_temp
, set_temp_auto_point_temp
, 2, 0);
762 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp
, S_IWUSR
| S_IRUGO
,
763 get_temp_auto_point_temp
, set_temp_auto_point_temp
, 2, 1);
764 static SENSOR_DEVICE_ATTR_2(temp2_auto_point3_temp
, S_IWUSR
| S_IRUGO
,
765 get_temp_auto_point_temp
, set_temp_auto_point_temp
, 2, 2);
769 static struct attribute
*amc6821_attrs
[] = {
770 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
771 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
772 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
773 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
774 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
775 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
776 &sensor_dev_attr_temp1_crit_alarm
.dev_attr
.attr
,
777 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
778 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
779 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
780 &sensor_dev_attr_temp2_crit
.dev_attr
.attr
,
781 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
782 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
783 &sensor_dev_attr_temp2_crit_alarm
.dev_attr
.attr
,
784 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
785 &sensor_dev_attr_fan1_input
.dev_attr
.attr
,
786 &sensor_dev_attr_fan1_min
.dev_attr
.attr
,
787 &sensor_dev_attr_fan1_max
.dev_attr
.attr
,
788 &sensor_dev_attr_fan1_fault
.dev_attr
.attr
,
789 &sensor_dev_attr_fan1_div
.dev_attr
.attr
,
790 &sensor_dev_attr_pwm1
.dev_attr
.attr
,
791 &sensor_dev_attr_pwm1_enable
.dev_attr
.attr
,
792 &sensor_dev_attr_pwm1_auto_channels_temp
.dev_attr
.attr
,
793 &sensor_dev_attr_pwm1_auto_point1_pwm
.dev_attr
.attr
,
794 &sensor_dev_attr_pwm1_auto_point2_pwm
.dev_attr
.attr
,
795 &sensor_dev_attr_pwm1_auto_point3_pwm
.dev_attr
.attr
,
796 &sensor_dev_attr_temp1_auto_point1_temp
.dev_attr
.attr
,
797 &sensor_dev_attr_temp1_auto_point2_temp
.dev_attr
.attr
,
798 &sensor_dev_attr_temp1_auto_point3_temp
.dev_attr
.attr
,
799 &sensor_dev_attr_temp2_auto_point1_temp
.dev_attr
.attr
,
800 &sensor_dev_attr_temp2_auto_point2_temp
.dev_attr
.attr
,
801 &sensor_dev_attr_temp2_auto_point3_temp
.dev_attr
.attr
,
805 static struct attribute_group amc6821_attr_grp
= {
806 .attrs
= amc6821_attrs
,
811 /* Return 0 if detection is successful, -ENODEV otherwise */
812 static int amc6821_detect(
813 struct i2c_client
*client
,
815 struct i2c_board_info
*info
)
817 struct i2c_adapter
*adapter
= client
->adapter
;
818 int address
= client
->addr
;
820 dev_dbg(&adapter
->dev
, "amc6821_detect called, kind = %d.\n", kind
);
822 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
823 dev_dbg(&adapter
->dev
,
824 "max6650: I2C bus doesn't support byte read mode, skipping.\n");
828 ((i2c_smbus_read_byte_data(client
, AMC6821_REG_DEV_ID
) &
830 (i2c_smbus_read_byte_data(client
, AMC6821_REG_COMP_ID
) &
832 dev_dbg(&adapter
->dev
,
833 "amc6821: detection failed at 0x%02x.\n",
838 dev_info(&adapter
->dev
, "amc6821: chip found at 0x%02x.\n", address
);
839 strlcpy(info
->type
, "amc6821", I2C_NAME_SIZE
);
844 static int amc6821_probe(
845 struct i2c_client
*client
,
846 const struct i2c_device_id
*id
)
848 struct amc6821_data
*data
;
851 data
= kzalloc(sizeof(struct amc6821_data
), GFP_KERNEL
);
853 dev_err(&client
->dev
, "out of memory.\n");
858 i2c_set_clientdata(client
, data
);
859 mutex_init(&data
->update_lock
);
862 * Initialize the amc6821 chip
864 err
= amc6821_init_client(client
);
868 err
= sysfs_create_group(&client
->dev
.kobj
, &amc6821_attr_grp
);
872 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
873 if (!IS_ERR(data
->hwmon_dev
))
876 err
= PTR_ERR(data
->hwmon_dev
);
877 dev_err(&client
->dev
, "error registering hwmon device.\n");
878 sysfs_remove_group(&client
->dev
.kobj
, &amc6821_attr_grp
);
884 static int amc6821_remove(struct i2c_client
*client
)
886 struct amc6821_data
*data
= i2c_get_clientdata(client
);
888 hwmon_device_unregister(data
->hwmon_dev
);
889 sysfs_remove_group(&client
->dev
.kobj
, &amc6821_attr_grp
);
897 static int amc6821_init_client(struct i2c_client
*client
)
903 config
= i2c_smbus_read_byte_data(client
, AMC6821_REG_CONF4
);
906 dev_err(&client
->dev
,
907 "Error reading configuration register, aborting.\n");
911 config
|= AMC6821_CONF4_MODE
;
913 if (i2c_smbus_write_byte_data(client
, AMC6821_REG_CONF4
,
915 dev_err(&client
->dev
,
916 "Configuration register write error, aborting.\n");
920 config
= i2c_smbus_read_byte_data(client
, AMC6821_REG_CONF3
);
923 dev_err(&client
->dev
,
924 "Error reading configuration register, aborting.\n");
928 config
&= ~AMC6821_CONF3_THERM_FAN_EN
;
930 if (i2c_smbus_write_byte_data(client
, AMC6821_REG_CONF3
,
932 dev_err(&client
->dev
,
933 "Configuration register write error, aborting.\n");
937 config
= i2c_smbus_read_byte_data(client
, AMC6821_REG_CONF2
);
940 dev_err(&client
->dev
,
941 "Error reading configuration register, aborting.\n");
945 config
&= ~AMC6821_CONF2_RTFIE
;
946 config
&= ~AMC6821_CONF2_LTOIE
;
947 config
&= ~AMC6821_CONF2_RTOIE
;
948 if (i2c_smbus_write_byte_data(client
,
949 AMC6821_REG_CONF2
, config
)) {
950 dev_err(&client
->dev
,
951 "Configuration register write error, aborting.\n");
955 config
= i2c_smbus_read_byte_data(client
, AMC6821_REG_CONF1
);
958 dev_err(&client
->dev
,
959 "Error reading configuration register, aborting.\n");
963 config
&= ~AMC6821_CONF1_THERMOVIE
;
964 config
&= ~AMC6821_CONF1_FANIE
;
965 config
|= AMC6821_CONF1_START
;
967 config
|= AMC6821_CONF1_PWMINV
;
969 config
&= ~AMC6821_CONF1_PWMINV
;
971 if (i2c_smbus_write_byte_data(
972 client
, AMC6821_REG_CONF1
, config
)) {
973 dev_err(&client
->dev
,
974 "Configuration register write error, aborting.\n");
982 static struct amc6821_data
*amc6821_update_device(struct device
*dev
)
984 struct i2c_client
*client
= to_i2c_client(dev
);
985 struct amc6821_data
*data
= i2c_get_clientdata(client
);
990 mutex_lock(&data
->update_lock
);
992 if (time_after(jiffies
, data
->last_updated
+ timeout
) ||
995 for (i
= 0; i
< TEMP_IDX_LEN
; i
++)
996 data
->temp
[i
] = i2c_smbus_read_byte_data(client
,
999 data
->stat1
= i2c_smbus_read_byte_data(client
,
1001 data
->stat2
= i2c_smbus_read_byte_data(client
,
1004 data
->pwm1
= i2c_smbus_read_byte_data(client
,
1006 for (i
= 0; i
< FAN1_IDX_LEN
; i
++) {
1007 data
->fan
[i
] = i2c_smbus_read_byte_data(
1010 data
->fan
[i
] += i2c_smbus_read_byte_data(
1012 fan_reg_hi
[i
]) << 8;
1014 data
->fan1_div
= i2c_smbus_read_byte_data(client
,
1016 data
->fan1_div
= data
->fan1_div
& AMC6821_CONF4_PSPR
? 4 : 2;
1018 data
->pwm1_auto_point_pwm
[0] = 0;
1019 data
->pwm1_auto_point_pwm
[2] = 255;
1020 data
->pwm1_auto_point_pwm
[1] = i2c_smbus_read_byte_data(client
,
1021 AMC6821_REG_DCY_LOW_TEMP
);
1023 data
->temp1_auto_point_temp
[0] =
1024 i2c_smbus_read_byte_data(client
,
1025 AMC6821_REG_PSV_TEMP
);
1026 data
->temp2_auto_point_temp
[0] =
1027 data
->temp1_auto_point_temp
[0];
1028 reg
= i2c_smbus_read_byte_data(client
,
1029 AMC6821_REG_LTEMP_FAN_CTRL
);
1030 data
->temp1_auto_point_temp
[1] = (reg
& 0xF8) >> 1;
1034 data
->temp1_auto_point_temp
[2] =
1035 data
->temp1_auto_point_temp
[1] +
1036 (data
->pwm1_auto_point_pwm
[2] -
1037 data
->pwm1_auto_point_pwm
[1]) / reg
;
1039 data
->temp1_auto_point_temp
[2] = 255;
1041 reg
= i2c_smbus_read_byte_data(client
,
1042 AMC6821_REG_RTEMP_FAN_CTRL
);
1043 data
->temp2_auto_point_temp
[1] = (reg
& 0xF8) >> 1;
1047 data
->temp2_auto_point_temp
[2] =
1048 data
->temp2_auto_point_temp
[1] +
1049 (data
->pwm1_auto_point_pwm
[2] -
1050 data
->pwm1_auto_point_pwm
[1]) / reg
;
1052 data
->temp2_auto_point_temp
[2] = 255;
1054 reg
= i2c_smbus_read_byte_data(client
, AMC6821_REG_CONF1
);
1055 reg
= (reg
>> 5) & 0x3;
1057 case 0: /*open loop: software sets pwm1*/
1058 data
->pwm1_auto_channels_temp
= 0;
1059 data
->pwm1_enable
= 1;
1061 case 2: /*closed loop: remote T (temp2)*/
1062 data
->pwm1_auto_channels_temp
= 2;
1063 data
->pwm1_enable
= 2;
1065 case 3: /*closed loop: local and remote T (temp2)*/
1066 data
->pwm1_auto_channels_temp
= 3;
1067 data
->pwm1_enable
= 3;
1069 case 1: /*semi-open loop: software sets rpm, chip controls pwm1,
1070 *currently not implemented
1072 data
->pwm1_auto_channels_temp
= 0;
1073 data
->pwm1_enable
= 0;
1077 data
->last_updated
= jiffies
;
1080 mutex_unlock(&data
->update_lock
);
1085 static int __init
amc6821_init(void)
1087 return i2c_add_driver(&amc6821_driver
);
1090 static void __exit
amc6821_exit(void)
1092 i2c_del_driver(&amc6821_driver
);
1095 module_init(amc6821_init
);
1096 module_exit(amc6821_exit
);
1099 MODULE_LICENSE("GPL");
1100 MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
1101 MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");
1102 MODULE_SUPPORTED_DEVICE("amc6821");