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