Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2018 Google LLC |
| 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 <device/i2c_simple.h> |
| 19 | #include <device/device.h> |
| 20 | #include <device/path.h> |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include "chip.h" |
| 24 | |
Gwendal Grignou | 145ef87 | 2018-07-03 14:31:31 -0700 | [diff] [blame] | 25 | #define I2C_SX9310_ACPI_ID "STH9310" |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 26 | #define I2C_SX9310_ACPI_NAME "Semtech SX9310" |
| 27 | |
| 28 | #define REGISTER(NAME) acpi_dp_add_integer(dsd, \ |
| 29 | I2C_SX9310_ACPI_ID "," #NAME, \ |
| 30 | config->NAME) |
| 31 | |
| 32 | static void i2c_sx9310_fill_ssdt(struct device *dev) |
| 33 | { |
| 34 | struct drivers_i2c_sx9310_config *config = dev->chip_info; |
| 35 | const char *scope = acpi_device_scope(dev); |
| 36 | struct acpi_i2c i2c = { |
| 37 | .address = dev->path.i2c.device, |
| 38 | .mode_10bit = dev->path.i2c.mode_10bit, |
Furquan Shaikh | a7e9250 | 2018-06-22 10:15:04 -0700 | [diff] [blame] | 39 | .speed = I2C_SPEED_FAST, |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 40 | .resource = scope, |
| 41 | }; |
| 42 | struct acpi_dp *dsd; |
| 43 | |
| 44 | if (!dev->enabled || !scope || !config) |
| 45 | return; |
| 46 | |
Furquan Shaikh | a7e9250 | 2018-06-22 10:15:04 -0700 | [diff] [blame] | 47 | if (config->speed) |
| 48 | i2c.speed = config->speed; |
| 49 | |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 50 | /* Device */ |
| 51 | acpigen_write_scope(scope); |
| 52 | acpigen_write_device(acpi_device_name(dev)); |
| 53 | acpigen_write_name_string("_HID", I2C_SX9310_ACPI_ID); |
| 54 | acpigen_write_name_integer("_UID", config->uid); |
| 55 | acpigen_write_name_string("_DDN", config->desc); |
Hung-Te Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame] | 56 | acpigen_write_STA(acpi_device_status(dev)); |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 57 | |
| 58 | /* Resources */ |
| 59 | acpigen_write_name("_CRS"); |
| 60 | acpigen_write_resourcetemplate_header(); |
| 61 | acpi_device_write_i2c(&i2c); |
Furquan Shaikh | aea9871 | 2019-04-10 10:22:06 -0700 | [diff] [blame] | 62 | |
| 63 | if (config->irq_gpio.pin_count) |
| 64 | acpi_device_write_gpio(&config->irq_gpio); |
| 65 | else |
| 66 | acpi_device_write_interrupt(&config->irq); |
| 67 | |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 68 | acpigen_write_resourcetemplate_footer(); |
| 69 | |
| 70 | /* DSD */ |
| 71 | dsd = acpi_dp_new_table("_DSD"); |
| 72 | #include "registers.h" |
| 73 | acpi_dp_write(dsd); |
| 74 | |
| 75 | acpigen_pop_len(); /* Device */ |
| 76 | acpigen_pop_len(); /* Scope */ |
| 77 | } |
| 78 | |
| 79 | #undef REGISTER |
| 80 | |
| 81 | static const char *i2c_sx9310_acpi_name(const struct device *dev) |
| 82 | { |
| 83 | static char name[5]; |
| 84 | |
| 85 | snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device); |
| 86 | return name; |
| 87 | } |
| 88 | |
| 89 | static struct device_operations i2c_sx9310_ops = { |
| 90 | .read_resources = DEVICE_NOOP, |
| 91 | .set_resources = DEVICE_NOOP, |
| 92 | .enable_resources = DEVICE_NOOP, |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 93 | .acpi_name = i2c_sx9310_acpi_name, |
| 94 | .acpi_fill_ssdt_generator = i2c_sx9310_fill_ssdt, |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static void i2c_sx9310_enable(struct device *dev) |
| 98 | { |
| 99 | struct drivers_i2c_sx9310_config *config = dev->chip_info; |
| 100 | |
| 101 | if (!config) { |
| 102 | dev->enabled = 0; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | dev->ops = &i2c_sx9310_ops; |
| 107 | |
| 108 | if (config->desc) |
| 109 | dev->name = config->desc; |
| 110 | } |
| 111 | |
| 112 | struct chip_operations drivers_i2c_sx9310_ops = { |
| 113 | CHIP_NAME(I2C_SX9310_ACPI_NAME) |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 114 | .enable_dev = i2c_sx9310_enable |
Enrico Granata | 76a1f49 | 2018-06-20 13:01:45 -0700 | [diff] [blame] | 115 | }; |