Commit | Line | Data |
---|---|---|
8b8b091b TL |
1 | /* |
2 | * Generic device tree based pinctrl driver for one register per pin | |
3 | * type pinmux controllers | |
4 | * | |
5 | * Copyright (C) 2012 Texas Instruments, Inc. | |
6 | * | |
7 | * This file is licensed under the terms of the GNU General Public | |
8 | * License version 2. This program is licensed "as is" without any | |
9 | * warranty of any kind, whether express or implied. | |
10 | */ | |
11 | ||
12 | #include <linux/init.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/io.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/err.h> | |
17 | #include <linux/list.h> | |
18 | ||
19 | #include <linux/of.h> | |
20 | #include <linux/of_device.h> | |
21 | #include <linux/of_address.h> | |
22 | ||
23 | #include <linux/pinctrl/pinctrl.h> | |
24 | #include <linux/pinctrl/pinmux.h> | |
9dddb4df | 25 | #include <linux/pinctrl/pinconf-generic.h> |
8b8b091b TL |
26 | |
27 | #include "core.h" | |
9dddb4df | 28 | #include "pinconf.h" |
8b8b091b TL |
29 | |
30 | #define DRIVER_NAME "pinctrl-single" | |
9e605cb6 PU |
31 | #define PCS_MUX_PINS_NAME "pinctrl-single,pins" |
32 | #define PCS_MUX_BITS_NAME "pinctrl-single,bits" | |
8b8b091b TL |
33 | #define PCS_REG_NAME_LEN ((sizeof(unsigned long) * 2) + 1) |
34 | #define PCS_OFF_DISABLED ~0U | |
35 | ||
36 | /** | |
37 | * struct pcs_pingroup - pingroups for a function | |
38 | * @np: pingroup device node pointer | |
39 | * @name: pingroup name | |
40 | * @gpins: array of the pins in the group | |
41 | * @ngpins: number of pins in the group | |
42 | * @node: list node | |
43 | */ | |
44 | struct pcs_pingroup { | |
45 | struct device_node *np; | |
46 | const char *name; | |
47 | int *gpins; | |
48 | int ngpins; | |
49 | struct list_head node; | |
50 | }; | |
51 | ||
52 | /** | |
53 | * struct pcs_func_vals - mux function register offset and value pair | |
54 | * @reg: register virtual address | |
55 | * @val: register value | |
56 | */ | |
57 | struct pcs_func_vals { | |
58 | void __iomem *reg; | |
59 | unsigned val; | |
9e605cb6 | 60 | unsigned mask; |
8b8b091b TL |
61 | }; |
62 | ||
9dddb4df HZ |
63 | /** |
64 | * struct pcs_conf_vals - pinconf parameter, pinconf register offset | |
65 | * and value, enable, disable, mask | |
66 | * @param: config parameter | |
67 | * @val: user input bits in the pinconf register | |
68 | * @enable: enable bits in the pinconf register | |
69 | * @disable: disable bits in the pinconf register | |
70 | * @mask: mask bits in the register value | |
71 | */ | |
72 | struct pcs_conf_vals { | |
73 | enum pin_config_param param; | |
74 | unsigned val; | |
75 | unsigned enable; | |
76 | unsigned disable; | |
77 | unsigned mask; | |
78 | }; | |
79 | ||
80 | /** | |
81 | * struct pcs_conf_type - pinconf property name, pinconf param pair | |
82 | * @name: property name in DTS file | |
83 | * @param: config parameter | |
84 | */ | |
85 | struct pcs_conf_type { | |
86 | const char *name; | |
87 | enum pin_config_param param; | |
88 | }; | |
89 | ||
8b8b091b TL |
90 | /** |
91 | * struct pcs_function - pinctrl function | |
92 | * @name: pinctrl function name | |
93 | * @vals: register and vals array | |
94 | * @nvals: number of entries in vals array | |
95 | * @pgnames: array of pingroup names the function uses | |
96 | * @npgnames: number of pingroup names the function uses | |
97 | * @node: list node | |
98 | */ | |
99 | struct pcs_function { | |
100 | const char *name; | |
101 | struct pcs_func_vals *vals; | |
102 | unsigned nvals; | |
103 | const char **pgnames; | |
104 | int npgnames; | |
9dddb4df HZ |
105 | struct pcs_conf_vals *conf; |
106 | int nconfs; | |
8b8b091b TL |
107 | struct list_head node; |
108 | }; | |
109 | ||
a1a277eb HZ |
110 | /** |
111 | * struct pcs_gpiofunc_range - pin ranges with same mux value of gpio function | |
112 | * @offset: offset base of pins | |
113 | * @npins: number pins with the same mux value of gpio function | |
114 | * @gpiofunc: mux value of gpio function | |
115 | * @node: list node | |
116 | */ | |
117 | struct pcs_gpiofunc_range { | |
118 | unsigned offset; | |
119 | unsigned npins; | |
120 | unsigned gpiofunc; | |
121 | struct list_head node; | |
122 | }; | |
123 | ||
8b8b091b TL |
124 | /** |
125 | * struct pcs_data - wrapper for data needed by pinctrl framework | |
126 | * @pa: pindesc array | |
127 | * @cur: index to current element | |
128 | * | |
129 | * REVISIT: We should be able to drop this eventually by adding | |
130 | * support for registering pins individually in the pinctrl | |
131 | * framework for those drivers that don't need a static array. | |
132 | */ | |
133 | struct pcs_data { | |
134 | struct pinctrl_pin_desc *pa; | |
135 | int cur; | |
136 | }; | |
137 | ||
138 | /** | |
139 | * struct pcs_name - register name for a pin | |
140 | * @name: name of the pinctrl register | |
141 | * | |
142 | * REVISIT: We may want to make names optional in the pinctrl | |
143 | * framework as some drivers may not care about pin names to | |
144 | * avoid kernel bloat. The pin names can be deciphered by user | |
145 | * space tools using debugfs based on the register address and | |
146 | * SoC packaging information. | |
147 | */ | |
148 | struct pcs_name { | |
149 | char name[PCS_REG_NAME_LEN]; | |
150 | }; | |
151 | ||
152 | /** | |
153 | * struct pcs_device - pinctrl device instance | |
154 | * @res: resources | |
155 | * @base: virtual address of the controller | |
156 | * @size: size of the ioremapped area | |
157 | * @dev: device entry | |
158 | * @pctl: pin controller device | |
159 | * @mutex: mutex protecting the lists | |
160 | * @width: bits per mux register | |
161 | * @fmask: function register mask | |
162 | * @fshift: function register shift | |
163 | * @foff: value to turn mux off | |
164 | * @fmax: max number of functions in fmask | |
9dddb4df | 165 | * @is_pinconf: whether supports pinconf |
4e7e8017 | 166 | * @bits_per_pin:number of bits per pin |
8b8b091b TL |
167 | * @names: array of register names for pins |
168 | * @pins: physical pins on the SoC | |
169 | * @pgtree: pingroup index radix tree | |
170 | * @ftree: function index radix tree | |
171 | * @pingroups: list of pingroups | |
172 | * @functions: list of functions | |
a1a277eb | 173 | * @gpiofuncs: list of gpio functions |
8b8b091b TL |
174 | * @ngroups: number of pingroups |
175 | * @nfuncs: number of functions | |
176 | * @desc: pin controller descriptor | |
177 | * @read: register read function to use | |
178 | * @write: register write function to use | |
179 | */ | |
180 | struct pcs_device { | |
181 | struct resource *res; | |
182 | void __iomem *base; | |
183 | unsigned size; | |
184 | struct device *dev; | |
185 | struct pinctrl_dev *pctl; | |
186 | struct mutex mutex; | |
187 | unsigned width; | |
188 | unsigned fmask; | |
189 | unsigned fshift; | |
190 | unsigned foff; | |
191 | unsigned fmax; | |
9e605cb6 | 192 | bool bits_per_mux; |
9dddb4df | 193 | bool is_pinconf; |
4e7e8017 | 194 | unsigned bits_per_pin; |
8b8b091b TL |
195 | struct pcs_name *names; |
196 | struct pcs_data pins; | |
197 | struct radix_tree_root pgtree; | |
198 | struct radix_tree_root ftree; | |
199 | struct list_head pingroups; | |
200 | struct list_head functions; | |
a1a277eb | 201 | struct list_head gpiofuncs; |
8b8b091b TL |
202 | unsigned ngroups; |
203 | unsigned nfuncs; | |
204 | struct pinctrl_desc desc; | |
205 | unsigned (*read)(void __iomem *reg); | |
206 | void (*write)(unsigned val, void __iomem *reg); | |
207 | }; | |
208 | ||
9dddb4df HZ |
209 | static int pcs_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin, |
210 | unsigned long *config); | |
211 | static int pcs_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin, | |
212 | unsigned long config); | |
213 | ||
214 | static enum pin_config_param pcs_bias[] = { | |
215 | PIN_CONFIG_BIAS_PULL_DOWN, | |
216 | PIN_CONFIG_BIAS_PULL_UP, | |
217 | }; | |
218 | ||
8b8b091b TL |
219 | /* |
220 | * REVISIT: Reads and writes could eventually use regmap or something | |
221 | * generic. But at least on omaps, some mux registers are performance | |
222 | * critical as they may need to be remuxed every time before and after | |
223 | * idle. Adding tests for register access width for every read and | |
224 | * write like regmap is doing is not desired, and caching the registers | |
225 | * does not help in this case. | |
226 | */ | |
227 | ||
228 | static unsigned __maybe_unused pcs_readb(void __iomem *reg) | |
229 | { | |
230 | return readb(reg); | |
231 | } | |
232 | ||
233 | static unsigned __maybe_unused pcs_readw(void __iomem *reg) | |
234 | { | |
235 | return readw(reg); | |
236 | } | |
237 | ||
238 | static unsigned __maybe_unused pcs_readl(void __iomem *reg) | |
239 | { | |
240 | return readl(reg); | |
241 | } | |
242 | ||
243 | static void __maybe_unused pcs_writeb(unsigned val, void __iomem *reg) | |
244 | { | |
245 | writeb(val, reg); | |
246 | } | |
247 | ||
248 | static void __maybe_unused pcs_writew(unsigned val, void __iomem *reg) | |
249 | { | |
250 | writew(val, reg); | |
251 | } | |
252 | ||
253 | static void __maybe_unused pcs_writel(unsigned val, void __iomem *reg) | |
254 | { | |
255 | writel(val, reg); | |
256 | } | |
257 | ||
258 | static int pcs_get_groups_count(struct pinctrl_dev *pctldev) | |
259 | { | |
260 | struct pcs_device *pcs; | |
261 | ||
262 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
263 | ||
264 | return pcs->ngroups; | |
265 | } | |
266 | ||
267 | static const char *pcs_get_group_name(struct pinctrl_dev *pctldev, | |
268 | unsigned gselector) | |
269 | { | |
270 | struct pcs_device *pcs; | |
271 | struct pcs_pingroup *group; | |
272 | ||
273 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
274 | group = radix_tree_lookup(&pcs->pgtree, gselector); | |
275 | if (!group) { | |
276 | dev_err(pcs->dev, "%s could not find pingroup%i\n", | |
277 | __func__, gselector); | |
278 | return NULL; | |
279 | } | |
280 | ||
281 | return group->name; | |
282 | } | |
283 | ||
284 | static int pcs_get_group_pins(struct pinctrl_dev *pctldev, | |
285 | unsigned gselector, | |
286 | const unsigned **pins, | |
287 | unsigned *npins) | |
288 | { | |
289 | struct pcs_device *pcs; | |
290 | struct pcs_pingroup *group; | |
291 | ||
292 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
293 | group = radix_tree_lookup(&pcs->pgtree, gselector); | |
294 | if (!group) { | |
295 | dev_err(pcs->dev, "%s could not find pingroup%i\n", | |
296 | __func__, gselector); | |
297 | return -EINVAL; | |
298 | } | |
299 | ||
300 | *pins = group->gpins; | |
301 | *npins = group->ngpins; | |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
306 | static void pcs_pin_dbg_show(struct pinctrl_dev *pctldev, | |
307 | struct seq_file *s, | |
e7ed6718 | 308 | unsigned pin) |
8b8b091b | 309 | { |
7d66ce7f | 310 | struct pcs_device *pcs; |
e7ed6718 | 311 | unsigned val, mux_bytes; |
7d66ce7f MP |
312 | |
313 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
314 | ||
e7ed6718 HZ |
315 | mux_bytes = pcs->width / BITS_PER_BYTE; |
316 | val = pcs->read(pcs->base + pin * mux_bytes); | |
7d66ce7f MP |
317 | |
318 | seq_printf(s, "%08x %s " , val, DRIVER_NAME); | |
8b8b091b TL |
319 | } |
320 | ||
321 | static void pcs_dt_free_map(struct pinctrl_dev *pctldev, | |
322 | struct pinctrl_map *map, unsigned num_maps) | |
323 | { | |
324 | struct pcs_device *pcs; | |
325 | ||
326 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
327 | devm_kfree(pcs->dev, map); | |
328 | } | |
329 | ||
330 | static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev, | |
331 | struct device_node *np_config, | |
332 | struct pinctrl_map **map, unsigned *num_maps); | |
333 | ||
022ab148 | 334 | static const struct pinctrl_ops pcs_pinctrl_ops = { |
8b8b091b TL |
335 | .get_groups_count = pcs_get_groups_count, |
336 | .get_group_name = pcs_get_group_name, | |
337 | .get_group_pins = pcs_get_group_pins, | |
338 | .pin_dbg_show = pcs_pin_dbg_show, | |
339 | .dt_node_to_map = pcs_dt_node_to_map, | |
340 | .dt_free_map = pcs_dt_free_map, | |
341 | }; | |
342 | ||
343 | static int pcs_get_functions_count(struct pinctrl_dev *pctldev) | |
344 | { | |
345 | struct pcs_device *pcs; | |
346 | ||
347 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
348 | ||
349 | return pcs->nfuncs; | |
350 | } | |
351 | ||
352 | static const char *pcs_get_function_name(struct pinctrl_dev *pctldev, | |
353 | unsigned fselector) | |
354 | { | |
355 | struct pcs_device *pcs; | |
356 | struct pcs_function *func; | |
357 | ||
358 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
359 | func = radix_tree_lookup(&pcs->ftree, fselector); | |
360 | if (!func) { | |
361 | dev_err(pcs->dev, "%s could not find function%i\n", | |
362 | __func__, fselector); | |
363 | return NULL; | |
364 | } | |
365 | ||
366 | return func->name; | |
367 | } | |
368 | ||
369 | static int pcs_get_function_groups(struct pinctrl_dev *pctldev, | |
370 | unsigned fselector, | |
371 | const char * const **groups, | |
372 | unsigned * const ngroups) | |
373 | { | |
374 | struct pcs_device *pcs; | |
375 | struct pcs_function *func; | |
376 | ||
377 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
378 | func = radix_tree_lookup(&pcs->ftree, fselector); | |
379 | if (!func) { | |
380 | dev_err(pcs->dev, "%s could not find function%i\n", | |
381 | __func__, fselector); | |
382 | return -EINVAL; | |
383 | } | |
384 | *groups = func->pgnames; | |
385 | *ngroups = func->npgnames; | |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
9dddb4df HZ |
390 | static int pcs_get_function(struct pinctrl_dev *pctldev, unsigned pin, |
391 | struct pcs_function **func) | |
392 | { | |
393 | struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev); | |
394 | struct pin_desc *pdesc = pin_desc_get(pctldev, pin); | |
395 | const struct pinctrl_setting_mux *setting; | |
396 | unsigned fselector; | |
397 | ||
398 | /* If pin is not described in DTS & enabled, mux_setting is NULL. */ | |
399 | setting = pdesc->mux_setting; | |
400 | if (!setting) | |
401 | return -ENOTSUPP; | |
402 | fselector = setting->func; | |
403 | *func = radix_tree_lookup(&pcs->ftree, fselector); | |
404 | if (!(*func)) { | |
405 | dev_err(pcs->dev, "%s could not find function%i\n", | |
406 | __func__, fselector); | |
407 | return -ENOTSUPP; | |
408 | } | |
409 | return 0; | |
410 | } | |
411 | ||
8b8b091b TL |
412 | static int pcs_enable(struct pinctrl_dev *pctldev, unsigned fselector, |
413 | unsigned group) | |
414 | { | |
415 | struct pcs_device *pcs; | |
416 | struct pcs_function *func; | |
417 | int i; | |
418 | ||
419 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
477ac771 HZ |
420 | /* If function mask is null, needn't enable it. */ |
421 | if (!pcs->fmask) | |
422 | return 0; | |
8b8b091b TL |
423 | func = radix_tree_lookup(&pcs->ftree, fselector); |
424 | if (!func) | |
425 | return -EINVAL; | |
426 | ||
427 | dev_dbg(pcs->dev, "enabling %s function%i\n", | |
428 | func->name, fselector); | |
429 | ||
430 | for (i = 0; i < func->nvals; i++) { | |
431 | struct pcs_func_vals *vals; | |
9e605cb6 | 432 | unsigned val, mask; |
8b8b091b TL |
433 | |
434 | vals = &func->vals[i]; | |
435 | val = pcs->read(vals->reg); | |
4e7e8017 MP |
436 | |
437 | if (pcs->bits_per_mux) | |
438 | mask = vals->mask; | |
9e605cb6 | 439 | else |
4e7e8017 | 440 | mask = pcs->fmask; |
9e605cb6 PU |
441 | |
442 | val &= ~mask; | |
443 | val |= (vals->val & mask); | |
8b8b091b TL |
444 | pcs->write(val, vals->reg); |
445 | } | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
450 | static void pcs_disable(struct pinctrl_dev *pctldev, unsigned fselector, | |
451 | unsigned group) | |
452 | { | |
453 | struct pcs_device *pcs; | |
454 | struct pcs_function *func; | |
455 | int i; | |
456 | ||
457 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
477ac771 HZ |
458 | /* If function mask is null, needn't disable it. */ |
459 | if (!pcs->fmask) | |
460 | return; | |
461 | ||
8b8b091b TL |
462 | func = radix_tree_lookup(&pcs->ftree, fselector); |
463 | if (!func) { | |
464 | dev_err(pcs->dev, "%s could not find function%i\n", | |
465 | __func__, fselector); | |
466 | return; | |
467 | } | |
468 | ||
469 | /* | |
470 | * Ignore disable if function-off is not specified. Some hardware | |
471 | * does not have clearly defined disable function. For pin specific | |
472 | * off modes, you can use alternate named states as described in | |
473 | * pinctrl-bindings.txt. | |
474 | */ | |
475 | if (pcs->foff == PCS_OFF_DISABLED) { | |
476 | dev_dbg(pcs->dev, "ignoring disable for %s function%i\n", | |
477 | func->name, fselector); | |
478 | return; | |
479 | } | |
480 | ||
481 | dev_dbg(pcs->dev, "disabling function%i %s\n", | |
482 | fselector, func->name); | |
483 | ||
484 | for (i = 0; i < func->nvals; i++) { | |
485 | struct pcs_func_vals *vals; | |
486 | unsigned val; | |
487 | ||
488 | vals = &func->vals[i]; | |
489 | val = pcs->read(vals->reg); | |
490 | val &= ~pcs->fmask; | |
491 | val |= pcs->foff << pcs->fshift; | |
492 | pcs->write(val, vals->reg); | |
493 | } | |
494 | } | |
495 | ||
496 | static int pcs_request_gpio(struct pinctrl_dev *pctldev, | |
a1a277eb | 497 | struct pinctrl_gpio_range *range, unsigned pin) |
8b8b091b | 498 | { |
a1a277eb HZ |
499 | struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev); |
500 | struct pcs_gpiofunc_range *frange = NULL; | |
501 | struct list_head *pos, *tmp; | |
502 | int mux_bytes = 0; | |
503 | unsigned data; | |
504 | ||
477ac771 HZ |
505 | /* If function mask is null, return directly. */ |
506 | if (!pcs->fmask) | |
507 | return -ENOTSUPP; | |
508 | ||
a1a277eb HZ |
509 | list_for_each_safe(pos, tmp, &pcs->gpiofuncs) { |
510 | frange = list_entry(pos, struct pcs_gpiofunc_range, node); | |
511 | if (pin >= frange->offset + frange->npins | |
512 | || pin < frange->offset) | |
513 | continue; | |
514 | mux_bytes = pcs->width / BITS_PER_BYTE; | |
515 | data = pcs->read(pcs->base + pin * mux_bytes) & ~pcs->fmask; | |
516 | data |= frange->gpiofunc; | |
517 | pcs->write(data, pcs->base + pin * mux_bytes); | |
518 | break; | |
519 | } | |
520 | return 0; | |
8b8b091b TL |
521 | } |
522 | ||
022ab148 | 523 | static const struct pinmux_ops pcs_pinmux_ops = { |
8b8b091b TL |
524 | .get_functions_count = pcs_get_functions_count, |
525 | .get_function_name = pcs_get_function_name, | |
526 | .get_function_groups = pcs_get_function_groups, | |
527 | .enable = pcs_enable, | |
528 | .disable = pcs_disable, | |
529 | .gpio_request_enable = pcs_request_gpio, | |
530 | }; | |
531 | ||
9dddb4df HZ |
532 | /* Clear BIAS value */ |
533 | static void pcs_pinconf_clear_bias(struct pinctrl_dev *pctldev, unsigned pin) | |
534 | { | |
535 | unsigned long config; | |
536 | int i; | |
537 | for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) { | |
538 | config = pinconf_to_config_packed(pcs_bias[i], 0); | |
539 | pcs_pinconf_set(pctldev, pin, config); | |
540 | } | |
541 | } | |
542 | ||
543 | /* | |
544 | * Check whether PIN_CONFIG_BIAS_DISABLE is valid. | |
545 | * It's depend on that PULL_DOWN & PULL_UP configs are all invalid. | |
546 | */ | |
547 | static bool pcs_pinconf_bias_disable(struct pinctrl_dev *pctldev, unsigned pin) | |
548 | { | |
549 | unsigned long config; | |
550 | int i; | |
551 | ||
552 | for (i = 0; i < ARRAY_SIZE(pcs_bias); i++) { | |
553 | config = pinconf_to_config_packed(pcs_bias[i], 0); | |
554 | if (!pcs_pinconf_get(pctldev, pin, &config)) | |
555 | goto out; | |
556 | } | |
557 | return true; | |
558 | out: | |
559 | return false; | |
560 | } | |
561 | ||
8b8b091b TL |
562 | static int pcs_pinconf_get(struct pinctrl_dev *pctldev, |
563 | unsigned pin, unsigned long *config) | |
564 | { | |
9dddb4df HZ |
565 | struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev); |
566 | struct pcs_function *func; | |
567 | enum pin_config_param param; | |
568 | unsigned offset = 0, data = 0, i, j, ret; | |
569 | ||
570 | ret = pcs_get_function(pctldev, pin, &func); | |
571 | if (ret) | |
572 | return ret; | |
573 | ||
574 | for (i = 0; i < func->nconfs; i++) { | |
575 | param = pinconf_to_config_param(*config); | |
576 | if (param == PIN_CONFIG_BIAS_DISABLE) { | |
577 | if (pcs_pinconf_bias_disable(pctldev, pin)) { | |
578 | *config = 0; | |
579 | return 0; | |
580 | } else { | |
581 | return -ENOTSUPP; | |
582 | } | |
583 | } else if (param != func->conf[i].param) { | |
584 | continue; | |
585 | } | |
586 | ||
587 | offset = pin * (pcs->width / BITS_PER_BYTE); | |
588 | data = pcs->read(pcs->base + offset) & func->conf[i].mask; | |
589 | switch (func->conf[i].param) { | |
590 | /* 4 parameters */ | |
591 | case PIN_CONFIG_BIAS_PULL_DOWN: | |
592 | case PIN_CONFIG_BIAS_PULL_UP: | |
593 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: | |
594 | if ((data != func->conf[i].enable) || | |
595 | (data == func->conf[i].disable)) | |
596 | return -ENOTSUPP; | |
597 | *config = 0; | |
598 | break; | |
599 | /* 2 parameters */ | |
600 | case PIN_CONFIG_INPUT_SCHMITT: | |
601 | for (j = 0; j < func->nconfs; j++) { | |
602 | switch (func->conf[j].param) { | |
603 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: | |
604 | if (data != func->conf[j].enable) | |
605 | return -ENOTSUPP; | |
606 | break; | |
607 | default: | |
608 | break; | |
609 | } | |
610 | } | |
611 | *config = data; | |
612 | break; | |
613 | case PIN_CONFIG_DRIVE_STRENGTH: | |
614 | case PIN_CONFIG_SLEW_RATE: | |
615 | default: | |
616 | *config = data; | |
617 | break; | |
618 | } | |
619 | return 0; | |
620 | } | |
8b8b091b TL |
621 | return -ENOTSUPP; |
622 | } | |
623 | ||
624 | static int pcs_pinconf_set(struct pinctrl_dev *pctldev, | |
625 | unsigned pin, unsigned long config) | |
626 | { | |
9dddb4df HZ |
627 | struct pcs_device *pcs = pinctrl_dev_get_drvdata(pctldev); |
628 | struct pcs_function *func; | |
7cba5b3f HZ |
629 | unsigned offset = 0, shift = 0, i, data, ret; |
630 | u16 arg; | |
9dddb4df HZ |
631 | |
632 | ret = pcs_get_function(pctldev, pin, &func); | |
633 | if (ret) | |
634 | return ret; | |
635 | ||
636 | for (i = 0; i < func->nconfs; i++) { | |
637 | if (pinconf_to_config_param(config) == func->conf[i].param) { | |
638 | offset = pin * (pcs->width / BITS_PER_BYTE); | |
639 | data = pcs->read(pcs->base + offset); | |
7cba5b3f | 640 | arg = pinconf_to_config_argument(config); |
9dddb4df HZ |
641 | 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: | |
646 | shift = ffs(func->conf[i].mask) - 1; | |
9dddb4df HZ |
647 | data &= ~func->conf[i].mask; |
648 | data |= (arg << shift) & func->conf[i].mask; | |
649 | break; | |
650 | /* 4 parameters */ | |
651 | case PIN_CONFIG_BIAS_DISABLE: | |
652 | pcs_pinconf_clear_bias(pctldev, pin); | |
653 | break; | |
654 | case PIN_CONFIG_BIAS_PULL_DOWN: | |
655 | case PIN_CONFIG_BIAS_PULL_UP: | |
7cba5b3f | 656 | if (arg) |
9dddb4df HZ |
657 | pcs_pinconf_clear_bias(pctldev, pin); |
658 | /* fall through */ | |
659 | case PIN_CONFIG_INPUT_SCHMITT_ENABLE: | |
660 | data &= ~func->conf[i].mask; | |
7cba5b3f | 661 | if (arg) |
9dddb4df HZ |
662 | data |= func->conf[i].enable; |
663 | else | |
664 | data |= func->conf[i].disable; | |
665 | break; | |
666 | default: | |
667 | return -ENOTSUPP; | |
668 | } | |
669 | pcs->write(data, pcs->base + offset); | |
670 | return 0; | |
671 | } | |
672 | } | |
8b8b091b TL |
673 | return -ENOTSUPP; |
674 | } | |
675 | ||
676 | static int pcs_pinconf_group_get(struct pinctrl_dev *pctldev, | |
677 | unsigned group, unsigned long *config) | |
678 | { | |
9dddb4df HZ |
679 | const unsigned *pins; |
680 | unsigned npins, old = 0; | |
681 | int i, ret; | |
682 | ||
683 | ret = pcs_get_group_pins(pctldev, group, &pins, &npins); | |
684 | if (ret) | |
685 | return ret; | |
686 | for (i = 0; i < npins; i++) { | |
687 | if (pcs_pinconf_get(pctldev, pins[i], config)) | |
688 | return -ENOTSUPP; | |
689 | /* configs do not match between two pins */ | |
690 | if (i && (old != *config)) | |
691 | return -ENOTSUPP; | |
692 | old = *config; | |
693 | } | |
694 | return 0; | |
8b8b091b TL |
695 | } |
696 | ||
697 | static int pcs_pinconf_group_set(struct pinctrl_dev *pctldev, | |
698 | unsigned group, unsigned long config) | |
699 | { | |
9dddb4df HZ |
700 | const unsigned *pins; |
701 | unsigned npins; | |
702 | int i, ret; | |
703 | ||
704 | ret = pcs_get_group_pins(pctldev, group, &pins, &npins); | |
705 | if (ret) | |
706 | return ret; | |
707 | for (i = 0; i < npins; i++) { | |
708 | if (pcs_pinconf_set(pctldev, pins[i], config)) | |
709 | return -ENOTSUPP; | |
710 | } | |
711 | return 0; | |
8b8b091b TL |
712 | } |
713 | ||
714 | static void pcs_pinconf_dbg_show(struct pinctrl_dev *pctldev, | |
9dddb4df | 715 | struct seq_file *s, unsigned pin) |
8b8b091b TL |
716 | { |
717 | } | |
718 | ||
719 | static void pcs_pinconf_group_dbg_show(struct pinctrl_dev *pctldev, | |
720 | struct seq_file *s, unsigned selector) | |
721 | { | |
722 | } | |
723 | ||
9dddb4df HZ |
724 | static void pcs_pinconf_config_dbg_show(struct pinctrl_dev *pctldev, |
725 | struct seq_file *s, | |
726 | unsigned long config) | |
727 | { | |
728 | pinconf_generic_dump_config(pctldev, s, config); | |
729 | } | |
730 | ||
022ab148 | 731 | static const struct pinconf_ops pcs_pinconf_ops = { |
8b8b091b TL |
732 | .pin_config_get = pcs_pinconf_get, |
733 | .pin_config_set = pcs_pinconf_set, | |
734 | .pin_config_group_get = pcs_pinconf_group_get, | |
735 | .pin_config_group_set = pcs_pinconf_group_set, | |
736 | .pin_config_dbg_show = pcs_pinconf_dbg_show, | |
737 | .pin_config_group_dbg_show = pcs_pinconf_group_dbg_show, | |
9dddb4df | 738 | .pin_config_config_dbg_show = pcs_pinconf_config_dbg_show, |
a7bbdd7f | 739 | .is_generic = true, |
8b8b091b TL |
740 | }; |
741 | ||
742 | /** | |
743 | * pcs_add_pin() - add a pin to the static per controller pin array | |
744 | * @pcs: pcs driver instance | |
745 | * @offset: register offset from base | |
746 | */ | |
150632b0 | 747 | static int pcs_add_pin(struct pcs_device *pcs, unsigned offset) |
8b8b091b TL |
748 | { |
749 | struct pinctrl_pin_desc *pin; | |
750 | struct pcs_name *pn; | |
751 | int i; | |
752 | ||
753 | i = pcs->pins.cur; | |
754 | if (i >= pcs->desc.npins) { | |
755 | dev_err(pcs->dev, "too many pins, max %i\n", | |
756 | pcs->desc.npins); | |
757 | return -ENOMEM; | |
758 | } | |
759 | ||
760 | pin = &pcs->pins.pa[i]; | |
761 | pn = &pcs->names[i]; | |
762 | sprintf(pn->name, "%lx", | |
763 | (unsigned long)pcs->res->start + offset); | |
764 | pin->name = pn->name; | |
765 | pin->number = i; | |
766 | pcs->pins.cur++; | |
767 | ||
768 | return i; | |
769 | } | |
770 | ||
771 | /** | |
772 | * pcs_allocate_pin_table() - adds all the pins for the pinctrl driver | |
773 | * @pcs: pcs driver instance | |
774 | * | |
775 | * In case of errors, resources are freed in pcs_free_resources. | |
776 | * | |
777 | * If your hardware needs holes in the address space, then just set | |
778 | * up multiple driver instances. | |
779 | */ | |
150632b0 | 780 | static int pcs_allocate_pin_table(struct pcs_device *pcs) |
8b8b091b TL |
781 | { |
782 | int mux_bytes, nr_pins, i; | |
783 | ||
784 | mux_bytes = pcs->width / BITS_PER_BYTE; | |
4e7e8017 MP |
785 | |
786 | if (pcs->bits_per_mux) { | |
787 | pcs->bits_per_pin = fls(pcs->fmask); | |
788 | nr_pins = (pcs->size * BITS_PER_BYTE) / pcs->bits_per_pin; | |
789 | } else { | |
790 | nr_pins = pcs->size / mux_bytes; | |
791 | } | |
8b8b091b TL |
792 | |
793 | dev_dbg(pcs->dev, "allocating %i pins\n", nr_pins); | |
794 | pcs->pins.pa = devm_kzalloc(pcs->dev, | |
795 | sizeof(*pcs->pins.pa) * nr_pins, | |
796 | GFP_KERNEL); | |
797 | if (!pcs->pins.pa) | |
798 | return -ENOMEM; | |
799 | ||
800 | pcs->names = devm_kzalloc(pcs->dev, | |
801 | sizeof(struct pcs_name) * nr_pins, | |
802 | GFP_KERNEL); | |
803 | if (!pcs->names) | |
804 | return -ENOMEM; | |
805 | ||
806 | pcs->desc.pins = pcs->pins.pa; | |
807 | pcs->desc.npins = nr_pins; | |
808 | ||
809 | for (i = 0; i < pcs->desc.npins; i++) { | |
810 | unsigned offset; | |
811 | int res; | |
4e7e8017 | 812 | int byte_num; |
8b8b091b | 813 | |
4e7e8017 MP |
814 | if (pcs->bits_per_mux) { |
815 | byte_num = (pcs->bits_per_pin * i) / BITS_PER_BYTE; | |
816 | offset = (byte_num / mux_bytes) * mux_bytes; | |
817 | } else { | |
818 | offset = i * mux_bytes; | |
819 | } | |
8b8b091b TL |
820 | res = pcs_add_pin(pcs, offset); |
821 | if (res < 0) { | |
822 | dev_err(pcs->dev, "error adding pins: %i\n", res); | |
823 | return res; | |
824 | } | |
825 | } | |
826 | ||
827 | return 0; | |
828 | } | |
829 | ||
830 | /** | |
831 | * pcs_add_function() - adds a new function to the function list | |
832 | * @pcs: pcs driver instance | |
833 | * @np: device node of the mux entry | |
834 | * @name: name of the function | |
835 | * @vals: array of mux register value pairs used by the function | |
836 | * @nvals: number of mux register value pairs | |
837 | * @pgnames: array of pingroup names for the function | |
838 | * @npgnames: number of pingroup names | |
839 | */ | |
840 | static struct pcs_function *pcs_add_function(struct pcs_device *pcs, | |
841 | struct device_node *np, | |
842 | const char *name, | |
843 | struct pcs_func_vals *vals, | |
844 | unsigned nvals, | |
845 | const char **pgnames, | |
846 | unsigned npgnames) | |
847 | { | |
848 | struct pcs_function *function; | |
849 | ||
850 | function = devm_kzalloc(pcs->dev, sizeof(*function), GFP_KERNEL); | |
851 | if (!function) | |
852 | return NULL; | |
853 | ||
854 | function->name = name; | |
855 | function->vals = vals; | |
856 | function->nvals = nvals; | |
857 | function->pgnames = pgnames; | |
858 | function->npgnames = npgnames; | |
859 | ||
860 | mutex_lock(&pcs->mutex); | |
861 | list_add_tail(&function->node, &pcs->functions); | |
862 | radix_tree_insert(&pcs->ftree, pcs->nfuncs, function); | |
863 | pcs->nfuncs++; | |
864 | mutex_unlock(&pcs->mutex); | |
865 | ||
866 | return function; | |
867 | } | |
868 | ||
869 | static void pcs_remove_function(struct pcs_device *pcs, | |
870 | struct pcs_function *function) | |
871 | { | |
872 | int i; | |
873 | ||
874 | mutex_lock(&pcs->mutex); | |
875 | for (i = 0; i < pcs->nfuncs; i++) { | |
876 | struct pcs_function *found; | |
877 | ||
878 | found = radix_tree_lookup(&pcs->ftree, i); | |
879 | if (found == function) | |
880 | radix_tree_delete(&pcs->ftree, i); | |
881 | } | |
882 | list_del(&function->node); | |
883 | mutex_unlock(&pcs->mutex); | |
884 | } | |
885 | ||
886 | /** | |
887 | * pcs_add_pingroup() - add a pingroup to the pingroup list | |
888 | * @pcs: pcs driver instance | |
889 | * @np: device node of the mux entry | |
890 | * @name: name of the pingroup | |
891 | * @gpins: array of the pins that belong to the group | |
892 | * @ngpins: number of pins in the group | |
893 | */ | |
894 | static int pcs_add_pingroup(struct pcs_device *pcs, | |
895 | struct device_node *np, | |
896 | const char *name, | |
897 | int *gpins, | |
898 | int ngpins) | |
899 | { | |
900 | struct pcs_pingroup *pingroup; | |
901 | ||
902 | pingroup = devm_kzalloc(pcs->dev, sizeof(*pingroup), GFP_KERNEL); | |
903 | if (!pingroup) | |
904 | return -ENOMEM; | |
905 | ||
906 | pingroup->name = name; | |
907 | pingroup->np = np; | |
908 | pingroup->gpins = gpins; | |
909 | pingroup->ngpins = ngpins; | |
910 | ||
911 | mutex_lock(&pcs->mutex); | |
912 | list_add_tail(&pingroup->node, &pcs->pingroups); | |
913 | radix_tree_insert(&pcs->pgtree, pcs->ngroups, pingroup); | |
914 | pcs->ngroups++; | |
915 | mutex_unlock(&pcs->mutex); | |
916 | ||
917 | return 0; | |
918 | } | |
919 | ||
920 | /** | |
921 | * pcs_get_pin_by_offset() - get a pin index based on the register offset | |
922 | * @pcs: pcs driver instance | |
923 | * @offset: register offset from the base | |
924 | * | |
925 | * Note that this is OK as long as the pins are in a static array. | |
926 | */ | |
927 | static int pcs_get_pin_by_offset(struct pcs_device *pcs, unsigned offset) | |
928 | { | |
929 | unsigned index; | |
930 | ||
931 | if (offset >= pcs->size) { | |
932 | dev_err(pcs->dev, "mux offset out of range: 0x%x (0x%x)\n", | |
933 | offset, pcs->size); | |
934 | return -EINVAL; | |
935 | } | |
936 | ||
4e7e8017 MP |
937 | if (pcs->bits_per_mux) |
938 | index = (offset * BITS_PER_BYTE) / pcs->bits_per_pin; | |
939 | else | |
940 | index = offset / (pcs->width / BITS_PER_BYTE); | |
8b8b091b TL |
941 | |
942 | return index; | |
943 | } | |
944 | ||
9dddb4df HZ |
945 | /* |
946 | * check whether data matches enable bits or disable bits | |
947 | * Return value: 1 for matching enable bits, 0 for matching disable bits, | |
948 | * and negative value for matching failure. | |
949 | */ | |
950 | static int pcs_config_match(unsigned data, unsigned enable, unsigned disable) | |
951 | { | |
952 | int ret = -EINVAL; | |
953 | ||
954 | if (data == enable) | |
955 | ret = 1; | |
956 | else if (data == disable) | |
957 | ret = 0; | |
958 | return ret; | |
959 | } | |
960 | ||
961 | static void add_config(struct pcs_conf_vals **conf, enum pin_config_param param, | |
962 | unsigned value, unsigned enable, unsigned disable, | |
963 | unsigned mask) | |
964 | { | |
965 | (*conf)->param = param; | |
966 | (*conf)->val = value; | |
967 | (*conf)->enable = enable; | |
968 | (*conf)->disable = disable; | |
969 | (*conf)->mask = mask; | |
970 | (*conf)++; | |
971 | } | |
972 | ||
973 | static void add_setting(unsigned long **setting, enum pin_config_param param, | |
974 | unsigned arg) | |
975 | { | |
976 | **setting = pinconf_to_config_packed(param, arg); | |
977 | (*setting)++; | |
978 | } | |
979 | ||
980 | /* add pinconf setting with 2 parameters */ | |
981 | static void pcs_add_conf2(struct pcs_device *pcs, struct device_node *np, | |
982 | const char *name, enum pin_config_param param, | |
983 | struct pcs_conf_vals **conf, unsigned long **settings) | |
984 | { | |
7cba5b3f | 985 | unsigned value[2], shift; |
9dddb4df HZ |
986 | int ret; |
987 | ||
988 | ret = of_property_read_u32_array(np, name, value, 2); | |
989 | if (ret) | |
990 | return; | |
991 | /* set value & mask */ | |
992 | value[0] &= value[1]; | |
7cba5b3f | 993 | shift = ffs(value[1]) - 1; |
9dddb4df HZ |
994 | /* skip enable & disable */ |
995 | add_config(conf, param, value[0], 0, 0, value[1]); | |
7cba5b3f | 996 | add_setting(settings, param, value[0] >> shift); |
9dddb4df HZ |
997 | } |
998 | ||
999 | /* add pinconf setting with 4 parameters */ | |
1000 | static void pcs_add_conf4(struct pcs_device *pcs, struct device_node *np, | |
1001 | const char *name, enum pin_config_param param, | |
1002 | struct pcs_conf_vals **conf, unsigned long **settings) | |
1003 | { | |
1004 | unsigned value[4]; | |
1005 | int ret; | |
1006 | ||
1007 | /* value to set, enable, disable, mask */ | |
1008 | ret = of_property_read_u32_array(np, name, value, 4); | |
1009 | if (ret) | |
1010 | return; | |
1011 | if (!value[3]) { | |
1012 | dev_err(pcs->dev, "mask field of the property can't be 0\n"); | |
1013 | return; | |
1014 | } | |
1015 | value[0] &= value[3]; | |
1016 | value[1] &= value[3]; | |
1017 | value[2] &= value[3]; | |
1018 | ret = pcs_config_match(value[0], value[1], value[2]); | |
1019 | if (ret < 0) | |
1020 | dev_dbg(pcs->dev, "failed to match enable or disable bits\n"); | |
1021 | add_config(conf, param, value[0], value[1], value[2], value[3]); | |
1022 | add_setting(settings, param, ret); | |
1023 | } | |
1024 | ||
1025 | static int pcs_parse_pinconf(struct pcs_device *pcs, struct device_node *np, | |
1026 | struct pcs_function *func, | |
1027 | struct pinctrl_map **map) | |
1028 | ||
1029 | { | |
1030 | struct pinctrl_map *m = *map; | |
1031 | int i = 0, nconfs = 0; | |
1032 | unsigned long *settings = NULL, *s = NULL; | |
1033 | struct pcs_conf_vals *conf = NULL; | |
1034 | struct pcs_conf_type prop2[] = { | |
1035 | { "pinctrl-single,drive-strength", PIN_CONFIG_DRIVE_STRENGTH, }, | |
1036 | { "pinctrl-single,slew-rate", PIN_CONFIG_SLEW_RATE, }, | |
1037 | { "pinctrl-single,input-schmitt", PIN_CONFIG_INPUT_SCHMITT, }, | |
1038 | }; | |
1039 | struct pcs_conf_type prop4[] = { | |
1040 | { "pinctrl-single,bias-pullup", PIN_CONFIG_BIAS_PULL_UP, }, | |
1041 | { "pinctrl-single,bias-pulldown", PIN_CONFIG_BIAS_PULL_DOWN, }, | |
1042 | { "pinctrl-single,input-schmitt-enable", | |
1043 | PIN_CONFIG_INPUT_SCHMITT_ENABLE, }, | |
1044 | }; | |
1045 | ||
1046 | /* If pinconf isn't supported, don't parse properties in below. */ | |
1047 | if (!pcs->is_pinconf) | |
1048 | return 0; | |
1049 | ||
1050 | /* cacluate how much properties are supported in current node */ | |
1051 | for (i = 0; i < ARRAY_SIZE(prop2); i++) { | |
1052 | if (of_find_property(np, prop2[i].name, NULL)) | |
1053 | nconfs++; | |
1054 | } | |
1055 | for (i = 0; i < ARRAY_SIZE(prop4); i++) { | |
1056 | if (of_find_property(np, prop4[i].name, NULL)) | |
1057 | nconfs++; | |
1058 | } | |
1059 | if (!nconfs) | |
1060 | return 0; | |
1061 | ||
1062 | func->conf = devm_kzalloc(pcs->dev, | |
1063 | sizeof(struct pcs_conf_vals) * nconfs, | |
1064 | GFP_KERNEL); | |
1065 | if (!func->conf) | |
1066 | return -ENOMEM; | |
1067 | func->nconfs = nconfs; | |
1068 | conf = &(func->conf[0]); | |
1069 | m++; | |
1070 | settings = devm_kzalloc(pcs->dev, sizeof(unsigned long) * nconfs, | |
1071 | GFP_KERNEL); | |
1072 | if (!settings) | |
1073 | return -ENOMEM; | |
1074 | s = &settings[0]; | |
1075 | ||
1076 | for (i = 0; i < ARRAY_SIZE(prop2); i++) | |
1077 | pcs_add_conf2(pcs, np, prop2[i].name, prop2[i].param, | |
1078 | &conf, &s); | |
1079 | for (i = 0; i < ARRAY_SIZE(prop4); i++) | |
1080 | pcs_add_conf4(pcs, np, prop4[i].name, prop4[i].param, | |
1081 | &conf, &s); | |
1082 | m->type = PIN_MAP_TYPE_CONFIGS_GROUP; | |
1083 | m->data.configs.group_or_pin = np->name; | |
1084 | m->data.configs.configs = settings; | |
1085 | m->data.configs.num_configs = nconfs; | |
1086 | return 0; | |
1087 | } | |
1088 | ||
1089 | static void pcs_free_pingroups(struct pcs_device *pcs); | |
1090 | ||
8b8b091b TL |
1091 | /** |
1092 | * smux_parse_one_pinctrl_entry() - parses a device tree mux entry | |
1093 | * @pcs: pinctrl driver instance | |
1094 | * @np: device node of the mux entry | |
1095 | * @map: map entry | |
9dddb4df | 1096 | * @num_maps: number of map |
8b8b091b TL |
1097 | * @pgnames: pingroup names |
1098 | * | |
1099 | * Note that this binding currently supports only sets of one register + value. | |
1100 | * | |
1101 | * Also note that this driver tries to avoid understanding pin and function | |
1102 | * names because of the extra bloat they would cause especially in the case of | |
1103 | * a large number of pins. This driver just sets what is specified for the board | |
1104 | * in the .dts file. Further user space debugging tools can be developed to | |
1105 | * decipher the pin and function names using debugfs. | |
1106 | * | |
1107 | * If you are concerned about the boot time, set up the static pins in | |
1108 | * the bootloader, and only set up selected pins as device tree entries. | |
1109 | */ | |
1110 | static int pcs_parse_one_pinctrl_entry(struct pcs_device *pcs, | |
1111 | struct device_node *np, | |
1112 | struct pinctrl_map **map, | |
9dddb4df | 1113 | unsigned *num_maps, |
8b8b091b TL |
1114 | const char **pgnames) |
1115 | { | |
1116 | struct pcs_func_vals *vals; | |
1117 | const __be32 *mux; | |
4e7e8017 | 1118 | int size, rows, *pins, index = 0, found = 0, res = -ENOMEM; |
8b8b091b TL |
1119 | struct pcs_function *function; |
1120 | ||
4e7e8017 MP |
1121 | mux = of_get_property(np, PCS_MUX_PINS_NAME, &size); |
1122 | if ((!mux) || (size < sizeof(*mux) * 2)) { | |
1123 | dev_err(pcs->dev, "bad data for mux %s\n", | |
1124 | np->name); | |
8b8b091b TL |
1125 | return -EINVAL; |
1126 | } | |
1127 | ||
1128 | size /= sizeof(*mux); /* Number of elements in array */ | |
4e7e8017 | 1129 | rows = size / 2; |
8b8b091b TL |
1130 | |
1131 | vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows, GFP_KERNEL); | |
1132 | if (!vals) | |
1133 | return -ENOMEM; | |
1134 | ||
1135 | pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows, GFP_KERNEL); | |
1136 | if (!pins) | |
1137 | goto free_vals; | |
1138 | ||
1139 | while (index < size) { | |
1140 | unsigned offset, val; | |
1141 | int pin; | |
1142 | ||
1143 | offset = be32_to_cpup(mux + index++); | |
1144 | val = be32_to_cpup(mux + index++); | |
1145 | vals[found].reg = pcs->base + offset; | |
1146 | vals[found].val = val; | |
1147 | ||
1148 | pin = pcs_get_pin_by_offset(pcs, offset); | |
1149 | if (pin < 0) { | |
1150 | dev_err(pcs->dev, | |
1151 | "could not add functions for %s %ux\n", | |
1152 | np->name, offset); | |
1153 | break; | |
1154 | } | |
1155 | pins[found++] = pin; | |
1156 | } | |
1157 | ||
1158 | pgnames[0] = np->name; | |
1159 | function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1); | |
1160 | if (!function) | |
1161 | goto free_pins; | |
1162 | ||
1163 | res = pcs_add_pingroup(pcs, np, np->name, pins, found); | |
1164 | if (res < 0) | |
1165 | goto free_function; | |
1166 | ||
1167 | (*map)->type = PIN_MAP_TYPE_MUX_GROUP; | |
1168 | (*map)->data.mux.group = np->name; | |
1169 | (*map)->data.mux.function = np->name; | |
1170 | ||
9dddb4df | 1171 | if (pcs->is_pinconf) { |
18442e65 WY |
1172 | res = pcs_parse_pinconf(pcs, np, function, map); |
1173 | if (res) | |
9dddb4df HZ |
1174 | goto free_pingroups; |
1175 | *num_maps = 2; | |
1176 | } else { | |
1177 | *num_maps = 1; | |
1178 | } | |
8b8b091b TL |
1179 | return 0; |
1180 | ||
9dddb4df HZ |
1181 | free_pingroups: |
1182 | pcs_free_pingroups(pcs); | |
1183 | *num_maps = 1; | |
8b8b091b TL |
1184 | free_function: |
1185 | pcs_remove_function(pcs, function); | |
1186 | ||
1187 | free_pins: | |
1188 | devm_kfree(pcs->dev, pins); | |
1189 | ||
4e7e8017 MP |
1190 | free_vals: |
1191 | devm_kfree(pcs->dev, vals); | |
1192 | ||
1193 | return res; | |
1194 | } | |
1195 | ||
1196 | #define PARAMS_FOR_BITS_PER_MUX 3 | |
1197 | ||
1198 | static int pcs_parse_bits_in_pinctrl_entry(struct pcs_device *pcs, | |
1199 | struct device_node *np, | |
1200 | struct pinctrl_map **map, | |
1201 | unsigned *num_maps, | |
1202 | const char **pgnames) | |
1203 | { | |
1204 | struct pcs_func_vals *vals; | |
1205 | const __be32 *mux; | |
1206 | int size, rows, *pins, index = 0, found = 0, res = -ENOMEM; | |
1207 | int npins_in_row; | |
1208 | struct pcs_function *function; | |
1209 | ||
1210 | mux = of_get_property(np, PCS_MUX_BITS_NAME, &size); | |
1211 | ||
1212 | if (!mux) { | |
1213 | dev_err(pcs->dev, "no valid property for %s\n", np->name); | |
1214 | return -EINVAL; | |
1215 | } | |
1216 | ||
1217 | if (size < (sizeof(*mux) * PARAMS_FOR_BITS_PER_MUX)) { | |
1218 | dev_err(pcs->dev, "bad data for %s\n", np->name); | |
1219 | return -EINVAL; | |
1220 | } | |
1221 | ||
1222 | /* Number of elements in array */ | |
1223 | size /= sizeof(*mux); | |
1224 | ||
1225 | rows = size / PARAMS_FOR_BITS_PER_MUX; | |
1226 | npins_in_row = pcs->width / pcs->bits_per_pin; | |
1227 | ||
1228 | vals = devm_kzalloc(pcs->dev, sizeof(*vals) * rows * npins_in_row, | |
1229 | GFP_KERNEL); | |
1230 | if (!vals) | |
1231 | return -ENOMEM; | |
1232 | ||
1233 | pins = devm_kzalloc(pcs->dev, sizeof(*pins) * rows * npins_in_row, | |
1234 | GFP_KERNEL); | |
1235 | if (!pins) | |
1236 | goto free_vals; | |
1237 | ||
1238 | while (index < size) { | |
1239 | unsigned offset, val; | |
1240 | unsigned mask, bit_pos, val_pos, mask_pos, submask; | |
1241 | unsigned pin_num_from_lsb; | |
1242 | int pin; | |
1243 | ||
1244 | offset = be32_to_cpup(mux + index++); | |
1245 | val = be32_to_cpup(mux + index++); | |
1246 | mask = be32_to_cpup(mux + index++); | |
1247 | ||
1248 | /* Parse pins in each row from LSB */ | |
1249 | while (mask) { | |
1250 | bit_pos = ffs(mask); | |
1251 | pin_num_from_lsb = bit_pos / pcs->bits_per_pin; | |
1252 | mask_pos = ((pcs->fmask) << (bit_pos - 1)); | |
1253 | val_pos = val & mask_pos; | |
1254 | submask = mask & mask_pos; | |
1255 | mask &= ~mask_pos; | |
1256 | ||
1257 | if (submask != mask_pos) { | |
1258 | dev_warn(pcs->dev, | |
1259 | "Invalid submask 0x%x for %s at 0x%x\n", | |
1260 | submask, np->name, offset); | |
1261 | continue; | |
1262 | } | |
1263 | ||
1264 | vals[found].mask = submask; | |
1265 | vals[found].reg = pcs->base + offset; | |
1266 | vals[found].val = val_pos; | |
1267 | ||
1268 | pin = pcs_get_pin_by_offset(pcs, offset); | |
1269 | if (pin < 0) { | |
1270 | dev_err(pcs->dev, | |
1271 | "could not add functions for %s %ux\n", | |
1272 | np->name, offset); | |
1273 | break; | |
1274 | } | |
1275 | pins[found++] = pin + pin_num_from_lsb; | |
1276 | } | |
1277 | } | |
1278 | ||
1279 | pgnames[0] = np->name; | |
1280 | function = pcs_add_function(pcs, np, np->name, vals, found, pgnames, 1); | |
1281 | if (!function) | |
1282 | goto free_pins; | |
1283 | ||
1284 | res = pcs_add_pingroup(pcs, np, np->name, pins, found); | |
1285 | if (res < 0) | |
1286 | goto free_function; | |
1287 | ||
1288 | (*map)->type = PIN_MAP_TYPE_MUX_GROUP; | |
1289 | (*map)->data.mux.group = np->name; | |
1290 | (*map)->data.mux.function = np->name; | |
1291 | ||
1292 | if (pcs->is_pinconf) { | |
1293 | dev_err(pcs->dev, "pinconf not supported\n"); | |
1294 | goto free_pingroups; | |
1295 | } | |
1296 | ||
1297 | *num_maps = 1; | |
1298 | return 0; | |
1299 | ||
1300 | free_pingroups: | |
1301 | pcs_free_pingroups(pcs); | |
1302 | *num_maps = 1; | |
1303 | free_function: | |
1304 | pcs_remove_function(pcs, function); | |
1305 | ||
1306 | free_pins: | |
1307 | devm_kfree(pcs->dev, pins); | |
1308 | ||
8b8b091b TL |
1309 | free_vals: |
1310 | devm_kfree(pcs->dev, vals); | |
1311 | ||
1312 | return res; | |
1313 | } | |
1314 | /** | |
1315 | * pcs_dt_node_to_map() - allocates and parses pinctrl maps | |
1316 | * @pctldev: pinctrl instance | |
1317 | * @np_config: device tree pinmux entry | |
1318 | * @map: array of map entries | |
1319 | * @num_maps: number of maps | |
1320 | */ | |
1321 | static int pcs_dt_node_to_map(struct pinctrl_dev *pctldev, | |
1322 | struct device_node *np_config, | |
1323 | struct pinctrl_map **map, unsigned *num_maps) | |
1324 | { | |
1325 | struct pcs_device *pcs; | |
1326 | const char **pgnames; | |
1327 | int ret; | |
1328 | ||
1329 | pcs = pinctrl_dev_get_drvdata(pctldev); | |
1330 | ||
9dddb4df HZ |
1331 | /* create 2 maps. One is for pinmux, and the other is for pinconf. */ |
1332 | *map = devm_kzalloc(pcs->dev, sizeof(**map) * 2, GFP_KERNEL); | |
00e79d12 | 1333 | if (!*map) |
8b8b091b TL |
1334 | return -ENOMEM; |
1335 | ||
1336 | *num_maps = 0; | |
1337 | ||
1338 | pgnames = devm_kzalloc(pcs->dev, sizeof(*pgnames), GFP_KERNEL); | |
1339 | if (!pgnames) { | |
1340 | ret = -ENOMEM; | |
1341 | goto free_map; | |
1342 | } | |
1343 | ||
4e7e8017 MP |
1344 | if (pcs->bits_per_mux) { |
1345 | ret = pcs_parse_bits_in_pinctrl_entry(pcs, np_config, map, | |
1346 | num_maps, pgnames); | |
1347 | if (ret < 0) { | |
1348 | dev_err(pcs->dev, "no pins entries for %s\n", | |
1349 | np_config->name); | |
1350 | goto free_pgnames; | |
1351 | } | |
1352 | } else { | |
1353 | ret = pcs_parse_one_pinctrl_entry(pcs, np_config, map, | |
1354 | num_maps, pgnames); | |
1355 | if (ret < 0) { | |
1356 | dev_err(pcs->dev, "no pins entries for %s\n", | |
1357 | np_config->name); | |
1358 | goto free_pgnames; | |
1359 | } | |
8b8b091b | 1360 | } |
8b8b091b TL |
1361 | |
1362 | return 0; | |
1363 | ||
1364 | free_pgnames: | |
1365 | devm_kfree(pcs->dev, pgnames); | |
1366 | free_map: | |
1367 | devm_kfree(pcs->dev, *map); | |
1368 | ||
1369 | return ret; | |
1370 | } | |
1371 | ||
1372 | /** | |
1373 | * pcs_free_funcs() - free memory used by functions | |
1374 | * @pcs: pcs driver instance | |
1375 | */ | |
1376 | static void pcs_free_funcs(struct pcs_device *pcs) | |
1377 | { | |
1378 | struct list_head *pos, *tmp; | |
1379 | int i; | |
1380 | ||
1381 | mutex_lock(&pcs->mutex); | |
1382 | for (i = 0; i < pcs->nfuncs; i++) { | |
1383 | struct pcs_function *func; | |
1384 | ||
1385 | func = radix_tree_lookup(&pcs->ftree, i); | |
1386 | if (!func) | |
1387 | continue; | |
1388 | radix_tree_delete(&pcs->ftree, i); | |
1389 | } | |
1390 | list_for_each_safe(pos, tmp, &pcs->functions) { | |
1391 | struct pcs_function *function; | |
1392 | ||
1393 | function = list_entry(pos, struct pcs_function, node); | |
1394 | list_del(&function->node); | |
1395 | } | |
1396 | mutex_unlock(&pcs->mutex); | |
1397 | } | |
1398 | ||
1399 | /** | |
1400 | * pcs_free_pingroups() - free memory used by pingroups | |
1401 | * @pcs: pcs driver instance | |
1402 | */ | |
1403 | static void pcs_free_pingroups(struct pcs_device *pcs) | |
1404 | { | |
1405 | struct list_head *pos, *tmp; | |
1406 | int i; | |
1407 | ||
1408 | mutex_lock(&pcs->mutex); | |
1409 | for (i = 0; i < pcs->ngroups; i++) { | |
1410 | struct pcs_pingroup *pingroup; | |
1411 | ||
1412 | pingroup = radix_tree_lookup(&pcs->pgtree, i); | |
1413 | if (!pingroup) | |
1414 | continue; | |
1415 | radix_tree_delete(&pcs->pgtree, i); | |
1416 | } | |
1417 | list_for_each_safe(pos, tmp, &pcs->pingroups) { | |
1418 | struct pcs_pingroup *pingroup; | |
1419 | ||
1420 | pingroup = list_entry(pos, struct pcs_pingroup, node); | |
1421 | list_del(&pingroup->node); | |
1422 | } | |
1423 | mutex_unlock(&pcs->mutex); | |
1424 | } | |
1425 | ||
1426 | /** | |
1427 | * pcs_free_resources() - free memory used by this driver | |
1428 | * @pcs: pcs driver instance | |
1429 | */ | |
1430 | static void pcs_free_resources(struct pcs_device *pcs) | |
1431 | { | |
1432 | if (pcs->pctl) | |
1433 | pinctrl_unregister(pcs->pctl); | |
1434 | ||
1435 | pcs_free_funcs(pcs); | |
1436 | pcs_free_pingroups(pcs); | |
1437 | } | |
1438 | ||
1439 | #define PCS_GET_PROP_U32(name, reg, err) \ | |
1440 | do { \ | |
1441 | ret = of_property_read_u32(np, name, reg); \ | |
1442 | if (ret) { \ | |
1443 | dev_err(pcs->dev, err); \ | |
1444 | return ret; \ | |
1445 | } \ | |
1446 | } while (0); | |
1447 | ||
1448 | static struct of_device_id pcs_of_match[]; | |
1449 | ||
a1a277eb HZ |
1450 | static int pcs_add_gpio_func(struct device_node *node, struct pcs_device *pcs) |
1451 | { | |
1452 | const char *propname = "pinctrl-single,gpio-range"; | |
1453 | const char *cellname = "#pinctrl-single,gpio-range-cells"; | |
1454 | struct of_phandle_args gpiospec; | |
1455 | struct pcs_gpiofunc_range *range; | |
1456 | int ret, i; | |
1457 | ||
1458 | for (i = 0; ; i++) { | |
1459 | ret = of_parse_phandle_with_args(node, propname, cellname, | |
1460 | i, &gpiospec); | |
1461 | /* Do not treat it as error. Only treat it as end condition. */ | |
1462 | if (ret) { | |
1463 | ret = 0; | |
1464 | break; | |
1465 | } | |
1466 | range = devm_kzalloc(pcs->dev, sizeof(*range), GFP_KERNEL); | |
1467 | if (!range) { | |
1468 | ret = -ENOMEM; | |
1469 | break; | |
1470 | } | |
1471 | range->offset = gpiospec.args[0]; | |
1472 | range->npins = gpiospec.args[1]; | |
1473 | range->gpiofunc = gpiospec.args[2]; | |
1474 | mutex_lock(&pcs->mutex); | |
1475 | list_add_tail(&range->node, &pcs->gpiofuncs); | |
1476 | mutex_unlock(&pcs->mutex); | |
1477 | } | |
1478 | return ret; | |
1479 | } | |
1480 | ||
150632b0 | 1481 | static int pcs_probe(struct platform_device *pdev) |
8b8b091b TL |
1482 | { |
1483 | struct device_node *np = pdev->dev.of_node; | |
1484 | const struct of_device_id *match; | |
1485 | struct resource *res; | |
1486 | struct pcs_device *pcs; | |
1487 | int ret; | |
1488 | ||
1489 | match = of_match_device(pcs_of_match, &pdev->dev); | |
1490 | if (!match) | |
1491 | return -EINVAL; | |
1492 | ||
1493 | pcs = devm_kzalloc(&pdev->dev, sizeof(*pcs), GFP_KERNEL); | |
1494 | if (!pcs) { | |
1495 | dev_err(&pdev->dev, "could not allocate\n"); | |
1496 | return -ENOMEM; | |
1497 | } | |
1498 | pcs->dev = &pdev->dev; | |
1499 | mutex_init(&pcs->mutex); | |
1500 | INIT_LIST_HEAD(&pcs->pingroups); | |
1501 | INIT_LIST_HEAD(&pcs->functions); | |
a1a277eb | 1502 | INIT_LIST_HEAD(&pcs->gpiofuncs); |
9dddb4df | 1503 | pcs->is_pinconf = match->data; |
8b8b091b TL |
1504 | |
1505 | PCS_GET_PROP_U32("pinctrl-single,register-width", &pcs->width, | |
1506 | "register width not specified\n"); | |
1507 | ||
477ac771 HZ |
1508 | ret = of_property_read_u32(np, "pinctrl-single,function-mask", |
1509 | &pcs->fmask); | |
1510 | if (!ret) { | |
1511 | pcs->fshift = ffs(pcs->fmask) - 1; | |
1512 | pcs->fmax = pcs->fmask >> pcs->fshift; | |
1513 | } else { | |
1514 | /* If mask property doesn't exist, function mux is invalid. */ | |
1515 | pcs->fmask = 0; | |
1516 | pcs->fshift = 0; | |
1517 | pcs->fmax = 0; | |
1518 | } | |
8b8b091b TL |
1519 | |
1520 | ret = of_property_read_u32(np, "pinctrl-single,function-off", | |
1521 | &pcs->foff); | |
1522 | if (ret) | |
1523 | pcs->foff = PCS_OFF_DISABLED; | |
1524 | ||
9e605cb6 PU |
1525 | pcs->bits_per_mux = of_property_read_bool(np, |
1526 | "pinctrl-single,bit-per-mux"); | |
1527 | ||
8b8b091b TL |
1528 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
1529 | if (!res) { | |
1530 | dev_err(pcs->dev, "could not get resource\n"); | |
1531 | return -ENODEV; | |
1532 | } | |
1533 | ||
1534 | pcs->res = devm_request_mem_region(pcs->dev, res->start, | |
1535 | resource_size(res), DRIVER_NAME); | |
1536 | if (!pcs->res) { | |
1537 | dev_err(pcs->dev, "could not get mem_region\n"); | |
1538 | return -EBUSY; | |
1539 | } | |
1540 | ||
1541 | pcs->size = resource_size(pcs->res); | |
1542 | pcs->base = devm_ioremap(pcs->dev, pcs->res->start, pcs->size); | |
1543 | if (!pcs->base) { | |
1544 | dev_err(pcs->dev, "could not ioremap\n"); | |
1545 | return -ENODEV; | |
1546 | } | |
1547 | ||
1548 | INIT_RADIX_TREE(&pcs->pgtree, GFP_KERNEL); | |
1549 | INIT_RADIX_TREE(&pcs->ftree, GFP_KERNEL); | |
1550 | platform_set_drvdata(pdev, pcs); | |
1551 | ||
1552 | switch (pcs->width) { | |
1553 | case 8: | |
1554 | pcs->read = pcs_readb; | |
1555 | pcs->write = pcs_writeb; | |
1556 | break; | |
1557 | case 16: | |
1558 | pcs->read = pcs_readw; | |
1559 | pcs->write = pcs_writew; | |
1560 | break; | |
1561 | case 32: | |
1562 | pcs->read = pcs_readl; | |
1563 | pcs->write = pcs_writel; | |
1564 | break; | |
1565 | default: | |
1566 | break; | |
1567 | } | |
1568 | ||
1569 | pcs->desc.name = DRIVER_NAME; | |
1570 | pcs->desc.pctlops = &pcs_pinctrl_ops; | |
1571 | pcs->desc.pmxops = &pcs_pinmux_ops; | |
a7bbdd7f AL |
1572 | if (pcs->is_pinconf) |
1573 | pcs->desc.confops = &pcs_pinconf_ops; | |
8b8b091b TL |
1574 | pcs->desc.owner = THIS_MODULE; |
1575 | ||
1576 | ret = pcs_allocate_pin_table(pcs); | |
1577 | if (ret < 0) | |
1578 | goto free; | |
1579 | ||
1580 | pcs->pctl = pinctrl_register(&pcs->desc, pcs->dev, pcs); | |
1581 | if (!pcs->pctl) { | |
1582 | dev_err(pcs->dev, "could not register single pinctrl driver\n"); | |
1583 | ret = -EINVAL; | |
1584 | goto free; | |
1585 | } | |
1586 | ||
a1a277eb HZ |
1587 | ret = pcs_add_gpio_func(np, pcs); |
1588 | if (ret < 0) | |
1589 | goto free; | |
1590 | ||
8b8b091b TL |
1591 | dev_info(pcs->dev, "%i pins at pa %p size %u\n", |
1592 | pcs->desc.npins, pcs->base, pcs->size); | |
1593 | ||
1594 | return 0; | |
1595 | ||
1596 | free: | |
1597 | pcs_free_resources(pcs); | |
1598 | ||
1599 | return ret; | |
1600 | } | |
1601 | ||
f90f54b3 | 1602 | static int pcs_remove(struct platform_device *pdev) |
8b8b091b TL |
1603 | { |
1604 | struct pcs_device *pcs = platform_get_drvdata(pdev); | |
1605 | ||
1606 | if (!pcs) | |
1607 | return 0; | |
1608 | ||
1609 | pcs_free_resources(pcs); | |
1610 | ||
1611 | return 0; | |
1612 | } | |
1613 | ||
99688ed7 | 1614 | static struct of_device_id pcs_of_match[] = { |
9dddb4df HZ |
1615 | { .compatible = "pinctrl-single", .data = (void *)false }, |
1616 | { .compatible = "pinconf-single", .data = (void *)true }, | |
8b8b091b TL |
1617 | { }, |
1618 | }; | |
1619 | MODULE_DEVICE_TABLE(of, pcs_of_match); | |
1620 | ||
1621 | static struct platform_driver pcs_driver = { | |
1622 | .probe = pcs_probe, | |
2a36f086 | 1623 | .remove = pcs_remove, |
8b8b091b TL |
1624 | .driver = { |
1625 | .owner = THIS_MODULE, | |
1626 | .name = DRIVER_NAME, | |
1627 | .of_match_table = pcs_of_match, | |
1628 | }, | |
1629 | }; | |
1630 | ||
1631 | module_platform_driver(pcs_driver); | |
1632 | ||
1633 | MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>"); | |
1634 | MODULE_DESCRIPTION("One-register-per-pin type device tree based pinctrl driver"); | |
1635 | MODULE_LICENSE("GPL v2"); |