1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Intel ECLite opregion driver for talking to ECLite firmware running on
4  * Intel Integrated Sensor Hub (ISH) using ISH Transport Protocol (ISHTP)
5  *
6  * Copyright (c) 2021, Intel Corporation.
7  */
8 
9 #include <linux/acpi.h>
10 #include <linux/bitops.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/intel-ish-client-if.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/suspend.h>
19 #include <linux/types.h>
20 #include <linux/uuid.h>
21 #include <linux/uaccess.h>
22 
23 #define ECLITE_DATA_OPREGION_ID	0x9E
24 #define ECLITE_CMD_OPREGION_ID	0x9F
25 
26 #define ECL_MSG_DATA	0x1
27 #define ECL_MSG_EVENT	0x2
28 
29 #define ECL_ISH_READ	0x1
30 #define ECL_ISH_WRITE	0x2
31 #define ECL_ISH_HEADER_VERSION	0
32 
33 #define ECL_CL_RX_RING_SIZE	16
34 #define ECL_CL_TX_RING_SIZE	8
35 
36 #define ECL_DATA_OPR_BUFLEN	384
37 #define ECL_EVENTS_NOTIFY	333
38 
39 #define cmd_opr_offsetof(element)	offsetof(struct opregion_cmd, element)
40 #define cl_data_to_dev(opr_dev)	ishtp_device((opr_dev)->cl_device)
41 
42 #ifndef BITS_TO_BYTES
43 #define BITS_TO_BYTES(x) ((x) / 8)
44 #endif
45 
46 struct opregion_cmd {
47 	unsigned int command;
48 	unsigned int offset;
49 	unsigned int length;
50 	unsigned int event_id;
51 };
52 
53 struct opregion_data {
54 	char data[ECL_DATA_OPR_BUFLEN];
55 };
56 
57 struct opregion_context {
58 	struct opregion_cmd cmd_area;
59 	struct opregion_data data_area;
60 };
61 
62 struct ecl_message_header {
63 	unsigned int version:2;
64 	unsigned int data_type:2;
65 	unsigned int request_type:2;
66 	unsigned int offset:9;
67 	unsigned int data_len:9;
68 	unsigned int event:8;
69 };
70 
71 struct ecl_message {
72 	struct ecl_message_header header;
73 	char payload[ECL_DATA_OPR_BUFLEN];
74 };
75 
76 struct ishtp_opregion_dev {
77 	struct opregion_context opr_context;
78 	struct ishtp_cl *ecl_ishtp_cl;
79 	struct ishtp_cl_device *cl_device;
80 	struct ishtp_fw_client *fw_client;
81 	struct ishtp_cl_rb *rb;
82 	struct acpi_device *adev;
83 	unsigned int dsm_event_id;
84 	unsigned int ish_link_ready;
85 	unsigned int ish_read_done;
86 	unsigned int acpi_init_done;
87 	wait_queue_head_t read_wait;
88 	struct work_struct event_work;
89 	struct work_struct reset_work;
90 	/* lock for opregion context */
91 	struct mutex lock;
92 
93 };
94 
95 /* eclite ishtp client UUID: 6a19cc4b-d760-4de3-b14d-f25ebd0fbcd9 */
96 static const guid_t ecl_ishtp_guid =
97 	GUID_INIT(0x6a19cc4b, 0xd760, 0x4de3,
98 		  0xb1, 0x4d, 0xf2, 0x5e, 0xbd, 0xf, 0xbc, 0xd9);
99 
100 /* ACPI DSM UUID: 91d936a7-1f01-49c6-a6b4-72f00ad8d8a5 */
101 static const guid_t ecl_acpi_guid =
102 	GUID_INIT(0x91d936a7, 0x1f01, 0x49c6, 0xa6,
103 		  0xb4, 0x72, 0xf0, 0x0a, 0xd8, 0xd8, 0xa5);
104 
105 /**
106  * ecl_ish_cl_read() - Read data from eclite FW
107  *
108  * @opr_dev:  pointer to opregion device
109  *
110  * This function issues a read request to eclite FW and waits until it
111  * receives a response. When response is received the read data is copied to
112  * opregion buffer.
113  */
114 static int ecl_ish_cl_read(struct ishtp_opregion_dev *opr_dev)
115 {
116 	struct ecl_message_header header;
117 	int len, rv;
118 
119 	if (!opr_dev->ish_link_ready)
120 		return -EIO;
121 
122 	if ((opr_dev->opr_context.cmd_area.offset +
123 	     opr_dev->opr_context.cmd_area.length) > ECL_DATA_OPR_BUFLEN) {
124 		return -EINVAL;
125 	}
126 
127 	header.version = ECL_ISH_HEADER_VERSION;
128 	header.data_type = ECL_MSG_DATA;
129 	header.request_type = ECL_ISH_READ;
130 	header.offset = opr_dev->opr_context.cmd_area.offset;
131 	header.data_len = opr_dev->opr_context.cmd_area.length;
132 	header.event = opr_dev->opr_context.cmd_area.event_id;
133 	len = sizeof(header);
134 
135 	opr_dev->ish_read_done = false;
136 	rv = ishtp_cl_send(opr_dev->ecl_ishtp_cl, (uint8_t *)&header, len);
137 	if (rv) {
138 		dev_err(cl_data_to_dev(opr_dev), "ish-read : send failed\n");
139 		return -EIO;
140 	}
141 
142 	dev_dbg(cl_data_to_dev(opr_dev),
143 		"[ish_rd] Req: off : %x, len : %x\n",
144 		header.offset,
145 		header.data_len);
146 
147 	rv = wait_event_interruptible_timeout(opr_dev->read_wait,
148 					      opr_dev->ish_read_done,
149 					      2 * HZ);
150 	if (!rv) {
151 		dev_err(cl_data_to_dev(opr_dev),
152 			"[ish_rd] No response from firmware\n");
153 		return -EIO;
154 	}
155 
156 	return 0;
157 }
158 
159 /**
160  * ecl_ish_cl_write() - This function writes data to eclite FW.
161  *
162  * @opr_dev:  pointer to opregion device
163  *
164  * This function writes data to eclite FW.
165  */
166 static int ecl_ish_cl_write(struct ishtp_opregion_dev *opr_dev)
167 {
168 	struct ecl_message message;
169 	int len;
170 
171 	if (!opr_dev->ish_link_ready)
172 		return -EIO;
173 
174 	if ((opr_dev->opr_context.cmd_area.offset +
175 	     opr_dev->opr_context.cmd_area.length) > ECL_DATA_OPR_BUFLEN) {
176 		return -EINVAL;
177 	}
178 
179 	message.header.version = ECL_ISH_HEADER_VERSION;
180 	message.header.data_type = ECL_MSG_DATA;
181 	message.header.request_type = ECL_ISH_WRITE;
182 	message.header.offset = opr_dev->opr_context.cmd_area.offset;
183 	message.header.data_len = opr_dev->opr_context.cmd_area.length;
184 	message.header.event = opr_dev->opr_context.cmd_area.event_id;
185 	len = sizeof(struct ecl_message_header) + message.header.data_len;
186 
187 	memcpy(message.payload,
188 	       opr_dev->opr_context.data_area.data + message.header.offset,
189 	       message.header.data_len);
190 
191 	dev_dbg(cl_data_to_dev(opr_dev),
192 		"[ish_wr] off : %x, len : %x\n",
193 		message.header.offset,
194 		message.header.data_len);
195 
196 	return ishtp_cl_send(opr_dev->ecl_ishtp_cl, (uint8_t *)&message, len);
197 }
198 
199 static acpi_status
200 ecl_opregion_cmd_handler(u32 function, acpi_physical_address address,
201 			 u32 bits, u64 *value64,
202 			 void *handler_context, void *region_context)
203 {
204 	struct ishtp_opregion_dev *opr_dev;
205 	struct opregion_cmd *cmd;
206 	acpi_status status = AE_OK;
207 
208 	if (!region_context || !value64)
209 		return AE_BAD_PARAMETER;
210 
211 	if (function == ACPI_READ)
212 		return AE_ERROR;
213 
214 	opr_dev = (struct ishtp_opregion_dev *)region_context;
215 
216 	mutex_lock(&opr_dev->lock);
217 
218 	cmd = &opr_dev->opr_context.cmd_area;
219 
220 	switch (address) {
221 	case cmd_opr_offsetof(command):
222 		cmd->command = (u32)*value64;
223 
224 		if (cmd->command == ECL_ISH_READ)
225 			status =  ecl_ish_cl_read(opr_dev);
226 		else if (cmd->command == ECL_ISH_WRITE)
227 			status = ecl_ish_cl_write(opr_dev);
228 		else
229 			status = AE_ERROR;
230 		break;
231 	case cmd_opr_offsetof(offset):
232 		cmd->offset = (u32)*value64;
233 		break;
234 	case cmd_opr_offsetof(length):
235 		cmd->length = (u32)*value64;
236 		break;
237 	case cmd_opr_offsetof(event_id):
238 		cmd->event_id = (u32)*value64;
239 		break;
240 	default:
241 		status = AE_ERROR;
242 	}
243 
244 	mutex_unlock(&opr_dev->lock);
245 
246 	return status;
247 }
248 
249 static acpi_status
250 ecl_opregion_data_handler(u32 function, acpi_physical_address address,
251 			  u32 bits, u64 *value64,
252 			  void *handler_context, void *region_context)
253 {
254 	struct ishtp_opregion_dev *opr_dev;
255 	unsigned int bytes = BITS_TO_BYTES(bits);
256 	void *data_addr;
257 
258 	if (!region_context || !value64)
259 		return AE_BAD_PARAMETER;
260 
261 	if (address + bytes > ECL_DATA_OPR_BUFLEN)
262 		return AE_BAD_PARAMETER;
263 
264 	opr_dev = (struct ishtp_opregion_dev *)region_context;
265 
266 	mutex_lock(&opr_dev->lock);
267 
268 	data_addr = &opr_dev->opr_context.data_area.data[address];
269 
270 	if (function == ACPI_READ) {
271 		memcpy(value64, data_addr, bytes);
272 	} else if (function == ACPI_WRITE) {
273 		memcpy(data_addr, value64, bytes);
274 	} else {
275 		mutex_unlock(&opr_dev->lock);
276 		return AE_BAD_PARAMETER;
277 	}
278 
279 	mutex_unlock(&opr_dev->lock);
280 
281 	return AE_OK;
282 }
283 
284 static int acpi_find_eclite_device(struct ishtp_opregion_dev *opr_dev)
285 {
286 	struct acpi_device *adev;
287 
288 	/* Find ECLite device and save reference */
289 	adev = acpi_dev_get_first_match_dev("INTC1035", NULL, -1);
290 	if (!adev) {
291 		dev_err(cl_data_to_dev(opr_dev), "eclite ACPI device not found\n");
292 		return -ENODEV;
293 	}
294 
295 	opr_dev->adev = adev;
296 
297 	return 0;
298 }
299 
300 static int acpi_opregion_init(struct ishtp_opregion_dev *opr_dev)
301 {
302 	acpi_status status;
303 
304 	status = acpi_install_address_space_handler(opr_dev->adev->handle,
305 						    ECLITE_CMD_OPREGION_ID,
306 						    ecl_opregion_cmd_handler,
307 						    NULL, opr_dev);
308 	if (ACPI_FAILURE(status)) {
309 		dev_err(cl_data_to_dev(opr_dev),
310 			"cmd space handler install failed\n");
311 		return -ENODEV;
312 	}
313 
314 	status = acpi_install_address_space_handler(opr_dev->adev->handle,
315 						    ECLITE_DATA_OPREGION_ID,
316 						    ecl_opregion_data_handler,
317 						    NULL, opr_dev);
318 	if (ACPI_FAILURE(status)) {
319 		dev_err(cl_data_to_dev(opr_dev),
320 			"data space handler install failed\n");
321 
322 		acpi_remove_address_space_handler(opr_dev->adev->handle,
323 						  ECLITE_CMD_OPREGION_ID,
324 						  ecl_opregion_cmd_handler);
325 		return -ENODEV;
326 	}
327 	opr_dev->acpi_init_done = true;
328 
329 	dev_dbg(cl_data_to_dev(opr_dev), "Opregion handlers are installed\n");
330 
331 	return 0;
332 }
333 
334 static void acpi_opregion_deinit(struct ishtp_opregion_dev *opr_dev)
335 {
336 	acpi_remove_address_space_handler(opr_dev->adev->handle,
337 					  ECLITE_CMD_OPREGION_ID,
338 					  ecl_opregion_cmd_handler);
339 
340 	acpi_remove_address_space_handler(opr_dev->adev->handle,
341 					  ECLITE_DATA_OPREGION_ID,
342 					  ecl_opregion_data_handler);
343 	opr_dev->acpi_init_done = false;
344 }
345 
346 static void ecl_acpi_invoke_dsm(struct work_struct *work)
347 {
348 	struct ishtp_opregion_dev *opr_dev;
349 	union acpi_object *obj;
350 
351 	opr_dev = container_of(work, struct ishtp_opregion_dev, event_work);
352 	if (!opr_dev->acpi_init_done)
353 		return;
354 
355 	obj = acpi_evaluate_dsm(opr_dev->adev->handle, &ecl_acpi_guid, 0,
356 				opr_dev->dsm_event_id, NULL);
357 	if (!obj) {
358 		dev_warn(cl_data_to_dev(opr_dev), "_DSM fn call failed\n");
359 		return;
360 	}
361 
362 	dev_dbg(cl_data_to_dev(opr_dev), "Exec DSM function code: %d success\n",
363 		opr_dev->dsm_event_id);
364 
365 	ACPI_FREE(obj);
366 }
367 
368 static void ecl_ish_process_rx_data(struct ishtp_opregion_dev *opr_dev)
369 {
370 	struct ecl_message *message =
371 		(struct ecl_message *)opr_dev->rb->buffer.data;
372 
373 	dev_dbg(cl_data_to_dev(opr_dev),
374 		"[ish_rd] Resp: off : %x, len : %x\n",
375 		message->header.offset,
376 		message->header.data_len);
377 
378 	if ((message->header.offset + message->header.data_len) >
379 			ECL_DATA_OPR_BUFLEN) {
380 		return;
381 	}
382 
383 	memcpy(opr_dev->opr_context.data_area.data + message->header.offset,
384 	       message->payload, message->header.data_len);
385 
386 	opr_dev->ish_read_done = true;
387 	wake_up_interruptible(&opr_dev->read_wait);
388 }
389 
390 static void ecl_ish_process_rx_event(struct ishtp_opregion_dev *opr_dev)
391 {
392 	struct ecl_message_header *header =
393 		(struct ecl_message_header *)opr_dev->rb->buffer.data;
394 
395 	dev_dbg(cl_data_to_dev(opr_dev),
396 		"[ish_ev] Evt received: %8x\n", header->event);
397 
398 	opr_dev->dsm_event_id = header->event;
399 	schedule_work(&opr_dev->event_work);
400 }
401 
402 static int ecl_ish_cl_enable_events(struct ishtp_opregion_dev *opr_dev,
403 				    bool config_enable)
404 {
405 	struct ecl_message message;
406 	int len;
407 
408 	message.header.version = ECL_ISH_HEADER_VERSION;
409 	message.header.data_type = ECL_MSG_DATA;
410 	message.header.request_type = ECL_ISH_WRITE;
411 	message.header.offset = ECL_EVENTS_NOTIFY;
412 	message.header.data_len = 1;
413 	message.payload[0] = config_enable;
414 
415 	len = sizeof(struct ecl_message_header) + message.header.data_len;
416 
417 	return ishtp_cl_send(opr_dev->ecl_ishtp_cl, (uint8_t *)&message, len);
418 }
419 
420 static void ecl_ishtp_cl_event_cb(struct ishtp_cl_device *cl_device)
421 {
422 	struct ishtp_cl *ecl_ishtp_cl = ishtp_get_drvdata(cl_device);
423 	struct ishtp_opregion_dev *opr_dev;
424 	struct ecl_message_header *header;
425 	struct ishtp_cl_rb *rb;
426 
427 	opr_dev = ishtp_get_client_data(ecl_ishtp_cl);
428 	while ((rb = ishtp_cl_rx_get_rb(opr_dev->ecl_ishtp_cl)) != NULL) {
429 		opr_dev->rb = rb;
430 		header = (struct ecl_message_header *)rb->buffer.data;
431 
432 		if (header->data_type == ECL_MSG_DATA)
433 			ecl_ish_process_rx_data(opr_dev);
434 		else if (header->data_type == ECL_MSG_EVENT)
435 			ecl_ish_process_rx_event(opr_dev);
436 		else
437 			/* Got an event with wrong data_type, ignore it */
438 			dev_err(cl_data_to_dev(opr_dev),
439 				"[ish_cb] Received wrong data_type\n");
440 
441 		ishtp_cl_io_rb_recycle(rb);
442 	}
443 }
444 
445 static int ecl_ishtp_cl_init(struct ishtp_cl *ecl_ishtp_cl)
446 {
447 	struct ishtp_opregion_dev *opr_dev =
448 		ishtp_get_client_data(ecl_ishtp_cl);
449 	struct ishtp_fw_client *fw_client;
450 	struct ishtp_device *dev;
451 	int rv;
452 
453 	rv = ishtp_cl_link(ecl_ishtp_cl);
454 	if (rv) {
455 		dev_err(cl_data_to_dev(opr_dev), "ishtp_cl_link failed\n");
456 		return	rv;
457 	}
458 
459 	dev = ishtp_get_ishtp_device(ecl_ishtp_cl);
460 
461 	/* Connect to FW client */
462 	ishtp_set_tx_ring_size(ecl_ishtp_cl, ECL_CL_TX_RING_SIZE);
463 	ishtp_set_rx_ring_size(ecl_ishtp_cl, ECL_CL_RX_RING_SIZE);
464 
465 	fw_client = ishtp_fw_cl_get_client(dev, &ecl_ishtp_guid);
466 	if (!fw_client) {
467 		dev_err(cl_data_to_dev(opr_dev), "fw client not found\n");
468 		return -ENOENT;
469 	}
470 
471 	ishtp_cl_set_fw_client_id(ecl_ishtp_cl,
472 				  ishtp_get_fw_client_id(fw_client));
473 
474 	ishtp_set_connection_state(ecl_ishtp_cl, ISHTP_CL_CONNECTING);
475 
476 	rv = ishtp_cl_connect(ecl_ishtp_cl);
477 	if (rv) {
478 		dev_err(cl_data_to_dev(opr_dev), "client connect failed\n");
479 
480 		ishtp_cl_unlink(ecl_ishtp_cl);
481 		return rv;
482 	}
483 
484 	dev_dbg(cl_data_to_dev(opr_dev), "Host connected to fw client\n");
485 
486 	return 0;
487 }
488 
489 static void ecl_ishtp_cl_deinit(struct ishtp_cl *ecl_ishtp_cl)
490 {
491 	ishtp_cl_unlink(ecl_ishtp_cl);
492 	ishtp_cl_flush_queues(ecl_ishtp_cl);
493 	ishtp_cl_free(ecl_ishtp_cl);
494 }
495 
496 static void ecl_ishtp_cl_reset_handler(struct work_struct *work)
497 {
498 	struct ishtp_opregion_dev *opr_dev;
499 	struct ishtp_cl_device *cl_device;
500 	struct ishtp_cl *ecl_ishtp_cl;
501 	int rv;
502 	int retry;
503 
504 	opr_dev = container_of(work, struct ishtp_opregion_dev, reset_work);
505 
506 	opr_dev->ish_link_ready = false;
507 
508 	cl_device = opr_dev->cl_device;
509 	ecl_ishtp_cl = opr_dev->ecl_ishtp_cl;
510 
511 	ecl_ishtp_cl_deinit(ecl_ishtp_cl);
512 
513 	ecl_ishtp_cl = ishtp_cl_allocate(cl_device);
514 	if (!ecl_ishtp_cl)
515 		return;
516 
517 	ishtp_set_drvdata(cl_device, ecl_ishtp_cl);
518 	ishtp_set_client_data(ecl_ishtp_cl, opr_dev);
519 
520 	opr_dev->ecl_ishtp_cl = ecl_ishtp_cl;
521 
522 	for (retry = 0; retry < 3; ++retry) {
523 		rv = ecl_ishtp_cl_init(ecl_ishtp_cl);
524 		if (!rv)
525 			break;
526 	}
527 	if (rv) {
528 		ishtp_cl_free(ecl_ishtp_cl);
529 		opr_dev->ecl_ishtp_cl = NULL;
530 		dev_err(cl_data_to_dev(opr_dev),
531 			"[ish_rst] Reset failed. Link not ready.\n");
532 		return;
533 	}
534 
535 	ishtp_register_event_cb(cl_device, ecl_ishtp_cl_event_cb);
536 	dev_info(cl_data_to_dev(opr_dev),
537 		 "[ish_rst] Reset Success. Link ready.\n");
538 
539 	opr_dev->ish_link_ready = true;
540 
541 	if (opr_dev->acpi_init_done)
542 		return;
543 
544 	rv = acpi_opregion_init(opr_dev);
545 	if (rv) {
546 		dev_err(cl_data_to_dev(opr_dev),
547 			"ACPI opregion init failed\n");
548 	}
549 }
550 
551 static int ecl_ishtp_cl_probe(struct ishtp_cl_device *cl_device)
552 {
553 	struct ishtp_cl *ecl_ishtp_cl;
554 	struct ishtp_opregion_dev *opr_dev;
555 	int rv;
556 
557 	opr_dev = devm_kzalloc(ishtp_device(cl_device), sizeof(*opr_dev),
558 			       GFP_KERNEL);
559 	if (!opr_dev)
560 		return -ENOMEM;
561 
562 	ecl_ishtp_cl = ishtp_cl_allocate(cl_device);
563 	if (!ecl_ishtp_cl)
564 		return -ENOMEM;
565 
566 	ishtp_set_drvdata(cl_device, ecl_ishtp_cl);
567 	ishtp_set_client_data(ecl_ishtp_cl, opr_dev);
568 	opr_dev->ecl_ishtp_cl = ecl_ishtp_cl;
569 	opr_dev->cl_device = cl_device;
570 
571 	init_waitqueue_head(&opr_dev->read_wait);
572 	INIT_WORK(&opr_dev->event_work, ecl_acpi_invoke_dsm);
573 	INIT_WORK(&opr_dev->reset_work, ecl_ishtp_cl_reset_handler);
574 
575 	/* Initialize ish client device */
576 	rv = ecl_ishtp_cl_init(ecl_ishtp_cl);
577 	if (rv) {
578 		dev_err(cl_data_to_dev(opr_dev), "Client init failed\n");
579 		goto err_exit;
580 	}
581 
582 	dev_dbg(cl_data_to_dev(opr_dev), "eclite-ishtp client initialised\n");
583 
584 	opr_dev->ish_link_ready = true;
585 	mutex_init(&opr_dev->lock);
586 
587 	rv = acpi_find_eclite_device(opr_dev);
588 	if (rv) {
589 		dev_err(cl_data_to_dev(opr_dev), "ECLite ACPI ID not found\n");
590 		goto err_exit;
591 	}
592 
593 	/* Register a handler for eclite fw events */
594 	ishtp_register_event_cb(cl_device, ecl_ishtp_cl_event_cb);
595 
596 	/* Now init opregion handlers */
597 	rv = acpi_opregion_init(opr_dev);
598 	if (rv) {
599 		dev_err(cl_data_to_dev(opr_dev), "ACPI opregion init failed\n");
600 		goto err_exit;
601 	}
602 
603 	/* Reprobe devices depending on ECLite - battery, fan, etc. */
604 	acpi_dev_clear_dependencies(opr_dev->adev);
605 
606 	return 0;
607 err_exit:
608 	ishtp_set_connection_state(ecl_ishtp_cl, ISHTP_CL_DISCONNECTING);
609 	ishtp_cl_disconnect(ecl_ishtp_cl);
610 	ecl_ishtp_cl_deinit(ecl_ishtp_cl);
611 
612 	return rv;
613 }
614 
615 static void ecl_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
616 {
617 	struct ishtp_cl *ecl_ishtp_cl = ishtp_get_drvdata(cl_device);
618 	struct ishtp_opregion_dev *opr_dev =
619 		ishtp_get_client_data(ecl_ishtp_cl);
620 
621 	if (opr_dev->acpi_init_done)
622 		acpi_opregion_deinit(opr_dev);
623 
624 	acpi_dev_put(opr_dev->adev);
625 
626 	ishtp_set_connection_state(ecl_ishtp_cl, ISHTP_CL_DISCONNECTING);
627 	ishtp_cl_disconnect(ecl_ishtp_cl);
628 	ecl_ishtp_cl_deinit(ecl_ishtp_cl);
629 
630 	cancel_work_sync(&opr_dev->reset_work);
631 	cancel_work_sync(&opr_dev->event_work);
632 }
633 
634 static int ecl_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
635 {
636 	struct ishtp_cl *ecl_ishtp_cl = ishtp_get_drvdata(cl_device);
637 	struct ishtp_opregion_dev *opr_dev =
638 		ishtp_get_client_data(ecl_ishtp_cl);
639 
640 	schedule_work(&opr_dev->reset_work);
641 
642 	return 0;
643 }
644 
645 static int ecl_ishtp_cl_suspend(struct device *device)
646 {
647 	struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
648 	struct ishtp_cl *ecl_ishtp_cl = ishtp_get_drvdata(cl_device);
649 	struct ishtp_opregion_dev *opr_dev =
650 		ishtp_get_client_data(ecl_ishtp_cl);
651 
652 	if (acpi_target_system_state() == ACPI_STATE_S0)
653 		return 0;
654 
655 	acpi_opregion_deinit(opr_dev);
656 	ecl_ish_cl_enable_events(opr_dev, false);
657 
658 	return 0;
659 }
660 
661 static int ecl_ishtp_cl_resume(struct device *device)
662 {
663 	/* A reset is expected to call after an Sx. At this point
664 	 * we are not sure if the link is up or not to restore anything,
665 	 * so do nothing in resume path
666 	 */
667 	return 0;
668 }
669 
670 static const struct dev_pm_ops ecl_ishtp_pm_ops = {
671 	.suspend = ecl_ishtp_cl_suspend,
672 	.resume = ecl_ishtp_cl_resume,
673 };
674 
675 static struct ishtp_cl_driver ecl_ishtp_cl_driver = {
676 	.name = "ishtp-eclite",
677 	.guid = &ecl_ishtp_guid,
678 	.probe = ecl_ishtp_cl_probe,
679 	.remove = ecl_ishtp_cl_remove,
680 	.reset = ecl_ishtp_cl_reset,
681 	.driver.pm = &ecl_ishtp_pm_ops,
682 };
683 
684 static int __init ecl_ishtp_init(void)
685 {
686 	return ishtp_cl_driver_register(&ecl_ishtp_cl_driver, THIS_MODULE);
687 }
688 
689 static void __exit ecl_ishtp_exit(void)
690 {
691 	return ishtp_cl_driver_unregister(&ecl_ishtp_cl_driver);
692 }
693 
694 late_initcall(ecl_ishtp_init);
695 module_exit(ecl_ishtp_exit);
696 
697 MODULE_DESCRIPTION("ISH ISHTP eclite client opregion driver");
698 MODULE_AUTHOR("K Naduvalath, Sumesh <sumesh.k.naduvalath@intel.com>");
699 
700 MODULE_LICENSE("GPL v2");
701 MODULE_ALIAS("ishtp:*");
702