2 * Generic device tree based pinctrl driver for one register per pin
3 * type pinmux controllers
5 * Copyright (C) 2012 Texas Instruments, Inc.
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <linux/init.h>
13 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/list.h>
20 #include <linux/of_device.h>
21 #include <linux/of_address.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/pinctrl/pinmux.h>
25 #include <linux/pinctrl/pinconf-generic.h>
30 #define DRIVER_NAME "pinctrl-single"
31 #define PCS_MUX_PINS_NAME "pinctrl-single,pins"
32 #define PCS_MUX_BITS_NAME "pinctrl-single,bits"
33 #define PCS_REG_NAME_LEN ((sizeof(unsigned long) * 2) + 3)
34 #define PCS_OFF_DISABLED ~0U
37 * struct pcs_pingroup - pingroups for a function
38 * @np: pingroup device node pointer
39 * @name: pingroup name
40 * @gpins: array of the pins in the group
41 * @ngpins: number of pins in the group
45 struct device_node
*np
;
49 struct list_head node
;
53 * struct pcs_func_vals - mux function register offset and value pair
54 * @reg: register virtual address
55 * @val: register value
57 struct pcs_func_vals
{
64 * struct pcs_conf_vals - pinconf parameter, pinconf register offset
65 * and value, enable, disable, mask
66 * @param: config parameter
67 * @val: user input bits in the pinconf register
68 * @enable: enable bits in the pinconf register
69 * @disable: disable bits in the pinconf register
70 * @mask: mask bits in the register value
72 struct pcs_conf_vals
{
73 enum pin_config_param param
;
81 * struct pcs_conf_type - pinconf property name, pinconf param pair
82 * @name: property name in DTS file
83 * @param: config parameter
85 struct pcs_conf_type
{
87 enum pin_config_param param
;
91 * struct pcs_function - pinctrl function
92 * @name: pinctrl function name
93 * @vals: register and vals array
94 * @nvals: number of entries in vals array
95 * @pgnames: array of pingroup names the function uses
96 * @npgnames: number of pingroup names the function uses
101 struct pcs_func_vals
*vals
;
103 const char **pgnames
;
105 struct pcs_conf_vals
*conf
;
107 struct list_head node
;
111 * struct pcs_gpiofunc_range - pin ranges with same mux value of gpio function
112 * @offset: offset base of pins
113 * @npins: number pins with the same mux value of gpio function
114 * @gpiofunc: mux value of gpio function
117 struct pcs_gpiofunc_range
{
121 struct list_head node
;
125 * struct pcs_data - wrapper for data needed by pinctrl framework
127 * @cur: index to current element
129 * REVISIT: We should be able to drop this eventually by adding
130 * support for registering pins individually in the pinctrl
131 * framework for those drivers that don't need a static array.
134 struct pinctrl_pin_desc
*pa
;
139 * struct pcs_name - register name for a pin
140 * @name: name of the pinctrl register
142 * REVISIT: We may want to make names optional in the pinctrl
143 * framework as some drivers may not care about pin names to
144 * avoid kernel bloat. The pin names can be deciphered by user
145 * space tools using debugfs based on the register address and
146 * SoC packaging information.
149 char name
[PCS_REG_NAME_LEN
];
153 * struct pcs_soc_data - SoC specific settings
154 * @flags: initial SoC specific PCS_FEAT_xxx values
156 struct pcs_soc_data
{
161 * struct pcs_device - pinctrl device instance
163 * @base: virtual address of the controller
164 * @size: size of the ioremapped area
166 * @pctl: pin controller device
167 * @flags: mask of PCS_FEAT_xxx values
168 * @mutex: mutex protecting the lists
169 * @width: bits per mux register
170 * @fmask: function register mask
171 * @fshift: function register shift
172 * @foff: value to turn mux off
173 * @fmax: max number of functions in fmask
174 * @bits_per_pin:number of bits per pin
175 * @names: array of register names for pins
176 * @pins: physical pins on the SoC
177 * @pgtree: pingroup index radix tree
178 * @ftree: function index radix tree
179 * @pingroups: list of pingroups
180 * @functions: list of functions
181 * @gpiofuncs: list of gpio functions
182 * @ngroups: number of pingroups
183 * @nfuncs: number of functions
184 * @desc: pin controller descriptor
185 * @read: register read function to use
186 * @write: register write function to use
189 struct resource
*res
;
193 struct pinctrl_dev
*pctl
;
195 #define PCS_FEAT_PINCONF (1 << 0)
203 unsigned bits_per_pin
;
204 struct pcs_name
*names
;
205 struct pcs_data pins
;
206 struct radix_tree_root pgtree
;
207 struct radix_tree_root ftree
;
208 struct list_head pingroups
;
209 struct list_head functions
;
210 struct list_head gpiofuncs
;
213 struct pinctrl_desc desc
;
214 unsigned (*read
)(void __iomem
*reg
);
215 void (*write
)(unsigned val
, void __iomem
*reg
);
218 #define PCS_HAS_PINCONF (pcs->flags & PCS_FEAT_PINCONF)
220 static int pcs_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
221 unsigned long *config
);
222 static int pcs_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
223 unsigned long *configs
, unsigned num_configs
);
225 static enum pin_config_param pcs_bias
[] = {
226 PIN_CONFIG_BIAS_PULL_DOWN
,
227 PIN_CONFIG_BIAS_PULL_UP
,
231 * REVISIT: Reads and writes could eventually use regmap or something
232 * generic. But at least on omaps, some mux registers are performance
233 * critical as they may need to be remuxed every time before and after
234 * idle. Adding tests for register access width for every read and
235 * write like regmap is doing is not desired, and caching the registers
236 * does not help in this case.
239 static unsigned __maybe_unused
pcs_readb(void __iomem
*reg
)
244 static unsigned __maybe_unused
pcs_readw(void __iomem
*reg
)
249 static unsigned __maybe_unused
pcs_readl(void __iomem
*reg
)
254 static void __maybe_unused
pcs_writeb(unsigned val
, void __iomem
*reg
)
259 static void __maybe_unused
pcs_writew(unsigned val
, void __iomem
*reg
)
264 static void __maybe_unused
pcs_writel(unsigned val
, void __iomem
*reg
)
269 static int pcs_get_groups_count(struct pinctrl_dev
*pctldev
)
271 struct pcs_device
*pcs
;
273 pcs
= pinctrl_dev_get_drvdata(pctldev
);
278 static const char *pcs_get_group_name(struct pinctrl_dev
*pctldev
,
281 struct pcs_device
*pcs
;
282 struct pcs_pingroup
*group
;
284 pcs
= pinctrl_dev_get_drvdata(pctldev
);
285 group
= radix_tree_lookup(&pcs
->pgtree
, gselector
);
287 dev_err(pcs
->dev
, "%s could not find pingroup%i\n",
288 __func__
, gselector
);
295 static int pcs_get_group_pins(struct pinctrl_dev
*pctldev
,
297 const unsigned **pins
,
300 struct pcs_device
*pcs
;
301 struct pcs_pingroup
*group
;
303 pcs
= pinctrl_dev_get_drvdata(pctldev
);
304 group
= radix_tree_lookup(&pcs
->pgtree
, gselector
);
306 dev_err(pcs
->dev
, "%s could not find pingroup%i\n",
307 __func__
, gselector
);
311 *pins
= group
->gpins
;
312 *npins
= group
->ngpins
;
317 static void pcs_pin_dbg_show(struct pinctrl_dev
*pctldev
,
321 struct pcs_device
*pcs
;
322 unsigned val
, mux_bytes
;
324 pcs
= pinctrl_dev_get_drvdata(pctldev
);
326 mux_bytes
= pcs
->width
/ BITS_PER_BYTE
;
327 val
= pcs
->read(pcs
->base
+ pin
* mux_bytes
);
329 seq_printf(s
, "%08x %s " , val
, DRIVER_NAME
);
332 static void pcs_dt_free_map(struct pinctrl_dev
*pctldev
,
333 struct pinctrl_map
*map
, unsigned num_maps
)
335 struct pcs_device
*pcs
;
337 pcs
= pinctrl_dev_get_drvdata(pctldev
);
338 devm_kfree(pcs
->dev
, map
);
341 static int pcs_dt_node_to_map(struct pinctrl_dev
*pctldev
,
342 struct device_node
*np_config
,
343 struct pinctrl_map
**map
, unsigned *num_maps
);
345 static const struct pinctrl_ops pcs_pinctrl_ops
= {
346 .get_groups_count
= pcs_get_groups_count
,
347 .get_group_name
= pcs_get_group_name
,
348 .get_group_pins
= pcs_get_group_pins
,
349 .pin_dbg_show
= pcs_pin_dbg_show
,
350 .dt_node_to_map
= pcs_dt_node_to_map
,
351 .dt_free_map
= pcs_dt_free_map
,
354 static int pcs_get_functions_count(struct pinctrl_dev
*pctldev
)
356 struct pcs_device
*pcs
;
358 pcs
= pinctrl_dev_get_drvdata(pctldev
);
363 static const char *pcs_get_function_name(struct pinctrl_dev
*pctldev
,
366 struct pcs_device
*pcs
;
367 struct pcs_function
*func
;
369 pcs
= pinctrl_dev_get_drvdata(pctldev
);
370 func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
372 dev_err(pcs
->dev
, "%s could not find function%i\n",
373 __func__
, fselector
);
380 static int pcs_get_function_groups(struct pinctrl_dev
*pctldev
,
382 const char * const **groups
,
383 unsigned * const ngroups
)
385 struct pcs_device
*pcs
;
386 struct pcs_function
*func
;
388 pcs
= pinctrl_dev_get_drvdata(pctldev
);
389 func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
391 dev_err(pcs
->dev
, "%s could not find function%i\n",
392 __func__
, fselector
);
395 *groups
= func
->pgnames
;
396 *ngroups
= func
->npgnames
;
401 static int pcs_get_function(struct pinctrl_dev
*pctldev
, unsigned pin
,
402 struct pcs_function
**func
)
404 struct pcs_device
*pcs
= pinctrl_dev_get_drvdata(pctldev
);
405 struct pin_desc
*pdesc
= pin_desc_get(pctldev
, pin
);
406 const struct pinctrl_setting_mux
*setting
;
409 /* If pin is not described in DTS & enabled, mux_setting is NULL. */
410 setting
= pdesc
->mux_setting
;
413 fselector
= setting
->func
;
414 *func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
416 dev_err(pcs
->dev
, "%s could not find function%i\n",
417 __func__
, fselector
);
423 static int pcs_enable(struct pinctrl_dev
*pctldev
, unsigned fselector
,
426 struct pcs_device
*pcs
;
427 struct pcs_function
*func
;
430 pcs
= pinctrl_dev_get_drvdata(pctldev
);
431 /* If function mask is null, needn't enable it. */
434 func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
438 dev_dbg(pcs
->dev
, "enabling %s function%i\n",
439 func
->name
, fselector
);
441 for (i
= 0; i
< func
->nvals
; i
++) {
442 struct pcs_func_vals
*vals
;
445 vals
= &func
->vals
[i
];
446 val
= pcs
->read(vals
->reg
);
448 if (pcs
->bits_per_mux
)
454 val
|= (vals
->val
& mask
);
455 pcs
->write(val
, vals
->reg
);
461 static void pcs_disable(struct pinctrl_dev
*pctldev
, unsigned fselector
,
464 struct pcs_device
*pcs
;
465 struct pcs_function
*func
;
468 pcs
= pinctrl_dev_get_drvdata(pctldev
);
469 /* If function mask is null, needn't disable it. */
473 func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
475 dev_err(pcs
->dev
, "%s could not find function%i\n",
476 __func__
, fselector
);
481 * Ignore disable if function-off is not specified. Some hardware
482 * does not have clearly defined disable function. For pin specific
483 * off modes, you can use alternate named states as described in
484 * pinctrl-bindings.txt.
486 if (pcs
->foff
== PCS_OFF_DISABLED
) {
487 dev_dbg(pcs
->dev
, "ignoring disable for %s function%i\n",
488 func
->name
, fselector
);
492 dev_dbg(pcs
->dev
, "disabling function%i %s\n",
493 fselector
, func
->name
);
495 for (i
= 0; i
< func
->nvals
; i
++) {
496 struct pcs_func_vals
*vals
;
499 vals
= &func
->vals
[i
];
500 val
= pcs
->read(vals
->reg
);
502 val
|= pcs
->foff
<< pcs
->fshift
;
503 pcs
->write(val
, vals
->reg
);
507 static int pcs_request_gpio(struct pinctrl_dev
*pctldev
,
508 struct pinctrl_gpio_range
*range
, unsigned pin
)
510 struct pcs_device
*pcs
= pinctrl_dev_get_drvdata(pctldev
);
511 struct pcs_gpiofunc_range
*frange
= NULL
;
512 struct list_head
*pos
, *tmp
;
516 /* If function mask is null, return directly. */
520 list_for_each_safe(pos
, tmp
, &pcs
->gpiofuncs
) {
521 frange
= list_entry(pos
, struct pcs_gpiofunc_range
, node
);
522 if (pin
>= frange
->offset
+ frange
->npins
523 || pin
< frange
->offset
)
525 mux_bytes
= pcs
->width
/ BITS_PER_BYTE
;
526 data
= pcs
->read(pcs
->base
+ pin
* mux_bytes
) & ~pcs
->fmask
;
527 data
|= frange
->gpiofunc
;
528 pcs
->write(data
, pcs
->base
+ pin
* mux_bytes
);
534 static const struct pinmux_ops pcs_pinmux_ops
= {
535 .get_functions_count
= pcs_get_functions_count
,
536 .get_function_name
= pcs_get_function_name
,
537 .get_function_groups
= pcs_get_function_groups
,
538 .enable
= pcs_enable
,
539 .disable
= pcs_disable
,
540 .gpio_request_enable
= pcs_request_gpio
,
543 /* Clear BIAS value */
544 static void pcs_pinconf_clear_bias(struct pinctrl_dev
*pctldev
, unsigned pin
)
546 unsigned long config
;
548 for (i
= 0; i
< ARRAY_SIZE(pcs_bias
); i
++) {
549 config
= pinconf_to_config_packed(pcs_bias
[i
], 0);
550 pcs_pinconf_set(pctldev
, pin
, &config
, 1);
555 * Check whether PIN_CONFIG_BIAS_DISABLE is valid.
556 * It's depend on that PULL_DOWN & PULL_UP configs are all invalid.
558 static bool pcs_pinconf_bias_disable(struct pinctrl_dev
*pctldev
, unsigned pin
)
560 unsigned long config
;
563 for (i
= 0; i
< ARRAY_SIZE(pcs_bias
); i
++) {
564 config
= pinconf_to_config_packed(pcs_bias
[i
], 0);
565 if (!pcs_pinconf_get(pctldev
, pin
, &config
))
573 static int pcs_pinconf_get(struct pinctrl_dev
*pctldev
,
574 unsigned pin
, unsigned long *config
)
576 struct pcs_device
*pcs
= pinctrl_dev_get_drvdata(pctldev
);
577 struct pcs_function
*func
;
578 enum pin_config_param param
;
579 unsigned offset
= 0, data
= 0, i
, j
, ret
;
581 ret
= pcs_get_function(pctldev
, pin
, &func
);
585 for (i
= 0; i
< func
->nconfs
; i
++) {
586 param
= pinconf_to_config_param(*config
);
587 if (param
== PIN_CONFIG_BIAS_DISABLE
) {
588 if (pcs_pinconf_bias_disable(pctldev
, pin
)) {
594 } else if (param
!= func
->conf
[i
].param
) {
598 offset
= pin
* (pcs
->width
/ BITS_PER_BYTE
);
599 data
= pcs
->read(pcs
->base
+ offset
) & func
->conf
[i
].mask
;
600 switch (func
->conf
[i
].param
) {
602 case PIN_CONFIG_BIAS_PULL_DOWN
:
603 case PIN_CONFIG_BIAS_PULL_UP
:
604 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
605 if ((data
!= func
->conf
[i
].enable
) ||
606 (data
== func
->conf
[i
].disable
))
611 case PIN_CONFIG_INPUT_SCHMITT
:
612 for (j
= 0; j
< func
->nconfs
; j
++) {
613 switch (func
->conf
[j
].param
) {
614 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
615 if (data
!= func
->conf
[j
].enable
)
624 case PIN_CONFIG_DRIVE_STRENGTH
:
625 case PIN_CONFIG_SLEW_RATE
:
635 static int pcs_pinconf_set(struct pinctrl_dev
*pctldev
,
636 unsigned pin
, unsigned long *configs
,
637 unsigned num_configs
)
639 struct pcs_device
*pcs
= pinctrl_dev_get_drvdata(pctldev
);
640 struct pcs_function
*func
;
641 unsigned offset
= 0, shift
= 0, i
, data
, ret
;
645 ret
= pcs_get_function(pctldev
, pin
, &func
);
649 for (j
= 0; j
< num_configs
; j
++) {
650 for (i
= 0; i
< func
->nconfs
; i
++) {
651 if (pinconf_to_config_param(configs
[j
])
652 != func
->conf
[i
].param
)
655 offset
= pin
* (pcs
->width
/ BITS_PER_BYTE
);
656 data
= pcs
->read(pcs
->base
+ offset
);
657 arg
= pinconf_to_config_argument(configs
[j
]);
658 switch (func
->conf
[i
].param
) {
660 case PIN_CONFIG_INPUT_SCHMITT
:
661 case PIN_CONFIG_DRIVE_STRENGTH
:
662 case PIN_CONFIG_SLEW_RATE
:
663 shift
= ffs(func
->conf
[i
].mask
) - 1;
664 data
&= ~func
->conf
[i
].mask
;
665 data
|= (arg
<< shift
) & func
->conf
[i
].mask
;
668 case PIN_CONFIG_BIAS_DISABLE
:
669 pcs_pinconf_clear_bias(pctldev
, pin
);
671 case PIN_CONFIG_BIAS_PULL_DOWN
:
672 case PIN_CONFIG_BIAS_PULL_UP
:
674 pcs_pinconf_clear_bias(pctldev
, pin
);
676 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
677 data
&= ~func
->conf
[i
].mask
;
679 data
|= func
->conf
[i
].enable
;
681 data
|= func
->conf
[i
].disable
;
686 pcs
->write(data
, pcs
->base
+ offset
);
690 if (i
>= func
->nconfs
)
692 } /* for each config */
697 static int pcs_pinconf_group_get(struct pinctrl_dev
*pctldev
,
698 unsigned group
, unsigned long *config
)
700 const unsigned *pins
;
701 unsigned npins
, old
= 0;
704 ret
= pcs_get_group_pins(pctldev
, group
, &pins
, &npins
);
707 for (i
= 0; i
< npins
; i
++) {
708 if (pcs_pinconf_get(pctldev
, pins
[i
], config
))
710 /* configs do not match between two pins */
711 if (i
&& (old
!= *config
))
718 static int pcs_pinconf_group_set(struct pinctrl_dev
*pctldev
,
719 unsigned group
, unsigned long *configs
,
720 unsigned num_configs
)
722 const unsigned *pins
;
726 ret
= pcs_get_group_pins(pctldev
, group
, &pins
, &npins
);
729 for (i
= 0; i
< npins
; i
++) {
730 if (pcs_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
))
736 static void pcs_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
737 struct seq_file
*s
, unsigned pin
)
741 static void pcs_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
742 struct seq_file
*s
, unsigned selector
)
746 static void pcs_pinconf_config_dbg_show(struct pinctrl_dev
*pctldev
,
748 unsigned long config
)
750 pinconf_generic_dump_config(pctldev
, s
, config
);
753 static const struct pinconf_ops pcs_pinconf_ops
= {
754 .pin_config_get
= pcs_pinconf_get
,
755 .pin_config_set
= pcs_pinconf_set
,
756 .pin_config_group_get
= pcs_pinconf_group_get
,
757 .pin_config_group_set
= pcs_pinconf_group_set
,
758 .pin_config_dbg_show
= pcs_pinconf_dbg_show
,
759 .pin_config_group_dbg_show
= pcs_pinconf_group_dbg_show
,
760 .pin_config_config_dbg_show
= pcs_pinconf_config_dbg_show
,
765 * pcs_add_pin() - add a pin to the static per controller pin array
766 * @pcs: pcs driver instance
767 * @offset: register offset from base
769 static int pcs_add_pin(struct pcs_device
*pcs
, unsigned offset
,
772 struct pinctrl_pin_desc
*pin
;
777 if (i
>= pcs
->desc
.npins
) {
778 dev_err(pcs
->dev
, "too many pins, max %i\n",
783 pin
= &pcs
->pins
.pa
[i
];
785 sprintf(pn
->name
, "%lx.%d",
786 (unsigned long)pcs
->res
->start
+ offset
, pin_pos
);
787 pin
->name
= pn
->name
;
795 * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
796 * @pcs: pcs driver instance
798 * In case of errors, resources are freed in pcs_free_resources.
800 * If your hardware needs holes in the address space, then just set
801 * up multiple driver instances.
803 static int pcs_allocate_pin_table(struct pcs_device
*pcs
)
805 int mux_bytes
, nr_pins
, i
;
806 int num_pins_in_register
= 0;
808 mux_bytes
= pcs
->width
/ BITS_PER_BYTE
;
810 if (pcs
->bits_per_mux
) {
811 pcs
->bits_per_pin
= fls(pcs
->fmask
);
812 nr_pins
= (pcs
->size
* BITS_PER_BYTE
) / pcs
->bits_per_pin
;
813 num_pins_in_register
= pcs
->width
/ pcs
->bits_per_pin
;
815 nr_pins
= pcs
->size
/ mux_bytes
;
818 dev_dbg(pcs
->dev
, "allocating %i pins\n", nr_pins
);
819 pcs
->pins
.pa
= devm_kzalloc(pcs
->dev
,
820 sizeof(*pcs
->pins
.pa
) * nr_pins
,
825 pcs
->names
= devm_kzalloc(pcs
->dev
,
826 sizeof(struct pcs_name
) * nr_pins
,
831 pcs
->desc
.pins
= pcs
->pins
.pa
;
832 pcs
->desc
.npins
= nr_pins
;
834 for (i
= 0; i
< pcs
->desc
.npins
; i
++) {
840 if (pcs
->bits_per_mux
) {
841 byte_num
= (pcs
->bits_per_pin
* i
) / BITS_PER_BYTE
;
842 offset
= (byte_num
/ mux_bytes
) * mux_bytes
;
843 pin_pos
= i
% num_pins_in_register
;
845 offset
= i
* mux_bytes
;
847 res
= pcs_add_pin(pcs
, offset
, pin_pos
);
849 dev_err(pcs
->dev
, "error adding pins: %i\n", res
);
858 * pcs_add_function() - adds a new function to the function list
859 * @pcs: pcs driver instance
860 * @np: device node of the mux entry
861 * @name: name of the function
862 * @vals: array of mux register value pairs used by the function
863 * @nvals: number of mux register value pairs
864 * @pgnames: array of pingroup names for the function
865 * @npgnames: number of pingroup names
867 static struct pcs_function
*pcs_add_function(struct pcs_device
*pcs
,
868 struct device_node
*np
,
870 struct pcs_func_vals
*vals
,
872 const char **pgnames
,
875 struct pcs_function
*function
;
877 function
= devm_kzalloc(pcs
->dev
, sizeof(*function
), GFP_KERNEL
);
881 function
->name
= name
;
882 function
->vals
= vals
;
883 function
->nvals
= nvals
;
884 function
->pgnames
= pgnames
;
885 function
->npgnames
= npgnames
;
887 mutex_lock(&pcs
->mutex
);
888 list_add_tail(&function
->node
, &pcs
->functions
);
889 radix_tree_insert(&pcs
->ftree
, pcs
->nfuncs
, function
);
891 mutex_unlock(&pcs
->mutex
);
896 static void pcs_remove_function(struct pcs_device
*pcs
,
897 struct pcs_function
*function
)
901 mutex_lock(&pcs
->mutex
);
902 for (i
= 0; i
< pcs
->nfuncs
; i
++) {
903 struct pcs_function
*found
;
905 found
= radix_tree_lookup(&pcs
->ftree
, i
);
906 if (found
== function
)
907 radix_tree_delete(&pcs
->ftree
, i
);
909 list_del(&function
->node
);
910 mutex_unlock(&pcs
->mutex
);
914 * pcs_add_pingroup() - add a pingroup to the pingroup list
915 * @pcs: pcs driver instance
916 * @np: device node of the mux entry
917 * @name: name of the pingroup
918 * @gpins: array of the pins that belong to the group
919 * @ngpins: number of pins in the group
921 static int pcs_add_pingroup(struct pcs_device
*pcs
,
922 struct device_node
*np
,
927 struct pcs_pingroup
*pingroup
;
929 pingroup
= devm_kzalloc(pcs
->dev
, sizeof(*pingroup
), GFP_KERNEL
);
933 pingroup
->name
= name
;
935 pingroup
->gpins
= gpins
;
936 pingroup
->ngpins
= ngpins
;
938 mutex_lock(&pcs
->mutex
);
939 list_add_tail(&pingroup
->node
, &pcs
->pingroups
);
940 radix_tree_insert(&pcs
->pgtree
, pcs
->ngroups
, pingroup
);
942 mutex_unlock(&pcs
->mutex
);
948 * pcs_get_pin_by_offset() - get a pin index based on the register offset
949 * @pcs: pcs driver instance
950 * @offset: register offset from the base
952 * Note that this is OK as long as the pins are in a static array.
954 static int pcs_get_pin_by_offset(struct pcs_device
*pcs
, unsigned offset
)
958 if (offset
>= pcs
->size
) {
959 dev_err(pcs
->dev
, "mux offset out of range: 0x%x (0x%x)\n",
964 if (pcs
->bits_per_mux
)
965 index
= (offset
* BITS_PER_BYTE
) / pcs
->bits_per_pin
;
967 index
= offset
/ (pcs
->width
/ BITS_PER_BYTE
);
973 * check whether data matches enable bits or disable bits
974 * Return value: 1 for matching enable bits, 0 for matching disable bits,
975 * and negative value for matching failure.
977 static int pcs_config_match(unsigned data
, unsigned enable
, unsigned disable
)
983 else if (data
== disable
)
988 static void add_config(struct pcs_conf_vals
**conf
, enum pin_config_param param
,
989 unsigned value
, unsigned enable
, unsigned disable
,
992 (*conf
)->param
= param
;
993 (*conf
)->val
= value
;
994 (*conf
)->enable
= enable
;
995 (*conf
)->disable
= disable
;
996 (*conf
)->mask
= mask
;
1000 static void add_setting(unsigned long **setting
, enum pin_config_param param
,
1003 **setting
= pinconf_to_config_packed(param
, arg
);
1007 /* add pinconf setting with 2 parameters */
1008 static void pcs_add_conf2(struct pcs_device
*pcs
, struct device_node
*np
,
1009 const char *name
, enum pin_config_param param
,
1010 struct pcs_conf_vals
**conf
, unsigned long **settings
)
1012 unsigned value
[2], shift
;
1015 ret
= of_property_read_u32_array(np
, name
, value
, 2);
1018 /* set value & mask */
1019 value
[0] &= value
[1];
1020 shift
= ffs(value
[1]) - 1;
1021 /* skip enable & disable */
1022 add_config(conf
, param
, value
[0], 0, 0, value
[1]);
1023 add_setting(settings
, param
, value
[0] >> shift
);
1026 /* add pinconf setting with 4 parameters */
1027 static void pcs_add_conf4(struct pcs_device
*pcs
, struct device_node
*np
,
1028 const char *name
, enum pin_config_param param
,
1029 struct pcs_conf_vals
**conf
, unsigned long **settings
)
1034 /* value to set, enable, disable, mask */
1035 ret
= of_property_read_u32_array(np
, name
, value
, 4);
1039 dev_err(pcs
->dev
, "mask field of the property can't be 0\n");
1042 value
[0] &= value
[3];
1043 value
[1] &= value
[3];
1044 value
[2] &= value
[3];
1045 ret
= pcs_config_match(value
[0], value
[1], value
[2]);
1047 dev_dbg(pcs
->dev
, "failed to match enable or disable bits\n");
1048 add_config(conf
, param
, value
[0], value
[1], value
[2], value
[3]);
1049 add_setting(settings
, param
, ret
);
1052 static int pcs_parse_pinconf(struct pcs_device
*pcs
, struct device_node
*np
,
1053 struct pcs_function
*func
,
1054 struct pinctrl_map
**map
)
1057 struct pinctrl_map
*m
= *map
;
1058 int i
= 0, nconfs
= 0;
1059 unsigned long *settings
= NULL
, *s
= NULL
;
1060 struct pcs_conf_vals
*conf
= NULL
;
1061 struct pcs_conf_type prop2
[] = {
1062 { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH
, },
1063 { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE
, },
1064 { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT
, },
1066 struct pcs_conf_type prop4
[] = {
1067 { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP
, },
1068 { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN
, },
1069 { "pinctrl-single,input-schmitt-enable",
1070 PIN_CONFIG_INPUT_SCHMITT_ENABLE
, },
1073 /* If pinconf isn't supported, don't parse properties in below. */
1074 if (!PCS_HAS_PINCONF
)
1077 /* cacluate how much properties are supported in current node */
1078 for (i
= 0; i
< ARRAY_SIZE(prop2
); i
++) {
1079 if (of_find_property(np
, prop2
[i
].name
, NULL
))
1082 for (i
= 0; i
< ARRAY_SIZE(prop4
); i
++) {
1083 if (of_find_property(np
, prop4
[i
].name
, NULL
))
1089 func
->conf
= devm_kzalloc(pcs
->dev
,
1090 sizeof(struct pcs_conf_vals
) * nconfs
,
1094 func
->nconfs
= nconfs
;
1095 conf
= &(func
->conf
[0]);
1097 settings
= devm_kzalloc(pcs
->dev
, sizeof(unsigned long) * nconfs
,
1103 for (i
= 0; i
< ARRAY_SIZE(prop2
); i
++)
1104 pcs_add_conf2(pcs
, np
, prop2
[i
].name
, prop2
[i
].param
,
1106 for (i
= 0; i
< ARRAY_SIZE(prop4
); i
++)
1107 pcs_add_conf4(pcs
, np
, prop4
[i
].name
, prop4
[i
].param
,
1109 m
->type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
1110 m
->data
.configs
.group_or_pin
= np
->name
;
1111 m
->data
.configs
.configs
= settings
;
1112 m
->data
.configs
.num_configs
= nconfs
;
1116 static void pcs_free_pingroups(struct pcs_device
*pcs
);
1119 * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
1120 * @pcs: pinctrl driver instance
1121 * @np: device node of the mux entry
1123 * @num_maps: number of map
1124 * @pgnames: pingroup names
1126 * Note that this binding currently supports only sets of one register + value.
1128 * Also note that this driver tries to avoid understanding pin and function
1129 * names because of the extra bloat they would cause especially in the case of
1130 * a large number of pins. This driver just sets what is specified for the board
1131 * in the .dts file. Further user space debugging tools can be developed to
1132 * decipher the pin and function names using debugfs.
1134 * If you are concerned about the boot time, set up the static pins in
1135 * the bootloader, and only set up selected pins as device tree entries.
1137 static int pcs_parse_one_pinctrl_entry(struct pcs_device
*pcs
,
1138 struct device_node
*np
,
1139 struct pinctrl_map
**map
,
1141 const char **pgnames
)
1143 struct pcs_func_vals
*vals
;
1145 int size
, rows
, *pins
, index
= 0, found
= 0, res
= -ENOMEM
;
1146 struct pcs_function
*function
;
1148 mux
= of_get_property(np
, PCS_MUX_PINS_NAME
, &size
);
1149 if ((!mux
) || (size
< sizeof(*mux
) * 2)) {
1150 dev_err(pcs
->dev
, "bad data for mux %s\n",
1155 size
/= sizeof(*mux
); /* Number of elements in array */
1158 vals
= devm_kzalloc(pcs
->dev
, sizeof(*vals
) * rows
, GFP_KERNEL
);
1162 pins
= devm_kzalloc(pcs
->dev
, sizeof(*pins
) * rows
, GFP_KERNEL
);
1166 while (index
< size
) {
1167 unsigned offset
, val
;
1170 offset
= be32_to_cpup(mux
+ index
++);
1171 val
= be32_to_cpup(mux
+ index
++);
1172 vals
[found
].reg
= pcs
->base
+ offset
;
1173 vals
[found
].val
= val
;
1175 pin
= pcs_get_pin_by_offset(pcs
, offset
);
1178 "could not add functions for %s %ux\n",
1182 pins
[found
++] = pin
;
1185 pgnames
[0] = np
->name
;
1186 function
= pcs_add_function(pcs
, np
, np
->name
, vals
, found
, pgnames
, 1);
1190 res
= pcs_add_pingroup(pcs
, np
, np
->name
, pins
, found
);
1194 (*map
)->type
= PIN_MAP_TYPE_MUX_GROUP
;
1195 (*map
)->data
.mux
.group
= np
->name
;
1196 (*map
)->data
.mux
.function
= np
->name
;
1198 if (PCS_HAS_PINCONF
) {
1199 res
= pcs_parse_pinconf(pcs
, np
, function
, map
);
1201 goto free_pingroups
;
1209 pcs_free_pingroups(pcs
);
1212 pcs_remove_function(pcs
, function
);
1215 devm_kfree(pcs
->dev
, pins
);
1218 devm_kfree(pcs
->dev
, vals
);
1223 #define PARAMS_FOR_BITS_PER_MUX 3
1225 static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device
*pcs
,
1226 struct device_node
*np
,
1227 struct pinctrl_map
**map
,
1229 const char **pgnames
)
1231 struct pcs_func_vals
*vals
;
1233 int size
, rows
, *pins
, index
= 0, found
= 0, res
= -ENOMEM
;
1235 struct pcs_function
*function
;
1237 mux
= of_get_property(np
, PCS_MUX_BITS_NAME
, &size
);
1240 dev_err(pcs
->dev
, "no valid property for %s\n", np
->name
);
1244 if (size
< (sizeof(*mux
) * PARAMS_FOR_BITS_PER_MUX
)) {
1245 dev_err(pcs
->dev
, "bad data for %s\n", np
->name
);
1249 /* Number of elements in array */
1250 size
/= sizeof(*mux
);
1252 rows
= size
/ PARAMS_FOR_BITS_PER_MUX
;
1253 npins_in_row
= pcs
->width
/ pcs
->bits_per_pin
;
1255 vals
= devm_kzalloc(pcs
->dev
, sizeof(*vals
) * rows
* npins_in_row
,
1260 pins
= devm_kzalloc(pcs
->dev
, sizeof(*pins
) * rows
* npins_in_row
,
1265 while (index
< size
) {
1266 unsigned offset
, val
;
1267 unsigned mask
, bit_pos
, val_pos
, mask_pos
, submask
;
1268 unsigned pin_num_from_lsb
;
1271 offset
= be32_to_cpup(mux
+ index
++);
1272 val
= be32_to_cpup(mux
+ index
++);
1273 mask
= be32_to_cpup(mux
+ index
++);
1275 /* Parse pins in each row from LSB */
1277 bit_pos
= ffs(mask
);
1278 pin_num_from_lsb
= bit_pos
/ pcs
->bits_per_pin
;
1279 mask_pos
= ((pcs
->fmask
) << (bit_pos
- 1));
1280 val_pos
= val
& mask_pos
;
1281 submask
= mask
& mask_pos
;
1284 if (submask
!= mask_pos
) {
1286 "Invalid submask 0x%x for %s at 0x%x\n",
1287 submask
, np
->name
, offset
);
1291 vals
[found
].mask
= submask
;
1292 vals
[found
].reg
= pcs
->base
+ offset
;
1293 vals
[found
].val
= val_pos
;
1295 pin
= pcs_get_pin_by_offset(pcs
, offset
);
1298 "could not add functions for %s %ux\n",
1302 pins
[found
++] = pin
+ pin_num_from_lsb
;
1306 pgnames
[0] = np
->name
;
1307 function
= pcs_add_function(pcs
, np
, np
->name
, vals
, found
, pgnames
, 1);
1311 res
= pcs_add_pingroup(pcs
, np
, np
->name
, pins
, found
);
1315 (*map
)->type
= PIN_MAP_TYPE_MUX_GROUP
;
1316 (*map
)->data
.mux
.group
= np
->name
;
1317 (*map
)->data
.mux
.function
= np
->name
;
1319 if (PCS_HAS_PINCONF
) {
1320 dev_err(pcs
->dev
, "pinconf not supported\n");
1321 goto free_pingroups
;
1328 pcs_free_pingroups(pcs
);
1331 pcs_remove_function(pcs
, function
);
1334 devm_kfree(pcs
->dev
, pins
);
1337 devm_kfree(pcs
->dev
, vals
);
1342 * pcs_dt_node_to_map() - allocates and parses pinctrl maps
1343 * @pctldev: pinctrl instance
1344 * @np_config: device tree pinmux entry
1345 * @map: array of map entries
1346 * @num_maps: number of maps
1348 static int pcs_dt_node_to_map(struct pinctrl_dev
*pctldev
,
1349 struct device_node
*np_config
,
1350 struct pinctrl_map
**map
, unsigned *num_maps
)
1352 struct pcs_device
*pcs
;
1353 const char **pgnames
;
1356 pcs
= pinctrl_dev_get_drvdata(pctldev
);
1358 /* create 2 maps. One is for pinmux, and the other is for pinconf. */
1359 *map
= devm_kzalloc(pcs
->dev
, sizeof(**map
) * 2, GFP_KERNEL
);
1365 pgnames
= devm_kzalloc(pcs
->dev
, sizeof(*pgnames
), GFP_KERNEL
);
1371 if (pcs
->bits_per_mux
) {
1372 ret
= pcs_parse_bits_in_pinctrl_entry(pcs
, np_config
, map
,
1375 dev_err(pcs
->dev
, "no pins entries for %s\n",
1380 ret
= pcs_parse_one_pinctrl_entry(pcs
, np_config
, map
,
1383 dev_err(pcs
->dev
, "no pins entries for %s\n",
1392 devm_kfree(pcs
->dev
, pgnames
);
1394 devm_kfree(pcs
->dev
, *map
);
1400 * pcs_free_funcs() - free memory used by functions
1401 * @pcs: pcs driver instance
1403 static void pcs_free_funcs(struct pcs_device
*pcs
)
1405 struct list_head
*pos
, *tmp
;
1408 mutex_lock(&pcs
->mutex
);
1409 for (i
= 0; i
< pcs
->nfuncs
; i
++) {
1410 struct pcs_function
*func
;
1412 func
= radix_tree_lookup(&pcs
->ftree
, i
);
1415 radix_tree_delete(&pcs
->ftree
, i
);
1417 list_for_each_safe(pos
, tmp
, &pcs
->functions
) {
1418 struct pcs_function
*function
;
1420 function
= list_entry(pos
, struct pcs_function
, node
);
1421 list_del(&function
->node
);
1423 mutex_unlock(&pcs
->mutex
);
1427 * pcs_free_pingroups() - free memory used by pingroups
1428 * @pcs: pcs driver instance
1430 static void pcs_free_pingroups(struct pcs_device
*pcs
)
1432 struct list_head
*pos
, *tmp
;
1435 mutex_lock(&pcs
->mutex
);
1436 for (i
= 0; i
< pcs
->ngroups
; i
++) {
1437 struct pcs_pingroup
*pingroup
;
1439 pingroup
= radix_tree_lookup(&pcs
->pgtree
, i
);
1442 radix_tree_delete(&pcs
->pgtree
, i
);
1444 list_for_each_safe(pos
, tmp
, &pcs
->pingroups
) {
1445 struct pcs_pingroup
*pingroup
;
1447 pingroup
= list_entry(pos
, struct pcs_pingroup
, node
);
1448 list_del(&pingroup
->node
);
1450 mutex_unlock(&pcs
->mutex
);
1454 * pcs_free_resources() - free memory used by this driver
1455 * @pcs: pcs driver instance
1457 static void pcs_free_resources(struct pcs_device
*pcs
)
1460 pinctrl_unregister(pcs
->pctl
);
1462 pcs_free_funcs(pcs
);
1463 pcs_free_pingroups(pcs
);
1466 #define PCS_GET_PROP_U32(name, reg, err) \
1468 ret = of_property_read_u32(np, name, reg); \
1470 dev_err(pcs->dev, err); \
1475 static struct of_device_id pcs_of_match
[];
1477 static int pcs_add_gpio_func(struct device_node
*node
, struct pcs_device
*pcs
)
1479 const char *propname
= "pinctrl-single,gpio-range";
1480 const char *cellname
= "#pinctrl-single,gpio-range-cells";
1481 struct of_phandle_args gpiospec
;
1482 struct pcs_gpiofunc_range
*range
;
1485 for (i
= 0; ; i
++) {
1486 ret
= of_parse_phandle_with_args(node
, propname
, cellname
,
1488 /* Do not treat it as error. Only treat it as end condition. */
1493 range
= devm_kzalloc(pcs
->dev
, sizeof(*range
), GFP_KERNEL
);
1498 range
->offset
= gpiospec
.args
[0];
1499 range
->npins
= gpiospec
.args
[1];
1500 range
->gpiofunc
= gpiospec
.args
[2];
1501 mutex_lock(&pcs
->mutex
);
1502 list_add_tail(&range
->node
, &pcs
->gpiofuncs
);
1503 mutex_unlock(&pcs
->mutex
);
1509 static int pinctrl_single_suspend(struct platform_device
*pdev
,
1512 struct pcs_device
*pcs
;
1514 pcs
= platform_get_drvdata(pdev
);
1518 return pinctrl_force_sleep(pcs
->pctl
);
1521 static int pinctrl_single_resume(struct platform_device
*pdev
)
1523 struct pcs_device
*pcs
;
1525 pcs
= platform_get_drvdata(pdev
);
1529 return pinctrl_force_default(pcs
->pctl
);
1533 static int pcs_probe(struct platform_device
*pdev
)
1535 struct device_node
*np
= pdev
->dev
.of_node
;
1536 const struct of_device_id
*match
;
1537 struct resource
*res
;
1538 struct pcs_device
*pcs
;
1539 const struct pcs_soc_data
*soc
;
1542 match
= of_match_device(pcs_of_match
, &pdev
->dev
);
1546 pcs
= devm_kzalloc(&pdev
->dev
, sizeof(*pcs
), GFP_KERNEL
);
1548 dev_err(&pdev
->dev
, "could not allocate\n");
1551 pcs
->dev
= &pdev
->dev
;
1552 mutex_init(&pcs
->mutex
);
1553 INIT_LIST_HEAD(&pcs
->pingroups
);
1554 INIT_LIST_HEAD(&pcs
->functions
);
1555 INIT_LIST_HEAD(&pcs
->gpiofuncs
);
1557 pcs
->flags
= soc
->flags
;
1559 PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs
->width
,
1560 "register width not specified\n");
1562 ret
= of_property_read_u32(np
, "pinctrl-single,function-mask",
1565 pcs
->fshift
= ffs(pcs
->fmask
) - 1;
1566 pcs
->fmax
= pcs
->fmask
>> pcs
->fshift
;
1568 /* If mask property doesn't exist, function mux is invalid. */
1574 ret
= of_property_read_u32(np
, "pinctrl-single,function-off",
1577 pcs
->foff
= PCS_OFF_DISABLED
;
1579 pcs
->bits_per_mux
= of_property_read_bool(np
,
1580 "pinctrl-single,bit-per-mux");
1582 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1584 dev_err(pcs
->dev
, "could not get resource\n");
1588 pcs
->res
= devm_request_mem_region(pcs
->dev
, res
->start
,
1589 resource_size(res
), DRIVER_NAME
);
1591 dev_err(pcs
->dev
, "could not get mem_region\n");
1595 pcs
->size
= resource_size(pcs
->res
);
1596 pcs
->base
= devm_ioremap(pcs
->dev
, pcs
->res
->start
, pcs
->size
);
1598 dev_err(pcs
->dev
, "could not ioremap\n");
1602 INIT_RADIX_TREE(&pcs
->pgtree
, GFP_KERNEL
);
1603 INIT_RADIX_TREE(&pcs
->ftree
, GFP_KERNEL
);
1604 platform_set_drvdata(pdev
, pcs
);
1606 switch (pcs
->width
) {
1608 pcs
->read
= pcs_readb
;
1609 pcs
->write
= pcs_writeb
;
1612 pcs
->read
= pcs_readw
;
1613 pcs
->write
= pcs_writew
;
1616 pcs
->read
= pcs_readl
;
1617 pcs
->write
= pcs_writel
;
1623 pcs
->desc
.name
= DRIVER_NAME
;
1624 pcs
->desc
.pctlops
= &pcs_pinctrl_ops
;
1625 pcs
->desc
.pmxops
= &pcs_pinmux_ops
;
1626 if (PCS_HAS_PINCONF
)
1627 pcs
->desc
.confops
= &pcs_pinconf_ops
;
1628 pcs
->desc
.owner
= THIS_MODULE
;
1630 ret
= pcs_allocate_pin_table(pcs
);
1634 pcs
->pctl
= pinctrl_register(&pcs
->desc
, pcs
->dev
, pcs
);
1636 dev_err(pcs
->dev
, "could not register single pinctrl driver\n");
1641 ret
= pcs_add_gpio_func(np
, pcs
);
1645 dev_info(pcs
->dev
, "%i pins at pa %p size %u\n",
1646 pcs
->desc
.npins
, pcs
->base
, pcs
->size
);
1651 pcs_free_resources(pcs
);
1656 static int pcs_remove(struct platform_device
*pdev
)
1658 struct pcs_device
*pcs
= platform_get_drvdata(pdev
);
1663 pcs_free_resources(pcs
);
1668 static const struct pcs_soc_data pinctrl_single
= {
1671 static const struct pcs_soc_data pinconf_single
= {
1672 .flags
= PCS_FEAT_PINCONF
,
1675 static struct of_device_id pcs_of_match
[] = {
1676 { .compatible
= "pinctrl-single", .data
= &pinctrl_single
},
1677 { .compatible
= "pinconf-single", .data
= &pinconf_single
},
1680 MODULE_DEVICE_TABLE(of
, pcs_of_match
);
1682 static struct platform_driver pcs_driver
= {
1684 .remove
= pcs_remove
,
1686 .owner
= THIS_MODULE
,
1687 .name
= DRIVER_NAME
,
1688 .of_match_table
= pcs_of_match
,
1691 .suspend
= pinctrl_single_suspend
,
1692 .resume
= pinctrl_single_resume
,
1696 module_platform_driver(pcs_driver
);
1698 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
1699 MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
1700 MODULE_LICENSE("GPL v2");