power_i2c.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011 Samsung Electronics
  4. * Lukasz Majewski <l.majewski@samsung.com>
  5. *
  6. * (C) Copyright 2010
  7. * Stefano Babic, DENX Software Engineering, sbabic@denx.de
  8. *
  9. * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
  10. * (C) Copyright 2019 NXP
  11. */
  12. #include <common.h>
  13. #include <log.h>
  14. #include <linux/types.h>
  15. #include <power/pmic.h>
  16. #include <i2c.h>
  17. #include <linux/compiler.h>
  18. int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
  19. {
  20. unsigned char buf[4] = { 0 };
  21. if (check_reg(p, reg))
  22. return -EINVAL;
  23. #if CONFIG_IS_ENABLED(DM_I2C)
  24. struct udevice *dev;
  25. int ret;
  26. ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
  27. 1, &dev);
  28. if (ret) {
  29. printf("%s: Cannot find udev for a bus %d\n", __func__,
  30. p->bus);
  31. return -ENXIO;
  32. }
  33. #else /* Non DM I2C support - will be removed */
  34. I2C_SET_BUS(p->bus);
  35. #endif
  36. switch (pmic_i2c_tx_num) {
  37. case 3:
  38. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
  39. buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
  40. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  41. buf[0] = cpu_to_le32(val) & 0xff;
  42. } else {
  43. buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
  44. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  45. buf[2] = cpu_to_le32(val) & 0xff;
  46. }
  47. break;
  48. case 2:
  49. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
  50. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  51. buf[0] = cpu_to_le32(val) & 0xff;
  52. } else {
  53. buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
  54. buf[1] = cpu_to_le32(val) & 0xff;
  55. }
  56. break;
  57. case 1:
  58. buf[0] = cpu_to_le32(val) & 0xff;
  59. break;
  60. default:
  61. printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
  62. return -EINVAL;
  63. }
  64. #if CONFIG_IS_ENABLED(DM_I2C)
  65. return dm_i2c_write(dev, reg, buf, pmic_i2c_tx_num);
  66. #else
  67. return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
  68. #endif
  69. }
  70. int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
  71. {
  72. unsigned char buf[4] = { 0 };
  73. u32 ret_val = 0;
  74. int ret;
  75. if (check_reg(p, reg))
  76. return -EINVAL;
  77. #if CONFIG_IS_ENABLED(DM_I2C)
  78. struct udevice *dev;
  79. ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
  80. 1, &dev);
  81. if (ret) {
  82. printf("%s: Cannot find udev for a bus %d\n", __func__,
  83. p->bus);
  84. return -ENXIO;
  85. }
  86. ret = dm_i2c_read(dev, reg, buf, pmic_i2c_tx_num);
  87. #else /* Non DM I2C support - will be removed */
  88. I2C_SET_BUS(p->bus);
  89. ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
  90. #endif
  91. if (ret)
  92. return ret;
  93. switch (pmic_i2c_tx_num) {
  94. case 3:
  95. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
  96. ret_val = le32_to_cpu(buf[2] << 16
  97. | buf[1] << 8 | buf[0]);
  98. else
  99. ret_val = le32_to_cpu(buf[0] << 16 |
  100. buf[1] << 8 | buf[2]);
  101. break;
  102. case 2:
  103. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
  104. ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
  105. else
  106. ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
  107. break;
  108. case 1:
  109. ret_val = le32_to_cpu(buf[0]);
  110. break;
  111. default:
  112. printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
  113. return -EINVAL;
  114. }
  115. memcpy(val, &ret_val, sizeof(ret_val));
  116. return 0;
  117. }
  118. int pmic_probe(struct pmic *p)
  119. {
  120. debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
  121. #if CONFIG_IS_ENABLED(DM_I2C)
  122. struct udevice *dev;
  123. int ret;
  124. ret = i2c_get_chip_for_busnum(p->bus, pmic_i2c_addr,
  125. 1, &dev);
  126. if (ret) {
  127. printf("%s: Cannot find udev for a bus %d\n", __func__,
  128. p->bus);
  129. return -ENXIO;
  130. }
  131. #else /* Non DM I2C support - will be removed */
  132. i2c_set_bus_num(p->bus);
  133. if (i2c_probe(pmic_i2c_addr)) {
  134. printf("Can't find PMIC:%s\n", p->name);
  135. return -ENODEV;
  136. }
  137. #endif
  138. return 0;
  139. }