video: sprdfd: disable ESD feature
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / leds / rt5033_fled.c
1 /* drivers/leds/rt5033_fled.c
2  * RT5033 Flash LED Driver
3  *
4  * Copyright (C) 2013 Richtek Technology Corp.
5  * Author: Patrick Chang <patrick_chang@richtek.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/leds/rtfled.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/mfd/rt5033.h>
20 #include <linux/mfd/rt5033_irq.h>
21 #include <linux/leds/rt5033_fled.h>
22 #include <linux/delay.h>
23 #include <linux/power_supply.h>
24 #include <linux/version.h>
25 #include <linux/of.h>
26
27 #define ALIAS_NAME "rt5033-fled"
28
29 #define RT5033_FLED_INFO(format, args...) \
30         printk(KERN_INFO "%s:%s() line-%d: " format, \
31                         ALIAS_NAME, __FUNCTION__, __LINE__, ## args)
32 #define RT5033_FLED_WARN(format, args...) \
33         printk(KERN_WARNING "%s:%s() line-%d: " format, \
34                         ALIAS_NAME, __FUNCTION__, __LINE__, ## args)
35 #define RT5033_FLED_ERR(format, args...) \
36         printk(KERN_ERR "%s:%s() line-%d: " format, \
37                         ALIAS_NAME, __FUNCTION__, __LINE__, ## args)
38
39
40 #define RT5033_FLED_PIN_CTRL (1<<4)
41
42 #define EN_FLED_IRQ 0
43
44 typedef struct rt5033_fled_info {
45         rt_fled_info_t base;
46         const rt5033_fled_platform_data_t *pdata;
47         rt5033_mfd_chip_t *chip;
48         struct mutex led_lock;
49         struct i2c_client *i2c_client;
50         int torch_current;
51         int strobe_current;
52         int boost : 1;
53         int strobe_status : 1;
54         int ta_exist : 1;
55         int led_count : 2;
56 } rt5033_fled_info_t;
57
58
59 static struct platform_device rt_fled_pdev = {
60         .name = "rt-flash-led",
61         .id = -1,
62 };
63
64 #define RT5033_OFF_EVENT_NRD 0x6B
65 #define FORCE_NR 0x01
66
67 static int rt5033_fled_set_ta_status(struct i2c_client *iic,
68                 int ta_good_and_exist);
69
70 static int rt5033_set_fled_osc_en(struct i2c_client *iic, int en)
71 {
72         return (en ? rt5033_set_bits : rt5033_clr_bits)(iic, 0x1a, (1 << 5));
73 }
74 #ifdef CONFIG_CHARGER_RT5033
75 extern int rt5033_chg_fled_init(struct i2c_client *client);
76 #endif
77
78 static int rt5033_fled_init(struct rt_fled_info *fled_info)
79 {
80         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
81         rt5033_mfd_platform_data_t *mfd_pdata;
82         BUG_ON(info == NULL);
83         mfd_pdata = info->chip->pdata;
84         mutex_lock(&info->led_lock);
85         rt5033_set_bits(info->i2c_client, RT5033_FLED_RESET, 0x80);
86         rt5033_fled_set_ta_status(info->i2c_client, 0);
87 #ifdef  CONFIG_CHARGER_RT5033
88         rt5033_chg_fled_init(info->i2c_client);
89 #else
90         /* Force to do normal read (read from e-fuse) ==> let FLED current be more accurate */
91         rt5033_set_bits(info->i2c_client, RT5033_OFF_EVENT_NRD, FORCE_NR);
92         /* Delay 100 us to wait for normal read complete */
93         udelay(100);
94         /* Finsh normal read and clear FORCE_NR bit */
95         rt5033_clr_bits(info->i2c_client, RT5033_OFF_EVENT_NRD, FORCE_NR);
96 #endif
97         if (!info->pdata->fled1_en)
98                 rt5033_clr_bits(info->i2c_client, RT5033_FLED_FUNCTION1, 0x01);
99         if (!info->pdata->fled2_en)
100                 rt5033_clr_bits(info->i2c_client, RT5033_FLED_FUNCTION1, 0x02);
101         if (info->pdata->fled_mid_track_alive)
102                 rt5033_set_bits(info->i2c_client, RT5033_FLED_CONTROL2, (1 << 6));
103         if (info->pdata->fled_mid_auto_track_en)
104                 rt5033_set_bits(info->i2c_client, RT5033_FLED_CONTROL2, (1 << 7));
105         rt5033_reg_write(info->i2c_client, RT5033_FLED_STROBE_CONTROL1,
106                         (info->pdata->fled_timeout_current_level << 5) |
107                         info->pdata->fled_strobe_current);
108         info->base.flashlight_dev->props.strobe_brightness =
109                 info->pdata->fled_strobe_current;
110
111         rt5033_reg_write(info->i2c_client, RT5033_FLED_STROBE_CONTROL2,
112                         info->pdata->fled_strobe_timeout);
113         info->base.flashlight_dev->props.strobe_timeout =
114                 info->base.hal->fled_strobe_timeout_list(fled_info,
115                                 info->pdata->fled_strobe_timeout);
116         RT5033_FLED_INFO("Strobe timeout = %d ms\n",
117                         info->base.flashlight_dev->props.strobe_timeout);
118         rt5033_reg_write(info->i2c_client, RT5033_FLED_CONTROL1,
119                         (info->pdata->fled_torch_current << 4) |
120                         info->pdata->fled_lv_protection);
121         info->base.flashlight_dev->props.torch_brightness =
122                 info->pdata->fled_torch_current;
123         rt5033_assign_bits(info->i2c_client, RT5033_FLED_CONTROL2,
124                         0x3f, info->pdata->fled_mid_level);
125         info->led_count = info->pdata->fled1_en + info->pdata->fled2_en;
126 #ifdef CONFIG_FLED_RT5033_I2C
127         rt5033_set_bits(info->i2c_client, RT5033_FLED_FUNCTION1, RT5033_FLED_PIN_CTRL);
128 #endif
129         mutex_unlock(&info->led_lock);
130         return 0;
131 }
132
133 static int rt5033_fled_suspend(struct rt_fled_info *info, pm_message_t state)
134 {
135         RT5033_FLED_INFO("Suspend\n");
136         return 0;
137 }
138
139 static int rt5033_fled_resume(struct rt_fled_info *info)
140 {
141         RT5033_FLED_INFO("Resume\n");
142         return 0;
143 }
144
145 static int rt5033_fled_set_ta_status(struct i2c_client *iic, int ta_good_and_exist)
146 {
147         return rt5033_assign_bits(iic, RT5033_FLED_CONTROL5, (0x03 << 6),
148                         ta_good_and_exist ? (0x03 << 6) : 0);
149
150 }
151
152 inline static int rt5033_set_uug_status(struct i2c_client *iic, int uug)
153 {
154         return rt5033_assign_bits(iic, 0x19, 0x02, uug);
155 }
156
157 #ifdef CONFIG_FLED_RT5033_EXT_GPIO
158 /* For GPIO operation, camera driver must use lock / unlock funtion */\rvoid rt5033_fled_strobe_critial_section_lock(struct rt_fled_info *fled_info)
159 {
160         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
161         BUG_ON(info == NULL);
162         rt5033_fled_lock(fled_info);
163         rt5033_fled_set_ta_status(info->i2c_client, 0);
164 }
165 EXPORT_SYMBOL(rt5033_fled_strobe_critial_section_lock);
166
167 void rt5033_fled_strobe_critial_section_unlock(struct rt_fled_info *fled_info)
168 {
169         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
170         BUG_ON(info == NULL);
171         rt5033_fled_set_ta_status(info->i2c_client, info->ta_exist);
172         rt5033_fled_unlock(fled_info);
173 }
174 EXPORT_SYMBOL(rt5033_fled_strobe_critial_section_unlock);
175 #endif /* CONFIG_FLED_RT5033_EXT_GPIO */
176
177 #ifdef CONFIG_FLED_RT5033_EXT_GPIO
178 int32_t rt5033_fled_enable(struct rt_fled_info *fled_info,
179                 int enable)
180 {
181         int32_t ret;
182         rt5033_fled_info_t *info =
183                 (rt5033_fled_info_t *)fled_info;
184         if (enable)
185                 ret = rt5033_clr_bits(info->i2c_client,
186                                 RT5033_FLED_FUNCTION2, 0x01);
187         else {
188                 rt5033_set_fled_osc_en(info->i2c_client, 1);
189                 ret = rt5033_set_bits(info->i2c_client,
190                                 RT5033_FLED_FUNCTION2, 0x01);
191                 rt5033_set_fled_osc_en(info->i2c_client, 0);
192         }
193         return ret;
194 }
195 EXPORT_SYMBOL(rt5033_fled_enable);
196 #endif
197
198
199 #ifdef CONFIG_FLED_RT5033_EXT_GPIO
200 /* For GPIO operation */
201 int32_t rt5033_charger_notification(struct rt_fled_info *fled_info,
202                 int32_t attach)
203 {
204         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
205         int force_torch_en = 0;
206         int chg_status;
207         int reg0x1a;
208         BUG_ON(info == NULL);
209         rt5033_fled_lock(fled_info);
210         info->ta_exist = attach;
211         reg0x1a = rt5033_reg_read(info->i2c_client, 0x1a);
212         reg0x1a |= 0xa0;
213         rt5033_fled_set_ta_status(info->i2c_client, attach);
214         if (attach == 0 && info->boost == 0) {
215                 chg_status = rt5033_reg_read(info->i2c_client, 0x00);
216                 /* remove TA, re-start FlashEN,
217                  * * and then become boost mode => torch enabled */
218                 force_torch_en = (chg_status & 0x08) ? 1 : 0;
219                 /* Enable hidden bit (Force boosting) for TA/USB detaching
220                  * To fix flicking issue for torch while TA is removing
221                  */
222                 if (force_torch_en)
223                         rt5033_reg_write(info->i2c_client, 0x1a, reg0x1a);
224         }
225         rt5033_set_uug_status(info->i2c_client, attach ? 0x02 : 0x00);
226
227         if (attach) {
228                 /* GPIO mode, 0x1 means disable
229                  * * Disable it and then enable it */
230                 rt5033_assign_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81, 0x1);
231                 rt5033_assign_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81, 0x0);
232         }
233
234         rt5033_fled_unlock(fled_info);
235         /* Disable hidden bit (Force boosting) for TA/USB detaching
236          * To fix flicking issue for torch while TA is removing
237          */
238         if (force_torch_en) {
239                 udelay(2500);
240                 rt5033_clr_bits(info->i2c_client, 0x1a, 0x80);
241         }
242         RT5033_FLED_INFO("force_torch_en = %d\n",
243                         force_torch_en);
244         return 0;
245 }
246 #else
247 int32_t rt5033_charger_notification(struct rt_fled_info *fled_info,
248                 int32_t attach)
249 {
250         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
251         int mode = fled_info->hal->fled_get_mode(fled_info);
252         BUG_ON(info == NULL);
253         rt5033_fled_lock(fled_info);
254         info->ta_exist = attach;
255         /* Enable hidden bit (Force boosting) for TA/USB detaching
256          * To fix flicking issue for torch while TA is removing
257          */
258         if (attach == 0) {
259                 /* For i2c FlashLED operation,
260                  * we will check torch had already been on or not
261                  */
262                 if (mode == FLASHLIGHT_MODE_TORCH || mode == FLASHLIGHT_MODE_MIXED)
263                         rt5033_set_bits(info->i2c_client, 0x1a, 0x80);
264         }
265         rt5033_fled_set_ta_status(info->i2c_client, attach);
266         rt5033_set_uug_status(info->i2c_client, attach ? 0x02 : 0x00);
267
268         if (mode == FLASHLIGHT_MODE_TORCH || mode == FLASHLIGHT_MODE_MIXED) {
269                 /* disable FlashEN and then enable it*/
270                 rt5033_assign_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81, 0x0);
271                 rt5033_assign_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81, 0x1);
272         }
273         rt5033_fled_unlock(fled_info);
274         /* Disable hidden bit (Force boosting) for TA/USB detaching
275          * To fix flicking issue for torch while TA is removing
276          */
277         if (attach == 0) {
278                 /* For i2c FlashLED operation,
279                  * we will check torch had already been on or not
280                  */
281                 if (mode == FLASHLIGHT_MODE_TORCH || mode == FLASHLIGHT_MODE_MIXED) {
282                         usleep_range(2500, 2600);
283                         rt5033_clr_bits(info->i2c_client, 0x1a, 0x80);
284                 }
285         }
286         return 0;
287 }
288 #endif
289 EXPORT_SYMBOL(rt5033_charger_notification);
290
291 int32_t rt5033_boost_notification(struct rt_fled_info *fled_info, int32_t on)
292 {
293         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
294         BUG_ON(info == NULL);
295         rt5033_fled_lock(fled_info);
296         info->boost = on;
297 #ifdef CONFIG_FLED_RT5033_EXT_GPIO
298         info->ta_exist = on;
299         rt5033_fled_set_ta_status(info->i2c_client, on);
300 #endif
301         rt5033_set_uug_status(info->i2c_client, on ? 0x02 : 0x00);
302         rt5033_fled_unlock(fled_info);
303         return 0;
304 }
305 EXPORT_SYMBOL(rt5033_boost_notification);
306
307 static int rt5033_fled_set_mode(struct rt_fled_info *fled_info,
308                 flashlight_mode_t mode)
309 {
310         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
311
312         if (info->strobe_status) {
313                 info->strobe_status = 0;
314                 rt5033_fled_set_ta_status(info->i2c_client, info->ta_exist);
315                 rt5033_set_uug_status(info->i2c_client,
316                                 (info->ta_exist | info->boost) ? 0x02 : 0x00);
317                 rt5033_fled_unlock(fled_info);
318         }
319         rt5033_fled_lock(fled_info);
320         switch (mode) {
321                 case FLASHLIGHT_MODE_OFF:
322                         rt5033_clr_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x80);
323                         usleep_range(500, 1000);
324                         rt5033_clr_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x01);
325                         rt5033_set_fled_osc_en(info->i2c_client, 0);
326
327                         break;
328                 case FLASHLIGHT_MODE_TORCH:
329                 case FLASHLIGHT_MODE_MIXED:
330                         rt5033_clr_bits(info->i2c_client, RT5033_FLED_FUNCTION1, 0x04);
331                         rt5033_assign_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81, 0x1);
332                         break;
333                 case FLASHLIGHT_MODE_FLASH:
334                         rt5033_assign_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81, 0x0);
335                         rt5033_set_bits(info->i2c_client, RT5033_FLED_FUNCTION1, 0x04);
336                         rt5033_assign_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81, 0x1);
337                         break;
338                 default:
339                         return -EINVAL;
340         }
341         rt5033_fled_unlock(fled_info);
342         info->base.flashlight_dev->props.mode = mode;
343         return 0;
344 }
345
346 static int rt5033_fled_get_mode(struct rt_fled_info *info)
347 {
348         rt5033_fled_info_t *rt5033_fled_info = (rt5033_fled_info_t *)info;
349         return rt5033_fled_info->base.flashlight_dev->props.mode;
350 }
351
352 void rt5033_fled_lock(struct rt_fled_info *fled_info)
353 {
354         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
355         mutex_lock(&info->led_lock);
356 }
357 EXPORT_SYMBOL(rt5033_fled_lock);
358
359 void rt5033_fled_unlock(struct rt_fled_info *fled_info)
360 {
361         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
362         mutex_unlock(&info->led_lock);
363 }
364 EXPORT_SYMBOL(rt5033_fled_unlock);
365
366 static int rt5033_fled_strobe(struct rt_fled_info *fled_info)
367 {
368         int ret = 0;
369         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
370         rt5033_set_fled_osc_en(info->i2c_client, 1);
371         if (info->strobe_status == 0) {
372                 /* Lock LED until setting to OFF MODE*/
373                 rt5033_fled_lock(fled_info);
374                 info->strobe_status = 1;
375                 rt5033_fled_set_ta_status(info->i2c_client, 0);
376                 rt5033_set_uug_status(info->i2c_client, 0);
377         }
378         switch (info->base.flashlight_dev->props.mode) {
379                 case FLASHLIGHT_MODE_FLASH:
380                         rt5033_set_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81);
381                         break;
382                 case FLASHLIGHT_MODE_MIXED:
383                         rt5033_assign_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81, 0x1);
384                         rt5033_set_bits(info->i2c_client, RT5033_FLED_FUNCTION1, 0x04);
385                         rt5033_clr_bits(info->i2c_client, RT5033_FLED_CONTROL2,
386                                         (1 << 7)); // DISABLE AUTO TRACK
387                         rt5033_set_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81);
388                         break;
389                 default:
390                         RT5033_FLED_ERR("Error : not flash / mixed mode\n");
391                         ret = -EINVAL;
392         }
393         return ret;
394 }
395
396 static int torch_current[] = {
397         12500,
398         25000,
399         37500,
400         50000,
401         62500,
402         75000,
403         87500,
404         100000,
405         112500,
406         125000,
407         137500,
408         150000,
409         162500,
410         175000,
411         187500,
412         200000,
413 };
414
415
416 /* Return value : -EINVAL => selector parameter is out of range, otherwise current in mA*/
417 static int rt5033_fled_troch_current_list(struct rt_fled_info *info,
418                 int selector)
419 {
420         if (selector < 0 || selector >= ARRAY_SIZE(torch_current))
421                 return -EINVAL;
422         return torch_current[selector];
423 }
424
425
426 static int rt5033_fled_strobe_current_list(struct rt_fled_info *info,
427                 int selector)
428 {
429         if (selector < 0 || selector >= 31)
430                 return -EINVAL;
431         return (50 + selector * 25) * 1000;
432 }
433
434 static int strobe_timeout_level[] = {
435         50000,
436         75000,
437         100000,
438         125000,
439         150000,
440         175000,
441         200000,
442 };
443
444
445 static int rt5033_fled_timeout_level_list(struct rt_fled_info *info,
446                 int selector)
447 {
448         if (selector < 0 || selector >= ARRAY_SIZE(strobe_timeout_level))
449                 return -EINVAL;
450         return strobe_timeout_level[selector];
451 }
452 static int lv_protection[] = {
453         2900,
454         3000,
455         3100,
456         3200,
457         3300,
458         3400,
459         3500,
460         3600,
461 };
462
463
464 static struct flashlight_properties rt5033_fled_props = {
465         .type = FLASHLIGHT_TYPE_LED,
466         .torch_brightness = 0,
467         .torch_max_brightness = ARRAY_SIZE(torch_current) - 1,
468         .strobe_brightness = 0,
469         .strobe_max_brightness = 31 - 1,
470         .strobe_delay = 2,
471         .strobe_timeout = 64,
472         .alias_name = "rt5033-fled",
473 };
474
475 /* Return value : -EINVAL => selector parameter is out of range, otherwise voltage in mV*/
476 static int rt5033_fled_lv_protection_list(struct rt_fled_info *info,
477                 int selector)
478 {
479         if (selector < 0 || selector >= ARRAY_SIZE(lv_protection))
480                 return -EINVAL;
481         return lv_protection[selector];
482 }
483 /* Return value : -EINVAL => selector parameter is out of range, otherwise time in ms*/
484 static int rt5033_fled_strobe_timeout_list(struct rt_fled_info *info,
485                 int selector)
486 {
487         if (selector < 0 || selector >= 37)
488                 return -EINVAL;
489         return (64 + selector * 32);
490 }
491
492 static int rt5033_fled_set_torch_current_sel(struct rt_fled_info *fled_info,
493                 int selector)
494 {
495         int rc;
496         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
497         RT5033_FLED_INFO("Set torch current to %d\n", selector);
498         if (selector < 0 || selector >  info->
499                         base.flashlight_dev->props.torch_max_brightness)
500                 return -EINVAL;
501         rc = rt5033_assign_bits(info->i2c_client, RT5033_FLED_CONTROL1,
502                         0xf0, selector << 4);
503         if (rc == 0)
504                 info->base.flashlight_dev->props.torch_brightness = selector;
505         return rc;
506 }
507 static int rt5033_fled_set_strobe_current_sel(struct rt_fled_info *fled_info,
508                 int selector)
509 {
510         int rc;
511         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
512         RT5033_FLED_INFO("Set strobe current to %d\n", selector);
513         if (selector < 0 || selector >  info->
514                         base.flashlight_dev->props.strobe_max_brightness)
515                 return -EINVAL;
516         rc = rt5033_assign_bits(info->i2c_client, RT5033_FLED_STROBE_CONTROL1,
517                         0x1f, selector);
518         if (rc == 0)
519                 info->base.flashlight_dev->props.strobe_brightness = selector;
520         return 0;
521 }
522 static int rt5033_fled_set_timeout_level_sel(struct rt_fled_info *fled_info,
523                 int selector)
524 {
525         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
526         RT5033_FLED_INFO("Set timeout level to %d\n", selector);
527         if (selector < 0 || selector >=  ARRAY_SIZE(strobe_timeout_level))
528                 return -EINVAL;
529         return rt5033_assign_bits(info->i2c_client, RT5033_FLED_STROBE_CONTROL1,
530                         0xe0, selector << 5);
531
532 }
533
534
535 static int rt5033_fled_set_lv_protection_sel(struct rt_fled_info *fled_info,
536                 int selector)
537 {
538         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
539         RT5033_FLED_INFO("Set lv protection to %d\n", selector);
540         if (selector < 0 || selector >=  ARRAY_SIZE(lv_protection))
541                 return -EINVAL;
542
543         return rt5033_assign_bits(info->i2c_client, RT5033_FLED_CONTROL1,
544                         0x07, selector);
545 }
546 static int rt5033_fled_set_strobe_timeout_sel(struct rt_fled_info *fled_info,
547                 int selector)
548 {
549         int rc;
550         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
551         RT5033_FLED_INFO("Set strobe timeout to %d\n", selector);
552         if (selector < 0 || selector >=  37)
553                 return -EINVAL;
554         rc = rt5033_assign_bits(info->i2c_client, RT5033_FLED_STROBE_CONTROL2,
555                         0x3f, selector);
556         if (rc == 0)
557                 fled_info->flashlight_dev->props.strobe_timeout =
558                         rt5033_fled_strobe_timeout_list(fled_info, selector);
559         return rc;
560 }
561
562 static int rt5033_fled_get_torch_current_sel(struct rt_fled_info *fled_info)
563 {
564         int rc;
565         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
566         rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_CONTROL1);
567         if (rc < 0)
568                 return rc;
569         return (rc & 0xf0) >> 4;
570 }
571
572 static int rt5033_fled_get_strobe_current_sel(struct rt_fled_info *fled_info)
573 {
574         int rc;
575         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
576         rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_STROBE_CONTROL1);
577         if (rc < 0)
578                 return rc;
579         return rc & 0x1f;
580 }
581
582 static int rt5033_fled_get_timeout_level_sel(struct rt_fled_info *fled_info)
583 {
584         int rc;
585         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
586         rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_STROBE_CONTROL1);
587         if (rc < 0)
588                 return rc;
589         return (rc & 0xe0) >> 5;
590 }
591
592 static int rt5033_fled_get_lv_protection_sel(struct rt_fled_info *fled_info)
593 {
594         int rc;
595         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
596         rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_CONTROL1);
597         if (rc < 0)
598                 return rc;
599         return rc & 0x07;
600 }
601
602 static int rt5033_fled_get_strobe_timeout_sel(struct rt_fled_info *fled_info)
603 {
604         int rc;
605         rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
606         rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_STROBE_CONTROL2);
607         if (rc < 0)
608                 return rc;
609         return rc & 0x3f;
610 }
611
612 static void rt5033_fled_shutdown(struct rt_fled_info *info)
613 {
614         flashlight_set_mode(info->flashlight_dev, FLASHLIGHT_MODE_OFF);
615         return;
616 }
617
618 static struct rt_fled_hal rt5033_fled_hal = {
619         .fled_init = rt5033_fled_init,
620         .fled_suspend = rt5033_fled_suspend,
621         .fled_resume = rt5033_fled_resume,
622         .fled_set_mode = rt5033_fled_set_mode,
623         .fled_get_mode = rt5033_fled_get_mode,
624         .fled_strobe = rt5033_fled_strobe,
625         .fled_troch_current_list = rt5033_fled_troch_current_list,
626         .fled_strobe_current_list = rt5033_fled_strobe_current_list,
627         .fled_timeout_level_list = rt5033_fled_timeout_level_list,
628         .fled_lv_protection_list = rt5033_fled_lv_protection_list,
629         .fled_strobe_timeout_list = rt5033_fled_strobe_timeout_list,
630         /* method to set */
631         .fled_set_torch_current_sel = rt5033_fled_set_torch_current_sel,
632         .fled_set_strobe_current_sel = rt5033_fled_set_strobe_current_sel,
633         .fled_set_timeout_level_sel = rt5033_fled_set_timeout_level_sel,
634
635         .fled_set_lv_protection_sel = rt5033_fled_set_lv_protection_sel,
636         .fled_set_strobe_timeout_sel = rt5033_fled_set_strobe_timeout_sel,
637
638         /* method to get */
639         .fled_get_torch_current_sel = rt5033_fled_get_torch_current_sel,
640         .fled_get_strobe_current_sel = rt5033_fled_get_strobe_current_sel,
641         .fled_get_timeout_level_sel = rt5033_fled_get_timeout_level_sel,
642         .fled_get_lv_protection_sel = rt5033_fled_get_lv_protection_sel,
643         .fled_get_strobe_timeout_sel = rt5033_fled_get_strobe_timeout_sel,
644         /* PM shutdown, optional */
645         .fled_shutdown = rt5033_fled_shutdown,
646
647 };
648
649
650 static rt5033_fled_platform_data_t rt5033_default_fled_pdata = {
651         .fled1_en = 1,
652         .fled2_en = 1,
653         .fled_mid_track_alive = 0,
654         .fled_mid_auto_track_en = 0,
655         .fled_timeout_current_level = RT5033_TIMEOUT_LEVEL(50),
656         .fled_strobe_current = RT5033_STROBE_CURRENT(750),
657         .fled_strobe_timeout = RT5033_STROBE_TIMEOUT(544),
658         .fled_torch_current = RT5033_TORCH_CURRENT(38),
659         .fled_lv_protection = RT5033_LV_PROTECTION(3200),
660         .fled_mid_level = RT5033_MID_REGULATION_LEVEL(5000),
661 };
662
663
664 #define FLAG_HIGH           (0x01)
665 #define FLAG_LOW            (0x02)
666 #define FLAG_LOW_TO_HIGH    (0x04)
667 #define FLAG_HIGH_TO_LOW    (0x08)
668 #define FLAG_CHANGED        (FLAG_LOW_TO_HIGH|FLAG_HIGH_TO_LOW)
669
670 struct rt5033_fled_irq_handler {
671         const char *name;
672         int irq_index;
673         irqreturn_t (*handler)(int irq, void *data);
674 };
675 #if EN_FLED_IRQ
676 static irqreturn_t rt5033_vf_l_irq_handler(int irq, void *data)
677 {
678         rt5033_fled_info_t *info = data;
679         RT5033_FLED_WARN("LED VF Low\n");
680         BUG_ON(info == NULL);
681         return IRQ_HANDLED;
682 }
683
684 static irqreturn_t rt5033_ledcs2_short_irq_handler(int irq, void *data)
685 {
686         rt5033_fled_info_t *info = data;
687         RT5033_FLED_WARN("LEDCS2 short\n");
688         BUG_ON(info == NULL);
689         return IRQ_HANDLED;
690 }
691
692 static irqreturn_t rt5033_ledcs1_short_irq_handler(int irq, void *data)
693 {
694         rt5033_fled_info_t *info = data;
695         RT5033_FLED_WARN("LEDCS1 short\n");
696         BUG_ON(info == NULL);
697         return IRQ_HANDLED;
698 }
699 #endif //#EN_FLED_IRQ
700
701 const struct rt5033_fled_irq_handler rt5033_fled_irq_handlers[] = {
702 #if EN_FLED_IRQ
703         {
704                 .name = "VF_L",
705                 .handler = rt5033_vf_l_irq_handler,
706                 .irq_index = RT5033_VF_L_IRQ,
707
708         },
709         {
710                 .name = "LEDCS2_SHORT",
711                 .handler = rt5033_ledcs2_short_irq_handler,
712                 .irq_index = RT5033_LEDCS2_SHORT_IRQ,
713         },
714         {
715                 .name = "LEDCS1_SHORT",
716                 .handler = rt5033_ledcs1_short_irq_handler,
717                 .irq_index = RT5033_LEDCS1_SHORT_IRQ,
718         },
719 #endif //EN_FLED_IRQ
720 };
721
722 static int register_irq(struct platform_device *pdev,
723                 rt5033_fled_info_t *info)
724 {
725         int irq;
726         int i, j;
727         int ret;
728         const struct rt5033_fled_irq_handler *irq_handler = rt5033_fled_irq_handlers;
729         const char *irq_name;
730         for (i = 0; i < ARRAY_SIZE(rt5033_fled_irq_handlers); i++) {
731                 irq_name = rt5033_get_irq_name_by_index(irq_handler[i].irq_index);
732                 irq = platform_get_irq_byname(pdev, irq_name);
733                 ret = request_threaded_irq(irq, NULL, irq_handler[i].handler,
734                                 IRQF_ONESHOT | IRQF_TRIGGER_RISING |
735                                 IRQF_NO_SUSPEND, irq_name, info);
736                 if (ret < 0) {
737                         RT5033_FLED_ERR("Failed to request IRQ (%s): #%d: %d\n", irq_name, irq, ret);
738                         goto err_irq;
739                 }
740         }
741
742         return 0;
743 err_irq:
744         for (j = 0; j < i; j++) {
745                 irq_name = rt5033_get_irq_name_by_index(irq_handler[j].irq_index);
746                 irq = platform_get_irq_byname(pdev, irq_name);
747                 free_irq(irq, info);
748         }
749         return ret;
750 }
751
752 static void unregister_irq(struct platform_device *pdev,
753                 rt5033_fled_info_t *info)
754 {
755         int irq;
756         int i;
757         const char *irq_name;
758         const struct rt5033_fled_irq_handler *irq_handler = rt5033_fled_irq_handlers;
759         for (i = 0; i < ARRAY_SIZE(rt5033_fled_irq_handlers); i++) {
760                 irq_name = rt5033_get_irq_name_by_index(irq_handler[i].irq_index);
761                 irq = platform_get_irq_byname(pdev, irq_name);
762                 free_irq(irq, info);
763         }
764 }
765
766
767 #ifdef CONFIG_OF
768 static int rt5033_fled_parse_dt(struct device *dev,
769                 struct rt5033_fled_platform_data *pdata)
770 {
771         struct device_node *np = dev->of_node;
772         u32 buffer[2];
773
774         /* copy default value */
775         *pdata = rt5033_default_fled_pdata;
776
777         if (of_property_read_u32_array(np, "enable", buffer, 2) == 0) {
778                 dev_info(dev, "enable = <%d %d>\n", buffer[0], buffer[1]);
779                 pdata->fled1_en = buffer[0];
780                 pdata->fled2_en = buffer[1];
781         }
782         if (of_property_read_u32_array(np, "mid_track_alive", buffer, 1) == 0) {
783                 dev_info(dev, "mid_track_alive = <%d>\n", buffer[0]);
784                 pdata->fled_mid_track_alive = buffer[0];
785         }
786
787         if (of_property_read_u32_array(np, "mid_auto_track_en", buffer, 1) == 0) {
788                 dev_info(dev, "mid_auto_track_en = <%d>\n", buffer[0]);
789                 pdata->fled_mid_auto_track_en = buffer[0];
790         }
791
792         if (of_property_read_u32_array(np, "timeout_current_level", buffer, 1)
793                         == 0) {
794                 dev_info(dev, "timeout_current_level = <%d>\n", buffer[0]);
795                 pdata->fled_timeout_current_level = RT5033_TIMEOUT_LEVEL(buffer[0]);
796         }
797
798         if (of_property_read_u32_array(np, "strobe_current", buffer, 1) == 0) {
799                 dev_info(dev, "strobe_current = <%d>\n", buffer[0]);
800                 pdata->fled_strobe_current = RT5033_STROBE_CURRENT(buffer[0]);
801         }
802
803         if (of_property_read_u32_array(np, "strobe_timeout", buffer, 1) == 0) {
804                 dev_info(dev, "strobe_timeout = <%d>\n", buffer[0]);
805                 pdata->fled_strobe_timeout = RT5033_STROBE_TIMEOUT(buffer[0]);
806         }
807
808         if (of_property_read_u32_array(np, "torch_current", buffer, 1) == 0) {
809                 dev_info(dev, "torch_current = <%d>\n", buffer[0]);
810                 pdata->fled_torch_current = RT5033_TORCH_CURRENT(buffer[0]);
811         }
812
813         if (of_property_read_u32_array(np, "lv_protection", buffer, 1) == 0) {
814                 dev_info(dev, "lv_protection = <%d>\n", buffer[0]);
815                 pdata->fled_lv_protection = RT5033_LV_PROTECTION(buffer[0]);
816         }
817
818         if (of_property_read_u32_array(np, "mid_level", buffer, 1) == 0) {
819                 dev_info(dev, "mid_level = <%d>\n", buffer[0]);
820                 pdata->fled_mid_level = RT5033_MID_REGULATION_LEVEL(buffer[0]);
821         }
822         return 0;
823 }
824
825 static struct of_device_id rt5033_fled_match_table[] = {
826         { .compatible = "richtek,rt5033-fled",},
827         {},
828 };
829 #else
830 static int rt5033_fled_parse_dt(struct device *dev,
831                 struct rt5033_fled_platform_data *pdata)
832 {
833         return 0;
834 }
835 #define rt5033_fled_match_table NULL
836 #endif
837
838
839 static int rt5033_fled_probe(struct platform_device *pdev)
840 {
841         int ret;
842         struct rt5033_mfd_chip *chip = dev_get_drvdata(pdev->dev.parent);
843         struct rt5033_mfd_platform_data *mfd_pdata = chip->dev->platform_data;
844         struct rt5033_fled_platform_data *pdata;
845         rt5033_fled_info_t *fled_info;
846         RT5033_FLED_INFO("Richtek RT5033 FlashLED driver probing...\n");
847 #ifdef CONFIG_OF
848 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0))
849         if (pdev->dev.parent->of_node) {
850                 pdev->dev.of_node = of_find_compatible_node(
851                                 of_node_get(pdev->dev.parent->of_node), NULL,
852                                 rt5033_fled_match_table[0].compatible);
853         }
854 #endif
855 #endif
856         if (pdev->dev.of_node) {
857                 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
858                 if (!pdata) {
859                         dev_err(&pdev->dev, "Failed to allocate memory\n");
860                         ret = -ENOMEM;
861                         goto err_parse_dt_nomem;
862                 }
863                 ret = rt5033_fled_parse_dt(&pdev->dev, pdata);
864                 if (ret < 0)
865                         goto err_parse_dt;
866         } else {
867                 BUG_ON(mfd_pdata == NULL);
868                 if (mfd_pdata->fled_platform_data)
869                         pdata = mfd_pdata->fled_platform_data;
870                 else
871                         pdata = &rt5033_default_fled_pdata;
872         }
873         fled_info = kzalloc(sizeof(*fled_info), GFP_KERNEL);
874         if (!fled_info) {
875                 ret = -ENOMEM;
876                 goto err_fled_nomem;
877         }
878         mutex_init(&fled_info->led_lock);
879         fled_info->i2c_client = chip->i2c_client;
880         fled_info->base.init_props = &rt5033_fled_props;
881         fled_info->base.hal = &rt5033_fled_hal;
882         fled_info->pdata = pdata;
883         fled_info->chip = chip;
884         chip->fled_info = fled_info;
885         platform_set_drvdata(pdev, fled_info);
886
887         rt_fled_pdev.dev.parent = &(pdev->dev);
888         ret = platform_device_register(&rt_fled_pdev);
889         if (ret < 0)
890                 goto err_register_pdev;
891         ret = register_irq(pdev, fled_info);
892         if (ret < 0) {
893                 RT5033_FLED_ERR("Error : can't register irq\n");
894                 goto err_register_irq;
895
896         }
897         return 0;
898 err_register_irq:
899 err_register_pdev:
900         kfree(fled_info);
901 err_fled_nomem:
902 err_parse_dt:
903 err_parse_dt_nomem:
904         return ret;
905 }
906
907 static int rt5033_fled_remove(struct platform_device *pdev)
908 {
909         struct rt5033_fled_info *fled_info;
910         RT5033_FLED_INFO("Richtek RT5033 FlashLED driver removing...\n");
911         fled_info = platform_get_drvdata(pdev);
912         unregister_irq(pdev, fled_info);
913         platform_device_unregister(&rt_fled_pdev);
914         mutex_destroy(&fled_info->led_lock);
915         kfree(fled_info);
916         return 0;
917 }
918
919 static struct platform_driver rt5033_fled_driver = {
920         .probe  = rt5033_fled_probe,
921         .remove = rt5033_fled_remove,
922         .driver = {
923                 .name   = "rt5033-fled",
924                 .owner  = THIS_MODULE,
925                 .of_match_table = rt5033_fled_match_table,
926         },
927 };
928
929
930 static int __init rt5033_fled_module_init(void)
931 {
932         return platform_driver_register(&rt5033_fled_driver);
933 }
934
935 static void __exit rt5033_fled_module_exit(void)
936 {
937         platform_driver_unregister(&rt5033_fled_driver);
938 }
939
940 device_initcall(rt5033_fled_module_init);
941 module_exit(rt5033_fled_module_exit);
942
943 MODULE_DESCRIPTION("Richtek RT5033 FlashLED Driver");
944 MODULE_AUTHOR("Patrick Chang <patrick_chang@richtek.com>");
945 MODULE_VERSION(RT5033_DRV_VER);
946 MODULE_LICENSE("GPL");
947 MODULE_ALIAS("platform:rt5033-flashLED");