twl6030-pwm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * twl6030_pwm.c
  3. * Driver for PHOENIX (TWL6030) Pulse Width Modulator
  4. *
  5. * Copyright (C) 2010 Texas Instruments
  6. * Author: Hemanth V <hemanthv@ti.com>
  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. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/i2c/twl.h>
  23. #include <linux/slab.h>
  24. #define LED_PWM_CTRL1 0xF4
  25. #define LED_PWM_CTRL2 0xF5
  26. /* Max value for CTRL1 register */
  27. #define PWM_CTRL1_MAX 255
  28. /* Pull down disable */
  29. #define PWM_CTRL2_DIS_PD (1 << 6)
  30. /* Current control 2.5 milli Amps */
  31. #define PWM_CTRL2_CURR_02 (2 << 4)
  32. /* LED supply source */
  33. #define PWM_CTRL2_SRC_VAC (1 << 2)
  34. /* LED modes */
  35. #define PWM_CTRL2_MODE_HW (0 << 0)
  36. #define PWM_CTRL2_MODE_SW (1 << 0)
  37. #define PWM_CTRL2_MODE_DIS (2 << 0)
  38. #define PWM_CTRL2_MODE_MASK 0x3
  39. struct pwm_device {
  40. const char *label;
  41. unsigned int pwm_id;
  42. };
  43. int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
  44. {
  45. u8 duty_cycle;
  46. int ret;
  47. if (pwm == NULL || period_ns == 0 || duty_ns > period_ns)
  48. return -EINVAL;
  49. duty_cycle = (duty_ns * PWM_CTRL1_MAX) / period_ns;
  50. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, duty_cycle, LED_PWM_CTRL1);
  51. if (ret < 0) {
  52. pr_err("%s: Failed to configure PWM, Error %d\n",
  53. pwm->label, ret);
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. EXPORT_SYMBOL(pwm_config);
  59. int pwm_enable(struct pwm_device *pwm)
  60. {
  61. u8 val;
  62. int ret;
  63. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2);
  64. if (ret < 0) {
  65. pr_err("%s: Failed to enable PWM, Error %d\n", pwm->label, ret);
  66. return ret;
  67. }
  68. /* Change mode to software control */
  69. val &= ~PWM_CTRL2_MODE_MASK;
  70. val |= PWM_CTRL2_MODE_SW;
  71. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, LED_PWM_CTRL2);
  72. if (ret < 0) {
  73. pr_err("%s: Failed to enable PWM, Error %d\n", pwm->label, ret);
  74. return ret;
  75. }
  76. twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2);
  77. return 0;
  78. }
  79. EXPORT_SYMBOL(pwm_enable);
  80. void pwm_disable(struct pwm_device *pwm)
  81. {
  82. u8 val;
  83. int ret;
  84. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, LED_PWM_CTRL2);
  85. if (ret < 0) {
  86. pr_err("%s: Failed to disable PWM, Error %d\n",
  87. pwm->label, ret);
  88. return;
  89. }
  90. val &= ~PWM_CTRL2_MODE_MASK;
  91. val |= PWM_CTRL2_MODE_HW;
  92. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, LED_PWM_CTRL2);
  93. if (ret < 0) {
  94. pr_err("%s: Failed to disable PWM, Error %d\n",
  95. pwm->label, ret);
  96. return;
  97. }
  98. return;
  99. }
  100. EXPORT_SYMBOL(pwm_disable);
  101. struct pwm_device *pwm_request(int pwm_id, const char *label)
  102. {
  103. u8 val;
  104. int ret;
  105. struct pwm_device *pwm;
  106. pwm = kzalloc(sizeof(struct pwm_device), GFP_KERNEL);
  107. if (pwm == NULL) {
  108. pr_err("%s: failed to allocate memory\n", label);
  109. return NULL;
  110. }
  111. pwm->label = label;
  112. pwm->pwm_id = pwm_id;
  113. /* Configure PWM */
  114. val = PWM_CTRL2_DIS_PD | PWM_CTRL2_CURR_02 | PWM_CTRL2_SRC_VAC |
  115. PWM_CTRL2_MODE_HW;
  116. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, LED_PWM_CTRL2);
  117. if (ret < 0) {
  118. pr_err("%s: Failed to configure PWM, Error %d\n",
  119. pwm->label, ret);
  120. kfree(pwm);
  121. return NULL;
  122. }
  123. return pwm;
  124. }
  125. EXPORT_SYMBOL(pwm_request);
  126. void pwm_free(struct pwm_device *pwm)
  127. {
  128. pwm_disable(pwm);
  129. kfree(pwm);
  130. }
  131. EXPORT_SYMBOL(pwm_free);
  132. MODULE_LICENSE("GPL");