1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2016 Broadcom Corporation
4 * This file contains the Northstar2 IOMUX driver that supports group
5 * based PINMUX configuration. The PWM is functional only when the
6 * corresponding mfio pin group is selected as gpio.
12 #include <linux/pinctrl/pinconf.h>
13 #include <linux/pinctrl/pinconf-generic.h>
14 #include <linux/pinctrl/pinctrl.h>
15 #include <linux/pinctrl/pinmux.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
20 #include "../pinctrl-utils.h"
22 #define NS2_NUM_IOMUX 19
23 #define NS2_NUM_PWM_MUX 4
25 #define NS2_PIN_MUX_BASE0 0x00
26 #define NS2_PIN_MUX_BASE1 0x01
27 #define NS2_PIN_CONF_BASE 0x02
28 #define NS2_MUX_PAD_FUNC1_OFFSET 0x04
30 #define NS2_PIN_SRC_MASK 0x01
31 #define NS2_PIN_PULL_MASK 0x03
32 #define NS2_PIN_DRIVE_STRENGTH_MASK 0x07
34 #define NS2_PIN_PULL_UP 0x01
35 #define NS2_PIN_PULL_DOWN 0x02
37 #define NS2_PIN_INPUT_EN_MASK 0x01
40 * Northstar2 IOMUX register description
42 * @base: base address number
43 * @offset: register offset for mux configuration of a group
44 * @shift: bit shift for mux configuration of a group
46 * @alt: alternate function to set to
57 * Keep track of Northstar2 IOMUX configuration and prevent double
60 * @ns2_mux: Northstar2 IOMUX register description
61 * @is_configured: flag to indicate whether a mux setting has already
70 * Group based IOMUX configuration
72 * @name: name of the group
73 * @pins: array of pins used by this group
74 * @num_pins: total number of pins used by this group
75 * @mux: Northstar2 group based IOMUX configuration
77 struct ns2_pin_group {
79 const unsigned int *pins;
80 const unsigned int num_pins;
81 const struct ns2_mux mux;
85 * Northstar2 mux function and supported pin groups
87 * @name: name of the function
88 * @groups: array of groups that can be supported by this function
89 * @num_groups: total number of groups that can be supported by function
91 struct ns2_pin_function {
93 const char * const *groups;
94 const unsigned int num_groups;
98 * Northstar2 IOMUX pinctrl core
100 * @pctl: pointer to pinctrl_dev
101 * @dev: pointer to device
102 * @base0: first IOMUX register base
103 * @base1: second IOMUX register base
104 * @pinconf_base: configuration register base
105 * @groups: pointer to array of groups
106 * @num_groups: total number of groups
107 * @functions: pointer to array of functions
108 * @num_functions: total number of functions
109 * @mux_log: pointer to the array of mux logs
110 * @lock: lock to protect register access
113 struct pinctrl_dev *pctl;
117 void __iomem *pinconf_base;
119 const struct ns2_pin_group *groups;
120 unsigned int num_groups;
122 const struct ns2_pin_function *functions;
123 unsigned int num_functions;
125 struct ns2_mux_log *mux_log;
131 * Pin configuration info
133 * @base: base address number
134 * @offset: register offset from base
135 * @src_shift: slew rate control bit shift in the register
136 * @input_en: input enable control bit shift
137 * @pull_shift: pull-up/pull-down control bit shift in the register
138 * @drive_shift: drive strength control bit shift in the register
143 unsigned int src_shift;
144 unsigned int input_en;
145 unsigned int pull_shift;
146 unsigned int drive_shift;
150 * Description of a pin in Northstar2
154 * @pin_conf: pin configuration structure
159 struct ns2_pinconf pin_conf;
162 #define NS2_PIN_DESC(p, n, b, o, s, i, pu, d) \
177 * List of pins in Northstar2
179 static struct ns2_pin ns2_pins[] = {
180 NS2_PIN_DESC(0, "mfio_0", -1, 0, 0, 0, 0, 0),
181 NS2_PIN_DESC(1, "mfio_1", -1, 0, 0, 0, 0, 0),
182 NS2_PIN_DESC(2, "mfio_2", -1, 0, 0, 0, 0, 0),
183 NS2_PIN_DESC(3, "mfio_3", -1, 0, 0, 0, 0, 0),
184 NS2_PIN_DESC(4, "mfio_4", -1, 0, 0, 0, 0, 0),
185 NS2_PIN_DESC(5, "mfio_5", -1, 0, 0, 0, 0, 0),
186 NS2_PIN_DESC(6, "mfio_6", -1, 0, 0, 0, 0, 0),
187 NS2_PIN_DESC(7, "mfio_7", -1, 0, 0, 0, 0, 0),
188 NS2_PIN_DESC(8, "mfio_8", -1, 0, 0, 0, 0, 0),
189 NS2_PIN_DESC(9, "mfio_9", -1, 0, 0, 0, 0, 0),
190 NS2_PIN_DESC(10, "mfio_10", -1, 0, 0, 0, 0, 0),
191 NS2_PIN_DESC(11, "mfio_11", -1, 0, 0, 0, 0, 0),
192 NS2_PIN_DESC(12, "mfio_12", -1, 0, 0, 0, 0, 0),
193 NS2_PIN_DESC(13, "mfio_13", -1, 0, 0, 0, 0, 0),
194 NS2_PIN_DESC(14, "mfio_14", -1, 0, 0, 0, 0, 0),
195 NS2_PIN_DESC(15, "mfio_15", -1, 0, 0, 0, 0, 0),
196 NS2_PIN_DESC(16, "mfio_16", -1, 0, 0, 0, 0, 0),
197 NS2_PIN_DESC(17, "mfio_17", -1, 0, 0, 0, 0, 0),
198 NS2_PIN_DESC(18, "mfio_18", -1, 0, 0, 0, 0, 0),
199 NS2_PIN_DESC(19, "mfio_19", -1, 0, 0, 0, 0, 0),
200 NS2_PIN_DESC(20, "mfio_20", -1, 0, 0, 0, 0, 0),
201 NS2_PIN_DESC(21, "mfio_21", -1, 0, 0, 0, 0, 0),
202 NS2_PIN_DESC(22, "mfio_22", -1, 0, 0, 0, 0, 0),
203 NS2_PIN_DESC(23, "mfio_23", -1, 0, 0, 0, 0, 0),
204 NS2_PIN_DESC(24, "mfio_24", -1, 0, 0, 0, 0, 0),
205 NS2_PIN_DESC(25, "mfio_25", -1, 0, 0, 0, 0, 0),
206 NS2_PIN_DESC(26, "mfio_26", -1, 0, 0, 0, 0, 0),
207 NS2_PIN_DESC(27, "mfio_27", -1, 0, 0, 0, 0, 0),
208 NS2_PIN_DESC(28, "mfio_28", -1, 0, 0, 0, 0, 0),
209 NS2_PIN_DESC(29, "mfio_29", -1, 0, 0, 0, 0, 0),
210 NS2_PIN_DESC(30, "mfio_30", -1, 0, 0, 0, 0, 0),
211 NS2_PIN_DESC(31, "mfio_31", -1, 0, 0, 0, 0, 0),
212 NS2_PIN_DESC(32, "mfio_32", -1, 0, 0, 0, 0, 0),
213 NS2_PIN_DESC(33, "mfio_33", -1, 0, 0, 0, 0, 0),
214 NS2_PIN_DESC(34, "mfio_34", -1, 0, 0, 0, 0, 0),
215 NS2_PIN_DESC(35, "mfio_35", -1, 0, 0, 0, 0, 0),
216 NS2_PIN_DESC(36, "mfio_36", -1, 0, 0, 0, 0, 0),
217 NS2_PIN_DESC(37, "mfio_37", -1, 0, 0, 0, 0, 0),
218 NS2_PIN_DESC(38, "mfio_38", -1, 0, 0, 0, 0, 0),
219 NS2_PIN_DESC(39, "mfio_39", -1, 0, 0, 0, 0, 0),
220 NS2_PIN_DESC(40, "mfio_40", -1, 0, 0, 0, 0, 0),
221 NS2_PIN_DESC(41, "mfio_41", -1, 0, 0, 0, 0, 0),
222 NS2_PIN_DESC(42, "mfio_42", -1, 0, 0, 0, 0, 0),
223 NS2_PIN_DESC(43, "mfio_43", -1, 0, 0, 0, 0, 0),
224 NS2_PIN_DESC(44, "mfio_44", -1, 0, 0, 0, 0, 0),
225 NS2_PIN_DESC(45, "mfio_45", -1, 0, 0, 0, 0, 0),
226 NS2_PIN_DESC(46, "mfio_46", -1, 0, 0, 0, 0, 0),
227 NS2_PIN_DESC(47, "mfio_47", -1, 0, 0, 0, 0, 0),
228 NS2_PIN_DESC(48, "mfio_48", -1, 0, 0, 0, 0, 0),
229 NS2_PIN_DESC(49, "mfio_49", -1, 0, 0, 0, 0, 0),
230 NS2_PIN_DESC(50, "mfio_50", -1, 0, 0, 0, 0, 0),
231 NS2_PIN_DESC(51, "mfio_51", -1, 0, 0, 0, 0, 0),
232 NS2_PIN_DESC(52, "mfio_52", -1, 0, 0, 0, 0, 0),
233 NS2_PIN_DESC(53, "mfio_53", -1, 0, 0, 0, 0, 0),
234 NS2_PIN_DESC(54, "mfio_54", -1, 0, 0, 0, 0, 0),
235 NS2_PIN_DESC(55, "mfio_55", -1, 0, 0, 0, 0, 0),
236 NS2_PIN_DESC(56, "mfio_56", -1, 0, 0, 0, 0, 0),
237 NS2_PIN_DESC(57, "mfio_57", -1, 0, 0, 0, 0, 0),
238 NS2_PIN_DESC(58, "mfio_58", -1, 0, 0, 0, 0, 0),
239 NS2_PIN_DESC(59, "mfio_59", -1, 0, 0, 0, 0, 0),
240 NS2_PIN_DESC(60, "mfio_60", -1, 0, 0, 0, 0, 0),
241 NS2_PIN_DESC(61, "mfio_61", -1, 0, 0, 0, 0, 0),
242 NS2_PIN_DESC(62, "mfio_62", -1, 0, 0, 0, 0, 0),
243 NS2_PIN_DESC(63, "qspi_wp", 2, 0x0, 31, 30, 27, 24),
244 NS2_PIN_DESC(64, "qspi_hold", 2, 0x0, 23, 22, 19, 16),
245 NS2_PIN_DESC(65, "qspi_cs", 2, 0x0, 15, 14, 11, 8),
246 NS2_PIN_DESC(66, "qspi_sck", 2, 0x0, 7, 6, 3, 0),
247 NS2_PIN_DESC(67, "uart3_sin", 2, 0x04, 31, 30, 27, 24),
248 NS2_PIN_DESC(68, "uart3_sout", 2, 0x04, 23, 22, 19, 16),
249 NS2_PIN_DESC(69, "qspi_mosi", 2, 0x04, 15, 14, 11, 8),
250 NS2_PIN_DESC(70, "qspi_miso", 2, 0x04, 7, 6, 3, 0),
251 NS2_PIN_DESC(71, "spi0_fss", 2, 0x08, 31, 30, 27, 24),
252 NS2_PIN_DESC(72, "spi0_rxd", 2, 0x08, 23, 22, 19, 16),
253 NS2_PIN_DESC(73, "spi0_txd", 2, 0x08, 15, 14, 11, 8),
254 NS2_PIN_DESC(74, "spi0_sck", 2, 0x08, 7, 6, 3, 0),
255 NS2_PIN_DESC(75, "spi1_fss", 2, 0x0c, 31, 30, 27, 24),
256 NS2_PIN_DESC(76, "spi1_rxd", 2, 0x0c, 23, 22, 19, 16),
257 NS2_PIN_DESC(77, "spi1_txd", 2, 0x0c, 15, 14, 11, 8),
258 NS2_PIN_DESC(78, "spi1_sck", 2, 0x0c, 7, 6, 3, 0),
259 NS2_PIN_DESC(79, "sdio0_data7", 2, 0x10, 31, 30, 27, 24),
260 NS2_PIN_DESC(80, "sdio0_emmc_rst", 2, 0x10, 23, 22, 19, 16),
261 NS2_PIN_DESC(81, "sdio0_led_on", 2, 0x10, 15, 14, 11, 8),
262 NS2_PIN_DESC(82, "sdio0_wp", 2, 0x10, 7, 6, 3, 0),
263 NS2_PIN_DESC(83, "sdio0_data3", 2, 0x14, 31, 30, 27, 24),
264 NS2_PIN_DESC(84, "sdio0_data4", 2, 0x14, 23, 22, 19, 16),
265 NS2_PIN_DESC(85, "sdio0_data5", 2, 0x14, 15, 14, 11, 8),
266 NS2_PIN_DESC(86, "sdio0_data6", 2, 0x14, 7, 6, 3, 0),
267 NS2_PIN_DESC(87, "sdio0_cmd", 2, 0x18, 31, 30, 27, 24),
268 NS2_PIN_DESC(88, "sdio0_data0", 2, 0x18, 23, 22, 19, 16),
269 NS2_PIN_DESC(89, "sdio0_data1", 2, 0x18, 15, 14, 11, 8),
270 NS2_PIN_DESC(90, "sdio0_data2", 2, 0x18, 7, 6, 3, 0),
271 NS2_PIN_DESC(91, "sdio1_led_on", 2, 0x1c, 31, 30, 27, 24),
272 NS2_PIN_DESC(92, "sdio1_wp", 2, 0x1c, 23, 22, 19, 16),
273 NS2_PIN_DESC(93, "sdio0_cd_l", 2, 0x1c, 15, 14, 11, 8),
274 NS2_PIN_DESC(94, "sdio0_clk", 2, 0x1c, 7, 6, 3, 0),
275 NS2_PIN_DESC(95, "sdio1_data5", 2, 0x20, 31, 30, 27, 24),
276 NS2_PIN_DESC(96, "sdio1_data6", 2, 0x20, 23, 22, 19, 16),
277 NS2_PIN_DESC(97, "sdio1_data7", 2, 0x20, 15, 14, 11, 8),
278 NS2_PIN_DESC(98, "sdio1_emmc_rst", 2, 0x20, 7, 6, 3, 0),
279 NS2_PIN_DESC(99, "sdio1_data1", 2, 0x24, 31, 30, 27, 24),
280 NS2_PIN_DESC(100, "sdio1_data2", 2, 0x24, 23, 22, 19, 16),
281 NS2_PIN_DESC(101, "sdio1_data3", 2, 0x24, 15, 14, 11, 8),
282 NS2_PIN_DESC(102, "sdio1_data4", 2, 0x24, 7, 6, 3, 0),
283 NS2_PIN_DESC(103, "sdio1_cd_l", 2, 0x28, 31, 30, 27, 24),
284 NS2_PIN_DESC(104, "sdio1_clk", 2, 0x28, 23, 22, 19, 16),
285 NS2_PIN_DESC(105, "sdio1_cmd", 2, 0x28, 15, 14, 11, 8),
286 NS2_PIN_DESC(106, "sdio1_data0", 2, 0x28, 7, 6, 3, 0),
287 NS2_PIN_DESC(107, "ext_mdio_0", 2, 0x2c, 15, 14, 11, 8),
288 NS2_PIN_DESC(108, "ext_mdc_0", 2, 0x2c, 7, 6, 3, 0),
289 NS2_PIN_DESC(109, "usb3_p1_vbus_ppc", 2, 0x34, 31, 30, 27, 24),
290 NS2_PIN_DESC(110, "usb3_p1_overcurrent", 2, 0x34, 23, 22, 19, 16),
291 NS2_PIN_DESC(111, "usb3_p0_vbus_ppc", 2, 0x34, 15, 14, 11, 8),
292 NS2_PIN_DESC(112, "usb3_p0_overcurrent", 2, 0x34, 7, 6, 3, 0),
293 NS2_PIN_DESC(113, "usb2_presence_indication", 2, 0x38, 31, 30, 27, 24),
294 NS2_PIN_DESC(114, "usb2_vbus_present", 2, 0x38, 23, 22, 19, 16),
295 NS2_PIN_DESC(115, "usb2_vbus_ppc", 2, 0x38, 15, 14, 11, 8),
296 NS2_PIN_DESC(116, "usb2_overcurrent", 2, 0x38, 7, 6, 3, 0),
297 NS2_PIN_DESC(117, "sata_led1", 2, 0x3c, 15, 14, 11, 8),
298 NS2_PIN_DESC(118, "sata_led0", 2, 0x3c, 7, 6, 3, 0),
302 * List of groups of pins
305 static const unsigned int nand_pins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
306 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
307 static const unsigned int nor_data_pins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
308 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
310 static const unsigned int gpio_0_1_pins[] = {24, 25};
311 static const unsigned int pwm_0_pins[] = {24};
312 static const unsigned int pwm_1_pins[] = {25};
314 static const unsigned int uart1_ext_clk_pins[] = {26};
315 static const unsigned int nor_adv_pins[] = {26};
317 static const unsigned int gpio_2_5_pins[] = {27, 28, 29, 30};
318 static const unsigned int pcie_ab1_clk_wak_pins[] = {27, 28, 29, 30};
319 static const unsigned int nor_addr_0_3_pins[] = {27, 28, 29, 30};
320 static const unsigned int pwm_2_pins[] = {27};
321 static const unsigned int pwm_3_pins[] = {28};
323 static const unsigned int gpio_6_7_pins[] = {31, 32};
324 static const unsigned int pcie_a3_clk_wak_pins[] = {31, 32};
325 static const unsigned int nor_addr_4_5_pins[] = {31, 32};
327 static const unsigned int gpio_8_9_pins[] = {33, 34};
328 static const unsigned int pcie_b3_clk_wak_pins[] = {33, 34};
329 static const unsigned int nor_addr_6_7_pins[] = {33, 34};
331 static const unsigned int gpio_10_11_pins[] = {35, 36};
332 static const unsigned int pcie_b2_clk_wak_pins[] = {35, 36};
333 static const unsigned int nor_addr_8_9_pins[] = {35, 36};
335 static const unsigned int gpio_12_13_pins[] = {37, 38};
336 static const unsigned int pcie_a2_clk_wak_pins[] = {37, 38};
337 static const unsigned int nor_addr_10_11_pins[] = {37, 38};
339 static const unsigned int gpio_14_17_pins[] = {39, 40, 41, 42};
340 static const unsigned int uart0_modem_pins[] = {39, 40, 41, 42};
341 static const unsigned int nor_addr_12_15_pins[] = {39, 40, 41, 42};
343 static const unsigned int gpio_18_19_pins[] = {43, 44};
344 static const unsigned int uart0_rts_cts_pins[] = {43, 44};
346 static const unsigned int gpio_20_21_pins[] = {45, 46};
347 static const unsigned int uart0_in_out_pins[] = {45, 46};
349 static const unsigned int gpio_22_23_pins[] = {47, 48};
350 static const unsigned int uart1_dcd_dsr_pins[] = {47, 48};
352 static const unsigned int gpio_24_25_pins[] = {49, 50};
353 static const unsigned int uart1_ri_dtr_pins[] = {49, 50};
355 static const unsigned int gpio_26_27_pins[] = {51, 52};
356 static const unsigned int uart1_rts_cts_pins[] = {51, 52};
358 static const unsigned int gpio_28_29_pins[] = {53, 54};
359 static const unsigned int uart1_in_out_pins[] = {53, 54};
361 static const unsigned int gpio_30_31_pins[] = {55, 56};
362 static const unsigned int uart2_rts_cts_pins[] = {55, 56};
364 #define NS2_PIN_GROUP(group_name, ba, off, sh, ma, al) \
366 .name = __stringify(group_name) "_grp", \
367 .pins = group_name ## _pins, \
368 .num_pins = ARRAY_SIZE(group_name ## _pins), \
379 * List of Northstar2 pin groups
381 static const struct ns2_pin_group ns2_pin_groups[] = {
382 NS2_PIN_GROUP(nand, 0, 0, 31, 1, 0),
383 NS2_PIN_GROUP(nor_data, 0, 0, 31, 1, 1),
384 NS2_PIN_GROUP(gpio_0_1, 0, 0, 31, 1, 0),
386 NS2_PIN_GROUP(uart1_ext_clk, 0, 4, 30, 3, 1),
387 NS2_PIN_GROUP(nor_adv, 0, 4, 30, 3, 2),
389 NS2_PIN_GROUP(gpio_2_5, 0, 4, 28, 3, 0),
390 NS2_PIN_GROUP(pcie_ab1_clk_wak, 0, 4, 28, 3, 1),
391 NS2_PIN_GROUP(nor_addr_0_3, 0, 4, 28, 3, 2),
393 NS2_PIN_GROUP(gpio_6_7, 0, 4, 26, 3, 0),
394 NS2_PIN_GROUP(pcie_a3_clk_wak, 0, 4, 26, 3, 1),
395 NS2_PIN_GROUP(nor_addr_4_5, 0, 4, 26, 3, 2),
397 NS2_PIN_GROUP(gpio_8_9, 0, 4, 24, 3, 0),
398 NS2_PIN_GROUP(pcie_b3_clk_wak, 0, 4, 24, 3, 1),
399 NS2_PIN_GROUP(nor_addr_6_7, 0, 4, 24, 3, 2),
401 NS2_PIN_GROUP(gpio_10_11, 0, 4, 22, 3, 0),
402 NS2_PIN_GROUP(pcie_b2_clk_wak, 0, 4, 22, 3, 1),
403 NS2_PIN_GROUP(nor_addr_8_9, 0, 4, 22, 3, 2),
405 NS2_PIN_GROUP(gpio_12_13, 0, 4, 20, 3, 0),
406 NS2_PIN_GROUP(pcie_a2_clk_wak, 0, 4, 20, 3, 1),
407 NS2_PIN_GROUP(nor_addr_10_11, 0, 4, 20, 3, 2),
409 NS2_PIN_GROUP(gpio_14_17, 0, 4, 18, 3, 0),
410 NS2_PIN_GROUP(uart0_modem, 0, 4, 18, 3, 1),
411 NS2_PIN_GROUP(nor_addr_12_15, 0, 4, 18, 3, 2),
413 NS2_PIN_GROUP(gpio_18_19, 0, 4, 16, 3, 0),
414 NS2_PIN_GROUP(uart0_rts_cts, 0, 4, 16, 3, 1),
416 NS2_PIN_GROUP(gpio_20_21, 0, 4, 14, 3, 0),
417 NS2_PIN_GROUP(uart0_in_out, 0, 4, 14, 3, 1),
419 NS2_PIN_GROUP(gpio_22_23, 0, 4, 12, 3, 0),
420 NS2_PIN_GROUP(uart1_dcd_dsr, 0, 4, 12, 3, 1),
422 NS2_PIN_GROUP(gpio_24_25, 0, 4, 10, 3, 0),
423 NS2_PIN_GROUP(uart1_ri_dtr, 0, 4, 10, 3, 1),
425 NS2_PIN_GROUP(gpio_26_27, 0, 4, 8, 3, 0),
426 NS2_PIN_GROUP(uart1_rts_cts, 0, 4, 8, 3, 1),
428 NS2_PIN_GROUP(gpio_28_29, 0, 4, 6, 3, 0),
429 NS2_PIN_GROUP(uart1_in_out, 0, 4, 6, 3, 1),
431 NS2_PIN_GROUP(gpio_30_31, 0, 4, 4, 3, 0),
432 NS2_PIN_GROUP(uart2_rts_cts, 0, 4, 4, 3, 1),
434 NS2_PIN_GROUP(pwm_0, 1, 0, 0, 1, 1),
435 NS2_PIN_GROUP(pwm_1, 1, 0, 1, 1, 1),
436 NS2_PIN_GROUP(pwm_2, 1, 0, 2, 1, 1),
437 NS2_PIN_GROUP(pwm_3, 1, 0, 3, 1, 1),
441 * List of groups supported by functions
444 static const char * const nand_grps[] = {"nand_grp"};
446 static const char * const nor_grps[] = {"nor_data_grp", "nor_adv_grp",
447 "nor_addr_0_3_grp", "nor_addr_4_5_grp", "nor_addr_6_7_grp",
448 "nor_addr_8_9_grp", "nor_addr_10_11_grp", "nor_addr_12_15_grp"};
450 static const char * const gpio_grps[] = {"gpio_0_1_grp", "gpio_2_5_grp",
451 "gpio_6_7_grp", "gpio_8_9_grp", "gpio_10_11_grp", "gpio_12_13_grp",
452 "gpio_14_17_grp", "gpio_18_19_grp", "gpio_20_21_grp", "gpio_22_23_grp",
453 "gpio_24_25_grp", "gpio_26_27_grp", "gpio_28_29_grp",
456 static const char * const pcie_grps[] = {"pcie_ab1_clk_wak_grp",
457 "pcie_a3_clk_wak_grp", "pcie_b3_clk_wak_grp", "pcie_b2_clk_wak_grp",
458 "pcie_a2_clk_wak_grp"};
460 static const char * const uart0_grps[] = {"uart0_modem_grp",
461 "uart0_rts_cts_grp", "uart0_in_out_grp"};
463 static const char * const uart1_grps[] = {"uart1_ext_clk_grp",
464 "uart1_dcd_dsr_grp", "uart1_ri_dtr_grp", "uart1_rts_cts_grp",
467 static const char * const uart2_grps[] = {"uart2_rts_cts_grp"};
469 static const char * const pwm_grps[] = {"pwm_0_grp", "pwm_1_grp",
470 "pwm_2_grp", "pwm_3_grp"};
472 #define NS2_PIN_FUNCTION(func) \
475 .groups = func ## _grps, \
476 .num_groups = ARRAY_SIZE(func ## _grps), \
480 * List of supported functions
482 static const struct ns2_pin_function ns2_pin_functions[] = {
483 NS2_PIN_FUNCTION(nand),
484 NS2_PIN_FUNCTION(nor),
485 NS2_PIN_FUNCTION(gpio),
486 NS2_PIN_FUNCTION(pcie),
487 NS2_PIN_FUNCTION(uart0),
488 NS2_PIN_FUNCTION(uart1),
489 NS2_PIN_FUNCTION(uart2),
490 NS2_PIN_FUNCTION(pwm),
493 static int ns2_get_groups_count(struct pinctrl_dev *pctrl_dev)
495 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
497 return pinctrl->num_groups;
500 static const char *ns2_get_group_name(struct pinctrl_dev *pctrl_dev,
501 unsigned int selector)
503 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
505 return pinctrl->groups[selector].name;
508 static int ns2_get_group_pins(struct pinctrl_dev *pctrl_dev,
509 unsigned int selector, const unsigned int **pins,
510 unsigned int *num_pins)
512 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
514 *pins = pinctrl->groups[selector].pins;
515 *num_pins = pinctrl->groups[selector].num_pins;
520 static void ns2_pin_dbg_show(struct pinctrl_dev *pctrl_dev,
521 struct seq_file *s, unsigned int offset)
523 seq_printf(s, " %s", dev_name(pctrl_dev->dev));
526 static const struct pinctrl_ops ns2_pinctrl_ops = {
527 .get_groups_count = ns2_get_groups_count,
528 .get_group_name = ns2_get_group_name,
529 .get_group_pins = ns2_get_group_pins,
530 .pin_dbg_show = ns2_pin_dbg_show,
531 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
532 .dt_free_map = pinctrl_utils_free_map,
535 static int ns2_get_functions_count(struct pinctrl_dev *pctrl_dev)
537 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
539 return pinctrl->num_functions;
542 static const char *ns2_get_function_name(struct pinctrl_dev *pctrl_dev,
543 unsigned int selector)
545 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
547 return pinctrl->functions[selector].name;
550 static int ns2_get_function_groups(struct pinctrl_dev *pctrl_dev,
551 unsigned int selector,
552 const char * const **groups,
553 unsigned int * const num_groups)
555 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
557 *groups = pinctrl->functions[selector].groups;
558 *num_groups = pinctrl->functions[selector].num_groups;
563 static int ns2_pinmux_set(struct ns2_pinctrl *pinctrl,
564 const struct ns2_pin_function *func,
565 const struct ns2_pin_group *grp,
566 struct ns2_mux_log *mux_log)
568 const struct ns2_mux *mux = &grp->mux;
572 void __iomem *base_address;
574 for (i = 0; i < NS2_NUM_IOMUX; i++) {
575 if ((mux->shift != mux_log[i].mux.shift) ||
576 (mux->base != mux_log[i].mux.base) ||
577 (mux->offset != mux_log[i].mux.offset))
580 /* if this is a new configuration, just do it! */
581 if (!mux_log[i].is_configured)
585 * IOMUX has been configured previously and one is trying to
586 * configure it to a different function
588 if (mux_log[i].mux.alt != mux->alt) {
589 dev_err(pinctrl->dev,
590 "double configuration error detected!\n");
591 dev_err(pinctrl->dev, "func:%s grp:%s\n",
592 func->name, grp->name);
598 if (i == NS2_NUM_IOMUX)
602 mux_log[i].mux.alt = mux->alt;
603 mux_log[i].is_configured = true;
606 case NS2_PIN_MUX_BASE0:
607 base_address = pinctrl->base0;
610 case NS2_PIN_MUX_BASE1:
611 base_address = pinctrl->base1;
618 spin_lock_irqsave(&pinctrl->lock, flags);
619 val = readl(base_address + grp->mux.offset);
620 val &= ~(mask << grp->mux.shift);
621 val |= grp->mux.alt << grp->mux.shift;
622 writel(val, (base_address + grp->mux.offset));
623 spin_unlock_irqrestore(&pinctrl->lock, flags);
628 static int ns2_pinmux_enable(struct pinctrl_dev *pctrl_dev,
629 unsigned int func_select, unsigned int grp_select)
631 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
632 const struct ns2_pin_function *func;
633 const struct ns2_pin_group *grp;
635 if (grp_select >= pinctrl->num_groups ||
636 func_select >= pinctrl->num_functions)
639 func = &pinctrl->functions[func_select];
640 grp = &pinctrl->groups[grp_select];
642 dev_dbg(pctrl_dev->dev, "func:%u name:%s grp:%u name:%s\n",
643 func_select, func->name, grp_select, grp->name);
645 dev_dbg(pctrl_dev->dev, "offset:0x%08x shift:%u alt:%u\n",
646 grp->mux.offset, grp->mux.shift, grp->mux.alt);
648 return ns2_pinmux_set(pinctrl, func, grp, pinctrl->mux_log);
651 static int ns2_pin_set_enable(struct pinctrl_dev *pctrldev, unsigned int pin,
654 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrldev);
655 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
658 void __iomem *base_address;
660 base_address = pinctrl->pinconf_base;
661 spin_lock_irqsave(&pinctrl->lock, flags);
662 val = readl(base_address + pin_data->pin_conf.offset);
663 val &= ~(NS2_PIN_SRC_MASK << pin_data->pin_conf.input_en);
666 val |= NS2_PIN_INPUT_EN_MASK << pin_data->pin_conf.input_en;
668 writel(val, (base_address + pin_data->pin_conf.offset));
669 spin_unlock_irqrestore(&pinctrl->lock, flags);
671 dev_dbg(pctrldev->dev, "pin:%u set enable:%d\n", pin, enable);
675 static int ns2_pin_get_enable(struct pinctrl_dev *pctrldev, unsigned int pin)
677 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrldev);
678 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
682 spin_lock_irqsave(&pinctrl->lock, flags);
683 enable = readl(pinctrl->pinconf_base + pin_data->pin_conf.offset);
684 enable = (enable >> pin_data->pin_conf.input_en) &
685 NS2_PIN_INPUT_EN_MASK;
686 spin_unlock_irqrestore(&pinctrl->lock, flags);
689 enable = NS2_PIN_INPUT_EN_MASK;
693 dev_dbg(pctrldev->dev, "pin:%u get disable:%d\n", pin, enable);
697 static int ns2_pin_set_slew(struct pinctrl_dev *pctrldev, unsigned int pin,
700 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrldev);
701 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
704 void __iomem *base_address;
706 base_address = pinctrl->pinconf_base;
707 spin_lock_irqsave(&pinctrl->lock, flags);
708 val = readl(base_address + pin_data->pin_conf.offset);
709 val &= ~(NS2_PIN_SRC_MASK << pin_data->pin_conf.src_shift);
712 val |= NS2_PIN_SRC_MASK << pin_data->pin_conf.src_shift;
714 writel(val, (base_address + pin_data->pin_conf.offset));
715 spin_unlock_irqrestore(&pinctrl->lock, flags);
717 dev_dbg(pctrldev->dev, "pin:%u set slew:%d\n", pin, slew);
721 static int ns2_pin_get_slew(struct pinctrl_dev *pctrldev, unsigned int pin,
724 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrldev);
725 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
729 spin_lock_irqsave(&pinctrl->lock, flags);
730 val = readl(pinctrl->pinconf_base + pin_data->pin_conf.offset);
731 *slew = (val >> pin_data->pin_conf.src_shift) & NS2_PIN_SRC_MASK;
732 spin_unlock_irqrestore(&pinctrl->lock, flags);
734 dev_dbg(pctrldev->dev, "pin:%u get slew:%d\n", pin, *slew);
738 static int ns2_pin_set_pull(struct pinctrl_dev *pctrldev, unsigned int pin,
739 bool pull_up, bool pull_down)
741 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrldev);
742 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
745 void __iomem *base_address;
747 base_address = pinctrl->pinconf_base;
748 spin_lock_irqsave(&pinctrl->lock, flags);
749 val = readl(base_address + pin_data->pin_conf.offset);
750 val &= ~(NS2_PIN_PULL_MASK << pin_data->pin_conf.pull_shift);
753 val |= NS2_PIN_PULL_UP << pin_data->pin_conf.pull_shift;
754 if (pull_down == true)
755 val |= NS2_PIN_PULL_DOWN << pin_data->pin_conf.pull_shift;
756 writel(val, (base_address + pin_data->pin_conf.offset));
757 spin_unlock_irqrestore(&pinctrl->lock, flags);
759 dev_dbg(pctrldev->dev, "pin:%u set pullup:%d pulldown: %d\n",
760 pin, pull_up, pull_down);
764 static void ns2_pin_get_pull(struct pinctrl_dev *pctrldev,
765 unsigned int pin, bool *pull_up,
768 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrldev);
769 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
773 spin_lock_irqsave(&pinctrl->lock, flags);
774 val = readl(pinctrl->pinconf_base + pin_data->pin_conf.offset);
775 val = (val >> pin_data->pin_conf.pull_shift) & NS2_PIN_PULL_MASK;
779 if (val == NS2_PIN_PULL_UP)
782 if (val == NS2_PIN_PULL_DOWN)
784 spin_unlock_irqrestore(&pinctrl->lock, flags);
787 static int ns2_pin_set_strength(struct pinctrl_dev *pctrldev, unsigned int pin,
790 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrldev);
791 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
794 void __iomem *base_address;
796 /* make sure drive strength is supported */
797 if (strength < 2 || strength > 16 || (strength % 2))
800 base_address = pinctrl->pinconf_base;
801 spin_lock_irqsave(&pinctrl->lock, flags);
802 val = readl(base_address + pin_data->pin_conf.offset);
803 val &= ~(NS2_PIN_DRIVE_STRENGTH_MASK << pin_data->pin_conf.drive_shift);
804 val |= ((strength / 2) - 1) << pin_data->pin_conf.drive_shift;
805 writel(val, (base_address + pin_data->pin_conf.offset));
806 spin_unlock_irqrestore(&pinctrl->lock, flags);
808 dev_dbg(pctrldev->dev, "pin:%u set drive strength:%d mA\n",
813 static int ns2_pin_get_strength(struct pinctrl_dev *pctrldev, unsigned int pin,
816 struct ns2_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctrldev);
817 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
821 spin_lock_irqsave(&pinctrl->lock, flags);
822 val = readl(pinctrl->pinconf_base + pin_data->pin_conf.offset);
823 *strength = (val >> pin_data->pin_conf.drive_shift) &
824 NS2_PIN_DRIVE_STRENGTH_MASK;
825 *strength = (*strength + 1) * 2;
826 spin_unlock_irqrestore(&pinctrl->lock, flags);
828 dev_dbg(pctrldev->dev, "pin:%u get drive strength:%d mA\n",
833 static int ns2_pin_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
834 unsigned long *config)
836 struct ns2_pin *pin_data = pctldev->desc->pins[pin].drv_data;
837 enum pin_config_param param = pinconf_to_config_param(*config);
838 bool pull_up, pull_down;
842 if (pin_data->pin_conf.base == -1)
846 case PIN_CONFIG_BIAS_DISABLE:
847 ns2_pin_get_pull(pctldev, pin, &pull_up, &pull_down);
848 if (!pull_up && !pull_down)
853 case PIN_CONFIG_BIAS_PULL_UP:
854 ns2_pin_get_pull(pctldev, pin, &pull_up, &pull_down);
860 case PIN_CONFIG_BIAS_PULL_DOWN:
861 ns2_pin_get_pull(pctldev, pin, &pull_up, &pull_down);
867 case PIN_CONFIG_DRIVE_STRENGTH:
868 ret = ns2_pin_get_strength(pctldev, pin, &arg);
871 *config = pinconf_to_config_packed(param, arg);
874 case PIN_CONFIG_SLEW_RATE:
875 ret = ns2_pin_get_slew(pctldev, pin, &arg);
878 *config = pinconf_to_config_packed(param, arg);
881 case PIN_CONFIG_INPUT_ENABLE:
882 ret = ns2_pin_get_enable(pctldev, pin);
893 static int ns2_pin_config_set(struct pinctrl_dev *pctrldev, unsigned int pin,
894 unsigned long *configs, unsigned int num_configs)
896 struct ns2_pin *pin_data = pctrldev->desc->pins[pin].drv_data;
897 enum pin_config_param param;
902 if (pin_data->pin_conf.base == -1)
905 for (i = 0; i < num_configs; i++) {
906 param = pinconf_to_config_param(configs[i]);
907 arg = pinconf_to_config_argument(configs[i]);
910 case PIN_CONFIG_BIAS_DISABLE:
911 ret = ns2_pin_set_pull(pctrldev, pin, false, false);
916 case PIN_CONFIG_BIAS_PULL_UP:
917 ret = ns2_pin_set_pull(pctrldev, pin, true, false);
922 case PIN_CONFIG_BIAS_PULL_DOWN:
923 ret = ns2_pin_set_pull(pctrldev, pin, false, true);
928 case PIN_CONFIG_DRIVE_STRENGTH:
929 ret = ns2_pin_set_strength(pctrldev, pin, arg);
934 case PIN_CONFIG_SLEW_RATE:
935 ret = ns2_pin_set_slew(pctrldev, pin, arg);
940 case PIN_CONFIG_INPUT_ENABLE:
941 ret = ns2_pin_set_enable(pctrldev, pin, arg);
947 dev_err(pctrldev->dev, "invalid configuration\n");
954 static const struct pinmux_ops ns2_pinmux_ops = {
955 .get_functions_count = ns2_get_functions_count,
956 .get_function_name = ns2_get_function_name,
957 .get_function_groups = ns2_get_function_groups,
958 .set_mux = ns2_pinmux_enable,
961 static const struct pinconf_ops ns2_pinconf_ops = {
963 .pin_config_get = ns2_pin_config_get,
964 .pin_config_set = ns2_pin_config_set,
967 static struct pinctrl_desc ns2_pinctrl_desc = {
968 .name = "ns2-pinmux",
969 .pctlops = &ns2_pinctrl_ops,
970 .pmxops = &ns2_pinmux_ops,
971 .confops = &ns2_pinconf_ops,
974 static int ns2_mux_log_init(struct ns2_pinctrl *pinctrl)
976 struct ns2_mux_log *log;
979 pinctrl->mux_log = devm_kcalloc(pinctrl->dev, NS2_NUM_IOMUX,
980 sizeof(struct ns2_mux_log),
982 if (!pinctrl->mux_log)
985 for (i = 0; i < NS2_NUM_IOMUX; i++)
986 pinctrl->mux_log[i].is_configured = false;
987 /* Group 0 uses bit 31 in the IOMUX_PAD_FUNCTION_0 register */
988 log = &pinctrl->mux_log[0];
989 log->mux.base = NS2_PIN_MUX_BASE0;
995 * Groups 1 through 14 use two bits each in the
996 * IOMUX_PAD_FUNCTION_1 register starting with
999 for (i = 1; i < (NS2_NUM_IOMUX - NS2_NUM_PWM_MUX); i++) {
1000 log = &pinctrl->mux_log[i];
1001 log->mux.base = NS2_PIN_MUX_BASE0;
1002 log->mux.offset = NS2_MUX_PAD_FUNC1_OFFSET;
1003 log->mux.shift = 32 - (i * 2);
1008 * Groups 15 through 18 use one bit each in the
1011 for (i = 0; i < NS2_NUM_PWM_MUX; i++) {
1012 log = &pinctrl->mux_log[(NS2_NUM_IOMUX - NS2_NUM_PWM_MUX) + i];
1013 log->mux.base = NS2_PIN_MUX_BASE1;
1014 log->mux.offset = 0;
1021 static int ns2_pinmux_probe(struct platform_device *pdev)
1023 struct ns2_pinctrl *pinctrl;
1024 struct resource *res;
1026 struct pinctrl_pin_desc *pins;
1027 unsigned int num_pins = ARRAY_SIZE(ns2_pins);
1029 pinctrl = devm_kzalloc(&pdev->dev, sizeof(*pinctrl), GFP_KERNEL);
1033 pinctrl->dev = &pdev->dev;
1034 platform_set_drvdata(pdev, pinctrl);
1035 spin_lock_init(&pinctrl->lock);
1037 pinctrl->base0 = devm_platform_ioremap_resource(pdev, 0);
1038 if (IS_ERR(pinctrl->base0))
1039 return PTR_ERR(pinctrl->base0);
1041 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1044 pinctrl->base1 = devm_ioremap(&pdev->dev, res->start,
1045 resource_size(res));
1046 if (!pinctrl->base1) {
1047 dev_err(&pdev->dev, "unable to map I/O space\n");
1051 pinctrl->pinconf_base = devm_platform_ioremap_resource(pdev, 2);
1052 if (IS_ERR(pinctrl->pinconf_base))
1053 return PTR_ERR(pinctrl->pinconf_base);
1055 ret = ns2_mux_log_init(pinctrl);
1057 dev_err(&pdev->dev, "unable to initialize IOMUX log\n");
1061 pins = devm_kcalloc(&pdev->dev, num_pins, sizeof(*pins), GFP_KERNEL);
1065 for (i = 0; i < num_pins; i++) {
1066 pins[i].number = ns2_pins[i].pin;
1067 pins[i].name = ns2_pins[i].name;
1068 pins[i].drv_data = &ns2_pins[i];
1071 pinctrl->groups = ns2_pin_groups;
1072 pinctrl->num_groups = ARRAY_SIZE(ns2_pin_groups);
1073 pinctrl->functions = ns2_pin_functions;
1074 pinctrl->num_functions = ARRAY_SIZE(ns2_pin_functions);
1075 ns2_pinctrl_desc.pins = pins;
1076 ns2_pinctrl_desc.npins = num_pins;
1078 pinctrl->pctl = pinctrl_register(&ns2_pinctrl_desc, &pdev->dev,
1080 if (IS_ERR(pinctrl->pctl)) {
1081 dev_err(&pdev->dev, "unable to register IOMUX pinctrl\n");
1082 return PTR_ERR(pinctrl->pctl);
1088 static const struct of_device_id ns2_pinmux_of_match[] = {
1089 {.compatible = "brcm,ns2-pinmux"},
1093 static struct platform_driver ns2_pinmux_driver = {
1095 .name = "ns2-pinmux",
1096 .of_match_table = ns2_pinmux_of_match,
1098 .probe = ns2_pinmux_probe,
1101 static int __init ns2_pinmux_init(void)
1103 return platform_driver_register(&ns2_pinmux_driver);
1105 arch_initcall(ns2_pinmux_init);