Linux 4.16.11
[linux/fpc-iii.git] / drivers / pinctrl / pinctrl-tz1090.c
blob2379ce2be3656c85f3e25b861982eb8daf83dcb3
1 /*
2 * Pinctrl driver for the Toumaz Xenif TZ1090 SoC
4 * Copyright (c) 2013, Imagination Technologies Ltd.
6 * Derived from Tegra code:
7 * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
9 * Derived from code:
10 * Copyright (C) 2010 Google, Inc.
11 * Copyright (C) 2010 NVIDIA Corporation
12 * Copyright (C) 2009-2011 ST-Ericsson AB
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms and conditions of the GNU General Public License,
16 * version 2, as published by the Free Software Foundation.
18 * This program is distributed in the hope it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * more details.
24 #include <linux/bitops.h>
25 #include <linux/io.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/platform_device.h>
29 #include <linux/pinctrl/machine.h>
30 #include <linux/pinctrl/pinconf-generic.h>
31 #include <linux/pinctrl/pinctrl.h>
32 #include <linux/pinctrl/pinmux.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
37 * The registers may be shared with other threads/cores, so we need to use the
38 * metag global lock2 for atomicity.
40 #include <asm/global_lock.h>
42 #include "core.h"
43 #include "pinconf.h"
45 /* Register offsets from bank base address */
46 #define REG_PINCTRL_SELECT 0x10
47 #define REG_PINCTRL_SCHMITT 0x90
48 #define REG_PINCTRL_PU_PD 0xa0
49 #define REG_PINCTRL_SR 0xc0
50 #define REG_PINCTRL_DR 0xd0
51 #define REG_PINCTRL_IF_CTL 0xe0
53 /* REG_PINCTRL_PU_PD field values */
54 #define REG_PU_PD_TRISTATE 0
55 #define REG_PU_PD_UP 1
56 #define REG_PU_PD_DOWN 2
57 #define REG_PU_PD_REPEATER 3
59 /* REG_PINCTRL_DR field values */
60 #define REG_DR_2mA 0
61 #define REG_DR_4mA 1
62 #define REG_DR_8mA 2
63 #define REG_DR_12mA 3
65 /**
66 * struct tz1090_function - TZ1090 pinctrl mux function
67 * @name: The name of the function, exported to pinctrl core.
68 * @groups: An array of pin groups that may select this function.
69 * @ngroups: The number of entries in @groups.
71 struct tz1090_function {
72 const char *name;
73 const char * const *groups;
74 unsigned int ngroups;
77 /**
78 * struct tz1090_muxdesc - TZ1090 individual mux description
79 * @funcs: Function for each mux value.
80 * @reg: Mux register offset. 0 if unsupported.
81 * @bit: Mux register bit. 0 if unsupported.
82 * @width: Mux field width. 0 if unsupported.
84 * A representation of a group of signals (possibly just one signal) in the
85 * TZ1090 which can be muxed to a set of functions or sub muxes.
87 struct tz1090_muxdesc {
88 int funcs[5];
89 u16 reg;
90 u8 bit;
91 u8 width;
94 /**
95 * struct tz1090_pingroup - TZ1090 pin group
96 * @name: Name of pin group.
97 * @pins: Array of pin numbers in this pin group.
98 * @npins: Number of pins in this pin group.
99 * @mux: Top level mux.
100 * @drv: Drive control supported, 0 if unsupported.
101 * This means Schmitt, Slew, and Drive strength.
102 * @slw_bit: Slew register bit. 0 if unsupported.
103 * The same bit is used for Schmitt, and Drive (*2).
104 * @func: Currently muxed function.
105 * @func_count: Number of pins using current mux function.
107 * A representation of a group of pins (possibly just one pin) in the TZ1090
108 * pin controller. Each group allows some parameter or parameters to be
109 * configured. The most common is mux function selection.
111 struct tz1090_pingroup {
112 const char *name;
113 const unsigned int *pins;
114 unsigned int npins;
115 struct tz1090_muxdesc mux;
117 bool drv;
118 u8 slw_bit;
120 int func;
121 unsigned int func_count;
125 * Most pins affected by the pinmux can also be GPIOs. Define these first.
126 * These must match how the GPIO driver names/numbers its pins.
129 enum tz1090_pin {
130 /* GPIO pins */
131 TZ1090_PIN_SDIO_CLK,
132 TZ1090_PIN_SDIO_CMD,
133 TZ1090_PIN_SDIO_D0,
134 TZ1090_PIN_SDIO_D1,
135 TZ1090_PIN_SDIO_D2,
136 TZ1090_PIN_SDIO_D3,
137 TZ1090_PIN_SDH_CD,
138 TZ1090_PIN_SDH_WP,
139 TZ1090_PIN_SPI0_MCLK,
140 TZ1090_PIN_SPI0_CS0,
141 TZ1090_PIN_SPI0_CS1,
142 TZ1090_PIN_SPI0_CS2,
143 TZ1090_PIN_SPI0_DOUT,
144 TZ1090_PIN_SPI0_DIN,
145 TZ1090_PIN_SPI1_MCLK,
146 TZ1090_PIN_SPI1_CS0,
147 TZ1090_PIN_SPI1_CS1,
148 TZ1090_PIN_SPI1_CS2,
149 TZ1090_PIN_SPI1_DOUT,
150 TZ1090_PIN_SPI1_DIN,
151 TZ1090_PIN_UART0_RXD,
152 TZ1090_PIN_UART0_TXD,
153 TZ1090_PIN_UART0_CTS,
154 TZ1090_PIN_UART0_RTS,
155 TZ1090_PIN_UART1_RXD,
156 TZ1090_PIN_UART1_TXD,
157 TZ1090_PIN_SCB0_SDAT,
158 TZ1090_PIN_SCB0_SCLK,
159 TZ1090_PIN_SCB1_SDAT,
160 TZ1090_PIN_SCB1_SCLK,
161 TZ1090_PIN_SCB2_SDAT,
162 TZ1090_PIN_SCB2_SCLK,
163 TZ1090_PIN_I2S_MCLK,
164 TZ1090_PIN_I2S_BCLK_OUT,
165 TZ1090_PIN_I2S_LRCLK_OUT,
166 TZ1090_PIN_I2S_DOUT0,
167 TZ1090_PIN_I2S_DOUT1,
168 TZ1090_PIN_I2S_DOUT2,
169 TZ1090_PIN_I2S_DIN,
170 TZ1090_PIN_PDM_A,
171 TZ1090_PIN_PDM_B,
172 TZ1090_PIN_PDM_C,
173 TZ1090_PIN_PDM_D,
174 TZ1090_PIN_TFT_RED0,
175 TZ1090_PIN_TFT_RED1,
176 TZ1090_PIN_TFT_RED2,
177 TZ1090_PIN_TFT_RED3,
178 TZ1090_PIN_TFT_RED4,
179 TZ1090_PIN_TFT_RED5,
180 TZ1090_PIN_TFT_RED6,
181 TZ1090_PIN_TFT_RED7,
182 TZ1090_PIN_TFT_GREEN0,
183 TZ1090_PIN_TFT_GREEN1,
184 TZ1090_PIN_TFT_GREEN2,
185 TZ1090_PIN_TFT_GREEN3,
186 TZ1090_PIN_TFT_GREEN4,
187 TZ1090_PIN_TFT_GREEN5,
188 TZ1090_PIN_TFT_GREEN6,
189 TZ1090_PIN_TFT_GREEN7,
190 TZ1090_PIN_TFT_BLUE0,
191 TZ1090_PIN_TFT_BLUE1,
192 TZ1090_PIN_TFT_BLUE2,
193 TZ1090_PIN_TFT_BLUE3,
194 TZ1090_PIN_TFT_BLUE4,
195 TZ1090_PIN_TFT_BLUE5,
196 TZ1090_PIN_TFT_BLUE6,
197 TZ1090_PIN_TFT_BLUE7,
198 TZ1090_PIN_TFT_VDDEN_GD,
199 TZ1090_PIN_TFT_PANELCLK,
200 TZ1090_PIN_TFT_BLANK_LS,
201 TZ1090_PIN_TFT_VSYNC_NS,
202 TZ1090_PIN_TFT_HSYNC_NR,
203 TZ1090_PIN_TFT_VD12ACB,
204 TZ1090_PIN_TFT_PWRSAVE,
205 TZ1090_PIN_TX_ON,
206 TZ1090_PIN_RX_ON,
207 TZ1090_PIN_PLL_ON,
208 TZ1090_PIN_PA_ON,
209 TZ1090_PIN_RX_HP,
210 TZ1090_PIN_GAIN0,
211 TZ1090_PIN_GAIN1,
212 TZ1090_PIN_GAIN2,
213 TZ1090_PIN_GAIN3,
214 TZ1090_PIN_GAIN4,
215 TZ1090_PIN_GAIN5,
216 TZ1090_PIN_GAIN6,
217 TZ1090_PIN_GAIN7,
218 TZ1090_PIN_ANT_SEL0,
219 TZ1090_PIN_ANT_SEL1,
220 TZ1090_PIN_SDH_CLK_IN,
222 /* Non-GPIO pins */
223 TZ1090_PIN_TCK,
224 TZ1090_PIN_TRST,
225 TZ1090_PIN_TDI,
226 TZ1090_PIN_TDO,
227 TZ1090_PIN_TMS,
228 TZ1090_PIN_CLK_OUT0,
229 TZ1090_PIN_CLK_OUT1,
231 NUM_GPIOS = TZ1090_PIN_TCK,
234 /* Pin names */
236 static const struct pinctrl_pin_desc tz1090_pins[] = {
237 /* GPIO pins */
238 PINCTRL_PIN(TZ1090_PIN_SDIO_CLK, "sdio_clk"),
239 PINCTRL_PIN(TZ1090_PIN_SDIO_CMD, "sdio_cmd"),
240 PINCTRL_PIN(TZ1090_PIN_SDIO_D0, "sdio_d0"),
241 PINCTRL_PIN(TZ1090_PIN_SDIO_D1, "sdio_d1"),
242 PINCTRL_PIN(TZ1090_PIN_SDIO_D2, "sdio_d2"),
243 PINCTRL_PIN(TZ1090_PIN_SDIO_D3, "sdio_d3"),
244 PINCTRL_PIN(TZ1090_PIN_SDH_CD, "sdh_cd"),
245 PINCTRL_PIN(TZ1090_PIN_SDH_WP, "sdh_wp"),
246 PINCTRL_PIN(TZ1090_PIN_SPI0_MCLK, "spi0_mclk"),
247 PINCTRL_PIN(TZ1090_PIN_SPI0_CS0, "spi0_cs0"),
248 PINCTRL_PIN(TZ1090_PIN_SPI0_CS1, "spi0_cs1"),
249 PINCTRL_PIN(TZ1090_PIN_SPI0_CS2, "spi0_cs2"),
250 PINCTRL_PIN(TZ1090_PIN_SPI0_DOUT, "spi0_dout"),
251 PINCTRL_PIN(TZ1090_PIN_SPI0_DIN, "spi0_din"),
252 PINCTRL_PIN(TZ1090_PIN_SPI1_MCLK, "spi1_mclk"),
253 PINCTRL_PIN(TZ1090_PIN_SPI1_CS0, "spi1_cs0"),
254 PINCTRL_PIN(TZ1090_PIN_SPI1_CS1, "spi1_cs1"),
255 PINCTRL_PIN(TZ1090_PIN_SPI1_CS2, "spi1_cs2"),
256 PINCTRL_PIN(TZ1090_PIN_SPI1_DOUT, "spi1_dout"),
257 PINCTRL_PIN(TZ1090_PIN_SPI1_DIN, "spi1_din"),
258 PINCTRL_PIN(TZ1090_PIN_UART0_RXD, "uart0_rxd"),
259 PINCTRL_PIN(TZ1090_PIN_UART0_TXD, "uart0_txd"),
260 PINCTRL_PIN(TZ1090_PIN_UART0_CTS, "uart0_cts"),
261 PINCTRL_PIN(TZ1090_PIN_UART0_RTS, "uart0_rts"),
262 PINCTRL_PIN(TZ1090_PIN_UART1_RXD, "uart1_rxd"),
263 PINCTRL_PIN(TZ1090_PIN_UART1_TXD, "uart1_txd"),
264 PINCTRL_PIN(TZ1090_PIN_SCB0_SDAT, "scb0_sdat"),
265 PINCTRL_PIN(TZ1090_PIN_SCB0_SCLK, "scb0_sclk"),
266 PINCTRL_PIN(TZ1090_PIN_SCB1_SDAT, "scb1_sdat"),
267 PINCTRL_PIN(TZ1090_PIN_SCB1_SCLK, "scb1_sclk"),
268 PINCTRL_PIN(TZ1090_PIN_SCB2_SDAT, "scb2_sdat"),
269 PINCTRL_PIN(TZ1090_PIN_SCB2_SCLK, "scb2_sclk"),
270 PINCTRL_PIN(TZ1090_PIN_I2S_MCLK, "i2s_mclk"),
271 PINCTRL_PIN(TZ1090_PIN_I2S_BCLK_OUT, "i2s_bclk_out"),
272 PINCTRL_PIN(TZ1090_PIN_I2S_LRCLK_OUT, "i2s_lrclk_out"),
273 PINCTRL_PIN(TZ1090_PIN_I2S_DOUT0, "i2s_dout0"),
274 PINCTRL_PIN(TZ1090_PIN_I2S_DOUT1, "i2s_dout1"),
275 PINCTRL_PIN(TZ1090_PIN_I2S_DOUT2, "i2s_dout2"),
276 PINCTRL_PIN(TZ1090_PIN_I2S_DIN, "i2s_din"),
277 PINCTRL_PIN(TZ1090_PIN_PDM_A, "pdm_a"),
278 PINCTRL_PIN(TZ1090_PIN_PDM_B, "pdm_b"),
279 PINCTRL_PIN(TZ1090_PIN_PDM_C, "pdm_c"),
280 PINCTRL_PIN(TZ1090_PIN_PDM_D, "pdm_d"),
281 PINCTRL_PIN(TZ1090_PIN_TFT_RED0, "tft_red0"),
282 PINCTRL_PIN(TZ1090_PIN_TFT_RED1, "tft_red1"),
283 PINCTRL_PIN(TZ1090_PIN_TFT_RED2, "tft_red2"),
284 PINCTRL_PIN(TZ1090_PIN_TFT_RED3, "tft_red3"),
285 PINCTRL_PIN(TZ1090_PIN_TFT_RED4, "tft_red4"),
286 PINCTRL_PIN(TZ1090_PIN_TFT_RED5, "tft_red5"),
287 PINCTRL_PIN(TZ1090_PIN_TFT_RED6, "tft_red6"),
288 PINCTRL_PIN(TZ1090_PIN_TFT_RED7, "tft_red7"),
289 PINCTRL_PIN(TZ1090_PIN_TFT_GREEN0, "tft_green0"),
290 PINCTRL_PIN(TZ1090_PIN_TFT_GREEN1, "tft_green1"),
291 PINCTRL_PIN(TZ1090_PIN_TFT_GREEN2, "tft_green2"),
292 PINCTRL_PIN(TZ1090_PIN_TFT_GREEN3, "tft_green3"),
293 PINCTRL_PIN(TZ1090_PIN_TFT_GREEN4, "tft_green4"),
294 PINCTRL_PIN(TZ1090_PIN_TFT_GREEN5, "tft_green5"),
295 PINCTRL_PIN(TZ1090_PIN_TFT_GREEN6, "tft_green6"),
296 PINCTRL_PIN(TZ1090_PIN_TFT_GREEN7, "tft_green7"),
297 PINCTRL_PIN(TZ1090_PIN_TFT_BLUE0, "tft_blue0"),
298 PINCTRL_PIN(TZ1090_PIN_TFT_BLUE1, "tft_blue1"),
299 PINCTRL_PIN(TZ1090_PIN_TFT_BLUE2, "tft_blue2"),
300 PINCTRL_PIN(TZ1090_PIN_TFT_BLUE3, "tft_blue3"),
301 PINCTRL_PIN(TZ1090_PIN_TFT_BLUE4, "tft_blue4"),
302 PINCTRL_PIN(TZ1090_PIN_TFT_BLUE5, "tft_blue5"),
303 PINCTRL_PIN(TZ1090_PIN_TFT_BLUE6, "tft_blue6"),
304 PINCTRL_PIN(TZ1090_PIN_TFT_BLUE7, "tft_blue7"),
305 PINCTRL_PIN(TZ1090_PIN_TFT_VDDEN_GD, "tft_vdden_gd"),
306 PINCTRL_PIN(TZ1090_PIN_TFT_PANELCLK, "tft_panelclk"),
307 PINCTRL_PIN(TZ1090_PIN_TFT_BLANK_LS, "tft_blank_ls"),
308 PINCTRL_PIN(TZ1090_PIN_TFT_VSYNC_NS, "tft_vsync_ns"),
309 PINCTRL_PIN(TZ1090_PIN_TFT_HSYNC_NR, "tft_hsync_nr"),
310 PINCTRL_PIN(TZ1090_PIN_TFT_VD12ACB, "tft_vd12acb"),
311 PINCTRL_PIN(TZ1090_PIN_TFT_PWRSAVE, "tft_pwrsave"),
312 PINCTRL_PIN(TZ1090_PIN_TX_ON, "tx_on"),
313 PINCTRL_PIN(TZ1090_PIN_RX_ON, "rx_on"),
314 PINCTRL_PIN(TZ1090_PIN_PLL_ON, "pll_on"),
315 PINCTRL_PIN(TZ1090_PIN_PA_ON, "pa_on"),
316 PINCTRL_PIN(TZ1090_PIN_RX_HP, "rx_hp"),
317 PINCTRL_PIN(TZ1090_PIN_GAIN0, "gain0"),
318 PINCTRL_PIN(TZ1090_PIN_GAIN1, "gain1"),
319 PINCTRL_PIN(TZ1090_PIN_GAIN2, "gain2"),
320 PINCTRL_PIN(TZ1090_PIN_GAIN3, "gain3"),
321 PINCTRL_PIN(TZ1090_PIN_GAIN4, "gain4"),
322 PINCTRL_PIN(TZ1090_PIN_GAIN5, "gain5"),
323 PINCTRL_PIN(TZ1090_PIN_GAIN6, "gain6"),
324 PINCTRL_PIN(TZ1090_PIN_GAIN7, "gain7"),
325 PINCTRL_PIN(TZ1090_PIN_ANT_SEL0, "ant_sel0"),
326 PINCTRL_PIN(TZ1090_PIN_ANT_SEL1, "ant_sel1"),
327 PINCTRL_PIN(TZ1090_PIN_SDH_CLK_IN, "sdh_clk_in"),
329 /* Non-GPIO pins */
330 PINCTRL_PIN(TZ1090_PIN_TCK, "tck"),
331 PINCTRL_PIN(TZ1090_PIN_TRST, "trst"),
332 PINCTRL_PIN(TZ1090_PIN_TDI, "tdi"),
333 PINCTRL_PIN(TZ1090_PIN_TDO, "tdo"),
334 PINCTRL_PIN(TZ1090_PIN_TMS, "tms"),
335 PINCTRL_PIN(TZ1090_PIN_CLK_OUT0, "clk_out0"),
336 PINCTRL_PIN(TZ1090_PIN_CLK_OUT1, "clk_out1"),
339 /* Pins in each pin group */
341 static const unsigned int spi1_cs2_pins[] = {
342 TZ1090_PIN_SPI1_CS2,
345 static const unsigned int pdm_d_pins[] = {
346 TZ1090_PIN_PDM_D,
349 static const unsigned int tft_pins[] = {
350 TZ1090_PIN_TFT_RED0,
351 TZ1090_PIN_TFT_RED1,
352 TZ1090_PIN_TFT_RED2,
353 TZ1090_PIN_TFT_RED3,
354 TZ1090_PIN_TFT_RED4,
355 TZ1090_PIN_TFT_RED5,
356 TZ1090_PIN_TFT_RED6,
357 TZ1090_PIN_TFT_RED7,
358 TZ1090_PIN_TFT_GREEN0,
359 TZ1090_PIN_TFT_GREEN1,
360 TZ1090_PIN_TFT_GREEN2,
361 TZ1090_PIN_TFT_GREEN3,
362 TZ1090_PIN_TFT_GREEN4,
363 TZ1090_PIN_TFT_GREEN5,
364 TZ1090_PIN_TFT_GREEN6,
365 TZ1090_PIN_TFT_GREEN7,
366 TZ1090_PIN_TFT_BLUE0,
367 TZ1090_PIN_TFT_BLUE1,
368 TZ1090_PIN_TFT_BLUE2,
369 TZ1090_PIN_TFT_BLUE3,
370 TZ1090_PIN_TFT_BLUE4,
371 TZ1090_PIN_TFT_BLUE5,
372 TZ1090_PIN_TFT_BLUE6,
373 TZ1090_PIN_TFT_BLUE7,
374 TZ1090_PIN_TFT_VDDEN_GD,
375 TZ1090_PIN_TFT_PANELCLK,
376 TZ1090_PIN_TFT_BLANK_LS,
377 TZ1090_PIN_TFT_VSYNC_NS,
378 TZ1090_PIN_TFT_HSYNC_NR,
379 TZ1090_PIN_TFT_VD12ACB,
380 TZ1090_PIN_TFT_PWRSAVE,
383 static const unsigned int afe_pins[] = {
384 TZ1090_PIN_TX_ON,
385 TZ1090_PIN_RX_ON,
386 TZ1090_PIN_PLL_ON,
387 TZ1090_PIN_PA_ON,
388 TZ1090_PIN_RX_HP,
389 TZ1090_PIN_ANT_SEL0,
390 TZ1090_PIN_ANT_SEL1,
391 TZ1090_PIN_GAIN0,
392 TZ1090_PIN_GAIN1,
393 TZ1090_PIN_GAIN2,
394 TZ1090_PIN_GAIN3,
395 TZ1090_PIN_GAIN4,
396 TZ1090_PIN_GAIN5,
397 TZ1090_PIN_GAIN6,
398 TZ1090_PIN_GAIN7,
401 static const unsigned int sdio_pins[] = {
402 TZ1090_PIN_SDIO_CLK,
403 TZ1090_PIN_SDIO_CMD,
404 TZ1090_PIN_SDIO_D0,
405 TZ1090_PIN_SDIO_D1,
406 TZ1090_PIN_SDIO_D2,
407 TZ1090_PIN_SDIO_D3,
410 static const unsigned int sdh_pins[] = {
411 TZ1090_PIN_SDH_CD,
412 TZ1090_PIN_SDH_WP,
413 TZ1090_PIN_SDH_CLK_IN,
416 static const unsigned int spi0_pins[] = {
417 TZ1090_PIN_SPI0_MCLK,
418 TZ1090_PIN_SPI0_CS0,
419 TZ1090_PIN_SPI0_CS1,
420 TZ1090_PIN_SPI0_CS2,
421 TZ1090_PIN_SPI0_DOUT,
422 TZ1090_PIN_SPI0_DIN,
425 static const unsigned int spi1_pins[] = {
426 TZ1090_PIN_SPI1_MCLK,
427 TZ1090_PIN_SPI1_CS0,
428 TZ1090_PIN_SPI1_CS1,
429 TZ1090_PIN_SPI1_CS2,
430 TZ1090_PIN_SPI1_DOUT,
431 TZ1090_PIN_SPI1_DIN,
434 static const unsigned int uart0_pins[] = {
435 TZ1090_PIN_UART0_RTS,
436 TZ1090_PIN_UART0_CTS,
437 TZ1090_PIN_UART0_TXD,
438 TZ1090_PIN_UART0_RXD,
441 static const unsigned int uart1_pins[] = {
442 TZ1090_PIN_UART1_TXD,
443 TZ1090_PIN_UART1_RXD,
446 static const unsigned int uart_pins[] = {
447 TZ1090_PIN_UART1_TXD,
448 TZ1090_PIN_UART1_RXD,
449 TZ1090_PIN_UART0_RTS,
450 TZ1090_PIN_UART0_CTS,
451 TZ1090_PIN_UART0_TXD,
452 TZ1090_PIN_UART0_RXD,
455 static const unsigned int scb0_pins[] = {
456 TZ1090_PIN_SCB0_SDAT,
457 TZ1090_PIN_SCB0_SCLK,
460 static const unsigned int scb1_pins[] = {
461 TZ1090_PIN_SCB1_SDAT,
462 TZ1090_PIN_SCB1_SCLK,
465 static const unsigned int scb2_pins[] = {
466 TZ1090_PIN_SCB2_SDAT,
467 TZ1090_PIN_SCB2_SCLK,
470 static const unsigned int i2s_pins[] = {
471 TZ1090_PIN_I2S_MCLK,
472 TZ1090_PIN_I2S_BCLK_OUT,
473 TZ1090_PIN_I2S_LRCLK_OUT,
474 TZ1090_PIN_I2S_DOUT0,
475 TZ1090_PIN_I2S_DOUT1,
476 TZ1090_PIN_I2S_DOUT2,
477 TZ1090_PIN_I2S_DIN,
480 static const unsigned int jtag_pins[] = {
481 TZ1090_PIN_TCK,
482 TZ1090_PIN_TRST,
483 TZ1090_PIN_TDI,
484 TZ1090_PIN_TDO,
485 TZ1090_PIN_TMS,
488 /* Pins in each drive pin group */
490 static const unsigned int drive_sdio_pins[] = {
491 TZ1090_PIN_SDIO_CLK,
492 TZ1090_PIN_SDIO_CMD,
493 TZ1090_PIN_SDIO_D0,
494 TZ1090_PIN_SDIO_D1,
495 TZ1090_PIN_SDIO_D2,
496 TZ1090_PIN_SDIO_D3,
497 TZ1090_PIN_SDH_WP,
498 TZ1090_PIN_SDH_CD,
499 TZ1090_PIN_SDH_CLK_IN,
502 static const unsigned int drive_i2s_pins[] = {
503 TZ1090_PIN_CLK_OUT1,
504 TZ1090_PIN_I2S_DIN,
505 TZ1090_PIN_I2S_DOUT0,
506 TZ1090_PIN_I2S_DOUT1,
507 TZ1090_PIN_I2S_DOUT2,
508 TZ1090_PIN_I2S_LRCLK_OUT,
509 TZ1090_PIN_I2S_BCLK_OUT,
510 TZ1090_PIN_I2S_MCLK,
513 static const unsigned int drive_scb0_pins[] = {
514 TZ1090_PIN_SCB0_SCLK,
515 TZ1090_PIN_SCB0_SDAT,
516 TZ1090_PIN_PDM_D,
517 TZ1090_PIN_PDM_C,
520 static const unsigned int drive_pdm_pins[] = {
521 TZ1090_PIN_CLK_OUT0,
522 TZ1090_PIN_PDM_B,
523 TZ1090_PIN_PDM_A,
526 /* Pin groups each function can be muxed to */
529 * The magic "perip" function allows otherwise non-muxing pins to be enabled in
530 * peripheral mode.
532 static const char * const perip_groups[] = {
533 /* non-muxing convenient gpio pingroups */
534 "uart",
535 "uart0",
536 "uart1",
537 "spi0",
538 "spi1",
539 "scb0",
540 "scb1",
541 "scb2",
542 "i2s",
543 /* individual pins not part of a pin mux group */
544 "spi0_mclk",
545 "spi0_cs0",
546 "spi0_cs1",
547 "spi0_cs2",
548 "spi0_dout",
549 "spi0_din",
550 "spi1_mclk",
551 "spi1_cs0",
552 "spi1_cs1",
553 "spi1_dout",
554 "spi1_din",
555 "uart0_rxd",
556 "uart0_txd",
557 "uart0_cts",
558 "uart0_rts",
559 "uart1_rxd",
560 "uart1_txd",
561 "scb0_sdat",
562 "scb0_sclk",
563 "scb1_sdat",
564 "scb1_sclk",
565 "scb2_sdat",
566 "scb2_sclk",
567 "i2s_mclk",
568 "i2s_bclk_out",
569 "i2s_lrclk_out",
570 "i2s_dout0",
571 "i2s_dout1",
572 "i2s_dout2",
573 "i2s_din",
574 "pdm_a",
575 "pdm_b",
576 "pdm_c",
579 static const char * const sdh_sdio_groups[] = {
580 "sdh",
581 "sdio",
582 /* sdh pins */
583 "sdh_cd",
584 "sdh_wp",
585 "sdh_clk_in",
586 /* sdio pins */
587 "sdio_clk",
588 "sdio_cmd",
589 "sdio_d0",
590 "sdio_d1",
591 "sdio_d2",
592 "sdio_d3",
595 static const char * const spi1_cs2_groups[] = {
596 "spi1_cs2",
599 static const char * const pdm_dac_groups[] = {
600 "pdm_d",
603 static const char * const usb_vbus_groups[] = {
604 "spi1_cs2",
605 "pdm_d",
608 static const char * const afe_groups[] = {
609 "afe",
610 /* afe pins */
611 "tx_on",
612 "rx_on",
613 "pll_on",
614 "pa_on",
615 "rx_hp",
616 "ant_sel0",
617 "ant_sel1",
618 "gain0",
619 "gain1",
620 "gain2",
621 "gain3",
622 "gain4",
623 "gain5",
624 "gain6",
625 "gain7",
628 static const char * const tft_groups[] = {
629 "tft",
630 /* tft pins */
631 "tft_red0",
632 "tft_red1",
633 "tft_red2",
634 "tft_red3",
635 "tft_red4",
636 "tft_red5",
637 "tft_red6",
638 "tft_red7",
639 "tft_green0",
640 "tft_green1",
641 "tft_green2",
642 "tft_green3",
643 "tft_green4",
644 "tft_green5",
645 "tft_green6",
646 "tft_green7",
647 "tft_blue0",
648 "tft_blue1",
649 "tft_blue2",
650 "tft_blue3",
651 "tft_blue4",
652 "tft_blue5",
653 "tft_blue6",
654 "tft_blue7",
655 "tft_vdden_gd",
656 "tft_panelclk",
657 "tft_blank_ls",
658 "tft_vsync_ns",
659 "tft_hsync_nr",
660 "tft_vd12acb",
661 "tft_pwrsave",
664 /* Mux functions that can be used by a mux */
666 enum tz1090_mux {
667 /* internal placeholder */
668 TZ1090_MUX_NA = -1,
669 /* magic per-non-muxing-GPIO-pin peripheral mode mux */
670 TZ1090_MUX_PERIP,
671 /* SDH/SDIO mux */
672 TZ1090_MUX_SDH,
673 TZ1090_MUX_SDIO,
674 /* USB_VBUS muxes */
675 TZ1090_MUX_SPI1_CS2,
676 TZ1090_MUX_PDM_DAC,
677 TZ1090_MUX_USB_VBUS,
678 /* AFE mux */
679 TZ1090_MUX_AFE,
680 TZ1090_MUX_TS_OUT_0,
681 /* EXT_DAC mux */
682 TZ1090_MUX_DAC,
683 TZ1090_MUX_NOT_IQADC_STB,
684 TZ1090_MUX_IQDAC_STB,
685 /* TFT mux */
686 TZ1090_MUX_TFT,
687 TZ1090_MUX_EXT_DAC,
688 TZ1090_MUX_TS_OUT_1,
689 TZ1090_MUX_LCD_TRACE,
690 TZ1090_MUX_PHY_RINGOSC,
693 #define FUNCTION(mux, fname, group) \
694 [(TZ1090_MUX_ ## mux)] = { \
695 .name = #fname, \
696 .groups = group##_groups, \
697 .ngroups = ARRAY_SIZE(group##_groups), \
699 /* For intermediate functions with submuxes */
700 #define NULL_FUNCTION(mux, fname) \
701 [(TZ1090_MUX_ ## mux)] = { \
702 .name = #fname, \
705 /* Must correlate with enum tz1090_mux */
706 static const struct tz1090_function tz1090_functions[] = {
707 /* FUNCTION function name pingroups */
708 FUNCTION(PERIP, perip, perip),
709 FUNCTION(SDH, sdh, sdh_sdio),
710 FUNCTION(SDIO, sdio, sdh_sdio),
711 FUNCTION(SPI1_CS2, spi1_cs2, spi1_cs2),
712 FUNCTION(PDM_DAC, pdm_dac, pdm_dac),
713 FUNCTION(USB_VBUS, usb_vbus, usb_vbus),
714 FUNCTION(AFE, afe, afe),
715 FUNCTION(TS_OUT_0, ts_out_0, afe),
716 FUNCTION(DAC, ext_dac, tft),
717 FUNCTION(NOT_IQADC_STB, not_iqadc_stb, tft),
718 FUNCTION(IQDAC_STB, iqdac_stb, tft),
719 FUNCTION(TFT, tft, tft),
720 NULL_FUNCTION(EXT_DAC, _ext_dac),
721 FUNCTION(TS_OUT_1, ts_out_1, tft),
722 FUNCTION(LCD_TRACE, lcd_trace, tft),
723 FUNCTION(PHY_RINGOSC, phy_ringosc, tft),
726 /* Sub muxes */
729 * MUX() - Initialise a mux description.
730 * @f0: Function 0 (TZ1090_MUX_ is prepended, NA for none)
731 * @f1: Function 1 (TZ1090_MUX_ is prepended, NA for none)
732 * @f2: Function 2 (TZ1090_MUX_ is prepended, NA for none)
733 * @f3: Function 3 (TZ1090_MUX_ is prepended, NA for none)
734 * @f4: Function 4 (TZ1090_MUX_ is prepended, NA for none)
735 * @mux_r: Mux register (REG_PINCTRL_ is prepended)
736 * @mux_b: Bit number in register that the mux field begins
737 * @mux_w: Width of mux field in register
739 #define MUX(f0, f1, f2, f3, f4, mux_r, mux_b, mux_w) \
741 .funcs = { \
742 TZ1090_MUX_ ## f0, \
743 TZ1090_MUX_ ## f1, \
744 TZ1090_MUX_ ## f2, \
745 TZ1090_MUX_ ## f3, \
746 TZ1090_MUX_ ## f4, \
747 }, \
748 .reg = (REG_PINCTRL_ ## mux_r), \
749 .bit = (mux_b), \
750 .width = (mux_w), \
754 * DEFINE_SUBMUX() - Defines a submux description separate from a pin group.
755 * @mux: Mux name (_submux is appended)
756 * @f0: Function 0 (TZ1090_MUX_ is prepended, NA for none)
757 * @f1: Function 1 (TZ1090_MUX_ is prepended, NA for none)
758 * @f2: Function 2 (TZ1090_MUX_ is prepended, NA for none)
759 * @f3: Function 3 (TZ1090_MUX_ is prepended, NA for none)
760 * @f4: Function 4 (TZ1090_MUX_ is prepended, NA for none)
761 * @mux_r: Mux register (REG_PINCTRL_ is prepended)
762 * @mux_b: Bit number in register that the mux field begins
763 * @mux_w: Width of mux field in register
765 * A sub mux is a nested mux that can be bound to a magic function number used
766 * by another mux description. For example value 4 of the top level mux might
767 * correspond to a function which has a submux pointed to in tz1090_submux[].
768 * The outer mux can then take on any function in the top level mux or the
769 * submux, and if a submux function is chosen both muxes are updated to route
770 * the signal from the submux.
772 * The submux can be defined with DEFINE_SUBMUX and pointed to from
773 * tz1090_submux[] using SUBMUX.
775 #define DEFINE_SUBMUX(mux, f0, f1, f2, f3, f4, mux_r, mux_b, mux_w) \
776 static struct tz1090_muxdesc mux ## _submux = \
777 MUX(f0, f1, f2, f3, f4, mux_r, mux_b, mux_w)
780 * SUBMUX() - Link a submux to a function number.
781 * @f: Function name (TZ1090_MUX_ is prepended)
782 * @submux: Submux name (_submux is appended)
784 * For use in tz1090_submux[] initialisation to link an intermediate function
785 * number to a particular submux description. It indicates that when the
786 * function is chosen the signal is connected to the submux.
788 #define SUBMUX(f, submux) [(TZ1090_MUX_ ## f)] = &(submux ## _submux)
791 * MUX_PG() - Initialise a pin group with mux control
792 * @pg_name: Pin group name (stringified, _pins appended to get pins array)
793 * @f0: Function 0 (TZ1090_MUX_ is prepended, NA for none)
794 * @f1: Function 1 (TZ1090_MUX_ is prepended, NA for none)
795 * @f2: Function 2 (TZ1090_MUX_ is prepended, NA for none)
796 * @f3: Function 3 (TZ1090_MUX_ is prepended, NA for none)
797 * @f4: Function 4 (TZ1090_MUX_ is prepended, NA for none)
798 * @mux_r: Mux register (REG_PINCTRL_ is prepended)
799 * @mux_b: Bit number in register that the mux field begins
800 * @mux_w: Width of mux field in register
802 #define MUX_PG(pg_name, f0, f1, f2, f3, f4, \
803 mux_r, mux_b, mux_w) \
805 .name = #pg_name, \
806 .pins = pg_name##_pins, \
807 .npins = ARRAY_SIZE(pg_name##_pins), \
808 .mux = MUX(f0, f1, f2, f3, f4, \
809 mux_r, mux_b, mux_w), \
813 * SIMPLE_PG() - Initialise a simple convenience pin group
814 * @pg_name: Pin group name (stringified, _pins appended to get pins array)
816 * A simple pin group is simply used for binding pins together so they can be
817 * referred to by a single name instead of having to list every pin
818 * individually.
820 #define SIMPLE_PG(pg_name) \
822 .name = #pg_name, \
823 .pins = pg_name##_pins, \
824 .npins = ARRAY_SIZE(pg_name##_pins), \
828 * DRV_PG() - Initialise a pin group with drive control
829 * @pg_name: Pin group name (stringified, _pins appended to get pins array)
830 * @slw_b: Slew register bit.
831 * The same bit is used for Schmitt, and Drive (*2).
833 #define DRV_PG(pg_name, slw_b) \
835 .name = #pg_name, \
836 .pins = pg_name##_pins, \
837 .npins = ARRAY_SIZE(pg_name##_pins), \
838 .drv = true, \
839 .slw_bit = (slw_b), \
843 * Define main muxing pin groups
846 /* submuxes */
848 /* name f0, f1, f2, f3, f4, mux r/b/w */
849 DEFINE_SUBMUX(ext_dac, DAC, NOT_IQADC_STB, IQDAC_STB, NA, NA, IF_CTL, 6, 2);
851 /* bind submuxes to internal functions */
852 static struct tz1090_muxdesc *tz1090_submux[] = {
853 SUBMUX(EXT_DAC, ext_dac),
857 * These are the pin mux groups. Pin muxing can be enabled and disabled for each
858 * pin individually so these groups are internal. The mapping of pins to pin mux
859 * group is below (tz1090_mux_pins).
861 static struct tz1090_pingroup tz1090_mux_groups[] = {
862 /* Muxing pin groups */
863 /* pg_name, f0, f1, f2, f3, f4, mux r/b/w */
864 MUX_PG(sdh, SDH, SDIO, NA, NA, NA, IF_CTL, 20, 2),
865 MUX_PG(sdio, SDIO, SDH, NA, NA, NA, IF_CTL, 16, 2),
866 MUX_PG(spi1_cs2, SPI1_CS2, USB_VBUS, NA, NA, NA, IF_CTL, 10, 2),
867 MUX_PG(pdm_d, PDM_DAC, USB_VBUS, NA, NA, NA, IF_CTL, 8, 2),
868 MUX_PG(afe, AFE, TS_OUT_0, NA, NA, NA, IF_CTL, 4, 2),
869 MUX_PG(tft, TFT, EXT_DAC, TS_OUT_1, LCD_TRACE, PHY_RINGOSC, IF_CTL, 0, 3),
873 * This is the mapping from GPIO pins to pin mux groups in tz1090_mux_groups[].
874 * Pins which aren't muxable to multiple peripherals are set to
875 * TZ1090_MUX_GROUP_MAX to enable the "perip" function to enable/disable
876 * peripheral control of the pin.
878 * This array is initialised in tz1090_init_mux_pins().
880 static u8 tz1090_mux_pins[NUM_GPIOS];
882 /* TZ1090_MUX_GROUP_MAX is used in tz1090_mux_pins[] for non-muxing pins */
883 #define TZ1090_MUX_GROUP_MAX ARRAY_SIZE(tz1090_mux_groups)
886 * tz1090_init_mux_pins() - Initialise GPIO pin to mux group mapping.
888 * Initialises the tz1090_mux_pins[] array to be the inverse of the pin lists in
889 * each pin mux group in tz1090_mux_groups[].
891 * It is assumed that no pin mux groups overlap (share pins).
893 static void __init tz1090_init_mux_pins(void)
895 unsigned int g, p;
896 const struct tz1090_pingroup *grp;
897 const unsigned int *pin;
899 for (p = 0; p < NUM_GPIOS; ++p)
900 tz1090_mux_pins[p] = TZ1090_MUX_GROUP_MAX;
902 grp = tz1090_mux_groups;
903 for (g = 0, grp = tz1090_mux_groups;
904 g < ARRAY_SIZE(tz1090_mux_groups); ++g, ++grp)
905 for (pin = grp->pins, p = 0; p < grp->npins; ++p, ++pin)
906 tz1090_mux_pins[*pin] = g;
910 * These are the externally visible pin groups. Some of them allow group control
911 * of drive configuration. Some are just simple convenience pingroups. All the
912 * internal pin mux groups in tz1090_mux_groups[] are mirrored here with the
913 * same pins.
914 * Pseudo pin groups follow in the group numbers after this array for each GPIO
915 * pin. Any group used for muxing must have all pins belonging to the same pin
916 * mux group.
918 static struct tz1090_pingroup tz1090_groups[] = {
919 /* Pin groups with drive control (with no out of place pins) */
920 /* pg_name, slw/schmitt/drv b */
921 DRV_PG(jtag, 11 /* 11, 22 */),
922 DRV_PG(tft, 10 /* 10, 20 */),
923 DRV_PG(scb2, 9 /* 9, 18 */),
924 DRV_PG(spi0, 7 /* 7, 14 */),
925 DRV_PG(uart, 5 /* 5, 10 */),
926 DRV_PG(scb1, 4 /* 4, 8 */),
927 DRV_PG(spi1, 3 /* 3, 6 */),
928 DRV_PG(afe, 0 /* 0, 0 */),
931 * Drive specific pin groups (with odd combinations of pins which makes
932 * the pin group naming somewhat arbitrary)
934 /* pg_name, slw/schmitt/drv b */
935 DRV_PG(drive_sdio, 8 /* 8, 16 */), /* sdio_* + sdh_* */
936 DRV_PG(drive_i2s, 6 /* 6, 12 */), /* i2s_* + clk_out1 */
937 DRV_PG(drive_scb0, 2 /* 2, 4 */), /* scb0_* + pdm_{c,d} */
938 DRV_PG(drive_pdm, 1 /* 1, 2 */), /* pdm_{a,b} + clk_out0 */
940 /* Convenience pin groups */
941 /* pg_name */
942 SIMPLE_PG(uart0),
943 SIMPLE_PG(uart1),
944 SIMPLE_PG(scb0),
945 SIMPLE_PG(i2s),
946 SIMPLE_PG(sdh),
947 SIMPLE_PG(sdio),
949 /* pseudo-pingroups for each GPIO pin follow */
953 * struct tz1090_pmx - Private pinctrl data
954 * @dev: Platform device
955 * @pctl: Pin control device
956 * @regs: Register region
957 * @lock: Lock protecting coherency of pin_en, gpio_en, and SELECT regs
958 * @pin_en: Pins that have been enabled (32 pins packed into each element)
959 * @gpio_en: GPIOs that have been enabled (32 pins packed into each element)
961 struct tz1090_pmx {
962 struct device *dev;
963 struct pinctrl_dev *pctl;
964 void __iomem *regs;
965 spinlock_t lock;
966 u32 pin_en[3];
967 u32 gpio_en[3];
970 static inline u32 pmx_read(struct tz1090_pmx *pmx, u32 reg)
972 return ioread32(pmx->regs + reg);
975 static inline void pmx_write(struct tz1090_pmx *pmx, u32 val, u32 reg)
977 iowrite32(val, pmx->regs + reg);
981 * Pin control operations
984 /* each GPIO pin has it's own pseudo pingroup containing only itself */
986 static int tz1090_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
988 return ARRAY_SIZE(tz1090_groups) + NUM_GPIOS;
991 static const char *tz1090_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
992 unsigned int group)
994 if (group < ARRAY_SIZE(tz1090_groups)) {
995 /* normal pingroup */
996 return tz1090_groups[group].name;
997 } else {
998 /* individual gpio pin pseudo-pingroup */
999 unsigned int pin = group - ARRAY_SIZE(tz1090_groups);
1000 return tz1090_pins[pin].name;
1004 static int tz1090_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
1005 unsigned int group,
1006 const unsigned int **pins,
1007 unsigned int *num_pins)
1009 if (group < ARRAY_SIZE(tz1090_groups)) {
1010 /* normal pingroup */
1011 *pins = tz1090_groups[group].pins;
1012 *num_pins = tz1090_groups[group].npins;
1013 } else {
1014 /* individual gpio pin pseudo-pingroup */
1015 unsigned int pin = group - ARRAY_SIZE(tz1090_groups);
1016 *pins = &tz1090_pins[pin].number;
1017 *num_pins = 1;
1020 return 0;
1023 #ifdef CONFIG_DEBUG_FS
1024 static void tz1090_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
1025 struct seq_file *s,
1026 unsigned int offset)
1028 seq_printf(s, " %s", dev_name(pctldev->dev));
1030 #endif
1032 static int reserve_map(struct device *dev, struct pinctrl_map **map,
1033 unsigned int *reserved_maps, unsigned int *num_maps,
1034 unsigned int reserve)
1036 unsigned int old_num = *reserved_maps;
1037 unsigned int new_num = *num_maps + reserve;
1038 struct pinctrl_map *new_map;
1040 if (old_num >= new_num)
1041 return 0;
1043 new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
1044 if (!new_map) {
1045 dev_err(dev, "krealloc(map) failed\n");
1046 return -ENOMEM;
1049 memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
1051 *map = new_map;
1052 *reserved_maps = new_num;
1054 return 0;
1057 static int add_map_mux(struct pinctrl_map **map, unsigned int *reserved_maps,
1058 unsigned int *num_maps, const char *group,
1059 const char *function)
1061 if (WARN_ON(*num_maps == *reserved_maps))
1062 return -ENOSPC;
1064 (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
1065 (*map)[*num_maps].data.mux.group = group;
1066 (*map)[*num_maps].data.mux.function = function;
1067 (*num_maps)++;
1069 return 0;
1072 static int add_map_configs(struct device *dev,
1073 struct pinctrl_map **map,
1074 unsigned int *reserved_maps, unsigned int *num_maps,
1075 const char *group, unsigned long *configs,
1076 unsigned int num_configs)
1078 unsigned long *dup_configs;
1080 if (WARN_ON(*num_maps == *reserved_maps))
1081 return -ENOSPC;
1083 dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
1084 GFP_KERNEL);
1085 if (!dup_configs)
1086 return -ENOMEM;
1088 (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
1089 (*map)[*num_maps].data.configs.group_or_pin = group;
1090 (*map)[*num_maps].data.configs.configs = dup_configs;
1091 (*map)[*num_maps].data.configs.num_configs = num_configs;
1092 (*num_maps)++;
1094 return 0;
1097 static void tz1090_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
1098 struct pinctrl_map *map,
1099 unsigned int num_maps)
1101 int i;
1103 for (i = 0; i < num_maps; i++)
1104 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
1105 kfree(map[i].data.configs.configs);
1107 kfree(map);
1110 static int tz1090_pinctrl_dt_subnode_to_map(struct device *dev,
1111 struct device_node *np,
1112 struct pinctrl_map **map,
1113 unsigned int *reserved_maps,
1114 unsigned int *num_maps)
1116 int ret;
1117 const char *function;
1118 unsigned long *configs = NULL;
1119 unsigned int num_configs = 0;
1120 unsigned int reserve;
1121 struct property *prop;
1122 const char *group;
1124 ret = of_property_read_string(np, "tz1090,function", &function);
1125 if (ret < 0) {
1126 /* EINVAL=missing, which is fine since it's optional */
1127 if (ret != -EINVAL)
1128 dev_err(dev, "could not parse property function\n");
1129 function = NULL;
1132 ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
1133 if (ret)
1134 return ret;
1136 reserve = 0;
1137 if (function != NULL)
1138 reserve++;
1139 if (num_configs)
1140 reserve++;
1141 ret = of_property_count_strings(np, "tz1090,pins");
1142 if (ret < 0) {
1143 dev_err(dev, "could not parse property pins\n");
1144 goto exit;
1146 reserve *= ret;
1148 ret = reserve_map(dev, map, reserved_maps, num_maps, reserve);
1149 if (ret < 0)
1150 goto exit;
1152 of_property_for_each_string(np, "tz1090,pins", prop, group) {
1153 if (function) {
1154 ret = add_map_mux(map, reserved_maps, num_maps,
1155 group, function);
1156 if (ret < 0)
1157 goto exit;
1160 if (num_configs) {
1161 ret = add_map_configs(dev, map, reserved_maps,
1162 num_maps, group, configs,
1163 num_configs);
1164 if (ret < 0)
1165 goto exit;
1169 ret = 0;
1171 exit:
1172 kfree(configs);
1173 return ret;
1176 static int tz1090_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
1177 struct device_node *np_config,
1178 struct pinctrl_map **map,
1179 unsigned int *num_maps)
1181 unsigned int reserved_maps;
1182 struct device_node *np;
1183 int ret;
1185 reserved_maps = 0;
1186 *map = NULL;
1187 *num_maps = 0;
1189 for_each_child_of_node(np_config, np) {
1190 ret = tz1090_pinctrl_dt_subnode_to_map(pctldev->dev, np, map,
1191 &reserved_maps,
1192 num_maps);
1193 if (ret < 0) {
1194 tz1090_pinctrl_dt_free_map(pctldev, *map, *num_maps);
1195 return ret;
1199 return 0;
1202 static const struct pinctrl_ops tz1090_pinctrl_ops = {
1203 .get_groups_count = tz1090_pinctrl_get_groups_count,
1204 .get_group_name = tz1090_pinctrl_get_group_name,
1205 .get_group_pins = tz1090_pinctrl_get_group_pins,
1206 #ifdef CONFIG_DEBUG_FS
1207 .pin_dbg_show = tz1090_pinctrl_pin_dbg_show,
1208 #endif
1209 .dt_node_to_map = tz1090_pinctrl_dt_node_to_map,
1210 .dt_free_map = tz1090_pinctrl_dt_free_map,
1214 * Pin mux operations
1217 static int tz1090_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
1219 return ARRAY_SIZE(tz1090_functions);
1222 static const char *tz1090_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
1223 unsigned int function)
1225 return tz1090_functions[function].name;
1228 static int tz1090_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
1229 unsigned int function,
1230 const char * const **groups,
1231 unsigned int * const num_groups)
1233 /* pingroup functions */
1234 *groups = tz1090_functions[function].groups;
1235 *num_groups = tz1090_functions[function].ngroups;
1236 return 0;
1240 * tz1090_pinctrl_select() - update bit in SELECT register
1241 * @pmx: Pinmux data
1242 * @pin: Pin number (must be within GPIO range)
1244 static void tz1090_pinctrl_select(struct tz1090_pmx *pmx,
1245 unsigned int pin)
1247 u32 reg, reg_shift, select, val;
1248 unsigned int pmx_index, pmx_shift;
1249 unsigned long flags;
1251 /* uses base 32 instead of base 30 */
1252 pmx_index = pin >> 5;
1253 pmx_shift = pin & 0x1f;
1255 /* select = !perip || gpio */
1256 select = ((~pmx->pin_en[pmx_index] |
1257 pmx->gpio_en[pmx_index]) >> pmx_shift) & 1;
1259 /* find register and bit offset (base 30) */
1260 reg = REG_PINCTRL_SELECT + 4*(pin / 30);
1261 reg_shift = pin % 30;
1263 /* modify gpio select bit */
1264 __global_lock2(flags);
1265 val = pmx_read(pmx, reg);
1266 val &= ~BIT(reg_shift);
1267 val |= select << reg_shift;
1268 pmx_write(pmx, val, reg);
1269 __global_unlock2(flags);
1273 * tz1090_pinctrl_gpio_select() - enable/disable GPIO usage for a pin
1274 * @pmx: Pinmux data
1275 * @pin: Pin number
1276 * @gpio_select: true to enable pin as GPIO,
1277 * false to leave control to whatever function is enabled
1279 * Records that GPIO usage is enabled/disabled so that enabling a function
1280 * doesn't override the SELECT register bit.
1282 static void tz1090_pinctrl_gpio_select(struct tz1090_pmx *pmx,
1283 unsigned int pin,
1284 bool gpio_select)
1286 unsigned int index, shift;
1287 u32 gpio_en;
1289 if (pin >= NUM_GPIOS)
1290 return;
1292 /* uses base 32 instead of base 30 */
1293 index = pin >> 5;
1294 shift = pin & 0x1f;
1296 spin_lock(&pmx->lock);
1298 /* keep a record whether gpio is selected */
1299 gpio_en = pmx->gpio_en[index];
1300 gpio_en &= ~BIT(shift);
1301 if (gpio_select)
1302 gpio_en |= BIT(shift);
1303 pmx->gpio_en[index] = gpio_en;
1305 /* update the select bit */
1306 tz1090_pinctrl_select(pmx, pin);
1308 spin_unlock(&pmx->lock);
1312 * tz1090_pinctrl_perip_select() - enable/disable peripheral interface for a pin
1313 * @pmx: Pinmux data
1314 * @pin: Pin number
1315 * @perip_select: true to enable peripheral interface when not GPIO,
1316 * false to leave pin in GPIO mode
1318 * Records that peripheral usage is enabled/disabled so that SELECT register can
1319 * be set appropriately when GPIO is disabled.
1321 static void tz1090_pinctrl_perip_select(struct tz1090_pmx *pmx,
1322 unsigned int pin,
1323 bool perip_select)
1325 unsigned int index, shift;
1326 u32 pin_en;
1328 if (pin >= NUM_GPIOS)
1329 return;
1331 /* uses base 32 instead of base 30 */
1332 index = pin >> 5;
1333 shift = pin & 0x1f;
1335 spin_lock(&pmx->lock);
1337 /* keep a record whether peripheral is selected */
1338 pin_en = pmx->pin_en[index];
1339 pin_en &= ~BIT(shift);
1340 if (perip_select)
1341 pin_en |= BIT(shift);
1342 pmx->pin_en[index] = pin_en;
1344 /* update the select bit */
1345 tz1090_pinctrl_select(pmx, pin);
1347 spin_unlock(&pmx->lock);
1351 * tz1090_pinctrl_enable_mux() - Switch a pin mux group to a function.
1352 * @pmx: Pinmux data
1353 * @desc: Pinmux description
1354 * @function: Function to switch to
1356 * Enable a particular function on a pin mux group. Since pin mux descriptions
1357 * are nested this function is recursive.
1359 static int tz1090_pinctrl_enable_mux(struct tz1090_pmx *pmx,
1360 const struct tz1090_muxdesc *desc,
1361 unsigned int function)
1363 const int *fit;
1364 unsigned long flags;
1365 int mux;
1366 unsigned int func, ret;
1367 u32 reg, mask;
1369 /* find the mux value for this function, searching recursively */
1370 for (mux = 0, fit = desc->funcs;
1371 mux < ARRAY_SIZE(desc->funcs); ++mux, ++fit) {
1372 func = *fit;
1373 if (func == function)
1374 goto found_mux;
1376 /* maybe it's a sub-mux */
1377 if (func < ARRAY_SIZE(tz1090_submux) && tz1090_submux[func]) {
1378 ret = tz1090_pinctrl_enable_mux(pmx,
1379 tz1090_submux[func],
1380 function);
1381 if (!ret)
1382 goto found_mux;
1386 return -EINVAL;
1387 found_mux:
1389 /* Set up the mux */
1390 if (desc->width) {
1391 mask = (BIT(desc->width) - 1) << desc->bit;
1392 __global_lock2(flags);
1393 reg = pmx_read(pmx, desc->reg);
1394 reg &= ~mask;
1395 reg |= (mux << desc->bit) & mask;
1396 pmx_write(pmx, reg, desc->reg);
1397 __global_unlock2(flags);
1400 return 0;
1404 * tz1090_pinctrl_enable() - Enable a function on a pin group.
1405 * @pctldev: Pin control data
1406 * @function: Function index to enable
1407 * @group: Group index to enable
1409 * Enable a particular function on a group of pins. The per GPIO pin pseudo pin
1410 * groups can be used (in which case the pin will be enabled in peripheral mode
1411 * and if it belongs to a pin mux group the mux will be switched if it isn't
1412 * already in use. Some convenience pin groups can also be used in which case
1413 * the effect is the same as enabling the function on each individual pin in the
1414 * group.
1416 static int tz1090_pinctrl_set_mux(struct pinctrl_dev *pctldev,
1417 unsigned int function, unsigned int group)
1419 struct tz1090_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
1420 struct tz1090_pingroup *grp;
1421 int ret;
1422 unsigned int pin_num, mux_group, i, npins;
1423 const unsigned int *pins;
1425 /* group of pins? */
1426 if (group < ARRAY_SIZE(tz1090_groups)) {
1427 grp = &tz1090_groups[group];
1428 npins = grp->npins;
1429 pins = grp->pins;
1431 * All pins in the group must belong to the same mux group,
1432 * which allows us to just use the mux group of the first pin.
1433 * By explicitly listing permitted pingroups for each function
1434 * the pinmux core should ensure this is always the case.
1436 } else {
1437 pin_num = group - ARRAY_SIZE(tz1090_groups);
1438 npins = 1;
1439 pins = &pin_num;
1441 mux_group = tz1090_mux_pins[*pins];
1443 /* no mux group, but can still be individually muxed to peripheral */
1444 if (mux_group >= TZ1090_MUX_GROUP_MAX) {
1445 if (function == TZ1090_MUX_PERIP)
1446 goto mux_pins;
1447 return -EINVAL;
1450 /* mux group already set to a different function? */
1451 grp = &tz1090_mux_groups[mux_group];
1452 if (grp->func_count && grp->func != function) {
1453 dev_err(pctldev->dev,
1454 "%s: can't mux pin(s) to '%s', group already muxed to '%s'\n",
1455 __func__, tz1090_functions[function].name,
1456 tz1090_functions[grp->func].name);
1457 return -EBUSY;
1460 dev_dbg(pctldev->dev, "%s: muxing %u pin(s) in '%s' to '%s'\n",
1461 __func__, npins, grp->name, tz1090_functions[function].name);
1463 /* if first pin in mux group to be enabled, enable the group mux */
1464 if (!grp->func_count) {
1465 grp->func = function;
1466 ret = tz1090_pinctrl_enable_mux(pmx, &grp->mux, function);
1467 if (ret)
1468 return ret;
1470 /* add pins to ref count and mux individually to peripheral */
1471 grp->func_count += npins;
1472 mux_pins:
1473 for (i = 0; i < npins; ++i)
1474 tz1090_pinctrl_perip_select(pmx, pins[i], true);
1476 return 0;
1480 * tz1090_pinctrl_gpio_request_enable() - Put pin in GPIO mode.
1481 * @pctldev: Pin control data
1482 * @range: GPIO range
1483 * @pin: Pin number
1485 * Puts a particular pin into GPIO mode, disabling peripheral control until it's
1486 * disabled again.
1488 static int tz1090_pinctrl_gpio_request_enable(struct pinctrl_dev *pctldev,
1489 struct pinctrl_gpio_range *range,
1490 unsigned int pin)
1492 struct tz1090_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
1493 tz1090_pinctrl_gpio_select(pmx, pin, true);
1494 return 0;
1498 * tz1090_pinctrl_gpio_disable_free() - Take pin out of GPIO mode.
1499 * @pctldev: Pin control data
1500 * @range: GPIO range
1501 * @pin: Pin number
1503 * Take a particular pin out of GPIO mode. If the pin is enabled for a
1504 * peripheral it will return to peripheral mode.
1506 static void tz1090_pinctrl_gpio_disable_free(struct pinctrl_dev *pctldev,
1507 struct pinctrl_gpio_range *range,
1508 unsigned int pin)
1510 struct tz1090_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
1511 tz1090_pinctrl_gpio_select(pmx, pin, false);
1514 static const struct pinmux_ops tz1090_pinmux_ops = {
1515 .get_functions_count = tz1090_pinctrl_get_funcs_count,
1516 .get_function_name = tz1090_pinctrl_get_func_name,
1517 .get_function_groups = tz1090_pinctrl_get_func_groups,
1518 .set_mux = tz1090_pinctrl_set_mux,
1519 .gpio_request_enable = tz1090_pinctrl_gpio_request_enable,
1520 .gpio_disable_free = tz1090_pinctrl_gpio_disable_free,
1524 * Pin config operations
1527 struct tz1090_pinconf_pullup {
1528 unsigned char index;
1529 unsigned char shift;
1532 /* The mapping of pin to pull up/down register index and shift */
1533 static struct tz1090_pinconf_pullup tz1090_pinconf_pullup[] = {
1534 {5, 22}, /* 0 - TZ1090_PIN_SDIO_CLK */
1535 {0, 14}, /* 1 - TZ1090_PIN_SDIO_CMD */
1536 {0, 6}, /* 2 - TZ1090_PIN_SDIO_D0 */
1537 {0, 8}, /* 3 - TZ1090_PIN_SDIO_D1 */
1538 {0, 10}, /* 4 - TZ1090_PIN_SDIO_D2 */
1539 {0, 12}, /* 5 - TZ1090_PIN_SDIO_D3 */
1540 {0, 2}, /* 6 - TZ1090_PIN_SDH_CD */
1541 {0, 4}, /* 7 - TZ1090_PIN_SDH_WP */
1542 {0, 16}, /* 8 - TZ1090_PIN_SPI0_MCLK */
1543 {0, 18}, /* 9 - TZ1090_PIN_SPI0_CS0 */
1544 {0, 20}, /* 10 - TZ1090_PIN_SPI0_CS1 */
1545 {0, 22}, /* 11 - TZ1090_PIN_SPI0_CS2 */
1546 {0, 24}, /* 12 - TZ1090_PIN_SPI0_DOUT */
1547 {0, 26}, /* 13 - TZ1090_PIN_SPI0_DIN */
1548 {0, 28}, /* 14 - TZ1090_PIN_SPI1_MCLK */
1549 {0, 30}, /* 15 - TZ1090_PIN_SPI1_CS0 */
1550 {1, 0}, /* 16 - TZ1090_PIN_SPI1_CS1 */
1551 {1, 2}, /* 17 - TZ1090_PIN_SPI1_CS2 */
1552 {1, 4}, /* 18 - TZ1090_PIN_SPI1_DOUT */
1553 {1, 6}, /* 19 - TZ1090_PIN_SPI1_DIN */
1554 {1, 8}, /* 20 - TZ1090_PIN_UART0_RXD */
1555 {1, 10}, /* 21 - TZ1090_PIN_UART0_TXD */
1556 {1, 12}, /* 22 - TZ1090_PIN_UART0_CTS */
1557 {1, 14}, /* 23 - TZ1090_PIN_UART0_RTS */
1558 {1, 16}, /* 24 - TZ1090_PIN_UART1_RXD */
1559 {1, 18}, /* 25 - TZ1090_PIN_UART1_TXD */
1560 {1, 20}, /* 26 - TZ1090_PIN_SCB0_SDAT */
1561 {1, 22}, /* 27 - TZ1090_PIN_SCB0_SCLK */
1562 {1, 24}, /* 28 - TZ1090_PIN_SCB1_SDAT */
1563 {1, 26}, /* 29 - TZ1090_PIN_SCB1_SCLK */
1565 {1, 28}, /* 30 - TZ1090_PIN_SCB2_SDAT */
1566 {1, 30}, /* 31 - TZ1090_PIN_SCB2_SCLK */
1567 {2, 0}, /* 32 - TZ1090_PIN_I2S_MCLK */
1568 {2, 2}, /* 33 - TZ1090_PIN_I2S_BCLK_OUT */
1569 {2, 4}, /* 34 - TZ1090_PIN_I2S_LRCLK_OUT */
1570 {2, 6}, /* 35 - TZ1090_PIN_I2S_DOUT0 */
1571 {2, 8}, /* 36 - TZ1090_PIN_I2S_DOUT1 */
1572 {2, 10}, /* 37 - TZ1090_PIN_I2S_DOUT2 */
1573 {2, 12}, /* 38 - TZ1090_PIN_I2S_DIN */
1574 {4, 12}, /* 39 - TZ1090_PIN_PDM_A */
1575 {4, 14}, /* 40 - TZ1090_PIN_PDM_B */
1576 {4, 18}, /* 41 - TZ1090_PIN_PDM_C */
1577 {4, 20}, /* 42 - TZ1090_PIN_PDM_D */
1578 {2, 14}, /* 43 - TZ1090_PIN_TFT_RED0 */
1579 {2, 16}, /* 44 - TZ1090_PIN_TFT_RED1 */
1580 {2, 18}, /* 45 - TZ1090_PIN_TFT_RED2 */
1581 {2, 20}, /* 46 - TZ1090_PIN_TFT_RED3 */
1582 {2, 22}, /* 47 - TZ1090_PIN_TFT_RED4 */
1583 {2, 24}, /* 48 - TZ1090_PIN_TFT_RED5 */
1584 {2, 26}, /* 49 - TZ1090_PIN_TFT_RED6 */
1585 {2, 28}, /* 50 - TZ1090_PIN_TFT_RED7 */
1586 {2, 30}, /* 51 - TZ1090_PIN_TFT_GREEN0 */
1587 {3, 0}, /* 52 - TZ1090_PIN_TFT_GREEN1 */
1588 {3, 2}, /* 53 - TZ1090_PIN_TFT_GREEN2 */
1589 {3, 4}, /* 54 - TZ1090_PIN_TFT_GREEN3 */
1590 {3, 6}, /* 55 - TZ1090_PIN_TFT_GREEN4 */
1591 {3, 8}, /* 56 - TZ1090_PIN_TFT_GREEN5 */
1592 {3, 10}, /* 57 - TZ1090_PIN_TFT_GREEN6 */
1593 {3, 12}, /* 58 - TZ1090_PIN_TFT_GREEN7 */
1594 {3, 14}, /* 59 - TZ1090_PIN_TFT_BLUE0 */
1596 {3, 16}, /* 60 - TZ1090_PIN_TFT_BLUE1 */
1597 {3, 18}, /* 61 - TZ1090_PIN_TFT_BLUE2 */
1598 {3, 20}, /* 62 - TZ1090_PIN_TFT_BLUE3 */
1599 {3, 22}, /* 63 - TZ1090_PIN_TFT_BLUE4 */
1600 {3, 24}, /* 64 - TZ1090_PIN_TFT_BLUE5 */
1601 {3, 26}, /* 65 - TZ1090_PIN_TFT_BLUE6 */
1602 {3, 28}, /* 66 - TZ1090_PIN_TFT_BLUE7 */
1603 {3, 30}, /* 67 - TZ1090_PIN_TFT_VDDEN_GD */
1604 {4, 0}, /* 68 - TZ1090_PIN_TFT_PANELCLK */
1605 {4, 2}, /* 69 - TZ1090_PIN_TFT_BLANK_LS */
1606 {4, 4}, /* 70 - TZ1090_PIN_TFT_VSYNC_NS */
1607 {4, 6}, /* 71 - TZ1090_PIN_TFT_HSYNC_NR */
1608 {4, 8}, /* 72 - TZ1090_PIN_TFT_VD12ACB */
1609 {4, 10}, /* 73 - TZ1090_PIN_TFT_PWRSAVE */
1610 {4, 24}, /* 74 - TZ1090_PIN_TX_ON */
1611 {4, 26}, /* 75 - TZ1090_PIN_RX_ON */
1612 {4, 28}, /* 76 - TZ1090_PIN_PLL_ON */
1613 {4, 30}, /* 77 - TZ1090_PIN_PA_ON */
1614 {5, 0}, /* 78 - TZ1090_PIN_RX_HP */
1615 {5, 6}, /* 79 - TZ1090_PIN_GAIN0 */
1616 {5, 8}, /* 80 - TZ1090_PIN_GAIN1 */
1617 {5, 10}, /* 81 - TZ1090_PIN_GAIN2 */
1618 {5, 12}, /* 82 - TZ1090_PIN_GAIN3 */
1619 {5, 14}, /* 83 - TZ1090_PIN_GAIN4 */
1620 {5, 16}, /* 84 - TZ1090_PIN_GAIN5 */
1621 {5, 18}, /* 85 - TZ1090_PIN_GAIN6 */
1622 {5, 20}, /* 86 - TZ1090_PIN_GAIN7 */
1623 {5, 2}, /* 87 - TZ1090_PIN_ANT_SEL0 */
1624 {5, 4}, /* 88 - TZ1090_PIN_ANT_SEL1 */
1625 {0, 0}, /* 89 - TZ1090_PIN_SDH_CLK_IN */
1627 {5, 24}, /* 90 - TZ1090_PIN_TCK */
1628 {5, 26}, /* 91 - TZ1090_PIN_TRST */
1629 {5, 28}, /* 92 - TZ1090_PIN_TDI */
1630 {5, 30}, /* 93 - TZ1090_PIN_TDO */
1631 {6, 0}, /* 94 - TZ1090_PIN_TMS */
1632 {4, 16}, /* 95 - TZ1090_PIN_CLK_OUT0 */
1633 {4, 22}, /* 96 - TZ1090_PIN_CLK_OUT1 */
1636 static int tz1090_pinconf_reg(struct pinctrl_dev *pctldev,
1637 unsigned int pin,
1638 enum pin_config_param param,
1639 bool report_err,
1640 u32 *reg, u32 *width, u32 *mask, u32 *shift,
1641 u32 *val)
1643 struct tz1090_pinconf_pullup *pu;
1645 /* All supported pins have controllable input bias */
1646 switch (param) {
1647 case PIN_CONFIG_BIAS_DISABLE:
1648 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
1649 *val = REG_PU_PD_TRISTATE;
1650 break;
1651 case PIN_CONFIG_BIAS_PULL_UP:
1652 *val = REG_PU_PD_UP;
1653 break;
1654 case PIN_CONFIG_BIAS_PULL_DOWN:
1655 *val = REG_PU_PD_DOWN;
1656 break;
1657 case PIN_CONFIG_BIAS_BUS_HOLD:
1658 *val = REG_PU_PD_REPEATER;
1659 break;
1660 default:
1661 return -ENOTSUPP;
1664 /* Only input bias parameters supported */
1665 pu = &tz1090_pinconf_pullup[pin];
1666 *reg = REG_PINCTRL_PU_PD + 4*pu->index;
1667 *shift = pu->shift;
1668 *width = 2;
1670 /* Calculate field information */
1671 *mask = (BIT(*width) - 1) << *shift;
1673 return 0;
1676 static int tz1090_pinconf_get(struct pinctrl_dev *pctldev,
1677 unsigned int pin, unsigned long *config)
1679 struct tz1090_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
1680 enum pin_config_param param = pinconf_to_config_param(*config);
1681 int ret;
1682 u32 reg, width, mask, shift, val, tmp, arg;
1684 /* Get register information */
1685 ret = tz1090_pinconf_reg(pctldev, pin, param, true,
1686 &reg, &width, &mask, &shift, &val);
1687 if (ret < 0)
1688 return ret;
1690 /* Extract field from register */
1691 tmp = pmx_read(pmx, reg);
1692 arg = ((tmp & mask) >> shift) == val;
1694 /* Config not active */
1695 if (!arg)
1696 return -EINVAL;
1698 /* And pack config */
1699 *config = pinconf_to_config_packed(param, arg);
1701 return 0;
1704 static int tz1090_pinconf_set(struct pinctrl_dev *pctldev,
1705 unsigned int pin, unsigned long *configs,
1706 unsigned num_configs)
1708 struct tz1090_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
1709 enum pin_config_param param;
1710 unsigned int arg;
1711 int ret;
1712 u32 reg, width, mask, shift, val, tmp;
1713 unsigned long flags;
1714 int i;
1716 for (i = 0; i < num_configs; i++) {
1717 param = pinconf_to_config_param(configs[i]);
1718 arg = pinconf_to_config_argument(configs[i]);
1720 dev_dbg(pctldev->dev, "%s(pin=%s, config=%#lx)\n",
1721 __func__, tz1090_pins[pin].name, configs[i]);
1723 /* Get register information */
1724 ret = tz1090_pinconf_reg(pctldev, pin, param, true,
1725 &reg, &width, &mask, &shift, &val);
1726 if (ret < 0)
1727 return ret;
1729 /* Unpack argument and range check it */
1730 if (arg > 1) {
1731 dev_dbg(pctldev->dev, "%s: arg %u out of range\n",
1732 __func__, arg);
1733 return -EINVAL;
1736 /* Write register field */
1737 __global_lock2(flags);
1738 tmp = pmx_read(pmx, reg);
1739 tmp &= ~mask;
1740 if (arg)
1741 tmp |= val << shift;
1742 pmx_write(pmx, tmp, reg);
1743 __global_unlock2(flags);
1744 } /* for each config */
1746 return 0;
1749 static const int tz1090_boolean_map[] = {
1750 [0] = -EINVAL,
1751 [1] = 1,
1754 static const int tz1090_dr_map[] = {
1755 [REG_DR_2mA] = 2,
1756 [REG_DR_4mA] = 4,
1757 [REG_DR_8mA] = 8,
1758 [REG_DR_12mA] = 12,
1761 static int tz1090_pinconf_group_reg(struct pinctrl_dev *pctldev,
1762 const struct tz1090_pingroup *g,
1763 enum pin_config_param param,
1764 bool report_err,
1765 u32 *reg, u32 *width, u32 *mask, u32 *shift,
1766 const int **map)
1768 /* Drive configuration applies in groups, but not to all groups. */
1769 if (!g->drv) {
1770 if (report_err)
1771 dev_dbg(pctldev->dev,
1772 "%s: group %s has no drive control\n",
1773 __func__, g->name);
1774 return -ENOTSUPP;
1777 /* Find information about drive parameter's register */
1778 switch (param) {
1779 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
1780 *reg = REG_PINCTRL_SCHMITT;
1781 *width = 1;
1782 *map = tz1090_boolean_map;
1783 break;
1784 case PIN_CONFIG_DRIVE_STRENGTH:
1785 *reg = REG_PINCTRL_DR;
1786 *width = 2;
1787 *map = tz1090_dr_map;
1788 break;
1789 default:
1790 return -ENOTSUPP;
1793 /* Calculate field information */
1794 *shift = g->slw_bit * *width;
1795 *mask = (BIT(*width) - 1) << *shift;
1797 return 0;
1800 static int tz1090_pinconf_group_get(struct pinctrl_dev *pctldev,
1801 unsigned int group,
1802 unsigned long *config)
1804 struct tz1090_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
1805 const struct tz1090_pingroup *g;
1806 enum pin_config_param param = pinconf_to_config_param(*config);
1807 int ret, arg;
1808 unsigned int pin;
1809 u32 reg, width, mask, shift, val;
1810 const int *map;
1812 if (group >= ARRAY_SIZE(tz1090_groups)) {
1813 pin = group - ARRAY_SIZE(tz1090_groups);
1814 return tz1090_pinconf_get(pctldev, pin, config);
1817 g = &tz1090_groups[group];
1818 if (g->npins == 1) {
1819 pin = g->pins[0];
1820 ret = tz1090_pinconf_get(pctldev, pin, config);
1821 if (ret != -ENOTSUPP)
1822 return ret;
1825 /* Get register information */
1826 ret = tz1090_pinconf_group_reg(pctldev, g, param, true,
1827 &reg, &width, &mask, &shift, &map);
1828 if (ret < 0)
1829 return ret;
1831 /* Extract field from register */
1832 val = pmx_read(pmx, reg);
1833 arg = map[(val & mask) >> shift];
1834 if (arg < 0)
1835 return arg;
1837 /* And pack config */
1838 *config = pinconf_to_config_packed(param, arg);
1840 return 0;
1843 static int tz1090_pinconf_group_set(struct pinctrl_dev *pctldev,
1844 unsigned int group, unsigned long *configs,
1845 unsigned num_configs)
1847 struct tz1090_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
1848 const struct tz1090_pingroup *g;
1849 enum pin_config_param param;
1850 unsigned int arg, pin, i;
1851 const unsigned int *pit;
1852 int ret;
1853 u32 reg, width, mask, shift, val;
1854 unsigned long flags;
1855 const int *map;
1856 int j;
1858 if (group >= ARRAY_SIZE(tz1090_groups)) {
1859 pin = group - ARRAY_SIZE(tz1090_groups);
1860 return tz1090_pinconf_set(pctldev, pin, configs, num_configs);
1863 g = &tz1090_groups[group];
1864 if (g->npins == 1) {
1865 pin = g->pins[0];
1866 ret = tz1090_pinconf_set(pctldev, pin, configs, num_configs);
1867 if (ret != -ENOTSUPP)
1868 return ret;
1871 for (j = 0; j < num_configs; j++) {
1872 param = pinconf_to_config_param(configs[j]);
1874 dev_dbg(pctldev->dev, "%s(group=%s, config=%#lx)\n",
1875 __func__, g->name, configs[j]);
1877 /* Get register information */
1878 ret = tz1090_pinconf_group_reg(pctldev, g, param, true, &reg,
1879 &width, &mask, &shift, &map);
1880 if (ret < 0) {
1882 * Maybe we're trying to set a per-pin configuration
1883 * of a group, so do the pins one by one. This is
1884 * mainly as a convenience.
1886 for (i = 0, pit = g->pins; i < g->npins; ++i, ++pit) {
1887 ret = tz1090_pinconf_set(pctldev, *pit, configs,
1888 num_configs);
1889 if (ret)
1890 return ret;
1892 return 0;
1895 /* Unpack argument and map it to register value */
1896 arg = pinconf_to_config_argument(configs[j]);
1897 for (i = 0; i < BIT(width); ++i) {
1898 if (map[i] == arg || (map[i] == -EINVAL && !arg)) {
1899 /* Write register field */
1900 __global_lock2(flags);
1901 val = pmx_read(pmx, reg);
1902 val &= ~mask;
1903 val |= i << shift;
1904 pmx_write(pmx, val, reg);
1905 __global_unlock2(flags);
1906 goto next_config;
1910 dev_dbg(pctldev->dev, "%s: arg %u not supported\n",
1911 __func__, arg);
1912 return -EINVAL;
1914 next_config:
1916 } /* for each config */
1918 return 0;
1921 static const struct pinconf_ops tz1090_pinconf_ops = {
1922 .is_generic = true,
1923 .pin_config_get = tz1090_pinconf_get,
1924 .pin_config_set = tz1090_pinconf_set,
1925 .pin_config_group_get = tz1090_pinconf_group_get,
1926 .pin_config_group_set = tz1090_pinconf_group_set,
1927 .pin_config_config_dbg_show = pinconf_generic_dump_config,
1931 * Pin control driver setup
1934 static struct pinctrl_desc tz1090_pinctrl_desc = {
1935 .pctlops = &tz1090_pinctrl_ops,
1936 .pmxops = &tz1090_pinmux_ops,
1937 .confops = &tz1090_pinconf_ops,
1938 .owner = THIS_MODULE,
1941 static int tz1090_pinctrl_probe(struct platform_device *pdev)
1943 struct tz1090_pmx *pmx;
1944 struct resource *res;
1946 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
1947 if (!pmx)
1948 return -ENOMEM;
1950 pmx->dev = &pdev->dev;
1951 spin_lock_init(&pmx->lock);
1953 tz1090_pinctrl_desc.name = dev_name(&pdev->dev);
1954 tz1090_pinctrl_desc.pins = tz1090_pins;
1955 tz1090_pinctrl_desc.npins = ARRAY_SIZE(tz1090_pins);
1957 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1958 pmx->regs = devm_ioremap_resource(&pdev->dev, res);
1959 if (IS_ERR(pmx->regs))
1960 return PTR_ERR(pmx->regs);
1962 pmx->pctl = devm_pinctrl_register(&pdev->dev, &tz1090_pinctrl_desc,
1963 pmx);
1964 if (IS_ERR(pmx->pctl)) {
1965 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1966 return PTR_ERR(pmx->pctl);
1969 platform_set_drvdata(pdev, pmx);
1971 dev_info(&pdev->dev, "TZ1090 pinctrl driver initialised\n");
1973 return 0;
1976 static const struct of_device_id tz1090_pinctrl_of_match[] = {
1977 { .compatible = "img,tz1090-pinctrl", },
1978 { },
1981 static struct platform_driver tz1090_pinctrl_driver = {
1982 .driver = {
1983 .name = "tz1090-pinctrl",
1984 .of_match_table = tz1090_pinctrl_of_match,
1986 .probe = tz1090_pinctrl_probe,
1989 static int __init tz1090_pinctrl_init(void)
1991 tz1090_init_mux_pins();
1992 return platform_driver_register(&tz1090_pinctrl_driver);
1994 arch_initcall(tz1090_pinctrl_init);
1996 static void __exit tz1090_pinctrl_exit(void)
1998 platform_driver_unregister(&tz1090_pinctrl_driver);
2000 module_exit(tz1090_pinctrl_exit);
2002 MODULE_AUTHOR("Imagination Technologies Ltd.");
2003 MODULE_DESCRIPTION("Toumaz Xenif TZ1090 pinctrl driver");
2004 MODULE_LICENSE("GPL v2");
2005 MODULE_DEVICE_TABLE(of, tz1090_pinctrl_of_match);