1 // SPDX-License-Identifier: GPL-2.0+
3 * OWL SoC's Pinctrl driver
5 * Copyright (c) 2014 Actions Semi Inc.
6 * Author: David Liu <liuwei@actions-semi.com>
8 * Copyright (c) 2018 Linaro Ltd.
9 * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
12 #include <linux/clk.h>
13 #include <linux/err.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pinctrl/machine.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/pinctrl/pinconf.h>
22 #include <linux/pinctrl/pinconf-generic.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
27 #include "../pinctrl-utils.h"
28 #include "pinctrl-owl.h"
31 * struct owl_pinctrl - pinctrl state of the device
33 * @pctrldev: pinctrl handle
34 * @lock: spinlock to protect registers
35 * @soc: reference to soc_data
36 * @base: pinctrl register base address
40 struct pinctrl_dev *pctrldev;
43 const struct owl_pinctrl_soc_data *soc;
47 static void owl_update_bits(void __iomem *base, u32 mask, u32 val)
51 reg_val = readl_relaxed(base);
53 reg_val = (reg_val & ~mask) | (val & mask);
55 writel_relaxed(reg_val, base);
58 static u32 owl_read_field(struct owl_pinctrl *pctrl, u32 reg,
63 tmp = readl_relaxed(pctrl->base + reg);
64 mask = (1 << width) - 1;
66 return (tmp >> bit) & mask;
69 static void owl_write_field(struct owl_pinctrl *pctrl, u32 reg, u32 arg,
74 mask = (1 << width) - 1;
77 owl_update_bits(pctrl->base + reg, mask, (arg << bit));
80 static int owl_get_groups_count(struct pinctrl_dev *pctrldev)
82 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
84 return pctrl->soc->ngroups;
87 static const char *owl_get_group_name(struct pinctrl_dev *pctrldev,
90 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
92 return pctrl->soc->groups[group].name;
95 static int owl_get_group_pins(struct pinctrl_dev *pctrldev,
97 const unsigned int **pins,
98 unsigned int *num_pins)
100 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
102 *pins = pctrl->soc->groups[group].pads;
103 *num_pins = pctrl->soc->groups[group].npads;
108 static void owl_pin_dbg_show(struct pinctrl_dev *pctrldev,
112 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
114 seq_printf(s, "%s", dev_name(pctrl->dev));
117 static struct pinctrl_ops owl_pinctrl_ops = {
118 .get_groups_count = owl_get_groups_count,
119 .get_group_name = owl_get_group_name,
120 .get_group_pins = owl_get_group_pins,
121 .pin_dbg_show = owl_pin_dbg_show,
122 .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
123 .dt_free_map = pinctrl_utils_free_map,
126 static int owl_get_funcs_count(struct pinctrl_dev *pctrldev)
128 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
130 return pctrl->soc->nfunctions;
133 static const char *owl_get_func_name(struct pinctrl_dev *pctrldev,
134 unsigned int function)
136 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
138 return pctrl->soc->functions[function].name;
141 static int owl_get_func_groups(struct pinctrl_dev *pctrldev,
142 unsigned int function,
143 const char * const **groups,
144 unsigned int * const num_groups)
146 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
148 *groups = pctrl->soc->functions[function].groups;
149 *num_groups = pctrl->soc->functions[function].ngroups;
154 static inline int get_group_mfp_mask_val(const struct owl_pingroup *g,
163 for (id = 0; id < g->nfuncs; id++) {
164 if (g->funcs[id] == function)
167 if (WARN_ON(id == g->nfuncs))
170 option_num = (1 << g->mfpctl_width);
174 option_mask = option_num - 1;
175 *mask = (option_mask << g->mfpctl_shift);
176 *val = (id << g->mfpctl_shift);
181 static int owl_set_mux(struct pinctrl_dev *pctrldev,
182 unsigned int function,
185 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
186 const struct owl_pingroup *g;
190 g = &pctrl->soc->groups[group];
192 if (get_group_mfp_mask_val(g, function, &mask, &val))
195 raw_spin_lock_irqsave(&pctrl->lock, flags);
197 owl_update_bits(pctrl->base + g->mfpctl_reg, mask, val);
199 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
204 static struct pinmux_ops owl_pinmux_ops = {
205 .get_functions_count = owl_get_funcs_count,
206 .get_function_name = owl_get_func_name,
207 .get_function_groups = owl_get_func_groups,
208 .set_mux = owl_set_mux,
211 static int owl_pad_pinconf_reg(const struct owl_padinfo *info,
218 case PIN_CONFIG_BIAS_BUS_HOLD:
219 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
220 case PIN_CONFIG_BIAS_PULL_DOWN:
221 case PIN_CONFIG_BIAS_PULL_UP:
224 *reg = info->pullctl->reg;
225 *bit = info->pullctl->shift;
226 *width = info->pullctl->width;
228 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
231 *reg = info->st->reg;
232 *bit = info->st->shift;
233 *width = info->st->width;
242 static int owl_pad_pinconf_arg2val(const struct owl_padinfo *info,
247 case PIN_CONFIG_BIAS_BUS_HOLD:
248 *arg = OWL_PINCONF_PULL_HOLD;
250 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
251 *arg = OWL_PINCONF_PULL_HIZ;
253 case PIN_CONFIG_BIAS_PULL_DOWN:
254 *arg = OWL_PINCONF_PULL_DOWN;
256 case PIN_CONFIG_BIAS_PULL_UP:
257 *arg = OWL_PINCONF_PULL_UP;
259 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
260 *arg = (*arg >= 1 ? 1 : 0);
269 static int owl_pad_pinconf_val2arg(const struct owl_padinfo *padinfo,
274 case PIN_CONFIG_BIAS_BUS_HOLD:
275 *arg = *arg == OWL_PINCONF_PULL_HOLD;
277 case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
278 *arg = *arg == OWL_PINCONF_PULL_HIZ;
280 case PIN_CONFIG_BIAS_PULL_DOWN:
281 *arg = *arg == OWL_PINCONF_PULL_DOWN;
283 case PIN_CONFIG_BIAS_PULL_UP:
284 *arg = *arg == OWL_PINCONF_PULL_UP;
286 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
296 static int owl_pin_config_get(struct pinctrl_dev *pctrldev,
298 unsigned long *config)
301 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
302 const struct owl_padinfo *info;
303 unsigned int param = pinconf_to_config_param(*config);
304 u32 reg, bit, width, arg;
306 info = &pctrl->soc->padinfo[pin];
308 ret = owl_pad_pinconf_reg(info, param, ®, &bit, &width);
312 arg = owl_read_field(pctrl, reg, bit, width);
314 ret = owl_pad_pinconf_val2arg(info, param, &arg);
318 *config = pinconf_to_config_packed(param, arg);
323 static int owl_pin_config_set(struct pinctrl_dev *pctrldev,
325 unsigned long *configs,
326 unsigned int num_configs)
328 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
329 const struct owl_padinfo *info;
332 u32 reg, bit, width, arg;
335 info = &pctrl->soc->padinfo[pin];
337 for (i = 0; i < num_configs; i++) {
338 param = pinconf_to_config_param(configs[i]);
339 arg = pinconf_to_config_argument(configs[i]);
341 ret = owl_pad_pinconf_reg(info, param, ®, &bit, &width);
345 ret = owl_pad_pinconf_arg2val(info, param, &arg);
349 raw_spin_lock_irqsave(&pctrl->lock, flags);
351 owl_write_field(pctrl, reg, arg, bit, width);
353 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
359 static int owl_group_pinconf_reg(const struct owl_pingroup *g,
366 case PIN_CONFIG_DRIVE_STRENGTH:
371 *width = g->drv_width;
373 case PIN_CONFIG_SLEW_RATE:
378 *width = g->sr_width;
387 static int owl_group_pinconf_arg2val(const struct owl_pingroup *g,
392 case PIN_CONFIG_DRIVE_STRENGTH:
395 *arg = OWL_PINCONF_DRV_2MA;
398 *arg = OWL_PINCONF_DRV_4MA;
401 *arg = OWL_PINCONF_DRV_8MA;
404 *arg = OWL_PINCONF_DRV_12MA;
410 case PIN_CONFIG_SLEW_RATE:
412 *arg = OWL_PINCONF_SLEW_FAST;
414 *arg = OWL_PINCONF_SLEW_SLOW;
423 static int owl_group_pinconf_val2arg(const struct owl_pingroup *g,
428 case PIN_CONFIG_DRIVE_STRENGTH:
430 case OWL_PINCONF_DRV_2MA:
433 case OWL_PINCONF_DRV_4MA:
436 case OWL_PINCONF_DRV_8MA:
439 case OWL_PINCONF_DRV_12MA:
446 case PIN_CONFIG_SLEW_RATE:
459 static int owl_group_config_get(struct pinctrl_dev *pctrldev,
461 unsigned long *config)
463 const struct owl_pingroup *g;
464 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
465 unsigned int param = pinconf_to_config_param(*config);
466 u32 reg, bit, width, arg;
469 g = &pctrl->soc->groups[group];
471 ret = owl_group_pinconf_reg(g, param, ®, &bit, &width);
475 arg = owl_read_field(pctrl, reg, bit, width);
477 ret = owl_group_pinconf_val2arg(g, param, &arg);
481 *config = pinconf_to_config_packed(param, arg);
487 static int owl_group_config_set(struct pinctrl_dev *pctrldev,
489 unsigned long *configs,
490 unsigned int num_configs)
492 const struct owl_pingroup *g;
493 struct owl_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctrldev);
496 u32 reg, bit, width, arg;
499 g = &pctrl->soc->groups[group];
501 for (i = 0; i < num_configs; i++) {
502 param = pinconf_to_config_param(configs[i]);
503 arg = pinconf_to_config_argument(configs[i]);
505 ret = owl_group_pinconf_reg(g, param, ®, &bit, &width);
509 ret = owl_group_pinconf_arg2val(g, param, &arg);
513 /* Update register */
514 raw_spin_lock_irqsave(&pctrl->lock, flags);
516 owl_write_field(pctrl, reg, arg, bit, width);
518 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
524 static const struct pinconf_ops owl_pinconf_ops = {
526 .pin_config_get = owl_pin_config_get,
527 .pin_config_set = owl_pin_config_set,
528 .pin_config_group_get = owl_group_config_get,
529 .pin_config_group_set = owl_group_config_set,
532 static struct pinctrl_desc owl_pinctrl_desc = {
533 .pctlops = &owl_pinctrl_ops,
534 .pmxops = &owl_pinmux_ops,
535 .confops = &owl_pinconf_ops,
536 .owner = THIS_MODULE,
539 int owl_pinctrl_probe(struct platform_device *pdev,
540 struct owl_pinctrl_soc_data *soc_data)
542 struct resource *res;
543 struct owl_pinctrl *pctrl;
546 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
550 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
551 pctrl->base = devm_ioremap_resource(&pdev->dev, res);
552 if (IS_ERR(pctrl->base))
553 return PTR_ERR(pctrl->base);
555 /* enable GPIO/MFP clock */
556 pctrl->clk = devm_clk_get(&pdev->dev, NULL);
557 if (IS_ERR(pctrl->clk)) {
558 dev_err(&pdev->dev, "no clock defined\n");
559 return PTR_ERR(pctrl->clk);
562 ret = clk_prepare_enable(pctrl->clk);
564 dev_err(&pdev->dev, "clk enable failed\n");
568 raw_spin_lock_init(&pctrl->lock);
570 owl_pinctrl_desc.name = dev_name(&pdev->dev);
571 owl_pinctrl_desc.pins = soc_data->pins;
572 owl_pinctrl_desc.npins = soc_data->npins;
574 pctrl->soc = soc_data;
575 pctrl->dev = &pdev->dev;
577 pctrl->pctrldev = devm_pinctrl_register(&pdev->dev,
578 &owl_pinctrl_desc, pctrl);
579 if (IS_ERR(pctrl->pctrldev)) {
580 dev_err(&pdev->dev, "could not register Actions OWL pinmux driver\n");
581 return PTR_ERR(pctrl->pctrldev);
584 platform_set_drvdata(pdev, pctrl);