blob: f36a9f1f3949e0c502a56f7577002ccfa57cd59c [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>
Tony Lindgren3e6cee12013-10-02 21:39:40 -070018#include <linux/interrupt.h>
19
20#include <linux/irqchip/chained_irq.h>
Tony Lindgren8b8b0912012-07-10 02:05:46 -070021
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/of_address.h>
Tony Lindgren3e6cee12013-10-02 21:39:40 -070025#include <linux/of_irq.h>
Tony Lindgren8b8b0912012-07-10 02:05:46 -070026
27#include <linux/pinctrl/pinctrl.h>
28#include <linux/pinctrl/pinmux.h>
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +080029#include <linux/pinctrl/pinconf-generic.h>
Tony Lindgren8b8b0912012-07-10 02:05:46 -070030
Tony Lindgrendc7743a2013-10-02 21:39:40 -070031#include <linux/platform_data/pinctrl-single.h>
32
Tony Lindgren8b8b0912012-07-10 02:05:46 -070033#include "core.h"
Tony Lindgren46222152016-11-03 09:35:48 -070034#include "devicetree.h"
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +080035#include "pinconf.h"
Tony Lindgren8b8b0912012-07-10 02:05:46 -070036
37#define DRIVER_NAME "pinctrl-single"
Tony Lindgren8b8b0912012-07-10 02:05:46 -070038#define PCS_OFF_DISABLED ~0U
39
40/**
41 * struct pcs_pingroup - pingroups for a function
42 * @np: pingroup device node pointer
43 * @name: pingroup name
44 * @gpins: array of the pins in the group
45 * @ngpins: number of pins in the group
46 * @node: list node
47 */
48struct pcs_pingroup {
49 struct device_node *np;
50 const char *name;
51 int *gpins;
52 int ngpins;
53 struct list_head node;
54};
55
56/**
57 * struct pcs_func_vals - mux function register offset and value pair
58 * @reg: register virtual address
59 * @val: register value
60 */
61struct pcs_func_vals {
62 void __iomem *reg;
63 unsigned val;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +030064 unsigned mask;
Tony Lindgren8b8b0912012-07-10 02:05:46 -070065};
66
67/**
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +080068 * struct pcs_conf_vals - pinconf parameter, pinconf register offset
69 * and value, enable, disable, mask
70 * @param: config parameter
71 * @val: user input bits in the pinconf register
72 * @enable: enable bits in the pinconf register
73 * @disable: disable bits in the pinconf register
74 * @mask: mask bits in the register value
75 */
76struct pcs_conf_vals {
77 enum pin_config_param param;
78 unsigned val;
79 unsigned enable;
80 unsigned disable;
81 unsigned mask;
82};
83
84/**
85 * struct pcs_conf_type - pinconf property name, pinconf param pair
86 * @name: property name in DTS file
87 * @param: config parameter
88 */
89struct pcs_conf_type {
90 const char *name;
91 enum pin_config_param param;
92};
93
94/**
Tony Lindgren8b8b0912012-07-10 02:05:46 -070095 * struct pcs_function - pinctrl function
96 * @name: pinctrl function name
97 * @vals: register and vals array
98 * @nvals: number of entries in vals array
99 * @pgnames: array of pingroup names the function uses
100 * @npgnames: number of pingroup names the function uses
101 * @node: list node
102 */
103struct pcs_function {
104 const char *name;
105 struct pcs_func_vals *vals;
106 unsigned nvals;
107 const char **pgnames;
108 int npgnames;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800109 struct pcs_conf_vals *conf;
110 int nconfs;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700111 struct list_head node;
112};
113
114/**
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800115 * struct pcs_gpiofunc_range - pin ranges with same mux value of gpio function
116 * @offset: offset base of pins
117 * @npins: number pins with the same mux value of gpio function
118 * @gpiofunc: mux value of gpio function
119 * @node: list node
120 */
121struct pcs_gpiofunc_range {
122 unsigned offset;
123 unsigned npins;
124 unsigned gpiofunc;
125 struct list_head node;
126};
127
128/**
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700129 * struct pcs_data - wrapper for data needed by pinctrl framework
130 * @pa: pindesc array
131 * @cur: index to current element
132 *
133 * REVISIT: We should be able to drop this eventually by adding
134 * support for registering pins individually in the pinctrl
135 * framework for those drivers that don't need a static array.
136 */
137struct pcs_data {
138 struct pinctrl_pin_desc *pa;
139 int cur;
140};
141
142/**
Tony Lindgren02e483f2013-10-02 21:39:39 -0700143 * struct pcs_soc_data - SoC specific settings
144 * @flags: initial SoC specific PCS_FEAT_xxx values
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700145 * @irq: optional interrupt for the controller
146 * @irq_enable_mask: optional SoC specific interrupt enable mask
147 * @irq_status_mask: optional SoC specific interrupt status mask
Tony Lindgrendc7743a2013-10-02 21:39:40 -0700148 * @rearm: optional SoC specific wake-up rearm function
Tony Lindgren02e483f2013-10-02 21:39:39 -0700149 */
150struct pcs_soc_data {
151 unsigned flags;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700152 int irq;
153 unsigned irq_enable_mask;
154 unsigned irq_status_mask;
Tony Lindgrendc7743a2013-10-02 21:39:40 -0700155 void (*rearm)(void);
Tony Lindgren02e483f2013-10-02 21:39:39 -0700156};
157
158/**
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700159 * struct pcs_device - pinctrl device instance
160 * @res: resources
161 * @base: virtual address of the controller
162 * @size: size of the ioremapped area
163 * @dev: device entry
Tony Lindgren46222152016-11-03 09:35:48 -0700164 * @np: device tree node
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700165 * @pctl: pin controller device
Tony Lindgren02e483f2013-10-02 21:39:39 -0700166 * @flags: mask of PCS_FEAT_xxx values
Tony Lindgren46222152016-11-03 09:35:48 -0700167 * @missing_nr_pinctrl_cells: for legacy binding, may go away
168 * @socdata: soc specific data
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700169 * @lock: spinlock for register access
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700170 * @mutex: mutex protecting the lists
171 * @width: bits per mux register
172 * @fmask: function register mask
173 * @fshift: function register shift
174 * @foff: value to turn mux off
175 * @fmax: max number of functions in fmask
Tony Lindgren46222152016-11-03 09:35:48 -0700176 * @bits_per_mux: number of bits per mux
177 * @bits_per_pin: number of bits per pin
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700178 * @pins: physical pins on the SoC
179 * @pgtree: pingroup index radix tree
180 * @ftree: function index radix tree
181 * @pingroups: list of pingroups
182 * @functions: list of functions
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800183 * @gpiofuncs: list of gpio functions
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700184 * @irqs: list of interrupt registers
185 * @chip: chip container for this instance
186 * @domain: IRQ domain for this instance
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700187 * @ngroups: number of pingroups
188 * @nfuncs: number of functions
189 * @desc: pin controller descriptor
190 * @read: register read function to use
191 * @write: register write function to use
192 */
193struct pcs_device {
194 struct resource *res;
195 void __iomem *base;
196 unsigned size;
197 struct device *dev;
Tony Lindgren46222152016-11-03 09:35:48 -0700198 struct device_node *np;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700199 struct pinctrl_dev *pctl;
Tony Lindgren02e483f2013-10-02 21:39:39 -0700200 unsigned flags;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700201#define PCS_QUIRK_SHARED_IRQ (1 << 2)
202#define PCS_FEAT_IRQ (1 << 1)
Tony Lindgren02e483f2013-10-02 21:39:39 -0700203#define PCS_FEAT_PINCONF (1 << 0)
Tony Lindgren46222152016-11-03 09:35:48 -0700204 struct property *missing_nr_pinctrl_cells;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700205 struct pcs_soc_data socdata;
206 raw_spinlock_t lock;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700207 struct mutex mutex;
208 unsigned width;
209 unsigned fmask;
210 unsigned fshift;
211 unsigned foff;
212 unsigned fmax;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300213 bool bits_per_mux;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530214 unsigned bits_per_pin;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700215 struct pcs_data pins;
216 struct radix_tree_root pgtree;
217 struct radix_tree_root ftree;
218 struct list_head pingroups;
219 struct list_head functions;
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800220 struct list_head gpiofuncs;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700221 struct list_head irqs;
222 struct irq_chip chip;
223 struct irq_domain *domain;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700224 unsigned ngroups;
225 unsigned nfuncs;
226 struct pinctrl_desc desc;
227 unsigned (*read)(void __iomem *reg);
228 void (*write)(unsigned val, void __iomem *reg);
229};
230
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700231#define PCS_QUIRK_HAS_SHARED_IRQ (pcs->flags & PCS_QUIRK_SHARED_IRQ)
232#define PCS_HAS_IRQ (pcs->flags & PCS_FEAT_IRQ)
Tony Lindgren02e483f2013-10-02 21:39:39 -0700233#define PCS_HAS_PINCONF (pcs->flags & PCS_FEAT_PINCONF)
234
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800235static int pcs_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
236 unsigned long *config);
237static int pcs_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
Sherman Yin03b054e2013-08-27 11:32:12 -0700238 unsigned long *configs, unsigned num_configs);
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800239
240static enum pin_config_param pcs_bias[] = {
241 PIN_CONFIG_BIAS_PULL_DOWN,
242 PIN_CONFIG_BIAS_PULL_UP,
243};
244
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700245/*
Sudeep Holla3c177a12016-02-01 18:28:17 +0000246 * This lock class tells lockdep that irqchip core that this single
247 * pinctrl can be in a different category than its parents, so it won't
248 * report false recursion.
249 */
250static struct lock_class_key pcs_lock_class;
251
252/*
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700253 * REVISIT: Reads and writes could eventually use regmap or something
254 * generic. But at least on omaps, some mux registers are performance
255 * critical as they may need to be remuxed every time before and after
256 * idle. Adding tests for register access width for every read and
257 * write like regmap is doing is not desired, and caching the registers
258 * does not help in this case.
259 */
260
261static unsigned __maybe_unused pcs_readb(void __iomem *reg)
262{
263 return readb(reg);
264}
265
266static unsigned __maybe_unused pcs_readw(void __iomem *reg)
267{
268 return readw(reg);
269}
270
271static unsigned __maybe_unused pcs_readl(void __iomem *reg)
272{
273 return readl(reg);
274}
275
276static void __maybe_unused pcs_writeb(unsigned val, void __iomem *reg)
277{
278 writeb(val, reg);
279}
280
281static void __maybe_unused pcs_writew(unsigned val, void __iomem *reg)
282{
283 writew(val, reg);
284}
285
286static void __maybe_unused pcs_writel(unsigned val, void __iomem *reg)
287{
288 writel(val, reg);
289}
290
291static int pcs_get_groups_count(struct pinctrl_dev *pctldev)
292{
293 struct pcs_device *pcs;
294
295 pcs = pinctrl_dev_get_drvdata(pctldev);
296
297 return pcs->ngroups;
298}
299
300static const char *pcs_get_group_name(struct pinctrl_dev *pctldev,
301 unsigned gselector)
302{
303 struct pcs_device *pcs;
304 struct pcs_pingroup *group;
305
306 pcs = pinctrl_dev_get_drvdata(pctldev);
307 group = radix_tree_lookup(&pcs->pgtree, gselector);
308 if (!group) {
309 dev_err(pcs->dev, "%s could not find pingroup%i\n",
310 __func__, gselector);
311 return NULL;
312 }
313
314 return group->name;
315}
316
317static int pcs_get_group_pins(struct pinctrl_dev *pctldev,
318 unsigned gselector,
319 const unsigned **pins,
320 unsigned *npins)
321{
322 struct pcs_device *pcs;
323 struct pcs_pingroup *group;
324
325 pcs = pinctrl_dev_get_drvdata(pctldev);
326 group = radix_tree_lookup(&pcs->pgtree, gselector);
327 if (!group) {
328 dev_err(pcs->dev, "%s could not find pingroup%i\n",
329 __func__, gselector);
330 return -EINVAL;
331 }
332
333 *pins = group->gpins;
334 *npins = group->ngpins;
335
336 return 0;
337}
338
339static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev,
340 struct seq_file *s,
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800341 unsigned pin)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700342{
Matt Porter7d66ce72012-09-26 15:07:43 -0400343 struct pcs_device *pcs;
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800344 unsigned val, mux_bytes;
Tony Lindgren223decc2016-10-27 07:59:52 -0700345 unsigned long offset;
346 size_t pa;
Matt Porter7d66ce72012-09-26 15:07:43 -0400347
348 pcs = pinctrl_dev_get_drvdata(pctldev);
349
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800350 mux_bytes = pcs->width / BITS_PER_BYTE;
Tony Lindgren223decc2016-10-27 07:59:52 -0700351 offset = pin * mux_bytes;
352 val = pcs->read(pcs->base + offset);
353 pa = pcs->res->start + offset;
Matt Porter7d66ce72012-09-26 15:07:43 -0400354
Tony Lindgren223decc2016-10-27 07:59:52 -0700355 seq_printf(s, "%zx %08x %s ", pa, val, DRIVER_NAME);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700356}
357
358static void pcs_dt_free_map(struct pinctrl_dev *pctldev,
359 struct pinctrl_map *map, unsigned num_maps)
360{
361 struct pcs_device *pcs;
362
363 pcs = pinctrl_dev_get_drvdata(pctldev);
364 devm_kfree(pcs->dev, map);
365}
366
367static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
368 struct device_node *np_config,
369 struct pinctrl_map **map, unsigned *num_maps);
370
Laurent Pinchart022ab142013-02-16 10:25:07 +0100371static const struct pinctrl_ops pcs_pinctrl_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700372 .get_groups_count = pcs_get_groups_count,
373 .get_group_name = pcs_get_group_name,
374 .get_group_pins = pcs_get_group_pins,
375 .pin_dbg_show = pcs_pin_dbg_show,
376 .dt_node_to_map = pcs_dt_node_to_map,
377 .dt_free_map = pcs_dt_free_map,
378};
379
380static int pcs_get_functions_count(struct pinctrl_dev *pctldev)
381{
382 struct pcs_device *pcs;
383
384 pcs = pinctrl_dev_get_drvdata(pctldev);
385
386 return pcs->nfuncs;
387}
388
389static const char *pcs_get_function_name(struct pinctrl_dev *pctldev,
390 unsigned fselector)
391{
392 struct pcs_device *pcs;
393 struct pcs_function *func;
394
395 pcs = pinctrl_dev_get_drvdata(pctldev);
396 func = radix_tree_lookup(&pcs->ftree, fselector);
397 if (!func) {
398 dev_err(pcs->dev, "%s could not find function%i\n",
399 __func__, fselector);
400 return NULL;
401 }
402
403 return func->name;
404}
405
406static int pcs_get_function_groups(struct pinctrl_dev *pctldev,
407 unsigned fselector,
408 const char * const **groups,
409 unsigned * const ngroups)
410{
411 struct pcs_device *pcs;
412 struct pcs_function *func;
413
414 pcs = pinctrl_dev_get_drvdata(pctldev);
415 func = radix_tree_lookup(&pcs->ftree, fselector);
416 if (!func) {
417 dev_err(pcs->dev, "%s could not find function%i\n",
418 __func__, fselector);
419 return -EINVAL;
420 }
421 *groups = func->pgnames;
422 *ngroups = func->npgnames;
423
424 return 0;
425}
426
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800427static int pcs_get_function(struct pinctrl_dev *pctldev, unsigned pin,
428 struct pcs_function **func)
429{
430 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
431 struct pin_desc *pdesc = pin_desc_get(pctldev, pin);
432 const struct pinctrl_setting_mux *setting;
433 unsigned fselector;
434
435 /* If pin is not described in DTS & enabled, mux_setting is NULL. */
436 setting = pdesc->mux_setting;
437 if (!setting)
438 return -ENOTSUPP;
439 fselector = setting->func;
440 *func = radix_tree_lookup(&pcs->ftree, fselector);
441 if (!(*func)) {
442 dev_err(pcs->dev, "%s could not find function%i\n",
443 __func__, fselector);
444 return -ENOTSUPP;
445 }
446 return 0;
447}
448
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200449static int pcs_set_mux(struct pinctrl_dev *pctldev, unsigned fselector,
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700450 unsigned group)
451{
452 struct pcs_device *pcs;
453 struct pcs_function *func;
454 int i;
455
456 pcs = pinctrl_dev_get_drvdata(pctldev);
Haojian Zhuang477ac772013-02-17 19:42:54 +0800457 /* If function mask is null, needn't enable it. */
458 if (!pcs->fmask)
459 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700460 func = radix_tree_lookup(&pcs->ftree, fselector);
461 if (!func)
462 return -EINVAL;
463
464 dev_dbg(pcs->dev, "enabling %s function%i\n",
465 func->name, fselector);
466
467 for (i = 0; i < func->nvals; i++) {
468 struct pcs_func_vals *vals;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700469 unsigned long flags;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300470 unsigned val, mask;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700471
472 vals = &func->vals[i];
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700473 raw_spin_lock_irqsave(&pcs->lock, flags);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700474 val = pcs->read(vals->reg);
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530475
476 if (pcs->bits_per_mux)
477 mask = vals->mask;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300478 else
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530479 mask = pcs->fmask;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300480
481 val &= ~mask;
482 val |= (vals->val & mask);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700483 pcs->write(val, vals->reg);
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700484 raw_spin_unlock_irqrestore(&pcs->lock, flags);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700485 }
486
487 return 0;
488}
489
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700490static int pcs_request_gpio(struct pinctrl_dev *pctldev,
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800491 struct pinctrl_gpio_range *range, unsigned pin)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700492{
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800493 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
494 struct pcs_gpiofunc_range *frange = NULL;
495 struct list_head *pos, *tmp;
496 int mux_bytes = 0;
497 unsigned data;
498
Haojian Zhuang477ac772013-02-17 19:42:54 +0800499 /* If function mask is null, return directly. */
500 if (!pcs->fmask)
501 return -ENOTSUPP;
502
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800503 list_for_each_safe(pos, tmp, &pcs->gpiofuncs) {
504 frange = list_entry(pos, struct pcs_gpiofunc_range, node);
505 if (pin >= frange->offset + frange->npins
506 || pin < frange->offset)
507 continue;
508 mux_bytes = pcs->width / BITS_PER_BYTE;
509 data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask;
510 data |= frange->gpiofunc;
511 pcs->write(data, pcs->base + pin * mux_bytes);
512 break;
513 }
514 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700515}
516
Laurent Pinchart022ab142013-02-16 10:25:07 +0100517static const struct pinmux_ops pcs_pinmux_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700518 .get_functions_count = pcs_get_functions_count,
519 .get_function_name = pcs_get_function_name,
520 .get_function_groups = pcs_get_function_groups,
Linus Walleij9e3a9792014-09-05 09:53:23 +0200521 .set_mux = pcs_set_mux,
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700522 .gpio_request_enable = pcs_request_gpio,
523};
524
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800525/* Clear BIAS value */
526static void pcs_pinconf_clear_bias(struct pinctrl_dev *pctldev, unsigned pin)
527{
528 unsigned long config;
529 int i;
530 for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) {
531 config = pinconf_to_config_packed(pcs_bias[i], 0);
Sherman Yin03b054e2013-08-27 11:32:12 -0700532 pcs_pinconf_set(pctldev, pin, &config, 1);
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800533 }
534}
535
536/*
537 * Check whether PIN_CONFIG_BIAS_DISABLE is valid.
538 * It's depend on that PULL_DOWN & PULL_UP configs are all invalid.
539 */
540static bool pcs_pinconf_bias_disable(struct pinctrl_dev *pctldev, unsigned pin)
541{
542 unsigned long config;
543 int i;
544
545 for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) {
546 config = pinconf_to_config_packed(pcs_bias[i], 0);
547 if (!pcs_pinconf_get(pctldev, pin, &config))
548 goto out;
549 }
550 return true;
551out:
552 return false;
553}
554
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700555static int pcs_pinconf_get(struct pinctrl_dev *pctldev,
556 unsigned pin, unsigned long *config)
557{
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800558 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
559 struct pcs_function *func;
560 enum pin_config_param param;
561 unsigned offset = 0, data = 0, i, j, ret;
562
563 ret = pcs_get_function(pctldev, pin, &func);
564 if (ret)
565 return ret;
566
567 for (i = 0; i < func->nconfs; i++) {
568 param = pinconf_to_config_param(*config);
569 if (param == PIN_CONFIG_BIAS_DISABLE) {
570 if (pcs_pinconf_bias_disable(pctldev, pin)) {
571 *config = 0;
572 return 0;
573 } else {
574 return -ENOTSUPP;
575 }
576 } else if (param != func->conf[i].param) {
577 continue;
578 }
579
580 offset = pin * (pcs->width / BITS_PER_BYTE);
581 data = pcs->read(pcs->base + offset) & func->conf[i].mask;
582 switch (func->conf[i].param) {
583 /* 4 parameters */
584 case PIN_CONFIG_BIAS_PULL_DOWN:
585 case PIN_CONFIG_BIAS_PULL_UP:
586 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
587 if ((data != func->conf[i].enable) ||
588 (data == func->conf[i].disable))
589 return -ENOTSUPP;
590 *config = 0;
591 break;
592 /* 2 parameters */
593 case PIN_CONFIG_INPUT_SCHMITT:
594 for (j = 0; j < func->nconfs; j++) {
595 switch (func->conf[j].param) {
596 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
597 if (data != func->conf[j].enable)
598 return -ENOTSUPP;
599 break;
600 default:
601 break;
602 }
603 }
604 *config = data;
605 break;
606 case PIN_CONFIG_DRIVE_STRENGTH:
607 case PIN_CONFIG_SLEW_RATE:
Chao Xie4bd75472014-01-28 15:20:44 +0800608 case PIN_CONFIG_LOW_POWER_MODE:
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800609 default:
610 *config = data;
611 break;
612 }
613 return 0;
614 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700615 return -ENOTSUPP;
616}
617
618static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700619 unsigned pin, unsigned long *configs,
620 unsigned num_configs)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700621{
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800622 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
623 struct pcs_function *func;
Haojian Zhuang7cba5b32013-03-13 16:01:26 +0800624 unsigned offset = 0, shift = 0, i, data, ret;
625 u16 arg;
Sherman Yin03b054e2013-08-27 11:32:12 -0700626 int j;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800627
628 ret = pcs_get_function(pctldev, pin, &func);
629 if (ret)
630 return ret;
631
Sherman Yin03b054e2013-08-27 11:32:12 -0700632 for (j = 0; j < num_configs; j++) {
633 for (i = 0; i < func->nconfs; i++) {
634 if (pinconf_to_config_param(configs[j])
635 != func->conf[i].param)
636 continue;
637
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800638 offset = pin * (pcs->width / BITS_PER_BYTE);
639 data = pcs->read(pcs->base + offset);
Sherman Yin03b054e2013-08-27 11:32:12 -0700640 arg = pinconf_to_config_argument(configs[j]);
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800641 switch (func->conf[i].param) {
642 /* 2 parameters */
643 case PIN_CONFIG_INPUT_SCHMITT:
644 case PIN_CONFIG_DRIVE_STRENGTH:
645 case PIN_CONFIG_SLEW_RATE:
Chao Xie4bd75472014-01-28 15:20:44 +0800646 case PIN_CONFIG_LOW_POWER_MODE:
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800647 shift = ffs(func->conf[i].mask) - 1;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800648 data &= ~func->conf[i].mask;
649 data |= (arg << shift) & func->conf[i].mask;
650 break;
651 /* 4 parameters */
652 case PIN_CONFIG_BIAS_DISABLE:
653 pcs_pinconf_clear_bias(pctldev, pin);
654 break;
655 case PIN_CONFIG_BIAS_PULL_DOWN:
656 case PIN_CONFIG_BIAS_PULL_UP:
Haojian Zhuang7cba5b32013-03-13 16:01:26 +0800657 if (arg)
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800658 pcs_pinconf_clear_bias(pctldev, pin);
659 /* fall through */
660 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
661 data &= ~func->conf[i].mask;
Haojian Zhuang7cba5b32013-03-13 16:01:26 +0800662 if (arg)
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800663 data |= func->conf[i].enable;
664 else
665 data |= func->conf[i].disable;
666 break;
667 default:
668 return -ENOTSUPP;
669 }
670 pcs->write(data, pcs->base + offset);
Sherman Yin03b054e2013-08-27 11:32:12 -0700671
672 break;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800673 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700674 if (i >= func->nconfs)
675 return -ENOTSUPP;
676 } /* for each config */
677
678 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700679}
680
681static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
682 unsigned group, unsigned long *config)
683{
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800684 const unsigned *pins;
685 unsigned npins, old = 0;
686 int i, ret;
687
688 ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
689 if (ret)
690 return ret;
691 for (i = 0; i < npins; i++) {
692 if (pcs_pinconf_get(pctldev, pins[i], config))
693 return -ENOTSUPP;
694 /* configs do not match between two pins */
695 if (i && (old != *config))
696 return -ENOTSUPP;
697 old = *config;
698 }
699 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700700}
701
702static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700703 unsigned group, unsigned long *configs,
704 unsigned num_configs)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700705{
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800706 const unsigned *pins;
707 unsigned npins;
708 int i, ret;
709
710 ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
711 if (ret)
712 return ret;
713 for (i = 0; i < npins; i++) {
Sherman Yin03b054e2013-08-27 11:32:12 -0700714 if (pcs_pinconf_set(pctldev, pins[i], configs, num_configs))
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800715 return -ENOTSUPP;
716 }
717 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700718}
719
720static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800721 struct seq_file *s, unsigned pin)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700722{
723}
724
725static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
726 struct seq_file *s, unsigned selector)
727{
728}
729
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800730static void pcs_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
731 struct seq_file *s,
732 unsigned long config)
733{
734 pinconf_generic_dump_config(pctldev, s, config);
735}
736
Laurent Pinchart022ab142013-02-16 10:25:07 +0100737static const struct pinconf_ops pcs_pinconf_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700738 .pin_config_get = pcs_pinconf_get,
739 .pin_config_set = pcs_pinconf_set,
740 .pin_config_group_get = pcs_pinconf_group_get,
741 .pin_config_group_set = pcs_pinconf_group_set,
742 .pin_config_dbg_show = pcs_pinconf_dbg_show,
743 .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800744 .pin_config_config_dbg_show = pcs_pinconf_config_dbg_show,
Axel Lina7bbdd72013-03-04 13:47:39 +0800745 .is_generic = true,
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700746};
747
748/**
749 * pcs_add_pin() - add a pin to the static per controller pin array
750 * @pcs: pcs driver instance
751 * @offset: register offset from base
752 */
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530753static int pcs_add_pin(struct pcs_device *pcs, unsigned offset,
754 unsigned pin_pos)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700755{
Tony Lindgren58968622014-04-10 16:47:19 -0700756 struct pcs_soc_data *pcs_soc = &pcs->socdata;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700757 struct pinctrl_pin_desc *pin;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700758 int i;
759
760 i = pcs->pins.cur;
761 if (i >= pcs->desc.npins) {
762 dev_err(pcs->dev, "too many pins, max %i\n",
763 pcs->desc.npins);
764 return -ENOMEM;
765 }
766
Tony Lindgren58968622014-04-10 16:47:19 -0700767 if (pcs_soc->irq_enable_mask) {
768 unsigned val;
769
770 val = pcs->read(pcs->base + offset);
771 if (val & pcs_soc->irq_enable_mask) {
772 dev_dbg(pcs->dev, "irq enabled at boot for pin at %lx (%x), clearing\n",
773 (unsigned long)pcs->res->start + offset, val);
774 val &= ~pcs_soc->irq_enable_mask;
775 pcs->write(val, pcs->base + offset);
776 }
777 }
778
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700779 pin = &pcs->pins.pa[i];
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700780 pin->number = i;
781 pcs->pins.cur++;
782
783 return i;
784}
785
786/**
787 * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
788 * @pcs: pcs driver instance
789 *
790 * In case of errors, resources are freed in pcs_free_resources.
791 *
792 * If your hardware needs holes in the address space, then just set
793 * up multiple driver instances.
794 */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800795static int pcs_allocate_pin_table(struct pcs_device *pcs)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700796{
797 int mux_bytes, nr_pins, i;
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530798 int num_pins_in_register = 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700799
800 mux_bytes = pcs->width / BITS_PER_BYTE;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530801
802 if (pcs->bits_per_mux) {
803 pcs->bits_per_pin = fls(pcs->fmask);
804 nr_pins = (pcs->size * BITS_PER_BYTE) / pcs->bits_per_pin;
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530805 num_pins_in_register = pcs->width / pcs->bits_per_pin;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530806 } else {
807 nr_pins = pcs->size / mux_bytes;
808 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700809
810 dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
811 pcs->pins.pa = devm_kzalloc(pcs->dev,
812 sizeof(*pcs->pins.pa) * nr_pins,
813 GFP_KERNEL);
814 if (!pcs->pins.pa)
815 return -ENOMEM;
816
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700817 pcs->desc.pins = pcs->pins.pa;
818 pcs->desc.npins = nr_pins;
819
820 for (i = 0; i < pcs->desc.npins; i++) {
821 unsigned offset;
822 int res;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530823 int byte_num;
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530824 int pin_pos = 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700825
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530826 if (pcs->bits_per_mux) {
827 byte_num = (pcs->bits_per_pin * i) / BITS_PER_BYTE;
828 offset = (byte_num / mux_bytes) * mux_bytes;
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530829 pin_pos = i % num_pins_in_register;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530830 } else {
831 offset = i * mux_bytes;
832 }
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530833 res = pcs_add_pin(pcs, offset, pin_pos);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700834 if (res < 0) {
835 dev_err(pcs->dev, "error adding pins: %i\n", res);
836 return res;
837 }
838 }
839
840 return 0;
841}
842
843/**
844 * pcs_add_function() - adds a new function to the function list
845 * @pcs: pcs driver instance
846 * @np: device node of the mux entry
847 * @name: name of the function
848 * @vals: array of mux register value pairs used by the function
849 * @nvals: number of mux register value pairs
850 * @pgnames: array of pingroup names for the function
851 * @npgnames: number of pingroup names
852 */
853static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
854 struct device_node *np,
855 const char *name,
856 struct pcs_func_vals *vals,
857 unsigned nvals,
858 const char **pgnames,
859 unsigned npgnames)
860{
861 struct pcs_function *function;
862
863 function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
864 if (!function)
865 return NULL;
866
867 function->name = name;
868 function->vals = vals;
869 function->nvals = nvals;
870 function->pgnames = pgnames;
871 function->npgnames = npgnames;
872
873 mutex_lock(&pcs->mutex);
874 list_add_tail(&function->node, &pcs->functions);
875 radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
876 pcs->nfuncs++;
877 mutex_unlock(&pcs->mutex);
878
879 return function;
880}
881
882static void pcs_remove_function(struct pcs_device *pcs,
883 struct pcs_function *function)
884{
885 int i;
886
887 mutex_lock(&pcs->mutex);
888 for (i = 0; i < pcs->nfuncs; i++) {
889 struct pcs_function *found;
890
891 found = radix_tree_lookup(&pcs->ftree, i);
892 if (found == function)
893 radix_tree_delete(&pcs->ftree, i);
894 }
895 list_del(&function->node);
896 mutex_unlock(&pcs->mutex);
897}
898
899/**
900 * pcs_add_pingroup() - add a pingroup to the pingroup list
901 * @pcs: pcs driver instance
902 * @np: device node of the mux entry
903 * @name: name of the pingroup
904 * @gpins: array of the pins that belong to the group
905 * @ngpins: number of pins in the group
906 */
907static int pcs_add_pingroup(struct pcs_device *pcs,
908 struct device_node *np,
909 const char *name,
910 int *gpins,
911 int ngpins)
912{
913 struct pcs_pingroup *pingroup;
914
915 pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
916 if (!pingroup)
917 return -ENOMEM;
918
919 pingroup->name = name;
920 pingroup->np = np;
921 pingroup->gpins = gpins;
922 pingroup->ngpins = ngpins;
923
924 mutex_lock(&pcs->mutex);
925 list_add_tail(&pingroup->node, &pcs->pingroups);
926 radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
927 pcs->ngroups++;
928 mutex_unlock(&pcs->mutex);
929
930 return 0;
931}
932
933/**
934 * pcs_get_pin_by_offset() - get a pin index based on the register offset
935 * @pcs: pcs driver instance
936 * @offset: register offset from the base
937 *
938 * Note that this is OK as long as the pins are in a static array.
939 */
940static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
941{
942 unsigned index;
943
944 if (offset >= pcs->size) {
945 dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
946 offset, pcs->size);
947 return -EINVAL;
948 }
949
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530950 if (pcs->bits_per_mux)
951 index = (offset * BITS_PER_BYTE) / pcs->bits_per_pin;
952 else
953 index = offset / (pcs->width / BITS_PER_BYTE);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700954
955 return index;
956}
957
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800958/*
959 * check whether data matches enable bits or disable bits
960 * Return value: 1 for matching enable bits, 0 for matching disable bits,
961 * and negative value for matching failure.
962 */
963static int pcs_config_match(unsigned data, unsigned enable, unsigned disable)
964{
965 int ret = -EINVAL;
966
967 if (data == enable)
968 ret = 1;
969 else if (data == disable)
970 ret = 0;
971 return ret;
972}
973
974static void add_config(struct pcs_conf_vals **conf, enum pin_config_param param,
975 unsigned value, unsigned enable, unsigned disable,
976 unsigned mask)
977{
978 (*conf)->param = param;
979 (*conf)->val = value;
980 (*conf)->enable = enable;
981 (*conf)->disable = disable;
982 (*conf)->mask = mask;
983 (*conf)++;
984}
985
986static void add_setting(unsigned long **setting, enum pin_config_param param,
987 unsigned arg)
988{
989 **setting = pinconf_to_config_packed(param, arg);
990 (*setting)++;
991}
992
993/* add pinconf setting with 2 parameters */
994static void pcs_add_conf2(struct pcs_device *pcs, struct device_node *np,
995 const char *name, enum pin_config_param param,
996 struct pcs_conf_vals **conf, unsigned long **settings)
997{
Haojian Zhuang7cba5b32013-03-13 16:01:26 +0800998 unsigned value[2], shift;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800999 int ret;
1000
1001 ret = of_property_read_u32_array(np, name, value, 2);
1002 if (ret)
1003 return;
1004 /* set value & mask */
1005 value[0] &= value[1];
Haojian Zhuang7cba5b32013-03-13 16:01:26 +08001006 shift = ffs(value[1]) - 1;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001007 /* skip enable & disable */
1008 add_config(conf, param, value[0], 0, 0, value[1]);
Haojian Zhuang7cba5b32013-03-13 16:01:26 +08001009 add_setting(settings, param, value[0] >> shift);
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001010}
1011
1012/* add pinconf setting with 4 parameters */
1013static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np,
1014 const char *name, enum pin_config_param param,
1015 struct pcs_conf_vals **conf, unsigned long **settings)
1016{
1017 unsigned value[4];
1018 int ret;
1019
1020 /* value to set, enable, disable, mask */
1021 ret = of_property_read_u32_array(np, name, value, 4);
1022 if (ret)
1023 return;
1024 if (!value[3]) {
1025 dev_err(pcs->dev, "mask field of the property can't be 0\n");
1026 return;
1027 }
1028 value[0] &= value[3];
1029 value[1] &= value[3];
1030 value[2] &= value[3];
1031 ret = pcs_config_match(value[0], value[1], value[2]);
1032 if (ret < 0)
1033 dev_dbg(pcs->dev, "failed to match enable or disable bits\n");
1034 add_config(conf, param, value[0], value[1], value[2], value[3]);
1035 add_setting(settings, param, ret);
1036}
1037
1038static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
1039 struct pcs_function *func,
1040 struct pinctrl_map **map)
1041
1042{
1043 struct pinctrl_map *m = *map;
1044 int i = 0, nconfs = 0;
1045 unsigned long *settings = NULL, *s = NULL;
1046 struct pcs_conf_vals *conf = NULL;
1047 struct pcs_conf_type prop2[] = {
1048 { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, },
1049 { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, },
1050 { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, },
Chao Xie4bd75472014-01-28 15:20:44 +08001051 { "pinctrl-single,low-power-mode", PIN_CONFIG_LOW_POWER_MODE, },
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001052 };
1053 struct pcs_conf_type prop4[] = {
1054 { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, },
1055 { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, },
1056 { "pinctrl-single,input-schmitt-enable",
1057 PIN_CONFIG_INPUT_SCHMITT_ENABLE, },
1058 };
1059
1060 /* If pinconf isn't supported, don't parse properties in below. */
Tony Lindgren02e483f2013-10-02 21:39:39 -07001061 if (!PCS_HAS_PINCONF)
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001062 return 0;
1063
1064 /* cacluate how much properties are supported in current node */
1065 for (i = 0; i < ARRAY_SIZE(prop2); i++) {
1066 if (of_find_property(np, prop2[i].name, NULL))
1067 nconfs++;
1068 }
1069 for (i = 0; i < ARRAY_SIZE(prop4); i++) {
1070 if (of_find_property(np, prop4[i].name, NULL))
1071 nconfs++;
1072 }
1073 if (!nconfs)
1074 return 0;
1075
1076 func->conf = devm_kzalloc(pcs->dev,
1077 sizeof(struct pcs_conf_vals) * nconfs,
1078 GFP_KERNEL);
1079 if (!func->conf)
1080 return -ENOMEM;
1081 func->nconfs = nconfs;
1082 conf = &(func->conf[0]);
1083 m++;
1084 settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs,
1085 GFP_KERNEL);
1086 if (!settings)
1087 return -ENOMEM;
1088 s = &settings[0];
1089
1090 for (i = 0; i < ARRAY_SIZE(prop2); i++)
1091 pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param,
1092 &conf, &s);
1093 for (i = 0; i < ARRAY_SIZE(prop4); i++)
1094 pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param,
1095 &conf, &s);
1096 m->type = PIN_MAP_TYPE_CONFIGS_GROUP;
1097 m->data.configs.group_or_pin = np->name;
1098 m->data.configs.configs = settings;
1099 m->data.configs.num_configs = nconfs;
1100 return 0;
1101}
1102
1103static void pcs_free_pingroups(struct pcs_device *pcs);
1104
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001105/**
1106 * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
1107 * @pcs: pinctrl driver instance
1108 * @np: device node of the mux entry
1109 * @map: map entry
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001110 * @num_maps: number of map
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001111 * @pgnames: pingroup names
1112 *
1113 * Note that this binding currently supports only sets of one register + value.
1114 *
1115 * Also note that this driver tries to avoid understanding pin and function
1116 * names because of the extra bloat they would cause especially in the case of
1117 * a large number of pins. This driver just sets what is specified for the board
1118 * in the .dts file. Further user space debugging tools can be developed to
1119 * decipher the pin and function names using debugfs.
1120 *
1121 * If you are concerned about the boot time, set up the static pins in
1122 * the bootloader, and only set up selected pins as device tree entries.
1123 */
1124static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
1125 struct device_node *np,
1126 struct pinctrl_map **map,
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001127 unsigned *num_maps,
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001128 const char **pgnames)
1129{
Tony Lindgren46222152016-11-03 09:35:48 -07001130 const char *name = "pinctrl-single,pins";
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001131 struct pcs_func_vals *vals;
Tony Lindgren46222152016-11-03 09:35:48 -07001132 int rows, *pins, found = 0, res = -ENOMEM, i;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001133 struct pcs_function *function;
1134
Tony Lindgren46222152016-11-03 09:35:48 -07001135 rows = pinctrl_count_index_with_args(np, name);
Axel Haslamde7416b2016-11-09 15:54:00 +01001136 if (rows <= 0) {
1137 dev_err(pcs->dev, "Ivalid number of rows: %d\n", rows);
1138 return -EINVAL;
1139 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001140
1141 vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
1142 if (!vals)
1143 return -ENOMEM;
1144
1145 pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
1146 if (!pins)
1147 goto free_vals;
1148
Tony Lindgren46222152016-11-03 09:35:48 -07001149 for (i = 0; i < rows; i++) {
1150 struct of_phandle_args pinctrl_spec;
1151 unsigned int offset;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001152 int pin;
1153
Tony Lindgren46222152016-11-03 09:35:48 -07001154 res = pinctrl_parse_index_with_args(np, name, i, &pinctrl_spec);
1155 if (res)
1156 return res;
1157
1158 if (pinctrl_spec.args_count < 2) {
1159 dev_err(pcs->dev, "invalid args_count for spec: %i\n",
1160 pinctrl_spec.args_count);
1161 break;
1162 }
1163
1164 /* Index plus one value cell */
1165 offset = pinctrl_spec.args[0];
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001166 vals[found].reg = pcs->base + offset;
Tony Lindgren46222152016-11-03 09:35:48 -07001167 vals[found].val = pinctrl_spec.args[1];
1168
1169 dev_dbg(pcs->dev, "%s index: 0x%x value: 0x%x\n",
1170 pinctrl_spec.np->name, offset, pinctrl_spec.args[1]);
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001171
1172 pin = pcs_get_pin_by_offset(pcs, offset);
1173 if (pin < 0) {
1174 dev_err(pcs->dev,
1175 "could not add functions for %s %ux\n",
1176 np->name, offset);
1177 break;
1178 }
1179 pins[found++] = pin;
1180 }
1181
1182 pgnames[0] = np->name;
1183 function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
1184 if (!function)
1185 goto free_pins;
1186
1187 res = pcs_add_pingroup(pcs, np, np->name, pins, found);
1188 if (res < 0)
1189 goto free_function;
1190
1191 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
1192 (*map)->data.mux.group = np->name;
1193 (*map)->data.mux.function = np->name;
1194
Tony Lindgren02e483f2013-10-02 21:39:39 -07001195 if (PCS_HAS_PINCONF) {
Wei Yongjun18442e62013-05-07 20:06:19 +08001196 res = pcs_parse_pinconf(pcs, np, function, map);
1197 if (res)
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001198 goto free_pingroups;
1199 *num_maps = 2;
1200 } else {
1201 *num_maps = 1;
1202 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001203 return 0;
1204
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001205free_pingroups:
1206 pcs_free_pingroups(pcs);
1207 *num_maps = 1;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001208free_function:
1209 pcs_remove_function(pcs, function);
1210
1211free_pins:
1212 devm_kfree(pcs->dev, pins);
1213
1214free_vals:
1215 devm_kfree(pcs->dev, vals);
1216
1217 return res;
1218}
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301219
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301220static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
1221 struct device_node *np,
1222 struct pinctrl_map **map,
1223 unsigned *num_maps,
1224 const char **pgnames)
1225{
Tony Lindgren22d51272016-11-03 09:35:49 -07001226 const char *name = "pinctrl-single,pins";
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301227 struct pcs_func_vals *vals;
Tony Lindgren22d51272016-11-03 09:35:49 -07001228 int rows, *pins, found = 0, res = -ENOMEM, i;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301229 int npins_in_row;
1230 struct pcs_function *function;
1231
Tony Lindgren22d51272016-11-03 09:35:49 -07001232 rows = pinctrl_count_index_with_args(np, name);
Axel Haslamde7416b2016-11-09 15:54:00 +01001233 if (rows <= 0) {
1234 dev_err(pcs->dev, "Invalid number of rows: %d\n", rows);
1235 return -EINVAL;
1236 }
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301237
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301238 npins_in_row = pcs->width / pcs->bits_per_pin;
1239
1240 vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows * npins_in_row,
1241 GFP_KERNEL);
1242 if (!vals)
1243 return -ENOMEM;
1244
1245 pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows * npins_in_row,
1246 GFP_KERNEL);
1247 if (!pins)
1248 goto free_vals;
1249
Tony Lindgren22d51272016-11-03 09:35:49 -07001250 for (i = 0; i < rows; i++) {
1251 struct of_phandle_args pinctrl_spec;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301252 unsigned offset, val;
1253 unsigned mask, bit_pos, val_pos, mask_pos, submask;
1254 unsigned pin_num_from_lsb;
1255 int pin;
1256
Tony Lindgren22d51272016-11-03 09:35:49 -07001257 res = pinctrl_parse_index_with_args(np, name, i, &pinctrl_spec);
1258 if (res)
1259 return res;
1260
1261 if (pinctrl_spec.args_count < 3) {
1262 dev_err(pcs->dev, "invalid args_count for spec: %i\n",
1263 pinctrl_spec.args_count);
1264 break;
1265 }
1266
1267 /* Index plus two value cells */
1268 offset = pinctrl_spec.args[0];
1269 val = pinctrl_spec.args[1];
1270 mask = pinctrl_spec.args[2];
1271
1272 dev_dbg(pcs->dev, "%s index: 0x%x value: 0x%x mask: 0x%x\n",
1273 pinctrl_spec.np->name, offset, val, mask);
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301274
1275 /* Parse pins in each row from LSB */
1276 while (mask) {
Keerthy56b367c2016-04-14 10:29:16 +05301277 bit_pos = __ffs(mask);
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301278 pin_num_from_lsb = bit_pos / pcs->bits_per_pin;
Keerthy56b367c2016-04-14 10:29:16 +05301279 mask_pos = ((pcs->fmask) << bit_pos);
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301280 val_pos = val & mask_pos;
1281 submask = mask & mask_pos;
Tomi Valkeinenad5d25f2014-01-09 14:50:29 +02001282
1283 if ((mask & mask_pos) == 0) {
1284 dev_err(pcs->dev,
1285 "Invalid mask for %s at 0x%x\n",
1286 np->name, offset);
1287 break;
1288 }
1289
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301290 mask &= ~mask_pos;
1291
1292 if (submask != mask_pos) {
1293 dev_warn(pcs->dev,
1294 "Invalid submask 0x%x for %s at 0x%x\n",
1295 submask, np->name, offset);
1296 continue;
1297 }
1298
1299 vals[found].mask = submask;
1300 vals[found].reg = pcs->base + offset;
1301 vals[found].val = val_pos;
1302
1303 pin = pcs_get_pin_by_offset(pcs, offset);
1304 if (pin < 0) {
1305 dev_err(pcs->dev,
1306 "could not add functions for %s %ux\n",
1307 np->name, offset);
1308 break;
1309 }
1310 pins[found++] = pin + pin_num_from_lsb;
1311 }
1312 }
1313
1314 pgnames[0] = np->name;
1315 function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
1316 if (!function)
1317 goto free_pins;
1318
1319 res = pcs_add_pingroup(pcs, np, np->name, pins, found);
1320 if (res < 0)
1321 goto free_function;
1322
1323 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
1324 (*map)->data.mux.group = np->name;
1325 (*map)->data.mux.function = np->name;
1326
Tony Lindgren02e483f2013-10-02 21:39:39 -07001327 if (PCS_HAS_PINCONF) {
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301328 dev_err(pcs->dev, "pinconf not supported\n");
1329 goto free_pingroups;
1330 }
1331
1332 *num_maps = 1;
1333 return 0;
1334
1335free_pingroups:
1336 pcs_free_pingroups(pcs);
1337 *num_maps = 1;
1338free_function:
1339 pcs_remove_function(pcs, function);
1340
1341free_pins:
1342 devm_kfree(pcs->dev, pins);
1343
1344free_vals:
1345 devm_kfree(pcs->dev, vals);
1346
1347 return res;
1348}
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001349/**
1350 * pcs_dt_node_to_map() - allocates and parses pinctrl maps
1351 * @pctldev: pinctrl instance
1352 * @np_config: device tree pinmux entry
1353 * @map: array of map entries
1354 * @num_maps: number of maps
1355 */
1356static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
1357 struct device_node *np_config,
1358 struct pinctrl_map **map, unsigned *num_maps)
1359{
1360 struct pcs_device *pcs;
1361 const char **pgnames;
1362 int ret;
1363
1364 pcs = pinctrl_dev_get_drvdata(pctldev);
1365
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001366 /* create 2 maps. One is for pinmux, and the other is for pinconf. */
1367 *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL);
Sachin Kamat00e79d12012-11-20 16:34:39 +05301368 if (!*map)
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001369 return -ENOMEM;
1370
1371 *num_maps = 0;
1372
1373 pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
1374 if (!pgnames) {
1375 ret = -ENOMEM;
1376 goto free_map;
1377 }
1378
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301379 if (pcs->bits_per_mux) {
1380 ret = pcs_parse_bits_in_pinctrl_entry(pcs, np_config, map,
1381 num_maps, pgnames);
1382 if (ret < 0) {
1383 dev_err(pcs->dev, "no pins entries for %s\n",
1384 np_config->name);
1385 goto free_pgnames;
1386 }
1387 } else {
1388 ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map,
1389 num_maps, pgnames);
1390 if (ret < 0) {
1391 dev_err(pcs->dev, "no pins entries for %s\n",
1392 np_config->name);
1393 goto free_pgnames;
1394 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001395 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001396
1397 return 0;
1398
1399free_pgnames:
1400 devm_kfree(pcs->dev, pgnames);
1401free_map:
1402 devm_kfree(pcs->dev, *map);
1403
1404 return ret;
1405}
1406
1407/**
1408 * pcs_free_funcs() - free memory used by functions
1409 * @pcs: pcs driver instance
1410 */
1411static void pcs_free_funcs(struct pcs_device *pcs)
1412{
1413 struct list_head *pos, *tmp;
1414 int i;
1415
1416 mutex_lock(&pcs->mutex);
1417 for (i = 0; i < pcs->nfuncs; i++) {
1418 struct pcs_function *func;
1419
1420 func = radix_tree_lookup(&pcs->ftree, i);
1421 if (!func)
1422 continue;
1423 radix_tree_delete(&pcs->ftree, i);
1424 }
1425 list_for_each_safe(pos, tmp, &pcs->functions) {
1426 struct pcs_function *function;
1427
1428 function = list_entry(pos, struct pcs_function, node);
1429 list_del(&function->node);
1430 }
1431 mutex_unlock(&pcs->mutex);
1432}
1433
1434/**
1435 * pcs_free_pingroups() - free memory used by pingroups
1436 * @pcs: pcs driver instance
1437 */
1438static void pcs_free_pingroups(struct pcs_device *pcs)
1439{
1440 struct list_head *pos, *tmp;
1441 int i;
1442
1443 mutex_lock(&pcs->mutex);
1444 for (i = 0; i < pcs->ngroups; i++) {
1445 struct pcs_pingroup *pingroup;
1446
1447 pingroup = radix_tree_lookup(&pcs->pgtree, i);
1448 if (!pingroup)
1449 continue;
1450 radix_tree_delete(&pcs->pgtree, i);
1451 }
1452 list_for_each_safe(pos, tmp, &pcs->pingroups) {
1453 struct pcs_pingroup *pingroup;
1454
1455 pingroup = list_entry(pos, struct pcs_pingroup, node);
1456 list_del(&pingroup->node);
1457 }
1458 mutex_unlock(&pcs->mutex);
1459}
1460
1461/**
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001462 * pcs_irq_free() - free interrupt
1463 * @pcs: pcs driver instance
1464 */
1465static void pcs_irq_free(struct pcs_device *pcs)
1466{
1467 struct pcs_soc_data *pcs_soc = &pcs->socdata;
1468
1469 if (pcs_soc->irq < 0)
1470 return;
1471
1472 if (pcs->domain)
1473 irq_domain_remove(pcs->domain);
1474
1475 if (PCS_QUIRK_HAS_SHARED_IRQ)
1476 free_irq(pcs_soc->irq, pcs_soc);
1477 else
1478 irq_set_chained_handler(pcs_soc->irq, NULL);
1479}
1480
1481/**
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001482 * pcs_free_resources() - free memory used by this driver
1483 * @pcs: pcs driver instance
1484 */
1485static void pcs_free_resources(struct pcs_device *pcs)
1486{
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001487 pcs_irq_free(pcs);
Markus Elfringf10a2582015-11-05 17:10:22 +01001488 pinctrl_unregister(pcs->pctl);
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001489 pcs_free_funcs(pcs);
1490 pcs_free_pingroups(pcs);
Tony Lindgren46222152016-11-03 09:35:48 -07001491#if IS_BUILTIN(CONFIG_PINCTRL_SINGLE)
1492 if (pcs->missing_nr_pinctrl_cells)
1493 of_remove_property(pcs->np, pcs->missing_nr_pinctrl_cells);
1494#endif
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001495}
1496
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001497static const struct of_device_id pcs_of_match[];
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001498
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001499static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
1500{
1501 const char *propname = "pinctrl-single,gpio-range";
1502 const char *cellname = "#pinctrl-single,gpio-range-cells";
1503 struct of_phandle_args gpiospec;
1504 struct pcs_gpiofunc_range *range;
1505 int ret, i;
1506
1507 for (i = 0; ; i++) {
1508 ret = of_parse_phandle_with_args(node, propname, cellname,
1509 i, &gpiospec);
1510 /* Do not treat it as error. Only treat it as end condition. */
1511 if (ret) {
1512 ret = 0;
1513 break;
1514 }
1515 range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
1516 if (!range) {
1517 ret = -ENOMEM;
1518 break;
1519 }
1520 range->offset = gpiospec.args[0];
1521 range->npins = gpiospec.args[1];
1522 range->gpiofunc = gpiospec.args[2];
1523 mutex_lock(&pcs->mutex);
1524 list_add_tail(&range->node, &pcs->gpiofuncs);
1525 mutex_unlock(&pcs->mutex);
1526 }
1527 return ret;
1528}
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001529/**
1530 * @reg: virtual address of interrupt register
1531 * @hwirq: hardware irq number
1532 * @irq: virtual irq number
1533 * @node: list node
1534 */
1535struct pcs_interrupt {
1536 void __iomem *reg;
1537 irq_hw_number_t hwirq;
1538 unsigned int irq;
1539 struct list_head node;
1540};
1541
1542/**
1543 * pcs_irq_set() - enables or disables an interrupt
1544 *
1545 * Note that this currently assumes one interrupt per pinctrl
1546 * register that is typically used for wake-up events.
1547 */
1548static inline void pcs_irq_set(struct pcs_soc_data *pcs_soc,
1549 int irq, const bool enable)
1550{
1551 struct pcs_device *pcs;
1552 struct list_head *pos;
1553 unsigned mask;
1554
1555 pcs = container_of(pcs_soc, struct pcs_device, socdata);
1556 list_for_each(pos, &pcs->irqs) {
1557 struct pcs_interrupt *pcswi;
1558 unsigned soc_mask;
1559
1560 pcswi = list_entry(pos, struct pcs_interrupt, node);
1561 if (irq != pcswi->irq)
1562 continue;
1563
1564 soc_mask = pcs_soc->irq_enable_mask;
1565 raw_spin_lock(&pcs->lock);
1566 mask = pcs->read(pcswi->reg);
1567 if (enable)
1568 mask |= soc_mask;
1569 else
1570 mask &= ~soc_mask;
1571 pcs->write(mask, pcswi->reg);
Tony Lindgren0ac3c0a42016-05-31 14:17:06 -07001572
1573 /* flush posted write */
1574 mask = pcs->read(pcswi->reg);
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001575 raw_spin_unlock(&pcs->lock);
1576 }
Roger Quadrosc9b3a7d2013-10-11 19:13:16 +03001577
1578 if (pcs_soc->rearm)
1579 pcs_soc->rearm();
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001580}
1581
1582/**
1583 * pcs_irq_mask() - mask pinctrl interrupt
1584 * @d: interrupt data
1585 */
1586static void pcs_irq_mask(struct irq_data *d)
1587{
1588 struct pcs_soc_data *pcs_soc = irq_data_get_irq_chip_data(d);
1589
1590 pcs_irq_set(pcs_soc, d->irq, false);
1591}
1592
1593/**
1594 * pcs_irq_unmask() - unmask pinctrl interrupt
1595 * @d: interrupt data
1596 */
1597static void pcs_irq_unmask(struct irq_data *d)
1598{
1599 struct pcs_soc_data *pcs_soc = irq_data_get_irq_chip_data(d);
1600
1601 pcs_irq_set(pcs_soc, d->irq, true);
1602}
1603
1604/**
1605 * pcs_irq_set_wake() - toggle the suspend and resume wake up
1606 * @d: interrupt data
1607 * @state: wake-up state
1608 *
1609 * Note that this should be called only for suspend and resume.
1610 * For runtime PM, the wake-up events should be enabled by default.
1611 */
1612static int pcs_irq_set_wake(struct irq_data *d, unsigned int state)
1613{
1614 if (state)
1615 pcs_irq_unmask(d);
1616 else
1617 pcs_irq_mask(d);
1618
1619 return 0;
1620}
1621
1622/**
1623 * pcs_irq_handle() - common interrupt handler
1624 * @pcs_irq: interrupt data
1625 *
1626 * Note that this currently assumes we have one interrupt bit per
1627 * mux register. This interrupt is typically used for wake-up events.
1628 * For more complex interrupts different handlers can be specified.
1629 */
1630static int pcs_irq_handle(struct pcs_soc_data *pcs_soc)
1631{
1632 struct pcs_device *pcs;
1633 struct list_head *pos;
1634 int count = 0;
1635
1636 pcs = container_of(pcs_soc, struct pcs_device, socdata);
1637 list_for_each(pos, &pcs->irqs) {
1638 struct pcs_interrupt *pcswi;
1639 unsigned mask;
1640
1641 pcswi = list_entry(pos, struct pcs_interrupt, node);
1642 raw_spin_lock(&pcs->lock);
1643 mask = pcs->read(pcswi->reg);
1644 raw_spin_unlock(&pcs->lock);
1645 if (mask & pcs_soc->irq_status_mask) {
1646 generic_handle_irq(irq_find_mapping(pcs->domain,
1647 pcswi->hwirq));
1648 count++;
1649 }
1650 }
1651
1652 return count;
1653}
1654
1655/**
1656 * pcs_irq_handler() - handler for the shared interrupt case
1657 * @irq: interrupt
1658 * @d: data
1659 *
1660 * Use this for cases where multiple instances of
1661 * pinctrl-single share a single interrupt like on omaps.
1662 */
1663static irqreturn_t pcs_irq_handler(int irq, void *d)
1664{
1665 struct pcs_soc_data *pcs_soc = d;
1666
1667 return pcs_irq_handle(pcs_soc) ? IRQ_HANDLED : IRQ_NONE;
1668}
1669
1670/**
1671 * pcs_irq_handle() - handler for the dedicated chained interrupt case
1672 * @irq: interrupt
1673 * @desc: interrupt descriptor
1674 *
1675 * Use this if you have a separate interrupt for each
1676 * pinctrl-single instance.
1677 */
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001678static void pcs_irq_chain_handler(struct irq_desc *desc)
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001679{
1680 struct pcs_soc_data *pcs_soc = irq_desc_get_handler_data(desc);
1681 struct irq_chip *chip;
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001682
Jiang Liu5663bb22015-06-04 12:13:16 +08001683 chip = irq_desc_get_chip(desc);
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001684 chained_irq_enter(chip, desc);
Rickard Strandqvist849bfe02014-06-26 15:43:01 +02001685 pcs_irq_handle(pcs_soc);
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001686 /* REVISIT: export and add handle_bad_irq(irq, desc)? */
1687 chained_irq_exit(chip, desc);
1688
1689 return;
1690}
1691
1692static int pcs_irqdomain_map(struct irq_domain *d, unsigned int irq,
1693 irq_hw_number_t hwirq)
1694{
1695 struct pcs_soc_data *pcs_soc = d->host_data;
1696 struct pcs_device *pcs;
1697 struct pcs_interrupt *pcswi;
1698
1699 pcs = container_of(pcs_soc, struct pcs_device, socdata);
1700 pcswi = devm_kzalloc(pcs->dev, sizeof(*pcswi), GFP_KERNEL);
1701 if (!pcswi)
1702 return -ENOMEM;
1703
1704 pcswi->reg = pcs->base + hwirq;
1705 pcswi->hwirq = hwirq;
1706 pcswi->irq = irq;
1707
1708 mutex_lock(&pcs->mutex);
1709 list_add_tail(&pcswi->node, &pcs->irqs);
1710 mutex_unlock(&pcs->mutex);
1711
1712 irq_set_chip_data(irq, pcs_soc);
1713 irq_set_chip_and_handler(irq, &pcs->chip,
1714 handle_level_irq);
Sudeep Holla3c177a12016-02-01 18:28:17 +00001715 irq_set_lockdep_class(irq, &pcs_lock_class);
Tony Lindgren1b9c0fb2013-10-18 16:20:05 -07001716 irq_set_noprobe(irq);
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001717
1718 return 0;
1719}
1720
Krzysztof Kozlowskie5b60952015-04-27 21:54:06 +09001721static const struct irq_domain_ops pcs_irqdomain_ops = {
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001722 .map = pcs_irqdomain_map,
1723 .xlate = irq_domain_xlate_onecell,
1724};
1725
1726/**
1727 * pcs_irq_init_chained_handler() - set up a chained interrupt handler
1728 * @pcs: pcs driver instance
1729 * @np: device node pointer
1730 */
1731static int pcs_irq_init_chained_handler(struct pcs_device *pcs,
1732 struct device_node *np)
1733{
1734 struct pcs_soc_data *pcs_soc = &pcs->socdata;
1735 const char *name = "pinctrl";
1736 int num_irqs;
1737
1738 if (!pcs_soc->irq_enable_mask ||
1739 !pcs_soc->irq_status_mask) {
1740 pcs_soc->irq = -1;
1741 return -EINVAL;
1742 }
1743
1744 INIT_LIST_HEAD(&pcs->irqs);
1745 pcs->chip.name = name;
1746 pcs->chip.irq_ack = pcs_irq_mask;
1747 pcs->chip.irq_mask = pcs_irq_mask;
1748 pcs->chip.irq_unmask = pcs_irq_unmask;
1749 pcs->chip.irq_set_wake = pcs_irq_set_wake;
1750
1751 if (PCS_QUIRK_HAS_SHARED_IRQ) {
1752 int res;
1753
1754 res = request_irq(pcs_soc->irq, pcs_irq_handler,
Grygorii Strashkoc10372e2015-07-06 18:13:37 +03001755 IRQF_SHARED | IRQF_NO_SUSPEND |
1756 IRQF_NO_THREAD,
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001757 name, pcs_soc);
1758 if (res) {
1759 pcs_soc->irq = -1;
1760 return res;
1761 }
1762 } else {
Thomas Gleixner20d5d142015-06-21 21:11:06 +02001763 irq_set_chained_handler_and_data(pcs_soc->irq,
1764 pcs_irq_chain_handler,
1765 pcs_soc);
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001766 }
1767
1768 /*
1769 * We can use the register offset as the hardirq
1770 * number as irq_domain_add_simple maps them lazily.
1771 * This way we can easily support more than one
1772 * interrupt per function if needed.
1773 */
1774 num_irqs = pcs->size;
1775
1776 pcs->domain = irq_domain_add_simple(np, num_irqs, 0,
1777 &pcs_irqdomain_ops,
1778 pcs_soc);
1779 if (!pcs->domain) {
1780 irq_set_chained_handler(pcs_soc->irq, NULL);
1781 return -EINVAL;
1782 }
1783
1784 return 0;
1785}
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001786
Jean-Francois Moine8cb440a2013-07-15 10:14:26 +02001787#ifdef CONFIG_PM
Hebbar Gururaja0f9bc4b2013-05-31 15:43:01 +05301788static int pinctrl_single_suspend(struct platform_device *pdev,
1789 pm_message_t state)
1790{
1791 struct pcs_device *pcs;
1792
1793 pcs = platform_get_drvdata(pdev);
1794 if (!pcs)
1795 return -EINVAL;
1796
1797 return pinctrl_force_sleep(pcs->pctl);
1798}
1799
1800static int pinctrl_single_resume(struct platform_device *pdev)
1801{
1802 struct pcs_device *pcs;
1803
1804 pcs = platform_get_drvdata(pdev);
1805 if (!pcs)
1806 return -EINVAL;
1807
1808 return pinctrl_force_default(pcs->pctl);
1809}
Jean-Francois Moine8cb440a2013-07-15 10:14:26 +02001810#endif
Hebbar Gururaja0f9bc4b2013-05-31 15:43:01 +05301811
Tony Lindgren46222152016-11-03 09:35:48 -07001812/**
1813 * pcs_quirk_missing_pinctrl_cells - handle legacy binding
1814 * @pcs: pinctrl driver instance
1815 * @np: device tree node
1816 * @cells: number of cells
1817 *
1818 * Handle legacy binding with no #pinctrl-cells. This should be
1819 * always two pinctrl-single,bit-per-mux and one for others.
1820 * At some point we may want to consider removing this.
1821 */
1822static int pcs_quirk_missing_pinctrl_cells(struct pcs_device *pcs,
1823 struct device_node *np,
1824 int cells)
1825{
1826 struct property *p;
1827 const char *name = "#pinctrl-cells";
1828 int error;
1829 u32 val;
1830
1831 error = of_property_read_u32(np, name, &val);
1832 if (!error)
1833 return 0;
1834
1835 dev_warn(pcs->dev, "please update dts to use %s = <%i>\n",
1836 name, cells);
1837
1838 p = devm_kzalloc(pcs->dev, sizeof(*p), GFP_KERNEL);
1839 if (!p)
1840 return -ENOMEM;
1841
1842 p->length = sizeof(__be32);
1843 p->value = devm_kzalloc(pcs->dev, sizeof(__be32), GFP_KERNEL);
1844 if (!p->value)
1845 return -ENOMEM;
1846 *(__be32 *)p->value = cpu_to_be32(cells);
1847
1848 p->name = devm_kstrdup(pcs->dev, name, GFP_KERNEL);
1849 if (!p->name)
1850 return -ENOMEM;
1851
1852 pcs->missing_nr_pinctrl_cells = p;
1853
1854#if IS_BUILTIN(CONFIG_PINCTRL_SINGLE)
1855 error = of_add_property(np, pcs->missing_nr_pinctrl_cells);
1856#endif
1857
1858 return error;
1859}
1860
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001861static int pcs_probe(struct platform_device *pdev)
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001862{
1863 struct device_node *np = pdev->dev.of_node;
1864 const struct of_device_id *match;
Tony Lindgrendc7743a2013-10-02 21:39:40 -07001865 struct pcs_pdata *pdata;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001866 struct resource *res;
1867 struct pcs_device *pcs;
Tony Lindgren02e483f2013-10-02 21:39:39 -07001868 const struct pcs_soc_data *soc;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001869 int ret;
1870
1871 match = of_match_device(pcs_of_match, &pdev->dev);
1872 if (!match)
1873 return -EINVAL;
1874
1875 pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
1876 if (!pcs) {
1877 dev_err(&pdev->dev, "could not allocate\n");
1878 return -ENOMEM;
1879 }
1880 pcs->dev = &pdev->dev;
Tony Lindgren46222152016-11-03 09:35:48 -07001881 pcs->np = np;
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001882 raw_spin_lock_init(&pcs->lock);
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001883 mutex_init(&pcs->mutex);
1884 INIT_LIST_HEAD(&pcs->pingroups);
1885 INIT_LIST_HEAD(&pcs->functions);
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001886 INIT_LIST_HEAD(&pcs->gpiofuncs);
Tony Lindgren02e483f2013-10-02 21:39:39 -07001887 soc = match->data;
1888 pcs->flags = soc->flags;
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001889 memcpy(&pcs->socdata, soc, sizeof(*soc));
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001890
Tony Lindgrencd236042016-10-25 09:09:00 -07001891 ret = of_property_read_u32(np, "pinctrl-single,register-width",
1892 &pcs->width);
1893 if (ret) {
1894 dev_err(pcs->dev, "register width not specified\n");
1895
1896 return ret;
1897 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001898
Haojian Zhuang477ac772013-02-17 19:42:54 +08001899 ret = of_property_read_u32(np, "pinctrl-single,function-mask",
1900 &pcs->fmask);
1901 if (!ret) {
Keerthy56b367c2016-04-14 10:29:16 +05301902 pcs->fshift = __ffs(pcs->fmask);
Haojian Zhuang477ac772013-02-17 19:42:54 +08001903 pcs->fmax = pcs->fmask >> pcs->fshift;
1904 } else {
1905 /* If mask property doesn't exist, function mux is invalid. */
1906 pcs->fmask = 0;
1907 pcs->fshift = 0;
1908 pcs->fmax = 0;
1909 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001910
1911 ret = of_property_read_u32(np, "pinctrl-single,function-off",
1912 &pcs->foff);
1913 if (ret)
1914 pcs->foff = PCS_OFF_DISABLED;
1915
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +03001916 pcs->bits_per_mux = of_property_read_bool(np,
1917 "pinctrl-single,bit-per-mux");
Tony Lindgren46222152016-11-03 09:35:48 -07001918 ret = pcs_quirk_missing_pinctrl_cells(pcs, np,
1919 pcs->bits_per_mux ? 2 : 1);
1920 if (ret) {
1921 dev_err(&pdev->dev, "unable to patch #pinctrl-cells\n");
1922
1923 return ret;
1924 }
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +03001925
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001926 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1927 if (!res) {
1928 dev_err(pcs->dev, "could not get resource\n");
1929 return -ENODEV;
1930 }
1931
1932 pcs->res = devm_request_mem_region(pcs->dev, res->start,
1933 resource_size(res), DRIVER_NAME);
1934 if (!pcs->res) {
1935 dev_err(pcs->dev, "could not get mem_region\n");
1936 return -EBUSY;
1937 }
1938
1939 pcs->size = resource_size(pcs->res);
1940 pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
1941 if (!pcs->base) {
1942 dev_err(pcs->dev, "could not ioremap\n");
1943 return -ENODEV;
1944 }
1945
1946 INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
1947 INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
1948 platform_set_drvdata(pdev, pcs);
1949
1950 switch (pcs->width) {
1951 case 8:
1952 pcs->read = pcs_readb;
1953 pcs->write = pcs_writeb;
1954 break;
1955 case 16:
1956 pcs->read = pcs_readw;
1957 pcs->write = pcs_writew;
1958 break;
1959 case 32:
1960 pcs->read = pcs_readl;
1961 pcs->write = pcs_writel;
1962 break;
1963 default:
1964 break;
1965 }
1966
1967 pcs->desc.name = DRIVER_NAME;
1968 pcs->desc.pctlops = &pcs_pinctrl_ops;
1969 pcs->desc.pmxops = &pcs_pinmux_ops;
Tony Lindgren02e483f2013-10-02 21:39:39 -07001970 if (PCS_HAS_PINCONF)
Axel Lina7bbdd72013-03-04 13:47:39 +08001971 pcs->desc.confops = &pcs_pinconf_ops;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001972 pcs->desc.owner = THIS_MODULE;
1973
1974 ret = pcs_allocate_pin_table(pcs);
1975 if (ret < 0)
1976 goto free;
1977
1978 pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001979 if (IS_ERR(pcs->pctl)) {
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001980 dev_err(pcs->dev, "could not register single pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001981 ret = PTR_ERR(pcs->pctl);
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001982 goto free;
1983 }
1984
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001985 ret = pcs_add_gpio_func(np, pcs);
1986 if (ret < 0)
1987 goto free;
1988
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001989 pcs->socdata.irq = irq_of_parse_and_map(np, 0);
1990 if (pcs->socdata.irq)
1991 pcs->flags |= PCS_FEAT_IRQ;
1992
Tony Lindgrendc7743a2013-10-02 21:39:40 -07001993 /* We still need auxdata for some omaps for PRM interrupts */
1994 pdata = dev_get_platdata(&pdev->dev);
1995 if (pdata) {
1996 if (pdata->rearm)
1997 pcs->socdata.rearm = pdata->rearm;
1998 if (pdata->irq) {
1999 pcs->socdata.irq = pdata->irq;
2000 pcs->flags |= PCS_FEAT_IRQ;
2001 }
2002 }
2003
Tony Lindgren3e6cee12013-10-02 21:39:40 -07002004 if (PCS_HAS_IRQ) {
2005 ret = pcs_irq_init_chained_handler(pcs, np);
2006 if (ret < 0)
2007 dev_warn(pcs->dev, "initialized with no interrupts\n");
2008 }
2009
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002010 dev_info(pcs->dev, "%i pins at pa %p size %u\n",
2011 pcs->desc.npins, pcs->base, pcs->size);
2012
2013 return 0;
2014
2015free:
2016 pcs_free_resources(pcs);
2017
2018 return ret;
2019}
2020
Bill Pembertonf90f54b2012-11-19 13:26:06 -05002021static int pcs_remove(struct platform_device *pdev)
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002022{
2023 struct pcs_device *pcs = platform_get_drvdata(pdev);
2024
2025 if (!pcs)
2026 return 0;
2027
2028 pcs_free_resources(pcs);
2029
2030 return 0;
2031}
2032
Tony Lindgren3e6cee12013-10-02 21:39:40 -07002033static const struct pcs_soc_data pinctrl_single_omap_wkup = {
2034 .flags = PCS_QUIRK_SHARED_IRQ,
2035 .irq_enable_mask = (1 << 14), /* OMAP_WAKEUP_EN */
2036 .irq_status_mask = (1 << 15), /* OMAP_WAKEUP_EVENT */
2037};
2038
Nishanth Menon31320be2014-08-22 09:01:01 -05002039static const struct pcs_soc_data pinctrl_single_dra7 = {
Nishanth Menon31320be2014-08-22 09:01:01 -05002040 .irq_enable_mask = (1 << 24), /* WAKEUPENABLE */
2041 .irq_status_mask = (1 << 25), /* WAKEUPEVENT */
2042};
2043
Keerthyaa2293d2014-08-22 09:01:02 -05002044static const struct pcs_soc_data pinctrl_single_am437x = {
2045 .flags = PCS_QUIRK_SHARED_IRQ,
2046 .irq_enable_mask = (1 << 29), /* OMAP_WAKEUP_EN */
2047 .irq_status_mask = (1 << 30), /* OMAP_WAKEUP_EVENT */
2048};
2049
Tony Lindgren02e483f2013-10-02 21:39:39 -07002050static const struct pcs_soc_data pinctrl_single = {
2051};
2052
2053static const struct pcs_soc_data pinconf_single = {
2054 .flags = PCS_FEAT_PINCONF,
2055};
2056
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01002057static const struct of_device_id pcs_of_match[] = {
Tony Lindgren3e6cee12013-10-02 21:39:40 -07002058 { .compatible = "ti,omap3-padconf", .data = &pinctrl_single_omap_wkup },
2059 { .compatible = "ti,omap4-padconf", .data = &pinctrl_single_omap_wkup },
2060 { .compatible = "ti,omap5-padconf", .data = &pinctrl_single_omap_wkup },
Nishanth Menon31320be2014-08-22 09:01:01 -05002061 { .compatible = "ti,dra7-padconf", .data = &pinctrl_single_dra7 },
Keerthyaa2293d2014-08-22 09:01:02 -05002062 { .compatible = "ti,am437-padconf", .data = &pinctrl_single_am437x },
Tony Lindgren02e483f2013-10-02 21:39:39 -07002063 { .compatible = "pinctrl-single", .data = &pinctrl_single },
2064 { .compatible = "pinconf-single", .data = &pinconf_single },
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002065 { },
2066};
2067MODULE_DEVICE_TABLE(of, pcs_of_match);
2068
2069static struct platform_driver pcs_driver = {
2070 .probe = pcs_probe,
Bill Pemberton2a36f082012-11-19 13:21:27 -05002071 .remove = pcs_remove,
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002072 .driver = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002073 .name = DRIVER_NAME,
2074 .of_match_table = pcs_of_match,
2075 },
Hebbar Gururaja0f9bc4b2013-05-31 15:43:01 +05302076#ifdef CONFIG_PM
2077 .suspend = pinctrl_single_suspend,
2078 .resume = pinctrl_single_resume,
2079#endif
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002080};
2081
2082module_platform_driver(pcs_driver);
2083
2084MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
2085MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
2086MODULE_LICENSE("GPL v2");