Linux Audio

Check our new training course

Loading...
v4.6
  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_cansleep(gpio->load_gpio, 1);
 50		udelay(1); /* registers load time (~10ns) */
 51		gpiod_set_value_cansleep(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
 57	mutex_unlock(&gpio->lock);
 58
 59	return ret;
 60}
 61
 62static int pisosr_gpio_get_direction(struct gpio_chip *chip,
 63				     unsigned offset)
 64{
 65	/* This device always input */
 66	return 1;
 67}
 68
 69static int pisosr_gpio_direction_input(struct gpio_chip *chip,
 70				       unsigned offset)
 71{
 72	/* This device always input */
 73	return 0;
 74}
 75
 76static int pisosr_gpio_direction_output(struct gpio_chip *chip,
 77					unsigned offset, int value)
 78{
 79	/* This device is input only */
 80	return -EINVAL;
 81}
 82
 83static int pisosr_gpio_get(struct gpio_chip *chip, unsigned offset)
 84{
 85	struct pisosr_gpio *gpio = gpiochip_get_data(chip);
 86
 87	/* Refresh may not always be needed */
 88	pisosr_gpio_refresh(gpio);
 89
 90	return (gpio->buffer[offset / 8] >> (offset % 8)) & 0x1;
 91}
 92
 93static struct gpio_chip template_chip = {
 94	.label			= "pisosr-gpio",
 95	.owner			= THIS_MODULE,
 96	.get_direction		= pisosr_gpio_get_direction,
 97	.direction_input	= pisosr_gpio_direction_input,
 98	.direction_output	= pisosr_gpio_direction_output,
 99	.get			= pisosr_gpio_get,
100	.base			= -1,
101	.ngpio			= DEFAULT_NGPIO,
102	.can_sleep		= true,
103};
104
105static int pisosr_gpio_probe(struct spi_device *spi)
106{
107	struct device *dev = &spi->dev;
108	struct pisosr_gpio *gpio;
109	int ret;
110
111	gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
112	if (!gpio)
113		return -ENOMEM;
114
115	spi_set_drvdata(spi, gpio);
116
117	gpio->chip = template_chip;
118	gpio->chip.parent = dev;
119	of_property_read_u16(dev->of_node, "ngpios", &gpio->chip.ngpio);
120
121	gpio->spi = spi;
122
123	gpio->buffer_size = DIV_ROUND_UP(gpio->chip.ngpio, 8);
124	gpio->buffer = devm_kzalloc(dev, gpio->buffer_size, GFP_KERNEL);
125	if (!gpio->buffer)
126		return -ENOMEM;
127
128	gpio->load_gpio = devm_gpiod_get_optional(dev, "load", GPIOD_OUT_LOW);
129	if (IS_ERR(gpio->load_gpio)) {
130		ret = PTR_ERR(gpio->load_gpio);
131		if (ret != -EPROBE_DEFER)
132			dev_err(dev, "Unable to allocate load GPIO\n");
133		return ret;
134	}
135
136	mutex_init(&gpio->lock);
137
138	ret = gpiochip_add_data(&gpio->chip, gpio);
139	if (ret < 0) {
140		dev_err(dev, "Unable to register gpiochip\n");
141		return ret;
142	}
143
144	return 0;
145}
146
147static int pisosr_gpio_remove(struct spi_device *spi)
148{
149	struct pisosr_gpio *gpio = spi_get_drvdata(spi);
150
151	gpiochip_remove(&gpio->chip);
152
153	mutex_destroy(&gpio->lock);
154
155	return 0;
156}
157
158static const struct spi_device_id pisosr_gpio_id_table[] = {
159	{ "pisosr-gpio", },
160	{ /* sentinel */ }
161};
162MODULE_DEVICE_TABLE(spi, pisosr_gpio_id_table);
163
164static const struct of_device_id pisosr_gpio_of_match_table[] = {
165	{ .compatible = "pisosr-gpio", },
166	{ /* sentinel */ }
167};
168MODULE_DEVICE_TABLE(of, pisosr_gpio_of_match_table);
169
170static struct spi_driver pisosr_gpio_driver = {
171	.driver = {
172		.name = "pisosr-gpio",
173		.of_match_table = pisosr_gpio_of_match_table,
174	},
175	.probe = pisosr_gpio_probe,
176	.remove = pisosr_gpio_remove,
177	.id_table = pisosr_gpio_id_table,
178};
179module_spi_driver(pisosr_gpio_driver);
180
181MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
182MODULE_DESCRIPTION("SPI Compatible PISO Shift Register GPIO Driver");
183MODULE_LICENSE("GPL v2");
v4.6
  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_cansleep(gpio->load_gpio, 1);
 50		udelay(1); /* registers load time (~10ns) */
 51		gpiod_set_value_cansleep(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
 57	mutex_unlock(&gpio->lock);
 58
 59	return ret;
 60}
 61
 62static int pisosr_gpio_get_direction(struct gpio_chip *chip,
 63				     unsigned offset)
 64{
 65	/* This device always input */
 66	return 1;
 67}
 68
 69static int pisosr_gpio_direction_input(struct gpio_chip *chip,
 70				       unsigned offset)
 71{
 72	/* This device always input */
 73	return 0;
 74}
 75
 76static int pisosr_gpio_direction_output(struct gpio_chip *chip,
 77					unsigned offset, int value)
 78{
 79	/* This device is input only */
 80	return -EINVAL;
 81}
 82
 83static int pisosr_gpio_get(struct gpio_chip *chip, unsigned offset)
 84{
 85	struct pisosr_gpio *gpio = gpiochip_get_data(chip);
 86
 87	/* Refresh may not always be needed */
 88	pisosr_gpio_refresh(gpio);
 89
 90	return (gpio->buffer[offset / 8] >> (offset % 8)) & 0x1;
 91}
 92
 93static struct gpio_chip template_chip = {
 94	.label			= "pisosr-gpio",
 95	.owner			= THIS_MODULE,
 96	.get_direction		= pisosr_gpio_get_direction,
 97	.direction_input	= pisosr_gpio_direction_input,
 98	.direction_output	= pisosr_gpio_direction_output,
 99	.get			= pisosr_gpio_get,
100	.base			= -1,
101	.ngpio			= DEFAULT_NGPIO,
102	.can_sleep		= true,
103};
104
105static int pisosr_gpio_probe(struct spi_device *spi)
106{
107	struct device *dev = &spi->dev;
108	struct pisosr_gpio *gpio;
109	int ret;
110
111	gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL);
112	if (!gpio)
113		return -ENOMEM;
114
115	spi_set_drvdata(spi, gpio);
116
117	gpio->chip = template_chip;
118	gpio->chip.parent = dev;
119	of_property_read_u16(dev->of_node, "ngpios", &gpio->chip.ngpio);
120
121	gpio->spi = spi;
122
123	gpio->buffer_size = DIV_ROUND_UP(gpio->chip.ngpio, 8);
124	gpio->buffer = devm_kzalloc(dev, gpio->buffer_size, GFP_KERNEL);
125	if (!gpio->buffer)
126		return -ENOMEM;
127
128	gpio->load_gpio = devm_gpiod_get_optional(dev, "load", GPIOD_OUT_LOW);
129	if (IS_ERR(gpio->load_gpio)) {
130		ret = PTR_ERR(gpio->load_gpio);
131		if (ret != -EPROBE_DEFER)
132			dev_err(dev, "Unable to allocate load GPIO\n");
133		return ret;
134	}
135
136	mutex_init(&gpio->lock);
137
138	ret = gpiochip_add_data(&gpio->chip, gpio);
139	if (ret < 0) {
140		dev_err(dev, "Unable to register gpiochip\n");
141		return ret;
142	}
143
144	return 0;
145}
146
147static int pisosr_gpio_remove(struct spi_device *spi)
148{
149	struct pisosr_gpio *gpio = spi_get_drvdata(spi);
150
151	gpiochip_remove(&gpio->chip);
152
153	mutex_destroy(&gpio->lock);
154
155	return 0;
156}
157
158static const struct spi_device_id pisosr_gpio_id_table[] = {
159	{ "pisosr-gpio", },
160	{ /* sentinel */ }
161};
162MODULE_DEVICE_TABLE(spi, pisosr_gpio_id_table);
163
164static const struct of_device_id pisosr_gpio_of_match_table[] = {
165	{ .compatible = "pisosr-gpio", },
166	{ /* sentinel */ }
167};
168MODULE_DEVICE_TABLE(of, pisosr_gpio_of_match_table);
169
170static struct spi_driver pisosr_gpio_driver = {
171	.driver = {
172		.name = "pisosr-gpio",
173		.of_match_table = pisosr_gpio_of_match_table,
174	},
175	.probe = pisosr_gpio_probe,
176	.remove = pisosr_gpio_remove,
177	.id_table = pisosr_gpio_id_table,
178};
179module_spi_driver(pisosr_gpio_driver);
180
181MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
182MODULE_DESCRIPTION("SPI Compatible PISO Shift Register GPIO Driver");
183MODULE_LICENSE("GPL v2");