lm3560.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * drivers/media/i2c/lm3560.c
  3. * General device driver for TI lm3560, FLASH LED Driver
  4. *
  5. * Copyright (C) 2013 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. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/slab.h>
  23. #include <linux/mutex.h>
  24. #include <linux/regmap.h>
  25. #include <linux/videodev2.h>
  26. #include <media/lm3560.h>
  27. #include <media/v4l2-ctrls.h>
  28. #include <media/v4l2-device.h>
  29. /* registers definitions */
  30. #define REG_ENABLE 0x10
  31. #define REG_TORCH_BR 0xa0
  32. #define REG_FLASH_BR 0xb0
  33. #define REG_FLASH_TOUT 0xc0
  34. #define REG_FLAG 0xd0
  35. #define REG_CONFIG1 0xe0
  36. /* fault mask */
  37. #define FAULT_TIMEOUT (1<<0)
  38. #define FAULT_OVERTEMP (1<<1)
  39. #define FAULT_SHORT_CIRCUIT (1<<2)
  40. enum led_enable {
  41. MODE_SHDN = 0x0,
  42. MODE_TORCH = 0x2,
  43. MODE_FLASH = 0x3,
  44. };
  45. /**
  46. * struct lm3560_flash
  47. *
  48. * @pdata: platform data
  49. * @regmap: reg. map for i2c
  50. * @lock: muxtex for serial access.
  51. * @led_mode: V4L2 LED mode
  52. * @ctrls_led: V4L2 contols
  53. * @subdev_led: V4L2 subdev
  54. */
  55. struct lm3560_flash {
  56. struct device *dev;
  57. struct lm3560_platform_data *pdata;
  58. struct regmap *regmap;
  59. struct mutex lock;
  60. enum v4l2_flash_led_mode led_mode;
  61. struct v4l2_ctrl_handler ctrls_led[LM3560_LED_MAX];
  62. struct v4l2_subdev subdev_led[LM3560_LED_MAX];
  63. };
  64. #define to_lm3560_flash(_ctrl, _no) \
  65. container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
  66. /* enable mode control */
  67. static int lm3560_mode_ctrl(struct lm3560_flash *flash)
  68. {
  69. int rval = -EINVAL;
  70. switch (flash->led_mode) {
  71. case V4L2_FLASH_LED_MODE_NONE:
  72. rval = regmap_update_bits(flash->regmap,
  73. REG_ENABLE, 0x03, MODE_SHDN);
  74. break;
  75. case V4L2_FLASH_LED_MODE_TORCH:
  76. rval = regmap_update_bits(flash->regmap,
  77. REG_ENABLE, 0x03, MODE_TORCH);
  78. break;
  79. case V4L2_FLASH_LED_MODE_FLASH:
  80. rval = regmap_update_bits(flash->regmap,
  81. REG_ENABLE, 0x03, MODE_FLASH);
  82. break;
  83. }
  84. return rval;
  85. }
  86. /* led1/2 enable/disable */
  87. static int lm3560_enable_ctrl(struct lm3560_flash *flash,
  88. enum lm3560_led_id led_no, bool on)
  89. {
  90. int rval;
  91. if (led_no == LM3560_LED0) {
  92. if (on)
  93. rval = regmap_update_bits(flash->regmap,
  94. REG_ENABLE, 0x08, 0x08);
  95. else
  96. rval = regmap_update_bits(flash->regmap,
  97. REG_ENABLE, 0x08, 0x00);
  98. } else {
  99. if (on)
  100. rval = regmap_update_bits(flash->regmap,
  101. REG_ENABLE, 0x10, 0x10);
  102. else
  103. rval = regmap_update_bits(flash->regmap,
  104. REG_ENABLE, 0x10, 0x00);
  105. }
  106. return rval;
  107. }
  108. /* torch1/2 brightness control */
  109. static int lm3560_torch_brt_ctrl(struct lm3560_flash *flash,
  110. enum lm3560_led_id led_no, unsigned int brt)
  111. {
  112. int rval;
  113. u8 br_bits;
  114. if (brt < LM3560_TORCH_BRT_MIN)
  115. return lm3560_enable_ctrl(flash, led_no, false);
  116. else
  117. rval = lm3560_enable_ctrl(flash, led_no, true);
  118. br_bits = LM3560_TORCH_BRT_uA_TO_REG(brt);
  119. if (led_no == LM3560_LED0)
  120. rval = regmap_update_bits(flash->regmap,
  121. REG_TORCH_BR, 0x07, br_bits);
  122. else
  123. rval = regmap_update_bits(flash->regmap,
  124. REG_TORCH_BR, 0x38, br_bits << 3);
  125. return rval;
  126. }
  127. /* flash1/2 brightness control */
  128. static int lm3560_flash_brt_ctrl(struct lm3560_flash *flash,
  129. enum lm3560_led_id led_no, unsigned int brt)
  130. {
  131. int rval;
  132. u8 br_bits;
  133. if (brt < LM3560_FLASH_BRT_MIN)
  134. return lm3560_enable_ctrl(flash, led_no, false);
  135. else
  136. rval = lm3560_enable_ctrl(flash, led_no, true);
  137. br_bits = LM3560_FLASH_BRT_uA_TO_REG(brt);
  138. if (led_no == LM3560_LED0)
  139. rval = regmap_update_bits(flash->regmap,
  140. REG_FLASH_BR, 0x0f, br_bits);
  141. else
  142. rval = regmap_update_bits(flash->regmap,
  143. REG_FLASH_BR, 0xf0, br_bits << 4);
  144. return rval;
  145. }
  146. /* v4l2 controls */
  147. static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
  148. {
  149. struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
  150. int rval = -EINVAL;
  151. mutex_lock(&flash->lock);
  152. if (ctrl->id == V4L2_CID_FLASH_FAULT) {
  153. s32 fault = 0;
  154. unsigned int reg_val;
  155. rval = regmap_read(flash->regmap, REG_FLAG, &reg_val);
  156. if (rval < 0)
  157. goto out;
  158. if (reg_val & FAULT_SHORT_CIRCUIT)
  159. fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
  160. if (reg_val & FAULT_OVERTEMP)
  161. fault |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
  162. if (reg_val & FAULT_TIMEOUT)
  163. fault |= V4L2_FLASH_FAULT_TIMEOUT;
  164. ctrl->cur.val = fault;
  165. }
  166. out:
  167. mutex_unlock(&flash->lock);
  168. return rval;
  169. }
  170. static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
  171. {
  172. struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
  173. u8 tout_bits;
  174. int rval = -EINVAL;
  175. mutex_lock(&flash->lock);
  176. switch (ctrl->id) {
  177. case V4L2_CID_FLASH_LED_MODE:
  178. flash->led_mode = ctrl->val;
  179. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  180. rval = lm3560_mode_ctrl(flash);
  181. break;
  182. case V4L2_CID_FLASH_STROBE_SOURCE:
  183. rval = regmap_update_bits(flash->regmap,
  184. REG_CONFIG1, 0x04, (ctrl->val) << 2);
  185. if (rval < 0)
  186. goto err_out;
  187. break;
  188. case V4L2_CID_FLASH_STROBE:
  189. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
  190. rval = -EBUSY;
  191. goto err_out;
  192. }
  193. flash->led_mode = V4L2_FLASH_LED_MODE_FLASH;
  194. rval = lm3560_mode_ctrl(flash);
  195. break;
  196. case V4L2_CID_FLASH_STROBE_STOP:
  197. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH) {
  198. rval = -EBUSY;
  199. goto err_out;
  200. }
  201. flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
  202. rval = lm3560_mode_ctrl(flash);
  203. break;
  204. case V4L2_CID_FLASH_TIMEOUT:
  205. tout_bits = LM3560_FLASH_TOUT_ms_TO_REG(ctrl->val);
  206. rval = regmap_update_bits(flash->regmap,
  207. REG_FLASH_TOUT, 0x1f, tout_bits);
  208. break;
  209. case V4L2_CID_FLASH_INTENSITY:
  210. rval = lm3560_flash_brt_ctrl(flash, led_no, ctrl->val);
  211. break;
  212. case V4L2_CID_FLASH_TORCH_INTENSITY:
  213. rval = lm3560_torch_brt_ctrl(flash, led_no, ctrl->val);
  214. break;
  215. }
  216. err_out:
  217. mutex_unlock(&flash->lock);
  218. return rval;
  219. }
  220. static int lm3560_led1_get_ctrl(struct v4l2_ctrl *ctrl)
  221. {
  222. return lm3560_get_ctrl(ctrl, LM3560_LED1);
  223. }
  224. static int lm3560_led1_set_ctrl(struct v4l2_ctrl *ctrl)
  225. {
  226. return lm3560_set_ctrl(ctrl, LM3560_LED1);
  227. }
  228. static int lm3560_led0_get_ctrl(struct v4l2_ctrl *ctrl)
  229. {
  230. return lm3560_get_ctrl(ctrl, LM3560_LED0);
  231. }
  232. static int lm3560_led0_set_ctrl(struct v4l2_ctrl *ctrl)
  233. {
  234. return lm3560_set_ctrl(ctrl, LM3560_LED0);
  235. }
  236. static const struct v4l2_ctrl_ops lm3560_led_ctrl_ops[LM3560_LED_MAX] = {
  237. [LM3560_LED0] = {
  238. .g_volatile_ctrl = lm3560_led0_get_ctrl,
  239. .s_ctrl = lm3560_led0_set_ctrl,
  240. },
  241. [LM3560_LED1] = {
  242. .g_volatile_ctrl = lm3560_led1_get_ctrl,
  243. .s_ctrl = lm3560_led1_set_ctrl,
  244. }
  245. };
  246. static int lm3560_init_controls(struct lm3560_flash *flash,
  247. enum lm3560_led_id led_no)
  248. {
  249. struct v4l2_ctrl *fault;
  250. u32 max_flash_brt = flash->pdata->max_flash_brt[led_no];
  251. u32 max_torch_brt = flash->pdata->max_torch_brt[led_no];
  252. struct v4l2_ctrl_handler *hdl = &flash->ctrls_led[led_no];
  253. const struct v4l2_ctrl_ops *ops = &lm3560_led_ctrl_ops[led_no];
  254. v4l2_ctrl_handler_init(hdl, 8);
  255. /* flash mode */
  256. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_LED_MODE,
  257. V4L2_FLASH_LED_MODE_TORCH, ~0x7,
  258. V4L2_FLASH_LED_MODE_NONE);
  259. flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
  260. /* flash source */
  261. v4l2_ctrl_new_std_menu(hdl, ops, V4L2_CID_FLASH_STROBE_SOURCE,
  262. 0x1, ~0x3, V4L2_FLASH_STROBE_SOURCE_SOFTWARE);
  263. /* flash strobe */
  264. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE, 0, 0, 0, 0);
  265. /* flash strobe stop */
  266. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_STROBE_STOP, 0, 0, 0, 0);
  267. /* flash strobe timeout */
  268. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TIMEOUT,
  269. LM3560_FLASH_TOUT_MIN,
  270. flash->pdata->max_flash_timeout,
  271. LM3560_FLASH_TOUT_STEP,
  272. flash->pdata->max_flash_timeout);
  273. /* flash brt */
  274. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_INTENSITY,
  275. LM3560_FLASH_BRT_MIN, max_flash_brt,
  276. LM3560_FLASH_BRT_STEP, max_flash_brt);
  277. /* torch brt */
  278. v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_TORCH_INTENSITY,
  279. LM3560_TORCH_BRT_MIN, max_torch_brt,
  280. LM3560_TORCH_BRT_STEP, max_torch_brt);
  281. /* fault */
  282. fault = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_FLASH_FAULT, 0,
  283. V4L2_FLASH_FAULT_OVER_VOLTAGE
  284. | V4L2_FLASH_FAULT_OVER_TEMPERATURE
  285. | V4L2_FLASH_FAULT_SHORT_CIRCUIT
  286. | V4L2_FLASH_FAULT_TIMEOUT, 0, 0);
  287. if (fault != NULL)
  288. fault->flags |= V4L2_CTRL_FLAG_VOLATILE;
  289. if (hdl->error)
  290. return hdl->error;
  291. flash->subdev_led[led_no].ctrl_handler = hdl;
  292. return 0;
  293. }
  294. /* initialize device */
  295. static const struct v4l2_subdev_ops lm3560_ops = {
  296. .core = NULL,
  297. };
  298. static const struct regmap_config lm3560_regmap = {
  299. .reg_bits = 8,
  300. .val_bits = 8,
  301. .max_register = 0xFF,
  302. };
  303. static int lm3560_subdev_init(struct lm3560_flash *flash,
  304. enum lm3560_led_id led_no, char *led_name)
  305. {
  306. struct i2c_client *client = to_i2c_client(flash->dev);
  307. int rval;
  308. v4l2_i2c_subdev_init(&flash->subdev_led[led_no], client, &lm3560_ops);
  309. flash->subdev_led[led_no].flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  310. strcpy(flash->subdev_led[led_no].name, led_name);
  311. rval = lm3560_init_controls(flash, led_no);
  312. if (rval)
  313. goto err_out;
  314. rval = media_entity_init(&flash->subdev_led[led_no].entity, 0, NULL, 0);
  315. if (rval < 0)
  316. goto err_out;
  317. flash->subdev_led[led_no].entity.type = MEDIA_ENT_T_V4L2_SUBDEV_FLASH;
  318. return rval;
  319. err_out:
  320. v4l2_ctrl_handler_free(&flash->ctrls_led[led_no]);
  321. return rval;
  322. }
  323. static int lm3560_init_device(struct lm3560_flash *flash)
  324. {
  325. int rval;
  326. unsigned int reg_val;
  327. /* set peak current */
  328. rval = regmap_update_bits(flash->regmap,
  329. REG_FLASH_TOUT, 0x60, flash->pdata->peak);
  330. if (rval < 0)
  331. return rval;
  332. /* output disable */
  333. flash->led_mode = V4L2_FLASH_LED_MODE_NONE;
  334. rval = lm3560_mode_ctrl(flash);
  335. if (rval < 0)
  336. return rval;
  337. /* reset faults */
  338. rval = regmap_read(flash->regmap, REG_FLAG, &reg_val);
  339. return rval;
  340. }
  341. static int lm3560_probe(struct i2c_client *client,
  342. const struct i2c_device_id *devid)
  343. {
  344. struct lm3560_flash *flash;
  345. struct lm3560_platform_data *pdata = dev_get_platdata(&client->dev);
  346. int rval;
  347. flash = devm_kzalloc(&client->dev, sizeof(*flash), GFP_KERNEL);
  348. if (flash == NULL)
  349. return -ENOMEM;
  350. flash->regmap = devm_regmap_init_i2c(client, &lm3560_regmap);
  351. if (IS_ERR(flash->regmap)) {
  352. rval = PTR_ERR(flash->regmap);
  353. return rval;
  354. }
  355. /* if there is no platform data, use chip default value */
  356. if (pdata == NULL) {
  357. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  358. if (pdata == NULL)
  359. return -ENODEV;
  360. pdata->peak = LM3560_PEAK_3600mA;
  361. pdata->max_flash_timeout = LM3560_FLASH_TOUT_MAX;
  362. /* led 1 */
  363. pdata->max_flash_brt[LM3560_LED0] = LM3560_FLASH_BRT_MAX;
  364. pdata->max_torch_brt[LM3560_LED0] = LM3560_TORCH_BRT_MAX;
  365. /* led 2 */
  366. pdata->max_flash_brt[LM3560_LED1] = LM3560_FLASH_BRT_MAX;
  367. pdata->max_torch_brt[LM3560_LED1] = LM3560_TORCH_BRT_MAX;
  368. }
  369. flash->pdata = pdata;
  370. flash->dev = &client->dev;
  371. mutex_init(&flash->lock);
  372. rval = lm3560_subdev_init(flash, LM3560_LED0, "lm3560-led0");
  373. if (rval < 0)
  374. return rval;
  375. rval = lm3560_subdev_init(flash, LM3560_LED1, "lm3560-led1");
  376. if (rval < 0)
  377. return rval;
  378. rval = lm3560_init_device(flash);
  379. if (rval < 0)
  380. return rval;
  381. i2c_set_clientdata(client, flash);
  382. return 0;
  383. }
  384. static int lm3560_remove(struct i2c_client *client)
  385. {
  386. struct lm3560_flash *flash = i2c_get_clientdata(client);
  387. unsigned int i;
  388. for (i = LM3560_LED0; i < LM3560_LED_MAX; i++) {
  389. v4l2_device_unregister_subdev(&flash->subdev_led[i]);
  390. v4l2_ctrl_handler_free(&flash->ctrls_led[i]);
  391. media_entity_cleanup(&flash->subdev_led[i].entity);
  392. }
  393. return 0;
  394. }
  395. static const struct i2c_device_id lm3560_id_table[] = {
  396. {LM3560_NAME, 0},
  397. {}
  398. };
  399. MODULE_DEVICE_TABLE(i2c, lm3560_id_table);
  400. static struct i2c_driver lm3560_i2c_driver = {
  401. .driver = {
  402. .name = LM3560_NAME,
  403. .pm = NULL,
  404. },
  405. .probe = lm3560_probe,
  406. .remove = lm3560_remove,
  407. .id_table = lm3560_id_table,
  408. };
  409. module_i2c_driver(lm3560_i2c_driver);
  410. MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
  411. MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
  412. MODULE_DESCRIPTION("Texas Instruments LM3560 LED flash driver");
  413. MODULE_LICENSE("GPL");