power_i2c.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics
  3. * Lukasz Majewski <l.majewski@samsung.com>
  4. *
  5. * (C) Copyright 2010
  6. * Stefano Babic, DENX Software Engineering, sbabic@denx.de
  7. *
  8. * (C) Copyright 2008-2009 Freescale Semiconductor, Inc.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <linux/types.h>
  14. #include <power/pmic.h>
  15. #include <i2c.h>
  16. #include <linux/compiler.h>
  17. int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
  18. {
  19. unsigned char buf[4] = { 0 };
  20. if (check_reg(p, reg))
  21. return -1;
  22. I2C_SET_BUS(p->bus);
  23. switch (pmic_i2c_tx_num) {
  24. case 3:
  25. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
  26. buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
  27. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  28. buf[0] = cpu_to_le32(val) & 0xff;
  29. } else {
  30. buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
  31. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  32. buf[2] = cpu_to_le32(val) & 0xff;
  33. }
  34. break;
  35. case 2:
  36. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
  37. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  38. buf[0] = cpu_to_le32(val) & 0xff;
  39. } else {
  40. buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
  41. buf[1] = cpu_to_le32(val) & 0xff;
  42. }
  43. break;
  44. case 1:
  45. buf[0] = cpu_to_le32(val) & 0xff;
  46. break;
  47. default:
  48. printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
  49. return -1;
  50. }
  51. if (i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
  52. return -1;
  53. return 0;
  54. }
  55. int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
  56. {
  57. unsigned char buf[4] = { 0 };
  58. u32 ret_val = 0;
  59. if (check_reg(p, reg))
  60. return -1;
  61. I2C_SET_BUS(p->bus);
  62. if (i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
  63. return -1;
  64. switch (pmic_i2c_tx_num) {
  65. case 3:
  66. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
  67. ret_val = le32_to_cpu(buf[2] << 16
  68. | buf[1] << 8 | buf[0]);
  69. else
  70. ret_val = le32_to_cpu(buf[0] << 16 |
  71. buf[1] << 8 | buf[2]);
  72. break;
  73. case 2:
  74. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
  75. ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
  76. else
  77. ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
  78. break;
  79. case 1:
  80. ret_val = le32_to_cpu(buf[0]);
  81. break;
  82. default:
  83. printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
  84. return -1;
  85. }
  86. memcpy(val, &ret_val, sizeof(ret_val));
  87. return 0;
  88. }
  89. int pmic_probe(struct pmic *p)
  90. {
  91. i2c_set_bus_num(p->bus);
  92. debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
  93. if (i2c_probe(pmic_i2c_addr)) {
  94. printf("Can't find PMIC:%s\n", p->name);
  95. return -1;
  96. }
  97. return 0;
  98. }