Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * asc7621.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
0004  * Copyright (c) 2007, 2010 George Joseph  <george.joseph@fairview5.com>
0005  */
0006 
0007 #include <linux/module.h>
0008 #include <linux/init.h>
0009 #include <linux/slab.h>
0010 #include <linux/jiffies.h>
0011 #include <linux/i2c.h>
0012 #include <linux/hwmon.h>
0013 #include <linux/hwmon-sysfs.h>
0014 #include <linux/err.h>
0015 #include <linux/mutex.h>
0016 
0017 /* Addresses to scan */
0018 static const unsigned short normal_i2c[] = {
0019     0x2c, 0x2d, 0x2e, I2C_CLIENT_END
0020 };
0021 
0022 enum asc7621_type {
0023     asc7621,
0024     asc7621a
0025 };
0026 
0027 #define INTERVAL_HIGH   (HZ + HZ / 2)
0028 #define INTERVAL_LOW    (1 * 60 * HZ)
0029 #define PRI_NONE        0
0030 #define PRI_LOW         1
0031 #define PRI_HIGH        2
0032 #define FIRST_CHIP      asc7621
0033 #define LAST_CHIP       asc7621a
0034 
0035 struct asc7621_chip {
0036     char *name;
0037     enum asc7621_type chip_type;
0038     u8 company_reg;
0039     u8 company_id;
0040     u8 verstep_reg;
0041     u8 verstep_id;
0042     const unsigned short *addresses;
0043 };
0044 
0045 static struct asc7621_chip asc7621_chips[] = {
0046     {
0047         .name = "asc7621",
0048         .chip_type = asc7621,
0049         .company_reg = 0x3e,
0050         .company_id = 0x61,
0051         .verstep_reg = 0x3f,
0052         .verstep_id = 0x6c,
0053         .addresses = normal_i2c,
0054      },
0055     {
0056         .name = "asc7621a",
0057         .chip_type = asc7621a,
0058         .company_reg = 0x3e,
0059         .company_id = 0x61,
0060         .verstep_reg = 0x3f,
0061         .verstep_id = 0x6d,
0062         .addresses = normal_i2c,
0063      },
0064 };
0065 
0066 /*
0067  * Defines the highest register to be used, not the count.
0068  * The actual count will probably be smaller because of gaps
0069  * in the implementation (unused register locations).
0070  * This define will safely set the array size of both the parameter
0071  * and data arrays.
0072  * This comes from the data sheet register description table.
0073  */
0074 #define LAST_REGISTER 0xff
0075 
0076 struct asc7621_data {
0077     struct i2c_client client;
0078     struct device *class_dev;
0079     struct mutex update_lock;
0080     bool valid;     /* true if following fields are valid */
0081     unsigned long last_high_reading;    /* In jiffies */
0082     unsigned long last_low_reading;     /* In jiffies */
0083     /*
0084      * Registers we care about occupy the corresponding index
0085      * in the array.  Registers we don't care about are left
0086      * at 0.
0087      */
0088     u8 reg[LAST_REGISTER + 1];
0089 };
0090 
0091 /*
0092  * Macro to get the parent asc7621_param structure
0093  * from a sensor_device_attribute passed into the
0094  * show/store functions.
0095  */
0096 #define to_asc7621_param(_sda) \
0097     container_of(_sda, struct asc7621_param, sda)
0098 
0099 /*
0100  * Each parameter to be retrieved needs an asc7621_param structure
0101  * allocated.  It contains the sensor_device_attribute structure
0102  * and the control info needed to retrieve the value from the register map.
0103  */
0104 struct asc7621_param {
0105     struct sensor_device_attribute sda;
0106     u8 priority;
0107     u8 msb[3];
0108     u8 lsb[3];
0109     u8 mask[3];
0110     u8 shift[3];
0111 };
0112 
0113 /*
0114  * This is the map that ultimately indicates whether we'll be
0115  * retrieving a register value or not, and at what frequency.
0116  */
0117 static u8 asc7621_register_priorities[255];
0118 
0119 static struct asc7621_data *asc7621_update_device(struct device *dev);
0120 
0121 static inline u8 read_byte(struct i2c_client *client, u8 reg)
0122 {
0123     int res = i2c_smbus_read_byte_data(client, reg);
0124     if (res < 0) {
0125         dev_err(&client->dev,
0126             "Unable to read from register 0x%02x.\n", reg);
0127         return 0;
0128     }
0129     return res & 0xff;
0130 }
0131 
0132 static inline int write_byte(struct i2c_client *client, u8 reg, u8 data)
0133 {
0134     int res = i2c_smbus_write_byte_data(client, reg, data);
0135     if (res < 0) {
0136         dev_err(&client->dev,
0137             "Unable to write value 0x%02x to register 0x%02x.\n",
0138             data, reg);
0139     }
0140     return res;
0141 }
0142 
0143 /*
0144  * Data Handlers
0145  * Each function handles the formatting, storage
0146  * and retrieval of like parameters.
0147  */
0148 
0149 #define SETUP_SHOW_DATA_PARAM(d, a) \
0150     struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
0151     struct asc7621_data *data = asc7621_update_device(d); \
0152     struct asc7621_param *param = to_asc7621_param(sda)
0153 
0154 #define SETUP_STORE_DATA_PARAM(d, a) \
0155     struct sensor_device_attribute *sda = to_sensor_dev_attr(a); \
0156     struct i2c_client *client = to_i2c_client(d); \
0157     struct asc7621_data *data = i2c_get_clientdata(client); \
0158     struct asc7621_param *param = to_asc7621_param(sda)
0159 
0160 /*
0161  * u8 is just what it sounds like...an unsigned byte with no
0162  * special formatting.
0163  */
0164 static ssize_t show_u8(struct device *dev, struct device_attribute *attr,
0165                char *buf)
0166 {
0167     SETUP_SHOW_DATA_PARAM(dev, attr);
0168 
0169     return sprintf(buf, "%u\n", data->reg[param->msb[0]]);
0170 }
0171 
0172 static ssize_t store_u8(struct device *dev, struct device_attribute *attr,
0173             const char *buf, size_t count)
0174 {
0175     SETUP_STORE_DATA_PARAM(dev, attr);
0176     long reqval;
0177 
0178     if (kstrtol(buf, 10, &reqval))
0179         return -EINVAL;
0180 
0181     reqval = clamp_val(reqval, 0, 255);
0182 
0183     mutex_lock(&data->update_lock);
0184     data->reg[param->msb[0]] = reqval;
0185     write_byte(client, param->msb[0], reqval);
0186     mutex_unlock(&data->update_lock);
0187     return count;
0188 }
0189 
0190 /*
0191  * Many of the config values occupy only a few bits of a register.
0192  */
0193 static ssize_t show_bitmask(struct device *dev,
0194                 struct device_attribute *attr, char *buf)
0195 {
0196     SETUP_SHOW_DATA_PARAM(dev, attr);
0197 
0198     return sprintf(buf, "%u\n",
0199                (data->reg[param->msb[0]] >> param->
0200             shift[0]) & param->mask[0]);
0201 }
0202 
0203 static ssize_t store_bitmask(struct device *dev,
0204                  struct device_attribute *attr,
0205                  const char *buf, size_t count)
0206 {
0207     SETUP_STORE_DATA_PARAM(dev, attr);
0208     long reqval;
0209     u8 currval;
0210 
0211     if (kstrtol(buf, 10, &reqval))
0212         return -EINVAL;
0213 
0214     reqval = clamp_val(reqval, 0, param->mask[0]);
0215 
0216     reqval = (reqval & param->mask[0]) << param->shift[0];
0217 
0218     mutex_lock(&data->update_lock);
0219     currval = read_byte(client, param->msb[0]);
0220     reqval |= (currval & ~(param->mask[0] << param->shift[0]));
0221     data->reg[param->msb[0]] = reqval;
0222     write_byte(client, param->msb[0], reqval);
0223     mutex_unlock(&data->update_lock);
0224     return count;
0225 }
0226 
0227 /*
0228  * 16 bit fan rpm values
0229  * reported by the device as the number of 11.111us periods (90khz)
0230  * between full fan rotations.  Therefore...
0231  * RPM = (90000 * 60) / register value
0232  */
0233 static ssize_t show_fan16(struct device *dev,
0234               struct device_attribute *attr, char *buf)
0235 {
0236     SETUP_SHOW_DATA_PARAM(dev, attr);
0237     u16 regval;
0238 
0239     mutex_lock(&data->update_lock);
0240     regval = (data->reg[param->msb[0]] << 8) | data->reg[param->lsb[0]];
0241     mutex_unlock(&data->update_lock);
0242 
0243     return sprintf(buf, "%u\n",
0244                (regval == 0 ? -1 : (regval) ==
0245             0xffff ? 0 : 5400000 / regval));
0246 }
0247 
0248 static ssize_t store_fan16(struct device *dev,
0249                struct device_attribute *attr, const char *buf,
0250                size_t count)
0251 {
0252     SETUP_STORE_DATA_PARAM(dev, attr);
0253     long reqval;
0254 
0255     if (kstrtol(buf, 10, &reqval))
0256         return -EINVAL;
0257 
0258     /*
0259      * If a minimum RPM of zero is requested, then we set the register to
0260      * 0xffff. This value allows the fan to be stopped completely without
0261      * generating an alarm.
0262      */
0263     reqval =
0264         (reqval <= 0 ? 0xffff : clamp_val(5400000 / reqval, 0, 0xfffe));
0265 
0266     mutex_lock(&data->update_lock);
0267     data->reg[param->msb[0]] = (reqval >> 8) & 0xff;
0268     data->reg[param->lsb[0]] = reqval & 0xff;
0269     write_byte(client, param->msb[0], data->reg[param->msb[0]]);
0270     write_byte(client, param->lsb[0], data->reg[param->lsb[0]]);
0271     mutex_unlock(&data->update_lock);
0272 
0273     return count;
0274 }
0275 
0276 /*
0277  * Voltages are scaled in the device so that the nominal voltage
0278  * is 3/4ths of the 0-255 range (i.e. 192).
0279  * If all voltages are 'normal' then all voltage registers will
0280  * read 0xC0.
0281  *
0282  * The data sheet provides us with the 3/4 scale value for each voltage
0283  * which is stored in in_scaling.  The sda->index parameter value provides
0284  * the index into in_scaling.
0285  *
0286  * NOTE: The chip expects the first 2 inputs be 2.5 and 2.25 volts
0287  * respectively. That doesn't mean that's what the motherboard provides. :)
0288  */
0289 
0290 static const int asc7621_in_scaling[] = {
0291     2500, 2250, 3300, 5000, 12000
0292 };
0293 
0294 static ssize_t show_in10(struct device *dev, struct device_attribute *attr,
0295              char *buf)
0296 {
0297     SETUP_SHOW_DATA_PARAM(dev, attr);
0298     u16 regval;
0299     u8 nr = sda->index;
0300 
0301     mutex_lock(&data->update_lock);
0302     regval = (data->reg[param->msb[0]] << 8) | (data->reg[param->lsb[0]]);
0303     mutex_unlock(&data->update_lock);
0304 
0305     /* The LSB value is a 2-bit scaling of the MSB's LSbit value. */
0306     regval = (regval >> 6) * asc7621_in_scaling[nr] / (0xc0 << 2);
0307 
0308     return sprintf(buf, "%u\n", regval);
0309 }
0310 
0311 /* 8 bit voltage values (the mins and maxs) */
0312 static ssize_t show_in8(struct device *dev, struct device_attribute *attr,
0313             char *buf)
0314 {
0315     SETUP_SHOW_DATA_PARAM(dev, attr);
0316     u8 nr = sda->index;
0317 
0318     return sprintf(buf, "%u\n",
0319                ((data->reg[param->msb[0]] *
0320              asc7621_in_scaling[nr]) / 0xc0));
0321 }
0322 
0323 static ssize_t store_in8(struct device *dev, struct device_attribute *attr,
0324              const char *buf, size_t count)
0325 {
0326     SETUP_STORE_DATA_PARAM(dev, attr);
0327     long reqval;
0328     u8 nr = sda->index;
0329 
0330     if (kstrtol(buf, 10, &reqval))
0331         return -EINVAL;
0332 
0333     reqval = clamp_val(reqval, 0, 0xffff);
0334 
0335     reqval = reqval * 0xc0 / asc7621_in_scaling[nr];
0336 
0337     reqval = clamp_val(reqval, 0, 0xff);
0338 
0339     mutex_lock(&data->update_lock);
0340     data->reg[param->msb[0]] = reqval;
0341     write_byte(client, param->msb[0], reqval);
0342     mutex_unlock(&data->update_lock);
0343 
0344     return count;
0345 }
0346 
0347 static ssize_t show_temp8(struct device *dev,
0348               struct device_attribute *attr, char *buf)
0349 {
0350     SETUP_SHOW_DATA_PARAM(dev, attr);
0351 
0352     return sprintf(buf, "%d\n", ((s8) data->reg[param->msb[0]]) * 1000);
0353 }
0354 
0355 static ssize_t store_temp8(struct device *dev,
0356                struct device_attribute *attr, const char *buf,
0357                size_t count)
0358 {
0359     SETUP_STORE_DATA_PARAM(dev, attr);
0360     long reqval;
0361     s8 temp;
0362 
0363     if (kstrtol(buf, 10, &reqval))
0364         return -EINVAL;
0365 
0366     reqval = clamp_val(reqval, -127000, 127000);
0367 
0368     temp = reqval / 1000;
0369 
0370     mutex_lock(&data->update_lock);
0371     data->reg[param->msb[0]] = temp;
0372     write_byte(client, param->msb[0], temp);
0373     mutex_unlock(&data->update_lock);
0374     return count;
0375 }
0376 
0377 /*
0378  * Temperatures that occupy 2 bytes always have the whole
0379  * number of degrees in the MSB with some part of the LSB
0380  * indicating fractional degrees.
0381  */
0382 
0383 /*   mmmmmmmm.llxxxxxx */
0384 static ssize_t show_temp10(struct device *dev,
0385                struct device_attribute *attr, char *buf)
0386 {
0387     SETUP_SHOW_DATA_PARAM(dev, attr);
0388     u8 msb, lsb;
0389     int temp;
0390 
0391     mutex_lock(&data->update_lock);
0392     msb = data->reg[param->msb[0]];
0393     lsb = (data->reg[param->lsb[0]] >> 6) & 0x03;
0394     temp = (((s8) msb) * 1000) + (lsb * 250);
0395     mutex_unlock(&data->update_lock);
0396 
0397     return sprintf(buf, "%d\n", temp);
0398 }
0399 
0400 /*   mmmmmm.ll */
0401 static ssize_t show_temp62(struct device *dev,
0402                struct device_attribute *attr, char *buf)
0403 {
0404     SETUP_SHOW_DATA_PARAM(dev, attr);
0405     u8 regval = data->reg[param->msb[0]];
0406     int temp = ((s8) (regval & 0xfc) * 1000) + ((regval & 0x03) * 250);
0407 
0408     return sprintf(buf, "%d\n", temp);
0409 }
0410 
0411 static ssize_t store_temp62(struct device *dev,
0412                 struct device_attribute *attr, const char *buf,
0413                 size_t count)
0414 {
0415     SETUP_STORE_DATA_PARAM(dev, attr);
0416     long reqval, i, f;
0417     s8 temp;
0418 
0419     if (kstrtol(buf, 10, &reqval))
0420         return -EINVAL;
0421 
0422     reqval = clamp_val(reqval, -32000, 31750);
0423     i = reqval / 1000;
0424     f = reqval - (i * 1000);
0425     temp = i << 2;
0426     temp |= f / 250;
0427 
0428     mutex_lock(&data->update_lock);
0429     data->reg[param->msb[0]] = temp;
0430     write_byte(client, param->msb[0], temp);
0431     mutex_unlock(&data->update_lock);
0432     return count;
0433 }
0434 
0435 /*
0436  * The aSC7621 doesn't provide an "auto_point2".  Instead, you
0437  * specify the auto_point1 and a range.  To keep with the sysfs
0438  * hwmon specs, we synthesize the auto_point_2 from them.
0439  */
0440 
0441 static const u32 asc7621_range_map[] = {
0442     2000, 2500, 3330, 4000, 5000, 6670, 8000, 10000,
0443     13330, 16000, 20000, 26670, 32000, 40000, 53330, 80000,
0444 };
0445 
0446 static ssize_t show_ap2_temp(struct device *dev,
0447                  struct device_attribute *attr, char *buf)
0448 {
0449     SETUP_SHOW_DATA_PARAM(dev, attr);
0450     long auto_point1;
0451     u8 regval;
0452     int temp;
0453 
0454     mutex_lock(&data->update_lock);
0455     auto_point1 = ((s8) data->reg[param->msb[1]]) * 1000;
0456     regval =
0457         ((data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0]);
0458     temp = auto_point1 + asc7621_range_map[clamp_val(regval, 0, 15)];
0459     mutex_unlock(&data->update_lock);
0460 
0461     return sprintf(buf, "%d\n", temp);
0462 
0463 }
0464 
0465 static ssize_t store_ap2_temp(struct device *dev,
0466                   struct device_attribute *attr,
0467                   const char *buf, size_t count)
0468 {
0469     SETUP_STORE_DATA_PARAM(dev, attr);
0470     long reqval, auto_point1;
0471     int i;
0472     u8 currval, newval = 0;
0473 
0474     if (kstrtol(buf, 10, &reqval))
0475         return -EINVAL;
0476 
0477     mutex_lock(&data->update_lock);
0478     auto_point1 = data->reg[param->msb[1]] * 1000;
0479     reqval = clamp_val(reqval, auto_point1 + 2000, auto_point1 + 80000);
0480 
0481     for (i = ARRAY_SIZE(asc7621_range_map) - 1; i >= 0; i--) {
0482         if (reqval >= auto_point1 + asc7621_range_map[i]) {
0483             newval = i;
0484             break;
0485         }
0486     }
0487 
0488     newval = (newval & param->mask[0]) << param->shift[0];
0489     currval = read_byte(client, param->msb[0]);
0490     newval |= (currval & ~(param->mask[0] << param->shift[0]));
0491     data->reg[param->msb[0]] = newval;
0492     write_byte(client, param->msb[0], newval);
0493     mutex_unlock(&data->update_lock);
0494     return count;
0495 }
0496 
0497 static ssize_t show_pwm_ac(struct device *dev,
0498                struct device_attribute *attr, char *buf)
0499 {
0500     SETUP_SHOW_DATA_PARAM(dev, attr);
0501     u8 config, altbit, regval;
0502     static const u8 map[] = {
0503         0x01, 0x02, 0x04, 0x1f, 0x00, 0x06, 0x07, 0x10,
0504         0x08, 0x0f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f
0505     };
0506 
0507     mutex_lock(&data->update_lock);
0508     config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
0509     altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
0510     regval = config | (altbit << 3);
0511     mutex_unlock(&data->update_lock);
0512 
0513     return sprintf(buf, "%u\n", map[clamp_val(regval, 0, 15)]);
0514 }
0515 
0516 static ssize_t store_pwm_ac(struct device *dev,
0517                 struct device_attribute *attr,
0518                 const char *buf, size_t count)
0519 {
0520     SETUP_STORE_DATA_PARAM(dev, attr);
0521     unsigned long reqval;
0522     u8 currval, config, altbit, newval;
0523     static const u16 map[] = {
0524         0x04, 0x00, 0x01, 0xff, 0x02, 0xff, 0x05, 0x06,
0525         0x08, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
0526         0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0527         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
0528     };
0529 
0530     if (kstrtoul(buf, 10, &reqval))
0531         return -EINVAL;
0532 
0533     if (reqval > 31)
0534         return -EINVAL;
0535 
0536     reqval = map[reqval];
0537     if (reqval == 0xff)
0538         return -EINVAL;
0539 
0540     config = reqval & 0x07;
0541     altbit = (reqval >> 3) & 0x01;
0542 
0543     config = (config & param->mask[0]) << param->shift[0];
0544     altbit = (altbit & param->mask[1]) << param->shift[1];
0545 
0546     mutex_lock(&data->update_lock);
0547     currval = read_byte(client, param->msb[0]);
0548     newval = config | (currval & ~(param->mask[0] << param->shift[0]));
0549     newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
0550     data->reg[param->msb[0]] = newval;
0551     write_byte(client, param->msb[0], newval);
0552     mutex_unlock(&data->update_lock);
0553     return count;
0554 }
0555 
0556 static ssize_t show_pwm_enable(struct device *dev,
0557                    struct device_attribute *attr, char *buf)
0558 {
0559     SETUP_SHOW_DATA_PARAM(dev, attr);
0560     u8 config, altbit, minoff, val, newval;
0561 
0562     mutex_lock(&data->update_lock);
0563     config = (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
0564     altbit = (data->reg[param->msb[1]] >> param->shift[1]) & param->mask[1];
0565     minoff = (data->reg[param->msb[2]] >> param->shift[2]) & param->mask[2];
0566     mutex_unlock(&data->update_lock);
0567 
0568     val = config | (altbit << 3);
0569 
0570     if (val == 3 || val >= 10)
0571         newval = 255;
0572     else if (val == 4)
0573         newval = 0;
0574     else if (val == 7)
0575         newval = 1;
0576     else if (minoff == 1)
0577         newval = 2;
0578     else
0579         newval = 3;
0580 
0581     return sprintf(buf, "%u\n", newval);
0582 }
0583 
0584 static ssize_t store_pwm_enable(struct device *dev,
0585                 struct device_attribute *attr,
0586                 const char *buf, size_t count)
0587 {
0588     SETUP_STORE_DATA_PARAM(dev, attr);
0589     long reqval;
0590     u8 currval, config, altbit, newval, minoff = 255;
0591 
0592     if (kstrtol(buf, 10, &reqval))
0593         return -EINVAL;
0594 
0595     switch (reqval) {
0596     case 0:
0597         newval = 0x04;
0598         break;
0599     case 1:
0600         newval = 0x07;
0601         break;
0602     case 2:
0603         newval = 0x00;
0604         minoff = 1;
0605         break;
0606     case 3:
0607         newval = 0x00;
0608         minoff = 0;
0609         break;
0610     case 255:
0611         newval = 0x03;
0612         break;
0613     default:
0614         return -EINVAL;
0615     }
0616 
0617     config = newval & 0x07;
0618     altbit = (newval >> 3) & 0x01;
0619 
0620     mutex_lock(&data->update_lock);
0621     config = (config & param->mask[0]) << param->shift[0];
0622     altbit = (altbit & param->mask[1]) << param->shift[1];
0623     currval = read_byte(client, param->msb[0]);
0624     newval = config | (currval & ~(param->mask[0] << param->shift[0]));
0625     newval = altbit | (newval & ~(param->mask[1] << param->shift[1]));
0626     data->reg[param->msb[0]] = newval;
0627     write_byte(client, param->msb[0], newval);
0628     if (minoff < 255) {
0629         minoff = (minoff & param->mask[2]) << param->shift[2];
0630         currval = read_byte(client, param->msb[2]);
0631         newval =
0632             minoff | (currval & ~(param->mask[2] << param->shift[2]));
0633         data->reg[param->msb[2]] = newval;
0634         write_byte(client, param->msb[2], newval);
0635     }
0636     mutex_unlock(&data->update_lock);
0637     return count;
0638 }
0639 
0640 static const u32 asc7621_pwm_freq_map[] = {
0641     10, 15, 23, 30, 38, 47, 62, 94,
0642     23000, 24000, 25000, 26000, 27000, 28000, 29000, 30000
0643 };
0644 
0645 static ssize_t show_pwm_freq(struct device *dev,
0646                  struct device_attribute *attr, char *buf)
0647 {
0648     SETUP_SHOW_DATA_PARAM(dev, attr);
0649     u8 regval =
0650         (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
0651 
0652     regval = clamp_val(regval, 0, 15);
0653 
0654     return sprintf(buf, "%u\n", asc7621_pwm_freq_map[regval]);
0655 }
0656 
0657 static ssize_t store_pwm_freq(struct device *dev,
0658                   struct device_attribute *attr,
0659                   const char *buf, size_t count)
0660 {
0661     SETUP_STORE_DATA_PARAM(dev, attr);
0662     unsigned long reqval;
0663     u8 currval, newval = 255;
0664     int i;
0665 
0666     if (kstrtoul(buf, 10, &reqval))
0667         return -EINVAL;
0668 
0669     for (i = 0; i < ARRAY_SIZE(asc7621_pwm_freq_map); i++) {
0670         if (reqval == asc7621_pwm_freq_map[i]) {
0671             newval = i;
0672             break;
0673         }
0674     }
0675     if (newval == 255)
0676         return -EINVAL;
0677 
0678     newval = (newval & param->mask[0]) << param->shift[0];
0679 
0680     mutex_lock(&data->update_lock);
0681     currval = read_byte(client, param->msb[0]);
0682     newval |= (currval & ~(param->mask[0] << param->shift[0]));
0683     data->reg[param->msb[0]] = newval;
0684     write_byte(client, param->msb[0], newval);
0685     mutex_unlock(&data->update_lock);
0686     return count;
0687 }
0688 
0689 static const u32 asc7621_pwm_auto_spinup_map[] =  {
0690     0, 100, 250, 400, 700, 1000, 2000, 4000
0691 };
0692 
0693 static ssize_t show_pwm_ast(struct device *dev,
0694                 struct device_attribute *attr, char *buf)
0695 {
0696     SETUP_SHOW_DATA_PARAM(dev, attr);
0697     u8 regval =
0698         (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
0699 
0700     regval = clamp_val(regval, 0, 7);
0701 
0702     return sprintf(buf, "%u\n", asc7621_pwm_auto_spinup_map[regval]);
0703 
0704 }
0705 
0706 static ssize_t store_pwm_ast(struct device *dev,
0707                  struct device_attribute *attr,
0708                  const char *buf, size_t count)
0709 {
0710     SETUP_STORE_DATA_PARAM(dev, attr);
0711     long reqval;
0712     u8 currval, newval = 255;
0713     u32 i;
0714 
0715     if (kstrtol(buf, 10, &reqval))
0716         return -EINVAL;
0717 
0718     for (i = 0; i < ARRAY_SIZE(asc7621_pwm_auto_spinup_map); i++) {
0719         if (reqval == asc7621_pwm_auto_spinup_map[i]) {
0720             newval = i;
0721             break;
0722         }
0723     }
0724     if (newval == 255)
0725         return -EINVAL;
0726 
0727     newval = (newval & param->mask[0]) << param->shift[0];
0728 
0729     mutex_lock(&data->update_lock);
0730     currval = read_byte(client, param->msb[0]);
0731     newval |= (currval & ~(param->mask[0] << param->shift[0]));
0732     data->reg[param->msb[0]] = newval;
0733     write_byte(client, param->msb[0], newval);
0734     mutex_unlock(&data->update_lock);
0735     return count;
0736 }
0737 
0738 static const u32 asc7621_temp_smoothing_time_map[] = {
0739     35000, 17600, 11800, 7000, 4400, 3000, 1600, 800
0740 };
0741 
0742 static ssize_t show_temp_st(struct device *dev,
0743                 struct device_attribute *attr, char *buf)
0744 {
0745     SETUP_SHOW_DATA_PARAM(dev, attr);
0746     u8 regval =
0747         (data->reg[param->msb[0]] >> param->shift[0]) & param->mask[0];
0748     regval = clamp_val(regval, 0, 7);
0749 
0750     return sprintf(buf, "%u\n", asc7621_temp_smoothing_time_map[regval]);
0751 }
0752 
0753 static ssize_t store_temp_st(struct device *dev,
0754                  struct device_attribute *attr,
0755                  const char *buf, size_t count)
0756 {
0757     SETUP_STORE_DATA_PARAM(dev, attr);
0758     long reqval;
0759     u8 currval, newval = 255;
0760     u32 i;
0761 
0762     if (kstrtol(buf, 10, &reqval))
0763         return -EINVAL;
0764 
0765     for (i = 0; i < ARRAY_SIZE(asc7621_temp_smoothing_time_map); i++) {
0766         if (reqval == asc7621_temp_smoothing_time_map[i]) {
0767             newval = i;
0768             break;
0769         }
0770     }
0771 
0772     if (newval == 255)
0773         return -EINVAL;
0774 
0775     newval = (newval & param->mask[0]) << param->shift[0];
0776 
0777     mutex_lock(&data->update_lock);
0778     currval = read_byte(client, param->msb[0]);
0779     newval |= (currval & ~(param->mask[0] << param->shift[0]));
0780     data->reg[param->msb[0]] = newval;
0781     write_byte(client, param->msb[0], newval);
0782     mutex_unlock(&data->update_lock);
0783     return count;
0784 }
0785 
0786 /*
0787  * End of data handlers
0788  *
0789  * These defines do nothing more than make the table easier
0790  * to read when wrapped at column 80.
0791  */
0792 
0793 /*
0794  * Creates a variable length array inititalizer.
0795  * VAA(1,3,5,7) would produce {1,3,5,7}
0796  */
0797 #define VAA(args...) {args}
0798 
0799 #define PREAD(name, n, pri, rm, rl, m, s, r) \
0800     {.sda = SENSOR_ATTR(name, S_IRUGO, show_##r, NULL, n), \
0801       .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
0802       .shift[0] = s,}
0803 
0804 #define PWRITE(name, n, pri, rm, rl, m, s, r) \
0805     {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
0806       .priority = pri, .msb[0] = rm, .lsb[0] = rl, .mask[0] = m, \
0807       .shift[0] = s,}
0808 
0809 /*
0810  * PWRITEM assumes that the initializers for the .msb, .lsb, .mask and .shift
0811  * were created using the VAA macro.
0812  */
0813 #define PWRITEM(name, n, pri, rm, rl, m, s, r) \
0814     {.sda = SENSOR_ATTR(name, S_IRUGO | S_IWUSR, show_##r, store_##r, n), \
0815       .priority = pri, .msb = rm, .lsb = rl, .mask = m, .shift = s,}
0816 
0817 static struct asc7621_param asc7621_params[] = {
0818     PREAD(in0_input, 0, PRI_HIGH, 0x20, 0x13, 0, 0, in10),
0819     PREAD(in1_input, 1, PRI_HIGH, 0x21, 0x18, 0, 0, in10),
0820     PREAD(in2_input, 2, PRI_HIGH, 0x22, 0x11, 0, 0, in10),
0821     PREAD(in3_input, 3, PRI_HIGH, 0x23, 0x12, 0, 0, in10),
0822     PREAD(in4_input, 4, PRI_HIGH, 0x24, 0x14, 0, 0, in10),
0823 
0824     PWRITE(in0_min, 0, PRI_LOW, 0x44, 0, 0, 0, in8),
0825     PWRITE(in1_min, 1, PRI_LOW, 0x46, 0, 0, 0, in8),
0826     PWRITE(in2_min, 2, PRI_LOW, 0x48, 0, 0, 0, in8),
0827     PWRITE(in3_min, 3, PRI_LOW, 0x4a, 0, 0, 0, in8),
0828     PWRITE(in4_min, 4, PRI_LOW, 0x4c, 0, 0, 0, in8),
0829 
0830     PWRITE(in0_max, 0, PRI_LOW, 0x45, 0, 0, 0, in8),
0831     PWRITE(in1_max, 1, PRI_LOW, 0x47, 0, 0, 0, in8),
0832     PWRITE(in2_max, 2, PRI_LOW, 0x49, 0, 0, 0, in8),
0833     PWRITE(in3_max, 3, PRI_LOW, 0x4b, 0, 0, 0, in8),
0834     PWRITE(in4_max, 4, PRI_LOW, 0x4d, 0, 0, 0, in8),
0835 
0836     PREAD(in0_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 0, bitmask),
0837     PREAD(in1_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 1, bitmask),
0838     PREAD(in2_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 2, bitmask),
0839     PREAD(in3_alarm, 3, PRI_HIGH, 0x41, 0, 0x01, 3, bitmask),
0840     PREAD(in4_alarm, 4, PRI_HIGH, 0x42, 0, 0x01, 0, bitmask),
0841 
0842     PREAD(fan1_input, 0, PRI_HIGH, 0x29, 0x28, 0, 0, fan16),
0843     PREAD(fan2_input, 1, PRI_HIGH, 0x2b, 0x2a, 0, 0, fan16),
0844     PREAD(fan3_input, 2, PRI_HIGH, 0x2d, 0x2c, 0, 0, fan16),
0845     PREAD(fan4_input, 3, PRI_HIGH, 0x2f, 0x2e, 0, 0, fan16),
0846 
0847     PWRITE(fan1_min, 0, PRI_LOW, 0x55, 0x54, 0, 0, fan16),
0848     PWRITE(fan2_min, 1, PRI_LOW, 0x57, 0x56, 0, 0, fan16),
0849     PWRITE(fan3_min, 2, PRI_LOW, 0x59, 0x58, 0, 0, fan16),
0850     PWRITE(fan4_min, 3, PRI_LOW, 0x5b, 0x5a, 0, 0, fan16),
0851 
0852     PREAD(fan1_alarm, 0, PRI_HIGH, 0x42, 0, 0x01, 2, bitmask),
0853     PREAD(fan2_alarm, 1, PRI_HIGH, 0x42, 0, 0x01, 3, bitmask),
0854     PREAD(fan3_alarm, 2, PRI_HIGH, 0x42, 0, 0x01, 4, bitmask),
0855     PREAD(fan4_alarm, 3, PRI_HIGH, 0x42, 0, 0x01, 5, bitmask),
0856 
0857     PREAD(temp1_input, 0, PRI_HIGH, 0x25, 0x10, 0, 0, temp10),
0858     PREAD(temp2_input, 1, PRI_HIGH, 0x26, 0x15, 0, 0, temp10),
0859     PREAD(temp3_input, 2, PRI_HIGH, 0x27, 0x16, 0, 0, temp10),
0860     PREAD(temp4_input, 3, PRI_HIGH, 0x33, 0x17, 0, 0, temp10),
0861     PREAD(temp5_input, 4, PRI_HIGH, 0xf7, 0xf6, 0, 0, temp10),
0862     PREAD(temp6_input, 5, PRI_HIGH, 0xf9, 0xf8, 0, 0, temp10),
0863     PREAD(temp7_input, 6, PRI_HIGH, 0xfb, 0xfa, 0, 0, temp10),
0864     PREAD(temp8_input, 7, PRI_HIGH, 0xfd, 0xfc, 0, 0, temp10),
0865 
0866     PWRITE(temp1_min, 0, PRI_LOW, 0x4e, 0, 0, 0, temp8),
0867     PWRITE(temp2_min, 1, PRI_LOW, 0x50, 0, 0, 0, temp8),
0868     PWRITE(temp3_min, 2, PRI_LOW, 0x52, 0, 0, 0, temp8),
0869     PWRITE(temp4_min, 3, PRI_LOW, 0x34, 0, 0, 0, temp8),
0870 
0871     PWRITE(temp1_max, 0, PRI_LOW, 0x4f, 0, 0, 0, temp8),
0872     PWRITE(temp2_max, 1, PRI_LOW, 0x51, 0, 0, 0, temp8),
0873     PWRITE(temp3_max, 2, PRI_LOW, 0x53, 0, 0, 0, temp8),
0874     PWRITE(temp4_max, 3, PRI_LOW, 0x35, 0, 0, 0, temp8),
0875 
0876     PREAD(temp1_alarm, 0, PRI_HIGH, 0x41, 0, 0x01, 4, bitmask),
0877     PREAD(temp2_alarm, 1, PRI_HIGH, 0x41, 0, 0x01, 5, bitmask),
0878     PREAD(temp3_alarm, 2, PRI_HIGH, 0x41, 0, 0x01, 6, bitmask),
0879     PREAD(temp4_alarm, 3, PRI_HIGH, 0x43, 0, 0x01, 0, bitmask),
0880 
0881     PWRITE(temp1_source, 0, PRI_LOW, 0x02, 0, 0x07, 4, bitmask),
0882     PWRITE(temp2_source, 1, PRI_LOW, 0x02, 0, 0x07, 0, bitmask),
0883     PWRITE(temp3_source, 2, PRI_LOW, 0x03, 0, 0x07, 4, bitmask),
0884     PWRITE(temp4_source, 3, PRI_LOW, 0x03, 0, 0x07, 0, bitmask),
0885 
0886     PWRITE(temp1_smoothing_enable, 0, PRI_LOW, 0x62, 0, 0x01, 3, bitmask),
0887     PWRITE(temp2_smoothing_enable, 1, PRI_LOW, 0x63, 0, 0x01, 7, bitmask),
0888     PWRITE(temp3_smoothing_enable, 2, PRI_LOW, 0x63, 0, 0x01, 3, bitmask),
0889     PWRITE(temp4_smoothing_enable, 3, PRI_LOW, 0x3c, 0, 0x01, 3, bitmask),
0890 
0891     PWRITE(temp1_smoothing_time, 0, PRI_LOW, 0x62, 0, 0x07, 0, temp_st),
0892     PWRITE(temp2_smoothing_time, 1, PRI_LOW, 0x63, 0, 0x07, 4, temp_st),
0893     PWRITE(temp3_smoothing_time, 2, PRI_LOW, 0x63, 0, 0x07, 0, temp_st),
0894     PWRITE(temp4_smoothing_time, 3, PRI_LOW, 0x3c, 0, 0x07, 0, temp_st),
0895 
0896     PWRITE(temp1_auto_point1_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
0897            bitmask),
0898     PWRITE(temp2_auto_point1_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
0899            bitmask),
0900     PWRITE(temp3_auto_point1_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
0901            bitmask),
0902     PWRITE(temp4_auto_point1_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
0903            bitmask),
0904 
0905     PREAD(temp1_auto_point2_temp_hyst, 0, PRI_LOW, 0x6d, 0, 0x0f, 4,
0906           bitmask),
0907     PREAD(temp2_auto_point2_temp_hyst, 1, PRI_LOW, 0x6d, 0, 0x0f, 0,
0908           bitmask),
0909     PREAD(temp3_auto_point2_temp_hyst, 2, PRI_LOW, 0x6e, 0, 0x0f, 4,
0910           bitmask),
0911     PREAD(temp4_auto_point2_temp_hyst, 3, PRI_LOW, 0x6e, 0, 0x0f, 0,
0912           bitmask),
0913 
0914     PWRITE(temp1_auto_point1_temp, 0, PRI_LOW, 0x67, 0, 0, 0, temp8),
0915     PWRITE(temp2_auto_point1_temp, 1, PRI_LOW, 0x68, 0, 0, 0, temp8),
0916     PWRITE(temp3_auto_point1_temp, 2, PRI_LOW, 0x69, 0, 0, 0, temp8),
0917     PWRITE(temp4_auto_point1_temp, 3, PRI_LOW, 0x3b, 0, 0, 0, temp8),
0918 
0919     PWRITEM(temp1_auto_point2_temp, 0, PRI_LOW, VAA(0x5f, 0x67), VAA(0),
0920         VAA(0x0f), VAA(4), ap2_temp),
0921     PWRITEM(temp2_auto_point2_temp, 1, PRI_LOW, VAA(0x60, 0x68), VAA(0),
0922         VAA(0x0f), VAA(4), ap2_temp),
0923     PWRITEM(temp3_auto_point2_temp, 2, PRI_LOW, VAA(0x61, 0x69), VAA(0),
0924         VAA(0x0f), VAA(4), ap2_temp),
0925     PWRITEM(temp4_auto_point2_temp, 3, PRI_LOW, VAA(0x3c, 0x3b), VAA(0),
0926         VAA(0x0f), VAA(4), ap2_temp),
0927 
0928     PWRITE(temp1_crit, 0, PRI_LOW, 0x6a, 0, 0, 0, temp8),
0929     PWRITE(temp2_crit, 1, PRI_LOW, 0x6b, 0, 0, 0, temp8),
0930     PWRITE(temp3_crit, 2, PRI_LOW, 0x6c, 0, 0, 0, temp8),
0931     PWRITE(temp4_crit, 3, PRI_LOW, 0x3d, 0, 0, 0, temp8),
0932 
0933     PWRITE(temp5_enable, 4, PRI_LOW, 0x0e, 0, 0x01, 0, bitmask),
0934     PWRITE(temp6_enable, 5, PRI_LOW, 0x0e, 0, 0x01, 1, bitmask),
0935     PWRITE(temp7_enable, 6, PRI_LOW, 0x0e, 0, 0x01, 2, bitmask),
0936     PWRITE(temp8_enable, 7, PRI_LOW, 0x0e, 0, 0x01, 3, bitmask),
0937 
0938     PWRITE(remote1_offset, 0, PRI_LOW, 0x1c, 0, 0, 0, temp62),
0939     PWRITE(remote2_offset, 1, PRI_LOW, 0x1d, 0, 0, 0, temp62),
0940 
0941     PWRITE(pwm1, 0, PRI_HIGH, 0x30, 0, 0, 0, u8),
0942     PWRITE(pwm2, 1, PRI_HIGH, 0x31, 0, 0, 0, u8),
0943     PWRITE(pwm3, 2, PRI_HIGH, 0x32, 0, 0, 0, u8),
0944 
0945     PWRITE(pwm1_invert, 0, PRI_LOW, 0x5c, 0, 0x01, 4, bitmask),
0946     PWRITE(pwm2_invert, 1, PRI_LOW, 0x5d, 0, 0x01, 4, bitmask),
0947     PWRITE(pwm3_invert, 2, PRI_LOW, 0x5e, 0, 0x01, 4, bitmask),
0948 
0949     PWRITEM(pwm1_enable, 0, PRI_LOW, VAA(0x5c, 0x5c, 0x62), VAA(0, 0, 0),
0950         VAA(0x07, 0x01, 0x01), VAA(5, 3, 5), pwm_enable),
0951     PWRITEM(pwm2_enable, 1, PRI_LOW, VAA(0x5d, 0x5d, 0x62), VAA(0, 0, 0),
0952         VAA(0x07, 0x01, 0x01), VAA(5, 3, 6), pwm_enable),
0953     PWRITEM(pwm3_enable, 2, PRI_LOW, VAA(0x5e, 0x5e, 0x62), VAA(0, 0, 0),
0954         VAA(0x07, 0x01, 0x01), VAA(5, 3, 7), pwm_enable),
0955 
0956     PWRITEM(pwm1_auto_channels, 0, PRI_LOW, VAA(0x5c, 0x5c), VAA(0, 0),
0957         VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
0958     PWRITEM(pwm2_auto_channels, 1, PRI_LOW, VAA(0x5d, 0x5d), VAA(0, 0),
0959         VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
0960     PWRITEM(pwm3_auto_channels, 2, PRI_LOW, VAA(0x5e, 0x5e), VAA(0, 0),
0961         VAA(0x07, 0x01), VAA(5, 3), pwm_ac),
0962 
0963     PWRITE(pwm1_auto_point1_pwm, 0, PRI_LOW, 0x64, 0, 0, 0, u8),
0964     PWRITE(pwm2_auto_point1_pwm, 1, PRI_LOW, 0x65, 0, 0, 0, u8),
0965     PWRITE(pwm3_auto_point1_pwm, 2, PRI_LOW, 0x66, 0, 0, 0, u8),
0966 
0967     PWRITE(pwm1_auto_point2_pwm, 0, PRI_LOW, 0x38, 0, 0, 0, u8),
0968     PWRITE(pwm2_auto_point2_pwm, 1, PRI_LOW, 0x39, 0, 0, 0, u8),
0969     PWRITE(pwm3_auto_point2_pwm, 2, PRI_LOW, 0x3a, 0, 0, 0, u8),
0970 
0971     PWRITE(pwm1_freq, 0, PRI_LOW, 0x5f, 0, 0x0f, 0, pwm_freq),
0972     PWRITE(pwm2_freq, 1, PRI_LOW, 0x60, 0, 0x0f, 0, pwm_freq),
0973     PWRITE(pwm3_freq, 2, PRI_LOW, 0x61, 0, 0x0f, 0, pwm_freq),
0974 
0975     PREAD(pwm1_auto_zone_assigned, 0, PRI_LOW, 0, 0, 0x03, 2, bitmask),
0976     PREAD(pwm2_auto_zone_assigned, 1, PRI_LOW, 0, 0, 0x03, 4, bitmask),
0977     PREAD(pwm3_auto_zone_assigned, 2, PRI_LOW, 0, 0, 0x03, 6, bitmask),
0978 
0979     PWRITE(pwm1_auto_spinup_time, 0, PRI_LOW, 0x5c, 0, 0x07, 0, pwm_ast),
0980     PWRITE(pwm2_auto_spinup_time, 1, PRI_LOW, 0x5d, 0, 0x07, 0, pwm_ast),
0981     PWRITE(pwm3_auto_spinup_time, 2, PRI_LOW, 0x5e, 0, 0x07, 0, pwm_ast),
0982 
0983     PWRITE(peci_enable, 0, PRI_LOW, 0x40, 0, 0x01, 4, bitmask),
0984     PWRITE(peci_avg, 0, PRI_LOW, 0x36, 0, 0x07, 0, bitmask),
0985     PWRITE(peci_domain, 0, PRI_LOW, 0x36, 0, 0x01, 3, bitmask),
0986     PWRITE(peci_legacy, 0, PRI_LOW, 0x36, 0, 0x01, 4, bitmask),
0987     PWRITE(peci_diode, 0, PRI_LOW, 0x0e, 0, 0x07, 4, bitmask),
0988     PWRITE(peci_4domain, 0, PRI_LOW, 0x0e, 0, 0x01, 4, bitmask),
0989 
0990 };
0991 
0992 static struct asc7621_data *asc7621_update_device(struct device *dev)
0993 {
0994     struct i2c_client *client = to_i2c_client(dev);
0995     struct asc7621_data *data = i2c_get_clientdata(client);
0996     int i;
0997 
0998 /*
0999  * The asc7621 chips guarantee consistent reads of multi-byte values
1000  * regardless of the order of the reads.  No special logic is needed
1001  * so we can just read the registers in whatever  order they appear
1002  * in the asc7621_params array.
1003  */
1004 
1005     mutex_lock(&data->update_lock);
1006 
1007     /* Read all the high priority registers */
1008 
1009     if (!data->valid ||
1010         time_after(jiffies, data->last_high_reading + INTERVAL_HIGH)) {
1011 
1012         for (i = 0; i < ARRAY_SIZE(asc7621_register_priorities); i++) {
1013             if (asc7621_register_priorities[i] == PRI_HIGH) {
1014                 data->reg[i] =
1015                     i2c_smbus_read_byte_data(client, i) & 0xff;
1016             }
1017         }
1018         data->last_high_reading = jiffies;
1019     }           /* last_reading */
1020 
1021     /* Read all the low priority registers. */
1022 
1023     if (!data->valid ||
1024         time_after(jiffies, data->last_low_reading + INTERVAL_LOW)) {
1025 
1026         for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1027             if (asc7621_register_priorities[i] == PRI_LOW) {
1028                 data->reg[i] =
1029                     i2c_smbus_read_byte_data(client, i) & 0xff;
1030             }
1031         }
1032         data->last_low_reading = jiffies;
1033     }           /* last_reading */
1034 
1035     data->valid = true;
1036 
1037     mutex_unlock(&data->update_lock);
1038 
1039     return data;
1040 }
1041 
1042 /*
1043  * Standard detection and initialization below
1044  *
1045  * Helper function that checks if an address is valid
1046  * for a particular chip.
1047  */
1048 
1049 static inline int valid_address_for_chip(int chip_type, int address)
1050 {
1051     int i;
1052 
1053     for (i = 0; asc7621_chips[chip_type].addresses[i] != I2C_CLIENT_END;
1054          i++) {
1055         if (asc7621_chips[chip_type].addresses[i] == address)
1056             return 1;
1057     }
1058     return 0;
1059 }
1060 
1061 static void asc7621_init_client(struct i2c_client *client)
1062 {
1063     int value;
1064 
1065     /* Warn if part was not "READY" */
1066 
1067     value = read_byte(client, 0x40);
1068 
1069     if (value & 0x02) {
1070         dev_err(&client->dev,
1071             "Client (%d,0x%02x) config is locked.\n",
1072             i2c_adapter_id(client->adapter), client->addr);
1073     }
1074     if (!(value & 0x04)) {
1075         dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
1076             i2c_adapter_id(client->adapter), client->addr);
1077     }
1078 
1079 /*
1080  * Start monitoring
1081  *
1082  * Try to clear LOCK, Set START, save everything else
1083  */
1084     value = (value & ~0x02) | 0x01;
1085     write_byte(client, 0x40, value & 0xff);
1086 
1087 }
1088 
1089 static int
1090 asc7621_probe(struct i2c_client *client)
1091 {
1092     struct asc7621_data *data;
1093     int i, err;
1094 
1095     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1096         return -EIO;
1097 
1098     data = devm_kzalloc(&client->dev, sizeof(struct asc7621_data),
1099                 GFP_KERNEL);
1100     if (data == NULL)
1101         return -ENOMEM;
1102 
1103     i2c_set_clientdata(client, data);
1104     mutex_init(&data->update_lock);
1105 
1106     /* Initialize the asc7621 chip */
1107     asc7621_init_client(client);
1108 
1109     /* Create the sysfs entries */
1110     for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1111         err =
1112             device_create_file(&client->dev,
1113                        &(asc7621_params[i].sda.dev_attr));
1114         if (err)
1115             goto exit_remove;
1116     }
1117 
1118     data->class_dev = hwmon_device_register(&client->dev);
1119     if (IS_ERR(data->class_dev)) {
1120         err = PTR_ERR(data->class_dev);
1121         goto exit_remove;
1122     }
1123 
1124     return 0;
1125 
1126 exit_remove:
1127     for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1128         device_remove_file(&client->dev,
1129                    &(asc7621_params[i].sda.dev_attr));
1130     }
1131 
1132     return err;
1133 }
1134 
1135 static int asc7621_detect(struct i2c_client *client,
1136               struct i2c_board_info *info)
1137 {
1138     struct i2c_adapter *adapter = client->adapter;
1139     int company, verstep, chip_index;
1140 
1141     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1142         return -ENODEV;
1143 
1144     for (chip_index = FIRST_CHIP; chip_index <= LAST_CHIP; chip_index++) {
1145 
1146         if (!valid_address_for_chip(chip_index, client->addr))
1147             continue;
1148 
1149         company = read_byte(client,
1150             asc7621_chips[chip_index].company_reg);
1151         verstep = read_byte(client,
1152             asc7621_chips[chip_index].verstep_reg);
1153 
1154         if (company == asc7621_chips[chip_index].company_id &&
1155             verstep == asc7621_chips[chip_index].verstep_id) {
1156             strlcpy(info->type, asc7621_chips[chip_index].name,
1157                 I2C_NAME_SIZE);
1158 
1159             dev_info(&adapter->dev, "Matched %s at 0x%02x\n",
1160                  asc7621_chips[chip_index].name, client->addr);
1161             return 0;
1162         }
1163     }
1164 
1165     return -ENODEV;
1166 }
1167 
1168 static int asc7621_remove(struct i2c_client *client)
1169 {
1170     struct asc7621_data *data = i2c_get_clientdata(client);
1171     int i;
1172 
1173     hwmon_device_unregister(data->class_dev);
1174 
1175     for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1176         device_remove_file(&client->dev,
1177                    &(asc7621_params[i].sda.dev_attr));
1178     }
1179 
1180     return 0;
1181 }
1182 
1183 static const struct i2c_device_id asc7621_id[] = {
1184     {"asc7621", asc7621},
1185     {"asc7621a", asc7621a},
1186     {},
1187 };
1188 
1189 MODULE_DEVICE_TABLE(i2c, asc7621_id);
1190 
1191 static struct i2c_driver asc7621_driver = {
1192     .class = I2C_CLASS_HWMON,
1193     .driver = {
1194         .name = "asc7621",
1195     },
1196     .probe_new = asc7621_probe,
1197     .remove = asc7621_remove,
1198     .id_table = asc7621_id,
1199     .detect = asc7621_detect,
1200     .address_list = normal_i2c,
1201 };
1202 
1203 static int __init sm_asc7621_init(void)
1204 {
1205     int i, j;
1206 /*
1207  * Collect all the registers needed into a single array.
1208  * This way, if a register isn't actually used for anything,
1209  * we don't retrieve it.
1210  */
1211 
1212     for (i = 0; i < ARRAY_SIZE(asc7621_params); i++) {
1213         for (j = 0; j < ARRAY_SIZE(asc7621_params[i].msb); j++)
1214             asc7621_register_priorities[asc7621_params[i].msb[j]] =
1215                 asc7621_params[i].priority;
1216         for (j = 0; j < ARRAY_SIZE(asc7621_params[i].lsb); j++)
1217             asc7621_register_priorities[asc7621_params[i].lsb[j]] =
1218                 asc7621_params[i].priority;
1219     }
1220     return i2c_add_driver(&asc7621_driver);
1221 }
1222 
1223 static void __exit sm_asc7621_exit(void)
1224 {
1225     i2c_del_driver(&asc7621_driver);
1226 }
1227 
1228 MODULE_LICENSE("GPL");
1229 MODULE_AUTHOR("George Joseph");
1230 MODULE_DESCRIPTION("Andigilog aSC7621 and aSC7621a driver");
1231 
1232 module_init(sm_asc7621_init);
1233 module_exit(sm_asc7621_exit);