18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Qualcomm Wireless Connectivity Subsystem Iris driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 Linaro Ltd
68c2ecf20Sopenharmony_ci * Copyright (C) 2014 Sony Mobile Communications AB
78c2ecf20Sopenharmony_ci * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/clk.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/of_device.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "qcom_wcnss.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistruct qcom_iris {
208c2ecf20Sopenharmony_ci	struct device *dev;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	struct clk *xo_clk;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	struct regulator_bulk_data *vregs;
258c2ecf20Sopenharmony_ci	size_t num_vregs;
268c2ecf20Sopenharmony_ci};
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct iris_data {
298c2ecf20Sopenharmony_ci	const struct wcnss_vreg_info *vregs;
308c2ecf20Sopenharmony_ci	size_t num_vregs;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	bool use_48mhz_xo;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic const struct iris_data wcn3620_data = {
368c2ecf20Sopenharmony_ci	.vregs = (struct wcnss_vreg_info[]) {
378c2ecf20Sopenharmony_ci		{ "vddxo",  1800000, 1800000, 10000 },
388c2ecf20Sopenharmony_ci		{ "vddrfa", 1300000, 1300000, 100000 },
398c2ecf20Sopenharmony_ci		{ "vddpa",  3300000, 3300000, 515000 },
408c2ecf20Sopenharmony_ci		{ "vdddig", 1800000, 1800000, 10000 },
418c2ecf20Sopenharmony_ci	},
428c2ecf20Sopenharmony_ci	.num_vregs = 4,
438c2ecf20Sopenharmony_ci	.use_48mhz_xo = false,
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const struct iris_data wcn3660_data = {
478c2ecf20Sopenharmony_ci	.vregs = (struct wcnss_vreg_info[]) {
488c2ecf20Sopenharmony_ci		{ "vddxo",  1800000, 1800000, 10000 },
498c2ecf20Sopenharmony_ci		{ "vddrfa", 1300000, 1300000, 100000 },
508c2ecf20Sopenharmony_ci		{ "vddpa",  2900000, 3000000, 515000 },
518c2ecf20Sopenharmony_ci		{ "vdddig", 1200000, 1225000, 10000 },
528c2ecf20Sopenharmony_ci	},
538c2ecf20Sopenharmony_ci	.num_vregs = 4,
548c2ecf20Sopenharmony_ci	.use_48mhz_xo = true,
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic const struct iris_data wcn3680_data = {
588c2ecf20Sopenharmony_ci	.vregs = (struct wcnss_vreg_info[]) {
598c2ecf20Sopenharmony_ci		{ "vddxo",  1800000, 1800000, 10000 },
608c2ecf20Sopenharmony_ci		{ "vddrfa", 1300000, 1300000, 100000 },
618c2ecf20Sopenharmony_ci		{ "vddpa",  3300000, 3300000, 515000 },
628c2ecf20Sopenharmony_ci		{ "vdddig", 1800000, 1800000, 10000 },
638c2ecf20Sopenharmony_ci	},
648c2ecf20Sopenharmony_ci	.num_vregs = 4,
658c2ecf20Sopenharmony_ci	.use_48mhz_xo = true,
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciint qcom_iris_enable(struct qcom_iris *iris)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	int ret;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	ret = regulator_bulk_enable(iris->num_vregs, iris->vregs);
738c2ecf20Sopenharmony_ci	if (ret)
748c2ecf20Sopenharmony_ci		return ret;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(iris->xo_clk);
778c2ecf20Sopenharmony_ci	if (ret) {
788c2ecf20Sopenharmony_ci		dev_err(iris->dev, "failed to enable xo clk\n");
798c2ecf20Sopenharmony_ci		goto disable_regulators;
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	return 0;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cidisable_regulators:
858c2ecf20Sopenharmony_ci	regulator_bulk_disable(iris->num_vregs, iris->vregs);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return ret;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_civoid qcom_iris_disable(struct qcom_iris *iris)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	clk_disable_unprepare(iris->xo_clk);
938c2ecf20Sopenharmony_ci	regulator_bulk_disable(iris->num_vregs, iris->vregs);
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic int qcom_iris_probe(struct platform_device *pdev)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	const struct iris_data *data;
998c2ecf20Sopenharmony_ci	struct qcom_wcnss *wcnss;
1008c2ecf20Sopenharmony_ci	struct qcom_iris *iris;
1018c2ecf20Sopenharmony_ci	int ret;
1028c2ecf20Sopenharmony_ci	int i;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	iris = devm_kzalloc(&pdev->dev, sizeof(struct qcom_iris), GFP_KERNEL);
1058c2ecf20Sopenharmony_ci	if (!iris)
1068c2ecf20Sopenharmony_ci		return -ENOMEM;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	data = of_device_get_match_data(&pdev->dev);
1098c2ecf20Sopenharmony_ci	wcnss = dev_get_drvdata(pdev->dev.parent);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	iris->xo_clk = devm_clk_get(&pdev->dev, "xo");
1128c2ecf20Sopenharmony_ci	if (IS_ERR(iris->xo_clk)) {
1138c2ecf20Sopenharmony_ci		if (PTR_ERR(iris->xo_clk) != -EPROBE_DEFER)
1148c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "failed to acquire xo clk\n");
1158c2ecf20Sopenharmony_ci		return PTR_ERR(iris->xo_clk);
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	iris->num_vregs = data->num_vregs;
1198c2ecf20Sopenharmony_ci	iris->vregs = devm_kcalloc(&pdev->dev,
1208c2ecf20Sopenharmony_ci				   iris->num_vregs,
1218c2ecf20Sopenharmony_ci				   sizeof(struct regulator_bulk_data),
1228c2ecf20Sopenharmony_ci				   GFP_KERNEL);
1238c2ecf20Sopenharmony_ci	if (!iris->vregs)
1248c2ecf20Sopenharmony_ci		return -ENOMEM;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	for (i = 0; i < iris->num_vregs; i++)
1278c2ecf20Sopenharmony_ci		iris->vregs[i].supply = data->vregs[i].name;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	ret = devm_regulator_bulk_get(&pdev->dev, iris->num_vregs, iris->vregs);
1308c2ecf20Sopenharmony_ci	if (ret) {
1318c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to get regulators\n");
1328c2ecf20Sopenharmony_ci		return ret;
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	for (i = 0; i < iris->num_vregs; i++) {
1368c2ecf20Sopenharmony_ci		if (data->vregs[i].max_voltage)
1378c2ecf20Sopenharmony_ci			regulator_set_voltage(iris->vregs[i].consumer,
1388c2ecf20Sopenharmony_ci					      data->vregs[i].min_voltage,
1398c2ecf20Sopenharmony_ci					      data->vregs[i].max_voltage);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		if (data->vregs[i].load_uA)
1428c2ecf20Sopenharmony_ci			regulator_set_load(iris->vregs[i].consumer,
1438c2ecf20Sopenharmony_ci					   data->vregs[i].load_uA);
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	qcom_wcnss_assign_iris(wcnss, iris, data->use_48mhz_xo);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	return 0;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic int qcom_iris_remove(struct platform_device *pdev)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct qcom_wcnss *wcnss = dev_get_drvdata(pdev->dev.parent);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	qcom_wcnss_assign_iris(wcnss, NULL, false);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	return 0;
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic const struct of_device_id iris_of_match[] = {
1618c2ecf20Sopenharmony_ci	{ .compatible = "qcom,wcn3620", .data = &wcn3620_data },
1628c2ecf20Sopenharmony_ci	{ .compatible = "qcom,wcn3660", .data = &wcn3660_data },
1638c2ecf20Sopenharmony_ci	{ .compatible = "qcom,wcn3680", .data = &wcn3680_data },
1648c2ecf20Sopenharmony_ci	{}
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, iris_of_match);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistruct platform_driver qcom_iris_driver = {
1698c2ecf20Sopenharmony_ci	.probe = qcom_iris_probe,
1708c2ecf20Sopenharmony_ci	.remove = qcom_iris_remove,
1718c2ecf20Sopenharmony_ci	.driver = {
1728c2ecf20Sopenharmony_ci		.name = "qcom-iris",
1738c2ecf20Sopenharmony_ci		.of_match_table = iris_of_match,
1748c2ecf20Sopenharmony_ci	},
1758c2ecf20Sopenharmony_ci};
176