blob: 51686d018e1646ee36c3124067afd3c89d202308 [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
Matt Delco879b3c12020-06-17 13:10:22 +05303#include <stdlib.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07004#include <acpi/acpi.h>
5#include <acpi/acpi_device.h>
6#include <acpi/acpigen.h>
V Sowmya9f8023a2017-02-28 17:52:05 +05307#include <console/console.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +02008#include <device/i2c_simple.h>
V Sowmya9f8023a2017-02-28 17:52:05 +05309#include <device/device.h>
10#include <device/path.h>
Matt Delco7d002932020-06-16 11:39:52 +053011#include <device/pci_def.h>
V Sowmya9f8023a2017-02-28 17:52:05 +053012#include "chip.h"
13
Matt Delco1245b1e2020-06-17 07:26:55 +053014#define SENSOR_NAME_UUID "822ace8f-2814-4174-a56b-5f029fe079ee"
15#define SENSOR_TYPE_UUID "26257549-9271-4ca4-bb43-c4899d5a4881"
16#define DEFAULT_ENDPOINT 0
Matt Delco879b3c12020-06-17 13:10:22 +053017#define DEFAULT_REMOTE_NAME "\\_SB.PCI0.CIO2"
18#define CIO2_PCI_DEV 0x14
19#define CIO2_PCI_FN 0x3
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +053020#define POWER_RESOURCE_NAME "PRIC"
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +053021#define GUARD_VARIABLE_FORMAT "RES%1d"
22#define ENABLE_METHOD_FORMAT "ENB%1d"
23#define DISABLE_METHOD_FORMAT "DSB%1d"
24#define UNKNOWN_METHOD_FORMAT "UNK%1d"
25#define CLK_ENABLE_METHOD "MCON"
26#define CLK_DISABLE_METHOD "MCOF"
27
28static struct camera_resource_manager res_mgr;
29
30static void resource_set_action_type(struct resource_config *res_config,
31 enum action_type action)
32{
33 if (res_config)
34 res_config->action = action;
35}
36
37static enum action_type resource_get_action_type(const struct resource_config *res_config)
38{
39 return res_config ? res_config->action : UNKNOWN_ACTION;
40}
41
42static enum ctrl_type resource_get_ctrl_type(const struct resource_config *res_config)
43{
44 return res_config ? res_config->type : UNKNOWN_CTRL;
45}
46
47static void resource_set_clk_config(struct resource_config *res_config,
48 const struct clk_config *clk_conf)
49{
50 if (res_config) {
51 res_config->type = IMGCLK;
52 res_config->clk_conf = clk_conf;
53 }
54}
55
56static const struct clk_config *resource_clk_config(const struct resource_config *res_config)
57{
58 return res_config ? res_config->clk_conf : NULL;
59}
60
61static void resource_set_gpio_config(struct resource_config *res_config,
62 const struct gpio_config *gpio_conf)
63{
64 if (res_config) {
65 res_config->type = GPIO;
66 res_config->gpio_conf = gpio_conf;
67 }
68}
69
70static const struct gpio_config *resource_gpio_config(const struct resource_config *res_config)
71{
72 return res_config ? res_config->gpio_conf : NULL;
73}
Matt Delco879b3c12020-06-17 13:10:22 +053074
75/*
76 * This implementation assumes there is only 1 endpoint at each end of every data port. It also
77 * assumes there are no clock lanes. It also assumes that any VCM or NVM for a CAM is on the
78 * same I2C bus as the CAM.
79 */
80
81/*
82 * Adds settings for a CIO2 device (typically at "\_SB.PCI0.CIO2"). A _DSD is added that
83 * specifies a child table for each port (e.g., PRT0 for "port0" and PRT1 for "port1"). Each
84 * PRTx table specifies a table for each endpoint (though only 1 endpoint is supported by this
85 * implementation so the table only has an "endpoint0" that points to a EPx0 table). The EPx0
86 * table primarily describes the # of lanes in "data-lines" and specifies the name of the other
87 * side in "remote-endpoint" (the name is usually of the form "\_SB.PCI0.I2Cx.CAM0" for the
88 * first port/cam and "\_SB.PCI0.I2Cx.CAM1" if there's a second port/cam).
89 */
90static void camera_fill_cio2(const struct device *dev)
91{
92 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
93 struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
94 char *ep_table_name[MAX_PORT_ENTRIES] = {NULL};
95 char *port_table_name[MAX_PORT_ENTRIES] = {NULL};
96 char *port_name[MAX_PORT_ENTRIES] = {NULL};
97 unsigned int i, j;
98 char name[BUS_PATH_MAX];
99 struct acpi_dp *ep_table = NULL;
100 struct acpi_dp *port_table = NULL;
101 struct acpi_dp *remote = NULL;
102 struct acpi_dp *lanes = NULL;
103
104 for (i = 0; i < config->cio2_num_ports && i < MAX_PORT_ENTRIES; ++i) {
105 snprintf(name, sizeof(name), "EP%u0", i);
106 ep_table_name[i] = strdup(name);
107 ep_table = acpi_dp_new_table(ep_table_name[i]);
108 acpi_dp_add_integer(ep_table, "endpoint", 0);
109 acpi_dp_add_integer(ep_table, "clock-lanes", 0);
110
111 if (config->cio2_lanes_used[i] > 0) {
112 lanes = acpi_dp_new_table("data-lanes");
113
114 for (j = 1; j <= config->cio2_lanes_used[i] &&
115 j <= MAX_PORT_ENTRIES; ++j)
116 acpi_dp_add_integer(lanes, NULL, j);
117 acpi_dp_add_array(ep_table, lanes);
118 }
119
120 if (config->cio2_lane_endpoint[i]) {
121 remote = acpi_dp_new_table("remote-endpoint");
122 acpi_dp_add_reference(remote, NULL, config->cio2_lane_endpoint[i]);
123 acpi_dp_add_integer(remote, NULL, 0);
124 acpi_dp_add_integer(remote, NULL, 0);
125 acpi_dp_add_array(ep_table, remote);
126 }
127
128 snprintf(name, sizeof(name), "PRT%u", i);
129 port_table_name[i] = strdup(name);
130 port_table = acpi_dp_new_table(port_table_name[i]);
131 acpi_dp_add_integer(port_table, "port", config->cio2_prt[i]);
132 acpi_dp_add_child(port_table, "endpoint0", ep_table);
133
134 snprintf(name, sizeof(name), "port%u", i);
135 port_name[i] = strdup(name);
136 acpi_dp_add_child(dsd, port_name[i], port_table);
137 }
138
139 acpi_dp_write(dsd);
140
141 for (i = 0; i < config->cio2_num_ports; ++i) {
142 free(ep_table_name[i]);
143 free(port_table_name[i]);
144 free(port_name[i]);
145 }
146}
Matt Delco1245b1e2020-06-17 07:26:55 +0530147
Matt Delco1ffee9d2020-06-17 12:55:35 +0530148static void apply_pld_defaults(struct drivers_intel_mipi_camera_config *config)
149{
150 if (!config->pld.ignore_color)
151 config->pld.ignore_color = 1;
152
153 if (!config->pld.visible)
154 config->pld.visible = 1;
155
156 if (!config->pld.vertical_offset)
157 config->pld.vertical_offset = 0xffff;
158
159 if (!config->pld.horizontal_offset)
160 config->pld.horizontal_offset = 0xffff;
161
162 /*
163 * PLD_PANEL_TOP has a value of zero, so the following will change any instance of
164 * PLD_PANEL_TOP to PLD_PANEL_FRONT unless disable_pld_defaults is set.
165 */
166 if (!config->pld.panel)
167 config->pld.panel = PLD_PANEL_FRONT;
168
169 /*
170 * PLD_HORIZONTAL_POSITION_LEFT has a value of zero, so the following will change any
171 * instance of that value to PLD_HORIZONTAL_POSITION_CENTER unless disable_pld_defaults
172 * is set.
173 */
174 if (!config->pld.horizontal_position)
175 config->pld.horizontal_position = PLD_HORIZONTAL_POSITION_CENTER;
176
177 /*
178 * The desired default for |vertical_position| is PLD_VERTICAL_POSITION_UPPER, which
179 * has a value of zero so no work is needed to set a default. The same applies for
180 * setting |shape| to PLD_SHAPE_ROUND.
181 */
182}
183
184static void camera_generate_pld(const struct device *dev)
185{
186 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
187
188 if (config->use_pld) {
189 if (!config->disable_pld_defaults)
190 apply_pld_defaults(config);
191
192 acpigen_write_pld(&config->pld);
193 }
194}
195
Matt Delco964033f2020-06-17 12:49:43 +0530196static uint32_t address_for_dev_type(const struct device *dev, uint8_t dev_type)
197{
198 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
199 uint16_t i2c_bus = dev->bus ? dev->bus->secondary : 0xFFFF;
200 uint16_t i2c_addr;
201
202 switch (dev_type) {
203 case DEV_TYPE_SENSOR:
204 i2c_addr = dev->path.i2c.device;
205 break;
206 case DEV_TYPE_VCM:
207 i2c_addr = config->vcm_address;
208 break;
209 case DEV_TYPE_ROM:
210 i2c_addr = config->rom_address;
211 break;
212 default:
213 return 0;
214 }
215
216 return (((uint32_t)i2c_bus) << 24 | ((uint32_t)i2c_addr) << 8 | dev_type);
217}
218
219static void camera_generate_dsm(const struct device *dev)
220{
221 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
222 int local1_ret = 1 + (config->ssdb.vcm_type ? 1 : 0) + (config->ssdb.rom_type ? 1 : 0);
223 int next_local1 = 1;
224 /* Method (_DSM, 4, NotSerialized) */
225 acpigen_write_method("_DSM", 4);
226
227 /* ToBuffer (Arg0, Local0) */
228 acpigen_write_to_buffer(ARG0_OP, LOCAL0_OP);
229
230 /* If (LEqual (Local0, ToUUID(uuid))) */
231 acpigen_write_if();
232 acpigen_emit_byte(LEQUAL_OP);
233 acpigen_emit_byte(LOCAL0_OP);
234 acpigen_write_uuid(SENSOR_NAME_UUID);
235 acpigen_write_return_string(config->sensor_name ? config->sensor_name : "UNKNOWN");
236 acpigen_pop_len(); /* If */
237
238 /* If (LEqual (Local0, ToUUID(uuid))) */
239 acpigen_write_if();
240 acpigen_emit_byte(LEQUAL_OP);
241 acpigen_emit_byte(LOCAL0_OP);
242 acpigen_write_uuid(SENSOR_TYPE_UUID);
243 /* ToInteger (Arg2, Local1) */
244 acpigen_write_to_integer(ARG2_OP, LOCAL1_OP);
245
246 /* If (LEqual (Local1, 1)) */
247 acpigen_write_if_lequal_op_int(LOCAL1_OP, next_local1++);
248 acpigen_write_return_integer(local1_ret);
249 acpigen_pop_len(); /* If Arg2=1 */
250
251 /* If (LEqual (Local1, 2)) */
252 acpigen_write_if_lequal_op_int(LOCAL1_OP, next_local1++);
253 acpigen_write_return_integer(address_for_dev_type(dev, DEV_TYPE_SENSOR));
254 acpigen_pop_len(); /* If Arg2=2 */
255
256 if (config->ssdb.vcm_type) {
257 /* If (LEqual (Local1, 3)) */
258 acpigen_write_if_lequal_op_int(LOCAL1_OP, next_local1++);
259 acpigen_write_return_integer(address_for_dev_type(dev, DEV_TYPE_VCM));
260 acpigen_pop_len(); /* If Arg2=3 */
261 }
262
263 if (config->ssdb.rom_type) {
264 /* If (LEqual (Local1, 3 or 4)) */
265 acpigen_write_if_lequal_op_int(LOCAL1_OP, next_local1);
266 acpigen_write_return_integer(address_for_dev_type(dev, DEV_TYPE_ROM));
267 acpigen_pop_len(); /* If Arg2=3 or 4 */
268 }
269
270 acpigen_pop_len(); /* If uuid */
271
272 /* Return (Buffer (One) { 0x0 }) */
273 acpigen_write_return_singleton_buffer(0x0);
274
275 acpigen_pop_len(); /* Method _DSM */
276}
277
Matt Delco879b3c12020-06-17 13:10:22 +0530278static void camera_fill_ssdb_defaults(struct drivers_intel_mipi_camera_config *config)
Matt Delco1245b1e2020-06-17 07:26:55 +0530279{
Matt Delco879b3c12020-06-17 13:10:22 +0530280 struct device *cio2 = pcidev_on_root(CIO2_PCI_DEV, CIO2_PCI_FN);
281 struct drivers_intel_mipi_camera_config *cio2_config;
282
Matt Delco1245b1e2020-06-17 07:26:55 +0530283 if (config->disable_ssdb_defaults)
284 return;
285
Matt Delco879b3c12020-06-17 13:10:22 +0530286 if (!config->ssdb.bdf_value)
287 config->ssdb.bdf_value = PCI_DEVFN(CIO2_PCI_DEV, CIO2_PCI_FN);
288
Matt Delco1245b1e2020-06-17 07:26:55 +0530289 if (!config->ssdb.platform)
290 config->ssdb.platform = PLATFORM_SKC;
291
292 if (!config->ssdb.flash_support)
293 config->ssdb.flash_support = FLASH_DISABLE;
294
295 if (!config->ssdb.privacy_led)
296 config->ssdb.privacy_led = PRIVACY_LED_A_16mA;
297
298 if (!config->ssdb.mipi_define)
299 config->ssdb.mipi_define = MIPI_INFO_ACPI_DEFINED;
300
301 if (!config->ssdb.mclk_speed)
302 config->ssdb.mclk_speed = CLK_FREQ_19_2MHZ;
Matt Delco879b3c12020-06-17 13:10:22 +0530303
304 if (!config->ssdb.lanes_used) {
305 cio2_config = cio2 ? cio2->chip_info : NULL;
306
307 if (!cio2_config) {
308 printk(BIOS_ERR, "Failed to get CIO2 config\n");
309 } else if (cio2_config->device_type != INTEL_ACPI_CAMERA_CIO2) {
310 printk(BIOS_ERR, "Device type isn't CIO2: %u\n",
311 (u32)cio2_config->device_type);
312 } else if (config->ssdb.link_used >= cio2_config->cio2_num_ports) {
313 printk(BIOS_ERR, "%u exceeds CIO2's %u links\n",
314 (u32)config->ssdb.link_used,
315 (u32)cio2_config->cio2_num_ports);
316 } else {
317 config->ssdb.lanes_used =
318 cio2_config->cio2_lanes_used[config->ssdb.link_used];
319 }
320 }
Matt Delco1245b1e2020-06-17 07:26:55 +0530321}
322
323/*
324 * Adds settings for a camera sensor device (typically at "\_SB.PCI0.I2Cx.CAMy"). The drivers
325 * for Linux tends to expect the camera sensor device and any related nvram / vcm devices to be
326 * separate ACPI devices, while the drivers for Windows want all of these to be grouped
327 * together in the camera sensor ACPI device. This implementation tries to satisfy both,
328 * though the unfortunate tradeoff is that the same I2C address for nvram and vcm is advertised
329 * by multiple devices in ACPI (via "_CRS"). The Windows driver can use the "_DSM" method to
330 * disambiguate the I2C resources in the camera sensor ACPI device. Drivers for Windows
331 * typically query "SSDB" for configuration information (represented as a binary blob dump of
332 * struct), while Linux drivers typically consult individual parameters in "_DSD".
333 *
334 * The tree of tables in "_DSD" is analogous to what's used for the "CIO2" device. The _DSD
335 * specifies a child table for the sensor's port (e.g., PRT0 for "port0"--this implementation
336 * assumes a camera only has 1 port). The PRT0 table specifies a table for each endpoint
337 * (though only 1 endpoint is supported by this implementation so the table only has an
338 * "endpoint0" that points to a EP00 table). The EP00 table primarily describes the # of lanes
339 * in "data-lines", a list of frequencies in "list-frequencies", and specifies the name of the
340 * other side in "remote-endpoint" (typically "\_SB.PCI0.CIO2").
341 */
342static void camera_fill_sensor(const struct device *dev)
343{
344 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
345 struct acpi_dp *ep00 = NULL;
346 struct acpi_dp *prt0 = NULL;
347 struct acpi_dp *dsd = NULL;
348 struct acpi_dp *remote = NULL;
349 const char *vcm_name = NULL;
350 struct acpi_dp *lens_focus = NULL;
Matt Delco879b3c12020-06-17 13:10:22 +0530351 const char *remote_name;
352 struct device *cio2 = pcidev_on_root(CIO2_PCI_DEV, CIO2_PCI_FN);
Matt Delco1245b1e2020-06-17 07:26:55 +0530353
Matt Delco1ffee9d2020-06-17 12:55:35 +0530354 camera_generate_pld(dev);
355
Matt Delco879b3c12020-06-17 13:10:22 +0530356 camera_fill_ssdb_defaults(config);
Matt Delco1245b1e2020-06-17 07:26:55 +0530357
Matt Delco964033f2020-06-17 12:49:43 +0530358 /* _DSM */
359 camera_generate_dsm(dev);
360
Matt Delco1245b1e2020-06-17 07:26:55 +0530361 ep00 = acpi_dp_new_table("EP00");
362 acpi_dp_add_integer(ep00, "endpoint", DEFAULT_ENDPOINT);
363 acpi_dp_add_integer(ep00, "clock-lanes", 0);
364
365 if (config->ssdb.lanes_used > 0) {
366 struct acpi_dp *lanes = acpi_dp_new_table("data-lanes");
367 uint32_t i;
368 for (i = 1; i <= config->ssdb.lanes_used; ++i)
369 acpi_dp_add_integer(lanes, NULL, i);
370 acpi_dp_add_array(ep00, lanes);
371 }
372
373 if (config->num_freq_entries) {
374 struct acpi_dp *freq = acpi_dp_new_table("link-frequencies");
375 uint32_t i;
376 for (i = 0; i < config->num_freq_entries && i < MAX_LINK_FREQ_ENTRIES; ++i)
377 acpi_dp_add_integer(freq, NULL, config->link_freq[i]);
378 acpi_dp_add_array(ep00, freq);
379 }
380
381 remote = acpi_dp_new_table("remote-endpoint");
382
Matt Delco879b3c12020-06-17 13:10:22 +0530383 if (config->remote_name) {
384 remote_name = config->remote_name;
385 } else {
386 if (cio2)
387 remote_name = acpi_device_path(cio2);
388 else
389 remote_name = DEFAULT_REMOTE_NAME;
390 }
391
392 acpi_dp_add_reference(remote, NULL, remote_name);
Matt Delco1245b1e2020-06-17 07:26:55 +0530393 acpi_dp_add_integer(remote, NULL, config->ssdb.link_used);
394 acpi_dp_add_integer(remote, NULL, DEFAULT_ENDPOINT);
395 acpi_dp_add_array(ep00, remote);
396
397 prt0 = acpi_dp_new_table("PRT0");
398
399 acpi_dp_add_integer(prt0, "port", 0);
400 acpi_dp_add_child(prt0, "endpoint0", ep00);
401 dsd = acpi_dp_new_table("_DSD");
402
403 acpi_dp_add_integer(dsd, "clock-frequency", config->ssdb.mclk_speed);
404
405 if (config->ssdb.degree)
406 acpi_dp_add_integer(dsd, "rotation", 180);
407
408 if (config->ssdb.vcm_type) {
409 if (config->vcm_name) {
410 vcm_name = config->vcm_name;
411 } else {
412 const struct device_path path = {
413 .type = DEVICE_PATH_I2C,
414 .i2c.device = config->vcm_address,
415 };
416 struct device *vcm_dev = find_dev_path(dev->bus, &path);
417 struct drivers_intel_mipi_camera_config *vcm_config;
418 vcm_config = vcm_dev ? vcm_dev->chip_info : NULL;
419
420 if (!vcm_config)
421 printk(BIOS_ERR, "Failed to get VCM\n");
422 else if (vcm_config->device_type != INTEL_ACPI_CAMERA_VCM)
423 printk(BIOS_ERR, "Device isn't VCM\n");
424 else
425 vcm_name = acpi_device_path(vcm_dev);
426 }
427 }
428
429 if (vcm_name) {
430 lens_focus = acpi_dp_new_table("lens-focus");
431 acpi_dp_add_reference(lens_focus, NULL, vcm_name);
432 acpi_dp_add_array(dsd, lens_focus);
433 }
434
435 acpi_dp_add_child(dsd, "port0", prt0);
436 acpi_dp_write(dsd);
437
438 acpigen_write_method_serialized("SSDB", 0);
439 acpigen_write_return_byte_buffer((uint8_t *)&config->ssdb, sizeof(config->ssdb));
440 acpigen_pop_len(); /* Method */
441
442 /* Fill Power Sequencing Data */
443 if (config->num_pwdb_entries > 0) {
444 acpigen_write_method_serialized("PWDB", 0);
445 acpigen_write_return_byte_buffer((uint8_t *)&config->pwdb,
446 sizeof(struct intel_pwdb) *
447 config->num_pwdb_entries);
448 acpigen_pop_len(); /* Method */
449 }
450}
451
Matt Delcoc3a83bf2020-06-16 12:02:34 +0530452static void camera_fill_nvm(const struct device *dev)
453{
454 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
455 struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
456
457 /* It might be possible to default size or width based on type. */
458 if (!config->disable_nvm_defaults && !config->nvm_pagesize)
459 config->nvm_pagesize = 1;
460
461 if (!config->disable_nvm_defaults && !config->nvm_readonly)
462 config->nvm_readonly = 1;
463
464 if (config->nvm_size)
465 acpi_dp_add_integer(dsd, "size", config->nvm_size);
466
467 if (config->nvm_pagesize)
468 acpi_dp_add_integer(dsd, "pagesize", config->nvm_pagesize);
469
470 if (config->nvm_readonly)
471 acpi_dp_add_integer(dsd, "read-only", config->nvm_readonly);
472
473 if (config->nvm_width)
474 acpi_dp_add_integer(dsd, "address-width", config->nvm_width);
475
476 acpi_dp_write(dsd);
477}
478
479static void camera_fill_vcm(const struct device *dev)
480{
481 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
482 struct acpi_dp *dsd;
483
484 if (!config->vcm_compat)
485 return;
486
487 dsd = acpi_dp_new_table("_DSD");
488 acpi_dp_add_string(dsd, "compatible", config->vcm_compat);
489 acpi_dp_write(dsd);
490}
491
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530492static int get_resource_index(const struct resource_config *res_config)
493{
494 enum ctrl_type type = resource_get_ctrl_type(res_config);
495 const struct clk_config *clk_config;
496 const struct gpio_config *gpio_config;
497 unsigned int i;
498 uint8_t res_id;
499
500 switch (type) {
501 case IMGCLK:
502 clk_config = resource_clk_config(res_config);
503 res_id = clk_config->clknum;
504 break;
505 case GPIO:
506 gpio_config = resource_gpio_config(res_config);
507 res_id = gpio_config->gpio_num;
508 break;
509 default:
510 printk(BIOS_ERR, "Unsupported power operation: %x\n"
511 "OS camera driver will likely not work", type);
512 return -1;
513 }
514
515 for (i = 0; i < res_mgr.cnt; i++)
516 if (res_mgr.resource[i].type == type && res_mgr.resource[i].id == res_id)
517 return i;
518
519 return -1;
520}
521
522static void add_guarded_method_namestring(struct resource_config *res_config, int res_index)
523{
524 char method_name[ACPI_NAME_BUFFER_SIZE];
525 enum action_type action = resource_get_action_type(res_config);
526
527 switch (action) {
528 case ENABLE:
529 snprintf(method_name, sizeof(method_name), ENABLE_METHOD_FORMAT, res_index);
530 break;
531 case DISABLE:
532 snprintf(method_name, sizeof(method_name), DISABLE_METHOD_FORMAT, res_index);
533 break;
534 default:
535 snprintf(method_name, sizeof(method_name), UNKNOWN_METHOD_FORMAT, res_index);
536 printk(BIOS_ERR, "Unsupported resource action: %x\n", action);
537 }
538
539 acpigen_emit_namestring(method_name);
540}
541
542static void call_guarded_method(struct resource_config *res_config)
543{
544 int res_index;
545
546 if (res_config == NULL)
547 return;
548
549 res_index = get_resource_index(res_config);
550
551 if (res_index != -1)
552 add_guarded_method_namestring(res_config, res_index);
553}
554
555static void add_clk_op(const struct clk_config *clk_config, enum action_type action)
556{
557 if (clk_config == NULL)
558 return;
559
560 switch (action) {
561 case ENABLE:
562 acpigen_write_if();
563 acpigen_emit_ext_op(COND_REFOF_OP);
564 acpigen_emit_string(CLK_ENABLE_METHOD);
565 acpigen_emit_namestring(CLK_ENABLE_METHOD);
566 acpigen_write_integer(clk_config->clknum);
567 acpigen_write_integer(clk_config->freq);
568 acpigen_pop_len(); /* CondRefOf */
569 break;
570 case DISABLE:
571 acpigen_write_if();
572 acpigen_emit_ext_op(COND_REFOF_OP);
573 acpigen_emit_string(CLK_DISABLE_METHOD);
574 acpigen_emit_namestring(CLK_DISABLE_METHOD);
575 acpigen_write_integer(clk_config->clknum);
576 acpigen_pop_len(); /* CondRefOf */
577 break;
578 default:
579 acpigen_write_debug_string("Unsupported clock action");
580 printk(BIOS_ERR, "Unsupported clock action: %x\n"
581 "OS camera driver will likely not work", action);
582 }
583}
584
585static void add_gpio_op(const struct gpio_config *gpio_config, enum action_type action)
586{
587 if (gpio_config == NULL)
588 return;
589
590 switch (action) {
591 case ENABLE:
592 acpigen_soc_set_tx_gpio(gpio_config->gpio_num);
593 break;
594 case DISABLE:
595 acpigen_soc_clear_tx_gpio(gpio_config->gpio_num);
596 break;
597 default:
598 acpigen_write_debug_string("Unsupported GPIO action");
599 printk(BIOS_ERR, "Unsupported GPIO action: %x\n"
600 "OS camera driver will likely not work\n", action);
601 }
602}
603
604static void add_power_operation(const struct resource_config *res_config)
605{
606 const struct clk_config *clk_config;
607 const struct gpio_config *gpio_config;
608 enum ctrl_type type = resource_get_ctrl_type(res_config);
609 enum action_type action = resource_get_action_type(res_config);
610
611 if (res_config == NULL)
612 return;
613
614 switch (type) {
615 case IMGCLK:
616 clk_config = resource_clk_config(res_config);
617 add_clk_op(clk_config, action);
618 break;
619 case GPIO:
620 gpio_config = resource_gpio_config(res_config);
621 add_gpio_op(gpio_config, action);
622 break;
623 default:
624 printk(BIOS_ERR, "Unsupported power operation: %x\n"
625 "OS camera driver will likely not work\n", type);
626 break;
627 }
628}
629
630static void write_guard_variable(uint8_t res_index)
631{
632 char varname[ACPI_NAME_BUFFER_SIZE];
633
634 snprintf(varname, sizeof(varname), GUARD_VARIABLE_FORMAT, res_index);
635 acpigen_write_name_integer(varname, 0);
636}
637
638static void write_enable_method(struct resource_config *res_config, uint8_t res_index)
639{
640 char method_name[ACPI_NAME_BUFFER_SIZE];
641 char varname[ACPI_NAME_BUFFER_SIZE];
642
643 snprintf(varname, sizeof(varname), GUARD_VARIABLE_FORMAT, res_index);
644
645 snprintf(method_name, sizeof(method_name), ENABLE_METHOD_FORMAT, res_index);
646
647 acpigen_write_method_serialized(method_name, 0);
648 acpigen_write_if_lequal_namestr_int(varname, 0);
649 resource_set_action_type(res_config, ENABLE);
650 add_power_operation(res_config);
651 acpigen_pop_len(); /* if */
652
653 acpigen_emit_byte(INCREMENT_OP);
654 acpigen_emit_namestring(varname);
655 acpigen_pop_len(); /* method_name */
656}
657
658static void write_disable_method(struct resource_config *res_config, uint8_t res_index)
659{
660 char method_name[ACPI_NAME_BUFFER_SIZE];
661 char varname[ACPI_NAME_BUFFER_SIZE];
662
663 snprintf(varname, sizeof(varname), GUARD_VARIABLE_FORMAT, res_index);
664
665 snprintf(method_name, sizeof(method_name), DISABLE_METHOD_FORMAT, res_index);
666
667 acpigen_write_method_serialized(method_name, 0);
668 acpigen_write_if();
669 acpigen_emit_byte(LGREATER_OP);
670 acpigen_emit_namestring(varname);
671 acpigen_write_integer(0x0);
672 acpigen_emit_byte(DECREMENT_OP);
673 acpigen_emit_namestring(varname);
674 acpigen_pop_len(); /* if */
675
676 acpigen_write_if_lequal_namestr_int(varname, 0);
677 resource_set_action_type(res_config, DISABLE);
678 add_power_operation(res_config);
679 acpigen_pop_len(); /* if */
680 acpigen_pop_len(); /* method_name */
681}
682
683static void add_guarded_operations(const struct drivers_intel_mipi_camera_config *config,
684 const struct operation_seq *seq)
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530685{
686 unsigned int i;
687 uint8_t index;
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530688 uint8_t res_id;
689 struct resource_config res_config;
690 int res_index;
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530691
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530692 for (i = 0; i < seq->ops_cnt && i < MAX_PWR_OPS; i++) {
693 index = seq->ops[i].index;
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530694 switch (seq->ops[i].type) {
695 case IMGCLK:
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530696 res_id = config->clk_panel.clks[index].clknum;
697 resource_set_clk_config(&res_config, &config->clk_panel.clks[index]);
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530698 break;
699 case GPIO:
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530700 res_id = config->gpio_panel.gpio[index].gpio_num;
701 resource_set_gpio_config(&res_config, &config->gpio_panel.gpio[index]);
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530702 break;
703 default:
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530704 printk(BIOS_ERR, "Unsupported power operation: %x\n"
705 "OS camera driver will likely not work\n",
706 seq->ops[i].type);
707 return;
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530708 }
709
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530710 res_index = get_resource_index(&res_config);
711
712 if (res_index == -1) {
713 if (res_mgr.cnt >= MAX_GUARDED_RESOURCES) {
714 printk(BIOS_ERR, "Unable to add guarded camera resource\n"
715 "OS camera driver will likely not work\n");
716 return;
717 }
718
719 res_mgr.resource[res_mgr.cnt].id = res_id;
720 res_mgr.resource[res_mgr.cnt].type = seq->ops[i].type;
721
722 write_guard_variable(res_mgr.cnt);
723 write_enable_method(&res_config, res_mgr.cnt);
724 write_disable_method(&res_config, res_mgr.cnt);
725
726 res_mgr.cnt++;
727 }
728 }
729}
730
731static void fill_power_res_sequence(struct drivers_intel_mipi_camera_config *config,
732 struct operation_seq *seq)
733{
734 struct resource_config res_config;
735 unsigned int i;
736 uint8_t index;
737
738 for (i = 0; i < seq->ops_cnt && i < MAX_PWR_OPS; i++) {
739 index = seq->ops[i].index;
740
741 switch (seq->ops[i].type) {
742 case IMGCLK:
743 resource_set_clk_config(&res_config, &config->clk_panel.clks[index]);
744 break;
745 case GPIO:
746 resource_set_gpio_config(&res_config, &config->gpio_panel.gpio[index]);
747 break;
748 default:
749 printk(BIOS_ERR, "Unsupported power operation: %x\n"
750 "OS camera driver will likely not work\n",
751 seq->ops[i].type);
752 return;
753 }
754
755 resource_set_action_type(&res_config, seq->ops[i].action);
756 call_guarded_method(&res_config);
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530757 if (seq->ops[i].delay_ms)
758 acpigen_write_sleep(seq->ops[i].delay_ms);
759 }
760}
761
Matt Delco7d002932020-06-16 11:39:52 +0530762static void write_pci_camera_device(const struct device *dev)
V Sowmya9f8023a2017-02-28 17:52:05 +0530763{
764 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
Matt Delco7d002932020-06-16 11:39:52 +0530765
766 if (dev->path.type != DEVICE_PATH_PCI) {
767 printk(BIOS_ERR, "CIO2/IMGU devices require PCI\n");
768 return;
769 }
770
771 acpigen_write_device(acpi_device_name(dev));
772 acpigen_write_ADR_pci_device(dev);
773 acpigen_write_name_string("_DDN", config->device_type == INTEL_ACPI_CAMERA_CIO2 ?
774 "Camera and Imaging Subsystem" : "Imaging Unit");
775}
776
777static void write_i2c_camera_device(const struct device *dev, const char *scope)
778{
779 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
V Sowmya9f8023a2017-02-28 17:52:05 +0530780 struct acpi_i2c i2c = {
781 .address = dev->path.i2c.device,
782 .mode_10bit = dev->path.i2c.mode_10bit,
783 .speed = I2C_SPEED_FAST,
784 .resource = scope,
785 };
786
Matt Delco7d002932020-06-16 11:39:52 +0530787 if (dev->path.type != DEVICE_PATH_I2C) {
788 printk(BIOS_ERR, "Non-CIO2/IMGU devices require I2C\n");
V Sowmya9f8023a2017-02-28 17:52:05 +0530789 return;
Matt Delco7d002932020-06-16 11:39:52 +0530790 }
V Sowmya9f8023a2017-02-28 17:52:05 +0530791
V Sowmya9f8023a2017-02-28 17:52:05 +0530792 acpigen_write_device(acpi_device_name(dev));
Matt Delco7d002932020-06-16 11:39:52 +0530793
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530794 /* add power resource */
795 if (config->has_power_resource) {
796 acpigen_write_power_res(POWER_RESOURCE_NAME, 0, 0, NULL, 0);
797 acpigen_write_name_integer("STA", 0);
798 acpigen_write_STA_ext("STA");
799
800 acpigen_write_method_serialized("_ON", 0);
801 acpigen_write_if();
802 acpigen_emit_byte(LEQUAL_OP);
803 acpigen_emit_namestring("STA");
804 acpigen_write_integer(0);
805
806 fill_power_res_sequence(config, &config->on_seq);
807
808 acpigen_write_store_op_to_namestr(1, "STA");
809 acpigen_pop_len(); /* if */
810 acpigen_pop_len(); /* _ON */
811
812 /* _OFF operations */
813 acpigen_write_method_serialized("_OFF", 0);
814 acpigen_write_if();
815 acpigen_emit_byte(LEQUAL_OP);
816 acpigen_emit_namestring("STA");
817 acpigen_write_integer(1);
818
819 fill_power_res_sequence(config, &config->off_seq);
820
821 acpigen_write_store_op_to_namestr(0, "STA");
822 acpigen_pop_len(); /* if */
823 acpigen_pop_len(); /* _ON */
824
825 acpigen_pop_len(); /* Power Resource */
826 }
827
Matt Delco7d002932020-06-16 11:39:52 +0530828 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR)
829 acpigen_write_name_integer("_ADR", 0);
830
831 if (config->acpi_hid)
832 acpigen_write_name_string("_HID", config->acpi_hid);
833 else if (config->device_type == INTEL_ACPI_CAMERA_VCM)
834 acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);
835 else if (config->device_type == INTEL_ACPI_CAMERA_NVM)
836 acpigen_write_name_string("_HID", "INT3499");
837
V Sowmya9f8023a2017-02-28 17:52:05 +0530838 acpigen_write_name_integer("_UID", config->acpi_uid);
839 acpigen_write_name_string("_DDN", config->chip_name);
Hung-Te Linb4be50c2018-09-10 10:55:49 +0800840 acpigen_write_STA(acpi_device_status(dev));
V Sowmya9f8023a2017-02-28 17:52:05 +0530841
842 /* Resources */
843 acpigen_write_name("_CRS");
844 acpigen_write_resourcetemplate_header();
845 acpi_device_write_i2c(&i2c);
V Sowmya9f8023a2017-02-28 17:52:05 +0530846
Matt Delco7d002932020-06-16 11:39:52 +0530847 /*
848 * The optional vcm/nvram devices are presumed to be on the same I2C bus as the camera
849 * sensor.
850 */
851 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR &&
852 config->ssdb.vcm_type && config->vcm_address) {
853 struct acpi_i2c i2c_vcm = i2c;
854 i2c_vcm.address = config->vcm_address;
855 acpi_device_write_i2c(&i2c_vcm);
V Sowmya9f8023a2017-02-28 17:52:05 +0530856 }
857
Matt Delco7d002932020-06-16 11:39:52 +0530858 if (config->device_type == INTEL_ACPI_CAMERA_SENSOR &&
859 config->ssdb.rom_type && config->rom_address) {
860 struct acpi_i2c i2c_rom = i2c;
861 i2c_rom.address = config->rom_address;
862 acpi_device_write_i2c(&i2c_rom);
863 }
864
865 acpigen_write_resourcetemplate_footer();
866}
867
868static void write_camera_device_common(const struct device *dev)
869{
870 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
871
872 /* Mark it as Camera related device */
873 if (config->device_type == INTEL_ACPI_CAMERA_CIO2 ||
874 config->device_type == INTEL_ACPI_CAMERA_IMGU ||
875 config->device_type == INTEL_ACPI_CAMERA_SENSOR ||
876 config->device_type == INTEL_ACPI_CAMERA_VCM) {
877 acpigen_write_name_integer("CAMD", config->device_type);
878 }
Matt Delcoc3a83bf2020-06-16 12:02:34 +0530879
Sugnan Prabhu Sb087a942020-05-21 20:41:03 +0530880 if (config->pr0 || config->has_power_resource) {
881 acpigen_write_name("_PR0");
882 acpigen_write_package(1);
883 if (config->pr0)
884 acpigen_emit_namestring(config->pr0); /* External power resource */
885 else
886 acpigen_emit_namestring(POWER_RESOURCE_NAME);
887
888 acpigen_pop_len(); /* _PR0 */
889 }
890
Matt Delcoc3a83bf2020-06-16 12:02:34 +0530891 switch (config->device_type) {
Matt Delco879b3c12020-06-17 13:10:22 +0530892 case INTEL_ACPI_CAMERA_CIO2:
893 camera_fill_cio2(dev);
894 break;
Matt Delco1245b1e2020-06-17 07:26:55 +0530895 case INTEL_ACPI_CAMERA_SENSOR:
896 camera_fill_sensor(dev);
897 break;
Matt Delcoc3a83bf2020-06-16 12:02:34 +0530898 case INTEL_ACPI_CAMERA_VCM:
899 camera_fill_vcm(dev);
900 break;
901 case INTEL_ACPI_CAMERA_NVM:
902 camera_fill_nvm(dev);
903 break;
904 default:
905 break;
906 }
Matt Delco7d002932020-06-16 11:39:52 +0530907}
908
909static void camera_fill_ssdt(const struct device *dev)
910{
911 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530912 const char *scope = NULL;
913 const struct device *pdev;
Matt Delco7d002932020-06-16 11:39:52 +0530914
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530915 if (!dev->enabled)
Matt Delco7d002932020-06-16 11:39:52 +0530916 return;
917
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530918 if (config->has_power_resource) {
919 pdev = dev->bus->dev;
920 if (!pdev || !pdev->enabled)
921 return;
922
923 scope = acpi_device_scope(pdev);
924 if (!scope)
925 return;
926
927 acpigen_write_scope(scope);
928 add_guarded_operations(config, &config->on_seq);
929 add_guarded_operations(config, &config->off_seq);
930 acpigen_pop_len(); /* Guarded power resource operations scope */
931 }
932
Matt Delco7d002932020-06-16 11:39:52 +0530933 /* Device */
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530934 scope = acpi_device_scope(dev);
935 if (!scope)
936 return;
937
Matt Delco7d002932020-06-16 11:39:52 +0530938 acpigen_write_scope(scope);
939
940 if (config->device_type == INTEL_ACPI_CAMERA_CIO2 ||
941 config->device_type == INTEL_ACPI_CAMERA_IMGU)
942 write_pci_camera_device(dev);
943 else
944 write_i2c_camera_device(dev, scope);
945
946 write_camera_device_common(dev);
V Sowmya9f8023a2017-02-28 17:52:05 +0530947
948 acpigen_pop_len(); /* Device */
949 acpigen_pop_len(); /* Scope */
Matt Delco7d002932020-06-16 11:39:52 +0530950
951 if (dev->path.type == DEVICE_PATH_PCI) {
952 printk(BIOS_INFO, "%s: %s PCI address 0%x\n", acpi_device_path(dev),
953 dev->chip_ops->name, dev->path.pci.devfn);
954 } else {
955 printk(BIOS_INFO, "%s: %s I2C address 0%xh\n", acpi_device_path(dev),
956 dev->chip_ops->name, dev->path.i2c.device);
957 }
V Sowmya9f8023a2017-02-28 17:52:05 +0530958}
959
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600960static const char *camera_acpi_name(const struct device *dev)
V Sowmya9f8023a2017-02-28 17:52:05 +0530961{
Matt Delco7d002932020-06-16 11:39:52 +0530962 const char *prefix = NULL;
Sugnan Prabhu S6d9f2432020-07-02 13:02:23 +0530963 static char name[ACPI_NAME_BUFFER_SIZE];
V Sowmya9f8023a2017-02-28 17:52:05 +0530964 struct drivers_intel_mipi_camera_config *config = dev->chip_info;
Matt Delco7d002932020-06-16 11:39:52 +0530965
966 if (config->acpi_name)
967 return config->acpi_name;
968
969 switch (config->device_type) {
970 case INTEL_ACPI_CAMERA_CIO2:
971 return "CIO2";
972 case INTEL_ACPI_CAMERA_IMGU:
973 return "IMGU";
974 case INTEL_ACPI_CAMERA_PMIC:
975 return "PMIC";
976 case INTEL_ACPI_CAMERA_SENSOR:
977 prefix = "CAM";
978 break;
979 case INTEL_ACPI_CAMERA_VCM:
980 prefix = "VCM";
981 break;
982 case INTEL_ACPI_CAMERA_NVM:
983 prefix = "NVM";
984 break;
985 default:
986 printk(BIOS_ERR, "Invalid device type: %x\n", config->device_type);
987 return NULL;
988 }
989
990 /*
991 * The camera # knows which link # they use, so that's used as the basis for the
992 * instance #. The VCM and NVM don't have this information, so the best we can go on is
993 * the _UID.
994 */
995 snprintf(name, sizeof(name), "%s%1u", prefix,
996 config->device_type == INTEL_ACPI_CAMERA_SENSOR ?
997 config->ssdb.link_used : config->acpi_uid);
998 return name;
V Sowmya9f8023a2017-02-28 17:52:05 +0530999}
1000
1001static struct device_operations camera_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +02001002 .read_resources = noop_read_resources,
1003 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +02001004 .acpi_name = camera_acpi_name,
1005 .acpi_fill_ssdt = camera_fill_ssdt,
V Sowmya9f8023a2017-02-28 17:52:05 +05301006};
1007
1008static void camera_enable(struct device *dev)
1009{
1010 dev->ops = &camera_ops;
1011}
1012
1013struct chip_operations drivers_intel_mipi_camera_ops = {
1014 CHIP_NAME("Intel MIPI Camera Device")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +01001015 .enable_dev = camera_enable
V Sowmya9f8023a2017-02-28 17:52:05 +05301016};