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 inline struct da9052_gpio *to_da9052_gpio(struct gpio_chip *chip)
56 return container_of(chip, struct da9052_gpio, gp);
59 static unsigned char da9052_gpio_port_odd(unsigned offset)
64 static int da9052_gpio_get(struct gpio_chip *gc, unsigned offset)
66 struct da9052_gpio *gpio = to_da9052_gpio(gc);
67 int da9052_port_direction = 0;
70 ret = da9052_reg_read(gpio->da9052,
71 DA9052_GPIO_0_1_REG + (offset >> 1));
75 if (da9052_gpio_port_odd(offset)) {
76 da9052_port_direction = ret & DA9052_GPIO_ODD_PORT_PIN;
77 da9052_port_direction >>= 4;
79 da9052_port_direction = ret & DA9052_GPIO_EVEN_PORT_PIN;
82 switch (da9052_port_direction) {
84 if (offset < DA9052_GPIO_MAX_PORTS_PER_REGISTER)
85 ret = da9052_reg_read(gpio->da9052,
88 ret = da9052_reg_read(gpio->da9052,
92 return !!(ret & (1 << DA9052_GPIO_SHIFT_COUNT(offset)));
93 case DA9052_OUTPUT_PUSHPULL:
94 if (da9052_gpio_port_odd(offset))
95 return !!(ret & DA9052_GPIO_ODD_PORT_MODE);
97 return !!(ret & DA9052_GPIO_EVEN_PORT_MODE);
103 static void da9052_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
105 struct da9052_gpio *gpio = to_da9052_gpio(gc);
108 if (da9052_gpio_port_odd(offset)) {
109 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
111 DA9052_GPIO_ODD_PORT_MODE,
112 value << DA9052_GPIO_ODD_SHIFT);
114 dev_err(gpio->da9052->dev,
115 "Failed to updated gpio odd reg,%d",
118 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
120 DA9052_GPIO_EVEN_PORT_MODE,
121 value << DA9052_GPIO_EVEN_SHIFT);
123 dev_err(gpio->da9052->dev,
124 "Failed to updated gpio even reg,%d",
129 static int da9052_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
131 struct da9052_gpio *gpio = to_da9052_gpio(gc);
132 unsigned char register_value;
135 /* Format: function - 2 bits type - 1 bit mode - 1 bit */
136 register_value = DA9052_INPUT | DA9052_ACTIVE_LOW << 2 |
137 DA9052_DEBOUNCING_ON << 3;
139 if (da9052_gpio_port_odd(offset))
140 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
142 DA9052_GPIO_MASK_UPPER_NIBBLE,
144 DA9052_GPIO_NIBBLE_SHIFT));
146 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
148 DA9052_GPIO_MASK_LOWER_NIBBLE,
154 static int da9052_gpio_direction_output(struct gpio_chip *gc,
155 unsigned offset, int value)
157 struct da9052_gpio *gpio = to_da9052_gpio(gc);
158 unsigned char register_value;
161 /* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
162 register_value = DA9052_OUTPUT_PUSHPULL | DA9052_SUPPLY_VDD_IO1 << 2 |
165 if (da9052_gpio_port_odd(offset))
166 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
168 DA9052_GPIO_MASK_UPPER_NIBBLE,
170 DA9052_GPIO_NIBBLE_SHIFT));
172 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
174 DA9052_GPIO_MASK_LOWER_NIBBLE,
180 static int da9052_gpio_to_irq(struct gpio_chip *gc, u32 offset)
182 struct da9052_gpio *gpio = to_da9052_gpio(gc);
183 struct da9052 *da9052 = gpio->da9052;
187 irq = regmap_irq_get_virq(da9052->irq_data, DA9052_IRQ_GPI0 + offset);
192 static struct gpio_chip reference_gp = {
193 .label = "da9052-gpio",
194 .owner = THIS_MODULE,
195 .get = da9052_gpio_get,
196 .set = da9052_gpio_set,
197 .direction_input = da9052_gpio_direction_input,
198 .direction_output = da9052_gpio_direction_output,
199 .to_irq = da9052_gpio_to_irq,
205 static int da9052_gpio_probe(struct platform_device *pdev)
207 struct da9052_gpio *gpio;
208 struct da9052_pdata *pdata;
211 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
215 gpio->da9052 = dev_get_drvdata(pdev->dev.parent);
216 pdata = dev_get_platdata(gpio->da9052->dev);
218 gpio->gp = reference_gp;
219 if (pdata && pdata->gpio_base)
220 gpio->gp.base = pdata->gpio_base;
222 ret = gpiochip_add(&gpio->gp);
224 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
228 platform_set_drvdata(pdev, gpio);
233 static int da9052_gpio_remove(struct platform_device *pdev)
235 struct da9052_gpio *gpio = platform_get_drvdata(pdev);
237 gpiochip_remove(&gpio->gp);
241 static struct platform_driver da9052_gpio_driver = {
242 .probe = da9052_gpio_probe,
243 .remove = da9052_gpio_remove,
245 .name = "da9052-gpio",
249 module_platform_driver(da9052_gpio_driver);
251 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
252 MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
253 MODULE_LICENSE("GPL");
254 MODULE_ALIAS("platform:da9052-gpio");