adp1653.c 12 KB

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