Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Driver for Gateworks System Controller Hardware Monitor module
0004  *
0005  * Copyright (C) 2020 Gateworks Corporation
0006  */
0007 #include <linux/hwmon.h>
0008 #include <linux/hwmon-sysfs.h>
0009 #include <linux/mfd/gsc.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/regmap.h>
0014 #include <linux/slab.h>
0015 
0016 #include <linux/platform_data/gsc_hwmon.h>
0017 
0018 #define GSC_HWMON_MAX_TEMP_CH   16
0019 #define GSC_HWMON_MAX_IN_CH 16
0020 #define GSC_HWMON_MAX_FAN_CH    16
0021 
0022 #define GSC_HWMON_RESOLUTION    12
0023 #define GSC_HWMON_VREF      2500
0024 
0025 struct gsc_hwmon_data {
0026     struct gsc_dev *gsc;
0027     struct gsc_hwmon_platform_data *pdata;
0028     struct regmap *regmap;
0029     const struct gsc_hwmon_channel *temp_ch[GSC_HWMON_MAX_TEMP_CH];
0030     const struct gsc_hwmon_channel *in_ch[GSC_HWMON_MAX_IN_CH];
0031     const struct gsc_hwmon_channel *fan_ch[GSC_HWMON_MAX_FAN_CH];
0032     u32 temp_config[GSC_HWMON_MAX_TEMP_CH + 1];
0033     u32 in_config[GSC_HWMON_MAX_IN_CH + 1];
0034     u32 fan_config[GSC_HWMON_MAX_FAN_CH + 1];
0035     struct hwmon_channel_info temp_info;
0036     struct hwmon_channel_info in_info;
0037     struct hwmon_channel_info fan_info;
0038     const struct hwmon_channel_info *info[4];
0039     struct hwmon_chip_info chip;
0040 };
0041 
0042 static struct regmap_bus gsc_hwmon_regmap_bus = {
0043     .reg_read = gsc_read,
0044     .reg_write = gsc_write,
0045 };
0046 
0047 static const struct regmap_config gsc_hwmon_regmap_config = {
0048     .reg_bits = 8,
0049     .val_bits = 8,
0050     .cache_type = REGCACHE_NONE,
0051 };
0052 
0053 static ssize_t pwm_auto_point_temp_show(struct device *dev,
0054                     struct device_attribute *devattr,
0055                     char *buf)
0056 {
0057     struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
0058     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0059     u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
0060     u8 regs[2];
0061     int ret;
0062 
0063     ret = regmap_bulk_read(hwmon->regmap, reg, regs, 2);
0064     if (ret)
0065         return ret;
0066 
0067     ret = regs[0] | regs[1] << 8;
0068     return sprintf(buf, "%d\n", ret * 10);
0069 }
0070 
0071 static ssize_t pwm_auto_point_temp_store(struct device *dev,
0072                      struct device_attribute *devattr,
0073                      const char *buf, size_t count)
0074 {
0075     struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
0076     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0077     u8 reg = hwmon->pdata->fan_base + (2 * attr->index);
0078     u8 regs[2];
0079     long temp;
0080     int err;
0081 
0082     if (kstrtol(buf, 10, &temp))
0083         return -EINVAL;
0084 
0085     temp = clamp_val(temp, 0, 10000);
0086     temp = DIV_ROUND_CLOSEST(temp, 10);
0087 
0088     regs[0] = temp & 0xff;
0089     regs[1] = (temp >> 8) & 0xff;
0090     err = regmap_bulk_write(hwmon->regmap, reg, regs, 2);
0091     if (err)
0092         return err;
0093 
0094     return count;
0095 }
0096 
0097 static ssize_t pwm_auto_point_pwm_show(struct device *dev,
0098                        struct device_attribute *devattr,
0099                        char *buf)
0100 {
0101     struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
0102 
0103     return sprintf(buf, "%d\n", 255 * (50 + (attr->index * 10)) / 100);
0104 }
0105 
0106 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point1_pwm, pwm_auto_point_pwm, 0);
0107 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point1_temp, pwm_auto_point_temp, 0);
0108 
0109 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point2_pwm, pwm_auto_point_pwm, 1);
0110 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point2_temp, pwm_auto_point_temp, 1);
0111 
0112 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point3_pwm, pwm_auto_point_pwm, 2);
0113 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point3_temp, pwm_auto_point_temp, 2);
0114 
0115 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point4_pwm, pwm_auto_point_pwm, 3);
0116 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point4_temp, pwm_auto_point_temp, 3);
0117 
0118 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point5_pwm, pwm_auto_point_pwm, 4);
0119 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point5_temp, pwm_auto_point_temp, 4);
0120 
0121 static SENSOR_DEVICE_ATTR_RO(pwm1_auto_point6_pwm, pwm_auto_point_pwm, 5);
0122 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_point6_temp, pwm_auto_point_temp, 5);
0123 
0124 static struct attribute *gsc_hwmon_attributes[] = {
0125     &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
0126     &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
0127     &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
0128     &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
0129     &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
0130     &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
0131     &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
0132     &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
0133     &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
0134     &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
0135     &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
0136     &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
0137     NULL
0138 };
0139 
0140 static const struct attribute_group gsc_hwmon_group = {
0141     .attrs = gsc_hwmon_attributes,
0142 };
0143 __ATTRIBUTE_GROUPS(gsc_hwmon);
0144 
0145 static int
0146 gsc_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
0147            int channel, long *val)
0148 {
0149     struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
0150     const struct gsc_hwmon_channel *ch;
0151     int sz, ret;
0152     long tmp;
0153     u8 buf[3];
0154 
0155     switch (type) {
0156     case hwmon_in:
0157         ch = hwmon->in_ch[channel];
0158         break;
0159     case hwmon_temp:
0160         ch = hwmon->temp_ch[channel];
0161         break;
0162     case hwmon_fan:
0163         ch = hwmon->fan_ch[channel];
0164         break;
0165     default:
0166         return -EOPNOTSUPP;
0167     }
0168 
0169     sz = (ch->mode == mode_voltage_24bit) ? 3 : 2;
0170     ret = regmap_bulk_read(hwmon->regmap, ch->reg, buf, sz);
0171     if (ret)
0172         return ret;
0173 
0174     tmp = 0;
0175     while (sz-- > 0)
0176         tmp |= (buf[sz] << (8 * sz));
0177 
0178     switch (ch->mode) {
0179     case mode_temperature:
0180         if (tmp > 0x8000)
0181             tmp -= 0xffff;
0182         tmp *= 100; /* convert to millidegrees celsius */
0183         break;
0184     case mode_voltage_raw:
0185         tmp = clamp_val(tmp, 0, BIT(GSC_HWMON_RESOLUTION));
0186         /* scale based on ref voltage and ADC resolution */
0187         tmp *= GSC_HWMON_VREF;
0188         tmp >>= GSC_HWMON_RESOLUTION;
0189         /* scale based on optional voltage divider */
0190         if (ch->vdiv[0] && ch->vdiv[1]) {
0191             tmp *= (ch->vdiv[0] + ch->vdiv[1]);
0192             tmp /= ch->vdiv[1];
0193         }
0194         /* adjust by uV offset */
0195         tmp += ch->mvoffset;
0196         break;
0197     case mode_fan:
0198         tmp *= 30; /* convert to revolutions per minute */
0199         break;
0200     case mode_voltage_24bit:
0201     case mode_voltage_16bit:
0202         /* no adjustment needed */
0203         break;
0204     }
0205 
0206     *val = tmp;
0207 
0208     return 0;
0209 }
0210 
0211 static int
0212 gsc_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
0213               u32 attr, int channel, const char **buf)
0214 {
0215     struct gsc_hwmon_data *hwmon = dev_get_drvdata(dev);
0216 
0217     switch (type) {
0218     case hwmon_in:
0219         *buf = hwmon->in_ch[channel]->name;
0220         break;
0221     case hwmon_temp:
0222         *buf = hwmon->temp_ch[channel]->name;
0223         break;
0224     case hwmon_fan:
0225         *buf = hwmon->fan_ch[channel]->name;
0226         break;
0227     default:
0228         return -ENOTSUPP;
0229     }
0230 
0231     return 0;
0232 }
0233 
0234 static umode_t
0235 gsc_hwmon_is_visible(const void *_data, enum hwmon_sensor_types type, u32 attr,
0236              int ch)
0237 {
0238     return 0444;
0239 }
0240 
0241 static const struct hwmon_ops gsc_hwmon_ops = {
0242     .is_visible = gsc_hwmon_is_visible,
0243     .read = gsc_hwmon_read,
0244     .read_string = gsc_hwmon_read_string,
0245 };
0246 
0247 static struct gsc_hwmon_platform_data *
0248 gsc_hwmon_get_devtree_pdata(struct device *dev)
0249 {
0250     struct gsc_hwmon_platform_data *pdata;
0251     struct gsc_hwmon_channel *ch;
0252     struct fwnode_handle *child;
0253     struct device_node *fan;
0254     int nchannels;
0255 
0256     nchannels = device_get_child_node_count(dev);
0257     if (nchannels == 0)
0258         return ERR_PTR(-ENODEV);
0259 
0260     pdata = devm_kzalloc(dev,
0261                  sizeof(*pdata) + nchannels * sizeof(*ch),
0262                  GFP_KERNEL);
0263     if (!pdata)
0264         return ERR_PTR(-ENOMEM);
0265     ch = (struct gsc_hwmon_channel *)(pdata + 1);
0266     pdata->channels = ch;
0267     pdata->nchannels = nchannels;
0268 
0269     /* fan controller base address */
0270     fan = of_find_compatible_node(dev->parent->of_node, NULL, "gw,gsc-fan");
0271     if (fan && of_property_read_u32(fan, "reg", &pdata->fan_base)) {
0272         of_node_put(fan);
0273         dev_err(dev, "fan node without base\n");
0274         return ERR_PTR(-EINVAL);
0275     }
0276 
0277     of_node_put(fan);
0278 
0279     /* allocate structures for channels and count instances of each type */
0280     device_for_each_child_node(dev, child) {
0281         if (fwnode_property_read_string(child, "label", &ch->name)) {
0282             dev_err(dev, "channel without label\n");
0283             fwnode_handle_put(child);
0284             return ERR_PTR(-EINVAL);
0285         }
0286         if (fwnode_property_read_u32(child, "reg", &ch->reg)) {
0287             dev_err(dev, "channel without reg\n");
0288             fwnode_handle_put(child);
0289             return ERR_PTR(-EINVAL);
0290         }
0291         if (fwnode_property_read_u32(child, "gw,mode", &ch->mode)) {
0292             dev_err(dev, "channel without mode\n");
0293             fwnode_handle_put(child);
0294             return ERR_PTR(-EINVAL);
0295         }
0296         if (ch->mode > mode_max) {
0297             dev_err(dev, "invalid channel mode\n");
0298             fwnode_handle_put(child);
0299             return ERR_PTR(-EINVAL);
0300         }
0301 
0302         if (!fwnode_property_read_u32(child,
0303                           "gw,voltage-offset-microvolt",
0304                           &ch->mvoffset))
0305             ch->mvoffset /= 1000;
0306         fwnode_property_read_u32_array(child,
0307                            "gw,voltage-divider-ohms",
0308                            ch->vdiv, ARRAY_SIZE(ch->vdiv));
0309         ch++;
0310     }
0311 
0312     return pdata;
0313 }
0314 
0315 static int gsc_hwmon_probe(struct platform_device *pdev)
0316 {
0317     struct gsc_dev *gsc = dev_get_drvdata(pdev->dev.parent);
0318     struct device *dev = &pdev->dev;
0319     struct device *hwmon_dev;
0320     struct gsc_hwmon_platform_data *pdata = dev_get_platdata(dev);
0321     struct gsc_hwmon_data *hwmon;
0322     const struct attribute_group **groups;
0323     int i, i_in, i_temp, i_fan;
0324 
0325     if (!pdata) {
0326         pdata = gsc_hwmon_get_devtree_pdata(dev);
0327         if (IS_ERR(pdata))
0328             return PTR_ERR(pdata);
0329     }
0330 
0331     hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
0332     if (!hwmon)
0333         return -ENOMEM;
0334     hwmon->gsc = gsc;
0335     hwmon->pdata = pdata;
0336 
0337     hwmon->regmap = devm_regmap_init(dev, &gsc_hwmon_regmap_bus,
0338                      gsc->i2c_hwmon,
0339                      &gsc_hwmon_regmap_config);
0340     if (IS_ERR(hwmon->regmap))
0341         return PTR_ERR(hwmon->regmap);
0342 
0343     for (i = 0, i_in = 0, i_temp = 0, i_fan = 0; i < hwmon->pdata->nchannels; i++) {
0344         const struct gsc_hwmon_channel *ch = &pdata->channels[i];
0345 
0346         switch (ch->mode) {
0347         case mode_temperature:
0348             if (i_temp == GSC_HWMON_MAX_TEMP_CH) {
0349                 dev_err(gsc->dev, "too many temp channels\n");
0350                 return -EINVAL;
0351             }
0352             hwmon->temp_ch[i_temp] = ch;
0353             hwmon->temp_config[i_temp] = HWMON_T_INPUT |
0354                              HWMON_T_LABEL;
0355             i_temp++;
0356             break;
0357         case mode_fan:
0358             if (i_fan == GSC_HWMON_MAX_FAN_CH) {
0359                 dev_err(gsc->dev, "too many fan channels\n");
0360                 return -EINVAL;
0361             }
0362             hwmon->fan_ch[i_fan] = ch;
0363             hwmon->fan_config[i_fan] = HWMON_F_INPUT |
0364                            HWMON_F_LABEL;
0365             i_fan++;
0366             break;
0367         case mode_voltage_24bit:
0368         case mode_voltage_16bit:
0369         case mode_voltage_raw:
0370             if (i_in == GSC_HWMON_MAX_IN_CH) {
0371                 dev_err(gsc->dev, "too many input channels\n");
0372                 return -EINVAL;
0373             }
0374             hwmon->in_ch[i_in] = ch;
0375             hwmon->in_config[i_in] =
0376                 HWMON_I_INPUT | HWMON_I_LABEL;
0377             i_in++;
0378             break;
0379         default:
0380             dev_err(gsc->dev, "invalid mode: %d\n", ch->mode);
0381             return -EINVAL;
0382         }
0383     }
0384 
0385     /* setup config structures */
0386     hwmon->chip.ops = &gsc_hwmon_ops;
0387     hwmon->chip.info = hwmon->info;
0388     hwmon->info[0] = &hwmon->temp_info;
0389     hwmon->info[1] = &hwmon->in_info;
0390     hwmon->info[2] = &hwmon->fan_info;
0391     hwmon->temp_info.type = hwmon_temp;
0392     hwmon->temp_info.config = hwmon->temp_config;
0393     hwmon->in_info.type = hwmon_in;
0394     hwmon->in_info.config = hwmon->in_config;
0395     hwmon->fan_info.type = hwmon_fan;
0396     hwmon->fan_info.config = hwmon->fan_config;
0397 
0398     groups = pdata->fan_base ? gsc_hwmon_groups : NULL;
0399     hwmon_dev = devm_hwmon_device_register_with_info(dev,
0400                              KBUILD_MODNAME, hwmon,
0401                              &hwmon->chip, groups);
0402     return PTR_ERR_OR_ZERO(hwmon_dev);
0403 }
0404 
0405 static const struct of_device_id gsc_hwmon_of_match[] = {
0406     { .compatible = "gw,gsc-adc", },
0407     {}
0408 };
0409 
0410 static struct platform_driver gsc_hwmon_driver = {
0411     .driver = {
0412         .name = "gsc-hwmon",
0413         .of_match_table = gsc_hwmon_of_match,
0414     },
0415     .probe = gsc_hwmon_probe,
0416 };
0417 
0418 module_platform_driver(gsc_hwmon_driver);
0419 
0420 MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
0421 MODULE_DESCRIPTION("GSC hardware monitor driver");
0422 MODULE_LICENSE("GPL v2");