|
| 1 | +From 915d4664b7158d8d0f44da810186742c69300f02 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Michael Shych < [email protected]> |
| 3 | +Date: Tue, 18 Jan 2022 09:56:10 +0200 |
| 4 | +Subject: [PATCH] hwmon: (powr1220) Upgrade driver to support hwmon info |
| 5 | + infrastructure |
| 6 | + |
| 7 | +Reduce code by using devm_hwmon_device_register_with_groups() API by |
| 8 | +devm_hwmon_device_register_with_info() API. |
| 9 | +The motivation is to reduce code and to allow easy support for similar |
| 10 | +devices by the same driver. |
| 11 | + |
| 12 | +Signed-off-by: Michael Shych < [email protected]> |
| 13 | +Reviewed-by: Vadim Pasternak < [email protected]> |
| 14 | +Link: https://lore.kernel.org/r/ [email protected] |
| 15 | +Signed-off-by: Guenter Roeck < [email protected]> |
| 16 | +--- |
| 17 | + drivers/hwmon/powr1220.c | 207 +++++++++++++++++++++-------------------------- |
| 18 | + 1 file changed, 92 insertions(+), 115 deletions(-) |
| 19 | + |
| 20 | +diff --git a/drivers/hwmon/powr1220.c b/drivers/hwmon/powr1220.c |
| 21 | +index 542e4a7b0234..0fa1a136eec8 100644 |
| 22 | +--- a/drivers/hwmon/powr1220.c |
| 23 | ++++ b/drivers/hwmon/powr1220.c |
| 24 | +@@ -111,7 +111,7 @@ |
| 25 | + mutex_lock(&data->update_lock); |
| 26 | + |
| 27 | + if (time_after(jiffies, data->adc_last_updated[ch_num] + HZ) || |
| 28 | +- !data->adc_valid[ch_num]) { |
| 29 | ++ !data->adc_valid[ch_num]) { |
| 30 | + /* |
| 31 | + * figure out if we need to use the attenuator for |
| 32 | + * high inputs or inputs that we don't yet have a measurement |
| 33 | +@@ -119,12 +119,12 @@ |
| 34 | + * max reading. |
| 35 | + */ |
| 36 | + if (data->adc_maxes[ch_num] > ADC_MAX_LOW_MEASUREMENT_MV || |
| 37 | +- data->adc_maxes[ch_num] == 0) |
| 38 | ++ data->adc_maxes[ch_num] == 0) |
| 39 | + adc_range = 1 << 4; |
| 40 | + |
| 41 | + /* set the attenuator and mux */ |
| 42 | + result = i2c_smbus_write_byte_data(data->client, ADC_MUX, |
| 43 | +- adc_range | ch_num); |
| 44 | ++ adc_range | ch_num); |
| 45 | + if (result) |
| 46 | + goto exit; |
| 47 | + |
| 48 | +@@ -167,135 +167,109 @@ |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | +-/* Shows the voltage associated with the specified ADC channel */ |
| 53 | +-static ssize_t powr1220_voltage_show(struct device *dev, |
| 54 | +- struct device_attribute *dev_attr, |
| 55 | +- char *buf) |
| 56 | ++static umode_t |
| 57 | ++powr1220_is_visible(const void *data, enum hwmon_sensor_types type, u32 |
| 58 | ++ attr, int channel) |
| 59 | + { |
| 60 | +- struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
| 61 | +- int adc_val = powr1220_read_adc(dev, attr->index); |
| 62 | +- |
| 63 | +- if (adc_val < 0) |
| 64 | +- return adc_val; |
| 65 | ++ switch (type) { |
| 66 | ++ case hwmon_in: |
| 67 | ++ switch (attr) { |
| 68 | ++ case hwmon_in_input: |
| 69 | ++ case hwmon_in_highest: |
| 70 | ++ case hwmon_in_label: |
| 71 | ++ return 0444; |
| 72 | ++ default: |
| 73 | ++ break; |
| 74 | ++ } |
| 75 | ++ break; |
| 76 | ++ default: |
| 77 | ++ break; |
| 78 | ++ } |
| 79 | + |
| 80 | +- return sprintf(buf, "%d\n", adc_val); |
| 81 | ++ return 0; |
| 82 | + } |
| 83 | + |
| 84 | +-/* Shows the maximum setting associated with the specified ADC channel */ |
| 85 | +-static ssize_t powr1220_max_show(struct device *dev, |
| 86 | +- struct device_attribute *dev_attr, char *buf) |
| 87 | ++static int |
| 88 | ++powr1220_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr, |
| 89 | ++ int channel, const char **str) |
| 90 | + { |
| 91 | +- struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
| 92 | +- struct powr1220_data *data = dev_get_drvdata(dev); |
| 93 | ++ switch (type) { |
| 94 | ++ case hwmon_in: |
| 95 | ++ switch (attr) { |
| 96 | ++ case hwmon_in_label: |
| 97 | ++ *str = input_names[channel]; |
| 98 | ++ return 0; |
| 99 | ++ default: |
| 100 | ++ return -EOPNOTSUPP; |
| 101 | ++ } |
| 102 | ++ break; |
| 103 | ++ default: |
| 104 | ++ return -EOPNOTSUPP; |
| 105 | ++ } |
| 106 | + |
| 107 | +- return sprintf(buf, "%d\n", data->adc_maxes[attr->index]); |
| 108 | ++ return -EOPNOTSUPP; |
| 109 | + } |
| 110 | + |
| 111 | +-/* Shows the label associated with the specified ADC channel */ |
| 112 | +-static ssize_t powr1220_label_show(struct device *dev, |
| 113 | +- struct device_attribute *dev_attr, |
| 114 | +- char *buf) |
| 115 | ++static int |
| 116 | ++powr1220_read(struct device *dev, enum hwmon_sensor_types type, u32 |
| 117 | ++ attr, int channel, long *val) |
| 118 | + { |
| 119 | +- struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
| 120 | ++ struct powr1220_data *data = dev_get_drvdata(dev); |
| 121 | ++ int ret; |
| 122 | + |
| 123 | +- return sprintf(buf, "%s\n", input_names[attr->index]); |
| 124 | ++ switch (type) { |
| 125 | ++ case hwmon_in: |
| 126 | ++ switch (attr) { |
| 127 | ++ case hwmon_in_input: |
| 128 | ++ ret = powr1220_read_adc(dev, channel); |
| 129 | ++ if (ret < 0) |
| 130 | ++ return ret; |
| 131 | ++ *val = ret; |
| 132 | ++ break; |
| 133 | ++ case hwmon_in_highest: |
| 134 | ++ *val = data->adc_maxes[channel]; |
| 135 | ++ break; |
| 136 | ++ default: |
| 137 | ++ return -EOPNOTSUPP; |
| 138 | ++ } |
| 139 | ++ break; |
| 140 | ++ default: |
| 141 | ++ return -EOPNOTSUPP; |
| 142 | + } |
| 143 | + |
| 144 | +-static SENSOR_DEVICE_ATTR_RO(in0_input, powr1220_voltage, VMON1); |
| 145 | +-static SENSOR_DEVICE_ATTR_RO(in1_input, powr1220_voltage, VMON2); |
| 146 | +-static SENSOR_DEVICE_ATTR_RO(in2_input, powr1220_voltage, VMON3); |
| 147 | +-static SENSOR_DEVICE_ATTR_RO(in3_input, powr1220_voltage, VMON4); |
| 148 | +-static SENSOR_DEVICE_ATTR_RO(in4_input, powr1220_voltage, VMON5); |
| 149 | +-static SENSOR_DEVICE_ATTR_RO(in5_input, powr1220_voltage, VMON6); |
| 150 | +-static SENSOR_DEVICE_ATTR_RO(in6_input, powr1220_voltage, VMON7); |
| 151 | +-static SENSOR_DEVICE_ATTR_RO(in7_input, powr1220_voltage, VMON8); |
| 152 | +-static SENSOR_DEVICE_ATTR_RO(in8_input, powr1220_voltage, VMON9); |
| 153 | +-static SENSOR_DEVICE_ATTR_RO(in9_input, powr1220_voltage, VMON10); |
| 154 | +-static SENSOR_DEVICE_ATTR_RO(in10_input, powr1220_voltage, VMON11); |
| 155 | +-static SENSOR_DEVICE_ATTR_RO(in11_input, powr1220_voltage, VMON12); |
| 156 | +-static SENSOR_DEVICE_ATTR_RO(in12_input, powr1220_voltage, VCCA); |
| 157 | +-static SENSOR_DEVICE_ATTR_RO(in13_input, powr1220_voltage, VCCINP); |
| 158 | +- |
| 159 | +-static SENSOR_DEVICE_ATTR_RO(in0_highest, powr1220_max, VMON1); |
| 160 | +-static SENSOR_DEVICE_ATTR_RO(in1_highest, powr1220_max, VMON2); |
| 161 | +-static SENSOR_DEVICE_ATTR_RO(in2_highest, powr1220_max, VMON3); |
| 162 | +-static SENSOR_DEVICE_ATTR_RO(in3_highest, powr1220_max, VMON4); |
| 163 | +-static SENSOR_DEVICE_ATTR_RO(in4_highest, powr1220_max, VMON5); |
| 164 | +-static SENSOR_DEVICE_ATTR_RO(in5_highest, powr1220_max, VMON6); |
| 165 | +-static SENSOR_DEVICE_ATTR_RO(in6_highest, powr1220_max, VMON7); |
| 166 | +-static SENSOR_DEVICE_ATTR_RO(in7_highest, powr1220_max, VMON8); |
| 167 | +-static SENSOR_DEVICE_ATTR_RO(in8_highest, powr1220_max, VMON9); |
| 168 | +-static SENSOR_DEVICE_ATTR_RO(in9_highest, powr1220_max, VMON10); |
| 169 | +-static SENSOR_DEVICE_ATTR_RO(in10_highest, powr1220_max, VMON11); |
| 170 | +-static SENSOR_DEVICE_ATTR_RO(in11_highest, powr1220_max, VMON12); |
| 171 | +-static SENSOR_DEVICE_ATTR_RO(in12_highest, powr1220_max, VCCA); |
| 172 | +-static SENSOR_DEVICE_ATTR_RO(in13_highest, powr1220_max, VCCINP); |
| 173 | +- |
| 174 | +-static SENSOR_DEVICE_ATTR_RO(in0_label, powr1220_label, VMON1); |
| 175 | +-static SENSOR_DEVICE_ATTR_RO(in1_label, powr1220_label, VMON2); |
| 176 | +-static SENSOR_DEVICE_ATTR_RO(in2_label, powr1220_label, VMON3); |
| 177 | +-static SENSOR_DEVICE_ATTR_RO(in3_label, powr1220_label, VMON4); |
| 178 | +-static SENSOR_DEVICE_ATTR_RO(in4_label, powr1220_label, VMON5); |
| 179 | +-static SENSOR_DEVICE_ATTR_RO(in5_label, powr1220_label, VMON6); |
| 180 | +-static SENSOR_DEVICE_ATTR_RO(in6_label, powr1220_label, VMON7); |
| 181 | +-static SENSOR_DEVICE_ATTR_RO(in7_label, powr1220_label, VMON8); |
| 182 | +-static SENSOR_DEVICE_ATTR_RO(in8_label, powr1220_label, VMON9); |
| 183 | +-static SENSOR_DEVICE_ATTR_RO(in9_label, powr1220_label, VMON10); |
| 184 | +-static SENSOR_DEVICE_ATTR_RO(in10_label, powr1220_label, VMON11); |
| 185 | +-static SENSOR_DEVICE_ATTR_RO(in11_label, powr1220_label, VMON12); |
| 186 | +-static SENSOR_DEVICE_ATTR_RO(in12_label, powr1220_label, VCCA); |
| 187 | +-static SENSOR_DEVICE_ATTR_RO(in13_label, powr1220_label, VCCINP); |
| 188 | +- |
| 189 | +-static struct attribute *powr1220_attrs[] = { |
| 190 | +- &sensor_dev_attr_in0_input.dev_attr.attr, |
| 191 | +- &sensor_dev_attr_in1_input.dev_attr.attr, |
| 192 | +- &sensor_dev_attr_in2_input.dev_attr.attr, |
| 193 | +- &sensor_dev_attr_in3_input.dev_attr.attr, |
| 194 | +- &sensor_dev_attr_in4_input.dev_attr.attr, |
| 195 | +- &sensor_dev_attr_in5_input.dev_attr.attr, |
| 196 | +- &sensor_dev_attr_in6_input.dev_attr.attr, |
| 197 | +- &sensor_dev_attr_in7_input.dev_attr.attr, |
| 198 | +- &sensor_dev_attr_in8_input.dev_attr.attr, |
| 199 | +- &sensor_dev_attr_in9_input.dev_attr.attr, |
| 200 | +- &sensor_dev_attr_in10_input.dev_attr.attr, |
| 201 | +- &sensor_dev_attr_in11_input.dev_attr.attr, |
| 202 | +- &sensor_dev_attr_in12_input.dev_attr.attr, |
| 203 | +- &sensor_dev_attr_in13_input.dev_attr.attr, |
| 204 | +- |
| 205 | +- &sensor_dev_attr_in0_highest.dev_attr.attr, |
| 206 | +- &sensor_dev_attr_in1_highest.dev_attr.attr, |
| 207 | +- &sensor_dev_attr_in2_highest.dev_attr.attr, |
| 208 | +- &sensor_dev_attr_in3_highest.dev_attr.attr, |
| 209 | +- &sensor_dev_attr_in4_highest.dev_attr.attr, |
| 210 | +- &sensor_dev_attr_in5_highest.dev_attr.attr, |
| 211 | +- &sensor_dev_attr_in6_highest.dev_attr.attr, |
| 212 | +- &sensor_dev_attr_in7_highest.dev_attr.attr, |
| 213 | +- &sensor_dev_attr_in8_highest.dev_attr.attr, |
| 214 | +- &sensor_dev_attr_in9_highest.dev_attr.attr, |
| 215 | +- &sensor_dev_attr_in10_highest.dev_attr.attr, |
| 216 | +- &sensor_dev_attr_in11_highest.dev_attr.attr, |
| 217 | +- &sensor_dev_attr_in12_highest.dev_attr.attr, |
| 218 | +- &sensor_dev_attr_in13_highest.dev_attr.attr, |
| 219 | +- |
| 220 | +- &sensor_dev_attr_in0_label.dev_attr.attr, |
| 221 | +- &sensor_dev_attr_in1_label.dev_attr.attr, |
| 222 | +- &sensor_dev_attr_in2_label.dev_attr.attr, |
| 223 | +- &sensor_dev_attr_in3_label.dev_attr.attr, |
| 224 | +- &sensor_dev_attr_in4_label.dev_attr.attr, |
| 225 | +- &sensor_dev_attr_in5_label.dev_attr.attr, |
| 226 | +- &sensor_dev_attr_in6_label.dev_attr.attr, |
| 227 | +- &sensor_dev_attr_in7_label.dev_attr.attr, |
| 228 | +- &sensor_dev_attr_in8_label.dev_attr.attr, |
| 229 | +- &sensor_dev_attr_in9_label.dev_attr.attr, |
| 230 | +- &sensor_dev_attr_in10_label.dev_attr.attr, |
| 231 | +- &sensor_dev_attr_in11_label.dev_attr.attr, |
| 232 | +- &sensor_dev_attr_in12_label.dev_attr.attr, |
| 233 | +- &sensor_dev_attr_in13_label.dev_attr.attr, |
| 234 | ++ return 0; |
| 235 | ++} |
| 236 | ++ |
| 237 | ++static const struct hwmon_channel_info *powr1220_info[] = { |
| 238 | ++ HWMON_CHANNEL_INFO(in, |
| 239 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 240 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 241 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 242 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 243 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 244 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 245 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 246 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 247 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 248 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 249 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 250 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 251 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL, |
| 252 | ++ HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL), |
| 253 | + |
| 254 | + NULL |
| 255 | + }; |
| 256 | + |
| 257 | +-ATTRIBUTE_GROUPS(powr1220); |
| 258 | ++static const struct hwmon_ops powr1220_hwmon_ops = { |
| 259 | ++ .read = powr1220_read, |
| 260 | ++ .read_string = powr1220_read_string, |
| 261 | ++ .is_visible = powr1220_is_visible, |
| 262 | ++}; |
| 263 | ++ |
| 264 | ++static const struct hwmon_chip_info powr1220_chip_info = { |
| 265 | ++ .ops = &powr1220_hwmon_ops, |
| 266 | ++ .info = powr1220_info, |
| 267 | ++}; |
| 268 | + |
| 269 | + static int powr1220_probe(struct i2c_client *client) |
| 270 | + { |
| 271 | +@@ -312,8 +286,11 @@ |
| 272 | + mutex_init(&data->update_lock); |
| 273 | + data->client = client; |
| 274 | + |
| 275 | +- hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
| 276 | +- client->name, data, powr1220_groups); |
| 277 | ++ hwmon_dev = devm_hwmon_device_register_with_info(&client->dev, |
| 278 | ++ client->name, |
| 279 | ++ data, |
| 280 | ++ &powr1220_chip_info, |
| 281 | ++ NULL); |
| 282 | + |
| 283 | + return PTR_ERR_OR_ZERO(hwmon_dev); |
| 284 | + } |
0 commit comments