aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/lm75.c
diff options
context:
space:
mode:
authorDavid Brownell <david-b@pacbell.net>2008-05-03 22:33:15 -0400
committerMark M. Hoffman <mhoffman@lightlink.com>2008-07-31 23:44:02 -0400
commit9ebd3d822efeca2e73565516a80373c76ce3fa12 (patch)
tree35947ad745ced49a5bfcd5dc1337ad0a4935d829 /drivers/hwmon/lm75.c
parent01a52397e95a8532c59506691759dba9262d6be7 (diff)
hwmon: (lm75) add new-style driver binding
More LM75 updates: - Teach the LM75 driver to use new-style driver binding: * Create a second driver struct, using new-style driver binding methods cribbed from the legacy code. * Add a MODULE_DEVICE_TABLE (for "newER-style binding") * The legacy probe logic delegates its work to this new code. * The legacy driver now uses the name "lm75_legacy". - More careful initialization. Chips are put into 9-bit mode so the current interconversion routines will never fail. - Save the original chip configuration, and restore it on exit. (Among other things, this normally turns off the mode where the chip is constantly sampling ... and thus saves power.) So the new-style code should catch all chips that boards declare, while the legacy code catches others. This particular coexistence strategy may need some work yet ... legacy modes might best be set up explicitly by some tool not unlike "sensors-detect". (Or else completely eradicated...) Signed-off-by: David Brownell <dbrownell@users.sourceforge.net> Acked-by: Jean Delvare <khali@linux-fr.org> Signed-off-by: Mark M. Hoffman <mhoffman@lightlink.com>
Diffstat (limited to 'drivers/hwmon/lm75.c')
-rw-r--r--drivers/hwmon/lm75.c206
1 files changed, 159 insertions, 47 deletions
diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c
index 25ed26584100..7880c273c2c5 100644
--- a/drivers/hwmon/lm75.c
+++ b/drivers/hwmon/lm75.c
@@ -32,10 +32,28 @@
32 32
33/* 33/*
34 * This driver handles the LM75 and compatible digital temperature sensors. 34 * This driver handles the LM75 and compatible digital temperature sensors.
35 * Compatibles include at least the DS75, DS1775, MCP980x, STDS75, TCN75, 35 * Only types which are _not_ listed in I2C_CLIENT_INSMOD_*() need to be
36 * TMP100, TMP101, TMP75, TMP175, and TMP275. 36 * listed here. We start at 9 since I2C_CLIENT_INSMOD_*() currently allow
37 * definition of up to 8 chip types (plus zero).
37 */ 38 */
38 39
40enum 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
39/* Addresses scanned by legacy style driver binding */ 57/* Addresses scanned by legacy style driver binding */
40static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, 58static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
41 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; 59 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
@@ -54,9 +72,10 @@ static const u8 LM75_REG_TEMP[3] = {
54 72
55/* Each client has this additional data */ 73/* Each client has this additional data */
56struct lm75_data { 74struct lm75_data {
57 struct i2c_client client; 75 struct i2c_client *client;
58 struct device *hwmon_dev; 76 struct device *hwmon_dev;
59 struct mutex update_lock; 77 struct mutex update_lock;
78 u8 orig_conf;
60 char valid; /* !=0 if registers are valid */ 79 char valid; /* !=0 if registers are valid */
61 unsigned long last_updated; /* In jiffies */ 80 unsigned long last_updated; /* In jiffies */
62 u16 temp[3]; /* Register values, 81 u16 temp[3]; /* Register values,
@@ -65,7 +84,6 @@ struct lm75_data {
65 2 = hyst */ 84 2 = hyst */
66}; 85};
67 86
68static void lm75_init_client(struct i2c_client *client);
69static int lm75_read_value(struct i2c_client *client, u8 reg); 87static int lm75_read_value(struct i2c_client *client, u8 reg);
70static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value); 88static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value);
71static struct lm75_data *lm75_update_device(struct device *dev); 89static struct lm75_data *lm75_update_device(struct device *dev);
@@ -120,16 +138,124 @@ static const struct attribute_group lm75_group = {
120 138
121/*-----------------------------------------------------------------------*/ 139/*-----------------------------------------------------------------------*/
122 140
141/* "New style" I2C driver binding -- following the driver model */
142
143static int
144lm75_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
200exit_remove:
201 sysfs_remove_group(&client->dev.kobj, &lm75_group);
202exit_free:
203 i2c_set_clientdata(client, NULL);
204 kfree(data);
205 return status;
206}
207
208static 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
220static 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};
237MODULE_DEVICE_TABLE(i2c, lm75_ids);
238
239static 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
123/* "Legacy" I2C driver binding */ 250/* "Legacy" I2C driver binding */
124 251
125static struct i2c_driver lm75_driver; 252static struct i2c_driver lm75_legacy_driver;
126 253
127/* This function is called by i2c_probe */ 254/* This function is called by i2c_probe */
128static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) 255static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
129{ 256{
130 int i; 257 int i;
131 struct i2c_client *new_client; 258 struct i2c_client *new_client;
132 struct lm75_data *data;
133 int err = 0; 259 int err = 0;
134 260
135 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 261 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
@@ -139,16 +265,15 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
139 /* OK. For now, we presume we have a valid address. We create the 265 /* OK. For now, we presume we have a valid address. We create the
140 client structure, even though there may be no sensor present. 266 client structure, even though there may be no sensor present.
141 But it allows us to use i2c_smbus_read_*_data() calls. */ 267 But it allows us to use i2c_smbus_read_*_data() calls. */
142 if (!(data = kzalloc(sizeof(struct lm75_data), GFP_KERNEL))) { 268 new_client = kzalloc(sizeof *new_client, GFP_KERNEL);
269 if (!new_client) {
143 err = -ENOMEM; 270 err = -ENOMEM;
144 goto exit; 271 goto exit;
145 } 272 }
146 273
147 new_client = &data->client;
148 i2c_set_clientdata(new_client, data);
149 new_client->addr = address; 274 new_client->addr = address;
150 new_client->adapter = adapter; 275 new_client->adapter = adapter;
151 new_client->driver = &lm75_driver; 276 new_client->driver = &lm75_legacy_driver;
152 new_client->flags = 0; 277 new_client->flags = 0;
153 278
154 /* Now, we do the remaining detection. There is no identification- 279 /* Now, we do the remaining detection. There is no identification-
@@ -189,38 +314,26 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, int kind)
189 goto exit_free; 314 goto exit_free;
190 } 315 }
191 316
192 /* NOTE: we treat "force=..." and "force_lm75=..." the same. */ 317 /* NOTE: we treat "force=..." and "force_lm75=..." the same.
318 * Only new-style driver binding distinguishes chip types.
319 */
193 strlcpy(new_client->name, "lm75", I2C_NAME_SIZE); 320 strlcpy(new_client->name, "lm75", I2C_NAME_SIZE);
194 321
195 /* Fill in the remaining client fields and put it into the global list */
196 data->valid = 0;
197 mutex_init(&data->update_lock);
198
199 /* Tell the I2C layer a new client has arrived */ 322 /* Tell the I2C layer a new client has arrived */
200 if ((err = i2c_attach_client(new_client))) 323 err = i2c_attach_client(new_client);
324 if (err)
201 goto exit_free; 325 goto exit_free;
202 326
203 /* Initialize the LM75 chip */ 327 err = lm75_probe(new_client, NULL);
204 lm75_init_client(new_client); 328 if (err < 0)
205
206 /* Register sysfs hooks */
207 if ((err = sysfs_create_group(&new_client->dev.kobj, &lm75_group)))
208 goto exit_detach; 329 goto exit_detach;
209 330
210 data->hwmon_dev = hwmon_device_register(&new_client->dev);
211 if (IS_ERR(data->hwmon_dev)) {
212 err = PTR_ERR(data->hwmon_dev);
213 goto exit_remove;
214 }
215
216 return 0; 331 return 0;
217 332
218exit_remove:
219 sysfs_remove_group(&new_client->dev.kobj, &lm75_group);
220exit_detach: 333exit_detach:
221 i2c_detach_client(new_client); 334 i2c_detach_client(new_client);
222exit_free: 335exit_free:
223 kfree(data); 336 kfree(new_client);
224exit: 337exit:
225 return err; 338 return err;
226} 339}
@@ -234,17 +347,15 @@ static int lm75_attach_adapter(struct i2c_adapter *adapter)
234 347
235static int lm75_detach_client(struct i2c_client *client) 348static int lm75_detach_client(struct i2c_client *client)
236{ 349{
237 struct lm75_data *data = i2c_get_clientdata(client); 350 lm75_remove(client);
238 hwmon_device_unregister(data->hwmon_dev);
239 sysfs_remove_group(&client->dev.kobj, &lm75_group);
240 i2c_detach_client(client); 351 i2c_detach_client(client);
241 kfree(data); 352 kfree(client);
242 return 0; 353 return 0;
243} 354}
244 355
245static struct i2c_driver lm75_driver = { 356static struct i2c_driver lm75_legacy_driver = {
246 .driver = { 357 .driver = {
247 .name = "lm75", 358 .name = "lm75_legacy",
248 }, 359 },
249 .attach_adapter = lm75_attach_adapter, 360 .attach_adapter = lm75_attach_adapter,
250 .detach_client = lm75_detach_client, 361 .detach_client = lm75_detach_client,
@@ -276,16 +387,6 @@ static int lm75_write_value(struct i2c_client *client, u8 reg, u16 value)
276 return i2c_smbus_write_word_data(client, reg, swab16(value)); 387 return i2c_smbus_write_word_data(client, reg, swab16(value));
277} 388}
278 389
279static void lm75_init_client(struct i2c_client *client)
280{
281 int reg;
282
283 /* Enable if in shutdown mode */
284 reg = lm75_read_value(client, LM75_REG_CONF);
285 if (reg >= 0 && (reg & 0x01))
286 lm75_write_value(client, LM75_REG_CONF, reg & 0xfe);
287}
288
289static struct lm75_data *lm75_update_device(struct device *dev) 390static struct lm75_data *lm75_update_device(struct device *dev)
290{ 391{
291 struct i2c_client *client = to_i2c_client(dev); 392 struct i2c_client *client = to_i2c_client(dev);
@@ -323,11 +424,22 @@ static struct lm75_data *lm75_update_device(struct device *dev)
323 424
324static int __init sensors_lm75_init(void) 425static int __init sensors_lm75_init(void)
325{ 426{
326 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;
327} 438}
328 439
329static void __exit sensors_lm75_exit(void) 440static void __exit sensors_lm75_exit(void)
330{ 441{
442 i2c_del_driver(&lm75_legacy_driver);
331 i2c_del_driver(&lm75_driver); 443 i2c_del_driver(&lm75_driver);
332} 444}
333 445