^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * System Control and Management Interface(SCMI) based hwmon sensor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2018-2020 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Sudeep Holla <sudeep.holla@arm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/scmi_protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static const struct scmi_sensor_proto_ops *sensor_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct scmi_sensors {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) const struct scmi_protocol_handle *ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) const struct scmi_sensor_info **info[hwmon_max];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static inline u64 __pow10(u8 x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u64 r = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) while (x--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) r *= 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int scmi_hwmon_scale(const struct scmi_sensor_info *sensor, u64 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int scale = sensor->scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u64 f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) switch (sensor->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) case TEMPERATURE_C:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) case VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) case CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) scale += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) case POWER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) case ENERGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) scale += 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (scale == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (abs(scale) > 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) f = __pow10(abs(scale));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (scale > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *value *= f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *value = div64_u64(*value, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static int scmi_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u32 attr, int channel, long *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u64 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) const struct scmi_sensor_info *sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct scmi_sensors *scmi_sensors = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) sensor = *(scmi_sensors->info[type] + channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ret = sensor_ops->reading_get(scmi_sensors->ph, sensor->id, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ret = scmi_hwmon_scale(sensor, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *val = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) scmi_hwmon_read_string(struct device *dev, enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) u32 attr, int channel, const char **str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) const struct scmi_sensor_info *sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct scmi_sensors *scmi_sensors = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) sensor = *(scmi_sensors->info[type] + channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *str = sensor->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static umode_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) scmi_hwmon_is_visible(const void *drvdata, enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) u32 attr, int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) const struct scmi_sensor_info *sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) const struct scmi_sensors *scmi_sensors = drvdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) sensor = *(scmi_sensors->info[type] + channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0444;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static const struct hwmon_ops scmi_hwmon_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) .is_visible = scmi_hwmon_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .read = scmi_hwmon_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .read_string = scmi_hwmon_read_string,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static struct hwmon_chip_info scmi_chip_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .ops = &scmi_hwmon_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .info = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int scmi_hwmon_add_chan_info(struct hwmon_channel_info *scmi_hwmon_chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct device *dev, int num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) enum hwmon_sensor_types type, u32 config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) u32 *cfg = devm_kcalloc(dev, num + 1, sizeof(*cfg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!cfg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) scmi_hwmon_chan->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) scmi_hwmon_chan->config = cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) for (i = 0; i < num; i++, cfg++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) *cfg = config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static enum hwmon_sensor_types scmi_types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) [TEMPERATURE_C] = hwmon_temp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) [VOLTAGE] = hwmon_in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) [CURRENT] = hwmon_curr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) [POWER] = hwmon_power,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) [ENERGY] = hwmon_energy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static u32 hwmon_attributes[hwmon_max] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) [hwmon_chip] = HWMON_C_REGISTER_TZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) [hwmon_temp] = HWMON_T_INPUT | HWMON_T_LABEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) [hwmon_in] = HWMON_I_INPUT | HWMON_I_LABEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) [hwmon_curr] = HWMON_C_INPUT | HWMON_C_LABEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) [hwmon_power] = HWMON_P_INPUT | HWMON_P_LABEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) [hwmon_energy] = HWMON_E_INPUT | HWMON_E_LABEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int scmi_hwmon_probe(struct scmi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int i, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) u16 nr_sensors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) enum hwmon_sensor_types type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct scmi_sensors *scmi_sensors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) const struct scmi_sensor_info *sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int nr_count[hwmon_max] = {0}, nr_types = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) const struct hwmon_chip_info *chip_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct device *hwdev, *dev = &sdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct hwmon_channel_info *scmi_hwmon_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) const struct hwmon_channel_info **ptr_scmi_ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) const struct scmi_handle *handle = sdev->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct scmi_protocol_handle *ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (!handle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) sensor_ops = handle->devm_get_protocol(sdev, SCMI_PROTOCOL_SENSOR, &ph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (IS_ERR(sensor_ops))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return PTR_ERR(sensor_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) nr_sensors = sensor_ops->count_get(ph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!nr_sensors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) scmi_sensors = devm_kzalloc(dev, sizeof(*scmi_sensors), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!scmi_sensors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) scmi_sensors->ph = ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) for (i = 0; i < nr_sensors; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) sensor = sensor_ops->info_get(ph, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) switch (sensor->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) case TEMPERATURE_C:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) case VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) case CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) case POWER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) case ENERGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) type = scmi_types[sensor->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (!nr_count[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) nr_count[type]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (nr_count[hwmon_temp]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) nr_count[hwmon_chip]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) nr_types++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) scmi_hwmon_chan = devm_kcalloc(dev, nr_types, sizeof(*scmi_hwmon_chan),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (!scmi_hwmon_chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ptr_scmi_ci = devm_kcalloc(dev, nr_types + 1, sizeof(*ptr_scmi_ci),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!ptr_scmi_ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) scmi_chip_info.info = ptr_scmi_ci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) chip_info = &scmi_chip_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) for (type = 0; type < hwmon_max; type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (!nr_count[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) scmi_hwmon_add_chan_info(scmi_hwmon_chan, dev, nr_count[type],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) type, hwmon_attributes[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) *ptr_scmi_ci++ = scmi_hwmon_chan++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) scmi_sensors->info[type] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) devm_kcalloc(dev, nr_count[type],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) sizeof(*scmi_sensors->info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!scmi_sensors->info[type])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) for (i = nr_sensors - 1; i >= 0 ; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) sensor = sensor_ops->info_get(ph, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) switch (sensor->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) case TEMPERATURE_C:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) case VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) case CURRENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) case POWER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) case ENERGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) type = scmi_types[sensor->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) idx = --nr_count[type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) *(scmi_sensors->info[type] + idx) = sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) hwdev = devm_hwmon_device_register_with_info(dev, "scmi_sensors",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) scmi_sensors, chip_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return PTR_ERR_OR_ZERO(hwdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static const struct scmi_device_id scmi_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) { SCMI_PROTOCOL_SENSOR, "hwmon" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) MODULE_DEVICE_TABLE(scmi, scmi_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static struct scmi_driver scmi_hwmon_drv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) .name = "scmi-hwmon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) .probe = scmi_hwmon_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) .id_table = scmi_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) module_scmi_driver(scmi_hwmon_drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) MODULE_DESCRIPTION("ARM SCMI HWMON interface driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) MODULE_LICENSE("GPL v2");