pca9551_led.c 3.4 KB

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