blob: dad65df36f4b438aaaf246df91579ca86a22efb5 [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
31#include "core.h"
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +080032#include "pinconf.h"
Tony Lindgren8b8b0912012-07-10 02:05:46 -070033
34#define DRIVER_NAME "pinctrl-single"
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +030035#define PCS_MUX_PINS_NAME "pinctrl-single,pins"
36#define PCS_MUX_BITS_NAME "pinctrl-single,bits"
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +053037#define PCS_REG_NAME_LEN ((sizeof(unsigned long) * 2) + 3)
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/**
143 * struct pcs_name - register name for a pin
144 * @name: name of the pinctrl register
145 *
146 * REVISIT: We may want to make names optional in the pinctrl
147 * framework as some drivers may not care about pin names to
148 * avoid kernel bloat. The pin names can be deciphered by user
149 * space tools using debugfs based on the register address and
150 * SoC packaging information.
151 */
152struct pcs_name {
153 char name[PCS_REG_NAME_LEN];
154};
155
156/**
Tony Lindgren02e483f2013-10-02 21:39:39 -0700157 * struct pcs_soc_data - SoC specific settings
158 * @flags: initial SoC specific PCS_FEAT_xxx values
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700159 * @irq: optional interrupt for the controller
160 * @irq_enable_mask: optional SoC specific interrupt enable mask
161 * @irq_status_mask: optional SoC specific interrupt status mask
Tony Lindgren02e483f2013-10-02 21:39:39 -0700162 */
163struct pcs_soc_data {
164 unsigned flags;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700165 int irq;
166 unsigned irq_enable_mask;
167 unsigned irq_status_mask;
Tony Lindgren02e483f2013-10-02 21:39:39 -0700168};
169
170/**
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700171 * struct pcs_device - pinctrl device instance
172 * @res: resources
173 * @base: virtual address of the controller
174 * @size: size of the ioremapped area
175 * @dev: device entry
176 * @pctl: pin controller device
Tony Lindgren02e483f2013-10-02 21:39:39 -0700177 * @flags: mask of PCS_FEAT_xxx values
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700178 * @lock: spinlock for register access
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700179 * @mutex: mutex protecting the lists
180 * @width: bits per mux register
181 * @fmask: function register mask
182 * @fshift: function register shift
183 * @foff: value to turn mux off
184 * @fmax: max number of functions in fmask
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530185 * @bits_per_pin:number of bits per pin
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700186 * @names: array of register names for pins
187 * @pins: physical pins on the SoC
188 * @pgtree: pingroup index radix tree
189 * @ftree: function index radix tree
190 * @pingroups: list of pingroups
191 * @functions: list of functions
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800192 * @gpiofuncs: list of gpio functions
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700193 * @irqs: list of interrupt registers
194 * @chip: chip container for this instance
195 * @domain: IRQ domain for this instance
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700196 * @ngroups: number of pingroups
197 * @nfuncs: number of functions
198 * @desc: pin controller descriptor
199 * @read: register read function to use
200 * @write: register write function to use
201 */
202struct pcs_device {
203 struct resource *res;
204 void __iomem *base;
205 unsigned size;
206 struct device *dev;
207 struct pinctrl_dev *pctl;
Tony Lindgren02e483f2013-10-02 21:39:39 -0700208 unsigned flags;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700209#define PCS_QUIRK_SHARED_IRQ (1 << 2)
210#define PCS_FEAT_IRQ (1 << 1)
Tony Lindgren02e483f2013-10-02 21:39:39 -0700211#define PCS_FEAT_PINCONF (1 << 0)
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700212 struct pcs_soc_data socdata;
213 raw_spinlock_t lock;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700214 struct mutex mutex;
215 unsigned width;
216 unsigned fmask;
217 unsigned fshift;
218 unsigned foff;
219 unsigned fmax;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300220 bool bits_per_mux;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530221 unsigned bits_per_pin;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700222 struct pcs_name *names;
223 struct pcs_data pins;
224 struct radix_tree_root pgtree;
225 struct radix_tree_root ftree;
226 struct list_head pingroups;
227 struct list_head functions;
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800228 struct list_head gpiofuncs;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700229 struct list_head irqs;
230 struct irq_chip chip;
231 struct irq_domain *domain;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700232 unsigned ngroups;
233 unsigned nfuncs;
234 struct pinctrl_desc desc;
235 unsigned (*read)(void __iomem *reg);
236 void (*write)(unsigned val, void __iomem *reg);
237};
238
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700239#define PCS_QUIRK_HAS_SHARED_IRQ (pcs->flags & PCS_QUIRK_SHARED_IRQ)
240#define PCS_HAS_IRQ (pcs->flags & PCS_FEAT_IRQ)
Tony Lindgren02e483f2013-10-02 21:39:39 -0700241#define PCS_HAS_PINCONF (pcs->flags & PCS_FEAT_PINCONF)
242
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800243static int pcs_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
244 unsigned long *config);
245static int pcs_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
Sherman Yin03b054e2013-08-27 11:32:12 -0700246 unsigned long *configs, unsigned num_configs);
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800247
248static enum pin_config_param pcs_bias[] = {
249 PIN_CONFIG_BIAS_PULL_DOWN,
250 PIN_CONFIG_BIAS_PULL_UP,
251};
252
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700253/*
254 * REVISIT: Reads and writes could eventually use regmap or something
255 * generic. But at least on omaps, some mux registers are performance
256 * critical as they may need to be remuxed every time before and after
257 * idle. Adding tests for register access width for every read and
258 * write like regmap is doing is not desired, and caching the registers
259 * does not help in this case.
260 */
261
262static unsigned __maybe_unused pcs_readb(void __iomem *reg)
263{
264 return readb(reg);
265}
266
267static unsigned __maybe_unused pcs_readw(void __iomem *reg)
268{
269 return readw(reg);
270}
271
272static unsigned __maybe_unused pcs_readl(void __iomem *reg)
273{
274 return readl(reg);
275}
276
277static void __maybe_unused pcs_writeb(unsigned val, void __iomem *reg)
278{
279 writeb(val, reg);
280}
281
282static void __maybe_unused pcs_writew(unsigned val, void __iomem *reg)
283{
284 writew(val, reg);
285}
286
287static void __maybe_unused pcs_writel(unsigned val, void __iomem *reg)
288{
289 writel(val, reg);
290}
291
292static int pcs_get_groups_count(struct pinctrl_dev *pctldev)
293{
294 struct pcs_device *pcs;
295
296 pcs = pinctrl_dev_get_drvdata(pctldev);
297
298 return pcs->ngroups;
299}
300
301static const char *pcs_get_group_name(struct pinctrl_dev *pctldev,
302 unsigned gselector)
303{
304 struct pcs_device *pcs;
305 struct pcs_pingroup *group;
306
307 pcs = pinctrl_dev_get_drvdata(pctldev);
308 group = radix_tree_lookup(&pcs->pgtree, gselector);
309 if (!group) {
310 dev_err(pcs->dev, "%s could not find pingroup%i\n",
311 __func__, gselector);
312 return NULL;
313 }
314
315 return group->name;
316}
317
318static int pcs_get_group_pins(struct pinctrl_dev *pctldev,
319 unsigned gselector,
320 const unsigned **pins,
321 unsigned *npins)
322{
323 struct pcs_device *pcs;
324 struct pcs_pingroup *group;
325
326 pcs = pinctrl_dev_get_drvdata(pctldev);
327 group = radix_tree_lookup(&pcs->pgtree, gselector);
328 if (!group) {
329 dev_err(pcs->dev, "%s could not find pingroup%i\n",
330 __func__, gselector);
331 return -EINVAL;
332 }
333
334 *pins = group->gpins;
335 *npins = group->ngpins;
336
337 return 0;
338}
339
340static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev,
341 struct seq_file *s,
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800342 unsigned pin)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700343{
Matt Porter7d66ce72012-09-26 15:07:43 -0400344 struct pcs_device *pcs;
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800345 unsigned val, mux_bytes;
Matt Porter7d66ce72012-09-26 15:07:43 -0400346
347 pcs = pinctrl_dev_get_drvdata(pctldev);
348
Haojian Zhuange7ed6712012-11-07 23:19:42 +0800349 mux_bytes = pcs->width / BITS_PER_BYTE;
350 val = pcs->read(pcs->base + pin * mux_bytes);
Matt Porter7d66ce72012-09-26 15:07:43 -0400351
352 seq_printf(s, "%08x %s " , val, DRIVER_NAME);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700353}
354
355static void pcs_dt_free_map(struct pinctrl_dev *pctldev,
356 struct pinctrl_map *map, unsigned num_maps)
357{
358 struct pcs_device *pcs;
359
360 pcs = pinctrl_dev_get_drvdata(pctldev);
361 devm_kfree(pcs->dev, map);
362}
363
364static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
365 struct device_node *np_config,
366 struct pinctrl_map **map, unsigned *num_maps);
367
Laurent Pinchart022ab142013-02-16 10:25:07 +0100368static const struct pinctrl_ops pcs_pinctrl_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700369 .get_groups_count = pcs_get_groups_count,
370 .get_group_name = pcs_get_group_name,
371 .get_group_pins = pcs_get_group_pins,
372 .pin_dbg_show = pcs_pin_dbg_show,
373 .dt_node_to_map = pcs_dt_node_to_map,
374 .dt_free_map = pcs_dt_free_map,
375};
376
377static int pcs_get_functions_count(struct pinctrl_dev *pctldev)
378{
379 struct pcs_device *pcs;
380
381 pcs = pinctrl_dev_get_drvdata(pctldev);
382
383 return pcs->nfuncs;
384}
385
386static const char *pcs_get_function_name(struct pinctrl_dev *pctldev,
387 unsigned fselector)
388{
389 struct pcs_device *pcs;
390 struct pcs_function *func;
391
392 pcs = pinctrl_dev_get_drvdata(pctldev);
393 func = radix_tree_lookup(&pcs->ftree, fselector);
394 if (!func) {
395 dev_err(pcs->dev, "%s could not find function%i\n",
396 __func__, fselector);
397 return NULL;
398 }
399
400 return func->name;
401}
402
403static int pcs_get_function_groups(struct pinctrl_dev *pctldev,
404 unsigned fselector,
405 const char * const **groups,
406 unsigned * const ngroups)
407{
408 struct pcs_device *pcs;
409 struct pcs_function *func;
410
411 pcs = pinctrl_dev_get_drvdata(pctldev);
412 func = radix_tree_lookup(&pcs->ftree, fselector);
413 if (!func) {
414 dev_err(pcs->dev, "%s could not find function%i\n",
415 __func__, fselector);
416 return -EINVAL;
417 }
418 *groups = func->pgnames;
419 *ngroups = func->npgnames;
420
421 return 0;
422}
423
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800424static int pcs_get_function(struct pinctrl_dev *pctldev, unsigned pin,
425 struct pcs_function **func)
426{
427 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
428 struct pin_desc *pdesc = pin_desc_get(pctldev, pin);
429 const struct pinctrl_setting_mux *setting;
430 unsigned fselector;
431
432 /* If pin is not described in DTS & enabled, mux_setting is NULL. */
433 setting = pdesc->mux_setting;
434 if (!setting)
435 return -ENOTSUPP;
436 fselector = setting->func;
437 *func = radix_tree_lookup(&pcs->ftree, fselector);
438 if (!(*func)) {
439 dev_err(pcs->dev, "%s could not find function%i\n",
440 __func__, fselector);
441 return -ENOTSUPP;
442 }
443 return 0;
444}
445
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700446static int pcs_enable(struct pinctrl_dev *pctldev, unsigned fselector,
447 unsigned group)
448{
449 struct pcs_device *pcs;
450 struct pcs_function *func;
451 int i;
452
453 pcs = pinctrl_dev_get_drvdata(pctldev);
Haojian Zhuang477ac772013-02-17 19:42:54 +0800454 /* If function mask is null, needn't enable it. */
455 if (!pcs->fmask)
456 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700457 func = radix_tree_lookup(&pcs->ftree, fselector);
458 if (!func)
459 return -EINVAL;
460
461 dev_dbg(pcs->dev, "enabling %s function%i\n",
462 func->name, fselector);
463
464 for (i = 0; i < func->nvals; i++) {
465 struct pcs_func_vals *vals;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700466 unsigned long flags;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300467 unsigned val, mask;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700468
469 vals = &func->vals[i];
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700470 raw_spin_lock_irqsave(&pcs->lock, flags);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700471 val = pcs->read(vals->reg);
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530472
473 if (pcs->bits_per_mux)
474 mask = vals->mask;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300475 else
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530476 mask = pcs->fmask;
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +0300477
478 val &= ~mask;
479 val |= (vals->val & mask);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700480 pcs->write(val, vals->reg);
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700481 raw_spin_unlock_irqrestore(&pcs->lock, flags);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700482 }
483
484 return 0;
485}
486
487static void pcs_disable(struct pinctrl_dev *pctldev, unsigned fselector,
488 unsigned group)
489{
490 struct pcs_device *pcs;
491 struct pcs_function *func;
492 int i;
493
494 pcs = pinctrl_dev_get_drvdata(pctldev);
Haojian Zhuang477ac772013-02-17 19:42:54 +0800495 /* If function mask is null, needn't disable it. */
496 if (!pcs->fmask)
497 return;
498
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700499 func = radix_tree_lookup(&pcs->ftree, fselector);
500 if (!func) {
501 dev_err(pcs->dev, "%s could not find function%i\n",
502 __func__, fselector);
503 return;
504 }
505
506 /*
507 * Ignore disable if function-off is not specified. Some hardware
508 * does not have clearly defined disable function. For pin specific
509 * off modes, you can use alternate named states as described in
510 * pinctrl-bindings.txt.
511 */
512 if (pcs->foff == PCS_OFF_DISABLED) {
513 dev_dbg(pcs->dev, "ignoring disable for %s function%i\n",
514 func->name, fselector);
515 return;
516 }
517
518 dev_dbg(pcs->dev, "disabling function%i %s\n",
519 fselector, func->name);
520
521 for (i = 0; i < func->nvals; i++) {
522 struct pcs_func_vals *vals;
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700523 unsigned long flags;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700524 unsigned val;
525
526 vals = &func->vals[i];
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700527 raw_spin_lock_irqsave(&pcs->lock, flags);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700528 val = pcs->read(vals->reg);
529 val &= ~pcs->fmask;
530 val |= pcs->foff << pcs->fshift;
531 pcs->write(val, vals->reg);
Tony Lindgren3e6cee12013-10-02 21:39:40 -0700532 raw_spin_unlock_irqrestore(&pcs->lock, flags);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700533 }
534}
535
536static int pcs_request_gpio(struct pinctrl_dev *pctldev,
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800537 struct pinctrl_gpio_range *range, unsigned pin)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700538{
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800539 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
540 struct pcs_gpiofunc_range *frange = NULL;
541 struct list_head *pos, *tmp;
542 int mux_bytes = 0;
543 unsigned data;
544
Haojian Zhuang477ac772013-02-17 19:42:54 +0800545 /* If function mask is null, return directly. */
546 if (!pcs->fmask)
547 return -ENOTSUPP;
548
Haojian Zhuanga1a277e2013-02-17 19:42:52 +0800549 list_for_each_safe(pos, tmp, &pcs->gpiofuncs) {
550 frange = list_entry(pos, struct pcs_gpiofunc_range, node);
551 if (pin >= frange->offset + frange->npins
552 || pin < frange->offset)
553 continue;
554 mux_bytes = pcs->width / BITS_PER_BYTE;
555 data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask;
556 data |= frange->gpiofunc;
557 pcs->write(data, pcs->base + pin * mux_bytes);
558 break;
559 }
560 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700561}
562
Laurent Pinchart022ab142013-02-16 10:25:07 +0100563static const struct pinmux_ops pcs_pinmux_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700564 .get_functions_count = pcs_get_functions_count,
565 .get_function_name = pcs_get_function_name,
566 .get_function_groups = pcs_get_function_groups,
567 .enable = pcs_enable,
568 .disable = pcs_disable,
569 .gpio_request_enable = pcs_request_gpio,
570};
571
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800572/* Clear BIAS value */
573static void pcs_pinconf_clear_bias(struct pinctrl_dev *pctldev, unsigned pin)
574{
575 unsigned long config;
576 int i;
577 for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) {
578 config = pinconf_to_config_packed(pcs_bias[i], 0);
Sherman Yin03b054e2013-08-27 11:32:12 -0700579 pcs_pinconf_set(pctldev, pin, &config, 1);
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800580 }
581}
582
583/*
584 * Check whether PIN_CONFIG_BIAS_DISABLE is valid.
585 * It's depend on that PULL_DOWN & PULL_UP configs are all invalid.
586 */
587static bool pcs_pinconf_bias_disable(struct pinctrl_dev *pctldev, unsigned pin)
588{
589 unsigned long config;
590 int i;
591
592 for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) {
593 config = pinconf_to_config_packed(pcs_bias[i], 0);
594 if (!pcs_pinconf_get(pctldev, pin, &config))
595 goto out;
596 }
597 return true;
598out:
599 return false;
600}
601
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700602static int pcs_pinconf_get(struct pinctrl_dev *pctldev,
603 unsigned pin, unsigned long *config)
604{
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800605 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
606 struct pcs_function *func;
607 enum pin_config_param param;
608 unsigned offset = 0, data = 0, i, j, ret;
609
610 ret = pcs_get_function(pctldev, pin, &func);
611 if (ret)
612 return ret;
613
614 for (i = 0; i < func->nconfs; i++) {
615 param = pinconf_to_config_param(*config);
616 if (param == PIN_CONFIG_BIAS_DISABLE) {
617 if (pcs_pinconf_bias_disable(pctldev, pin)) {
618 *config = 0;
619 return 0;
620 } else {
621 return -ENOTSUPP;
622 }
623 } else if (param != func->conf[i].param) {
624 continue;
625 }
626
627 offset = pin * (pcs->width / BITS_PER_BYTE);
628 data = pcs->read(pcs->base + offset) & func->conf[i].mask;
629 switch (func->conf[i].param) {
630 /* 4 parameters */
631 case PIN_CONFIG_BIAS_PULL_DOWN:
632 case PIN_CONFIG_BIAS_PULL_UP:
633 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
634 if ((data != func->conf[i].enable) ||
635 (data == func->conf[i].disable))
636 return -ENOTSUPP;
637 *config = 0;
638 break;
639 /* 2 parameters */
640 case PIN_CONFIG_INPUT_SCHMITT:
641 for (j = 0; j < func->nconfs; j++) {
642 switch (func->conf[j].param) {
643 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
644 if (data != func->conf[j].enable)
645 return -ENOTSUPP;
646 break;
647 default:
648 break;
649 }
650 }
651 *config = data;
652 break;
653 case PIN_CONFIG_DRIVE_STRENGTH:
654 case PIN_CONFIG_SLEW_RATE:
655 default:
656 *config = data;
657 break;
658 }
659 return 0;
660 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700661 return -ENOTSUPP;
662}
663
664static int pcs_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700665 unsigned pin, unsigned long *configs,
666 unsigned num_configs)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700667{
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800668 struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev);
669 struct pcs_function *func;
Haojian Zhuang7cba5b32013-03-13 16:01:26 +0800670 unsigned offset = 0, shift = 0, i, data, ret;
671 u16 arg;
Sherman Yin03b054e2013-08-27 11:32:12 -0700672 int j;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800673
674 ret = pcs_get_function(pctldev, pin, &func);
675 if (ret)
676 return ret;
677
Sherman Yin03b054e2013-08-27 11:32:12 -0700678 for (j = 0; j < num_configs; j++) {
679 for (i = 0; i < func->nconfs; i++) {
680 if (pinconf_to_config_param(configs[j])
681 != func->conf[i].param)
682 continue;
683
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800684 offset = pin * (pcs->width / BITS_PER_BYTE);
685 data = pcs->read(pcs->base + offset);
Sherman Yin03b054e2013-08-27 11:32:12 -0700686 arg = pinconf_to_config_argument(configs[j]);
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800687 switch (func->conf[i].param) {
688 /* 2 parameters */
689 case PIN_CONFIG_INPUT_SCHMITT:
690 case PIN_CONFIG_DRIVE_STRENGTH:
691 case PIN_CONFIG_SLEW_RATE:
692 shift = ffs(func->conf[i].mask) - 1;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800693 data &= ~func->conf[i].mask;
694 data |= (arg << shift) & func->conf[i].mask;
695 break;
696 /* 4 parameters */
697 case PIN_CONFIG_BIAS_DISABLE:
698 pcs_pinconf_clear_bias(pctldev, pin);
699 break;
700 case PIN_CONFIG_BIAS_PULL_DOWN:
701 case PIN_CONFIG_BIAS_PULL_UP:
Haojian Zhuang7cba5b32013-03-13 16:01:26 +0800702 if (arg)
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800703 pcs_pinconf_clear_bias(pctldev, pin);
704 /* fall through */
705 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
706 data &= ~func->conf[i].mask;
Haojian Zhuang7cba5b32013-03-13 16:01:26 +0800707 if (arg)
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800708 data |= func->conf[i].enable;
709 else
710 data |= func->conf[i].disable;
711 break;
712 default:
713 return -ENOTSUPP;
714 }
715 pcs->write(data, pcs->base + offset);
Sherman Yin03b054e2013-08-27 11:32:12 -0700716
717 break;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800718 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700719 if (i >= func->nconfs)
720 return -ENOTSUPP;
721 } /* for each config */
722
723 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700724}
725
726static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev,
727 unsigned group, unsigned long *config)
728{
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800729 const unsigned *pins;
730 unsigned npins, old = 0;
731 int i, ret;
732
733 ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
734 if (ret)
735 return ret;
736 for (i = 0; i < npins; i++) {
737 if (pcs_pinconf_get(pctldev, pins[i], config))
738 return -ENOTSUPP;
739 /* configs do not match between two pins */
740 if (i && (old != *config))
741 return -ENOTSUPP;
742 old = *config;
743 }
744 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700745}
746
747static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700748 unsigned group, unsigned long *configs,
749 unsigned num_configs)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700750{
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800751 const unsigned *pins;
752 unsigned npins;
753 int i, ret;
754
755 ret = pcs_get_group_pins(pctldev, group, &pins, &npins);
756 if (ret)
757 return ret;
758 for (i = 0; i < npins; i++) {
Sherman Yin03b054e2013-08-27 11:32:12 -0700759 if (pcs_pinconf_set(pctldev, pins[i], configs, num_configs))
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800760 return -ENOTSUPP;
761 }
762 return 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700763}
764
765static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev,
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800766 struct seq_file *s, unsigned pin)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700767{
768}
769
770static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
771 struct seq_file *s, unsigned selector)
772{
773}
774
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800775static void pcs_pinconf_config_dbg_show(struct pinctrl_dev *pctldev,
776 struct seq_file *s,
777 unsigned long config)
778{
779 pinconf_generic_dump_config(pctldev, s, config);
780}
781
Laurent Pinchart022ab142013-02-16 10:25:07 +0100782static const struct pinconf_ops pcs_pinconf_ops = {
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700783 .pin_config_get = pcs_pinconf_get,
784 .pin_config_set = pcs_pinconf_set,
785 .pin_config_group_get = pcs_pinconf_group_get,
786 .pin_config_group_set = pcs_pinconf_group_set,
787 .pin_config_dbg_show = pcs_pinconf_dbg_show,
788 .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show,
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +0800789 .pin_config_config_dbg_show = pcs_pinconf_config_dbg_show,
Axel Lina7bbdd72013-03-04 13:47:39 +0800790 .is_generic = true,
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700791};
792
793/**
794 * pcs_add_pin() - add a pin to the static per controller pin array
795 * @pcs: pcs driver instance
796 * @offset: register offset from base
797 */
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530798static int pcs_add_pin(struct pcs_device *pcs, unsigned offset,
799 unsigned pin_pos)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700800{
801 struct pinctrl_pin_desc *pin;
802 struct pcs_name *pn;
803 int i;
804
805 i = pcs->pins.cur;
806 if (i >= pcs->desc.npins) {
807 dev_err(pcs->dev, "too many pins, max %i\n",
808 pcs->desc.npins);
809 return -ENOMEM;
810 }
811
812 pin = &pcs->pins.pa[i];
813 pn = &pcs->names[i];
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530814 sprintf(pn->name, "%lx.%d",
815 (unsigned long)pcs->res->start + offset, pin_pos);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700816 pin->name = pn->name;
817 pin->number = i;
818 pcs->pins.cur++;
819
820 return i;
821}
822
823/**
824 * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver
825 * @pcs: pcs driver instance
826 *
827 * In case of errors, resources are freed in pcs_free_resources.
828 *
829 * If your hardware needs holes in the address space, then just set
830 * up multiple driver instances.
831 */
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800832static int pcs_allocate_pin_table(struct pcs_device *pcs)
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700833{
834 int mux_bytes, nr_pins, i;
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530835 int num_pins_in_register = 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700836
837 mux_bytes = pcs->width / BITS_PER_BYTE;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530838
839 if (pcs->bits_per_mux) {
840 pcs->bits_per_pin = fls(pcs->fmask);
841 nr_pins = (pcs->size * BITS_PER_BYTE) / pcs->bits_per_pin;
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530842 num_pins_in_register = pcs->width / pcs->bits_per_pin;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530843 } else {
844 nr_pins = pcs->size / mux_bytes;
845 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700846
847 dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins);
848 pcs->pins.pa = devm_kzalloc(pcs->dev,
849 sizeof(*pcs->pins.pa) * nr_pins,
850 GFP_KERNEL);
851 if (!pcs->pins.pa)
852 return -ENOMEM;
853
854 pcs->names = devm_kzalloc(pcs->dev,
855 sizeof(struct pcs_name) * nr_pins,
856 GFP_KERNEL);
857 if (!pcs->names)
858 return -ENOMEM;
859
860 pcs->desc.pins = pcs->pins.pa;
861 pcs->desc.npins = nr_pins;
862
863 for (i = 0; i < pcs->desc.npins; i++) {
864 unsigned offset;
865 int res;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530866 int byte_num;
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530867 int pin_pos = 0;
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700868
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530869 if (pcs->bits_per_mux) {
870 byte_num = (pcs->bits_per_pin * i) / BITS_PER_BYTE;
871 offset = (byte_num / mux_bytes) * mux_bytes;
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530872 pin_pos = i % num_pins_in_register;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530873 } else {
874 offset = i * mux_bytes;
875 }
Manjunathappa, Prakash6f924b02013-05-21 19:38:01 +0530876 res = pcs_add_pin(pcs, offset, pin_pos);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700877 if (res < 0) {
878 dev_err(pcs->dev, "error adding pins: %i\n", res);
879 return res;
880 }
881 }
882
883 return 0;
884}
885
886/**
887 * pcs_add_function() - adds a new function to the function list
888 * @pcs: pcs driver instance
889 * @np: device node of the mux entry
890 * @name: name of the function
891 * @vals: array of mux register value pairs used by the function
892 * @nvals: number of mux register value pairs
893 * @pgnames: array of pingroup names for the function
894 * @npgnames: number of pingroup names
895 */
896static struct pcs_function *pcs_add_function(struct pcs_device *pcs,
897 struct device_node *np,
898 const char *name,
899 struct pcs_func_vals *vals,
900 unsigned nvals,
901 const char **pgnames,
902 unsigned npgnames)
903{
904 struct pcs_function *function;
905
906 function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL);
907 if (!function)
908 return NULL;
909
910 function->name = name;
911 function->vals = vals;
912 function->nvals = nvals;
913 function->pgnames = pgnames;
914 function->npgnames = npgnames;
915
916 mutex_lock(&pcs->mutex);
917 list_add_tail(&function->node, &pcs->functions);
918 radix_tree_insert(&pcs->ftree, pcs->nfuncs, function);
919 pcs->nfuncs++;
920 mutex_unlock(&pcs->mutex);
921
922 return function;
923}
924
925static void pcs_remove_function(struct pcs_device *pcs,
926 struct pcs_function *function)
927{
928 int i;
929
930 mutex_lock(&pcs->mutex);
931 for (i = 0; i < pcs->nfuncs; i++) {
932 struct pcs_function *found;
933
934 found = radix_tree_lookup(&pcs->ftree, i);
935 if (found == function)
936 radix_tree_delete(&pcs->ftree, i);
937 }
938 list_del(&function->node);
939 mutex_unlock(&pcs->mutex);
940}
941
942/**
943 * pcs_add_pingroup() - add a pingroup to the pingroup list
944 * @pcs: pcs driver instance
945 * @np: device node of the mux entry
946 * @name: name of the pingroup
947 * @gpins: array of the pins that belong to the group
948 * @ngpins: number of pins in the group
949 */
950static int pcs_add_pingroup(struct pcs_device *pcs,
951 struct device_node *np,
952 const char *name,
953 int *gpins,
954 int ngpins)
955{
956 struct pcs_pingroup *pingroup;
957
958 pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL);
959 if (!pingroup)
960 return -ENOMEM;
961
962 pingroup->name = name;
963 pingroup->np = np;
964 pingroup->gpins = gpins;
965 pingroup->ngpins = ngpins;
966
967 mutex_lock(&pcs->mutex);
968 list_add_tail(&pingroup->node, &pcs->pingroups);
969 radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup);
970 pcs->ngroups++;
971 mutex_unlock(&pcs->mutex);
972
973 return 0;
974}
975
976/**
977 * pcs_get_pin_by_offset() - get a pin index based on the register offset
978 * @pcs: pcs driver instance
979 * @offset: register offset from the base
980 *
981 * Note that this is OK as long as the pins are in a static array.
982 */
983static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset)
984{
985 unsigned index;
986
987 if (offset >= pcs->size) {
988 dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n",
989 offset, pcs->size);
990 return -EINVAL;
991 }
992
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +0530993 if (pcs->bits_per_mux)
994 index = (offset * BITS_PER_BYTE) / pcs->bits_per_pin;
995 else
996 index = offset / (pcs->width / BITS_PER_BYTE);
Tony Lindgren8b8b0912012-07-10 02:05:46 -0700997
998 return index;
999}
1000
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001001/*
1002 * check whether data matches enable bits or disable bits
1003 * Return value: 1 for matching enable bits, 0 for matching disable bits,
1004 * and negative value for matching failure.
1005 */
1006static int pcs_config_match(unsigned data, unsigned enable, unsigned disable)
1007{
1008 int ret = -EINVAL;
1009
1010 if (data == enable)
1011 ret = 1;
1012 else if (data == disable)
1013 ret = 0;
1014 return ret;
1015}
1016
1017static void add_config(struct pcs_conf_vals **conf, enum pin_config_param param,
1018 unsigned value, unsigned enable, unsigned disable,
1019 unsigned mask)
1020{
1021 (*conf)->param = param;
1022 (*conf)->val = value;
1023 (*conf)->enable = enable;
1024 (*conf)->disable = disable;
1025 (*conf)->mask = mask;
1026 (*conf)++;
1027}
1028
1029static void add_setting(unsigned long **setting, enum pin_config_param param,
1030 unsigned arg)
1031{
1032 **setting = pinconf_to_config_packed(param, arg);
1033 (*setting)++;
1034}
1035
1036/* add pinconf setting with 2 parameters */
1037static void pcs_add_conf2(struct pcs_device *pcs, struct device_node *np,
1038 const char *name, enum pin_config_param param,
1039 struct pcs_conf_vals **conf, unsigned long **settings)
1040{
Haojian Zhuang7cba5b32013-03-13 16:01:26 +08001041 unsigned value[2], shift;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001042 int ret;
1043
1044 ret = of_property_read_u32_array(np, name, value, 2);
1045 if (ret)
1046 return;
1047 /* set value & mask */
1048 value[0] &= value[1];
Haojian Zhuang7cba5b32013-03-13 16:01:26 +08001049 shift = ffs(value[1]) - 1;
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001050 /* skip enable & disable */
1051 add_config(conf, param, value[0], 0, 0, value[1]);
Haojian Zhuang7cba5b32013-03-13 16:01:26 +08001052 add_setting(settings, param, value[0] >> shift);
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001053}
1054
1055/* add pinconf setting with 4 parameters */
1056static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np,
1057 const char *name, enum pin_config_param param,
1058 struct pcs_conf_vals **conf, unsigned long **settings)
1059{
1060 unsigned value[4];
1061 int ret;
1062
1063 /* value to set, enable, disable, mask */
1064 ret = of_property_read_u32_array(np, name, value, 4);
1065 if (ret)
1066 return;
1067 if (!value[3]) {
1068 dev_err(pcs->dev, "mask field of the property can't be 0\n");
1069 return;
1070 }
1071 value[0] &= value[3];
1072 value[1] &= value[3];
1073 value[2] &= value[3];
1074 ret = pcs_config_match(value[0], value[1], value[2]);
1075 if (ret < 0)
1076 dev_dbg(pcs->dev, "failed to match enable or disable bits\n");
1077 add_config(conf, param, value[0], value[1], value[2], value[3]);
1078 add_setting(settings, param, ret);
1079}
1080
1081static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np,
1082 struct pcs_function *func,
1083 struct pinctrl_map **map)
1084
1085{
1086 struct pinctrl_map *m = *map;
1087 int i = 0, nconfs = 0;
1088 unsigned long *settings = NULL, *s = NULL;
1089 struct pcs_conf_vals *conf = NULL;
1090 struct pcs_conf_type prop2[] = {
1091 { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, },
1092 { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, },
1093 { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, },
1094 };
1095 struct pcs_conf_type prop4[] = {
1096 { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, },
1097 { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, },
1098 { "pinctrl-single,input-schmitt-enable",
1099 PIN_CONFIG_INPUT_SCHMITT_ENABLE, },
1100 };
1101
1102 /* If pinconf isn't supported, don't parse properties in below. */
Tony Lindgren02e483f2013-10-02 21:39:39 -07001103 if (!PCS_HAS_PINCONF)
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001104 return 0;
1105
1106 /* cacluate how much properties are supported in current node */
1107 for (i = 0; i < ARRAY_SIZE(prop2); i++) {
1108 if (of_find_property(np, prop2[i].name, NULL))
1109 nconfs++;
1110 }
1111 for (i = 0; i < ARRAY_SIZE(prop4); i++) {
1112 if (of_find_property(np, prop4[i].name, NULL))
1113 nconfs++;
1114 }
1115 if (!nconfs)
1116 return 0;
1117
1118 func->conf = devm_kzalloc(pcs->dev,
1119 sizeof(struct pcs_conf_vals) * nconfs,
1120 GFP_KERNEL);
1121 if (!func->conf)
1122 return -ENOMEM;
1123 func->nconfs = nconfs;
1124 conf = &(func->conf[0]);
1125 m++;
1126 settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs,
1127 GFP_KERNEL);
1128 if (!settings)
1129 return -ENOMEM;
1130 s = &settings[0];
1131
1132 for (i = 0; i < ARRAY_SIZE(prop2); i++)
1133 pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param,
1134 &conf, &s);
1135 for (i = 0; i < ARRAY_SIZE(prop4); i++)
1136 pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param,
1137 &conf, &s);
1138 m->type = PIN_MAP_TYPE_CONFIGS_GROUP;
1139 m->data.configs.group_or_pin = np->name;
1140 m->data.configs.configs = settings;
1141 m->data.configs.num_configs = nconfs;
1142 return 0;
1143}
1144
1145static void pcs_free_pingroups(struct pcs_device *pcs);
1146
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001147/**
1148 * smux_parse_one_pinctrl_entry() - parses a device tree mux entry
1149 * @pcs: pinctrl driver instance
1150 * @np: device node of the mux entry
1151 * @map: map entry
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001152 * @num_maps: number of map
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001153 * @pgnames: pingroup names
1154 *
1155 * Note that this binding currently supports only sets of one register + value.
1156 *
1157 * Also note that this driver tries to avoid understanding pin and function
1158 * names because of the extra bloat they would cause especially in the case of
1159 * a large number of pins. This driver just sets what is specified for the board
1160 * in the .dts file. Further user space debugging tools can be developed to
1161 * decipher the pin and function names using debugfs.
1162 *
1163 * If you are concerned about the boot time, set up the static pins in
1164 * the bootloader, and only set up selected pins as device tree entries.
1165 */
1166static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs,
1167 struct device_node *np,
1168 struct pinctrl_map **map,
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001169 unsigned *num_maps,
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001170 const char **pgnames)
1171{
1172 struct pcs_func_vals *vals;
1173 const __be32 *mux;
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301174 int size, rows, *pins, index = 0, found = 0, res = -ENOMEM;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001175 struct pcs_function *function;
1176
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301177 mux = of_get_property(np, PCS_MUX_PINS_NAME, &size);
1178 if ((!mux) || (size < sizeof(*mux) * 2)) {
1179 dev_err(pcs->dev, "bad data for mux %s\n",
1180 np->name);
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001181 return -EINVAL;
1182 }
1183
1184 size /= sizeof(*mux); /* Number of elements in array */
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301185 rows = size / 2;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001186
1187 vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL);
1188 if (!vals)
1189 return -ENOMEM;
1190
1191 pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL);
1192 if (!pins)
1193 goto free_vals;
1194
1195 while (index < size) {
1196 unsigned offset, val;
1197 int pin;
1198
1199 offset = be32_to_cpup(mux + index++);
1200 val = be32_to_cpup(mux + index++);
1201 vals[found].reg = pcs->base + offset;
1202 vals[found].val = val;
1203
1204 pin = pcs_get_pin_by_offset(pcs, offset);
1205 if (pin < 0) {
1206 dev_err(pcs->dev,
1207 "could not add functions for %s %ux\n",
1208 np->name, offset);
1209 break;
1210 }
1211 pins[found++] = pin;
1212 }
1213
1214 pgnames[0] = np->name;
1215 function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
1216 if (!function)
1217 goto free_pins;
1218
1219 res = pcs_add_pingroup(pcs, np, np->name, pins, found);
1220 if (res < 0)
1221 goto free_function;
1222
1223 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
1224 (*map)->data.mux.group = np->name;
1225 (*map)->data.mux.function = np->name;
1226
Tony Lindgren02e483f2013-10-02 21:39:39 -07001227 if (PCS_HAS_PINCONF) {
Wei Yongjun18442e62013-05-07 20:06:19 +08001228 res = pcs_parse_pinconf(pcs, np, function, map);
1229 if (res)
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001230 goto free_pingroups;
1231 *num_maps = 2;
1232 } else {
1233 *num_maps = 1;
1234 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001235 return 0;
1236
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001237free_pingroups:
1238 pcs_free_pingroups(pcs);
1239 *num_maps = 1;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001240free_function:
1241 pcs_remove_function(pcs, function);
1242
1243free_pins:
1244 devm_kfree(pcs->dev, pins);
1245
1246free_vals:
1247 devm_kfree(pcs->dev, vals);
1248
1249 return res;
1250}
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301251
1252#define PARAMS_FOR_BITS_PER_MUX 3
1253
1254static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs,
1255 struct device_node *np,
1256 struct pinctrl_map **map,
1257 unsigned *num_maps,
1258 const char **pgnames)
1259{
1260 struct pcs_func_vals *vals;
1261 const __be32 *mux;
1262 int size, rows, *pins, index = 0, found = 0, res = -ENOMEM;
1263 int npins_in_row;
1264 struct pcs_function *function;
1265
1266 mux = of_get_property(np, PCS_MUX_BITS_NAME, &size);
1267
1268 if (!mux) {
1269 dev_err(pcs->dev, "no valid property for %s\n", np->name);
1270 return -EINVAL;
1271 }
1272
1273 if (size < (sizeof(*mux) * PARAMS_FOR_BITS_PER_MUX)) {
1274 dev_err(pcs->dev, "bad data for %s\n", np->name);
1275 return -EINVAL;
1276 }
1277
1278 /* Number of elements in array */
1279 size /= sizeof(*mux);
1280
1281 rows = size / PARAMS_FOR_BITS_PER_MUX;
1282 npins_in_row = pcs->width / pcs->bits_per_pin;
1283
1284 vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows * npins_in_row,
1285 GFP_KERNEL);
1286 if (!vals)
1287 return -ENOMEM;
1288
1289 pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows * npins_in_row,
1290 GFP_KERNEL);
1291 if (!pins)
1292 goto free_vals;
1293
1294 while (index < size) {
1295 unsigned offset, val;
1296 unsigned mask, bit_pos, val_pos, mask_pos, submask;
1297 unsigned pin_num_from_lsb;
1298 int pin;
1299
1300 offset = be32_to_cpup(mux + index++);
1301 val = be32_to_cpup(mux + index++);
1302 mask = be32_to_cpup(mux + index++);
1303
1304 /* Parse pins in each row from LSB */
1305 while (mask) {
1306 bit_pos = ffs(mask);
1307 pin_num_from_lsb = bit_pos / pcs->bits_per_pin;
1308 mask_pos = ((pcs->fmask) << (bit_pos - 1));
1309 val_pos = val & mask_pos;
1310 submask = mask & mask_pos;
1311 mask &= ~mask_pos;
1312
1313 if (submask != mask_pos) {
1314 dev_warn(pcs->dev,
1315 "Invalid submask 0x%x for %s at 0x%x\n",
1316 submask, np->name, offset);
1317 continue;
1318 }
1319
1320 vals[found].mask = submask;
1321 vals[found].reg = pcs->base + offset;
1322 vals[found].val = val_pos;
1323
1324 pin = pcs_get_pin_by_offset(pcs, offset);
1325 if (pin < 0) {
1326 dev_err(pcs->dev,
1327 "could not add functions for %s %ux\n",
1328 np->name, offset);
1329 break;
1330 }
1331 pins[found++] = pin + pin_num_from_lsb;
1332 }
1333 }
1334
1335 pgnames[0] = np->name;
1336 function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1);
1337 if (!function)
1338 goto free_pins;
1339
1340 res = pcs_add_pingroup(pcs, np, np->name, pins, found);
1341 if (res < 0)
1342 goto free_function;
1343
1344 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
1345 (*map)->data.mux.group = np->name;
1346 (*map)->data.mux.function = np->name;
1347
Tony Lindgren02e483f2013-10-02 21:39:39 -07001348 if (PCS_HAS_PINCONF) {
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301349 dev_err(pcs->dev, "pinconf not supported\n");
1350 goto free_pingroups;
1351 }
1352
1353 *num_maps = 1;
1354 return 0;
1355
1356free_pingroups:
1357 pcs_free_pingroups(pcs);
1358 *num_maps = 1;
1359free_function:
1360 pcs_remove_function(pcs, function);
1361
1362free_pins:
1363 devm_kfree(pcs->dev, pins);
1364
1365free_vals:
1366 devm_kfree(pcs->dev, vals);
1367
1368 return res;
1369}
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001370/**
1371 * pcs_dt_node_to_map() - allocates and parses pinctrl maps
1372 * @pctldev: pinctrl instance
1373 * @np_config: device tree pinmux entry
1374 * @map: array of map entries
1375 * @num_maps: number of maps
1376 */
1377static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev,
1378 struct device_node *np_config,
1379 struct pinctrl_map **map, unsigned *num_maps)
1380{
1381 struct pcs_device *pcs;
1382 const char **pgnames;
1383 int ret;
1384
1385 pcs = pinctrl_dev_get_drvdata(pctldev);
1386
Haojian Zhuang9dddb4d2013-02-17 19:42:55 +08001387 /* create 2 maps. One is for pinmux, and the other is for pinconf. */
1388 *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL);
Sachin Kamat00e79d12012-11-20 16:34:39 +05301389 if (!*map)
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001390 return -ENOMEM;
1391
1392 *num_maps = 0;
1393
1394 pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL);
1395 if (!pgnames) {
1396 ret = -ENOMEM;
1397 goto free_map;
1398 }
1399
Manjunathappa, Prakash4e7e8012013-05-21 19:38:00 +05301400 if (pcs->bits_per_mux) {
1401 ret = pcs_parse_bits_in_pinctrl_entry(pcs, np_config, map,
1402 num_maps, pgnames);
1403 if (ret < 0) {
1404 dev_err(pcs->dev, "no pins entries for %s\n",
1405 np_config->name);
1406 goto free_pgnames;
1407 }
1408 } else {
1409 ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map,
1410 num_maps, pgnames);
1411 if (ret < 0) {
1412 dev_err(pcs->dev, "no pins entries for %s\n",
1413 np_config->name);
1414 goto free_pgnames;
1415 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001416 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001417
1418 return 0;
1419
1420free_pgnames:
1421 devm_kfree(pcs->dev, pgnames);
1422free_map:
1423 devm_kfree(pcs->dev, *map);
1424
1425 return ret;
1426}
1427
1428/**
1429 * pcs_free_funcs() - free memory used by functions
1430 * @pcs: pcs driver instance
1431 */
1432static void pcs_free_funcs(struct pcs_device *pcs)
1433{
1434 struct list_head *pos, *tmp;
1435 int i;
1436
1437 mutex_lock(&pcs->mutex);
1438 for (i = 0; i < pcs->nfuncs; i++) {
1439 struct pcs_function *func;
1440
1441 func = radix_tree_lookup(&pcs->ftree, i);
1442 if (!func)
1443 continue;
1444 radix_tree_delete(&pcs->ftree, i);
1445 }
1446 list_for_each_safe(pos, tmp, &pcs->functions) {
1447 struct pcs_function *function;
1448
1449 function = list_entry(pos, struct pcs_function, node);
1450 list_del(&function->node);
1451 }
1452 mutex_unlock(&pcs->mutex);
1453}
1454
1455/**
1456 * pcs_free_pingroups() - free memory used by pingroups
1457 * @pcs: pcs driver instance
1458 */
1459static void pcs_free_pingroups(struct pcs_device *pcs)
1460{
1461 struct list_head *pos, *tmp;
1462 int i;
1463
1464 mutex_lock(&pcs->mutex);
1465 for (i = 0; i < pcs->ngroups; i++) {
1466 struct pcs_pingroup *pingroup;
1467
1468 pingroup = radix_tree_lookup(&pcs->pgtree, i);
1469 if (!pingroup)
1470 continue;
1471 radix_tree_delete(&pcs->pgtree, i);
1472 }
1473 list_for_each_safe(pos, tmp, &pcs->pingroups) {
1474 struct pcs_pingroup *pingroup;
1475
1476 pingroup = list_entry(pos, struct pcs_pingroup, node);
1477 list_del(&pingroup->node);
1478 }
1479 mutex_unlock(&pcs->mutex);
1480}
1481
1482/**
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001483 * pcs_irq_free() - free interrupt
1484 * @pcs: pcs driver instance
1485 */
1486static void pcs_irq_free(struct pcs_device *pcs)
1487{
1488 struct pcs_soc_data *pcs_soc = &pcs->socdata;
1489
1490 if (pcs_soc->irq < 0)
1491 return;
1492
1493 if (pcs->domain)
1494 irq_domain_remove(pcs->domain);
1495
1496 if (PCS_QUIRK_HAS_SHARED_IRQ)
1497 free_irq(pcs_soc->irq, pcs_soc);
1498 else
1499 irq_set_chained_handler(pcs_soc->irq, NULL);
1500}
1501
1502/**
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001503 * pcs_free_resources() - free memory used by this driver
1504 * @pcs: pcs driver instance
1505 */
1506static void pcs_free_resources(struct pcs_device *pcs)
1507{
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001508 pcs_irq_free(pcs);
1509
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001510 if (pcs->pctl)
1511 pinctrl_unregister(pcs->pctl);
1512
1513 pcs_free_funcs(pcs);
1514 pcs_free_pingroups(pcs);
1515}
1516
1517#define PCS_GET_PROP_U32(name, reg, err) \
1518 do { \
1519 ret = of_property_read_u32(np, name, reg); \
1520 if (ret) { \
1521 dev_err(pcs->dev, err); \
1522 return ret; \
1523 } \
1524 } while (0);
1525
1526static struct of_device_id pcs_of_match[];
1527
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001528static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs)
1529{
1530 const char *propname = "pinctrl-single,gpio-range";
1531 const char *cellname = "#pinctrl-single,gpio-range-cells";
1532 struct of_phandle_args gpiospec;
1533 struct pcs_gpiofunc_range *range;
1534 int ret, i;
1535
1536 for (i = 0; ; i++) {
1537 ret = of_parse_phandle_with_args(node, propname, cellname,
1538 i, &gpiospec);
1539 /* Do not treat it as error. Only treat it as end condition. */
1540 if (ret) {
1541 ret = 0;
1542 break;
1543 }
1544 range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL);
1545 if (!range) {
1546 ret = -ENOMEM;
1547 break;
1548 }
1549 range->offset = gpiospec.args[0];
1550 range->npins = gpiospec.args[1];
1551 range->gpiofunc = gpiospec.args[2];
1552 mutex_lock(&pcs->mutex);
1553 list_add_tail(&range->node, &pcs->gpiofuncs);
1554 mutex_unlock(&pcs->mutex);
1555 }
1556 return ret;
1557}
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001558/**
1559 * @reg: virtual address of interrupt register
1560 * @hwirq: hardware irq number
1561 * @irq: virtual irq number
1562 * @node: list node
1563 */
1564struct pcs_interrupt {
1565 void __iomem *reg;
1566 irq_hw_number_t hwirq;
1567 unsigned int irq;
1568 struct list_head node;
1569};
1570
1571/**
1572 * pcs_irq_set() - enables or disables an interrupt
1573 *
1574 * Note that this currently assumes one interrupt per pinctrl
1575 * register that is typically used for wake-up events.
1576 */
1577static inline void pcs_irq_set(struct pcs_soc_data *pcs_soc,
1578 int irq, const bool enable)
1579{
1580 struct pcs_device *pcs;
1581 struct list_head *pos;
1582 unsigned mask;
1583
1584 pcs = container_of(pcs_soc, struct pcs_device, socdata);
1585 list_for_each(pos, &pcs->irqs) {
1586 struct pcs_interrupt *pcswi;
1587 unsigned soc_mask;
1588
1589 pcswi = list_entry(pos, struct pcs_interrupt, node);
1590 if (irq != pcswi->irq)
1591 continue;
1592
1593 soc_mask = pcs_soc->irq_enable_mask;
1594 raw_spin_lock(&pcs->lock);
1595 mask = pcs->read(pcswi->reg);
1596 if (enable)
1597 mask |= soc_mask;
1598 else
1599 mask &= ~soc_mask;
1600 pcs->write(mask, pcswi->reg);
1601 raw_spin_unlock(&pcs->lock);
1602 }
1603}
1604
1605/**
1606 * pcs_irq_mask() - mask pinctrl interrupt
1607 * @d: interrupt data
1608 */
1609static void pcs_irq_mask(struct irq_data *d)
1610{
1611 struct pcs_soc_data *pcs_soc = irq_data_get_irq_chip_data(d);
1612
1613 pcs_irq_set(pcs_soc, d->irq, false);
1614}
1615
1616/**
1617 * pcs_irq_unmask() - unmask pinctrl interrupt
1618 * @d: interrupt data
1619 */
1620static void pcs_irq_unmask(struct irq_data *d)
1621{
1622 struct pcs_soc_data *pcs_soc = irq_data_get_irq_chip_data(d);
1623
1624 pcs_irq_set(pcs_soc, d->irq, true);
1625}
1626
1627/**
1628 * pcs_irq_set_wake() - toggle the suspend and resume wake up
1629 * @d: interrupt data
1630 * @state: wake-up state
1631 *
1632 * Note that this should be called only for suspend and resume.
1633 * For runtime PM, the wake-up events should be enabled by default.
1634 */
1635static int pcs_irq_set_wake(struct irq_data *d, unsigned int state)
1636{
1637 if (state)
1638 pcs_irq_unmask(d);
1639 else
1640 pcs_irq_mask(d);
1641
1642 return 0;
1643}
1644
1645/**
1646 * pcs_irq_handle() - common interrupt handler
1647 * @pcs_irq: interrupt data
1648 *
1649 * Note that this currently assumes we have one interrupt bit per
1650 * mux register. This interrupt is typically used for wake-up events.
1651 * For more complex interrupts different handlers can be specified.
1652 */
1653static int pcs_irq_handle(struct pcs_soc_data *pcs_soc)
1654{
1655 struct pcs_device *pcs;
1656 struct list_head *pos;
1657 int count = 0;
1658
1659 pcs = container_of(pcs_soc, struct pcs_device, socdata);
1660 list_for_each(pos, &pcs->irqs) {
1661 struct pcs_interrupt *pcswi;
1662 unsigned mask;
1663
1664 pcswi = list_entry(pos, struct pcs_interrupt, node);
1665 raw_spin_lock(&pcs->lock);
1666 mask = pcs->read(pcswi->reg);
1667 raw_spin_unlock(&pcs->lock);
1668 if (mask & pcs_soc->irq_status_mask) {
1669 generic_handle_irq(irq_find_mapping(pcs->domain,
1670 pcswi->hwirq));
1671 count++;
1672 }
1673 }
1674
1675 return count;
1676}
1677
1678/**
1679 * pcs_irq_handler() - handler for the shared interrupt case
1680 * @irq: interrupt
1681 * @d: data
1682 *
1683 * Use this for cases where multiple instances of
1684 * pinctrl-single share a single interrupt like on omaps.
1685 */
1686static irqreturn_t pcs_irq_handler(int irq, void *d)
1687{
1688 struct pcs_soc_data *pcs_soc = d;
1689
1690 return pcs_irq_handle(pcs_soc) ? IRQ_HANDLED : IRQ_NONE;
1691}
1692
1693/**
1694 * pcs_irq_handle() - handler for the dedicated chained interrupt case
1695 * @irq: interrupt
1696 * @desc: interrupt descriptor
1697 *
1698 * Use this if you have a separate interrupt for each
1699 * pinctrl-single instance.
1700 */
1701static void pcs_irq_chain_handler(unsigned int irq, struct irq_desc *desc)
1702{
1703 struct pcs_soc_data *pcs_soc = irq_desc_get_handler_data(desc);
1704 struct irq_chip *chip;
1705 int res;
1706
1707 chip = irq_get_chip(irq);
1708 chained_irq_enter(chip, desc);
1709 res = pcs_irq_handle(pcs_soc);
1710 /* REVISIT: export and add handle_bad_irq(irq, desc)? */
1711 chained_irq_exit(chip, desc);
1712
1713 return;
1714}
1715
1716static int pcs_irqdomain_map(struct irq_domain *d, unsigned int irq,
1717 irq_hw_number_t hwirq)
1718{
1719 struct pcs_soc_data *pcs_soc = d->host_data;
1720 struct pcs_device *pcs;
1721 struct pcs_interrupt *pcswi;
1722
1723 pcs = container_of(pcs_soc, struct pcs_device, socdata);
1724 pcswi = devm_kzalloc(pcs->dev, sizeof(*pcswi), GFP_KERNEL);
1725 if (!pcswi)
1726 return -ENOMEM;
1727
1728 pcswi->reg = pcs->base + hwirq;
1729 pcswi->hwirq = hwirq;
1730 pcswi->irq = irq;
1731
1732 mutex_lock(&pcs->mutex);
1733 list_add_tail(&pcswi->node, &pcs->irqs);
1734 mutex_unlock(&pcs->mutex);
1735
1736 irq_set_chip_data(irq, pcs_soc);
1737 irq_set_chip_and_handler(irq, &pcs->chip,
1738 handle_level_irq);
1739 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
1740
1741 return 0;
1742}
1743
1744static struct irq_domain_ops pcs_irqdomain_ops = {
1745 .map = pcs_irqdomain_map,
1746 .xlate = irq_domain_xlate_onecell,
1747};
1748
1749/**
1750 * pcs_irq_init_chained_handler() - set up a chained interrupt handler
1751 * @pcs: pcs driver instance
1752 * @np: device node pointer
1753 */
1754static int pcs_irq_init_chained_handler(struct pcs_device *pcs,
1755 struct device_node *np)
1756{
1757 struct pcs_soc_data *pcs_soc = &pcs->socdata;
1758 const char *name = "pinctrl";
1759 int num_irqs;
1760
1761 if (!pcs_soc->irq_enable_mask ||
1762 !pcs_soc->irq_status_mask) {
1763 pcs_soc->irq = -1;
1764 return -EINVAL;
1765 }
1766
1767 INIT_LIST_HEAD(&pcs->irqs);
1768 pcs->chip.name = name;
1769 pcs->chip.irq_ack = pcs_irq_mask;
1770 pcs->chip.irq_mask = pcs_irq_mask;
1771 pcs->chip.irq_unmask = pcs_irq_unmask;
1772 pcs->chip.irq_set_wake = pcs_irq_set_wake;
1773
1774 if (PCS_QUIRK_HAS_SHARED_IRQ) {
1775 int res;
1776
1777 res = request_irq(pcs_soc->irq, pcs_irq_handler,
1778 IRQF_SHARED | IRQF_NO_SUSPEND,
1779 name, pcs_soc);
1780 if (res) {
1781 pcs_soc->irq = -1;
1782 return res;
1783 }
1784 } else {
1785 irq_set_handler_data(pcs_soc->irq, pcs_soc);
1786 irq_set_chained_handler(pcs_soc->irq,
1787 pcs_irq_chain_handler);
1788 }
1789
1790 /*
1791 * We can use the register offset as the hardirq
1792 * number as irq_domain_add_simple maps them lazily.
1793 * This way we can easily support more than one
1794 * interrupt per function if needed.
1795 */
1796 num_irqs = pcs->size;
1797
1798 pcs->domain = irq_domain_add_simple(np, num_irqs, 0,
1799 &pcs_irqdomain_ops,
1800 pcs_soc);
1801 if (!pcs->domain) {
1802 irq_set_chained_handler(pcs_soc->irq, NULL);
1803 return -EINVAL;
1804 }
1805
1806 return 0;
1807}
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001808
Jean-Francois Moine8cb440a2013-07-15 10:14:26 +02001809#ifdef CONFIG_PM
Hebbar Gururaja0f9bc4b2013-05-31 15:43:01 +05301810static int pinctrl_single_suspend(struct platform_device *pdev,
1811 pm_message_t state)
1812{
1813 struct pcs_device *pcs;
1814
1815 pcs = platform_get_drvdata(pdev);
1816 if (!pcs)
1817 return -EINVAL;
1818
1819 return pinctrl_force_sleep(pcs->pctl);
1820}
1821
1822static int pinctrl_single_resume(struct platform_device *pdev)
1823{
1824 struct pcs_device *pcs;
1825
1826 pcs = platform_get_drvdata(pdev);
1827 if (!pcs)
1828 return -EINVAL;
1829
1830 return pinctrl_force_default(pcs->pctl);
1831}
Jean-Francois Moine8cb440a2013-07-15 10:14:26 +02001832#endif
Hebbar Gururaja0f9bc4b2013-05-31 15:43:01 +05301833
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001834static int pcs_probe(struct platform_device *pdev)
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001835{
1836 struct device_node *np = pdev->dev.of_node;
1837 const struct of_device_id *match;
1838 struct resource *res;
1839 struct pcs_device *pcs;
Tony Lindgren02e483f2013-10-02 21:39:39 -07001840 const struct pcs_soc_data *soc;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001841 int ret;
1842
1843 match = of_match_device(pcs_of_match, &pdev->dev);
1844 if (!match)
1845 return -EINVAL;
1846
1847 pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL);
1848 if (!pcs) {
1849 dev_err(&pdev->dev, "could not allocate\n");
1850 return -ENOMEM;
1851 }
1852 pcs->dev = &pdev->dev;
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001853 raw_spin_lock_init(&pcs->lock);
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001854 mutex_init(&pcs->mutex);
1855 INIT_LIST_HEAD(&pcs->pingroups);
1856 INIT_LIST_HEAD(&pcs->functions);
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001857 INIT_LIST_HEAD(&pcs->gpiofuncs);
Tony Lindgren02e483f2013-10-02 21:39:39 -07001858 soc = match->data;
1859 pcs->flags = soc->flags;
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001860 memcpy(&pcs->socdata, soc, sizeof(*soc));
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001861
1862 PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width,
1863 "register width not specified\n");
1864
Haojian Zhuang477ac772013-02-17 19:42:54 +08001865 ret = of_property_read_u32(np, "pinctrl-single,function-mask",
1866 &pcs->fmask);
1867 if (!ret) {
1868 pcs->fshift = ffs(pcs->fmask) - 1;
1869 pcs->fmax = pcs->fmask >> pcs->fshift;
1870 } else {
1871 /* If mask property doesn't exist, function mux is invalid. */
1872 pcs->fmask = 0;
1873 pcs->fshift = 0;
1874 pcs->fmax = 0;
1875 }
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001876
1877 ret = of_property_read_u32(np, "pinctrl-single,function-off",
1878 &pcs->foff);
1879 if (ret)
1880 pcs->foff = PCS_OFF_DISABLED;
1881
Peter Ujfalusi9e605cb2012-09-11 11:54:24 +03001882 pcs->bits_per_mux = of_property_read_bool(np,
1883 "pinctrl-single,bit-per-mux");
1884
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001885 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1886 if (!res) {
1887 dev_err(pcs->dev, "could not get resource\n");
1888 return -ENODEV;
1889 }
1890
1891 pcs->res = devm_request_mem_region(pcs->dev, res->start,
1892 resource_size(res), DRIVER_NAME);
1893 if (!pcs->res) {
1894 dev_err(pcs->dev, "could not get mem_region\n");
1895 return -EBUSY;
1896 }
1897
1898 pcs->size = resource_size(pcs->res);
1899 pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size);
1900 if (!pcs->base) {
1901 dev_err(pcs->dev, "could not ioremap\n");
1902 return -ENODEV;
1903 }
1904
1905 INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL);
1906 INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL);
1907 platform_set_drvdata(pdev, pcs);
1908
1909 switch (pcs->width) {
1910 case 8:
1911 pcs->read = pcs_readb;
1912 pcs->write = pcs_writeb;
1913 break;
1914 case 16:
1915 pcs->read = pcs_readw;
1916 pcs->write = pcs_writew;
1917 break;
1918 case 32:
1919 pcs->read = pcs_readl;
1920 pcs->write = pcs_writel;
1921 break;
1922 default:
1923 break;
1924 }
1925
1926 pcs->desc.name = DRIVER_NAME;
1927 pcs->desc.pctlops = &pcs_pinctrl_ops;
1928 pcs->desc.pmxops = &pcs_pinmux_ops;
Tony Lindgren02e483f2013-10-02 21:39:39 -07001929 if (PCS_HAS_PINCONF)
Axel Lina7bbdd72013-03-04 13:47:39 +08001930 pcs->desc.confops = &pcs_pinconf_ops;
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001931 pcs->desc.owner = THIS_MODULE;
1932
1933 ret = pcs_allocate_pin_table(pcs);
1934 if (ret < 0)
1935 goto free;
1936
1937 pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs);
1938 if (!pcs->pctl) {
1939 dev_err(pcs->dev, "could not register single pinctrl driver\n");
1940 ret = -EINVAL;
1941 goto free;
1942 }
1943
Haojian Zhuanga1a277e2013-02-17 19:42:52 +08001944 ret = pcs_add_gpio_func(np, pcs);
1945 if (ret < 0)
1946 goto free;
1947
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001948 pcs->socdata.irq = irq_of_parse_and_map(np, 0);
1949 if (pcs->socdata.irq)
1950 pcs->flags |= PCS_FEAT_IRQ;
1951
1952 if (PCS_HAS_IRQ) {
1953 ret = pcs_irq_init_chained_handler(pcs, np);
1954 if (ret < 0)
1955 dev_warn(pcs->dev, "initialized with no interrupts\n");
1956 }
1957
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001958 dev_info(pcs->dev, "%i pins at pa %p size %u\n",
1959 pcs->desc.npins, pcs->base, pcs->size);
1960
1961 return 0;
1962
1963free:
1964 pcs_free_resources(pcs);
1965
1966 return ret;
1967}
1968
Bill Pembertonf90f54b2012-11-19 13:26:06 -05001969static int pcs_remove(struct platform_device *pdev)
Tony Lindgren8b8b0912012-07-10 02:05:46 -07001970{
1971 struct pcs_device *pcs = platform_get_drvdata(pdev);
1972
1973 if (!pcs)
1974 return 0;
1975
1976 pcs_free_resources(pcs);
1977
1978 return 0;
1979}
1980
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001981static const struct pcs_soc_data pinctrl_single_omap_wkup = {
1982 .flags = PCS_QUIRK_SHARED_IRQ,
1983 .irq_enable_mask = (1 << 14), /* OMAP_WAKEUP_EN */
1984 .irq_status_mask = (1 << 15), /* OMAP_WAKEUP_EVENT */
1985};
1986
Tony Lindgren02e483f2013-10-02 21:39:39 -07001987static const struct pcs_soc_data pinctrl_single = {
1988};
1989
1990static const struct pcs_soc_data pinconf_single = {
1991 .flags = PCS_FEAT_PINCONF,
1992};
1993
Bill Pemberton99688ed2012-11-19 13:24:27 -05001994static struct of_device_id pcs_of_match[] = {
Tony Lindgren3e6cee12013-10-02 21:39:40 -07001995 { .compatible = "ti,omap3-padconf", .data = &pinctrl_single_omap_wkup },
1996 { .compatible = "ti,omap4-padconf", .data = &pinctrl_single_omap_wkup },
1997 { .compatible = "ti,omap5-padconf", .data = &pinctrl_single_omap_wkup },
Tony Lindgren02e483f2013-10-02 21:39:39 -07001998 { .compatible = "pinctrl-single", .data = &pinctrl_single },
1999 { .compatible = "pinconf-single", .data = &pinconf_single },
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002000 { },
2001};
2002MODULE_DEVICE_TABLE(of, pcs_of_match);
2003
2004static struct platform_driver pcs_driver = {
2005 .probe = pcs_probe,
Bill Pemberton2a36f082012-11-19 13:21:27 -05002006 .remove = pcs_remove,
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002007 .driver = {
2008 .owner = THIS_MODULE,
2009 .name = DRIVER_NAME,
2010 .of_match_table = pcs_of_match,
2011 },
Hebbar Gururaja0f9bc4b2013-05-31 15:43:01 +05302012#ifdef CONFIG_PM
2013 .suspend = pinctrl_single_suspend,
2014 .resume = pinctrl_single_resume,
2015#endif
Tony Lindgren8b8b0912012-07-10 02:05:46 -07002016};
2017
2018module_platform_driver(pcs_driver);
2019
2020MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
2021MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver");
2022MODULE_LICENSE("GPL v2");