xref: /aosp_15_r20/external/coreboot/src/drivers/i2c/max98396/max98396.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 MAX98396_ACPI_HID "ADS8396"
13 
max98396_fill_ssdt(const struct device * dev)14 static void max98396_fill_ssdt(const struct device *dev)
15 {
16 	struct drivers_i2c_max98396_config *config = dev->chip_info;
17 	const char *scope = acpi_device_scope(dev);
18 	struct acpi_i2c i2c = {
19 		.address = dev->path.i2c.device,
20 		.mode_10bit = dev->path.i2c.mode_10bit,
21 		.speed = config->bus_speed ? : I2C_SPEED_FAST,
22 		.resource = scope,
23 	};
24 	struct acpi_dp *dp;
25 	const char *path = acpi_device_path(dev);
26 
27 	if (!scope) {
28 		printk(BIOS_ERR, "%s: dev not enabled\n", __func__);
29 		return;
30 	}
31 
32 	/* Device */
33 	acpigen_write_scope(scope);
34 	acpigen_write_device(acpi_device_name(dev));
35 	acpigen_write_name_string("_HID", MAX98396_ACPI_HID);
36 	acpigen_write_name_integer("_UID", config->uid);
37 	if (config->desc)
38 		acpigen_write_name_string("_DDN", config->desc);
39 	acpigen_write_STA(acpi_device_status(dev));
40 
41 	/* Resources */
42 	acpigen_write_name("_CRS");
43 	acpigen_write_resourcetemplate_header();
44 	acpi_device_write_i2c(&i2c);
45 	if (config->reset_gpio.pin_count)
46 		acpi_device_write_gpio(&config->reset_gpio);
47 	acpigen_write_resourcetemplate_footer();
48 
49 	/* Device Properties */
50 	dp = acpi_dp_new_table("_DSD");
51 
52 	acpi_dp_add_integer(dp, "adi,vmon-slot-no", config->vmon_slot_no);
53 	acpi_dp_add_integer(dp, "adi,imon-slot-no", config->imon_slot_no);
54 	acpi_dp_add_integer(dp, "adi,spkfb-slot-no", config->spkfb_slot_no);
55 	if (config->reset_gpio.pin_count)
56 		acpi_dp_add_gpio(dp, "reset-gpios", path,
57 				0,  /* Index = 0 */
58 				0,  /* Pin = 0 */
59 				config->reset_gpio.active_low);
60 	acpi_dp_write(dp);
61 
62 	acpigen_write_device_end();
63 	acpigen_write_scope_end();
64 
65 	printk(BIOS_INFO, "%s: %s address 0%xh\n", acpi_device_path(dev),
66 		dev->chip_ops->name, dev->path.i2c.device);
67 }
68 
max98396_acpi_name(const struct device * dev)69 static const char *max98396_acpi_name(const struct device *dev)
70 {
71 	struct drivers_i2c_max98396_config *config = dev->chip_info;
72 	static char name[ACPI_NAME_BUFFER_SIZE];
73 
74 	if (config->name && strlen(config->name) == 4)
75 		return config->name;
76 
77 	snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device);
78 	return name;
79 }
80 
81 static struct device_operations max98396_ops = {
82 	.read_resources		= noop_read_resources,
83 	.set_resources		= noop_set_resources,
84 	.acpi_name		= max98396_acpi_name,
85 	.acpi_fill_ssdt		= max98396_fill_ssdt,
86 };
87 
max98396_enable(struct device * dev)88 static void max98396_enable(struct device *dev)
89 {
90 	struct drivers_i2c_max98396_config *config = dev->chip_info;
91 
92 	dev->ops = &max98396_ops;
93 
94 	if (config->desc)
95 		dev->name = config->desc;
96 }
97 
98 struct chip_operations drivers_i2c_max98396_ops = {
99 	.name = "Maxim MAX98396 Codec",
100 	.enable_dev = max98396_enable
101 };
102