1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2023 Intel Corporation. All rights reserved. 4 * Intel Visual Sensing Controller ACE Linux driver 5 */ 6 7 /* 8 * To set ownership of camera sensor, there is specific command, which 9 * is sent via MEI protocol. That's a two-step scheme where the firmware 10 * first acks receipt of the command and later responses the command was 11 * executed. The command sending function uses "completion" as the 12 * synchronization mechanism. The notification for command is received 13 * via a mei callback which wakes up the caller. There can be only one 14 * outstanding command at a time. 15 * 16 * The power line of camera sensor is directly connected to IVSC instead 17 * of host, when camera sensor ownership is switched to host, sensor is 18 * already powered up by firmware. 19 */ 20 21 #include <linux/acpi.h> 22 #include <linux/completion.h> 23 #include <linux/delay.h> 24 #include <linux/kernel.h> 25 #include <linux/mei_cl_bus.h> 26 #include <linux/module.h> 27 #include <linux/mutex.h> 28 #include <linux/pm_runtime.h> 29 #include <linux/slab.h> 30 #include <linux/uuid.h> 31 #include <linux/workqueue.h> 32 33 /* indicating driver message */ 34 #define ACE_DRV_MSG 1 35 /* indicating set command */ 36 #define ACE_CMD_SET 4 37 /* command timeout determined experimentally */ 38 #define ACE_CMD_TIMEOUT (5 * HZ) 39 /* indicating the first command block */ 40 #define ACE_CMD_INIT_BLOCK 1 41 /* indicating the last command block */ 42 #define ACE_CMD_FINAL_BLOCK 1 43 /* size of camera status notification content */ 44 #define ACE_CAMERA_STATUS_SIZE 5 45 46 /* UUID used to get firmware id */ 47 #define ACE_GET_FW_ID_UUID UUID_LE(0x6167DCFB, 0x72F1, 0x4584, 0xBF, \ 48 0xE3, 0x84, 0x17, 0x71, 0xAA, 0x79, 0x0B) 49 50 /* UUID used to get csi device */ 51 #define MEI_CSI_UUID UUID_LE(0x92335FCF, 0x3203, 0x4472, \ 52 0xAF, 0x93, 0x7b, 0x44, 0x53, 0xAC, 0x29, 0xDA) 53 54 /* identify firmware event type */ 55 enum ace_event_type { 56 /* firmware ready */ 57 ACE_FW_READY = 0x8, 58 59 /* command response */ 60 ACE_CMD_RESPONSE = 0x10, 61 }; 62 63 /* identify camera sensor ownership */ 64 enum ace_camera_owner { 65 ACE_CAMERA_IVSC, 66 ACE_CAMERA_HOST, 67 }; 68 69 /* identify the command id supported by firmware IPC */ 70 enum ace_cmd_id { 71 /* used to switch camera sensor to host */ 72 ACE_SWITCH_CAMERA_TO_HOST = 0x13, 73 74 /* used to switch camera sensor to IVSC */ 75 ACE_SWITCH_CAMERA_TO_IVSC = 0x14, 76 77 /* used to get firmware id */ 78 ACE_GET_FW_ID = 0x1A, 79 }; 80 81 /* ACE command header structure */ 82 struct ace_cmd_hdr { 83 u32 firmware_id : 16; 84 u32 instance_id : 8; 85 u32 type : 5; 86 u32 rsp : 1; 87 u32 msg_tgt : 1; 88 u32 _hw_rsvd_1 : 1; 89 u32 param_size : 20; 90 u32 cmd_id : 8; 91 u32 final_block : 1; 92 u32 init_block : 1; 93 u32 _hw_rsvd_2 : 2; 94 } __packed; 95 96 /* ACE command parameter structure */ 97 union ace_cmd_param { 98 uuid_le uuid; 99 u32 param; 100 }; 101 102 /* ACE command structure */ 103 struct ace_cmd { 104 struct ace_cmd_hdr hdr; 105 union ace_cmd_param param; 106 } __packed; 107 108 /* ACE notification header */ 109 union ace_notif_hdr { 110 struct _confirm { 111 u32 status : 24; 112 u32 type : 5; 113 u32 rsp : 1; 114 u32 msg_tgt : 1; 115 u32 _hw_rsvd_1 : 1; 116 u32 param_size : 20; 117 u32 cmd_id : 8; 118 u32 final_block : 1; 119 u32 init_block : 1; 120 u32 _hw_rsvd_2 : 2; 121 } __packed ack; 122 123 struct _event { 124 u32 rsvd1 : 16; 125 u32 event_type : 8; 126 u32 type : 5; 127 u32 ack : 1; 128 u32 msg_tgt : 1; 129 u32 _hw_rsvd_1 : 1; 130 u32 rsvd2 : 30; 131 u32 _hw_rsvd_2 : 2; 132 } __packed event; 133 134 struct _response { 135 u32 event_id : 16; 136 u32 notif_type : 8; 137 u32 type : 5; 138 u32 rsp : 1; 139 u32 msg_tgt : 1; 140 u32 _hw_rsvd_1 : 1; 141 u32 event_data_size : 16; 142 u32 request_target : 1; 143 u32 request_type : 5; 144 u32 cmd_id : 8; 145 u32 _hw_rsvd_2 : 2; 146 } __packed response; 147 }; 148 149 /* ACE notification content */ 150 union ace_notif_cont { 151 u16 firmware_id; 152 u8 state_notif; 153 u8 camera_status[ACE_CAMERA_STATUS_SIZE]; 154 }; 155 156 /* ACE notification structure */ 157 struct ace_notif { 158 union ace_notif_hdr hdr; 159 union ace_notif_cont cont; 160 } __packed; 161 162 struct mei_ace { 163 struct mei_cl_device *cldev; 164 165 /* command ack */ 166 struct ace_notif cmd_ack; 167 /* command response */ 168 struct ace_notif cmd_response; 169 /* used to wait for command ack and response */ 170 struct completion cmd_completion; 171 /* lock used to prevent multiple call to send command */ 172 struct mutex lock; 173 174 /* used to construct command */ 175 u16 firmware_id; 176 177 struct device *csi_dev; 178 179 /* runtime PM link from ace to csi */ 180 struct device_link *csi_link; 181 182 struct work_struct work; 183 }; 184 185 static inline void init_cmd_hdr(struct ace_cmd_hdr *hdr) 186 { 187 memset(hdr, 0, sizeof(struct ace_cmd_hdr)); 188 189 hdr->type = ACE_CMD_SET; 190 hdr->msg_tgt = ACE_DRV_MSG; 191 hdr->init_block = ACE_CMD_INIT_BLOCK; 192 hdr->final_block = ACE_CMD_FINAL_BLOCK; 193 } 194 195 static int construct_command(struct mei_ace *ace, struct ace_cmd *cmd, 196 enum ace_cmd_id cmd_id) 197 { 198 union ace_cmd_param *param = &cmd->param; 199 struct ace_cmd_hdr *hdr = &cmd->hdr; 200 201 init_cmd_hdr(hdr); 202 203 hdr->cmd_id = cmd_id; 204 switch (cmd_id) { 205 case ACE_GET_FW_ID: 206 param->uuid = ACE_GET_FW_ID_UUID; 207 hdr->param_size = sizeof(param->uuid); 208 break; 209 case ACE_SWITCH_CAMERA_TO_IVSC: 210 param->param = 0; 211 hdr->firmware_id = ace->firmware_id; 212 hdr->param_size = sizeof(param->param); 213 break; 214 case ACE_SWITCH_CAMERA_TO_HOST: 215 hdr->firmware_id = ace->firmware_id; 216 break; 217 default: 218 return -EINVAL; 219 } 220 221 return hdr->param_size + sizeof(cmd->hdr); 222 } 223 224 /* send command to firmware */ 225 static int mei_ace_send(struct mei_ace *ace, struct ace_cmd *cmd, 226 size_t len, bool only_ack) 227 { 228 union ace_notif_hdr *resp_hdr = &ace->cmd_response.hdr; 229 union ace_notif_hdr *ack_hdr = &ace->cmd_ack.hdr; 230 struct ace_cmd_hdr *cmd_hdr = &cmd->hdr; 231 int ret; 232 233 mutex_lock(&ace->lock); 234 235 reinit_completion(&ace->cmd_completion); 236 237 ret = mei_cldev_send(ace->cldev, (u8 *)cmd, len); 238 if (ret < 0) 239 goto out; 240 241 ret = wait_for_completion_killable_timeout(&ace->cmd_completion, 242 ACE_CMD_TIMEOUT); 243 if (ret < 0) { 244 goto out; 245 } else if (!ret) { 246 ret = -ETIMEDOUT; 247 goto out; 248 } 249 250 if (ack_hdr->ack.cmd_id != cmd_hdr->cmd_id) { 251 ret = -EINVAL; 252 goto out; 253 } 254 255 /* command ack status */ 256 ret = ack_hdr->ack.status; 257 if (ret) { 258 ret = -EIO; 259 goto out; 260 } 261 262 if (only_ack) 263 goto out; 264 265 ret = wait_for_completion_killable_timeout(&ace->cmd_completion, 266 ACE_CMD_TIMEOUT); 267 if (ret < 0) { 268 goto out; 269 } else if (!ret) { 270 ret = -ETIMEDOUT; 271 goto out; 272 } else { 273 ret = 0; 274 } 275 276 if (resp_hdr->response.cmd_id != cmd_hdr->cmd_id) 277 ret = -EINVAL; 278 279 out: 280 mutex_unlock(&ace->lock); 281 282 return ret; 283 } 284 285 static int ace_set_camera_owner(struct mei_ace *ace, 286 enum ace_camera_owner owner) 287 { 288 enum ace_cmd_id cmd_id; 289 struct ace_cmd cmd; 290 int cmd_size; 291 int ret; 292 293 if (owner == ACE_CAMERA_IVSC) 294 cmd_id = ACE_SWITCH_CAMERA_TO_IVSC; 295 else 296 cmd_id = ACE_SWITCH_CAMERA_TO_HOST; 297 298 cmd_size = construct_command(ace, &cmd, cmd_id); 299 if (cmd_size >= 0) 300 ret = mei_ace_send(ace, &cmd, cmd_size, false); 301 else 302 ret = cmd_size; 303 304 return ret; 305 } 306 307 /* the first command downloaded to firmware */ 308 static inline int ace_get_firmware_id(struct mei_ace *ace) 309 { 310 struct ace_cmd cmd; 311 int cmd_size; 312 int ret; 313 314 cmd_size = construct_command(ace, &cmd, ACE_GET_FW_ID); 315 if (cmd_size >= 0) 316 ret = mei_ace_send(ace, &cmd, cmd_size, true); 317 else 318 ret = cmd_size; 319 320 return ret; 321 } 322 323 static void handle_command_response(struct mei_ace *ace, 324 struct ace_notif *resp, int len) 325 { 326 union ace_notif_hdr *hdr = &resp->hdr; 327 328 switch (hdr->response.cmd_id) { 329 case ACE_SWITCH_CAMERA_TO_IVSC: 330 case ACE_SWITCH_CAMERA_TO_HOST: 331 memcpy(&ace->cmd_response, resp, len); 332 complete(&ace->cmd_completion); 333 break; 334 case ACE_GET_FW_ID: 335 break; 336 default: 337 break; 338 } 339 } 340 341 static void handle_command_ack(struct mei_ace *ace, 342 struct ace_notif *ack, int len) 343 { 344 union ace_notif_hdr *hdr = &ack->hdr; 345 346 switch (hdr->ack.cmd_id) { 347 case ACE_GET_FW_ID: 348 ace->firmware_id = ack->cont.firmware_id; 349 fallthrough; 350 case ACE_SWITCH_CAMERA_TO_IVSC: 351 case ACE_SWITCH_CAMERA_TO_HOST: 352 memcpy(&ace->cmd_ack, ack, len); 353 complete(&ace->cmd_completion); 354 break; 355 default: 356 break; 357 } 358 } 359 360 /* callback for receive */ 361 static void mei_ace_rx(struct mei_cl_device *cldev) 362 { 363 struct mei_ace *ace = mei_cldev_get_drvdata(cldev); 364 struct ace_notif event; 365 union ace_notif_hdr *hdr = &event.hdr; 366 int ret; 367 368 ret = mei_cldev_recv(cldev, (u8 *)&event, sizeof(event)); 369 if (ret < 0) { 370 dev_err(&cldev->dev, "recv error: %d\n", ret); 371 return; 372 } 373 374 if (hdr->event.ack) { 375 handle_command_ack(ace, &event, ret); 376 return; 377 } 378 379 switch (hdr->event.event_type) { 380 case ACE_CMD_RESPONSE: 381 handle_command_response(ace, &event, ret); 382 break; 383 case ACE_FW_READY: 384 /* 385 * firmware ready notification sent to driver 386 * after HECI client connected with firmware. 387 */ 388 dev_dbg(&cldev->dev, "firmware ready\n"); 389 break; 390 default: 391 break; 392 } 393 } 394 395 static int mei_ace_setup_dev_link(struct mei_ace *ace) 396 { 397 struct device *dev = &ace->cldev->dev; 398 uuid_le uuid = MEI_CSI_UUID; 399 struct device *csi_dev; 400 char name[64]; 401 int ret; 402 403 snprintf(name, sizeof(name), "%s-%pUl", dev_name(dev->parent), &uuid); 404 405 csi_dev = device_find_child_by_name(dev->parent, name); 406 if (!csi_dev) { 407 ret = -EPROBE_DEFER; 408 goto err; 409 } else if (!dev_fwnode(csi_dev)) { 410 ret = -EPROBE_DEFER; 411 goto err_put; 412 } 413 414 /* setup link between mei_ace and mei_csi */ 415 ace->csi_link = device_link_add(csi_dev, dev, DL_FLAG_PM_RUNTIME | 416 DL_FLAG_RPM_ACTIVE | DL_FLAG_STATELESS); 417 if (!ace->csi_link) { 418 ret = -EINVAL; 419 dev_err(dev, "failed to link to %s\n", dev_name(csi_dev)); 420 goto err_put; 421 } 422 423 ace->csi_dev = csi_dev; 424 425 return 0; 426 427 err_put: 428 put_device(csi_dev); 429 430 err: 431 return ret; 432 } 433 434 /* switch camera to host before probe sensor device */ 435 static void mei_ace_post_probe_work(struct work_struct *work) 436 { 437 struct acpi_device *adev; 438 struct mei_ace *ace; 439 struct device *dev; 440 int ret; 441 442 ace = container_of(work, struct mei_ace, work); 443 dev = &ace->cldev->dev; 444 445 ret = ace_set_camera_owner(ace, ACE_CAMERA_HOST); 446 if (ret) { 447 dev_err(dev, "switch camera to host failed: %d\n", ret); 448 return; 449 } 450 451 adev = ACPI_COMPANION(dev->parent); 452 if (!adev) 453 return; 454 455 acpi_dev_clear_dependencies(adev); 456 } 457 458 static int mei_ace_probe(struct mei_cl_device *cldev, 459 const struct mei_cl_device_id *id) 460 { 461 struct device *dev = &cldev->dev; 462 struct mei_ace *ace; 463 int ret; 464 465 ace = devm_kzalloc(dev, sizeof(struct mei_ace), GFP_KERNEL); 466 if (!ace) 467 return -ENOMEM; 468 469 ace->cldev = cldev; 470 mutex_init(&ace->lock); 471 init_completion(&ace->cmd_completion); 472 INIT_WORK(&ace->work, mei_ace_post_probe_work); 473 474 mei_cldev_set_drvdata(cldev, ace); 475 476 ret = mei_cldev_enable(cldev); 477 if (ret < 0) { 478 dev_err(dev, "mei_cldev_enable failed: %d\n", ret); 479 goto destroy_mutex; 480 } 481 482 ret = mei_cldev_register_rx_cb(cldev, mei_ace_rx); 483 if (ret) { 484 dev_err(dev, "event cb registration failed: %d\n", ret); 485 goto err_disable; 486 } 487 488 ret = ace_get_firmware_id(ace); 489 if (ret) { 490 dev_err(dev, "get firmware id failed: %d\n", ret); 491 goto err_disable; 492 } 493 494 pm_runtime_set_active(dev); 495 pm_runtime_enable(dev); 496 497 ret = mei_ace_setup_dev_link(ace); 498 if (ret) 499 goto disable_pm; 500 501 schedule_work(&ace->work); 502 503 return 0; 504 505 disable_pm: 506 pm_runtime_disable(dev); 507 pm_runtime_set_suspended(dev); 508 509 err_disable: 510 mei_cldev_disable(cldev); 511 512 destroy_mutex: 513 mutex_destroy(&ace->lock); 514 515 return ret; 516 } 517 518 static void mei_ace_remove(struct mei_cl_device *cldev) 519 { 520 struct mei_ace *ace = mei_cldev_get_drvdata(cldev); 521 522 cancel_work_sync(&ace->work); 523 524 device_link_del(ace->csi_link); 525 put_device(ace->csi_dev); 526 527 pm_runtime_disable(&cldev->dev); 528 pm_runtime_set_suspended(&cldev->dev); 529 530 ace_set_camera_owner(ace, ACE_CAMERA_IVSC); 531 532 mutex_destroy(&ace->lock); 533 } 534 535 static int __maybe_unused mei_ace_runtime_suspend(struct device *dev) 536 { 537 struct mei_ace *ace = dev_get_drvdata(dev); 538 539 return ace_set_camera_owner(ace, ACE_CAMERA_IVSC); 540 } 541 542 static int __maybe_unused mei_ace_runtime_resume(struct device *dev) 543 { 544 struct mei_ace *ace = dev_get_drvdata(dev); 545 546 return ace_set_camera_owner(ace, ACE_CAMERA_HOST); 547 } 548 549 static const struct dev_pm_ops mei_ace_pm_ops = { 550 SET_RUNTIME_PM_OPS(mei_ace_runtime_suspend, 551 mei_ace_runtime_resume, NULL) 552 }; 553 554 #define MEI_ACE_UUID UUID_LE(0x5DB76CF6, 0x0A68, 0x4ED6, \ 555 0x9B, 0x78, 0x03, 0x61, 0x63, 0x5E, 0x24, 0x47) 556 557 static const struct mei_cl_device_id mei_ace_tbl[] = { 558 { .uuid = MEI_ACE_UUID, .version = MEI_CL_VERSION_ANY }, 559 { /* sentinel */ } 560 }; 561 MODULE_DEVICE_TABLE(mei, mei_ace_tbl); 562 563 static struct mei_cl_driver mei_ace_driver = { 564 .id_table = mei_ace_tbl, 565 .name = KBUILD_MODNAME, 566 567 .probe = mei_ace_probe, 568 .remove = mei_ace_remove, 569 570 .driver = { 571 .pm = &mei_ace_pm_ops, 572 }, 573 }; 574 575 module_mei_cl_driver(mei_ace_driver); 576 577 MODULE_AUTHOR("Wentong Wu <wentong.wu@intel.com>"); 578 MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>"); 579 MODULE_DESCRIPTION("Device driver for IVSC ACE"); 580 MODULE_LICENSE("GPL"); 581