lp8788_bl.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. * FIXME: pwm_apply_args() should be removed when switching to
  128. * the atomic PWM API.
  129. */
  130. pwm_apply_args(pwm);
  131. }
  132. pwm_config(bl->pwm, duty, period);
  133. if (duty)
  134. pwm_enable(bl->pwm);
  135. else
  136. pwm_disable(bl->pwm);
  137. }
  138. static int lp8788_bl_update_status(struct backlight_device *bl_dev)
  139. {
  140. struct lp8788_bl *bl = bl_get_data(bl_dev);
  141. enum lp8788_bl_ctrl_mode mode = bl->mode;
  142. if (bl_dev->props.state & BL_CORE_SUSPENDED)
  143. bl_dev->props.brightness = 0;
  144. if (is_brightness_ctrl_by_pwm(mode)) {
  145. int brt = bl_dev->props.brightness;
  146. int max = bl_dev->props.max_brightness;
  147. lp8788_pwm_ctrl(bl, brt, max);
  148. } else if (is_brightness_ctrl_by_register(mode)) {
  149. u8 brt = bl_dev->props.brightness;
  150. lp8788_write_byte(bl->lp, LP8788_BL_BRIGHTNESS, brt);
  151. }
  152. return 0;
  153. }
  154. static const struct backlight_ops lp8788_bl_ops = {
  155. .options = BL_CORE_SUSPENDRESUME,
  156. .update_status = lp8788_bl_update_status,
  157. };
  158. static int lp8788_backlight_register(struct lp8788_bl *bl)
  159. {
  160. struct backlight_device *bl_dev;
  161. struct backlight_properties props;
  162. struct lp8788_backlight_platform_data *pdata = bl->pdata;
  163. int init_brt;
  164. char *name;
  165. props.type = BACKLIGHT_PLATFORM;
  166. props.max_brightness = MAX_BRIGHTNESS;
  167. /* Initial brightness */
  168. if (pdata)
  169. init_brt = min_t(int, pdata->initial_brightness,
  170. props.max_brightness);
  171. else
  172. init_brt = 0;
  173. props.brightness = init_brt;
  174. /* Backlight device name */
  175. if (!pdata || !pdata->name)
  176. name = DEFAULT_BL_NAME;
  177. else
  178. name = pdata->name;
  179. bl_dev = backlight_device_register(name, bl->lp->dev, bl,
  180. &lp8788_bl_ops, &props);
  181. if (IS_ERR(bl_dev))
  182. return PTR_ERR(bl_dev);
  183. bl->bl_dev = bl_dev;
  184. return 0;
  185. }
  186. static void lp8788_backlight_unregister(struct lp8788_bl *bl)
  187. {
  188. struct backlight_device *bl_dev = bl->bl_dev;
  189. backlight_device_unregister(bl_dev);
  190. }
  191. static ssize_t lp8788_get_bl_ctl_mode(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct lp8788_bl *bl = dev_get_drvdata(dev);
  195. enum lp8788_bl_ctrl_mode mode = bl->mode;
  196. char *strmode;
  197. if (is_brightness_ctrl_by_pwm(mode))
  198. strmode = "PWM based";
  199. else if (is_brightness_ctrl_by_register(mode))
  200. strmode = "Register based";
  201. else
  202. strmode = "Invalid mode";
  203. return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
  204. }
  205. static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp8788_get_bl_ctl_mode, NULL);
  206. static struct attribute *lp8788_attributes[] = {
  207. &dev_attr_bl_ctl_mode.attr,
  208. NULL,
  209. };
  210. static const struct attribute_group lp8788_attr_group = {
  211. .attrs = lp8788_attributes,
  212. };
  213. static int lp8788_backlight_probe(struct platform_device *pdev)
  214. {
  215. struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
  216. struct lp8788_bl *bl;
  217. int ret;
  218. bl = devm_kzalloc(lp->dev, sizeof(struct lp8788_bl), GFP_KERNEL);
  219. if (!bl)
  220. return -ENOMEM;
  221. bl->lp = lp;
  222. if (lp->pdata)
  223. bl->pdata = lp->pdata->bl_pdata;
  224. platform_set_drvdata(pdev, bl);
  225. ret = lp8788_backlight_configure(bl);
  226. if (ret) {
  227. dev_err(lp->dev, "backlight config err: %d\n", ret);
  228. goto err_dev;
  229. }
  230. ret = lp8788_backlight_register(bl);
  231. if (ret) {
  232. dev_err(lp->dev, "register backlight err: %d\n", ret);
  233. goto err_dev;
  234. }
  235. ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
  236. if (ret) {
  237. dev_err(lp->dev, "register sysfs err: %d\n", ret);
  238. goto err_sysfs;
  239. }
  240. backlight_update_status(bl->bl_dev);
  241. return 0;
  242. err_sysfs:
  243. lp8788_backlight_unregister(bl);
  244. err_dev:
  245. return ret;
  246. }
  247. static int lp8788_backlight_remove(struct platform_device *pdev)
  248. {
  249. struct lp8788_bl *bl = platform_get_drvdata(pdev);
  250. struct backlight_device *bl_dev = bl->bl_dev;
  251. bl_dev->props.brightness = 0;
  252. backlight_update_status(bl_dev);
  253. sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group);
  254. lp8788_backlight_unregister(bl);
  255. return 0;
  256. }
  257. static struct platform_driver lp8788_bl_driver = {
  258. .probe = lp8788_backlight_probe,
  259. .remove = lp8788_backlight_remove,
  260. .driver = {
  261. .name = LP8788_DEV_BACKLIGHT,
  262. },
  263. };
  264. module_platform_driver(lp8788_bl_driver);
  265. MODULE_DESCRIPTION("Texas Instruments LP8788 Backlight Driver");
  266. MODULE_AUTHOR("Milo Kim");
  267. MODULE_LICENSE("GPL");
  268. MODULE_ALIAS("platform:lp8788-backlight");