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> |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 4 | #include <device/device.h> |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 5 | #include <string.h> |
| 6 | #include "chip.h" |
Eric Lai | 18060d7 | 2019-04-24 14:21:04 +0800 | [diff] [blame] | 7 | #include <gpio.h> |
| 8 | #include <console/console.h> |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 9 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 10 | #if CONFIG(HAVE_ACPI_TABLES) |
Furquan Shaikh | 8220c4b | 2020-04-24 21:44:27 -0700 | [diff] [blame] | 11 | static void i2c_hid_fill_dsm(const struct device *dev) |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 12 | { |
| 13 | struct drivers_i2c_hid_config *config = dev->chip_info; |
| 14 | struct dsm_i2c_hid_config dsm_config = { |
| 15 | .hid_desc_reg_offset = config->hid_desc_reg_offset, |
| 16 | }; |
| 17 | |
| 18 | acpigen_write_dsm_i2c_hid(&dsm_config); |
| 19 | } |
| 20 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 21 | static void i2c_hid_fill_ssdt_generator(const struct device *dev) |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 22 | { |
| 23 | struct drivers_i2c_hid_config *config = dev->chip_info; |
| 24 | config->generic.cid = I2C_HID_CID; |
| 25 | i2c_generic_fill_ssdt(dev, &i2c_hid_fill_dsm, &config->generic); |
| 26 | } |
| 27 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 28 | static const char *i2c_hid_acpi_name(const struct device *dev) |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 29 | { |
| 30 | static char name[5]; |
Matt DeVillier | 3d62781 | 2020-11-19 18:31:17 -0600 | [diff] [blame^] | 31 | struct drivers_i2c_hid_config *config = dev->chip_info; |
| 32 | if (config->generic.name) |
| 33 | return config->generic.name; |
| 34 | |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 35 | snprintf(name, sizeof(name), "H%03.3X", dev->path.i2c.device); |
| 36 | name[4] = '\0'; |
| 37 | return name; |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | static struct device_operations i2c_hid_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 42 | .read_resources = noop_read_resources, |
| 43 | .set_resources = noop_set_resources, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 44 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 45 | .acpi_name = i2c_hid_acpi_name, |
| 46 | .acpi_fill_ssdt = i2c_hid_fill_ssdt_generator, |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 47 | #endif |
| 48 | }; |
| 49 | |
| 50 | static void i2c_hid_enable(struct device *dev) |
| 51 | { |
Naresh G Solanki | 69e9e71 | 2018-06-04 17:45:19 +0530 | [diff] [blame] | 52 | struct drivers_i2c_hid_config *config = dev->chip_info; |
| 53 | |
Eric Lai | 18060d7 | 2019-04-24 14:21:04 +0800 | [diff] [blame] | 54 | if (!config) |
| 55 | return; |
| 56 | |
| 57 | /* Check if device is present by reading GPIO */ |
| 58 | if (config->generic.device_present_gpio) { |
| 59 | int present = gpio_get(config->generic.device_present_gpio); |
| 60 | present ^= config->generic.device_present_gpio_invert; |
| 61 | |
| 62 | printk(BIOS_INFO, "%s is %spresent\n", |
| 63 | dev->chip_ops->name, present ? "" : "not "); |
| 64 | |
| 65 | if (!present) { |
| 66 | dev->enabled = 0; |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 71 | dev->ops = &i2c_hid_ops; |
Naresh G Solanki | 69e9e71 | 2018-06-04 17:45:19 +0530 | [diff] [blame] | 72 | |
| 73 | if (config && config->generic.desc) { |
| 74 | dev->name = config->generic.desc; |
| 75 | } |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | struct chip_operations drivers_i2c_hid_ops = { |
| 79 | CHIP_NAME("I2C HID Device") |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 80 | .enable_dev = i2c_hid_enable |
Duncan Laurie | a12fc81 | 2016-12-13 16:43:40 -0800 | [diff] [blame] | 81 | }; |