diff options
author | Axel Lin <axel.lin@ingics.com> | 2014-07-10 22:44:48 -0400 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2014-08-04 10:01:38 -0400 |
commit | b8d56898307f561d7e100d33c244001a34ee1262 (patch) | |
tree | 91d649eb137469e245a05fa2e8cf7234b2a4b158 /drivers/hwmon/sht21.c | |
parent | 4395d78056a8b5716f7e1858feb721ccf91fb104 (diff) |
hwmon: (sht21) 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>
Diffstat (limited to 'drivers/hwmon/sht21.c')
-rw-r--r-- | drivers/hwmon/sht21.c | 82 |
1 files changed, 22 insertions, 60 deletions
diff --git a/drivers/hwmon/sht21.c b/drivers/hwmon/sht21.c index 2e9f9570b6f8..84cdb1cf0fb4 100644 --- a/drivers/hwmon/sht21.c +++ b/drivers/hwmon/sht21.c | |||
@@ -45,7 +45,7 @@ | |||
45 | * @humidity: cached humidity measurement value | 45 | * @humidity: cached humidity measurement value |
46 | */ | 46 | */ |
47 | struct sht21 { | 47 | struct sht21 { |
48 | struct device *hwmon_dev; | 48 | struct i2c_client *client; |
49 | struct mutex lock; | 49 | struct mutex lock; |
50 | char valid; | 50 | char valid; |
51 | unsigned long last_update; | 51 | unsigned long last_update; |
@@ -85,14 +85,15 @@ static inline int sht21_rh_ticks_to_per_cent_mille(int ticks) | |||
85 | 85 | ||
86 | /** | 86 | /** |
87 | * sht21_update_measurements() - get updated measurements from device | 87 | * sht21_update_measurements() - get updated measurements from device |
88 | * @client: I2C client device | 88 | * @dev: device |
89 | * | 89 | * |
90 | * Returns 0 on success, else negative errno. | 90 | * Returns 0 on success, else negative errno. |
91 | */ | 91 | */ |
92 | static int sht21_update_measurements(struct i2c_client *client) | 92 | static int sht21_update_measurements(struct device *dev) |
93 | { | 93 | { |
94 | int ret = 0; | 94 | int ret = 0; |
95 | struct sht21 *sht21 = i2c_get_clientdata(client); | 95 | struct sht21 *sht21 = dev_get_drvdata(dev); |
96 | struct i2c_client *client = sht21->client; | ||
96 | 97 | ||
97 | mutex_lock(&sht21->lock); | 98 | mutex_lock(&sht21->lock); |
98 | /* | 99 | /* |
@@ -133,9 +134,10 @@ static ssize_t sht21_show_temperature(struct device *dev, | |||
133 | struct device_attribute *attr, | 134 | struct device_attribute *attr, |
134 | char *buf) | 135 | char *buf) |
135 | { | 136 | { |
136 | struct i2c_client *client = to_i2c_client(dev); | 137 | struct sht21 *sht21 = dev_get_drvdata(dev); |
137 | struct sht21 *sht21 = i2c_get_clientdata(client); | 138 | int ret; |
138 | int ret = sht21_update_measurements(client); | 139 | |
140 | ret = sht21_update_measurements(dev); | ||
139 | if (ret < 0) | 141 | if (ret < 0) |
140 | return ret; | 142 | return ret; |
141 | return sprintf(buf, "%d\n", sht21->temperature); | 143 | return sprintf(buf, "%d\n", sht21->temperature); |
@@ -154,9 +156,10 @@ static ssize_t sht21_show_humidity(struct device *dev, | |||
154 | struct device_attribute *attr, | 156 | struct device_attribute *attr, |
155 | char *buf) | 157 | char *buf) |
156 | { | 158 | { |
157 | struct i2c_client *client = to_i2c_client(dev); | 159 | struct sht21 *sht21 = dev_get_drvdata(dev); |
158 | struct sht21 *sht21 = i2c_get_clientdata(client); | 160 | int ret; |
159 | int ret = sht21_update_measurements(client); | 161 | |
162 | ret = sht21_update_measurements(dev); | ||
160 | if (ret < 0) | 163 | if (ret < 0) |
161 | return ret; | 164 | return ret; |
162 | return sprintf(buf, "%d\n", sht21->humidity); | 165 | return sprintf(buf, "%d\n", sht21->humidity); |
@@ -168,30 +171,20 @@ static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sht21_show_temperature, | |||
168 | static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity, | 171 | static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO, sht21_show_humidity, |
169 | NULL, 0); | 172 | NULL, 0); |
170 | 173 | ||
171 | static struct attribute *sht21_attributes[] = { | 174 | static struct attribute *sht21_attrs[] = { |
172 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 175 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
173 | &sensor_dev_attr_humidity1_input.dev_attr.attr, | 176 | &sensor_dev_attr_humidity1_input.dev_attr.attr, |
174 | NULL | 177 | NULL |
175 | }; | 178 | }; |
176 | 179 | ||
177 | static const struct attribute_group sht21_attr_group = { | 180 | ATTRIBUTE_GROUPS(sht21); |
178 | .attrs = sht21_attributes, | ||
179 | }; | ||
180 | 181 | ||
181 | /** | ||
182 | * sht21_probe() - probe device | ||
183 | * @client: I2C client device | ||
184 | * @id: device ID | ||
185 | * | ||
186 | * Called by the I2C core when an entry in the ID table matches a | ||
187 | * device's name. | ||
188 | * Returns 0 on success. | ||
189 | */ | ||
190 | static int sht21_probe(struct i2c_client *client, | 182 | static int sht21_probe(struct i2c_client *client, |
191 | const struct i2c_device_id *id) | 183 | const struct i2c_device_id *id) |
192 | { | 184 | { |
185 | struct device *dev = &client->dev; | ||
186 | struct device *hwmon_dev; | ||
193 | struct sht21 *sht21; | 187 | struct sht21 *sht21; |
194 | int err; | ||
195 | 188 | ||
196 | if (!i2c_check_functionality(client->adapter, | 189 | if (!i2c_check_functionality(client->adapter, |
197 | I2C_FUNC_SMBUS_WORD_DATA)) { | 190 | I2C_FUNC_SMBUS_WORD_DATA)) { |
@@ -200,47 +193,17 @@ static int sht21_probe(struct i2c_client *client, | |||
200 | return -ENODEV; | 193 | return -ENODEV; |
201 | } | 194 | } |
202 | 195 | ||
203 | sht21 = devm_kzalloc(&client->dev, sizeof(*sht21), GFP_KERNEL); | 196 | sht21 = devm_kzalloc(dev, sizeof(*sht21), GFP_KERNEL); |
204 | if (!sht21) | 197 | if (!sht21) |
205 | return -ENOMEM; | 198 | return -ENOMEM; |
206 | 199 | ||
207 | i2c_set_clientdata(client, sht21); | 200 | sht21->client = client; |
208 | 201 | ||
209 | mutex_init(&sht21->lock); | 202 | mutex_init(&sht21->lock); |
210 | 203 | ||
211 | err = sysfs_create_group(&client->dev.kobj, &sht21_attr_group); | 204 | hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, |
212 | if (err) { | 205 | sht21, sht21_groups); |
213 | dev_dbg(&client->dev, "could not create sysfs files\n"); | 206 | return PTR_ERR_OR_ZERO(hwmon_dev); |
214 | return err; | ||
215 | } | ||
216 | sht21->hwmon_dev = hwmon_device_register(&client->dev); | ||
217 | if (IS_ERR(sht21->hwmon_dev)) { | ||
218 | dev_dbg(&client->dev, "unable to register hwmon device\n"); | ||
219 | err = PTR_ERR(sht21->hwmon_dev); | ||
220 | goto fail_remove_sysfs; | ||
221 | } | ||
222 | |||
223 | dev_info(&client->dev, "initialized\n"); | ||
224 | |||
225 | return 0; | ||
226 | |||
227 | fail_remove_sysfs: | ||
228 | sysfs_remove_group(&client->dev.kobj, &sht21_attr_group); | ||
229 | return err; | ||
230 | } | ||
231 | |||
232 | /** | ||
233 | * sht21_remove() - remove device | ||
234 | * @client: I2C client device | ||
235 | */ | ||
236 | static int sht21_remove(struct i2c_client *client) | ||
237 | { | ||
238 | struct sht21 *sht21 = i2c_get_clientdata(client); | ||
239 | |||
240 | hwmon_device_unregister(sht21->hwmon_dev); | ||
241 | sysfs_remove_group(&client->dev.kobj, &sht21_attr_group); | ||
242 | |||
243 | return 0; | ||
244 | } | 207 | } |
245 | 208 | ||
246 | /* Device ID table */ | 209 | /* Device ID table */ |
@@ -253,7 +216,6 @@ MODULE_DEVICE_TABLE(i2c, sht21_id); | |||
253 | static struct i2c_driver sht21_driver = { | 216 | static struct i2c_driver sht21_driver = { |
254 | .driver.name = "sht21", | 217 | .driver.name = "sht21", |
255 | .probe = sht21_probe, | 218 | .probe = sht21_probe, |
256 | .remove = sht21_remove, | ||
257 | .id_table = sht21_id, | 219 | .id_table = sht21_id, |
258 | }; | 220 | }; |
259 | 221 | ||