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 /*
28  * Each transfer is performed as:
29  * 1. chip-select active
30  * 2. send 8-bit start code
31  * 3. send 16-bit data
32  * 4. chip-select inactive
33  */
send_word(struct s6e63d6 * data,u8 rs,u16 word)34 static int send_word(struct s6e63d6 *data, u8 rs, u16 word)
35 {
36 	/*
37 	 * The start byte looks like (binary):
38 	 * 01110<ID><RS><R/W>
39 	 * RS is 0 for index or 1 for data, and R/W is 0 for write.
40 	 */
41 	u32 buf8 = 0x70 | data->id | (rs & 2);
42 	u32 buf16 = cpu_to_le16(word);
43 	u32 buf_in;
44 	int err;
45 
46 	err = spi_xfer(data->slave, 8, &buf8, &buf_in, SPI_XFER_BEGIN);
47 	if (err)
48 		return err;
49 
50 	return spi_xfer(data->slave, 16, &buf16, &buf_in, SPI_XFER_END);
51 }
52 
53 /* Index and param differ in Register Select bit */
s6e63d6_index(struct s6e63d6 * data,u8 idx)54 int s6e63d6_index(struct s6e63d6 *data, u8 idx)
55 {
56 	return send_word(data, 0, idx);
57 }
58 
s6e63d6_param(struct s6e63d6 * data,u16 param)59 int s6e63d6_param(struct s6e63d6 *data, u16 param)
60 {
61 	return send_word(data, 2, param);
62 }
63 
s6e63d6_init(struct s6e63d6 * data)64 int s6e63d6_init(struct s6e63d6 *data)
65 {
66 	if (data->id != 0 && data->id != 4) {
67 		printf("s6e63d6: invalid ID %u\n", data->id);
68 		return 1;
69 	}
70 
71 	data->slave = spi_setup_slave(data->bus, data->cs, 100000, SPI_MODE_3);
72 	if (!data->slave)
73 		return 1;
74 
75 	return 0;
76 }
77