power_i2c.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. */
  11. #include <common.h>
  12. #include <linux/types.h>
  13. #include <power/pmic.h>
  14. #include <i2c.h>
  15. #include <linux/compiler.h>
  16. int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
  17. {
  18. unsigned char buf[4] = { 0 };
  19. if (check_reg(p, reg))
  20. return -EINVAL;
  21. I2C_SET_BUS(p->bus);
  22. switch (pmic_i2c_tx_num) {
  23. case 3:
  24. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
  25. buf[2] = (cpu_to_le32(val) >> 16) & 0xff;
  26. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  27. buf[0] = cpu_to_le32(val) & 0xff;
  28. } else {
  29. buf[0] = (cpu_to_le32(val) >> 16) & 0xff;
  30. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  31. buf[2] = cpu_to_le32(val) & 0xff;
  32. }
  33. break;
  34. case 2:
  35. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG) {
  36. buf[1] = (cpu_to_le32(val) >> 8) & 0xff;
  37. buf[0] = cpu_to_le32(val) & 0xff;
  38. } else {
  39. buf[0] = (cpu_to_le32(val) >> 8) & 0xff;
  40. buf[1] = cpu_to_le32(val) & 0xff;
  41. }
  42. break;
  43. case 1:
  44. buf[0] = cpu_to_le32(val) & 0xff;
  45. break;
  46. default:
  47. printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
  48. return -EINVAL;
  49. }
  50. return i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
  51. }
  52. int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
  53. {
  54. unsigned char buf[4] = { 0 };
  55. u32 ret_val = 0;
  56. int ret;
  57. if (check_reg(p, reg))
  58. return -EINVAL;
  59. I2C_SET_BUS(p->bus);
  60. ret = i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num);
  61. if (ret)
  62. return ret;
  63. switch (pmic_i2c_tx_num) {
  64. case 3:
  65. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
  66. ret_val = le32_to_cpu(buf[2] << 16
  67. | buf[1] << 8 | buf[0]);
  68. else
  69. ret_val = le32_to_cpu(buf[0] << 16 |
  70. buf[1] << 8 | buf[2]);
  71. break;
  72. case 2:
  73. if (p->sensor_byte_order == PMIC_SENSOR_BYTE_ORDER_BIG)
  74. ret_val = le32_to_cpu(buf[1] << 8 | buf[0]);
  75. else
  76. ret_val = le32_to_cpu(buf[0] << 8 | buf[1]);
  77. break;
  78. case 1:
  79. ret_val = le32_to_cpu(buf[0]);
  80. break;
  81. default:
  82. printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
  83. return -EINVAL;
  84. }
  85. memcpy(val, &ret_val, sizeof(ret_val));
  86. return 0;
  87. }
  88. int pmic_probe(struct pmic *p)
  89. {
  90. i2c_set_bus_num(p->bus);
  91. debug("Bus: %d PMIC:%s probed!\n", p->bus, p->name);
  92. if (i2c_probe(pmic_i2c_addr)) {
  93. printf("Can't find PMIC:%s\n", p->name);
  94. return -ENODEV;
  95. }
  96. return 0;
  97. }