blob: 82e4fb21853ef750e3efc66206479d6afed985b2 [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"
Laurent Pinchartf9492fd2012-12-15 23:50:45 +010012#define pr_fmt(fmt) KBUILD_MODNAME " pinctrl: " fmt
Paul Mundtca5481c62012-07-10 12:08:14 +090013
Laurent Pinchart1724acf2012-12-15 23:50:48 +010014#include <linux/device.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010015#include <linux/err.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090016#include <linux/init.h>
17#include <linux/module.h>
Laurent Pinchart90efde22012-12-15 23:50:52 +010018#include <linux/pinctrl/consumer.h>
19#include <linux/pinctrl/pinconf.h>
20#include <linux/pinctrl/pinconf-generic.h>
21#include <linux/pinctrl/pinctrl.h>
22#include <linux/pinctrl/pinmux.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090023#include <linux/slab.h>
Paul Mundtd93a8912012-07-11 17:17:10 +090024#include <linux/spinlock.h>
Paul Mundtca5481c62012-07-10 12:08:14 +090025
Laurent Pinchartf9165132012-12-15 23:50:44 +010026#include "core.h"
Laurent Pinchartc58d9c12013-03-10 16:44:02 +010027#include "../core.h"
28#include "../pinconf.h"
Laurent Pinchartf9165132012-12-15 23:50:44 +010029
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010030struct sh_pfc_pin_config {
31 u32 type;
32};
33
Paul Mundtca5481c62012-07-10 12:08:14 +090034struct sh_pfc_pinctrl {
35 struct pinctrl_dev *pctl;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +010036 struct pinctrl_desc pctl_desc;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +010037
Paul Mundtca5481c62012-07-10 12:08:14 +090038 struct sh_pfc *pfc;
39
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010040 struct pinctrl_pin_desc *pins;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +010041 struct sh_pfc_pin_config *configs;
Paul Mundtca5481c62012-07-10 12:08:14 +090042};
43
Paul Mundte3f805e2012-07-17 15:48:18 +090044static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
Paul Mundtca5481c62012-07-10 12:08:14 +090045{
Paul Mundte3f805e2012-07-17 15:48:18 +090046 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
47
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010048 return pmx->pfc->info->nr_groups;
Paul Mundtca5481c62012-07-10 12:08:14 +090049}
50
Paul Mundte3f805e2012-07-17 15:48:18 +090051static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
Paul Mundtca5481c62012-07-10 12:08:14 +090052 unsigned selector)
53{
Paul Mundte3f805e2012-07-17 15:48:18 +090054 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
55
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010056 return pmx->pfc->info->groups[selector].name;
Paul Mundtca5481c62012-07-10 12:08:14 +090057}
58
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010059static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +090060 const unsigned **pins, unsigned *num_pins)
61{
Paul Mundte3f805e2012-07-17 15:48:18 +090062 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
63
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010064 *pins = pmx->pfc->info->groups[selector].pins;
65 *num_pins = pmx->pfc->info->groups[selector].nr_pins;
Paul Mundte3f805e2012-07-17 15:48:18 +090066
67 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +090068}
69
Paul Mundtfdd85ec2012-07-20 16:39:09 +090070static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
71 unsigned offset)
72{
73 seq_printf(s, "%s", DRV_NAME);
74}
75
Laurent Pinchartfe330ce2013-02-15 16:04:47 +010076static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
Paul Mundte3f805e2012-07-17 15:48:18 +090077 .get_groups_count = sh_pfc_get_groups_count,
78 .get_group_name = sh_pfc_get_group_name,
Paul Mundtca5481c62012-07-10 12:08:14 +090079 .get_group_pins = sh_pfc_get_group_pins,
Paul Mundtfdd85ec2012-07-20 16:39:09 +090080 .pin_dbg_show = sh_pfc_pin_dbg_show,
Paul Mundtca5481c62012-07-10 12:08:14 +090081};
82
Paul Mundtd93a8912012-07-11 17:17:10 +090083static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
84{
85 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtca5481c62012-07-10 12:08:14 +090086
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010087 return pmx->pfc->info->nr_functions;
Paul Mundtd93a8912012-07-11 17:17:10 +090088}
89
90static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
91 unsigned selector)
92{
93 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
94
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010095 return pmx->pfc->info->functions[selector].name;
Paul Mundtd93a8912012-07-11 17:17:10 +090096}
97
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +010098static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
99 unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900100 const char * const **groups,
101 unsigned * const num_groups)
102{
Paul Mundtd93a8912012-07-11 17:17:10 +0900103 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
104
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100105 *groups = pmx->pfc->info->functions[selector].groups;
106 *num_groups = pmx->pfc->info->functions[selector].nr_groups;
Paul Mundtd93a8912012-07-11 17:17:10 +0900107
Paul Mundtca5481c62012-07-10 12:08:14 +0900108 return 0;
109}
110
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100111static int sh_pfc_func_enable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900112 unsigned group)
113{
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100114 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
115 struct sh_pfc *pfc = pmx->pfc;
116 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
117 unsigned long flags;
118 unsigned int i;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100119 int ret = 0;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100120
121 spin_lock_irqsave(&pfc->lock, flags);
122
123 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100124 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
125 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
126
127 if (cfg->type != PINMUX_TYPE_NONE) {
128 ret = -EBUSY;
129 goto done;
130 }
131 }
132
133 for (i = 0; i < grp->nr_pins; ++i) {
Laurent Pinchartb705c052013-03-10 16:38:23 +0100134 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
135 if (ret < 0)
136 break;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100137 }
138
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100139done:
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100140 spin_unlock_irqrestore(&pfc->lock, flags);
141 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900142}
143
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100144static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900145 unsigned group)
146{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100147 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
148 struct sh_pfc *pfc = pmx->pfc;
149 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
150 unsigned long flags;
151 unsigned int i;
152
153 spin_lock_irqsave(&pfc->lock, flags);
154
155 for (i = 0; i < grp->nr_pins; ++i) {
156 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
157 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
158
159 cfg->type = PINMUX_TYPE_NONE;
160 }
161
162 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900163}
164
165static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
166 struct pinctrl_gpio_range *range,
167 unsigned offset)
168{
169 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
170 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100171 int idx = sh_pfc_get_pin_index(pfc, offset);
172 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Paul Mundtca5481c62012-07-10 12:08:14 +0900173 unsigned long flags;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100174 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900175
176 spin_lock_irqsave(&pfc->lock, flags);
177
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100178 if (cfg->type != PINMUX_TYPE_NONE) {
179 pr_err("Pin %u is busy, can't configure it as GPIO.\n", offset);
180 ret = -EBUSY;
181 goto done;
Paul Mundtd93a8912012-07-11 17:17:10 +0900182 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900183
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100184 cfg->type = PINMUX_TYPE_GPIO;
185
Paul Mundtca5481c62012-07-10 12:08:14 +0900186 ret = 0;
187
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100188done:
Paul Mundtca5481c62012-07-10 12:08:14 +0900189 spin_unlock_irqrestore(&pfc->lock, flags);
190
191 return ret;
192}
193
194static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
195 struct pinctrl_gpio_range *range,
196 unsigned offset)
197{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100198 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
199 struct sh_pfc *pfc = pmx->pfc;
200 int idx = sh_pfc_get_pin_index(pfc, offset);
201 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
202 unsigned long flags;
203
204 spin_lock_irqsave(&pfc->lock, flags);
205 cfg->type = PINMUX_TYPE_NONE;
206 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900207}
208
209static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
210 struct pinctrl_gpio_range *range,
211 unsigned offset, bool input)
212{
213 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100214 struct sh_pfc *pfc = pmx->pfc;
215 int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
216 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100217 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100218 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100219 unsigned long flags;
220 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900221
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100222 spin_lock_irqsave(&pfc->lock, flags);
223
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100224 ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100225 if (ret < 0)
226 goto done;
227
228 cfg->type = new_type;
229
230done:
231 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100232 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900233}
234
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100235static const struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900236 .get_functions_count = sh_pfc_get_functions_count,
237 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900238 .get_function_groups = sh_pfc_get_function_groups,
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100239 .enable = sh_pfc_func_enable,
240 .disable = sh_pfc_func_disable,
Paul Mundtca5481c62012-07-10 12:08:14 +0900241 .gpio_request_enable = sh_pfc_gpio_request_enable,
242 .gpio_disable_free = sh_pfc_gpio_disable_free,
243 .gpio_set_direction = sh_pfc_gpio_set_direction,
244};
245
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100246/* Check whether the requested parameter is supported for a pin. */
247static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
248 enum pin_config_param param)
249{
250 int idx = sh_pfc_get_pin_index(pfc, _pin);
251 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
252
253 switch (param) {
254 case PIN_CONFIG_BIAS_DISABLE:
255 return true;
256
257 case PIN_CONFIG_BIAS_PULL_UP:
258 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
259
260 case PIN_CONFIG_BIAS_PULL_DOWN:
261 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
262
263 default:
264 return false;
265 }
266}
267
Laurent Pinchart934cb022013-02-14 22:35:09 +0100268static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900269 unsigned long *config)
270{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900271 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
272 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100273 enum pin_config_param param = pinconf_to_config_param(*config);
274 unsigned long flags;
275 unsigned int bias;
Paul Mundtd93a8912012-07-11 17:17:10 +0900276
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100277 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
278 return -ENOTSUPP;
279
280 switch (param) {
281 case PIN_CONFIG_BIAS_DISABLE:
282 case PIN_CONFIG_BIAS_PULL_UP:
283 case PIN_CONFIG_BIAS_PULL_DOWN:
284 if (!pfc->info->ops || !pfc->info->ops->get_bias)
285 return -ENOTSUPP;
286
287 spin_lock_irqsave(&pfc->lock, flags);
288 bias = pfc->info->ops->get_bias(pfc, _pin);
289 spin_unlock_irqrestore(&pfc->lock, flags);
290
291 if (bias != param)
292 return -EINVAL;
293
294 *config = 0;
295 break;
296
297 default:
298 return -ENOTSUPP;
299 }
Paul Mundtd93a8912012-07-11 17:17:10 +0900300
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900301 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900302}
303
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100304static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900305 unsigned long config)
306{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900307 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100308 struct sh_pfc *pfc = pmx->pfc;
309 enum pin_config_param param = pinconf_to_config_param(config);
310 unsigned long flags;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900311
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100312 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
313 return -ENOTSUPP;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900314
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100315 switch (param) {
316 case PIN_CONFIG_BIAS_PULL_UP:
317 case PIN_CONFIG_BIAS_PULL_DOWN:
318 case PIN_CONFIG_BIAS_DISABLE:
319 if (!pfc->info->ops || !pfc->info->ops->set_bias)
320 return -ENOTSUPP;
321
322 spin_lock_irqsave(&pfc->lock, flags);
323 pfc->info->ops->set_bias(pfc, _pin, param);
324 spin_unlock_irqrestore(&pfc->lock, flags);
325
326 break;
327
328 default:
329 return -ENOTSUPP;
330 }
331
332 return 0;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900333}
334
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100335static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
336 unsigned long config)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900337{
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100338 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
339 const unsigned int *pins;
340 unsigned int num_pins;
341 unsigned int i;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900342
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100343 pins = pmx->pfc->info->groups[group].pins;
344 num_pins = pmx->pfc->info->groups[group].nr_pins;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900345
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100346 for (i = 0; i < num_pins; ++i)
347 sh_pfc_pinconf_set(pctldev, pins[i], config);
348
349 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900350}
351
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100352static const struct pinconf_ops sh_pfc_pinconf_ops = {
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100353 .is_generic = true,
354 .pin_config_get = sh_pfc_pinconf_get,
355 .pin_config_set = sh_pfc_pinconf_set,
356 .pin_config_group_set = sh_pfc_pinconf_group_set,
357 .pin_config_config_dbg_show = pinconf_generic_dump_config,
Paul Mundtca5481c62012-07-10 12:08:14 +0900358};
359
Laurent Pinchart63d57382013-02-15 01:33:38 +0100360/* PFC ranges -> pinctrl pin descs */
361static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtca5481c62012-07-10 12:08:14 +0900362{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100363 const struct pinmux_range *ranges;
364 struct pinmux_range def_range;
365 unsigned int nr_ranges;
366 unsigned int nr_pins;
367 unsigned int i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900368
Laurent Pinchart63d57382013-02-15 01:33:38 +0100369 if (pfc->info->ranges == NULL) {
370 def_range.begin = 0;
371 def_range.end = pfc->info->nr_pins - 1;
372 ranges = &def_range;
373 nr_ranges = 1;
374 } else {
375 ranges = pfc->info->ranges;
376 nr_ranges = pfc->info->nr_ranges;
377 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900378
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100379 pmx->pins = devm_kzalloc(pfc->dev,
380 sizeof(*pmx->pins) * pfc->info->nr_pins,
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100381 GFP_KERNEL);
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100382 if (unlikely(!pmx->pins))
Paul Mundtca5481c62012-07-10 12:08:14 +0900383 return -ENOMEM;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100384
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100385 pmx->configs = devm_kzalloc(pfc->dev,
386 sizeof(*pmx->configs) * pfc->info->nr_pins,
387 GFP_KERNEL);
388 if (unlikely(!pmx->configs))
389 return -ENOMEM;
390
Laurent Pinchart63d57382013-02-15 01:33:38 +0100391 for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
392 const struct pinmux_range *range = &ranges[i];
393 unsigned int number;
394
395 for (number = range->begin; number <= range->end;
396 number++, nr_pins++) {
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100397 struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100398 struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100399 const struct sh_pfc_pin *info =
400 &pfc->info->pins[nr_pins];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100401
402 pin->number = number;
403 pin->name = info->name;
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100404 cfg->type = PINMUX_TYPE_NONE;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100405 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900406 }
407
Laurent Pinchart63d57382013-02-15 01:33:38 +0100408 pfc->nr_pins = ranges[nr_ranges-1].end + 1;
Paul Mundtca5481c62012-07-10 12:08:14 +0900409
Laurent Pinchart63d57382013-02-15 01:33:38 +0100410 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900411}
412
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100413int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900414{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100415 struct sh_pfc_pinctrl *pmx;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100416 int nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900417
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100418 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100419 if (unlikely(!pmx))
420 return -ENOMEM;
Paul Mundtca5481c62012-07-10 12:08:14 +0900421
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100422 pmx->pfc = pfc;
423 pfc->pinctrl = pmx;
424
Laurent Pinchart63d57382013-02-15 01:33:38 +0100425 nr_ranges = sh_pfc_map_pins(pfc, pmx);
426 if (unlikely(nr_ranges < 0))
427 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900428
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100429 pmx->pctl_desc.name = DRV_NAME;
430 pmx->pctl_desc.owner = THIS_MODULE;
431 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
432 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
433 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100434 pmx->pctl_desc.pins = pmx->pins;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100435 pmx->pctl_desc.npins = pfc->info->nr_pins;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100436
437 pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
Wei Yongjunfd9d05b2013-03-11 22:08:12 +0800438 if (pmx->pctl == NULL)
439 return -EINVAL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900440
Paul Mundtca5481c62012-07-10 12:08:14 +0900441 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900442}
443
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100444int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900445{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100446 struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
Paul Mundtca5481c62012-07-10 12:08:14 +0900447
Paul Mundtca5481c62012-07-10 12:08:14 +0900448 pinctrl_unregister(pmx->pctl);
449
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100450 pfc->pinctrl = NULL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900451 return 0;
452}