Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * System Control and Power Interface(SCPI) based hwmon sensor driver
0004  *
0005  * Copyright (C) 2015 ARM Ltd.
0006  * Punit Agrawal <punit.agrawal@arm.com>
0007  */
0008 
0009 #include <linux/hwmon.h>
0010 #include <linux/module.h>
0011 #include <linux/of_device.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/scpi_protocol.h>
0014 #include <linux/slab.h>
0015 #include <linux/sysfs.h>
0016 #include <linux/thermal.h>
0017 
0018 struct sensor_data {
0019     unsigned int scale;
0020     struct scpi_sensor_info info;
0021     struct device_attribute dev_attr_input;
0022     struct device_attribute dev_attr_label;
0023     char input[20];
0024     char label[20];
0025 };
0026 
0027 struct scpi_thermal_zone {
0028     int sensor_id;
0029     struct scpi_sensors *scpi_sensors;
0030 };
0031 
0032 struct scpi_sensors {
0033     struct scpi_ops *scpi_ops;
0034     struct sensor_data *data;
0035     struct list_head thermal_zones;
0036     struct attribute **attrs;
0037     struct attribute_group group;
0038     const struct attribute_group *groups[2];
0039 };
0040 
0041 static const u32 gxbb_scpi_scale[] = {
0042     [TEMPERATURE]   = 1,        /* (celsius)        */
0043     [VOLTAGE]   = 1000,     /* (millivolts)     */
0044     [CURRENT]   = 1000,     /* (milliamperes)   */
0045     [POWER]     = 1000000,  /* (microwatts)     */
0046     [ENERGY]    = 1000000,  /* (microjoules)    */
0047 };
0048 
0049 static const u32 scpi_scale[] = {
0050     [TEMPERATURE]   = 1000,     /* (millicelsius)   */
0051     [VOLTAGE]   = 1000,     /* (millivolts)     */
0052     [CURRENT]   = 1000,     /* (milliamperes)   */
0053     [POWER]     = 1000000,  /* (microwatts)     */
0054     [ENERGY]    = 1000000,  /* (microjoules)    */
0055 };
0056 
0057 static void scpi_scale_reading(u64 *value, struct sensor_data *sensor)
0058 {
0059     if (scpi_scale[sensor->info.class] != sensor->scale) {
0060         *value *= scpi_scale[sensor->info.class];
0061         do_div(*value, sensor->scale);
0062     }
0063 }
0064 
0065 static int scpi_read_temp(void *dev, int *temp)
0066 {
0067     struct scpi_thermal_zone *zone = dev;
0068     struct scpi_sensors *scpi_sensors = zone->scpi_sensors;
0069     struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
0070     struct sensor_data *sensor = &scpi_sensors->data[zone->sensor_id];
0071     u64 value;
0072     int ret;
0073 
0074     ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
0075     if (ret)
0076         return ret;
0077 
0078     scpi_scale_reading(&value, sensor);
0079 
0080     *temp = value;
0081     return 0;
0082 }
0083 
0084 /* hwmon callback functions */
0085 static ssize_t
0086 scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf)
0087 {
0088     struct scpi_sensors *scpi_sensors = dev_get_drvdata(dev);
0089     struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops;
0090     struct sensor_data *sensor;
0091     u64 value;
0092     int ret;
0093 
0094     sensor = container_of(attr, struct sensor_data, dev_attr_input);
0095 
0096     ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value);
0097     if (ret)
0098         return ret;
0099 
0100     scpi_scale_reading(&value, sensor);
0101 
0102     /*
0103      * Temperature sensor values are treated as signed values based on
0104      * observation even though that is not explicitly specified, and
0105      * because an unsigned u64 temperature does not really make practical
0106      * sense especially when the temperature is below zero degrees Celsius.
0107      */
0108     if (sensor->info.class == TEMPERATURE)
0109         return sprintf(buf, "%lld\n", (s64)value);
0110 
0111     return sprintf(buf, "%llu\n", value);
0112 }
0113 
0114 static ssize_t
0115 scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf)
0116 {
0117     struct sensor_data *sensor;
0118 
0119     sensor = container_of(attr, struct sensor_data, dev_attr_label);
0120 
0121     return sprintf(buf, "%s\n", sensor->info.name);
0122 }
0123 
0124 static const struct thermal_zone_of_device_ops scpi_sensor_ops = {
0125     .get_temp = scpi_read_temp,
0126 };
0127 
0128 static const struct of_device_id scpi_of_match[] = {
0129     {.compatible = "arm,scpi-sensors", .data = &scpi_scale},
0130     {.compatible = "amlogic,meson-gxbb-scpi-sensors", .data = &gxbb_scpi_scale},
0131     {},
0132 };
0133 MODULE_DEVICE_TABLE(of, scpi_of_match);
0134 
0135 static int scpi_hwmon_probe(struct platform_device *pdev)
0136 {
0137     u16 nr_sensors, i;
0138     const u32 *scale;
0139     int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0;
0140     int num_energy = 0;
0141     struct scpi_ops *scpi_ops;
0142     struct device *hwdev, *dev = &pdev->dev;
0143     struct scpi_sensors *scpi_sensors;
0144     int idx, ret;
0145 
0146     scpi_ops = get_scpi_ops();
0147     if (!scpi_ops)
0148         return -EPROBE_DEFER;
0149 
0150     ret = scpi_ops->sensor_get_capability(&nr_sensors);
0151     if (ret)
0152         return ret;
0153 
0154     if (!nr_sensors)
0155         return -ENODEV;
0156 
0157     scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL);
0158     if (!scpi_sensors)
0159         return -ENOMEM;
0160 
0161     scpi_sensors->data = devm_kcalloc(dev, nr_sensors,
0162                    sizeof(*scpi_sensors->data), GFP_KERNEL);
0163     if (!scpi_sensors->data)
0164         return -ENOMEM;
0165 
0166     scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1,
0167                    sizeof(*scpi_sensors->attrs), GFP_KERNEL);
0168     if (!scpi_sensors->attrs)
0169         return -ENOMEM;
0170 
0171     scpi_sensors->scpi_ops = scpi_ops;
0172 
0173     scale = of_device_get_match_data(&pdev->dev);
0174     if (!scale) {
0175         dev_err(&pdev->dev, "Unable to initialize scpi-hwmon data\n");
0176         return -ENODEV;
0177     }
0178 
0179     for (i = 0, idx = 0; i < nr_sensors; i++) {
0180         struct sensor_data *sensor = &scpi_sensors->data[idx];
0181 
0182         ret = scpi_ops->sensor_get_info(i, &sensor->info);
0183         if (ret)
0184             return ret;
0185 
0186         switch (sensor->info.class) {
0187         case TEMPERATURE:
0188             snprintf(sensor->input, sizeof(sensor->input),
0189                  "temp%d_input", num_temp + 1);
0190             snprintf(sensor->label, sizeof(sensor->input),
0191                  "temp%d_label", num_temp + 1);
0192             num_temp++;
0193             break;
0194         case VOLTAGE:
0195             snprintf(sensor->input, sizeof(sensor->input),
0196                  "in%d_input", num_volt);
0197             snprintf(sensor->label, sizeof(sensor->input),
0198                  "in%d_label", num_volt);
0199             num_volt++;
0200             break;
0201         case CURRENT:
0202             snprintf(sensor->input, sizeof(sensor->input),
0203                  "curr%d_input", num_current + 1);
0204             snprintf(sensor->label, sizeof(sensor->input),
0205                  "curr%d_label", num_current + 1);
0206             num_current++;
0207             break;
0208         case POWER:
0209             snprintf(sensor->input, sizeof(sensor->input),
0210                  "power%d_input", num_power + 1);
0211             snprintf(sensor->label, sizeof(sensor->input),
0212                  "power%d_label", num_power + 1);
0213             num_power++;
0214             break;
0215         case ENERGY:
0216             snprintf(sensor->input, sizeof(sensor->input),
0217                  "energy%d_input", num_energy + 1);
0218             snprintf(sensor->label, sizeof(sensor->input),
0219                  "energy%d_label", num_energy + 1);
0220             num_energy++;
0221             break;
0222         default:
0223             continue;
0224         }
0225 
0226         sensor->scale = scale[sensor->info.class];
0227 
0228         sensor->dev_attr_input.attr.mode = 0444;
0229         sensor->dev_attr_input.show = scpi_show_sensor;
0230         sensor->dev_attr_input.attr.name = sensor->input;
0231 
0232         sensor->dev_attr_label.attr.mode = 0444;
0233         sensor->dev_attr_label.show = scpi_show_label;
0234         sensor->dev_attr_label.attr.name = sensor->label;
0235 
0236         scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr;
0237         scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr;
0238 
0239         sysfs_attr_init(scpi_sensors->attrs[idx << 1]);
0240         sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]);
0241         idx++;
0242     }
0243 
0244     scpi_sensors->group.attrs = scpi_sensors->attrs;
0245     scpi_sensors->groups[0] = &scpi_sensors->group;
0246 
0247     platform_set_drvdata(pdev, scpi_sensors);
0248 
0249     hwdev = devm_hwmon_device_register_with_groups(dev,
0250             "scpi_sensors", scpi_sensors, scpi_sensors->groups);
0251 
0252     if (IS_ERR(hwdev))
0253         return PTR_ERR(hwdev);
0254 
0255     /*
0256      * Register the temperature sensors with the thermal framework
0257      * to allow their usage in setting up the thermal zones from
0258      * device tree.
0259      *
0260      * NOTE: Not all temperature sensors maybe used for thermal
0261      * control
0262      */
0263     INIT_LIST_HEAD(&scpi_sensors->thermal_zones);
0264     for (i = 0; i < nr_sensors; i++) {
0265         struct sensor_data *sensor = &scpi_sensors->data[i];
0266         struct thermal_zone_device *z;
0267         struct scpi_thermal_zone *zone;
0268 
0269         if (sensor->info.class != TEMPERATURE)
0270             continue;
0271 
0272         zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL);
0273         if (!zone)
0274             return -ENOMEM;
0275 
0276         zone->sensor_id = i;
0277         zone->scpi_sensors = scpi_sensors;
0278         z = devm_thermal_zone_of_sensor_register(dev,
0279                              sensor->info.sensor_id,
0280                              zone,
0281                              &scpi_sensor_ops);
0282         /*
0283          * The call to thermal_zone_of_sensor_register returns
0284          * an error for sensors that are not associated with
0285          * any thermal zones or if the thermal subsystem is
0286          * not configured.
0287          */
0288         if (IS_ERR(z))
0289             devm_kfree(dev, zone);
0290     }
0291 
0292     return 0;
0293 }
0294 
0295 static struct platform_driver scpi_hwmon_platdrv = {
0296     .driver = {
0297         .name   = "scpi-hwmon",
0298         .of_match_table = scpi_of_match,
0299     },
0300     .probe      = scpi_hwmon_probe,
0301 };
0302 module_platform_driver(scpi_hwmon_platdrv);
0303 
0304 MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>");
0305 MODULE_DESCRIPTION("ARM SCPI HWMON interface driver");
0306 MODULE_LICENSE("GPL v2");