guowenxue
2020-08-21 1e6bc76e37f1a0aab7429aac91394c1b4f138bc0
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*********************************************************************************
 *      Copyright:  (C)  2017 LingYun IoT Studio <www.iot-yun.com> 
 *                  All rights reserved.
 *
 *       Filename:  platdrv_led.c
 *    Description:  This is the common LED driver runs on S3C24XX.
 *                 
 *        Version:  1.0.0(10/27/2011~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/27/2011 11:39:10 AM"
 *                 
 ********************************************************************************/
 
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/version.h>
 
#include "platdev_led.h"
 
#define TIMER_TIMEOUT             40
 
#define PLATDRV_MAGIC             0x60
#define LED_OFF                   _IO (PLATDRV_MAGIC, 0x18)
#define LED_ON                    _IO (PLATDRV_MAGIC, 0x19)
#define LED_BLINK                 _IO (PLATDRV_MAGIC, 0x20)
 
static int dev_major = 0;
 
struct led_device
{
    struct s3c_led_platform_data    *data;
    struct cdev                     cdev;
    struct class                    *dev_class;
    struct timer_list               blink_timer;
} led_device;
 
 
void led_timer_handler(unsigned long data)
    int  i; 
    struct s3c_led_platform_data *pdata = (struct s3c_led_platform_data *)data;
 
    for(i=0; i<pdata->nleds; i++) 
    { 
        if(ON == pdata->leds[i].status)
        {
              s3c2410_gpio_setpin(pdata->leds[i].gpio, pdata->leds[i].active_level); 
        }
        else
        {
              s3c2410_gpio_setpin(pdata->leds[i].gpio, ~pdata->leds[i].active_level); 
        }
 
        if(ENABLE == pdata->leds[i].blink )  /* LED should blink */
        {
            /* Switch status between 0 and 1 to turn LED ON or off */
            pdata->leds[i].status = pdata->leds[i].status ^ 0x01;  
        }
 
        mod_timer(&(led_device.blink_timer), jiffies + TIMER_TIMEOUT);
    }
}
 
 
static int led_open(struct inode *inode, struct file *file)
    struct led_device *pdev ;
    struct s3c_led_platform_data *pdata;
 
    pdev = container_of(inode->i_cdev,struct led_device, cdev);
    pdata = pdev->data;
 
    file->private_data = pdata;
 
    return 0;
}
 
 
static int led_release(struct inode *inode, struct file *file)
    return 0;
}
 
static void print_led_help(void)
{
    printk("Follow is the ioctl() command for LED driver:\n");
    printk("Turn LED on command        : %u\n", LED_ON);
    printk("Turn LED off command       : %u\n", LED_OFF);
    printk("Turn LED blink command     : %u\n", LED_BLINK);
}
 
