drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / gpio / gpio-adp5520.c
blobc55e821c63b6663a7884e0549a2d2e63c2bfbd6b
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * GPIO driver for Analog Devices ADP5520 MFD PMICs
5 * Copyright 2009 Analog Devices Inc.
6 */
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/platform_device.h>
13 #include <linux/mfd/adp5520.h>
14 #include <linux/gpio/driver.h>
16 struct adp5520_gpio {
17 struct device *master;
18 struct gpio_chip gpio_chip;
19 unsigned char lut[ADP5520_MAXGPIOS];
20 unsigned long output;
23 static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
25 struct adp5520_gpio *dev;
26 uint8_t reg_val;
28 dev = gpiochip_get_data(chip);
31 * There are dedicated registers for GPIO IN/OUT.
32 * Make sure we return the right value, even when configured as output
35 if (test_bit(off, &dev->output))
36 adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
37 else
38 adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
40 return !!(reg_val & dev->lut[off]);
43 static void adp5520_gpio_set_value(struct gpio_chip *chip,
44 unsigned off, int val)
46 struct adp5520_gpio *dev;
47 dev = gpiochip_get_data(chip);
49 if (val)
50 adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
51 else
52 adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
55 static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
57 struct adp5520_gpio *dev;
58 dev = gpiochip_get_data(chip);
60 clear_bit(off, &dev->output);
62 return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
63 dev->lut[off]);
66 static int adp5520_gpio_direction_output(struct gpio_chip *chip,
67 unsigned off, int val)
69 struct adp5520_gpio *dev;
70 int ret = 0;
71 dev = gpiochip_get_data(chip);
73 set_bit(off, &dev->output);
75 if (val)
76 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
77 dev->lut[off]);
78 else
79 ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
80 dev->lut[off]);
82 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
83 dev->lut[off]);
85 return ret;
88 static int adp5520_gpio_probe(struct platform_device *pdev)
90 struct adp5520_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
91 struct adp5520_gpio *dev;
92 struct gpio_chip *gc;
93 int ret, i, gpios;
94 unsigned char ctl_mask = 0;
96 if (pdata == NULL) {
97 dev_err(&pdev->dev, "missing platform data\n");
98 return -ENODEV;
101 if (pdev->id != ID_ADP5520) {
102 dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
103 return -ENODEV;
106 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
107 if (dev == NULL)
108 return -ENOMEM;
110 dev->master = pdev->dev.parent;
112 for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
113 if (pdata->gpio_en_mask & (1 << i))
114 dev->lut[gpios++] = 1 << i;
116 if (gpios < 1)
117 return -EINVAL;
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;
124 gc->can_sleep = true;
126 gc->base = pdata->gpio_start;
127 gc->ngpio = gpios;
128 gc->label = pdev->name;
129 gc->owner = THIS_MODULE;
131 ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
132 pdata->gpio_en_mask);
134 if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
135 ctl_mask |= ADP5520_C3_MODE;
137 if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
138 ctl_mask |= ADP5520_R3_MODE;
140 if (ctl_mask)
141 ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
142 ctl_mask);
144 ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
145 pdata->gpio_pullup_mask);
147 if (ret) {
148 dev_err(&pdev->dev, "failed to write\n");
149 return ret;
152 return devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev);
155 static struct platform_driver adp5520_gpio_driver = {
156 .driver = {
157 .name = "adp5520-gpio",
159 .probe = adp5520_gpio_probe,
162 module_platform_driver(adp5520_gpio_driver);
164 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
165 MODULE_DESCRIPTION("GPIO ADP5520 Driver");
166 MODULE_LICENSE("GPL");
167 MODULE_ALIAS("platform:adp5520-gpio");