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