aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbgardner@wabtec.com <bgardner@wabtec.com>2005-06-07 09:55:38 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2005-06-22 00:52:05 -0400
commit69dd204b6b45987dbf9ce7058cd238d355865281 (patch)
treee033f9697109d4a411e5c6707b0e6991e00ede7f
parent10c08f8100ee2c4d27b862635574cdf4ef439e67 (diff)
[PATCH] I2C: add new pca9539 driver
This is an i2c driver for the Philips PCA9539 (16 bit I/O port). It uses the new i2c-sysfs interfaces. The patch includes documentation. It depends on the patch that renames "i2c-sysfs.h" to "hwmon-sysfs.h" Signed-off-by: Ben Gardner <bgardner@wabtec.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
-rw-r--r--Documentation/i2c/chips/pca953947
-rw-r--r--drivers/i2c/chips/Kconfig10
-rw-r--r--drivers/i2c/chips/Makefile1
-rw-r--r--drivers/i2c/chips/pca9539.c192
4 files changed, 250 insertions, 0 deletions
diff --git a/Documentation/i2c/chips/pca9539 b/Documentation/i2c/chips/pca9539
new file mode 100644
index 000000000000..c4fce6a13537
--- /dev/null
+++ b/Documentation/i2c/chips/pca9539
@@ -0,0 +1,47 @@
1Kernel driver pca9539
2=====================
3
4Supported chips:
5 * Philips PCA9539
6 Prefix: 'pca9539'
7 Addresses scanned: 0x74 - 0x77
8 Datasheet:
9 http://www.semiconductors.philips.com/acrobat/datasheets/PCA9539_2.pdf
10
11Author: Ben Gardner <bgardner@wabtec.com>
12
13
14Description
15-----------
16
17The Philips PCA9539 is a 16 bit low power I/O device.
18All 16 lines can be individually configured as an input or output.
19The input sense can also be inverted.
20The 16 lines are split between two bytes.
21
22
23Sysfs entries
24-------------
25
26Each is a byte that maps to the 8 I/O bits.
27A '0' suffix is for bits 0-7, while '1' is for bits 8-15.
28
29input[01] - read the current value
30output[01] - sets the output value
31direction[01] - direction of each bit: 1=input, 0=output
32invert[01] - toggle the input bit sense
33
34input reads the actual state of the line and is always available.
35The direction defaults to input for all channels.
36
37
38General Remarks
39---------------
40
41Note that each output, direction, and invert entry controls 8 lines.
42You should use the read, modify, write sequence.
43For example. to set output bit 0 of 1.
44 val=$(cat output0)
45 val=$(( $val | 1 ))
46 echo $val > output0
47
diff --git a/drivers/i2c/chips/Kconfig b/drivers/i2c/chips/Kconfig
index 88437d046518..33de80afd6c6 100644
--- a/drivers/i2c/chips/Kconfig
+++ b/drivers/i2c/chips/Kconfig
@@ -440,6 +440,16 @@ config SENSORS_PCF8574
440 This driver can also be built as a module. If so, the module 440 This driver can also be built as a module. If so, the module
441 will be called pcf8574. 441 will be called pcf8574.
442 442
443config SENSORS_PCA9539
444 tristate "Philips PCA9539 16-bit I/O port"
445 depends on I2C && EXPERIMENTAL
446 help
447 If you say yes here you get support for the Philips PCA9539
448 16-bit I/O port.
449
450 This driver can also be built as a module. If so, the module
451 will be called pca9539.
452
443config SENSORS_PCF8591 453config SENSORS_PCF8591
444 tristate "Philips PCF8591" 454 tristate "Philips PCF8591"
445 depends on I2C && EXPERIMENTAL 455 depends on I2C && EXPERIMENTAL
diff --git a/drivers/i2c/chips/Makefile b/drivers/i2c/chips/Makefile
index 8eed2381da68..6bebdc104166 100644
--- a/drivers/i2c/chips/Makefile
+++ b/drivers/i2c/chips/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_SENSORS_MAX1619) += max1619.o
35obj-$(CONFIG_SENSORS_MAX6875) += max6875.o 35obj-$(CONFIG_SENSORS_MAX6875) += max6875.o
36obj-$(CONFIG_SENSORS_M41T00) += m41t00.o 36obj-$(CONFIG_SENSORS_M41T00) += m41t00.o
37obj-$(CONFIG_SENSORS_PC87360) += pc87360.o 37obj-$(CONFIG_SENSORS_PC87360) += pc87360.o
38obj-$(CONFIG_SENSORS_PCA9539) += pca9539.o
38obj-$(CONFIG_SENSORS_PCF8574) += pcf8574.o 39obj-$(CONFIG_SENSORS_PCF8574) += pcf8574.o
39obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o 40obj-$(CONFIG_SENSORS_PCF8591) += pcf8591.o
40obj-$(CONFIG_SENSORS_RTC8564) += rtc8564.o 41obj-$(CONFIG_SENSORS_RTC8564) += rtc8564.o
diff --git a/drivers/i2c/chips/pca9539.c b/drivers/i2c/chips/pca9539.c
new file mode 100644
index 000000000000..9f3ad45daae2
--- /dev/null
+++ b/drivers/i2c/chips/pca9539.c
@@ -0,0 +1,192 @@
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 */
19static unsigned short normal_i2c[] = {0x74, 0x75, 0x76, 0x77, I2C_CLIENT_END};
20static unsigned int normal_isa[] = {I2C_CLIENT_ISA_END};
21
22/* Insmod parameters */
23SENSORS_INSMOD_1(pca9539);
24
25enum pca9539_cmd
26{
27 PCA9539_INPUT_0 = 0,
28 PCA9539_INPUT_1 = 1,
29 PCA9539_OUTPUT_0 = 2,
30 PCA9539_OUTPUT_1 = 3,
31 PCA9539_INVERT_0 = 4,
32 PCA9539_INVERT_1 = 5,
33 PCA9539_DIRECTION_0 = 6,
34 PCA9539_DIRECTION_1 = 7,
35};
36
37static int pca9539_attach_adapter(struct i2c_adapter *adapter);
38static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind);
39static int pca9539_detach_client(struct i2c_client *client);
40
41/* This is the driver that will be inserted */
42static struct i2c_driver pca9539_driver = {
43 .owner = THIS_MODULE,
44 .name = "pca9539",
45 .flags = I2C_DF_NOTIFY,
46 .attach_adapter = pca9539_attach_adapter,
47 .detach_client = pca9539_detach_client,
48};
49
50struct pca9539_data {
51 struct i2c_client client;
52};
53
54/* following are the sysfs callback functions */
55static ssize_t pca9539_show(struct device *dev, struct device_attribute *attr,
56 char *buf)
57{
58 struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
59 struct i2c_client *client = to_i2c_client(dev);
60 return sprintf(buf, "%d\n", i2c_smbus_read_byte_data(client,
61 psa->index));
62}
63
64static ssize_t pca9539_store(struct device *dev, struct device_attribute *attr,
65 const char *buf, size_t count)
66{
67 struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
68 struct i2c_client *client = to_i2c_client(dev);
69 unsigned long val = simple_strtoul(buf, NULL, 0);
70 if (val > 0xff)
71 return -EINVAL;
72 i2c_smbus_write_byte_data(client, psa->index, val);
73 return count;
74}
75
76/* Define the device attributes */
77
78#define PCA9539_ENTRY_RO(name, cmd_idx) \
79 static SENSOR_DEVICE_ATTR(name, S_IRUGO, pca9539_show, NULL, cmd_idx)
80
81#define PCA9539_ENTRY_RW(name, cmd_idx) \
82 static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, pca9539_show, \
83 pca9539_store, cmd_idx)
84
85PCA9539_ENTRY_RO(input0, PCA9539_INPUT_0);
86PCA9539_ENTRY_RO(input1, PCA9539_INPUT_1);
87PCA9539_ENTRY_RW(output0, PCA9539_OUTPUT_0);
88PCA9539_ENTRY_RW(output1, PCA9539_OUTPUT_1);
89PCA9539_ENTRY_RW(invert0, PCA9539_INVERT_0);
90PCA9539_ENTRY_RW(invert1, PCA9539_INVERT_1);
91PCA9539_ENTRY_RW(direction0, PCA9539_DIRECTION_0);
92PCA9539_ENTRY_RW(direction1, PCA9539_DIRECTION_1);
93
94static struct attribute *pca9539_attributes[] = {
95 &sensor_dev_attr_input0.dev_attr.attr,
96 &sensor_dev_attr_input1.dev_attr.attr,
97 &sensor_dev_attr_output0.dev_attr.attr,
98 &sensor_dev_attr_output1.dev_attr.attr,
99 &sensor_dev_attr_invert0.dev_attr.attr,
100 &sensor_dev_attr_invert1.dev_attr.attr,
101 &sensor_dev_attr_direction0.dev_attr.attr,
102 &sensor_dev_attr_direction1.dev_attr.attr,
103 NULL
104};
105
106static struct attribute_group pca9539_defattr_group = {
107 .attrs = pca9539_attributes,
108};
109
110static int pca9539_attach_adapter(struct i2c_adapter *adapter)
111{
112 return i2c_detect(adapter, &addr_data, pca9539_detect);
113}
114
115/* This function is called by i2c_detect */
116static int pca9539_detect(struct i2c_adapter *adapter, int address, int kind)
117{
118 struct i2c_client *new_client;
119 struct pca9539_data *data;
120 int err = 0;
121
122 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
123 goto exit;
124
125 /* OK. For now, we presume we have a valid client. We now create the
126 client structure, even though we cannot fill it completely yet. */
127 if (!(data = kmalloc(sizeof(struct pca9539_data), GFP_KERNEL))) {
128 err = -ENOMEM;
129 goto exit;
130 }
131 memset(data, 0, sizeof(struct pca9539_data));
132
133 new_client = &data->client;
134 i2c_set_clientdata(new_client, data);
135 new_client->addr = address;
136 new_client->adapter = adapter;
137 new_client->driver = &pca9539_driver;
138 new_client->flags = 0;
139
140 /* Detection: the pca9539 only has 8 registers (0-7).
141 A read of 7 should succeed, but a read of 8 should fail. */
142 if ((i2c_smbus_read_byte_data(new_client, 7) < 0) ||
143 (i2c_smbus_read_byte_data(new_client, 8) >= 0))
144 goto exit_kfree;
145
146 strlcpy(new_client->name, "pca9539", I2C_NAME_SIZE);
147
148 /* Tell the I2C layer a new client has arrived */
149 if ((err = i2c_attach_client(new_client)))
150 goto exit_kfree;
151
152 /* Register sysfs hooks (don't care about failure) */
153 sysfs_create_group(&new_client->dev.kobj, &pca9539_defattr_group);
154
155 return 0;
156
157exit_kfree:
158 kfree(data);
159exit:
160 return err;
161}
162
163static int pca9539_detach_client(struct i2c_client *client)
164{
165 int err;
166
167 if ((err = i2c_detach_client(client))) {
168 dev_err(&client->dev, "Client deregistration failed.\n");
169 return err;
170 }
171
172 kfree(i2c_get_clientdata(client));
173 return 0;
174}
175
176static int __init pca9539_init(void)
177{
178 return i2c_add_driver(&pca9539_driver);
179}
180
181static void __exit pca9539_exit(void)
182{
183 i2c_del_driver(&pca9539_driver);
184}
185
186MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
187MODULE_DESCRIPTION("PCA9539 driver");
188MODULE_LICENSE("GPL");
189
190module_init(pca9539_init);
191module_exit(pca9539_exit);
192