lm3646.c 11 KB

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