diff options
Diffstat (limited to 'drivers/hwmon/tmp102.c')
-rw-r--r-- | drivers/hwmon/tmp102.c | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/drivers/hwmon/tmp102.c b/drivers/hwmon/tmp102.c index 6748b4583e7b..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 | ||
53 | struct tmp102 { | 53 | struct 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 | ||
80 | static struct tmp102 *tmp102_update_device(struct i2c_client *client) | 81 | static 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 | ||
99 | static int tmp102_read_temp(void *dev, long *temp) | 101 | static 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, | |||
145 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, | 147 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, |
146 | tmp102_set_temp, 2); | 148 | tmp102_set_temp, 2); |
147 | 149 | ||
148 | static struct attribute *tmp102_attributes[] = { | 150 | static 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 | 156 | ATTRIBUTE_GROUPS(tmp102); | |
155 | static 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) |
@@ -162,72 +161,68 @@ static const struct attribute_group tmp102_attr_group = { | |||
162 | static int tmp102_probe(struct i2c_client *client, | 161 | static int tmp102_probe(struct i2c_client *client, |
163 | const struct i2c_device_id *id) | 162 | const struct i2c_device_id *id) |
164 | { | 163 | { |
164 | struct device *dev = &client->dev; | ||
165 | struct device *hwmon_dev; | ||
165 | struct tmp102 *tmp102; | 166 | struct tmp102 *tmp102; |
166 | int status; | 167 | int status; |
167 | 168 | ||
168 | if (!i2c_check_functionality(client->adapter, | 169 | if (!i2c_check_functionality(client->adapter, |
169 | I2C_FUNC_SMBUS_WORD_DATA)) { | 170 | I2C_FUNC_SMBUS_WORD_DATA)) { |
170 | dev_err(&client->dev, | 171 | dev_err(dev, |
171 | "adapter doesn't support SMBus word transactions\n"); | 172 | "adapter doesn't support SMBus word transactions\n"); |
172 | return -ENODEV; | 173 | return -ENODEV; |
173 | } | 174 | } |
174 | 175 | ||
175 | tmp102 = devm_kzalloc(&client->dev, sizeof(*tmp102), GFP_KERNEL); | 176 | tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL); |
176 | if (!tmp102) | 177 | if (!tmp102) |
177 | return -ENOMEM; | 178 | return -ENOMEM; |
178 | 179 | ||
179 | i2c_set_clientdata(client, tmp102); | 180 | i2c_set_clientdata(client, tmp102); |
181 | tmp102->client = client; | ||
180 | 182 | ||
181 | status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); | 183 | status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); |
182 | if (status < 0) { | 184 | if (status < 0) { |
183 | dev_err(&client->dev, "error reading config register\n"); | 185 | dev_err(dev, "error reading config register\n"); |
184 | return status; | 186 | return status; |
185 | } | 187 | } |
186 | tmp102->config_orig = status; | 188 | tmp102->config_orig = status; |
187 | status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, | 189 | status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, |
188 | TMP102_CONFIG); | 190 | TMP102_CONFIG); |
189 | if (status < 0) { | 191 | if (status < 0) { |
190 | dev_err(&client->dev, "error writing config register\n"); | 192 | dev_err(dev, "error writing config register\n"); |
191 | goto fail_restore_config; | 193 | goto fail_restore_config; |
192 | } | 194 | } |
193 | status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); | 195 | status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG); |
194 | if (status < 0) { | 196 | if (status < 0) { |
195 | dev_err(&client->dev, "error reading config register\n"); | 197 | dev_err(dev, "error reading config register\n"); |
196 | goto fail_restore_config; | 198 | goto fail_restore_config; |
197 | } | 199 | } |
198 | status &= ~TMP102_CONFIG_RD_ONLY; | 200 | status &= ~TMP102_CONFIG_RD_ONLY; |
199 | if (status != TMP102_CONFIG) { | 201 | if (status != TMP102_CONFIG) { |
200 | dev_err(&client->dev, "config settings did not stick\n"); | 202 | dev_err(dev, "config settings did not stick\n"); |
201 | status = -ENODEV; | 203 | status = -ENODEV; |
202 | goto fail_restore_config; | 204 | goto fail_restore_config; |
203 | } | 205 | } |
204 | tmp102->last_update = jiffies - HZ; | 206 | tmp102->last_update = jiffies - HZ; |
205 | mutex_init(&tmp102->lock); | 207 | mutex_init(&tmp102->lock); |
206 | 208 | ||
207 | status = sysfs_create_group(&client->dev.kobj, &tmp102_attr_group); | 209 | hwmon_dev = hwmon_device_register_with_groups(dev, client->name, |
208 | if (status) { | 210 | tmp102, tmp102_groups); |
209 | dev_dbg(&client->dev, "could not create sysfs files\n"); | 211 | if (IS_ERR(hwmon_dev)) { |
212 | dev_dbg(dev, "unable to register hwmon device\n"); | ||
213 | status = PTR_ERR(hwmon_dev); | ||
210 | goto fail_restore_config; | 214 | goto fail_restore_config; |
211 | } | 215 | } |
212 | tmp102->hwmon_dev = hwmon_device_register(&client->dev); | 216 | tmp102->hwmon_dev = hwmon_dev; |
213 | if (IS_ERR(tmp102->hwmon_dev)) { | 217 | tmp102->tz = thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev, |
214 | dev_dbg(&client->dev, "unable to register hwmon device\n"); | ||
215 | status = PTR_ERR(tmp102->hwmon_dev); | ||
216 | goto fail_remove_sysfs; | ||
217 | } | ||
218 | |||
219 | tmp102->tz = thermal_zone_of_sensor_register(&client->dev, 0, | ||
220 | &client->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; |
224 | 221 | ||
225 | dev_info(&client->dev, "initialized\n"); | 222 | dev_info(dev, "initialized\n"); |
226 | 223 | ||
227 | return 0; | 224 | return 0; |
228 | 225 | ||
229 | fail_remove_sysfs: | ||
230 | sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group); | ||
231 | fail_restore_config: | 226 | fail_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) { |