aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/chips/pcf8574.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/chips/pcf8574.c')
-rw-r--r--drivers/i2c/chips/pcf8574.c229
1 files changed, 229 insertions, 0 deletions
diff --git a/drivers/i2c/chips/pcf8574.c b/drivers/i2c/chips/pcf8574.c
new file mode 100644
index 000000000000..48b4e22eaffe
--- /dev/null
+++ b/drivers/i2c/chips/pcf8574.c
@@ -0,0 +1,229 @@
1/*
2 pcf8574.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring
4 Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>,
5 Philip Edelbrock <phil@netroedge.com>,
6 Dan Eaton <dan.eaton@rocketlogix.com>
7 Ported to Linux 2.6 by Aurelien Jarno <aurel32@debian.org> with
8 the help of Jean Delvare <khali@linux-fr.org>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23*/
24
25/* A few notes about the PCF8574:
26
27* The PCF8574 is an 8-bit I/O expander for the I2C bus produced by
28 Philips Semiconductors. It is designed to provide a byte I2C
29 interface to up to 8 separate devices.
30
31* The PCF8574 appears as a very simple SMBus device which can be
32 read from or written to with SMBUS byte read/write accesses.
33
34 --Dan
35
36*/
37
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/i2c.h>
42#include <linux/i2c-sensor.h>
43
44/* Addresses to scan */
45static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
46 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
47 I2C_CLIENT_END };
48static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
49
50/* Insmod parameters */
51SENSORS_INSMOD_2(pcf8574, pcf8574a);
52
53/* Initial values */
54#define PCF8574_INIT 255 /* All outputs on (input mode) */
55
56/* Each client has this additional data */
57struct pcf8574_data {
58 struct i2c_client client;
59
60 u8 read, write; /* Register values */
61};
62
63static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
64static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
65static int pcf8574_detach_client(struct i2c_client *client);
66static void pcf8574_init_client(struct i2c_client *client);
67
68/* This is the driver that will be inserted */
69static struct i2c_driver pcf8574_driver = {
70 .owner = THIS_MODULE,
71 .name = "pcf8574",
72 .id = I2C_DRIVERID_PCF8574,
73 .flags = I2C_DF_NOTIFY,
74 .attach_adapter = pcf8574_attach_adapter,
75 .detach_client = pcf8574_detach_client,
76};
77
78/* following are the sysfs callback functions */
79static ssize_t show_read(struct device *dev, char *buf)
80{
81 struct i2c_client *client = to_i2c_client(dev);
82 struct pcf8574_data *data = i2c_get_clientdata(client);
83 data->read = i2c_smbus_read_byte(client);
84 return sprintf(buf, "%u\n", data->read);
85}
86
87static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
88
89static ssize_t show_write(struct device *dev, char *buf)
90{
91 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
92 return sprintf(buf, "%u\n", data->write);
93}
94
95static ssize_t set_write(struct device *dev, const char *buf,
96 size_t count)
97{
98 struct i2c_client *client = to_i2c_client(dev);
99 struct pcf8574_data *data = i2c_get_clientdata(client);
100 unsigned long val = simple_strtoul(buf, NULL, 10);
101
102 if (val > 0xff)
103 return -EINVAL;
104
105 data->write = val;
106 i2c_smbus_write_byte(client, data->write);
107 return count;
108}
109
110static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
111
112/*
113 * Real code
114 */
115
116static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
117{
118 return i2c_detect(adapter, &addr_data, pcf8574_detect);
119}
120
121/* This function is called by i2c_detect */
122int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
123{
124 struct i2c_client *new_client;
125 struct pcf8574_data *data;
126 int err = 0;
127 const char *client_name = "";
128
129 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
130 goto exit;
131
132 /* OK. For now, we presume we have a valid client. We now create the
133 client structure, even though we cannot fill it completely yet. */
134 if (!(data = kmalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
135 err = -ENOMEM;
136 goto exit;
137 }
138 memset(data, 0, sizeof(struct pcf8574_data));
139
140 new_client = &data->client;
141 i2c_set_clientdata(new_client, data);
142 new_client->addr = address;
143 new_client->adapter = adapter;
144 new_client->driver = &pcf8574_driver;
145 new_client->flags = 0;
146
147 /* Now, we would do the remaining detection. But the PCF8574 is plainly
148 impossible to detect! Stupid chip. */
149
150 /* Determine the chip type */
151 if (kind <= 0) {
152 if (address >= 0x38 && address <= 0x3f)
153 kind = pcf8574a;
154 else
155 kind = pcf8574;
156 }
157
158 if (kind == pcf8574a)
159 client_name = "pcf8574a";
160 else
161 client_name = "pcf8574";
162
163 /* Fill in the remaining client fields and put it into the global list */
164 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
165
166 /* Tell the I2C layer a new client has arrived */
167 if ((err = i2c_attach_client(new_client)))
168 goto exit_free;
169
170 /* Initialize the PCF8574 chip */
171 pcf8574_init_client(new_client);
172
173 /* Register sysfs hooks */
174 device_create_file(&new_client->dev, &dev_attr_read);
175 device_create_file(&new_client->dev, &dev_attr_write);
176 return 0;
177
178/* OK, this is not exactly good programming practice, usually. But it is
179 very code-efficient in this case. */
180
181 exit_free:
182 kfree(data);
183 exit:
184 return err;
185}
186
187static int pcf8574_detach_client(struct i2c_client *client)
188{
189 int err;
190
191 if ((err = i2c_detach_client(client))) {
192 dev_err(&client->dev,
193 "Client deregistration failed, client not detached.\n");
194 return err;
195 }
196
197 kfree(i2c_get_clientdata(client));
198 return 0;
199}
200
201/* Called when we have found a new PCF8574. */
202static void pcf8574_init_client(struct i2c_client *client)
203{
204 struct pcf8574_data *data = i2c_get_clientdata(client);
205 data->write = PCF8574_INIT;
206 i2c_smbus_write_byte(client, data->write);
207}
208
209static int __init pcf8574_init(void)
210{
211 return i2c_add_driver(&pcf8574_driver);
212}
213
214static void __exit pcf8574_exit(void)
215{
216 i2c_del_driver(&pcf8574_driver);
217}
218
219
220MODULE_AUTHOR
221 ("Frodo Looijaard <frodol@dds.nl>, "
222 "Philip Edelbrock <phil@netroedge.com>, "
223 "Dan Eaton <dan.eaton@rocketlogix.com> "
224 "and Aurelien Jarno <aurelien@aurel32.net>");
225MODULE_DESCRIPTION("PCF8574 driver");
226MODULE_LICENSE("GPL");
227
228module_init(pcf8574_init);
229module_exit(pcf8574_exit);