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>
26 #define DA9052_INPUT 1
27 #define DA9052_OUTPUT_OPENDRAIN 2
28 #define DA9052_OUTPUT_PUSHPULL 3
30 #define DA9052_SUPPLY_VDD_IO1 0
32 #define DA9052_DEBOUNCING_OFF 0
33 #define DA9052_DEBOUNCING_ON 1
35 #define DA9052_OUTPUT_LOWLEVEL 0
37 #define DA9052_ACTIVE_LOW 0
38 #define DA9052_ACTIVE_HIGH 1
40 #define DA9052_GPIO_MAX_PORTS_PER_REGISTER 8
41 #define DA9052_GPIO_SHIFT_COUNT(no) (no%8)
42 #define DA9052_GPIO_MASK_UPPER_NIBBLE 0xF0
43 #define DA9052_GPIO_MASK_LOWER_NIBBLE 0x0F
44 #define DA9052_GPIO_NIBBLE_SHIFT 4
45 #define DA9052_IRQ_GPI0 16
46 #define DA9052_GPIO_ODD_SHIFT 7
47 #define DA9052_GPIO_EVEN_SHIFT 3
50 struct da9052
*da9052
;
54 static unsigned char da9052_gpio_port_odd(unsigned offset
)
59 static int da9052_gpio_get(struct gpio_chip
*gc
, unsigned offset
)
61 struct da9052_gpio
*gpio
= gpiochip_get_data(gc
);
62 int da9052_port_direction
= 0;
65 ret
= da9052_reg_read(gpio
->da9052
,
66 DA9052_GPIO_0_1_REG
+ (offset
>> 1));
70 if (da9052_gpio_port_odd(offset
)) {
71 da9052_port_direction
= ret
& DA9052_GPIO_ODD_PORT_PIN
;
72 da9052_port_direction
>>= 4;
74 da9052_port_direction
= ret
& DA9052_GPIO_EVEN_PORT_PIN
;
77 switch (da9052_port_direction
) {
79 if (offset
< DA9052_GPIO_MAX_PORTS_PER_REGISTER
)
80 ret
= da9052_reg_read(gpio
->da9052
,
83 ret
= da9052_reg_read(gpio
->da9052
,
87 return !!(ret
& (1 << DA9052_GPIO_SHIFT_COUNT(offset
)));
88 case DA9052_OUTPUT_PUSHPULL
:
89 if (da9052_gpio_port_odd(offset
))
90 return !!(ret
& DA9052_GPIO_ODD_PORT_MODE
);
92 return !!(ret
& DA9052_GPIO_EVEN_PORT_MODE
);
98 static void da9052_gpio_set(struct gpio_chip
*gc
, unsigned offset
, int value
)
100 struct da9052_gpio
*gpio
= gpiochip_get_data(gc
);
103 if (da9052_gpio_port_odd(offset
)) {
104 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
106 DA9052_GPIO_ODD_PORT_MODE
,
107 value
<< DA9052_GPIO_ODD_SHIFT
);
109 dev_err(gpio
->da9052
->dev
,
110 "Failed to updated gpio odd reg,%d",
113 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
115 DA9052_GPIO_EVEN_PORT_MODE
,
116 value
<< DA9052_GPIO_EVEN_SHIFT
);
118 dev_err(gpio
->da9052
->dev
,
119 "Failed to updated gpio even reg,%d",
124 static int da9052_gpio_direction_input(struct gpio_chip
*gc
, unsigned offset
)
126 struct da9052_gpio
*gpio
= gpiochip_get_data(gc
);
127 unsigned char register_value
;
130 /* Format: function - 2 bits type - 1 bit mode - 1 bit */
131 register_value
= DA9052_INPUT
| DA9052_ACTIVE_LOW
<< 2 |
132 DA9052_DEBOUNCING_ON
<< 3;
134 if (da9052_gpio_port_odd(offset
))
135 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
137 DA9052_GPIO_MASK_UPPER_NIBBLE
,
139 DA9052_GPIO_NIBBLE_SHIFT
));
141 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
143 DA9052_GPIO_MASK_LOWER_NIBBLE
,
149 static int da9052_gpio_direction_output(struct gpio_chip
*gc
,
150 unsigned offset
, int value
)
152 struct da9052_gpio
*gpio
= gpiochip_get_data(gc
);
153 unsigned char register_value
;
156 /* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
157 register_value
= DA9052_OUTPUT_PUSHPULL
| DA9052_SUPPLY_VDD_IO1
<< 2 |
160 if (da9052_gpio_port_odd(offset
))
161 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
163 DA9052_GPIO_MASK_UPPER_NIBBLE
,
165 DA9052_GPIO_NIBBLE_SHIFT
));
167 ret
= da9052_reg_update(gpio
->da9052
, (offset
>> 1) +
169 DA9052_GPIO_MASK_LOWER_NIBBLE
,
175 static int da9052_gpio_to_irq(struct gpio_chip
*gc
, u32 offset
)
177 struct da9052_gpio
*gpio
= gpiochip_get_data(gc
);
178 struct da9052
*da9052
= gpio
->da9052
;
182 irq
= regmap_irq_get_virq(da9052
->irq_data
, DA9052_IRQ_GPI0
+ offset
);
187 static const struct gpio_chip reference_gp
= {
188 .label
= "da9052-gpio",
189 .owner
= THIS_MODULE
,
190 .get
= da9052_gpio_get
,
191 .set
= da9052_gpio_set
,
192 .direction_input
= da9052_gpio_direction_input
,
193 .direction_output
= da9052_gpio_direction_output
,
194 .to_irq
= da9052_gpio_to_irq
,
200 static int da9052_gpio_probe(struct platform_device
*pdev
)
202 struct da9052_gpio
*gpio
;
203 struct da9052_pdata
*pdata
;
206 gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio
), GFP_KERNEL
);
210 gpio
->da9052
= dev_get_drvdata(pdev
->dev
.parent
);
211 pdata
= dev_get_platdata(gpio
->da9052
->dev
);
213 gpio
->gp
= reference_gp
;
214 if (pdata
&& pdata
->gpio_base
)
215 gpio
->gp
.base
= pdata
->gpio_base
;
217 ret
= devm_gpiochip_add_data(&pdev
->dev
, &gpio
->gp
, gpio
);
219 dev_err(&pdev
->dev
, "Could not register gpiochip, %d\n", ret
);
223 platform_set_drvdata(pdev
, gpio
);
228 static struct platform_driver da9052_gpio_driver
= {
229 .probe
= da9052_gpio_probe
,
231 .name
= "da9052-gpio",
235 module_platform_driver(da9052_gpio_driver
);
237 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
238 MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
239 MODULE_LICENSE("GPL");
240 MODULE_ALIAS("platform:da9052-gpio");