mm-only debug patch...
[mmotm.git] / drivers / hwmon / amc6821.c
blob26e7635893e956f4b1ef8529de5f23c5b16b785d
1 /*
2 amc6821.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
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>
35 * Addresses to scan.
38 static const unsigned short normal_i2c[] = {0x18, 0x19, 0x1a, 0x2c, 0x2d, 0x2e,
39 0x4c, 0x4d, 0x4e, I2C_CLIENT_END};
44 * Insmod parameters
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,
129 TEMP_IDX_LEN, };
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,
141 FAN1_IDX_LEN, };
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,
157 int kind,
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,
176 .driver = {
177 .name = "amc6821",
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];
201 u8 fan1_div;
203 u8 pwm1;
204 u8 temp1_auto_point_temp[3];
205 u8 temp2_auto_point_temp[3];
206 u8 pwm1_auto_point_pwm[3];
207 u8 pwm1_enable;
208 u8 pwm1_auto_channels_temp;
210 u8 stat1;
211 u8 stat2;
215 static ssize_t get_temp(
216 struct device *dev,
217 struct device_attribute *devattr,
218 char *buf)
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(
229 struct device *dev,
230 struct device_attribute *attr,
231 const char *buf,
232 size_t count)
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;
237 long val;
239 int ret = strict_strtol(buf, 10, &val);
240 if (ret)
241 return ret;
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");
248 count = -EIO;
250 mutex_unlock(&data->update_lock);
251 return count;
257 static ssize_t get_temp_alarm(
258 struct device *dev,
259 struct device_attribute *devattr,
260 char *buf)
262 struct amc6821_data *data = amc6821_update_device(dev);
263 int ix = to_sensor_dev_attr(devattr)->index;
264 u8 flag;
266 switch (ix) {
267 case IDX_TEMP1_MIN:
268 flag = data->stat1 & AMC6821_STAT1_LTL;
269 break;
270 case IDX_TEMP1_MAX:
271 flag = data->stat1 & AMC6821_STAT1_LTH;
272 break;
273 case IDX_TEMP1_CRIT:
274 flag = data->stat2 & AMC6821_STAT2_LTC;
275 break;
276 case IDX_TEMP2_MIN:
277 flag = data->stat1 & AMC6821_STAT1_RTL;
278 break;
279 case IDX_TEMP2_MAX:
280 flag = data->stat1 & AMC6821_STAT1_RTH;
281 break;
282 case IDX_TEMP2_CRIT:
283 flag = data->stat2 & AMC6821_STAT2_RTC;
284 break;
285 default:
286 dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
287 return -EINVAL;
289 if (flag)
290 return sprintf(buf, "1");
291 else
292 return sprintf(buf, "0");
298 static ssize_t get_temp2_fault(
299 struct device *dev,
300 struct device_attribute *devattr,
301 char *buf)
303 struct amc6821_data *data = amc6821_update_device(dev);
304 if (data->stat1 & AMC6821_STAT1_RTF)
305 return sprintf(buf, "1");
306 else
307 return sprintf(buf, "0");
310 static ssize_t get_pwm1(
311 struct device *dev,
312 struct device_attribute *devattr,
313 char *buf)
315 struct amc6821_data *data = amc6821_update_device(dev);
316 return sprintf(buf, "%d\n", data->pwm1);
319 static ssize_t set_pwm1(
320 struct device *dev,
321 struct device_attribute *devattr,
322 const char *buf,
323 size_t count)
325 struct i2c_client *client = to_i2c_client(dev);
326 struct amc6821_data *data = i2c_get_clientdata(client);
327 long val;
328 int ret = strict_strtol(buf, 10, &val);
329 if (ret)
330 return ret;
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);
336 return count;
339 static ssize_t get_pwm1_enable(
340 struct device *dev,
341 struct device_attribute *devattr,
342 char *buf)
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(
349 struct device *dev,
350 struct device_attribute *attr,
351 const char *buf,
352 size_t count)
354 struct i2c_client *client = to_i2c_client(dev);
355 struct amc6821_data *data = i2c_get_clientdata(client);
356 long val;
357 int config = strict_strtol(buf, 10, &val);
358 if (config)
359 return config;
361 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
362 if (config < 0) {
363 dev_err(&client->dev,
364 "Error reading configuration register, aborting.\n");
365 return -EIO;
368 switch (val) {
369 case 1:
370 config &= ~AMC6821_CONF1_FDRC0;
371 config &= ~AMC6821_CONF1_FDRC1;
372 break;
373 case 2:
374 config &= ~AMC6821_CONF1_FDRC0;
375 config |= AMC6821_CONF1_FDRC1;
376 break;
377 case 3:
378 config |= AMC6821_CONF1_FDRC0;
379 config |= AMC6821_CONF1_FDRC1;
380 break;
381 default:
382 return -EINVAL;
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");
388 count = -EIO;
390 mutex_unlock(&data->update_lock);
391 return count;
395 static ssize_t get_pwm1_auto_channels_temp(
396 struct device *dev,
397 struct device_attribute *devattr,
398 char *buf)
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(
406 struct device *dev,
407 struct device_attribute *devattr,
408 char *buf)
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);
413 switch (nr) {
414 case 1:
415 return sprintf(buf, "%d\n",
416 data->temp1_auto_point_temp[ix] * 1000);
417 break;
418 case 2:
419 return sprintf(buf, "%d\n",
420 data->temp2_auto_point_temp[ix] * 1000);
421 break;
422 default:
423 dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
424 return -EINVAL;
429 static ssize_t get_pwm1_auto_point_pwm(
430 struct device *dev,
431 struct device_attribute *devattr,
432 char *buf)
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,
441 u8 reg,
442 u8 dpwm,
443 u8 *ptemp)
445 int dt;
446 u8 tmp;
448 dt = ptemp[2]-ptemp[1];
449 for (tmp = 4; tmp > 0; tmp--) {
450 if (dt * (0x20 >> tmp) >= dpwm)
451 break;
453 tmp |= (ptemp[1] & 0x7C) << 1;
454 if (i2c_smbus_write_byte_data(client,
455 reg, tmp)) {
456 dev_err(&client->dev, "Register write error, aborting.\n");
457 return -EIO;
459 return 0;
464 static ssize_t set_temp_auto_point_temp(
465 struct device *dev,
466 struct device_attribute *attr,
467 const char *buf,
468 size_t count)
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;
474 u8 *ptemp;
475 u8 reg;
476 int dpwm;
477 long val;
478 int ret = strict_strtol(buf, 10, &val);
479 if (ret)
480 return ret;
482 switch (nr) {
483 case 1:
484 ptemp = data->temp1_auto_point_temp;
485 reg = AMC6821_REG_LTEMP_FAN_CTRL;
486 break;
487 case 2:
488 ptemp = data->temp2_auto_point_temp;
489 reg = AMC6821_REG_RTEMP_FAN_CTRL;
490 break;
491 default:
492 dev_dbg(dev, "Unknown attr->nr (%d).\n", nr);
493 return -EINVAL;
496 data->valid = 0;
497 mutex_lock(&data->update_lock);
498 switch (ix) {
499 case 0:
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(
506 client,
507 AMC6821_REG_PSV_TEMP,
508 ptemp[0])) {
509 dev_err(&client->dev,
510 "Register write error, aborting.\n");
511 count = -EIO;
513 goto EXIT;
514 break;
515 case 1:
516 ptemp[1] = SENSORS_LIMIT(
517 val / 1000,
518 (ptemp[0] & 0x7C) + 4,
519 124);
520 ptemp[1] &= 0x7C;
521 ptemp[2] = SENSORS_LIMIT(
522 ptemp[2], ptemp[1] + 1,
523 255);
524 break;
525 case 2:
526 ptemp[2] = SENSORS_LIMIT(
527 val / 1000,
528 ptemp[1]+1,
529 255);
530 break;
531 default:
532 dev_dbg(dev, "Unknown attr->index (%d).\n", ix);
533 count = -EINVAL;
534 goto EXIT;
536 dpwm = data->pwm1_auto_point_pwm[2] - data->pwm1_auto_point_pwm[1];
537 if (set_slope_register(client, reg, dpwm, ptemp))
538 count = -EIO;
540 EXIT:
541 mutex_unlock(&data->update_lock);
542 return count;
547 static ssize_t set_pwm1_auto_point_pwm(
548 struct device *dev,
549 struct device_attribute *attr,
550 const char *buf,
551 size_t count)
553 struct i2c_client *client = to_i2c_client(dev);
554 struct amc6821_data *data = i2c_get_clientdata(client);
555 int dpwm;
556 long val;
557 int ret = strict_strtol(buf, 10, &val);
558 if (ret)
559 return ret;
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");
566 count = -EIO;
567 goto EXIT;
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)) {
572 count = -EIO;
573 goto EXIT;
575 if (set_slope_register(client, AMC6821_REG_RTEMP_FAN_CTRL, dpwm,
576 data->temp2_auto_point_temp)) {
577 count = -EIO;
578 goto EXIT;
581 EXIT:
582 data->valid = 0;
583 mutex_unlock(&data->update_lock);
584 return count;
587 static ssize_t get_fan(
588 struct device *dev,
589 struct device_attribute *devattr,
590 char *buf)
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(
602 struct device *dev,
603 struct device_attribute *devattr,
604 char *buf)
606 struct amc6821_data *data = amc6821_update_device(dev);
607 if (data->stat1 & AMC6821_STAT1_FANS)
608 return sprintf(buf, "1");
609 else
610 return sprintf(buf, "0");
615 static ssize_t set_fan(
616 struct device *dev,
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);
622 long val;
623 int ix = to_sensor_dev_attr(attr)->index;
624 int ret = strict_strtol(buf, 10, &val);
625 if (ret)
626 return ret;
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");
634 count = -EIO;
635 goto EXIT;
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");
640 count = -EIO;
642 EXIT:
643 mutex_unlock(&data->update_lock);
644 return count;
649 static ssize_t get_fan1_div(
650 struct device *dev,
651 struct device_attribute *devattr,
652 char *buf)
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(
659 struct device *dev,
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);
665 long val;
666 int config = strict_strtol(buf, 10, &val);
667 if (config)
668 return config;
670 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
671 if (config < 0) {
672 dev_err(&client->dev,
673 "Error reading configuration register, aborting.\n");
674 return -EIO;
676 mutex_lock(&data->update_lock);
677 switch (val) {
678 case 2:
679 config &= ~AMC6821_CONF4_PSPR;
680 data->fan1_div = 2;
681 break;
682 case 4:
683 config |= AMC6821_CONF4_PSPR;
684 data->fan1_div = 4;
685 break;
686 default:
687 mutex_unlock(&data->update_lock);
688 count = -EINVAL;
689 goto EXIT;
691 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4, config)) {
692 dev_err(&client->dev,
693 "Configuration register write error, aborting.\n");
694 count = -EIO;
696 EXIT:
697 mutex_unlock(&data->update_lock);
698 return count;
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,
802 NULL
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,
814 int kind,
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");
825 return -ENODEV;
827 if ((kind < 0) &&
828 ((i2c_smbus_read_byte_data(client, AMC6821_REG_DEV_ID) &
829 0xDE) ||
830 (i2c_smbus_read_byte_data(client, AMC6821_REG_COMP_ID) &
831 0xB6))) {
832 dev_dbg(&adapter->dev,
833 "amc6821: detection failed at 0x%02x.\n",
834 address);
835 return -ENODEV;
838 dev_info(&adapter->dev, "amc6821: chip found at 0x%02x.\n", address);
839 strlcpy(info->type, "amc6821", I2C_NAME_SIZE);
841 return 0;
844 static int amc6821_probe(
845 struct i2c_client *client,
846 const struct i2c_device_id *id)
848 struct amc6821_data *data;
849 int err;
851 data = kzalloc(sizeof(struct amc6821_data), GFP_KERNEL);
852 if (!data) {
853 dev_err(&client->dev, "out of memory.\n");
854 return -ENOMEM;
858 i2c_set_clientdata(client, data);
859 mutex_init(&data->update_lock);
862 * Initialize the amc6821 chip
864 err = amc6821_init_client(client);
865 if (err)
866 goto err_free;
868 err = sysfs_create_group(&client->dev.kobj, &amc6821_attr_grp);
869 if (err)
870 goto err_free;
872 data->hwmon_dev = hwmon_device_register(&client->dev);
873 if (!IS_ERR(data->hwmon_dev))
874 return 0;
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);
879 err_free:
880 kfree(data);
881 return err;
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);
891 kfree(data);
893 return 0;
897 static int amc6821_init_client(struct i2c_client *client)
899 int config;
900 int err = -EIO;
902 if (init) {
903 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF4);
905 if (config < 0) {
906 dev_err(&client->dev,
907 "Error reading configuration register, aborting.\n");
908 return err;
911 config |= AMC6821_CONF4_MODE;
913 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF4,
914 config)) {
915 dev_err(&client->dev,
916 "Configuration register write error, aborting.\n");
917 return err;
920 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF3);
922 if (config < 0) {
923 dev_err(&client->dev,
924 "Error reading configuration register, aborting.\n");
925 return err;
928 config &= ~AMC6821_CONF3_THERM_FAN_EN;
930 if (i2c_smbus_write_byte_data(client, AMC6821_REG_CONF3,
931 config)) {
932 dev_err(&client->dev,
933 "Configuration register write error, aborting.\n");
934 return err;
937 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF2);
939 if (config < 0) {
940 dev_err(&client->dev,
941 "Error reading configuration register, aborting.\n");
942 return err;
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");
952 return err;
955 config = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
957 if (config < 0) {
958 dev_err(&client->dev,
959 "Error reading configuration register, aborting.\n");
960 return err;
963 config &= ~AMC6821_CONF1_THERMOVIE;
964 config &= ~AMC6821_CONF1_FANIE;
965 config |= AMC6821_CONF1_START;
966 if (pwminv)
967 config |= AMC6821_CONF1_PWMINV;
968 else
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");
975 return err;
978 return 0;
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);
986 int timeout = HZ;
987 u8 reg;
988 int i;
990 mutex_lock(&data->update_lock);
992 if (time_after(jiffies, data->last_updated + timeout) ||
993 !data->valid) {
995 for (i = 0; i < TEMP_IDX_LEN; i++)
996 data->temp[i] = i2c_smbus_read_byte_data(client,
997 temp_reg[i]);
999 data->stat1 = i2c_smbus_read_byte_data(client,
1000 AMC6821_REG_STAT1);
1001 data->stat2 = i2c_smbus_read_byte_data(client,
1002 AMC6821_REG_STAT2);
1004 data->pwm1 = i2c_smbus_read_byte_data(client,
1005 AMC6821_REG_DCY);
1006 for (i = 0; i < FAN1_IDX_LEN; i++) {
1007 data->fan[i] = i2c_smbus_read_byte_data(
1008 client,
1009 fan_reg_low[i]);
1010 data->fan[i] += i2c_smbus_read_byte_data(
1011 client,
1012 fan_reg_hi[i]) << 8;
1014 data->fan1_div = i2c_smbus_read_byte_data(client,
1015 AMC6821_REG_CONF4);
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;
1031 reg &= 0x07;
1032 reg = 0x20 >> reg;
1033 if (reg > 0)
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;
1038 else
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;
1044 reg &= 0x07;
1045 reg = 0x20 >> reg;
1046 if (reg > 0)
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;
1051 else
1052 data->temp2_auto_point_temp[2] = 255;
1054 reg = i2c_smbus_read_byte_data(client, AMC6821_REG_CONF1);
1055 reg = (reg >> 5) & 0x3;
1056 switch (reg) {
1057 case 0: /*open loop: software sets pwm1*/
1058 data->pwm1_auto_channels_temp = 0;
1059 data->pwm1_enable = 1;
1060 break;
1061 case 2: /*closed loop: remote T (temp2)*/
1062 data->pwm1_auto_channels_temp = 2;
1063 data->pwm1_enable = 2;
1064 break;
1065 case 3: /*closed loop: local and remote T (temp2)*/
1066 data->pwm1_auto_channels_temp = 3;
1067 data->pwm1_enable = 3;
1068 break;
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;
1074 break;
1077 data->last_updated = jiffies;
1078 data->valid = 1;
1080 mutex_unlock(&data->update_lock);
1081 return data;
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");