1 /* drivers/leds/rt5033_fled.c
2 * RT5033 Flash LED Driver
4 * Copyright (C) 2013 Richtek Technology Corp.
5 * Author: Patrick Chang <patrick_chang@richtek.com>
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.
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>
27 #define ALIAS_NAME "rt5033-fled"
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)
40 #define RT5033_FLED_PIN_CTRL (1<<4)
44 typedef struct rt5033_fled_info {
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;
53 int strobe_status : 1;
59 static struct platform_device rt_fled_pdev = {
60 .name = "rt-flash-led",
64 #define RT5033_OFF_EVENT_NRD 0x6B
67 static int rt5033_fled_set_ta_status(struct i2c_client *iic,
68 int ta_good_and_exist);
70 static int rt5033_set_fled_osc_en(struct i2c_client *iic, int en)
72 return (en ? rt5033_set_bits : rt5033_clr_bits)(iic, 0x1a, (1 << 5));
74 #ifdef CONFIG_CHARGER_RT5033
75 extern int rt5033_chg_fled_init(struct i2c_client *client);
78 static int rt5033_fled_init(struct rt_fled_info *fled_info)
80 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
81 rt5033_mfd_platform_data_t *mfd_pdata;
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);
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 */
94 /* Finsh normal read and clear FORCE_NR bit */
95 rt5033_clr_bits(info->i2c_client, RT5033_OFF_EVENT_NRD, FORCE_NR);
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;
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);
129 mutex_unlock(&info->led_lock);
133 static int rt5033_fled_suspend(struct rt_fled_info *info, pm_message_t state)
135 RT5033_FLED_INFO("Suspend\n");
139 static int rt5033_fled_resume(struct rt_fled_info *info)
141 RT5033_FLED_INFO("Resume\n");
145 static int rt5033_fled_set_ta_status(struct i2c_client *iic, int ta_good_and_exist)
147 return rt5033_assign_bits(iic, RT5033_FLED_CONTROL5, (0x03 << 6),
148 ta_good_and_exist ? (0x03 << 6) : 0);
152 inline static int rt5033_set_uug_status(struct i2c_client *iic, int uug)
154 return rt5033_assign_bits(iic, 0x19, 0x02, uug);
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)
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);
165 EXPORT_SYMBOL(rt5033_fled_strobe_critial_section_lock);
167 void rt5033_fled_strobe_critial_section_unlock(struct rt_fled_info *fled_info)
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);
174 EXPORT_SYMBOL(rt5033_fled_strobe_critial_section_unlock);
175 #endif /* CONFIG_FLED_RT5033_EXT_GPIO */
177 #ifdef CONFIG_FLED_RT5033_EXT_GPIO
178 int32_t rt5033_fled_enable(struct rt_fled_info *fled_info,
182 rt5033_fled_info_t *info =
183 (rt5033_fled_info_t *)fled_info;
185 ret = rt5033_clr_bits(info->i2c_client,
186 RT5033_FLED_FUNCTION2, 0x01);
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);
195 EXPORT_SYMBOL(rt5033_fled_enable);
199 #ifdef CONFIG_FLED_RT5033_EXT_GPIO
200 /* For GPIO operation */
201 int32_t rt5033_charger_notification(struct rt_fled_info *fled_info,
204 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
205 int force_torch_en = 0;
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);
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
223 rt5033_reg_write(info->i2c_client, 0x1a, reg0x1a);
225 rt5033_set_uug_status(info->i2c_client, attach ? 0x02 : 0x00);
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);
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
238 if (force_torch_en) {
240 rt5033_clr_bits(info->i2c_client, 0x1a, 0x80);
242 RT5033_FLED_INFO("force_torch_en = %d\n",
247 int32_t rt5033_charger_notification(struct rt_fled_info *fled_info,
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
259 /* For i2c FlashLED operation,
260 * we will check torch had already been on or not
262 if (mode == FLASHLIGHT_MODE_TORCH || mode == FLASHLIGHT_MODE_MIXED)
263 rt5033_set_bits(info->i2c_client, 0x1a, 0x80);
265 rt5033_fled_set_ta_status(info->i2c_client, attach);
266 rt5033_set_uug_status(info->i2c_client, attach ? 0x02 : 0x00);
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);
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
278 /* For i2c FlashLED operation,
279 * we will check torch had already been on or not
281 if (mode == FLASHLIGHT_MODE_TORCH || mode == FLASHLIGHT_MODE_MIXED) {
282 usleep_range(2500, 2600);
283 rt5033_clr_bits(info->i2c_client, 0x1a, 0x80);
289 EXPORT_SYMBOL(rt5033_charger_notification);
291 int32_t rt5033_boost_notification(struct rt_fled_info *fled_info, int32_t on)
293 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
294 BUG_ON(info == NULL);
295 rt5033_fled_lock(fled_info);
297 #ifdef CONFIG_FLED_RT5033_EXT_GPIO
299 rt5033_fled_set_ta_status(info->i2c_client, on);
301 rt5033_set_uug_status(info->i2c_client, on ? 0x02 : 0x00);
302 rt5033_fled_unlock(fled_info);
305 EXPORT_SYMBOL(rt5033_boost_notification);
307 static int rt5033_fled_set_mode(struct rt_fled_info *fled_info,
308 flashlight_mode_t mode)
310 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
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);
319 rt5033_fled_lock(fled_info);
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);
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);
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);
341 rt5033_fled_unlock(fled_info);
342 info->base.flashlight_dev->props.mode = mode;
346 static int rt5033_fled_get_mode(struct rt_fled_info *info)
348 rt5033_fled_info_t *rt5033_fled_info = (rt5033_fled_info_t *)info;
349 return rt5033_fled_info->base.flashlight_dev->props.mode;
352 void rt5033_fled_lock(struct rt_fled_info *fled_info)
354 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
355 mutex_lock(&info->led_lock);
357 EXPORT_SYMBOL(rt5033_fled_lock);
359 void rt5033_fled_unlock(struct rt_fled_info *fled_info)
361 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
362 mutex_unlock(&info->led_lock);
364 EXPORT_SYMBOL(rt5033_fled_unlock);
366 static int rt5033_fled_strobe(struct rt_fled_info *fled_info)
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);
378 switch (info->base.flashlight_dev->props.mode) {
379 case FLASHLIGHT_MODE_FLASH:
380 rt5033_set_bits(info->i2c_client, RT5033_FLED_FUNCTION2, 0x81);
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);
390 RT5033_FLED_ERR("Error : not flash / mixed mode\n");
396 static int torch_current[] = {
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,
420 if (selector < 0 || selector >= ARRAY_SIZE(torch_current))
422 return torch_current[selector];
426 static int rt5033_fled_strobe_current_list(struct rt_fled_info *info,
429 if (selector < 0 || selector >= 31)
431 return (50 + selector * 25) * 1000;
434 static int strobe_timeout_level[] = {
445 static int rt5033_fled_timeout_level_list(struct rt_fled_info *info,
448 if (selector < 0 || selector >= ARRAY_SIZE(strobe_timeout_level))
450 return strobe_timeout_level[selector];
452 static int lv_protection[] = {
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,
471 .strobe_timeout = 64,
472 .alias_name = "rt5033-fled",
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,
479 if (selector < 0 || selector >= ARRAY_SIZE(lv_protection))
481 return lv_protection[selector];
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,
487 if (selector < 0 || selector >= 37)
489 return (64 + selector * 32);
492 static int rt5033_fled_set_torch_current_sel(struct rt_fled_info *fled_info,
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)
501 rc = rt5033_assign_bits(info->i2c_client, RT5033_FLED_CONTROL1,
502 0xf0, selector << 4);
504 info->base.flashlight_dev->props.torch_brightness = selector;
507 static int rt5033_fled_set_strobe_current_sel(struct rt_fled_info *fled_info,
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)
516 rc = rt5033_assign_bits(info->i2c_client, RT5033_FLED_STROBE_CONTROL1,
519 info->base.flashlight_dev->props.strobe_brightness = selector;
522 static int rt5033_fled_set_timeout_level_sel(struct rt_fled_info *fled_info,
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))
529 return rt5033_assign_bits(info->i2c_client, RT5033_FLED_STROBE_CONTROL1,
530 0xe0, selector << 5);
535 static int rt5033_fled_set_lv_protection_sel(struct rt_fled_info *fled_info,
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))
543 return rt5033_assign_bits(info->i2c_client, RT5033_FLED_CONTROL1,
546 static int rt5033_fled_set_strobe_timeout_sel(struct rt_fled_info *fled_info,
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)
554 rc = rt5033_assign_bits(info->i2c_client, RT5033_FLED_STROBE_CONTROL2,
557 fled_info->flashlight_dev->props.strobe_timeout =
558 rt5033_fled_strobe_timeout_list(fled_info, selector);
562 static int rt5033_fled_get_torch_current_sel(struct rt_fled_info *fled_info)
565 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
566 rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_CONTROL1);
569 return (rc & 0xf0) >> 4;
572 static int rt5033_fled_get_strobe_current_sel(struct rt_fled_info *fled_info)
575 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
576 rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_STROBE_CONTROL1);
582 static int rt5033_fled_get_timeout_level_sel(struct rt_fled_info *fled_info)
585 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
586 rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_STROBE_CONTROL1);
589 return (rc & 0xe0) >> 5;
592 static int rt5033_fled_get_lv_protection_sel(struct rt_fled_info *fled_info)
595 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
596 rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_CONTROL1);
602 static int rt5033_fled_get_strobe_timeout_sel(struct rt_fled_info *fled_info)
605 rt5033_fled_info_t *info = (rt5033_fled_info_t *)fled_info;
606 rc = rt5033_reg_read(info->i2c_client, RT5033_FLED_STROBE_CONTROL2);
612 static void rt5033_fled_shutdown(struct rt_fled_info *info)
614 flashlight_set_mode(info->flashlight_dev, FLASHLIGHT_MODE_OFF);
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,
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,
635 .fled_set_lv_protection_sel = rt5033_fled_set_lv_protection_sel,
636 .fled_set_strobe_timeout_sel = rt5033_fled_set_strobe_timeout_sel,
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,
650 static rt5033_fled_platform_data_t rt5033_default_fled_pdata = {
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),
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)
670 struct rt5033_fled_irq_handler {
673 irqreturn_t (*handler)(int irq, void *data);
676 static irqreturn_t rt5033_vf_l_irq_handler(int irq, void *data)
678 rt5033_fled_info_t *info = data;
679 RT5033_FLED_WARN("LED VF Low\n");
680 BUG_ON(info == NULL);
684 static irqreturn_t rt5033_ledcs2_short_irq_handler(int irq, void *data)
686 rt5033_fled_info_t *info = data;
687 RT5033_FLED_WARN("LEDCS2 short\n");
688 BUG_ON(info == NULL);
692 static irqreturn_t rt5033_ledcs1_short_irq_handler(int irq, void *data)
694 rt5033_fled_info_t *info = data;
695 RT5033_FLED_WARN("LEDCS1 short\n");
696 BUG_ON(info == NULL);
699 #endif //#EN_FLED_IRQ
701 const struct rt5033_fled_irq_handler rt5033_fled_irq_handlers[] = {
705 .handler = rt5033_vf_l_irq_handler,
706 .irq_index = RT5033_VF_L_IRQ,
710 .name = "LEDCS2_SHORT",
711 .handler = rt5033_ledcs2_short_irq_handler,
712 .irq_index = RT5033_LEDCS2_SHORT_IRQ,
715 .name = "LEDCS1_SHORT",
716 .handler = rt5033_ledcs1_short_irq_handler,
717 .irq_index = RT5033_LEDCS1_SHORT_IRQ,
722 static int register_irq(struct platform_device *pdev,
723 rt5033_fled_info_t *info)
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);
737 RT5033_FLED_ERR("Failed to request IRQ (%s): #%d: %d\n", irq_name, irq, ret);
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);
752 static void unregister_irq(struct platform_device *pdev,
753 rt5033_fled_info_t *info)
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);
768 static int rt5033_fled_parse_dt(struct device *dev,
769 struct rt5033_fled_platform_data *pdata)
771 struct device_node *np = dev->of_node;
774 /* copy default value */
775 *pdata = rt5033_default_fled_pdata;
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];
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];
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];
792 if (of_property_read_u32_array(np, "timeout_current_level", buffer, 1)
794 dev_info(dev, "timeout_current_level = <%d>\n", buffer[0]);
795 pdata->fled_timeout_current_level = RT5033_TIMEOUT_LEVEL(buffer[0]);
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]);
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]);
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]);
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]);
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]);
825 static struct of_device_id rt5033_fled_match_table[] = {
826 { .compatible = "richtek,rt5033-fled",},
830 static int rt5033_fled_parse_dt(struct device *dev,
831 struct rt5033_fled_platform_data *pdata)
835 #define rt5033_fled_match_table NULL
839 static int rt5033_fled_probe(struct platform_device *pdev)
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");
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);
856 if (pdev->dev.of_node) {
857 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
859 dev_err(&pdev->dev, "Failed to allocate memory\n");
861 goto err_parse_dt_nomem;
863 ret = rt5033_fled_parse_dt(&pdev->dev, pdata);
867 BUG_ON(mfd_pdata == NULL);
868 if (mfd_pdata->fled_platform_data)
869 pdata = mfd_pdata->fled_platform_data;
871 pdata = &rt5033_default_fled_pdata;
873 fled_info = kzalloc(sizeof(*fled_info), GFP_KERNEL);
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);
887 rt_fled_pdev.dev.parent = &(pdev->dev);
888 ret = platform_device_register(&rt_fled_pdev);
890 goto err_register_pdev;
891 ret = register_irq(pdev, fled_info);
893 RT5033_FLED_ERR("Error : can't register irq\n");
894 goto err_register_irq;
907 static int rt5033_fled_remove(struct platform_device *pdev)
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);
919 static struct platform_driver rt5033_fled_driver = {
920 .probe = rt5033_fled_probe,
921 .remove = rt5033_fled_remove,
923 .name = "rt5033-fled",
924 .owner = THIS_MODULE,
925 .of_match_table = rt5033_fled_match_table,
930 static int __init rt5033_fled_module_init(void)
932 return platform_driver_register(&rt5033_fled_driver);
935 static void __exit rt5033_fled_module_exit(void)
937 platform_driver_unregister(&rt5033_fled_driver);
940 device_initcall(rt5033_fled_module_init);
941 module_exit(rt5033_fled_module_exit);
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");