3 * Viktar Palstsiuk, Promwad, viktar.palstsiuk@promwad.com
5 * SPDX-License-Identifier: GPL-2.0+
9 * Driver for Semtech SX151x SPI GPIO Expanders
16 #ifndef CONFIG_SX151X_SPI_BUS
17 #define CONFIG_SX151X_SPI_BUS 0
21 * The SX151x registers
24 #ifdef CONFIG_SX151X_GPIO_COUNT_8
26 #define SX151X_REG_DIR 0x07
27 #define SX151X_REG_DATA 0x08
30 #define SX151X_REG_DIR 0x0F
31 #define SX151X_REG_DATA 0x11
33 #define SX151X_REG_RESET 0x7D
35 static int sx151x_spi_write(int chip, unsigned char reg, unsigned char val)
37 struct spi_slave *slave;
41 slave = spi_setup_slave(CONFIG_SX151X_SPI_BUS, chip, 1000000,
51 ret = spi_xfer(slave, 16, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END);
53 printf("spi%d.%d write fail: can't write %02x to %02x: %d\n",
54 CONFIG_SX151X_SPI_BUS, chip, val, reg, ret);
56 printf("spi%d.%d write 0x%02x to register 0x%02x\n",
57 CONFIG_SX151X_SPI_BUS, chip, val, reg);
58 spi_release_bus(slave);
59 spi_free_slave(slave);
64 static int sx151x_spi_read(int chip, unsigned char reg)
66 struct spi_slave *slave;
69 slave = spi_setup_slave(CONFIG_SX151X_SPI_BUS, chip, 1000000,
76 ret = spi_w8r8(slave, reg | 0x80);
78 printf("spi%d.%d read fail: can't read %02x: %d\n",
79 CONFIG_SX151X_SPI_BUS, chip, reg, ret);
81 printf("spi%d.%d read register 0x%02x: 0x%02x\n",
82 CONFIG_SX151X_SPI_BUS, chip, reg, ret);
84 spi_release_bus(slave);
85 spi_free_slave(slave);
90 static inline void sx151x_find_cfg(int gpio, unsigned char *reg, unsigned char *mask)
93 *mask = 1 << (gpio % 8);
96 static int sx151x_write_cfg(int chip, unsigned char gpio, unsigned char reg, int val)
102 sx151x_find_cfg(gpio, ®, &mask);
103 ret = sx151x_spi_read(chip, reg);
109 data |= (val << (gpio % 8)) & mask;
110 return sx151x_spi_write(chip, reg, data);
113 int sx151x_get_value(int chip, int gpio)
115 unsigned char reg = SX151X_REG_DATA;
119 sx151x_find_cfg(gpio, ®, &mask);
120 ret = sx151x_spi_read(chip, reg);
122 ret = (ret & mask) != 0 ? 1 : 0;
127 int sx151x_set_value(int chip, int gpio, int val)
129 return sx151x_write_cfg(chip, gpio, SX151X_REG_DATA, (val ? 1 : 0));
132 int sx151x_direction_input(int chip, int gpio)
134 return sx151x_write_cfg(chip, gpio, SX151X_REG_DIR, 1);
137 int sx151x_direction_output(int chip, int gpio)
139 return sx151x_write_cfg(chip, gpio, SX151X_REG_DIR, 0);
142 int sx151x_reset(int chip)
146 err = sx151x_spi_write(chip, SX151X_REG_RESET, 0x12);
150 err = sx151x_spi_write(chip, SX151X_REG_RESET, 0x34);
154 #ifdef CONFIG_CMD_SX151X
156 int do_sx151x(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
158 int ret = CMD_RET_USAGE, chip = 0, gpio = 0, val = 0;
161 return CMD_RET_USAGE;
163 /* arg2 used as chip number */
164 chip = simple_strtoul(argv[2], NULL, 10);
166 if (strcmp(argv[1], "reset") == 0) {
167 ret = sx151x_reset(chip);
169 printf("Device at spi%d.%d was reset\n",
170 CONFIG_SX151X_SPI_BUS, chip);
176 return CMD_RET_USAGE;
178 /* arg3 used as gpio number */
179 gpio = simple_strtoul(argv[3], NULL, 10);
181 if (strcmp(argv[1], "get") == 0) {
182 ret = sx151x_get_value(chip, gpio);
184 printf("Failed to get value at spi%d.%d gpio %d\n",
185 CONFIG_SX151X_SPI_BUS, chip, gpio);
187 printf("Value at spi%d.%d gpio %d is %d\n",
188 CONFIG_SX151X_SPI_BUS, chip, gpio, ret);
195 return CMD_RET_USAGE;
197 /* arg4 used as value or direction */
198 val = simple_strtoul(argv[4], NULL, 10);
200 if (strcmp(argv[1], "set") == 0) {
201 ret = sx151x_set_value(chip, gpio, val);
203 printf("Failed to set value at spi%d.%d gpio %d\n",
204 CONFIG_SX151X_SPI_BUS, chip, gpio);
206 printf("New value at spi%d.%d gpio %d is %d\n",
207 CONFIG_SX151X_SPI_BUS, chip, gpio, val);
209 } else if (strcmp(argv[1], "dir") == 0) {
211 ret = sx151x_direction_output(chip, gpio);
213 ret = sx151x_direction_input(chip, gpio);
216 printf("Failed to set direction of spi%d.%d gpio %d\n",
217 CONFIG_SX151X_SPI_BUS, chip, gpio);
219 printf("New direction of spi%d.%d gpio %d is %d\n",
220 CONFIG_SX151X_SPI_BUS, chip, gpio, val);
224 printf("Please see usage\n");
230 sx151x, 5, 1, do_sx151x,
231 "sx151x gpio access",
232 "dir chip gpio 0|1\n"
233 " - set gpio direction (0 for output, 1 for input)\n"
234 "sx151x get chip gpio\n"
235 " - get gpio value\n"
236 "sx151x set chip gpio 0|1\n"
237 " - set gpio value\n"
238 "sx151x reset chip\n"
242 #endif /* CONFIG_CMD_SX151X */