power_spi.c 1.3 KB

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