kb3886_bl.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Backlight Driver for the KB3886 Backlight
  4. *
  5. * Copyright (c) 2007-2008 Claudio Nieder
  6. *
  7. * Based on corgi_bl.c by Richard Purdie and kb3886 driver by Robert Woerle
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/mutex.h>
  14. #include <linux/fb.h>
  15. #include <linux/backlight.h>
  16. #include <linux/delay.h>
  17. #include <linux/dmi.h>
  18. #define KB3886_PARENT 0x64
  19. #define KB3886_IO 0x60
  20. #define KB3886_ADC_DAC_PWM 0xC4
  21. #define KB3886_PWM0_WRITE 0x81
  22. #define KB3886_PWM0_READ 0x41
  23. static DEFINE_MUTEX(bl_mutex);
  24. static void kb3886_bl_set_intensity(int intensity)
  25. {
  26. mutex_lock(&bl_mutex);
  27. intensity = intensity&0xff;
  28. outb(KB3886_ADC_DAC_PWM, KB3886_PARENT);
  29. usleep_range(10000, 11000);
  30. outb(KB3886_PWM0_WRITE, KB3886_IO);
  31. usleep_range(10000, 11000);
  32. outb(intensity, KB3886_IO);
  33. mutex_unlock(&bl_mutex);
  34. }
  35. struct kb3886bl_machinfo {
  36. int max_intensity;
  37. int default_intensity;
  38. int limit_mask;
  39. void (*set_bl_intensity)(int intensity);
  40. };
  41. static struct kb3886bl_machinfo kb3886_bl_machinfo = {
  42. .max_intensity = 0xff,
  43. .default_intensity = 0xa0,
  44. .limit_mask = 0x7f,
  45. .set_bl_intensity = kb3886_bl_set_intensity,
  46. };
  47. static struct platform_device kb3886bl_device = {
  48. .name = "kb3886-bl",
  49. .dev = {
  50. .platform_data = &kb3886_bl_machinfo,
  51. },
  52. .id = -1,
  53. };
  54. static struct platform_device *devices[] __initdata = {
  55. &kb3886bl_device,
  56. };
  57. /*
  58. * Back to driver
  59. */
  60. static int kb3886bl_intensity;
  61. static struct backlight_device *kb3886_backlight_device;
  62. static struct kb3886bl_machinfo *bl_machinfo;
  63. static unsigned long kb3886bl_flags;
  64. #define KB3886BL_SUSPENDED 0x01
  65. static const struct dmi_system_id kb3886bl_device_table[] __initconst = {
  66. {
  67. .ident = "Sahara Touch-iT",
  68. .matches = {
  69. DMI_MATCH(DMI_SYS_VENDOR, "SDV"),
  70. DMI_MATCH(DMI_PRODUCT_NAME, "iTouch T201"),
  71. },
  72. },
  73. { }
  74. };
  75. static int kb3886bl_send_intensity(struct backlight_device *bd)
  76. {
  77. int intensity = backlight_get_brightness(bd);
  78. if (kb3886bl_flags & KB3886BL_SUSPENDED)
  79. intensity = 0;
  80. bl_machinfo->set_bl_intensity(intensity);
  81. kb3886bl_intensity = intensity;
  82. return 0;
  83. }
  84. #ifdef CONFIG_PM_SLEEP
  85. static int kb3886bl_suspend(struct device *dev)
  86. {
  87. struct backlight_device *bd = dev_get_drvdata(dev);
  88. kb3886bl_flags |= KB3886BL_SUSPENDED;
  89. backlight_update_status(bd);
  90. return 0;
  91. }
  92. static int kb3886bl_resume(struct device *dev)
  93. {
  94. struct backlight_device *bd = dev_get_drvdata(dev);
  95. kb3886bl_flags &= ~KB3886BL_SUSPENDED;
  96. backlight_update_status(bd);
  97. return 0;
  98. }
  99. #endif
  100. static SIMPLE_DEV_PM_OPS(kb3886bl_pm_ops, kb3886bl_suspend, kb3886bl_resume);
  101. static int kb3886bl_get_intensity(struct backlight_device *bd)
  102. {
  103. return kb3886bl_intensity;
  104. }
  105. static const struct backlight_ops kb3886bl_ops = {
  106. .get_brightness = kb3886bl_get_intensity,
  107. .update_status = kb3886bl_send_intensity,
  108. };
  109. static int kb3886bl_probe(struct platform_device *pdev)
  110. {
  111. struct backlight_properties props;
  112. struct kb3886bl_machinfo *machinfo = dev_get_platdata(&pdev->dev);
  113. bl_machinfo = machinfo;
  114. if (!machinfo->limit_mask)
  115. machinfo->limit_mask = -1;
  116. memset(&props, 0, sizeof(struct backlight_properties));
  117. props.type = BACKLIGHT_RAW;
  118. props.max_brightness = machinfo->max_intensity;
  119. kb3886_backlight_device = devm_backlight_device_register(&pdev->dev,
  120. "kb3886-bl", &pdev->dev,
  121. NULL, &kb3886bl_ops,
  122. &props);
  123. if (IS_ERR(kb3886_backlight_device))
  124. return PTR_ERR(kb3886_backlight_device);
  125. platform_set_drvdata(pdev, kb3886_backlight_device);
  126. kb3886_backlight_device->props.power = FB_BLANK_UNBLANK;
  127. kb3886_backlight_device->props.brightness = machinfo->default_intensity;
  128. backlight_update_status(kb3886_backlight_device);
  129. return 0;
  130. }
  131. static struct platform_driver kb3886bl_driver = {
  132. .probe = kb3886bl_probe,
  133. .driver = {
  134. .name = "kb3886-bl",
  135. .pm = &kb3886bl_pm_ops,
  136. },
  137. };
  138. static int __init kb3886_init(void)
  139. {
  140. if (!dmi_check_system(kb3886bl_device_table))
  141. return -ENODEV;
  142. platform_add_devices(devices, ARRAY_SIZE(devices));
  143. return platform_driver_register(&kb3886bl_driver);
  144. }
  145. static void __exit kb3886_exit(void)
  146. {
  147. platform_driver_unregister(&kb3886bl_driver);
  148. }
  149. module_init(kb3886_init);
  150. module_exit(kb3886_exit);
  151. MODULE_AUTHOR("Claudio Nieder <private@claudio.ch>");
  152. MODULE_DESCRIPTION("Tabletkiosk Sahara Touch-iT Backlight Driver");
  153. MODULE_LICENSE("GPL");
  154. MODULE_ALIAS("dmi:*:svnSDV:pniTouchT201:*");