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.
25 #include <linux/kernel.h> /* Needed for KERN_INFO */
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/jiffies.h>
30 #include <linux/i2c.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/err.h>
34 #include <linux/mutex.h>
41 static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
42 0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
50 static int pwminv; /*Inverted PWM output. */
51 module_param(pwminv, int, S_IRUGO);
53 static int init = 1; /*Power-on initialization.*/
54 module_param(init, int, S_IRUGO);
57 enum chips { amc6821 };
59 #define AMC6821_REG_DEV_ID 0x3D
60 #define AMC6821_REG_COMP_ID 0x3E
61 #define AMC6821_REG_CONF1 0x00
62 #define AMC6821_REG_CONF2 0x01
63 #define AMC6821_REG_CONF3 0x3F
64 #define AMC6821_REG_CONF4 0x04
65 #define AMC6821_REG_STAT1 0x02
66 #define AMC6821_REG_STAT2 0x03
67 #define AMC6821_REG_TDATA_LOW 0x08
68 #define AMC6821_REG_TDATA_HI 0x09
69 #define AMC6821_REG_LTEMP_HI 0x0A
70 #define AMC6821_REG_RTEMP_HI 0x0B
71 #define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
72 #define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
73 #define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
74 #define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
75 #define AMC6821_REG_LTEMP_CRIT 0x1B
76 #define AMC6821_REG_RTEMP_CRIT 0x1D
77 #define AMC6821_REG_PSV_TEMP 0x1C
78 #define AMC6821_REG_DCY 0x22
79 #define AMC6821_REG_LTEMP_FAN_CTRL 0x24
80 #define AMC6821_REG_RTEMP_FAN_CTRL 0x25
81 #define AMC6821_REG_DCY_LOW_TEMP 0x21
83 #define AMC6821_REG_TACH_LLIMITL 0x10
84 #define AMC6821_REG_TACH_LLIMITH 0x11
85 #define AMC6821_REG_TACH_HLIMITL 0x12
86 #define AMC6821_REG_TACH_HLIMITH 0x13
88 #define AMC6821_CONF1_START 0x01
89 #define AMC6821_CONF1_FAN_INT_EN 0x02
90 #define AMC6821_CONF1_FANIE 0x04
91 #define AMC6821_CONF1_PWMINV 0x08
92 #define AMC6821_CONF1_FAN_FAULT_EN 0x10
93 #define AMC6821_CONF1_FDRC0 0x20
94 #define AMC6821_CONF1_FDRC1 0x40
95 #define AMC6821_CONF1_THERMOVIE 0x80
97 #define AMC6821_CONF2_PWM_EN 0x01
98 #define AMC6821_CONF2_TACH_MODE 0x02
99 #define AMC6821_CONF2_TACH_EN 0x04
100 #define AMC6821_CONF2_RTFIE 0x08
101 #define AMC6821_CONF2_LTOIE 0x10
102 #define AMC6821_CONF2_RTOIE 0x20
103 #define AMC6821_CONF2_PSVIE 0x40
104 #define AMC6821_CONF2_RST 0x80
106 #define AMC6821_CONF3_THERM_FAN_EN 0x80
107 #define AMC6821_CONF3_REV_MASK 0x0F
109 #define AMC6821_CONF4_OVREN 0x10
110 #define AMC6821_CONF4_TACH_FAST 0x20
111 #define AMC6821_CONF4_PSPR 0x40
112 #define AMC6821_CONF4_MODE 0x80
114 #define AMC6821_STAT1_RPM_ALARM 0x01
115 #define AMC6821_STAT1_FANS 0x02
116 #define AMC6821_STAT1_RTH 0x04
117 #define AMC6821_STAT1_RTL 0x08
118 #define AMC6821_STAT1_R_THERM 0x10
119 #define AMC6821_STAT1_RTF 0x20
120 #define AMC6821_STAT1_LTH 0x40
121 #define AMC6821_STAT1_LTL 0x80
123 #define AMC6821_STAT2_RTC 0x08
124 #define AMC6821_STAT2_LTC 0x10
125 #define AMC6821_STAT2_LPSV 0x20
126 #define AMC6821_STAT2_L_THERM 0x40
127 #define AMC6821_STAT2_THERM_IN 0x80
129 enum {IDX_TEMP1_INPUT = 0, IDX_TEMP1_MIN, IDX_TEMP1_MAX,
130 IDX_TEMP1_CRIT, IDX_TEMP2_INPUT, IDX_TEMP2_MIN,
131 IDX_TEMP2_MAX, IDX_TEMP2_CRIT,
134 static const u8 temp_reg[] = {AMC6821_REG_LTEMP_HI,
135 AMC6821_REG_LTEMP_LIMIT_MIN,
136 AMC6821_REG_LTEMP_LIMIT_MAX,
137 AMC6821_REG_LTEMP_CRIT,
138 AMC6821_REG_RTEMP_HI,
139 AMC6821_REG_RTEMP_LIMIT_MIN,
140 AMC6821_REG_RTEMP_LIMIT_MAX,
141 AMC6821_REG_RTEMP_CRIT, };
143 enum {IDX_FAN1_INPUT = 0, IDX_FAN1_MIN, IDX_FAN1_MAX,
146 static const u8 fan_reg_low[] = {AMC6821_REG_TDATA_LOW,
147 AMC6821_REG_TACH_LLIMITL,
148 AMC6821_REG_TACH_HLIMITL, };
151 static const u8 fan_reg_hi[] = {AMC6821_REG_TDATA_HI,
152 AMC6821_REG_TACH_LLIMITH,
153 AMC6821_REG_TACH_HLIMITH, };
155 static int amc6821_probe(
156 struct i2c_client *client,
157 const struct i2c_device_id *id);
158 static int amc6821_detect(
159 struct i2c_client *client,
160 struct i2c_board_info *info);
161 static int amc6821_init_client(struct i2c_client *client);
162 static int amc6821_remove(struct i2c_client *client);
163 static struct amc6821_data *amc6821_update_device(struct device *dev);
166 * Driver data (common to all clients)
169 static const struct i2c_device_id amc6821_id[] = {
170 { "amc6821", amc6821 },
174 MODULE_DEVICE_TABLE(i2c, amc6821_id);
176 static struct i2c_driver amc6821_driver = {
177 .class = I2C_CLASS_HWMON,
181 .probe = amc6821_probe,
182 .remove = amc6821_remove,
183 .id_table = amc6821_id,
184 .detect = amc6821_detect,
185 .address_list = normal_i2c,
190 * Client data (each client gets its own)
193 struct amc6821_data {
194 struct device *hwmon_dev;
195 struct mutex update_lock;
196 char valid; /* zero until following fields are valid */
197 unsigned long last_updated; /* in jiffies */
199 /* register values */
200 int temp[TEMP_IDX_LEN];
202 u16 fan[FAN1_IDX_LEN];
206 u8 temp1_auto_point_temp[3];
207 u8 temp2_auto_point_temp[3];
208 u8 pwm1_auto_point_pwm[3];
210 u8 pwm1_auto_channels_temp;
217 static ssize_t get_temp(
219 struct device_attribute *devattr,
222 struct amc6821_data *data = amc6821_update_device(dev);
223 int ix = to_sensor_dev_attr(devattr)->index;
225 return sprintf(buf, "%d\n", data->temp[ix] * 1000);
230 static ssize_t set_temp(
232 struct device_attribute *attr,
236 struct i2c_client *client = to_i2c_client(dev);
237 struct amc6821_data *data = i2c_get_clientdata(client);
238 int ix = to_sensor_dev_attr(attr)->index;
241 int ret = kstrtol(buf, 10, &val);
244 val = clamp_val(val / 1000, -128, 127);
246 mutex_lock(&data->update_lock);
247 data->temp[ix] = val;
248 if (i2c_smbus_write_byte_data(client, temp_reg[ix], data->temp[ix])) {
249 dev_err(&client->dev, "Register write error, aborting.\n");
252 mutex_unlock(&data->update_lock);
259 static ssize_t get_temp_alarm(
261 struct device_attribute *devattr,
264 struct amc6821_data *data = amc6821_update_device(dev);
265 int ix = to_sensor_dev_attr(devattr)->index;
270 flag = data->stat1 & AMC6821_STAT1_LTL;
273 flag = data->stat1 & AMC6821_STAT1_LTH;
276 flag = data->stat2 & AMC6821_STAT2_LTC;
279 flag = data->stat1 & AMC6821_STAT1_RTL;
282 flag = data->stat1 & AMC6821_STAT1_RTH;
285 flag = data->stat2 & AMC6821_STAT2_RTC;
288 dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
292 return sprintf(buf, "1");
294 return sprintf(buf, "0");
300 static ssize_t get_temp2_fault(
302 struct device_attribute *devattr,
305 struct amc6821_data *data = amc6821_update_device(dev);
306 if (data->stat1 & AMC6821_STAT1_RTF)
307 return sprintf(buf, "1");
309 return sprintf(buf, "0");
312 static ssize_t get_pwm1(
314 struct device_attribute *devattr,
317 struct amc6821_data *data = amc6821_update_device(dev);
318 return sprintf(buf, "%d\n", data->pwm1);
321 static ssize_t set_pwm1(
323 struct device_attribute *devattr,
327 struct i2c_client *client = to_i2c_client(dev);
328 struct amc6821_data *data = i2c_get_clientdata(client);
330 int ret = kstrtol(buf, 10, &val);
334 mutex_lock(&data->update_lock);
335 data->pwm1 = clamp_val(val , 0, 255);
336 i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
337 mutex_unlock(&data->update_lock);
341 static ssize_t get_pwm1_enable(
343 struct device_attribute *devattr,
346 struct amc6821_data *data = amc6821_update_device(dev);
347 return sprintf(buf, "%d\n", data->pwm1_enable);
350 static ssize_t set_pwm1_enable(
352 struct device_attribute *attr,
356 struct i2c_client *client = to_i2c_client(dev);
357 struct amc6821_data *data = i2c_get_clientdata(client);
359 int config = kstrtol(buf, 10, &val);
363 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
365 dev_err(&client->dev,
366 "Error reading configuration register, aborting.\n");
372 config &= ~AMC6821_CONF1_FDRC0;
373 config &= ~AMC6821_CONF1_FDRC1;
376 config &= ~AMC6821_CONF1_FDRC0;
377 config |= AMC6821_CONF1_FDRC1;
380 config |= AMC6821_CONF1_FDRC0;
381 config |= AMC6821_CONF1_FDRC1;
386 mutex_lock(&data->update_lock);
387 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF1, config)) {
388 dev_err(&client->dev,
389 "Configuration register write error, aborting.\n");
392 mutex_unlock(&data->update_lock);
397 static ssize_t get_pwm1_auto_channels_temp(
399 struct device_attribute *devattr,
402 struct amc6821_data *data = amc6821_update_device(dev);
403 return sprintf(buf, "%d\n", data->pwm1_auto_channels_temp);
407 static ssize_t get_temp_auto_point_temp(
409 struct device_attribute *devattr,
412 int ix = to_sensor_dev_attr_2(devattr)->index;
413 int nr = to_sensor_dev_attr_2(devattr)->nr;
414 struct amc6821_data *data = amc6821_update_device(dev);
417 return sprintf(buf, "%d\n",
418 data->temp1_auto_point_temp[ix] * 1000);
421 return sprintf(buf, "%d\n",
422 data->temp2_auto_point_temp[ix] * 1000);
425 dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
431 static ssize_t get_pwm1_auto_point_pwm(
433 struct device_attribute *devattr,
436 int ix = to_sensor_dev_attr(devattr)->index;
437 struct amc6821_data *data = amc6821_update_device(dev);
438 return sprintf(buf, "%d\n", data->pwm1_auto_point_pwm[ix]);
442 static inline ssize_t set_slope_register(struct i2c_client *client,
450 dt = ptemp[2]-ptemp[1];
451 for (tmp = 4; tmp > 0; tmp--) {
452 if (dt * (0x20 >> tmp) >= dpwm)
455 tmp |= (ptemp[1] & 0x7C) << 1;
456 if (i2c_smbus_write_byte_data(client,
458 dev_err(&client->dev, "Register write error, aborting.\n");
466 static ssize_t set_temp_auto_point_temp(
468 struct device_attribute *attr,
472 struct i2c_client *client = to_i2c_client(dev);
473 struct amc6821_data *data = amc6821_update_device(dev);
474 int ix = to_sensor_dev_attr_2(attr)->index;
475 int nr = to_sensor_dev_attr_2(attr)->nr;
480 int ret = kstrtol(buf, 10, &val);
486 ptemp = data->temp1_auto_point_temp;
487 reg = AMC6821_REG_LTEMP_FAN_CTRL;
490 ptemp = data->temp2_auto_point_temp;
491 reg = AMC6821_REG_RTEMP_FAN_CTRL;
494 dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
499 mutex_lock(&data->update_lock);
502 ptemp[0] = clamp_val(val / 1000, 0,
503 data->temp1_auto_point_temp[1]);
504 ptemp[0] = clamp_val(ptemp[0], 0,
505 data->temp2_auto_point_temp[1]);
506 ptemp[0] = clamp_val(ptemp[0], 0, 63);
507 if (i2c_smbus_write_byte_data(
509 AMC6821_REG_PSV_TEMP,
511 dev_err(&client->dev,
512 "Register write error, aborting.\n");
518 ptemp[1] = clamp_val(val / 1000, (ptemp[0] & 0x7C) + 4, 124);
520 ptemp[2] = clamp_val(ptemp[2], ptemp[1] + 1, 255);
523 ptemp[2] = clamp_val(val / 1000, ptemp[1]+1, 255);
526 dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
530 dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
531 if (set_slope_register(client, reg, dpwm, ptemp))
535 mutex_unlock(&data->update_lock);
541 static ssize_t set_pwm1_auto_point_pwm(
543 struct device_attribute *attr,
547 struct i2c_client *client = to_i2c_client(dev);
548 struct amc6821_data *data = i2c_get_clientdata(client);
551 int ret = kstrtol(buf, 10, &val);
555 mutex_lock(&data->update_lock);
556 data->pwm1_auto_point_pwm[1] = clamp_val(val, 0, 254);
557 if (i2c_smbus_write_byte_data(client, AMC6821_REG_DCY_LOW_TEMP,
558 data->pwm1_auto_point_pwm[1])) {
559 dev_err(&client->dev, "Register write error, aborting.\n");
563 dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
564 if (set_slope_register(client, AMC6821_REG_LTEMP_FAN_CTRL, dpwm,
565 data->temp1_auto_point_temp)) {
569 if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
570 data->temp2_auto_point_temp)) {
577 mutex_unlock(&data->update_lock);
581 static ssize_t get_fan(
583 struct device_attribute *devattr,
586 struct amc6821_data *data = amc6821_update_device(dev);
587 int ix = to_sensor_dev_attr(devattr)->index;
588 if (0 == data->fan[ix])
589 return sprintf(buf, "0");
590 return sprintf(buf, "%d\n", (int)(6000000 / data->fan[ix]));
595 static ssize_t get_fan1_fault(
597 struct device_attribute *devattr,
600 struct amc6821_data *data = amc6821_update_device(dev);
601 if (data->stat1 & AMC6821_STAT1_FANS)
602 return sprintf(buf, "1");
604 return sprintf(buf, "0");
609 static ssize_t set_fan(
611 struct device_attribute *attr,
612 const char *buf, size_t count)
614 struct i2c_client *client = to_i2c_client(dev);
615 struct amc6821_data *data = i2c_get_clientdata(client);
617 int ix = to_sensor_dev_attr(attr)->index;
618 int ret = kstrtol(buf, 10, &val);
621 val = 1 > val ? 0xFFFF : 6000000/val;
623 mutex_lock(&data->update_lock);
624 data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
625 if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
626 data->fan[ix] & 0xFF)) {
627 dev_err(&client->dev, "Register write error, aborting.\n");
631 if (i2c_smbus_write_byte_data(client,
632 fan_reg_hi[ix], data->fan[ix] >> 8)) {
633 dev_err(&client->dev, "Register write error, aborting.\n");
637 mutex_unlock(&data->update_lock);
643 static ssize_t get_fan1_div(
645 struct device_attribute *devattr,
648 struct amc6821_data *data = amc6821_update_device(dev);
649 return sprintf(buf, "%d\n", data->fan1_div);
652 static ssize_t set_fan1_div(
654 struct device_attribute *attr,
655 const char *buf, size_t count)
657 struct i2c_client *client = to_i2c_client(dev);
658 struct amc6821_data *data = i2c_get_clientdata(client);
660 int config = kstrtol(buf, 10, &val);
664 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
666 dev_err(&client->dev,
667 "Error reading configuration register, aborting.\n");
670 mutex_lock(&data->update_lock);
673 config &= ~AMC6821_CONF4_PSPR;
677 config |= AMC6821_CONF4_PSPR;
684 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
685 dev_err(&client->dev,
686 "Configuration register write error, aborting.\n");
690 mutex_unlock(&data->update_lock);
696 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
697 get_temp, NULL, IDX_TEMP1_INPUT);
698 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, get_temp,
699 set_temp, IDX_TEMP1_MIN);
700 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, get_temp,
701 set_temp, IDX_TEMP1_MAX);
702 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR, get_temp,
703 set_temp, IDX_TEMP1_CRIT);
704 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO,
705 get_temp_alarm, NULL, IDX_TEMP1_MIN);
706 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO,
707 get_temp_alarm, NULL, IDX_TEMP1_MAX);
708 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO,
709 get_temp_alarm, NULL, IDX_TEMP1_CRIT);
710 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO | S_IWUSR,
711 get_temp, NULL, IDX_TEMP2_INPUT);
712 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, get_temp,
713 set_temp, IDX_TEMP2_MIN);
714 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, get_temp,
715 set_temp, IDX_TEMP2_MAX);
716 static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO | S_IWUSR, get_temp,
717 set_temp, IDX_TEMP2_CRIT);
718 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO,
719 get_temp2_fault, NULL, 0);
720 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO,
721 get_temp_alarm, NULL, IDX_TEMP2_MIN);
722 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO,
723 get_temp_alarm, NULL, IDX_TEMP2_MAX);
724 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO,
725 get_temp_alarm, NULL, IDX_TEMP2_CRIT);
726 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, IDX_FAN1_INPUT);
727 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
728 get_fan, set_fan, IDX_FAN1_MIN);
729 static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO | S_IWUSR,
730 get_fan, set_fan, IDX_FAN1_MAX);
731 static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan1_fault, NULL, 0);
732 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR,
733 get_fan1_div, set_fan1_div, 0);
735 static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm1, set_pwm1, 0);
736 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
737 get_pwm1_enable, set_pwm1_enable, 0);
738 static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO,
739 get_pwm1_auto_point_pwm, NULL, 0);
740 static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
741 get_pwm1_auto_point_pwm, set_pwm1_auto_point_pwm, 1);
742 static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO,
743 get_pwm1_auto_point_pwm, NULL, 2);
744 static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
745 get_pwm1_auto_channels_temp, NULL, 0);
746 static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO,
747 get_temp_auto_point_temp, NULL, 1, 0);
748 static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
749 get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 1);
750 static SENSOR_DEVICE_ATTR_2(temp1_auto_point3_temp, S_IWUSR | S_IRUGO,
751 get_temp_auto_point_temp, set_temp_auto_point_temp, 1, 2);
753 static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
754 get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 0);
755 static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
756 get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 1);
757 static SENSOR_DEVICE_ATTR_2(temp2_auto_point3_temp, S_IWUSR | S_IRUGO,
758 get_temp_auto_point_temp, set_temp_auto_point_temp, 2, 2);
762 static struct attribute *amc6821_attrs[] = {
763 &sensor_dev_attr_temp1_input.dev_attr.attr,
764 &sensor_dev_attr_temp1_min.dev_attr.attr,
765 &sensor_dev_attr_temp1_max.dev_attr.attr,
766 &sensor_dev_attr_temp1_crit.dev_attr.attr,
767 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
768 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
769 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
770 &sensor_dev_attr_temp2_input.dev_attr.attr,
771 &sensor_dev_attr_temp2_min.dev_attr.attr,
772 &sensor_dev_attr_temp2_max.dev_attr.attr,
773 &sensor_dev_attr_temp2_crit.dev_attr.attr,
774 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
775 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
776 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
777 &sensor_dev_attr_temp2_fault.dev_attr.attr,
778 &sensor_dev_attr_fan1_input.dev_attr.attr,
779 &sensor_dev_attr_fan1_min.dev_attr.attr,
780 &sensor_dev_attr_fan1_max.dev_attr.attr,
781 &sensor_dev_attr_fan1_fault.dev_attr.attr,
782 &sensor_dev_attr_fan1_div.dev_attr.attr,
783 &sensor_dev_attr_pwm1.dev_attr.attr,
784 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
785 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
786 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
787 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
788 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
789 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
790 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
791 &sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
792 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
793 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
794 &sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
798 static struct attribute_group amc6821_attr_grp = {
799 .attrs = amc6821_attrs,
804 /* Return 0 if detection is successful, -ENODEV otherwise */
805 static int amc6821_detect(
806 struct i2c_client *client,
807 struct i2c_board_info *info)
809 struct i2c_adapter *adapter = client->adapter;
810 int address = client->addr;
813 dev_dbg(&adapter->dev, "amc6821_detect called.\n");
815 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
816 dev_dbg(&adapter->dev,
817 "amc6821: I2C bus doesn't support byte mode, "
822 dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
823 comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
824 if (dev_id != 0x21 || comp_id != 0x49) {
825 dev_dbg(&adapter->dev,
826 "amc6821: detection failed at 0x%02x.\n",
832 * Bit 7 of the address register is ignored, so we can check the
835 dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
836 comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
837 if (dev_id != 0x21 || comp_id != 0x49) {
838 dev_dbg(&adapter->dev,
839 "amc6821: detection failed at 0x%02x.\n",
844 dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
845 strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
850 static int amc6821_probe(
851 struct i2c_client *client,
852 const struct i2c_device_id *id)
854 struct amc6821_data *data;
857 data = devm_kzalloc(&client->dev, sizeof(struct amc6821_data),
862 i2c_set_clientdata(client, data);
863 mutex_init(&data->update_lock);
866 * Initialize the amc6821 chip
868 err = amc6821_init_client(client);
872 err = sysfs_create_group(&client->dev.kobj, &amc6821_attr_grp);
876 data->hwmon_dev = hwmon_device_register(&client->dev);
877 if (!IS_ERR(data->hwmon_dev))
880 err = PTR_ERR(data->hwmon_dev);
881 dev_err(&client->dev, "error registering hwmon device.\n");
882 sysfs_remove_group(&client->dev.kobj, &amc6821_attr_grp);
886 static int amc6821_remove(struct i2c_client *client)
888 struct amc6821_data *data = i2c_get_clientdata(client);
890 hwmon_device_unregister(data->hwmon_dev);
891 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 dev_info(&client->dev, "Revision %d\n", config & 0x0f);
930 config &= ~AMC6821_CONF3_THERM_FAN_EN;
932 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
934 dev_err(&client->dev,
935 "Configuration register write error, aborting.\n");
939 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
942 dev_err(&client->dev,
943 "Error reading configuration register, aborting.\n");
947 config &= ~AMC6821_CONF2_RTFIE;
948 config &= ~AMC6821_CONF2_LTOIE;
949 config &= ~AMC6821_CONF2_RTOIE;
950 if (i2c_smbus_write_byte_data(client,
951 AMC6821_REG_CONF2, config)) {
952 dev_err(&client->dev,
953 "Configuration register write error, aborting.\n");
957 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
960 dev_err(&client->dev,
961 "Error reading configuration register, aborting.\n");
965 config &= ~AMC6821_CONF1_THERMOVIE;
966 config &= ~AMC6821_CONF1_FANIE;
967 config |= AMC6821_CONF1_START;
969 config |= AMC6821_CONF1_PWMINV;
971 config &= ~AMC6821_CONF1_PWMINV;
973 if (i2c_smbus_write_byte_data(
974 client, AMC6821_REG_CONF1, config)) {
975 dev_err(&client->dev,
976 "Configuration register write error, aborting.\n");
984 static struct amc6821_data *amc6821_update_device(struct device *dev)
986 struct i2c_client *client = to_i2c_client(dev);
987 struct amc6821_data *data = i2c_get_clientdata(client);
992 mutex_lock(&data->update_lock);
994 if (time_after(jiffies, data->last_updated + timeout) ||
997 for (i = 0; i < TEMP_IDX_LEN; i++)
998 data->temp[i] = i2c_smbus_read_byte_data(client,
1001 data->stat1 = i2c_smbus_read_byte_data(client,
1003 data->stat2 = i2c_smbus_read_byte_data(client,
1006 data->pwm1 = i2c_smbus_read_byte_data(client,
1008 for (i = 0; i < FAN1_IDX_LEN; i++) {
1009 data->fan[i] = i2c_smbus_read_byte_data(
1012 data->fan[i] += i2c_smbus_read_byte_data(
1014 fan_reg_hi[i]) << 8;
1016 data->fan1_div = i2c_smbus_read_byte_data(client,
1018 data->fan1_div = data->fan1_div & AMC6821_CONF4_PSPR ? 4 : 2;
1020 data->pwm1_auto_point_pwm[0] = 0;
1021 data->pwm1_auto_point_pwm[2] = 255;
1022 data->pwm1_auto_point_pwm[1] = i2c_smbus_read_byte_data(client,
1023 AMC6821_REG_DCY_LOW_TEMP);
1025 data->temp1_auto_point_temp[0] =
1026 i2c_smbus_read_byte_data(client,
1027 AMC6821_REG_PSV_TEMP);
1028 data->temp2_auto_point_temp[0] =
1029 data->temp1_auto_point_temp[0];
1030 reg = i2c_smbus_read_byte_data(client,
1031 AMC6821_REG_LTEMP_FAN_CTRL);
1032 data->temp1_auto_point_temp[1] = (reg & 0xF8) >> 1;
1036 data->temp1_auto_point_temp[2] =
1037 data->temp1_auto_point_temp[1] +
1038 (data->pwm1_auto_point_pwm[2] -
1039 data->pwm1_auto_point_pwm[1]) / reg;
1041 data->temp1_auto_point_temp[2] = 255;
1043 reg = i2c_smbus_read_byte_data(client,
1044 AMC6821_REG_RTEMP_FAN_CTRL);
1045 data->temp2_auto_point_temp[1] = (reg & 0xF8) >> 1;
1049 data->temp2_auto_point_temp[2] =
1050 data->temp2_auto_point_temp[1] +
1051 (data->pwm1_auto_point_pwm[2] -
1052 data->pwm1_auto_point_pwm[1]) / reg;
1054 data->temp2_auto_point_temp[2] = 255;
1056 reg = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
1057 reg = (reg >> 5) & 0x3;
1059 case 0: /*open loop: software sets pwm1*/
1060 data->pwm1_auto_channels_temp = 0;
1061 data->pwm1_enable = 1;
1063 case 2: /*closed loop: remote T (temp2)*/
1064 data->pwm1_auto_channels_temp = 2;
1065 data->pwm1_enable = 2;
1067 case 3: /*closed loop: local and remote T (temp2)*/
1068 data->pwm1_auto_channels_temp = 3;
1069 data->pwm1_enable = 3;
1072 * semi-open loop: software sets rpm, chip controls
1073 * pwm1, currently not implemented
1075 data->pwm1_auto_channels_temp = 0;
1076 data->pwm1_enable = 0;
1080 data->last_updated = jiffies;
1083 mutex_unlock(&data->update_lock);
1087 module_i2c_driver(amc6821_driver);
1089 MODULE_LICENSE("GPL");
1090 MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
1091 MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");