[PATCH] hwmon: hwmon vs i2c, second round (04/11)
[safe/jmp/linux-2.6] / drivers / i2c / chips / pca9539.c
1 /*
2     pca9539.c - 16-bit I/O port with interrupt and reset
3
4     Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
5
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 */
10
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>
16 #include <linux/i2c-sensor.h>
17
18 /* Addresses to scan */
19 static unsigned short normal_i2c[] = {0x74, 0x75, 0x76, 0x77, I2C_CLIENT_END};
20
21 /* Insmod parameters */
22 SENSORS_INSMOD_1(pca9539);
23
24 enum pca9539_cmd
25 {
26         PCA9539_INPUT_0         = 0,
27         PCA9539_INPUT_1         = 1,
28         PCA9539_OUTPUT_0        = 2,
29         PCA9539_OUTPUT_1        = 3,
30         PCA9539_INVERT_0        = 4,
31         PCA9539_INVERT_1        = 5,
32         PCA9539_DIRECTION_0     = 6,
33         PCA9539_DIRECTION_1     = 7,
34 };
35
36 static int pca9539_attach_adapter(struct i2c_adapter *adapter);
37 static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind);
38 static int pca9539_detach_client(struct i2c_client *client);
39
40 /* This is the driver that will be inserted */
41 static struct i2c_driver pca9539_driver = {
42         .owner          = THIS_MODULE,
43         .name           = "pca9539",
44         .flags          = I2C_DF_NOTIFY,
45         .attach_adapter = pca9539_attach_adapter,
46         .detach_client  = pca9539_detach_client,
47 };
48
49 struct pca9539_data {
50         struct i2c_client client;
51 };
52
53 /* following are the sysfs callback functions */
54 static ssize_t pca9539_show(struct device *dev, struct device_attribute *attr,
55                             char *buf)
56 {
57         struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
58         struct i2c_client *client = to_i2c_client(dev);
59         return sprintf(buf, "%d\n", i2c_smbus_read_byte_data(client,
60                                                              psa->index));
61 }
62
63 static ssize_t pca9539_store(struct device *dev, struct device_attribute *attr,
64                              const char *buf, size_t count)
65 {
66         struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
67         struct i2c_client *client = to_i2c_client(dev);
68         unsigned long val = simple_strtoul(buf, NULL, 0);
69         if (val > 0xff)
70                 return -EINVAL;
71         i2c_smbus_write_byte_data(client, psa->index, val);
72         return count;
73 }
74
75 /* Define the device attributes */
76
77 #define PCA9539_ENTRY_RO(name, cmd_idx) \
78         static SENSOR_DEVICE_ATTR(name, S_IRUGO, pca9539_show, NULL, cmd_idx)
79
80 #define PCA9539_ENTRY_RW(name, cmd_idx) \
81         static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, pca9539_show, \
82                                   pca9539_store, cmd_idx)
83
84 PCA9539_ENTRY_RO(input0, PCA9539_INPUT_0);
85 PCA9539_ENTRY_RO(input1, PCA9539_INPUT_1);
86 PCA9539_ENTRY_RW(output0, PCA9539_OUTPUT_0);
87 PCA9539_ENTRY_RW(output1, PCA9539_OUTPUT_1);
88 PCA9539_ENTRY_RW(invert0, PCA9539_INVERT_0);
89 PCA9539_ENTRY_RW(invert1, PCA9539_INVERT_1);
90 PCA9539_ENTRY_RW(direction0, PCA9539_DIRECTION_0);
91 PCA9539_ENTRY_RW(direction1, PCA9539_DIRECTION_1);
92
93 static struct attribute *pca9539_attributes[] = {
94         &sensor_dev_attr_input0.dev_attr.attr,
95         &sensor_dev_attr_input1.dev_attr.attr,
96         &sensor_dev_attr_output0.dev_attr.attr,
97         &sensor_dev_attr_output1.dev_attr.attr,
98         &sensor_dev_attr_invert0.dev_attr.attr,
99         &sensor_dev_attr_invert1.dev_attr.attr,
100         &sensor_dev_attr_direction0.dev_attr.attr,
101         &sensor_dev_attr_direction1.dev_attr.attr,
102         NULL
103 };
104
105 static struct attribute_group pca9539_defattr_group = {
106         .attrs = pca9539_attributes,
107 };
108
109 static int pca9539_attach_adapter(struct i2c_adapter *adapter)
110 {
111         return i2c_probe(adapter, &addr_data, pca9539_detect);
112 }
113
114 /* This function is called by i2c_probe */
115 static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind)
116 {
117         struct i2c_client *new_client;
118         struct pca9539_data *data;
119         int err = 0;
120
121         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
122                 goto exit;
123
124         /* OK. For now, we presume we have a valid client. We now create the
125            client structure, even though we cannot fill it completely yet. */
126         if (!(data = kmalloc(sizeof(struct pca9539_data), GFP_KERNEL))) {
127                 err = -ENOMEM;
128                 goto exit;
129         }
130         memset(data, 0, sizeof(struct pca9539_data));
131
132         new_client = &data->client;
133         i2c_set_clientdata(new_client, data);
134         new_client->addr = address;
135         new_client->adapter = adapter;
136         new_client->driver = &pca9539_driver;
137         new_client->flags = 0;
138
139         /* Detection: the pca9539 only has 8 registers (0-7).
140            A read of 7 should succeed, but a read of 8 should fail. */
141         if ((i2c_smbus_read_byte_data(new_client, 7) < 0) ||
142             (i2c_smbus_read_byte_data(new_client, 8) >= 0))
143                 goto exit_kfree;
144
145         strlcpy(new_client->name, "pca9539", I2C_NAME_SIZE);
146
147         /* Tell the I2C layer a new client has arrived */
148         if ((err = i2c_attach_client(new_client)))
149                 goto exit_kfree;
150
151         /* Register sysfs hooks (don't care about failure) */
152         sysfs_create_group(&new_client->dev.kobj, &pca9539_defattr_group);
153
154         return 0;
155
156 exit_kfree:
157         kfree(data);
158 exit:
159         return err;
160 }
161
162 static int pca9539_detach_client(struct i2c_client *client)
163 {
164         int err;
165
166         if ((err = i2c_detach_client(client)))
167                 return err;
168
169         kfree(i2c_get_clientdata(client));
170         return 0;
171 }
172
173 static int __init pca9539_init(void)
174 {
175         return i2c_add_driver(&pca9539_driver);
176 }
177
178 static void __exit pca9539_exit(void)
179 {
180         i2c_del_driver(&pca9539_driver);
181 }
182
183 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
184 MODULE_DESCRIPTION("PCA9539 driver");
185 MODULE_LICENSE("GPL");
186
187 module_init(pca9539_init);
188 module_exit(pca9539_exit);
189