Revert "[COMMON] fimc-is2: add condition for flash S2MU106 initilize"
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / media / platform / exynos / fimc-is2 / sensor / module_framework / flash / fimc-is-flash-s2mu106.c
1 /*
2 * Samsung Exynos5 SoC series Flash driver
3 *
4 *
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd
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 #include <linux/gpio.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/platform_device.h>
17 #include <linux/of_gpio.h>
18
19 #include <linux/videodev2.h>
20 #include <linux/videodev2_exynos_camera.h>
21
22 #include "fimc-is-device-sensor.h"
23 #include "fimc-is-device-sensor-peri.h"
24 #include "fimc-is-core.h"
25
26 #include <linux/leds-s2mu106.h>
27
28 #define CAPTURE_MAX_TOTAL_CURRENT (1500)
29 #define TORCH_MAX_TOTAL_CURRENT (150)
30 #define MAX_FLASH_INTENSITY (256)
31
32 static int flash_s2mu106_init(struct v4l2_subdev *subdev, u32 val)
33 {
34 int ret = 0;
35 struct fimc_is_flash *flash;
36 int i;
37
38 FIMC_BUG(!subdev);
39
40 flash = (struct fimc_is_flash *)v4l2_get_subdevdata(subdev);
41
42 FIMC_BUG(!flash);
43
44 /* TODO: init flash driver */
45 flash->flash_data.mode = CAM2_FLASH_MODE_OFF;
46 flash->flash_data.intensity = 100; /* TODO: Need to figure out min/max range */
47 flash->flash_data.firing_time_us = 1 * 1000 * 1000; /* Max firing time is 1sec */
48 flash->flash_data.flash_fired = false;
49 flash->flash_data.cal_en = false;
50
51 for (i = 0; i < FLASH_LED_CH_MAX; i++) {
52 if (flash->led_ch[i] >= 0)
53 s2mu106_fled_set_mode_ctrl(flash->led_ch[i], CAM_FLASH_MODE_OFF);
54 }
55
56 return ret;
57 }
58
59 static int flash_s2mu106_adj_current(struct fimc_is_flash *flash, enum flash_mode mode, u32 intensity)
60 {
61 int adj_current = 0;
62 int max_current = 0;
63 int led_num = 0;
64 int i;
65
66 for (i = 0; i < FLASH_LED_CH_MAX; i++) {
67 if (flash->led_ch[i] != -1)
68 led_num++;
69 }
70
71 if (led_num == 0) {
72 err("wrong flash led number, set to 0");
73 return 0;
74 }
75
76 if (mode == CAM2_FLASH_MODE_SINGLE)
77 max_current = CAPTURE_MAX_TOTAL_CURRENT;
78 else if (mode == CAM2_FLASH_MODE_TORCH)
79 max_current = TORCH_MAX_TOTAL_CURRENT;
80 else
81 return 0;
82
83 #ifdef FLASH_CAL_DATA_ENABLE
84 if (led_num > 2)
85 warn("Num of LED is over 2: %d\n", led_num);
86
87 led_num = 0;
88
89 if (flash->flash_data.cal_en == false) {
90 /* flash or torch set by ddk */
91 adj_current = ((max_current * intensity) / MAX_FLASH_INTENSITY);
92
93 if (adj_current > max_current) {
94 warn("flash intensity(%d) > max(%d), set to max forcely",
95 adj_current, max_current);
96 adj_current = max_current;
97 }
98
99 for (i = 0; i < FLASH_LED_CH_MAX; i++) {
100 if (flash->led_ch[i] == -1)
101 continue;
102
103 led_num++;
104 if (led_num == 1) {
105 flash->flash_data.inp_current[i] = adj_current;
106 dbg_flash("[CH: %d] Flash set with adj_current: %d\n",
107 flash->led_ch[i], flash->flash_data.inp_current[i]);
108 } else if (led_num == 2) {
109 flash->flash_data.inp_current[i] = max_current - adj_current;
110 dbg_flash("[CH: %d] Flash set with adj_current: %d\n",
111 flash->led_ch[i], flash->flash_data.inp_current[i]);
112 } else {
113 warn("skip set current value\n");
114 }
115 }
116 } else {
117 /* flash or torch set by hal */
118 for (i = 0; i < FLASH_LED_CH_MAX; i++) {
119 if (flash->led_ch[i] != -1) {
120 led_num++;
121 adj_current = flash->flash_data.inp_current[i];
122 if (adj_current > max_current) {
123 warn("flash intensity(%d) > max(%d), set to max forcely",
124 adj_current, max_current);
125 flash->flash_data.inp_current[i] = max_current;
126 }
127 dbg_flash("[CH: %d] Flash set with adj_current: %d\n",
128 flash->led_ch[i], flash->flash_data.inp_current[i]);
129 }
130 }
131 }
132
133 dbg_flash("%s: mode: %s, led_numt: %d\n", __func__,
134 mode == CAM2_FLASH_MODE_OFF ? "OFF" :
135 mode == CAM2_FLASH_MODE_SINGLE ? "FLASH" : "TORCH",
136 led_num);
137
138 return 0;
139 #else
140 if (intensity > MAX_FLASH_INTENSITY) {
141 warn("flash intensity(%d) > max(%d), set to max forcely",
142 intensity, MAX_FLASH_INTENSITY);
143 intensity = MAX_FLASH_INTENSITY;
144 }
145
146 adj_current = ((max_current * intensity) / MAX_FLASH_INTENSITY) / led_num;
147
148 dbg_flash("%s: mode: %s, adj_current: %d\n", __func__,
149 mode == CAM2_FLASH_MODE_OFF ? "OFF" :
150 mode == CAM2_FLASH_MODE_SINGLE ? "FLASH" : "TORCH",
151 adj_current);
152
153 return adj_current;
154 #endif
155 }
156
157 static int flash_s2mu106_control(struct v4l2_subdev *subdev, enum flash_mode mode, u32 intensity)
158 {
159 int ret = 0;
160 struct fimc_is_flash *flash = NULL;
161 int i;
162 int adj_current = 0;
163
164 FIMC_BUG(!subdev);
165
166 flash = (struct fimc_is_flash *)v4l2_get_subdevdata(subdev);
167 FIMC_BUG(!flash);
168
169 dbg_flash("%s : mode = %s, intensity = %d\n", __func__,
170 mode == CAM2_FLASH_MODE_OFF ? "OFF" :
171 mode == CAM2_FLASH_MODE_SINGLE ? "FLASH" : "TORCH",
172 intensity);
173
174 adj_current = flash_s2mu106_adj_current(flash, mode, intensity);
175
176 for (i = 0; i < FLASH_LED_CH_MAX; i++) {
177 if (flash->led_ch[i] == -1)
178 continue;
179 #ifdef FLASH_CAL_DATA_ENABLE
180 adj_current = flash->flash_data.inp_current[i];
181
182 /* If adj_current value is zero, it must be skipped to set */
183 /* Even if zero is set to flash, 50mA will flow, because 50mA is minimized value */
184 if (adj_current == 0 && mode != CAM2_FLASH_MODE_OFF) {
185 dbg_flash("[CH: %d] current value is 0, so current set need to skip\n", flash->led_ch[i]);
186 continue;
187 }
188
189 dbg_flash("[CH: %d] current is set with val: %d\n", flash->led_ch[i], adj_current);
190 #endif
191 switch (mode) {
192 case CAM2_FLASH_MODE_OFF:
193 ret = s2mu106_fled_set_mode_ctrl(flash->led_ch[i], CAM_FLASH_MODE_OFF);
194 if (ret < 0) {
195 err("flash off fail(led_ch:%d)", flash->led_ch[i]);
196 ret = -EINVAL;
197 }
198 break;
199 case CAM2_FLASH_MODE_SINGLE:
200 ret = s2mu106_fled_set_curr(flash->led_ch[i], CAM_FLASH_MODE_SINGLE, adj_current);
201 if (ret < 0) {
202 err("capture flash set current fail(led_ch:%d)", flash->led_ch[i]);
203 ret = -EINVAL;
204 }
205
206 ret |= s2mu106_fled_set_mode_ctrl(flash->led_ch[i], CAM_FLASH_MODE_SINGLE);
207 if (ret < 0) {
208 err("capture flash on fail(led_ch:%d)", flash->led_ch[i]);
209 ret = -EINVAL;
210 }
211 break;
212 case CAM2_FLASH_MODE_TORCH:
213 ret = s2mu106_fled_set_curr(flash->led_ch[i], CAM_FLASH_MODE_TORCH, adj_current);
214 if (ret < 0) {
215 err("torch flash set current fail(led_ch:%d)", flash->led_ch[i]);
216 ret = -EINVAL;
217 }
218
219 ret |= s2mu106_fled_set_mode_ctrl(flash->led_ch[i], CAM_FLASH_MODE_TORCH);
220 if (ret < 0) {
221 err("torch flash on fail(led_ch:%d)", flash->led_ch[i]);
222 ret = -EINVAL;
223 }
224 break;
225 default:
226 err("Invalid flash mode");
227 ret = -EINVAL;
228 goto p_err;
229 }
230 }
231
232 p_err:
233 return ret;
234 }
235
236 int flash_s2mu106_s_ctrl(struct v4l2_subdev *subdev, struct v4l2_control *ctrl)
237 {
238 int ret = 0;
239 struct fimc_is_flash *flash = NULL;
240
241 FIMC_BUG(!subdev);
242
243 flash = (struct fimc_is_flash *)v4l2_get_subdevdata(subdev);
244 FIMC_BUG(!flash);
245
246 switch (ctrl->id) {
247 #ifdef FLASH_CAL_DATA_ENABLE
248 case V4L2_CID_FLASH_SET_CAL_EN:
249 if (ctrl->value < 0) {
250 err("failed to flash set cal_en: %d\n", ctrl->value);
251 ret = -EINVAL;
252 goto p_err;
253 }
254 flash->flash_data.cal_en = ctrl->value;
255 dbg_flash("cal_en ctrl set: %s\n", (flash->flash_data.cal_en ? "enable" : "disable"));
256 break;
257 case V4L2_CID_FLASH_SET_BY_CAL_CH0:
258 if (ctrl->value < 0) {
259 err("[ch0] failed to flash set current val by cal: %d\n", ctrl->value);
260 ret = -EINVAL;
261 goto p_err;
262 }
263 flash->flash_data.inp_current[0] = ctrl->value;
264 break;
265 case V4L2_CID_FLASH_SET_BY_CAL_CH1:
266 if (ctrl->value < 0) {
267 err("[ch1] failed to flash set current val by cal: %d\n", ctrl->value);
268 ret = -EINVAL;
269 goto p_err;
270 }
271 flash->flash_data.inp_current[1] = ctrl->value;
272 break;
273 #else
274 case V4L2_CID_FLASH_SET_INTENSITY:
275 /* TODO : Check min/max intensity */
276 if (ctrl->value < 0) {
277 err("failed to flash set intensity: %d\n", ctrl->value);
278 ret = -EINVAL;
279 goto p_err;
280 }
281 flash->flash_data.intensity = ctrl->value;
282 break;
283 #endif
284 case V4L2_CID_FLASH_SET_FIRING_TIME:
285 /* TODO : Check min/max firing time */
286 if (ctrl->value < 0) {
287 err("failed to flash set firing time: %d\n", ctrl->value);
288 ret = -EINVAL;
289 goto p_err;
290 }
291 flash->flash_data.firing_time_us = ctrl->value;
292 break;
293 case V4L2_CID_FLASH_SET_FIRE:
294 ret = flash_s2mu106_control(subdev, flash->flash_data.mode, ctrl->value);
295 if (ret) {
296 err("flash_s2mu106_control(mode:%d, val:%d) is fail(%d)",
297 (int)flash->flash_data.mode, ctrl->value, ret);
298 goto p_err;
299 }
300 break;
301 default:
302 err("err!!! Unknown CID(%#x)", ctrl->id);
303 ret = -EINVAL;
304 goto p_err;
305 }
306
307 p_err:
308 return ret;
309 }
310
311 static const struct v4l2_subdev_core_ops core_ops = {
312 .init = flash_s2mu106_init,
313 .s_ctrl = flash_s2mu106_s_ctrl,
314 };
315
316 static const struct v4l2_subdev_ops subdev_ops = {
317 .core = &core_ops,
318 };
319
320 static int __init flash_s2mu106_probe(struct device *dev, struct i2c_client *client)
321 {
322 int ret = 0;
323 struct fimc_is_core *core;
324 struct v4l2_subdev *subdev_flash;
325 struct fimc_is_device_sensor *device;
326 struct fimc_is_flash *flash;
327 u32 sensor_id = 0;
328 struct device_node *dnode;
329 int i, elements;
330
331 FIMC_BUG(!fimc_is_dev);
332 FIMC_BUG(!dev);
333
334 dnode = dev->of_node;
335
336 ret = of_property_read_u32(dnode, "id", &sensor_id);
337 if (ret) {
338 err("id read is fail(%d)", ret);
339 goto p_err;
340 }
341
342 core = (struct fimc_is_core *)dev_get_drvdata(fimc_is_dev);
343 if (!core) {
344 probe_info("core device is not yet probed");
345 ret = -EPROBE_DEFER;
346 goto p_err;
347 }
348
349 device = &core->sensor[sensor_id];
350
351 flash = kzalloc(sizeof(struct fimc_is_flash), GFP_KERNEL);
352 if (!flash) {
353 err("flash is NULL");
354 ret = -ENOMEM;
355 goto p_err;
356 }
357
358 subdev_flash = kzalloc(sizeof(struct v4l2_subdev), GFP_KERNEL);
359 if (!subdev_flash) {
360 err("subdev_flash is NULL");
361 ret = -ENOMEM;
362 kfree(flash);
363 goto p_err;
364 }
365
366 flash->id = FLADRV_NAME_S2MU106;
367 flash->subdev = subdev_flash;
368 flash->client = client;
369
370 flash->flash_data.mode = CAM2_FLASH_MODE_OFF;
371 flash->flash_data.intensity = 100; /* TODO: Need to figure out min/max range */
372 flash->flash_data.firing_time_us = 1 * 1000 * 1000; /* Max firing time is 1sec */
373
374 /* get flash_led ch by dt */
375 for (i = 0; i < FLASH_LED_CH_MAX; i++)
376 flash->led_ch[i] = -1;
377
378 elements = of_property_count_u32_elems(dnode, "led_ch");
379 if (elements < 0 || elements > FLASH_LED_CH_MAX) {
380 warn("flash led elements is too much or wrong(%d), set to max(%d)\n",
381 elements, FLASH_LED_CH_MAX);
382 elements = FLASH_LED_CH_MAX;
383 }
384
385 if (elements) {
386 if (of_property_read_u32_array(dnode, "led_ch", flash->led_ch, elements)) {
387 err("cannot get flash led_ch, set only ch1\n");
388 flash->led_ch[0] = 1;
389 }
390 } else {
391 probe_info("set flash_ch as default(only ch1)\n");
392 flash->led_ch[0] = 1;
393 }
394
395 device->subdev_flash = subdev_flash;
396 device->flash = flash;
397
398 v4l2_subdev_init(subdev_flash, &subdev_ops);
399
400 v4l2_set_subdevdata(subdev_flash, flash);
401 v4l2_set_subdev_hostdata(subdev_flash, device);
402 snprintf(subdev_flash->name, V4L2_SUBDEV_NAME_SIZE, "flash-subdev.%d", flash->id);
403
404 p_err:
405 return ret;
406 }
407
408 static int __init flash_s2mu106_platform_probe(struct platform_device *pdev)
409 {
410 int ret = 0;
411 struct device *dev;
412
413 FIMC_BUG(!pdev);
414
415 dev = &pdev->dev;
416
417 ret = flash_s2mu106_probe(dev, NULL);
418 if (ret < 0) {
419 probe_err("flash gpio probe fail(%d)\n", ret);
420 goto p_err;
421 }
422
423 probe_info("%s done\n", __func__);
424
425 p_err:
426 return ret;
427 }
428
429 static const struct of_device_id exynos_fimc_is_sensor_flash_s2mu106_match[] = {
430 {
431 .compatible = "samsung,sensor-flash-s2mu106",
432 },
433 {
434 .compatible = "samsung,sensor-front-flash-s2mu106",
435 },
436 {},
437 };
438 MODULE_DEVICE_TABLE(of, exynos_fimc_is_sensor_flash_s2mu106_match);
439
440 /* register platform driver */
441 static struct platform_driver sensor_flash_s2mu106_platform_driver = {
442 .driver = {
443 .name = "FIMC-IS-SENSOR-FLASH-S2MU106-PLATFORM",
444 .owner = THIS_MODULE,
445 .of_match_table = exynos_fimc_is_sensor_flash_s2mu106_match,
446 }
447 };
448
449 static int __init fimc_is_sensor_flash_s2mu106_init(void)
450 {
451 int ret;
452
453 ret = platform_driver_probe(&sensor_flash_s2mu106_platform_driver,
454 flash_s2mu106_platform_probe);
455 if (ret)
456 err("failed to probe %s driver: %d\n",
457 sensor_flash_s2mu106_platform_driver.driver.name, ret);
458
459 return ret;
460 }
461 late_initcall_sync(fimc_is_sensor_flash_s2mu106_init);