2 * Greybus Lights protocol driver.
4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/kernel.h>
11 #include <linux/leds.h>
12 #include <linux/led-class-flash.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/version.h>
16 #include <media/v4l2-flash-led-class.h>
19 #include "greybus_protocols.h"
32 struct attribute **attrs;
33 struct attribute_group *attr_group;
34 const struct attribute_group **attr_groups;
35 struct led_classdev *led;
36 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
37 struct led_classdev_flash fled;
38 struct led_flash_setting intensity_uA;
39 struct led_flash_setting timeout_us;
41 struct led_classdev cled;
43 struct gb_light *light;
54 struct gb_lights *glights;
57 struct gb_channel *channels;
60 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
61 struct v4l2_flash *v4l2_flash;
66 struct gb_connection *connection;
68 struct gb_light *lights;
69 struct mutex lights_lock;
72 static void gb_lights_channel_free(struct gb_channel *channel);
74 static struct gb_connection *get_conn_from_channel(struct gb_channel *channel)
76 return channel->light->glights->connection;
79 static struct gb_connection *get_conn_from_light(struct gb_light *light)
81 return light->glights->connection;
84 static bool is_channel_flash(struct gb_channel *channel)
86 return !!(channel->mode & (GB_CHANNEL_MODE_FLASH | GB_CHANNEL_MODE_TORCH
87 | GB_CHANNEL_MODE_INDICATOR));
90 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
91 static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
93 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
95 return container_of(fled_cdev, struct gb_channel, fled);
98 static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
100 return &channel->fled.led_cdev;
103 static struct gb_channel *get_channel_from_mode(struct gb_light *light,
106 struct gb_channel *channel = NULL;
109 for (i = 0; i < light->channels_count; i++) {
110 channel = &light->channels[i];
111 if (channel && channel->mode == mode)
117 static int __gb_lights_flash_intensity_set(struct gb_channel *channel,
120 struct gb_connection *connection = get_conn_from_channel(channel);
121 struct gb_bundle *bundle = connection->bundle;
122 struct gb_lights_set_flash_intensity_request req;
125 if (channel->releasing)
128 ret = gb_pm_runtime_get_sync(bundle);
132 req.light_id = channel->light->id;
133 req.channel_id = channel->id;
134 req.intensity_uA = cpu_to_le32(intensity);
136 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_INTENSITY,
137 &req, sizeof(req), NULL, 0);
139 gb_pm_runtime_put_autosuspend(bundle);
144 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
148 /* If the channel is flash we need to get the attached torch channel */
149 if (channel->mode & GB_CHANNEL_MODE_FLASH)
150 channel = get_channel_from_mode(channel->light,
151 GB_CHANNEL_MODE_TORCH);
153 /* For not flash we need to convert brightness to intensity */
154 intensity = channel->intensity_uA.min +
155 (channel->intensity_uA.step * channel->led->brightness);
157 return __gb_lights_flash_intensity_set(channel, intensity);
160 static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
162 return container_of(cdev, struct gb_channel, cled);
165 static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
167 return &channel->cled;
170 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
176 static int gb_lights_color_set(struct gb_channel *channel, u32 color);
177 static int gb_lights_fade_set(struct gb_channel *channel);
179 static void led_lock(struct led_classdev *cdev)
181 mutex_lock(&cdev->led_access);
184 static void led_unlock(struct led_classdev *cdev)
186 mutex_unlock(&cdev->led_access);
189 #define gb_lights_fade_attr(__dir) \
190 static ssize_t fade_##__dir##_show(struct device *dev, \
191 struct device_attribute *attr, \
194 struct led_classdev *cdev = dev_get_drvdata(dev); \
195 struct gb_channel *channel = get_channel_from_cdev(cdev); \
197 return sprintf(buf, "%u\n", channel->fade_##__dir); \
200 static ssize_t fade_##__dir##_store(struct device *dev, \
201 struct device_attribute *attr, \
202 const char *buf, size_t size) \
204 struct led_classdev *cdev = dev_get_drvdata(dev); \
205 struct gb_channel *channel = get_channel_from_cdev(cdev); \
210 if (led_sysfs_is_disabled(cdev)) { \
215 ret = kstrtou8(buf, 0, &fade); \
217 dev_err(dev, "could not parse fade value %d\n", ret); \
220 if (channel->fade_##__dir == fade) \
222 channel->fade_##__dir = fade; \
224 ret = gb_lights_fade_set(channel); \
233 static DEVICE_ATTR_RW(fade_##__dir)
235 gb_lights_fade_attr(in);
236 gb_lights_fade_attr(out);
238 static ssize_t color_show(struct device *dev, struct device_attribute *attr,
241 struct led_classdev *cdev = dev_get_drvdata(dev);
242 struct gb_channel *channel = get_channel_from_cdev(cdev);
244 return sprintf(buf, "0x%08x\n", channel->color);
247 static ssize_t color_store(struct device *dev, struct device_attribute *attr,
248 const char *buf, size_t size)
250 struct led_classdev *cdev = dev_get_drvdata(dev);
251 struct gb_channel *channel = get_channel_from_cdev(cdev);
256 if (led_sysfs_is_disabled(cdev)) {
260 ret = kstrtou32(buf, 0, &color);
262 dev_err(dev, "could not parse color value %d\n", ret);
266 ret = gb_lights_color_set(channel, color);
270 channel->color = color;
276 static DEVICE_ATTR_RW(color);
278 static int channel_attr_groups_set(struct gb_channel *channel,
279 struct led_classdev *cdev)
284 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
286 if (channel->flags & GB_LIGHT_CHANNEL_FADER)
292 /* Set attributes based in the channel flags */
293 channel->attrs = kcalloc(size + 1, sizeof(*channel->attrs), GFP_KERNEL);
296 channel->attr_group = kcalloc(1, sizeof(*channel->attr_group),
298 if (!channel->attr_group)
300 channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
302 if (!channel->attr_groups)
305 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
306 channel->attrs[attr++] = &dev_attr_color.attr;
307 if (channel->flags & GB_LIGHT_CHANNEL_FADER) {
308 channel->attrs[attr++] = &dev_attr_fade_in.attr;
309 channel->attrs[attr++] = &dev_attr_fade_out.attr;
312 channel->attr_group->attrs = channel->attrs;
314 channel->attr_groups[0] = channel->attr_group;
316 cdev->groups = channel->attr_groups;
321 static int gb_lights_fade_set(struct gb_channel *channel)
323 struct gb_connection *connection = get_conn_from_channel(channel);
324 struct gb_bundle *bundle = connection->bundle;
325 struct gb_lights_set_fade_request req;
328 if (channel->releasing)
331 ret = gb_pm_runtime_get_sync(bundle);
335 req.light_id = channel->light->id;
336 req.channel_id = channel->id;
337 req.fade_in = channel->fade_in;
338 req.fade_out = channel->fade_out;
339 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FADE,
340 &req, sizeof(req), NULL, 0);
342 gb_pm_runtime_put_autosuspend(bundle);
347 static int gb_lights_color_set(struct gb_channel *channel, u32 color)
349 struct gb_connection *connection = get_conn_from_channel(channel);
350 struct gb_bundle *bundle = connection->bundle;
351 struct gb_lights_set_color_request req;
354 if (channel->releasing)
357 ret = gb_pm_runtime_get_sync(bundle);
361 req.light_id = channel->light->id;
362 req.channel_id = channel->id;
363 req.color = cpu_to_le32(color);
364 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_COLOR,
365 &req, sizeof(req), NULL, 0);
367 gb_pm_runtime_put_autosuspend(bundle);
372 static int __gb_lights_led_brightness_set(struct gb_channel *channel)
374 struct gb_lights_set_brightness_request req;
375 struct gb_connection *connection = get_conn_from_channel(channel);
376 struct gb_bundle *bundle = connection->bundle;
380 mutex_lock(&channel->lock);
381 ret = gb_pm_runtime_get_sync(bundle);
385 old_active = channel->active;
387 req.light_id = channel->light->id;
388 req.channel_id = channel->id;
389 req.brightness = (u8)channel->led->brightness;
391 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
392 &req, sizeof(req), NULL, 0);
396 if (channel->led->brightness)
397 channel->active = true;
399 channel->active = false;
401 /* we need to keep module alive when turning to active state */
402 if (!old_active && channel->active)
406 * on the other hand if going to inactive we still hold a reference and
407 * need to put it, so we could go to suspend.
409 if (old_active && !channel->active)
410 gb_pm_runtime_put_autosuspend(bundle);
413 gb_pm_runtime_put_autosuspend(bundle);
415 mutex_unlock(&channel->lock);
420 static int __gb_lights_brightness_set(struct gb_channel *channel)
424 if (channel->releasing)
427 if (is_channel_flash(channel))
428 ret = __gb_lights_flash_brightness_set(channel);
430 ret = __gb_lights_led_brightness_set(channel);
435 static int gb_brightness_set(struct led_classdev *cdev,
436 enum led_brightness value)
438 struct gb_channel *channel = get_channel_from_cdev(cdev);
440 channel->led->brightness = value;
442 return __gb_lights_brightness_set(channel);
445 static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
448 struct gb_channel *channel = get_channel_from_cdev(cdev);
450 return channel->led->brightness;
453 static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
454 unsigned long *delay_off)
456 struct gb_channel *channel = get_channel_from_cdev(cdev);
457 struct gb_connection *connection = get_conn_from_channel(channel);
458 struct gb_bundle *bundle = connection->bundle;
459 struct gb_lights_blink_request req;
463 if (channel->releasing)
466 if (!delay_on || !delay_off)
469 mutex_lock(&channel->lock);
470 ret = gb_pm_runtime_get_sync(bundle);
474 old_active = channel->active;
476 req.light_id = channel->light->id;
477 req.channel_id = channel->id;
478 req.time_on_ms = cpu_to_le16(*delay_on);
479 req.time_off_ms = cpu_to_le16(*delay_off);
481 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
482 sizeof(req), NULL, 0);
487 channel->active = true;
489 channel->active = false;
491 /* we need to keep module alive when turning to active state */
492 if (!old_active && channel->active)
496 * on the other hand if going to inactive we still hold a reference and
497 * need to put it, so we could go to suspend.
499 if (old_active && !channel->active)
500 gb_pm_runtime_put_autosuspend(bundle);
503 gb_pm_runtime_put_autosuspend(bundle);
505 mutex_unlock(&channel->lock);
510 static void gb_lights_led_operations_set(struct gb_channel *channel,
511 struct led_classdev *cdev)
513 cdev->brightness_get = gb_brightness_get;
514 cdev->brightness_set_blocking = gb_brightness_set;
516 if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
517 cdev->blink_set = gb_blink_set;
520 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
521 /* V4L2 specific helpers */
522 static const struct v4l2_flash_ops v4l2_flash_ops;
524 static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
525 struct led_flash_setting *v4l2_s)
527 v4l2_s->min = channel_s->min;
528 v4l2_s->max = channel_s->max;
529 v4l2_s->step = channel_s->step;
530 /* For v4l2 val is the default value */
531 v4l2_s->val = channel_s->max;
534 static int gb_lights_light_v4l2_register(struct gb_light *light)
536 struct gb_connection *connection = get_conn_from_light(light);
537 struct device *dev = &connection->bundle->dev;
538 struct v4l2_flash_config *sd_cfg;
539 struct led_classdev_flash *fled;
540 struct led_classdev_flash *iled = NULL;
541 struct gb_channel *channel_torch, *channel_ind, *channel_flash;
544 sd_cfg = kcalloc(1, sizeof(*sd_cfg), GFP_KERNEL);
548 channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
550 __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
551 &sd_cfg->torch_intensity);
553 channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
555 __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
556 &sd_cfg->indicator_intensity);
557 iled = &channel_ind->fled;
560 channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
561 WARN_ON(!channel_flash);
563 fled = &channel_flash->fled;
565 snprintf(sd_cfg->dev_name, sizeof(sd_cfg->dev_name), "%s", light->name);
567 /* Set the possible values to faults, in our case all faults */
568 sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
569 LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
570 LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
571 LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
572 LED_FAULT_LED_OVER_TEMPERATURE;
574 light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, iled,
575 &v4l2_flash_ops, sd_cfg);
576 if (IS_ERR_OR_NULL(light->v4l2_flash)) {
577 ret = PTR_ERR(light->v4l2_flash);
588 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
590 v4l2_flash_release(light->v4l2_flash);
593 static int gb_lights_light_v4l2_register(struct gb_light *light)
595 struct gb_connection *connection = get_conn_from_light(light);
597 dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
601 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
606 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
607 /* Flash specific operations */
608 static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
611 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
615 ret = __gb_lights_flash_intensity_set(channel, brightness);
619 fcdev->brightness.val = brightness;
624 static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
627 *brightness = fcdev->brightness.val;
632 static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
635 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
637 struct gb_connection *connection = get_conn_from_channel(channel);
638 struct gb_bundle *bundle = connection->bundle;
639 struct gb_lights_set_flash_strobe_request req;
642 if (channel->releasing)
645 ret = gb_pm_runtime_get_sync(bundle);
649 req.light_id = channel->light->id;
650 req.channel_id = channel->id;
651 req.state = state ? 1 : 0;
653 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
654 &req, sizeof(req), NULL, 0);
656 channel->strobe_state = state;
658 gb_pm_runtime_put_autosuspend(bundle);
663 static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
666 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
669 *state = channel->strobe_state;
673 static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
676 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
678 struct gb_connection *connection = get_conn_from_channel(channel);
679 struct gb_bundle *bundle = connection->bundle;
680 struct gb_lights_set_flash_timeout_request req;
683 if (channel->releasing)
686 ret = gb_pm_runtime_get_sync(bundle);
690 req.light_id = channel->light->id;
691 req.channel_id = channel->id;
692 req.timeout_us = cpu_to_le32(timeout);
694 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
695 &req, sizeof(req), NULL, 0);
697 fcdev->timeout.val = timeout;
699 gb_pm_runtime_put_autosuspend(bundle);
704 static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
707 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
709 struct gb_connection *connection = get_conn_from_channel(channel);
710 struct gb_bundle *bundle = connection->bundle;
711 struct gb_lights_get_flash_fault_request req;
712 struct gb_lights_get_flash_fault_response resp;
715 if (channel->releasing)
718 ret = gb_pm_runtime_get_sync(bundle);
722 req.light_id = channel->light->id;
723 req.channel_id = channel->id;
725 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
726 &req, sizeof(req), &resp, sizeof(resp));
728 *fault = le32_to_cpu(resp.fault);
730 gb_pm_runtime_put_autosuspend(bundle);
735 static const struct led_flash_ops gb_lights_flash_ops = {
736 .flash_brightness_set = gb_lights_flash_intensity_set,
737 .flash_brightness_get = gb_lights_flash_intensity_get,
738 .strobe_set = gb_lights_flash_strobe_set,
739 .strobe_get = gb_lights_flash_strobe_get,
740 .timeout_set = gb_lights_flash_timeout_set,
741 .fault_get = gb_lights_flash_fault_get,
744 static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
745 struct gb_channel *channel_torch)
749 /* we can only attach torch to a flash channel */
750 if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
753 /* Move torch brightness to the destination */
754 channel->led->max_brightness = channel_torch->led->max_brightness;
756 /* append mode name to flash name */
757 name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
758 channel_torch->mode_name);
761 kfree(channel->led->name);
762 channel->led->name = name;
764 channel_torch->led = channel->led;
769 static int __gb_lights_flash_led_register(struct gb_channel *channel)
771 struct gb_connection *connection = get_conn_from_channel(channel);
772 struct led_classdev_flash *fled = &channel->fled;
773 struct led_flash_setting *fset;
774 struct gb_channel *channel_torch;
777 fled->ops = &gb_lights_flash_ops;
779 fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
781 fset = &fled->brightness;
782 fset->min = channel->intensity_uA.min;
783 fset->max = channel->intensity_uA.max;
784 fset->step = channel->intensity_uA.step;
785 fset->val = channel->intensity_uA.max;
787 /* Only the flash mode have the timeout constraints settings */
788 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
789 fset = &fled->timeout;
790 fset->min = channel->timeout_us.min;
791 fset->max = channel->timeout_us.max;
792 fset->step = channel->timeout_us.step;
793 fset->val = channel->timeout_us.max;
797 * If light have torch mode channel, this channel will be the led
798 * classdev of the registered above flash classdev
800 channel_torch = get_channel_from_mode(channel->light,
801 GB_CHANNEL_MODE_TORCH);
803 ret = __gb_lights_channel_torch_attach(channel, channel_torch);
808 ret = led_classdev_flash_register(&connection->bundle->dev, fled);
812 channel->is_registered = true;
819 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
821 if (!channel->is_registered)
824 led_classdev_flash_unregister(&channel->fled);
827 static int gb_lights_channel_flash_config(struct gb_channel *channel)
829 struct gb_connection *connection = get_conn_from_channel(channel);
830 struct gb_lights_get_channel_flash_config_request req;
831 struct gb_lights_get_channel_flash_config_response conf;
832 struct led_flash_setting *fset;
835 req.light_id = channel->light->id;
836 req.channel_id = channel->id;
838 ret = gb_operation_sync(connection,
839 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
840 &req, sizeof(req), &conf, sizeof(conf));
845 * Intensity constraints for flash related modes: flash, torch,
846 * indicator. They will be needed for v4l2 registration.
848 fset = &channel->intensity_uA;
849 fset->min = le32_to_cpu(conf.intensity_min_uA);
850 fset->max = le32_to_cpu(conf.intensity_max_uA);
851 fset->step = le32_to_cpu(conf.intensity_step_uA);
854 * On flash type, max brightness is set as the number of intensity steps
857 channel->led->max_brightness = (fset->max - fset->min) / fset->step;
859 /* Only the flash mode have the timeout constraints settings */
860 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
861 fset = &channel->timeout_us;
862 fset->min = le32_to_cpu(conf.timeout_min_us);
863 fset->max = le32_to_cpu(conf.timeout_max_us);
864 fset->step = le32_to_cpu(conf.timeout_step_us);
870 static int gb_lights_channel_flash_config(struct gb_channel *channel)
872 struct gb_connection *connection = get_conn_from_channel(channel);
874 dev_err(&connection->bundle->dev, "no support for flash devices\n");
878 static int __gb_lights_flash_led_register(struct gb_channel *channel)
883 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
889 static int __gb_lights_led_register(struct gb_channel *channel)
891 struct gb_connection *connection = get_conn_from_channel(channel);
892 struct led_classdev *cdev = get_channel_cdev(channel);
895 ret = led_classdev_register(&connection->bundle->dev, cdev);
899 channel->is_registered = true;
903 static int gb_lights_channel_register(struct gb_channel *channel)
905 /* Normal LED channel, just register in led classdev and we are done */
906 if (!is_channel_flash(channel))
907 return __gb_lights_led_register(channel);
910 * Flash Type need more work, register flash classdev, indicator as
911 * flash classdev, torch will be led classdev of the flash classdev.
913 if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
914 return __gb_lights_flash_led_register(channel);
919 static void __gb_lights_led_unregister(struct gb_channel *channel)
921 struct led_classdev *cdev = get_channel_cdev(channel);
923 if (!channel->is_registered)
926 led_classdev_unregister(cdev);
932 static void gb_lights_channel_unregister(struct gb_channel *channel)
934 /* The same as register, handle channels differently */
935 if (!is_channel_flash(channel)) {
936 __gb_lights_led_unregister(channel);
940 if (channel->mode & GB_CHANNEL_MODE_TORCH)
941 __gb_lights_led_unregister(channel);
943 __gb_lights_flash_led_unregister(channel);
946 static int gb_lights_channel_config(struct gb_light *light,
947 struct gb_channel *channel)
949 struct gb_lights_get_channel_config_response conf;
950 struct gb_lights_get_channel_config_request req;
951 struct gb_connection *connection = get_conn_from_light(light);
952 struct led_classdev *cdev = get_channel_cdev(channel);
956 req.light_id = light->id;
957 req.channel_id = channel->id;
959 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
960 &req, sizeof(req), &conf, sizeof(conf));
964 channel->light = light;
965 channel->mode = le32_to_cpu(conf.mode);
966 channel->flags = le32_to_cpu(conf.flags);
967 channel->color = le32_to_cpu(conf.color);
968 channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
969 if (!channel->color_name)
971 channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
972 if (!channel->mode_name)
977 name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
978 channel->color_name, channel->mode_name);
984 cdev->max_brightness = conf.max_brightness;
986 ret = channel_attr_groups_set(channel, cdev);
990 gb_lights_led_operations_set(channel, cdev);
993 * If it is not a flash related channel (flash, torch or indicator) we
994 * are done here. If not, continue and fetch flash related
997 if (!is_channel_flash(channel))
1000 light->has_flash = true;
1002 ret = gb_lights_channel_flash_config(channel);
1009 static int gb_lights_light_config(struct gb_lights *glights, u8 id)
1011 struct gb_light *light = &glights->lights[id];
1012 struct gb_lights_get_light_config_request req;
1013 struct gb_lights_get_light_config_response conf;
1017 light->glights = glights;
1022 ret = gb_operation_sync(glights->connection,
1023 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
1024 &req, sizeof(req), &conf, sizeof(conf));
1028 if (!conf.channel_count)
1030 if (!strlen(conf.name))
1033 light->channels_count = conf.channel_count;
1034 light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1036 light->channels = kzalloc(light->channels_count *
1037 sizeof(struct gb_channel), GFP_KERNEL);
1038 if (!light->channels)
1041 /* First we collect all the configurations for all channels */
1042 for (i = 0; i < light->channels_count; i++) {
1043 light->channels[i].id = i;
1044 ret = gb_lights_channel_config(light, &light->channels[i]);
1052 static int gb_lights_light_register(struct gb_light *light)
1058 * Then, if everything went ok in getting configurations, we register
1059 * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1062 for (i = 0; i < light->channels_count; i++) {
1063 ret = gb_lights_channel_register(&light->channels[i]);
1067 mutex_init(&light->channels[i].lock);
1070 light->ready = true;
1072 if (light->has_flash) {
1073 ret = gb_lights_light_v4l2_register(light);
1075 light->has_flash = false;
1083 static void gb_lights_channel_free(struct gb_channel *channel)
1085 kfree(channel->attrs);
1086 kfree(channel->attr_group);
1087 kfree(channel->attr_groups);
1088 kfree(channel->color_name);
1089 kfree(channel->mode_name);
1090 mutex_destroy(&channel->lock);
1093 static void gb_lights_channel_release(struct gb_channel *channel)
1095 channel->releasing = true;
1097 gb_lights_channel_unregister(channel);
1099 gb_lights_channel_free(channel);
1102 static void gb_lights_light_release(struct gb_light *light)
1106 light->ready = false;
1108 if (light->has_flash)
1109 gb_lights_light_v4l2_unregister(light);
1110 light->has_flash = false;
1112 for (i = 0; i < light->channels_count; i++)
1113 gb_lights_channel_release(&light->channels[i]);
1114 light->channels_count = 0;
1116 kfree(light->channels);
1117 light->channels = NULL;
1122 static void gb_lights_release(struct gb_lights *glights)
1129 mutex_lock(&glights->lights_lock);
1130 if (!glights->lights)
1133 for (i = 0; i < glights->lights_count; i++)
1134 gb_lights_light_release(&glights->lights[i]);
1136 kfree(glights->lights);
1139 mutex_unlock(&glights->lights_lock);
1140 mutex_destroy(&glights->lights_lock);
1144 static int gb_lights_get_count(struct gb_lights *glights)
1146 struct gb_lights_get_lights_response resp;
1149 ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1150 NULL, 0, &resp, sizeof(resp));
1154 if (!resp.lights_count)
1157 glights->lights_count = resp.lights_count;
1162 static int gb_lights_create_all(struct gb_lights *glights)
1164 struct gb_connection *connection = glights->connection;
1168 mutex_lock(&glights->lights_lock);
1169 ret = gb_lights_get_count(glights);
1173 glights->lights = kzalloc(glights->lights_count *
1174 sizeof(struct gb_light), GFP_KERNEL);
1175 if (!glights->lights) {
1180 for (i = 0; i < glights->lights_count; i++) {
1181 ret = gb_lights_light_config(glights, i);
1183 dev_err(&connection->bundle->dev,
1184 "Fail to configure lights device\n");
1190 mutex_unlock(&glights->lights_lock);
1194 static int gb_lights_register_all(struct gb_lights *glights)
1196 struct gb_connection *connection = glights->connection;
1200 mutex_lock(&glights->lights_lock);
1201 for (i = 0; i < glights->lights_count; i++) {
1202 ret = gb_lights_light_register(&glights->lights[i]);
1204 dev_err(&connection->bundle->dev,
1205 "Fail to enable lights device\n");
1210 mutex_unlock(&glights->lights_lock);
1214 static int gb_lights_request_handler(struct gb_operation *op)
1216 struct gb_connection *connection = op->connection;
1217 struct device *dev = &connection->bundle->dev;
1218 struct gb_lights *glights = gb_connection_get_data(connection);
1219 struct gb_light *light;
1220 struct gb_message *request;
1221 struct gb_lights_event_request *payload;
1226 if (op->type != GB_LIGHTS_TYPE_EVENT) {
1227 dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1231 request = op->request;
1233 if (request->payload_size < sizeof(*payload)) {
1234 dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1235 request->payload_size, sizeof(*payload));
1239 payload = request->payload;
1240 light_id = payload->light_id;
1242 if (light_id >= glights->lights_count ||
1243 !glights->lights[light_id].ready) {
1244 dev_err(dev, "Event received for unconfigured light id: %d\n",
1249 event = payload->event;
1251 if (event & GB_LIGHTS_LIGHT_CONFIG) {
1252 light = &glights->lights[light_id];
1254 mutex_lock(&glights->lights_lock);
1255 gb_lights_light_release(light);
1256 ret = gb_lights_light_config(glights, light_id);
1258 ret = gb_lights_light_register(light);
1260 gb_lights_light_release(light);
1261 mutex_unlock(&glights->lights_lock);
1267 static int gb_lights_probe(struct gb_bundle *bundle,
1268 const struct greybus_bundle_id *id)
1270 struct greybus_descriptor_cport *cport_desc;
1271 struct gb_connection *connection;
1272 struct gb_lights *glights;
1275 if (bundle->num_cports != 1)
1278 cport_desc = &bundle->cport_desc[0];
1279 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1282 glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1286 mutex_init(&glights->lights_lock);
1288 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1289 gb_lights_request_handler);
1290 if (IS_ERR(connection)) {
1291 ret = PTR_ERR(connection);
1295 glights->connection = connection;
1296 gb_connection_set_data(connection, glights);
1298 greybus_set_drvdata(bundle, glights);
1300 /* We aren't ready to receive an incoming request yet */
1301 ret = gb_connection_enable_tx(connection);
1303 goto error_connection_destroy;
1306 * Setup all the lights devices over this connection, if anything goes
1307 * wrong tear down all lights
1309 ret = gb_lights_create_all(glights);
1311 goto error_connection_disable;
1313 /* We are ready to receive an incoming request now, enable RX as well */
1314 ret = gb_connection_enable(connection);
1316 goto error_connection_disable;
1318 /* Enable & register lights */
1319 ret = gb_lights_register_all(glights);
1321 goto error_connection_disable;
1323 gb_pm_runtime_put_autosuspend(bundle);
1327 error_connection_disable:
1328 gb_connection_disable(connection);
1329 error_connection_destroy:
1330 gb_connection_destroy(connection);
1332 gb_lights_release(glights);
1336 static void gb_lights_disconnect(struct gb_bundle *bundle)
1338 struct gb_lights *glights = greybus_get_drvdata(bundle);
1340 if (gb_pm_runtime_get_sync(bundle))
1341 gb_pm_runtime_get_noresume(bundle);
1343 gb_connection_disable(glights->connection);
1344 gb_connection_destroy(glights->connection);
1346 gb_lights_release(glights);
1349 static const struct greybus_bundle_id gb_lights_id_table[] = {
1350 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1353 MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1355 static struct greybus_driver gb_lights_driver = {
1357 .probe = gb_lights_probe,
1358 .disconnect = gb_lights_disconnect,
1359 .id_table = gb_lights_id_table,
1361 module_greybus_driver(gb_lights_driver);
1363 MODULE_LICENSE("GPL v2");