aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/lm95241.c
diff options
context:
space:
mode:
authorGuenter Roeck <linux@roeck-us.net>2014-01-20 12:25:50 -0500
committerGuenter Roeck <linux@roeck-us.net>2014-03-03 11:01:03 -0500
commitf809621e8c36c15e9caf248dabe441ac25507e62 (patch)
tree24350710b5015de412ada1bfe75c9de12ea2f687 /drivers/hwmon/lm95241.c
parent0414855fdc4a40da05221fc6062cccbc0c30f169 (diff)
hwmon: (lm95241) Convert to use devm_hwmon_device_register_with_groups
Simplify code, reduce code size, and attach hwmon attributes to hwmon device. Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon/lm95241.c')
-rw-r--r--drivers/hwmon/lm95241.c91
1 files changed, 30 insertions, 61 deletions
diff --git a/drivers/hwmon/lm95241.c b/drivers/hwmon/lm95241.c
index 4b68fb2a31d7..7d5ce89908c2 100644
--- a/drivers/hwmon/lm95241.c
+++ b/drivers/hwmon/lm95241.c
@@ -89,7 +89,7 @@ static const u8 lm95241_reg_address[] = {
89 89
90/* Client data (each client gets its own) */ 90/* Client data (each client gets its own) */
91struct lm95241_data { 91struct lm95241_data {
92 struct device *hwmon_dev; 92 struct i2c_client *client;
93 struct mutex update_lock; 93 struct mutex update_lock;
94 unsigned long last_updated, interval; /* in jiffies */ 94 unsigned long last_updated, interval; /* in jiffies */
95 char valid; /* zero until following fields are valid */ 95 char valid; /* zero until following fields are valid */
@@ -113,8 +113,8 @@ static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
113 113
114static struct lm95241_data *lm95241_update_device(struct device *dev) 114static struct lm95241_data *lm95241_update_device(struct device *dev)
115{ 115{
116 struct i2c_client *client = to_i2c_client(dev); 116 struct lm95241_data *data = dev_get_drvdata(dev);
117 struct lm95241_data *data = i2c_get_clientdata(client); 117 struct i2c_client *client = data->client;
118 118
119 mutex_lock(&data->update_lock); 119 mutex_lock(&data->update_lock);
120 120
@@ -122,7 +122,7 @@ static struct lm95241_data *lm95241_update_device(struct device *dev)
122 !data->valid) { 122 !data->valid) {
123 int i; 123 int i;
124 124
125 dev_dbg(&client->dev, "Updating lm95241 data.\n"); 125 dev_dbg(dev, "Updating lm95241 data.\n");
126 for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++) 126 for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
127 data->temp[i] 127 data->temp[i]
128 = i2c_smbus_read_byte_data(client, 128 = i2c_smbus_read_byte_data(client,
@@ -153,8 +153,7 @@ static ssize_t show_input(struct device *dev, struct device_attribute *attr,
153static ssize_t show_type(struct device *dev, struct device_attribute *attr, 153static ssize_t show_type(struct device *dev, struct device_attribute *attr,
154 char *buf) 154 char *buf)
155{ 155{
156 struct i2c_client *client = to_i2c_client(dev); 156 struct lm95241_data *data = dev_get_drvdata(dev);
157 struct lm95241_data *data = i2c_get_clientdata(client);
158 157
159 return snprintf(buf, PAGE_SIZE - 1, 158 return snprintf(buf, PAGE_SIZE - 1,
160 data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n"); 159 data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n");
@@ -163,8 +162,8 @@ static ssize_t show_type(struct device *dev, struct device_attribute *attr,
163static ssize_t set_type(struct device *dev, struct device_attribute *attr, 162static ssize_t set_type(struct device *dev, struct device_attribute *attr,
164 const char *buf, size_t count) 163 const char *buf, size_t count)
165{ 164{
166 struct i2c_client *client = to_i2c_client(dev); 165 struct lm95241_data *data = dev_get_drvdata(dev);
167 struct lm95241_data *data = i2c_get_clientdata(client); 166 struct i2c_client *client = data->client;
168 unsigned long val; 167 unsigned long val;
169 int shift; 168 int shift;
170 u8 mask = to_sensor_dev_attr(attr)->index; 169 u8 mask = to_sensor_dev_attr(attr)->index;
@@ -201,8 +200,7 @@ static ssize_t set_type(struct device *dev, struct device_attribute *attr,
201static ssize_t show_min(struct device *dev, struct device_attribute *attr, 200static ssize_t show_min(struct device *dev, struct device_attribute *attr,
202 char *buf) 201 char *buf)
203{ 202{
204 struct i2c_client *client = to_i2c_client(dev); 203 struct lm95241_data *data = dev_get_drvdata(dev);
205 struct lm95241_data *data = i2c_get_clientdata(client);
206 204
207 return snprintf(buf, PAGE_SIZE - 1, 205 return snprintf(buf, PAGE_SIZE - 1,
208 data->config & to_sensor_dev_attr(attr)->index ? 206 data->config & to_sensor_dev_attr(attr)->index ?
@@ -212,8 +210,7 @@ static ssize_t show_min(struct device *dev, struct device_attribute *attr,
212static ssize_t set_min(struct device *dev, struct device_attribute *attr, 210static ssize_t set_min(struct device *dev, struct device_attribute *attr,
213 const char *buf, size_t count) 211 const char *buf, size_t count)
214{ 212{
215 struct i2c_client *client = to_i2c_client(dev); 213 struct lm95241_data *data = dev_get_drvdata(dev);
216 struct lm95241_data *data = i2c_get_clientdata(client);
217 long val; 214 long val;
218 215
219 if (kstrtol(buf, 10, &val) < 0) 216 if (kstrtol(buf, 10, &val) < 0)
@@ -229,7 +226,8 @@ static ssize_t set_min(struct device *dev, struct device_attribute *attr,
229 data->config &= ~to_sensor_dev_attr(attr)->index; 226 data->config &= ~to_sensor_dev_attr(attr)->index;
230 data->valid = 0; 227 data->valid = 0;
231 228
232 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); 229 i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
230 data->config);
233 231
234 mutex_unlock(&data->update_lock); 232 mutex_unlock(&data->update_lock);
235 233
@@ -239,8 +237,7 @@ static ssize_t set_min(struct device *dev, struct device_attribute *attr,
239static ssize_t show_max(struct device *dev, struct device_attribute *attr, 237static ssize_t show_max(struct device *dev, struct device_attribute *attr,
240 char *buf) 238 char *buf)
241{ 239{
242 struct i2c_client *client = to_i2c_client(dev); 240 struct lm95241_data *data = dev_get_drvdata(dev);
243 struct lm95241_data *data = i2c_get_clientdata(client);
244 241
245 return snprintf(buf, PAGE_SIZE - 1, 242 return snprintf(buf, PAGE_SIZE - 1,
246 data->config & to_sensor_dev_attr(attr)->index ? 243 data->config & to_sensor_dev_attr(attr)->index ?
@@ -250,8 +247,7 @@ static ssize_t show_max(struct device *dev, struct device_attribute *attr,
250static ssize_t set_max(struct device *dev, struct device_attribute *attr, 247static ssize_t set_max(struct device *dev, struct device_attribute *attr,
251 const char *buf, size_t count) 248 const char *buf, size_t count)
252{ 249{
253 struct i2c_client *client = to_i2c_client(dev); 250 struct lm95241_data *data = dev_get_drvdata(dev);
254 struct lm95241_data *data = i2c_get_clientdata(client);
255 long val; 251 long val;
256 252
257 if (kstrtol(buf, 10, &val) < 0) 253 if (kstrtol(buf, 10, &val) < 0)
@@ -267,7 +263,8 @@ static ssize_t set_max(struct device *dev, struct device_attribute *attr,
267 data->config &= ~to_sensor_dev_attr(attr)->index; 263 data->config &= ~to_sensor_dev_attr(attr)->index;
268 data->valid = 0; 264 data->valid = 0;
269 265
270 i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config); 266 i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
267 data->config);
271 268
272 mutex_unlock(&data->update_lock); 269 mutex_unlock(&data->update_lock);
273 270
@@ -286,8 +283,7 @@ static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
286static ssize_t set_interval(struct device *dev, struct device_attribute *attr, 283static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count) 284 const char *buf, size_t count)
288{ 285{
289 struct i2c_client *client = to_i2c_client(dev); 286 struct lm95241_data *data = dev_get_drvdata(dev);
290 struct lm95241_data *data = i2c_get_clientdata(client);
291 unsigned long val; 287 unsigned long val;
292 288
293 if (kstrtoul(buf, 10, &val) < 0) 289 if (kstrtoul(buf, 10, &val) < 0)
@@ -316,7 +312,7 @@ static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
316static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval, 312static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
317 set_interval); 313 set_interval);
318 314
319static struct attribute *lm95241_attributes[] = { 315static struct attribute *lm95241_attrs[] = {
320 &sensor_dev_attr_temp1_input.dev_attr.attr, 316 &sensor_dev_attr_temp1_input.dev_attr.attr,
321 &sensor_dev_attr_temp2_input.dev_attr.attr, 317 &sensor_dev_attr_temp2_input.dev_attr.attr,
322 &sensor_dev_attr_temp3_input.dev_attr.attr, 318 &sensor_dev_attr_temp3_input.dev_attr.attr,
@@ -329,10 +325,7 @@ static struct attribute *lm95241_attributes[] = {
329 &dev_attr_update_interval.attr, 325 &dev_attr_update_interval.attr,
330 NULL 326 NULL
331}; 327};
332 328ATTRIBUTE_GROUPS(lm95241);
333static const struct attribute_group lm95241_group = {
334 .attrs = lm95241_attributes,
335};
336 329
337/* Return 0 if detection is successful, -ENODEV otherwise */ 330/* Return 0 if detection is successful, -ENODEV otherwise */
338static int lm95241_detect(struct i2c_client *new_client, 331static int lm95241_detect(struct i2c_client *new_client,
@@ -366,10 +359,9 @@ static int lm95241_detect(struct i2c_client *new_client,
366 return 0; 359 return 0;
367} 360}
368 361
369static void lm95241_init_client(struct i2c_client *client) 362static void lm95241_init_client(struct i2c_client *client,
363 struct lm95241_data *data)
370{ 364{
371 struct lm95241_data *data = i2c_get_clientdata(client);
372
373 data->interval = HZ; /* 1 sec default */ 365 data->interval = HZ; /* 1 sec default */
374 data->valid = 0; 366 data->valid = 0;
375 data->config = CFG_CR0076; 367 data->config = CFG_CR0076;
@@ -385,49 +377,27 @@ static void lm95241_init_client(struct i2c_client *client)
385 data->model); 377 data->model);
386} 378}
387 379
388static int lm95241_probe(struct i2c_client *new_client, 380static int lm95241_probe(struct i2c_client *client,
389 const struct i2c_device_id *id) 381 const struct i2c_device_id *id)
390{ 382{
383 struct device *dev = &client->dev;
391 struct lm95241_data *data; 384 struct lm95241_data *data;
392 int err; 385 struct device *hwmon_dev;
393 386
394 data = devm_kzalloc(&new_client->dev, sizeof(struct lm95241_data), 387 data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL);
395 GFP_KERNEL);
396 if (!data) 388 if (!data)
397 return -ENOMEM; 389 return -ENOMEM;
398 390
399 i2c_set_clientdata(new_client, data); 391 data->client = client;
400 mutex_init(&data->update_lock); 392 mutex_init(&data->update_lock);
401 393
402 /* Initialize the LM95241 chip */ 394 /* Initialize the LM95241 chip */
403 lm95241_init_client(new_client); 395 lm95241_init_client(client, data);
404
405 /* Register sysfs hooks */
406 err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
407 if (err)
408 return err;
409
410 data->hwmon_dev = hwmon_device_register(&new_client->dev);
411 if (IS_ERR(data->hwmon_dev)) {
412 err = PTR_ERR(data->hwmon_dev);
413 goto exit_remove_files;
414 }
415
416 return 0;
417
418exit_remove_files:
419 sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
420 return err;
421}
422 396
423static int lm95241_remove(struct i2c_client *client) 397 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
424{ 398 data,
425 struct lm95241_data *data = i2c_get_clientdata(client); 399 lm95241_groups);
426 400 return PTR_ERR_OR_ZERO(hwmon_dev);
427 hwmon_device_unregister(data->hwmon_dev);
428 sysfs_remove_group(&client->dev.kobj, &lm95241_group);
429
430 return 0;
431} 401}
432 402
433/* Driver data (common to all clients) */ 403/* Driver data (common to all clients) */
@@ -444,7 +414,6 @@ static struct i2c_driver lm95241_driver = {
444 .name = DEVNAME, 414 .name = DEVNAME,
445 }, 415 },
446 .probe = lm95241_probe, 416 .probe = lm95241_probe,
447 .remove = lm95241_remove,
448 .id_table = lm95241_id, 417 .id_table = lm95241_id,
449 .detect = lm95241_detect, 418 .detect = lm95241_detect,
450 .address_list = normal_i2c, 419 .address_list = normal_i2c,