leds-lm3642.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Simple driver for Texas Instruments LM3642 LED Flash driver chip
  3. * Copyright (C) 2012 Texas Instruments
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/i2c.h>
  13. #include <linux/leds.h>
  14. #include <linux/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/fs.h>
  17. #include <linux/regmap.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/platform_data/leds-lm3642.h>
  20. #define REG_FILT_TIME (0x0)
  21. #define REG_IVFM_MODE (0x1)
  22. #define REG_TORCH_TIME (0x6)
  23. #define REG_FLASH (0x8)
  24. #define REG_I_CTRL (0x9)
  25. #define REG_ENABLE (0xA)
  26. #define REG_FLAG (0xB)
  27. #define REG_MAX (0xB)
  28. #define UVLO_EN_SHIFT (7)
  29. #define IVM_D_TH_SHIFT (2)
  30. #define TORCH_RAMP_UP_TIME_SHIFT (3)
  31. #define TORCH_RAMP_DN_TIME_SHIFT (0)
  32. #define INDUCTOR_I_LIMIT_SHIFT (6)
  33. #define FLASH_RAMP_TIME_SHIFT (3)
  34. #define FLASH_TOUT_TIME_SHIFT (0)
  35. #define TORCH_I_SHIFT (4)
  36. #define FLASH_I_SHIFT (0)
  37. #define IVFM_SHIFT (7)
  38. #define TX_PIN_EN_SHIFT (6)
  39. #define STROBE_PIN_EN_SHIFT (5)
  40. #define TORCH_PIN_EN_SHIFT (4)
  41. #define MODE_BITS_SHIFT (0)
  42. #define UVLO_EN_MASK (0x1)
  43. #define IVM_D_TH_MASK (0x7)
  44. #define TORCH_RAMP_UP_TIME_MASK (0x7)
  45. #define TORCH_RAMP_DN_TIME_MASK (0x7)
  46. #define INDUCTOR_I_LIMIT_MASK (0x1)
  47. #define FLASH_RAMP_TIME_MASK (0x7)
  48. #define FLASH_TOUT_TIME_MASK (0x7)
  49. #define TORCH_I_MASK (0x7)
  50. #define FLASH_I_MASK (0xF)
  51. #define IVFM_MASK (0x1)
  52. #define TX_PIN_EN_MASK (0x1)
  53. #define STROBE_PIN_EN_MASK (0x1)
  54. #define TORCH_PIN_EN_MASK (0x1)
  55. #define MODE_BITS_MASK (0x73)
  56. #define EX_PIN_CONTROL_MASK (0x71)
  57. #define EX_PIN_ENABLE_MASK (0x70)
  58. enum lm3642_mode {
  59. MODES_STASNDBY = 0,
  60. MODES_INDIC,
  61. MODES_TORCH,
  62. MODES_FLASH
  63. };
  64. struct lm3642_chip_data {
  65. struct device *dev;
  66. struct led_classdev cdev_flash;
  67. struct led_classdev cdev_torch;
  68. struct led_classdev cdev_indicator;
  69. struct work_struct work_flash;
  70. struct work_struct work_torch;
  71. struct work_struct work_indicator;
  72. u8 br_flash;
  73. u8 br_torch;
  74. u8 br_indicator;
  75. enum lm3642_torch_pin_enable torch_pin;
  76. enum lm3642_strobe_pin_enable strobe_pin;
  77. enum lm3642_tx_pin_enable tx_pin;
  78. struct lm3642_platform_data *pdata;
  79. struct regmap *regmap;
  80. struct mutex lock;
  81. unsigned int last_flag;
  82. };
  83. /* chip initialize */
  84. static int lm3642_chip_init(struct lm3642_chip_data *chip)
  85. {
  86. int ret;
  87. struct lm3642_platform_data *pdata = chip->pdata;
  88. /* set enable register */
  89. ret = regmap_update_bits(chip->regmap, REG_ENABLE, EX_PIN_ENABLE_MASK,
  90. pdata->tx_pin);
  91. if (ret < 0)
  92. dev_err(chip->dev, "Failed to update REG_ENABLE Register\n");
  93. return ret;
  94. }
  95. /* chip control */
  96. static int lm3642_control(struct lm3642_chip_data *chip,
  97. u8 brightness, enum lm3642_mode opmode)
  98. {
  99. int ret;
  100. ret = regmap_read(chip->regmap, REG_FLAG, &chip->last_flag);
  101. if (ret < 0) {
  102. dev_err(chip->dev, "Failed to read REG_FLAG Register\n");
  103. goto out;
  104. }
  105. if (chip->last_flag)
  106. dev_info(chip->dev, "Last FLAG is 0x%x\n", chip->last_flag);
  107. /* brightness 0 means off-state */
  108. if (!brightness)
  109. opmode = MODES_STASNDBY;
  110. switch (opmode) {
  111. case MODES_TORCH:
  112. ret = regmap_update_bits(chip->regmap, REG_I_CTRL,
  113. TORCH_I_MASK << TORCH_I_SHIFT,
  114. (brightness - 1) << TORCH_I_SHIFT);
  115. if (chip->torch_pin)
  116. opmode |= (TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT);
  117. break;
  118. case MODES_FLASH:
  119. ret = regmap_update_bits(chip->regmap, REG_I_CTRL,
  120. FLASH_I_MASK << FLASH_I_SHIFT,
  121. (brightness - 1) << FLASH_I_SHIFT);
  122. if (chip->strobe_pin)
  123. opmode |= (STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT);
  124. break;
  125. case MODES_INDIC:
  126. ret = regmap_update_bits(chip->regmap, REG_I_CTRL,
  127. TORCH_I_MASK << TORCH_I_SHIFT,
  128. (brightness - 1) << TORCH_I_SHIFT);
  129. break;
  130. case MODES_STASNDBY:
  131. break;
  132. default:
  133. return ret;
  134. }
  135. if (ret < 0) {
  136. dev_err(chip->dev, "Failed to write REG_I_CTRL Register\n");
  137. goto out;
  138. }
  139. if (chip->tx_pin)
  140. opmode |= (TX_PIN_EN_MASK << TX_PIN_EN_SHIFT);
  141. ret = regmap_update_bits(chip->regmap, REG_ENABLE,
  142. MODE_BITS_MASK << MODE_BITS_SHIFT,
  143. opmode << MODE_BITS_SHIFT);
  144. out:
  145. return ret;
  146. }
  147. /* torch */
  148. /* torch pin config for lm3642*/
  149. static ssize_t lm3642_torch_pin_store(struct device *dev,
  150. struct device_attribute *attr,
  151. const char *buf, size_t size)
  152. {
  153. ssize_t ret;
  154. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  155. struct lm3642_chip_data *chip =
  156. container_of(led_cdev, struct lm3642_chip_data, cdev_indicator);
  157. unsigned int state;
  158. ret = kstrtouint(buf, 10, &state);
  159. if (ret)
  160. goto out_strtoint;
  161. if (state != 0)
  162. state = 0x01 << TORCH_PIN_EN_SHIFT;
  163. chip->torch_pin = state;
  164. ret = regmap_update_bits(chip->regmap, REG_ENABLE,
  165. TORCH_PIN_EN_MASK << TORCH_PIN_EN_SHIFT,
  166. state);
  167. if (ret < 0)
  168. goto out;
  169. return size;
  170. out:
  171. dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
  172. return ret;
  173. out_strtoint:
  174. dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
  175. return ret;
  176. }
  177. static DEVICE_ATTR(torch_pin, S_IWUSR, NULL, lm3642_torch_pin_store);
  178. static void lm3642_deferred_torch_brightness_set(struct work_struct *work)
  179. {
  180. struct lm3642_chip_data *chip =
  181. container_of(work, struct lm3642_chip_data, work_torch);
  182. mutex_lock(&chip->lock);
  183. lm3642_control(chip, chip->br_torch, MODES_TORCH);
  184. mutex_unlock(&chip->lock);
  185. }
  186. static void lm3642_torch_brightness_set(struct led_classdev *cdev,
  187. enum led_brightness brightness)
  188. {
  189. struct lm3642_chip_data *chip =
  190. container_of(cdev, struct lm3642_chip_data, cdev_torch);
  191. chip->br_torch = brightness;
  192. schedule_work(&chip->work_torch);
  193. }
  194. /* flash */
  195. /* strobe pin config for lm3642*/
  196. static ssize_t lm3642_strobe_pin_store(struct device *dev,
  197. struct device_attribute *attr,
  198. const char *buf, size_t size)
  199. {
  200. ssize_t ret;
  201. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  202. struct lm3642_chip_data *chip =
  203. container_of(led_cdev, struct lm3642_chip_data, cdev_indicator);
  204. unsigned int state;
  205. ret = kstrtouint(buf, 10, &state);
  206. if (ret)
  207. goto out_strtoint;
  208. if (state != 0)
  209. state = 0x01 << STROBE_PIN_EN_SHIFT;
  210. chip->strobe_pin = state;
  211. ret = regmap_update_bits(chip->regmap, REG_ENABLE,
  212. STROBE_PIN_EN_MASK << STROBE_PIN_EN_SHIFT,
  213. state);
  214. if (ret < 0)
  215. goto out;
  216. return size;
  217. out:
  218. dev_err(chip->dev, "%s:i2c access fail to register\n", __func__);
  219. return ret;
  220. out_strtoint:
  221. dev_err(chip->dev, "%s: fail to change str to int\n", __func__);
  222. return ret;
  223. }
  224. static DEVICE_ATTR(strobe_pin, S_IWUSR, NULL, lm3642_strobe_pin_store);
  225. static void lm3642_deferred_strobe_brightness_set(struct work_struct *work)
  226. {
  227. struct lm3642_chip_data *chip =
  228. container_of(work, struct lm3642_chip_data, work_flash);
  229. mutex_lock(&chip->lock);
  230. lm3642_control(chip, chip->br_flash, MODES_FLASH);
  231. mutex_unlock(&chip->lock);
  232. }
  233. static void lm3642_strobe_brightness_set(struct led_classdev *cdev,
  234. enum led_brightness brightness)
  235. {
  236. struct lm3642_chip_data *chip =
  237. container_of(cdev, struct lm3642_chip_data, cdev_flash);
  238. chip->br_flash = brightness;
  239. schedule_work(&chip->work_flash);
  240. }
  241. /* indicator */
  242. static void lm3642_deferred_indicator_brightness_set(struct work_struct *work)
  243. {
  244. struct lm3642_chip_data *chip =
  245. container_of(work, struct lm3642_chip_data, work_indicator);
  246. mutex_lock(&chip->lock);
  247. lm3642_control(chip, chip->br_indicator, MODES_INDIC);
  248. mutex_unlock(&chip->lock);
  249. }
  250. static void lm3642_indicator_brightness_set(struct led_classdev *cdev,
  251. enum led_brightness brightness)
  252. {
  253. struct lm3642_chip_data *chip =
  254. container_of(cdev, struct lm3642_chip_data, cdev_indicator);
  255. chip->br_indicator = brightness;
  256. schedule_work(&chip->work_indicator);
  257. }
  258. static const struct regmap_config lm3642_regmap = {
  259. .reg_bits = 8,
  260. .val_bits = 8,
  261. .max_register = REG_MAX,
  262. };
  263. static struct attribute *lm3642_flash_attrs[] = {
  264. &dev_attr_strobe_pin.attr,
  265. NULL
  266. };
  267. ATTRIBUTE_GROUPS(lm3642_flash);
  268. static struct attribute *lm3642_torch_attrs[] = {
  269. &dev_attr_torch_pin.attr,
  270. NULL
  271. };
  272. ATTRIBUTE_GROUPS(lm3642_torch);
  273. static int lm3642_probe(struct i2c_client *client,
  274. const struct i2c_device_id *id)
  275. {
  276. struct lm3642_platform_data *pdata = dev_get_platdata(&client->dev);
  277. struct lm3642_chip_data *chip;
  278. int err;
  279. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  280. dev_err(&client->dev, "i2c functionality check fail.\n");
  281. return -EOPNOTSUPP;
  282. }
  283. if (pdata == NULL) {
  284. dev_err(&client->dev, "needs Platform Data.\n");
  285. return -ENODATA;
  286. }
  287. chip = devm_kzalloc(&client->dev,
  288. sizeof(struct lm3642_chip_data), GFP_KERNEL);
  289. if (!chip)
  290. return -ENOMEM;
  291. chip->dev = &client->dev;
  292. chip->pdata = pdata;
  293. chip->tx_pin = pdata->tx_pin;
  294. chip->torch_pin = pdata->torch_pin;
  295. chip->strobe_pin = pdata->strobe_pin;
  296. chip->regmap = devm_regmap_init_i2c(client, &lm3642_regmap);
  297. if (IS_ERR(chip->regmap)) {
  298. err = PTR_ERR(chip->regmap);
  299. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  300. err);
  301. return err;
  302. }
  303. mutex_init(&chip->lock);
  304. i2c_set_clientdata(client, chip);
  305. err = lm3642_chip_init(chip);
  306. if (err < 0)
  307. goto err_out;
  308. /* flash */
  309. INIT_WORK(&chip->work_flash, lm3642_deferred_strobe_brightness_set);
  310. chip->cdev_flash.name = "flash";
  311. chip->cdev_flash.max_brightness = 16;
  312. chip->cdev_flash.brightness_set = lm3642_strobe_brightness_set;
  313. chip->cdev_flash.default_trigger = "flash";
  314. chip->cdev_flash.groups = lm3642_flash_groups,
  315. err = led_classdev_register((struct device *)
  316. &client->dev, &chip->cdev_flash);
  317. if (err < 0) {
  318. dev_err(chip->dev, "failed to register flash\n");
  319. goto err_out;
  320. }
  321. /* torch */
  322. INIT_WORK(&chip->work_torch, lm3642_deferred_torch_brightness_set);
  323. chip->cdev_torch.name = "torch";
  324. chip->cdev_torch.max_brightness = 8;
  325. chip->cdev_torch.brightness_set = lm3642_torch_brightness_set;
  326. chip->cdev_torch.default_trigger = "torch";
  327. chip->cdev_torch.groups = lm3642_torch_groups,
  328. err = led_classdev_register((struct device *)
  329. &client->dev, &chip->cdev_torch);
  330. if (err < 0) {
  331. dev_err(chip->dev, "failed to register torch\n");
  332. goto err_create_torch_file;
  333. }
  334. /* indicator */
  335. INIT_WORK(&chip->work_indicator,
  336. lm3642_deferred_indicator_brightness_set);
  337. chip->cdev_indicator.name = "indicator";
  338. chip->cdev_indicator.max_brightness = 8;
  339. chip->cdev_indicator.brightness_set = lm3642_indicator_brightness_set;
  340. err = led_classdev_register((struct device *)
  341. &client->dev, &chip->cdev_indicator);
  342. if (err < 0) {
  343. dev_err(chip->dev, "failed to register indicator\n");
  344. goto err_create_indicator_file;
  345. }
  346. dev_info(&client->dev, "LM3642 is initialized\n");
  347. return 0;
  348. err_create_indicator_file:
  349. led_classdev_unregister(&chip->cdev_torch);
  350. err_create_torch_file:
  351. led_classdev_unregister(&chip->cdev_flash);
  352. err_out:
  353. return err;
  354. }
  355. static int lm3642_remove(struct i2c_client *client)
  356. {
  357. struct lm3642_chip_data *chip = i2c_get_clientdata(client);
  358. led_classdev_unregister(&chip->cdev_indicator);
  359. flush_work(&chip->work_indicator);
  360. led_classdev_unregister(&chip->cdev_torch);
  361. flush_work(&chip->work_torch);
  362. led_classdev_unregister(&chip->cdev_flash);
  363. flush_work(&chip->work_flash);
  364. regmap_write(chip->regmap, REG_ENABLE, 0);
  365. return 0;
  366. }
  367. static const struct i2c_device_id lm3642_id[] = {
  368. {LM3642_NAME, 0},
  369. {}
  370. };
  371. MODULE_DEVICE_TABLE(i2c, lm3642_id);
  372. static struct i2c_driver lm3642_i2c_driver = {
  373. .driver = {
  374. .name = LM3642_NAME,
  375. .pm = NULL,
  376. },
  377. .probe = lm3642_probe,
  378. .remove = lm3642_remove,
  379. .id_table = lm3642_id,
  380. };
  381. module_i2c_driver(lm3642_i2c_driver);
  382. MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3642");
  383. MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
  384. MODULE_AUTHOR("G.Shark Jeong <gshark.jeong@gmail.com>");
  385. MODULE_LICENSE("GPL v2");