diff options
Diffstat (limited to 'drivers/hwmon/lm75.c')
-rw-r--r-- | drivers/hwmon/lm75.c | 282 |
1 files changed, 203 insertions, 79 deletions
diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c index de698dc73020..7880c273c2c5 100644 --- a/drivers/hwmon/lm75.c +++ b/drivers/hwmon/lm75.c | |||
@@ -30,14 +30,37 @@ | |||
30 | #include "lm75.h" | 30 | #include "lm75.h" |
31 | 31 | ||
32 | 32 | ||
33 | /* Addresses to scan */ | 33 | /* |
34 | * This driver handles the LM75 and compatible digital temperature sensors. | ||
35 | * Only types which are _not_ listed in I2C_CLIENT_INSMOD_*() need to be | ||
36 | * listed here. We start at 9 since I2C_CLIENT_INSMOD_*() currently allow | ||
37 | * definition of up to 8 chip types (plus zero). | ||
38 | */ | ||
39 | |||
40 | enum lm75_type { /* keep sorted in alphabetical order */ | ||
41 | ds1775 = 9, | ||
42 | ds75, | ||
43 | /* lm75 -- in I2C_CLIENT_INSMOD_1() */ | ||
44 | lm75a, | ||
45 | max6625, | ||
46 | max6626, | ||
47 | mcp980x, | ||
48 | stds75, | ||
49 | tcn75, | ||
50 | tmp100, | ||
51 | tmp101, | ||
52 | tmp175, | ||
53 | tmp275, | ||
54 | tmp75, | ||
55 | }; | ||
56 | |||
57 | /* Addresses scanned by legacy style driver binding */ | ||
34 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, | 58 | static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, |
35 | 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; | 59 | 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; |
36 | 60 | ||
37 | /* Insmod parameters */ | 61 | /* Insmod parameters (only for legacy style driver binding) */ |
38 | I2C_CLIENT_INSMOD_1(lm75); | 62 | I2C_CLIENT_INSMOD_1(lm75); |
39 | 63 | ||
40 | /* Many LM75 constants specified below */ | ||
41 | 64 | ||
42 | /* The LM75 registers */ | 65 | /* The LM75 registers */ |
43 | #define LM75_REG_CONF 0x01 | 66 | #define LM75_REG_CONF 0x01 |
@@ -49,10 +72,11 @@ static const u8 LM75_REG_TEMP[3] = { | |||
49 | 72 | ||
50 | /* Each client has this additional data */ | 73 | /* Each client has this additional data */ |
51 | struct lm75_data { | 74 | struct lm75_data { |
52 | struct i2c_client client; | 75 | struct i2c_client *client; |
53 | struct device *hwmon_dev; | 76 | struct device *hwmon_dev; |
54 | struct mutex update_lock; | 77 | struct mutex update_lock; |
55 | char valid; /* !=0 if following fields are valid */ | 78 | u8 orig_conf; |
79 | char valid; /* !=0 if registers are valid */ | ||
56 | unsigned long last_updated; /* In jiffies */ | 80 | unsigned long last_updated; /* In jiffies */ |
57 | u16 temp[3]; /* Register values, | 81 | u16 temp[3]; /* Register values, |
58 | 0 = input | 82 | 0 = input |
@@ -60,23 +84,14 @@ struct lm75_data { | |||
60 | 2 = hyst */ | 84 | 2 = hyst */ |
61 | }; | 85 | }; |
62 | 86 | ||
63 | static int lm75_attach_adapter(struct i2c_adapter *adapter); | ||
64 | static int lm75_detect(struct i2c_adapter *adapter, int address, int kind); | ||
65 | static void lm75_init_client(struct i2c_client *client); | ||
66 | static int lm75_detach_client(struct i2c_client *client); | ||
67 | static int lm75_read_value(struct i2c_client *client, u8 reg); | 87 | static int lm75_read_value(struct i2c_client *client, u8 reg); |
68 | static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value); | 88 | static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value); |
69 | static struct lm75_data *lm75_update_device(struct device *dev); | 89 | static struct lm75_data *lm75_update_device(struct device *dev); |
70 | 90 | ||
71 | 91 | ||
72 | /* This is the driver that will be inserted */ | 92 | /*-----------------------------------------------------------------------*/ |
73 | static struct i2c_driver lm75_driver = { | 93 | |
74 | .driver = { | 94 | /* sysfs attributes for hwmon */ |
75 | .name = "lm75", | ||
76 | }, | ||
77 | .attach_adapter = lm75_attach_adapter, | ||
78 | .detach_client = lm75_detach_client, | ||
79 | }; | ||
80 | 95 | ||
81 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, | 96 | static ssize_t show_temp(struct device *dev, struct device_attribute *da, |
82 | char *buf) | 97 | char *buf) |
@@ -109,13 +124,6 @@ static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, | |||
109 | show_temp, set_temp, 2); | 124 | show_temp, set_temp, 2); |
110 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | 125 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
111 | 126 | ||
112 | static int lm75_attach_adapter(struct i2c_adapter *adapter) | ||
113 | { | ||
114 | if (!(adapter->class & I2C_CLASS_HWMON)) | ||
115 | return 0; | ||
116 | return i2c_probe(adapter, &addr_data, lm75_detect); | ||
117 | } | ||
118 | |||
119 | static struct attribute *lm75_attributes[] = { | 127 | static struct attribute *lm75_attributes[] = { |
120 | &sensor_dev_attr_temp1_input.dev_attr.attr, | 128 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
121 | &sensor_dev_attr_temp1_max.dev_attr.attr, | 129 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
@@ -128,32 +136,144 @@ static const struct attribute_group lm75_group = { | |||
128 | .attrs = lm75_attributes, | 136 | .attrs = lm75_attributes, |
129 | }; | 137 | }; |
130 | 138 | ||
139 | /*-----------------------------------------------------------------------*/ | ||
140 | |||
141 | /* "New style" I2C driver binding -- following the driver model */ | ||
142 | |||
143 | static int | ||
144 | lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) | ||
145 | { | ||
146 | struct lm75_data *data; | ||
147 | int status; | ||
148 | u8 set_mask, clr_mask; | ||
149 | int new; | ||
150 | |||
151 | if (!i2c_check_functionality(client->adapter, | ||
152 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) | ||
153 | return -EIO; | ||
154 | |||
155 | data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL); | ||
156 | if (!data) | ||
157 | return -ENOMEM; | ||
158 | |||
159 | i2c_set_clientdata(client, data); | ||
160 | |||
161 | data->client = client; | ||
162 | mutex_init(&data->update_lock); | ||
163 | |||
164 | /* Set to LM75 resolution (9 bits, 1/2 degree C) and range. | ||
165 | * Then tweak to be more precise when appropriate. | ||
166 | */ | ||
167 | set_mask = 0; | ||
168 | clr_mask = (1 << 0) /* continuous conversions */ | ||
169 | | (1 << 6) | (1 << 5); /* 9-bit mode */ | ||
170 | |||
171 | /* configure as specified */ | ||
172 | status = lm75_read_value(client, LM75_REG_CONF); | ||
173 | if (status < 0) { | ||
174 | dev_dbg(&client->dev, "Can't read config? %d\n", status); | ||
175 | goto exit_free; | ||
176 | } | ||
177 | data->orig_conf = status; | ||
178 | new = status & ~clr_mask; | ||
179 | new |= set_mask; | ||
180 | if (status != new) | ||
181 | lm75_write_value(client, LM75_REG_CONF, new); | ||
182 | dev_dbg(&client->dev, "Config %02x\n", new); | ||
183 | |||
184 | /* Register sysfs hooks */ | ||
185 | status = sysfs_create_group(&client->dev.kobj, &lm75_group); | ||
186 | if (status) | ||
187 | goto exit_free; | ||
188 | |||
189 | data->hwmon_dev = hwmon_device_register(&client->dev); | ||
190 | if (IS_ERR(data->hwmon_dev)) { | ||
191 | status = PTR_ERR(data->hwmon_dev); | ||
192 | goto exit_remove; | ||
193 | } | ||
194 | |||
195 | dev_info(&client->dev, "%s: sensor '%s'\n", | ||
196 | data->hwmon_dev->bus_id, client->name); | ||
197 | |||
198 | return 0; | ||
199 | |||
200 | exit_remove: | ||
201 | sysfs_remove_group(&client->dev.kobj, &lm75_group); | ||
202 | exit_free: | ||
203 | i2c_set_clientdata(client, NULL); | ||
204 | kfree(data); | ||
205 | return status; | ||
206 | } | ||
207 | |||
208 | static int lm75_remove(struct i2c_client *client) | ||
209 | { | ||
210 | struct lm75_data *data = i2c_get_clientdata(client); | ||
211 | |||
212 | hwmon_device_unregister(data->hwmon_dev); | ||
213 | sysfs_remove_group(&client->dev.kobj, &lm75_group); | ||
214 | lm75_write_value(client, LM75_REG_CONF, data->orig_conf); | ||
215 | i2c_set_clientdata(client, NULL); | ||
216 | kfree(data); | ||
217 | return 0; | ||
218 | } | ||
219 | |||
220 | static const struct i2c_device_id lm75_ids[] = { | ||
221 | { "ds1775", ds1775, }, | ||
222 | { "ds75", ds75, }, | ||
223 | { "lm75", lm75, }, | ||
224 | { "lm75a", lm75a, }, | ||
225 | { "max6625", max6625, }, | ||
226 | { "max6626", max6626, }, | ||
227 | { "mcp980x", mcp980x, }, | ||
228 | { "stds75", stds75, }, | ||
229 | { "tcn75", tcn75, }, | ||
230 | { "tmp100", tmp100, }, | ||
231 | { "tmp101", tmp101, }, | ||
232 | { "tmp175", tmp175, }, | ||
233 | { "tmp275", tmp275, }, | ||
234 | { "tmp75", tmp75, }, | ||
235 | { /* LIST END */ } | ||
236 | }; | ||
237 | MODULE_DEVICE_TABLE(i2c, lm75_ids); | ||
238 | |||
239 | static struct i2c_driver lm75_driver = { | ||
240 | .driver = { | ||
241 | .name = "lm75", | ||
242 | }, | ||
243 | .probe = lm75_probe, | ||
244 | .remove = lm75_remove, | ||
245 | .id_table = lm75_ids, | ||
246 | }; | ||
247 | |||
248 | /*-----------------------------------------------------------------------*/ | ||
249 | |||
250 | /* "Legacy" I2C driver binding */ | ||
251 | |||
252 | static struct i2c_driver lm75_legacy_driver; | ||
253 | |||
131 | /* This function is called by i2c_probe */ | 254 | /* This function is called by i2c_probe */ |
132 | static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) | 255 | static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) |
133 | { | 256 | { |
134 | int i; | 257 | int i; |
135 | struct i2c_client *new_client; | 258 | struct i2c_client *new_client; |
136 | struct lm75_data *data; | ||
137 | int err = 0; | 259 | int err = 0; |
138 | const char *name = ""; | ||
139 | 260 | ||
140 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | | 261 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | |
141 | I2C_FUNC_SMBUS_WORD_DATA)) | 262 | I2C_FUNC_SMBUS_WORD_DATA)) |
142 | goto exit; | 263 | goto exit; |
143 | 264 | ||
144 | /* OK. For now, we presume we have a valid client. We now create the | 265 | /* OK. For now, we presume we have a valid address. We create the |
145 | client structure, even though we cannot fill it completely yet. | 266 | client structure, even though there may be no sensor present. |
146 | But it allows us to access lm75_{read,write}_value. */ | 267 | But it allows us to use i2c_smbus_read_*_data() calls. */ |
147 | if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) { | 268 | new_client = kzalloc(sizeof *new_client, GFP_KERNEL); |
269 | if (!new_client) { | ||
148 | err = -ENOMEM; | 270 | err = -ENOMEM; |
149 | goto exit; | 271 | goto exit; |
150 | } | 272 | } |
151 | 273 | ||
152 | new_client = &data->client; | ||
153 | i2c_set_clientdata(new_client, data); | ||
154 | new_client->addr = address; | 274 | new_client->addr = address; |
155 | new_client->adapter = adapter; | 275 | new_client->adapter = adapter; |
156 | new_client->driver = &lm75_driver; | 276 | new_client->driver = &lm75_legacy_driver; |
157 | new_client->flags = 0; | 277 | new_client->flags = 0; |
158 | 278 | ||
159 | /* Now, we do the remaining detection. There is no identification- | 279 | /* Now, we do the remaining detection. There is no identification- |
@@ -174,17 +294,17 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) | |||
174 | || i2c_smbus_read_word_data(new_client, 5) != hyst | 294 | || i2c_smbus_read_word_data(new_client, 5) != hyst |
175 | || i2c_smbus_read_word_data(new_client, 6) != hyst | 295 | || i2c_smbus_read_word_data(new_client, 6) != hyst |
176 | || i2c_smbus_read_word_data(new_client, 7) != hyst) | 296 | || i2c_smbus_read_word_data(new_client, 7) != hyst) |
177 | goto exit_free; | 297 | goto exit_free; |
178 | os = i2c_smbus_read_word_data(new_client, 3); | 298 | os = i2c_smbus_read_word_data(new_client, 3); |
179 | if (i2c_smbus_read_word_data(new_client, 4) != os | 299 | if (i2c_smbus_read_word_data(new_client, 4) != os |
180 | || i2c_smbus_read_word_data(new_client, 5) != os | 300 | || i2c_smbus_read_word_data(new_client, 5) != os |
181 | || i2c_smbus_read_word_data(new_client, 6) != os | 301 | || i2c_smbus_read_word_data(new_client, 6) != os |
182 | || i2c_smbus_read_word_data(new_client, 7) != os) | 302 | || i2c_smbus_read_word_data(new_client, 7) != os) |
183 | goto exit_free; | 303 | goto exit_free; |
184 | 304 | ||
185 | /* Unused bits */ | 305 | /* Unused bits */ |
186 | if (conf & 0xe0) | 306 | if (conf & 0xe0) |
187 | goto exit_free; | 307 | goto exit_free; |
188 | 308 | ||
189 | /* Addresses cycling */ | 309 | /* Addresses cycling */ |
190 | for (i = 8; i < 0xff; i += 8) | 310 | for (i = 8; i < 0xff; i += 8) |
@@ -194,58 +314,57 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) | |||
194 | goto exit_free; | 314 | goto exit_free; |
195 | } | 315 | } |
196 | 316 | ||
197 | /* Determine the chip type - only one kind supported! */ | 317 | /* NOTE: we treat "force=..." and "force_lm75=..." the same. |
198 | if (kind <= 0) | 318 | * Only new-style driver binding distinguishes chip types. |
199 | kind = lm75; | 319 | */ |
200 | 320 | strlcpy(new_client->name, "lm75", I2C_NAME_SIZE); | |
201 | if (kind == lm75) { | ||
202 | name = "lm75"; | ||
203 | } | ||
204 | |||
205 | /* Fill in the remaining client fields and put it into the global list */ | ||
206 | strlcpy(new_client->name, name, I2C_NAME_SIZE); | ||
207 | data->valid = 0; | ||
208 | mutex_init(&data->update_lock); | ||
209 | 321 | ||
210 | /* Tell the I2C layer a new client has arrived */ | 322 | /* Tell the I2C layer a new client has arrived */ |
211 | if ((err = i2c_attach_client(new_client))) | 323 | err = i2c_attach_client(new_client); |
324 | if (err) | ||
212 | goto exit_free; | 325 | goto exit_free; |
213 | 326 | ||
214 | /* Initialize the LM75 chip */ | 327 | err = lm75_probe(new_client, NULL); |
215 | lm75_init_client(new_client); | 328 | if (err < 0) |
216 | |||
217 | /* Register sysfs hooks */ | ||
218 | if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group))) | ||
219 | goto exit_detach; | 329 | goto exit_detach; |
220 | 330 | ||
221 | data->hwmon_dev = hwmon_device_register(&new_client->dev); | ||
222 | if (IS_ERR(data->hwmon_dev)) { | ||
223 | err = PTR_ERR(data->hwmon_dev); | ||
224 | goto exit_remove; | ||
225 | } | ||
226 | |||
227 | return 0; | 331 | return 0; |
228 | 332 | ||
229 | exit_remove: | ||
230 | sysfs_remove_group(&new_client->dev.kobj, &lm75_group); | ||
231 | exit_detach: | 333 | exit_detach: |
232 | i2c_detach_client(new_client); | 334 | i2c_detach_client(new_client); |
233 | exit_free: | 335 | exit_free: |
234 | kfree(data); | 336 | kfree(new_client); |
235 | exit: | 337 | exit: |
236 | return err; | 338 | return err; |
237 | } | 339 | } |
238 | 340 | ||
341 | static int lm75_attach_adapter(struct i2c_adapter *adapter) | ||
342 | { | ||
343 | if (!(adapter->class & I2C_CLASS_HWMON)) | ||
344 | return 0; | ||
345 | return i2c_probe(adapter, &addr_data, lm75_detect); | ||
346 | } | ||
347 | |||
239 | static int lm75_detach_client(struct i2c_client *client) | 348 | static int lm75_detach_client(struct i2c_client *client) |
240 | { | 349 | { |
241 | struct lm75_data *data = i2c_get_clientdata(client); | 350 | lm75_remove(client); |
242 | hwmon_device_unregister(data->hwmon_dev); | ||
243 | sysfs_remove_group(&client->dev.kobj, &lm75_group); | ||
244 | i2c_detach_client(client); | 351 | i2c_detach_client(client); |
245 | kfree(data); | 352 | kfree(client); |
246 | return 0; | 353 | return 0; |
247 | } | 354 | } |
248 | 355 | ||
356 | static struct i2c_driver lm75_legacy_driver = { | ||
357 | .driver = { | ||
358 | .name = "lm75_legacy", | ||
359 | }, | ||
360 | .attach_adapter = lm75_attach_adapter, | ||
361 | .detach_client = lm75_detach_client, | ||
362 | }; | ||
363 | |||
364 | /*-----------------------------------------------------------------------*/ | ||
365 | |||
366 | /* register access */ | ||
367 | |||
249 | /* All registers are word-sized, except for the configuration register. | 368 | /* All registers are word-sized, except for the configuration register. |
250 | LM75 uses a high-byte first convention, which is exactly opposite to | 369 | LM75 uses a high-byte first convention, which is exactly opposite to |
251 | the SMBus standard. */ | 370 | the SMBus standard. */ |
@@ -268,16 +387,6 @@ static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value) | |||
268 | return i2c_smbus_write_word_data(client, reg, swab16(value)); | 387 | return i2c_smbus_write_word_data(client, reg, swab16(value)); |
269 | } | 388 | } |
270 | 389 | ||
271 | static void lm75_init_client(struct i2c_client *client) | ||
272 | { | ||
273 | int reg; | ||
274 | |||
275 | /* Enable if in shutdown mode */ | ||
276 | reg = lm75_read_value(client, LM75_REG_CONF); | ||
277 | if (reg >= 0 && (reg & 0x01)) | ||
278 | lm75_write_value(client, LM75_REG_CONF, reg & 0xfe); | ||
279 | } | ||
280 | |||
281 | static struct lm75_data *lm75_update_device(struct device *dev) | 390 | static struct lm75_data *lm75_update_device(struct device *dev) |
282 | { | 391 | { |
283 | struct i2c_client *client = to_i2c_client(dev); | 392 | struct i2c_client *client = to_i2c_client(dev); |
@@ -309,13 +418,28 @@ static struct lm75_data *lm75_update_device(struct device *dev) | |||
309 | return data; | 418 | return data; |
310 | } | 419 | } |
311 | 420 | ||
421 | /*-----------------------------------------------------------------------*/ | ||
422 | |||
423 | /* module glue */ | ||
424 | |||
312 | static int __init sensors_lm75_init(void) | 425 | static int __init sensors_lm75_init(void) |
313 | { | 426 | { |
314 | return i2c_add_driver(&lm75_driver); | 427 | int status; |
428 | |||
429 | status = i2c_add_driver(&lm75_driver); | ||
430 | if (status < 0) | ||
431 | return status; | ||
432 | |||
433 | status = i2c_add_driver(&lm75_legacy_driver); | ||
434 | if (status < 0) | ||
435 | i2c_del_driver(&lm75_driver); | ||
436 | |||
437 | return status; | ||
315 | } | 438 | } |
316 | 439 | ||
317 | static void __exit sensors_lm75_exit(void) | 440 | static void __exit sensors_lm75_exit(void) |
318 | { | 441 | { |
442 | i2c_del_driver(&lm75_legacy_driver); | ||
319 | i2c_del_driver(&lm75_driver); | 443 | i2c_del_driver(&lm75_driver); |
320 | } | 444 | } |
321 | 445 | ||