adp1653.c 14 KB

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