pca9551_led.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Stefan Roese <sr@denx.de>
  4. */
  5. #include <common.h>
  6. #include <errno.h>
  7. #include <i2c.h>
  8. #include <status_led.h>
  9. #ifndef CONFIG_PCA9551_I2C_ADDR
  10. #error "CONFIG_PCA9551_I2C_ADDR not defined!"
  11. #endif
  12. #define PCA9551_REG_INPUT 0x00 /* Input register (read only) */
  13. #define PCA9551_REG_PSC0 0x01 /* Frequency prescaler 0 */
  14. #define PCA9551_REG_PWM0 0x02 /* PWM0 */
  15. #define PCA9551_REG_PSC1 0x03 /* Frequency prescaler 1 */
  16. #define PCA9551_REG_PWM1 0x04 /* PWM1 */
  17. #define PCA9551_REG_LS0 0x05 /* LED0 to LED3 selector */
  18. #define PCA9551_REG_LS1 0x06 /* LED4 to LED7 selector */
  19. #define PCA9551_CTRL_AI (1 << 4) /* Auto-increment flag */
  20. #define PCA9551_LED_STATE_ON 0x00
  21. #define PCA9551_LED_STATE_OFF 0x01
  22. #define PCA9551_LED_STATE_BLINK0 0x02
  23. #define PCA9551_LED_STATE_BLINK1 0x03
  24. struct pca9551_blink_rate {
  25. u8 psc; /* Frequency preescaler, see PCA9551_7.pdf p. 6 */
  26. u8 pwm; /* Pulse width modulation, see PCA9551_7.pdf p. 6 */
  27. };
  28. static int freq_last = -1;
  29. static int mask_last = -1;
  30. static int idx_last = -1;
  31. static int mode_last;
  32. static int pca9551_led_get_state(int led, int *state)
  33. {
  34. unsigned int reg;
  35. u8 shift, buf;
  36. int ret;
  37. if (led < 0 || led > 7) {
  38. return -EINVAL;
  39. } else if (led < 4) {
  40. reg = PCA9551_REG_LS0;
  41. shift = led << 1;
  42. } else {
  43. reg = PCA9551_REG_LS1;
  44. shift = (led - 4) << 1;
  45. }
  46. ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
  47. if (ret)
  48. return ret;
  49. *state = (buf >> shift) & 0x03;
  50. return 0;
  51. }
  52. static int pca9551_led_set_state(int led, int state)
  53. {
  54. unsigned int reg;
  55. u8 shift, buf, mask;
  56. int ret;
  57. if (led < 0 || led > 7) {
  58. return -EINVAL;
  59. } else if (led < 4) {
  60. reg = PCA9551_REG_LS0;
  61. shift = led << 1;
  62. } else {
  63. reg = PCA9551_REG_LS1;
  64. shift = (led - 4) << 1;
  65. }
  66. mask = 0x03 << shift;
  67. ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
  68. if (ret)
  69. return ret;
  70. buf = (buf & ~mask) | ((state & 0x03) << shift);
  71. ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
  72. if (ret)
  73. return ret;
  74. return 0;
  75. }
  76. static int pca9551_led_set_blink_rate(int idx, struct pca9551_blink_rate rate)
  77. {
  78. unsigned int reg;
  79. int ret;
  80. switch (idx) {
  81. case 0:
  82. reg = PCA9551_REG_PSC0;
  83. break;
  84. case 1:
  85. reg = PCA9551_REG_PSC1;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. reg |= PCA9551_CTRL_AI;
  91. ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, (u8 *)&rate, 2);
  92. if (ret)
  93. return ret;
  94. return 0;
  95. }
  96. /*
  97. * Functions referenced by cmd_led.c or status_led.c
  98. */
  99. void __led_init(led_id_t id, int state)
  100. {
  101. }
  102. void __led_set(led_id_t mask, int state)
  103. {
  104. if (state == CONFIG_LED_STATUS_OFF)
  105. pca9551_led_set_state(mask, PCA9551_LED_STATE_OFF);
  106. else
  107. pca9551_led_set_state(mask, PCA9551_LED_STATE_ON);
  108. }
  109. void __led_toggle(led_id_t mask)
  110. {
  111. int state = 0;
  112. pca9551_led_get_state(mask, &state);
  113. pca9551_led_set_state(mask, !state);
  114. }
  115. void __led_blink(led_id_t mask, int freq)
  116. {
  117. struct pca9551_blink_rate rate;
  118. int mode;
  119. int idx;
  120. if ((freq == freq_last) || (mask == mask_last)) {
  121. idx = idx_last;
  122. mode = mode_last;
  123. } else {
  124. /* Toggle blink index */
  125. if (idx_last == 0) {
  126. idx = 1;
  127. mode = PCA9551_LED_STATE_BLINK1;
  128. } else {
  129. idx = 0;
  130. mode = PCA9551_LED_STATE_BLINK0;
  131. }
  132. idx_last = idx;
  133. mode_last = mode;
  134. }
  135. freq_last = freq;
  136. mask_last = mask;
  137. rate.psc = ((freq * 38) / 1000) - 1;
  138. rate.pwm = 128; /* 50% duty cycle */
  139. pca9551_led_set_blink_rate(idx, rate);
  140. pca9551_led_set_state(mask, mode);
  141. }