pandora_bl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Backlight driver for Pandora handheld.
  3. * Pandora uses TWL4030 PWM0 -> TPS61161 combo for control backlight.
  4. * Based on pwm_bl.c
  5. *
  6. * Copyright 2009,2012 Gražvydas Ignotas <notasas@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/delay.h>
  16. #include <linux/fb.h>
  17. #include <linux/backlight.h>
  18. #include <linux/i2c/twl.h>
  19. #include <linux/err.h>
  20. #define TWL_PWM0_ON 0x00
  21. #define TWL_PWM0_OFF 0x01
  22. #define TWL_INTBR_GPBR1 0x0c
  23. #define TWL_INTBR_PMBR1 0x0d
  24. #define TWL_PMBR1_PWM0_MUXMASK 0x0c
  25. #define TWL_PMBR1_PWM0 0x04
  26. #define PWM0_CLK_ENABLE BIT(0)
  27. #define PWM0_ENABLE BIT(2)
  28. /* range accepted by hardware */
  29. #define MIN_VALUE 9
  30. #define MAX_VALUE 63
  31. #define MAX_USER_VALUE (MAX_VALUE - MIN_VALUE)
  32. #define PANDORABL_WAS_OFF BL_CORE_DRIVER1
  33. static int pandora_backlight_update_status(struct backlight_device *bl)
  34. {
  35. int brightness = bl->props.brightness;
  36. u8 r;
  37. if (bl->props.power != FB_BLANK_UNBLANK)
  38. brightness = 0;
  39. if (bl->props.state & BL_CORE_FBBLANK)
  40. brightness = 0;
  41. if (bl->props.state & BL_CORE_SUSPENDED)
  42. brightness = 0;
  43. if ((unsigned int)brightness > MAX_USER_VALUE)
  44. brightness = MAX_USER_VALUE;
  45. if (brightness == 0) {
  46. if (bl->props.state & PANDORABL_WAS_OFF)
  47. goto done;
  48. /* first disable PWM0 output, then clock */
  49. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_GPBR1);
  50. r &= ~PWM0_ENABLE;
  51. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
  52. r &= ~PWM0_CLK_ENABLE;
  53. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
  54. goto done;
  55. }
  56. if (bl->props.state & PANDORABL_WAS_OFF) {
  57. /*
  58. * set PWM duty cycle to max. TPS61161 seems to use this
  59. * to calibrate it's PWM sensitivity when it starts.
  60. */
  61. twl_i2c_write_u8(TWL_MODULE_PWM, MAX_VALUE, TWL_PWM0_OFF);
  62. /* first enable clock, then PWM0 out */
  63. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_GPBR1);
  64. r &= ~PWM0_ENABLE;
  65. r |= PWM0_CLK_ENABLE;
  66. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
  67. r |= PWM0_ENABLE;
  68. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
  69. /*
  70. * TI made it very easy to enable digital control, so easy that
  71. * it often triggers unintentionally and disabes PWM control,
  72. * so wait until 1 wire mode detection window ends.
  73. */
  74. usleep_range(2000, 10000);
  75. }
  76. twl_i2c_write_u8(TWL_MODULE_PWM, MIN_VALUE + brightness, TWL_PWM0_OFF);
  77. done:
  78. if (brightness != 0)
  79. bl->props.state &= ~PANDORABL_WAS_OFF;
  80. else
  81. bl->props.state |= PANDORABL_WAS_OFF;
  82. return 0;
  83. }
  84. static const struct backlight_ops pandora_backlight_ops = {
  85. .options = BL_CORE_SUSPENDRESUME,
  86. .update_status = pandora_backlight_update_status,
  87. };
  88. static int pandora_backlight_probe(struct platform_device *pdev)
  89. {
  90. struct backlight_properties props;
  91. struct backlight_device *bl;
  92. u8 r;
  93. memset(&props, 0, sizeof(props));
  94. props.max_brightness = MAX_USER_VALUE;
  95. props.type = BACKLIGHT_RAW;
  96. bl = devm_backlight_device_register(&pdev->dev, pdev->name, &pdev->dev,
  97. NULL, &pandora_backlight_ops, &props);
  98. if (IS_ERR(bl)) {
  99. dev_err(&pdev->dev, "failed to register backlight\n");
  100. return PTR_ERR(bl);
  101. }
  102. platform_set_drvdata(pdev, bl);
  103. /* 64 cycle period, ON position 0 */
  104. twl_i2c_write_u8(TWL_MODULE_PWM, 0x80, TWL_PWM0_ON);
  105. bl->props.state |= PANDORABL_WAS_OFF;
  106. bl->props.brightness = MAX_USER_VALUE;
  107. backlight_update_status(bl);
  108. /* enable PWM function in pin mux */
  109. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_PMBR1);
  110. r &= ~TWL_PMBR1_PWM0_MUXMASK;
  111. r |= TWL_PMBR1_PWM0;
  112. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_PMBR1);
  113. return 0;
  114. }
  115. static struct platform_driver pandora_backlight_driver = {
  116. .driver = {
  117. .name = "pandora-backlight",
  118. },
  119. .probe = pandora_backlight_probe,
  120. };
  121. module_platform_driver(pandora_backlight_driver);
  122. MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
  123. MODULE_DESCRIPTION("Pandora Backlight Driver");
  124. MODULE_LICENSE("GPL");
  125. MODULE_ALIAS("platform:pandora-backlight");