Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/drivers/pinctrl/pinctrl-lantiq.c
0004  *  based on linux/drivers/pinctrl/pinctrl-pxa3xx.c
0005  *
0006  *  Copyright (C) 2012 John Crispin <john@phrozen.org>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/device.h>
0011 #include <linux/io.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/slab.h>
0014 #include <linux/of.h>
0015 
0016 #include "pinctrl-lantiq.h"
0017 
0018 static int ltq_get_group_count(struct pinctrl_dev *pctrldev)
0019 {
0020     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
0021     return info->num_grps;
0022 }
0023 
0024 static const char *ltq_get_group_name(struct pinctrl_dev *pctrldev,
0025                      unsigned selector)
0026 {
0027     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
0028     if (selector >= info->num_grps)
0029         return NULL;
0030     return info->grps[selector].name;
0031 }
0032 
0033 static int ltq_get_group_pins(struct pinctrl_dev *pctrldev,
0034                  unsigned selector,
0035                  const unsigned **pins,
0036                  unsigned *num_pins)
0037 {
0038     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
0039     if (selector >= info->num_grps)
0040         return -EINVAL;
0041     *pins = info->grps[selector].pins;
0042     *num_pins = info->grps[selector].npins;
0043     return 0;
0044 }
0045 
0046 static void ltq_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
0047                     struct pinctrl_map *map, unsigned num_maps)
0048 {
0049     int i;
0050 
0051     for (i = 0; i < num_maps; i++)
0052         if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN ||
0053             map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
0054             kfree(map[i].data.configs.configs);
0055     kfree(map);
0056 }
0057 
0058 static void ltq_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
0059                     struct seq_file *s,
0060                     unsigned offset)
0061 {
0062     seq_printf(s, " %s", dev_name(pctldev->dev));
0063 }
0064 
0065 static void ltq_pinctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
0066                 struct device_node *np,
0067                 struct pinctrl_map **map)
0068 {
0069     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctldev);
0070     struct property *pins = of_find_property(np, "lantiq,pins", NULL);
0071     struct property *groups = of_find_property(np, "lantiq,groups", NULL);
0072     unsigned long configs[3];
0073     unsigned num_configs = 0;
0074     struct property *prop;
0075     const char *group, *pin;
0076     const char *function;
0077     int ret, i;
0078 
0079     if (!pins && !groups) {
0080         dev_err(pctldev->dev, "%pOFn defines neither pins nor groups\n",
0081             np);
0082         return;
0083     }
0084 
0085     if (pins && groups) {
0086         dev_err(pctldev->dev, "%pOFn defines both pins and groups\n",
0087             np);
0088         return;
0089     }
0090 
0091     ret = of_property_read_string(np, "lantiq,function", &function);
0092     if (groups && !ret) {
0093         of_property_for_each_string(np, "lantiq,groups", prop, group) {
0094             (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
0095             (*map)->name = function;
0096             (*map)->data.mux.group = group;
0097             (*map)->data.mux.function = function;
0098             (*map)++;
0099         }
0100     }
0101 
0102     for (i = 0; i < info->num_params; i++) {
0103         u32 val;
0104         int ret = of_property_read_u32(np,
0105                 info->params[i].property, &val);
0106         if (!ret)
0107             configs[num_configs++] =
0108                 LTQ_PINCONF_PACK(info->params[i].param,
0109                 val);
0110     }
0111 
0112     if (!num_configs)
0113         return;
0114 
0115     of_property_for_each_string(np, "lantiq,pins", prop, pin) {
0116         (*map)->data.configs.configs = kmemdup(configs,
0117                     num_configs * sizeof(unsigned long),
0118                     GFP_KERNEL);
0119         (*map)->type = PIN_MAP_TYPE_CONFIGS_PIN;
0120         (*map)->name = pin;
0121         (*map)->data.configs.group_or_pin = pin;
0122         (*map)->data.configs.num_configs = num_configs;
0123         (*map)++;
0124     }
0125     of_property_for_each_string(np, "lantiq,groups", prop, group) {
0126         (*map)->data.configs.configs = kmemdup(configs,
0127                     num_configs * sizeof(unsigned long),
0128                     GFP_KERNEL);
0129         (*map)->type = PIN_MAP_TYPE_CONFIGS_GROUP;
0130         (*map)->name = group;
0131         (*map)->data.configs.group_or_pin = group;
0132         (*map)->data.configs.num_configs = num_configs;
0133         (*map)++;
0134     }
0135 }
0136 
0137 static int ltq_pinctrl_dt_subnode_size(struct device_node *np)
0138 {
0139     int ret;
0140 
0141     ret = of_property_count_strings(np, "lantiq,groups");
0142     if (ret < 0)
0143         ret = of_property_count_strings(np, "lantiq,pins");
0144     return ret;
0145 }
0146 
0147 static int ltq_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
0148                       struct device_node *np_config,
0149                       struct pinctrl_map **map,
0150                       unsigned *num_maps)
0151 {
0152     struct pinctrl_map *tmp;
0153     struct device_node *np;
0154     int max_maps = 0;
0155 
0156     for_each_child_of_node(np_config, np)
0157         max_maps += ltq_pinctrl_dt_subnode_size(np);
0158     *map = kzalloc(array3_size(max_maps, sizeof(struct pinctrl_map), 2),
0159                GFP_KERNEL);
0160     if (!*map)
0161         return -ENOMEM;
0162     tmp = *map;
0163 
0164     for_each_child_of_node(np_config, np)
0165         ltq_pinctrl_dt_subnode_to_map(pctldev, np, &tmp);
0166     *num_maps = ((int)(tmp - *map));
0167 
0168     return 0;
0169 }
0170 
0171 static const struct pinctrl_ops ltq_pctrl_ops = {
0172     .get_groups_count   = ltq_get_group_count,
0173     .get_group_name     = ltq_get_group_name,
0174     .get_group_pins     = ltq_get_group_pins,
0175     .pin_dbg_show       = ltq_pinctrl_pin_dbg_show,
0176     .dt_node_to_map     = ltq_pinctrl_dt_node_to_map,
0177     .dt_free_map        = ltq_pinctrl_dt_free_map,
0178 };
0179 
0180 static int ltq_pmx_func_count(struct pinctrl_dev *pctrldev)
0181 {
0182     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
0183 
0184     return info->num_funcs;
0185 }
0186 
0187 static const char *ltq_pmx_func_name(struct pinctrl_dev *pctrldev,
0188                      unsigned selector)
0189 {
0190     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
0191 
0192     if (selector >= info->num_funcs)
0193         return NULL;
0194 
0195     return info->funcs[selector].name;
0196 }
0197 
0198 static int ltq_pmx_get_groups(struct pinctrl_dev *pctrldev,
0199                 unsigned func,
0200                 const char * const **groups,
0201                 unsigned * const num_groups)
0202 {
0203     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
0204 
0205     *groups = info->funcs[func].groups;
0206     *num_groups = info->funcs[func].num_groups;
0207 
0208     return 0;
0209 }
0210 
0211 /* Return function number. If failure, return negative value. */
0212 static int match_mux(const struct ltq_mfp_pin *mfp, unsigned mux)
0213 {
0214     int i;
0215     for (i = 0; i < LTQ_MAX_MUX; i++) {
0216         if (mfp->func[i] == mux)
0217             break;
0218     }
0219     if (i >= LTQ_MAX_MUX)
0220         return -EINVAL;
0221     return i;
0222 }
0223 
0224 /* don't assume .mfp is linearly mapped. find the mfp with the correct .pin */
0225 static int match_mfp(const struct ltq_pinmux_info *info, int pin)
0226 {
0227     int i;
0228     for (i = 0; i < info->num_mfp; i++) {
0229         if (info->mfp[i].pin == pin)
0230             return i;
0231     }
0232     return -1;
0233 }
0234 
0235 /* check whether current pin configuration is valid. Negative for failure */
0236 static int match_group_mux(const struct ltq_pin_group *grp,
0237                const struct ltq_pinmux_info *info,
0238                unsigned mux)
0239 {
0240     int i, pin, ret = 0;
0241     for (i = 0; i < grp->npins; i++) {
0242         pin = match_mfp(info, grp->pins[i]);
0243         if (pin < 0) {
0244             dev_err(info->dev, "could not find mfp for pin %d\n",
0245                 grp->pins[i]);
0246             return -EINVAL;
0247         }
0248         ret = match_mux(&info->mfp[pin], mux);
0249         if (ret < 0) {
0250             dev_err(info->dev, "Can't find mux %d on pin%d\n",
0251                 mux, pin);
0252             break;
0253         }
0254     }
0255     return ret;
0256 }
0257 
0258 static int ltq_pmx_set(struct pinctrl_dev *pctrldev,
0259                unsigned func,
0260                unsigned group)
0261 {
0262     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
0263     const struct ltq_pin_group *pin_grp = &info->grps[group];
0264     int i, pin, pin_func, ret;
0265 
0266     if (!pin_grp->npins ||
0267         (match_group_mux(pin_grp, info, pin_grp->mux) < 0)) {
0268         dev_err(info->dev, "Failed to set the pin group: %s\n",
0269             info->grps[group].name);
0270         return -EINVAL;
0271     }
0272     for (i = 0; i < pin_grp->npins; i++) {
0273         pin = match_mfp(info, pin_grp->pins[i]);
0274         if (pin < 0) {
0275             dev_err(info->dev, "could not find mfp for pin %d\n",
0276                 pin_grp->pins[i]);
0277             return -EINVAL;
0278         }
0279         pin_func = match_mux(&info->mfp[pin], pin_grp->mux);
0280         ret = info->apply_mux(pctrldev, pin, pin_func);
0281         if (ret) {
0282             dev_err(info->dev,
0283                 "failed to apply mux %d for pin %d\n",
0284                 pin_func, pin);
0285             return ret;
0286         }
0287     }
0288     return 0;
0289 }
0290 
0291 static int ltq_pmx_gpio_request_enable(struct pinctrl_dev *pctrldev,
0292                 struct pinctrl_gpio_range *range,
0293                 unsigned pin)
0294 {
0295     struct ltq_pinmux_info *info = pinctrl_dev_get_drvdata(pctrldev);
0296     int mfp = match_mfp(info, pin);
0297     int pin_func;
0298 
0299     if (mfp < 0) {
0300         dev_err(info->dev, "could not find mfp for pin %d\n", pin);
0301         return -EINVAL;
0302     }
0303 
0304     pin_func = match_mux(&info->mfp[mfp], 0);
0305     if (pin_func < 0) {
0306         dev_err(info->dev, "No GPIO function on pin%d\n", mfp);
0307         return -EINVAL;
0308     }
0309 
0310     return info->apply_mux(pctrldev, mfp, pin_func);
0311 }
0312 
0313 static const struct pinmux_ops ltq_pmx_ops = {
0314     .get_functions_count    = ltq_pmx_func_count,
0315     .get_function_name  = ltq_pmx_func_name,
0316     .get_function_groups    = ltq_pmx_get_groups,
0317     .set_mux        = ltq_pmx_set,
0318     .gpio_request_enable    = ltq_pmx_gpio_request_enable,
0319 };
0320 
0321 /*
0322  * allow different socs to register with the generic part of the lanti
0323  * pinctrl code
0324  */
0325 int ltq_pinctrl_register(struct platform_device *pdev,
0326                 struct ltq_pinmux_info *info)
0327 {
0328     struct pinctrl_desc *desc;
0329 
0330     if (!info)
0331         return -EINVAL;
0332     desc = info->desc;
0333     desc->pctlops = &ltq_pctrl_ops;
0334     desc->pmxops = &ltq_pmx_ops;
0335     info->dev = &pdev->dev;
0336 
0337     info->pctrl = devm_pinctrl_register(&pdev->dev, desc, info);
0338     if (IS_ERR(info->pctrl)) {
0339         dev_err(&pdev->dev, "failed to register LTQ pinmux driver\n");
0340         return PTR_ERR(info->pctrl);
0341     }
0342     platform_set_drvdata(pdev, info);
0343     return 0;
0344 }