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