leds-lm3642.c 11 KB

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