1 //=============================================================================
3 // n2-io.c version 0.1.7
4 // Author: Karen Spearel <kas11 at tampabay.rr.com>
5 // please report problems/bugs directly to the address above
7 // Boilerplate to be added "real soon now"...it is and has always been GPL'ed per
8 // MODULE_LICENSE but is offered without warrantee of any sort..use at your own risk
10 // NOTE: THIS IS INCOMPLETE. INCLUDED ONLY TO KEEP FROM BREAKING THE BUILD,
11 // IT BEEPS AND SENDS A MESSAGE TO /proc/poweroff. EVENTUALLY IT
12 // WILL TALK TO THE n2_pbd DAEMON. EVENTUALLY THE LED DRIVER
13 // WILL TALK TO SOME USERLAND APP BUT ***NOT*** SET_LEDS.
15 //=============================================================================
16 // GPIO Function State
18 // 1 Green LED Ready = 1
19 // 2 Disk 2 LED On = 0
20 // 3 Disk 1 LED On = 0
22 // 5 Power Button Pressed = 1
23 // 8 Power Down Output = 1 powers down N2
24 // 12 Reset Pressed = 0
25 //=============================================================================
26 // this driver is N2 specific and is purposely designed to do the minimum
27 // necessary to provide the necessary services given the limited memory resources
28 // of the N2. As OpenN2 develops, addition features will be added as
29 // suggested by the community.
31 //=============================================================================
33 #include <linux/config.h>
34 #include <linux/version.h>
35 #include <linux/module.h>
36 #include <linux/utsname.h>
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/string.h>
40 #include <linux/proc_fs.h>
41 #include <linux/slab.h>
42 #include <linux/init.h>
43 #include <linux/errno.h>
45 #include <linux/miscdevice.h>
46 #include <linux/device.h>
47 #include <linux/interrupt.h>
48 #include <linux/moduleparam.h>
49 #include <linux/timer.h>
51 #include <asm/system.h>
52 #include <asm/uaccess.h>
53 #include <asm/hardware.h>
54 #include <asm-arm/irq.h>
55 #include <asm-arm/delay.h>
56 #include <asm-arm/signal.h>
59 #define VERSION "0.1.7"
64 #define N2LM_MAJOR 126
66 #define N2PB_IRQ 22 //gpio5
67 #define N2RB_IRQ 29 //gpio12
69 #define N2_BEEP_DUR_LONG 2000
70 #define N2_BEEP_DUR_MED 400
71 #define N2_BEEP_DUR_SHORT 100
72 #define N2_BEEP_PITCH_HIGH 250
73 #define N2_BEEP_PITCH_MED 500
74 #define N2_BEEP_PITCH_LOW 1000
75 #define N2_LONG_DELAY 30000
79 #define N2_PO_GPIO 8 //power off
82 #define GPIO_BZ_BM 0x0010 //b0000 0000 0001 0000
83 #define GPIO_PB_BM 0x0020 //b0000 0000 0010 0000
84 #define GPIO_PO_BM 0x0100 //b0000 0001 0000 0000
85 #define GPIO_RB_BM 0x1000 //b0001 0000 0000 0000
92 #define PWR_OFF_STR "poweroff"
95 // ioctls -- 'M" is used for sound cards...we don't got one so it seems safe
97 #define N2BZ_BEEP_STOP _IO('M',0) //stop multi-beep at end of audible
98 #define N2BZ_BEEP _IO('M',1) //one beep at current defaults
99 #define N2BZ_BEEPS _IOW('M',3,long) //param beeps at current defaults
100 #define N2BZ_TONESET _IOW('M',4,long) //set tone: range is high=250 to low=2000
101 #define N2BZ_ONTIME _IOW('M',5,long) //ontime for multi-beeps in jiffies
102 #define N2BZ_SILENTTIME _IOW('M',6,long) //offtime for multi-beeps in jiffies
103 #define N2BZ_REPEATCNT _IOW('M',7,long) //number of repeats for multi-beeps 0 = forever
104 #define N2BZ_COMBINED _IOW('M',8,long) //combine all params in a long
106 #define N2LM_OFF _IOW('M',32,long)
107 #define N2LM_ON _IOW('M',33,long)
108 #define N2LM_BLINK _IOW('M',34,long)
109 #define N2LM_ALT _IOW('M',35,long)
110 #define N2LM_ALL_ON _IO('M',36)
111 #define N2LM_ALL_OFF _IO('M',37)
114 #define BLINK_DELAY 25
116 // OR Masks to turn these LEDs ON
118 #define RS_RED_ON 0x00000001 //0b0000 0000 0000 0010
119 #define RS_GRN_ON 0x00000002 //0b0000 0000 0000 0001
120 #define RS_YEL_ON 0x00000003 //0b0000 0000 0000 0011
122 // AND Masks to turn these LEDs OFF
124 #define RS_RED_OFF 0xfffffffe //0b1111 1111 1111 1101
125 #define RS_GRN_OFF 0xfffffffd //0b1111 1111 1111 1110
126 #define RS_YEL_OFF 0xfffffffc //0b1111 1111 1111 1100
128 // AND Masks to turn these LEDs ON
130 #define DISK1_ON 0xfffffff7 //0b1111 1111 1111 0111
131 #define DISK2_ON 0xfffffffb //0b1111 1111 1111 1011
133 // Or Masks to turn these LEDs OFF
135 #define DISK1_OFF 0x00000008 //0b0000 0000 0000 1000
136 #define DISK2_OFF 0x00000004 //0b0000 0000 0000 0100
138 // EOR masks for toggling LEDs on/off
140 #define RS_RG_ALT 0x00000003 //eor mask to toggle rs rg bits
141 #define RS_GRN_TGL 0x00000002
142 #define RS_RED_TGL 0x00000001
143 #define DISK1_TGL 0x00000008
144 #define DISK2_TGL 0x00000004
146 // The LED names for switches
154 static unsigned long init_jiffy = 0; //jiffies at init time
155 static unsigned long rb_presses = 0; //number of reset button presses
156 static unsigned long ontime = 50;
157 static unsigned long offtime = 450;
158 static unsigned long bz_repeatcnt = 10;
159 static unsigned long tone = 1000;
161 DECLARE_WAIT_QUEUE_HEAD(n2rb_waitq);
162 DECLARE_WAIT_QUEUE_HEAD(n2pb_waitq);
164 static struct timer_list n2lm_rsg_timer; //rs green
165 static struct timer_list n2lm_rsr_timer; //rs red
166 static struct timer_list n2lm_d1_timer; //drive 1
167 static struct timer_list n2lm_d2_timer; //drive 2
168 static struct timer_list n2rb_timer;
169 static struct timer_list n2pb_timer;
170 static struct timer_list n2bz_timer; //beeper
172 //==================================================================================================
174 // Blinking is handled entirely by the 4 timer handlers. On timeout, the bit in the
175 // GPIO output register is xor'd with a mask corresponding to the selected led which simply
176 // flips that bit. No record of what any of the other leds is doing is needed.
178 //==================================================================================================
179 // this blinks rs green or green/yellow if rs red is on
180 static void n2lm_rsg_handler(unsigned long data)
182 *IXP4XX_GPIO_GPOUTR ^= RS_GRN_TGL; //flip the led
183 n2lm_rsg_timer.expires = jiffies + BLINK_DELAY; //next timeout
184 add_timer(&n2lm_rsg_timer); //reinit timer
188 // this blinks or alternates rs red green... inited wit green on/red off
189 static void n2lm_rsr_handler(unsigned long data)
191 *IXP4XX_GPIO_GPOUTR ^= n2lm_rsr_timer.data;
192 n2lm_rsr_timer.expires = jiffies + BLINK_DELAY;
193 add_timer(&n2lm_rsr_timer);
197 static void n2lm_d1_handler(unsigned long data)
199 *IXP4XX_GPIO_GPOUTR ^= DISK1_TGL;
200 n2lm_d1_timer.expires = jiffies + BLINK_DELAY;
201 add_timer(&n2lm_d1_timer);
205 static void n2lm_d2_handler(unsigned long data)
207 *IXP4XX_GPIO_GPOUTR ^= DISK2_TGL;
208 n2lm_d2_timer.expires = jiffies + BLINK_DELAY;
209 add_timer(&n2lm_d2_timer);
213 //==================================================================================================
215 static void n2lm_timer_start(unsigned long led)
218 printk(KERN_DEBUG "timer: %ld\n",led);
222 n2lm_rsr_timer.expires = jiffies + BLINK_DELAY;
223 add_timer(&n2lm_rsr_timer);
227 n2lm_rsg_timer.expires = jiffies + BLINK_DELAY;
228 add_timer(&n2lm_rsg_timer);
232 n2lm_d1_timer.expires = jiffies + BLINK_DELAY;
233 add_timer(&n2lm_d1_timer);
237 n2lm_d2_timer.expires = jiffies + BLINK_DELAY;
238 add_timer(&n2lm_d2_timer);
247 //==================================================================================================
249 static void n2lm_timer_stop(unsigned long led)
253 del_timer(&n2lm_rsr_timer);
256 del_timer(&n2lm_rsg_timer);
259 del_timer(&n2lm_d1_timer);
262 del_timer(&n2lm_d2_timer);
270 //--------------------------------------------------------------------------------------------------
272 static void n2lm_timer_stop_all(void)
274 del_timer(&n2lm_rsg_timer);
275 del_timer(&n2lm_rsr_timer);
276 del_timer(&n2lm_d1_timer);
277 del_timer(&n2lm_d2_timer);
280 //--------------------------------------------------------------------------------------------------
282 static void n2lm_ledon(unsigned long led)
285 printk(KERN_DEBUG "ledon: %ld\n", led);
289 *IXP4XX_GPIO_GPOUTR |= RS_RED_ON; //1
292 *IXP4XX_GPIO_GPOUTR |= RS_GRN_ON; //2
295 *IXP4XX_GPIO_GPOUTR &= DISK1_ON; //0xfffffffb
298 *IXP4XX_GPIO_GPOUTR &= DISK2_ON; //0xfffffff7
300 case LED_ALL: //all green
301 *IXP4XX_GPIO_GPOUTR |= RS_GRN_ON;
302 *IXP4XX_GPIO_GPOUTR &= (DISK1_ON & DISK2_ON);
307 //--------------------------------------------------------------------------------------------------
309 static void n2lm_ledoff(unsigned long led)
314 *IXP4XX_GPIO_GPOUTR &= RS_RED_OFF; //0xffffffffe
317 *IXP4XX_GPIO_GPOUTR &= RS_GRN_OFF; //0xfffffffd
320 *IXP4XX_GPIO_GPOUTR |= DISK1_OFF; //0x00000004
323 *IXP4XX_GPIO_GPOUTR |= DISK2_OFF; //0x00000008
326 *IXP4XX_GPIO_GPOUTR &= (RS_GRN_OFF & RS_RED_OFF);
327 *IXP4XX_GPIO_GPOUTR |= (DISK1_OFF | DISK2_OFF);
331 //==================================================================================================
333 static int n2lm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long led)
336 printk(KERN_DEBUG "cmd=%d, led=%ld\n", cmd, led);
338 if (led < 0 || led >= PHYS_LEDS)
343 n2lm_timer_stop(led);
348 n2lm_timer_stop(led);
354 if (led == LED_RS_RED)
355 n2lm_rsr_timer.data = RS_RED_TGL;
356 if (led == LED_RS_GRN)
357 n2lm_rsr_timer.data = RS_GRN_TGL;
358 n2lm_timer_start(led);
362 if (led == LED_RS_RED)
364 n2lm_ledon(LED_RS_GRN);
365 n2lm_ledoff(LED_RS_RED);
366 n2lm_rsr_timer.data = RS_RG_ALT;
367 n2lm_timer_start(LED_RS_RED);
373 n2lm_timer_stop_all();
378 n2lm_timer_stop_all();
379 n2lm_ledoff(LED_ALL);
389 static struct file_operations n2lm_fops = {
390 .owner = THIS_MODULE,
393 //==================================================================================================
394 // We can't do anything fancy here since the system tick rate is far below that required to
395 // generate a desirable tone. Therefore we haven't much choice but to use a busy loop until
396 // I get up to speed on the timers. The saving grace is that for the normal uses, nothing
397 // important should be haprepening.
398 //==================================================================================================
400 static void n2_buzz(int tone_delay, int duration)
404 *IXP4XX_GPIO_GPOER &= ~GPIO_BZ_BM;
406 for (i = 1; i < duration; i++) {
407 *IXP4XX_GPIO_GPOUTR &= ~GPIO_BZ_BM;
409 *IXP4XX_GPIO_GPOUTR |= GPIO_BZ_BM;
412 *IXP4XX_GPIO_GPOER |= GPIO_BZ_BM;
416 //=================================================================================================
418 // this handles the buzzer duty cycle
419 static void n2bz_handler(unsigned long data)
421 if (--bz_repeatcnt > 0) { //if just one beep left to do
422 n2bz_timer.expires = jiffies + ontime + offtime; //next timeout
423 add_timer(&n2bz_timer); //reinit timer
425 n2_buzz(tone/2, ontime);
426 printk(KERN_DEBUG "Count = %d\tOntime = %d\n", bz_repeatcnt, ontime);
430 //==================================================================================================
432 static int n2bz_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long param)
436 n2_buzz(tone/2, ontime);
440 del_timer(&n2bz_timer);
445 bz_repeatcnt = 0xffffffff;
447 bz_repeatcnt = param;
452 if (param >= 250 && param <= 2000)
457 if (param > 4 && param < 201)
461 case N2BZ_SILENTTIME:
462 if (param > ontime) //enforce a reasonable duty cycle
470 bz_repeatcnt = 0xffffffff;
472 bz_repeatcnt = param;
476 bz_repeatcnt = (param & 0xF0000000) >> 28; //repeat 1 - 16
477 ontime = (param & 0x0FF00000) >> 20; //ontime 1 - 256 jiffies
478 offtime = (param & 0x000FFF00) >> 8; //offtime 1 - 4095 jiffies
479 tone = (param & 0x000000FF) << 4; //tone (1 - 255) * 16
488 static struct file_operations n2bz_fops = {
489 .owner = THIS_MODULE,
493 //==================================================================================================
495 static irqreturn_t n2pb_handler (int irq, void *dev_id, struct pt_regs *regs)
499 wake_up(&n2pb_waitq);
500 remove_proc_entry(PWR_OFF_STR, NULL); //no parent
501 n2_buzz(N2_BEEP_PITCH_MED, N2_BEEP_DUR_MED);
502 ret = create_proc_entry(PWR_OFF_STR, 0, NULL);
503 printk(KERN_DEBUG "cpe ret = %p\n", ret);
505 // WARNING: This is RUDE...it unconditionally pulls the power plug.
506 // Your data will be at risk...since this is just a test system
507 // I am leaving it enabled...eventually userland needs to get the
508 // message, do an orderly shutdown and use an ioctl or something in
509 // /proc/powerdowm to actually have us pull the plug.
511 *IXP4XX_GPIO_GPOER &= ~GPIO_PO_BM; // enable the pwr cntl gpio
512 *IXP4XX_GPIO_GPOUTR |= GPIO_PO_BM; // do the deed
517 //==================================================================================================
519 //static void do_rb_timeout(unsigned long data)
523 // for (i = 0; i < rb_presses; i++)
524 // n2_buzz(N2_BEEP_PITCH_MED,N2_BEEP_DUR_SHORT);
528 //==================================================================================================
529 // does nothing -- waiting for userland to define
530 // This thing is sorta braindead...edge triggered IRQs aren't available in the drivers yet...so
531 // we hang in a loop until the button is no longer pressed
538 static irqreturn_t n2rb_handler (int irq, void *dev_id, struct pt_regs *regs)
541 static struct testr test[] = {
565 printk("Reset Entry IRQ =%d Presses = %d Jiffies = %08lx\tIO = %x\tIOW = %x\n", irq, rb_presses, jiffies, (int)_IO('M',rb_presses), (int)_IOW('M',rb_presses,long));
567 wake_up(&n2rb_waitq);
568 while ((*IXP4XX_GPIO_GPINR & GPIO_RB_BM) == 0)
569 ; //wait for button release
573 tone = (rb_presses * 50) + 200;
574 ontime = (rb_presses*10) + 100;
575 offtime = 500 - (rb_presses*20);
576 printk("Ontime = %d\tOfftime = %d\tTone = %d\n",ontime,offtime,tone);
579 n2bz_ioctl(NULL,NULL, N2BZ_BEEPS, rb_presses);
580 n2lm_ioctl(NULL,NULL, test[rb_presses].ctl, test[rb_presses].param);
581 // if (rb_presses == 0) {
582 // init_jiffy = jiffies;
583 // init_timer (&n2rb_timer);
584 // n2rb_timer.function = do_rb_timeout;
587 // if (rb_presses == 8)
589 // if (rb_presses & 1)
590 // n2lm_ledon(test[rb_presses]);
592 // n2lm_ledoff(test[rb_presses]);
594 // n2rb_timer.expires = (jiffies + RB_DELAY);
595 // add_timer (&n2rb_timer);
596 // if (rb_presses < 5) {
597 // if (rb_presses > 0)
598 // n2lm_ledoff(rb_presses);
599 // n2lm_ledon(++rb_presses);
600 // n2lm_timer_start(rb_presses);
603 printk(KERN_DEBUG "Reset Exit IRQ=%d Presses= %d Jiffies= %08lx\n", irq, rb_presses, jiffies);
608 //==================================================================================================
609 // What to do here is majorly undetermined...
611 static int n2rb_read (struct file *filp, char __user *buffer, size_t count, loff_t *ppos)
613 printk(KERN_DEBUG "Reset Button Wait\n");
614 interruptible_sleep_on(&n2rb_waitq);
615 return copy_to_user(buffer, "reset", 5) ? -EFAULT : 5;
619 //==================================================================================================
620 // What to do here is majorly undetermined...
622 static int n2pb_read (struct file *filp, char __user *buffer, size_t count, loff_t *ppos)
624 printk(KERN_DEBUG "Power Button Wait\n");
625 interruptible_sleep_on(&n2pb_waitq);
626 return copy_to_user(buffer, "poweroff", 8) ? -EFAULT : 8;
630 //--------------------------------------------------------------------------------------------------
632 static struct file_operations n2rb_fops = {
633 .owner = THIS_MODULE,
637 //--------------------------------------------------------------------------------------------------
639 static struct file_operations n2pb_fops = {
640 .owner = THIS_MODULE,
644 //==================================================================================================
646 static void n2iom_initarch(void)
648 printk(KERN_DEBUG "setup_interrupts - jiffies=%ld init_jiffy=%ld\n", jiffies, init_jiffy);
650 *IXP4XX_GPIO_GPISR = 0x20400000; // read the 2 irqs to clr
651 gpio_line_config(N2_RB_GPIO, IXP4XX_GPIO_IN | IXP4XX_GPIO_ACTIVE_LOW);
652 gpio_line_isr_clear(N2_RB_GPIO);
653 gpio_line_config(N2_PB_GPIO, IXP4XX_GPIO_IN | IXP4XX_GPIO_ACTIVE_HIGH);
654 gpio_line_isr_clear(N2_PB_GPIO);
656 init_timer(&n2lm_rsg_timer);
657 init_timer(&n2lm_rsr_timer);
658 init_timer(&n2lm_d1_timer);
659 init_timer(&n2lm_d2_timer);
660 // init_timer(&n2rb_timer);
661 // init_timer(&n2pb_timer);
662 init_timer(&n2bz_timer);
663 n2lm_rsr_timer.function = n2lm_rsr_handler;
664 n2lm_rsg_timer.function = n2lm_rsg_handler;
665 n2lm_d2_timer.function = n2lm_d2_handler;
666 n2lm_d1_timer.function = n2lm_d1_handler;
667 n2bz_timer.function = n2bz_handler;
668 n2lm_rsr_timer.data = n2lm_rsg_timer.data = n2lm_d1_timer.data = n2lm_d2_timer.data = n2bz_timer.data = 0;
670 *IXP4XX_GPIO_GPOER &= 0xfffffff0; //enable gpio 0-3
671 *IXP4XX_GPIO_GPOUTR |= 0x00000003; //turn off the leds
672 *IXP4XX_GPIO_GPOUTR &= 0xfffffffc;
674 n2_buzz(N2_BEEP_PITCH_MED, N2_BEEP_DUR_SHORT);
675 n2lm_ledoff(LED_ALL);
676 // Default the Ready/Status to Red during kernel boot, Turn Green at the end of sysvinit
677 n2lm_ledon(LED_RS_RED);
682 //==================================================================================================
684 static int __init n2iom_init(void)
686 printk(KERN_INFO "OpenN2 Misc I/O Driver Version %s\n", VERSION);
688 init_jiffy = jiffies;
689 printk(KERN_DEBUG "init_jiffy=%ld\n",init_jiffy);
692 if (register_chrdev(N2RB_MAJOR, "n2_rbm", &n2pb_fops) < NOERR) {
693 printk(KERN_DEBUG "Reset Button Major %d not available\n", N2RB_MAJOR);
696 if (register_chrdev(N2PB_MAJOR, "n2_pbm", &n2rb_fops) < NOERR) {
697 printk(KERN_DEBUG "Power Button Major %d not available\n", N2PB_MAJOR);
700 if (register_chrdev(N2LM_MAJOR, "n2_ledm", &n2lm_fops) < NOERR) {
701 printk(KERN_DEBUG "Led Manager Major %d not available\n", N2LM_MAJOR);
704 if (register_chrdev(N2BZ_MAJOR, "n2_bzm", &n2bz_fops) < NOERR) {
705 printk(KERN_DEBUG "Buzzer Major %d not available\n", N2BZ_MAJOR);
709 if (request_irq(N2RB_IRQ, &n2rb_handler, SA_INTERRUPT, "n2_rb", NULL) < NOERR) {
710 printk(KERN_DEBUG "Reset Button IRQ %d not available\n", N2RB_IRQ);
713 if (request_irq(N2PB_IRQ, &n2pb_handler, SA_INTERRUPT, "n2_pb", NULL) < NOERR) {
714 printk(KERN_DEBUG "Power Button IRQ %d not available\n", N2PB_IRQ);
718 enable_irq(N2PB_IRQ);
719 enable_irq(N2RB_IRQ);
723 //==================================================================================================
725 static void __exit n2iom_exit(void)
727 remove_proc_entry(PWR_OFF_STR, NULL);
728 del_timer(&n2rb_timer);
729 free_irq(N2RB_IRQ,NULL);
730 unregister_chrdev(N2PB_MAJOR, "n2pb");
731 del_timer(&n2pb_timer);
732 free_irq(N2PB_IRQ, NULL);
733 unregister_chrdev(N2RB_MAJOR, "n2rb" );
734 del_timer(&n2lm_rsg_timer);
735 del_timer(&n2lm_rsr_timer);
736 del_timer(&n2lm_d1_timer);
737 del_timer(&n2lm_d2_timer);
738 unregister_chrdev(N2LM_MAJOR, "n2lm" );
741 module_init (n2iom_init);
742 module_exit (n2iom_exit);
744 MODULE_AUTHOR("Karen Spearel <kas11@tampabay.rr.com>");
745 MODULE_DESCRIPTION("OpenN2 Buttons/LEDs IO Driver");
746 MODULE_LICENSE("GPL");
747 static int debug = 7;
748 module_param(debug, int, 0644);
749 MODULE_PARM_DESC(debug, "Debugging enabled = 8");