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 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
33 struct led_classdev_flash fled;
34 struct led_flash_setting intensity_uA;
35 struct led_flash_setting timeout_us;
37 struct led_classdev cled;
39 struct gb_light *light;
50 struct gb_lights *glights;
53 struct gb_channel *channels;
56 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
57 struct v4l2_flash *v4l2_flash;
58 struct v4l2_flash *v4l2_flash_ind;
63 struct gb_connection *connection;
65 struct gb_light *lights;
66 struct mutex lights_lock;
69 static void gb_lights_channel_free(struct gb_channel *channel);
71 static struct gb_connection *get_conn_from_channel(struct gb_channel *channel)
73 return channel->light->glights->connection;
76 static struct gb_connection *get_conn_from_light(struct gb_light *light)
78 return light->glights->connection;
81 static bool is_channel_flash(struct gb_channel *channel)
83 return !!(channel->mode & (GB_CHANNEL_MODE_FLASH | GB_CHANNEL_MODE_TORCH
84 | GB_CHANNEL_MODE_INDICATOR));
87 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
88 static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
90 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
92 return container_of(fled_cdev, struct gb_channel, fled);
95 static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
97 return &channel->fled.led_cdev;
100 static struct gb_channel *get_channel_from_mode(struct gb_light *light,
103 struct gb_channel *channel = NULL;
106 for (i = 0; i < light->channels_count; i++) {
107 channel = &light->channels[i];
108 if (channel && channel->mode == mode)
114 static int __gb_lights_flash_intensity_set(struct gb_channel *channel,
117 struct gb_connection *connection = get_conn_from_channel(channel);
118 struct gb_bundle *bundle = connection->bundle;
119 struct gb_lights_set_flash_intensity_request req;
122 if (channel->releasing)
125 ret = gb_pm_runtime_get_sync(bundle);
129 req.light_id = channel->light->id;
130 req.channel_id = channel->id;
131 req.intensity_uA = cpu_to_le32(intensity);
133 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_INTENSITY,
134 &req, sizeof(req), NULL, 0);
136 gb_pm_runtime_put_autosuspend(bundle);
141 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
145 /* If the channel is flash we need to get the attached torch channel */
146 if (channel->mode & GB_CHANNEL_MODE_FLASH)
147 channel = get_channel_from_mode(channel->light,
148 GB_CHANNEL_MODE_TORCH);
150 /* For not flash we need to convert brightness to intensity */
151 intensity = channel->intensity_uA.min +
152 (channel->intensity_uA.step * channel->led->brightness);
154 return __gb_lights_flash_intensity_set(channel, intensity);
157 static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
159 return container_of(cdev, struct gb_channel, cled);
162 static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
164 return &channel->cled;
167 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
173 static int gb_lights_color_set(struct gb_channel *channel, u32 color);
174 static int gb_lights_fade_set(struct gb_channel *channel);
176 static void led_lock(struct led_classdev *cdev)
178 mutex_lock(&cdev->led_access);
181 static void led_unlock(struct led_classdev *cdev)
183 mutex_unlock(&cdev->led_access);
186 #define gb_lights_fade_attr(__dir) \
187 static ssize_t fade_##__dir##_show(struct device *dev, \
188 struct device_attribute *attr, \
191 struct led_classdev *cdev = dev_get_drvdata(dev); \
192 struct gb_channel *channel = get_channel_from_cdev(cdev); \
194 return sprintf(buf, "%u\n", channel->fade_##__dir); \
197 static ssize_t fade_##__dir##_store(struct device *dev, \
198 struct device_attribute *attr, \
199 const char *buf, size_t size) \
201 struct led_classdev *cdev = dev_get_drvdata(dev); \
202 struct gb_channel *channel = get_channel_from_cdev(cdev); \
207 if (led_sysfs_is_disabled(cdev)) { \
212 ret = kstrtou8(buf, 0, &fade); \
214 dev_err(dev, "could not parse fade value %d\n", ret); \
217 if (channel->fade_##__dir == fade) \
219 channel->fade_##__dir = fade; \
221 ret = gb_lights_fade_set(channel); \
230 static DEVICE_ATTR_RW(fade_##__dir)
232 gb_lights_fade_attr(in);
233 gb_lights_fade_attr(out);
235 static ssize_t color_show(struct device *dev, struct device_attribute *attr,
238 struct led_classdev *cdev = dev_get_drvdata(dev);
239 struct gb_channel *channel = get_channel_from_cdev(cdev);
241 return sprintf(buf, "0x%08x\n", channel->color);
244 static ssize_t color_store(struct device *dev, struct device_attribute *attr,
245 const char *buf, size_t size)
247 struct led_classdev *cdev = dev_get_drvdata(dev);
248 struct gb_channel *channel = get_channel_from_cdev(cdev);
253 if (led_sysfs_is_disabled(cdev)) {
257 ret = kstrtou32(buf, 0, &color);
259 dev_err(dev, "could not parse color value %d\n", ret);
263 ret = gb_lights_color_set(channel, color);
267 channel->color = color;
273 static DEVICE_ATTR_RW(color);
275 static int channel_attr_groups_set(struct gb_channel *channel,
276 struct led_classdev *cdev)
281 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
283 if (channel->flags & GB_LIGHT_CHANNEL_FADER)
289 /* Set attributes based in the channel flags */
290 channel->attrs = kcalloc(size + 1, sizeof(*channel->attrs), GFP_KERNEL);
293 channel->attr_group = kzalloc(sizeof(*channel->attr_group), GFP_KERNEL);
294 if (!channel->attr_group)
296 channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
298 if (!channel->attr_groups)
301 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
302 channel->attrs[attr++] = &dev_attr_color.attr;
303 if (channel->flags & GB_LIGHT_CHANNEL_FADER) {
304 channel->attrs[attr++] = &dev_attr_fade_in.attr;
305 channel->attrs[attr++] = &dev_attr_fade_out.attr;
308 channel->attr_group->attrs = channel->attrs;
310 channel->attr_groups[0] = channel->attr_group;
312 cdev->groups = channel->attr_groups;
317 static int gb_lights_fade_set(struct gb_channel *channel)
319 struct gb_connection *connection = get_conn_from_channel(channel);
320 struct gb_bundle *bundle = connection->bundle;
321 struct gb_lights_set_fade_request req;
324 if (channel->releasing)
327 ret = gb_pm_runtime_get_sync(bundle);
331 req.light_id = channel->light->id;
332 req.channel_id = channel->id;
333 req.fade_in = channel->fade_in;
334 req.fade_out = channel->fade_out;
335 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FADE,
336 &req, sizeof(req), NULL, 0);
338 gb_pm_runtime_put_autosuspend(bundle);
343 static int gb_lights_color_set(struct gb_channel *channel, u32 color)
345 struct gb_connection *connection = get_conn_from_channel(channel);
346 struct gb_bundle *bundle = connection->bundle;
347 struct gb_lights_set_color_request req;
350 if (channel->releasing)
353 ret = gb_pm_runtime_get_sync(bundle);
357 req.light_id = channel->light->id;
358 req.channel_id = channel->id;
359 req.color = cpu_to_le32(color);
360 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_COLOR,
361 &req, sizeof(req), NULL, 0);
363 gb_pm_runtime_put_autosuspend(bundle);
368 static int __gb_lights_led_brightness_set(struct gb_channel *channel)
370 struct gb_lights_set_brightness_request req;
371 struct gb_connection *connection = get_conn_from_channel(channel);
372 struct gb_bundle *bundle = connection->bundle;
376 mutex_lock(&channel->lock);
377 ret = gb_pm_runtime_get_sync(bundle);
381 old_active = channel->active;
383 req.light_id = channel->light->id;
384 req.channel_id = channel->id;
385 req.brightness = (u8)channel->led->brightness;
387 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
388 &req, sizeof(req), NULL, 0);
392 if (channel->led->brightness)
393 channel->active = true;
395 channel->active = false;
397 /* we need to keep module alive when turning to active state */
398 if (!old_active && channel->active)
402 * on the other hand if going to inactive we still hold a reference and
403 * need to put it, so we could go to suspend.
405 if (old_active && !channel->active)
406 gb_pm_runtime_put_autosuspend(bundle);
409 gb_pm_runtime_put_autosuspend(bundle);
411 mutex_unlock(&channel->lock);
416 static int __gb_lights_brightness_set(struct gb_channel *channel)
420 if (channel->releasing)
423 if (is_channel_flash(channel))
424 ret = __gb_lights_flash_brightness_set(channel);
426 ret = __gb_lights_led_brightness_set(channel);
431 static int gb_brightness_set(struct led_classdev *cdev,
432 enum led_brightness value)
434 struct gb_channel *channel = get_channel_from_cdev(cdev);
436 channel->led->brightness = value;
438 return __gb_lights_brightness_set(channel);
441 static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
444 struct gb_channel *channel = get_channel_from_cdev(cdev);
446 return channel->led->brightness;
449 static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
450 unsigned long *delay_off)
452 struct gb_channel *channel = get_channel_from_cdev(cdev);
453 struct gb_connection *connection = get_conn_from_channel(channel);
454 struct gb_bundle *bundle = connection->bundle;
455 struct gb_lights_blink_request req;
459 if (channel->releasing)
462 if (!delay_on || !delay_off)
465 mutex_lock(&channel->lock);
466 ret = gb_pm_runtime_get_sync(bundle);
470 old_active = channel->active;
472 req.light_id = channel->light->id;
473 req.channel_id = channel->id;
474 req.time_on_ms = cpu_to_le16(*delay_on);
475 req.time_off_ms = cpu_to_le16(*delay_off);
477 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
478 sizeof(req), NULL, 0);
483 channel->active = true;
485 channel->active = false;
487 /* we need to keep module alive when turning to active state */
488 if (!old_active && channel->active)
492 * on the other hand if going to inactive we still hold a reference and
493 * need to put it, so we could go to suspend.
495 if (old_active && !channel->active)
496 gb_pm_runtime_put_autosuspend(bundle);
499 gb_pm_runtime_put_autosuspend(bundle);
501 mutex_unlock(&channel->lock);
506 static void gb_lights_led_operations_set(struct gb_channel *channel,
507 struct led_classdev *cdev)
509 cdev->brightness_get = gb_brightness_get;
510 cdev->brightness_set_blocking = gb_brightness_set;
512 if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
513 cdev->blink_set = gb_blink_set;
516 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
517 /* V4L2 specific helpers */
518 static const struct v4l2_flash_ops v4l2_flash_ops;
520 static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
521 struct led_flash_setting *v4l2_s)
523 v4l2_s->min = channel_s->min;
524 v4l2_s->max = channel_s->max;
525 v4l2_s->step = channel_s->step;
526 /* For v4l2 val is the default value */
527 v4l2_s->val = channel_s->max;
530 static int gb_lights_light_v4l2_register(struct gb_light *light)
532 struct gb_connection *connection = get_conn_from_light(light);
533 struct device *dev = &connection->bundle->dev;
534 struct v4l2_flash_config sd_cfg = { {0} }, sd_cfg_ind = { {0} };
535 struct led_classdev_flash *fled;
536 struct led_classdev *iled = NULL;
537 struct gb_channel *channel_torch, *channel_ind, *channel_flash;
539 channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
541 __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
544 channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
546 __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
547 &sd_cfg_ind.intensity);
548 iled = &channel_ind->fled.led_cdev;
551 channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
552 WARN_ON(!channel_flash);
554 fled = &channel_flash->fled;
556 snprintf(sd_cfg.dev_name, sizeof(sd_cfg.dev_name), "%s", light->name);
557 snprintf(sd_cfg_ind.dev_name, sizeof(sd_cfg_ind.dev_name),
558 "%s indicator", light->name);
560 /* Set the possible values to faults, in our case all faults */
561 sd_cfg.flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
562 LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
563 LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
564 LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
565 LED_FAULT_LED_OVER_TEMPERATURE;
567 light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, &v4l2_flash_ops,
569 if (IS_ERR(light->v4l2_flash))
570 return PTR_ERR(light->v4l2_flash);
573 light->v4l2_flash_ind =
574 v4l2_flash_indicator_init(dev, NULL, iled, &sd_cfg_ind);
575 if (IS_ERR(light->v4l2_flash_ind)) {
576 v4l2_flash_release(light->v4l2_flash);
577 return PTR_ERR(light->v4l2_flash_ind);
584 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
586 v4l2_flash_release(light->v4l2_flash_ind);
587 v4l2_flash_release(light->v4l2_flash);
590 static int gb_lights_light_v4l2_register(struct gb_light *light)
592 struct gb_connection *connection = get_conn_from_light(light);
594 dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
598 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
603 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
604 /* Flash specific operations */
605 static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
608 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
612 ret = __gb_lights_flash_intensity_set(channel, brightness);
616 fcdev->brightness.val = brightness;
621 static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
624 *brightness = fcdev->brightness.val;
629 static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
632 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
634 struct gb_connection *connection = get_conn_from_channel(channel);
635 struct gb_bundle *bundle = connection->bundle;
636 struct gb_lights_set_flash_strobe_request req;
639 if (channel->releasing)
642 ret = gb_pm_runtime_get_sync(bundle);
646 req.light_id = channel->light->id;
647 req.channel_id = channel->id;
648 req.state = state ? 1 : 0;
650 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
651 &req, sizeof(req), NULL, 0);
653 channel->strobe_state = state;
655 gb_pm_runtime_put_autosuspend(bundle);
660 static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
663 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
666 *state = channel->strobe_state;
670 static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
673 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
675 struct gb_connection *connection = get_conn_from_channel(channel);
676 struct gb_bundle *bundle = connection->bundle;
677 struct gb_lights_set_flash_timeout_request req;
680 if (channel->releasing)
683 ret = gb_pm_runtime_get_sync(bundle);
687 req.light_id = channel->light->id;
688 req.channel_id = channel->id;
689 req.timeout_us = cpu_to_le32(timeout);
691 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
692 &req, sizeof(req), NULL, 0);
694 fcdev->timeout.val = timeout;
696 gb_pm_runtime_put_autosuspend(bundle);
701 static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
704 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
706 struct gb_connection *connection = get_conn_from_channel(channel);
707 struct gb_bundle *bundle = connection->bundle;
708 struct gb_lights_get_flash_fault_request req;
709 struct gb_lights_get_flash_fault_response resp;
712 if (channel->releasing)
715 ret = gb_pm_runtime_get_sync(bundle);
719 req.light_id = channel->light->id;
720 req.channel_id = channel->id;
722 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
723 &req, sizeof(req), &resp, sizeof(resp));
725 *fault = le32_to_cpu(resp.fault);
727 gb_pm_runtime_put_autosuspend(bundle);
732 static const struct led_flash_ops gb_lights_flash_ops = {
733 .flash_brightness_set = gb_lights_flash_intensity_set,
734 .flash_brightness_get = gb_lights_flash_intensity_get,
735 .strobe_set = gb_lights_flash_strobe_set,
736 .strobe_get = gb_lights_flash_strobe_get,
737 .timeout_set = gb_lights_flash_timeout_set,
738 .fault_get = gb_lights_flash_fault_get,
741 static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
742 struct gb_channel *channel_torch)
746 /* we can only attach torch to a flash channel */
747 if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
750 /* Move torch brightness to the destination */
751 channel->led->max_brightness = channel_torch->led->max_brightness;
753 /* append mode name to flash name */
754 name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
755 channel_torch->mode_name);
758 kfree(channel->led->name);
759 channel->led->name = name;
761 channel_torch->led = channel->led;
766 static int __gb_lights_flash_led_register(struct gb_channel *channel)
768 struct gb_connection *connection = get_conn_from_channel(channel);
769 struct led_classdev_flash *fled = &channel->fled;
770 struct led_flash_setting *fset;
771 struct gb_channel *channel_torch;
774 fled->ops = &gb_lights_flash_ops;
776 fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
778 fset = &fled->brightness;
779 fset->min = channel->intensity_uA.min;
780 fset->max = channel->intensity_uA.max;
781 fset->step = channel->intensity_uA.step;
782 fset->val = channel->intensity_uA.max;
784 /* Only the flash mode have the timeout constraints settings */
785 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
786 fset = &fled->timeout;
787 fset->min = channel->timeout_us.min;
788 fset->max = channel->timeout_us.max;
789 fset->step = channel->timeout_us.step;
790 fset->val = channel->timeout_us.max;
794 * If light have torch mode channel, this channel will be the led
795 * classdev of the registered above flash classdev
797 channel_torch = get_channel_from_mode(channel->light,
798 GB_CHANNEL_MODE_TORCH);
800 ret = __gb_lights_channel_torch_attach(channel, channel_torch);
805 ret = led_classdev_flash_register(&connection->bundle->dev, fled);
809 channel->is_registered = true;
816 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
818 if (!channel->is_registered)
821 led_classdev_flash_unregister(&channel->fled);
824 static int gb_lights_channel_flash_config(struct gb_channel *channel)
826 struct gb_connection *connection = get_conn_from_channel(channel);
827 struct gb_lights_get_channel_flash_config_request req;
828 struct gb_lights_get_channel_flash_config_response conf;
829 struct led_flash_setting *fset;
832 req.light_id = channel->light->id;
833 req.channel_id = channel->id;
835 ret = gb_operation_sync(connection,
836 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
837 &req, sizeof(req), &conf, sizeof(conf));
842 * Intensity constraints for flash related modes: flash, torch,
843 * indicator. They will be needed for v4l2 registration.
845 fset = &channel->intensity_uA;
846 fset->min = le32_to_cpu(conf.intensity_min_uA);
847 fset->max = le32_to_cpu(conf.intensity_max_uA);
848 fset->step = le32_to_cpu(conf.intensity_step_uA);
851 * On flash type, max brightness is set as the number of intensity steps
854 channel->led->max_brightness = (fset->max - fset->min) / fset->step;
856 /* Only the flash mode have the timeout constraints settings */
857 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
858 fset = &channel->timeout_us;
859 fset->min = le32_to_cpu(conf.timeout_min_us);
860 fset->max = le32_to_cpu(conf.timeout_max_us);
861 fset->step = le32_to_cpu(conf.timeout_step_us);
867 static int gb_lights_channel_flash_config(struct gb_channel *channel)
869 struct gb_connection *connection = get_conn_from_channel(channel);
871 dev_err(&connection->bundle->dev, "no support for flash devices\n");
875 static int __gb_lights_flash_led_register(struct gb_channel *channel)
880 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
886 static int __gb_lights_led_register(struct gb_channel *channel)
888 struct gb_connection *connection = get_conn_from_channel(channel);
889 struct led_classdev *cdev = get_channel_cdev(channel);
892 ret = led_classdev_register(&connection->bundle->dev, cdev);
896 channel->is_registered = true;
900 static int gb_lights_channel_register(struct gb_channel *channel)
902 /* Normal LED channel, just register in led classdev and we are done */
903 if (!is_channel_flash(channel))
904 return __gb_lights_led_register(channel);
907 * Flash Type need more work, register flash classdev, indicator as
908 * flash classdev, torch will be led classdev of the flash classdev.
910 if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
911 return __gb_lights_flash_led_register(channel);
916 static void __gb_lights_led_unregister(struct gb_channel *channel)
918 struct led_classdev *cdev = get_channel_cdev(channel);
920 if (!channel->is_registered)
923 led_classdev_unregister(cdev);
929 static void gb_lights_channel_unregister(struct gb_channel *channel)
931 /* The same as register, handle channels differently */
932 if (!is_channel_flash(channel)) {
933 __gb_lights_led_unregister(channel);
937 if (channel->mode & GB_CHANNEL_MODE_TORCH)
938 __gb_lights_led_unregister(channel);
940 __gb_lights_flash_led_unregister(channel);
943 static int gb_lights_channel_config(struct gb_light *light,
944 struct gb_channel *channel)
946 struct gb_lights_get_channel_config_response conf;
947 struct gb_lights_get_channel_config_request req;
948 struct gb_connection *connection = get_conn_from_light(light);
949 struct led_classdev *cdev = get_channel_cdev(channel);
953 req.light_id = light->id;
954 req.channel_id = channel->id;
956 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
957 &req, sizeof(req), &conf, sizeof(conf));
961 channel->light = light;
962 channel->mode = le32_to_cpu(conf.mode);
963 channel->flags = le32_to_cpu(conf.flags);
964 channel->color = le32_to_cpu(conf.color);
965 channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
966 if (!channel->color_name)
968 channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
969 if (!channel->mode_name)
974 name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
975 channel->color_name, channel->mode_name);
981 cdev->max_brightness = conf.max_brightness;
983 ret = channel_attr_groups_set(channel, cdev);
987 gb_lights_led_operations_set(channel, cdev);
990 * If it is not a flash related channel (flash, torch or indicator) we
991 * are done here. If not, continue and fetch flash related
994 if (!is_channel_flash(channel))
997 light->has_flash = true;
999 return gb_lights_channel_flash_config(channel);
1002 static int gb_lights_light_config(struct gb_lights *glights, u8 id)
1004 struct gb_light *light = &glights->lights[id];
1005 struct gb_lights_get_light_config_request req;
1006 struct gb_lights_get_light_config_response conf;
1010 light->glights = glights;
1015 ret = gb_operation_sync(glights->connection,
1016 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
1017 &req, sizeof(req), &conf, sizeof(conf));
1021 if (!conf.channel_count)
1023 if (!strlen(conf.name))
1026 light->channels_count = conf.channel_count;
1027 light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1030 light->channels = kcalloc(light->channels_count,
1031 sizeof(struct gb_channel), GFP_KERNEL);
1032 if (!light->channels)
1035 /* First we collect all the configurations for all channels */
1036 for (i = 0; i < light->channels_count; i++) {
1037 light->channels[i].id = i;
1038 ret = gb_lights_channel_config(light, &light->channels[i]);
1046 static int gb_lights_light_register(struct gb_light *light)
1052 * Then, if everything went ok in getting configurations, we register
1053 * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1056 for (i = 0; i < light->channels_count; i++) {
1057 ret = gb_lights_channel_register(&light->channels[i]);
1061 mutex_init(&light->channels[i].lock);
1064 light->ready = true;
1066 if (light->has_flash) {
1067 ret = gb_lights_light_v4l2_register(light);
1069 light->has_flash = false;
1077 static void gb_lights_channel_free(struct gb_channel *channel)
1079 kfree(channel->attrs);
1080 kfree(channel->attr_group);
1081 kfree(channel->attr_groups);
1082 kfree(channel->color_name);
1083 kfree(channel->mode_name);
1084 mutex_destroy(&channel->lock);
1087 static void gb_lights_channel_release(struct gb_channel *channel)
1089 channel->releasing = true;
1091 gb_lights_channel_unregister(channel);
1093 gb_lights_channel_free(channel);
1096 static void gb_lights_light_release(struct gb_light *light)
1100 light->ready = false;
1102 if (light->has_flash)
1103 gb_lights_light_v4l2_unregister(light);
1104 light->has_flash = false;
1106 for (i = 0; i < light->channels_count; i++)
1107 gb_lights_channel_release(&light->channels[i]);
1108 light->channels_count = 0;
1110 kfree(light->channels);
1111 light->channels = NULL;
1116 static void gb_lights_release(struct gb_lights *glights)
1123 mutex_lock(&glights->lights_lock);
1124 if (!glights->lights)
1127 for (i = 0; i < glights->lights_count; i++)
1128 gb_lights_light_release(&glights->lights[i]);
1130 kfree(glights->lights);
1133 mutex_unlock(&glights->lights_lock);
1134 mutex_destroy(&glights->lights_lock);
1138 static int gb_lights_get_count(struct gb_lights *glights)
1140 struct gb_lights_get_lights_response resp;
1143 ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1144 NULL, 0, &resp, sizeof(resp));
1148 if (!resp.lights_count)
1151 glights->lights_count = resp.lights_count;
1156 static int gb_lights_create_all(struct gb_lights *glights)
1158 struct gb_connection *connection = glights->connection;
1162 mutex_lock(&glights->lights_lock);
1163 ret = gb_lights_get_count(glights);
1167 glights->lights = kcalloc(glights->lights_count,
1168 sizeof(struct gb_light), GFP_KERNEL);
1169 if (!glights->lights) {
1174 for (i = 0; i < glights->lights_count; i++) {
1175 ret = gb_lights_light_config(glights, i);
1177 dev_err(&connection->bundle->dev,
1178 "Fail to configure lights device\n");
1184 mutex_unlock(&glights->lights_lock);
1188 static int gb_lights_register_all(struct gb_lights *glights)
1190 struct gb_connection *connection = glights->connection;
1194 mutex_lock(&glights->lights_lock);
1195 for (i = 0; i < glights->lights_count; i++) {
1196 ret = gb_lights_light_register(&glights->lights[i]);
1198 dev_err(&connection->bundle->dev,
1199 "Fail to enable lights device\n");
1204 mutex_unlock(&glights->lights_lock);
1208 static int gb_lights_request_handler(struct gb_operation *op)
1210 struct gb_connection *connection = op->connection;
1211 struct device *dev = &connection->bundle->dev;
1212 struct gb_lights *glights = gb_connection_get_data(connection);
1213 struct gb_light *light;
1214 struct gb_message *request;
1215 struct gb_lights_event_request *payload;
1220 if (op->type != GB_LIGHTS_TYPE_EVENT) {
1221 dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1225 request = op->request;
1227 if (request->payload_size < sizeof(*payload)) {
1228 dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1229 request->payload_size, sizeof(*payload));
1233 payload = request->payload;
1234 light_id = payload->light_id;
1236 if (light_id >= glights->lights_count ||
1237 !glights->lights[light_id].ready) {
1238 dev_err(dev, "Event received for unconfigured light id: %d\n",
1243 event = payload->event;
1245 if (event & GB_LIGHTS_LIGHT_CONFIG) {
1246 light = &glights->lights[light_id];
1248 mutex_lock(&glights->lights_lock);
1249 gb_lights_light_release(light);
1250 ret = gb_lights_light_config(glights, light_id);
1252 ret = gb_lights_light_register(light);
1254 gb_lights_light_release(light);
1255 mutex_unlock(&glights->lights_lock);
1261 static int gb_lights_probe(struct gb_bundle *bundle,
1262 const struct greybus_bundle_id *id)
1264 struct greybus_descriptor_cport *cport_desc;
1265 struct gb_connection *connection;
1266 struct gb_lights *glights;
1269 if (bundle->num_cports != 1)
1272 cport_desc = &bundle->cport_desc[0];
1273 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1276 glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1280 mutex_init(&glights->lights_lock);
1282 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1283 gb_lights_request_handler);
1284 if (IS_ERR(connection)) {
1285 ret = PTR_ERR(connection);
1289 glights->connection = connection;
1290 gb_connection_set_data(connection, glights);
1292 greybus_set_drvdata(bundle, glights);
1294 /* We aren't ready to receive an incoming request yet */
1295 ret = gb_connection_enable_tx(connection);
1297 goto error_connection_destroy;
1300 * Setup all the lights devices over this connection, if anything goes
1301 * wrong tear down all lights
1303 ret = gb_lights_create_all(glights);
1305 goto error_connection_disable;
1307 /* We are ready to receive an incoming request now, enable RX as well */
1308 ret = gb_connection_enable(connection);
1310 goto error_connection_disable;
1312 /* Enable & register lights */
1313 ret = gb_lights_register_all(glights);
1315 goto error_connection_disable;
1317 gb_pm_runtime_put_autosuspend(bundle);
1321 error_connection_disable:
1322 gb_connection_disable(connection);
1323 error_connection_destroy:
1324 gb_connection_destroy(connection);
1326 gb_lights_release(glights);
1330 static void gb_lights_disconnect(struct gb_bundle *bundle)
1332 struct gb_lights *glights = greybus_get_drvdata(bundle);
1334 if (gb_pm_runtime_get_sync(bundle))
1335 gb_pm_runtime_get_noresume(bundle);
1337 gb_connection_disable(glights->connection);
1338 gb_connection_destroy(glights->connection);
1340 gb_lights_release(glights);
1343 static const struct greybus_bundle_id gb_lights_id_table[] = {
1344 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1347 MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1349 static struct greybus_driver gb_lights_driver = {
1351 .probe = gb_lights_probe,
1352 .disconnect = gb_lights_disconnect,
1353 .id_table = gb_lights_id_table,
1355 module_greybus_driver(gb_lights_driver);
1357 MODULE_LICENSE("GPL v2");