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 = kcalloc(1, sizeof(*channel->attr_group),
295 if (!channel->attr_group)
297 channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
299 if (!channel->attr_groups)
302 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
303 channel->attrs[attr++] = &dev_attr_color.attr;
304 if (channel->flags & GB_LIGHT_CHANNEL_FADER) {
305 channel->attrs[attr++] = &dev_attr_fade_in.attr;
306 channel->attrs[attr++] = &dev_attr_fade_out.attr;
309 channel->attr_group->attrs = channel->attrs;
311 channel->attr_groups[0] = channel->attr_group;
313 cdev->groups = channel->attr_groups;
318 static int gb_lights_fade_set(struct gb_channel *channel)
320 struct gb_connection *connection = get_conn_from_channel(channel);
321 struct gb_bundle *bundle = connection->bundle;
322 struct gb_lights_set_fade_request req;
325 if (channel->releasing)
328 ret = gb_pm_runtime_get_sync(bundle);
332 req.light_id = channel->light->id;
333 req.channel_id = channel->id;
334 req.fade_in = channel->fade_in;
335 req.fade_out = channel->fade_out;
336 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FADE,
337 &req, sizeof(req), NULL, 0);
339 gb_pm_runtime_put_autosuspend(bundle);
344 static int gb_lights_color_set(struct gb_channel *channel, u32 color)
346 struct gb_connection *connection = get_conn_from_channel(channel);
347 struct gb_bundle *bundle = connection->bundle;
348 struct gb_lights_set_color_request req;
351 if (channel->releasing)
354 ret = gb_pm_runtime_get_sync(bundle);
358 req.light_id = channel->light->id;
359 req.channel_id = channel->id;
360 req.color = cpu_to_le32(color);
361 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_COLOR,
362 &req, sizeof(req), NULL, 0);
364 gb_pm_runtime_put_autosuspend(bundle);
369 static int __gb_lights_led_brightness_set(struct gb_channel *channel)
371 struct gb_lights_set_brightness_request req;
372 struct gb_connection *connection = get_conn_from_channel(channel);
373 struct gb_bundle *bundle = connection->bundle;
377 mutex_lock(&channel->lock);
378 ret = gb_pm_runtime_get_sync(bundle);
382 old_active = channel->active;
384 req.light_id = channel->light->id;
385 req.channel_id = channel->id;
386 req.brightness = (u8)channel->led->brightness;
388 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
389 &req, sizeof(req), NULL, 0);
393 if (channel->led->brightness)
394 channel->active = true;
396 channel->active = false;
398 /* we need to keep module alive when turning to active state */
399 if (!old_active && channel->active)
403 * on the other hand if going to inactive we still hold a reference and
404 * need to put it, so we could go to suspend.
406 if (old_active && !channel->active)
407 gb_pm_runtime_put_autosuspend(bundle);
410 gb_pm_runtime_put_autosuspend(bundle);
412 mutex_unlock(&channel->lock);
417 static int __gb_lights_brightness_set(struct gb_channel *channel)
421 if (channel->releasing)
424 if (is_channel_flash(channel))
425 ret = __gb_lights_flash_brightness_set(channel);
427 ret = __gb_lights_led_brightness_set(channel);
432 static int gb_brightness_set(struct led_classdev *cdev,
433 enum led_brightness value)
435 struct gb_channel *channel = get_channel_from_cdev(cdev);
437 channel->led->brightness = value;
439 return __gb_lights_brightness_set(channel);
442 static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
445 struct gb_channel *channel = get_channel_from_cdev(cdev);
447 return channel->led->brightness;
450 static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
451 unsigned long *delay_off)
453 struct gb_channel *channel = get_channel_from_cdev(cdev);
454 struct gb_connection *connection = get_conn_from_channel(channel);
455 struct gb_bundle *bundle = connection->bundle;
456 struct gb_lights_blink_request req;
460 if (channel->releasing)
463 if (!delay_on || !delay_off)
466 mutex_lock(&channel->lock);
467 ret = gb_pm_runtime_get_sync(bundle);
471 old_active = channel->active;
473 req.light_id = channel->light->id;
474 req.channel_id = channel->id;
475 req.time_on_ms = cpu_to_le16(*delay_on);
476 req.time_off_ms = cpu_to_le16(*delay_off);
478 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
479 sizeof(req), NULL, 0);
484 channel->active = true;
486 channel->active = false;
488 /* we need to keep module alive when turning to active state */
489 if (!old_active && channel->active)
493 * on the other hand if going to inactive we still hold a reference and
494 * need to put it, so we could go to suspend.
496 if (old_active && !channel->active)
497 gb_pm_runtime_put_autosuspend(bundle);
500 gb_pm_runtime_put_autosuspend(bundle);
502 mutex_unlock(&channel->lock);
507 static void gb_lights_led_operations_set(struct gb_channel *channel,
508 struct led_classdev *cdev)
510 cdev->brightness_get = gb_brightness_get;
511 cdev->brightness_set_blocking = gb_brightness_set;
513 if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
514 cdev->blink_set = gb_blink_set;
517 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
518 /* V4L2 specific helpers */
519 static const struct v4l2_flash_ops v4l2_flash_ops;
521 static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
522 struct led_flash_setting *v4l2_s)
524 v4l2_s->min = channel_s->min;
525 v4l2_s->max = channel_s->max;
526 v4l2_s->step = channel_s->step;
527 /* For v4l2 val is the default value */
528 v4l2_s->val = channel_s->max;
531 static int gb_lights_light_v4l2_register(struct gb_light *light)
533 struct gb_connection *connection = get_conn_from_light(light);
534 struct device *dev = &connection->bundle->dev;
535 struct v4l2_flash_config sd_cfg = { {0} }, sd_cfg_ind = { {0} };
536 struct led_classdev_flash *fled;
537 struct led_classdev *iled = NULL;
538 struct gb_channel *channel_torch, *channel_ind, *channel_flash;
540 channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
542 __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
545 channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
547 __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
548 &sd_cfg_ind.intensity);
549 iled = &channel_ind->fled.led_cdev;
552 channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
553 WARN_ON(!channel_flash);
555 fled = &channel_flash->fled;
557 snprintf(sd_cfg.dev_name, sizeof(sd_cfg.dev_name), "%s", light->name);
558 snprintf(sd_cfg_ind.dev_name, sizeof(sd_cfg_ind.dev_name),
559 "%s indicator", light->name);
561 /* Set the possible values to faults, in our case all faults */
562 sd_cfg.flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
563 LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
564 LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
565 LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
566 LED_FAULT_LED_OVER_TEMPERATURE;
568 light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, &v4l2_flash_ops,
570 if (IS_ERR(light->v4l2_flash))
571 return PTR_ERR(light->v4l2_flash);
574 light->v4l2_flash_ind =
575 v4l2_flash_indicator_init(dev, NULL, iled, &sd_cfg_ind);
576 if (IS_ERR(light->v4l2_flash_ind)) {
577 v4l2_flash_release(light->v4l2_flash);
578 return PTR_ERR(light->v4l2_flash_ind);
585 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
587 v4l2_flash_release(light->v4l2_flash_ind);
588 v4l2_flash_release(light->v4l2_flash);
591 static int gb_lights_light_v4l2_register(struct gb_light *light)
593 struct gb_connection *connection = get_conn_from_light(light);
595 dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
599 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
604 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
605 /* Flash specific operations */
606 static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
609 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
613 ret = __gb_lights_flash_intensity_set(channel, brightness);
617 fcdev->brightness.val = brightness;
622 static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
625 *brightness = fcdev->brightness.val;
630 static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
633 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
635 struct gb_connection *connection = get_conn_from_channel(channel);
636 struct gb_bundle *bundle = connection->bundle;
637 struct gb_lights_set_flash_strobe_request req;
640 if (channel->releasing)
643 ret = gb_pm_runtime_get_sync(bundle);
647 req.light_id = channel->light->id;
648 req.channel_id = channel->id;
649 req.state = state ? 1 : 0;
651 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
652 &req, sizeof(req), NULL, 0);
654 channel->strobe_state = state;
656 gb_pm_runtime_put_autosuspend(bundle);
661 static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
664 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
667 *state = channel->strobe_state;
671 static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
674 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
676 struct gb_connection *connection = get_conn_from_channel(channel);
677 struct gb_bundle *bundle = connection->bundle;
678 struct gb_lights_set_flash_timeout_request req;
681 if (channel->releasing)
684 ret = gb_pm_runtime_get_sync(bundle);
688 req.light_id = channel->light->id;
689 req.channel_id = channel->id;
690 req.timeout_us = cpu_to_le32(timeout);
692 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
693 &req, sizeof(req), NULL, 0);
695 fcdev->timeout.val = timeout;
697 gb_pm_runtime_put_autosuspend(bundle);
702 static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
705 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
707 struct gb_connection *connection = get_conn_from_channel(channel);
708 struct gb_bundle *bundle = connection->bundle;
709 struct gb_lights_get_flash_fault_request req;
710 struct gb_lights_get_flash_fault_response resp;
713 if (channel->releasing)
716 ret = gb_pm_runtime_get_sync(bundle);
720 req.light_id = channel->light->id;
721 req.channel_id = channel->id;
723 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
724 &req, sizeof(req), &resp, sizeof(resp));
726 *fault = le32_to_cpu(resp.fault);
728 gb_pm_runtime_put_autosuspend(bundle);
733 static const struct led_flash_ops gb_lights_flash_ops = {
734 .flash_brightness_set = gb_lights_flash_intensity_set,
735 .flash_brightness_get = gb_lights_flash_intensity_get,
736 .strobe_set = gb_lights_flash_strobe_set,
737 .strobe_get = gb_lights_flash_strobe_get,
738 .timeout_set = gb_lights_flash_timeout_set,
739 .fault_get = gb_lights_flash_fault_get,
742 static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
743 struct gb_channel *channel_torch)
747 /* we can only attach torch to a flash channel */
748 if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
751 /* Move torch brightness to the destination */
752 channel->led->max_brightness = channel_torch->led->max_brightness;
754 /* append mode name to flash name */
755 name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
756 channel_torch->mode_name);
759 kfree(channel->led->name);
760 channel->led->name = name;
762 channel_torch->led = channel->led;
767 static int __gb_lights_flash_led_register(struct gb_channel *channel)
769 struct gb_connection *connection = get_conn_from_channel(channel);
770 struct led_classdev_flash *fled = &channel->fled;
771 struct led_flash_setting *fset;
772 struct gb_channel *channel_torch;
775 fled->ops = &gb_lights_flash_ops;
777 fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
779 fset = &fled->brightness;
780 fset->min = channel->intensity_uA.min;
781 fset->max = channel->intensity_uA.max;
782 fset->step = channel->intensity_uA.step;
783 fset->val = channel->intensity_uA.max;
785 /* Only the flash mode have the timeout constraints settings */
786 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
787 fset = &fled->timeout;
788 fset->min = channel->timeout_us.min;
789 fset->max = channel->timeout_us.max;
790 fset->step = channel->timeout_us.step;
791 fset->val = channel->timeout_us.max;
795 * If light have torch mode channel, this channel will be the led
796 * classdev of the registered above flash classdev
798 channel_torch = get_channel_from_mode(channel->light,
799 GB_CHANNEL_MODE_TORCH);
801 ret = __gb_lights_channel_torch_attach(channel, channel_torch);
806 ret = led_classdev_flash_register(&connection->bundle->dev, fled);
810 channel->is_registered = true;
817 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
819 if (!channel->is_registered)
822 led_classdev_flash_unregister(&channel->fled);
825 static int gb_lights_channel_flash_config(struct gb_channel *channel)
827 struct gb_connection *connection = get_conn_from_channel(channel);
828 struct gb_lights_get_channel_flash_config_request req;
829 struct gb_lights_get_channel_flash_config_response conf;
830 struct led_flash_setting *fset;
833 req.light_id = channel->light->id;
834 req.channel_id = channel->id;
836 ret = gb_operation_sync(connection,
837 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
838 &req, sizeof(req), &conf, sizeof(conf));
843 * Intensity constraints for flash related modes: flash, torch,
844 * indicator. They will be needed for v4l2 registration.
846 fset = &channel->intensity_uA;
847 fset->min = le32_to_cpu(conf.intensity_min_uA);
848 fset->max = le32_to_cpu(conf.intensity_max_uA);
849 fset->step = le32_to_cpu(conf.intensity_step_uA);
852 * On flash type, max brightness is set as the number of intensity steps
855 channel->led->max_brightness = (fset->max - fset->min) / fset->step;
857 /* Only the flash mode have the timeout constraints settings */
858 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
859 fset = &channel->timeout_us;
860 fset->min = le32_to_cpu(conf.timeout_min_us);
861 fset->max = le32_to_cpu(conf.timeout_max_us);
862 fset->step = le32_to_cpu(conf.timeout_step_us);
868 static int gb_lights_channel_flash_config(struct gb_channel *channel)
870 struct gb_connection *connection = get_conn_from_channel(channel);
872 dev_err(&connection->bundle->dev, "no support for flash devices\n");
876 static int __gb_lights_flash_led_register(struct gb_channel *channel)
881 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
887 static int __gb_lights_led_register(struct gb_channel *channel)
889 struct gb_connection *connection = get_conn_from_channel(channel);
890 struct led_classdev *cdev = get_channel_cdev(channel);
893 ret = led_classdev_register(&connection->bundle->dev, cdev);
897 channel->is_registered = true;
901 static int gb_lights_channel_register(struct gb_channel *channel)
903 /* Normal LED channel, just register in led classdev and we are done */
904 if (!is_channel_flash(channel))
905 return __gb_lights_led_register(channel);
908 * Flash Type need more work, register flash classdev, indicator as
909 * flash classdev, torch will be led classdev of the flash classdev.
911 if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
912 return __gb_lights_flash_led_register(channel);
917 static void __gb_lights_led_unregister(struct gb_channel *channel)
919 struct led_classdev *cdev = get_channel_cdev(channel);
921 if (!channel->is_registered)
924 led_classdev_unregister(cdev);
930 static void gb_lights_channel_unregister(struct gb_channel *channel)
932 /* The same as register, handle channels differently */
933 if (!is_channel_flash(channel)) {
934 __gb_lights_led_unregister(channel);
938 if (channel->mode & GB_CHANNEL_MODE_TORCH)
939 __gb_lights_led_unregister(channel);
941 __gb_lights_flash_led_unregister(channel);
944 static int gb_lights_channel_config(struct gb_light *light,
945 struct gb_channel *channel)
947 struct gb_lights_get_channel_config_response conf;
948 struct gb_lights_get_channel_config_request req;
949 struct gb_connection *connection = get_conn_from_light(light);
950 struct led_classdev *cdev = get_channel_cdev(channel);
954 req.light_id = light->id;
955 req.channel_id = channel->id;
957 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
958 &req, sizeof(req), &conf, sizeof(conf));
962 channel->light = light;
963 channel->mode = le32_to_cpu(conf.mode);
964 channel->flags = le32_to_cpu(conf.flags);
965 channel->color = le32_to_cpu(conf.color);
966 channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
967 if (!channel->color_name)
969 channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
970 if (!channel->mode_name)
975 name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
976 channel->color_name, channel->mode_name);
982 cdev->max_brightness = conf.max_brightness;
984 ret = channel_attr_groups_set(channel, cdev);
988 gb_lights_led_operations_set(channel, cdev);
991 * If it is not a flash related channel (flash, torch or indicator) we
992 * are done here. If not, continue and fetch flash related
995 if (!is_channel_flash(channel))
998 light->has_flash = true;
1000 return gb_lights_channel_flash_config(channel);
1003 static int gb_lights_light_config(struct gb_lights *glights, u8 id)
1005 struct gb_light *light = &glights->lights[id];
1006 struct gb_lights_get_light_config_request req;
1007 struct gb_lights_get_light_config_response conf;
1011 light->glights = glights;
1016 ret = gb_operation_sync(glights->connection,
1017 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
1018 &req, sizeof(req), &conf, sizeof(conf));
1022 if (!conf.channel_count)
1024 if (!strlen(conf.name))
1027 light->channels_count = conf.channel_count;
1028 light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1031 light->channels = kcalloc(light->channels_count,
1032 sizeof(struct gb_channel), GFP_KERNEL);
1033 if (!light->channels)
1036 /* First we collect all the configurations for all channels */
1037 for (i = 0; i < light->channels_count; i++) {
1038 light->channels[i].id = i;
1039 ret = gb_lights_channel_config(light, &light->channels[i]);
1047 static int gb_lights_light_register(struct gb_light *light)
1053 * Then, if everything went ok in getting configurations, we register
1054 * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1057 for (i = 0; i < light->channels_count; i++) {
1058 ret = gb_lights_channel_register(&light->channels[i]);
1062 mutex_init(&light->channels[i].lock);
1065 light->ready = true;
1067 if (light->has_flash) {
1068 ret = gb_lights_light_v4l2_register(light);
1070 light->has_flash = false;
1078 static void gb_lights_channel_free(struct gb_channel *channel)
1080 kfree(channel->attrs);
1081 kfree(channel->attr_group);
1082 kfree(channel->attr_groups);
1083 kfree(channel->color_name);
1084 kfree(channel->mode_name);
1085 mutex_destroy(&channel->lock);
1088 static void gb_lights_channel_release(struct gb_channel *channel)
1090 channel->releasing = true;
1092 gb_lights_channel_unregister(channel);
1094 gb_lights_channel_free(channel);
1097 static void gb_lights_light_release(struct gb_light *light)
1101 light->ready = false;
1103 if (light->has_flash)
1104 gb_lights_light_v4l2_unregister(light);
1105 light->has_flash = false;
1107 for (i = 0; i < light->channels_count; i++)
1108 gb_lights_channel_release(&light->channels[i]);
1109 light->channels_count = 0;
1111 kfree(light->channels);
1112 light->channels = NULL;
1117 static void gb_lights_release(struct gb_lights *glights)
1124 mutex_lock(&glights->lights_lock);
1125 if (!glights->lights)
1128 for (i = 0; i < glights->lights_count; i++)
1129 gb_lights_light_release(&glights->lights[i]);
1131 kfree(glights->lights);
1134 mutex_unlock(&glights->lights_lock);
1135 mutex_destroy(&glights->lights_lock);
1139 static int gb_lights_get_count(struct gb_lights *glights)
1141 struct gb_lights_get_lights_response resp;
1144 ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1145 NULL, 0, &resp, sizeof(resp));
1149 if (!resp.lights_count)
1152 glights->lights_count = resp.lights_count;
1157 static int gb_lights_create_all(struct gb_lights *glights)
1159 struct gb_connection *connection = glights->connection;
1163 mutex_lock(&glights->lights_lock);
1164 ret = gb_lights_get_count(glights);
1168 glights->lights = kcalloc(glights->lights_count,
1169 sizeof(struct gb_light), GFP_KERNEL);
1170 if (!glights->lights) {
1175 for (i = 0; i < glights->lights_count; i++) {
1176 ret = gb_lights_light_config(glights, i);
1178 dev_err(&connection->bundle->dev,
1179 "Fail to configure lights device\n");
1185 mutex_unlock(&glights->lights_lock);
1189 static int gb_lights_register_all(struct gb_lights *glights)
1191 struct gb_connection *connection = glights->connection;
1195 mutex_lock(&glights->lights_lock);
1196 for (i = 0; i < glights->lights_count; i++) {
1197 ret = gb_lights_light_register(&glights->lights[i]);
1199 dev_err(&connection->bundle->dev,
1200 "Fail to enable lights device\n");
1205 mutex_unlock(&glights->lights_lock);
1209 static int gb_lights_request_handler(struct gb_operation *op)
1211 struct gb_connection *connection = op->connection;
1212 struct device *dev = &connection->bundle->dev;
1213 struct gb_lights *glights = gb_connection_get_data(connection);
1214 struct gb_light *light;
1215 struct gb_message *request;
1216 struct gb_lights_event_request *payload;
1221 if (op->type != GB_LIGHTS_TYPE_EVENT) {
1222 dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1226 request = op->request;
1228 if (request->payload_size < sizeof(*payload)) {
1229 dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1230 request->payload_size, sizeof(*payload));
1234 payload = request->payload;
1235 light_id = payload->light_id;
1237 if (light_id >= glights->lights_count ||
1238 !glights->lights[light_id].ready) {
1239 dev_err(dev, "Event received for unconfigured light id: %d\n",
1244 event = payload->event;
1246 if (event & GB_LIGHTS_LIGHT_CONFIG) {
1247 light = &glights->lights[light_id];
1249 mutex_lock(&glights->lights_lock);
1250 gb_lights_light_release(light);
1251 ret = gb_lights_light_config(glights, light_id);
1253 ret = gb_lights_light_register(light);
1255 gb_lights_light_release(light);
1256 mutex_unlock(&glights->lights_lock);
1262 static int gb_lights_probe(struct gb_bundle *bundle,
1263 const struct greybus_bundle_id *id)
1265 struct greybus_descriptor_cport *cport_desc;
1266 struct gb_connection *connection;
1267 struct gb_lights *glights;
1270 if (bundle->num_cports != 1)
1273 cport_desc = &bundle->cport_desc[0];
1274 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1277 glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1281 mutex_init(&glights->lights_lock);
1283 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1284 gb_lights_request_handler);
1285 if (IS_ERR(connection)) {
1286 ret = PTR_ERR(connection);
1290 glights->connection = connection;
1291 gb_connection_set_data(connection, glights);
1293 greybus_set_drvdata(bundle, glights);
1295 /* We aren't ready to receive an incoming request yet */
1296 ret = gb_connection_enable_tx(connection);
1298 goto error_connection_destroy;
1301 * Setup all the lights devices over this connection, if anything goes
1302 * wrong tear down all lights
1304 ret = gb_lights_create_all(glights);
1306 goto error_connection_disable;
1308 /* We are ready to receive an incoming request now, enable RX as well */
1309 ret = gb_connection_enable(connection);
1311 goto error_connection_disable;
1313 /* Enable & register lights */
1314 ret = gb_lights_register_all(glights);
1316 goto error_connection_disable;
1318 gb_pm_runtime_put_autosuspend(bundle);
1322 error_connection_disable:
1323 gb_connection_disable(connection);
1324 error_connection_destroy:
1325 gb_connection_destroy(connection);
1327 gb_lights_release(glights);
1331 static void gb_lights_disconnect(struct gb_bundle *bundle)
1333 struct gb_lights *glights = greybus_get_drvdata(bundle);
1335 if (gb_pm_runtime_get_sync(bundle))
1336 gb_pm_runtime_get_noresume(bundle);
1338 gb_connection_disable(glights->connection);
1339 gb_connection_destroy(glights->connection);
1341 gb_lights_release(glights);
1344 static const struct greybus_bundle_id gb_lights_id_table[] = {
1345 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1348 MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1350 static struct greybus_driver gb_lights_driver = {
1352 .probe = gb_lights_probe,
1353 .disconnect = gb_lights_disconnect,
1354 .id_table = gb_lights_id_table,
1356 module_greybus_driver(gb_lights_driver);
1358 MODULE_LICENSE("GPL v2");