lp8788_bl.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * TI LP8788 MFD - backlight driver
  3. *
  4. * Copyright 2012 Texas Instruments
  5. *
  6. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/backlight.h>
  14. #include <linux/err.h>
  15. #include <linux/mfd/lp8788.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pwm.h>
  19. #include <linux/slab.h>
  20. /* Register address */
  21. #define LP8788_BL_CONFIG 0x96
  22. #define LP8788_BL_EN BIT(0)
  23. #define LP8788_BL_PWM_INPUT_EN BIT(5)
  24. #define LP8788_BL_FULLSCALE_SHIFT 2
  25. #define LP8788_BL_DIM_MODE_SHIFT 1
  26. #define LP8788_BL_PWM_POLARITY_SHIFT 6
  27. #define LP8788_BL_BRIGHTNESS 0x97
  28. #define LP8788_BL_RAMP 0x98
  29. #define LP8788_BL_RAMP_RISE_SHIFT 4
  30. #define MAX_BRIGHTNESS 127
  31. #define DEFAULT_BL_NAME "lcd-backlight"
  32. struct lp8788_bl_config {
  33. enum lp8788_bl_ctrl_mode bl_mode;
  34. enum lp8788_bl_dim_mode dim_mode;
  35. enum lp8788_bl_full_scale_current full_scale;
  36. enum lp8788_bl_ramp_step rise_time;
  37. enum lp8788_bl_ramp_step fall_time;
  38. enum pwm_polarity pwm_pol;
  39. };
  40. struct lp8788_bl {
  41. struct lp8788 *lp;
  42. struct backlight_device *bl_dev;
  43. struct lp8788_backlight_platform_data *pdata;
  44. enum lp8788_bl_ctrl_mode mode;
  45. struct pwm_device *pwm;
  46. };
  47. static struct lp8788_bl_config default_bl_config = {
  48. .bl_mode = LP8788_BL_REGISTER_ONLY,
  49. .dim_mode = LP8788_DIM_EXPONENTIAL,
  50. .full_scale = LP8788_FULLSCALE_1900uA,
  51. .rise_time = LP8788_RAMP_8192us,
  52. .fall_time = LP8788_RAMP_8192us,
  53. .pwm_pol = PWM_POLARITY_NORMAL,
  54. };
  55. static inline bool is_brightness_ctrl_by_pwm(enum lp8788_bl_ctrl_mode mode)
  56. {
  57. return mode == LP8788_BL_COMB_PWM_BASED;
  58. }
  59. static inline bool is_brightness_ctrl_by_register(enum lp8788_bl_ctrl_mode mode)
  60. {
  61. return mode == LP8788_BL_REGISTER_ONLY ||
  62. mode == LP8788_BL_COMB_REGISTER_BASED;
  63. }
  64. static int lp8788_backlight_configure(struct lp8788_bl *bl)
  65. {
  66. struct lp8788_backlight_platform_data *pdata = bl->pdata;
  67. struct lp8788_bl_config *cfg = &default_bl_config;
  68. int ret;
  69. u8 val;
  70. /*
  71. * Update chip configuration if platform data exists,
  72. * otherwise use the default settings.
  73. */
  74. if (pdata) {
  75. cfg->bl_mode = pdata->bl_mode;
  76. cfg->dim_mode = pdata->dim_mode;
  77. cfg->full_scale = pdata->full_scale;
  78. cfg->rise_time = pdata->rise_time;
  79. cfg->fall_time = pdata->fall_time;
  80. cfg->pwm_pol = pdata->pwm_pol;
  81. }
  82. /* Brightness ramp up/down */
  83. val = (cfg->rise_time << LP8788_BL_RAMP_RISE_SHIFT) | cfg->fall_time;
  84. ret = lp8788_write_byte(bl->lp, LP8788_BL_RAMP, val);
  85. if (ret)
  86. return ret;
  87. /* Fullscale current setting */
  88. val = (cfg->full_scale << LP8788_BL_FULLSCALE_SHIFT) |
  89. (cfg->dim_mode << LP8788_BL_DIM_MODE_SHIFT);
  90. /* Brightness control mode */
  91. switch (cfg->bl_mode) {
  92. case LP8788_BL_REGISTER_ONLY:
  93. val |= LP8788_BL_EN;
  94. break;
  95. case LP8788_BL_COMB_PWM_BASED:
  96. case LP8788_BL_COMB_REGISTER_BASED:
  97. val |= LP8788_BL_EN | LP8788_BL_PWM_INPUT_EN |
  98. (cfg->pwm_pol << LP8788_BL_PWM_POLARITY_SHIFT);
  99. break;
  100. default:
  101. dev_err(bl->lp->dev, "invalid mode: %d\n", cfg->bl_mode);
  102. return -EINVAL;
  103. }
  104. bl->mode = cfg->bl_mode;
  105. return lp8788_write_byte(bl->lp, LP8788_BL_CONFIG, val);
  106. }
  107. static void lp8788_pwm_ctrl(struct lp8788_bl *bl, int br, int max_br)
  108. {
  109. unsigned int period;
  110. unsigned int duty;
  111. struct device *dev;
  112. struct pwm_device *pwm;
  113. if (!bl->pdata)
  114. return;
  115. period = bl->pdata->period_ns;
  116. duty = br * period / max_br;
  117. dev = bl->lp->dev;
  118. /* request PWM device with the consumer name */
  119. if (!bl->pwm) {
  120. pwm = devm_pwm_get(dev, LP8788_DEV_BACKLIGHT);
  121. if (IS_ERR(pwm)) {
  122. dev_err(dev, "can not get PWM device\n");
  123. return;
  124. }
  125. bl->pwm = pwm;
  126. }
  127. pwm_config(bl->pwm, duty, period);
  128. if (duty)
  129. pwm_enable(bl->pwm);
  130. else
  131. pwm_disable(bl->pwm);
  132. }
  133. static int lp8788_bl_update_status(struct backlight_device *bl_dev)
  134. {
  135. struct lp8788_bl *bl = bl_get_data(bl_dev);
  136. enum lp8788_bl_ctrl_mode mode = bl->mode;
  137. if (bl_dev->props.state & BL_CORE_SUSPENDED)
  138. bl_dev->props.brightness = 0;
  139. if (is_brightness_ctrl_by_pwm(mode)) {
  140. int brt = bl_dev->props.brightness;
  141. int max = bl_dev->props.max_brightness;
  142. lp8788_pwm_ctrl(bl, brt, max);
  143. } else if (is_brightness_ctrl_by_register(mode)) {
  144. u8 brt = bl_dev->props.brightness;
  145. lp8788_write_byte(bl->lp, LP8788_BL_BRIGHTNESS, brt);
  146. }
  147. return 0;
  148. }
  149. static const struct backlight_ops lp8788_bl_ops = {
  150. .options = BL_CORE_SUSPENDRESUME,
  151. .update_status = lp8788_bl_update_status,
  152. };
  153. static int lp8788_backlight_register(struct lp8788_bl *bl)
  154. {
  155. struct backlight_device *bl_dev;
  156. struct backlight_properties props;
  157. struct lp8788_backlight_platform_data *pdata = bl->pdata;
  158. int init_brt;
  159. char *name;
  160. props.type = BACKLIGHT_PLATFORM;
  161. props.max_brightness = MAX_BRIGHTNESS;
  162. /* Initial brightness */
  163. if (pdata)
  164. init_brt = min_t(int, pdata->initial_brightness,
  165. props.max_brightness);
  166. else
  167. init_brt = 0;
  168. props.brightness = init_brt;
  169. /* Backlight device name */
  170. if (!pdata || !pdata->name)
  171. name = DEFAULT_BL_NAME;
  172. else
  173. name = pdata->name;
  174. bl_dev = backlight_device_register(name, bl->lp->dev, bl,
  175. &lp8788_bl_ops, &props);
  176. if (IS_ERR(bl_dev))
  177. return PTR_ERR(bl_dev);
  178. bl->bl_dev = bl_dev;
  179. return 0;
  180. }
  181. static void lp8788_backlight_unregister(struct lp8788_bl *bl)
  182. {
  183. struct backlight_device *bl_dev = bl->bl_dev;
  184. if (bl_dev)
  185. backlight_device_unregister(bl_dev);
  186. }
  187. static ssize_t lp8788_get_bl_ctl_mode(struct device *dev,
  188. struct device_attribute *attr, char *buf)
  189. {
  190. struct lp8788_bl *bl = dev_get_drvdata(dev);
  191. enum lp8788_bl_ctrl_mode mode = bl->mode;
  192. char *strmode;
  193. if (is_brightness_ctrl_by_pwm(mode))
  194. strmode = "PWM based";
  195. else if (is_brightness_ctrl_by_register(mode))
  196. strmode = "Register based";
  197. else
  198. strmode = "Invalid mode";
  199. return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
  200. }
  201. static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp8788_get_bl_ctl_mode, NULL);
  202. static struct attribute *lp8788_attributes[] = {
  203. &dev_attr_bl_ctl_mode.attr,
  204. NULL,
  205. };
  206. static const struct attribute_group lp8788_attr_group = {
  207. .attrs = lp8788_attributes,
  208. };
  209. static int lp8788_backlight_probe(struct platform_device *pdev)
  210. {
  211. struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
  212. struct lp8788_bl *bl;
  213. int ret;
  214. bl = devm_kzalloc(lp->dev, sizeof(struct lp8788_bl), GFP_KERNEL);
  215. if (!bl)
  216. return -ENOMEM;
  217. bl->lp = lp;
  218. if (lp->pdata)
  219. bl->pdata = lp->pdata->bl_pdata;
  220. platform_set_drvdata(pdev, bl);
  221. ret = lp8788_backlight_configure(bl);
  222. if (ret) {
  223. dev_err(lp->dev, "backlight config err: %d\n", ret);
  224. goto err_dev;
  225. }
  226. ret = lp8788_backlight_register(bl);
  227. if (ret) {
  228. dev_err(lp->dev, "register backlight err: %d\n", ret);
  229. goto err_dev;
  230. }
  231. ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
  232. if (ret) {
  233. dev_err(lp->dev, "register sysfs err: %d\n", ret);
  234. goto err_sysfs;
  235. }
  236. backlight_update_status(bl->bl_dev);
  237. return 0;
  238. err_sysfs:
  239. lp8788_backlight_unregister(bl);
  240. err_dev:
  241. return ret;
  242. }
  243. static int lp8788_backlight_remove(struct platform_device *pdev)
  244. {
  245. struct lp8788_bl *bl = platform_get_drvdata(pdev);
  246. struct backlight_device *bl_dev = bl->bl_dev;
  247. bl_dev->props.brightness = 0;
  248. backlight_update_status(bl_dev);
  249. sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group);
  250. lp8788_backlight_unregister(bl);
  251. return 0;
  252. }
  253. static struct platform_driver lp8788_bl_driver = {
  254. .probe = lp8788_backlight_probe,
  255. .remove = lp8788_backlight_remove,
  256. .driver = {
  257. .name = LP8788_DEV_BACKLIGHT,
  258. },
  259. };
  260. module_platform_driver(lp8788_bl_driver);
  261. MODULE_DESCRIPTION("Texas Instruments LP8788 Backlight Driver");
  262. MODULE_AUTHOR("Milo Kim");
  263. MODULE_LICENSE("GPL");
  264. MODULE_ALIAS("platform:lp8788-backlight");