blob: 50946081bbcb7e0db43aef95bd3745b8cd9fcb66 [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>
8#include <device/path.h>
Enrico Granata76a1f492018-06-20 13:01:45 -07009#include <string.h>
10#include "chip.h"
11
Gwendal Grignou145ef872018-07-03 14:31:31 -070012#define I2C_SX9310_ACPI_ID "STH9310"
Enrico Granata76a1f492018-06-20 13:01:45 -070013#define I2C_SX9310_ACPI_NAME "Semtech SX9310"
14
Gwendal Grignou689c25b2021-01-27 23:29:38 -080015static const char * const i2c_sx9310_resolution[] = {
16 [SX9310_COARSEST] = "coarsest",
17 [SX9310_VERY_COARSE] = "very-coarse",
18 [SX9310_COARSE] = "coarse",
19 [SX9310_MEDIUM_COARSE] = "medium-coarse",
20 [SX9310_MEDIUM] = "medium",
21 [SX9310_FINE] = "fine",
22 [SX9310_VERY_FINE] = "very-fine",
23 [SX9310_FINEST] = "finest",
24};
Enrico Granata76a1f492018-06-20 13:01:45 -070025
Furquan Shaikh7536a392020-04-24 21:59:21 -070026static void i2c_sx9310_fill_ssdt(const struct device *dev)
Enrico Granata76a1f492018-06-20 13:01:45 -070027{
28 struct drivers_i2c_sx9310_config *config = dev->chip_info;
29 const char *scope = acpi_device_scope(dev);
30 struct acpi_i2c i2c = {
31 .address = dev->path.i2c.device,
32 .mode_10bit = dev->path.i2c.mode_10bit,
Furquan Shaikha7e92502018-06-22 10:15:04 -070033 .speed = I2C_SPEED_FAST,
Enrico Granata76a1f492018-06-20 13:01:45 -070034 .resource = scope,
35 };
36 struct acpi_dp *dsd;
Gwendal Grignou689c25b2021-01-27 23:29:38 -080037 struct acpi_dp *combined_sensors;
Enrico Granata76a1f492018-06-20 13:01:45 -070038
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -070039 if (!scope || !config)
Enrico Granata76a1f492018-06-20 13:01:45 -070040 return;
41
Furquan Shaikha7e92502018-06-22 10:15:04 -070042 if (config->speed)
43 i2c.speed = config->speed;
44
Enrico Granata76a1f492018-06-20 13:01:45 -070045 /* Device */
46 acpigen_write_scope(scope);
47 acpigen_write_device(acpi_device_name(dev));
48 acpigen_write_name_string("_HID", I2C_SX9310_ACPI_ID);
49 acpigen_write_name_integer("_UID", config->uid);
50 acpigen_write_name_string("_DDN", config->desc);
Matt DeVillier27172062022-11-28 16:19:18 -060051 acpigen_write_STA(ACPI_STATUS_DEVICE_HIDDEN_ON);
Enrico Granata76a1f492018-06-20 13:01:45 -070052
53 /* Resources */
54 acpigen_write_name("_CRS");
55 acpigen_write_resourcetemplate_header();
56 acpi_device_write_i2c(&i2c);
Furquan Shaikhaea98712019-04-10 10:22:06 -070057
58 if (config->irq_gpio.pin_count)
59 acpi_device_write_gpio(&config->irq_gpio);
60 else
61 acpi_device_write_interrupt(&config->irq);
62
Enrico Granata76a1f492018-06-20 13:01:45 -070063 acpigen_write_resourcetemplate_footer();
64
65 /* DSD */
66 dsd = acpi_dp_new_table("_DSD");
Enrico Granata76a1f492018-06-20 13:01:45 -070067
Gwendal Grignou689c25b2021-01-27 23:29:38 -080068 /*
69 * Format describe in linux kernel documentation. See
70 * https://www.kernel.org/doc/Documentation/devicetree/bindings/iio/proximity/semtech%2Csx9310.yaml
71 */
72 acpi_dp_add_integer(dsd, "semtech,cs0-ground", config->cs0_ground);
73 acpi_dp_add_integer(dsd, "semtech,startup-sensor",
74 config->startup_sensor);
75 acpi_dp_add_integer(dsd, "semtech,proxraw-strength",
76 config->proxraw_strength);
77 acpi_dp_add_integer(dsd, "semtech,avg-pos-strength",
78 config->avg_pos_strength);
79
80 /* Add combined_sensors package */
81 if (config->combined_sensors_count > 0) {
82 combined_sensors = acpi_dp_new_table("semtech,combined-sensors");
83 for (int i = 0;
84 i < config->combined_sensors_count &&
85 i < MAX_COMBINED_SENSORS_ENTRIES; ++i) {
86 acpi_dp_add_integer(combined_sensors, NULL,
87 config->combined_sensors[i]);
88 }
89 acpi_dp_add_array(dsd, combined_sensors);
90 }
91 if (config->resolution && config->resolution < ARRAY_SIZE(i2c_sx9310_resolution))
92 acpi_dp_add_string(dsd, "semtech,resolution",
93 i2c_sx9310_resolution[config->resolution]);
94
95 acpi_dp_write(dsd);
Enrico Granata76a1f492018-06-20 13:01:45 -070096 acpigen_pop_len(); /* Device */
97 acpigen_pop_len(); /* Scope */
Eric Laib90739d2019-06-13 15:21:20 +080098
99 printk(BIOS_INFO, "%s: %s at %s\n", acpi_device_path(dev),
100 config->desc ? : dev->chip_ops->name, dev_path(dev));
Enrico Granata76a1f492018-06-20 13:01:45 -0700101}
102
Enrico Granata76a1f492018-06-20 13:01:45 -0700103static const char *i2c_sx9310_acpi_name(const struct device *dev)
104{
105 static char name[5];
106
107 snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device);
108 return name;
109}
110
111static struct device_operations i2c_sx9310_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200112 .read_resources = noop_read_resources,
113 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200114 .acpi_name = i2c_sx9310_acpi_name,
115 .acpi_fill_ssdt = i2c_sx9310_fill_ssdt,
Enrico Granata76a1f492018-06-20 13:01:45 -0700116};
117
118static void i2c_sx9310_enable(struct device *dev)
119{
120 struct drivers_i2c_sx9310_config *config = dev->chip_info;
121
122 if (!config) {
123 dev->enabled = 0;
124 return;
125 }
126
127 dev->ops = &i2c_sx9310_ops;
128
129 if (config->desc)
130 dev->name = config->desc;
131}
132
133struct chip_operations drivers_i2c_sx9310_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900134 .name = I2C_SX9310_ACPI_NAME,
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100135 .enable_dev = i2c_sx9310_enable
Enrico Granata76a1f492018-06-20 13:01:45 -0700136};