xref: /openbmc/linux/drivers/gpio/gpio-tps6586x.c (revision 3836309d93462bbf34851c078be6e5e77d888e3d)
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 
83*3836309dSBill Pemberton static int tps6586x_gpio_probe(struct platform_device *pdev)
8472bd9860SLaxman Dewangan {
8572bd9860SLaxman Dewangan 	struct tps6586x_platform_data *pdata;
8672bd9860SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio;
8772bd9860SLaxman Dewangan 	int ret;
8872bd9860SLaxman Dewangan 
8972bd9860SLaxman Dewangan 	pdata = dev_get_platdata(pdev->dev.parent);
9072bd9860SLaxman Dewangan 	tps6586x_gpio = devm_kzalloc(&pdev->dev,
9172bd9860SLaxman Dewangan 				sizeof(*tps6586x_gpio), GFP_KERNEL);
9272bd9860SLaxman Dewangan 	if (!tps6586x_gpio) {
9372bd9860SLaxman Dewangan 		dev_err(&pdev->dev, "Could not allocate tps6586x_gpio\n");
9472bd9860SLaxman Dewangan 		return -ENOMEM;
9572bd9860SLaxman Dewangan 	}
9672bd9860SLaxman Dewangan 
9772bd9860SLaxman Dewangan 	tps6586x_gpio->parent = pdev->dev.parent;
9872bd9860SLaxman Dewangan 
9972bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.owner = THIS_MODULE;
10072bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.label = pdev->name;
10172bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.dev = &pdev->dev;
10272bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.ngpio = 4;
10372bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.can_sleep = 1;
10472bd9860SLaxman Dewangan 
10572bd9860SLaxman Dewangan 	/* FIXME: add handling of GPIOs as dedicated inputs */
10672bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output;
10772bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.set	= tps6586x_gpio_set;
10872bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.get	= tps6586x_gpio_get;
10972bd9860SLaxman Dewangan 
11072bd9860SLaxman Dewangan #ifdef CONFIG_OF_GPIO
11172bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.of_node = pdev->dev.parent->of_node;
11272bd9860SLaxman Dewangan #endif
11372bd9860SLaxman Dewangan 	if (pdata && pdata->gpio_base)
11472bd9860SLaxman Dewangan 		tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
11572bd9860SLaxman Dewangan 	else
11672bd9860SLaxman Dewangan 		tps6586x_gpio->gpio_chip.base = -1;
11772bd9860SLaxman Dewangan 
11872bd9860SLaxman Dewangan 	ret = gpiochip_add(&tps6586x_gpio->gpio_chip);
11972bd9860SLaxman Dewangan 	if (ret < 0) {
12072bd9860SLaxman Dewangan 		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
12172bd9860SLaxman Dewangan 		return ret;
12272bd9860SLaxman Dewangan 	}
12372bd9860SLaxman Dewangan 
12472bd9860SLaxman Dewangan 	platform_set_drvdata(pdev, tps6586x_gpio);
12572bd9860SLaxman Dewangan 
12672bd9860SLaxman Dewangan 	return ret;
12772bd9860SLaxman Dewangan }
12872bd9860SLaxman Dewangan 
12972bd9860SLaxman Dewangan static int __devexit tps6586x_gpio_remove(struct platform_device *pdev)
13072bd9860SLaxman Dewangan {
13172bd9860SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio = platform_get_drvdata(pdev);
13272bd9860SLaxman Dewangan 
13372bd9860SLaxman Dewangan 	return gpiochip_remove(&tps6586x_gpio->gpio_chip);
13472bd9860SLaxman Dewangan }
13572bd9860SLaxman Dewangan 
13672bd9860SLaxman Dewangan static struct platform_driver tps6586x_gpio_driver = {
13772bd9860SLaxman Dewangan 	.driver.name	= "tps6586x-gpio",
13872bd9860SLaxman Dewangan 	.driver.owner	= THIS_MODULE,
13972bd9860SLaxman Dewangan 	.probe		= tps6586x_gpio_probe,
1408283c4ffSBill Pemberton 	.remove		= tps6586x_gpio_remove,
14172bd9860SLaxman Dewangan };
14272bd9860SLaxman Dewangan 
14372bd9860SLaxman Dewangan static int __init tps6586x_gpio_init(void)
14472bd9860SLaxman Dewangan {
14572bd9860SLaxman Dewangan 	return platform_driver_register(&tps6586x_gpio_driver);
14672bd9860SLaxman Dewangan }
14772bd9860SLaxman Dewangan subsys_initcall(tps6586x_gpio_init);
14872bd9860SLaxman Dewangan 
14972bd9860SLaxman Dewangan static void __exit tps6586x_gpio_exit(void)
15072bd9860SLaxman Dewangan {
15172bd9860SLaxman Dewangan 	platform_driver_unregister(&tps6586x_gpio_driver);
15272bd9860SLaxman Dewangan }
15372bd9860SLaxman Dewangan module_exit(tps6586x_gpio_exit);
15472bd9860SLaxman Dewangan 
15572bd9860SLaxman Dewangan MODULE_ALIAS("platform:tps6586x-gpio");
15672bd9860SLaxman Dewangan MODULE_DESCRIPTION("GPIO interface for TPS6586X PMIC");
15772bd9860SLaxman Dewangan MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
15872bd9860SLaxman Dewangan MODULE_LICENSE("GPL");
159