xref: /aosp_15_r20/external/coreboot/src/drivers/i2c/max98390/max98390.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include <acpi/acpi_device.h>
4 #include <acpi/acpigen.h>
5 #include <console/console.h>
6 #include <device/i2c.h>
7 #include <device/device.h>
8 #include <identity.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <vendorcode/google/dsm_calib.h>
12 #include "chip.h"
13 
14 #define MAX98390_ACPI_HID "MX98390"
15 
16 #define MAX98390_DP_INT(key, val) acpi_dp_add_integer(dp, "maxim," key, (val))
17 
max98390_fill_ssdt(const struct device * dev)18 static void max98390_fill_ssdt(const struct device *dev)
19 {
20 	struct drivers_i2c_max98390_config *config = dev->chip_info;
21 	const char *scope = acpi_device_scope(dev);
22 	struct acpi_i2c i2c = {
23 		.address = dev->path.i2c.device,
24 		.mode_10bit = dev->path.i2c.mode_10bit,
25 		.speed = I2C_SPEED_FAST,
26 		.resource = scope,
27 	};
28 	struct acpi_dp *dp = NULL;
29 	uint64_t r0_value, temp_value;
30 	char dsm_name[80] = {};
31 
32 	if (!scope)
33 		return;
34 
35 	/* Device */
36 	acpigen_write_scope(scope);
37 	acpigen_write_device(acpi_device_name(dev));
38 	acpigen_write_name_string("_HID", MAX98390_ACPI_HID);
39 	acpigen_write_name_integer("_UID", config->uid);
40 	acpigen_write_name_string("_DDN", config->desc);
41 	acpigen_write_STA(acpi_device_status(dev));
42 
43 	/* Resources */
44 	acpigen_write_name("_CRS");
45 	acpigen_write_resourcetemplate_header();
46 	acpi_device_write_i2c(&i2c);
47 	acpigen_write_resourcetemplate_footer();
48 
49 	/* Device Properties */
50 	if (CONFIG(GOOGLE_DSM_CALIB)) {
51 		if (get_dsm_calibration_from_key(config->r0_calib_key, &r0_value)
52 		    || get_dsm_calibration_from_key(config->temperature_calib_key,
53 						    &temp_value)) {
54 			printk(BIOS_ERR,
55 			       "Failed to get dsm_calib parameters from VPD"
56 			       " with key %s and %s\n",
57 			       config->r0_calib_key, config->temperature_calib_key);
58 		} else {
59 			dp = acpi_dp_new_table("_DSD");
60 			MAX98390_DP_INT("r0_calib", r0_value);
61 			MAX98390_DP_INT("temperature_calib", temp_value);
62 			printk(BIOS_INFO, "set dsm_calib properties\n");
63 		}
64 	}
65 
66 	if (CONFIG(GOOGLE_DSM_PARAM_FILE_NAME)) {
67 		if (config->dsm_param_file_name) {
68 			if (!dp)
69 				dp = acpi_dp_new_table("_DSD");
70 
71 			size_t chars = snprintf(dsm_name, sizeof(dsm_name), "%s_%s_%s.bin",
72 					config->dsm_param_file_name, mainboard_vendor,
73 					mainboard_part_number);
74 
75 			if (chars >= sizeof(dsm_name))
76 				printk(BIOS_ERR, "String too long in %s\n", __func__);
77 
78 			acpi_dp_add_string(dp, "maxim,dsm_param_name", dsm_name);
79 		}
80 	}
81 
82 	if (!dp)
83 		dp = acpi_dp_new_table("_DSD");
84 
85 	acpi_dp_add_integer(dp, "maxim,vmon-slot-no", config->vmon_slot_no);
86 	acpi_dp_add_integer(dp, "maxim,imon-slot-no", config->imon_slot_no);
87 
88 	if (dp)
89 		acpi_dp_write(dp);
90 
91 	acpigen_pop_len(); /* Device */
92 	acpigen_pop_len(); /* Scope */
93 
94 	printk(BIOS_INFO, "%s: %s address 0%xh\n", acpi_device_path(dev), dev->chip_ops->name,
95 	       dev->path.i2c.device);
96 }
97 
max98390_acpi_name(const struct device * dev)98 static const char *max98390_acpi_name(const struct device *dev)
99 {
100 	struct drivers_i2c_max98390_config *config = dev->chip_info;
101 	static char name[5];
102 
103 	if (config->name)
104 		return config->name;
105 
106 	snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device);
107 	return name;
108 }
109 
110 static struct device_operations max98390_ops = {
111 	.read_resources = noop_read_resources,
112 	.set_resources = noop_set_resources,
113 	.acpi_name = max98390_acpi_name,
114 	.acpi_fill_ssdt = max98390_fill_ssdt,
115 };
116 
max98390_enable(struct device * dev)117 static void max98390_enable(struct device *dev)
118 {
119 	struct drivers_i2c_max98390_config *config = dev->chip_info;
120 
121 	if (!config)
122 		return;
123 
124 	dev->ops = &max98390_ops;
125 
126 	/* Name the device as per description provided in devicetree */
127 	if (config->desc)
128 		dev->name = config->desc;
129 }
130 
131 struct chip_operations drivers_i2c_max98390_ops = {
132 	.name = "Maxim MAX98390 Codec",
133 	.enable_dev = max98390_enable
134 };
135