1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun // Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd.
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun * V0.0X01.0X01 fix get wrong time info issue.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/delay.h>
8*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
9*4882a593Smuzhiyun #include <linux/i2c.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/of.h>
12*4882a593Smuzhiyun #include <linux/pm_runtime.h>
13*4882a593Smuzhiyun #include <linux/rk-camera-module.h>
14*4882a593Smuzhiyun #include <linux/rk-led-flash.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/version.h>
17*4882a593Smuzhiyun #include <media/v4l2-ctrls.h>
18*4882a593Smuzhiyun #include <media/v4l2-device.h>
19*4882a593Smuzhiyun #include <linux/compat.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x1)
22*4882a593Smuzhiyun #define SGM3784_NAME "sgm3784"
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define SGM3784_REG_ID 0x00
25*4882a593Smuzhiyun #define SGM3784_ID 0x18
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #define SGM3784_REG_MODE 0x01
28*4882a593Smuzhiyun #define SGM3784_REG_LED0_FLASH_CUR 0x06
29*4882a593Smuzhiyun #define SGM3784_REG_LED0_TORCH_CUR 0x08
30*4882a593Smuzhiyun #define SGM3784_REG_LED1_FLASH_CUR 0x09
31*4882a593Smuzhiyun #define SGM3784_REG_LED1_TORCH_CUR 0x0b
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define SGM3784_REG_FAULT 0x0c
34*4882a593Smuzhiyun #define SGM3784_REG_FL_OVP BIT(7)
35*4882a593Smuzhiyun #define SGM3784_REG_FL_SC BIT(6)
36*4882a593Smuzhiyun #define SGM3784_REG_FL_OT BIT(5)
37*4882a593Smuzhiyun #define SGM3784_REG_FL_TO BIT(4)
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define SGM3784_REG_ENABLE 0x0f
40*4882a593Smuzhiyun #define SGM3784_ON 0x03
41*4882a593Smuzhiyun #define SGM3784_OFF 0x00
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #define SGM3784_MIN_FLASH_INTENSITY 0
44*4882a593Smuzhiyun #define SGM3784_MAX_FLASH_INTENSITY 1122000
45*4882a593Smuzhiyun #define SGM3784_FLASH_INTENSITY_DEFAULT 748000
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #define SGM3784_MIN_TORCH_INTENSITY 0
48*4882a593Smuzhiyun #define SGM3784_MAX_TORCH_INTENSITY 299200
49*4882a593Smuzhiyun #define SGM3784_TORCH_INTENSITY_DEFAULT 74800
50*4882a593Smuzhiyun #define SGM3784_INTENSITY_STEP 18700
51*4882a593Smuzhiyun #define TIMEOUT_MAX 1600000
52*4882a593Smuzhiyun #define TIMEOUT_STEP 100000
53*4882a593Smuzhiyun #define TIMEOUT_MIN 100000
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun static int debug;
56*4882a593Smuzhiyun module_param(debug, int, 0644);
57*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "debug level (0-2)");
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun enum sgm3784_led_id {
60*4882a593Smuzhiyun LED0 = 0,
61*4882a593Smuzhiyun LED1,
62*4882a593Smuzhiyun LED_MAX
63*4882a593Smuzhiyun };
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun struct sgm3784_led {
66*4882a593Smuzhiyun struct v4l2_subdev sd;
67*4882a593Smuzhiyun struct v4l2_ctrl_handler ctrls;
68*4882a593Smuzhiyun struct v4l2_ctrl *flash_brt;
69*4882a593Smuzhiyun struct v4l2_ctrl *torch_brt;
70*4882a593Smuzhiyun struct __kernel_old_timeval timestamp;
71*4882a593Smuzhiyun u32 max_flash_timeout;
72*4882a593Smuzhiyun u32 max_flash_intensity;
73*4882a593Smuzhiyun u32 max_torch_intensity;
74*4882a593Smuzhiyun };
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun struct sgm3784_flash {
77*4882a593Smuzhiyun struct i2c_client *client;
78*4882a593Smuzhiyun struct sgm3784_led leds[LED_MAX];
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun struct gpio_desc *en_gpio;
81*4882a593Smuzhiyun struct gpio_desc *torch_gpio;
82*4882a593Smuzhiyun struct gpio_desc *strobe_gpio;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun struct mutex lock;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun u32 flash_timeout;
87*4882a593Smuzhiyun enum v4l2_flash_led_mode cur_mode;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun u32 module_index;
90*4882a593Smuzhiyun const char *module_facing;
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun
sgm3784_i2c_write(struct sgm3784_flash * flash,u8 reg,u8 val)93*4882a593Smuzhiyun static int sgm3784_i2c_write(struct sgm3784_flash *flash, u8 reg, u8 val)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun struct i2c_client *client = flash->client;
96*4882a593Smuzhiyun int ret;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun ret = i2c_smbus_write_byte_data(client, reg, val);
99*4882a593Smuzhiyun if (ret < 0)
100*4882a593Smuzhiyun dev_err(&client->dev,
101*4882a593Smuzhiyun "%s: reg:0x%x val:0x%x failed\n",
102*4882a593Smuzhiyun __func__, reg, val);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun v4l2_dbg(2, debug, &flash->leds[0].sd,
105*4882a593Smuzhiyun "%s: reg:0x%x val:0x%x\n",
106*4882a593Smuzhiyun __func__, reg, val);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun return ret;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
sgm3784_i2c_read(struct sgm3784_flash * flash,u8 reg)111*4882a593Smuzhiyun static int sgm3784_i2c_read(struct sgm3784_flash *flash, u8 reg)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun struct i2c_client *client = flash->client;
114*4882a593Smuzhiyun int ret;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun ret = i2c_smbus_read_byte_data(client, reg);
117*4882a593Smuzhiyun if (ret < 0)
118*4882a593Smuzhiyun dev_err(&client->dev,
119*4882a593Smuzhiyun "%s: reg:0x%x failed\n",
120*4882a593Smuzhiyun __func__, reg);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun v4l2_dbg(2, debug, &flash->leds[0].sd,
123*4882a593Smuzhiyun "%s: reg:0x%x val:0x%x\n",
124*4882a593Smuzhiyun __func__, reg, ret);
125*4882a593Smuzhiyun return ret;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
sgm3784_led_on(struct sgm3784_flash * flash,bool on)128*4882a593Smuzhiyun static int sgm3784_led_on(struct sgm3784_flash *flash, bool on)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun int ret;
131*4882a593Smuzhiyun u8 val;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun v4l2_dbg(1, debug, &flash->leds[0].sd,
134*4882a593Smuzhiyun "%s: on:%d\n", __func__, on);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun val = on ? SGM3784_ON : SGM3784_OFF;
137*4882a593Smuzhiyun ret = sgm3784_i2c_write(flash, SGM3784_REG_ENABLE, val);
138*4882a593Smuzhiyun flash->leds[0].timestamp = ns_to_kernel_old_timeval(ktime_get_ns());
139*4882a593Smuzhiyun flash->leds[1].timestamp = flash->leds[0].timestamp;
140*4882a593Smuzhiyun return ret;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
sgm3784_get_fault(struct sgm3784_flash * flash)143*4882a593Smuzhiyun static int sgm3784_get_fault(struct sgm3784_flash *flash)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun int fault = 0;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun fault = sgm3784_i2c_read(flash, SGM3784_REG_FAULT);
148*4882a593Smuzhiyun if (fault < 0)
149*4882a593Smuzhiyun return fault;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun if (!fault)
152*4882a593Smuzhiyun return 0;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun v4l2_info(&flash->leds[0].sd,
155*4882a593Smuzhiyun "%s: 0x%x\n", __func__, fault);
156*4882a593Smuzhiyun return fault;
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
sgm3784_set_timeout(struct sgm3784_flash * flash)159*4882a593Smuzhiyun static int sgm3784_set_timeout(struct sgm3784_flash *flash)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun int ret;
162*4882a593Smuzhiyun u8 val;
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun v4l2_dbg(1, debug, &flash->leds[0].sd,
165*4882a593Smuzhiyun "%s: %d\n",
166*4882a593Smuzhiyun __func__, flash->flash_timeout);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun ret = sgm3784_i2c_read(flash, 0x02);
169*4882a593Smuzhiyun if (ret != 0)
170*4882a593Smuzhiyun return ret;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun val = (flash->flash_timeout - TIMEOUT_MIN) / TIMEOUT_STEP;
173*4882a593Smuzhiyun return sgm3784_i2c_write(flash, 0x02, val | (ret & 0xf0));
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
sgm3784_torch_brt(struct sgm3784_flash * flash,enum sgm3784_led_id id)176*4882a593Smuzhiyun static int sgm3784_torch_brt(struct sgm3784_flash *flash,
177*4882a593Smuzhiyun enum sgm3784_led_id id)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun struct sgm3784_led *led = &flash->leds[id];
180*4882a593Smuzhiyun u8 val, reg;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun v4l2_dbg(1, debug, &led->sd,
183*4882a593Smuzhiyun "%s: %d\n", __func__, led->torch_brt->val);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun val = led->torch_brt->val / SGM3784_INTENSITY_STEP;
186*4882a593Smuzhiyun reg = id ? SGM3784_REG_LED1_TORCH_CUR : SGM3784_REG_LED0_TORCH_CUR;
187*4882a593Smuzhiyun return sgm3784_i2c_write(flash, reg, val);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
sgm3784_flash_brt(struct sgm3784_flash * flash,enum sgm3784_led_id id)190*4882a593Smuzhiyun static int sgm3784_flash_brt(struct sgm3784_flash *flash,
191*4882a593Smuzhiyun enum sgm3784_led_id id)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun struct sgm3784_led *led = &flash->leds[id];
194*4882a593Smuzhiyun u8 val, reg;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun v4l2_dbg(1, debug, &led->sd,
197*4882a593Smuzhiyun "%s: %d\n", __func__, led->flash_brt->val);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun val = led->flash_brt->val / SGM3784_INTENSITY_STEP;
200*4882a593Smuzhiyun reg = id ? SGM3784_REG_LED1_FLASH_CUR : SGM3784_REG_LED0_FLASH_CUR;
201*4882a593Smuzhiyun return sgm3784_i2c_write(flash, reg, val);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
sgm3784_set_mode(struct sgm3784_flash * flash,enum sgm3784_led_id id,unsigned int mode)204*4882a593Smuzhiyun static int sgm3784_set_mode(struct sgm3784_flash *flash,
205*4882a593Smuzhiyun enum sgm3784_led_id id, unsigned int mode)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun int ret = 0;
208*4882a593Smuzhiyun u8 val;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun v4l2_dbg(1, debug, &flash->leds[id].sd,
211*4882a593Smuzhiyun "%s: %d cur:%d\n", __func__,
212*4882a593Smuzhiyun mode, flash->cur_mode);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun if (flash->cur_mode == mode)
215*4882a593Smuzhiyun return 0;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun sgm3784_led_on(flash, false);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun flash->cur_mode = mode;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun val = (flash->flash_timeout - TIMEOUT_MIN) / TIMEOUT_STEP;
222*4882a593Smuzhiyun if (mode == V4L2_FLASH_LED_MODE_FLASH) {
223*4882a593Smuzhiyun if (flash->torch_gpio)
224*4882a593Smuzhiyun gpiod_direction_output(flash->torch_gpio, 0);
225*4882a593Smuzhiyun ret = sgm3784_i2c_write(flash, 0x01, 0xfb);
226*4882a593Smuzhiyun ret |= sgm3784_i2c_write(flash, 0x02, val | 0xc0);
227*4882a593Smuzhiyun ret |= sgm3784_i2c_write(flash, 0x03, 0x48);
228*4882a593Smuzhiyun ret |= sgm3784_flash_brt(flash, LED0);
229*4882a593Smuzhiyun ret |= sgm3784_flash_brt(flash, LED1);
230*4882a593Smuzhiyun gpiod_direction_output(flash->strobe_gpio, 1);
231*4882a593Smuzhiyun } else if (mode == V4L2_FLASH_LED_MODE_TORCH) {
232*4882a593Smuzhiyun gpiod_direction_output(flash->strobe_gpio, 0);
233*4882a593Smuzhiyun if (flash->torch_gpio) {
234*4882a593Smuzhiyun /* torch mode */
235*4882a593Smuzhiyun ret = sgm3784_i2c_write(flash, 0x01, 0xf8);
236*4882a593Smuzhiyun ret |= sgm3784_i2c_write(flash, 0x02, val | 0xf0);
237*4882a593Smuzhiyun } else {
238*4882a593Smuzhiyun /* assist mode */
239*4882a593Smuzhiyun ret = sgm3784_i2c_write(flash, 0x01, 0xfa);
240*4882a593Smuzhiyun ret |= sgm3784_i2c_write(flash, 0x02, val | 0xc0);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun ret |= sgm3784_i2c_write(flash, 0x03, 0x48);
243*4882a593Smuzhiyun ret |= sgm3784_torch_brt(flash, LED0);
244*4882a593Smuzhiyun ret |= sgm3784_torch_brt(flash, LED1);
245*4882a593Smuzhiyun ret |= sgm3784_led_on(flash, true);
246*4882a593Smuzhiyun if (flash->torch_gpio)
247*4882a593Smuzhiyun gpiod_direction_output(flash->torch_gpio, 1);
248*4882a593Smuzhiyun } else {
249*4882a593Smuzhiyun ret = sgm3784_i2c_write(flash, 0x01, 0xf8);
250*4882a593Smuzhiyun gpiod_direction_output(flash->strobe_gpio, 0);
251*4882a593Smuzhiyun if (flash->torch_gpio)
252*4882a593Smuzhiyun gpiod_direction_output(flash->torch_gpio, 0);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun return ret;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
sgm3784_strobe(struct sgm3784_flash * flash,bool on)258*4882a593Smuzhiyun static int sgm3784_strobe(struct sgm3784_flash *flash, bool on)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun v4l2_dbg(1, debug, &flash->leds[0].sd,
261*4882a593Smuzhiyun "%s: on %d\n", __func__, on);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun if (flash->cur_mode != V4L2_FLASH_LED_MODE_FLASH)
264*4882a593Smuzhiyun return -EBUSY;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun return sgm3784_led_on(flash, on);
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
sgm3784_get_ctrl(struct v4l2_ctrl * ctrl,enum sgm3784_led_id id)269*4882a593Smuzhiyun static int sgm3784_get_ctrl(struct v4l2_ctrl *ctrl, enum sgm3784_led_id id)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun struct sgm3784_led *led =
272*4882a593Smuzhiyun container_of(ctrl->handler, struct sgm3784_led, ctrls);
273*4882a593Smuzhiyun struct sgm3784_flash *flash =
274*4882a593Smuzhiyun container_of(led, struct sgm3784_flash, leds[id]);
275*4882a593Smuzhiyun int ret = 0;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun v4l2_dbg(1, debug, &led->sd,
278*4882a593Smuzhiyun "%s: id 0x%x\n", __func__, ctrl->id);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun mutex_lock(&flash->lock);
281*4882a593Smuzhiyun switch (ctrl->id) {
282*4882a593Smuzhiyun case V4L2_CID_FLASH_FAULT:
283*4882a593Smuzhiyun ret = sgm3784_get_fault(flash);
284*4882a593Smuzhiyun ctrl->val = 0;
285*4882a593Smuzhiyun if (ret & SGM3784_REG_FL_SC)
286*4882a593Smuzhiyun ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
287*4882a593Smuzhiyun if (ret & SGM3784_REG_FL_OT)
288*4882a593Smuzhiyun ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
289*4882a593Smuzhiyun if (ret & SGM3784_REG_FL_TO)
290*4882a593Smuzhiyun ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
291*4882a593Smuzhiyun if (ret & SGM3784_REG_FL_OVP)
292*4882a593Smuzhiyun ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
293*4882a593Smuzhiyun ret = 0;
294*4882a593Smuzhiyun break;
295*4882a593Smuzhiyun default:
296*4882a593Smuzhiyun v4l2_err(&led->sd,
297*4882a593Smuzhiyun "ctrl 0x%x not supported\n", ctrl->id);
298*4882a593Smuzhiyun ret = -EINVAL;
299*4882a593Smuzhiyun break;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun mutex_unlock(&flash->lock);
303*4882a593Smuzhiyun return ret;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
sgm3784_set_ctrl(struct v4l2_ctrl * ctrl,enum sgm3784_led_id id)306*4882a593Smuzhiyun static int sgm3784_set_ctrl(struct v4l2_ctrl *ctrl, enum sgm3784_led_id id)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun struct sgm3784_led *led =
309*4882a593Smuzhiyun container_of(ctrl->handler, struct sgm3784_led, ctrls);
310*4882a593Smuzhiyun struct sgm3784_flash *flash =
311*4882a593Smuzhiyun container_of(led, struct sgm3784_flash, leds[id]);
312*4882a593Smuzhiyun int ret = 0;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun v4l2_dbg(1, debug, &led->sd,
315*4882a593Smuzhiyun "%s: id 0x%x val 0x%x\n",
316*4882a593Smuzhiyun __func__, ctrl->id, ctrl->val);
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun mutex_lock(&flash->lock);
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun ret = sgm3784_get_fault(flash);
321*4882a593Smuzhiyun if (ret != 0)
322*4882a593Smuzhiyun goto err;
323*4882a593Smuzhiyun if ((ret & (SGM3784_REG_FL_OVP |
324*4882a593Smuzhiyun SGM3784_REG_FL_OT |
325*4882a593Smuzhiyun SGM3784_REG_FL_SC)) &&
326*4882a593Smuzhiyun (ctrl->id == V4L2_CID_FLASH_STROBE ||
327*4882a593Smuzhiyun ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
328*4882a593Smuzhiyun ctrl->id == V4L2_CID_FLASH_LED_MODE)) {
329*4882a593Smuzhiyun ret = -EBUSY;
330*4882a593Smuzhiyun goto err;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun switch (ctrl->id) {
334*4882a593Smuzhiyun case V4L2_CID_FLASH_LED_MODE:
335*4882a593Smuzhiyun ret = sgm3784_set_mode(flash, id, ctrl->val);
336*4882a593Smuzhiyun break;
337*4882a593Smuzhiyun case V4L2_CID_FLASH_STROBE:
338*4882a593Smuzhiyun ret = sgm3784_strobe(flash, true);
339*4882a593Smuzhiyun break;
340*4882a593Smuzhiyun case V4L2_CID_FLASH_STROBE_STOP:
341*4882a593Smuzhiyun ret = sgm3784_strobe(flash, false);
342*4882a593Smuzhiyun break;
343*4882a593Smuzhiyun case V4L2_CID_FLASH_TIMEOUT:
344*4882a593Smuzhiyun ret = sgm3784_set_timeout(flash);
345*4882a593Smuzhiyun break;
346*4882a593Smuzhiyun case V4L2_CID_FLASH_INTENSITY:
347*4882a593Smuzhiyun ret = sgm3784_flash_brt(flash, id);
348*4882a593Smuzhiyun break;
349*4882a593Smuzhiyun case V4L2_CID_FLASH_TORCH_INTENSITY:
350*4882a593Smuzhiyun ret = sgm3784_torch_brt(flash, id);
351*4882a593Smuzhiyun break;
352*4882a593Smuzhiyun default:
353*4882a593Smuzhiyun v4l2_err(&led->sd,
354*4882a593Smuzhiyun "ctrl 0x%x not supported\n", ctrl->id);
355*4882a593Smuzhiyun ret = -EINVAL;
356*4882a593Smuzhiyun break;
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun err:
360*4882a593Smuzhiyun mutex_unlock(&flash->lock);
361*4882a593Smuzhiyun return ret;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
sgm3784_led0_get_ctrl(struct v4l2_ctrl * ctrl)364*4882a593Smuzhiyun static int sgm3784_led0_get_ctrl(struct v4l2_ctrl *ctrl)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun return sgm3784_get_ctrl(ctrl, LED0);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun
sgm3784_led0_set_ctrl(struct v4l2_ctrl * ctrl)369*4882a593Smuzhiyun static int sgm3784_led0_set_ctrl(struct v4l2_ctrl *ctrl)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun return sgm3784_set_ctrl(ctrl, LED0);
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
sgm3784_led1_get_ctrl(struct v4l2_ctrl * ctrl)374*4882a593Smuzhiyun static int sgm3784_led1_get_ctrl(struct v4l2_ctrl *ctrl)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun return sgm3784_get_ctrl(ctrl, LED1);
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun
sgm3784_led1_set_ctrl(struct v4l2_ctrl * ctrl)379*4882a593Smuzhiyun static int sgm3784_led1_set_ctrl(struct v4l2_ctrl *ctrl)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun return sgm3784_set_ctrl(ctrl, LED1);
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun static const struct v4l2_ctrl_ops sgm3784_ctrl_ops[LED_MAX] = {
385*4882a593Smuzhiyun [LED0] = {
386*4882a593Smuzhiyun .g_volatile_ctrl = sgm3784_led0_get_ctrl,
387*4882a593Smuzhiyun .s_ctrl = sgm3784_led0_set_ctrl,
388*4882a593Smuzhiyun },
389*4882a593Smuzhiyun [LED1] = {
390*4882a593Smuzhiyun .g_volatile_ctrl = sgm3784_led1_get_ctrl,
391*4882a593Smuzhiyun .s_ctrl = sgm3784_led1_set_ctrl,
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun };
394*4882a593Smuzhiyun
sgm3784_init_controls(struct sgm3784_flash * flash,enum sgm3784_led_id id)395*4882a593Smuzhiyun static int sgm3784_init_controls(struct sgm3784_flash *flash,
396*4882a593Smuzhiyun enum sgm3784_led_id id)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun struct v4l2_ctrl *fault;
399*4882a593Smuzhiyun struct v4l2_ctrl_handler *hdl = &flash->leds[id].ctrls;
400*4882a593Smuzhiyun const struct v4l2_ctrl_ops *ops = &sgm3784_ctrl_ops[id];
401*4882a593Smuzhiyun struct sgm3784_led *led = &flash->leds[id];
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun v4l2_ctrl_handler_init(hdl, 8);
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun v4l2_ctrl_new_std_menu(hdl, ops,
406*4882a593Smuzhiyun V4L2_CID_FLASH_LED_MODE,
407*4882a593Smuzhiyun V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
408*4882a593Smuzhiyun flash->cur_mode = V4L2_FLASH_LED_MODE_NONE;
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun v4l2_ctrl_new_std_menu(hdl, ops,
411*4882a593Smuzhiyun V4L2_CID_FLASH_STROBE_SOURCE,
412*4882a593Smuzhiyun V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun v4l2_ctrl_new_std(hdl, ops,
415*4882a593Smuzhiyun V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun v4l2_ctrl_new_std(hdl, ops,
418*4882a593Smuzhiyun V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun v4l2_ctrl_new_std(hdl, ops,
421*4882a593Smuzhiyun V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
422*4882a593Smuzhiyun led->max_flash_timeout,
423*4882a593Smuzhiyun TIMEOUT_STEP,
424*4882a593Smuzhiyun led->max_flash_timeout);
425*4882a593Smuzhiyun flash->flash_timeout = led->max_flash_timeout;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun flash->leds[id].flash_brt =
428*4882a593Smuzhiyun v4l2_ctrl_new_std(hdl, ops,
429*4882a593Smuzhiyun V4L2_CID_FLASH_INTENSITY,
430*4882a593Smuzhiyun SGM3784_MIN_FLASH_INTENSITY,
431*4882a593Smuzhiyun led->max_flash_intensity,
432*4882a593Smuzhiyun SGM3784_INTENSITY_STEP,
433*4882a593Smuzhiyun SGM3784_FLASH_INTENSITY_DEFAULT);
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun flash->leds[id].torch_brt =
436*4882a593Smuzhiyun v4l2_ctrl_new_std(hdl, ops,
437*4882a593Smuzhiyun V4L2_CID_FLASH_TORCH_INTENSITY,
438*4882a593Smuzhiyun SGM3784_MIN_TORCH_INTENSITY,
439*4882a593Smuzhiyun led->max_torch_intensity,
440*4882a593Smuzhiyun SGM3784_INTENSITY_STEP,
441*4882a593Smuzhiyun SGM3784_TORCH_INTENSITY_DEFAULT);
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun fault = v4l2_ctrl_new_std(hdl, ops,
444*4882a593Smuzhiyun V4L2_CID_FLASH_FAULT, 0,
445*4882a593Smuzhiyun V4L2_FLASH_FAULT_OVER_VOLTAGE
446*4882a593Smuzhiyun | V4L2_FLASH_FAULT_TIMEOUT
447*4882a593Smuzhiyun | V4L2_FLASH_FAULT_OVER_TEMPERATURE
448*4882a593Smuzhiyun | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun if (hdl->error)
451*4882a593Smuzhiyun return hdl->error;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun flash->leds[id].sd.ctrl_handler = hdl;
456*4882a593Smuzhiyun return 0;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
sgm3784_get_time_info(struct v4l2_subdev * sd,struct __kernel_old_timeval * ti)459*4882a593Smuzhiyun static void sgm3784_get_time_info(struct v4l2_subdev *sd,
460*4882a593Smuzhiyun struct __kernel_old_timeval *ti)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun struct sgm3784_led *led =
463*4882a593Smuzhiyun container_of(sd, struct sgm3784_led, sd);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun memset(ti, 0, sizeof(*ti));
466*4882a593Smuzhiyun ti->tv_sec = led->timestamp.tv_sec;
467*4882a593Smuzhiyun ti->tv_usec = led->timestamp.tv_usec;
468*4882a593Smuzhiyun v4l2_dbg(1, debug, sd,
469*4882a593Smuzhiyun "%s: tv_sec:%ld, tv_usec:%ld\n", __func__, ti->tv_sec, ti->tv_usec);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
sgm3784_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)472*4882a593Smuzhiyun static long sgm3784_ioctl(struct v4l2_subdev *sd,
473*4882a593Smuzhiyun unsigned int cmd, void *arg)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun long ret = 0;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun switch (cmd) {
478*4882a593Smuzhiyun case RK_VIDIOC_FLASH_TIMEINFO:
479*4882a593Smuzhiyun sgm3784_get_time_info(sd, (struct __kernel_old_timeval *)arg);
480*4882a593Smuzhiyun break;
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun default:
483*4882a593Smuzhiyun ret = -ENOIOCTLCMD;
484*4882a593Smuzhiyun break;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun return ret;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
490*4882a593Smuzhiyun #define RK_VIDIOC_COMPAT_FLASH_TIMEINFO \
491*4882a593Smuzhiyun _IOR('V', BASE_VIDIOC_PRIVATE + 0, struct old_timeval32)
492*4882a593Smuzhiyun
sgm3784_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)493*4882a593Smuzhiyun static long sgm3784_compat_ioctl32(struct v4l2_subdev *sd,
494*4882a593Smuzhiyun unsigned int cmd,
495*4882a593Smuzhiyun unsigned long arg)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun void __user *up = compat_ptr(arg);
498*4882a593Smuzhiyun struct old_timeval32 *compat_t;
499*4882a593Smuzhiyun long ret;
500*4882a593Smuzhiyun struct __kernel_old_timeval t;
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun v4l2_dbg(1, debug, sd,
503*4882a593Smuzhiyun "%s: cmd 0x%x\n", __func__, cmd);
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun switch (cmd) {
506*4882a593Smuzhiyun case RK_VIDIOC_COMPAT_FLASH_TIMEINFO:
507*4882a593Smuzhiyun compat_t = kzalloc(sizeof(*compat_t), GFP_KERNEL);
508*4882a593Smuzhiyun if (!compat_t) {
509*4882a593Smuzhiyun ret = -ENOMEM;
510*4882a593Smuzhiyun return ret;
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun ret = sgm3784_ioctl(sd, RK_VIDIOC_FLASH_TIMEINFO, &t);
513*4882a593Smuzhiyun if (!ret) {
514*4882a593Smuzhiyun compat_t->tv_sec = t.tv_sec;
515*4882a593Smuzhiyun compat_t->tv_usec = t.tv_usec;
516*4882a593Smuzhiyun ret = copy_to_user(up, compat_t, sizeof(*compat_t));
517*4882a593Smuzhiyun if (ret)
518*4882a593Smuzhiyun ret = -EFAULT;
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun kfree(compat_t);
521*4882a593Smuzhiyun break;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun default:
524*4882a593Smuzhiyun ret = -ENOIOCTLCMD;
525*4882a593Smuzhiyun break;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun return ret;
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun #endif
532*4882a593Smuzhiyun
sgm3784_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)533*4882a593Smuzhiyun static int sgm3784_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun int rval;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun rval = pm_runtime_get_sync(sd->dev);
538*4882a593Smuzhiyun if (rval < 0) {
539*4882a593Smuzhiyun pm_runtime_put_noidle(sd->dev);
540*4882a593Smuzhiyun return rval;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun return 0;
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun
sgm3784_close(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)546*4882a593Smuzhiyun static int sgm3784_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun pm_runtime_put(sd->dev);
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun return 0;
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun static const struct v4l2_subdev_core_ops sgm3784_core_ops = {
554*4882a593Smuzhiyun .ioctl = sgm3784_ioctl,
555*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
556*4882a593Smuzhiyun .compat_ioctl32 = sgm3784_compat_ioctl32
557*4882a593Smuzhiyun #endif
558*4882a593Smuzhiyun };
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun static const struct v4l2_subdev_ops sgm3784_ops = {
561*4882a593Smuzhiyun .core = &sgm3784_core_ops,
562*4882a593Smuzhiyun };
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun static const struct v4l2_subdev_internal_ops sgm3784_internal_ops = {
565*4882a593Smuzhiyun .open = sgm3784_open,
566*4882a593Smuzhiyun .close = sgm3784_close,
567*4882a593Smuzhiyun };
568*4882a593Smuzhiyun
__sgm3784_set_power(struct sgm3784_flash * flash,bool on)569*4882a593Smuzhiyun static int __sgm3784_set_power(struct sgm3784_flash *flash, bool on)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun gpiod_direction_output(flash->en_gpio, on);
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun return 0;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
sgm3784_check_id(struct sgm3784_flash * flash)576*4882a593Smuzhiyun static int sgm3784_check_id(struct sgm3784_flash *flash)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun int ret = 0;
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun __sgm3784_set_power(flash, true);
581*4882a593Smuzhiyun ret = sgm3784_i2c_read(flash, SGM3784_REG_ID);
582*4882a593Smuzhiyun __sgm3784_set_power(flash, false);
583*4882a593Smuzhiyun if (ret != SGM3784_ID) {
584*4882a593Smuzhiyun dev_err(&flash->client->dev,
585*4882a593Smuzhiyun "Read chip id error\n");
586*4882a593Smuzhiyun return -ENODEV;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun dev_info(&flash->client->dev,
590*4882a593Smuzhiyun "Detected sgm3784 flash id:0x%x\n", ret);
591*4882a593Smuzhiyun return 0;
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun
sgm3784_of_init(struct i2c_client * client,struct sgm3784_flash * flash)594*4882a593Smuzhiyun static int sgm3784_of_init(struct i2c_client *client,
595*4882a593Smuzhiyun struct sgm3784_flash *flash)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun struct device_node *node = client->dev.of_node;
598*4882a593Smuzhiyun struct device_node *child;
599*4882a593Smuzhiyun struct sgm3784_led *led;
600*4882a593Smuzhiyun int ret = 0;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun if (!node) {
603*4882a593Smuzhiyun dev_err(&client->dev,
604*4882a593Smuzhiyun "get device node failed\n");
605*4882a593Smuzhiyun goto err;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun ret = of_property_read_u32(node,
609*4882a593Smuzhiyun RKMODULE_CAMERA_MODULE_INDEX,
610*4882a593Smuzhiyun &flash->module_index);
611*4882a593Smuzhiyun ret |= of_property_read_string(node,
612*4882a593Smuzhiyun RKMODULE_CAMERA_MODULE_FACING,
613*4882a593Smuzhiyun &flash->module_facing);
614*4882a593Smuzhiyun if (ret) {
615*4882a593Smuzhiyun dev_err(&client->dev,
616*4882a593Smuzhiyun "could not get module information!\n");
617*4882a593Smuzhiyun goto err;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun flash->en_gpio = devm_gpiod_get(&client->dev,
621*4882a593Smuzhiyun "enable", GPIOD_OUT_LOW);
622*4882a593Smuzhiyun if (IS_ERR(flash->en_gpio)) {
623*4882a593Smuzhiyun dev_err(&client->dev, "get enable-gpio failed\n");
624*4882a593Smuzhiyun goto err;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun flash->torch_gpio = devm_gpiod_get(&client->dev,
628*4882a593Smuzhiyun "torch", GPIOD_OUT_LOW);
629*4882a593Smuzhiyun if (IS_ERR(flash->torch_gpio)) {
630*4882a593Smuzhiyun flash->torch_gpio = NULL;
631*4882a593Smuzhiyun dev_warn(&client->dev,
632*4882a593Smuzhiyun "get torch-gpio failed, using assist light mode\n");
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun flash->strobe_gpio = devm_gpiod_get(&client->dev,
636*4882a593Smuzhiyun "strobe", GPIOD_OUT_LOW);
637*4882a593Smuzhiyun if (IS_ERR(flash->en_gpio)) {
638*4882a593Smuzhiyun dev_err(&client->dev, "get strobe-gpio failed\n");
639*4882a593Smuzhiyun goto err;
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun for_each_child_of_node(node, child) {
643*4882a593Smuzhiyun u32 id = 0;
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun of_property_read_u32(child, "reg", &id);
646*4882a593Smuzhiyun if (id >= LED_MAX) {
647*4882a593Smuzhiyun dev_err(&client->dev, "only support 2 leds\n");
648*4882a593Smuzhiyun goto err;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun led = &flash->leds[id];
651*4882a593Smuzhiyun led->sd.fwnode = of_fwnode_handle(child);
652*4882a593Smuzhiyun if (of_property_read_u32(child, "flash-max-timeout-us",
653*4882a593Smuzhiyun &led->max_flash_timeout)) {
654*4882a593Smuzhiyun dev_err(&client->dev,
655*4882a593Smuzhiyun "get led%d flash-max-timeout-us fail\n", id);
656*4882a593Smuzhiyun goto err;
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun if (led->max_flash_timeout > TIMEOUT_MAX)
659*4882a593Smuzhiyun led->max_flash_timeout = TIMEOUT_MAX;
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun if (of_property_read_u32(child, "flash-max-microamp",
662*4882a593Smuzhiyun &led->max_flash_intensity)) {
663*4882a593Smuzhiyun dev_err(&client->dev,
664*4882a593Smuzhiyun "get led%d flash-max-microamp fail\n", id);
665*4882a593Smuzhiyun goto err;
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun if (led->max_flash_intensity > SGM3784_MAX_FLASH_INTENSITY)
668*4882a593Smuzhiyun led->max_flash_intensity = SGM3784_MAX_FLASH_INTENSITY;
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun if (of_property_read_u32(child, "led-max-microamp",
671*4882a593Smuzhiyun &led->max_torch_intensity)) {
672*4882a593Smuzhiyun dev_err(&client->dev,
673*4882a593Smuzhiyun "get led%d led-max-microamp fail\n", id);
674*4882a593Smuzhiyun goto err;
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun if (led->max_torch_intensity > SGM3784_MAX_TORCH_INTENSITY)
677*4882a593Smuzhiyun led->max_torch_intensity = SGM3784_MAX_TORCH_INTENSITY;
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun dev_info(&client->dev,
680*4882a593Smuzhiyun "led%d max torch:%dUA flash:%dUA timeout:%dUS\n",
681*4882a593Smuzhiyun id, led->max_torch_intensity,
682*4882a593Smuzhiyun led->max_flash_intensity,
683*4882a593Smuzhiyun led->max_flash_timeout);
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun return 0;
687*4882a593Smuzhiyun err:
688*4882a593Smuzhiyun return -EINVAL;
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun
sgm3784_probe(struct i2c_client * client,const struct i2c_device_id * devid)691*4882a593Smuzhiyun static int sgm3784_probe(struct i2c_client *client,
692*4882a593Smuzhiyun const struct i2c_device_id *devid)
693*4882a593Smuzhiyun {
694*4882a593Smuzhiyun struct sgm3784_flash *flash;
695*4882a593Smuzhiyun struct v4l2_subdev *sd;
696*4882a593Smuzhiyun char facing[2];
697*4882a593Smuzhiyun int i, ret;
698*4882a593Smuzhiyun
699*4882a593Smuzhiyun dev_info(&client->dev, "driver version: %02x.%02x.%02x",
700*4882a593Smuzhiyun DRIVER_VERSION >> 16,
701*4882a593Smuzhiyun (DRIVER_VERSION & 0xff00) >> 8,
702*4882a593Smuzhiyun DRIVER_VERSION & 0x00ff);
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
705*4882a593Smuzhiyun if (!flash)
706*4882a593Smuzhiyun return -ENOMEM;
707*4882a593Smuzhiyun
708*4882a593Smuzhiyun flash->client = client;
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun ret = sgm3784_of_init(client, flash);
711*4882a593Smuzhiyun if (ret)
712*4882a593Smuzhiyun return ret;
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun ret = sgm3784_check_id(flash);
715*4882a593Smuzhiyun if (ret)
716*4882a593Smuzhiyun return ret;
717*4882a593Smuzhiyun
718*4882a593Smuzhiyun for (i = 0; i < LED_MAX; i++) {
719*4882a593Smuzhiyun sd = &flash->leds[i].sd;
720*4882a593Smuzhiyun v4l2_i2c_subdev_init(sd, client, &sgm3784_ops);
721*4882a593Smuzhiyun sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
722*4882a593Smuzhiyun sd->internal_ops = &sgm3784_internal_ops;
723*4882a593Smuzhiyun
724*4882a593Smuzhiyun memset(facing, 0, sizeof(facing));
725*4882a593Smuzhiyun if (strcmp(flash->module_facing, "back") == 0)
726*4882a593Smuzhiyun facing[0] = 'b';
727*4882a593Smuzhiyun else
728*4882a593Smuzhiyun facing[0] = 'f';
729*4882a593Smuzhiyun /* NOTE: to distinguish between two led
730*4882a593Smuzhiyun * name: led0 meet the main led
731*4882a593Smuzhiyun * name: led1 meet the secondary led
732*4882a593Smuzhiyun */
733*4882a593Smuzhiyun snprintf(sd->name, sizeof(sd->name),
734*4882a593Smuzhiyun "m%02d_%s_%s_led%d %s",
735*4882a593Smuzhiyun flash->module_index, facing,
736*4882a593Smuzhiyun SGM3784_NAME, i, dev_name(sd->dev));
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun ret = sgm3784_init_controls(flash, i);
739*4882a593Smuzhiyun if (ret)
740*4882a593Smuzhiyun goto err;
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun ret = media_entity_pads_init(&sd->entity, 0, NULL);
743*4882a593Smuzhiyun if (ret < 0)
744*4882a593Smuzhiyun goto free_ctl;
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun sd->entity.function = MEDIA_ENT_F_FLASH;
747*4882a593Smuzhiyun ret = v4l2_async_register_subdev(sd);
748*4882a593Smuzhiyun if (ret)
749*4882a593Smuzhiyun goto free_media;
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun i2c_set_clientdata(client, flash);
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun mutex_init(&flash->lock);
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun pm_runtime_set_active(&client->dev);
757*4882a593Smuzhiyun pm_runtime_enable(&client->dev);
758*4882a593Smuzhiyun pm_runtime_idle(&client->dev);
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun dev_info(&client->dev, "probing successful\n");
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun return 0;
763*4882a593Smuzhiyun free_media:
764*4882a593Smuzhiyun media_entity_cleanup(&flash->leds[i].sd.entity);
765*4882a593Smuzhiyun free_ctl:
766*4882a593Smuzhiyun v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
767*4882a593Smuzhiyun err:
768*4882a593Smuzhiyun for (--i; i >= 0; --i) {
769*4882a593Smuzhiyun v4l2_device_unregister_subdev(&flash->leds[i].sd);
770*4882a593Smuzhiyun media_entity_cleanup(&flash->leds[i].sd.entity);
771*4882a593Smuzhiyun v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun return ret;
774*4882a593Smuzhiyun }
775*4882a593Smuzhiyun
sgm3784_remove(struct i2c_client * client)776*4882a593Smuzhiyun static int sgm3784_remove(struct i2c_client *client)
777*4882a593Smuzhiyun {
778*4882a593Smuzhiyun struct sgm3784_flash *flash = i2c_get_clientdata(client);
779*4882a593Smuzhiyun unsigned int i;
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun pm_runtime_disable(&client->dev);
782*4882a593Smuzhiyun for (i = 0; i < LED_MAX; i++) {
783*4882a593Smuzhiyun v4l2_device_unregister_subdev(&flash->leds[i].sd);
784*4882a593Smuzhiyun v4l2_ctrl_handler_free(&flash->leds[i].ctrls);
785*4882a593Smuzhiyun media_entity_cleanup(&flash->leds[i].sd.entity);
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun mutex_destroy(&flash->lock);
788*4882a593Smuzhiyun return 0;
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun
sgm3784_runtime_suspend(struct device * dev)791*4882a593Smuzhiyun static int __maybe_unused sgm3784_runtime_suspend(struct device *dev)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
794*4882a593Smuzhiyun struct sgm3784_flash *flash = i2c_get_clientdata(client);
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun return __sgm3784_set_power(flash, false);
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun
sgm3784_runtime_resume(struct device * dev)799*4882a593Smuzhiyun static int __maybe_unused sgm3784_runtime_resume(struct device *dev)
800*4882a593Smuzhiyun {
801*4882a593Smuzhiyun struct i2c_client *client = to_i2c_client(dev);
802*4882a593Smuzhiyun struct sgm3784_flash *flash = i2c_get_clientdata(client);
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun return __sgm3784_set_power(flash, true);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun static const struct i2c_device_id sgm3784_id_table[] = {
808*4882a593Smuzhiyun { SGM3784_NAME, 0 },
809*4882a593Smuzhiyun { { 0 } }
810*4882a593Smuzhiyun };
811*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, sgm3784_id_table);
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun static const struct of_device_id sgm3784_of_table[] = {
814*4882a593Smuzhiyun { .compatible = "sgmicro,gsm3784" },
815*4882a593Smuzhiyun { { 0 } }
816*4882a593Smuzhiyun };
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun static const struct dev_pm_ops sgm3784_pm_ops = {
819*4882a593Smuzhiyun SET_RUNTIME_PM_OPS(sgm3784_runtime_suspend,
820*4882a593Smuzhiyun sgm3784_runtime_resume, NULL)
821*4882a593Smuzhiyun };
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun static struct i2c_driver sgm3784_i2c_driver = {
824*4882a593Smuzhiyun .driver = {
825*4882a593Smuzhiyun .name = SGM3784_NAME,
826*4882a593Smuzhiyun .pm = &sgm3784_pm_ops,
827*4882a593Smuzhiyun .of_match_table = sgm3784_of_table,
828*4882a593Smuzhiyun },
829*4882a593Smuzhiyun .probe = sgm3784_probe,
830*4882a593Smuzhiyun .remove = sgm3784_remove,
831*4882a593Smuzhiyun .id_table = sgm3784_id_table,
832*4882a593Smuzhiyun };
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun module_i2c_driver(sgm3784_i2c_driver);
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun MODULE_DESCRIPTION("SGM3784 LED flash driver");
837*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
838