1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus Lights protocol driver.
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
9 #include <linux/kernel.h>
10 #include <linux/leds.h>
11 #include <linux/led-class-flash.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/greybus.h>
15 #include <media/v4l2-flash-led-class.h>
28 struct attribute
**attrs
;
29 struct attribute_group
*attr_group
;
30 const struct attribute_group
**attr_groups
;
31 struct led_classdev
*led
;
32 struct led_classdev_flash fled
;
33 struct led_flash_setting intensity_uA
;
34 struct led_flash_setting timeout_us
;
35 struct gb_light
*light
;
46 struct gb_lights
*glights
;
49 struct gb_channel
*channels
;
52 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
53 struct v4l2_flash
*v4l2_flash
;
54 struct v4l2_flash
*v4l2_flash_ind
;
59 struct gb_connection
*connection
;
61 struct gb_light
*lights
;
62 struct mutex lights_lock
;
65 static void gb_lights_channel_free(struct gb_channel
*channel
);
67 static struct gb_connection
*get_conn_from_channel(struct gb_channel
*channel
)
69 return channel
->light
->glights
->connection
;
72 static struct gb_connection
*get_conn_from_light(struct gb_light
*light
)
74 return light
->glights
->connection
;
77 static bool is_channel_flash(struct gb_channel
*channel
)
79 return !!(channel
->mode
& (GB_CHANNEL_MODE_FLASH
| GB_CHANNEL_MODE_TORCH
80 | GB_CHANNEL_MODE_INDICATOR
));
83 static struct gb_channel
*get_channel_from_cdev(struct led_classdev
*cdev
)
85 struct led_classdev_flash
*fled_cdev
= lcdev_to_flcdev(cdev
);
87 return container_of(fled_cdev
, struct gb_channel
, fled
);
90 static struct led_classdev
*get_channel_cdev(struct gb_channel
*channel
)
92 return &channel
->fled
.led_cdev
;
95 static struct gb_channel
*get_channel_from_mode(struct gb_light
*light
,
98 struct gb_channel
*channel
;
101 for (i
= 0; i
< light
->channels_count
; i
++) {
102 channel
= &light
->channels
[i
];
103 if (channel
->mode
== mode
)
109 static int __gb_lights_flash_intensity_set(struct gb_channel
*channel
,
112 struct gb_connection
*connection
= get_conn_from_channel(channel
);
113 struct gb_bundle
*bundle
= connection
->bundle
;
114 struct gb_lights_set_flash_intensity_request req
;
117 if (channel
->releasing
)
120 ret
= gb_pm_runtime_get_sync(bundle
);
124 req
.light_id
= channel
->light
->id
;
125 req
.channel_id
= channel
->id
;
126 req
.intensity_uA
= cpu_to_le32(intensity
);
128 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_SET_FLASH_INTENSITY
,
129 &req
, sizeof(req
), NULL
, 0);
131 gb_pm_runtime_put_autosuspend(bundle
);
136 static int __gb_lights_flash_brightness_set(struct gb_channel
*channel
)
140 /* If the channel is flash we need to get the attached torch channel */
141 if (channel
->mode
& GB_CHANNEL_MODE_FLASH
)
142 channel
= get_channel_from_mode(channel
->light
,
143 GB_CHANNEL_MODE_TORCH
);
148 /* For not flash we need to convert brightness to intensity */
149 intensity
= channel
->intensity_uA
.min
+
150 (channel
->intensity_uA
.step
* channel
->led
->brightness
);
152 return __gb_lights_flash_intensity_set(channel
, intensity
);
155 static int gb_lights_color_set(struct gb_channel
*channel
, u32 color
);
156 static int gb_lights_fade_set(struct gb_channel
*channel
);
158 static void led_lock(struct led_classdev
*cdev
)
160 mutex_lock(&cdev
->led_access
);
163 static void led_unlock(struct led_classdev
*cdev
)
165 mutex_unlock(&cdev
->led_access
);
168 #define gb_lights_fade_attr(__dir) \
169 static ssize_t fade_##__dir##_show(struct device *dev, \
170 struct device_attribute *attr, \
173 struct led_classdev *cdev = dev_get_drvdata(dev); \
174 struct gb_channel *channel = get_channel_from_cdev(cdev); \
176 return sprintf(buf, "%u\n", channel->fade_##__dir); \
179 static ssize_t fade_##__dir##_store(struct device *dev, \
180 struct device_attribute *attr, \
181 const char *buf, size_t size) \
183 struct led_classdev *cdev = dev_get_drvdata(dev); \
184 struct gb_channel *channel = get_channel_from_cdev(cdev); \
189 if (led_sysfs_is_disabled(cdev)) { \
194 ret = kstrtou8(buf, 0, &fade); \
196 dev_err(dev, "could not parse fade value %d\n", ret); \
199 if (channel->fade_##__dir == fade) \
201 channel->fade_##__dir = fade; \
203 ret = gb_lights_fade_set(channel); \
212 static DEVICE_ATTR_RW(fade_##__dir)
214 gb_lights_fade_attr(in
);
215 gb_lights_fade_attr(out
);
217 static ssize_t
color_show(struct device
*dev
, struct device_attribute
*attr
,
220 struct led_classdev
*cdev
= dev_get_drvdata(dev
);
221 struct gb_channel
*channel
= get_channel_from_cdev(cdev
);
223 return sprintf(buf
, "0x%08x\n", channel
->color
);
226 static ssize_t
color_store(struct device
*dev
, struct device_attribute
*attr
,
227 const char *buf
, size_t size
)
229 struct led_classdev
*cdev
= dev_get_drvdata(dev
);
230 struct gb_channel
*channel
= get_channel_from_cdev(cdev
);
235 if (led_sysfs_is_disabled(cdev
)) {
239 ret
= kstrtou32(buf
, 0, &color
);
241 dev_err(dev
, "could not parse color value %d\n", ret
);
245 ret
= gb_lights_color_set(channel
, color
);
249 channel
->color
= color
;
255 static DEVICE_ATTR_RW(color
);
257 static int channel_attr_groups_set(struct gb_channel
*channel
,
258 struct led_classdev
*cdev
)
263 if (channel
->flags
& GB_LIGHT_CHANNEL_MULTICOLOR
)
265 if (channel
->flags
& GB_LIGHT_CHANNEL_FADER
)
271 /* Set attributes based in the channel flags */
272 channel
->attrs
= kcalloc(size
+ 1, sizeof(*channel
->attrs
), GFP_KERNEL
);
275 channel
->attr_group
= kzalloc(sizeof(*channel
->attr_group
), GFP_KERNEL
);
276 if (!channel
->attr_group
)
278 channel
->attr_groups
= kcalloc(2, sizeof(*channel
->attr_groups
),
280 if (!channel
->attr_groups
)
283 if (channel
->flags
& GB_LIGHT_CHANNEL_MULTICOLOR
)
284 channel
->attrs
[attr
++] = &dev_attr_color
.attr
;
285 if (channel
->flags
& GB_LIGHT_CHANNEL_FADER
) {
286 channel
->attrs
[attr
++] = &dev_attr_fade_in
.attr
;
287 channel
->attrs
[attr
++] = &dev_attr_fade_out
.attr
;
290 channel
->attr_group
->attrs
= channel
->attrs
;
292 channel
->attr_groups
[0] = channel
->attr_group
;
294 cdev
->groups
= channel
->attr_groups
;
299 static int gb_lights_fade_set(struct gb_channel
*channel
)
301 struct gb_connection
*connection
= get_conn_from_channel(channel
);
302 struct gb_bundle
*bundle
= connection
->bundle
;
303 struct gb_lights_set_fade_request req
;
306 if (channel
->releasing
)
309 ret
= gb_pm_runtime_get_sync(bundle
);
313 req
.light_id
= channel
->light
->id
;
314 req
.channel_id
= channel
->id
;
315 req
.fade_in
= channel
->fade_in
;
316 req
.fade_out
= channel
->fade_out
;
317 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_SET_FADE
,
318 &req
, sizeof(req
), NULL
, 0);
320 gb_pm_runtime_put_autosuspend(bundle
);
325 static int gb_lights_color_set(struct gb_channel
*channel
, u32 color
)
327 struct gb_connection
*connection
= get_conn_from_channel(channel
);
328 struct gb_bundle
*bundle
= connection
->bundle
;
329 struct gb_lights_set_color_request req
;
332 if (channel
->releasing
)
335 ret
= gb_pm_runtime_get_sync(bundle
);
339 req
.light_id
= channel
->light
->id
;
340 req
.channel_id
= channel
->id
;
341 req
.color
= cpu_to_le32(color
);
342 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_SET_COLOR
,
343 &req
, sizeof(req
), NULL
, 0);
345 gb_pm_runtime_put_autosuspend(bundle
);
350 static int __gb_lights_led_brightness_set(struct gb_channel
*channel
)
352 struct gb_lights_set_brightness_request req
;
353 struct gb_connection
*connection
= get_conn_from_channel(channel
);
354 struct gb_bundle
*bundle
= connection
->bundle
;
358 mutex_lock(&channel
->lock
);
359 ret
= gb_pm_runtime_get_sync(bundle
);
363 old_active
= channel
->active
;
365 req
.light_id
= channel
->light
->id
;
366 req
.channel_id
= channel
->id
;
367 req
.brightness
= (u8
)channel
->led
->brightness
;
369 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_SET_BRIGHTNESS
,
370 &req
, sizeof(req
), NULL
, 0);
374 if (channel
->led
->brightness
)
375 channel
->active
= true;
377 channel
->active
= false;
379 /* we need to keep module alive when turning to active state */
380 if (!old_active
&& channel
->active
)
384 * on the other hand if going to inactive we still hold a reference and
385 * need to put it, so we could go to suspend.
387 if (old_active
&& !channel
->active
)
388 gb_pm_runtime_put_autosuspend(bundle
);
391 gb_pm_runtime_put_autosuspend(bundle
);
393 mutex_unlock(&channel
->lock
);
398 static int __gb_lights_brightness_set(struct gb_channel
*channel
)
402 if (channel
->releasing
)
405 if (is_channel_flash(channel
))
406 ret
= __gb_lights_flash_brightness_set(channel
);
408 ret
= __gb_lights_led_brightness_set(channel
);
413 static int gb_brightness_set(struct led_classdev
*cdev
,
414 enum led_brightness value
)
416 struct gb_channel
*channel
= get_channel_from_cdev(cdev
);
418 channel
->led
->brightness
= value
;
420 return __gb_lights_brightness_set(channel
);
423 static enum led_brightness
gb_brightness_get(struct led_classdev
*cdev
)
426 struct gb_channel
*channel
= get_channel_from_cdev(cdev
);
428 return channel
->led
->brightness
;
431 static int gb_blink_set(struct led_classdev
*cdev
, unsigned long *delay_on
,
432 unsigned long *delay_off
)
434 struct gb_channel
*channel
= get_channel_from_cdev(cdev
);
435 struct gb_connection
*connection
= get_conn_from_channel(channel
);
436 struct gb_bundle
*bundle
= connection
->bundle
;
437 struct gb_lights_blink_request req
;
441 if (channel
->releasing
)
444 if (!delay_on
|| !delay_off
)
447 mutex_lock(&channel
->lock
);
448 ret
= gb_pm_runtime_get_sync(bundle
);
452 old_active
= channel
->active
;
454 req
.light_id
= channel
->light
->id
;
455 req
.channel_id
= channel
->id
;
456 req
.time_on_ms
= cpu_to_le16(*delay_on
);
457 req
.time_off_ms
= cpu_to_le16(*delay_off
);
459 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_SET_BLINK
, &req
,
460 sizeof(req
), NULL
, 0);
465 channel
->active
= true;
467 channel
->active
= false;
469 /* we need to keep module alive when turning to active state */
470 if (!old_active
&& channel
->active
)
474 * on the other hand if going to inactive we still hold a reference and
475 * need to put it, so we could go to suspend.
477 if (old_active
&& !channel
->active
)
478 gb_pm_runtime_put_autosuspend(bundle
);
481 gb_pm_runtime_put_autosuspend(bundle
);
483 mutex_unlock(&channel
->lock
);
488 static void gb_lights_led_operations_set(struct gb_channel
*channel
,
489 struct led_classdev
*cdev
)
491 cdev
->brightness_get
= gb_brightness_get
;
492 cdev
->brightness_set_blocking
= gb_brightness_set
;
494 if (channel
->flags
& GB_LIGHT_CHANNEL_BLINK
)
495 cdev
->blink_set
= gb_blink_set
;
498 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
499 /* V4L2 specific helpers */
500 static const struct v4l2_flash_ops v4l2_flash_ops
;
502 static void __gb_lights_channel_v4l2_config(struct led_flash_setting
*channel_s
,
503 struct led_flash_setting
*v4l2_s
)
505 v4l2_s
->min
= channel_s
->min
;
506 v4l2_s
->max
= channel_s
->max
;
507 v4l2_s
->step
= channel_s
->step
;
508 /* For v4l2 val is the default value */
509 v4l2_s
->val
= channel_s
->max
;
512 static int gb_lights_light_v4l2_register(struct gb_light
*light
)
514 struct gb_connection
*connection
= get_conn_from_light(light
);
515 struct device
*dev
= &connection
->bundle
->dev
;
516 struct v4l2_flash_config sd_cfg
= { {0} }, sd_cfg_ind
= { {0} };
517 struct led_classdev_flash
*fled
;
518 struct led_classdev
*iled
= NULL
;
519 struct gb_channel
*channel_torch
, *channel_ind
, *channel_flash
;
521 channel_torch
= get_channel_from_mode(light
, GB_CHANNEL_MODE_TORCH
);
523 __gb_lights_channel_v4l2_config(&channel_torch
->intensity_uA
,
526 channel_ind
= get_channel_from_mode(light
, GB_CHANNEL_MODE_INDICATOR
);
528 __gb_lights_channel_v4l2_config(&channel_ind
->intensity_uA
,
529 &sd_cfg_ind
.intensity
);
530 iled
= &channel_ind
->fled
.led_cdev
;
533 channel_flash
= get_channel_from_mode(light
, GB_CHANNEL_MODE_FLASH
);
534 if (!channel_flash
) {
535 dev_err(dev
, "failed to get flash channel from mode\n");
539 fled
= &channel_flash
->fled
;
541 snprintf(sd_cfg
.dev_name
, sizeof(sd_cfg
.dev_name
), "%s", light
->name
);
542 snprintf(sd_cfg_ind
.dev_name
, sizeof(sd_cfg_ind
.dev_name
),
543 "%s indicator", light
->name
);
545 /* Set the possible values to faults, in our case all faults */
546 sd_cfg
.flash_faults
= LED_FAULT_OVER_VOLTAGE
| LED_FAULT_TIMEOUT
|
547 LED_FAULT_OVER_TEMPERATURE
| LED_FAULT_SHORT_CIRCUIT
|
548 LED_FAULT_OVER_CURRENT
| LED_FAULT_INDICATOR
|
549 LED_FAULT_UNDER_VOLTAGE
| LED_FAULT_INPUT_VOLTAGE
|
550 LED_FAULT_LED_OVER_TEMPERATURE
;
552 light
->v4l2_flash
= v4l2_flash_init(dev
, NULL
, fled
, &v4l2_flash_ops
,
554 if (IS_ERR(light
->v4l2_flash
))
555 return PTR_ERR(light
->v4l2_flash
);
558 light
->v4l2_flash_ind
=
559 v4l2_flash_indicator_init(dev
, NULL
, iled
, &sd_cfg_ind
);
560 if (IS_ERR(light
->v4l2_flash_ind
)) {
561 v4l2_flash_release(light
->v4l2_flash
);
562 return PTR_ERR(light
->v4l2_flash_ind
);
569 static void gb_lights_light_v4l2_unregister(struct gb_light
*light
)
571 v4l2_flash_release(light
->v4l2_flash_ind
);
572 v4l2_flash_release(light
->v4l2_flash
);
575 static int gb_lights_light_v4l2_register(struct gb_light
*light
)
577 struct gb_connection
*connection
= get_conn_from_light(light
);
579 dev_err(&connection
->bundle
->dev
, "no support for v4l2 subdevices\n");
583 static void gb_lights_light_v4l2_unregister(struct gb_light
*light
)
588 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
589 /* Flash specific operations */
590 static int gb_lights_flash_intensity_set(struct led_classdev_flash
*fcdev
,
593 struct gb_channel
*channel
= container_of(fcdev
, struct gb_channel
,
597 ret
= __gb_lights_flash_intensity_set(channel
, brightness
);
601 fcdev
->brightness
.val
= brightness
;
606 static int gb_lights_flash_intensity_get(struct led_classdev_flash
*fcdev
,
609 *brightness
= fcdev
->brightness
.val
;
614 static int gb_lights_flash_strobe_set(struct led_classdev_flash
*fcdev
,
617 struct gb_channel
*channel
= container_of(fcdev
, struct gb_channel
,
619 struct gb_connection
*connection
= get_conn_from_channel(channel
);
620 struct gb_bundle
*bundle
= connection
->bundle
;
621 struct gb_lights_set_flash_strobe_request req
;
624 if (channel
->releasing
)
627 ret
= gb_pm_runtime_get_sync(bundle
);
631 req
.light_id
= channel
->light
->id
;
632 req
.channel_id
= channel
->id
;
633 req
.state
= state
? 1 : 0;
635 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_SET_FLASH_STROBE
,
636 &req
, sizeof(req
), NULL
, 0);
638 channel
->strobe_state
= state
;
640 gb_pm_runtime_put_autosuspend(bundle
);
645 static int gb_lights_flash_strobe_get(struct led_classdev_flash
*fcdev
,
648 struct gb_channel
*channel
= container_of(fcdev
, struct gb_channel
,
651 *state
= channel
->strobe_state
;
655 static int gb_lights_flash_timeout_set(struct led_classdev_flash
*fcdev
,
658 struct gb_channel
*channel
= container_of(fcdev
, struct gb_channel
,
660 struct gb_connection
*connection
= get_conn_from_channel(channel
);
661 struct gb_bundle
*bundle
= connection
->bundle
;
662 struct gb_lights_set_flash_timeout_request req
;
665 if (channel
->releasing
)
668 ret
= gb_pm_runtime_get_sync(bundle
);
672 req
.light_id
= channel
->light
->id
;
673 req
.channel_id
= channel
->id
;
674 req
.timeout_us
= cpu_to_le32(timeout
);
676 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT
,
677 &req
, sizeof(req
), NULL
, 0);
679 fcdev
->timeout
.val
= timeout
;
681 gb_pm_runtime_put_autosuspend(bundle
);
686 static int gb_lights_flash_fault_get(struct led_classdev_flash
*fcdev
,
689 struct gb_channel
*channel
= container_of(fcdev
, struct gb_channel
,
691 struct gb_connection
*connection
= get_conn_from_channel(channel
);
692 struct gb_bundle
*bundle
= connection
->bundle
;
693 struct gb_lights_get_flash_fault_request req
;
694 struct gb_lights_get_flash_fault_response resp
;
697 if (channel
->releasing
)
700 ret
= gb_pm_runtime_get_sync(bundle
);
704 req
.light_id
= channel
->light
->id
;
705 req
.channel_id
= channel
->id
;
707 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_GET_FLASH_FAULT
,
708 &req
, sizeof(req
), &resp
, sizeof(resp
));
710 *fault
= le32_to_cpu(resp
.fault
);
712 gb_pm_runtime_put_autosuspend(bundle
);
717 static const struct led_flash_ops gb_lights_flash_ops
= {
718 .flash_brightness_set
= gb_lights_flash_intensity_set
,
719 .flash_brightness_get
= gb_lights_flash_intensity_get
,
720 .strobe_set
= gb_lights_flash_strobe_set
,
721 .strobe_get
= gb_lights_flash_strobe_get
,
722 .timeout_set
= gb_lights_flash_timeout_set
,
723 .fault_get
= gb_lights_flash_fault_get
,
726 static int __gb_lights_channel_torch_attach(struct gb_channel
*channel
,
727 struct gb_channel
*channel_torch
)
731 /* we can only attach torch to a flash channel */
732 if (!(channel
->mode
& GB_CHANNEL_MODE_FLASH
))
735 /* Move torch brightness to the destination */
736 channel
->led
->max_brightness
= channel_torch
->led
->max_brightness
;
738 /* append mode name to flash name */
739 name
= kasprintf(GFP_KERNEL
, "%s_%s", channel
->led
->name
,
740 channel_torch
->mode_name
);
743 kfree(channel
->led
->name
);
744 channel
->led
->name
= name
;
746 channel_torch
->led
= channel
->led
;
751 static int __gb_lights_flash_led_register(struct gb_channel
*channel
)
753 struct gb_connection
*connection
= get_conn_from_channel(channel
);
754 struct led_classdev_flash
*fled
= &channel
->fled
;
755 struct led_flash_setting
*fset
;
756 struct gb_channel
*channel_torch
;
759 fled
->ops
= &gb_lights_flash_ops
;
761 fled
->led_cdev
.flags
|= LED_DEV_CAP_FLASH
;
763 fset
= &fled
->brightness
;
764 fset
->min
= channel
->intensity_uA
.min
;
765 fset
->max
= channel
->intensity_uA
.max
;
766 fset
->step
= channel
->intensity_uA
.step
;
767 fset
->val
= channel
->intensity_uA
.max
;
769 /* Only the flash mode have the timeout constraints settings */
770 if (channel
->mode
& GB_CHANNEL_MODE_FLASH
) {
771 fset
= &fled
->timeout
;
772 fset
->min
= channel
->timeout_us
.min
;
773 fset
->max
= channel
->timeout_us
.max
;
774 fset
->step
= channel
->timeout_us
.step
;
775 fset
->val
= channel
->timeout_us
.max
;
779 * If light have torch mode channel, this channel will be the led
780 * classdev of the registered above flash classdev
782 channel_torch
= get_channel_from_mode(channel
->light
,
783 GB_CHANNEL_MODE_TORCH
);
785 ret
= __gb_lights_channel_torch_attach(channel
, channel_torch
);
790 ret
= led_classdev_flash_register(&connection
->bundle
->dev
, fled
);
794 channel
->is_registered
= true;
801 static void __gb_lights_flash_led_unregister(struct gb_channel
*channel
)
803 if (!channel
->is_registered
)
806 led_classdev_flash_unregister(&channel
->fled
);
809 static int gb_lights_channel_flash_config(struct gb_channel
*channel
)
811 struct gb_connection
*connection
= get_conn_from_channel(channel
);
812 struct gb_lights_get_channel_flash_config_request req
;
813 struct gb_lights_get_channel_flash_config_response conf
;
814 struct led_flash_setting
*fset
;
817 req
.light_id
= channel
->light
->id
;
818 req
.channel_id
= channel
->id
;
820 ret
= gb_operation_sync(connection
,
821 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG
,
822 &req
, sizeof(req
), &conf
, sizeof(conf
));
827 * Intensity constraints for flash related modes: flash, torch,
828 * indicator. They will be needed for v4l2 registration.
830 fset
= &channel
->intensity_uA
;
831 fset
->min
= le32_to_cpu(conf
.intensity_min_uA
);
832 fset
->max
= le32_to_cpu(conf
.intensity_max_uA
);
833 fset
->step
= le32_to_cpu(conf
.intensity_step_uA
);
836 * On flash type, max brightness is set as the number of intensity steps
839 channel
->led
->max_brightness
= (fset
->max
- fset
->min
) / fset
->step
;
841 /* Only the flash mode have the timeout constraints settings */
842 if (channel
->mode
& GB_CHANNEL_MODE_FLASH
) {
843 fset
= &channel
->timeout_us
;
844 fset
->min
= le32_to_cpu(conf
.timeout_min_us
);
845 fset
->max
= le32_to_cpu(conf
.timeout_max_us
);
846 fset
->step
= le32_to_cpu(conf
.timeout_step_us
);
852 static int gb_lights_channel_flash_config(struct gb_channel
*channel
)
854 struct gb_connection
*connection
= get_conn_from_channel(channel
);
856 dev_err(&connection
->bundle
->dev
, "no support for flash devices\n");
860 static int __gb_lights_flash_led_register(struct gb_channel
*channel
)
865 static void __gb_lights_flash_led_unregister(struct gb_channel
*channel
)
871 static int __gb_lights_led_register(struct gb_channel
*channel
)
873 struct gb_connection
*connection
= get_conn_from_channel(channel
);
874 struct led_classdev
*cdev
= get_channel_cdev(channel
);
877 ret
= led_classdev_register(&connection
->bundle
->dev
, cdev
);
881 channel
->is_registered
= true;
885 static int gb_lights_channel_register(struct gb_channel
*channel
)
887 /* Normal LED channel, just register in led classdev and we are done */
888 if (!is_channel_flash(channel
))
889 return __gb_lights_led_register(channel
);
892 * Flash Type need more work, register flash classdev, indicator as
893 * flash classdev, torch will be led classdev of the flash classdev.
895 if (!(channel
->mode
& GB_CHANNEL_MODE_TORCH
))
896 return __gb_lights_flash_led_register(channel
);
901 static void __gb_lights_led_unregister(struct gb_channel
*channel
)
903 struct led_classdev
*cdev
= get_channel_cdev(channel
);
905 if (!channel
->is_registered
)
908 led_classdev_unregister(cdev
);
914 static void gb_lights_channel_unregister(struct gb_channel
*channel
)
916 /* The same as register, handle channels differently */
917 if (!is_channel_flash(channel
)) {
918 __gb_lights_led_unregister(channel
);
922 if (channel
->mode
& GB_CHANNEL_MODE_TORCH
)
923 __gb_lights_led_unregister(channel
);
925 __gb_lights_flash_led_unregister(channel
);
928 static int gb_lights_channel_config(struct gb_light
*light
,
929 struct gb_channel
*channel
)
931 struct gb_lights_get_channel_config_response conf
;
932 struct gb_lights_get_channel_config_request req
;
933 struct gb_connection
*connection
= get_conn_from_light(light
);
934 struct led_classdev
*cdev
= get_channel_cdev(channel
);
938 req
.light_id
= light
->id
;
939 req
.channel_id
= channel
->id
;
941 ret
= gb_operation_sync(connection
, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG
,
942 &req
, sizeof(req
), &conf
, sizeof(conf
));
946 channel
->light
= light
;
947 channel
->mode
= le32_to_cpu(conf
.mode
);
948 channel
->flags
= le32_to_cpu(conf
.flags
);
949 channel
->color
= le32_to_cpu(conf
.color
);
950 channel
->color_name
= kstrndup(conf
.color_name
, NAMES_MAX
, GFP_KERNEL
);
951 if (!channel
->color_name
)
953 channel
->mode_name
= kstrndup(conf
.mode_name
, NAMES_MAX
, GFP_KERNEL
);
954 if (!channel
->mode_name
)
959 name
= kasprintf(GFP_KERNEL
, "%s:%s:%s", light
->name
,
960 channel
->color_name
, channel
->mode_name
);
966 cdev
->max_brightness
= conf
.max_brightness
;
968 ret
= channel_attr_groups_set(channel
, cdev
);
972 gb_lights_led_operations_set(channel
, cdev
);
975 * If it is not a flash related channel (flash, torch or indicator) we
976 * are done here. If not, continue and fetch flash related
979 if (!is_channel_flash(channel
))
982 light
->has_flash
= true;
984 return gb_lights_channel_flash_config(channel
);
987 static int gb_lights_light_config(struct gb_lights
*glights
, u8 id
)
989 struct gb_light
*light
= &glights
->lights
[id
];
990 struct gb_lights_get_light_config_request req
;
991 struct gb_lights_get_light_config_response conf
;
995 light
->glights
= glights
;
1000 ret
= gb_operation_sync(glights
->connection
,
1001 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG
,
1002 &req
, sizeof(req
), &conf
, sizeof(conf
));
1006 if (!conf
.channel_count
)
1008 if (!strlen(conf
.name
))
1011 light
->channels_count
= conf
.channel_count
;
1012 light
->name
= kstrndup(conf
.name
, NAMES_MAX
, GFP_KERNEL
);
1015 light
->channels
= kcalloc(light
->channels_count
,
1016 sizeof(struct gb_channel
), GFP_KERNEL
);
1017 if (!light
->channels
)
1020 /* First we collect all the configurations for all channels */
1021 for (i
= 0; i
< light
->channels_count
; i
++) {
1022 light
->channels
[i
].id
= i
;
1023 ret
= gb_lights_channel_config(light
, &light
->channels
[i
]);
1031 static int gb_lights_light_register(struct gb_light
*light
)
1037 * Then, if everything went ok in getting configurations, we register
1038 * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1041 for (i
= 0; i
< light
->channels_count
; i
++) {
1042 ret
= gb_lights_channel_register(&light
->channels
[i
]);
1046 mutex_init(&light
->channels
[i
].lock
);
1049 light
->ready
= true;
1051 if (light
->has_flash
) {
1052 ret
= gb_lights_light_v4l2_register(light
);
1054 light
->has_flash
= false;
1062 static void gb_lights_channel_free(struct gb_channel
*channel
)
1064 kfree(channel
->attrs
);
1065 kfree(channel
->attr_group
);
1066 kfree(channel
->attr_groups
);
1067 kfree(channel
->color_name
);
1068 kfree(channel
->mode_name
);
1069 mutex_destroy(&channel
->lock
);
1072 static void gb_lights_channel_release(struct gb_channel
*channel
)
1074 channel
->releasing
= true;
1076 gb_lights_channel_unregister(channel
);
1078 gb_lights_channel_free(channel
);
1081 static void gb_lights_light_release(struct gb_light
*light
)
1085 light
->ready
= false;
1087 if (light
->has_flash
)
1088 gb_lights_light_v4l2_unregister(light
);
1089 light
->has_flash
= false;
1091 for (i
= 0; i
< light
->channels_count
; i
++)
1092 gb_lights_channel_release(&light
->channels
[i
]);
1093 light
->channels_count
= 0;
1095 kfree(light
->channels
);
1096 light
->channels
= NULL
;
1101 static void gb_lights_release(struct gb_lights
*glights
)
1108 mutex_lock(&glights
->lights_lock
);
1109 if (!glights
->lights
)
1112 for (i
= 0; i
< glights
->lights_count
; i
++)
1113 gb_lights_light_release(&glights
->lights
[i
]);
1115 kfree(glights
->lights
);
1118 mutex_unlock(&glights
->lights_lock
);
1119 mutex_destroy(&glights
->lights_lock
);
1123 static int gb_lights_get_count(struct gb_lights
*glights
)
1125 struct gb_lights_get_lights_response resp
;
1128 ret
= gb_operation_sync(glights
->connection
, GB_LIGHTS_TYPE_GET_LIGHTS
,
1129 NULL
, 0, &resp
, sizeof(resp
));
1133 if (!resp
.lights_count
)
1136 glights
->lights_count
= resp
.lights_count
;
1141 static int gb_lights_create_all(struct gb_lights
*glights
)
1143 struct gb_connection
*connection
= glights
->connection
;
1147 mutex_lock(&glights
->lights_lock
);
1148 ret
= gb_lights_get_count(glights
);
1152 glights
->lights
= kcalloc(glights
->lights_count
,
1153 sizeof(struct gb_light
), GFP_KERNEL
);
1154 if (!glights
->lights
) {
1159 for (i
= 0; i
< glights
->lights_count
; i
++) {
1160 ret
= gb_lights_light_config(glights
, i
);
1162 dev_err(&connection
->bundle
->dev
,
1163 "Fail to configure lights device\n");
1169 mutex_unlock(&glights
->lights_lock
);
1173 static int gb_lights_register_all(struct gb_lights
*glights
)
1175 struct gb_connection
*connection
= glights
->connection
;
1179 mutex_lock(&glights
->lights_lock
);
1180 for (i
= 0; i
< glights
->lights_count
; i
++) {
1181 ret
= gb_lights_light_register(&glights
->lights
[i
]);
1183 dev_err(&connection
->bundle
->dev
,
1184 "Fail to enable lights device\n");
1189 mutex_unlock(&glights
->lights_lock
);
1193 static int gb_lights_request_handler(struct gb_operation
*op
)
1195 struct gb_connection
*connection
= op
->connection
;
1196 struct device
*dev
= &connection
->bundle
->dev
;
1197 struct gb_lights
*glights
= gb_connection_get_data(connection
);
1198 struct gb_light
*light
;
1199 struct gb_message
*request
;
1200 struct gb_lights_event_request
*payload
;
1205 if (op
->type
!= GB_LIGHTS_TYPE_EVENT
) {
1206 dev_err(dev
, "Unsupported unsolicited event: %u\n", op
->type
);
1210 request
= op
->request
;
1212 if (request
->payload_size
< sizeof(*payload
)) {
1213 dev_err(dev
, "Wrong event size received (%zu < %zu)\n",
1214 request
->payload_size
, sizeof(*payload
));
1218 payload
= request
->payload
;
1219 light_id
= payload
->light_id
;
1221 if (light_id
>= glights
->lights_count
||
1222 !glights
->lights
[light_id
].ready
) {
1223 dev_err(dev
, "Event received for unconfigured light id: %d\n",
1228 event
= payload
->event
;
1230 if (event
& GB_LIGHTS_LIGHT_CONFIG
) {
1231 light
= &glights
->lights
[light_id
];
1233 mutex_lock(&glights
->lights_lock
);
1234 gb_lights_light_release(light
);
1235 ret
= gb_lights_light_config(glights
, light_id
);
1237 ret
= gb_lights_light_register(light
);
1239 gb_lights_light_release(light
);
1240 mutex_unlock(&glights
->lights_lock
);
1246 static int gb_lights_probe(struct gb_bundle
*bundle
,
1247 const struct greybus_bundle_id
*id
)
1249 struct greybus_descriptor_cport
*cport_desc
;
1250 struct gb_connection
*connection
;
1251 struct gb_lights
*glights
;
1254 if (bundle
->num_cports
!= 1)
1257 cport_desc
= &bundle
->cport_desc
[0];
1258 if (cport_desc
->protocol_id
!= GREYBUS_PROTOCOL_LIGHTS
)
1261 glights
= kzalloc(sizeof(*glights
), GFP_KERNEL
);
1265 mutex_init(&glights
->lights_lock
);
1267 connection
= gb_connection_create(bundle
, le16_to_cpu(cport_desc
->id
),
1268 gb_lights_request_handler
);
1269 if (IS_ERR(connection
)) {
1270 ret
= PTR_ERR(connection
);
1274 glights
->connection
= connection
;
1275 gb_connection_set_data(connection
, glights
);
1277 greybus_set_drvdata(bundle
, glights
);
1279 /* We aren't ready to receive an incoming request yet */
1280 ret
= gb_connection_enable_tx(connection
);
1282 goto error_connection_destroy
;
1285 * Setup all the lights devices over this connection, if anything goes
1286 * wrong tear down all lights
1288 ret
= gb_lights_create_all(glights
);
1290 goto error_connection_disable
;
1292 /* We are ready to receive an incoming request now, enable RX as well */
1293 ret
= gb_connection_enable(connection
);
1295 goto error_connection_disable
;
1297 /* Enable & register lights */
1298 ret
= gb_lights_register_all(glights
);
1300 goto error_connection_disable
;
1302 gb_pm_runtime_put_autosuspend(bundle
);
1306 error_connection_disable
:
1307 gb_connection_disable(connection
);
1308 error_connection_destroy
:
1309 gb_connection_destroy(connection
);
1311 gb_lights_release(glights
);
1315 static void gb_lights_disconnect(struct gb_bundle
*bundle
)
1317 struct gb_lights
*glights
= greybus_get_drvdata(bundle
);
1319 if (gb_pm_runtime_get_sync(bundle
))
1320 gb_pm_runtime_get_noresume(bundle
);
1322 gb_connection_disable(glights
->connection
);
1323 gb_connection_destroy(glights
->connection
);
1325 gb_lights_release(glights
);
1328 static const struct greybus_bundle_id gb_lights_id_table
[] = {
1329 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS
) },
1332 MODULE_DEVICE_TABLE(greybus
, gb_lights_id_table
);
1334 static struct greybus_driver gb_lights_driver
= {
1336 .probe
= gb_lights_probe
,
1337 .disconnect
= gb_lights_disconnect
,
1338 .id_table
= gb_lights_id_table
,
1340 module_greybus_driver(gb_lights_driver
);
1342 MODULE_DESCRIPTION("Greybus Lights protocol driver");
1343 MODULE_LICENSE("GPL v2");