pca9551_led.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2015 Stefan Roese <sr@denx.de>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <i2c.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 freq0, freq1;
  29. static int pca9551_led_get_state(int led, int *state)
  30. {
  31. unsigned int reg;
  32. u8 shift, buf;
  33. int ret;
  34. if (led < 0 || led > 7) {
  35. return -EINVAL;
  36. } else if (led < 4) {
  37. reg = PCA9551_REG_LS0;
  38. shift = led << 1;
  39. } else {
  40. reg = PCA9551_REG_LS1;
  41. shift = (led - 4) << 1;
  42. }
  43. ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
  44. if (ret)
  45. return ret;
  46. *state = (buf >> shift) & 0x03;
  47. return 0;
  48. }
  49. static int pca9551_led_set_state(int led, int state)
  50. {
  51. unsigned int reg;
  52. u8 shift, buf, mask;
  53. int ret;
  54. if (led < 0 || led > 7) {
  55. return -EINVAL;
  56. } else if (led < 4) {
  57. reg = PCA9551_REG_LS0;
  58. shift = led << 1;
  59. } else {
  60. reg = PCA9551_REG_LS1;
  61. shift = (led - 4) << 1;
  62. }
  63. mask = 0x03 << shift;
  64. ret = i2c_read(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
  65. if (ret)
  66. return ret;
  67. buf = (buf & ~mask) | ((state & 0x03) << shift);
  68. ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, &buf, 1);
  69. if (ret)
  70. return ret;
  71. return 0;
  72. }
  73. static int pca9551_led_set_blink_rate(int idx, struct pca9551_blink_rate rate)
  74. {
  75. unsigned int reg;
  76. int ret;
  77. switch (idx) {
  78. case 0:
  79. reg = PCA9551_REG_PSC0;
  80. break;
  81. case 1:
  82. reg = PCA9551_REG_PSC1;
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. reg |= PCA9551_CTRL_AI;
  88. ret = i2c_write(CONFIG_PCA9551_I2C_ADDR, reg, 1, (u8 *)&rate, 2);
  89. if (ret)
  90. return ret;
  91. return 0;
  92. }
  93. /*
  94. * Functions referenced by cmd_led.c
  95. */
  96. void __led_set(led_id_t mask, int state)
  97. {
  98. if (state == STATUS_LED_OFF)
  99. pca9551_led_set_state(mask, PCA9551_LED_STATE_OFF);
  100. else
  101. pca9551_led_set_state(mask, PCA9551_LED_STATE_ON);
  102. }
  103. void __led_toggle(led_id_t mask)
  104. {
  105. int state = 0;
  106. pca9551_led_get_state(mask, &state);
  107. pca9551_led_set_state(mask, !state);
  108. }
  109. void __led_blink(led_id_t mask, int freq)
  110. {
  111. struct pca9551_blink_rate rate;
  112. int mode;
  113. int blink;
  114. if ((freq0 == 0) || (freq == freq0)) {
  115. blink = 0;
  116. mode = PCA9551_LED_STATE_BLINK0;
  117. freq0 = freq;
  118. } else {
  119. blink = 1;
  120. mode = PCA9551_LED_STATE_BLINK1;
  121. freq1 = freq;
  122. }
  123. rate.psc = ((freq * 38) / 1000) - 1;
  124. rate.pwm = 128; /* 50% duty cycle */
  125. pca9551_led_set_blink_rate(blink, rate);
  126. pca9551_led_set_state(mask, mode);
  127. }