blob: b9c270b0c594db84db88f16a5a71c1792880755a [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Duncan Laurie7db15092016-05-11 11:27:47 -07003
4#include <arch/acpi_device.h>
5#include <arch/acpigen.h>
6#include <console/console.h>
7#include <device/device.h>
8#include <device/path.h>
9#include <gpio.h>
10#include <stdint.h>
Duncan Laurie7db15092016-05-11 11:27:47 -070011#include "chip.h"
12
Julius Wernercd49cce2019-03-05 16:53:33 -080013#if CONFIG(HAVE_ACPI_TABLES)
Duncan Laurie7db15092016-05-11 11:27:47 -070014
15#define MAX98357A_ACPI_NAME "MAXM"
Duncan Laurie7db15092016-05-11 11:27:47 -070016
Furquan Shaikh7536a392020-04-24 21:59:21 -070017static void max98357a_fill_ssdt(const struct device *dev)
Duncan Laurie7db15092016-05-11 11:27:47 -070018{
19 struct drivers_generic_max98357a_config *config = dev->chip_info;
Duncan Laurie366cd212016-06-29 22:34:01 -070020 const char *path;
Duncan Laurieffc99902016-07-02 19:56:06 -070021 struct acpi_dp *dp;
Duncan Laurie7db15092016-05-11 11:27:47 -070022
Duncan Laurie366cd212016-06-29 22:34:01 -070023 if (!dev->enabled || !config)
Duncan Laurie7db15092016-05-11 11:27:47 -070024 return;
25
Jacob Garberbdcb4d32019-05-27 17:10:24 -060026 const char *scope = acpi_device_scope(dev);
27 const char *name = acpi_device_name(dev);
28 if (!scope || !name)
29 return;
30
Duncan Laurie7db15092016-05-11 11:27:47 -070031 /* Device */
Jacob Garberbdcb4d32019-05-27 17:10:24 -060032 acpigen_write_scope(scope);
33 acpigen_write_device(name);
Aamir Bohraa1c82c52020-03-16 18:57:48 +053034
35 if (!config->hid) {
36 printk(BIOS_ERR, "%s: ERROR: _HID required\n", dev_path(dev));
37 return;
38 }
39
40 acpigen_write_name_string("_HID", config->hid);
Duncan Laurie7db15092016-05-11 11:27:47 -070041 acpigen_write_name_integer("_UID", 0);
42 acpigen_write_name_string("_DDN", dev->chip_ops->name);
Hung-Te Linb4be50c2018-09-10 10:55:49 +080043 acpigen_write_STA(acpi_device_status(dev));
Duncan Laurie7db15092016-05-11 11:27:47 -070044
45 /* Resources */
46 acpigen_write_name("_CRS");
47 acpigen_write_resourcetemplate_header();
48 acpi_device_write_gpio(&config->sdmode_gpio);
49 acpigen_write_resourcetemplate_footer();
50
51 /* _DSD for devicetree properties */
Duncan Laurie7db15092016-05-11 11:27:47 -070052 /* This points to the first pin in the first gpio entry in _CRS */
Duncan Laurie366cd212016-06-29 22:34:01 -070053 path = acpi_device_path(dev);
Duncan Laurieffc99902016-07-02 19:56:06 -070054 dp = acpi_dp_new_table("_DSD");
Furquan Shaikh028200f2016-10-04 10:53:32 -070055 acpi_dp_add_gpio(dp, "sdmode-gpio", path, 0, 0,
56 config->sdmode_gpio.polarity);
Duncan Laurieffc99902016-07-02 19:56:06 -070057 acpi_dp_add_integer(dp, "sdmode-delay", config->sdmode_delay);
58 acpi_dp_write(dp);
Duncan Laurie7db15092016-05-11 11:27:47 -070059
60 acpigen_pop_len(); /* Device */
61 acpigen_pop_len(); /* Scope */
62
63 printk(BIOS_INFO, "%s: %s\n", path, dev->chip_ops->name);
64}
65
Aaron Durbinaa090cb2017-09-13 16:01:52 -060066static const char *max98357a_acpi_name(const struct device *dev)
Duncan Laurie7db15092016-05-11 11:27:47 -070067{
68 return MAX98357A_ACPI_NAME;
69}
70#endif
71
72static struct device_operations max98357a_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +020073 .read_resources = noop_read_resources,
74 .set_resources = noop_set_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -080075#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +020076 .acpi_name = max98357a_acpi_name,
77 .acpi_fill_ssdt = max98357a_fill_ssdt,
Duncan Laurie7db15092016-05-11 11:27:47 -070078#endif
79};
80
81static void max98357a_enable(struct device *dev)
82{
83 struct drivers_generic_max98357a_config *config = dev->chip_info;
84
85 /* Check if device is present by reading GPIO */
86 if (config->device_present_gpio) {
87 int present = gpio_get(config->device_present_gpio);
88 present ^= config->device_present_gpio_invert;
89
90 printk(BIOS_INFO, "%s is %spresent\n",
91 dev->chip_ops->name, present ? "" : "not ");
92
93 if (!present) {
94 dev->enabled = 0;
95 return;
96 }
97 }
98
99 dev->ops = &max98357a_ops;
100}
101
102struct chip_operations drivers_generic_max98357a_ops = {
103 CHIP_NAME("Maxim Integrated 98357A Amplifier")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100104 .enable_dev = max98357a_enable
Duncan Laurie7db15092016-05-11 11:27:47 -0700105};