lm3646.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * drivers/media/i2c/lm3646.c
  3. * General device driver for TI lm3646, Dual FLASH LED Driver
  4. *
  5. * Copyright (C) 2014 Texas Instruments
  6. *
  7. * Contact: Daniel Jeong <gshark.jeong@gmail.com>
  8. * Ldd-Mlp <ldd-mlp@list.ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/regmap.h>
  19. #include <linux/videodev2.h>
  20. #include <media/lm3646.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-device.h>
  23. /* registers definitions */
  24. #define REG_ENABLE 0x01
  25. #define REG_TORCH_BR 0x05
  26. #define REG_FLASH_BR 0x05
  27. #define REG_FLASH_TOUT 0x04
  28. #define REG_FLAG 0x08
  29. #define REG_STROBE_SRC 0x06
  30. #define REG_LED1_FLASH_BR 0x06
  31. #define REG_LED1_TORCH_BR 0x07
  32. #define MASK_ENABLE 0x03
  33. #define MASK_TORCH_BR 0x70
  34. #define MASK_FLASH_BR 0x0F
  35. #define MASK_FLASH_TOUT 0x07
  36. #define MASK_FLAG 0xFF
  37. #define MASK_STROBE_SRC 0x80
  38. /* Fault Mask */
  39. #define FAULT_TIMEOUT (1<<0)
  40. #define FAULT_SHORT_CIRCUIT (1<<1)
  41. #define FAULT_UVLO (1<<2)
  42. #define FAULT_IVFM (1<<3)
  43. #define FAULT_OCP (1<<4)
  44. #define FAULT_OVERTEMP (1<<5)
  45. #define FAULT_NTC_TRIP (1<<6)
  46. #define FAULT_OVP (1<<7)
  47. enum led_mode {
  48. MODE_SHDN = 0x0,
  49. MODE_TORCH = 0x2,
  50. MODE_FLASH = 0x3,
  51. };
  52. /*
  53. * struct lm3646_flash
  54. *
  55. * @pdata: platform data
  56. * @regmap: reg. map for i2c
  57. * @lock: muxtex for serial access.
  58. * @led_mode: V4L2 LED mode
  59. * @ctrls_led: V4L2 contols
  60. * @subdev_led: V4L2 subdev
  61. * @mode_reg : mode register value
  62. */
  63. struct lm3646_flash {
  64. struct device *dev;
  65. struct lm3646_platform_data *pdata;
  66. struct regmap *regmap;
  67. struct v4l2_ctrl_handler ctrls_led;
  68. struct v4l2_subdev subdev_led;
  69. u8 mode_reg;
  70. };
  71. #define to_lm3646_flash(_ctrl) \
  72. container_of(_ctrl->handler, struct lm3646_flash, ctrls_led)
  73. /* enable mode control */
  74. static int lm3646_mode_ctrl(struct lm3646_flash *flash,
  75. enum v4l2_flash_led_mode led_mode)
  76. {
  77. switch (led_mode) {
  78. case V4L2_FLASH_LED_MODE_NONE:
  79. return regmap_write(flash->regmap,
  80. REG_ENABLE, flash->mode_reg | MODE_SHDN);
  81. case V4L2_FLASH_LED_MODE_TORCH:
  82. return regmap_write(flash->regmap,
  83. REG_ENABLE, flash->mode_reg | MODE_TORCH);
  84. case V4L2_FLASH_LED_MODE_FLASH:
  85. return regmap_write(flash->regmap,
  86. REG_ENABLE, flash->mode_reg | MODE_FLASH);
  87. }
  88. return -EINVAL;
  89. }
  90. /* V4L2 controls */
  91. static int lm3646_get_ctrl(struct v4l2_ctrl *ctrl)
  92. {
  93. struct lm3646_flash *flash = to_lm3646_flash(ctrl);
  94. unsigned int reg_val;
  95. int rval;
  96. if (ctrl->id != V4L2_CID_FLASH_FAULT)
  97. return -EINVAL;
  98. rval = regmap_read(flash->regmap, REG_FLAG, &reg_val);
  99. if (rval < 0)
  100. return rval;
  101. ctrl->val = 0;
  102. if (reg_val & FAULT_TIMEOUT)
  103. ctrl->val |= V4L2_FLASH_FAULT_TIMEOUT;
  104. if (reg_val & FAULT_SHORT_CIRCUIT)
  105. ctrl->val |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
  106. if (reg_val & FAULT_UVLO)
  107. ctrl->val |= V4L2_FLASH_FAULT_UNDER_VOLTAGE;
  108. if (reg_val & FAULT_IVFM)
  109. ctrl->val |= V4L2_FLASH_FAULT_INPUT_VOLTAGE;
  110. if (reg_val & FAULT_OCP)
  111. ctrl->val |= V4L2_FLASH_FAULT_OVER_CURRENT;
  112. if (reg_val & FAULT_OVERTEMP)
  113. ctrl->val |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
  114. if (reg_val & FAULT_NTC_TRIP)
  115. ctrl->val |= V4L2_FLASH_FAULT_LED_OVER_TEMPERATURE;
  116. if (reg_val & FAULT_OVP)
  117. ctrl->val |= V4L2_FLASH_FAULT_OVER_VOLTAGE;
  118. return 0;
  119. }
  120. static int lm3646_set_ctrl(struct v4l2_ctrl *ctrl)
  121. {
  122. struct lm3646_flash *flash = to_lm3646_flash(ctrl);
  123. unsigned int reg_val;
  124. int rval = -EINVAL;
  125. switch (ctrl->id) {
  126. case V4L2_CID_FLASH_LED_MODE:
  127. if (ctrl->val != V4L2_FLASH_LED_MODE_FLASH)
  128. return lm3646_mode_ctrl(flash, ctrl->val);
  129. /* switch to SHDN mode before flash strobe on */
  130. return lm3646_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
  131. case V4L2_CID_FLASH_STROBE_SOURCE:
  132. return regmap_update_bits(flash->regmap,
  133. REG_STROBE_SRC, MASK_STROBE_SRC,
  134. (ctrl->val) << 7);
  135. case V4L2_CID_FLASH_STROBE:
  136. /* read and check current mode of chip to start flash */
  137. rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
  138. if (rval < 0 || ((reg_val & MASK_ENABLE) != MODE_SHDN))
  139. return rval;
  140. /* flash on */
  141. return lm3646_mode_ctrl(flash, V4L2_FLASH_LED_MODE_FLASH);
  142. case V4L2_CID_FLASH_STROBE_STOP:
  143. /*
  144. * flash mode will be turned automatically
  145. * from FLASH mode to SHDN mode after flash duration timeout
  146. * read and check current mode of chip to stop flash
  147. */
  148. rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
  149. if (rval < 0)
  150. return rval;
  151. if ((reg_val & MASK_ENABLE) == MODE_FLASH)
  152. return lm3646_mode_ctrl(flash,
  153. V4L2_FLASH_LED_MODE_NONE);
  154. return rval;
  155. case V4L2_CID_FLASH_TIMEOUT:
  156. return regmap_update_bits(flash->regmap,
  157. REG_FLASH_TOUT, MASK_FLASH_TOUT,
  158. LM3646_FLASH_TOUT_ms_TO_REG
  159. (ctrl->val));
  160. case V4L2_CID_FLASH_INTENSITY:
  161. return regmap_update_bits(flash->regmap,
  162. REG_FLASH_BR, MASK_FLASH_BR,
  163. LM3646_TOTAL_FLASH_BRT_uA_TO_REG
  164. (ctrl->val));
  165. case V4L2_CID_FLASH_TORCH_INTENSITY:
  166. return regmap_update_bits(flash->regmap,
  167. REG_TORCH_BR, MASK_TORCH_BR,
  168. LM3646_TOTAL_TORCH_BRT_uA_TO_REG
  169. (ctrl->val) << 4);
  170. }
  171. return -EINVAL;
  172. }
  173. static const struct v4l2_ctrl_ops lm3646_led_ctrl_ops = {
  174. .g_volatile_ctrl = lm3646_get_ctrl,
  175. .s_ctrl = lm3646_set_ctrl,
  176. };
  177. static int lm3646_init_controls(struct lm3646_flash *flash)
  178. {
  179. struct v4l2_ctrl *fault;
  180. struct v4l2_ctrl_handler *hdl = &flash->ctrls_led;
  181. const struct v4l2_ctrl_ops *ops = &lm3646_led_ctrl_ops;
  182. v4l2_ctrl_handler_init(hdl, 8);
  183. /* flash mode */
  184. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
  185. V4L2_FLASH_LED_MODE_TORCH, ~0x7,
  186. V4L2_FLASH_LED_MODE_NONE);
  187. /* flash source */
  188. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
  189. 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
  190. /* flash strobe */
  191. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
  192. /* flash strobe stop */
  193. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
  194. /* flash strobe timeout */
  195. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
  196. LM3646_FLASH_TOUT_MIN,
  197. LM3646_FLASH_TOUT_MAX,
  198. LM3646_FLASH_TOUT_STEP, flash->pdata->flash_timeout);
  199. /* max flash current */
  200. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
  201. LM3646_TOTAL_FLASH_BRT_MIN,
  202. LM3646_TOTAL_FLASH_BRT_MAX,
  203. LM3646_TOTAL_FLASH_BRT_STEP,
  204. LM3646_TOTAL_FLASH_BRT_MAX);
  205. /* max torch current */
  206. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
  207. LM3646_TOTAL_TORCH_BRT_MIN,
  208. LM3646_TOTAL_TORCH_BRT_MAX,
  209. LM3646_TOTAL_TORCH_BRT_STEP,
  210. LM3646_TOTAL_TORCH_BRT_MAX);
  211. /* fault */
  212. fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
  213. V4L2_FLASH_FAULT_OVER_VOLTAGE
  214. | V4L2_FLASH_FAULT_OVER_TEMPERATURE
  215. | V4L2_FLASH_FAULT_SHORT_CIRCUIT
  216. | V4L2_FLASH_FAULT_TIMEOUT, 0, 0);
  217. if (fault != NULL)
  218. fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
  219. if (hdl->error)
  220. return hdl->error;
  221. flash->subdev_led.ctrl_handler = hdl;
  222. return 0;
  223. }
  224. /* initialize device */
  225. static const struct v4l2_subdev_ops lm3646_ops = {
  226. .core = NULL,
  227. };
  228. static const struct regmap_config lm3646_regmap = {
  229. .reg_bits = 8,
  230. .val_bits = 8,
  231. .max_register = 0xFF,
  232. };
  233. static int lm3646_subdev_init(struct lm3646_flash *flash)
  234. {
  235. struct i2c_client *client = to_i2c_client(flash->dev);
  236. int rval;
  237. v4l2_i2c_subdev_init(&flash->subdev_led, client, &lm3646_ops);
  238. flash->subdev_led.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  239. strcpy(flash->subdev_led.name, LM3646_NAME);
  240. rval = lm3646_init_controls(flash);
  241. if (rval)
  242. goto err_out;
  243. rval = media_entity_init(&flash->subdev_led.entity, 0, NULL, 0);
  244. if (rval < 0)
  245. goto err_out;
  246. flash->subdev_led.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
  247. return rval;
  248. err_out:
  249. v4l2_ctrl_handler_free(&flash->ctrls_led);
  250. return rval;
  251. }
  252. static int lm3646_init_device(struct lm3646_flash *flash)
  253. {
  254. unsigned int reg_val;
  255. int rval;
  256. /* read the value of mode register to reduce redundant i2c accesses */
  257. rval = regmap_read(flash->regmap, REG_ENABLE, &reg_val);
  258. if (rval < 0)
  259. return rval;
  260. flash->mode_reg = reg_val & 0xfc;
  261. /* output disable */
  262. rval = lm3646_mode_ctrl(flash, V4L2_FLASH_LED_MODE_NONE);
  263. if (rval < 0)
  264. return rval;
  265. /*
  266. * LED1 flash current setting
  267. * LED2 flash current = Total(Max) flash current - LED1 flash current
  268. */
  269. rval = regmap_update_bits(flash->regmap,
  270. REG_LED1_FLASH_BR, 0x7F,
  271. LM3646_LED1_FLASH_BRT_uA_TO_REG
  272. (flash->pdata->led1_flash_brt));
  273. if (rval < 0)
  274. return rval;
  275. /*
  276. * LED1 torch current setting
  277. * LED2 torch current = Total(Max) torch current - LED1 torch current
  278. */
  279. rval = regmap_update_bits(flash->regmap,
  280. REG_LED1_TORCH_BR, 0x7F,
  281. LM3646_LED1_TORCH_BRT_uA_TO_REG
  282. (flash->pdata->led1_torch_brt));
  283. if (rval < 0)
  284. return rval;
  285. /* Reset flag register */
  286. return regmap_read(flash->regmap, REG_FLAG, &reg_val);
  287. }
  288. static int lm3646_probe(struct i2c_client *client,
  289. const struct i2c_device_id *devid)
  290. {
  291. struct lm3646_flash *flash;
  292. struct lm3646_platform_data *pdata = dev_get_platdata(&client->dev);
  293. int rval;
  294. flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
  295. if (flash == NULL)
  296. return -ENOMEM;
  297. flash->regmap = devm_regmap_init_i2c(client, &lm3646_regmap);
  298. if (IS_ERR(flash->regmap))
  299. return PTR_ERR(flash->regmap);
  300. /* check device tree if there is no platform data */
  301. if (pdata == NULL) {
  302. pdata = devm_kzalloc(&client->dev,
  303. sizeof(struct lm3646_platform_data),
  304. GFP_KERNEL);
  305. if (pdata == NULL)
  306. return -ENOMEM;
  307. /* use default data in case of no platform data */
  308. pdata->flash_timeout = LM3646_FLASH_TOUT_MAX;
  309. pdata->led1_torch_brt = LM3646_LED1_TORCH_BRT_MAX;
  310. pdata->led1_flash_brt = LM3646_LED1_FLASH_BRT_MAX;
  311. }
  312. flash->pdata = pdata;
  313. flash->dev = &client->dev;
  314. rval = lm3646_subdev_init(flash);
  315. if (rval < 0)
  316. return rval;
  317. rval = lm3646_init_device(flash);
  318. if (rval < 0)
  319. return rval;
  320. i2c_set_clientdata(client, flash);
  321. return 0;
  322. }
  323. static int lm3646_remove(struct i2c_client *client)
  324. {
  325. struct lm3646_flash *flash = i2c_get_clientdata(client);
  326. v4l2_device_unregister_subdev(&flash->subdev_led);
  327. v4l2_ctrl_handler_free(&flash->ctrls_led);
  328. media_entity_cleanup(&flash->subdev_led.entity);
  329. return 0;
  330. }
  331. static const struct i2c_device_id lm3646_id_table[] = {
  332. {LM3646_NAME, 0},
  333. {}
  334. };
  335. MODULE_DEVICE_TABLE(i2c, lm3646_id_table);
  336. static struct i2c_driver lm3646_i2c_driver = {
  337. .driver = {
  338. .name = LM3646_NAME,
  339. },
  340. .probe = lm3646_probe,
  341. .remove = lm3646_remove,
  342. .id_table = lm3646_id_table,
  343. };
  344. module_i2c_driver(lm3646_i2c_driver);
  345. MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
  346. MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
  347. MODULE_DESCRIPTION("Texas Instruments LM3646 Dual Flash LED driver");
  348. MODULE_LICENSE("GPL");