s6e63d6.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2009
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <spi.h>
  25. #include <s6e63d6.h>
  26. /*
  27. * Each transfer is performed as:
  28. * 1. chip-select active
  29. * 2. send 8-bit start code
  30. * 3. send 16-bit data
  31. * 4. chip-select inactive
  32. */
  33. static int send_word(struct s6e63d6 *data, u8 rs, u16 word)
  34. {
  35. /*
  36. * The start byte looks like (binary):
  37. * 01110<ID><RS><R/W>
  38. * RS is 0 for index or 1 for data, and R/W is 0 for write.
  39. */
  40. u32 buf8 = 0x70 | data->id | (rs & 2);
  41. u32 buf16 = cpu_to_le16(word);
  42. u32 buf_in;
  43. int err;
  44. err = spi_xfer(data->slave, 8, &buf8, &buf_in, SPI_XFER_BEGIN);
  45. if (err)
  46. return err;
  47. return spi_xfer(data->slave, 16, &buf16, &buf_in, SPI_XFER_END);
  48. }
  49. /* Index and param differ in Register Select bit */
  50. int s6e63d6_index(struct s6e63d6 *data, u8 idx)
  51. {
  52. return send_word(data, 0, idx);
  53. }
  54. int s6e63d6_param(struct s6e63d6 *data, u16 param)
  55. {
  56. return send_word(data, 2, param);
  57. }
  58. int s6e63d6_init(struct s6e63d6 *data)
  59. {
  60. if (data->id != 0 && data->id != 4) {
  61. printf("s6e63d6: invalid ID %u\n", data->id);
  62. return 1;
  63. }
  64. data->slave = spi_setup_slave(data->bus, data->cs, 100000, SPI_MODE_3);
  65. if (!data->slave)
  66. return 1;
  67. return 0;
  68. }