Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awilliam
GitHub Repository: awilliam/linux-vfio
Path: blob/master/arch/cris/arch-v32/drivers/mach-a3/gpio.c
15133 views
1
/*
2
* Artec-3 general port I/O device
3
*
4
* Copyright (c) 2007 Axis Communications AB
5
*
6
* Authors: Bjorn Wesen (initial version)
7
* Ola Knutsson (LED handling)
8
* Johan Adolfsson (read/set directions, write, port G,
9
* port to ETRAX FS.
10
* Ricard Wanderlof (PWM for Artpec-3)
11
*
12
*/
13
14
#include <linux/module.h>
15
#include <linux/sched.h>
16
#include <linux/slab.h>
17
#include <linux/ioport.h>
18
#include <linux/errno.h>
19
#include <linux/kernel.h>
20
#include <linux/fs.h>
21
#include <linux/string.h>
22
#include <linux/poll.h>
23
#include <linux/init.h>
24
#include <linux/interrupt.h>
25
#include <linux/spinlock.h>
26
#include <linux/mutex.h>
27
28
#include <asm/etraxgpio.h>
29
#include <hwregs/reg_map.h>
30
#include <hwregs/reg_rdwr.h>
31
#include <hwregs/gio_defs.h>
32
#include <hwregs/intr_vect_defs.h>
33
#include <asm/io.h>
34
#include <asm/system.h>
35
#include <asm/irq.h>
36
#include <mach/pinmux.h>
37
38
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
39
#include "../i2c.h"
40
41
#define VIRT_I2C_ADDR 0x40
42
#endif
43
44
/* The following gio ports on ARTPEC-3 is available:
45
* pa 32 bits
46
* pb 32 bits
47
* pc 16 bits
48
* each port has a rw_px_dout, r_px_din and rw_px_oe register.
49
*/
50
51
#define GPIO_MAJOR 120 /* experimental MAJOR number */
52
53
#define I2C_INTERRUPT_BITS 0x300 /* i2c0_done and i2c1_done bits */
54
55
#define D(x)
56
57
#if 0
58
static int dp_cnt;
59
#define DP(x) \
60
do { \
61
dp_cnt++; \
62
if (dp_cnt % 1000 == 0) \
63
x; \
64
} while (0)
65
#else
66
#define DP(x)
67
#endif
68
69
static DEFINE_MUTEX(gpio_mutex);
70
static char gpio_name[] = "etrax gpio";
71
72
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
73
static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
74
unsigned long arg);
75
#endif
76
static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
77
static ssize_t gpio_write(struct file *file, const char __user *buf,
78
size_t count, loff_t *off);
79
static int gpio_open(struct inode *inode, struct file *filp);
80
static int gpio_release(struct inode *inode, struct file *filp);
81
static unsigned int gpio_poll(struct file *filp,
82
struct poll_table_struct *wait);
83
84
/* private data per open() of this driver */
85
86
struct gpio_private {
87
struct gpio_private *next;
88
/* The IO_CFG_WRITE_MODE_VALUE only support 8 bits: */
89
unsigned char clk_mask;
90
unsigned char data_mask;
91
unsigned char write_msb;
92
unsigned char pad1;
93
/* These fields are generic */
94
unsigned long highalarm, lowalarm;
95
wait_queue_head_t alarm_wq;
96
int minor;
97
};
98
99
static void gpio_set_alarm(struct gpio_private *priv);
100
static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
101
static int gpio_pwm_ioctl(struct gpio_private *priv, unsigned int cmd,
102
unsigned long arg);
103
104
105
/* linked list of alarms to check for */
106
107
static struct gpio_private *alarmlist;
108
109
static int wanted_interrupts;
110
111
static DEFINE_SPINLOCK(gpio_lock);
112
113
#define NUM_PORTS (GPIO_MINOR_LAST+1)
114
#define GIO_REG_RD_ADDR(reg) \
115
(unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
116
#define GIO_REG_WR_ADDR(reg) \
117
(unsigned long *)(regi_gio + REG_WR_ADDR_gio_##reg)
118
static unsigned long led_dummy;
119
static unsigned long port_d_dummy; /* Only input on Artpec-3 */
120
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
121
static unsigned long port_e_dummy; /* Non existent on Artpec-3 */
122
static unsigned long virtual_dummy;
123
static unsigned long virtual_rw_pv_oe = CONFIG_ETRAX_DEF_GIO_PV_OE;
124
static unsigned short cached_virtual_gpio_read;
125
#endif
126
127
static unsigned long *data_out[NUM_PORTS] = {
128
GIO_REG_WR_ADDR(rw_pa_dout),
129
GIO_REG_WR_ADDR(rw_pb_dout),
130
&led_dummy,
131
GIO_REG_WR_ADDR(rw_pc_dout),
132
&port_d_dummy,
133
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
134
&port_e_dummy,
135
&virtual_dummy,
136
#endif
137
};
138
139
static unsigned long *data_in[NUM_PORTS] = {
140
GIO_REG_RD_ADDR(r_pa_din),
141
GIO_REG_RD_ADDR(r_pb_din),
142
&led_dummy,
143
GIO_REG_RD_ADDR(r_pc_din),
144
GIO_REG_RD_ADDR(r_pd_din),
145
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
146
&port_e_dummy,
147
&virtual_dummy,
148
#endif
149
};
150
151
static unsigned long changeable_dir[NUM_PORTS] = {
152
CONFIG_ETRAX_PA_CHANGEABLE_DIR,
153
CONFIG_ETRAX_PB_CHANGEABLE_DIR,
154
0,
155
CONFIG_ETRAX_PC_CHANGEABLE_DIR,
156
0,
157
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
158
0,
159
CONFIG_ETRAX_PV_CHANGEABLE_DIR,
160
#endif
161
};
162
163
static unsigned long changeable_bits[NUM_PORTS] = {
164
CONFIG_ETRAX_PA_CHANGEABLE_BITS,
165
CONFIG_ETRAX_PB_CHANGEABLE_BITS,
166
0,
167
CONFIG_ETRAX_PC_CHANGEABLE_BITS,
168
0,
169
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
170
0,
171
CONFIG_ETRAX_PV_CHANGEABLE_BITS,
172
#endif
173
};
174
175
static unsigned long *dir_oe[NUM_PORTS] = {
176
GIO_REG_WR_ADDR(rw_pa_oe),
177
GIO_REG_WR_ADDR(rw_pb_oe),
178
&led_dummy,
179
GIO_REG_WR_ADDR(rw_pc_oe),
180
&port_d_dummy,
181
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
182
&port_e_dummy,
183
&virtual_rw_pv_oe,
184
#endif
185
};
186
187
static void gpio_set_alarm(struct gpio_private *priv)
188
{
189
int bit;
190
int intr_cfg;
191
int mask;
192
int pins;
193
unsigned long flags;
194
195
spin_lock_irqsave(&gpio_lock, flags);
196
intr_cfg = REG_RD_INT(gio, regi_gio, rw_intr_cfg);
197
pins = REG_RD_INT(gio, regi_gio, rw_intr_pins);
198
mask = REG_RD_INT(gio, regi_gio, rw_intr_mask) & I2C_INTERRUPT_BITS;
199
200
for (bit = 0; bit < 32; bit++) {
201
int intr = bit % 8;
202
int pin = bit / 8;
203
if (priv->minor < GPIO_MINOR_LEDS)
204
pin += priv->minor * 4;
205
else
206
pin += (priv->minor - 1) * 4;
207
208
if (priv->highalarm & (1<<bit)) {
209
intr_cfg |= (regk_gio_hi << (intr * 3));
210
mask |= 1 << intr;
211
wanted_interrupts = mask & 0xff;
212
pins |= pin << (intr * 4);
213
} else if (priv->lowalarm & (1<<bit)) {
214
intr_cfg |= (regk_gio_lo << (intr * 3));
215
mask |= 1 << intr;
216
wanted_interrupts = mask & 0xff;
217
pins |= pin << (intr * 4);
218
}
219
}
220
221
REG_WR_INT(gio, regi_gio, rw_intr_cfg, intr_cfg);
222
REG_WR_INT(gio, regi_gio, rw_intr_pins, pins);
223
REG_WR_INT(gio, regi_gio, rw_intr_mask, mask);
224
225
spin_unlock_irqrestore(&gpio_lock, flags);
226
}
227
228
static unsigned int gpio_poll(struct file *file, struct poll_table_struct *wait)
229
{
230
unsigned int mask = 0;
231
struct gpio_private *priv = file->private_data;
232
unsigned long data;
233
unsigned long tmp;
234
235
if (priv->minor >= GPIO_MINOR_PWM0 &&
236
priv->minor <= GPIO_MINOR_LAST_PWM)
237
return 0;
238
239
poll_wait(file, &priv->alarm_wq, wait);
240
if (priv->minor <= GPIO_MINOR_D) {
241
data = readl(data_in[priv->minor]);
242
REG_WR_INT(gio, regi_gio, rw_ack_intr, wanted_interrupts);
243
tmp = REG_RD_INT(gio, regi_gio, rw_intr_mask);
244
tmp &= I2C_INTERRUPT_BITS;
245
tmp |= wanted_interrupts;
246
REG_WR_INT(gio, regi_gio, rw_intr_mask, tmp);
247
} else
248
return 0;
249
250
if ((data & priv->highalarm) || (~data & priv->lowalarm))
251
mask = POLLIN|POLLRDNORM;
252
253
DP(printk(KERN_DEBUG "gpio_poll ready: mask 0x%08X\n", mask));
254
return mask;
255
}
256
257
static irqreturn_t gpio_interrupt(int irq, void *dev_id)
258
{
259
reg_gio_rw_intr_mask intr_mask;
260
reg_gio_r_masked_intr masked_intr;
261
reg_gio_rw_ack_intr ack_intr;
262
unsigned long flags;
263
unsigned long tmp;
264
unsigned long tmp2;
265
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
266
unsigned char enable_gpiov_ack = 0;
267
#endif
268
269
/* Find what PA interrupts are active */
270
masked_intr = REG_RD(gio, regi_gio, r_masked_intr);
271
tmp = REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr, masked_intr);
272
273
/* Find those that we have enabled */
274
spin_lock_irqsave(&gpio_lock, flags);
275
tmp &= wanted_interrupts;
276
spin_unlock_irqrestore(&gpio_lock, flags);
277
278
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
279
/* Something changed on virtual GPIO. Interrupt is acked by
280
* reading the device.
281
*/
282
if (tmp & (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN)) {
283
i2c_read(VIRT_I2C_ADDR, (void *)&cached_virtual_gpio_read,
284
sizeof(cached_virtual_gpio_read));
285
enable_gpiov_ack = 1;
286
}
287
#endif
288
289
/* Ack them */
290
ack_intr = REG_TYPE_CONV(reg_gio_rw_ack_intr, unsigned long, tmp);
291
REG_WR(gio, regi_gio, rw_ack_intr, ack_intr);
292
293
/* Disable those interrupts.. */
294
intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
295
tmp2 = REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask, intr_mask);
296
tmp2 &= ~tmp;
297
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
298
/* Do not disable interrupt on virtual GPIO. Changes on virtual
299
* pins are only noticed by an interrupt.
300
*/
301
if (enable_gpiov_ack)
302
tmp2 |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
303
#endif
304
intr_mask = REG_TYPE_CONV(reg_gio_rw_intr_mask, unsigned long, tmp2);
305
REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
306
307
return IRQ_RETVAL(tmp);
308
}
309
310
static void gpio_write_bit(unsigned long *port, unsigned char data, int bit,
311
unsigned char clk_mask, unsigned char data_mask)
312
{
313
unsigned long shadow = readl(port) & ~clk_mask;
314
writel(shadow, port);
315
if (data & 1 << bit)
316
shadow |= data_mask;
317
else
318
shadow &= ~data_mask;
319
writel(shadow, port);
320
/* For FPGA: min 5.0ns (DCC) before CCLK high */
321
shadow |= clk_mask;
322
writel(shadow, port);
323
}
324
325
static void gpio_write_byte(struct gpio_private *priv, unsigned long *port,
326
unsigned char data)
327
{
328
int i;
329
330
if (priv->write_msb)
331
for (i = 7; i >= 0; i--)
332
gpio_write_bit(port, data, i, priv->clk_mask,
333
priv->data_mask);
334
else
335
for (i = 0; i <= 7; i++)
336
gpio_write_bit(port, data, i, priv->clk_mask,
337
priv->data_mask);
338
}
339
340
341
static ssize_t gpio_write(struct file *file, const char __user *buf,
342
size_t count, loff_t *off)
343
{
344
struct gpio_private *priv = file->private_data;
345
unsigned long flags;
346
ssize_t retval = count;
347
/* Only bits 0-7 may be used for write operations but allow all
348
devices except leds... */
349
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
350
if (priv->minor == GPIO_MINOR_V)
351
return -EFAULT;
352
#endif
353
if (priv->minor == GPIO_MINOR_LEDS)
354
return -EFAULT;
355
356
if (priv->minor >= GPIO_MINOR_PWM0 &&
357
priv->minor <= GPIO_MINOR_LAST_PWM)
358
return -EFAULT;
359
360
if (!access_ok(VERIFY_READ, buf, count))
361
return -EFAULT;
362
363
/* It must have been configured using the IO_CFG_WRITE_MODE */
364
/* Perhaps a better error code? */
365
if (priv->clk_mask == 0 || priv->data_mask == 0)
366
return -EPERM;
367
368
D(printk(KERN_DEBUG "gpio_write: %lu to data 0x%02X clk 0x%02X "
369
"msb: %i\n",
370
count, priv->data_mask, priv->clk_mask, priv->write_msb));
371
372
spin_lock_irqsave(&gpio_lock, flags);
373
374
while (count--)
375
gpio_write_byte(priv, data_out[priv->minor], *buf++);
376
377
spin_unlock_irqrestore(&gpio_lock, flags);
378
return retval;
379
}
380
381
static int gpio_open(struct inode *inode, struct file *filp)
382
{
383
struct gpio_private *priv;
384
int p = iminor(inode);
385
386
if (p > GPIO_MINOR_LAST_PWM ||
387
(p > GPIO_MINOR_LAST && p < GPIO_MINOR_PWM0))
388
return -EINVAL;
389
390
priv = kmalloc(sizeof(struct gpio_private), GFP_KERNEL);
391
392
if (!priv)
393
return -ENOMEM;
394
395
mutex_lock(&gpio_mutex);
396
memset(priv, 0, sizeof(*priv));
397
398
priv->minor = p;
399
filp->private_data = priv;
400
401
/* initialize the io/alarm struct, not for PWM ports though */
402
if (p <= GPIO_MINOR_LAST) {
403
404
priv->clk_mask = 0;
405
priv->data_mask = 0;
406
priv->highalarm = 0;
407
priv->lowalarm = 0;
408
409
init_waitqueue_head(&priv->alarm_wq);
410
411
/* link it into our alarmlist */
412
spin_lock_irq(&gpio_lock);
413
priv->next = alarmlist;
414
alarmlist = priv;
415
spin_unlock_irq(&gpio_lock);
416
}
417
418
mutex_unlock(&gpio_mutex);
419
return 0;
420
}
421
422
static int gpio_release(struct inode *inode, struct file *filp)
423
{
424
struct gpio_private *p;
425
struct gpio_private *todel;
426
/* local copies while updating them: */
427
unsigned long a_high, a_low;
428
429
/* prepare to free private structure */
430
todel = filp->private_data;
431
432
/* unlink from alarmlist - only for non-PWM ports though */
433
if (todel->minor <= GPIO_MINOR_LAST) {
434
spin_lock_irq(&gpio_lock);
435
p = alarmlist;
436
437
if (p == todel)
438
alarmlist = todel->next;
439
else {
440
while (p->next != todel)
441
p = p->next;
442
p->next = todel->next;
443
}
444
445
/* Check if there are still any alarms set */
446
p = alarmlist;
447
a_high = 0;
448
a_low = 0;
449
while (p) {
450
if (p->minor == GPIO_MINOR_A) {
451
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
452
p->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
453
#endif
454
a_high |= p->highalarm;
455
a_low |= p->lowalarm;
456
}
457
458
p = p->next;
459
}
460
461
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
462
/* Variable 'a_low' needs to be set here again
463
* to ensure that interrupt for virtual GPIO is handled.
464
*/
465
a_low |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
466
#endif
467
468
spin_unlock_irq(&gpio_lock);
469
}
470
kfree(todel);
471
472
return 0;
473
}
474
475
/* Main device API. ioctl's to read/set/clear bits, as well as to
476
* set alarms to wait for using a subsequent select().
477
*/
478
479
inline unsigned long setget_input(struct gpio_private *priv, unsigned long arg)
480
{
481
/* Set direction 0=unchanged 1=input,
482
* return mask with 1=input
483
*/
484
unsigned long flags;
485
unsigned long dir_shadow;
486
487
spin_lock_irqsave(&gpio_lock, flags);
488
489
dir_shadow = readl(dir_oe[priv->minor]) &
490
~(arg & changeable_dir[priv->minor]);
491
writel(dir_shadow, dir_oe[priv->minor]);
492
493
spin_unlock_irqrestore(&gpio_lock, flags);
494
495
if (priv->minor == GPIO_MINOR_C)
496
dir_shadow ^= 0xFFFF; /* Only 16 bits */
497
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
498
else if (priv->minor == GPIO_MINOR_V)
499
dir_shadow ^= 0xFFFF; /* Only 16 bits */
500
#endif
501
else
502
dir_shadow ^= 0xFFFFFFFF; /* PA, PB and PD 32 bits */
503
504
return dir_shadow;
505
506
} /* setget_input */
507
508
static inline unsigned long setget_output(struct gpio_private *priv,
509
unsigned long arg)
510
{
511
unsigned long flags;
512
unsigned long dir_shadow;
513
514
spin_lock_irqsave(&gpio_lock, flags);
515
516
dir_shadow = readl(dir_oe[priv->minor]) |
517
(arg & changeable_dir[priv->minor]);
518
writel(dir_shadow, dir_oe[priv->minor]);
519
520
spin_unlock_irqrestore(&gpio_lock, flags);
521
return dir_shadow;
522
} /* setget_output */
523
524
static long gpio_ioctl_unlocked(struct file *file,
525
unsigned int cmd, unsigned long arg)
526
{
527
unsigned long flags;
528
unsigned long val;
529
unsigned long shadow;
530
struct gpio_private *priv = file->private_data;
531
532
if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
533
return -ENOTTY;
534
535
/* Check for special ioctl handlers first */
536
537
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
538
if (priv->minor == GPIO_MINOR_V)
539
return virtual_gpio_ioctl(file, cmd, arg);
540
#endif
541
542
if (priv->minor == GPIO_MINOR_LEDS)
543
return gpio_leds_ioctl(cmd, arg);
544
545
if (priv->minor >= GPIO_MINOR_PWM0 &&
546
priv->minor <= GPIO_MINOR_LAST_PWM)
547
return gpio_pwm_ioctl(priv, cmd, arg);
548
549
switch (_IOC_NR(cmd)) {
550
case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
551
/* Read the port. */
552
return readl(data_in[priv->minor]);
553
case IO_SETBITS:
554
spin_lock_irqsave(&gpio_lock, flags);
555
/* Set changeable bits with a 1 in arg. */
556
shadow = readl(data_out[priv->minor]) |
557
(arg & changeable_bits[priv->minor]);
558
writel(shadow, data_out[priv->minor]);
559
spin_unlock_irqrestore(&gpio_lock, flags);
560
break;
561
case IO_CLRBITS:
562
spin_lock_irqsave(&gpio_lock, flags);
563
/* Clear changeable bits with a 1 in arg. */
564
shadow = readl(data_out[priv->minor]) &
565
~(arg & changeable_bits[priv->minor]);
566
writel(shadow, data_out[priv->minor]);
567
spin_unlock_irqrestore(&gpio_lock, flags);
568
break;
569
case IO_HIGHALARM:
570
/* Set alarm when bits with 1 in arg go high. */
571
priv->highalarm |= arg;
572
gpio_set_alarm(priv);
573
break;
574
case IO_LOWALARM:
575
/* Set alarm when bits with 1 in arg go low. */
576
priv->lowalarm |= arg;
577
gpio_set_alarm(priv);
578
break;
579
case IO_CLRALARM:
580
/* Clear alarm for bits with 1 in arg. */
581
priv->highalarm &= ~arg;
582
priv->lowalarm &= ~arg;
583
gpio_set_alarm(priv);
584
break;
585
case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
586
/* Read direction 0=input 1=output */
587
return readl(dir_oe[priv->minor]);
588
589
case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
590
/* Set direction 0=unchanged 1=input,
591
* return mask with 1=input
592
*/
593
return setget_input(priv, arg);
594
595
case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
596
/* Set direction 0=unchanged 1=output,
597
* return mask with 1=output
598
*/
599
return setget_output(priv, arg);
600
601
case IO_CFG_WRITE_MODE:
602
{
603
int res = -EPERM;
604
unsigned long dir_shadow, clk_mask, data_mask, write_msb;
605
606
clk_mask = arg & 0xFF;
607
data_mask = (arg >> 8) & 0xFF;
608
write_msb = (arg >> 16) & 0x01;
609
610
/* Check if we're allowed to change the bits and
611
* the direction is correct
612
*/
613
spin_lock_irqsave(&gpio_lock, flags);
614
dir_shadow = readl(dir_oe[priv->minor]);
615
if ((clk_mask & changeable_bits[priv->minor]) &&
616
(data_mask & changeable_bits[priv->minor]) &&
617
(clk_mask & dir_shadow) &&
618
(data_mask & dir_shadow)) {
619
priv->clk_mask = clk_mask;
620
priv->data_mask = data_mask;
621
priv->write_msb = write_msb;
622
res = 0;
623
}
624
spin_unlock_irqrestore(&gpio_lock, flags);
625
626
return res;
627
}
628
case IO_READ_INBITS:
629
/* *arg is result of reading the input pins */
630
val = readl(data_in[priv->minor]);
631
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
632
return -EFAULT;
633
return 0;
634
case IO_READ_OUTBITS:
635
/* *arg is result of reading the output shadow */
636
val = *data_out[priv->minor];
637
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
638
return -EFAULT;
639
break;
640
case IO_SETGET_INPUT:
641
/* bits set in *arg is set to input,
642
* *arg updated with current input pins.
643
*/
644
if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
645
return -EFAULT;
646
val = setget_input(priv, val);
647
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
648
return -EFAULT;
649
break;
650
case IO_SETGET_OUTPUT:
651
/* bits set in *arg is set to output,
652
* *arg updated with current output pins.
653
*/
654
if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
655
return -EFAULT;
656
val = setget_output(priv, val);
657
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
658
return -EFAULT;
659
break;
660
default:
661
return -EINVAL;
662
} /* switch */
663
664
return 0;
665
}
666
667
static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
668
{
669
long ret;
670
671
mutex_lock(&gpio_mutex);
672
ret = gpio_ioctl_unlocked(file, cmd, arg);
673
mutex_unlock(&gpio_mutex);
674
675
return ret;
676
}
677
678
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
679
static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
680
unsigned long arg)
681
{
682
unsigned long flags;
683
unsigned short val;
684
unsigned short shadow;
685
struct gpio_private *priv = file->private_data;
686
687
switch (_IOC_NR(cmd)) {
688
case IO_SETBITS:
689
spin_lock_irqsave(&gpio_lock, flags);
690
/* Set changeable bits with a 1 in arg. */
691
i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
692
shadow |= ~readl(dir_oe[priv->minor]) |
693
(arg & changeable_bits[priv->minor]);
694
i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
695
spin_unlock_irqrestore(&gpio_lock, flags);
696
break;
697
case IO_CLRBITS:
698
spin_lock_irqsave(&gpio_lock, flags);
699
/* Clear changeable bits with a 1 in arg. */
700
i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
701
shadow |= ~readl(dir_oe[priv->minor]) &
702
~(arg & changeable_bits[priv->minor]);
703
i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
704
spin_unlock_irqrestore(&gpio_lock, flags);
705
break;
706
case IO_HIGHALARM:
707
/* Set alarm when bits with 1 in arg go high. */
708
priv->highalarm |= arg;
709
break;
710
case IO_LOWALARM:
711
/* Set alarm when bits with 1 in arg go low. */
712
priv->lowalarm |= arg;
713
break;
714
case IO_CLRALARM:
715
/* Clear alarm for bits with 1 in arg. */
716
priv->highalarm &= ~arg;
717
priv->lowalarm &= ~arg;
718
break;
719
case IO_CFG_WRITE_MODE:
720
{
721
unsigned long dir_shadow;
722
dir_shadow = readl(dir_oe[priv->minor]);
723
724
priv->clk_mask = arg & 0xFF;
725
priv->data_mask = (arg >> 8) & 0xFF;
726
priv->write_msb = (arg >> 16) & 0x01;
727
/* Check if we're allowed to change the bits and
728
* the direction is correct
729
*/
730
if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
731
(priv->data_mask & changeable_bits[priv->minor]) &&
732
(priv->clk_mask & dir_shadow) &&
733
(priv->data_mask & dir_shadow))) {
734
priv->clk_mask = 0;
735
priv->data_mask = 0;
736
return -EPERM;
737
}
738
break;
739
}
740
case IO_READ_INBITS:
741
/* *arg is result of reading the input pins */
742
val = cached_virtual_gpio_read & ~readl(dir_oe[priv->minor]);
743
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
744
return -EFAULT;
745
return 0;
746
747
case IO_READ_OUTBITS:
748
/* *arg is result of reading the output shadow */
749
i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
750
val &= readl(dir_oe[priv->minor]);
751
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
752
return -EFAULT;
753
break;
754
case IO_SETGET_INPUT:
755
{
756
/* bits set in *arg is set to input,
757
* *arg updated with current input pins.
758
*/
759
unsigned short input_mask = ~readl(dir_oe[priv->minor]);
760
if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
761
return -EFAULT;
762
val = setget_input(priv, val);
763
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
764
return -EFAULT;
765
if ((input_mask & val) != input_mask) {
766
/* Input pins changed. All ports desired as input
767
* should be set to logic 1.
768
*/
769
unsigned short change = input_mask ^ val;
770
i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
771
sizeof(shadow));
772
shadow &= ~change;
773
shadow |= val;
774
i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
775
sizeof(shadow));
776
}
777
break;
778
}
779
case IO_SETGET_OUTPUT:
780
/* bits set in *arg is set to output,
781
* *arg updated with current output pins.
782
*/
783
if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
784
return -EFAULT;
785
val = setget_output(priv, val);
786
if (copy_to_user((void __user *)arg, &val, sizeof(val)))
787
return -EFAULT;
788
break;
789
default:
790
return -EINVAL;
791
} /* switch */
792
return 0;
793
}
794
#endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
795
796
static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
797
{
798
unsigned char green;
799
unsigned char red;
800
801
switch (_IOC_NR(cmd)) {
802
case IO_LEDACTIVE_SET:
803
green = ((unsigned char) arg) & 1;
804
red = (((unsigned char) arg) >> 1) & 1;
805
CRIS_LED_ACTIVE_SET_G(green);
806
CRIS_LED_ACTIVE_SET_R(red);
807
break;
808
809
default:
810
return -EINVAL;
811
} /* switch */
812
813
return 0;
814
}
815
816
static int gpio_pwm_set_mode(unsigned long arg, int pwm_port)
817
{
818
int pinmux_pwm = pinmux_pwm0 + pwm_port;
819
int mode;
820
reg_gio_rw_pwm0_ctrl rw_pwm_ctrl = {
821
.ccd_val = 0,
822
.ccd_override = regk_gio_no,
823
.mode = regk_gio_no
824
};
825
int allocstatus;
826
827
if (get_user(mode, &((struct io_pwm_set_mode *) arg)->mode))
828
return -EFAULT;
829
rw_pwm_ctrl.mode = mode;
830
if (mode != PWM_OFF)
831
allocstatus = crisv32_pinmux_alloc_fixed(pinmux_pwm);
832
else
833
allocstatus = crisv32_pinmux_dealloc_fixed(pinmux_pwm);
834
if (allocstatus)
835
return allocstatus;
836
REG_WRITE(reg_gio_rw_pwm0_ctrl, REG_ADDR(gio, regi_gio, rw_pwm0_ctrl) +
837
12 * pwm_port, rw_pwm_ctrl);
838
return 0;
839
}
840
841
static int gpio_pwm_set_period(unsigned long arg, int pwm_port)
842
{
843
struct io_pwm_set_period periods;
844
reg_gio_rw_pwm0_var rw_pwm_widths;
845
846
if (copy_from_user(&periods, (void __user *)arg, sizeof(periods)))
847
return -EFAULT;
848
if (periods.lo > 8191 || periods.hi > 8191)
849
return -EINVAL;
850
rw_pwm_widths.lo = periods.lo;
851
rw_pwm_widths.hi = periods.hi;
852
REG_WRITE(reg_gio_rw_pwm0_var, REG_ADDR(gio, regi_gio, rw_pwm0_var) +
853
12 * pwm_port, rw_pwm_widths);
854
return 0;
855
}
856
857
static int gpio_pwm_set_duty(unsigned long arg, int pwm_port)
858
{
859
unsigned int duty;
860
reg_gio_rw_pwm0_data rw_pwm_duty;
861
862
if (get_user(duty, &((struct io_pwm_set_duty *) arg)->duty))
863
return -EFAULT;
864
if (duty > 255)
865
return -EINVAL;
866
rw_pwm_duty.data = duty;
867
REG_WRITE(reg_gio_rw_pwm0_data, REG_ADDR(gio, regi_gio, rw_pwm0_data) +
868
12 * pwm_port, rw_pwm_duty);
869
return 0;
870
}
871
872
static int gpio_pwm_ioctl(struct gpio_private *priv, unsigned int cmd,
873
unsigned long arg)
874
{
875
int pwm_port = priv->minor - GPIO_MINOR_PWM0;
876
877
switch (_IOC_NR(cmd)) {
878
case IO_PWM_SET_MODE:
879
return gpio_pwm_set_mode(arg, pwm_port);
880
case IO_PWM_SET_PERIOD:
881
return gpio_pwm_set_period(arg, pwm_port);
882
case IO_PWM_SET_DUTY:
883
return gpio_pwm_set_duty(arg, pwm_port);
884
default:
885
return -EINVAL;
886
}
887
return 0;
888
}
889
890
static const struct file_operations gpio_fops = {
891
.owner = THIS_MODULE,
892
.poll = gpio_poll,
893
.unlocked_ioctl = gpio_ioctl,
894
.write = gpio_write,
895
.open = gpio_open,
896
.release = gpio_release,
897
.llseek = noop_llseek,
898
};
899
900
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
901
static void __init virtual_gpio_init(void)
902
{
903
reg_gio_rw_intr_cfg intr_cfg;
904
reg_gio_rw_intr_mask intr_mask;
905
unsigned short shadow;
906
907
shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
908
shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
909
i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
910
911
/* Set interrupt mask and on what state the interrupt shall trigger.
912
* For virtual gpio the interrupt shall trigger on logic '0'.
913
*/
914
intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
915
intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
916
917
switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
918
case 0:
919
intr_cfg.pa0 = regk_gio_lo;
920
intr_mask.pa0 = regk_gio_yes;
921
break;
922
case 1:
923
intr_cfg.pa1 = regk_gio_lo;
924
intr_mask.pa1 = regk_gio_yes;
925
break;
926
case 2:
927
intr_cfg.pa2 = regk_gio_lo;
928
intr_mask.pa2 = regk_gio_yes;
929
break;
930
case 3:
931
intr_cfg.pa3 = regk_gio_lo;
932
intr_mask.pa3 = regk_gio_yes;
933
break;
934
case 4:
935
intr_cfg.pa4 = regk_gio_lo;
936
intr_mask.pa4 = regk_gio_yes;
937
break;
938
case 5:
939
intr_cfg.pa5 = regk_gio_lo;
940
intr_mask.pa5 = regk_gio_yes;
941
break;
942
case 6:
943
intr_cfg.pa6 = regk_gio_lo;
944
intr_mask.pa6 = regk_gio_yes;
945
break;
946
case 7:
947
intr_cfg.pa7 = regk_gio_lo;
948
intr_mask.pa7 = regk_gio_yes;
949
break;
950
}
951
952
REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
953
REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
954
}
955
#endif
956
957
/* main driver initialization routine, called from mem.c */
958
959
static int __init gpio_init(void)
960
{
961
int res;
962
963
printk(KERN_INFO "ETRAX FS GPIO driver v2.7, (c) 2003-2008 "
964
"Axis Communications AB\n");
965
966
/* do the formalities */
967
968
res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
969
if (res < 0) {
970
printk(KERN_ERR "gpio: couldn't get a major number.\n");
971
return res;
972
}
973
974
/* Clear all leds */
975
CRIS_LED_NETWORK_GRP0_SET(0);
976
CRIS_LED_NETWORK_GRP1_SET(0);
977
CRIS_LED_ACTIVE_SET(0);
978
CRIS_LED_DISK_READ(0);
979
CRIS_LED_DISK_WRITE(0);
980
981
int res2 = request_irq(GIO_INTR_VECT, gpio_interrupt,
982
IRQF_SHARED | IRQF_DISABLED, "gpio", &alarmlist);
983
if (res2) {
984
printk(KERN_ERR "err: irq for gpio\n");
985
return res2;
986
}
987
988
/* No IRQs by default. */
989
REG_WR_INT(gio, regi_gio, rw_intr_pins, 0);
990
991
#ifdef CONFIG_ETRAX_VIRTUAL_GPIO
992
virtual_gpio_init();
993
#endif
994
995
return res;
996
}
997
998
/* this makes sure that gpio_init is called during kernel boot */
999
1000
module_init(gpio_init);
1001
1002