2 * Copyright (C) 2010 ROCKCHIP, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 /*******************************************************************/
14 /* COPYRIGHT (C) ROCK-CHIPS FUZHOU . ALL RIGHTS RESERVED.*/
15 /*******************************************************************
20 ********************************************************************/
21 #include <asm/mach/time.h>
22 #include <linux/clk.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <asm/mach-types.h>
26 #include <linux/irq.h>
27 #include <linux/debugfs.h>
28 #include <linux/seq_file.h>
29 #include <linux/kernel.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
33 #include <mach/hardware.h>
34 #include <mach/gpio.h>
35 #include <mach/rk2818_iomap.h>
36 #include <mach/iomux.h>
37 #include <linux/device.h>
38 #include <mach/gpio.h>
40 #include <linux/i2c.h>
41 #include <linux/workqueue.h>
42 #include <mach/board.h>
43 #include <linux/delay.h>
44 #include <linux/i2c/tca6424.h>
45 #include <linux/ktime.h>
46 #include "../drivers/gpio/expand_gpio_soft_interrupt.h"
49 #define DBG(x...) printk(KERN_INFO x)
53 #define DBGERR(x...) printk(KERN_INFO x)
58 unsigned int gpio_start;
59 unsigned int gpio_pin_num;
61 #ifdef TCA6424_OUTREGLOCK
62 struct mutex outreglock;
64 #ifdef TCA6424_INPUTREGLOCK
65 struct mutex inputreglock;
67 #ifdef TCA6424_CONFIGREGLOCK
68 struct mutex configreglock;
71 struct i2c_client *client;
72 struct expand_gpio_soft_int *expand;
73 struct expand_gpio_global_variable gtca6424_struct;
74 struct gpio_chip gpio_chip;
78 static const struct i2c_device_id tca6424_id[] =
80 {"extend_gpio_tca6424",8,},
83 MODULE_DEVICE_TABLE(i2c, tca6424_id);
85 static short int portnum[TCA6424_PortNum]={ TCA6424_Port0PinNum,
86 TCA6424_Port1PinNum,TCA6424_Port2PinNum};
88 extern inline struct gpio_chip *gpio_to_chip(unsigned gpio);
90 int tca6424_irq_read_inputreg(void *data,char *buf)
93 struct tca6424_chip *tca6424data=(struct tca6424_chip *)data;
95 ret = i2c_master_reg8_recv(tca6424data->client, TCA6424_Auto_InputLevel_Reg, buf, 3, TCA6424_I2C_RATE);
99 static int tca6424_write_reg(struct i2c_client *client, uint8_t reg, uint8_t val)
102 struct i2c_adapter *adap;
107 adap = client->adapter;
111 msg.addr = client->addr;
114 msg.flags = client->flags;
115 msg.scl_rate = TCA6424_I2C_RATE;
116 ret = i2c_transfer(adap, &msg, 1);
120 static int tca6424_read_reg(struct i2c_client *client, uint8_t reg, uint8_t *val)
123 struct i2c_adapter *adap;
124 struct i2c_msg msgs[2];
128 adap = client->adapter;
130 msgs[0].addr = client->addr;
132 msgs[0].flags = client->flags;
134 msgs[0].scl_rate = TCA6424_I2C_RATE;
137 msgs[1].addr = client->addr;
138 msgs[1].flags = client->flags | I2C_M_RD;
140 msgs[1].scl_rate = TCA6424_I2C_RATE;
142 ret = i2c_transfer(adap, msgs, 2);
147 static int tca6424_write_three_reg(struct i2c_client *client, const char reg, const char *buf, int count, int rate)
150 ret = i2c_master_reg8_send(client, reg, buf, count, rate);
151 return (ret>0)?0:ret;
154 static int tca6424_read_three_reg(struct i2c_client *client, const char reg, char *buf, int count, int rate)
157 ret = i2c_master_reg8_recv(client, reg, buf, count, rate);
158 return (ret>0)?0:ret;
161 static int tca6424_gpio_direction_input(struct gpio_chip *gc, unsigned pin_num)
163 struct tca6424_chip *chip;
165 uint8_t gpioPortPinNum;
170 chip = container_of(gc, struct tca6424_chip, gpio_chip);
171 gpioPortNum = pin_num/8;
172 gpioPortPinNum= pin_num%8;
173 if((gpioPortNum>=TCA6424_PortNum)||(gpioPortPinNum>=portnum[gpioPortNum]))
175 Regaddr = TCA6424_Config_Reg+gpioPortNum;
177 #ifdef TCA6424_CONFIGREGLOCK
178 if (!mutex_trylock(&chip->configreglock))
180 DBGERR("**%s[%d]Did not get the configreglock**\n",__FUNCTION__,__LINE__);
185 if(((chip->gtca6424_struct.reg_direction[gpioPortNum]>>gpioPortPinNum)& 0x01)==EXTGPIO_OUTPUT)
187 reg_val = tca6424setbit(chip->gtca6424_struct.reg_direction[gpioPortNum], gpioPortPinNum);
188 ret = tca6424_write_reg(chip->client, Regaddr, reg_val);
191 chip->gtca6424_struct.reg_direction[gpioPortNum] = reg_val;
192 //DBG("**%s[%d],set config address[0x%2x]=%2x,ret=%d**\n",__FUNCTION__,__LINE__,Regaddr,reg_val,ret);
195 #ifdef TCA6424_CONFIGREGLOCK
196 mutex_unlock(&chip->configreglock);
201 static int tca6424_gpio_direction_output(struct gpio_chip *gc, unsigned pin_num, int val)
203 struct tca6424_chip *chip;
205 uint8_t gpioPortPinNum;
210 chip = container_of(gc, struct tca6424_chip, gpio_chip);
211 gpioPortNum = pin_num/8;
212 gpioPortPinNum = pin_num%8;
213 if((gpioPortNum>=TCA6424_PortNum)||(gpioPortPinNum>=portnum[gpioPortNum]))
215 Regaddr = TCA6424_Config_Reg+gpioPortNum;
217 #ifdef TCA6424_CONFIGREGLOCK
218 if (!mutex_trylock(&chip->configreglock))
220 DBGERR("**%s[%d]Did not get the configreglock**\n",__FUNCTION__,__LINE__);
225 if(((chip->gtca6424_struct.reg_direction[gpioPortNum]>>gpioPortPinNum)& 0x01)==EXTGPIO_INPUT)
227 reg_val = tca6424clearbit(chip->gtca6424_struct.reg_direction[gpioPortNum], gpioPortPinNum);
228 //DBG("**%s[%d],set config address[0x%2x]=%2x,**\n",__FUNCTION__,__LINE__,Regaddr,reg_val);
229 ret = tca6424_write_reg(chip->client, Regaddr, reg_val);
232 #ifdef TCA6424_CONFIGREGLOCK
233 mutex_unlock(&chip->configreglock);
235 DBGERR("**%s[%d] set direction reg is error,reg_val=%x,ret=%d**\n",__FUNCTION__,__LINE__,reg_val,ret);
238 chip->gtca6424_struct.reg_direction[gpioPortNum] = reg_val;
241 #ifdef TCA6424_CONFIGREGLOCK
242 mutex_unlock(&chip->configreglock);
246 #ifdef TCA6424_OUTREGLOCK
247 if (!mutex_trylock(&chip->outreglock))
249 DBGERR("**%s[%d] Did not get the outreglock**\n",__FUNCTION__,__LINE__);
254 if(((chip->gtca6424_struct.reg_output[gpioPortNum]>>gpioPortPinNum)& 0x01) != val)
257 reg_val = tca6424setbit(chip->gtca6424_struct.reg_output[gpioPortNum], gpioPortPinNum);
259 reg_val = tca6424clearbit(chip->gtca6424_struct.reg_output[gpioPortNum], gpioPortPinNum);
261 Regaddr = TCA6424_OutputLevel_Reg+gpioPortNum;
262 ret = tca6424_write_reg(chip->client, Regaddr, reg_val);
265 #ifdef TCA6424_OUTREGLOCK
266 mutex_unlock(&chip->outreglock);
268 DBGERR("**%s[%d] set out reg is error,reg_val=%x,ret=%d**\n",__FUNCTION__,__LINE__,reg_val,ret);
271 chip->gtca6424_struct.reg_output[gpioPortNum] = reg_val;
274 #ifdef TCA6424_OUTREGLOCK
275 mutex_unlock(&chip->outreglock);
277 //DBG("**%s[%d],set output address[0x%2x]=%2x,ret=%d**\n",__FUNCTION__,__LINE__,Regaddr,reg_val,ret);
281 static int tca6424_gpio_get_value(struct gpio_chip *gc, unsigned pin_num)
283 struct tca6424_chip *chip;
285 uint8_t gpioPortPinNum;
289 chip = container_of(gc, struct tca6424_chip, gpio_chip);
291 #ifdef CONFIG_EXPAND_GPIO_SOFT_INTERRUPT
292 ret = wait_untill_input_reg_flash( );
296 DBGERR("**********tca6424 get value error***************\n");
300 gpioPortNum = pin_num/8;
301 gpioPortPinNum= pin_num%8;
302 Regaddr = TCA6424_OutputLevel_Reg+gpioPortNum;
304 if((gpioPortNum>=TCA6424_PortNum)||(gpioPortPinNum>=portnum[gpioPortNum]))
307 #ifndef CONFIG_EXPAND_GPIO_SOFT_INTERRUPT
309 ret = tca6424_read_reg(chip->client, Regaddr, ®_val);
312 chip->gtca6424_struct.reg_input[gpioPortNum] = reg_val;
315 //DBG("**%s[%d] read input address[0x%2x]=%2x**\n",__FUNCTION__,__LINE__,Regaddr,chip->reg_input[gpioPortNum]);
316 return ((chip->gtca6424_struct.reg_input[gpioPortNum] >> gpioPortPinNum) & 0x01);
319 static void tca6424_gpio_set_value(struct gpio_chip *gc, unsigned pin_num, int val)
321 struct tca6424_chip *chip;
323 uint8_t gpioPortPinNum;
328 chip = container_of(gc, struct tca6424_chip, gpio_chip);
329 gpioPortNum = pin_num/8;
330 gpioPortPinNum= pin_num%8;
331 if((gpioPortNum>=TCA6424_PortNum)||(gpioPortPinNum>=portnum[gpioPortNum]))
333 Regaddr = TCA6424_OutputLevel_Reg+gpioPortNum;
334 if(tca6424getbit(chip->gtca6424_struct.reg_direction[gpioPortNum],gpioPortPinNum)) // input state
339 #ifdef TCA6424_OUTREGLOCK
340 if (!mutex_trylock(&chip->outreglock))
342 DBGERR("**%s[%d] Did not get the outreglock**\n",__FUNCTION__,__LINE__);
346 if(((chip->gtca6424_struct.reg_output[gpioPortNum]>>gpioPortPinNum)& 0x01) != val)
349 reg_val = tca6424setbit(chip->gtca6424_struct.reg_output[gpioPortNum], gpioPortPinNum);
351 reg_val = tca6424clearbit(chip->gtca6424_struct.reg_output[gpioPortNum], gpioPortPinNum);
353 ret = tca6424_write_reg(chip->client, Regaddr, reg_val);
356 chip->gtca6424_struct.reg_output[gpioPortNum] = reg_val;
357 //DBG("**%s[%d],set output address[0x%2x]=%2x,ret=%d**\n",__FUNCTION__,__LINE__,Regaddr,reg_val,ret);
360 #ifdef TCA6424_OUTREGLOCK
361 mutex_unlock(&chip->outreglock);
366 int tca6424_checkrange(int start,int num,int val)
368 if((val<(start+num))&&(val>=start))
374 static int tca6424_gpio_to_irq(struct gpio_chip *chip,unsigned offset)
376 struct tca6424_chip *pchip = container_of(chip, struct tca6424_chip, gpio_chip);
377 if(((pchip->gpio_start+offset)>=chip->base)&&((pchip->gpio_start+offset)<(chip->base+chip->ngpio)))
379 //DBG("**%s,offset=%d,gpio_irq_start=%d,base=%d,ngpio=%d,gpio_irq_start=%d**\n",
380 // __FUNCTION__,offset,pchip->expand->gpio_irq_start,chip->base,chip->ngpio,pchip->expand->gpio_irq_start);
381 return (offset+pchip->expand->gpio_irq_start);
389 static void tca6424_setup_gpio(struct tca6424_chip *chip, int gpios)
391 struct gpio_chip *gc;
392 gc = &chip->gpio_chip;
393 gc->direction_input = tca6424_gpio_direction_input;
394 gc->direction_output = tca6424_gpio_direction_output;
395 gc->get = tca6424_gpio_get_value;
396 gc->set = tca6424_gpio_set_value;
397 gc->to_irq = tca6424_gpio_to_irq;
399 gc->base = chip->gpio_start;
400 gc->ngpio = chip->gpio_pin_num;
401 gc->label = chip->client->name;
402 gc->dev = &chip->client->dev;
403 gc->owner = THIS_MODULE;
404 gc->names = chip->names;
407 int tca6424_init_pintype(struct tca6424_chip *chip,struct i2c_client *client)
409 struct tca6424_platform_data *platform_data=(struct tca6424_platform_data *)client->dev.platform_data;
410 struct rk2818_gpio_expander_info *tca6424_gpio_settinginfo;
411 uint8_t reg_output[TCA6424_PortNum]={0,0,0};
412 uint8_t reg_direction[TCA6424_PortNum]={0,0,0};
413 uint8_t reg_invert[TCA6424_PortNum]={0,0,0};
414 uint8_t tca6424_pin_num;
416 uint8_t gpioPortPinNum,tca6424_settingpin_num=0;
420 tca6424_gpio_settinginfo=platform_data->settinginfo;
421 if(tca6424_gpio_settinginfo)
423 tca6424_settingpin_num=platform_data->settinginfolen;
424 for(i=0;i<tca6424_settingpin_num;i++)
426 if(!tca6424_checkrange(chip->gpio_start,chip->gpio_pin_num,tca6424_gpio_settinginfo[i].gpio_num))
428 tca6424_pin_num=tca6424_gpio_settinginfo[i].gpio_num-chip->gpio_start;
429 gpioPortNum = tca6424_pin_num/ TCA6424_PortPinNum;
430 gpioPortPinNum= tca6424_pin_num% TCA6424_PortPinNum;
431 //DBG("gpioPortNum=%d,gpioPortNum=%d,tca6424_pin_num=%d,reg_direction=%x,reg_output=%x,reg_input=%x\n",gpioPortNum,gpioPortPinNum,tca6424_pin_num,reg_direction[i],reg_output[i]);
432 if((gpioPortNum>=TCA6424_PortNum)||(gpioPortPinNum>=portnum[gpioPortNum]))
434 if(tca6424_gpio_settinginfo[i].pin_type==GPIO_IN)
436 reg_direction[gpioPortNum]=tca6424setbit(reg_direction[gpioPortNum],gpioPortPinNum);
440 if(tca6424_gpio_settinginfo[i].pin_value==GPIO_HIGH)
442 reg_output[gpioPortNum]=tca6424setbit(reg_output[gpioPortNum],gpioPortPinNum);
450 #ifdef TCA6424_OUTREGLOCK
451 mutex_init(&chip->outreglock);
453 #ifdef TCA6424_INPUTREGLOCK
454 mutex_init(&chip->inputreglock);
456 #ifdef TCA6424_OUTREGLOCK
457 mutex_init(&chip->configreglock);
460 if(tca6424_write_three_reg(client, TCA6424_Auto_Config_Reg , ®_direction[0], 3, TCA6424_I2C_RATE)<0)
462 DBGERR("*%s %d* write reg err\n",__FUNCTION__,__LINE__);
465 if (tca6424_write_three_reg(client, TCA6424_Auto_OutputLevel_Reg, ®_output[0], 3, TCA6424_I2C_RATE)<0)
467 DBGERR("*%s %d* write reg err\n",__FUNCTION__,__LINE__);
470 if (tca6424_write_three_reg(client, TCA6424_Auto_Invert_Reg, ®_invert[0], 3, TCA6424_I2C_RATE)<0) //make sure this reg be 0
472 DBGERR("*%s %d* write reg err\n",__FUNCTION__,__LINE__);
475 if(tca6424_read_three_reg(client, TCA6424_Auto_InputLevel_Reg, &chip->gtca6424_struct.reg_input[0], 3, TCA6424_I2C_RATE)<0)
477 DBGERR("*%s %d read reg err*\n",__FUNCTION__,__LINE__);
480 for(i=0; i<TCA6424_PortNum; i++)
482 chip->gtca6424_struct.reg_direction[i]=reg_direction[i];
483 chip->gtca6424_struct.reg_output[i]=reg_output[i];
484 DBG("reg_direction=%x,reg_output=%x,reg_input=%x\n",chip->gtca6424_struct.reg_direction[i],chip->gtca6424_struct.reg_output[i],chip->gtca6424_struct.reg_input[i]);
489 static int __devinit tca6424_probe(struct i2c_client *client,const struct i2c_device_id *id)
491 struct tca6424_chip *chip;
492 struct tca6424_platform_data *pdata;
494 DBG(KERN_ALERT"*******gpio %s in %d line,dev adr is %x**\n",__FUNCTION__,__LINE__,client->addr);
495 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
498 chip = kzalloc(sizeof(struct tca6424_chip), GFP_KERNEL);
501 pdata = client->dev.platform_data;
503 DBGERR(" %s no platform data\n",__FUNCTION__);
507 //used by old tca6424,it will remove later
508 client->adapter->dev.platform_data = pdata;
510 chip->gpio_start = pdata->gpio_base;
511 chip->gpio_pin_num=pdata->gpio_pin_num;
512 chip->client = client;
513 chip->names = pdata->names;
515 #ifdef CONFIG_EXPAND_GPIO_SOFT_INTERRUPT
516 chip->expand = &expand_irq_data;
517 chip->expand->gpio_irq_start =pdata->gpio_irq_start;
518 chip->expand->irq_pin_num = pdata->irq_pin_num;
519 chip->expand->irq_gpiopin=pdata->tca6424_irq_pin;
520 chip->expand->irq_chain = gpio_to_irq(pdata->tca6424_irq_pin);
521 chip->expand->expand_port_group = pdata->expand_port_group;
\r
522 chip->expand->expand_port_pinnum = pdata->expand_port_pinnum;
\r
523 chip->expand->rk_irq_mode = pdata->rk_irq_mode;
\r
524 chip->expand->rk_irq_gpio_pull_up_down = pdata->rk_irq_gpio_pull_up_down;
527 /* initialize cached registers from their original values.
528 * we can't share this chip with another i2c master.
530 tca6424_setup_gpio(chip, id->driver_data);
531 ret = gpiochip_add(&chip->gpio_chip);
534 if(tca6424_init_pintype(chip,client))
537 ret = pdata->setup(client, chip->gpio_chip.base,
538 chip->gpio_chip.ngpio, pdata->context);
540 DBGERR(" %s setup failed, %d\n",__FUNCTION__,ret);
542 i2c_set_clientdata(client, chip);
544 #ifdef CONFIG_EXPAND_GPIO_SOFT_INTERRUPT
545 expand_irq_init(chip,&chip->gtca6424_struct,tca6424_irq_read_inputreg);
553 static int tca6424_remove(struct i2c_client *client)
555 struct tca6424_platform_data *pdata = client->dev.platform_data;
556 struct tca6424_chip *chip = i2c_get_clientdata(client);
559 if (pdata->teardown) {
560 ret = pdata->teardown(client, chip->gpio_chip.base,
561 chip->gpio_chip.ngpio, pdata->context);
563 DBGERR(" %s failed, %d\n",__FUNCTION__,ret);
568 ret = gpiochip_remove(&chip->gpio_chip);
570 dev_err(&client->dev, "%s failed, %d\n",
571 "gpiochip_remove()", ret);
578 static int tca6424_suspend(struct i2c_client *client, pm_message_t mesg)
580 DBG("*****************tca6424 suspend*******************");
584 static int tca6424_resume(struct i2c_client *client)
586 DBG("*****************tca6424 resume*******************");
590 static struct i2c_driver tca6424_driver = {
592 .owner = THIS_MODULE,
593 .name = "extend_gpio_tca6424",
595 .probe = tca6424_probe,
596 .remove = tca6424_remove,
597 .id_table = tca6424_id,
598 .resume = tca6424_resume,
599 .suspend = tca6424_suspend,
601 static int __init tca6424_init(void)
604 DBG(KERN_ALERT"**********tca6424_init**********\n");
605 tmp=i2c_add_driver(&tca6424_driver);
608 subsys_initcall(tca6424_init);
610 static void __exit tca6424_exit(void)
612 DBG(KERN_ALERT"**********tca6424_exit**********\n");
613 i2c_del_driver(&tca6424_driver);
615 module_exit(tca6424_exit);
617 MODULE_AUTHOR(" XXX XXX@rock-chips.com");
618 MODULE_DESCRIPTION("Driver for rk2818 tca6424 device");
619 MODULE_LICENSE("GPL");