blob: 8157874d07a56fc2d7ba3f1cc4f3df1195af587b [file] [log] [blame]
Enrico Granata76a1f492018-06-20 13:01:45 -07001/*
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>
Eric Laib90739d2019-06-13 15:21:20 +080018#include <console/console.h>
Enrico Granata76a1f492018-06-20 13:01:45 -070019#include <device/i2c_simple.h>
20#include <device/device.h>
21#include <device/path.h>
22#include <stdint.h>
23#include <string.h>
24#include "chip.h"
25
Gwendal Grignou145ef872018-07-03 14:31:31 -070026#define I2C_SX9310_ACPI_ID "STH9310"
Enrico Granata76a1f492018-06-20 13:01:45 -070027#define I2C_SX9310_ACPI_NAME "Semtech SX9310"
28
29#define REGISTER(NAME) acpi_dp_add_integer(dsd, \
30 I2C_SX9310_ACPI_ID "," #NAME, \
31 config->NAME)
32
33static void i2c_sx9310_fill_ssdt(struct device *dev)
34{
35 struct drivers_i2c_sx9310_config *config = dev->chip_info;
36 const char *scope = acpi_device_scope(dev);
37 struct acpi_i2c i2c = {
38 .address = dev->path.i2c.device,
39 .mode_10bit = dev->path.i2c.mode_10bit,
Furquan Shaikha7e92502018-06-22 10:15:04 -070040 .speed = I2C_SPEED_FAST,
Enrico Granata76a1f492018-06-20 13:01:45 -070041 .resource = scope,
42 };
43 struct acpi_dp *dsd;
44
45 if (!dev->enabled || !scope || !config)
46 return;
47
Furquan Shaikha7e92502018-06-22 10:15:04 -070048 if (config->speed)
49 i2c.speed = config->speed;
50
Enrico Granata76a1f492018-06-20 13:01:45 -070051 /* Device */
52 acpigen_write_scope(scope);
53 acpigen_write_device(acpi_device_name(dev));
54 acpigen_write_name_string("_HID", I2C_SX9310_ACPI_ID);
55 acpigen_write_name_integer("_UID", config->uid);
56 acpigen_write_name_string("_DDN", config->desc);
Hung-Te Linb4be50c2018-09-10 10:55:49 +080057 acpigen_write_STA(acpi_device_status(dev));
Enrico Granata76a1f492018-06-20 13:01:45 -070058
59 /* Resources */
60 acpigen_write_name("_CRS");
61 acpigen_write_resourcetemplate_header();
62 acpi_device_write_i2c(&i2c);
Furquan Shaikhaea98712019-04-10 10:22:06 -070063
64 if (config->irq_gpio.pin_count)
65 acpi_device_write_gpio(&config->irq_gpio);
66 else
67 acpi_device_write_interrupt(&config->irq);
68
Enrico Granata76a1f492018-06-20 13:01:45 -070069 acpigen_write_resourcetemplate_footer();
70
71 /* DSD */
72 dsd = acpi_dp_new_table("_DSD");
73#include "registers.h"
74 acpi_dp_write(dsd);
75
76 acpigen_pop_len(); /* Device */
77 acpigen_pop_len(); /* Scope */
Eric Laib90739d2019-06-13 15:21:20 +080078
79 printk(BIOS_INFO, "%s: %s at %s\n", acpi_device_path(dev),
80 config->desc ? : dev->chip_ops->name, dev_path(dev));
Enrico Granata76a1f492018-06-20 13:01:45 -070081}
82
83#undef REGISTER
84
85static const char *i2c_sx9310_acpi_name(const struct device *dev)
86{
87 static char name[5];
88
89 snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device);
90 return name;
91}
92
93static struct device_operations i2c_sx9310_ops = {
94 .read_resources = DEVICE_NOOP,
95 .set_resources = DEVICE_NOOP,
96 .enable_resources = DEVICE_NOOP,
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +010097 .acpi_name = i2c_sx9310_acpi_name,
98 .acpi_fill_ssdt_generator = i2c_sx9310_fill_ssdt,
Enrico Granata76a1f492018-06-20 13:01:45 -070099};
100
101static void i2c_sx9310_enable(struct device *dev)
102{
103 struct drivers_i2c_sx9310_config *config = dev->chip_info;
104
105 if (!config) {
106 dev->enabled = 0;
107 return;
108 }
109
110 dev->ops = &i2c_sx9310_ops;
111
112 if (config->desc)
113 dev->name = config->desc;
114}
115
116struct chip_operations drivers_i2c_sx9310_ops = {
117 CHIP_NAME(I2C_SX9310_ACPI_NAME)
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100118 .enable_dev = i2c_sx9310_enable
Enrico Granata76a1f492018-06-20 13:01:45 -0700119};