• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <acpi/acpigen_dsm.h>
4 #include <acpi/acpi_device.h>
5 #include <assert.h>
6 #include <device/device.h>
7 #include <stdio.h>
8 
9 #include "chip.h"
10 #include <gpio.h>
11 #include <console/console.h>
12 
13 #if CONFIG(HAVE_ACPI_TABLES)
i2c_hid_fill_dsm(const struct device * dev)14 static void i2c_hid_fill_dsm(const struct device *dev)
15 {
16 	struct drivers_i2c_hid_config *config = dev->chip_info;
17 	struct dsm_i2c_hid_config dsm_config = {
18 		.hid_desc_reg_offset = config->hid_desc_reg_offset,
19 	};
20 
21 	acpigen_write_dsm_i2c_hid(&dsm_config);
22 }
23 
i2c_hid_fill_ssdt_generator(const struct device * dev)24 static void i2c_hid_fill_ssdt_generator(const struct device *dev)
25 {
26 	struct drivers_i2c_hid_config *config = dev->chip_info;
27 	config->generic.cid = I2C_HID_CID;
28 	i2c_generic_fill_ssdt(dev, &i2c_hid_fill_dsm, &config->generic);
29 }
30 
i2c_hid_acpi_name(const struct device * dev)31 static const char *i2c_hid_acpi_name(const struct device *dev)
32 {
33 	static char name[5];
34 	struct drivers_i2c_hid_config *config = dev->chip_info;
35 	if (config->generic.name)
36 		return config->generic.name;
37 
38 	snprintf(name, sizeof(name), "H%03.3X", dev->path.i2c.device);
39 	name[4] = '\0';
40 	return name;
41 }
42 #endif
43 
44 static struct device_operations i2c_hid_ops = {
45 	.read_resources		= noop_read_resources,
46 	.set_resources		= noop_set_resources,
47 #if CONFIG(HAVE_ACPI_TABLES)
48 	.acpi_name		= i2c_hid_acpi_name,
49 	.acpi_fill_ssdt		= i2c_hid_fill_ssdt_generator,
50 #endif
51 };
52 
i2c_hid_enable(struct device * dev)53 static void i2c_hid_enable(struct device *dev)
54 {
55 	struct drivers_i2c_hid_config *config = dev->chip_info;
56 
57 	if (!config)
58 		return;
59 
60 	/* Check if device is present by reading GPIO */
61 	if (config->generic.device_present_gpio) {
62 		int present = gpio_get(config->generic.device_present_gpio);
63 		present ^= config->generic.device_present_gpio_invert;
64 
65 		printk(BIOS_INFO, "%s is %spresent\n",
66 		       dev->chip_ops->name, present ? "" : "not ");
67 
68 		if (!present) {
69 			dev->enabled = 0;
70 			return;
71 		}
72 	}
73 
74 	/*
75 	 * Ensure that I2C HID devices use level triggered interrupts as per ACPI
76 	 * I2C HID requirement. Check interrupt and GPIO interrupt.
77 	 */
78 	if ((!config->generic.irq_gpio.pin_count &&
79 	      config->generic.irq.mode != ACPI_IRQ_LEVEL_TRIGGERED) ||
80 	    (config->generic.irq_gpio.pin_count &&
81 	     config->generic.irq_gpio.irq.mode != ACPI_IRQ_LEVEL_TRIGGERED)) {
82 		printk(BIOS_ERR, "%s IRQ is not level triggered.\n", config->generic.hid);
83 		BUG();
84 	}
85 
86 	dev->ops = &i2c_hid_ops;
87 
88 	if (config && config->generic.desc) {
89 		dev->name = config->generic.desc;
90 	}
91 }
92 
93 struct chip_operations drivers_i2c_hid_ops = {
94 	.name = "I2C HID Device",
95 	.enable_dev = i2c_hid_enable
96 };
97