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