1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
5 * Copyright (C) 2009 T. Mertelj <tomaz.mertelj@guest.arnes.si>
8 * Copyright (C) 2007 Hans J. Koch <hjk@hansjkoch.de>
11 #include <linux/kernel.h> /* Needed for KERN_INFO */
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/jiffies.h>
16 #include <linux/i2c.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/err.h>
20 #include <linux/mutex.h>
26 static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
27 0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
33 static int pwminv; /*Inverted PWM output. */
34 module_param(pwminv, int, 0444);
36 static int init = 1; /*Power-on initialization.*/
37 module_param(init, int, 0444);
39 enum chips { amc6821 };
41 #define AMC6821_REG_DEV_ID 0x3D
42 #define AMC6821_REG_COMP_ID 0x3E
43 #define AMC6821_REG_CONF1 0x00
44 #define AMC6821_REG_CONF2 0x01
45 #define AMC6821_REG_CONF3 0x3F
46 #define AMC6821_REG_CONF4 0x04
47 #define AMC6821_REG_STAT1 0x02
48 #define AMC6821_REG_STAT2 0x03
49 #define AMC6821_REG_TDATA_LOW 0x08
50 #define AMC6821_REG_TDATA_HI 0x09
51 #define AMC6821_REG_LTEMP_HI 0x0A
52 #define AMC6821_REG_RTEMP_HI 0x0B
53 #define AMC6821_REG_LTEMP_LIMIT_MIN 0x15
54 #define AMC6821_REG_LTEMP_LIMIT_MAX 0x14
55 #define AMC6821_REG_RTEMP_LIMIT_MIN 0x19
56 #define AMC6821_REG_RTEMP_LIMIT_MAX 0x18
57 #define AMC6821_REG_LTEMP_CRIT 0x1B
58 #define AMC6821_REG_RTEMP_CRIT 0x1D
59 #define AMC6821_REG_PSV_TEMP 0x1C
60 #define AMC6821_REG_DCY 0x22
61 #define AMC6821_REG_LTEMP_FAN_CTRL 0x24
62 #define AMC6821_REG_RTEMP_FAN_CTRL 0x25
63 #define AMC6821_REG_DCY_LOW_TEMP 0x21
65 #define AMC6821_REG_TACH_LLIMITL 0x10
66 #define AMC6821_REG_TACH_LLIMITH 0x11
67 #define AMC6821_REG_TACH_HLIMITL 0x12
68 #define AMC6821_REG_TACH_HLIMITH 0x13
70 #define AMC6821_CONF1_START 0x01
71 #define AMC6821_CONF1_FAN_INT_EN 0x02
72 #define AMC6821_CONF1_FANIE 0x04
73 #define AMC6821_CONF1_PWMINV 0x08
74 #define AMC6821_CONF1_FAN_FAULT_EN 0x10
75 #define AMC6821_CONF1_FDRC0 0x20
76 #define AMC6821_CONF1_FDRC1 0x40
77 #define AMC6821_CONF1_THERMOVIE 0x80
79 #define AMC6821_CONF2_PWM_EN 0x01
80 #define AMC6821_CONF2_TACH_MODE 0x02
81 #define AMC6821_CONF2_TACH_EN 0x04
82 #define AMC6821_CONF2_RTFIE 0x08
83 #define AMC6821_CONF2_LTOIE 0x10
84 #define AMC6821_CONF2_RTOIE 0x20
85 #define AMC6821_CONF2_PSVIE 0x40
86 #define AMC6821_CONF2_RST 0x80
88 #define AMC6821_CONF3_THERM_FAN_EN 0x80
89 #define AMC6821_CONF3_REV_MASK 0x0F
91 #define AMC6821_CONF4_OVREN 0x10
92 #define AMC6821_CONF4_TACH_FAST 0x20
93 #define AMC6821_CONF4_PSPR 0x40
94 #define AMC6821_CONF4_MODE 0x80
96 #define AMC6821_STAT1_RPM_ALARM 0x01
97 #define AMC6821_STAT1_FANS 0x02
98 #define AMC6821_STAT1_RTH 0x04
99 #define AMC6821_STAT1_RTL 0x08
100 #define AMC6821_STAT1_R_THERM 0x10
101 #define AMC6821_STAT1_RTF 0x20
102 #define AMC6821_STAT1_LTH 0x40
103 #define AMC6821_STAT1_LTL 0x80
105 #define AMC6821_STAT2_RTC 0x08
106 #define AMC6821_STAT2_LTC 0x10
107 #define AMC6821_STAT2_LPSV 0x20
108 #define AMC6821_STAT2_L_THERM 0x40
109 #define AMC6821_STAT2_THERM_IN 0x80
111 enum {IDX_TEMP1_INPUT = 0, IDX_TEMP1_MIN, IDX_TEMP1_MAX,
112 IDX_TEMP1_CRIT, IDX_TEMP2_INPUT, IDX_TEMP2_MIN,
113 IDX_TEMP2_MAX, IDX_TEMP2_CRIT,
116 static const u8 temp_reg[] = {AMC6821_REG_LTEMP_HI,
117 AMC6821_REG_LTEMP_LIMIT_MIN,
118 AMC6821_REG_LTEMP_LIMIT_MAX,
119 AMC6821_REG_LTEMP_CRIT,
120 AMC6821_REG_RTEMP_HI,
121 AMC6821_REG_RTEMP_LIMIT_MIN,
122 AMC6821_REG_RTEMP_LIMIT_MAX,
123 AMC6821_REG_RTEMP_CRIT, };
125 enum {IDX_FAN1_INPUT = 0, IDX_FAN1_MIN, IDX_FAN1_MAX,
128 static const u8 fan_reg_low[] = {AMC6821_REG_TDATA_LOW,
129 AMC6821_REG_TACH_LLIMITL,
130 AMC6821_REG_TACH_HLIMITL, };
133 static const u8 fan_reg_hi[] = {AMC6821_REG_TDATA_HI,
134 AMC6821_REG_TACH_LLIMITH,
135 AMC6821_REG_TACH_HLIMITH, };
138 * Client data (each client gets its own)
141 struct amc6821_data {
142 struct i2c_client *client;
143 struct mutex update_lock;
144 char valid; /* zero until following fields are valid */
145 unsigned long last_updated; /* in jiffies */
147 /* register values */
148 int temp[TEMP_IDX_LEN];
150 u16 fan[FAN1_IDX_LEN];
154 u8 temp1_auto_point_temp[3];
155 u8 temp2_auto_point_temp[3];
156 u8 pwm1_auto_point_pwm[3];
158 u8 pwm1_auto_channels_temp;
164 static struct amc6821_data *amc6821_update_device(struct device *dev)
166 struct amc6821_data *data = dev_get_drvdata(dev);
167 struct i2c_client *client = data->client;
172 mutex_lock(&data->update_lock);
174 if (time_after(jiffies, data->last_updated + timeout) ||
177 for (i = 0; i < TEMP_IDX_LEN; i++)
178 data->temp[i] = (int8_t)i2c_smbus_read_byte_data(
179 client, temp_reg[i]);
181 data->stat1 = i2c_smbus_read_byte_data(client,
183 data->stat2 = i2c_smbus_read_byte_data(client,
186 data->pwm1 = i2c_smbus_read_byte_data(client,
188 for (i = 0; i < FAN1_IDX_LEN; i++) {
189 data->fan[i] = i2c_smbus_read_byte_data(
192 data->fan[i] += i2c_smbus_read_byte_data(
196 data->fan1_div = i2c_smbus_read_byte_data(client,
198 data->fan1_div = data->fan1_div & AMC6821_CONF4_PSPR ? 4 : 2;
200 data->pwm1_auto_point_pwm[0] = 0;
201 data->pwm1_auto_point_pwm[2] = 255;
202 data->pwm1_auto_point_pwm[1] = i2c_smbus_read_byte_data(client,
203 AMC6821_REG_DCY_LOW_TEMP);
205 data->temp1_auto_point_temp[0] =
206 i2c_smbus_read_byte_data(client,
207 AMC6821_REG_PSV_TEMP);
208 data->temp2_auto_point_temp[0] =
209 data->temp1_auto_point_temp[0];
210 reg = i2c_smbus_read_byte_data(client,
211 AMC6821_REG_LTEMP_FAN_CTRL);
212 data->temp1_auto_point_temp[1] = (reg & 0xF8) >> 1;
216 data->temp1_auto_point_temp[2] =
217 data->temp1_auto_point_temp[1] +
218 (data->pwm1_auto_point_pwm[2] -
219 data->pwm1_auto_point_pwm[1]) / reg;
221 data->temp1_auto_point_temp[2] = 255;
223 reg = i2c_smbus_read_byte_data(client,
224 AMC6821_REG_RTEMP_FAN_CTRL);
225 data->temp2_auto_point_temp[1] = (reg & 0xF8) >> 1;
229 data->temp2_auto_point_temp[2] =
230 data->temp2_auto_point_temp[1] +
231 (data->pwm1_auto_point_pwm[2] -
232 data->pwm1_auto_point_pwm[1]) / reg;
234 data->temp2_auto_point_temp[2] = 255;
236 reg = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
237 reg = (reg >> 5) & 0x3;
239 case 0: /*open loop: software sets pwm1*/
240 data->pwm1_auto_channels_temp = 0;
241 data->pwm1_enable = 1;
243 case 2: /*closed loop: remote T (temp2)*/
244 data->pwm1_auto_channels_temp = 2;
245 data->pwm1_enable = 2;
247 case 3: /*closed loop: local and remote T (temp2)*/
248 data->pwm1_auto_channels_temp = 3;
249 data->pwm1_enable = 3;
252 * semi-open loop: software sets rpm, chip controls
253 * pwm1, currently not implemented
255 data->pwm1_auto_channels_temp = 0;
256 data->pwm1_enable = 0;
260 data->last_updated = jiffies;
263 mutex_unlock(&data->update_lock);
267 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
270 struct amc6821_data *data = amc6821_update_device(dev);
271 int ix = to_sensor_dev_attr(devattr)->index;
273 return sprintf(buf, "%d\n", data->temp[ix] * 1000);
276 static ssize_t temp_store(struct device *dev, struct device_attribute *attr,
277 const char *buf, size_t count)
279 struct amc6821_data *data = dev_get_drvdata(dev);
280 struct i2c_client *client = data->client;
281 int ix = to_sensor_dev_attr(attr)->index;
284 int ret = kstrtol(buf, 10, &val);
287 val = clamp_val(val / 1000, -128, 127);
289 mutex_lock(&data->update_lock);
290 data->temp[ix] = val;
291 if (i2c_smbus_write_byte_data(client, temp_reg[ix], data->temp[ix])) {
292 dev_err(&client->dev, "Register write error, aborting.\n");
295 mutex_unlock(&data->update_lock);
299 static ssize_t temp_alarm_show(struct device *dev,
300 struct device_attribute *devattr, char *buf)
302 struct amc6821_data *data = amc6821_update_device(dev);
303 int ix = to_sensor_dev_attr(devattr)->index;
308 flag = data->stat1 & AMC6821_STAT1_LTL;
311 flag = data->stat1 & AMC6821_STAT1_LTH;
314 flag = data->stat2 & AMC6821_STAT2_LTC;
317 flag = data->stat1 & AMC6821_STAT1_RTL;
320 flag = data->stat1 & AMC6821_STAT1_RTH;
323 flag = data->stat2 & AMC6821_STAT2_RTC;
326 dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
330 return sprintf(buf, "1");
332 return sprintf(buf, "0");
335 static ssize_t temp2_fault_show(struct device *dev,
336 struct device_attribute *devattr, char *buf)
338 struct amc6821_data *data = amc6821_update_device(dev);
339 if (data->stat1 & AMC6821_STAT1_RTF)
340 return sprintf(buf, "1");
342 return sprintf(buf, "0");
345 static ssize_t pwm1_show(struct device *dev, struct device_attribute *devattr,
348 struct amc6821_data *data = amc6821_update_device(dev);
349 return sprintf(buf, "%d\n", data->pwm1);
352 static ssize_t pwm1_store(struct device *dev,
353 struct device_attribute *devattr, const char *buf,
356 struct amc6821_data *data = dev_get_drvdata(dev);
357 struct i2c_client *client = data->client;
359 int ret = kstrtol(buf, 10, &val);
363 mutex_lock(&data->update_lock);
364 data->pwm1 = clamp_val(val , 0, 255);
365 i2c_smbus_write_byte_data(client, AMC6821_REG_DCY, data->pwm1);
366 mutex_unlock(&data->update_lock);
370 static ssize_t pwm1_enable_show(struct device *dev,
371 struct device_attribute *devattr, char *buf)
373 struct amc6821_data *data = amc6821_update_device(dev);
374 return sprintf(buf, "%d\n", data->pwm1_enable);
377 static ssize_t pwm1_enable_store(struct device *dev,
378 struct device_attribute *attr,
379 const char *buf, size_t count)
381 struct amc6821_data *data = dev_get_drvdata(dev);
382 struct i2c_client *client = data->client;
384 int config = kstrtol(buf, 10, &val);
388 mutex_lock(&data->update_lock);
389 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
391 dev_err(&client->dev,
392 "Error reading configuration register, aborting.\n");
399 config &= ~AMC6821_CONF1_FDRC0;
400 config &= ~AMC6821_CONF1_FDRC1;
403 config &= ~AMC6821_CONF1_FDRC0;
404 config |= AMC6821_CONF1_FDRC1;
407 config |= AMC6821_CONF1_FDRC0;
408 config |= AMC6821_CONF1_FDRC1;
414 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF1, config)) {
415 dev_err(&client->dev,
416 "Configuration register write error, aborting.\n");
420 mutex_unlock(&data->update_lock);
424 static ssize_t pwm1_auto_channels_temp_show(struct device *dev,
425 struct device_attribute *devattr,
428 struct amc6821_data *data = amc6821_update_device(dev);
429 return sprintf(buf, "%d\n", data->pwm1_auto_channels_temp);
432 static ssize_t temp_auto_point_temp_show(struct device *dev,
433 struct device_attribute *devattr,
436 int ix = to_sensor_dev_attr_2(devattr)->index;
437 int nr = to_sensor_dev_attr_2(devattr)->nr;
438 struct amc6821_data *data = amc6821_update_device(dev);
441 return sprintf(buf, "%d\n",
442 data->temp1_auto_point_temp[ix] * 1000);
444 return sprintf(buf, "%d\n",
445 data->temp2_auto_point_temp[ix] * 1000);
447 dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
452 static ssize_t pwm1_auto_point_pwm_show(struct device *dev,
453 struct device_attribute *devattr,
456 int ix = to_sensor_dev_attr(devattr)->index;
457 struct amc6821_data *data = amc6821_update_device(dev);
458 return sprintf(buf, "%d\n", data->pwm1_auto_point_pwm[ix]);
461 static inline ssize_t set_slope_register(struct i2c_client *client,
469 dt = ptemp[2]-ptemp[1];
470 for (tmp = 4; tmp > 0; tmp--) {
471 if (dt * (0x20 >> tmp) >= dpwm)
474 tmp |= (ptemp[1] & 0x7C) << 1;
475 if (i2c_smbus_write_byte_data(client,
477 dev_err(&client->dev, "Register write error, aborting.\n");
483 static ssize_t temp_auto_point_temp_store(struct device *dev,
484 struct device_attribute *attr,
485 const char *buf, size_t count)
487 struct amc6821_data *data = amc6821_update_device(dev);
488 struct i2c_client *client = data->client;
489 int ix = to_sensor_dev_attr_2(attr)->index;
490 int nr = to_sensor_dev_attr_2(attr)->nr;
495 int ret = kstrtol(buf, 10, &val);
501 ptemp = data->temp1_auto_point_temp;
502 reg = AMC6821_REG_LTEMP_FAN_CTRL;
505 ptemp = data->temp2_auto_point_temp;
506 reg = AMC6821_REG_RTEMP_FAN_CTRL;
509 dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
513 mutex_lock(&data->update_lock);
518 ptemp[0] = clamp_val(val / 1000, 0,
519 data->temp1_auto_point_temp[1]);
520 ptemp[0] = clamp_val(ptemp[0], 0,
521 data->temp2_auto_point_temp[1]);
522 ptemp[0] = clamp_val(ptemp[0], 0, 63);
523 if (i2c_smbus_write_byte_data(
525 AMC6821_REG_PSV_TEMP,
527 dev_err(&client->dev,
528 "Register write error, aborting.\n");
533 ptemp[1] = clamp_val(val / 1000, (ptemp[0] & 0x7C) + 4, 124);
535 ptemp[2] = clamp_val(ptemp[2], ptemp[1] + 1, 255);
538 ptemp[2] = clamp_val(val / 1000, ptemp[1]+1, 255);
541 dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
545 dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
546 if (set_slope_register(client, reg, dpwm, ptemp))
550 mutex_unlock(&data->update_lock);
554 static ssize_t pwm1_auto_point_pwm_store(struct device *dev,
555 struct device_attribute *attr,
556 const char *buf, size_t count)
558 struct amc6821_data *data = dev_get_drvdata(dev);
559 struct i2c_client *client = data->client;
562 int ret = kstrtol(buf, 10, &val);
566 mutex_lock(&data->update_lock);
567 data->pwm1_auto_point_pwm[1] = clamp_val(val, 0, 254);
568 if (i2c_smbus_write_byte_data(client, AMC6821_REG_DCY_LOW_TEMP,
569 data->pwm1_auto_point_pwm[1])) {
570 dev_err(&client->dev, "Register write error, aborting.\n");
574 dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
575 if (set_slope_register(client, AMC6821_REG_LTEMP_FAN_CTRL, dpwm,
576 data->temp1_auto_point_temp)) {
580 if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
581 data->temp2_auto_point_temp)) {
588 mutex_unlock(&data->update_lock);
592 static ssize_t fan_show(struct device *dev, struct device_attribute *devattr,
595 struct amc6821_data *data = amc6821_update_device(dev);
596 int ix = to_sensor_dev_attr(devattr)->index;
597 if (0 == data->fan[ix])
598 return sprintf(buf, "0");
599 return sprintf(buf, "%d\n", (int)(6000000 / data->fan[ix]));
602 static ssize_t fan1_fault_show(struct device *dev,
603 struct device_attribute *devattr, char *buf)
605 struct amc6821_data *data = amc6821_update_device(dev);
606 if (data->stat1 & AMC6821_STAT1_FANS)
607 return sprintf(buf, "1");
609 return sprintf(buf, "0");
612 static ssize_t fan_store(struct device *dev, struct device_attribute *attr,
613 const char *buf, size_t count)
615 struct amc6821_data *data = dev_get_drvdata(dev);
616 struct i2c_client *client = data->client;
618 int ix = to_sensor_dev_attr(attr)->index;
619 int ret = kstrtol(buf, 10, &val);
622 val = 1 > val ? 0xFFFF : 6000000/val;
624 mutex_lock(&data->update_lock);
625 data->fan[ix] = (u16) clamp_val(val, 1, 0xFFFF);
626 if (i2c_smbus_write_byte_data(client, fan_reg_low[ix],
627 data->fan[ix] & 0xFF)) {
628 dev_err(&client->dev, "Register write error, aborting.\n");
632 if (i2c_smbus_write_byte_data(client,
633 fan_reg_hi[ix], data->fan[ix] >> 8)) {
634 dev_err(&client->dev, "Register write error, aborting.\n");
638 mutex_unlock(&data->update_lock);
642 static ssize_t fan1_div_show(struct device *dev,
643 struct device_attribute *devattr, char *buf)
645 struct amc6821_data *data = amc6821_update_device(dev);
646 return sprintf(buf, "%d\n", data->fan1_div);
649 static ssize_t fan1_div_store(struct device *dev,
650 struct device_attribute *attr, const char *buf,
653 struct amc6821_data *data = dev_get_drvdata(dev);
654 struct i2c_client *client = data->client;
656 int config = kstrtol(buf, 10, &val);
660 mutex_lock(&data->update_lock);
661 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
663 dev_err(&client->dev,
664 "Error reading configuration register, aborting.\n");
670 config &= ~AMC6821_CONF4_PSPR;
674 config |= AMC6821_CONF4_PSPR;
681 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
682 dev_err(&client->dev,
683 "Configuration register write error, aborting.\n");
687 mutex_unlock(&data->update_lock);
691 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, IDX_TEMP1_INPUT);
692 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp, IDX_TEMP1_MIN);
693 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, IDX_TEMP1_MAX);
694 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, IDX_TEMP1_CRIT);
695 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, temp_alarm, IDX_TEMP1_MIN);
696 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, temp_alarm, IDX_TEMP1_MAX);
697 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, temp_alarm, IDX_TEMP1_CRIT);
698 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, IDX_TEMP2_INPUT);
699 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp, IDX_TEMP2_MIN);
700 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp, IDX_TEMP2_MAX);
701 static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp, IDX_TEMP2_CRIT);
702 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp2_fault, 0);
703 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, temp_alarm, IDX_TEMP2_MIN);
704 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, temp_alarm, IDX_TEMP2_MAX);
705 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, temp_alarm, IDX_TEMP2_CRIT);
706 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, IDX_FAN1_INPUT);
707 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan, IDX_FAN1_MIN);
708 static SENSOR_DEVICE_ATTR_RW(fan1_max, fan, IDX_FAN1_MAX);
709 static SENSOR_DEVICE_ATTR_RO(fan1_fault, fan1_fault, 0);
710 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan1_div, 0);
712 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm1, 0);
713 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm1_enable, 0);
714 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm1_auto_point_pwm, 0);
715 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_pwm, pwm1_auto_point_pwm, 1);
716 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm1_auto_point_pwm, 2);
717 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_channels_temp, pwm1_auto_channels_temp,
719 static SENSOR_DEVICE_ATTR_2_RO(temp1_auto_point1_temp, temp_auto_point_temp,
721 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point2_temp, temp_auto_point_temp,
723 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_point3_temp, temp_auto_point_temp,
726 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point1_temp, temp_auto_point_temp,
728 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point2_temp, temp_auto_point_temp,
730 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_point3_temp, temp_auto_point_temp,
733 static struct attribute *amc6821_attrs[] = {
734 &sensor_dev_attr_temp1_input.dev_attr.attr,
735 &sensor_dev_attr_temp1_min.dev_attr.attr,
736 &sensor_dev_attr_temp1_max.dev_attr.attr,
737 &sensor_dev_attr_temp1_crit.dev_attr.attr,
738 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
739 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
740 &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
741 &sensor_dev_attr_temp2_input.dev_attr.attr,
742 &sensor_dev_attr_temp2_min.dev_attr.attr,
743 &sensor_dev_attr_temp2_max.dev_attr.attr,
744 &sensor_dev_attr_temp2_crit.dev_attr.attr,
745 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
746 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
747 &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
748 &sensor_dev_attr_temp2_fault.dev_attr.attr,
749 &sensor_dev_attr_fan1_input.dev_attr.attr,
750 &sensor_dev_attr_fan1_min.dev_attr.attr,
751 &sensor_dev_attr_fan1_max.dev_attr.attr,
752 &sensor_dev_attr_fan1_fault.dev_attr.attr,
753 &sensor_dev_attr_fan1_div.dev_attr.attr,
754 &sensor_dev_attr_pwm1.dev_attr.attr,
755 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
756 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
757 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
758 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
759 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
760 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
761 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
762 &sensor_dev_attr_temp1_auto_point3_temp.dev_attr.attr,
763 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
764 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
765 &sensor_dev_attr_temp2_auto_point3_temp.dev_attr.attr,
769 ATTRIBUTE_GROUPS(amc6821);
771 /* Return 0 if detection is successful, -ENODEV otherwise */
772 static int amc6821_detect(
773 struct i2c_client *client,
774 struct i2c_board_info *info)
776 struct i2c_adapter *adapter = client->adapter;
777 int address = client->addr;
780 dev_dbg(&adapter->dev, "amc6821_detect called.\n");
782 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
783 dev_dbg(&adapter->dev,
784 "amc6821: I2C bus doesn't support byte mode, "
789 dev_id = i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID);
790 comp_id = i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID);
791 if (dev_id != 0x21 || comp_id != 0x49) {
792 dev_dbg(&adapter->dev,
793 "amc6821: detection failed at 0x%02x.\n",
799 * Bit 7 of the address register is ignored, so we can check the
802 dev_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_DEV_ID);
803 comp_id = i2c_smbus_read_byte_data(client, 0x80 | AMC6821_REG_COMP_ID);
804 if (dev_id != 0x21 || comp_id != 0x49) {
805 dev_dbg(&adapter->dev,
806 "amc6821: detection failed at 0x%02x.\n",
811 dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
812 strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
817 static int amc6821_init_client(struct i2c_client *client)
823 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
826 dev_err(&client->dev,
827 "Error reading configuration register, aborting.\n");
831 config |= AMC6821_CONF4_MODE;
833 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4,
835 dev_err(&client->dev,
836 "Configuration register write error, aborting.\n");
840 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF3);
843 dev_err(&client->dev,
844 "Error reading configuration register, aborting.\n");
848 dev_info(&client->dev, "Revision %d\n", config & 0x0f);
850 config &= ~AMC6821_CONF3_THERM_FAN_EN;
852 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
854 dev_err(&client->dev,
855 "Configuration register write error, aborting.\n");
859 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
862 dev_err(&client->dev,
863 "Error reading configuration register, aborting.\n");
867 config &= ~AMC6821_CONF2_RTFIE;
868 config &= ~AMC6821_CONF2_LTOIE;
869 config &= ~AMC6821_CONF2_RTOIE;
870 if (i2c_smbus_write_byte_data(client,
871 AMC6821_REG_CONF2, config)) {
872 dev_err(&client->dev,
873 "Configuration register write error, aborting.\n");
877 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
880 dev_err(&client->dev,
881 "Error reading configuration register, aborting.\n");
885 config &= ~AMC6821_CONF1_THERMOVIE;
886 config &= ~AMC6821_CONF1_FANIE;
887 config |= AMC6821_CONF1_START;
889 config |= AMC6821_CONF1_PWMINV;
891 config &= ~AMC6821_CONF1_PWMINV;
893 if (i2c_smbus_write_byte_data(
894 client, AMC6821_REG_CONF1, config)) {
895 dev_err(&client->dev,
896 "Configuration register write error, aborting.\n");
903 static int amc6821_probe(struct i2c_client *client)
905 struct device *dev = &client->dev;
906 struct amc6821_data *data;
907 struct device *hwmon_dev;
910 data = devm_kzalloc(dev, sizeof(struct amc6821_data), GFP_KERNEL);
914 data->client = client;
915 mutex_init(&data->update_lock);
918 * Initialize the amc6821 chip
920 err = amc6821_init_client(client);
924 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
927 return PTR_ERR_OR_ZERO(hwmon_dev);
930 static const struct i2c_device_id amc6821_id[] = {
931 { "amc6821", amc6821 },
935 MODULE_DEVICE_TABLE(i2c, amc6821_id);
937 static struct i2c_driver amc6821_driver = {
938 .class = I2C_CLASS_HWMON,
942 .probe_new = amc6821_probe,
943 .id_table = amc6821_id,
944 .detect = amc6821_detect,
945 .address_list = normal_i2c,
948 module_i2c_driver(amc6821_driver);
950 MODULE_LICENSE("GPL");
951 MODULE_AUTHOR("T. Mertelj <tomaz.mertelj@guest.arnes.si>");
952 MODULE_DESCRIPTION("Texas Instruments amc6821 hwmon driver");