Root/Examples/drivers/blink/blinker.c

1/*
2 * blinker device driver
3 *
4 * Author: Carlos Camargo
5 * Created: Abril 29 2010
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 */
13
14#include <linux/module.h> /* Needed by all modules */
15#include <linux/kernel.h> /* Needed for KERN_INFO */
16#include <linux/ioport.h>
17#include <linux/device.h>
18#include <linux/interrupt.h> /* We want an interrupt */
19#include <linux/irq.h> /* We want an interrupt */
20#include <linux/platform_device.h>
21#include <linux/fs.h>
22#include <asm/delay.h>
23#include <linux/delay.h>
24
25#include <asm/uaccess.h>
26#include <asm/io.h>
27#include <linux/gpio.h>
28#include <asm/mach-jz4740/gpio.h>
29
30#define LED_PORT JZ_GPIO_BASE_C
31#define LED_PIN JZ_GPIO_PORTC(17)
32
33
34#define SUCCESS 0
35#define DEVICE_NAME "blink" /* Dev name as it appears in /proc/devices */
36#define BUF_LEN 80 /* Max length of the message from the device */
37
38static int is_device_open = 0; /* Used to prevent multiple access to device */
39static int Major;
40
41static int device_open(struct inode *inode, struct file *file)
42{
43
44  unsigned int i;
45  printk( KERN_INFO "Open BLINKER\n" );
46  if (is_device_open)
47    return -EBUSY;
48
49  is_device_open = 1;
50
51
52  for( i=0; i<5; i++ ){
53    gpio_set_value(LED_PIN, 1);
54    mdelay(0x0040);
55    gpio_set_value(LED_PIN, 0);
56    mdelay(0x0040);
57  }
58
59  try_module_get(THIS_MODULE);
60
61  return SUCCESS;
62}
63
64static ssize_t
65device_write(struct file *filp, const char *buff, size_t count, loff_t * off)
66{
67  const char cmd = buff[0];
68  
69  if(cmd=='Q')
70  {
71    printk(KERN_INFO "Q...\n");
72    gpio_set_value(LED_PIN, 1);
73  }
74  else
75    if(cmd=='S'){
76      printk(KERN_INFO "S...\n");
77      gpio_set_value(LED_PIN, 0);
78    }
79  
80  return 1;
81}
82
83
84static int device_release(struct inode *inode, struct file *file)
85{
86  is_device_open = 0;
87
88  module_put(THIS_MODULE);
89
90  printk( KERN_INFO "Close BLINKER\n" );
91
92  return 0;
93}
94
95struct file_operations fops = {
96  .open = device_open,
97  .write = device_write,
98  .release = device_release,
99};
100
101static int __init blink_init(void)
102{
103  printk(KERN_INFO "BLINK module is Up.\n");
104
105  Major = register_chrdev(0, DEVICE_NAME, &fops);
106
107  if (Major < 0) {
108    printk(KERN_ALERT "Registering char device failed with %d\n", Major);
109    return Major;
110  }
111
112  printk(KERN_ALERT "I was assigned major number %d. To talk to\n", Major);
113  printk(KERN_ALERT "the driver, create a dev file with\n");
114  printk(KERN_ALERT "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);
115
116  jz_gpio_set_function(LED_PIN, JZ_GPIO_FUNC_NONE);
117  gpio_direction_output(LED_PIN,0);
118  gpio_set_value(LED_PIN, 1);
119
120
121  return 0;
122}
123
124
125static void __exit blink_exit(void)
126{
127  gpio_set_value(LED_PIN, 0);
128  unregister_chrdev(Major, DEVICE_NAME);
129  printk( KERN_INFO "BLINK driver is down...\n" );
130}
131
132
133module_init(blink_init);
134module_exit(blink_exit);
135
136
137MODULE_LICENSE("GPL");
138MODULE_AUTHOR("Carlos Camargo <cicamargoba@gmail.com>");
139MODULE_DESCRIPTION("BLINKER LED driver");
140MODULE_VERSION("1:0.1");
141
142
143
144

Archive Download this file

Branches:
master



interactive