blob: a83f40070b3ba7ddc81b578e198bdd1c3ad736ae [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"
27
Paul Mundtca5481c62012-07-10 12:08:14 +090028struct sh_pfc_pinctrl {
29 struct pinctrl_dev *pctl;
Laurent Pinchartdcc427e2013-02-16 16:38:30 +010030 struct pinctrl_desc pctl_desc;
31 struct pinctrl_gpio_range range;
32
Paul Mundtca5481c62012-07-10 12:08:14 +090033 struct sh_pfc *pfc;
34
Laurent Pincharta373ed02012-11-29 13:24:07 +010035 struct pinmux_func **functions;
Paul Mundtd93a8912012-07-11 17:17:10 +090036 unsigned int nr_functions;
37
Paul Mundtca5481c62012-07-10 12:08:14 +090038 struct pinctrl_pin_desc *pads;
39 unsigned int nr_pads;
40};
41
Paul Mundte3f805e2012-07-17 15:48:18 +090042static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
Paul Mundtca5481c62012-07-10 12:08:14 +090043{
Paul Mundte3f805e2012-07-17 15:48:18 +090044 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
45
46 return pmx->nr_pads;
Paul Mundtca5481c62012-07-10 12:08:14 +090047}
48
Paul Mundte3f805e2012-07-17 15:48:18 +090049static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
Paul Mundtca5481c62012-07-10 12:08:14 +090050 unsigned selector)
51{
Paul Mundte3f805e2012-07-17 15:48:18 +090052 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
53
54 return pmx->pads[selector].name;
Paul Mundtca5481c62012-07-10 12:08:14 +090055}
56
57static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
58 const unsigned **pins, unsigned *num_pins)
59{
Paul Mundte3f805e2012-07-17 15:48:18 +090060 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
61
62 *pins = &pmx->pads[group].number;
63 *num_pins = 1;
64
65 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +090066}
67
Paul Mundtfdd85ec2012-07-20 16:39:09 +090068static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
69 unsigned offset)
70{
71 seq_printf(s, "%s", DRV_NAME);
72}
73
Laurent Pinchartfe330ce2013-02-15 16:04:47 +010074static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
Paul Mundte3f805e2012-07-17 15:48:18 +090075 .get_groups_count = sh_pfc_get_groups_count,
76 .get_group_name = sh_pfc_get_group_name,
Paul Mundtca5481c62012-07-10 12:08:14 +090077 .get_group_pins = sh_pfc_get_group_pins,
Paul Mundtfdd85ec2012-07-20 16:39:09 +090078 .pin_dbg_show = sh_pfc_pin_dbg_show,
Paul Mundtca5481c62012-07-10 12:08:14 +090079};
80
Paul Mundtd93a8912012-07-11 17:17:10 +090081static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
82{
83 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtca5481c62012-07-10 12:08:14 +090084
Paul Mundtd93a8912012-07-11 17:17:10 +090085 return pmx->nr_functions;
86}
87
88static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
89 unsigned selector)
90{
91 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
92
93 return pmx->functions[selector]->name;
94}
95
Paul Mundtca5481c62012-07-10 12:08:14 +090096static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, unsigned func,
97 const char * const **groups,
98 unsigned * const num_groups)
99{
Paul Mundtd93a8912012-07-11 17:17:10 +0900100 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
101
102 *groups = &pmx->functions[func]->name;
103 *num_groups = 1;
104
Paul Mundtca5481c62012-07-10 12:08:14 +0900105 return 0;
106}
107
108static int sh_pfc_noop_enable(struct pinctrl_dev *pctldev, unsigned func,
109 unsigned group)
110{
111 return 0;
112}
113
114static void sh_pfc_noop_disable(struct pinctrl_dev *pctldev, unsigned func,
115 unsigned group)
116{
117}
118
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900119static int sh_pfc_reconfig_pin(struct sh_pfc *pfc, unsigned offset,
120 int new_type)
121{
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100122 unsigned int mark = pfc->info->pins[offset].enum_id;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900123 unsigned long flags;
124 int pinmux_type;
125 int ret = -EINVAL;
126
127 spin_lock_irqsave(&pfc->lock, flags);
128
Laurent Pincharta373ed02012-11-29 13:24:07 +0100129 pinmux_type = pfc->info->pins[offset].flags & PINMUX_FLAG_TYPE;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900130
131 /*
132 * See if the present config needs to first be de-configured.
133 */
134 switch (pinmux_type) {
135 case PINMUX_TYPE_GPIO:
136 break;
137 case PINMUX_TYPE_OUTPUT:
138 case PINMUX_TYPE_INPUT:
139 case PINMUX_TYPE_INPUT_PULLUP:
140 case PINMUX_TYPE_INPUT_PULLDOWN:
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100141 sh_pfc_config_mux(pfc, mark, pinmux_type, GPIO_CFG_FREE);
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900142 break;
143 default:
144 goto err;
145 }
146
147 /*
148 * Dry run
149 */
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100150 if (sh_pfc_config_mux(pfc, mark, new_type, GPIO_CFG_DRYRUN) != 0)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900151 goto err;
152
153 /*
154 * Request
155 */
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100156 if (sh_pfc_config_mux(pfc, mark, new_type, GPIO_CFG_REQ) != 0)
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900157 goto err;
158
Laurent Pincharta373ed02012-11-29 13:24:07 +0100159 pfc->info->pins[offset].flags &= ~PINMUX_FLAG_TYPE;
160 pfc->info->pins[offset].flags |= new_type;
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900161
162 ret = 0;
163
164err:
165 spin_unlock_irqrestore(&pfc->lock, flags);
166
167 return ret;
168}
169
Paul Mundtca5481c62012-07-10 12:08:14 +0900170static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
171 struct pinctrl_gpio_range *range,
172 unsigned offset)
173{
174 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
175 struct sh_pfc *pfc = pmx->pfc;
Paul Mundtca5481c62012-07-10 12:08:14 +0900176 unsigned long flags;
Paul Mundtd93a8912012-07-11 17:17:10 +0900177 int ret, pinmux_type;
Paul Mundtca5481c62012-07-10 12:08:14 +0900178
179 spin_lock_irqsave(&pfc->lock, flags);
180
Laurent Pincharta373ed02012-11-29 13:24:07 +0100181 pinmux_type = pfc->info->pins[offset].flags & PINMUX_FLAG_TYPE;
Paul Mundtca5481c62012-07-10 12:08:14 +0900182
Paul Mundtd93a8912012-07-11 17:17:10 +0900183 switch (pinmux_type) {
Paul Mundtd93a8912012-07-11 17:17:10 +0900184 case PINMUX_TYPE_GPIO:
Paul Mundt16d74eb2012-09-25 11:51:05 +0900185 case PINMUX_TYPE_INPUT:
186 case PINMUX_TYPE_OUTPUT:
Paul Mundtd93a8912012-07-11 17:17:10 +0900187 break;
Laurent Pinchart2119f7c2012-11-29 13:03:53 +0100188 case PINMUX_TYPE_FUNCTION:
Paul Mundtd93a8912012-07-11 17:17:10 +0900189 default:
190 pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type);
Laurent Pinchart077664a2012-09-14 20:25:48 +0200191 ret = -ENOTSUPP;
192 goto err;
Paul Mundtd93a8912012-07-11 17:17:10 +0900193 }
Paul Mundtca5481c62012-07-10 12:08:14 +0900194
195 ret = 0;
196
197err:
198 spin_unlock_irqrestore(&pfc->lock, flags);
199
200 return ret;
201}
202
203static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
204 struct pinctrl_gpio_range *range,
205 unsigned offset)
206{
207 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
208 struct sh_pfc *pfc = pmx->pfc;
209 unsigned long flags;
210 int pinmux_type;
211
212 spin_lock_irqsave(&pfc->lock, flags);
213
Laurent Pincharta373ed02012-11-29 13:24:07 +0100214 pinmux_type = pfc->info->pins[offset].flags & PINMUX_FLAG_TYPE;
Paul Mundtca5481c62012-07-10 12:08:14 +0900215
Laurent Pincharta68fdca92013-02-14 17:36:56 +0100216 sh_pfc_config_mux(pfc, pfc->info->pins[offset].enum_id, pinmux_type,
217 GPIO_CFG_FREE);
Paul Mundtca5481c62012-07-10 12:08:14 +0900218
Paul Mundtca5481c62012-07-10 12:08:14 +0900219 spin_unlock_irqrestore(&pfc->lock, flags);
220}
221
222static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
223 struct pinctrl_gpio_range *range,
224 unsigned offset, bool input)
225{
226 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900227 int type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
Paul Mundtca5481c62012-07-10 12:08:14 +0900228
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900229 return sh_pfc_reconfig_pin(pmx->pfc, offset, type);
Paul Mundtca5481c62012-07-10 12:08:14 +0900230}
231
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100232static const struct pinmux_ops sh_pfc_pinmux_ops = {
Paul Mundtd93a8912012-07-11 17:17:10 +0900233 .get_functions_count = sh_pfc_get_functions_count,
234 .get_function_name = sh_pfc_get_function_name,
Paul Mundtca5481c62012-07-10 12:08:14 +0900235 .get_function_groups = sh_pfc_get_function_groups,
236 .enable = sh_pfc_noop_enable,
237 .disable = sh_pfc_noop_disable,
238 .gpio_request_enable = sh_pfc_gpio_request_enable,
239 .gpio_disable_free = sh_pfc_gpio_disable_free,
240 .gpio_set_direction = sh_pfc_gpio_set_direction,
241};
242
243static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
244 unsigned long *config)
245{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900246 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
247 struct sh_pfc *pfc = pmx->pfc;
Paul Mundtd93a8912012-07-11 17:17:10 +0900248
Laurent Pincharta373ed02012-11-29 13:24:07 +0100249 *config = pfc->info->pins[pin].flags & PINMUX_FLAG_TYPE;
Paul Mundtd93a8912012-07-11 17:17:10 +0900250
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900251 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900252}
253
254static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
255 unsigned long config)
256{
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900257 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900258
259 /* Validate the new type */
260 if (config >= PINMUX_FLAG_TYPE)
261 return -EINVAL;
262
263 return sh_pfc_reconfig_pin(pmx->pfc, pin, config);
264}
265
266static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev *pctldev,
267 struct seq_file *s, unsigned pin)
268{
269 const char *pinmux_type_str[] = {
270 [PINMUX_TYPE_NONE] = "none",
271 [PINMUX_TYPE_FUNCTION] = "function",
272 [PINMUX_TYPE_GPIO] = "gpio",
273 [PINMUX_TYPE_OUTPUT] = "output",
274 [PINMUX_TYPE_INPUT] = "input",
275 [PINMUX_TYPE_INPUT_PULLUP] = "input bias pull up",
276 [PINMUX_TYPE_INPUT_PULLDOWN] = "input bias pull down",
277 };
278 unsigned long config;
279 int rc;
280
281 rc = sh_pfc_pinconf_get(pctldev, pin, &config);
282 if (unlikely(rc != 0))
283 return;
284
285 seq_printf(s, " %s", pinmux_type_str[config]);
Paul Mundtca5481c62012-07-10 12:08:14 +0900286}
287
Laurent Pinchartfe330ce2013-02-15 16:04:47 +0100288static const struct pinconf_ops sh_pfc_pinconf_ops = {
Paul Mundtca5481c62012-07-10 12:08:14 +0900289 .pin_config_get = sh_pfc_pinconf_get,
290 .pin_config_set = sh_pfc_pinconf_set,
Paul Mundtfdd85ec2012-07-20 16:39:09 +0900291 .pin_config_dbg_show = sh_pfc_pinconf_dbg_show,
Paul Mundtca5481c62012-07-10 12:08:14 +0900292};
293
Paul Mundtca5481c62012-07-10 12:08:14 +0900294/* pinmux ranges -> pinctrl pin descs */
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800295static int sh_pfc_map_gpios(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtca5481c62012-07-10 12:08:14 +0900296{
297 int i;
298
Laurent Pincharta373ed02012-11-29 13:24:07 +0100299 pmx->nr_pads = pfc->info->nr_pins;
Paul Mundtca5481c62012-07-10 12:08:14 +0900300
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100301 pmx->pads = devm_kzalloc(pfc->dev, sizeof(*pmx->pads) * pmx->nr_pads,
302 GFP_KERNEL);
Paul Mundtca5481c62012-07-10 12:08:14 +0900303 if (unlikely(!pmx->pads)) {
304 pmx->nr_pads = 0;
305 return -ENOMEM;
306 }
307
Paul Mundtca5481c62012-07-10 12:08:14 +0900308 for (i = 0; i < pmx->nr_pads; i++) {
309 struct pinctrl_pin_desc *pin = pmx->pads + i;
Laurent Pincharta3db40a2013-01-02 14:53:37 +0100310 struct sh_pfc_pin *gpio = pfc->info->pins + i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900311
Laurent Pinchartd7a7ca52012-11-28 17:51:00 +0100312 pin->number = i;
Paul Mundtca5481c62012-07-10 12:08:14 +0900313 pin->name = gpio->name;
314 }
315
Paul Mundtca5481c62012-07-10 12:08:14 +0900316 return 0;
317}
318
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800319static int sh_pfc_map_functions(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
Paul Mundtd93a8912012-07-11 17:17:10 +0900320{
Paul Mundtd93a8912012-07-11 17:17:10 +0900321 int i, fn;
322
Laurent Pincharta373ed02012-11-29 13:24:07 +0100323 for (i = 0; i < pfc->info->nr_func_gpios; i++) {
324 struct pinmux_func *func = pfc->info->func_gpios + i;
325
326 if (func->enum_id)
327 pmx->nr_functions++;
328 }
329
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100330 pmx->functions = devm_kzalloc(pfc->dev, pmx->nr_functions *
331 sizeof(*pmx->functions), GFP_KERNEL);
Paul Mundtd93a8912012-07-11 17:17:10 +0900332 if (unlikely(!pmx->functions))
333 return -ENOMEM;
334
Laurent Pincharta373ed02012-11-29 13:24:07 +0100335 for (i = fn = 0; i < pfc->info->nr_func_gpios; i++) {
336 struct pinmux_func *func = pfc->info->func_gpios + i;
Paul Mundtd93a8912012-07-11 17:17:10 +0900337
Laurent Pincharta373ed02012-11-29 13:24:07 +0100338 if (func->enum_id)
339 pmx->functions[fn++] = func;
Paul Mundtd93a8912012-07-11 17:17:10 +0900340 }
341
Paul Mundtd93a8912012-07-11 17:17:10 +0900342 return 0;
343}
344
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100345int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900346{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100347 struct sh_pfc_pinctrl *pmx;
Paul Mundtca5481c62012-07-10 12:08:14 +0900348 int ret;
349
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100350 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100351 if (unlikely(!pmx))
352 return -ENOMEM;
Paul Mundtca5481c62012-07-10 12:08:14 +0900353
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100354 pmx->pfc = pfc;
355 pfc->pinctrl = pmx;
356
357 ret = sh_pfc_map_gpios(pfc, pmx);
Paul Mundtca5481c62012-07-10 12:08:14 +0900358 if (unlikely(ret != 0))
359 return ret;
360
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100361 ret = sh_pfc_map_functions(pfc, pmx);
Paul Mundtd93a8912012-07-11 17:17:10 +0900362 if (unlikely(ret != 0))
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100363 return ret;
Paul Mundtd93a8912012-07-11 17:17:10 +0900364
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100365 pmx->pctl_desc.name = DRV_NAME;
366 pmx->pctl_desc.owner = THIS_MODULE;
367 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
368 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
369 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
370 pmx->pctl_desc.pins = pmx->pads;
371 pmx->pctl_desc.npins = pmx->nr_pads;
372
373 pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
Laurent Pinchart1724acf2012-12-15 23:50:48 +0100374 if (IS_ERR(pmx->pctl))
375 return PTR_ERR(pmx->pctl);
Paul Mundtca5481c62012-07-10 12:08:14 +0900376
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100377 pmx->range.name = DRV_NAME,
378 pmx->range.id = 0;
Laurent Pinchart2119f7c2012-11-29 13:03:53 +0100379 pmx->range.npins = pfc->info->nr_pins;
Laurent Pinchartd7a7ca52012-11-28 17:51:00 +0100380 pmx->range.base = 0;
381 pmx->range.pin_base = 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900382
Laurent Pinchartdcc427e2013-02-16 16:38:30 +0100383 pinctrl_add_gpio_range(pmx->pctl, &pmx->range);
Paul Mundtca5481c62012-07-10 12:08:14 +0900384
385 return 0;
Paul Mundtca5481c62012-07-10 12:08:14 +0900386}
387
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100388int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
Paul Mundtca5481c62012-07-10 12:08:14 +0900389{
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100390 struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
Paul Mundtca5481c62012-07-10 12:08:14 +0900391
Paul Mundtca5481c62012-07-10 12:08:14 +0900392 pinctrl_unregister(pmx->pctl);
393
Laurent Pinchartc6193ea2012-12-15 23:50:47 +0100394 pfc->pinctrl = NULL;
Paul Mundtca5481c62012-07-10 12:08:14 +0900395 return 0;
396}