blob: e208ee04a9f45482044606bc509c70ec5170c4cb [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;
Laurent Pinchart16ccaf52015-06-30 11:29:57 +030043
44 const char *func_prop_name;
45 const char *groups_prop_name;
46 const char *pins_prop_name;
Paul Mundtca5481c62012-07-10 12:08:14 +090047};
48
Paul Mundte3f805e2012-07-17 15:48:18 +090049static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
Paul Mundtca5481c62012-07-10 12:08:14 +090050{
Paul Mundte3f805e2012-07-17 15:48:18 +090051 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
52
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010053 return pmx->pfc->info->nr_groups;
Paul Mundtca5481c62012-07-10 12:08:14 +090054}
55
Paul Mundte3f805e2012-07-17 15:48:18 +090056static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
Paul Mundtca5481c62012-07-10 12:08:14 +090057 unsigned selector)
58{
Paul Mundte3f805e2012-07-17 15:48:18 +090059 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
60
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010061 return pmx->pfc->info->groups[selector].name;
Paul Mundtca5481c62012-07-10 12:08:14 +090062}
63
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010064static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +090065 const unsigned **pins, unsigned *num_pins)
66{
Paul Mundte3f805e2012-07-17 15:48:18 +090067 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
68
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010069 *pins = pmx->pfc->info->groups[selector].pins;
70 *num_pins = pmx->pfc->info->groups[selector].nr_pins;
Paul Mundte3f805e2012-07-17 15:48:18 +090071
72 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +090073}
74
Paul Mundtfdd85ec2012-07-20 16:39:09 +090075static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
76 unsigned offset)
77{
78 seq_printf(s, "%s", DRV_NAME);
79}
80
Laurent Pinchart3a8d63d2013-06-19 13:26:02 +020081#ifdef CONFIG_OF
Laurent Pinchart12f3ad82013-06-17 20:50:03 +020082static int sh_pfc_map_add_config(struct pinctrl_map *map,
83 const char *group_or_pin,
84 enum pinctrl_map_type type,
85 unsigned long *configs,
86 unsigned int num_configs)
87{
88 unsigned long *cfgs;
89
90 cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
91 GFP_KERNEL);
92 if (cfgs == NULL)
93 return -ENOMEM;
94
95 map->type = type;
96 map->data.configs.group_or_pin = group_or_pin;
97 map->data.configs.configs = cfgs;
98 map->data.configs.num_configs = num_configs;
99
100 return 0;
101}
102
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300103static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev *pctldev,
104 struct device_node *np,
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200105 struct pinctrl_map **map,
106 unsigned int *num_maps, unsigned int *index)
107{
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300108 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
109 struct device *dev = pmx->pfc->dev;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200110 struct pinctrl_map *maps = *map;
111 unsigned int nmaps = *num_maps;
112 unsigned int idx = *index;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200113 unsigned int num_configs;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200114 const char *function = NULL;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200115 unsigned long *configs;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200116 struct property *prop;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200117 unsigned int num_groups;
118 unsigned int num_pins;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200119 const char *group;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200120 const char *pin;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200121 int ret;
122
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300123 /* Support both the old Renesas-specific properties and the new standard
124 * properties. Mixing old and new properties isn't allowed, neither
125 * inside a subnode nor across subnodes.
126 */
127 if (!pmx->func_prop_name) {
128 if (of_find_property(np, "groups", NULL) ||
129 of_find_property(np, "pins", NULL)) {
130 pmx->func_prop_name = "function";
131 pmx->groups_prop_name = "groups";
132 pmx->pins_prop_name = "pins";
133 } else {
134 pmx->func_prop_name = "renesas,function";
135 pmx->groups_prop_name = "renesas,groups";
136 pmx->pins_prop_name = "renesas,pins";
137 }
138 }
139
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200140 /* Parse the function and configuration properties. At least a function
141 * or one configuration must be specified.
142 */
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300143 ret = of_property_read_string(np, pmx->func_prop_name, &function);
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200144 if (ret < 0 && ret != -EINVAL) {
145 dev_err(dev, "Invalid function in DT\n");
146 return ret;
147 }
148
Soren Brinkmanndd4d01f2015-01-09 07:43:46 -0800149 ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200150 if (ret < 0)
151 return ret;
152
153 if (!function && num_configs == 0) {
154 dev_err(dev,
155 "DT node must contain at least a function or config\n");
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300156 ret = -ENODEV;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200157 goto done;
158 }
159
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200160 /* Count the number of pins and groups and reallocate mappings. */
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300161 ret = of_property_count_strings(np, pmx->pins_prop_name);
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200162 if (ret == -EINVAL) {
163 num_pins = 0;
164 } else if (ret < 0) {
165 dev_err(dev, "Invalid pins list in DT\n");
166 goto done;
167 } else {
168 num_pins = ret;
169 }
170
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300171 ret = of_property_count_strings(np, pmx->groups_prop_name);
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200172 if (ret == -EINVAL) {
173 num_groups = 0;
174 } else if (ret < 0) {
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200175 dev_err(dev, "Invalid pin groups list in DT\n");
176 goto done;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200177 } else {
178 num_groups = ret;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200179 }
180
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200181 if (!num_pins && !num_groups) {
182 dev_err(dev, "No pin or group provided in DT node\n");
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200183 ret = -ENODEV;
184 goto done;
185 }
186
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200187 if (function)
188 nmaps += num_groups;
189 if (configs)
190 nmaps += num_pins + num_groups;
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200191
192 maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
193 if (maps == NULL) {
194 ret = -ENOMEM;
195 goto done;
196 }
197
198 *map = maps;
199 *num_maps = nmaps;
200
201 /* Iterate over pins and groups and create the mappings. */
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300202 of_property_for_each_string(np, pmx->groups_prop_name, prop, group) {
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200203 if (function) {
204 maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
205 maps[idx].data.mux.group = group;
206 maps[idx].data.mux.function = function;
207 idx++;
208 }
209
210 if (configs) {
211 ret = sh_pfc_map_add_config(&maps[idx], group,
212 PIN_MAP_TYPE_CONFIGS_GROUP,
213 configs, num_configs);
214 if (ret < 0)
215 goto done;
216
217 idx++;
218 }
219 }
220
221 if (!configs) {
222 ret = 0;
223 goto done;
224 }
225
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300226 of_property_for_each_string(np, pmx->pins_prop_name, prop, pin) {
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200227 ret = sh_pfc_map_add_config(&maps[idx], pin,
228 PIN_MAP_TYPE_CONFIGS_PIN,
229 configs, num_configs);
230 if (ret < 0)
231 goto done;
232
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200233 idx++;
234 }
235
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200236done:
237 *index = idx;
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200238 kfree(configs);
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200239 return ret;
240}
241
242static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
243 struct pinctrl_map *map, unsigned num_maps)
244{
Laurent Pinchart12f3ad82013-06-17 20:50:03 +0200245 unsigned int i;
246
247 if (map == NULL)
248 return;
249
250 for (i = 0; i < num_maps; ++i) {
251 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
252 map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
253 kfree(map[i].data.configs.configs);
254 }
255
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200256 kfree(map);
257}
258
259static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
260 struct device_node *np,
261 struct pinctrl_map **map, unsigned *num_maps)
262{
263 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
264 struct device *dev = pmx->pfc->dev;
265 struct device_node *child;
266 unsigned int index;
267 int ret;
268
269 *map = NULL;
270 *num_maps = 0;
271 index = 0;
272
273 for_each_child_of_node(np, child) {
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300274 ret = sh_pfc_dt_subnode_to_map(pctldev, child, map, num_maps,
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200275 &index);
Julia Lawalld0b3ed42015-12-21 17:39:46 +0100276 if (ret < 0) {
277 of_node_put(child);
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200278 goto done;
Julia Lawalld0b3ed42015-12-21 17:39:46 +0100279 }
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200280 }
281
282 /* If no mapping has been found in child nodes try the config node. */
283 if (*num_maps == 0) {
Laurent Pinchart16ccaf52015-06-30 11:29:57 +0300284 ret = sh_pfc_dt_subnode_to_map(pctldev, np, map, num_maps,
285 &index);
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200286 if (ret < 0)
287 goto done;
288 }
289
290 if (*num_maps)
291 return 0;
292
293 dev_err(dev, "no mapping found in node %s\n", np->full_name);
294 ret = -EINVAL;
295
296done:
297 if (ret < 0)
298 sh_pfc_dt_free_map(pctldev, *map, *num_maps);
299
300 return ret;
301}
Laurent Pinchart3a8d63d2013-06-19 13:26:02 +0200302#endif /* CONFIG_OF */
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200303
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100304static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
Paul Mundte3f805e2012-07-17 15:48:18 +0900305 .get_groups_count = sh_pfc_get_groups_count,
306 .get_group_name = sh_pfc_get_group_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900307 .get_group_pins = sh_pfc_get_group_pins,
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900308 .pin_dbg_show = sh_pfc_pin_dbg_show,
Laurent Pinchart3a8d63d2013-06-19 13:26:02 +0200309#ifdef CONFIG_OF
Laurent Pinchartfe1c9a82013-06-17 20:50:02 +0200310 .dt_node_to_map = sh_pfc_dt_node_to_map,
311 .dt_free_map = sh_pfc_dt_free_map,
Laurent Pinchart3a8d63d2013-06-19 13:26:02 +0200312#endif
Paul Mundtca5481c62012-07-10 12:08:14 +0900313};
314
Paul Mundtd93a8912012-07-11 17:17:10 +0900315static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
316{
317 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtca5481c62012-07-10 12:08:14 +0900318
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100319 return pmx->pfc->info->nr_functions;
Paul Mundtd93a8912012-07-11 17:17:10 +0900320}
321
322static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
323 unsigned selector)
324{
325 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
326
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100327 return pmx->pfc->info->functions[selector].name;
Paul Mundtd93a8912012-07-11 17:17:10 +0900328}
329
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100330static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
331 unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900332 const char * const **groups,
333 unsigned * const num_groups)
334{
Paul Mundtd93a8912012-07-11 17:17:10 +0900335 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
336
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100337 *groups = pmx->pfc->info->functions[selector].groups;
338 *num_groups = pmx->pfc->info->functions[selector].nr_groups;
Paul Mundtd93a8912012-07-11 17:17:10 +0900339
Paul Mundtca5481c62012-07-10 12:08:14 +0900340 return 0;
341}
342
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200343static int sh_pfc_func_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
344 unsigned group)
Paul Mundtca5481c62012-07-10 12:08:14 +0900345{
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100346 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
347 struct sh_pfc *pfc = pmx->pfc;
348 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
349 unsigned long flags;
350 unsigned int i;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100351 int ret = 0;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100352
353 spin_lock_irqsave(&pfc->lock, flags);
354
355 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100356 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
357 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
358
359 if (cfg->type != PINMUX_TYPE_NONE) {
360 ret = -EBUSY;
361 goto done;
362 }
363 }
364
365 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchartb705c052013-03-10 16:38:23 +0100366 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
367 if (ret < 0)
368 break;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100369 }
370
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100371done:
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100372 spin_unlock_irqrestore(&pfc->lock, flags);
373 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900374}
375
Paul Mundtca5481c62012-07-10 12:08:14 +0900376static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
377 struct pinctrl_gpio_range *range,
378 unsigned offset)
379{
380 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
381 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100382 int idx = sh_pfc_get_pin_index(pfc, offset);
383 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Paul Mundtca5481c62012-07-10 12:08:14 +0900384 unsigned long flags;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100385 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900386
387 spin_lock_irqsave(&pfc->lock, flags);
388
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100389 if (cfg->type != PINMUX_TYPE_NONE) {
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100390 dev_err(pfc->dev,
391 "Pin %u is busy, can't configure it as GPIO.\n",
392 offset);
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100393 ret = -EBUSY;
394 goto done;
Paul Mundtd93a8912012-07-11 17:17:10 +0900395 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900396
Laurent Pincharte3c470512013-03-10 17:30:25 +0100397 if (!pfc->gpio) {
398 /* If GPIOs are handled externally the pin mux type need to be
399 * set to GPIO here.
400 */
401 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
402
403 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
404 if (ret < 0)
405 goto done;
406 }
407
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100408 cfg->type = PINMUX_TYPE_GPIO;
409
Paul Mundtca5481c62012-07-10 12:08:14 +0900410 ret = 0;
411
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100412done:
Paul Mundtca5481c62012-07-10 12:08:14 +0900413 spin_unlock_irqrestore(&pfc->lock, flags);
414
415 return ret;
416}
417
418static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
419 struct pinctrl_gpio_range *range,
420 unsigned offset)
421{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100422 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
423 struct sh_pfc *pfc = pmx->pfc;
424 int idx = sh_pfc_get_pin_index(pfc, offset);
425 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
426 unsigned long flags;
427
428 spin_lock_irqsave(&pfc->lock, flags);
429 cfg->type = PINMUX_TYPE_NONE;
430 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900431}
432
433static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
434 struct pinctrl_gpio_range *range,
435 unsigned offset, bool input)
436{
437 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100438 struct sh_pfc *pfc = pmx->pfc;
439 int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
440 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100441 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100442 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100443 unsigned long flags;
Laurent Pinchart6dc9b452013-03-13 18:18:30 +0100444 unsigned int dir;
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100445 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900446
Laurent Pinchart6dc9b452013-03-13 18:18:30 +0100447 /* Check if the requested direction is supported by the pin. Not all SoC
448 * provide pin config data, so perform the check conditionally.
449 */
450 if (pin->configs) {
451 dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
452 if (!(pin->configs & dir))
453 return -EINVAL;
454 }
455
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100456 spin_lock_irqsave(&pfc->lock, flags);
457
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100458 ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100459 if (ret < 0)
460 goto done;
461
462 cfg->type = new_type;
463
464done:
465 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100466 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900467}
468
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100469static const struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900470 .get_functions_count = sh_pfc_get_functions_count,
471 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900472 .get_function_groups = sh_pfc_get_function_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200473 .set_mux = sh_pfc_func_set_mux,
Paul Mundtca5481c62012-07-10 12:08:14 +0900474 .gpio_request_enable = sh_pfc_gpio_request_enable,
475 .gpio_disable_free = sh_pfc_gpio_disable_free,
476 .gpio_set_direction = sh_pfc_gpio_set_direction,
477};
478
Laurent Pinchart3caa7d82016-03-23 16:06:00 +0200479static u32 sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc,
480 unsigned int pin, unsigned int *offset, unsigned int *size)
481{
482 const struct pinmux_drive_reg_field *field;
483 const struct pinmux_drive_reg *reg;
484 unsigned int i;
485
486 for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
487 for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
488 field = &reg->fields[i];
489
490 if (field->size && field->pin == pin) {
491 *offset = field->offset;
492 *size = field->size;
493
494 return reg->reg;
495 }
496 }
497 }
498
499 return 0;
500}
501
502static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc *pfc,
503 unsigned int pin)
504{
505 unsigned long flags;
506 unsigned int offset;
507 unsigned int size;
508 u32 reg;
509 u32 val;
510
511 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
512 if (!reg)
513 return -EINVAL;
514
515 spin_lock_irqsave(&pfc->lock, flags);
516 val = sh_pfc_read_reg(pfc, reg, 32);
517 spin_unlock_irqrestore(&pfc->lock, flags);
518
519 val = (val >> offset) & GENMASK(size - 1, 0);
520
521 /* Convert the value to mA based on a full drive strength value of 24mA.
522 * We can make the full value configurable later if needed.
523 */
524 return (val + 1) * (size == 2 ? 6 : 3);
525}
526
527static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
528 unsigned int pin, u16 strength)
529{
530 unsigned long flags;
531 unsigned int offset;
532 unsigned int size;
533 unsigned int step;
534 u32 reg;
535 u32 val;
536
537 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
538 if (!reg)
539 return -EINVAL;
540
541 step = size == 2 ? 6 : 3;
542
543 if (strength < step || strength > 24)
544 return -EINVAL;
545
546 /* Convert the value from mA based on a full drive strength value of
547 * 24mA. We can make the full value configurable later if needed.
548 */
549 strength = strength / step - 1;
550
551 spin_lock_irqsave(&pfc->lock, flags);
552
553 val = sh_pfc_read_reg(pfc, reg, 32);
554 val &= ~GENMASK(offset + size - 1, offset);
555 val |= strength << offset;
556
557 sh_pfc_write_reg(pfc, reg, 32, val);
558
559 spin_unlock_irqrestore(&pfc->lock, flags);
560
561 return 0;
562}
563
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100564/* Check whether the requested parameter is supported for a pin. */
565static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
566 enum pin_config_param param)
567{
568 int idx = sh_pfc_get_pin_index(pfc, _pin);
569 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
570
571 switch (param) {
572 case PIN_CONFIG_BIAS_DISABLE:
573 return true;
574
575 case PIN_CONFIG_BIAS_PULL_UP:
576 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
577
578 case PIN_CONFIG_BIAS_PULL_DOWN:
579 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
580
Laurent Pinchart3caa7d82016-03-23 16:06:00 +0200581 case PIN_CONFIG_DRIVE_STRENGTH:
582 return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
583
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100584 case PIN_CONFIG_POWER_SOURCE:
585 return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
586
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100587 default:
588 return false;
589 }
590}
591
Laurent Pinchart934cb022013-02-14 22:35:09 +0100592static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900593 unsigned long *config)
594{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900595 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
596 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100597 enum pin_config_param param = pinconf_to_config_param(*config);
598 unsigned long flags;
Paul Mundtd93a8912012-07-11 17:17:10 +0900599
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100600 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
601 return -ENOTSUPP;
602
603 switch (param) {
604 case PIN_CONFIG_BIAS_DISABLE:
605 case PIN_CONFIG_BIAS_PULL_UP:
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100606 case PIN_CONFIG_BIAS_PULL_DOWN: {
607 unsigned int bias;
608
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100609 if (!pfc->info->ops || !pfc->info->ops->get_bias)
610 return -ENOTSUPP;
611
612 spin_lock_irqsave(&pfc->lock, flags);
613 bias = pfc->info->ops->get_bias(pfc, _pin);
614 spin_unlock_irqrestore(&pfc->lock, flags);
615
616 if (bias != param)
617 return -EINVAL;
618
619 *config = 0;
620 break;
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100621 }
622
Laurent Pinchart3caa7d82016-03-23 16:06:00 +0200623 case PIN_CONFIG_DRIVE_STRENGTH: {
624 int ret;
625
626 ret = sh_pfc_pinconf_get_drive_strength(pfc, _pin);
627 if (ret < 0)
628 return ret;
629
630 *config = ret;
631 break;
632 }
633
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100634 case PIN_CONFIG_POWER_SOURCE: {
Wolfram Sang87753062016-06-06 18:08:25 +0200635 u32 pocctrl, val;
636 int bit;
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100637
Wolfram Sang87753062016-06-06 18:08:25 +0200638 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100639 return -ENOTSUPP;
640
Wolfram Sang87753062016-06-06 18:08:25 +0200641 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
642 if (WARN(bit < 0, "invalid pin %#x", _pin))
643 return bit;
644
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100645 spin_lock_irqsave(&pfc->lock, flags);
Wolfram Sang87753062016-06-06 18:08:25 +0200646 val = sh_pfc_read_reg(pfc, pocctrl, 32);
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100647 spin_unlock_irqrestore(&pfc->lock, flags);
648
Wolfram Sang87753062016-06-06 18:08:25 +0200649 *config = (val & BIT(bit)) ? 3300 : 1800;
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100650 break;
651 }
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100652
653 default:
654 return -ENOTSUPP;
655 }
Paul Mundtd93a8912012-07-11 17:17:10 +0900656
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900657 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900658}
659
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100660static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
Sherman Yin03b054e2013-08-27 11:32:12 -0700661 unsigned long *configs, unsigned num_configs)
Paul Mundtca5481c62012-07-10 12:08:14 +0900662{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900663 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100664 struct sh_pfc *pfc = pmx->pfc;
Sherman Yin03b054e2013-08-27 11:32:12 -0700665 enum pin_config_param param;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100666 unsigned long flags;
Sherman Yin03b054e2013-08-27 11:32:12 -0700667 unsigned int i;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900668
Sherman Yin03b054e2013-08-27 11:32:12 -0700669 for (i = 0; i < num_configs; i++) {
670 param = pinconf_to_config_param(configs[i]);
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900671
Sherman Yin03b054e2013-08-27 11:32:12 -0700672 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100673 return -ENOTSUPP;
674
Sherman Yin03b054e2013-08-27 11:32:12 -0700675 switch (param) {
676 case PIN_CONFIG_BIAS_PULL_UP:
677 case PIN_CONFIG_BIAS_PULL_DOWN:
678 case PIN_CONFIG_BIAS_DISABLE:
679 if (!pfc->info->ops || !pfc->info->ops->set_bias)
680 return -ENOTSUPP;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100681
Sherman Yin03b054e2013-08-27 11:32:12 -0700682 spin_lock_irqsave(&pfc->lock, flags);
683 pfc->info->ops->set_bias(pfc, _pin, param);
684 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100685
Sherman Yin03b054e2013-08-27 11:32:12 -0700686 break;
687
Laurent Pinchart3caa7d82016-03-23 16:06:00 +0200688 case PIN_CONFIG_DRIVE_STRENGTH: {
689 unsigned int arg =
690 pinconf_to_config_argument(configs[i]);
691 int ret;
692
693 ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
694 if (ret < 0)
695 return ret;
696
697 break;
698 }
699
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100700 case PIN_CONFIG_POWER_SOURCE: {
Wolfram Sang87753062016-06-06 18:08:25 +0200701 unsigned int mV = pinconf_to_config_argument(configs[i]);
702 u32 pocctrl, val;
703 int bit;
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100704
Wolfram Sang87753062016-06-06 18:08:25 +0200705 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100706 return -ENOTSUPP;
707
Wolfram Sang87753062016-06-06 18:08:25 +0200708 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
709 if (WARN(bit < 0, "invalid pin %#x", _pin))
710 return bit;
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100711
Wolfram Sang87753062016-06-06 18:08:25 +0200712 if (mV != 1800 && mV != 3300)
713 return -EINVAL;
714
715 spin_lock_irqsave(&pfc->lock, flags);
716 val = sh_pfc_read_reg(pfc, pocctrl, 32);
717 if (mV == 3300)
718 val |= BIT(bit);
719 else
720 val &= ~BIT(bit);
721 sh_pfc_write_reg(pfc, pocctrl, 32, val);
722 spin_unlock_irqrestore(&pfc->lock, flags);
Ben Hutchings5b9eaa52015-06-30 17:53:59 +0100723
724 break;
725 }
726
Sherman Yin03b054e2013-08-27 11:32:12 -0700727 default:
728 return -ENOTSUPP;
729 }
730 } /* for each config */
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100731
732 return 0;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900733}
734
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100735static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
Sherman Yin03b054e2013-08-27 11:32:12 -0700736 unsigned long *configs,
737 unsigned num_configs)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900738{
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100739 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
740 const unsigned int *pins;
741 unsigned int num_pins;
742 unsigned int i;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900743
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100744 pins = pmx->pfc->info->groups[group].pins;
745 num_pins = pmx->pfc->info->groups[group].nr_pins;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900746
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100747 for (i = 0; i < num_pins; ++i)
Sherman Yin03b054e2013-08-27 11:32:12 -0700748 sh_pfc_pinconf_set(pctldev, pins[i], configs, num_configs);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100749
750 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900751}
752
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100753static const struct pinconf_ops sh_pfc_pinconf_ops = {
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100754 .is_generic = true,
755 .pin_config_get = sh_pfc_pinconf_get,
756 .pin_config_set = sh_pfc_pinconf_set,
757 .pin_config_group_set = sh_pfc_pinconf_group_set,
758 .pin_config_config_dbg_show = pinconf_generic_dump_config,
Paul Mundtca5481c62012-07-10 12:08:14 +0900759};
760
Laurent Pinchart63d57382013-02-15 01:33:38 +0100761/* PFC ranges -> pinctrl pin descs */
762static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtca5481c62012-07-10 12:08:14 +0900763{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100764 unsigned int i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900765
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200766 /* Allocate and initialize the pins and configs arrays. */
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100767 pmx->pins = devm_kzalloc(pfc->dev,
768 sizeof(*pmx->pins) * pfc->info->nr_pins,
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100769 GFP_KERNEL);
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100770 if (unlikely(!pmx->pins))
Paul Mundtca5481c62012-07-10 12:08:14 +0900771 return -ENOMEM;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100772
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100773 pmx->configs = devm_kzalloc(pfc->dev,
774 sizeof(*pmx->configs) * pfc->info->nr_pins,
775 GFP_KERNEL);
776 if (unlikely(!pmx->configs))
777 return -ENOMEM;
778
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200779 for (i = 0; i < pfc->info->nr_pins; ++i) {
780 const struct sh_pfc_pin *info = &pfc->info->pins[i];
781 struct sh_pfc_pin_config *cfg = &pmx->configs[i];
782 struct pinctrl_pin_desc *pin = &pmx->pins[i];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100783
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200784 /* If the pin number is equal to -1 all pins are considered */
785 pin->number = info->pin != (u16)-1 ? info->pin : i;
786 pin->name = info->name;
787 cfg->type = PINMUX_TYPE_NONE;
Paul Mundtca5481c62012-07-10 12:08:14 +0900788 }
789
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200790 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900791}
792
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100793int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900794{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100795 struct sh_pfc_pinctrl *pmx;
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200796 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900797
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100798 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100799 if (unlikely(!pmx))
800 return -ENOMEM;
Paul Mundtca5481c62012-07-10 12:08:14 +0900801
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100802 pmx->pfc = pfc;
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100803
Laurent Pinchartacac8ed2013-07-15 18:38:30 +0200804 ret = sh_pfc_map_pins(pfc, pmx);
805 if (ret < 0)
806 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900807
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100808 pmx->pctl_desc.name = DRV_NAME;
809 pmx->pctl_desc.owner = THIS_MODULE;
810 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
811 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
812 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100813 pmx->pctl_desc.pins = pmx->pins;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100814 pmx->pctl_desc.npins = pfc->info->nr_pins;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100815
Laxman Dewangan67ec8d72016-02-28 14:37:47 +0530816 pmx->pctl = devm_pinctrl_register(pfc->dev, &pmx->pctl_desc, pmx);
Wei Yongjunf7973d82016-07-06 12:03:32 +0000817 return PTR_ERR_OR_ZERO(pmx->pctl);
Paul Mundtca5481c62012-07-10 12:08:14 +0900818}