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