1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com/ 4 * Andrew F. Davis <afd@ti.com> 5 */ 6 7 #include <linux/bitmap.h> 8 #include <linux/bitops.h> 9 #include <linux/delay.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/gpio/driver.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/spi/spi.h> 15 16 #define DEFAULT_NGPIO 8 17 18 /** 19 * struct pisosr_gpio - GPIO driver data 20 * @chip: GPIO controller chip 21 * @spi: SPI device pointer 22 * @buffer: Buffer for device reads 23 * @buffer_size: Size of buffer 24 * @load_gpio: GPIO pin used to load input into device 25 * @lock: Protects read sequences 26 */ 27 struct pisosr_gpio { 28 struct gpio_chip chip; 29 struct spi_device *spi; 30 u8 *buffer; 31 size_t buffer_size; 32 struct gpio_desc *load_gpio; 33 struct mutex lock; 34 }; 35 36 static int pisosr_gpio_refresh(struct pisosr_gpio *gpio) 37 { 38 int ret; 39 40 mutex_lock(&gpio->lock); 41 42 if (gpio->load_gpio) { 43 gpiod_set_value_cansleep(gpio->load_gpio, 1); 44 udelay(1); /* registers load time (~10ns) */ 45 gpiod_set_value_cansleep(gpio->load_gpio, 0); 46 udelay(1); /* registers recovery time (~5ns) */ 47 } 48 49 ret = spi_read(gpio->spi, gpio->buffer, gpio->buffer_size); 50 51 mutex_unlock(&gpio->lock); 52 53 return ret; 54 } 55 56 static int pisosr_gpio_get_direction(struct gpio_chip *chip, 57 unsigned offset) 58 { 59 /* This device always input */ 60 return GPIO_LINE_DIRECTION_IN; 61 } 62 63 static int pisosr_gpio_direction_input(struct gpio_chip *chip, 64 unsigned offset) 65 { 66 /* This device always input */ 67 return 0; 68 } 69 70 static int pisosr_gpio_direction_output(struct gpio_chip *chip, 71 unsigned offset, int value) 72 { 73 /* This device is input only */ 74 return -EINVAL; 75 } 76 77 static int pisosr_gpio_get(struct gpio_chip *chip, unsigned offset) 78 { 79 struct pisosr_gpio *gpio = gpiochip_get_data(chip); 80 81 /* Refresh may not always be needed */ 82 pisosr_gpio_refresh(gpio); 83 84 return (gpio->buffer[offset / 8] >> (offset % 8)) & 0x1; 85 } 86 87 static int pisosr_gpio_get_multiple(struct gpio_chip *chip, 88 unsigned long *mask, unsigned long *bits) 89 { 90 struct pisosr_gpio *gpio = gpiochip_get_data(chip); 91 unsigned long offset; 92 unsigned long gpio_mask; 93 unsigned long buffer_state; 94 95 pisosr_gpio_refresh(gpio); 96 97 bitmap_zero(bits, chip->ngpio); 98 for_each_set_clump8(offset, gpio_mask, mask, chip->ngpio) { 99 buffer_state = gpio->buffer[offset / 8] & gpio_mask; 100 bitmap_set_value8(bits, buffer_state, offset); 101 } 102 103 return 0; 104 } 105 106 static const struct gpio_chip template_chip = { 107 .label = "pisosr-gpio", 108 .owner = THIS_MODULE, 109 .get_direction = pisosr_gpio_get_direction, 110 .direction_input = pisosr_gpio_direction_input, 111 .direction_output = pisosr_gpio_direction_output, 112 .get = pisosr_gpio_get, 113 .get_multiple = pisosr_gpio_get_multiple, 114 .base = -1, 115 .ngpio = DEFAULT_NGPIO, 116 .can_sleep = true, 117 }; 118 119 static int pisosr_gpio_probe(struct spi_device *spi) 120 { 121 struct device *dev = &spi->dev; 122 struct pisosr_gpio *gpio; 123 int ret; 124 125 gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL); 126 if (!gpio) 127 return -ENOMEM; 128 129 spi_set_drvdata(spi, gpio); 130 131 gpio->chip = template_chip; 132 gpio->chip.parent = dev; 133 of_property_read_u16(dev->of_node, "ngpios", &gpio->chip.ngpio); 134 135 gpio->spi = spi; 136 137 gpio->buffer_size = DIV_ROUND_UP(gpio->chip.ngpio, 8); 138 gpio->buffer = devm_kzalloc(dev, gpio->buffer_size, GFP_KERNEL); 139 if (!gpio->buffer) 140 return -ENOMEM; 141 142 gpio->load_gpio = devm_gpiod_get_optional(dev, "load", GPIOD_OUT_LOW); 143 if (IS_ERR(gpio->load_gpio)) 144 return dev_err_probe(dev, PTR_ERR(gpio->load_gpio), 145 "Unable to allocate load GPIO\n"); 146 147 mutex_init(&gpio->lock); 148 149 ret = gpiochip_add_data(&gpio->chip, gpio); 150 if (ret < 0) { 151 dev_err(dev, "Unable to register gpiochip\n"); 152 return ret; 153 } 154 155 return 0; 156 } 157 158 static void pisosr_gpio_remove(struct spi_device *spi) 159 { 160 struct pisosr_gpio *gpio = spi_get_drvdata(spi); 161 162 gpiochip_remove(&gpio->chip); 163 164 mutex_destroy(&gpio->lock); 165 } 166 167 static const struct spi_device_id pisosr_gpio_id_table[] = { 168 { "pisosr-gpio", }, 169 { /* sentinel */ } 170 }; 171 MODULE_DEVICE_TABLE(spi, pisosr_gpio_id_table); 172 173 static const struct of_device_id pisosr_gpio_of_match_table[] = { 174 { .compatible = "pisosr-gpio", }, 175 { /* sentinel */ } 176 }; 177 MODULE_DEVICE_TABLE(of, pisosr_gpio_of_match_table); 178 179 static struct spi_driver pisosr_gpio_driver = { 180 .driver = { 181 .name = "pisosr-gpio", 182 .of_match_table = pisosr_gpio_of_match_table, 183 }, 184 .probe = pisosr_gpio_probe, 185 .remove = pisosr_gpio_remove, 186 .id_table = pisosr_gpio_id_table, 187 }; 188 module_spi_driver(pisosr_gpio_driver); 189 190 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>"); 191 MODULE_DESCRIPTION("SPI Compatible PISO Shift Register GPIO Driver"); 192 MODULE_LICENSE("GPL v2"); 193