Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  Copyright (C) 2013 John Crispin <blogic@openwrt.org>
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/device.h>
0008 #include <linux/io.h>
0009 #include <linux/platform_device.h>
0010 #include <linux/slab.h>
0011 #include <linux/of.h>
0012 #include <linux/pinctrl/pinctrl.h>
0013 #include <linux/pinctrl/pinconf.h>
0014 #include <linux/pinctrl/pinconf-generic.h>
0015 #include <linux/pinctrl/pinmux.h>
0016 #include <linux/pinctrl/consumer.h>
0017 #include <linux/pinctrl/machine.h>
0018 
0019 #include <asm/mach-ralink/ralink_regs.h>
0020 #include <asm/mach-ralink/mt7620.h>
0021 
0022 #include "pinctrl-ralink.h"
0023 #include "../core.h"
0024 #include "../pinctrl-utils.h"
0025 
0026 #define SYSC_REG_GPIO_MODE  0x60
0027 #define SYSC_REG_GPIO_MODE2 0x64
0028 
0029 struct ralink_priv {
0030     struct device *dev;
0031 
0032     struct pinctrl_pin_desc *pads;
0033     struct pinctrl_desc *desc;
0034 
0035     struct ralink_pmx_func **func;
0036     int func_count;
0037 
0038     struct ralink_pmx_group *groups;
0039     const char **group_names;
0040     int group_count;
0041 
0042     u8 *gpio;
0043     int max_pins;
0044 };
0045 
0046 static int ralink_get_group_count(struct pinctrl_dev *pctrldev)
0047 {
0048     struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
0049 
0050     return p->group_count;
0051 }
0052 
0053 static const char *ralink_get_group_name(struct pinctrl_dev *pctrldev,
0054                      unsigned int group)
0055 {
0056     struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
0057 
0058     return (group >= p->group_count) ? NULL : p->group_names[group];
0059 }
0060 
0061 static int ralink_get_group_pins(struct pinctrl_dev *pctrldev,
0062                  unsigned int group,
0063                  const unsigned int **pins,
0064                  unsigned int *num_pins)
0065 {
0066     struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
0067 
0068     if (group >= p->group_count)
0069         return -EINVAL;
0070 
0071     *pins = p->groups[group].func[0].pins;
0072     *num_pins = p->groups[group].func[0].pin_count;
0073 
0074     return 0;
0075 }
0076 
0077 static const struct pinctrl_ops ralink_pctrl_ops = {
0078     .get_groups_count   = ralink_get_group_count,
0079     .get_group_name     = ralink_get_group_name,
0080     .get_group_pins     = ralink_get_group_pins,
0081     .dt_node_to_map     = pinconf_generic_dt_node_to_map_all,
0082     .dt_free_map        = pinconf_generic_dt_free_map,
0083 };
0084 
0085 static int ralink_pmx_func_count(struct pinctrl_dev *pctrldev)
0086 {
0087     struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
0088 
0089     return p->func_count;
0090 }
0091 
0092 static const char *ralink_pmx_func_name(struct pinctrl_dev *pctrldev,
0093                     unsigned int func)
0094 {
0095     struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
0096 
0097     return p->func[func]->name;
0098 }
0099 
0100 static int ralink_pmx_group_get_groups(struct pinctrl_dev *pctrldev,
0101                        unsigned int func,
0102                        const char * const **groups,
0103                        unsigned int * const num_groups)
0104 {
0105     struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
0106 
0107     if (p->func[func]->group_count == 1)
0108         *groups = &p->group_names[p->func[func]->groups[0]];
0109     else
0110         *groups = p->group_names;
0111 
0112     *num_groups = p->func[func]->group_count;
0113 
0114     return 0;
0115 }
0116 
0117 static int ralink_pmx_group_enable(struct pinctrl_dev *pctrldev,
0118                    unsigned int func, unsigned int group)
0119 {
0120     struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
0121     u32 mode = 0;
0122     u32 reg = SYSC_REG_GPIO_MODE;
0123     int i;
0124     int shift;
0125 
0126     /* dont allow double use */
0127     if (p->groups[group].enabled) {
0128         dev_err(p->dev, "%s is already enabled\n",
0129             p->groups[group].name);
0130         return 0;
0131     }
0132 
0133     p->groups[group].enabled = 1;
0134     p->func[func]->enabled = 1;
0135 
0136     shift = p->groups[group].shift;
0137     if (shift >= 32) {
0138         shift -= 32;
0139         reg = SYSC_REG_GPIO_MODE2;
0140     }
0141     mode = rt_sysc_r32(reg);
0142     mode &= ~(p->groups[group].mask << shift);
0143 
0144     /* mark the pins as gpio */
0145     for (i = 0; i < p->groups[group].func[0].pin_count; i++)
0146         p->gpio[p->groups[group].func[0].pins[i]] = 1;
0147 
0148     /* function 0 is gpio and needs special handling */
0149     if (func == 0) {
0150         mode |= p->groups[group].gpio << shift;
0151     } else {
0152         for (i = 0; i < p->func[func]->pin_count; i++)
0153             p->gpio[p->func[func]->pins[i]] = 0;
0154         mode |= p->func[func]->value << shift;
0155     }
0156     rt_sysc_w32(mode, reg);
0157 
0158     return 0;
0159 }
0160 
0161 static int ralink_pmx_group_gpio_request_enable(struct pinctrl_dev *pctrldev,
0162                         struct pinctrl_gpio_range *range,
0163                         unsigned int pin)
0164 {
0165     struct ralink_priv *p = pinctrl_dev_get_drvdata(pctrldev);
0166 
0167     if (!p->gpio[pin]) {
0168         dev_err(p->dev, "pin %d is not set to gpio mux\n", pin);
0169         return -EINVAL;
0170     }
0171 
0172     return 0;
0173 }
0174 
0175 static const struct pinmux_ops ralink_pmx_group_ops = {
0176     .get_functions_count    = ralink_pmx_func_count,
0177     .get_function_name  = ralink_pmx_func_name,
0178     .get_function_groups    = ralink_pmx_group_get_groups,
0179     .set_mux        = ralink_pmx_group_enable,
0180     .gpio_request_enable    = ralink_pmx_group_gpio_request_enable,
0181 };
0182 
0183 static struct pinctrl_desc ralink_pctrl_desc = {
0184     .owner      = THIS_MODULE,
0185     .name       = "ralink-pinctrl",
0186     .pctlops    = &ralink_pctrl_ops,
0187     .pmxops     = &ralink_pmx_group_ops,
0188 };
0189 
0190 static struct ralink_pmx_func gpio_func = {
0191     .name = "gpio",
0192 };
0193 
0194 static int ralink_pinctrl_index(struct ralink_priv *p)
0195 {
0196     struct ralink_pmx_group *mux = p->groups;
0197     int i, j, c = 0;
0198 
0199     /* count the mux functions */
0200     while (mux->name) {
0201         p->group_count++;
0202         mux++;
0203     }
0204 
0205     /* allocate the group names array needed by the gpio function */
0206     p->group_names = devm_kcalloc(p->dev, p->group_count,
0207                       sizeof(char *), GFP_KERNEL);
0208     if (!p->group_names)
0209         return -ENOMEM;
0210 
0211     for (i = 0; i < p->group_count; i++) {
0212         p->group_names[i] = p->groups[i].name;
0213         p->func_count += p->groups[i].func_count;
0214     }
0215 
0216     /* we have a dummy function[0] for gpio */
0217     p->func_count++;
0218 
0219     /* allocate our function and group mapping index buffers */
0220     p->func = devm_kcalloc(p->dev, p->func_count,
0221                    sizeof(*p->func), GFP_KERNEL);
0222     gpio_func.groups = devm_kcalloc(p->dev, p->group_count, sizeof(int),
0223                     GFP_KERNEL);
0224     if (!p->func || !gpio_func.groups)
0225         return -ENOMEM;
0226 
0227     /* add a backpointer to the function so it knows its group */
0228     gpio_func.group_count = p->group_count;
0229     for (i = 0; i < gpio_func.group_count; i++)
0230         gpio_func.groups[i] = i;
0231 
0232     p->func[c] = &gpio_func;
0233     c++;
0234 
0235     /* add remaining functions */
0236     for (i = 0; i < p->group_count; i++) {
0237         for (j = 0; j < p->groups[i].func_count; j++) {
0238             p->func[c] = &p->groups[i].func[j];
0239             p->func[c]->groups = devm_kzalloc(p->dev, sizeof(int),
0240                             GFP_KERNEL);
0241             if (!p->func[c]->groups)
0242                 return -ENOMEM;
0243             p->func[c]->groups[0] = i;
0244             p->func[c]->group_count = 1;
0245             c++;
0246         }
0247     }
0248     return 0;
0249 }
0250 
0251 static int ralink_pinctrl_pins(struct ralink_priv *p)
0252 {
0253     int i, j;
0254 
0255     /*
0256      * loop over the functions and initialize the pins array.
0257      * also work out the highest pin used.
0258      */
0259     for (i = 0; i < p->func_count; i++) {
0260         int pin;
0261 
0262         if (!p->func[i]->pin_count)
0263             continue;
0264 
0265         p->func[i]->pins = devm_kcalloc(p->dev,
0266                         p->func[i]->pin_count,
0267                         sizeof(int),
0268                         GFP_KERNEL);
0269         if (!p->func[i]->pins)
0270             return -ENOMEM;
0271         for (j = 0; j < p->func[i]->pin_count; j++)
0272             p->func[i]->pins[j] = p->func[i]->pin_first + j;
0273 
0274         pin = p->func[i]->pin_first + p->func[i]->pin_count;
0275         if (pin > p->max_pins)
0276             p->max_pins = pin;
0277     }
0278 
0279     /* the buffer that tells us which pins are gpio */
0280     p->gpio = devm_kcalloc(p->dev, p->max_pins, sizeof(u8), GFP_KERNEL);
0281     /* the pads needed to tell pinctrl about our pins */
0282     p->pads = devm_kcalloc(p->dev, p->max_pins,
0283                    sizeof(struct pinctrl_pin_desc), GFP_KERNEL);
0284     if (!p->pads || !p->gpio)
0285         return -ENOMEM;
0286 
0287     memset(p->gpio, 1, sizeof(u8) * p->max_pins);
0288     for (i = 0; i < p->func_count; i++) {
0289         if (!p->func[i]->pin_count)
0290             continue;
0291 
0292         for (j = 0; j < p->func[i]->pin_count; j++)
0293             p->gpio[p->func[i]->pins[j]] = 0;
0294     }
0295 
0296     /* pin 0 is always a gpio */
0297     p->gpio[0] = 1;
0298 
0299     /* set the pads */
0300     for (i = 0; i < p->max_pins; i++) {
0301         /* strlen("ioXY") + 1 = 5 */
0302         char *name = devm_kzalloc(p->dev, 5, GFP_KERNEL);
0303 
0304         if (!name)
0305             return -ENOMEM;
0306         snprintf(name, 5, "io%d", i);
0307         p->pads[i].number = i;
0308         p->pads[i].name = name;
0309     }
0310     p->desc->pins = p->pads;
0311     p->desc->npins = p->max_pins;
0312 
0313     return 0;
0314 }
0315 
0316 int ralink_pinctrl_init(struct platform_device *pdev,
0317             struct ralink_pmx_group *data)
0318 {
0319     struct ralink_priv *p;
0320     struct pinctrl_dev *dev;
0321     int err;
0322 
0323     if (!data)
0324         return -ENOTSUPP;
0325 
0326     /* setup the private data */
0327     p = devm_kzalloc(&pdev->dev, sizeof(struct ralink_priv), GFP_KERNEL);
0328     if (!p)
0329         return -ENOMEM;
0330 
0331     p->dev = &pdev->dev;
0332     p->desc = &ralink_pctrl_desc;
0333     p->groups = data;
0334     platform_set_drvdata(pdev, p);
0335 
0336     /* init the device */
0337     err = ralink_pinctrl_index(p);
0338     if (err) {
0339         dev_err(&pdev->dev, "failed to load index\n");
0340         return err;
0341     }
0342 
0343     err = ralink_pinctrl_pins(p);
0344     if (err) {
0345         dev_err(&pdev->dev, "failed to load pins\n");
0346         return err;
0347     }
0348     dev = pinctrl_register(p->desc, &pdev->dev, p);
0349 
0350     return PTR_ERR_OR_ZERO(dev);
0351 }