blob: a109dfd3dbe7ec322a99e37f55b117e1a17221dd [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>
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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Insmod parameters */
50SENSORS_INSMOD_2(pcf8574, pcf8574a);
51
52/* Initial values */
53#define PCF8574_INIT 255 /* All outputs on (input mode) */
54
55/* Each client has this additional data */
56struct pcf8574_data {
57 struct i2c_client client;
58
Jean Delvareeb071cb2005-06-04 13:17:43 +020059 u8 write; /* Remember last written value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
62static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
63static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
64static int pcf8574_detach_client(struct i2c_client *client);
65static void pcf8574_init_client(struct i2c_client *client);
66
67/* This is the driver that will be inserted */
68static struct i2c_driver pcf8574_driver = {
69 .owner = THIS_MODULE,
70 .name = "pcf8574",
71 .id = I2C_DRIVERID_PCF8574,
72 .flags = I2C_DF_NOTIFY,
73 .attach_adapter = pcf8574_attach_adapter,
74 .detach_client = pcf8574_detach_client,
75};
76
77/* following are the sysfs callback functions */
Yani Ioannoua5099cf2005-05-17 06:42:25 -040078static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb071cb2005-06-04 13:17:43 +020081 return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
85
Yani Ioannoua5099cf2005-05-17 06:42:25 -040086static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
89 return sprintf(buf, "%u\n", data->write);
90}
91
Yani Ioannoua5099cf2005-05-17 06:42:25 -040092static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 size_t count)
94{
95 struct i2c_client *client = to_i2c_client(dev);
96 struct pcf8574_data *data = i2c_get_clientdata(client);
97 unsigned long val = simple_strtoul(buf, NULL, 10);
98
99 if (val > 0xff)
100 return -EINVAL;
101
102 data->write = val;
103 i2c_smbus_write_byte(client, data->write);
104 return count;
105}
106
107static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
108
109/*
110 * Real code
111 */
112
113static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
114{
115 return i2c_detect(adapter, &addr_data, pcf8574_detect);
116}
117
118/* This function is called by i2c_detect */
119int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
120{
121 struct i2c_client *new_client;
122 struct pcf8574_data *data;
123 int err = 0;
124 const char *client_name = "";
125
126 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
127 goto exit;
128
129 /* OK. For now, we presume we have a valid client. We now create the
130 client structure, even though we cannot fill it completely yet. */
131 if (!(data = kmalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
132 err = -ENOMEM;
133 goto exit;
134 }
135 memset(data, 0, sizeof(struct pcf8574_data));
136
137 new_client = &data->client;
138 i2c_set_clientdata(new_client, data);
139 new_client->addr = address;
140 new_client->adapter = adapter;
141 new_client->driver = &pcf8574_driver;
142 new_client->flags = 0;
143
144 /* Now, we would do the remaining detection. But the PCF8574 is plainly
145 impossible to detect! Stupid chip. */
146
147 /* Determine the chip type */
148 if (kind <= 0) {
149 if (address >= 0x38 && address <= 0x3f)
150 kind = pcf8574a;
151 else
152 kind = pcf8574;
153 }
154
155 if (kind == pcf8574a)
156 client_name = "pcf8574a";
157 else
158 client_name = "pcf8574";
159
160 /* Fill in the remaining client fields and put it into the global list */
161 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
162
163 /* Tell the I2C layer a new client has arrived */
164 if ((err = i2c_attach_client(new_client)))
165 goto exit_free;
166
167 /* Initialize the PCF8574 chip */
168 pcf8574_init_client(new_client);
169
170 /* Register sysfs hooks */
171 device_create_file(&new_client->dev, &dev_attr_read);
172 device_create_file(&new_client->dev, &dev_attr_write);
173 return 0;
174
175/* OK, this is not exactly good programming practice, usually. But it is
176 very code-efficient in this case. */
177
178 exit_free:
179 kfree(data);
180 exit:
181 return err;
182}
183
184static int pcf8574_detach_client(struct i2c_client *client)
185{
186 int err;
187
Jean Delvare7bef5592005-07-27 22:14:49 +0200188 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 kfree(i2c_get_clientdata(client));
192 return 0;
193}
194
195/* Called when we have found a new PCF8574. */
196static void pcf8574_init_client(struct i2c_client *client)
197{
198 struct pcf8574_data *data = i2c_get_clientdata(client);
199 data->write = PCF8574_INIT;
200 i2c_smbus_write_byte(client, data->write);
201}
202
203static int __init pcf8574_init(void)
204{
205 return i2c_add_driver(&pcf8574_driver);
206}
207
208static void __exit pcf8574_exit(void)
209{
210 i2c_del_driver(&pcf8574_driver);
211}
212
213
214MODULE_AUTHOR
215 ("Frodo Looijaard <frodol@dds.nl>, "
216 "Philip Edelbrock <phil@netroedge.com>, "
217 "Dan Eaton <dan.eaton@rocketlogix.com> "
218 "and Aurelien Jarno <aurelien@aurel32.net>");
219MODULE_DESCRIPTION("PCF8574 driver");
220MODULE_LICENSE("GPL");
221
222module_init(pcf8574_init);
223module_exit(pcf8574_exit);