sh-pfc: Simplify the sh_pfc_gpio_is_pin() logic
[deliverable/linux.git] / drivers / pinctrl / sh-pfc / gpio.c
CommitLineData
b3c185a7
PM
1/*
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 */
c6193eac
LP
11
12#define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
b3c185a7 13
1724acfd 14#include <linux/device.h>
b3c185a7 15#include <linux/gpio.h>
90efde22 16#include <linux/init.h>
b3c185a7 17#include <linux/module.h>
ca5481c6 18#include <linux/pinctrl/consumer.h>
90efde22
LP
19#include <linux/slab.h>
20#include <linux/spinlock.h>
b3c185a7 21
f9165132
LP
22#include "core.h"
23
b3c185a7
PM
24struct sh_pfc_chip {
25 struct sh_pfc *pfc;
26 struct gpio_chip gpio_chip;
27};
28
29static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
30{
31 return container_of(gc, struct sh_pfc_chip, gpio_chip);
32}
33
34static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
35{
36 return gpio_to_pfc_chip(gc)->pfc;
37}
38
16883814
LP
39/* -----------------------------------------------------------------------------
40 * Pin GPIOs
41 */
b3c185a7 42
16883814 43static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
b3c185a7 44{
0b73ee5d
LP
45 struct sh_pfc *pfc = gpio_to_pfc(gc);
46
47 if (pfc->info->pins[offset].enum_id == 0)
48 return -EINVAL;
49
16883814 50 return pinctrl_request_gpio(offset);
b3c185a7
PM
51}
52
16883814 53static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
b3c185a7 54{
16883814 55 return pinctrl_free_gpio(offset);
b3c185a7
PM
56}
57
16883814 58static void gpio_pin_set_value(struct sh_pfc *pfc, unsigned offset, int value)
b3c185a7 59{
0b73ee5d
LP
60 struct pinmux_data_reg *dr;
61 int bit;
b3c185a7 62
0b73ee5d 63 sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
16883814 64 sh_pfc_write_bit(dr, bit, value);
b3c185a7
PM
65}
66
16883814 67static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
ca5481c6
PM
68{
69 return pinctrl_gpio_direction_input(offset);
70}
71
16883814 72static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
ca5481c6
PM
73 int value)
74{
16883814 75 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
ca5481c6
PM
76
77 return pinctrl_gpio_direction_output(offset);
78}
79
16883814 80static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
b3c185a7 81{
16883814 82 struct sh_pfc *pfc = gpio_to_pfc(gc);
0b73ee5d
LP
83 struct pinmux_data_reg *dr;
84 int bit;
16883814 85
0b73ee5d 86 sh_pfc_get_data_reg(pfc, offset, &dr, &bit);
16883814 87 return sh_pfc_read_bit(dr, bit);
b3c185a7
PM
88}
89
16883814 90static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
b3c185a7 91{
16883814 92 gpio_pin_set_value(gpio_to_pfc(gc), offset, value);
b3c185a7
PM
93}
94
16883814 95static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
b3c185a7
PM
96{
97 struct sh_pfc *pfc = gpio_to_pfc(gc);
c07f54f6
LP
98 int i, k;
99
100 for (i = 0; i < pfc->info->gpio_irq_size; i++) {
101 unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
102
103 for (k = 0; gpios[k]; k++) {
104 if (gpios[k] == offset)
105 return pfc->info->gpio_irq[i].irq;
b3c185a7
PM
106 }
107 }
108
109 return -ENOSYS;
110}
111
16883814 112static void gpio_pin_setup(struct sh_pfc_chip *chip)
b3c185a7
PM
113{
114 struct sh_pfc *pfc = chip->pfc;
115 struct gpio_chip *gc = &chip->gpio_chip;
116
16883814
LP
117 gc->request = gpio_pin_request;
118 gc->free = gpio_pin_free;
119 gc->direction_input = gpio_pin_direction_input;
120 gc->get = gpio_pin_get;
121 gc->direction_output = gpio_pin_direction_output;
122 gc->set = gpio_pin_set;
123 gc->to_irq = gpio_pin_to_irq;
b3c185a7 124
19bb7fe3 125 gc->label = pfc->info->name;
16883814 126 gc->dev = pfc->dev;
b3c185a7 127 gc->owner = THIS_MODULE;
d7a7ca57 128 gc->base = 0;
16883814 129 gc->ngpio = pfc->info->nr_pins;
b3c185a7
PM
130}
131
16883814
LP
132/* -----------------------------------------------------------------------------
133 * Function GPIOs
134 */
135
136static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
137{
138 struct sh_pfc *pfc = gpio_to_pfc(gc);
a68fdca9 139 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
16883814
LP
140 unsigned long flags;
141 int ret = -EINVAL;
142
143 pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
144
a68fdca9 145 if (mark == 0)
16883814
LP
146 return ret;
147
148 spin_lock_irqsave(&pfc->lock, flags);
149
a68fdca9 150 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
16883814
LP
151 goto done;
152
a68fdca9 153 if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
16883814
LP
154 goto done;
155
156 ret = 0;
157
158done:
159 spin_unlock_irqrestore(&pfc->lock, flags);
160 return ret;
161}
162
163static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
164{
165 struct sh_pfc *pfc = gpio_to_pfc(gc);
a68fdca9 166 unsigned int mark = pfc->info->func_gpios[offset].enum_id;
16883814
LP
167 unsigned long flags;
168
169 spin_lock_irqsave(&pfc->lock, flags);
170
a68fdca9 171 sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
16883814
LP
172
173 spin_unlock_irqrestore(&pfc->lock, flags);
174}
175
176static void gpio_function_setup(struct sh_pfc_chip *chip)
177{
178 struct sh_pfc *pfc = chip->pfc;
179 struct gpio_chip *gc = &chip->gpio_chip;
180
181 gc->request = gpio_function_request;
182 gc->free = gpio_function_free;
183
184 gc->label = pfc->info->name;
185 gc->owner = THIS_MODULE;
186 gc->base = pfc->info->nr_pins;
187 gc->ngpio = pfc->info->nr_func_gpios;
188}
189
190/* -----------------------------------------------------------------------------
191 * Register/unregister
192 */
193
194static struct sh_pfc_chip *
195sh_pfc_add_gpiochip(struct sh_pfc *pfc, void(*setup)(struct sh_pfc_chip *))
b3c185a7
PM
196{
197 struct sh_pfc_chip *chip;
198 int ret;
199
1724acfd 200 chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
b3c185a7 201 if (unlikely(!chip))
16883814 202 return ERR_PTR(-ENOMEM);
b3c185a7
PM
203
204 chip->pfc = pfc;
205
16883814 206 setup(chip);
b3c185a7
PM
207
208 ret = gpiochip_add(&chip->gpio_chip);
1724acfd 209 if (unlikely(ret < 0))
16883814
LP
210 return ERR_PTR(ret);
211
212 pr_info("%s handling gpio %u -> %u\n",
213 chip->gpio_chip.label, chip->gpio_chip.base,
214 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
215
216 return chip;
217}
218
219int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
220{
221 struct sh_pfc_chip *chip;
222
223 chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
224 if (IS_ERR(chip))
225 return PTR_ERR(chip);
6f6a4a68
LP
226
227 pfc->gpio = chip;
b3c185a7 228
16883814
LP
229 chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
230 if (IS_ERR(chip))
231 return PTR_ERR(chip);
232
233 pfc->func = chip;
b3c185a7 234
b3c185a7
PM
235 return 0;
236}
237
6f6a4a68 238int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
b3c185a7 239{
16883814 240 int err;
b3c185a7
PM
241 int ret;
242
16883814
LP
243 ret = gpiochip_remove(&pfc->gpio->gpio_chip);
244 err = gpiochip_remove(&pfc->func->gpio_chip);
b3c185a7 245
16883814 246 return ret < 0 ? ret : err;
b3c185a7 247}
This page took 0.108563 seconds and 5 git commands to generate.