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); |
| 46 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 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"); |
| 58 | acpi_dp_add_gpio(dp, "sdmode-gpio", path, 0, 0, 0); |
| 59 | acpi_dp_add_integer(dp, "sdmode-delay", config->sdmode_delay); |
| 60 | acpi_dp_write(dp); |
Duncan Laurie | 7db1509 | 2016-05-11 11:27:47 -0700 | [diff] [blame] | 61 | |
| 62 | acpigen_pop_len(); /* Device */ |
| 63 | acpigen_pop_len(); /* Scope */ |
| 64 | |
| 65 | printk(BIOS_INFO, "%s: %s\n", path, dev->chip_ops->name); |
| 66 | } |
| 67 | |
| 68 | static const char *max98357a_acpi_name(struct device *dev) |
| 69 | { |
| 70 | return MAX98357A_ACPI_NAME; |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | static struct device_operations max98357a_ops = { |
| 75 | .read_resources = DEVICE_NOOP, |
| 76 | .set_resources = DEVICE_NOOP, |
| 77 | .enable_resources = DEVICE_NOOP, |
| 78 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) |
| 79 | .acpi_name = &max98357a_acpi_name, |
| 80 | .acpi_fill_ssdt_generator = &max98357a_fill_ssdt, |
| 81 | #endif |
| 82 | }; |
| 83 | |
| 84 | static void max98357a_enable(struct device *dev) |
| 85 | { |
| 86 | struct drivers_generic_max98357a_config *config = dev->chip_info; |
| 87 | |
| 88 | /* Check if device is present by reading GPIO */ |
| 89 | if (config->device_present_gpio) { |
| 90 | int present = gpio_get(config->device_present_gpio); |
| 91 | present ^= config->device_present_gpio_invert; |
| 92 | |
| 93 | printk(BIOS_INFO, "%s is %spresent\n", |
| 94 | dev->chip_ops->name, present ? "" : "not "); |
| 95 | |
| 96 | if (!present) { |
| 97 | dev->enabled = 0; |
| 98 | return; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | dev->ops = &max98357a_ops; |
| 103 | } |
| 104 | |
| 105 | struct chip_operations drivers_generic_max98357a_ops = { |
| 106 | CHIP_NAME("Maxim Integrated 98357A Amplifier") |
| 107 | .enable_dev = &max98357a_enable |
| 108 | }; |