1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#include <linux/types.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/init.h> #include <linux/module.h> #include <linux/errno.h> #include <linux/gpio.h> #include <linux/cdev.h> #include <linux/device.h> #include <linux/of_gpio.h> #include <linux/semaphore.h> #include <linux/timer.h> #include <linux/irq.h> #include <linux/wait.h> #include <linux/poll.h> #include <linux/fs.h> #include <linux/fcntl.h> #include <linux/file.h> #include <linux/platform_device.h> #include <linux/irq.h>
#define FLOWINGLED_CNT 1 #define FLOWINGLED_NAME "hcw-flowingled" #define FLOWINGLED_ON 1 #define FLOWINGLED_OFF 0
struct flowingled_dev{ dev_t devid; struct cdev cdev; struct class *class; struct device *device; int major; struct device_node *node;
int gpio[3]; int led_num;
struct timer_list timer; unsigned int period_ms; unsigned int cur_led; unsigned int dir; unsigned int running; };
struct flowingled_dev flowingled;
static void timer_callbacl(struct timer_list *t) { if(flowingled.running) { flowingled.cur_led ++; if(flowingled.cur_led > 2) flowingled.cur_led = 0; else if(flowingled.cur_led < 0) flowingled.cur_led = 2;
for(int i = 0;i < 3;i++) { if(i == flowingled.cur_led) { gpio_set_value(flowingled.gpio[i],1); continue; } gpio_set_value(flowingled.gpio[i],0); } } mod_timer(&flowingled.timer, jiffies + msecs_to_jiffies(500)); }
static int flowingled_open(struct inode *inode, struct file *filp){ filp->private_data = &flowingled; return 0; }
static ssize_t flowingled_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt) { int retvalue; unsigned char databuf[2]; unsigned char led_running_flag;
struct flowingled_dev *dev = (struct flowingled_dev *)filp->private_data;
retvalue = copy_from_user(databuf,buf,cnt); if(retvalue < 0) { printk("kernel write failed\r\n"); return -EFAULT; }
led_running_flag = databuf[0];
if(led_running_flag == FLOWINGLED_ON) { dev->running = 1; } else if(led_running_flag == FLOWINGLED_OFF) { dev->running = 0; }
return 0; }
static struct file_operations flowingled_fops = { .owner = THIS_MODULE, .open = flowingled_open, .write = flowingled_write };
static int flowingled_probe(struct platform_device *dev) { printk("driver and device matched");
if(flowingled.major){ flowingled.devid = MKDEV(flowingled.major, 0); register_chrdev_region(flowingled.devid, FLOWINGLED_CNT, FLOWINGLED_NAME); } else { alloc_chrdev_region(&flowingled.devid, 0, FLOWINGLED_CNT, FLOWINGLED_NAME); flowingled.major = MAJOR(flowingled.devid); }
cdev_init(&flowingled.cdev, &flowingled_fops); cdev_add(&flowingled.cdev, flowingled.devid,FLOWINGLED_CNT);
flowingled.class = class_create(THIS_MODULE, FLOWINGLED_NAME); if(IS_ERR(flowingled.class)) { return PTR_ERR(flowingled.class); }
flowingled.device = device_create(flowingled.class , NULL, flowingled.devid, NULL, FLOWINGLED_NAME); if(IS_ERR(flowingled.device)) { return PTR_ERR(flowingled.device); }
flowingled.node = of_find_node_by_path("/flowingled"); if(flowingled.node == NULL) { printk("flowingled does not found\r\n"); return -EINVAL; }
for (int i = 0; i < 3; i++) { char buf[8]; flowingled.gpio[i] = of_get_named_gpio(flowingled.node, "flowingled-gpios", i); if (flowingled.gpio[i] < 0) { printk("can't get flowingled-gpios[%d]\n", i); return flowingled.gpio[i]; } sprintf(buf ,"LED%d", i); gpio_request(flowingled.gpio[i],buf); gpio_direction_output(flowingled.gpio[i],0); }
timer_setup(&flowingled.timer, timer_callbacl, 0);
mod_timer(&flowingled.timer, jiffies + msecs_to_jiffies(500));
flowingled.running = 0; flowingled.cur_led = 0;
return 0; }
static int flowingled_remove(struct platform_device *dev) { for(int i = 0;i < 3; i++) { gpio_set_value(flowingled.gpio[i],0); }
del_timer_sync(&flowingled.timer);
cdev_del(&flowingled.cdev);
unregister_chrdev_region(flowingled.devid, FLOWINGLED_CNT); device_destroy(flowingled.class, flowingled.devid); class_destroy(flowingled.class); return 0; }
static const struct of_device_id flowing_of_match[] = { {.compatible = "hcw-flowingled"}, {} };
static struct platform_driver flowingled_drv = { .driver = { .name = "imx6u-flowingled", .of_match_table = flowing_of_match, }, .probe = flowingled_probe, .remove = flowingled_remove };
static int __init flowingled_init(void) { return platform_driver_register(&flowingled_drv); }
static void __exit flowingled_exit(void) { platform_driver_unregister(&flowingled_drv); }
module_init(flowingled_init); module_exit(flowingled_exit);
MODULE_AUTHOR("hcw"); MODULE_LICENSE("GPL");
|