1 // SPDX-License-Identifier: GPL-2.0+
4 * Support for EC-connected GPIOs for identify
5 * LED/button on Barco P50 board
7 * Copyright (C) 2021 Barco NV
8 * Author: Santosh Kumar Yadav <santoshkumar.yadav@barco.com>
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/delay.h>
14 #include <linux/dmi.h>
15 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/leds.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/gpio_keys.h>
22 #include <linux/gpio/driver.h>
23 #include <linux/gpio/machine.h>
24 #include <linux/input.h>
27 #define DRIVER_NAME "barco-p50-gpio"
30 #define P50_GPIO_LINE_LED 0
31 #define P50_GPIO_LINE_BTN 1
34 #define P50_GPIO_IO_PORT_BASE 0x299
36 #define P50_PORT_DATA 0x00
37 #define P50_PORT_CMD 0x01
39 #define P50_STATUS_OBF 0x01 /* EC output buffer full */
40 #define P50_STATUS_IBF 0x02 /* EC input buffer full */
42 #define P50_CMD_READ 0xa0
43 #define P50_CMD_WRITE 0x50
45 /* EC mailbox registers */
46 #define P50_MBOX_REG_CMD 0x00
47 #define P50_MBOX_REG_STATUS 0x01
48 #define P50_MBOX_REG_PARAM 0x02
49 #define P50_MBOX_REG_DATA 0x03
51 #define P50_MBOX_CMD_READ_GPIO 0x11
52 #define P50_MBOX_CMD_WRITE_GPIO 0x12
53 #define P50_MBOX_CMD_CLEAR 0xff
55 #define P50_MBOX_STATUS_SUCCESS 0x01
57 #define P50_MBOX_PARAM_LED 0x12
58 #define P50_MBOX_PARAM_BTN 0x13
65 struct platform_device
*leds_pdev
;
66 struct platform_device
*keys_pdev
;
69 static struct platform_device
*gpio_pdev
;
71 static int gpio_params
[] = {
72 [P50_GPIO_LINE_LED
] = P50_MBOX_PARAM_LED
,
73 [P50_GPIO_LINE_BTN
] = P50_MBOX_PARAM_BTN
,
76 static const char * const gpio_names
[] = {
77 [P50_GPIO_LINE_LED
] = "identify-led",
78 [P50_GPIO_LINE_BTN
] = "identify-button",
82 static struct gpiod_lookup_table p50_gpio_led_table
= {
83 .dev_id
= "leds-gpio",
85 GPIO_LOOKUP_IDX(DRIVER_NAME
, P50_GPIO_LINE_LED
, NULL
, 0, GPIO_ACTIVE_HIGH
),
91 static struct gpio_led leds
[] = {
92 { .name
= "identify" }
95 static struct gpio_led_platform_data leds_pdata
= {
96 .num_leds
= ARRAY_SIZE(leds
),
101 static struct gpio_keys_button buttons
[] = {
104 .gpio
= P50_GPIO_LINE_BTN
,
111 static struct gpio_keys_platform_data keys_pdata
= {
113 .nbuttons
= ARRAY_SIZE(buttons
),
114 .poll_interval
= 100,
120 /* low level access routines */
122 static int p50_wait_ec(struct p50_gpio
*p50
, int mask
, int expected
)
126 for (i
= 0; i
< 100; i
++) {
127 val
= inb(p50
->base
+ P50_PORT_CMD
) & mask
;
130 usleep_range(500, 2000);
133 dev_err(p50
->gc
.parent
, "Timed out waiting for EC (0x%x)\n", val
);
138 static int p50_read_mbox_reg(struct p50_gpio
*p50
, int reg
)
142 ret
= p50_wait_ec(p50
, P50_STATUS_IBF
, 0);
146 /* clear output buffer flag, prevent unfinished commands */
147 inb(p50
->base
+ P50_PORT_DATA
);
150 outb(P50_CMD_READ
| reg
, p50
->base
+ P50_PORT_CMD
);
152 ret
= p50_wait_ec(p50
, P50_STATUS_OBF
, P50_STATUS_OBF
);
156 return inb(p50
->base
+ P50_PORT_DATA
);
159 static int p50_write_mbox_reg(struct p50_gpio
*p50
, int reg
, int val
)
163 ret
= p50_wait_ec(p50
, P50_STATUS_IBF
, 0);
168 outb(P50_CMD_WRITE
| reg
, p50
->base
+ P50_PORT_CMD
);
170 ret
= p50_wait_ec(p50
, P50_STATUS_IBF
, 0);
175 outb(val
, p50
->base
+ P50_PORT_DATA
);
183 static int p50_wait_mbox_idle(struct p50_gpio
*p50
)
187 for (i
= 0; i
< 1000; i
++) {
188 val
= p50_read_mbox_reg(p50
, P50_MBOX_REG_CMD
);
189 /* cmd is 0 when idle */
193 usleep_range(500, 2000);
196 dev_err(p50
->gc
.parent
, "Timed out waiting for EC mbox idle (CMD: 0x%x)\n", val
);
201 static int p50_send_mbox_cmd(struct p50_gpio
*p50
, int cmd
, int param
, int data
)
205 ret
= p50_wait_mbox_idle(p50
);
209 ret
= p50_write_mbox_reg(p50
, P50_MBOX_REG_DATA
, data
);
213 ret
= p50_write_mbox_reg(p50
, P50_MBOX_REG_PARAM
, param
);
217 ret
= p50_write_mbox_reg(p50
, P50_MBOX_REG_CMD
, cmd
);
221 ret
= p50_wait_mbox_idle(p50
);
225 ret
= p50_read_mbox_reg(p50
, P50_MBOX_REG_STATUS
);
229 if (ret
== P50_MBOX_STATUS_SUCCESS
)
232 dev_err(p50
->gc
.parent
, "Mbox command failed (CMD=0x%x STAT=0x%x PARAM=0x%x DATA=0x%x)\n",
233 cmd
, ret
, param
, data
);
241 static int p50_gpio_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
244 case P50_GPIO_LINE_BTN
:
245 return GPIO_LINE_DIRECTION_IN
;
247 case P50_GPIO_LINE_LED
:
248 return GPIO_LINE_DIRECTION_OUT
;
255 static int p50_gpio_get(struct gpio_chip
*gc
, unsigned int offset
)
257 struct p50_gpio
*p50
= gpiochip_get_data(gc
);
260 mutex_lock(&p50
->lock
);
262 ret
= p50_send_mbox_cmd(p50
, P50_MBOX_CMD_READ_GPIO
, gpio_params
[offset
], 0);
264 ret
= p50_read_mbox_reg(p50
, P50_MBOX_REG_DATA
);
266 mutex_unlock(&p50
->lock
);
271 static void p50_gpio_set(struct gpio_chip
*gc
, unsigned int offset
, int value
)
273 struct p50_gpio
*p50
= gpiochip_get_data(gc
);
275 mutex_lock(&p50
->lock
);
277 p50_send_mbox_cmd(p50
, P50_MBOX_CMD_WRITE_GPIO
, gpio_params
[offset
], value
);
279 mutex_unlock(&p50
->lock
);
282 static int p50_gpio_probe(struct platform_device
*pdev
)
284 struct p50_gpio
*p50
;
285 struct resource
*res
;
288 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
290 dev_err(&pdev
->dev
, "Cannot get I/O ports\n");
294 if (!devm_request_region(&pdev
->dev
, res
->start
, resource_size(res
), pdev
->name
)) {
295 dev_err(&pdev
->dev
, "Unable to reserve I/O region\n");
299 p50
= devm_kzalloc(&pdev
->dev
, sizeof(*p50
), GFP_KERNEL
);
303 platform_set_drvdata(pdev
, p50
);
304 mutex_init(&p50
->lock
);
305 p50
->base
= res
->start
;
306 p50
->gc
.owner
= THIS_MODULE
;
307 p50
->gc
.parent
= &pdev
->dev
;
308 p50
->gc
.label
= dev_name(&pdev
->dev
);
309 p50
->gc
.ngpio
= ARRAY_SIZE(gpio_names
);
310 p50
->gc
.names
= gpio_names
;
311 p50
->gc
.can_sleep
= true;
313 p50
->gc
.get_direction
= p50_gpio_get_direction
;
314 p50
->gc
.get
= p50_gpio_get
;
315 p50
->gc
.set
= p50_gpio_set
;
319 ret
= p50_wait_mbox_idle(p50
);
323 ret
= p50_write_mbox_reg(p50
, P50_MBOX_REG_CMD
, P50_MBOX_CMD_CLEAR
);
327 ret
= p50_wait_mbox_idle(p50
);
332 ret
= devm_gpiochip_add_data(&pdev
->dev
, &p50
->gc
, p50
);
334 dev_err(&pdev
->dev
, "Could not register gpiochip: %d\n", ret
);
338 gpiod_add_lookup_table(&p50_gpio_led_table
);
340 p50
->leds_pdev
= platform_device_register_data(&pdev
->dev
,
341 "leds-gpio", PLATFORM_DEVID_NONE
, &leds_pdata
, sizeof(leds_pdata
));
343 if (IS_ERR(p50
->leds_pdev
)) {
344 ret
= PTR_ERR(p50
->leds_pdev
);
345 dev_err(&pdev
->dev
, "Could not register leds-gpio: %d\n", ret
);
349 /* gpio-keys-polled uses old-style gpio interface, pass the right identifier */
350 buttons
[0].gpio
+= p50
->gc
.base
;
353 platform_device_register_data(&pdev
->dev
, "gpio-keys-polled",
355 &keys_pdata
, sizeof(keys_pdata
));
357 if (IS_ERR(p50
->keys_pdev
)) {
358 ret
= PTR_ERR(p50
->keys_pdev
);
359 dev_err(&pdev
->dev
, "Could not register gpio-keys-polled: %d\n", ret
);
366 platform_device_unregister(p50
->leds_pdev
);
368 gpiod_remove_lookup_table(&p50_gpio_led_table
);
373 static void p50_gpio_remove(struct platform_device
*pdev
)
375 struct p50_gpio
*p50
= platform_get_drvdata(pdev
);
377 platform_device_unregister(p50
->keys_pdev
);
378 platform_device_unregister(p50
->leds_pdev
);
380 gpiod_remove_lookup_table(&p50_gpio_led_table
);
383 static struct platform_driver p50_gpio_driver
= {
387 .probe
= p50_gpio_probe
,
388 .remove
= p50_gpio_remove
,
392 static const struct dmi_system_id dmi_ids
[] __initconst
= {
395 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "Barco"),
396 DMI_EXACT_MATCH(DMI_PRODUCT_FAMILY
, "P50")
401 MODULE_DEVICE_TABLE(dmi
, dmi_ids
);
403 static int __init
p50_module_init(void)
405 struct resource res
= DEFINE_RES_IO(P50_GPIO_IO_PORT_BASE
, P50_PORT_CMD
+ 1);
408 if (!dmi_first_match(dmi_ids
))
411 ret
= platform_driver_register(&p50_gpio_driver
);
415 gpio_pdev
= platform_device_register_simple(DRIVER_NAME
, PLATFORM_DEVID_NONE
, &res
, 1);
416 if (IS_ERR(gpio_pdev
)) {
417 pr_err("failed registering %s: %ld\n", DRIVER_NAME
, PTR_ERR(gpio_pdev
));
418 platform_driver_unregister(&p50_gpio_driver
);
419 return PTR_ERR(gpio_pdev
);
425 static void __exit
p50_module_exit(void)
427 platform_device_unregister(gpio_pdev
);
428 platform_driver_unregister(&p50_gpio_driver
);
431 module_init(p50_module_init
);
432 module_exit(p50_module_exit
);
434 MODULE_AUTHOR("Santosh Kumar Yadav, Barco NV <santoshkumar.yadav@barco.com>");
435 MODULE_DESCRIPTION("Barco P50 identify GPIOs driver");
436 MODULE_LICENSE("GPL");