Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 1 | /* |
| 2 | * GPIO Driver for Dialog DA9055 PMICs. |
| 3 | * |
| 4 | * Copyright(c) 2012 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/platform_device.h> |
| 16 | #include <linux/gpio.h> |
| 17 | |
| 18 | #include <linux/mfd/da9055/core.h> |
| 19 | #include <linux/mfd/da9055/reg.h> |
| 20 | #include <linux/mfd/da9055/pdata.h> |
| 21 | |
| 22 | #define DA9055_VDD_IO 0x0 |
| 23 | #define DA9055_PUSH_PULL 0x3 |
| 24 | #define DA9055_ACT_LOW 0x0 |
| 25 | #define DA9055_GPI 0x1 |
| 26 | #define DA9055_PORT_MASK 0x3 |
| 27 | #define DA9055_PORT_SHIFT(offset) (4 * (offset % 2)) |
| 28 | |
| 29 | #define DA9055_INPUT DA9055_GPI |
| 30 | #define DA9055_OUTPUT DA9055_PUSH_PULL |
| 31 | #define DA9055_IRQ_GPI0 3 |
| 32 | |
| 33 | struct da9055_gpio { |
| 34 | struct da9055 *da9055; |
| 35 | struct gpio_chip gp; |
| 36 | }; |
| 37 | |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 38 | static int da9055_gpio_get(struct gpio_chip *gc, unsigned offset) |
| 39 | { |
Linus Walleij | 552dd79 | 2015-12-04 16:21:53 +0100 | [diff] [blame] | 40 | struct da9055_gpio *gpio = gpiochip_get_data(gc); |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 41 | int gpio_direction = 0; |
| 42 | int ret; |
| 43 | |
| 44 | /* Get GPIO direction */ |
| 45 | ret = da9055_reg_read(gpio->da9055, (offset >> 1) + DA9055_REG_GPIO0_1); |
| 46 | if (ret < 0) |
| 47 | return ret; |
| 48 | |
| 49 | gpio_direction = ret & (DA9055_PORT_MASK) << DA9055_PORT_SHIFT(offset); |
| 50 | gpio_direction >>= DA9055_PORT_SHIFT(offset); |
| 51 | switch (gpio_direction) { |
| 52 | case DA9055_INPUT: |
| 53 | ret = da9055_reg_read(gpio->da9055, DA9055_REG_STATUS_B); |
| 54 | if (ret < 0) |
| 55 | return ret; |
| 56 | break; |
| 57 | case DA9055_OUTPUT: |
| 58 | ret = da9055_reg_read(gpio->da9055, DA9055_REG_GPIO_MODE0_2); |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | return ret & (1 << offset); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | static void da9055_gpio_set(struct gpio_chip *gc, unsigned offset, int value) |
| 68 | { |
Linus Walleij | 552dd79 | 2015-12-04 16:21:53 +0100 | [diff] [blame] | 69 | struct da9055_gpio *gpio = gpiochip_get_data(gc); |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 70 | |
| 71 | da9055_reg_update(gpio->da9055, |
| 72 | DA9055_REG_GPIO_MODE0_2, |
| 73 | 1 << offset, |
| 74 | value << offset); |
| 75 | } |
| 76 | |
| 77 | static int da9055_gpio_direction_input(struct gpio_chip *gc, unsigned offset) |
| 78 | { |
Linus Walleij | 552dd79 | 2015-12-04 16:21:53 +0100 | [diff] [blame] | 79 | struct da9055_gpio *gpio = gpiochip_get_data(gc); |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 80 | unsigned char reg_byte; |
| 81 | |
| 82 | reg_byte = (DA9055_ACT_LOW | DA9055_GPI) |
| 83 | << DA9055_PORT_SHIFT(offset); |
| 84 | |
| 85 | return da9055_reg_update(gpio->da9055, (offset >> 1) + |
| 86 | DA9055_REG_GPIO0_1, |
| 87 | DA9055_PORT_MASK << |
| 88 | DA9055_PORT_SHIFT(offset), |
| 89 | reg_byte); |
| 90 | } |
| 91 | |
| 92 | static int da9055_gpio_direction_output(struct gpio_chip *gc, |
| 93 | unsigned offset, int value) |
| 94 | { |
Linus Walleij | 552dd79 | 2015-12-04 16:21:53 +0100 | [diff] [blame] | 95 | struct da9055_gpio *gpio = gpiochip_get_data(gc); |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 96 | unsigned char reg_byte; |
| 97 | int ret; |
| 98 | |
| 99 | reg_byte = (DA9055_VDD_IO | DA9055_PUSH_PULL) |
| 100 | << DA9055_PORT_SHIFT(offset); |
| 101 | |
| 102 | ret = da9055_reg_update(gpio->da9055, (offset >> 1) + |
| 103 | DA9055_REG_GPIO0_1, |
| 104 | DA9055_PORT_MASK << |
| 105 | DA9055_PORT_SHIFT(offset), |
| 106 | reg_byte); |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | |
| 110 | da9055_gpio_set(gc, offset, value); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int da9055_gpio_to_irq(struct gpio_chip *gc, u32 offset) |
| 116 | { |
Linus Walleij | 552dd79 | 2015-12-04 16:21:53 +0100 | [diff] [blame] | 117 | struct da9055_gpio *gpio = gpiochip_get_data(gc); |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 118 | struct da9055 *da9055 = gpio->da9055; |
| 119 | |
| 120 | return regmap_irq_get_virq(da9055->irq_data, |
| 121 | DA9055_IRQ_GPI0 + offset); |
| 122 | } |
| 123 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 124 | static struct gpio_chip reference_gp = { |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 125 | .label = "da9055-gpio", |
| 126 | .owner = THIS_MODULE, |
| 127 | .get = da9055_gpio_get, |
| 128 | .set = da9055_gpio_set, |
| 129 | .direction_input = da9055_gpio_direction_input, |
| 130 | .direction_output = da9055_gpio_direction_output, |
| 131 | .to_irq = da9055_gpio_to_irq, |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 132 | .can_sleep = true, |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 133 | .ngpio = 3, |
| 134 | .base = -1, |
| 135 | }; |
| 136 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 137 | static int da9055_gpio_probe(struct platform_device *pdev) |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 138 | { |
| 139 | struct da9055_gpio *gpio; |
| 140 | struct da9055_pdata *pdata; |
| 141 | int ret; |
| 142 | |
| 143 | gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); |
Varka Bhadram | afeb7b4 | 2015-03-31 09:49:11 +0530 | [diff] [blame] | 144 | if (!gpio) |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 145 | return -ENOMEM; |
| 146 | |
| 147 | gpio->da9055 = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 148 | pdata = dev_get_platdata(gpio->da9055->dev); |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 149 | |
| 150 | gpio->gp = reference_gp; |
| 151 | if (pdata && pdata->gpio_base) |
| 152 | gpio->gp.base = pdata->gpio_base; |
| 153 | |
Linus Walleij | 552dd79 | 2015-12-04 16:21:53 +0100 | [diff] [blame] | 154 | ret = gpiochip_add_data(&gpio->gp, gpio); |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 155 | if (ret < 0) { |
| 156 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); |
| 157 | goto err_mem; |
| 158 | } |
| 159 | |
| 160 | platform_set_drvdata(pdev, gpio); |
| 161 | |
| 162 | return 0; |
| 163 | |
| 164 | err_mem: |
| 165 | return ret; |
| 166 | } |
| 167 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 168 | static int da9055_gpio_remove(struct platform_device *pdev) |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 169 | { |
| 170 | struct da9055_gpio *gpio = platform_get_drvdata(pdev); |
| 171 | |
abdoulaye berthe | 9f5132a | 2014-07-12 22:30:12 +0200 | [diff] [blame] | 172 | gpiochip_remove(&gpio->gp); |
| 173 | return 0; |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static struct platform_driver da9055_gpio_driver = { |
| 177 | .probe = da9055_gpio_probe, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 178 | .remove = da9055_gpio_remove, |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 179 | .driver = { |
| 180 | .name = "da9055-gpio", |
Ashish Jangam | 04ed427 | 2012-09-14 19:00:16 +0530 | [diff] [blame] | 181 | }, |
| 182 | }; |
| 183 | |
| 184 | static int __init da9055_gpio_init(void) |
| 185 | { |
| 186 | return platform_driver_register(&da9055_gpio_driver); |
| 187 | } |
| 188 | subsys_initcall(da9055_gpio_init); |
| 189 | |
| 190 | static void __exit da9055_gpio_exit(void) |
| 191 | { |
| 192 | platform_driver_unregister(&da9055_gpio_driver); |
| 193 | } |
| 194 | module_exit(da9055_gpio_exit); |
| 195 | |
| 196 | MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>"); |
| 197 | MODULE_DESCRIPTION("DA9055 GPIO Device Driver"); |
| 198 | MODULE_LICENSE("GPL"); |
| 199 | MODULE_ALIAS("platform:da9055-gpio"); |