Blame view

bootloader/u-boot_2015_04/drivers/gpio/sx151x.c 5.09 KB
6b13f685e   김민수   BSP 최초 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  /*
   * (C) Copyright 2013
   * Viktar Palstsiuk, Promwad, viktar.palstsiuk@promwad.com
   *
   * SPDX-License-Identifier:	GPL-2.0+
   */
  
  /*
   * Driver for Semtech SX151x SPI GPIO Expanders
   */
  
  #include <common.h>
  #include <spi.h>
  #include <sx151x.h>
  
  #ifndef CONFIG_SX151X_SPI_BUS
  #define CONFIG_SX151X_SPI_BUS 0
  #endif
  
  /*
   * The SX151x registers
   */
  
  #ifdef CONFIG_SX151X_GPIO_COUNT_8
  /* 8bit: SX1511 */
  #define SX151X_REG_DIR		0x07
  #define SX151X_REG_DATA		0x08
  #else
  /* 16bit: SX1512 */
  #define SX151X_REG_DIR		0x0F
  #define SX151X_REG_DATA		0x11
  #endif
  #define SX151X_REG_RESET	0x7D
  
  static int sx151x_spi_write(int chip, unsigned char reg, unsigned char val)
  {
  	struct spi_slave *slave;
  	unsigned char buf[2];
  	int ret;
  
  	slave = spi_setup_slave(CONFIG_SX151X_SPI_BUS, chip, 1000000,
  				SPI_MODE_0);
  	if (!slave)
  		return 0;
  
  	spi_claim_bus(slave);
  
  	buf[0] = reg;
  	buf[1] = val;
  
  	ret = spi_xfer(slave, 16, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END);
  	if (ret < 0)
  		printf("spi%d.%d write fail: can't write %02x to %02x: %d
  ",
  			CONFIG_SX151X_SPI_BUS, chip, val, reg, ret);
  	else
  		printf("spi%d.%d write 0x%02x to register 0x%02x
  ",
  		       CONFIG_SX151X_SPI_BUS, chip, val, reg);
  	spi_release_bus(slave);
  	spi_free_slave(slave);
  
  	return ret;
  }
  
  static int sx151x_spi_read(int chip, unsigned char reg)
  {
  	struct spi_slave *slave;
  	int ret;
  
  	slave = spi_setup_slave(CONFIG_SX151X_SPI_BUS, chip, 1000000,
  				SPI_MODE_0);
  	if (!slave)
  		return 0;
  
  	spi_claim_bus(slave);
  
  	ret = spi_w8r8(slave, reg | 0x80);
  	if (ret < 0)
  		printf("spi%d.%d read fail: can't read %02x: %d
  ",
  			CONFIG_SX151X_SPI_BUS, chip, reg, ret);
  	else
  		printf("spi%d.%d read register 0x%02x: 0x%02x
  ",
  		       CONFIG_SX151X_SPI_BUS, chip, reg, ret);
  
  	spi_release_bus(slave);
  	spi_free_slave(slave);
  
  	return ret;
  }
  
  static inline void sx151x_find_cfg(int gpio, unsigned char *reg, unsigned char *mask)
  {
  	*reg   -= gpio / 8;
  	*mask   = 1 << (gpio % 8);
  }
  
  static int sx151x_write_cfg(int chip, unsigned char gpio, unsigned char reg, int val)
  {
  	unsigned char  mask;
  	unsigned char  data;
  	int ret;
  
  	sx151x_find_cfg(gpio, &reg, &mask);
  	ret = sx151x_spi_read(chip, reg);
  	if (ret < 0)
  		return ret;
  	else
  		data = ret;
  	data &= ~mask;
  	data |= (val << (gpio % 8)) & mask;
  	return sx151x_spi_write(chip, reg, data);
  }
  
  int sx151x_get_value(int chip, int gpio)
  {
  	unsigned char  reg = SX151X_REG_DATA;
  	unsigned char  mask;
  	int ret;
  
  	sx151x_find_cfg(gpio, &reg, &mask);
  	ret = sx151x_spi_read(chip, reg);
  	if (ret >= 0)
  		ret = (ret & mask) != 0 ? 1 : 0;
  
  	return ret;
  }
  
  int sx151x_set_value(int chip, int gpio, int val)
  {
  	return sx151x_write_cfg(chip, gpio, SX151X_REG_DATA, (val ? 1 : 0));
  }
  
  int sx151x_direction_input(int chip, int gpio)
  {
  	return sx151x_write_cfg(chip, gpio, SX151X_REG_DIR, 1);
  }
  
  int sx151x_direction_output(int chip, int gpio)
  {
  	return sx151x_write_cfg(chip, gpio, SX151X_REG_DIR, 0);
  }
  
  int sx151x_reset(int chip)
  {
  	int err;
  
  	err = sx151x_spi_write(chip, SX151X_REG_RESET, 0x12);
  	if (err < 0)
  		return err;
  
  	err = sx151x_spi_write(chip, SX151X_REG_RESET, 0x34);
  	return err;
  }
  
  #ifdef CONFIG_CMD_SX151X
  
  int do_sx151x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  {
  	int ret = CMD_RET_USAGE, chip = 0, gpio = 0, val = 0;
  
  	if (argc < 3)
  		return CMD_RET_USAGE;
  
  	/* arg2 used as chip number */
  	chip = simple_strtoul(argv[2], NULL, 10);
  
  	if (strcmp(argv[1], "reset") == 0) {
  		ret = sx151x_reset(chip);
  		if (!ret) {
  			printf("Device at spi%d.%d was reset
  ",
  			       CONFIG_SX151X_SPI_BUS, chip);
  		}
  		return ret;
  	}
  
  	if (argc < 4)
  		return CMD_RET_USAGE;
  
  	/* arg3 used as gpio number */
  	gpio = simple_strtoul(argv[3], NULL, 10);
  
  	if (strcmp(argv[1], "get") == 0) {
  		ret = sx151x_get_value(chip, gpio);
  		if (ret < 0)
  			printf("Failed to get value at spi%d.%d gpio %d
  ",
  			       CONFIG_SX151X_SPI_BUS, chip, gpio);
  		else {
  			printf("Value at spi%d.%d gpio %d is %d
  ",
  			       CONFIG_SX151X_SPI_BUS, chip, gpio, ret);
  			ret = 0;
  		}
  		return ret;
  	}
  
  	if (argc < 5)
  		return CMD_RET_USAGE;
  
  	/* arg4 used as value or direction */
  	val = simple_strtoul(argv[4], NULL, 10);
  
  	if (strcmp(argv[1], "set") == 0) {
  		ret = sx151x_set_value(chip, gpio, val);
  		if (ret < 0)
  			printf("Failed to set value at spi%d.%d gpio %d
  ",
  			       CONFIG_SX151X_SPI_BUS, chip, gpio);
  		else
  			printf("New value at spi%d.%d gpio %d is %d
  ",
  			       CONFIG_SX151X_SPI_BUS, chip, gpio, val);
  		return ret;
  	} else if (strcmp(argv[1], "dir") == 0) {
  		if (val == 0)
  			ret = sx151x_direction_output(chip, gpio);
  		else
  			ret = sx151x_direction_input(chip, gpio);
  
  		if (ret < 0)
  			printf("Failed to set direction of spi%d.%d gpio %d
  ",
  			       CONFIG_SX151X_SPI_BUS, chip, gpio);
  		else
  			printf("New direction of spi%d.%d gpio %d is %d
  ",
  			       CONFIG_SX151X_SPI_BUS, chip, gpio, val);
  		return ret;
  	}
  
  	printf("Please see usage
  ");
  
  	return ret;
  }
  
  U_BOOT_CMD(
  	sx151x,	5,	1,	do_sx151x,
  	"sx151x gpio access",
  	"dir chip gpio 0|1
  "
  	"	- set gpio direction (0 for output, 1 for input)
  "
  	"sx151x get chip gpio
  "
  	"	- get gpio value
  "
  	"sx151x set chip gpio 0|1
  "
  	"	- set gpio value
  "
  	"sx151x reset chip
  "
  	"	- reset chip"
  );
  
  #endif /* CONFIG_CMD_SX151X */