blob: df154f4ab73ea1fab6954f3f850f2811254be172 [file] [log] [blame]
Seunghwan Kim14a1c272021-11-08 15:55:01 +09001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <acpi/acpi_device.h>
4#include <acpi/acpigen.h>
5#include <console/console.h>
6#include <device/i2c_simple.h>
7#include <device/device.h>
Seunghwan Kim14a1c272021-11-08 15:55:01 +09008#include <string.h>
9#include "chip.h"
10
11#define I2C_SX9360_ACPI_ID "STH9360"
12#define I2C_SX9360_CHIP_NAME "Semtech SX9360"
13
14static void i2c_sx9360_fill_ssdt(const struct device *dev)
15{
16 struct drivers_i2c_sx9360_config *config = dev->chip_info;
17 const char *scope = acpi_device_scope(dev);
18 struct acpi_i2c i2c = {
19 .address = dev->path.i2c.device,
20 .mode_10bit = dev->path.i2c.mode_10bit,
21 .speed = I2C_SPEED_FAST,
22 .resource = scope,
23 };
24 struct acpi_dp *dsd;
25
26 if (!scope || !config)
27 return;
28
29 if (config->speed)
30 i2c.speed = config->speed;
31
32 /* Device */
33 acpigen_write_scope(scope);
34 acpigen_write_device(acpi_device_name(dev));
35 acpigen_write_name_string("_HID", I2C_SX9360_ACPI_ID);
36 acpigen_write_name_integer("_UID", config->uid);
37 acpigen_write_name_string("_DDN", config->desc);
38 acpigen_write_STA(acpi_device_status(dev));
39
40 /* Resources */
41 acpigen_write_name("_CRS");
42 acpigen_write_resourcetemplate_header();
43 acpi_device_write_i2c(&i2c);
44
45 if (config->irq_gpio.pin_count)
46 acpi_device_write_gpio(&config->irq_gpio);
47 else
48 acpi_device_write_interrupt(&config->irq);
49
50 acpigen_write_resourcetemplate_footer();
51
52 /* DSD */
53 dsd = acpi_dp_new_table("_DSD");
54
55 /*
56 * Format described in linux kernel documentation. See
57 * https://www.kernel.org/doc/Documentation/devicetree/bindings/iio/proximity/semtech%2Csx9360.yaml
58 */
59 acpi_dp_add_integer(dsd, "semtech,proxraw-strength",
60 config->proxraw_strength);
61 acpi_dp_add_integer(dsd, "semtech,avg-pos-strength",
62 config->avg_pos_strength);
63 acpi_dp_add_integer(dsd, "semtech,resolution",
64 config->resolution);
65
66 acpi_dp_write(dsd);
67
68 acpigen_pop_len(); /* Device */
69 acpigen_pop_len(); /* Scope */
70
71 printk(BIOS_INFO, "%s: %s at %s\n", acpi_device_path(dev),
72 config->desc ? : dev->chip_ops->name, dev_path(dev));
73}
74
75static const char *i2c_sx9360_acpi_name(const struct device *dev)
76{
77 static char name[5];
78
79 snprintf(name, sizeof(name), "SX%02.2X", dev->path.i2c.device);
80 return name;
81}
82
83static struct device_operations i2c_sx9360_ops = {
84 .read_resources = noop_read_resources,
85 .set_resources = noop_set_resources,
86 .acpi_name = i2c_sx9360_acpi_name,
87 .acpi_fill_ssdt = i2c_sx9360_fill_ssdt,
88};
89
90static void i2c_sx9360_enable(struct device *dev)
91{
92 struct drivers_i2c_sx9360_config *config = config_of(dev);
93
94 if (!is_dev_enabled(dev))
95 return;
96
97 dev->ops = &i2c_sx9360_ops;
98
99 if (config->desc)
100 dev->name = config->desc;
101}
102
103struct chip_operations drivers_i2c_sx9360_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900104 .name = I2C_SX9360_CHIP_NAME,
Seunghwan Kim14a1c272021-11-08 15:55:01 +0900105 .enable_dev = i2c_sx9360_enable
106};