1 /*
2  * Copyright (C) 2009
3  * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <spi.h>
9 #include <s6e63d6.h>
10 
11 /*
12  * Each transfer is performed as:
13  * 1. chip-select active
14  * 2. send 8-bit start code
15  * 3. send 16-bit data
16  * 4. chip-select inactive
17  */
send_word(struct s6e63d6 * data,u8 rs,u16 word)18 static int send_word(struct s6e63d6 *data, u8 rs, u16 word)
19 {
20 	/*
21 	 * The start byte looks like (binary):
22 	 * 01110<ID><RS><R/W>
23 	 * RS is 0 for index or 1 for data, and R/W is 0 for write.
24 	 */
25 	u32 buf8 = 0x70 | data->id | (rs & 2);
26 	u32 buf16 = cpu_to_le16(word);
27 	u32 buf_in;
28 	int err;
29 
30 	err = spi_xfer(data->slave, 8, &buf8, &buf_in, SPI_XFER_BEGIN);
31 	if (err)
32 		return err;
33 
34 	return spi_xfer(data->slave, 16, &buf16, &buf_in, SPI_XFER_END);
35 }
36 
37 /* Index and param differ in Register Select bit */
s6e63d6_index(struct s6e63d6 * data,u8 idx)38 int s6e63d6_index(struct s6e63d6 *data, u8 idx)
39 {
40 	return send_word(data, 0, idx);
41 }
42 
s6e63d6_param(struct s6e63d6 * data,u16 param)43 int s6e63d6_param(struct s6e63d6 *data, u16 param)
44 {
45 	return send_word(data, 2, param);
46 }
47 
s6e63d6_init(struct s6e63d6 * data)48 int s6e63d6_init(struct s6e63d6 *data)
49 {
50 	if (data->id != 0 && data->id != 4) {
51 		printf("s6e63d6: invalid ID %u\n", data->id);
52 		return 1;
53 	}
54 
55 	data->slave = spi_setup_slave(data->bus, data->cs, 100000, SPI_MODE_3);
56 	if (!data->slave)
57 		return 1;
58 
59 	return 0;
60 }
61