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. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. *
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/module.h>
  27. #include <linux/i2c.h>
  28. #include <linux/slab.h>
  29. #include <linux/mutex.h>
  30. #include <linux/regmap.h>
  31. #include <linux/videodev2.h>
  32. #include <media/lm3560.h>
  33. #include <media/v4l2-ctrls.h>
  34. #include <media/v4l2-device.h>
  35. /* registers definitions */
  36. #define REG_ENABLE 0x10
  37. #define REG_TORCH_BR 0xa0
  38. #define REG_FLASH_BR 0xb0
  39. #define REG_FLASH_TOUT 0xc0
  40. #define REG_FLAG 0xd0
  41. #define REG_CONFIG1 0xe0
  42. /* Fault Mask */
  43. #define FAULT_TIMEOUT (1<<0)
  44. #define FAULT_OVERTEMP (1<<1)
  45. #define FAULT_SHORT_CIRCUIT (1<<2)
  46. enum led_enable {
  47. MODE_SHDN = 0x0,
  48. MODE_TORCH = 0x2,
  49. MODE_FLASH = 0x3,
  50. };
  51. /* struct lm3560_flash
  52. *
  53. * @pdata: platform data
  54. * @regmap: reg. map for i2c
  55. * @lock: muxtex for serial access.
  56. * @led_mode: V4L2 LED mode
  57. * @ctrls_led: V4L2 contols
  58. * @subdev_led: V4L2 subdev
  59. */
  60. struct lm3560_flash {
  61. struct device *dev;
  62. struct lm3560_platform_data *pdata;
  63. struct regmap *regmap;
  64. struct mutex lock;
  65. enum v4l2_flash_led_mode led_mode;
  66. struct v4l2_ctrl_handler ctrls_led[LM3560_LED_MAX];
  67. struct v4l2_subdev subdev_led[LM3560_LED_MAX];
  68. };
  69. #define to_lm3560_flash(_ctrl, _no) \
  70. container_of(_ctrl->handler, struct lm3560_flash, ctrls_led[_no])
  71. /* enable mode control */
  72. static int lm3560_mode_ctrl(struct lm3560_flash *flash)
  73. {
  74. int rval = -EINVAL;
  75. switch (flash->led_mode) {
  76. case V4L2_FLASH_LED_MODE_NONE:
  77. rval = regmap_update_bits(flash->regmap,
  78. REG_ENABLE, 0x03, MODE_SHDN);
  79. break;
  80. case V4L2_FLASH_LED_MODE_TORCH:
  81. rval = regmap_update_bits(flash->regmap,
  82. REG_ENABLE, 0x03, MODE_TORCH);
  83. break;
  84. case V4L2_FLASH_LED_MODE_FLASH:
  85. rval = regmap_update_bits(flash->regmap,
  86. REG_ENABLE, 0x03, MODE_FLASH);
  87. break;
  88. }
  89. return rval;
  90. }
  91. /* led1/2 enable/disable */
  92. static int lm3560_enable_ctrl(struct lm3560_flash *flash,
  93. enum lm3560_led_id led_no, bool on)
  94. {
  95. int rval;
  96. if (led_no == LM3560_LED0) {
  97. if (on == true)
  98. rval = regmap_update_bits(flash->regmap,
  99. REG_ENABLE, 0x08, 0x08);
  100. else
  101. rval = regmap_update_bits(flash->regmap,
  102. REG_ENABLE, 0x08, 0x00);
  103. } else {
  104. if (on == true)
  105. rval = regmap_update_bits(flash->regmap,
  106. REG_ENABLE, 0x10, 0x10);
  107. else
  108. rval = regmap_update_bits(flash->regmap,
  109. REG_ENABLE, 0x10, 0x00);
  110. }
  111. return rval;
  112. }
  113. /* torch1/2 brightness control */
  114. static int lm3560_torch_brt_ctrl(struct lm3560_flash *flash,
  115. enum lm3560_led_id led_no, unsigned int brt)
  116. {
  117. int rval;
  118. u8 br_bits;
  119. if (brt < LM3560_TORCH_BRT_MIN)
  120. return lm3560_enable_ctrl(flash, led_no, false);
  121. else
  122. rval = lm3560_enable_ctrl(flash, led_no, true);
  123. br_bits = LM3560_TORCH_BRT_uA_TO_REG(brt);
  124. if (led_no == LM3560_LED0)
  125. rval = regmap_update_bits(flash->regmap,
  126. REG_TORCH_BR, 0x07, br_bits);
  127. else
  128. rval = regmap_update_bits(flash->regmap,
  129. REG_TORCH_BR, 0x38, br_bits << 3);
  130. return rval;
  131. }
  132. /* flash1/2 brightness control */
  133. static int lm3560_flash_brt_ctrl(struct lm3560_flash *flash,
  134. enum lm3560_led_id led_no, unsigned int brt)
  135. {
  136. int rval;
  137. u8 br_bits;
  138. if (brt < LM3560_FLASH_BRT_MIN)
  139. return lm3560_enable_ctrl(flash, led_no, false);
  140. else
  141. rval = lm3560_enable_ctrl(flash, led_no, true);
  142. br_bits = LM3560_FLASH_BRT_uA_TO_REG(brt);
  143. if (led_no == LM3560_LED0)
  144. rval = regmap_update_bits(flash->regmap,
  145. REG_FLASH_BR, 0x0f, br_bits);
  146. else
  147. rval = regmap_update_bits(flash->regmap,
  148. REG_FLASH_BR, 0xf0, br_bits << 4);
  149. return rval;
  150. }
  151. /* V4L2 controls */
  152. static int lm3560_get_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
  153. {
  154. struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
  155. mutex_lock(&flash->lock);
  156. if (ctrl->id == V4L2_CID_FLASH_FAULT) {
  157. int rval;
  158. s32 fault = 0;
  159. unsigned int reg_val;
  160. rval = regmap_read(flash->regmap, REG_FLAG, &reg_val);
  161. if (rval < 0)
  162. return rval;
  163. if (rval & FAULT_SHORT_CIRCUIT)
  164. fault |= V4L2_FLASH_FAULT_SHORT_CIRCUIT;
  165. if (rval & FAULT_OVERTEMP)
  166. fault |= V4L2_FLASH_FAULT_OVER_TEMPERATURE;
  167. if (rval & FAULT_TIMEOUT)
  168. fault |= V4L2_FLASH_FAULT_TIMEOUT;
  169. ctrl->cur.val = fault;
  170. return 0;
  171. }
  172. mutex_unlock(&flash->lock);
  173. return -EINVAL;
  174. }
  175. static int lm3560_set_ctrl(struct v4l2_ctrl *ctrl, enum lm3560_led_id led_no)
  176. {
  177. struct lm3560_flash *flash = to_lm3560_flash(ctrl, led_no);
  178. u8 tout_bits;
  179. int rval = -EINVAL;
  180. mutex_lock(&flash->lock);
  181. switch (ctrl->id) {
  182. case V4L2_CID_FLASH_LED_MODE:
  183. flash->led_mode = ctrl->val;
  184. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  185. rval = lm3560_mode_ctrl(flash);
  186. break;
  187. case V4L2_CID_FLASH_STROBE_SOURCE:
  188. rval = regmap_update_bits(flash->regmap,
  189. REG_CONFIG1, 0x04, (ctrl->val) << 2);
  190. if (rval < 0)
  191. goto err_out;
  192. break;
  193. case V4L2_CID_FLASH_STROBE:
  194. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  195. return -EBUSY;
  196. flash->led_mode = V4L2_FLASH_LED_MODE_FLASH;
  197. rval = lm3560_mode_ctrl(flash);
  198. break;
  199. case V4L2_CID_FLASH_STROBE_STOP:
  200. if (flash->led_mode != V4L2_FLASH_LED_MODE_FLASH)
  201. return -EBUSY;
  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. mutex_unlock(&flash->lock);
  218. err_out:
  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_init(&flash->subdev_led[led_no].entity, 0, NULL, 0);
  316. if (rval < 0)
  317. goto err_out;
  318. flash->subdev_led[led_no].entity.type = MEDIA_ENT_T_V4L2_SUBDEV_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 =
  359. kzalloc(sizeof(struct lm3560_platform_data), GFP_KERNEL);
  360. if (pdata == NULL)
  361. return -ENODEV;
  362. pdata->peak = LM3560_PEAK_3600mA;
  363. pdata->max_flash_timeout = LM3560_FLASH_TOUT_MAX;
  364. /* led 1 */
  365. pdata->max_flash_brt[LM3560_LED0] = LM3560_FLASH_BRT_MAX;
  366. pdata->max_torch_brt[LM3560_LED0] = LM3560_TORCH_BRT_MAX;
  367. /* led 2 */
  368. pdata->max_flash_brt[LM3560_LED1] = LM3560_FLASH_BRT_MAX;
  369. pdata->max_torch_brt[LM3560_LED1] = LM3560_TORCH_BRT_MAX;
  370. }
  371. flash->pdata = pdata;
  372. flash->dev = &client->dev;
  373. mutex_init(&flash->lock);
  374. rval = lm3560_subdev_init(flash, LM3560_LED0, "lm3560-led0");
  375. if (rval < 0)
  376. return rval;
  377. rval = lm3560_subdev_init(flash, LM3560_LED1, "lm3560-led1");
  378. if (rval < 0)
  379. return rval;
  380. rval = lm3560_init_device(flash);
  381. if (rval < 0)
  382. return rval;
  383. return 0;
  384. }
  385. static int lm3560_remove(struct i2c_client *client)
  386. {
  387. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  388. struct lm3560_flash *flash = container_of(subdev, struct lm3560_flash,
  389. subdev_led[LM3560_LED_MAX]);
  390. unsigned int i;
  391. for (i = LM3560_LED0; i < LM3560_LED_MAX; i++) {
  392. v4l2_device_unregister_subdev(&flash->subdev_led[i]);
  393. v4l2_ctrl_handler_free(&flash->ctrls_led[i]);
  394. media_entity_cleanup(&flash->subdev_led[i].entity);
  395. }
  396. return 0;
  397. }
  398. static const struct i2c_device_id lm3560_id_table[] = {
  399. {LM3560_NAME, 0},
  400. {}
  401. };
  402. MODULE_DEVICE_TABLE(i2c, lm3560_id_table);
  403. static struct i2c_driver lm3560_i2c_driver = {
  404. .driver = {
  405. .name = LM3560_NAME,
  406. .pm = NULL,
  407. },
  408. .probe = lm3560_probe,
  409. .remove = lm3560_remove,
  410. .id_table = lm3560_id_table,
  411. };
  412. module_i2c_driver(lm3560_i2c_driver);
  413. MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
  414. MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
  415. MODULE_DESCRIPTION("Texas Instruments LM3560 LED flash driver");
  416. MODULE_LICENSE("GPL");