Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (ad7418) Convert to devm_hwmon_device_register_with_groups

Use ATTRIBUTE_GROUPS macro and devm_hwmon_device_register_with_groups() to
simplify the code a bit.

Signed-off-by: Axel Lin <axel.lin@ingics.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Axel Lin and committed by
Guenter Roeck
337076f9 991763a9

+24 -41
+24 -41
drivers/hwmon/ad7418.c
··· 44 44 AD7418_REG_TEMP_OS }; 45 45 46 46 struct ad7418_data { 47 - struct device *hwmon_dev; 48 - struct attribute_group attrs; 47 + struct i2c_client *client; 49 48 enum chips type; 50 49 struct mutex lock; 51 50 int adc_max; /* number of ADC channels */ ··· 56 57 57 58 static struct ad7418_data *ad7418_update_device(struct device *dev) 58 59 { 59 - struct i2c_client *client = to_i2c_client(dev); 60 - struct ad7418_data *data = i2c_get_clientdata(client); 60 + struct ad7418_data *data = dev_get_drvdata(dev); 61 + struct i2c_client *client = data->client; 61 62 62 63 mutex_lock(&data->lock); 63 64 ··· 126 127 const char *buf, size_t count) 127 128 { 128 129 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 129 - struct i2c_client *client = to_i2c_client(dev); 130 - struct ad7418_data *data = i2c_get_clientdata(client); 130 + struct ad7418_data *data = dev_get_drvdata(dev); 131 + struct i2c_client *client = data->client; 131 132 long temp; 132 133 int ret = kstrtol(buf, 10, &temp); 133 134 ··· 154 155 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2); 155 156 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3); 156 157 157 - static struct attribute *ad7416_attributes[] = { 158 + static struct attribute *ad7416_attrs[] = { 158 159 &sensor_dev_attr_temp1_max.dev_attr.attr, 159 160 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 160 161 &sensor_dev_attr_temp1_input.dev_attr.attr, 161 162 NULL 162 163 }; 164 + ATTRIBUTE_GROUPS(ad7416); 163 165 164 - static struct attribute *ad7417_attributes[] = { 166 + static struct attribute *ad7417_attrs[] = { 165 167 &sensor_dev_attr_temp1_max.dev_attr.attr, 166 168 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 167 169 &sensor_dev_attr_temp1_input.dev_attr.attr, ··· 172 172 &sensor_dev_attr_in4_input.dev_attr.attr, 173 173 NULL 174 174 }; 175 + ATTRIBUTE_GROUPS(ad7417); 175 176 176 - static struct attribute *ad7418_attributes[] = { 177 + static struct attribute *ad7418_attrs[] = { 177 178 &sensor_dev_attr_temp1_max.dev_attr.attr, 178 179 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 179 180 &sensor_dev_attr_temp1_input.dev_attr.attr, 180 181 &sensor_dev_attr_in1_input.dev_attr.attr, 181 182 NULL 182 183 }; 184 + ATTRIBUTE_GROUPS(ad7418); 183 185 184 186 static void ad7418_init_client(struct i2c_client *client) 185 187 { ··· 203 201 static int ad7418_probe(struct i2c_client *client, 204 202 const struct i2c_device_id *id) 205 203 { 204 + struct device *dev = &client->dev; 206 205 struct i2c_adapter *adapter = client->adapter; 207 206 struct ad7418_data *data; 208 - int err; 207 + struct device *hwmon_dev; 208 + const struct attribute_group **attr_groups = NULL; 209 209 210 210 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 211 211 I2C_FUNC_SMBUS_WORD_DATA)) 212 212 return -EOPNOTSUPP; 213 213 214 - data = devm_kzalloc(&client->dev, sizeof(struct ad7418_data), 215 - GFP_KERNEL); 214 + data = devm_kzalloc(dev, sizeof(struct ad7418_data), GFP_KERNEL); 216 215 if (!data) 217 216 return -ENOMEM; 218 217 219 218 i2c_set_clientdata(client, data); 220 219 221 220 mutex_init(&data->lock); 221 + data->client = client; 222 222 data->type = id->driver_data; 223 223 224 224 switch (data->type) { 225 225 case ad7416: 226 226 data->adc_max = 0; 227 - data->attrs.attrs = ad7416_attributes; 227 + attr_groups = ad7416_groups; 228 228 break; 229 229 230 230 case ad7417: 231 231 data->adc_max = 4; 232 - data->attrs.attrs = ad7417_attributes; 232 + attr_groups = ad7417_groups; 233 233 break; 234 234 235 235 case ad7418: 236 236 data->adc_max = 1; 237 - data->attrs.attrs = ad7418_attributes; 237 + attr_groups = ad7418_groups; 238 238 break; 239 239 } 240 240 241 - dev_info(&client->dev, "%s chip found\n", client->name); 241 + dev_info(dev, "%s chip found\n", client->name); 242 242 243 243 /* Initialize the AD7418 chip */ 244 244 ad7418_init_client(client); 245 245 246 - /* Register sysfs hooks */ 247 - err = sysfs_create_group(&client->dev.kobj, &data->attrs); 248 - if (err) 249 - return err; 250 - 251 - data->hwmon_dev = hwmon_device_register(&client->dev); 252 - if (IS_ERR(data->hwmon_dev)) { 253 - err = PTR_ERR(data->hwmon_dev); 254 - goto exit_remove; 255 - } 256 - 257 - return 0; 258 - 259 - exit_remove: 260 - sysfs_remove_group(&client->dev.kobj, &data->attrs); 261 - return err; 262 - } 263 - 264 - static int ad7418_remove(struct i2c_client *client) 265 - { 266 - struct ad7418_data *data = i2c_get_clientdata(client); 267 - hwmon_device_unregister(data->hwmon_dev); 268 - sysfs_remove_group(&client->dev.kobj, &data->attrs); 269 - return 0; 246 + hwmon_dev = devm_hwmon_device_register_with_groups(dev, 247 + client->name, 248 + data, attr_groups); 249 + return PTR_ERR_OR_ZERO(hwmon_dev); 270 250 } 271 251 272 252 static const struct i2c_device_id ad7418_id[] = { ··· 264 280 .name = "ad7418", 265 281 }, 266 282 .probe = ad7418_probe, 267 - .remove = ad7418_remove, 268 283 .id_table = ad7418_id, 269 284 }; 270 285