sh-pfc: Don't modify pinmux_data_reg SoC data
[firefly-linux-kernel-4.4.55.git] / drivers / pinctrl / sh-pfc / gpio.c
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  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME " gpio: " fmt
13
14 #include <linux/device.h>
15 #include <linux/gpio.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21
22 #include "core.h"
23
24 struct sh_pfc_gpio_data_reg {
25         const struct pinmux_data_reg *info;
26         unsigned long shadow;
27 };
28
29 struct sh_pfc_chip {
30         struct sh_pfc           *pfc;
31         struct gpio_chip        gpio_chip;
32
33         struct sh_pfc_window    *mem;
34         struct sh_pfc_gpio_data_reg     *regs;
35 };
36
37 static struct sh_pfc_chip *gpio_to_pfc_chip(struct gpio_chip *gc)
38 {
39         return container_of(gc, struct sh_pfc_chip, gpio_chip);
40 }
41
42 static struct sh_pfc *gpio_to_pfc(struct gpio_chip *gc)
43 {
44         return gpio_to_pfc_chip(gc)->pfc;
45 }
46
47 static void gpio_get_data_reg(struct sh_pfc_chip *chip, unsigned int gpio,
48                               struct sh_pfc_gpio_data_reg **reg,
49                               unsigned int *bit)
50 {
51         struct sh_pfc_pin *gpiop = sh_pfc_get_pin(chip->pfc, gpio);
52         unsigned int reg_idx;
53
54         reg_idx = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
55
56         *reg = &chip->regs[reg_idx];
57         *bit = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
58 }
59
60 static unsigned long gpio_read_data_reg(struct sh_pfc_chip *chip,
61                                         const struct pinmux_data_reg *dreg)
62 {
63         void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt;
64
65         return sh_pfc_read_raw_reg(mem, dreg->reg_width);
66 }
67
68 static void gpio_write_data_reg(struct sh_pfc_chip *chip,
69                                 const struct pinmux_data_reg *dreg,
70                                 unsigned long value)
71 {
72         void __iomem *mem = dreg->reg - chip->mem->phys + chip->mem->virt;
73
74         sh_pfc_write_raw_reg(mem, dreg->reg_width, value);
75 }
76
77 static void gpio_setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
78 {
79         struct sh_pfc_pin *gpiop = &pfc->info->pins[gpio];
80         const struct pinmux_data_reg *dreg;
81         unsigned int bit;
82         unsigned int i;
83
84         for (i = 0, dreg = pfc->info->data_regs; dreg->reg; ++i, ++dreg) {
85                 for (bit = 0; bit < dreg->reg_width; bit++) {
86                         if (dreg->enum_ids[bit] == gpiop->enum_id) {
87                                 gpiop->flags &= ~PINMUX_FLAG_DREG;
88                                 gpiop->flags |= i << PINMUX_FLAG_DREG_SHIFT;
89                                 gpiop->flags &= ~PINMUX_FLAG_DBIT;
90                                 gpiop->flags |= bit << PINMUX_FLAG_DBIT_SHIFT;
91                                 return;
92                         }
93                 }
94         }
95
96         BUG();
97 }
98
99 static int gpio_setup_data_regs(struct sh_pfc_chip *chip)
100 {
101         struct sh_pfc *pfc = chip->pfc;
102         unsigned long addr = pfc->info->data_regs[0].reg;
103         const struct pinmux_data_reg *dreg;
104         unsigned int i;
105
106         /* Find the window that contain the GPIO registers. */
107         for (i = 0; i < pfc->num_windows; ++i) {
108                 struct sh_pfc_window *window = &pfc->window[i];
109
110                 if (addr >= window->phys && addr < window->phys + window->size)
111                         break;
112         }
113
114         if (i == pfc->num_windows)
115                 return -EINVAL;
116
117         /* GPIO data registers must be in the first memory resource. */
118         chip->mem = &pfc->window[i];
119
120         /* Count the number of data registers, allocate memory and initialize
121          * them.
122          */
123         for (i = 0; pfc->info->data_regs[i].reg_width; ++i)
124                 ;
125
126         chip->regs = devm_kzalloc(pfc->dev, i * sizeof(*chip->regs),
127                                   GFP_KERNEL);
128         if (chip->regs == NULL)
129                 return -ENOMEM;
130
131         for (i = 0, dreg = pfc->info->data_regs; dreg->reg_width; ++i, ++dreg) {
132                 chip->regs[i].info = dreg;
133                 chip->regs[i].shadow = gpio_read_data_reg(chip, dreg);
134         }
135
136         for (i = 0; i < pfc->info->nr_pins; i++) {
137                 if (pfc->info->pins[i].enum_id == 0)
138                         continue;
139
140                 gpio_setup_data_reg(pfc, i);
141         }
142
143         return 0;
144 }
145
146 /* -----------------------------------------------------------------------------
147  * Pin GPIOs
148  */
149
150 static int gpio_pin_request(struct gpio_chip *gc, unsigned offset)
151 {
152         struct sh_pfc *pfc = gpio_to_pfc(gc);
153         struct sh_pfc_pin *pin = sh_pfc_get_pin(pfc, offset);
154
155         if (pin == NULL || pin->enum_id == 0)
156                 return -EINVAL;
157
158         return pinctrl_request_gpio(offset);
159 }
160
161 static void gpio_pin_free(struct gpio_chip *gc, unsigned offset)
162 {
163         return pinctrl_free_gpio(offset);
164 }
165
166 static void gpio_pin_set_value(struct sh_pfc_chip *chip, unsigned offset,
167                                int value)
168 {
169         struct sh_pfc_gpio_data_reg *reg;
170         unsigned long pos;
171         unsigned int bit;
172
173         gpio_get_data_reg(chip, offset, &reg, &bit);
174
175         pos = reg->info->reg_width - (bit + 1);
176
177         if (value)
178                 set_bit(pos, &reg->shadow);
179         else
180                 clear_bit(pos, &reg->shadow);
181
182         gpio_write_data_reg(chip, reg->info, reg->shadow);
183 }
184
185 static int gpio_pin_direction_input(struct gpio_chip *gc, unsigned offset)
186 {
187         return pinctrl_gpio_direction_input(offset);
188 }
189
190 static int gpio_pin_direction_output(struct gpio_chip *gc, unsigned offset,
191                                     int value)
192 {
193         gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
194
195         return pinctrl_gpio_direction_output(offset);
196 }
197
198 static int gpio_pin_get(struct gpio_chip *gc, unsigned offset)
199 {
200         struct sh_pfc_chip *chip = gpio_to_pfc_chip(gc);
201         struct sh_pfc_gpio_data_reg *reg;
202         unsigned long pos;
203         unsigned int bit;
204
205         gpio_get_data_reg(chip, offset, &reg, &bit);
206
207         pos = reg->info->reg_width - (bit + 1);
208
209         return (gpio_read_data_reg(chip, reg->info) >> pos) & 1;
210 }
211
212 static void gpio_pin_set(struct gpio_chip *gc, unsigned offset, int value)
213 {
214         gpio_pin_set_value(gpio_to_pfc_chip(gc), offset, value);
215 }
216
217 static int gpio_pin_to_irq(struct gpio_chip *gc, unsigned offset)
218 {
219         struct sh_pfc *pfc = gpio_to_pfc(gc);
220         int i, k;
221
222         for (i = 0; i < pfc->info->gpio_irq_size; i++) {
223                 unsigned short *gpios = pfc->info->gpio_irq[i].gpios;
224
225                 for (k = 0; gpios[k]; k++) {
226                         if (gpios[k] == offset)
227                                 return pfc->info->gpio_irq[i].irq;
228                 }
229         }
230
231         return -ENOSYS;
232 }
233
234 static int gpio_pin_setup(struct sh_pfc_chip *chip)
235 {
236         struct sh_pfc *pfc = chip->pfc;
237         struct gpio_chip *gc = &chip->gpio_chip;
238         int ret;
239
240         ret = gpio_setup_data_regs(chip);
241         if (ret < 0)
242                 return ret;
243
244         gc->request = gpio_pin_request;
245         gc->free = gpio_pin_free;
246         gc->direction_input = gpio_pin_direction_input;
247         gc->get = gpio_pin_get;
248         gc->direction_output = gpio_pin_direction_output;
249         gc->set = gpio_pin_set;
250         gc->to_irq = gpio_pin_to_irq;
251
252         gc->label = pfc->info->name;
253         gc->dev = pfc->dev;
254         gc->owner = THIS_MODULE;
255         gc->base = 0;
256         gc->ngpio = pfc->nr_pins;
257
258         return 0;
259 }
260
261 /* -----------------------------------------------------------------------------
262  * Function GPIOs
263  */
264
265 static int gpio_function_request(struct gpio_chip *gc, unsigned offset)
266 {
267         struct sh_pfc *pfc = gpio_to_pfc(gc);
268         unsigned int mark = pfc->info->func_gpios[offset].enum_id;
269         unsigned long flags;
270         int ret = -EINVAL;
271
272         pr_notice_once("Use of GPIO API for function requests is deprecated, convert to pinctrl\n");
273
274         if (mark == 0)
275                 return ret;
276
277         spin_lock_irqsave(&pfc->lock, flags);
278
279         if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_DRYRUN))
280                 goto done;
281
282         if (sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_REQ))
283                 goto done;
284
285         ret = 0;
286
287 done:
288         spin_unlock_irqrestore(&pfc->lock, flags);
289         return ret;
290 }
291
292 static void gpio_function_free(struct gpio_chip *gc, unsigned offset)
293 {
294         struct sh_pfc *pfc = gpio_to_pfc(gc);
295         unsigned int mark = pfc->info->func_gpios[offset].enum_id;
296         unsigned long flags;
297
298         spin_lock_irqsave(&pfc->lock, flags);
299
300         sh_pfc_config_mux(pfc, mark, PINMUX_TYPE_FUNCTION, GPIO_CFG_FREE);
301
302         spin_unlock_irqrestore(&pfc->lock, flags);
303 }
304
305 static int gpio_function_setup(struct sh_pfc_chip *chip)
306 {
307         struct sh_pfc *pfc = chip->pfc;
308         struct gpio_chip *gc = &chip->gpio_chip;
309
310         gc->request = gpio_function_request;
311         gc->free = gpio_function_free;
312
313         gc->label = pfc->info->name;
314         gc->owner = THIS_MODULE;
315         gc->base = pfc->nr_pins;
316         gc->ngpio = pfc->info->nr_func_gpios;
317
318         return 0;
319 }
320
321 /* -----------------------------------------------------------------------------
322  * Register/unregister
323  */
324
325 static struct sh_pfc_chip *
326 sh_pfc_add_gpiochip(struct sh_pfc *pfc, int(*setup)(struct sh_pfc_chip *))
327 {
328         struct sh_pfc_chip *chip;
329         int ret;
330
331         chip = devm_kzalloc(pfc->dev, sizeof(*chip), GFP_KERNEL);
332         if (unlikely(!chip))
333                 return ERR_PTR(-ENOMEM);
334
335         chip->pfc = pfc;
336
337         ret = setup(chip);
338         if (ret < 0)
339                 return ERR_PTR(ret);
340
341         ret = gpiochip_add(&chip->gpio_chip);
342         if (unlikely(ret < 0))
343                 return ERR_PTR(ret);
344
345         pr_info("%s handling gpio %u -> %u\n",
346                 chip->gpio_chip.label, chip->gpio_chip.base,
347                 chip->gpio_chip.base + chip->gpio_chip.ngpio - 1);
348
349         return chip;
350 }
351
352 int sh_pfc_register_gpiochip(struct sh_pfc *pfc)
353 {
354         const struct pinmux_range *ranges;
355         struct pinmux_range def_range;
356         struct sh_pfc_chip *chip;
357         unsigned int nr_ranges;
358         unsigned int i;
359         int ret;
360
361         /* Register the real GPIOs chip. */
362         chip = sh_pfc_add_gpiochip(pfc, gpio_pin_setup);
363         if (IS_ERR(chip))
364                 return PTR_ERR(chip);
365
366         pfc->gpio = chip;
367
368         /* Register the GPIO to pin mappings. */
369         if (pfc->info->ranges == NULL) {
370                 def_range.begin = 0;
371                 def_range.end = pfc->info->nr_pins - 1;
372                 ranges = &def_range;
373                 nr_ranges = 1;
374         } else {
375                 ranges = pfc->info->ranges;
376                 nr_ranges = pfc->info->nr_ranges;
377         }
378
379         for (i = 0; i < nr_ranges; ++i) {
380                 const struct pinmux_range *range = &ranges[i];
381
382                 ret = gpiochip_add_pin_range(&chip->gpio_chip,
383                                              dev_name(pfc->dev),
384                                              range->begin, range->begin,
385                                              range->end - range->begin + 1);
386                 if (ret < 0)
387                         return ret;
388         }
389
390         /* Register the function GPIOs chip. */
391         chip = sh_pfc_add_gpiochip(pfc, gpio_function_setup);
392         if (IS_ERR(chip))
393                 return PTR_ERR(chip);
394
395         pfc->func = chip;
396
397         return 0;
398 }
399
400 int sh_pfc_unregister_gpiochip(struct sh_pfc *pfc)
401 {
402         int err;
403         int ret;
404
405         ret = gpiochip_remove(&pfc->gpio->gpio_chip);
406         err = gpiochip_remove(&pfc->func->gpio_chip);
407
408         return ret < 0 ? ret : err;
409 }