pcmcia/cm4000: fix lock imbalance
[linux-2.6/mini2440.git] / drivers / i2c / chips / pca9539.c
blob270de4e56a81c436aaf3f3e814d7ca7fd7e73e75
1 /*
2 pca9539.c - 16-bit I/O port with interrupt and reset
4 Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; version 2 of the License.
9 */
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/hwmon-sysfs.h>
17 /* Addresses to scan: none, device is not autodetected */
18 static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
20 /* Insmod parameters */
21 I2C_CLIENT_INSMOD_1(pca9539);
23 enum pca9539_cmd
25 PCA9539_INPUT_0 = 0,
26 PCA9539_INPUT_1 = 1,
27 PCA9539_OUTPUT_0 = 2,
28 PCA9539_OUTPUT_1 = 3,
29 PCA9539_INVERT_0 = 4,
30 PCA9539_INVERT_1 = 5,
31 PCA9539_DIRECTION_0 = 6,
32 PCA9539_DIRECTION_1 = 7,
35 /* following are the sysfs callback functions */
36 static ssize_t pca9539_show(struct device *dev, struct device_attribute *attr,
37 char *buf)
39 struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
40 struct i2c_client *client = to_i2c_client(dev);
41 return sprintf(buf, "%d\n", i2c_smbus_read_byte_data(client,
42 psa->index));
45 static ssize_t pca9539_store(struct device *dev, struct device_attribute *attr,
46 const char *buf, size_t count)
48 struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
49 struct i2c_client *client = to_i2c_client(dev);
50 unsigned long val = simple_strtoul(buf, NULL, 0);
51 if (val > 0xff)
52 return -EINVAL;
53 i2c_smbus_write_byte_data(client, psa->index, val);
54 return count;
57 /* Define the device attributes */
59 #define PCA9539_ENTRY_RO(name, cmd_idx) \
60 static SENSOR_DEVICE_ATTR(name, S_IRUGO, pca9539_show, NULL, cmd_idx)
62 #define PCA9539_ENTRY_RW(name, cmd_idx) \
63 static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, pca9539_show, \
64 pca9539_store, cmd_idx)
66 PCA9539_ENTRY_RO(input0, PCA9539_INPUT_0);
67 PCA9539_ENTRY_RO(input1, PCA9539_INPUT_1);
68 PCA9539_ENTRY_RW(output0, PCA9539_OUTPUT_0);
69 PCA9539_ENTRY_RW(output1, PCA9539_OUTPUT_1);
70 PCA9539_ENTRY_RW(invert0, PCA9539_INVERT_0);
71 PCA9539_ENTRY_RW(invert1, PCA9539_INVERT_1);
72 PCA9539_ENTRY_RW(direction0, PCA9539_DIRECTION_0);
73 PCA9539_ENTRY_RW(direction1, PCA9539_DIRECTION_1);
75 static struct attribute *pca9539_attributes[] = {
76 &sensor_dev_attr_input0.dev_attr.attr,
77 &sensor_dev_attr_input1.dev_attr.attr,
78 &sensor_dev_attr_output0.dev_attr.attr,
79 &sensor_dev_attr_output1.dev_attr.attr,
80 &sensor_dev_attr_invert0.dev_attr.attr,
81 &sensor_dev_attr_invert1.dev_attr.attr,
82 &sensor_dev_attr_direction0.dev_attr.attr,
83 &sensor_dev_attr_direction1.dev_attr.attr,
84 NULL
87 static struct attribute_group pca9539_defattr_group = {
88 .attrs = pca9539_attributes,
91 /* Return 0 if detection is successful, -ENODEV otherwise */
92 static int pca9539_detect(struct i2c_client *client, int kind,
93 struct i2c_board_info *info)
95 struct i2c_adapter *adapter = client->adapter;
97 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
98 return -ENODEV;
100 strlcpy(info->type, "pca9539", I2C_NAME_SIZE);
102 return 0;
105 static int pca9539_probe(struct i2c_client *client,
106 const struct i2c_device_id *id)
108 /* Register sysfs hooks */
109 return sysfs_create_group(&client->dev.kobj,
110 &pca9539_defattr_group);
113 static int pca9539_remove(struct i2c_client *client)
115 sysfs_remove_group(&client->dev.kobj, &pca9539_defattr_group);
116 return 0;
119 static const struct i2c_device_id pca9539_id[] = {
120 { "pca9539", 0 },
124 static struct i2c_driver pca9539_driver = {
125 .driver = {
126 .name = "pca9539",
128 .probe = pca9539_probe,
129 .remove = pca9539_remove,
130 .id_table = pca9539_id,
132 .detect = pca9539_detect,
133 .address_data = &addr_data,
136 static int __init pca9539_init(void)
138 return i2c_add_driver(&pca9539_driver);
141 static void __exit pca9539_exit(void)
143 i2c_del_driver(&pca9539_driver);
146 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
147 MODULE_DESCRIPTION("PCA9539 driver");
148 MODULE_LICENSE("GPL");
150 module_init(pca9539_init);
151 module_exit(pca9539_exit);