adp5520_bl.c 10.0 KB

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