xref: /aosp_15_r20/external/coreboot/src/drivers/i2c/sx9310/sx9310.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <acpi/acpi_device.h>
4 #include <acpi/acpigen.h>
5 #include <console/console.h>
6 #include <device/i2c_simple.h>
7 #include <device/device.h>
8 #include <stdio.h>
9 
10 #include "chip.h"
11 
12 #define I2C_SX9310_ACPI_ID	"STH9310"
13 #define I2C_SX9310_ACPI_NAME	"Semtech SX9310"
14 
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 };
25 
i2c_sx9310_fill_ssdt(const struct device * dev)26 static void i2c_sx9310_fill_ssdt(const struct device *dev)
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,
33 		.speed = I2C_SPEED_FAST,
34 		.resource = scope,
35 	};
36 	struct acpi_dp *dsd;
37 	struct acpi_dp *combined_sensors;
38 
39 	if (!scope || !config)
40 		return;
41 
42 	if (config->speed)
43 		i2c.speed = config->speed;
44 
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);
51 	acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
52 
53 	/* Resources */
54 	acpigen_write_name("_CRS");
55 	acpigen_write_resourcetemplate_header();
56 	acpi_device_write_i2c(&i2c);
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 
63 	acpigen_write_resourcetemplate_footer();
64 
65 	/* DSD */
66 	dsd = acpi_dp_new_table("_DSD");
67 
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);
96 	acpigen_pop_len(); /* Device */
97 	acpigen_pop_len(); /* Scope */
98 
99 	printk(BIOS_INFO, "%s: %s at %s\n", acpi_device_path(dev),
100 	       config->desc ? : dev->chip_ops->name, dev_path(dev));
101 }
102 
i2c_sx9310_acpi_name(const struct device * dev)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 = {
112 	.read_resources		= noop_read_resources,
113 	.set_resources		= noop_set_resources,
114 	.acpi_name		= i2c_sx9310_acpi_name,
115 	.acpi_fill_ssdt		= i2c_sx9310_fill_ssdt,
116 };
117 
i2c_sx9310_enable(struct device * dev)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 = {
134 	.name = I2C_SX9310_ACPI_NAME,
135 	.enable_dev = i2c_sx9310_enable
136 };
137