adp5520_bl.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Backlight driver for Analog Devices ADP5520/ADP5501 MFD PMICs
  3. *
  4. * Copyright 2009 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/fb.h>
  12. #include <linux/backlight.h>
  13. #include <linux/mfd/adp5520.h>
  14. struct adp5520_bl {
  15. struct device *master;
  16. struct adp5520_backlight_platfrom_data *pdata;
  17. struct mutex lock;
  18. unsigned long cached_daylight_max;
  19. int id;
  20. int current_brightness;
  21. };
  22. static int adp5520_bl_set(struct backlight_device *bl, int brightness)
  23. {
  24. struct adp5520_bl *data = bl_get_data(bl);
  25. struct device *master = data->master;
  26. int ret = 0;
  27. if (data->pdata->en_ambl_sens) {
  28. if ((brightness > 0) && (brightness < ADP5020_MAX_BRIGHTNESS)) {
  29. /* Disable Ambient Light auto adjust */
  30. ret |= adp5520_clr_bits(master, BL_CONTROL,
  31. BL_AUTO_ADJ);
  32. ret |= adp5520_write(master, DAYLIGHT_MAX, brightness);
  33. } else {
  34. /*
  35. * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
  36. * restore daylight l3 sysfs brightness
  37. */
  38. ret |= adp5520_write(master, DAYLIGHT_MAX,
  39. data->cached_daylight_max);
  40. ret |= adp5520_set_bits(master, BL_CONTROL,
  41. BL_AUTO_ADJ);
  42. }
  43. } else {
  44. ret |= adp5520_write(master, DAYLIGHT_MAX, brightness);
  45. }
  46. if (data->current_brightness && brightness == 0)
  47. ret |= adp5520_set_bits(master,
  48. MODE_STATUS, DIM_EN);
  49. else if (data->current_brightness == 0 && brightness)
  50. ret |= adp5520_clr_bits(master,
  51. MODE_STATUS, DIM_EN);
  52. if (!ret)
  53. data->current_brightness = brightness;
  54. return ret;
  55. }
  56. static int adp5520_bl_update_status(struct backlight_device *bl)
  57. {
  58. int brightness = bl->props.brightness;
  59. if (bl->props.power != FB_BLANK_UNBLANK)
  60. brightness = 0;
  61. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  62. brightness = 0;
  63. return adp5520_bl_set(bl, brightness);
  64. }
  65. static int adp5520_bl_get_brightness(struct backlight_device *bl)
  66. {
  67. struct adp5520_bl *data = bl_get_data(bl);
  68. int error;
  69. uint8_t reg_val;
  70. error = adp5520_read(data->master, BL_VALUE, &reg_val);
  71. return error ? data->current_brightness : reg_val;
  72. }
  73. static struct backlight_ops adp5520_bl_ops = {
  74. .update_status = adp5520_bl_update_status,
  75. .get_brightness = adp5520_bl_get_brightness,
  76. };
  77. static int adp5520_bl_setup(struct backlight_device *bl)
  78. {
  79. struct adp5520_bl *data = bl_get_data(bl);
  80. struct device *master = data->master;
  81. struct adp5520_backlight_platfrom_data *pdata = data->pdata;
  82. int ret = 0;
  83. ret |= adp5520_write(master, DAYLIGHT_MAX, pdata->l1_daylight_max);
  84. ret |= adp5520_write(master, DAYLIGHT_DIM, pdata->l1_daylight_dim);
  85. if (pdata->en_ambl_sens) {
  86. data->cached_daylight_max = pdata->l1_daylight_max;
  87. ret |= adp5520_write(master, OFFICE_MAX, pdata->l2_office_max);
  88. ret |= adp5520_write(master, OFFICE_DIM, pdata->l2_office_dim);
  89. ret |= adp5520_write(master, DARK_MAX, pdata->l3_dark_max);
  90. ret |= adp5520_write(master, DARK_DIM, pdata->l3_dark_dim);
  91. ret |= adp5520_write(master, L2_TRIP, pdata->l2_trip);
  92. ret |= adp5520_write(master, L2_HYS, pdata->l2_hyst);
  93. ret |= adp5520_write(master, L3_TRIP, pdata->l3_trip);
  94. ret |= adp5520_write(master, L3_HYS, pdata->l3_hyst);
  95. ret |= adp5520_write(master, ALS_CMPR_CFG,
  96. ALS_CMPR_CFG_VAL(pdata->abml_filt, L3_EN));
  97. }
  98. ret |= adp5520_write(master, BL_CONTROL,
  99. BL_CTRL_VAL(pdata->fade_led_law, pdata->en_ambl_sens));
  100. ret |= adp5520_write(master, BL_FADE, FADE_VAL(pdata->fade_in,
  101. pdata->fade_out));
  102. ret |= adp5520_set_bits(master, MODE_STATUS, BL_EN | DIM_EN);
  103. return ret;
  104. }
  105. static ssize_t adp5520_show(struct device *dev, char *buf, int reg)
  106. {
  107. struct adp5520_bl *data = dev_get_drvdata(dev);
  108. int error;
  109. uint8_t reg_val;
  110. mutex_lock(&data->lock);
  111. error = adp5520_read(data->master, reg, &reg_val);
  112. mutex_unlock(&data->lock);
  113. return sprintf(buf, "%u\n", reg_val);
  114. }
  115. static ssize_t adp5520_store(struct device *dev, const char *buf,
  116. size_t count, int reg)
  117. {
  118. struct adp5520_bl *data = dev_get_drvdata(dev);
  119. unsigned long val;
  120. int ret;
  121. ret = strict_strtoul(buf, 10, &val);
  122. if (ret)
  123. return ret;
  124. mutex_lock(&data->lock);
  125. adp5520_write(data->master, reg, val);
  126. mutex_unlock(&data->lock);
  127. return count;
  128. }
  129. static ssize_t adp5520_bl_dark_max_show(struct device *dev,
  130. struct device_attribute *attr, char *buf)
  131. {
  132. return adp5520_show(dev, buf, DARK_MAX);
  133. }
  134. static ssize_t adp5520_bl_dark_max_store(struct device *dev,
  135. struct device_attribute *attr, const char *buf, size_t count)
  136. {
  137. return adp5520_store(dev, buf, count, DARK_MAX);
  138. }
  139. static DEVICE_ATTR(dark_max, 0664, adp5520_bl_dark_max_show,
  140. adp5520_bl_dark_max_store);
  141. static ssize_t adp5520_bl_office_max_show(struct device *dev,
  142. struct device_attribute *attr, char *buf)
  143. {
  144. return adp5520_show(dev, buf, OFFICE_MAX);
  145. }
  146. static ssize_t adp5520_bl_office_max_store(struct device *dev,
  147. struct device_attribute *attr, const char *buf, size_t count)
  148. {
  149. return adp5520_store(dev, buf, count, OFFICE_MAX);
  150. }
  151. static DEVICE_ATTR(office_max, 0664, adp5520_bl_office_max_show,
  152. adp5520_bl_office_max_store);
  153. static ssize_t adp5520_bl_daylight_max_show(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. return adp5520_show(dev, buf, DAYLIGHT_MAX);
  157. }
  158. static ssize_t adp5520_bl_daylight_max_store(struct device *dev,
  159. struct device_attribute *attr, const char *buf, size_t count)
  160. {
  161. struct adp5520_bl *data = dev_get_drvdata(dev);
  162. strict_strtoul(buf, 10, &data->cached_daylight_max);
  163. return adp5520_store(dev, buf, count, DAYLIGHT_MAX);
  164. }
  165. static DEVICE_ATTR(daylight_max, 0664, adp5520_bl_daylight_max_show,
  166. adp5520_bl_daylight_max_store);
  167. static ssize_t adp5520_bl_dark_dim_show(struct device *dev,
  168. struct device_attribute *attr, char *buf)
  169. {
  170. return adp5520_show(dev, buf, DARK_DIM);
  171. }
  172. static ssize_t adp5520_bl_dark_dim_store(struct device *dev,
  173. struct device_attribute *attr,
  174. const char *buf, size_t count)
  175. {
  176. return adp5520_store(dev, buf, count, DARK_DIM);
  177. }
  178. static DEVICE_ATTR(dark_dim, 0664, adp5520_bl_dark_dim_show,
  179. adp5520_bl_dark_dim_store);
  180. static ssize_t adp5520_bl_office_dim_show(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. return adp5520_show(dev, buf, OFFICE_DIM);
  184. }
  185. static ssize_t adp5520_bl_office_dim_store(struct device *dev,
  186. struct device_attribute *attr,
  187. const char *buf, size_t count)
  188. {
  189. return adp5520_store(dev, buf, count, OFFICE_DIM);
  190. }
  191. static DEVICE_ATTR(office_dim, 0664, adp5520_bl_office_dim_show,
  192. adp5520_bl_office_dim_store);
  193. static ssize_t adp5520_bl_daylight_dim_show(struct device *dev,
  194. struct device_attribute *attr, char *buf)
  195. {
  196. return adp5520_show(dev, buf, DAYLIGHT_DIM);
  197. }
  198. static ssize_t adp5520_bl_daylight_dim_store(struct device *dev,
  199. struct device_attribute *attr,
  200. const char *buf, size_t count)
  201. {
  202. return adp5520_store(dev, buf, count, DAYLIGHT_DIM);
  203. }
  204. static DEVICE_ATTR(daylight_dim, 0664, adp5520_bl_daylight_dim_show,
  205. adp5520_bl_daylight_dim_store);
  206. static struct attribute *adp5520_bl_attributes[] = {
  207. &dev_attr_dark_max.attr,
  208. &dev_attr_dark_dim.attr,
  209. &dev_attr_office_max.attr,
  210. &dev_attr_office_dim.attr,
  211. &dev_attr_daylight_max.attr,
  212. &dev_attr_daylight_dim.attr,
  213. NULL
  214. };
  215. static const struct attribute_group adp5520_bl_attr_group = {
  216. .attrs = adp5520_bl_attributes,
  217. };
  218. static int __devinit adp5520_bl_probe(struct platform_device *pdev)
  219. {
  220. struct backlight_device *bl;
  221. struct adp5520_bl *data;
  222. int ret = 0;
  223. data = kzalloc(sizeof(*data), GFP_KERNEL);
  224. if (data == NULL)
  225. return -ENOMEM;
  226. data->master = pdev->dev.parent;
  227. data->pdata = pdev->dev.platform_data;
  228. if (data->pdata == NULL) {
  229. dev_err(&pdev->dev, "missing platform data\n");
  230. kfree(data);
  231. return -ENODEV;
  232. }
  233. data->id = pdev->id;
  234. data->current_brightness = 0;
  235. mutex_init(&data->lock);
  236. bl = backlight_device_register(pdev->name, data->master,
  237. data, &adp5520_bl_ops);
  238. if (IS_ERR(bl)) {
  239. dev_err(&pdev->dev, "failed to register backlight\n");
  240. kfree(data);
  241. return PTR_ERR(bl);
  242. }
  243. bl->props.max_brightness =
  244. bl->props.brightness = ADP5020_MAX_BRIGHTNESS;
  245. if (data->pdata->en_ambl_sens)
  246. ret = sysfs_create_group(&bl->dev.kobj,
  247. &adp5520_bl_attr_group);
  248. if (ret) {
  249. dev_err(&pdev->dev, "failed to register sysfs\n");
  250. backlight_device_unregister(bl);
  251. kfree(data);
  252. }
  253. platform_set_drvdata(pdev, bl);
  254. ret |= adp5520_bl_setup(bl);
  255. backlight_update_status(bl);
  256. return ret;
  257. }
  258. static int __devexit adp5520_bl_remove(struct platform_device *pdev)
  259. {
  260. struct backlight_device *bl = platform_get_drvdata(pdev);
  261. struct adp5520_bl *data = bl_get_data(bl);
  262. adp5520_clr_bits(data->master, MODE_STATUS, BL_EN);
  263. if (data->pdata->en_ambl_sens)
  264. sysfs_remove_group(&bl->dev.kobj,
  265. &adp5520_bl_attr_group);
  266. backlight_device_unregister(bl);
  267. kfree(data);
  268. return 0;
  269. }
  270. #ifdef CONFIG_PM
  271. static int adp5520_bl_suspend(struct platform_device *pdev,
  272. pm_message_t state)
  273. {
  274. struct backlight_device *bl = platform_get_drvdata(pdev);
  275. return adp5520_bl_set(bl, 0);
  276. }
  277. static int adp5520_bl_resume(struct platform_device *pdev)
  278. {
  279. struct backlight_device *bl = platform_get_drvdata(pdev);
  280. backlight_update_status(bl);
  281. return 0;
  282. }
  283. #else
  284. #define adp5520_bl_suspend NULL
  285. #define adp5520_bl_resume NULL
  286. #endif
  287. static struct platform_driver adp5520_bl_driver = {
  288. .driver = {
  289. .name = "adp5520-backlight",
  290. .owner = THIS_MODULE,
  291. },
  292. .probe = adp5520_bl_probe,
  293. .remove = __devexit_p(adp5520_bl_remove),
  294. .suspend = adp5520_bl_suspend,
  295. .resume = adp5520_bl_resume,
  296. };
  297. static int __init adp5520_bl_init(void)
  298. {
  299. return platform_driver_register(&adp5520_bl_driver);
  300. }
  301. module_init(adp5520_bl_init);
  302. static void __exit adp5520_bl_exit(void)
  303. {
  304. platform_driver_unregister(&adp5520_bl_driver);
  305. }
  306. module_exit(adp5520_bl_exit);
  307. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  308. MODULE_DESCRIPTION("ADP5520(01) Backlight Driver");
  309. MODULE_LICENSE("GPL");
  310. MODULE_ALIAS("platform:adp5520-backlight");