aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/ad7418.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon/ad7418.c')
-rw-r--r--drivers/hwmon/ad7418.c109
1 files changed, 26 insertions, 83 deletions
diff --git a/drivers/hwmon/ad7418.c b/drivers/hwmon/ad7418.c
index 466b9ee92797..f97b5b356875 100644
--- a/drivers/hwmon/ad7418.c
+++ b/drivers/hwmon/ad7418.c
@@ -23,12 +23,9 @@
23 23
24#include "lm75.h" 24#include "lm75.h"
25 25
26#define DRV_VERSION "0.3" 26#define DRV_VERSION "0.4"
27 27
28/* Addresses to scan */ 28enum chips { ad7416, ad7417, ad7418 };
29static const unsigned short normal_i2c[] = { 0x28, I2C_CLIENT_END };
30/* Insmod parameters */
31I2C_CLIENT_INSMOD_3(ad7416, ad7417, ad7418);
32 29
33/* AD7418 registers */ 30/* AD7418 registers */
34#define AD7418_REG_TEMP_IN 0x00 31#define AD7418_REG_TEMP_IN 0x00
@@ -46,7 +43,6 @@ static const u8 AD7418_REG_TEMP[] = { AD7418_REG_TEMP_IN,
46 AD7418_REG_TEMP_OS }; 43 AD7418_REG_TEMP_OS };
47 44
48struct ad7418_data { 45struct ad7418_data {
49 struct i2c_client client;
50 struct device *hwmon_dev; 46 struct device *hwmon_dev;
51 struct attribute_group attrs; 47 struct attribute_group attrs;
52 enum chips type; 48 enum chips type;
@@ -58,16 +54,25 @@ struct ad7418_data {
58 u16 in[4]; 54 u16 in[4];
59}; 55};
60 56
61static int ad7418_attach_adapter(struct i2c_adapter *adapter); 57static int ad7418_probe(struct i2c_client *client,
62static int ad7418_detect(struct i2c_adapter *adapter, int address, int kind); 58 const struct i2c_device_id *id);
63static int ad7418_detach_client(struct i2c_client *client); 59static int ad7418_remove(struct i2c_client *client);
60
61static const struct i2c_device_id ad7418_id[] = {
62 { "ad7416", ad7416 },
63 { "ad7417", ad7417 },
64 { "ad7418", ad7418 },
65 { }
66};
67MODULE_DEVICE_TABLE(i2c, ad7418_id);
64 68
65static struct i2c_driver ad7418_driver = { 69static struct i2c_driver ad7418_driver = {
66 .driver = { 70 .driver = {
67 .name = "ad7418", 71 .name = "ad7418",
68 }, 72 },
69 .attach_adapter = ad7418_attach_adapter, 73 .probe = ad7418_probe,
70 .detach_client = ad7418_detach_client, 74 .remove = ad7418_remove,
75 .id_table = ad7418_id,
71}; 76};
72 77
73/* All registers are word-sized, except for the configuration registers. 78/* All registers are word-sized, except for the configuration registers.
@@ -192,13 +197,6 @@ static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_adc, NULL, 1);
192static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2); 197static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_adc, NULL, 2);
193static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3); 198static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_adc, NULL, 3);
194 199
195static int ad7418_attach_adapter(struct i2c_adapter *adapter)
196{
197 if (!(adapter->class & I2C_CLASS_HWMON))
198 return 0;
199 return i2c_probe(adapter, &addr_data, ad7418_detect);
200}
201
202static struct attribute *ad7416_attributes[] = { 200static struct attribute *ad7416_attributes[] = {
203 &sensor_dev_attr_temp1_max.dev_attr.attr, 201 &sensor_dev_attr_temp1_max.dev_attr.attr,
204 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, 202 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
@@ -225,98 +223,46 @@ static struct attribute *ad7418_attributes[] = {
225 NULL 223 NULL
226}; 224};
227 225
228static int ad7418_detect(struct i2c_adapter *adapter, int address, int kind) 226static int ad7418_probe(struct i2c_client *client,
227 const struct i2c_device_id *id)
229{ 228{
230 struct i2c_client *client; 229 struct i2c_adapter *adapter = client->adapter;
231 struct ad7418_data *data; 230 struct ad7418_data *data;
232 int err = 0; 231 int err;
233 232
234 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | 233 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
235 I2C_FUNC_SMBUS_WORD_DATA)) 234 I2C_FUNC_SMBUS_WORD_DATA)) {
235 err = -EOPNOTSUPP;
236 goto exit; 236 goto exit;
237 }
237 238
238 if (!(data = kzalloc(sizeof(struct ad7418_data), GFP_KERNEL))) { 239 if (!(data = kzalloc(sizeof(struct ad7418_data), GFP_KERNEL))) {
239 err = -ENOMEM; 240 err = -ENOMEM;
240 goto exit; 241 goto exit;
241 } 242 }
242 243
243 client = &data->client;
244 client->addr = address;
245 client->adapter = adapter;
246 client->driver = &ad7418_driver;
247
248 i2c_set_clientdata(client, data); 244 i2c_set_clientdata(client, data);
249 245
250 mutex_init(&data->lock); 246 mutex_init(&data->lock);
251 247 data->type = id->driver_data;
252 /* AD7418 has a curious behaviour on registers 6 and 7. They
253 * both always read 0xC071 and are not documented on the datasheet.
254 * We use them to detect the chip.
255 */
256 if (kind <= 0) {
257 int reg, reg6, reg7;
258
259 /* the AD7416 lies within this address range, but I have
260 * no means to check.
261 */
262 if (address >= 0x48 && address <= 0x4f) {
263 /* XXX add tests for AD7416 here */
264 /* data->type = ad7416; */
265 }
266 /* here we might have AD7417 or AD7418 */
267 else if (address >= 0x28 && address <= 0x2f) {
268 reg6 = i2c_smbus_read_word_data(client, 0x06);
269 reg7 = i2c_smbus_read_word_data(client, 0x07);
270
271 if (address == 0x28 && reg6 == 0xC071 && reg7 == 0xC071)
272 data->type = ad7418;
273
274 /* XXX add tests for AD7417 here */
275
276
277 /* both AD7417 and AD7418 have bits 0-5 of
278 * the CONF2 register at 0
279 */
280 reg = i2c_smbus_read_byte_data(client,
281 AD7418_REG_CONF2);
282 if (reg & 0x3F)
283 data->type = any_chip; /* detection failed */
284 }
285 } else {
286 dev_dbg(&adapter->dev, "detection forced\n");
287 }
288
289 if (kind > 0)
290 data->type = kind;
291 else if (kind < 0 && data->type == any_chip) {
292 err = -ENODEV;
293 goto exit_free;
294 }
295 248
296 switch (data->type) { 249 switch (data->type) {
297 case any_chip:
298 case ad7416: 250 case ad7416:
299 data->adc_max = 0; 251 data->adc_max = 0;
300 data->attrs.attrs = ad7416_attributes; 252 data->attrs.attrs = ad7416_attributes;
301 strlcpy(client->name, "ad7416", I2C_NAME_SIZE);
302 break; 253 break;
303 254
304 case ad7417: 255 case ad7417:
305 data->adc_max = 4; 256 data->adc_max = 4;
306 data->attrs.attrs = ad7417_attributes; 257 data->attrs.attrs = ad7417_attributes;
307 strlcpy(client->name, "ad7417", I2C_NAME_SIZE);
308 break; 258 break;
309 259
310 case ad7418: 260 case ad7418:
311 data->adc_max = 1; 261 data->adc_max = 1;
312 data->attrs.attrs = ad7418_attributes; 262 data->attrs.attrs = ad7418_attributes;
313 strlcpy(client->name, "ad7418", I2C_NAME_SIZE);
314 break; 263 break;
315 } 264 }
316 265
317 if ((err = i2c_attach_client(client)))
318 goto exit_free;
319
320 dev_info(&client->dev, "%s chip found\n", client->name); 266 dev_info(&client->dev, "%s chip found\n", client->name);
321 267
322 /* Initialize the AD7418 chip */ 268 /* Initialize the AD7418 chip */
@@ -324,7 +270,7 @@ static int ad7418_detect(struct i2c_adapter *adapter, int address, int kind)
324 270
325 /* Register sysfs hooks */ 271 /* Register sysfs hooks */
326 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs))) 272 if ((err = sysfs_create_group(&client->dev.kobj, &data->attrs)))
327 goto exit_detach; 273 goto exit_free;
328 274
329 data->hwmon_dev = hwmon_device_register(&client->dev); 275 data->hwmon_dev = hwmon_device_register(&client->dev);
330 if (IS_ERR(data->hwmon_dev)) { 276 if (IS_ERR(data->hwmon_dev)) {
@@ -336,20 +282,17 @@ static int ad7418_detect(struct i2c_adapter *adapter, int address, int kind)
336 282
337exit_remove: 283exit_remove:
338 sysfs_remove_group(&client->dev.kobj, &data->attrs); 284 sysfs_remove_group(&client->dev.kobj, &data->attrs);
339exit_detach:
340 i2c_detach_client(client);
341exit_free: 285exit_free:
342 kfree(data); 286 kfree(data);
343exit: 287exit:
344 return err; 288 return err;
345} 289}
346 290
347static int ad7418_detach_client(struct i2c_client *client) 291static int ad7418_remove(struct i2c_client *client)
348{ 292{
349 struct ad7418_data *data = i2c_get_clientdata(client); 293 struct ad7418_data *data = i2c_get_clientdata(client);
350 hwmon_device_unregister(data->hwmon_dev); 294 hwmon_device_unregister(data->hwmon_dev);
351 sysfs_remove_group(&client->dev.kobj, &data->attrs); 295 sysfs_remove_group(&client->dev.kobj, &data->attrs);
352 i2c_detach_client(client);
353 kfree(data); 296 kfree(data);
354 return 0; 297 return 0;
355} 298}