2 Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>,
3 Philip Edelbrock <phil@netroedge.com>,
4 Dan Eaton <dan.eaton@rocketlogix.com>
5 Ported to Linux 2.6 by Aurelien Jarno <aurel32@debian.org> with
6 the help of Jean Delvare <khali@linux-fr.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* A few notes about the PCF8574:
25 * The PCF8574 is an 8-bit I/O expander for the I2C bus produced by
26 Philips Semiconductors. It is designed to provide a byte I2C
27 interface to up to 8 separate devices.
29 * The PCF8574 appears as a very simple SMBus device which can be
30 read from or written to with SMBUS byte read/write accesses.
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/i2c.h>
41 /* Addresses to scan */
42 static const unsigned short normal_i2c[] = {
43 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
44 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
48 /* Insmod parameters */
49 I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a);
51 /* Each client has this additional data */
53 struct i2c_client client;
55 int write; /* Remember last written value */
58 static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
59 static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
60 static int pcf8574_detach_client(struct i2c_client *client);
61 static void pcf8574_init_client(struct i2c_client *client);
63 /* This is the driver that will be inserted */
64 static struct i2c_driver pcf8574_driver = {
68 .attach_adapter = pcf8574_attach_adapter,
69 .detach_client = pcf8574_detach_client,
72 /* following are the sysfs callback functions */
73 static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
75 struct i2c_client *client = to_i2c_client(dev);
76 return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
79 static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
81 static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
83 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
88 return sprintf(buf, "%d\n", data->write);
91 static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
94 struct i2c_client *client = to_i2c_client(dev);
95 struct pcf8574_data *data = i2c_get_clientdata(client);
96 unsigned long val = simple_strtoul(buf, NULL, 10);
102 i2c_smbus_write_byte(client, data->write);
106 static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
108 static struct attribute *pcf8574_attributes[] = {
110 &dev_attr_write.attr,
114 static const struct attribute_group pcf8574_attr_group = {
115 .attrs = pcf8574_attributes,
122 static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
124 return i2c_probe(adapter, &addr_data, pcf8574_detect);
127 /* This function is called by i2c_probe */
128 static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
130 struct i2c_client *client;
131 struct pcf8574_data *data;
133 const char *client_name = "";
135 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
138 /* OK. For now, we presume we have a valid client. We now create the
139 client structure, even though we cannot fill it completely yet. */
140 if (!(data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
145 client = &data->client;
146 i2c_set_clientdata(client, data);
147 client->addr = address;
148 client->adapter = adapter;
149 client->driver = &pcf8574_driver;
151 /* Now, we would do the remaining detection. But the PCF8574 is plainly
152 impossible to detect! Stupid chip. */
154 /* Determine the chip type */
156 if (address >= 0x38 && address <= 0x3f)
162 if (kind == pcf8574a)
163 client_name = "pcf8574a";
165 client_name = "pcf8574";
167 /* Fill in the remaining client fields and put it into the global list */
168 strlcpy(client->name, client_name, I2C_NAME_SIZE);
170 /* Tell the I2C layer a new client has arrived */
171 if ((err = i2c_attach_client(client)))
174 /* Initialize the PCF8574 chip */
175 pcf8574_init_client(client);
177 /* Register sysfs hooks */
178 err = sysfs_create_group(&client->dev.kobj, &pcf8574_attr_group);
184 i2c_detach_client(client);
191 static int pcf8574_detach_client(struct i2c_client *client)
195 sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group);
197 if ((err = i2c_detach_client(client)))
200 kfree(i2c_get_clientdata(client));
204 /* Called when we have found a new PCF8574. */
205 static void pcf8574_init_client(struct i2c_client *client)
207 struct pcf8574_data *data = i2c_get_clientdata(client);
208 data->write = -EAGAIN;
211 static int __init pcf8574_init(void)
213 return i2c_add_driver(&pcf8574_driver);
216 static void __exit pcf8574_exit(void)
218 i2c_del_driver(&pcf8574_driver);
223 ("Frodo Looijaard <frodol@dds.nl>, "
224 "Philip Edelbrock <phil@netroedge.com>, "
225 "Dan Eaton <dan.eaton@rocketlogix.com> "
226 "and Aurelien Jarno <aurelien@aurel32.net>");
227 MODULE_DESCRIPTION("PCF8574 driver");
228 MODULE_LICENSE("GPL");
230 module_init(pcf8574_init);
231 module_exit(pcf8574_exit);