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

hwmon: (htu21) 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
fa0efc40 7b3dadc4

+22 -47
+22 -47
drivers/hwmon/htu21.c
··· 31 31 #define HTU21_RH_MEASUREMENT_HM 0xE5 32 32 33 33 struct htu21 { 34 - struct device *hwmon_dev; 34 + struct i2c_client *client; 35 35 struct mutex lock; 36 36 bool valid; 37 37 unsigned long last_update; ··· 59 59 return ((15625 * ticks) >> 13) - 6000; 60 60 } 61 61 62 - static int htu21_update_measurements(struct i2c_client *client) 62 + static int htu21_update_measurements(struct device *dev) 63 63 { 64 + struct htu21 *htu21 = dev_get_drvdata(dev); 65 + struct i2c_client *client = htu21->client; 64 66 int ret = 0; 65 - struct htu21 *htu21 = i2c_get_clientdata(client); 66 67 67 68 mutex_lock(&htu21->lock); 68 69 ··· 91 90 static ssize_t htu21_show_temperature(struct device *dev, 92 91 struct device_attribute *attr, char *buf) 93 92 { 94 - struct i2c_client *client = to_i2c_client(dev); 95 - struct htu21 *htu21 = i2c_get_clientdata(client); 96 - int ret = htu21_update_measurements(client); 93 + struct htu21 *htu21 = dev_get_drvdata(dev); 94 + int ret; 95 + 96 + ret = htu21_update_measurements(dev); 97 97 if (ret < 0) 98 98 return ret; 99 99 return sprintf(buf, "%d\n", htu21->temperature); ··· 103 101 static ssize_t htu21_show_humidity(struct device *dev, 104 102 struct device_attribute *attr, char *buf) 105 103 { 106 - struct i2c_client *client = to_i2c_client(dev); 107 - struct htu21 *htu21 = i2c_get_clientdata(client); 108 - int ret = htu21_update_measurements(client); 104 + struct htu21 *htu21 = dev_get_drvdata(dev); 105 + int ret; 106 + 107 + ret = htu21_update_measurements(dev); 109 108 if (ret < 0) 110 109 return ret; 111 110 return sprintf(buf, "%d\n", htu21->humidity); ··· 117 114 static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, 118 115 htu21_show_humidity, NULL, 0); 119 116 120 - static struct attribute *htu21_attributes[] = { 117 + static struct attribute *htu21_attrs[] = { 121 118 &sensor_dev_attr_temp1_input.dev_attr.attr, 122 119 &sensor_dev_attr_humidity1_input.dev_attr.attr, 123 120 NULL 124 121 }; 125 122 126 - static const struct attribute_group htu21_group = { 127 - .attrs = htu21_attributes, 128 - }; 123 + ATTRIBUTE_GROUPS(htu21); 129 124 130 125 static int htu21_probe(struct i2c_client *client, 131 126 const struct i2c_device_id *id) 132 127 { 128 + struct device *dev = &client->dev; 133 129 struct htu21 *htu21; 134 - int err; 130 + struct device *hwmon_dev; 135 131 136 132 if (!i2c_check_functionality(client->adapter, 137 133 I2C_FUNC_SMBUS_READ_WORD_DATA)) { ··· 139 137 return -ENODEV; 140 138 } 141 139 142 - htu21 = devm_kzalloc(&client->dev, sizeof(*htu21), GFP_KERNEL); 140 + htu21 = devm_kzalloc(dev, sizeof(*htu21), GFP_KERNEL); 143 141 if (!htu21) 144 142 return -ENOMEM; 145 143 146 - i2c_set_clientdata(client, htu21); 147 - 144 + htu21->client = client; 148 145 mutex_init(&htu21->lock); 149 146 150 - err = sysfs_create_group(&client->dev.kobj, &htu21_group); 151 - if (err) { 152 - dev_dbg(&client->dev, "could not create sysfs files\n"); 153 - return err; 154 - } 155 - htu21->hwmon_dev = hwmon_device_register(&client->dev); 156 - if (IS_ERR(htu21->hwmon_dev)) { 157 - dev_dbg(&client->dev, "unable to register hwmon device\n"); 158 - err = PTR_ERR(htu21->hwmon_dev); 159 - goto error; 160 - } 161 - 162 - dev_info(&client->dev, "initialized\n"); 163 - 164 - return 0; 165 - 166 - error: 167 - sysfs_remove_group(&client->dev.kobj, &htu21_group); 168 - return err; 169 - } 170 - 171 - static int htu21_remove(struct i2c_client *client) 172 - { 173 - struct htu21 *htu21 = i2c_get_clientdata(client); 174 - 175 - hwmon_device_unregister(htu21->hwmon_dev); 176 - sysfs_remove_group(&client->dev.kobj, &htu21_group); 177 - 178 - return 0; 147 + hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 148 + htu21, 149 + htu21_groups); 150 + return PTR_ERR_OR_ZERO(hwmon_dev); 179 151 } 180 152 181 153 static const struct i2c_device_id htu21_id[] = { ··· 164 188 .name = "htu21", 165 189 }, 166 190 .probe = htu21_probe, 167 - .remove = htu21_remove, 168 191 .id_table = htu21_id, 169 192 }; 170 193