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_device - pinctrl device instance
155 * @base: virtual address of the controller
156 * @size: size of the ioremapped area
158 * @pctl: pin controller device
159 * @mutex: mutex protecting the lists
160 * @width: bits per mux register
161 * @fmask: function register mask
162 * @fshift: function register shift
163 * @foff: value to turn mux off
164 * @fmax: max number of functions in fmask
165 * @is_pinconf: whether supports pinconf
166 * @bits_per_pin:number of bits per pin
167 * @names: array of register names for pins
168 * @pins: physical pins on the SoC
169 * @pgtree: pingroup index radix tree
170 * @ftree: function index radix tree
171 * @pingroups: list of pingroups
172 * @functions: list of functions
173 * @gpiofuncs: list of gpio functions
174 * @ngroups: number of pingroups
175 * @nfuncs: number of functions
176 * @desc: pin controller descriptor
177 * @read: register read function to use
178 * @write: register write function to use
181 struct resource
*res
;
185 struct pinctrl_dev
*pctl
;
194 unsigned bits_per_pin
;
195 struct pcs_name
*names
;
196 struct pcs_data pins
;
197 struct radix_tree_root pgtree
;
198 struct radix_tree_root ftree
;
199 struct list_head pingroups
;
200 struct list_head functions
;
201 struct list_head gpiofuncs
;
204 struct pinctrl_desc desc
;
205 unsigned (*read
)(void __iomem
*reg
);
206 void (*write
)(unsigned val
, void __iomem
*reg
);
209 static int pcs_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned pin
,
210 unsigned long *config
);
211 static int pcs_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned pin
,
212 unsigned long config
);
214 static enum pin_config_param pcs_bias
[] = {
215 PIN_CONFIG_BIAS_PULL_DOWN
,
216 PIN_CONFIG_BIAS_PULL_UP
,
220 * REVISIT: Reads and writes could eventually use regmap or something
221 * generic. But at least on omaps, some mux registers are performance
222 * critical as they may need to be remuxed every time before and after
223 * idle. Adding tests for register access width for every read and
224 * write like regmap is doing is not desired, and caching the registers
225 * does not help in this case.
228 static unsigned __maybe_unused
pcs_readb(void __iomem
*reg
)
233 static unsigned __maybe_unused
pcs_readw(void __iomem
*reg
)
238 static unsigned __maybe_unused
pcs_readl(void __iomem
*reg
)
243 static void __maybe_unused
pcs_writeb(unsigned val
, void __iomem
*reg
)
248 static void __maybe_unused
pcs_writew(unsigned val
, void __iomem
*reg
)
253 static void __maybe_unused
pcs_writel(unsigned val
, void __iomem
*reg
)
258 static int pcs_get_groups_count(struct pinctrl_dev
*pctldev
)
260 struct pcs_device
*pcs
;
262 pcs
= pinctrl_dev_get_drvdata(pctldev
);
267 static const char *pcs_get_group_name(struct pinctrl_dev
*pctldev
,
270 struct pcs_device
*pcs
;
271 struct pcs_pingroup
*group
;
273 pcs
= pinctrl_dev_get_drvdata(pctldev
);
274 group
= radix_tree_lookup(&pcs
->pgtree
, gselector
);
276 dev_err(pcs
->dev
, "%s could not find pingroup%i\n",
277 __func__
, gselector
);
284 static int pcs_get_group_pins(struct pinctrl_dev
*pctldev
,
286 const unsigned **pins
,
289 struct pcs_device
*pcs
;
290 struct pcs_pingroup
*group
;
292 pcs
= pinctrl_dev_get_drvdata(pctldev
);
293 group
= radix_tree_lookup(&pcs
->pgtree
, gselector
);
295 dev_err(pcs
->dev
, "%s could not find pingroup%i\n",
296 __func__
, gselector
);
300 *pins
= group
->gpins
;
301 *npins
= group
->ngpins
;
306 static void pcs_pin_dbg_show(struct pinctrl_dev
*pctldev
,
310 struct pcs_device
*pcs
;
311 unsigned val
, mux_bytes
;
313 pcs
= pinctrl_dev_get_drvdata(pctldev
);
315 mux_bytes
= pcs
->width
/ BITS_PER_BYTE
;
316 val
= pcs
->read(pcs
->base
+ pin
* mux_bytes
);
318 seq_printf(s
, "%08x %s " , val
, DRIVER_NAME
);
321 static void pcs_dt_free_map(struct pinctrl_dev
*pctldev
,
322 struct pinctrl_map
*map
, unsigned num_maps
)
324 struct pcs_device
*pcs
;
326 pcs
= pinctrl_dev_get_drvdata(pctldev
);
327 devm_kfree(pcs
->dev
, map
);
330 static int pcs_dt_node_to_map(struct pinctrl_dev
*pctldev
,
331 struct device_node
*np_config
,
332 struct pinctrl_map
**map
, unsigned *num_maps
);
334 static const struct pinctrl_ops pcs_pinctrl_ops
= {
335 .get_groups_count
= pcs_get_groups_count
,
336 .get_group_name
= pcs_get_group_name
,
337 .get_group_pins
= pcs_get_group_pins
,
338 .pin_dbg_show
= pcs_pin_dbg_show
,
339 .dt_node_to_map
= pcs_dt_node_to_map
,
340 .dt_free_map
= pcs_dt_free_map
,
343 static int pcs_get_functions_count(struct pinctrl_dev
*pctldev
)
345 struct pcs_device
*pcs
;
347 pcs
= pinctrl_dev_get_drvdata(pctldev
);
352 static const char *pcs_get_function_name(struct pinctrl_dev
*pctldev
,
355 struct pcs_device
*pcs
;
356 struct pcs_function
*func
;
358 pcs
= pinctrl_dev_get_drvdata(pctldev
);
359 func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
361 dev_err(pcs
->dev
, "%s could not find function%i\n",
362 __func__
, fselector
);
369 static int pcs_get_function_groups(struct pinctrl_dev
*pctldev
,
371 const char * const **groups
,
372 unsigned * const ngroups
)
374 struct pcs_device
*pcs
;
375 struct pcs_function
*func
;
377 pcs
= pinctrl_dev_get_drvdata(pctldev
);
378 func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
380 dev_err(pcs
->dev
, "%s could not find function%i\n",
381 __func__
, fselector
);
384 *groups
= func
->pgnames
;
385 *ngroups
= func
->npgnames
;
390 static int pcs_get_function(struct pinctrl_dev
*pctldev
, unsigned pin
,
391 struct pcs_function
**func
)
393 struct pcs_device
*pcs
= pinctrl_dev_get_drvdata(pctldev
);
394 struct pin_desc
*pdesc
= pin_desc_get(pctldev
, pin
);
395 const struct pinctrl_setting_mux
*setting
;
398 /* If pin is not described in DTS & enabled, mux_setting is NULL. */
399 setting
= pdesc
->mux_setting
;
402 fselector
= setting
->func
;
403 *func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
405 dev_err(pcs
->dev
, "%s could not find function%i\n",
406 __func__
, fselector
);
412 static int pcs_enable(struct pinctrl_dev
*pctldev
, unsigned fselector
,
415 struct pcs_device
*pcs
;
416 struct pcs_function
*func
;
419 pcs
= pinctrl_dev_get_drvdata(pctldev
);
420 /* If function mask is null, needn't enable it. */
423 func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
427 dev_dbg(pcs
->dev
, "enabling %s function%i\n",
428 func
->name
, fselector
);
430 for (i
= 0; i
< func
->nvals
; i
++) {
431 struct pcs_func_vals
*vals
;
434 vals
= &func
->vals
[i
];
435 val
= pcs
->read(vals
->reg
);
437 if (pcs
->bits_per_mux
)
443 val
|= (vals
->val
& mask
);
444 pcs
->write(val
, vals
->reg
);
450 static void pcs_disable(struct pinctrl_dev
*pctldev
, unsigned fselector
,
453 struct pcs_device
*pcs
;
454 struct pcs_function
*func
;
457 pcs
= pinctrl_dev_get_drvdata(pctldev
);
458 /* If function mask is null, needn't disable it. */
462 func
= radix_tree_lookup(&pcs
->ftree
, fselector
);
464 dev_err(pcs
->dev
, "%s could not find function%i\n",
465 __func__
, fselector
);
470 * Ignore disable if function-off is not specified. Some hardware
471 * does not have clearly defined disable function. For pin specific
472 * off modes, you can use alternate named states as described in
473 * pinctrl-bindings.txt.
475 if (pcs
->foff
== PCS_OFF_DISABLED
) {
476 dev_dbg(pcs
->dev
, "ignoring disable for %s function%i\n",
477 func
->name
, fselector
);
481 dev_dbg(pcs
->dev
, "disabling function%i %s\n",
482 fselector
, func
->name
);
484 for (i
= 0; i
< func
->nvals
; i
++) {
485 struct pcs_func_vals
*vals
;
488 vals
= &func
->vals
[i
];
489 val
= pcs
->read(vals
->reg
);
491 val
|= pcs
->foff
<< pcs
->fshift
;
492 pcs
->write(val
, vals
->reg
);
496 static int pcs_request_gpio(struct pinctrl_dev
*pctldev
,
497 struct pinctrl_gpio_range
*range
, unsigned pin
)
499 struct pcs_device
*pcs
= pinctrl_dev_get_drvdata(pctldev
);
500 struct pcs_gpiofunc_range
*frange
= NULL
;
501 struct list_head
*pos
, *tmp
;
505 /* If function mask is null, return directly. */
509 list_for_each_safe(pos
, tmp
, &pcs
->gpiofuncs
) {
510 frange
= list_entry(pos
, struct pcs_gpiofunc_range
, node
);
511 if (pin
>= frange
->offset
+ frange
->npins
512 || pin
< frange
->offset
)
514 mux_bytes
= pcs
->width
/ BITS_PER_BYTE
;
515 data
= pcs
->read(pcs
->base
+ pin
* mux_bytes
) & ~pcs
->fmask
;
516 data
|= frange
->gpiofunc
;
517 pcs
->write(data
, pcs
->base
+ pin
* mux_bytes
);
523 static const struct pinmux_ops pcs_pinmux_ops
= {
524 .get_functions_count
= pcs_get_functions_count
,
525 .get_function_name
= pcs_get_function_name
,
526 .get_function_groups
= pcs_get_function_groups
,
527 .enable
= pcs_enable
,
528 .disable
= pcs_disable
,
529 .gpio_request_enable
= pcs_request_gpio
,
532 /* Clear BIAS value */
533 static void pcs_pinconf_clear_bias(struct pinctrl_dev
*pctldev
, unsigned pin
)
535 unsigned long config
;
537 for (i
= 0; i
< ARRAY_SIZE(pcs_bias
); i
++) {
538 config
= pinconf_to_config_packed(pcs_bias
[i
], 0);
539 pcs_pinconf_set(pctldev
, pin
, config
);
544 * Check whether PIN_CONFIG_BIAS_DISABLE is valid.
545 * It's depend on that PULL_DOWN & PULL_UP configs are all invalid.
547 static bool pcs_pinconf_bias_disable(struct pinctrl_dev
*pctldev
, unsigned pin
)
549 unsigned long config
;
552 for (i
= 0; i
< ARRAY_SIZE(pcs_bias
); i
++) {
553 config
= pinconf_to_config_packed(pcs_bias
[i
], 0);
554 if (!pcs_pinconf_get(pctldev
, pin
, &config
))
562 static int pcs_pinconf_get(struct pinctrl_dev
*pctldev
,
563 unsigned pin
, unsigned long *config
)
565 struct pcs_device
*pcs
= pinctrl_dev_get_drvdata(pctldev
);
566 struct pcs_function
*func
;
567 enum pin_config_param param
;
568 unsigned offset
= 0, data
= 0, i
, j
, ret
;
570 ret
= pcs_get_function(pctldev
, pin
, &func
);
574 for (i
= 0; i
< func
->nconfs
; i
++) {
575 param
= pinconf_to_config_param(*config
);
576 if (param
== PIN_CONFIG_BIAS_DISABLE
) {
577 if (pcs_pinconf_bias_disable(pctldev
, pin
)) {
583 } else if (param
!= func
->conf
[i
].param
) {
587 offset
= pin
* (pcs
->width
/ BITS_PER_BYTE
);
588 data
= pcs
->read(pcs
->base
+ offset
) & func
->conf
[i
].mask
;
589 switch (func
->conf
[i
].param
) {
591 case PIN_CONFIG_BIAS_PULL_DOWN
:
592 case PIN_CONFIG_BIAS_PULL_UP
:
593 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
594 if ((data
!= func
->conf
[i
].enable
) ||
595 (data
== func
->conf
[i
].disable
))
600 case PIN_CONFIG_INPUT_SCHMITT
:
601 for (j
= 0; j
< func
->nconfs
; j
++) {
602 switch (func
->conf
[j
].param
) {
603 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
604 if (data
!= func
->conf
[j
].enable
)
613 case PIN_CONFIG_DRIVE_STRENGTH
:
614 case PIN_CONFIG_SLEW_RATE
:
624 static int pcs_pinconf_set(struct pinctrl_dev
*pctldev
,
625 unsigned pin
, unsigned long config
)
627 struct pcs_device
*pcs
= pinctrl_dev_get_drvdata(pctldev
);
628 struct pcs_function
*func
;
629 unsigned offset
= 0, shift
= 0, i
, data
, ret
;
632 ret
= pcs_get_function(pctldev
, pin
, &func
);
636 for (i
= 0; i
< func
->nconfs
; i
++) {
637 if (pinconf_to_config_param(config
) == func
->conf
[i
].param
) {
638 offset
= pin
* (pcs
->width
/ BITS_PER_BYTE
);
639 data
= pcs
->read(pcs
->base
+ offset
);
640 arg
= pinconf_to_config_argument(config
);
641 switch (func
->conf
[i
].param
) {
643 case PIN_CONFIG_INPUT_SCHMITT
:
644 case PIN_CONFIG_DRIVE_STRENGTH
:
645 case PIN_CONFIG_SLEW_RATE
:
646 shift
= ffs(func
->conf
[i
].mask
) - 1;
647 data
&= ~func
->conf
[i
].mask
;
648 data
|= (arg
<< shift
) & func
->conf
[i
].mask
;
651 case PIN_CONFIG_BIAS_DISABLE
:
652 pcs_pinconf_clear_bias(pctldev
, pin
);
654 case PIN_CONFIG_BIAS_PULL_DOWN
:
655 case PIN_CONFIG_BIAS_PULL_UP
:
657 pcs_pinconf_clear_bias(pctldev
, pin
);
659 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
660 data
&= ~func
->conf
[i
].mask
;
662 data
|= func
->conf
[i
].enable
;
664 data
|= func
->conf
[i
].disable
;
669 pcs
->write(data
, pcs
->base
+ offset
);
676 static int pcs_pinconf_group_get(struct pinctrl_dev
*pctldev
,
677 unsigned group
, unsigned long *config
)
679 const unsigned *pins
;
680 unsigned npins
, old
= 0;
683 ret
= pcs_get_group_pins(pctldev
, group
, &pins
, &npins
);
686 for (i
= 0; i
< npins
; i
++) {
687 if (pcs_pinconf_get(pctldev
, pins
[i
], config
))
689 /* configs do not match between two pins */
690 if (i
&& (old
!= *config
))
697 static int pcs_pinconf_group_set(struct pinctrl_dev
*pctldev
,
698 unsigned group
, unsigned long config
)
700 const unsigned *pins
;
704 ret
= pcs_get_group_pins(pctldev
, group
, &pins
, &npins
);
707 for (i
= 0; i
< npins
; i
++) {
708 if (pcs_pinconf_set(pctldev
, pins
[i
], config
))
714 static void pcs_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
715 struct seq_file
*s
, unsigned pin
)
719 static void pcs_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
720 struct seq_file
*s
, unsigned selector
)
724 static void pcs_pinconf_config_dbg_show(struct pinctrl_dev
*pctldev
,
726 unsigned long config
)
728 pinconf_generic_dump_config(pctldev
, s
, config
);
731 static const struct pinconf_ops pcs_pinconf_ops
= {
732 .pin_config_get
= pcs_pinconf_get
,
733 .pin_config_set
= pcs_pinconf_set
,
734 .pin_config_group_get
= pcs_pinconf_group_get
,
735 .pin_config_group_set
= pcs_pinconf_group_set
,
736 .pin_config_dbg_show
= pcs_pinconf_dbg_show
,
737 .pin_config_group_dbg_show
= pcs_pinconf_group_dbg_show
,
738 .pin_config_config_dbg_show
= pcs_pinconf_config_dbg_show
,
743 * pcs_add_pin() - add a pin to the static per controller pin array
744 * @pcs: pcs driver instance
745 * @offset: register offset from base
747 static int pcs_add_pin(struct pcs_device
*pcs
, unsigned offset
,
750 struct pinctrl_pin_desc
*pin
;
755 if (i
>= pcs
->desc
.npins
) {
756 dev_err(pcs
->dev
, "too many pins, max %i\n",
761 pin
= &pcs
->pins
.pa
[i
];
763 sprintf(pn
->name
, "%lx.%d",
764 (unsigned long)pcs
->res
->start
+ offset
, pin_pos
);
765 pin
->name
= pn
->name
;
773 * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
774 * @pcs: pcs driver instance
776 * In case of errors, resources are freed in pcs_free_resources.
778 * If your hardware needs holes in the address space, then just set
779 * up multiple driver instances.
781 static int pcs_allocate_pin_table(struct pcs_device
*pcs
)
783 int mux_bytes
, nr_pins
, i
;
784 int num_pins_in_register
= 0;
786 mux_bytes
= pcs
->width
/ BITS_PER_BYTE
;
788 if (pcs
->bits_per_mux
) {
789 pcs
->bits_per_pin
= fls(pcs
->fmask
);
790 nr_pins
= (pcs
->size
* BITS_PER_BYTE
) / pcs
->bits_per_pin
;
791 num_pins_in_register
= pcs
->width
/ pcs
->bits_per_pin
;
793 nr_pins
= pcs
->size
/ mux_bytes
;
796 dev_dbg(pcs
->dev
, "allocating %i pins\n", nr_pins
);
797 pcs
->pins
.pa
= devm_kzalloc(pcs
->dev
,
798 sizeof(*pcs
->pins
.pa
) * nr_pins
,
803 pcs
->names
= devm_kzalloc(pcs
->dev
,
804 sizeof(struct pcs_name
) * nr_pins
,
809 pcs
->desc
.pins
= pcs
->pins
.pa
;
810 pcs
->desc
.npins
= nr_pins
;
812 for (i
= 0; i
< pcs
->desc
.npins
; i
++) {
818 if (pcs
->bits_per_mux
) {
819 byte_num
= (pcs
->bits_per_pin
* i
) / BITS_PER_BYTE
;
820 offset
= (byte_num
/ mux_bytes
) * mux_bytes
;
821 pin_pos
= i
% num_pins_in_register
;
823 offset
= i
* mux_bytes
;
825 res
= pcs_add_pin(pcs
, offset
, pin_pos
);
827 dev_err(pcs
->dev
, "error adding pins: %i\n", res
);
836 * pcs_add_function() - adds a new function to the function list
837 * @pcs: pcs driver instance
838 * @np: device node of the mux entry
839 * @name: name of the function
840 * @vals: array of mux register value pairs used by the function
841 * @nvals: number of mux register value pairs
842 * @pgnames: array of pingroup names for the function
843 * @npgnames: number of pingroup names
845 static struct pcs_function
*pcs_add_function(struct pcs_device
*pcs
,
846 struct device_node
*np
,
848 struct pcs_func_vals
*vals
,
850 const char **pgnames
,
853 struct pcs_function
*function
;
855 function
= devm_kzalloc(pcs
->dev
, sizeof(*function
), GFP_KERNEL
);
859 function
->name
= name
;
860 function
->vals
= vals
;
861 function
->nvals
= nvals
;
862 function
->pgnames
= pgnames
;
863 function
->npgnames
= npgnames
;
865 mutex_lock(&pcs
->mutex
);
866 list_add_tail(&function
->node
, &pcs
->functions
);
867 radix_tree_insert(&pcs
->ftree
, pcs
->nfuncs
, function
);
869 mutex_unlock(&pcs
->mutex
);
874 static void pcs_remove_function(struct pcs_device
*pcs
,
875 struct pcs_function
*function
)
879 mutex_lock(&pcs
->mutex
);
880 for (i
= 0; i
< pcs
->nfuncs
; i
++) {
881 struct pcs_function
*found
;
883 found
= radix_tree_lookup(&pcs
->ftree
, i
);
884 if (found
== function
)
885 radix_tree_delete(&pcs
->ftree
, i
);
887 list_del(&function
->node
);
888 mutex_unlock(&pcs
->mutex
);
892 * pcs_add_pingroup() - add a pingroup to the pingroup list
893 * @pcs: pcs driver instance
894 * @np: device node of the mux entry
895 * @name: name of the pingroup
896 * @gpins: array of the pins that belong to the group
897 * @ngpins: number of pins in the group
899 static int pcs_add_pingroup(struct pcs_device
*pcs
,
900 struct device_node
*np
,
905 struct pcs_pingroup
*pingroup
;
907 pingroup
= devm_kzalloc(pcs
->dev
, sizeof(*pingroup
), GFP_KERNEL
);
911 pingroup
->name
= name
;
913 pingroup
->gpins
= gpins
;
914 pingroup
->ngpins
= ngpins
;
916 mutex_lock(&pcs
->mutex
);
917 list_add_tail(&pingroup
->node
, &pcs
->pingroups
);
918 radix_tree_insert(&pcs
->pgtree
, pcs
->ngroups
, pingroup
);
920 mutex_unlock(&pcs
->mutex
);
926 * pcs_get_pin_by_offset() - get a pin index based on the register offset
927 * @pcs: pcs driver instance
928 * @offset: register offset from the base
930 * Note that this is OK as long as the pins are in a static array.
932 static int pcs_get_pin_by_offset(struct pcs_device
*pcs
, unsigned offset
)
936 if (offset
>= pcs
->size
) {
937 dev_err(pcs
->dev
, "mux offset out of range: 0x%x (0x%x)\n",
942 if (pcs
->bits_per_mux
)
943 index
= (offset
* BITS_PER_BYTE
) / pcs
->bits_per_pin
;
945 index
= offset
/ (pcs
->width
/ BITS_PER_BYTE
);
951 * check whether data matches enable bits or disable bits
952 * Return value: 1 for matching enable bits, 0 for matching disable bits,
953 * and negative value for matching failure.
955 static int pcs_config_match(unsigned data
, unsigned enable
, unsigned disable
)
961 else if (data
== disable
)
966 static void add_config(struct pcs_conf_vals
**conf
, enum pin_config_param param
,
967 unsigned value
, unsigned enable
, unsigned disable
,
970 (*conf
)->param
= param
;
971 (*conf
)->val
= value
;
972 (*conf
)->enable
= enable
;
973 (*conf
)->disable
= disable
;
974 (*conf
)->mask
= mask
;
978 static void add_setting(unsigned long **setting
, enum pin_config_param param
,
981 **setting
= pinconf_to_config_packed(param
, arg
);
985 /* add pinconf setting with 2 parameters */
986 static void pcs_add_conf2(struct pcs_device
*pcs
, struct device_node
*np
,
987 const char *name
, enum pin_config_param param
,
988 struct pcs_conf_vals
**conf
, unsigned long **settings
)
990 unsigned value
[2], shift
;
993 ret
= of_property_read_u32_array(np
, name
, value
, 2);
996 /* set value & mask */
997 value
[0] &= value
[1];
998 shift
= ffs(value
[1]) - 1;
999 /* skip enable & disable */
1000 add_config(conf
, param
, value
[0], 0, 0, value
[1]);
1001 add_setting(settings
, param
, value
[0] >> shift
);
1004 /* add pinconf setting with 4 parameters */
1005 static void pcs_add_conf4(struct pcs_device
*pcs
, struct device_node
*np
,
1006 const char *name
, enum pin_config_param param
,
1007 struct pcs_conf_vals
**conf
, unsigned long **settings
)
1012 /* value to set, enable, disable, mask */
1013 ret
= of_property_read_u32_array(np
, name
, value
, 4);
1017 dev_err(pcs
->dev
, "mask field of the property can't be 0\n");
1020 value
[0] &= value
[3];
1021 value
[1] &= value
[3];
1022 value
[2] &= value
[3];
1023 ret
= pcs_config_match(value
[0], value
[1], value
[2]);
1025 dev_dbg(pcs
->dev
, "failed to match enable or disable bits\n");
1026 add_config(conf
, param
, value
[0], value
[1], value
[2], value
[3]);
1027 add_setting(settings
, param
, ret
);
1030 static int pcs_parse_pinconf(struct pcs_device
*pcs
, struct device_node
*np
,
1031 struct pcs_function
*func
,
1032 struct pinctrl_map
**map
)
1035 struct pinctrl_map
*m
= *map
;
1036 int i
= 0, nconfs
= 0;
1037 unsigned long *settings
= NULL
, *s
= NULL
;
1038 struct pcs_conf_vals
*conf
= NULL
;
1039 struct pcs_conf_type prop2
[] = {
1040 { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH
, },
1041 { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE
, },
1042 { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT
, },
1044 struct pcs_conf_type prop4
[] = {
1045 { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP
, },
1046 { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN
, },
1047 { "pinctrl-single,input-schmitt-enable",
1048 PIN_CONFIG_INPUT_SCHMITT_ENABLE
, },
1051 /* If pinconf isn't supported, don't parse properties in below. */
1052 if (!pcs
->is_pinconf
)
1055 /* cacluate how much properties are supported in current node */
1056 for (i
= 0; i
< ARRAY_SIZE(prop2
); i
++) {
1057 if (of_find_property(np
, prop2
[i
].name
, NULL
))
1060 for (i
= 0; i
< ARRAY_SIZE(prop4
); i
++) {
1061 if (of_find_property(np
, prop4
[i
].name
, NULL
))
1067 func
->conf
= devm_kzalloc(pcs
->dev
,
1068 sizeof(struct pcs_conf_vals
) * nconfs
,
1072 func
->nconfs
= nconfs
;
1073 conf
= &(func
->conf
[0]);
1075 settings
= devm_kzalloc(pcs
->dev
, sizeof(unsigned long) * nconfs
,
1081 for (i
= 0; i
< ARRAY_SIZE(prop2
); i
++)
1082 pcs_add_conf2(pcs
, np
, prop2
[i
].name
, prop2
[i
].param
,
1084 for (i
= 0; i
< ARRAY_SIZE(prop4
); i
++)
1085 pcs_add_conf4(pcs
, np
, prop4
[i
].name
, prop4
[i
].param
,
1087 m
->type
= PIN_MAP_TYPE_CONFIGS_GROUP
;
1088 m
->data
.configs
.group_or_pin
= np
->name
;
1089 m
->data
.configs
.configs
= settings
;
1090 m
->data
.configs
.num_configs
= nconfs
;
1094 static void pcs_free_pingroups(struct pcs_device
*pcs
);
1097 * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
1098 * @pcs: pinctrl driver instance
1099 * @np: device node of the mux entry
1101 * @num_maps: number of map
1102 * @pgnames: pingroup names
1104 * Note that this binding currently supports only sets of one register + value.
1106 * Also note that this driver tries to avoid understanding pin and function
1107 * names because of the extra bloat they would cause especially in the case of
1108 * a large number of pins. This driver just sets what is specified for the board
1109 * in the .dts file. Further user space debugging tools can be developed to
1110 * decipher the pin and function names using debugfs.
1112 * If you are concerned about the boot time, set up the static pins in
1113 * the bootloader, and only set up selected pins as device tree entries.
1115 static int pcs_parse_one_pinctrl_entry(struct pcs_device
*pcs
,
1116 struct device_node
*np
,
1117 struct pinctrl_map
**map
,
1119 const char **pgnames
)
1121 struct pcs_func_vals
*vals
;
1123 int size
, rows
, *pins
, index
= 0, found
= 0, res
= -ENOMEM
;
1124 struct pcs_function
*function
;
1126 mux
= of_get_property(np
, PCS_MUX_PINS_NAME
, &size
);
1127 if ((!mux
) || (size
< sizeof(*mux
) * 2)) {
1128 dev_err(pcs
->dev
, "bad data for mux %s\n",
1133 size
/= sizeof(*mux
); /* Number of elements in array */
1136 vals
= devm_kzalloc(pcs
->dev
, sizeof(*vals
) * rows
, GFP_KERNEL
);
1140 pins
= devm_kzalloc(pcs
->dev
, sizeof(*pins
) * rows
, GFP_KERNEL
);
1144 while (index
< size
) {
1145 unsigned offset
, val
;
1148 offset
= be32_to_cpup(mux
+ index
++);
1149 val
= be32_to_cpup(mux
+ index
++);
1150 vals
[found
].reg
= pcs
->base
+ offset
;
1151 vals
[found
].val
= val
;
1153 pin
= pcs_get_pin_by_offset(pcs
, offset
);
1156 "could not add functions for %s %ux\n",
1160 pins
[found
++] = pin
;
1163 pgnames
[0] = np
->name
;
1164 function
= pcs_add_function(pcs
, np
, np
->name
, vals
, found
, pgnames
, 1);
1168 res
= pcs_add_pingroup(pcs
, np
, np
->name
, pins
, found
);
1172 (*map
)->type
= PIN_MAP_TYPE_MUX_GROUP
;
1173 (*map
)->data
.mux
.group
= np
->name
;
1174 (*map
)->data
.mux
.function
= np
->name
;
1176 if (pcs
->is_pinconf
) {
1177 res
= pcs_parse_pinconf(pcs
, np
, function
, map
);
1179 goto free_pingroups
;
1187 pcs_free_pingroups(pcs
);
1190 pcs_remove_function(pcs
, function
);
1193 devm_kfree(pcs
->dev
, pins
);
1196 devm_kfree(pcs
->dev
, vals
);
1201 #define PARAMS_FOR_BITS_PER_MUX 3
1203 static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device
*pcs
,
1204 struct device_node
*np
,
1205 struct pinctrl_map
**map
,
1207 const char **pgnames
)
1209 struct pcs_func_vals
*vals
;
1211 int size
, rows
, *pins
, index
= 0, found
= 0, res
= -ENOMEM
;
1213 struct pcs_function
*function
;
1215 mux
= of_get_property(np
, PCS_MUX_BITS_NAME
, &size
);
1218 dev_err(pcs
->dev
, "no valid property for %s\n", np
->name
);
1222 if (size
< (sizeof(*mux
) * PARAMS_FOR_BITS_PER_MUX
)) {
1223 dev_err(pcs
->dev
, "bad data for %s\n", np
->name
);
1227 /* Number of elements in array */
1228 size
/= sizeof(*mux
);
1230 rows
= size
/ PARAMS_FOR_BITS_PER_MUX
;
1231 npins_in_row
= pcs
->width
/ pcs
->bits_per_pin
;
1233 vals
= devm_kzalloc(pcs
->dev
, sizeof(*vals
) * rows
* npins_in_row
,
1238 pins
= devm_kzalloc(pcs
->dev
, sizeof(*pins
) * rows
* npins_in_row
,
1243 while (index
< size
) {
1244 unsigned offset
, val
;
1245 unsigned mask
, bit_pos
, val_pos
, mask_pos
, submask
;
1246 unsigned pin_num_from_lsb
;
1249 offset
= be32_to_cpup(mux
+ index
++);
1250 val
= be32_to_cpup(mux
+ index
++);
1251 mask
= be32_to_cpup(mux
+ index
++);
1253 /* Parse pins in each row from LSB */
1255 bit_pos
= ffs(mask
);
1256 pin_num_from_lsb
= bit_pos
/ pcs
->bits_per_pin
;
1257 mask_pos
= ((pcs
->fmask
) << (bit_pos
- 1));
1258 val_pos
= val
& mask_pos
;
1259 submask
= mask
& mask_pos
;
1262 if (submask
!= mask_pos
) {
1264 "Invalid submask 0x%x for %s at 0x%x\n",
1265 submask
, np
->name
, offset
);
1269 vals
[found
].mask
= submask
;
1270 vals
[found
].reg
= pcs
->base
+ offset
;
1271 vals
[found
].val
= val_pos
;
1273 pin
= pcs_get_pin_by_offset(pcs
, offset
);
1276 "could not add functions for %s %ux\n",
1280 pins
[found
++] = pin
+ pin_num_from_lsb
;
1284 pgnames
[0] = np
->name
;
1285 function
= pcs_add_function(pcs
, np
, np
->name
, vals
, found
, pgnames
, 1);
1289 res
= pcs_add_pingroup(pcs
, np
, np
->name
, pins
, found
);
1293 (*map
)->type
= PIN_MAP_TYPE_MUX_GROUP
;
1294 (*map
)->data
.mux
.group
= np
->name
;
1295 (*map
)->data
.mux
.function
= np
->name
;
1297 if (pcs
->is_pinconf
) {
1298 dev_err(pcs
->dev
, "pinconf not supported\n");
1299 goto free_pingroups
;
1306 pcs_free_pingroups(pcs
);
1309 pcs_remove_function(pcs
, function
);
1312 devm_kfree(pcs
->dev
, pins
);
1315 devm_kfree(pcs
->dev
, vals
);
1320 * pcs_dt_node_to_map() - allocates and parses pinctrl maps
1321 * @pctldev: pinctrl instance
1322 * @np_config: device tree pinmux entry
1323 * @map: array of map entries
1324 * @num_maps: number of maps
1326 static int pcs_dt_node_to_map(struct pinctrl_dev
*pctldev
,
1327 struct device_node
*np_config
,
1328 struct pinctrl_map
**map
, unsigned *num_maps
)
1330 struct pcs_device
*pcs
;
1331 const char **pgnames
;
1334 pcs
= pinctrl_dev_get_drvdata(pctldev
);
1336 /* create 2 maps. One is for pinmux, and the other is for pinconf. */
1337 *map
= devm_kzalloc(pcs
->dev
, sizeof(**map
) * 2, GFP_KERNEL
);
1343 pgnames
= devm_kzalloc(pcs
->dev
, sizeof(*pgnames
), GFP_KERNEL
);
1349 if (pcs
->bits_per_mux
) {
1350 ret
= pcs_parse_bits_in_pinctrl_entry(pcs
, np_config
, map
,
1353 dev_err(pcs
->dev
, "no pins entries for %s\n",
1358 ret
= pcs_parse_one_pinctrl_entry(pcs
, np_config
, map
,
1361 dev_err(pcs
->dev
, "no pins entries for %s\n",
1370 devm_kfree(pcs
->dev
, pgnames
);
1372 devm_kfree(pcs
->dev
, *map
);
1378 * pcs_free_funcs() - free memory used by functions
1379 * @pcs: pcs driver instance
1381 static void pcs_free_funcs(struct pcs_device
*pcs
)
1383 struct list_head
*pos
, *tmp
;
1386 mutex_lock(&pcs
->mutex
);
1387 for (i
= 0; i
< pcs
->nfuncs
; i
++) {
1388 struct pcs_function
*func
;
1390 func
= radix_tree_lookup(&pcs
->ftree
, i
);
1393 radix_tree_delete(&pcs
->ftree
, i
);
1395 list_for_each_safe(pos
, tmp
, &pcs
->functions
) {
1396 struct pcs_function
*function
;
1398 function
= list_entry(pos
, struct pcs_function
, node
);
1399 list_del(&function
->node
);
1401 mutex_unlock(&pcs
->mutex
);
1405 * pcs_free_pingroups() - free memory used by pingroups
1406 * @pcs: pcs driver instance
1408 static void pcs_free_pingroups(struct pcs_device
*pcs
)
1410 struct list_head
*pos
, *tmp
;
1413 mutex_lock(&pcs
->mutex
);
1414 for (i
= 0; i
< pcs
->ngroups
; i
++) {
1415 struct pcs_pingroup
*pingroup
;
1417 pingroup
= radix_tree_lookup(&pcs
->pgtree
, i
);
1420 radix_tree_delete(&pcs
->pgtree
, i
);
1422 list_for_each_safe(pos
, tmp
, &pcs
->pingroups
) {
1423 struct pcs_pingroup
*pingroup
;
1425 pingroup
= list_entry(pos
, struct pcs_pingroup
, node
);
1426 list_del(&pingroup
->node
);
1428 mutex_unlock(&pcs
->mutex
);
1432 * pcs_free_resources() - free memory used by this driver
1433 * @pcs: pcs driver instance
1435 static void pcs_free_resources(struct pcs_device
*pcs
)
1438 pinctrl_unregister(pcs
->pctl
);
1440 pcs_free_funcs(pcs
);
1441 pcs_free_pingroups(pcs
);
1444 #define PCS_GET_PROP_U32(name, reg, err) \
1446 ret = of_property_read_u32(np, name, reg); \
1448 dev_err(pcs->dev, err); \
1453 static struct of_device_id pcs_of_match
[];
1455 static int pcs_add_gpio_func(struct device_node
*node
, struct pcs_device
*pcs
)
1457 const char *propname
= "pinctrl-single,gpio-range";
1458 const char *cellname
= "#pinctrl-single,gpio-range-cells";
1459 struct of_phandle_args gpiospec
;
1460 struct pcs_gpiofunc_range
*range
;
1463 for (i
= 0; ; i
++) {
1464 ret
= of_parse_phandle_with_args(node
, propname
, cellname
,
1466 /* Do not treat it as error. Only treat it as end condition. */
1471 range
= devm_kzalloc(pcs
->dev
, sizeof(*range
), GFP_KERNEL
);
1476 range
->offset
= gpiospec
.args
[0];
1477 range
->npins
= gpiospec
.args
[1];
1478 range
->gpiofunc
= gpiospec
.args
[2];
1479 mutex_lock(&pcs
->mutex
);
1480 list_add_tail(&range
->node
, &pcs
->gpiofuncs
);
1481 mutex_unlock(&pcs
->mutex
);
1486 static int pinctrl_single_suspend(struct platform_device
*pdev
,
1489 struct pcs_device
*pcs
;
1491 pcs
= platform_get_drvdata(pdev
);
1495 return pinctrl_force_sleep(pcs
->pctl
);
1498 static int pinctrl_single_resume(struct platform_device
*pdev
)
1500 struct pcs_device
*pcs
;
1502 pcs
= platform_get_drvdata(pdev
);
1506 return pinctrl_force_default(pcs
->pctl
);
1509 static int pcs_probe(struct platform_device
*pdev
)
1511 struct device_node
*np
= pdev
->dev
.of_node
;
1512 const struct of_device_id
*match
;
1513 struct resource
*res
;
1514 struct pcs_device
*pcs
;
1517 match
= of_match_device(pcs_of_match
, &pdev
->dev
);
1521 pcs
= devm_kzalloc(&pdev
->dev
, sizeof(*pcs
), GFP_KERNEL
);
1523 dev_err(&pdev
->dev
, "could not allocate\n");
1526 pcs
->dev
= &pdev
->dev
;
1527 mutex_init(&pcs
->mutex
);
1528 INIT_LIST_HEAD(&pcs
->pingroups
);
1529 INIT_LIST_HEAD(&pcs
->functions
);
1530 INIT_LIST_HEAD(&pcs
->gpiofuncs
);
1531 pcs
->is_pinconf
= match
->data
;
1533 PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs
->width
,
1534 "register width not specified\n");
1536 ret
= of_property_read_u32(np
, "pinctrl-single,function-mask",
1539 pcs
->fshift
= ffs(pcs
->fmask
) - 1;
1540 pcs
->fmax
= pcs
->fmask
>> pcs
->fshift
;
1542 /* If mask property doesn't exist, function mux is invalid. */
1548 ret
= of_property_read_u32(np
, "pinctrl-single,function-off",
1551 pcs
->foff
= PCS_OFF_DISABLED
;
1553 pcs
->bits_per_mux
= of_property_read_bool(np
,
1554 "pinctrl-single,bit-per-mux");
1556 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1558 dev_err(pcs
->dev
, "could not get resource\n");
1562 pcs
->res
= devm_request_mem_region(pcs
->dev
, res
->start
,
1563 resource_size(res
), DRIVER_NAME
);
1565 dev_err(pcs
->dev
, "could not get mem_region\n");
1569 pcs
->size
= resource_size(pcs
->res
);
1570 pcs
->base
= devm_ioremap(pcs
->dev
, pcs
->res
->start
, pcs
->size
);
1572 dev_err(pcs
->dev
, "could not ioremap\n");
1576 INIT_RADIX_TREE(&pcs
->pgtree
, GFP_KERNEL
);
1577 INIT_RADIX_TREE(&pcs
->ftree
, GFP_KERNEL
);
1578 platform_set_drvdata(pdev
, pcs
);
1580 switch (pcs
->width
) {
1582 pcs
->read
= pcs_readb
;
1583 pcs
->write
= pcs_writeb
;
1586 pcs
->read
= pcs_readw
;
1587 pcs
->write
= pcs_writew
;
1590 pcs
->read
= pcs_readl
;
1591 pcs
->write
= pcs_writel
;
1597 pcs
->desc
.name
= DRIVER_NAME
;
1598 pcs
->desc
.pctlops
= &pcs_pinctrl_ops
;
1599 pcs
->desc
.pmxops
= &pcs_pinmux_ops
;
1600 if (pcs
->is_pinconf
)
1601 pcs
->desc
.confops
= &pcs_pinconf_ops
;
1602 pcs
->desc
.owner
= THIS_MODULE
;
1604 ret
= pcs_allocate_pin_table(pcs
);
1608 pcs
->pctl
= pinctrl_register(&pcs
->desc
, pcs
->dev
, pcs
);
1610 dev_err(pcs
->dev
, "could not register single pinctrl driver\n");
1615 ret
= pcs_add_gpio_func(np
, pcs
);
1619 dev_info(pcs
->dev
, "%i pins at pa %p size %u\n",
1620 pcs
->desc
.npins
, pcs
->base
, pcs
->size
);
1625 pcs_free_resources(pcs
);
1630 static int pcs_remove(struct platform_device
*pdev
)
1632 struct pcs_device
*pcs
= platform_get_drvdata(pdev
);
1637 pcs_free_resources(pcs
);
1642 static struct of_device_id pcs_of_match
[] = {
1643 { .compatible
= "pinctrl-single", .data
= (void *)false },
1644 { .compatible
= "pinconf-single", .data
= (void *)true },
1647 MODULE_DEVICE_TABLE(of
, pcs_of_match
);
1649 static struct platform_driver pcs_driver
= {
1651 .remove
= pcs_remove
,
1653 .owner
= THIS_MODULE
,
1654 .name
= DRIVER_NAME
,
1655 .of_match_table
= pcs_of_match
,
1658 .suspend
= pinctrl_single_suspend
,
1659 .resume
= pinctrl_single_resume
,
1663 module_platform_driver(pcs_driver
);
1665 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
1666 MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
1667 MODULE_LICENSE("GPL v2");