1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2023 Intel Corporation. All rights reserved.
4 * Intel Visual Sensing Controller ACE Linux driver
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.
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.
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>
33 /* indicating driver message */
35 /* indicating set command */
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
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)
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)
54 /* identify firmware event type */
59 /* command response */
60 ACE_CMD_RESPONSE
= 0x10,
63 /* identify camera sensor ownership */
64 enum ace_camera_owner
{
69 /* identify the command id supported by firmware IPC */
71 /* used to switch camera sensor to host */
72 ACE_SWITCH_CAMERA_TO_HOST
= 0x13,
74 /* used to switch camera sensor to IVSC */
75 ACE_SWITCH_CAMERA_TO_IVSC
= 0x14,
77 /* used to get firmware id */
81 /* ACE command header structure */
96 /* ACE command parameter structure */
102 /* ACE command structure */
104 struct ace_cmd_hdr hdr
;
105 union ace_cmd_param param
;
108 /* ACE notification header */
109 union ace_notif_hdr
{
141 u32 event_data_size
: 16;
142 u32 request_target
: 1;
143 u32 request_type
: 5;
149 /* ACE notification content */
150 union ace_notif_cont
{
153 u8 camera_status
[ACE_CAMERA_STATUS_SIZE
];
156 /* ACE notification structure */
158 union ace_notif_hdr hdr
;
159 union ace_notif_cont cont
;
163 struct mei_cl_device
*cldev
;
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 */
174 /* used to construct command */
177 struct device
*csi_dev
;
179 /* runtime PM link from ace to csi */
180 struct device_link
*csi_link
;
182 struct work_struct work
;
185 static inline void init_cmd_hdr(struct ace_cmd_hdr
*hdr
)
187 memset(hdr
, 0, sizeof(struct ace_cmd_hdr
));
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
;
195 static int construct_command(struct mei_ace
*ace
, struct ace_cmd
*cmd
,
196 enum ace_cmd_id cmd_id
)
198 union ace_cmd_param
*param
= &cmd
->param
;
199 struct ace_cmd_hdr
*hdr
= &cmd
->hdr
;
203 hdr
->cmd_id
= cmd_id
;
206 param
->uuid
= ACE_GET_FW_ID_UUID
;
207 hdr
->param_size
= sizeof(param
->uuid
);
209 case ACE_SWITCH_CAMERA_TO_IVSC
:
211 hdr
->firmware_id
= ace
->firmware_id
;
212 hdr
->param_size
= sizeof(param
->param
);
214 case ACE_SWITCH_CAMERA_TO_HOST
:
215 hdr
->firmware_id
= ace
->firmware_id
;
221 return hdr
->param_size
+ sizeof(cmd
->hdr
);
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
)
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
;
233 mutex_lock(&ace
->lock
);
235 reinit_completion(&ace
->cmd_completion
);
237 ret
= mei_cldev_send(ace
->cldev
, (u8
*)cmd
, len
);
241 ret
= wait_for_completion_killable_timeout(&ace
->cmd_completion
,
250 if (ack_hdr
->ack
.cmd_id
!= cmd_hdr
->cmd_id
) {
255 /* command ack status */
256 ret
= ack_hdr
->ack
.status
;
265 ret
= wait_for_completion_killable_timeout(&ace
->cmd_completion
,
276 if (resp_hdr
->response
.cmd_id
!= cmd_hdr
->cmd_id
)
280 mutex_unlock(&ace
->lock
);
285 static int ace_set_camera_owner(struct mei_ace
*ace
,
286 enum ace_camera_owner owner
)
288 enum ace_cmd_id cmd_id
;
293 if (owner
== ACE_CAMERA_IVSC
)
294 cmd_id
= ACE_SWITCH_CAMERA_TO_IVSC
;
296 cmd_id
= ACE_SWITCH_CAMERA_TO_HOST
;
298 cmd_size
= construct_command(ace
, &cmd
, cmd_id
);
300 ret
= mei_ace_send(ace
, &cmd
, cmd_size
, false);
307 /* the first command downloaded to firmware */
308 static inline int ace_get_firmware_id(struct mei_ace
*ace
)
314 cmd_size
= construct_command(ace
, &cmd
, ACE_GET_FW_ID
);
316 ret
= mei_ace_send(ace
, &cmd
, cmd_size
, true);
323 static void handle_command_response(struct mei_ace
*ace
,
324 struct ace_notif
*resp
, int len
)
326 union ace_notif_hdr
*hdr
= &resp
->hdr
;
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
);
341 static void handle_command_ack(struct mei_ace
*ace
,
342 struct ace_notif
*ack
, int len
)
344 union ace_notif_hdr
*hdr
= &ack
->hdr
;
346 switch (hdr
->ack
.cmd_id
) {
348 ace
->firmware_id
= ack
->cont
.firmware_id
;
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
);
360 /* callback for receive */
361 static void mei_ace_rx(struct mei_cl_device
*cldev
)
363 struct mei_ace
*ace
= mei_cldev_get_drvdata(cldev
);
364 struct ace_notif event
;
365 union ace_notif_hdr
*hdr
= &event
.hdr
;
368 ret
= mei_cldev_recv(cldev
, (u8
*)&event
, sizeof(event
));
370 dev_err(&cldev
->dev
, "recv error: %d\n", ret
);
374 if (hdr
->event
.ack
) {
375 handle_command_ack(ace
, &event
, ret
);
379 switch (hdr
->event
.event_type
) {
380 case ACE_CMD_RESPONSE
:
381 handle_command_response(ace
, &event
, ret
);
385 * firmware ready notification sent to driver
386 * after HECI client connected with firmware.
388 dev_dbg(&cldev
->dev
, "firmware ready\n");
395 static int mei_ace_setup_dev_link(struct mei_ace
*ace
)
397 struct device
*dev
= &ace
->cldev
->dev
;
398 uuid_le uuid
= MEI_CSI_UUID
;
399 struct device
*csi_dev
;
403 snprintf(name
, sizeof(name
), "%s-%pUl", dev_name(dev
->parent
), &uuid
);
405 csi_dev
= device_find_child_by_name(dev
->parent
, name
);
409 } else if (!dev_fwnode(csi_dev
)) {
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
) {
419 dev_err(dev
, "failed to link to %s\n", dev_name(csi_dev
));
423 ace
->csi_dev
= csi_dev
;
434 /* switch camera to host before probe sensor device */
435 static void mei_ace_post_probe_work(struct work_struct
*work
)
437 struct acpi_device
*adev
;
442 ace
= container_of(work
, struct mei_ace
, work
);
443 dev
= &ace
->cldev
->dev
;
445 ret
= ace_set_camera_owner(ace
, ACE_CAMERA_HOST
);
447 dev_err(dev
, "switch camera to host failed: %d\n", ret
);
451 adev
= ACPI_COMPANION(dev
->parent
);
455 acpi_dev_clear_dependencies(adev
);
458 static int mei_ace_probe(struct mei_cl_device
*cldev
,
459 const struct mei_cl_device_id
*id
)
461 struct device
*dev
= &cldev
->dev
;
465 ace
= devm_kzalloc(dev
, sizeof(struct mei_ace
), GFP_KERNEL
);
470 mutex_init(&ace
->lock
);
471 init_completion(&ace
->cmd_completion
);
472 INIT_WORK(&ace
->work
, mei_ace_post_probe_work
);
474 mei_cldev_set_drvdata(cldev
, ace
);
476 ret
= mei_cldev_enable(cldev
);
478 dev_err(dev
, "mei_cldev_enable failed: %d\n", ret
);
482 ret
= mei_cldev_register_rx_cb(cldev
, mei_ace_rx
);
484 dev_err(dev
, "event cb registration failed: %d\n", ret
);
488 ret
= ace_get_firmware_id(ace
);
490 dev_err(dev
, "get firmware id failed: %d\n", ret
);
494 pm_runtime_set_active(dev
);
495 pm_runtime_enable(dev
);
497 ret
= mei_ace_setup_dev_link(ace
);
501 schedule_work(&ace
->work
);
506 pm_runtime_disable(dev
);
507 pm_runtime_set_suspended(dev
);
510 mei_cldev_disable(cldev
);
513 mutex_destroy(&ace
->lock
);
518 static void mei_ace_remove(struct mei_cl_device
*cldev
)
520 struct mei_ace
*ace
= mei_cldev_get_drvdata(cldev
);
522 cancel_work_sync(&ace
->work
);
524 device_link_del(ace
->csi_link
);
525 put_device(ace
->csi_dev
);
527 pm_runtime_disable(&cldev
->dev
);
528 pm_runtime_set_suspended(&cldev
->dev
);
530 ace_set_camera_owner(ace
, ACE_CAMERA_IVSC
);
532 mutex_destroy(&ace
->lock
);
535 static int __maybe_unused
mei_ace_runtime_suspend(struct device
*dev
)
537 struct mei_ace
*ace
= dev_get_drvdata(dev
);
539 return ace_set_camera_owner(ace
, ACE_CAMERA_IVSC
);
542 static int __maybe_unused
mei_ace_runtime_resume(struct device
*dev
)
544 struct mei_ace
*ace
= dev_get_drvdata(dev
);
546 return ace_set_camera_owner(ace
, ACE_CAMERA_HOST
);
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
)
554 #define MEI_ACE_UUID UUID_LE(0x5DB76CF6, 0x0A68, 0x4ED6, \
555 0x9B, 0x78, 0x03, 0x61, 0x63, 0x5E, 0x24, 0x47)
557 static const struct mei_cl_device_id mei_ace_tbl
[] = {
558 { .uuid
= MEI_ACE_UUID
, .version
= MEI_CL_VERSION_ANY
},
561 MODULE_DEVICE_TABLE(mei
, mei_ace_tbl
);
563 static struct mei_cl_driver mei_ace_driver
= {
564 .id_table
= mei_ace_tbl
,
565 .name
= KBUILD_MODNAME
,
567 .probe
= mei_ace_probe
,
568 .remove
= mei_ace_remove
,
571 .pm
= &mei_ace_pm_ops
,
575 module_mei_cl_driver(mei_ace_driver
);
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");