ot200_bl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (C) 2012 Bachmann electronic GmbH
  3. * Christian Gmeiner <christian.gmeiner@gmail.com>
  4. *
  5. * Backlight driver for ot200 visualisation device from
  6. * Bachmann electronic GmbH.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/fb.h>
  14. #include <linux/backlight.h>
  15. #include <linux/gpio.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/cs5535.h>
  18. static struct cs5535_mfgpt_timer *pwm_timer;
  19. /* this array defines the mapping of brightness in % to pwm frequency */
  20. static const u8 dim_table[101] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
  21. 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  22. 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9,
  23. 10, 10, 11, 11, 12, 12, 13, 14, 15, 15, 16,
  24. 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28,
  25. 30, 31, 33, 35, 37, 39, 41, 43, 45, 47, 50,
  26. 53, 55, 58, 61, 65, 68, 72, 75, 79, 84, 88,
  27. 93, 97, 103, 108, 114, 120, 126, 133, 140,
  28. 147, 155, 163};
  29. struct ot200_backlight_data {
  30. int current_brightness;
  31. };
  32. #define GPIO_DIMM 27
  33. #define SCALE 1
  34. #define CMP1MODE 0x2 /* compare on GE; output high on compare
  35. * greater than or equal */
  36. #define PWM_SETUP (SCALE | CMP1MODE << 6 | MFGPT_SETUP_CNTEN)
  37. #define MAX_COMP2 163
  38. static int ot200_backlight_update_status(struct backlight_device *bl)
  39. {
  40. struct ot200_backlight_data *data = bl_get_data(bl);
  41. int brightness = bl->props.brightness;
  42. if (bl->props.state & BL_CORE_FBBLANK)
  43. brightness = 0;
  44. /* enable or disable PWM timer */
  45. if (brightness == 0)
  46. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, 0);
  47. else if (data->current_brightness == 0) {
  48. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0);
  49. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP,
  50. MFGPT_SETUP_CNTEN);
  51. }
  52. /* apply new brightness value */
  53. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1,
  54. MAX_COMP2 - dim_table[brightness]);
  55. data->current_brightness = brightness;
  56. return 0;
  57. }
  58. static int ot200_backlight_get_brightness(struct backlight_device *bl)
  59. {
  60. struct ot200_backlight_data *data = bl_get_data(bl);
  61. return data->current_brightness;
  62. }
  63. static const struct backlight_ops ot200_backlight_ops = {
  64. .update_status = ot200_backlight_update_status,
  65. .get_brightness = ot200_backlight_get_brightness,
  66. };
  67. static int ot200_backlight_probe(struct platform_device *pdev)
  68. {
  69. struct backlight_device *bl;
  70. struct ot200_backlight_data *data;
  71. struct backlight_properties props;
  72. int retval = 0;
  73. /* request gpio */
  74. if (devm_gpio_request(&pdev->dev, GPIO_DIMM,
  75. "ot200 backlight dimmer") < 0) {
  76. dev_err(&pdev->dev, "failed to request GPIO %d\n", GPIO_DIMM);
  77. return -ENODEV;
  78. }
  79. /* request timer */
  80. pwm_timer = cs5535_mfgpt_alloc_timer(7, MFGPT_DOMAIN_ANY);
  81. if (!pwm_timer) {
  82. dev_err(&pdev->dev, "MFGPT 7 not available\n");
  83. return -ENODEV;
  84. }
  85. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  86. if (!data) {
  87. retval = -ENOMEM;
  88. goto error_devm_kzalloc;
  89. }
  90. /* setup gpio */
  91. cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_ENABLE);
  92. cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_AUX1);
  93. /* setup timer */
  94. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, 0);
  95. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP2, MAX_COMP2);
  96. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, PWM_SETUP);
  97. data->current_brightness = 100;
  98. props.max_brightness = 100;
  99. props.brightness = 100;
  100. props.type = BACKLIGHT_RAW;
  101. bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev),
  102. &pdev->dev, data, &ot200_backlight_ops,
  103. &props);
  104. if (IS_ERR(bl)) {
  105. dev_err(&pdev->dev, "failed to register backlight\n");
  106. retval = PTR_ERR(bl);
  107. goto error_devm_kzalloc;
  108. }
  109. platform_set_drvdata(pdev, bl);
  110. return 0;
  111. error_devm_kzalloc:
  112. cs5535_mfgpt_free_timer(pwm_timer);
  113. return retval;
  114. }
  115. static int ot200_backlight_remove(struct platform_device *pdev)
  116. {
  117. /* on module unload set brightness to 100% */
  118. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0);
  119. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN);
  120. cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1,
  121. MAX_COMP2 - dim_table[100]);
  122. cs5535_mfgpt_free_timer(pwm_timer);
  123. return 0;
  124. }
  125. static struct platform_driver ot200_backlight_driver = {
  126. .driver = {
  127. .name = "ot200-backlight",
  128. },
  129. .probe = ot200_backlight_probe,
  130. .remove = ot200_backlight_remove,
  131. };
  132. module_platform_driver(ot200_backlight_driver);
  133. MODULE_DESCRIPTION("backlight driver for ot200 visualisation device");
  134. MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>");
  135. MODULE_LICENSE("GPL");
  136. MODULE_ALIAS("platform:ot200-backlight");