blob: da0304b764a5187e35aacabfcdd101f93b747b89 [file] [log] [blame]
Linus Walleijd5a4da12018-08-29 16:49:14 +02001// SPDX-License-Identifier: GPL-2.0
Laxman Dewangan72bd9862012-07-18 11:50:48 +05302/*
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>
Laxman Dewangan72bd9862012-07-18 11:50:48 +053011 */
12
13#include <linux/errno.h>
Linus Walleij5d756832018-08-29 16:45:30 +020014#include <linux/gpio/driver.h>
Laxman Dewangan72bd9862012-07-18 11:50:48 +053015#include <linux/kernel.h>
Paul Gortmakera0e63732016-04-01 14:49:38 -040016#include <linux/init.h>
Laxman Dewangan72bd9862012-07-18 11:50:48 +053017#include <linux/mfd/tps6586x.h>
18#include <linux/of_device.h>
19#include <linux/platform_device.h>
20
21/* GPIO control registers */
22#define TPS6586X_GPIOSET1 0x5d
23#define TPS6586X_GPIOSET2 0x5e
24
25struct tps6586x_gpio {
26 struct gpio_chip gpio_chip;
27 struct device *parent;
28};
29
Laxman Dewangan72bd9862012-07-18 11:50:48 +053030static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
31{
Linus Walleij94a90372015-12-07 14:46:45 +010032 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
Laxman Dewangan72bd9862012-07-18 11:50:48 +053033 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
43static void tps6586x_gpio_set(struct gpio_chip *gc, unsigned offset,
44 int value)
45{
Linus Walleij94a90372015-12-07 14:46:45 +010046 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
Laxman Dewangan72bd9862012-07-18 11:50:48 +053047
48 tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET2,
49 value << offset, 1 << offset);
50}
51
52static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
53 int value)
54{
Linus Walleij94a90372015-12-07 14:46:45 +010055 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
Laxman Dewangan72bd9862012-07-18 11:50:48 +053056 uint8_t val, mask;
57
58 tps6586x_gpio_set(gc, offset, value);
59
60 val = 0x1 << (offset * 2);
61 mask = 0x3 << (offset * 2);
62
63 return tps6586x_update(tps6586x_gpio->parent, TPS6586X_GPIOSET1,
64 val, mask);
65}
66
Laxman Dewanganfe39f2f2012-11-13 19:18:07 +053067static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
68{
Linus Walleij94a90372015-12-07 14:46:45 +010069 struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc);
Laxman Dewanganfe39f2f2012-11-13 19:18:07 +053070
71 return tps6586x_irq_get_virq(tps6586x_gpio->parent,
72 TPS6586X_INT_PLDO_0 + offset);
73}
74
Bill Pemberton38363092012-11-19 13:22:34 -050075static int tps6586x_gpio_probe(struct platform_device *pdev)
Laxman Dewangan72bd9862012-07-18 11:50:48 +053076{
77 struct tps6586x_platform_data *pdata;
78 struct tps6586x_gpio *tps6586x_gpio;
Laxman Dewangan72bd9862012-07-18 11:50:48 +053079
80 pdata = dev_get_platdata(pdev->dev.parent);
81 tps6586x_gpio = devm_kzalloc(&pdev->dev,
82 sizeof(*tps6586x_gpio), GFP_KERNEL);
Jingoo Hane1ccdb82014-04-29 17:44:48 +090083 if (!tps6586x_gpio)
Laxman Dewangan72bd9862012-07-18 11:50:48 +053084 return -ENOMEM;
Laxman Dewangan72bd9862012-07-18 11:50:48 +053085
86 tps6586x_gpio->parent = pdev->dev.parent;
87
88 tps6586x_gpio->gpio_chip.owner = THIS_MODULE;
89 tps6586x_gpio->gpio_chip.label = pdev->name;
Linus Walleij58383c782015-11-04 09:56:26 +010090 tps6586x_gpio->gpio_chip.parent = &pdev->dev;
Laxman Dewangan72bd9862012-07-18 11:50:48 +053091 tps6586x_gpio->gpio_chip.ngpio = 4;
Linus Walleij9fb1f392013-12-04 14:42:46 +010092 tps6586x_gpio->gpio_chip.can_sleep = true;
Laxman Dewangan72bd9862012-07-18 11:50:48 +053093
94 /* FIXME: add handling of GPIOs as dedicated inputs */
95 tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output;
96 tps6586x_gpio->gpio_chip.set = tps6586x_gpio_set;
97 tps6586x_gpio->gpio_chip.get = tps6586x_gpio_get;
Laxman Dewanganfe39f2f2012-11-13 19:18:07 +053098 tps6586x_gpio->gpio_chip.to_irq = tps6586x_gpio_to_irq;
Laxman Dewangan72bd9862012-07-18 11:50:48 +053099
100#ifdef CONFIG_OF_GPIO
101 tps6586x_gpio->gpio_chip.of_node = pdev->dev.parent->of_node;
102#endif
103 if (pdata && pdata->gpio_base)
104 tps6586x_gpio->gpio_chip.base = pdata->gpio_base;
105 else
106 tps6586x_gpio->gpio_chip.base = -1;
107
Alexandru Ardeleancc7af0b2021-05-15 10:52:33 +0300108 return devm_gpiochip_add_data(&pdev->dev, &tps6586x_gpio->gpio_chip,
109 tps6586x_gpio);
Laxman Dewangan72bd9862012-07-18 11:50:48 +0530110}
111
Laxman Dewangan72bd9862012-07-18 11:50:48 +0530112static struct platform_driver tps6586x_gpio_driver = {
113 .driver.name = "tps6586x-gpio",
Laxman Dewangan72bd9862012-07-18 11:50:48 +0530114 .probe = tps6586x_gpio_probe,
Laxman Dewangan72bd9862012-07-18 11:50:48 +0530115};
116
117static int __init tps6586x_gpio_init(void)
118{
119 return platform_driver_register(&tps6586x_gpio_driver);
120}
121subsys_initcall(tps6586x_gpio_init);