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>
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: none, device can't be detected */
36 static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
38 /* Insmod parameters */
42 /* Each client has this additional data */
44 int write; /* last written value, or error code */
47 /* following are the sysfs callback functions */
48 static ssize_t show_read(struct device *dev, struct device_attribute *attr,
51 struct i2c_client *client = to_i2c_client(dev);
55 i2c_master_recv(client, iopin_state, 2);
58 val |= iopin_state[1] << 8;
60 return sprintf(buf, "%u\n", val);
63 static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
65 static ssize_t show_write(struct device *dev, struct device_attribute *attr,
68 struct pcf8575_data *data = dev_get_drvdata(dev);
71 return sprintf(buf, "%d\n", data->write);
74 static ssize_t set_write(struct device *dev, struct device_attribute *attr,
75 const char *buf, size_t count)
77 struct i2c_client *client = to_i2c_client(dev);
78 struct pcf8575_data *data = i2c_get_clientdata(client);
79 unsigned long val = simple_strtoul(buf, NULL, 10);
87 iopin_state[0] = val & 0xFF;
88 iopin_state[1] = val >> 8;
90 i2c_master_send(client, iopin_state, 2);
95 static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
97 static struct attribute *pcf8575_attributes[] = {
103 static const struct attribute_group pcf8575_attr_group = {
104 .attrs = pcf8575_attributes,
111 /* Return 0 if detection is successful, -ENODEV otherwise */
112 static int pcf8575_detect(struct i2c_client *client, int kind,
113 struct i2c_board_info *info)
115 struct i2c_adapter *adapter = client->adapter;
117 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
120 /* This is the place to detect whether the chip at the specified
121 address really is a PCF8575 chip. However, there is no method known
122 to detect whether an I2C chip is a PCF8575 or any other I2C chip. */
124 strlcpy(info->type, "pcf8575", I2C_NAME_SIZE);
129 static int pcf8575_probe(struct i2c_client *client,
130 const struct i2c_device_id *id)
132 struct pcf8575_data *data;
135 data = kzalloc(sizeof(struct pcf8575_data), GFP_KERNEL);
141 i2c_set_clientdata(client, data);
142 data->write = -EAGAIN;
144 /* Register sysfs hooks */
145 err = sysfs_create_group(&client->dev.kobj, &pcf8575_attr_group);
157 static int pcf8575_remove(struct i2c_client *client)
159 sysfs_remove_group(&client->dev.kobj, &pcf8575_attr_group);
160 kfree(i2c_get_clientdata(client));
164 static const struct i2c_device_id pcf8575_id[] = {
169 static struct i2c_driver pcf8575_driver = {
171 .owner = THIS_MODULE,
174 .probe = pcf8575_probe,
175 .remove = pcf8575_remove,
176 .id_table = pcf8575_id,
178 .detect = pcf8575_detect,
179 .address_data = &addr_data,
182 static int __init pcf8575_init(void)
184 return i2c_add_driver(&pcf8575_driver);
187 static void __exit pcf8575_exit(void)
189 i2c_del_driver(&pcf8575_driver);
192 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>, "
193 "Bart Van Assche <bart.vanassche@gmail.com>");
194 MODULE_DESCRIPTION("pcf8575 driver");
195 MODULE_LICENSE("GPL");
197 module_init(pcf8575_init);
198 module_exit(pcf8575_exit);