Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 2 | |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 3 | #include <acpi/acpigen_dsm.h> |
Karthikeyan Ramasubramanian | a7e6788 | 2021-02-09 10:07:47 -0700 | [diff] [blame] | 4 | #include <acpi/acpi_device.h> |
| 5 | #include <assert.h> |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 6 | #include <device/device.h> |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include "chip.h" |
Eric Lai | 18060d7 | 2019-04-24 14:21:04 +0800 | [diff] [blame] | 9 | #include <gpio.h> |
| 10 | #include <console/console.h> |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 11 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 12 | #if CONFIG(HAVE_ACPI_TABLES) |
Furquan Shaikh | 8220c4b | 2020-04-24 21:44:27 -0700 | [diff] [blame] | 13 | static void i2c_hid_fill_dsm(const struct device *dev) |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 14 | { |
| 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 Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 23 | static void i2c_hid_fill_ssdt_generator(const struct device *dev) |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 24 | { |
| 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 Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 30 | static const char *i2c_hid_acpi_name(const struct device *dev) |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 31 | { |
| 32 | static char name[5]; |
Matt DeVillier | 3d62781 | 2020-11-19 18:31:17 -0600 | [diff] [blame] | 33 | struct drivers_i2c_hid_config *config = dev->chip_info; |
| 34 | if (config->generic.name) |
| 35 | return config->generic.name; |
| 36 | |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 37 | snprintf(name, sizeof(name), "H%03.3X", dev->path.i2c.device); |
| 38 | name[4] = '\0'; |
| 39 | return name; |
| 40 | } |
| 41 | #endif |
| 42 | |
| 43 | static struct device_operations i2c_hid_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 44 | .read_resources = noop_read_resources, |
| 45 | .set_resources = noop_set_resources, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 46 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 47 | .acpi_name = i2c_hid_acpi_name, |
| 48 | .acpi_fill_ssdt = i2c_hid_fill_ssdt_generator, |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 49 | #endif |
| 50 | }; |
| 51 | |
| 52 | static void i2c_hid_enable(struct device *dev) |
| 53 | { |
Naresh G Solanki | 69e9e71 | 2018-06-04 17:45:19 +0530 | [diff] [blame] | 54 | struct drivers_i2c_hid_config *config = dev->chip_info; |
| 55 | |
Eric Lai | 18060d7 | 2019-04-24 14:21:04 +0800 | [diff] [blame] | 56 | if (!config) |
| 57 | return; |
| 58 | |
| 59 | /* Check if device is present by reading GPIO */ |
| 60 | if (config->generic.device_present_gpio) { |
| 61 | int present = gpio_get(config->generic.device_present_gpio); |
| 62 | present ^= config->generic.device_present_gpio_invert; |
| 63 | |
| 64 | printk(BIOS_INFO, "%s is %spresent\n", |
| 65 | dev->chip_ops->name, present ? "" : "not "); |
| 66 | |
| 67 | if (!present) { |
| 68 | dev->enabled = 0; |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
Karthikeyan Ramasubramanian | a7e6788 | 2021-02-09 10:07:47 -0700 | [diff] [blame] | 73 | /* |
| 74 | * Ensure that I2C HID devices use level triggered interrupts as per ACPI |
| 75 | * I2C HID requirement. Check interrupt and GPIO interrupt. |
| 76 | */ |
| 77 | if ((!config->generic.irq_gpio.pin_count && |
| 78 | config->generic.irq.mode != ACPI_IRQ_LEVEL_TRIGGERED) || |
| 79 | (config->generic.irq_gpio.pin_count && |
| 80 | config->generic.irq_gpio.irq.mode != ACPI_IRQ_LEVEL_TRIGGERED)) { |
| 81 | printk(BIOS_ERR, "%s IRQ is not level triggered.\n", config->generic.hid); |
| 82 | BUG(); |
| 83 | } |
| 84 | |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 85 | dev->ops = &i2c_hid_ops; |
Naresh G Solanki | 69e9e71 | 2018-06-04 17:45:19 +0530 | [diff] [blame] | 86 | |
| 87 | if (config && config->generic.desc) { |
| 88 | dev->name = config->generic.desc; |
| 89 | } |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | struct chip_operations drivers_i2c_hid_ops = { |
| 93 | CHIP_NAME("I2C HID Device") |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 94 | .enable_dev = i2c_hid_enable |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 95 | }; |