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