blob: d4ea3ee23b9c993dc3c573507a64aed1d1654424 [file] [log] [blame]
Linus Walleijbf972792018-08-29 17:08:53 +02001// SPDX-License-Identifier: GPL-2.0
Vivien Didelot759f5f32012-12-07 21:36:34 -05002/*
3 * Digital I/O driver for Technologic Systems TS-5500
4 *
5 * Copyright (c) 2012 Savoir-faire Linux Inc.
6 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
7 *
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:
12 *
13 * TS-5500:
14 * Documentation: http://wiki.embeddedarm.com/wiki/TS-5500
15 * Blocks: DIO1, DIO2 and LCD port.
16 *
17 * TS-5600:
18 * Documentation: http://wiki.embeddedarm.com/wiki/TS-5600
19 * Blocks: LCD port (identical to TS-5500 LCD).
Vivien Didelot759f5f32012-12-07 21:36:34 -050020 */
21
22#include <linux/bitops.h>
Linus Walleijc99601f2018-08-29 17:07:29 +020023#include <linux/gpio/driver.h>
Vivien Didelot759f5f32012-12-07 21:36:34 -050024#include <linux/io.h>
25#include <linux/module.h>
26#include <linux/platform_data/gpio-ts5500.h>
27#include <linux/platform_device.h>
28#include <linux/slab.h>
29
30/* List of supported Technologic Systems platforms DIO blocks */
31enum ts5500_blocks { TS5500_DIO1, TS5500_DIO2, TS5500_LCD, TS5600_LCD };
32
33struct ts5500_priv {
34 const struct ts5500_dio *pinout;
35 struct gpio_chip gpio_chip;
36 spinlock_t lock;
37 bool strap;
38 u8 hwirq;
39};
40
41/*
42 * Hex 7D is used to control several blocks (e.g. DIO2 and LCD port).
43 * This flag ensures that the region has been requested by this driver.
44 */
45static bool hex7d_reserved;
46
47/*
48 * This structure is used to describe capabilities of DIO lines,
49 * such as available directions and connected interrupt (if any).
50 */
51struct ts5500_dio {
52 const u8 value_addr;
53 const u8 value_mask;
54 const u8 control_addr;
55 const u8 control_mask;
56 const bool no_input;
57 const bool no_output;
58 const u8 irq;
59};
60
61#define TS5500_DIO_IN_OUT(vaddr, vbit, caddr, cbit) \
62 { \
63 .value_addr = vaddr, \
64 .value_mask = BIT(vbit), \
65 .control_addr = caddr, \
66 .control_mask = BIT(cbit), \
67 }
68
69#define TS5500_DIO_IN(addr, bit) \
70 { \
71 .value_addr = addr, \
72 .value_mask = BIT(bit), \
73 .no_output = true, \
74 }
75
76#define TS5500_DIO_IN_IRQ(addr, bit, _irq) \
77 { \
78 .value_addr = addr, \
79 .value_mask = BIT(bit), \
80 .no_output = true, \
81 .irq = _irq, \
82 }
83
84#define TS5500_DIO_OUT(addr, bit) \
85 { \
86 .value_addr = addr, \
87 .value_mask = BIT(bit), \
88 .no_input = true, \
89 }
90
91/*
92 * Input/Output DIO lines are programmed in groups of 4. Their values are
93 * available through 4 consecutive bits in a value port, whereas the direction
94 * of these 4 lines is driven by only 1 bit in a control port.
95 */
96#define TS5500_DIO_GROUP(vaddr, vbitfrom, caddr, cbit) \
97 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 0, caddr, cbit), \
98 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 1, caddr, cbit), \
99 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 2, caddr, cbit), \
100 TS5500_DIO_IN_OUT(vaddr, vbitfrom + 3, caddr, cbit)
101
102/*
103 * TS-5500 DIO1 block
104 *
105 * value control dir hw
106 * addr bit addr bit in out irq name pin offset
107 *
108 * 0x7b 0 0x7a 0 x x DIO1_0 1 0
109 * 0x7b 1 0x7a 0 x x DIO1_1 3 1
110 * 0x7b 2 0x7a 0 x x DIO1_2 5 2
111 * 0x7b 3 0x7a 0 x x DIO1_3 7 3
112 * 0x7b 4 0x7a 1 x x DIO1_4 9 4
113 * 0x7b 5 0x7a 1 x x DIO1_5 11 5
114 * 0x7b 6 0x7a 1 x x DIO1_6 13 6
115 * 0x7b 7 0x7a 1 x x DIO1_7 15 7
116 * 0x7c 0 0x7a 5 x x DIO1_8 4 8
117 * 0x7c 1 0x7a 5 x x DIO1_9 6 9
118 * 0x7c 2 0x7a 5 x x DIO1_10 8 10
119 * 0x7c 3 0x7a 5 x x DIO1_11 10 11
120 * 0x7c 4 x DIO1_12 12 12
121 * 0x7c 5 x 7 DIO1_13 14 13
122 */
123static const struct ts5500_dio ts5500_dio1[] = {
124 TS5500_DIO_GROUP(0x7b, 0, 0x7a, 0),
125 TS5500_DIO_GROUP(0x7b, 4, 0x7a, 1),
126 TS5500_DIO_GROUP(0x7c, 0, 0x7a, 5),
127 TS5500_DIO_IN(0x7c, 4),
128 TS5500_DIO_IN_IRQ(0x7c, 5, 7),
129};
130
131/*
132 * TS-5500 DIO2 block
133 *
134 * value control dir hw
135 * addr bit addr bit in out irq name pin offset
136 *
137 * 0x7e 0 0x7d 0 x x DIO2_0 1 0
138 * 0x7e 1 0x7d 0 x x DIO2_1 3 1
139 * 0x7e 2 0x7d 0 x x DIO2_2 5 2
140 * 0x7e 3 0x7d 0 x x DIO2_3 7 3
141 * 0x7e 4 0x7d 1 x x DIO2_4 9 4
142 * 0x7e 5 0x7d 1 x x DIO2_5 11 5
143 * 0x7e 6 0x7d 1 x x DIO2_6 13 6
144 * 0x7e 7 0x7d 1 x x DIO2_7 15 7
145 * 0x7f 0 0x7d 5 x x DIO2_8 4 8
146 * 0x7f 1 0x7d 5 x x DIO2_9 6 9
147 * 0x7f 2 0x7d 5 x x DIO2_10 8 10
148 * 0x7f 3 0x7d 5 x x DIO2_11 10 11
149 * 0x7f 4 x 6 DIO2_13 14 12
150 */
151static const struct ts5500_dio ts5500_dio2[] = {
152 TS5500_DIO_GROUP(0x7e, 0, 0x7d, 0),
153 TS5500_DIO_GROUP(0x7e, 4, 0x7d, 1),
154 TS5500_DIO_GROUP(0x7f, 0, 0x7d, 5),
155 TS5500_DIO_IN_IRQ(0x7f, 4, 6),
156};
157
158/*
159 * TS-5500 LCD port used as DIO block
160 * TS-5600 LCD port is identical
161 *
162 * value control dir hw
163 * addr bit addr bit in out irq name pin offset
164 *
165 * 0x72 0 0x7d 2 x x LCD_0 8 0
166 * 0x72 1 0x7d 2 x x LCD_1 7 1
167 * 0x72 2 0x7d 2 x x LCD_2 10 2
168 * 0x72 3 0x7d 2 x x LCD_3 9 3
169 * 0x72 4 0x7d 3 x x LCD_4 12 4
170 * 0x72 5 0x7d 3 x x LCD_5 11 5
171 * 0x72 6 0x7d 3 x x LCD_6 14 6
172 * 0x72 7 0x7d 3 x x LCD_7 13 7
173 * 0x73 0 x LCD_EN 5 8
174 * 0x73 6 x LCD_WR 6 9
175 * 0x73 7 x 1 LCD_RS 3 10
176 */
177static const struct ts5500_dio ts5500_lcd[] = {
178 TS5500_DIO_GROUP(0x72, 0, 0x7d, 2),
179 TS5500_DIO_GROUP(0x72, 4, 0x7d, 3),
180 TS5500_DIO_OUT(0x73, 0),
181 TS5500_DIO_IN(0x73, 6),
182 TS5500_DIO_IN_IRQ(0x73, 7, 1),
183};
184
Vivien Didelot759f5f32012-12-07 21:36:34 -0500185static inline void ts5500_set_mask(u8 mask, u8 addr)
186{
187 u8 val = inb(addr);
188 val |= mask;
189 outb(val, addr);
190}
191
192static inline void ts5500_clear_mask(u8 mask, u8 addr)
193{
194 u8 val = inb(addr);
195 val &= ~mask;
196 outb(val, addr);
197}
198
199static int ts5500_gpio_input(struct gpio_chip *chip, unsigned offset)
200{
Linus Walleij11ab89a2015-12-07 14:52:55 +0100201 struct ts5500_priv *priv = gpiochip_get_data(chip);
Vivien Didelot759f5f32012-12-07 21:36:34 -0500202 const struct ts5500_dio line = priv->pinout[offset];
203 unsigned long flags;
204
205 if (line.no_input)
206 return -ENXIO;
207
208 if (line.no_output)
209 return 0;
210
211 spin_lock_irqsave(&priv->lock, flags);
212 ts5500_clear_mask(line.control_mask, line.control_addr);
213 spin_unlock_irqrestore(&priv->lock, flags);
214
215 return 0;
216}
217
218static int ts5500_gpio_get(struct gpio_chip *chip, unsigned offset)
219{
Linus Walleij11ab89a2015-12-07 14:52:55 +0100220 struct ts5500_priv *priv = gpiochip_get_data(chip);
Vivien Didelot759f5f32012-12-07 21:36:34 -0500221 const struct ts5500_dio line = priv->pinout[offset];
222
223 return !!(inb(line.value_addr) & line.value_mask);
224}
225
226static int ts5500_gpio_output(struct gpio_chip *chip, unsigned offset, int val)
227{
Linus Walleij11ab89a2015-12-07 14:52:55 +0100228 struct ts5500_priv *priv = gpiochip_get_data(chip);
Vivien Didelot759f5f32012-12-07 21:36:34 -0500229 const struct ts5500_dio line = priv->pinout[offset];
230 unsigned long flags;
231
232 if (line.no_output)
233 return -ENXIO;
234
235 spin_lock_irqsave(&priv->lock, flags);
236 if (!line.no_input)
237 ts5500_set_mask(line.control_mask, line.control_addr);
238
239 if (val)
240 ts5500_set_mask(line.value_mask, line.value_addr);
241 else
242 ts5500_clear_mask(line.value_mask, line.value_addr);
243 spin_unlock_irqrestore(&priv->lock, flags);
244
245 return 0;
246}
247
248static void ts5500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
249{
Linus Walleij11ab89a2015-12-07 14:52:55 +0100250 struct ts5500_priv *priv = gpiochip_get_data(chip);
Vivien Didelot759f5f32012-12-07 21:36:34 -0500251 const struct ts5500_dio line = priv->pinout[offset];
252 unsigned long flags;
253
254 spin_lock_irqsave(&priv->lock, flags);
255 if (val)
256 ts5500_set_mask(line.value_mask, line.value_addr);
257 else
258 ts5500_clear_mask(line.value_mask, line.value_addr);
259 spin_unlock_irqrestore(&priv->lock, flags);
260}
261
262static int ts5500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
263{
Linus Walleij11ab89a2015-12-07 14:52:55 +0100264 struct ts5500_priv *priv = gpiochip_get_data(chip);
Vivien Didelot759f5f32012-12-07 21:36:34 -0500265 const struct ts5500_dio *block = priv->pinout;
266 const struct ts5500_dio line = block[offset];
267
268 /* Only one pin is connected to an interrupt */
269 if (line.irq)
270 return line.irq;
271
272 /* As this pin is input-only, we may strap it to another in/out pin */
273 if (priv->strap)
274 return priv->hwirq;
275
276 return -ENXIO;
277}
278
279static int ts5500_enable_irq(struct ts5500_priv *priv)
280{
281 int ret = 0;
282 unsigned long flags;
283
284 spin_lock_irqsave(&priv->lock, flags);
285 if (priv->hwirq == 7)
286 ts5500_set_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
287 else if (priv->hwirq == 6)
288 ts5500_set_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
289 else if (priv->hwirq == 1)
290 ts5500_set_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
291 else
292 ret = -EINVAL;
293 spin_unlock_irqrestore(&priv->lock, flags);
294
295 return ret;
296}
297
298static void ts5500_disable_irq(struct ts5500_priv *priv)
299{
300 unsigned long flags;
301
302 spin_lock_irqsave(&priv->lock, flags);
303 if (priv->hwirq == 7)
304 ts5500_clear_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
305 else if (priv->hwirq == 6)
306 ts5500_clear_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
307 else if (priv->hwirq == 1)
308 ts5500_clear_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
309 else
Linus Walleij58383c782015-11-04 09:56:26 +0100310 dev_err(priv->gpio_chip.parent, "invalid hwirq %d\n",
311 priv->hwirq);
Vivien Didelot759f5f32012-12-07 21:36:34 -0500312 spin_unlock_irqrestore(&priv->lock, flags);
313}
314
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800315static int ts5500_dio_probe(struct platform_device *pdev)
Vivien Didelot759f5f32012-12-07 21:36:34 -0500316{
317 enum ts5500_blocks block = platform_get_device_id(pdev)->driver_data;
Jingoo Hane56aee12013-07-30 17:08:05 +0900318 struct ts5500_dio_platform_data *pdata = dev_get_platdata(&pdev->dev);
Vivien Didelot759f5f32012-12-07 21:36:34 -0500319 struct device *dev = &pdev->dev;
320 const char *name = dev_name(dev);
321 struct ts5500_priv *priv;
322 struct resource *res;
323 unsigned long flags;
324 int ret;
325
326 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
327 if (!res) {
328 dev_err(dev, "missing IRQ resource\n");
329 return -EINVAL;
330 }
331
332 priv = devm_kzalloc(dev, sizeof(struct ts5500_priv), GFP_KERNEL);
333 if (!priv)
334 return -ENOMEM;
335
336 platform_set_drvdata(pdev, priv);
337 priv->hwirq = res->start;
338 spin_lock_init(&priv->lock);
339
340 priv->gpio_chip.owner = THIS_MODULE;
341 priv->gpio_chip.label = name;
Linus Walleij58383c782015-11-04 09:56:26 +0100342 priv->gpio_chip.parent = dev;
Vivien Didelot759f5f32012-12-07 21:36:34 -0500343 priv->gpio_chip.direction_input = ts5500_gpio_input;
344 priv->gpio_chip.direction_output = ts5500_gpio_output;
345 priv->gpio_chip.get = ts5500_gpio_get;
346 priv->gpio_chip.set = ts5500_gpio_set;
347 priv->gpio_chip.to_irq = ts5500_gpio_to_irq;
348 priv->gpio_chip.base = -1;
349 if (pdata) {
350 priv->gpio_chip.base = pdata->base;
351 priv->strap = pdata->strap;
352 }
353
354 switch (block) {
355 case TS5500_DIO1:
356 priv->pinout = ts5500_dio1;
357 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio1);
358
359 if (!devm_request_region(dev, 0x7a, 3, name)) {
360 dev_err(dev, "failed to request %s ports\n", name);
361 return -EBUSY;
362 }
363 break;
364 case TS5500_DIO2:
365 priv->pinout = ts5500_dio2;
366 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio2);
367
368 if (!devm_request_region(dev, 0x7e, 2, name)) {
369 dev_err(dev, "failed to request %s ports\n", name);
370 return -EBUSY;
371 }
372
373 if (hex7d_reserved)
374 break;
375
376 if (!devm_request_region(dev, 0x7d, 1, name)) {
377 dev_err(dev, "failed to request %s 7D\n", name);
378 return -EBUSY;
379 }
380
381 hex7d_reserved = true;
382 break;
383 case TS5500_LCD:
384 case TS5600_LCD:
385 priv->pinout = ts5500_lcd;
386 priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_lcd);
387
388 if (!devm_request_region(dev, 0x72, 2, name)) {
389 dev_err(dev, "failed to request %s ports\n", name);
390 return -EBUSY;
391 }
392
393 if (!hex7d_reserved) {
394 if (!devm_request_region(dev, 0x7d, 1, name)) {
395 dev_err(dev, "failed to request %s 7D\n", name);
396 return -EBUSY;
397 }
398
399 hex7d_reserved = true;
400 }
401
402 /* Ensure usage of LCD port as DIO */
403 spin_lock_irqsave(&priv->lock, flags);
404 ts5500_clear_mask(BIT(4), 0x7d);
405 spin_unlock_irqrestore(&priv->lock, flags);
406 break;
407 }
408
Laxman Dewangan973eff02016-02-22 17:43:28 +0530409 ret = devm_gpiochip_add_data(dev, &priv->gpio_chip, priv);
Vivien Didelot759f5f32012-12-07 21:36:34 -0500410 if (ret) {
411 dev_err(dev, "failed to register the gpio chip\n");
412 return ret;
413 }
414
415 ret = ts5500_enable_irq(priv);
416 if (ret) {
417 dev_err(dev, "invalid interrupt %d\n", priv->hwirq);
Laxman Dewangan973eff02016-02-22 17:43:28 +0530418 return ret;
Vivien Didelot759f5f32012-12-07 21:36:34 -0500419 }
420
421 return 0;
Vivien Didelot759f5f32012-12-07 21:36:34 -0500422}
423
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800424static int ts5500_dio_remove(struct platform_device *pdev)
Vivien Didelot759f5f32012-12-07 21:36:34 -0500425{
426 struct ts5500_priv *priv = platform_get_drvdata(pdev);
427
428 ts5500_disable_irq(priv);
Laxman Dewangan973eff02016-02-22 17:43:28 +0530429
abdoulaye berthe9f5132a2014-07-12 22:30:12 +0200430 return 0;
Vivien Didelot759f5f32012-12-07 21:36:34 -0500431}
432
Krzysztof Kozlowskif4f79d42015-05-02 00:56:47 +0900433static const struct platform_device_id ts5500_dio_ids[] = {
Vivien Didelot759f5f32012-12-07 21:36:34 -0500434 { "ts5500-dio1", TS5500_DIO1 },
435 { "ts5500-dio2", TS5500_DIO2 },
436 { "ts5500-dio-lcd", TS5500_LCD },
437 { "ts5600-dio-lcd", TS5600_LCD },
438 { }
439};
440MODULE_DEVICE_TABLE(platform, ts5500_dio_ids);
441
442static struct platform_driver ts5500_dio_driver = {
443 .driver = {
444 .name = "ts5500-dio",
Vivien Didelot759f5f32012-12-07 21:36:34 -0500445 },
446 .probe = ts5500_dio_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800447 .remove = ts5500_dio_remove,
Vivien Didelot759f5f32012-12-07 21:36:34 -0500448 .id_table = ts5500_dio_ids,
449};
450
451module_platform_driver(ts5500_dio_driver);
452
453MODULE_LICENSE("GPL");
454MODULE_AUTHOR("Savoir-faire Linux Inc. <kernel@savoirfairelinux.com>");
455MODULE_DESCRIPTION("Technologic Systems TS-5500 Digital I/O driver");