1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * TI TPS6586x GPIO driver 4 * 5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. 6 * Author: Laxman dewangan <ldewangan@nvidia.com> 7 * 8 * Based on tps6586x.c 9 * Copyright (c) 2010 CompuLab Ltd. 10 * Mike Rapoport <mike@compulab.co.il> 11 */ 12 13 #include <linux/errno.h> 14 #include <linux/gpio/driver.h> 15 #include <linux/kernel.h> 16 #include <linux/init.h> 17 #include <linux/mfd/tps6586x.h> 18 #include <linux/of.h> 19 #include <linux/platform_device.h> 20 21 /* GPIO control registers */ 22 #define TPS6586X_GPIOSET1 0x5d 23 #define TPS6586X_GPIOSET2 0x5e 24 25 struct tps6586x_gpio { 26 struct gpio_chip gpio_chip; 27 struct device *parent; 28 }; 29 30 static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset) 31 { 32 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc); 33 uint8_t val; 34 int ret; 35 36 ret = tps6586x_read(tps6586x_gpio->parent, TPS6586X_GPIOSET2, &val); 37 if (ret) 38 return ret; 39 40 return !!(val & (1 << offset)); 41 } 42 43 static int tps6586x_gpio_set(struct gpio_chip *gc, unsigned int offset, 44 int value) 45 { 46 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc); 47 48 return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET2, 49 value << offset, 1 << offset); 50 } 51 52 static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset, 53 int value) 54 { 55 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc); 56 uint8_t val, mask; 57 int ret; 58 59 ret = tps6586x_gpio_set(gc, offset, value); 60 if (ret) 61 return ret; 62 63 val = 0x1 << (offset * 2); 64 mask = 0x3 << (offset * 2); 65 66 return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET1, 67 val, mask); 68 } 69 70 static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset) 71 { 72 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc); 73 74 return tps6586x_irq_get_virq(tps6586x_gpio->parent, 75 TPS6586X_INT_PLDO_0 + offset); 76 } 77 78 static int tps6586x_gpio_probe(struct platform_device *pdev) 79 { 80 struct tps6586x_platform_data *pdata; 81 struct tps6586x_gpio *tps6586x_gpio; 82 83 device_set_node(&pdev->dev, dev_fwnode(pdev->dev.parent)); 84 85 pdata = dev_get_platdata(pdev->dev.parent); 86 tps6586x_gpio = devm_kzalloc(&pdev->dev, 87 sizeof(*tps6586x_gpio), GFP_KERNEL); 88 if (!tps6586x_gpio) 89 return -ENOMEM; 90 91 tps6586x_gpio->parent = pdev->dev.parent; 92 93 tps6586x_gpio->gpio_chip.owner = THIS_MODULE; 94 tps6586x_gpio->gpio_chip.label = pdev->name; 95 tps6586x_gpio->gpio_chip.parent = &pdev->dev; 96 tps6586x_gpio->gpio_chip.ngpio = 4; 97 tps6586x_gpio->gpio_chip.can_sleep = true; 98 99 /* FIXME: add handling of GPIOs as dedicated inputs */ 100 tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output; 101 tps6586x_gpio->gpio_chip.set = tps6586x_gpio_set; 102 tps6586x_gpio->gpio_chip.get = tps6586x_gpio_get; 103 tps6586x_gpio->gpio_chip.to_irq = tps6586x_gpio_to_irq; 104 105 if (pdata && pdata->gpio_base) 106 tps6586x_gpio->gpio_chip.base = pdata->gpio_base; 107 else 108 tps6586x_gpio->gpio_chip.base = -1; 109 110 return devm_gpiochip_add_data(&pdev->dev, &tps6586x_gpio->gpio_chip, 111 tps6586x_gpio); 112 } 113 114 static struct platform_driver tps6586x_gpio_driver = { 115 .driver.name = "tps6586x-gpio", 116 .probe = tps6586x_gpio_probe, 117 }; 118 119 static int __init tps6586x_gpio_init(void) 120 { 121 return platform_driver_register(&tps6586x_gpio_driver); 122 } 123 subsys_initcall(tps6586x_gpio_init); 124