2 * drivers/i2c/chips/pca9535.c
4 * Driver for Philips PCA9535 16-bit low power I/O port with interrupt.
5 * Tested with OMAP-1510 based iPAQ h63xx series of mobile phones.
6 * (h6315, h6340 and h6365)
8 * Copyright (C) 2009 Mika Laitio
9 * Copyright (C) 2009 Husam Senussi
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Copyright (C) 2005 Husam Senussi
26 * Framework based on Pawel Kolodziejski's pca9535 driver in
27 * handheld.org's 2.6.13 kernel. Driver updated by Mika Laitio.
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/i2c.h>
34 #include <linux/hwmon-sysfs.h>
35 #include <linux/hwmon.h>
36 #include <linux/err.h>
38 #include <asm/arch/pca9535.h>
39 #include <linux/delay.h>
41 #include <linux/interrupt.h>
42 #include <asm/mach-types.h>
44 #include <asm/mach/arch.h>
45 #include <asm/hardware.h>
47 EXPORT_SYMBOL(pca9535_gpio_read);
48 EXPORT_SYMBOL(pca9535_gpio_write);
49 EXPORT_SYMBOL(pca9535_gpio_direction);
51 static int pca9535_attach_adapter(struct i2c_adapter *adapter);
52 static int pca9535_detach_client(struct i2c_client *client);
53 static int pca9535_attach(struct i2c_adapter *adapter, int address, int zero_or_minus_one);
54 static u32 pca9535_read_reg(struct i2c_client *client, u8 regaddr);
55 static void pca9535_write_reg(struct i2c_client *client, u8 regaddr, u16 param);
65 PCA9535_DIRECTION_0 = 6,
66 PCA9535_DIRECTION_1 = 7,
70 struct semaphore lock;
71 struct i2c_client client;
74 static struct i2c_driver pca9535_driver = {
78 .attach_adapter = pca9535_attach_adapter,
79 .detach_client = pca9535_detach_client,
82 static struct i2c_client *pca9535_i2c_client = NULL;
83 static struct pca9535_data pca9535_inited;
85 static unsigned short normal_i2c[] = { 0x20, I2C_CLIENT_END };
87 #define DRIVER_VERSION "20 OCT 2005"
88 #define DRIVER_NAME "PCA9535"
91 * sysfs callback function.
93 static ssize_t pca9535_show(struct device *dev, struct device_attribute *attr,
96 struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
97 struct i2c_client *client = to_i2c_client(dev);
98 return sprintf(buf, "%02X\n", (pca9535_read_reg(client, psa->index) >> 8));
102 * sysfs callback function.
104 static ssize_t pca9535_store(struct device *dev, struct device_attribute *attr,
105 const char *buf, size_t count)
107 struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
108 struct i2c_client *client = to_i2c_client(dev);
109 unsigned long val = simple_strtoul(buf, NULL, 0);
110 unsigned long old = pca9535_read_reg(client, psa->index);
115 val = (old & 0xff) | (val << 8);
116 pca9535_write_reg(client, psa->index, val);
120 #define PCA9535_ENTRY_RO(name, cmd_idx) \
121 static SENSOR_DEVICE_ATTR(name, S_IRUGO, pca9535_show, NULL, cmd_idx)
123 #define PCA9535_ENTRY_RW(name, cmd_idx) \
124 static SENSOR_DEVICE_ATTR(name, S_IRUGO | S_IWUSR, pca9535_show, \
125 pca9535_store, cmd_idx)
127 PCA9535_ENTRY_RO(input0, PCA9535_INPUT_0);
128 PCA9535_ENTRY_RO(input1, PCA9535_INPUT_1);
129 PCA9535_ENTRY_RW(output0, PCA9535_OUTPUT_0);
130 PCA9535_ENTRY_RW(output1, PCA9535_OUTPUT_1);
131 PCA9535_ENTRY_RW(invert0, PCA9535_INVERT_0);
132 PCA9535_ENTRY_RW(invert1, PCA9535_INVERT_1);
133 PCA9535_ENTRY_RW(direction0, PCA9535_DIRECTION_0);
134 PCA9535_ENTRY_RW(direction1, PCA9535_DIRECTION_1);
136 static struct attribute *pca9535_attributes[] = {
137 &sensor_dev_attr_input0.dev_attr.attr,
138 &sensor_dev_attr_input1.dev_attr.attr,
139 &sensor_dev_attr_output0.dev_attr.attr,
140 &sensor_dev_attr_output1.dev_attr.attr,
141 &sensor_dev_attr_invert0.dev_attr.attr,
142 &sensor_dev_attr_invert1.dev_attr.attr,
143 &sensor_dev_attr_direction0.dev_attr.attr,
144 &sensor_dev_attr_direction1.dev_attr.attr,
148 static struct attribute_group pca9535_defattr_group = {
149 .attrs = pca9535_attributes,
151 //End of sysfs management code.
155 u32 pca9535_read_input(void)
157 return pca9535_read_reg(pca9535_i2c_client, 0);
159 EXPORT_SYMBOL(pca9535_read_input);
161 void pca9535_write_output(u16 param)
163 pca9535_write_reg(pca9535_i2c_client, 2, param);
165 EXPORT_SYMBOL(pca9535_write_output);
167 void pca9535_set_dir(u16 param)
169 pca9535_write_reg(pca9535_i2c_client, 6, param);
171 EXPORT_SYMBOL(pca9535_set_dir);
173 static int pca9535_attach_adapter(struct i2c_adapter *adapter)
175 return i2c_probe(adapter, &addr_data, pca9535_attach);
178 static int pca9535_attach(struct i2c_adapter *adapter, int address, int zero_or_minus_one)
180 struct i2c_client *new_client;
183 new_client = &(pca9535_inited.client);
184 i2c_set_clientdata(new_client, 0);
185 new_client->addr = address;
186 new_client->adapter = adapter;
187 new_client->driver = &pca9535_driver;
188 strcpy(new_client->name, DRIVER_NAME);
190 if ((err = i2c_attach_client(new_client)))
193 pca9535_i2c_client = new_client;
195 init_MUTEX(&pca9535_inited.lock);
196 i2c_set_clientdata(pca9535_i2c_client, &pca9535_inited);
198 sysfs_create_group(&pca9535_i2c_client->dev.kobj, &pca9535_defattr_group);
200 printk("pca9535_attach() ok, address = %d, zero_or_minus_one = %d\n", address, zero_or_minus_one);
204 printk("pca9535_attach() failed, error code = %d\n", err);
208 static int pca9535_detach_client(struct i2c_client *client)
212 if ((err = i2c_detach_client(client))) {
213 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
216 pca9535_i2c_client = NULL;
221 static int __init pca9535_init(void)
223 return i2c_add_driver(&pca9535_driver);
226 static void __exit pca9535_exit(void)
228 i2c_del_driver(&pca9535_driver);
232 * Reads the value of GPIO available via I2C.
234 int pca9535_gpio_read(int gpio){
235 unsigned char reg = 0;
236 unsigned long val = 0;
238 printk("9535_gpio_read() called\n");
239 if(!pca9535_i2c_client)
242 if(gpio < GPIO0 || gpio > GPIO17)
245 if(gpio >= GPIO0 && gpio <= GPIO7){
246 reg = PCA9535_INPUT_0;
248 }else if(gpio >= GPIO8 && gpio <= GPIO17){
249 reg = PCA9535_INPUT_1;
253 down(&pca9535_inited.lock);
255 // Read the existing values first
256 val = pca9535_read_reg(pca9535_i2c_client, reg) >> 8;
257 val = (val >> gpio) & 0x01;
259 up(&pca9535_inited.lock);
265 * Set the value of I2C GPIO.
267 int pca9535_gpio_write(int gpio, unsigned char value){
268 unsigned char in_reg = 0;
269 unsigned char out_reg = 0;
270 unsigned long val = 0;
271 unsigned long old = 0;
274 if(!pca9535_i2c_client)
277 if(gpio < GPIO0 || gpio > GPIO17)
280 if(gpio >= GPIO0 && gpio <= GPIO7){
281 in_reg = PCA9535_INPUT_0;
282 out_reg = PCA9535_OUTPUT_0;
284 }else if(gpio >= GPIO8 && gpio <= GPIO17){
285 in_reg = PCA9535_INPUT_1;
286 out_reg = PCA9535_OUTPUT_1;
290 down(&pca9535_inited.lock);
292 // Read the existing values first
293 val = pca9535_read_reg(pca9535_i2c_client, in_reg);
308 val = (val & 0xff) | (old << 8);
310 // write the values back to the register
311 pca9535_write_reg(pca9535_i2c_client, out_reg, val);
314 up(&pca9535_inited.lock);
319 * Set the direction of I2C GPIO.
321 int pca9535_gpio_direction(int gpio, unsigned char direction){
322 unsigned char reg = 0;
323 unsigned long val = 0;
324 unsigned long old = 0;
327 if(!pca9535_i2c_client)
330 if(gpio < GPIO0 || gpio > GPIO17)
333 if(gpio >= GPIO0 && gpio <= GPIO7){
334 reg = PCA9535_DIRECTION_0;
336 }else if(gpio >= GPIO8 && gpio <= GPIO17){
337 reg = PCA9535_DIRECTION_1;
341 down(&pca9535_inited.lock);
343 // Read the existing values first
344 old = pca9535_read_reg(pca9535_i2c_client, reg);
359 val = (old & 0xff) | (val << 8);
361 // write the values back to the register
362 pca9535_write_reg(pca9535_i2c_client, reg, val);
365 up(&pca9535_inited.lock);
369 static u32 pca9535_read_reg(struct i2c_client *client, u8 regaddr)
379 r = i2c_master_send(client, buffer, 1);
381 printk(KERN_ERR "pca9535: read failed, status %d\n", r);
385 r = i2c_master_recv(client, buffer, 3);
387 printk(KERN_ERR "pca9535: read failed, status %d\n", r);
392 data |= buffer[2] << 8;
393 //printk(KERN_ERR "%s: reading %x in %x\n", __FUNCTION__, data, regaddr);
398 static void pca9535_write_reg(struct i2c_client *client, u8 regaddr, u16 data)
403 //printk(KERN_ERR "%s: writing %x in %x\n", __FUNCTION__, data, regaddr);
405 buffer[1] = data >> 8;
406 buffer[2] = data & 0xff;
408 r = i2c_master_send(client, buffer, 3);
410 printk(KERN_ERR "pca9535: write failed, status %d\n", r);
414 MODULE_AUTHOR("Husam Senussi <husamsenussi@gmail.com>");
415 MODULE_DESCRIPTION("PCA9535 driver");
416 MODULE_LICENSE("GPL");
418 module_init(pca9535_init);
419 module_exit(pca9535_exit);