mt6370_pmu_bled.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <platform/mt_typedefs.h>
  2. #include <platform/mt_i2c.h>
  3. #include <platform/errno.h>
  4. #include <printf.h>
  5. #include <string.h>
  6. #define MT6370_PMU_BLEN 0xA2
  7. #define MT6370_MASK_PWM 0x80
  8. static struct mt_i2c_t i2c = {
  9. .id = I2C_APPM,
  10. .addr = 0x34,
  11. .mode = HS_MODE,
  12. .speed = 3400,
  13. .pushpull = true,
  14. };
  15. struct mt6370_bled_config_t {
  16. u8 addr;
  17. u8 val;
  18. };
  19. static struct mt6370_bled_config_t mt6370_bled_settings[8] = {
  20. {.addr = 0xA1, .val = 0xed},
  21. {.addr = 0xA2, .val = 0xb4},
  22. {.addr = 0xA3, .val = 0x30},
  23. {.addr = 0xA4, .val = 0x07},
  24. {.addr = 0xA5, .val = 0x3f},
  25. {.addr = 0xA6, .val = 0x00},
  26. {.addr = 0xA7, .val = 0x08},
  27. {.addr = 0xA0, .val = 0x7e}
  28. };
  29. /* ========================= */
  30. /* I2C operations */
  31. /* ========================= */
  32. static int mt6370_i2c_write_byte(struct mt_i2c_t *i2c, u8 cmd, u8 data)
  33. {
  34. unsigned int ret = I2C_OK;
  35. unsigned char write_buf[2] = {cmd, data};
  36. ret = i2c_write(i2c, write_buf, 2);
  37. if (ret != I2C_OK)
  38. dprintf(CRITICAL, "%s: I2CW[0x%02X] = 0x%02X failed, code = %d\n",
  39. __func__, cmd, data, ret);
  40. else
  41. dprintf(INFO, "%s: I2CW[0x%02X] = 0x%02X\n",
  42. __func__, cmd, data);
  43. return ret;
  44. }
  45. static int mt6370_i2c_read_byte(struct mt_i2c_t *i2c, u8 cmd, u8 *data)
  46. {
  47. int ret = I2C_OK;
  48. u8 ret_data = cmd;
  49. ret = i2c_write_read(i2c, &ret_data, 1, 1);
  50. if (ret != I2C_OK)
  51. dprintf(CRITICAL, "%s: I2CR[0x%02X] failed, code = %d\n",
  52. __func__, cmd, ret);
  53. else {
  54. dprintf(INFO, "%s: I2CR[0x%02X] = 0x%02X\n",
  55. __func__, cmd, ret_data);
  56. *data = ret_data;
  57. }
  58. return ret;
  59. }
  60. static int mt6370_i2c_update_bits(struct mt_i2c_t *i2c, u8 cmd,
  61. u8 mask, u8 data)
  62. {
  63. int ret = 0;
  64. u8 reg_data = 0;
  65. ret = mt6370_i2c_read_byte(i2c, cmd, &reg_data);
  66. if (ret != I2C_OK)
  67. return ret;
  68. reg_data = reg_data & 0xFF;
  69. reg_data &= ~mask;
  70. reg_data |= (data & mask);
  71. return mt6370_i2c_write_byte(i2c, cmd, reg_data);
  72. }
  73. static int mt6370_set_pwm_mode(void)
  74. {
  75. return mt6370_i2c_update_bits(&i2c, MT6370_PMU_BLEN, MT6370_MASK_PWM, 0x80);
  76. }
  77. static int mt6370_bled_init_setting(void)
  78. {
  79. int ret = 0;
  80. unsigned int i;
  81. for (i = 0; i < sizeof(mt6370_bled_settings) / sizeof(struct mt6370_bled_config_t); i++) {
  82. ret = mt6370_i2c_write_byte(&i2c, mt6370_bled_settings[i].addr, mt6370_bled_settings[i].val);
  83. if (ret < 0)
  84. return ret;
  85. }
  86. return ret;
  87. }
  88. int mt6370_bled_probe(void)
  89. {
  90. int ret = 0;
  91. ret = mt6370_bled_init_setting();
  92. if (ret < 0)
  93. dprintf(CRITICAL, "%s fail, ret = %d\n", __func__, ret);
  94. return ret;
  95. }