2 * amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
4 * Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
7 * Copyright (C) 2007 Hans J. Koch <hjk@hansjkoch.de>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/kernel.h> /* Needed for KERN_INFO */
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/i2c.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
39 static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
40 0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
46 static int pwminv; /*Inverted PWM output. */
47 module_param(pwminv, int, S_IRUGO);
49 static int init = 1; /*Power-on initialization.*/
50 module_param(init, int, S_IRUGO);
52 enum chips { amc6821 };
54 #define AMC6821_REG_DEV_ID 0x3D
55 #define AMC6821_REG_COMP_ID 0x3E
56 #define AMC6821_REG_CONF1 0x00
57 #define AMC6821_REG_CONF2 0x01
58 #define AMC6821_REG_CONF3 0x3F
59 #define AMC6821_REG_CONF4 0x04
60 #define AMC6821_REG_STAT1 0x02
61 #define AMC6821_REG_STAT2 0x03
62 #define AMC6821_REG_TDATA_LOW 0x08
63 #define AMC6821_REG_TDATA_HI 0x09
64 #define AMC6821_REG_LTEMP_HI 0x0A
65 #define AMC6821_REG_RTEMP_HI 0x0B
66 #define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
67 #define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
68 #define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
69 #define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
70 #define AMC6821_REG_LTEMP_CRIT 0x1B
71 #define AMC6821_REG_RTEMP_CRIT 0x1D
72 #define AMC6821_REG_PSV_TEMP 0x1C
73 #define AMC6821_REG_DCY 0x22
74 #define AMC6821_REG_LTEMP_FAN_CTRL 0x24
75 #define AMC6821_REG_RTEMP_FAN_CTRL 0x25
76 #define AMC6821_REG_DCY_LOW_TEMP 0x21
78 #define AMC6821_REG_TACH_LLIMITL 0x10
79 #define AMC6821_REG_TACH_LLIMITH 0x11
80 #define AMC6821_REG_TACH_HLIMITL 0x12
81 #define AMC6821_REG_TACH_HLIMITH 0x13
83 #define AMC6821_CONF1_START 0x01
84 #define AMC6821_CONF1_FAN_INT_EN 0x02
85 #define AMC6821_CONF1_FANIE 0x04
86 #define AMC6821_CONF1_PWMINV 0x08
87 #define AMC6821_CONF1_FAN_FAULT_EN 0x10
88 #define AMC6821_CONF1_FDRC0 0x20
89 #define AMC6821_CONF1_FDRC1 0x40
90 #define AMC6821_CONF1_THERMOVIE 0x80
92 #define AMC6821_CONF2_PWM_EN 0x01
93 #define AMC6821_CONF2_TACH_MODE 0x02
94 #define AMC6821_CONF2_TACH_EN 0x04
95 #define AMC6821_CONF2_RTFIE 0x08
96 #define AMC6821_CONF2_LTOIE 0x10
97 #define AMC6821_CONF2_RTOIE 0x20
98 #define AMC6821_CONF2_PSVIE 0x40
99 #define AMC6821_CONF2_RST 0x80
101 #define AMC6821_CONF3_THERM_FAN_EN 0x80
102 #define AMC6821_CONF3_REV_MASK 0x0F
104 #define AMC6821_CONF4_OVREN 0x10
105 #define AMC6821_CONF4_TACH_FAST 0x20
106 #define AMC6821_CONF4_PSPR 0x40
107 #define AMC6821_CONF4_MODE 0x80
109 #define AMC6821_STAT1_RPM_ALARM 0x01
110 #define AMC6821_STAT1_FANS 0x02
111 #define AMC6821_STAT1_RTH 0x04
112 #define AMC6821_STAT1_RTL 0x08
113 #define AMC6821_STAT1_R_THERM 0x10
114 #define AMC6821_STAT1_RTF 0x20
115 #define AMC6821_STAT1_LTH 0x40
116 #define AMC6821_STAT1_LTL 0x80
118 #define AMC6821_STAT2_RTC 0x08
119 #define AMC6821_STAT2_LTC 0x10
120 #define AMC6821_STAT2_LPSV 0x20
121 #define AMC6821_STAT2_L_THERM 0x40
122 #define AMC6821_STAT2_THERM_IN 0x80
124 enum {IDX_TEMP1_INPUT = 0, IDX_TEMP1_MIN, IDX_TEMP1_MAX,
125 IDX_TEMP1_CRIT, IDX_TEMP2_INPUT, IDX_TEMP2_MIN,
126 IDX_TEMP2_MAX, IDX_TEMP2_CRIT,
129 static const u8 temp_reg[] = {AMC6821_REG_LTEMP_HI,
130 AMC6821_REG_LTEMP_LIMIT_MIN,
131 AMC6821_REG_LTEMP_LIMIT_MAX,
132 AMC6821_REG_LTEMP_CRIT,
133 AMC6821_REG_RTEMP_HI,
134 AMC6821_REG_RTEMP_LIMIT_MIN,
135 AMC6821_REG_RTEMP_LIMIT_MAX,
136 AMC6821_REG_RTEMP_CRIT, };
138 enum {IDX_FAN1_INPUT = 0, IDX_FAN1_MIN, IDX_FAN1_MAX,
141 static const u8 fan_reg_low[] = {AMC6821_REG_TDATA_LOW,
142 AMC6821_REG_TACH_LLIMITL,
143 AMC6821_REG_TACH_HLIMITL, };
146 static const u8 fan_reg_hi[] = {AMC6821_REG_TDATA_HI,
147 AMC6821_REG_TACH_LLIMITH,
148 AMC6821_REG_TACH_HLIMITH, };
151 * Client data (each client gets its own)
154 struct amc6821_data {
155 struct i2c_client *client;
156 struct mutex update_lock;
157 char valid; /* zero until following fields are valid */
158 unsigned long last_updated; /* in jiffies */
160 /* register values */
161 int temp[TEMP_IDX_LEN];
163 u16 fan[FAN1_IDX_LEN];
167 u8 temp1_auto_point_temp[3];
168 u8 temp2_auto_point_temp[3];
169 u8 pwm1_auto_point_pwm[3];
171 u8 pwm1_auto_channels_temp;
177 static struct amc6821_data *amc6821_update_device(struct device *dev)
179 struct amc6821_data *data = dev_get_drvdata(dev);
180 struct i2c_client *client = data->client;
185 mutex_lock(&data->update_lock);
187 if (time_after(jiffies, data->last_updated + timeout) ||
190 for (i = 0; i < TEMP_IDX_LEN; i++)
191 data->temp[i] = i2c_smbus_read_byte_data(client,
194 data->stat1 = i2c_smbus_read_byte_data(client,
196 data->stat2 = i2c_smbus_read_byte_data(client,
199 data->pwm1 = i2c_smbus_read_byte_data(client,
201 for (i = 0; i < FAN1_IDX_LEN; i++) {
202 data->fan[i] = i2c_smbus_read_byte_data(
205 data->fan[i] += i2c_smbus_read_byte_data(
209 data->fan1_div = i2c_smbus_read_byte_data(client,
211 data->fan1_div = data->fan1_div & AMC6821_CONF4_PSPR ? 4 : 2;
213 data->pwm1_auto_point_pwm[0] = 0;
214 data->pwm1_auto_point_pwm[2] = 255;
215 data->pwm1_auto_point_pwm[1] = i2c_smbus_read_byte_data(client,
216 AMC6821_REG_DCY_LOW_TEMP);
218 data->temp1_auto_point_temp[0] =
219 i2c_smbus_read_byte_data(client,
220 AMC6821_REG_PSV_TEMP);
221 data->temp2_auto_point_temp[0] =
222 data->temp1_auto_point_temp[0];
223 reg = i2c_smbus_read_byte_data(client,
224 AMC6821_REG_LTEMP_FAN_CTRL);
225 data->temp1_auto_point_temp[1] = (reg & 0xF8) >> 1;
229 data->temp1_auto_point_temp[2] =
230 data->temp1_auto_point_temp[1] +
231 (data->pwm1_auto_point_pwm[2] -
232 data->pwm1_auto_point_pwm[1]) / reg;
234 data->temp1_auto_point_temp[2] = 255;
236 reg = i2c_smbus_read_byte_data(client,
237 AMC6821_REG_RTEMP_FAN_CTRL);
238 data->temp2_auto_point_temp[1] = (reg & 0xF8) >> 1;
242 data->temp2_auto_point_temp[2] =
243 data->temp2_auto_point_temp[1] +
244 (data->pwm1_auto_point_pwm[2] -
245 data->pwm1_auto_point_pwm[1]) / reg;
247 data->temp2_auto_point_temp[2] = 255;
249 reg = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
250 reg = (reg >> 5) & 0x3;
252 case 0: /*open loop: software sets pwm1*/
253 data->pwm1_auto_channels_temp = 0;
254 data->pwm1_enable = 1;
256 case 2: /*closed loop: remote T (temp2)*/
257 data->pwm1_auto_channels_temp = 2;
258 data->pwm1_enable = 2;
260 case 3: /*closed loop: local and remote T (temp2)*/
261 data->pwm1_auto_channels_temp = 3;
262 data->pwm1_enable = 3;
265 * semi-open loop: software sets rpm, chip controls
266 * pwm1, currently not implemented
268 data->pwm1_auto_channels_temp = 0;
269 data->pwm1_enable = 0;
273 data->last_updated = jiffies;
276 mutex_unlock(&data->update_lock);
280 static ssize_t get_temp(
282 struct device_attribute *devattr,
285 struct amc6821_data *data = amc6821_update_device(dev);
286 int ix = to_sensor_dev_attr(devattr)->index;
288 return sprintf(buf, "%d\n", data->temp[ix] * 1000);
291 static ssize_t set_temp(
293 struct device_attribute *attr,
297 struct amc6821_data *data = dev_get_drvdata(dev);
298 struct i2c_client *client = data->client;
299 int ix = to_sensor_dev_attr(attr)->index;
302 int ret = kstrtol(buf, 10, &val);
305 val = clamp_val(val / 1000, -128, 127);
307 mutex_lock(&data->update_lock);
308 data->temp[ix] = val;
309 if (i2c_smbus_write_byte_data(client, temp_reg[ix], data->temp[ix])) {
310 dev_err(&client->dev, "Register write error, aborting.\n");
313 mutex_unlock(&data->update_lock);
317 static ssize_t get_temp_alarm(
319 struct device_attribute *devattr,
322 struct amc6821_data *data = amc6821_update_device(dev);
323 int ix = to_sensor_dev_attr(devattr)->index;
328 flag = data->stat1 & AMC6821_STAT1_LTL;
331 flag = data->stat1 & AMC6821_STAT1_LTH;
334 flag = data->stat2 & AMC6821_STAT2_LTC;
337 flag = data->stat1 & AMC6821_STAT1_RTL;
340 flag = data->stat1 & AMC6821_STAT1_RTH;
343 flag = data->stat2 & AMC6821_STAT2_RTC;
346 dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
350 return sprintf(buf, "1");
352 return sprintf(buf, "0");
355 static ssize_t get_temp2_fault(
357 struct device_attribute *devattr,
360 struct amc6821_data *data = amc6821_update_device(dev);
361 if (data->stat1 & AMC6821_STAT1_RTF)
362 return sprintf(buf, "1");
364 return sprintf(buf, "0");
367 static ssize_t get_pwm1(
369 struct device_attribute *devattr,
372 struct amc6821_data *data = amc6821_update_device(dev);
373 return sprintf(buf, "%d\n", data->pwm1);
376 static ssize_t set_pwm1(
378 struct device_attribute *devattr,
382 struct amc6821_data *data = dev_get_drvdata(dev);
383 struct i2c_client *client = data->client;
385 int ret = kstrtol(buf, 10, &val);
389 mutex_lock(&data->update_lock);
390 data->pwm1 = clamp_val(val , 0, 255);
391 i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
392 mutex_unlock(&data->update_lock);
396 static ssize_t get_pwm1_enable(
398 struct device_attribute *devattr,
401 struct amc6821_data *data = amc6821_update_device(dev);
402 return sprintf(buf, "%d\n", data->pwm1_enable);
405 static ssize_t set_pwm1_enable(
407 struct device_attribute *attr,
411 struct amc6821_data *data = dev_get_drvdata(dev);
412 struct i2c_client *client = data->client;
414 int config = kstrtol(buf, 10, &val);
418 mutex_lock(&data->update_lock);
419 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
421 dev_err(&client->dev,
422 "Error reading configuration register, aborting.\n");
429 config &= ~AMC6821_CONF1_FDRC0;
430 config &= ~AMC6821_CONF1_FDRC1;
433 config &= ~AMC6821_CONF1_FDRC0;
434 config |= AMC6821_CONF1_FDRC1;
437 config |= AMC6821_CONF1_FDRC0;
438 config |= AMC6821_CONF1_FDRC1;
444 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF1, config)) {
445 dev_err(&client->dev,
446 "Configuration register write error, aborting.\n");
450 mutex_unlock(&data->update_lock);
454 static ssize_t get_pwm1_auto_channels_temp(
456 struct device_attribute *devattr,
459 struct amc6821_data *data = amc6821_update_device(dev);
460 return sprintf(buf, "%d\n", data->pwm1_auto_channels_temp);
463 static ssize_t get_temp_auto_point_temp(
465 struct device_attribute *devattr,
468 int ix = to_sensor_dev_attr_2(devattr)->index;
469 int nr = to_sensor_dev_attr_2(devattr)->nr;
470 struct amc6821_data *data = amc6821_update_device(dev);
473 return sprintf(buf, "%d\n",
474 data->temp1_auto_point_temp[ix] * 1000);
476 return sprintf(buf, "%d\n",
477 data->temp2_auto_point_temp[ix] * 1000);
479 dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
484 static ssize_t get_pwm1_auto_point_pwm(
486 struct device_attribute *devattr,
489 int ix = to_sensor_dev_attr(devattr)->index;
490 struct amc6821_data *data = amc6821_update_device(dev);
491 return sprintf(buf, "%d\n", data->pwm1_auto_point_pwm[ix]);
494 static inline ssize_t set_slope_register(struct i2c_client *client,
502 dt = ptemp[2]-ptemp[1];
503 for (tmp = 4; tmp > 0; tmp--) {
504 if (dt * (0x20 >> tmp) >= dpwm)
507 tmp |= (ptemp[1] & 0x7C) << 1;
508 if (i2c_smbus_write_byte_data(client,
510 dev_err(&client->dev, "Register write error, aborting.\n");
516 static ssize_t set_temp_auto_point_temp(
518 struct device_attribute *attr,
522 struct amc6821_data *data = amc6821_update_device(dev);
523 struct i2c_client *client = data->client;
524 int ix = to_sensor_dev_attr_2(attr)->index;
525 int nr = to_sensor_dev_attr_2(attr)->nr;
530 int ret = kstrtol(buf, 10, &val);
536 ptemp = data->temp1_auto_point_temp;
537 reg = AMC6821_REG_LTEMP_FAN_CTRL;
540 ptemp = data->temp2_auto_point_temp;
541 reg = AMC6821_REG_RTEMP_FAN_CTRL;
544 dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
548 mutex_lock(&data->update_lock);
553 ptemp[0] = clamp_val(val / 1000, 0,
554 data->temp1_auto_point_temp[1]);
555 ptemp[0] = clamp_val(ptemp[0], 0,
556 data->temp2_auto_point_temp[1]);
557 ptemp[0] = clamp_val(ptemp[0], 0, 63);
558 if (i2c_smbus_write_byte_data(
560 AMC6821_REG_PSV_TEMP,
562 dev_err(&client->dev,
563 "Register write error, aborting.\n");
568 ptemp[1] = clamp_val(val / 1000, (ptemp[0] & 0x7C) + 4, 124);
570 ptemp[2] = clamp_val(ptemp[2], ptemp[1] + 1, 255);
573 ptemp[2] = clamp_val(val / 1000, ptemp[1]+1, 255);
576 dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
580 dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
581 if (set_slope_register(client, reg, dpwm, ptemp))
585 mutex_unlock(&data->update_lock);
589 static ssize_t set_pwm1_auto_point_pwm(
591 struct device_attribute *attr,
595 struct amc6821_data *data = dev_get_drvdata(dev);
596 struct i2c_client *client = data->client;
599 int ret = kstrtol(buf, 10, &val);
603 mutex_lock(&data->update_lock);
604 data->pwm1_auto_point_pwm[1] = clamp_val(val, 0, 254);
605 if (i2c_smbus_write_byte_data(client, AMC6821_REG_DCY_LOW_TEMP,
606 data->pwm1_auto_point_pwm[1])) {
607 dev_err(&client->dev, "Register write error, aborting.\n");
611 dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
612 if (set_slope_register(client, AMC6821_REG_LTEMP_FAN_CTRL, dpwm,
613 data->temp1_auto_point_temp)) {
617 if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
618 data->temp2_auto_point_temp)) {
625 mutex_unlock(&data->update_lock);
629 static ssize_t get_fan(
631 struct device_attribute *devattr,
634 struct amc6821_data *data = amc6821_update_device(dev);
635 int ix = to_sensor_dev_attr(devattr)->index;
636 if (0 == data->fan[ix])
637 return sprintf(buf, "0");
638 return sprintf(buf, "%d\n", (int)(6000000 / data->fan[ix]));
641 static ssize_t get_fan1_fault(
643 struct device_attribute *devattr,
646 struct amc6821_data *data = amc6821_update_device(dev);
647 if (data->stat1 & AMC6821_STAT1_FANS)
648 return sprintf(buf, "1");
650 return sprintf(buf, "0");
653 static ssize_t set_fan(
655 struct device_attribute *attr,
656 const char *buf, size_t count)
658 struct amc6821_data *data = dev_get_drvdata(dev);
659 struct i2c_client *client = data->client;
661 int ix = to_sensor_dev_attr(attr)->index;
662 int ret = kstrtol(buf, 10, &val);
665 val = 1 > val ? 0xFFFF : 6000000/val;
667 mutex_lock(&data->update_lock);
668 data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
669 if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
670 data->fan[ix] & 0xFF)) {
671 dev_err(&client->dev, "Register write error, aborting.\n");
675 if (i2c_smbus_write_byte_data(client,
676 fan_reg_hi[ix], data->fan[ix] >> 8)) {
677 dev_err(&client->dev, "Register write error, aborting.\n");
681 mutex_unlock(&data->update_lock);
685 static ssize_t get_fan1_div(
687 struct device_attribute *devattr,
690 struct amc6821_data *data = amc6821_update_device(dev);
691 return sprintf(buf, "%d\n", data->fan1_div);
694 static ssize_t set_fan1_div(
696 struct device_attribute *attr,
697 const char *buf, size_t count)
699 struct amc6821_data *data = dev_get_drvdata(dev);
700 struct i2c_client *client = data->client;
702 int config = kstrtol(buf, 10, &val);
706 mutex_lock(&data->update_lock);
707 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
709 dev_err(&client->dev,
710 "Error reading configuration register, aborting.\n");
716 config &= ~AMC6821_CONF4_PSPR;
720 config |= AMC6821_CONF4_PSPR;
727 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
728 dev_err(&client->dev,
729 "Configuration register write error, aborting.\n");
733 mutex_unlock(&data->update_lock);
737 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
738 get_temp, NULL, IDX_TEMP1_INPUT);
739 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, get_temp,
740 set_temp, IDX_TEMP1_MIN);
741 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, get_temp,
742 set_temp, IDX_TEMP1_MAX);
743 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, get_temp,
744 set_temp, IDX_TEMP1_CRIT);
745 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
746 get_temp_alarm, NULL, IDX_TEMP1_MIN);
747 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
748 get_temp_alarm, NULL, IDX_TEMP1_MAX);
749 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
750 get_temp_alarm, NULL, IDX_TEMP1_CRIT);
751 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO,
752 get_temp, NULL, IDX_TEMP2_INPUT);
753 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, get_temp,
754 set_temp, IDX_TEMP2_MIN);
755 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, get_temp,
756 set_temp, IDX_TEMP2_MAX);
757 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, get_temp,
758 set_temp, IDX_TEMP2_CRIT);
759 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO,
760 get_temp2_fault, NULL, 0);
761 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
762 get_temp_alarm, NULL, IDX_TEMP2_MIN);
763 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
764 get_temp_alarm, NULL, IDX_TEMP2_MAX);
765 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
766 get_temp_alarm, NULL, IDX_TEMP2_CRIT);
767 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, IDX_FAN1_INPUT);
768 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
769 get_fan, set_fan, IDX_FAN1_MIN);
770 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO | S_IWUSR,
771 get_fan, set_fan, IDX_FAN1_MAX);
772 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan1_fault, NULL, 0);
773 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
774 get_fan1_div, set_fan1_div, 0);
776 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm1, set_pwm1, 0);
777 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
778 get_pwm1_enable, set_pwm1_enable, 0);
779 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO,
780 get_pwm1_auto_point_pwm, NULL, 0);
781 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
782 get_pwm1_auto_point_pwm, set_pwm1_auto_point_pwm, 1);
783 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO,
784 get_pwm1_auto_point_pwm, NULL, 2);
785 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
786 get_pwm1_auto_channels_temp, NULL, 0);
787 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO,
788 get_temp_auto_point_temp, NULL, 1, 0);
789 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
790 get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 1);
791 static SENSOR_DEVICE_ATTR_2(temp1_auto_point3_temp, S_IWUSR | S_IRUGO,
792 get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 2);
794 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
795 get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 0);
796 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
797 get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 1);
798 static SENSOR_DEVICE_ATTR_2(temp2_auto_point3_temp, S_IWUSR | S_IRUGO,
799 get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 2);
801 static struct attribute *amc6821_attrs[] = {
802 &sensor_dev_attr_temp1_input.dev_attr.attr,
803 &sensor_dev_attr_temp1_min.dev_attr.attr,
804 &sensor_dev_attr_temp1_max.dev_attr.attr,
805 &sensor_dev_attr_temp1_crit.dev_attr.attr,
806 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
807 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
808 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
809 &sensor_dev_attr_temp2_input.dev_attr.attr,
810 &sensor_dev_attr_temp2_min.dev_attr.attr,
811 &sensor_dev_attr_temp2_max.dev_attr.attr,
812 &sensor_dev_attr_temp2_crit.dev_attr.attr,
813 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
814 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
815 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
816 &sensor_dev_attr_temp2_fault.dev_attr.attr,
817 &sensor_dev_attr_fan1_input.dev_attr.attr,
818 &sensor_dev_attr_fan1_min.dev_attr.attr,
819 &sensor_dev_attr_fan1_max.dev_attr.attr,
820 &sensor_dev_attr_fan1_fault.dev_attr.attr,
821 &sensor_dev_attr_fan1_div.dev_attr.attr,
822 &sensor_dev_attr_pwm1.dev_attr.attr,
823 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
824 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
825 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
826 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
827 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
828 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
829 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
830 &sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
831 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
832 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
833 &sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
837 ATTRIBUTE_GROUPS(amc6821);
839 /* Return 0 if detection is successful, -ENODEV otherwise */
840 static int amc6821_detect(
841 struct i2c_client *client,
842 struct i2c_board_info *info)
844 struct i2c_adapter *adapter = client->adapter;
845 int address = client->addr;
848 dev_dbg(&adapter->dev, "amc6821_detect called.\n");
850 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
851 dev_dbg(&adapter->dev,
852 "amc6821: I2C bus doesn't support byte mode, "
857 dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
858 comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
859 if (dev_id != 0x21 || comp_id != 0x49) {
860 dev_dbg(&adapter->dev,
861 "amc6821: detection failed at 0x%02x.\n",
867 * Bit 7 of the address register is ignored, so we can check the
870 dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
871 comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
872 if (dev_id != 0x21 || comp_id != 0x49) {
873 dev_dbg(&adapter->dev,
874 "amc6821: detection failed at 0x%02x.\n",
879 dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
880 strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
885 static int amc6821_init_client(struct i2c_client *client)
891 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
894 dev_err(&client->dev,
895 "Error reading configuration register, aborting.\n");
899 config |= AMC6821_CONF4_MODE;
901 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4,
903 dev_err(&client->dev,
904 "Configuration register write error, aborting.\n");
908 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF3);
911 dev_err(&client->dev,
912 "Error reading configuration register, aborting.\n");
916 dev_info(&client->dev, "Revision %d\n", config & 0x0f);
918 config &= ~AMC6821_CONF3_THERM_FAN_EN;
920 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
922 dev_err(&client->dev,
923 "Configuration register write error, aborting.\n");
927 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
930 dev_err(&client->dev,
931 "Error reading configuration register, aborting.\n");
935 config &= ~AMC6821_CONF2_RTFIE;
936 config &= ~AMC6821_CONF2_LTOIE;
937 config &= ~AMC6821_CONF2_RTOIE;
938 if (i2c_smbus_write_byte_data(client,
939 AMC6821_REG_CONF2, config)) {
940 dev_err(&client->dev,
941 "Configuration register write error, aborting.\n");
945 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
948 dev_err(&client->dev,
949 "Error reading configuration register, aborting.\n");
953 config &= ~AMC6821_CONF1_THERMOVIE;
954 config &= ~AMC6821_CONF1_FANIE;
955 config |= AMC6821_CONF1_START;
957 config |= AMC6821_CONF1_PWMINV;
959 config &= ~AMC6821_CONF1_PWMINV;
961 if (i2c_smbus_write_byte_data(
962 client, AMC6821_REG_CONF1, config)) {
963 dev_err(&client->dev,
964 "Configuration register write error, aborting.\n");
971 static int amc6821_probe(struct i2c_client *client,
972 const struct i2c_device_id *id)
974 struct device *dev = &client->dev;
975 struct amc6821_data *data;
976 struct device *hwmon_dev;
979 data = devm_kzalloc(dev, sizeof(struct amc6821_data), GFP_KERNEL);
983 data->client = client;
984 mutex_init(&data->update_lock);
987 * Initialize the amc6821 chip
989 err = amc6821_init_client(client);
993 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
996 return PTR_ERR_OR_ZERO(hwmon_dev);
999 static const struct i2c_device_id amc6821_id[] = {
1000 { "amc6821", amc6821 },
1004 MODULE_DEVICE_TABLE(i2c, amc6821_id);
1006 static struct i2c_driver amc6821_driver = {
1007 .class = I2C_CLASS_HWMON,
1011 .probe = amc6821_probe,
1012 .id_table = amc6821_id,
1013 .detect = amc6821_detect,
1014 .address_list = normal_i2c,
1017 module_i2c_driver(amc6821_driver);
1019 MODULE_LICENSE("GPL");
1020 MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
1021 MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");