1 // SPDX-License-Identifier: GPL-2.0
3 * SGI IOC3 PS/2 controller driver for linux
5 * Copyright (C) 2019 Thomas Bogendoerfer <tbogendoerfer@suse.de>
7 * Based on code Copyright (C) 2005 Stanislaw Skowronek <skylark@unaligned.org>
8 * Copyright (C) 2009 Johannes Dickgreber <tanzy@gmx.de>
11 #include <linux/delay.h>
12 #include <linux/init.h>
14 #include <linux/serio.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
18 #include <asm/sn/ioc3.h>
21 struct ioc3_serioregs __iomem
*regs
;
22 struct serio
*kbd
, *aux
;
23 bool kbd_exists
, aux_exists
;
27 static int ioc3kbd_wait(struct ioc3_serioregs __iomem
*regs
, u32 mask
)
29 unsigned long timeout
= 0;
31 while ((readl(®s
->km_csr
) & mask
) && (timeout
< 250)) {
35 return (timeout
>= 250) ? -ETIMEDOUT
: 0;
38 static int ioc3kbd_write(struct serio
*dev
, u8 val
)
40 struct ioc3kbd_data
*d
= dev
->port_data
;
43 ret
= ioc3kbd_wait(d
->regs
, KM_CSR_K_WRT_PEND
);
47 writel(val
, &d
->regs
->k_wd
);
52 static int ioc3kbd_start(struct serio
*dev
)
54 struct ioc3kbd_data
*d
= dev
->port_data
;
60 static void ioc3kbd_stop(struct serio
*dev
)
62 struct ioc3kbd_data
*d
= dev
->port_data
;
64 d
->kbd_exists
= false;
67 static int ioc3aux_write(struct serio
*dev
, u8 val
)
69 struct ioc3kbd_data
*d
= dev
->port_data
;
72 ret
= ioc3kbd_wait(d
->regs
, KM_CSR_M_WRT_PEND
);
76 writel(val
, &d
->regs
->m_wd
);
81 static int ioc3aux_start(struct serio
*dev
)
83 struct ioc3kbd_data
*d
= dev
->port_data
;
89 static void ioc3aux_stop(struct serio
*dev
)
91 struct ioc3kbd_data
*d
= dev
->port_data
;
93 d
->aux_exists
= false;
96 static void ioc3kbd_process_data(struct serio
*dev
, u32 data
)
98 if (data
& KM_RD_VALID_0
)
99 serio_interrupt(dev
, (data
>> KM_RD_DATA_0_SHIFT
) & 0xff, 0);
100 if (data
& KM_RD_VALID_1
)
101 serio_interrupt(dev
, (data
>> KM_RD_DATA_1_SHIFT
) & 0xff, 0);
102 if (data
& KM_RD_VALID_2
)
103 serio_interrupt(dev
, (data
>> KM_RD_DATA_2_SHIFT
) & 0xff, 0);
106 static irqreturn_t
ioc3kbd_intr(int itq
, void *dev_id
)
108 struct ioc3kbd_data
*d
= dev_id
;
111 data_k
= readl(&d
->regs
->k_rd
);
113 ioc3kbd_process_data(d
->kbd
, data_k
);
115 data_m
= readl(&d
->regs
->m_rd
);
117 ioc3kbd_process_data(d
->aux
, data_m
);
122 static int ioc3kbd_probe(struct platform_device
*pdev
)
124 struct ioc3_serioregs __iomem
*regs
;
125 struct device
*dev
= &pdev
->dev
;
126 struct ioc3kbd_data
*d
;
127 struct serio
*sk
, *sa
;
130 regs
= devm_platform_ioremap_resource(pdev
, 0);
132 return PTR_ERR(regs
);
134 irq
= platform_get_irq(pdev
, 0);
138 d
= devm_kzalloc(dev
, sizeof(*d
), GFP_KERNEL
);
142 sk
= kzalloc(sizeof(*sk
), GFP_KERNEL
);
146 sa
= kzalloc(sizeof(*sa
), GFP_KERNEL
);
152 sk
->id
.type
= SERIO_8042
;
153 sk
->write
= ioc3kbd_write
;
154 sk
->start
= ioc3kbd_start
;
155 sk
->stop
= ioc3kbd_stop
;
156 snprintf(sk
->name
, sizeof(sk
->name
), "IOC3 keyboard %d", pdev
->id
);
157 snprintf(sk
->phys
, sizeof(sk
->phys
), "ioc3/serio%dkbd", pdev
->id
);
159 sk
->dev
.parent
= dev
;
161 sa
->id
.type
= SERIO_8042
;
162 sa
->write
= ioc3aux_write
;
163 sa
->start
= ioc3aux_start
;
164 sa
->stop
= ioc3aux_stop
;
165 snprintf(sa
->name
, sizeof(sa
->name
), "IOC3 auxiliary %d", pdev
->id
);
166 snprintf(sa
->phys
, sizeof(sa
->phys
), "ioc3/serio%daux", pdev
->id
);
168 sa
->dev
.parent
= dev
;
175 platform_set_drvdata(pdev
, d
);
176 serio_register_port(d
->kbd
);
177 serio_register_port(d
->aux
);
179 ret
= request_irq(irq
, ioc3kbd_intr
, IRQF_SHARED
, "ioc3-kbd", d
);
181 dev_err(dev
, "could not request IRQ %d\n", irq
);
182 serio_unregister_port(d
->kbd
);
183 serio_unregister_port(d
->aux
);
188 writel(KM_CSR_K_CLAMP_3
| KM_CSR_M_CLAMP_3
, ®s
->km_csr
);
193 static int ioc3kbd_remove(struct platform_device
*pdev
)
195 struct ioc3kbd_data
*d
= platform_get_drvdata(pdev
);
199 serio_unregister_port(d
->kbd
);
200 serio_unregister_port(d
->aux
);
205 static struct platform_driver ioc3kbd_driver
= {
206 .probe
= ioc3kbd_probe
,
207 .remove
= ioc3kbd_remove
,
212 module_platform_driver(ioc3kbd_driver
);
214 MODULE_AUTHOR("Thomas Bogendoerfer <tbogendoerfer@suse.de>");
215 MODULE_DESCRIPTION("SGI IOC3 serio driver");
216 MODULE_LICENSE("GPL");