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