Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / i2c / chips / pcf8574.c
blobe5b31329b56e146e1022be115c0d7e0cbdc88ede
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>
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.
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.
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.
25 /* A few notes about the PCF8574:
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.
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.
34 --Dan
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/i2c.h>
43 /* Addresses to scan */
44 static 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
50 /* Insmod parameters */
51 I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a);
53 /* Each client has this additional data */
54 struct pcf8574_data {
55 struct i2c_client client;
57 int write; /* Remember last written value */
60 static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
61 static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
62 static int pcf8574_detach_client(struct i2c_client *client);
63 static void pcf8574_init_client(struct i2c_client *client);
65 /* This is the driver that will be inserted */
66 static struct i2c_driver pcf8574_driver = {
67 .driver = {
68 .name = "pcf8574",
70 .attach_adapter = pcf8574_attach_adapter,
71 .detach_client = pcf8574_detach_client,
74 /* following are the sysfs callback functions */
75 static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
77 struct i2c_client *client = to_i2c_client(dev);
78 return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
81 static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
83 static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
85 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
87 if (data->write < 0)
88 return data->write;
90 return sprintf(buf, "%d\n", data->write);
93 static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
94 size_t count)
96 struct i2c_client *client = to_i2c_client(dev);
97 struct pcf8574_data *data = i2c_get_clientdata(client);
98 unsigned long val = simple_strtoul(buf, NULL, 10);
100 if (val > 0xff)
101 return -EINVAL;
103 data->write = val;
104 i2c_smbus_write_byte(client, data->write);
105 return count;
108 static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
110 static struct attribute *pcf8574_attributes[] = {
111 &dev_attr_read.attr,
112 &dev_attr_write.attr,
113 NULL
116 static const struct attribute_group pcf8574_attr_group = {
117 .attrs = pcf8574_attributes,
121 * Real code
124 static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
126 return i2c_probe(adapter, &addr_data, pcf8574_detect);
129 /* This function is called by i2c_probe */
130 static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
132 struct i2c_client *new_client;
133 struct pcf8574_data *data;
134 int err = 0;
135 const char *client_name = "";
137 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
138 goto exit;
140 /* OK. For now, we presume we have a valid client. We now create the
141 client structure, even though we cannot fill it completely yet. */
142 if (!(data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
143 err = -ENOMEM;
144 goto exit;
147 new_client = &data->client;
148 i2c_set_clientdata(new_client, data);
149 new_client->addr = address;
150 new_client->adapter = adapter;
151 new_client->driver = &pcf8574_driver;
152 new_client->flags = 0;
154 /* Now, we would do the remaining detection. But the PCF8574 is plainly
155 impossible to detect! Stupid chip. */
157 /* Determine the chip type */
158 if (kind <= 0) {
159 if (address >= 0x38 && address <= 0x3f)
160 kind = pcf8574a;
161 else
162 kind = pcf8574;
165 if (kind == pcf8574a)
166 client_name = "pcf8574a";
167 else
168 client_name = "pcf8574";
170 /* Fill in the remaining client fields and put it into the global list */
171 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
173 /* Tell the I2C layer a new client has arrived */
174 if ((err = i2c_attach_client(new_client)))
175 goto exit_free;
177 /* Initialize the PCF8574 chip */
178 pcf8574_init_client(new_client);
180 /* Register sysfs hooks */
181 err = sysfs_create_group(&new_client->dev.kobj, &pcf8574_attr_group);
182 if (err)
183 goto exit_detach;
184 return 0;
186 exit_detach:
187 i2c_detach_client(new_client);
188 exit_free:
189 kfree(data);
190 exit:
191 return err;
194 static int pcf8574_detach_client(struct i2c_client *client)
196 int err;
198 sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group);
200 if ((err = i2c_detach_client(client)))
201 return err;
203 kfree(i2c_get_clientdata(client));
204 return 0;
207 /* Called when we have found a new PCF8574. */
208 static void pcf8574_init_client(struct i2c_client *client)
210 struct pcf8574_data *data = i2c_get_clientdata(client);
211 data->write = -EAGAIN;
214 static int __init pcf8574_init(void)
216 return i2c_add_driver(&pcf8574_driver);
219 static void __exit pcf8574_exit(void)
221 i2c_del_driver(&pcf8574_driver);
225 MODULE_AUTHOR
226 ("Frodo Looijaard <frodol@dds.nl>, "
227 "Philip Edelbrock <phil@netroedge.com>, "
228 "Dan Eaton <dan.eaton@rocketlogix.com> "
229 "and Aurelien Jarno <aurelien@aurel32.net>");
230 MODULE_DESCRIPTION("PCF8574 driver");
231 MODULE_LICENSE("GPL");
233 module_init(pcf8574_init);
234 module_exit(pcf8574_exit);