da903x_bl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Backlight driver for Dialog Semiconductor DA9030/DA9034
  3. *
  4. * Copyright (C) 2008 Compulab, Ltd.
  5. * Mike Rapoport <mike@compulab.co.il>
  6. *
  7. * Copyright (C) 2006-2008 Marvell International Ltd.
  8. * Eric Miao <eric.miao@marvell.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/fb.h>
  18. #include <linux/backlight.h>
  19. #include <linux/mfd/da903x.h>
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #define DA9030_WLED_CONTROL 0x25
  23. #define DA9030_WLED_CP_EN (1 << 6)
  24. #define DA9030_WLED_TRIM(x) ((x) & 0x7)
  25. #define DA9034_WLED_CONTROL1 0x3C
  26. #define DA9034_WLED_CONTROL2 0x3D
  27. #define DA9034_WLED_ISET(x) ((x) & 0x1f)
  28. #define DA9034_WLED_BOOST_EN (1 << 5)
  29. #define DA9030_MAX_BRIGHTNESS 7
  30. #define DA9034_MAX_BRIGHTNESS 0x7f
  31. struct da903x_backlight_data {
  32. struct device *da903x_dev;
  33. int id;
  34. int current_brightness;
  35. };
  36. static int da903x_backlight_set(struct backlight_device *bl, int brightness)
  37. {
  38. struct da903x_backlight_data *data = bl_get_data(bl);
  39. struct device *dev = data->da903x_dev;
  40. uint8_t val;
  41. int ret = 0;
  42. switch (data->id) {
  43. case DA9034_ID_WLED:
  44. ret = da903x_update(dev, DA9034_WLED_CONTROL1,
  45. brightness, 0x7f);
  46. if (ret)
  47. return ret;
  48. if (data->current_brightness && brightness == 0)
  49. ret = da903x_clr_bits(dev,
  50. DA9034_WLED_CONTROL2,
  51. DA9034_WLED_BOOST_EN);
  52. if (data->current_brightness == 0 && brightness)
  53. ret = da903x_set_bits(dev,
  54. DA9034_WLED_CONTROL2,
  55. DA9034_WLED_BOOST_EN);
  56. break;
  57. case DA9030_ID_WLED:
  58. val = DA9030_WLED_TRIM(brightness);
  59. val |= brightness ? DA9030_WLED_CP_EN : 0;
  60. ret = da903x_write(dev, DA9030_WLED_CONTROL, val);
  61. break;
  62. }
  63. if (ret)
  64. return ret;
  65. data->current_brightness = brightness;
  66. return 0;
  67. }
  68. static int da903x_backlight_update_status(struct backlight_device *bl)
  69. {
  70. int brightness = bl->props.brightness;
  71. if (bl->props.power != FB_BLANK_UNBLANK)
  72. brightness = 0;
  73. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  74. brightness = 0;
  75. if (bl->props.state & BL_CORE_SUSPENDED)
  76. brightness = 0;
  77. return da903x_backlight_set(bl, brightness);
  78. }
  79. static int da903x_backlight_get_brightness(struct backlight_device *bl)
  80. {
  81. struct da903x_backlight_data *data = bl_get_data(bl);
  82. return data->current_brightness;
  83. }
  84. static const struct backlight_ops da903x_backlight_ops = {
  85. .options = BL_CORE_SUSPENDRESUME,
  86. .update_status = da903x_backlight_update_status,
  87. .get_brightness = da903x_backlight_get_brightness,
  88. };
  89. static int da903x_backlight_probe(struct platform_device *pdev)
  90. {
  91. struct da9034_backlight_pdata *pdata = dev_get_platdata(&pdev->dev);
  92. struct da903x_backlight_data *data;
  93. struct backlight_device *bl;
  94. struct backlight_properties props;
  95. int max_brightness;
  96. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  97. if (data == NULL)
  98. return -ENOMEM;
  99. switch (pdev->id) {
  100. case DA9030_ID_WLED:
  101. max_brightness = DA9030_MAX_BRIGHTNESS;
  102. break;
  103. case DA9034_ID_WLED:
  104. max_brightness = DA9034_MAX_BRIGHTNESS;
  105. break;
  106. default:
  107. dev_err(&pdev->dev, "invalid backlight device ID(%d)\n",
  108. pdev->id);
  109. return -EINVAL;
  110. }
  111. data->id = pdev->id;
  112. data->da903x_dev = pdev->dev.parent;
  113. data->current_brightness = 0;
  114. /* adjust the WLED output current */
  115. if (pdata)
  116. da903x_write(data->da903x_dev, DA9034_WLED_CONTROL2,
  117. DA9034_WLED_ISET(pdata->output_current));
  118. memset(&props, 0, sizeof(props));
  119. props.type = BACKLIGHT_RAW;
  120. props.max_brightness = max_brightness;
  121. bl = devm_backlight_device_register(&pdev->dev, pdev->name,
  122. data->da903x_dev, data,
  123. &da903x_backlight_ops, &props);
  124. if (IS_ERR(bl)) {
  125. dev_err(&pdev->dev, "failed to register backlight\n");
  126. return PTR_ERR(bl);
  127. }
  128. bl->props.brightness = max_brightness;
  129. platform_set_drvdata(pdev, bl);
  130. backlight_update_status(bl);
  131. return 0;
  132. }
  133. static struct platform_driver da903x_backlight_driver = {
  134. .driver = {
  135. .name = "da903x-backlight",
  136. },
  137. .probe = da903x_backlight_probe,
  138. };
  139. module_platform_driver(da903x_backlight_driver);
  140. MODULE_DESCRIPTION("Backlight Driver for Dialog Semiconductor DA9030/DA9034");
  141. MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
  142. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  143. MODULE_LICENSE("GPL");
  144. MODULE_ALIAS("platform:da903x-backlight");