1 // SPDX-License-Identifier: GPL-2.0
3 * Digital I/O driver for Technologic Systems TS-5500
5 * Copyright (c) 2012 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
8 * Technologic Systems platforms have pin blocks, exposing several Digital
9 * Input/Output lines (DIO). This driver aims to support single pin blocks.
10 * In that sense, the support is not limited to the TS-5500 blocks.
11 * Actually, the following platforms have DIO support:
14 * Documentation: http://wiki.embeddedarm.com/wiki/TS-5500
15 * Blocks: DIO1, DIO2 and LCD port.
18 * Documentation: http://wiki.embeddedarm.com/wiki/TS-5600
19 * Blocks: LCD port (identical to TS-5500 LCD).
22 #include <linux/bitops.h>
23 #include <linux/gpio/driver.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
29 /* List of supported Technologic Systems platforms DIO blocks */
30 enum ts5500_blocks { TS5500_DIO1, TS5500_DIO2, TS5500_LCD, TS5600_LCD };
33 const struct ts5500_dio *pinout;
34 struct gpio_chip gpio_chip;
41 * Hex 7D is used to control several blocks (e.g. DIO2 and LCD port).
42 * This flag ensures that the region has been requested by this driver.
44 static bool hex7d_reserved;
47 * This structure is used to describe capabilities of DIO lines,
48 * such as available directions and connected interrupt (if any).
53 const u8 control_addr;
54 const u8 control_mask;
60 #define TS5500_DIO_IN_OUT(vaddr, vbit, caddr, cbit) \
62 .value_addr = vaddr, \
63 .value_mask = BIT(vbit), \
64 .control_addr = caddr, \
65 .control_mask = BIT(cbit), \
68 #define TS5500_DIO_IN(addr, bit) \
71 .value_mask = BIT(bit), \
75 #define TS5500_DIO_IN_IRQ(addr, bit, _irq) \
78 .value_mask = BIT(bit), \
83 #define TS5500_DIO_OUT(addr, bit) \
86 .value_mask = BIT(bit), \
91 * Input/Output DIO lines are programmed in groups of 4. Their values are
92 * available through 4 consecutive bits in a value port, whereas the direction
93 * of these 4 lines is driven by only 1 bit in a control port.
95 #define TS5500_DIO_GROUP(vaddr, vbitfrom, caddr, cbit) \
96 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 0, caddr, cbit), \
97 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 1, caddr, cbit), \
98 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 2, caddr, cbit), \
99 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 3, caddr, cbit)
104 * value control dir hw
105 * addr bit addr bit in out irq name pin offset
107 * 0x7b 0 0x7a 0 x x DIO1_0 1 0
108 * 0x7b 1 0x7a 0 x x DIO1_1 3 1
109 * 0x7b 2 0x7a 0 x x DIO1_2 5 2
110 * 0x7b 3 0x7a 0 x x DIO1_3 7 3
111 * 0x7b 4 0x7a 1 x x DIO1_4 9 4
112 * 0x7b 5 0x7a 1 x x DIO1_5 11 5
113 * 0x7b 6 0x7a 1 x x DIO1_6 13 6
114 * 0x7b 7 0x7a 1 x x DIO1_7 15 7
115 * 0x7c 0 0x7a 5 x x DIO1_8 4 8
116 * 0x7c 1 0x7a 5 x x DIO1_9 6 9
117 * 0x7c 2 0x7a 5 x x DIO1_10 8 10
118 * 0x7c 3 0x7a 5 x x DIO1_11 10 11
119 * 0x7c 4 x DIO1_12 12 12
120 * 0x7c 5 x 7 DIO1_13 14 13
122 static const struct ts5500_dio ts5500_dio1[] = {
123 TS5500_DIO_GROUP(0x7b, 0, 0x7a, 0),
124 TS5500_DIO_GROUP(0x7b, 4, 0x7a, 1),
125 TS5500_DIO_GROUP(0x7c, 0, 0x7a, 5),
126 TS5500_DIO_IN(0x7c, 4),
127 TS5500_DIO_IN_IRQ(0x7c, 5, 7),
133 * value control dir hw
134 * addr bit addr bit in out irq name pin offset
136 * 0x7e 0 0x7d 0 x x DIO2_0 1 0
137 * 0x7e 1 0x7d 0 x x DIO2_1 3 1
138 * 0x7e 2 0x7d 0 x x DIO2_2 5 2
139 * 0x7e 3 0x7d 0 x x DIO2_3 7 3
140 * 0x7e 4 0x7d 1 x x DIO2_4 9 4
141 * 0x7e 5 0x7d 1 x x DIO2_5 11 5
142 * 0x7e 6 0x7d 1 x x DIO2_6 13 6
143 * 0x7e 7 0x7d 1 x x DIO2_7 15 7
144 * 0x7f 0 0x7d 5 x x DIO2_8 4 8
145 * 0x7f 1 0x7d 5 x x DIO2_9 6 9
146 * 0x7f 2 0x7d 5 x x DIO2_10 8 10
147 * 0x7f 3 0x7d 5 x x DIO2_11 10 11
148 * 0x7f 4 x 6 DIO2_13 14 12
150 static const struct ts5500_dio ts5500_dio2[] = {
151 TS5500_DIO_GROUP(0x7e, 0, 0x7d, 0),
152 TS5500_DIO_GROUP(0x7e, 4, 0x7d, 1),
153 TS5500_DIO_GROUP(0x7f, 0, 0x7d, 5),
154 TS5500_DIO_IN_IRQ(0x7f, 4, 6),
158 * TS-5500 LCD port used as DIO block
159 * TS-5600 LCD port is identical
161 * value control dir hw
162 * addr bit addr bit in out irq name pin offset
164 * 0x72 0 0x7d 2 x x LCD_0 8 0
165 * 0x72 1 0x7d 2 x x LCD_1 7 1
166 * 0x72 2 0x7d 2 x x LCD_2 10 2
167 * 0x72 3 0x7d 2 x x LCD_3 9 3
168 * 0x72 4 0x7d 3 x x LCD_4 12 4
169 * 0x72 5 0x7d 3 x x LCD_5 11 5
170 * 0x72 6 0x7d 3 x x LCD_6 14 6
171 * 0x72 7 0x7d 3 x x LCD_7 13 7
172 * 0x73 0 x LCD_EN 5 8
173 * 0x73 6 x LCD_WR 6 9
174 * 0x73 7 x 1 LCD_RS 3 10
176 static const struct ts5500_dio ts5500_lcd[] = {
177 TS5500_DIO_GROUP(0x72, 0, 0x7d, 2),
178 TS5500_DIO_GROUP(0x72, 4, 0x7d, 3),
179 TS5500_DIO_OUT(0x73, 0),
180 TS5500_DIO_IN(0x73, 6),
181 TS5500_DIO_IN_IRQ(0x73, 7, 1),
184 static inline void ts5500_set_mask(u8 mask, u8 addr)
191 static inline void ts5500_clear_mask(u8 mask, u8 addr)
198 static int ts5500_gpio_input(struct gpio_chip *chip, unsigned offset)
200 struct ts5500_priv *priv = gpiochip_get_data(chip);
201 const struct ts5500_dio line = priv->pinout[offset];
210 spin_lock_irqsave(&priv->lock, flags);
211 ts5500_clear_mask(line.control_mask, line.control_addr);
212 spin_unlock_irqrestore(&priv->lock, flags);
217 static int ts5500_gpio_get(struct gpio_chip *chip, unsigned offset)
219 struct ts5500_priv *priv = gpiochip_get_data(chip);
220 const struct ts5500_dio line = priv->pinout[offset];
222 return !!(inb(line.value_addr) & line.value_mask);
225 static int ts5500_gpio_output(struct gpio_chip *chip, unsigned offset, int val)
227 struct ts5500_priv *priv = gpiochip_get_data(chip);
228 const struct ts5500_dio line = priv->pinout[offset];
234 spin_lock_irqsave(&priv->lock, flags);
236 ts5500_set_mask(line.control_mask, line.control_addr);
239 ts5500_set_mask(line.value_mask, line.value_addr);
241 ts5500_clear_mask(line.value_mask, line.value_addr);
242 spin_unlock_irqrestore(&priv->lock, flags);
247 static void ts5500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
249 struct ts5500_priv *priv = gpiochip_get_data(chip);
250 const struct ts5500_dio line = priv->pinout[offset];
253 spin_lock_irqsave(&priv->lock, flags);
255 ts5500_set_mask(line.value_mask, line.value_addr);
257 ts5500_clear_mask(line.value_mask, line.value_addr);
258 spin_unlock_irqrestore(&priv->lock, flags);
261 static int ts5500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
263 struct ts5500_priv *priv = gpiochip_get_data(chip);
264 const struct ts5500_dio *block = priv->pinout;
265 const struct ts5500_dio line = block[offset];
267 /* Only one pin is connected to an interrupt */
271 /* As this pin is input-only, we may strap it to another in/out pin */
278 static int ts5500_enable_irq(struct ts5500_priv *priv)
283 spin_lock_irqsave(&priv->lock, flags);
284 if (priv->hwirq == 7)
285 ts5500_set_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
286 else if (priv->hwirq == 6)
287 ts5500_set_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
288 else if (priv->hwirq == 1)
289 ts5500_set_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
292 spin_unlock_irqrestore(&priv->lock, flags);
297 static void ts5500_disable_irq(struct ts5500_priv *priv)
301 spin_lock_irqsave(&priv->lock, flags);
302 if (priv->hwirq == 7)
303 ts5500_clear_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
304 else if (priv->hwirq == 6)
305 ts5500_clear_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
306 else if (priv->hwirq == 1)
307 ts5500_clear_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
309 dev_err(priv->gpio_chip.parent, "invalid hwirq %d\n",
311 spin_unlock_irqrestore(&priv->lock, flags);
314 static int ts5500_dio_probe(struct platform_device *pdev)
316 enum ts5500_blocks block = platform_get_device_id(pdev)->driver_data;
317 struct device *dev = &pdev->dev;
318 const char *name = dev_name(dev);
319 struct ts5500_priv *priv;
320 struct resource *res;
324 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
326 dev_err(dev, "missing IRQ resource\n");
330 priv = devm_kzalloc(dev, sizeof(struct ts5500_priv), GFP_KERNEL);
334 platform_set_drvdata(pdev, priv);
335 priv->hwirq = res->start;
336 spin_lock_init(&priv->lock);
338 priv->gpio_chip.owner = THIS_MODULE;
339 priv->gpio_chip.label = name;
340 priv->gpio_chip.parent = dev;
341 priv->gpio_chip.direction_input = ts5500_gpio_input;
342 priv->gpio_chip.direction_output = ts5500_gpio_output;
343 priv->gpio_chip.get = ts5500_gpio_get;
344 priv->gpio_chip.set = ts5500_gpio_set;
345 priv->gpio_chip.to_irq = ts5500_gpio_to_irq;
346 priv->gpio_chip.base = -1;
350 priv->pinout = ts5500_dio1;
351 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio1);
353 if (!devm_request_region(dev, 0x7a, 3, name)) {
354 dev_err(dev, "failed to request %s ports\n", name);
359 priv->pinout = ts5500_dio2;
360 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio2);
362 if (!devm_request_region(dev, 0x7e, 2, name)) {
363 dev_err(dev, "failed to request %s ports\n", name);
370 if (!devm_request_region(dev, 0x7d, 1, name)) {
371 dev_err(dev, "failed to request %s 7D\n", name);
375 hex7d_reserved = true;
379 priv->pinout = ts5500_lcd;
380 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_lcd);
382 if (!devm_request_region(dev, 0x72, 2, name)) {
383 dev_err(dev, "failed to request %s ports\n", name);
387 if (!hex7d_reserved) {
388 if (!devm_request_region(dev, 0x7d, 1, name)) {
389 dev_err(dev, "failed to request %s 7D\n", name);
393 hex7d_reserved = true;
396 /* Ensure usage of LCD port as DIO */
397 spin_lock_irqsave(&priv->lock, flags);
398 ts5500_clear_mask(BIT(4), 0x7d);
399 spin_unlock_irqrestore(&priv->lock, flags);
403 ret = devm_gpiochip_add_data(dev, &priv->gpio_chip, priv);
405 dev_err(dev, "failed to register the gpio chip\n");
409 ret = ts5500_enable_irq(priv);
411 dev_err(dev, "invalid interrupt %d\n", priv->hwirq);
418 static int ts5500_dio_remove(struct platform_device *pdev)
420 struct ts5500_priv *priv = platform_get_drvdata(pdev);
422 ts5500_disable_irq(priv);
427 static const struct platform_device_id ts5500_dio_ids[] = {
428 { "ts5500-dio1", TS5500_DIO1 },
429 { "ts5500-dio2", TS5500_DIO2 },
430 { "ts5500-dio-lcd", TS5500_LCD },
431 { "ts5600-dio-lcd", TS5600_LCD },
434 MODULE_DEVICE_TABLE(platform, ts5500_dio_ids);
436 static struct platform_driver ts5500_dio_driver = {
438 .name = "ts5500-dio",
440 .probe = ts5500_dio_probe,
441 .remove = ts5500_dio_remove,
442 .id_table = ts5500_dio_ids,
445 module_platform_driver(ts5500_dio_driver);
447 MODULE_LICENSE("GPL");
448 MODULE_AUTHOR("Savoir-faire Linux Inc. <kernel@savoirfairelinux.com>");
449 MODULE_DESCRIPTION("Technologic Systems TS-5500 Digital I/O driver");