blob: 30dec0ee7f35d89b6dbcac9503d88ad5fa876f0e [file] [log] [blame]
Masahiro Yamada6e908892015-07-14 11:40:01 +09001/*
Masahiro Yamada8ef364b32017-03-16 02:38:14 +09002 * Copyright (C) 2015-2017 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada6e908892015-07-14 11:40:01 +09004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/export.h>
17#include <linux/mfd/syscon.h>
Masahiro Yamada3e030b02016-05-31 17:05:21 +090018#include <linux/of.h>
Masahiro Yamada6e908892015-07-14 11:40:01 +090019#include <linux/pinctrl/pinconf.h>
20#include <linux/pinctrl/pinconf-generic.h>
21#include <linux/pinctrl/pinctrl.h>
22#include <linux/pinctrl/pinmux.h>
23#include <linux/platform_device.h>
24#include <linux/regmap.h>
25
26#include "../core.h"
27#include "../pinctrl-utils.h"
28#include "pinctrl-uniphier.h"
29
Masahiro Yamada8ef364b32017-03-16 02:38:14 +090030#define UNIPHIER_PINCTRL_PINMUX_BASE 0x1000
31#define UNIPHIER_PINCTRL_LOAD_PINMUX 0x1700
32#define UNIPHIER_PINCTRL_DRVCTRL_BASE 0x1800
33#define UNIPHIER_PINCTRL_DRV2CTRL_BASE 0x1900
34#define UNIPHIER_PINCTRL_DRV3CTRL_BASE 0x1980
35#define UNIPHIER_PINCTRL_PUPDCTRL_BASE 0x1a00
36#define UNIPHIER_PINCTRL_IECTRL 0x1d00
37
Masahiro Yamada6e908892015-07-14 11:40:01 +090038struct uniphier_pinctrl_priv {
Masahiro Yamadafc78a562016-05-31 17:05:12 +090039 struct pinctrl_desc pctldesc;
Masahiro Yamada6e908892015-07-14 11:40:01 +090040 struct pinctrl_dev *pctldev;
41 struct regmap *regmap;
42 struct uniphier_pinctrl_socdata *socdata;
43};
44
45static int uniphier_pctl_get_groups_count(struct pinctrl_dev *pctldev)
46{
47 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
48
49 return priv->socdata->groups_count;
50}
51
52static const char *uniphier_pctl_get_group_name(struct pinctrl_dev *pctldev,
53 unsigned selector)
54{
55 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
56
57 return priv->socdata->groups[selector].name;
58}
59
60static int uniphier_pctl_get_group_pins(struct pinctrl_dev *pctldev,
61 unsigned selector,
62 const unsigned **pins,
63 unsigned *num_pins)
64{
65 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
66
67 *pins = priv->socdata->groups[selector].pins;
68 *num_pins = priv->socdata->groups[selector].num_pins;
69
70 return 0;
71}
72
73#ifdef CONFIG_DEBUG_FS
74static void uniphier_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
75 struct seq_file *s, unsigned offset)
76{
Masahiro Yamada94bf1762016-05-31 17:05:15 +090077 const struct pin_desc *desc = pin_desc_get(pctldev, offset);
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +090078 const char *pull_dir, *drv_type;
Masahiro Yamada6e908892015-07-14 11:40:01 +090079
Masahiro Yamada94bf1762016-05-31 17:05:15 +090080 switch (uniphier_pin_get_pull_dir(desc->drv_data)) {
Masahiro Yamada6e908892015-07-14 11:40:01 +090081 case UNIPHIER_PIN_PULL_UP:
82 pull_dir = "UP";
83 break;
84 case UNIPHIER_PIN_PULL_DOWN:
85 pull_dir = "DOWN";
86 break;
Masahiro Yamada10ef8272016-05-31 15:30:10 +090087 case UNIPHIER_PIN_PULL_UP_FIXED:
88 pull_dir = "UP(FIXED)";
89 break;
90 case UNIPHIER_PIN_PULL_DOWN_FIXED:
91 pull_dir = "DOWN(FIXED)";
92 break;
Masahiro Yamada6e908892015-07-14 11:40:01 +090093 case UNIPHIER_PIN_PULL_NONE:
94 pull_dir = "NONE";
95 break;
96 default:
97 BUG();
98 }
99
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900100 switch (uniphier_pin_get_drv_type(desc->drv_data)) {
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900101 case UNIPHIER_PIN_DRV_1BIT:
102 drv_type = "4/8(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900103 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900104 case UNIPHIER_PIN_DRV_2BIT:
105 drv_type = "8/12/16/20(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900106 break;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900107 case UNIPHIER_PIN_DRV_3BIT:
108 drv_type = "4/5/7/9/11/12/14/16(mA)";
109 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900110 case UNIPHIER_PIN_DRV_FIXED4:
111 drv_type = "4(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900112 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900113 case UNIPHIER_PIN_DRV_FIXED5:
114 drv_type = "5(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900115 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900116 case UNIPHIER_PIN_DRV_FIXED8:
117 drv_type = "8(mA)";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900118 break;
119 case UNIPHIER_PIN_DRV_NONE:
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900120 drv_type = "NONE";
Masahiro Yamada6e908892015-07-14 11:40:01 +0900121 break;
122 default:
123 BUG();
124 }
125
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900126 seq_printf(s, " PULL_DIR=%s DRV_TYPE=%s", pull_dir, drv_type);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900127}
128#endif
129
130static const struct pinctrl_ops uniphier_pctlops = {
131 .get_groups_count = uniphier_pctl_get_groups_count,
132 .get_group_name = uniphier_pctl_get_group_name,
133 .get_group_pins = uniphier_pctl_get_group_pins,
134#ifdef CONFIG_DEBUG_FS
135 .pin_dbg_show = uniphier_pctl_pin_dbg_show,
136#endif
137 .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
Irina Tirdead32f7fd2016-03-31 14:44:42 +0300138 .dt_free_map = pinctrl_utils_free_map,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900139};
140
141static int uniphier_conf_pin_bias_get(struct pinctrl_dev *pctldev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900142 const struct pin_desc *desc,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900143 enum pin_config_param param)
144{
145 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
146 enum uniphier_pin_pull_dir pull_dir =
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900147 uniphier_pin_get_pull_dir(desc->drv_data);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900148 unsigned int pupdctrl, reg, shift, val;
149 unsigned int expected = 1;
150 int ret;
151
152 switch (param) {
153 case PIN_CONFIG_BIAS_DISABLE:
154 if (pull_dir == UNIPHIER_PIN_PULL_NONE)
155 return 0;
156 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
157 pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
158 return -EINVAL;
159 expected = 0;
160 break;
161 case PIN_CONFIG_BIAS_PULL_UP:
162 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED)
163 return 0;
164 if (pull_dir != UNIPHIER_PIN_PULL_UP)
165 return -EINVAL;
166 break;
167 case PIN_CONFIG_BIAS_PULL_DOWN:
168 if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED)
169 return 0;
170 if (pull_dir != UNIPHIER_PIN_PULL_DOWN)
171 return -EINVAL;
172 break;
173 default:
174 BUG();
175 }
176
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900177 pupdctrl = uniphier_pin_get_pupdctrl(desc->drv_data);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900178
179 reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
180 shift = pupdctrl % 32;
181
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900182 ret = regmap_read(priv->regmap, reg, &val);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900183 if (ret)
184 return ret;
185
186 val = (val >> shift) & 1;
187
188 return (val == expected) ? 0 : -EINVAL;
189}
190
191static int uniphier_conf_pin_drive_get(struct pinctrl_dev *pctldev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900192 const struct pin_desc *desc,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900193 u16 *strength)
194{
195 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900196 enum uniphier_pin_drv_type type =
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900197 uniphier_pin_get_drv_type(desc->drv_data);
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900198 const unsigned int strength_1bit[] = {4, 8};
199 const unsigned int strength_2bit[] = {8, 12, 16, 20};
Masahiro Yamada72e57062016-05-31 17:05:14 +0900200 const unsigned int strength_3bit[] = {4, 5, 7, 9, 11, 12, 14, 16};
Masahiro Yamada6e908892015-07-14 11:40:01 +0900201 const unsigned int *supported_strength;
202 unsigned int drvctrl, reg, shift, mask, width, val;
203 int ret;
204
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900205 switch (type) {
206 case UNIPHIER_PIN_DRV_1BIT:
207 supported_strength = strength_1bit;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900208 reg = UNIPHIER_PINCTRL_DRVCTRL_BASE;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900209 width = 1;
210 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900211 case UNIPHIER_PIN_DRV_2BIT:
212 supported_strength = strength_2bit;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900213 reg = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900214 width = 2;
215 break;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900216 case UNIPHIER_PIN_DRV_3BIT:
217 supported_strength = strength_3bit;
218 reg = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
219 width = 4;
220 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900221 case UNIPHIER_PIN_DRV_FIXED4:
Masahiro Yamada6e908892015-07-14 11:40:01 +0900222 *strength = 4;
223 return 0;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900224 case UNIPHIER_PIN_DRV_FIXED5:
Masahiro Yamada6e908892015-07-14 11:40:01 +0900225 *strength = 5;
226 return 0;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900227 case UNIPHIER_PIN_DRV_FIXED8:
Masahiro Yamada6e908892015-07-14 11:40:01 +0900228 *strength = 8;
229 return 0;
230 default:
231 /* drive strength control is not supported for this pin */
232 return -EINVAL;
233 }
234
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900235 drvctrl = uniphier_pin_get_drvctrl(desc->drv_data);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900236 drvctrl *= width;
237
Masahiro Yamada6e908892015-07-14 11:40:01 +0900238 reg += drvctrl / 32 * 4;
239 shift = drvctrl % 32;
240 mask = (1U << width) - 1;
241
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900242 ret = regmap_read(priv->regmap, reg, &val);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900243 if (ret)
244 return ret;
245
246 *strength = supported_strength[(val >> shift) & mask];
247
248 return 0;
249}
250
251static int uniphier_conf_pin_input_enable_get(struct pinctrl_dev *pctldev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900252 const struct pin_desc *desc)
Masahiro Yamada6e908892015-07-14 11:40:01 +0900253{
254 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900255 unsigned int iectrl = uniphier_pin_get_iectrl(desc->drv_data);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900256 unsigned int val;
257 int ret;
258
259 if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
260 /* This pin is always input-enabled. */
261 return 0;
262
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900263 ret = regmap_read(priv->regmap, UNIPHIER_PINCTRL_IECTRL, &val);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900264 if (ret)
265 return ret;
266
267 return val & BIT(iectrl) ? 0 : -EINVAL;
268}
269
270static int uniphier_conf_pin_config_get(struct pinctrl_dev *pctldev,
271 unsigned pin,
272 unsigned long *configs)
273{
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900274 const struct pin_desc *desc = pin_desc_get(pctldev, pin);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900275 enum pin_config_param param = pinconf_to_config_param(*configs);
276 bool has_arg = false;
277 u16 arg;
278 int ret;
279
280 switch (param) {
281 case PIN_CONFIG_BIAS_DISABLE:
282 case PIN_CONFIG_BIAS_PULL_UP:
283 case PIN_CONFIG_BIAS_PULL_DOWN:
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900284 ret = uniphier_conf_pin_bias_get(pctldev, desc, param);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900285 break;
286 case PIN_CONFIG_DRIVE_STRENGTH:
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900287 ret = uniphier_conf_pin_drive_get(pctldev, desc, &arg);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900288 has_arg = true;
289 break;
290 case PIN_CONFIG_INPUT_ENABLE:
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900291 ret = uniphier_conf_pin_input_enable_get(pctldev, desc);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900292 break;
293 default:
294 /* unsupported parameter */
295 ret = -EINVAL;
296 break;
297 }
298
299 if (ret == 0 && has_arg)
300 *configs = pinconf_to_config_packed(param, arg);
301
302 return ret;
303}
304
305static int uniphier_conf_pin_bias_set(struct pinctrl_dev *pctldev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900306 const struct pin_desc *desc,
Mika Westerberg58957d22017-01-23 15:34:32 +0300307 enum pin_config_param param, u32 arg)
Masahiro Yamada6e908892015-07-14 11:40:01 +0900308{
309 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
310 enum uniphier_pin_pull_dir pull_dir =
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900311 uniphier_pin_get_pull_dir(desc->drv_data);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900312 unsigned int pupdctrl, reg, shift;
313 unsigned int val = 1;
314
315 switch (param) {
316 case PIN_CONFIG_BIAS_DISABLE:
317 if (pull_dir == UNIPHIER_PIN_PULL_NONE)
318 return 0;
319 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED ||
320 pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED) {
321 dev_err(pctldev->dev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900322 "can not disable pull register for pin %s\n",
323 desc->name);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900324 return -EINVAL;
325 }
326 val = 0;
327 break;
328 case PIN_CONFIG_BIAS_PULL_UP:
329 if (pull_dir == UNIPHIER_PIN_PULL_UP_FIXED && arg != 0)
330 return 0;
331 if (pull_dir != UNIPHIER_PIN_PULL_UP) {
332 dev_err(pctldev->dev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900333 "pull-up is unsupported for pin %s\n",
334 desc->name);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900335 return -EINVAL;
336 }
337 if (arg == 0) {
338 dev_err(pctldev->dev, "pull-up can not be total\n");
339 return -EINVAL;
340 }
341 break;
342 case PIN_CONFIG_BIAS_PULL_DOWN:
343 if (pull_dir == UNIPHIER_PIN_PULL_DOWN_FIXED && arg != 0)
344 return 0;
345 if (pull_dir != UNIPHIER_PIN_PULL_DOWN) {
346 dev_err(pctldev->dev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900347 "pull-down is unsupported for pin %s\n",
348 desc->name);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900349 return -EINVAL;
350 }
351 if (arg == 0) {
352 dev_err(pctldev->dev, "pull-down can not be total\n");
353 return -EINVAL;
354 }
355 break;
356 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
357 if (pull_dir == UNIPHIER_PIN_PULL_NONE) {
358 dev_err(pctldev->dev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900359 "pull-up/down is unsupported for pin %s\n",
360 desc->name);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900361 return -EINVAL;
362 }
363
364 if (arg == 0)
365 return 0; /* configuration ingored */
366 break;
367 default:
368 BUG();
369 }
370
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900371 pupdctrl = uniphier_pin_get_pupdctrl(desc->drv_data);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900372
373 reg = UNIPHIER_PINCTRL_PUPDCTRL_BASE + pupdctrl / 32 * 4;
374 shift = pupdctrl % 32;
375
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900376 return regmap_update_bits(priv->regmap, reg, 1 << shift, val << shift);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900377}
378
379static int uniphier_conf_pin_drive_set(struct pinctrl_dev *pctldev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900380 const struct pin_desc *desc,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900381 u16 strength)
382{
383 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900384 enum uniphier_pin_drv_type type =
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900385 uniphier_pin_get_drv_type(desc->drv_data);
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900386 const unsigned int strength_1bit[] = {4, 8, -1};
387 const unsigned int strength_2bit[] = {8, 12, 16, 20, -1};
Masahiro Yamada72e57062016-05-31 17:05:14 +0900388 const unsigned int strength_3bit[] = {4, 5, 7, 9, 11, 12, 14, 16, -1};
Masahiro Yamada6e908892015-07-14 11:40:01 +0900389 const unsigned int *supported_strength;
390 unsigned int drvctrl, reg, shift, mask, width, val;
391
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900392 switch (type) {
393 case UNIPHIER_PIN_DRV_1BIT:
394 supported_strength = strength_1bit;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900395 reg = UNIPHIER_PINCTRL_DRVCTRL_BASE;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900396 width = 1;
397 break;
Masahiro Yamada9eaa98a2016-05-31 17:05:13 +0900398 case UNIPHIER_PIN_DRV_2BIT:
399 supported_strength = strength_2bit;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900400 reg = UNIPHIER_PINCTRL_DRV2CTRL_BASE;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900401 width = 2;
402 break;
Masahiro Yamada72e57062016-05-31 17:05:14 +0900403 case UNIPHIER_PIN_DRV_3BIT:
404 supported_strength = strength_3bit;
405 reg = UNIPHIER_PINCTRL_DRV3CTRL_BASE;
406 width = 4;
407 break;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900408 default:
409 dev_err(pctldev->dev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900410 "cannot change drive strength for pin %s\n",
411 desc->name);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900412 return -EINVAL;
413 }
414
415 for (val = 0; supported_strength[val] > 0; val++) {
416 if (supported_strength[val] > strength)
417 break;
418 }
419
420 if (val == 0) {
421 dev_err(pctldev->dev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900422 "unsupported drive strength %u mA for pin %s\n",
423 strength, desc->name);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900424 return -EINVAL;
425 }
426
427 val--;
428
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900429 drvctrl = uniphier_pin_get_drvctrl(desc->drv_data);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900430 drvctrl *= width;
431
Masahiro Yamada6e908892015-07-14 11:40:01 +0900432 reg += drvctrl / 32 * 4;
433 shift = drvctrl % 32;
434 mask = (1U << width) - 1;
435
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900436 return regmap_update_bits(priv->regmap, reg,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900437 mask << shift, val << shift);
438}
439
440static int uniphier_conf_pin_input_enable(struct pinctrl_dev *pctldev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900441 const struct pin_desc *desc,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900442 u16 enable)
443{
444 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900445 unsigned int iectrl = uniphier_pin_get_iectrl(desc->drv_data);
Masahiro Yamadaaa543882016-05-31 17:05:17 +0900446 unsigned int reg, mask;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900447
Masahiro Yamadaaa543882016-05-31 17:05:17 +0900448 /*
449 * Multiple pins share one input enable, per-pin disabling is
450 * impossible.
451 */
452 if (!(priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_PERPIN_IECTRL) &&
453 !enable)
Masahiro Yamada6e908892015-07-14 11:40:01 +0900454 return -EINVAL;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900455
Masahiro Yamadaaa543882016-05-31 17:05:17 +0900456 /* UNIPHIER_PIN_IECTRL_NONE means the pin is always input-enabled */
Masahiro Yamada6e908892015-07-14 11:40:01 +0900457 if (iectrl == UNIPHIER_PIN_IECTRL_NONE)
Masahiro Yamadaaa543882016-05-31 17:05:17 +0900458 return enable ? 0 : -EINVAL;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900459
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900460 reg = UNIPHIER_PINCTRL_IECTRL + iectrl / 32 * 4;
Masahiro Yamadaaa543882016-05-31 17:05:17 +0900461 mask = BIT(iectrl % 32);
462
463 return regmap_update_bits(priv->regmap, reg, mask, enable ? mask : 0);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900464}
465
466static int uniphier_conf_pin_config_set(struct pinctrl_dev *pctldev,
467 unsigned pin,
468 unsigned long *configs,
469 unsigned num_configs)
470{
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900471 const struct pin_desc *desc = pin_desc_get(pctldev, pin);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900472 int i, ret;
473
474 for (i = 0; i < num_configs; i++) {
475 enum pin_config_param param =
476 pinconf_to_config_param(configs[i]);
Mika Westerberg58957d22017-01-23 15:34:32 +0300477 u32 arg = pinconf_to_config_argument(configs[i]);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900478
479 switch (param) {
480 case PIN_CONFIG_BIAS_DISABLE:
481 case PIN_CONFIG_BIAS_PULL_UP:
482 case PIN_CONFIG_BIAS_PULL_DOWN:
483 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900484 ret = uniphier_conf_pin_bias_set(pctldev, desc,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900485 param, arg);
486 break;
487 case PIN_CONFIG_DRIVE_STRENGTH:
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900488 ret = uniphier_conf_pin_drive_set(pctldev, desc, arg);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900489 break;
490 case PIN_CONFIG_INPUT_ENABLE:
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900491 ret = uniphier_conf_pin_input_enable(pctldev, desc,
492 arg);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900493 break;
494 default:
495 dev_err(pctldev->dev,
496 "unsupported configuration parameter %u\n",
497 param);
498 return -EINVAL;
499 }
500
501 if (ret)
502 return ret;
503 }
504
505 return 0;
506}
507
508static int uniphier_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
509 unsigned selector,
510 unsigned long *configs,
511 unsigned num_configs)
512{
513 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
514 const unsigned *pins = priv->socdata->groups[selector].pins;
515 unsigned num_pins = priv->socdata->groups[selector].num_pins;
516 int i, ret;
517
518 for (i = 0; i < num_pins; i++) {
519 ret = uniphier_conf_pin_config_set(pctldev, pins[i],
520 configs, num_configs);
521 if (ret)
522 return ret;
523 }
524
525 return 0;
526}
527
528static const struct pinconf_ops uniphier_confops = {
529 .is_generic = true,
530 .pin_config_get = uniphier_conf_pin_config_get,
531 .pin_config_set = uniphier_conf_pin_config_set,
532 .pin_config_group_set = uniphier_conf_pin_config_group_set,
533};
534
535static int uniphier_pmx_get_functions_count(struct pinctrl_dev *pctldev)
536{
537 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
538
539 return priv->socdata->functions_count;
540}
541
542static const char *uniphier_pmx_get_function_name(struct pinctrl_dev *pctldev,
543 unsigned selector)
544{
545 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
546
547 return priv->socdata->functions[selector].name;
548}
549
550static int uniphier_pmx_get_function_groups(struct pinctrl_dev *pctldev,
551 unsigned selector,
552 const char * const **groups,
553 unsigned *num_groups)
554{
555 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
556
557 *groups = priv->socdata->functions[selector].groups;
558 *num_groups = priv->socdata->functions[selector].num_groups;
559
560 return 0;
561}
562
563static int uniphier_pmx_set_one_mux(struct pinctrl_dev *pctldev, unsigned pin,
Masahiro Yamada39ec9ac2016-05-31 17:05:18 +0900564 int muxval)
Masahiro Yamada6e908892015-07-14 11:40:01 +0900565{
566 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
Masahiro Yamadac2ebf472016-05-31 17:05:16 +0900567 unsigned int mux_bits, reg_stride, reg, reg_end, shift, mask;
568 bool load_pinctrl;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900569 int ret;
570
Masahiro Yamadabac7f4c2015-10-20 17:25:09 +0900571 /* some pins need input-enabling */
572 ret = uniphier_conf_pin_input_enable(pctldev,
Masahiro Yamada94bf1762016-05-31 17:05:15 +0900573 pin_desc_get(pctldev, pin), 1);
Masahiro Yamadabac7f4c2015-10-20 17:25:09 +0900574 if (ret)
575 return ret;
576
Masahiro Yamada39ec9ac2016-05-31 17:05:18 +0900577 if (muxval < 0)
578 return 0; /* dedicated pin; nothing to do for pin-mux */
579
Masahiro Yamadac2ebf472016-05-31 17:05:16 +0900580 if (priv->socdata->caps & UNIPHIER_PINCTRL_CAPS_DBGMUX_SEPARATE) {
581 /*
582 * Mode reg_offset bit_position
583 * Normal 4 * n shift+3:shift
584 * Debug 4 * n shift+7:shift+4
585 */
586 mux_bits = 4;
587 reg_stride = 8;
588 load_pinctrl = true;
589 } else {
590 /*
591 * Mode reg_offset bit_position
592 * Normal 8 * n shift+3:shift
593 * Debug 8 * n + 4 shift+3:shift
594 */
595 mux_bits = 8;
596 reg_stride = 4;
597 load_pinctrl = false;
598 }
599
Masahiro Yamada6e908892015-07-14 11:40:01 +0900600 reg = UNIPHIER_PINCTRL_PINMUX_BASE + pin * mux_bits / 32 * reg_stride;
601 reg_end = reg + reg_stride;
602 shift = pin * mux_bits % 32;
603 mask = (1U << mux_bits) - 1;
604
605 /*
606 * If reg_stride is greater than 4, the MSB of each pinsel shall be
607 * stored in the offset+4.
608 */
609 for (; reg < reg_end; reg += 4) {
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900610 ret = regmap_update_bits(priv->regmap, reg,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900611 mask << shift, muxval << shift);
612 if (ret)
613 return ret;
614 muxval >>= mux_bits;
615 }
616
Masahiro Yamadac2ebf472016-05-31 17:05:16 +0900617 if (load_pinctrl) {
Masahiro Yamada6e908892015-07-14 11:40:01 +0900618 ret = regmap_write(priv->regmap,
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900619 UNIPHIER_PINCTRL_LOAD_PINMUX, 1);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900620 if (ret)
621 return ret;
622 }
623
Masahiro Yamadabac7f4c2015-10-20 17:25:09 +0900624 return 0;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900625}
626
627static int uniphier_pmx_set_mux(struct pinctrl_dev *pctldev,
628 unsigned func_selector,
629 unsigned group_selector)
630{
631 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
632 const struct uniphier_pinctrl_group *grp =
633 &priv->socdata->groups[group_selector];
634 int i;
635 int ret;
636
637 for (i = 0; i < grp->num_pins; i++) {
638 ret = uniphier_pmx_set_one_mux(pctldev, grp->pins[i],
639 grp->muxvals[i]);
640 if (ret)
641 return ret;
642 }
643
644 return 0;
645}
646
647static int uniphier_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
648 struct pinctrl_gpio_range *range,
649 unsigned offset)
650{
651 struct uniphier_pinctrl_priv *priv = pinctrl_dev_get_drvdata(pctldev);
652 const struct uniphier_pinctrl_group *groups = priv->socdata->groups;
653 int groups_count = priv->socdata->groups_count;
654 enum uniphier_pinmux_gpio_range_type range_type;
655 int i, j;
656
657 if (strstr(range->name, "irq"))
658 range_type = UNIPHIER_PINMUX_GPIO_RANGE_IRQ;
659 else
660 range_type = UNIPHIER_PINMUX_GPIO_RANGE_PORT;
661
662 for (i = 0; i < groups_count; i++) {
663 if (groups[i].range_type != range_type)
664 continue;
665
666 for (j = 0; j < groups[i].num_pins; j++)
667 if (groups[i].pins[j] == offset)
668 goto found;
669 }
670
671 dev_err(pctldev->dev, "pin %u does not support GPIO\n", offset);
672 return -EINVAL;
673
674found:
675 return uniphier_pmx_set_one_mux(pctldev, offset, groups[i].muxvals[j]);
676}
677
678static const struct pinmux_ops uniphier_pmxops = {
679 .get_functions_count = uniphier_pmx_get_functions_count,
680 .get_function_name = uniphier_pmx_get_function_name,
681 .get_function_groups = uniphier_pmx_get_function_groups,
682 .set_mux = uniphier_pmx_set_mux,
683 .gpio_request_enable = uniphier_pmx_gpio_request_enable,
684 .strict = true,
685};
686
687int uniphier_pinctrl_probe(struct platform_device *pdev,
Masahiro Yamada6e908892015-07-14 11:40:01 +0900688 struct uniphier_pinctrl_socdata *socdata)
689{
690 struct device *dev = &pdev->dev;
691 struct uniphier_pinctrl_priv *priv;
Masahiro Yamada3e030b02016-05-31 17:05:21 +0900692 struct device_node *parent;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900693
694 if (!socdata ||
Masahiro Yamadafc78a562016-05-31 17:05:12 +0900695 !socdata->pins || !socdata->npins ||
Masahiro Yamadac2ebf472016-05-31 17:05:16 +0900696 !socdata->groups || !socdata->groups_count ||
697 !socdata->functions || !socdata->functions_count) {
Masahiro Yamada6e908892015-07-14 11:40:01 +0900698 dev_err(dev, "pinctrl socdata lacks necessary members\n");
699 return -EINVAL;
700 }
701
702 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
703 if (!priv)
704 return -ENOMEM;
705
Masahiro Yamada8ef364b32017-03-16 02:38:14 +0900706 parent = of_get_parent(dev->of_node);
707 priv->regmap = syscon_node_to_regmap(parent);
708 of_node_put(parent);
Masahiro Yamada3e030b02016-05-31 17:05:21 +0900709
Masahiro Yamada6e908892015-07-14 11:40:01 +0900710 if (IS_ERR(priv->regmap)) {
711 dev_err(dev, "failed to get regmap\n");
712 return PTR_ERR(priv->regmap);
713 }
714
715 priv->socdata = socdata;
Masahiro Yamadafc78a562016-05-31 17:05:12 +0900716 priv->pctldesc.name = dev->driver->name;
717 priv->pctldesc.pins = socdata->pins;
718 priv->pctldesc.npins = socdata->npins;
719 priv->pctldesc.pctlops = &uniphier_pctlops;
720 priv->pctldesc.pmxops = &uniphier_pmxops;
721 priv->pctldesc.confops = &uniphier_confops;
722 priv->pctldesc.owner = dev->driver->owner;
Masahiro Yamada6e908892015-07-14 11:40:01 +0900723
Masahiro Yamadafc78a562016-05-31 17:05:12 +0900724 priv->pctldev = devm_pinctrl_register(dev, &priv->pctldesc, priv);
Masahiro Yamada6e908892015-07-14 11:40:01 +0900725 if (IS_ERR(priv->pctldev)) {
726 dev_err(dev, "failed to register UniPhier pinctrl driver\n");
727 return PTR_ERR(priv->pctldev);
728 }
729
730 platform_set_drvdata(pdev, priv);
731
732 return 0;
733}
734EXPORT_SYMBOL_GPL(uniphier_pinctrl_probe);