ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 1 | /* |
| 2 | * GPIO Driver for Dialog DA9052 PMICs. |
| 3 | * |
| 4 | * Copyright(c) 2011 Dialog Semiconductor Ltd. |
| 5 | * |
| 6 | * Author: David Dajun Chen <dchen@diasemi.com> |
| 7 | * |
| 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. |
| 12 | * |
| 13 | */ |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/fs.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> |
| 21 | |
| 22 | #include <linux/mfd/da9052/da9052.h> |
| 23 | #include <linux/mfd/da9052/reg.h> |
| 24 | #include <linux/mfd/da9052/pdata.h> |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 25 | |
| 26 | #define DA9052_INPUT 1 |
| 27 | #define DA9052_OUTPUT_OPENDRAIN 2 |
| 28 | #define DA9052_OUTPUT_PUSHPULL 3 |
| 29 | |
| 30 | #define DA9052_SUPPLY_VDD_IO1 0 |
| 31 | |
| 32 | #define DA9052_DEBOUNCING_OFF 0 |
| 33 | #define DA9052_DEBOUNCING_ON 1 |
| 34 | |
| 35 | #define DA9052_OUTPUT_LOWLEVEL 0 |
| 36 | |
| 37 | #define DA9052_ACTIVE_LOW 0 |
| 38 | #define DA9052_ACTIVE_HIGH 1 |
| 39 | |
| 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 |
Ashish Jangam | 87b9b0e | 2011-12-15 14:55:46 +0530 | [diff] [blame] | 45 | #define DA9052_IRQ_GPI0 16 |
| 46 | #define DA9052_GPIO_ODD_SHIFT 7 |
| 47 | #define DA9052_GPIO_EVEN_SHIFT 3 |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 48 | |
| 49 | struct da9052_gpio { |
| 50 | struct da9052 *da9052; |
| 51 | struct gpio_chip gp; |
| 52 | }; |
| 53 | |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 54 | static unsigned char da9052_gpio_port_odd(unsigned offset) |
| 55 | { |
| 56 | return offset % 2; |
| 57 | } |
| 58 | |
| 59 | static int da9052_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 60 | { |
Linus Walleij | 0f6082d | 2015-12-04 16:19:52 +0100 | [diff] [blame] | 61 | struct da9052_gpio *gpio = gpiochip_get_data(gc); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 62 | int da9052_port_direction = 0; |
| 63 | int ret; |
| 64 | |
| 65 | ret = da9052_reg_read(gpio->da9052, |
| 66 | DA9052_GPIO_0_1_REG + (offset >> 1)); |
| 67 | if (ret < 0) |
| 68 | return ret; |
| 69 | |
| 70 | if (da9052_gpio_port_odd(offset)) { |
| 71 | da9052_port_direction = ret & DA9052_GPIO_ODD_PORT_PIN; |
| 72 | da9052_port_direction >>= 4; |
| 73 | } else { |
| 74 | da9052_port_direction = ret & DA9052_GPIO_EVEN_PORT_PIN; |
| 75 | } |
| 76 | |
| 77 | switch (da9052_port_direction) { |
| 78 | case DA9052_INPUT: |
| 79 | if (offset < DA9052_GPIO_MAX_PORTS_PER_REGISTER) |
| 80 | ret = da9052_reg_read(gpio->da9052, |
| 81 | DA9052_STATUS_C_REG); |
| 82 | else |
| 83 | ret = da9052_reg_read(gpio->da9052, |
| 84 | DA9052_STATUS_D_REG); |
| 85 | if (ret < 0) |
| 86 | return ret; |
Linus Walleij | 1f66adf | 2015-12-21 10:30:02 +0100 | [diff] [blame] | 87 | return !!(ret & (1 << DA9052_GPIO_SHIFT_COUNT(offset))); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 88 | case DA9052_OUTPUT_PUSHPULL: |
| 89 | if (da9052_gpio_port_odd(offset)) |
Linus Walleij | 1f66adf | 2015-12-21 10:30:02 +0100 | [diff] [blame] | 90 | return !!(ret & DA9052_GPIO_ODD_PORT_MODE); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 91 | else |
Linus Walleij | 1f66adf | 2015-12-21 10:30:02 +0100 | [diff] [blame] | 92 | return !!(ret & DA9052_GPIO_EVEN_PORT_MODE); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 93 | default: |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static void da9052_gpio_set(struct gpio_chip *gc, unsigned offset, int value) |
| 99 | { |
Linus Walleij | 0f6082d | 2015-12-04 16:19:52 +0100 | [diff] [blame] | 100 | struct da9052_gpio *gpio = gpiochip_get_data(gc); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 101 | int ret; |
| 102 | |
| 103 | if (da9052_gpio_port_odd(offset)) { |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 104 | ret = da9052_reg_update(gpio->da9052, (offset >> 1) + |
| 105 | DA9052_GPIO_0_1_REG, |
| 106 | DA9052_GPIO_ODD_PORT_MODE, |
Ashish Jangam | 87b9b0e | 2011-12-15 14:55:46 +0530 | [diff] [blame] | 107 | value << DA9052_GPIO_ODD_SHIFT); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 108 | if (ret != 0) |
| 109 | dev_err(gpio->da9052->dev, |
| 110 | "Failed to updated gpio odd reg,%d", |
| 111 | ret); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 112 | } else { |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 113 | ret = da9052_reg_update(gpio->da9052, (offset >> 1) + |
| 114 | DA9052_GPIO_0_1_REG, |
| 115 | DA9052_GPIO_EVEN_PORT_MODE, |
Ashish Jangam | 87b9b0e | 2011-12-15 14:55:46 +0530 | [diff] [blame] | 116 | value << DA9052_GPIO_EVEN_SHIFT); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 117 | if (ret != 0) |
| 118 | dev_err(gpio->da9052->dev, |
| 119 | "Failed to updated gpio even reg,%d", |
| 120 | ret); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | static int da9052_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 125 | { |
Linus Walleij | 0f6082d | 2015-12-04 16:19:52 +0100 | [diff] [blame] | 126 | struct da9052_gpio *gpio = gpiochip_get_data(gc); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 127 | unsigned char register_value; |
| 128 | int ret; |
| 129 | |
| 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; |
| 133 | |
| 134 | if (da9052_gpio_port_odd(offset)) |
| 135 | ret = da9052_reg_update(gpio->da9052, (offset >> 1) + |
| 136 | DA9052_GPIO_0_1_REG, |
| 137 | DA9052_GPIO_MASK_UPPER_NIBBLE, |
| 138 | (register_value << |
| 139 | DA9052_GPIO_NIBBLE_SHIFT)); |
| 140 | else |
| 141 | ret = da9052_reg_update(gpio->da9052, (offset >> 1) + |
| 142 | DA9052_GPIO_0_1_REG, |
| 143 | DA9052_GPIO_MASK_LOWER_NIBBLE, |
| 144 | register_value); |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | static int da9052_gpio_direction_output(struct gpio_chip *gc, |
| 150 | unsigned offset, int value) |
| 151 | { |
Linus Walleij | 0f6082d | 2015-12-04 16:19:52 +0100 | [diff] [blame] | 152 | struct da9052_gpio *gpio = gpiochip_get_data(gc); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 153 | unsigned char register_value; |
| 154 | int ret; |
| 155 | |
| 156 | /* Format: Function - 2 bits Type - 1 bit Mode - 1 bit */ |
| 157 | register_value = DA9052_OUTPUT_PUSHPULL | DA9052_SUPPLY_VDD_IO1 << 2 | |
| 158 | value << 3; |
| 159 | |
| 160 | if (da9052_gpio_port_odd(offset)) |
| 161 | ret = da9052_reg_update(gpio->da9052, (offset >> 1) + |
| 162 | DA9052_GPIO_0_1_REG, |
| 163 | DA9052_GPIO_MASK_UPPER_NIBBLE, |
| 164 | (register_value << |
| 165 | DA9052_GPIO_NIBBLE_SHIFT)); |
| 166 | else |
| 167 | ret = da9052_reg_update(gpio->da9052, (offset >> 1) + |
| 168 | DA9052_GPIO_0_1_REG, |
| 169 | DA9052_GPIO_MASK_LOWER_NIBBLE, |
| 170 | register_value); |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | static int da9052_gpio_to_irq(struct gpio_chip *gc, u32 offset) |
| 176 | { |
Linus Walleij | 0f6082d | 2015-12-04 16:19:52 +0100 | [diff] [blame] | 177 | struct da9052_gpio *gpio = gpiochip_get_data(gc); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 178 | struct da9052 *da9052 = gpio->da9052; |
| 179 | |
Fabio Estevam | d511b9c | 2012-10-04 00:15:09 -0300 | [diff] [blame] | 180 | int irq; |
| 181 | |
| 182 | irq = regmap_irq_get_virq(da9052->irq_data, DA9052_IRQ_GPI0 + offset); |
| 183 | |
| 184 | return irq; |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 185 | } |
| 186 | |
Julia Lawall | e35b5ab | 2016-09-11 14:14:37 +0200 | [diff] [blame] | 187 | static const struct gpio_chip reference_gp = { |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 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, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 195 | .can_sleep = true, |
Ashish Jangam | 87b9b0e | 2011-12-15 14:55:46 +0530 | [diff] [blame] | 196 | .ngpio = 16, |
| 197 | .base = -1, |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 198 | }; |
| 199 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 200 | static int da9052_gpio_probe(struct platform_device *pdev) |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 201 | { |
| 202 | struct da9052_gpio *gpio; |
| 203 | struct da9052_pdata *pdata; |
| 204 | int ret; |
| 205 | |
Axel Lin | eb7cf95 | 2012-09-01 17:34:15 +0800 | [diff] [blame] | 206 | gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); |
Varka Bhadram | afeb7b4 | 2015-03-31 09:49:11 +0530 | [diff] [blame] | 207 | if (!gpio) |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 208 | return -ENOMEM; |
| 209 | |
| 210 | gpio->da9052 = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 211 | pdata = dev_get_platdata(gpio->da9052->dev); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 212 | |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 213 | gpio->gp = reference_gp; |
| 214 | if (pdata && pdata->gpio_base) |
| 215 | gpio->gp.base = pdata->gpio_base; |
| 216 | |
Laxman Dewangan | b9cb1bb | 2016-02-22 17:43:28 +0530 | [diff] [blame] | 217 | ret = devm_gpiochip_add_data(&pdev->dev, &gpio->gp, gpio); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 218 | if (ret < 0) { |
| 219 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
Axel Lin | eb7cf95 | 2012-09-01 17:34:15 +0800 | [diff] [blame] | 220 | return ret; |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | platform_set_drvdata(pdev, gpio); |
| 224 | |
| 225 | return 0; |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 226 | } |
| 227 | |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 228 | static struct platform_driver da9052_gpio_driver = { |
| 229 | .probe = da9052_gpio_probe, |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 230 | .driver = { |
| 231 | .name = "da9052-gpio", |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 232 | }, |
| 233 | }; |
| 234 | |
Mark Brown | 6f61415 | 2011-12-08 00:24:00 +0800 | [diff] [blame] | 235 | module_platform_driver(da9052_gpio_driver); |
ashishj3 | 07bfc91 | 2011-07-06 16:02:13 +0530 | [diff] [blame] | 236 | |
| 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"); |