root/drivers/pinctrl/bcm/pinctrl-ns.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ns_pinctrl_get_groups_count
  2. ns_pinctrl_get_group_name
  3. ns_pinctrl_get_group_pins
  4. ns_pinctrl_get_functions_count
  5. ns_pinctrl_get_function_name
  6. ns_pinctrl_get_function_groups
  7. ns_pinctrl_set_mux
  8. ns_pinctrl_probe

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  * Copyright (C) 2018 Rafał Miłecki <rafal@milecki.pl>
   4  */
   5 
   6 #include <linux/err.h>
   7 #include <linux/io.h>
   8 #include <linux/mfd/syscon.h>
   9 #include <linux/module.h>
  10 #include <linux/of.h>
  11 #include <linux/of_device.h>
  12 #include <linux/pinctrl/pinconf-generic.h>
  13 #include <linux/pinctrl/pinctrl.h>
  14 #include <linux/pinctrl/pinmux.h>
  15 #include <linux/platform_device.h>
  16 #include <linux/regmap.h>
  17 #include <linux/slab.h>
  18 
  19 #define FLAG_BCM4708            BIT(1)
  20 #define FLAG_BCM4709            BIT(2)
  21 #define FLAG_BCM53012           BIT(3)
  22 
  23 struct ns_pinctrl {
  24         struct device *dev;
  25         unsigned int chipset_flag;
  26         struct pinctrl_dev *pctldev;
  27         struct regmap *regmap;
  28         u32 offset;
  29 
  30         struct pinctrl_desc pctldesc;
  31         struct ns_pinctrl_group *groups;
  32         unsigned int num_groups;
  33         struct ns_pinctrl_function *functions;
  34         unsigned int num_functions;
  35 };
  36 
  37 /*
  38  * Pins
  39  */
  40 
  41 static const struct pinctrl_pin_desc ns_pinctrl_pins[] = {
  42         { 0, "spi_clk", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  43         { 1, "spi_ss", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  44         { 2, "spi_mosi", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  45         { 3, "spi_miso", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  46         { 4, "i2c_scl", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  47         { 5, "i2c_sda", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  48         { 6, "mdc", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  49         { 7, "mdio", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  50         { 8, "pwm0", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  51         { 9, "pwm1", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  52         { 10, "pwm2", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  53         { 11, "pwm3", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  54         { 12, "uart1_rx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  55         { 13, "uart1_tx", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  56         { 14, "uart1_cts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  57         { 15, "uart1_rts", (void *)(FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012) },
  58         { 16, "uart2_rx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  59         { 17, "uart2_tx", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  60 /* TODO { ??, "xtal_out", (void *)(FLAG_BCM4709) }, */
  61         { 22, "sdio_pwr", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  62         { 23, "sdio_en_1p8v", (void *)(FLAG_BCM4709 | FLAG_BCM53012) },
  63 };
  64 
  65 /*
  66  * Groups
  67  */
  68 
  69 struct ns_pinctrl_group {
  70         const char *name;
  71         const unsigned int *pins;
  72         const unsigned int num_pins;
  73         unsigned int chipsets;
  74 };
  75 
  76 static const unsigned int spi_pins[] = { 0, 1, 2, 3 };
  77 static const unsigned int i2c_pins[] = { 4, 5 };
  78 static const unsigned int mdio_pins[] = { 6, 7 };
  79 static const unsigned int pwm0_pins[] = { 8 };
  80 static const unsigned int pwm1_pins[] = { 9 };
  81 static const unsigned int pwm2_pins[] = { 10 };
  82 static const unsigned int pwm3_pins[] = { 11 };
  83 static const unsigned int uart1_pins[] = { 12, 13, 14, 15 };
  84 static const unsigned int uart2_pins[] = { 16, 17 };
  85 static const unsigned int sdio_pwr_pins[] = { 22 };
  86 static const unsigned int sdio_1p8v_pins[] = { 23 };
  87 
  88 #define NS_GROUP(_name, _pins, _chipsets)               \
  89 {                                                       \
  90         .name = _name,                                  \
  91         .pins = _pins,                                  \
  92         .num_pins = ARRAY_SIZE(_pins),                  \
  93         .chipsets = _chipsets,                          \
  94 }
  95 
  96 static const struct ns_pinctrl_group ns_pinctrl_groups[] = {
  97         NS_GROUP("spi_grp", spi_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  98         NS_GROUP("i2c_grp", i2c_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
  99         NS_GROUP("mdio_grp", mdio_pins, FLAG_BCM4709 | FLAG_BCM53012),
 100         NS_GROUP("pwm0_grp", pwm0_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 101         NS_GROUP("pwm1_grp", pwm1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 102         NS_GROUP("pwm2_grp", pwm2_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 103         NS_GROUP("pwm3_grp", pwm3_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 104         NS_GROUP("uart1_grp", uart1_pins, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 105         NS_GROUP("uart2_grp", uart2_pins, FLAG_BCM4709 | FLAG_BCM53012),
 106         NS_GROUP("sdio_pwr_grp", sdio_pwr_pins, FLAG_BCM4709 | FLAG_BCM53012),
 107         NS_GROUP("sdio_1p8v_grp", sdio_1p8v_pins, FLAG_BCM4709 | FLAG_BCM53012),
 108 };
 109 
 110 /*
 111  * Functions
 112  */
 113 
 114 struct ns_pinctrl_function {
 115         const char *name;
 116         const char * const *groups;
 117         const unsigned int num_groups;
 118         unsigned int chipsets;
 119 };
 120 
 121 static const char * const spi_groups[] = { "spi_grp" };
 122 static const char * const i2c_groups[] = { "i2c_grp" };
 123 static const char * const mdio_groups[] = { "mdio_grp" };
 124 static const char * const pwm_groups[] = { "pwm0_grp", "pwm1_grp", "pwm2_grp",
 125                                            "pwm3_grp" };
 126 static const char * const uart1_groups[] = { "uart1_grp" };
 127 static const char * const uart2_groups[] = { "uart2_grp" };
 128 static const char * const sdio_groups[] = { "sdio_pwr_grp", "sdio_1p8v_grp" };
 129 
 130 #define NS_FUNCTION(_name, _groups, _chipsets)          \
 131 {                                                       \
 132         .name = _name,                                  \
 133         .groups = _groups,                              \
 134         .num_groups = ARRAY_SIZE(_groups),              \
 135         .chipsets = _chipsets,                          \
 136 }
 137 
 138 static const struct ns_pinctrl_function ns_pinctrl_functions[] = {
 139         NS_FUNCTION("spi", spi_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 140         NS_FUNCTION("i2c", i2c_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 141         NS_FUNCTION("mdio", mdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
 142         NS_FUNCTION("pwm", pwm_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 143         NS_FUNCTION("uart1", uart1_groups, FLAG_BCM4708 | FLAG_BCM4709 | FLAG_BCM53012),
 144         NS_FUNCTION("uart2", uart2_groups, FLAG_BCM4709 | FLAG_BCM53012),
 145         NS_FUNCTION("sdio", sdio_groups, FLAG_BCM4709 | FLAG_BCM53012),
 146 };
 147 
 148 /*
 149  * Groups code
 150  */
 151 
 152 static int ns_pinctrl_get_groups_count(struct pinctrl_dev *pctrl_dev)
 153 {
 154         struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
 155 
 156         return ns_pinctrl->num_groups;
 157 }
 158 
 159 static const char *ns_pinctrl_get_group_name(struct pinctrl_dev *pctrl_dev,
 160                                              unsigned int selector)
 161 {
 162         struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
 163 
 164         return ns_pinctrl->groups[selector].name;
 165 }
 166 
 167 static int ns_pinctrl_get_group_pins(struct pinctrl_dev *pctrl_dev,
 168                                      unsigned int selector,
 169                                      const unsigned int **pins,
 170                                      unsigned int *num_pins)
 171 {
 172         struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
 173 
 174         *pins = ns_pinctrl->groups[selector].pins;
 175         *num_pins = ns_pinctrl->groups[selector].num_pins;
 176 
 177         return 0;
 178 }
 179 
 180 static const struct pinctrl_ops ns_pinctrl_ops = {
 181         .get_groups_count = ns_pinctrl_get_groups_count,
 182         .get_group_name = ns_pinctrl_get_group_name,
 183         .get_group_pins = ns_pinctrl_get_group_pins,
 184         .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
 185         .dt_free_map = pinconf_generic_dt_free_map,
 186 };
 187 
 188 /*
 189  * Functions code
 190  */
 191 
 192 static int ns_pinctrl_get_functions_count(struct pinctrl_dev *pctrl_dev)
 193 {
 194         struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
 195 
 196         return ns_pinctrl->num_functions;
 197 }
 198 
 199 static const char *ns_pinctrl_get_function_name(struct pinctrl_dev *pctrl_dev,
 200                                                 unsigned int selector)
 201 {
 202         struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
 203 
 204         return ns_pinctrl->functions[selector].name;
 205 }
 206 
 207 static int ns_pinctrl_get_function_groups(struct pinctrl_dev *pctrl_dev,
 208                                           unsigned int selector,
 209                                           const char * const **groups,
 210                                           unsigned * const num_groups)
 211 {
 212         struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
 213 
 214         *groups = ns_pinctrl->functions[selector].groups;
 215         *num_groups = ns_pinctrl->functions[selector].num_groups;
 216 
 217         return 0;
 218 }
 219 
 220 static int ns_pinctrl_set_mux(struct pinctrl_dev *pctrl_dev,
 221                               unsigned int func_select,
 222                               unsigned int grp_select)
 223 {
 224         struct ns_pinctrl *ns_pinctrl = pinctrl_dev_get_drvdata(pctrl_dev);
 225         u32 unset = 0;
 226         u32 tmp;
 227         int i;
 228 
 229         for (i = 0; i < ns_pinctrl->groups[grp_select].num_pins; i++) {
 230                 int pin_number = ns_pinctrl->groups[grp_select].pins[i];
 231 
 232                 unset |= BIT(pin_number);
 233         }
 234 
 235         regmap_read(ns_pinctrl->regmap, ns_pinctrl->offset, &tmp);
 236         tmp &= ~unset;
 237         regmap_write(ns_pinctrl->regmap, ns_pinctrl->offset, tmp);
 238 
 239         return 0;
 240 }
 241 
 242 static const struct pinmux_ops ns_pinctrl_pmxops = {
 243         .get_functions_count = ns_pinctrl_get_functions_count,
 244         .get_function_name = ns_pinctrl_get_function_name,
 245         .get_function_groups = ns_pinctrl_get_function_groups,
 246         .set_mux = ns_pinctrl_set_mux,
 247 };
 248 
 249 /*
 250  * Controller code
 251  */
 252 
 253 static struct pinctrl_desc ns_pinctrl_desc = {
 254         .name = "pinctrl-ns",
 255         .pctlops = &ns_pinctrl_ops,
 256         .pmxops = &ns_pinctrl_pmxops,
 257 };
 258 
 259 static const struct of_device_id ns_pinctrl_of_match_table[] = {
 260         { .compatible = "brcm,bcm4708-pinmux", .data = (void *)FLAG_BCM4708, },
 261         { .compatible = "brcm,bcm4709-pinmux", .data = (void *)FLAG_BCM4709, },
 262         { .compatible = "brcm,bcm53012-pinmux", .data = (void *)FLAG_BCM53012, },
 263         { }
 264 };
 265 
 266 static int ns_pinctrl_probe(struct platform_device *pdev)
 267 {
 268         struct device *dev = &pdev->dev;
 269         struct device_node *np = dev->of_node;
 270         const struct of_device_id *of_id;
 271         struct ns_pinctrl *ns_pinctrl;
 272         struct pinctrl_desc *pctldesc;
 273         struct pinctrl_pin_desc *pin;
 274         struct ns_pinctrl_group *group;
 275         struct ns_pinctrl_function *function;
 276         int i;
 277 
 278         ns_pinctrl = devm_kzalloc(dev, sizeof(*ns_pinctrl), GFP_KERNEL);
 279         if (!ns_pinctrl)
 280                 return -ENOMEM;
 281         pctldesc = &ns_pinctrl->pctldesc;
 282         platform_set_drvdata(pdev, ns_pinctrl);
 283 
 284         /* Set basic properties */
 285 
 286         ns_pinctrl->dev = dev;
 287 
 288         of_id = of_match_device(ns_pinctrl_of_match_table, dev);
 289         if (!of_id)
 290                 return -EINVAL;
 291         ns_pinctrl->chipset_flag = (uintptr_t)of_id->data;
 292 
 293         ns_pinctrl->regmap = syscon_node_to_regmap(of_get_parent(np));
 294         if (IS_ERR(ns_pinctrl->regmap)) {
 295                 int err = PTR_ERR(ns_pinctrl->regmap);
 296 
 297                 dev_err(dev, "Failed to map pinctrl regs: %d\n", err);
 298 
 299                 return err;
 300         }
 301 
 302         if (of_property_read_u32(np, "offset", &ns_pinctrl->offset)) {
 303                 dev_err(dev, "Failed to get register offset\n");
 304                 return -ENOENT;
 305         }
 306 
 307         memcpy(pctldesc, &ns_pinctrl_desc, sizeof(*pctldesc));
 308 
 309         /* Set pinctrl properties */
 310 
 311         pctldesc->pins = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_pins),
 312                                       sizeof(struct pinctrl_pin_desc),
 313                                       GFP_KERNEL);
 314         if (!pctldesc->pins)
 315                 return -ENOMEM;
 316         for (i = 0, pin = (struct pinctrl_pin_desc *)&pctldesc->pins[0];
 317              i < ARRAY_SIZE(ns_pinctrl_pins); i++) {
 318                 const struct pinctrl_pin_desc *src = &ns_pinctrl_pins[i];
 319                 unsigned int chipsets = (uintptr_t)src->drv_data;
 320 
 321                 if (chipsets & ns_pinctrl->chipset_flag) {
 322                         memcpy(pin++, src, sizeof(*src));
 323                         pctldesc->npins++;
 324                 }
 325         }
 326 
 327         ns_pinctrl->groups = devm_kcalloc(dev, ARRAY_SIZE(ns_pinctrl_groups),
 328                                           sizeof(struct ns_pinctrl_group),
 329                                           GFP_KERNEL);
 330         if (!ns_pinctrl->groups)
 331                 return -ENOMEM;
 332         for (i = 0, group = &ns_pinctrl->groups[0];
 333              i < ARRAY_SIZE(ns_pinctrl_groups); i++) {
 334                 const struct ns_pinctrl_group *src = &ns_pinctrl_groups[i];
 335 
 336                 if (src->chipsets & ns_pinctrl->chipset_flag) {
 337                         memcpy(group++, src, sizeof(*src));
 338                         ns_pinctrl->num_groups++;
 339                 }
 340         }
 341 
 342         ns_pinctrl->functions = devm_kcalloc(dev,
 343                                              ARRAY_SIZE(ns_pinctrl_functions),
 344                                              sizeof(struct ns_pinctrl_function),
 345                                              GFP_KERNEL);
 346         if (!ns_pinctrl->functions)
 347                 return -ENOMEM;
 348         for (i = 0, function = &ns_pinctrl->functions[0];
 349              i < ARRAY_SIZE(ns_pinctrl_functions); i++) {
 350                 const struct ns_pinctrl_function *src = &ns_pinctrl_functions[i];
 351 
 352                 if (src->chipsets & ns_pinctrl->chipset_flag) {
 353                         memcpy(function++, src, sizeof(*src));
 354                         ns_pinctrl->num_functions++;
 355                 }
 356         }
 357 
 358         /* Register */
 359 
 360         ns_pinctrl->pctldev = devm_pinctrl_register(dev, pctldesc, ns_pinctrl);
 361         if (IS_ERR(ns_pinctrl->pctldev)) {
 362                 dev_err(dev, "Failed to register pinctrl\n");
 363                 return PTR_ERR(ns_pinctrl->pctldev);
 364         }
 365 
 366         return 0;
 367 }
 368 
 369 static struct platform_driver ns_pinctrl_driver = {
 370         .probe = ns_pinctrl_probe,
 371         .driver = {
 372                 .name = "ns-pinmux",
 373                 .of_match_table = ns_pinctrl_of_match_table,
 374         },
 375 };
 376 
 377 module_platform_driver(ns_pinctrl_driver);
 378 
 379 MODULE_AUTHOR("Rafał Miłecki");
 380 MODULE_LICENSE("GPL v2");
 381 MODULE_DEVICE_TABLE(of, ns_pinctrl_of_match_table);

/* [<][>][^][v][top][bottom][index][help] */