xref: /openbmc/linux/drivers/gpio/gpio-tps6586x.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1d5a4da15SLinus Walleij // SPDX-License-Identifier: GPL-2.0
272bd9860SLaxman Dewangan /*
372bd9860SLaxman Dewangan  * TI TPS6586x GPIO driver
472bd9860SLaxman Dewangan  *
572bd9860SLaxman Dewangan  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
672bd9860SLaxman Dewangan  * Author: Laxman dewangan <ldewangan@nvidia.com>
772bd9860SLaxman Dewangan  *
872bd9860SLaxman Dewangan  * Based on tps6586x.c
972bd9860SLaxman Dewangan  * Copyright (c) 2010 CompuLab Ltd.
1072bd9860SLaxman Dewangan  * Mike Rapoport <mike@compulab.co.il>
1172bd9860SLaxman Dewangan  */
1272bd9860SLaxman Dewangan 
1372bd9860SLaxman Dewangan #include <linux/errno.h>
145d75683eSLinus Walleij #include <linux/gpio/driver.h>
1572bd9860SLaxman Dewangan #include <linux/kernel.h>
16a0e63738SPaul Gortmaker #include <linux/init.h>
1772bd9860SLaxman Dewangan #include <linux/mfd/tps6586x.h>
18*e91d0f05SRob Herring #include <linux/of.h>
1972bd9860SLaxman Dewangan #include <linux/platform_device.h>
2072bd9860SLaxman Dewangan 
2172bd9860SLaxman Dewangan /* GPIO control registers */
2272bd9860SLaxman Dewangan #define TPS6586X_GPIOSET1	0x5d
2372bd9860SLaxman Dewangan #define TPS6586X_GPIOSET2	0x5e
2472bd9860SLaxman Dewangan 
2572bd9860SLaxman Dewangan struct tps6586x_gpio {
2672bd9860SLaxman Dewangan 	struct gpio_chip gpio_chip;
2772bd9860SLaxman Dewangan 	struct device *parent;
2872bd9860SLaxman Dewangan };
2972bd9860SLaxman Dewangan 
tps6586x_gpio_get(struct gpio_chip * gc,unsigned offset)3072bd9860SLaxman Dewangan static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
3172bd9860SLaxman Dewangan {
3294a90370SLinus Walleij 	struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
3372bd9860SLaxman Dewangan 	uint8_t val;
3472bd9860SLaxman Dewangan 	int ret;
3572bd9860SLaxman Dewangan 
3672bd9860SLaxman Dewangan 	ret = tps6586x_read(tps6586x_gpio->parent, TPS6586X_GPIOSET2, &val);
3772bd9860SLaxman Dewangan 	if (ret)
3872bd9860SLaxman Dewangan 		return ret;
3972bd9860SLaxman Dewangan 
4072bd9860SLaxman Dewangan 	return !!(val & (1 << offset));
4172bd9860SLaxman Dewangan }
4272bd9860SLaxman Dewangan 
tps6586x_gpio_set(struct gpio_chip * gc,unsigned offset,int value)4372bd9860SLaxman Dewangan static void tps6586x_gpio_set(struct gpio_chip *gc, unsigned offset,
4472bd9860SLaxman Dewangan 			      int value)
4572bd9860SLaxman Dewangan {
4694a90370SLinus Walleij 	struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
4772bd9860SLaxman Dewangan 
4872bd9860SLaxman Dewangan 	tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET2,
4972bd9860SLaxman Dewangan 			value << offset, 1 << offset);
5072bd9860SLaxman Dewangan }
5172bd9860SLaxman Dewangan 
tps6586x_gpio_output(struct gpio_chip * gc,unsigned offset,int value)5272bd9860SLaxman Dewangan static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
5372bd9860SLaxman Dewangan 				int value)
5472bd9860SLaxman Dewangan {
5594a90370SLinus Walleij 	struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
5672bd9860SLaxman Dewangan 	uint8_t val, mask;
5772bd9860SLaxman Dewangan 
5872bd9860SLaxman Dewangan 	tps6586x_gpio_set(gc, offset, value);
5972bd9860SLaxman Dewangan 
6072bd9860SLaxman Dewangan 	val = 0x1 << (offset * 2);
6172bd9860SLaxman Dewangan 	mask = 0x3 << (offset * 2);
6272bd9860SLaxman Dewangan 
6372bd9860SLaxman Dewangan 	return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET1,
6472bd9860SLaxman Dewangan 				val, mask);
6572bd9860SLaxman Dewangan }
6672bd9860SLaxman Dewangan 
tps6586x_gpio_to_irq(struct gpio_chip * gc,unsigned offset)67fe39f2f4SLaxman Dewangan static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
68fe39f2f4SLaxman Dewangan {
6994a90370SLinus Walleij 	struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
70fe39f2f4SLaxman Dewangan 
71fe39f2f4SLaxman Dewangan 	return tps6586x_irq_get_virq(tps6586x_gpio->parent,
72fe39f2f4SLaxman Dewangan 				TPS6586X_INT_PLDO_0 + offset);
73fe39f2f4SLaxman Dewangan }
74fe39f2f4SLaxman Dewangan 
tps6586x_gpio_probe(struct platform_device * pdev)753836309dSBill Pemberton static int tps6586x_gpio_probe(struct platform_device *pdev)
7672bd9860SLaxman Dewangan {
7772bd9860SLaxman Dewangan 	struct tps6586x_platform_data *pdata;
7872bd9860SLaxman Dewangan 	struct tps6586x_gpio *tps6586x_gpio;
7972bd9860SLaxman Dewangan 
806dbe6c07SAndy Shevchenko 	device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent));
816dbe6c07SAndy Shevchenko 
8272bd9860SLaxman Dewangan 	pdata = dev_get_platdata(pdev->dev.parent);
8372bd9860SLaxman Dewangan 	tps6586x_gpio = devm_kzalloc(&pdev->dev,
8472bd9860SLaxman Dewangan 				sizeof(*tps6586x_gpio), GFP_KERNEL);
85e1ccdb82SJingoo Han 	if (!tps6586x_gpio)
8672bd9860SLaxman Dewangan 		return -ENOMEM;
8772bd9860SLaxman Dewangan 
8872bd9860SLaxman Dewangan 	tps6586x_gpio->parent = pdev->dev.parent;
8972bd9860SLaxman Dewangan 
9072bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.owner = THIS_MODULE;
9172bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.label = pdev->name;
9258383c78SLinus Walleij 	tps6586x_gpio->gpio_chip.parent = &pdev->dev;
9372bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.ngpio = 4;
949fb1f39eSLinus Walleij 	tps6586x_gpio->gpio_chip.can_sleep = true;
9572bd9860SLaxman Dewangan 
9672bd9860SLaxman Dewangan 	/* FIXME: add handling of GPIOs as dedicated inputs */
9772bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output;
9872bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.set	= tps6586x_gpio_set;
9972bd9860SLaxman Dewangan 	tps6586x_gpio->gpio_chip.get	= tps6586x_gpio_get;
100fe39f2f4SLaxman Dewangan 	tps6586x_gpio->gpio_chip.to_irq	= tps6586x_gpio_to_irq;
10172bd9860SLaxman Dewangan 
10272bd9860SLaxman Dewangan 	if (pdata && pdata->gpio_base)
10372bd9860SLaxman Dewangan 		tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
10472bd9860SLaxman Dewangan 	else
10572bd9860SLaxman Dewangan 		tps6586x_gpio->gpio_chip.base = -1;
10672bd9860SLaxman Dewangan 
107cc7af0bfSAlexandru Ardelean 	return devm_gpiochip_add_data(&pdev->dev, &tps6586x_gpio->gpio_chip,
10879c676f8SLaxman Dewangan 				      tps6586x_gpio);
10972bd9860SLaxman Dewangan }
11072bd9860SLaxman Dewangan 
11172bd9860SLaxman Dewangan static struct platform_driver tps6586x_gpio_driver = {
11272bd9860SLaxman Dewangan 	.driver.name	= "tps6586x-gpio",
11372bd9860SLaxman Dewangan 	.probe		= tps6586x_gpio_probe,
11472bd9860SLaxman Dewangan };
11572bd9860SLaxman Dewangan 
tps6586x_gpio_init(void)11672bd9860SLaxman Dewangan static int __init tps6586x_gpio_init(void)
11772bd9860SLaxman Dewangan {
11872bd9860SLaxman Dewangan 	return platform_driver_register(&tps6586x_gpio_driver);
11972bd9860SLaxman Dewangan }
12072bd9860SLaxman Dewangan subsys_initcall(tps6586x_gpio_init);
121