// SPDX-License-Identifier: GPL-2.0-only /* * gpio-max3191x.c - GPIO driver for Maxim MAX3191x industrial serializer * * Copyright (C) 2017 KUNBUS GmbH * * The MAX3191x makes 8 digital 24V inputs available via SPI. * Multiple chips can be daisy-chained, the spec does not impose * a limit on the number of chips and neither does this driver. * * Either of two modes is selectable: In 8-bit mode, only the state * of the inputs is clocked out to achieve high readout speeds; * In 16-bit mode, an additional status byte is clocked out with * a CRC and indicator bits for undervoltage and overtemperature. * The driver returns an error instead of potentially bogus data * if any of these fault conditions occur. However it does allow * readout of non-faulting chips in the same daisy-chain. * * MAX3191x supports four debounce settings and the driver is * capable of configuring these differently for each chip in the * daisy-chain. * * If the chips are hardwired to 8-bit mode ("modesel" pulled high), * gpio-pisosr.c can be used alternatively to this driver. * * https://datasheets.maximintegrated.com/en/ds/MAX31910.pdf * https://datasheets.maximintegrated.com/en/ds/MAX31911.pdf * https://datasheets.maximintegrated.com/en/ds/MAX31912.pdf * https://datasheets.maximintegrated.com/en/ds/MAX31913.pdf * https://datasheets.maximintegrated.com/en/ds/MAX31953-MAX31963.pdf */ #include <linux/bitmap.h> #include <linux/bitops.h> #include <linux/crc8.h> #include <linux/gpio/consumer.h> #include <linux/gpio/driver.h> #include <linux/module.h> #include <linux/spi/spi.h> enum max3191x_mode { … }; /** * struct max3191x_chip - max3191x daisy-chain * @gpio: GPIO controller struct * @lock: protects read sequences * @nchips: number of chips in the daisy-chain * @mode: current mode, 0 for 16-bit, 1 for 8-bit; * for simplicity, all chips in the daisy-chain are assumed * to use the same mode * @modesel_pins: GPIO pins to configure modesel of each chip * @fault_pins: GPIO pins to detect fault of each chip * @db0_pins: GPIO pins to configure debounce of each chip * @db1_pins: GPIO pins to configure debounce of each chip * @mesg: SPI message to perform a readout * @xfer: SPI transfer used by @mesg * @crc_error: bitmap signaling CRC error for each chip * @overtemp: bitmap signaling overtemperature alarm for each chip * @undervolt1: bitmap signaling undervoltage alarm for each chip * @undervolt2: bitmap signaling undervoltage warning for each chip * @fault: bitmap signaling assertion of @fault_pins for each chip * @ignore_uv: whether to ignore undervoltage alarms; * set by a device property if the chips are powered through * 5VOUT instead of VCC24V, in which case they will constantly * signal undervoltage; * for simplicity, all chips in the daisy-chain are assumed * to be powered the same way */ struct max3191x_chip { … }; #define MAX3191X_NGPIO … #define MAX3191X_CRC8_POLYNOMIAL … DECLARE_CRC8_TABLE(max3191x_crc8); static int max3191x_get_direction(struct gpio_chip *gpio, unsigned int offset) { … } static int max3191x_direction_input(struct gpio_chip *gpio, unsigned int offset) { … } static int max3191x_direction_output(struct gpio_chip *gpio, unsigned int offset, int value) { … } static void max3191x_set(struct gpio_chip *gpio, unsigned int offset, int value) { … } static void max3191x_set_multiple(struct gpio_chip *gpio, unsigned long *mask, unsigned long *bits) { … } static unsigned int max3191x_wordlen(struct max3191x_chip *max3191x) { … } static int max3191x_readout_locked(struct max3191x_chip *max3191x) { … } static bool max3191x_chip_is_faulting(struct max3191x_chip *max3191x, unsigned int chipnum) { … } static int max3191x_get(struct gpio_chip *gpio, unsigned int offset) { … } static int max3191x_get_multiple(struct gpio_chip *gpio, unsigned long *mask, unsigned long *bits) { … } static int max3191x_set_config(struct gpio_chip *gpio, unsigned int offset, unsigned long config) { … } static void gpiod_set_array_single_value_cansleep(unsigned int ndescs, struct gpio_desc **desc, struct gpio_array *info, int value) { … } static struct gpio_descs *devm_gpiod_get_array_optional_count( struct device *dev, const char *con_id, enum gpiod_flags flags, unsigned int expected) { … } static int max3191x_probe(struct spi_device *spi) { … } static void max3191x_remove(struct spi_device *spi) { … } static int __init max3191x_register_driver(struct spi_driver *sdrv) { … } static const struct of_device_id max3191x_of_id[] = …; MODULE_DEVICE_TABLE(of, max3191x_of_id); static const struct spi_device_id max3191x_spi_id[] = …; MODULE_DEVICE_TABLE(spi, max3191x_spi_id); static struct spi_driver max3191x_driver = …; module_driver(…); MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;