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