xref: /linux/drivers/pinctrl/freescale/pinctrl-imx.c (revision e7b37a522aa92da5f47106aa07b6c1fc58bfd922)
1edad3b2aSLinus Walleij /*
2edad3b2aSLinus Walleij  * Core driver for the imx pin controller
3edad3b2aSLinus Walleij  *
4edad3b2aSLinus Walleij  * Copyright (C) 2012 Freescale Semiconductor, Inc.
5edad3b2aSLinus Walleij  * Copyright (C) 2012 Linaro Ltd.
6edad3b2aSLinus Walleij  *
7edad3b2aSLinus Walleij  * Author: Dong Aisheng <dong.aisheng@linaro.org>
8edad3b2aSLinus Walleij  *
9edad3b2aSLinus Walleij  * This program is free software; you can redistribute it and/or modify
10edad3b2aSLinus Walleij  * it under the terms of the GNU General Public License as published by
11edad3b2aSLinus Walleij  * the Free Software Foundation; either version 2 of the License, or
12edad3b2aSLinus Walleij  * (at your option) any later version.
13edad3b2aSLinus Walleij  */
14edad3b2aSLinus Walleij 
15edad3b2aSLinus Walleij #include <linux/err.h>
16edad3b2aSLinus Walleij #include <linux/init.h>
17edad3b2aSLinus Walleij #include <linux/io.h>
18edad3b2aSLinus Walleij #include <linux/module.h>
19edad3b2aSLinus Walleij #include <linux/of.h>
20edad3b2aSLinus Walleij #include <linux/of_device.h>
21edad3b2aSLinus Walleij #include <linux/pinctrl/machine.h>
22edad3b2aSLinus Walleij #include <linux/pinctrl/pinconf.h>
23edad3b2aSLinus Walleij #include <linux/pinctrl/pinctrl.h>
24edad3b2aSLinus Walleij #include <linux/pinctrl/pinmux.h>
25edad3b2aSLinus Walleij #include <linux/slab.h>
26edad3b2aSLinus Walleij 
27edad3b2aSLinus Walleij #include "../core.h"
28edad3b2aSLinus Walleij #include "pinctrl-imx.h"
29edad3b2aSLinus Walleij 
30edad3b2aSLinus Walleij /* The bits in CONFIG cell defined in binding doc*/
31edad3b2aSLinus Walleij #define IMX_NO_PAD_CTL	0x80000000	/* no pin config need */
32edad3b2aSLinus Walleij #define IMX_PAD_SION 0x40000000		/* set SION */
33edad3b2aSLinus Walleij 
34edad3b2aSLinus Walleij /**
35edad3b2aSLinus Walleij  * @dev: a pointer back to containing device
36edad3b2aSLinus Walleij  * @base: the offset to the controller in virtual memory
37edad3b2aSLinus Walleij  */
38edad3b2aSLinus Walleij struct imx_pinctrl {
39edad3b2aSLinus Walleij 	struct device *dev;
40edad3b2aSLinus Walleij 	struct pinctrl_dev *pctl;
41edad3b2aSLinus Walleij 	void __iomem *base;
42edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info;
43edad3b2aSLinus Walleij };
44edad3b2aSLinus Walleij 
45edad3b2aSLinus Walleij static const inline struct imx_pin_group *imx_pinctrl_find_group_by_name(
46edad3b2aSLinus Walleij 				const struct imx_pinctrl_soc_info *info,
47edad3b2aSLinus Walleij 				const char *name)
48edad3b2aSLinus Walleij {
49edad3b2aSLinus Walleij 	const struct imx_pin_group *grp = NULL;
50edad3b2aSLinus Walleij 	int i;
51edad3b2aSLinus Walleij 
52edad3b2aSLinus Walleij 	for (i = 0; i < info->ngroups; i++) {
53edad3b2aSLinus Walleij 		if (!strcmp(info->groups[i].name, name)) {
54edad3b2aSLinus Walleij 			grp = &info->groups[i];
55edad3b2aSLinus Walleij 			break;
56edad3b2aSLinus Walleij 		}
57edad3b2aSLinus Walleij 	}
58edad3b2aSLinus Walleij 
59edad3b2aSLinus Walleij 	return grp;
60edad3b2aSLinus Walleij }
61edad3b2aSLinus Walleij 
62edad3b2aSLinus Walleij static int imx_get_groups_count(struct pinctrl_dev *pctldev)
63edad3b2aSLinus Walleij {
64edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
65edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
66edad3b2aSLinus Walleij 
67edad3b2aSLinus Walleij 	return info->ngroups;
68edad3b2aSLinus Walleij }
69edad3b2aSLinus Walleij 
70edad3b2aSLinus Walleij static const char *imx_get_group_name(struct pinctrl_dev *pctldev,
71edad3b2aSLinus Walleij 				       unsigned selector)
72edad3b2aSLinus Walleij {
73edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
74edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
75edad3b2aSLinus Walleij 
76edad3b2aSLinus Walleij 	return info->groups[selector].name;
77edad3b2aSLinus Walleij }
78edad3b2aSLinus Walleij 
79edad3b2aSLinus Walleij static int imx_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
80edad3b2aSLinus Walleij 			       const unsigned **pins,
81edad3b2aSLinus Walleij 			       unsigned *npins)
82edad3b2aSLinus Walleij {
83edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
84edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
85edad3b2aSLinus Walleij 
86edad3b2aSLinus Walleij 	if (selector >= info->ngroups)
87edad3b2aSLinus Walleij 		return -EINVAL;
88edad3b2aSLinus Walleij 
89edad3b2aSLinus Walleij 	*pins = info->groups[selector].pin_ids;
90edad3b2aSLinus Walleij 	*npins = info->groups[selector].npins;
91edad3b2aSLinus Walleij 
92edad3b2aSLinus Walleij 	return 0;
93edad3b2aSLinus Walleij }
94edad3b2aSLinus Walleij 
95edad3b2aSLinus Walleij static void imx_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
96edad3b2aSLinus Walleij 		   unsigned offset)
97edad3b2aSLinus Walleij {
98edad3b2aSLinus Walleij 	seq_printf(s, "%s", dev_name(pctldev->dev));
99edad3b2aSLinus Walleij }
100edad3b2aSLinus Walleij 
101edad3b2aSLinus Walleij static int imx_dt_node_to_map(struct pinctrl_dev *pctldev,
102edad3b2aSLinus Walleij 			struct device_node *np,
103edad3b2aSLinus Walleij 			struct pinctrl_map **map, unsigned *num_maps)
104edad3b2aSLinus Walleij {
105edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
106edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
107edad3b2aSLinus Walleij 	const struct imx_pin_group *grp;
108edad3b2aSLinus Walleij 	struct pinctrl_map *new_map;
109edad3b2aSLinus Walleij 	struct device_node *parent;
110edad3b2aSLinus Walleij 	int map_num = 1;
111edad3b2aSLinus Walleij 	int i, j;
112edad3b2aSLinus Walleij 
113edad3b2aSLinus Walleij 	/*
114edad3b2aSLinus Walleij 	 * first find the group of this node and check if we need create
115edad3b2aSLinus Walleij 	 * config maps for pins
116edad3b2aSLinus Walleij 	 */
117edad3b2aSLinus Walleij 	grp = imx_pinctrl_find_group_by_name(info, np->name);
118edad3b2aSLinus Walleij 	if (!grp) {
119edad3b2aSLinus Walleij 		dev_err(info->dev, "unable to find group for node %s\n",
120edad3b2aSLinus Walleij 			np->name);
121edad3b2aSLinus Walleij 		return -EINVAL;
122edad3b2aSLinus Walleij 	}
123edad3b2aSLinus Walleij 
124edad3b2aSLinus Walleij 	for (i = 0; i < grp->npins; i++) {
125edad3b2aSLinus Walleij 		if (!(grp->pins[i].config & IMX_NO_PAD_CTL))
126edad3b2aSLinus Walleij 			map_num++;
127edad3b2aSLinus Walleij 	}
128edad3b2aSLinus Walleij 
129edad3b2aSLinus Walleij 	new_map = kmalloc(sizeof(struct pinctrl_map) * map_num, GFP_KERNEL);
130edad3b2aSLinus Walleij 	if (!new_map)
131edad3b2aSLinus Walleij 		return -ENOMEM;
132edad3b2aSLinus Walleij 
133edad3b2aSLinus Walleij 	*map = new_map;
134edad3b2aSLinus Walleij 	*num_maps = map_num;
135edad3b2aSLinus Walleij 
136edad3b2aSLinus Walleij 	/* create mux map */
137edad3b2aSLinus Walleij 	parent = of_get_parent(np);
138edad3b2aSLinus Walleij 	if (!parent) {
139edad3b2aSLinus Walleij 		kfree(new_map);
140edad3b2aSLinus Walleij 		return -EINVAL;
141edad3b2aSLinus Walleij 	}
142edad3b2aSLinus Walleij 	new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
143edad3b2aSLinus Walleij 	new_map[0].data.mux.function = parent->name;
144edad3b2aSLinus Walleij 	new_map[0].data.mux.group = np->name;
145edad3b2aSLinus Walleij 	of_node_put(parent);
146edad3b2aSLinus Walleij 
147edad3b2aSLinus Walleij 	/* create config map */
148edad3b2aSLinus Walleij 	new_map++;
149edad3b2aSLinus Walleij 	for (i = j = 0; i < grp->npins; i++) {
150edad3b2aSLinus Walleij 		if (!(grp->pins[i].config & IMX_NO_PAD_CTL)) {
151edad3b2aSLinus Walleij 			new_map[j].type = PIN_MAP_TYPE_CONFIGS_PIN;
152edad3b2aSLinus Walleij 			new_map[j].data.configs.group_or_pin =
153edad3b2aSLinus Walleij 					pin_get_name(pctldev, grp->pins[i].pin);
154edad3b2aSLinus Walleij 			new_map[j].data.configs.configs = &grp->pins[i].config;
155edad3b2aSLinus Walleij 			new_map[j].data.configs.num_configs = 1;
156edad3b2aSLinus Walleij 			j++;
157edad3b2aSLinus Walleij 		}
158edad3b2aSLinus Walleij 	}
159edad3b2aSLinus Walleij 
160edad3b2aSLinus Walleij 	dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
161edad3b2aSLinus Walleij 		(*map)->data.mux.function, (*map)->data.mux.group, map_num);
162edad3b2aSLinus Walleij 
163edad3b2aSLinus Walleij 	return 0;
164edad3b2aSLinus Walleij }
165edad3b2aSLinus Walleij 
166edad3b2aSLinus Walleij static void imx_dt_free_map(struct pinctrl_dev *pctldev,
167edad3b2aSLinus Walleij 				struct pinctrl_map *map, unsigned num_maps)
168edad3b2aSLinus Walleij {
169edad3b2aSLinus Walleij 	kfree(map);
170edad3b2aSLinus Walleij }
171edad3b2aSLinus Walleij 
172edad3b2aSLinus Walleij static const struct pinctrl_ops imx_pctrl_ops = {
173edad3b2aSLinus Walleij 	.get_groups_count = imx_get_groups_count,
174edad3b2aSLinus Walleij 	.get_group_name = imx_get_group_name,
175edad3b2aSLinus Walleij 	.get_group_pins = imx_get_group_pins,
176edad3b2aSLinus Walleij 	.pin_dbg_show = imx_pin_dbg_show,
177edad3b2aSLinus Walleij 	.dt_node_to_map = imx_dt_node_to_map,
178edad3b2aSLinus Walleij 	.dt_free_map = imx_dt_free_map,
179edad3b2aSLinus Walleij 
180edad3b2aSLinus Walleij };
181edad3b2aSLinus Walleij 
182edad3b2aSLinus Walleij static int imx_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
183edad3b2aSLinus Walleij 		       unsigned group)
184edad3b2aSLinus Walleij {
185edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
186edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
187edad3b2aSLinus Walleij 	const struct imx_pin_reg *pin_reg;
188edad3b2aSLinus Walleij 	unsigned int npins, pin_id;
189edad3b2aSLinus Walleij 	int i;
190edad3b2aSLinus Walleij 	struct imx_pin_group *grp;
191edad3b2aSLinus Walleij 
192edad3b2aSLinus Walleij 	/*
193edad3b2aSLinus Walleij 	 * Configure the mux mode for each pin in the group for a specific
194edad3b2aSLinus Walleij 	 * function.
195edad3b2aSLinus Walleij 	 */
196edad3b2aSLinus Walleij 	grp = &info->groups[group];
197edad3b2aSLinus Walleij 	npins = grp->npins;
198edad3b2aSLinus Walleij 
199edad3b2aSLinus Walleij 	dev_dbg(ipctl->dev, "enable function %s group %s\n",
200edad3b2aSLinus Walleij 		info->functions[selector].name, grp->name);
201edad3b2aSLinus Walleij 
202edad3b2aSLinus Walleij 	for (i = 0; i < npins; i++) {
203edad3b2aSLinus Walleij 		struct imx_pin *pin = &grp->pins[i];
204edad3b2aSLinus Walleij 		pin_id = pin->pin;
205edad3b2aSLinus Walleij 		pin_reg = &info->pin_regs[pin_id];
206edad3b2aSLinus Walleij 
2073dac1918SStefan Agner 		if (pin_reg->mux_reg == -1) {
208edad3b2aSLinus Walleij 			dev_err(ipctl->dev, "Pin(%s) does not support mux function\n",
209edad3b2aSLinus Walleij 				info->pins[pin_id].name);
210edad3b2aSLinus Walleij 			return -EINVAL;
211edad3b2aSLinus Walleij 		}
212edad3b2aSLinus Walleij 
213edad3b2aSLinus Walleij 		if (info->flags & SHARE_MUX_CONF_REG) {
214edad3b2aSLinus Walleij 			u32 reg;
215edad3b2aSLinus Walleij 			reg = readl(ipctl->base + pin_reg->mux_reg);
216edad3b2aSLinus Walleij 			reg &= ~(0x7 << 20);
217edad3b2aSLinus Walleij 			reg |= (pin->mux_mode << 20);
218edad3b2aSLinus Walleij 			writel(reg, ipctl->base + pin_reg->mux_reg);
219edad3b2aSLinus Walleij 		} else {
220edad3b2aSLinus Walleij 			writel(pin->mux_mode, ipctl->base + pin_reg->mux_reg);
221edad3b2aSLinus Walleij 		}
222edad3b2aSLinus Walleij 		dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%x\n",
223edad3b2aSLinus Walleij 			pin_reg->mux_reg, pin->mux_mode);
224edad3b2aSLinus Walleij 
225edad3b2aSLinus Walleij 		/*
226edad3b2aSLinus Walleij 		 * If the select input value begins with 0xff, it's a quirky
227edad3b2aSLinus Walleij 		 * select input and the value should be interpreted as below.
228edad3b2aSLinus Walleij 		 *     31     23      15      7        0
229edad3b2aSLinus Walleij 		 *     | 0xff | shift | width | select |
230edad3b2aSLinus Walleij 		 * It's used to work around the problem that the select
231edad3b2aSLinus Walleij 		 * input for some pin is not implemented in the select
232edad3b2aSLinus Walleij 		 * input register but in some general purpose register.
233edad3b2aSLinus Walleij 		 * We encode the select input value, width and shift of
234edad3b2aSLinus Walleij 		 * the bit field into input_val cell of pin function ID
235edad3b2aSLinus Walleij 		 * in device tree, and then decode them here for setting
236edad3b2aSLinus Walleij 		 * up the select input bits in general purpose register.
237edad3b2aSLinus Walleij 		 */
238edad3b2aSLinus Walleij 		if (pin->input_val >> 24 == 0xff) {
239edad3b2aSLinus Walleij 			u32 val = pin->input_val;
240edad3b2aSLinus Walleij 			u8 select = val & 0xff;
241edad3b2aSLinus Walleij 			u8 width = (val >> 8) & 0xff;
242edad3b2aSLinus Walleij 			u8 shift = (val >> 16) & 0xff;
243edad3b2aSLinus Walleij 			u32 mask = ((1 << width) - 1) << shift;
244edad3b2aSLinus Walleij 			/*
245edad3b2aSLinus Walleij 			 * The input_reg[i] here is actually some IOMUXC general
246edad3b2aSLinus Walleij 			 * purpose register, not regular select input register.
247edad3b2aSLinus Walleij 			 */
248edad3b2aSLinus Walleij 			val = readl(ipctl->base + pin->input_reg);
249edad3b2aSLinus Walleij 			val &= ~mask;
250edad3b2aSLinus Walleij 			val |= select << shift;
251edad3b2aSLinus Walleij 			writel(val, ipctl->base + pin->input_reg);
252edad3b2aSLinus Walleij 		} else if (pin->input_reg) {
253edad3b2aSLinus Walleij 			/*
254edad3b2aSLinus Walleij 			 * Regular select input register can never be at offset
255edad3b2aSLinus Walleij 			 * 0, and we only print register value for regular case.
256edad3b2aSLinus Walleij 			 */
257edad3b2aSLinus Walleij 			writel(pin->input_val, ipctl->base + pin->input_reg);
258edad3b2aSLinus Walleij 			dev_dbg(ipctl->dev,
259edad3b2aSLinus Walleij 				"==>select_input: offset 0x%x val 0x%x\n",
260edad3b2aSLinus Walleij 				pin->input_reg, pin->input_val);
261edad3b2aSLinus Walleij 		}
262edad3b2aSLinus Walleij 	}
263edad3b2aSLinus Walleij 
264edad3b2aSLinus Walleij 	return 0;
265edad3b2aSLinus Walleij }
266edad3b2aSLinus Walleij 
267edad3b2aSLinus Walleij static int imx_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
268edad3b2aSLinus Walleij {
269edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
270edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
271edad3b2aSLinus Walleij 
272edad3b2aSLinus Walleij 	return info->nfunctions;
273edad3b2aSLinus Walleij }
274edad3b2aSLinus Walleij 
275edad3b2aSLinus Walleij static const char *imx_pmx_get_func_name(struct pinctrl_dev *pctldev,
276edad3b2aSLinus Walleij 					  unsigned selector)
277edad3b2aSLinus Walleij {
278edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
279edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
280edad3b2aSLinus Walleij 
281edad3b2aSLinus Walleij 	return info->functions[selector].name;
282edad3b2aSLinus Walleij }
283edad3b2aSLinus Walleij 
284edad3b2aSLinus Walleij static int imx_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
285edad3b2aSLinus Walleij 			       const char * const **groups,
286edad3b2aSLinus Walleij 			       unsigned * const num_groups)
287edad3b2aSLinus Walleij {
288edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
289edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
290edad3b2aSLinus Walleij 
291edad3b2aSLinus Walleij 	*groups = info->functions[selector].groups;
292edad3b2aSLinus Walleij 	*num_groups = info->functions[selector].num_groups;
293edad3b2aSLinus Walleij 
294edad3b2aSLinus Walleij 	return 0;
295edad3b2aSLinus Walleij }
296edad3b2aSLinus Walleij 
2971f2b0452SStefan Agner static int imx_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
2981f2b0452SStefan Agner 			struct pinctrl_gpio_range *range, unsigned offset)
2991f2b0452SStefan Agner {
3001f2b0452SStefan Agner 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
3011f2b0452SStefan Agner 	const struct imx_pinctrl_soc_info *info = ipctl->info;
3021f2b0452SStefan Agner 	const struct imx_pin_reg *pin_reg;
3031f2b0452SStefan Agner 	struct imx_pin_group *grp;
3041f2b0452SStefan Agner 	struct imx_pin *imx_pin;
3051f2b0452SStefan Agner 	unsigned int pin, group;
3061f2b0452SStefan Agner 	u32 reg;
3071f2b0452SStefan Agner 
3081f2b0452SStefan Agner 	/* Currently implementation only for shared mux/conf register */
3091f2b0452SStefan Agner 	if (!(info->flags & SHARE_MUX_CONF_REG))
3101f2b0452SStefan Agner 		return -EINVAL;
3111f2b0452SStefan Agner 
3121f2b0452SStefan Agner 	pin_reg = &info->pin_regs[offset];
3131f2b0452SStefan Agner 	if (pin_reg->mux_reg == -1)
3141f2b0452SStefan Agner 		return -EINVAL;
3151f2b0452SStefan Agner 
3161f2b0452SStefan Agner 	/* Find the pinctrl config with GPIO mux mode for the requested pin */
3171f2b0452SStefan Agner 	for (group = 0; group < info->ngroups; group++) {
3181f2b0452SStefan Agner 		grp = &info->groups[group];
3191f2b0452SStefan Agner 		for (pin = 0; pin < grp->npins; pin++) {
3201f2b0452SStefan Agner 			imx_pin = &grp->pins[pin];
3211f2b0452SStefan Agner 			if (imx_pin->pin == offset && !imx_pin->mux_mode)
3221f2b0452SStefan Agner 				goto mux_pin;
3231f2b0452SStefan Agner 		}
3241f2b0452SStefan Agner 	}
3251f2b0452SStefan Agner 
3261f2b0452SStefan Agner 	return -EINVAL;
3271f2b0452SStefan Agner 
3281f2b0452SStefan Agner mux_pin:
3291f2b0452SStefan Agner 	reg = readl(ipctl->base + pin_reg->mux_reg);
3301f2b0452SStefan Agner 	reg &= ~(0x7 << 20);
3311f2b0452SStefan Agner 	reg |= imx_pin->config;
3321f2b0452SStefan Agner 	writel(reg, ipctl->base + pin_reg->mux_reg);
3331f2b0452SStefan Agner 
3341f2b0452SStefan Agner 	return 0;
3351f2b0452SStefan Agner }
3361f2b0452SStefan Agner 
3371f2b0452SStefan Agner static int imx_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
3381f2b0452SStefan Agner 	   struct pinctrl_gpio_range *range, unsigned offset, bool input)
3391f2b0452SStefan Agner {
3401f2b0452SStefan Agner 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
3411f2b0452SStefan Agner 	const struct imx_pinctrl_soc_info *info = ipctl->info;
3421f2b0452SStefan Agner 	const struct imx_pin_reg *pin_reg;
3431f2b0452SStefan Agner 	u32 reg;
3441f2b0452SStefan Agner 
3451f2b0452SStefan Agner 	/*
3461f2b0452SStefan Agner 	 * Only Vybrid has the input/output buffer enable flags (IBE/OBE)
3471f2b0452SStefan Agner 	 * They are part of the shared mux/conf register.
3481f2b0452SStefan Agner 	 */
3491f2b0452SStefan Agner 	if (!(info->flags & SHARE_MUX_CONF_REG))
3501f2b0452SStefan Agner 		return -EINVAL;
3511f2b0452SStefan Agner 
3521f2b0452SStefan Agner 	pin_reg = &info->pin_regs[offset];
3531f2b0452SStefan Agner 	if (pin_reg->mux_reg == -1)
3541f2b0452SStefan Agner 		return -EINVAL;
3551f2b0452SStefan Agner 
3561f2b0452SStefan Agner 	/* IBE always enabled allows us to read the value "on the wire" */
3571f2b0452SStefan Agner 	reg = readl(ipctl->base + pin_reg->mux_reg);
3581f2b0452SStefan Agner 	if (input)
3591f2b0452SStefan Agner 		reg &= ~0x2;
3601f2b0452SStefan Agner 	else
3611f2b0452SStefan Agner 		reg |= 0x2;
3621f2b0452SStefan Agner 	writel(reg, ipctl->base + pin_reg->mux_reg);
3631f2b0452SStefan Agner 
3641f2b0452SStefan Agner 	return 0;
3651f2b0452SStefan Agner }
3661f2b0452SStefan Agner 
367edad3b2aSLinus Walleij static const struct pinmux_ops imx_pmx_ops = {
368edad3b2aSLinus Walleij 	.get_functions_count = imx_pmx_get_funcs_count,
369edad3b2aSLinus Walleij 	.get_function_name = imx_pmx_get_func_name,
370edad3b2aSLinus Walleij 	.get_function_groups = imx_pmx_get_groups,
371edad3b2aSLinus Walleij 	.set_mux = imx_pmx_set,
3721f2b0452SStefan Agner 	.gpio_request_enable = imx_pmx_gpio_request_enable,
3731f2b0452SStefan Agner 	.gpio_set_direction = imx_pmx_gpio_set_direction,
374edad3b2aSLinus Walleij };
375edad3b2aSLinus Walleij 
376edad3b2aSLinus Walleij static int imx_pinconf_get(struct pinctrl_dev *pctldev,
377edad3b2aSLinus Walleij 			     unsigned pin_id, unsigned long *config)
378edad3b2aSLinus Walleij {
379edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
380edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
381edad3b2aSLinus Walleij 	const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
382edad3b2aSLinus Walleij 
3833dac1918SStefan Agner 	if (pin_reg->conf_reg == -1) {
384edad3b2aSLinus Walleij 		dev_err(info->dev, "Pin(%s) does not support config function\n",
385edad3b2aSLinus Walleij 			info->pins[pin_id].name);
386edad3b2aSLinus Walleij 		return -EINVAL;
387edad3b2aSLinus Walleij 	}
388edad3b2aSLinus Walleij 
389edad3b2aSLinus Walleij 	*config = readl(ipctl->base + pin_reg->conf_reg);
390edad3b2aSLinus Walleij 
391edad3b2aSLinus Walleij 	if (info->flags & SHARE_MUX_CONF_REG)
392edad3b2aSLinus Walleij 		*config &= 0xffff;
393edad3b2aSLinus Walleij 
394edad3b2aSLinus Walleij 	return 0;
395edad3b2aSLinus Walleij }
396edad3b2aSLinus Walleij 
397edad3b2aSLinus Walleij static int imx_pinconf_set(struct pinctrl_dev *pctldev,
398edad3b2aSLinus Walleij 			     unsigned pin_id, unsigned long *configs,
399edad3b2aSLinus Walleij 			     unsigned num_configs)
400edad3b2aSLinus Walleij {
401edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
402edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
403edad3b2aSLinus Walleij 	const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
404edad3b2aSLinus Walleij 	int i;
405edad3b2aSLinus Walleij 
4063dac1918SStefan Agner 	if (pin_reg->conf_reg == -1) {
407edad3b2aSLinus Walleij 		dev_err(info->dev, "Pin(%s) does not support config function\n",
408edad3b2aSLinus Walleij 			info->pins[pin_id].name);
409edad3b2aSLinus Walleij 		return -EINVAL;
410edad3b2aSLinus Walleij 	}
411edad3b2aSLinus Walleij 
412edad3b2aSLinus Walleij 	dev_dbg(ipctl->dev, "pinconf set pin %s\n",
413edad3b2aSLinus Walleij 		info->pins[pin_id].name);
414edad3b2aSLinus Walleij 
415edad3b2aSLinus Walleij 	for (i = 0; i < num_configs; i++) {
416edad3b2aSLinus Walleij 		if (info->flags & SHARE_MUX_CONF_REG) {
417edad3b2aSLinus Walleij 			u32 reg;
418edad3b2aSLinus Walleij 			reg = readl(ipctl->base + pin_reg->conf_reg);
419edad3b2aSLinus Walleij 			reg &= ~0xffff;
420edad3b2aSLinus Walleij 			reg |= configs[i];
421edad3b2aSLinus Walleij 			writel(reg, ipctl->base + pin_reg->conf_reg);
422edad3b2aSLinus Walleij 		} else {
423edad3b2aSLinus Walleij 			writel(configs[i], ipctl->base + pin_reg->conf_reg);
424edad3b2aSLinus Walleij 		}
425edad3b2aSLinus Walleij 		dev_dbg(ipctl->dev, "write: offset 0x%x val 0x%lx\n",
426edad3b2aSLinus Walleij 			pin_reg->conf_reg, configs[i]);
427edad3b2aSLinus Walleij 	} /* for each config */
428edad3b2aSLinus Walleij 
429edad3b2aSLinus Walleij 	return 0;
430edad3b2aSLinus Walleij }
431edad3b2aSLinus Walleij 
432edad3b2aSLinus Walleij static void imx_pinconf_dbg_show(struct pinctrl_dev *pctldev,
433edad3b2aSLinus Walleij 				   struct seq_file *s, unsigned pin_id)
434edad3b2aSLinus Walleij {
435edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
436edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
437edad3b2aSLinus Walleij 	const struct imx_pin_reg *pin_reg = &info->pin_regs[pin_id];
438edad3b2aSLinus Walleij 	unsigned long config;
439edad3b2aSLinus Walleij 
4404ff0f034SUwe Kleine-König 	if (!pin_reg || pin_reg->conf_reg == -1) {
441edad3b2aSLinus Walleij 		seq_printf(s, "N/A");
442edad3b2aSLinus Walleij 		return;
443edad3b2aSLinus Walleij 	}
444edad3b2aSLinus Walleij 
445edad3b2aSLinus Walleij 	config = readl(ipctl->base + pin_reg->conf_reg);
446edad3b2aSLinus Walleij 	seq_printf(s, "0x%lx", config);
447edad3b2aSLinus Walleij }
448edad3b2aSLinus Walleij 
449edad3b2aSLinus Walleij static void imx_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
450edad3b2aSLinus Walleij 					 struct seq_file *s, unsigned group)
451edad3b2aSLinus Walleij {
452edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = pinctrl_dev_get_drvdata(pctldev);
453edad3b2aSLinus Walleij 	const struct imx_pinctrl_soc_info *info = ipctl->info;
454edad3b2aSLinus Walleij 	struct imx_pin_group *grp;
455edad3b2aSLinus Walleij 	unsigned long config;
456edad3b2aSLinus Walleij 	const char *name;
457edad3b2aSLinus Walleij 	int i, ret;
458edad3b2aSLinus Walleij 
459edad3b2aSLinus Walleij 	if (group > info->ngroups)
460edad3b2aSLinus Walleij 		return;
461edad3b2aSLinus Walleij 
462edad3b2aSLinus Walleij 	seq_printf(s, "\n");
463edad3b2aSLinus Walleij 	grp = &info->groups[group];
464edad3b2aSLinus Walleij 	for (i = 0; i < grp->npins; i++) {
465edad3b2aSLinus Walleij 		struct imx_pin *pin = &grp->pins[i];
466edad3b2aSLinus Walleij 		name = pin_get_name(pctldev, pin->pin);
467edad3b2aSLinus Walleij 		ret = imx_pinconf_get(pctldev, pin->pin, &config);
468edad3b2aSLinus Walleij 		if (ret)
469edad3b2aSLinus Walleij 			return;
470edad3b2aSLinus Walleij 		seq_printf(s, "%s: 0x%lx", name, config);
471edad3b2aSLinus Walleij 	}
472edad3b2aSLinus Walleij }
473edad3b2aSLinus Walleij 
474edad3b2aSLinus Walleij static const struct pinconf_ops imx_pinconf_ops = {
475edad3b2aSLinus Walleij 	.pin_config_get = imx_pinconf_get,
476edad3b2aSLinus Walleij 	.pin_config_set = imx_pinconf_set,
477edad3b2aSLinus Walleij 	.pin_config_dbg_show = imx_pinconf_dbg_show,
478edad3b2aSLinus Walleij 	.pin_config_group_dbg_show = imx_pinconf_group_dbg_show,
479edad3b2aSLinus Walleij };
480edad3b2aSLinus Walleij 
481edad3b2aSLinus Walleij static struct pinctrl_desc imx_pinctrl_desc = {
482edad3b2aSLinus Walleij 	.pctlops = &imx_pctrl_ops,
483edad3b2aSLinus Walleij 	.pmxops = &imx_pmx_ops,
484edad3b2aSLinus Walleij 	.confops = &imx_pinconf_ops,
485edad3b2aSLinus Walleij 	.owner = THIS_MODULE,
486edad3b2aSLinus Walleij };
487edad3b2aSLinus Walleij 
488edad3b2aSLinus Walleij /*
489edad3b2aSLinus Walleij  * Each pin represented in fsl,pins consists of 5 u32 PIN_FUNC_ID and
490edad3b2aSLinus Walleij  * 1 u32 CONFIG, so 24 types in total for each pin.
491edad3b2aSLinus Walleij  */
492edad3b2aSLinus Walleij #define FSL_PIN_SIZE 24
493edad3b2aSLinus Walleij #define SHARE_FSL_PIN_SIZE 20
494edad3b2aSLinus Walleij 
495edad3b2aSLinus Walleij static int imx_pinctrl_parse_groups(struct device_node *np,
496edad3b2aSLinus Walleij 				    struct imx_pin_group *grp,
497edad3b2aSLinus Walleij 				    struct imx_pinctrl_soc_info *info,
498edad3b2aSLinus Walleij 				    u32 index)
499edad3b2aSLinus Walleij {
500edad3b2aSLinus Walleij 	int size, pin_size;
501edad3b2aSLinus Walleij 	const __be32 *list;
502edad3b2aSLinus Walleij 	int i;
503edad3b2aSLinus Walleij 	u32 config;
504edad3b2aSLinus Walleij 
505edad3b2aSLinus Walleij 	dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
506edad3b2aSLinus Walleij 
507edad3b2aSLinus Walleij 	if (info->flags & SHARE_MUX_CONF_REG)
508edad3b2aSLinus Walleij 		pin_size = SHARE_FSL_PIN_SIZE;
509edad3b2aSLinus Walleij 	else
510edad3b2aSLinus Walleij 		pin_size = FSL_PIN_SIZE;
511edad3b2aSLinus Walleij 	/* Initialise group */
512edad3b2aSLinus Walleij 	grp->name = np->name;
513edad3b2aSLinus Walleij 
514edad3b2aSLinus Walleij 	/*
515edad3b2aSLinus Walleij 	 * the binding format is fsl,pins = <PIN_FUNC_ID CONFIG ...>,
516edad3b2aSLinus Walleij 	 * do sanity check and calculate pins number
517edad3b2aSLinus Walleij 	 */
518edad3b2aSLinus Walleij 	list = of_get_property(np, "fsl,pins", &size);
519edad3b2aSLinus Walleij 	if (!list) {
520edad3b2aSLinus Walleij 		dev_err(info->dev, "no fsl,pins property in node %s\n", np->full_name);
521edad3b2aSLinus Walleij 		return -EINVAL;
522edad3b2aSLinus Walleij 	}
523edad3b2aSLinus Walleij 
524edad3b2aSLinus Walleij 	/* we do not check return since it's safe node passed down */
525edad3b2aSLinus Walleij 	if (!size || size % pin_size) {
526edad3b2aSLinus Walleij 		dev_err(info->dev, "Invalid fsl,pins property in node %s\n", np->full_name);
527edad3b2aSLinus Walleij 		return -EINVAL;
528edad3b2aSLinus Walleij 	}
529edad3b2aSLinus Walleij 
530edad3b2aSLinus Walleij 	grp->npins = size / pin_size;
531edad3b2aSLinus Walleij 	grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(struct imx_pin),
532edad3b2aSLinus Walleij 				GFP_KERNEL);
533edad3b2aSLinus Walleij 	grp->pin_ids = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
534edad3b2aSLinus Walleij 				GFP_KERNEL);
535edad3b2aSLinus Walleij 	if (!grp->pins || ! grp->pin_ids)
536edad3b2aSLinus Walleij 		return -ENOMEM;
537edad3b2aSLinus Walleij 
538edad3b2aSLinus Walleij 	for (i = 0; i < grp->npins; i++) {
539edad3b2aSLinus Walleij 		u32 mux_reg = be32_to_cpu(*list++);
540edad3b2aSLinus Walleij 		u32 conf_reg;
541edad3b2aSLinus Walleij 		unsigned int pin_id;
542edad3b2aSLinus Walleij 		struct imx_pin_reg *pin_reg;
543edad3b2aSLinus Walleij 		struct imx_pin *pin = &grp->pins[i];
544edad3b2aSLinus Walleij 
545*e7b37a52SAdrian Alonso 		if (!(info->flags & ZERO_OFFSET_VALID) && !mux_reg)
546*e7b37a52SAdrian Alonso 			mux_reg = -1;
547*e7b37a52SAdrian Alonso 
54816837f95SMarkus Pargmann 		if (info->flags & SHARE_MUX_CONF_REG) {
549edad3b2aSLinus Walleij 			conf_reg = mux_reg;
55016837f95SMarkus Pargmann 		} else {
551edad3b2aSLinus Walleij 			conf_reg = be32_to_cpu(*list++);
55216837f95SMarkus Pargmann 			if (!conf_reg)
55316837f95SMarkus Pargmann 				conf_reg = -1;
55416837f95SMarkus Pargmann 		}
555edad3b2aSLinus Walleij 
556*e7b37a52SAdrian Alonso 		pin_id = (mux_reg != -1) ? mux_reg / 4 : conf_reg / 4;
557edad3b2aSLinus Walleij 		pin_reg = &info->pin_regs[pin_id];
558edad3b2aSLinus Walleij 		pin->pin = pin_id;
559edad3b2aSLinus Walleij 		grp->pin_ids[i] = pin_id;
560edad3b2aSLinus Walleij 		pin_reg->mux_reg = mux_reg;
561edad3b2aSLinus Walleij 		pin_reg->conf_reg = conf_reg;
562edad3b2aSLinus Walleij 		pin->input_reg = be32_to_cpu(*list++);
563edad3b2aSLinus Walleij 		pin->mux_mode = be32_to_cpu(*list++);
564edad3b2aSLinus Walleij 		pin->input_val = be32_to_cpu(*list++);
565edad3b2aSLinus Walleij 
566edad3b2aSLinus Walleij 		/* SION bit is in mux register */
567edad3b2aSLinus Walleij 		config = be32_to_cpu(*list++);
568edad3b2aSLinus Walleij 		if (config & IMX_PAD_SION)
569edad3b2aSLinus Walleij 			pin->mux_mode |= IOMUXC_CONFIG_SION;
570edad3b2aSLinus Walleij 		pin->config = config & ~IMX_PAD_SION;
571edad3b2aSLinus Walleij 
572edad3b2aSLinus Walleij 		dev_dbg(info->dev, "%s: 0x%x 0x%08lx", info->pins[pin_id].name,
573edad3b2aSLinus Walleij 				pin->mux_mode, pin->config);
574edad3b2aSLinus Walleij 	}
575edad3b2aSLinus Walleij 
576edad3b2aSLinus Walleij 	return 0;
577edad3b2aSLinus Walleij }
578edad3b2aSLinus Walleij 
579edad3b2aSLinus Walleij static int imx_pinctrl_parse_functions(struct device_node *np,
580edad3b2aSLinus Walleij 				       struct imx_pinctrl_soc_info *info,
581edad3b2aSLinus Walleij 				       u32 index)
582edad3b2aSLinus Walleij {
583edad3b2aSLinus Walleij 	struct device_node *child;
584edad3b2aSLinus Walleij 	struct imx_pmx_func *func;
585edad3b2aSLinus Walleij 	struct imx_pin_group *grp;
586edad3b2aSLinus Walleij 	u32 i = 0;
587edad3b2aSLinus Walleij 
588edad3b2aSLinus Walleij 	dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
589edad3b2aSLinus Walleij 
590edad3b2aSLinus Walleij 	func = &info->functions[index];
591edad3b2aSLinus Walleij 
592edad3b2aSLinus Walleij 	/* Initialise function */
593edad3b2aSLinus Walleij 	func->name = np->name;
594edad3b2aSLinus Walleij 	func->num_groups = of_get_child_count(np);
595edad3b2aSLinus Walleij 	if (func->num_groups == 0) {
596edad3b2aSLinus Walleij 		dev_err(info->dev, "no groups defined in %s\n", np->full_name);
597edad3b2aSLinus Walleij 		return -EINVAL;
598edad3b2aSLinus Walleij 	}
599edad3b2aSLinus Walleij 	func->groups = devm_kzalloc(info->dev,
600edad3b2aSLinus Walleij 			func->num_groups * sizeof(char *), GFP_KERNEL);
601edad3b2aSLinus Walleij 
602edad3b2aSLinus Walleij 	for_each_child_of_node(np, child) {
603edad3b2aSLinus Walleij 		func->groups[i] = child->name;
604ee163518SRobin Gong 		grp = &info->groups[info->group_index++];
605edad3b2aSLinus Walleij 		imx_pinctrl_parse_groups(child, grp, info, i++);
606edad3b2aSLinus Walleij 	}
607edad3b2aSLinus Walleij 
608edad3b2aSLinus Walleij 	return 0;
609edad3b2aSLinus Walleij }
610edad3b2aSLinus Walleij 
6115fcdf6a7SMarkus Pargmann /*
6125fcdf6a7SMarkus Pargmann  * Check if the DT contains pins in the direct child nodes. This indicates the
6135fcdf6a7SMarkus Pargmann  * newer DT format to store pins. This function returns true if the first found
6145fcdf6a7SMarkus Pargmann  * fsl,pins property is in a child of np. Otherwise false is returned.
6155fcdf6a7SMarkus Pargmann  */
6165fcdf6a7SMarkus Pargmann static bool imx_pinctrl_dt_is_flat_functions(struct device_node *np)
6175fcdf6a7SMarkus Pargmann {
6185fcdf6a7SMarkus Pargmann 	struct device_node *function_np;
6195fcdf6a7SMarkus Pargmann 	struct device_node *pinctrl_np;
6205fcdf6a7SMarkus Pargmann 
6215fcdf6a7SMarkus Pargmann 	for_each_child_of_node(np, function_np) {
6225fcdf6a7SMarkus Pargmann 		if (of_property_read_bool(function_np, "fsl,pins"))
6235fcdf6a7SMarkus Pargmann 			return true;
6245fcdf6a7SMarkus Pargmann 
6255fcdf6a7SMarkus Pargmann 		for_each_child_of_node(function_np, pinctrl_np) {
6265fcdf6a7SMarkus Pargmann 			if (of_property_read_bool(pinctrl_np, "fsl,pins"))
6275fcdf6a7SMarkus Pargmann 				return false;
6285fcdf6a7SMarkus Pargmann 		}
6295fcdf6a7SMarkus Pargmann 	}
6305fcdf6a7SMarkus Pargmann 
6315fcdf6a7SMarkus Pargmann 	return true;
6325fcdf6a7SMarkus Pargmann }
6335fcdf6a7SMarkus Pargmann 
634edad3b2aSLinus Walleij static int imx_pinctrl_probe_dt(struct platform_device *pdev,
635edad3b2aSLinus Walleij 				struct imx_pinctrl_soc_info *info)
636edad3b2aSLinus Walleij {
637edad3b2aSLinus Walleij 	struct device_node *np = pdev->dev.of_node;
638edad3b2aSLinus Walleij 	struct device_node *child;
639edad3b2aSLinus Walleij 	u32 nfuncs = 0;
640edad3b2aSLinus Walleij 	u32 i = 0;
6415fcdf6a7SMarkus Pargmann 	bool flat_funcs;
642edad3b2aSLinus Walleij 
643edad3b2aSLinus Walleij 	if (!np)
644edad3b2aSLinus Walleij 		return -ENODEV;
645edad3b2aSLinus Walleij 
6465fcdf6a7SMarkus Pargmann 	flat_funcs = imx_pinctrl_dt_is_flat_functions(np);
6475fcdf6a7SMarkus Pargmann 	if (flat_funcs) {
6485fcdf6a7SMarkus Pargmann 		nfuncs = 1;
6495fcdf6a7SMarkus Pargmann 	} else {
650edad3b2aSLinus Walleij 		nfuncs = of_get_child_count(np);
651edad3b2aSLinus Walleij 		if (nfuncs <= 0) {
652edad3b2aSLinus Walleij 			dev_err(&pdev->dev, "no functions defined\n");
653edad3b2aSLinus Walleij 			return -EINVAL;
654edad3b2aSLinus Walleij 		}
6555fcdf6a7SMarkus Pargmann 	}
656edad3b2aSLinus Walleij 
657edad3b2aSLinus Walleij 	info->nfunctions = nfuncs;
658edad3b2aSLinus Walleij 	info->functions = devm_kzalloc(&pdev->dev, nfuncs * sizeof(struct imx_pmx_func),
659edad3b2aSLinus Walleij 					GFP_KERNEL);
660edad3b2aSLinus Walleij 	if (!info->functions)
661edad3b2aSLinus Walleij 		return -ENOMEM;
662edad3b2aSLinus Walleij 
6635fcdf6a7SMarkus Pargmann 	if (flat_funcs) {
6645fcdf6a7SMarkus Pargmann 		info->ngroups = of_get_child_count(np);
6655fcdf6a7SMarkus Pargmann 	} else {
666edad3b2aSLinus Walleij 		info->ngroups = 0;
667edad3b2aSLinus Walleij 		for_each_child_of_node(np, child)
668edad3b2aSLinus Walleij 			info->ngroups += of_get_child_count(child);
6695fcdf6a7SMarkus Pargmann 	}
670edad3b2aSLinus Walleij 	info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct imx_pin_group),
671edad3b2aSLinus Walleij 					GFP_KERNEL);
672edad3b2aSLinus Walleij 	if (!info->groups)
673edad3b2aSLinus Walleij 		return -ENOMEM;
674edad3b2aSLinus Walleij 
6755fcdf6a7SMarkus Pargmann 	if (flat_funcs) {
6765fcdf6a7SMarkus Pargmann 		imx_pinctrl_parse_functions(np, info, 0);
6775fcdf6a7SMarkus Pargmann 	} else {
678edad3b2aSLinus Walleij 		for_each_child_of_node(np, child)
679edad3b2aSLinus Walleij 			imx_pinctrl_parse_functions(child, info, i++);
6805fcdf6a7SMarkus Pargmann 	}
681edad3b2aSLinus Walleij 
682edad3b2aSLinus Walleij 	return 0;
683edad3b2aSLinus Walleij }
684edad3b2aSLinus Walleij 
685edad3b2aSLinus Walleij int imx_pinctrl_probe(struct platform_device *pdev,
686edad3b2aSLinus Walleij 		      struct imx_pinctrl_soc_info *info)
687edad3b2aSLinus Walleij {
688edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl;
689edad3b2aSLinus Walleij 	struct resource *res;
6904691dd01SStefan Agner 	int ret, i;
691edad3b2aSLinus Walleij 
692edad3b2aSLinus Walleij 	if (!info || !info->pins || !info->npins) {
693edad3b2aSLinus Walleij 		dev_err(&pdev->dev, "wrong pinctrl info\n");
694edad3b2aSLinus Walleij 		return -EINVAL;
695edad3b2aSLinus Walleij 	}
696edad3b2aSLinus Walleij 	info->dev = &pdev->dev;
697edad3b2aSLinus Walleij 
698edad3b2aSLinus Walleij 	/* Create state holders etc for this driver */
699edad3b2aSLinus Walleij 	ipctl = devm_kzalloc(&pdev->dev, sizeof(*ipctl), GFP_KERNEL);
700edad3b2aSLinus Walleij 	if (!ipctl)
701edad3b2aSLinus Walleij 		return -ENOMEM;
702edad3b2aSLinus Walleij 
7033dac1918SStefan Agner 	info->pin_regs = devm_kmalloc(&pdev->dev, sizeof(*info->pin_regs) *
704edad3b2aSLinus Walleij 				      info->npins, GFP_KERNEL);
705edad3b2aSLinus Walleij 	if (!info->pin_regs)
706edad3b2aSLinus Walleij 		return -ENOMEM;
7074691dd01SStefan Agner 
7084691dd01SStefan Agner 	for (i = 0; i < info->npins; i++) {
7094691dd01SStefan Agner 		info->pin_regs[i].mux_reg = -1;
7104691dd01SStefan Agner 		info->pin_regs[i].conf_reg = -1;
7114691dd01SStefan Agner 	}
712edad3b2aSLinus Walleij 
713edad3b2aSLinus Walleij 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
714edad3b2aSLinus Walleij 	ipctl->base = devm_ioremap_resource(&pdev->dev, res);
715edad3b2aSLinus Walleij 	if (IS_ERR(ipctl->base))
716edad3b2aSLinus Walleij 		return PTR_ERR(ipctl->base);
717edad3b2aSLinus Walleij 
718edad3b2aSLinus Walleij 	imx_pinctrl_desc.name = dev_name(&pdev->dev);
719edad3b2aSLinus Walleij 	imx_pinctrl_desc.pins = info->pins;
720edad3b2aSLinus Walleij 	imx_pinctrl_desc.npins = info->npins;
721edad3b2aSLinus Walleij 
722edad3b2aSLinus Walleij 	ret = imx_pinctrl_probe_dt(pdev, info);
723edad3b2aSLinus Walleij 	if (ret) {
724edad3b2aSLinus Walleij 		dev_err(&pdev->dev, "fail to probe dt properties\n");
725edad3b2aSLinus Walleij 		return ret;
726edad3b2aSLinus Walleij 	}
727edad3b2aSLinus Walleij 
728edad3b2aSLinus Walleij 	ipctl->info = info;
729edad3b2aSLinus Walleij 	ipctl->dev = info->dev;
730edad3b2aSLinus Walleij 	platform_set_drvdata(pdev, ipctl);
731edad3b2aSLinus Walleij 	ipctl->pctl = pinctrl_register(&imx_pinctrl_desc, &pdev->dev, ipctl);
732323de9efSMasahiro Yamada 	if (IS_ERR(ipctl->pctl)) {
733edad3b2aSLinus Walleij 		dev_err(&pdev->dev, "could not register IMX pinctrl driver\n");
734323de9efSMasahiro Yamada 		return PTR_ERR(ipctl->pctl);
735edad3b2aSLinus Walleij 	}
736edad3b2aSLinus Walleij 
737edad3b2aSLinus Walleij 	dev_info(&pdev->dev, "initialized IMX pinctrl driver\n");
738edad3b2aSLinus Walleij 
739edad3b2aSLinus Walleij 	return 0;
740edad3b2aSLinus Walleij }
741edad3b2aSLinus Walleij 
742edad3b2aSLinus Walleij int imx_pinctrl_remove(struct platform_device *pdev)
743edad3b2aSLinus Walleij {
744edad3b2aSLinus Walleij 	struct imx_pinctrl *ipctl = platform_get_drvdata(pdev);
745edad3b2aSLinus Walleij 
746edad3b2aSLinus Walleij 	pinctrl_unregister(ipctl->pctl);
747edad3b2aSLinus Walleij 
748edad3b2aSLinus Walleij 	return 0;
749edad3b2aSLinus Walleij }
750