blob: 47ccfcc8a647ccacdd0880ab770dc2c102d2ba9c [file] [log] [blame]
Dong Aishengae75ff82012-04-27 20:26:16 +08001/*
2 * Core driver for the imx pin controller
3 *
4 * Copyright (C) 2012 Freescale Semiconductor, Inc.
5 * Copyright (C) 2012 Linaro Ltd.
6 *
7 * Author: Dong Aisheng <dong.aisheng@linaro.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
15#include <linux/err.h>
16#include <linux/init.h>
17#include <linux/io.h>
Philipp Zabel8626ada2016-02-25 18:23:07 +010018#include <linux/mfd/syscon.h>
Dong Aishengae75ff82012-04-27 20:26:16 +080019#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
Adrian Alonso26d8cde2015-09-25 16:06:00 -050022#include <linux/of_address.h>
Dong Aishengae75ff82012-04-27 20:26:16 +080023#include <linux/pinctrl/machine.h>
24#include <linux/pinctrl/pinconf.h>
25#include <linux/pinctrl/pinctrl.h>
26#include <linux/pinctrl/pinmux.h>
27#include <linux/slab.h>
Philipp Zabel8626ada2016-02-25 18:23:07 +010028#include <linux/regmap.h>
Dong Aishengae75ff82012-04-27 20:26:16 +080029
Linus Walleijedad3b22014-09-03 13:37:38 +020030#include "../core.h"
Dong Aishengae75ff82012-04-27 20:26:16 +080031#include "pinctrl-imx.h"
32
Dong Aishengae75ff82012-04-27 20:26:16 +080033/* The bits in CONFIG cell defined in binding doc*/
34#define IMX_NO_PAD_CTL 0x80000000 /* no pin config need */
35#define IMX_PAD_SION 0x40000000 /* set SION */
36
37/**
38 * @dev: a pointer back to containing device
39 * @base: the offset to the controller in virtual memory
40 */
41struct imx_pinctrl {
42 struct device *dev;
43 struct pinctrl_dev *pctl;
44 void __iomem *base;
Adrian Alonso26d8cde2015-09-25 16:06:00 -050045 void __iomem *input_sel_base;
Dong Aishengae75ff82012-04-27 20:26:16 +080046 const struct imx_pinctrl_soc_info *info;
47};
48
Dong Aishengae75ff82012-04-27 20:26:16 +080049static const inline struct imx_pin_group *imx_pinctrl_find_group_by_name(
50 const struct imx_pinctrl_soc_info *info,
51 const char *name)
52{
53 const struct imx_pin_group *grp = NULL;
54 int i;
55
56 for (i = 0; i < info->ngroups; i++) {
57 if (!strcmp(info->groups[i].name, name)) {
58 grp = &info->groups[i];
59 break;
60 }
61 }
62
63 return grp;
64}
65
66static int imx_get_groups_count(struct pinctrl_dev *pctldev)
67{
68 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
69 const struct imx_pinctrl_soc_info *info = ipctl->info;
70
71 return info->ngroups;
72}
73
74static const char *imx_get_group_name(struct pinctrl_dev *pctldev,
75 unsigned selector)
76{
77 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
78 const struct imx_pinctrl_soc_info *info = ipctl->info;
79
80 return info->groups[selector].name;
81}
82
83static int imx_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
84 const unsigned **pins,
85 unsigned *npins)
86{
87 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
88 const struct imx_pinctrl_soc_info *info = ipctl->info;
89
90 if (selector >= info->ngroups)
91 return -EINVAL;
92
Sascha Hauer8f903f82013-07-28 16:29:22 +020093 *pins = info->groups[selector].pin_ids;
Dong Aishengae75ff82012-04-27 20:26:16 +080094 *npins = info->groups[selector].npins;
95
96 return 0;
97}
98
99static void imx_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
100 unsigned offset)
101{
102 seq_printf(s, "%s", dev_name(pctldev->dev));
103}
104
105static int imx_dt_node_to_map(struct pinctrl_dev *pctldev,
106 struct device_node *np,
107 struct pinctrl_map **map, unsigned *num_maps)
108{
109 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
110 const struct imx_pinctrl_soc_info *info = ipctl->info;
111 const struct imx_pin_group *grp;
112 struct pinctrl_map *new_map;
113 struct device_node *parent;
114 int map_num = 1;
Hui Wang18071612012-06-20 18:13:47 +0800115 int i, j;
Dong Aishengae75ff82012-04-27 20:26:16 +0800116
117 /*
118 * first find the group of this node and check if we need create
119 * config maps for pins
120 */
121 grp = imx_pinctrl_find_group_by_name(info, np->name);
122 if (!grp) {
123 dev_err(info->dev, "unable to find group for node %s\n",
124 np->name);
125 return -EINVAL;
126 }
127
128 for (i = 0; i < grp->npins; i++) {
Sascha Hauer8f903f82013-07-28 16:29:22 +0200129 if (!(grp->pins[i].config & IMX_NO_PAD_CTL))
Dong Aishengae75ff82012-04-27 20:26:16 +0800130 map_num++;
131 }
132
133 new_map = kmalloc(sizeof(struct pinctrl_map) * map_num, GFP_KERNEL);
134 if (!new_map)
135 return -ENOMEM;
136
137 *map = new_map;
138 *num_maps = map_num;
139
140 /* create mux map */
141 parent = of_get_parent(np);
Devendra Nagac71157c2012-06-07 22:19:26 +0530142 if (!parent) {
143 kfree(new_map);
Dong Aishengae75ff82012-04-27 20:26:16 +0800144 return -EINVAL;
Devendra Nagac71157c2012-06-07 22:19:26 +0530145 }
Dong Aishengae75ff82012-04-27 20:26:16 +0800146 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
147 new_map[0].data.mux.function = parent->name;
148 new_map[0].data.mux.group = np->name;
149 of_node_put(parent);
150
151 /* create config map */
152 new_map++;
Hui Wang18071612012-06-20 18:13:47 +0800153 for (i = j = 0; i < grp->npins; i++) {
Sascha Hauer8f903f82013-07-28 16:29:22 +0200154 if (!(grp->pins[i].config & IMX_NO_PAD_CTL)) {
Hui Wang18071612012-06-20 18:13:47 +0800155 new_map[j].type = PIN_MAP_TYPE_CONFIGS_PIN;
156 new_map[j].data.configs.group_or_pin =
Sascha Hauer8f903f82013-07-28 16:29:22 +0200157 pin_get_name(pctldev, grp->pins[i].pin);
158 new_map[j].data.configs.configs = &grp->pins[i].config;
Hui Wang18071612012-06-20 18:13:47 +0800159 new_map[j].data.configs.num_configs = 1;
160 j++;
Dong Aishengae75ff82012-04-27 20:26:16 +0800161 }
162 }
163
164 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
Dong Aisheng67695f22012-06-08 21:33:12 +0800165 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
Dong Aishengae75ff82012-04-27 20:26:16 +0800166
167 return 0;
168}
169
170static void imx_dt_free_map(struct pinctrl_dev *pctldev,
171 struct pinctrl_map *map, unsigned num_maps)
172{
Devendra Naga3a86a5f2012-06-09 00:52:11 +0530173 kfree(map);
Dong Aishengae75ff82012-04-27 20:26:16 +0800174}
175
Laurent Pinchart022ab142013-02-16 10:25:07 +0100176static const struct pinctrl_ops imx_pctrl_ops = {
Dong Aishengae75ff82012-04-27 20:26:16 +0800177 .get_groups_count = imx_get_groups_count,
178 .get_group_name = imx_get_group_name,
179 .get_group_pins = imx_get_group_pins,
180 .pin_dbg_show = imx_pin_dbg_show,
181 .dt_node_to_map = imx_dt_node_to_map,
182 .dt_free_map = imx_dt_free_map,
183
184};
185
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200186static int imx_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
187 unsigned group)
Dong Aishengae75ff82012-04-27 20:26:16 +0800188{
189 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
190 const struct imx_pinctrl_soc_info *info = ipctl->info;
191 const struct imx_pin_reg *pin_reg;
Dong Aishengae75ff82012-04-27 20:26:16 +0800192 unsigned int npins, pin_id;
193 int i;
Sascha Hauer8f903f82013-07-28 16:29:22 +0200194 struct imx_pin_group *grp;
Dong Aishengae75ff82012-04-27 20:26:16 +0800195
196 /*
197 * Configure the mux mode for each pin in the group for a specific
198 * function.
199 */
Sascha Hauer8f903f82013-07-28 16:29:22 +0200200 grp = &info->groups[group];
201 npins = grp->npins;
Dong Aishengae75ff82012-04-27 20:26:16 +0800202
203 dev_dbg(ipctl->dev, "enable function %s group %s\n",
Sascha Hauer8f903f82013-07-28 16:29:22 +0200204 info->functions[selector].name, grp->name);
Dong Aishengae75ff82012-04-27 20:26:16 +0800205
206 for (i = 0; i < npins; i++) {
Sascha Hauer8f903f82013-07-28 16:29:22 +0200207 struct imx_pin *pin = &grp->pins[i];
208 pin_id = pin->pin;
Shawn Guoe1641532013-02-20 10:32:52 +0800209 pin_reg = &info->pin_regs[pin_id];
Dong Aishengae75ff82012-04-27 20:26:16 +0800210
Stefan Agner3dac1912014-09-06 18:25:04 +0200211 if (pin_reg->mux_reg == -1) {
Dong Aishengae75ff82012-04-27 20:26:16 +0800212 dev_err(ipctl->dev, "Pin(%s) does not support mux function\n",
213 info->pins[pin_id].name);
214 return -EINVAL;
215 }
216
Jingchang Lubf5a5302013-05-28 17:32:07 +0800217 if (info->flags & SHARE_MUX_CONF_REG) {
218 u32 reg;
219 reg = readl(ipctl->base + pin_reg->mux_reg);
220 reg &= ~(0x7 << 20);
Sascha Hauer8f903f82013-07-28 16:29:22 +0200221 reg |= (pin->mux_mode << 20);
Jingchang Lubf5a5302013-05-28 17:32:07 +0800222 writel(reg, ipctl->base + pin_reg->mux_reg);
223 } else {
Sascha Hauer8f903f82013-07-28 16:29:22 +0200224 writel(pin->mux_mode, ipctl->base + pin_reg->mux_reg);
Jingchang Lubf5a5302013-05-28 17:32:07 +0800225 }
Dong Aishengae75ff82012-04-27 20:26:16 +0800226 dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
Sascha Hauer8f903f82013-07-28 16:29:22 +0200227 pin_reg->mux_reg, pin->mux_mode);
Dong Aishengae75ff82012-04-27 20:26:16 +0800228
Shawn Guo94176fa2013-08-04 21:39:23 +0800229 /*
230 * If the select input value begins with 0xff, it's a quirky
231 * select input and the value should be interpreted as below.
232 * 31 23 15 7 0
233 * | 0xff | shift | width | select |
234 * It's used to work around the problem that the select
235 * input for some pin is not implemented in the select
236 * input register but in some general purpose register.
237 * We encode the select input value, width and shift of
238 * the bit field into input_val cell of pin function ID
239 * in device tree, and then decode them here for setting
240 * up the select input bits in general purpose register.
241 */
Sascha Hauer8f903f82013-07-28 16:29:22 +0200242 if (pin->input_val >> 24 == 0xff) {
243 u32 val = pin->input_val;
Shawn Guo94176fa2013-08-04 21:39:23 +0800244 u8 select = val & 0xff;
245 u8 width = (val >> 8) & 0xff;
246 u8 shift = (val >> 16) & 0xff;
247 u32 mask = ((1 << width) - 1) << shift;
248 /*
249 * The input_reg[i] here is actually some IOMUXC general
250 * purpose register, not regular select input register.
251 */
Peter Chena3183c62013-10-28 14:01:16 +0800252 val = readl(ipctl->base + pin->input_reg);
Shawn Guo94176fa2013-08-04 21:39:23 +0800253 val &= ~mask;
254 val |= select << shift;
Peter Chena3183c62013-10-28 14:01:16 +0800255 writel(val, ipctl->base + pin->input_reg);
256 } else if (pin->input_reg) {
Shawn Guo94176fa2013-08-04 21:39:23 +0800257 /*
258 * Regular select input register can never be at offset
259 * 0, and we only print register value for regular case.
260 */
Adrian Alonso26d8cde2015-09-25 16:06:00 -0500261 if (ipctl->input_sel_base)
262 writel(pin->input_val, ipctl->input_sel_base +
263 pin->input_reg);
264 else
265 writel(pin->input_val, ipctl->base +
266 pin->input_reg);
Dong Aishengae75ff82012-04-27 20:26:16 +0800267 dev_dbg(ipctl->dev,
268 "==>select_input: offset 0x%x val 0x%x\n",
Sascha Hauer8f903f82013-07-28 16:29:22 +0200269 pin->input_reg, pin->input_val);
Dong Aishengae75ff82012-04-27 20:26:16 +0800270 }
271 }
272
273 return 0;
274}
275
Dong Aishengae75ff82012-04-27 20:26:16 +0800276static int imx_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
277{
278 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
279 const struct imx_pinctrl_soc_info *info = ipctl->info;
280
281 return info->nfunctions;
282}
283
284static const char *imx_pmx_get_func_name(struct pinctrl_dev *pctldev,
285 unsigned selector)
286{
287 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
288 const struct imx_pinctrl_soc_info *info = ipctl->info;
289
290 return info->functions[selector].name;
291}
292
293static int imx_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
294 const char * const **groups,
295 unsigned * const num_groups)
296{
297 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
298 const struct imx_pinctrl_soc_info *info = ipctl->info;
299
300 *groups = info->functions[selector].groups;
301 *num_groups = info->functions[selector].num_groups;
302
303 return 0;
304}
305
Stefan Agner1f2b0452014-10-16 21:47:57 +0200306static int imx_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
307 struct pinctrl_gpio_range *range, unsigned offset)
308{
309 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
310 const struct imx_pinctrl_soc_info *info = ipctl->info;
311 const struct imx_pin_reg *pin_reg;
312 struct imx_pin_group *grp;
313 struct imx_pin *imx_pin;
314 unsigned int pin, group;
315 u32 reg;
316
317 /* Currently implementation only for shared mux/conf register */
318 if (!(info->flags & SHARE_MUX_CONF_REG))
319 return -EINVAL;
320
321 pin_reg = &info->pin_regs[offset];
322 if (pin_reg->mux_reg == -1)
323 return -EINVAL;
324
325 /* Find the pinctrl config with GPIO mux mode for the requested pin */
326 for (group = 0; group < info->ngroups; group++) {
327 grp = &info->groups[group];
328 for (pin = 0; pin < grp->npins; pin++) {
329 imx_pin = &grp->pins[pin];
330 if (imx_pin->pin == offset && !imx_pin->mux_mode)
331 goto mux_pin;
332 }
333 }
334
335 return -EINVAL;
336
337mux_pin:
338 reg = readl(ipctl->base + pin_reg->mux_reg);
339 reg &= ~(0x7 << 20);
340 reg |= imx_pin->config;
341 writel(reg, ipctl->base + pin_reg->mux_reg);
342
343 return 0;
344}
345
Stefan Agner23c39602016-01-08 10:50:30 -0800346static void imx_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
347 struct pinctrl_gpio_range *range, unsigned offset)
348{
349 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
350 const struct imx_pinctrl_soc_info *info = ipctl->info;
351 const struct imx_pin_reg *pin_reg;
352 u32 reg;
353
354 /*
355 * Only Vybrid has the input/output buffer enable flags (IBE/OBE)
356 * They are part of the shared mux/conf register.
357 */
358 if (!(info->flags & SHARE_MUX_CONF_REG))
359 return;
360
361 pin_reg = &info->pin_regs[offset];
362 if (pin_reg->mux_reg == -1)
363 return;
364
365 /* Clear IBE/OBE/PUE to disable the pin (Hi-Z) */
366 reg = readl(ipctl->base + pin_reg->mux_reg);
367 reg &= ~0x7;
368 writel(reg, ipctl->base + pin_reg->mux_reg);
369}
370
Stefan Agner1f2b0452014-10-16 21:47:57 +0200371static int imx_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
372 struct pinctrl_gpio_range *range, unsigned offset, bool input)
373{
374 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
375 const struct imx_pinctrl_soc_info *info = ipctl->info;
376 const struct imx_pin_reg *pin_reg;
377 u32 reg;
378
379 /*
380 * Only Vybrid has the input/output buffer enable flags (IBE/OBE)
381 * They are part of the shared mux/conf register.
382 */
383 if (!(info->flags & SHARE_MUX_CONF_REG))
384 return -EINVAL;
385
386 pin_reg = &info->pin_regs[offset];
387 if (pin_reg->mux_reg == -1)
388 return -EINVAL;
389
390 /* IBE always enabled allows us to read the value "on the wire" */
391 reg = readl(ipctl->base + pin_reg->mux_reg);
392 if (input)
393 reg &= ~0x2;
394 else
395 reg |= 0x2;
396 writel(reg, ipctl->base + pin_reg->mux_reg);
397
398 return 0;
399}
400
Laurent Pinchart022ab142013-02-16 10:25:07 +0100401static const struct pinmux_ops imx_pmx_ops = {
Dong Aishengae75ff82012-04-27 20:26:16 +0800402 .get_functions_count = imx_pmx_get_funcs_count,
403 .get_function_name = imx_pmx_get_func_name,
404 .get_function_groups = imx_pmx_get_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200405 .set_mux = imx_pmx_set,
Stefan Agner1f2b0452014-10-16 21:47:57 +0200406 .gpio_request_enable = imx_pmx_gpio_request_enable,
Stefan Agner23c39602016-01-08 10:50:30 -0800407 .gpio_disable_free = imx_pmx_gpio_disable_free,
Stefan Agner1f2b0452014-10-16 21:47:57 +0200408 .gpio_set_direction = imx_pmx_gpio_set_direction,
Dong Aishengae75ff82012-04-27 20:26:16 +0800409};
410
411static int imx_pinconf_get(struct pinctrl_dev *pctldev,
412 unsigned pin_id, unsigned long *config)
413{
414 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
415 const struct imx_pinctrl_soc_info *info = ipctl->info;
Shawn Guoe1641532013-02-20 10:32:52 +0800416 const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
Dong Aishengae75ff82012-04-27 20:26:16 +0800417
Stefan Agner3dac1912014-09-06 18:25:04 +0200418 if (pin_reg->conf_reg == -1) {
Dong Aishengae75ff82012-04-27 20:26:16 +0800419 dev_err(info->dev, "Pin(%s) does not support config function\n",
420 info->pins[pin_id].name);
421 return -EINVAL;
422 }
423
424 *config = readl(ipctl->base + pin_reg->conf_reg);
425
Jingchang Lubf5a5302013-05-28 17:32:07 +0800426 if (info->flags & SHARE_MUX_CONF_REG)
427 *config &= 0xffff;
428
Dong Aishengae75ff82012-04-27 20:26:16 +0800429 return 0;
430}
431
432static int imx_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700433 unsigned pin_id, unsigned long *configs,
434 unsigned num_configs)
Dong Aishengae75ff82012-04-27 20:26:16 +0800435{
436 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
437 const struct imx_pinctrl_soc_info *info = ipctl->info;
Shawn Guoe1641532013-02-20 10:32:52 +0800438 const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
Sherman Yin03b054e2013-08-27 11:32:12 -0700439 int i;
Dong Aishengae75ff82012-04-27 20:26:16 +0800440
Stefan Agner3dac1912014-09-06 18:25:04 +0200441 if (pin_reg->conf_reg == -1) {
Dong Aishengae75ff82012-04-27 20:26:16 +0800442 dev_err(info->dev, "Pin(%s) does not support config function\n",
443 info->pins[pin_id].name);
444 return -EINVAL;
445 }
446
447 dev_dbg(ipctl->dev, "pinconf set pin %s\n",
448 info->pins[pin_id].name);
449
Sherman Yin03b054e2013-08-27 11:32:12 -0700450 for (i = 0; i < num_configs; i++) {
451 if (info->flags & SHARE_MUX_CONF_REG) {
452 u32 reg;
453 reg = readl(ipctl->base + pin_reg->conf_reg);
454 reg &= ~0xffff;
455 reg |= configs[i];
456 writel(reg, ipctl->base + pin_reg->conf_reg);
457 } else {
458 writel(configs[i], ipctl->base + pin_reg->conf_reg);
459 }
460 dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%lx\n",
461 pin_reg->conf_reg, configs[i]);
462 } /* for each config */
Dong Aishengae75ff82012-04-27 20:26:16 +0800463
464 return 0;
465}
466
467static void imx_pinconf_dbg_show(struct pinctrl_dev *pctldev,
468 struct seq_file *s, unsigned pin_id)
469{
470 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
471 const struct imx_pinctrl_soc_info *info = ipctl->info;
Shawn Guoe1641532013-02-20 10:32:52 +0800472 const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
Dong Aishengae75ff82012-04-27 20:26:16 +0800473 unsigned long config;
474
Uwe Kleine-König4ff0f032015-01-27 23:50:25 +0100475 if (!pin_reg || pin_reg->conf_reg == -1) {
Dong Aishengae75ff82012-04-27 20:26:16 +0800476 seq_printf(s, "N/A");
477 return;
478 }
479
480 config = readl(ipctl->base + pin_reg->conf_reg);
481 seq_printf(s, "0x%lx", config);
482}
483
484static void imx_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
485 struct seq_file *s, unsigned group)
486{
487 struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
488 const struct imx_pinctrl_soc_info *info = ipctl->info;
489 struct imx_pin_group *grp;
490 unsigned long config;
491 const char *name;
492 int i, ret;
493
494 if (group > info->ngroups)
495 return;
496
497 seq_printf(s, "\n");
498 grp = &info->groups[group];
499 for (i = 0; i < grp->npins; i++) {
Sascha Hauer8f903f82013-07-28 16:29:22 +0200500 struct imx_pin *pin = &grp->pins[i];
501 name = pin_get_name(pctldev, pin->pin);
502 ret = imx_pinconf_get(pctldev, pin->pin, &config);
Dong Aishengae75ff82012-04-27 20:26:16 +0800503 if (ret)
504 return;
505 seq_printf(s, "%s: 0x%lx", name, config);
506 }
507}
508
Laurent Pinchart022ab142013-02-16 10:25:07 +0100509static const struct pinconf_ops imx_pinconf_ops = {
Dong Aishengae75ff82012-04-27 20:26:16 +0800510 .pin_config_get = imx_pinconf_get,
511 .pin_config_set = imx_pinconf_set,
512 .pin_config_dbg_show = imx_pinconf_dbg_show,
513 .pin_config_group_dbg_show = imx_pinconf_group_dbg_show,
514};
515
516static struct pinctrl_desc imx_pinctrl_desc = {
517 .pctlops = &imx_pctrl_ops,
518 .pmxops = &imx_pmx_ops,
519 .confops = &imx_pinconf_ops,
520 .owner = THIS_MODULE,
521};
522
Shawn Guoe1641532013-02-20 10:32:52 +0800523/*
524 * Each pin represented in fsl,pins consists of 5 u32 PIN_FUNC_ID and
525 * 1 u32 CONFIG, so 24 types in total for each pin.
526 */
527#define FSL_PIN_SIZE 24
Jingchang Lubf5a5302013-05-28 17:32:07 +0800528#define SHARE_FSL_PIN_SIZE 20
Dong Aishengae75ff82012-04-27 20:26:16 +0800529
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800530static int imx_pinctrl_parse_groups(struct device_node *np,
531 struct imx_pin_group *grp,
532 struct imx_pinctrl_soc_info *info,
533 u32 index)
Dong Aishengae75ff82012-04-27 20:26:16 +0800534{
Jingchang Lubf5a5302013-05-28 17:32:07 +0800535 int size, pin_size;
Richard Zhaoa6951452012-09-18 14:54:00 +0800536 const __be32 *list;
Shawn Guoe1641532013-02-20 10:32:52 +0800537 int i;
Dong Aishengae75ff82012-04-27 20:26:16 +0800538 u32 config;
539
540 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
541
Jingchang Lubf5a5302013-05-28 17:32:07 +0800542 if (info->flags & SHARE_MUX_CONF_REG)
543 pin_size = SHARE_FSL_PIN_SIZE;
544 else
545 pin_size = FSL_PIN_SIZE;
Dong Aishengae75ff82012-04-27 20:26:16 +0800546 /* Initialise group */
547 grp->name = np->name;
548
549 /*
550 * the binding format is fsl,pins = <PIN_FUNC_ID CONFIG ...>,
551 * do sanity check and calculate pins number
552 */
553 list = of_get_property(np, "fsl,pins", &size);
Sascha Hauer1bf1fea92013-08-09 14:20:51 +0200554 if (!list) {
555 dev_err(info->dev, "no fsl,pins property in node %s\n", np->full_name);
556 return -EINVAL;
557 }
558
Dong Aishengae75ff82012-04-27 20:26:16 +0800559 /* we do not check return since it's safe node passed down */
Jingchang Lubf5a5302013-05-28 17:32:07 +0800560 if (!size || size % pin_size) {
Sascha Hauer01312512013-08-09 14:20:50 +0200561 dev_err(info->dev, "Invalid fsl,pins property in node %s\n", np->full_name);
Dong Aishengae75ff82012-04-27 20:26:16 +0800562 return -EINVAL;
563 }
564
Jingchang Lubf5a5302013-05-28 17:32:07 +0800565 grp->npins = size / pin_size;
Sascha Hauer8f903f82013-07-28 16:29:22 +0200566 grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(struct imx_pin),
Dong Aishengae75ff82012-04-27 20:26:16 +0800567 GFP_KERNEL);
Sascha Hauer8f903f82013-07-28 16:29:22 +0200568 grp->pin_ids = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
Dong Aishengae75ff82012-04-27 20:26:16 +0800569 GFP_KERNEL);
Sascha Hauer8f903f82013-07-28 16:29:22 +0200570 if (!grp->pins || ! grp->pin_ids)
571 return -ENOMEM;
572
Shawn Guoe1641532013-02-20 10:32:52 +0800573 for (i = 0; i < grp->npins; i++) {
574 u32 mux_reg = be32_to_cpu(*list++);
Jingchang Lubf5a5302013-05-28 17:32:07 +0800575 u32 conf_reg;
576 unsigned int pin_id;
577 struct imx_pin_reg *pin_reg;
Sascha Hauer8f903f82013-07-28 16:29:22 +0200578 struct imx_pin *pin = &grp->pins[i];
Shawn Guoe1641532013-02-20 10:32:52 +0800579
Adrian Alonsoe7b37a52015-09-25 16:05:59 -0500580 if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg)
581 mux_reg = -1;
582
Markus Pargmann16837f92015-03-24 16:26:18 +0100583 if (info->flags & SHARE_MUX_CONF_REG) {
Jingchang Lubf5a5302013-05-28 17:32:07 +0800584 conf_reg = mux_reg;
Markus Pargmann16837f92015-03-24 16:26:18 +0100585 } else {
Jingchang Lubf5a5302013-05-28 17:32:07 +0800586 conf_reg = be32_to_cpu(*list++);
Markus Pargmann16837f92015-03-24 16:26:18 +0100587 if (!conf_reg)
588 conf_reg = -1;
589 }
Jingchang Lubf5a5302013-05-28 17:32:07 +0800590
Adrian Alonsoe7b37a52015-09-25 16:05:59 -0500591 pin_id = (mux_reg != -1) ? mux_reg / 4 : conf_reg / 4;
Jingchang Lubf5a5302013-05-28 17:32:07 +0800592 pin_reg = &info->pin_regs[pin_id];
Sascha Hauer8f903f82013-07-28 16:29:22 +0200593 pin->pin = pin_id;
594 grp->pin_ids[i] = pin_id;
Shawn Guoe1641532013-02-20 10:32:52 +0800595 pin_reg->mux_reg = mux_reg;
596 pin_reg->conf_reg = conf_reg;
Sascha Hauer8f903f82013-07-28 16:29:22 +0200597 pin->input_reg = be32_to_cpu(*list++);
598 pin->mux_mode = be32_to_cpu(*list++);
599 pin->input_val = be32_to_cpu(*list++);
Shawn Guoe1641532013-02-20 10:32:52 +0800600
Dong Aishengae75ff82012-04-27 20:26:16 +0800601 /* SION bit is in mux register */
602 config = be32_to_cpu(*list++);
603 if (config & IMX_PAD_SION)
Sascha Hauer8f903f82013-07-28 16:29:22 +0200604 pin->mux_mode |= IOMUXC_CONFIG_SION;
605 pin->config = config & ~IMX_PAD_SION;
Dong Aishengae75ff82012-04-27 20:26:16 +0800606
Fabio Estevam08b51952014-04-13 12:09:05 -0300607 dev_dbg(info->dev, "%s: 0x%x 0x%08lx", info->pins[pin_id].name,
Sascha Hauer40604462013-08-23 10:38:57 +0200608 pin->mux_mode, pin->config);
609 }
Devendra Naga3a86a5f2012-06-09 00:52:11 +0530610
Dong Aishengae75ff82012-04-27 20:26:16 +0800611 return 0;
612}
613
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800614static int imx_pinctrl_parse_functions(struct device_node *np,
615 struct imx_pinctrl_soc_info *info,
616 u32 index)
Dong Aishengae75ff82012-04-27 20:26:16 +0800617{
618 struct device_node *child;
619 struct imx_pmx_func *func;
620 struct imx_pin_group *grp;
Dong Aishengae75ff82012-04-27 20:26:16 +0800621 u32 i = 0;
622
623 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
624
625 func = &info->functions[index];
626
627 /* Initialise function */
628 func->name = np->name;
629 func->num_groups = of_get_child_count(np);
Rickard Strandqvist9eedfd62014-06-26 13:28:16 +0200630 if (func->num_groups == 0) {
Sascha Hauer01312512013-08-09 14:20:50 +0200631 dev_err(info->dev, "no groups defined in %s\n", np->full_name);
Dong Aishengae75ff82012-04-27 20:26:16 +0800632 return -EINVAL;
633 }
634 func->groups = devm_kzalloc(info->dev,
635 func->num_groups * sizeof(char *), GFP_KERNEL);
636
637 for_each_child_of_node(np, child) {
638 func->groups[i] = child->name;
Robin Gongee163512015-09-24 15:53:57 -0500639 grp = &info->groups[info->group_index++];
Sascha Hauer5e13762c2013-08-09 14:20:52 +0200640 imx_pinctrl_parse_groups(child, grp, info, i++);
Dong Aishengae75ff82012-04-27 20:26:16 +0800641 }
642
643 return 0;
644}
645
Markus Pargmann5fcdf6a2015-04-10 16:22:38 +0200646/*
647 * Check if the DT contains pins in the direct child nodes. This indicates the
648 * newer DT format to store pins. This function returns true if the first found
649 * fsl,pins property is in a child of np. Otherwise false is returned.
650 */
651static bool imx_pinctrl_dt_is_flat_functions(struct device_node *np)
652{
653 struct device_node *function_np;
654 struct device_node *pinctrl_np;
655
656 for_each_child_of_node(np, function_np) {
657 if (of_property_read_bool(function_np, "fsl,pins"))
658 return true;
659
660 for_each_child_of_node(function_np, pinctrl_np) {
661 if (of_property_read_bool(pinctrl_np, "fsl,pins"))
662 return false;
663 }
664 }
665
666 return true;
667}
668
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800669static int imx_pinctrl_probe_dt(struct platform_device *pdev,
Dong Aishengae75ff82012-04-27 20:26:16 +0800670 struct imx_pinctrl_soc_info *info)
671{
672 struct device_node *np = pdev->dev.of_node;
673 struct device_node *child;
Dong Aishengae75ff82012-04-27 20:26:16 +0800674 u32 nfuncs = 0;
675 u32 i = 0;
Markus Pargmann5fcdf6a2015-04-10 16:22:38 +0200676 bool flat_funcs;
Dong Aishengae75ff82012-04-27 20:26:16 +0800677
678 if (!np)
679 return -ENODEV;
680
Markus Pargmann5fcdf6a2015-04-10 16:22:38 +0200681 flat_funcs = imx_pinctrl_dt_is_flat_functions(np);
682 if (flat_funcs) {
683 nfuncs = 1;
684 } else {
685 nfuncs = of_get_child_count(np);
686 if (nfuncs <= 0) {
687 dev_err(&pdev->dev, "no functions defined\n");
688 return -EINVAL;
689 }
Dong Aishengae75ff82012-04-27 20:26:16 +0800690 }
691
692 info->nfunctions = nfuncs;
693 info->functions = devm_kzalloc(&pdev->dev, nfuncs * sizeof(struct imx_pmx_func),
694 GFP_KERNEL);
695 if (!info->functions)
696 return -ENOMEM;
697
Markus Pargmann5fcdf6a2015-04-10 16:22:38 +0200698 if (flat_funcs) {
699 info->ngroups = of_get_child_count(np);
700 } else {
701 info->ngroups = 0;
702 for_each_child_of_node(np, child)
703 info->ngroups += of_get_child_count(child);
704 }
Dong Aishengae75ff82012-04-27 20:26:16 +0800705 info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct imx_pin_group),
706 GFP_KERNEL);
707 if (!info->groups)
708 return -ENOMEM;
709
Markus Pargmann5fcdf6a2015-04-10 16:22:38 +0200710 if (flat_funcs) {
711 imx_pinctrl_parse_functions(np, info, 0);
712 } else {
713 for_each_child_of_node(np, child)
714 imx_pinctrl_parse_functions(child, info, i++);
715 }
Dong Aishengae75ff82012-04-27 20:26:16 +0800716
717 return 0;
718}
719
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800720int imx_pinctrl_probe(struct platform_device *pdev,
721 struct imx_pinctrl_soc_info *info)
Dong Aishengae75ff82012-04-27 20:26:16 +0800722{
Philipp Zabel8626ada2016-02-25 18:23:07 +0100723 struct regmap_config config = { .name = "gpr" };
Adrian Alonso26d8cde2015-09-25 16:06:00 -0500724 struct device_node *dev_np = pdev->dev.of_node;
725 struct device_node *np;
Dong Aishengae75ff82012-04-27 20:26:16 +0800726 struct imx_pinctrl *ipctl;
727 struct resource *res;
Philipp Zabel8626ada2016-02-25 18:23:07 +0100728 struct regmap *gpr;
Stefan Agner4691dd02015-02-06 17:30:56 +0100729 int ret, i;
Dong Aishengae75ff82012-04-27 20:26:16 +0800730
Shawn Guoe1641532013-02-20 10:32:52 +0800731 if (!info || !info->pins || !info->npins) {
Dong Aishengae75ff82012-04-27 20:26:16 +0800732 dev_err(&pdev->dev, "wrong pinctrl info\n");
733 return -EINVAL;
734 }
735 info->dev = &pdev->dev;
736
Philipp Zabel8626ada2016-02-25 18:23:07 +0100737 if (info->gpr_compatible) {
738 gpr = syscon_regmap_lookup_by_compatible(info->gpr_compatible);
739 if (!IS_ERR(gpr))
740 regmap_attach_dev(&pdev->dev, gpr, &config);
741 }
742
Dong Aishengae75ff82012-04-27 20:26:16 +0800743 /* Create state holders etc for this driver */
744 ipctl = devm_kzalloc(&pdev->dev, sizeof(*ipctl), GFP_KERNEL);
745 if (!ipctl)
746 return -ENOMEM;
747
Stefan Agner3dac1912014-09-06 18:25:04 +0200748 info->pin_regs = devm_kmalloc(&pdev->dev, sizeof(*info->pin_regs) *
Shawn Guoe1641532013-02-20 10:32:52 +0800749 info->npins, GFP_KERNEL);
750 if (!info->pin_regs)
751 return -ENOMEM;
Stefan Agner4691dd02015-02-06 17:30:56 +0100752
753 for (i = 0; i < info->npins; i++) {
754 info->pin_regs[i].mux_reg = -1;
755 info->pin_regs[i].conf_reg = -1;
756 }
Shawn Guoe1641532013-02-20 10:32:52 +0800757
Dong Aishengae75ff82012-04-27 20:26:16 +0800758 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding9e0c1fb2013-01-21 11:09:14 +0100759 ipctl->base = devm_ioremap_resource(&pdev->dev, res);
760 if (IS_ERR(ipctl->base))
761 return PTR_ERR(ipctl->base);
Dong Aishengae75ff82012-04-27 20:26:16 +0800762
Adrian Alonso26d8cde2015-09-25 16:06:00 -0500763 if (of_property_read_bool(dev_np, "fsl,input-sel")) {
764 np = of_parse_phandle(dev_np, "fsl,input-sel", 0);
Vladimir Zapolskiy9a4f4242016-03-09 02:45:36 +0200765 if (!np) {
Adrian Alonso26d8cde2015-09-25 16:06:00 -0500766 dev_err(&pdev->dev, "iomuxc fsl,input-sel property not found\n");
767 return -EINVAL;
768 }
Vladimir Zapolskiy9a4f4242016-03-09 02:45:36 +0200769
770 ipctl->input_sel_base = of_iomap(np, 0);
Adrian Alonso26d8cde2015-09-25 16:06:00 -0500771 of_node_put(np);
Vladimir Zapolskiy9a4f4242016-03-09 02:45:36 +0200772 if (!ipctl->input_sel_base) {
773 dev_err(&pdev->dev,
774 "iomuxc input select base address not found\n");
775 return -ENOMEM;
776 }
Adrian Alonso26d8cde2015-09-25 16:06:00 -0500777 }
778
Dong Aishengae75ff82012-04-27 20:26:16 +0800779 imx_pinctrl_desc.name = dev_name(&pdev->dev);
780 imx_pinctrl_desc.pins = info->pins;
781 imx_pinctrl_desc.npins = info->npins;
782
783 ret = imx_pinctrl_probe_dt(pdev, info);
784 if (ret) {
785 dev_err(&pdev->dev, "fail to probe dt properties\n");
786 return ret;
787 }
788
789 ipctl->info = info;
790 ipctl->dev = info->dev;
791 platform_set_drvdata(pdev, ipctl);
Laxman Dewangana4b0f452016-02-24 14:44:07 +0530792 ipctl->pctl = devm_pinctrl_register(&pdev->dev, &imx_pinctrl_desc, ipctl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900793 if (IS_ERR(ipctl->pctl)) {
Dong Aishengae75ff82012-04-27 20:26:16 +0800794 dev_err(&pdev->dev, "could not register IMX pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900795 return PTR_ERR(ipctl->pctl);
Dong Aishengae75ff82012-04-27 20:26:16 +0800796 }
797
798 dev_info(&pdev->dev, "initialized IMX pinctrl driver\n");
799
800 return 0;
801}