Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.24-rc2 233 lines 6.4 kB view raw
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> 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 }; 47 48/* Insmod parameters */ 49I2C_CLIENT_INSMOD_2(pcf8574, pcf8574a); 50 51/* Each client has this additional data */ 52struct pcf8574_data { 53 struct i2c_client client; 54 55 int write; /* Remember last written value */ 56}; 57 58static int pcf8574_attach_adapter(struct i2c_adapter *adapter); 59static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind); 60static int pcf8574_detach_client(struct i2c_client *client); 61static void pcf8574_init_client(struct i2c_client *client); 62 63/* This is the driver that will be inserted */ 64static struct i2c_driver pcf8574_driver = { 65 .driver = { 66 .name = "pcf8574", 67 }, 68 .id = I2C_DRIVERID_PCF8574, 69 .attach_adapter = pcf8574_attach_adapter, 70 .detach_client = pcf8574_detach_client, 71}; 72 73/* following are the sysfs callback functions */ 74static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf) 75{ 76 struct i2c_client *client = to_i2c_client(dev); 77 return sprintf(buf, "%u\n", i2c_smbus_read_byte(client)); 78} 79 80static DEVICE_ATTR(read, S_IRUGO, show_read, NULL); 81 82static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf) 83{ 84 struct pcf8574_data *data = i2c_get_clientdata(to_i2c_client(dev)); 85 86 if (data->write < 0) 87 return data->write; 88 89 return sprintf(buf, "%d\n", data->write); 90} 91 92static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf, 93 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 109static struct attribute *pcf8574_attributes[] = { 110 &dev_attr_read.attr, 111 &dev_attr_write.attr, 112 NULL 113}; 114 115static const struct attribute_group pcf8574_attr_group = { 116 .attrs = pcf8574_attributes, 117}; 118 119/* 120 * Real code 121 */ 122 123static int pcf8574_attach_adapter(struct i2c_adapter *adapter) 124{ 125 return i2c_probe(adapter, &addr_data, pcf8574_detect); 126} 127 128/* This function is called by i2c_probe */ 129static int pcf8574_detect(struct i2c_adapter *adapter, int address, int kind) 130{ 131 struct i2c_client *new_client; 132 struct pcf8574_data *data; 133 int err = 0; 134 const char *client_name = ""; 135 136 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) 137 goto exit; 138 139 /* OK. For now, we presume we have a valid client. We now create the 140 client structure, even though we cannot fill it completely yet. */ 141 if (!(data = kzalloc(sizeof(struct pcf8574_data), GFP_KERNEL))) { 142 err = -ENOMEM; 143 goto exit; 144 } 145 146 new_client = &data->client; 147 i2c_set_clientdata(new_client, data); 148 new_client->addr = address; 149 new_client->adapter = adapter; 150 new_client->driver = &pcf8574_driver; 151 new_client->flags = 0; 152 153 /* Now, we would do the remaining detection. But the PCF8574 is plainly 154 impossible to detect! Stupid chip. */ 155 156 /* Determine the chip type */ 157 if (kind <= 0) { 158 if (address >= 0x38 && address <= 0x3f) 159 kind = pcf8574a; 160 else 161 kind = pcf8574; 162 } 163 164 if (kind == pcf8574a) 165 client_name = "pcf8574a"; 166 else 167 client_name = "pcf8574"; 168 169 /* Fill in the remaining client fields and put it into the global list */ 170 strlcpy(new_client->name, client_name, I2C_NAME_SIZE); 171 172 /* Tell the I2C layer a new client has arrived */ 173 if ((err = i2c_attach_client(new_client))) 174 goto exit_free; 175 176 /* Initialize the PCF8574 chip */ 177 pcf8574_init_client(new_client); 178 179 /* Register sysfs hooks */ 180 err = sysfs_create_group(&new_client->dev.kobj, &pcf8574_attr_group); 181 if (err) 182 goto exit_detach; 183 return 0; 184 185 exit_detach: 186 i2c_detach_client(new_client); 187 exit_free: 188 kfree(data); 189 exit: 190 return err; 191} 192 193static int pcf8574_detach_client(struct i2c_client *client) 194{ 195 int err; 196 197 sysfs_remove_group(&client->dev.kobj, &pcf8574_attr_group); 198 199 if ((err = i2c_detach_client(client))) 200 return err; 201 202 kfree(i2c_get_clientdata(client)); 203 return 0; 204} 205 206/* Called when we have found a new PCF8574. */ 207static void pcf8574_init_client(struct i2c_client *client) 208{ 209 struct pcf8574_data *data = i2c_get_clientdata(client); 210 data->write = -EAGAIN; 211} 212 213static int __init pcf8574_init(void) 214{ 215 return i2c_add_driver(&pcf8574_driver); 216} 217 218static void __exit pcf8574_exit(void) 219{ 220 i2c_del_driver(&pcf8574_driver); 221} 222 223 224MODULE_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>"); 229MODULE_DESCRIPTION("PCF8574 driver"); 230MODULE_LICENSE("GPL"); 231 232module_init(pcf8574_init); 233module_exit(pcf8574_exit);