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