14 #include <linux/module.h>
20 #include <linux/slab.h>
26 #define LM3533_LVCTRLBANK_MIN 2
27 #define LM3533_LVCTRLBANK_MAX 5
28 #define LM3533_LVCTRLBANK_COUNT 4
29 #define LM3533_RISEFALLTIME_MAX 7
30 #define LM3533_ALS_CHANNEL_LV_MIN 1
31 #define LM3533_ALS_CHANNEL_LV_MAX 2
33 #define LM3533_REG_CTRLBANK_BCONF_BASE 0x1b
34 #define LM3533_REG_PATTERN_ENABLE 0x28
35 #define LM3533_REG_PATTERN_LOW_TIME_BASE 0x71
36 #define LM3533_REG_PATTERN_HIGH_TIME_BASE 0x72
37 #define LM3533_REG_PATTERN_RISETIME_BASE 0x74
38 #define LM3533_REG_PATTERN_FALLTIME_BASE 0x75
40 #define LM3533_REG_PATTERN_STEP 0x10
42 #define LM3533_REG_CTRLBANK_BCONF_MAPPING_MASK 0x04
43 #define LM3533_REG_CTRLBANK_BCONF_ALS_EN_MASK 0x02
44 #define LM3533_REG_CTRLBANK_BCONF_ALS_CHANNEL_MASK 0x01
46 #define LM3533_LED_FLAG_PATTERN_ENABLE 1
68 static inline int lm3533_led_get_ctrlbank_id(
struct lm3533_led *
led)
75 return base + led->
id;
97 dev_dbg(led->
cdev.dev,
"%s - %d\n", __func__, enable);
102 if ((enable && state) || (!enable && !state))
105 pattern = lm3533_led_get_pattern(led);
115 dev_err(led->
cdev.dev,
"failed to enable pattern %d (%d)\n",
134 lm3533_led_pattern_enable(led, 0);
144 dev_dbg(led->
cdev.dev,
"%s - %d\n", __func__, value);
160 dev_dbg(led->
cdev.dev,
"%s - %u\n", __func__, val);
166 #define LM3533_LED_DELAY1_VMIN 0x00
167 #define LM3533_LED_DELAY2_VMIN 0x3d
168 #define LM3533_LED_DELAY3_VMIN 0x80
170 #define LM3533_LED_DELAY1_VMAX (LM3533_LED_DELAY2_VMIN - 1)
171 #define LM3533_LED_DELAY2_VMAX (LM3533_LED_DELAY3_VMIN - 1)
172 #define LM3533_LED_DELAY3_VMAX 0xff
174 #define LM3533_LED_DELAY1_TMIN 16384U
175 #define LM3533_LED_DELAY2_TMIN 1130496U
176 #define LM3533_LED_DELAY3_TMIN 10305536U
178 #define LM3533_LED_DELAY1_TMAX 999424U
179 #define LM3533_LED_DELAY2_TMAX 9781248U
180 #define LM3533_LED_DELAY3_TMAX 76890112U
183 #define LM3533_LED_DELAY1_TSTEP 16384
184 #define LM3533_LED_DELAY2_TSTEP 131072
185 #define LM3533_LED_DELAY3_TSTEP 524288
188 #define LM3533_LED_DELAY_ON_MAX \
189 ((LM3533_LED_DELAY2_TMAX + LM3533_LED_DELAY2_TSTEP / 2) / 1000)
190 #define LM3533_LED_DELAY_OFF_MAX \
191 ((LM3533_LED_DELAY3_TMAX + LM3533_LED_DELAY3_TSTEP / 2) / 1000)
201 static u8 time_to_val(
unsigned *
t,
unsigned t_min,
unsigned t_step,
206 val = (*t + t_step / 2 - t_min) / t_step + v_min;
208 *t = t_step * (val - v_min) + t_min;
226 static u8 lm3533_led_get_hw_delay(
unsigned *
delay)
253 *delay = (t + 500) / 1000;
263 unsigned long *delay)
270 t = (unsigned)*delay;
276 val = lm3533_led_get_hw_delay(&t);
278 dev_dbg(led->
cdev.dev,
"%s - %lu: %u (0x%02x)\n", __func__,
280 reg = lm3533_led_get_pattern_reg(led, base);
283 dev_err(led->
cdev.dev,
"failed to set delay (%02x)\n", reg);
290 static int lm3533_led_delay_on_set(
struct lm3533_led *led,
unsigned long *t)
295 static int lm3533_led_delay_off_set(
struct lm3533_led *led,
unsigned long *t)
300 static int lm3533_led_blink_set(
struct led_classdev *cdev,
301 unsigned long *delay_on,
302 unsigned long *delay_off)
307 dev_dbg(led->
cdev.dev,
"%s - on = %lu, off = %lu\n", __func__,
308 *delay_on, *delay_off);
314 if (*delay_on == 0 && *delay_off == 0) {
319 ret = lm3533_led_delay_on_set(led, delay_on);
323 ret = lm3533_led_delay_off_set(led, delay_off);
327 return lm3533_led_pattern_enable(led, 1);
334 struct lm3533_led *led = to_lm3533_led(led_cdev);
356 struct lm3533_led *led = to_lm3533_led(led_cdev);
361 reg = lm3533_led_get_pattern_reg(led, base);
372 return show_risefalltime(dev, attr, buf,
379 return show_risefalltime(dev, attr, buf,
385 const char *buf,
size_t len,
u8 base)
388 struct lm3533_led *led = to_lm3533_led(led_cdev);
396 reg = lm3533_led_get_pattern_reg(led, base);
406 const char *buf,
size_t len)
408 return store_risefalltime(dev, attr, buf, len,
414 const char *buf,
size_t len)
416 return store_risefalltime(dev, attr, buf, len,
424 struct lm3533_led *led = to_lm3533_led(led_cdev);
442 const char *buf,
size_t len)
445 struct lm3533_led *led = to_lm3533_led(led_cdev);
474 struct lm3533_led *led = to_lm3533_led(led_cdev);
492 const char *buf,
size_t len)
495 struct lm3533_led *led = to_lm3533_led(led_cdev);
524 struct lm3533_led *led = to_lm3533_led(led_cdev);
545 const char *buf,
size_t len)
548 struct lm3533_led *led = to_lm3533_led(led_cdev);
555 if (kstrtoul(buf, 0, &linear))
578 struct lm3533_led *led = to_lm3533_led(led_cdev);
591 const char *buf,
size_t len)
594 struct lm3533_led *led = to_lm3533_led(led_cdev);
616 static struct attribute *lm3533_led_attributes[] = {
617 &dev_attr_als_channel.attr,
618 &dev_attr_als_en.attr,
619 &dev_attr_falltime.attr,
621 &dev_attr_linear.attr,
623 &dev_attr_risetime.attr,
632 struct lm3533_led *led = to_lm3533_led(led_cdev);
635 if (attr == &dev_attr_als_channel.attr ||
636 attr == &dev_attr_als_en.attr) {
637 if (!led->
lm3533->have_als)
645 .is_visible = lm3533_led_attr_is_visible,
646 .attrs = lm3533_led_attributes
674 pdata = pdev->
dev.platform_data;
692 led->
cdev.brightness_set = lm3533_led_set;
693 led->
cdev.brightness_get = lm3533_led_get;
694 led->
cdev.blink_set = lm3533_led_blink_set;
706 led->
cb.id = lm3533_led_get_ctrlbank_id(led);
707 led->
cb.dev = lm3533->
dev;
709 platform_set_drvdata(pdev, led);
713 dev_err(&pdev->
dev,
"failed to register LED %d\n", pdev->
id);
717 led->
cb.dev = led->
cdev.dev;
720 &lm3533_led_attribute_group);
722 dev_err(&pdev->
dev,
"failed to create sysfs attributes\n");
726 ret = lm3533_led_setup(led, pdata);
728 goto err_sysfs_remove;
732 goto err_sysfs_remove;
747 struct lm3533_led *led = platform_get_drvdata(pdev);
762 struct lm3533_led *led = platform_get_drvdata(pdev);
773 .name =
"lm3533-leds",
776 .probe = lm3533_led_probe,
778 .shutdown = lm3533_led_shutdown,