]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/i2c/chips/pcf8574.c
[PATCH] hwmon: hwmon vs i2c, second round (06/11)
[mirror_ubuntu-zesty-kernel.git] / drivers / i2c / chips / pcf8574.c
CommitLineData
1da177e4
LT
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>
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>
1da177e4
LT
42
43/* Addresses to scan */
44static unsigned short normal_i2c[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
45 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
46 I2C_CLIENT_END };
1da177e4
LT
47
48/* Insmod parameters */
f4b50261 49I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a);
1da177e4
LT
50
51/* Initial values */
52#define PCF8574_INIT 255 /* All outputs on (input mode) */
53
54/* Each client has this additional data */
55struct pcf8574_data {
56 struct i2c_client client;
57
eb071cbb 58 u8 write; /* Remember last written value */
1da177e4
LT
59};
60
61static int pcf8574_attach_adapter(struct i2c_adapter *adapter);
62static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind);
63static int pcf8574_detach_client(struct i2c_client *client);
64static void pcf8574_init_client(struct i2c_client *client);
65
66/* This is the driver that will be inserted */
67static struct i2c_driver pcf8574_driver = {
68 .owner = THIS_MODULE,
69 .name = "pcf8574",
70 .id = I2C_DRIVERID_PCF8574,
71 .flags = I2C_DF_NOTIFY,
72 .attach_adapter = pcf8574_attach_adapter,
73 .detach_client = pcf8574_detach_client,
74};
75
76/* following are the sysfs callback functions */
a5099cfc 77static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
78{
79 struct i2c_client *client = to_i2c_client(dev);
eb071cbb 80 return sprintf(buf, "%u\n", i2c_smbus_read_byte(client));
1da177e4
LT
81}
82
83static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
84
a5099cfc 85static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
86{
87 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev));
88 return sprintf(buf, "%u\n", data->write);
89}
90
a5099cfc 91static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
1da177e4
LT
92 size_t count)
93{
94 struct i2c_client *client = to_i2c_client(dev);
95 struct pcf8574_data *data = i2c_get_clientdata(client);
96 unsigned long val = simple_strtoul(buf, NULL, 10);
97
98 if (val > 0xff)
99 return -EINVAL;
100
101 data->write = val;
102 i2c_smbus_write_byte(client, data->write);
103 return count;
104}
105
106static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
107
108/*
109 * Real code
110 */
111
112static int pcf8574_attach_adapter(struct i2c_adapter *adapter)
113{
2ed2dc3c 114 return i2c_probe(adapter, &addr_data, pcf8574_detect);
1da177e4
LT
115}
116
2ed2dc3c 117/* This function is called by i2c_probe */
1da177e4
LT
118int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind)
119{
120 struct i2c_client *new_client;
121 struct pcf8574_data *data;
122 int err = 0;
123 const char *client_name = "";
124
125 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
126 goto exit;
127
128 /* OK. For now, we presume we have a valid client. We now create the
129 client structure, even though we cannot fill it completely yet. */
130 if (!(data = kmalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) {
131 err = -ENOMEM;
132 goto exit;
133 }
134 memset(data, 0, sizeof(struct pcf8574_data));
135
136 new_client = &data->client;
137 i2c_set_clientdata(new_client, data);
138 new_client->addr = address;
139 new_client->adapter = adapter;
140 new_client->driver = &pcf8574_driver;
141 new_client->flags = 0;
142
143 /* Now, we would do the remaining detection. But the PCF8574 is plainly
144 impossible to detect! Stupid chip. */
145
146 /* Determine the chip type */
147 if (kind <= 0) {
148 if (address >= 0x38 && address <= 0x3f)
149 kind = pcf8574a;
150 else
151 kind = pcf8574;
152 }
153
154 if (kind == pcf8574a)
155 client_name = "pcf8574a";
156 else
157 client_name = "pcf8574";
158
159 /* Fill in the remaining client fields and put it into the global list */
160 strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
161
162 /* Tell the I2C layer a new client has arrived */
163 if ((err = i2c_attach_client(new_client)))
164 goto exit_free;
165
166 /* Initialize the PCF8574 chip */
167 pcf8574_init_client(new_client);
168
169 /* Register sysfs hooks */
170 device_create_file(&new_client->dev, &dev_attr_read);
171 device_create_file(&new_client->dev, &dev_attr_write);
172 return 0;
173
174/* OK, this is not exactly good programming practice, usually. But it is
175 very code-efficient in this case. */
176
177 exit_free:
178 kfree(data);
179 exit:
180 return err;
181}
182
183static int pcf8574_detach_client(struct i2c_client *client)
184{
185 int err;
186
7bef5594 187 if ((err = i2c_detach_client(client)))
1da177e4 188 return err;
1da177e4
LT
189
190 kfree(i2c_get_clientdata(client));
191 return 0;
192}
193
194/* Called when we have found a new PCF8574. */
195static void pcf8574_init_client(struct i2c_client *client)
196{
197 struct pcf8574_data *data = i2c_get_clientdata(client);
198 data->write = PCF8574_INIT;
199 i2c_smbus_write_byte(client, data->write);
200}
201
202static int __init pcf8574_init(void)
203{
204 return i2c_add_driver(&pcf8574_driver);
205}
206
207static void __exit pcf8574_exit(void)
208{
209 i2c_del_driver(&pcf8574_driver);
210}
211
212
213MODULE_AUTHOR
214 ("Frodo Looijaard <frodol@dds.nl>, "
215 "Philip Edelbrock <phil@netroedge.com>, "
216 "Dan Eaton <dan.eaton@rocketlogix.com> "
217 "and Aurelien Jarno <aurelien@aurel32.net>");
218MODULE_DESCRIPTION("PCF8574 driver");
219MODULE_LICENSE("GPL");
220
221module_init(pcf8574_init);
222module_exit(pcf8574_exit);