Lines Matching +full:spi +full:- +full:gpio
1 // SPDX-License-Identifier: GPL-2.0-only
3 * gpio-max3191x.c - GPIO driver for Maxim MAX3191x industrial serializer
7 * The MAX3191x makes 8 digital 24V inputs available via SPI.
8 * Multiple chips can be daisy-chained, the spec does not impose
11 * Either of two modes is selectable: In 8-bit mode, only the state
13 * In 16-bit mode, an additional status byte is clocked out with
17 * readout of non-faulting chips in the same daisy-chain.
21 * daisy-chain.
23 * If the chips are hardwired to 8-bit mode ("modesel" pulled high),
24 * gpio-pisosr.c can be used alternatively to this driver.
30 * https://datasheets.maximintegrated.com/en/ds/MAX31953-MAX31963.pdf
36 #include <linux/gpio/consumer.h>
37 #include <linux/gpio/driver.h>
39 #include <linux/spi/spi.h>
47 * struct max3191x_chip - max3191x daisy-chain
48 * @gpio: GPIO controller struct
50 * @nchips: number of chips in the daisy-chain
51 * @mode: current mode, 0 for 16-bit, 1 for 8-bit;
52 * for simplicity, all chips in the daisy-chain are assumed
54 * @modesel_pins: GPIO pins to configure modesel of each chip
55 * @fault_pins: GPIO pins to detect fault of each chip
56 * @db0_pins: GPIO pins to configure debounce of each chip
57 * @db1_pins: GPIO pins to configure debounce of each chip
58 * @mesg: SPI message to perform a readout
59 * @xfer: SPI transfer used by @mesg
69 * for simplicity, all chips in the daisy-chain are assumed
73 struct gpio_chip gpio; member
96 static int max3191x_get_direction(struct gpio_chip *gpio, unsigned int offset) in max3191x_get_direction() argument
101 static int max3191x_direction_input(struct gpio_chip *gpio, unsigned int offset) in max3191x_direction_input() argument
106 static int max3191x_direction_output(struct gpio_chip *gpio, in max3191x_direction_output() argument
109 return -EINVAL; in max3191x_direction_output()
112 static void max3191x_set(struct gpio_chip *gpio, unsigned int offset, int value) in max3191x_set() argument
115 static void max3191x_set_multiple(struct gpio_chip *gpio, unsigned long *mask, in max3191x_set_multiple() argument
121 return max3191x->mode == STATUS_BYTE_ENABLED ? 2 : 1; in max3191x_wordlen()
126 struct device *dev = max3191x->gpio.parent; in max3191x_readout_locked()
127 struct spi_device *spi = to_spi_device(dev); in max3191x_readout_locked() local
130 val = spi_sync(spi, &max3191x->mesg); in max3191x_readout_locked()
132 dev_err_ratelimited(dev, "SPI receive error %d\n", val); in max3191x_readout_locked()
136 for (i = 0; i < max3191x->nchips; i++) { in max3191x_readout_locked()
137 if (max3191x->mode == STATUS_BYTE_ENABLED) { in max3191x_readout_locked()
138 u8 in = ((u8 *)max3191x->xfer.rx_buf)[i * 2]; in max3191x_readout_locked()
139 u8 status = ((u8 *)max3191x->xfer.rx_buf)[i * 2 + 1]; in max3191x_readout_locked()
142 __assign_bit(i, max3191x->crc_error, val); in max3191x_readout_locked()
148 __assign_bit(i, max3191x->overtemp, ot); in max3191x_readout_locked()
153 if (!max3191x->ignore_uv) { in max3191x_readout_locked()
155 __assign_bit(i, max3191x->undervolt1, uv1); in max3191x_readout_locked()
161 __assign_bit(i, max3191x->undervolt2, val); in max3191x_readout_locked()
168 if (max3191x->fault_pins && !max3191x->ignore_uv) { in max3191x_readout_locked()
171 (max3191x->fault_pins->ndescs == 1) in max3191x_readout_locked()
172 ? max3191x->fault_pins->desc[0] in max3191x_readout_locked()
173 : max3191x->fault_pins->desc[i]; in max3191x_readout_locked()
178 "GPIO read error %d\n", val); in max3191x_readout_locked()
181 __assign_bit(i, max3191x->fault, val); in max3191x_readout_locked()
195 if (!max3191x->ignore_uv && test_bit(chipnum, max3191x->fault)) in max3191x_chip_is_faulting()
198 if (max3191x->mode == STATUS_BYTE_DISABLED) in max3191x_chip_is_faulting()
201 return test_bit(chipnum, max3191x->crc_error) || in max3191x_chip_is_faulting()
202 test_bit(chipnum, max3191x->overtemp) || in max3191x_chip_is_faulting()
203 (!max3191x->ignore_uv && in max3191x_chip_is_faulting()
204 test_bit(chipnum, max3191x->undervolt1)); in max3191x_chip_is_faulting()
207 static int max3191x_get(struct gpio_chip *gpio, unsigned int offset) in max3191x_get() argument
209 struct max3191x_chip *max3191x = gpiochip_get_data(gpio); in max3191x_get()
213 mutex_lock(&max3191x->lock); in max3191x_get()
220 ret = -EIO; in max3191x_get()
224 in = ((u8 *)max3191x->xfer.rx_buf)[chipnum * wordlen]; in max3191x_get()
228 mutex_unlock(&max3191x->lock); in max3191x_get()
232 static int max3191x_get_multiple(struct gpio_chip *gpio, unsigned long *mask, in max3191x_get_multiple() argument
235 struct max3191x_chip *max3191x = gpiochip_get_data(gpio); in max3191x_get_multiple()
242 mutex_lock(&max3191x->lock); in max3191x_get_multiple()
247 bitmap_zero(bits, gpio->ngpio); in max3191x_get_multiple()
248 for_each_set_clump8(bit, gpio_mask, mask, gpio->ngpio) { in max3191x_get_multiple()
252 ret = -EIO; in max3191x_get_multiple()
256 in = ((u8 *)max3191x->xfer.rx_buf)[chipnum * wordlen]; in max3191x_get_multiple()
262 mutex_unlock(&max3191x->lock); in max3191x_get_multiple()
266 static int max3191x_set_config(struct gpio_chip *gpio, unsigned int offset, in max3191x_set_config() argument
269 struct max3191x_chip *max3191x = gpiochip_get_data(gpio); in max3191x_set_config()
273 return -ENOTSUPP; in max3191x_set_config()
275 if (!max3191x->db0_pins || !max3191x->db1_pins) in max3191x_set_config()
276 return -EINVAL; in max3191x_set_config()
297 return -EINVAL; in max3191x_set_config()
300 if (max3191x->db0_pins->ndescs == 1) in max3191x_set_config()
305 mutex_lock(&max3191x->lock); in max3191x_set_config()
306 gpiod_set_value_cansleep(max3191x->db0_pins->desc[chipnum], db0_val); in max3191x_set_config()
307 gpiod_set_value_cansleep(max3191x->db1_pins->desc[chipnum], db1_val); in max3191x_set_config()
308 mutex_unlock(&max3191x->lock); in max3191x_set_config()
339 if (found == -ENOENT) in devm_gpiod_get_array_optional_count()
343 dev_err(dev, "ignoring %s-gpios: found %d, expected %u or 1\n", in devm_gpiod_get_array_optional_count()
351 dev_err(dev, "failed to get %s-gpios: %ld\n", in devm_gpiod_get_array_optional_count()
359 static int max3191x_probe(struct spi_device *spi) in max3191x_probe() argument
361 struct device *dev = &spi->dev; in max3191x_probe()
367 return -ENOMEM; in max3191x_probe()
368 spi_set_drvdata(spi, max3191x); in max3191x_probe()
370 max3191x->nchips = 1; in max3191x_probe()
371 device_property_read_u32(dev, "#daisy-chained-devices", in max3191x_probe()
372 &max3191x->nchips); in max3191x_probe()
374 n = BITS_TO_LONGS(max3191x->nchips); in max3191x_probe()
375 max3191x->crc_error = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL); in max3191x_probe()
376 max3191x->undervolt1 = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL); in max3191x_probe()
377 max3191x->undervolt2 = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL); in max3191x_probe()
378 max3191x->overtemp = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL); in max3191x_probe()
379 max3191x->fault = devm_kcalloc(dev, n, sizeof(long), GFP_KERNEL); in max3191x_probe()
380 max3191x->xfer.rx_buf = devm_kcalloc(dev, max3191x->nchips, in max3191x_probe()
382 if (!max3191x->crc_error || !max3191x->undervolt1 || in max3191x_probe()
383 !max3191x->overtemp || !max3191x->undervolt2 || in max3191x_probe()
384 !max3191x->fault || !max3191x->xfer.rx_buf) in max3191x_probe()
385 return -ENOMEM; in max3191x_probe()
387 max3191x->modesel_pins = devm_gpiod_get_array_optional_count(dev, in max3191x_probe()
388 "maxim,modesel", GPIOD_ASIS, max3191x->nchips); in max3191x_probe()
389 max3191x->fault_pins = devm_gpiod_get_array_optional_count(dev, in max3191x_probe()
390 "maxim,fault", GPIOD_IN, max3191x->nchips); in max3191x_probe()
391 max3191x->db0_pins = devm_gpiod_get_array_optional_count(dev, in max3191x_probe()
392 "maxim,db0", GPIOD_OUT_LOW, max3191x->nchips); in max3191x_probe()
393 max3191x->db1_pins = devm_gpiod_get_array_optional_count(dev, in max3191x_probe()
394 "maxim,db1", GPIOD_OUT_LOW, max3191x->nchips); in max3191x_probe()
396 max3191x->mode = device_property_read_bool(dev, "maxim,modesel-8bit") in max3191x_probe()
398 if (max3191x->modesel_pins) in max3191x_probe()
400 max3191x->modesel_pins->ndescs, in max3191x_probe()
401 max3191x->modesel_pins->desc, in max3191x_probe()
402 max3191x->modesel_pins->info, max3191x->mode); in max3191x_probe()
404 max3191x->ignore_uv = device_property_read_bool(dev, in max3191x_probe()
405 "maxim,ignore-undervoltage"); in max3191x_probe()
407 if (max3191x->db0_pins && max3191x->db1_pins && in max3191x_probe()
408 max3191x->db0_pins->ndescs != max3191x->db1_pins->ndescs) { in max3191x_probe()
409 dev_err(dev, "ignoring maxim,db*-gpios: array len mismatch\n"); in max3191x_probe()
410 devm_gpiod_put_array(dev, max3191x->db0_pins); in max3191x_probe()
411 devm_gpiod_put_array(dev, max3191x->db1_pins); in max3191x_probe()
412 max3191x->db0_pins = NULL; in max3191x_probe()
413 max3191x->db1_pins = NULL; in max3191x_probe()
416 max3191x->xfer.len = max3191x->nchips * max3191x_wordlen(max3191x); in max3191x_probe()
417 spi_message_init_with_transfers(&max3191x->mesg, &max3191x->xfer, 1); in max3191x_probe()
419 max3191x->gpio.label = spi->modalias; in max3191x_probe()
420 max3191x->gpio.owner = THIS_MODULE; in max3191x_probe()
421 max3191x->gpio.parent = dev; in max3191x_probe()
422 max3191x->gpio.base = -1; in max3191x_probe()
423 max3191x->gpio.ngpio = max3191x->nchips * MAX3191X_NGPIO; in max3191x_probe()
424 max3191x->gpio.can_sleep = true; in max3191x_probe()
426 max3191x->gpio.get_direction = max3191x_get_direction; in max3191x_probe()
427 max3191x->gpio.direction_input = max3191x_direction_input; in max3191x_probe()
428 max3191x->gpio.direction_output = max3191x_direction_output; in max3191x_probe()
429 max3191x->gpio.set = max3191x_set; in max3191x_probe()
430 max3191x->gpio.set_multiple = max3191x_set_multiple; in max3191x_probe()
431 max3191x->gpio.get = max3191x_get; in max3191x_probe()
432 max3191x->gpio.get_multiple = max3191x_get_multiple; in max3191x_probe()
433 max3191x->gpio.set_config = max3191x_set_config; in max3191x_probe()
435 mutex_init(&max3191x->lock); in max3191x_probe()
437 ret = gpiochip_add_data(&max3191x->gpio, max3191x); in max3191x_probe()
439 mutex_destroy(&max3191x->lock); in max3191x_probe()
446 static void max3191x_remove(struct spi_device *spi) in max3191x_remove() argument
448 struct max3191x_chip *max3191x = spi_get_drvdata(spi); in max3191x_remove()
450 gpiochip_remove(&max3191x->gpio); in max3191x_remove()
451 mutex_destroy(&max3191x->lock); in max3191x_remove()
480 MODULE_DEVICE_TABLE(spi, max3191x_spi_id);
494 MODULE_DESCRIPTION("GPIO driver for Maxim MAX3191x industrial serializer");