blob: 8649ec3910a3e1c8be863340468e94ada76954c5 [file] [log] [blame]
Paul Mundtca5481c62012-07-10 12:08:14 +09001/*
2 * SuperH Pin Function Controller pinmux support.
3 *
4 * Copyright (C) 2012 Paul Mundt
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
Paul Mundt54407112012-07-20 16:18:21 +090010
Laurent Pinchartc6193ea2012-12-15 23:50:47 +010011#define DRV_NAME "sh-pfc"
Paul Mundtca5481c62012-07-10 12:08:14 +090012
Laurent Pinchart1724acf2012-12-15 23:50:48 +010013#include <linux/device.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010014#include <linux/err.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090015#include <linux/init.h>
16#include <linux/module.h>
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +020017#include <linux/of.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010018#include <linux/pinctrl/consumer.h>
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +020019#include <linux/pinctrl/machine.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010020#include <linux/pinctrl/pinconf.h>
21#include <linux/pinctrl/pinconf-generic.h>
22#include <linux/pinctrl/pinctrl.h>
23#include <linux/pinctrl/pinmux.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090024#include <linux/slab.h>
Paul Mundtd93a8912012-07-11 17:17:10 +090025#include <linux/spinlock.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090026
Laurent Pinchartf9165132012-12-15 23:50:44 +010027#include "core.h"
Laurent Pinchartc58d9c12013-03-10 16:44:02 +010028#include "../core.h"
29#include "../pinconf.h"
Laurent Pinchartf9165132012-12-15 23:50:44 +010030
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010031struct sh_pfc_pin_config {
32 u32 type;
33};
34
Paul Mundtca5481c62012-07-10 12:08:14 +090035struct sh_pfc_pinctrl {
36 struct pinctrl_dev *pctl;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +010037 struct pinctrl_desc pctl_desc;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +010038
Paul Mundtca5481c62012-07-10 12:08:14 +090039 struct sh_pfc *pfc;
40
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010041 struct pinctrl_pin_desc *pins;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010042 struct sh_pfc_pin_config *configs;
Paul Mundtca5481c62012-07-10 12:08:14 +090043};
44
Paul Mundte3f805e2012-07-17 15:48:18 +090045static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
Paul Mundtca5481c62012-07-10 12:08:14 +090046{
Paul Mundte3f805e2012-07-17 15:48:18 +090047 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
48
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010049 return pmx->pfc->info->nr_groups;
Paul Mundtca5481c62012-07-10 12:08:14 +090050}
51
Paul Mundte3f805e2012-07-17 15:48:18 +090052static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
Paul Mundtca5481c62012-07-10 12:08:14 +090053 unsigned selector)
54{
Paul Mundte3f805e2012-07-17 15:48:18 +090055 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
56
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010057 return pmx->pfc->info->groups[selector].name;
Paul Mundtca5481c62012-07-10 12:08:14 +090058}
59
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010060static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +090061 const unsigned **pins, unsigned *num_pins)
62{
Paul Mundte3f805e2012-07-17 15:48:18 +090063 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
64
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010065 *pins = pmx->pfc->info->groups[selector].pins;
66 *num_pins = pmx->pfc->info->groups[selector].nr_pins;
Paul Mundte3f805e2012-07-17 15:48:18 +090067
68 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +090069}
70
Paul Mundtfdd85ec2012-07-20 16:39:09 +090071static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
72 unsigned offset)
73{
74 seq_printf(s, "%s", DRV_NAME);
75}
76
Laurent Pinchart3a8d63d2013-06-19 13:26:02 +020077#ifdef CONFIG_OF
Laurent Pinchart12f3ad82013-06-17 20:50:03 +020078static int sh_pfc_map_add_config(struct pinctrl_map *map,
79 const char *group_or_pin,
80 enum pinctrl_map_type type,
81 unsigned long *configs,
82 unsigned int num_configs)
83{
84 unsigned long *cfgs;
85
86 cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
87 GFP_KERNEL);
88 if (cfgs == NULL)
89 return -ENOMEM;
90
91 map->type = type;
92 map->data.configs.group_or_pin = group_or_pin;
93 map->data.configs.configs = cfgs;
94 map->data.configs.num_configs = num_configs;
95
96 return 0;
97}
98
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +020099static int sh_pfc_dt_subnode_to_map(struct device *dev, struct device_node *np,
100 struct pinctrl_map **map,
101 unsigned int *num_maps, unsigned int *index)
102{
103 struct pinctrl_map *maps = *map;
104 unsigned int nmaps = *num_maps;
105 unsigned int idx = *index;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200106 unsigned int num_configs;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200107 const char *function = NULL;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200108 unsigned long *configs;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200109 struct property *prop;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200110 unsigned int num_groups;
111 unsigned int num_pins;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200112 const char *group;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200113 const char *pin;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200114 int ret;
115
116 /* Parse the function and configuration properties. At least a function
117 * or one configuration must be specified.
118 */
119 ret = of_property_read_string(np, "renesas,function", &function);
120 if (ret < 0 && ret != -EINVAL) {
121 dev_err(dev, "Invalid function in DT\n");
122 return ret;
123 }
124
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200125 ret = pinconf_generic_parse_dt_config(np, &configs, &num_configs);
126 if (ret < 0)
127 return ret;
128
129 if (!function && num_configs == 0) {
130 dev_err(dev,
131 "DT node must contain at least a function or config\n");
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200132 goto done;
133 }
134
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200135 /* Count the number of pins and groups and reallocate mappings. */
136 ret = of_property_count_strings(np, "renesas,pins");
137 if (ret == -EINVAL) {
138 num_pins = 0;
139 } else if (ret < 0) {
140 dev_err(dev, "Invalid pins list in DT\n");
141 goto done;
142 } else {
143 num_pins = ret;
144 }
145
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200146 ret = of_property_count_strings(np, "renesas,groups");
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200147 if (ret == -EINVAL) {
148 num_groups = 0;
149 } else if (ret < 0) {
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200150 dev_err(dev, "Invalid pin groups list in DT\n");
151 goto done;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200152 } else {
153 num_groups = ret;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200154 }
155
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200156 if (!num_pins && !num_groups) {
157 dev_err(dev, "No pin or group provided in DT node\n");
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200158 ret = -ENODEV;
159 goto done;
160 }
161
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200162 if (function)
163 nmaps += num_groups;
164 if (configs)
165 nmaps += num_pins + num_groups;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200166
167 maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
168 if (maps == NULL) {
169 ret = -ENOMEM;
170 goto done;
171 }
172
173 *map = maps;
174 *num_maps = nmaps;
175
176 /* Iterate over pins and groups and create the mappings. */
177 of_property_for_each_string(np, "renesas,groups", prop, group) {
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200178 if (function) {
179 maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
180 maps[idx].data.mux.group = group;
181 maps[idx].data.mux.function = function;
182 idx++;
183 }
184
185 if (configs) {
186 ret = sh_pfc_map_add_config(&maps[idx], group,
187 PIN_MAP_TYPE_CONFIGS_GROUP,
188 configs, num_configs);
189 if (ret < 0)
190 goto done;
191
192 idx++;
193 }
194 }
195
196 if (!configs) {
197 ret = 0;
198 goto done;
199 }
200
201 of_property_for_each_string(np, "renesas,pins", prop, pin) {
202 ret = sh_pfc_map_add_config(&maps[idx], pin,
203 PIN_MAP_TYPE_CONFIGS_PIN,
204 configs, num_configs);
205 if (ret < 0)
206 goto done;
207
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200208 idx++;
209 }
210
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200211done:
212 *index = idx;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200213 kfree(configs);
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200214 return ret;
215}
216
217static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
218 struct pinctrl_map *map, unsigned num_maps)
219{
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200220 unsigned int i;
221
222 if (map == NULL)
223 return;
224
225 for (i = 0; i < num_maps; ++i) {
226 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
227 map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
228 kfree(map[i].data.configs.configs);
229 }
230
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200231 kfree(map);
232}
233
234static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
235 struct device_node *np,
236 struct pinctrl_map **map, unsigned *num_maps)
237{
238 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
239 struct device *dev = pmx->pfc->dev;
240 struct device_node *child;
241 unsigned int index;
242 int ret;
243
244 *map = NULL;
245 *num_maps = 0;
246 index = 0;
247
248 for_each_child_of_node(np, child) {
249 ret = sh_pfc_dt_subnode_to_map(dev, child, map, num_maps,
250 &index);
251 if (ret < 0)
252 goto done;
253 }
254
255 /* If no mapping has been found in child nodes try the config node. */
256 if (*num_maps == 0) {
257 ret = sh_pfc_dt_subnode_to_map(dev, np, map, num_maps, &index);
258 if (ret < 0)
259 goto done;
260 }
261
262 if (*num_maps)
263 return 0;
264
265 dev_err(dev, "no mapping found in node %s\n", np->full_name);
266 ret = -EINVAL;
267
268done:
269 if (ret < 0)
270 sh_pfc_dt_free_map(pctldev, *map, *num_maps);
271
272 return ret;
273}
Laurent Pinchart3a8d63d2013-06-19 13:26:02 +0200274#endif /* CONFIG_OF */
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200275
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100276static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
Paul Mundte3f805e2012-07-17 15:48:18 +0900277 .get_groups_count = sh_pfc_get_groups_count,
278 .get_group_name = sh_pfc_get_group_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900279 .get_group_pins = sh_pfc_get_group_pins,
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900280 .pin_dbg_show = sh_pfc_pin_dbg_show,
Laurent Pinchart3a8d63d2013-06-19 13:26:02 +0200281#ifdef CONFIG_OF
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200282 .dt_node_to_map = sh_pfc_dt_node_to_map,
283 .dt_free_map = sh_pfc_dt_free_map,
Laurent Pinchart3a8d63d2013-06-19 13:26:02 +0200284#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900285};
286
Paul Mundtd93a8912012-07-11 17:17:10 +0900287static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
288{
289 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtca5481c62012-07-10 12:08:14 +0900290
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100291 return pmx->pfc->info->nr_functions;
Paul Mundtd93a8912012-07-11 17:17:10 +0900292}
293
294static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
295 unsigned selector)
296{
297 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
298
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100299 return pmx->pfc->info->functions[selector].name;
Paul Mundtd93a8912012-07-11 17:17:10 +0900300}
301
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100302static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
303 unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900304 const char * const **groups,
305 unsigned * const num_groups)
306{
Paul Mundtd93a8912012-07-11 17:17:10 +0900307 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
308
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100309 *groups = pmx->pfc->info->functions[selector].groups;
310 *num_groups = pmx->pfc->info->functions[selector].nr_groups;
Paul Mundtd93a8912012-07-11 17:17:10 +0900311
Paul Mundtca5481c62012-07-10 12:08:14 +0900312 return 0;
313}
314
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100315static int sh_pfc_func_enable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900316 unsigned group)
317{
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100318 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
319 struct sh_pfc *pfc = pmx->pfc;
320 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
321 unsigned long flags;
322 unsigned int i;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100323 int ret = 0;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100324
325 spin_lock_irqsave(&pfc->lock, flags);
326
327 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100328 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
329 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
330
331 if (cfg->type != PINMUX_TYPE_NONE) {
332 ret = -EBUSY;
333 goto done;
334 }
335 }
336
337 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchartb705c052013-03-10 16:38:23 +0100338 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
339 if (ret < 0)
340 break;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100341 }
342
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100343done:
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100344 spin_unlock_irqrestore(&pfc->lock, flags);
345 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900346}
347
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100348static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900349 unsigned group)
350{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100351 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
352 struct sh_pfc *pfc = pmx->pfc;
353 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
354 unsigned long flags;
355 unsigned int i;
356
357 spin_lock_irqsave(&pfc->lock, flags);
358
359 for (i = 0; i < grp->nr_pins; ++i) {
360 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
361 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
362
363 cfg->type = PINMUX_TYPE_NONE;
364 }
365
366 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900367}
368
369static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
370 struct pinctrl_gpio_range *range,
371 unsigned offset)
372{
373 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
374 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100375 int idx = sh_pfc_get_pin_index(pfc, offset);
376 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Paul Mundtca5481c62012-07-10 12:08:14 +0900377 unsigned long flags;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100378 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900379
380 spin_lock_irqsave(&pfc->lock, flags);
381
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100382 if (cfg->type != PINMUX_TYPE_NONE) {
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100383 dev_err(pfc->dev,
384 "Pin %u is busy, can't configure it as GPIO.\n",
385 offset);
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100386 ret = -EBUSY;
387 goto done;
Paul Mundtd93a8912012-07-11 17:17:10 +0900388 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900389
Laurent Pincharte3c470512013-03-10 17:30:25 +0100390 if (!pfc->gpio) {
391 /* If GPIOs are handled externally the pin mux type need to be
392 * set to GPIO here.
393 */
394 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
395
396 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
397 if (ret < 0)
398 goto done;
399 }
400
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100401 cfg->type = PINMUX_TYPE_GPIO;
402
Paul Mundtca5481c62012-07-10 12:08:14 +0900403 ret = 0;
404
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100405done:
Paul Mundtca5481c62012-07-10 12:08:14 +0900406 spin_unlock_irqrestore(&pfc->lock, flags);
407
408 return ret;
409}
410
411static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
412 struct pinctrl_gpio_range *range,
413 unsigned offset)
414{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100415 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
416 struct sh_pfc *pfc = pmx->pfc;
417 int idx = sh_pfc_get_pin_index(pfc, offset);
418 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
419 unsigned long flags;
420
421 spin_lock_irqsave(&pfc->lock, flags);
422 cfg->type = PINMUX_TYPE_NONE;
423 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900424}
425
426static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
427 struct pinctrl_gpio_range *range,
428 unsigned offset, bool input)
429{
430 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100431 struct sh_pfc *pfc = pmx->pfc;
432 int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
433 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100434 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100435 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100436 unsigned long flags;
Laurent Pinchart6dc9b452013-03-13 18:18:30 +0100437 unsigned int dir;
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100438 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900439
Laurent Pinchart6dc9b452013-03-13 18:18:30 +0100440 /* Check if the requested direction is supported by the pin. Not all SoC
441 * provide pin config data, so perform the check conditionally.
442 */
443 if (pin->configs) {
444 dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
445 if (!(pin->configs & dir))
446 return -EINVAL;
447 }
448
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100449 spin_lock_irqsave(&pfc->lock, flags);
450
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100451 ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100452 if (ret < 0)
453 goto done;
454
455 cfg->type = new_type;
456
457done:
458 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100459 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900460}
461
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100462static const struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900463 .get_functions_count = sh_pfc_get_functions_count,
464 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900465 .get_function_groups = sh_pfc_get_function_groups,
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100466 .enable = sh_pfc_func_enable,
467 .disable = sh_pfc_func_disable,
Paul Mundtca5481c62012-07-10 12:08:14 +0900468 .gpio_request_enable = sh_pfc_gpio_request_enable,
469 .gpio_disable_free = sh_pfc_gpio_disable_free,
470 .gpio_set_direction = sh_pfc_gpio_set_direction,
471};
472
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100473/* Check whether the requested parameter is supported for a pin. */
474static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
475 enum pin_config_param param)
476{
477 int idx = sh_pfc_get_pin_index(pfc, _pin);
478 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
479
480 switch (param) {
481 case PIN_CONFIG_BIAS_DISABLE:
482 return true;
483
484 case PIN_CONFIG_BIAS_PULL_UP:
485 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
486
487 case PIN_CONFIG_BIAS_PULL_DOWN:
488 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
489
490 default:
491 return false;
492 }
493}
494
Laurent Pinchart934cb022013-02-14 22:35:09 +0100495static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900496 unsigned long *config)
497{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900498 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
499 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100500 enum pin_config_param param = pinconf_to_config_param(*config);
501 unsigned long flags;
502 unsigned int bias;
Paul Mundtd93a8912012-07-11 17:17:10 +0900503
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100504 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
505 return -ENOTSUPP;
506
507 switch (param) {
508 case PIN_CONFIG_BIAS_DISABLE:
509 case PIN_CONFIG_BIAS_PULL_UP:
510 case PIN_CONFIG_BIAS_PULL_DOWN:
511 if (!pfc->info->ops || !pfc->info->ops->get_bias)
512 return -ENOTSUPP;
513
514 spin_lock_irqsave(&pfc->lock, flags);
515 bias = pfc->info->ops->get_bias(pfc, _pin);
516 spin_unlock_irqrestore(&pfc->lock, flags);
517
518 if (bias != param)
519 return -EINVAL;
520
521 *config = 0;
522 break;
523
524 default:
525 return -ENOTSUPP;
526 }
Paul Mundtd93a8912012-07-11 17:17:10 +0900527
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900528 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900529}
530
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100531static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900532 unsigned long config)
533{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900534 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100535 struct sh_pfc *pfc = pmx->pfc;
536 enum pin_config_param param = pinconf_to_config_param(config);
537 unsigned long flags;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900538
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100539 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
540 return -ENOTSUPP;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900541
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100542 switch (param) {
543 case PIN_CONFIG_BIAS_PULL_UP:
544 case PIN_CONFIG_BIAS_PULL_DOWN:
545 case PIN_CONFIG_BIAS_DISABLE:
546 if (!pfc->info->ops || !pfc->info->ops->set_bias)
547 return -ENOTSUPP;
548
549 spin_lock_irqsave(&pfc->lock, flags);
550 pfc->info->ops->set_bias(pfc, _pin, param);
551 spin_unlock_irqrestore(&pfc->lock, flags);
552
553 break;
554
555 default:
556 return -ENOTSUPP;
557 }
558
559 return 0;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900560}
561
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100562static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
563 unsigned long config)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900564{
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100565 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
566 const unsigned int *pins;
567 unsigned int num_pins;
568 unsigned int i;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900569
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100570 pins = pmx->pfc->info->groups[group].pins;
571 num_pins = pmx->pfc->info->groups[group].nr_pins;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900572
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100573 for (i = 0; i < num_pins; ++i)
574 sh_pfc_pinconf_set(pctldev, pins[i], config);
575
576 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900577}
578
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100579static const struct pinconf_ops sh_pfc_pinconf_ops = {
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100580 .is_generic = true,
581 .pin_config_get = sh_pfc_pinconf_get,
582 .pin_config_set = sh_pfc_pinconf_set,
583 .pin_config_group_set = sh_pfc_pinconf_group_set,
584 .pin_config_config_dbg_show = pinconf_generic_dump_config,
Paul Mundtca5481c62012-07-10 12:08:14 +0900585};
586
Laurent Pinchart63d57382013-02-15 01:33:38 +0100587/* PFC ranges -> pinctrl pin descs */
588static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtca5481c62012-07-10 12:08:14 +0900589{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100590 unsigned int i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900591
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200592 /* Allocate and initialize the pins and configs arrays. */
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100593 pmx->pins = devm_kzalloc(pfc->dev,
594 sizeof(*pmx->pins) * pfc->info->nr_pins,
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100595 GFP_KERNEL);
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100596 if (unlikely(!pmx->pins))
Paul Mundtca5481c62012-07-10 12:08:14 +0900597 return -ENOMEM;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100598
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100599 pmx->configs = devm_kzalloc(pfc->dev,
600 sizeof(*pmx->configs) * pfc->info->nr_pins,
601 GFP_KERNEL);
602 if (unlikely(!pmx->configs))
603 return -ENOMEM;
604
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200605 for (i = 0; i < pfc->info->nr_pins; ++i) {
606 const struct sh_pfc_pin *info = &pfc->info->pins[i];
607 struct sh_pfc_pin_config *cfg = &pmx->configs[i];
608 struct pinctrl_pin_desc *pin = &pmx->pins[i];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100609
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200610 /* If the pin number is equal to -1 all pins are considered */
611 pin->number = info->pin != (u16)-1 ? info->pin : i;
612 pin->name = info->name;
613 cfg->type = PINMUX_TYPE_NONE;
Paul Mundtca5481c62012-07-10 12:08:14 +0900614 }
615
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200616 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900617}
618
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100619int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900620{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100621 struct sh_pfc_pinctrl *pmx;
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200622 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900623
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100624 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100625 if (unlikely(!pmx))
626 return -ENOMEM;
Paul Mundtca5481c62012-07-10 12:08:14 +0900627
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100628 pmx->pfc = pfc;
629 pfc->pinctrl = pmx;
630
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200631 ret = sh_pfc_map_pins(pfc, pmx);
632 if (ret < 0)
633 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900634
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100635 pmx->pctl_desc.name = DRV_NAME;
636 pmx->pctl_desc.owner = THIS_MODULE;
637 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
638 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
639 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100640 pmx->pctl_desc.pins = pmx->pins;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100641 pmx->pctl_desc.npins = pfc->info->nr_pins;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100642
643 pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
Wei Yongjunfd9d05b2013-03-11 22:08:12 +0800644 if (pmx->pctl == NULL)
645 return -EINVAL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900646
Paul Mundtca5481c62012-07-10 12:08:14 +0900647 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900648}
649
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100650int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900651{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100652 struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
Paul Mundtca5481c62012-07-10 12:08:14 +0900653
Paul Mundtca5481c62012-07-10 12:08:14 +0900654 pinctrl_unregister(pmx->pctl);
655
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100656 pfc->pinctrl = NULL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900657 return 0;
658}