pandora_bl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/mfd/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. struct pandora_private {
  33. unsigned old_state;
  34. #define PANDORABL_WAS_OFF 1
  35. };
  36. static int pandora_backlight_update_status(struct backlight_device *bl)
  37. {
  38. int brightness = bl->props.brightness;
  39. struct pandora_private *priv = bl_get_data(bl);
  40. u8 r;
  41. if (bl->props.power != FB_BLANK_UNBLANK)
  42. brightness = 0;
  43. if (bl->props.state & BL_CORE_FBBLANK)
  44. brightness = 0;
  45. if (bl->props.state & BL_CORE_SUSPENDED)
  46. brightness = 0;
  47. if ((unsigned int)brightness > MAX_USER_VALUE)
  48. brightness = MAX_USER_VALUE;
  49. if (brightness == 0) {
  50. if (priv->old_state == PANDORABL_WAS_OFF)
  51. goto done;
  52. /* first disable PWM0 output, then clock */
  53. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_GPBR1);
  54. r &= ~PWM0_ENABLE;
  55. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
  56. r &= ~PWM0_CLK_ENABLE;
  57. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
  58. goto done;
  59. }
  60. if (priv->old_state == PANDORABL_WAS_OFF) {
  61. /*
  62. * set PWM duty cycle to max. TPS61161 seems to use this
  63. * to calibrate it's PWM sensitivity when it starts.
  64. */
  65. twl_i2c_write_u8(TWL_MODULE_PWM, MAX_VALUE, TWL_PWM0_OFF);
  66. /* first enable clock, then PWM0 out */
  67. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_GPBR1);
  68. r &= ~PWM0_ENABLE;
  69. r |= PWM0_CLK_ENABLE;
  70. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
  71. r |= PWM0_ENABLE;
  72. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_GPBR1);
  73. /*
  74. * TI made it very easy to enable digital control, so easy that
  75. * it often triggers unintentionally and disabes PWM control,
  76. * so wait until 1 wire mode detection window ends.
  77. */
  78. usleep_range(2000, 10000);
  79. }
  80. twl_i2c_write_u8(TWL_MODULE_PWM, MIN_VALUE + brightness, TWL_PWM0_OFF);
  81. done:
  82. if (brightness != 0)
  83. priv->old_state = 0;
  84. else
  85. priv->old_state = PANDORABL_WAS_OFF;
  86. return 0;
  87. }
  88. static const struct backlight_ops pandora_backlight_ops = {
  89. .options = BL_CORE_SUSPENDRESUME,
  90. .update_status = pandora_backlight_update_status,
  91. };
  92. static int pandora_backlight_probe(struct platform_device *pdev)
  93. {
  94. struct backlight_properties props;
  95. struct backlight_device *bl;
  96. struct pandora_private *priv;
  97. u8 r;
  98. priv = devm_kmalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  99. if (!priv) {
  100. dev_err(&pdev->dev, "failed to allocate driver private data\n");
  101. return -ENOMEM;
  102. }
  103. memset(&props, 0, sizeof(props));
  104. props.max_brightness = MAX_USER_VALUE;
  105. props.type = BACKLIGHT_RAW;
  106. bl = devm_backlight_device_register(&pdev->dev, pdev->name, &pdev->dev,
  107. priv, &pandora_backlight_ops, &props);
  108. if (IS_ERR(bl)) {
  109. dev_err(&pdev->dev, "failed to register backlight\n");
  110. return PTR_ERR(bl);
  111. }
  112. platform_set_drvdata(pdev, bl);
  113. /* 64 cycle period, ON position 0 */
  114. twl_i2c_write_u8(TWL_MODULE_PWM, 0x80, TWL_PWM0_ON);
  115. priv->old_state = PANDORABL_WAS_OFF;
  116. bl->props.brightness = MAX_USER_VALUE;
  117. backlight_update_status(bl);
  118. /* enable PWM function in pin mux */
  119. twl_i2c_read_u8(TWL4030_MODULE_INTBR, &r, TWL_INTBR_PMBR1);
  120. r &= ~TWL_PMBR1_PWM0_MUXMASK;
  121. r |= TWL_PMBR1_PWM0;
  122. twl_i2c_write_u8(TWL4030_MODULE_INTBR, r, TWL_INTBR_PMBR1);
  123. return 0;
  124. }
  125. static struct platform_driver pandora_backlight_driver = {
  126. .driver = {
  127. .name = "pandora-backlight",
  128. },
  129. .probe = pandora_backlight_probe,
  130. };
  131. module_platform_driver(pandora_backlight_driver);
  132. MODULE_AUTHOR("Gražvydas Ignotas <notasas@gmail.com>");
  133. MODULE_DESCRIPTION("Pandora Backlight Driver");
  134. MODULE_LICENSE("GPL");
  135. MODULE_ALIAS("platform:pandora-backlight");