blob: 79fa170b487279e890bfafbf9d74cb6383627d77 [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 Pinchartb705c052013-03-10 16:38:23 +0100124 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
125 if (ret < 0)
126 break;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100127 }
128
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100129 spin_unlock_irqrestore(&pfc->lock, flags);
130 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900131}
132
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100133static void sh_pfc_func_disable(struct pinctrl_dev *pctldev, unsigned selector,
Paul Mundtca5481c62012-07-10 12:08:14 +0900134 unsigned group)
135{
136}
137
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100138static int sh_pfc_reconfig_pin(struct sh_pfc_pinctrl *pmx, unsigned offset,
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900139 int new_type)
140{
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100141 struct sh_pfc *pfc = pmx->pfc;
142 int idx = sh_pfc_get_pin_index(pfc, offset);
143 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100144 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pinchart934cb022013-02-14 22:35:09 +0100145 unsigned int mark = pin->enum_id;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900146 unsigned long flags;
Laurent Pinchartb705c052013-03-10 16:38:23 +0100147 int ret;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900148
149 spin_lock_irqsave(&pfc->lock, flags);
150
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100151 switch (cfg->type) {
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900152 case PINMUX_TYPE_GPIO:
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900153 case PINMUX_TYPE_OUTPUT:
154 case PINMUX_TYPE_INPUT:
155 case PINMUX_TYPE_INPUT_PULLUP:
156 case PINMUX_TYPE_INPUT_PULLDOWN:
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900157 break;
158 default:
Laurent Pinchartb705c052013-03-10 16:38:23 +0100159 ret = -EINVAL;
160 goto done;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900161 }
162
Laurent Pinchartb705c052013-03-10 16:38:23 +0100163 ret = sh_pfc_config_mux(pfc, mark, new_type);
164 if (ret < 0)
165 goto done;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900166
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100167 cfg->type = new_type;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900168
Laurent Pinchartb705c052013-03-10 16:38:23 +0100169done:
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900170 spin_unlock_irqrestore(&pfc->lock, flags);
171
172 return ret;
173}
174
Paul Mundtca5481c62012-07-10 12:08:14 +0900175static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
176 struct pinctrl_gpio_range *range,
177 unsigned offset)
178{
179 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
180 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100181 int idx = sh_pfc_get_pin_index(pfc, offset);
182 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Paul Mundtca5481c62012-07-10 12:08:14 +0900183 unsigned long flags;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100184 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900185
186 spin_lock_irqsave(&pfc->lock, flags);
187
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100188 switch (cfg->type) {
Paul Mundtd93a8912012-07-11 17:17:10 +0900189 case PINMUX_TYPE_GPIO:
Paul Mundt16d74eb2012-09-25 11:51:05 +0900190 case PINMUX_TYPE_INPUT:
191 case PINMUX_TYPE_OUTPUT:
Paul Mundtd93a8912012-07-11 17:17:10 +0900192 break;
Laurent Pinchart2119f7c2012-11-29 13:03:53 +0100193 case PINMUX_TYPE_FUNCTION:
Paul Mundtd93a8912012-07-11 17:17:10 +0900194 default:
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100195 pr_err("Unsupported mux type (%d), bailing...\n", cfg->type);
Laurent Pinchart077664a2012-09-14 20:25:48 +0200196 ret = -ENOTSUPP;
197 goto err;
Paul Mundtd93a8912012-07-11 17:17:10 +0900198 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900199
200 ret = 0;
201
202err:
203 spin_unlock_irqrestore(&pfc->lock, flags);
204
205 return ret;
206}
207
208static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
209 struct pinctrl_gpio_range *range,
210 unsigned offset)
211{
Paul Mundtca5481c62012-07-10 12:08:14 +0900212}
213
214static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
215 struct pinctrl_gpio_range *range,
216 unsigned offset, bool input)
217{
218 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900219 int type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
Paul Mundtca5481c62012-07-10 12:08:14 +0900220
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100221 return sh_pfc_reconfig_pin(pmx, offset, type);
Paul Mundtca5481c62012-07-10 12:08:14 +0900222}
223
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100224static const struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900225 .get_functions_count = sh_pfc_get_functions_count,
226 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900227 .get_function_groups = sh_pfc_get_function_groups,
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100228 .enable = sh_pfc_func_enable,
229 .disable = sh_pfc_func_disable,
Paul Mundtca5481c62012-07-10 12:08:14 +0900230 .gpio_request_enable = sh_pfc_gpio_request_enable,
231 .gpio_disable_free = sh_pfc_gpio_disable_free,
232 .gpio_set_direction = sh_pfc_gpio_set_direction,
233};
234
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100235/* Check whether the requested parameter is supported for a pin. */
236static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
237 enum pin_config_param param)
238{
239 int idx = sh_pfc_get_pin_index(pfc, _pin);
240 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
241
242 switch (param) {
243 case PIN_CONFIG_BIAS_DISABLE:
244 return true;
245
246 case PIN_CONFIG_BIAS_PULL_UP:
247 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
248
249 case PIN_CONFIG_BIAS_PULL_DOWN:
250 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
251
252 default:
253 return false;
254 }
255}
256
Laurent Pinchart934cb022013-02-14 22:35:09 +0100257static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900258 unsigned long *config)
259{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900260 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
261 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100262 enum pin_config_param param = pinconf_to_config_param(*config);
263 unsigned long flags;
264 unsigned int bias;
Paul Mundtd93a8912012-07-11 17:17:10 +0900265
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100266 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
267 return -ENOTSUPP;
268
269 switch (param) {
270 case PIN_CONFIG_BIAS_DISABLE:
271 case PIN_CONFIG_BIAS_PULL_UP:
272 case PIN_CONFIG_BIAS_PULL_DOWN:
273 if (!pfc->info->ops || !pfc->info->ops->get_bias)
274 return -ENOTSUPP;
275
276 spin_lock_irqsave(&pfc->lock, flags);
277 bias = pfc->info->ops->get_bias(pfc, _pin);
278 spin_unlock_irqrestore(&pfc->lock, flags);
279
280 if (bias != param)
281 return -EINVAL;
282
283 *config = 0;
284 break;
285
286 default:
287 return -ENOTSUPP;
288 }
Paul Mundtd93a8912012-07-11 17:17:10 +0900289
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900290 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900291}
292
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100293static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900294 unsigned long config)
295{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900296 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100297 struct sh_pfc *pfc = pmx->pfc;
298 enum pin_config_param param = pinconf_to_config_param(config);
299 unsigned long flags;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900300
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100301 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
302 return -ENOTSUPP;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900303
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100304 switch (param) {
305 case PIN_CONFIG_BIAS_PULL_UP:
306 case PIN_CONFIG_BIAS_PULL_DOWN:
307 case PIN_CONFIG_BIAS_DISABLE:
308 if (!pfc->info->ops || !pfc->info->ops->set_bias)
309 return -ENOTSUPP;
310
311 spin_lock_irqsave(&pfc->lock, flags);
312 pfc->info->ops->set_bias(pfc, _pin, param);
313 spin_unlock_irqrestore(&pfc->lock, flags);
314
315 break;
316
317 default:
318 return -ENOTSUPP;
319 }
320
321 return 0;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900322}
323
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100324static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
325 unsigned long config)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900326{
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100327 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
328 const unsigned int *pins;
329 unsigned int num_pins;
330 unsigned int i;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900331
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100332 pins = pmx->pfc->info->groups[group].pins;
333 num_pins = pmx->pfc->info->groups[group].nr_pins;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900334
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100335 for (i = 0; i < num_pins; ++i)
336 sh_pfc_pinconf_set(pctldev, pins[i], config);
337
338 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900339}
340
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100341static const struct pinconf_ops sh_pfc_pinconf_ops = {
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100342 .is_generic = true,
343 .pin_config_get = sh_pfc_pinconf_get,
344 .pin_config_set = sh_pfc_pinconf_set,
345 .pin_config_group_set = sh_pfc_pinconf_group_set,
346 .pin_config_config_dbg_show = pinconf_generic_dump_config,
Paul Mundtca5481c62012-07-10 12:08:14 +0900347};
348
Laurent Pinchart63d57382013-02-15 01:33:38 +0100349/* PFC ranges -> pinctrl pin descs */
350static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtca5481c62012-07-10 12:08:14 +0900351{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100352 const struct pinmux_range *ranges;
353 struct pinmux_range def_range;
354 unsigned int nr_ranges;
355 unsigned int nr_pins;
356 unsigned int i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900357
Laurent Pinchart63d57382013-02-15 01:33:38 +0100358 if (pfc->info->ranges == NULL) {
359 def_range.begin = 0;
360 def_range.end = pfc->info->nr_pins - 1;
361 ranges = &def_range;
362 nr_ranges = 1;
363 } else {
364 ranges = pfc->info->ranges;
365 nr_ranges = pfc->info->nr_ranges;
366 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900367
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100368 pmx->pins = devm_kzalloc(pfc->dev,
369 sizeof(*pmx->pins) * pfc->info->nr_pins,
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100370 GFP_KERNEL);
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100371 if (unlikely(!pmx->pins))
Paul Mundtca5481c62012-07-10 12:08:14 +0900372 return -ENOMEM;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100373
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100374 pmx->configs = devm_kzalloc(pfc->dev,
375 sizeof(*pmx->configs) * pfc->info->nr_pins,
376 GFP_KERNEL);
377 if (unlikely(!pmx->configs))
378 return -ENOMEM;
379
Laurent Pinchart63d57382013-02-15 01:33:38 +0100380 for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
381 const struct pinmux_range *range = &ranges[i];
382 unsigned int number;
383
384 for (number = range->begin; number <= range->end;
385 number++, nr_pins++) {
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100386 struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100387 struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100388 const struct sh_pfc_pin *info =
389 &pfc->info->pins[nr_pins];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100390
391 pin->number = number;
392 pin->name = info->name;
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100393 cfg->type = PINMUX_TYPE_GPIO;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100394 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900395 }
396
Laurent Pinchart63d57382013-02-15 01:33:38 +0100397 pfc->nr_pins = ranges[nr_ranges-1].end + 1;
Paul Mundtca5481c62012-07-10 12:08:14 +0900398
Laurent Pinchart63d57382013-02-15 01:33:38 +0100399 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900400}
401
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100402int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900403{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100404 struct sh_pfc_pinctrl *pmx;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100405 int nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900406
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100407 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100408 if (unlikely(!pmx))
409 return -ENOMEM;
Paul Mundtca5481c62012-07-10 12:08:14 +0900410
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100411 pmx->pfc = pfc;
412 pfc->pinctrl = pmx;
413
Laurent Pinchart63d57382013-02-15 01:33:38 +0100414 nr_ranges = sh_pfc_map_pins(pfc, pmx);
415 if (unlikely(nr_ranges < 0))
416 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900417
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100418 pmx->pctl_desc.name = DRV_NAME;
419 pmx->pctl_desc.owner = THIS_MODULE;
420 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
421 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
422 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100423 pmx->pctl_desc.pins = pmx->pins;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100424 pmx->pctl_desc.npins = pfc->info->nr_pins;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100425
426 pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
Wei Yongjunfd9d05b2013-03-11 22:08:12 +0800427 if (pmx->pctl == NULL)
428 return -EINVAL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900429
Paul Mundtca5481c62012-07-10 12:08:14 +0900430 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900431}
432
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100433int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900434{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100435 struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
Paul Mundtca5481c62012-07-10 12:08:14 +0900436
Paul Mundtca5481c62012-07-10 12:08:14 +0900437 pinctrl_unregister(pmx->pctl);
438
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100439 pfc->pinctrl = NULL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900440 return 0;
441}