Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * System Control and Management Interface(SCMI) based hwmon sensor driver
0004  *
0005  * Copyright (C) 2018-2021 ARM Ltd.
0006  * Sudeep Holla <sudeep.holla@arm.com>
0007  */
0008 
0009 #include <linux/hwmon.h>
0010 #include <linux/module.h>
0011 #include <linux/scmi_protocol.h>
0012 #include <linux/slab.h>
0013 #include <linux/sysfs.h>
0014 #include <linux/thermal.h>
0015 
0016 static const struct scmi_sensor_proto_ops *sensor_ops;
0017 
0018 struct scmi_sensors {
0019     const struct scmi_protocol_handle *ph;
0020     const struct scmi_sensor_info **info[hwmon_max];
0021 };
0022 
0023 static inline u64 __pow10(u8 x)
0024 {
0025     u64 r = 1;
0026 
0027     while (x--)
0028         r *= 10;
0029 
0030     return r;
0031 }
0032 
0033 static int scmi_hwmon_scale(const struct scmi_sensor_info *sensor, u64 *value)
0034 {
0035     int scale = sensor->scale;
0036     u64 f;
0037 
0038     switch (sensor->type) {
0039     case TEMPERATURE_C:
0040     case VOLTAGE:
0041     case CURRENT:
0042         scale += 3;
0043         break;
0044     case POWER:
0045     case ENERGY:
0046         scale += 6;
0047         break;
0048     default:
0049         break;
0050     }
0051 
0052     if (scale == 0)
0053         return 0;
0054 
0055     if (abs(scale) > 19)
0056         return -E2BIG;
0057 
0058     f = __pow10(abs(scale));
0059     if (scale > 0)
0060         *value *= f;
0061     else
0062         *value = div64_u64(*value, f);
0063 
0064     return 0;
0065 }
0066 
0067 static int scmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
0068                u32 attr, int channel, long *val)
0069 {
0070     int ret;
0071     u64 value;
0072     const struct scmi_sensor_info *sensor;
0073     struct scmi_sensors *scmi_sensors = dev_get_drvdata(dev);
0074 
0075     sensor = *(scmi_sensors->info[type] + channel);
0076     ret = sensor_ops->reading_get(scmi_sensors->ph, sensor->id, &value);
0077     if (ret)
0078         return ret;
0079 
0080     ret = scmi_hwmon_scale(sensor, &value);
0081     if (!ret)
0082         *val = value;
0083 
0084     return ret;
0085 }
0086 
0087 static int
0088 scmi_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
0089                u32 attr, int channel, const char **str)
0090 {
0091     const struct scmi_sensor_info *sensor;
0092     struct scmi_sensors *scmi_sensors = dev_get_drvdata(dev);
0093 
0094     sensor = *(scmi_sensors->info[type] + channel);
0095     *str = sensor->name;
0096 
0097     return 0;
0098 }
0099 
0100 static umode_t
0101 scmi_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
0102               u32 attr, int channel)
0103 {
0104     const struct scmi_sensor_info *sensor;
0105     const struct scmi_sensors *scmi_sensors = drvdata;
0106 
0107     sensor = *(scmi_sensors->info[type] + channel);
0108     if (sensor)
0109         return 0444;
0110 
0111     return 0;
0112 }
0113 
0114 static const struct hwmon_ops scmi_hwmon_ops = {
0115     .is_visible = scmi_hwmon_is_visible,
0116     .read = scmi_hwmon_read,
0117     .read_string = scmi_hwmon_read_string,
0118 };
0119 
0120 static struct hwmon_chip_info scmi_chip_info = {
0121     .ops = &scmi_hwmon_ops,
0122     .info = NULL,
0123 };
0124 
0125 static int scmi_hwmon_add_chan_info(struct hwmon_channel_info *scmi_hwmon_chan,
0126                     struct device *dev, int num,
0127                     enum hwmon_sensor_types type, u32 config)
0128 {
0129     int i;
0130     u32 *cfg = devm_kcalloc(dev, num + 1, sizeof(*cfg), GFP_KERNEL);
0131 
0132     if (!cfg)
0133         return -ENOMEM;
0134 
0135     scmi_hwmon_chan->type = type;
0136     scmi_hwmon_chan->config = cfg;
0137     for (i = 0; i < num; i++, cfg++)
0138         *cfg = config;
0139 
0140     return 0;
0141 }
0142 
0143 static enum hwmon_sensor_types scmi_types[] = {
0144     [TEMPERATURE_C] = hwmon_temp,
0145     [VOLTAGE] = hwmon_in,
0146     [CURRENT] = hwmon_curr,
0147     [POWER] = hwmon_power,
0148     [ENERGY] = hwmon_energy,
0149 };
0150 
0151 static u32 hwmon_attributes[hwmon_max] = {
0152     [hwmon_chip] = HWMON_C_REGISTER_TZ,
0153     [hwmon_temp] = HWMON_T_INPUT | HWMON_T_LABEL,
0154     [hwmon_in] = HWMON_I_INPUT | HWMON_I_LABEL,
0155     [hwmon_curr] = HWMON_C_INPUT | HWMON_C_LABEL,
0156     [hwmon_power] = HWMON_P_INPUT | HWMON_P_LABEL,
0157     [hwmon_energy] = HWMON_E_INPUT | HWMON_E_LABEL,
0158 };
0159 
0160 static int scmi_hwmon_probe(struct scmi_device *sdev)
0161 {
0162     int i, idx;
0163     u16 nr_sensors;
0164     enum hwmon_sensor_types type;
0165     struct scmi_sensors *scmi_sensors;
0166     const struct scmi_sensor_info *sensor;
0167     int nr_count[hwmon_max] = {0}, nr_types = 0;
0168     const struct hwmon_chip_info *chip_info;
0169     struct device *hwdev, *dev = &sdev->dev;
0170     struct hwmon_channel_info *scmi_hwmon_chan;
0171     const struct hwmon_channel_info **ptr_scmi_ci;
0172     const struct scmi_handle *handle = sdev->handle;
0173     struct scmi_protocol_handle *ph;
0174 
0175     if (!handle)
0176         return -ENODEV;
0177 
0178     sensor_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_SENSOR, &ph);
0179     if (IS_ERR(sensor_ops))
0180         return PTR_ERR(sensor_ops);
0181 
0182     nr_sensors = sensor_ops->count_get(ph);
0183     if (!nr_sensors)
0184         return -EIO;
0185 
0186     scmi_sensors = devm_kzalloc(dev, sizeof(*scmi_sensors), GFP_KERNEL);
0187     if (!scmi_sensors)
0188         return -ENOMEM;
0189 
0190     scmi_sensors->ph = ph;
0191 
0192     for (i = 0; i < nr_sensors; i++) {
0193         sensor = sensor_ops->info_get(ph, i);
0194         if (!sensor)
0195             return -EINVAL;
0196 
0197         switch (sensor->type) {
0198         case TEMPERATURE_C:
0199         case VOLTAGE:
0200         case CURRENT:
0201         case POWER:
0202         case ENERGY:
0203             type = scmi_types[sensor->type];
0204             if (!nr_count[type])
0205                 nr_types++;
0206             nr_count[type]++;
0207             break;
0208         }
0209     }
0210 
0211     if (nr_count[hwmon_temp]) {
0212         nr_count[hwmon_chip]++;
0213         nr_types++;
0214     }
0215 
0216     scmi_hwmon_chan = devm_kcalloc(dev, nr_types, sizeof(*scmi_hwmon_chan),
0217                        GFP_KERNEL);
0218     if (!scmi_hwmon_chan)
0219         return -ENOMEM;
0220 
0221     ptr_scmi_ci = devm_kcalloc(dev, nr_types + 1, sizeof(*ptr_scmi_ci),
0222                    GFP_KERNEL);
0223     if (!ptr_scmi_ci)
0224         return -ENOMEM;
0225 
0226     scmi_chip_info.info = ptr_scmi_ci;
0227     chip_info = &scmi_chip_info;
0228 
0229     for (type = 0; type < hwmon_max; type++) {
0230         if (!nr_count[type])
0231             continue;
0232 
0233         scmi_hwmon_add_chan_info(scmi_hwmon_chan, dev, nr_count[type],
0234                      type, hwmon_attributes[type]);
0235         *ptr_scmi_ci++ = scmi_hwmon_chan++;
0236 
0237         scmi_sensors->info[type] =
0238             devm_kcalloc(dev, nr_count[type],
0239                      sizeof(*scmi_sensors->info), GFP_KERNEL);
0240         if (!scmi_sensors->info[type])
0241             return -ENOMEM;
0242     }
0243 
0244     for (i = nr_sensors - 1; i >= 0 ; i--) {
0245         sensor = sensor_ops->info_get(ph, i);
0246         if (!sensor)
0247             continue;
0248 
0249         switch (sensor->type) {
0250         case TEMPERATURE_C:
0251         case VOLTAGE:
0252         case CURRENT:
0253         case POWER:
0254         case ENERGY:
0255             type = scmi_types[sensor->type];
0256             idx = --nr_count[type];
0257             *(scmi_sensors->info[type] + idx) = sensor;
0258             break;
0259         }
0260     }
0261 
0262     hwdev = devm_hwmon_device_register_with_info(dev, "scmi_sensors",
0263                              scmi_sensors, chip_info,
0264                              NULL);
0265 
0266     return PTR_ERR_OR_ZERO(hwdev);
0267 }
0268 
0269 static const struct scmi_device_id scmi_id_table[] = {
0270     { SCMI_PROTOCOL_SENSOR, "hwmon" },
0271     { },
0272 };
0273 MODULE_DEVICE_TABLE(scmi, scmi_id_table);
0274 
0275 static struct scmi_driver scmi_hwmon_drv = {
0276     .name       = "scmi-hwmon",
0277     .probe      = scmi_hwmon_probe,
0278     .id_table   = scmi_id_table,
0279 };
0280 module_scmi_driver(scmi_hwmon_drv);
0281 
0282 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
0283 MODULE_DESCRIPTION("ARM SCMI HWMON interface driver");
0284 MODULE_LICENSE("GPL v2");