blob: 3492ec9a33b78ec760561e3dc63f642e3ea84c4a [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 Pincharte3c470512013-03-10 17:30:25 +0100185 if (!pfc->gpio) {
186 /* If GPIOs are handled externally the pin mux type need to be
187 * set to GPIO here.
188 */
189 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
190
191 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
192 if (ret < 0)
193 goto done;
194 }
195
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100196 cfg->type = PINMUX_TYPE_GPIO;
197
Paul Mundtca5481c62012-07-10 12:08:14 +0900198 ret = 0;
199
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100200done:
Paul Mundtca5481c62012-07-10 12:08:14 +0900201 spin_unlock_irqrestore(&pfc->lock, flags);
202
203 return ret;
204}
205
206static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
207 struct pinctrl_gpio_range *range,
208 unsigned offset)
209{
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100210 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
211 struct sh_pfc *pfc = pmx->pfc;
212 int idx = sh_pfc_get_pin_index(pfc, offset);
213 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
214 unsigned long flags;
215
216 spin_lock_irqsave(&pfc->lock, flags);
217 cfg->type = PINMUX_TYPE_NONE;
218 spin_unlock_irqrestore(&pfc->lock, flags);
Paul Mundtca5481c62012-07-10 12:08:14 +0900219}
220
221static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
222 struct pinctrl_gpio_range *range,
223 unsigned offset, bool input)
224{
225 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100226 struct sh_pfc *pfc = pmx->pfc;
227 int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
228 int idx = sh_pfc_get_pin_index(pfc, offset);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100229 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100230 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100231 unsigned long flags;
Laurent Pinchart6dc9b452013-03-13 18:18:30 +0100232 unsigned int dir;
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100233 int ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900234
Laurent Pinchart6dc9b452013-03-13 18:18:30 +0100235 /* Check if the requested direction is supported by the pin. Not all SoC
236 * provide pin config data, so perform the check conditionally.
237 */
238 if (pin->configs) {
239 dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
240 if (!(pin->configs & dir))
241 return -EINVAL;
242 }
243
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100244 spin_lock_irqsave(&pfc->lock, flags);
245
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100246 ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100247 if (ret < 0)
248 goto done;
249
250 cfg->type = new_type;
251
252done:
253 spin_unlock_irqrestore(&pfc->lock, flags);
Laurent Pinchart0d00f002013-03-10 16:55:19 +0100254 return ret;
Paul Mundtca5481c62012-07-10 12:08:14 +0900255}
256
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100257static const struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900258 .get_functions_count = sh_pfc_get_functions_count,
259 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900260 .get_function_groups = sh_pfc_get_function_groups,
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100261 .enable = sh_pfc_func_enable,
262 .disable = sh_pfc_func_disable,
Paul Mundtca5481c62012-07-10 12:08:14 +0900263 .gpio_request_enable = sh_pfc_gpio_request_enable,
264 .gpio_disable_free = sh_pfc_gpio_disable_free,
265 .gpio_set_direction = sh_pfc_gpio_set_direction,
266};
267
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100268/* Check whether the requested parameter is supported for a pin. */
269static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
270 enum pin_config_param param)
271{
272 int idx = sh_pfc_get_pin_index(pfc, _pin);
273 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
274
275 switch (param) {
276 case PIN_CONFIG_BIAS_DISABLE:
277 return true;
278
279 case PIN_CONFIG_BIAS_PULL_UP:
280 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
281
282 case PIN_CONFIG_BIAS_PULL_DOWN:
283 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
284
285 default:
286 return false;
287 }
288}
289
Laurent Pinchart934cb022013-02-14 22:35:09 +0100290static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900291 unsigned long *config)
292{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900293 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
294 struct sh_pfc *pfc = pmx->pfc;
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100295 enum pin_config_param param = pinconf_to_config_param(*config);
296 unsigned long flags;
297 unsigned int bias;
Paul Mundtd93a8912012-07-11 17:17:10 +0900298
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100299 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
300 return -ENOTSUPP;
301
302 switch (param) {
303 case PIN_CONFIG_BIAS_DISABLE:
304 case PIN_CONFIG_BIAS_PULL_UP:
305 case PIN_CONFIG_BIAS_PULL_DOWN:
306 if (!pfc->info->ops || !pfc->info->ops->get_bias)
307 return -ENOTSUPP;
308
309 spin_lock_irqsave(&pfc->lock, flags);
310 bias = pfc->info->ops->get_bias(pfc, _pin);
311 spin_unlock_irqrestore(&pfc->lock, flags);
312
313 if (bias != param)
314 return -EINVAL;
315
316 *config = 0;
317 break;
318
319 default:
320 return -ENOTSUPP;
321 }
Paul Mundtd93a8912012-07-11 17:17:10 +0900322
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900323 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900324}
325
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100326static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
Paul Mundtca5481c62012-07-10 12:08:14 +0900327 unsigned long config)
328{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900329 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100330 struct sh_pfc *pfc = pmx->pfc;
331 enum pin_config_param param = pinconf_to_config_param(config);
332 unsigned long flags;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900333
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100334 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
335 return -ENOTSUPP;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900336
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100337 switch (param) {
338 case PIN_CONFIG_BIAS_PULL_UP:
339 case PIN_CONFIG_BIAS_PULL_DOWN:
340 case PIN_CONFIG_BIAS_DISABLE:
341 if (!pfc->info->ops || !pfc->info->ops->set_bias)
342 return -ENOTSUPP;
343
344 spin_lock_irqsave(&pfc->lock, flags);
345 pfc->info->ops->set_bias(pfc, _pin, param);
346 spin_unlock_irqrestore(&pfc->lock, flags);
347
348 break;
349
350 default:
351 return -ENOTSUPP;
352 }
353
354 return 0;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900355}
356
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100357static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
358 unsigned long config)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900359{
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100360 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
361 const unsigned int *pins;
362 unsigned int num_pins;
363 unsigned int i;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900364
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100365 pins = pmx->pfc->info->groups[group].pins;
366 num_pins = pmx->pfc->info->groups[group].nr_pins;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900367
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100368 for (i = 0; i < num_pins; ++i)
369 sh_pfc_pinconf_set(pctldev, pins[i], config);
370
371 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900372}
373
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100374static const struct pinconf_ops sh_pfc_pinconf_ops = {
Laurent Pinchartc58d9c12013-03-10 16:44:02 +0100375 .is_generic = true,
376 .pin_config_get = sh_pfc_pinconf_get,
377 .pin_config_set = sh_pfc_pinconf_set,
378 .pin_config_group_set = sh_pfc_pinconf_group_set,
379 .pin_config_config_dbg_show = pinconf_generic_dump_config,
Paul Mundtca5481c62012-07-10 12:08:14 +0900380};
381
Laurent Pinchart63d57382013-02-15 01:33:38 +0100382/* PFC ranges -> pinctrl pin descs */
383static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtca5481c62012-07-10 12:08:14 +0900384{
Laurent Pinchart63d57382013-02-15 01:33:38 +0100385 const struct pinmux_range *ranges;
386 struct pinmux_range def_range;
387 unsigned int nr_ranges;
388 unsigned int nr_pins;
389 unsigned int i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900390
Laurent Pinchart63d57382013-02-15 01:33:38 +0100391 if (pfc->info->ranges == NULL) {
392 def_range.begin = 0;
393 def_range.end = pfc->info->nr_pins - 1;
394 ranges = &def_range;
395 nr_ranges = 1;
396 } else {
397 ranges = pfc->info->ranges;
398 nr_ranges = pfc->info->nr_ranges;
399 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900400
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100401 pmx->pins = devm_kzalloc(pfc->dev,
402 sizeof(*pmx->pins) * pfc->info->nr_pins,
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100403 GFP_KERNEL);
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100404 if (unlikely(!pmx->pins))
Paul Mundtca5481c62012-07-10 12:08:14 +0900405 return -ENOMEM;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100406
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100407 pmx->configs = devm_kzalloc(pfc->dev,
408 sizeof(*pmx->configs) * pfc->info->nr_pins,
409 GFP_KERNEL);
410 if (unlikely(!pmx->configs))
411 return -ENOMEM;
412
Laurent Pinchart63d57382013-02-15 01:33:38 +0100413 for (i = 0, nr_pins = 0; i < nr_ranges; ++i) {
414 const struct pinmux_range *range = &ranges[i];
415 unsigned int number;
416
417 for (number = range->begin; number <= range->end;
418 number++, nr_pins++) {
Laurent Pinchart1a0039d2013-03-08 17:43:54 +0100419 struct sh_pfc_pin_config *cfg = &pmx->configs[nr_pins];
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100420 struct pinctrl_pin_desc *pin = &pmx->pins[nr_pins];
Laurent Pinchartcd3c1be2013-02-16 18:47:05 +0100421 const struct sh_pfc_pin *info =
422 &pfc->info->pins[nr_pins];
Laurent Pinchart63d57382013-02-15 01:33:38 +0100423
424 pin->number = number;
425 pin->name = info->name;
Laurent Pinchart9fddc4a2013-03-10 17:25:29 +0100426 cfg->type = PINMUX_TYPE_NONE;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100427 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900428 }
429
Laurent Pinchart63d57382013-02-15 01:33:38 +0100430 pfc->nr_pins = ranges[nr_ranges-1].end + 1;
Paul Mundtca5481c62012-07-10 12:08:14 +0900431
Laurent Pinchart63d57382013-02-15 01:33:38 +0100432 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900433}
434
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100435int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900436{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100437 struct sh_pfc_pinctrl *pmx;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100438 int nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900439
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100440 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100441 if (unlikely(!pmx))
442 return -ENOMEM;
Paul Mundtca5481c62012-07-10 12:08:14 +0900443
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100444 pmx->pfc = pfc;
445 pfc->pinctrl = pmx;
446
Laurent Pinchart63d57382013-02-15 01:33:38 +0100447 nr_ranges = sh_pfc_map_pins(pfc, pmx);
448 if (unlikely(nr_ranges < 0))
449 return nr_ranges;
Paul Mundtca5481c62012-07-10 12:08:14 +0900450
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100451 pmx->pctl_desc.name = DRV_NAME;
452 pmx->pctl_desc.owner = THIS_MODULE;
453 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
454 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
455 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
Laurent Pinchart3d8d9f12013-01-03 14:33:13 +0100456 pmx->pctl_desc.pins = pmx->pins;
Laurent Pinchart63d57382013-02-15 01:33:38 +0100457 pmx->pctl_desc.npins = pfc->info->nr_pins;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100458
459 pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
Wei Yongjunfd9d05b2013-03-11 22:08:12 +0800460 if (pmx->pctl == NULL)
461 return -EINVAL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900462
Paul Mundtca5481c62012-07-10 12:08:14 +0900463 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900464}
465
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100466int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900467{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100468 struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
Paul Mundtca5481c62012-07-10 12:08:14 +0900469
Paul Mundtca5481c62012-07-10 12:08:14 +0900470 pinctrl_unregister(pmx->pctl);
471
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100472 pfc->pinctrl = NULL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900473 return 0;
474}