1
2
3
4
5
6
7
8
9
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>
17
18#include "greybus.h"
19#include "greybus_protocols.h"
20
21#define NAMES_MAX 32
22
23struct gb_channel {
24 u8 id;
25 u32 flags;
26 u32 color;
27 char *color_name;
28 u8 fade_in;
29 u8 fade_out;
30 u32 mode;
31 char *mode_name;
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;
40#else
41 struct led_classdev cled;
42#endif
43 struct gb_light *light;
44 bool is_registered;
45 bool releasing;
46 bool strobe_state;
47 bool active;
48 struct mutex lock;
49};
50
51struct gb_light {
52 u8 id;
53 char *name;
54 struct gb_lights *glights;
55 u32 flags;
56 u8 channels_count;
57 struct gb_channel *channels;
58 bool has_flash;
59 bool ready;
60#if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
61 struct v4l2_flash *v4l2_flash;
62#endif
63};
64
65struct gb_lights {
66 struct gb_connection *connection;
67 u8 lights_count;
68 struct gb_light *lights;
69 struct mutex lights_lock;
70};
71
72static void gb_lights_channel_free(struct gb_channel *channel);
73
74static struct gb_connection *get_conn_from_channel(struct gb_channel *channel)
75{
76 return channel->light->glights->connection;
77}
78
79static struct gb_connection *get_conn_from_light(struct gb_light *light)
80{
81 return light->glights->connection;
82}
83
84static bool is_channel_flash(struct gb_channel *channel)
85{
86 return !!(channel->mode & (GB_CHANNEL_MODE_FLASH | GB_CHANNEL_MODE_TORCH
87 | GB_CHANNEL_MODE_INDICATOR));
88}
89
90#if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
91static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
92{
93 struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
94
95 return container_of(fled_cdev, struct gb_channel, fled);
96}
97
98static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
99{
100 return &channel->fled.led_cdev;
101}
102
103static struct gb_channel *get_channel_from_mode(struct gb_light *light,
104 u32 mode)
105{
106 struct gb_channel *channel = NULL;
107 int i;
108
109 for (i = 0; i < light->channels_count; i++) {
110 channel = &light->channels[i];
111 if (channel && channel->mode == mode)
112 break;
113 }
114 return channel;
115}
116
117static int __gb_lights_flash_intensity_set(struct gb_channel *channel,
118 u32 intensity)
119{
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;
123 int ret;
124
125 if (channel->releasing)
126 return -ESHUTDOWN;
127
128 ret = gb_pm_runtime_get_sync(bundle);
129 if (ret < 0)
130 return ret;
131
132 req.light_id = channel->light->id;
133 req.channel_id = channel->id;
134 req.intensity_uA = cpu_to_le32(intensity);
135
136 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_INTENSITY,
137 &req, sizeof(req), NULL, 0);
138
139 gb_pm_runtime_put_autosuspend(bundle);
140
141 return ret;
142}
143
144static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
145{
146 u32 intensity;
147
148
149 if (channel->mode & GB_CHANNEL_MODE_FLASH)
150 channel = get_channel_from_mode(channel->light,
151 GB_CHANNEL_MODE_TORCH);
152
153
154 intensity = channel->intensity_uA.min +
155 (channel->intensity_uA.step * channel->led->brightness);
156
157 return __gb_lights_flash_intensity_set(channel, intensity);
158}
159#else
160static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
161{
162 return container_of(cdev, struct gb_channel, cled);
163}
164
165static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
166{
167 return &channel->cled;
168}
169
170static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
171{
172 return 0;
173}
174#endif
175
176static int gb_lights_color_set(struct gb_channel *channel, u32 color);
177static int gb_lights_fade_set(struct gb_channel *channel);
178
179static void led_lock(struct led_classdev *cdev)
180{
181 mutex_lock(&cdev->led_access);
182}
183
184static void led_unlock(struct led_classdev *cdev)
185{
186 mutex_unlock(&cdev->led_access);
187}
188
189#define gb_lights_fade_attr(__dir) \
190static ssize_t fade_##__dir##_show(struct device *dev, \
191 struct device_attribute *attr, \
192 char *buf) \
193{ \
194 struct led_classdev *cdev = dev_get_drvdata(dev); \
195 struct gb_channel *channel = get_channel_from_cdev(cdev); \
196 \
197 return sprintf(buf, "%u\n", channel->fade_##__dir); \
198} \
199 \
200static ssize_t fade_##__dir##_store(struct device *dev, \
201 struct device_attribute *attr, \
202 const char *buf, size_t size) \
203{ \
204 struct led_classdev *cdev = dev_get_drvdata(dev); \
205 struct gb_channel *channel = get_channel_from_cdev(cdev); \
206 u8 fade; \
207 int ret; \
208 \
209 led_lock(cdev); \
210 if (led_sysfs_is_disabled(cdev)) { \
211 ret = -EBUSY; \
212 goto unlock; \
213 } \
214 \
215 ret = kstrtou8(buf, 0, &fade); \
216 if (ret < 0) { \
217 dev_err(dev, "could not parse fade value %d\n", ret); \
218 goto unlock; \
219 } \
220 if (channel->fade_##__dir == fade) \
221 goto unlock; \
222 channel->fade_##__dir = fade; \
223 \
224 ret = gb_lights_fade_set(channel); \
225 if (ret < 0) \
226 goto unlock; \
227 \
228 ret = size; \
229unlock: \
230 led_unlock(cdev); \
231 return ret; \
232} \
233static DEVICE_ATTR_RW(fade_##__dir)
234
235gb_lights_fade_attr(in);
236gb_lights_fade_attr(out);
237
238static ssize_t color_show(struct device *dev, struct device_attribute *attr,
239 char *buf)
240{
241 struct led_classdev *cdev = dev_get_drvdata(dev);
242 struct gb_channel *channel = get_channel_from_cdev(cdev);
243
244 return sprintf(buf, "0x%08x\n", channel->color);
245}
246
247static ssize_t color_store(struct device *dev, struct device_attribute *attr,
248 const char *buf, size_t size)
249{
250 struct led_classdev *cdev = dev_get_drvdata(dev);
251 struct gb_channel *channel = get_channel_from_cdev(cdev);
252 u32 color;
253 int ret;
254
255 led_lock(cdev);
256 if (led_sysfs_is_disabled(cdev)) {
257 ret = -EBUSY;
258 goto unlock;
259 }
260 ret = kstrtou32(buf, 0, &color);
261 if (ret < 0) {
262 dev_err(dev, "could not parse color value %d\n", ret);
263 goto unlock;
264 }
265
266 ret = gb_lights_color_set(channel, color);
267 if (ret < 0)
268 goto unlock;
269
270 channel->color = color;
271 ret = size;
272unlock:
273 led_unlock(cdev);
274 return ret;
275}
276static DEVICE_ATTR_RW(color);
277
278static int channel_attr_groups_set(struct gb_channel *channel,
279 struct led_classdev *cdev)
280{
281 int attr = 0;
282 int size = 0;
283
284 if (channel->flags & GB_LIGHT_CHANNEL_MULTICOLOR)
285 size++;
286 if (channel->flags & GB_LIGHT_CHANNEL_FADER)
287 size += 2;
288
289 if (!size)
290 return 0;
291
292
293 channel->attrs = kcalloc(size + 1, sizeof(*channel->attrs), GFP_KERNEL);
294 if (!channel->attrs)
295 return -ENOMEM;
296 channel->attr_group = kcalloc(1, sizeof(*channel->attr_group),
297 GFP_KERNEL);
298 if (!channel->attr_group)
299 return -ENOMEM;
300 channel->attr_groups = kcalloc(2, sizeof(*channel->attr_groups),
301 GFP_KERNEL);
302 if (!channel->attr_groups)
303 return -ENOMEM;
304
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;
310 }
311
312 channel->attr_group->attrs = channel->attrs;
313
314 channel->attr_groups[0] = channel->attr_group;
315
316 cdev->groups = channel->attr_groups;
317
318 return 0;
319}
320
321static int gb_lights_fade_set(struct gb_channel *channel)
322{
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;
326 int ret;
327
328 if (channel->releasing)
329 return -ESHUTDOWN;
330
331 ret = gb_pm_runtime_get_sync(bundle);
332 if (ret < 0)
333 return ret;
334
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);
341
342 gb_pm_runtime_put_autosuspend(bundle);
343
344 return ret;
345}
346
347static int gb_lights_color_set(struct gb_channel *channel, u32 color)
348{
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;
352 int ret;
353
354 if (channel->releasing)
355 return -ESHUTDOWN;
356
357 ret = gb_pm_runtime_get_sync(bundle);
358 if (ret < 0)
359 return ret;
360
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);
366
367 gb_pm_runtime_put_autosuspend(bundle);
368
369 return ret;
370}
371
372static int __gb_lights_led_brightness_set(struct gb_channel *channel)
373{
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;
377 bool old_active;
378 int ret;
379
380 mutex_lock(&channel->lock);
381 ret = gb_pm_runtime_get_sync(bundle);
382 if (ret < 0)
383 goto out_unlock;
384
385 old_active = channel->active;
386
387 req.light_id = channel->light->id;
388 req.channel_id = channel->id;
389 req.brightness = (u8)channel->led->brightness;
390
391 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BRIGHTNESS,
392 &req, sizeof(req), NULL, 0);
393 if (ret < 0)
394 goto out_pm_put;
395
396 if (channel->led->brightness)
397 channel->active = true;
398 else
399 channel->active = false;
400
401
402 if (!old_active && channel->active)
403 goto out_unlock;
404
405
406
407
408
409 if (old_active && !channel->active)
410 gb_pm_runtime_put_autosuspend(bundle);
411
412out_pm_put:
413 gb_pm_runtime_put_autosuspend(bundle);
414out_unlock:
415 mutex_unlock(&channel->lock);
416
417 return ret;
418}
419
420static int __gb_lights_brightness_set(struct gb_channel *channel)
421{
422 int ret;
423
424 if (channel->releasing)
425 return 0;
426
427 if (is_channel_flash(channel))
428 ret = __gb_lights_flash_brightness_set(channel);
429 else
430 ret = __gb_lights_led_brightness_set(channel);
431
432 return ret;
433}
434
435static int gb_brightness_set(struct led_classdev *cdev,
436 enum led_brightness value)
437{
438 struct gb_channel *channel = get_channel_from_cdev(cdev);
439
440 channel->led->brightness = value;
441
442 return __gb_lights_brightness_set(channel);
443}
444
445static enum led_brightness gb_brightness_get(struct led_classdev *cdev)
446
447{
448 struct gb_channel *channel = get_channel_from_cdev(cdev);
449
450 return channel->led->brightness;
451}
452
453static int gb_blink_set(struct led_classdev *cdev, unsigned long *delay_on,
454 unsigned long *delay_off)
455{
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;
460 bool old_active;
461 int ret;
462
463 if (channel->releasing)
464 return -ESHUTDOWN;
465
466 if (!delay_on || !delay_off)
467 return -EINVAL;
468
469 mutex_lock(&channel->lock);
470 ret = gb_pm_runtime_get_sync(bundle);
471 if (ret < 0)
472 goto out_unlock;
473
474 old_active = channel->active;
475
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);
480
481 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_BLINK, &req,
482 sizeof(req), NULL, 0);
483 if (ret < 0)
484 goto out_pm_put;
485
486 if (*delay_on)
487 channel->active = true;
488 else
489 channel->active = false;
490
491
492 if (!old_active && channel->active)
493 goto out_unlock;
494
495
496
497
498
499 if (old_active && !channel->active)
500 gb_pm_runtime_put_autosuspend(bundle);
501
502out_pm_put:
503 gb_pm_runtime_put_autosuspend(bundle);
504out_unlock:
505 mutex_unlock(&channel->lock);
506
507 return ret;
508}
509
510static void gb_lights_led_operations_set(struct gb_channel *channel,
511 struct led_classdev *cdev)
512{
513 cdev->brightness_get = gb_brightness_get;
514 cdev->brightness_set_blocking = gb_brightness_set;
515
516 if (channel->flags & GB_LIGHT_CHANNEL_BLINK)
517 cdev->blink_set = gb_blink_set;
518}
519
520#if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
521
522static const struct v4l2_flash_ops v4l2_flash_ops;
523
524static void __gb_lights_channel_v4l2_config(struct led_flash_setting *channel_s,
525 struct led_flash_setting *v4l2_s)
526{
527 v4l2_s->min = channel_s->min;
528 v4l2_s->max = channel_s->max;
529 v4l2_s->step = channel_s->step;
530
531 v4l2_s->val = channel_s->max;
532}
533
534static int gb_lights_light_v4l2_register(struct gb_light *light)
535{
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;
542 int ret = 0;
543
544 sd_cfg = kcalloc(1, sizeof(*sd_cfg), GFP_KERNEL);
545 if (!sd_cfg)
546 return -ENOMEM;
547
548 channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
549 if (channel_torch)
550 __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
551 &sd_cfg->torch_intensity);
552
553 channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
554 if (channel_ind) {
555 __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
556 &sd_cfg->indicator_intensity);
557 iled = &channel_ind->fled;
558 }
559
560 channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
561 WARN_ON(!channel_flash);
562
563 fled = &channel_flash->fled;
564
565 snprintf(sd_cfg->dev_name, sizeof(sd_cfg->dev_name), "%s", light->name);
566
567
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;
573
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);
578 goto out_free;
579 }
580
581 return ret;
582
583out_free:
584 kfree(sd_cfg);
585 return ret;
586}
587
588static void gb_lights_light_v4l2_unregister(struct gb_light *light)
589{
590 v4l2_flash_release(light->v4l2_flash);
591}
592#else
593static int gb_lights_light_v4l2_register(struct gb_light *light)
594{
595 struct gb_connection *connection = get_conn_from_light(light);
596
597 dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
598 return 0;
599}
600
601static void gb_lights_light_v4l2_unregister(struct gb_light *light)
602{
603}
604#endif
605
606#if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
607
608static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
609 u32 brightness)
610{
611 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
612 fled);
613 int ret;
614
615 ret = __gb_lights_flash_intensity_set(channel, brightness);
616 if (ret < 0)
617 return ret;
618
619 fcdev->brightness.val = brightness;
620
621 return 0;
622}
623
624static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
625 u32 *brightness)
626{
627 *brightness = fcdev->brightness.val;
628
629 return 0;
630}
631
632static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
633 bool state)
634{
635 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
636 fled);
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;
640 int ret;
641
642 if (channel->releasing)
643 return -ESHUTDOWN;
644
645 ret = gb_pm_runtime_get_sync(bundle);
646 if (ret < 0)
647 return ret;
648
649 req.light_id = channel->light->id;
650 req.channel_id = channel->id;
651 req.state = state ? 1 : 0;
652
653 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
654 &req, sizeof(req), NULL, 0);
655 if (!ret)
656 channel->strobe_state = state;
657
658 gb_pm_runtime_put_autosuspend(bundle);
659
660 return ret;
661}
662
663static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
664 bool *state)
665{
666 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
667 fled);
668
669 *state = channel->strobe_state;
670 return 0;
671}
672
673static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
674 u32 timeout)
675{
676 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
677 fled);
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;
681 int ret;
682
683 if (channel->releasing)
684 return -ESHUTDOWN;
685
686 ret = gb_pm_runtime_get_sync(bundle);
687 if (ret < 0)
688 return ret;
689
690 req.light_id = channel->light->id;
691 req.channel_id = channel->id;
692 req.timeout_us = cpu_to_le32(timeout);
693
694 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
695 &req, sizeof(req), NULL, 0);
696 if (!ret)
697 fcdev->timeout.val = timeout;
698
699 gb_pm_runtime_put_autosuspend(bundle);
700
701 return ret;
702}
703
704static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
705 u32 *fault)
706{
707 struct gb_channel *channel = container_of(fcdev, struct gb_channel,
708 fled);
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;
713 int ret;
714
715 if (channel->releasing)
716 return -ESHUTDOWN;
717
718 ret = gb_pm_runtime_get_sync(bundle);
719 if (ret < 0)
720 return ret;
721
722 req.light_id = channel->light->id;
723 req.channel_id = channel->id;
724
725 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
726 &req, sizeof(req), &resp, sizeof(resp));
727 if (!ret)
728 *fault = le32_to_cpu(resp.fault);
729
730 gb_pm_runtime_put_autosuspend(bundle);
731
732 return ret;
733}
734
735static 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,
742};
743
744static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
745 struct gb_channel *channel_torch)
746{
747 char *name;
748
749
750 if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
751 return 0;
752
753
754 channel->led->max_brightness = channel_torch->led->max_brightness;
755
756
757 name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
758 channel_torch->mode_name);
759 if (!name)
760 return -ENOMEM;
761 kfree(channel->led->name);
762 channel->led->name = name;
763
764 channel_torch->led = channel->led;
765
766 return 0;
767}
768
769static int __gb_lights_flash_led_register(struct gb_channel *channel)
770{
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;
775 int ret;
776
777 fled->ops = &gb_lights_flash_ops;
778
779 fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
780
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;
786
787
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;
794 }
795
796
797
798
799
800 channel_torch = get_channel_from_mode(channel->light,
801 GB_CHANNEL_MODE_TORCH);
802 if (channel_torch) {
803 ret = __gb_lights_channel_torch_attach(channel, channel_torch);
804 if (ret < 0)
805 goto fail;
806 }
807
808 ret = led_classdev_flash_register(&connection->bundle->dev, fled);
809 if (ret < 0)
810 goto fail;
811
812 channel->is_registered = true;
813 return 0;
814fail:
815 channel->led = NULL;
816 return ret;
817}
818
819static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
820{
821 if (!channel->is_registered)
822 return;
823
824 led_classdev_flash_unregister(&channel->fled);
825}
826
827static int gb_lights_channel_flash_config(struct gb_channel *channel)
828{
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;
833 int ret;
834
835 req.light_id = channel->light->id;
836 req.channel_id = channel->id;
837
838 ret = gb_operation_sync(connection,
839 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
840 &req, sizeof(req), &conf, sizeof(conf));
841 if (ret < 0)
842 return ret;
843
844
845
846
847
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);
852
853
854
855
856
857 channel->led->max_brightness = (fset->max - fset->min) / fset->step;
858
859
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);
865 }
866
867 return 0;
868}
869#else
870static int gb_lights_channel_flash_config(struct gb_channel *channel)
871{
872 struct gb_connection *connection = get_conn_from_channel(channel);
873
874 dev_err(&connection->bundle->dev, "no support for flash devices\n");
875 return 0;
876}
877
878static int __gb_lights_flash_led_register(struct gb_channel *channel)
879{
880 return 0;
881}
882
883static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
884{
885}
886
887#endif
888
889static int __gb_lights_led_register(struct gb_channel *channel)
890{
891 struct gb_connection *connection = get_conn_from_channel(channel);
892 struct led_classdev *cdev = get_channel_cdev(channel);
893 int ret;
894
895 ret = led_classdev_register(&connection->bundle->dev, cdev);
896 if (ret < 0)
897 channel->led = NULL;
898 else
899 channel->is_registered = true;
900 return ret;
901}
902
903static int gb_lights_channel_register(struct gb_channel *channel)
904{
905
906 if (!is_channel_flash(channel))
907 return __gb_lights_led_register(channel);
908
909
910
911
912
913 if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
914 return __gb_lights_flash_led_register(channel);
915
916 return 0;
917}
918
919static void __gb_lights_led_unregister(struct gb_channel *channel)
920{
921 struct led_classdev *cdev = get_channel_cdev(channel);
922
923 if (!channel->is_registered)
924 return;
925
926 led_classdev_unregister(cdev);
927 channel->led = NULL;
928}
929
930static void gb_lights_channel_unregister(struct gb_channel *channel)
931{
932
933 if (!is_channel_flash(channel)) {
934 __gb_lights_led_unregister(channel);
935 return;
936 }
937
938 if (channel->mode & GB_CHANNEL_MODE_TORCH)
939 __gb_lights_led_unregister(channel);
940 else
941 __gb_lights_flash_led_unregister(channel);
942}
943
944static int gb_lights_channel_config(struct gb_light *light,
945 struct gb_channel *channel)
946{
947 struct gb_lights_get_channel_config_response conf;
948 struct gb_lights_get_channel_config_request req;
949 struct gb_connection *connection = get_conn_from_light(light);
950 struct led_classdev *cdev = get_channel_cdev(channel);
951 char *name;
952 int ret;
953
954 req.light_id = light->id;
955 req.channel_id = channel->id;
956
957 ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
958 &req, sizeof(req), &conf, sizeof(conf));
959 if (ret < 0)
960 return ret;
961
962 channel->light = light;
963 channel->mode = le32_to_cpu(conf.mode);
964 channel->flags = le32_to_cpu(conf.flags);
965 channel->color = le32_to_cpu(conf.color);
966 channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
967 if (!channel->color_name)
968 return -ENOMEM;
969 channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
970 if (!channel->mode_name)
971 return -ENOMEM;
972
973 channel->led = cdev;
974
975 name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
976 channel->color_name, channel->mode_name);
977 if (!name)
978 return -ENOMEM;
979
980 cdev->name = name;
981
982 cdev->max_brightness = conf.max_brightness;
983
984 ret = channel_attr_groups_set(channel, cdev);
985 if (ret < 0)
986 return ret;
987
988 gb_lights_led_operations_set(channel, cdev);
989
990
991
992
993
994
995 if (!is_channel_flash(channel))
996 return ret;
997
998 light->has_flash = true;
999
1000 ret = gb_lights_channel_flash_config(channel);
1001 if (ret < 0)
1002 return ret;
1003
1004 return ret;
1005}
1006
1007static int gb_lights_light_config(struct gb_lights *glights, u8 id)
1008{
1009 struct gb_light *light = &glights->lights[id];
1010 struct gb_lights_get_light_config_request req;
1011 struct gb_lights_get_light_config_response conf;
1012 int ret;
1013 int i;
1014
1015 light->glights = glights;
1016 light->id = id;
1017
1018 req.id = id;
1019
1020 ret = gb_operation_sync(glights->connection,
1021 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
1022 &req, sizeof(req), &conf, sizeof(conf));
1023 if (ret < 0)
1024 return ret;
1025
1026 if (!conf.channel_count)
1027 return -EINVAL;
1028 if (!strlen(conf.name))
1029 return -EINVAL;
1030
1031 light->channels_count = conf.channel_count;
1032 light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1033
1034 light->channels = kzalloc(light->channels_count *
1035 sizeof(struct gb_channel), GFP_KERNEL);
1036 if (!light->channels)
1037 return -ENOMEM;
1038
1039
1040 for (i = 0; i < light->channels_count; i++) {
1041 light->channels[i].id = i;
1042 ret = gb_lights_channel_config(light, &light->channels[i]);
1043 if (ret < 0)
1044 return ret;
1045 }
1046
1047 return 0;
1048}
1049
1050static int gb_lights_light_register(struct gb_light *light)
1051{
1052 int ret;
1053 int i;
1054
1055
1056
1057
1058
1059
1060 for (i = 0; i < light->channels_count; i++) {
1061 ret = gb_lights_channel_register(&light->channels[i]);
1062 if (ret < 0)
1063 return ret;
1064
1065 mutex_init(&light->channels[i].lock);
1066 }
1067
1068 light->ready = true;
1069
1070 if (light->has_flash) {
1071 ret = gb_lights_light_v4l2_register(light);
1072 if (ret < 0) {
1073 light->has_flash = false;
1074 return ret;
1075 }
1076 }
1077
1078 return 0;
1079}
1080
1081static void gb_lights_channel_free(struct gb_channel *channel)
1082{
1083 kfree(channel->attrs);
1084 kfree(channel->attr_group);
1085 kfree(channel->attr_groups);
1086 kfree(channel->color_name);
1087 kfree(channel->mode_name);
1088 mutex_destroy(&channel->lock);
1089}
1090
1091static void gb_lights_channel_release(struct gb_channel *channel)
1092{
1093 channel->releasing = true;
1094
1095 gb_lights_channel_unregister(channel);
1096
1097 gb_lights_channel_free(channel);
1098}
1099
1100static void gb_lights_light_release(struct gb_light *light)
1101{
1102 int i;
1103 int count;
1104
1105 light->ready = false;
1106
1107 count = light->channels_count;
1108
1109 if (light->has_flash)
1110 gb_lights_light_v4l2_unregister(light);
1111
1112 for (i = 0; i < count; i++) {
1113 gb_lights_channel_release(&light->channels[i]);
1114 light->channels_count--;
1115 }
1116 kfree(light->channels);
1117 kfree(light->name);
1118}
1119
1120static void gb_lights_release(struct gb_lights *glights)
1121{
1122 int i;
1123
1124 if (!glights)
1125 return;
1126
1127 mutex_lock(&glights->lights_lock);
1128 if (!glights->lights)
1129 goto free_glights;
1130
1131 for (i = 0; i < glights->lights_count; i++)
1132 gb_lights_light_release(&glights->lights[i]);
1133
1134 kfree(glights->lights);
1135
1136free_glights:
1137 mutex_unlock(&glights->lights_lock);
1138 mutex_destroy(&glights->lights_lock);
1139 kfree(glights);
1140}
1141
1142static int gb_lights_get_count(struct gb_lights *glights)
1143{
1144 struct gb_lights_get_lights_response resp;
1145 int ret;
1146
1147 ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1148 NULL, 0, &resp, sizeof(resp));
1149 if (ret < 0)
1150 return ret;
1151
1152 if (!resp.lights_count)
1153 return -EINVAL;
1154
1155 glights->lights_count = resp.lights_count;
1156
1157 return 0;
1158}
1159
1160static int gb_lights_create_all(struct gb_lights *glights)
1161{
1162 struct gb_connection *connection = glights->connection;
1163 int ret;
1164 int i;
1165
1166 mutex_lock(&glights->lights_lock);
1167 ret = gb_lights_get_count(glights);
1168 if (ret < 0)
1169 goto out;
1170
1171 glights->lights = kzalloc(glights->lights_count *
1172 sizeof(struct gb_light), GFP_KERNEL);
1173 if (!glights->lights) {
1174 ret = -ENOMEM;
1175 goto out;
1176 }
1177
1178 for (i = 0; i < glights->lights_count; i++) {
1179 ret = gb_lights_light_config(glights, i);
1180 if (ret < 0) {
1181 dev_err(&connection->bundle->dev,
1182 "Fail to configure lights device\n");
1183 goto out;
1184 }
1185 }
1186
1187out:
1188 mutex_unlock(&glights->lights_lock);
1189 return ret;
1190}
1191
1192static int gb_lights_register_all(struct gb_lights *glights)
1193{
1194 struct gb_connection *connection = glights->connection;
1195 int ret = 0;
1196 int i;
1197
1198 mutex_lock(&glights->lights_lock);
1199 for (i = 0; i < glights->lights_count; i++) {
1200 ret = gb_lights_light_register(&glights->lights[i]);
1201 if (ret < 0) {
1202 dev_err(&connection->bundle->dev,
1203 "Fail to enable lights device\n");
1204 break;
1205 }
1206 }
1207
1208 mutex_unlock(&glights->lights_lock);
1209 return ret;
1210}
1211
1212static int gb_lights_request_handler(struct gb_operation *op)
1213{
1214 struct gb_connection *connection = op->connection;
1215 struct device *dev = &connection->bundle->dev;
1216 struct gb_lights *glights = gb_connection_get_data(connection);
1217 struct gb_light *light;
1218 struct gb_message *request;
1219 struct gb_lights_event_request *payload;
1220 int ret = 0;
1221 u8 light_id;
1222 u8 event;
1223
1224 if (op->type != GB_LIGHTS_TYPE_EVENT) {
1225 dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1226 return -EINVAL;
1227 }
1228
1229 request = op->request;
1230
1231 if (request->payload_size < sizeof(*payload)) {
1232 dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1233 request->payload_size, sizeof(*payload));
1234 return -EINVAL;
1235 }
1236
1237 payload = request->payload;
1238 light_id = payload->light_id;
1239
1240 if (light_id >= glights->lights_count ||
1241 !glights->lights[light_id].ready) {
1242 dev_err(dev, "Event received for unconfigured light id: %d\n",
1243 light_id);
1244 return -EINVAL;
1245 }
1246
1247 event = payload->event;
1248
1249 if (event & GB_LIGHTS_LIGHT_CONFIG) {
1250 light = &glights->lights[light_id];
1251
1252 mutex_lock(&glights->lights_lock);
1253 gb_lights_light_release(light);
1254 ret = gb_lights_light_config(glights, light_id);
1255 if (!ret)
1256 ret = gb_lights_light_register(light);
1257 if (ret < 0)
1258 gb_lights_light_release(light);
1259 mutex_unlock(&glights->lights_lock);
1260 }
1261
1262 return ret;
1263}
1264
1265static int gb_lights_probe(struct gb_bundle *bundle,
1266 const struct greybus_bundle_id *id)
1267{
1268 struct greybus_descriptor_cport *cport_desc;
1269 struct gb_connection *connection;
1270 struct gb_lights *glights;
1271 int ret;
1272
1273 if (bundle->num_cports != 1)
1274 return -ENODEV;
1275
1276 cport_desc = &bundle->cport_desc[0];
1277 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1278 return -ENODEV;
1279
1280 glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1281 if (!glights)
1282 return -ENOMEM;
1283
1284 mutex_init(&glights->lights_lock);
1285
1286 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1287 gb_lights_request_handler);
1288 if (IS_ERR(connection)) {
1289 ret = PTR_ERR(connection);
1290 goto out;
1291 }
1292
1293 glights->connection = connection;
1294 gb_connection_set_data(connection, glights);
1295
1296 greybus_set_drvdata(bundle, glights);
1297
1298
1299 ret = gb_connection_enable_tx(connection);
1300 if (ret)
1301 goto error_connection_destroy;
1302
1303
1304
1305
1306
1307 ret = gb_lights_create_all(glights);
1308 if (ret < 0)
1309 goto error_connection_disable;
1310
1311
1312 ret = gb_connection_enable(connection);
1313 if (ret)
1314 goto error_connection_disable;
1315
1316
1317 ret = gb_lights_register_all(glights);
1318 if (ret < 0)
1319 goto error_connection_disable;
1320
1321 gb_pm_runtime_put_autosuspend(bundle);
1322
1323 return 0;
1324
1325error_connection_disable:
1326 gb_connection_disable(connection);
1327error_connection_destroy:
1328 gb_connection_destroy(connection);
1329out:
1330 gb_lights_release(glights);
1331 return ret;
1332}
1333
1334static void gb_lights_disconnect(struct gb_bundle *bundle)
1335{
1336 struct gb_lights *glights = greybus_get_drvdata(bundle);
1337
1338 if (gb_pm_runtime_get_sync(bundle))
1339 gb_pm_runtime_get_noresume(bundle);
1340
1341 gb_connection_disable(glights->connection);
1342 gb_connection_destroy(glights->connection);
1343
1344 gb_lights_release(glights);
1345}
1346
1347static const struct greybus_bundle_id gb_lights_id_table[] = {
1348 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1349 { }
1350};
1351MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1352
1353static struct greybus_driver gb_lights_driver = {
1354 .name = "lights",
1355 .probe = gb_lights_probe,
1356 .disconnect = gb_lights_disconnect,
1357 .id_table = gb_lights_id_table,
1358};
1359module_greybus_driver(gb_lights_driver);
1360
1361MODULE_LICENSE("GPL v2");
1362