Commit | Line | Data |
---|---|---|
abd46274 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
5c7f2c76 K |
2 | /* |
3 | * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/ | |
4 | * Keerthy <j-keerthy@ti.com> | |
5 | * | |
5c7f2c76 K |
6 | * Based on the LP873X driver |
7 | */ | |
8 | ||
9 | #include <linux/gpio/driver.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/platform_device.h> | |
12 | #include <linux/regmap.h> | |
13 | ||
14 | #include <linux/mfd/lp87565.h> | |
15 | ||
16 | struct lp87565_gpio { | |
17 | struct gpio_chip chip; | |
18 | struct regmap *map; | |
19 | }; | |
20 | ||
fa8f6d06 AL |
21 | static int lp87565_gpio_get(struct gpio_chip *chip, unsigned int offset) |
22 | { | |
23 | struct lp87565_gpio *gpio = gpiochip_get_data(chip); | |
24 | int ret, val; | |
25 | ||
26 | ret = regmap_read(gpio->map, LP87565_REG_GPIO_IN, &val); | |
27 | if (ret < 0) | |
28 | return ret; | |
29 | ||
30 | return !!(val & BIT(offset)); | |
31 | } | |
32 | ||
33 | static void lp87565_gpio_set(struct gpio_chip *chip, unsigned int offset, | |
34 | int value) | |
35 | { | |
36 | struct lp87565_gpio *gpio = gpiochip_get_data(chip); | |
37 | ||
38 | regmap_update_bits(gpio->map, LP87565_REG_GPIO_OUT, | |
39 | BIT(offset), value ? BIT(offset) : 0); | |
40 | } | |
41 | ||
5c7f2c76 K |
42 | static int lp87565_gpio_get_direction(struct gpio_chip *chip, |
43 | unsigned int offset) | |
44 | { | |
45 | struct lp87565_gpio *gpio = gpiochip_get_data(chip); | |
46 | int ret, val; | |
47 | ||
48 | ret = regmap_read(gpio->map, LP87565_REG_GPIO_CONFIG, &val); | |
49 | if (ret < 0) | |
50 | return ret; | |
51 | ||
e42615ec MV |
52 | if (val & BIT(offset)) |
53 | return GPIO_LINE_DIRECTION_OUT; | |
54 | ||
55 | return GPIO_LINE_DIRECTION_IN; | |
5c7f2c76 K |
56 | } |
57 | ||
58 | static int lp87565_gpio_direction_input(struct gpio_chip *chip, | |
59 | unsigned int offset) | |
60 | { | |
61 | struct lp87565_gpio *gpio = gpiochip_get_data(chip); | |
62 | ||
63 | return regmap_update_bits(gpio->map, | |
64 | LP87565_REG_GPIO_CONFIG, | |
65 | BIT(offset), 0); | |
66 | } | |
67 | ||
68 | static int lp87565_gpio_direction_output(struct gpio_chip *chip, | |
69 | unsigned int offset, int value) | |
70 | { | |
71 | struct lp87565_gpio *gpio = gpiochip_get_data(chip); | |
72 | ||
fa8f6d06 AL |
73 | lp87565_gpio_set(chip, offset, value); |
74 | ||
5c7f2c76 K |
75 | return regmap_update_bits(gpio->map, |
76 | LP87565_REG_GPIO_CONFIG, | |
fa8f6d06 | 77 | BIT(offset), BIT(offset)); |
5c7f2c76 K |
78 | } |
79 | ||
80 | static int lp87565_gpio_request(struct gpio_chip *gc, unsigned int offset) | |
81 | { | |
82 | struct lp87565_gpio *gpio = gpiochip_get_data(gc); | |
83 | int ret; | |
84 | ||
85 | switch (offset) { | |
86 | case 0: | |
87 | case 1: | |
88 | case 2: | |
89 | /* | |
90 | * MUX can program the pin to be in EN1/2/3 pin mode | |
91 | * Or GPIO1/2/3 mode. | |
92 | * Setup the GPIO*_SEL MUX to GPIO mode | |
93 | */ | |
94 | ret = regmap_update_bits(gpio->map, | |
95 | LP87565_REG_PIN_FUNCTION, | |
96 | BIT(offset), BIT(offset)); | |
97 | if (ret) | |
98 | return ret; | |
99 | ||
100 | break; | |
101 | default: | |
102 | return -EINVAL; | |
103 | } | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
108 | static int lp87565_gpio_set_config(struct gpio_chip *gc, unsigned int offset, | |
109 | unsigned long config) | |
110 | { | |
111 | struct lp87565_gpio *gpio = gpiochip_get_data(gc); | |
112 | ||
113 | switch (pinconf_to_config_param(config)) { | |
114 | case PIN_CONFIG_DRIVE_OPEN_DRAIN: | |
115 | return regmap_update_bits(gpio->map, | |
116 | LP87565_REG_GPIO_CONFIG, | |
117 | BIT(offset + | |
c06a40e9 | 118 | __ffs(LP87565_GPIO1_OD)), |
5c7f2c76 | 119 | BIT(offset + |
c06a40e9 | 120 | __ffs(LP87565_GPIO1_OD))); |
5c7f2c76 K |
121 | case PIN_CONFIG_DRIVE_PUSH_PULL: |
122 | return regmap_update_bits(gpio->map, | |
123 | LP87565_REG_GPIO_CONFIG, | |
124 | BIT(offset + | |
c06a40e9 | 125 | __ffs(LP87565_GPIO1_OD)), 0); |
5c7f2c76 K |
126 | default: |
127 | return -ENOTSUPP; | |
128 | } | |
129 | } | |
130 | ||
131 | static const struct gpio_chip template_chip = { | |
132 | .label = "lp87565-gpio", | |
133 | .owner = THIS_MODULE, | |
134 | .request = lp87565_gpio_request, | |
135 | .get_direction = lp87565_gpio_get_direction, | |
136 | .direction_input = lp87565_gpio_direction_input, | |
137 | .direction_output = lp87565_gpio_direction_output, | |
138 | .get = lp87565_gpio_get, | |
139 | .set = lp87565_gpio_set, | |
140 | .set_config = lp87565_gpio_set_config, | |
141 | .base = -1, | |
142 | .ngpio = 3, | |
143 | .can_sleep = true, | |
144 | }; | |
145 | ||
146 | static int lp87565_gpio_probe(struct platform_device *pdev) | |
147 | { | |
148 | struct lp87565_gpio *gpio; | |
149 | struct lp87565 *lp87565; | |
150 | int ret; | |
151 | ||
152 | gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); | |
153 | if (!gpio) | |
154 | return -ENOMEM; | |
155 | ||
156 | lp87565 = dev_get_drvdata(pdev->dev.parent); | |
157 | gpio->chip = template_chip; | |
158 | gpio->chip.parent = lp87565->dev; | |
159 | gpio->map = lp87565->regmap; | |
160 | ||
161 | ret = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio); | |
162 | if (ret < 0) { | |
163 | dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret); | |
164 | return ret; | |
165 | } | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | static const struct platform_device_id lp87565_gpio_id_table[] = { | |
171 | { "lp87565-q1-gpio", }, | |
172 | { /* sentinel */ } | |
173 | }; | |
174 | MODULE_DEVICE_TABLE(platform, lp87565_gpio_id_table); | |
175 | ||
176 | static struct platform_driver lp87565_gpio_driver = { | |
177 | .driver = { | |
178 | .name = "lp87565-gpio", | |
179 | }, | |
180 | .probe = lp87565_gpio_probe, | |
181 | .id_table = lp87565_gpio_id_table, | |
182 | }; | |
183 | module_platform_driver(lp87565_gpio_driver); | |
184 | ||
185 | MODULE_AUTHOR("Keerthy <j-keerthy@ti.com>"); | |
186 | MODULE_DESCRIPTION("LP87565 GPIO driver"); | |
187 | MODULE_LICENSE("GPL v2"); |