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 = NULL;
101 for (i = 0; i < light->channels_count; i++) {
102 channel = &light->channels[i];
103 if (channel && 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);
145 /* For not flash we need to convert brightness to intensity */
146 intensity = channel->intensity_uA.min +
147 (channel->intensity_uA.step * channel->led->brightness);
149 return __gb_lights_flash_intensity_set(channel, intensity);
152 static int gb_lights_color_set(struct gb_channel *channel, u32 color);
153 static int gb_lights_fade_set(struct gb_channel *channel);
155 static void led_lock(struct led_classdev *cdev)
157 mutex_lock(&cdev->led_access);
160 static void led_unlock(struct led_classdev *cdev)
162 mutex_unlock(&cdev->led_access);
165 #define gb_lights_fade_attr(__dir) \
166 static ssize_t fade_##__dir##_show(struct device *dev, \
167 struct device_attribute *attr, \
170 struct led_classdev *cdev = dev_get_drvdata(dev); \
171 struct gb_channel *channel = get_channel_from_cdev(cdev); \
173 return sprintf(buf, "%u\n", channel->fade_##__dir); \
176 static ssize_t fade_##__dir##_store(struct device *dev, \
177 struct device_attribute *attr, \
178 const char *buf, size_t size) \
180 struct led_classdev *cdev = dev_get_drvdata(dev); \
181 struct gb_channel *channel = get_channel_from_cdev(cdev); \
186 if (led_sysfs_is_disabled(cdev)) { \
191 ret = kstrtou8(buf, 0, &fade); \
193 dev_err(dev, "could not parse fade value %d\n", ret); \
196 if (channel->fade_##__dir == fade) \
198 channel->fade_##__dir = fade; \
200 ret = gb_lights_fade_set(channel); \
209 static DEVICE_ATTR_RW(fade_##__dir)
211 gb_lights_fade_attr(in);
212 gb_lights_fade_attr(out);
214 static ssize_t color_show(struct device *dev, struct device_attribute *attr,
217 struct led_classdev *cdev = dev_get_drvdata(dev);
218 struct gb_channel *channel = get_channel_from_cdev(cdev);
220 return sprintf(buf, "0x%08x\n", channel->color);
223 static ssize_t color_store(struct device *dev, struct device_attribute *attr,
224 const char *buf, size_t size)
226 struct led_classdev *cdev = dev_get_drvdata(dev);
227 struct gb_channel *channel = get_channel_from_cdev(cdev);
232 if (led_sysfs_is_disabled(cdev)) {
236 ret = kstrtou32(buf, 0, &color);
238 dev_err(dev, "could not parse color value %d\n", ret);
242 ret = gb_lights_color_set(channel, color);
246 channel->color = color;
252 static DEVICE_ATTR_RW(color);
254 static int channel_attr_groups_set(struct gb_channel *channel,
255 struct led_classdev *cdev)
260 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
262 if (channel->flags & GB_LIGHT_CHANNEL_FADER)
268 /* Set attributes based in the channel flags */
269 channel->attrs = kcalloc(size + 1, sizeof(*channel->attrs), GFP_KERNEL);
272 channel->attr_group = kzalloc(sizeof(*channel->attr_group), GFP_KERNEL);
273 if (!channel->attr_group)
275 channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
277 if (!channel->attr_groups)
280 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
281 channel->attrs[attr++] = &dev_attr_color.attr;
282 if (channel->flags & GB_LIGHT_CHANNEL_FADER) {
283 channel->attrs[attr++] = &dev_attr_fade_in.attr;
284 channel->attrs[attr++] = &dev_attr_fade_out.attr;
287 channel->attr_group->attrs = channel->attrs;
289 channel->attr_groups[0] = channel->attr_group;
291 cdev->groups = channel->attr_groups;
296 static int gb_lights_fade_set(struct gb_channel *channel)
298 struct gb_connection *connection = get_conn_from_channel(channel);
299 struct gb_bundle *bundle = connection->bundle;
300 struct gb_lights_set_fade_request req;
303 if (channel->releasing)
306 ret = gb_pm_runtime_get_sync(bundle);
310 req.light_id = channel->light->id;
311 req.channel_id = channel->id;
312 req.fade_in = channel->fade_in;
313 req.fade_out = channel->fade_out;
314 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FADE,
315 &req, sizeof(req), NULL, 0);
317 gb_pm_runtime_put_autosuspend(bundle);
322 static int gb_lights_color_set(struct gb_channel *channel, u32 color)
324 struct gb_connection *connection = get_conn_from_channel(channel);
325 struct gb_bundle *bundle = connection->bundle;
326 struct gb_lights_set_color_request req;
329 if (channel->releasing)
332 ret = gb_pm_runtime_get_sync(bundle);
336 req.light_id = channel->light->id;
337 req.channel_id = channel->id;
338 req.color = cpu_to_le32(color);
339 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_COLOR,
340 &req, sizeof(req), NULL, 0);
342 gb_pm_runtime_put_autosuspend(bundle);
347 static int __gb_lights_led_brightness_set(struct gb_channel *channel)
349 struct gb_lights_set_brightness_request req;
350 struct gb_connection *connection = get_conn_from_channel(channel);
351 struct gb_bundle *bundle = connection->bundle;
355 mutex_lock(&channel->lock);
356 ret = gb_pm_runtime_get_sync(bundle);
360 old_active = channel->active;
362 req.light_id = channel->light->id;
363 req.channel_id = channel->id;
364 req.brightness = (u8)channel->led->brightness;
366 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
367 &req, sizeof(req), NULL, 0);
371 if (channel->led->brightness)
372 channel->active = true;
374 channel->active = false;
376 /* we need to keep module alive when turning to active state */
377 if (!old_active && channel->active)
381 * on the other hand if going to inactive we still hold a reference and
382 * need to put it, so we could go to suspend.
384 if (old_active && !channel->active)
385 gb_pm_runtime_put_autosuspend(bundle);
388 gb_pm_runtime_put_autosuspend(bundle);
390 mutex_unlock(&channel->lock);
395 static int __gb_lights_brightness_set(struct gb_channel *channel)
399 if (channel->releasing)
402 if (is_channel_flash(channel))
403 ret = __gb_lights_flash_brightness_set(channel);
405 ret = __gb_lights_led_brightness_set(channel);
410 static int gb_brightness_set(struct led_classdev *cdev,
411 enum led_brightness value)
413 struct gb_channel *channel = get_channel_from_cdev(cdev);
415 channel->led->brightness = value;
417 return __gb_lights_brightness_set(channel);
420 static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
423 struct gb_channel *channel = get_channel_from_cdev(cdev);
425 return channel->led->brightness;
428 static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
429 unsigned long *delay_off)
431 struct gb_channel *channel = get_channel_from_cdev(cdev);
432 struct gb_connection *connection = get_conn_from_channel(channel);
433 struct gb_bundle *bundle = connection->bundle;
434 struct gb_lights_blink_request req;
438 if (channel->releasing)
441 if (!delay_on || !delay_off)
444 mutex_lock(&channel->lock);
445 ret = gb_pm_runtime_get_sync(bundle);
449 old_active = channel->active;
451 req.light_id = channel->light->id;
452 req.channel_id = channel->id;
453 req.time_on_ms = cpu_to_le16(*delay_on);
454 req.time_off_ms = cpu_to_le16(*delay_off);
456 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
457 sizeof(req), NULL, 0);
462 channel->active = true;
464 channel->active = false;
466 /* we need to keep module alive when turning to active state */
467 if (!old_active && channel->active)
471 * on the other hand if going to inactive we still hold a reference and
472 * need to put it, so we could go to suspend.
474 if (old_active && !channel->active)
475 gb_pm_runtime_put_autosuspend(bundle);
478 gb_pm_runtime_put_autosuspend(bundle);
480 mutex_unlock(&channel->lock);
485 static void gb_lights_led_operations_set(struct gb_channel *channel,
486 struct led_classdev *cdev)
488 cdev->brightness_get = gb_brightness_get;
489 cdev->brightness_set_blocking = gb_brightness_set;
491 if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
492 cdev->blink_set = gb_blink_set;
495 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
496 /* V4L2 specific helpers */
497 static const struct v4l2_flash_ops v4l2_flash_ops;
499 static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
500 struct led_flash_setting *v4l2_s)
502 v4l2_s->min = channel_s->min;
503 v4l2_s->max = channel_s->max;
504 v4l2_s->step = channel_s->step;
505 /* For v4l2 val is the default value */
506 v4l2_s->val = channel_s->max;
509 static int gb_lights_light_v4l2_register(struct gb_light *light)
511 struct gb_connection *connection = get_conn_from_light(light);
512 struct device *dev = &connection->bundle->dev;
513 struct v4l2_flash_config sd_cfg = { {0} }, sd_cfg_ind = { {0} };
514 struct led_classdev_flash *fled;
515 struct led_classdev *iled = NULL;
516 struct gb_channel *channel_torch, *channel_ind, *channel_flash;
518 channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
520 __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
523 channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
525 __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
526 &sd_cfg_ind.intensity);
527 iled = &channel_ind->fled.led_cdev;
530 channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
531 WARN_ON(!channel_flash);
533 fled = &channel_flash->fled;
535 snprintf(sd_cfg.dev_name, sizeof(sd_cfg.dev_name), "%s", light->name);
536 snprintf(sd_cfg_ind.dev_name, sizeof(sd_cfg_ind.dev_name),
537 "%s indicator", light->name);
539 /* Set the possible values to faults, in our case all faults */
540 sd_cfg.flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
541 LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
542 LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
543 LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
544 LED_FAULT_LED_OVER_TEMPERATURE;
546 light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, &v4l2_flash_ops,
548 if (IS_ERR(light->v4l2_flash))
549 return PTR_ERR(light->v4l2_flash);
552 light->v4l2_flash_ind =
553 v4l2_flash_indicator_init(dev, NULL, iled, &sd_cfg_ind);
554 if (IS_ERR(light->v4l2_flash_ind)) {
555 v4l2_flash_release(light->v4l2_flash);
556 return PTR_ERR(light->v4l2_flash_ind);
563 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
565 v4l2_flash_release(light->v4l2_flash_ind);
566 v4l2_flash_release(light->v4l2_flash);
569 static int gb_lights_light_v4l2_register(struct gb_light *light)
571 struct gb_connection *connection = get_conn_from_light(light);
573 dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
577 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
582 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
583 /* Flash specific operations */
584 static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
587 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
591 ret = __gb_lights_flash_intensity_set(channel, brightness);
595 fcdev->brightness.val = brightness;
600 static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
603 *brightness = fcdev->brightness.val;
608 static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
611 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
613 struct gb_connection *connection = get_conn_from_channel(channel);
614 struct gb_bundle *bundle = connection->bundle;
615 struct gb_lights_set_flash_strobe_request req;
618 if (channel->releasing)
621 ret = gb_pm_runtime_get_sync(bundle);
625 req.light_id = channel->light->id;
626 req.channel_id = channel->id;
627 req.state = state ? 1 : 0;
629 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
630 &req, sizeof(req), NULL, 0);
632 channel->strobe_state = state;
634 gb_pm_runtime_put_autosuspend(bundle);
639 static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
642 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
645 *state = channel->strobe_state;
649 static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
652 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
654 struct gb_connection *connection = get_conn_from_channel(channel);
655 struct gb_bundle *bundle = connection->bundle;
656 struct gb_lights_set_flash_timeout_request req;
659 if (channel->releasing)
662 ret = gb_pm_runtime_get_sync(bundle);
666 req.light_id = channel->light->id;
667 req.channel_id = channel->id;
668 req.timeout_us = cpu_to_le32(timeout);
670 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
671 &req, sizeof(req), NULL, 0);
673 fcdev->timeout.val = timeout;
675 gb_pm_runtime_put_autosuspend(bundle);
680 static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
683 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
685 struct gb_connection *connection = get_conn_from_channel(channel);
686 struct gb_bundle *bundle = connection->bundle;
687 struct gb_lights_get_flash_fault_request req;
688 struct gb_lights_get_flash_fault_response resp;
691 if (channel->releasing)
694 ret = gb_pm_runtime_get_sync(bundle);
698 req.light_id = channel->light->id;
699 req.channel_id = channel->id;
701 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
702 &req, sizeof(req), &resp, sizeof(resp));
704 *fault = le32_to_cpu(resp.fault);
706 gb_pm_runtime_put_autosuspend(bundle);
711 static const struct led_flash_ops gb_lights_flash_ops = {
712 .flash_brightness_set = gb_lights_flash_intensity_set,
713 .flash_brightness_get = gb_lights_flash_intensity_get,
714 .strobe_set = gb_lights_flash_strobe_set,
715 .strobe_get = gb_lights_flash_strobe_get,
716 .timeout_set = gb_lights_flash_timeout_set,
717 .fault_get = gb_lights_flash_fault_get,
720 static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
721 struct gb_channel *channel_torch)
725 /* we can only attach torch to a flash channel */
726 if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
729 /* Move torch brightness to the destination */
730 channel->led->max_brightness = channel_torch->led->max_brightness;
732 /* append mode name to flash name */
733 name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
734 channel_torch->mode_name);
737 kfree(channel->led->name);
738 channel->led->name = name;
740 channel_torch->led = channel->led;
745 static int __gb_lights_flash_led_register(struct gb_channel *channel)
747 struct gb_connection *connection = get_conn_from_channel(channel);
748 struct led_classdev_flash *fled = &channel->fled;
749 struct led_flash_setting *fset;
750 struct gb_channel *channel_torch;
753 fled->ops = &gb_lights_flash_ops;
755 fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
757 fset = &fled->brightness;
758 fset->min = channel->intensity_uA.min;
759 fset->max = channel->intensity_uA.max;
760 fset->step = channel->intensity_uA.step;
761 fset->val = channel->intensity_uA.max;
763 /* Only the flash mode have the timeout constraints settings */
764 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
765 fset = &fled->timeout;
766 fset->min = channel->timeout_us.min;
767 fset->max = channel->timeout_us.max;
768 fset->step = channel->timeout_us.step;
769 fset->val = channel->timeout_us.max;
773 * If light have torch mode channel, this channel will be the led
774 * classdev of the registered above flash classdev
776 channel_torch = get_channel_from_mode(channel->light,
777 GB_CHANNEL_MODE_TORCH);
779 ret = __gb_lights_channel_torch_attach(channel, channel_torch);
784 ret = led_classdev_flash_register(&connection->bundle->dev, fled);
788 channel->is_registered = true;
795 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
797 if (!channel->is_registered)
800 led_classdev_flash_unregister(&channel->fled);
803 static int gb_lights_channel_flash_config(struct gb_channel *channel)
805 struct gb_connection *connection = get_conn_from_channel(channel);
806 struct gb_lights_get_channel_flash_config_request req;
807 struct gb_lights_get_channel_flash_config_response conf;
808 struct led_flash_setting *fset;
811 req.light_id = channel->light->id;
812 req.channel_id = channel->id;
814 ret = gb_operation_sync(connection,
815 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
816 &req, sizeof(req), &conf, sizeof(conf));
821 * Intensity constraints for flash related modes: flash, torch,
822 * indicator. They will be needed for v4l2 registration.
824 fset = &channel->intensity_uA;
825 fset->min = le32_to_cpu(conf.intensity_min_uA);
826 fset->max = le32_to_cpu(conf.intensity_max_uA);
827 fset->step = le32_to_cpu(conf.intensity_step_uA);
830 * On flash type, max brightness is set as the number of intensity steps
833 channel->led->max_brightness = (fset->max - fset->min) / fset->step;
835 /* Only the flash mode have the timeout constraints settings */
836 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
837 fset = &channel->timeout_us;
838 fset->min = le32_to_cpu(conf.timeout_min_us);
839 fset->max = le32_to_cpu(conf.timeout_max_us);
840 fset->step = le32_to_cpu(conf.timeout_step_us);
846 static int gb_lights_channel_flash_config(struct gb_channel *channel)
848 struct gb_connection *connection = get_conn_from_channel(channel);
850 dev_err(&connection->bundle->dev, "no support for flash devices\n");
854 static int __gb_lights_flash_led_register(struct gb_channel *channel)
859 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
865 static int __gb_lights_led_register(struct gb_channel *channel)
867 struct gb_connection *connection = get_conn_from_channel(channel);
868 struct led_classdev *cdev = get_channel_cdev(channel);
871 ret = led_classdev_register(&connection->bundle->dev, cdev);
875 channel->is_registered = true;
879 static int gb_lights_channel_register(struct gb_channel *channel)
881 /* Normal LED channel, just register in led classdev and we are done */
882 if (!is_channel_flash(channel))
883 return __gb_lights_led_register(channel);
886 * Flash Type need more work, register flash classdev, indicator as
887 * flash classdev, torch will be led classdev of the flash classdev.
889 if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
890 return __gb_lights_flash_led_register(channel);
895 static void __gb_lights_led_unregister(struct gb_channel *channel)
897 struct led_classdev *cdev = get_channel_cdev(channel);
899 if (!channel->is_registered)
902 led_classdev_unregister(cdev);
908 static void gb_lights_channel_unregister(struct gb_channel *channel)
910 /* The same as register, handle channels differently */
911 if (!is_channel_flash(channel)) {
912 __gb_lights_led_unregister(channel);
916 if (channel->mode & GB_CHANNEL_MODE_TORCH)
917 __gb_lights_led_unregister(channel);
919 __gb_lights_flash_led_unregister(channel);
922 static int gb_lights_channel_config(struct gb_light *light,
923 struct gb_channel *channel)
925 struct gb_lights_get_channel_config_response conf;
926 struct gb_lights_get_channel_config_request req;
927 struct gb_connection *connection = get_conn_from_light(light);
928 struct led_classdev *cdev = get_channel_cdev(channel);
932 req.light_id = light->id;
933 req.channel_id = channel->id;
935 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
936 &req, sizeof(req), &conf, sizeof(conf));
940 channel->light = light;
941 channel->mode = le32_to_cpu(conf.mode);
942 channel->flags = le32_to_cpu(conf.flags);
943 channel->color = le32_to_cpu(conf.color);
944 channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
945 if (!channel->color_name)
947 channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
948 if (!channel->mode_name)
953 name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
954 channel->color_name, channel->mode_name);
960 cdev->max_brightness = conf.max_brightness;
962 ret = channel_attr_groups_set(channel, cdev);
966 gb_lights_led_operations_set(channel, cdev);
969 * If it is not a flash related channel (flash, torch or indicator) we
970 * are done here. If not, continue and fetch flash related
973 if (!is_channel_flash(channel))
976 light->has_flash = true;
978 return gb_lights_channel_flash_config(channel);
981 static int gb_lights_light_config(struct gb_lights *glights, u8 id)
983 struct gb_light *light = &glights->lights[id];
984 struct gb_lights_get_light_config_request req;
985 struct gb_lights_get_light_config_response conf;
989 light->glights = glights;
994 ret = gb_operation_sync(glights->connection,
995 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
996 &req, sizeof(req), &conf, sizeof(conf));
1000 if (!conf.channel_count)
1002 if (!strlen(conf.name))
1005 light->channels_count = conf.channel_count;
1006 light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1009 light->channels = kcalloc(light->channels_count,
1010 sizeof(struct gb_channel), GFP_KERNEL);
1011 if (!light->channels)
1014 /* First we collect all the configurations for all channels */
1015 for (i = 0; i < light->channels_count; i++) {
1016 light->channels[i].id = i;
1017 ret = gb_lights_channel_config(light, &light->channels[i]);
1025 static int gb_lights_light_register(struct gb_light *light)
1031 * Then, if everything went ok in getting configurations, we register
1032 * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1035 for (i = 0; i < light->channels_count; i++) {
1036 ret = gb_lights_channel_register(&light->channels[i]);
1040 mutex_init(&light->channels[i].lock);
1043 light->ready = true;
1045 if (light->has_flash) {
1046 ret = gb_lights_light_v4l2_register(light);
1048 light->has_flash = false;
1056 static void gb_lights_channel_free(struct gb_channel *channel)
1058 kfree(channel->attrs);
1059 kfree(channel->attr_group);
1060 kfree(channel->attr_groups);
1061 kfree(channel->color_name);
1062 kfree(channel->mode_name);
1063 mutex_destroy(&channel->lock);
1066 static void gb_lights_channel_release(struct gb_channel *channel)
1068 channel->releasing = true;
1070 gb_lights_channel_unregister(channel);
1072 gb_lights_channel_free(channel);
1075 static void gb_lights_light_release(struct gb_light *light)
1079 light->ready = false;
1081 if (light->has_flash)
1082 gb_lights_light_v4l2_unregister(light);
1083 light->has_flash = false;
1085 for (i = 0; i < light->channels_count; i++)
1086 gb_lights_channel_release(&light->channels[i]);
1087 light->channels_count = 0;
1089 kfree(light->channels);
1090 light->channels = NULL;
1095 static void gb_lights_release(struct gb_lights *glights)
1102 mutex_lock(&glights->lights_lock);
1103 if (!glights->lights)
1106 for (i = 0; i < glights->lights_count; i++)
1107 gb_lights_light_release(&glights->lights[i]);
1109 kfree(glights->lights);
1112 mutex_unlock(&glights->lights_lock);
1113 mutex_destroy(&glights->lights_lock);
1117 static int gb_lights_get_count(struct gb_lights *glights)
1119 struct gb_lights_get_lights_response resp;
1122 ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1123 NULL, 0, &resp, sizeof(resp));
1127 if (!resp.lights_count)
1130 glights->lights_count = resp.lights_count;
1135 static int gb_lights_create_all(struct gb_lights *glights)
1137 struct gb_connection *connection = glights->connection;
1141 mutex_lock(&glights->lights_lock);
1142 ret = gb_lights_get_count(glights);
1146 glights->lights = kcalloc(glights->lights_count,
1147 sizeof(struct gb_light), GFP_KERNEL);
1148 if (!glights->lights) {
1153 for (i = 0; i < glights->lights_count; i++) {
1154 ret = gb_lights_light_config(glights, i);
1156 dev_err(&connection->bundle->dev,
1157 "Fail to configure lights device\n");
1163 mutex_unlock(&glights->lights_lock);
1167 static int gb_lights_register_all(struct gb_lights *glights)
1169 struct gb_connection *connection = glights->connection;
1173 mutex_lock(&glights->lights_lock);
1174 for (i = 0; i < glights->lights_count; i++) {
1175 ret = gb_lights_light_register(&glights->lights[i]);
1177 dev_err(&connection->bundle->dev,
1178 "Fail to enable lights device\n");
1183 mutex_unlock(&glights->lights_lock);
1187 static int gb_lights_request_handler(struct gb_operation *op)
1189 struct gb_connection *connection = op->connection;
1190 struct device *dev = &connection->bundle->dev;
1191 struct gb_lights *glights = gb_connection_get_data(connection);
1192 struct gb_light *light;
1193 struct gb_message *request;
1194 struct gb_lights_event_request *payload;
1199 if (op->type != GB_LIGHTS_TYPE_EVENT) {
1200 dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1204 request = op->request;
1206 if (request->payload_size < sizeof(*payload)) {
1207 dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1208 request->payload_size, sizeof(*payload));
1212 payload = request->payload;
1213 light_id = payload->light_id;
1215 if (light_id >= glights->lights_count ||
1216 !glights->lights[light_id].ready) {
1217 dev_err(dev, "Event received for unconfigured light id: %d\n",
1222 event = payload->event;
1224 if (event & GB_LIGHTS_LIGHT_CONFIG) {
1225 light = &glights->lights[light_id];
1227 mutex_lock(&glights->lights_lock);
1228 gb_lights_light_release(light);
1229 ret = gb_lights_light_config(glights, light_id);
1231 ret = gb_lights_light_register(light);
1233 gb_lights_light_release(light);
1234 mutex_unlock(&glights->lights_lock);
1240 static int gb_lights_probe(struct gb_bundle *bundle,
1241 const struct greybus_bundle_id *id)
1243 struct greybus_descriptor_cport *cport_desc;
1244 struct gb_connection *connection;
1245 struct gb_lights *glights;
1248 if (bundle->num_cports != 1)
1251 cport_desc = &bundle->cport_desc[0];
1252 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1255 glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1259 mutex_init(&glights->lights_lock);
1261 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1262 gb_lights_request_handler);
1263 if (IS_ERR(connection)) {
1264 ret = PTR_ERR(connection);
1268 glights->connection = connection;
1269 gb_connection_set_data(connection, glights);
1271 greybus_set_drvdata(bundle, glights);
1273 /* We aren't ready to receive an incoming request yet */
1274 ret = gb_connection_enable_tx(connection);
1276 goto error_connection_destroy;
1279 * Setup all the lights devices over this connection, if anything goes
1280 * wrong tear down all lights
1282 ret = gb_lights_create_all(glights);
1284 goto error_connection_disable;
1286 /* We are ready to receive an incoming request now, enable RX as well */
1287 ret = gb_connection_enable(connection);
1289 goto error_connection_disable;
1291 /* Enable & register lights */
1292 ret = gb_lights_register_all(glights);
1294 goto error_connection_disable;
1296 gb_pm_runtime_put_autosuspend(bundle);
1300 error_connection_disable:
1301 gb_connection_disable(connection);
1302 error_connection_destroy:
1303 gb_connection_destroy(connection);
1305 gb_lights_release(glights);
1309 static void gb_lights_disconnect(struct gb_bundle *bundle)
1311 struct gb_lights *glights = greybus_get_drvdata(bundle);
1313 if (gb_pm_runtime_get_sync(bundle))
1314 gb_pm_runtime_get_noresume(bundle);
1316 gb_connection_disable(glights->connection);
1317 gb_connection_destroy(glights->connection);
1319 gb_lights_release(glights);
1322 static const struct greybus_bundle_id gb_lights_id_table[] = {
1323 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1326 MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1328 static struct greybus_driver gb_lights_driver = {
1330 .probe = gb_lights_probe,
1331 .disconnect = gb_lights_disconnect,
1332 .id_table = gb_lights_id_table,
1334 module_greybus_driver(gb_lights_driver);
1336 MODULE_LICENSE("GPL v2");