Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Driver for the ST Microelectronics SPEAr pinmux
0003  *
0004  * Copyright (C) 2012 ST Microelectronics
0005  * Viresh Kumar <vireshk@kernel.org>
0006  *
0007  * Inspired from:
0008  * - U300 Pinctl drivers
0009  * - Tegra Pinctl drivers
0010  *
0011  * This file is licensed under the terms of the GNU General Public
0012  * License version 2. This program is licensed "as is" without any
0013  * warranty of any kind, whether express or implied.
0014  */
0015 
0016 #include <linux/err.h>
0017 #include <linux/mfd/syscon.h>
0018 #include <linux/module.h>
0019 #include <linux/of.h>
0020 #include <linux/of_address.h>
0021 #include <linux/of_gpio.h>
0022 #include <linux/pinctrl/machine.h>
0023 #include <linux/pinctrl/pinctrl.h>
0024 #include <linux/pinctrl/pinmux.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/slab.h>
0027 
0028 #include "pinctrl-spear.h"
0029 
0030 #define DRIVER_NAME "spear-pinmux"
0031 
0032 static void muxregs_endisable(struct spear_pmx *pmx,
0033         struct spear_muxreg *muxregs, u8 count, bool enable)
0034 {
0035     struct spear_muxreg *muxreg;
0036     u32 val, temp, j;
0037 
0038     for (j = 0; j < count; j++) {
0039         muxreg = &muxregs[j];
0040 
0041         val = pmx_readl(pmx, muxreg->reg);
0042         val &= ~muxreg->mask;
0043 
0044         if (enable)
0045             temp = muxreg->val;
0046         else
0047             temp = ~muxreg->val;
0048 
0049         val |= muxreg->mask & temp;
0050         pmx_writel(pmx, val, muxreg->reg);
0051     }
0052 }
0053 
0054 static int set_mode(struct spear_pmx *pmx, int mode)
0055 {
0056     struct spear_pmx_mode *pmx_mode = NULL;
0057     int i;
0058     u32 val;
0059 
0060     if (!pmx->machdata->pmx_modes || !pmx->machdata->npmx_modes)
0061         return -EINVAL;
0062 
0063     for (i = 0; i < pmx->machdata->npmx_modes; i++) {
0064         if (pmx->machdata->pmx_modes[i]->mode == (1 << mode)) {
0065             pmx_mode = pmx->machdata->pmx_modes[i];
0066             break;
0067         }
0068     }
0069 
0070     if (!pmx_mode)
0071         return -EINVAL;
0072 
0073     val = pmx_readl(pmx, pmx_mode->reg);
0074     val &= ~pmx_mode->mask;
0075     val |= pmx_mode->val;
0076     pmx_writel(pmx, val, pmx_mode->reg);
0077 
0078     pmx->machdata->mode = pmx_mode->mode;
0079     dev_info(pmx->dev, "Configured Mode: %s with id: %x\n\n",
0080             pmx_mode->name ? pmx_mode->name : "no_name",
0081             pmx_mode->reg);
0082 
0083     return 0;
0084 }
0085 
0086 void pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup *gpio_pingroup,
0087                  unsigned count, u16 reg)
0088 {
0089     int i, j;
0090 
0091     for (i = 0; i < count; i++)
0092         for (j = 0; j < gpio_pingroup[i].nmuxregs; j++)
0093             gpio_pingroup[i].muxregs[j].reg = reg;
0094 }
0095 
0096 void pmx_init_addr(struct spear_pinctrl_machdata *machdata, u16 reg)
0097 {
0098     struct spear_pingroup *pgroup;
0099     struct spear_modemux *modemux;
0100     int i, j, group;
0101 
0102     for (group = 0; group < machdata->ngroups; group++) {
0103         pgroup = machdata->groups[group];
0104 
0105         for (i = 0; i < pgroup->nmodemuxs; i++) {
0106             modemux = &pgroup->modemuxs[i];
0107 
0108             for (j = 0; j < modemux->nmuxregs; j++)
0109                 if (modemux->muxregs[j].reg == 0xFFFF)
0110                     modemux->muxregs[j].reg = reg;
0111         }
0112     }
0113 }
0114 
0115 static int spear_pinctrl_get_groups_cnt(struct pinctrl_dev *pctldev)
0116 {
0117     struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0118 
0119     return pmx->machdata->ngroups;
0120 }
0121 
0122 static const char *spear_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
0123         unsigned group)
0124 {
0125     struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0126 
0127     return pmx->machdata->groups[group]->name;
0128 }
0129 
0130 static int spear_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
0131         unsigned group, const unsigned **pins, unsigned *num_pins)
0132 {
0133     struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0134 
0135     *pins = pmx->machdata->groups[group]->pins;
0136     *num_pins = pmx->machdata->groups[group]->npins;
0137 
0138     return 0;
0139 }
0140 
0141 static void spear_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
0142         struct seq_file *s, unsigned offset)
0143 {
0144     seq_printf(s, " " DRIVER_NAME);
0145 }
0146 
0147 static int spear_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 spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0153     struct device_node *np;
0154     struct property *prop;
0155     const char *function, *group;
0156     int ret, index = 0, count = 0;
0157 
0158     /* calculate number of maps required */
0159     for_each_child_of_node(np_config, np) {
0160         ret = of_property_read_string(np, "st,function", &function);
0161         if (ret < 0) {
0162             of_node_put(np);
0163             return ret;
0164         }
0165 
0166         ret = of_property_count_strings(np, "st,pins");
0167         if (ret < 0) {
0168             of_node_put(np);
0169             return ret;
0170         }
0171 
0172         count += ret;
0173     }
0174 
0175     if (!count) {
0176         dev_err(pmx->dev, "No child nodes passed via DT\n");
0177         return -ENODEV;
0178     }
0179 
0180     *map = kcalloc(count, sizeof(**map), GFP_KERNEL);
0181     if (!*map)
0182         return -ENOMEM;
0183 
0184     for_each_child_of_node(np_config, np) {
0185         of_property_read_string(np, "st,function", &function);
0186         of_property_for_each_string(np, "st,pins", prop, group) {
0187             (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
0188             (*map)[index].data.mux.group = group;
0189             (*map)[index].data.mux.function = function;
0190             index++;
0191         }
0192     }
0193 
0194     *num_maps = count;
0195 
0196     return 0;
0197 }
0198 
0199 static void spear_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
0200                       struct pinctrl_map *map,
0201                       unsigned num_maps)
0202 {
0203     kfree(map);
0204 }
0205 
0206 static const struct pinctrl_ops spear_pinctrl_ops = {
0207     .get_groups_count = spear_pinctrl_get_groups_cnt,
0208     .get_group_name = spear_pinctrl_get_group_name,
0209     .get_group_pins = spear_pinctrl_get_group_pins,
0210     .pin_dbg_show = spear_pinctrl_pin_dbg_show,
0211     .dt_node_to_map = spear_pinctrl_dt_node_to_map,
0212     .dt_free_map = spear_pinctrl_dt_free_map,
0213 };
0214 
0215 static int spear_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
0216 {
0217     struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0218 
0219     return pmx->machdata->nfunctions;
0220 }
0221 
0222 static const char *spear_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
0223         unsigned function)
0224 {
0225     struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0226 
0227     return pmx->machdata->functions[function]->name;
0228 }
0229 
0230 static int spear_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
0231         unsigned function, const char *const **groups,
0232         unsigned * const ngroups)
0233 {
0234     struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0235 
0236     *groups = pmx->machdata->functions[function]->groups;
0237     *ngroups = pmx->machdata->functions[function]->ngroups;
0238 
0239     return 0;
0240 }
0241 
0242 static int spear_pinctrl_endisable(struct pinctrl_dev *pctldev,
0243         unsigned function, unsigned group, bool enable)
0244 {
0245     struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0246     const struct spear_pingroup *pgroup;
0247     const struct spear_modemux *modemux;
0248     int i;
0249     bool found = false;
0250 
0251     pgroup = pmx->machdata->groups[group];
0252 
0253     for (i = 0; i < pgroup->nmodemuxs; i++) {
0254         modemux = &pgroup->modemuxs[i];
0255 
0256         /* SoC have any modes */
0257         if (pmx->machdata->modes_supported) {
0258             if (!(pmx->machdata->mode & modemux->modes))
0259                 continue;
0260         }
0261 
0262         found = true;
0263         muxregs_endisable(pmx, modemux->muxregs, modemux->nmuxregs,
0264                 enable);
0265     }
0266 
0267     if (!found) {
0268         dev_err(pmx->dev, "pinmux group: %s not supported\n",
0269                 pgroup->name);
0270         return -ENODEV;
0271     }
0272 
0273     return 0;
0274 }
0275 
0276 static int spear_pinctrl_set_mux(struct pinctrl_dev *pctldev, unsigned function,
0277         unsigned group)
0278 {
0279     return spear_pinctrl_endisable(pctldev, function, group, true);
0280 }
0281 
0282 /* gpio with pinmux */
0283 static struct spear_gpio_pingroup *get_gpio_pingroup(struct spear_pmx *pmx,
0284         unsigned pin)
0285 {
0286     struct spear_gpio_pingroup *gpio_pingroup;
0287     int i, j;
0288 
0289     if (!pmx->machdata->gpio_pingroups)
0290         return NULL;
0291 
0292     for (i = 0; i < pmx->machdata->ngpio_pingroups; i++) {
0293         gpio_pingroup = &pmx->machdata->gpio_pingroups[i];
0294 
0295         for (j = 0; j < gpio_pingroup->npins; j++) {
0296             if (gpio_pingroup->pins[j] == pin)
0297                 return gpio_pingroup;
0298         }
0299     }
0300 
0301     return NULL;
0302 }
0303 
0304 static int gpio_request_endisable(struct pinctrl_dev *pctldev,
0305         struct pinctrl_gpio_range *range, unsigned offset, bool enable)
0306 {
0307     struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
0308     struct spear_pinctrl_machdata *machdata = pmx->machdata;
0309     struct spear_gpio_pingroup *gpio_pingroup;
0310 
0311     /*
0312      * Some SoC have configuration options applicable to group of pins,
0313      * rather than a single pin.
0314      */
0315     gpio_pingroup = get_gpio_pingroup(pmx, offset);
0316     if (gpio_pingroup)
0317         muxregs_endisable(pmx, gpio_pingroup->muxregs,
0318                 gpio_pingroup->nmuxregs, enable);
0319 
0320     /*
0321      * SoC may need some extra configurations, or configurations for single
0322      * pin
0323      */
0324     if (machdata->gpio_request_endisable)
0325         machdata->gpio_request_endisable(pmx, offset, enable);
0326 
0327     return 0;
0328 }
0329 
0330 static int gpio_request_enable(struct pinctrl_dev *pctldev,
0331         struct pinctrl_gpio_range *range, unsigned offset)
0332 {
0333     return gpio_request_endisable(pctldev, range, offset, true);
0334 }
0335 
0336 static void gpio_disable_free(struct pinctrl_dev *pctldev,
0337         struct pinctrl_gpio_range *range, unsigned offset)
0338 {
0339     gpio_request_endisable(pctldev, range, offset, false);
0340 }
0341 
0342 static const struct pinmux_ops spear_pinmux_ops = {
0343     .get_functions_count = spear_pinctrl_get_funcs_count,
0344     .get_function_name = spear_pinctrl_get_func_name,
0345     .get_function_groups = spear_pinctrl_get_func_groups,
0346     .set_mux = spear_pinctrl_set_mux,
0347     .gpio_request_enable = gpio_request_enable,
0348     .gpio_disable_free = gpio_disable_free,
0349 };
0350 
0351 static struct pinctrl_desc spear_pinctrl_desc = {
0352     .name = DRIVER_NAME,
0353     .pctlops = &spear_pinctrl_ops,
0354     .pmxops = &spear_pinmux_ops,
0355     .owner = THIS_MODULE,
0356 };
0357 
0358 int spear_pinctrl_probe(struct platform_device *pdev,
0359             struct spear_pinctrl_machdata *machdata)
0360 {
0361     struct device_node *np = pdev->dev.of_node;
0362     struct spear_pmx *pmx;
0363 
0364     if (!machdata)
0365         return -ENODEV;
0366 
0367     pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
0368     if (!pmx)
0369         return -ENOMEM;
0370 
0371     pmx->regmap = device_node_to_regmap(np);
0372     if (IS_ERR(pmx->regmap)) {
0373         dev_err(&pdev->dev, "Init regmap failed (%pe).\n",
0374             pmx->regmap);
0375         return PTR_ERR(pmx->regmap);
0376     }
0377 
0378     pmx->dev = &pdev->dev;
0379     pmx->machdata = machdata;
0380 
0381     /* configure mode, if supported by SoC */
0382     if (machdata->modes_supported) {
0383         int mode = 0;
0384 
0385         if (of_property_read_u32(np, "st,pinmux-mode", &mode)) {
0386             dev_err(&pdev->dev, "OF: pinmux mode not passed\n");
0387             return -EINVAL;
0388         }
0389 
0390         if (set_mode(pmx, mode)) {
0391             dev_err(&pdev->dev, "OF: Couldn't configure mode: %x\n",
0392                     mode);
0393             return -EINVAL;
0394         }
0395     }
0396 
0397     platform_set_drvdata(pdev, pmx);
0398 
0399     spear_pinctrl_desc.pins = machdata->pins;
0400     spear_pinctrl_desc.npins = machdata->npins;
0401 
0402     pmx->pctl = devm_pinctrl_register(&pdev->dev, &spear_pinctrl_desc, pmx);
0403     if (IS_ERR(pmx->pctl)) {
0404         dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
0405         return PTR_ERR(pmx->pctl);
0406     }
0407 
0408     return 0;
0409 }