2 * Digital I/O driver for Technologic Systems TS-5500
4 * Copyright (c) 2012 Savoir-faire Linux Inc.
5 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 * Technologic Systems platforms have pin blocks, exposing several Digital
8 * Input/Output lines (DIO). This driver aims to support single pin blocks.
9 * In that sense, the support is not limited to the TS-5500 blocks.
10 * Actually, the following platforms have DIO support:
13 * Documentation: http://wiki.embeddedarm.com/wiki/TS-5500
14 * Blocks: DIO1, DIO2 and LCD port.
17 * Documentation: http://wiki.embeddedarm.com/wiki/TS-5600
18 * Blocks: LCD port (identical to TS-5500 LCD).
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2 as
22 * published by the Free Software Foundation.
25 #include <linux/bitops.h>
26 #include <linux/gpio.h>
28 #include <linux/module.h>
29 #include <linux/platform_data/gpio-ts5500.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
33 /* List of supported Technologic Systems platforms DIO blocks */
34 enum ts5500_blocks { TS5500_DIO1, TS5500_DIO2, TS5500_LCD, TS5600_LCD };
37 const struct ts5500_dio *pinout;
38 struct gpio_chip gpio_chip;
45 * Hex 7D is used to control several blocks (e.g. DIO2 and LCD port).
46 * This flag ensures that the region has been requested by this driver.
48 static bool hex7d_reserved;
51 * This structure is used to describe capabilities of DIO lines,
52 * such as available directions and connected interrupt (if any).
57 const u8 control_addr;
58 const u8 control_mask;
64 #define TS5500_DIO_IN_OUT(vaddr, vbit, caddr, cbit) \
66 .value_addr = vaddr, \
67 .value_mask = BIT(vbit), \
68 .control_addr = caddr, \
69 .control_mask = BIT(cbit), \
72 #define TS5500_DIO_IN(addr, bit) \
75 .value_mask = BIT(bit), \
79 #define TS5500_DIO_IN_IRQ(addr, bit, _irq) \
82 .value_mask = BIT(bit), \
87 #define TS5500_DIO_OUT(addr, bit) \
90 .value_mask = BIT(bit), \
95 * Input/Output DIO lines are programmed in groups of 4. Their values are
96 * available through 4 consecutive bits in a value port, whereas the direction
97 * of these 4 lines is driven by only 1 bit in a control port.
99 #define TS5500_DIO_GROUP(vaddr, vbitfrom, caddr, cbit) \
100 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 0, caddr, cbit), \
101 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 1, caddr, cbit), \
102 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 2, caddr, cbit), \
103 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 3, caddr, cbit)
108 * value control dir hw
109 * addr bit addr bit in out irq name pin offset
111 * 0x7b 0 0x7a 0 x x DIO1_0 1 0
112 * 0x7b 1 0x7a 0 x x DIO1_1 3 1
113 * 0x7b 2 0x7a 0 x x DIO1_2 5 2
114 * 0x7b 3 0x7a 0 x x DIO1_3 7 3
115 * 0x7b 4 0x7a 1 x x DIO1_4 9 4
116 * 0x7b 5 0x7a 1 x x DIO1_5 11 5
117 * 0x7b 6 0x7a 1 x x DIO1_6 13 6
118 * 0x7b 7 0x7a 1 x x DIO1_7 15 7
119 * 0x7c 0 0x7a 5 x x DIO1_8 4 8
120 * 0x7c 1 0x7a 5 x x DIO1_9 6 9
121 * 0x7c 2 0x7a 5 x x DIO1_10 8 10
122 * 0x7c 3 0x7a 5 x x DIO1_11 10 11
123 * 0x7c 4 x DIO1_12 12 12
124 * 0x7c 5 x 7 DIO1_13 14 13
126 static const struct ts5500_dio ts5500_dio1[] = {
127 TS5500_DIO_GROUP(0x7b, 0, 0x7a, 0),
128 TS5500_DIO_GROUP(0x7b, 4, 0x7a, 1),
129 TS5500_DIO_GROUP(0x7c, 0, 0x7a, 5),
130 TS5500_DIO_IN(0x7c, 4),
131 TS5500_DIO_IN_IRQ(0x7c, 5, 7),
137 * value control dir hw
138 * addr bit addr bit in out irq name pin offset
140 * 0x7e 0 0x7d 0 x x DIO2_0 1 0
141 * 0x7e 1 0x7d 0 x x DIO2_1 3 1
142 * 0x7e 2 0x7d 0 x x DIO2_2 5 2
143 * 0x7e 3 0x7d 0 x x DIO2_3 7 3
144 * 0x7e 4 0x7d 1 x x DIO2_4 9 4
145 * 0x7e 5 0x7d 1 x x DIO2_5 11 5
146 * 0x7e 6 0x7d 1 x x DIO2_6 13 6
147 * 0x7e 7 0x7d 1 x x DIO2_7 15 7
148 * 0x7f 0 0x7d 5 x x DIO2_8 4 8
149 * 0x7f 1 0x7d 5 x x DIO2_9 6 9
150 * 0x7f 2 0x7d 5 x x DIO2_10 8 10
151 * 0x7f 3 0x7d 5 x x DIO2_11 10 11
152 * 0x7f 4 x 6 DIO2_13 14 12
154 static const struct ts5500_dio ts5500_dio2[] = {
155 TS5500_DIO_GROUP(0x7e, 0, 0x7d, 0),
156 TS5500_DIO_GROUP(0x7e, 4, 0x7d, 1),
157 TS5500_DIO_GROUP(0x7f, 0, 0x7d, 5),
158 TS5500_DIO_IN_IRQ(0x7f, 4, 6),
162 * TS-5500 LCD port used as DIO block
163 * TS-5600 LCD port is identical
165 * value control dir hw
166 * addr bit addr bit in out irq name pin offset
168 * 0x72 0 0x7d 2 x x LCD_0 8 0
169 * 0x72 1 0x7d 2 x x LCD_1 7 1
170 * 0x72 2 0x7d 2 x x LCD_2 10 2
171 * 0x72 3 0x7d 2 x x LCD_3 9 3
172 * 0x72 4 0x7d 3 x x LCD_4 12 4
173 * 0x72 5 0x7d 3 x x LCD_5 11 5
174 * 0x72 6 0x7d 3 x x LCD_6 14 6
175 * 0x72 7 0x7d 3 x x LCD_7 13 7
176 * 0x73 0 x LCD_EN 5 8
177 * 0x73 6 x LCD_WR 6 9
178 * 0x73 7 x 1 LCD_RS 3 10
180 static const struct ts5500_dio ts5500_lcd[] = {
181 TS5500_DIO_GROUP(0x72, 0, 0x7d, 2),
182 TS5500_DIO_GROUP(0x72, 4, 0x7d, 3),
183 TS5500_DIO_OUT(0x73, 0),
184 TS5500_DIO_IN(0x73, 6),
185 TS5500_DIO_IN_IRQ(0x73, 7, 1),
188 static inline void ts5500_set_mask(u8 mask, u8 addr)
195 static inline void ts5500_clear_mask(u8 mask, u8 addr)
202 static int ts5500_gpio_input(struct gpio_chip *chip, unsigned offset)
204 struct ts5500_priv *priv = gpiochip_get_data(chip);
205 const struct ts5500_dio line = priv->pinout[offset];
214 spin_lock_irqsave(&priv->lock, flags);
215 ts5500_clear_mask(line.control_mask, line.control_addr);
216 spin_unlock_irqrestore(&priv->lock, flags);
221 static int ts5500_gpio_get(struct gpio_chip *chip, unsigned offset)
223 struct ts5500_priv *priv = gpiochip_get_data(chip);
224 const struct ts5500_dio line = priv->pinout[offset];
226 return !!(inb(line.value_addr) & line.value_mask);
229 static int ts5500_gpio_output(struct gpio_chip *chip, unsigned offset, int val)
231 struct ts5500_priv *priv = gpiochip_get_data(chip);
232 const struct ts5500_dio line = priv->pinout[offset];
238 spin_lock_irqsave(&priv->lock, flags);
240 ts5500_set_mask(line.control_mask, line.control_addr);
243 ts5500_set_mask(line.value_mask, line.value_addr);
245 ts5500_clear_mask(line.value_mask, line.value_addr);
246 spin_unlock_irqrestore(&priv->lock, flags);
251 static void ts5500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
253 struct ts5500_priv *priv = gpiochip_get_data(chip);
254 const struct ts5500_dio line = priv->pinout[offset];
257 spin_lock_irqsave(&priv->lock, flags);
259 ts5500_set_mask(line.value_mask, line.value_addr);
261 ts5500_clear_mask(line.value_mask, line.value_addr);
262 spin_unlock_irqrestore(&priv->lock, flags);
265 static int ts5500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
267 struct ts5500_priv *priv = gpiochip_get_data(chip);
268 const struct ts5500_dio *block = priv->pinout;
269 const struct ts5500_dio line = block[offset];
271 /* Only one pin is connected to an interrupt */
275 /* As this pin is input-only, we may strap it to another in/out pin */
282 static int ts5500_enable_irq(struct ts5500_priv *priv)
287 spin_lock_irqsave(&priv->lock, flags);
288 if (priv->hwirq == 7)
289 ts5500_set_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
290 else if (priv->hwirq == 6)
291 ts5500_set_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
292 else if (priv->hwirq == 1)
293 ts5500_set_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
296 spin_unlock_irqrestore(&priv->lock, flags);
301 static void ts5500_disable_irq(struct ts5500_priv *priv)
305 spin_lock_irqsave(&priv->lock, flags);
306 if (priv->hwirq == 7)
307 ts5500_clear_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
308 else if (priv->hwirq == 6)
309 ts5500_clear_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
310 else if (priv->hwirq == 1)
311 ts5500_clear_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
313 dev_err(priv->gpio_chip.parent, "invalid hwirq %d\n",
315 spin_unlock_irqrestore(&priv->lock, flags);
318 static int ts5500_dio_probe(struct platform_device *pdev)
320 enum ts5500_blocks block = platform_get_device_id(pdev)->driver_data;
321 struct ts5500_dio_platform_data *pdata = dev_get_platdata(&pdev->dev);
322 struct device *dev = &pdev->dev;
323 const char *name = dev_name(dev);
324 struct ts5500_priv *priv;
325 struct resource *res;
329 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
331 dev_err(dev, "missing IRQ resource\n");
335 priv = devm_kzalloc(dev, sizeof(struct ts5500_priv), GFP_KERNEL);
339 platform_set_drvdata(pdev, priv);
340 priv->hwirq = res->start;
341 spin_lock_init(&priv->lock);
343 priv->gpio_chip.owner = THIS_MODULE;
344 priv->gpio_chip.label = name;
345 priv->gpio_chip.parent = dev;
346 priv->gpio_chip.direction_input = ts5500_gpio_input;
347 priv->gpio_chip.direction_output = ts5500_gpio_output;
348 priv->gpio_chip.get = ts5500_gpio_get;
349 priv->gpio_chip.set = ts5500_gpio_set;
350 priv->gpio_chip.to_irq = ts5500_gpio_to_irq;
351 priv->gpio_chip.base = -1;
353 priv->gpio_chip.base = pdata->base;
354 priv->strap = pdata->strap;
359 priv->pinout = ts5500_dio1;
360 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio1);
362 if (!devm_request_region(dev, 0x7a, 3, name)) {
363 dev_err(dev, "failed to request %s ports\n", name);
368 priv->pinout = ts5500_dio2;
369 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio2);
371 if (!devm_request_region(dev, 0x7e, 2, name)) {
372 dev_err(dev, "failed to request %s ports\n", name);
379 if (!devm_request_region(dev, 0x7d, 1, name)) {
380 dev_err(dev, "failed to request %s 7D\n", name);
384 hex7d_reserved = true;
388 priv->pinout = ts5500_lcd;
389 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_lcd);
391 if (!devm_request_region(dev, 0x72, 2, name)) {
392 dev_err(dev, "failed to request %s ports\n", name);
396 if (!hex7d_reserved) {
397 if (!devm_request_region(dev, 0x7d, 1, name)) {
398 dev_err(dev, "failed to request %s 7D\n", name);
402 hex7d_reserved = true;
405 /* Ensure usage of LCD port as DIO */
406 spin_lock_irqsave(&priv->lock, flags);
407 ts5500_clear_mask(BIT(4), 0x7d);
408 spin_unlock_irqrestore(&priv->lock, flags);
412 ret = devm_gpiochip_add_data(dev, &priv->gpio_chip, priv);
414 dev_err(dev, "failed to register the gpio chip\n");
418 ret = ts5500_enable_irq(priv);
420 dev_err(dev, "invalid interrupt %d\n", priv->hwirq);
427 static int ts5500_dio_remove(struct platform_device *pdev)
429 struct ts5500_priv *priv = platform_get_drvdata(pdev);
431 ts5500_disable_irq(priv);
436 static const struct platform_device_id ts5500_dio_ids[] = {
437 { "ts5500-dio1", TS5500_DIO1 },
438 { "ts5500-dio2", TS5500_DIO2 },
439 { "ts5500-dio-lcd", TS5500_LCD },
440 { "ts5600-dio-lcd", TS5600_LCD },
443 MODULE_DEVICE_TABLE(platform, ts5500_dio_ids);
445 static struct platform_driver ts5500_dio_driver = {
447 .name = "ts5500-dio",
449 .probe = ts5500_dio_probe,
450 .remove = ts5500_dio_remove,
451 .id_table = ts5500_dio_ids,
454 module_platform_driver(ts5500_dio_driver);
456 MODULE_LICENSE("GPL");
457 MODULE_AUTHOR("Savoir-faire Linux Inc. <kernel@savoirfairelinux.com>");
458 MODULE_DESCRIPTION("Technologic Systems TS-5500 Digital I/O driver");