Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * tc654.c - Linux kernel modules for fan speed controller
0004  *
0005  * Copyright (C) 2016 Allied Telesis Labs NZ
0006  */
0007 
0008 #include <linux/bitops.h>
0009 #include <linux/err.h>
0010 #include <linux/hwmon.h>
0011 #include <linux/hwmon-sysfs.h>
0012 #include <linux/i2c.h>
0013 #include <linux/init.h>
0014 #include <linux/jiffies.h>
0015 #include <linux/module.h>
0016 #include <linux/mutex.h>
0017 #include <linux/slab.h>
0018 #include <linux/thermal.h>
0019 #include <linux/util_macros.h>
0020 
0021 enum tc654_regs {
0022     TC654_REG_RPM1 = 0x00,  /* RPM Output 1 */
0023     TC654_REG_RPM2 = 0x01,  /* RPM Output 2 */
0024     TC654_REG_FAN_FAULT1 = 0x02,    /* Fan Fault 1 Threshold */
0025     TC654_REG_FAN_FAULT2 = 0x03,    /* Fan Fault 2 Threshold */
0026     TC654_REG_CONFIG = 0x04,    /* Configuration */
0027     TC654_REG_STATUS = 0x05,    /* Status */
0028     TC654_REG_DUTY_CYCLE = 0x06,    /* Fan Speed Duty Cycle */
0029     TC654_REG_MFR_ID = 0x07,    /* Manufacturer Identification */
0030     TC654_REG_VER_ID = 0x08,    /* Version Identification */
0031 };
0032 
0033 /* Macros to easily index the registers */
0034 #define TC654_REG_RPM(idx)      (TC654_REG_RPM1 + (idx))
0035 #define TC654_REG_FAN_FAULT(idx)    (TC654_REG_FAN_FAULT1 + (idx))
0036 
0037 /* Config register bits */
0038 #define TC654_REG_CONFIG_RES        BIT(6)  /* Resolution Selection */
0039 #define TC654_REG_CONFIG_DUTYC      BIT(5)  /* Duty Cycle Control */
0040 #define TC654_REG_CONFIG_SDM        BIT(0)  /* Shutdown Mode */
0041 
0042 /* Status register bits */
0043 #define TC654_REG_STATUS_F2F        BIT(1)  /* Fan 2 Fault */
0044 #define TC654_REG_STATUS_F1F        BIT(0)  /* Fan 1 Fault */
0045 
0046 /* RPM resolution for RPM Output registers */
0047 #define TC654_HIGH_RPM_RESOLUTION   25  /* 25 RPM resolution */
0048 #define TC654_LOW_RPM_RESOLUTION    50  /* 50 RPM resolution */
0049 
0050 /* Convert to the fan fault RPM threshold from register value */
0051 #define TC654_FAN_FAULT_FROM_REG(val)   ((val) * 50)    /* 50 RPM resolution */
0052 
0053 /* Convert to register value from the fan fault RPM threshold */
0054 #define TC654_FAN_FAULT_TO_REG(val) (((val) / 50) & 0xff)
0055 
0056 /* Register data is read (and cached) at most once per second. */
0057 #define TC654_UPDATE_INTERVAL       HZ
0058 
0059 struct tc654_data {
0060     struct i2c_client *client;
0061 
0062     /* update mutex */
0063     struct mutex update_lock;
0064 
0065     /* tc654 register cache */
0066     bool valid;
0067     unsigned long last_updated; /* in jiffies */
0068 
0069     u8 rpm_output[2];   /* The fan RPM data for fans 1 and 2 is then
0070                  * written to registers RPM1 and RPM2
0071                  */
0072     u8 fan_fault[2];    /* The Fan Fault Threshold Registers are used to
0073                  * set the fan fault threshold levels for fan 1
0074                  * and fan 2
0075                  */
0076     u8 config;  /* The Configuration Register is an 8-bit read/
0077              * writable multi-function control register
0078              *   7: Fan Fault Clear
0079              *      1 = Clear Fan Fault
0080              *      0 = Normal Operation (default)
0081              *   6: Resolution Selection for RPM Output Registers
0082              *      RPM Output Registers (RPM1 and RPM2) will be
0083              *      set for
0084              *      1 = 25 RPM (9-bit) resolution
0085              *      0 = 50 RPM (8-bit) resolution (default)
0086              *   5: Duty Cycle Control Method
0087              *      The V OUT duty cycle will be controlled via
0088              *      1 = the SMBus interface.
0089              *      0 = via the V IN analog input pin. (default)
0090              * 4,3: Fan 2 Pulses Per Rotation
0091              *      00 = 1
0092              *      01 = 2 (default)
0093              *      10 = 4
0094              *      11 = 8
0095              * 2,1: Fan 1 Pulses Per Rotation
0096              *      00 = 1
0097              *      01 = 2 (default)
0098              *      10 = 4
0099              *      11 = 8
0100              *   0: Shutdown Mode
0101              *      1 = Shutdown mode.
0102              *      0 = Normal operation. (default)
0103              */
0104     u8 status;  /* The Status register provides all the information
0105              * about what is going on within the TC654/TC655
0106              * devices.
0107              * 7,6: Unimplemented, Read as '0'
0108              *   5: Over-Temperature Fault Condition
0109              *      1 = Over-Temperature condition has occurred
0110              *      0 = Normal operation. V IN is less than 2.6V
0111              *   4: RPM2 Counter Overflow
0112              *      1 = Fault condition
0113              *      0 = Normal operation
0114              *   3: RPM1 Counter Overflow
0115              *      1 = Fault condition
0116              *      0 = Normal operation
0117              *   2: V IN Input Status
0118              *      1 = V IN is open
0119              *      0 = Normal operation. voltage present at V IN
0120              *   1: Fan 2 Fault
0121              *      1 = Fault condition
0122              *      0 = Normal operation
0123              *   0: Fan 1 Fault
0124              *      1 = Fault condition
0125              *      0 = Normal operation
0126              */
0127     u8 duty_cycle;  /* The DUTY_CYCLE register is a 4-bit read/
0128              * writable register used to control the duty
0129              * cycle of the V OUT output.
0130              */
0131 };
0132 
0133 /* helper to grab and cache data, at most one time per second */
0134 static struct tc654_data *tc654_update_client(struct device *dev)
0135 {
0136     struct tc654_data *data = dev_get_drvdata(dev);
0137     struct i2c_client *client = data->client;
0138     int ret = 0;
0139 
0140     mutex_lock(&data->update_lock);
0141     if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) &&
0142         likely(data->valid))
0143         goto out;
0144 
0145     ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0));
0146     if (ret < 0)
0147         goto out;
0148     data->rpm_output[0] = ret;
0149 
0150     ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1));
0151     if (ret < 0)
0152         goto out;
0153     data->rpm_output[1] = ret;
0154 
0155     ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0));
0156     if (ret < 0)
0157         goto out;
0158     data->fan_fault[0] = ret;
0159 
0160     ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1));
0161     if (ret < 0)
0162         goto out;
0163     data->fan_fault[1] = ret;
0164 
0165     ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
0166     if (ret < 0)
0167         goto out;
0168     data->config = ret;
0169 
0170     ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS);
0171     if (ret < 0)
0172         goto out;
0173     data->status = ret;
0174 
0175     ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE);
0176     if (ret < 0)
0177         goto out;
0178     data->duty_cycle = ret & 0x0f;
0179 
0180     data->last_updated = jiffies;
0181     data->valid = true;
0182 out:
0183     mutex_unlock(&data->update_lock);
0184 
0185     if (ret < 0)        /* upon error, encode it in return value */
0186         data = ERR_PTR(ret);
0187 
0188     return data;
0189 }
0190 
0191 /*
0192  * sysfs attributes
0193  */
0194 
0195 static ssize_t fan_show(struct device *dev, struct device_attribute *da,
0196             char *buf)
0197 {
0198     int nr = to_sensor_dev_attr(da)->index;
0199     struct tc654_data *data = tc654_update_client(dev);
0200     int val;
0201 
0202     if (IS_ERR(data))
0203         return PTR_ERR(data);
0204 
0205     if (data->config & TC654_REG_CONFIG_RES)
0206         val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION;
0207     else
0208         val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION;
0209 
0210     return sprintf(buf, "%d\n", val);
0211 }
0212 
0213 static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
0214                 char *buf)
0215 {
0216     int nr = to_sensor_dev_attr(da)->index;
0217     struct tc654_data *data = tc654_update_client(dev);
0218 
0219     if (IS_ERR(data))
0220         return PTR_ERR(data);
0221 
0222     return sprintf(buf, "%d\n",
0223                TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr]));
0224 }
0225 
0226 static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
0227                  const char *buf, size_t count)
0228 {
0229     int nr = to_sensor_dev_attr(da)->index;
0230     struct tc654_data *data = dev_get_drvdata(dev);
0231     struct i2c_client *client = data->client;
0232     unsigned long val;
0233     int ret;
0234 
0235     if (kstrtoul(buf, 10, &val))
0236         return -EINVAL;
0237 
0238     val = clamp_val(val, 0, 12750);
0239 
0240     mutex_lock(&data->update_lock);
0241 
0242     data->fan_fault[nr] = TC654_FAN_FAULT_TO_REG(val);
0243     ret = i2c_smbus_write_byte_data(client, TC654_REG_FAN_FAULT(nr),
0244                     data->fan_fault[nr]);
0245 
0246     mutex_unlock(&data->update_lock);
0247     return ret < 0 ? ret : count;
0248 }
0249 
0250 static ssize_t fan_alarm_show(struct device *dev, struct device_attribute *da,
0251                   char *buf)
0252 {
0253     int nr = to_sensor_dev_attr(da)->index;
0254     struct tc654_data *data = tc654_update_client(dev);
0255     int val;
0256 
0257     if (IS_ERR(data))
0258         return PTR_ERR(data);
0259 
0260     if (nr == 0)
0261         val = !!(data->status & TC654_REG_STATUS_F1F);
0262     else
0263         val = !!(data->status & TC654_REG_STATUS_F2F);
0264 
0265     return sprintf(buf, "%d\n", val);
0266 }
0267 
0268 static const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 };
0269 
0270 static ssize_t fan_pulses_show(struct device *dev,
0271                    struct device_attribute *da, char *buf)
0272 {
0273     int nr = to_sensor_dev_attr(da)->index;
0274     struct tc654_data *data = tc654_update_client(dev);
0275     u8 val;
0276 
0277     if (IS_ERR(data))
0278         return PTR_ERR(data);
0279 
0280     val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03);
0281     return sprintf(buf, "%d\n", val);
0282 }
0283 
0284 static ssize_t fan_pulses_store(struct device *dev,
0285                 struct device_attribute *da, const char *buf,
0286                 size_t count)
0287 {
0288     int nr = to_sensor_dev_attr(da)->index;
0289     struct tc654_data *data = dev_get_drvdata(dev);
0290     struct i2c_client *client = data->client;
0291     u8 config;
0292     unsigned long val;
0293     int ret;
0294 
0295     if (kstrtoul(buf, 10, &val))
0296         return -EINVAL;
0297 
0298     switch (val) {
0299     case 1:
0300         config = 0;
0301         break;
0302     case 2:
0303         config = 1;
0304         break;
0305     case 4:
0306         config = 2;
0307         break;
0308     case 8:
0309         config = 3;
0310         break;
0311     default:
0312         return -EINVAL;
0313     }
0314 
0315     mutex_lock(&data->update_lock);
0316 
0317     data->config &= ~(0x03 << TC654_FAN_PULSE_SHIFT[nr]);
0318     data->config |= (config << TC654_FAN_PULSE_SHIFT[nr]);
0319     ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
0320 
0321     mutex_unlock(&data->update_lock);
0322     return ret < 0 ? ret : count;
0323 }
0324 
0325 static ssize_t pwm_mode_show(struct device *dev, struct device_attribute *da,
0326                  char *buf)
0327 {
0328     struct tc654_data *data = tc654_update_client(dev);
0329 
0330     if (IS_ERR(data))
0331         return PTR_ERR(data);
0332 
0333     return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC));
0334 }
0335 
0336 static ssize_t pwm_mode_store(struct device *dev, struct device_attribute *da,
0337                   const char *buf, size_t count)
0338 {
0339     struct tc654_data *data = dev_get_drvdata(dev);
0340     struct i2c_client *client = data->client;
0341     unsigned long val;
0342     int ret;
0343 
0344     if (kstrtoul(buf, 10, &val))
0345         return -EINVAL;
0346 
0347     if (val != 0 && val != 1)
0348         return -EINVAL;
0349 
0350     mutex_lock(&data->update_lock);
0351 
0352     if (val)
0353         data->config |= TC654_REG_CONFIG_DUTYC;
0354     else
0355         data->config &= ~TC654_REG_CONFIG_DUTYC;
0356 
0357     ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
0358 
0359     mutex_unlock(&data->update_lock);
0360     return ret < 0 ? ret : count;
0361 }
0362 
0363 static const int tc654_pwm_map[16] = { 77,  88, 102, 112, 124, 136, 148, 160,
0364                       172, 184, 196, 207, 219, 231, 243, 255};
0365 
0366 static ssize_t pwm_show(struct device *dev, struct device_attribute *da,
0367             char *buf)
0368 {
0369     struct tc654_data *data = tc654_update_client(dev);
0370     int pwm;
0371 
0372     if (IS_ERR(data))
0373         return PTR_ERR(data);
0374 
0375     if (data->config & TC654_REG_CONFIG_SDM)
0376         pwm = 0;
0377     else
0378         pwm = tc654_pwm_map[data->duty_cycle];
0379 
0380     return sprintf(buf, "%d\n", pwm);
0381 }
0382 
0383 static int _set_pwm(struct tc654_data *data, unsigned long val)
0384 {
0385     struct i2c_client *client = data->client;
0386     int ret;
0387 
0388     mutex_lock(&data->update_lock);
0389 
0390     if (val == 0) {
0391         data->config |= TC654_REG_CONFIG_SDM;
0392         data->duty_cycle = 0;
0393     } else {
0394         data->config &= ~TC654_REG_CONFIG_SDM;
0395         data->duty_cycle = val - 1;
0396     }
0397 
0398     ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
0399     if (ret < 0)
0400         goto out;
0401 
0402     ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE,
0403                     data->duty_cycle);
0404 
0405 out:
0406     mutex_unlock(&data->update_lock);
0407     return ret;
0408 }
0409 
0410 static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
0411              const char *buf, size_t count)
0412 {
0413     struct tc654_data *data = dev_get_drvdata(dev);
0414     unsigned long val;
0415     int ret;
0416 
0417     if (kstrtoul(buf, 10, &val))
0418         return -EINVAL;
0419     if (val > 255)
0420         return -EINVAL;
0421     if (val > 0)
0422         val = find_closest(val, tc654_pwm_map, ARRAY_SIZE(tc654_pwm_map)) + 1;
0423 
0424     ret = _set_pwm(data, val);
0425     return ret < 0 ? ret : count;
0426 }
0427 
0428 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
0429 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
0430 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
0431 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
0432 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
0433 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
0434 static SENSOR_DEVICE_ATTR_RW(fan1_pulses, fan_pulses, 0);
0435 static SENSOR_DEVICE_ATTR_RW(fan2_pulses, fan_pulses, 1);
0436 static SENSOR_DEVICE_ATTR_RW(pwm1_mode, pwm_mode, 0);
0437 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
0438 
0439 /* Driver data */
0440 static struct attribute *tc654_attrs[] = {
0441     &sensor_dev_attr_fan1_input.dev_attr.attr,
0442     &sensor_dev_attr_fan2_input.dev_attr.attr,
0443     &sensor_dev_attr_fan1_min.dev_attr.attr,
0444     &sensor_dev_attr_fan2_min.dev_attr.attr,
0445     &sensor_dev_attr_fan1_alarm.dev_attr.attr,
0446     &sensor_dev_attr_fan2_alarm.dev_attr.attr,
0447     &sensor_dev_attr_fan1_pulses.dev_attr.attr,
0448     &sensor_dev_attr_fan2_pulses.dev_attr.attr,
0449     &sensor_dev_attr_pwm1_mode.dev_attr.attr,
0450     &sensor_dev_attr_pwm1.dev_attr.attr,
0451     NULL
0452 };
0453 
0454 ATTRIBUTE_GROUPS(tc654);
0455 
0456 /*
0457  * thermal cooling device functions
0458  *
0459  * Account for the "ShutDown Mode (SDM)" state by offsetting
0460  * the 16 PWM duty cycle states by 1.
0461  *
0462  * State  0 =   0% PWM | Shutdown - Fan(s) are off
0463  * State  1 =  30% PWM | duty_cycle =  0
0464  * State  2 = ~35% PWM | duty_cycle =  1
0465  * [...]
0466  * State 15 = ~95% PWM | duty_cycle = 14
0467  * State 16 = 100% PWM | duty_cycle = 15
0468  */
0469 #define TC654_MAX_COOLING_STATE 16
0470 
0471 static int tc654_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
0472 {
0473     *state = TC654_MAX_COOLING_STATE;
0474     return 0;
0475 }
0476 
0477 static int tc654_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
0478 {
0479     struct tc654_data *data = tc654_update_client(cdev->devdata);
0480 
0481     if (IS_ERR(data))
0482         return PTR_ERR(data);
0483 
0484     if (data->config & TC654_REG_CONFIG_SDM)
0485         *state = 0; /* FAN is off */
0486     else
0487         *state = data->duty_cycle + 1;  /* offset PWM States by 1 */
0488 
0489     return 0;
0490 }
0491 
0492 static int tc654_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
0493 {
0494     struct tc654_data *data = tc654_update_client(cdev->devdata);
0495 
0496     if (IS_ERR(data))
0497         return PTR_ERR(data);
0498 
0499     return _set_pwm(data, clamp_val(state, 0, TC654_MAX_COOLING_STATE));
0500 }
0501 
0502 static const struct thermal_cooling_device_ops tc654_fan_cool_ops = {
0503     .get_max_state = tc654_get_max_state,
0504     .get_cur_state = tc654_get_cur_state,
0505     .set_cur_state = tc654_set_cur_state,
0506 };
0507 
0508 /*
0509  * device probe and removal
0510  */
0511 
0512 static int tc654_probe(struct i2c_client *client)
0513 {
0514     struct device *dev = &client->dev;
0515     struct tc654_data *data;
0516     struct device *hwmon_dev;
0517     int ret;
0518 
0519     if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0520         return -ENODEV;
0521 
0522     data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL);
0523     if (!data)
0524         return -ENOMEM;
0525 
0526     data->client = client;
0527     mutex_init(&data->update_lock);
0528 
0529     ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
0530     if (ret < 0)
0531         return ret;
0532 
0533     data->config = ret;
0534 
0535     hwmon_dev =
0536         devm_hwmon_device_register_with_groups(dev, client->name, data,
0537                            tc654_groups);
0538     if (IS_ERR(hwmon_dev))
0539         return PTR_ERR(hwmon_dev);
0540 
0541     if (IS_ENABLED(CONFIG_THERMAL)) {
0542         struct thermal_cooling_device *cdev;
0543 
0544         cdev = devm_thermal_of_cooling_device_register(dev, dev->of_node, client->name,
0545                                    hwmon_dev, &tc654_fan_cool_ops);
0546         return PTR_ERR_OR_ZERO(cdev);
0547     }
0548 
0549     return 0;
0550 }
0551 
0552 static const struct i2c_device_id tc654_id[] = {
0553     {"tc654", 0},
0554     {"tc655", 0},
0555     {}
0556 };
0557 
0558 MODULE_DEVICE_TABLE(i2c, tc654_id);
0559 
0560 static struct i2c_driver tc654_driver = {
0561     .driver = {
0562            .name = "tc654",
0563            },
0564     .probe_new = tc654_probe,
0565     .id_table = tc654_id,
0566 };
0567 
0568 module_i2c_driver(tc654_driver);
0569 
0570 MODULE_AUTHOR("Allied Telesis Labs");
0571 MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
0572 MODULE_LICENSE("GPL");