blob: 52a437738cc20f578580ff37dfc93acfc3e5e2dd [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>
16#include <linux/of.h>
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +000017#include <linux/of_irq.h>
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +010018#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
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100209enum st_retime_style {
210 st_retime_style_none,
211 st_retime_style_packed,
212 st_retime_style_dedicated,
213};
214
215struct st_retime_dedicated {
216 struct regmap_field *rt[ST_GPIO_PINS_PER_BANK];
217};
218
219struct st_retime_packed {
220 struct regmap_field *clk1notclk0;
221 struct regmap_field *delay_0;
222 struct regmap_field *delay_1;
223 struct regmap_field *invertclk;
224 struct regmap_field *retime;
225 struct regmap_field *clknotdata;
226 struct regmap_field *double_edge;
227};
228
229struct st_pio_control {
230 u32 rt_pin_mask;
231 struct regmap_field *alt, *oe, *pu, *od;
232 /* retiming */
233 union {
234 struct st_retime_packed rt_p;
235 struct st_retime_dedicated rt_d;
236 } rt;
237};
238
239struct st_pctl_data {
Maxime COQUELINa4bc1f52014-04-08 17:21:48 +0200240 const enum st_retime_style rt_style;
241 const unsigned int *input_delays;
242 const int ninput_delays;
243 const unsigned int *output_delays;
244 const int noutput_delays;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100245 /* register offset information */
Maxime COQUELINa4bc1f52014-04-08 17:21:48 +0200246 const int alt, oe, pu, od, rt;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100247};
248
249struct st_pinconf {
250 int pin;
251 const char *name;
252 unsigned long config;
253 int altfunc;
254};
255
256struct st_pmx_func {
257 const char *name;
258 const char **groups;
259 unsigned ngroups;
260};
261
262struct st_pctl_group {
263 const char *name;
264 unsigned int *pins;
265 unsigned npins;
266 struct st_pinconf *pin_conf;
267};
268
Srinivas Kandagatla155795b2014-01-16 15:37:31 +0000269/*
270 * Edge triggers are not supported at hardware level, it is supported by
271 * software by exploiting the level trigger support in hardware.
272 * Software uses a virtual register (EDGE_CONF) for edge trigger configuration
273 * of each gpio pin in a GPIO bank.
274 *
275 * Each bank has a 32 bit EDGE_CONF register which is divided in to 8 parts of
276 * 4-bits. Each 4-bit space is allocated for each pin in a gpio bank.
277 *
278 * bit allocation per pin is:
279 * Bits: [0 - 3] | [4 - 7] [8 - 11] ... ... ... ... [ 28 - 31]
280 * --------------------------------------------------------
281 * | pin-0 | pin-2 | pin-3 | ... ... ... ... | pin -7 |
282 * --------------------------------------------------------
283 *
284 * A pin can have one of following the values in its edge configuration field.
285 *
286 * ------- ----------------------------
287 * [0-3] - Description
288 * ------- ----------------------------
289 * 0000 - No edge IRQ.
290 * 0001 - Falling edge IRQ.
291 * 0010 - Rising edge IRQ.
292 * 0011 - Rising and Falling edge IRQ.
293 * ------- ----------------------------
294 */
295
296#define ST_IRQ_EDGE_CONF_BITS_PER_PIN 4
297#define ST_IRQ_EDGE_MASK 0xf
298#define ST_IRQ_EDGE_FALLING BIT(0)
299#define ST_IRQ_EDGE_RISING BIT(1)
300#define ST_IRQ_EDGE_BOTH (BIT(0) | BIT(1))
301
302#define ST_IRQ_RISING_EDGE_CONF(pin) \
303 (ST_IRQ_EDGE_RISING << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
304
305#define ST_IRQ_FALLING_EDGE_CONF(pin) \
306 (ST_IRQ_EDGE_FALLING << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
307
308#define ST_IRQ_BOTH_EDGE_CONF(pin) \
309 (ST_IRQ_EDGE_BOTH << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
310
311#define ST_IRQ_EDGE_CONF(conf, pin) \
312 (conf >> (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN) & ST_IRQ_EDGE_MASK)
313
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100314struct st_gpio_bank {
315 struct gpio_chip gpio_chip;
316 struct pinctrl_gpio_range range;
317 void __iomem *base;
318 struct st_pio_control pc;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +0000319 unsigned long irq_edge_conf;
320 spinlock_t lock;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100321};
322
323struct st_pinctrl {
324 struct device *dev;
325 struct pinctrl_dev *pctl;
326 struct st_gpio_bank *banks;
327 int nbanks;
328 struct st_pmx_func *functions;
329 int nfunctions;
330 struct st_pctl_group *groups;
331 int ngroups;
332 struct regmap *regmap;
333 const struct st_pctl_data *data;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +0000334 void __iomem *irqmux_base;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100335};
336
337/* SOC specific data */
338/* STiH415 data */
Maxime COQUELINa4bc1f52014-04-08 17:21:48 +0200339static const unsigned int stih415_input_delays[] = {0, 500, 1000, 1500};
340static const unsigned int stih415_output_delays[] = {0, 1000, 2000, 3000};
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100341
342#define STIH415_PCTRL_COMMON_DATA \
343 .rt_style = st_retime_style_packed, \
344 .input_delays = stih415_input_delays, \
Maxime COQUELIN3b02dad2014-04-08 17:21:49 +0200345 .ninput_delays = ARRAY_SIZE(stih415_input_delays), \
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100346 .output_delays = stih415_output_delays, \
Maxime COQUELIN3b02dad2014-04-08 17:21:49 +0200347 .noutput_delays = ARRAY_SIZE(stih415_output_delays)
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100348
349static const struct st_pctl_data stih415_sbc_data = {
350 STIH415_PCTRL_COMMON_DATA,
351 .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 16,
352};
353
354static const struct st_pctl_data stih415_front_data = {
355 STIH415_PCTRL_COMMON_DATA,
356 .alt = 0, .oe = 8, .pu = 10, .od = 12, .rt = 16,
357};
358
359static const struct st_pctl_data stih415_rear_data = {
360 STIH415_PCTRL_COMMON_DATA,
361 .alt = 0, .oe = 6, .pu = 8, .od = 10, .rt = 38,
362};
363
364static const struct st_pctl_data stih415_left_data = {
365 STIH415_PCTRL_COMMON_DATA,
366 .alt = 0, .oe = 3, .pu = 4, .od = 5, .rt = 6,
367};
368
369static const struct st_pctl_data stih415_right_data = {
370 STIH415_PCTRL_COMMON_DATA,
371 .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 11,
372};
373
374/* STiH416 data */
Maxime COQUELINa4bc1f52014-04-08 17:21:48 +0200375static const unsigned int stih416_delays[] = {0, 300, 500, 750, 1000, 1250,
376 1500, 1750, 2000, 2250, 2500, 2750, 3000, 3250 };
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100377
378static const struct st_pctl_data stih416_data = {
379 .rt_style = st_retime_style_dedicated,
380 .input_delays = stih416_delays,
Maxime COQUELIN88430ac2014-03-12 09:50:08 +0100381 .ninput_delays = ARRAY_SIZE(stih416_delays),
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100382 .output_delays = stih416_delays,
Maxime COQUELIN88430ac2014-03-12 09:50:08 +0100383 .noutput_delays = ARRAY_SIZE(stih416_delays),
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100384 .alt = 0, .oe = 40, .pu = 50, .od = 60, .rt = 100,
385};
386
Giuseppe Cavallaro7ce717d2014-03-12 09:50:07 +0100387static const struct st_pctl_data stih407_flashdata = {
388 .rt_style = st_retime_style_none,
389 .input_delays = stih416_delays,
390 .ninput_delays = ARRAY_SIZE(stih416_delays),
391 .output_delays = stih416_delays,
392 .noutput_delays = ARRAY_SIZE(stih416_delays),
393 .alt = 0,
394 .oe = -1, /* Not Available */
395 .pu = -1, /* Not Available */
396 .od = 60,
397 .rt = 100,
398};
399
Lee Jonesf89e68f2015-03-18 17:21:16 +0000400static struct st_pio_control *st_get_pio_control(
401 struct pinctrl_dev *pctldev, int pin)
402{
403 struct pinctrl_gpio_range *range =
404 pinctrl_find_gpio_range_from_pin(pctldev, pin);
405 struct st_gpio_bank *bank = gpio_range_to_bank(range);
406
407 return &bank->pc;
408}
409
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100410/* Low level functions.. */
411static inline int st_gpio_bank(int gpio)
412{
413 return gpio/ST_GPIO_PINS_PER_BANK;
414}
415
416static inline int st_gpio_pin(int gpio)
417{
418 return gpio%ST_GPIO_PINS_PER_BANK;
419}
420
421static void st_pinconf_set_config(struct st_pio_control *pc,
422 int pin, unsigned long config)
423{
424 struct regmap_field *output_enable = pc->oe;
425 struct regmap_field *pull_up = pc->pu;
426 struct regmap_field *open_drain = pc->od;
427 unsigned int oe_value, pu_value, od_value;
428 unsigned long mask = BIT(pin);
429
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +0100430 if (output_enable) {
431 regmap_field_read(output_enable, &oe_value);
432 oe_value &= ~mask;
433 if (config & ST_PINCONF_OE)
434 oe_value |= mask;
435 regmap_field_write(output_enable, oe_value);
436 }
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100437
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +0100438 if (pull_up) {
439 regmap_field_read(pull_up, &pu_value);
440 pu_value &= ~mask;
441 if (config & ST_PINCONF_PU)
442 pu_value |= mask;
443 regmap_field_write(pull_up, pu_value);
444 }
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100445
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +0100446 if (open_drain) {
447 regmap_field_read(open_drain, &od_value);
448 od_value &= ~mask;
449 if (config & ST_PINCONF_OD)
450 od_value |= mask;
451 regmap_field_write(open_drain, od_value);
452 }
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100453}
454
455static void st_pctl_set_function(struct st_pio_control *pc,
456 int pin_id, int function)
457{
458 struct regmap_field *alt = pc->alt;
459 unsigned int val;
460 int pin = st_gpio_pin(pin_id);
461 int offset = pin * 4;
462
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +0100463 if (!alt)
464 return;
465
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100466 regmap_field_read(alt, &val);
467 val &= ~(0xf << offset);
468 val |= function << offset;
469 regmap_field_write(alt, val);
470}
471
Lee Jonesc2a4bf42015-03-18 17:21:15 +0000472static unsigned int st_pctl_get_pin_function(struct st_pio_control *pc, int pin)
473{
474 struct regmap_field *alt = pc->alt;
475 unsigned int val;
476 int offset = pin * 4;
477
478 if (!alt)
479 return 0;
480
481 regmap_field_read(alt, &val);
482
483 return (val >> offset) & 0xf;
484}
485
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100486static unsigned long st_pinconf_delay_to_bit(unsigned int delay,
487 const struct st_pctl_data *data, unsigned long config)
488{
Maxime COQUELINa4bc1f52014-04-08 17:21:48 +0200489 const unsigned int *delay_times;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100490 int num_delay_times, i, closest_index = -1;
491 unsigned int closest_divergence = UINT_MAX;
492
493 if (ST_PINCONF_UNPACK_OE(config)) {
494 delay_times = data->output_delays;
495 num_delay_times = data->noutput_delays;
496 } else {
497 delay_times = data->input_delays;
498 num_delay_times = data->ninput_delays;
499 }
500
501 for (i = 0; i < num_delay_times; i++) {
502 unsigned int divergence = abs(delay - delay_times[i]);
503
504 if (divergence == 0)
505 return i;
506
507 if (divergence < closest_divergence) {
508 closest_divergence = divergence;
509 closest_index = i;
510 }
511 }
512
513 pr_warn("Attempt to set delay %d, closest available %d\n",
514 delay, delay_times[closest_index]);
515
516 return closest_index;
517}
518
519static unsigned long st_pinconf_bit_to_delay(unsigned int index,
520 const struct st_pctl_data *data, unsigned long output)
521{
Maxime COQUELINa4bc1f52014-04-08 17:21:48 +0200522 const unsigned int *delay_times;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100523 int num_delay_times;
524
525 if (output) {
526 delay_times = data->output_delays;
527 num_delay_times = data->noutput_delays;
528 } else {
529 delay_times = data->input_delays;
530 num_delay_times = data->ninput_delays;
531 }
532
533 if (index < num_delay_times) {
534 return delay_times[index];
535 } else {
536 pr_warn("Delay not found in/out delay list\n");
537 return 0;
538 }
539}
540
541static void st_regmap_field_bit_set_clear_pin(struct regmap_field *field,
542 int enable, int pin)
543{
544 unsigned int val = 0;
545
546 regmap_field_read(field, &val);
547 if (enable)
548 val |= BIT(pin);
549 else
550 val &= ~BIT(pin);
551 regmap_field_write(field, val);
552}
553
554static void st_pinconf_set_retime_packed(struct st_pinctrl *info,
555 struct st_pio_control *pc, unsigned long config, int pin)
556{
557 const struct st_pctl_data *data = info->data;
558 struct st_retime_packed *rt_p = &pc->rt.rt_p;
559 unsigned int delay;
560
561 st_regmap_field_bit_set_clear_pin(rt_p->clk1notclk0,
562 ST_PINCONF_UNPACK_RT_CLK(config), pin);
563
564 st_regmap_field_bit_set_clear_pin(rt_p->clknotdata,
565 ST_PINCONF_UNPACK_RT_CLKNOTDATA(config), pin);
566
567 st_regmap_field_bit_set_clear_pin(rt_p->double_edge,
568 ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config), pin);
569
570 st_regmap_field_bit_set_clear_pin(rt_p->invertclk,
571 ST_PINCONF_UNPACK_RT_INVERTCLK(config), pin);
572
573 st_regmap_field_bit_set_clear_pin(rt_p->retime,
574 ST_PINCONF_UNPACK_RT(config), pin);
575
576 delay = st_pinconf_delay_to_bit(ST_PINCONF_UNPACK_RT_DELAY(config),
577 data, config);
578 /* 2 bit delay, lsb */
579 st_regmap_field_bit_set_clear_pin(rt_p->delay_0, delay & 0x1, pin);
580 /* 2 bit delay, msb */
581 st_regmap_field_bit_set_clear_pin(rt_p->delay_1, delay & 0x2, pin);
582
583}
584
585static void st_pinconf_set_retime_dedicated(struct st_pinctrl *info,
586 struct st_pio_control *pc, unsigned long config, int pin)
587{
588 int input = ST_PINCONF_UNPACK_OE(config) ? 0 : 1;
589 int clk = ST_PINCONF_UNPACK_RT_CLK(config);
590 int clknotdata = ST_PINCONF_UNPACK_RT_CLKNOTDATA(config);
591 int double_edge = ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config);
592 int invertclk = ST_PINCONF_UNPACK_RT_INVERTCLK(config);
593 int retime = ST_PINCONF_UNPACK_RT(config);
594
595 unsigned long delay = st_pinconf_delay_to_bit(
596 ST_PINCONF_UNPACK_RT_DELAY(config),
597 info->data, config);
598 struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
599
600 unsigned long retime_config =
601 ((clk) << RT_D_CFG_CLK_SHIFT) |
602 ((delay) << RT_D_CFG_DELAY_SHIFT) |
603 ((input) << RT_D_CFG_DELAY_INNOTOUT_SHIFT) |
604 ((retime) << RT_D_CFG_RETIME_SHIFT) |
605 ((clknotdata) << RT_D_CFG_CLKNOTDATA_SHIFT) |
606 ((invertclk) << RT_D_CFG_INVERTCLK_SHIFT) |
607 ((double_edge) << RT_D_CFG_DOUBLE_EDGE_SHIFT);
608
609 regmap_field_write(rt_d->rt[pin], retime_config);
610}
611
612static void st_pinconf_get_direction(struct st_pio_control *pc,
613 int pin, unsigned long *config)
614{
615 unsigned int oe_value, pu_value, od_value;
616
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +0100617 if (pc->oe) {
618 regmap_field_read(pc->oe, &oe_value);
619 if (oe_value & BIT(pin))
620 ST_PINCONF_PACK_OE(*config);
621 }
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100622
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +0100623 if (pc->pu) {
624 regmap_field_read(pc->pu, &pu_value);
625 if (pu_value & BIT(pin))
626 ST_PINCONF_PACK_PU(*config);
627 }
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100628
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +0100629 if (pc->od) {
630 regmap_field_read(pc->od, &od_value);
631 if (od_value & BIT(pin))
632 ST_PINCONF_PACK_OD(*config);
633 }
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100634}
635
636static int st_pinconf_get_retime_packed(struct st_pinctrl *info,
637 struct st_pio_control *pc, int pin, unsigned long *config)
638{
639 const struct st_pctl_data *data = info->data;
640 struct st_retime_packed *rt_p = &pc->rt.rt_p;
641 unsigned int delay_bits, delay, delay0, delay1, val;
642 int output = ST_PINCONF_UNPACK_OE(*config);
643
644 if (!regmap_field_read(rt_p->retime, &val) && (val & BIT(pin)))
645 ST_PINCONF_PACK_RT(*config);
646
647 if (!regmap_field_read(rt_p->clk1notclk0, &val) && (val & BIT(pin)))
648 ST_PINCONF_PACK_RT_CLK(*config, 1);
649
650 if (!regmap_field_read(rt_p->clknotdata, &val) && (val & BIT(pin)))
651 ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
652
653 if (!regmap_field_read(rt_p->double_edge, &val) && (val & BIT(pin)))
654 ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
655
656 if (!regmap_field_read(rt_p->invertclk, &val) && (val & BIT(pin)))
657 ST_PINCONF_PACK_RT_INVERTCLK(*config);
658
659 regmap_field_read(rt_p->delay_0, &delay0);
660 regmap_field_read(rt_p->delay_1, &delay1);
661 delay_bits = (((delay1 & BIT(pin)) ? 1 : 0) << 1) |
662 (((delay0 & BIT(pin)) ? 1 : 0));
663 delay = st_pinconf_bit_to_delay(delay_bits, data, output);
664 ST_PINCONF_PACK_RT_DELAY(*config, delay);
665
666 return 0;
667}
668
669static int st_pinconf_get_retime_dedicated(struct st_pinctrl *info,
670 struct st_pio_control *pc, int pin, unsigned long *config)
671{
672 unsigned int value;
673 unsigned long delay_bits, delay, rt_clk;
674 int output = ST_PINCONF_UNPACK_OE(*config);
675 struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
676
677 regmap_field_read(rt_d->rt[pin], &value);
678
679 rt_clk = (value & RT_D_CFG_CLK_MASK) >> RT_D_CFG_CLK_SHIFT;
680 ST_PINCONF_PACK_RT_CLK(*config, rt_clk);
681
682 delay_bits = (value & RT_D_CFG_DELAY_MASK) >> RT_D_CFG_DELAY_SHIFT;
683 delay = st_pinconf_bit_to_delay(delay_bits, info->data, output);
684 ST_PINCONF_PACK_RT_DELAY(*config, delay);
685
686 if (value & RT_D_CFG_CLKNOTDATA_MASK)
687 ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
688
689 if (value & RT_D_CFG_DOUBLE_EDGE_MASK)
690 ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
691
692 if (value & RT_D_CFG_INVERTCLK_MASK)
693 ST_PINCONF_PACK_RT_INVERTCLK(*config);
694
695 if (value & RT_D_CFG_RETIME_MASK)
696 ST_PINCONF_PACK_RT(*config);
697
698 return 0;
699}
700
701/* GPIO related functions */
702
703static inline void __st_gpio_set(struct st_gpio_bank *bank,
704 unsigned offset, int value)
705{
706 if (value)
707 writel(BIT(offset), bank->base + REG_PIO_SET_POUT);
708 else
709 writel(BIT(offset), bank->base + REG_PIO_CLR_POUT);
710}
711
712static void st_gpio_direction(struct st_gpio_bank *bank,
713 unsigned int gpio, unsigned int direction)
714{
715 int offset = st_gpio_pin(gpio);
716 int i = 0;
717 /**
718 * There are three configuration registers (PIOn_PC0, PIOn_PC1
719 * and PIOn_PC2) for each port. These are used to configure the
720 * PIO port pins. Each pin can be configured as an input, output,
721 * bidirectional, or alternative function pin. Three bits, one bit
722 * from each of the three registers, configure the corresponding bit of
723 * the port. Valid bit settings is:
724 *
725 * PC2 PC1 PC0 Direction.
726 * 0 0 0 [Input Weak pull-up]
727 * 0 0 or 1 1 [Bidirection]
728 * 0 1 0 [Output]
729 * 1 0 0 [Input]
730 *
731 * PIOn_SET_PC and PIOn_CLR_PC registers are used to set and clear bits
732 * individually.
733 */
734 for (i = 0; i <= 2; i++) {
735 if (direction & BIT(i))
736 writel(BIT(offset), bank->base + REG_PIO_SET_PC(i));
737 else
738 writel(BIT(offset), bank->base + REG_PIO_CLR_PC(i));
739 }
740}
741
742static int st_gpio_request(struct gpio_chip *chip, unsigned offset)
743{
744 return pinctrl_request_gpio(chip->base + offset);
745}
746
747static void st_gpio_free(struct gpio_chip *chip, unsigned offset)
748{
749 pinctrl_free_gpio(chip->base + offset);
750}
751
752static int st_gpio_get(struct gpio_chip *chip, unsigned offset)
753{
754 struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
755
756 return !!(readl(bank->base + REG_PIO_PIN) & BIT(offset));
757}
758
759static void st_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
760{
761 struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
762 __st_gpio_set(bank, offset, value);
763}
764
765static int st_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
766{
767 pinctrl_gpio_direction_input(chip->base + offset);
768
769 return 0;
770}
771
772static int st_gpio_direction_output(struct gpio_chip *chip,
773 unsigned offset, int value)
774{
775 struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
776
777 __st_gpio_set(bank, offset, value);
778 pinctrl_gpio_direction_output(chip->base + offset);
779
780 return 0;
781}
782
Lee Jones1e702ec2015-03-18 17:21:17 +0000783static int st_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
784{
785 struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
786 struct st_pio_control pc = bank->pc;
787 unsigned long config;
788 unsigned int direction = 0;
789 unsigned int function;
790 unsigned int value;
791 int i = 0;
792
793 /* Alternate function direction is handled by Pinctrl */
794 function = st_pctl_get_pin_function(&pc, offset);
795 if (function) {
796 st_pinconf_get_direction(&pc, offset, &config);
797 return !ST_PINCONF_UNPACK_OE(config);
798 }
799
800 /*
801 * GPIO direction is handled differently
802 * - See st_gpio_direction() above for an explanation
803 */
804 for (i = 0; i <= 2; i++) {
805 value = readl(bank->base + REG_PIO_PC(i));
806 direction |= ((value >> offset) & 0x1) << i;
807 }
808
809 return (direction == ST_GPIO_DIRECTION_IN);
810}
811
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100812static int st_gpio_xlate(struct gpio_chip *gc,
813 const struct of_phandle_args *gpiospec, u32 *flags)
814{
815 if (WARN_ON(gc->of_gpio_n_cells < 1))
816 return -EINVAL;
817
818 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
819 return -EINVAL;
820
821 if (gpiospec->args[0] > gc->ngpio)
822 return -EINVAL;
823
824 return gpiospec->args[0];
825}
826
827/* Pinctrl Groups */
828static int st_pctl_get_groups_count(struct pinctrl_dev *pctldev)
829{
830 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
831
832 return info->ngroups;
833}
834
835static const char *st_pctl_get_group_name(struct pinctrl_dev *pctldev,
836 unsigned selector)
837{
838 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
839
840 return info->groups[selector].name;
841}
842
843static int st_pctl_get_group_pins(struct pinctrl_dev *pctldev,
844 unsigned selector, const unsigned **pins, unsigned *npins)
845{
846 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
847
848 if (selector >= info->ngroups)
849 return -EINVAL;
850
851 *pins = info->groups[selector].pins;
852 *npins = info->groups[selector].npins;
853
854 return 0;
855}
856
857static const inline struct st_pctl_group *st_pctl_find_group_by_name(
858 const struct st_pinctrl *info, const char *name)
859{
860 int i;
861
862 for (i = 0; i < info->ngroups; i++) {
863 if (!strcmp(info->groups[i].name, name))
864 return &info->groups[i];
865 }
866
867 return NULL;
868}
869
870static int st_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
871 struct device_node *np, struct pinctrl_map **map, unsigned *num_maps)
872{
873 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
874 const struct st_pctl_group *grp;
875 struct pinctrl_map *new_map;
876 struct device_node *parent;
877 int map_num, i;
878
879 grp = st_pctl_find_group_by_name(info, np->name);
880 if (!grp) {
881 dev_err(info->dev, "unable to find group for node %s\n",
882 np->name);
883 return -EINVAL;
884 }
885
886 map_num = grp->npins + 1;
887 new_map = devm_kzalloc(pctldev->dev,
888 sizeof(*new_map) * map_num, GFP_KERNEL);
889 if (!new_map)
890 return -ENOMEM;
891
892 parent = of_get_parent(np);
893 if (!parent) {
894 devm_kfree(pctldev->dev, new_map);
895 return -EINVAL;
896 }
897
898 *map = new_map;
899 *num_maps = map_num;
900 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
901 new_map[0].data.mux.function = parent->name;
902 new_map[0].data.mux.group = np->name;
903 of_node_put(parent);
904
905 /* create config map per pin */
906 new_map++;
907 for (i = 0; i < grp->npins; i++) {
908 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
909 new_map[i].data.configs.group_or_pin =
910 pin_get_name(pctldev, grp->pins[i]);
911 new_map[i].data.configs.configs = &grp->pin_conf[i].config;
912 new_map[i].data.configs.num_configs = 1;
913 }
914 dev_info(pctldev->dev, "maps: function %s group %s num %d\n",
915 (*map)->data.mux.function, grp->name, map_num);
916
917 return 0;
918}
919
920static void st_pctl_dt_free_map(struct pinctrl_dev *pctldev,
921 struct pinctrl_map *map, unsigned num_maps)
922{
923}
924
925static struct pinctrl_ops st_pctlops = {
926 .get_groups_count = st_pctl_get_groups_count,
927 .get_group_pins = st_pctl_get_group_pins,
928 .get_group_name = st_pctl_get_group_name,
929 .dt_node_to_map = st_pctl_dt_node_to_map,
930 .dt_free_map = st_pctl_dt_free_map,
931};
932
933/* Pinmux */
934static int st_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
935{
936 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
937
938 return info->nfunctions;
939}
940
Sachin Kamatef75bfd2013-07-29 09:52:56 +0530941static const char *st_pmx_get_fname(struct pinctrl_dev *pctldev,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100942 unsigned selector)
943{
944 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
945
946 return info->functions[selector].name;
947}
948
949static int st_pmx_get_groups(struct pinctrl_dev *pctldev,
950 unsigned selector, const char * const **grps, unsigned * const ngrps)
951{
952 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
953 *grps = info->functions[selector].groups;
954 *ngrps = info->functions[selector].ngroups;
955
956 return 0;
957}
958
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200959static int st_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned fselector,
960 unsigned group)
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100961{
962 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
963 struct st_pinconf *conf = info->groups[group].pin_conf;
964 struct st_pio_control *pc;
965 int i;
966
967 for (i = 0; i < info->groups[group].npins; i++) {
968 pc = st_get_pio_control(pctldev, conf[i].pin);
969 st_pctl_set_function(pc, conf[i].pin, conf[i].altfunc);
970 }
971
972 return 0;
973}
974
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100975static int st_pmx_set_gpio_direction(struct pinctrl_dev *pctldev,
976 struct pinctrl_gpio_range *range, unsigned gpio,
977 bool input)
978{
979 struct st_gpio_bank *bank = gpio_range_to_bank(range);
980 /*
981 * When a PIO bank is used in its primary function mode (altfunc = 0)
982 * Output Enable (OE), Open Drain(OD), and Pull Up (PU)
983 * for the primary PIO functions are driven by the related PIO block
984 */
985 st_pctl_set_function(&bank->pc, gpio, 0);
986 st_gpio_direction(bank, gpio, input ?
987 ST_GPIO_DIRECTION_IN : ST_GPIO_DIRECTION_OUT);
988
989 return 0;
990}
991
992static struct pinmux_ops st_pmxops = {
993 .get_functions_count = st_pmx_get_funcs_count,
994 .get_function_name = st_pmx_get_fname,
995 .get_function_groups = st_pmx_get_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200996 .set_mux = st_pmx_set_mux,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100997 .gpio_set_direction = st_pmx_set_gpio_direction,
998};
999
1000/* Pinconf */
1001static void st_pinconf_get_retime(struct st_pinctrl *info,
1002 struct st_pio_control *pc, int pin, unsigned long *config)
1003{
1004 if (info->data->rt_style == st_retime_style_packed)
1005 st_pinconf_get_retime_packed(info, pc, pin, config);
1006 else if (info->data->rt_style == st_retime_style_dedicated)
1007 if ((BIT(pin) & pc->rt_pin_mask))
1008 st_pinconf_get_retime_dedicated(info, pc,
1009 pin, config);
1010}
1011
1012static void st_pinconf_set_retime(struct st_pinctrl *info,
1013 struct st_pio_control *pc, int pin, unsigned long config)
1014{
1015 if (info->data->rt_style == st_retime_style_packed)
1016 st_pinconf_set_retime_packed(info, pc, config, pin);
1017 else if (info->data->rt_style == st_retime_style_dedicated)
1018 if ((BIT(pin) & pc->rt_pin_mask))
1019 st_pinconf_set_retime_dedicated(info, pc,
1020 config, pin);
1021}
1022
Sherman Yin03b054e2013-08-27 11:32:12 -07001023static int st_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin_id,
1024 unsigned long *configs, unsigned num_configs)
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001025{
1026 int pin = st_gpio_pin(pin_id);
1027 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1028 struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
Sherman Yin03b054e2013-08-27 11:32:12 -07001029 int i;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001030
Sherman Yin03b054e2013-08-27 11:32:12 -07001031 for (i = 0; i < num_configs; i++) {
1032 st_pinconf_set_config(pc, pin, configs[i]);
1033 st_pinconf_set_retime(info, pc, pin, configs[i]);
1034 } /* for each config */
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001035
1036 return 0;
1037}
1038
1039static int st_pinconf_get(struct pinctrl_dev *pctldev,
1040 unsigned pin_id, unsigned long *config)
1041{
1042 int pin = st_gpio_pin(pin_id);
1043 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1044 struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
1045
1046 *config = 0;
1047 st_pinconf_get_direction(pc, pin, config);
1048 st_pinconf_get_retime(info, pc, pin, config);
1049
1050 return 0;
1051}
1052
1053static void st_pinconf_dbg_show(struct pinctrl_dev *pctldev,
1054 struct seq_file *s, unsigned pin_id)
1055{
1056 unsigned long config;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001057
Francesco VIRLINZI96d16c32015-01-05 11:04:13 +01001058 mutex_unlock(&pctldev->mutex);
1059 st_pinconf_get(pctldev, pin_id, &config);
1060 mutex_lock(&pctldev->mutex);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001061 seq_printf(s, "[OE:%ld,PU:%ld,OD:%ld]\n"
1062 "\t\t[retime:%ld,invclk:%ld,clknotdat:%ld,"
1063 "de:%ld,rt-clk:%ld,rt-delay:%ld]",
1064 ST_PINCONF_UNPACK_OE(config),
1065 ST_PINCONF_UNPACK_PU(config),
1066 ST_PINCONF_UNPACK_OD(config),
1067 ST_PINCONF_UNPACK_RT(config),
1068 ST_PINCONF_UNPACK_RT_INVERTCLK(config),
1069 ST_PINCONF_UNPACK_RT_CLKNOTDATA(config),
1070 ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config),
1071 ST_PINCONF_UNPACK_RT_CLK(config),
1072 ST_PINCONF_UNPACK_RT_DELAY(config));
1073}
1074
1075static struct pinconf_ops st_confops = {
1076 .pin_config_get = st_pinconf_get,
1077 .pin_config_set = st_pinconf_set,
1078 .pin_config_dbg_show = st_pinconf_dbg_show,
1079};
1080
1081static void st_pctl_dt_child_count(struct st_pinctrl *info,
1082 struct device_node *np)
1083{
1084 struct device_node *child;
1085 for_each_child_of_node(np, child) {
1086 if (of_property_read_bool(child, "gpio-controller")) {
1087 info->nbanks++;
1088 } else {
1089 info->nfunctions++;
1090 info->ngroups += of_get_child_count(child);
1091 }
1092 }
1093}
1094
1095static int st_pctl_dt_setup_retime_packed(struct st_pinctrl *info,
1096 int bank, struct st_pio_control *pc)
1097{
1098 struct device *dev = info->dev;
1099 struct regmap *rm = info->regmap;
1100 const struct st_pctl_data *data = info->data;
1101 /* 2 registers per bank */
1102 int reg = (data->rt + bank * RT_P_CFGS_PER_BANK) * 4;
1103 struct st_retime_packed *rt_p = &pc->rt.rt_p;
1104 /* cfg0 */
1105 struct reg_field clk1notclk0 = RT_P_CFG0_CLK1NOTCLK0_FIELD(reg);
1106 struct reg_field delay_0 = RT_P_CFG0_DELAY_0_FIELD(reg);
1107 struct reg_field delay_1 = RT_P_CFG0_DELAY_1_FIELD(reg);
1108 /* cfg1 */
1109 struct reg_field invertclk = RT_P_CFG1_INVERTCLK_FIELD(reg + 4);
1110 struct reg_field retime = RT_P_CFG1_RETIME_FIELD(reg + 4);
1111 struct reg_field clknotdata = RT_P_CFG1_CLKNOTDATA_FIELD(reg + 4);
1112 struct reg_field double_edge = RT_P_CFG1_DOUBLE_EDGE_FIELD(reg + 4);
1113
1114 rt_p->clk1notclk0 = devm_regmap_field_alloc(dev, rm, clk1notclk0);
1115 rt_p->delay_0 = devm_regmap_field_alloc(dev, rm, delay_0);
1116 rt_p->delay_1 = devm_regmap_field_alloc(dev, rm, delay_1);
1117 rt_p->invertclk = devm_regmap_field_alloc(dev, rm, invertclk);
1118 rt_p->retime = devm_regmap_field_alloc(dev, rm, retime);
1119 rt_p->clknotdata = devm_regmap_field_alloc(dev, rm, clknotdata);
1120 rt_p->double_edge = devm_regmap_field_alloc(dev, rm, double_edge);
1121
1122 if (IS_ERR(rt_p->clk1notclk0) || IS_ERR(rt_p->delay_0) ||
1123 IS_ERR(rt_p->delay_1) || IS_ERR(rt_p->invertclk) ||
1124 IS_ERR(rt_p->retime) || IS_ERR(rt_p->clknotdata) ||
1125 IS_ERR(rt_p->double_edge))
1126 return -EINVAL;
1127
1128 return 0;
1129}
1130
1131static int st_pctl_dt_setup_retime_dedicated(struct st_pinctrl *info,
1132 int bank, struct st_pio_control *pc)
1133{
1134 struct device *dev = info->dev;
1135 struct regmap *rm = info->regmap;
1136 const struct st_pctl_data *data = info->data;
1137 /* 8 registers per bank */
1138 int reg_offset = (data->rt + bank * RT_D_CFGS_PER_BANK) * 4;
1139 struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
1140 unsigned int j;
1141 u32 pin_mask = pc->rt_pin_mask;
1142
1143 for (j = 0; j < RT_D_CFGS_PER_BANK; j++) {
1144 if (BIT(j) & pin_mask) {
1145 struct reg_field reg = REG_FIELD(reg_offset, 0, 31);
1146 rt_d->rt[j] = devm_regmap_field_alloc(dev, rm, reg);
1147 if (IS_ERR(rt_d->rt[j]))
1148 return -EINVAL;
1149 reg_offset += 4;
1150 }
1151 }
1152 return 0;
1153}
1154
1155static int st_pctl_dt_setup_retime(struct st_pinctrl *info,
1156 int bank, struct st_pio_control *pc)
1157{
1158 const struct st_pctl_data *data = info->data;
1159 if (data->rt_style == st_retime_style_packed)
1160 return st_pctl_dt_setup_retime_packed(info, bank, pc);
1161 else if (data->rt_style == st_retime_style_dedicated)
1162 return st_pctl_dt_setup_retime_dedicated(info, bank, pc);
1163
1164 return -EINVAL;
1165}
1166
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +01001167
1168static struct regmap_field *st_pc_get_value(struct device *dev,
1169 struct regmap *regmap, int bank,
1170 int data, int lsb, int msb)
1171{
1172 struct reg_field reg = REG_FIELD((data + bank) * 4, lsb, msb);
1173
1174 if (data < 0)
1175 return NULL;
1176
1177 return devm_regmap_field_alloc(dev, regmap, reg);
1178}
1179
1180static void st_parse_syscfgs(struct st_pinctrl *info, int bank,
1181 struct device_node *np)
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001182{
1183 const struct st_pctl_data *data = info->data;
1184 /**
1185 * For a given shared register like OE/PU/OD, there are 8 bits per bank
1186 * 0:7 belongs to bank0, 8:15 belongs to bank1 ...
1187 * So each register is shared across 4 banks.
1188 */
1189 int lsb = (bank%4) * ST_GPIO_PINS_PER_BANK;
1190 int msb = lsb + ST_GPIO_PINS_PER_BANK - 1;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001191 struct st_pio_control *pc = &info->banks[bank].pc;
1192 struct device *dev = info->dev;
1193 struct regmap *regmap = info->regmap;
1194
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +01001195 pc->alt = st_pc_get_value(dev, regmap, bank, data->alt, 0, 31);
1196 pc->oe = st_pc_get_value(dev, regmap, bank/4, data->oe, lsb, msb);
1197 pc->pu = st_pc_get_value(dev, regmap, bank/4, data->pu, lsb, msb);
1198 pc->od = st_pc_get_value(dev, regmap, bank/4, data->od, lsb, msb);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001199
1200 /* retime avaiable for all pins by default */
1201 pc->rt_pin_mask = 0xff;
1202 of_property_read_u32(np, "st,retime-pin-mask", &pc->rt_pin_mask);
1203 st_pctl_dt_setup_retime(info, bank, pc);
1204
Giuseppe Cavallaro4e6a6092014-03-12 09:50:06 +01001205 return;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001206}
1207
1208/*
1209 * Each pin is represented in of the below forms.
1210 * <bank offset mux direction rt_type rt_delay rt_clk>
1211 */
1212static int st_pctl_dt_parse_groups(struct device_node *np,
1213 struct st_pctl_group *grp, struct st_pinctrl *info, int idx)
1214{
1215 /* bank pad direction val altfunction */
1216 const __be32 *list;
1217 struct property *pp;
1218 struct st_pinconf *conf;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001219 struct device_node *pins;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001220 int i = 0, npins = 0, nr_props;
1221
1222 pins = of_get_child_by_name(np, "st,pins");
1223 if (!pins)
1224 return -ENODATA;
1225
1226 for_each_property_of_node(pins, pp) {
1227 /* Skip those we do not want to proceed */
1228 if (!strcmp(pp->name, "name"))
1229 continue;
1230
1231 if (pp && (pp->length/sizeof(__be32)) >= OF_GPIO_ARGS_MIN) {
1232 npins++;
1233 } else {
1234 pr_warn("Invalid st,pins in %s node\n", np->name);
1235 return -EINVAL;
1236 }
1237 }
1238
1239 grp->npins = npins;
1240 grp->name = np->name;
1241 grp->pins = devm_kzalloc(info->dev, npins * sizeof(u32), GFP_KERNEL);
1242 grp->pin_conf = devm_kzalloc(info->dev,
1243 npins * sizeof(*conf), GFP_KERNEL);
1244
1245 if (!grp->pins || !grp->pin_conf)
1246 return -ENOMEM;
1247
1248 /* <bank offset mux direction rt_type rt_delay rt_clk> */
1249 for_each_property_of_node(pins, pp) {
1250 if (!strcmp(pp->name, "name"))
1251 continue;
1252 nr_props = pp->length/sizeof(u32);
1253 list = pp->value;
1254 conf = &grp->pin_conf[i];
1255
1256 /* bank & offset */
Rickard Strandqvist1f978212014-06-26 15:44:32 +02001257 be32_to_cpup(list++);
1258 be32_to_cpup(list++);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001259 conf->pin = of_get_named_gpio(pins, pp->name, 0);
1260 conf->name = pp->name;
1261 grp->pins[i] = conf->pin;
1262 /* mux */
1263 conf->altfunc = be32_to_cpup(list++);
1264 conf->config = 0;
1265 /* direction */
1266 conf->config |= be32_to_cpup(list++);
1267 /* rt_type rt_delay rt_clk */
1268 if (nr_props >= OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN) {
1269 /* rt_type */
1270 conf->config |= be32_to_cpup(list++);
1271 /* rt_delay */
1272 conf->config |= be32_to_cpup(list++);
1273 /* rt_clk */
1274 if (nr_props > OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN)
1275 conf->config |= be32_to_cpup(list++);
1276 }
1277 i++;
1278 }
1279 of_node_put(pins);
1280
1281 return 0;
1282}
1283
1284static int st_pctl_parse_functions(struct device_node *np,
1285 struct st_pinctrl *info, u32 index, int *grp_index)
1286{
1287 struct device_node *child;
1288 struct st_pmx_func *func;
1289 struct st_pctl_group *grp;
1290 int ret, i;
1291
1292 func = &info->functions[index];
1293 func->name = np->name;
1294 func->ngroups = of_get_child_count(np);
Rickard Strandqvist8b0c1072014-06-26 13:32:49 +02001295 if (func->ngroups == 0) {
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001296 dev_err(info->dev, "No groups defined\n");
1297 return -EINVAL;
1298 }
1299 func->groups = devm_kzalloc(info->dev,
1300 func->ngroups * sizeof(char *), GFP_KERNEL);
1301 if (!func->groups)
1302 return -ENOMEM;
1303
1304 i = 0;
1305 for_each_child_of_node(np, child) {
1306 func->groups[i] = child->name;
1307 grp = &info->groups[*grp_index];
1308 *grp_index += 1;
1309 ret = st_pctl_dt_parse_groups(child, grp, info, i++);
1310 if (ret)
1311 return ret;
1312 }
1313 dev_info(info->dev, "Function[%d\t name:%s,\tgroups:%d]\n",
1314 index, func->name, func->ngroups);
1315
1316 return 0;
1317}
1318
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001319static void st_gpio_irq_mask(struct irq_data *d)
1320{
Linus Walleij130cbe32014-04-08 14:45:47 +02001321 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1322 struct st_gpio_bank *bank = gpio_chip_to_bank(gc);
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001323
1324 writel(BIT(d->hwirq), bank->base + REG_PIO_CLR_PMASK);
1325}
1326
1327static void st_gpio_irq_unmask(struct irq_data *d)
1328{
Linus Walleij130cbe32014-04-08 14:45:47 +02001329 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1330 struct st_gpio_bank *bank = gpio_chip_to_bank(gc);
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001331
1332 writel(BIT(d->hwirq), bank->base + REG_PIO_SET_PMASK);
1333}
1334
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001335static int st_gpio_irq_set_type(struct irq_data *d, unsigned type)
1336{
Linus Walleij130cbe32014-04-08 14:45:47 +02001337 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1338 struct st_gpio_bank *bank = gpio_chip_to_bank(gc);
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001339 unsigned long flags;
1340 int comp, pin = d->hwirq;
1341 u32 val;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001342 u32 pin_edge_conf = 0;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001343
1344 switch (type) {
1345 case IRQ_TYPE_LEVEL_HIGH:
1346 comp = 0;
1347 break;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001348 case IRQ_TYPE_EDGE_FALLING:
1349 comp = 0;
1350 pin_edge_conf = ST_IRQ_FALLING_EDGE_CONF(pin);
1351 break;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001352 case IRQ_TYPE_LEVEL_LOW:
1353 comp = 1;
1354 break;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001355 case IRQ_TYPE_EDGE_RISING:
1356 comp = 1;
1357 pin_edge_conf = ST_IRQ_RISING_EDGE_CONF(pin);
1358 break;
1359 case IRQ_TYPE_EDGE_BOTH:
1360 comp = st_gpio_get(&bank->gpio_chip, pin);
1361 pin_edge_conf = ST_IRQ_BOTH_EDGE_CONF(pin);
1362 break;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001363 default:
1364 return -EINVAL;
1365 }
1366
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001367 spin_lock_irqsave(&bank->lock, flags);
1368 bank->irq_edge_conf &= ~(ST_IRQ_EDGE_MASK << (
1369 pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN));
1370 bank->irq_edge_conf |= pin_edge_conf;
1371 spin_unlock_irqrestore(&bank->lock, flags);
1372
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001373 val = readl(bank->base + REG_PIO_PCOMP);
1374 val &= ~BIT(pin);
1375 val |= (comp << pin);
1376 writel(val, bank->base + REG_PIO_PCOMP);
1377
1378 return 0;
1379}
1380
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001381/*
1382 * As edge triggers are not supported at hardware level, it is supported by
1383 * software by exploiting the level trigger support in hardware.
1384 *
1385 * Steps for detection raising edge interrupt in software.
1386 *
1387 * Step 1: CONFIGURE pin to detect level LOW interrupts.
1388 *
1389 * Step 2: DETECT level LOW interrupt and in irqmux/gpio bank interrupt handler,
1390 * if the value of pin is low, then CONFIGURE pin for level HIGH interrupt.
1391 * IGNORE calling the actual interrupt handler for the pin at this stage.
1392 *
1393 * Step 3: DETECT level HIGH interrupt and in irqmux/gpio-bank interrupt handler
1394 * if the value of pin is HIGH, CONFIGURE pin for level LOW interrupt and then
1395 * DISPATCH the interrupt to the interrupt handler of the pin.
1396 *
1397 * step-1 ________ __________
1398 * | | step - 3
1399 * | |
1400 * step -2 |_____|
1401 *
1402 * falling edge is also detected int the same way.
1403 *
1404 */
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001405static void __gpio_irq_handler(struct st_gpio_bank *bank)
1406{
1407 unsigned long port_in, port_mask, port_comp, active_irqs;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001408 unsigned long bank_edge_mask, flags;
1409 int n, val, ecfg;
1410
1411 spin_lock_irqsave(&bank->lock, flags);
1412 bank_edge_mask = bank->irq_edge_conf;
1413 spin_unlock_irqrestore(&bank->lock, flags);
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001414
1415 for (;;) {
1416 port_in = readl(bank->base + REG_PIO_PIN);
1417 port_comp = readl(bank->base + REG_PIO_PCOMP);
1418 port_mask = readl(bank->base + REG_PIO_PMASK);
1419
1420 active_irqs = (port_in ^ port_comp) & port_mask;
1421
1422 if (active_irqs == 0)
1423 break;
1424
1425 for_each_set_bit(n, &active_irqs, BITS_PER_LONG) {
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001426 /* check if we are detecting fake edges ... */
1427 ecfg = ST_IRQ_EDGE_CONF(bank_edge_mask, n);
1428
1429 if (ecfg) {
1430 /* edge detection. */
1431 val = st_gpio_get(&bank->gpio_chip, n);
1432
1433 writel(BIT(n),
1434 val ? bank->base + REG_PIO_SET_PCOMP :
1435 bank->base + REG_PIO_CLR_PCOMP);
1436
1437 if (ecfg != ST_IRQ_EDGE_BOTH &&
1438 !((ecfg & ST_IRQ_EDGE_FALLING) ^ val))
1439 continue;
1440 }
1441
Linus Walleij130cbe32014-04-08 14:45:47 +02001442 generic_handle_irq(irq_find_mapping(bank->gpio_chip.irqdomain, n));
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001443 }
1444 }
1445}
1446
1447static void st_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
1448{
1449 /* interrupt dedicated per bank */
1450 struct irq_chip *chip = irq_get_chip(irq);
Linus Walleij130cbe32014-04-08 14:45:47 +02001451 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1452 struct st_gpio_bank *bank = gpio_chip_to_bank(gc);
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001453
1454 chained_irq_enter(chip, desc);
1455 __gpio_irq_handler(bank);
1456 chained_irq_exit(chip, desc);
1457}
1458
1459static void st_gpio_irqmux_handler(unsigned irq, struct irq_desc *desc)
1460{
1461 struct irq_chip *chip = irq_get_chip(irq);
1462 struct st_pinctrl *info = irq_get_handler_data(irq);
1463 unsigned long status;
1464 int n;
1465
1466 chained_irq_enter(chip, desc);
1467
1468 status = readl(info->irqmux_base);
1469
Maxime COQUELIN7a2decc2014-06-20 13:34:54 +02001470 for_each_set_bit(n, &status, info->nbanks)
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001471 __gpio_irq_handler(&info->banks[n]);
1472
1473 chained_irq_exit(chip, desc);
1474}
1475
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001476static struct gpio_chip st_gpio_template = {
1477 .request = st_gpio_request,
1478 .free = st_gpio_free,
1479 .get = st_gpio_get,
1480 .set = st_gpio_set,
1481 .direction_input = st_gpio_direction_input,
1482 .direction_output = st_gpio_direction_output,
Lee Jones1e702ec2015-03-18 17:21:17 +00001483 .get_direction = st_gpio_get_direction,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001484 .ngpio = ST_GPIO_PINS_PER_BANK,
1485 .of_gpio_n_cells = 1,
1486 .of_xlate = st_gpio_xlate,
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001487};
1488
1489static struct irq_chip st_gpio_irqchip = {
1490 .name = "GPIO",
Patrice CHOTARDfce7fcc2015-01-05 11:04:14 +01001491 .irq_disable = st_gpio_irq_mask,
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001492 .irq_mask = st_gpio_irq_mask,
1493 .irq_unmask = st_gpio_irq_unmask,
1494 .irq_set_type = st_gpio_irq_set_type,
David PARIS8708ebc2014-06-25 17:49:04 +02001495 .flags = IRQCHIP_SKIP_SET_WAKE,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001496};
1497
1498static int st_gpiolib_register_bank(struct st_pinctrl *info,
1499 int bank_nr, struct device_node *np)
1500{
1501 struct st_gpio_bank *bank = &info->banks[bank_nr];
1502 struct pinctrl_gpio_range *range = &bank->range;
1503 struct device *dev = info->dev;
1504 int bank_num = of_alias_get_id(np, "gpio");
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001505 struct resource res, irq_res;
Linus Walleij130cbe32014-04-08 14:45:47 +02001506 int gpio_irq = 0, err;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001507
1508 if (of_address_to_resource(np, 0, &res))
1509 return -ENODEV;
1510
Sachin Kamat656445f2013-07-29 09:52:55 +05301511 bank->base = devm_ioremap_resource(dev, &res);
1512 if (IS_ERR(bank->base))
1513 return PTR_ERR(bank->base);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001514
1515 bank->gpio_chip = st_gpio_template;
1516 bank->gpio_chip.base = bank_num * ST_GPIO_PINS_PER_BANK;
1517 bank->gpio_chip.ngpio = ST_GPIO_PINS_PER_BANK;
1518 bank->gpio_chip.of_node = np;
Linus Walleij130cbe32014-04-08 14:45:47 +02001519 bank->gpio_chip.dev = dev;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001520 spin_lock_init(&bank->lock);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001521
1522 of_property_read_string(np, "st,bank-name", &range->name);
1523 bank->gpio_chip.label = range->name;
1524
1525 range->id = bank_num;
1526 range->pin_base = range->base = range->id * ST_GPIO_PINS_PER_BANK;
1527 range->npins = bank->gpio_chip.ngpio;
1528 range->gc = &bank->gpio_chip;
1529 err = gpiochip_add(&bank->gpio_chip);
1530 if (err) {
1531 dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_num);
1532 return err;
1533 }
1534 dev_info(dev, "%s bank added.\n", range->name);
1535
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001536 /**
1537 * GPIO bank can have one of the two possible types of
1538 * interrupt-wirings.
1539 *
1540 * First type is via irqmux, single interrupt is used by multiple
1541 * gpio banks. This reduces number of overall interrupts numbers
1542 * required. All these banks belong to a single pincontroller.
1543 * _________
1544 * | |----> [gpio-bank (n) ]
1545 * | |----> [gpio-bank (n + 1)]
1546 * [irqN]-- | irq-mux |----> [gpio-bank (n + 2)]
1547 * | |----> [gpio-bank (... )]
1548 * |_________|----> [gpio-bank (n + 7)]
1549 *
1550 * Second type has a dedicated interrupt per each gpio bank.
1551 *
1552 * [irqN]----> [gpio-bank (n)]
1553 */
1554
Srinivas Kandagatlabcca9222014-03-12 13:35:05 +00001555 if (of_irq_to_resource(np, 0, &irq_res)) {
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001556 gpio_irq = irq_res.start;
Linus Walleij130cbe32014-04-08 14:45:47 +02001557 gpiochip_set_chained_irqchip(&bank->gpio_chip, &st_gpio_irqchip,
1558 gpio_irq, st_gpio_irq_handler);
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001559 }
1560
Pramod Gurav2e537272014-09-30 11:39:17 +05301561 if (info->irqmux_base || gpio_irq > 0) {
Linus Walleij130cbe32014-04-08 14:45:47 +02001562 err = gpiochip_irqchip_add(&bank->gpio_chip, &st_gpio_irqchip,
1563 0, handle_simple_irq,
1564 IRQ_TYPE_LEVEL_LOW);
1565 if (err) {
Pramod Gurav74717252014-09-09 13:21:40 +05301566 gpiochip_remove(&bank->gpio_chip);
Linus Walleij130cbe32014-04-08 14:45:47 +02001567 dev_info(dev, "could not add irqchip\n");
1568 return err;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001569 }
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001570 } else {
1571 dev_info(dev, "No IRQ support for %s bank\n", np->full_name);
1572 }
1573
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001574 return 0;
1575}
1576
1577static struct of_device_id st_pctl_of_match[] = {
1578 { .compatible = "st,stih415-sbc-pinctrl", .data = &stih415_sbc_data },
1579 { .compatible = "st,stih415-rear-pinctrl", .data = &stih415_rear_data },
1580 { .compatible = "st,stih415-left-pinctrl", .data = &stih415_left_data },
1581 { .compatible = "st,stih415-right-pinctrl",
1582 .data = &stih415_right_data },
1583 { .compatible = "st,stih415-front-pinctrl",
1584 .data = &stih415_front_data },
1585 { .compatible = "st,stih416-sbc-pinctrl", .data = &stih416_data},
1586 { .compatible = "st,stih416-front-pinctrl", .data = &stih416_data},
1587 { .compatible = "st,stih416-rear-pinctrl", .data = &stih416_data},
1588 { .compatible = "st,stih416-fvdp-fe-pinctrl", .data = &stih416_data},
1589 { .compatible = "st,stih416-fvdp-lite-pinctrl", .data = &stih416_data},
Giuseppe Cavallaro7ce717d2014-03-12 09:50:07 +01001590 { .compatible = "st,stih407-sbc-pinctrl", .data = &stih416_data},
1591 { .compatible = "st,stih407-front-pinctrl", .data = &stih416_data},
1592 { .compatible = "st,stih407-rear-pinctrl", .data = &stih416_data},
1593 { .compatible = "st,stih407-flash-pinctrl", .data = &stih407_flashdata},
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001594 { /* sentinel */ }
1595};
1596
1597static int st_pctl_probe_dt(struct platform_device *pdev,
1598 struct pinctrl_desc *pctl_desc, struct st_pinctrl *info)
1599{
1600 int ret = 0;
1601 int i = 0, j = 0, k = 0, bank;
1602 struct pinctrl_pin_desc *pdesc;
1603 struct device_node *np = pdev->dev.of_node;
1604 struct device_node *child;
1605 int grp_index = 0;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001606 int irq = 0;
1607 struct resource *res;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001608
1609 st_pctl_dt_child_count(info, np);
1610 if (!info->nbanks) {
1611 dev_err(&pdev->dev, "you need atleast one gpio bank\n");
1612 return -EINVAL;
1613 }
1614
1615 dev_info(&pdev->dev, "nbanks = %d\n", info->nbanks);
1616 dev_info(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1617 dev_info(&pdev->dev, "ngroups = %d\n", info->ngroups);
1618
1619 info->functions = devm_kzalloc(&pdev->dev,
1620 info->nfunctions * sizeof(*info->functions), GFP_KERNEL);
1621
1622 info->groups = devm_kzalloc(&pdev->dev,
1623 info->ngroups * sizeof(*info->groups) , GFP_KERNEL);
1624
1625 info->banks = devm_kzalloc(&pdev->dev,
1626 info->nbanks * sizeof(*info->banks), GFP_KERNEL);
1627
1628 if (!info->functions || !info->groups || !info->banks)
1629 return -ENOMEM;
1630
1631 info->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
Wei Yongjun5c75acd2013-06-28 19:30:40 +08001632 if (IS_ERR(info->regmap)) {
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001633 dev_err(info->dev, "No syscfg phandle specified\n");
Wei Yongjun5c75acd2013-06-28 19:30:40 +08001634 return PTR_ERR(info->regmap);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001635 }
1636 info->data = of_match_node(st_pctl_of_match, np)->data;
1637
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001638 irq = platform_get_irq(pdev, 0);
1639
1640 if (irq > 0) {
1641 res = platform_get_resource_byname(pdev,
1642 IORESOURCE_MEM, "irqmux");
1643 info->irqmux_base = devm_ioremap_resource(&pdev->dev, res);
1644
1645 if (IS_ERR(info->irqmux_base))
1646 return PTR_ERR(info->irqmux_base);
1647
1648 irq_set_chained_handler(irq, st_gpio_irqmux_handler);
1649 irq_set_handler_data(irq, info);
1650
1651 }
1652
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001653 pctl_desc->npins = info->nbanks * ST_GPIO_PINS_PER_BANK;
1654 pdesc = devm_kzalloc(&pdev->dev,
1655 sizeof(*pdesc) * pctl_desc->npins, GFP_KERNEL);
1656 if (!pdesc)
1657 return -ENOMEM;
1658
1659 pctl_desc->pins = pdesc;
1660
1661 bank = 0;
1662 for_each_child_of_node(np, child) {
1663 if (of_property_read_bool(child, "gpio-controller")) {
1664 const char *bank_name = NULL;
1665 ret = st_gpiolib_register_bank(info, bank, child);
1666 if (ret)
1667 return ret;
1668
1669 k = info->banks[bank].range.pin_base;
1670 bank_name = info->banks[bank].range.name;
1671 for (j = 0; j < ST_GPIO_PINS_PER_BANK; j++, k++) {
1672 pdesc->number = k;
1673 pdesc->name = kasprintf(GFP_KERNEL, "%s[%d]",
1674 bank_name, j);
1675 pdesc++;
1676 }
1677 st_parse_syscfgs(info, bank, child);
1678 bank++;
1679 } else {
1680 ret = st_pctl_parse_functions(child, info,
1681 i++, &grp_index);
1682 if (ret) {
1683 dev_err(&pdev->dev, "No functions found.\n");
1684 return ret;
1685 }
1686 }
1687 }
1688
1689 return 0;
1690}
1691
1692static int st_pctl_probe(struct platform_device *pdev)
1693{
1694 struct st_pinctrl *info;
1695 struct pinctrl_desc *pctl_desc;
1696 int ret, i;
1697
1698 if (!pdev->dev.of_node) {
1699 dev_err(&pdev->dev, "device node not found.\n");
1700 return -EINVAL;
1701 }
1702
1703 pctl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctl_desc), GFP_KERNEL);
1704 if (!pctl_desc)
1705 return -ENOMEM;
1706
1707 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1708 if (!info)
1709 return -ENOMEM;
1710
1711 info->dev = &pdev->dev;
1712 platform_set_drvdata(pdev, info);
1713 ret = st_pctl_probe_dt(pdev, pctl_desc, info);
1714 if (ret)
1715 return ret;
1716
Srinivas Kandagatlac9dd66b2014-01-14 14:52:05 +00001717 pctl_desc->owner = THIS_MODULE;
1718 pctl_desc->pctlops = &st_pctlops;
1719 pctl_desc->pmxops = &st_pmxops;
1720 pctl_desc->confops = &st_confops;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001721 pctl_desc->name = dev_name(&pdev->dev);
1722
1723 info->pctl = pinctrl_register(pctl_desc, &pdev->dev, info);
Wei Yongjun5c75acd2013-06-28 19:30:40 +08001724 if (!info->pctl) {
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001725 dev_err(&pdev->dev, "Failed pinctrl registration\n");
Wei Yongjun5c75acd2013-06-28 19:30:40 +08001726 return -EINVAL;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001727 }
1728
1729 for (i = 0; i < info->nbanks; i++)
1730 pinctrl_add_gpio_range(info->pctl, &info->banks[i].range);
1731
1732 return 0;
1733}
1734
1735static struct platform_driver st_pctl_driver = {
1736 .driver = {
1737 .name = "st-pinctrl",
Axel Lin539fde52013-06-30 08:58:57 +08001738 .of_match_table = st_pctl_of_match,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001739 },
1740 .probe = st_pctl_probe,
1741};
1742
1743static int __init st_pctl_init(void)
1744{
1745 return platform_driver_register(&st_pctl_driver);
1746}
1747arch_initcall(st_pctl_init);