blob: b5881152750b51ac9d39353137be39516d372f19 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Duncan Lauriea12fc812016-12-13 16:43:40 -08003
Furquan Shaikh76cedd22020-05-02 10:24:23 -07004#include <acpi/acpigen_dsm.h>
Duncan Lauriea12fc812016-12-13 16:43:40 -08005#include <device/device.h>
6#include <stdint.h>
7#include <string.h>
8#include "chip.h"
Eric Lai18060d72019-04-24 14:21:04 +08009#include <gpio.h>
10#include <console/console.h>
Duncan Lauriea12fc812016-12-13 16:43:40 -080011
Julius Wernercd49cce2019-03-05 16:53:33 -080012#if CONFIG(HAVE_ACPI_TABLES)
Furquan Shaikh8220c4b2020-04-24 21:44:27 -070013static void i2c_hid_fill_dsm(const struct device *dev)
Duncan Lauriea12fc812016-12-13 16:43:40 -080014{
15 struct drivers_i2c_hid_config *config = dev->chip_info;
16 struct dsm_i2c_hid_config dsm_config = {
17 .hid_desc_reg_offset = config->hid_desc_reg_offset,
18 };
19
20 acpigen_write_dsm_i2c_hid(&dsm_config);
21}
22
Furquan Shaikh7536a392020-04-24 21:59:21 -070023static void i2c_hid_fill_ssdt_generator(const struct device *dev)
Duncan Lauriea12fc812016-12-13 16:43:40 -080024{
25 struct drivers_i2c_hid_config *config = dev->chip_info;
26 config->generic.cid = I2C_HID_CID;
27 i2c_generic_fill_ssdt(dev, &i2c_hid_fill_dsm, &config->generic);
28}
29
Aaron Durbinaa090cb2017-09-13 16:01:52 -060030static const char *i2c_hid_acpi_name(const struct device *dev)
Duncan Lauriea12fc812016-12-13 16:43:40 -080031{
32 static char name[5];
33 snprintf(name, sizeof(name), "H%03.3X", dev->path.i2c.device);
34 name[4] = '\0';
35 return name;
36}
37#endif
38
39static struct device_operations i2c_hid_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +020040 .read_resources = noop_read_resources,
41 .set_resources = noop_set_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -080042#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +020043 .acpi_name = i2c_hid_acpi_name,
44 .acpi_fill_ssdt = i2c_hid_fill_ssdt_generator,
Duncan Lauriea12fc812016-12-13 16:43:40 -080045#endif
46};
47
48static void i2c_hid_enable(struct device *dev)
49{
Naresh G Solanki69e9e712018-06-04 17:45:19 +053050 struct drivers_i2c_hid_config *config = dev->chip_info;
51
Eric Lai18060d72019-04-24 14:21:04 +080052 if (!config)
53 return;
54
55 /* Check if device is present by reading GPIO */
56 if (config->generic.device_present_gpio) {
57 int present = gpio_get(config->generic.device_present_gpio);
58 present ^= config->generic.device_present_gpio_invert;
59
60 printk(BIOS_INFO, "%s is %spresent\n",
61 dev->chip_ops->name, present ? "" : "not ");
62
63 if (!present) {
64 dev->enabled = 0;
65 return;
66 }
67 }
68
Duncan Lauriea12fc812016-12-13 16:43:40 -080069 dev->ops = &i2c_hid_ops;
Naresh G Solanki69e9e712018-06-04 17:45:19 +053070
71 if (config && config->generic.desc) {
72 dev->name = config->generic.desc;
73 }
Duncan Lauriea12fc812016-12-13 16:43:40 -080074}
75
76struct chip_operations drivers_i2c_hid_ops = {
77 CHIP_NAME("I2C HID Device")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +010078 .enable_dev = i2c_hid_enable
Duncan Lauriea12fc812016-12-13 16:43:40 -080079};