Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 2 | |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 3 | #include <acpi/acpi_device.h> |
| 4 | #include <acpi/acpigen.h> |
Eric Lai | b90739d | 2019-06-13 15:21:20 +0800 | [diff] [blame] | 5 | #include <console/console.h> |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 6 | #include <device/i2c_simple.h> |
| 7 | #include <device/device.h> |
Elyes Haouas | bdd03c2 | 2024-05-27 11:20:07 +0200 | [diff] [blame^] | 8 | #include <stdio.h> |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 9 | #include <string.h> |
| 10 | #include "chip.h" |
| 11 | |
Gwendal Grignou | 145ef87 | 2018-07-03 14:31:31 -0700 | [diff] [blame] | 12 | #define I2C_SX9310_ACPI_ID "STH9310" |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 13 | #define I2C_SX9310_ACPI_NAME "Semtech SX9310" |
| 14 | |
Gwendal Grignou | 689c25b | 2021-01-27 23:29:38 -0800 | [diff] [blame] | 15 | static const char * const i2c_sx9310_resolution[] = { |
| 16 | [SX9310_COARSEST] = "coarsest", |
| 17 | [SX9310_VERY_COARSE] = "very-coarse", |
| 18 | [SX9310_COARSE] = "coarse", |
| 19 | [SX9310_MEDIUM_COARSE] = "medium-coarse", |
| 20 | [SX9310_MEDIUM] = "medium", |
| 21 | [SX9310_FINE] = "fine", |
| 22 | [SX9310_VERY_FINE] = "very-fine", |
| 23 | [SX9310_FINEST] = "finest", |
| 24 | }; |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 25 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 26 | static void i2c_sx9310_fill_ssdt(const struct device *dev) |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 27 | { |
| 28 | struct drivers_i2c_sx9310_config *config = dev->chip_info; |
| 29 | const char *scope = acpi_device_scope(dev); |
| 30 | struct acpi_i2c i2c = { |
| 31 | .address = dev->path.i2c.device, |
| 32 | .mode_10bit = dev->path.i2c.mode_10bit, |
Furquan Shaikh | a7e9250 | 2018-06-22 10:15:04 -0700 | [diff] [blame] | 33 | .speed = I2C_SPEED_FAST, |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 34 | .resource = scope, |
| 35 | }; |
| 36 | struct acpi_dp *dsd; |
Gwendal Grignou | 689c25b | 2021-01-27 23:29:38 -0800 | [diff] [blame] | 37 | struct acpi_dp *combined_sensors; |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 38 | |
Karthikeyan Ramasubramanian | d1c0f95 | 2020-11-02 16:26:52 -0700 | [diff] [blame] | 39 | if (!scope || !config) |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 40 | return; |
| 41 | |
Furquan Shaikh | a7e9250 | 2018-06-22 10:15:04 -0700 | [diff] [blame] | 42 | if (config->speed) |
| 43 | i2c.speed = config->speed; |
| 44 | |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 45 | /* Device */ |
| 46 | acpigen_write_scope(scope); |
| 47 | acpigen_write_device(acpi_device_name(dev)); |
| 48 | acpigen_write_name_string("_HID", I2C_SX9310_ACPI_ID); |
| 49 | acpigen_write_name_integer("_UID", config->uid); |
| 50 | acpigen_write_name_string("_DDN", config->desc); |
Matt DeVillier | 2717206 | 2022-11-28 16:19:18 -0600 | [diff] [blame] | 51 | acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON); |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 52 | |
| 53 | /* Resources */ |
| 54 | acpigen_write_name("_CRS"); |
| 55 | acpigen_write_resourcetemplate_header(); |
| 56 | acpi_device_write_i2c(&i2c); |
Furquan Shaikh | aea9871 | 2019-04-10 10:22:06 -0700 | [diff] [blame] | 57 | |
| 58 | if (config->irq_gpio.pin_count) |
| 59 | acpi_device_write_gpio(&config->irq_gpio); |
| 60 | else |
| 61 | acpi_device_write_interrupt(&config->irq); |
| 62 | |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 63 | acpigen_write_resourcetemplate_footer(); |
| 64 | |
| 65 | /* DSD */ |
| 66 | dsd = acpi_dp_new_table("_DSD"); |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 67 | |
Gwendal Grignou | 689c25b | 2021-01-27 23:29:38 -0800 | [diff] [blame] | 68 | /* |
| 69 | * Format describe in linux kernel documentation. See |
| 70 | * https://www.kernel.org/doc/Documentation/devicetree/bindings/iio/proximity/semtech%2Csx9310.yaml |
| 71 | */ |
| 72 | acpi_dp_add_integer(dsd, "semtech,cs0-ground", config->cs0_ground); |
| 73 | acpi_dp_add_integer(dsd, "semtech,startup-sensor", |
| 74 | config->startup_sensor); |
| 75 | acpi_dp_add_integer(dsd, "semtech,proxraw-strength", |
| 76 | config->proxraw_strength); |
| 77 | acpi_dp_add_integer(dsd, "semtech,avg-pos-strength", |
| 78 | config->avg_pos_strength); |
| 79 | |
| 80 | /* Add combined_sensors package */ |
| 81 | if (config->combined_sensors_count > 0) { |
| 82 | combined_sensors = acpi_dp_new_table("semtech,combined-sensors"); |
| 83 | for (int i = 0; |
| 84 | i < config->combined_sensors_count && |
| 85 | i < MAX_COMBINED_SENSORS_ENTRIES; ++i) { |
| 86 | acpi_dp_add_integer(combined_sensors, NULL, |
| 87 | config->combined_sensors[i]); |
| 88 | } |
| 89 | acpi_dp_add_array(dsd, combined_sensors); |
| 90 | } |
| 91 | if (config->resolution && config->resolution < ARRAY_SIZE(i2c_sx9310_resolution)) |
| 92 | acpi_dp_add_string(dsd, "semtech,resolution", |
| 93 | i2c_sx9310_resolution[config->resolution]); |
| 94 | |
| 95 | acpi_dp_write(dsd); |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 96 | acpigen_pop_len(); /* Device */ |
| 97 | acpigen_pop_len(); /* Scope */ |
Eric Lai | b90739d | 2019-06-13 15:21:20 +0800 | [diff] [blame] | 98 | |
| 99 | printk(BIOS_INFO, "%s: %s at %s\n", acpi_device_path(dev), |
| 100 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 103 | static const char *i2c_sx9310_acpi_name(const struct device *dev) |
| 104 | { |
| 105 | static char name[5]; |
| 106 | |
| 107 | snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device); |
| 108 | return name; |
| 109 | } |
| 110 | |
| 111 | static struct device_operations i2c_sx9310_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 112 | .read_resources = noop_read_resources, |
| 113 | .set_resources = noop_set_resources, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 114 | .acpi_name = i2c_sx9310_acpi_name, |
| 115 | .acpi_fill_ssdt = i2c_sx9310_fill_ssdt, |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static void i2c_sx9310_enable(struct device *dev) |
| 119 | { |
| 120 | struct drivers_i2c_sx9310_config *config = dev->chip_info; |
| 121 | |
| 122 | if (!config) { |
| 123 | dev->enabled = 0; |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | dev->ops = &i2c_sx9310_ops; |
| 128 | |
| 129 | if (config->desc) |
| 130 | dev->name = config->desc; |
| 131 | } |
| 132 | |
| 133 | struct chip_operations drivers_i2c_sx9310_ops = { |
Nicholas Sudsgaard | bfb11be | 2024-01-30 09:53:46 +0900 | [diff] [blame] | 134 | .name = I2C_SX9310_ACPI_NAME, |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 135 | .enable_dev = i2c_sx9310_enable |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 136 | }; |