forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/platform/chrome/cros_ec_spi.c
....@@ -1,28 +1,20 @@
1
-/*
2
- * ChromeOS EC multi-function device (SPI)
3
- *
4
- * Copyright (C) 2012 Google, Inc
5
- *
6
- * This software is licensed under the terms of the GNU General Public
7
- * License version 2, as published by the Free Software Foundation, and
8
- * may be copied, distributed, and modified under those terms.
9
- *
10
- * This program is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- * GNU General Public License for more details.
14
- */
1
+// SPDX-License-Identifier: GPL-2.0
2
+// SPI interface for ChromeOS Embedded Controller
3
+//
4
+// Copyright (C) 2012 Google, Inc
155
166 #include <linux/delay.h>
177 #include <linux/kernel.h>
188 #include <linux/module.h>
19
-#include <linux/mfd/cros_ec.h>
20
-#include <linux/mfd/cros_ec_commands.h>
219 #include <linux/of.h>
10
+#include <linux/platform_data/cros_ec_commands.h>
11
+#include <linux/platform_data/cros_ec_proto.h>
2212 #include <linux/platform_device.h>
2313 #include <linux/slab.h>
2414 #include <linux/spi/spi.h>
15
+#include <uapi/linux/sched/types.h>
2516
17
+#include "cros_ec.h"
2618
2719 /* The header byte, which follows the preamble */
2820 #define EC_MSG_HEADER 0xec
....@@ -77,12 +69,35 @@
7769 * is sent when we want to turn on CS at the start of a transaction.
7870 * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
7971 * is sent when we want to turn off CS at the end of a transaction.
72
+ * @high_pri_worker: Used to schedule high priority work.
8073 */
8174 struct cros_ec_spi {
8275 struct spi_device *spi;
8376 s64 last_transfer_ns;
8477 unsigned int start_of_msg_delay;
8578 unsigned int end_of_msg_delay;
79
+ struct kthread_worker *high_pri_worker;
80
+};
81
+
82
+typedef int (*cros_ec_xfer_fn_t) (struct cros_ec_device *ec_dev,
83
+ struct cros_ec_command *ec_msg);
84
+
85
+/**
86
+ * struct cros_ec_xfer_work_params - params for our high priority workers
87
+ *
88
+ * @work: The work_struct needed to queue work
89
+ * @fn: The function to use to transfer
90
+ * @ec_dev: ChromeOS EC device
91
+ * @ec_msg: Message to transfer
92
+ * @ret: The return value of the function
93
+ */
94
+
95
+struct cros_ec_xfer_work_params {
96
+ struct kthread_work work;
97
+ cros_ec_xfer_fn_t fn;
98
+ struct cros_ec_device *ec_dev;
99
+ struct cros_ec_command *ec_msg;
100
+ int ret;
86101 };
87102
88103 static void debug_packet(struct device *dev, const char *name, u8 *ptr,
....@@ -112,7 +127,8 @@
112127 */
113128 spi_message_init(&msg);
114129 memset(&trans, 0, sizeof(trans));
115
- trans.delay_usecs = ec_spi->end_of_msg_delay;
130
+ trans.delay.value = ec_spi->end_of_msg_delay;
131
+ trans.delay.unit = SPI_DELAY_UNIT_USECS;
116132 spi_message_add_tail(&trans, &msg);
117133
118134 ret = spi_sync_locked(ec_spi->spi, &msg);
....@@ -132,6 +148,10 @@
132148 * receive_n_bytes - receive n bytes from the EC.
133149 *
134150 * Assumes buf is a pointer into the ec_dev->din buffer
151
+ *
152
+ * @ec_dev: ChromeOS EC device.
153
+ * @buf: Pointer to the buffer receiving the data.
154
+ * @n: Number of bytes received.
135155 */
136156 static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n)
137157 {
....@@ -360,13 +380,13 @@
360380 }
361381
362382 /**
363
- * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
383
+ * do_cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
364384 *
365385 * @ec_dev: ChromeOS EC device
366386 * @ec_msg: Message to transfer
367387 */
368
-static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
369
- struct cros_ec_command *ec_msg)
388
+static int do_cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
389
+ struct cros_ec_command *ec_msg)
370390 {
371391 struct ec_host_response *response;
372392 struct cros_ec_spi *ec_spi = ec_dev->priv;
....@@ -401,7 +421,8 @@
401421 spi_message_init(&msg);
402422 if (ec_spi->start_of_msg_delay) {
403423 memset(&trans_delay, 0, sizeof(trans_delay));
404
- trans_delay.delay_usecs = ec_spi->start_of_msg_delay;
424
+ trans_delay.delay.value = ec_spi->start_of_msg_delay;
425
+ trans_delay.delay.unit = SPI_DELAY_UNIT_USECS;
405426 spi_message_add_tail(&trans_delay, &msg);
406427 }
407428
....@@ -503,13 +524,13 @@
503524 }
504525
505526 /**
506
- * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
527
+ * do_cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
507528 *
508529 * @ec_dev: ChromeOS EC device
509530 * @ec_msg: Message to transfer
510531 */
511
-static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
512
- struct cros_ec_command *ec_msg)
532
+static int do_cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
533
+ struct cros_ec_command *ec_msg)
513534 {
514535 struct cros_ec_spi *ec_spi = ec_dev->priv;
515536 struct spi_transfer trans;
....@@ -621,6 +642,54 @@
621642 return ret;
622643 }
623644
645
+static void cros_ec_xfer_high_pri_work(struct kthread_work *work)
646
+{
647
+ struct cros_ec_xfer_work_params *params;
648
+
649
+ params = container_of(work, struct cros_ec_xfer_work_params, work);
650
+ params->ret = params->fn(params->ec_dev, params->ec_msg);
651
+}
652
+
653
+static int cros_ec_xfer_high_pri(struct cros_ec_device *ec_dev,
654
+ struct cros_ec_command *ec_msg,
655
+ cros_ec_xfer_fn_t fn)
656
+{
657
+ struct cros_ec_spi *ec_spi = ec_dev->priv;
658
+ struct cros_ec_xfer_work_params params = {
659
+ .work = KTHREAD_WORK_INIT(params.work,
660
+ cros_ec_xfer_high_pri_work),
661
+ .ec_dev = ec_dev,
662
+ .ec_msg = ec_msg,
663
+ .fn = fn,
664
+ };
665
+
666
+ /*
667
+ * This looks a bit ridiculous. Why do the work on a
668
+ * different thread if we're just going to block waiting for
669
+ * the thread to finish? The key here is that the thread is
670
+ * running at high priority but the calling context might not
671
+ * be. We need to be at high priority to avoid getting
672
+ * context switched out for too long and the EC giving up on
673
+ * the transfer.
674
+ */
675
+ kthread_queue_work(ec_spi->high_pri_worker, &params.work);
676
+ kthread_flush_work(&params.work);
677
+
678
+ return params.ret;
679
+}
680
+
681
+static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
682
+ struct cros_ec_command *ec_msg)
683
+{
684
+ return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_pkt_xfer_spi);
685
+}
686
+
687
+static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
688
+ struct cros_ec_command *ec_msg)
689
+{
690
+ return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_cmd_xfer_spi);
691
+}
692
+
624693 static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
625694 {
626695 struct device_node *np = dev->of_node;
....@@ -636,6 +705,35 @@
636705 ec_spi->end_of_msg_delay = val;
637706 }
638707
708
+static void cros_ec_spi_high_pri_release(void *worker)
709
+{
710
+ kthread_destroy_worker(worker);
711
+}
712
+
713
+static int cros_ec_spi_devm_high_pri_alloc(struct device *dev,
714
+ struct cros_ec_spi *ec_spi)
715
+{
716
+ int err;
717
+
718
+ ec_spi->high_pri_worker =
719
+ kthread_create_worker(0, "cros_ec_spi_high_pri");
720
+
721
+ if (IS_ERR(ec_spi->high_pri_worker)) {
722
+ err = PTR_ERR(ec_spi->high_pri_worker);
723
+ dev_err(dev, "Can't create cros_ec high pri worker: %d\n", err);
724
+ return err;
725
+ }
726
+
727
+ err = devm_add_action_or_reset(dev, cros_ec_spi_high_pri_release,
728
+ ec_spi->high_pri_worker);
729
+ if (err)
730
+ return err;
731
+
732
+ sched_set_fifo(ec_spi->high_pri_worker->task);
733
+
734
+ return 0;
735
+}
736
+
639737 static int cros_ec_spi_probe(struct spi_device *spi)
640738 {
641739 struct device *dev = &spi->dev;
....@@ -644,7 +742,7 @@
644742 int err;
645743
646744 spi->bits_per_word = 8;
647
- spi->mode = SPI_MODE_0;
745
+ spi->rt = true;
648746 err = spi_setup(spi);
649747 if (err < 0)
650748 return err;
....@@ -674,6 +772,10 @@
674772
675773 ec_spi->last_transfer_ns = ktime_get_ns();
676774
775
+ err = cros_ec_spi_devm_high_pri_alloc(dev, ec_spi);
776
+ if (err)
777
+ return err;
778
+
677779 err = cros_ec_register(ec_dev);
678780 if (err) {
679781 dev_err(dev, "cannot register EC\n");
....@@ -687,12 +789,9 @@
687789
688790 static int cros_ec_spi_remove(struct spi_device *spi)
689791 {
690
- struct cros_ec_device *ec_dev;
792
+ struct cros_ec_device *ec_dev = spi_get_drvdata(spi);
691793
692
- ec_dev = spi_get_drvdata(spi);
693
- cros_ec_remove(ec_dev);
694
-
695
- return 0;
794
+ return cros_ec_unregister(ec_dev);
696795 }
697796
698797 #ifdef CONFIG_PM_SLEEP
....@@ -729,7 +828,7 @@
729828 static struct spi_driver cros_ec_driver_spi = {
730829 .driver = {
731830 .name = "cros-ec-spi",
732
- .of_match_table = of_match_ptr(cros_ec_spi_of_match),
831
+ .of_match_table = cros_ec_spi_of_match,
733832 .pm = &cros_ec_spi_pm_ops,
734833 },
735834 .probe = cros_ec_spi_probe,
....@@ -740,4 +839,4 @@
740839 module_spi_driver(cros_ec_driver_spi);
741840
742841 MODULE_LICENSE("GPL v2");
743
-MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");
842
+MODULE_DESCRIPTION("SPI interface for ChromeOS Embedded Controller");