Guenter Roeck | 23297ed | 2019-06-20 06:14:42 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 2 | /* |
| 3 | * System Control and Power Interface(SCPI) based hwmon sensor driver |
| 4 | * |
| 5 | * Copyright (C) 2015 ARM Ltd. |
| 6 | * Punit Agrawal <punit.agrawal@arm.com> |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/hwmon.h> |
| 10 | #include <linux/module.h> |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 11 | #include <linux/of_device.h> |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/scpi_protocol.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/sysfs.h> |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 16 | #include <linux/thermal.h> |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 17 | |
| 18 | struct sensor_data { |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 19 | unsigned int scale; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 20 | struct scpi_sensor_info info; |
| 21 | struct device_attribute dev_attr_input; |
| 22 | struct device_attribute dev_attr_label; |
| 23 | char input[20]; |
| 24 | char label[20]; |
| 25 | }; |
| 26 | |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 27 | struct scpi_thermal_zone { |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 28 | int sensor_id; |
| 29 | struct scpi_sensors *scpi_sensors; |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 30 | }; |
| 31 | |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 32 | struct scpi_sensors { |
| 33 | struct scpi_ops *scpi_ops; |
| 34 | struct sensor_data *data; |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 35 | struct list_head thermal_zones; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 36 | struct attribute **attrs; |
| 37 | struct attribute_group group; |
| 38 | const struct attribute_group *groups[2]; |
| 39 | }; |
| 40 | |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 41 | static const u32 gxbb_scpi_scale[] = { |
| 42 | [TEMPERATURE] = 1, /* (celsius) */ |
| 43 | [VOLTAGE] = 1000, /* (millivolts) */ |
| 44 | [CURRENT] = 1000, /* (milliamperes) */ |
| 45 | [POWER] = 1000000, /* (microwatts) */ |
| 46 | [ENERGY] = 1000000, /* (microjoules) */ |
| 47 | }; |
| 48 | |
| 49 | static const u32 scpi_scale[] = { |
| 50 | [TEMPERATURE] = 1000, /* (millicelsius) */ |
| 51 | [VOLTAGE] = 1000, /* (millivolts) */ |
| 52 | [CURRENT] = 1000, /* (milliamperes) */ |
| 53 | [POWER] = 1000000, /* (microwatts) */ |
| 54 | [ENERGY] = 1000000, /* (microjoules) */ |
| 55 | }; |
| 56 | |
| 57 | static void scpi_scale_reading(u64 *value, struct sensor_data *sensor) |
| 58 | { |
| 59 | if (scpi_scale[sensor->info.class] != sensor->scale) { |
| 60 | *value *= scpi_scale[sensor->info.class]; |
| 61 | do_div(*value, sensor->scale); |
| 62 | } |
| 63 | } |
| 64 | |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 65 | static int scpi_read_temp(void *dev, int *temp) |
| 66 | { |
| 67 | struct scpi_thermal_zone *zone = dev; |
| 68 | struct scpi_sensors *scpi_sensors = zone->scpi_sensors; |
| 69 | struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops; |
| 70 | struct sensor_data *sensor = &scpi_sensors->data[zone->sensor_id]; |
Sudeep Holla | 2e87415 | 2016-01-14 17:58:02 +0000 | [diff] [blame] | 71 | u64 value; |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 72 | int ret; |
| 73 | |
| 74 | ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value); |
| 75 | if (ret) |
| 76 | return ret; |
| 77 | |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 78 | scpi_scale_reading(&value, sensor); |
| 79 | |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 80 | *temp = value; |
| 81 | return 0; |
| 82 | } |
| 83 | |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 84 | /* hwmon callback functions */ |
| 85 | static ssize_t |
| 86 | scpi_show_sensor(struct device *dev, struct device_attribute *attr, char *buf) |
| 87 | { |
| 88 | struct scpi_sensors *scpi_sensors = dev_get_drvdata(dev); |
| 89 | struct scpi_ops *scpi_ops = scpi_sensors->scpi_ops; |
| 90 | struct sensor_data *sensor; |
Sudeep Holla | 2e87415 | 2016-01-14 17:58:02 +0000 | [diff] [blame] | 91 | u64 value; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 92 | int ret; |
| 93 | |
| 94 | sensor = container_of(attr, struct sensor_data, dev_attr_input); |
| 95 | |
| 96 | ret = scpi_ops->sensor_get_value(sensor->info.sensor_id, &value); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 100 | scpi_scale_reading(&value, sensor); |
| 101 | |
Riwen Lu | 78d1355 | 2021-06-04 11:09:59 +0800 | [diff] [blame] | 102 | /* |
| 103 | * Temperature sensor values are treated as signed values based on |
| 104 | * observation even though that is not explicitly specified, and |
| 105 | * because an unsigned u64 temperature does not really make practical |
| 106 | * sense especially when the temperature is below zero degrees Celsius. |
| 107 | */ |
| 108 | if (sensor->info.class == TEMPERATURE) |
| 109 | return sprintf(buf, "%lld\n", (s64)value); |
| 110 | |
Sudeep Holla | 2e87415 | 2016-01-14 17:58:02 +0000 | [diff] [blame] | 111 | return sprintf(buf, "%llu\n", value); |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static ssize_t |
| 115 | scpi_show_label(struct device *dev, struct device_attribute *attr, char *buf) |
| 116 | { |
| 117 | struct sensor_data *sensor; |
| 118 | |
| 119 | sensor = container_of(attr, struct sensor_data, dev_attr_label); |
| 120 | |
| 121 | return sprintf(buf, "%s\n", sensor->info.name); |
| 122 | } |
| 123 | |
Julia Lawall | 778fb96 | 2017-08-08 17:09:03 +0200 | [diff] [blame] | 124 | static const struct thermal_zone_of_device_ops scpi_sensor_ops = { |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 125 | .get_temp = scpi_read_temp, |
| 126 | }; |
| 127 | |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 128 | static const struct of_device_id scpi_of_match[] = { |
| 129 | {.compatible = "arm,scpi-sensors", .data = &scpi_scale}, |
| 130 | {.compatible = "amlogic,meson-gxbb-scpi-sensors", .data = &gxbb_scpi_scale}, |
| 131 | {}, |
| 132 | }; |
| 133 | MODULE_DEVICE_TABLE(of, scpi_of_match); |
| 134 | |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 135 | static int scpi_hwmon_probe(struct platform_device *pdev) |
| 136 | { |
| 137 | u16 nr_sensors, i; |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 138 | const u32 *scale; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 139 | int num_temp = 0, num_volt = 0, num_current = 0, num_power = 0; |
Sudeep Holla | fb3b07e | 2016-01-25 10:53:38 +0000 | [diff] [blame] | 140 | int num_energy = 0; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 141 | struct scpi_ops *scpi_ops; |
| 142 | struct device *hwdev, *dev = &pdev->dev; |
| 143 | struct scpi_sensors *scpi_sensors; |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 144 | const struct of_device_id *of_id; |
Eduardo Valentin | d7817ff | 2016-03-09 13:03:17 -0800 | [diff] [blame] | 145 | int idx, ret; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 146 | |
| 147 | scpi_ops = get_scpi_ops(); |
| 148 | if (!scpi_ops) |
| 149 | return -EPROBE_DEFER; |
| 150 | |
| 151 | ret = scpi_ops->sensor_get_capability(&nr_sensors); |
| 152 | if (ret) |
| 153 | return ret; |
| 154 | |
| 155 | if (!nr_sensors) |
| 156 | return -ENODEV; |
| 157 | |
| 158 | scpi_sensors = devm_kzalloc(dev, sizeof(*scpi_sensors), GFP_KERNEL); |
| 159 | if (!scpi_sensors) |
| 160 | return -ENOMEM; |
| 161 | |
| 162 | scpi_sensors->data = devm_kcalloc(dev, nr_sensors, |
| 163 | sizeof(*scpi_sensors->data), GFP_KERNEL); |
| 164 | if (!scpi_sensors->data) |
| 165 | return -ENOMEM; |
| 166 | |
| 167 | scpi_sensors->attrs = devm_kcalloc(dev, (nr_sensors * 2) + 1, |
| 168 | sizeof(*scpi_sensors->attrs), GFP_KERNEL); |
| 169 | if (!scpi_sensors->attrs) |
| 170 | return -ENOMEM; |
| 171 | |
| 172 | scpi_sensors->scpi_ops = scpi_ops; |
| 173 | |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 174 | of_id = of_match_device(scpi_of_match, &pdev->dev); |
| 175 | if (!of_id) { |
| 176 | dev_err(&pdev->dev, "Unable to initialize scpi-hwmon data\n"); |
| 177 | return -ENODEV; |
| 178 | } |
| 179 | scale = of_id->data; |
| 180 | |
Sudeep Holla | cef03d7 | 2015-10-28 17:17:31 +0000 | [diff] [blame] | 181 | for (i = 0, idx = 0; i < nr_sensors; i++) { |
| 182 | struct sensor_data *sensor = &scpi_sensors->data[idx]; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 183 | |
| 184 | ret = scpi_ops->sensor_get_info(i, &sensor->info); |
| 185 | if (ret) |
| 186 | return ret; |
| 187 | |
| 188 | switch (sensor->info.class) { |
| 189 | case TEMPERATURE: |
| 190 | snprintf(sensor->input, sizeof(sensor->input), |
| 191 | "temp%d_input", num_temp + 1); |
| 192 | snprintf(sensor->label, sizeof(sensor->input), |
| 193 | "temp%d_label", num_temp + 1); |
| 194 | num_temp++; |
| 195 | break; |
| 196 | case VOLTAGE: |
| 197 | snprintf(sensor->input, sizeof(sensor->input), |
| 198 | "in%d_input", num_volt); |
| 199 | snprintf(sensor->label, sizeof(sensor->input), |
| 200 | "in%d_label", num_volt); |
| 201 | num_volt++; |
| 202 | break; |
| 203 | case CURRENT: |
| 204 | snprintf(sensor->input, sizeof(sensor->input), |
| 205 | "curr%d_input", num_current + 1); |
| 206 | snprintf(sensor->label, sizeof(sensor->input), |
| 207 | "curr%d_label", num_current + 1); |
| 208 | num_current++; |
| 209 | break; |
| 210 | case POWER: |
| 211 | snprintf(sensor->input, sizeof(sensor->input), |
| 212 | "power%d_input", num_power + 1); |
| 213 | snprintf(sensor->label, sizeof(sensor->input), |
| 214 | "power%d_label", num_power + 1); |
| 215 | num_power++; |
| 216 | break; |
Sudeep Holla | fb3b07e | 2016-01-25 10:53:38 +0000 | [diff] [blame] | 217 | case ENERGY: |
| 218 | snprintf(sensor->input, sizeof(sensor->input), |
| 219 | "energy%d_input", num_energy + 1); |
| 220 | snprintf(sensor->label, sizeof(sensor->input), |
| 221 | "energy%d_label", num_energy + 1); |
| 222 | num_energy++; |
| 223 | break; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 224 | default: |
Sudeep Holla | cef03d7 | 2015-10-28 17:17:31 +0000 | [diff] [blame] | 225 | continue; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Carlo Caione | ccc9b82 | 2017-05-30 11:05:07 +0200 | [diff] [blame] | 228 | sensor->scale = scale[sensor->info.class]; |
| 229 | |
Guenter Roeck | 6a0785a | 2018-12-10 14:02:20 -0800 | [diff] [blame] | 230 | sensor->dev_attr_input.attr.mode = 0444; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 231 | sensor->dev_attr_input.show = scpi_show_sensor; |
| 232 | sensor->dev_attr_input.attr.name = sensor->input; |
| 233 | |
Guenter Roeck | 6a0785a | 2018-12-10 14:02:20 -0800 | [diff] [blame] | 234 | sensor->dev_attr_label.attr.mode = 0444; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 235 | sensor->dev_attr_label.show = scpi_show_label; |
| 236 | sensor->dev_attr_label.attr.name = sensor->label; |
| 237 | |
Sudeep Holla | cef03d7 | 2015-10-28 17:17:31 +0000 | [diff] [blame] | 238 | scpi_sensors->attrs[idx << 1] = &sensor->dev_attr_input.attr; |
| 239 | scpi_sensors->attrs[(idx << 1) + 1] = &sensor->dev_attr_label.attr; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 240 | |
Sudeep Holla | cef03d7 | 2015-10-28 17:17:31 +0000 | [diff] [blame] | 241 | sysfs_attr_init(scpi_sensors->attrs[idx << 1]); |
| 242 | sysfs_attr_init(scpi_sensors->attrs[(idx << 1) + 1]); |
| 243 | idx++; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | scpi_sensors->group.attrs = scpi_sensors->attrs; |
| 247 | scpi_sensors->groups[0] = &scpi_sensors->group; |
| 248 | |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 249 | platform_set_drvdata(pdev, scpi_sensors); |
| 250 | |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 251 | hwdev = devm_hwmon_device_register_with_groups(dev, |
| 252 | "scpi_sensors", scpi_sensors, scpi_sensors->groups); |
| 253 | |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 254 | if (IS_ERR(hwdev)) |
| 255 | return PTR_ERR(hwdev); |
| 256 | |
| 257 | /* |
| 258 | * Register the temperature sensors with the thermal framework |
| 259 | * to allow their usage in setting up the thermal zones from |
| 260 | * device tree. |
| 261 | * |
| 262 | * NOTE: Not all temperature sensors maybe used for thermal |
| 263 | * control |
| 264 | */ |
| 265 | INIT_LIST_HEAD(&scpi_sensors->thermal_zones); |
| 266 | for (i = 0; i < nr_sensors; i++) { |
| 267 | struct sensor_data *sensor = &scpi_sensors->data[i]; |
Eduardo Valentin | d7817ff | 2016-03-09 13:03:17 -0800 | [diff] [blame] | 268 | struct thermal_zone_device *z; |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 269 | struct scpi_thermal_zone *zone; |
| 270 | |
| 271 | if (sensor->info.class != TEMPERATURE) |
| 272 | continue; |
| 273 | |
| 274 | zone = devm_kzalloc(dev, sizeof(*zone), GFP_KERNEL); |
Eduardo Valentin | d7817ff | 2016-03-09 13:03:17 -0800 | [diff] [blame] | 275 | if (!zone) |
| 276 | return -ENOMEM; |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 277 | |
| 278 | zone->sensor_id = i; |
| 279 | zone->scpi_sensors = scpi_sensors; |
Eduardo Valentin | d7817ff | 2016-03-09 13:03:17 -0800 | [diff] [blame] | 280 | z = devm_thermal_zone_of_sensor_register(dev, |
| 281 | sensor->info.sensor_id, |
| 282 | zone, |
| 283 | &scpi_sensor_ops); |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 284 | /* |
| 285 | * The call to thermal_zone_of_sensor_register returns |
| 286 | * an error for sensors that are not associated with |
| 287 | * any thermal zones or if the thermal subsystem is |
| 288 | * not configured. |
| 289 | */ |
zhong jiang | 3045b5d | 2018-09-21 09:30:03 +0800 | [diff] [blame] | 290 | if (IS_ERR(z)) |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 291 | devm_kfree(dev, zone); |
Punit Agrawal | 68acc77 | 2015-06-24 19:03:16 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | return 0; |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 297 | static struct platform_driver scpi_hwmon_platdrv = { |
| 298 | .driver = { |
| 299 | .name = "scpi-hwmon", |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 300 | .of_match_table = scpi_of_match, |
| 301 | }, |
| 302 | .probe = scpi_hwmon_probe, |
Punit Agrawal | ea98b29 | 2015-05-21 15:08:45 +0100 | [diff] [blame] | 303 | }; |
| 304 | module_platform_driver(scpi_hwmon_platdrv); |
| 305 | |
| 306 | MODULE_AUTHOR("Punit Agrawal <punit.agrawal@arm.com>"); |
| 307 | MODULE_DESCRIPTION("ARM SCPI HWMON interface driver"); |
| 308 | MODULE_LICENSE("GPL v2"); |