blob: f4bc602cdb087f681ee412ee1290774ae303b63f [file] [log] [blame]
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001/*
2 * Generic device tree based pinctrl driver for one register per pin
3 * type pinmux controllers
4 *
5 * Copyright (C) 2012 Texas Instruments, Inc.
6 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/io.h>
15#include <linux/slab.h>
16#include <linux/err.h>
17#include <linux/list.h>
18
19#include <linux/of.h>
20#include <linux/of_device.h>
21#include <linux/of_address.h>
22
23#include <linux/pinctrl/pinctrl.h>
24#include <linux/pinctrl/pinmux.h>
25
26#include "core.h"
27
28#define DRIVER_NAME "pinctrl-single"
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +030029#define PCS_MUX_PINS_NAME "pinctrl-single,pins"
30#define PCS_MUX_BITS_NAME "pinctrl-single,bits"
Tony Lindgren8b8b0912012-07-10 02:05:46 -070031#define PCS_REG_NAME_LEN ((sizeof(unsigned long) * 2) + 1)
32#define PCS_OFF_DISABLED ~0U
33
34/**
35 * struct pcs_pingroup - pingroups for a function
36 * @np: pingroup device node pointer
37 * @name: pingroup name
38 * @gpins: array of the pins in the group
39 * @ngpins: number of pins in the group
40 * @node: list node
41 */
42struct pcs_pingroup {
43 struct device_node *np;
44 const char *name;
45 int *gpins;
46 int ngpins;
47 struct list_head node;
48};
49
50/**
51 * struct pcs_func_vals - mux function register offset and value pair
52 * @reg: register virtual address
53 * @val: register value
54 */
55struct pcs_func_vals {
56 void __iomem *reg;
57 unsigned val;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +030058 unsigned mask;
Tony Lindgren8b8b0912012-07-10 02:05:46 -070059};
60
61/**
62 * struct pcs_function - pinctrl function
63 * @name: pinctrl function name
64 * @vals: register and vals array
65 * @nvals: number of entries in vals array
66 * @pgnames: array of pingroup names the function uses
67 * @npgnames: number of pingroup names the function uses
68 * @node: list node
69 */
70struct pcs_function {
71 const char *name;
72 struct pcs_func_vals *vals;
73 unsigned nvals;
74 const char **pgnames;
75 int npgnames;
76 struct list_head node;
77};
78
79/**
Haojian Zhuanga1a277e2013-02-17 19:42:52 +080080 * struct pcs_gpiofunc_range - pin ranges with same mux value of gpio function
81 * @offset: offset base of pins
82 * @npins: number pins with the same mux value of gpio function
83 * @gpiofunc: mux value of gpio function
84 * @node: list node
85 */
86struct pcs_gpiofunc_range {
87 unsigned offset;
88 unsigned npins;
89 unsigned gpiofunc;
90 struct list_head node;
91};
92
93/**
Tony Lindgren8b8b0912012-07-10 02:05:46 -070094 * struct pcs_data - wrapper for data needed by pinctrl framework
95 * @pa: pindesc array
96 * @cur: index to current element
97 *
98 * REVISIT: We should be able to drop this eventually by adding
99 * support for registering pins individually in the pinctrl
100 * framework for those drivers that don't need a static array.
101 */
102struct pcs_data {
103 struct pinctrl_pin_desc *pa;
104 int cur;
105};
106
107/**
108 * struct pcs_name - register name for a pin
109 * @name: name of the pinctrl register
110 *
111 * REVISIT: We may want to make names optional in the pinctrl
112 * framework as some drivers may not care about pin names to
113 * avoid kernel bloat. The pin names can be deciphered by user
114 * space tools using debugfs based on the register address and
115 * SoC packaging information.
116 */
117struct pcs_name {
118 char name[PCS_REG_NAME_LEN];
119};
120
121/**
122 * struct pcs_device - pinctrl device instance
123 * @res: resources
124 * @base: virtual address of the controller
125 * @size: size of the ioremapped area
126 * @dev: device entry
127 * @pctl: pin controller device
128 * @mutex: mutex protecting the lists
129 * @width: bits per mux register
130 * @fmask: function register mask
131 * @fshift: function register shift
132 * @foff: value to turn mux off
133 * @fmax: max number of functions in fmask
134 * @names: array of register names for pins
135 * @pins: physical pins on the SoC
136 * @pgtree: pingroup index radix tree
137 * @ftree: function index radix tree
138 * @pingroups: list of pingroups
139 * @functions: list of functions
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800140 * @gpiofuncs: list of gpio functions
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700141 * @ngroups: number of pingroups
142 * @nfuncs: number of functions
143 * @desc: pin controller descriptor
144 * @read: register read function to use
145 * @write: register write function to use
146 */
147struct pcs_device {
148 struct resource *res;
149 void __iomem *base;
150 unsigned size;
151 struct device *dev;
152 struct pinctrl_dev *pctl;
153 struct mutex mutex;
154 unsigned width;
155 unsigned fmask;
156 unsigned fshift;
157 unsigned foff;
158 unsigned fmax;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300159 bool bits_per_mux;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700160 struct pcs_name *names;
161 struct pcs_data pins;
162 struct radix_tree_root pgtree;
163 struct radix_tree_root ftree;
164 struct list_head pingroups;
165 struct list_head functions;
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800166 struct list_head gpiofuncs;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700167 unsigned ngroups;
168 unsigned nfuncs;
169 struct pinctrl_desc desc;
170 unsigned (*read)(void __iomem *reg);
171 void (*write)(unsigned val, void __iomem *reg);
172};
173
174/*
175 * REVISIT: Reads and writes could eventually use regmap or something
176 * generic. But at least on omaps, some mux registers are performance
177 * critical as they may need to be remuxed every time before and after
178 * idle. Adding tests for register access width for every read and
179 * write like regmap is doing is not desired, and caching the registers
180 * does not help in this case.
181 */
182
183static unsigned __maybe_unused pcs_readb(void __iomem *reg)
184{
185 return readb(reg);
186}
187
188static unsigned __maybe_unused pcs_readw(void __iomem *reg)
189{
190 return readw(reg);
191}
192
193static unsigned __maybe_unused pcs_readl(void __iomem *reg)
194{
195 return readl(reg);
196}
197
198static void __maybe_unused pcs_writeb(unsigned val, void __iomem *reg)
199{
200 writeb(val, reg);
201}
202
203static void __maybe_unused pcs_writew(unsigned val, void __iomem *reg)
204{
205 writew(val, reg);
206}
207
208static void __maybe_unused pcs_writel(unsigned val, void __iomem *reg)
209{
210 writel(val, reg);
211}
212
213static int pcs_get_groups_count(struct pinctrl_dev *pctldev)
214{
215 struct pcs_device *pcs;
216
217 pcs = pinctrl_dev_get_drvdata(pctldev);
218
219 return pcs->ngroups;
220}
221
222static const char *pcs_get_group_name(struct pinctrl_dev *pctldev,
223 unsigned gselector)
224{
225 struct pcs_device *pcs;
226 struct pcs_pingroup *group;
227
228 pcs = pinctrl_dev_get_drvdata(pctldev);
229 group = radix_tree_lookup(&pcs->pgtree, gselector);
230 if (!group) {
231 dev_err(pcs->dev, "%s could not find pingroup%i\n",
232 __func__, gselector);
233 return NULL;
234 }
235
236 return group->name;
237}
238
239static int pcs_get_group_pins(struct pinctrl_dev *pctldev,
240 unsigned gselector,
241 const unsigned **pins,
242 unsigned *npins)
243{
244 struct pcs_device *pcs;
245 struct pcs_pingroup *group;
246
247 pcs = pinctrl_dev_get_drvdata(pctldev);
248 group = radix_tree_lookup(&pcs->pgtree, gselector);
249 if (!group) {
250 dev_err(pcs->dev, "%s could not find pingroup%i\n",
251 __func__, gselector);
252 return -EINVAL;
253 }
254
255 *pins = group->gpins;
256 *npins = group->ngpins;
257
258 return 0;
259}
260
261static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev,
262 struct seq_file *s,
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800263 unsigned pin)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700264{
Matt Porter7d66ce72012-09-26 15:07:43 -0400265 struct pcs_device *pcs;
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800266 unsigned val, mux_bytes;
Matt Porter7d66ce72012-09-26 15:07:43 -0400267
268 pcs = pinctrl_dev_get_drvdata(pctldev);
269
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800270 mux_bytes = pcs->width / BITS_PER_BYTE;
271 val = pcs->read(pcs->base + pin * mux_bytes);
Matt Porter7d66ce72012-09-26 15:07:43 -0400272
273 seq_printf(s, "%08x %s " , val, DRIVER_NAME);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700274}
275
276static void pcs_dt_free_map(struct pinctrl_dev *pctldev,
277 struct pinctrl_map *map, unsigned num_maps)
278{
279 struct pcs_device *pcs;
280
281 pcs = pinctrl_dev_get_drvdata(pctldev);
282 devm_kfree(pcs->dev, map);
283}
284
285static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
286 struct device_node *np_config,
287 struct pinctrl_map **map, unsigned *num_maps);
288
Laurent Pinchart022ab142013-02-16 10:25:07 +0100289static const struct pinctrl_ops pcs_pinctrl_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700290 .get_groups_count = pcs_get_groups_count,
291 .get_group_name = pcs_get_group_name,
292 .get_group_pins = pcs_get_group_pins,
293 .pin_dbg_show = pcs_pin_dbg_show,
294 .dt_node_to_map = pcs_dt_node_to_map,
295 .dt_free_map = pcs_dt_free_map,
296};
297
298static int pcs_get_functions_count(struct pinctrl_dev *pctldev)
299{
300 struct pcs_device *pcs;
301
302 pcs = pinctrl_dev_get_drvdata(pctldev);
303
304 return pcs->nfuncs;
305}
306
307static const char *pcs_get_function_name(struct pinctrl_dev *pctldev,
308 unsigned fselector)
309{
310 struct pcs_device *pcs;
311 struct pcs_function *func;
312
313 pcs = pinctrl_dev_get_drvdata(pctldev);
314 func = radix_tree_lookup(&pcs->ftree, fselector);
315 if (!func) {
316 dev_err(pcs->dev, "%s could not find function%i\n",
317 __func__, fselector);
318 return NULL;
319 }
320
321 return func->name;
322}
323
324static int pcs_get_function_groups(struct pinctrl_dev *pctldev,
325 unsigned fselector,
326 const char * const **groups,
327 unsigned * const ngroups)
328{
329 struct pcs_device *pcs;
330 struct pcs_function *func;
331
332 pcs = pinctrl_dev_get_drvdata(pctldev);
333 func = radix_tree_lookup(&pcs->ftree, fselector);
334 if (!func) {
335 dev_err(pcs->dev, "%s could not find function%i\n",
336 __func__, fselector);
337 return -EINVAL;
338 }
339 *groups = func->pgnames;
340 *ngroups = func->npgnames;
341
342 return 0;
343}
344
345static int pcs_enable(struct pinctrl_dev *pctldev, unsigned fselector,
346 unsigned group)
347{
348 struct pcs_device *pcs;
349 struct pcs_function *func;
350 int i;
351
352 pcs = pinctrl_dev_get_drvdata(pctldev);
353 func = radix_tree_lookup(&pcs->ftree, fselector);
354 if (!func)
355 return -EINVAL;
356
357 dev_dbg(pcs->dev, "enabling %s function%i\n",
358 func->name, fselector);
359
360 for (i = 0; i < func->nvals; i++) {
361 struct pcs_func_vals *vals;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300362 unsigned val, mask;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700363
364 vals = &func->vals[i];
365 val = pcs->read(vals->reg);
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300366 if (!vals->mask)
367 mask = pcs->fmask;
368 else
369 mask = pcs->fmask & vals->mask;
370
371 val &= ~mask;
372 val |= (vals->val & mask);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700373 pcs->write(val, vals->reg);
374 }
375
376 return 0;
377}
378
379static void pcs_disable(struct pinctrl_dev *pctldev, unsigned fselector,
380 unsigned group)
381{
382 struct pcs_device *pcs;
383 struct pcs_function *func;
384 int i;
385
386 pcs = pinctrl_dev_get_drvdata(pctldev);
387 func = radix_tree_lookup(&pcs->ftree, fselector);
388 if (!func) {
389 dev_err(pcs->dev, "%s could not find function%i\n",
390 __func__, fselector);
391 return;
392 }
393
394 /*
395 * Ignore disable if function-off is not specified. Some hardware
396 * does not have clearly defined disable function. For pin specific
397 * off modes, you can use alternate named states as described in
398 * pinctrl-bindings.txt.
399 */
400 if (pcs->foff == PCS_OFF_DISABLED) {
401 dev_dbg(pcs->dev, "ignoring disable for %s function%i\n",
402 func->name, fselector);
403 return;
404 }
405
406 dev_dbg(pcs->dev, "disabling function%i %s\n",
407 fselector, func->name);
408
409 for (i = 0; i < func->nvals; i++) {
410 struct pcs_func_vals *vals;
411 unsigned val;
412
413 vals = &func->vals[i];
414 val = pcs->read(vals->reg);
415 val &= ~pcs->fmask;
416 val |= pcs->foff << pcs->fshift;
417 pcs->write(val, vals->reg);
418 }
419}
420
421static int pcs_request_gpio(struct pinctrl_dev *pctldev,
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800422 struct pinctrl_gpio_range *range, unsigned pin)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700423{
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800424 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
425 struct pcs_gpiofunc_range *frange = NULL;
426 struct list_head *pos, *tmp;
427 int mux_bytes = 0;
428 unsigned data;
429
430 list_for_each_safe(pos, tmp, &pcs->gpiofuncs) {
431 frange = list_entry(pos, struct pcs_gpiofunc_range, node);
432 if (pin >= frange->offset + frange->npins
433 || pin < frange->offset)
434 continue;
435 mux_bytes = pcs->width / BITS_PER_BYTE;
436 data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask;
437 data |= frange->gpiofunc;
438 pcs->write(data, pcs->base + pin * mux_bytes);
439 break;
440 }
441 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700442}
443
Laurent Pinchart022ab142013-02-16 10:25:07 +0100444static const struct pinmux_ops pcs_pinmux_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700445 .get_functions_count = pcs_get_functions_count,
446 .get_function_name = pcs_get_function_name,
447 .get_function_groups = pcs_get_function_groups,
448 .enable = pcs_enable,
449 .disable = pcs_disable,
450 .gpio_request_enable = pcs_request_gpio,
451};
452
453static int pcs_pinconf_get(struct pinctrl_dev *pctldev,
454 unsigned pin, unsigned long *config)
455{
456 return -ENOTSUPP;
457}
458
459static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
460 unsigned pin, unsigned long config)
461{
462 return -ENOTSUPP;
463}
464
465static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
466 unsigned group, unsigned long *config)
467{
468 return -ENOTSUPP;
469}
470
471static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
472 unsigned group, unsigned long config)
473{
474 return -ENOTSUPP;
475}
476
477static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
478 struct seq_file *s, unsigned offset)
479{
480}
481
482static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
483 struct seq_file *s, unsigned selector)
484{
485}
486
Laurent Pinchart022ab142013-02-16 10:25:07 +0100487static const struct pinconf_ops pcs_pinconf_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700488 .pin_config_get = pcs_pinconf_get,
489 .pin_config_set = pcs_pinconf_set,
490 .pin_config_group_get = pcs_pinconf_group_get,
491 .pin_config_group_set = pcs_pinconf_group_set,
492 .pin_config_dbg_show = pcs_pinconf_dbg_show,
493 .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
494};
495
496/**
497 * pcs_add_pin() - add a pin to the static per controller pin array
498 * @pcs: pcs driver instance
499 * @offset: register offset from base
500 */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800501static int pcs_add_pin(struct pcs_device *pcs, unsigned offset)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700502{
503 struct pinctrl_pin_desc *pin;
504 struct pcs_name *pn;
505 int i;
506
507 i = pcs->pins.cur;
508 if (i >= pcs->desc.npins) {
509 dev_err(pcs->dev, "too many pins, max %i\n",
510 pcs->desc.npins);
511 return -ENOMEM;
512 }
513
514 pin = &pcs->pins.pa[i];
515 pn = &pcs->names[i];
516 sprintf(pn->name, "%lx",
517 (unsigned long)pcs->res->start + offset);
518 pin->name = pn->name;
519 pin->number = i;
520 pcs->pins.cur++;
521
522 return i;
523}
524
525/**
526 * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
527 * @pcs: pcs driver instance
528 *
529 * In case of errors, resources are freed in pcs_free_resources.
530 *
531 * If your hardware needs holes in the address space, then just set
532 * up multiple driver instances.
533 */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800534static int pcs_allocate_pin_table(struct pcs_device *pcs)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700535{
536 int mux_bytes, nr_pins, i;
537
538 mux_bytes = pcs->width / BITS_PER_BYTE;
539 nr_pins = pcs->size / mux_bytes;
540
541 dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
542 pcs->pins.pa = devm_kzalloc(pcs->dev,
543 sizeof(*pcs->pins.pa) * nr_pins,
544 GFP_KERNEL);
545 if (!pcs->pins.pa)
546 return -ENOMEM;
547
548 pcs->names = devm_kzalloc(pcs->dev,
549 sizeof(struct pcs_name) * nr_pins,
550 GFP_KERNEL);
551 if (!pcs->names)
552 return -ENOMEM;
553
554 pcs->desc.pins = pcs->pins.pa;
555 pcs->desc.npins = nr_pins;
556
557 for (i = 0; i < pcs->desc.npins; i++) {
558 unsigned offset;
559 int res;
560
561 offset = i * mux_bytes;
562 res = pcs_add_pin(pcs, offset);
563 if (res < 0) {
564 dev_err(pcs->dev, "error adding pins: %i\n", res);
565 return res;
566 }
567 }
568
569 return 0;
570}
571
572/**
573 * pcs_add_function() - adds a new function to the function list
574 * @pcs: pcs driver instance
575 * @np: device node of the mux entry
576 * @name: name of the function
577 * @vals: array of mux register value pairs used by the function
578 * @nvals: number of mux register value pairs
579 * @pgnames: array of pingroup names for the function
580 * @npgnames: number of pingroup names
581 */
582static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
583 struct device_node *np,
584 const char *name,
585 struct pcs_func_vals *vals,
586 unsigned nvals,
587 const char **pgnames,
588 unsigned npgnames)
589{
590 struct pcs_function *function;
591
592 function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
593 if (!function)
594 return NULL;
595
596 function->name = name;
597 function->vals = vals;
598 function->nvals = nvals;
599 function->pgnames = pgnames;
600 function->npgnames = npgnames;
601
602 mutex_lock(&pcs->mutex);
603 list_add_tail(&function->node, &pcs->functions);
604 radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
605 pcs->nfuncs++;
606 mutex_unlock(&pcs->mutex);
607
608 return function;
609}
610
611static void pcs_remove_function(struct pcs_device *pcs,
612 struct pcs_function *function)
613{
614 int i;
615
616 mutex_lock(&pcs->mutex);
617 for (i = 0; i < pcs->nfuncs; i++) {
618 struct pcs_function *found;
619
620 found = radix_tree_lookup(&pcs->ftree, i);
621 if (found == function)
622 radix_tree_delete(&pcs->ftree, i);
623 }
624 list_del(&function->node);
625 mutex_unlock(&pcs->mutex);
626}
627
628/**
629 * pcs_add_pingroup() - add a pingroup to the pingroup list
630 * @pcs: pcs driver instance
631 * @np: device node of the mux entry
632 * @name: name of the pingroup
633 * @gpins: array of the pins that belong to the group
634 * @ngpins: number of pins in the group
635 */
636static int pcs_add_pingroup(struct pcs_device *pcs,
637 struct device_node *np,
638 const char *name,
639 int *gpins,
640 int ngpins)
641{
642 struct pcs_pingroup *pingroup;
643
644 pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
645 if (!pingroup)
646 return -ENOMEM;
647
648 pingroup->name = name;
649 pingroup->np = np;
650 pingroup->gpins = gpins;
651 pingroup->ngpins = ngpins;
652
653 mutex_lock(&pcs->mutex);
654 list_add_tail(&pingroup->node, &pcs->pingroups);
655 radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
656 pcs->ngroups++;
657 mutex_unlock(&pcs->mutex);
658
659 return 0;
660}
661
662/**
663 * pcs_get_pin_by_offset() - get a pin index based on the register offset
664 * @pcs: pcs driver instance
665 * @offset: register offset from the base
666 *
667 * Note that this is OK as long as the pins are in a static array.
668 */
669static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
670{
671 unsigned index;
672
673 if (offset >= pcs->size) {
674 dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
675 offset, pcs->size);
676 return -EINVAL;
677 }
678
679 index = offset / (pcs->width / BITS_PER_BYTE);
680
681 return index;
682}
683
684/**
685 * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
686 * @pcs: pinctrl driver instance
687 * @np: device node of the mux entry
688 * @map: map entry
689 * @pgnames: pingroup names
690 *
691 * Note that this binding currently supports only sets of one register + value.
692 *
693 * Also note that this driver tries to avoid understanding pin and function
694 * names because of the extra bloat they would cause especially in the case of
695 * a large number of pins. This driver just sets what is specified for the board
696 * in the .dts file. Further user space debugging tools can be developed to
697 * decipher the pin and function names using debugfs.
698 *
699 * If you are concerned about the boot time, set up the static pins in
700 * the bootloader, and only set up selected pins as device tree entries.
701 */
702static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
703 struct device_node *np,
704 struct pinctrl_map **map,
705 const char **pgnames)
706{
707 struct pcs_func_vals *vals;
708 const __be32 *mux;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300709 int size, params, rows, *pins, index = 0, found = 0, res = -ENOMEM;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700710 struct pcs_function *function;
711
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300712 if (pcs->bits_per_mux) {
713 params = 3;
714 mux = of_get_property(np, PCS_MUX_BITS_NAME, &size);
715 } else {
716 params = 2;
717 mux = of_get_property(np, PCS_MUX_PINS_NAME, &size);
718 }
719
720 if (!mux) {
721 dev_err(pcs->dev, "no valid property for %s\n", np->name);
722 return -EINVAL;
723 }
724
725 if (size < (sizeof(*mux) * params)) {
726 dev_err(pcs->dev, "bad data for %s\n", np->name);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700727 return -EINVAL;
728 }
729
730 size /= sizeof(*mux); /* Number of elements in array */
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300731 rows = size / params;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700732
733 vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
734 if (!vals)
735 return -ENOMEM;
736
737 pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
738 if (!pins)
739 goto free_vals;
740
741 while (index < size) {
742 unsigned offset, val;
743 int pin;
744
745 offset = be32_to_cpup(mux + index++);
746 val = be32_to_cpup(mux + index++);
747 vals[found].reg = pcs->base + offset;
748 vals[found].val = val;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300749 if (params == 3) {
750 val = be32_to_cpup(mux + index++);
751 vals[found].mask = val;
752 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700753
754 pin = pcs_get_pin_by_offset(pcs, offset);
755 if (pin < 0) {
756 dev_err(pcs->dev,
757 "could not add functions for %s %ux\n",
758 np->name, offset);
759 break;
760 }
761 pins[found++] = pin;
762 }
763
764 pgnames[0] = np->name;
765 function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
766 if (!function)
767 goto free_pins;
768
769 res = pcs_add_pingroup(pcs, np, np->name, pins, found);
770 if (res < 0)
771 goto free_function;
772
773 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
774 (*map)->data.mux.group = np->name;
775 (*map)->data.mux.function = np->name;
776
777 return 0;
778
779free_function:
780 pcs_remove_function(pcs, function);
781
782free_pins:
783 devm_kfree(pcs->dev, pins);
784
785free_vals:
786 devm_kfree(pcs->dev, vals);
787
788 return res;
789}
790/**
791 * pcs_dt_node_to_map() - allocates and parses pinctrl maps
792 * @pctldev: pinctrl instance
793 * @np_config: device tree pinmux entry
794 * @map: array of map entries
795 * @num_maps: number of maps
796 */
797static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
798 struct device_node *np_config,
799 struct pinctrl_map **map, unsigned *num_maps)
800{
801 struct pcs_device *pcs;
802 const char **pgnames;
803 int ret;
804
805 pcs = pinctrl_dev_get_drvdata(pctldev);
806
807 *map = devm_kzalloc(pcs->dev, sizeof(**map), GFP_KERNEL);
Sachin Kamat00e79d12012-11-20 16:34:39 +0530808 if (!*map)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700809 return -ENOMEM;
810
811 *num_maps = 0;
812
813 pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
814 if (!pgnames) {
815 ret = -ENOMEM;
816 goto free_map;
817 }
818
819 ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map, pgnames);
820 if (ret < 0) {
821 dev_err(pcs->dev, "no pins entries for %s\n",
822 np_config->name);
823 goto free_pgnames;
824 }
825 *num_maps = 1;
826
827 return 0;
828
829free_pgnames:
830 devm_kfree(pcs->dev, pgnames);
831free_map:
832 devm_kfree(pcs->dev, *map);
833
834 return ret;
835}
836
837/**
838 * pcs_free_funcs() - free memory used by functions
839 * @pcs: pcs driver instance
840 */
841static void pcs_free_funcs(struct pcs_device *pcs)
842{
843 struct list_head *pos, *tmp;
844 int i;
845
846 mutex_lock(&pcs->mutex);
847 for (i = 0; i < pcs->nfuncs; i++) {
848 struct pcs_function *func;
849
850 func = radix_tree_lookup(&pcs->ftree, i);
851 if (!func)
852 continue;
853 radix_tree_delete(&pcs->ftree, i);
854 }
855 list_for_each_safe(pos, tmp, &pcs->functions) {
856 struct pcs_function *function;
857
858 function = list_entry(pos, struct pcs_function, node);
859 list_del(&function->node);
860 }
861 mutex_unlock(&pcs->mutex);
862}
863
864/**
865 * pcs_free_pingroups() - free memory used by pingroups
866 * @pcs: pcs driver instance
867 */
868static void pcs_free_pingroups(struct pcs_device *pcs)
869{
870 struct list_head *pos, *tmp;
871 int i;
872
873 mutex_lock(&pcs->mutex);
874 for (i = 0; i < pcs->ngroups; i++) {
875 struct pcs_pingroup *pingroup;
876
877 pingroup = radix_tree_lookup(&pcs->pgtree, i);
878 if (!pingroup)
879 continue;
880 radix_tree_delete(&pcs->pgtree, i);
881 }
882 list_for_each_safe(pos, tmp, &pcs->pingroups) {
883 struct pcs_pingroup *pingroup;
884
885 pingroup = list_entry(pos, struct pcs_pingroup, node);
886 list_del(&pingroup->node);
887 }
888 mutex_unlock(&pcs->mutex);
889}
890
891/**
892 * pcs_free_resources() - free memory used by this driver
893 * @pcs: pcs driver instance
894 */
895static void pcs_free_resources(struct pcs_device *pcs)
896{
897 if (pcs->pctl)
898 pinctrl_unregister(pcs->pctl);
899
900 pcs_free_funcs(pcs);
901 pcs_free_pingroups(pcs);
902}
903
904#define PCS_GET_PROP_U32(name, reg, err) \
905 do { \
906 ret = of_property_read_u32(np, name, reg); \
907 if (ret) { \
908 dev_err(pcs->dev, err); \
909 return ret; \
910 } \
911 } while (0);
912
913static struct of_device_id pcs_of_match[];
914
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800915static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
916{
917 const char *propname = "pinctrl-single,gpio-range";
918 const char *cellname = "#pinctrl-single,gpio-range-cells";
919 struct of_phandle_args gpiospec;
920 struct pcs_gpiofunc_range *range;
921 int ret, i;
922
923 for (i = 0; ; i++) {
924 ret = of_parse_phandle_with_args(node, propname, cellname,
925 i, &gpiospec);
926 /* Do not treat it as error. Only treat it as end condition. */
927 if (ret) {
928 ret = 0;
929 break;
930 }
931 range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
932 if (!range) {
933 ret = -ENOMEM;
934 break;
935 }
936 range->offset = gpiospec.args[0];
937 range->npins = gpiospec.args[1];
938 range->gpiofunc = gpiospec.args[2];
939 mutex_lock(&pcs->mutex);
940 list_add_tail(&range->node, &pcs->gpiofuncs);
941 mutex_unlock(&pcs->mutex);
942 }
943 return ret;
944}
945
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800946static int pcs_probe(struct platform_device *pdev)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700947{
948 struct device_node *np = pdev->dev.of_node;
949 const struct of_device_id *match;
950 struct resource *res;
951 struct pcs_device *pcs;
952 int ret;
953
954 match = of_match_device(pcs_of_match, &pdev->dev);
955 if (!match)
956 return -EINVAL;
957
958 pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
959 if (!pcs) {
960 dev_err(&pdev->dev, "could not allocate\n");
961 return -ENOMEM;
962 }
963 pcs->dev = &pdev->dev;
964 mutex_init(&pcs->mutex);
965 INIT_LIST_HEAD(&pcs->pingroups);
966 INIT_LIST_HEAD(&pcs->functions);
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800967 INIT_LIST_HEAD(&pcs->gpiofuncs);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700968
969 PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width,
970 "register width not specified\n");
971
972 PCS_GET_PROP_U32("pinctrl-single,function-mask", &pcs->fmask,
973 "function register mask not specified\n");
974 pcs->fshift = ffs(pcs->fmask) - 1;
975 pcs->fmax = pcs->fmask >> pcs->fshift;
976
977 ret = of_property_read_u32(np, "pinctrl-single,function-off",
978 &pcs->foff);
979 if (ret)
980 pcs->foff = PCS_OFF_DISABLED;
981
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300982 pcs->bits_per_mux = of_property_read_bool(np,
983 "pinctrl-single,bit-per-mux");
984
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700985 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
986 if (!res) {
987 dev_err(pcs->dev, "could not get resource\n");
988 return -ENODEV;
989 }
990
991 pcs->res = devm_request_mem_region(pcs->dev, res->start,
992 resource_size(res), DRIVER_NAME);
993 if (!pcs->res) {
994 dev_err(pcs->dev, "could not get mem_region\n");
995 return -EBUSY;
996 }
997
998 pcs->size = resource_size(pcs->res);
999 pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
1000 if (!pcs->base) {
1001 dev_err(pcs->dev, "could not ioremap\n");
1002 return -ENODEV;
1003 }
1004
1005 INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
1006 INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
1007 platform_set_drvdata(pdev, pcs);
1008
1009 switch (pcs->width) {
1010 case 8:
1011 pcs->read = pcs_readb;
1012 pcs->write = pcs_writeb;
1013 break;
1014 case 16:
1015 pcs->read = pcs_readw;
1016 pcs->write = pcs_writew;
1017 break;
1018 case 32:
1019 pcs->read = pcs_readl;
1020 pcs->write = pcs_writel;
1021 break;
1022 default:
1023 break;
1024 }
1025
1026 pcs->desc.name = DRIVER_NAME;
1027 pcs->desc.pctlops = &pcs_pinctrl_ops;
1028 pcs->desc.pmxops = &pcs_pinmux_ops;
1029 pcs->desc.confops = &pcs_pinconf_ops;
1030 pcs->desc.owner = THIS_MODULE;
1031
1032 ret = pcs_allocate_pin_table(pcs);
1033 if (ret < 0)
1034 goto free;
1035
1036 pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
1037 if (!pcs->pctl) {
1038 dev_err(pcs->dev, "could not register single pinctrl driver\n");
1039 ret = -EINVAL;
1040 goto free;
1041 }
1042
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001043 ret = pcs_add_gpio_func(np, pcs);
1044 if (ret < 0)
1045 goto free;
1046
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001047 dev_info(pcs->dev, "%i pins at pa %p size %u\n",
1048 pcs->desc.npins, pcs->base, pcs->size);
1049
1050 return 0;
1051
1052free:
1053 pcs_free_resources(pcs);
1054
1055 return ret;
1056}
1057
Bill Pembertonf90f54b2012-11-19 13:26:06 -05001058static int pcs_remove(struct platform_device *pdev)
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001059{
1060 struct pcs_device *pcs = platform_get_drvdata(pdev);
1061
1062 if (!pcs)
1063 return 0;
1064
1065 pcs_free_resources(pcs);
1066
1067 return 0;
1068}
1069
Bill Pemberton99688ed2012-11-19 13:24:27 -05001070static struct of_device_id pcs_of_match[] = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001071 { .compatible = DRIVER_NAME, },
1072 { },
1073};
1074MODULE_DEVICE_TABLE(of, pcs_of_match);
1075
1076static struct platform_driver pcs_driver = {
1077 .probe = pcs_probe,
Bill Pemberton2a36f082012-11-19 13:21:27 -05001078 .remove = pcs_remove,
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001079 .driver = {
1080 .owner = THIS_MODULE,
1081 .name = DRIVER_NAME,
1082 .of_match_table = pcs_of_match,
1083 },
1084};
1085
1086module_platform_driver(pcs_driver);
1087
1088MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
1089MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
1090MODULE_LICENSE("GPL v2");