blob: f8397a09491cf7bc785b80addebf38dd934b3d64 [file] [log] [blame]
Magnus Damm2967dab2008-10-08 20:41:43 +09001/*
2 * Pinmuxed GPIO support for SuperH.
3 *
4 * Copyright (C) 2008 Magnus Damm
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 */
10
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/list.h>
14#include <linux/module.h>
15#include <linux/clk.h>
16#include <linux/err.h>
17#include <linux/io.h>
18#include <linux/irq.h>
19#include <linux/bitops.h>
20#include <linux/gpio.h>
21
22static struct pinmux_info *registered_gpio;
23
24static struct pinmux_info *gpio_controller(unsigned gpio)
25{
26 if (!registered_gpio)
27 return NULL;
28
29 if (gpio < registered_gpio->first_gpio)
30 return NULL;
31
32 if (gpio > registered_gpio->last_gpio)
33 return NULL;
34
35 return registered_gpio;
36}
37
38static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
39{
40 if (enum_id < r->begin)
41 return 0;
42
43 if (enum_id > r->end)
44 return 0;
45
46 return 1;
47}
48
Magnus Damm0fc64cc2008-12-25 18:17:18 +090049static int gpio_read_reg(unsigned long reg, unsigned long reg_width,
50 unsigned long field_width, unsigned long in_pos)
Magnus Damm2967dab2008-10-08 20:41:43 +090051{
52 unsigned long data, mask, pos;
53
54 data = 0;
55 mask = (1 << field_width) - 1;
56 pos = reg_width - ((in_pos + 1) * field_width);
57
58#ifdef DEBUG
Magnus Damm0fc64cc2008-12-25 18:17:18 +090059 pr_info("read_reg: addr = %lx, pos = %ld, "
Magnus Damm2967dab2008-10-08 20:41:43 +090060 "r_width = %ld, f_width = %ld\n",
Magnus Damm0fc64cc2008-12-25 18:17:18 +090061 reg, pos, reg_width, field_width);
Magnus Damm2967dab2008-10-08 20:41:43 +090062#endif
63
64 switch (reg_width) {
65 case 8:
66 data = ctrl_inb(reg);
67 break;
68 case 16:
69 data = ctrl_inw(reg);
70 break;
71 case 32:
72 data = ctrl_inl(reg);
73 break;
74 }
75
Magnus Damm0fc64cc2008-12-25 18:17:18 +090076 return (data >> pos) & mask;
77}
Magnus Damm2967dab2008-10-08 20:41:43 +090078
Magnus Damm0fc64cc2008-12-25 18:17:18 +090079static void gpio_write_reg(unsigned long reg, unsigned long reg_width,
80 unsigned long field_width, unsigned long in_pos,
81 unsigned long value)
82{
83 unsigned long mask, pos;
84
85 mask = (1 << field_width) - 1;
86 pos = reg_width - ((in_pos + 1) * field_width);
87
88#ifdef DEBUG
89 pr_info("write_reg addr = %lx, value = %ld, pos = %ld, "
90 "r_width = %ld, f_width = %ld\n",
91 reg, value, pos, reg_width, field_width);
92#endif
93
94 mask = ~(mask << pos);
95 value = value << pos;
Magnus Damm2967dab2008-10-08 20:41:43 +090096
97 switch (reg_width) {
98 case 8:
Magnus Damm0fc64cc2008-12-25 18:17:18 +090099 ctrl_outb((ctrl_inb(reg) & mask) | value, reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900100 break;
101 case 16:
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900102 ctrl_outw((ctrl_inw(reg) & mask) | value, reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900103 break;
104 case 32:
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900105 ctrl_outl((ctrl_inl(reg) & mask) | value, reg);
Magnus Damm2967dab2008-10-08 20:41:43 +0900106 break;
107 }
Magnus Damm2967dab2008-10-08 20:41:43 +0900108}
109
Magnus Damm18801be2008-12-25 18:17:09 +0900110static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900111{
Magnus Damm18801be2008-12-25 18:17:09 +0900112 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
Magnus Damm2967dab2008-10-08 20:41:43 +0900113 struct pinmux_data_reg *data_reg;
114 int k, n;
115
Magnus Damm18801be2008-12-25 18:17:09 +0900116 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
Magnus Damm2967dab2008-10-08 20:41:43 +0900117 return -1;
118
119 k = 0;
120 while (1) {
121 data_reg = gpioc->data_regs + k;
122
123 if (!data_reg->reg_width)
124 break;
125
126 for (n = 0; n < data_reg->reg_width; n++) {
Magnus Damm18801be2008-12-25 18:17:09 +0900127 if (data_reg->enum_ids[n] == gpiop->enum_id) {
128 gpiop->flags &= ~PINMUX_FLAG_DREG;
129 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
130 gpiop->flags &= ~PINMUX_FLAG_DBIT;
131 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
Magnus Damm2967dab2008-10-08 20:41:43 +0900132 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900133 }
134 }
135 k++;
136 }
137
Magnus Damm18801be2008-12-25 18:17:09 +0900138 BUG();
139
Magnus Damm2967dab2008-10-08 20:41:43 +0900140 return -1;
141}
142
Magnus Damm18801be2008-12-25 18:17:09 +0900143static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
144 struct pinmux_data_reg **drp, int *bitp)
145{
146 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
147 int k, n;
148
149 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
150 return -1;
151
152 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
153 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
154 *drp = gpioc->data_regs + k;
155 *bitp = n;
156 return 0;
157}
158
Magnus Damm2967dab2008-10-08 20:41:43 +0900159static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
160 struct pinmux_cfg_reg **crp, int *indexp,
161 unsigned long **cntp)
162{
163 struct pinmux_cfg_reg *config_reg;
164 unsigned long r_width, f_width;
165 int k, n;
166
167 k = 0;
168 while (1) {
169 config_reg = gpioc->cfg_regs + k;
170
171 r_width = config_reg->reg_width;
172 f_width = config_reg->field_width;
173
174 if (!r_width)
175 break;
176 for (n = 0; n < (r_width / f_width) * 1 << f_width; n++) {
177 if (config_reg->enum_ids[n] == enum_id) {
178 *crp = config_reg;
179 *indexp = n;
180 *cntp = &config_reg->cnt[n / (1 << f_width)];
181 return 0;
182 }
183 }
184 k++;
185 }
186
187 return -1;
188}
189
190static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
191 int pos, pinmux_enum_t *enum_idp)
192{
193 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
194 pinmux_enum_t *data = gpioc->gpio_data;
195 int k;
196
197 if (!enum_in_range(enum_id, &gpioc->data)) {
198 if (!enum_in_range(enum_id, &gpioc->mark)) {
199 pr_err("non data/mark enum_id for gpio %d\n", gpio);
200 return -1;
201 }
202 }
203
204 if (pos) {
205 *enum_idp = data[pos + 1];
206 return pos + 1;
207 }
208
209 for (k = 0; k < gpioc->gpio_data_size; k++) {
210 if (data[k] == enum_id) {
211 *enum_idp = data[k + 1];
212 return k + 1;
213 }
214 }
215
216 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
217 return -1;
218}
219
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900220static void write_config_reg(struct pinmux_info *gpioc,
221 struct pinmux_cfg_reg *crp,
222 int index)
Magnus Damm2967dab2008-10-08 20:41:43 +0900223{
224 unsigned long ncomb, pos, value;
225
226 ncomb = 1 << crp->field_width;
227 pos = index / ncomb;
228 value = index % ncomb;
229
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900230 gpio_write_reg(crp->reg, crp->reg_width, crp->field_width, pos, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900231}
232
233static int check_config_reg(struct pinmux_info *gpioc,
234 struct pinmux_cfg_reg *crp,
235 int index)
236{
237 unsigned long ncomb, pos, value;
238
239 ncomb = 1 << crp->field_width;
240 pos = index / ncomb;
241 value = index % ncomb;
242
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900243 if (gpio_read_reg(crp->reg, crp->reg_width,
244 crp->field_width, pos) == value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900245 return 0;
246
247 return -1;
248}
249
250enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
251
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900252static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
253 int pinmux_type, int cfg_mode)
Magnus Damm2967dab2008-10-08 20:41:43 +0900254{
255 struct pinmux_cfg_reg *cr = NULL;
256 pinmux_enum_t enum_id;
257 struct pinmux_range *range;
258 int in_range, pos, index;
259 unsigned long *cntp;
260
261 switch (pinmux_type) {
262
263 case PINMUX_TYPE_FUNCTION:
264 range = NULL;
265 break;
266
267 case PINMUX_TYPE_OUTPUT:
268 range = &gpioc->output;
269 break;
270
271 case PINMUX_TYPE_INPUT:
272 range = &gpioc->input;
273 break;
274
275 case PINMUX_TYPE_INPUT_PULLUP:
276 range = &gpioc->input_pu;
277 break;
278
279 case PINMUX_TYPE_INPUT_PULLDOWN:
280 range = &gpioc->input_pd;
281 break;
282
283 default:
284 goto out_err;
285 }
286
287 pos = 0;
288 enum_id = 0;
289 index = 0;
290 while (1) {
291 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
292 if (pos <= 0)
293 goto out_err;
294
295 if (!enum_id)
296 break;
297
298 in_range = enum_in_range(enum_id, &gpioc->function);
Magnus Damm42eed422008-10-22 18:29:17 +0900299 if (!in_range && range) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900300 in_range = enum_in_range(enum_id, range);
301
Magnus Damm42eed422008-10-22 18:29:17 +0900302 if (in_range && enum_id == range->force)
303 continue;
304 }
305
Magnus Damm2967dab2008-10-08 20:41:43 +0900306 if (!in_range)
307 continue;
308
309 if (get_config_reg(gpioc, enum_id, &cr, &index, &cntp) != 0)
310 goto out_err;
311
312 switch (cfg_mode) {
313 case GPIO_CFG_DRYRUN:
314 if (!*cntp || !check_config_reg(gpioc, cr, index))
315 continue;
316 break;
317
318 case GPIO_CFG_REQ:
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900319 write_config_reg(gpioc, cr, index);
Magnus Damm2967dab2008-10-08 20:41:43 +0900320 *cntp = *cntp + 1;
321 break;
322
323 case GPIO_CFG_FREE:
324 *cntp = *cntp - 1;
325 break;
326 }
327 }
328
329 return 0;
330 out_err:
331 return -1;
332}
333
334static DEFINE_SPINLOCK(gpio_lock);
335
336int __gpio_request(unsigned gpio)
337{
338 struct pinmux_info *gpioc = gpio_controller(gpio);
339 struct pinmux_data_reg *dummy;
340 unsigned long flags;
341 int i, ret, pinmux_type;
342
343 ret = -EINVAL;
344
345 if (!gpioc)
346 goto err_out;
347
348 spin_lock_irqsave(&gpio_lock, flags);
349
350 if ((gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
351 goto err_unlock;
352
353 /* setup pin function here if no data is associated with pin */
354
355 if (get_data_reg(gpioc, gpio, &dummy, &i) != 0)
356 pinmux_type = PINMUX_TYPE_FUNCTION;
357 else
358 pinmux_type = PINMUX_TYPE_GPIO;
359
360 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
361 if (pinmux_config_gpio(gpioc, gpio,
362 pinmux_type,
363 GPIO_CFG_DRYRUN) != 0)
364 goto err_unlock;
365
366 if (pinmux_config_gpio(gpioc, gpio,
367 pinmux_type,
368 GPIO_CFG_REQ) != 0)
369 BUG();
370 }
371
Magnus Damm18801be2008-12-25 18:17:09 +0900372 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
373 gpioc->gpios[gpio].flags |= pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900374
375 ret = 0;
376 err_unlock:
377 spin_unlock_irqrestore(&gpio_lock, flags);
378 err_out:
379 return ret;
380}
381EXPORT_SYMBOL(__gpio_request);
382
383void gpio_free(unsigned gpio)
384{
385 struct pinmux_info *gpioc = gpio_controller(gpio);
386 unsigned long flags;
387 int pinmux_type;
388
389 if (!gpioc)
390 return;
391
392 spin_lock_irqsave(&gpio_lock, flags);
393
394 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
395 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
Magnus Damm18801be2008-12-25 18:17:09 +0900396 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
397 gpioc->gpios[gpio].flags |= PINMUX_TYPE_NONE;
Magnus Damm2967dab2008-10-08 20:41:43 +0900398
399 spin_unlock_irqrestore(&gpio_lock, flags);
400}
401EXPORT_SYMBOL(gpio_free);
402
403static int pinmux_direction(struct pinmux_info *gpioc,
404 unsigned gpio, int new_pinmux_type)
405{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900406 int pinmux_type;
407 int ret = -EINVAL;
Magnus Damm2967dab2008-10-08 20:41:43 +0900408
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900409 if (!gpioc)
410 goto err_out;
411
Magnus Damm2967dab2008-10-08 20:41:43 +0900412 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
413
414 switch (pinmux_type) {
415 case PINMUX_TYPE_GPIO:
416 break;
417 case PINMUX_TYPE_OUTPUT:
418 case PINMUX_TYPE_INPUT:
419 case PINMUX_TYPE_INPUT_PULLUP:
420 case PINMUX_TYPE_INPUT_PULLDOWN:
421 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
422 break;
423 default:
424 goto err_out;
425 }
426
427 if (pinmux_config_gpio(gpioc, gpio,
428 new_pinmux_type,
429 GPIO_CFG_DRYRUN) != 0)
430 goto err_out;
431
432 if (pinmux_config_gpio(gpioc, gpio,
433 new_pinmux_type,
434 GPIO_CFG_REQ) != 0)
435 BUG();
436
Magnus Damm18801be2008-12-25 18:17:09 +0900437 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
438 gpioc->gpios[gpio].flags |= new_pinmux_type;
Magnus Damm2967dab2008-10-08 20:41:43 +0900439
440 ret = 0;
441 err_out:
442 return ret;
443}
444
445int gpio_direction_input(unsigned gpio)
446{
447 struct pinmux_info *gpioc = gpio_controller(gpio);
448 unsigned long flags;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900449 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900450
451 spin_lock_irqsave(&gpio_lock, flags);
452 ret = pinmux_direction(gpioc, gpio, PINMUX_TYPE_INPUT);
453 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900454
Magnus Damm2967dab2008-10-08 20:41:43 +0900455 return ret;
456}
457EXPORT_SYMBOL(gpio_direction_input);
458
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900459static void __gpio_set_value(struct pinmux_info *gpioc,
460 unsigned gpio, int value)
Magnus Damm2967dab2008-10-08 20:41:43 +0900461{
462 struct pinmux_data_reg *dr = NULL;
463 int bit = 0;
464
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900465 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
Magnus Damm2967dab2008-10-08 20:41:43 +0900466 BUG();
467 else
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900468 gpio_write_reg(dr->reg, dr->reg_width, 1, bit, !!value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900469}
470
471int gpio_direction_output(unsigned gpio, int value)
472{
473 struct pinmux_info *gpioc = gpio_controller(gpio);
474 unsigned long flags;
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900475 int ret;
Magnus Damm2967dab2008-10-08 20:41:43 +0900476
477 spin_lock_irqsave(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900478 __gpio_set_value(gpioc, gpio, value);
Magnus Damm2967dab2008-10-08 20:41:43 +0900479 ret = pinmux_direction(gpioc, gpio, PINMUX_TYPE_OUTPUT);
480 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900481
Magnus Damm2967dab2008-10-08 20:41:43 +0900482 return ret;
483}
484EXPORT_SYMBOL(gpio_direction_output);
485
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900486static int __gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
Magnus Damm2967dab2008-10-08 20:41:43 +0900487{
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900488 struct pinmux_data_reg *dr = NULL;
489 int bit = 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900490
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900491 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0) {
Magnus Damm2967dab2008-10-08 20:41:43 +0900492 BUG();
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900493 return 0;
Magnus Damm2967dab2008-10-08 20:41:43 +0900494 }
495
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900496 return gpio_read_reg(dr->reg, dr->reg_width, 1, bit);
497}
498
499int gpio_get_value(unsigned gpio)
500{
501 return __gpio_get_value(gpio_controller(gpio), gpio);
Magnus Damm2967dab2008-10-08 20:41:43 +0900502}
503EXPORT_SYMBOL(gpio_get_value);
504
505void gpio_set_value(unsigned gpio, int value)
506{
507 struct pinmux_info *gpioc = gpio_controller(gpio);
508 unsigned long flags;
509
Magnus Damm0fc64cc2008-12-25 18:17:18 +0900510 spin_lock_irqsave(&gpio_lock, flags);
511 __gpio_set_value(gpioc, gpio, value);
512 spin_unlock_irqrestore(&gpio_lock, flags);
Magnus Damm2967dab2008-10-08 20:41:43 +0900513}
514EXPORT_SYMBOL(gpio_set_value);
515
516int register_pinmux(struct pinmux_info *pip)
517{
Magnus Damm18801be2008-12-25 18:17:09 +0900518 int k;
519
Magnus Damm2967dab2008-10-08 20:41:43 +0900520 registered_gpio = pip;
521 pr_info("pinmux: %s handling gpio %d -> %d\n",
522 pip->name, pip->first_gpio, pip->last_gpio);
523
Magnus Damm18801be2008-12-25 18:17:09 +0900524 for (k = pip->first_gpio; k <= pip->last_gpio; k++)
525 setup_data_reg(pip, k);
526
Magnus Damm2967dab2008-10-08 20:41:43 +0900527 return 0;
528}