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 223
| #include <linux/types.h> #include <linux/kernel.h> #include <linux/delay.h> #include <linux/ide.h> #include <linux/init.h> #include <linux/module.h> #include <linux/errno.h> #include <linux/gpio.h> #include <asm/mach/map.h> #include <linux/uaccess.h> #include <asm/io.h> #include <linux/cdev.h> #include <linux/of.h> #include <linux/of_address.h> #include <linux/of_gpio.h>
#define GPIOLED_CNT 1 #define GPIOLED_NAME "gpioled0"
struct gpioled_dev { dev_t devid; struct cdev cdev; struct class *class; struct device *device; int major; int minor; struct device_node *nd; int led_gpio; };
static struct gpioled_dev gpioled;
static int led_open(struct inode *inode, struct file *filp) { filp->private_data = &gpioled; return 0; }
static ssize_t led_read(struct file *filp, char __user *buf,size_t cnt, loff_t *offt) { return 0; }
static ssize_t led_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt) { int ret; char kern_buf[1]; struct gpioled_dev* pgpioled=(struct gpioled_dev*)filp->private_data;
ret = copy_from_user(kern_buf, buf, cnt); if(0 > ret) { printk(KERN_ERR "kernel write failed!\r\n"); return -EFAULT; }
if (0 == kern_buf[0]) gpio_set_value(pgpioled->led_gpio, 0); else if (1 == kern_buf[0]) gpio_set_value(pgpioled->led_gpio, 1);
return 0; }
static int led_release(struct inode *inode, struct file *filp) { return 0; }
static struct file_operations gpioled_fops = { .owner = THIS_MODULE, .open = led_open, .read = led_read, .write = led_write, .release = led_release, };
static int __init led_init(void) { const char *str; int ret;
gpioled.nd = of_find_node_by_path("/leds/led0"); if(NULL == gpioled.nd) { printk(KERN_ERR "gpioled: Failed to get /led node\n"); return -EINVAL; }
ret = of_property_read_string(gpioled.nd, "status", &str); if(!ret) { if (strcmp(str, "okay")) return -EINVAL; }
ret = of_property_read_string(gpioled.nd, "label", &str); if(0 > ret) { printk(KERN_ERR "gpioled: Failed to get compatible property\n"); return ret; }
if (strcmp(str, "ps_led")) { printk(KERN_ERR "gpioled: Compatible match failed\n"); return -EINVAL; }
printk(KERN_INFO "gpioled: device matching successful!\r\n");
gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpio", 0); if(!gpio_is_valid(gpioled.led_gpio)) { printk(KERN_ERR "gpioled: Failed to get led-gpio\n"); return -EINVAL; }
printk(KERN_INFO "gpioled: led-gpio num = %d\r\n", gpioled.led_gpio);
ret = gpio_request(gpioled.led_gpio, "LED-GPIO"); if (ret) { printk(KERN_ERR "gpioled: Failed to request led-gpio\n"); return ret; }
gpio_direction_output(gpioled.led_gpio, 0);
ret = of_property_read_string(gpioled.nd, "default-state", &str); if(!ret) { if (!strcmp(str, "on")) gpio_set_value(gpioled.led_gpio, 1); else gpio_set_value(gpioled.led_gpio, 0); } else gpio_set_value(gpioled.led_gpio, 0);
if (gpioled.major) { gpioled.devid = MKDEV(gpioled.major, 0); ret = register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME); if (ret) goto out1; } else { ret = alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME); if (ret) goto out1;
gpioled.major = MAJOR(gpioled.devid); gpioled.minor = MINOR(gpioled.devid); }
printk("gpioled: major=%d,minor=%d\r\n",gpioled.major, gpioled.minor);
gpioled.cdev.owner = THIS_MODULE; cdev_init(&gpioled.cdev, &gpioled_fops);
ret = cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT); if (ret) goto out2;
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME); if (IS_ERR(gpioled.class)) { ret = PTR_ERR(gpioled.class); goto out3; }
gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME); if (IS_ERR(gpioled.device)) { ret = PTR_ERR(gpioled.device); goto out4; } return 0;
out4: class_destroy(gpioled.class);
out3: cdev_del(&gpioled.cdev);
out2: unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
out1: gpio_free(gpioled.led_gpio);
return ret; }
static void __exit led_exit(void) { device_destroy(gpioled.class, gpioled.devid);
class_destroy(gpioled.class);
cdev_del(&gpioled.cdev);
unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
gpio_free(gpioled.led_gpio); }
module_init(led_init); module_exit(led_exit);
MODULE_AUTHOR("LO"); MODULE_DESCRIPTION("LO ZYNQ GPIO LED Driver"); MODULE_LICENSE("GPL");
|