diff options
author | Guenter Roeck <linux@roeck-us.net> | 2013-09-29 14:49:53 -0400 |
---|---|---|
committer | Guenter Roeck <linux@roeck-us.net> | 2013-10-18 12:12:03 -0400 |
commit | 454aee17fd441b8ee8e956196dd3ddc9c8ee96d6 (patch) | |
tree | 753525c8ed3d881904671f8e841f052304b929dd | |
parent | 3f08d7f49f8365d5c9050522ee733951a503e955 (diff) |
hwmon: (emc1403) Convert to use devm_hwmon_device_register_with_groups
Simplify code and reduce its size.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
-rw-r--r-- | drivers/hwmon/emc1403.c | 73 |
1 files changed, 26 insertions, 47 deletions
diff --git a/drivers/hwmon/emc1403.c b/drivers/hwmon/emc1403.c index 142e1cb8dea7..dc2a917a87bb 100644 --- a/drivers/hwmon/emc1403.c +++ b/drivers/hwmon/emc1403.c | |||
@@ -40,7 +40,7 @@ | |||
40 | #define THERMAL_REVISION_REG 0xff | 40 | #define THERMAL_REVISION_REG 0xff |
41 | 41 | ||
42 | struct thermal_data { | 42 | struct thermal_data { |
43 | struct device *hwmon_dev; | 43 | struct i2c_client *client; |
44 | struct mutex mutex; | 44 | struct mutex mutex; |
45 | /* | 45 | /* |
46 | * Cache the hyst value so we don't keep re-reading it. In theory | 46 | * Cache the hyst value so we don't keep re-reading it. In theory |
@@ -53,10 +53,11 @@ struct thermal_data { | |||
53 | static ssize_t show_temp(struct device *dev, | 53 | static ssize_t show_temp(struct device *dev, |
54 | struct device_attribute *attr, char *buf) | 54 | struct device_attribute *attr, char *buf) |
55 | { | 55 | { |
56 | struct i2c_client *client = to_i2c_client(dev); | ||
57 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); | 56 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
58 | int retval = i2c_smbus_read_byte_data(client, sda->index); | 57 | struct thermal_data *data = dev_get_drvdata(dev); |
58 | int retval; | ||
59 | 59 | ||
60 | retval = i2c_smbus_read_byte_data(data->client, sda->index); | ||
60 | if (retval < 0) | 61 | if (retval < 0) |
61 | return retval; | 62 | return retval; |
62 | return sprintf(buf, "%d000\n", retval); | 63 | return sprintf(buf, "%d000\n", retval); |
@@ -65,27 +66,27 @@ static ssize_t show_temp(struct device *dev, | |||
65 | static ssize_t show_bit(struct device *dev, | 66 | static ssize_t show_bit(struct device *dev, |
66 | struct device_attribute *attr, char *buf) | 67 | struct device_attribute *attr, char *buf) |
67 | { | 68 | { |
68 | struct i2c_client *client = to_i2c_client(dev); | ||
69 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); | 69 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
70 | int retval = i2c_smbus_read_byte_data(client, sda->nr); | 70 | struct thermal_data *data = dev_get_drvdata(dev); |
71 | int retval; | ||
71 | 72 | ||
73 | retval = i2c_smbus_read_byte_data(data->client, sda->nr); | ||
72 | if (retval < 0) | 74 | if (retval < 0) |
73 | return retval; | 75 | return retval; |
74 | retval &= sda->index; | 76 | return sprintf(buf, "%d\n", !!(retval & sda->index)); |
75 | return sprintf(buf, "%d\n", retval ? 1 : 0); | ||
76 | } | 77 | } |
77 | 78 | ||
78 | static ssize_t store_temp(struct device *dev, | 79 | static ssize_t store_temp(struct device *dev, |
79 | struct device_attribute *attr, const char *buf, size_t count) | 80 | struct device_attribute *attr, const char *buf, size_t count) |
80 | { | 81 | { |
81 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); | 82 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
82 | struct i2c_client *client = to_i2c_client(dev); | 83 | struct thermal_data *data = dev_get_drvdata(dev); |
83 | unsigned long val; | 84 | unsigned long val; |
84 | int retval; | 85 | int retval; |
85 | 86 | ||
86 | if (kstrtoul(buf, 10, &val)) | 87 | if (kstrtoul(buf, 10, &val)) |
87 | return -EINVAL; | 88 | return -EINVAL; |
88 | retval = i2c_smbus_write_byte_data(client, sda->index, | 89 | retval = i2c_smbus_write_byte_data(data->client, sda->index, |
89 | DIV_ROUND_CLOSEST(val, 1000)); | 90 | DIV_ROUND_CLOSEST(val, 1000)); |
90 | if (retval < 0) | 91 | if (retval < 0) |
91 | return retval; | 92 | return retval; |
@@ -95,9 +96,9 @@ static ssize_t store_temp(struct device *dev, | |||
95 | static ssize_t store_bit(struct device *dev, | 96 | static ssize_t store_bit(struct device *dev, |
96 | struct device_attribute *attr, const char *buf, size_t count) | 97 | struct device_attribute *attr, const char *buf, size_t count) |
97 | { | 98 | { |
98 | struct i2c_client *client = to_i2c_client(dev); | ||
99 | struct thermal_data *data = i2c_get_clientdata(client); | ||
100 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); | 99 | struct sensor_device_attribute_2 *sda = to_sensor_dev_attr_2(attr); |
100 | struct thermal_data *data = dev_get_drvdata(dev); | ||
101 | struct i2c_client *client = data->client; | ||
101 | unsigned long val; | 102 | unsigned long val; |
102 | int retval; | 103 | int retval; |
103 | 104 | ||
@@ -124,9 +125,9 @@ fail: | |||
124 | static ssize_t show_hyst(struct device *dev, | 125 | static ssize_t show_hyst(struct device *dev, |
125 | struct device_attribute *attr, char *buf) | 126 | struct device_attribute *attr, char *buf) |
126 | { | 127 | { |
127 | struct i2c_client *client = to_i2c_client(dev); | ||
128 | struct thermal_data *data = i2c_get_clientdata(client); | ||
129 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); | 128 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
129 | struct thermal_data *data = dev_get_drvdata(dev); | ||
130 | struct i2c_client *client = data->client; | ||
130 | int retval; | 131 | int retval; |
131 | int hyst; | 132 | int hyst; |
132 | 133 | ||
@@ -147,9 +148,9 @@ static ssize_t show_hyst(struct device *dev, | |||
147 | static ssize_t store_hyst(struct device *dev, | 148 | static ssize_t store_hyst(struct device *dev, |
148 | struct device_attribute *attr, const char *buf, size_t count) | 149 | struct device_attribute *attr, const char *buf, size_t count) |
149 | { | 150 | { |
150 | struct i2c_client *client = to_i2c_client(dev); | ||
151 | struct thermal_data *data = i2c_get_clientdata(client); | ||
152 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); | 151 | struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); |
152 | struct thermal_data *data = dev_get_drvdata(dev); | ||
153 | struct i2c_client *client = data->client; | ||
153 | int retval; | 154 | int retval; |
154 | int hyst; | 155 | int hyst; |
155 | unsigned long val; | 156 | unsigned long val; |
@@ -235,7 +236,7 @@ static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO | S_IWUSR, | |||
235 | static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR, | 236 | static SENSOR_DEVICE_ATTR_2(power_state, S_IRUGO | S_IWUSR, |
236 | show_bit, store_bit, 0x03, 0x40); | 237 | show_bit, store_bit, 0x03, 0x40); |
237 | 238 | ||
238 | static struct attribute *mid_att_thermal[] = { | 239 | static struct attribute *emc1403_attrs[] = { |
239 | &sensor_dev_attr_temp1_min.dev_attr.attr, | 240 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
240 | &sensor_dev_attr_temp1_max.dev_attr.attr, | 241 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
241 | &sensor_dev_attr_temp1_crit.dev_attr.attr, | 242 | &sensor_dev_attr_temp1_crit.dev_attr.attr, |
@@ -263,10 +264,7 @@ static struct attribute *mid_att_thermal[] = { | |||
263 | &sensor_dev_attr_power_state.dev_attr.attr, | 264 | &sensor_dev_attr_power_state.dev_attr.attr, |
264 | NULL | 265 | NULL |
265 | }; | 266 | }; |
266 | 267 | ATTRIBUTE_GROUPS(emc1403); | |
267 | static const struct attribute_group m_thermal_gr = { | ||
268 | .attrs = mid_att_thermal | ||
269 | }; | ||
270 | 268 | ||
271 | static int emc1403_detect(struct i2c_client *client, | 269 | static int emc1403_detect(struct i2c_client *client, |
272 | struct i2c_board_info *info) | 270 | struct i2c_board_info *info) |
@@ -304,43 +302,25 @@ static int emc1403_detect(struct i2c_client *client, | |||
304 | static int emc1403_probe(struct i2c_client *client, | 302 | static int emc1403_probe(struct i2c_client *client, |
305 | const struct i2c_device_id *id) | 303 | const struct i2c_device_id *id) |
306 | { | 304 | { |
307 | int res; | ||
308 | struct thermal_data *data; | 305 | struct thermal_data *data; |
306 | struct device *hwmon_dev; | ||
309 | 307 | ||
310 | data = devm_kzalloc(&client->dev, sizeof(struct thermal_data), | 308 | data = devm_kzalloc(&client->dev, sizeof(struct thermal_data), |
311 | GFP_KERNEL); | 309 | GFP_KERNEL); |
312 | if (data == NULL) | 310 | if (data == NULL) |
313 | return -ENOMEM; | 311 | return -ENOMEM; |
314 | 312 | ||
315 | i2c_set_clientdata(client, data); | 313 | data->client = client; |
316 | mutex_init(&data->mutex); | 314 | mutex_init(&data->mutex); |
317 | data->hyst_valid = jiffies - 1; /* Expired */ | 315 | data->hyst_valid = jiffies - 1; /* Expired */ |
318 | 316 | ||
319 | res = sysfs_create_group(&client->dev.kobj, &m_thermal_gr); | 317 | hwmon_dev = hwmon_device_register_with_groups(&client->dev, |
320 | if (res) { | 318 | client->name, data, |
321 | dev_warn(&client->dev, "create group failed\n"); | 319 | emc1403_groups); |
322 | return res; | 320 | if (IS_ERR(hwmon_dev)) |
323 | } | 321 | return PTR_ERR(hwmon_dev); |
324 | data->hwmon_dev = hwmon_device_register(&client->dev); | ||
325 | if (IS_ERR(data->hwmon_dev)) { | ||
326 | res = PTR_ERR(data->hwmon_dev); | ||
327 | dev_warn(&client->dev, "register hwmon dev failed\n"); | ||
328 | goto thermal_error; | ||
329 | } | ||
330 | dev_info(&client->dev, "EMC1403 Thermal chip found\n"); | ||
331 | return 0; | ||
332 | |||
333 | thermal_error: | ||
334 | sysfs_remove_group(&client->dev.kobj, &m_thermal_gr); | ||
335 | return res; | ||
336 | } | ||
337 | 322 | ||
338 | static int emc1403_remove(struct i2c_client *client) | 323 | dev_info(&client->dev, "EMC1403 Thermal chip found\n"); |
339 | { | ||
340 | struct thermal_data *data = i2c_get_clientdata(client); | ||
341 | |||
342 | hwmon_device_unregister(data->hwmon_dev); | ||
343 | sysfs_remove_group(&client->dev.kobj, &m_thermal_gr); | ||
344 | return 0; | 324 | return 0; |
345 | } | 325 | } |
346 | 326 | ||
@@ -362,7 +342,6 @@ static struct i2c_driver sensor_emc1403 = { | |||
362 | }, | 342 | }, |
363 | .detect = emc1403_detect, | 343 | .detect = emc1403_detect, |
364 | .probe = emc1403_probe, | 344 | .probe = emc1403_probe, |
365 | .remove = emc1403_remove, | ||
366 | .id_table = emc1403_idtable, | 345 | .id_table = emc1403_idtable, |
367 | .address_list = emc1403_address_list, | 346 | .address_list = emc1403_address_list, |
368 | }; | 347 | }; |