blob: 017b84fd9333ce40051d20b53a43610143063524 [file] [log] [blame]
Masahiro Yamada6e908892015-07-14 11:40:01 +09001/*
2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/export.h>
16#include <linux/mfd/syscon.h>
17#include <linux/pinctrl/pinconf.h>
18#include <linux/pinctrl/pinconf-generic.h>
19#include <linux/pinctrl/pinctrl.h>
20#include <linux/pinctrl/pinmux.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
23
24#include "../core.h"
25#include "../pinctrl-utils.h"
26#include "pinctrl-uniphier.h"
27
28struct uniphier_pinctrl_priv {
Masahiro Yamadafc78a562016-05-31 17:05:12 +090029 struct pinctrl_desc pctldesc;
Masahiro Yamada6e908892015-07-14 11:40:01 +090030 struct pinctrl_dev *pctldev;
31 struct regmap *regmap;
32 struct uniphier_pinctrl_socdata *socdata;
33};
34
35static int uniphier_pctl_get_groups_count(struct pinctrl_dev *pctldev)
36{
37 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
38
39 return priv->socdata->groups_count;
40}
41
42static const char *uniphier_pctl_get_group_name(struct pinctrl_dev *pctldev,
43 unsigned selector)
44{
45 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
46
47 return priv->socdata->groups[selector].name;
48}
49
50static int uniphier_pctl_get_group_pins(struct pinctrl_dev *pctldev,
51 unsigned selector,
52 const unsigned **pins,
53 unsigned *num_pins)
54{
55 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
56
57 *pins = priv->socdata->groups[selector].pins;
58 *num_pins = priv->socdata->groups[selector].num_pins;
59
60 return 0;
61}
62
63#ifdef CONFIG_DEBUG_FS
64static void uniphier_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
65 struct seq_file *s, unsigned offset)
66{
67 const struct pinctrl_pin_desc *pin = &pctldev->desc->pins[offset];
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +090068 const char *pull_dir, *drv_type;
Masahiro Yamada6e908892015-07-14 11:40:01 +090069
70 switch (uniphier_pin_get_pull_dir(pin->drv_data)) {
71 case UNIPHIER_PIN_PULL_UP:
72 pull_dir = "UP";
73 break;
74 case UNIPHIER_PIN_PULL_DOWN:
75 pull_dir = "DOWN";
76 break;
Masahiro Yamada10ef8272016-05-31 15:30:10 +090077 case UNIPHIER_PIN_PULL_UP_FIXED:
78 pull_dir = "UP(FIXED)";
79 break;
80 case UNIPHIER_PIN_PULL_DOWN_FIXED:
81 pull_dir = "DOWN(FIXED)";
82 break;
Masahiro Yamada6e908892015-07-14 11:40:01 +090083 case UNIPHIER_PIN_PULL_NONE:
84 pull_dir = "NONE";
85 break;
86 default:
87 BUG();
88 }
89
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +090090 switch (uniphier_pin_get_drv_type(pin->drv_data)) {
91 case UNIPHIER_PIN_DRV_1BIT:
92 drv_type = "4/8(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +090093 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +090094 case UNIPHIER_PIN_DRV_2BIT:
95 drv_type = "8/12/16/20(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +090096 break;
Masahiro Yamada72e57062016-05-31 17:05:14 +090097 case UNIPHIER_PIN_DRV_3BIT:
98 drv_type = "4/5/7/9/11/12/14/16(mA)";
99 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900100 case UNIPHIER_PIN_DRV_FIXED4:
101 drv_type = "4(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900102 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900103 case UNIPHIER_PIN_DRV_FIXED5:
104 drv_type = "5(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900105 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900106 case UNIPHIER_PIN_DRV_FIXED8:
107 drv_type = "8(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900108 break;
109 case UNIPHIER_PIN_DRV_NONE:
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900110 drv_type = "NONE";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900111 break;
112 default:
113 BUG();
114 }
115
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900116 seq_printf(s, " PULL_DIR=%s DRV_TYPE=%s", pull_dir, drv_type);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900117}
118#endif
119
120static const struct pinctrl_ops uniphier_pctlops = {
121 .get_groups_count = uniphier_pctl_get_groups_count,
122 .get_group_name = uniphier_pctl_get_group_name,
123 .get_group_pins = uniphier_pctl_get_group_pins,
124#ifdef CONFIG_DEBUG_FS
125 .pin_dbg_show = uniphier_pctl_pin_dbg_show,
126#endif
127 .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300128 .dt_free_map = pinctrl_utils_free_map,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900129};
130
131static int uniphier_conf_pin_bias_get(struct pinctrl_dev *pctldev,
132 const struct pinctrl_pin_desc *pin,
133 enum pin_config_param param)
134{
135 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
136 enum uniphier_pin_pull_dir pull_dir =
137 uniphier_pin_get_pull_dir(pin->drv_data);
138 unsigned int pupdctrl, reg, shift, val;
139 unsigned int expected = 1;
140 int ret;
141
142 switch (param) {
143 case PIN_CONFIG_BIAS_DISABLE:
144 if (pull_dir == UNIPHIER_PIN_PULL_NONE)
145 return 0;
146 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
147 pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
148 return -EINVAL;
149 expected = 0;
150 break;
151 case PIN_CONFIG_BIAS_PULL_UP:
152 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED)
153 return 0;
154 if (pull_dir != UNIPHIER_PIN_PULL_UP)
155 return -EINVAL;
156 break;
157 case PIN_CONFIG_BIAS_PULL_DOWN:
158 if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
159 return 0;
160 if (pull_dir != UNIPHIER_PIN_PULL_DOWN)
161 return -EINVAL;
162 break;
163 default:
164 BUG();
165 }
166
167 pupdctrl = uniphier_pin_get_pupdctrl(pin->drv_data);
168
169 reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
170 shift = pupdctrl % 32;
171
172 ret = regmap_read(priv->regmap, reg, &val);
173 if (ret)
174 return ret;
175
176 val = (val >> shift) & 1;
177
178 return (val == expected) ? 0 : -EINVAL;
179}
180
181static int uniphier_conf_pin_drive_get(struct pinctrl_dev *pctldev,
182 const struct pinctrl_pin_desc *pin,
183 u16 *strength)
184{
185 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900186 enum uniphier_pin_drv_type type =
187 uniphier_pin_get_drv_type(pin->drv_data);
188 const unsigned int strength_1bit[] = {4, 8};
189 const unsigned int strength_2bit[] = {8, 12, 16, 20};
Masahiro Yamada72e57062016-05-31 17:05:14 +0900190 const unsigned int strength_3bit[] = {4, 5, 7, 9, 11, 12, 14, 16};
Masahiro Yamada6e908892015-07-14 11:40:01 +0900191 const unsigned int *supported_strength;
192 unsigned int drvctrl, reg, shift, mask, width, val;
193 int ret;
194
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900195 switch (type) {
196 case UNIPHIER_PIN_DRV_1BIT:
197 supported_strength = strength_1bit;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900198 reg = UNIPHIER_PINCTRL_DRVCTRL_BASE;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900199 width = 1;
200 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900201 case UNIPHIER_PIN_DRV_2BIT:
202 supported_strength = strength_2bit;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900203 reg = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900204 width = 2;
205 break;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900206 case UNIPHIER_PIN_DRV_3BIT:
207 supported_strength = strength_3bit;
208 reg = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
209 width = 4;
210 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900211 case UNIPHIER_PIN_DRV_FIXED4:
Masahiro Yamada6e908892015-07-14 11:40:01 +0900212 *strength = 4;
213 return 0;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900214 case UNIPHIER_PIN_DRV_FIXED5:
Masahiro Yamada6e908892015-07-14 11:40:01 +0900215 *strength = 5;
216 return 0;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900217 case UNIPHIER_PIN_DRV_FIXED8:
Masahiro Yamada6e908892015-07-14 11:40:01 +0900218 *strength = 8;
219 return 0;
220 default:
221 /* drive strength control is not supported for this pin */
222 return -EINVAL;
223 }
224
225 drvctrl = uniphier_pin_get_drvctrl(pin->drv_data);
226 drvctrl *= width;
227
Masahiro Yamada6e908892015-07-14 11:40:01 +0900228 reg += drvctrl / 32 * 4;
229 shift = drvctrl % 32;
230 mask = (1U << width) - 1;
231
232 ret = regmap_read(priv->regmap, reg, &val);
233 if (ret)
234 return ret;
235
236 *strength = supported_strength[(val >> shift) & mask];
237
238 return 0;
239}
240
241static int uniphier_conf_pin_input_enable_get(struct pinctrl_dev *pctldev,
242 const struct pinctrl_pin_desc *pin)
243{
244 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
245 unsigned int iectrl = uniphier_pin_get_iectrl(pin->drv_data);
246 unsigned int val;
247 int ret;
248
249 if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
250 /* This pin is always input-enabled. */
251 return 0;
252
253 ret = regmap_read(priv->regmap, UNIPHIER_PINCTRL_IECTRL, &val);
254 if (ret)
255 return ret;
256
257 return val & BIT(iectrl) ? 0 : -EINVAL;
258}
259
260static int uniphier_conf_pin_config_get(struct pinctrl_dev *pctldev,
261 unsigned pin,
262 unsigned long *configs)
263{
264 const struct pinctrl_pin_desc *pin_desc = &pctldev->desc->pins[pin];
265 enum pin_config_param param = pinconf_to_config_param(*configs);
266 bool has_arg = false;
267 u16 arg;
268 int ret;
269
270 switch (param) {
271 case PIN_CONFIG_BIAS_DISABLE:
272 case PIN_CONFIG_BIAS_PULL_UP:
273 case PIN_CONFIG_BIAS_PULL_DOWN:
274 ret = uniphier_conf_pin_bias_get(pctldev, pin_desc, param);
275 break;
276 case PIN_CONFIG_DRIVE_STRENGTH:
277 ret = uniphier_conf_pin_drive_get(pctldev, pin_desc, &arg);
278 has_arg = true;
279 break;
280 case PIN_CONFIG_INPUT_ENABLE:
281 ret = uniphier_conf_pin_input_enable_get(pctldev, pin_desc);
282 break;
283 default:
284 /* unsupported parameter */
285 ret = -EINVAL;
286 break;
287 }
288
289 if (ret == 0 && has_arg)
290 *configs = pinconf_to_config_packed(param, arg);
291
292 return ret;
293}
294
295static int uniphier_conf_pin_bias_set(struct pinctrl_dev *pctldev,
296 const struct pinctrl_pin_desc *pin,
297 enum pin_config_param param,
298 u16 arg)
299{
300 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
301 enum uniphier_pin_pull_dir pull_dir =
302 uniphier_pin_get_pull_dir(pin->drv_data);
303 unsigned int pupdctrl, reg, shift;
304 unsigned int val = 1;
305
306 switch (param) {
307 case PIN_CONFIG_BIAS_DISABLE:
308 if (pull_dir == UNIPHIER_PIN_PULL_NONE)
309 return 0;
310 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
311 pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED) {
312 dev_err(pctldev->dev,
313 "can not disable pull register for pin %u (%s)\n",
314 pin->number, pin->name);
315 return -EINVAL;
316 }
317 val = 0;
318 break;
319 case PIN_CONFIG_BIAS_PULL_UP:
320 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED && arg != 0)
321 return 0;
322 if (pull_dir != UNIPHIER_PIN_PULL_UP) {
323 dev_err(pctldev->dev,
324 "pull-up is unsupported for pin %u (%s)\n",
325 pin->number, pin->name);
326 return -EINVAL;
327 }
328 if (arg == 0) {
329 dev_err(pctldev->dev, "pull-up can not be total\n");
330 return -EINVAL;
331 }
332 break;
333 case PIN_CONFIG_BIAS_PULL_DOWN:
334 if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED && arg != 0)
335 return 0;
336 if (pull_dir != UNIPHIER_PIN_PULL_DOWN) {
337 dev_err(pctldev->dev,
338 "pull-down is unsupported for pin %u (%s)\n",
339 pin->number, pin->name);
340 return -EINVAL;
341 }
342 if (arg == 0) {
343 dev_err(pctldev->dev, "pull-down can not be total\n");
344 return -EINVAL;
345 }
346 break;
347 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
348 if (pull_dir == UNIPHIER_PIN_PULL_NONE) {
349 dev_err(pctldev->dev,
350 "pull-up/down is unsupported for pin %u (%s)\n",
351 pin->number, pin->name);
352 return -EINVAL;
353 }
354
355 if (arg == 0)
356 return 0; /* configuration ingored */
357 break;
358 default:
359 BUG();
360 }
361
362 pupdctrl = uniphier_pin_get_pupdctrl(pin->drv_data);
363
364 reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
365 shift = pupdctrl % 32;
366
367 return regmap_update_bits(priv->regmap, reg, 1 << shift, val << shift);
368}
369
370static int uniphier_conf_pin_drive_set(struct pinctrl_dev *pctldev,
371 const struct pinctrl_pin_desc *pin,
372 u16 strength)
373{
374 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900375 enum uniphier_pin_drv_type type =
376 uniphier_pin_get_drv_type(pin->drv_data);
377 const unsigned int strength_1bit[] = {4, 8, -1};
378 const unsigned int strength_2bit[] = {8, 12, 16, 20, -1};
Masahiro Yamada72e57062016-05-31 17:05:14 +0900379 const unsigned int strength_3bit[] = {4, 5, 7, 9, 11, 12, 14, 16, -1};
Masahiro Yamada6e908892015-07-14 11:40:01 +0900380 const unsigned int *supported_strength;
381 unsigned int drvctrl, reg, shift, mask, width, val;
382
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900383 switch (type) {
384 case UNIPHIER_PIN_DRV_1BIT:
385 supported_strength = strength_1bit;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900386 reg = UNIPHIER_PINCTRL_DRVCTRL_BASE;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900387 width = 1;
388 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900389 case UNIPHIER_PIN_DRV_2BIT:
390 supported_strength = strength_2bit;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900391 reg = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900392 width = 2;
393 break;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900394 case UNIPHIER_PIN_DRV_3BIT:
395 supported_strength = strength_3bit;
396 reg = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
397 width = 4;
398 break;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900399 default:
400 dev_err(pctldev->dev,
401 "cannot change drive strength for pin %u (%s)\n",
402 pin->number, pin->name);
403 return -EINVAL;
404 }
405
406 for (val = 0; supported_strength[val] > 0; val++) {
407 if (supported_strength[val] > strength)
408 break;
409 }
410
411 if (val == 0) {
412 dev_err(pctldev->dev,
413 "unsupported drive strength %u mA for pin %u (%s)\n",
414 strength, pin->number, pin->name);
415 return -EINVAL;
416 }
417
418 val--;
419
420 drvctrl = uniphier_pin_get_drvctrl(pin->drv_data);
421 drvctrl *= width;
422
Masahiro Yamada6e908892015-07-14 11:40:01 +0900423 reg += drvctrl / 32 * 4;
424 shift = drvctrl % 32;
425 mask = (1U << width) - 1;
426
427 return regmap_update_bits(priv->regmap, reg,
428 mask << shift, val << shift);
429}
430
431static int uniphier_conf_pin_input_enable(struct pinctrl_dev *pctldev,
432 const struct pinctrl_pin_desc *pin,
433 u16 enable)
434{
435 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
436 unsigned int iectrl = uniphier_pin_get_iectrl(pin->drv_data);
437
438 if (enable == 0) {
439 /*
440 * Multiple pins share one input enable, so per-pin disabling
441 * is impossible.
442 */
443 dev_err(pctldev->dev, "unable to disable input\n");
444 return -EINVAL;
445 }
446
447 if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
448 /* This pin is always input-enabled. nothing to do. */
449 return 0;
450
451 return regmap_update_bits(priv->regmap, UNIPHIER_PINCTRL_IECTRL,
452 BIT(iectrl), BIT(iectrl));
453}
454
455static int uniphier_conf_pin_config_set(struct pinctrl_dev *pctldev,
456 unsigned pin,
457 unsigned long *configs,
458 unsigned num_configs)
459{
460 const struct pinctrl_pin_desc *pin_desc = &pctldev->desc->pins[pin];
461 int i, ret;
462
463 for (i = 0; i < num_configs; i++) {
464 enum pin_config_param param =
465 pinconf_to_config_param(configs[i]);
466 u16 arg = pinconf_to_config_argument(configs[i]);
467
468 switch (param) {
469 case PIN_CONFIG_BIAS_DISABLE:
470 case PIN_CONFIG_BIAS_PULL_UP:
471 case PIN_CONFIG_BIAS_PULL_DOWN:
472 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
473 ret = uniphier_conf_pin_bias_set(pctldev, pin_desc,
474 param, arg);
475 break;
476 case PIN_CONFIG_DRIVE_STRENGTH:
477 ret = uniphier_conf_pin_drive_set(pctldev, pin_desc,
478 arg);
479 break;
480 case PIN_CONFIG_INPUT_ENABLE:
481 ret = uniphier_conf_pin_input_enable(pctldev,
482 pin_desc, arg);
483 break;
484 default:
485 dev_err(pctldev->dev,
486 "unsupported configuration parameter %u\n",
487 param);
488 return -EINVAL;
489 }
490
491 if (ret)
492 return ret;
493 }
494
495 return 0;
496}
497
498static int uniphier_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
499 unsigned selector,
500 unsigned long *configs,
501 unsigned num_configs)
502{
503 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
504 const unsigned *pins = priv->socdata->groups[selector].pins;
505 unsigned num_pins = priv->socdata->groups[selector].num_pins;
506 int i, ret;
507
508 for (i = 0; i < num_pins; i++) {
509 ret = uniphier_conf_pin_config_set(pctldev, pins[i],
510 configs, num_configs);
511 if (ret)
512 return ret;
513 }
514
515 return 0;
516}
517
518static const struct pinconf_ops uniphier_confops = {
519 .is_generic = true,
520 .pin_config_get = uniphier_conf_pin_config_get,
521 .pin_config_set = uniphier_conf_pin_config_set,
522 .pin_config_group_set = uniphier_conf_pin_config_group_set,
523};
524
525static int uniphier_pmx_get_functions_count(struct pinctrl_dev *pctldev)
526{
527 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
528
529 return priv->socdata->functions_count;
530}
531
532static const char *uniphier_pmx_get_function_name(struct pinctrl_dev *pctldev,
533 unsigned selector)
534{
535 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
536
537 return priv->socdata->functions[selector].name;
538}
539
540static int uniphier_pmx_get_function_groups(struct pinctrl_dev *pctldev,
541 unsigned selector,
542 const char * const **groups,
543 unsigned *num_groups)
544{
545 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
546
547 *groups = priv->socdata->functions[selector].groups;
548 *num_groups = priv->socdata->functions[selector].num_groups;
549
550 return 0;
551}
552
553static int uniphier_pmx_set_one_mux(struct pinctrl_dev *pctldev, unsigned pin,
554 unsigned muxval)
555{
556 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
557 unsigned mux_bits = priv->socdata->mux_bits;
558 unsigned reg_stride = priv->socdata->reg_stride;
559 unsigned reg, reg_end, shift, mask;
560 int ret;
561
Masahiro Yamadabac7f4c2015-10-20 17:25:09 +0900562 /* some pins need input-enabling */
563 ret = uniphier_conf_pin_input_enable(pctldev,
564 &pctldev->desc->pins[pin], 1);
565 if (ret)
566 return ret;
567
Masahiro Yamada6e908892015-07-14 11:40:01 +0900568 reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
569 reg_end = reg + reg_stride;
570 shift = pin * mux_bits % 32;
571 mask = (1U << mux_bits) - 1;
572
573 /*
574 * If reg_stride is greater than 4, the MSB of each pinsel shall be
575 * stored in the offset+4.
576 */
577 for (; reg < reg_end; reg += 4) {
578 ret = regmap_update_bits(priv->regmap, reg,
579 mask << shift, muxval << shift);
580 if (ret)
581 return ret;
582 muxval >>= mux_bits;
583 }
584
585 if (priv->socdata->load_pinctrl) {
586 ret = regmap_write(priv->regmap,
587 UNIPHIER_PINCTRL_LOAD_PINMUX, 1);
588 if (ret)
589 return ret;
590 }
591
Masahiro Yamadabac7f4c2015-10-20 17:25:09 +0900592 return 0;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900593}
594
595static int uniphier_pmx_set_mux(struct pinctrl_dev *pctldev,
596 unsigned func_selector,
597 unsigned group_selector)
598{
599 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
600 const struct uniphier_pinctrl_group *grp =
601 &priv->socdata->groups[group_selector];
602 int i;
603 int ret;
604
605 for (i = 0; i < grp->num_pins; i++) {
606 ret = uniphier_pmx_set_one_mux(pctldev, grp->pins[i],
607 grp->muxvals[i]);
608 if (ret)
609 return ret;
610 }
611
612 return 0;
613}
614
615static int uniphier_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
616 struct pinctrl_gpio_range *range,
617 unsigned offset)
618{
619 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
620 const struct uniphier_pinctrl_group *groups = priv->socdata->groups;
621 int groups_count = priv->socdata->groups_count;
622 enum uniphier_pinmux_gpio_range_type range_type;
623 int i, j;
624
625 if (strstr(range->name, "irq"))
626 range_type = UNIPHIER_PINMUX_GPIO_RANGE_IRQ;
627 else
628 range_type = UNIPHIER_PINMUX_GPIO_RANGE_PORT;
629
630 for (i = 0; i < groups_count; i++) {
631 if (groups[i].range_type != range_type)
632 continue;
633
634 for (j = 0; j < groups[i].num_pins; j++)
635 if (groups[i].pins[j] == offset)
636 goto found;
637 }
638
639 dev_err(pctldev->dev, "pin %u does not support GPIO\n", offset);
640 return -EINVAL;
641
642found:
643 return uniphier_pmx_set_one_mux(pctldev, offset, groups[i].muxvals[j]);
644}
645
646static const struct pinmux_ops uniphier_pmxops = {
647 .get_functions_count = uniphier_pmx_get_functions_count,
648 .get_function_name = uniphier_pmx_get_function_name,
649 .get_function_groups = uniphier_pmx_get_function_groups,
650 .set_mux = uniphier_pmx_set_mux,
651 .gpio_request_enable = uniphier_pmx_gpio_request_enable,
652 .strict = true,
653};
654
655int uniphier_pinctrl_probe(struct platform_device *pdev,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900656 struct uniphier_pinctrl_socdata *socdata)
657{
658 struct device *dev = &pdev->dev;
659 struct uniphier_pinctrl_priv *priv;
660
661 if (!socdata ||
Masahiro Yamadafc78a562016-05-31 17:05:12 +0900662 !socdata->pins || !socdata->npins ||
Masahiro Yamada6e908892015-07-14 11:40:01 +0900663 !socdata->groups ||
664 !socdata->groups_count ||
665 !socdata->functions ||
666 !socdata->functions_count ||
667 !socdata->mux_bits ||
668 !socdata->reg_stride) {
669 dev_err(dev, "pinctrl socdata lacks necessary members\n");
670 return -EINVAL;
671 }
672
673 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
674 if (!priv)
675 return -ENOMEM;
676
677 priv->regmap = syscon_node_to_regmap(dev->of_node);
678 if (IS_ERR(priv->regmap)) {
679 dev_err(dev, "failed to get regmap\n");
680 return PTR_ERR(priv->regmap);
681 }
682
683 priv->socdata = socdata;
Masahiro Yamadafc78a562016-05-31 17:05:12 +0900684 priv->pctldesc.name = dev->driver->name;
685 priv->pctldesc.pins = socdata->pins;
686 priv->pctldesc.npins = socdata->npins;
687 priv->pctldesc.pctlops = &uniphier_pctlops;
688 priv->pctldesc.pmxops = &uniphier_pmxops;
689 priv->pctldesc.confops = &uniphier_confops;
690 priv->pctldesc.owner = dev->driver->owner;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900691
Masahiro Yamadafc78a562016-05-31 17:05:12 +0900692 priv->pctldev = devm_pinctrl_register(dev, &priv->pctldesc, priv);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900693 if (IS_ERR(priv->pctldev)) {
694 dev_err(dev, "failed to register UniPhier pinctrl driver\n");
695 return PTR_ERR(priv->pctldev);
696 }
697
698 platform_set_drvdata(pdev, priv);
699
700 return 0;
701}
702EXPORT_SYMBOL_GPL(uniphier_pinctrl_probe);