lm3630a_bl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Simple driver for Texas Instruments LM3630A Backlight 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/backlight.h>
  14. #include <linux/err.h>
  15. #include <linux/delay.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/regmap.h>
  19. #include <linux/pwm.h>
  20. #include <linux/platform_data/lm3630a_bl.h>
  21. #define REG_CTRL 0x00
  22. #define REG_BOOST 0x02
  23. #define REG_CONFIG 0x01
  24. #define REG_BRT_A 0x03
  25. #define REG_BRT_B 0x04
  26. #define REG_I_A 0x05
  27. #define REG_I_B 0x06
  28. #define REG_INT_STATUS 0x09
  29. #define REG_INT_EN 0x0A
  30. #define REG_FAULT 0x0B
  31. #define REG_PWM_OUTLOW 0x12
  32. #define REG_PWM_OUTHIGH 0x13
  33. #define REG_MAX 0x1F
  34. #define INT_DEBOUNCE_MSEC 10
  35. struct lm3630a_chip {
  36. struct device *dev;
  37. struct delayed_work work;
  38. int irq;
  39. struct workqueue_struct *irqthread;
  40. struct lm3630a_platform_data *pdata;
  41. struct backlight_device *bleda;
  42. struct backlight_device *bledb;
  43. struct regmap *regmap;
  44. struct pwm_device *pwmd;
  45. };
  46. /* i2c access */
  47. static int lm3630a_read(struct lm3630a_chip *pchip, unsigned int reg)
  48. {
  49. int rval;
  50. unsigned int reg_val;
  51. rval = regmap_read(pchip->regmap, reg, &reg_val);
  52. if (rval < 0)
  53. return rval;
  54. return reg_val & 0xFF;
  55. }
  56. static int lm3630a_write(struct lm3630a_chip *pchip,
  57. unsigned int reg, unsigned int data)
  58. {
  59. return regmap_write(pchip->regmap, reg, data);
  60. }
  61. static int lm3630a_update(struct lm3630a_chip *pchip,
  62. unsigned int reg, unsigned int mask,
  63. unsigned int data)
  64. {
  65. return regmap_update_bits(pchip->regmap, reg, mask, data);
  66. }
  67. /* initialize chip */
  68. static int lm3630a_chip_init(struct lm3630a_chip *pchip)
  69. {
  70. int rval;
  71. struct lm3630a_platform_data *pdata = pchip->pdata;
  72. usleep_range(1000, 2000);
  73. /* set Filter Strength Register */
  74. rval = lm3630a_write(pchip, 0x50, 0x03);
  75. /* set Cofig. register */
  76. rval |= lm3630a_update(pchip, REG_CONFIG, 0x07, pdata->pwm_ctrl);
  77. /* set boost control */
  78. rval |= lm3630a_write(pchip, REG_BOOST, 0x38);
  79. /* set current A */
  80. rval |= lm3630a_update(pchip, REG_I_A, 0x1F, 0x1F);
  81. /* set current B */
  82. rval |= lm3630a_write(pchip, REG_I_B, 0x1F);
  83. /* set control */
  84. rval |= lm3630a_update(pchip, REG_CTRL, 0x14, pdata->leda_ctrl);
  85. rval |= lm3630a_update(pchip, REG_CTRL, 0x0B, pdata->ledb_ctrl);
  86. usleep_range(1000, 2000);
  87. /* set brightness A and B */
  88. rval |= lm3630a_write(pchip, REG_BRT_A, pdata->leda_init_brt);
  89. rval |= lm3630a_write(pchip, REG_BRT_B, pdata->ledb_init_brt);
  90. if (rval < 0)
  91. dev_err(pchip->dev, "i2c failed to access register\n");
  92. return rval;
  93. }
  94. /* interrupt handling */
  95. static void lm3630a_delayed_func(struct work_struct *work)
  96. {
  97. int rval;
  98. struct lm3630a_chip *pchip;
  99. pchip = container_of(work, struct lm3630a_chip, work.work);
  100. rval = lm3630a_read(pchip, REG_INT_STATUS);
  101. if (rval < 0) {
  102. dev_err(pchip->dev,
  103. "i2c failed to access REG_INT_STATUS Register\n");
  104. return;
  105. }
  106. dev_info(pchip->dev, "REG_INT_STATUS Register is 0x%x\n", rval);
  107. }
  108. static irqreturn_t lm3630a_isr_func(int irq, void *chip)
  109. {
  110. int rval;
  111. struct lm3630a_chip *pchip = chip;
  112. unsigned long delay = msecs_to_jiffies(INT_DEBOUNCE_MSEC);
  113. queue_delayed_work(pchip->irqthread, &pchip->work, delay);
  114. rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
  115. if (rval < 0) {
  116. dev_err(pchip->dev, "i2c failed to access register\n");
  117. return IRQ_NONE;
  118. }
  119. return IRQ_HANDLED;
  120. }
  121. static int lm3630a_intr_config(struct lm3630a_chip *pchip)
  122. {
  123. int rval;
  124. rval = lm3630a_write(pchip, REG_INT_EN, 0x87);
  125. if (rval < 0)
  126. return rval;
  127. INIT_DELAYED_WORK(&pchip->work, lm3630a_delayed_func);
  128. pchip->irqthread = create_singlethread_workqueue("lm3630a-irqthd");
  129. if (!pchip->irqthread) {
  130. dev_err(pchip->dev, "create irq thread fail\n");
  131. return -ENOMEM;
  132. }
  133. if (request_threaded_irq
  134. (pchip->irq, NULL, lm3630a_isr_func,
  135. IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm3630a_irq", pchip)) {
  136. dev_err(pchip->dev, "request threaded irq fail\n");
  137. destroy_workqueue(pchip->irqthread);
  138. return -ENOMEM;
  139. }
  140. return rval;
  141. }
  142. static void lm3630a_pwm_ctrl(struct lm3630a_chip *pchip, int br, int br_max)
  143. {
  144. unsigned int period = pwm_get_period(pchip->pwmd);
  145. unsigned int duty = br * period / br_max;
  146. pwm_config(pchip->pwmd, duty, period);
  147. if (duty)
  148. pwm_enable(pchip->pwmd);
  149. else
  150. pwm_disable(pchip->pwmd);
  151. }
  152. /* update and get brightness */
  153. static int lm3630a_bank_a_update_status(struct backlight_device *bl)
  154. {
  155. int ret;
  156. struct lm3630a_chip *pchip = bl_get_data(bl);
  157. enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
  158. /* pwm control */
  159. if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
  160. lm3630a_pwm_ctrl(pchip, bl->props.brightness,
  161. bl->props.max_brightness);
  162. return bl->props.brightness;
  163. }
  164. /* disable sleep */
  165. ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
  166. if (ret < 0)
  167. goto out_i2c_err;
  168. usleep_range(1000, 2000);
  169. /* minimum brightness is 0x04 */
  170. ret = lm3630a_write(pchip, REG_BRT_A, bl->props.brightness);
  171. if (bl->props.brightness < 0x4)
  172. ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDA_ENABLE, 0);
  173. else
  174. ret |= lm3630a_update(pchip, REG_CTRL,
  175. LM3630A_LEDA_ENABLE, LM3630A_LEDA_ENABLE);
  176. if (ret < 0)
  177. goto out_i2c_err;
  178. return bl->props.brightness;
  179. out_i2c_err:
  180. dev_err(pchip->dev, "i2c failed to access\n");
  181. return bl->props.brightness;
  182. }
  183. static int lm3630a_bank_a_get_brightness(struct backlight_device *bl)
  184. {
  185. int brightness, rval;
  186. struct lm3630a_chip *pchip = bl_get_data(bl);
  187. enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
  188. if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) {
  189. rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
  190. if (rval < 0)
  191. goto out_i2c_err;
  192. brightness = (rval & 0x01) << 8;
  193. rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
  194. if (rval < 0)
  195. goto out_i2c_err;
  196. brightness |= rval;
  197. goto out;
  198. }
  199. /* disable sleep */
  200. rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
  201. if (rval < 0)
  202. goto out_i2c_err;
  203. usleep_range(1000, 2000);
  204. rval = lm3630a_read(pchip, REG_BRT_A);
  205. if (rval < 0)
  206. goto out_i2c_err;
  207. brightness = rval;
  208. out:
  209. bl->props.brightness = brightness;
  210. return bl->props.brightness;
  211. out_i2c_err:
  212. dev_err(pchip->dev, "i2c failed to access register\n");
  213. return 0;
  214. }
  215. static const struct backlight_ops lm3630a_bank_a_ops = {
  216. .options = BL_CORE_SUSPENDRESUME,
  217. .update_status = lm3630a_bank_a_update_status,
  218. .get_brightness = lm3630a_bank_a_get_brightness,
  219. };
  220. /* update and get brightness */
  221. static int lm3630a_bank_b_update_status(struct backlight_device *bl)
  222. {
  223. int ret;
  224. struct lm3630a_chip *pchip = bl_get_data(bl);
  225. enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
  226. /* pwm control */
  227. if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
  228. lm3630a_pwm_ctrl(pchip, bl->props.brightness,
  229. bl->props.max_brightness);
  230. return bl->props.brightness;
  231. }
  232. /* disable sleep */
  233. ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
  234. if (ret < 0)
  235. goto out_i2c_err;
  236. usleep_range(1000, 2000);
  237. /* minimum brightness is 0x04 */
  238. ret = lm3630a_write(pchip, REG_BRT_B, bl->props.brightness);
  239. if (bl->props.brightness < 0x4)
  240. ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDB_ENABLE, 0);
  241. else
  242. ret |= lm3630a_update(pchip, REG_CTRL,
  243. LM3630A_LEDB_ENABLE, LM3630A_LEDB_ENABLE);
  244. if (ret < 0)
  245. goto out_i2c_err;
  246. return bl->props.brightness;
  247. out_i2c_err:
  248. dev_err(pchip->dev, "i2c failed to access REG_CTRL\n");
  249. return bl->props.brightness;
  250. }
  251. static int lm3630a_bank_b_get_brightness(struct backlight_device *bl)
  252. {
  253. int brightness, rval;
  254. struct lm3630a_chip *pchip = bl_get_data(bl);
  255. enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl;
  256. if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) {
  257. rval = lm3630a_read(pchip, REG_PWM_OUTHIGH);
  258. if (rval < 0)
  259. goto out_i2c_err;
  260. brightness = (rval & 0x01) << 8;
  261. rval = lm3630a_read(pchip, REG_PWM_OUTLOW);
  262. if (rval < 0)
  263. goto out_i2c_err;
  264. brightness |= rval;
  265. goto out;
  266. }
  267. /* disable sleep */
  268. rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00);
  269. if (rval < 0)
  270. goto out_i2c_err;
  271. usleep_range(1000, 2000);
  272. rval = lm3630a_read(pchip, REG_BRT_B);
  273. if (rval < 0)
  274. goto out_i2c_err;
  275. brightness = rval;
  276. out:
  277. bl->props.brightness = brightness;
  278. return bl->props.brightness;
  279. out_i2c_err:
  280. dev_err(pchip->dev, "i2c failed to access register\n");
  281. return 0;
  282. }
  283. static const struct backlight_ops lm3630a_bank_b_ops = {
  284. .options = BL_CORE_SUSPENDRESUME,
  285. .update_status = lm3630a_bank_b_update_status,
  286. .get_brightness = lm3630a_bank_b_get_brightness,
  287. };
  288. static int lm3630a_backlight_register(struct lm3630a_chip *pchip)
  289. {
  290. struct backlight_properties props;
  291. struct lm3630a_platform_data *pdata = pchip->pdata;
  292. props.type = BACKLIGHT_RAW;
  293. if (pdata->leda_ctrl != LM3630A_LEDA_DISABLE) {
  294. props.brightness = pdata->leda_init_brt;
  295. props.max_brightness = pdata->leda_max_brt;
  296. pchip->bleda =
  297. devm_backlight_device_register(pchip->dev, "lm3630a_leda",
  298. pchip->dev, pchip,
  299. &lm3630a_bank_a_ops, &props);
  300. if (IS_ERR(pchip->bleda))
  301. return PTR_ERR(pchip->bleda);
  302. }
  303. if ((pdata->ledb_ctrl != LM3630A_LEDB_DISABLE) &&
  304. (pdata->ledb_ctrl != LM3630A_LEDB_ON_A)) {
  305. props.brightness = pdata->ledb_init_brt;
  306. props.max_brightness = pdata->ledb_max_brt;
  307. pchip->bledb =
  308. devm_backlight_device_register(pchip->dev, "lm3630a_ledb",
  309. pchip->dev, pchip,
  310. &lm3630a_bank_b_ops, &props);
  311. if (IS_ERR(pchip->bledb))
  312. return PTR_ERR(pchip->bledb);
  313. }
  314. return 0;
  315. }
  316. static const struct regmap_config lm3630a_regmap = {
  317. .reg_bits = 8,
  318. .val_bits = 8,
  319. .max_register = REG_MAX,
  320. };
  321. static int lm3630a_probe(struct i2c_client *client,
  322. const struct i2c_device_id *id)
  323. {
  324. struct lm3630a_platform_data *pdata = dev_get_platdata(&client->dev);
  325. struct lm3630a_chip *pchip;
  326. int rval;
  327. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  328. dev_err(&client->dev, "fail : i2c functionality check\n");
  329. return -EOPNOTSUPP;
  330. }
  331. pchip = devm_kzalloc(&client->dev, sizeof(struct lm3630a_chip),
  332. GFP_KERNEL);
  333. if (!pchip)
  334. return -ENOMEM;
  335. pchip->dev = &client->dev;
  336. pchip->regmap = devm_regmap_init_i2c(client, &lm3630a_regmap);
  337. if (IS_ERR(pchip->regmap)) {
  338. rval = PTR_ERR(pchip->regmap);
  339. dev_err(&client->dev, "fail : allocate reg. map: %d\n", rval);
  340. return rval;
  341. }
  342. i2c_set_clientdata(client, pchip);
  343. if (pdata == NULL) {
  344. pdata = devm_kzalloc(pchip->dev,
  345. sizeof(struct lm3630a_platform_data),
  346. GFP_KERNEL);
  347. if (pdata == NULL)
  348. return -ENOMEM;
  349. /* default values */
  350. pdata->leda_ctrl = LM3630A_LEDA_ENABLE;
  351. pdata->ledb_ctrl = LM3630A_LEDB_ENABLE;
  352. pdata->leda_max_brt = LM3630A_MAX_BRIGHTNESS;
  353. pdata->ledb_max_brt = LM3630A_MAX_BRIGHTNESS;
  354. pdata->leda_init_brt = LM3630A_MAX_BRIGHTNESS;
  355. pdata->ledb_init_brt = LM3630A_MAX_BRIGHTNESS;
  356. }
  357. pchip->pdata = pdata;
  358. /* chip initialize */
  359. rval = lm3630a_chip_init(pchip);
  360. if (rval < 0) {
  361. dev_err(&client->dev, "fail : init chip\n");
  362. return rval;
  363. }
  364. /* backlight register */
  365. rval = lm3630a_backlight_register(pchip);
  366. if (rval < 0) {
  367. dev_err(&client->dev, "fail : backlight register.\n");
  368. return rval;
  369. }
  370. /* pwm */
  371. if (pdata->pwm_ctrl != LM3630A_PWM_DISABLE) {
  372. pchip->pwmd = devm_pwm_get(pchip->dev, "lm3630a-pwm");
  373. if (IS_ERR(pchip->pwmd)) {
  374. dev_err(&client->dev, "fail : get pwm device\n");
  375. return PTR_ERR(pchip->pwmd);
  376. }
  377. }
  378. pchip->pwmd->period = pdata->pwm_period;
  379. /* interrupt enable : irq 0 is not allowed */
  380. pchip->irq = client->irq;
  381. if (pchip->irq) {
  382. rval = lm3630a_intr_config(pchip);
  383. if (rval < 0)
  384. return rval;
  385. }
  386. dev_info(&client->dev, "LM3630A backlight register OK.\n");
  387. return 0;
  388. }
  389. static int lm3630a_remove(struct i2c_client *client)
  390. {
  391. int rval;
  392. struct lm3630a_chip *pchip = i2c_get_clientdata(client);
  393. rval = lm3630a_write(pchip, REG_BRT_A, 0);
  394. if (rval < 0)
  395. dev_err(pchip->dev, "i2c failed to access register\n");
  396. rval = lm3630a_write(pchip, REG_BRT_B, 0);
  397. if (rval < 0)
  398. dev_err(pchip->dev, "i2c failed to access register\n");
  399. if (pchip->irq) {
  400. free_irq(pchip->irq, pchip);
  401. flush_workqueue(pchip->irqthread);
  402. destroy_workqueue(pchip->irqthread);
  403. }
  404. return 0;
  405. }
  406. static const struct i2c_device_id lm3630a_id[] = {
  407. {LM3630A_NAME, 0},
  408. {}
  409. };
  410. MODULE_DEVICE_TABLE(i2c, lm3630a_id);
  411. static struct i2c_driver lm3630a_i2c_driver = {
  412. .driver = {
  413. .name = LM3630A_NAME,
  414. },
  415. .probe = lm3630a_probe,
  416. .remove = lm3630a_remove,
  417. .id_table = lm3630a_id,
  418. };
  419. module_i2c_driver(lm3630a_i2c_driver);
  420. MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A");
  421. MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>");
  422. MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>");
  423. MODULE_LICENSE("GPL v2");