Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[linux-2.6/lfs.git] / drivers / i2c / chips / pcf8575.c
blob3ea08ac0bfa33a1720f362775baf6fc37e582ef9
1 /*
2 pcf8575.c
4 About the PCF8575 chip: the PCF8575 is a 16-bit I/O expander for the I2C bus
5 produced by a.o. Philips Semiconductors.
7 Copyright (C) 2006 Michael Hennerich, Analog Devices Inc.
8 <hennerich@blackfin.uclinux.org>
9 Based on pcf8574.c.
11 Copyright (c) 2007 Bart Van Assche <bart.vanassche@gmail.com>.
12 Ported this driver from ucLinux to the mainstream Linux kernel.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/i2c.h>
32 #include <linux/slab.h> /* kzalloc() */
33 #include <linux/sysfs.h> /* sysfs_create_group() */
35 /* Addresses to scan */
36 static const unsigned short normal_i2c[] = {
37 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
38 I2C_CLIENT_END
41 /* Insmod parameters */
42 I2C_CLIENT_INSMOD;
45 /* Each client has this additional data */
46 struct pcf8575_data {
47 struct i2c_client client;
48 int write; /* last written value, or error code */
51 static int pcf8575_attach_adapter(struct i2c_adapter *adapter);
52 static int pcf8575_detect(struct i2c_adapter *adapter, int address, int kind);
53 static int pcf8575_detach_client(struct i2c_client *client);
55 /* This is the driver that will be inserted */
56 static struct i2c_driver pcf8575_driver = {
57 .driver = {
58 .owner = THIS_MODULE,
59 .name = "pcf8575",
61 .attach_adapter = pcf8575_attach_adapter,
62 .detach_client = pcf8575_detach_client,
65 /* following are the sysfs callback functions */
66 static ssize_t show_read(struct device *dev, struct device_attribute *attr,
67 char *buf)
69 struct i2c_client *client = to_i2c_client(dev);
70 u16 val;
71 u8 iopin_state[2];
73 i2c_master_recv(client, iopin_state, 2);
75 val = iopin_state[0];
76 val |= iopin_state[1] << 8;
78 return sprintf(buf, "%u\n", val);
81 static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
83 static ssize_t show_write(struct device *dev, struct device_attribute *attr,
84 char *buf)
86 struct pcf8575_data *data = dev_get_drvdata(dev);
87 if (data->write < 0)
88 return data->write;
89 return sprintf(buf, "%d\n", data->write);
92 static ssize_t set_write(struct device *dev, struct device_attribute *attr,
93 const char *buf, size_t count)
95 struct i2c_client *client = to_i2c_client(dev);
96 struct pcf8575_data *data = i2c_get_clientdata(client);
97 unsigned long val = simple_strtoul(buf, NULL, 10);
98 u8 iopin_state[2];
100 if (val > 0xffff)
101 return -EINVAL;
103 data->write = val;
105 iopin_state[0] = val & 0xFF;
106 iopin_state[1] = val >> 8;
108 i2c_master_send(client, iopin_state, 2);
110 return count;
113 static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
115 static struct attribute *pcf8575_attributes[] = {
116 &dev_attr_read.attr,
117 &dev_attr_write.attr,
118 NULL
121 static const struct attribute_group pcf8575_attr_group = {
122 .attrs = pcf8575_attributes,
126 * Real code
129 static int pcf8575_attach_adapter(struct i2c_adapter *adapter)
131 return i2c_probe(adapter, &addr_data, pcf8575_detect);
134 /* This function is called by i2c_probe */
135 static int pcf8575_detect(struct i2c_adapter *adapter, int address, int kind)
137 struct i2c_client *client;
138 struct pcf8575_data *data;
139 int err = 0;
141 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
142 goto exit;
144 /* OK. For now, we presume we have a valid client. We now create the
145 client structure, even though we cannot fill it completely yet. */
146 data = kzalloc(sizeof(struct pcf8575_data), GFP_KERNEL);
147 if (!data) {
148 err = -ENOMEM;
149 goto exit;
152 client = &data->client;
153 i2c_set_clientdata(client, data);
154 client->addr = address;
155 client->adapter = adapter;
156 client->driver = &pcf8575_driver;
157 strlcpy(client->name, "pcf8575", I2C_NAME_SIZE);
158 data->write = -EAGAIN;
160 /* This is the place to detect whether the chip at the specified
161 address really is a PCF8575 chip. However, there is no method known
162 to detect whether an I2C chip is a PCF8575 or any other I2C chip. */
164 /* Tell the I2C layer a new client has arrived */
165 err = i2c_attach_client(client);
166 if (err)
167 goto exit_free;
169 /* Register sysfs hooks */
170 err = sysfs_create_group(&client->dev.kobj, &pcf8575_attr_group);
171 if (err)
172 goto exit_detach;
174 return 0;
176 exit_detach:
177 i2c_detach_client(client);
178 exit_free:
179 kfree(data);
180 exit:
181 return err;
184 static int pcf8575_detach_client(struct i2c_client *client)
186 int err;
188 sysfs_remove_group(&client->dev.kobj, &pcf8575_attr_group);
190 err = i2c_detach_client(client);
191 if (err)
192 return err;
194 kfree(i2c_get_clientdata(client));
195 return 0;
198 static int __init pcf8575_init(void)
200 return i2c_add_driver(&pcf8575_driver);
203 static void __exit pcf8575_exit(void)
205 i2c_del_driver(&pcf8575_driver);
208 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>, "
209 "Bart Van Assche <bart.vanassche@gmail.com>");
210 MODULE_DESCRIPTION("pcf8575 driver");
211 MODULE_LICENSE("GPL");
213 module_init(pcf8575_init);
214 module_exit(pcf8575_exit);