aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/tmp102.c
diff options
context:
space:
mode:
authorGuenter Roeck <linux@roeck-us.net>2014-02-02 13:08:09 -0500
committerGuenter Roeck <linux@roeck-us.net>2014-05-21 19:02:20 -0400
commitad9beea43f22f80217ac1850b0373718adce5fbc (patch)
tree5b0de841aa959ab1ad81811bee25933c867fd806 /drivers/hwmon/tmp102.c
parentfbd9af164c4a70e6f37b6985de8d481a6958cc2c (diff)
hwmon: (tmp102) Convert to use hwmon_device_register_with_groups
Simplify code, reduce code size, and attach sysfs attributes to hwmon device. Reviewed-by: Jean Delvare <jdelvare@suse.de> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon/tmp102.c')
-rw-r--r--drivers/hwmon/tmp102.c46
1 files changed, 20 insertions, 26 deletions
diff --git a/drivers/hwmon/tmp102.c b/drivers/hwmon/tmp102.c
index 5ea99a25fdfe..51719956cc03 100644
--- a/drivers/hwmon/tmp102.c
+++ b/drivers/hwmon/tmp102.c
@@ -51,6 +51,7 @@
51#define TMP102_THIGH_REG 0x03 51#define TMP102_THIGH_REG 0x03
52 52
53struct tmp102 { 53struct tmp102 {
54 struct i2c_client *client;
54 struct device *hwmon_dev; 55 struct device *hwmon_dev;
55 struct thermal_zone_device *tz; 56 struct thermal_zone_device *tz;
56 struct mutex lock; 57 struct mutex lock;
@@ -77,9 +78,10 @@ static const u8 tmp102_reg[] = {
77 TMP102_THIGH_REG, 78 TMP102_THIGH_REG,
78}; 79};
79 80
80static struct tmp102 *tmp102_update_device(struct i2c_client *client) 81static struct tmp102 *tmp102_update_device(struct device *dev)
81{ 82{
82 struct tmp102 *tmp102 = i2c_get_clientdata(client); 83 struct tmp102 *tmp102 = dev_get_drvdata(dev);
84 struct i2c_client *client = tmp102->client;
83 85
84 mutex_lock(&tmp102->lock); 86 mutex_lock(&tmp102->lock);
85 if (time_after(jiffies, tmp102->last_update + HZ / 3)) { 87 if (time_after(jiffies, tmp102->last_update + HZ / 3)) {
@@ -98,7 +100,7 @@ static struct tmp102 *tmp102_update_device(struct i2c_client *client)
98 100
99static int tmp102_read_temp(void *dev, long *temp) 101static int tmp102_read_temp(void *dev, long *temp)
100{ 102{
101 struct tmp102 *tmp102 = tmp102_update_device(to_i2c_client(dev)); 103 struct tmp102 *tmp102 = tmp102_update_device(dev);
102 104
103 *temp = tmp102->temp[0]; 105 *temp = tmp102->temp[0];
104 106
@@ -110,7 +112,7 @@ static ssize_t tmp102_show_temp(struct device *dev,
110 char *buf) 112 char *buf)
111{ 113{
112 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 114 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
113 struct tmp102 *tmp102 = tmp102_update_device(to_i2c_client(dev)); 115 struct tmp102 *tmp102 = tmp102_update_device(dev);
114 116
115 return sprintf(buf, "%d\n", tmp102->temp[sda->index]); 117 return sprintf(buf, "%d\n", tmp102->temp[sda->index]);
116} 118}
@@ -120,8 +122,8 @@ static ssize_t tmp102_set_temp(struct device *dev,
120 const char *buf, size_t count) 122 const char *buf, size_t count)
121{ 123{
122 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); 124 struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
123 struct i2c_client *client = to_i2c_client(dev); 125 struct tmp102 *tmp102 = dev_get_drvdata(dev);
124 struct tmp102 *tmp102 = i2c_get_clientdata(client); 126 struct i2c_client *client = tmp102->client;
125 long val; 127 long val;
126 int status; 128 int status;
127 129
@@ -145,16 +147,13 @@ static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
145static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, 147static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
146 tmp102_set_temp, 2); 148 tmp102_set_temp, 2);
147 149
148static struct attribute *tmp102_attributes[] = { 150static struct attribute *tmp102_attrs[] = {
149 &sensor_dev_attr_temp1_input.dev_attr.attr, 151 &sensor_dev_attr_temp1_input.dev_attr.attr,
150 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 152 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
151 &sensor_dev_attr_temp1_max.dev_attr.attr, 153 &sensor_dev_attr_temp1_max.dev_attr.attr,
152 NULL 154 NULL
153}; 155};
154 156ATTRIBUTE_GROUPS(tmp102);
155static const struct attribute_group tmp102_attr_group = {
156 .attrs = tmp102_attributes,
157};
158 157
159#define TMP102_CONFIG (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1) 158#define TMP102_CONFIG (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1)
160#define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL) 159#define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL)
@@ -163,6 +162,7 @@ static int tmp102_probe(struct i2c_client *client,
163 const struct i2c_device_id *id) 162 const struct i2c_device_id *id)
164{ 163{
165 struct device *dev = &client->dev; 164 struct device *dev = &client->dev;
165 struct device *hwmon_dev;
166 struct tmp102 *tmp102; 166 struct tmp102 *tmp102;
167 int status; 167 int status;
168 168
@@ -178,6 +178,7 @@ static int tmp102_probe(struct i2c_client *client,
178 return -ENOMEM; 178 return -ENOMEM;
179 179
180 i2c_set_clientdata(client, tmp102); 180 i2c_set_clientdata(client, tmp102);
181 tmp102->client = client;
181 182
182 status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); 183 status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
183 if (status < 0) { 184 if (status < 0) {
@@ -205,19 +206,15 @@ static int tmp102_probe(struct i2c_client *client,
205 tmp102->last_update = jiffies - HZ; 206 tmp102->last_update = jiffies - HZ;
206 mutex_init(&tmp102->lock); 207 mutex_init(&tmp102->lock);
207 208
208 status = sysfs_create_group(&dev->kobj, &tmp102_attr_group); 209 hwmon_dev = hwmon_device_register_with_groups(dev, client->name,
209 if (status) { 210 tmp102, tmp102_groups);
210 dev_dbg(dev, "could not create sysfs files\n"); 211 if (IS_ERR(hwmon_dev)) {
211 goto fail_restore_config;
212 }
213 tmp102->hwmon_dev = hwmon_device_register(dev);
214 if (IS_ERR(tmp102->hwmon_dev)) {
215 dev_dbg(dev, "unable to register hwmon device\n"); 212 dev_dbg(dev, "unable to register hwmon device\n");
216 status = PTR_ERR(tmp102->hwmon_dev); 213 status = PTR_ERR(hwmon_dev);
217 goto fail_remove_sysfs; 214 goto fail_restore_config;
218 } 215 }
219 216 tmp102->hwmon_dev = hwmon_dev;
220 tmp102->tz = thermal_zone_of_sensor_register(dev, 0, dev, 217 tmp102->tz = thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
221 tmp102_read_temp, NULL); 218 tmp102_read_temp, NULL);
222 if (IS_ERR(tmp102->tz)) 219 if (IS_ERR(tmp102->tz))
223 tmp102->tz = NULL; 220 tmp102->tz = NULL;
@@ -226,8 +223,6 @@ static int tmp102_probe(struct i2c_client *client,
226 223
227 return 0; 224 return 0;
228 225
229fail_remove_sysfs:
230 sysfs_remove_group(&dev->kobj, &tmp102_attr_group);
231fail_restore_config: 226fail_restore_config:
232 i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, 227 i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
233 tmp102->config_orig); 228 tmp102->config_orig);
@@ -238,9 +233,8 @@ static int tmp102_remove(struct i2c_client *client)
238{ 233{
239 struct tmp102 *tmp102 = i2c_get_clientdata(client); 234 struct tmp102 *tmp102 = i2c_get_clientdata(client);
240 235
241 thermal_zone_of_sensor_unregister(&client->dev, tmp102->tz); 236 thermal_zone_of_sensor_unregister(tmp102->hwmon_dev, tmp102->tz);
242 hwmon_device_unregister(tmp102->hwmon_dev); 237 hwmon_device_unregister(tmp102->hwmon_dev);
243 sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group);
244 238
245 /* Stop monitoring if device was stopped originally */ 239 /* Stop monitoring if device was stopped originally */
246 if (tmp102->config_orig & TMP102_CONF_SD) { 240 if (tmp102->config_orig & TMP102_CONF_SD) {