2 * asc7621.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3 * Copyright (c) 2007, 2010 George Joseph <george.joseph@fairview5.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/jiffies.h>
24 #include <linux/i2c.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/err.h>
28 #include <linux/mutex.h>
30 /* Addresses to scan */
31 static const unsigned short normal_i2c[] = {
32 0x2c, 0x2d, 0x2e, I2C_CLIENT_END
40 #define INTERVAL_HIGH (HZ + HZ / 2)
41 #define INTERVAL_LOW (1 * 60 * HZ)
45 #define FIRST_CHIP asc7621
46 #define LAST_CHIP asc7621a
50 enum asc7621_type chip_type;
55 const unsigned short *addresses;
58 static struct asc7621_chip asc7621_chips[] = {
66 .addresses = normal_i2c,
70 .chip_type = asc7621a,
75 .addresses = normal_i2c,
80 * Defines the highest register to be used, not the count.
81 * The actual count will probably be smaller because of gaps
82 * in the implementation (unused register locations).
83 * This define will safely set the array size of both the parameter
85 * This comes from the data sheet register description table.
87 #define LAST_REGISTER 0xff
90 struct i2c_client client;
91 struct device *class_dev;
92 struct mutex update_lock;
93 int valid; /* !=0 if following fields are valid */
94 unsigned long last_high_reading; /* In jiffies */
95 unsigned long last_low_reading; /* In jiffies */
97 * Registers we care about occupy the corresponding index
98 * in the array. Registers we don't care about are left
101 u8 reg[LAST_REGISTER + 1];
105 * Macro to get the parent asc7621_param structure
106 * from a sensor_device_attribute passed into the
107 * show/store functions.
109 #define to_asc7621_param(_sda) \
110 container_of(_sda, struct asc7621_param, sda)
113 * Each parameter to be retrieved needs an asc7621_param structure
114 * allocated. It contains the sensor_device_attribute structure
115 * and the control info needed to retrieve the value from the register map.
117 struct asc7621_param {
118 struct sensor_device_attribute sda;
127 * This is the map that ultimately indicates whether we'll be
128 * retrieving a register value or not, and at what frequency.
130 static u8 asc7621_register_priorities[255];
132 static struct asc7621_data *asc7621_update_device(struct device *dev);
134 static inline u8 read_byte(struct i2c_client *client, u8 reg)
136 int res = i2c_smbus_read_byte_data(client, reg);
138 dev_err(&client->dev,
139 "Unable to read from register 0x%02x.\n", reg);
145 static inline int write_byte(struct i2c_client *client, u8 reg, u8 data)
147 int res = i2c_smbus_write_byte_data(client, reg, data);
149 dev_err(&client->dev,
150 "Unable to write value 0x%02x to register 0x%02x.\n",
158 * Each function handles the formatting, storage
159 * and retrieval of like parameters.
162 #define SETUP_SHOW_data_param(d, a) \
163 struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
164 struct asc7621_data *data = asc7621_update_device(d); \
165 struct asc7621_param *param = to_asc7621_param(sda)
167 #define SETUP_STORE_data_param(d, a) \
168 struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
169 struct i2c_client *client = to_i2c_client(d); \
170 struct asc7621_data *data = i2c_get_clientdata(client); \
171 struct asc7621_param *param = to_asc7621_param(sda)
174 * u8 is just what it sounds like...an unsigned byte with no
175 * special formatting.
177 static ssize_t show_u8(struct device *dev, struct device_attribute *attr,
180 SETUP_SHOW_data_param(dev, attr);
182 return sprintf(buf, "%u\n", data->reg[param->msb[0]]);
185 static ssize_t store_u8(struct device *dev, struct device_attribute *attr,
186 const char *buf, size_t count)
188 SETUP_STORE_data_param(dev, attr);
191 if (kstrtol(buf, 10, &reqval))
194 reqval = clamp_val(reqval, 0, 255);
196 mutex_lock(&data->update_lock);
197 data->reg[param->msb[0]] = reqval;
198 write_byte(client, param->msb[0], reqval);
199 mutex_unlock(&data->update_lock);
204 * Many of the config values occupy only a few bits of a register.
206 static ssize_t show_bitmask(struct device *dev,
207 struct device_attribute *attr, char *buf)
209 SETUP_SHOW_data_param(dev, attr);
211 return sprintf(buf, "%u\n",
212 (data->reg[param->msb[0]] >> param->
213 shift[0]) & param->mask[0]);
216 static ssize_t store_bitmask(struct device *dev,
217 struct device_attribute *attr,
218 const char *buf, size_t count)
220 SETUP_STORE_data_param(dev, attr);
224 if (kstrtol(buf, 10, &reqval))
227 reqval = clamp_val(reqval, 0, param->mask[0]);
229 reqval = (reqval & param->mask[0]) << param->shift[0];
231 mutex_lock(&data->update_lock);
232 currval = read_byte(client, param->msb[0]);
233 reqval |= (currval & ~(param->mask[0] << param->shift[0]));
234 data->reg[param->msb[0]] = reqval;
235 write_byte(client, param->msb[0], reqval);
236 mutex_unlock(&data->update_lock);
241 * 16 bit fan rpm values
242 * reported by the device as the number of 11.111us periods (90khz)
243 * between full fan rotations. Therefore...
244 * RPM = (90000 * 60) / register value
246 static ssize_t show_fan16(struct device *dev,
247 struct device_attribute *attr, char *buf)
249 SETUP_SHOW_data_param(dev, attr);
252 mutex_lock(&data->update_lock);
253 regval = (data->reg[param->msb[0]] << 8) | data->reg[param->lsb[0]];
254 mutex_unlock(&data->update_lock);
256 return sprintf(buf, "%u\n",
257 (regval == 0 ? -1 : (regval) ==
258 0xffff ? 0 : 5400000 / regval));
261 static ssize_t store_fan16(struct device *dev,
262 struct device_attribute *attr, const char *buf,
265 SETUP_STORE_data_param(dev, attr);
268 if (kstrtol(buf, 10, &reqval))
272 * If a minimum RPM of zero is requested, then we set the register to
273 * 0xffff. This value allows the fan to be stopped completely without
274 * generating an alarm.
277 (reqval <= 0 ? 0xffff : clamp_val(5400000 / reqval, 0, 0xfffe));
279 mutex_lock(&data->update_lock);
280 data->reg[param->msb[0]] = (reqval >> 8) & 0xff;
281 data->reg[param->lsb[0]] = reqval & 0xff;
282 write_byte(client, param->msb[0], data->reg[param->msb[0]]);
283 write_byte(client, param->lsb[0], data->reg[param->lsb[0]]);
284 mutex_unlock(&data->update_lock);
290 * Voltages are scaled in the device so that the nominal voltage
291 * is 3/4ths of the 0-255 range (i.e. 192).
292 * If all voltages are 'normal' then all voltage registers will
295 * The data sheet provides us with the 3/4 scale value for each voltage
296 * which is stored in in_scaling. The sda->index parameter value provides
297 * the index into in_scaling.
299 * NOTE: The chip expects the first 2 inputs be 2.5 and 2.25 volts
300 * respectively. That doesn't mean that's what the motherboard provides. :)
303 static int asc7621_in_scaling[] = {
304 2500, 2250, 3300, 5000, 12000
307 static ssize_t show_in10(struct device *dev, struct device_attribute *attr,
310 SETUP_SHOW_data_param(dev, attr);
314 mutex_lock(&data->update_lock);
315 regval = (data->reg[param->msb[0]] << 8) | (data->reg[param->lsb[0]]);
316 mutex_unlock(&data->update_lock);
318 /* The LSB value is a 2-bit scaling of the MSB's LSbit value. */
319 regval = (regval >> 6) * asc7621_in_scaling[nr] / (0xc0 << 2);
321 return sprintf(buf, "%u\n", regval);
324 /* 8 bit voltage values (the mins and maxs) */
325 static ssize_t show_in8(struct device *dev, struct device_attribute *attr,
328 SETUP_SHOW_data_param(dev, attr);
331 return sprintf(buf, "%u\n",
332 ((data->reg[param->msb[0]] *
333 asc7621_in_scaling[nr]) / 0xc0));
336 static ssize_t store_in8(struct device *dev, struct device_attribute *attr,
337 const char *buf, size_t count)
339 SETUP_STORE_data_param(dev, attr);
343 if (kstrtol(buf, 10, &reqval))
346 reqval = clamp_val(reqval, 0, 0xffff);
348 reqval = reqval * 0xc0 / asc7621_in_scaling[nr];
350 reqval = clamp_val(reqval, 0, 0xff);
352 mutex_lock(&data->update_lock);
353 data->reg[param->msb[0]] = reqval;
354 write_byte(client, param->msb[0], reqval);
355 mutex_unlock(&data->update_lock);
360 static ssize_t show_temp8(struct device *dev,
361 struct device_attribute *attr, char *buf)
363 SETUP_SHOW_data_param(dev, attr);
365 return sprintf(buf, "%d\n", ((s8) data->reg[param->msb[0]]) * 1000);
368 static ssize_t store_temp8(struct device *dev,
369 struct device_attribute *attr, const char *buf,
372 SETUP_STORE_data_param(dev, attr);
376 if (kstrtol(buf, 10, &reqval))
379 reqval = clamp_val(reqval, -127000, 127000);
381 temp = reqval / 1000;
383 mutex_lock(&data->update_lock);
384 data->reg[param->msb[0]] = temp;
385 write_byte(client, param->msb[0], temp);
386 mutex_unlock(&data->update_lock);
391 * Temperatures that occupy 2 bytes always have the whole
392 * number of degrees in the MSB with some part of the LSB
393 * indicating fractional degrees.
396 /* mmmmmmmm.llxxxxxx */
397 static ssize_t show_temp10(struct device *dev,
398 struct device_attribute *attr, char *buf)
400 SETUP_SHOW_data_param(dev, attr);
404 mutex_lock(&data->update_lock);
405 msb = data->reg[param->msb[0]];
406 lsb = (data->reg[param->lsb[0]] >> 6) & 0x03;
407 temp = (((s8) msb) * 1000) + (lsb * 250);
408 mutex_unlock(&data->update_lock);
410 return sprintf(buf, "%d\n", temp);
414 static ssize_t show_temp62(struct device *dev,
415 struct device_attribute *attr, char *buf)
417 SETUP_SHOW_data_param(dev, attr);
418 u8 regval = data->reg[param->msb[0]];
419 int temp = ((s8) (regval & 0xfc) * 1000) + ((regval & 0x03) * 250);
421 return sprintf(buf, "%d\n", temp);
424 static ssize_t store_temp62(struct device *dev,
425 struct device_attribute *attr, const char *buf,
428 SETUP_STORE_data_param(dev, attr);
432 if (kstrtol(buf, 10, &reqval))
435 reqval = clamp_val(reqval, -32000, 31750);
437 f = reqval - (i * 1000);
441 mutex_lock(&data->update_lock);
442 data->reg[param->msb[0]] = temp;
443 write_byte(client, param->msb[0], temp);
444 mutex_unlock(&data->update_lock);
449 * The aSC7621 doesn't provide an "auto_point2". Instead, you
450 * specify the auto_point1 and a range. To keep with the sysfs
451 * hwmon specs, we synthesize the auto_point_2 from them.
454 static u32 asc7621_range_map[] = {
455 2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
456 13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
459 static ssize_t show_ap2_temp(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 SETUP_SHOW_data_param(dev, attr);
467 mutex_lock(&data->update_lock);
468 auto_point1 = ((s8) data->reg[param->msb[1]]) * 1000;
470 ((data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0]);
471 temp = auto_point1 + asc7621_range_map[clamp_val(regval, 0, 15)];
472 mutex_unlock(&data->update_lock);
474 return sprintf(buf, "%d\n", temp);
478 static ssize_t store_ap2_temp(struct device *dev,
479 struct device_attribute *attr,
480 const char *buf, size_t count)
482 SETUP_STORE_data_param(dev, attr);
483 long reqval, auto_point1;
485 u8 currval, newval = 0;
487 if (kstrtol(buf, 10, &reqval))
490 mutex_lock(&data->update_lock);
491 auto_point1 = data->reg[param->msb[1]] * 1000;
492 reqval = clamp_val(reqval, auto_point1 + 2000, auto_point1 + 80000);
494 for (i = ARRAY_SIZE(asc7621_range_map) - 1; i >= 0; i--) {
495 if (reqval >= auto_point1 + asc7621_range_map[i]) {
501 newval = (newval & param->mask[0]) << param->shift[0];
502 currval = read_byte(client, param->msb[0]);
503 newval |= (currval & ~(param->mask[0] << param->shift[0]));
504 data->reg[param->msb[0]] = newval;
505 write_byte(client, param->msb[0], newval);
506 mutex_unlock(&data->update_lock);
510 static ssize_t show_pwm_ac(struct device *dev,
511 struct device_attribute *attr, char *buf)
513 SETUP_SHOW_data_param(dev, attr);
514 u8 config, altbit, regval;
516 0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
517 0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
520 mutex_lock(&data->update_lock);
521 config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
522 altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
523 regval = config | (altbit << 3);
524 mutex_unlock(&data->update_lock);
526 return sprintf(buf, "%u\n", map[clamp_val(regval, 0, 15)]);
529 static ssize_t store_pwm_ac(struct device *dev,
530 struct device_attribute *attr,
531 const char *buf, size_t count)
533 SETUP_STORE_data_param(dev, attr);
534 unsigned long reqval;
535 u8 currval, config, altbit, newval;
537 0x04, 0x00, 0x01, 0xff, 0x02, 0xff, 0x05, 0x06,
538 0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
539 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
540 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
543 if (kstrtoul(buf, 10, &reqval))
549 reqval = map[reqval];
553 config = reqval & 0x07;
554 altbit = (reqval >> 3) & 0x01;
556 config = (config & param->mask[0]) << param->shift[0];
557 altbit = (altbit & param->mask[1]) << param->shift[1];
559 mutex_lock(&data->update_lock);
560 currval = read_byte(client, param->msb[0]);
561 newval = config | (currval & ~(param->mask[0] << param->shift[0]));
562 newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
563 data->reg[param->msb[0]] = newval;
564 write_byte(client, param->msb[0], newval);
565 mutex_unlock(&data->update_lock);
569 static ssize_t show_pwm_enable(struct device *dev,
570 struct device_attribute *attr, char *buf)
572 SETUP_SHOW_data_param(dev, attr);
573 u8 config, altbit, minoff, val, newval;
575 mutex_lock(&data->update_lock);
576 config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
577 altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
578 minoff = (data->reg[param->msb[2]] >> param->shift[2]) & param->mask[2];
579 mutex_unlock(&data->update_lock);
581 val = config | (altbit << 3);
584 if (val == 3 || val >= 10)
590 else if (minoff == 1)
595 return sprintf(buf, "%u\n", newval);
598 static ssize_t store_pwm_enable(struct device *dev,
599 struct device_attribute *attr,
600 const char *buf, size_t count)
602 SETUP_STORE_data_param(dev, attr);
604 u8 currval, config, altbit, newval, minoff = 255;
606 if (kstrtol(buf, 10, &reqval))
631 config = newval & 0x07;
632 altbit = (newval >> 3) & 0x01;
634 mutex_lock(&data->update_lock);
635 config = (config & param->mask[0]) << param->shift[0];
636 altbit = (altbit & param->mask[1]) << param->shift[1];
637 currval = read_byte(client, param->msb[0]);
638 newval = config | (currval & ~(param->mask[0] << param->shift[0]));
639 newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
640 data->reg[param->msb[0]] = newval;
641 write_byte(client, param->msb[0], newval);
643 minoff = (minoff & param->mask[2]) << param->shift[2];
644 currval = read_byte(client, param->msb[2]);
646 minoff | (currval & ~(param->mask[2] << param->shift[2]));
647 data->reg[param->msb[2]] = newval;
648 write_byte(client, param->msb[2], newval);
650 mutex_unlock(&data->update_lock);
654 static u32 asc7621_pwm_freq_map[] = {
655 10, 15, 23, 30, 38, 47, 62, 94,
656 23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000
659 static ssize_t show_pwm_freq(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 SETUP_SHOW_data_param(dev, attr);
664 (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
666 regval = clamp_val(regval, 0, 15);
668 return sprintf(buf, "%u\n", asc7621_pwm_freq_map[regval]);
671 static ssize_t store_pwm_freq(struct device *dev,
672 struct device_attribute *attr,
673 const char *buf, size_t count)
675 SETUP_STORE_data_param(dev, attr);
676 unsigned long reqval;
677 u8 currval, newval = 255;
680 if (kstrtoul(buf, 10, &reqval))
683 for (i = 0; i < ARRAY_SIZE(asc7621_pwm_freq_map); i++) {
684 if (reqval == asc7621_pwm_freq_map[i]) {
692 newval = (newval & param->mask[0]) << param->shift[0];
694 mutex_lock(&data->update_lock);
695 currval = read_byte(client, param->msb[0]);
696 newval |= (currval & ~(param->mask[0] << param->shift[0]));
697 data->reg[param->msb[0]] = newval;
698 write_byte(client, param->msb[0], newval);
699 mutex_unlock(&data->update_lock);
703 static u32 asc7621_pwm_auto_spinup_map[] = {
704 0, 100, 250, 400, 700, 1000, 2000, 4000
707 static ssize_t show_pwm_ast(struct device *dev,
708 struct device_attribute *attr, char *buf)
710 SETUP_SHOW_data_param(dev, attr);
712 (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
714 regval = clamp_val(regval, 0, 7);
716 return sprintf(buf, "%u\n", asc7621_pwm_auto_spinup_map[regval]);
720 static ssize_t store_pwm_ast(struct device *dev,
721 struct device_attribute *attr,
722 const char *buf, size_t count)
724 SETUP_STORE_data_param(dev, attr);
726 u8 currval, newval = 255;
729 if (kstrtol(buf, 10, &reqval))
732 for (i = 0; i < ARRAY_SIZE(asc7621_pwm_auto_spinup_map); i++) {
733 if (reqval == asc7621_pwm_auto_spinup_map[i]) {
741 newval = (newval & param->mask[0]) << param->shift[0];
743 mutex_lock(&data->update_lock);
744 currval = read_byte(client, param->msb[0]);
745 newval |= (currval & ~(param->mask[0] << param->shift[0]));
746 data->reg[param->msb[0]] = newval;
747 write_byte(client, param->msb[0], newval);
748 mutex_unlock(&data->update_lock);
752 static u32 asc7621_temp_smoothing_time_map[] = {
753 35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
756 static ssize_t show_temp_st(struct device *dev,
757 struct device_attribute *attr, char *buf)
759 SETUP_SHOW_data_param(dev, attr);
761 (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
762 regval = clamp_val(regval, 0, 7);
764 return sprintf(buf, "%u\n", asc7621_temp_smoothing_time_map[regval]);
767 static ssize_t store_temp_st(struct device *dev,
768 struct device_attribute *attr,
769 const char *buf, size_t count)
771 SETUP_STORE_data_param(dev, attr);
773 u8 currval, newval = 255;
776 if (kstrtol(buf, 10, &reqval))
779 for (i = 0; i < ARRAY_SIZE(asc7621_temp_smoothing_time_map); i++) {
780 if (reqval == asc7621_temp_smoothing_time_map[i]) {
789 newval = (newval & param->mask[0]) << param->shift[0];
791 mutex_lock(&data->update_lock);
792 currval = read_byte(client, param->msb[0]);
793 newval |= (currval & ~(param->mask[0] << param->shift[0]));
794 data->reg[param->msb[0]] = newval;
795 write_byte(client, param->msb[0], newval);
796 mutex_unlock(&data->update_lock);
801 * End of data handlers
803 * These defines do nothing more than make the table easier
804 * to read when wrapped at column 80.
808 * Creates a variable length array inititalizer.
809 * VAA(1,3,5,7) would produce {1,3,5,7}
811 #define VAA(args...) {args}
813 #define PREAD(name, n, pri, rm, rl, m, s, r) \
814 {.sda = SENSOR_ATTR(name, S_IRUGO, show_##r, NULL, n), \
815 .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
818 #define PWRITE(name, n, pri, rm, rl, m, s, r) \
819 {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
820 .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
824 * PWRITEM assumes that the initializers for the .msb, .lsb, .mask and .shift
825 * were created using the VAA macro.
827 #define PWRITEM(name, n, pri, rm, rl, m, s, r) \
828 {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
829 .priority = pri, .msb = rm, .lsb = rl, .mask = m, .shift = s,}
831 static struct asc7621_param asc7621_params[] = {
832 PREAD(in0_input, 0, PRI_HIGH, 0x20, 0x13, 0, 0, in10),
833 PREAD(in1_input, 1, PRI_HIGH, 0x21, 0x18, 0, 0, in10),
834 PREAD(in2_input, 2, PRI_HIGH, 0x22, 0x11, 0, 0, in10),
835 PREAD(in3_input, 3, PRI_HIGH, 0x23, 0x12, 0, 0, in10),
836 PREAD(in4_input, 4, PRI_HIGH, 0x24, 0x14, 0, 0, in10),
838 PWRITE(in0_min, 0, PRI_LOW, 0x44, 0, 0, 0, in8),
839 PWRITE(in1_min, 1, PRI_LOW, 0x46, 0, 0, 0, in8),
840 PWRITE(in2_min, 2, PRI_LOW, 0x48, 0, 0, 0, in8),
841 PWRITE(in3_min, 3, PRI_LOW, 0x4a, 0, 0, 0, in8),
842 PWRITE(in4_min, 4, PRI_LOW, 0x4c, 0, 0, 0, in8),
844 PWRITE(in0_max, 0, PRI_LOW, 0x45, 0, 0, 0, in8),
845 PWRITE(in1_max, 1, PRI_LOW, 0x47, 0, 0, 0, in8),
846 PWRITE(in2_max, 2, PRI_LOW, 0x49, 0, 0, 0, in8),
847 PWRITE(in3_max, 3, PRI_LOW, 0x4b, 0, 0, 0, in8),
848 PWRITE(in4_max, 4, PRI_LOW, 0x4d, 0, 0, 0, in8),
850 PREAD(in0_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 0, bitmask),
851 PREAD(in1_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 1, bitmask),
852 PREAD(in2_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 2, bitmask),
853 PREAD(in3_alarm, 3, PRI_HIGH, 0x41, 0, 0x01, 3, bitmask),
854 PREAD(in4_alarm, 4, PRI_HIGH, 0x42, 0, 0x01, 0, bitmask),
856 PREAD(fan1_input, 0, PRI_HIGH, 0x29, 0x28, 0, 0, fan16),
857 PREAD(fan2_input, 1, PRI_HIGH, 0x2b, 0x2a, 0, 0, fan16),
858 PREAD(fan3_input, 2, PRI_HIGH, 0x2d, 0x2c, 0, 0, fan16),
859 PREAD(fan4_input, 3, PRI_HIGH, 0x2f, 0x2e, 0, 0, fan16),
861 PWRITE(fan1_min, 0, PRI_LOW, 0x55, 0x54, 0, 0, fan16),
862 PWRITE(fan2_min, 1, PRI_LOW, 0x57, 0x56, 0, 0, fan16),
863 PWRITE(fan3_min, 2, PRI_LOW, 0x59, 0x58, 0, 0, fan16),
864 PWRITE(fan4_min, 3, PRI_LOW, 0x5b, 0x5a, 0, 0, fan16),
866 PREAD(fan1_alarm, 0, PRI_HIGH, 0x42, 0, 0x01, 2, bitmask),
867 PREAD(fan2_alarm, 1, PRI_HIGH, 0x42, 0, 0x01, 3, bitmask),
868 PREAD(fan3_alarm, 2, PRI_HIGH, 0x42, 0, 0x01, 4, bitmask),
869 PREAD(fan4_alarm, 3, PRI_HIGH, 0x42, 0, 0x01, 5, bitmask),
871 PREAD(temp1_input, 0, PRI_HIGH, 0x25, 0x10, 0, 0, temp10),
872 PREAD(temp2_input, 1, PRI_HIGH, 0x26, 0x15, 0, 0, temp10),
873 PREAD(temp3_input, 2, PRI_HIGH, 0x27, 0x16, 0, 0, temp10),
874 PREAD(temp4_input, 3, PRI_HIGH, 0x33, 0x17, 0, 0, temp10),
875 PREAD(temp5_input, 4, PRI_HIGH, 0xf7, 0xf6, 0, 0, temp10),
876 PREAD(temp6_input, 5, PRI_HIGH, 0xf9, 0xf8, 0, 0, temp10),
877 PREAD(temp7_input, 6, PRI_HIGH, 0xfb, 0xfa, 0, 0, temp10),
878 PREAD(temp8_input, 7, PRI_HIGH, 0xfd, 0xfc, 0, 0, temp10),
880 PWRITE(temp1_min, 0, PRI_LOW, 0x4e, 0, 0, 0, temp8),
881 PWRITE(temp2_min, 1, PRI_LOW, 0x50, 0, 0, 0, temp8),
882 PWRITE(temp3_min, 2, PRI_LOW, 0x52, 0, 0, 0, temp8),
883 PWRITE(temp4_min, 3, PRI_LOW, 0x34, 0, 0, 0, temp8),
885 PWRITE(temp1_max, 0, PRI_LOW, 0x4f, 0, 0, 0, temp8),
886 PWRITE(temp2_max, 1, PRI_LOW, 0x51, 0, 0, 0, temp8),
887 PWRITE(temp3_max, 2, PRI_LOW, 0x53, 0, 0, 0, temp8),
888 PWRITE(temp4_max, 3, PRI_LOW, 0x35, 0, 0, 0, temp8),
890 PREAD(temp1_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 4, bitmask),
891 PREAD(temp2_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 5, bitmask),
892 PREAD(temp3_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 6, bitmask),
893 PREAD(temp4_alarm, 3, PRI_HIGH, 0x43, 0, 0x01, 0, bitmask),
895 PWRITE(temp1_source, 0, PRI_LOW, 0x02, 0, 0x07, 4, bitmask),
896 PWRITE(temp2_source, 1, PRI_LOW, 0x02, 0, 0x07, 0, bitmask),
897 PWRITE(temp3_source, 2, PRI_LOW, 0x03, 0, 0x07, 4, bitmask),
898 PWRITE(temp4_source, 3, PRI_LOW, 0x03, 0, 0x07, 0, bitmask),
900 PWRITE(temp1_smoothing_enable, 0, PRI_LOW, 0x62, 0, 0x01, 3, bitmask),
901 PWRITE(temp2_smoothing_enable, 1, PRI_LOW, 0x63, 0, 0x01, 7, bitmask),
902 PWRITE(temp3_smoothing_enable, 2, PRI_LOW, 0x63, 0, 0x01, 3, bitmask),
903 PWRITE(temp4_smoothing_enable, 3, PRI_LOW, 0x3c, 0, 0x01, 3, bitmask),
905 PWRITE(temp1_smoothing_time, 0, PRI_LOW, 0x62, 0, 0x07, 0, temp_st),
906 PWRITE(temp2_smoothing_time, 1, PRI_LOW, 0x63, 0, 0x07, 4, temp_st),
907 PWRITE(temp3_smoothing_time, 2, PRI_LOW, 0x63, 0, 0x07, 0, temp_st),
908 PWRITE(temp4_smoothing_time, 3, PRI_LOW, 0x3c, 0, 0x07, 0, temp_st),
910 PWRITE(temp1_auto_point1_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
912 PWRITE(temp2_auto_point1_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
914 PWRITE(temp3_auto_point1_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
916 PWRITE(temp4_auto_point1_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
919 PREAD(temp1_auto_point2_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
921 PREAD(temp2_auto_point2_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
923 PREAD(temp3_auto_point2_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
925 PREAD(temp4_auto_point2_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
928 PWRITE(temp1_auto_point1_temp, 0, PRI_LOW, 0x67, 0, 0, 0, temp8),
929 PWRITE(temp2_auto_point1_temp, 1, PRI_LOW, 0x68, 0, 0, 0, temp8),
930 PWRITE(temp3_auto_point1_temp, 2, PRI_LOW, 0x69, 0, 0, 0, temp8),
931 PWRITE(temp4_auto_point1_temp, 3, PRI_LOW, 0x3b, 0, 0, 0, temp8),
933 PWRITEM(temp1_auto_point2_temp, 0, PRI_LOW, VAA(0x5f, 0x67), VAA(0),
934 VAA(0x0f), VAA(4), ap2_temp),
935 PWRITEM(temp2_auto_point2_temp, 1, PRI_LOW, VAA(0x60, 0x68), VAA(0),
936 VAA(0x0f), VAA(4), ap2_temp),
937 PWRITEM(temp3_auto_point2_temp, 2, PRI_LOW, VAA(0x61, 0x69), VAA(0),
938 VAA(0x0f), VAA(4), ap2_temp),
939 PWRITEM(temp4_auto_point2_temp, 3, PRI_LOW, VAA(0x3c, 0x3b), VAA(0),
940 VAA(0x0f), VAA(4), ap2_temp),
942 PWRITE(temp1_crit, 0, PRI_LOW, 0x6a, 0, 0, 0, temp8),
943 PWRITE(temp2_crit, 1, PRI_LOW, 0x6b, 0, 0, 0, temp8),
944 PWRITE(temp3_crit, 2, PRI_LOW, 0x6c, 0, 0, 0, temp8),
945 PWRITE(temp4_crit, 3, PRI_LOW, 0x3d, 0, 0, 0, temp8),
947 PWRITE(temp5_enable, 4, PRI_LOW, 0x0e, 0, 0x01, 0, bitmask),
948 PWRITE(temp6_enable, 5, PRI_LOW, 0x0e, 0, 0x01, 1, bitmask),
949 PWRITE(temp7_enable, 6, PRI_LOW, 0x0e, 0, 0x01, 2, bitmask),
950 PWRITE(temp8_enable, 7, PRI_LOW, 0x0e, 0, 0x01, 3, bitmask),
952 PWRITE(remote1_offset, 0, PRI_LOW, 0x1c, 0, 0, 0, temp62),
953 PWRITE(remote2_offset, 1, PRI_LOW, 0x1d, 0, 0, 0, temp62),
955 PWRITE(pwm1, 0, PRI_HIGH, 0x30, 0, 0, 0, u8),
956 PWRITE(pwm2, 1, PRI_HIGH, 0x31, 0, 0, 0, u8),
957 PWRITE(pwm3, 2, PRI_HIGH, 0x32, 0, 0, 0, u8),
959 PWRITE(pwm1_invert, 0, PRI_LOW, 0x5c, 0, 0x01, 4, bitmask),
960 PWRITE(pwm2_invert, 1, PRI_LOW, 0x5d, 0, 0x01, 4, bitmask),
961 PWRITE(pwm3_invert, 2, PRI_LOW, 0x5e, 0, 0x01, 4, bitmask),
963 PWRITEM(pwm1_enable, 0, PRI_LOW, VAA(0x5c, 0x5c, 0x62), VAA(0, 0, 0),
964 VAA(0x07, 0x01, 0x01), VAA(5, 3, 5), pwm_enable),
965 PWRITEM(pwm2_enable, 1, PRI_LOW, VAA(0x5d, 0x5d, 0x62), VAA(0, 0, 0),
966 VAA(0x07, 0x01, 0x01), VAA(5, 3, 6), pwm_enable),
967 PWRITEM(pwm3_enable, 2, PRI_LOW, VAA(0x5e, 0x5e, 0x62), VAA(0, 0, 0),
968 VAA(0x07, 0x01, 0x01), VAA(5, 3, 7), pwm_enable),
970 PWRITEM(pwm1_auto_channels, 0, PRI_LOW, VAA(0x5c, 0x5c), VAA(0, 0),
971 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
972 PWRITEM(pwm2_auto_channels, 1, PRI_LOW, VAA(0x5d, 0x5d), VAA(0, 0),
973 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
974 PWRITEM(pwm3_auto_channels, 2, PRI_LOW, VAA(0x5e, 0x5e), VAA(0, 0),
975 VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
977 PWRITE(pwm1_auto_point1_pwm, 0, PRI_LOW, 0x64, 0, 0, 0, u8),
978 PWRITE(pwm2_auto_point1_pwm, 1, PRI_LOW, 0x65, 0, 0, 0, u8),
979 PWRITE(pwm3_auto_point1_pwm, 2, PRI_LOW, 0x66, 0, 0, 0, u8),
981 PWRITE(pwm1_auto_point2_pwm, 0, PRI_LOW, 0x38, 0, 0, 0, u8),
982 PWRITE(pwm2_auto_point2_pwm, 1, PRI_LOW, 0x39, 0, 0, 0, u8),
983 PWRITE(pwm3_auto_point2_pwm, 2, PRI_LOW, 0x3a, 0, 0, 0, u8),
985 PWRITE(pwm1_freq, 0, PRI_LOW, 0x5f, 0, 0x0f, 0, pwm_freq),
986 PWRITE(pwm2_freq, 1, PRI_LOW, 0x60, 0, 0x0f, 0, pwm_freq),
987 PWRITE(pwm3_freq, 2, PRI_LOW, 0x61, 0, 0x0f, 0, pwm_freq),
989 PREAD(pwm1_auto_zone_assigned, 0, PRI_LOW, 0, 0, 0x03, 2, bitmask),
990 PREAD(pwm2_auto_zone_assigned, 1, PRI_LOW, 0, 0, 0x03, 4, bitmask),
991 PREAD(pwm3_auto_zone_assigned, 2, PRI_LOW, 0, 0, 0x03, 6, bitmask),
993 PWRITE(pwm1_auto_spinup_time, 0, PRI_LOW, 0x5c, 0, 0x07, 0, pwm_ast),
994 PWRITE(pwm2_auto_spinup_time, 1, PRI_LOW, 0x5d, 0, 0x07, 0, pwm_ast),
995 PWRITE(pwm3_auto_spinup_time, 2, PRI_LOW, 0x5e, 0, 0x07, 0, pwm_ast),
997 PWRITE(peci_enable, 0, PRI_LOW, 0x40, 0, 0x01, 4, bitmask),
998 PWRITE(peci_avg, 0, PRI_LOW, 0x36, 0, 0x07, 0, bitmask),
999 PWRITE(peci_domain, 0, PRI_LOW, 0x36, 0, 0x01, 3, bitmask),
1000 PWRITE(peci_legacy, 0, PRI_LOW, 0x36, 0, 0x01, 4, bitmask),
1001 PWRITE(peci_diode, 0, PRI_LOW, 0x0e, 0, 0x07, 4, bitmask),
1002 PWRITE(peci_4domain, 0, PRI_LOW, 0x0e, 0, 0x01, 4, bitmask),
1006 static struct asc7621_data *asc7621_update_device(struct device *dev)
1008 struct i2c_client *client = to_i2c_client(dev);
1009 struct asc7621_data *data = i2c_get_clientdata(client);
1013 * The asc7621 chips guarantee consistent reads of multi-byte values
1014 * regardless of the order of the reads. No special logic is needed
1015 * so we can just read the registers in whatever order they appear
1016 * in the asc7621_params array.
1019 mutex_lock(&data->update_lock);
1021 /* Read all the high priority registers */
1024 time_after(jiffies, data->last_high_reading + INTERVAL_HIGH)) {
1026 for (i = 0; i < ARRAY_SIZE(asc7621_register_priorities); i++) {
1027 if (asc7621_register_priorities[i] == PRI_HIGH) {
1029 i2c_smbus_read_byte_data(client, i) & 0xff;
1032 data->last_high_reading = jiffies;
1033 }; /* last_reading */
1035 /* Read all the low priority registers. */
1038 time_after(jiffies, data->last_low_reading + INTERVAL_LOW)) {
1040 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1041 if (asc7621_register_priorities[i] == PRI_LOW) {
1043 i2c_smbus_read_byte_data(client, i) & 0xff;
1046 data->last_low_reading = jiffies;
1047 }; /* last_reading */
1051 mutex_unlock(&data->update_lock);
1057 * Standard detection and initialization below
1059 * Helper function that checks if an address is valid
1060 * for a particular chip.
1063 static inline int valid_address_for_chip(int chip_type, int address)
1067 for (i = 0; asc7621_chips[chip_type].addresses[i] != I2C_CLIENT_END;
1069 if (asc7621_chips[chip_type].addresses[i] == address)
1075 static void asc7621_init_client(struct i2c_client *client)
1079 /* Warn if part was not "READY" */
1081 value = read_byte(client, 0x40);
1084 dev_err(&client->dev,
1085 "Client (%d,0x%02x) config is locked.\n",
1086 i2c_adapter_id(client->adapter), client->addr);
1088 if (!(value & 0x04)) {
1089 dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1090 i2c_adapter_id(client->adapter), client->addr);
1096 * Try to clear LOCK, Set START, save everything else
1098 value = (value & ~0x02) | 0x01;
1099 write_byte(client, 0x40, value & 0xff);
1104 asc7621_probe(struct i2c_client *client, const struct i2c_device_id *id)
1106 struct asc7621_data *data;
1109 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1112 data = devm_kzalloc(&client->dev, sizeof(struct asc7621_data),
1117 i2c_set_clientdata(client, data);
1119 mutex_init(&data->update_lock);
1121 /* Initialize the asc7621 chip */
1122 asc7621_init_client(client);
1124 /* Create the sysfs entries */
1125 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1127 device_create_file(&client->dev,
1128 &(asc7621_params[i].sda.dev_attr));
1133 data->class_dev = hwmon_device_register(&client->dev);
1134 if (IS_ERR(data->class_dev)) {
1135 err = PTR_ERR(data->class_dev);
1142 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1143 device_remove_file(&client->dev,
1144 &(asc7621_params[i].sda.dev_attr));
1150 static int asc7621_detect(struct i2c_client *client,
1151 struct i2c_board_info *info)
1153 struct i2c_adapter *adapter = client->adapter;
1154 int company, verstep, chip_index;
1156 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1159 for (chip_index = FIRST_CHIP; chip_index <= LAST_CHIP; chip_index++) {
1161 if (!valid_address_for_chip(chip_index, client->addr))
1164 company = read_byte(client,
1165 asc7621_chips[chip_index].company_reg);
1166 verstep = read_byte(client,
1167 asc7621_chips[chip_index].verstep_reg);
1169 if (company == asc7621_chips[chip_index].company_id &&
1170 verstep == asc7621_chips[chip_index].verstep_id) {
1171 strlcpy(info->type, asc7621_chips[chip_index].name,
1174 dev_info(&adapter->dev, "Matched %s at 0x%02x\n",
1175 asc7621_chips[chip_index].name, client->addr);
1183 static int asc7621_remove(struct i2c_client *client)
1185 struct asc7621_data *data = i2c_get_clientdata(client);
1188 hwmon_device_unregister(data->class_dev);
1190 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1191 device_remove_file(&client->dev,
1192 &(asc7621_params[i].sda.dev_attr));
1198 static const struct i2c_device_id asc7621_id[] = {
1199 {"asc7621", asc7621},
1200 {"asc7621a", asc7621a},
1204 MODULE_DEVICE_TABLE(i2c, asc7621_id);
1206 static struct i2c_driver asc7621_driver = {
1207 .class = I2C_CLASS_HWMON,
1211 .probe = asc7621_probe,
1212 .remove = asc7621_remove,
1213 .id_table = asc7621_id,
1214 .detect = asc7621_detect,
1215 .address_list = normal_i2c,
1218 static int __init sm_asc7621_init(void)
1222 * Collect all the registers needed into a single array.
1223 * This way, if a register isn't actually used for anything,
1224 * we don't retrieve it.
1227 for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1228 for (j = 0; j < ARRAY_SIZE(asc7621_params[i].msb); j++)
1229 asc7621_register_priorities[asc7621_params[i].msb[j]] =
1230 asc7621_params[i].priority;
1231 for (j = 0; j < ARRAY_SIZE(asc7621_params[i].lsb); j++)
1232 asc7621_register_priorities[asc7621_params[i].lsb[j]] =
1233 asc7621_params[i].priority;
1235 return i2c_add_driver(&asc7621_driver);
1238 static void __exit sm_asc7621_exit(void)
1240 i2c_del_driver(&asc7621_driver);
1243 MODULE_LICENSE("GPL");
1244 MODULE_AUTHOR("George Joseph");
1245 MODULE_DESCRIPTION("Andigilog aSC7621 and aSC7621a driver");
1247 module_init(sm_asc7621_init);
1248 module_exit(sm_asc7621_exit);