adp5520_bl.c 10 KB

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