Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / pinctrl / sh-pfc / gpio.c
blob946d9be50b62cb329250762780c809805ed79735
1 /*
2 * SuperH Pin Function Controller GPIO driver.
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
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.
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>
20 #include "core.h"
22 struct sh_pfc_gpio_data_reg {
23 const struct pinmux_data_reg *info;
24 u32 shadow;
27 struct sh_pfc_gpio_pin {
28 u8 dbit;
29 u8 dreg;
32 struct sh_pfc_chip {
33 struct sh_pfc *pfc;
34 struct gpio_chip gpio_chip;
36 struct sh_pfc_window *mem;
37 struct sh_pfc_gpio_data_reg *regs;
38 struct sh_pfc_gpio_pin *pins;
41 static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
43 struct sh_pfc_chip *chip = gpiochip_get_data(gc);
44 return chip->pfc;
47 static 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)
51 int idx = sh_pfc_get_pin_index(chip->pfc, offset);
52 struct sh_pfc_gpio_pin *gpio_pin = &chip->pins[idx];
54 *reg = &chip->regs[gpio_pin->dreg];
55 *bit = gpio_pin->dbit;
58 static u32 gpio_read_data_reg(struct sh_pfc_chip *chip,
59 const struct pinmux_data_reg *dreg)
61 phys_addr_t address = dreg->reg;
62 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
64 return sh_pfc_read_raw_reg(mem, dreg->reg_width);
67 static void gpio_write_data_reg(struct sh_pfc_chip *chip,
68 const struct pinmux_data_reg *dreg, u32 value)
70 phys_addr_t address = dreg->reg;
71 void __iomem *mem = address - chip->mem->phys + chip->mem->virt;
73 sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
76 static void gpio_setup_data_reg(struct sh_pfc_chip *chip, unsigned idx)
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;
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;
95 BUG();
98 static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
100 struct sh_pfc *pfc = chip->pfc;
101 const struct pinmux_data_reg *dreg;
102 unsigned int i;
104 /* Count the number of data registers, allocate memory and initialize
105 * them.
107 for (i = 0; pfc->info->data_regs[i].reg_width; ++i)
110 chip->regs = devm_kzalloc(pfc->dev, i * sizeof(*chip->regs),
111 GFP_KERNEL);
112 if (chip->regs == NULL)
113 return -ENOMEM;
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);
120 for (i = 0; i < pfc->info->nr_pins; i++) {
121 if (pfc->info->pins[i].enum_id == 0)
122 continue;
124 gpio_setup_data_reg(chip, i);
127 return 0;
130 /* -----------------------------------------------------------------------------
131 * Pin GPIOs
134 static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
136 struct sh_pfc *pfc = gpio_to_pfc(gc);
137 int idx = sh_pfc_get_pin_index(pfc, offset);
139 if (idx < 0 || pfc->info->pins[idx].enum_id == 0)
140 return -EINVAL;
142 return pinctrl_gpio_request(offset);
145 static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
147 return pinctrl_gpio_free(offset);
150 static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
151 int value)
153 struct sh_pfc_gpio_data_reg *reg;
154 unsigned int bit;
155 unsigned int pos;
157 gpio_get_data_reg(chip, offset, &reg, &bit);
159 pos = reg->info->reg_width - (bit + 1);
161 if (value)
162 reg->shadow |= BIT(pos);
163 else
164 reg->shadow &= ~BIT(pos);
166 gpio_write_data_reg(chip, reg->info, reg->shadow);
169 static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
171 return pinctrl_gpio_direction_input(offset);
174 static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
175 int value)
177 gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
179 return pinctrl_gpio_direction_output(offset);
182 static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
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;
189 gpio_get_data_reg(chip, offset, &reg, &bit);
191 pos = reg->info->reg_width - (bit + 1);
193 return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
196 static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
198 gpio_pin_set_value(gpiochip_get_data(gc), offset, value);
201 static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
203 struct sh_pfc *pfc = gpio_to_pfc(gc);
204 unsigned int i, k;
206 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
207 const short *gpios = pfc->info->gpio_irq[i].gpios;
209 for (k = 0; gpios[k] >= 0; k++) {
210 if (gpios[k] == offset)
211 goto found;
215 return 0;
217 found:
218 return pfc->irqs[i];
221 static int gpio_pin_setup(struct sh_pfc_chip *chip)
223 struct sh_pfc *pfc = chip->pfc;
224 struct gpio_chip *gc = &chip->gpio_chip;
225 int ret;
227 chip->pins = devm_kzalloc(pfc->dev, pfc->info->nr_pins *
228 sizeof(*chip->pins), GFP_KERNEL);
229 if (chip->pins == NULL)
230 return -ENOMEM;
232 ret = gpio_setup_data_regs(chip);
233 if (ret < 0)
234 return ret;
236 gc->request = gpio_pin_request;
237 gc->free = gpio_pin_free;
238 gc->direction_input = gpio_pin_direction_input;
239 gc->get = gpio_pin_get;
240 gc->direction_output = gpio_pin_direction_output;
241 gc->set = gpio_pin_set;
242 gc->to_irq = gpio_pin_to_irq;
244 gc->label = pfc->info->name;
245 gc->parent = pfc->dev;
246 gc->owner = THIS_MODULE;
247 gc->base = 0;
248 gc->ngpio = pfc->nr_gpio_pins;
250 return 0;
253 /* -----------------------------------------------------------------------------
254 * Function GPIOs
257 #ifdef CONFIG_SUPERH
258 static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
260 static bool __print_once;
261 struct sh_pfc *pfc = gpio_to_pfc(gc);
262 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
263 unsigned long flags;
264 int ret;
266 if (!__print_once) {
267 dev_notice(pfc->dev,
268 "Use of GPIO API for function requests is deprecated."
269 " Convert to pinctrl\n");
270 __print_once = true;
273 if (mark == 0)
274 return -EINVAL;
276 spin_lock_irqsave(&pfc->lock, flags);
277 ret = sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION);
278 spin_unlock_irqrestore(&pfc->lock, flags);
280 return ret;
283 static int gpio_function_setup(struct sh_pfc_chip *chip)
285 struct sh_pfc *pfc = chip->pfc;
286 struct gpio_chip *gc = &chip->gpio_chip;
288 gc->request = gpio_function_request;
290 gc->label = pfc->info->name;
291 gc->owner = THIS_MODULE;
292 gc->base = pfc->nr_gpio_pins;
293 gc->ngpio = pfc->info->nr_func_gpios;
295 return 0;
297 #endif
299 /* -----------------------------------------------------------------------------
300 * Register/unregister
303 static struct sh_pfc_chip *
304 sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *),
305 struct sh_pfc_window *mem)
307 struct sh_pfc_chip *chip;
308 int ret;
310 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
311 if (unlikely(!chip))
312 return ERR_PTR(-ENOMEM);
314 chip->mem = mem;
315 chip->pfc = pfc;
317 ret = setup(chip);
318 if (ret < 0)
319 return ERR_PTR(ret);
321 ret = devm_gpiochip_add_data(pfc->dev, &chip->gpio_chip, chip);
322 if (unlikely(ret < 0))
323 return ERR_PTR(ret);
325 dev_info(pfc->dev, "%s handling gpio %u -> %u\n",
326 chip->gpio_chip.label, chip->gpio_chip.base,
327 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
329 return chip;
332 int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
334 struct sh_pfc_chip *chip;
335 phys_addr_t address;
336 unsigned int i;
338 if (pfc->info->data_regs == NULL)
339 return 0;
341 /* Find the memory window that contain the GPIO registers. Boards that
342 * register a separate GPIO device will not supply a memory resource
343 * that covers the data registers. In that case don't try to handle
344 * GPIOs.
346 address = pfc->info->data_regs[0].reg;
347 for (i = 0; i < pfc->num_windows; ++i) {
348 struct sh_pfc_window *window = &pfc->windows[i];
350 if (address >= window->phys &&
351 address < window->phys + window->size)
352 break;
355 if (i == pfc->num_windows)
356 return 0;
358 /* If we have IRQ resources make sure their number is correct. */
359 if (pfc->num_irqs != pfc->info->gpio_irq_size) {
360 dev_err(pfc->dev, "invalid number of IRQ resources\n");
361 return -EINVAL;
364 /* Register the real GPIOs chip. */
365 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup, &pfc->windows[i]);
366 if (IS_ERR(chip))
367 return PTR_ERR(chip);
369 pfc->gpio = chip;
371 if (IS_ENABLED(CONFIG_OF) && pfc->dev->of_node)
372 return 0;
374 #ifdef CONFIG_SUPERH
376 * Register the GPIO to pin mappings. As pins with GPIO ports
377 * must come first in the ranges, skip the pins without GPIO
378 * ports by stopping at the first range that contains such a
379 * pin.
381 for (i = 0; i < pfc->nr_ranges; ++i) {
382 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
383 int ret;
385 if (range->start >= pfc->nr_gpio_pins)
386 break;
388 ret = gpiochip_add_pin_range(&chip->gpio_chip,
389 dev_name(pfc->dev), range->start, range->start,
390 range->end - range->start + 1);
391 if (ret < 0)
392 return ret;
395 /* Register the function GPIOs chip. */
396 if (pfc->info->nr_func_gpios == 0)
397 return 0;
399 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup, NULL);
400 if (IS_ERR(chip))
401 return PTR_ERR(chip);
402 #endif /* CONFIG_SUPERH */
404 return 0;