pinctrl: st: Move st_get_pio_control() further up the source file
[cascardo/linux.git] / drivers / pinctrl / pinctrl-st.c
1 /*
2  * Copyright (C) 2013 STMicroelectronics (R&D) Limited.
3  * Authors:
4  *      Srinivas Kandagatla <srinivas.kandagatla@st.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/err.h>
15 #include <linux/io.h>
16 #include <linux/of.h>
17 #include <linux/of_irq.h>
18 #include <linux/of_gpio.h>
19 #include <linux/of_address.h>
20 #include <linux/regmap.h>
21 #include <linux/mfd/syscon.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/platform_device.h>
26 #include "core.h"
27
28 /* PIO Block registers */
29 /* PIO output */
30 #define REG_PIO_POUT                    0x00
31 /* Set bits of POUT */
32 #define REG_PIO_SET_POUT                0x04
33 /* Clear bits of POUT */
34 #define REG_PIO_CLR_POUT                0x08
35 /* PIO input */
36 #define REG_PIO_PIN                     0x10
37 /* PIO configuration */
38 #define REG_PIO_PC(n)                   (0x20 + (n) * 0x10)
39 /* Set bits of PC[2:0] */
40 #define REG_PIO_SET_PC(n)               (0x24 + (n) * 0x10)
41 /* Clear bits of PC[2:0] */
42 #define REG_PIO_CLR_PC(n)               (0x28 + (n) * 0x10)
43 /* PIO input comparison */
44 #define REG_PIO_PCOMP                   0x50
45 /* Set bits of PCOMP */
46 #define REG_PIO_SET_PCOMP               0x54
47 /* Clear bits of PCOMP */
48 #define REG_PIO_CLR_PCOMP               0x58
49 /* PIO input comparison mask */
50 #define REG_PIO_PMASK                   0x60
51 /* Set bits of PMASK */
52 #define REG_PIO_SET_PMASK               0x64
53 /* Clear bits of PMASK */
54 #define REG_PIO_CLR_PMASK               0x68
55
56 #define ST_GPIO_DIRECTION_BIDIR 0x1
57 #define ST_GPIO_DIRECTION_OUT   0x2
58 #define ST_GPIO_DIRECTION_IN    0x4
59
60 /**
61  *  Packed style retime configuration.
62  *  There are two registers cfg0 and cfg1 in this style for each bank.
63  *  Each field in this register is 8 bit corresponding to 8 pins in the bank.
64  */
65 #define RT_P_CFGS_PER_BANK                      2
66 #define RT_P_CFG0_CLK1NOTCLK0_FIELD(reg)        REG_FIELD(reg, 0, 7)
67 #define RT_P_CFG0_DELAY_0_FIELD(reg)            REG_FIELD(reg, 16, 23)
68 #define RT_P_CFG0_DELAY_1_FIELD(reg)            REG_FIELD(reg, 24, 31)
69 #define RT_P_CFG1_INVERTCLK_FIELD(reg)          REG_FIELD(reg, 0, 7)
70 #define RT_P_CFG1_RETIME_FIELD(reg)             REG_FIELD(reg, 8, 15)
71 #define RT_P_CFG1_CLKNOTDATA_FIELD(reg)         REG_FIELD(reg, 16, 23)
72 #define RT_P_CFG1_DOUBLE_EDGE_FIELD(reg)        REG_FIELD(reg, 24, 31)
73
74 /**
75  * Dedicated style retime Configuration register
76  * each register is dedicated per pin.
77  */
78 #define RT_D_CFGS_PER_BANK              8
79 #define RT_D_CFG_CLK_SHIFT              0
80 #define RT_D_CFG_CLK_MASK               (0x3 << 0)
81 #define RT_D_CFG_CLKNOTDATA_SHIFT       2
82 #define RT_D_CFG_CLKNOTDATA_MASK        BIT(2)
83 #define RT_D_CFG_DELAY_SHIFT            3
84 #define RT_D_CFG_DELAY_MASK             (0xf << 3)
85 #define RT_D_CFG_DELAY_INNOTOUT_SHIFT   7
86 #define RT_D_CFG_DELAY_INNOTOUT_MASK    BIT(7)
87 #define RT_D_CFG_DOUBLE_EDGE_SHIFT      8
88 #define RT_D_CFG_DOUBLE_EDGE_MASK       BIT(8)
89 #define RT_D_CFG_INVERTCLK_SHIFT        9
90 #define RT_D_CFG_INVERTCLK_MASK         BIT(9)
91 #define RT_D_CFG_RETIME_SHIFT           10
92 #define RT_D_CFG_RETIME_MASK            BIT(10)
93
94 /*
95  * Pinconf is represented in an opaque unsigned long variable.
96  * Below is the bit allocation details for each possible configuration.
97  * All the bit fields can be encapsulated into four variables
98  * (direction, retime-type, retime-clk, retime-delay)
99  *
100  *       +----------------+
101  *[31:28]| reserved-3     |
102  *       +----------------+-------------
103  *[27]   |      oe        |             |
104  *       +----------------+             v
105  *[26]   |      pu        |     [Direction      ]
106  *       +----------------+             ^
107  *[25]   |      od        |             |
108  *       +----------------+-------------
109  *[24]   | reserved-2     |
110  *       +----------------+-------------
111  *[23]   |    retime      |             |
112  *       +----------------+             |
113  *[22]   | retime-invclk  |             |
114  *       +----------------+             v
115  *[21]   |retime-clknotdat|     [Retime-type    ]
116  *       +----------------+             ^
117  *[20]   | retime-de      |             |
118  *       +----------------+-------------
119  *[19:18]| retime-clk     |------>[Retime-Clk   ]
120  *       +----------------+
121  *[17:16]|  reserved-1    |
122  *       +----------------+
123  *[15..0]| retime-delay   |------>[Retime Delay]
124  *       +----------------+
125  */
126
127 #define ST_PINCONF_UNPACK(conf, param)\
128                                 ((conf >> ST_PINCONF_ ##param ##_SHIFT) \
129                                 & ST_PINCONF_ ##param ##_MASK)
130
131 #define ST_PINCONF_PACK(conf, val, param)       (conf |=\
132                                 ((val & ST_PINCONF_ ##param ##_MASK) << \
133                                         ST_PINCONF_ ##param ##_SHIFT))
134
135 /* Output enable */
136 #define ST_PINCONF_OE_MASK              0x1
137 #define ST_PINCONF_OE_SHIFT             27
138 #define ST_PINCONF_OE                   BIT(27)
139 #define ST_PINCONF_UNPACK_OE(conf)      ST_PINCONF_UNPACK(conf, OE)
140 #define ST_PINCONF_PACK_OE(conf)        ST_PINCONF_PACK(conf, 1, OE)
141
142 /* Pull Up */
143 #define ST_PINCONF_PU_MASK              0x1
144 #define ST_PINCONF_PU_SHIFT             26
145 #define ST_PINCONF_PU                   BIT(26)
146 #define ST_PINCONF_UNPACK_PU(conf)      ST_PINCONF_UNPACK(conf, PU)
147 #define ST_PINCONF_PACK_PU(conf)        ST_PINCONF_PACK(conf, 1, PU)
148
149 /* Open Drain */
150 #define ST_PINCONF_OD_MASK              0x1
151 #define ST_PINCONF_OD_SHIFT             25
152 #define ST_PINCONF_OD                   BIT(25)
153 #define ST_PINCONF_UNPACK_OD(conf)      ST_PINCONF_UNPACK(conf, OD)
154 #define ST_PINCONF_PACK_OD(conf)        ST_PINCONF_PACK(conf, 1, OD)
155
156 #define ST_PINCONF_RT_MASK              0x1
157 #define ST_PINCONF_RT_SHIFT             23
158 #define ST_PINCONF_RT                   BIT(23)
159 #define ST_PINCONF_UNPACK_RT(conf)      ST_PINCONF_UNPACK(conf, RT)
160 #define ST_PINCONF_PACK_RT(conf)        ST_PINCONF_PACK(conf, 1, RT)
161
162 #define ST_PINCONF_RT_INVERTCLK_MASK    0x1
163 #define ST_PINCONF_RT_INVERTCLK_SHIFT   22
164 #define ST_PINCONF_RT_INVERTCLK         BIT(22)
165 #define ST_PINCONF_UNPACK_RT_INVERTCLK(conf) \
166                         ST_PINCONF_UNPACK(conf, RT_INVERTCLK)
167 #define ST_PINCONF_PACK_RT_INVERTCLK(conf) \
168                         ST_PINCONF_PACK(conf, 1, RT_INVERTCLK)
169
170 #define ST_PINCONF_RT_CLKNOTDATA_MASK   0x1
171 #define ST_PINCONF_RT_CLKNOTDATA_SHIFT  21
172 #define ST_PINCONF_RT_CLKNOTDATA        BIT(21)
173 #define ST_PINCONF_UNPACK_RT_CLKNOTDATA(conf)   \
174                                 ST_PINCONF_UNPACK(conf, RT_CLKNOTDATA)
175 #define ST_PINCONF_PACK_RT_CLKNOTDATA(conf) \
176                                 ST_PINCONF_PACK(conf, 1, RT_CLKNOTDATA)
177
178 #define ST_PINCONF_RT_DOUBLE_EDGE_MASK  0x1
179 #define ST_PINCONF_RT_DOUBLE_EDGE_SHIFT 20
180 #define ST_PINCONF_RT_DOUBLE_EDGE       BIT(20)
181 #define ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(conf) \
182                                 ST_PINCONF_UNPACK(conf, RT_DOUBLE_EDGE)
183 #define ST_PINCONF_PACK_RT_DOUBLE_EDGE(conf) \
184                                 ST_PINCONF_PACK(conf, 1, RT_DOUBLE_EDGE)
185
186 #define ST_PINCONF_RT_CLK_MASK          0x3
187 #define ST_PINCONF_RT_CLK_SHIFT         18
188 #define ST_PINCONF_RT_CLK               BIT(18)
189 #define ST_PINCONF_UNPACK_RT_CLK(conf)  ST_PINCONF_UNPACK(conf, RT_CLK)
190 #define ST_PINCONF_PACK_RT_CLK(conf, val) ST_PINCONF_PACK(conf, val, RT_CLK)
191
192 /* RETIME_DELAY in Pico Secs */
193 #define ST_PINCONF_RT_DELAY_MASK        0xffff
194 #define ST_PINCONF_RT_DELAY_SHIFT       0
195 #define ST_PINCONF_UNPACK_RT_DELAY(conf) ST_PINCONF_UNPACK(conf, RT_DELAY)
196 #define ST_PINCONF_PACK_RT_DELAY(conf, val) \
197                                 ST_PINCONF_PACK(conf, val, RT_DELAY)
198
199 #define ST_GPIO_PINS_PER_BANK   (8)
200 #define OF_GPIO_ARGS_MIN        (4)
201 #define OF_RT_ARGS_MIN          (2)
202
203 #define gpio_range_to_bank(chip) \
204                 container_of(chip, struct st_gpio_bank, range)
205
206 #define gpio_chip_to_bank(chip) \
207                 container_of(chip, struct st_gpio_bank, gpio_chip)
208
209
210 enum st_retime_style {
211         st_retime_style_none,
212         st_retime_style_packed,
213         st_retime_style_dedicated,
214 };
215
216 struct st_retime_dedicated {
217         struct regmap_field *rt[ST_GPIO_PINS_PER_BANK];
218 };
219
220 struct st_retime_packed {
221         struct regmap_field *clk1notclk0;
222         struct regmap_field *delay_0;
223         struct regmap_field *delay_1;
224         struct regmap_field *invertclk;
225         struct regmap_field *retime;
226         struct regmap_field *clknotdata;
227         struct regmap_field *double_edge;
228 };
229
230 struct st_pio_control {
231         u32 rt_pin_mask;
232         struct regmap_field *alt, *oe, *pu, *od;
233         /* retiming */
234         union {
235                 struct st_retime_packed         rt_p;
236                 struct st_retime_dedicated      rt_d;
237         } rt;
238 };
239
240 struct st_pctl_data {
241         const enum st_retime_style      rt_style;
242         const unsigned int              *input_delays;
243         const int                       ninput_delays;
244         const unsigned int              *output_delays;
245         const int                       noutput_delays;
246         /* register offset information */
247         const int alt, oe, pu, od, rt;
248 };
249
250 struct st_pinconf {
251         int             pin;
252         const char      *name;
253         unsigned long   config;
254         int             altfunc;
255 };
256
257 struct st_pmx_func {
258         const char      *name;
259         const char      **groups;
260         unsigned        ngroups;
261 };
262
263 struct st_pctl_group {
264         const char              *name;
265         unsigned int            *pins;
266         unsigned                npins;
267         struct st_pinconf       *pin_conf;
268 };
269
270 /*
271  * Edge triggers are not supported at hardware level, it is supported by
272  * software by exploiting the level trigger support in hardware.
273  * Software uses a virtual register (EDGE_CONF) for edge trigger configuration
274  * of each gpio pin in a GPIO bank.
275  *
276  * Each bank has a 32 bit EDGE_CONF register which is divided in to 8 parts of
277  * 4-bits. Each 4-bit space is allocated for each pin in a gpio bank.
278  *
279  * bit allocation per pin is:
280  * Bits:  [0 - 3] | [4 - 7]  [8 - 11] ... ... ... ...  [ 28 - 31]
281  *       --------------------------------------------------------
282  *       |  pin-0  |  pin-2 | pin-3  | ... ... ... ... | pin -7 |
283  *       --------------------------------------------------------
284  *
285  *  A pin can have one of following the values in its edge configuration field.
286  *
287  *      -------   ----------------------------
288  *      [0-3]   - Description
289  *      -------   ----------------------------
290  *      0000    - No edge IRQ.
291  *      0001    - Falling edge IRQ.
292  *      0010    - Rising edge IRQ.
293  *      0011    - Rising and Falling edge IRQ.
294  *      -------   ----------------------------
295  */
296
297 #define ST_IRQ_EDGE_CONF_BITS_PER_PIN   4
298 #define ST_IRQ_EDGE_MASK                0xf
299 #define ST_IRQ_EDGE_FALLING             BIT(0)
300 #define ST_IRQ_EDGE_RISING              BIT(1)
301 #define ST_IRQ_EDGE_BOTH                (BIT(0) | BIT(1))
302
303 #define ST_IRQ_RISING_EDGE_CONF(pin) \
304         (ST_IRQ_EDGE_RISING << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
305
306 #define ST_IRQ_FALLING_EDGE_CONF(pin) \
307         (ST_IRQ_EDGE_FALLING << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
308
309 #define ST_IRQ_BOTH_EDGE_CONF(pin) \
310         (ST_IRQ_EDGE_BOTH << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
311
312 #define ST_IRQ_EDGE_CONF(conf, pin) \
313         (conf >> (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN) & ST_IRQ_EDGE_MASK)
314
315 struct st_gpio_bank {
316         struct gpio_chip                gpio_chip;
317         struct pinctrl_gpio_range       range;
318         void __iomem                    *base;
319         struct st_pio_control           pc;
320         unsigned long                   irq_edge_conf;
321         spinlock_t                      lock;
322 };
323
324 struct st_pinctrl {
325         struct device                   *dev;
326         struct pinctrl_dev              *pctl;
327         struct st_gpio_bank             *banks;
328         int                             nbanks;
329         struct st_pmx_func              *functions;
330         int                             nfunctions;
331         struct st_pctl_group            *groups;
332         int                             ngroups;
333         struct regmap                   *regmap;
334         const struct st_pctl_data       *data;
335         void __iomem                    *irqmux_base;
336 };
337
338 /* SOC specific data */
339 /* STiH415 data */
340 static const unsigned int stih415_input_delays[] = {0, 500, 1000, 1500};
341 static const unsigned int stih415_output_delays[] = {0, 1000, 2000, 3000};
342
343 #define STIH415_PCTRL_COMMON_DATA                               \
344         .rt_style       = st_retime_style_packed,               \
345         .input_delays   = stih415_input_delays,                 \
346         .ninput_delays  = ARRAY_SIZE(stih415_input_delays),     \
347         .output_delays = stih415_output_delays,                 \
348         .noutput_delays = ARRAY_SIZE(stih415_output_delays)
349
350 static const struct st_pctl_data  stih415_sbc_data = {
351         STIH415_PCTRL_COMMON_DATA,
352         .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 16,
353 };
354
355 static const struct st_pctl_data  stih415_front_data = {
356         STIH415_PCTRL_COMMON_DATA,
357         .alt = 0, .oe = 8, .pu = 10, .od = 12, .rt = 16,
358 };
359
360 static const struct st_pctl_data  stih415_rear_data = {
361         STIH415_PCTRL_COMMON_DATA,
362         .alt = 0, .oe = 6, .pu = 8, .od = 10, .rt = 38,
363 };
364
365 static const struct st_pctl_data  stih415_left_data = {
366         STIH415_PCTRL_COMMON_DATA,
367         .alt = 0, .oe = 3, .pu = 4, .od = 5, .rt = 6,
368 };
369
370 static const struct st_pctl_data  stih415_right_data = {
371         STIH415_PCTRL_COMMON_DATA,
372         .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 11,
373 };
374
375 /* STiH416 data */
376 static const unsigned int stih416_delays[] = {0, 300, 500, 750, 1000, 1250,
377                         1500, 1750, 2000, 2250, 2500, 2750, 3000, 3250 };
378
379 static const struct st_pctl_data  stih416_data = {
380         .rt_style       = st_retime_style_dedicated,
381         .input_delays   = stih416_delays,
382         .ninput_delays  = ARRAY_SIZE(stih416_delays),
383         .output_delays  = stih416_delays,
384         .noutput_delays = ARRAY_SIZE(stih416_delays),
385         .alt = 0, .oe = 40, .pu = 50, .od = 60, .rt = 100,
386 };
387
388 static const struct st_pctl_data stih407_flashdata = {
389         .rt_style       = st_retime_style_none,
390         .input_delays   = stih416_delays,
391         .ninput_delays  = ARRAY_SIZE(stih416_delays),
392         .output_delays  = stih416_delays,
393         .noutput_delays = ARRAY_SIZE(stih416_delays),
394         .alt = 0,
395         .oe = -1, /* Not Available */
396         .pu = -1, /* Not Available */
397         .od = 60,
398         .rt = 100,
399 };
400
401 static struct st_pio_control *st_get_pio_control(
402                         struct pinctrl_dev *pctldev, int pin)
403 {
404         struct pinctrl_gpio_range *range =
405                          pinctrl_find_gpio_range_from_pin(pctldev, pin);
406         struct st_gpio_bank *bank = gpio_range_to_bank(range);
407
408         return &bank->pc;
409 }
410
411 /* Low level functions.. */
412 static inline int st_gpio_bank(int gpio)
413 {
414         return gpio/ST_GPIO_PINS_PER_BANK;
415 }
416
417 static inline int st_gpio_pin(int gpio)
418 {
419         return gpio%ST_GPIO_PINS_PER_BANK;
420 }
421
422 static void st_pinconf_set_config(struct st_pio_control *pc,
423                                 int pin, unsigned long config)
424 {
425         struct regmap_field *output_enable = pc->oe;
426         struct regmap_field *pull_up = pc->pu;
427         struct regmap_field *open_drain = pc->od;
428         unsigned int oe_value, pu_value, od_value;
429         unsigned long mask = BIT(pin);
430
431         if (output_enable) {
432                 regmap_field_read(output_enable, &oe_value);
433                 oe_value &= ~mask;
434                 if (config & ST_PINCONF_OE)
435                         oe_value |= mask;
436                 regmap_field_write(output_enable, oe_value);
437         }
438
439         if (pull_up) {
440                 regmap_field_read(pull_up, &pu_value);
441                 pu_value &= ~mask;
442                 if (config & ST_PINCONF_PU)
443                         pu_value |= mask;
444                 regmap_field_write(pull_up, pu_value);
445         }
446
447         if (open_drain) {
448                 regmap_field_read(open_drain, &od_value);
449                 od_value &= ~mask;
450                 if (config & ST_PINCONF_OD)
451                         od_value |= mask;
452                 regmap_field_write(open_drain, od_value);
453         }
454 }
455
456 static void st_pctl_set_function(struct st_pio_control *pc,
457                                 int pin_id, int function)
458 {
459         struct regmap_field *alt = pc->alt;
460         unsigned int val;
461         int pin = st_gpio_pin(pin_id);
462         int offset = pin * 4;
463
464         if (!alt)
465                 return;
466
467         regmap_field_read(alt, &val);
468         val &= ~(0xf << offset);
469         val |= function << offset;
470         regmap_field_write(alt, val);
471 }
472
473 static unsigned int st_pctl_get_pin_function(struct st_pio_control *pc, int pin)
474 {
475         struct regmap_field *alt = pc->alt;
476         unsigned int val;
477         int offset = pin * 4;
478
479         if (!alt)
480                 return 0;
481
482         regmap_field_read(alt, &val);
483
484         return (val >> offset) & 0xf;
485 }
486
487 static unsigned long st_pinconf_delay_to_bit(unsigned int delay,
488         const struct st_pctl_data *data, unsigned long config)
489 {
490         const unsigned int *delay_times;
491         int num_delay_times, i, closest_index = -1;
492         unsigned int closest_divergence = UINT_MAX;
493
494         if (ST_PINCONF_UNPACK_OE(config)) {
495                 delay_times = data->output_delays;
496                 num_delay_times = data->noutput_delays;
497         } else {
498                 delay_times = data->input_delays;
499                 num_delay_times = data->ninput_delays;
500         }
501
502         for (i = 0; i < num_delay_times; i++) {
503                 unsigned int divergence = abs(delay - delay_times[i]);
504
505                 if (divergence == 0)
506                         return i;
507
508                 if (divergence < closest_divergence) {
509                         closest_divergence = divergence;
510                         closest_index = i;
511                 }
512         }
513
514         pr_warn("Attempt to set delay %d, closest available %d\n",
515              delay, delay_times[closest_index]);
516
517         return closest_index;
518 }
519
520 static unsigned long st_pinconf_bit_to_delay(unsigned int index,
521         const struct st_pctl_data *data, unsigned long output)
522 {
523         const unsigned int *delay_times;
524         int num_delay_times;
525
526         if (output) {
527                 delay_times = data->output_delays;
528                 num_delay_times = data->noutput_delays;
529         } else {
530                 delay_times = data->input_delays;
531                 num_delay_times = data->ninput_delays;
532         }
533
534         if (index < num_delay_times) {
535                 return delay_times[index];
536         } else {
537                 pr_warn("Delay not found in/out delay list\n");
538                 return 0;
539         }
540 }
541
542 static void st_regmap_field_bit_set_clear_pin(struct regmap_field *field,
543         int enable, int pin)
544 {
545         unsigned int val = 0;
546
547         regmap_field_read(field, &val);
548         if (enable)
549                 val |= BIT(pin);
550         else
551                 val &= ~BIT(pin);
552         regmap_field_write(field, val);
553 }
554
555 static void st_pinconf_set_retime_packed(struct st_pinctrl *info,
556         struct st_pio_control *pc,      unsigned long config, int pin)
557 {
558         const struct st_pctl_data *data = info->data;
559         struct st_retime_packed *rt_p = &pc->rt.rt_p;
560         unsigned int delay;
561
562         st_regmap_field_bit_set_clear_pin(rt_p->clk1notclk0,
563                                 ST_PINCONF_UNPACK_RT_CLK(config), pin);
564
565         st_regmap_field_bit_set_clear_pin(rt_p->clknotdata,
566                                 ST_PINCONF_UNPACK_RT_CLKNOTDATA(config), pin);
567
568         st_regmap_field_bit_set_clear_pin(rt_p->double_edge,
569                                 ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config), pin);
570
571         st_regmap_field_bit_set_clear_pin(rt_p->invertclk,
572                                 ST_PINCONF_UNPACK_RT_INVERTCLK(config), pin);
573
574         st_regmap_field_bit_set_clear_pin(rt_p->retime,
575                                 ST_PINCONF_UNPACK_RT(config), pin);
576
577         delay = st_pinconf_delay_to_bit(ST_PINCONF_UNPACK_RT_DELAY(config),
578                                         data, config);
579         /* 2 bit delay, lsb */
580         st_regmap_field_bit_set_clear_pin(rt_p->delay_0, delay & 0x1, pin);
581         /* 2 bit delay, msb */
582         st_regmap_field_bit_set_clear_pin(rt_p->delay_1, delay & 0x2, pin);
583
584 }
585
586 static void st_pinconf_set_retime_dedicated(struct st_pinctrl *info,
587         struct st_pio_control *pc, unsigned long config, int pin)
588 {
589         int input       = ST_PINCONF_UNPACK_OE(config) ? 0 : 1;
590         int clk         = ST_PINCONF_UNPACK_RT_CLK(config);
591         int clknotdata  = ST_PINCONF_UNPACK_RT_CLKNOTDATA(config);
592         int double_edge = ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config);
593         int invertclk   = ST_PINCONF_UNPACK_RT_INVERTCLK(config);
594         int retime      = ST_PINCONF_UNPACK_RT(config);
595
596         unsigned long delay = st_pinconf_delay_to_bit(
597                         ST_PINCONF_UNPACK_RT_DELAY(config),
598                         info->data, config);
599         struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
600
601         unsigned long retime_config =
602                 ((clk) << RT_D_CFG_CLK_SHIFT) |
603                 ((delay) << RT_D_CFG_DELAY_SHIFT) |
604                 ((input) << RT_D_CFG_DELAY_INNOTOUT_SHIFT) |
605                 ((retime) << RT_D_CFG_RETIME_SHIFT) |
606                 ((clknotdata) << RT_D_CFG_CLKNOTDATA_SHIFT) |
607                 ((invertclk) << RT_D_CFG_INVERTCLK_SHIFT) |
608                 ((double_edge) << RT_D_CFG_DOUBLE_EDGE_SHIFT);
609
610         regmap_field_write(rt_d->rt[pin], retime_config);
611 }
612
613 static void st_pinconf_get_direction(struct st_pio_control *pc,
614         int pin, unsigned long *config)
615 {
616         unsigned int oe_value, pu_value, od_value;
617
618         if (pc->oe) {
619                 regmap_field_read(pc->oe, &oe_value);
620                 if (oe_value & BIT(pin))
621                         ST_PINCONF_PACK_OE(*config);
622         }
623
624         if (pc->pu) {
625                 regmap_field_read(pc->pu, &pu_value);
626                 if (pu_value & BIT(pin))
627                         ST_PINCONF_PACK_PU(*config);
628         }
629
630         if (pc->od) {
631                 regmap_field_read(pc->od, &od_value);
632                 if (od_value & BIT(pin))
633                         ST_PINCONF_PACK_OD(*config);
634         }
635 }
636
637 static int st_pinconf_get_retime_packed(struct st_pinctrl *info,
638         struct st_pio_control *pc,      int pin, unsigned long *config)
639 {
640         const struct st_pctl_data *data = info->data;
641         struct st_retime_packed *rt_p = &pc->rt.rt_p;
642         unsigned int delay_bits, delay, delay0, delay1, val;
643         int output = ST_PINCONF_UNPACK_OE(*config);
644
645         if (!regmap_field_read(rt_p->retime, &val) && (val & BIT(pin)))
646                 ST_PINCONF_PACK_RT(*config);
647
648         if (!regmap_field_read(rt_p->clk1notclk0, &val) && (val & BIT(pin)))
649                 ST_PINCONF_PACK_RT_CLK(*config, 1);
650
651         if (!regmap_field_read(rt_p->clknotdata, &val) && (val & BIT(pin)))
652                 ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
653
654         if (!regmap_field_read(rt_p->double_edge, &val) && (val & BIT(pin)))
655                 ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
656
657         if (!regmap_field_read(rt_p->invertclk, &val) && (val & BIT(pin)))
658                 ST_PINCONF_PACK_RT_INVERTCLK(*config);
659
660         regmap_field_read(rt_p->delay_0, &delay0);
661         regmap_field_read(rt_p->delay_1, &delay1);
662         delay_bits = (((delay1 & BIT(pin)) ? 1 : 0) << 1) |
663                         (((delay0 & BIT(pin)) ? 1 : 0));
664         delay =  st_pinconf_bit_to_delay(delay_bits, data, output);
665         ST_PINCONF_PACK_RT_DELAY(*config, delay);
666
667         return 0;
668 }
669
670 static int st_pinconf_get_retime_dedicated(struct st_pinctrl *info,
671         struct st_pio_control *pc,      int pin, unsigned long *config)
672 {
673         unsigned int value;
674         unsigned long delay_bits, delay, rt_clk;
675         int output = ST_PINCONF_UNPACK_OE(*config);
676         struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
677
678         regmap_field_read(rt_d->rt[pin], &value);
679
680         rt_clk = (value & RT_D_CFG_CLK_MASK) >> RT_D_CFG_CLK_SHIFT;
681         ST_PINCONF_PACK_RT_CLK(*config, rt_clk);
682
683         delay_bits = (value & RT_D_CFG_DELAY_MASK) >> RT_D_CFG_DELAY_SHIFT;
684         delay =  st_pinconf_bit_to_delay(delay_bits, info->data, output);
685         ST_PINCONF_PACK_RT_DELAY(*config, delay);
686
687         if (value & RT_D_CFG_CLKNOTDATA_MASK)
688                 ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
689
690         if (value & RT_D_CFG_DOUBLE_EDGE_MASK)
691                 ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
692
693         if (value & RT_D_CFG_INVERTCLK_MASK)
694                 ST_PINCONF_PACK_RT_INVERTCLK(*config);
695
696         if (value & RT_D_CFG_RETIME_MASK)
697                 ST_PINCONF_PACK_RT(*config);
698
699         return 0;
700 }
701
702 /* GPIO related functions */
703
704 static inline void __st_gpio_set(struct st_gpio_bank *bank,
705         unsigned offset, int value)
706 {
707         if (value)
708                 writel(BIT(offset), bank->base + REG_PIO_SET_POUT);
709         else
710                 writel(BIT(offset), bank->base + REG_PIO_CLR_POUT);
711 }
712
713 static void st_gpio_direction(struct st_gpio_bank *bank,
714                 unsigned int gpio, unsigned int direction)
715 {
716         int offset = st_gpio_pin(gpio);
717         int i = 0;
718         /**
719          * There are three configuration registers (PIOn_PC0, PIOn_PC1
720          * and PIOn_PC2) for each port. These are used to configure the
721          * PIO port pins. Each pin can be configured as an input, output,
722          * bidirectional, or alternative function pin. Three bits, one bit
723          * from each of the three registers, configure the corresponding bit of
724          * the port. Valid bit settings is:
725          *
726          * PC2          PC1             PC0     Direction.
727          * 0            0               0       [Input Weak pull-up]
728          * 0            0 or 1          1       [Bidirection]
729          * 0            1               0       [Output]
730          * 1            0               0       [Input]
731          *
732          * PIOn_SET_PC and PIOn_CLR_PC registers are used to set and clear bits
733          * individually.
734          */
735         for (i = 0; i <= 2; i++) {
736                 if (direction & BIT(i))
737                         writel(BIT(offset), bank->base + REG_PIO_SET_PC(i));
738                 else
739                         writel(BIT(offset), bank->base + REG_PIO_CLR_PC(i));
740         }
741 }
742
743 static int st_gpio_request(struct gpio_chip *chip, unsigned offset)
744 {
745         return pinctrl_request_gpio(chip->base + offset);
746 }
747
748 static void st_gpio_free(struct gpio_chip *chip, unsigned offset)
749 {
750         pinctrl_free_gpio(chip->base + offset);
751 }
752
753 static int st_gpio_get(struct gpio_chip *chip, unsigned offset)
754 {
755         struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
756
757         return !!(readl(bank->base + REG_PIO_PIN) & BIT(offset));
758 }
759
760 static void st_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
761 {
762         struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
763         __st_gpio_set(bank, offset, value);
764 }
765
766 static int st_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
767 {
768         pinctrl_gpio_direction_input(chip->base + offset);
769
770         return 0;
771 }
772
773 static int st_gpio_direction_output(struct gpio_chip *chip,
774         unsigned offset, int value)
775 {
776         struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
777
778         __st_gpio_set(bank, offset, value);
779         pinctrl_gpio_direction_output(chip->base + offset);
780
781         return 0;
782 }
783
784 static int st_gpio_xlate(struct gpio_chip *gc,
785                         const struct of_phandle_args *gpiospec, u32 *flags)
786 {
787         if (WARN_ON(gc->of_gpio_n_cells < 1))
788                 return -EINVAL;
789
790         if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
791                 return -EINVAL;
792
793         if (gpiospec->args[0] > gc->ngpio)
794                 return -EINVAL;
795
796         return gpiospec->args[0];
797 }
798
799 /* Pinctrl Groups */
800 static int st_pctl_get_groups_count(struct pinctrl_dev *pctldev)
801 {
802         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
803
804         return info->ngroups;
805 }
806
807 static const char *st_pctl_get_group_name(struct pinctrl_dev *pctldev,
808                                        unsigned selector)
809 {
810         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
811
812         return info->groups[selector].name;
813 }
814
815 static int st_pctl_get_group_pins(struct pinctrl_dev *pctldev,
816         unsigned selector, const unsigned **pins, unsigned *npins)
817 {
818         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
819
820         if (selector >= info->ngroups)
821                 return -EINVAL;
822
823         *pins = info->groups[selector].pins;
824         *npins = info->groups[selector].npins;
825
826         return 0;
827 }
828
829 static const inline struct st_pctl_group *st_pctl_find_group_by_name(
830         const struct st_pinctrl *info, const char *name)
831 {
832         int i;
833
834         for (i = 0; i < info->ngroups; i++) {
835                 if (!strcmp(info->groups[i].name, name))
836                         return &info->groups[i];
837         }
838
839         return NULL;
840 }
841
842 static int st_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
843         struct device_node *np, struct pinctrl_map **map, unsigned *num_maps)
844 {
845         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
846         const struct st_pctl_group *grp;
847         struct pinctrl_map *new_map;
848         struct device_node *parent;
849         int map_num, i;
850
851         grp = st_pctl_find_group_by_name(info, np->name);
852         if (!grp) {
853                 dev_err(info->dev, "unable to find group for node %s\n",
854                         np->name);
855                 return -EINVAL;
856         }
857
858         map_num = grp->npins + 1;
859         new_map = devm_kzalloc(pctldev->dev,
860                                 sizeof(*new_map) * map_num, GFP_KERNEL);
861         if (!new_map)
862                 return -ENOMEM;
863
864         parent = of_get_parent(np);
865         if (!parent) {
866                 devm_kfree(pctldev->dev, new_map);
867                 return -EINVAL;
868         }
869
870         *map = new_map;
871         *num_maps = map_num;
872         new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
873         new_map[0].data.mux.function = parent->name;
874         new_map[0].data.mux.group = np->name;
875         of_node_put(parent);
876
877         /* create config map per pin */
878         new_map++;
879         for (i = 0; i < grp->npins; i++) {
880                 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
881                 new_map[i].data.configs.group_or_pin =
882                                 pin_get_name(pctldev, grp->pins[i]);
883                 new_map[i].data.configs.configs = &grp->pin_conf[i].config;
884                 new_map[i].data.configs.num_configs = 1;
885         }
886         dev_info(pctldev->dev, "maps: function %s group %s num %d\n",
887                 (*map)->data.mux.function, grp->name, map_num);
888
889         return 0;
890 }
891
892 static void st_pctl_dt_free_map(struct pinctrl_dev *pctldev,
893                         struct pinctrl_map *map, unsigned num_maps)
894 {
895 }
896
897 static struct pinctrl_ops st_pctlops = {
898         .get_groups_count       = st_pctl_get_groups_count,
899         .get_group_pins         = st_pctl_get_group_pins,
900         .get_group_name         = st_pctl_get_group_name,
901         .dt_node_to_map         = st_pctl_dt_node_to_map,
902         .dt_free_map            = st_pctl_dt_free_map,
903 };
904
905 /* Pinmux */
906 static int st_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
907 {
908         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
909
910         return info->nfunctions;
911 }
912
913 static const char *st_pmx_get_fname(struct pinctrl_dev *pctldev,
914         unsigned selector)
915 {
916         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
917
918         return info->functions[selector].name;
919 }
920
921 static int st_pmx_get_groups(struct pinctrl_dev *pctldev,
922         unsigned selector, const char * const **grps, unsigned * const ngrps)
923 {
924         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
925         *grps = info->functions[selector].groups;
926         *ngrps = info->functions[selector].ngroups;
927
928         return 0;
929 }
930
931 static int st_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned fselector,
932                         unsigned group)
933 {
934         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
935         struct st_pinconf *conf = info->groups[group].pin_conf;
936         struct st_pio_control *pc;
937         int i;
938
939         for (i = 0; i < info->groups[group].npins; i++) {
940                 pc = st_get_pio_control(pctldev, conf[i].pin);
941                 st_pctl_set_function(pc, conf[i].pin, conf[i].altfunc);
942         }
943
944         return 0;
945 }
946
947 static int st_pmx_set_gpio_direction(struct pinctrl_dev *pctldev,
948                         struct pinctrl_gpio_range *range, unsigned gpio,
949                         bool input)
950 {
951         struct st_gpio_bank *bank = gpio_range_to_bank(range);
952         /*
953          * When a PIO bank is used in its primary function mode (altfunc = 0)
954          * Output Enable (OE), Open Drain(OD), and Pull Up (PU)
955          * for the primary PIO functions are driven by the related PIO block
956          */
957         st_pctl_set_function(&bank->pc, gpio, 0);
958         st_gpio_direction(bank, gpio, input ?
959                 ST_GPIO_DIRECTION_IN : ST_GPIO_DIRECTION_OUT);
960
961         return 0;
962 }
963
964 static struct pinmux_ops st_pmxops = {
965         .get_functions_count    = st_pmx_get_funcs_count,
966         .get_function_name      = st_pmx_get_fname,
967         .get_function_groups    = st_pmx_get_groups,
968         .set_mux                = st_pmx_set_mux,
969         .gpio_set_direction     = st_pmx_set_gpio_direction,
970 };
971
972 /* Pinconf  */
973 static void st_pinconf_get_retime(struct st_pinctrl *info,
974         struct st_pio_control *pc, int pin, unsigned long *config)
975 {
976         if (info->data->rt_style == st_retime_style_packed)
977                 st_pinconf_get_retime_packed(info, pc, pin, config);
978         else if (info->data->rt_style == st_retime_style_dedicated)
979                 if ((BIT(pin) & pc->rt_pin_mask))
980                         st_pinconf_get_retime_dedicated(info, pc,
981                                         pin, config);
982 }
983
984 static void st_pinconf_set_retime(struct st_pinctrl *info,
985         struct st_pio_control *pc, int pin, unsigned long config)
986 {
987         if (info->data->rt_style == st_retime_style_packed)
988                 st_pinconf_set_retime_packed(info, pc, config, pin);
989         else if (info->data->rt_style == st_retime_style_dedicated)
990                 if ((BIT(pin) & pc->rt_pin_mask))
991                         st_pinconf_set_retime_dedicated(info, pc,
992                                                         config, pin);
993 }
994
995 static int st_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin_id,
996                         unsigned long *configs, unsigned num_configs)
997 {
998         int pin = st_gpio_pin(pin_id);
999         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1000         struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
1001         int i;
1002
1003         for (i = 0; i < num_configs; i++) {
1004                 st_pinconf_set_config(pc, pin, configs[i]);
1005                 st_pinconf_set_retime(info, pc, pin, configs[i]);
1006         } /* for each config */
1007
1008         return 0;
1009 }
1010
1011 static int st_pinconf_get(struct pinctrl_dev *pctldev,
1012                              unsigned pin_id, unsigned long *config)
1013 {
1014         int pin = st_gpio_pin(pin_id);
1015         struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1016         struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
1017
1018         *config = 0;
1019         st_pinconf_get_direction(pc, pin, config);
1020         st_pinconf_get_retime(info, pc, pin, config);
1021
1022         return 0;
1023 }
1024
1025 static void st_pinconf_dbg_show(struct pinctrl_dev *pctldev,
1026                                    struct seq_file *s, unsigned pin_id)
1027 {
1028         unsigned long config;
1029
1030         mutex_unlock(&pctldev->mutex);
1031         st_pinconf_get(pctldev, pin_id, &config);
1032         mutex_lock(&pctldev->mutex);
1033         seq_printf(s, "[OE:%ld,PU:%ld,OD:%ld]\n"
1034                 "\t\t[retime:%ld,invclk:%ld,clknotdat:%ld,"
1035                 "de:%ld,rt-clk:%ld,rt-delay:%ld]",
1036                 ST_PINCONF_UNPACK_OE(config),
1037                 ST_PINCONF_UNPACK_PU(config),
1038                 ST_PINCONF_UNPACK_OD(config),
1039                 ST_PINCONF_UNPACK_RT(config),
1040                 ST_PINCONF_UNPACK_RT_INVERTCLK(config),
1041                 ST_PINCONF_UNPACK_RT_CLKNOTDATA(config),
1042                 ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config),
1043                 ST_PINCONF_UNPACK_RT_CLK(config),
1044                 ST_PINCONF_UNPACK_RT_DELAY(config));
1045 }
1046
1047 static struct pinconf_ops st_confops = {
1048         .pin_config_get         = st_pinconf_get,
1049         .pin_config_set         = st_pinconf_set,
1050         .pin_config_dbg_show    = st_pinconf_dbg_show,
1051 };
1052
1053 static void st_pctl_dt_child_count(struct st_pinctrl *info,
1054                                      struct device_node *np)
1055 {
1056         struct device_node *child;
1057         for_each_child_of_node(np, child) {
1058                 if (of_property_read_bool(child, "gpio-controller")) {
1059                         info->nbanks++;
1060                 } else {
1061                         info->nfunctions++;
1062                         info->ngroups += of_get_child_count(child);
1063                 }
1064         }
1065 }
1066
1067 static int st_pctl_dt_setup_retime_packed(struct st_pinctrl *info,
1068         int bank, struct st_pio_control *pc)
1069 {
1070         struct device *dev = info->dev;
1071         struct regmap *rm = info->regmap;
1072         const struct st_pctl_data *data = info->data;
1073         /* 2 registers per bank */
1074         int reg = (data->rt + bank * RT_P_CFGS_PER_BANK) * 4;
1075         struct st_retime_packed *rt_p = &pc->rt.rt_p;
1076         /* cfg0 */
1077         struct reg_field clk1notclk0 = RT_P_CFG0_CLK1NOTCLK0_FIELD(reg);
1078         struct reg_field delay_0 = RT_P_CFG0_DELAY_0_FIELD(reg);
1079         struct reg_field delay_1 = RT_P_CFG0_DELAY_1_FIELD(reg);
1080         /* cfg1 */
1081         struct reg_field invertclk = RT_P_CFG1_INVERTCLK_FIELD(reg + 4);
1082         struct reg_field retime = RT_P_CFG1_RETIME_FIELD(reg + 4);
1083         struct reg_field clknotdata = RT_P_CFG1_CLKNOTDATA_FIELD(reg + 4);
1084         struct reg_field double_edge = RT_P_CFG1_DOUBLE_EDGE_FIELD(reg + 4);
1085
1086         rt_p->clk1notclk0 = devm_regmap_field_alloc(dev, rm, clk1notclk0);
1087         rt_p->delay_0   = devm_regmap_field_alloc(dev, rm, delay_0);
1088         rt_p->delay_1 = devm_regmap_field_alloc(dev, rm, delay_1);
1089         rt_p->invertclk = devm_regmap_field_alloc(dev, rm, invertclk);
1090         rt_p->retime = devm_regmap_field_alloc(dev, rm, retime);
1091         rt_p->clknotdata = devm_regmap_field_alloc(dev, rm, clknotdata);
1092         rt_p->double_edge = devm_regmap_field_alloc(dev, rm, double_edge);
1093
1094         if (IS_ERR(rt_p->clk1notclk0) || IS_ERR(rt_p->delay_0) ||
1095                  IS_ERR(rt_p->delay_1) || IS_ERR(rt_p->invertclk) ||
1096                  IS_ERR(rt_p->retime) || IS_ERR(rt_p->clknotdata) ||
1097                  IS_ERR(rt_p->double_edge))
1098                 return -EINVAL;
1099
1100         return 0;
1101 }
1102
1103 static int st_pctl_dt_setup_retime_dedicated(struct st_pinctrl *info,
1104         int bank, struct st_pio_control *pc)
1105 {
1106         struct device *dev = info->dev;
1107         struct regmap *rm = info->regmap;
1108         const struct st_pctl_data *data = info->data;
1109         /* 8 registers per bank */
1110         int reg_offset = (data->rt + bank * RT_D_CFGS_PER_BANK) * 4;
1111         struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
1112         unsigned int j;
1113         u32 pin_mask = pc->rt_pin_mask;
1114
1115         for (j = 0; j < RT_D_CFGS_PER_BANK; j++) {
1116                 if (BIT(j) & pin_mask) {
1117                         struct reg_field reg = REG_FIELD(reg_offset, 0, 31);
1118                         rt_d->rt[j] = devm_regmap_field_alloc(dev, rm, reg);
1119                         if (IS_ERR(rt_d->rt[j]))
1120                                 return -EINVAL;
1121                         reg_offset += 4;
1122                 }
1123         }
1124         return 0;
1125 }
1126
1127 static int st_pctl_dt_setup_retime(struct st_pinctrl *info,
1128         int bank, struct st_pio_control *pc)
1129 {
1130         const struct st_pctl_data *data = info->data;
1131         if (data->rt_style  == st_retime_style_packed)
1132                 return st_pctl_dt_setup_retime_packed(info, bank, pc);
1133         else if (data->rt_style == st_retime_style_dedicated)
1134                 return st_pctl_dt_setup_retime_dedicated(info, bank, pc);
1135
1136         return -EINVAL;
1137 }
1138
1139
1140 static struct regmap_field *st_pc_get_value(struct device *dev,
1141                                             struct regmap *regmap, int bank,
1142                                             int data, int lsb, int msb)
1143 {
1144         struct reg_field reg = REG_FIELD((data + bank) * 4, lsb, msb);
1145
1146         if (data < 0)
1147                 return NULL;
1148
1149         return devm_regmap_field_alloc(dev, regmap, reg);
1150 }
1151
1152 static void st_parse_syscfgs(struct st_pinctrl *info, int bank,
1153                              struct device_node *np)
1154 {
1155         const struct st_pctl_data *data = info->data;
1156         /**
1157          * For a given shared register like OE/PU/OD, there are 8 bits per bank
1158          * 0:7 belongs to bank0, 8:15 belongs to bank1 ...
1159          * So each register is shared across 4 banks.
1160          */
1161         int lsb = (bank%4) * ST_GPIO_PINS_PER_BANK;
1162         int msb = lsb + ST_GPIO_PINS_PER_BANK - 1;
1163         struct st_pio_control *pc = &info->banks[bank].pc;
1164         struct device *dev = info->dev;
1165         struct regmap *regmap  = info->regmap;
1166
1167         pc->alt = st_pc_get_value(dev, regmap, bank, data->alt, 0, 31);
1168         pc->oe = st_pc_get_value(dev, regmap, bank/4, data->oe, lsb, msb);
1169         pc->pu = st_pc_get_value(dev, regmap, bank/4, data->pu, lsb, msb);
1170         pc->od = st_pc_get_value(dev, regmap, bank/4, data->od, lsb, msb);
1171
1172         /* retime avaiable for all pins by default */
1173         pc->rt_pin_mask = 0xff;
1174         of_property_read_u32(np, "st,retime-pin-mask", &pc->rt_pin_mask);
1175         st_pctl_dt_setup_retime(info, bank, pc);
1176
1177         return;
1178 }
1179
1180 /*
1181  * Each pin is represented in of the below forms.
1182  * <bank offset mux direction rt_type rt_delay rt_clk>
1183  */
1184 static int st_pctl_dt_parse_groups(struct device_node *np,
1185         struct st_pctl_group *grp, struct st_pinctrl *info, int idx)
1186 {
1187         /* bank pad direction val altfunction */
1188         const __be32 *list;
1189         struct property *pp;
1190         struct st_pinconf *conf;
1191         struct device_node *pins;
1192         int i = 0, npins = 0, nr_props;
1193
1194         pins = of_get_child_by_name(np, "st,pins");
1195         if (!pins)
1196                 return -ENODATA;
1197
1198         for_each_property_of_node(pins, pp) {
1199                 /* Skip those we do not want to proceed */
1200                 if (!strcmp(pp->name, "name"))
1201                         continue;
1202
1203                 if (pp  && (pp->length/sizeof(__be32)) >= OF_GPIO_ARGS_MIN) {
1204                         npins++;
1205                 } else {
1206                         pr_warn("Invalid st,pins in %s node\n", np->name);
1207                         return -EINVAL;
1208                 }
1209         }
1210
1211         grp->npins = npins;
1212         grp->name = np->name;
1213         grp->pins = devm_kzalloc(info->dev, npins * sizeof(u32), GFP_KERNEL);
1214         grp->pin_conf = devm_kzalloc(info->dev,
1215                                         npins * sizeof(*conf), GFP_KERNEL);
1216
1217         if (!grp->pins || !grp->pin_conf)
1218                 return -ENOMEM;
1219
1220         /* <bank offset mux direction rt_type rt_delay rt_clk> */
1221         for_each_property_of_node(pins, pp) {
1222                 if (!strcmp(pp->name, "name"))
1223                         continue;
1224                 nr_props = pp->length/sizeof(u32);
1225                 list = pp->value;
1226                 conf = &grp->pin_conf[i];
1227
1228                 /* bank & offset */
1229                 be32_to_cpup(list++);
1230                 be32_to_cpup(list++);
1231                 conf->pin = of_get_named_gpio(pins, pp->name, 0);
1232                 conf->name = pp->name;
1233                 grp->pins[i] = conf->pin;
1234                 /* mux */
1235                 conf->altfunc = be32_to_cpup(list++);
1236                 conf->config = 0;
1237                 /* direction */
1238                 conf->config |= be32_to_cpup(list++);
1239                 /* rt_type rt_delay rt_clk */
1240                 if (nr_props >= OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN) {
1241                         /* rt_type */
1242                         conf->config |= be32_to_cpup(list++);
1243                         /* rt_delay */
1244                         conf->config |= be32_to_cpup(list++);
1245                         /* rt_clk */
1246                         if (nr_props > OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN)
1247                                 conf->config |= be32_to_cpup(list++);
1248                 }
1249                 i++;
1250         }
1251         of_node_put(pins);
1252
1253         return 0;
1254 }
1255
1256 static int st_pctl_parse_functions(struct device_node *np,
1257                         struct st_pinctrl *info, u32 index, int *grp_index)
1258 {
1259         struct device_node *child;
1260         struct st_pmx_func *func;
1261         struct st_pctl_group *grp;
1262         int ret, i;
1263
1264         func = &info->functions[index];
1265         func->name = np->name;
1266         func->ngroups = of_get_child_count(np);
1267         if (func->ngroups == 0) {
1268                 dev_err(info->dev, "No groups defined\n");
1269                 return -EINVAL;
1270         }
1271         func->groups = devm_kzalloc(info->dev,
1272                         func->ngroups * sizeof(char *), GFP_KERNEL);
1273         if (!func->groups)
1274                 return -ENOMEM;
1275
1276         i = 0;
1277         for_each_child_of_node(np, child) {
1278                 func->groups[i] = child->name;
1279                 grp = &info->groups[*grp_index];
1280                 *grp_index += 1;
1281                 ret = st_pctl_dt_parse_groups(child, grp, info, i++);
1282                 if (ret)
1283                         return ret;
1284         }
1285         dev_info(info->dev, "Function[%d\t name:%s,\tgroups:%d]\n",
1286                                 index, func->name, func->ngroups);
1287
1288         return 0;
1289 }
1290
1291 static void st_gpio_irq_mask(struct irq_data *d)
1292 {
1293         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1294         struct st_gpio_bank *bank = gpio_chip_to_bank(gc);
1295
1296         writel(BIT(d->hwirq), bank->base + REG_PIO_CLR_PMASK);
1297 }
1298
1299 static void st_gpio_irq_unmask(struct irq_data *d)
1300 {
1301         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1302         struct st_gpio_bank *bank = gpio_chip_to_bank(gc);
1303
1304         writel(BIT(d->hwirq), bank->base + REG_PIO_SET_PMASK);
1305 }
1306
1307 static int st_gpio_irq_set_type(struct irq_data *d, unsigned type)
1308 {
1309         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1310         struct st_gpio_bank *bank = gpio_chip_to_bank(gc);
1311         unsigned long flags;
1312         int comp, pin = d->hwirq;
1313         u32 val;
1314         u32 pin_edge_conf = 0;
1315
1316         switch (type) {
1317         case IRQ_TYPE_LEVEL_HIGH:
1318                 comp = 0;
1319                 break;
1320         case IRQ_TYPE_EDGE_FALLING:
1321                 comp = 0;
1322                 pin_edge_conf = ST_IRQ_FALLING_EDGE_CONF(pin);
1323                 break;
1324         case IRQ_TYPE_LEVEL_LOW:
1325                 comp = 1;
1326                 break;
1327         case IRQ_TYPE_EDGE_RISING:
1328                 comp = 1;
1329                 pin_edge_conf = ST_IRQ_RISING_EDGE_CONF(pin);
1330                 break;
1331         case IRQ_TYPE_EDGE_BOTH:
1332                 comp = st_gpio_get(&bank->gpio_chip, pin);
1333                 pin_edge_conf = ST_IRQ_BOTH_EDGE_CONF(pin);
1334                 break;
1335         default:
1336                 return -EINVAL;
1337         }
1338
1339         spin_lock_irqsave(&bank->lock, flags);
1340         bank->irq_edge_conf &=  ~(ST_IRQ_EDGE_MASK << (
1341                                 pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN));
1342         bank->irq_edge_conf |= pin_edge_conf;
1343         spin_unlock_irqrestore(&bank->lock, flags);
1344
1345         val = readl(bank->base + REG_PIO_PCOMP);
1346         val &= ~BIT(pin);
1347         val |= (comp << pin);
1348         writel(val, bank->base + REG_PIO_PCOMP);
1349
1350         return 0;
1351 }
1352
1353 /*
1354  * As edge triggers are not supported at hardware level, it is supported by
1355  * software by exploiting the level trigger support in hardware.
1356  *
1357  * Steps for detection raising edge interrupt in software.
1358  *
1359  * Step 1: CONFIGURE pin to detect level LOW interrupts.
1360  *
1361  * Step 2: DETECT level LOW interrupt and in irqmux/gpio bank interrupt handler,
1362  * if the value of pin is low, then CONFIGURE pin for level HIGH interrupt.
1363  * IGNORE calling the actual interrupt handler for the pin at this stage.
1364  *
1365  * Step 3: DETECT level HIGH interrupt and in irqmux/gpio-bank interrupt handler
1366  * if the value of pin is HIGH, CONFIGURE pin for level LOW interrupt and then
1367  * DISPATCH the interrupt to the interrupt handler of the pin.
1368  *
1369  *               step-1  ________     __________
1370  *                              |     | step - 3
1371  *                              |     |
1372  *                      step -2 |_____|
1373  *
1374  * falling edge is also detected int the same way.
1375  *
1376  */
1377 static void __gpio_irq_handler(struct st_gpio_bank *bank)
1378 {
1379         unsigned long port_in, port_mask, port_comp, active_irqs;
1380         unsigned long bank_edge_mask, flags;
1381         int n, val, ecfg;
1382
1383         spin_lock_irqsave(&bank->lock, flags);
1384         bank_edge_mask = bank->irq_edge_conf;
1385         spin_unlock_irqrestore(&bank->lock, flags);
1386
1387         for (;;) {
1388                 port_in = readl(bank->base + REG_PIO_PIN);
1389                 port_comp = readl(bank->base + REG_PIO_PCOMP);
1390                 port_mask = readl(bank->base + REG_PIO_PMASK);
1391
1392                 active_irqs = (port_in ^ port_comp) & port_mask;
1393
1394                 if (active_irqs == 0)
1395                         break;
1396
1397                 for_each_set_bit(n, &active_irqs, BITS_PER_LONG) {
1398                         /* check if we are detecting fake edges ... */
1399                         ecfg = ST_IRQ_EDGE_CONF(bank_edge_mask, n);
1400
1401                         if (ecfg) {
1402                                 /* edge detection. */
1403                                 val = st_gpio_get(&bank->gpio_chip, n);
1404
1405                                 writel(BIT(n),
1406                                         val ? bank->base + REG_PIO_SET_PCOMP :
1407                                         bank->base + REG_PIO_CLR_PCOMP);
1408
1409                                 if (ecfg != ST_IRQ_EDGE_BOTH &&
1410                                         !((ecfg & ST_IRQ_EDGE_FALLING) ^ val))
1411                                         continue;
1412                         }
1413
1414                         generic_handle_irq(irq_find_mapping(bank->gpio_chip.irqdomain, n));
1415                 }
1416         }
1417 }
1418
1419 static void st_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
1420 {
1421         /* interrupt dedicated per bank */
1422         struct irq_chip *chip = irq_get_chip(irq);
1423         struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1424         struct st_gpio_bank *bank = gpio_chip_to_bank(gc);
1425
1426         chained_irq_enter(chip, desc);
1427         __gpio_irq_handler(bank);
1428         chained_irq_exit(chip, desc);
1429 }
1430
1431 static void st_gpio_irqmux_handler(unsigned irq, struct irq_desc *desc)
1432 {
1433         struct irq_chip *chip = irq_get_chip(irq);
1434         struct st_pinctrl *info = irq_get_handler_data(irq);
1435         unsigned long status;
1436         int n;
1437
1438         chained_irq_enter(chip, desc);
1439
1440         status = readl(info->irqmux_base);
1441
1442         for_each_set_bit(n, &status, info->nbanks)
1443                 __gpio_irq_handler(&info->banks[n]);
1444
1445         chained_irq_exit(chip, desc);
1446 }
1447
1448 static struct gpio_chip st_gpio_template = {
1449         .request                = st_gpio_request,
1450         .free                   = st_gpio_free,
1451         .get                    = st_gpio_get,
1452         .set                    = st_gpio_set,
1453         .direction_input        = st_gpio_direction_input,
1454         .direction_output       = st_gpio_direction_output,
1455         .ngpio                  = ST_GPIO_PINS_PER_BANK,
1456         .of_gpio_n_cells        = 1,
1457         .of_xlate               = st_gpio_xlate,
1458 };
1459
1460 static struct irq_chip st_gpio_irqchip = {
1461         .name           = "GPIO",
1462         .irq_disable    = st_gpio_irq_mask,
1463         .irq_mask       = st_gpio_irq_mask,
1464         .irq_unmask     = st_gpio_irq_unmask,
1465         .irq_set_type   = st_gpio_irq_set_type,
1466         .flags          = IRQCHIP_SKIP_SET_WAKE,
1467 };
1468
1469 static int st_gpiolib_register_bank(struct st_pinctrl *info,
1470         int bank_nr, struct device_node *np)
1471 {
1472         struct st_gpio_bank *bank = &info->banks[bank_nr];
1473         struct pinctrl_gpio_range *range = &bank->range;
1474         struct device *dev = info->dev;
1475         int bank_num = of_alias_get_id(np, "gpio");
1476         struct resource res, irq_res;
1477         int gpio_irq = 0, err;
1478
1479         if (of_address_to_resource(np, 0, &res))
1480                 return -ENODEV;
1481
1482         bank->base = devm_ioremap_resource(dev, &res);
1483         if (IS_ERR(bank->base))
1484                 return PTR_ERR(bank->base);
1485
1486         bank->gpio_chip = st_gpio_template;
1487         bank->gpio_chip.base = bank_num * ST_GPIO_PINS_PER_BANK;
1488         bank->gpio_chip.ngpio = ST_GPIO_PINS_PER_BANK;
1489         bank->gpio_chip.of_node = np;
1490         bank->gpio_chip.dev = dev;
1491         spin_lock_init(&bank->lock);
1492
1493         of_property_read_string(np, "st,bank-name", &range->name);
1494         bank->gpio_chip.label = range->name;
1495
1496         range->id = bank_num;
1497         range->pin_base = range->base = range->id * ST_GPIO_PINS_PER_BANK;
1498         range->npins = bank->gpio_chip.ngpio;
1499         range->gc = &bank->gpio_chip;
1500         err  = gpiochip_add(&bank->gpio_chip);
1501         if (err) {
1502                 dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_num);
1503                 return err;
1504         }
1505         dev_info(dev, "%s bank added.\n", range->name);
1506
1507         /**
1508          * GPIO bank can have one of the two possible types of
1509          * interrupt-wirings.
1510          *
1511          * First type is via irqmux, single interrupt is used by multiple
1512          * gpio banks. This reduces number of overall interrupts numbers
1513          * required. All these banks belong to a single pincontroller.
1514          *                _________
1515          *               |         |----> [gpio-bank (n)    ]
1516          *               |         |----> [gpio-bank (n + 1)]
1517          *      [irqN]-- | irq-mux |----> [gpio-bank (n + 2)]
1518          *               |         |----> [gpio-bank (...  )]
1519          *               |_________|----> [gpio-bank (n + 7)]
1520          *
1521          * Second type has a dedicated interrupt per each gpio bank.
1522          *
1523          *      [irqN]----> [gpio-bank (n)]
1524          */
1525
1526         if (of_irq_to_resource(np, 0, &irq_res)) {
1527                 gpio_irq = irq_res.start;
1528                 gpiochip_set_chained_irqchip(&bank->gpio_chip, &st_gpio_irqchip,
1529                                              gpio_irq, st_gpio_irq_handler);
1530         }
1531
1532         if (info->irqmux_base || gpio_irq > 0) {
1533                 err = gpiochip_irqchip_add(&bank->gpio_chip, &st_gpio_irqchip,
1534                                            0, handle_simple_irq,
1535                                            IRQ_TYPE_LEVEL_LOW);
1536                 if (err) {
1537                         gpiochip_remove(&bank->gpio_chip);
1538                         dev_info(dev, "could not add irqchip\n");
1539                         return err;
1540                 }
1541         } else {
1542                 dev_info(dev, "No IRQ support for %s bank\n", np->full_name);
1543         }
1544
1545         return 0;
1546 }
1547
1548 static struct of_device_id st_pctl_of_match[] = {
1549         { .compatible = "st,stih415-sbc-pinctrl", .data = &stih415_sbc_data },
1550         { .compatible = "st,stih415-rear-pinctrl", .data = &stih415_rear_data },
1551         { .compatible = "st,stih415-left-pinctrl", .data = &stih415_left_data },
1552         { .compatible = "st,stih415-right-pinctrl",
1553                 .data = &stih415_right_data },
1554         { .compatible = "st,stih415-front-pinctrl",
1555                 .data = &stih415_front_data },
1556         { .compatible = "st,stih416-sbc-pinctrl", .data = &stih416_data},
1557         { .compatible = "st,stih416-front-pinctrl", .data = &stih416_data},
1558         { .compatible = "st,stih416-rear-pinctrl", .data = &stih416_data},
1559         { .compatible = "st,stih416-fvdp-fe-pinctrl", .data = &stih416_data},
1560         { .compatible = "st,stih416-fvdp-lite-pinctrl", .data = &stih416_data},
1561         { .compatible = "st,stih407-sbc-pinctrl", .data = &stih416_data},
1562         { .compatible = "st,stih407-front-pinctrl", .data = &stih416_data},
1563         { .compatible = "st,stih407-rear-pinctrl", .data = &stih416_data},
1564         { .compatible = "st,stih407-flash-pinctrl", .data = &stih407_flashdata},
1565         { /* sentinel */ }
1566 };
1567
1568 static int st_pctl_probe_dt(struct platform_device *pdev,
1569         struct pinctrl_desc *pctl_desc, struct st_pinctrl *info)
1570 {
1571         int ret = 0;
1572         int i = 0, j = 0, k = 0, bank;
1573         struct pinctrl_pin_desc *pdesc;
1574         struct device_node *np = pdev->dev.of_node;
1575         struct device_node *child;
1576         int grp_index = 0;
1577         int irq = 0;
1578         struct resource *res;
1579
1580         st_pctl_dt_child_count(info, np);
1581         if (!info->nbanks) {
1582                 dev_err(&pdev->dev, "you need atleast one gpio bank\n");
1583                 return -EINVAL;
1584         }
1585
1586         dev_info(&pdev->dev, "nbanks = %d\n", info->nbanks);
1587         dev_info(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1588         dev_info(&pdev->dev, "ngroups = %d\n", info->ngroups);
1589
1590         info->functions = devm_kzalloc(&pdev->dev,
1591                 info->nfunctions * sizeof(*info->functions), GFP_KERNEL);
1592
1593         info->groups = devm_kzalloc(&pdev->dev,
1594                         info->ngroups * sizeof(*info->groups) , GFP_KERNEL);
1595
1596         info->banks = devm_kzalloc(&pdev->dev,
1597                         info->nbanks * sizeof(*info->banks), GFP_KERNEL);
1598
1599         if (!info->functions || !info->groups || !info->banks)
1600                 return -ENOMEM;
1601
1602         info->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1603         if (IS_ERR(info->regmap)) {
1604                 dev_err(info->dev, "No syscfg phandle specified\n");
1605                 return PTR_ERR(info->regmap);
1606         }
1607         info->data = of_match_node(st_pctl_of_match, np)->data;
1608
1609         irq = platform_get_irq(pdev, 0);
1610
1611         if (irq > 0) {
1612                 res = platform_get_resource_byname(pdev,
1613                                         IORESOURCE_MEM, "irqmux");
1614                 info->irqmux_base = devm_ioremap_resource(&pdev->dev, res);
1615
1616                 if (IS_ERR(info->irqmux_base))
1617                         return PTR_ERR(info->irqmux_base);
1618
1619                 irq_set_chained_handler(irq, st_gpio_irqmux_handler);
1620                 irq_set_handler_data(irq, info);
1621
1622         }
1623
1624         pctl_desc->npins = info->nbanks * ST_GPIO_PINS_PER_BANK;
1625         pdesc = devm_kzalloc(&pdev->dev,
1626                         sizeof(*pdesc) * pctl_desc->npins, GFP_KERNEL);
1627         if (!pdesc)
1628                 return -ENOMEM;
1629
1630         pctl_desc->pins = pdesc;
1631
1632         bank = 0;
1633         for_each_child_of_node(np, child) {
1634                 if (of_property_read_bool(child, "gpio-controller")) {
1635                         const char *bank_name = NULL;
1636                         ret = st_gpiolib_register_bank(info, bank, child);
1637                         if (ret)
1638                                 return ret;
1639
1640                         k = info->banks[bank].range.pin_base;
1641                         bank_name = info->banks[bank].range.name;
1642                         for (j = 0; j < ST_GPIO_PINS_PER_BANK; j++, k++) {
1643                                 pdesc->number = k;
1644                                 pdesc->name = kasprintf(GFP_KERNEL, "%s[%d]",
1645                                                         bank_name, j);
1646                                 pdesc++;
1647                         }
1648                         st_parse_syscfgs(info, bank, child);
1649                         bank++;
1650                 } else {
1651                         ret = st_pctl_parse_functions(child, info,
1652                                                         i++, &grp_index);
1653                         if (ret) {
1654                                 dev_err(&pdev->dev, "No functions found.\n");
1655                                 return ret;
1656                         }
1657                 }
1658         }
1659
1660         return 0;
1661 }
1662
1663 static int st_pctl_probe(struct platform_device *pdev)
1664 {
1665         struct st_pinctrl *info;
1666         struct pinctrl_desc *pctl_desc;
1667         int ret, i;
1668
1669         if (!pdev->dev.of_node) {
1670                 dev_err(&pdev->dev, "device node not found.\n");
1671                 return -EINVAL;
1672         }
1673
1674         pctl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctl_desc), GFP_KERNEL);
1675         if (!pctl_desc)
1676                 return -ENOMEM;
1677
1678         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1679         if (!info)
1680                 return -ENOMEM;
1681
1682         info->dev = &pdev->dev;
1683         platform_set_drvdata(pdev, info);
1684         ret = st_pctl_probe_dt(pdev, pctl_desc, info);
1685         if (ret)
1686                 return ret;
1687
1688         pctl_desc->owner        = THIS_MODULE;
1689         pctl_desc->pctlops      = &st_pctlops;
1690         pctl_desc->pmxops       = &st_pmxops;
1691         pctl_desc->confops      = &st_confops;
1692         pctl_desc->name         = dev_name(&pdev->dev);
1693
1694         info->pctl = pinctrl_register(pctl_desc, &pdev->dev, info);
1695         if (!info->pctl) {
1696                 dev_err(&pdev->dev, "Failed pinctrl registration\n");
1697                 return -EINVAL;
1698         }
1699
1700         for (i = 0; i < info->nbanks; i++)
1701                 pinctrl_add_gpio_range(info->pctl, &info->banks[i].range);
1702
1703         return 0;
1704 }
1705
1706 static struct platform_driver st_pctl_driver = {
1707         .driver = {
1708                 .name = "st-pinctrl",
1709                 .of_match_table = st_pctl_of_match,
1710         },
1711         .probe = st_pctl_probe,
1712 };
1713
1714 static int __init st_pctl_init(void)
1715 {
1716         return platform_driver_register(&st_pctl_driver);
1717 }
1718 arch_initcall(st_pctl_init);