2 * GPIO Driver for Dialog DA9052 PMICs.
4 * Copyright(c) 2011 Dialog Semiconductor Ltd.
6 * Author: David Dajun Chen <dchen@diasemi.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/module.h>
16 #include <linux/uaccess.h>
17 #include <linux/platform_device.h>
18 #include <linux/gpio.h>
19 #include <linux/syscalls.h>
20 #include <linux/seq_file.h>
22 #include <linux/mfd/da9052/da9052.h>
23 #include <linux/mfd/da9052/reg.h>
24 #include <linux/mfd/da9052/pdata.h>
25 #include <linux/mfd/da9052/gpio.h>
27 #define DA9052_INPUT 1
28 #define DA9052_OUTPUT_OPENDRAIN 2
29 #define DA9052_OUTPUT_PUSHPULL 3
31 #define DA9052_SUPPLY_VDD_IO1 0
33 #define DA9052_DEBOUNCING_OFF 0
34 #define DA9052_DEBOUNCING_ON 1
36 #define DA9052_OUTPUT_LOWLEVEL 0
38 #define DA9052_ACTIVE_LOW 0
39 #define DA9052_ACTIVE_HIGH 1
41 #define DA9052_GPIO_MAX_PORTS_PER_REGISTER 8
42 #define DA9052_GPIO_SHIFT_COUNT(no) (no%8)
43 #define DA9052_GPIO_MASK_UPPER_NIBBLE 0xF0
44 #define DA9052_GPIO_MASK_LOWER_NIBBLE 0x0F
45 #define DA9052_GPIO_NIBBLE_SHIFT 4
48 struct da9052
*da9052
;
52 static inline struct da9052_gpio
*to_da9052_gpio(struct gpio_chip
*chip
)
54 return container_of(chip
, struct da9052_gpio
, gp
);
57 static unsigned char da9052_gpio_port_odd(unsigned offset
)
62 static int da9052_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
64 struct da9052_gpio
*gpio
= to_da9052_gpio(gc
);
65 int da9052_port_direction
= 0;
68 ret
= da9052_reg_read(gpio
->da9052
,
69 DA9052_GPIO_0_1_REG
+ (offset
>> 1));
73 if (da9052_gpio_port_odd(offset
)) {
74 da9052_port_direction
= ret
& DA9052_GPIO_ODD_PORT_PIN
;
75 da9052_port_direction
>>= 4;
77 da9052_port_direction
= ret
& DA9052_GPIO_EVEN_PORT_PIN
;
80 switch (da9052_port_direction
) {
82 if (offset
< DA9052_GPIO_MAX_PORTS_PER_REGISTER
)
83 ret
= da9052_reg_read(gpio
->da9052
,
86 ret
= da9052_reg_read(gpio
->da9052
,
90 if (ret
& (1 << DA9052_GPIO_SHIFT_COUNT(offset
)))
94 case DA9052_OUTPUT_PUSHPULL
:
95 if (da9052_gpio_port_odd(offset
))
96 return ret
& DA9052_GPIO_ODD_PORT_MODE
;
98 return ret
& DA9052_GPIO_EVEN_PORT_MODE
;
104 static void da9052_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
106 struct da9052_gpio
*gpio
= to_da9052_gpio(gc
);
107 unsigned char register_value
= 0;
110 if (da9052_gpio_port_odd(offset
)) {
112 register_value
= DA9052_GPIO_ODD_PORT_MODE
;
113 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
115 DA9052_GPIO_ODD_PORT_MODE
,
118 dev_err(gpio
->da9052
->dev
,
119 "Failed to updated gpio odd reg,%d",
124 register_value
= DA9052_GPIO_EVEN_PORT_MODE
;
125 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
127 DA9052_GPIO_EVEN_PORT_MODE
,
130 dev_err(gpio
->da9052
->dev
,
131 "Failed to updated gpio even reg,%d",
137 static int da9052_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
139 struct da9052_gpio
*gpio
= to_da9052_gpio(gc
);
140 unsigned char register_value
;
143 /* Format: function - 2 bits type - 1 bit mode - 1 bit */
144 register_value
= DA9052_INPUT
| DA9052_ACTIVE_LOW
<< 2 |
145 DA9052_DEBOUNCING_ON
<< 3;
147 if (da9052_gpio_port_odd(offset
))
148 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
150 DA9052_GPIO_MASK_UPPER_NIBBLE
,
152 DA9052_GPIO_NIBBLE_SHIFT
));
154 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
156 DA9052_GPIO_MASK_LOWER_NIBBLE
,
162 static int da9052_gpio_direction_output(struct gpio_chip
*gc
,
163 unsigned offset
, int value
)
165 struct da9052_gpio
*gpio
= to_da9052_gpio(gc
);
166 unsigned char register_value
;
169 /* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
170 register_value
= DA9052_OUTPUT_PUSHPULL
| DA9052_SUPPLY_VDD_IO1
<< 2 |
173 if (da9052_gpio_port_odd(offset
))
174 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
176 DA9052_GPIO_MASK_UPPER_NIBBLE
,
178 DA9052_GPIO_NIBBLE_SHIFT
));
180 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
182 DA9052_GPIO_MASK_LOWER_NIBBLE
,
188 static int da9052_gpio_to_irq(struct gpio_chip
*gc
, u32 offset
)
190 struct da9052_gpio
*gpio
= to_da9052_gpio(gc
);
191 struct da9052
*da9052
= gpio
->da9052
;
193 return da9052
->irq_base
+ DA9052_IRQ_GPI0
+ offset
;
196 static struct gpio_chip reference_gp __devinitdata
= {
197 .label
= "da9052-gpio",
198 .owner
= THIS_MODULE
,
199 .get
= da9052_gpio_get
,
200 .set
= da9052_gpio_set
,
201 .direction_input
= da9052_gpio_direction_input
,
202 .direction_output
= da9052_gpio_direction_output
,
203 .to_irq
= da9052_gpio_to_irq
,
209 static int __devinit
da9052_gpio_probe(struct platform_device
*pdev
)
211 struct da9052_gpio
*gpio
;
212 struct da9052_pdata
*pdata
;
215 gpio
= kzalloc(sizeof(*gpio
), GFP_KERNEL
);
219 gpio
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
220 pdata
= gpio
->da9052
->dev
->platform_data
;
222 gpio
->gp
= reference_gp
;
223 if (pdata
&& pdata
->gpio_base
)
224 gpio
->gp
.base
= pdata
->gpio_base
;
226 ret
= gpiochip_add(&gpio
->gp
);
228 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n", ret
);
232 platform_set_drvdata(pdev
, gpio
);
241 static int __devexit
da9052_gpio_remove(struct platform_device
*pdev
)
243 struct da9052_gpio
*gpio
= platform_get_drvdata(pdev
);
246 ret
= gpiochip_remove(&gpio
->gp
);
253 static struct platform_driver da9052_gpio_driver
= {
254 .probe
= da9052_gpio_probe
,
255 .remove
= __devexit_p(da9052_gpio_remove
),
257 .name
= "da9052-gpio",
258 .owner
= THIS_MODULE
,
262 static int __init
da9052_gpio_init(void)
264 return platform_driver_register(&da9052_gpio_driver
);
266 module_init(da9052_gpio_init
);
268 static void __exit
da9052_gpio_exit(void)
270 return platform_driver_unregister(&da9052_gpio_driver
);
272 module_exit(da9052_gpio_exit
);
274 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
275 MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
276 MODULE_LICENSE("GPL");
277 MODULE_ALIAS("platform:da9052-gpio");