summaryrefslogtreecommitdiffstats
path: root/drivers/media/platform/amphion/vpu_mbox.c
blob: b6d5b4844f67291a2932e6fb26d786ae4d832d76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright 2020-2021 NXP
 */

#include <linux/init.h>
#include <linux/interconnect.h>
#include <linux/ioctl.h>
#include <linux/list.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/platform_device.h>
#include "vpu.h"
#include "vpu_mbox.h"
#include "vpu_msgs.h"

static void vpu_mbox_rx_callback(struct mbox_client *cl, void *msg)
{
	struct vpu_mbox *rx = container_of(cl, struct vpu_mbox, cl);
	struct vpu_core *core = container_of(rx, struct vpu_core, rx);

	vpu_isr(core, *(u32 *)msg);
}

static int vpu_mbox_request_channel(struct device *dev, struct vpu_mbox *mbox)
{
	struct mbox_chan *ch;
	struct mbox_client *cl;

	if (!dev || !mbox)
		return -EINVAL;
	if (mbox->ch)
		return 0;

	cl = &mbox->cl;
	cl->dev = dev;
	if (mbox->block) {
		cl->tx_block = true;
		cl->tx_tout = 1000;
	} else {
		cl->tx_block = false;
	}
	cl->knows_txdone = false;
	cl->rx_callback = vpu_mbox_rx_callback;

	ch = mbox_request_channel_byname(cl, mbox->name);
	if (IS_ERR(ch))
		return dev_err_probe(dev, PTR_ERR(ch),
				     "Failed to request mbox chan %s\n",
				     mbox->name);

	mbox->ch = ch;
	return 0;
}

int vpu_mbox_init(struct vpu_core *core)
{
	scnprintf(core->tx_type.name, sizeof(core->tx_type.name) - 1, "tx0");
	core->tx_type.block = true;

	scnprintf(core->tx_data.name, sizeof(core->tx_data.name) - 1, "tx1");
	core->tx_data.block = false;

	scnprintf(core->rx.name, sizeof(core->rx.name) - 1, "rx");
	core->rx.block = true;

	return 0;
}

int vpu_mbox_request(struct vpu_core *core)
{
	int ret;

	ret = vpu_mbox_request_channel(core->dev, &core->tx_type);
	if (ret)
		goto error;
	ret = vpu_mbox_request_channel(core->dev, &core->tx_data);
	if (ret)
		goto error;
	ret = vpu_mbox_request_channel(core->dev, &core->rx);
	if (ret)
		goto error;

	dev_dbg(core->dev, "%s request mbox\n", vpu_core_type_desc(core->type));
	return 0;
error:
	vpu_mbox_free(core);
	return ret;
}

void vpu_mbox_free(struct vpu_core *core)
{
	mbox_free_channel(core->tx_type.ch);
	mbox_free_channel(core->tx_data.ch);
	mbox_free_channel(core->rx.ch);
	core->tx_type.ch = NULL;
	core->tx_data.ch = NULL;
	core->rx.ch = NULL;
	dev_dbg(core->dev, "%s free mbox\n", vpu_core_type_desc(core->type));
}

void vpu_mbox_send_type(struct vpu_core *core, u32 type)
{
	mbox_send_message(core->tx_type.ch, &type);
}

void vpu_mbox_send_msg(struct vpu_core *core, u32 type, u32 data)
{
	mbox_send_message(core->tx_data.ch, &data);
	mbox_send_message(core->tx_type.ch, &type);
}

void vpu_mbox_enable_rx(struct vpu_dev *dev)
{
}