lm3639_bl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Simple driver for Texas Instruments LM3639 Backlight + Flash LED 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/slab.h>
  12. #include <linux/i2c.h>
  13. #include <linux/leds.h>
  14. #include <linux/backlight.h>
  15. #include <linux/err.h>
  16. #include <linux/delay.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/regmap.h>
  20. #include <linux/platform_data/lm3639_bl.h>
  21. #define REG_DEV_ID 0x00
  22. #define REG_CHECKSUM 0x01
  23. #define REG_BL_CONF_1 0x02
  24. #define REG_BL_CONF_2 0x03
  25. #define REG_BL_CONF_3 0x04
  26. #define REG_BL_CONF_4 0x05
  27. #define REG_FL_CONF_1 0x06
  28. #define REG_FL_CONF_2 0x07
  29. #define REG_FL_CONF_3 0x08
  30. #define REG_IO_CTRL 0x09
  31. #define REG_ENABLE 0x0A
  32. #define REG_FLAG 0x0B
  33. #define REG_MAX REG_FLAG
  34. struct lm3639_chip_data {
  35. struct device *dev;
  36. struct lm3639_platform_data *pdata;
  37. struct backlight_device *bled;
  38. struct led_classdev cdev_flash;
  39. struct led_classdev cdev_torch;
  40. struct regmap *regmap;
  41. unsigned int bled_mode;
  42. unsigned int bled_map;
  43. unsigned int last_flag;
  44. };
  45. /* initialize chip */
  46. static int lm3639_chip_init(struct lm3639_chip_data *pchip)
  47. {
  48. int ret;
  49. unsigned int reg_val;
  50. struct lm3639_platform_data *pdata = pchip->pdata;
  51. /* input pins config. */
  52. ret =
  53. regmap_update_bits(pchip->regmap, REG_BL_CONF_1, 0x08,
  54. pdata->pin_pwm);
  55. if (ret < 0)
  56. goto out;
  57. reg_val = (pdata->pin_pwm & 0x40) | pdata->pin_strobe | pdata->pin_tx;
  58. ret = regmap_update_bits(pchip->regmap, REG_IO_CTRL, 0x7C, reg_val);
  59. if (ret < 0)
  60. goto out;
  61. /* init brightness */
  62. ret = regmap_write(pchip->regmap, REG_BL_CONF_4, pdata->init_brt_led);
  63. if (ret < 0)
  64. goto out;
  65. ret = regmap_write(pchip->regmap, REG_BL_CONF_3, pdata->init_brt_led);
  66. if (ret < 0)
  67. goto out;
  68. /* output pins config. */
  69. if (!pdata->init_brt_led) {
  70. reg_val = pdata->fled_pins;
  71. reg_val |= pdata->bled_pins;
  72. } else {
  73. reg_val = pdata->fled_pins;
  74. reg_val |= pdata->bled_pins | 0x01;
  75. }
  76. ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x79, reg_val);
  77. if (ret < 0)
  78. goto out;
  79. return ret;
  80. out:
  81. dev_err(pchip->dev, "i2c failed to access register\n");
  82. return ret;
  83. }
  84. /* update and get brightness */
  85. static int lm3639_bled_update_status(struct backlight_device *bl)
  86. {
  87. int ret;
  88. unsigned int reg_val;
  89. struct lm3639_chip_data *pchip = bl_get_data(bl);
  90. struct lm3639_platform_data *pdata = pchip->pdata;
  91. ret = regmap_read(pchip->regmap, REG_FLAG, &reg_val);
  92. if (ret < 0)
  93. goto out;
  94. if (reg_val != 0)
  95. dev_info(pchip->dev, "last flag is 0x%x\n", reg_val);
  96. /* pwm control */
  97. if (pdata->pin_pwm) {
  98. if (pdata->pwm_set_intensity)
  99. pdata->pwm_set_intensity(bl->props.brightness,
  100. pdata->max_brt_led);
  101. else
  102. dev_err(pchip->dev,
  103. "No pwm control func. in plat-data\n");
  104. return bl->props.brightness;
  105. }
  106. /* i2c control and set brigtness */
  107. ret = regmap_write(pchip->regmap, REG_BL_CONF_4, bl->props.brightness);
  108. if (ret < 0)
  109. goto out;
  110. ret = regmap_write(pchip->regmap, REG_BL_CONF_3, bl->props.brightness);
  111. if (ret < 0)
  112. goto out;
  113. if (!bl->props.brightness)
  114. ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x01, 0x00);
  115. else
  116. ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x01, 0x01);
  117. if (ret < 0)
  118. goto out;
  119. return bl->props.brightness;
  120. out:
  121. dev_err(pchip->dev, "i2c failed to access registers\n");
  122. return bl->props.brightness;
  123. }
  124. static int lm3639_bled_get_brightness(struct backlight_device *bl)
  125. {
  126. int ret;
  127. unsigned int reg_val;
  128. struct lm3639_chip_data *pchip = bl_get_data(bl);
  129. struct lm3639_platform_data *pdata = pchip->pdata;
  130. if (pdata->pin_pwm) {
  131. if (pdata->pwm_get_intensity)
  132. bl->props.brightness = pdata->pwm_get_intensity();
  133. else
  134. dev_err(pchip->dev,
  135. "No pwm control func. in plat-data\n");
  136. return bl->props.brightness;
  137. }
  138. ret = regmap_read(pchip->regmap, REG_BL_CONF_1, &reg_val);
  139. if (ret < 0)
  140. goto out;
  141. if (reg_val & 0x10)
  142. ret = regmap_read(pchip->regmap, REG_BL_CONF_4, &reg_val);
  143. else
  144. ret = regmap_read(pchip->regmap, REG_BL_CONF_3, &reg_val);
  145. if (ret < 0)
  146. goto out;
  147. bl->props.brightness = reg_val;
  148. return bl->props.brightness;
  149. out:
  150. dev_err(pchip->dev, "i2c failed to access register\n");
  151. return bl->props.brightness;
  152. }
  153. static const struct backlight_ops lm3639_bled_ops = {
  154. .options = BL_CORE_SUSPENDRESUME,
  155. .update_status = lm3639_bled_update_status,
  156. .get_brightness = lm3639_bled_get_brightness,
  157. };
  158. /* backlight mapping mode */
  159. static ssize_t lm3639_bled_mode_store(struct device *dev,
  160. struct device_attribute *devAttr,
  161. const char *buf, size_t size)
  162. {
  163. ssize_t ret;
  164. struct lm3639_chip_data *pchip = dev_get_drvdata(dev);
  165. unsigned int state;
  166. ret = kstrtouint(buf, 10, &state);
  167. if (ret)
  168. goto out_input;
  169. if (!state)
  170. ret =
  171. regmap_update_bits(pchip->regmap, REG_BL_CONF_1, 0x10,
  172. 0x00);
  173. else
  174. ret =
  175. regmap_update_bits(pchip->regmap, REG_BL_CONF_1, 0x10,
  176. 0x10);
  177. if (ret < 0)
  178. goto out;
  179. return size;
  180. out:
  181. dev_err(pchip->dev, "%s:i2c access fail to register\n", __func__);
  182. return ret;
  183. out_input:
  184. dev_err(pchip->dev, "%s:input conversion fail\n", __func__);
  185. return ret;
  186. }
  187. static DEVICE_ATTR(bled_mode, S_IWUSR, NULL, lm3639_bled_mode_store);
  188. /* torch */
  189. static void lm3639_torch_brightness_set(struct led_classdev *cdev,
  190. enum led_brightness brightness)
  191. {
  192. int ret;
  193. unsigned int reg_val;
  194. struct lm3639_chip_data *pchip;
  195. pchip = container_of(cdev, struct lm3639_chip_data, cdev_torch);
  196. ret = regmap_read(pchip->regmap, REG_FLAG, &reg_val);
  197. if (ret < 0)
  198. goto out;
  199. if (reg_val != 0)
  200. dev_info(pchip->dev, "last flag is 0x%x\n", reg_val);
  201. /* brightness 0 means off state */
  202. if (!brightness) {
  203. ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x06, 0x00);
  204. if (ret < 0)
  205. goto out;
  206. return;
  207. }
  208. ret = regmap_update_bits(pchip->regmap,
  209. REG_FL_CONF_1, 0x70, (brightness - 1) << 4);
  210. if (ret < 0)
  211. goto out;
  212. ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x06, 0x02);
  213. if (ret < 0)
  214. goto out;
  215. return;
  216. out:
  217. dev_err(pchip->dev, "i2c failed to access register\n");
  218. }
  219. /* flash */
  220. static void lm3639_flash_brightness_set(struct led_classdev *cdev,
  221. enum led_brightness brightness)
  222. {
  223. int ret;
  224. unsigned int reg_val;
  225. struct lm3639_chip_data *pchip;
  226. pchip = container_of(cdev, struct lm3639_chip_data, cdev_flash);
  227. ret = regmap_read(pchip->regmap, REG_FLAG, &reg_val);
  228. if (ret < 0)
  229. goto out;
  230. if (reg_val != 0)
  231. dev_info(pchip->dev, "last flag is 0x%x\n", reg_val);
  232. /* torch off before flash control */
  233. ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x06, 0x00);
  234. if (ret < 0)
  235. goto out;
  236. /* brightness 0 means off state */
  237. if (!brightness)
  238. return;
  239. ret = regmap_update_bits(pchip->regmap,
  240. REG_FL_CONF_1, 0x0F, brightness - 1);
  241. if (ret < 0)
  242. goto out;
  243. ret = regmap_update_bits(pchip->regmap, REG_ENABLE, 0x06, 0x06);
  244. if (ret < 0)
  245. goto out;
  246. return;
  247. out:
  248. dev_err(pchip->dev, "i2c failed to access register\n");
  249. }
  250. static const struct regmap_config lm3639_regmap = {
  251. .reg_bits = 8,
  252. .val_bits = 8,
  253. .max_register = REG_MAX,
  254. };
  255. static int lm3639_probe(struct i2c_client *client,
  256. const struct i2c_device_id *id)
  257. {
  258. int ret;
  259. struct lm3639_chip_data *pchip;
  260. struct lm3639_platform_data *pdata = dev_get_platdata(&client->dev);
  261. struct backlight_properties props;
  262. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  263. dev_err(&client->dev, "i2c functionality check fail.\n");
  264. return -EOPNOTSUPP;
  265. }
  266. if (pdata == NULL) {
  267. dev_err(&client->dev, "Needs Platform Data.\n");
  268. return -ENODATA;
  269. }
  270. pchip = devm_kzalloc(&client->dev,
  271. sizeof(struct lm3639_chip_data), GFP_KERNEL);
  272. if (!pchip)
  273. return -ENOMEM;
  274. pchip->pdata = pdata;
  275. pchip->dev = &client->dev;
  276. pchip->regmap = devm_regmap_init_i2c(client, &lm3639_regmap);
  277. if (IS_ERR(pchip->regmap)) {
  278. ret = PTR_ERR(pchip->regmap);
  279. dev_err(&client->dev, "fail : allocate register map: %d\n",
  280. ret);
  281. return ret;
  282. }
  283. i2c_set_clientdata(client, pchip);
  284. /* chip initialize */
  285. ret = lm3639_chip_init(pchip);
  286. if (ret < 0) {
  287. dev_err(&client->dev, "fail : chip init\n");
  288. goto err_out;
  289. }
  290. /* backlight */
  291. props.type = BACKLIGHT_RAW;
  292. props.brightness = pdata->init_brt_led;
  293. props.max_brightness = pdata->max_brt_led;
  294. pchip->bled =
  295. devm_backlight_device_register(pchip->dev, "lm3639_bled",
  296. pchip->dev, pchip, &lm3639_bled_ops,
  297. &props);
  298. if (IS_ERR(pchip->bled)) {
  299. dev_err(&client->dev, "fail : backlight register\n");
  300. ret = PTR_ERR(pchip->bled);
  301. goto err_out;
  302. }
  303. ret = device_create_file(&(pchip->bled->dev), &dev_attr_bled_mode);
  304. if (ret < 0) {
  305. dev_err(&client->dev, "failed : add sysfs entries\n");
  306. goto err_out;
  307. }
  308. /* flash */
  309. pchip->cdev_flash.name = "lm3639_flash";
  310. pchip->cdev_flash.max_brightness = 16;
  311. pchip->cdev_flash.brightness_set = lm3639_flash_brightness_set;
  312. ret = led_classdev_register((struct device *)
  313. &client->dev, &pchip->cdev_flash);
  314. if (ret < 0) {
  315. dev_err(&client->dev, "fail : flash register\n");
  316. goto err_flash;
  317. }
  318. /* torch */
  319. pchip->cdev_torch.name = "lm3639_torch";
  320. pchip->cdev_torch.max_brightness = 8;
  321. pchip->cdev_torch.brightness_set = lm3639_torch_brightness_set;
  322. ret = led_classdev_register((struct device *)
  323. &client->dev, &pchip->cdev_torch);
  324. if (ret < 0) {
  325. dev_err(&client->dev, "fail : torch register\n");
  326. goto err_torch;
  327. }
  328. return 0;
  329. err_torch:
  330. led_classdev_unregister(&pchip->cdev_flash);
  331. err_flash:
  332. device_remove_file(&(pchip->bled->dev), &dev_attr_bled_mode);
  333. err_out:
  334. return ret;
  335. }
  336. static int lm3639_remove(struct i2c_client *client)
  337. {
  338. struct lm3639_chip_data *pchip = i2c_get_clientdata(client);
  339. regmap_write(pchip->regmap, REG_ENABLE, 0x00);
  340. if (&pchip->cdev_torch)
  341. led_classdev_unregister(&pchip->cdev_torch);
  342. if (&pchip->cdev_flash)
  343. led_classdev_unregister(&pchip->cdev_flash);
  344. if (pchip->bled)
  345. device_remove_file(&(pchip->bled->dev), &dev_attr_bled_mode);
  346. return 0;
  347. }
  348. static const struct i2c_device_id lm3639_id[] = {
  349. {LM3639_NAME, 0},
  350. {}
  351. };
  352. MODULE_DEVICE_TABLE(i2c, lm3639_id);
  353. static struct i2c_driver lm3639_i2c_driver = {
  354. .driver = {
  355. .name = LM3639_NAME,
  356. },
  357. .probe = lm3639_probe,
  358. .remove = lm3639_remove,
  359. .id_table = lm3639_id,
  360. };
  361. module_i2c_driver(lm3639_i2c_driver);
  362. MODULE_DESCRIPTION("Texas Instruments Backlight+Flash LED driver for LM3639");
  363. MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
  364. MODULE_AUTHOR("Ldd Mlp <ldd-mlp@list.ti.com>");
  365. MODULE_LICENSE("GPL v2");