Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * GPIO driver for the ACCES 104-IDIO-16 family
  4 * Copyright (C) 2015 William Breathitt Gray
  5 *
 
 
 
 
 
 
 
 
 
  6 * This driver supports the following ACCES devices: 104-IDIO-16,
  7 * 104-IDIO-16E, 104-IDO-16, 104-IDIO-8, 104-IDIO-8E, and 104-IDO-8.
  8 */
  9#include <linux/bits.h>
 10#include <linux/device.h>
 11#include <linux/err.h>
 
 
 12#include <linux/ioport.h>
 13#include <linux/irq.h>
 
 14#include <linux/isa.h>
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/moduleparam.h>
 18#include <linux/regmap.h>
 19#include <linux/types.h>
 20
 21#include "gpio-idio-16.h"
 22
 23#define IDIO_16_EXTENT 8
 24#define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
 25
 26static unsigned int base[MAX_NUM_IDIO_16];
 27static unsigned int num_idio_16;
 28module_param_hw_array(base, uint, ioport, &num_idio_16, 0);
 29MODULE_PARM_DESC(base, "ACCES 104-IDIO-16 base addresses");
 30
 31static unsigned int irq[MAX_NUM_IDIO_16];
 32static unsigned int num_irq;
 33module_param_hw_array(irq, uint, irq, &num_irq, 0);
 34MODULE_PARM_DESC(irq, "ACCES 104-IDIO-16 interrupt line numbers");
 35
 36static const struct regmap_range idio_16_wr_ranges[] = {
 37	regmap_reg_range(0x0, 0x2), regmap_reg_range(0x4, 0x4),
 38};
 39static const struct regmap_range idio_16_rd_ranges[] = {
 40	regmap_reg_range(0x1, 0x2), regmap_reg_range(0x5, 0x5),
 41};
 42static const struct regmap_range idio_16_precious_ranges[] = {
 43	regmap_reg_range(0x2, 0x2),
 44};
 45static const struct regmap_access_table idio_16_wr_table = {
 46	.yes_ranges = idio_16_wr_ranges,
 47	.n_yes_ranges = ARRAY_SIZE(idio_16_wr_ranges),
 48};
 49static const struct regmap_access_table idio_16_rd_table = {
 50	.yes_ranges = idio_16_rd_ranges,
 51	.n_yes_ranges = ARRAY_SIZE(idio_16_rd_ranges),
 52};
 53static const struct regmap_access_table idio_16_precious_table = {
 54	.yes_ranges = idio_16_precious_ranges,
 55	.n_yes_ranges = ARRAY_SIZE(idio_16_precious_ranges),
 56};
 57static const struct regmap_config idio_16_regmap_config = {
 58	.reg_bits = 8,
 59	.reg_stride = 1,
 60	.val_bits = 8,
 61	.io_port = true,
 62	.wr_table = &idio_16_wr_table,
 63	.rd_table = &idio_16_rd_table,
 64	.volatile_table = &idio_16_rd_table,
 65	.precious_table = &idio_16_precious_table,
 66	.cache_type = REGCACHE_FLAT,
 67	.use_raw_spinlock = true,
 68};
 69
 70/* Only input lines (GPIO 16-31) support interrupts */
 71#define IDIO_16_REGMAP_IRQ(_id)						\
 72	[16 + _id] = {							\
 73		.mask = BIT(_id),					\
 74		.type = { .types_supported = IRQ_TYPE_EDGE_BOTH },	\
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 75	}
 
 76
 77static const struct regmap_irq idio_16_regmap_irqs[] = {
 78	IDIO_16_REGMAP_IRQ(0), IDIO_16_REGMAP_IRQ(1), IDIO_16_REGMAP_IRQ(2), /* 0-2 */
 79	IDIO_16_REGMAP_IRQ(3), IDIO_16_REGMAP_IRQ(4), IDIO_16_REGMAP_IRQ(5), /* 3-5 */
 80	IDIO_16_REGMAP_IRQ(6), IDIO_16_REGMAP_IRQ(7), IDIO_16_REGMAP_IRQ(8), /* 6-8 */
 81	IDIO_16_REGMAP_IRQ(9), IDIO_16_REGMAP_IRQ(10), IDIO_16_REGMAP_IRQ(11), /* 9-11 */
 82	IDIO_16_REGMAP_IRQ(12), IDIO_16_REGMAP_IRQ(13), IDIO_16_REGMAP_IRQ(14), /* 12-14 */
 83	IDIO_16_REGMAP_IRQ(15), /* 15 */
 
 
 
 
 
 
 
 
 
 84};
 85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 86static int idio_16_probe(struct device *dev, unsigned int id)
 87{
 
 88	const char *const name = dev_name(dev);
 89	struct idio_16_regmap_config config = {};
 90	void __iomem *regs;
 91	struct regmap *map;
 
 
 92
 93	if (!devm_request_region(dev, base[id], IDIO_16_EXTENT, name)) {
 94		dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
 95			base[id], base[id] + IDIO_16_EXTENT);
 96		return -EBUSY;
 97	}
 98
 99	regs = devm_ioport_map(dev, base[id], IDIO_16_EXTENT);
