blob: 52179bbcf6b4ba4b0a42569596f65505b1a94111 [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 Pinchart90efde22012-12-15 23:50:52 +010017#include <linux/pinctrl/consumer.h>
18#include <linux/pinctrl/pinconf.h>
19#include <linux/pinctrl/pinconf-generic.h>
20#include <linux/pinctrl/pinctrl.h>
21#include <linux/pinctrl/pinmux.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090022#include <linux/slab.h>
Paul Mundtd93a8912012-07-11 17:17:10 +090023#include <linux/spinlock.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090024
Laurent Pinchartf9165132012-12-15 23:50:44 +010025#include "core.h"
Laurent Pinchartc58d9c12013-03-10 16:44:02 +010026#include "../core.h"
27#include "../pinconf.h"
Laurent Pinchartf9165132012-12-15 23:50:44 +010028
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010029struct sh_pfc_pin_config {
30 u32 type;
31};
32
Paul Mundtca5481c62012-07-10 12:08:14 +090033struct sh_pfc_pinctrl {
34 struct pinctrl_dev *pctl;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +010035 struct pinctrl_desc pctl_desc;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +010036
Paul Mundtca5481c62012-07-10 12:08:14 +090037 struct sh_pfc *pfc;
38
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010039 struct pinctrl_pin_desc *pins;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010040 struct sh_pfc_pin_config *configs;
Paul Mundtca5481c62012-07-10 12:08:14 +090041};
42
Paul Mundte3f805e2012-07-17 15:48:18 +090043static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
Paul Mundtca5481c62012-07-10 12:08:14 +090044{
Paul Mundte3f805e2012-07-17 15:48:18 +090045 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
46
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010047 return pmx->pfc->info->nr_groups;
Paul Mundtca5481c62012-07-10 12:08:14 +090048}
49
Paul Mundte3f805e2012-07-17 15:48:18 +090050static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
Paul Mundtca5481c62012-07-10 12:08:14 +090051 unsigned selector)
52{
Paul Mundte3f805e2012-07-17 15:48:18 +090053 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
54
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010055 return pmx->pfc->info->groups[selector].name;
Paul Mundtca5481c62012-07-10 12:08:14 +090056}
57
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010058static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +090059 const unsigned **pins, unsigned *num_pins)
60{
Paul Mundte3f805e2012-07-17 15:48:18 +090061 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
62
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010063 *pins = pmx->pfc->info->groups[selector].pins;
64 *num_pins = pmx->pfc->info->groups[selector].nr_pins;
Paul Mundte3f805e2012-07-17 15:48:18 +090065
66 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +090067}
68
Paul Mundtfdd85ec2012-07-20 16:39:09 +090069static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
70 unsigned offset)
71{
72 seq_printf(s, "%s", DRV_NAME);
73}
74
Laurent Pinchartfe330ce2013-02-15 16:04:47 +010075static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
Paul Mundte3f805e2012-07-17 15:48:18 +090076 .get_groups_count = sh_pfc_get_groups_count,
77 .get_group_name = sh_pfc_get_group_name,
Paul Mundtca5481c62012-07-10 12:08:14 +090078 .get_group_pins = sh_pfc_get_group_pins,
Paul Mundtfdd85ec2012-07-20 16:39:09 +090079 .pin_dbg_show = sh_pfc_pin_dbg_show,
Paul Mundtca5481c62012-07-10 12:08:14 +090080};
81
Paul Mundtd93a8912012-07-11 17:17:10 +090082static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
83{
84 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtca5481c62012-07-10 12:08:14 +090085
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010086 return pmx->pfc->info->nr_functions;
Paul Mundtd93a8912012-07-11 17:17:10 +090087}
88
89static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
90 unsigned selector)
91{
92 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
93
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010094 return pmx->pfc->info->functions[selector].name;
Paul Mundtd93a8912012-07-11 17:17:10 +090095}
96
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010097static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
98 unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +090099 const char * const **groups,
100 unsigned * const num_groups)
101{
Paul Mundtd93a8912012-07-11 17:17:10 +0900102 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
103
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100104 *groups = pmx->pfc->info->functions[selector].groups;
105 *num_groups = pmx->pfc->info->functions[selector].nr_groups;
Paul Mundtd93a8912012-07-11 17:17:10 +0900106
Paul Mundtca5481c62012-07-10 12:08:14 +0900107 return 0;
108}
109
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100110static int sh_pfc_func_enable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900111 unsigned group)
112{
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100113 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
114 struct sh_pfc *pfc = pmx->pfc;
115 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
116 unsigned long flags;
117 unsigned int i;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100118 int ret = 0;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100119
120 spin_lock_irqsave(&pfc->lock, flags);
121
122 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100123 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
124 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
125
126 if (cfg->type != PINMUX_TYPE_NONE) {
127 ret = -EBUSY;
128 goto done;
129 }
130 }
131
132 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchartb705c052013-03-10 16:38:23 +0100133 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
134 if (ret < 0)
135 break;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100136 }
137
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100138done:
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100139 spin_unlock_irqrestore(&pfc->lock, flags);
140 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900141}
142
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100143static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900144 unsigned group)
145{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100146 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
147 struct sh_pfc *pfc = pmx->pfc;
148 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
149 unsigned long flags;
150 unsigned int i;
151
152 spin_lock_irqsave(&pfc->lock, flags);
153
154 for (i = 0; i < grp->nr_pins; ++i) {
155 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
156 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
157
158 cfg->type = PINMUX_TYPE_NONE;
159 }
160
161 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900162}
163
164static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
165 struct pinctrl_gpio_range *range,
166 unsigned offset)
167{
168 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
169 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100170 int idx = sh_pfc_get_pin_index(pfc, offset);
171 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Paul Mundtca5481c62012-07-10 12:08:14 +0900172 unsigned long flags;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100173 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900174
175 spin_lock_irqsave(&pfc->lock, flags);
176
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100177 if (cfg->type != PINMUX_TYPE_NONE) {
Laurent Pinchart9a643c92013-03-10 18:00:02 +0100178 dev_err(pfc->dev,
179 "Pin %u is busy, can't configure it as GPIO.\n",
180 offset);
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100181 ret = -EBUSY;
182 goto done;
Paul Mundtd93a8912012-07-11 17:17:10 +0900183 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900184
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100185 cfg->type = PINMUX_TYPE_GPIO;
186
Paul Mundtca5481c62012-07-10 12:08:14 +0900187 ret = 0;
188
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100189done:
Paul Mundtca5481c62012-07-10 12:08:14 +0900190 spin_unlock_irqrestore(&pfc->lock, flags);
191
192 return ret;
193}
194
195static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
196 struct pinctrl_gpio_range *range,
197 unsigned offset)
198{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100199 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
200 struct sh_pfc *pfc = pmx->pfc;
201 int idx = sh_pfc_get_pin_index(pfc, offset);
202 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
203 unsigned long flags;
204
205 spin_lock_irqsave(&pfc->lock, flags);
206 cfg->type = PINMUX_TYPE_NONE;
207 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900208}
209
210static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
211 struct pinctrl_gpio_range *range,
212 unsigned offset, bool input)
213{
214 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100215 struct sh_pfc *pfc = pmx->pfc;
216 int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
217 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100218 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100219 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100220 unsigned long flags;
221 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900222
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100223 spin_lock_irqsave(&pfc->lock, flags);
224
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100225 ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100226 if (ret < 0)
227 goto done;
228
229 cfg->type = new_type;
230
231done:
232 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100233 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900234}
235
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100236static const struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900237 .get_functions_count = sh_pfc_get_functions_count,
238 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900239 .get_function_groups = sh_pfc_get_function_groups,
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100240 .enable = sh_pfc_func_enable,
241 .disable = sh_pfc_func_disable,
Paul Mundtca5481c62012-07-10 12:08:14 +0900242 .gpio_request_enable = sh_pfc_gpio_request_enable,
243 .gpio_disable_free = sh_pfc_gpio_disable_free,
244 .gpio_set_direction = sh_pfc_gpio_set_direction,
245};
246
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100247/* Check whether the requested parameter is supported for a pin. */
248static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
249 enum pin_config_param param)
250{
251 int idx = sh_pfc_get_pin_index(pfc, _pin);
252 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
253
254 switch (param) {
255 case PIN_CONFIG_BIAS_DISABLE:
256 return true;
257
258 case PIN_CONFIG_BIAS_PULL_UP:
259 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
260
261 case PIN_CONFIG_BIAS_PULL_DOWN:
262 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
263
264 default:
265 return false;
266 }
267}
268
Laurent Pinchart934cb022013-02-14 22:35:09 +0100269static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900270 unsigned long *config)
271{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900272 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
273 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100274 enum pin_config_param param = pinconf_to_config_param(*config);
275 unsigned long flags;
276 unsigned int bias;
Paul Mundtd93a8912012-07-11 17:17:10 +0900277
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100278 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
279 return -ENOTSUPP;
280
281 switch (param) {
282 case PIN_CONFIG_BIAS_DISABLE:
283 case PIN_CONFIG_BIAS_PULL_UP:
284 case PIN_CONFIG_BIAS_PULL_DOWN:
285 if (!pfc->info->ops || !pfc->info->ops->get_bias)
286 return -ENOTSUPP;
287
288 spin_lock_irqsave(&pfc->lock, flags);
289 bias = pfc->info->ops->get_bias(pfc, _pin);
290 spin_unlock_irqrestore(&pfc->lock, flags);
291
292 if (bias != param)
293 return -EINVAL;
294
295 *config = 0;
296 break;
297
298 default:
299 return -ENOTSUPP;
300 }
Paul Mundtd93a8912012-07-11 17:17:10 +0900301
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900302 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900303}
304
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100305static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900306 unsigned long config)
307{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900308 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100309 struct sh_pfc *pfc = pmx->pfc;
310 enum pin_config_param param = pinconf_to_config_param(config);
311 unsigned long flags;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900312
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100313 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
314 return -ENOTSUPP;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900315
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100316 switch (param) {
317 case PIN_CONFIG_BIAS_PULL_UP:
318 case PIN_CONFIG_BIAS_PULL_DOWN:
319 case PIN_CONFIG_BIAS_DISABLE:
320 if (!pfc->info->ops || !pfc->info->ops->set_bias)
321 return -ENOTSUPP;
322
323 spin_lock_irqsave(&pfc->lock, flags);
324 pfc->info->ops->set_bias(pfc, _pin, param);
325 spin_unlock_irqrestore(&pfc->lock, flags);
326
327 break;
328
329 default:
330 return -ENOTSUPP;
331 }
332
333 return 0;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900334}
335
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100336static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
337 unsigned long config)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900338{
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100339 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
340 const unsigned int *pins;
341 unsigned int num_pins;
342 unsigned int i;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900343
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100344 pins = pmx->pfc->info->groups[group].pins;
345 num_pins = pmx->pfc->info->groups[group].nr_pins;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900346
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100347 for (i = 0; i < num_pins; ++i)
348 sh_pfc_pinconf_set(pctldev, pins[i], config);
349
350 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900351}
352
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100353static const struct pinconf_ops sh_pfc_pinconf_ops = {
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100354 .is_generic = true,
355 .pin_config_get = sh_pfc_pinconf_get,
356 .pin_config_set = sh_pfc_pinconf_set,
357 .pin_config_group_set = sh_pfc_pinconf_group_set,
358 .pin_config_config_dbg_show = pinconf_generic_dump_config,
Paul Mundtca5481c62012-07-10 12:08:14 +0900359};
360
Laurent Pinchart63d57382013-02-15 01:33:38 +0100361/* PFC ranges -> pinctrl pin descs */
362static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtca5481c62012-07-10 12:08:14 +0900363{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100364 const struct pinmux_range *ranges;
365 struct pinmux_range def_range;
366 unsigned int nr_ranges;
367 unsigned int nr_pins;
368 unsigned int i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900369
Laurent Pinchart63d57382013-02-15 01:33:38 +0100370 if (pfc->info->ranges == NULL) {
371 def_range.begin = 0;
372 def_range.end = pfc->info->nr_pins - 1;
373 ranges = &def_range;
374 nr_ranges = 1;
375 } else {
376 ranges = pfc->info->ranges;
377 nr_ranges = pfc->info->nr_ranges;
378 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900379
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100380 pmx->pins = devm_kzalloc(pfc->dev,
381 sizeof(*pmx->pins) * pfc->info->nr_pins,
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100382 GFP_KERNEL);
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100383 if (unlikely(!pmx->pins))
Paul Mundtca5481c62012-07-10 12:08:14 +0900384 return -ENOMEM;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100385
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100386 pmx->configs = devm_kzalloc(pfc->dev,
387 sizeof(*pmx->configs) * pfc->info->nr_pins,
388 GFP_KERNEL);
389 if (unlikely(!pmx->configs))
390 return -ENOMEM;
391
Laurent Pinchart63d57382013-02-15 01:33:38 +0100392 for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
393 const struct pinmux_range *range = &ranges[i];
394 unsigned int number;
395
396 for (number = range->begin; number <= range->end;
397 number++, nr_pins++) {
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100398 struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100399 struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100400 const struct sh_pfc_pin *info =
401 &pfc->info->pins[nr_pins];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100402
403 pin->number = number;
404 pin->name = info->name;
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100405 cfg->type = PINMUX_TYPE_NONE;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100406 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900407 }
408
Laurent Pinchart63d57382013-02-15 01:33:38 +0100409 pfc->nr_pins = ranges[nr_ranges-1].end + 1;
Paul Mundtca5481c62012-07-10 12:08:14 +0900410
Laurent Pinchart63d57382013-02-15 01:33:38 +0100411 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900412}
413
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100414int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900415{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100416 struct sh_pfc_pinctrl *pmx;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100417 int nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900418
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100419 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100420 if (unlikely(!pmx))
421 return -ENOMEM;
Paul Mundtca5481c62012-07-10 12:08:14 +0900422
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100423 pmx->pfc = pfc;
424 pfc->pinctrl = pmx;
425
Laurent Pinchart63d57382013-02-15 01:33:38 +0100426 nr_ranges = sh_pfc_map_pins(pfc, pmx);
427 if (unlikely(nr_ranges < 0))
428 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900429
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100430 pmx->pctl_desc.name = DRV_NAME;
431 pmx->pctl_desc.owner = THIS_MODULE;
432 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
433 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
434 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100435 pmx->pctl_desc.pins = pmx->pins;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100436 pmx->pctl_desc.npins = pfc->info->nr_pins;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100437
438 pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
Wei Yongjunfd9d05b2013-03-11 22:08:12 +0800439 if (pmx->pctl == NULL)
440 return -EINVAL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900441
Paul Mundtca5481c62012-07-10 12:08:14 +0900442 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900443}
444
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100445int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900446{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100447 struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
Paul Mundtca5481c62012-07-10 12:08:14 +0900448
Paul Mundtca5481c62012-07-10 12:08:14 +0900449 pinctrl_unregister(pmx->pctl);
450
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100451 pfc->pinctrl = NULL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900452 return 0;
453}