// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2023 MediaTek Inc.
 */

#define MTK_RNG_DEV KBUILD_MODNAME

#include <linux/delay.h>
#include <linux/err.h>
#include <linux/hw_random.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>

#include "tee_client_api.h"

#define TA_UUID	"6d81405fd46e496f816218c2e774e1bf"

#define RNG_SERVICE_UUID \
		{ 0x6d81405f, 0xd46e, 0x496f,  \
		{ 0x81, 0x62, 0x18, 0xc2, 0xe7, 0x74, 0xe1, 0xbf } }

#define PTA_MTK_RNG_CMD_RD		0

#define to_mtk_rng(p)	container_of(p, struct mtk_rng, rng)

struct mtk_rng {
	struct hwrng rng;
};

static int mtk_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
{
	struct mtk_rng *priv = to_mtk_rng(rng);
	struct TEEC_Operation op;
	struct TEEC_Session sess;
	struct TEEC_Context ctx;
	unsigned int retval = 0;
	int err_origin;
	TEEC_Result res;
	struct device *dev = (struct device *)priv->rng.priv;
	struct TEEC_UUID uuid = (struct TEEC_UUID)RNG_SERVICE_UUID;

	/* Initialize a context connecting us to the TEE. */
	res = TEEC_InitializeContext(TA_UUID, &ctx);
	if (res != TEEC_SUCCESS) {
		dev_err(dev, "TEEC_InitializeContext failed(0x%x)\n", res);
		return res;
	}

	/* Open a session to the "pta_rng.ta" TA. */
	res = TEEC_OpenSession(&ctx, &sess, &uuid,
				   TEEC_LOGIN_PUBLIC, NULL, NULL, &err_origin);
	if (res != TEEC_SUCCESS) {
		dev_err(dev, "TEEC_Opensession failed(0x%x 0x%x)\n",
			res, err_origin);
		goto final_session;
	}

	 /* Execute a function in the TA by invoking it. */
	memset(&op, 0, sizeof(op));

	/* Prepare the argument. */
	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
					 TEEC_NONE, TEEC_NONE);

	while (max >= sizeof(u32)) {

		res = TEEC_InvokeCommand(&sess, PTA_MTK_RNG_CMD_RD, &op,
					 &err_origin);
		if (res != TEEC_SUCCESS) {
			dev_err(dev, "TEEC_InvokeCommand failed(0x%x 0x%x)\n",
				res, err_origin);
			goto close_session;
		}

		*(u32 *)buf = op.params[0].value.a;
		retval += sizeof(u32);
		buf += sizeof(u32);
		max -= sizeof(u32);
	}

close_session:
	TEEC_CloseSession(&sess);
final_session:
	TEEC_FinalizeContext(&ctx);

	return retval || !wait ? retval : -EIO;
}

static int mtk_rng_probe(struct platform_device *pdev)
{
	struct mtk_rng *priv;
	int ret;

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->rng.name = pdev->name;
	priv->rng.read = mtk_rng_read;
	priv->rng.priv = (unsigned long)&pdev->dev;
	priv->rng.quality = 900;

	ret = devm_hwrng_register(&pdev->dev, &priv->rng);
	if (ret) {
		dev_err(&pdev->dev, "failed to register rng device: %d\n",
			ret);
		return ret;
	}

	dev_info(&pdev->dev, "registered RNG driver\n");

	return 0;
}

static const struct of_device_id mtk_rng_match[] = {
	{ .compatible = "mediatek,mt8696-tee-rng" },
	{},
};
MODULE_DEVICE_TABLE(of, mtk_rng_match);

static struct platform_driver mtk_rng_driver = {
	.probe          = mtk_rng_probe,
	.driver = {
		.name = MTK_RNG_DEV,
		.of_match_table = mtk_rng_match,
	},
};

static int __init mtk_rng_init(void)
{
	return platform_driver_register(&mtk_rng_driver);
}
late_initcall(mtk_rng_init);
MODULE_DESCRIPTION("Mediatek Random Number Generator Driver");
MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
MODULE_LICENSE("GPL");
