lm3560.c 12 KB

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