drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / staging / greybus / light.c
blobe509fdc715dbbf53fa4cbd9a9f6d7b539ce7e354
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus Lights protocol driver.
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
7 */
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>
17 #define NAMES_MAX 32
19 struct gb_channel {
20 u8 id;
21 u32 flags;
22 u32 color;
23 char *color_name;
24 u8 fade_in;
25 u8 fade_out;
26 u32 mode;
27 char *mode_name;
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;
36 bool is_registered;
37 bool releasing;
38 bool strobe_state;
39 bool active;
40 struct mutex lock;
43 struct gb_light {
44 u8 id;
45 char *name;
46 struct gb_lights *glights;
47 u32 flags;
48 u8 channels_count;
49 struct gb_channel *channels;
50 bool has_flash;
51 bool ready;
52 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
53 struct v4l2_flash *v4l2_flash;
54 struct v4l2_flash *v4l2_flash_ind;
55 #endif
58 struct gb_lights {
59 struct gb_connection *connection;
60 u8 lights_count;
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,
96 u32 mode)
98 struct gb_channel *channel;
99 int i;
101 for (i = 0; i < light->channels_count; i++) {
102 channel = &light->channels[i];
103 if (channel->mode == mode)
104 return channel;
106 return NULL;
109 static int __gb_lights_flash_intensity_set(struct gb_channel *channel,
110 u32 intensity)
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;
115 int ret;
117 if (channel->releasing)
118 return -ESHUTDOWN;
120 ret = gb_pm_runtime_get_sync(bundle);
121 if (ret < 0)
122 return ret;
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);
133 return ret;
136 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
138 u32 intensity;
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 if (!channel)
146 return -EINVAL;
148 /* For not flash we need to convert brightness to intensity */
149 intensity = channel->intensity_uA.min +
150 (channel->intensity_uA.step * channel->led->brightness);
152 return __gb_lights_flash_intensity_set(channel, intensity);
155 static int gb_lights_color_set(struct gb_channel *channel, u32 color);
156 static int gb_lights_fade_set(struct gb_channel *channel);
158 static void led_lock(struct led_classdev *cdev)
160 mutex_lock(&cdev->led_access);
163 static void led_unlock(struct led_classdev *cdev)
165 mutex_unlock(&cdev->led_access);
168 #define gb_lights_fade_attr(__dir) \
169 static ssize_t fade_##__dir##_show(struct device *dev, \
170 struct device_attribute *attr, \
171 char *buf) \
173 struct led_classdev *cdev = dev_get_drvdata(dev); \
174 struct gb_channel *channel = get_channel_from_cdev(cdev); \
176 return sprintf(buf, "%u\n", channel->fade_##__dir); \
179 static ssize_t fade_##__dir##_store(struct device *dev, \
180 struct device_attribute *attr, \
181 const char *buf, size_t size) \
183 struct led_classdev *cdev = dev_get_drvdata(dev); \
184 struct gb_channel *channel = get_channel_from_cdev(cdev); \
185 u8 fade; \
186 int ret; \
188 led_lock(cdev); \
189 if (led_sysfs_is_disabled(cdev)) { \
190 ret = -EBUSY; \
191 goto unlock; \
194 ret = kstrtou8(buf, 0, &fade); \
195 if (ret < 0) { \
196 dev_err(dev, "could not parse fade value %d\n", ret); \
197 goto unlock; \
199 if (channel->fade_##__dir == fade) \
200 goto unlock; \
201 channel->fade_##__dir = fade; \
203 ret = gb_lights_fade_set(channel); \
204 if (ret < 0) \
205 goto unlock; \
207 ret = size; \
208 unlock: \
209 led_unlock(cdev); \
210 return ret; \
212 static DEVICE_ATTR_RW(fade_##__dir)
214 gb_lights_fade_attr(in);
215 gb_lights_fade_attr(out);
217 static ssize_t color_show(struct device *dev, struct device_attribute *attr,
218 char *buf)
220 struct led_classdev *cdev = dev_get_drvdata(dev);
221 struct gb_channel *channel = get_channel_from_cdev(cdev);
223 return sprintf(buf, "0x%08x\n", channel->color);
226 static ssize_t color_store(struct device *dev, struct device_attribute *attr,
227 const char *buf, size_t size)
229 struct led_classdev *cdev = dev_get_drvdata(dev);
230 struct gb_channel *channel = get_channel_from_cdev(cdev);
231 u32 color;
232 int ret;
234 led_lock(cdev);
235 if (led_sysfs_is_disabled(cdev)) {
236 ret = -EBUSY;
237 goto unlock;
239 ret = kstrtou32(buf, 0, &color);
240 if (ret < 0) {
241 dev_err(dev, "could not parse color value %d\n", ret);
242 goto unlock;
245 ret = gb_lights_color_set(channel, color);
246 if (ret < 0)
247 goto unlock;
249 channel->color = color;
250 ret = size;
251 unlock:
252 led_unlock(cdev);
253 return ret;
255 static DEVICE_ATTR_RW(color);
257 static int channel_attr_groups_set(struct gb_channel *channel,
258 struct led_classdev *cdev)
260 int attr = 0;
261 int size = 0;
263 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
264 size++;
265 if (channel->flags & GB_LIGHT_CHANNEL_FADER)
266 size += 2;
268 if (!size)
269 return 0;
271 /* Set attributes based in the channel flags */
272 channel->attrs = kcalloc(size + 1, sizeof(*channel->attrs), GFP_KERNEL);
273 if (!channel->attrs)
274 return -ENOMEM;
275 channel->attr_group = kzalloc(sizeof(*channel->attr_group), GFP_KERNEL);
276 if (!channel->attr_group)
277 return -ENOMEM;
278 channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
279 GFP_KERNEL);
280 if (!channel->attr_groups)
281 return -ENOMEM;
283 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
284 channel->attrs[attr++] = &dev_attr_color.attr;
285 if (channel->flags & GB_LIGHT_CHANNEL_FADER) {
286 channel->attrs[attr++] = &dev_attr_fade_in.attr;
287 channel->attrs[attr++] = &dev_attr_fade_out.attr;
290 channel->attr_group->attrs = channel->attrs;
292 channel->attr_groups[0] = channel->attr_group;
294 cdev->groups = channel->attr_groups;
296 return 0;
299 static int gb_lights_fade_set(struct gb_channel *channel)
301 struct gb_connection *connection = get_conn_from_channel(channel);
302 struct gb_bundle *bundle = connection->bundle;
303 struct gb_lights_set_fade_request req;
304 int ret;
306 if (channel->releasing)
307 return -ESHUTDOWN;
309 ret = gb_pm_runtime_get_sync(bundle);
310 if (ret < 0)
311 return ret;
313 req.light_id = channel->light->id;
314 req.channel_id = channel->id;
315 req.fade_in = channel->fade_in;
316 req.fade_out = channel->fade_out;
317 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FADE,
318 &req, sizeof(req), NULL, 0);
320 gb_pm_runtime_put_autosuspend(bundle);
322 return ret;
325 static int gb_lights_color_set(struct gb_channel *channel, u32 color)
327 struct gb_connection *connection = get_conn_from_channel(channel);
328 struct gb_bundle *bundle = connection->bundle;
329 struct gb_lights_set_color_request req;
330 int ret;
332 if (channel->releasing)
333 return -ESHUTDOWN;
335 ret = gb_pm_runtime_get_sync(bundle);
336 if (ret < 0)
337 return ret;
339 req.light_id = channel->light->id;
340 req.channel_id = channel->id;
341 req.color = cpu_to_le32(color);
342 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_COLOR,
343 &req, sizeof(req), NULL, 0);
345 gb_pm_runtime_put_autosuspend(bundle);
347 return ret;
350 static int __gb_lights_led_brightness_set(struct gb_channel *channel)
352 struct gb_lights_set_brightness_request req;
353 struct gb_connection *connection = get_conn_from_channel(channel);
354 struct gb_bundle *bundle = connection->bundle;
355 bool old_active;
356 int ret;
358 mutex_lock(&channel->lock);
359 ret = gb_pm_runtime_get_sync(bundle);
360 if (ret < 0)
361 goto out_unlock;
363 old_active = channel->active;
365 req.light_id = channel->light->id;
366 req.channel_id = channel->id;
367 req.brightness = (u8)channel->led->brightness;
369 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
370 &req, sizeof(req), NULL, 0);
371 if (ret < 0)
372 goto out_pm_put;
374 if (channel->led->brightness)
375 channel->active = true;
376 else
377 channel->active = false;
379 /* we need to keep module alive when turning to active state */
380 if (!old_active && channel->active)
381 goto out_unlock;
384 * on the other hand if going to inactive we still hold a reference and
385 * need to put it, so we could go to suspend.
387 if (old_active && !channel->active)
388 gb_pm_runtime_put_autosuspend(bundle);
390 out_pm_put:
391 gb_pm_runtime_put_autosuspend(bundle);
392 out_unlock:
393 mutex_unlock(&channel->lock);
395 return ret;
398 static int __gb_lights_brightness_set(struct gb_channel *channel)
400 int ret;
402 if (channel->releasing)
403 return 0;
405 if (is_channel_flash(channel))
406 ret = __gb_lights_flash_brightness_set(channel);
407 else
408 ret = __gb_lights_led_brightness_set(channel);
410 return ret;
413 static int gb_brightness_set(struct led_classdev *cdev,
414 enum led_brightness value)
416 struct gb_channel *channel = get_channel_from_cdev(cdev);
418 channel->led->brightness = value;
420 return __gb_lights_brightness_set(channel);
423 static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
426 struct gb_channel *channel = get_channel_from_cdev(cdev);
428 return channel->led->brightness;
431 static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
432 unsigned long *delay_off)
434 struct gb_channel *channel = get_channel_from_cdev(cdev);
435 struct gb_connection *connection = get_conn_from_channel(channel);
436 struct gb_bundle *bundle = connection->bundle;
437 struct gb_lights_blink_request req;
438 bool old_active;
439 int ret;
441 if (channel->releasing)
442 return -ESHUTDOWN;
444 if (!delay_on || !delay_off)
445 return -EINVAL;
447 mutex_lock(&channel->lock);
448 ret = gb_pm_runtime_get_sync(bundle);
449 if (ret < 0)
450 goto out_unlock;
452 old_active = channel->active;
454 req.light_id = channel->light->id;
455 req.channel_id = channel->id;
456 req.time_on_ms = cpu_to_le16(*delay_on);
457 req.time_off_ms = cpu_to_le16(*delay_off);
459 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
460 sizeof(req), NULL, 0);
461 if (ret < 0)
462 goto out_pm_put;
464 if (*delay_on)
465 channel->active = true;
466 else
467 channel->active = false;
469 /* we need to keep module alive when turning to active state */
470 if (!old_active && channel->active)
471 goto out_unlock;
474 * on the other hand if going to inactive we still hold a reference and
475 * need to put it, so we could go to suspend.
477 if (old_active && !channel->active)
478 gb_pm_runtime_put_autosuspend(bundle);
480 out_pm_put:
481 gb_pm_runtime_put_autosuspend(bundle);
482 out_unlock:
483 mutex_unlock(&channel->lock);
485 return ret;
488 static void gb_lights_led_operations_set(struct gb_channel *channel,
489 struct led_classdev *cdev)
491 cdev->brightness_get = gb_brightness_get;
492 cdev->brightness_set_blocking = gb_brightness_set;
494 if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
495 cdev->blink_set = gb_blink_set;
498 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
499 /* V4L2 specific helpers */
500 static const struct v4l2_flash_ops v4l2_flash_ops;
502 static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
503 struct led_flash_setting *v4l2_s)
505 v4l2_s->min = channel_s->min;
506 v4l2_s->max = channel_s->max;
507 v4l2_s->step = channel_s->step;
508 /* For v4l2 val is the default value */
509 v4l2_s->val = channel_s->max;
512 static int gb_lights_light_v4l2_register(struct gb_light *light)
514 struct gb_connection *connection = get_conn_from_light(light);
515 struct device *dev = &connection->bundle->dev;
516 struct v4l2_flash_config sd_cfg = { {0} }, sd_cfg_ind = { {0} };
517 struct led_classdev_flash *fled;
518 struct led_classdev *iled = NULL;
519 struct gb_channel *channel_torch, *channel_ind, *channel_flash;
521 channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
522 if (channel_torch)
523 __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
524 &sd_cfg.intensity);
526 channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
527 if (channel_ind) {
528 __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
529 &sd_cfg_ind.intensity);
530 iled = &channel_ind->fled.led_cdev;
533 channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
534 if (!channel_flash) {
535 dev_err(dev, "failed to get flash channel from mode\n");
536 return -EINVAL;
539 fled = &channel_flash->fled;
541 snprintf(sd_cfg.dev_name, sizeof(sd_cfg.dev_name), "%s", light->name);
542 snprintf(sd_cfg_ind.dev_name, sizeof(sd_cfg_ind.dev_name),
543 "%s indicator", light->name);
545 /* Set the possible values to faults, in our case all faults */
546 sd_cfg.flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
547 LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
548 LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
549 LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
550 LED_FAULT_LED_OVER_TEMPERATURE;
552 light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, &v4l2_flash_ops,
553 &sd_cfg);
554 if (IS_ERR(light->v4l2_flash))
555 return PTR_ERR(light->v4l2_flash);
557 if (channel_ind) {
558 light->v4l2_flash_ind =
559 v4l2_flash_indicator_init(dev, NULL, iled, &sd_cfg_ind);
560 if (IS_ERR(light->v4l2_flash_ind)) {
561 v4l2_flash_release(light->v4l2_flash);
562 return PTR_ERR(light->v4l2_flash_ind);
566 return 0;
569 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
571 v4l2_flash_release(light->v4l2_flash_ind);
572 v4l2_flash_release(light->v4l2_flash);
574 #else
575 static int gb_lights_light_v4l2_register(struct gb_light *light)
577 struct gb_connection *connection = get_conn_from_light(light);
579 dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
580 return 0;
583 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
586 #endif
588 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
589 /* Flash specific operations */
590 static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
591 u32 brightness)
593 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
594 fled);
595 int ret;
597 ret = __gb_lights_flash_intensity_set(channel, brightness);
598 if (ret < 0)
599 return ret;
601 fcdev->brightness.val = brightness;
603 return 0;
606 static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
607 u32 *brightness)
609 *brightness = fcdev->brightness.val;
611 return 0;
614 static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
615 bool state)
617 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
618 fled);
619 struct gb_connection *connection = get_conn_from_channel(channel);
620 struct gb_bundle *bundle = connection->bundle;
621 struct gb_lights_set_flash_strobe_request req;
622 int ret;
624 if (channel->releasing)
625 return -ESHUTDOWN;
627 ret = gb_pm_runtime_get_sync(bundle);
628 if (ret < 0)
629 return ret;
631 req.light_id = channel->light->id;
632 req.channel_id = channel->id;
633 req.state = state ? 1 : 0;
635 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
636 &req, sizeof(req), NULL, 0);
637 if (!ret)
638 channel->strobe_state = state;
640 gb_pm_runtime_put_autosuspend(bundle);
642 return ret;
645 static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
646 bool *state)
648 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
649 fled);
651 *state = channel->strobe_state;
652 return 0;
655 static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
656 u32 timeout)
658 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
659 fled);
660 struct gb_connection *connection = get_conn_from_channel(channel);
661 struct gb_bundle *bundle = connection->bundle;
662 struct gb_lights_set_flash_timeout_request req;
663 int ret;
665 if (channel->releasing)
666 return -ESHUTDOWN;
668 ret = gb_pm_runtime_get_sync(bundle);
669 if (ret < 0)
670 return ret;
672 req.light_id = channel->light->id;
673 req.channel_id = channel->id;
674 req.timeout_us = cpu_to_le32(timeout);
676 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
677 &req, sizeof(req), NULL, 0);
678 if (!ret)
679 fcdev->timeout.val = timeout;
681 gb_pm_runtime_put_autosuspend(bundle);
683 return ret;
686 static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
687 u32 *fault)
689 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
690 fled);
691 struct gb_connection *connection = get_conn_from_channel(channel);
692 struct gb_bundle *bundle = connection->bundle;
693 struct gb_lights_get_flash_fault_request req;
694 struct gb_lights_get_flash_fault_response resp;
695 int ret;
697 if (channel->releasing)
698 return -ESHUTDOWN;
700 ret = gb_pm_runtime_get_sync(bundle);
701 if (ret < 0)
702 return ret;
704 req.light_id = channel->light->id;
705 req.channel_id = channel->id;
707 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
708 &req, sizeof(req), &resp, sizeof(resp));
709 if (!ret)
710 *fault = le32_to_cpu(resp.fault);
712 gb_pm_runtime_put_autosuspend(bundle);
714 return ret;
717 static const struct led_flash_ops gb_lights_flash_ops = {
718 .flash_brightness_set = gb_lights_flash_intensity_set,
719 .flash_brightness_get = gb_lights_flash_intensity_get,
720 .strobe_set = gb_lights_flash_strobe_set,
721 .strobe_get = gb_lights_flash_strobe_get,
722 .timeout_set = gb_lights_flash_timeout_set,
723 .fault_get = gb_lights_flash_fault_get,
726 static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
727 struct gb_channel *channel_torch)
729 char *name;
731 /* we can only attach torch to a flash channel */
732 if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
733 return 0;
735 /* Move torch brightness to the destination */
736 channel->led->max_brightness = channel_torch->led->max_brightness;
738 /* append mode name to flash name */
739 name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
740 channel_torch->mode_name);
741 if (!name)
742 return -ENOMEM;
743 kfree(channel->led->name);
744 channel->led->name = name;
746 channel_torch->led = channel->led;
748 return 0;
751 static int __gb_lights_flash_led_register(struct gb_channel *channel)
753 struct gb_connection *connection = get_conn_from_channel(channel);
754 struct led_classdev_flash *fled = &channel->fled;
755 struct led_flash_setting *fset;
756 struct gb_channel *channel_torch;
757 int ret;
759 fled->ops = &gb_lights_flash_ops;
761 fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
763 fset = &fled->brightness;
764 fset->min = channel->intensity_uA.min;
765 fset->max = channel->intensity_uA.max;
766 fset->step = channel->intensity_uA.step;
767 fset->val = channel->intensity_uA.max;
769 /* Only the flash mode have the timeout constraints settings */
770 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
771 fset = &fled->timeout;
772 fset->min = channel->timeout_us.min;
773 fset->max = channel->timeout_us.max;
774 fset->step = channel->timeout_us.step;
775 fset->val = channel->timeout_us.max;
779 * If light have torch mode channel, this channel will be the led
780 * classdev of the registered above flash classdev
782 channel_torch = get_channel_from_mode(channel->light,
783 GB_CHANNEL_MODE_TORCH);
784 if (channel_torch) {
785 ret = __gb_lights_channel_torch_attach(channel, channel_torch);
786 if (ret < 0)
787 goto fail;
790 ret = led_classdev_flash_register(&connection->bundle->dev, fled);
791 if (ret < 0)
792 goto fail;
794 channel->is_registered = true;
795 return 0;
796 fail:
797 channel->led = NULL;
798 return ret;
801 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
803 if (!channel->is_registered)
804 return;
806 led_classdev_flash_unregister(&channel->fled);
809 static int gb_lights_channel_flash_config(struct gb_channel *channel)
811 struct gb_connection *connection = get_conn_from_channel(channel);
812 struct gb_lights_get_channel_flash_config_request req;
813 struct gb_lights_get_channel_flash_config_response conf;
814 struct led_flash_setting *fset;
815 int ret;
817 req.light_id = channel->light->id;
818 req.channel_id = channel->id;
820 ret = gb_operation_sync(connection,
821 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
822 &req, sizeof(req), &conf, sizeof(conf));
823 if (ret < 0)
824 return ret;
827 * Intensity constraints for flash related modes: flash, torch,
828 * indicator. They will be needed for v4l2 registration.
830 fset = &channel->intensity_uA;
831 fset->min = le32_to_cpu(conf.intensity_min_uA);
832 fset->max = le32_to_cpu(conf.intensity_max_uA);
833 fset->step = le32_to_cpu(conf.intensity_step_uA);
836 * On flash type, max brightness is set as the number of intensity steps
837 * available.
839 channel->led->max_brightness = (fset->max - fset->min) / fset->step;
841 /* Only the flash mode have the timeout constraints settings */
842 if (channel->mode & GB_CHANNEL_MODE_FLASH) {
843 fset = &channel->timeout_us;
844 fset->min = le32_to_cpu(conf.timeout_min_us);
845 fset->max = le32_to_cpu(conf.timeout_max_us);
846 fset->step = le32_to_cpu(conf.timeout_step_us);
849 return 0;
851 #else
852 static int gb_lights_channel_flash_config(struct gb_channel *channel)
854 struct gb_connection *connection = get_conn_from_channel(channel);
856 dev_err(&connection->bundle->dev, "no support for flash devices\n");
857 return 0;
860 static int __gb_lights_flash_led_register(struct gb_channel *channel)
862 return 0;
865 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
869 #endif
871 static int __gb_lights_led_register(struct gb_channel *channel)
873 struct gb_connection *connection = get_conn_from_channel(channel);
874 struct led_classdev *cdev = get_channel_cdev(channel);
875 int ret;
877 ret = led_classdev_register(&connection->bundle->dev, cdev);
878 if (ret < 0)
879 channel->led = NULL;
880 else
881 channel->is_registered = true;
882 return ret;
885 static int gb_lights_channel_register(struct gb_channel *channel)
887 /* Normal LED channel, just register in led classdev and we are done */
888 if (!is_channel_flash(channel))
889 return __gb_lights_led_register(channel);
892 * Flash Type need more work, register flash classdev, indicator as
893 * flash classdev, torch will be led classdev of the flash classdev.
895 if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
896 return __gb_lights_flash_led_register(channel);
898 return 0;
901 static void __gb_lights_led_unregister(struct gb_channel *channel)
903 struct led_classdev *cdev = get_channel_cdev(channel);
905 if (!channel->is_registered)
906 return;
908 led_classdev_unregister(cdev);
909 kfree(cdev->name);
910 cdev->name = NULL;
911 channel->led = NULL;
914 static void gb_lights_channel_unregister(struct gb_channel *channel)
916 /* The same as register, handle channels differently */
917 if (!is_channel_flash(channel)) {
918 __gb_lights_led_unregister(channel);
919 return;
922 if (channel->mode & GB_CHANNEL_MODE_TORCH)
923 __gb_lights_led_unregister(channel);
924 else
925 __gb_lights_flash_led_unregister(channel);
928 static int gb_lights_channel_config(struct gb_light *light,
929 struct gb_channel *channel)
931 struct gb_lights_get_channel_config_response conf;
932 struct gb_lights_get_channel_config_request req;
933 struct gb_connection *connection = get_conn_from_light(light);
934 struct led_classdev *cdev = get_channel_cdev(channel);
935 char *name;
936 int ret;
938 req.light_id = light->id;
939 req.channel_id = channel->id;
941 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
942 &req, sizeof(req), &conf, sizeof(conf));
943 if (ret < 0)
944 return ret;
946 channel->light = light;
947 channel->mode = le32_to_cpu(conf.mode);
948 channel->flags = le32_to_cpu(conf.flags);
949 channel->color = le32_to_cpu(conf.color);
950 channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
951 if (!channel->color_name)
952 return -ENOMEM;
953 channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
954 if (!channel->mode_name)
955 return -ENOMEM;
957 channel->led = cdev;
959 name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
960 channel->color_name, channel->mode_name);
961 if (!name)
962 return -ENOMEM;
964 cdev->name = name;
966 cdev->max_brightness = conf.max_brightness;
968 ret = channel_attr_groups_set(channel, cdev);
969 if (ret < 0)
970 return ret;
972 gb_lights_led_operations_set(channel, cdev);
975 * If it is not a flash related channel (flash, torch or indicator) we
976 * are done here. If not, continue and fetch flash related
977 * configurations.
979 if (!is_channel_flash(channel))
980 return ret;
982 light->has_flash = true;
984 return gb_lights_channel_flash_config(channel);
987 static int gb_lights_light_config(struct gb_lights *glights, u8 id)
989 struct gb_light *light = &glights->lights[id];
990 struct gb_lights_get_light_config_request req;
991 struct gb_lights_get_light_config_response conf;
992 int ret;
993 int i;
995 light->glights = glights;
996 light->id = id;
998 req.id = id;
1000 ret = gb_operation_sync(glights->connection,
1001 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
1002 &req, sizeof(req), &conf, sizeof(conf));
1003 if (ret < 0)
1004 return ret;
1006 if (!conf.channel_count)
1007 return -EINVAL;
1008 if (!strlen(conf.name))
1009 return -EINVAL;
1011 light->channels_count = conf.channel_count;
1012 light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1013 if (!light->name)
1014 return -ENOMEM;
1015 light->channels = kcalloc(light->channels_count,
1016 sizeof(struct gb_channel), GFP_KERNEL);
1017 if (!light->channels)
1018 return -ENOMEM;
1020 /* First we collect all the configurations for all channels */
1021 for (i = 0; i < light->channels_count; i++) {
1022 light->channels[i].id = i;
1023 ret = gb_lights_channel_config(light, &light->channels[i]);
1024 if (ret < 0)
1025 return ret;
1028 return 0;
1031 static int gb_lights_light_register(struct gb_light *light)
1033 int ret;
1034 int i;
1037 * Then, if everything went ok in getting configurations, we register
1038 * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1039 * found.
1041 for (i = 0; i < light->channels_count; i++) {
1042 ret = gb_lights_channel_register(&light->channels[i]);
1043 if (ret < 0)
1044 return ret;
1046 mutex_init(&light->channels[i].lock);
1049 light->ready = true;
1051 if (light->has_flash) {
1052 ret = gb_lights_light_v4l2_register(light);
1053 if (ret < 0) {
1054 light->has_flash = false;
1055 return ret;
1059 return 0;
1062 static void gb_lights_channel_free(struct gb_channel *channel)
1064 kfree(channel->attrs);
1065 kfree(channel->attr_group);
1066 kfree(channel->attr_groups);
1067 kfree(channel->color_name);
1068 kfree(channel->mode_name);
1069 mutex_destroy(&channel->lock);
1072 static void gb_lights_channel_release(struct gb_channel *channel)
1074 channel->releasing = true;
1076 gb_lights_channel_unregister(channel);
1078 gb_lights_channel_free(channel);
1081 static void gb_lights_light_release(struct gb_light *light)
1083 int i;
1085 light->ready = false;
1087 if (light->has_flash)
1088 gb_lights_light_v4l2_unregister(light);
1089 light->has_flash = false;
1091 for (i = 0; i < light->channels_count; i++)
1092 gb_lights_channel_release(&light->channels[i]);
1093 light->channels_count = 0;
1095 kfree(light->channels);
1096 light->channels = NULL;
1097 kfree(light->name);
1098 light->name = NULL;
1101 static void gb_lights_release(struct gb_lights *glights)
1103 int i;
1105 if (!glights)
1106 return;
1108 mutex_lock(&glights->lights_lock);
1109 if (!glights->lights)
1110 goto free_glights;
1112 for (i = 0; i < glights->lights_count; i++)
1113 gb_lights_light_release(&glights->lights[i]);
1115 kfree(glights->lights);
1117 free_glights:
1118 mutex_unlock(&glights->lights_lock);
1119 mutex_destroy(&glights->lights_lock);
1120 kfree(glights);
1123 static int gb_lights_get_count(struct gb_lights *glights)
1125 struct gb_lights_get_lights_response resp;
1126 int ret;
1128 ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1129 NULL, 0, &resp, sizeof(resp));
1130 if (ret < 0)
1131 return ret;
1133 if (!resp.lights_count)
1134 return -EINVAL;
1136 glights->lights_count = resp.lights_count;
1138 return 0;
1141 static int gb_lights_create_all(struct gb_lights *glights)
1143 struct gb_connection *connection = glights->connection;
1144 int ret;
1145 int i;
1147 mutex_lock(&glights->lights_lock);
1148 ret = gb_lights_get_count(glights);
1149 if (ret < 0)
1150 goto out;
1152 glights->lights = kcalloc(glights->lights_count,
1153 sizeof(struct gb_light), GFP_KERNEL);
1154 if (!glights->lights) {
1155 ret = -ENOMEM;
1156 goto out;
1159 for (i = 0; i < glights->lights_count; i++) {
1160 ret = gb_lights_light_config(glights, i);
1161 if (ret < 0) {
1162 dev_err(&connection->bundle->dev,
1163 "Fail to configure lights device\n");
1164 goto out;
1168 out:
1169 mutex_unlock(&glights->lights_lock);
1170 return ret;
1173 static int gb_lights_register_all(struct gb_lights *glights)
1175 struct gb_connection *connection = glights->connection;
1176 int ret = 0;
1177 int i;
1179 mutex_lock(&glights->lights_lock);
1180 for (i = 0; i < glights->lights_count; i++) {
1181 ret = gb_lights_light_register(&glights->lights[i]);
1182 if (ret < 0) {
1183 dev_err(&connection->bundle->dev,
1184 "Fail to enable lights device\n");
1185 break;
1189 mutex_unlock(&glights->lights_lock);
1190 return ret;
1193 static int gb_lights_request_handler(struct gb_operation *op)
1195 struct gb_connection *connection = op->connection;
1196 struct device *dev = &connection->bundle->dev;
1197 struct gb_lights *glights = gb_connection_get_data(connection);
1198 struct gb_light *light;
1199 struct gb_message *request;
1200 struct gb_lights_event_request *payload;
1201 int ret = 0;
1202 u8 light_id;
1203 u8 event;
1205 if (op->type != GB_LIGHTS_TYPE_EVENT) {
1206 dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1207 return -EINVAL;
1210 request = op->request;
1212 if (request->payload_size < sizeof(*payload)) {
1213 dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1214 request->payload_size, sizeof(*payload));
1215 return -EINVAL;
1218 payload = request->payload;
1219 light_id = payload->light_id;
1221 if (light_id >= glights->lights_count ||
1222 !glights->lights[light_id].ready) {
1223 dev_err(dev, "Event received for unconfigured light id: %d\n",
1224 light_id);
1225 return -EINVAL;
1228 event = payload->event;
1230 if (event & GB_LIGHTS_LIGHT_CONFIG) {
1231 light = &glights->lights[light_id];
1233 mutex_lock(&glights->lights_lock);
1234 gb_lights_light_release(light);
1235 ret = gb_lights_light_config(glights, light_id);
1236 if (!ret)
1237 ret = gb_lights_light_register(light);
1238 if (ret < 0)
1239 gb_lights_light_release(light);
1240 mutex_unlock(&glights->lights_lock);
1243 return ret;
1246 static int gb_lights_probe(struct gb_bundle *bundle,
1247 const struct greybus_bundle_id *id)
1249 struct greybus_descriptor_cport *cport_desc;
1250 struct gb_connection *connection;
1251 struct gb_lights *glights;
1252 int ret;
1254 if (bundle->num_cports != 1)
1255 return -ENODEV;
1257 cport_desc = &bundle->cport_desc[0];
1258 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1259 return -ENODEV;
1261 glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1262 if (!glights)
1263 return -ENOMEM;
1265 mutex_init(&glights->lights_lock);
1267 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1268 gb_lights_request_handler);
1269 if (IS_ERR(connection)) {
1270 ret = PTR_ERR(connection);
1271 goto out;
1274 glights->connection = connection;
1275 gb_connection_set_data(connection, glights);
1277 greybus_set_drvdata(bundle, glights);
1279 /* We aren't ready to receive an incoming request yet */
1280 ret = gb_connection_enable_tx(connection);
1281 if (ret)
1282 goto error_connection_destroy;
1285 * Setup all the lights devices over this connection, if anything goes
1286 * wrong tear down all lights
1288 ret = gb_lights_create_all(glights);
1289 if (ret < 0)
1290 goto error_connection_disable;
1292 /* We are ready to receive an incoming request now, enable RX as well */
1293 ret = gb_connection_enable(connection);
1294 if (ret)
1295 goto error_connection_disable;
1297 /* Enable & register lights */
1298 ret = gb_lights_register_all(glights);
1299 if (ret < 0)
1300 goto error_connection_disable;
1302 gb_pm_runtime_put_autosuspend(bundle);
1304 return 0;
1306 error_connection_disable:
1307 gb_connection_disable(connection);
1308 error_connection_destroy:
1309 gb_connection_destroy(connection);
1310 out:
1311 gb_lights_release(glights);
1312 return ret;
1315 static void gb_lights_disconnect(struct gb_bundle *bundle)
1317 struct gb_lights *glights = greybus_get_drvdata(bundle);
1319 if (gb_pm_runtime_get_sync(bundle))
1320 gb_pm_runtime_get_noresume(bundle);
1322 gb_connection_disable(glights->connection);
1323 gb_connection_destroy(glights->connection);
1325 gb_lights_release(glights);
1328 static const struct greybus_bundle_id gb_lights_id_table[] = {
1329 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1332 MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1334 static struct greybus_driver gb_lights_driver = {
1335 .name = "lights",
1336 .probe = gb_lights_probe,
1337 .disconnect = gb_lights_disconnect,
1338 .id_table = gb_lights_id_table,
1340 module_greybus_driver(gb_lights_driver);
1342 MODULE_DESCRIPTION("Greybus Lights protocol driver");
1343 MODULE_LICENSE("GPL v2");