xref: /openbmc/linux/drivers/gpio/gpio-tps6586x.c (revision 58383c78425e4ee1c077253cf297b641c861c02e)
172bd9860SLaxman Dewangan /*
272bd9860SLaxman Dewangan  * TI TPS6586x GPIO driver
372bd9860SLaxman Dewangan  *
472bd9860SLaxman Dewangan  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
572bd9860SLaxman Dewangan  * Author: Laxman dewangan <ldewangan@nvidia.com>
672bd9860SLaxman Dewangan  *
772bd9860SLaxman Dewangan  * Based on tps6586x.c
872bd9860SLaxman Dewangan  * Copyright (c) 2010 CompuLab Ltd.
972bd9860SLaxman Dewangan  * Mike Rapoport <mike@compulab.co.il>
1072bd9860SLaxman Dewangan  *
1172bd9860SLaxman Dewangan  * This program is free software; you can redistribute it and/or modify it
1272bd9860SLaxman Dewangan  * under the terms and conditions of the GNU General Public License,
1372bd9860SLaxman Dewangan  * version 2, as published by the Free Software Foundation.
1472bd9860SLaxman Dewangan  *
1572bd9860SLaxman Dewangan  * This program is distributed in the hope it will be useful, but WITHOUT
1672bd9860SLaxman Dewangan  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1772bd9860SLaxman Dewangan  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1872bd9860SLaxman Dewangan  * more details.
1972bd9860SLaxman Dewangan  *
2072bd9860SLaxman Dewangan  * You should have received a copy of the GNU General Public License
2172bd9860SLaxman Dewangan  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2272bd9860SLaxman Dewangan  */
2372bd9860SLaxman Dewangan 
2472bd9860SLaxman Dewangan #include <linux/errno.h>
2572bd9860SLaxman Dewangan #include <linux/gpio.h>
2672bd9860SLaxman Dewangan #include <linux/kernel.h>
2772bd9860SLaxman Dewangan #include <linux/module.h>
2872bd9860SLaxman Dewangan #include <linux/mfd/tps6586x.h>
2972bd9860SLaxman Dewangan #include <linux/of_device.h>
3072bd9860SLaxman Dewangan #include <linux/platform_device.h>
3172bd9860SLaxman Dewangan 
3272bd9860SLaxman Dewangan /* GPIO control registers */
3372bd9860SLaxman Dewangan #define TPS6586X_GPIOSET1	0x5d
3472bd9860SLaxman Dewangan #define TPS6586X_GPIOSET2	0x5e
3572bd9860SLaxman Dewangan 
3672bd9860SLaxman Dewangan struct tps6586x_gpio {
3772bd9860SLaxman Dewangan 	struct gpio_chip gpio_chip;
3872bd9860SLaxman Dewangan 	struct device *parent;
3972bd9860SLaxman Dewangan };
4072bd9860SLaxman Dewangan 
4172bd9860SLaxman Dewangan static inline struct tps6586x_gpio *to_tps6586x_gpio(struct gpio_chip *chip)
4272bd9860SLaxman Dewangan {
4372bd9860SLaxman Dewangan 	return container_of(chip, struct tps6586x_gpio, gpio_chip);
4472bd9860SLaxman Dewangan }
4572bd9860SLaxman Dewangan 
4672bd9860SLaxman Dewangan static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
4772bd9860SLaxman Dewangan {
4872bd9860SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio = to_tps6586x_gpio(gc);
4972bd9860SLaxman Dewangan 	uint8_t val;
5072bd9860SLaxman Dewangan 	int ret;
5172bd9860SLaxman Dewangan 
5272bd9860SLaxman Dewangan 	ret = tps6586x_read(tps6586x_gpio->parent, TPS6586X_GPIOSET2, &val);
5372bd9860SLaxman Dewangan 	if (ret)
5472bd9860SLaxman Dewangan 		return ret;
5572bd9860SLaxman Dewangan 
5672bd9860SLaxman Dewangan 	return !!(val & (1 << offset));
5772bd9860SLaxman Dewangan }
5872bd9860SLaxman Dewangan 
5972bd9860SLaxman Dewangan static void tps6586x_gpio_set(struct gpio_chip *gc, unsigned offset,
6072bd9860SLaxman Dewangan 			      int value)
6172bd9860SLaxman Dewangan {
6272bd9860SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio = to_tps6586x_gpio(gc);
6372bd9860SLaxman Dewangan 
6472bd9860SLaxman Dewangan 	tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET2,
6572bd9860SLaxman Dewangan 			value << offset, 1 << offset);
6672bd9860SLaxman Dewangan }
6772bd9860SLaxman Dewangan 
6872bd9860SLaxman Dewangan static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
6972bd9860SLaxman Dewangan 				int value)
7072bd9860SLaxman Dewangan {
7172bd9860SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio = to_tps6586x_gpio(gc);
7272bd9860SLaxman Dewangan 	uint8_t val, mask;
7372bd9860SLaxman Dewangan 
7472bd9860SLaxman Dewangan 	tps6586x_gpio_set(gc, offset, value);
7572bd9860SLaxman Dewangan 
7672bd9860SLaxman Dewangan 	val = 0x1 << (offset * 2);
7772bd9860SLaxman Dewangan 	mask = 0x3 << (offset * 2);
7872bd9860SLaxman Dewangan 
7972bd9860SLaxman Dewangan 	return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET1,
8072bd9860SLaxman Dewangan 				val, mask);
8172bd9860SLaxman Dewangan }
8272bd9860SLaxman Dewangan 
83fe39f2f4SLaxman Dewangan static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
84fe39f2f4SLaxman Dewangan {
85fe39f2f4SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio = to_tps6586x_gpio(gc);
86fe39f2f4SLaxman Dewangan 
87fe39f2f4SLaxman Dewangan 	return tps6586x_irq_get_virq(tps6586x_gpio->parent,
88fe39f2f4SLaxman Dewangan 				TPS6586X_INT_PLDO_0 + offset);
89fe39f2f4SLaxman Dewangan }
90fe39f2f4SLaxman Dewangan 
913836309dSBill Pemberton static int tps6586x_gpio_probe(struct platform_device *pdev)
9272bd9860SLaxman Dewangan {
9372bd9860SLaxman Dewangan 	struct tps6586x_platform_data *pdata;
9472bd9860SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio;
9572bd9860SLaxman Dewangan 	int ret;
9672bd9860SLaxman Dewangan 
9772bd9860SLaxman Dewangan 	pdata = dev_get_platdata(pdev->dev.parent);
9872bd9860SLaxman Dewangan 	tps6586x_gpio = devm_kzalloc(&pdev->dev,
9972bd9860SLaxman Dewangan 				sizeof(*tps6586x_gpio), GFP_KERNEL);
100e1ccdb82SJingoo Han 	if (!tps6586x_gpio)
10172bd9860SLaxman Dewangan 		return -ENOMEM;
10272bd9860SLaxman Dewangan 
10372bd9860SLaxman Dewangan 	tps6586x_gpio->parent = pdev->dev.parent;
10472bd9860SLaxman Dewangan 
10572bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.owner = THIS_MODULE;
10672bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.label = pdev->name;
107*58383c78SLinus Walleij 	tps6586x_gpio->gpio_chip.parent = &pdev->dev;
10872bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.ngpio = 4;
1099fb1f39eSLinus Walleij 	tps6586x_gpio->gpio_chip.can_sleep = true;
11072bd9860SLaxman Dewangan 
11172bd9860SLaxman Dewangan 	/* FIXME: add handling of GPIOs as dedicated inputs */
11272bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output;
11372bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.set	= tps6586x_gpio_set;
11472bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.get	= tps6586x_gpio_get;
115fe39f2f4SLaxman Dewangan 	tps6586x_gpio->gpio_chip.to_irq	= tps6586x_gpio_to_irq;
11672bd9860SLaxman Dewangan 
11772bd9860SLaxman Dewangan #ifdef CONFIG_OF_GPIO
11872bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.of_node = pdev->dev.parent->of_node;
11972bd9860SLaxman Dewangan #endif
12072bd9860SLaxman Dewangan 	if (pdata && pdata->gpio_base)
12172bd9860SLaxman Dewangan 		tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
12272bd9860SLaxman Dewangan 	else
12372bd9860SLaxman Dewangan 		tps6586x_gpio->gpio_chip.base = -1;
12472bd9860SLaxman Dewangan 
12572bd9860SLaxman Dewangan 	ret = gpiochip_add(&tps6586x_gpio->gpio_chip);
12672bd9860SLaxman Dewangan 	if (ret < 0) {
12772bd9860SLaxman Dewangan 		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
12872bd9860SLaxman Dewangan 		return ret;
12972bd9860SLaxman Dewangan 	}
13072bd9860SLaxman Dewangan 
13172bd9860SLaxman Dewangan 	platform_set_drvdata(pdev, tps6586x_gpio);
13272bd9860SLaxman Dewangan 
13372bd9860SLaxman Dewangan 	return ret;
13472bd9860SLaxman Dewangan }
13572bd9860SLaxman Dewangan 
136206210ceSBill Pemberton static int tps6586x_gpio_remove(struct platform_device *pdev)
13772bd9860SLaxman Dewangan {
13872bd9860SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio = platform_get_drvdata(pdev);
13972bd9860SLaxman Dewangan 
1409f5132aeSabdoulaye berthe 	gpiochip_remove(&tps6586x_gpio->gpio_chip);
1419f5132aeSabdoulaye berthe 	return 0;
14272bd9860SLaxman Dewangan }
14372bd9860SLaxman Dewangan 
14472bd9860SLaxman Dewangan static struct platform_driver tps6586x_gpio_driver = {
14572bd9860SLaxman Dewangan 	.driver.name	= "tps6586x-gpio",
14672bd9860SLaxman Dewangan 	.driver.owner	= THIS_MODULE,
14772bd9860SLaxman Dewangan 	.probe		= tps6586x_gpio_probe,
1488283c4ffSBill Pemberton 	.remove		= tps6586x_gpio_remove,
14972bd9860SLaxman Dewangan };
15072bd9860SLaxman Dewangan 
15172bd9860SLaxman Dewangan static int __init tps6586x_gpio_init(void)
15272bd9860SLaxman Dewangan {
15372bd9860SLaxman Dewangan 	return platform_driver_register(&tps6586x_gpio_driver);
15472bd9860SLaxman Dewangan }
15572bd9860SLaxman Dewangan subsys_initcall(tps6586x_gpio_init);
15672bd9860SLaxman Dewangan 
15772bd9860SLaxman Dewangan static void __exit tps6586x_gpio_exit(void)
15872bd9860SLaxman Dewangan {
15972bd9860SLaxman Dewangan 	platform_driver_unregister(&tps6586x_gpio_driver);
16072bd9860SLaxman Dewangan }
16172bd9860SLaxman Dewangan module_exit(tps6586x_gpio_exit);
16272bd9860SLaxman Dewangan 
16372bd9860SLaxman Dewangan MODULE_ALIAS("platform:tps6586x-gpio");
16472bd9860SLaxman Dewangan MODULE_DESCRIPTION("GPIO interface for TPS6586X PMIC");
16572bd9860SLaxman Dewangan MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
16672bd9860SLaxman Dewangan MODULE_LICENSE("GPL");
167