]>
Commit | Line | Data |
---|---|---|
d5a4da15 | 1 | // SPDX-License-Identifier: GPL-2.0 |
72bd9860 LD |
2 | /* |
3 | * TI TPS6586x GPIO driver | |
4 | * | |
5 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. | |
6 | * Author: Laxman dewangan <[email protected]> | |
7 | * | |
8 | * Based on tps6586x.c | |
9 | * Copyright (c) 2010 CompuLab Ltd. | |
10 | * Mike Rapoport <[email protected]> | |
72bd9860 LD |
11 | */ |
12 | ||
13 | #include <linux/errno.h> | |
5d75683e | 14 | #include <linux/gpio/driver.h> |
72bd9860 | 15 | #include <linux/kernel.h> |
a0e63738 | 16 | #include <linux/init.h> |
72bd9860 LD |
17 | #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 | ||
25 | struct tps6586x_gpio { | |
26 | struct gpio_chip gpio_chip; | |
27 | struct device *parent; | |
28 | }; | |
29 | ||
72bd9860 LD |
30 | static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset) |
31 | { | |
94a90370 | 32 | struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc); |
72bd9860 LD |
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 void tps6586x_gpio_set(struct gpio_chip *gc, unsigned offset, | |
44 | int value) | |
45 | { | |
94a90370 | 46 | struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc); |
72bd9860 LD |
47 | |
48 | 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 | { | |
94a90370 | 55 | struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc); |
72bd9860 LD |
56 | 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 | ||
fe39f2f4 LD |
67 | static int tps6586x_gpio_to_irq(struct gpio_chip *gc, unsigned offset) |
68 | { | |
94a90370 | 69 | struct tps6586x_gpio *tps6586x_gpio = gpiochip_get_data(gc); |
fe39f2f4 LD |
70 | |
71 | return tps6586x_irq_get_virq(tps6586x_gpio->parent, | |
72 | TPS6586X_INT_PLDO_0 + offset); | |
73 | } | |
74 | ||
3836309d | 75 | static int tps6586x_gpio_probe(struct platform_device *pdev) |
72bd9860 LD |
76 | { |
77 | struct tps6586x_platform_data *pdata; | |
78 | struct tps6586x_gpio *tps6586x_gpio; | |
79 | int ret; | |
80 | ||
81 | pdata = dev_get_platdata(pdev->dev.parent); | |
82 | tps6586x_gpio = devm_kzalloc(&pdev->dev, | |
83 | sizeof(*tps6586x_gpio), GFP_KERNEL); | |
e1ccdb82 | 84 | if (!tps6586x_gpio) |
72bd9860 | 85 | return -ENOMEM; |
72bd9860 LD |
86 | |
87 | tps6586x_gpio->parent = pdev->dev.parent; | |
88 | ||
89 | tps6586x_gpio->gpio_chip.owner = THIS_MODULE; | |
90 | tps6586x_gpio->gpio_chip.label = pdev->name; | |
58383c78 | 91 | tps6586x_gpio->gpio_chip.parent = &pdev->dev; |
72bd9860 | 92 | tps6586x_gpio->gpio_chip.ngpio = 4; |
9fb1f39e | 93 | tps6586x_gpio->gpio_chip.can_sleep = true; |
72bd9860 LD |
94 | |
95 | /* FIXME: add handling of GPIOs as dedicated inputs */ | |
96 | tps6586x_gpio->gpio_chip.direction_output = tps6586x_gpio_output; | |
97 | tps6586x_gpio->gpio_chip.set = tps6586x_gpio_set; | |
98 | tps6586x_gpio->gpio_chip.get = tps6586x_gpio_get; | |
fe39f2f4 | 99 | tps6586x_gpio->gpio_chip.to_irq = tps6586x_gpio_to_irq; |
72bd9860 LD |
100 | |
101 | #ifdef CONFIG_OF_GPIO | |
102 | tps6586x_gpio->gpio_chip.of_node = pdev->dev.parent->of_node; | |
103 | #endif | |
104 | if (pdata && pdata->gpio_base) | |
105 | tps6586x_gpio->gpio_chip.base = pdata->gpio_base; | |
106 | else | |
107 | tps6586x_gpio->gpio_chip.base = -1; | |
108 | ||
79c676f8 LD |
109 | ret = devm_gpiochip_add_data(&pdev->dev, &tps6586x_gpio->gpio_chip, |
110 | tps6586x_gpio); | |
72bd9860 LD |
111 | if (ret < 0) { |
112 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); | |
113 | return ret; | |
114 | } | |
115 | ||
116 | platform_set_drvdata(pdev, tps6586x_gpio); | |
117 | ||
118 | return ret; | |
119 | } | |
120 | ||
72bd9860 LD |
121 | static struct platform_driver tps6586x_gpio_driver = { |
122 | .driver.name = "tps6586x-gpio", | |
72bd9860 | 123 | .probe = tps6586x_gpio_probe, |
72bd9860 LD |
124 | }; |
125 | ||
126 | static int __init tps6586x_gpio_init(void) | |
127 | { | |
128 | return platform_driver_register(&tps6586x_gpio_driver); | |
129 | } | |
130 | subsys_initcall(tps6586x_gpio_init); |