diff options
author | Guenter Roeck <linux@roeck-us.net> | 2013-09-01 19:48:48 -0400 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2013-10-13 19:16:28 -0400 |
commit | 9bd024e9fd2bb82fdeb2a2bc8a4b687548f1467e (patch) | |
tree | 055045e03facf60b9cc7e83518fab98e84c9f929 /drivers/hwmon/max6642.c | |
parent | 0d36dce0f1d15a9ca55f893b3aa5ce7c297e11c8 (diff) |
hwmon: (max6642) Convert to use devm_hwmon_device_register_with_groups
Also rename new_client variable to client and introduce
new variable dev pointing to client->dev in the probe function,
and use new macro ATTRIBUTE_GROUPS to declare attribute groups.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon/max6642.c')
-rw-r--r-- | drivers/hwmon/max6642.c | 72 |
1 files changed, 24 insertions, 48 deletions
diff --git a/drivers/hwmon/max6642.c b/drivers/hwmon/max6642.c index 57d58cd32206..3d61f8d719d5 100644 --- a/drivers/hwmon/max6642.c +++ b/drivers/hwmon/max6642.c | |||
@@ -87,7 +87,7 @@ static int temp_to_reg(int val) | |||
87 | */ | 87 | */ |
88 | 88 | ||
89 | struct max6642_data { | 89 | struct max6642_data { |
90 | struct device *hwmon_dev; | 90 | struct i2c_client *client; |
91 | struct mutex update_lock; | 91 | struct mutex update_lock; |
92 | bool valid; /* zero until following fields are valid */ | 92 | bool valid; /* zero until following fields are valid */ |
93 | unsigned long last_updated; /* in jiffies */ | 93 | unsigned long last_updated; /* in jiffies */ |
@@ -102,10 +102,10 @@ struct max6642_data { | |||
102 | * Real code | 102 | * Real code |
103 | */ | 103 | */ |
104 | 104 | ||
105 | static void max6642_init_client(struct i2c_client *client) | 105 | static void max6642_init_client(struct max6642_data *data, |
106 | struct i2c_client *client) | ||
106 | { | 107 | { |
107 | u8 config; | 108 | u8 config; |
108 | struct max6642_data *data = i2c_get_clientdata(client); | ||
109 | 109 | ||
110 | /* | 110 | /* |
111 | * Start the conversions. | 111 | * Start the conversions. |
@@ -168,14 +168,14 @@ static int max6642_detect(struct i2c_client *client, | |||
168 | 168 | ||
169 | static struct max6642_data *max6642_update_device(struct device *dev) | 169 | static struct max6642_data *max6642_update_device(struct device *dev) |
170 | { | 170 | { |
171 | struct i2c_client *client = to_i2c_client(dev); | 171 | struct max6642_data *data = dev_get_drvdata(dev); |
172 | struct max6642_data *data = i2c_get_clientdata(client); | 172 | struct i2c_client *client = data->client; |
173 | u16 val, tmp; | 173 | u16 val, tmp; |
174 | 174 | ||
175 | mutex_lock(&data->update_lock); | 175 | mutex_lock(&data->update_lock); |
176 | 176 | ||
177 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { | 177 | if (time_after(jiffies, data->last_updated + HZ) || !data->valid) { |
178 | dev_dbg(&client->dev, "Updating max6642 data.\n"); | 178 | dev_dbg(dev, "Updating max6642 data.\n"); |
179 | val = i2c_smbus_read_byte_data(client, | 179 | val = i2c_smbus_read_byte_data(client, |
180 | MAX6642_REG_R_LOCAL_TEMPL); | 180 | MAX6642_REG_R_LOCAL_TEMPL); |
181 | tmp = (val >> 6) & 3; | 181 | tmp = (val >> 6) & 3; |
@@ -209,8 +209,8 @@ static struct max6642_data *max6642_update_device(struct device *dev) | |||
209 | static ssize_t show_temp_max10(struct device *dev, | 209 | static ssize_t show_temp_max10(struct device *dev, |
210 | struct device_attribute *dev_attr, char *buf) | 210 | struct device_attribute *dev_attr, char *buf) |
211 | { | 211 | { |
212 | struct max6642_data *data = max6642_update_device(dev); | ||
213 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); | 212 | struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr); |
213 | struct max6642_data *data = max6642_update_device(dev); | ||
214 | 214 | ||
215 | return sprintf(buf, "%d\n", | 215 | return sprintf(buf, "%d\n", |
216 | temp_from_reg10(data->temp_input[attr->index])); | 216 | temp_from_reg10(data->temp_input[attr->index])); |
@@ -219,8 +219,8 @@ static ssize_t show_temp_max10(struct device *dev, | |||
219 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, | 219 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
220 | char *buf) | 220 | char *buf) |
221 | { | 221 | { |
222 | struct max6642_data *data = max6642_update_device(dev); | ||
223 | struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr); | 222 | struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr); |
223 | struct max6642_data *data = max6642_update_device(dev); | ||
224 | 224 | ||
225 | return sprintf(buf, "%d\n", temp_from_reg(data->temp_high[attr2->nr])); | 225 | return sprintf(buf, "%d\n", temp_from_reg(data->temp_high[attr2->nr])); |
226 | } | 226 | } |
@@ -228,11 +228,10 @@ static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, | |||
228 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, | 228 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
229 | const char *buf, size_t count) | 229 | const char *buf, size_t count) |
230 | { | 230 | { |
231 | struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr); | ||
232 | struct max6642_data *data = dev_get_drvdata(dev); | ||
231 | unsigned long val; | 233 | unsigned long val; |
232 | int err; | 234 | int err; |
233 | struct i2c_client *client = to_i2c_client(dev); | ||
234 | struct max6642_data *data = i2c_get_clientdata(client); | ||
235 | struct sensor_device_attribute_2 *attr2 = to_sensor_dev_attr_2(attr); | ||
236 | 235 | ||
237 | err = kstrtoul(buf, 10, &val); | 236 | err = kstrtoul(buf, 10, &val); |
238 | if (err < 0) | 237 | if (err < 0) |
@@ -240,7 +239,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, | |||
240 | 239 | ||
241 | mutex_lock(&data->update_lock); | 240 | mutex_lock(&data->update_lock); |
242 | data->temp_high[attr2->nr] = clamp_val(temp_to_reg(val), 0, 255); | 241 | data->temp_high[attr2->nr] = clamp_val(temp_to_reg(val), 0, 255); |
243 | i2c_smbus_write_byte_data(client, attr2->index, | 242 | i2c_smbus_write_byte_data(data->client, attr2->index, |
244 | data->temp_high[attr2->nr]); | 243 | data->temp_high[attr2->nr]); |
245 | mutex_unlock(&data->update_lock); | 244 | mutex_unlock(&data->update_lock); |
246 | return count; | 245 | return count; |
@@ -264,7 +263,7 @@ static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); | |||
264 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | 263 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
265 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | 264 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
266 | 265 | ||
267 | static struct attribute *max6642_attributes[] = { | 266 | static struct attribute *max6642_attrs[] = { |
268 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 267 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
269 | &sensor_dev_attr_temp2_input.dev_attr.attr, | 268 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
270 | &sensor_dev_attr_temp1_max.dev_attr.attr, | 269 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
@@ -275,52 +274,30 @@ static struct attribute *max6642_attributes[] = { | |||
275 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | 274 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
276 | NULL | 275 | NULL |
277 | }; | 276 | }; |
277 | ATTRIBUTE_GROUPS(max6642); | ||
278 | 278 | ||
279 | static const struct attribute_group max6642_group = { | 279 | static int max6642_probe(struct i2c_client *client, |
280 | .attrs = max6642_attributes, | ||
281 | }; | ||
282 | |||
283 | static int max6642_probe(struct i2c_client *new_client, | ||
284 | const struct i2c_device_id *id) | 280 | const struct i2c_device_id *id) |
285 | { | 281 | { |
282 | struct device *dev = &client->dev; | ||
286 | struct max6642_data *data; | 283 | struct max6642_data *data; |
287 | int err; | 284 | struct device *hwmon_dev; |
288 | 285 | ||
289 | data = devm_kzalloc(&new_client->dev, sizeof(struct max6642_data), | 286 | data = devm_kzalloc(dev, sizeof(struct max6642_data), GFP_KERNEL); |
290 | GFP_KERNEL); | ||
291 | if (!data) | 287 | if (!data) |
292 | return -ENOMEM; | 288 | return -ENOMEM; |
293 | 289 | ||
294 | i2c_set_clientdata(new_client, data); | 290 | data->client = client; |
295 | mutex_init(&data->update_lock); | 291 | mutex_init(&data->update_lock); |
296 | 292 | ||
297 | /* Initialize the MAX6642 chip */ | 293 | /* Initialize the MAX6642 chip */ |
298 | max6642_init_client(new_client); | 294 | max6642_init_client(data, client); |
299 | |||
300 | /* Register sysfs hooks */ | ||
301 | err = sysfs_create_group(&new_client->dev.kobj, &max6642_group); | ||
302 | if (err) | ||
303 | return err; | ||
304 | |||
305 | data->hwmon_dev = hwmon_device_register(&new_client->dev); | ||
306 | if (IS_ERR(data->hwmon_dev)) { | ||
307 | err = PTR_ERR(data->hwmon_dev); | ||
308 | goto exit_remove_files; | ||
309 | } | ||
310 | |||
311 | return 0; | ||
312 | |||
313 | exit_remove_files: | ||
314 | sysfs_remove_group(&new_client->dev.kobj, &max6642_group); | ||
315 | return err; | ||
316 | } | ||
317 | |||
318 | static int max6642_remove(struct i2c_client *client) | ||
319 | { | ||
320 | struct max6642_data *data = i2c_get_clientdata(client); | ||
321 | 295 | ||
322 | hwmon_device_unregister(data->hwmon_dev); | 296 | hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev, |
323 | sysfs_remove_group(&client->dev.kobj, &max6642_group); | 297 | client->name, data, |
298 | max6642_groups); | ||
299 | if (IS_ERR(hwmon_dev)) | ||
300 | return PTR_ERR(hwmon_dev); | ||
324 | 301 | ||
325 | return 0; | 302 | return 0; |
326 | } | 303 | } |
@@ -341,7 +318,6 @@ static struct i2c_driver max6642_driver = { | |||
341 | .name = "max6642", | 318 | .name = "max6642", |
342 | }, | 319 | }, |
343 | .probe = max6642_probe, | 320 | .probe = max6642_probe, |
344 | .remove = max6642_remove, | ||
345 | .id_table = max6642_id, | 321 | .id_table = max6642_id, |
346 | .detect = max6642_detect, | 322 | .detect = max6642_detect, |
347 | .address_list = normal_i2c, | 323 | .address_list = normal_i2c, |