blob: 838491dc84ef2fba404254fdeaf135e9ea36ac70 [file] [log] [blame]
Duncan Laurie7db15092016-05-11 11:27:47 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2016 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <arch/acpi_device.h>
17#include <arch/acpigen.h>
18#include <console/console.h>
19#include <device/device.h>
20#include <device/path.h>
21#include <gpio.h>
22#include <stdint.h>
Duncan Laurie7db15092016-05-11 11:27:47 -070023#include "chip.h"
24
Julius Wernercd49cce2019-03-05 16:53:33 -080025#if CONFIG(HAVE_ACPI_TABLES)
Duncan Laurie7db15092016-05-11 11:27:47 -070026
27#define MAX98357A_ACPI_NAME "MAXM"
28#define MAX98357A_ACPI_HID "MX98357A"
29
30static void max98357a_fill_ssdt(struct device *dev)
31{
32 struct drivers_generic_max98357a_config *config = dev->chip_info;
Duncan Laurie366cd212016-06-29 22:34:01 -070033 const char *path;
Duncan Laurieffc99902016-07-02 19:56:06 -070034 struct acpi_dp *dp;
Duncan Laurie7db15092016-05-11 11:27:47 -070035
Duncan Laurie366cd212016-06-29 22:34:01 -070036 if (!dev->enabled || !config)
Duncan Laurie7db15092016-05-11 11:27:47 -070037 return;
38
Jacob Garberbdcb4d32019-05-27 17:10:24 -060039 const char *scope = acpi_device_scope(dev);
40 const char *name = acpi_device_name(dev);
41 if (!scope || !name)
42 return;
43
Duncan Laurie7db15092016-05-11 11:27:47 -070044 /* Device */
Jacob Garberbdcb4d32019-05-27 17:10:24 -060045 acpigen_write_scope(scope);
46 acpigen_write_device(name);
Duncan Laurie7db15092016-05-11 11:27:47 -070047 acpigen_write_name_string("_HID", MAX98357A_ACPI_HID);
48 acpigen_write_name_integer("_UID", 0);
49 acpigen_write_name_string("_DDN", dev->chip_ops->name);
Hung-Te Linb4be50c2018-09-10 10:55:49 +080050 acpigen_write_STA(acpi_device_status(dev));
Duncan Laurie7db15092016-05-11 11:27:47 -070051
52 /* Resources */
53 acpigen_write_name("_CRS");
54 acpigen_write_resourcetemplate_header();
55 acpi_device_write_gpio(&config->sdmode_gpio);
56 acpigen_write_resourcetemplate_footer();
57
58 /* _DSD for devicetree properties */
Duncan Laurie7db15092016-05-11 11:27:47 -070059 /* This points to the first pin in the first gpio entry in _CRS */
Duncan Laurie366cd212016-06-29 22:34:01 -070060 path = acpi_device_path(dev);
Duncan Laurieffc99902016-07-02 19:56:06 -070061 dp = acpi_dp_new_table("_DSD");
Furquan Shaikh028200f2016-10-04 10:53:32 -070062 acpi_dp_add_gpio(dp, "sdmode-gpio", path, 0, 0,
63 config->sdmode_gpio.polarity);
Duncan Laurieffc99902016-07-02 19:56:06 -070064 acpi_dp_add_integer(dp, "sdmode-delay", config->sdmode_delay);
65 acpi_dp_write(dp);
Duncan Laurie7db15092016-05-11 11:27:47 -070066
67 acpigen_pop_len(); /* Device */
68 acpigen_pop_len(); /* Scope */
69
70 printk(BIOS_INFO, "%s: %s\n", path, dev->chip_ops->name);
71}
72
Aaron Durbinaa090cb2017-09-13 16:01:52 -060073static const char *max98357a_acpi_name(const struct device *dev)
Duncan Laurie7db15092016-05-11 11:27:47 -070074{
75 return MAX98357A_ACPI_NAME;
76}
77#endif
78
79static struct device_operations max98357a_ops = {
80 .read_resources = DEVICE_NOOP,
81 .set_resources = DEVICE_NOOP,
82 .enable_resources = DEVICE_NOOP,
Julius Wernercd49cce2019-03-05 16:53:33 -080083#if CONFIG(HAVE_ACPI_TABLES)
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +010084 .acpi_name = max98357a_acpi_name,
85 .acpi_fill_ssdt_generator = max98357a_fill_ssdt,
Duncan Laurie7db15092016-05-11 11:27:47 -070086#endif
87};
88
89static void max98357a_enable(struct device *dev)
90{
91 struct drivers_generic_max98357a_config *config = dev->chip_info;
92
93 /* Check if device is present by reading GPIO */
94 if (config->device_present_gpio) {
95 int present = gpio_get(config->device_present_gpio);
96 present ^= config->device_present_gpio_invert;
97
98 printk(BIOS_INFO, "%s is %spresent\n",
99 dev->chip_ops->name, present ? "" : "not ");
100
101 if (!present) {
102 dev->enabled = 0;
103 return;
104 }
105 }
106
107 dev->ops = &max98357a_ops;
108}
109
110struct chip_operations drivers_generic_max98357a_ops = {
111 CHIP_NAME("Maxim Integrated 98357A Amplifier")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100112 .enable_dev = max98357a_enable
Duncan Laurie7db15092016-05-11 11:27:47 -0700113};