adp1653.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * drivers/media/video/adp1653.c
  3. *
  4. * Copyright (C) 2008--2011 Nokia Corporation
  5. *
  6. * Contact: Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>
  7. *
  8. * Contributors:
  9. * Sakari Ailus <sakari.ailus@maxwell.research.nokia.com>
  10. * Tuukka Toivonen <tuukkat76@gmail.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * version 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  24. * 02110-1301 USA
  25. *
  26. * TODO:
  27. * - fault interrupt handling
  28. * - hardware strobe
  29. * - power doesn't need to be ON if all lights are off
  30. *
  31. */
  32. #include <linux/delay.h>
  33. #include <linux/module.h>
  34. #include <linux/i2c.h>
  35. #include <linux/slab.h>
  36. #include <linux/version.h>
  37. #include <media/adp1653.h>
  38. #include <media/v4l2-device.h>
  39. #define TIMEOUT_MAX 820000
  40. #define TIMEOUT_STEP 54600
  41. #define TIMEOUT_MIN (TIMEOUT_MAX - ADP1653_REG_CONFIG_TMR_SET_MAX \
  42. * TIMEOUT_STEP)
  43. #define TIMEOUT_US_TO_CODE(t) ((TIMEOUT_MAX + (TIMEOUT_STEP / 2) - (t)) \
  44. / TIMEOUT_STEP)
  45. #define TIMEOUT_CODE_TO_US(c) (TIMEOUT_MAX - (c) * TIMEOUT_STEP)
  46. /* Write values into ADP1653 registers. */
  47. static int adp1653_update_hw(struct adp1653_flash *flash)
  48. {
  49. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  50. u8 out_sel;
  51. u8 config = 0;
  52. int rval;
  53. out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
  54. flash->indicator_intensity->val)
  55. << ADP1653_REG_OUT_SEL_ILED_SHIFT;
  56. switch (flash->led_mode->val) {
  57. case V4L2_FLASH_LED_MODE_NONE:
  58. break;
  59. case V4L2_FLASH_LED_MODE_FLASH:
  60. /* Flash mode, light on with strobe, duration from timer */
  61. config = ADP1653_REG_CONFIG_TMR_CFG;
  62. config |= TIMEOUT_US_TO_CODE(flash->flash_timeout->val)
  63. << ADP1653_REG_CONFIG_TMR_SET_SHIFT;
  64. break;
  65. case V4L2_FLASH_LED_MODE_TORCH:
  66. /* Torch mode, light immediately on, duration indefinite */
  67. out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
  68. flash->torch_intensity->val)
  69. << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
  70. break;
  71. }
  72. rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
  73. if (rval < 0)
  74. return rval;
  75. rval = i2c_smbus_write_byte_data(client, ADP1653_REG_CONFIG, config);
  76. if (rval < 0)
  77. return rval;
  78. return 0;
  79. }
  80. static int adp1653_get_fault(struct adp1653_flash *flash)
  81. {
  82. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  83. int fault;
  84. int rval;
  85. fault = i2c_smbus_read_byte_data(client, ADP1653_REG_FAULT);
  86. if (IS_ERR_VALUE(fault))
  87. return fault;
  88. flash->fault |= fault;
  89. if (!flash->fault)
  90. return 0;
  91. /* Clear faults. */
  92. rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
  93. if (IS_ERR_VALUE(rval))
  94. return rval;
  95. flash->led_mode->val = V4L2_FLASH_LED_MODE_NONE;
  96. rval = adp1653_update_hw(flash);
  97. if (IS_ERR_VALUE(rval))
  98. return rval;
  99. return flash->fault;
  100. }
  101. static int adp1653_strobe(struct adp1653_flash *flash, int enable)
  102. {
  103. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  104. u8 out_sel = ADP1653_INDICATOR_INTENSITY_uA_TO_REG(
  105. flash->indicator_intensity->val)
  106. << ADP1653_REG_OUT_SEL_ILED_SHIFT;
  107. int rval;
  108. if (flash->led_mode->val != V4L2_FLASH_LED_MODE_FLASH)
  109. return -EBUSY;
  110. if (!enable)
  111. return i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL,
  112. out_sel);
  113. out_sel |= ADP1653_FLASH_INTENSITY_mA_TO_REG(
  114. flash->flash_intensity->val)
  115. << ADP1653_REG_OUT_SEL_HPLED_SHIFT;
  116. rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, out_sel);
  117. if (rval)
  118. return rval;
  119. /* Software strobe using i2c */
  120. rval = i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE,
  121. ADP1653_REG_SW_STROBE_SW_STROBE);
  122. if (rval)
  123. return rval;
  124. return i2c_smbus_write_byte_data(client, ADP1653_REG_SW_STROBE, 0);
  125. }
  126. /* --------------------------------------------------------------------------
  127. * V4L2 controls
  128. */
  129. static int adp1653_get_ctrl(struct v4l2_ctrl *ctrl)
  130. {
  131. struct adp1653_flash *flash =
  132. container_of(ctrl->handler, struct adp1653_flash, ctrls);
  133. int rval;
  134. rval = adp1653_get_fault(flash);
  135. if (IS_ERR_VALUE(rval))
  136. return rval;
  137. ctrl->cur.val = 0;
  138. if (flash->fault & ADP1653_REG_FAULT_FLT_SCP)
  139. ctrl->cur.val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
  140. if (flash->fault & ADP1653_REG_FAULT_FLT_OT)
  141. ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
  142. if (flash->fault & ADP1653_REG_FAULT_FLT_TMR)
  143. ctrl->cur.val |= V4L2_FLASH_FAULT_TIMEOUT;
  144. if (flash->fault & ADP1653_REG_FAULT_FLT_OV)
  145. ctrl->cur.val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
  146. flash->fault = 0;
  147. return 0;
  148. }
  149. static int adp1653_set_ctrl(struct v4l2_ctrl *ctrl)
  150. {
  151. struct adp1653_flash *flash =
  152. container_of(ctrl->handler, struct adp1653_flash, ctrls);
  153. int rval;
  154. rval = adp1653_get_fault(flash);
  155. if (IS_ERR_VALUE(rval))
  156. return rval;
  157. if ((rval & (ADP1653_REG_FAULT_FLT_SCP |
  158. ADP1653_REG_FAULT_FLT_OT |
  159. ADP1653_REG_FAULT_FLT_OV)) &&
  160. (ctrl->id == V4L2_CID_FLASH_STROBE ||
  161. ctrl->id == V4L2_CID_FLASH_TORCH_INTENSITY ||
  162. ctrl->id == V4L2_CID_FLASH_LED_MODE))
  163. return -EBUSY;
  164. switch (ctrl->id) {
  165. case V4L2_CID_FLASH_STROBE:
  166. return adp1653_strobe(flash, 1);
  167. case V4L2_CID_FLASH_STROBE_STOP:
  168. return adp1653_strobe(flash, 0);
  169. }
  170. return adp1653_update_hw(flash);
  171. }
  172. static const struct v4l2_ctrl_ops adp1653_ctrl_ops = {
  173. .g_volatile_ctrl = adp1653_get_ctrl,
  174. .s_ctrl = adp1653_set_ctrl,
  175. };
  176. static int adp1653_init_controls(struct adp1653_flash *flash)
  177. {
  178. struct v4l2_ctrl *fault;
  179. v4l2_ctrl_handler_init(&flash->ctrls, 9);
  180. flash->led_mode =
  181. v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
  182. V4L2_CID_FLASH_LED_MODE,
  183. V4L2_FLASH_LED_MODE_TORCH, ~0x7, 0);
  184. v4l2_ctrl_new_std_menu(&flash->ctrls, &adp1653_ctrl_ops,
  185. V4L2_CID_FLASH_STROBE_SOURCE,
  186. V4L2_FLASH_STROBE_SOURCE_SOFTWARE, ~0x1, 0);
  187. v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
  188. V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
  189. v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
  190. V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
  191. flash->flash_timeout =
  192. v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
  193. V4L2_CID_FLASH_TIMEOUT, TIMEOUT_MIN,
  194. flash->platform_data->max_flash_timeout,
  195. TIMEOUT_STEP,
  196. flash->platform_data->max_flash_timeout);
  197. flash->flash_intensity =
  198. v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
  199. V4L2_CID_FLASH_INTENSITY,
  200. ADP1653_FLASH_INTENSITY_MIN,
  201. flash->platform_data->max_flash_intensity,
  202. 1, flash->platform_data->max_flash_intensity);
  203. flash->torch_intensity =
  204. v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
  205. V4L2_CID_FLASH_TORCH_INTENSITY,
  206. ADP1653_TORCH_INTENSITY_MIN,
  207. flash->platform_data->max_torch_intensity,
  208. ADP1653_FLASH_INTENSITY_STEP,
  209. flash->platform_data->max_torch_intensity);
  210. flash->indicator_intensity =
  211. v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
  212. V4L2_CID_FLASH_INDICATOR_INTENSITY,
  213. ADP1653_INDICATOR_INTENSITY_MIN,
  214. flash->platform_data->max_indicator_intensity,
  215. ADP1653_INDICATOR_INTENSITY_STEP,
  216. ADP1653_INDICATOR_INTENSITY_MIN);
  217. fault = v4l2_ctrl_new_std(&flash->ctrls, &adp1653_ctrl_ops,
  218. V4L2_CID_FLASH_FAULT, 0,
  219. V4L2_FLASH_FAULT_OVER_VOLTAGE
  220. | V4L2_FLASH_FAULT_OVER_TEMPERATURE
  221. | V4L2_FLASH_FAULT_SHORT_CIRCUIT, 0, 0);
  222. if (flash->ctrls.error)
  223. return flash->ctrls.error;
  224. fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
  225. flash->subdev.ctrl_handler = &flash->ctrls;
  226. return 0;
  227. }
  228. /* --------------------------------------------------------------------------
  229. * V4L2 subdev operations
  230. */
  231. static int
  232. adp1653_init_device(struct adp1653_flash *flash)
  233. {
  234. struct i2c_client *client = v4l2_get_subdevdata(&flash->subdev);
  235. int rval;
  236. /* Clear FAULT register by writing zero to OUT_SEL */
  237. rval = i2c_smbus_write_byte_data(client, ADP1653_REG_OUT_SEL, 0);
  238. if (rval < 0) {
  239. dev_err(&client->dev, "failed writing fault register\n");
  240. return -EIO;
  241. }
  242. mutex_lock(&flash->ctrls.lock);
  243. /* Reset faults before reading new ones. */
  244. flash->fault = 0;
  245. rval = adp1653_get_fault(flash);
  246. mutex_unlock(&flash->ctrls.lock);
  247. if (rval > 0) {
  248. dev_err(&client->dev, "faults detected: 0x%1.1x\n", rval);
  249. return -EIO;
  250. }
  251. mutex_lock(&flash->ctrls.lock);
  252. rval = adp1653_update_hw(flash);
  253. mutex_unlock(&flash->ctrls.lock);
  254. if (rval) {
  255. dev_err(&client->dev,
  256. "adp1653_update_hw failed at %s\n", __func__);
  257. return -EIO;
  258. }
  259. return 0;
  260. }
  261. static int
  262. __adp1653_set_power(struct adp1653_flash *flash, int on)
  263. {
  264. int ret;
  265. ret = flash->platform_data->power(&flash->subdev, on);
  266. if (ret < 0)
  267. return ret;
  268. if (!on)
  269. return 0;
  270. ret = adp1653_init_device(flash);
  271. if (ret < 0)
  272. flash->platform_data->power(&flash->subdev, 0);
  273. return ret;
  274. }
  275. static int
  276. adp1653_set_power(struct v4l2_subdev *subdev, int on)
  277. {
  278. struct adp1653_flash *flash = to_adp1653_flash(subdev);
  279. int ret = 0;
  280. mutex_lock(&flash->power_lock);
  281. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  282. * update the power state.
  283. */
  284. if (flash->power_count == !on) {
  285. ret = __adp1653_set_power(flash, !!on);
  286. if (ret < 0)
  287. goto done;
  288. }
  289. /* Update the power count. */
  290. flash->power_count += on ? 1 : -1;
  291. WARN_ON(flash->power_count < 0);
  292. done:
  293. mutex_unlock(&flash->power_lock);
  294. return ret;
  295. }
  296. static int adp1653_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  297. {
  298. return adp1653_set_power(sd, 1);
  299. }
  300. static int adp1653_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  301. {
  302. return adp1653_set_power(sd, 0);
  303. }
  304. static const struct v4l2_subdev_core_ops adp1653_core_ops = {
  305. .s_power = adp1653_set_power,
  306. };
  307. static const struct v4l2_subdev_ops adp1653_ops = {
  308. .core = &adp1653_core_ops,
  309. };
  310. static const struct v4l2_subdev_internal_ops adp1653_internal_ops = {
  311. .open = adp1653_open,
  312. .close = adp1653_close,
  313. };
  314. /* --------------------------------------------------------------------------
  315. * I2C driver
  316. */
  317. #ifdef CONFIG_PM
  318. static int adp1653_suspend(struct device *dev)
  319. {
  320. struct i2c_client *client = to_i2c_client(dev);
  321. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  322. struct adp1653_flash *flash = to_adp1653_flash(subdev);
  323. if (!flash->power_count)
  324. return 0;
  325. return __adp1653_set_power(flash, 0);
  326. }
  327. static int adp1653_resume(struct device *dev)
  328. {
  329. struct i2c_client *client = to_i2c_client(dev);
  330. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  331. struct adp1653_flash *flash = to_adp1653_flash(subdev);
  332. if (!flash->power_count)
  333. return 0;
  334. return __adp1653_set_power(flash, 1);
  335. }
  336. #else
  337. #define adp1653_suspend NULL
  338. #define adp1653_resume NULL
  339. #endif /* CONFIG_PM */
  340. static int adp1653_probe(struct i2c_client *client,
  341. const struct i2c_device_id *devid)
  342. {
  343. struct adp1653_flash *flash;
  344. int ret;
  345. /* we couldn't work without platform data */
  346. if (client->dev.platform_data == NULL)
  347. return -ENODEV;
  348. flash = kzalloc(sizeof(*flash), GFP_KERNEL);
  349. if (flash == NULL)
  350. return -ENOMEM;
  351. flash->platform_data = client->dev.platform_data;
  352. mutex_init(&flash->power_lock);
  353. v4l2_i2c_subdev_init(&flash->subdev, client, &adp1653_ops);
  354. flash->subdev.internal_ops = &adp1653_internal_ops;
  355. flash->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  356. ret = adp1653_init_controls(flash);
  357. if (ret)
  358. goto free_and_quit;
  359. ret = media_entity_init(&flash->subdev.entity, 0, NULL, 0);
  360. if (ret < 0)
  361. goto free_and_quit;
  362. flash->subdev.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
  363. return 0;
  364. free_and_quit:
  365. v4l2_ctrl_handler_free(&flash->ctrls);
  366. kfree(flash);
  367. return ret;
  368. }
  369. static int __exit adp1653_remove(struct i2c_client *client)
  370. {
  371. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  372. struct adp1653_flash *flash = to_adp1653_flash(subdev);
  373. v4l2_device_unregister_subdev(&flash->subdev);
  374. v4l2_ctrl_handler_free(&flash->ctrls);
  375. media_entity_cleanup(&flash->subdev.entity);
  376. kfree(flash);
  377. return 0;
  378. }
  379. static const struct i2c_device_id adp1653_id_table[] = {
  380. { ADP1653_NAME, 0 },
  381. { }
  382. };
  383. MODULE_DEVICE_TABLE(i2c, adp1653_id_table);
  384. static struct dev_pm_ops adp1653_pm_ops = {
  385. .suspend = adp1653_suspend,
  386. .resume = adp1653_resume,
  387. };
  388. static struct i2c_driver adp1653_i2c_driver = {
  389. .driver = {
  390. .name = ADP1653_NAME,
  391. .pm = &adp1653_pm_ops,
  392. },
  393. .probe = adp1653_probe,
  394. .remove = __exit_p(adp1653_remove),
  395. .id_table = adp1653_id_table,
  396. };
  397. module_i2c_driver(adp1653_i2c_driver);
  398. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
  399. MODULE_DESCRIPTION("Analog Devices ADP1653 LED flash driver");
  400. MODULE_LICENSE("GPL");