blob: 0244e803c1e06b046fc855daeb30027a26cbf052 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * LEDs driver for GPIOs
3 *
4 * Copyright (C) 2007 8D Technologies inc.
5 * Raphael Assenat <raph@8d.com>
6 * Copyright (C) 2008 Freescale Semiconductor, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Modified by YinWenguan: for
13 * (1) All LEDs controlled by GPIOs.
14 * (2) Adding the differences of the GPIO functions.
15 * (3)
16 */
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/platform_device.h>
20#include <linux/gpio.h>
21#include <linux/leds.h>
22#include <linux/of_platform.h>
23#include <linux/of_gpio.h>
24#include <linux/slab.h>
25#include <linux/workqueue.h>
26#include <linux/module.h>
27
28#include <linux/errno.h>
29#include <linux/clk.h>
30#include <mach/irqs.h>
31#include <mach/timex.h>
32#include <linux/irqreturn.h>
33#include <linux/wakelock.h>
34#include <linux/interrupt.h>
35#include <mach/pcu.h>
36#include <linux/mfd/zx234290.h>
37#include <linux/semaphore.h>
38#include <linux/kthread.h>
39
40struct gpio_led_data {
41 struct led_classdev cdev;
42 unsigned gpio;
43 unsigned pin_select;
44 struct work_struct work;
45 u8 new_level;
46 u8 can_sleep;
47 u8 active_low;
48 u8 blinking;
49 int (*platform_gpio_blink_set)(unsigned pin_sel,unsigned gpio, int state,
50 unsigned long *delay_on, unsigned long *delay_off);
51};
52
53struct semaphore s_sink1_sem;
54struct semaphore s_sink2_sem;
55int s_sink1_level;
56int s_sink2_level;
57
58static void gpio_led_work(struct work_struct *work)
59{
60 struct gpio_led_data *led_dat =
61 container_of(work, struct gpio_led_data, work);
62
63 if (led_dat->blinking) {
64 led_dat->platform_gpio_blink_set(led_dat->pin_select,led_dat->gpio,
65 led_dat->new_level,
66 NULL, NULL);
67 led_dat->blinking = 0;
68 } else
69 if(led_dat->pin_select==0)
70 gpio_set_value_cansleep(led_dat->gpio, led_dat->new_level);
71}
72
73static void gpio_led_set(struct led_classdev *led_cdev,
74 enum led_brightness value)
75{
76 struct gpio_led_data *led_dat =
77 container_of(led_cdev, struct gpio_led_data, cdev);
78 int level;
79
80 if (value == LED_OFF)
81 level = 0;
82 else
83 level = 1;
84
85 if (led_dat->active_low)
86 level = !level;
87
88 /* Setting GPIOs with I2C/etc requires a task context, and we don't
89 * seem to have a reliable way to know if we're already in one; so
90 * let's just assume the worst.
91 */
92 if (led_dat->can_sleep) {
93 led_dat->new_level = level;
94 schedule_work(&led_dat->work);
95 } else {
96#if 0
97 if (led_dat->blinking) {/*
98 led_dat->platform_gpio_blink_set(led_dat->pin_select,led_dat->gpio, level,
99 NULL, NULL);
100 led_dat->blinking = 0;*/
101 gpio_set_value(led_dat->gpio, level);
102
103 } else
104 gpio_set_value(led_dat->gpio, level);
105
106#endif
107 if(led_dat->pin_select ==0)
108 gpio_set_value(led_dat->gpio, level);
109 else if(led_dat->pin_select ==1){
110 s_sink1_level = level;
111 up(&s_sink1_sem);
112 }
113 else if(led_dat->pin_select ==2){
114 //zx234297_set_sink(ZX234297_SINK2,level,SINK_CURRENT_5MA);
115 s_sink2_level = level;
116 up(&s_sink2_sem);
117 }
118 else
119 ;
120 }
121}
122
123static int gpio_blink_set(struct led_classdev *led_cdev,
124 unsigned long *delay_on, unsigned long *delay_off)
125{
126 struct gpio_led_data *led_dat =
127 container_of(led_cdev, struct gpio_led_data, cdev);
128
129 if((delay_on ==NULL)||(delay_off ==NULL)){
130 led_dat->blinking = 0;
131 }else{
132 led_dat->blinking = 1;
133 }
134
135 return led_dat->platform_gpio_blink_set(led_dat->pin_select,led_dat->gpio, GPIO_LED_BLINK,
136 delay_on, delay_off);
137}
138
139static int __devinit create_gpio_led(const struct gpio_led *template,
140 struct gpio_led_data *led_dat, struct device *parent,
141 int (*blink_set)(unsigned, unsigned, int, unsigned long *, unsigned long *))
142{
143 int ret=0, state;
144
145 led_dat->gpio = -1;
146
147 /* skip leds that aren't available */
148 if ((!gpio_is_valid(template->gpio))&&(0==template->pin_select)) {
149 printk(KERN_INFO "Skipping unavailable LED gpio %d (%s)\n",
150 template->gpio, template->name);
151 return 0;
152 }
153
154 if(0==template->pin_select){
155 ret = gpio_request(template->gpio, template->name);
156 if (ret < 0)
157 return ret;
158 /* Added by YinWenguan: gpio operation */
159 zx29_gpio_config(template->gpio, template->func);
160 zx29_gpio_set_direction(template->gpio, GPIO_OUT);
161 //zx29_gpio_output_data(template->gpio, GPIO_LOW);
162 }
163
164 led_dat->cdev.name = template->name;
165 led_dat->cdev.default_trigger = template->default_trigger;
166
167 if(0==template->pin_select){
168 led_dat->gpio = template->gpio;
169 led_dat->can_sleep = gpio_cansleep(template->gpio);
170 }
171 led_dat->active_low = template->active_low;
172 led_dat->blinking = 0;
173 if (blink_set) {
174 led_dat->platform_gpio_blink_set = blink_set;
175 led_dat->cdev.blink_set = gpio_blink_set;
176 }
177 led_dat->cdev.brightness_set = gpio_led_set;
178 if ((0==template->pin_select)&&(template->default_state == LEDS_GPIO_DEFSTATE_KEEP))
179 state = !!gpio_get_value_cansleep(led_dat->gpio) ^ led_dat->active_low;
180 else
181 state = (template->default_state == LEDS_GPIO_DEFSTATE_ON);
182 led_dat->cdev.brightness = state ? LED_FULL : LED_OFF;
183 if (!template->retain_state_suspended)
184 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
185 if(0==template->pin_select){
186 ret = gpio_direction_output(led_dat->gpio, led_dat->active_low ^ state);
187 }else if(1==template->pin_select){
188 zx234297_set_sink(ZX234297_SINK1,led_dat->active_low ^ state,SINK_CURRENT_5MA);
189 }else if(2==template->pin_select){
190 zx234297_set_sink(ZX234297_SINK2,led_dat->active_low ^ state,SINK_CURRENT_5MA);
191 }else{
192 printk("led ctrl by gpio or sink set error\n");
193 }
194
195 if (ret < 0)
196 goto err;
197
198 INIT_WORK(&led_dat->work, gpio_led_work);
199
200 ret = led_classdev_register(parent, &led_dat->cdev);
201 if (ret < 0)
202 goto err;
203
204 return 0;
205err:
206 gpio_free(led_dat->gpio);
207 return ret;
208}
209
210static void delete_gpio_led(struct gpio_led_data *led)
211{
212 if ((0==led->pin_select)&&(!gpio_is_valid(led->gpio)))
213 return;
214 led_classdev_unregister(&led->cdev);
215 cancel_work_sync(&led->work);
216 if(0==led->pin_select)/*the pin is gpio ,not sink*/
217 gpio_free(led->gpio);
218}
219
220struct gpio_leds_priv {
221 int num_leds;
222 struct gpio_led_data leds[];
223};
224
225static inline int sizeof_gpio_leds_priv(int num_leds)
226{
227 return sizeof(struct gpio_leds_priv) +
228 (sizeof(struct gpio_led_data) * num_leds);
229}
230
231/* Code to create from OpenFirmware platform devices */
232#ifdef CONFIG_OF_GPIO
233static struct gpio_leds_priv * __devinit gpio_leds_create_of(struct platform_device *pdev)
234{
235 struct device_node *np = pdev->dev.of_node, *child;
236 struct gpio_leds_priv *priv;
237 int count = 0, ret;
238
239 /* count LEDs in this device, so we know how much to allocate */
240 for_each_child_of_node(np, child)
241 count++;
242 if (!count)
243 return NULL;
244
245 priv = kzalloc(sizeof_gpio_leds_priv(count), GFP_KERNEL);
246 if (!priv)
247 return NULL;
248
249 for_each_child_of_node(np, child) {
250 struct gpio_led led = {};
251 enum of_gpio_flags flags;
252 const char *state;
253
254 led.gpio = of_get_gpio_flags(child, 0, &flags);
255 led.active_low = flags & OF_GPIO_ACTIVE_LOW;
256 led.name = of_get_property(child, "label", NULL) ? : child->name;
257 led.default_trigger =
258 of_get_property(child, "linux,default-trigger", NULL);
259 state = of_get_property(child, "default-state", NULL);
260 if (state) {
261 if (!strcmp(state, "keep"))
262 led.default_state = LEDS_GPIO_DEFSTATE_KEEP;
263 else if (!strcmp(state, "on"))
264 led.default_state = LEDS_GPIO_DEFSTATE_ON;
265 else
266 led.default_state = LEDS_GPIO_DEFSTATE_OFF;
267 }
268
269 ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
270 &pdev->dev, NULL);
271 if (ret < 0) {
272 of_node_put(child);
273 goto err;
274 }
275 }
276
277 return priv;
278
279err:
280 for (count = priv->num_leds - 2; count >= 0; count--)
281 delete_gpio_led(&priv->leds[count]);
282 kfree(priv);
283 return NULL;
284}
285
286static const struct of_device_id of_gpio_leds_match[] = {
287 { .compatible = "gpio-leds", },
288 {},
289};
290#else /* CONFIG_OF_GPIO */
291static struct gpio_leds_priv * __devinit gpio_leds_create_of(struct platform_device *pdev)
292{
293 return NULL;
294}
295#define of_gpio_leds_match NULL
296#endif /* CONFIG_OF_GPIO */
297
298#if 1 /*qihongfang*/
299
300struct led_hw_timer
301{
302 struct clk *led_timer_wclk;
303 struct clk *led_timer_pclk;
304 struct wake_lock led_timer_wake_lock;
305 int led_timer_state;
306 int led_delay_on;
307 int led_delay_off;
308 int flag_ledtimer;
309};
310static struct led_hw_timer s_led_hw_timer;
311
312int platform_gpio_blink_set(unsigned pin_sel,unsigned gpio, int state,
313 unsigned long *delay_on, unsigned long *delay_off)
314{
315 if((delay_on ==NULL)||(delay_off==NULL)){/*stop timer*/
316 timer_stop(LED_TIMER_BASE);
317 return 0;
318 }
319
320 if(*delay_on ==0 ||*delay_off ==0 ){
321 timer_stop(LED_TIMER_BASE);
322 return 0;
323 }
324
325 timer_stop(LED_TIMER_BASE);
326 if(pin_sel==0)
327 gpio_set_value(gpio, LEDS_GPIO_DEFSTATE_ON);
328 else if(pin_sel==1){
329 s_sink1_level = 1;
330 up(&s_sink1_sem);
331 }
332 else if(pin_sel==2){
333 //zx234297_set_sink(ZX234297_SINK2,1,SINK_CURRENT_5MA);
334 s_sink2_level = 1;
335 up(&s_sink2_sem);
336 }
337 s_led_hw_timer.led_timer_state = LEDS_GPIO_DEFSTATE_ON;
338 s_led_hw_timer.led_delay_on = *delay_on;
339 s_led_hw_timer.led_delay_off = *delay_off;
340 timer_set_load(LED_TIMER_BASE, (s_led_hw_timer.led_delay_on*LED_CLOCK_RATE)/1000);
341 timer_set_mode(LED_TIMER_BASE, false);
342 timer_start(LED_TIMER_BASE);
343
344 return 0;
345
346}
347
348
349/*******************************************************************************
350* Function: zx29_kpd_irq_handler
351* Description: clear irq , wake thread irq
352* Parameters:
353* Input:
354* Output:
355********************************************************************************/
356static irqreturn_t led_timer_irq_handler(int irq, void *dev_id)
357{
358 int gpio = 0;
359 const struct gpio_led *pdata;
360
361 //wake_lock(&(s_led_hw_timer.led_timer_wake_lock));
362 pcu_clr_irq_pending(irq);
363
364 //wake_lock(&(s_led_hw_timer.led_timer_wake_lock));
365 pdata = (struct gpio_led_platform_data *)dev_id;
366 gpio = pdata->gpio;
367
368 if(LEDS_GPIO_DEFSTATE_ON==s_led_hw_timer.led_timer_state){
369 gpio_set_value(gpio, LEDS_GPIO_DEFSTATE_OFF);
370 s_led_hw_timer.led_timer_state = LEDS_GPIO_DEFSTATE_OFF;
371
372 timer_set_load(LED_TIMER_BASE, (s_led_hw_timer.led_delay_off*LED_CLOCK_RATE)/1000);
373 timer_set_mode(LED_TIMER_BASE, false);
374 timer_start(LED_TIMER_BASE);
375 }
376 else{
377 gpio_set_value(gpio, LEDS_GPIO_DEFSTATE_ON);
378 s_led_hw_timer.led_timer_state = LEDS_GPIO_DEFSTATE_ON;
379
380 timer_set_load(LED_TIMER_BASE, (s_led_hw_timer.led_delay_on*LED_CLOCK_RATE)/1000);
381 timer_set_mode(LED_TIMER_BASE, false);
382 timer_start(LED_TIMER_BASE);
383 }
384
385 //wake_unlock(&(s_led_hw_timer.led_timer_wake_lock));
386
387 return IRQ_HANDLED;
388}
389static int led_timer_init(const struct gpio_led *template)
390{
391 int ret = -1;
392
393 if(1==s_led_hw_timer.flag_ledtimer){
394 printk(KERN_INFO "warning! led_timer_init can init only one time\n");
395 return 0;
396 }
397 s_led_hw_timer.flag_ledtimer = 1;
398 s_led_hw_timer.led_timer_wclk = timer_get_clk(LED_TIMER_NAME, "work_clk");
399 if (s_led_hw_timer.led_timer_wclk == 0)
400 return ret;
401
402 s_led_hw_timer.led_timer_pclk = timer_get_clk(LED_TIMER_NAME, "apb_clk");
403 if (s_led_hw_timer.led_timer_pclk == 0)
404 return ret;
405
406 clk_prepare_enable(s_led_hw_timer.led_timer_pclk);
407
408 clk_set_rate(s_led_hw_timer.led_timer_wclk, LED_CLOCK_RATE);
409 clk_prepare_enable(s_led_hw_timer.led_timer_wclk);
410 //printk("*led_timer=%lu, parent=%s \n", clk_get_rate(s_led_hw_timer.led_timer_wclk), __clk_get_name(clk_get_parent(s_led_hw_timer.led_timer_wclk)));
411
412 irq_set_irq_wake(PS_TIMER2_INT,1);
413
414 //wake_lock_init(&(s_led_hw_timer.led_timer_wake_lock), WAKE_LOCK_SUSPEND, "led_timer");
415
416 //ret = request_threaded_irq(AP_TIMER2_INT, led_timer_irq_handler, led_timer_irq_thread,
417 // /*host->irq_flags |*/IRQF_ONESHOT, "led_timer", template);
418 ret = request_irq(PS_TIMER2_INT, led_timer_irq_handler, IRQF_ONESHOT, "led_timer", template);
419 if(ret < 0)
420 printk(KERN_INFO "warning! led_timer_irq_handler request irq error\n");
421
422 return ret;
423
424}
425 static void led_timer_unint()
426{
427 // wake_lock_destroy(&(s_led_hw_timer.led_timer_wake_lock));
428}
429#endif
430#if 1
431static irqreturn_t led_sink1_thread(void *data)
432{
433 struct gpio_led_data *led_data = data;
434 struct sched_param param = { .sched_priority = 2 };
435 param.sched_priority= 31;
436 sched_setscheduler(current, SCHED_FIFO, &param);
437
438 while(1)
439 {
440 down(&s_sink1_sem);
441 zx234297_set_sink(ZX234297_SINK1,s_sink1_level,SINK_CURRENT_5MA);
442 }
443}
444
445static irqreturn_t led_sink2_thread(void *data)
446{
447 struct gpio_led_data *led_data = data;
448 struct sched_param param = { .sched_priority = 2 };
449 param.sched_priority= 31;
450 sched_setscheduler(current, SCHED_FIFO, &param);
451
452 while(1)
453 {
454 down(&s_sink2_sem);
455 zx234297_set_sink(ZX234297_SINK2,s_sink2_level,SINK_CURRENT_5MA);
456 }
457}
458
459#endif
460static int __devinit gpio_led_probe(struct platform_device *pdev)
461{
462 struct gpio_led_platform_data *pdata = pdev->dev.platform_data;
463 struct gpio_leds_priv *priv;
464 int i, ret = 0;
465 struct task_struct *sink1_thread;
466 struct task_struct *sink2_thread;
467
468 if (pdata && pdata->num_leds) {
469 priv = kzalloc(sizeof_gpio_leds_priv(pdata->num_leds),
470 GFP_KERNEL);
471 if (!priv)
472 return -ENOMEM;
473
474 priv->num_leds = pdata->num_leds;
475 for (i = 0; i < priv->num_leds; i++) {
476 priv->leds[i].pin_select = pdata->leds[i].pin_select;
477 if(priv->leds[i].pin_select ==1){
478 sema_init(&s_sink1_sem, 0);
479 sink1_thread = kthread_run(led_sink1_thread, &priv->leds[i], "led_sink1");
480 BUG_ON(IS_ERR(sink1_thread));
481 }
482 if(priv->leds[i].pin_select ==2){
483 sema_init(&s_sink2_sem, 0);
484 sink2_thread = kthread_run(led_sink2_thread, &priv->leds[i], "led_sink2");
485 BUG_ON(IS_ERR(sink2_thread));
486 }
487
488 if(1==pdata->leds[i].hw_timer){
489 led_timer_init(&pdata->leds[i]);
490 ret = create_gpio_led(&pdata->leds[i],
491 &priv->leds[i],
492 &pdev->dev, pdata->gpio_blink_set);
493
494 }
495 else{
496 ret = create_gpio_led(&pdata->leds[i],
497 &priv->leds[i],
498 &pdev->dev, NULL);
499 }
500 if (ret < 0) {
501 /* On failure: unwind the led creations */
502 for (i = i - 1; i >= 0; i--)
503 delete_gpio_led(&priv->leds[i]);
504 kfree(priv);
505 return ret;
506 }
507 }
508 } else {
509 priv = gpio_leds_create_of(pdev);
510 if (!priv)
511 return -ENODEV;
512 }
513
514 platform_set_drvdata(pdev, priv);
515
516 return 0;
517}
518
519static int __devexit gpio_led_remove(struct platform_device *pdev)
520{
521 struct gpio_leds_priv *priv = dev_get_drvdata(&pdev->dev);
522 int i;
523
524 if(!priv)
525 return -EINVAL;
526
527 for (i = 0; i < priv->num_leds; i++)
528 delete_gpio_led(&priv->leds[i]);
529
530 dev_set_drvdata(&pdev->dev, NULL);
531 kfree(priv);
532
533 if(s_led_hw_timer.flag_ledtimer)
534 led_timer_unint();
535
536 return 0;
537}
538
539static struct platform_driver gpio_led_driver = {
540 .probe = gpio_led_probe,
541 .remove = __devexit_p(gpio_led_remove),
542 .driver = {
543 .name = "leds-gpio",
544 .owner = THIS_MODULE,
545 .of_match_table = of_gpio_leds_match,
546 },
547};
548
549module_platform_driver(gpio_led_driver);
550
551MODULE_AUTHOR("Raphael Assenat <raph@8d.com>, Trent Piepho <tpiepho@freescale.com>");
552MODULE_DESCRIPTION("GPIO LED driver");
553MODULE_LICENSE("GPL");
554MODULE_ALIAS("platform:leds-gpio");