pmic_spi.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <spi.h>
  32. static struct spi_slave *slave;
  33. void pmic_spi_free(struct spi_slave *slave)
  34. {
  35. if (slave)
  36. spi_free_slave(slave);
  37. }
  38. struct spi_slave *pmic_spi_probe(struct pmic *p)
  39. {
  40. return spi_setup_slave(p->bus,
  41. p->hw.spi.cs,
  42. p->hw.spi.clk,
  43. p->hw.spi.mode);
  44. }
  45. static u32 pmic_reg(struct pmic *p, u32 reg, u32 *val, u32 write)
  46. {
  47. u32 pmic_tx, pmic_rx;
  48. u32 tmp;
  49. if (!slave) {
  50. slave = pmic_spi_probe(p);
  51. if (!slave)
  52. return -1;
  53. }
  54. if (check_reg(reg))
  55. return -1;
  56. if (spi_claim_bus(slave))
  57. return -1;
  58. pmic_tx = p->hw.spi.prepare_tx(reg, val, write);
  59. tmp = cpu_to_be32(pmic_tx);
  60. if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
  61. pmic_spi_flags)) {
  62. spi_release_bus(slave);
  63. return -1;
  64. }
  65. if (write) {
  66. pmic_tx = p->hw.spi.prepare_tx(0, NULL, write);
  67. pmic_tx &= ~(1 << 31);
  68. tmp = cpu_to_be32(pmic_tx);
  69. if (spi_xfer(slave, pmic_spi_bitlen, &tmp, &pmic_rx,
  70. pmic_spi_flags)) {
  71. spi_release_bus(slave);
  72. return -1;
  73. }
  74. }
  75. spi_release_bus(slave);
  76. *val = cpu_to_be32(pmic_rx);
  77. return 0;
  78. }
  79. int pmic_reg_write(struct pmic *p, u32 reg, u32 val)
  80. {
  81. if (pmic_reg(p, reg, &val, 1))
  82. return -1;
  83. return 0;
  84. }
  85. int pmic_reg_read(struct pmic *p, u32 reg, u32 *val)
  86. {
  87. if (pmic_reg(p, reg, val, 0))
  88. return -1;
  89. return 0;
  90. }