power_spi.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <spi.h>
  16. static struct spi_slave *slave;
  17. static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
  18. {
  19. u32 pmic_tx, pmic_rx;
  20. u32 tmp;
  21. if (!slave) {
  22. slave = spi_setup_slave(p->bus, p->hw.spi.cs, p->hw.spi.clk,
  23. p->hw.spi.mode);
  24. if (!slave)
  25. return -1;
  26. }
  27. if (check_reg(p, reg))
  28. return -1;
  29. if (spi_claim_bus(slave))
  30. return -1;
  31. pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
  32. tmp = cpu_to_be32(pmic_tx);
  33. if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
  34. pmic_spi_flags))
  35. goto err;
  36. if (write) {
  37. pmic_tx = p->hw.spi.prepare_tx(reg, val, 0);
  38. tmp = cpu_to_be32(pmic_tx);
  39. if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
  40. pmic_spi_flags))
  41. goto err;
  42. }
  43. spi_release_bus(slave);
  44. *val = cpu_to_be32(pmic_rx);
  45. return 0;
  46. err:
  47. spi_release_bus(slave);
  48. return -1;
  49. }
  50. int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
  51. {
  52. if (pmic_reg(p, reg, &val, 1))
  53. return -1;
  54. return 0;
  55. }
  56. int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
  57. {
  58. if (pmic_reg(p, reg, val, 0))
  59. return -1;
  60. return 0;
  61. }