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
14 #include <linux/delay.h>
15 #include <linux/dmi.h>
16 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/leds.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio_keys.h>
23 #include <linux/gpio/driver.h>
24 #include <linux/gpio/machine.h>
25 #include <linux/input.h>
28 #define DRIVER_NAME "barco-p50-gpio"
31 #define P50_GPIO_LINE_LED 0
32 #define P50_GPIO_LINE_BTN 1
35 #define P50_GPIO_IO_PORT_BASE 0x299
37 #define P50_PORT_DATA 0x00
38 #define P50_PORT_CMD 0x01
40 #define P50_STATUS_OBF 0x01 /* EC output buffer full */
41 #define P50_STATUS_IBF 0x02 /* EC input buffer full */
43 #define P50_CMD_READ 0xa0
44 #define P50_CMD_WRITE 0x50
46 /* EC mailbox registers */
47 #define P50_MBOX_REG_CMD 0x00
48 #define P50_MBOX_REG_STATUS 0x01
49 #define P50_MBOX_REG_PARAM 0x02
50 #define P50_MBOX_REG_DATA 0x03
52 #define P50_MBOX_CMD_READ_GPIO 0x11
53 #define P50_MBOX_CMD_WRITE_GPIO 0x12
54 #define P50_MBOX_CMD_CLEAR 0xff
56 #define P50_MBOX_STATUS_SUCCESS 0x01
58 #define P50_MBOX_PARAM_LED 0x12
59 #define P50_MBOX_PARAM_BTN 0x13
66 struct platform_device *leds_pdev;
67 struct platform_device *keys_pdev;
70 static struct platform_device *gpio_pdev;
72 static int gpio_params[] = {
73 [P50_GPIO_LINE_LED] = P50_MBOX_PARAM_LED,
74 [P50_GPIO_LINE_BTN] = P50_MBOX_PARAM_BTN,
77 static const char * const gpio_names[] = {
78 [P50_GPIO_LINE_LED] = "identify-led",
79 [P50_GPIO_LINE_BTN] = "identify-button",
83 static struct gpiod_lookup_table p50_gpio_led_table = {
84 .dev_id = "leds-gpio",
86 GPIO_LOOKUP_IDX(DRIVER_NAME, P50_GPIO_LINE_LED, NULL, 0, GPIO_ACTIVE_HIGH),
92 static struct gpio_led leds[] = {
93 { .name = "identify" }
96 static struct gpio_led_platform_data leds_pdata = {
97 .num_leds = ARRAY_SIZE(leds),
102 static struct gpio_keys_button buttons[] = {
105 .gpio = P50_GPIO_LINE_BTN,
112 static struct gpio_keys_platform_data keys_pdata = {
114 .nbuttons = ARRAY_SIZE(buttons),
115 .poll_interval = 100,
121 /* low level access routines */
123 static int p50_wait_ec(struct p50_gpio *p50, int mask, int expected)
127 for (i = 0; i < 100; i++) {
128 val = inb(p50->base + P50_PORT_CMD) & mask;
131 usleep_range(500, 2000);
134 dev_err(p50->gc.parent, "Timed out waiting for EC (0x%x)\n", val);
139 static int p50_read_mbox_reg(struct p50_gpio *p50, int reg)
143 ret = p50_wait_ec(p50, P50_STATUS_IBF, 0);
147 /* clear output buffer flag, prevent unfinished commands */
148 inb(p50->base + P50_PORT_DATA);
151 outb(P50_CMD_READ | reg, p50->base + P50_PORT_CMD);
153 ret = p50_wait_ec(p50, P50_STATUS_OBF, P50_STATUS_OBF);
157 return inb(p50->base + P50_PORT_DATA);
160 static int p50_write_mbox_reg(struct p50_gpio *p50, int reg, int val)
164 ret = p50_wait_ec(p50, P50_STATUS_IBF, 0);
169 outb(P50_CMD_WRITE | reg, p50->base + P50_PORT_CMD);
171 ret = p50_wait_ec(p50, P50_STATUS_IBF, 0);
176 outb(val, p50->base + P50_PORT_DATA);
184 static int p50_wait_mbox_idle(struct p50_gpio *p50)
188 for (i = 0; i < 1000; i++) {
189 val = p50_read_mbox_reg(p50, P50_MBOX_REG_CMD);
190 /* cmd is 0 when idle */
194 usleep_range(500, 2000);
197 dev_err(p50->gc.parent, "Timed out waiting for EC mbox idle (CMD: 0x%x)\n", val);
202 static int p50_send_mbox_cmd(struct p50_gpio *p50, int cmd, int param, int data)
206 ret = p50_wait_mbox_idle(p50);
210 ret = p50_write_mbox_reg(p50, P50_MBOX_REG_DATA, data);
214 ret = p50_write_mbox_reg(p50, P50_MBOX_REG_PARAM, param);
218 ret = p50_write_mbox_reg(p50, P50_MBOX_REG_CMD, cmd);
222 ret = p50_wait_mbox_idle(p50);
226 ret = p50_read_mbox_reg(p50, P50_MBOX_REG_STATUS);
230 if (ret == P50_MBOX_STATUS_SUCCESS)
233 dev_err(p50->gc.parent, "Mbox command failed (CMD=0x%x STAT=0x%x PARAM=0x%x DATA=0x%x)\n",
234 cmd, ret, param, data);
242 static int p50_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
245 case P50_GPIO_LINE_BTN:
246 return GPIO_LINE_DIRECTION_IN;
248 case P50_GPIO_LINE_LED:
249 return GPIO_LINE_DIRECTION_OUT;
256 static int p50_gpio_get(struct gpio_chip *gc, unsigned int offset)
258 struct p50_gpio *p50 = gpiochip_get_data(gc);
261 mutex_lock(&p50->lock);
263 ret = p50_send_mbox_cmd(p50, P50_MBOX_CMD_READ_GPIO, gpio_params[offset], 0);
265 ret = p50_read_mbox_reg(p50, P50_MBOX_REG_DATA);
267 mutex_unlock(&p50->lock);
272 static void p50_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
274 struct p50_gpio *p50 = gpiochip_get_data(gc);
276 mutex_lock(&p50->lock);
278 p50_send_mbox_cmd(p50, P50_MBOX_CMD_WRITE_GPIO, gpio_params[offset], value);
280 mutex_unlock(&p50->lock);
283 static int p50_gpio_probe(struct platform_device *pdev)
285 struct p50_gpio *p50;
286 struct resource *res;
289 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
291 dev_err(&pdev->dev, "Cannot get I/O ports\n");
295 if (!devm_request_region(&pdev->dev, res->start, resource_size(res), pdev->name)) {
296 dev_err(&pdev->dev, "Unable to reserve I/O region\n");
300 p50 = devm_kzalloc(&pdev->dev, sizeof(*p50), GFP_KERNEL);
304 platform_set_drvdata(pdev, p50);
305 mutex_init(&p50->lock);
306 p50->base = res->start;
307 p50->gc.owner = THIS_MODULE;
308 p50->gc.parent = &pdev->dev;
309 p50->gc.label = dev_name(&pdev->dev);
310 p50->gc.ngpio = ARRAY_SIZE(gpio_names);
311 p50->gc.names = gpio_names;
312 p50->gc.can_sleep = true;
314 p50->gc.get_direction = p50_gpio_get_direction;
315 p50->gc.get = p50_gpio_get;
316 p50->gc.set = p50_gpio_set;
320 ret = p50_wait_mbox_idle(p50);
324 ret = p50_write_mbox_reg(p50, P50_MBOX_REG_CMD, P50_MBOX_CMD_CLEAR);
328 ret = p50_wait_mbox_idle(p50);
333 ret = devm_gpiochip_add_data(&pdev->dev, &p50->gc, p50);
335 dev_err(&pdev->dev, "Could not register gpiochip: %d\n", ret);
339 gpiod_add_lookup_table(&p50_gpio_led_table);
341 p50->leds_pdev = platform_device_register_data(&pdev->dev,
342 "leds-gpio", PLATFORM_DEVID_NONE, &leds_pdata, sizeof(leds_pdata));
344 if (IS_ERR(p50->leds_pdev)) {
345 ret = PTR_ERR(p50->leds_pdev);
346 dev_err(&pdev->dev, "Could not register leds-gpio: %d\n", ret);
350 /* gpio-keys-polled uses old-style gpio interface, pass the right identifier */
351 buttons[0].gpio += p50->gc.base;
354 platform_device_register_data(&pdev->dev, "gpio-keys-polled",
356 &keys_pdata, sizeof(keys_pdata));
358 if (IS_ERR(p50->keys_pdev)) {
359 ret = PTR_ERR(p50->keys_pdev);
360 dev_err(&pdev->dev, "Could not register gpio-keys-polled: %d\n", ret);
367 platform_device_unregister(p50->leds_pdev);
369 gpiod_remove_lookup_table(&p50_gpio_led_table);
374 static int p50_gpio_remove(struct platform_device *pdev)
376 struct p50_gpio *p50 = platform_get_drvdata(pdev);
378 platform_device_unregister(p50->keys_pdev);
379 platform_device_unregister(p50->leds_pdev);
381 gpiod_remove_lookup_table(&p50_gpio_led_table);
386 static struct platform_driver p50_gpio_driver = {
390 .probe = p50_gpio_probe,
391 .remove = p50_gpio_remove,
395 static const struct dmi_system_id dmi_ids[] __initconst = {
398 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Barco"),
399 DMI_EXACT_MATCH(DMI_PRODUCT_FAMILY, "P50")
404 MODULE_DEVICE_TABLE(dmi, dmi_ids);
406 static int __init p50_module_init(void)
408 struct resource res = DEFINE_RES_IO(P50_GPIO_IO_PORT_BASE, P50_PORT_CMD + 1);
410 if (!dmi_first_match(dmi_ids))
413 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");