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