v4l2-flash-led-class.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * V4L2 flash LED sub-device registration helpers.
  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. #include <linux/led-class-flash.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/property.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <media/v4l2-flash-led-class.h>
  18. #define has_flash_op(v4l2_flash, op) \
  19. (v4l2_flash && v4l2_flash->ops && v4l2_flash->ops->op)
  20. #define call_flash_op(v4l2_flash, op, arg) \
  21. (has_flash_op(v4l2_flash, op) ? \
  22. v4l2_flash->ops->op(v4l2_flash, arg) : \
  23. -EINVAL)
  24. enum ctrl_init_data_id {
  25. LED_MODE,
  26. TORCH_INTENSITY,
  27. FLASH_INTENSITY,
  28. INDICATOR_INTENSITY,
  29. FLASH_TIMEOUT,
  30. STROBE_SOURCE,
  31. /*
  32. * Only above values are applicable to
  33. * the 'ctrls' array in the struct v4l2_flash.
  34. */
  35. FLASH_STROBE,
  36. STROBE_STOP,
  37. STROBE_STATUS,
  38. FLASH_FAULT,
  39. NUM_FLASH_CTRLS,
  40. };
  41. static enum led_brightness __intensity_to_led_brightness(
  42. struct v4l2_ctrl *ctrl, s32 intensity)
  43. {
  44. intensity -= ctrl->minimum;
  45. intensity /= (u32) ctrl->step;
  46. /*
  47. * Indicator LEDs, unlike torch LEDs, are turned on/off basing on
  48. * the state of V4L2_CID_FLASH_INDICATOR_INTENSITY control only.
  49. * Therefore it must be possible to set it to 0 level which in
  50. * the LED subsystem reflects LED_OFF state.
  51. */
  52. if (ctrl->minimum)
  53. ++intensity;
  54. return intensity;
  55. }
  56. static s32 __led_brightness_to_intensity(struct v4l2_ctrl *ctrl,
  57. enum led_brightness brightness)
  58. {
  59. /*
  60. * Indicator LEDs, unlike torch LEDs, are turned on/off basing on
  61. * the state of V4L2_CID_FLASH_INDICATOR_INTENSITY control only.
  62. * Do not decrement brightness read from the LED subsystem for
  63. * indicator LED as it may equal 0. For torch LEDs this function
  64. * is called only when V4L2_FLASH_LED_MODE_TORCH is set and the
  65. * brightness read is guaranteed to be greater than 0. In the mode
  66. * V4L2_FLASH_LED_MODE_NONE the cached torch intensity value is used.
  67. */
  68. if (ctrl->id != V4L2_CID_FLASH_INDICATOR_INTENSITY)
  69. --brightness;
  70. return (brightness * ctrl->step) + ctrl->minimum;
  71. }
  72. static void v4l2_flash_set_led_brightness(struct v4l2_flash *v4l2_flash,
  73. struct v4l2_ctrl *ctrl)
  74. {
  75. struct v4l2_ctrl **ctrls = v4l2_flash->ctrls;
  76. enum led_brightness brightness;
  77. if (has_flash_op(v4l2_flash, intensity_to_led_brightness))
  78. brightness = call_flash_op(v4l2_flash,
  79. intensity_to_led_brightness,
  80. ctrl->val);
  81. else
  82. brightness = __intensity_to_led_brightness(ctrl, ctrl->val);
  83. /*
  84. * In case a LED Flash class driver provides ops for custom
  85. * brightness <-> intensity conversion, it also must have defined
  86. * related v4l2 control step == 1. In such a case a backward conversion
  87. * from led brightness to v4l2 intensity is required to find out the
  88. * the aligned intensity value.
  89. */
  90. if (has_flash_op(v4l2_flash, led_brightness_to_intensity))
  91. ctrl->val = call_flash_op(v4l2_flash,
  92. led_brightness_to_intensity,
  93. brightness);
  94. if (ctrl == ctrls[TORCH_INTENSITY]) {
  95. if (ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_TORCH)
  96. return;
  97. led_set_brightness_sync(&v4l2_flash->fled_cdev->led_cdev,
  98. brightness);
  99. } else {
  100. led_set_brightness_sync(v4l2_flash->iled_cdev,
  101. brightness);
  102. }
  103. }
  104. static int v4l2_flash_update_led_brightness(struct v4l2_flash *v4l2_flash,
  105. struct v4l2_ctrl *ctrl)
  106. {
  107. struct v4l2_ctrl **ctrls = v4l2_flash->ctrls;
  108. struct led_classdev *led_cdev;
  109. int ret;
  110. if (ctrl == ctrls[TORCH_INTENSITY]) {
  111. /*
  112. * Update torch brightness only if in TORCH_MODE. In other modes
  113. * torch led is turned off, which would spuriously inform the
  114. * user space that V4L2_CID_FLASH_TORCH_INTENSITY control value
  115. * has changed to 0.
  116. */
  117. if (ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_TORCH)
  118. return 0;
  119. led_cdev = &v4l2_flash->fled_cdev->led_cdev;
  120. } else {
  121. led_cdev = v4l2_flash->iled_cdev;
  122. }
  123. ret = led_update_brightness(led_cdev);
  124. if (ret < 0)
  125. return ret;
  126. if (has_flash_op(v4l2_flash, led_brightness_to_intensity))
  127. ctrl->val = call_flash_op(v4l2_flash,
  128. led_brightness_to_intensity,
  129. led_cdev->brightness);
  130. else
  131. ctrl->val = __led_brightness_to_intensity(ctrl,
  132. led_cdev->brightness);
  133. return 0;
  134. }
  135. static int v4l2_flash_g_volatile_ctrl(struct v4l2_ctrl *c)
  136. {
  137. struct v4l2_flash *v4l2_flash = v4l2_ctrl_to_v4l2_flash(c);
  138. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  139. bool is_strobing;
  140. int ret;
  141. switch (c->id) {
  142. case V4L2_CID_FLASH_TORCH_INTENSITY:
  143. case V4L2_CID_FLASH_INDICATOR_INTENSITY:
  144. return v4l2_flash_update_led_brightness(v4l2_flash, c);
  145. case V4L2_CID_FLASH_INTENSITY:
  146. ret = led_update_flash_brightness(fled_cdev);
  147. if (ret < 0)
  148. return ret;
  149. /*
  150. * No conversion is needed as LED Flash class also uses
  151. * microamperes for flash intensity units.
  152. */
  153. c->val = fled_cdev->brightness.val;
  154. return 0;
  155. case V4L2_CID_FLASH_STROBE_STATUS:
  156. ret = led_get_flash_strobe(fled_cdev, &is_strobing);
  157. if (ret < 0)
  158. return ret;
  159. c->val = is_strobing;
  160. return 0;
  161. case V4L2_CID_FLASH_FAULT:
  162. /* LED faults map directly to V4L2 flash faults */
  163. return led_get_flash_fault(fled_cdev, &c->val);
  164. default:
  165. return -EINVAL;
  166. }
  167. }
  168. static bool __software_strobe_mode_inactive(struct v4l2_ctrl **ctrls)
  169. {
  170. return ((ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_FLASH) ||
  171. (ctrls[STROBE_SOURCE] && (ctrls[STROBE_SOURCE]->val !=
  172. V4L2_FLASH_STROBE_SOURCE_SOFTWARE)));
  173. }
  174. static int v4l2_flash_s_ctrl(struct v4l2_ctrl *c)
  175. {
  176. struct v4l2_flash *v4l2_flash = v4l2_ctrl_to_v4l2_flash(c);
  177. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  178. struct led_classdev *led_cdev = fled_cdev ? &fled_cdev->led_cdev : NULL;
  179. struct v4l2_ctrl **ctrls = v4l2_flash->ctrls;
  180. bool external_strobe;
  181. int ret = 0;
  182. switch (c->id) {
  183. case V4L2_CID_FLASH_LED_MODE:
  184. switch (c->val) {
  185. case V4L2_FLASH_LED_MODE_NONE:
  186. led_set_brightness_sync(led_cdev, LED_OFF);
  187. return led_set_flash_strobe(fled_cdev, false);
  188. case V4L2_FLASH_LED_MODE_FLASH:
  189. /* Turn the torch LED off */
  190. led_set_brightness_sync(led_cdev, LED_OFF);
  191. if (ctrls[STROBE_SOURCE]) {
  192. external_strobe = (ctrls[STROBE_SOURCE]->val ==
  193. V4L2_FLASH_STROBE_SOURCE_EXTERNAL);
  194. ret = call_flash_op(v4l2_flash,
  195. external_strobe_set,
  196. external_strobe);
  197. }
  198. return ret;
  199. case V4L2_FLASH_LED_MODE_TORCH:
  200. if (ctrls[STROBE_SOURCE]) {
  201. ret = call_flash_op(v4l2_flash,
  202. external_strobe_set,
  203. false);
  204. if (ret < 0)
  205. return ret;
  206. }
  207. /* Stop flash strobing */
  208. ret = led_set_flash_strobe(fled_cdev, false);
  209. if (ret < 0)
  210. return ret;
  211. v4l2_flash_set_led_brightness(v4l2_flash,
  212. ctrls[TORCH_INTENSITY]);
  213. return 0;
  214. }
  215. break;
  216. case V4L2_CID_FLASH_STROBE_SOURCE:
  217. external_strobe = (c->val == V4L2_FLASH_STROBE_SOURCE_EXTERNAL);
  218. /*
  219. * For some hardware arrangements setting strobe source may
  220. * affect torch mode. Therefore, if not in the flash mode,
  221. * cache only this setting. It will be applied upon switching
  222. * to flash mode.
  223. */
  224. if (ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_FLASH)
  225. return 0;
  226. return call_flash_op(v4l2_flash, external_strobe_set,
  227. external_strobe);
  228. case V4L2_CID_FLASH_STROBE:
  229. if (__software_strobe_mode_inactive(ctrls))
  230. return -EBUSY;
  231. return led_set_flash_strobe(fled_cdev, true);
  232. case V4L2_CID_FLASH_STROBE_STOP:
  233. if (__software_strobe_mode_inactive(ctrls))
  234. return -EBUSY;
  235. return led_set_flash_strobe(fled_cdev, false);
  236. case V4L2_CID_FLASH_TIMEOUT:
  237. /*
  238. * No conversion is needed as LED Flash class also uses
  239. * microseconds for flash timeout units.
  240. */
  241. return led_set_flash_timeout(fled_cdev, c->val);
  242. case V4L2_CID_FLASH_INTENSITY:
  243. /*
  244. * No conversion is needed as LED Flash class also uses
  245. * microamperes for flash intensity units.
  246. */
  247. return led_set_flash_brightness(fled_cdev, c->val);
  248. case V4L2_CID_FLASH_TORCH_INTENSITY:
  249. case V4L2_CID_FLASH_INDICATOR_INTENSITY:
  250. v4l2_flash_set_led_brightness(v4l2_flash, c);
  251. return 0;
  252. }
  253. return -EINVAL;
  254. }
  255. static const struct v4l2_ctrl_ops v4l2_flash_ctrl_ops = {
  256. .g_volatile_ctrl = v4l2_flash_g_volatile_ctrl,
  257. .s_ctrl = v4l2_flash_s_ctrl,
  258. };
  259. static void __lfs_to_v4l2_ctrl_config(struct led_flash_setting *s,
  260. struct v4l2_ctrl_config *c)
  261. {
  262. c->min = s->min;
  263. c->max = s->max;
  264. c->step = s->step;
  265. c->def = s->val;
  266. }
  267. static void __fill_ctrl_init_data(struct v4l2_flash *v4l2_flash,
  268. struct v4l2_flash_config *flash_cfg,
  269. struct v4l2_flash_ctrl_data *ctrl_init_data)
  270. {
  271. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  272. struct led_classdev *led_cdev = fled_cdev ? &fled_cdev->led_cdev : NULL;
  273. struct v4l2_ctrl_config *ctrl_cfg;
  274. u32 mask;
  275. /* Init INDICATOR_INTENSITY ctrl data */
  276. if (v4l2_flash->iled_cdev) {
  277. ctrl_init_data[INDICATOR_INTENSITY].cid =
  278. V4L2_CID_FLASH_INDICATOR_INTENSITY;
  279. ctrl_cfg = &ctrl_init_data[INDICATOR_INTENSITY].config;
  280. __lfs_to_v4l2_ctrl_config(&flash_cfg->intensity,
  281. ctrl_cfg);
  282. ctrl_cfg->id = V4L2_CID_FLASH_INDICATOR_INTENSITY;
  283. ctrl_cfg->min = 0;
  284. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  285. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  286. }
  287. if (!led_cdev || WARN_ON(!(led_cdev->flags & LED_DEV_CAP_FLASH)))
  288. return;
  289. /* Init FLASH_FAULT ctrl data */
  290. if (flash_cfg->flash_faults) {
  291. ctrl_init_data[FLASH_FAULT].cid = V4L2_CID_FLASH_FAULT;
  292. ctrl_cfg = &ctrl_init_data[FLASH_FAULT].config;
  293. ctrl_cfg->id = V4L2_CID_FLASH_FAULT;
  294. ctrl_cfg->max = flash_cfg->flash_faults;
  295. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  296. V4L2_CTRL_FLAG_READ_ONLY;
  297. }
  298. /* Init FLASH_LED_MODE ctrl data */
  299. mask = 1 << V4L2_FLASH_LED_MODE_NONE |
  300. 1 << V4L2_FLASH_LED_MODE_TORCH;
  301. if (led_cdev->flags & LED_DEV_CAP_FLASH)
  302. mask |= 1 << V4L2_FLASH_LED_MODE_FLASH;
  303. ctrl_init_data[LED_MODE].cid = V4L2_CID_FLASH_LED_MODE;
  304. ctrl_cfg = &ctrl_init_data[LED_MODE].config;
  305. ctrl_cfg->id = V4L2_CID_FLASH_LED_MODE;
  306. ctrl_cfg->max = V4L2_FLASH_LED_MODE_TORCH;
  307. ctrl_cfg->menu_skip_mask = ~mask;
  308. ctrl_cfg->def = V4L2_FLASH_LED_MODE_NONE;
  309. ctrl_cfg->flags = 0;
  310. /* Init TORCH_INTENSITY ctrl data */
  311. ctrl_init_data[TORCH_INTENSITY].cid = V4L2_CID_FLASH_TORCH_INTENSITY;
  312. ctrl_cfg = &ctrl_init_data[TORCH_INTENSITY].config;
  313. __lfs_to_v4l2_ctrl_config(&flash_cfg->intensity, ctrl_cfg);
  314. ctrl_cfg->id = V4L2_CID_FLASH_TORCH_INTENSITY;
  315. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  316. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  317. /* Init FLASH_STROBE ctrl data */
  318. ctrl_init_data[FLASH_STROBE].cid = V4L2_CID_FLASH_STROBE;
  319. ctrl_cfg = &ctrl_init_data[FLASH_STROBE].config;
  320. ctrl_cfg->id = V4L2_CID_FLASH_STROBE;
  321. /* Init STROBE_STOP ctrl data */
  322. ctrl_init_data[STROBE_STOP].cid = V4L2_CID_FLASH_STROBE_STOP;
  323. ctrl_cfg = &ctrl_init_data[STROBE_STOP].config;
  324. ctrl_cfg->id = V4L2_CID_FLASH_STROBE_STOP;
  325. /* Init FLASH_STROBE_SOURCE ctrl data */
  326. if (flash_cfg->has_external_strobe) {
  327. mask = (1 << V4L2_FLASH_STROBE_SOURCE_SOFTWARE) |
  328. (1 << V4L2_FLASH_STROBE_SOURCE_EXTERNAL);
  329. ctrl_init_data[STROBE_SOURCE].cid =
  330. V4L2_CID_FLASH_STROBE_SOURCE;
  331. ctrl_cfg = &ctrl_init_data[STROBE_SOURCE].config;
  332. ctrl_cfg->id = V4L2_CID_FLASH_STROBE_SOURCE;
  333. ctrl_cfg->max = V4L2_FLASH_STROBE_SOURCE_EXTERNAL;
  334. ctrl_cfg->menu_skip_mask = ~mask;
  335. ctrl_cfg->def = V4L2_FLASH_STROBE_SOURCE_SOFTWARE;
  336. }
  337. /* Init STROBE_STATUS ctrl data */
  338. if (has_flash_op(fled_cdev, strobe_get)) {
  339. ctrl_init_data[STROBE_STATUS].cid =
  340. V4L2_CID_FLASH_STROBE_STATUS;
  341. ctrl_cfg = &ctrl_init_data[STROBE_STATUS].config;
  342. ctrl_cfg->id = V4L2_CID_FLASH_STROBE_STATUS;
  343. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  344. V4L2_CTRL_FLAG_READ_ONLY;
  345. }
  346. /* Init FLASH_TIMEOUT ctrl data */
  347. if (has_flash_op(fled_cdev, timeout_set)) {
  348. ctrl_init_data[FLASH_TIMEOUT].cid = V4L2_CID_FLASH_TIMEOUT;
  349. ctrl_cfg = &ctrl_init_data[FLASH_TIMEOUT].config;
  350. __lfs_to_v4l2_ctrl_config(&fled_cdev->timeout, ctrl_cfg);
  351. ctrl_cfg->id = V4L2_CID_FLASH_TIMEOUT;
  352. }
  353. /* Init FLASH_INTENSITY ctrl data */
  354. if (has_flash_op(fled_cdev, flash_brightness_set)) {
  355. ctrl_init_data[FLASH_INTENSITY].cid = V4L2_CID_FLASH_INTENSITY;
  356. ctrl_cfg = &ctrl_init_data[FLASH_INTENSITY].config;
  357. __lfs_to_v4l2_ctrl_config(&fled_cdev->brightness, ctrl_cfg);
  358. ctrl_cfg->id = V4L2_CID_FLASH_INTENSITY;
  359. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  360. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  361. }
  362. }
  363. static int v4l2_flash_init_controls(struct v4l2_flash *v4l2_flash,
  364. struct v4l2_flash_config *flash_cfg)
  365. {
  366. struct v4l2_flash_ctrl_data *ctrl_init_data;
  367. struct v4l2_ctrl *ctrl;
  368. struct v4l2_ctrl_config *ctrl_cfg;
  369. int i, ret, num_ctrls = 0;
  370. v4l2_flash->ctrls = devm_kcalloc(v4l2_flash->sd.dev,
  371. STROBE_SOURCE + 1,
  372. sizeof(*v4l2_flash->ctrls),
  373. GFP_KERNEL);
  374. if (!v4l2_flash->ctrls)
  375. return -ENOMEM;
  376. /* allocate memory dynamically so as not to exceed stack frame size */
  377. ctrl_init_data = kcalloc(NUM_FLASH_CTRLS, sizeof(*ctrl_init_data),
  378. GFP_KERNEL);
  379. if (!ctrl_init_data)
  380. return -ENOMEM;
  381. __fill_ctrl_init_data(v4l2_flash, flash_cfg, ctrl_init_data);
  382. for (i = 0; i < NUM_FLASH_CTRLS; ++i)
  383. if (ctrl_init_data[i].cid)
  384. ++num_ctrls;
  385. v4l2_ctrl_handler_init(&v4l2_flash->hdl, num_ctrls);
  386. for (i = 0; i < NUM_FLASH_CTRLS; ++i) {
  387. ctrl_cfg = &ctrl_init_data[i].config;
  388. if (!ctrl_init_data[i].cid)
  389. continue;
  390. if (ctrl_cfg->id == V4L2_CID_FLASH_LED_MODE ||
  391. ctrl_cfg->id == V4L2_CID_FLASH_STROBE_SOURCE)
  392. ctrl = v4l2_ctrl_new_std_menu(&v4l2_flash->hdl,
  393. &v4l2_flash_ctrl_ops,
  394. ctrl_cfg->id,
  395. ctrl_cfg->max,
  396. ctrl_cfg->menu_skip_mask,
  397. ctrl_cfg->def);
  398. else
  399. ctrl = v4l2_ctrl_new_std(&v4l2_flash->hdl,
  400. &v4l2_flash_ctrl_ops,
  401. ctrl_cfg->id,
  402. ctrl_cfg->min,
  403. ctrl_cfg->max,
  404. ctrl_cfg->step,
  405. ctrl_cfg->def);
  406. if (ctrl)
  407. ctrl->flags |= ctrl_cfg->flags;
  408. if (i <= STROBE_SOURCE)
  409. v4l2_flash->ctrls[i] = ctrl;
  410. }
  411. kfree(ctrl_init_data);
  412. if (v4l2_flash->hdl.error) {
  413. ret = v4l2_flash->hdl.error;
  414. goto error_free_handler;
  415. }
  416. v4l2_ctrl_handler_setup(&v4l2_flash->hdl);
  417. v4l2_flash->sd.ctrl_handler = &v4l2_flash->hdl;
  418. return 0;
  419. error_free_handler:
  420. v4l2_ctrl_handler_free(&v4l2_flash->hdl);
  421. return ret;
  422. }
  423. static int __sync_device_with_v4l2_controls(struct v4l2_flash *v4l2_flash)
  424. {
  425. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  426. struct v4l2_ctrl **ctrls = v4l2_flash->ctrls;
  427. int ret = 0;
  428. if (ctrls[TORCH_INTENSITY])
  429. v4l2_flash_set_led_brightness(v4l2_flash,
  430. ctrls[TORCH_INTENSITY]);
  431. if (ctrls[INDICATOR_INTENSITY])
  432. v4l2_flash_set_led_brightness(v4l2_flash,
  433. ctrls[INDICATOR_INTENSITY]);
  434. if (ctrls[FLASH_TIMEOUT]) {
  435. ret = led_set_flash_timeout(fled_cdev,
  436. ctrls[FLASH_TIMEOUT]->val);
  437. if (ret < 0)
  438. return ret;
  439. }
  440. if (ctrls[FLASH_INTENSITY]) {
  441. ret = led_set_flash_brightness(fled_cdev,
  442. ctrls[FLASH_INTENSITY]->val);
  443. if (ret < 0)
  444. return ret;
  445. }
  446. /*
  447. * For some hardware arrangements setting strobe source may affect
  448. * torch mode. Synchronize strobe source setting only if not in torch
  449. * mode. For torch mode case it will get synchronized upon switching
  450. * to flash mode.
  451. */
  452. if (ctrls[STROBE_SOURCE] &&
  453. ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_TORCH)
  454. ret = call_flash_op(v4l2_flash, external_strobe_set,
  455. ctrls[STROBE_SOURCE]->val);
  456. return ret;
  457. }
  458. /*
  459. * V4L2 subdev internal operations
  460. */
  461. static int v4l2_flash_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  462. {
  463. struct v4l2_flash *v4l2_flash = v4l2_subdev_to_v4l2_flash(sd);
  464. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  465. struct led_classdev *led_cdev = fled_cdev ? &fled_cdev->led_cdev : NULL;
  466. struct led_classdev *led_cdev_ind = v4l2_flash->iled_cdev;
  467. int ret = 0;
  468. if (!v4l2_fh_is_singular(&fh->vfh))
  469. return 0;
  470. if (led_cdev) {
  471. mutex_lock(&led_cdev->led_access);
  472. led_sysfs_disable(led_cdev);
  473. led_trigger_remove(led_cdev);
  474. mutex_unlock(&led_cdev->led_access);
  475. }
  476. if (led_cdev_ind) {
  477. mutex_lock(&led_cdev_ind->led_access);
  478. led_sysfs_disable(led_cdev_ind);
  479. led_trigger_remove(led_cdev_ind);
  480. mutex_unlock(&led_cdev_ind->led_access);
  481. }
  482. ret = __sync_device_with_v4l2_controls(v4l2_flash);
  483. if (ret < 0)
  484. goto out_sync_device;
  485. return 0;
  486. out_sync_device:
  487. if (led_cdev) {
  488. mutex_lock(&led_cdev->led_access);
  489. led_sysfs_enable(led_cdev);
  490. mutex_unlock(&led_cdev->led_access);
  491. }
  492. if (led_cdev_ind) {
  493. mutex_lock(&led_cdev_ind->led_access);
  494. led_sysfs_enable(led_cdev_ind);
  495. mutex_unlock(&led_cdev_ind->led_access);
  496. }
  497. return ret;
  498. }
  499. static int v4l2_flash_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  500. {
  501. struct v4l2_flash *v4l2_flash = v4l2_subdev_to_v4l2_flash(sd);
  502. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  503. struct led_classdev *led_cdev = fled_cdev ? &fled_cdev->led_cdev : NULL;
  504. struct led_classdev *led_cdev_ind = v4l2_flash->iled_cdev;
  505. int ret = 0;
  506. if (!v4l2_fh_is_singular(&fh->vfh))
  507. return 0;
  508. if (led_cdev) {
  509. mutex_lock(&led_cdev->led_access);
  510. if (v4l2_flash->ctrls[STROBE_SOURCE])
  511. ret = v4l2_ctrl_s_ctrl(
  512. v4l2_flash->ctrls[STROBE_SOURCE],
  513. V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
  514. led_sysfs_enable(led_cdev);
  515. mutex_unlock(&led_cdev->led_access);
  516. }
  517. if (led_cdev_ind) {
  518. mutex_lock(&led_cdev_ind->led_access);
  519. led_sysfs_enable(led_cdev_ind);
  520. mutex_unlock(&led_cdev_ind->led_access);
  521. }
  522. return ret;
  523. }
  524. static const struct v4l2_subdev_internal_ops v4l2_flash_subdev_internal_ops = {
  525. .open = v4l2_flash_open,
  526. .close = v4l2_flash_close,
  527. };
  528. static const struct v4l2_subdev_ops v4l2_flash_subdev_ops;
  529. static struct v4l2_flash *__v4l2_flash_init(
  530. struct device *dev, struct fwnode_handle *fwn,
  531. struct led_classdev_flash *fled_cdev, struct led_classdev *iled_cdev,
  532. const struct v4l2_flash_ops *ops, struct v4l2_flash_config *config)
  533. {
  534. struct v4l2_flash *v4l2_flash;
  535. struct v4l2_subdev *sd;
  536. int ret;
  537. if (!config)
  538. return ERR_PTR(-EINVAL);
  539. v4l2_flash = devm_kzalloc(dev, sizeof(*v4l2_flash), GFP_KERNEL);
  540. if (!v4l2_flash)
  541. return ERR_PTR(-ENOMEM);
  542. sd = &v4l2_flash->sd;
  543. v4l2_flash->fled_cdev = fled_cdev;
  544. v4l2_flash->iled_cdev = iled_cdev;
  545. v4l2_flash->ops = ops;
  546. sd->dev = dev;
  547. sd->fwnode = fwn ? fwn : dev_fwnode(dev);
  548. v4l2_subdev_init(sd, &v4l2_flash_subdev_ops);
  549. sd->internal_ops = &v4l2_flash_subdev_internal_ops;
  550. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  551. strlcpy(sd->name, config->dev_name, sizeof(sd->name));
  552. ret = media_entity_pads_init(&sd->entity, 0, NULL);
  553. if (ret < 0)
  554. return ERR_PTR(ret);
  555. sd->entity.function = MEDIA_ENT_F_FLASH;
  556. ret = v4l2_flash_init_controls(v4l2_flash, config);
  557. if (ret < 0)
  558. goto err_init_controls;
  559. fwnode_handle_get(sd->fwnode);
  560. ret = v4l2_async_register_subdev(sd);
  561. if (ret < 0)
  562. goto err_async_register_sd;
  563. return v4l2_flash;
  564. err_async_register_sd:
  565. fwnode_handle_put(sd->fwnode);
  566. v4l2_ctrl_handler_free(sd->ctrl_handler);
  567. err_init_controls:
  568. media_entity_cleanup(&sd->entity);
  569. return ERR_PTR(ret);
  570. }
  571. struct v4l2_flash *v4l2_flash_init(
  572. struct device *dev, struct fwnode_handle *fwn,
  573. struct led_classdev_flash *fled_cdev,
  574. const struct v4l2_flash_ops *ops,
  575. struct v4l2_flash_config *config)
  576. {
  577. return __v4l2_flash_init(dev, fwn, fled_cdev, NULL, ops, config);
  578. }
  579. EXPORT_SYMBOL_GPL(v4l2_flash_init);
  580. struct v4l2_flash *v4l2_flash_indicator_init(
  581. struct device *dev, struct fwnode_handle *fwn,
  582. struct led_classdev *iled_cdev,
  583. struct v4l2_flash_config *config)
  584. {
  585. return __v4l2_flash_init(dev, fwn, NULL, iled_cdev, NULL, config);
  586. }
  587. EXPORT_SYMBOL_GPL(v4l2_flash_indicator_init);
  588. void v4l2_flash_release(struct v4l2_flash *v4l2_flash)
  589. {
  590. struct v4l2_subdev *sd;
  591. if (IS_ERR_OR_NULL(v4l2_flash))
  592. return;
  593. sd = &v4l2_flash->sd;
  594. v4l2_async_unregister_subdev(sd);
  595. fwnode_handle_put(sd->fwnode);
  596. v4l2_ctrl_handler_free(sd->ctrl_handler);
  597. media_entity_cleanup(&sd->entity);
  598. }
  599. EXPORT_SYMBOL_GPL(v4l2_flash_release);
  600. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  601. MODULE_DESCRIPTION("V4L2 Flash sub-device helpers");
  602. MODULE_LICENSE("GPL v2");