v4l2-flash-led-class.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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/of.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->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(&v4l2_flash->fled_cdev->led_cdev,
  98. brightness);
  99. } else {
  100. led_set_brightness(&v4l2_flash->iled_cdev->led_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->led_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->led_cdev;
  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(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(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. const struct led_flash_ops *fled_cdev_ops = fled_cdev->ops;
  273. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  274. struct v4l2_ctrl_config *ctrl_cfg;
  275. u32 mask;
  276. /* Init FLASH_FAULT ctrl data */
  277. if (flash_cfg->flash_faults) {
  278. ctrl_init_data[FLASH_FAULT].cid = V4L2_CID_FLASH_FAULT;
  279. ctrl_cfg = &ctrl_init_data[FLASH_FAULT].config;
  280. ctrl_cfg->id = V4L2_CID_FLASH_FAULT;
  281. ctrl_cfg->max = flash_cfg->flash_faults;
  282. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  283. V4L2_CTRL_FLAG_READ_ONLY;
  284. }
  285. /* Init FLASH_LED_MODE ctrl data */
  286. mask = 1 << V4L2_FLASH_LED_MODE_NONE |
  287. 1 << V4L2_FLASH_LED_MODE_TORCH;
  288. if (led_cdev->flags & LED_DEV_CAP_FLASH)
  289. mask |= 1 << V4L2_FLASH_LED_MODE_FLASH;
  290. ctrl_init_data[LED_MODE].cid = V4L2_CID_FLASH_LED_MODE;
  291. ctrl_cfg = &ctrl_init_data[LED_MODE].config;
  292. ctrl_cfg->id = V4L2_CID_FLASH_LED_MODE;
  293. ctrl_cfg->max = V4L2_FLASH_LED_MODE_TORCH;
  294. ctrl_cfg->menu_skip_mask = ~mask;
  295. ctrl_cfg->def = V4L2_FLASH_LED_MODE_NONE;
  296. ctrl_cfg->flags = 0;
  297. /* Init TORCH_INTENSITY ctrl data */
  298. ctrl_init_data[TORCH_INTENSITY].cid = V4L2_CID_FLASH_TORCH_INTENSITY;
  299. ctrl_cfg = &ctrl_init_data[TORCH_INTENSITY].config;
  300. __lfs_to_v4l2_ctrl_config(&flash_cfg->torch_intensity, ctrl_cfg);
  301. ctrl_cfg->id = V4L2_CID_FLASH_TORCH_INTENSITY;
  302. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  303. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  304. /* Init INDICATOR_INTENSITY ctrl data */
  305. if (v4l2_flash->iled_cdev) {
  306. ctrl_init_data[INDICATOR_INTENSITY].cid =
  307. V4L2_CID_FLASH_INDICATOR_INTENSITY;
  308. ctrl_cfg = &ctrl_init_data[INDICATOR_INTENSITY].config;
  309. __lfs_to_v4l2_ctrl_config(&flash_cfg->indicator_intensity,
  310. ctrl_cfg);
  311. ctrl_cfg->id = V4L2_CID_FLASH_INDICATOR_INTENSITY;
  312. ctrl_cfg->min = 0;
  313. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  314. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  315. }
  316. if (!(led_cdev->flags & LED_DEV_CAP_FLASH))
  317. return;
  318. /* Init FLASH_STROBE ctrl data */
  319. ctrl_init_data[FLASH_STROBE].cid = V4L2_CID_FLASH_STROBE;
  320. ctrl_cfg = &ctrl_init_data[FLASH_STROBE].config;
  321. ctrl_cfg->id = V4L2_CID_FLASH_STROBE;
  322. /* Init STROBE_STOP ctrl data */
  323. ctrl_init_data[STROBE_STOP].cid = V4L2_CID_FLASH_STROBE_STOP;
  324. ctrl_cfg = &ctrl_init_data[STROBE_STOP].config;
  325. ctrl_cfg->id = V4L2_CID_FLASH_STROBE_STOP;
  326. /* Init FLASH_STROBE_SOURCE ctrl data */
  327. if (flash_cfg->has_external_strobe) {
  328. mask = (1 << V4L2_FLASH_STROBE_SOURCE_SOFTWARE) |
  329. (1 << V4L2_FLASH_STROBE_SOURCE_EXTERNAL);
  330. ctrl_init_data[STROBE_SOURCE].cid =
  331. V4L2_CID_FLASH_STROBE_SOURCE;
  332. ctrl_cfg = &ctrl_init_data[STROBE_SOURCE].config;
  333. ctrl_cfg->id = V4L2_CID_FLASH_STROBE_SOURCE;
  334. ctrl_cfg->max = V4L2_FLASH_STROBE_SOURCE_EXTERNAL;
  335. ctrl_cfg->menu_skip_mask = ~mask;
  336. ctrl_cfg->def = V4L2_FLASH_STROBE_SOURCE_SOFTWARE;
  337. }
  338. /* Init STROBE_STATUS ctrl data */
  339. if (fled_cdev_ops->strobe_get) {
  340. ctrl_init_data[STROBE_STATUS].cid =
  341. V4L2_CID_FLASH_STROBE_STATUS;
  342. ctrl_cfg = &ctrl_init_data[STROBE_STATUS].config;
  343. ctrl_cfg->id = V4L2_CID_FLASH_STROBE_STATUS;
  344. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  345. V4L2_CTRL_FLAG_READ_ONLY;
  346. }
  347. /* Init FLASH_TIMEOUT ctrl data */
  348. if (fled_cdev_ops->timeout_set) {
  349. ctrl_init_data[FLASH_TIMEOUT].cid = V4L2_CID_FLASH_TIMEOUT;
  350. ctrl_cfg = &ctrl_init_data[FLASH_TIMEOUT].config;
  351. __lfs_to_v4l2_ctrl_config(&fled_cdev->timeout, ctrl_cfg);
  352. ctrl_cfg->id = V4L2_CID_FLASH_TIMEOUT;
  353. }
  354. /* Init FLASH_INTENSITY ctrl data */
  355. if (fled_cdev_ops->flash_brightness_set) {
  356. ctrl_init_data[FLASH_INTENSITY].cid = V4L2_CID_FLASH_INTENSITY;
  357. ctrl_cfg = &ctrl_init_data[FLASH_INTENSITY].config;
  358. __lfs_to_v4l2_ctrl_config(&fled_cdev->brightness, ctrl_cfg);
  359. ctrl_cfg->id = V4L2_CID_FLASH_INTENSITY;
  360. ctrl_cfg->flags = V4L2_CTRL_FLAG_VOLATILE |
  361. V4L2_CTRL_FLAG_EXECUTE_ON_WRITE;
  362. }
  363. }
  364. static int v4l2_flash_init_controls(struct v4l2_flash *v4l2_flash,
  365. struct v4l2_flash_config *flash_cfg)
  366. {
  367. struct v4l2_flash_ctrl_data *ctrl_init_data;
  368. struct v4l2_ctrl *ctrl;
  369. struct v4l2_ctrl_config *ctrl_cfg;
  370. int i, ret, num_ctrls = 0;
  371. v4l2_flash->ctrls = devm_kzalloc(v4l2_flash->sd.dev,
  372. sizeof(*v4l2_flash->ctrls) *
  373. (STROBE_SOURCE + 1), 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. v4l2_flash_set_led_brightness(v4l2_flash, ctrls[TORCH_INTENSITY]);
  429. if (ctrls[INDICATOR_INTENSITY])
  430. v4l2_flash_set_led_brightness(v4l2_flash,
  431. ctrls[INDICATOR_INTENSITY]);
  432. if (ctrls[FLASH_TIMEOUT]) {
  433. ret = led_set_flash_timeout(fled_cdev,
  434. ctrls[FLASH_TIMEOUT]->val);
  435. if (ret < 0)
  436. return ret;
  437. }
  438. if (ctrls[FLASH_INTENSITY]) {
  439. ret = led_set_flash_brightness(fled_cdev,
  440. ctrls[FLASH_INTENSITY]->val);
  441. if (ret < 0)
  442. return ret;
  443. }
  444. /*
  445. * For some hardware arrangements setting strobe source may affect
  446. * torch mode. Synchronize strobe source setting only if not in torch
  447. * mode. For torch mode case it will get synchronized upon switching
  448. * to flash mode.
  449. */
  450. if (ctrls[STROBE_SOURCE] &&
  451. ctrls[LED_MODE]->val != V4L2_FLASH_LED_MODE_TORCH)
  452. ret = call_flash_op(v4l2_flash, external_strobe_set,
  453. ctrls[STROBE_SOURCE]->val);
  454. return ret;
  455. }
  456. /*
  457. * V4L2 subdev internal operations
  458. */
  459. static int v4l2_flash_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  460. {
  461. struct v4l2_flash *v4l2_flash = v4l2_subdev_to_v4l2_flash(sd);
  462. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  463. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  464. struct led_classdev_flash *iled_cdev = v4l2_flash->iled_cdev;
  465. struct led_classdev *led_cdev_ind = NULL;
  466. int ret = 0;
  467. if (!v4l2_fh_is_singular(&fh->vfh))
  468. return 0;
  469. mutex_lock(&led_cdev->led_access);
  470. led_sysfs_disable(led_cdev);
  471. led_trigger_remove(led_cdev);
  472. mutex_unlock(&led_cdev->led_access);
  473. if (iled_cdev) {
  474. led_cdev_ind = &iled_cdev->led_cdev;
  475. mutex_lock(&led_cdev_ind->led_access);
  476. led_sysfs_disable(led_cdev_ind);
  477. led_trigger_remove(led_cdev_ind);
  478. mutex_unlock(&led_cdev_ind->led_access);
  479. }
  480. ret = __sync_device_with_v4l2_controls(v4l2_flash);
  481. if (ret < 0)
  482. goto out_sync_device;
  483. return 0;
  484. out_sync_device:
  485. mutex_lock(&led_cdev->led_access);
  486. led_sysfs_enable(led_cdev);
  487. mutex_unlock(&led_cdev->led_access);
  488. if (led_cdev_ind) {
  489. mutex_lock(&led_cdev_ind->led_access);
  490. led_sysfs_enable(led_cdev_ind);
  491. mutex_unlock(&led_cdev_ind->led_access);
  492. }
  493. return ret;
  494. }
  495. static int v4l2_flash_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  496. {
  497. struct v4l2_flash *v4l2_flash = v4l2_subdev_to_v4l2_flash(sd);
  498. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  499. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  500. struct led_classdev_flash *iled_cdev = v4l2_flash->iled_cdev;
  501. int ret = 0;
  502. if (!v4l2_fh_is_singular(&fh->vfh))
  503. return 0;
  504. mutex_lock(&led_cdev->led_access);
  505. if (v4l2_flash->ctrls[STROBE_SOURCE])
  506. ret = v4l2_ctrl_s_ctrl(v4l2_flash->ctrls[STROBE_SOURCE],
  507. V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
  508. led_sysfs_enable(led_cdev);
  509. mutex_unlock(&led_cdev->led_access);
  510. if (iled_cdev) {
  511. struct led_classdev *led_cdev_ind = &iled_cdev->led_cdev;
  512. mutex_lock(&led_cdev_ind->led_access);
  513. led_sysfs_enable(led_cdev_ind);
  514. mutex_unlock(&led_cdev_ind->led_access);
  515. }
  516. return ret;
  517. }
  518. static const struct v4l2_subdev_internal_ops v4l2_flash_subdev_internal_ops = {
  519. .open = v4l2_flash_open,
  520. .close = v4l2_flash_close,
  521. };
  522. static const struct v4l2_subdev_core_ops v4l2_flash_core_ops = {
  523. .queryctrl = v4l2_subdev_queryctrl,
  524. .querymenu = v4l2_subdev_querymenu,
  525. };
  526. static const struct v4l2_subdev_ops v4l2_flash_subdev_ops = {
  527. .core = &v4l2_flash_core_ops,
  528. };
  529. struct v4l2_flash *v4l2_flash_init(
  530. struct device *dev, struct device_node *of_node,
  531. struct led_classdev_flash *fled_cdev,
  532. struct led_classdev_flash *iled_cdev,
  533. const struct v4l2_flash_ops *ops,
  534. struct v4l2_flash_config *config)
  535. {
  536. struct v4l2_flash *v4l2_flash;
  537. struct led_classdev *led_cdev;
  538. struct v4l2_subdev *sd;
  539. int ret;
  540. if (!fled_cdev || !ops || !config)
  541. return ERR_PTR(-EINVAL);
  542. led_cdev = &fled_cdev->led_cdev;
  543. v4l2_flash = devm_kzalloc(led_cdev->dev, sizeof(*v4l2_flash),
  544. GFP_KERNEL);
  545. if (!v4l2_flash)
  546. return ERR_PTR(-ENOMEM);
  547. sd = &v4l2_flash->sd;
  548. v4l2_flash->fled_cdev = fled_cdev;
  549. v4l2_flash->iled_cdev = iled_cdev;
  550. v4l2_flash->ops = ops;
  551. sd->dev = dev;
  552. sd->of_node = of_node;
  553. v4l2_subdev_init(sd, &v4l2_flash_subdev_ops);
  554. sd->internal_ops = &v4l2_flash_subdev_internal_ops;
  555. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  556. strlcpy(sd->name, config->dev_name, sizeof(sd->name));
  557. ret = media_entity_init(&sd->entity, 0, NULL, 0);
  558. if (ret < 0)
  559. return ERR_PTR(ret);
  560. sd->entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
  561. ret = v4l2_flash_init_controls(v4l2_flash, config);
  562. if (ret < 0)
  563. goto err_init_controls;
  564. if (sd->of_node)
  565. of_node_get(sd->of_node);
  566. else
  567. of_node_get(led_cdev->dev->of_node);
  568. ret = v4l2_async_register_subdev(sd);
  569. if (ret < 0)
  570. goto err_async_register_sd;
  571. return v4l2_flash;
  572. err_async_register_sd:
  573. of_node_put(led_cdev->dev->of_node);
  574. v4l2_ctrl_handler_free(sd->ctrl_handler);
  575. err_init_controls:
  576. media_entity_cleanup(&sd->entity);
  577. return ERR_PTR(ret);
  578. }
  579. EXPORT_SYMBOL_GPL(v4l2_flash_init);
  580. void v4l2_flash_release(struct v4l2_flash *v4l2_flash)
  581. {
  582. struct v4l2_subdev *sd;
  583. struct led_classdev *led_cdev;
  584. if (IS_ERR_OR_NULL(v4l2_flash))
  585. return;
  586. sd = &v4l2_flash->sd;
  587. led_cdev = &v4l2_flash->fled_cdev->led_cdev;
  588. v4l2_async_unregister_subdev(sd);
  589. if (sd->of_node)
  590. of_node_put(sd->of_node);
  591. else
  592. of_node_put(led_cdev->dev->of_node);
  593. v4l2_ctrl_handler_free(sd->ctrl_handler);
  594. media_entity_cleanup(&sd->entity);
  595. }
  596. EXPORT_SYMBOL_GPL(v4l2_flash_release);
  597. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  598. MODULE_DESCRIPTION("V4L2 Flash sub-device helpers");
  599. MODULE_LICENSE("GPL v2");