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