blob: 9fb66aa796aaf1989ce0c5025dde3ab0bdf49cb2 [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
Srinivas Kandagatla155795b2014-01-16 15:37:31 +0000274/*
275 * Edge triggers are not supported at hardware level, it is supported by
276 * software by exploiting the level trigger support in hardware.
277 * Software uses a virtual register (EDGE_CONF) for edge trigger configuration
278 * of each gpio pin in a GPIO bank.
279 *
280 * Each bank has a 32 bit EDGE_CONF register which is divided in to 8 parts of
281 * 4-bits. Each 4-bit space is allocated for each pin in a gpio bank.
282 *
283 * bit allocation per pin is:
284 * Bits: [0 - 3] | [4 - 7] [8 - 11] ... ... ... ... [ 28 - 31]
285 * --------------------------------------------------------
286 * | pin-0 | pin-2 | pin-3 | ... ... ... ... | pin -7 |
287 * --------------------------------------------------------
288 *
289 * A pin can have one of following the values in its edge configuration field.
290 *
291 * ------- ----------------------------
292 * [0-3] - Description
293 * ------- ----------------------------
294 * 0000 - No edge IRQ.
295 * 0001 - Falling edge IRQ.
296 * 0010 - Rising edge IRQ.
297 * 0011 - Rising and Falling edge IRQ.
298 * ------- ----------------------------
299 */
300
301#define ST_IRQ_EDGE_CONF_BITS_PER_PIN 4
302#define ST_IRQ_EDGE_MASK 0xf
303#define ST_IRQ_EDGE_FALLING BIT(0)
304#define ST_IRQ_EDGE_RISING BIT(1)
305#define ST_IRQ_EDGE_BOTH (BIT(0) | BIT(1))
306
307#define ST_IRQ_RISING_EDGE_CONF(pin) \
308 (ST_IRQ_EDGE_RISING << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
309
310#define ST_IRQ_FALLING_EDGE_CONF(pin) \
311 (ST_IRQ_EDGE_FALLING << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
312
313#define ST_IRQ_BOTH_EDGE_CONF(pin) \
314 (ST_IRQ_EDGE_BOTH << (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN))
315
316#define ST_IRQ_EDGE_CONF(conf, pin) \
317 (conf >> (pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN) & ST_IRQ_EDGE_MASK)
318
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100319struct st_gpio_bank {
320 struct gpio_chip gpio_chip;
321 struct pinctrl_gpio_range range;
322 void __iomem *base;
323 struct st_pio_control pc;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +0000324 struct irq_domain *domain;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +0000325 unsigned long irq_edge_conf;
326 spinlock_t lock;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100327};
328
329struct st_pinctrl {
330 struct device *dev;
331 struct pinctrl_dev *pctl;
332 struct st_gpio_bank *banks;
333 int nbanks;
334 struct st_pmx_func *functions;
335 int nfunctions;
336 struct st_pctl_group *groups;
337 int ngroups;
338 struct regmap *regmap;
339 const struct st_pctl_data *data;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +0000340 void __iomem *irqmux_base;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100341};
342
343/* SOC specific data */
344/* STiH415 data */
Sachin Kamatef75bfd2013-07-29 09:52:56 +0530345static unsigned int stih415_input_delays[] = {0, 500, 1000, 1500};
346static unsigned int stih415_output_delays[] = {0, 1000, 2000, 3000};
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100347
348#define STIH415_PCTRL_COMMON_DATA \
349 .rt_style = st_retime_style_packed, \
350 .input_delays = stih415_input_delays, \
351 .ninput_delays = 4, \
352 .output_delays = stih415_output_delays, \
353 .noutput_delays = 4
354
355static const struct st_pctl_data stih415_sbc_data = {
356 STIH415_PCTRL_COMMON_DATA,
357 .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 16,
358};
359
360static const struct st_pctl_data stih415_front_data = {
361 STIH415_PCTRL_COMMON_DATA,
362 .alt = 0, .oe = 8, .pu = 10, .od = 12, .rt = 16,
363};
364
365static const struct st_pctl_data stih415_rear_data = {
366 STIH415_PCTRL_COMMON_DATA,
367 .alt = 0, .oe = 6, .pu = 8, .od = 10, .rt = 38,
368};
369
370static const struct st_pctl_data stih415_left_data = {
371 STIH415_PCTRL_COMMON_DATA,
372 .alt = 0, .oe = 3, .pu = 4, .od = 5, .rt = 6,
373};
374
375static const struct st_pctl_data stih415_right_data = {
376 STIH415_PCTRL_COMMON_DATA,
377 .alt = 0, .oe = 5, .pu = 7, .od = 9, .rt = 11,
378};
379
380/* STiH416 data */
Sachin Kamatef75bfd2013-07-29 09:52:56 +0530381static unsigned int stih416_delays[] = {0, 300, 500, 750, 1000, 1250, 1500,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100382 1750, 2000, 2250, 2500, 2750, 3000, 3250 };
383
384static const struct st_pctl_data stih416_data = {
385 .rt_style = st_retime_style_dedicated,
386 .input_delays = stih416_delays,
387 .ninput_delays = 14,
388 .output_delays = stih416_delays,
389 .noutput_delays = 14,
390 .alt = 0, .oe = 40, .pu = 50, .od = 60, .rt = 100,
391};
392
393/* Low level functions.. */
394static inline int st_gpio_bank(int gpio)
395{
396 return gpio/ST_GPIO_PINS_PER_BANK;
397}
398
399static inline int st_gpio_pin(int gpio)
400{
401 return gpio%ST_GPIO_PINS_PER_BANK;
402}
403
404static void st_pinconf_set_config(struct st_pio_control *pc,
405 int pin, unsigned long config)
406{
407 struct regmap_field *output_enable = pc->oe;
408 struct regmap_field *pull_up = pc->pu;
409 struct regmap_field *open_drain = pc->od;
410 unsigned int oe_value, pu_value, od_value;
411 unsigned long mask = BIT(pin);
412
413 regmap_field_read(output_enable, &oe_value);
414 regmap_field_read(pull_up, &pu_value);
415 regmap_field_read(open_drain, &od_value);
416
417 /* Clear old values */
418 oe_value &= ~mask;
419 pu_value &= ~mask;
420 od_value &= ~mask;
421
422 if (config & ST_PINCONF_OE)
423 oe_value |= mask;
424 if (config & ST_PINCONF_PU)
425 pu_value |= mask;
426 if (config & ST_PINCONF_OD)
427 od_value |= mask;
428
429 regmap_field_write(output_enable, oe_value);
430 regmap_field_write(pull_up, pu_value);
431 regmap_field_write(open_drain, od_value);
432}
433
434static void st_pctl_set_function(struct st_pio_control *pc,
435 int pin_id, int function)
436{
437 struct regmap_field *alt = pc->alt;
438 unsigned int val;
439 int pin = st_gpio_pin(pin_id);
440 int offset = pin * 4;
441
442 regmap_field_read(alt, &val);
443 val &= ~(0xf << offset);
444 val |= function << offset;
445 regmap_field_write(alt, val);
446}
447
448static unsigned long st_pinconf_delay_to_bit(unsigned int delay,
449 const struct st_pctl_data *data, unsigned long config)
450{
451 unsigned int *delay_times;
452 int num_delay_times, i, closest_index = -1;
453 unsigned int closest_divergence = UINT_MAX;
454
455 if (ST_PINCONF_UNPACK_OE(config)) {
456 delay_times = data->output_delays;
457 num_delay_times = data->noutput_delays;
458 } else {
459 delay_times = data->input_delays;
460 num_delay_times = data->ninput_delays;
461 }
462
463 for (i = 0; i < num_delay_times; i++) {
464 unsigned int divergence = abs(delay - delay_times[i]);
465
466 if (divergence == 0)
467 return i;
468
469 if (divergence < closest_divergence) {
470 closest_divergence = divergence;
471 closest_index = i;
472 }
473 }
474
475 pr_warn("Attempt to set delay %d, closest available %d\n",
476 delay, delay_times[closest_index]);
477
478 return closest_index;
479}
480
481static unsigned long st_pinconf_bit_to_delay(unsigned int index,
482 const struct st_pctl_data *data, unsigned long output)
483{
484 unsigned int *delay_times;
485 int num_delay_times;
486
487 if (output) {
488 delay_times = data->output_delays;
489 num_delay_times = data->noutput_delays;
490 } else {
491 delay_times = data->input_delays;
492 num_delay_times = data->ninput_delays;
493 }
494
495 if (index < num_delay_times) {
496 return delay_times[index];
497 } else {
498 pr_warn("Delay not found in/out delay list\n");
499 return 0;
500 }
501}
502
503static void st_regmap_field_bit_set_clear_pin(struct regmap_field *field,
504 int enable, int pin)
505{
506 unsigned int val = 0;
507
508 regmap_field_read(field, &val);
509 if (enable)
510 val |= BIT(pin);
511 else
512 val &= ~BIT(pin);
513 regmap_field_write(field, val);
514}
515
516static void st_pinconf_set_retime_packed(struct st_pinctrl *info,
517 struct st_pio_control *pc, unsigned long config, int pin)
518{
519 const struct st_pctl_data *data = info->data;
520 struct st_retime_packed *rt_p = &pc->rt.rt_p;
521 unsigned int delay;
522
523 st_regmap_field_bit_set_clear_pin(rt_p->clk1notclk0,
524 ST_PINCONF_UNPACK_RT_CLK(config), pin);
525
526 st_regmap_field_bit_set_clear_pin(rt_p->clknotdata,
527 ST_PINCONF_UNPACK_RT_CLKNOTDATA(config), pin);
528
529 st_regmap_field_bit_set_clear_pin(rt_p->double_edge,
530 ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config), pin);
531
532 st_regmap_field_bit_set_clear_pin(rt_p->invertclk,
533 ST_PINCONF_UNPACK_RT_INVERTCLK(config), pin);
534
535 st_regmap_field_bit_set_clear_pin(rt_p->retime,
536 ST_PINCONF_UNPACK_RT(config), pin);
537
538 delay = st_pinconf_delay_to_bit(ST_PINCONF_UNPACK_RT_DELAY(config),
539 data, config);
540 /* 2 bit delay, lsb */
541 st_regmap_field_bit_set_clear_pin(rt_p->delay_0, delay & 0x1, pin);
542 /* 2 bit delay, msb */
543 st_regmap_field_bit_set_clear_pin(rt_p->delay_1, delay & 0x2, pin);
544
545}
546
547static void st_pinconf_set_retime_dedicated(struct st_pinctrl *info,
548 struct st_pio_control *pc, unsigned long config, int pin)
549{
550 int input = ST_PINCONF_UNPACK_OE(config) ? 0 : 1;
551 int clk = ST_PINCONF_UNPACK_RT_CLK(config);
552 int clknotdata = ST_PINCONF_UNPACK_RT_CLKNOTDATA(config);
553 int double_edge = ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config);
554 int invertclk = ST_PINCONF_UNPACK_RT_INVERTCLK(config);
555 int retime = ST_PINCONF_UNPACK_RT(config);
556
557 unsigned long delay = st_pinconf_delay_to_bit(
558 ST_PINCONF_UNPACK_RT_DELAY(config),
559 info->data, config);
560 struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
561
562 unsigned long retime_config =
563 ((clk) << RT_D_CFG_CLK_SHIFT) |
564 ((delay) << RT_D_CFG_DELAY_SHIFT) |
565 ((input) << RT_D_CFG_DELAY_INNOTOUT_SHIFT) |
566 ((retime) << RT_D_CFG_RETIME_SHIFT) |
567 ((clknotdata) << RT_D_CFG_CLKNOTDATA_SHIFT) |
568 ((invertclk) << RT_D_CFG_INVERTCLK_SHIFT) |
569 ((double_edge) << RT_D_CFG_DOUBLE_EDGE_SHIFT);
570
571 regmap_field_write(rt_d->rt[pin], retime_config);
572}
573
574static void st_pinconf_get_direction(struct st_pio_control *pc,
575 int pin, unsigned long *config)
576{
577 unsigned int oe_value, pu_value, od_value;
578
579 regmap_field_read(pc->oe, &oe_value);
580 regmap_field_read(pc->pu, &pu_value);
581 regmap_field_read(pc->od, &od_value);
582
583 if (oe_value & BIT(pin))
584 ST_PINCONF_PACK_OE(*config);
585 if (pu_value & BIT(pin))
586 ST_PINCONF_PACK_PU(*config);
587 if (od_value & BIT(pin))
588 ST_PINCONF_PACK_OD(*config);
589
590}
591
592static int st_pinconf_get_retime_packed(struct st_pinctrl *info,
593 struct st_pio_control *pc, int pin, unsigned long *config)
594{
595 const struct st_pctl_data *data = info->data;
596 struct st_retime_packed *rt_p = &pc->rt.rt_p;
597 unsigned int delay_bits, delay, delay0, delay1, val;
598 int output = ST_PINCONF_UNPACK_OE(*config);
599
600 if (!regmap_field_read(rt_p->retime, &val) && (val & BIT(pin)))
601 ST_PINCONF_PACK_RT(*config);
602
603 if (!regmap_field_read(rt_p->clk1notclk0, &val) && (val & BIT(pin)))
604 ST_PINCONF_PACK_RT_CLK(*config, 1);
605
606 if (!regmap_field_read(rt_p->clknotdata, &val) && (val & BIT(pin)))
607 ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
608
609 if (!regmap_field_read(rt_p->double_edge, &val) && (val & BIT(pin)))
610 ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
611
612 if (!regmap_field_read(rt_p->invertclk, &val) && (val & BIT(pin)))
613 ST_PINCONF_PACK_RT_INVERTCLK(*config);
614
615 regmap_field_read(rt_p->delay_0, &delay0);
616 regmap_field_read(rt_p->delay_1, &delay1);
617 delay_bits = (((delay1 & BIT(pin)) ? 1 : 0) << 1) |
618 (((delay0 & BIT(pin)) ? 1 : 0));
619 delay = st_pinconf_bit_to_delay(delay_bits, data, output);
620 ST_PINCONF_PACK_RT_DELAY(*config, delay);
621
622 return 0;
623}
624
625static int st_pinconf_get_retime_dedicated(struct st_pinctrl *info,
626 struct st_pio_control *pc, int pin, unsigned long *config)
627{
628 unsigned int value;
629 unsigned long delay_bits, delay, rt_clk;
630 int output = ST_PINCONF_UNPACK_OE(*config);
631 struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
632
633 regmap_field_read(rt_d->rt[pin], &value);
634
635 rt_clk = (value & RT_D_CFG_CLK_MASK) >> RT_D_CFG_CLK_SHIFT;
636 ST_PINCONF_PACK_RT_CLK(*config, rt_clk);
637
638 delay_bits = (value & RT_D_CFG_DELAY_MASK) >> RT_D_CFG_DELAY_SHIFT;
639 delay = st_pinconf_bit_to_delay(delay_bits, info->data, output);
640 ST_PINCONF_PACK_RT_DELAY(*config, delay);
641
642 if (value & RT_D_CFG_CLKNOTDATA_MASK)
643 ST_PINCONF_PACK_RT_CLKNOTDATA(*config);
644
645 if (value & RT_D_CFG_DOUBLE_EDGE_MASK)
646 ST_PINCONF_PACK_RT_DOUBLE_EDGE(*config);
647
648 if (value & RT_D_CFG_INVERTCLK_MASK)
649 ST_PINCONF_PACK_RT_INVERTCLK(*config);
650
651 if (value & RT_D_CFG_RETIME_MASK)
652 ST_PINCONF_PACK_RT(*config);
653
654 return 0;
655}
656
657/* GPIO related functions */
658
659static inline void __st_gpio_set(struct st_gpio_bank *bank,
660 unsigned offset, int value)
661{
662 if (value)
663 writel(BIT(offset), bank->base + REG_PIO_SET_POUT);
664 else
665 writel(BIT(offset), bank->base + REG_PIO_CLR_POUT);
666}
667
668static void st_gpio_direction(struct st_gpio_bank *bank,
669 unsigned int gpio, unsigned int direction)
670{
671 int offset = st_gpio_pin(gpio);
672 int i = 0;
673 /**
674 * There are three configuration registers (PIOn_PC0, PIOn_PC1
675 * and PIOn_PC2) for each port. These are used to configure the
676 * PIO port pins. Each pin can be configured as an input, output,
677 * bidirectional, or alternative function pin. Three bits, one bit
678 * from each of the three registers, configure the corresponding bit of
679 * the port. Valid bit settings is:
680 *
681 * PC2 PC1 PC0 Direction.
682 * 0 0 0 [Input Weak pull-up]
683 * 0 0 or 1 1 [Bidirection]
684 * 0 1 0 [Output]
685 * 1 0 0 [Input]
686 *
687 * PIOn_SET_PC and PIOn_CLR_PC registers are used to set and clear bits
688 * individually.
689 */
690 for (i = 0; i <= 2; i++) {
691 if (direction & BIT(i))
692 writel(BIT(offset), bank->base + REG_PIO_SET_PC(i));
693 else
694 writel(BIT(offset), bank->base + REG_PIO_CLR_PC(i));
695 }
696}
697
698static int st_gpio_request(struct gpio_chip *chip, unsigned offset)
699{
700 return pinctrl_request_gpio(chip->base + offset);
701}
702
703static void st_gpio_free(struct gpio_chip *chip, unsigned offset)
704{
705 pinctrl_free_gpio(chip->base + offset);
706}
707
708static int st_gpio_get(struct gpio_chip *chip, unsigned offset)
709{
710 struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
711
712 return !!(readl(bank->base + REG_PIO_PIN) & BIT(offset));
713}
714
715static void st_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
716{
717 struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
718 __st_gpio_set(bank, offset, value);
719}
720
721static int st_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
722{
723 pinctrl_gpio_direction_input(chip->base + offset);
724
725 return 0;
726}
727
728static int st_gpio_direction_output(struct gpio_chip *chip,
729 unsigned offset, int value)
730{
731 struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
732
733 __st_gpio_set(bank, offset, value);
734 pinctrl_gpio_direction_output(chip->base + offset);
735
736 return 0;
737}
738
739static int st_gpio_xlate(struct gpio_chip *gc,
740 const struct of_phandle_args *gpiospec, u32 *flags)
741{
742 if (WARN_ON(gc->of_gpio_n_cells < 1))
743 return -EINVAL;
744
745 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
746 return -EINVAL;
747
748 if (gpiospec->args[0] > gc->ngpio)
749 return -EINVAL;
750
751 return gpiospec->args[0];
752}
753
754/* Pinctrl Groups */
755static int st_pctl_get_groups_count(struct pinctrl_dev *pctldev)
756{
757 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
758
759 return info->ngroups;
760}
761
762static const char *st_pctl_get_group_name(struct pinctrl_dev *pctldev,
763 unsigned selector)
764{
765 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
766
767 return info->groups[selector].name;
768}
769
770static int st_pctl_get_group_pins(struct pinctrl_dev *pctldev,
771 unsigned selector, const unsigned **pins, unsigned *npins)
772{
773 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
774
775 if (selector >= info->ngroups)
776 return -EINVAL;
777
778 *pins = info->groups[selector].pins;
779 *npins = info->groups[selector].npins;
780
781 return 0;
782}
783
784static const inline struct st_pctl_group *st_pctl_find_group_by_name(
785 const struct st_pinctrl *info, const char *name)
786{
787 int i;
788
789 for (i = 0; i < info->ngroups; i++) {
790 if (!strcmp(info->groups[i].name, name))
791 return &info->groups[i];
792 }
793
794 return NULL;
795}
796
797static int st_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
798 struct device_node *np, struct pinctrl_map **map, unsigned *num_maps)
799{
800 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
801 const struct st_pctl_group *grp;
802 struct pinctrl_map *new_map;
803 struct device_node *parent;
804 int map_num, i;
805
806 grp = st_pctl_find_group_by_name(info, np->name);
807 if (!grp) {
808 dev_err(info->dev, "unable to find group for node %s\n",
809 np->name);
810 return -EINVAL;
811 }
812
813 map_num = grp->npins + 1;
814 new_map = devm_kzalloc(pctldev->dev,
815 sizeof(*new_map) * map_num, GFP_KERNEL);
816 if (!new_map)
817 return -ENOMEM;
818
819 parent = of_get_parent(np);
820 if (!parent) {
821 devm_kfree(pctldev->dev, new_map);
822 return -EINVAL;
823 }
824
825 *map = new_map;
826 *num_maps = map_num;
827 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
828 new_map[0].data.mux.function = parent->name;
829 new_map[0].data.mux.group = np->name;
830 of_node_put(parent);
831
832 /* create config map per pin */
833 new_map++;
834 for (i = 0; i < grp->npins; i++) {
835 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
836 new_map[i].data.configs.group_or_pin =
837 pin_get_name(pctldev, grp->pins[i]);
838 new_map[i].data.configs.configs = &grp->pin_conf[i].config;
839 new_map[i].data.configs.num_configs = 1;
840 }
841 dev_info(pctldev->dev, "maps: function %s group %s num %d\n",
842 (*map)->data.mux.function, grp->name, map_num);
843
844 return 0;
845}
846
847static void st_pctl_dt_free_map(struct pinctrl_dev *pctldev,
848 struct pinctrl_map *map, unsigned num_maps)
849{
850}
851
852static struct pinctrl_ops st_pctlops = {
853 .get_groups_count = st_pctl_get_groups_count,
854 .get_group_pins = st_pctl_get_group_pins,
855 .get_group_name = st_pctl_get_group_name,
856 .dt_node_to_map = st_pctl_dt_node_to_map,
857 .dt_free_map = st_pctl_dt_free_map,
858};
859
860/* Pinmux */
861static int st_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
862{
863 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
864
865 return info->nfunctions;
866}
867
Sachin Kamatef75bfd2013-07-29 09:52:56 +0530868static const char *st_pmx_get_fname(struct pinctrl_dev *pctldev,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100869 unsigned selector)
870{
871 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
872
873 return info->functions[selector].name;
874}
875
876static int st_pmx_get_groups(struct pinctrl_dev *pctldev,
877 unsigned selector, const char * const **grps, unsigned * const ngrps)
878{
879 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
880 *grps = info->functions[selector].groups;
881 *ngrps = info->functions[selector].ngroups;
882
883 return 0;
884}
885
886static struct st_pio_control *st_get_pio_control(
887 struct pinctrl_dev *pctldev, int pin)
888{
889 struct pinctrl_gpio_range *range =
890 pinctrl_find_gpio_range_from_pin(pctldev, pin);
891 struct st_gpio_bank *bank = gpio_range_to_bank(range);
892
893 return &bank->pc;
894}
895
896static int st_pmx_enable(struct pinctrl_dev *pctldev, unsigned fselector,
897 unsigned group)
898{
899 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
900 struct st_pinconf *conf = info->groups[group].pin_conf;
901 struct st_pio_control *pc;
902 int i;
903
904 for (i = 0; i < info->groups[group].npins; i++) {
905 pc = st_get_pio_control(pctldev, conf[i].pin);
906 st_pctl_set_function(pc, conf[i].pin, conf[i].altfunc);
907 }
908
909 return 0;
910}
911
912static void st_pmx_disable(struct pinctrl_dev *pctldev, unsigned selector,
913 unsigned group)
914{
915}
916
917static int st_pmx_set_gpio_direction(struct pinctrl_dev *pctldev,
918 struct pinctrl_gpio_range *range, unsigned gpio,
919 bool input)
920{
921 struct st_gpio_bank *bank = gpio_range_to_bank(range);
922 /*
923 * When a PIO bank is used in its primary function mode (altfunc = 0)
924 * Output Enable (OE), Open Drain(OD), and Pull Up (PU)
925 * for the primary PIO functions are driven by the related PIO block
926 */
927 st_pctl_set_function(&bank->pc, gpio, 0);
928 st_gpio_direction(bank, gpio, input ?
929 ST_GPIO_DIRECTION_IN : ST_GPIO_DIRECTION_OUT);
930
931 return 0;
932}
933
934static struct pinmux_ops st_pmxops = {
935 .get_functions_count = st_pmx_get_funcs_count,
936 .get_function_name = st_pmx_get_fname,
937 .get_function_groups = st_pmx_get_groups,
938 .enable = st_pmx_enable,
939 .disable = st_pmx_disable,
940 .gpio_set_direction = st_pmx_set_gpio_direction,
941};
942
943/* Pinconf */
944static void st_pinconf_get_retime(struct st_pinctrl *info,
945 struct st_pio_control *pc, int pin, unsigned long *config)
946{
947 if (info->data->rt_style == st_retime_style_packed)
948 st_pinconf_get_retime_packed(info, pc, pin, config);
949 else if (info->data->rt_style == st_retime_style_dedicated)
950 if ((BIT(pin) & pc->rt_pin_mask))
951 st_pinconf_get_retime_dedicated(info, pc,
952 pin, config);
953}
954
955static void st_pinconf_set_retime(struct st_pinctrl *info,
956 struct st_pio_control *pc, int pin, unsigned long config)
957{
958 if (info->data->rt_style == st_retime_style_packed)
959 st_pinconf_set_retime_packed(info, pc, config, pin);
960 else if (info->data->rt_style == st_retime_style_dedicated)
961 if ((BIT(pin) & pc->rt_pin_mask))
962 st_pinconf_set_retime_dedicated(info, pc,
963 config, pin);
964}
965
Sherman Yin03b054e2013-08-27 11:32:12 -0700966static int st_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin_id,
967 unsigned long *configs, unsigned num_configs)
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100968{
969 int pin = st_gpio_pin(pin_id);
970 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
971 struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
Sherman Yin03b054e2013-08-27 11:32:12 -0700972 int i;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100973
Sherman Yin03b054e2013-08-27 11:32:12 -0700974 for (i = 0; i < num_configs; i++) {
975 st_pinconf_set_config(pc, pin, configs[i]);
976 st_pinconf_set_retime(info, pc, pin, configs[i]);
977 } /* for each config */
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +0100978
979 return 0;
980}
981
982static int st_pinconf_get(struct pinctrl_dev *pctldev,
983 unsigned pin_id, unsigned long *config)
984{
985 int pin = st_gpio_pin(pin_id);
986 struct st_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
987 struct st_pio_control *pc = st_get_pio_control(pctldev, pin_id);
988
989 *config = 0;
990 st_pinconf_get_direction(pc, pin, config);
991 st_pinconf_get_retime(info, pc, pin, config);
992
993 return 0;
994}
995
996static void st_pinconf_dbg_show(struct pinctrl_dev *pctldev,
997 struct seq_file *s, unsigned pin_id)
998{
999 unsigned long config;
1000 st_pinconf_get(pctldev, pin_id, &config);
1001
1002 seq_printf(s, "[OE:%ld,PU:%ld,OD:%ld]\n"
1003 "\t\t[retime:%ld,invclk:%ld,clknotdat:%ld,"
1004 "de:%ld,rt-clk:%ld,rt-delay:%ld]",
1005 ST_PINCONF_UNPACK_OE(config),
1006 ST_PINCONF_UNPACK_PU(config),
1007 ST_PINCONF_UNPACK_OD(config),
1008 ST_PINCONF_UNPACK_RT(config),
1009 ST_PINCONF_UNPACK_RT_INVERTCLK(config),
1010 ST_PINCONF_UNPACK_RT_CLKNOTDATA(config),
1011 ST_PINCONF_UNPACK_RT_DOUBLE_EDGE(config),
1012 ST_PINCONF_UNPACK_RT_CLK(config),
1013 ST_PINCONF_UNPACK_RT_DELAY(config));
1014}
1015
1016static struct pinconf_ops st_confops = {
1017 .pin_config_get = st_pinconf_get,
1018 .pin_config_set = st_pinconf_set,
1019 .pin_config_dbg_show = st_pinconf_dbg_show,
1020};
1021
1022static void st_pctl_dt_child_count(struct st_pinctrl *info,
1023 struct device_node *np)
1024{
1025 struct device_node *child;
1026 for_each_child_of_node(np, child) {
1027 if (of_property_read_bool(child, "gpio-controller")) {
1028 info->nbanks++;
1029 } else {
1030 info->nfunctions++;
1031 info->ngroups += of_get_child_count(child);
1032 }
1033 }
1034}
1035
1036static int st_pctl_dt_setup_retime_packed(struct st_pinctrl *info,
1037 int bank, struct st_pio_control *pc)
1038{
1039 struct device *dev = info->dev;
1040 struct regmap *rm = info->regmap;
1041 const struct st_pctl_data *data = info->data;
1042 /* 2 registers per bank */
1043 int reg = (data->rt + bank * RT_P_CFGS_PER_BANK) * 4;
1044 struct st_retime_packed *rt_p = &pc->rt.rt_p;
1045 /* cfg0 */
1046 struct reg_field clk1notclk0 = RT_P_CFG0_CLK1NOTCLK0_FIELD(reg);
1047 struct reg_field delay_0 = RT_P_CFG0_DELAY_0_FIELD(reg);
1048 struct reg_field delay_1 = RT_P_CFG0_DELAY_1_FIELD(reg);
1049 /* cfg1 */
1050 struct reg_field invertclk = RT_P_CFG1_INVERTCLK_FIELD(reg + 4);
1051 struct reg_field retime = RT_P_CFG1_RETIME_FIELD(reg + 4);
1052 struct reg_field clknotdata = RT_P_CFG1_CLKNOTDATA_FIELD(reg + 4);
1053 struct reg_field double_edge = RT_P_CFG1_DOUBLE_EDGE_FIELD(reg + 4);
1054
1055 rt_p->clk1notclk0 = devm_regmap_field_alloc(dev, rm, clk1notclk0);
1056 rt_p->delay_0 = devm_regmap_field_alloc(dev, rm, delay_0);
1057 rt_p->delay_1 = devm_regmap_field_alloc(dev, rm, delay_1);
1058 rt_p->invertclk = devm_regmap_field_alloc(dev, rm, invertclk);
1059 rt_p->retime = devm_regmap_field_alloc(dev, rm, retime);
1060 rt_p->clknotdata = devm_regmap_field_alloc(dev, rm, clknotdata);
1061 rt_p->double_edge = devm_regmap_field_alloc(dev, rm, double_edge);
1062
1063 if (IS_ERR(rt_p->clk1notclk0) || IS_ERR(rt_p->delay_0) ||
1064 IS_ERR(rt_p->delay_1) || IS_ERR(rt_p->invertclk) ||
1065 IS_ERR(rt_p->retime) || IS_ERR(rt_p->clknotdata) ||
1066 IS_ERR(rt_p->double_edge))
1067 return -EINVAL;
1068
1069 return 0;
1070}
1071
1072static int st_pctl_dt_setup_retime_dedicated(struct st_pinctrl *info,
1073 int bank, struct st_pio_control *pc)
1074{
1075 struct device *dev = info->dev;
1076 struct regmap *rm = info->regmap;
1077 const struct st_pctl_data *data = info->data;
1078 /* 8 registers per bank */
1079 int reg_offset = (data->rt + bank * RT_D_CFGS_PER_BANK) * 4;
1080 struct st_retime_dedicated *rt_d = &pc->rt.rt_d;
1081 unsigned int j;
1082 u32 pin_mask = pc->rt_pin_mask;
1083
1084 for (j = 0; j < RT_D_CFGS_PER_BANK; j++) {
1085 if (BIT(j) & pin_mask) {
1086 struct reg_field reg = REG_FIELD(reg_offset, 0, 31);
1087 rt_d->rt[j] = devm_regmap_field_alloc(dev, rm, reg);
1088 if (IS_ERR(rt_d->rt[j]))
1089 return -EINVAL;
1090 reg_offset += 4;
1091 }
1092 }
1093 return 0;
1094}
1095
1096static int st_pctl_dt_setup_retime(struct st_pinctrl *info,
1097 int bank, struct st_pio_control *pc)
1098{
1099 const struct st_pctl_data *data = info->data;
1100 if (data->rt_style == st_retime_style_packed)
1101 return st_pctl_dt_setup_retime_packed(info, bank, pc);
1102 else if (data->rt_style == st_retime_style_dedicated)
1103 return st_pctl_dt_setup_retime_dedicated(info, bank, pc);
1104
1105 return -EINVAL;
1106}
1107
1108static int st_parse_syscfgs(struct st_pinctrl *info,
1109 int bank, struct device_node *np)
1110{
1111 const struct st_pctl_data *data = info->data;
1112 /**
1113 * For a given shared register like OE/PU/OD, there are 8 bits per bank
1114 * 0:7 belongs to bank0, 8:15 belongs to bank1 ...
1115 * So each register is shared across 4 banks.
1116 */
1117 int lsb = (bank%4) * ST_GPIO_PINS_PER_BANK;
1118 int msb = lsb + ST_GPIO_PINS_PER_BANK - 1;
1119 struct reg_field alt_reg = REG_FIELD((data->alt + bank) * 4, 0, 31);
1120 struct reg_field oe_reg = REG_FIELD((data->oe + bank/4) * 4, lsb, msb);
1121 struct reg_field pu_reg = REG_FIELD((data->pu + bank/4) * 4, lsb, msb);
1122 struct reg_field od_reg = REG_FIELD((data->od + bank/4) * 4, lsb, msb);
1123 struct st_pio_control *pc = &info->banks[bank].pc;
1124 struct device *dev = info->dev;
1125 struct regmap *regmap = info->regmap;
1126
1127 pc->alt = devm_regmap_field_alloc(dev, regmap, alt_reg);
1128 pc->oe = devm_regmap_field_alloc(dev, regmap, oe_reg);
1129 pc->pu = devm_regmap_field_alloc(dev, regmap, pu_reg);
1130 pc->od = devm_regmap_field_alloc(dev, regmap, od_reg);
1131
1132 if (IS_ERR(pc->alt) || IS_ERR(pc->oe) ||
1133 IS_ERR(pc->pu) || IS_ERR(pc->od))
1134 return -EINVAL;
1135
1136 /* retime avaiable for all pins by default */
1137 pc->rt_pin_mask = 0xff;
1138 of_property_read_u32(np, "st,retime-pin-mask", &pc->rt_pin_mask);
1139 st_pctl_dt_setup_retime(info, bank, pc);
1140
1141 return 0;
1142}
1143
1144/*
1145 * Each pin is represented in of the below forms.
1146 * <bank offset mux direction rt_type rt_delay rt_clk>
1147 */
1148static int st_pctl_dt_parse_groups(struct device_node *np,
1149 struct st_pctl_group *grp, struct st_pinctrl *info, int idx)
1150{
1151 /* bank pad direction val altfunction */
1152 const __be32 *list;
1153 struct property *pp;
1154 struct st_pinconf *conf;
1155 phandle phandle;
1156 struct device_node *pins;
1157 u32 pin;
1158 int i = 0, npins = 0, nr_props;
1159
1160 pins = of_get_child_by_name(np, "st,pins");
1161 if (!pins)
1162 return -ENODATA;
1163
1164 for_each_property_of_node(pins, pp) {
1165 /* Skip those we do not want to proceed */
1166 if (!strcmp(pp->name, "name"))
1167 continue;
1168
1169 if (pp && (pp->length/sizeof(__be32)) >= OF_GPIO_ARGS_MIN) {
1170 npins++;
1171 } else {
1172 pr_warn("Invalid st,pins in %s node\n", np->name);
1173 return -EINVAL;
1174 }
1175 }
1176
1177 grp->npins = npins;
1178 grp->name = np->name;
1179 grp->pins = devm_kzalloc(info->dev, npins * sizeof(u32), GFP_KERNEL);
1180 grp->pin_conf = devm_kzalloc(info->dev,
1181 npins * sizeof(*conf), GFP_KERNEL);
1182
1183 if (!grp->pins || !grp->pin_conf)
1184 return -ENOMEM;
1185
1186 /* <bank offset mux direction rt_type rt_delay rt_clk> */
1187 for_each_property_of_node(pins, pp) {
1188 if (!strcmp(pp->name, "name"))
1189 continue;
1190 nr_props = pp->length/sizeof(u32);
1191 list = pp->value;
1192 conf = &grp->pin_conf[i];
1193
1194 /* bank & offset */
1195 phandle = be32_to_cpup(list++);
1196 pin = be32_to_cpup(list++);
1197 conf->pin = of_get_named_gpio(pins, pp->name, 0);
1198 conf->name = pp->name;
1199 grp->pins[i] = conf->pin;
1200 /* mux */
1201 conf->altfunc = be32_to_cpup(list++);
1202 conf->config = 0;
1203 /* direction */
1204 conf->config |= be32_to_cpup(list++);
1205 /* rt_type rt_delay rt_clk */
1206 if (nr_props >= OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN) {
1207 /* rt_type */
1208 conf->config |= be32_to_cpup(list++);
1209 /* rt_delay */
1210 conf->config |= be32_to_cpup(list++);
1211 /* rt_clk */
1212 if (nr_props > OF_GPIO_ARGS_MIN + OF_RT_ARGS_MIN)
1213 conf->config |= be32_to_cpup(list++);
1214 }
1215 i++;
1216 }
1217 of_node_put(pins);
1218
1219 return 0;
1220}
1221
1222static int st_pctl_parse_functions(struct device_node *np,
1223 struct st_pinctrl *info, u32 index, int *grp_index)
1224{
1225 struct device_node *child;
1226 struct st_pmx_func *func;
1227 struct st_pctl_group *grp;
1228 int ret, i;
1229
1230 func = &info->functions[index];
1231 func->name = np->name;
1232 func->ngroups = of_get_child_count(np);
1233 if (func->ngroups <= 0) {
1234 dev_err(info->dev, "No groups defined\n");
1235 return -EINVAL;
1236 }
1237 func->groups = devm_kzalloc(info->dev,
1238 func->ngroups * sizeof(char *), GFP_KERNEL);
1239 if (!func->groups)
1240 return -ENOMEM;
1241
1242 i = 0;
1243 for_each_child_of_node(np, child) {
1244 func->groups[i] = child->name;
1245 grp = &info->groups[*grp_index];
1246 *grp_index += 1;
1247 ret = st_pctl_dt_parse_groups(child, grp, info, i++);
1248 if (ret)
1249 return ret;
1250 }
1251 dev_info(info->dev, "Function[%d\t name:%s,\tgroups:%d]\n",
1252 index, func->name, func->ngroups);
1253
1254 return 0;
1255}
1256
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001257static int st_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
1258{
1259 struct st_gpio_bank *bank = gpio_chip_to_bank(chip);
1260 int irq = -ENXIO;
1261
1262 if (offset < chip->ngpio)
1263 irq = irq_find_mapping(bank->domain, offset);
1264
1265 dev_info(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
1266 chip->label, offset + chip->base, irq);
1267 return irq;
1268}
1269
1270static void st_gpio_irq_mask(struct irq_data *d)
1271{
1272 struct st_gpio_bank *bank = irq_data_get_irq_chip_data(d);
1273
1274 writel(BIT(d->hwirq), bank->base + REG_PIO_CLR_PMASK);
1275}
1276
1277static void st_gpio_irq_unmask(struct irq_data *d)
1278{
1279 struct st_gpio_bank *bank = irq_data_get_irq_chip_data(d);
1280
1281 writel(BIT(d->hwirq), bank->base + REG_PIO_SET_PMASK);
1282}
1283
1284static unsigned int st_gpio_irq_startup(struct irq_data *d)
1285{
1286 struct st_gpio_bank *bank = irq_data_get_irq_chip_data(d);
1287
1288 if (gpio_lock_as_irq(&bank->gpio_chip, d->hwirq))
1289 dev_err(bank->gpio_chip.dev,
1290 "unable to lock HW IRQ %lu for IRQ\n",
1291 d->hwirq);
1292
1293 st_gpio_irq_unmask(d);
1294
1295 return 0;
1296}
1297
1298static void st_gpio_irq_shutdown(struct irq_data *d)
1299{
1300 struct st_gpio_bank *bank = irq_data_get_irq_chip_data(d);
1301
1302 st_gpio_irq_mask(d);
1303 gpio_unlock_as_irq(&bank->gpio_chip, d->hwirq);
1304}
1305
1306static int st_gpio_irq_set_type(struct irq_data *d, unsigned type)
1307{
1308 struct st_gpio_bank *bank = irq_data_get_irq_chip_data(d);
1309 unsigned long flags;
1310 int comp, pin = d->hwirq;
1311 u32 val;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001312 u32 pin_edge_conf = 0;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001313
1314 switch (type) {
1315 case IRQ_TYPE_LEVEL_HIGH:
1316 comp = 0;
1317 break;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001318 case IRQ_TYPE_EDGE_FALLING:
1319 comp = 0;
1320 pin_edge_conf = ST_IRQ_FALLING_EDGE_CONF(pin);
1321 break;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001322 case IRQ_TYPE_LEVEL_LOW:
1323 comp = 1;
1324 break;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001325 case IRQ_TYPE_EDGE_RISING:
1326 comp = 1;
1327 pin_edge_conf = ST_IRQ_RISING_EDGE_CONF(pin);
1328 break;
1329 case IRQ_TYPE_EDGE_BOTH:
1330 comp = st_gpio_get(&bank->gpio_chip, pin);
1331 pin_edge_conf = ST_IRQ_BOTH_EDGE_CONF(pin);
1332 break;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001333 default:
1334 return -EINVAL;
1335 }
1336
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001337 spin_lock_irqsave(&bank->lock, flags);
1338 bank->irq_edge_conf &= ~(ST_IRQ_EDGE_MASK << (
1339 pin * ST_IRQ_EDGE_CONF_BITS_PER_PIN));
1340 bank->irq_edge_conf |= pin_edge_conf;
1341 spin_unlock_irqrestore(&bank->lock, flags);
1342
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001343 val = readl(bank->base + REG_PIO_PCOMP);
1344 val &= ~BIT(pin);
1345 val |= (comp << pin);
1346 writel(val, bank->base + REG_PIO_PCOMP);
1347
1348 return 0;
1349}
1350
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001351/*
1352 * As edge triggers are not supported at hardware level, it is supported by
1353 * software by exploiting the level trigger support in hardware.
1354 *
1355 * Steps for detection raising edge interrupt in software.
1356 *
1357 * Step 1: CONFIGURE pin to detect level LOW interrupts.
1358 *
1359 * Step 2: DETECT level LOW interrupt and in irqmux/gpio bank interrupt handler,
1360 * if the value of pin is low, then CONFIGURE pin for level HIGH interrupt.
1361 * IGNORE calling the actual interrupt handler for the pin at this stage.
1362 *
1363 * Step 3: DETECT level HIGH interrupt and in irqmux/gpio-bank interrupt handler
1364 * if the value of pin is HIGH, CONFIGURE pin for level LOW interrupt and then
1365 * DISPATCH the interrupt to the interrupt handler of the pin.
1366 *
1367 * step-1 ________ __________
1368 * | | step - 3
1369 * | |
1370 * step -2 |_____|
1371 *
1372 * falling edge is also detected int the same way.
1373 *
1374 */
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001375static void __gpio_irq_handler(struct st_gpio_bank *bank)
1376{
1377 unsigned long port_in, port_mask, port_comp, active_irqs;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001378 unsigned long bank_edge_mask, flags;
1379 int n, val, ecfg;
1380
1381 spin_lock_irqsave(&bank->lock, flags);
1382 bank_edge_mask = bank->irq_edge_conf;
1383 spin_unlock_irqrestore(&bank->lock, flags);
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001384
1385 for (;;) {
1386 port_in = readl(bank->base + REG_PIO_PIN);
1387 port_comp = readl(bank->base + REG_PIO_PCOMP);
1388 port_mask = readl(bank->base + REG_PIO_PMASK);
1389
1390 active_irqs = (port_in ^ port_comp) & port_mask;
1391
1392 if (active_irqs == 0)
1393 break;
1394
1395 for_each_set_bit(n, &active_irqs, BITS_PER_LONG) {
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001396 /* check if we are detecting fake edges ... */
1397 ecfg = ST_IRQ_EDGE_CONF(bank_edge_mask, n);
1398
1399 if (ecfg) {
1400 /* edge detection. */
1401 val = st_gpio_get(&bank->gpio_chip, n);
1402
1403 writel(BIT(n),
1404 val ? bank->base + REG_PIO_SET_PCOMP :
1405 bank->base + REG_PIO_CLR_PCOMP);
1406
1407 if (ecfg != ST_IRQ_EDGE_BOTH &&
1408 !((ecfg & ST_IRQ_EDGE_FALLING) ^ val))
1409 continue;
1410 }
1411
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001412 generic_handle_irq(irq_find_mapping(bank->domain, n));
1413 }
1414 }
1415}
1416
1417static void st_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
1418{
1419 /* interrupt dedicated per bank */
1420 struct irq_chip *chip = irq_get_chip(irq);
1421 struct st_gpio_bank *bank = irq_get_handler_data(irq);
1422
1423 chained_irq_enter(chip, desc);
1424 __gpio_irq_handler(bank);
1425 chained_irq_exit(chip, desc);
1426}
1427
1428static void st_gpio_irqmux_handler(unsigned irq, struct irq_desc *desc)
1429{
1430 struct irq_chip *chip = irq_get_chip(irq);
1431 struct st_pinctrl *info = irq_get_handler_data(irq);
1432 unsigned long status;
1433 int n;
1434
1435 chained_irq_enter(chip, desc);
1436
1437 status = readl(info->irqmux_base);
1438
1439 for_each_set_bit(n, &status, ST_GPIO_PINS_PER_BANK)
1440 __gpio_irq_handler(&info->banks[n]);
1441
1442 chained_irq_exit(chip, desc);
1443}
1444
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001445static struct gpio_chip st_gpio_template = {
1446 .request = st_gpio_request,
1447 .free = st_gpio_free,
1448 .get = st_gpio_get,
1449 .set = st_gpio_set,
1450 .direction_input = st_gpio_direction_input,
1451 .direction_output = st_gpio_direction_output,
1452 .ngpio = ST_GPIO_PINS_PER_BANK,
1453 .of_gpio_n_cells = 1,
1454 .of_xlate = st_gpio_xlate,
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001455 .to_irq = st_gpio_to_irq,
1456};
1457
1458static struct irq_chip st_gpio_irqchip = {
1459 .name = "GPIO",
1460 .irq_mask = st_gpio_irq_mask,
1461 .irq_unmask = st_gpio_irq_unmask,
1462 .irq_set_type = st_gpio_irq_set_type,
1463 .irq_startup = st_gpio_irq_startup,
1464 .irq_shutdown = st_gpio_irq_shutdown,
1465};
1466
1467static int st_gpio_irq_domain_map(struct irq_domain *h,
1468 unsigned int virq, irq_hw_number_t hw)
1469{
1470 struct st_gpio_bank *bank = h->host_data;
1471
1472 irq_set_chip(virq, &st_gpio_irqchip);
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001473 irq_set_handler(virq, handle_simple_irq);
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001474 set_irq_flags(virq, IRQF_VALID);
1475 irq_set_chip_data(virq, bank);
1476
1477 return 0;
1478}
1479
1480static struct irq_domain_ops st_gpio_irq_ops = {
1481 .map = st_gpio_irq_domain_map,
1482 .xlate = irq_domain_xlate_twocell,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001483};
1484
1485static int st_gpiolib_register_bank(struct st_pinctrl *info,
1486 int bank_nr, struct device_node *np)
1487{
1488 struct st_gpio_bank *bank = &info->banks[bank_nr];
1489 struct pinctrl_gpio_range *range = &bank->range;
1490 struct device *dev = info->dev;
1491 int bank_num = of_alias_get_id(np, "gpio");
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001492 struct resource res, irq_res;
1493 int gpio_irq = 0, err, i;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001494
1495 if (of_address_to_resource(np, 0, &res))
1496 return -ENODEV;
1497
Sachin Kamat656445f2013-07-29 09:52:55 +05301498 bank->base = devm_ioremap_resource(dev, &res);
1499 if (IS_ERR(bank->base))
1500 return PTR_ERR(bank->base);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001501
1502 bank->gpio_chip = st_gpio_template;
1503 bank->gpio_chip.base = bank_num * ST_GPIO_PINS_PER_BANK;
1504 bank->gpio_chip.ngpio = ST_GPIO_PINS_PER_BANK;
1505 bank->gpio_chip.of_node = np;
Srinivas Kandagatla155795b2014-01-16 15:37:31 +00001506 spin_lock_init(&bank->lock);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001507
1508 of_property_read_string(np, "st,bank-name", &range->name);
1509 bank->gpio_chip.label = range->name;
1510
1511 range->id = bank_num;
1512 range->pin_base = range->base = range->id * ST_GPIO_PINS_PER_BANK;
1513 range->npins = bank->gpio_chip.ngpio;
1514 range->gc = &bank->gpio_chip;
1515 err = gpiochip_add(&bank->gpio_chip);
1516 if (err) {
1517 dev_err(dev, "Failed to add gpiochip(%d)!\n", bank_num);
1518 return err;
1519 }
1520 dev_info(dev, "%s bank added.\n", range->name);
1521
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001522 /**
1523 * GPIO bank can have one of the two possible types of
1524 * interrupt-wirings.
1525 *
1526 * First type is via irqmux, single interrupt is used by multiple
1527 * gpio banks. This reduces number of overall interrupts numbers
1528 * required. All these banks belong to a single pincontroller.
1529 * _________
1530 * | |----> [gpio-bank (n) ]
1531 * | |----> [gpio-bank (n + 1)]
1532 * [irqN]-- | irq-mux |----> [gpio-bank (n + 2)]
1533 * | |----> [gpio-bank (... )]
1534 * |_________|----> [gpio-bank (n + 7)]
1535 *
1536 * Second type has a dedicated interrupt per each gpio bank.
1537 *
1538 * [irqN]----> [gpio-bank (n)]
1539 */
1540
1541 if (!of_irq_to_resource(np, 0, &irq_res)) {
1542 gpio_irq = irq_res.start;
1543 irq_set_chained_handler(gpio_irq, st_gpio_irq_handler);
1544 irq_set_handler_data(gpio_irq, bank);
1545 }
1546
1547 if (info->irqmux_base > 0 || gpio_irq > 0) {
1548 /* Setup IRQ domain */
1549 bank->domain = irq_domain_add_linear(np,
1550 ST_GPIO_PINS_PER_BANK,
1551 &st_gpio_irq_ops, bank);
1552 if (!bank->domain) {
1553 dev_err(dev, "Failed to add irq domain for %s\n",
1554 np->full_name);
1555 } else {
1556 for (i = 0; i < ST_GPIO_PINS_PER_BANK; i++) {
1557 if (irq_create_mapping(bank->domain, i) < 0)
1558 dev_err(dev,
1559 "Failed to map IRQ %i\n", i);
1560 }
1561 }
1562
1563 } else {
1564 dev_info(dev, "No IRQ support for %s bank\n", np->full_name);
1565 }
1566
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001567 return 0;
1568}
1569
1570static struct of_device_id st_pctl_of_match[] = {
1571 { .compatible = "st,stih415-sbc-pinctrl", .data = &stih415_sbc_data },
1572 { .compatible = "st,stih415-rear-pinctrl", .data = &stih415_rear_data },
1573 { .compatible = "st,stih415-left-pinctrl", .data = &stih415_left_data },
1574 { .compatible = "st,stih415-right-pinctrl",
1575 .data = &stih415_right_data },
1576 { .compatible = "st,stih415-front-pinctrl",
1577 .data = &stih415_front_data },
1578 { .compatible = "st,stih416-sbc-pinctrl", .data = &stih416_data},
1579 { .compatible = "st,stih416-front-pinctrl", .data = &stih416_data},
1580 { .compatible = "st,stih416-rear-pinctrl", .data = &stih416_data},
1581 { .compatible = "st,stih416-fvdp-fe-pinctrl", .data = &stih416_data},
1582 { .compatible = "st,stih416-fvdp-lite-pinctrl", .data = &stih416_data},
1583 { /* sentinel */ }
1584};
1585
1586static int st_pctl_probe_dt(struct platform_device *pdev,
1587 struct pinctrl_desc *pctl_desc, struct st_pinctrl *info)
1588{
1589 int ret = 0;
1590 int i = 0, j = 0, k = 0, bank;
1591 struct pinctrl_pin_desc *pdesc;
1592 struct device_node *np = pdev->dev.of_node;
1593 struct device_node *child;
1594 int grp_index = 0;
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001595 int irq = 0;
1596 struct resource *res;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001597
1598 st_pctl_dt_child_count(info, np);
1599 if (!info->nbanks) {
1600 dev_err(&pdev->dev, "you need atleast one gpio bank\n");
1601 return -EINVAL;
1602 }
1603
1604 dev_info(&pdev->dev, "nbanks = %d\n", info->nbanks);
1605 dev_info(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1606 dev_info(&pdev->dev, "ngroups = %d\n", info->ngroups);
1607
1608 info->functions = devm_kzalloc(&pdev->dev,
1609 info->nfunctions * sizeof(*info->functions), GFP_KERNEL);
1610
1611 info->groups = devm_kzalloc(&pdev->dev,
1612 info->ngroups * sizeof(*info->groups) , GFP_KERNEL);
1613
1614 info->banks = devm_kzalloc(&pdev->dev,
1615 info->nbanks * sizeof(*info->banks), GFP_KERNEL);
1616
1617 if (!info->functions || !info->groups || !info->banks)
1618 return -ENOMEM;
1619
1620 info->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
Wei Yongjun5c75acd2013-06-28 19:30:40 +08001621 if (IS_ERR(info->regmap)) {
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001622 dev_err(info->dev, "No syscfg phandle specified\n");
Wei Yongjun5c75acd2013-06-28 19:30:40 +08001623 return PTR_ERR(info->regmap);
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001624 }
1625 info->data = of_match_node(st_pctl_of_match, np)->data;
1626
Srinivas Kandagatla727b0f72014-01-16 15:36:53 +00001627 irq = platform_get_irq(pdev, 0);
1628
1629 if (irq > 0) {
1630 res = platform_get_resource_byname(pdev,
1631 IORESOURCE_MEM, "irqmux");
1632 info->irqmux_base = devm_ioremap_resource(&pdev->dev, res);
1633
1634 if (IS_ERR(info->irqmux_base))
1635 return PTR_ERR(info->irqmux_base);
1636
1637 irq_set_chained_handler(irq, st_gpio_irqmux_handler);
1638 irq_set_handler_data(irq, info);
1639
1640 }
1641
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001642 pctl_desc->npins = info->nbanks * ST_GPIO_PINS_PER_BANK;
1643 pdesc = devm_kzalloc(&pdev->dev,
1644 sizeof(*pdesc) * pctl_desc->npins, GFP_KERNEL);
1645 if (!pdesc)
1646 return -ENOMEM;
1647
1648 pctl_desc->pins = pdesc;
1649
1650 bank = 0;
1651 for_each_child_of_node(np, child) {
1652 if (of_property_read_bool(child, "gpio-controller")) {
1653 const char *bank_name = NULL;
1654 ret = st_gpiolib_register_bank(info, bank, child);
1655 if (ret)
1656 return ret;
1657
1658 k = info->banks[bank].range.pin_base;
1659 bank_name = info->banks[bank].range.name;
1660 for (j = 0; j < ST_GPIO_PINS_PER_BANK; j++, k++) {
1661 pdesc->number = k;
1662 pdesc->name = kasprintf(GFP_KERNEL, "%s[%d]",
1663 bank_name, j);
1664 pdesc++;
1665 }
1666 st_parse_syscfgs(info, bank, child);
1667 bank++;
1668 } else {
1669 ret = st_pctl_parse_functions(child, info,
1670 i++, &grp_index);
1671 if (ret) {
1672 dev_err(&pdev->dev, "No functions found.\n");
1673 return ret;
1674 }
1675 }
1676 }
1677
1678 return 0;
1679}
1680
1681static int st_pctl_probe(struct platform_device *pdev)
1682{
1683 struct st_pinctrl *info;
1684 struct pinctrl_desc *pctl_desc;
1685 int ret, i;
1686
1687 if (!pdev->dev.of_node) {
1688 dev_err(&pdev->dev, "device node not found.\n");
1689 return -EINVAL;
1690 }
1691
1692 pctl_desc = devm_kzalloc(&pdev->dev, sizeof(*pctl_desc), GFP_KERNEL);
1693 if (!pctl_desc)
1694 return -ENOMEM;
1695
1696 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1697 if (!info)
1698 return -ENOMEM;
1699
1700 info->dev = &pdev->dev;
1701 platform_set_drvdata(pdev, info);
1702 ret = st_pctl_probe_dt(pdev, pctl_desc, info);
1703 if (ret)
1704 return ret;
1705
Srinivas Kandagatlac9dd66b2014-01-14 14:52:05 +00001706 pctl_desc->owner = THIS_MODULE;
1707 pctl_desc->pctlops = &st_pctlops;
1708 pctl_desc->pmxops = &st_pmxops;
1709 pctl_desc->confops = &st_confops;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001710 pctl_desc->name = dev_name(&pdev->dev);
1711
1712 info->pctl = pinctrl_register(pctl_desc, &pdev->dev, info);
Wei Yongjun5c75acd2013-06-28 19:30:40 +08001713 if (!info->pctl) {
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001714 dev_err(&pdev->dev, "Failed pinctrl registration\n");
Wei Yongjun5c75acd2013-06-28 19:30:40 +08001715 return -EINVAL;
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001716 }
1717
1718 for (i = 0; i < info->nbanks; i++)
1719 pinctrl_add_gpio_range(info->pctl, &info->banks[i].range);
1720
1721 return 0;
1722}
1723
1724static struct platform_driver st_pctl_driver = {
1725 .driver = {
1726 .name = "st-pinctrl",
1727 .owner = THIS_MODULE,
Axel Lin539fde52013-06-30 08:58:57 +08001728 .of_match_table = st_pctl_of_match,
Srinivas KANDAGATLA701016c2013-06-20 15:05:38 +01001729 },
1730 .probe = st_pctl_probe,
1731};
1732
1733static int __init st_pctl_init(void)
1734{
1735 return platform_driver_register(&st_pctl_driver);
1736}
1737arch_initcall(st_pctl_init);