Thomas Gleixner | 80503b2 | 2019-05-24 12:04:09 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * GPIO driver for Analog Devices ADP5520 MFD PMICs |
| 4 | * |
| 5 | * Copyright 2009 Analog Devices Inc. |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/mfd/adp5520.h> |
Linus Walleij | 8a3b4f2 | 2018-01-13 22:12:07 +0100 | [diff] [blame] | 14 | #include <linux/gpio/driver.h> |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 15 | |
| 16 | struct adp5520_gpio { |
| 17 | struct device *master; |
| 18 | struct gpio_chip gpio_chip; |
| 19 | unsigned char lut[ADP5520_MAXGPIOS]; |
| 20 | unsigned long output; |
| 21 | }; |
| 22 | |
| 23 | static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off) |
| 24 | { |
| 25 | struct adp5520_gpio *dev; |
| 26 | uint8_t reg_val; |
| 27 | |
Linus Walleij | 5060e0e | 2015-12-04 15:01:50 +0100 | [diff] [blame] | 28 | dev = gpiochip_get_data(chip); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * There are dedicated registers for GPIO IN/OUT. |
| 32 | * Make sure we return the right value, even when configured as output |
| 33 | */ |
| 34 | |
| 35 | if (test_bit(off, &dev->output)) |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 36 | adp5520_read(dev->master, ADP5520_GPIO_OUT, ®_val); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 37 | else |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 38 | adp5520_read(dev->master, ADP5520_GPIO_IN, ®_val); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 39 | |
| 40 | return !!(reg_val & dev->lut[off]); |
| 41 | } |
| 42 | |
| 43 | static void adp5520_gpio_set_value(struct gpio_chip *chip, |
| 44 | unsigned off, int val) |
| 45 | { |
| 46 | struct adp5520_gpio *dev; |
Linus Walleij | 5060e0e | 2015-12-04 15:01:50 +0100 | [diff] [blame] | 47 | dev = gpiochip_get_data(chip); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 48 | |
| 49 | if (val) |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 50 | adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 51 | else |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 52 | adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off) |
| 56 | { |
| 57 | struct adp5520_gpio *dev; |
Linus Walleij | 5060e0e | 2015-12-04 15:01:50 +0100 | [diff] [blame] | 58 | dev = gpiochip_get_data(chip); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 59 | |
| 60 | clear_bit(off, &dev->output); |
| 61 | |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 62 | return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2, |
| 63 | dev->lut[off]); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static int adp5520_gpio_direction_output(struct gpio_chip *chip, |
| 67 | unsigned off, int val) |
| 68 | { |
| 69 | struct adp5520_gpio *dev; |
| 70 | int ret = 0; |
Linus Walleij | 5060e0e | 2015-12-04 15:01:50 +0100 | [diff] [blame] | 71 | dev = gpiochip_get_data(chip); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 72 | |
| 73 | set_bit(off, &dev->output); |
| 74 | |
| 75 | if (val) |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 76 | ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, |
| 77 | dev->lut[off]); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 78 | else |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 79 | ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, |
| 80 | dev->lut[off]); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 81 | |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 82 | ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2, |
| 83 | dev->lut[off]); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
Bill Pemberton | 3836309 | 2012-11-19 13:22:34 -0500 | [diff] [blame] | 88 | static int adp5520_gpio_probe(struct platform_device *pdev) |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 89 | { |
Jingoo Han | e56aee1 | 2013-07-30 17:08:05 +0900 | [diff] [blame] | 90 | struct adp5520_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 91 | struct adp5520_gpio *dev; |
| 92 | struct gpio_chip *gc; |
| 93 | int ret, i, gpios; |
| 94 | unsigned char ctl_mask = 0; |
| 95 | |
| 96 | if (pdata == NULL) { |
| 97 | dev_err(&pdev->dev, "missing platform data\n"); |
| 98 | return -ENODEV; |
| 99 | } |
| 100 | |
| 101 | if (pdev->id != ID_ADP5520) { |
| 102 | dev_err(&pdev->dev, "only ADP5520 supports GPIO\n"); |
| 103 | return -ENODEV; |
| 104 | } |
| 105 | |
Jingoo Han | 24bb381 | 2013-03-15 18:14:01 +0900 | [diff] [blame] | 106 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
Jingoo Han | e8a71aa | 2014-04-29 17:31:31 +0900 | [diff] [blame] | 107 | if (dev == NULL) |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 108 | return -ENOMEM; |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 109 | |
| 110 | dev->master = pdev->dev.parent; |
| 111 | |
| 112 | for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++) |
| 113 | if (pdata->gpio_en_mask & (1 << i)) |
| 114 | dev->lut[gpios++] = 1 << i; |
| 115 | |
Alexandru Ardelean | 6681db5 | 2021-05-14 11:56:27 +0300 | [diff] [blame] | 116 | if (gpios < 1) |
| 117 | return -EINVAL; |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 118 | |
| 119 | gc = &dev->gpio_chip; |
| 120 | gc->direction_input = adp5520_gpio_direction_input; |
| 121 | gc->direction_output = adp5520_gpio_direction_output; |
| 122 | gc->get = adp5520_gpio_get_value; |
| 123 | gc->set = adp5520_gpio_set_value; |
Linus Walleij | 9fb1f39 | 2013-12-04 14:42:46 +0100 | [diff] [blame] | 124 | gc->can_sleep = true; |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 125 | |
| 126 | gc->base = pdata->gpio_start; |
| 127 | gc->ngpio = gpios; |
| 128 | gc->label = pdev->name; |
| 129 | gc->owner = THIS_MODULE; |
| 130 | |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 131 | ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1, |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 132 | pdata->gpio_en_mask); |
| 133 | |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 134 | if (pdata->gpio_en_mask & ADP5520_GPIO_C3) |
| 135 | ctl_mask |= ADP5520_C3_MODE; |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 136 | |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 137 | if (pdata->gpio_en_mask & ADP5520_GPIO_R3) |
| 138 | ctl_mask |= ADP5520_R3_MODE; |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 139 | |
| 140 | if (ctl_mask) |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 141 | ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL, |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 142 | ctl_mask); |
| 143 | |
Michael Hennerich | 7c29a47 | 2009-10-10 13:54:03 -0400 | [diff] [blame] | 144 | ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP, |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 145 | pdata->gpio_pullup_mask); |
| 146 | |
| 147 | if (ret) { |
| 148 | dev_err(&pdev->dev, "failed to write\n"); |
Alexandru Ardelean | 6681db5 | 2021-05-14 11:56:27 +0300 | [diff] [blame] | 149 | return ret; |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Alexandru Ardelean | 6681db5 | 2021-05-14 11:56:27 +0300 | [diff] [blame] | 152 | return devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 155 | static struct platform_driver adp5520_gpio_driver = { |
| 156 | .driver = { |
| 157 | .name = "adp5520-gpio", |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 158 | }, |
| 159 | .probe = adp5520_gpio_probe, |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
Mark Brown | 6f61415 | 2011-12-08 00:24:00 +0800 | [diff] [blame] | 162 | module_platform_driver(adp5520_gpio_driver); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 163 | |
Michael Hennerich | be88784 | 2018-08-14 13:29:18 +0200 | [diff] [blame] | 164 | MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); |
Michael Hennerich | ef72af4 | 2009-09-22 16:46:39 -0700 | [diff] [blame] | 165 | MODULE_DESCRIPTION("GPIO ADP5520 Driver"); |
| 166 | MODULE_LICENSE("GPL"); |
| 167 | MODULE_ALIAS("platform:adp5520-gpio"); |