/
leds-msm-pmic.c
661 lines (540 loc) · 16.3 KB
/
leds-msm-pmic.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
* leds-msm-pmic.c - MSM PMIC LEDs driver.
*
* Copyright (c) 2009, Code Aurora Forum. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* modified history
* when who what where
* 11/07/26 zhang.yu_1 for led indication support, red and green
*/
#ifdef CONFIG_LEDS_MSM_PMIC
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/timer.h>
#include <linux/ctype.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <mach/pmic.h>
#include <linux/gpio.h>
#define ZHY_BL_TAG "[ZHY@pmic-leds]"
#define MAX_PMIC_BL_LEVEL 16
#define BLINK_LED_NUM 2
#define GPIO_RED_LIGHT_CTRL 10
#define GPIO_GREEN_LIGHT_CTRL 11
static unsigned data_leds_gpio[] = {
GPIO_CFG(GPIO_RED_LIGHT_CTRL, 0, GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), /* red */
GPIO_CFG(GPIO_GREEN_LIGHT_CTRL, 0, GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), /* green */
};
struct BLINK_LED_data{
int blink_flag;
int blink_led_flag; // 0: off, 1:0n
int blink_on_time; //ms
int blink_off_time; //ms
struct timer_list timer;
struct work_struct work_led_on;
struct work_struct work_led_off;
struct led_classdev led;
};
struct STATUS_LED_data {
spinlock_t data_lock;
struct BLINK_LED_data blink_led[2]; /* red, green */
};
struct STATUS_LED_data *STATUS_LED;
static int led_not_suspend_flag=1; //chenchongbao.20120101 suspend when blink, the blink timer will control led via rpc, rpc will break suspend.
static void pmic_red_led_on(struct work_struct *work)
{
gpio_direction_output(GPIO_RED_LIGHT_CTRL, 1);
}
static void pmic_red_led_off(struct work_struct *work)
{
gpio_direction_output(GPIO_RED_LIGHT_CTRL, 0);
}
static void pmic_green_led_on(struct work_struct *work)
{
gpio_direction_output(GPIO_GREEN_LIGHT_CTRL, 1);
}
static void pmic_green_led_off(struct work_struct *work)
{
gpio_direction_output(GPIO_GREEN_LIGHT_CTRL, 0);
}
static void (*func[4])(struct work_struct *work) = {
pmic_red_led_on,
pmic_red_led_off,
pmic_green_led_on,
pmic_green_led_off,
};
static void msm_pmic_bl_led_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
int ret;
if (!strcmp(led_cdev->name, "red"))
{
if(value == LED_OFF)
{
ret = gpio_direction_output(GPIO_RED_LIGHT_CTRL, 0);
}
else
{
ret = gpio_direction_output(GPIO_RED_LIGHT_CTRL, 1);
}
}
else //green
{
if(value == LED_OFF)
{
ret = gpio_direction_output(GPIO_GREEN_LIGHT_CTRL, 0);
}
else
{
ret = gpio_direction_output(GPIO_GREEN_LIGHT_CTRL, 1);
}
}
if (ret)
dev_err(led_cdev->dev, "[ZHY@PMIC LEDS]can't set pmic backlight\n");
}
static void pmic_leds_timer(unsigned long arg)
{
struct BLINK_LED_data *b_led = (struct BLINK_LED_data *)arg;
if(b_led->blink_flag)
{
if(b_led->blink_led_flag)
{
if(led_not_suspend_flag)
schedule_work(&b_led->work_led_off);
b_led->blink_led_flag = 0;
mod_timer(&b_led->timer,jiffies + msecs_to_jiffies(b_led->blink_off_time));
}
else
{
if(led_not_suspend_flag)
schedule_work(&b_led->work_led_on);
b_led->blink_led_flag = 1;
mod_timer(&b_led->timer,jiffies + msecs_to_jiffies(b_led->blink_on_time));
}
}
else
{
if(b_led->led.brightness)
schedule_work(&b_led->work_led_on);
else
schedule_work(&b_led->work_led_off);
}
}
static ssize_t led_blink_solid_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct STATUS_LED_data *STATUS_LED;
int idx = 1;
struct led_classdev *led_cdev = dev_get_drvdata(dev);
ssize_t ret = 0;
if (!strcmp(led_cdev->name, "red"))
idx = 0;
else
idx = 1;
STATUS_LED = container_of(led_cdev, struct STATUS_LED_data, blink_led[idx].led);
/* no lock needed for this */
sprintf(buf, "%u\n", STATUS_LED->blink_led[idx].blink_flag);
ret = strlen(buf) + 1;
return ret;
}
static ssize_t led_blink_solid_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct STATUS_LED_data *STATUS_LED;
int idx = 1;
char *after;
unsigned long state;
ssize_t ret = -EINVAL;
size_t count;
struct led_classdev *led_cdev = dev_get_drvdata(dev);
if (!strcmp(led_cdev->name, "red"))
idx = 0;
else
idx = 1;
STATUS_LED = container_of(led_cdev, struct STATUS_LED_data, blink_led[idx].led);
state = simple_strtoul(buf, &after, 10);
count = after - buf;
if (*after && isspace(*after))
count++;
if (count == size) {
ret = count;
spin_lock(&STATUS_LED->data_lock);
if(0==state)
{
STATUS_LED->blink_led[idx].blink_flag= 0;
pr_info(ZHY_BL_TAG "DISABLE %s led blink \n",idx?"green":"red");
}
else
{
STATUS_LED->blink_led[idx].blink_flag= 1;
STATUS_LED->blink_led[idx].blink_led_flag = 1;
schedule_work(&STATUS_LED->blink_led[idx].work_led_on);
mod_timer(&STATUS_LED->blink_led[idx].timer,jiffies + msecs_to_jiffies(STATUS_LED->blink_led[idx].blink_off_time));
pr_info(ZHY_BL_TAG "ENABLE %s led blink \n",idx?"green":"red");
}
spin_unlock(&STATUS_LED->data_lock);
}
return ret;
}
static DEVICE_ATTR(blink, 0644, led_blink_solid_show, led_blink_solid_store);
static ssize_t cpldled_grpfreq_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct STATUS_LED_data *STATUS_LED;
int idx = 1;
struct led_classdev *led_cdev = dev_get_drvdata(dev);
if (!strcmp(led_cdev->name, "red"))
idx = 0;
else
idx = 1;
STATUS_LED = container_of(led_cdev, struct STATUS_LED_data, blink_led[idx].led);
return sprintf(buf, "blink_on_time = %u ms \n", STATUS_LED->blink_led[idx].blink_on_time);
}
static ssize_t cpldled_grpfreq_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct STATUS_LED_data *STATUS_LED;
int idx = 1;
char *after;
unsigned long state;
ssize_t ret = -EINVAL;
size_t count;
struct led_classdev *led_cdev = dev_get_drvdata(dev);
if (!strcmp(led_cdev->name, "red"))
idx = 0;
else
idx = 1;
STATUS_LED = container_of(led_cdev, struct STATUS_LED_data, blink_led[idx].led);
state = simple_strtoul(buf, &after, 10);
count = after - buf;
if (*after && isspace(*after))
count++;
if (count == size) {
ret = count;
spin_lock(&STATUS_LED->data_lock);
STATUS_LED->blink_led[idx].blink_on_time = state; //in ms
pr_info(ZHY_BL_TAG "Set %s led blink_on_time=%d ms \n",idx?"green":"red",STATUS_LED->blink_led[idx].blink_on_time);
spin_unlock(&STATUS_LED->data_lock);
}
return ret;
}
static DEVICE_ATTR(grpfreq, 0644, cpldled_grpfreq_show, cpldled_grpfreq_store);
static ssize_t cpldled_grppwm_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct STATUS_LED_data *STATUS_LED;
int idx = 1;
struct led_classdev *led_cdev = dev_get_drvdata(dev);
if (!strcmp(led_cdev->name, "red"))
idx = 0;
else
idx = 1;
STATUS_LED = container_of(led_cdev, struct STATUS_LED_data, blink_led[idx].led);
return sprintf(buf, "blink_off_time = %u ms\n", STATUS_LED->blink_led[idx].blink_off_time);
}
static ssize_t cpldled_grppwm_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t size)
{
struct STATUS_LED_data *STATUS_LED;
int idx = 1;
char *after;
unsigned long state;
ssize_t ret = -EINVAL;
size_t count;
struct led_classdev *led_cdev = dev_get_drvdata(dev);
if (!strcmp(led_cdev->name, "red"))
idx = 0;
else
idx = 1;
STATUS_LED = container_of(led_cdev, struct STATUS_LED_data, blink_led[idx].led);
state = simple_strtoul(buf, &after, 10);
count = after - buf;
if (*after && isspace(*after))
count++;
if (count == size) {
ret = count;
spin_lock(&STATUS_LED->data_lock);
STATUS_LED->blink_led[idx].blink_off_time= state; //in ms
pr_info(ZHY_BL_TAG "Set %s led blink_off_time=%d ms \n",idx?"green":"red",STATUS_LED->blink_led[idx].blink_off_time);
spin_unlock(&STATUS_LED->data_lock);
}
return ret;
}
static DEVICE_ATTR(grppwm, 0644, cpldled_grppwm_show, cpldled_grppwm_store);
static int msm_pmic_led_probe(struct platform_device *pdev)
{
int ret = 0;
int i, j;
for (i = 0;i < 2; i++)
{
ret= gpio_tlmm_config(data_leds_gpio[i],GPIO_CFG_ENABLE);
if (ret)
{
printk(KERN_ERR "%s: gpio_tlmm_config(%#x)=%d\n", __func__, data_leds_gpio[i], ret);
return -EIO;
}
}
if((gpio_direction_output(GPIO_RED_LIGHT_CTRL, 0))<0) //turn off red
{
printk(KERN_ERR "gpio_direction_output: %d failed!\n", GPIO_RED_LIGHT_CTRL);
return -EIO;
}
if((gpio_direction_output(GPIO_GREEN_LIGHT_CTRL, 0))<0) //turn off green
{
printk(KERN_ERR "gpio_direction_output: %d failed!\n", GPIO_GREEN_LIGHT_CTRL);
return -EIO;
}
STATUS_LED = kzalloc(sizeof(struct STATUS_LED_data), GFP_KERNEL);
if (STATUS_LED == NULL) {
printk(KERN_ERR "STATUS_LED_probe: no memory for device\n");
ret = -ENOMEM;
goto err_alloc_failed;
}
STATUS_LED->blink_led[0].led.name = "red";
STATUS_LED->blink_led[0].led.brightness_set = msm_pmic_bl_led_set;
STATUS_LED->blink_led[0].led.brightness = LED_OFF;
STATUS_LED->blink_led[0].blink_flag = 0;
STATUS_LED->blink_led[0].blink_on_time = 500;
STATUS_LED->blink_led[0].blink_off_time = 500;
STATUS_LED->blink_led[1].led.name = "green";
STATUS_LED->blink_led[1].led.brightness_set = msm_pmic_bl_led_set;
STATUS_LED->blink_led[1].led.brightness = LED_OFF;
STATUS_LED->blink_led[1].blink_flag = 0;
STATUS_LED->blink_led[1].blink_on_time = 500;
STATUS_LED->blink_led[1].blink_off_time = 500;
spin_lock_init(&STATUS_LED->data_lock);
for (i = 0; i < 2; i++) { /* red, green */
ret = led_classdev_register(&pdev->dev, &STATUS_LED->blink_led[i].led);
if (ret) {
printk(KERN_ERR "STATUS_LED: led_classdev_register failed\n");
goto err_led_classdev_register_failed;
}
}
for (i = 0; i < 2; i++) {
ret = device_create_file(STATUS_LED->blink_led[i].led.dev, &dev_attr_blink);
if (ret) {
printk(KERN_ERR "STATUS_LED: create dev_attr_blink failed\n");
goto err_out_attr_blink;
}
}
for (i = 0; i < 2; i++) {
ret = device_create_file(STATUS_LED->blink_led[i].led.dev, &dev_attr_grppwm);
if (ret) {
printk(KERN_ERR "STATUS_LED: create dev_attr_grppwm failed\n");
goto err_out_attr_grppwm;
}
}
for (i = 0; i < 2; i++) {
ret = device_create_file(STATUS_LED->blink_led[i].led.dev, &dev_attr_grpfreq);
if (ret) {
printk(KERN_ERR "STATUS_LED: create dev_attr_grpfreq failed\n");
goto err_out_attr_grpfreq;
}
}
dev_set_drvdata(&pdev->dev, STATUS_LED);
for (i = 0; i < 2; i++)
{
INIT_WORK(&STATUS_LED->blink_led[i].work_led_on, func[i*2]);
INIT_WORK(&STATUS_LED->blink_led[i].work_led_off, func[i*2+1]);
setup_timer(&STATUS_LED->blink_led[i].timer, pmic_leds_timer, (unsigned long)&STATUS_LED->blink_led[i]);
msm_pmic_bl_led_set(&STATUS_LED->blink_led[i].led, LED_OFF);
}
return 0;
err_out_attr_grpfreq:
for (j = 0; j < i; j++)
device_remove_file(STATUS_LED->blink_led[i].led.dev, &dev_attr_blink);
i = 2;
err_out_attr_grppwm:
for (j = 0; j < i; j++)
device_remove_file(STATUS_LED->blink_led[i].led.dev, &dev_attr_blink);
i = 2;
err_out_attr_blink:
for (j = 0; j < i; j++)
device_remove_file(STATUS_LED->blink_led[i].led.dev, &dev_attr_blink);
i = 2;
err_led_classdev_register_failed:
for (j = 0; j < i; j++)
led_classdev_unregister(&STATUS_LED->blink_led[i].led);
err_alloc_failed:
kfree(STATUS_LED);
return ret;
}
static int __devexit msm_pmic_led_remove(struct platform_device *pdev)
{
int i;
for (i = 0; i < 2; i++)
led_classdev_unregister(&STATUS_LED->blink_led[i].led);
return 0;
}
#ifndef CONFIG_ZTE_NLED_BLINK_WHILE_APP_SUSPEND //LHX_NLED_20110107_01 enable NLED to blink for all projects
#define CONFIG_ZTE_NLED_BLINK_WHILE_APP_SUSPEND
#endif
#ifdef CONFIG_ZTE_NLED_BLINK_WHILE_APP_SUSPEND
#include "../../arch/arm/mach-msm/proc_comm.h"
#define NLED_APP2SLEEP_FLAG_LCD 0x0001 //green
#define NLED_APP2SLEEP_FLAG_LCD_BLINK 0x0002 //green blink flag
#define NLED_APP2SLEEP_FLAG_KBD 0x0010 //red
#define NLED_APP2SLEEP_FLAG_KBD_BLINK 0x0020 //red blink flag
#define ZTE_PROC_COMM_CMD3_NLED_BLINK_DISABLE 2
#define ZTE_PROC_COMM_CMD3_NLED_BLINK_ENABLE 3
//inform modem the states of NLED through PCOM_CUSTOMER_CMD2
int msm_pmic_led_config_while_app2sleep(unsigned blink_kbd,unsigned blink_lcd, int blink_kbd_flag, int blink_lcd_flag, unsigned set)
{
int ret=0;
int config_last = 0;
if(blink_kbd > 0) //red
{
config_last |= NLED_APP2SLEEP_FLAG_KBD;
}
if(blink_lcd > 0) //green
{
config_last |= NLED_APP2SLEEP_FLAG_LCD;
}
if(blink_kbd_flag > 0) //red
{
config_last |= NLED_APP2SLEEP_FLAG_KBD_BLINK;
}
if(blink_lcd_flag > 0) //green
{
config_last |= NLED_APP2SLEEP_FLAG_LCD_BLINK;
}
if(config_last != 0){ //chenchongbao.20120113 optimize
pr_info(ZHY_BL_TAG"green %d,green_blink %d,red %d,red_blink %d;set(2:dis;3:enable):%d\n",blink_lcd,blink_lcd_flag,blink_kbd,blink_kbd_flag,set);
ret = msm_proc_comm(PCOM_CUSTOMER_CMD3, &config_last, &set);
}
return ret;
}
#endif
#ifdef CONFIG_PM
static int msm_pmic_led_suspend(struct platform_device *dev,
pm_message_t state)
{
led_not_suspend_flag = 0; //ccb add
#ifdef CONFIG_ZTE_NLED_BLINK_WHILE_APP_SUSPEND
msm_pmic_led_config_while_app2sleep( STATUS_LED->blink_led[0].led.brightness,
STATUS_LED->blink_led[1].led.brightness, STATUS_LED->blink_led[0].blink_flag,
STATUS_LED->blink_led[1].blink_flag, ZTE_PROC_COMM_CMD3_NLED_BLINK_ENABLE);
#endif
return 0;
}
static int msm_pmic_led_resume(struct platform_device *dev)
{
led_not_suspend_flag = 1; //ccb add
return 0;
}
#else
#define msm_pmic_led_suspend NULL
#define msm_pmic_led_resume NULL
#endif
static struct platform_driver msm_pmic_led_driver = {
.probe = msm_pmic_led_probe,
.remove = __devexit_p(msm_pmic_led_remove),
.suspend = msm_pmic_led_suspend,
.resume = msm_pmic_led_resume,
.driver = {
.name = "pmic-leds",
.owner = THIS_MODULE,
},
};
static int __init msm_pmic_led_init(void)
{
return platform_driver_register(&msm_pmic_led_driver);
}
module_init(msm_pmic_led_init);
static void __exit msm_pmic_led_exit(void)
{
platform_driver_unregister(&msm_pmic_led_driver);
}
module_exit(msm_pmic_led_exit);
MODULE_DESCRIPTION("MSM PMIC LEDs driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:pmic-leds");
#else
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <mach/pmic.h>
#define MAX_KEYPAD_BL_LEVEL 16
static void msm_keypad_bl_led_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
int ret;
ret = pmic_set_led_intensity(LED_KEYPAD, value / MAX_KEYPAD_BL_LEVEL);
if (ret)
dev_err(led_cdev->dev, "can't set keypad backlight\n");
}
static struct led_classdev msm_kp_bl_led = {
.name = "keyboard-backlight",
.brightness_set = msm_keypad_bl_led_set,
.brightness = LED_OFF,
};
static int msm_pmic_led_probe(struct platform_device *pdev)
{
int rc;
rc = led_classdev_register(&pdev->dev, &msm_kp_bl_led);
if (rc) {
dev_err(&pdev->dev, "unable to register led class driver\n");
return rc;
}
msm_keypad_bl_led_set(&msm_kp_bl_led, LED_OFF);
return rc;
}
static int __devexit msm_pmic_led_remove(struct platform_device *pdev)
{
led_classdev_unregister(&msm_kp_bl_led);
return 0;
}
#ifdef CONFIG_PM
static int msm_pmic_led_suspend(struct platform_device *dev,
pm_message_t state)
{
led_classdev_suspend(&msm_kp_bl_led);
return 0;
}
static int msm_pmic_led_resume(struct platform_device *dev)
{
led_classdev_resume(&msm_kp_bl_led);
return 0;
}
#else
#define msm_pmic_led_suspend NULL
#define msm_pmic_led_resume NULL
#endif
static struct platform_driver msm_pmic_led_driver = {
.probe = msm_pmic_led_probe,
.remove = __devexit_p(msm_pmic_led_remove),
.suspend = msm_pmic_led_suspend,
.resume = msm_pmic_led_resume,
.driver = {
.name = "pmic-leds",
.owner = THIS_MODULE,
},
};
static int __init msm_pmic_led_init(void)
{
return platform_driver_register(&msm_pmic_led_driver);
}
module_init(msm_pmic_led_init);
static void __exit msm_pmic_led_exit(void)
{
platform_driver_unregister(&msm_pmic_led_driver);
}
module_exit(msm_pmic_led_exit);
MODULE_DESCRIPTION("MSM PMIC LEDs driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:pmic-leds");
#endif