Duncan Laurie | 7db1509 | 2016-05-11 11:27:47 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 23 | #include <string.h> |
| 24 | #include "chip.h" |
| 25 | |
| 26 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) |
| 27 | |
| 28 | #define MAX98357A_ACPI_NAME "MAXM" |
| 29 | #define MAX98357A_ACPI_HID "MX98357A" |
| 30 | |
| 31 | static void max98357a_fill_ssdt(struct device *dev) |
| 32 | { |
| 33 | struct drivers_generic_max98357a_config *config = dev->chip_info; |
Duncan Laurie | 366cd21 | 2016-06-29 22:34:01 -0700 | [diff] [blame] | 34 | const char *path; |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 35 | struct acpi_dp *dp; |
Duncan Laurie | 7db1509 | 2016-05-11 11:27:47 -0700 | [diff] [blame] | 36 | |
Duncan Laurie | 366cd21 | 2016-06-29 22:34:01 -0700 | [diff] [blame] | 37 | if (!dev->enabled || !config) |
Duncan Laurie | 7db1509 | 2016-05-11 11:27:47 -0700 | [diff] [blame] | 38 | return; |
| 39 | |
| 40 | /* Device */ |
| 41 | acpigen_write_scope(acpi_device_scope(dev)); |
| 42 | acpigen_write_device(acpi_device_name(dev)); |
| 43 | acpigen_write_name_string("_HID", MAX98357A_ACPI_HID); |
| 44 | acpigen_write_name_integer("_UID", 0); |
| 45 | acpigen_write_name_string("_DDN", dev->chip_ops->name); |
Hung-Te Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame^] | 46 | acpigen_write_STA(acpi_device_status(dev)); |
Duncan Laurie | 7db1509 | 2016-05-11 11:27:47 -0700 | [diff] [blame] | 47 | |
| 48 | /* Resources */ |
| 49 | acpigen_write_name("_CRS"); |
| 50 | acpigen_write_resourcetemplate_header(); |
| 51 | acpi_device_write_gpio(&config->sdmode_gpio); |
| 52 | acpigen_write_resourcetemplate_footer(); |
| 53 | |
| 54 | /* _DSD for devicetree properties */ |
Duncan Laurie | 7db1509 | 2016-05-11 11:27:47 -0700 | [diff] [blame] | 55 | /* This points to the first pin in the first gpio entry in _CRS */ |
Duncan Laurie | 366cd21 | 2016-06-29 22:34:01 -0700 | [diff] [blame] | 56 | path = acpi_device_path(dev); |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 57 | dp = acpi_dp_new_table("_DSD"); |
Furquan Shaikh | 028200f | 2016-10-04 10:53:32 -0700 | [diff] [blame] | 58 | acpi_dp_add_gpio(dp, "sdmode-gpio", path, 0, 0, |
| 59 | config->sdmode_gpio.polarity); |
Duncan Laurie | ffc9990 | 2016-07-02 19:56:06 -0700 | [diff] [blame] | 60 | acpi_dp_add_integer(dp, "sdmode-delay", config->sdmode_delay); |
| 61 | acpi_dp_write(dp); |
Duncan Laurie | 7db1509 | 2016-05-11 11:27:47 -0700 | [diff] [blame] | 62 | |
| 63 | acpigen_pop_len(); /* Device */ |
| 64 | acpigen_pop_len(); /* Scope */ |
| 65 | |
| 66 | printk(BIOS_INFO, "%s: %s\n", path, dev->chip_ops->name); |
| 67 | } |
| 68 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 69 | static const char *max98357a_acpi_name(const struct device *dev) |
Duncan Laurie | 7db1509 | 2016-05-11 11:27:47 -0700 | [diff] [blame] | 70 | { |
| 71 | return MAX98357A_ACPI_NAME; |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | static struct device_operations max98357a_ops = { |
| 76 | .read_resources = DEVICE_NOOP, |
| 77 | .set_resources = DEVICE_NOOP, |
| 78 | .enable_resources = DEVICE_NOOP, |
| 79 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) |
| 80 | .acpi_name = &max98357a_acpi_name, |
| 81 | .acpi_fill_ssdt_generator = &max98357a_fill_ssdt, |
| 82 | #endif |
| 83 | }; |
| 84 | |
| 85 | static void max98357a_enable(struct device *dev) |
| 86 | { |
| 87 | struct drivers_generic_max98357a_config *config = dev->chip_info; |
| 88 | |
| 89 | /* Check if device is present by reading GPIO */ |
| 90 | if (config->device_present_gpio) { |
| 91 | int present = gpio_get(config->device_present_gpio); |
| 92 | present ^= config->device_present_gpio_invert; |
| 93 | |
| 94 | printk(BIOS_INFO, "%s is %spresent\n", |
| 95 | dev->chip_ops->name, present ? "" : "not "); |
| 96 | |
| 97 | if (!present) { |
| 98 | dev->enabled = 0; |
| 99 | return; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | dev->ops = &max98357a_ops; |
| 104 | } |
| 105 | |
| 106 | struct chip_operations drivers_generic_max98357a_ops = { |
| 107 | CHIP_NAME("Maxim Integrated 98357A Amplifier") |
| 108 | .enable_dev = &max98357a_enable |
| 109 | }; |