adp1653.c 14 KB

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