GNU Linux-libre 5.10.219-gnu1
[releases.git] / drivers / staging / greybus / light.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Greybus Lights protocol driver.
4  *
5  * Copyright 2015 Google Inc.
6  * Copyright 2015 Linaro Ltd.
7  */
8
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>
16
17 #define NAMES_MAX       32
18
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 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
33         struct led_classdev_flash       fled;
34         struct led_flash_setting        intensity_uA;
35         struct led_flash_setting        timeout_us;
36 #else
37         struct led_classdev             cled;
38 #endif
39         struct gb_light                 *light;
40         bool                            is_registered;
41         bool                            releasing;
42         bool                            strobe_state;
43         bool                            active;
44         struct mutex                    lock;
45 };
46
47 struct gb_light {
48         u8                      id;
49         char                    *name;
50         struct gb_lights        *glights;
51         u32                     flags;
52         u8                      channels_count;
53         struct gb_channel       *channels;
54         bool                    has_flash;
55         bool                    ready;
56 #if IS_REACHABLE(CONFIG_V4L2_FLASH_LED_CLASS)
57         struct v4l2_flash       *v4l2_flash;
58         struct v4l2_flash       *v4l2_flash_ind;
59 #endif
60 };
61
62 struct gb_lights {
63         struct gb_connection    *connection;
64         u8                      lights_count;
65         struct gb_light         *lights;
66         struct mutex            lights_lock;
67 };
68
69 static void gb_lights_channel_free(struct gb_channel *channel);
70
71 static struct gb_connection *get_conn_from_channel(struct gb_channel *channel)
72 {
73         return channel->light->glights->connection;
74 }
75
76 static struct gb_connection *get_conn_from_light(struct gb_light *light)
77 {
78         return light->glights->connection;
79 }
80
81 static bool is_channel_flash(struct gb_channel *channel)
82 {
83         return !!(channel->mode & (GB_CHANNEL_MODE_FLASH | GB_CHANNEL_MODE_TORCH
84                                    | GB_CHANNEL_MODE_INDICATOR));
85 }
86
87 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
88 static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
89 {
90         struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
91
92         return container_of(fled_cdev, struct gb_channel, fled);
93 }
94
95 static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
96 {
97         return &channel->fled.led_cdev;
98 }
99
100 static struct gb_channel *get_channel_from_mode(struct gb_light *light,
101                                                 u32 mode)
102 {
103         struct gb_channel *channel;
104         int i;
105
106         for (i = 0; i < light->channels_count; i++) {
107                 channel = &light->channels[i];
108                 if (channel->mode == mode)
109                         return channel;
110         }
111         return NULL;
112 }
113
114 static int __gb_lights_flash_intensity_set(struct gb_channel *channel,
115                                            u32 intensity)
116 {
117         struct gb_connection *connection = get_conn_from_channel(channel);
118         struct gb_bundle *bundle = connection->bundle;
119         struct gb_lights_set_flash_intensity_request req;
120         int ret;
121
122         if (channel->releasing)
123                 return -ESHUTDOWN;
124
125         ret = gb_pm_runtime_get_sync(bundle);
126         if (ret < 0)
127                 return ret;
128
129         req.light_id = channel->light->id;
130         req.channel_id = channel->id;
131         req.intensity_uA = cpu_to_le32(intensity);
132
133         ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_INTENSITY,
134                                 &req, sizeof(req), NULL, 0);
135
136         gb_pm_runtime_put_autosuspend(bundle);
137
138         return ret;
139 }
140
141 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
142 {
143         u32 intensity;
144
145         /* If the channel is flash we need to get the attached torch channel */
146         if (channel->mode & GB_CHANNEL_MODE_FLASH)
147                 channel = get_channel_from_mode(channel->light,
148                                                 GB_CHANNEL_MODE_TORCH);
149
150         if (!channel)
151                 return -EINVAL;
152
153         /* For not flash we need to convert brightness to intensity */
154         intensity = channel->intensity_uA.min +
155                         (channel->intensity_uA.step * channel->led->brightness);
156
157         return __gb_lights_flash_intensity_set(channel, intensity);
158 }
159 #else
160 static struct gb_channel *get_channel_from_cdev(struct led_classdev *cdev)
161 {
162         return container_of(cdev, struct gb_channel, cled);
163 }
164
165 static struct led_classdev *get_channel_cdev(struct gb_channel *channel)
166 {
167         return &channel->cled;
168 }
169
170 static int __gb_lights_flash_brightness_set(struct gb_channel *channel)
171 {
172         return 0;
173 }
174 #endif
175
176 static int gb_lights_color_set(struct gb_channel *channel, u32 color);
177 static int gb_lights_fade_set(struct gb_channel *channel);
178
179 static void led_lock(struct led_classdev *cdev)
180 {
181         mutex_lock(&cdev->led_access);
182 }
183
184 static void led_unlock(struct led_classdev *cdev)
185 {
186         mutex_unlock(&cdev->led_access);
187 }
188
189 #define gb_lights_fade_attr(__dir)                                      \
190 static 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                                                                         \
200 static 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;                                                     \
229 unlock:                                                                 \
230         led_unlock(cdev);                                               \
231         return ret;                                                     \
232 }                                                                       \
233 static DEVICE_ATTR_RW(fade_##__dir)
234
235 gb_lights_fade_attr(in);
236 gb_lights_fade_attr(out);
237
238 static 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
247 static 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;
272 unlock:
273         led_unlock(cdev);
274         return ret;
275 }
276 static DEVICE_ATTR_RW(color);
277
278 static 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         /* Set attributes based in the channel flags */
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
321 static 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
347 static 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
372 static 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         /* we need to keep module alive when turning to active state */
402         if (!old_active && channel->active)
403                 goto out_unlock;
404
405         /*
406          * on the other hand if going to inactive we still hold a reference and
407          * need to put it, so we could go to suspend.
408          */
409         if (old_active && !channel->active)
410                 gb_pm_runtime_put_autosuspend(bundle);
411
412 out_pm_put:
413         gb_pm_runtime_put_autosuspend(bundle);
414 out_unlock:
415         mutex_unlock(&channel->lock);
416
417         return ret;
418 }
419
420 static 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
435 static 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
445 static 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
453 static 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         /* we need to keep module alive when turning to active state */
492         if (!old_active && channel->active)
493                 goto out_unlock;
494
495         /*
496          * on the other hand if going to inactive we still hold a reference and
497          * need to put it, so we could go to suspend.
498          */
499         if (old_active && !channel->active)
500                 gb_pm_runtime_put_autosuspend(bundle);
501
502 out_pm_put:
503         gb_pm_runtime_put_autosuspend(bundle);
504 out_unlock:
505         mutex_unlock(&channel->lock);
506
507         return ret;
508 }
509
510 static 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 /* V4L2 specific helpers */
522 static const struct v4l2_flash_ops v4l2_flash_ops;
523
524 static 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         /* For v4l2 val is the default value */
531         v4l2_s->val = channel_s->max;
532 }
533
534 static 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 = { {0} }, sd_cfg_ind = { {0} };
539         struct led_classdev_flash *fled;
540         struct led_classdev *iled = NULL;
541         struct gb_channel *channel_torch, *channel_ind, *channel_flash;
542
543         channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
544         if (channel_torch)
545                 __gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
546                                                 &sd_cfg.intensity);
547
548         channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
549         if (channel_ind) {
550                 __gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
551                                                 &sd_cfg_ind.intensity);
552                 iled = &channel_ind->fled.led_cdev;
553         }
554
555         channel_flash = get_channel_from_mode(light, GB_CHANNEL_MODE_FLASH);
556         if (!channel_flash) {
557                 dev_err(dev, "failed to get flash channel from mode\n");
558                 return -EINVAL;
559         }
560
561         fled = &channel_flash->fled;
562
563         snprintf(sd_cfg.dev_name, sizeof(sd_cfg.dev_name), "%s", light->name);
564         snprintf(sd_cfg_ind.dev_name, sizeof(sd_cfg_ind.dev_name),
565                  "%s indicator", light->name);
566
567         /* Set the possible values to faults, in our case all faults */
568         sd_cfg.flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
569                 LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
570                 LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
571                 LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
572                 LED_FAULT_LED_OVER_TEMPERATURE;
573
574         light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, &v4l2_flash_ops,
575                                             &sd_cfg);
576         if (IS_ERR(light->v4l2_flash))
577                 return PTR_ERR(light->v4l2_flash);
578
579         if (channel_ind) {
580                 light->v4l2_flash_ind =
581                         v4l2_flash_indicator_init(dev, NULL, iled, &sd_cfg_ind);
582                 if (IS_ERR(light->v4l2_flash_ind)) {
583                         v4l2_flash_release(light->v4l2_flash);
584                         return PTR_ERR(light->v4l2_flash_ind);
585                 }
586         }
587
588         return 0;
589 }
590
591 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
592 {
593         v4l2_flash_release(light->v4l2_flash_ind);
594         v4l2_flash_release(light->v4l2_flash);
595 }
596 #else
597 static int gb_lights_light_v4l2_register(struct gb_light *light)
598 {
599         struct gb_connection *connection = get_conn_from_light(light);
600
601         dev_err(&connection->bundle->dev, "no support for v4l2 subdevices\n");
602         return 0;
603 }
604
605 static void gb_lights_light_v4l2_unregister(struct gb_light *light)
606 {
607 }
608 #endif
609
610 #if IS_REACHABLE(CONFIG_LEDS_CLASS_FLASH)
611 /* Flash specific operations */
612 static int gb_lights_flash_intensity_set(struct led_classdev_flash *fcdev,
613                                          u32 brightness)
614 {
615         struct gb_channel *channel = container_of(fcdev, struct gb_channel,
616                                                   fled);
617         int ret;
618
619         ret = __gb_lights_flash_intensity_set(channel, brightness);
620         if (ret < 0)
621                 return ret;
622
623         fcdev->brightness.val = brightness;
624
625         return 0;
626 }
627
628 static int gb_lights_flash_intensity_get(struct led_classdev_flash *fcdev,
629                                          u32 *brightness)
630 {
631         *brightness = fcdev->brightness.val;
632
633         return 0;
634 }
635
636 static int gb_lights_flash_strobe_set(struct led_classdev_flash *fcdev,
637                                       bool state)
638 {
639         struct gb_channel *channel = container_of(fcdev, struct gb_channel,
640                                                   fled);
641         struct gb_connection *connection = get_conn_from_channel(channel);
642         struct gb_bundle *bundle = connection->bundle;
643         struct gb_lights_set_flash_strobe_request req;
644         int ret;
645
646         if (channel->releasing)
647                 return -ESHUTDOWN;
648
649         ret = gb_pm_runtime_get_sync(bundle);
650         if (ret < 0)
651                 return ret;
652
653         req.light_id = channel->light->id;
654         req.channel_id = channel->id;
655         req.state = state ? 1 : 0;
656
657         ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_STROBE,
658                                 &req, sizeof(req), NULL, 0);
659         if (!ret)
660                 channel->strobe_state = state;
661
662         gb_pm_runtime_put_autosuspend(bundle);
663
664         return ret;
665 }
666
667 static int gb_lights_flash_strobe_get(struct led_classdev_flash *fcdev,
668                                       bool *state)
669 {
670         struct gb_channel *channel = container_of(fcdev, struct gb_channel,
671                                                   fled);
672
673         *state = channel->strobe_state;
674         return 0;
675 }
676
677 static int gb_lights_flash_timeout_set(struct led_classdev_flash *fcdev,
678                                        u32 timeout)
679 {
680         struct gb_channel *channel = container_of(fcdev, struct gb_channel,
681                                                   fled);
682         struct gb_connection *connection = get_conn_from_channel(channel);
683         struct gb_bundle *bundle = connection->bundle;
684         struct gb_lights_set_flash_timeout_request req;
685         int ret;
686
687         if (channel->releasing)
688                 return -ESHUTDOWN;
689
690         ret = gb_pm_runtime_get_sync(bundle);
691         if (ret < 0)
692                 return ret;
693
694         req.light_id = channel->light->id;
695         req.channel_id = channel->id;
696         req.timeout_us = cpu_to_le32(timeout);
697
698         ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_SET_FLASH_TIMEOUT,
699                                 &req, sizeof(req), NULL, 0);
700         if (!ret)
701                 fcdev->timeout.val = timeout;
702
703         gb_pm_runtime_put_autosuspend(bundle);
704
705         return ret;
706 }
707
708 static int gb_lights_flash_fault_get(struct led_classdev_flash *fcdev,
709                                      u32 *fault)
710 {
711         struct gb_channel *channel = container_of(fcdev, struct gb_channel,
712                                                   fled);
713         struct gb_connection *connection = get_conn_from_channel(channel);
714         struct gb_bundle *bundle = connection->bundle;
715         struct gb_lights_get_flash_fault_request req;
716         struct gb_lights_get_flash_fault_response resp;
717         int ret;
718
719         if (channel->releasing)
720                 return -ESHUTDOWN;
721
722         ret = gb_pm_runtime_get_sync(bundle);
723         if (ret < 0)
724                 return ret;
725
726         req.light_id = channel->light->id;
727         req.channel_id = channel->id;
728
729         ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_FLASH_FAULT,
730                                 &req, sizeof(req), &resp, sizeof(resp));
731         if (!ret)
732                 *fault = le32_to_cpu(resp.fault);
733
734         gb_pm_runtime_put_autosuspend(bundle);
735
736         return ret;
737 }
738
739 static const struct led_flash_ops gb_lights_flash_ops = {
740         .flash_brightness_set   = gb_lights_flash_intensity_set,
741         .flash_brightness_get   = gb_lights_flash_intensity_get,
742         .strobe_set             = gb_lights_flash_strobe_set,
743         .strobe_get             = gb_lights_flash_strobe_get,
744         .timeout_set            = gb_lights_flash_timeout_set,
745         .fault_get              = gb_lights_flash_fault_get,
746 };
747
748 static int __gb_lights_channel_torch_attach(struct gb_channel *channel,
749                                             struct gb_channel *channel_torch)
750 {
751         char *name;
752
753         /* we can only attach torch to a flash channel */
754         if (!(channel->mode & GB_CHANNEL_MODE_FLASH))
755                 return 0;
756
757         /* Move torch brightness to the destination */
758         channel->led->max_brightness = channel_torch->led->max_brightness;
759
760         /* append mode name to flash name */
761         name = kasprintf(GFP_KERNEL, "%s_%s", channel->led->name,
762                          channel_torch->mode_name);
763         if (!name)
764                 return -ENOMEM;
765         kfree(channel->led->name);
766         channel->led->name = name;
767
768         channel_torch->led = channel->led;
769
770         return 0;
771 }
772
773 static int __gb_lights_flash_led_register(struct gb_channel *channel)
774 {
775         struct gb_connection *connection = get_conn_from_channel(channel);
776         struct led_classdev_flash *fled = &channel->fled;
777         struct led_flash_setting *fset;
778         struct gb_channel *channel_torch;
779         int ret;
780
781         fled->ops = &gb_lights_flash_ops;
782
783         fled->led_cdev.flags |= LED_DEV_CAP_FLASH;
784
785         fset = &fled->brightness;
786         fset->min = channel->intensity_uA.min;
787         fset->max = channel->intensity_uA.max;
788         fset->step = channel->intensity_uA.step;
789         fset->val = channel->intensity_uA.max;
790
791         /* Only the flash mode have the timeout constraints settings */
792         if (channel->mode & GB_CHANNEL_MODE_FLASH) {
793                 fset = &fled->timeout;
794                 fset->min = channel->timeout_us.min;
795                 fset->max = channel->timeout_us.max;
796                 fset->step = channel->timeout_us.step;
797                 fset->val = channel->timeout_us.max;
798         }
799
800         /*
801          * If light have torch mode channel, this channel will be the led
802          * classdev of the registered above flash classdev
803          */
804         channel_torch = get_channel_from_mode(channel->light,
805                                               GB_CHANNEL_MODE_TORCH);
806         if (channel_torch) {
807                 ret = __gb_lights_channel_torch_attach(channel, channel_torch);
808                 if (ret < 0)
809                         goto fail;
810         }
811
812         ret = led_classdev_flash_register(&connection->bundle->dev, fled);
813         if (ret < 0)
814                 goto fail;
815
816         channel->is_registered = true;
817         return 0;
818 fail:
819         channel->led = NULL;
820         return ret;
821 }
822
823 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
824 {
825         if (!channel->is_registered)
826                 return;
827
828         led_classdev_flash_unregister(&channel->fled);
829 }
830
831 static int gb_lights_channel_flash_config(struct gb_channel *channel)
832 {
833         struct gb_connection *connection = get_conn_from_channel(channel);
834         struct gb_lights_get_channel_flash_config_request req;
835         struct gb_lights_get_channel_flash_config_response conf;
836         struct led_flash_setting *fset;
837         int ret;
838
839         req.light_id = channel->light->id;
840         req.channel_id = channel->id;
841
842         ret = gb_operation_sync(connection,
843                                 GB_LIGHTS_TYPE_GET_CHANNEL_FLASH_CONFIG,
844                                 &req, sizeof(req), &conf, sizeof(conf));
845         if (ret < 0)
846                 return ret;
847
848         /*
849          * Intensity constraints for flash related modes: flash, torch,
850          * indicator.  They will be needed for v4l2 registration.
851          */
852         fset = &channel->intensity_uA;
853         fset->min = le32_to_cpu(conf.intensity_min_uA);
854         fset->max = le32_to_cpu(conf.intensity_max_uA);
855         fset->step = le32_to_cpu(conf.intensity_step_uA);
856
857         /*
858          * On flash type, max brightness is set as the number of intensity steps
859          * available.
860          */
861         channel->led->max_brightness = (fset->max - fset->min) / fset->step;
862
863         /* Only the flash mode have the timeout constraints settings */
864         if (channel->mode & GB_CHANNEL_MODE_FLASH) {
865                 fset = &channel->timeout_us;
866                 fset->min = le32_to_cpu(conf.timeout_min_us);
867                 fset->max = le32_to_cpu(conf.timeout_max_us);
868                 fset->step = le32_to_cpu(conf.timeout_step_us);
869         }
870
871         return 0;
872 }
873 #else
874 static int gb_lights_channel_flash_config(struct gb_channel *channel)
875 {
876         struct gb_connection *connection = get_conn_from_channel(channel);
877
878         dev_err(&connection->bundle->dev, "no support for flash devices\n");
879         return 0;
880 }
881
882 static int __gb_lights_flash_led_register(struct gb_channel *channel)
883 {
884         return 0;
885 }
886
887 static void __gb_lights_flash_led_unregister(struct gb_channel *channel)
888 {
889 }
890
891 #endif
892
893 static int __gb_lights_led_register(struct gb_channel *channel)
894 {
895         struct gb_connection *connection = get_conn_from_channel(channel);
896         struct led_classdev *cdev = get_channel_cdev(channel);
897         int ret;
898
899         ret = led_classdev_register(&connection->bundle->dev, cdev);
900         if (ret < 0)
901                 channel->led = NULL;
902         else
903                 channel->is_registered = true;
904         return ret;
905 }
906
907 static int gb_lights_channel_register(struct gb_channel *channel)
908 {
909         /* Normal LED channel, just register in led classdev and we are done */
910         if (!is_channel_flash(channel))
911                 return __gb_lights_led_register(channel);
912
913         /*
914          * Flash Type need more work, register flash classdev, indicator as
915          * flash classdev, torch will be led classdev of the flash classdev.
916          */
917         if (!(channel->mode & GB_CHANNEL_MODE_TORCH))
918                 return __gb_lights_flash_led_register(channel);
919
920         return 0;
921 }
922
923 static void __gb_lights_led_unregister(struct gb_channel *channel)
924 {
925         struct led_classdev *cdev = get_channel_cdev(channel);
926
927         if (!channel->is_registered)
928                 return;
929
930         led_classdev_unregister(cdev);
931         kfree(cdev->name);
932         cdev->name = NULL;
933         channel->led = NULL;
934 }
935
936 static void gb_lights_channel_unregister(struct gb_channel *channel)
937 {
938         /* The same as register, handle channels differently */
939         if (!is_channel_flash(channel)) {
940                 __gb_lights_led_unregister(channel);
941                 return;
942         }
943
944         if (channel->mode & GB_CHANNEL_MODE_TORCH)
945                 __gb_lights_led_unregister(channel);
946         else
947                 __gb_lights_flash_led_unregister(channel);
948 }
949
950 static int gb_lights_channel_config(struct gb_light *light,
951                                     struct gb_channel *channel)
952 {
953         struct gb_lights_get_channel_config_response conf;
954         struct gb_lights_get_channel_config_request req;
955         struct gb_connection *connection = get_conn_from_light(light);
956         struct led_classdev *cdev = get_channel_cdev(channel);
957         char *name;
958         int ret;
959
960         req.light_id = light->id;
961         req.channel_id = channel->id;
962
963         ret = gb_operation_sync(connection, GB_LIGHTS_TYPE_GET_CHANNEL_CONFIG,
964                                 &req, sizeof(req), &conf, sizeof(conf));
965         if (ret < 0)
966                 return ret;
967
968         channel->light = light;
969         channel->mode = le32_to_cpu(conf.mode);
970         channel->flags = le32_to_cpu(conf.flags);
971         channel->color = le32_to_cpu(conf.color);
972         channel->color_name = kstrndup(conf.color_name, NAMES_MAX, GFP_KERNEL);
973         if (!channel->color_name)
974                 return -ENOMEM;
975         channel->mode_name = kstrndup(conf.mode_name, NAMES_MAX, GFP_KERNEL);
976         if (!channel->mode_name)
977                 return -ENOMEM;
978
979         channel->led = cdev;
980
981         name = kasprintf(GFP_KERNEL, "%s:%s:%s", light->name,
982                          channel->color_name, channel->mode_name);
983         if (!name)
984                 return -ENOMEM;
985
986         cdev->name = name;
987
988         cdev->max_brightness = conf.max_brightness;
989
990         ret = channel_attr_groups_set(channel, cdev);
991         if (ret < 0)
992                 return ret;
993
994         gb_lights_led_operations_set(channel, cdev);
995
996         /*
997          * If it is not a flash related channel (flash, torch or indicator) we
998          * are done here. If not, continue and fetch flash related
999          * configurations.
1000          */
1001         if (!is_channel_flash(channel))
1002                 return ret;
1003
1004         light->has_flash = true;
1005
1006         return gb_lights_channel_flash_config(channel);
1007 }
1008
1009 static int gb_lights_light_config(struct gb_lights *glights, u8 id)
1010 {
1011         struct gb_light *light = &glights->lights[id];
1012         struct gb_lights_get_light_config_request req;
1013         struct gb_lights_get_light_config_response conf;
1014         int ret;
1015         int i;
1016
1017         light->glights = glights;
1018         light->id = id;
1019
1020         req.id = id;
1021
1022         ret = gb_operation_sync(glights->connection,
1023                                 GB_LIGHTS_TYPE_GET_LIGHT_CONFIG,
1024                                 &req, sizeof(req), &conf, sizeof(conf));
1025         if (ret < 0)
1026                 return ret;
1027
1028         if (!conf.channel_count)
1029                 return -EINVAL;
1030         if (!strlen(conf.name))
1031                 return -EINVAL;
1032
1033         light->channels_count = conf.channel_count;
1034         light->name = kstrndup(conf.name, NAMES_MAX, GFP_KERNEL);
1035         if (!light->name)
1036                 return -ENOMEM;
1037         light->channels = kcalloc(light->channels_count,
1038                                   sizeof(struct gb_channel), GFP_KERNEL);
1039         if (!light->channels)
1040                 return -ENOMEM;
1041
1042         /* First we collect all the configurations for all channels */
1043         for (i = 0; i < light->channels_count; i++) {
1044                 light->channels[i].id = i;
1045                 ret = gb_lights_channel_config(light, &light->channels[i]);
1046                 if (ret < 0)
1047                         return ret;
1048         }
1049
1050         return 0;
1051 }
1052
1053 static int gb_lights_light_register(struct gb_light *light)
1054 {
1055         int ret;
1056         int i;
1057
1058         /*
1059          * Then, if everything went ok in getting configurations, we register
1060          * the classdev, flash classdev and v4l2 subsystem, if a flash device is
1061          * found.
1062          */
1063         for (i = 0; i < light->channels_count; i++) {
1064                 ret = gb_lights_channel_register(&light->channels[i]);
1065                 if (ret < 0)
1066                         return ret;
1067
1068                 mutex_init(&light->channels[i].lock);
1069         }
1070
1071         light->ready = true;
1072
1073         if (light->has_flash) {
1074                 ret = gb_lights_light_v4l2_register(light);
1075                 if (ret < 0) {
1076                         light->has_flash = false;
1077                         return ret;
1078                 }
1079         }
1080
1081         return 0;
1082 }
1083
1084 static void gb_lights_channel_free(struct gb_channel *channel)
1085 {
1086         kfree(channel->attrs);
1087         kfree(channel->attr_group);
1088         kfree(channel->attr_groups);
1089         kfree(channel->color_name);
1090         kfree(channel->mode_name);
1091         mutex_destroy(&channel->lock);
1092 }
1093
1094 static void gb_lights_channel_release(struct gb_channel *channel)
1095 {
1096         channel->releasing = true;
1097
1098         gb_lights_channel_unregister(channel);
1099
1100         gb_lights_channel_free(channel);
1101 }
1102
1103 static void gb_lights_light_release(struct gb_light *light)
1104 {
1105         int i;
1106
1107         light->ready = false;
1108
1109         if (light->has_flash)
1110                 gb_lights_light_v4l2_unregister(light);
1111         light->has_flash = false;
1112
1113         for (i = 0; i < light->channels_count; i++)
1114                 gb_lights_channel_release(&light->channels[i]);
1115         light->channels_count = 0;
1116
1117         kfree(light->channels);
1118         light->channels = NULL;
1119         kfree(light->name);
1120         light->name = NULL;
1121 }
1122
1123 static void gb_lights_release(struct gb_lights *glights)
1124 {
1125         int i;
1126
1127         if (!glights)
1128                 return;
1129
1130         mutex_lock(&glights->lights_lock);
1131         if (!glights->lights)
1132                 goto free_glights;
1133
1134         for (i = 0; i < glights->lights_count; i++)
1135                 gb_lights_light_release(&glights->lights[i]);
1136
1137         kfree(glights->lights);
1138
1139 free_glights:
1140         mutex_unlock(&glights->lights_lock);
1141         mutex_destroy(&glights->lights_lock);
1142         kfree(glights);
1143 }
1144
1145 static int gb_lights_get_count(struct gb_lights *glights)
1146 {
1147         struct gb_lights_get_lights_response resp;
1148         int ret;
1149
1150         ret = gb_operation_sync(glights->connection, GB_LIGHTS_TYPE_GET_LIGHTS,
1151                                 NULL, 0, &resp, sizeof(resp));
1152         if (ret < 0)
1153                 return ret;
1154
1155         if (!resp.lights_count)
1156                 return -EINVAL;
1157
1158         glights->lights_count = resp.lights_count;
1159
1160         return 0;
1161 }
1162
1163 static int gb_lights_create_all(struct gb_lights *glights)
1164 {
1165         struct gb_connection *connection = glights->connection;
1166         int ret;
1167         int i;
1168
1169         mutex_lock(&glights->lights_lock);
1170         ret = gb_lights_get_count(glights);
1171         if (ret < 0)
1172                 goto out;
1173
1174         glights->lights = kcalloc(glights->lights_count,
1175                                   sizeof(struct gb_light), GFP_KERNEL);
1176         if (!glights->lights) {
1177                 ret = -ENOMEM;
1178                 goto out;
1179         }
1180
1181         for (i = 0; i < glights->lights_count; i++) {
1182                 ret = gb_lights_light_config(glights, i);
1183                 if (ret < 0) {
1184                         dev_err(&connection->bundle->dev,
1185                                 "Fail to configure lights device\n");
1186                         goto out;
1187                 }
1188         }
1189
1190 out:
1191         mutex_unlock(&glights->lights_lock);
1192         return ret;
1193 }
1194
1195 static int gb_lights_register_all(struct gb_lights *glights)
1196 {
1197         struct gb_connection *connection = glights->connection;
1198         int ret = 0;
1199         int i;
1200
1201         mutex_lock(&glights->lights_lock);
1202         for (i = 0; i < glights->lights_count; i++) {
1203                 ret = gb_lights_light_register(&glights->lights[i]);
1204                 if (ret < 0) {
1205                         dev_err(&connection->bundle->dev,
1206                                 "Fail to enable lights device\n");
1207                         break;
1208                 }
1209         }
1210
1211         mutex_unlock(&glights->lights_lock);
1212         return ret;
1213 }
1214
1215 static int gb_lights_request_handler(struct gb_operation *op)
1216 {
1217         struct gb_connection *connection = op->connection;
1218         struct device *dev = &connection->bundle->dev;
1219         struct gb_lights *glights = gb_connection_get_data(connection);
1220         struct gb_light *light;
1221         struct gb_message *request;
1222         struct gb_lights_event_request *payload;
1223         int ret =  0;
1224         u8 light_id;
1225         u8 event;
1226
1227         if (op->type != GB_LIGHTS_TYPE_EVENT) {
1228                 dev_err(dev, "Unsupported unsolicited event: %u\n", op->type);
1229                 return -EINVAL;
1230         }
1231
1232         request = op->request;
1233
1234         if (request->payload_size < sizeof(*payload)) {
1235                 dev_err(dev, "Wrong event size received (%zu < %zu)\n",
1236                         request->payload_size, sizeof(*payload));
1237                 return -EINVAL;
1238         }
1239
1240         payload = request->payload;
1241         light_id = payload->light_id;
1242
1243         if (light_id >= glights->lights_count ||
1244             !glights->lights[light_id].ready) {
1245                 dev_err(dev, "Event received for unconfigured light id: %d\n",
1246                         light_id);
1247                 return -EINVAL;
1248         }
1249
1250         event = payload->event;
1251
1252         if (event & GB_LIGHTS_LIGHT_CONFIG) {
1253                 light = &glights->lights[light_id];
1254
1255                 mutex_lock(&glights->lights_lock);
1256                 gb_lights_light_release(light);
1257                 ret = gb_lights_light_config(glights, light_id);
1258                 if (!ret)
1259                         ret = gb_lights_light_register(light);
1260                 if (ret < 0)
1261                         gb_lights_light_release(light);
1262                 mutex_unlock(&glights->lights_lock);
1263         }
1264
1265         return ret;
1266 }
1267
1268 static int gb_lights_probe(struct gb_bundle *bundle,
1269                            const struct greybus_bundle_id *id)
1270 {
1271         struct greybus_descriptor_cport *cport_desc;
1272         struct gb_connection *connection;
1273         struct gb_lights *glights;
1274         int ret;
1275
1276         if (bundle->num_cports != 1)
1277                 return -ENODEV;
1278
1279         cport_desc = &bundle->cport_desc[0];
1280         if (cport_desc->protocol_id != GREYBUS_PROTOCOL_LIGHTS)
1281                 return -ENODEV;
1282
1283         glights = kzalloc(sizeof(*glights), GFP_KERNEL);
1284         if (!glights)
1285                 return -ENOMEM;
1286
1287         mutex_init(&glights->lights_lock);
1288
1289         connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
1290                                           gb_lights_request_handler);
1291         if (IS_ERR(connection)) {
1292                 ret = PTR_ERR(connection);
1293                 goto out;
1294         }
1295
1296         glights->connection = connection;
1297         gb_connection_set_data(connection, glights);
1298
1299         greybus_set_drvdata(bundle, glights);
1300
1301         /* We aren't ready to receive an incoming request yet */
1302         ret = gb_connection_enable_tx(connection);
1303         if (ret)
1304                 goto error_connection_destroy;
1305
1306         /*
1307          * Setup all the lights devices over this connection, if anything goes
1308          * wrong tear down all lights
1309          */
1310         ret = gb_lights_create_all(glights);
1311         if (ret < 0)
1312                 goto error_connection_disable;
1313
1314         /* We are ready to receive an incoming request now, enable RX as well */
1315         ret = gb_connection_enable(connection);
1316         if (ret)
1317                 goto error_connection_disable;
1318
1319         /* Enable & register lights */
1320         ret = gb_lights_register_all(glights);
1321         if (ret < 0)
1322                 goto error_connection_disable;
1323
1324         gb_pm_runtime_put_autosuspend(bundle);
1325
1326         return 0;
1327
1328 error_connection_disable:
1329         gb_connection_disable(connection);
1330 error_connection_destroy:
1331         gb_connection_destroy(connection);
1332 out:
1333         gb_lights_release(glights);
1334         return ret;
1335 }
1336
1337 static void gb_lights_disconnect(struct gb_bundle *bundle)
1338 {
1339         struct gb_lights *glights = greybus_get_drvdata(bundle);
1340
1341         if (gb_pm_runtime_get_sync(bundle))
1342                 gb_pm_runtime_get_noresume(bundle);
1343
1344         gb_connection_disable(glights->connection);
1345         gb_connection_destroy(glights->connection);
1346
1347         gb_lights_release(glights);
1348 }
1349
1350 static const struct greybus_bundle_id gb_lights_id_table[] = {
1351         { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_LIGHTS) },
1352         { }
1353 };
1354 MODULE_DEVICE_TABLE(greybus, gb_lights_id_table);
1355
1356 static struct greybus_driver gb_lights_driver = {
1357         .name           = "lights",
1358         .probe          = gb_lights_probe,
1359         .disconnect     = gb_lights_disconnect,
1360         .id_table       = gb_lights_id_table,
1361 };
1362 module_greybus_driver(gb_lights_driver);
1363
1364 MODULE_LICENSE("GPL v2");