blob: 2cf23476adf84ab25aa4b125f32eaca04d37e155 [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 Pinchart12f3ad82013-06-17 20:50:03 +020077static int sh_pfc_map_add_config(struct pinctrl_map *map,
78 const char *group_or_pin,
79 enum pinctrl_map_type type,
80 unsigned long *configs,
81 unsigned int num_configs)
82{
83 unsigned long *cfgs;
84
85 cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
86 GFP_KERNEL);
87 if (cfgs == NULL)
88 return -ENOMEM;
89
90 map->type = type;
91 map->data.configs.group_or_pin = group_or_pin;
92 map->data.configs.configs = cfgs;
93 map->data.configs.num_configs = num_configs;
94
95 return 0;
96}
97
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +020098static int sh_pfc_dt_subnode_to_map(struct device *dev, struct device_node *np,
99 struct pinctrl_map **map,
100 unsigned int *num_maps, unsigned int *index)
101{
102 struct pinctrl_map *maps = *map;
103 unsigned int nmaps = *num_maps;
104 unsigned int idx = *index;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200105 unsigned int num_configs;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200106 const char *function = NULL;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200107 unsigned long *configs;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200108 struct property *prop;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200109 unsigned int num_groups;
110 unsigned int num_pins;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200111 const char *group;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200112 const char *pin;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200113 int ret;
114
115 /* Parse the function and configuration properties. At least a function
116 * or one configuration must be specified.
117 */
118 ret = of_property_read_string(np, "renesas,function", &function);
119 if (ret < 0 && ret != -EINVAL) {
120 dev_err(dev, "Invalid function in DT\n");
121 return ret;
122 }
123
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200124 ret = pinconf_generic_parse_dt_config(np, &configs, &num_configs);
125 if (ret < 0)
126 return ret;
127
128 if (!function && num_configs == 0) {
129 dev_err(dev,
130 "DT node must contain at least a function or config\n");
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200131 goto done;
132 }
133
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200134 /* Count the number of pins and groups and reallocate mappings. */
135 ret = of_property_count_strings(np, "renesas,pins");
136 if (ret == -EINVAL) {
137 num_pins = 0;
138 } else if (ret < 0) {
139 dev_err(dev, "Invalid pins list in DT\n");
140 goto done;
141 } else {
142 num_pins = ret;
143 }
144
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200145 ret = of_property_count_strings(np, "renesas,groups");
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200146 if (ret == -EINVAL) {
147 num_groups = 0;
148 } else if (ret < 0) {
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200149 dev_err(dev, "Invalid pin groups list in DT\n");
150 goto done;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200151 } else {
152 num_groups = ret;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200153 }
154
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200155 if (!num_pins && !num_groups) {
156 dev_err(dev, "No pin or group provided in DT node\n");
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200157 ret = -ENODEV;
158 goto done;
159 }
160
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200161 if (function)
162 nmaps += num_groups;
163 if (configs)
164 nmaps += num_pins + num_groups;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200165
166 maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
167 if (maps == NULL) {
168 ret = -ENOMEM;
169 goto done;
170 }
171
172 *map = maps;
173 *num_maps = nmaps;
174
175 /* Iterate over pins and groups and create the mappings. */
176 of_property_for_each_string(np, "renesas,groups", prop, group) {
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200177 if (function) {
178 maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
179 maps[idx].data.mux.group = group;
180 maps[idx].data.mux.function = function;
181 idx++;
182 }
183
184 if (configs) {
185 ret = sh_pfc_map_add_config(&maps[idx], group,
186 PIN_MAP_TYPE_CONFIGS_GROUP,
187 configs, num_configs);
188 if (ret < 0)
189 goto done;
190
191 idx++;
192 }
193 }
194
195 if (!configs) {
196 ret = 0;
197 goto done;
198 }
199
200 of_property_for_each_string(np, "renesas,pins", prop, pin) {
201 ret = sh_pfc_map_add_config(&maps[idx], pin,
202 PIN_MAP_TYPE_CONFIGS_PIN,
203 configs, num_configs);
204 if (ret < 0)
205 goto done;
206
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200207 idx++;
208 }
209
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200210done:
211 *index = idx;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200212 kfree(configs);
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200213 return ret;
214}
215
216static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
217 struct pinctrl_map *map, unsigned num_maps)
218{
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200219 unsigned int i;
220
221 if (map == NULL)
222 return;
223
224 for (i = 0; i < num_maps; ++i) {
225 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
226 map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
227 kfree(map[i].data.configs.configs);
228 }
229
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200230 kfree(map);
231}
232
233static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
234 struct device_node *np,
235 struct pinctrl_map **map, unsigned *num_maps)
236{
237 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
238 struct device *dev = pmx->pfc->dev;
239 struct device_node *child;
240 unsigned int index;
241 int ret;
242
243 *map = NULL;
244 *num_maps = 0;
245 index = 0;
246
247 for_each_child_of_node(np, child) {
248 ret = sh_pfc_dt_subnode_to_map(dev, child, map, num_maps,
249 &index);
250 if (ret < 0)
251 goto done;
252 }
253
254 /* If no mapping has been found in child nodes try the config node. */
255 if (*num_maps == 0) {
256 ret = sh_pfc_dt_subnode_to_map(dev, np, map, num_maps, &index);
257 if (ret < 0)
258 goto done;
259 }
260
261 if (*num_maps)
262 return 0;
263
264 dev_err(dev, "no mapping found in node %s\n", np->full_name);
265 ret = -EINVAL;
266
267done:
268 if (ret < 0)
269 sh_pfc_dt_free_map(pctldev, *map, *num_maps);
270
271 return ret;
272}
273
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100274static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
Paul Mundte3f805e2012-07-17 15:48:18 +0900275 .get_groups_count = sh_pfc_get_groups_count,
276 .get_group_name = sh_pfc_get_group_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900277 .get_group_pins = sh_pfc_get_group_pins,
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900278 .pin_dbg_show = sh_pfc_pin_dbg_show,
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200279 .dt_node_to_map = sh_pfc_dt_node_to_map,
280 .dt_free_map = sh_pfc_dt_free_map,
Paul Mundtca5481c62012-07-10 12:08:14 +0900281};
282
Paul Mundtd93a8912012-07-11 17:17:10 +0900283static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
284{
285 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtca5481c62012-07-10 12:08:14 +0900286
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100287 return pmx->pfc->info->nr_functions;
Paul Mundtd93a8912012-07-11 17:17:10 +0900288}
289
290static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
291 unsigned selector)
292{
293 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
294
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100295 return pmx->pfc->info->functions[selector].name;
Paul Mundtd93a8912012-07-11 17:17:10 +0900296}
297
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100298static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
299 unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900300 const char * const **groups,
301 unsigned * const num_groups)
302{
Paul Mundtd93a8912012-07-11 17:17:10 +0900303 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
304
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100305 *groups = pmx->pfc->info->functions[selector].groups;
306 *num_groups = pmx->pfc->info->functions[selector].nr_groups;
Paul Mundtd93a8912012-07-11 17:17:10 +0900307
Paul Mundtca5481c62012-07-10 12:08:14 +0900308 return 0;
309}
310
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100311static int sh_pfc_func_enable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900312 unsigned group)
313{
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100314 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
315 struct sh_pfc *pfc = pmx->pfc;
316 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
317 unsigned long flags;
318 unsigned int i;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100319 int ret = 0;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100320
321 spin_lock_irqsave(&pfc->lock, flags);
322
323 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100324 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
325 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
326
327 if (cfg->type != PINMUX_TYPE_NONE) {
328 ret = -EBUSY;
329 goto done;
330 }
331 }
332
333 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchartb705c052013-03-10 16:38:23 +0100334 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
335 if (ret < 0)
336 break;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100337 }
338
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100339done:
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100340 spin_unlock_irqrestore(&pfc->lock, flags);
341 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900342}
343
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100344static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900345 unsigned group)
346{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100347 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
348 struct sh_pfc *pfc = pmx->pfc;
349 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
350 unsigned long flags;
351 unsigned int i;
352
353 spin_lock_irqsave(&pfc->lock, flags);
354
355 for (i = 0; i < grp->nr_pins; ++i) {
356 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
357 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
358
359 cfg->type = PINMUX_TYPE_NONE;
360 }
361
362 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900363}
364
365static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
366 struct pinctrl_gpio_range *range,
367 unsigned offset)
368{
369 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
370 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100371 int idx = sh_pfc_get_pin_index(pfc, offset);
372 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Paul Mundtca5481c62012-07-10 12:08:14 +0900373 unsigned long flags;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100374 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900375
376 spin_lock_irqsave(&pfc->lock, flags);
377
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100378 if (cfg->type != PINMUX_TYPE_NONE) {
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100379 dev_err(pfc->dev,
380 "Pin %u is busy, can't configure it as GPIO.\n",
381 offset);
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100382 ret = -EBUSY;
383 goto done;
Paul Mundtd93a8912012-07-11 17:17:10 +0900384 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900385
Laurent Pincharte3c470512013-03-10 17:30:25 +0100386 if (!pfc->gpio) {
387 /* If GPIOs are handled externally the pin mux type need to be
388 * set to GPIO here.
389 */
390 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
391
392 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
393 if (ret < 0)
394 goto done;
395 }
396
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100397 cfg->type = PINMUX_TYPE_GPIO;
398
Paul Mundtca5481c62012-07-10 12:08:14 +0900399 ret = 0;
400
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100401done:
Paul Mundtca5481c62012-07-10 12:08:14 +0900402 spin_unlock_irqrestore(&pfc->lock, flags);
403
404 return ret;
405}
406
407static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
408 struct pinctrl_gpio_range *range,
409 unsigned offset)
410{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100411 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
412 struct sh_pfc *pfc = pmx->pfc;
413 int idx = sh_pfc_get_pin_index(pfc, offset);
414 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
415 unsigned long flags;
416
417 spin_lock_irqsave(&pfc->lock, flags);
418 cfg->type = PINMUX_TYPE_NONE;
419 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900420}
421
422static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
423 struct pinctrl_gpio_range *range,
424 unsigned offset, bool input)
425{
426 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100427 struct sh_pfc *pfc = pmx->pfc;
428 int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
429 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100430 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100431 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100432 unsigned long flags;
Laurent Pinchart6dc9b452013-03-13 18:18:30 +0100433 unsigned int dir;
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100434 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900435
Laurent Pinchart6dc9b452013-03-13 18:18:30 +0100436 /* Check if the requested direction is supported by the pin. Not all SoC
437 * provide pin config data, so perform the check conditionally.
438 */
439 if (pin->configs) {
440 dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
441 if (!(pin->configs & dir))
442 return -EINVAL;
443 }
444
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100445 spin_lock_irqsave(&pfc->lock, flags);
446
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100447 ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100448 if (ret < 0)
449 goto done;
450
451 cfg->type = new_type;
452
453done:
454 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100455 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900456}
457
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100458static const struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900459 .get_functions_count = sh_pfc_get_functions_count,
460 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900461 .get_function_groups = sh_pfc_get_function_groups,
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100462 .enable = sh_pfc_func_enable,
463 .disable = sh_pfc_func_disable,
Paul Mundtca5481c62012-07-10 12:08:14 +0900464 .gpio_request_enable = sh_pfc_gpio_request_enable,
465 .gpio_disable_free = sh_pfc_gpio_disable_free,
466 .gpio_set_direction = sh_pfc_gpio_set_direction,
467};
468
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100469/* Check whether the requested parameter is supported for a pin. */
470static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
471 enum pin_config_param param)
472{
473 int idx = sh_pfc_get_pin_index(pfc, _pin);
474 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
475
476 switch (param) {
477 case PIN_CONFIG_BIAS_DISABLE:
478 return true;
479
480 case PIN_CONFIG_BIAS_PULL_UP:
481 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
482
483 case PIN_CONFIG_BIAS_PULL_DOWN:
484 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
485
486 default:
487 return false;
488 }
489}
490
Laurent Pinchart934cb022013-02-14 22:35:09 +0100491static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900492 unsigned long *config)
493{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900494 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
495 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100496 enum pin_config_param param = pinconf_to_config_param(*config);
497 unsigned long flags;
498 unsigned int bias;
Paul Mundtd93a8912012-07-11 17:17:10 +0900499
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100500 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
501 return -ENOTSUPP;
502
503 switch (param) {
504 case PIN_CONFIG_BIAS_DISABLE:
505 case PIN_CONFIG_BIAS_PULL_UP:
506 case PIN_CONFIG_BIAS_PULL_DOWN:
507 if (!pfc->info->ops || !pfc->info->ops->get_bias)
508 return -ENOTSUPP;
509
510 spin_lock_irqsave(&pfc->lock, flags);
511 bias = pfc->info->ops->get_bias(pfc, _pin);
512 spin_unlock_irqrestore(&pfc->lock, flags);
513
514 if (bias != param)
515 return -EINVAL;
516
517 *config = 0;
518 break;
519
520 default:
521 return -ENOTSUPP;
522 }
Paul Mundtd93a8912012-07-11 17:17:10 +0900523
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900524 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900525}
526
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100527static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900528 unsigned long config)
529{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900530 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100531 struct sh_pfc *pfc = pmx->pfc;
532 enum pin_config_param param = pinconf_to_config_param(config);
533 unsigned long flags;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900534
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100535 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
536 return -ENOTSUPP;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900537
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100538 switch (param) {
539 case PIN_CONFIG_BIAS_PULL_UP:
540 case PIN_CONFIG_BIAS_PULL_DOWN:
541 case PIN_CONFIG_BIAS_DISABLE:
542 if (!pfc->info->ops || !pfc->info->ops->set_bias)
543 return -ENOTSUPP;
544
545 spin_lock_irqsave(&pfc->lock, flags);
546 pfc->info->ops->set_bias(pfc, _pin, param);
547 spin_unlock_irqrestore(&pfc->lock, flags);
548
549 break;
550
551 default:
552 return -ENOTSUPP;
553 }
554
555 return 0;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900556}
557
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100558static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
559 unsigned long config)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900560{
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100561 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
562 const unsigned int *pins;
563 unsigned int num_pins;
564 unsigned int i;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900565
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100566 pins = pmx->pfc->info->groups[group].pins;
567 num_pins = pmx->pfc->info->groups[group].nr_pins;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900568
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100569 for (i = 0; i < num_pins; ++i)
570 sh_pfc_pinconf_set(pctldev, pins[i], config);
571
572 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900573}
574
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100575static const struct pinconf_ops sh_pfc_pinconf_ops = {
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100576 .is_generic = true,
577 .pin_config_get = sh_pfc_pinconf_get,
578 .pin_config_set = sh_pfc_pinconf_set,
579 .pin_config_group_set = sh_pfc_pinconf_group_set,
580 .pin_config_config_dbg_show = pinconf_generic_dump_config,
Paul Mundtca5481c62012-07-10 12:08:14 +0900581};
582
Laurent Pinchart63d57382013-02-15 01:33:38 +0100583/* PFC ranges -> pinctrl pin descs */
584static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtca5481c62012-07-10 12:08:14 +0900585{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100586 const struct pinmux_range *ranges;
587 struct pinmux_range def_range;
588 unsigned int nr_ranges;
589 unsigned int nr_pins;
590 unsigned int i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900591
Laurent Pinchart63d57382013-02-15 01:33:38 +0100592 if (pfc->info->ranges == NULL) {
593 def_range.begin = 0;
594 def_range.end = pfc->info->nr_pins - 1;
595 ranges = &def_range;
596 nr_ranges = 1;
597 } else {
598 ranges = pfc->info->ranges;
599 nr_ranges = pfc->info->nr_ranges;
600 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900601
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100602 pmx->pins = devm_kzalloc(pfc->dev,
603 sizeof(*pmx->pins) * pfc->info->nr_pins,
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100604 GFP_KERNEL);
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100605 if (unlikely(!pmx->pins))
Paul Mundtca5481c62012-07-10 12:08:14 +0900606 return -ENOMEM;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100607
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100608 pmx->configs = devm_kzalloc(pfc->dev,
609 sizeof(*pmx->configs) * pfc->info->nr_pins,
610 GFP_KERNEL);
611 if (unlikely(!pmx->configs))
612 return -ENOMEM;
613
Laurent Pinchart63d57382013-02-15 01:33:38 +0100614 for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
615 const struct pinmux_range *range = &ranges[i];
616 unsigned int number;
617
618 for (number = range->begin; number <= range->end;
619 number++, nr_pins++) {
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100620 struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100621 struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100622 const struct sh_pfc_pin *info =
623 &pfc->info->pins[nr_pins];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100624
625 pin->number = number;
626 pin->name = info->name;
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100627 cfg->type = PINMUX_TYPE_NONE;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100628 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900629 }
630
Laurent Pinchart63d57382013-02-15 01:33:38 +0100631 pfc->nr_pins = ranges[nr_ranges-1].end + 1;
Paul Mundtca5481c62012-07-10 12:08:14 +0900632
Laurent Pinchart63d57382013-02-15 01:33:38 +0100633 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900634}
635
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100636int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900637{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100638 struct sh_pfc_pinctrl *pmx;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100639 int nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900640
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100641 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100642 if (unlikely(!pmx))
643 return -ENOMEM;
Paul Mundtca5481c62012-07-10 12:08:14 +0900644
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100645 pmx->pfc = pfc;
646 pfc->pinctrl = pmx;
647
Laurent Pinchart63d57382013-02-15 01:33:38 +0100648 nr_ranges = sh_pfc_map_pins(pfc, pmx);
649 if (unlikely(nr_ranges < 0))
650 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900651
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100652 pmx->pctl_desc.name = DRV_NAME;
653 pmx->pctl_desc.owner = THIS_MODULE;
654 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
655 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
656 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100657 pmx->pctl_desc.pins = pmx->pins;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100658 pmx->pctl_desc.npins = pfc->info->nr_pins;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100659
660 pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
Wei Yongjunfd9d05b2013-03-11 22:08:12 +0800661 if (pmx->pctl == NULL)
662 return -EINVAL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900663
Paul Mundtca5481c62012-07-10 12:08:14 +0900664 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900665}
666
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100667int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900668{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100669 struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
Paul Mundtca5481c62012-07-10 12:08:14 +0900670
Paul Mundtca5481c62012-07-10 12:08:14 +0900671 pinctrl_unregister(pmx->pctl);
672
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100673 pfc->pinctrl = NULL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900674 return 0;
675}