1 /*
2  * LED Flash class interface
3  *
4  * Copyright (C) 2015 Samsung Electronics Co., Ltd.
5  * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12 #ifndef __LINUX_FLASH_LEDS_H_INCLUDED
13 #define __LINUX_FLASH_LEDS_H_INCLUDED
14 
15 #include <linux/leds.h>
16 #include <uapi/linux/v4l2-controls.h>
17 
18 struct device_node;
19 struct led_classdev_flash;
20 
21 /*
22  * Supported led fault bits - must be kept in synch
23  * with V4L2_FLASH_FAULT bits.
24  */
25 #define LED_FAULT_OVER_VOLTAGE		(1 << 0)
26 #define LED_FAULT_TIMEOUT		(1 << 1)
27 #define LED_FAULT_OVER_TEMPERATURE	(1 << 2)
28 #define LED_FAULT_SHORT_CIRCUIT		(1 << 3)
29 #define LED_FAULT_OVER_CURRENT		(1 << 4)
30 #define LED_FAULT_INDICATOR		(1 << 5)
31 #define LED_FAULT_UNDER_VOLTAGE		(1 << 6)
32 #define LED_FAULT_INPUT_VOLTAGE		(1 << 7)
33 #define LED_FAULT_LED_OVER_TEMPERATURE	(1 << 8)
34 #define LED_NUM_FLASH_FAULTS		9
35 
36 #define LED_FLASH_MAX_SYSFS_GROUPS 7
37 
38 struct led_flash_ops {
39 	/* set flash brightness */
40 	int (*flash_brightness_set)(struct led_classdev_flash *fled_cdev,
41 					u32 brightness);
42 	/* get flash brightness */
43 	int (*flash_brightness_get)(struct led_classdev_flash *fled_cdev,
44 					u32 *brightness);
45 	/* set flash strobe state */
46 	int (*strobe_set)(struct led_classdev_flash *fled_cdev, bool state);
47 	/* get flash strobe state */
48 	int (*strobe_get)(struct led_classdev_flash *fled_cdev, bool *state);
49 	/* set flash timeout */
50 	int (*timeout_set)(struct led_classdev_flash *fled_cdev, u32 timeout);
51 	/* get the flash LED fault */
52 	int (*fault_get)(struct led_classdev_flash *fled_cdev, u32 *fault);
53 };
54 
55 /*
56  * Current value of a flash setting along
57  * with its constraints.
58  */
59 struct led_flash_setting {
60 	/* maximum allowed value */
61 	u32 min;
62 	/* maximum allowed value */
63 	u32 max;
64 	/* step value */
65 	u32 step;
66 	/* current value */
67 	u32 val;
68 };
69 
70 struct led_classdev_flash {
71 	/* led class device */
72 	struct led_classdev led_cdev;
73 
74 	/* flash led specific ops */
75 	const struct led_flash_ops *ops;
76 
77 	/* flash brightness value in microamperes along with its constraints */
78 	struct led_flash_setting brightness;
79 
80 	/* flash timeout value in microseconds along with its constraints */
81 	struct led_flash_setting timeout;
82 
83 	/* LED Flash class sysfs groups */
84 	const struct attribute_group *sysfs_groups[LED_FLASH_MAX_SYSFS_GROUPS];
85 
86 	/* LEDs available for flash strobe synchronization */
87 	struct led_classdev_flash **sync_leds;
88 
89 	/* Number of LEDs available for flash strobe synchronization */
90 	int num_sync_leds;
91 
92 	/*
93 	 * The identifier of the sub-led to synchronize the flash strobe with.
94 	 * Identifiers start from 1, which reflects the first element from the
95 	 * sync_leds array. 0 means that the flash strobe should not be
96 	 * synchronized.
97 	 */
98 	u32 sync_led_id;
99 };
100 
101 static inline struct led_classdev_flash *lcdev_to_flcdev(
102 						struct led_classdev *lcdev)
103 {
104 	return container_of(lcdev, struct led_classdev_flash, led_cdev);
105 }
106 
107 /**
108  * led_classdev_flash_register - register a new object of led_classdev class
109  *				 with support for flash LEDs
110  * @parent: the flash LED to register
111  * @fled_cdev: the led_classdev_flash structure for this device
112  *
113  * Returns: 0 on success or negative error value on failure
114  */
115 extern int led_classdev_flash_register(struct device *parent,
116 				struct led_classdev_flash *fled_cdev);
117 
118 /**
119  * led_classdev_flash_unregister - unregisters an object of led_classdev class
120  *				   with support for flash LEDs
121  * @fled_cdev: the flash LED to unregister
122  *
123  * Unregister a previously registered via led_classdev_flash_register object
124  */
125 extern void led_classdev_flash_unregister(struct led_classdev_flash *fled_cdev);
126 
127 /**
128  * led_set_flash_strobe - setup flash strobe
129  * @fled_cdev: the flash LED to set strobe on
130  * @state: 1 - strobe flash, 0 - stop flash strobe
131  *
132  * Strobe the flash LED.
133  *
134  * Returns: 0 on success or negative error value on failure
135  */
136 static inline int led_set_flash_strobe(struct led_classdev_flash *fled_cdev,
137 					bool state)
138 {
139 	return fled_cdev->ops->strobe_set(fled_cdev, state);
140 }
141 
142 /**
143  * led_get_flash_strobe - get flash strobe status
144  * @fled_cdev: the flash LED to query
145  * @state: 1 - flash is strobing, 0 - flash is off
146  *
147  * Check whether the flash is strobing at the moment.
148  *
149  * Returns: 0 on success or negative error value on failure
150  */
151 static inline int led_get_flash_strobe(struct led_classdev_flash *fled_cdev,
152 					bool *state)
153 {
154 	if (fled_cdev->ops->strobe_get)
155 		return fled_cdev->ops->strobe_get(fled_cdev, state);
156 
157 	return -EINVAL;
158 }
159 
160 /**
161  * led_set_flash_brightness - set flash LED brightness
162  * @fled_cdev: the flash LED to set
163  * @brightness: the brightness to set it to
164  *
165  * Set a flash LED's brightness.
166  *
167  * Returns: 0 on success or negative error value on failure
168  */
169 extern int led_set_flash_brightness(struct led_classdev_flash *fled_cdev,
170 					u32 brightness);
171 
172 /**
173  * led_update_flash_brightness - update flash LED brightness
174  * @fled_cdev: the flash LED to query
175  *
176  * Get a flash LED's current brightness and update led_flash->brightness
177  * member with the obtained value.
178  *
179  * Returns: 0 on success or negative error value on failure
180  */
181 extern int led_update_flash_brightness(struct led_classdev_flash *fled_cdev);
182 
183 /**
184  * led_set_flash_timeout - set flash LED timeout
185  * @fled_cdev: the flash LED to set
186  * @timeout: the flash timeout to set it to
187  *
188  * Set the flash strobe duration.
189  *
190  * Returns: 0 on success or negative error value on failure
191  */
192 extern int led_set_flash_timeout(struct led_classdev_flash *fled_cdev,
193 					u32 timeout);
194 
195 /**
196  * led_get_flash_fault - get the flash LED fault
197  * @fled_cdev: the flash LED to query
198  * @fault: bitmask containing flash faults
199  *
200  * Get the flash LED fault.
201  *
202  * Returns: 0 on success or negative error value on failure
203  */
204 extern int led_get_flash_fault(struct led_classdev_flash *fled_cdev,
205 					u32 *fault);
206 
207 #endif	/* __LINUX_FLASH_LEDS_H_INCLUDED */
208