20 #include <linux/module.h>
22 #include <linux/slab.h>
24 #include <linux/i2c.h>
31 static const unsigned short normal_i2c[] = {
40 #define INTERVAL_HIGH (HZ + HZ / 2)
41 #define INTERVAL_LOW (1 * 60 * HZ)
45 #define FIRST_CHIP asc7621
46 #define LAST_CHIP asc7621a
66 .addresses = normal_i2c,
75 .addresses = normal_i2c,
87 #define LAST_REGISTER 0xff
109 #define to_asc7621_param(_sda) \
110 container_of(_sda, struct asc7621_param, sda)
130 static u8 asc7621_register_priorities[255];
139 "Unable to read from register 0x%02x.\n", reg);
150 "Unable to write value 0x%02x to register 0x%02x.\n",
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)
191 if (kstrtol(buf, 10, &reqval))
194 reqval = SENSORS_LIMIT(reqval, 0, 255);
197 data->reg[
param->msb[0]] = reqval;
198 write_byte(client,
param->msb[0], reqval);
213 shift[0]) &
param->mask[0]);
218 const char *buf,
size_t count)
224 if (kstrtol(buf, 10, &reqval))
227 reqval = SENSORS_LIMIT(reqval, 0,
param->mask[0]);
229 reqval = (reqval &
param->mask[0]) <<
param->shift[0];
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);
253 regval = (data->reg[
param->msb[0]] << 8) | data->reg[
param->lsb[0]];
257 (regval == 0 ? -1 : (regval) ==
258 0xffff ? 0 : 5400000 / regval));
268 if (kstrtol(buf, 10, &reqval))
277 (reqval <= 0 ? 0xffff : SENSORS_LIMIT(5400000 / reqval, 0, 0xfffe));
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]]);
303 static int asc7621_in_scaling[] = {
304 2500, 2250, 3300, 5000, 12000
315 regval = (data->reg[
param->msb[0]] << 8) | (data->reg[
param->lsb[0]]);
319 regval = (regval >> 6) * asc7621_in_scaling[nr] / (0xc0 << 2);
321 return sprintf(buf,
"%u\n", regval);
332 ((data->reg[
param->msb[0]] *
333 asc7621_in_scaling[nr]) / 0xc0));
337 const char *buf,
size_t count)
343 if (kstrtol(buf, 10, &reqval))
346 reqval = SENSORS_LIMIT(reqval, 0, 0xffff);
348 reqval = reqval * 0xc0 / asc7621_in_scaling[
nr];
350 reqval = SENSORS_LIMIT(reqval, 0, 0xff);
353 data->reg[
param->msb[0]] = reqval;
354 write_byte(client,
param->msb[0], reqval);
365 return sprintf(buf,
"%d\n", ((
s8) data->reg[
param->msb[0]]) * 1000);
376 if (kstrtol(buf, 10, &reqval))
379 reqval = SENSORS_LIMIT(reqval, -127000, 127000);
381 temp = reqval / 1000;
385 write_byte(client,
param->msb[0], temp);
405 msb = data->reg[
param->msb[0]];
406 lsb = (data->reg[
param->lsb[0]] >> 6) & 0x03;
407 temp = (((
s8) msb) * 1000) + (lsb * 250);
410 return sprintf(buf,
"%d\n", temp);
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);
432 if (kstrtol(buf, 10, &reqval))
435 reqval = SENSORS_LIMIT(reqval, -32000, 31750);
437 f = reqval - (i * 1000);
443 write_byte(client,
param->msb[0], temp);
454 static u32 asc7621_range_map[] = {
455 2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
456 13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
468 auto_point1 = ((
s8) data->reg[
param->msb[1]]) * 1000;
471 temp = auto_point1 + asc7621_range_map[SENSORS_LIMIT(regval, 0, 15)];
474 return sprintf(buf,
"%d\n", temp);
480 const char *buf,
size_t count)
483 long reqval, auto_point1;
485 u8 currval, newval = 0;
487 if (kstrtol(buf, 10, &reqval))
491 auto_point1 = data->reg[
param->msb[1]] * 1000;
492 reqval = SENSORS_LIMIT(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);
516 0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
517 0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
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);
526 return sprintf(buf,
"%u\n", map[SENSORS_LIMIT(regval, 0, 15)]);
531 const char *buf,
size_t count)
534 unsigned long reqval;
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];
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);
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];
581 val = config | (altbit << 3);
584 if (val == 3 || val >= 10)
590 else if (minoff == 1)
595 return sprintf(buf,
"%u\n", newval);
600 const char *buf,
size_t count)
604 u8 currval,
config, altbit, newval, minoff = 255;
606 if (kstrtol(buf, 10, &reqval))
631 config = newval & 0x07;
632 altbit = (newval >> 3) & 0x01;
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);
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
666 regval = SENSORS_LIMIT(regval, 0, 15);
668 return sprintf(buf,
"%u\n", asc7621_pwm_freq_map[regval]);
673 const char *buf,
size_t count)
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];
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);
703 static u32 asc7621_pwm_auto_spinup_map[] = {
704 0, 100, 250, 400, 700, 1000, 2000, 4000
714 regval = SENSORS_LIMIT(regval, 0, 7);
716 return sprintf(buf,
"%u\n", asc7621_pwm_auto_spinup_map[regval]);
722 const char *buf,
size_t count)
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];
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);
752 static u32 asc7621_temp_smoothing_time_map[] = {
753 35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
762 regval = SENSORS_LIMIT(regval, 0, 7);
764 return sprintf(buf,
"%u\n", asc7621_temp_smoothing_time_map[regval]);
769 const char *buf,
size_t count)
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];
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);
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, \
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,}
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),
934 VAA(0x0f),
VAA(4), ap2_temp),
936 VAA(0x0f),
VAA(4), ap2_temp),
938 VAA(0x0f),
VAA(4), ap2_temp),
940 VAA(0x0f),
VAA(4), ap2_temp),
971 VAA(0x07, 0x01),
VAA(5, 3), pwm_ac),
973 VAA(0x07, 0x01),
VAA(5, 3), pwm_ac),
975 VAA(0x07, 0x01),
VAA(5, 3), pwm_ac),
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),
1009 struct asc7621_data *data = i2c_get_clientdata(client);
1026 for (i = 0; i <
ARRAY_SIZE(asc7621_register_priorities); i++) {
1027 if (asc7621_register_priorities[i] ==
PRI_HIGH) {
1040 for (i = 0; i <
ARRAY_SIZE(asc7621_params); i++) {
1041 if (asc7621_register_priorities[i] ==
PRI_LOW) {
1069 if (asc7621_chips[chip_type].addresses[i] == address)
1075 static void asc7621_init_client(
struct i2c_client *client)
1081 value = read_byte(client, 0x40);
1085 "Client (%d,0x%02x) config is locked.\n",
1088 if (!(value & 0x04)) {
1089 dev_err(&client->
dev,
"Client (%d,0x%02x) is not ready.\n",
1098 value = (value & ~0x02) | 0x01;
1099 write_byte(client, 0x40, value & 0xff);
1117 i2c_set_clientdata(client, data);
1122 asc7621_init_client(client);
1125 for (i = 0; i <
ARRAY_SIZE(asc7621_params); i++) {
1128 &(asc7621_params[i].
sda.dev_attr));
1142 for (i = 0; i <
ARRAY_SIZE(asc7621_params); i++) {
1144 &(asc7621_params[i].
sda.dev_attr));
1150 static int asc7621_detect(
struct i2c_client *client,
1154 int company, verstep, 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) {
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);
1190 for (i = 0; i <
ARRAY_SIZE(asc7621_params); i++) {
1192 &(asc7621_params[i].
sda.dev_attr));
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)
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]] =
1231 for (j = 0; j <
ARRAY_SIZE(asc7621_params[i].lsb); j++)
1232 asc7621_register_priorities[asc7621_params[i].lsb[j]] =
1235 return i2c_add_driver(&asc7621_driver);
1238 static void __exit sm_asc7621_exit(
void)