|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * ADG1414 SPST Switch Driver |
| 4 | + * |
| 5 | + * Copyright 2023 Analog Devices Inc. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/delay.h> |
| 9 | +#include <linux/gpio/consumer.h> |
| 10 | +#include <linux/gpio/driver.h> |
| 11 | +#include <linux/spi/spi.h> |
| 12 | +#include <linux/module.h> |
| 13 | +#include <linux/mutex.h> |
| 14 | +#include <linux/property.h> |
| 15 | + |
| 16 | +/* |
| 17 | + * Definition used because the latter is not available |
| 18 | + * in project's source tree. |
| 19 | + */ |
| 20 | +#define GPIO_DMA_MINALIGN ARCH_KMALLOC_MINALIGN |
| 21 | + |
| 22 | +struct adg1414_state { |
| 23 | + struct spi_device *spi; |
| 24 | + struct gpio_chip chip; |
| 25 | + struct mutex lock; /* protect sensor state */ |
| 26 | + u8 buffer[32]; |
| 27 | + |
| 28 | + __be32 tx __aligned(GPIO_DMA_MINALIGN); |
| 29 | +}; |
| 30 | + |
| 31 | +static void adg1414_set(struct gpio_chip *chip, |
| 32 | + unsigned int offset, |
| 33 | + int value) |
| 34 | +{ |
| 35 | + struct adg1414_state *st = gpiochip_get_data(chip); |
| 36 | + int ret, i; |
| 37 | + u32 tx = 0; |
| 38 | + |
| 39 | + struct spi_transfer xfer = { |
| 40 | + .tx_buf = &st->tx, |
| 41 | + .len = st->chip.ngpio / 8, |
| 42 | + }; |
| 43 | + |
| 44 | + st->buffer[offset] = value; |
| 45 | + |
| 46 | + for (i = 0; i < st->chip.ngpio; i++) |
| 47 | + tx |= st->buffer[i] << i; |
| 48 | + |
| 49 | + st->tx = cpu_to_be32(tx << (32 - st->chip.ngpio)); |
| 50 | + |
| 51 | + mutex_lock(&st->lock); |
| 52 | + |
| 53 | + ret = spi_sync_transfer(st->spi, &xfer, 1); |
| 54 | + if (ret) |
| 55 | + goto out; |
| 56 | + |
| 57 | +out: |
| 58 | + mutex_unlock(&st->lock); |
| 59 | +} |
| 60 | + |
| 61 | +static int adg1414_get(struct gpio_chip *chip, |
| 62 | + unsigned int offset) |
| 63 | +{ |
| 64 | + struct adg1414_state *st = gpiochip_get_data(chip); |
| 65 | + |
| 66 | + return st->buffer[offset]; |
| 67 | +} |
| 68 | + |
| 69 | +static int adg1414_get_direction(struct gpio_chip *chip, |
| 70 | + unsigned int offset) |
| 71 | +{ |
| 72 | + return GPIO_LINE_DIRECTION_OUT; |
| 73 | +} |
| 74 | + |
| 75 | +static int adg1414_probe(struct spi_device *spi) |
| 76 | +{ |
| 77 | + struct adg1414_state *st; |
| 78 | + struct gpio_desc *reset; |
| 79 | + struct device *dev = &spi->dev; |
| 80 | + u32 num_devices; |
| 81 | + int ret; |
| 82 | + |
| 83 | + st = devm_kzalloc(dev, sizeof(*st), GFP_KERNEL); |
| 84 | + if (!st) |
| 85 | + return -ENOMEM; |
| 86 | + |
| 87 | + st->spi = spi; |
| 88 | + |
| 89 | + /* Use reset pin to reset the device */ |
| 90 | + reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
| 91 | + if (IS_ERR(reset)) |
| 92 | + return dev_err_probe(dev, PTR_ERR(reset), "Failed to get reset gpio"); |
| 93 | + |
| 94 | + if (reset) { |
| 95 | + ndelay(15); |
| 96 | + gpiod_set_value_cansleep(reset, 1); |
| 97 | + } |
| 98 | + |
| 99 | + num_devices = 1; |
| 100 | + ret = device_property_read_u32(dev, "daisy-chained-devices", |
| 101 | + &num_devices); |
| 102 | + if (!ret) { |
| 103 | + if (num_devices > 4 || num_devices < 1) |
| 104 | + return dev_err_probe(dev, ret, |
| 105 | + "Failed to get daisy-chained-devices property\n"); |
| 106 | + } |
| 107 | + |
| 108 | + st->chip.label = "adg1414"; |
| 109 | + st->chip.parent = dev; |
| 110 | + st->chip.owner = THIS_MODULE; |
| 111 | + st->chip.get_direction = adg1414_get_direction; |
| 112 | + st->chip.set = adg1414_set; |
| 113 | + st->chip.get = adg1414_get; |
| 114 | + st->chip.base = -1; |
| 115 | + st->chip.ngpio = num_devices * 8; |
| 116 | + st->chip.can_sleep = true; |
| 117 | + |
| 118 | + mutex_init(&st->lock); |
| 119 | + |
| 120 | + return devm_gpiochip_add_data(dev, &st->chip, st); |
| 121 | +} |
| 122 | + |
| 123 | +static const struct of_device_id adg1414_of_match[] = { |
| 124 | + { .compatible = "adi,adg1414" }, |
| 125 | + { } |
| 126 | +}; |
| 127 | +MODULE_DEVICE_TABLE(of, adg1414_of_match); |
| 128 | + |
| 129 | +static struct spi_driver adg1414_driver = { |
| 130 | + .driver = { |
| 131 | + .name = "adg1414", |
| 132 | + .of_match_table = adg1414_of_match, |
| 133 | + }, |
| 134 | + .probe = adg1414_probe, |
| 135 | +}; |
| 136 | +module_spi_driver(adg1414_driver); |
| 137 | + |
| 138 | +MODULE_AUTHOR("Kim Seer Paller <kimseer.paller@analog.com>"); |
| 139 | +MODULE_DESCRIPTION("ADG1414 SPST Switch Driver"); |
| 140 | +MODULE_LICENSE("GPL"); |
0 commit comments