1deda8287SViresh Kumar /*
2deda8287SViresh Kumar * Driver for the ST Microelectronics SPEAr pinmux
3deda8287SViresh Kumar *
4deda8287SViresh Kumar * Copyright (C) 2012 ST Microelectronics
5da89947bSViresh Kumar * Viresh Kumar <vireshk@kernel.org>
6deda8287SViresh Kumar *
7deda8287SViresh Kumar * Inspired from:
8deda8287SViresh Kumar * - U300 Pinctl drivers
9deda8287SViresh Kumar * - Tegra Pinctl drivers
10deda8287SViresh Kumar *
11deda8287SViresh Kumar * This file is licensed under the terms of the GNU General Public
12deda8287SViresh Kumar * License version 2. This program is licensed "as is" without any
13deda8287SViresh Kumar * warranty of any kind, whether express or implied.
14deda8287SViresh Kumar */
15deda8287SViresh Kumar
16deda8287SViresh Kumar #include <linux/err.h>
17d11db044SHerve Codina #include <linux/mfd/syscon.h>
18deda8287SViresh Kumar #include <linux/module.h>
19deda8287SViresh Kumar #include <linux/of.h>
20deda8287SViresh Kumar #include <linux/of_address.h>
21f993216dSAndy Shevchenko #include <linux/platform_device.h>
22f993216dSAndy Shevchenko #include <linux/seq_file.h>
23f993216dSAndy Shevchenko #include <linux/slab.h>
24f993216dSAndy Shevchenko
25deda8287SViresh Kumar #include <linux/pinctrl/machine.h>
26deda8287SViresh Kumar #include <linux/pinctrl/pinctrl.h>
27deda8287SViresh Kumar #include <linux/pinctrl/pinmux.h>
28deda8287SViresh Kumar
29deda8287SViresh Kumar #include "pinctrl-spear.h"
30deda8287SViresh Kumar
31deda8287SViresh Kumar #define DRIVER_NAME "spear-pinmux"
32deda8287SViresh Kumar
muxregs_endisable(struct spear_pmx * pmx,struct spear_muxreg * muxregs,u8 count,bool enable)33f4f8e563SViresh Kumar static void muxregs_endisable(struct spear_pmx *pmx,
34f4f8e563SViresh Kumar struct spear_muxreg *muxregs, u8 count, bool enable)
35f4f8e563SViresh Kumar {
36f4f8e563SViresh Kumar struct spear_muxreg *muxreg;
37f4f8e563SViresh Kumar u32 val, temp, j;
38f4f8e563SViresh Kumar
39f4f8e563SViresh Kumar for (j = 0; j < count; j++) {
40f4f8e563SViresh Kumar muxreg = &muxregs[j];
41f4f8e563SViresh Kumar
42f4f8e563SViresh Kumar val = pmx_readl(pmx, muxreg->reg);
43f4f8e563SViresh Kumar val &= ~muxreg->mask;
44f4f8e563SViresh Kumar
45f4f8e563SViresh Kumar if (enable)
46f4f8e563SViresh Kumar temp = muxreg->val;
47f4f8e563SViresh Kumar else
48f4f8e563SViresh Kumar temp = ~muxreg->val;
49f4f8e563SViresh Kumar
50f4f8e563SViresh Kumar val |= muxreg->mask & temp;
51f4f8e563SViresh Kumar pmx_writel(pmx, val, muxreg->reg);
52f4f8e563SViresh Kumar }
53f4f8e563SViresh Kumar }
54f4f8e563SViresh Kumar
set_mode(struct spear_pmx * pmx,int mode)55deda8287SViresh Kumar static int set_mode(struct spear_pmx *pmx, int mode)
56deda8287SViresh Kumar {
57deda8287SViresh Kumar struct spear_pmx_mode *pmx_mode = NULL;
58deda8287SViresh Kumar int i;
59deda8287SViresh Kumar u32 val;
60deda8287SViresh Kumar
61deda8287SViresh Kumar if (!pmx->machdata->pmx_modes || !pmx->machdata->npmx_modes)
62deda8287SViresh Kumar return -EINVAL;
63deda8287SViresh Kumar
64deda8287SViresh Kumar for (i = 0; i < pmx->machdata->npmx_modes; i++) {
65deda8287SViresh Kumar if (pmx->machdata->pmx_modes[i]->mode == (1 << mode)) {
66deda8287SViresh Kumar pmx_mode = pmx->machdata->pmx_modes[i];
67deda8287SViresh Kumar break;
68deda8287SViresh Kumar }
69deda8287SViresh Kumar }
70deda8287SViresh Kumar
71deda8287SViresh Kumar if (!pmx_mode)
72deda8287SViresh Kumar return -EINVAL;
73deda8287SViresh Kumar
74deda8287SViresh Kumar val = pmx_readl(pmx, pmx_mode->reg);
75deda8287SViresh Kumar val &= ~pmx_mode->mask;
76deda8287SViresh Kumar val |= pmx_mode->val;
77deda8287SViresh Kumar pmx_writel(pmx, val, pmx_mode->reg);
78deda8287SViresh Kumar
79deda8287SViresh Kumar pmx->machdata->mode = pmx_mode->mode;
80deda8287SViresh Kumar dev_info(pmx->dev, "Configured Mode: %s with id: %x\n\n",
81deda8287SViresh Kumar pmx_mode->name ? pmx_mode->name : "no_name",
82deda8287SViresh Kumar pmx_mode->reg);
83deda8287SViresh Kumar
84deda8287SViresh Kumar return 0;
85deda8287SViresh Kumar }
86deda8287SViresh Kumar
pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup * gpio_pingroup,unsigned count,u16 reg)87150632b0SGreg Kroah-Hartman void pmx_init_gpio_pingroup_addr(struct spear_gpio_pingroup *gpio_pingroup,
88f4f8e563SViresh Kumar unsigned count, u16 reg)
89f4f8e563SViresh Kumar {
904484d0b1SAxel Lin int i, j;
91f4f8e563SViresh Kumar
924484d0b1SAxel Lin for (i = 0; i < count; i++)
934484d0b1SAxel Lin for (j = 0; j < gpio_pingroup[i].nmuxregs; j++)
94f4f8e563SViresh Kumar gpio_pingroup[i].muxregs[j].reg = reg;
95f4f8e563SViresh Kumar }
96f4f8e563SViresh Kumar
pmx_init_addr(struct spear_pinctrl_machdata * machdata,u16 reg)97150632b0SGreg Kroah-Hartman void pmx_init_addr(struct spear_pinctrl_machdata *machdata, u16 reg)
98deda8287SViresh Kumar {
99deda8287SViresh Kumar struct spear_pingroup *pgroup;
100deda8287SViresh Kumar struct spear_modemux *modemux;
101deda8287SViresh Kumar int i, j, group;
102deda8287SViresh Kumar
103deda8287SViresh Kumar for (group = 0; group < machdata->ngroups; group++) {
104deda8287SViresh Kumar pgroup = machdata->groups[group];
105deda8287SViresh Kumar
106deda8287SViresh Kumar for (i = 0; i < pgroup->nmodemuxs; i++) {
107deda8287SViresh Kumar modemux = &pgroup->modemuxs[i];
108deda8287SViresh Kumar
109deda8287SViresh Kumar for (j = 0; j < modemux->nmuxregs; j++)
110deda8287SViresh Kumar if (modemux->muxregs[j].reg == 0xFFFF)
111deda8287SViresh Kumar modemux->muxregs[j].reg = reg;
112deda8287SViresh Kumar }
113deda8287SViresh Kumar }
114deda8287SViresh Kumar }
115deda8287SViresh Kumar
spear_pinctrl_get_groups_cnt(struct pinctrl_dev * pctldev)116deda8287SViresh Kumar static int spear_pinctrl_get_groups_cnt(struct pinctrl_dev *pctldev)
117deda8287SViresh Kumar {
118deda8287SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
119deda8287SViresh Kumar
120deda8287SViresh Kumar return pmx->machdata->ngroups;
121deda8287SViresh Kumar }
122deda8287SViresh Kumar
spear_pinctrl_get_group_name(struct pinctrl_dev * pctldev,unsigned group)123deda8287SViresh Kumar static const char *spear_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
124deda8287SViresh Kumar unsigned group)
125deda8287SViresh Kumar {
126deda8287SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
127deda8287SViresh Kumar
128deda8287SViresh Kumar return pmx->machdata->groups[group]->name;
129deda8287SViresh Kumar }
130deda8287SViresh Kumar
spear_pinctrl_get_group_pins(struct pinctrl_dev * pctldev,unsigned group,const unsigned ** pins,unsigned * num_pins)131deda8287SViresh Kumar static int spear_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
132deda8287SViresh Kumar unsigned group, const unsigned **pins, unsigned *num_pins)
133deda8287SViresh Kumar {
134deda8287SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
135deda8287SViresh Kumar
136deda8287SViresh Kumar *pins = pmx->machdata->groups[group]->pins;
137deda8287SViresh Kumar *num_pins = pmx->machdata->groups[group]->npins;
138deda8287SViresh Kumar
139deda8287SViresh Kumar return 0;
140deda8287SViresh Kumar }
141deda8287SViresh Kumar
spear_pinctrl_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned offset)142deda8287SViresh Kumar static void spear_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev,
143deda8287SViresh Kumar struct seq_file *s, unsigned offset)
144deda8287SViresh Kumar {
145deda8287SViresh Kumar seq_printf(s, " " DRIVER_NAME);
146deda8287SViresh Kumar }
147deda8287SViresh Kumar
spear_pinctrl_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np_config,struct pinctrl_map ** map,unsigned * num_maps)1489b2b1740SAxel Lin static int spear_pinctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
149deda8287SViresh Kumar struct device_node *np_config,
1509b2b1740SAxel Lin struct pinctrl_map **map,
1519b2b1740SAxel Lin unsigned *num_maps)
152deda8287SViresh Kumar {
153deda8287SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
154deda8287SViresh Kumar struct property *prop;
155deda8287SViresh Kumar const char *function, *group;
156deda8287SViresh Kumar int ret, index = 0, count = 0;
157deda8287SViresh Kumar
158deda8287SViresh Kumar /* calculate number of maps required */
159*8c5dc2a5SPeng Fan for_each_child_of_node_scoped(np_config, np) {
160deda8287SViresh Kumar ret = of_property_read_string(np, "st,function", &function);
161*8c5dc2a5SPeng Fan if (ret < 0)
162deda8287SViresh Kumar return ret;
163deda8287SViresh Kumar
164deda8287SViresh Kumar ret = of_property_count_strings(np, "st,pins");
165*8c5dc2a5SPeng Fan if (ret < 0)
166deda8287SViresh Kumar return ret;
167deda8287SViresh Kumar
168deda8287SViresh Kumar count += ret;
169deda8287SViresh Kumar }
170deda8287SViresh Kumar
171deda8287SViresh Kumar if (!count) {
172deda8287SViresh Kumar dev_err(pmx->dev, "No child nodes passed via DT\n");
173deda8287SViresh Kumar return -ENODEV;
174deda8287SViresh Kumar }
175deda8287SViresh Kumar
1766396bb22SKees Cook *map = kcalloc(count, sizeof(**map), GFP_KERNEL);
177deda8287SViresh Kumar if (!*map)
178deda8287SViresh Kumar return -ENOMEM;
179deda8287SViresh Kumar
180*8c5dc2a5SPeng Fan for_each_child_of_node_scoped(np_config, np) {
181deda8287SViresh Kumar of_property_read_string(np, "st,function", &function);
182deda8287SViresh Kumar of_property_for_each_string(np, "st,pins", prop, group) {
183deda8287SViresh Kumar (*map)[index].type = PIN_MAP_TYPE_MUX_GROUP;
184deda8287SViresh Kumar (*map)[index].data.mux.group = group;
185deda8287SViresh Kumar (*map)[index].data.mux.function = function;
186deda8287SViresh Kumar index++;
187deda8287SViresh Kumar }
188deda8287SViresh Kumar }
189deda8287SViresh Kumar
190deda8287SViresh Kumar *num_maps = count;
191deda8287SViresh Kumar
192deda8287SViresh Kumar return 0;
193deda8287SViresh Kumar }
194deda8287SViresh Kumar
spear_pinctrl_dt_free_map(struct pinctrl_dev * pctldev,struct pinctrl_map * map,unsigned num_maps)1959b2b1740SAxel Lin static void spear_pinctrl_dt_free_map(struct pinctrl_dev *pctldev,
1969b2b1740SAxel Lin struct pinctrl_map *map,
1979b2b1740SAxel Lin unsigned num_maps)
198deda8287SViresh Kumar {
199deda8287SViresh Kumar kfree(map);
200deda8287SViresh Kumar }
201deda8287SViresh Kumar
202022ab148SLaurent Pinchart static const struct pinctrl_ops spear_pinctrl_ops = {
203deda8287SViresh Kumar .get_groups_count = spear_pinctrl_get_groups_cnt,
204deda8287SViresh Kumar .get_group_name = spear_pinctrl_get_group_name,
205deda8287SViresh Kumar .get_group_pins = spear_pinctrl_get_group_pins,
206deda8287SViresh Kumar .pin_dbg_show = spear_pinctrl_pin_dbg_show,
207deda8287SViresh Kumar .dt_node_to_map = spear_pinctrl_dt_node_to_map,
208deda8287SViresh Kumar .dt_free_map = spear_pinctrl_dt_free_map,
209deda8287SViresh Kumar };
210deda8287SViresh Kumar
spear_pinctrl_get_funcs_count(struct pinctrl_dev * pctldev)211deda8287SViresh Kumar static int spear_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
212deda8287SViresh Kumar {
213deda8287SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
214deda8287SViresh Kumar
215deda8287SViresh Kumar return pmx->machdata->nfunctions;
216deda8287SViresh Kumar }
217deda8287SViresh Kumar
spear_pinctrl_get_func_name(struct pinctrl_dev * pctldev,unsigned function)218deda8287SViresh Kumar static const char *spear_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
219deda8287SViresh Kumar unsigned function)
220deda8287SViresh Kumar {
221deda8287SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
222deda8287SViresh Kumar
223deda8287SViresh Kumar return pmx->machdata->functions[function]->name;
224deda8287SViresh Kumar }
225deda8287SViresh Kumar
spear_pinctrl_get_func_groups(struct pinctrl_dev * pctldev,unsigned function,const char * const ** groups,unsigned * const ngroups)226deda8287SViresh Kumar static int spear_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
227deda8287SViresh Kumar unsigned function, const char *const **groups,
228deda8287SViresh Kumar unsigned * const ngroups)
229deda8287SViresh Kumar {
230deda8287SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
231deda8287SViresh Kumar
232deda8287SViresh Kumar *groups = pmx->machdata->functions[function]->groups;
233deda8287SViresh Kumar *ngroups = pmx->machdata->functions[function]->ngroups;
234deda8287SViresh Kumar
235deda8287SViresh Kumar return 0;
236deda8287SViresh Kumar }
237deda8287SViresh Kumar
spear_pinctrl_endisable(struct pinctrl_dev * pctldev,unsigned function,unsigned group,bool enable)238deda8287SViresh Kumar static int spear_pinctrl_endisable(struct pinctrl_dev *pctldev,
239deda8287SViresh Kumar unsigned function, unsigned group, bool enable)
240deda8287SViresh Kumar {
241deda8287SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
242deda8287SViresh Kumar const struct spear_pingroup *pgroup;
243deda8287SViresh Kumar const struct spear_modemux *modemux;
244f4f8e563SViresh Kumar int i;
245deda8287SViresh Kumar bool found = false;
246deda8287SViresh Kumar
247deda8287SViresh Kumar pgroup = pmx->machdata->groups[group];
248deda8287SViresh Kumar
249deda8287SViresh Kumar for (i = 0; i < pgroup->nmodemuxs; i++) {
250deda8287SViresh Kumar modemux = &pgroup->modemuxs[i];
251deda8287SViresh Kumar
252deda8287SViresh Kumar /* SoC have any modes */
253deda8287SViresh Kumar if (pmx->machdata->modes_supported) {
254deda8287SViresh Kumar if (!(pmx->machdata->mode & modemux->modes))
255deda8287SViresh Kumar continue;
256deda8287SViresh Kumar }
257deda8287SViresh Kumar
258deda8287SViresh Kumar found = true;
259f4f8e563SViresh Kumar muxregs_endisable(pmx, modemux->muxregs, modemux->nmuxregs,
260f4f8e563SViresh Kumar enable);
261deda8287SViresh Kumar }
262deda8287SViresh Kumar
263deda8287SViresh Kumar if (!found) {
264deda8287SViresh Kumar dev_err(pmx->dev, "pinmux group: %s not supported\n",
265deda8287SViresh Kumar pgroup->name);
266deda8287SViresh Kumar return -ENODEV;
267deda8287SViresh Kumar }
268deda8287SViresh Kumar
269deda8287SViresh Kumar return 0;
270deda8287SViresh Kumar }
271deda8287SViresh Kumar
spear_pinctrl_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)27203e9f0caSLinus Walleij static int spear_pinctrl_set_mux(struct pinctrl_dev *pctldev, unsigned function,
273deda8287SViresh Kumar unsigned group)
274deda8287SViresh Kumar {
275deda8287SViresh Kumar return spear_pinctrl_endisable(pctldev, function, group, true);
276deda8287SViresh Kumar }
277deda8287SViresh Kumar
278f4f8e563SViresh Kumar /* gpio with pinmux */
get_gpio_pingroup(struct spear_pmx * pmx,unsigned pin)279f4f8e563SViresh Kumar static struct spear_gpio_pingroup *get_gpio_pingroup(struct spear_pmx *pmx,
280f4f8e563SViresh Kumar unsigned pin)
281f4f8e563SViresh Kumar {
282f4f8e563SViresh Kumar struct spear_gpio_pingroup *gpio_pingroup;
2836f37b1b4SAxel Lin int i, j;
284f4f8e563SViresh Kumar
285f4f8e563SViresh Kumar if (!pmx->machdata->gpio_pingroups)
286f4f8e563SViresh Kumar return NULL;
287f4f8e563SViresh Kumar
2886f37b1b4SAxel Lin for (i = 0; i < pmx->machdata->ngpio_pingroups; i++) {
289f4f8e563SViresh Kumar gpio_pingroup = &pmx->machdata->gpio_pingroups[i];
290f4f8e563SViresh Kumar
291f4f8e563SViresh Kumar for (j = 0; j < gpio_pingroup->npins; j++) {
292f4f8e563SViresh Kumar if (gpio_pingroup->pins[j] == pin)
293f4f8e563SViresh Kumar return gpio_pingroup;
294f4f8e563SViresh Kumar }
295f4f8e563SViresh Kumar }
296f4f8e563SViresh Kumar
2976f37b1b4SAxel Lin return NULL;
298f4f8e563SViresh Kumar }
299f4f8e563SViresh Kumar
gpio_request_endisable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset,bool enable)300f4f8e563SViresh Kumar static int gpio_request_endisable(struct pinctrl_dev *pctldev,
301f4f8e563SViresh Kumar struct pinctrl_gpio_range *range, unsigned offset, bool enable)
302f4f8e563SViresh Kumar {
303f4f8e563SViresh Kumar struct spear_pmx *pmx = pinctrl_dev_get_drvdata(pctldev);
304826d6ca8SShiraz Hashim struct spear_pinctrl_machdata *machdata = pmx->machdata;
305f4f8e563SViresh Kumar struct spear_gpio_pingroup *gpio_pingroup;
306f4f8e563SViresh Kumar
307826d6ca8SShiraz Hashim /*
308826d6ca8SShiraz Hashim * Some SoC have configuration options applicable to group of pins,
309826d6ca8SShiraz Hashim * rather than a single pin.
310826d6ca8SShiraz Hashim */
311f4f8e563SViresh Kumar gpio_pingroup = get_gpio_pingroup(pmx, offset);
312f4f8e563SViresh Kumar if (gpio_pingroup)
313f4f8e563SViresh Kumar muxregs_endisable(pmx, gpio_pingroup->muxregs,
314f4f8e563SViresh Kumar gpio_pingroup->nmuxregs, enable);
315f4f8e563SViresh Kumar
316826d6ca8SShiraz Hashim /*
317826d6ca8SShiraz Hashim * SoC may need some extra configurations, or configurations for single
318826d6ca8SShiraz Hashim * pin
319826d6ca8SShiraz Hashim */
320826d6ca8SShiraz Hashim if (machdata->gpio_request_endisable)
321826d6ca8SShiraz Hashim machdata->gpio_request_endisable(pmx, offset, enable);
322826d6ca8SShiraz Hashim
323f4f8e563SViresh Kumar return 0;
324f4f8e563SViresh Kumar }
325f4f8e563SViresh Kumar
gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)326f4f8e563SViresh Kumar static int gpio_request_enable(struct pinctrl_dev *pctldev,
327f4f8e563SViresh Kumar struct pinctrl_gpio_range *range, unsigned offset)
328f4f8e563SViresh Kumar {
329f4f8e563SViresh Kumar return gpio_request_endisable(pctldev, range, offset, true);
330f4f8e563SViresh Kumar }
331f4f8e563SViresh Kumar
gpio_disable_free(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)332f4f8e563SViresh Kumar static void gpio_disable_free(struct pinctrl_dev *pctldev,
333f4f8e563SViresh Kumar struct pinctrl_gpio_range *range, unsigned offset)
334f4f8e563SViresh Kumar {
335f4f8e563SViresh Kumar gpio_request_endisable(pctldev, range, offset, false);
336f4f8e563SViresh Kumar }
337f4f8e563SViresh Kumar
338022ab148SLaurent Pinchart static const struct pinmux_ops spear_pinmux_ops = {
339deda8287SViresh Kumar .get_functions_count = spear_pinctrl_get_funcs_count,
340deda8287SViresh Kumar .get_function_name = spear_pinctrl_get_func_name,
341deda8287SViresh Kumar .get_function_groups = spear_pinctrl_get_func_groups,
34203e9f0caSLinus Walleij .set_mux = spear_pinctrl_set_mux,
343f4f8e563SViresh Kumar .gpio_request_enable = gpio_request_enable,
344f4f8e563SViresh Kumar .gpio_disable_free = gpio_disable_free,
345deda8287SViresh Kumar };
346deda8287SViresh Kumar
347deda8287SViresh Kumar static struct pinctrl_desc spear_pinctrl_desc = {
348deda8287SViresh Kumar .name = DRIVER_NAME,
349deda8287SViresh Kumar .pctlops = &spear_pinctrl_ops,
350deda8287SViresh Kumar .pmxops = &spear_pinmux_ops,
351deda8287SViresh Kumar .owner = THIS_MODULE,
352deda8287SViresh Kumar };
353deda8287SViresh Kumar
spear_pinctrl_probe(struct platform_device * pdev,struct spear_pinctrl_machdata * machdata)354150632b0SGreg Kroah-Hartman int spear_pinctrl_probe(struct platform_device *pdev,
355deda8287SViresh Kumar struct spear_pinctrl_machdata *machdata)
356deda8287SViresh Kumar {
357deda8287SViresh Kumar struct device_node *np = pdev->dev.of_node;
358deda8287SViresh Kumar struct spear_pmx *pmx;
359deda8287SViresh Kumar
360deda8287SViresh Kumar if (!machdata)
361deda8287SViresh Kumar return -ENODEV;
362deda8287SViresh Kumar
363deda8287SViresh Kumar pmx = devm_kzalloc(&pdev->dev, sizeof(*pmx), GFP_KERNEL);
36451d7a036SMarkus Elfring if (!pmx)
365deda8287SViresh Kumar return -ENOMEM;
366deda8287SViresh Kumar
367d11db044SHerve Codina pmx->regmap = device_node_to_regmap(np);
368d11db044SHerve Codina if (IS_ERR(pmx->regmap)) {
369d11db044SHerve Codina dev_err(&pdev->dev, "Init regmap failed (%pe).\n",
370d11db044SHerve Codina pmx->regmap);
371d11db044SHerve Codina return PTR_ERR(pmx->regmap);
372d11db044SHerve Codina }
373deda8287SViresh Kumar
374deda8287SViresh Kumar pmx->dev = &pdev->dev;
375deda8287SViresh Kumar pmx->machdata = machdata;
376deda8287SViresh Kumar
377deda8287SViresh Kumar /* configure mode, if supported by SoC */
378deda8287SViresh Kumar if (machdata->modes_supported) {
379deda8287SViresh Kumar int mode = 0;
380deda8287SViresh Kumar
381deda8287SViresh Kumar if (of_property_read_u32(np, "st,pinmux-mode", &mode)) {
382deda8287SViresh Kumar dev_err(&pdev->dev, "OF: pinmux mode not passed\n");
383deda8287SViresh Kumar return -EINVAL;
384deda8287SViresh Kumar }
385deda8287SViresh Kumar
386deda8287SViresh Kumar if (set_mode(pmx, mode)) {
387deda8287SViresh Kumar dev_err(&pdev->dev, "OF: Couldn't configure mode: %x\n",
388deda8287SViresh Kumar mode);
389deda8287SViresh Kumar return -EINVAL;
390deda8287SViresh Kumar }
391deda8287SViresh Kumar }
392deda8287SViresh Kumar
393deda8287SViresh Kumar platform_set_drvdata(pdev, pmx);
394deda8287SViresh Kumar
395deda8287SViresh Kumar spear_pinctrl_desc.pins = machdata->pins;
396deda8287SViresh Kumar spear_pinctrl_desc.npins = machdata->npins;
397deda8287SViresh Kumar
398d39de313SLaxman Dewangan pmx->pctl = devm_pinctrl_register(&pdev->dev, &spear_pinctrl_desc, pmx);
399323de9efSMasahiro Yamada if (IS_ERR(pmx->pctl)) {
400deda8287SViresh Kumar dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
401323de9efSMasahiro Yamada return PTR_ERR(pmx->pctl);
402deda8287SViresh Kumar }
403deda8287SViresh Kumar
404deda8287SViresh Kumar return 0;
405deda8287SViresh Kumar }
406