blob: 6ffdc6beb203c83dbbe0828c3357bfb50db80996 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * SuperH Pin Function Controller GPIO driver.
3 *
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
9 * for more details.
10 */
11
12#include <linux/device.h>
13#include <linux/gpio.h>
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/pinctrl/consumer.h>
17#include <linux/slab.h>
18#include <linux/spinlock.h>
19
20#include "core.h"
21
22struct sh_pfc_gpio_data_reg {
23 const struct pinmux_data_reg *info;
24 u32 shadow;
25};
26
27struct sh_pfc_gpio_pin {
28 u8 dbit;
29 u8 dreg;
30};
31
32struct sh_pfc_chip {
33 struct sh_pfc *pfc;
34 struct gpio_chip gpio_chip;
35
36 struct sh_pfc_window *mem;
37 struct sh_pfc_gpio_data_reg *regs;
38 struct sh_pfc_gpio_pin *pins;
39};
40
41static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
42{
43 struct sh_pfc_chip *chip = gpiochip_get_data(gc);
44 return chip->pfc;
45}
46
47static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int offset,
48 struct sh_pfc_gpio_data_reg **reg,
49 unsigned int *bit)
50{
51 int idx = sh_pfc_get_pin_index(chip->pfc, offset);
52 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
53
54 *reg = &chip->regs[gpio_pin->dreg];
55 *bit = gpio_pin->dbit;
56}
57
58static u32 gpio_read_data_reg(struct sh_pfc_chip *chip,
59 const struct pinmux_data_reg *dreg)
60{
61 phys_addr_t address = dreg->reg;
62 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
63
64 return sh_pfc_read_raw_reg(mem, dreg->reg_width);
65}
66
67static void gpio_write_data_reg(struct sh_pfc_chip *chip,
68 const struct pinmux_data_reg *dreg, u32 value)
69{
70 phys_addr_t address = dreg->reg;
71 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
72
73 sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
74}
75
76static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned idx)
77{
78 struct sh_pfc *pfc = chip->pfc;
79 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
80 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
81 const struct pinmux_data_reg *dreg;
82 unsigned int bit;
83 unsigned int i;
84
85 for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
86 for (bit = 0; bit < dreg->reg_width; bit++) {
87 if (dreg->enum_ids[bit] == pin->enum_id) {
88 gpio_pin->dreg = i;
89 gpio_pin->dbit = bit;
90 return;
91 }
92 }
93 }
94
95 BUG();
96}
97
98static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
99{
100 struct sh_pfc *pfc = chip->pfc;
101 const struct pinmux_data_reg *dreg;
102 unsigned int i;
103
104 /* Count the number of data registers, allocate memory and initialize
105 * them.
106 */
107 for (i = 0; pfc->info->data_regs[i].reg_width; ++i)
108 ;
109
110 chip->regs = devm_kcalloc(pfc->dev, i, sizeof(*chip->regs),
111 GFP_KERNEL);
112 if (chip->regs == NULL)
113 return -ENOMEM;
114
115 for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
116 chip->regs[i].info = dreg;
117 chip->regs[i].shadow = gpio_read_data_reg(chip, dreg);
118 }
119
120 for (i = 0; i < pfc->info->nr_pins; i++) {
121 if (pfc->info->pins[i].enum_id == 0)
122 continue;
123
124 gpio_setup_data_reg(chip, i);
125 }
126
127 return 0;
128}
129
130/* -----------------------------------------------------------------------------
131 * Pin GPIOs
132 */
133
134static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
135{
136 struct sh_pfc *pfc = gpio_to_pfc(gc);
137 int idx = sh_pfc_get_pin_index(pfc, offset);
138
139 if (idx < 0 || pfc->info->pins[idx].enum_id == 0)
140 return -EINVAL;
141
142 return pinctrl_gpio_request(offset);
143}
144
145static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
146{
147 return pinctrl_gpio_free(offset);
148}
149
150static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
151 int value)
152{
153 struct sh_pfc_gpio_data_reg *reg;
154 unsigned int bit;
155 unsigned int pos;
156
157 gpio_get_data_reg(chip, offset, &reg, &bit);
158
159 pos = reg->info->reg_width - (bit + 1);
160
161 if (value)
162 reg->shadow |= BIT(pos);
163 else
164 reg->shadow &= ~BIT(pos);
165
166 gpio_write_data_reg(chip, reg->info, reg->shadow);
167}
168
169static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
170{
171 return pinctrl_gpio_direction_input(offset);
172}
173
174static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
175 int value)
176{
177 gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
178
179 return pinctrl_gpio_direction_output(offset);
180}
181
182static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
183{
184 struct sh_pfc_chip *chip = gpiochip_get_data(gc);
185 struct sh_pfc_gpio_data_reg *reg;
186 unsigned int bit;
187 unsigned int pos;
188
189 gpio_get_data_reg(chip, offset, &reg, &bit);
190
191 pos = reg->info->reg_width - (bit + 1);
192
193 return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
194}
195
196static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
197{
198 gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
199}
200
201static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
202{
203 struct sh_pfc *pfc = gpio_to_pfc(gc);
204 unsigned int i, k;
205
206 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
207 const short *gpios = pfc->info->gpio_irq[i].gpios;
208
209 for (k = 0; gpios[k] >= 0; k++) {
210 if (gpios[k] == offset)
211 goto found;
212 }
213 }
214
215 return 0;
216
217found:
218 return pfc->irqs[i];
219}
220
221static int gpio_pin_setup(struct sh_pfc_chip *chip)
222{
223 struct sh_pfc *pfc = chip->pfc;
224 struct gpio_chip *gc = &chip->gpio_chip;
225 int ret;
226
227 chip->pins = devm_kcalloc(pfc->dev,
228 pfc->info->nr_pins, sizeof(*chip->pins),
229 GFP_KERNEL);
230 if (chip->pins == NULL)
231 return -ENOMEM;
232
233 ret = gpio_setup_data_regs(chip);
234 if (ret < 0)
235 return ret;
236
237 gc->request = gpio_pin_request;
238 gc->free = gpio_pin_free;
239 gc->direction_input = gpio_pin_direction_input;
240 gc->get = gpio_pin_get;
241 gc->direction_output = gpio_pin_direction_output;
242 gc->set = gpio_pin_set;
243 gc->to_irq = gpio_pin_to_irq;
244
245 gc->label = pfc->info->name;
246 gc->parent = pfc->dev;
247 gc->owner = THIS_MODULE;
248 gc->base = 0;
249 gc->ngpio = pfc->nr_gpio_pins;
250
251 return 0;
252}
253
254/* -----------------------------------------------------------------------------
255 * Function GPIOs
256 */
257
258#ifdef CONFIG_SUPERH
259static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
260{
261 static bool __print_once;
262 struct sh_pfc *pfc = gpio_to_pfc(gc);
263 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
264 unsigned long flags;
265 int ret;
266
267 if (!__print_once) {
268 dev_notice(pfc->dev,
269 "Use of GPIO API for function requests is deprecated."
270 " Convert to pinctrl\n");
271 __print_once = true;
272 }
273
274 if (mark == 0)
275 return -EINVAL;
276
277 spin_lock_irqsave(&pfc->lock, flags);
278 ret = sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION);
279 spin_unlock_irqrestore(&pfc->lock, flags);
280
281 return ret;
282}
283
284static int gpio_function_setup(struct sh_pfc_chip *chip)
285{
286 struct sh_pfc *pfc = chip->pfc;
287 struct gpio_chip *gc = &chip->gpio_chip;
288
289 gc->request = gpio_function_request;
290
291 gc->label = pfc->info->name;
292 gc->owner = THIS_MODULE;
293 gc->base = pfc->nr_gpio_pins;
294 gc->ngpio = pfc->info->nr_func_gpios;
295
296 return 0;
297}
298#endif
299
300/* -----------------------------------------------------------------------------
301 * Register/unregister
302 */
303
304static struct sh_pfc_chip *
305sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *),
306 struct sh_pfc_window *mem)
307{
308 struct sh_pfc_chip *chip;
309 int ret;
310
311 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
312 if (unlikely(!chip))
313 return ERR_PTR(-ENOMEM);
314
315 chip->mem = mem;
316 chip->pfc = pfc;
317
318 ret = setup(chip);
319 if (ret < 0)
320 return ERR_PTR(ret);
321
322 ret = devm_gpiochip_add_data(pfc->dev, &chip->gpio_chip, chip);
323 if (unlikely(ret < 0))
324 return ERR_PTR(ret);
325
326 dev_info(pfc->dev, "%s handling gpio %u -> %u\n",
327 chip->gpio_chip.label, chip->gpio_chip.base,
328 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
329
330 return chip;
331}
332
333int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
334{
335 struct sh_pfc_chip *chip;
336 phys_addr_t address;
337 unsigned int i;
338
339 if (pfc->info->data_regs == NULL)
340 return 0;
341
342 /* Find the memory window that contain the GPIO registers. Boards that
343 * register a separate GPIO device will not supply a memory resource
344 * that covers the data registers. In that case don't try to handle
345 * GPIOs.
346 */
347 address = pfc->info->data_regs[0].reg;
348 for (i = 0; i < pfc->num_windows; ++i) {
349 struct sh_pfc_window *window = &pfc->windows[i];
350
351 if (address >= window->phys &&
352 address < window->phys + window->size)
353 break;
354 }
355
356 if (i == pfc->num_windows)
357 return 0;
358
359 /* If we have IRQ resources make sure their number is correct. */
360 if (pfc->num_irqs != pfc->info->gpio_irq_size) {
361 dev_err(pfc->dev, "invalid number of IRQ resources\n");
362 return -EINVAL;
363 }
364
365 /* Register the real GPIOs chip. */
366 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup, &pfc->windows[i]);
367 if (IS_ERR(chip))
368 return PTR_ERR(chip);
369
370 pfc->gpio = chip;
371
372 if (IS_ENABLED(CONFIG_OF) && pfc->dev->of_node)
373 return 0;
374
375#ifdef CONFIG_SUPERH
376 /*
377 * Register the GPIO to pin mappings. As pins with GPIO ports
378 * must come first in the ranges, skip the pins without GPIO
379 * ports by stopping at the first range that contains such a
380 * pin.
381 */
382 for (i = 0; i < pfc->nr_ranges; ++i) {
383 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
384 int ret;
385
386 if (range->start >= pfc->nr_gpio_pins)
387 break;
388
389 ret = gpiochip_add_pin_range(&chip->gpio_chip,
390 dev_name(pfc->dev), range->start, range->start,
391 range->end - range->start + 1);
392 if (ret < 0)
393 return ret;
394 }
395
396 /* Register the function GPIOs chip. */
397 if (pfc->info->nr_func_gpios == 0)
398 return 0;
399
400 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL);
401 if (IS_ERR(chip))
402 return PTR_ERR(chip);
403#endif /* CONFIG_SUPERH */
404
405 return 0;
406}