/* compatible with kernel version >=2.6.38*/
static long led_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    struct s3c_led_platform_data *pdata = file->private_data;
 
    switch (cmd)
    {
        case LED_OFF:
            if(pdata->nleds <= arg)
            {
               printk("LED%ld doesn't exist\n", arg);  
               return -ENOTTY;
            }
            pdata->leds[arg].status = OFF;
            pdata->leds[arg].blink = DISABLE;
            break;
 
        case LED_ON:
            if(pdata->nleds <= arg)
            {
               printk("LED%ld doesn't exist\n", arg);  
               return -ENOTTY;
            }
            pdata->leds[arg].status = ON;
            pdata->leds[arg].blink = DISABLE;
            break;
 
        case LED_BLINK:
            if(pdata->nleds <= arg)
            {
               printk("LED%ld doesn't exist\n", arg);  
               return -ENOTTY;
            }
            pdata->leds[arg].blink = ENABLE;
            pdata->leds[arg].status = ON;
            break;
 
        default: 
            printk("LED driver don't support ioctl command=%d\n", cmd); 
            print_led_help();
            return -EINVAL;
 
    }
    return 0;
}
 
 
static struct file_operations led_fops = { 
    .owner = THIS_MODULE, 
    .open = led_open, 
    .release = led_release, 
    .unlocked_ioctl = led_ioctl, /* compatible with kernel version >=2.6.38*/
};
 
 
static int s3c_led_probe(struct platform_device *dev)
{
    struct s3c_led_platform_data *pdata = dev->dev.platform_data; 
    int result = 0;
    int i;
    dev_t devno;
 
    /* Initialize the LED status */
    for(i=0; i<pdata->nleds; i++)
    {
         s3c2410_gpio_cfgpin(pdata->leds[i].gpio, S3C2410_GPIO_OUTPUT);
         if(ON == pdata->leds[i].status)
         {
            s3c2410_gpio_setpin(pdata->leds[i].gpio, pdata->leds[i].active_level); 
         }
         else
         {
            s3c2410_gpio_setpin(pdata->leds[i].gpio, ~pdata->leds[i].active_level); 
         }
    }
 
    /*  Alloc the device for driver */
    if (0 != dev_major) 
    { 
        devno = MKDEV(dev_major, 0); 
        result = register_chrdev_region(devno, 1, "led"); 
    } 
    else 
    { 
        result = alloc_chrdev_region(&devno, 0, 1, "led"); 
        dev_major = MAJOR(devno); 
    }
 
    /* Alloc for device major failure */ 
    if (result < 0) 
    { 
        printk("LED driver can't get major %d\n", dev_major); 
        return result; 
    }
 
    /* Initialize button structure and register cdev*/
    memset(&led_device, 0, sizeof(led_device));
    led_device.data = dev->dev.platform_data;
    cdev_init (&(led_device.cdev), &led_fops);
    led_device.cdev.owner  = THIS_MODULE;
 
    result = cdev_add (&(led_device.cdev), devno , 1); 
    if (result) 
    { 
        printk (KERN_NOTICE "error %d add led device", result ); 
        goto ERROR; 
    } 
    
    led_device.dev_class = class_create(THIS_MODULE, "led"); 
    if(IS_ERR(led_device.dev_class)) 
    { 
        printk("LED driver create class failure\n"); 
        result =  -ENOMEM; 
        goto ERROR; 
    }
 
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)     
    device_create(led_device.dev_class, NULL, devno, NULL, "led");
#else
    device_create (led_device.dev_class, NULL, devno, "led");
#endif
 
    /*  Initial the LED blink timer */
    init_timer(&(led_device.blink_timer));
    led_device.blink_timer.function = led_timer_handler;
    led_device.blink_timer.data = (unsigned long)pdata;
    led_device.blink_timer.expires  = jiffies + TIMER_TIMEOUT;
    add_timer(&(led_device.blink_timer)); 
 
    printk("S3C led driver version 1.0.0 initiliazed.\n"); 
 
    return 0;
               
 
ERROR: 
    printk("S3C led driver version 1.0.0 install failure.\n"); 
    cdev_del(&(led_device.cdev)); 
 
    unregister_chrdev_region(devno, 1); 
    return result;
 
}
 
static int s3c_led_remove(struct platform_device *dev)
{
    dev_t devno = MKDEV(dev_major, 0);
 
    del_timer(&(led_device.blink_timer));
 
    cdev_del(&(led_device.cdev)); 
    device_destroy(led_device.dev_class, devno); 
    class_destroy(led_device.dev_class); 
    
    unregister_chrdev_region(devno, 1); 
    printk("S3C led driver removed\n" );
 
    return 0;
}
 
 
static struct platform_driver s3c_led_driver = { 
    .probe      = s3c_led_probe, 
    .remove     = s3c_led_remove, 
    .driver     = { 
        .name       = "s3c_led", 
        .owner      = THIS_MODULE, 
    },
};
 
 
static int __init platdrv_led_init(void)
{
   int       rv = 0;
 
   rv = platform_driver_register(&s3c_led_driver);
   if(rv)
   {
        printk(KERN_ERR "%s:%d: Can't register platform driver %d\n", __FUNCTION__,__LINE__, rv); 
        return rv;
   }
   printk("Regist S3C LED Platform Driver successfully.\n");
 
   return 0;
}
 
 
static void platdrv_led_exit(void)
{
    printk("%s():%d remove LED platform drvier\n", __FUNCTION__,__LINE__);
    platform_driver_unregister(&s3c_led_driver);
}
 
module_init(platdrv_led_init);
module_exit(platdrv_led_exit);
 
module_param(dev_major, int, S_IRUGO);
 
MODULE_AUTHOR("GuoWenxue<guowenxue@gmail.com>");
MODULE_DESCRIPTION("FL2440 LED driver platform driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:s3c_platdrv_led");