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 if (ret & (1 << DA9052_GPIO_SHIFT_COUNT(offset)))
96 case DA9052_OUTPUT_PUSHPULL:
97 if (da9052_gpio_port_odd(offset))
98 return ret & DA9052_GPIO_ODD_PORT_MODE;
100 return ret & DA9052_GPIO_EVEN_PORT_MODE;
106 static void da9052_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
108 struct da9052_gpio *gpio = to_da9052_gpio(gc);
111 if (da9052_gpio_port_odd(offset)) {
112 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
114 DA9052_GPIO_ODD_PORT_MODE,
115 value << DA9052_GPIO_ODD_SHIFT);
117 dev_err(gpio->da9052->dev,
118 "Failed to updated gpio odd reg,%d",
121 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
123 DA9052_GPIO_EVEN_PORT_MODE,
124 value << DA9052_GPIO_EVEN_SHIFT);
126 dev_err(gpio->da9052->dev,
127 "Failed to updated gpio even reg,%d",
132 static int da9052_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
134 struct da9052_gpio *gpio = to_da9052_gpio(gc);
135 unsigned char register_value;
138 /* Format: function - 2 bits type - 1 bit mode - 1 bit */
139 register_value = DA9052_INPUT | DA9052_ACTIVE_LOW << 2 |
140 DA9052_DEBOUNCING_ON << 3;
142 if (da9052_gpio_port_odd(offset))
143 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
145 DA9052_GPIO_MASK_UPPER_NIBBLE,
147 DA9052_GPIO_NIBBLE_SHIFT));
149 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
151 DA9052_GPIO_MASK_LOWER_NIBBLE,
157 static int da9052_gpio_direction_output(struct gpio_chip *gc,
158 unsigned offset, int value)
160 struct da9052_gpio *gpio = to_da9052_gpio(gc);
161 unsigned char register_value;
164 /* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */
165 register_value = DA9052_OUTPUT_PUSHPULL | DA9052_SUPPLY_VDD_IO1 << 2 |
168 if (da9052_gpio_port_odd(offset))
169 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
171 DA9052_GPIO_MASK_UPPER_NIBBLE,
173 DA9052_GPIO_NIBBLE_SHIFT));
175 ret = da9052_reg_update(gpio->da9052, (offset >> 1) +
177 DA9052_GPIO_MASK_LOWER_NIBBLE,
183 static int da9052_gpio_to_irq(struct gpio_chip *gc, u32 offset)
185 struct da9052_gpio *gpio = to_da9052_gpio(gc);
186 struct da9052 *da9052 = gpio->da9052;
190 irq = regmap_irq_get_virq(da9052->irq_data, DA9052_IRQ_GPI0 + offset);
195 static struct gpio_chip reference_gp = {
196 .label = "da9052-gpio",
197 .owner = THIS_MODULE,
198 .get = da9052_gpio_get,
199 .set = da9052_gpio_set,
200 .direction_input = da9052_gpio_direction_input,
201 .direction_output = da9052_gpio_direction_output,
202 .to_irq = da9052_gpio_to_irq,
208 static int da9052_gpio_probe(struct platform_device *pdev)
210 struct da9052_gpio *gpio;
211 struct da9052_pdata *pdata;
214 gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
218 gpio->da9052 = dev_get_drvdata(pdev->dev.parent);
219 pdata = dev_get_platdata(gpio->da9052->dev);
221 gpio->gp = reference_gp;
222 if (pdata && pdata->gpio_base)
223 gpio->gp.base = pdata->gpio_base;
225 ret = gpiochip_add(&gpio->gp);
227 dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
231 platform_set_drvdata(pdev, gpio);
236 static int da9052_gpio_remove(struct platform_device *pdev)
238 struct da9052_gpio *gpio = platform_get_drvdata(pdev);
240 return gpiochip_remove(&gpio->gp);
243 static struct platform_driver da9052_gpio_driver = {
244 .probe = da9052_gpio_probe,
245 .remove = da9052_gpio_remove,
247 .name = "da9052-gpio",
248 .owner = THIS_MODULE,
252 module_platform_driver(da9052_gpio_driver);
254 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
255 MODULE_DESCRIPTION("DA9052 GPIO Device Driver");
256 MODULE_LICENSE("GPL");
257 MODULE_ALIAS("platform:da9052-gpio");