1 /* 2 * Driver for the ST Microelectronics SPEAr pinmux 3 * 4 * Copyright (C) 2012 ST Microelectronics 5 * Viresh Kumar <viresh.linux@gmail.com> 6 * 7 * Inspired from: 8 * - U300 Pinctl drivers 9 * - Tegra Pinctl drivers 10 * 11 * This file is licensed under the terms of the GNU General Public 12 * License version 2. This program is licensed "as is" without any 13 * warranty of any kind, whether express or implied. 14 */ 15 16 #include <linux/err.h> 17 #include <linux/module.h> 18 #include <linux/of.h> 19 #include <linux/of_address.h> 20 #include <linux/of_gpio.h> 21 #include <linux/pinctrl/machine.h> 22 #include <linux/pinctrl/pinctrl.h> 23 #include <linux/pinctrl/pinmux.h> 24 #include <linux/platform_device.h> 25 #include <linux/slab.h> 26 27 #include "pinctrl-spear.h" 28 29 #define DRIVER_NAME "spear-pinmux" 30 31 static void muxregs_endisable(struct spear_pmx *pmx, 32 struct spear_muxreg *muxregs, u8 count, bool enable) 33 { 34 struct spear_muxreg *muxreg; 35 u32 val, temp, j; 36 37 for (j = 0; j < count; j++) { 38 muxreg = &muxregs[j]; 39 40 val = pmx_readl(pmx, muxreg->reg); 41 val &= ~muxreg->mask; 42 43 if (enable) 44 temp = muxreg->val; 45 else 46 temp = ~muxreg->val; 47 48 val |= muxreg->mask & temp; 49 pmx_writel(pmx, val, muxreg->reg); 50 } 51 } 52 53 static int set_mode(struct spear_pmx *pmx, int mode) 54 { 55 struct spear_pmx_mode *pmx_mode = NULL; 56 int i; 57 u32 val; 58 59 if (!pmx->machdata->pmx_modes || !pmx->machdata->npmx_modes) 60 return -EINVAL; 61 62 for (i = 0; i < pmx->machdata->npmx_modes; i++) { 63 if (pmx->machdata->pmx_modes[i]->mode == (1 << mode)) { 64 pmx_mode = pmx->machdata->pmx_modes[i]; 65 break; 66 } 67 } 68 69 if (!pmx_mode) 70 return -EINVAL; 71 72 val = pmx_readl(pmx, pmx_mode->reg); 73 val &= ~pmx_mode->mask; 74 val |= pmx_mode->val; 75 pmx_writel(pmx, val, pmx_mode->reg); 76 77 pmx->machdata->mode = pmx_mode->mode; 78 dev_info(pmx->dev, "Configured Mode: %s with id: %x\n\n", 79 pmx_mode->name ? pmx_mode->name : "no_name", 80 pmx_mode->reg); 81 82 return 0; 83 } 84 85 void __devinit 86 pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup *gpio_pingroup, 87 unsigned count, u16 reg) 88 { 89 int i, j; 90 91 for (i = 0; i < count; i++) 92 for (j = 0; j < gpio_pingroup[i].nmuxregs; j++) 93 gpio_pingroup[i].muxregs[j].reg = reg; 94 } 95 96 void __devinit pmx_init_addr(struct spear_pinctrl_machdata *machdata, u16 reg) 97 { 98 struct spear_pingroup *pgroup; 99 struct spear_modemux *modemux; 100 int i, j, group; 101 102 for (group = 0; group < machdata->ngroups; group++) { 103 pgroup = machdata->groups[group]; 104 105 for (i = 0; i < pgroup->nmodemuxs; i++) { 106 modemux = &pgroup->modemuxs[i]; 107 108 for (j = 0; j < modemux->nmuxregs; j++) 109 if (modemux->muxregs[j].reg == 0xFFFF) 110 modemux->muxregs[j].reg = reg; 111 } 112 } 113 } 114 115 static int spear_pinctrl_get_groups_cnt(struct pinctrl_dev *pctldev) 116 { 117 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 118 119 return pmx->machdata->ngroups; 120 } 121 122 static const char *spear_pinctrl_get_group_name(struct pinctrl_dev *pctldev, 123 unsigned group) 124 { 125 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 126 127 return pmx->machdata->groups[group]->name; 128 } 129 130 static int spear_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, 131 unsigned group, const unsigned **pins, unsigned *num_pins) 132 { 133 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 134 135 *pins = pmx->machdata->groups[group]->pins; 136 *num_pins = pmx->machdata->groups[group]->npins; 137 138 return 0; 139 } 140 141 static void spear_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev, 142 struct seq_file *s, unsigned offset) 143 { 144 seq_printf(s, " " DRIVER_NAME); 145 } 146 147 static int spear_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev, 148 struct device_node *np_config, 149 struct pinctrl_map **map, 150 unsigned *num_maps) 151 { 152 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 153 struct device_node *np; 154 struct property *prop; 155 const char *function, *group; 156 int ret, index = 0, count = 0; 157 158 /* calculate number of maps required */ 159 for_each_child_of_node(np_config, np) { 160 ret = of_property_read_string(np, "st,function", &function); 161 if (ret < 0) 162 return ret; 163 164 ret = of_property_count_strings(np, "st,pins"); 165 if (ret < 0) 166 return ret; 167 168 count += ret; 169 } 170 171 if (!count) { 172 dev_err(pmx->dev, "No child nodes passed via DT\n"); 173 return -ENODEV; 174 } 175 176 *map = kzalloc(sizeof(**map) * count, GFP_KERNEL); 177 if (!*map) 178 return -ENOMEM; 179 180 for_each_child_of_node(np_config, np) { 181 of_property_read_string(np, "st,function", &function); 182 of_property_for_each_string(np, "st,pins", prop, group) { 183 (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP; 184 (*map)[index].data.mux.group = group; 185 (*map)[index].data.mux.function = function; 186 index++; 187 } 188 } 189 190 *num_maps = count; 191 192 return 0; 193 } 194 195 static void spear_pinctrl_dt_free_map(struct pinctrl_dev *pctldev, 196 struct pinctrl_map *map, 197 unsigned num_maps) 198 { 199 kfree(map); 200 } 201 202 static struct pinctrl_ops spear_pinctrl_ops = { 203 .get_groups_count = spear_pinctrl_get_groups_cnt, 204 .get_group_name = spear_pinctrl_get_group_name, 205 .get_group_pins = spear_pinctrl_get_group_pins, 206 .pin_dbg_show = spear_pinctrl_pin_dbg_show, 207 .dt_node_to_map = spear_pinctrl_dt_node_to_map, 208 .dt_free_map = spear_pinctrl_dt_free_map, 209 }; 210 211 static int spear_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev) 212 { 213 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 214 215 return pmx->machdata->nfunctions; 216 } 217 218 static const char *spear_pinctrl_get_func_name(struct pinctrl_dev *pctldev, 219 unsigned function) 220 { 221 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 222 223 return pmx->machdata->functions[function]->name; 224 } 225 226 static int spear_pinctrl_get_func_groups(struct pinctrl_dev *pctldev, 227 unsigned function, const char *const **groups, 228 unsigned * const ngroups) 229 { 230 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 231 232 *groups = pmx->machdata->functions[function]->groups; 233 *ngroups = pmx->machdata->functions[function]->ngroups; 234 235 return 0; 236 } 237 238 static int spear_pinctrl_endisable(struct pinctrl_dev *pctldev, 239 unsigned function, unsigned group, bool enable) 240 { 241 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 242 const struct spear_pingroup *pgroup; 243 const struct spear_modemux *modemux; 244 int i; 245 bool found = false; 246 247 pgroup = pmx->machdata->groups[group]; 248 249 for (i = 0; i < pgroup->nmodemuxs; i++) { 250 modemux = &pgroup->modemuxs[i]; 251 252 /* SoC have any modes */ 253 if (pmx->machdata->modes_supported) { 254 if (!(pmx->machdata->mode & modemux->modes)) 255 continue; 256 } 257 258 found = true; 259 muxregs_endisable(pmx, modemux->muxregs, modemux->nmuxregs, 260 enable); 261 } 262 263 if (!found) { 264 dev_err(pmx->dev, "pinmux group: %s not supported\n", 265 pgroup->name); 266 return -ENODEV; 267 } 268 269 return 0; 270 } 271 272 static int spear_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned function, 273 unsigned group) 274 { 275 return spear_pinctrl_endisable(pctldev, function, group, true); 276 } 277 278 static void spear_pinctrl_disable(struct pinctrl_dev *pctldev, 279 unsigned function, unsigned group) 280 { 281 spear_pinctrl_endisable(pctldev, function, group, false); 282 } 283 284 /* gpio with pinmux */ 285 static struct spear_gpio_pingroup *get_gpio_pingroup(struct spear_pmx *pmx, 286 unsigned pin) 287 { 288 struct spear_gpio_pingroup *gpio_pingroup; 289 int i, j; 290 291 if (!pmx->machdata->gpio_pingroups) 292 return NULL; 293 294 for (i = 0; i < pmx->machdata->ngpio_pingroups; i++) { 295 gpio_pingroup = &pmx->machdata->gpio_pingroups[i]; 296 297 for (j = 0; j < gpio_pingroup->npins; j++) { 298 if (gpio_pingroup->pins[j] == pin) 299 return gpio_pingroup; 300 } 301 } 302 303 return NULL; 304 } 305 306 static int gpio_request_endisable(struct pinctrl_dev *pctldev, 307 struct pinctrl_gpio_range *range, unsigned offset, bool enable) 308 { 309 struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev); 310 struct spear_pinctrl_machdata *machdata = pmx->machdata; 311 struct spear_gpio_pingroup *gpio_pingroup; 312 313 /* 314 * Some SoC have configuration options applicable to group of pins, 315 * rather than a single pin. 316 */ 317 gpio_pingroup = get_gpio_pingroup(pmx, offset); 318 if (gpio_pingroup) 319 muxregs_endisable(pmx, gpio_pingroup->muxregs, 320 gpio_pingroup->nmuxregs, enable); 321 322 /* 323 * SoC may need some extra configurations, or configurations for single 324 * pin 325 */ 326 if (machdata->gpio_request_endisable) 327 machdata->gpio_request_endisable(pmx, offset, enable); 328 329 return 0; 330 } 331 332 static int gpio_request_enable(struct pinctrl_dev *pctldev, 333 struct pinctrl_gpio_range *range, unsigned offset) 334 { 335 return gpio_request_endisable(pctldev, range, offset, true); 336 } 337 338 static void gpio_disable_free(struct pinctrl_dev *pctldev, 339 struct pinctrl_gpio_range *range, unsigned offset) 340 { 341 gpio_request_endisable(pctldev, range, offset, false); 342 } 343 344 static struct pinmux_ops spear_pinmux_ops = { 345 .get_functions_count = spear_pinctrl_get_funcs_count, 346 .get_function_name = spear_pinctrl_get_func_name, 347 .get_function_groups = spear_pinctrl_get_func_groups, 348 .enable = spear_pinctrl_enable, 349 .disable = spear_pinctrl_disable, 350 .gpio_request_enable = gpio_request_enable, 351 .gpio_disable_free = gpio_disable_free, 352 }; 353 354 static struct pinctrl_desc spear_pinctrl_desc = { 355 .name = DRIVER_NAME, 356 .pctlops = &spear_pinctrl_ops, 357 .pmxops = &spear_pinmux_ops, 358 .owner = THIS_MODULE, 359 }; 360 361 int __devinit spear_pinctrl_probe(struct platform_device *pdev, 362 struct spear_pinctrl_machdata *machdata) 363 { 364 struct device_node *np = pdev->dev.of_node; 365 struct resource *res; 366 struct spear_pmx *pmx; 367 368 if (!machdata) 369 return -ENODEV; 370 371 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 372 if (!res) 373 return -EINVAL; 374 375 pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL); 376 if (!pmx) { 377 dev_err(&pdev->dev, "Can't alloc spear_pmx\n"); 378 return -ENOMEM; 379 } 380 381 pmx->vbase = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 382 if (!pmx->vbase) { 383 dev_err(&pdev->dev, "Couldn't ioremap at index 0\n"); 384 return -ENODEV; 385 } 386 387 pmx->dev = &pdev->dev; 388 pmx->machdata = machdata; 389 390 /* configure mode, if supported by SoC */ 391 if (machdata->modes_supported) { 392 int mode = 0; 393 394 if (of_property_read_u32(np, "st,pinmux-mode", &mode)) { 395 dev_err(&pdev->dev, "OF: pinmux mode not passed\n"); 396 return -EINVAL; 397 } 398 399 if (set_mode(pmx, mode)) { 400 dev_err(&pdev->dev, "OF: Couldn't configure mode: %x\n", 401 mode); 402 return -EINVAL; 403 } 404 } 405 406 platform_set_drvdata(pdev, pmx); 407 408 spear_pinctrl_desc.pins = machdata->pins; 409 spear_pinctrl_desc.npins = machdata->npins; 410 411 pmx->pctl = pinctrl_register(&spear_pinctrl_desc, &pdev->dev, pmx); 412 if (!pmx->pctl) { 413 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n"); 414 return -ENODEV; 415 } 416 417 return 0; 418 } 419 420 int spear_pinctrl_remove(struct platform_device *pdev) 421 { 422 struct spear_pmx *pmx = platform_get_drvdata(pdev); 423 424 pinctrl_unregister(pmx->pctl); 425 426 return 0; 427 } 428