blob: e5142bca3686c9bd2cedb4dadf6dd875e175c180 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Enrico Granata76a1f492018-06-20 13:01:45 -07002
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpi_device.h>
4#include <acpi/acpigen.h>
Eric Laib90739d2019-06-13 15:21:20 +08005#include <console/console.h>
Enrico Granata76a1f492018-06-20 13:01:45 -07006#include <device/i2c_simple.h>
7#include <device/device.h>
Enrico Granata76a1f492018-06-20 13:01:45 -07008#include <string.h>
9#include "chip.h"
10
Gwendal Grignou145ef872018-07-03 14:31:31 -070011#define I2C_SX9310_ACPI_ID "STH9310"
Enrico Granata76a1f492018-06-20 13:01:45 -070012#define I2C_SX9310_ACPI_NAME "Semtech SX9310"
13
Gwendal Grignou689c25b2021-01-27 23:29:38 -080014static const char * const i2c_sx9310_resolution[] = {
15 [SX9310_COARSEST] = "coarsest",
16 [SX9310_VERY_COARSE] = "very-coarse",
17 [SX9310_COARSE] = "coarse",
18 [SX9310_MEDIUM_COARSE] = "medium-coarse",
19 [SX9310_MEDIUM] = "medium",
20 [SX9310_FINE] = "fine",
21 [SX9310_VERY_FINE] = "very-fine",
22 [SX9310_FINEST] = "finest",
23};
Enrico Granata76a1f492018-06-20 13:01:45 -070024
Furquan Shaikh7536a392020-04-24 21:59:21 -070025static void i2c_sx9310_fill_ssdt(const struct device *dev)
Enrico Granata76a1f492018-06-20 13:01:45 -070026{
27 struct drivers_i2c_sx9310_config *config = dev->chip_info;
28 const char *scope = acpi_device_scope(dev);
29 struct acpi_i2c i2c = {
30 .address = dev->path.i2c.device,
31 .mode_10bit = dev->path.i2c.mode_10bit,
Furquan Shaikha7e92502018-06-22 10:15:04 -070032 .speed = I2C_SPEED_FAST,
Enrico Granata76a1f492018-06-20 13:01:45 -070033 .resource = scope,
34 };
35 struct acpi_dp *dsd;
Gwendal Grignou689c25b2021-01-27 23:29:38 -080036 struct acpi_dp *combined_sensors;
Enrico Granata76a1f492018-06-20 13:01:45 -070037
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -070038 if (!scope || !config)
Enrico Granata76a1f492018-06-20 13:01:45 -070039 return;
40
Furquan Shaikha7e92502018-06-22 10:15:04 -070041 if (config->speed)
42 i2c.speed = config->speed;
43
Enrico Granata76a1f492018-06-20 13:01:45 -070044 /* Device */
45 acpigen_write_scope(scope);
46 acpigen_write_device(acpi_device_name(dev));
47 acpigen_write_name_string("_HID", I2C_SX9310_ACPI_ID);
48 acpigen_write_name_integer("_UID", config->uid);
49 acpigen_write_name_string("_DDN", config->desc);
Matt DeVillier27172062022-11-28 16:19:18 -060050 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Enrico Granata76a1f492018-06-20 13:01:45 -070051
52 /* Resources */
53 acpigen_write_name("_CRS");
54 acpigen_write_resourcetemplate_header();
55 acpi_device_write_i2c(&i2c);
Furquan Shaikhaea98712019-04-10 10:22:06 -070056
57 if (config->irq_gpio.pin_count)
58 acpi_device_write_gpio(&config->irq_gpio);
59 else
60 acpi_device_write_interrupt(&config->irq);
61
Enrico Granata76a1f492018-06-20 13:01:45 -070062 acpigen_write_resourcetemplate_footer();
63
64 /* DSD */
65 dsd = acpi_dp_new_table("_DSD");
Enrico Granata76a1f492018-06-20 13:01:45 -070066
Gwendal Grignou689c25b2021-01-27 23:29:38 -080067 /*
68 * Format describe in linux kernel documentation. See
69 * https://www.kernel.org/doc/Documentation/devicetree/bindings/iio/proximity/semtech%2Csx9310.yaml
70 */
71 acpi_dp_add_integer(dsd, "semtech,cs0-ground", config->cs0_ground);
72 acpi_dp_add_integer(dsd, "semtech,startup-sensor",
73 config->startup_sensor);
74 acpi_dp_add_integer(dsd, "semtech,proxraw-strength",
75 config->proxraw_strength);
76 acpi_dp_add_integer(dsd, "semtech,avg-pos-strength",
77 config->avg_pos_strength);
78
79 /* Add combined_sensors package */
80 if (config->combined_sensors_count > 0) {
81 combined_sensors = acpi_dp_new_table("semtech,combined-sensors");
82 for (int i = 0;
83 i < config->combined_sensors_count &&
84 i < MAX_COMBINED_SENSORS_ENTRIES; ++i) {
85 acpi_dp_add_integer(combined_sensors, NULL,
86 config->combined_sensors[i]);
87 }
88 acpi_dp_add_array(dsd, combined_sensors);
89 }
90 if (config->resolution && config->resolution < ARRAY_SIZE(i2c_sx9310_resolution))
91 acpi_dp_add_string(dsd, "semtech,resolution",
92 i2c_sx9310_resolution[config->resolution]);
93
94 acpi_dp_write(dsd);
Enrico Granata76a1f492018-06-20 13:01:45 -070095 acpigen_pop_len(); /* Device */
96 acpigen_pop_len(); /* Scope */
Eric Laib90739d2019-06-13 15:21:20 +080097
98 printk(BIOS_INFO, "%s: %s at %s\n", acpi_device_path(dev),
99 config->desc ? : dev->chip_ops->name, dev_path(dev));
Enrico Granata76a1f492018-06-20 13:01:45 -0700100}
101
Enrico Granata76a1f492018-06-20 13:01:45 -0700102static const char *i2c_sx9310_acpi_name(const struct device *dev)
103{
104 static char name[5];
105
106 snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device);
107 return name;
108}
109
110static struct device_operations i2c_sx9310_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200111 .read_resources = noop_read_resources,
112 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200113 .acpi_name = i2c_sx9310_acpi_name,
114 .acpi_fill_ssdt = i2c_sx9310_fill_ssdt,
Enrico Granata76a1f492018-06-20 13:01:45 -0700115};
116
117static void i2c_sx9310_enable(struct device *dev)
118{
119 struct drivers_i2c_sx9310_config *config = dev->chip_info;
120
121 if (!config) {
122 dev->enabled = 0;
123 return;
124 }
125
126 dev->ops = &i2c_sx9310_ops;
127
128 if (config->desc)
129 dev->name = config->desc;
130}
131
132struct chip_operations drivers_i2c_sx9310_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900133 .name = I2C_SX9310_ACPI_NAME,
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100134 .enable_dev = i2c_sx9310_enable
Enrico Granata76a1f492018-06-20 13:01:45 -0700135};