blob: b3b830ccf209ec7e0b1aac96f14bc8d5e8797943 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* Addresses to scan */
Jean Delvare2cdddeb2008-01-27 18:14:47 +010044static const unsigned short normal_i2c[] = {
45 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
46 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
47 I2C_CLIENT_END
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020051I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Each client has this additional data */
54struct pcf8574_data {
55 struct i2c_client client;
56
Jean Delvare553515e2007-10-13 23:56:31 +020057 int write; /* Remember last written value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058};
59
60static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
61static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
62static int pcf8574_detach_client(struct i2c_client *client);
63static void pcf8574_init_client(struct i2c_client *client);
64
65/* This is the driver that will be inserted */
66static struct i2c_driver pcf8574_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010067 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +010068 .name = "pcf8574",
69 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .id = I2C_DRIVERID_PCF8574,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .attach_adapter = pcf8574_attach_adapter,
72 .detach_client = pcf8574_detach_client,
73};
74
75/* following are the sysfs callback functions */
Yani Ioannoua5099cf2005-05-17 06:42:25 -040076static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb071cb2005-06-04 13:17:43 +020079 return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
83
Yani Ioannoua5099cf2005-05-17 06:42:25 -040084static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
Jean Delvare553515e2007-10-13 23:56:31 +020087
88 if (data->write < 0)
89 return data->write;
90
91 return sprintf(buf, "%d\n", data->write);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Yani Ioannoua5099cf2005-05-17 06:42:25 -040094static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 size_t count)
96{
97 struct i2c_client *client = to_i2c_client(dev);
98 struct pcf8574_data *data = i2c_get_clientdata(client);
99 unsigned long val = simple_strtoul(buf, NULL, 10);
100
101 if (val > 0xff)
102 return -EINVAL;
103
104 data->write = val;
105 i2c_smbus_write_byte(client, data->write);
106 return count;
107}
108
109static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
110
Jean Delvare7d9db672006-09-03 22:20:24 +0200111static struct attribute *pcf8574_attributes[] = {
112 &dev_attr_read.attr,
113 &dev_attr_write.attr,
114 NULL
115};
116
117static const struct attribute_group pcf8574_attr_group = {
118 .attrs = pcf8574_attributes,
119};
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
122 * Real code
123 */
124
125static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
126{
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200127 return i2c_probe(adapter, &addr_data, pcf8574_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200130/* This function is called by i2c_probe */
Ben Dooks6344a8e2005-10-26 21:09:41 +0200131static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct i2c_client *new_client;
134 struct pcf8574_data *data;
135 int err = 0;
136 const char *client_name = "";
137
138 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
139 goto exit;
140
141 /* OK. For now, we presume we have a valid client. We now create the
142 client structure, even though we cannot fill it completely yet. */
Deepak Saxena5263ebb2005-10-17 23:09:43 +0200143 if (!(data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 err = -ENOMEM;
145 goto exit;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 new_client = &data->client;
149 i2c_set_clientdata(new_client, data);
150 new_client->addr = address;
151 new_client->adapter = adapter;
152 new_client->driver = &pcf8574_driver;
153 new_client->flags = 0;
154
155 /* Now, we would do the remaining detection. But the PCF8574 is plainly
156 impossible to detect! Stupid chip. */
157
158 /* Determine the chip type */
159 if (kind <= 0) {
160 if (address >= 0x38 && address <= 0x3f)
161 kind = pcf8574a;
162 else
163 kind = pcf8574;
164 }
165
166 if (kind == pcf8574a)
167 client_name = "pcf8574a";
168 else
169 client_name = "pcf8574";
170
171 /* Fill in the remaining client fields and put it into the global list */
172 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
173
174 /* Tell the I2C layer a new client has arrived */
175 if ((err = i2c_attach_client(new_client)))
176 goto exit_free;
177
178 /* Initialize the PCF8574 chip */
179 pcf8574_init_client(new_client);
180
181 /* Register sysfs hooks */
Jean Delvare7d9db672006-09-03 22:20:24 +0200182 err = sysfs_create_group(&new_client->dev.kobj, &pcf8574_attr_group);
183 if (err)
184 goto exit_detach;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
186
Jean Delvare7d9db672006-09-03 22:20:24 +0200187 exit_detach:
188 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 exit_free:
190 kfree(data);
191 exit:
192 return err;
193}
194
195static int pcf8574_detach_client(struct i2c_client *client)
196{
197 int err;
198
Jean Delvare7d9db672006-09-03 22:20:24 +0200199 sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group);
200
Jean Delvare7bef5592005-07-27 22:14:49 +0200201 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 kfree(i2c_get_clientdata(client));
205 return 0;
206}
207
208/* Called when we have found a new PCF8574. */
209static void pcf8574_init_client(struct i2c_client *client)
210{
211 struct pcf8574_data *data = i2c_get_clientdata(client);
Jean Delvare553515e2007-10-13 23:56:31 +0200212 data->write = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215static int __init pcf8574_init(void)
216{
217 return i2c_add_driver(&pcf8574_driver);
218}
219
220static void __exit pcf8574_exit(void)
221{
222 i2c_del_driver(&pcf8574_driver);
223}
224
225
226MODULE_AUTHOR
227 ("Frodo Looijaard <frodol@dds.nl>, "
228 "Philip Edelbrock <phil@netroedge.com>, "
229 "Dan Eaton <dan.eaton@rocketlogix.com> "
230 "and Aurelien Jarno <aurelien@aurel32.net>");
231MODULE_DESCRIPTION("PCF8574 driver");
232MODULE_LICENSE("GPL");
233
234module_init(pcf8574_init);
235module_exit(pcf8574_exit);