blob: 1b3db2b3ada9506d2a108f499753ba933d33be00 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 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>
7
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.
12
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.
17
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.
21*/
22
23/* A few notes about the PCF8574:
24
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.
28
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.
31
32 --Dan
33
34*/
35
36#include <linux/module.h>
37#include <linux/init.h>
38#include <linux/slab.h>
39#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Addresses to scan */
Jean Delvare2cdddeb2008-01-27 18:14:47 +010042static const unsigned short normal_i2c[] = {
43 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
44 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
45 I2C_CLIENT_END
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020049I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* Each client has this additional data */
52struct pcf8574_data {
53 struct i2c_client client;
54
Jean Delvare553515e2007-10-13 23:56:31 +020055 int write; /* Remember last written value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
57
58static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
59static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
60static int pcf8574_detach_client(struct i2c_client *client);
61static void pcf8574_init_client(struct i2c_client *client);
62
63/* This is the driver that will be inserted */
64static struct i2c_driver pcf8574_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010065 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010066 .name = "pcf8574",
67 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .attach_adapter = pcf8574_attach_adapter,
69 .detach_client = pcf8574_detach_client,
70};
71
72/* following are the sysfs callback functions */
Yani Ioannoua5099cf2005-05-17 06:42:25 -040073static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb071cb2005-06-04 13:17:43 +020076 return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
80
Yani Ioannoua5099cf2005-05-17 06:42:25 -040081static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
Jean Delvare553515e2007-10-13 23:56:31 +020084
85 if (data->write < 0)
86 return data->write;
87
88 return sprintf(buf, "%d\n", data->write);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Yani Ioannoua5099cf2005-05-17 06:42:25 -040091static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 size_t count)
93{
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);
97
98 if (val > 0xff)
99 return -EINVAL;
100
101 data->write = val;
102 i2c_smbus_write_byte(client, data->write);
103 return count;
104}
105
106static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
107
Jean Delvare7d9db672006-09-03 22:20:24 +0200108static struct attribute *pcf8574_attributes[] = {
109 &dev_attr_read.attr,
110 &dev_attr_write.attr,
111 NULL
112};
113
114static const struct attribute_group pcf8574_attr_group = {
115 .attrs = pcf8574_attributes,
116};
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * Real code
120 */
121
122static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
123{
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200124 return i2c_probe(adapter, &addr_data, pcf8574_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200127/* This function is called by i2c_probe */
Ben Dooks6344a8e2005-10-26 21:09:41 +0200128static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Jean Delvaref741f672008-07-14 22:38:36 +0200130 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct pcf8574_data *data;
132 int err = 0;
133 const char *client_name = "";
134
135 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
136 goto exit;
137
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. */
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200140 if (!(data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 err = -ENOMEM;
142 goto exit;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jean Delvaref741f672008-07-14 22:38:36 +0200145 client = &data->client;
146 i2c_set_clientdata(client, data);
147 client->addr = address;
148 client->adapter = adapter;
149 client->driver = &pcf8574_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* Now, we would do the remaining detection. But the PCF8574 is plainly
152 impossible to detect! Stupid chip. */
153
154 /* Determine the chip type */
155 if (kind <= 0) {
156 if (address >= 0x38 && address <= 0x3f)
157 kind = pcf8574a;
158 else
159 kind = pcf8574;
160 }
161
162 if (kind == pcf8574a)
163 client_name = "pcf8574a";
164 else
165 client_name = "pcf8574";
166
167 /* Fill in the remaining client fields and put it into the global list */
Jean Delvaref741f672008-07-14 22:38:36 +0200168 strlcpy(client->name, client_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* Tell the I2C layer a new client has arrived */
Jean Delvaref741f672008-07-14 22:38:36 +0200171 if ((err = i2c_attach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto exit_free;
173
174 /* Initialize the PCF8574 chip */
Jean Delvaref741f672008-07-14 22:38:36 +0200175 pcf8574_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* Register sysfs hooks */
Jean Delvaref741f672008-07-14 22:38:36 +0200178 err = sysfs_create_group(&client->dev.kobj, &pcf8574_attr_group);
Jean Delvare7d9db672006-09-03 22:20:24 +0200179 if (err)
180 goto exit_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182
Jean Delvare7d9db672006-09-03 22:20:24 +0200183 exit_detach:
Jean Delvaref741f672008-07-14 22:38:36 +0200184 i2c_detach_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 exit_free:
186 kfree(data);
187 exit:
188 return err;
189}
190
191static int pcf8574_detach_client(struct i2c_client *client)
192{
193 int err;
194
Jean Delvare7d9db672006-09-03 22:20:24 +0200195 sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group);
196
Jean Delvare7bef5592005-07-27 22:14:49 +0200197 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 kfree(i2c_get_clientdata(client));
201 return 0;
202}
203
204/* Called when we have found a new PCF8574. */
205static void pcf8574_init_client(struct i2c_client *client)
206{
207 struct pcf8574_data *data = i2c_get_clientdata(client);
Jean Delvare553515e2007-10-13 23:56:31 +0200208 data->write = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211static int __init pcf8574_init(void)
212{
213 return i2c_add_driver(&pcf8574_driver);
214}
215
216static void __exit pcf8574_exit(void)
217{
218 i2c_del_driver(&pcf8574_driver);
219}
220
221
222MODULE_AUTHOR
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>");
227MODULE_DESCRIPTION("PCF8574 driver");
228MODULE_LICENSE("GPL");
229
230module_init(pcf8574_init);
231module_exit(pcf8574_exit);