Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

Feb 18-20, 2025
Register
Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * GPIO driver for Analog Devices ADP5520 MFD PMICs
  4 *
  5 * Copyright 2009 Analog Devices Inc.
 
 
  6 */
  7
  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>
 15
 16struct adp5520_gpio {
 17	struct device *master;
 18	struct gpio_chip gpio_chip;
 19	unsigned char lut[ADP5520_MAXGPIOS];
 20	unsigned long output;
 21};
 22
 23static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
 24{
 25	struct adp5520_gpio *dev;
 26	uint8_t reg_val;
 27
 28	dev = gpiochip_get_data(chip);
 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))
 36		adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
 37	else
 38		adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
 39
 40	return !!(reg_val & dev->lut[off]);
 41}
 42
 43static void adp5520_gpio_set_value(struct gpio_chip *chip,
 44		unsigned off, int val)
 45{
 46	struct adp5520_gpio *dev;
 47	dev = gpiochip_get_data(chip);
 48
 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]);
 53}
 54
 55static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
 56{
 57	struct adp5520_gpio *dev;
 58	dev = gpiochip_get_data(chip);
 59
 60	clear_bit(off, &dev->output);
 61
 62	return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
 63				dev->lut[off]);
 64}
 65
 66static int adp5520_gpio_direction_output(struct gpio_chip *chip,
 67		unsigned off, int val)
 68{
 69	struct adp5520_gpio *dev;
 70	int ret = 0;
 71	dev = gpiochip_get_data(chip);
 72
 73	set_bit(off, &dev->output);
 74
 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]);
 81
 82	ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
 83					dev->lut[off]);
 84
 85	return ret;
 86}
 87
 88static int adp5520_gpio_probe(struct platform_device *pdev)
 89{
 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;
 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
106	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
107	if (dev == NULL)
108		return -ENOMEM;
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
116	if (gpios < 1) {
117		ret = -EINVAL;
118		goto err;
119	}
120
121	gc = &dev->gpio_chip;
122	gc->direction_input  = adp5520_gpio_direction_input;
123	gc->direction_output = adp5520_gpio_direction_output;
124	gc->get = adp5520_gpio_get_value;
125	gc->set = adp5520_gpio_set_value;
126	gc->can_sleep = true;
127
128	gc->base = pdata->gpio_start;
129	gc->ngpio = gpios;
130	gc->label = pdev->name;
131	gc->owner = THIS_MODULE;
132
133	ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
134		pdata->gpio_en_mask);
135
136	if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
137		ctl_mask |= ADP5520_C3_MODE;
138
139	if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
140		ctl_mask |= ADP5520_R3_MODE;
141
142	if (ctl_mask)
143		ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
144			ctl_mask);
145
146	ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
147		pdata->gpio_pullup_mask);
148
149	if (ret) {
150		dev_err(&pdev->dev, "failed to write\n");
151		goto err;
152	}
153
154	ret = devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev);
155	if (ret)
156		goto err;
157
158	platform_set_drvdata(pdev, dev);
159	return 0;
160
161err:
162	return ret;
163}
164
165static struct platform_driver adp5520_gpio_driver = {
166	.driver	= {
167		.name	= "adp5520-gpio",
168	},
169	.probe		= adp5520_gpio_probe,
170};
171
172module_platform_driver(adp5520_gpio_driver);
173
174MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
175MODULE_DESCRIPTION("GPIO ADP5520 Driver");
176MODULE_LICENSE("GPL");
177MODULE_ALIAS("platform:adp5520-gpio");
v4.17
 
  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>
 10#include <linux/slab.h>
 11#include <linux/kernel.h>
 12#include <linux/init.h>
 13#include <linux/platform_device.h>
 14#include <linux/mfd/adp5520.h>
 15#include <linux/gpio/driver.h>
 16
 17struct adp5520_gpio {
 18	struct device *master;
 19	struct gpio_chip gpio_chip;
 20	unsigned char lut[ADP5520_MAXGPIOS];
 21	unsigned long output;
 22};
 23
 24static int adp5520_gpio_get_value(struct gpio_chip *chip, unsigned off)
 25{
 26	struct adp5520_gpio *dev;
 27	uint8_t reg_val;
 28
 29	dev = gpiochip_get_data(chip);
 30
 31	/*
 32	 * There are dedicated registers for GPIO IN/OUT.
 33	 * Make sure we return the right value, even when configured as output
 34	 */
 35
 36	if (test_bit(off, &dev->output))
 37		adp5520_read(dev->master, ADP5520_GPIO_OUT, &reg_val);
 38	else
 39		adp5520_read(dev->master, ADP5520_GPIO_IN, &reg_val);
 40
 41	return !!(reg_val & dev->lut[off]);
 42}
 43
 44static void adp5520_gpio_set_value(struct gpio_chip *chip,
 45		unsigned off, int val)
 46{
 47	struct adp5520_gpio *dev;
 48	dev = gpiochip_get_data(chip);
 49
 50	if (val)
 51		adp5520_set_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
 52	else
 53		adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT, dev->lut[off]);
 54}
 55
 56static int adp5520_gpio_direction_input(struct gpio_chip *chip, unsigned off)
 57{
 58	struct adp5520_gpio *dev;
 59	dev = gpiochip_get_data(chip);
 60
 61	clear_bit(off, &dev->output);
 62
 63	return adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_2,
 64				dev->lut[off]);
 65}
 66
 67static int adp5520_gpio_direction_output(struct gpio_chip *chip,
 68		unsigned off, int val)
 69{
 70	struct adp5520_gpio *dev;
 71	int ret = 0;
 72	dev = gpiochip_get_data(chip);
 73
 74	set_bit(off, &dev->output);
 75
 76	if (val)
 77		ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_OUT,
 78					dev->lut[off]);
 79	else
 80		ret |= adp5520_clr_bits(dev->master, ADP5520_GPIO_OUT,
 81					dev->lut[off]);
 82
 83	ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_CFG_2,
 84					dev->lut[off]);
 85
 86	return ret;
 87}
 88
 89static int adp5520_gpio_probe(struct platform_device *pdev)
 90{
 91	struct adp5520_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
 92	struct adp5520_gpio *dev;
 93	struct gpio_chip *gc;
 94	int ret, i, gpios;
 95	unsigned char ctl_mask = 0;
 96
 97	if (pdata == NULL) {
 98		dev_err(&pdev->dev, "missing platform data\n");
 99		return -ENODEV;
100	}
101
102	if (pdev->id != ID_ADP5520) {
103		dev_err(&pdev->dev, "only ADP5520 supports GPIO\n");
104		return -ENODEV;
105	}
106
107	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
108	if (dev == NULL)
109		return -ENOMEM;
110
111	dev->master = pdev->dev.parent;
112
113	for (gpios = 0, i = 0; i < ADP5520_MAXGPIOS; i++)
114		if (pdata->gpio_en_mask & (1 << i))
115			dev->lut[gpios++] = 1 << i;
116
117	if (gpios < 1) {
118		ret = -EINVAL;
119		goto err;
120	}
121
122	gc = &dev->gpio_chip;
123	gc->direction_input  = adp5520_gpio_direction_input;
124	gc->direction_output = adp5520_gpio_direction_output;
125	gc->get = adp5520_gpio_get_value;
126	gc->set = adp5520_gpio_set_value;
127	gc->can_sleep = true;
128
129	gc->base = pdata->gpio_start;
130	gc->ngpio = gpios;
131	gc->label = pdev->name;
132	gc->owner = THIS_MODULE;
133
134	ret = adp5520_clr_bits(dev->master, ADP5520_GPIO_CFG_1,
135		pdata->gpio_en_mask);
136
137	if (pdata->gpio_en_mask & ADP5520_GPIO_C3)
138		ctl_mask |= ADP5520_C3_MODE;
139
140	if (pdata->gpio_en_mask & ADP5520_GPIO_R3)
141		ctl_mask |= ADP5520_R3_MODE;
142
143	if (ctl_mask)
144		ret = adp5520_set_bits(dev->master, ADP5520_LED_CONTROL,
145			ctl_mask);
146
147	ret |= adp5520_set_bits(dev->master, ADP5520_GPIO_PULLUP,
148		pdata->gpio_pullup_mask);
149
150	if (ret) {
151		dev_err(&pdev->dev, "failed to write\n");
152		goto err;
153	}
154
155	ret = devm_gpiochip_add_data(&pdev->dev, &dev->gpio_chip, dev);
156	if (ret)
157		goto err;
158
159	platform_set_drvdata(pdev, dev);
160	return 0;
161
162err:
163	return ret;
164}
165
166static struct platform_driver adp5520_gpio_driver = {
167	.driver	= {
168		.name	= "adp5520-gpio",
169	},
170	.probe		= adp5520_gpio_probe,
171};
172
173module_platform_driver(adp5520_gpio_driver);
174
175MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
176MODULE_DESCRIPTION("GPIO ADP5520 Driver");
177MODULE_LICENSE("GPL");
178MODULE_ALIAS("platform:adp5520-gpio");