blob: 7c0774dd7a5b214da88a13d3ced7304060f5350d [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 Delco1245b1e2020-06-17 07:26:55 +053013#define SENSOR_NAME_UUID "822ace8f-2814-4174-a56b-5f029fe079ee"
14#define SENSOR_TYPE_UUID "26257549-9271-4ca4-bb43-c4899d5a4881"
15#define DEFAULT_ENDPOINT 0
16
Matt Delco1ffee9d2020-06-17 12:55:35 +053017static void apply_pld_defaults(struct drivers_intel_mipi_camera_config *config)
18{
19 if (!config->pld.ignore_color)
20 config->pld.ignore_color = 1;
21
22 if (!config->pld.visible)
23 config->pld.visible = 1;
24
25 if (!config->pld.vertical_offset)
26 config->pld.vertical_offset = 0xffff;
27
28 if (!config->pld.horizontal_offset)
29 config->pld.horizontal_offset = 0xffff;
30
31 /*
32 * PLD_PANEL_TOP has a value of zero, so the following will change any instance of
33 * PLD_PANEL_TOP to PLD_PANEL_FRONT unless disable_pld_defaults is set.
34 */
35 if (!config->pld.panel)
36 config->pld.panel = PLD_PANEL_FRONT;
37
38 /*
39 * PLD_HORIZONTAL_POSITION_LEFT has a value of zero, so the following will change any
40 * instance of that value to PLD_HORIZONTAL_POSITION_CENTER unless disable_pld_defaults
41 * is set.
42 */
43 if (!config->pld.horizontal_position)
44 config->pld.horizontal_position = PLD_HORIZONTAL_POSITION_CENTER;
45
46 /*
47 * The desired default for |vertical_position| is PLD_VERTICAL_POSITION_UPPER, which
48 * has a value of zero so no work is needed to set a default. The same applies for
49 * setting |shape| to PLD_SHAPE_ROUND.
50 */
51}
52
53static void camera_generate_pld(const struct device *dev)
54{
55 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
56
57 if (config->use_pld) {
58 if (!config->disable_pld_defaults)
59 apply_pld_defaults(config);
60
61 acpigen_write_pld(&config->pld);
62 }
63}
64
Matt Delco964033f2020-06-17 12:49:43 +053065static uint32_t address_for_dev_type(const struct device *dev, uint8_t dev_type)
66{
67 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
68 uint16_t i2c_bus = dev->bus ? dev->bus->secondary : 0xFFFF;
69 uint16_t i2c_addr;
70
71 switch (dev_type) {
72 case DEV_TYPE_SENSOR:
73 i2c_addr = dev->path.i2c.device;
74 break;
75 case DEV_TYPE_VCM:
76 i2c_addr = config->vcm_address;
77 break;
78 case DEV_TYPE_ROM:
79 i2c_addr = config->rom_address;
80 break;
81 default:
82 return 0;
83 }
84
85 return (((uint32_t)i2c_bus) << 24 | ((uint32_t)i2c_addr) << 8 | dev_type);
86}
87
88static void camera_generate_dsm(const struct device *dev)
89{
90 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
91 int local1_ret = 1 + (config->ssdb.vcm_type ? 1 : 0) + (config->ssdb.rom_type ? 1 : 0);
92 int next_local1 = 1;
93 /* Method (_DSM, 4, NotSerialized) */
94 acpigen_write_method("_DSM", 4);
95
96 /* ToBuffer (Arg0, Local0) */
97 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
98
99 /* If (LEqual (Local0, ToUUID(uuid))) */
100 acpigen_write_if();
101 acpigen_emit_byte(LEQUAL_OP);
102 acpigen_emit_byte(LOCAL0_OP);
103 acpigen_write_uuid(SENSOR_NAME_UUID);
104 acpigen_write_return_string(config->sensor_name ? config->sensor_name : "UNKNOWN");
105 acpigen_pop_len(); /* If */
106
107 /* If (LEqual (Local0, ToUUID(uuid))) */
108 acpigen_write_if();
109 acpigen_emit_byte(LEQUAL_OP);
110 acpigen_emit_byte(LOCAL0_OP);
111 acpigen_write_uuid(SENSOR_TYPE_UUID);
112 /* ToInteger (Arg2, Local1) */
113 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
114
115 /* If (LEqual (Local1, 1)) */
116 acpigen_write_if_lequal_op_int(LOCAL1_OP, next_local1++);
117 acpigen_write_return_integer(local1_ret);
118 acpigen_pop_len(); /* If Arg2=1 */
119
120 /* If (LEqual (Local1, 2)) */
121 acpigen_write_if_lequal_op_int(LOCAL1_OP, next_local1++);
122 acpigen_write_return_integer(address_for_dev_type(dev, DEV_TYPE_SENSOR));
123 acpigen_pop_len(); /* If Arg2=2 */
124
125 if (config->ssdb.vcm_type) {
126 /* If (LEqual (Local1, 3)) */
127 acpigen_write_if_lequal_op_int(LOCAL1_OP, next_local1++);
128 acpigen_write_return_integer(address_for_dev_type(dev, DEV_TYPE_VCM));
129 acpigen_pop_len(); /* If Arg2=3 */
130 }
131
132 if (config->ssdb.rom_type) {
133 /* If (LEqual (Local1, 3 or 4)) */
134 acpigen_write_if_lequal_op_int(LOCAL1_OP, next_local1);
135 acpigen_write_return_integer(address_for_dev_type(dev, DEV_TYPE_ROM));
136 acpigen_pop_len(); /* If Arg2=3 or 4 */
137 }
138
139 acpigen_pop_len(); /* If uuid */
140
141 /* Return (Buffer (One) { 0x0 }) */
142 acpigen_write_return_singleton_buffer(0x0);
143
144 acpigen_pop_len(); /* Method _DSM */
145}
146
Matt Delco1245b1e2020-06-17 07:26:55 +0530147static void camera_fill_sensor_defaults(struct drivers_intel_mipi_camera_config *config)
148{
149 if (config->disable_ssdb_defaults)
150 return;
151
152 if (!config->ssdb.platform)
153 config->ssdb.platform = PLATFORM_SKC;
154
155 if (!config->ssdb.flash_support)
156 config->ssdb.flash_support = FLASH_DISABLE;
157
158 if (!config->ssdb.privacy_led)
159 config->ssdb.privacy_led = PRIVACY_LED_A_16mA;
160
161 if (!config->ssdb.mipi_define)
162 config->ssdb.mipi_define = MIPI_INFO_ACPI_DEFINED;
163
164 if (!config->ssdb.mclk_speed)
165 config->ssdb.mclk_speed = CLK_FREQ_19_2MHZ;
166}
167
168/*
169 * Adds settings for a camera sensor device (typically at "\_SB.PCI0.I2Cx.CAMy"). The drivers
170 * for Linux tends to expect the camera sensor device and any related nvram / vcm devices to be
171 * separate ACPI devices, while the drivers for Windows want all of these to be grouped
172 * together in the camera sensor ACPI device. This implementation tries to satisfy both,
173 * though the unfortunate tradeoff is that the same I2C address for nvram and vcm is advertised
174 * by multiple devices in ACPI (via "_CRS"). The Windows driver can use the "_DSM" method to
175 * disambiguate the I2C resources in the camera sensor ACPI device. Drivers for Windows
176 * typically query "SSDB" for configuration information (represented as a binary blob dump of
177 * struct), while Linux drivers typically consult individual parameters in "_DSD".
178 *
179 * The tree of tables in "_DSD" is analogous to what's used for the "CIO2" device. The _DSD
180 * specifies a child table for the sensor's port (e.g., PRT0 for "port0"--this implementation
181 * assumes a camera only has 1 port). The PRT0 table specifies a table for each endpoint
182 * (though only 1 endpoint is supported by this implementation so the table only has an
183 * "endpoint0" that points to a EP00 table). The EP00 table primarily describes the # of lanes
184 * in "data-lines", a list of frequencies in "list-frequencies", and specifies the name of the
185 * other side in "remote-endpoint" (typically "\_SB.PCI0.CIO2").
186 */
187static void camera_fill_sensor(const struct device *dev)
188{
189 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
190 struct acpi_dp *ep00 = NULL;
191 struct acpi_dp *prt0 = NULL;
192 struct acpi_dp *dsd = NULL;
193 struct acpi_dp *remote = NULL;
194 const char *vcm_name = NULL;
195 struct acpi_dp *lens_focus = NULL;
196
Matt Delco1ffee9d2020-06-17 12:55:35 +0530197 camera_generate_pld(dev);
198
Matt Delco1245b1e2020-06-17 07:26:55 +0530199 camera_fill_sensor_defaults(config);
200
Matt Delco964033f2020-06-17 12:49:43 +0530201 /* _DSM */
202 camera_generate_dsm(dev);
203
Matt Delco1245b1e2020-06-17 07:26:55 +0530204 ep00 = acpi_dp_new_table("EP00");
205 acpi_dp_add_integer(ep00, "endpoint", DEFAULT_ENDPOINT);
206 acpi_dp_add_integer(ep00, "clock-lanes", 0);
207
208 if (config->ssdb.lanes_used > 0) {
209 struct acpi_dp *lanes = acpi_dp_new_table("data-lanes");
210 uint32_t i;
211 for (i = 1; i <= config->ssdb.lanes_used; ++i)
212 acpi_dp_add_integer(lanes, NULL, i);
213 acpi_dp_add_array(ep00, lanes);
214 }
215
216 if (config->num_freq_entries) {
217 struct acpi_dp *freq = acpi_dp_new_table("link-frequencies");
218 uint32_t i;
219 for (i = 0; i < config->num_freq_entries && i < MAX_LINK_FREQ_ENTRIES; ++i)
220 acpi_dp_add_integer(freq, NULL, config->link_freq[i]);
221 acpi_dp_add_array(ep00, freq);
222 }
223
224 remote = acpi_dp_new_table("remote-endpoint");
225
226 acpi_dp_add_reference(remote, NULL, config->remote_name);
227 acpi_dp_add_integer(remote, NULL, config->ssdb.link_used);
228 acpi_dp_add_integer(remote, NULL, DEFAULT_ENDPOINT);
229 acpi_dp_add_array(ep00, remote);
230
231 prt0 = acpi_dp_new_table("PRT0");
232
233 acpi_dp_add_integer(prt0, "port", 0);
234 acpi_dp_add_child(prt0, "endpoint0", ep00);
235 dsd = acpi_dp_new_table("_DSD");
236
237 acpi_dp_add_integer(dsd, "clock-frequency", config->ssdb.mclk_speed);
238
239 if (config->ssdb.degree)
240 acpi_dp_add_integer(dsd, "rotation", 180);
241
242 if (config->ssdb.vcm_type) {
243 if (config->vcm_name) {
244 vcm_name = config->vcm_name;
245 } else {
246 const struct device_path path = {
247 .type = DEVICE_PATH_I2C,
248 .i2c.device = config->vcm_address,
249 };
250 struct device *vcm_dev = find_dev_path(dev->bus, &path);
251 struct drivers_intel_mipi_camera_config *vcm_config;
252 vcm_config = vcm_dev ? vcm_dev->chip_info : NULL;
253
254 if (!vcm_config)
255 printk(BIOS_ERR, "Failed to get VCM\n");
256 else if (vcm_config->device_type != INTEL_ACPI_CAMERA_VCM)
257 printk(BIOS_ERR, "Device isn't VCM\n");
258 else
259 vcm_name = acpi_device_path(vcm_dev);
260 }
261 }
262
263 if (vcm_name) {
264 lens_focus = acpi_dp_new_table("lens-focus");
265 acpi_dp_add_reference(lens_focus, NULL, vcm_name);
266 acpi_dp_add_array(dsd, lens_focus);
267 }
268
269 acpi_dp_add_child(dsd, "port0", prt0);
270 acpi_dp_write(dsd);
271
272 acpigen_write_method_serialized("SSDB", 0);
273 acpigen_write_return_byte_buffer((uint8_t *)&config->ssdb, sizeof(config->ssdb));
274 acpigen_pop_len(); /* Method */
275
276 /* Fill Power Sequencing Data */
277 if (config->num_pwdb_entries > 0) {
278 acpigen_write_method_serialized("PWDB", 0);
279 acpigen_write_return_byte_buffer((uint8_t *)&config->pwdb,
280 sizeof(struct intel_pwdb) *
281 config->num_pwdb_entries);
282 acpigen_pop_len(); /* Method */
283 }
284}
285
Matt Delcoc3a83bf2020-06-16 12:02:34 +0530286static void camera_fill_nvm(const struct device *dev)
287{
288 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
289 struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
290
291 /* It might be possible to default size or width based on type. */
292 if (!config->disable_nvm_defaults && !config->nvm_pagesize)
293 config->nvm_pagesize = 1;
294
295 if (!config->disable_nvm_defaults && !config->nvm_readonly)
296 config->nvm_readonly = 1;
297
298 if (config->nvm_size)
299 acpi_dp_add_integer(dsd, "size", config->nvm_size);
300
301 if (config->nvm_pagesize)
302 acpi_dp_add_integer(dsd, "pagesize", config->nvm_pagesize);
303
304 if (config->nvm_readonly)
305 acpi_dp_add_integer(dsd, "read-only", config->nvm_readonly);
306
307 if (config->nvm_width)
308 acpi_dp_add_integer(dsd, "address-width", config->nvm_width);
309
310 acpi_dp_write(dsd);
311}
312
313static void camera_fill_vcm(const struct device *dev)
314{
315 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
316 struct acpi_dp *dsd;
317
318 if (!config->vcm_compat)
319 return;
320
321 dsd = acpi_dp_new_table("_DSD");
322 acpi_dp_add_string(dsd, "compatible", config->vcm_compat);
323 acpi_dp_write(dsd);
324}
325
Matt Delco7d002932020-06-16 11:39:52 +0530326static void write_pci_camera_device(const struct device *dev)
V Sowmya9f8023a2017-02-28 17:52:05 +0530327{
328 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
Matt Delco7d002932020-06-16 11:39:52 +0530329
330 if (dev->path.type != DEVICE_PATH_PCI) {
331 printk(BIOS_ERR, "CIO2/IMGU devices require PCI\n");
332 return;
333 }
334
335 acpigen_write_device(acpi_device_name(dev));
336 acpigen_write_ADR_pci_device(dev);
337 acpigen_write_name_string("_DDN", config->device_type == INTEL_ACPI_CAMERA_CIO2 ?
338 "Camera and Imaging Subsystem" : "Imaging Unit");
339}
340
341static void write_i2c_camera_device(const struct device *dev, const char *scope)
342{
343 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
V Sowmya9f8023a2017-02-28 17:52:05 +0530344 struct acpi_i2c i2c = {
345 .address = dev->path.i2c.device,
346 .mode_10bit = dev->path.i2c.mode_10bit,
347 .speed = I2C_SPEED_FAST,
348 .resource = scope,
349 };
350
Matt Delco7d002932020-06-16 11:39:52 +0530351 if (dev->path.type != DEVICE_PATH_I2C) {
352 printk(BIOS_ERR, "Non-CIO2/IMGU devices require I2C\n");
V Sowmya9f8023a2017-02-28 17:52:05 +0530353 return;
Matt Delco7d002932020-06-16 11:39:52 +0530354 }
V Sowmya9f8023a2017-02-28 17:52:05 +0530355
V Sowmya9f8023a2017-02-28 17:52:05 +0530356 acpigen_write_device(acpi_device_name(dev));
Matt Delco7d002932020-06-16 11:39:52 +0530357
358 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR)
359 acpigen_write_name_integer("_ADR", 0);
360
361 if (config->acpi_hid)
362 acpigen_write_name_string("_HID", config->acpi_hid);
363 else if (config->device_type == INTEL_ACPI_CAMERA_VCM)
364 acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);
365 else if (config->device_type == INTEL_ACPI_CAMERA_NVM)
366 acpigen_write_name_string("_HID", "INT3499");
367
V Sowmya9f8023a2017-02-28 17:52:05 +0530368 acpigen_write_name_integer("_UID", config->acpi_uid);
369 acpigen_write_name_string("_DDN", config->chip_name);
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800370 acpigen_write_STA(acpi_device_status(dev));
V Sowmya9f8023a2017-02-28 17:52:05 +0530371
372 /* Resources */
373 acpigen_write_name("_CRS");
374 acpigen_write_resourcetemplate_header();
375 acpi_device_write_i2c(&i2c);
V Sowmya9f8023a2017-02-28 17:52:05 +0530376
Matt Delco7d002932020-06-16 11:39:52 +0530377 /*
378 * The optional vcm/nvram devices are presumed to be on the same I2C bus as the camera
379 * sensor.
380 */
381 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR &&
382 config->ssdb.vcm_type && config->vcm_address) {
383 struct acpi_i2c i2c_vcm = i2c;
384 i2c_vcm.address = config->vcm_address;
385 acpi_device_write_i2c(&i2c_vcm);
V Sowmya9f8023a2017-02-28 17:52:05 +0530386 }
387
Matt Delco7d002932020-06-16 11:39:52 +0530388 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR &&
389 config->ssdb.rom_type && config->rom_address) {
390 struct acpi_i2c i2c_rom = i2c;
391 i2c_rom.address = config->rom_address;
392 acpi_device_write_i2c(&i2c_rom);
393 }
394
395 acpigen_write_resourcetemplate_footer();
396}
397
398static void write_camera_device_common(const struct device *dev)
399{
400 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
401
402 /* Mark it as Camera related device */
403 if (config->device_type == INTEL_ACPI_CAMERA_CIO2 ||
404 config->device_type == INTEL_ACPI_CAMERA_IMGU ||
405 config->device_type == INTEL_ACPI_CAMERA_SENSOR ||
406 config->device_type == INTEL_ACPI_CAMERA_VCM) {
407 acpigen_write_name_integer("CAMD", config->device_type);
408 }
Matt Delcoc3a83bf2020-06-16 12:02:34 +0530409
410 switch (config->device_type) {
Matt Delco1245b1e2020-06-17 07:26:55 +0530411 case INTEL_ACPI_CAMERA_SENSOR:
412 camera_fill_sensor(dev);
413 break;
Matt Delcoc3a83bf2020-06-16 12:02:34 +0530414 case INTEL_ACPI_CAMERA_VCM:
415 camera_fill_vcm(dev);
416 break;
417 case INTEL_ACPI_CAMERA_NVM:
418 camera_fill_nvm(dev);
419 break;
420 default:
421 break;
422 }
Matt Delco7d002932020-06-16 11:39:52 +0530423}
424
425static void camera_fill_ssdt(const struct device *dev)
426{
427 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
428 const char *scope = acpi_device_scope(dev);
429
430 if (!dev->enabled || !scope)
431 return;
432
433 /* Device */
434 acpigen_write_scope(scope);
435
436 if (config->device_type == INTEL_ACPI_CAMERA_CIO2 ||
437 config->device_type == INTEL_ACPI_CAMERA_IMGU)
438 write_pci_camera_device(dev);
439 else
440 write_i2c_camera_device(dev, scope);
441
442 write_camera_device_common(dev);
V Sowmya9f8023a2017-02-28 17:52:05 +0530443
444 acpigen_pop_len(); /* Device */
445 acpigen_pop_len(); /* Scope */
Matt Delco7d002932020-06-16 11:39:52 +0530446
447 if (dev->path.type == DEVICE_PATH_PCI) {
448 printk(BIOS_INFO, "%s: %s PCI address 0%x\n", acpi_device_path(dev),
449 dev->chip_ops->name, dev->path.pci.devfn);
450 } else {
451 printk(BIOS_INFO, "%s: %s I2C address 0%xh\n", acpi_device_path(dev),
452 dev->chip_ops->name, dev->path.i2c.device);
453 }
V Sowmya9f8023a2017-02-28 17:52:05 +0530454}
455
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600456static const char *camera_acpi_name(const struct device *dev)
V Sowmya9f8023a2017-02-28 17:52:05 +0530457{
Matt Delco7d002932020-06-16 11:39:52 +0530458 const char *prefix = NULL;
459 static char name[5];
V Sowmya9f8023a2017-02-28 17:52:05 +0530460 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
Matt Delco7d002932020-06-16 11:39:52 +0530461
462 if (config->acpi_name)
463 return config->acpi_name;
464
465 switch (config->device_type) {
466 case INTEL_ACPI_CAMERA_CIO2:
467 return "CIO2";
468 case INTEL_ACPI_CAMERA_IMGU:
469 return "IMGU";
470 case INTEL_ACPI_CAMERA_PMIC:
471 return "PMIC";
472 case INTEL_ACPI_CAMERA_SENSOR:
473 prefix = "CAM";
474 break;
475 case INTEL_ACPI_CAMERA_VCM:
476 prefix = "VCM";
477 break;
478 case INTEL_ACPI_CAMERA_NVM:
479 prefix = "NVM";
480 break;
481 default:
482 printk(BIOS_ERR, "Invalid device type: %x\n", config->device_type);
483 return NULL;
484 }
485
486 /*
487 * The camera # knows which link # they use, so that's used as the basis for the
488 * instance #. The VCM and NVM don't have this information, so the best we can go on is
489 * the _UID.
490 */
491 snprintf(name, sizeof(name), "%s%1u", prefix,
492 config->device_type == INTEL_ACPI_CAMERA_SENSOR ?
493 config->ssdb.link_used : config->acpi_uid);
494 return name;
V Sowmya9f8023a2017-02-28 17:52:05 +0530495}
496
497static struct device_operations camera_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200498 .read_resources = noop_read_resources,
499 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200500 .acpi_name = camera_acpi_name,
501 .acpi_fill_ssdt = camera_fill_ssdt,
V Sowmya9f8023a2017-02-28 17:52:05 +0530502};
503
504static void camera_enable(struct device *dev)
505{
506 dev->ops = &camera_ops;
507}
508
509struct chip_operations drivers_intel_mipi_camera_ops = {
510 CHIP_NAME("Intel MIPI Camera Device")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100511 .enable_dev = camera_enable
V Sowmya9f8023a2017-02-28 17:52:05 +0530512};