adp1653.c 13 KB

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