100	if (!regs)
101		return -ENOMEM;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
103	map = devm_regmap_init_mmio(dev, regs, &idio_16_regmap_config);
104	if (IS_ERR(map))
105		return dev_err_probe(dev, PTR_ERR(map), "Unable to initialize register map\n");
106
107	config.parent = dev;
108	config.map = map;
109	config.regmap_irqs = idio_16_regmap_irqs;
110	config.num_regmap_irqs = ARRAY_SIZE(idio_16_regmap_irqs);
111	config.irq = irq[id];
112	config.no_status = true;
113
114	return devm_idio_16_regmap_register(dev, &config);
115}
116
117static struct isa_driver idio_16_driver = {
118	.probe = idio_16_probe,
119	.driver = {
120		.name = "104-idio-16"
121	},
 
122};
123
124module_isa_driver_with_irq(idio_16_driver, num_idio_16, num_irq);
125
126MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
127MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
128MODULE_LICENSE("GPL v2");
129MODULE_IMPORT_NS("GPIO_IDIO_16");
v4.10.11
 
  1/*
  2 * GPIO driver for the ACCES 104-IDIO-16 family
  3 * Copyright (C) 2015 William Breathitt Gray
  4 *
  5 * This program is free software; you can redistribute it and/or modify
  6 * 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 in the hope that it will be useful, but
 10 * WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12 * General Public License for more details.
 13 *
 14 * This driver supports the following ACCES devices: 104-IDIO-16,
 15 * 104-IDIO-16E, 104-IDO-16, 104-IDIO-8, 104-IDIO-8E, and 104-IDO-8.
 16 */
 17#include <linux/bitops.h>
 18#include <linux/device.h>
 19#include <linux/errno.h>
 20#include <linux/gpio/driver.h>
 21#include <linux/io.h>
 22#include <linux/ioport.h>
 23#include <linux/interrupt.h>
 24#include <linux/irqdesc.h>
 25#include <linux/isa.h>
 26#include <linux/kernel.h>
 27#include <linux/module.h>
 28#include <linux/moduleparam.h>
 29#include <linux/spinlock.h>
 
 
 
 30
 31#define IDIO_16_EXTENT 8
 32#define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
 33
 34static unsigned int base[MAX_NUM_IDIO_16];
 35static unsigned int num_idio_16;
 36module_param_array(base, uint, &num_idio_16, 0);
 37MODULE_PARM_DESC(base, "ACCES 104-IDIO-16 base addresses");
 38
 39static unsigned int irq[MAX_NUM_IDIO_16];
 40module_param_array(irq, uint, NULL, 0);
 
 41MODULE_PARM_DESC(irq, "ACCES 104-IDIO-16 interrupt line numbers");
 42
 43/**
 44 * struct idio_16_gpio - GPIO device private data structure
 45 * @chip:	instance of the gpio_chip
 46 * @lock:	synchronization lock to prevent I/O race conditions
 47 * @irq_mask:	I/O bits affected by interrupts
 48 * @base:	base port address of the GPIO device
 49 * @irq:	Interrupt line number
 50 * @out_state:	output bits state
 51 */
 52struct idio_16_gpio {
 53	struct gpio_chip chip;
 54	spinlock_t lock;
 55	unsigned long irq_mask;
 56	unsigned base;
 57	unsigned irq;
 58	unsigned out_state;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 59};
 60
 61static int idio_16_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
 62{
 63	if (offset > 15)
 64		return 1;
 65
 66	return 0;
 67}
 68
 69static int idio_16_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
 70{
 71	return 0;
 72}
 73
 74static int idio_16_gpio_direction_output(struct gpio_chip *chip,
 75	unsigned offset, int value)
 76{
 77	chip->set(chip, offset, value);
 78	return 0;
 79}
 80
 81static int idio_16_gpio_get(struct gpio_chip *chip, unsigned offset)
 82{
 83	struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
 84	const unsigned mask = BIT(offset-16);
 85
 86	if (offset < 16)
 87		return -EINVAL;
 88
 89	if (offset < 24)
 90		return !!(inb(idio16gpio->base + 1) & mask);
 91
 92	return !!(inb(idio16gpio->base + 5) & (mask>>8));
 93}
 94
 95static void idio_16_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
 96{
 97	struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
 98	const unsigned mask = BIT(offset);
 99	unsigned long flags;
100
101	if (offset > 15)
102		return;
103
104	spin_lock_irqsave(&idio16gpio->lock, flags);
105
106	if (value)
107		idio16gpio->out_state |= mask;
108	else
109		idio16gpio->out_state &= ~mask;
110
111	if (offset > 7)
112		outb(idio16gpio->out_state >> 8, idio16gpio->base + 4);
113	else
114		outb(idio16gpio->out_state, idio16gpio->base);
115
116	spin_unlock_irqrestore(&idio16gpio->lock, flags);
117}
118
119static void idio_16_irq_ack(struct irq_data *data)
120{
121}
122
123static void idio_16_irq_mask(struct irq_data *data)
124{
125	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
126	struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
127	const unsigned long mask = BIT(irqd_to_hwirq(data));
128	unsigned long flags;
129
130	idio16gpio->irq_mask &= ~mask;
131
132	if (!idio16gpio->irq_mask) {
133		spin_lock_irqsave(&idio16gpio->lock, flags);
134
135		outb(0, idio16gpio->base + 2);
136
137		spin_unlock_irqrestore(&idio16gpio->lock, flags);
138	}
139}
140
141static void idio_16_irq_unmask(struct irq_data *data)
142{
143	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
144	struct idio_16_gpio *const idio16gpio = gpiochip_get_data(chip);
145	const unsigned long mask = BIT(irqd_to_hwirq(data));
146	const unsigned long prev_irq_mask = idio16gpio->irq_mask;
147	unsigned long flags;
148
149	idio16gpio->irq_mask |= mask;
150
151	if (!prev_irq_mask) {
152		spin_lock_irqsave(&idio16gpio->lock, flags);
153
154		inb(idio16gpio->base + 2);
155
156		spin_unlock_irqrestore(&idio16gpio->lock, flags);
157	}
158}
159
160static int idio_16_irq_set_type(struct irq_data *data, unsigned flow_type)
161{
162	/* The only valid irq types are none and both-edges */
163	if (flow_type != IRQ_TYPE_NONE &&
164		(flow_type & IRQ_TYPE_EDGE_BOTH) != IRQ_TYPE_EDGE_BOTH)
165		return -EINVAL;
166
167	return 0;
168}
169
170static struct irq_chip idio_16_irqchip = {
171	.name = "104-idio-16",
172	.irq_ack = idio_16_irq_ack,
173	.irq_mask = idio_16_irq_mask,
174	.irq_unmask = idio_16_irq_unmask,
175	.irq_set_type = idio_16_irq_set_type
176};
177
178static irqreturn_t idio_16_irq_handler(int irq, void *dev_id)
179{
180	struct idio_16_gpio *const idio16gpio = dev_id;
181	struct gpio_chip *const chip = &idio16gpio->chip;
182	int gpio;
183
184	for_each_set_bit(gpio, &idio16gpio->irq_mask, chip->ngpio)
185		generic_handle_irq(irq_find_mapping(chip->irqdomain, gpio));
186
187	spin_lock(&idio16gpio->lock);
188
189	outb(0, idio16gpio->base + 1);
190
191	spin_unlock(&idio16gpio->lock);
192
193	return IRQ_HANDLED;
194}
195
196static int idio_16_probe(struct device *dev, unsigned int id)
197{
198	struct idio_16_gpio *idio16gpio;
199	const char *const name = dev_name(dev);
200	int err;
201
202	idio16gpio = devm_kzalloc(dev, sizeof(*idio16gpio), GFP_KERNEL);
203	if (!idio16gpio)
204		return -ENOMEM;
205
206	if (!devm_request_region(dev, base[id], IDIO_16_EXTENT, name)) {
207		dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
208			base[id], base[id] + IDIO_16_EXTENT);
209		return -EBUSY;
210	}
211
212	idio16gpio->chip.label = name;
213	idio16gpio->chip.parent = dev;
214	idio16gpio->chip.owner = THIS_MODULE;
215	idio16gpio->chip.base = -1;
216	idio16gpio->chip.ngpio = 32;
217	idio16gpio->chip.get_direction = idio_16_gpio_get_direction;
218	idio16gpio->chip.direction_input = idio_16_gpio_direction_input;
219	idio16gpio->chip.direction_output = idio_16_gpio_direction_output;
220	idio16gpio->chip.get = idio_16_gpio_get;
221	idio16gpio->chip.set = idio_16_gpio_set;
222	idio16gpio->base = base[id];
223	idio16gpio->irq = irq[id];
224	idio16gpio->out_state = 0xFFFF;
225
226	spin_lock_init(&idio16gpio->lock);
227
228	dev_set_drvdata(dev, idio16gpio);
229
230	err = gpiochip_add_data(&idio16gpio->chip, idio16gpio);
231	if (err) {
232		dev_err(dev, "GPIO registering failed (%d)\n", err);
233		return err;
234	}
235
236	/* Disable IRQ by default */
237	outb(0, base[id] + 2);
238	outb(0, base[id] + 1);
239
240	err = gpiochip_irqchip_add(&idio16gpio->chip, &idio_16_irqchip, 0,
241		handle_edge_irq, IRQ_TYPE_NONE);
242	if (err) {
243		dev_err(dev, "Could not add irqchip (%d)\n", err);
244		goto err_gpiochip_remove;
245	}
246
247	err = request_irq(irq[id], idio_16_irq_handler, 0, name, idio16gpio);
248	if (err) {
249		dev_err(dev, "IRQ handler registering failed (%d)\n", err);
250		goto err_gpiochip_remove;
251	}
252
253	return 0;
254
255err_gpiochip_remove:
256	gpiochip_remove(&idio16gpio->chip);
257	return err;
258}
259
260static int idio_16_remove(struct device *dev, unsigned int id)
261{
262	struct idio_16_gpio *const idio16gpio = dev_get_drvdata(dev);
263
264	free_irq(idio16gpio->irq, idio16gpio);
265	gpiochip_remove(&idio16gpio->chip);
 
 
 
 
 
 
 
 
266
267	return 0;
268}
269
270static struct isa_driver idio_16_driver = {
271	.probe = idio_16_probe,
272	.driver = {
273		.name = "104-idio-16"
274	},
275	.remove = idio_16_remove
276};
277
278module_isa_driver(idio_16_driver, num_idio_16);
279
280MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
281MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
282MODULE_LICENSE("GPL v2");