Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* Addresses to scan */ |
| 44 | static 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 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | /* Insmod parameters */ |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 49 | I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | /* Initial values */ |
| 52 | #define PCF8574_INIT 255 /* All outputs on (input mode) */ |
| 53 | |
| 54 | /* Each client has this additional data */ |
| 55 | struct pcf8574_data { |
| 56 | struct i2c_client client; |
| 57 | |
Jean Delvare | eb071cb | 2005-06-04 13:17:43 +0200 | [diff] [blame] | 58 | u8 write; /* Remember last written value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static int pcf8574_attach_adapter(struct i2c_adapter *adapter); |
| 62 | static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind); |
| 63 | static int pcf8574_detach_client(struct i2c_client *client); |
| 64 | static void pcf8574_init_client(struct i2c_client *client); |
| 65 | |
| 66 | /* This is the driver that will be inserted */ |
| 67 | static struct i2c_driver pcf8574_driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 68 | .driver = { |
Laurent Riffard | a9718b0 | 2005-11-26 20:36:00 +0100 | [diff] [blame] | 69 | .name = "pcf8574", |
| 70 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | .id = I2C_DRIVERID_PCF8574, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | .attach_adapter = pcf8574_attach_adapter, |
| 73 | .detach_client = pcf8574_detach_client, |
| 74 | }; |
| 75 | |
| 76 | /* following are the sysfs callback functions */ |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 77 | static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
| 79 | struct i2c_client *client = to_i2c_client(dev); |
Jean Delvare | eb071cb | 2005-06-04 13:17:43 +0200 | [diff] [blame] | 80 | return sprintf(buf, "%u\n", i2c_smbus_read_byte(client)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static DEVICE_ATTR(read, S_IRUGO, show_read, NULL); |
| 84 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 85 | static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev)); |
| 88 | return sprintf(buf, "%u\n", data->write); |
| 89 | } |
| 90 | |
Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 91 | static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 106 | static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write); |
| 107 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 108 | static struct attribute *pcf8574_attributes[] = { |
| 109 | &dev_attr_read.attr, |
| 110 | &dev_attr_write.attr, |
| 111 | NULL |
| 112 | }; |
| 113 | |
| 114 | static const struct attribute_group pcf8574_attr_group = { |
| 115 | .attrs = pcf8574_attributes, |
| 116 | }; |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | /* |
| 119 | * Real code |
| 120 | */ |
| 121 | |
| 122 | static int pcf8574_attach_adapter(struct i2c_adapter *adapter) |
| 123 | { |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 124 | return i2c_probe(adapter, &addr_data, pcf8574_detect); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 127 | /* This function is called by i2c_probe */ |
Ben Dooks | 6344a8e | 2005-10-26 21:09:41 +0200 | [diff] [blame] | 128 | static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
| 130 | struct i2c_client *new_client; |
| 131 | struct pcf8574_data *data; |
| 132 | int err = 0; |
| 133 | const char *client_name = ""; |
| 134 | |
| 135 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) |
| 136 | goto exit; |
| 137 | |
| 138 | /* OK. For now, we presume we have a valid client. We now create the |
| 139 | client structure, even though we cannot fill it completely yet. */ |
Deepak Saxena | 5263ebb | 2005-10-17 23:09:43 +0200 | [diff] [blame] | 140 | if (!(data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | err = -ENOMEM; |
| 142 | goto exit; |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | new_client = &data->client; |
| 146 | i2c_set_clientdata(new_client, data); |
| 147 | new_client->addr = address; |
| 148 | new_client->adapter = adapter; |
| 149 | new_client->driver = &pcf8574_driver; |
| 150 | new_client->flags = 0; |
| 151 | |
| 152 | /* Now, we would do the remaining detection. But the PCF8574 is plainly |
| 153 | impossible to detect! Stupid chip. */ |
| 154 | |
| 155 | /* Determine the chip type */ |
| 156 | if (kind <= 0) { |
| 157 | if (address >= 0x38 && address <= 0x3f) |
| 158 | kind = pcf8574a; |
| 159 | else |
| 160 | kind = pcf8574; |
| 161 | } |
| 162 | |
| 163 | if (kind == pcf8574a) |
| 164 | client_name = "pcf8574a"; |
| 165 | else |
| 166 | client_name = "pcf8574"; |
| 167 | |
| 168 | /* Fill in the remaining client fields and put it into the global list */ |
| 169 | strlcpy(new_client->name, client_name, I2C_NAME_SIZE); |
| 170 | |
| 171 | /* Tell the I2C layer a new client has arrived */ |
| 172 | if ((err = i2c_attach_client(new_client))) |
| 173 | goto exit_free; |
| 174 | |
| 175 | /* Initialize the PCF8574 chip */ |
| 176 | pcf8574_init_client(new_client); |
| 177 | |
| 178 | /* Register sysfs hooks */ |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 179 | err = sysfs_create_group(&new_client->dev.kobj, &pcf8574_attr_group); |
| 180 | if (err) |
| 181 | goto exit_detach; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return 0; |
| 183 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 184 | exit_detach: |
| 185 | i2c_detach_client(new_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | exit_free: |
| 187 | kfree(data); |
| 188 | exit: |
| 189 | return err; |
| 190 | } |
| 191 | |
| 192 | static int pcf8574_detach_client(struct i2c_client *client) |
| 193 | { |
| 194 | int err; |
| 195 | |
Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 196 | sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group); |
| 197 | |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 198 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | kfree(i2c_get_clientdata(client)); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* Called when we have found a new PCF8574. */ |
| 206 | static void pcf8574_init_client(struct i2c_client *client) |
| 207 | { |
| 208 | struct pcf8574_data *data = i2c_get_clientdata(client); |
| 209 | data->write = PCF8574_INIT; |
| 210 | i2c_smbus_write_byte(client, data->write); |
| 211 | } |
| 212 | |
| 213 | static int __init pcf8574_init(void) |
| 214 | { |
| 215 | return i2c_add_driver(&pcf8574_driver); |
| 216 | } |
| 217 | |
| 218 | static void __exit pcf8574_exit(void) |
| 219 | { |
| 220 | i2c_del_driver(&pcf8574_driver); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | MODULE_AUTHOR |
| 225 | ("Frodo Looijaard <frodol@dds.nl>, " |
| 226 | "Philip Edelbrock <phil@netroedge.com>, " |
| 227 | "Dan Eaton <dan.eaton@rocketlogix.com> " |
| 228 | "and Aurelien Jarno <aurelien@aurel32.net>"); |
| 229 | MODULE_DESCRIPTION("PCF8574 driver"); |
| 230 | MODULE_LICENSE("GPL"); |
| 231 | |
| 232 | module_init(pcf8574_init); |
| 233 | module_exit(pcf8574_exit); |