| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | Copyright (c) 2000  Frodo Looijaard <frodol@dds.nl>, | 
|  | 3 | Philip Edelbrock <phil@netroedge.com>, | 
|  | 4 | Dan Eaton <dan.eaton@rocketlogix.com> | 
|  | 5 | Ported to Linux 2.6 by Aurelien Jarno <aurel32@debian.org> with | 
|  | 6 | the help of Jean Delvare <khali@linux-fr.org> | 
|  | 7 |  | 
|  | 8 | This program is free software; you can redistribute it and/or modify | 
|  | 9 | it under the terms of the GNU General Public License as published by | 
|  | 10 | the Free Software Foundation; either version 2 of the License, or | 
|  | 11 | (at your option) any later version. | 
|  | 12 |  | 
|  | 13 | This program is distributed in the hope that it will be useful, | 
|  | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 16 | GNU General Public License for more details. | 
|  | 17 |  | 
|  | 18 | You should have received a copy of the GNU General Public License | 
|  | 19 | along with this program; if not, write to the Free Software | 
|  | 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|  | 21 | */ | 
|  | 22 |  | 
|  | 23 | /* A few notes about the PCF8574: | 
|  | 24 |  | 
|  | 25 | * The PCF8574 is an 8-bit I/O expander for the I2C bus produced by | 
|  | 26 | Philips Semiconductors.  It is designed to provide a byte I2C | 
|  | 27 | interface to up to 8 separate devices. | 
|  | 28 |  | 
|  | 29 | * The PCF8574 appears as a very simple SMBus device which can be | 
|  | 30 | read from or written to with SMBUS byte read/write accesses. | 
|  | 31 |  | 
|  | 32 | --Dan | 
|  | 33 |  | 
|  | 34 | */ | 
|  | 35 |  | 
|  | 36 | #include <linux/module.h> | 
|  | 37 | #include <linux/init.h> | 
|  | 38 | #include <linux/slab.h> | 
|  | 39 | #include <linux/i2c.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 41 | /* Addresses to scan: none, device can't be detected */ | 
|  | 42 | static const unsigned short normal_i2c[] = { I2C_CLIENT_END }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | /* Insmod parameters */ | 
| Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 45 | I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* Each client has this additional data */ | 
|  | 48 | struct pcf8574_data { | 
| Jean Delvare | 553515e | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 49 | int write;			/* Remember last written value */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | }; | 
|  | 51 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static void pcf8574_init_client(struct i2c_client *client); | 
|  | 53 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | /* following are the sysfs callback functions */ | 
| Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 55 | 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] | 56 | { | 
|  | 57 | struct i2c_client *client = to_i2c_client(dev); | 
| Jean Delvare | eb071cb | 2005-06-04 13:17:43 +0200 | [diff] [blame] | 58 | return sprintf(buf, "%u\n", i2c_smbus_read_byte(client)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
|  | 61 | static DEVICE_ATTR(read, S_IRUGO, show_read, NULL); | 
|  | 62 |  | 
| Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 63 | 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] | 64 | { | 
|  | 65 | struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev)); | 
| Jean Delvare | 553515e | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 66 |  | 
|  | 67 | if (data->write < 0) | 
|  | 68 | return data->write; | 
|  | 69 |  | 
|  | 70 | return sprintf(buf, "%d\n", data->write); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Yani Ioannou | a5099cf | 2005-05-17 06:42:25 -0400 | [diff] [blame] | 73 | 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] | 74 | size_t count) | 
|  | 75 | { | 
|  | 76 | struct i2c_client *client = to_i2c_client(dev); | 
|  | 77 | struct pcf8574_data *data = i2c_get_clientdata(client); | 
|  | 78 | unsigned long val = simple_strtoul(buf, NULL, 10); | 
|  | 79 |  | 
|  | 80 | if (val > 0xff) | 
|  | 81 | return -EINVAL; | 
|  | 82 |  | 
|  | 83 | data->write = val; | 
|  | 84 | i2c_smbus_write_byte(client, data->write); | 
|  | 85 | return count; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write); | 
|  | 89 |  | 
| Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 90 | static struct attribute *pcf8574_attributes[] = { | 
|  | 91 | &dev_attr_read.attr, | 
|  | 92 | &dev_attr_write.attr, | 
|  | 93 | NULL | 
|  | 94 | }; | 
|  | 95 |  | 
|  | 96 | static const struct attribute_group pcf8574_attr_group = { | 
|  | 97 | .attrs = pcf8574_attributes, | 
|  | 98 | }; | 
|  | 99 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | /* | 
|  | 101 | * Real code | 
|  | 102 | */ | 
|  | 103 |  | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 104 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
|  | 105 | static int pcf8574_detect(struct i2c_client *client, int kind, | 
|  | 106 | struct i2c_board_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 108 | struct i2c_adapter *adapter = client->adapter; | 
|  | 109 | const char *client_name; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 |  | 
|  | 111 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 112 | return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 |  | 
|  | 114 | /* Now, we would do the remaining detection. But the PCF8574 is plainly | 
|  | 115 | impossible to detect! Stupid chip. */ | 
|  | 116 |  | 
|  | 117 | /* Determine the chip type */ | 
|  | 118 | if (kind <= 0) { | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 119 | if (client->addr >= 0x38 && client->addr <= 0x3f) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | kind = pcf8574a; | 
|  | 121 | else | 
|  | 122 | kind = pcf8574; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | if (kind == pcf8574a) | 
|  | 126 | client_name = "pcf8574a"; | 
|  | 127 | else | 
|  | 128 | client_name = "pcf8574"; | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 129 | strlcpy(info->type, client_name, I2C_NAME_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 |  | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 131 | return 0; | 
|  | 132 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 |  | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 134 | static int pcf8574_probe(struct i2c_client *client, | 
|  | 135 | const struct i2c_device_id *id) | 
|  | 136 | { | 
|  | 137 | struct pcf8574_data *data; | 
|  | 138 | int err; | 
|  | 139 |  | 
|  | 140 | data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL); | 
|  | 141 | if (!data) { | 
|  | 142 | err = -ENOMEM; | 
|  | 143 | goto exit; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | i2c_set_clientdata(client, data); | 
|  | 147 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* Initialize the PCF8574 chip */ | 
| Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 149 | pcf8574_init_client(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 |  | 
|  | 151 | /* Register sysfs hooks */ | 
| Jean Delvare | f741f67 | 2008-07-14 22:38:36 +0200 | [diff] [blame] | 152 | err = sysfs_create_group(&client->dev.kobj, &pcf8574_attr_group); | 
| Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 153 | if (err) | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 154 | goto exit_free; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return 0; | 
|  | 156 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | exit_free: | 
|  | 158 | kfree(data); | 
|  | 159 | exit: | 
|  | 160 | return err; | 
|  | 161 | } | 
|  | 162 |  | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 163 | static int pcf8574_remove(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { | 
| Jean Delvare | 7d9db67 | 2006-09-03 22:20:24 +0200 | [diff] [blame] | 165 | sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | kfree(i2c_get_clientdata(client)); | 
|  | 167 | return 0; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | /* Called when we have found a new PCF8574. */ | 
|  | 171 | static void pcf8574_init_client(struct i2c_client *client) | 
|  | 172 | { | 
|  | 173 | struct pcf8574_data *data = i2c_get_clientdata(client); | 
| Jean Delvare | 553515e | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 174 | data->write = -EAGAIN; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Jean Delvare | 833bedb | 2008-07-16 19:30:06 +0200 | [diff] [blame] | 177 | static const struct i2c_device_id pcf8574_id[] = { | 
|  | 178 | { "pcf8574", 0 }, | 
|  | 179 | { "pcf8574a", 0 }, | 
|  | 180 | { } | 
|  | 181 | }; | 
|  | 182 |  | 
|  | 183 | static struct i2c_driver pcf8574_driver = { | 
|  | 184 | .driver = { | 
|  | 185 | .name	= "pcf8574", | 
|  | 186 | }, | 
|  | 187 | .probe		= pcf8574_probe, | 
|  | 188 | .remove		= pcf8574_remove, | 
|  | 189 | .id_table	= pcf8574_id, | 
|  | 190 |  | 
|  | 191 | .detect		= pcf8574_detect, | 
|  | 192 | .address_data	= &addr_data, | 
|  | 193 | }; | 
|  | 194 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | static int __init pcf8574_init(void) | 
|  | 196 | { | 
|  | 197 | return i2c_add_driver(&pcf8574_driver); | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | static void __exit pcf8574_exit(void) | 
|  | 201 | { | 
|  | 202 | i2c_del_driver(&pcf8574_driver); | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 |  | 
|  | 206 | MODULE_AUTHOR | 
|  | 207 | ("Frodo Looijaard <frodol@dds.nl>, " | 
|  | 208 | "Philip Edelbrock <phil@netroedge.com>, " | 
|  | 209 | "Dan Eaton <dan.eaton@rocketlogix.com> " | 
|  | 210 | "and Aurelien Jarno <aurelien@aurel32.net>"); | 
|  | 211 | MODULE_DESCRIPTION("PCF8574 driver"); | 
|  | 212 | MODULE_LICENSE("GPL"); | 
|  | 213 |  | 
|  | 214 | module_init(pcf8574_init); | 
|  | 215 | module_exit(pcf8574_exit); |