blob: b1075b85edace0bb795cb2cdf8802e0720454932 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
V Sowmya9f8023a2017-02-28 17:52:05 +05302
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpi.h>
4#include <acpi/acpi_device.h>
5#include <acpi/acpigen.h>
V Sowmya9f8023a2017-02-28 17:52:05 +05306#include <console/console.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +02007#include <device/i2c_simple.h>
V Sowmya9f8023a2017-02-28 17:52:05 +05308#include <device/device.h>
9#include <device/path.h>
Matt Delco7d002932020-06-16 11:39:52 +053010#include <device/pci_def.h>
V Sowmya9f8023a2017-02-28 17:52:05 +053011#include "chip.h"
12
Matt Delcoc3a83bf2020-06-16 12:02:34 +053013static void camera_fill_nvm(const struct device *dev)
14{
15 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
16 struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
17
18 /* It might be possible to default size or width based on type. */
19 if (!config->disable_nvm_defaults && !config->nvm_pagesize)
20 config->nvm_pagesize = 1;
21
22 if (!config->disable_nvm_defaults && !config->nvm_readonly)
23 config->nvm_readonly = 1;
24
25 if (config->nvm_size)
26 acpi_dp_add_integer(dsd, "size", config->nvm_size);
27
28 if (config->nvm_pagesize)
29 acpi_dp_add_integer(dsd, "pagesize", config->nvm_pagesize);
30
31 if (config->nvm_readonly)
32 acpi_dp_add_integer(dsd, "read-only", config->nvm_readonly);
33
34 if (config->nvm_width)
35 acpi_dp_add_integer(dsd, "address-width", config->nvm_width);
36
37 acpi_dp_write(dsd);
38}
39
40static void camera_fill_vcm(const struct device *dev)
41{
42 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
43 struct acpi_dp *dsd;
44
45 if (!config->vcm_compat)
46 return;
47
48 dsd = acpi_dp_new_table("_DSD");
49 acpi_dp_add_string(dsd, "compatible", config->vcm_compat);
50 acpi_dp_write(dsd);
51}
52
Matt Delco7d002932020-06-16 11:39:52 +053053static void write_pci_camera_device(const struct device *dev)
V Sowmya9f8023a2017-02-28 17:52:05 +053054{
55 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
Matt Delco7d002932020-06-16 11:39:52 +053056
57 if (dev->path.type != DEVICE_PATH_PCI) {
58 printk(BIOS_ERR, "CIO2/IMGU devices require PCI\n");
59 return;
60 }
61
62 acpigen_write_device(acpi_device_name(dev));
63 acpigen_write_ADR_pci_device(dev);
64 acpigen_write_name_string("_DDN", config->device_type == INTEL_ACPI_CAMERA_CIO2 ?
65 "Camera and Imaging Subsystem" : "Imaging Unit");
66}
67
68static void write_i2c_camera_device(const struct device *dev, const char *scope)
69{
70 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
V Sowmya9f8023a2017-02-28 17:52:05 +053071 struct acpi_i2c i2c = {
72 .address = dev->path.i2c.device,
73 .mode_10bit = dev->path.i2c.mode_10bit,
74 .speed = I2C_SPEED_FAST,
75 .resource = scope,
76 };
77
Matt Delco7d002932020-06-16 11:39:52 +053078 if (dev->path.type != DEVICE_PATH_I2C) {
79 printk(BIOS_ERR, "Non-CIO2/IMGU devices require I2C\n");
V Sowmya9f8023a2017-02-28 17:52:05 +053080 return;
Matt Delco7d002932020-06-16 11:39:52 +053081 }
V Sowmya9f8023a2017-02-28 17:52:05 +053082
V Sowmya9f8023a2017-02-28 17:52:05 +053083 acpigen_write_device(acpi_device_name(dev));
Matt Delco7d002932020-06-16 11:39:52 +053084
85 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR)
86 acpigen_write_name_integer("_ADR", 0);
87
88 if (config->acpi_hid)
89 acpigen_write_name_string("_HID", config->acpi_hid);
90 else if (config->device_type == INTEL_ACPI_CAMERA_VCM)
91 acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);
92 else if (config->device_type == INTEL_ACPI_CAMERA_NVM)
93 acpigen_write_name_string("_HID", "INT3499");
94
V Sowmya9f8023a2017-02-28 17:52:05 +053095 acpigen_write_name_integer("_UID", config->acpi_uid);
96 acpigen_write_name_string("_DDN", config->chip_name);
Hung-Te Linb4be50c2018-09-10 10:55:49 +080097 acpigen_write_STA(acpi_device_status(dev));
V Sowmya9f8023a2017-02-28 17:52:05 +053098
99 /* Resources */
100 acpigen_write_name("_CRS");
101 acpigen_write_resourcetemplate_header();
102 acpi_device_write_i2c(&i2c);
V Sowmya9f8023a2017-02-28 17:52:05 +0530103
Matt Delco7d002932020-06-16 11:39:52 +0530104 /*
105 * The optional vcm/nvram devices are presumed to be on the same I2C bus as the camera
106 * sensor.
107 */
108 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR &&
109 config->ssdb.vcm_type && config->vcm_address) {
110 struct acpi_i2c i2c_vcm = i2c;
111 i2c_vcm.address = config->vcm_address;
112 acpi_device_write_i2c(&i2c_vcm);
V Sowmya9f8023a2017-02-28 17:52:05 +0530113 }
114
Matt Delco7d002932020-06-16 11:39:52 +0530115 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR &&
116 config->ssdb.rom_type && config->rom_address) {
117 struct acpi_i2c i2c_rom = i2c;
118 i2c_rom.address = config->rom_address;
119 acpi_device_write_i2c(&i2c_rom);
120 }
121
122 acpigen_write_resourcetemplate_footer();
123}
124
125static void write_camera_device_common(const struct device *dev)
126{
127 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
128
129 /* Mark it as Camera related device */
130 if (config->device_type == INTEL_ACPI_CAMERA_CIO2 ||
131 config->device_type == INTEL_ACPI_CAMERA_IMGU ||
132 config->device_type == INTEL_ACPI_CAMERA_SENSOR ||
133 config->device_type == INTEL_ACPI_CAMERA_VCM) {
134 acpigen_write_name_integer("CAMD", config->device_type);
135 }
Matt Delcoc3a83bf2020-06-16 12:02:34 +0530136
137 switch (config->device_type) {
138 case INTEL_ACPI_CAMERA_VCM:
139 camera_fill_vcm(dev);
140 break;
141 case INTEL_ACPI_CAMERA_NVM:
142 camera_fill_nvm(dev);
143 break;
144 default:
145 break;
146 }
Matt Delco7d002932020-06-16 11:39:52 +0530147}
148
149static void camera_fill_ssdt(const struct device *dev)
150{
151 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
152 const char *scope = acpi_device_scope(dev);
153
154 if (!dev->enabled || !scope)
155 return;
156
157 /* Device */
158 acpigen_write_scope(scope);
159
160 if (config->device_type == INTEL_ACPI_CAMERA_CIO2 ||
161 config->device_type == INTEL_ACPI_CAMERA_IMGU)
162 write_pci_camera_device(dev);
163 else
164 write_i2c_camera_device(dev, scope);
165
166 write_camera_device_common(dev);
V Sowmya9f8023a2017-02-28 17:52:05 +0530167
168 acpigen_pop_len(); /* Device */
169 acpigen_pop_len(); /* Scope */
Matt Delco7d002932020-06-16 11:39:52 +0530170
171 if (dev->path.type == DEVICE_PATH_PCI) {
172 printk(BIOS_INFO, "%s: %s PCI address 0%x\n", acpi_device_path(dev),
173 dev->chip_ops->name, dev->path.pci.devfn);
174 } else {
175 printk(BIOS_INFO, "%s: %s I2C address 0%xh\n", acpi_device_path(dev),
176 dev->chip_ops->name, dev->path.i2c.device);
177 }
V Sowmya9f8023a2017-02-28 17:52:05 +0530178}
179
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600180static const char *camera_acpi_name(const struct device *dev)
V Sowmya9f8023a2017-02-28 17:52:05 +0530181{
Matt Delco7d002932020-06-16 11:39:52 +0530182 const char *prefix = NULL;
183 static char name[5];
V Sowmya9f8023a2017-02-28 17:52:05 +0530184 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
Matt Delco7d002932020-06-16 11:39:52 +0530185
186 if (config->acpi_name)
187 return config->acpi_name;
188
189 switch (config->device_type) {
190 case INTEL_ACPI_CAMERA_CIO2:
191 return "CIO2";
192 case INTEL_ACPI_CAMERA_IMGU:
193 return "IMGU";
194 case INTEL_ACPI_CAMERA_PMIC:
195 return "PMIC";
196 case INTEL_ACPI_CAMERA_SENSOR:
197 prefix = "CAM";
198 break;
199 case INTEL_ACPI_CAMERA_VCM:
200 prefix = "VCM";
201 break;
202 case INTEL_ACPI_CAMERA_NVM:
203 prefix = "NVM";
204 break;
205 default:
206 printk(BIOS_ERR, "Invalid device type: %x\n", config->device_type);
207 return NULL;
208 }
209
210 /*
211 * The camera # knows which link # they use, so that's used as the basis for the
212 * instance #. The VCM and NVM don't have this information, so the best we can go on is
213 * the _UID.
214 */
215 snprintf(name, sizeof(name), "%s%1u", prefix,
216 config->device_type == INTEL_ACPI_CAMERA_SENSOR ?
217 config->ssdb.link_used : config->acpi_uid);
218 return name;
V Sowmya9f8023a2017-02-28 17:52:05 +0530219}
220
221static struct device_operations camera_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200222 .read_resources = noop_read_resources,
223 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200224 .acpi_name = camera_acpi_name,
225 .acpi_fill_ssdt = camera_fill_ssdt,
V Sowmya9f8023a2017-02-28 17:52:05 +0530226};
227
228static void camera_enable(struct device *dev)
229{
230 dev->ops = &camera_ops;
231}
232
233struct chip_operations drivers_intel_mipi_camera_ops = {
234 CHIP_NAME("Intel MIPI Camera Device")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100235 .enable_dev = camera_enable
V Sowmya9f8023a2017-02-28 17:52:05 +0530236};