pmic_i2c.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. */
  28. #include <common.h>
  29. #include <linux/types.h>
  30. #include <pmic.h>
  31. #include <i2c.h>
  32. int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
  33. {
  34. unsigned char buf[4] = { 0 };
  35. if (check_reg(reg))
  36. return -1;
  37. switch (pmic_i2c_tx_num) {
  38. case 3:
  39. buf[0] = (val >> 16) & 0xff;
  40. buf[1] = (val >> 8) & 0xff;
  41. buf[2] = val & 0xff;
  42. break;
  43. case 1:
  44. buf[0] = val & 0xff;
  45. break;
  46. default:
  47. printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
  48. return -1;
  49. }
  50. if (i2c_write(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
  51. return -1;
  52. return 0;
  53. }
  54. int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
  55. {
  56. unsigned char buf[4] = { 0 };
  57. u32 ret_val = 0;
  58. if (check_reg(reg))
  59. return -1;
  60. if (i2c_read(pmic_i2c_addr, reg, 1, buf, pmic_i2c_tx_num))
  61. return -1;
  62. switch (pmic_i2c_tx_num) {
  63. case 3:
  64. ret_val = buf[0] << 16 | buf[1] << 8 | buf[2];
  65. break;
  66. case 1:
  67. ret_val = buf[0];
  68. break;
  69. default:
  70. printf("%s: invalid tx_num: %d", __func__, pmic_i2c_tx_num);
  71. return -1;
  72. }
  73. memcpy(val, &ret_val, sizeof(ret_val));
  74. return 0;
  75. }
  76. int pmic_probe(struct pmic *p)
  77. {
  78. I2C_SET_BUS(p->bus);
  79. debug("PMIC:%s probed!\n", p->name);
  80. if (i2c_probe(pmic_i2c_addr)) {
  81. printf("Can't find PMIC:%s\n", p->name);
  82. return -1;
  83. }
  84. return 0;
  85. }