18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015 - 2016 Samsung Electronics Co., Ltd.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors: Inha Song <ideal.song@samsung.com>
68c2ecf20Sopenharmony_ci *          Sylwester Nawrocki <s.nawrocki@samsung.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Samsung Exynos SoC series Low Power Audio Subsystem driver.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This module provides regmap for the Top SFR region and instantiates
118c2ecf20Sopenharmony_ci * devices for IP blocks like DMAC, I2S, UART.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/clk.h>
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/io.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
198c2ecf20Sopenharmony_ci#include <linux/of.h>
208c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
218c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
228c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
238c2ecf20Sopenharmony_ci#include <linux/regmap.h>
248c2ecf20Sopenharmony_ci#include <linux/soc/samsung/exynos-regs-pmu.h>
258c2ecf20Sopenharmony_ci#include <linux/types.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* LPASS Top register definitions */
288c2ecf20Sopenharmony_ci#define SFR_LPASS_CORE_SW_RESET		0x08
298c2ecf20Sopenharmony_ci#define  LPASS_SB_SW_RESET		BIT(11)
308c2ecf20Sopenharmony_ci#define  LPASS_UART_SW_RESET		BIT(10)
318c2ecf20Sopenharmony_ci#define  LPASS_PCM_SW_RESET		BIT(9)
328c2ecf20Sopenharmony_ci#define  LPASS_I2S_SW_RESET		BIT(8)
338c2ecf20Sopenharmony_ci#define  LPASS_WDT1_SW_RESET		BIT(4)
348c2ecf20Sopenharmony_ci#define  LPASS_WDT0_SW_RESET		BIT(3)
358c2ecf20Sopenharmony_ci#define  LPASS_TIMER_SW_RESET		BIT(2)
368c2ecf20Sopenharmony_ci#define  LPASS_MEM_SW_RESET		BIT(1)
378c2ecf20Sopenharmony_ci#define  LPASS_DMA_SW_RESET		BIT(0)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define SFR_LPASS_INTR_CA5_MASK		0x48
408c2ecf20Sopenharmony_ci#define SFR_LPASS_INTR_CPU_MASK		0x58
418c2ecf20Sopenharmony_ci#define  LPASS_INTR_APM			BIT(9)
428c2ecf20Sopenharmony_ci#define  LPASS_INTR_MIF			BIT(8)
438c2ecf20Sopenharmony_ci#define  LPASS_INTR_TIMER		BIT(7)
448c2ecf20Sopenharmony_ci#define  LPASS_INTR_DMA			BIT(6)
458c2ecf20Sopenharmony_ci#define  LPASS_INTR_GPIO		BIT(5)
468c2ecf20Sopenharmony_ci#define  LPASS_INTR_I2S			BIT(4)
478c2ecf20Sopenharmony_ci#define  LPASS_INTR_PCM			BIT(3)
488c2ecf20Sopenharmony_ci#define  LPASS_INTR_SLIMBUS		BIT(2)
498c2ecf20Sopenharmony_ci#define  LPASS_INTR_UART		BIT(1)
508c2ecf20Sopenharmony_ci#define  LPASS_INTR_SFR			BIT(0)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct exynos_lpass {
538c2ecf20Sopenharmony_ci	/* pointer to the LPASS TOP regmap */
548c2ecf20Sopenharmony_ci	struct regmap *top;
558c2ecf20Sopenharmony_ci	struct clk *sfr0_clk;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic void exynos_lpass_core_sw_reset(struct exynos_lpass *lpass, int mask)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	unsigned int val = 0;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	regmap_read(lpass->top, SFR_LPASS_CORE_SW_RESET, &val);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	val &= ~mask;
658c2ecf20Sopenharmony_ci	regmap_write(lpass->top, SFR_LPASS_CORE_SW_RESET, val);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	usleep_range(100, 150);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	val |= mask;
708c2ecf20Sopenharmony_ci	regmap_write(lpass->top, SFR_LPASS_CORE_SW_RESET, val);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void exynos_lpass_enable(struct exynos_lpass *lpass)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	clk_prepare_enable(lpass->sfr0_clk);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* Unmask SFR, DMA and I2S interrupt */
788c2ecf20Sopenharmony_ci	regmap_write(lpass->top, SFR_LPASS_INTR_CA5_MASK,
798c2ecf20Sopenharmony_ci		     LPASS_INTR_SFR | LPASS_INTR_DMA | LPASS_INTR_I2S);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	regmap_write(lpass->top, SFR_LPASS_INTR_CPU_MASK,
828c2ecf20Sopenharmony_ci		     LPASS_INTR_SFR | LPASS_INTR_DMA | LPASS_INTR_I2S |
838c2ecf20Sopenharmony_ci		     LPASS_INTR_UART);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	exynos_lpass_core_sw_reset(lpass, LPASS_I2S_SW_RESET);
868c2ecf20Sopenharmony_ci	exynos_lpass_core_sw_reset(lpass, LPASS_DMA_SW_RESET);
878c2ecf20Sopenharmony_ci	exynos_lpass_core_sw_reset(lpass, LPASS_MEM_SW_RESET);
888c2ecf20Sopenharmony_ci	exynos_lpass_core_sw_reset(lpass, LPASS_UART_SW_RESET);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void exynos_lpass_disable(struct exynos_lpass *lpass)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	/* Mask any unmasked IP interrupt sources */
948c2ecf20Sopenharmony_ci	regmap_write(lpass->top, SFR_LPASS_INTR_CPU_MASK, 0);
958c2ecf20Sopenharmony_ci	regmap_write(lpass->top, SFR_LPASS_INTR_CA5_MASK, 0);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	clk_disable_unprepare(lpass->sfr0_clk);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic const struct regmap_config exynos_lpass_reg_conf = {
1018c2ecf20Sopenharmony_ci	.reg_bits	= 32,
1028c2ecf20Sopenharmony_ci	.reg_stride	= 4,
1038c2ecf20Sopenharmony_ci	.val_bits	= 32,
1048c2ecf20Sopenharmony_ci	.max_register	= 0xfc,
1058c2ecf20Sopenharmony_ci	.fast_io	= true,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int exynos_lpass_probe(struct platform_device *pdev)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
1118c2ecf20Sopenharmony_ci	struct exynos_lpass *lpass;
1128c2ecf20Sopenharmony_ci	void __iomem *base_top;
1138c2ecf20Sopenharmony_ci	struct resource *res;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	lpass = devm_kzalloc(dev, sizeof(*lpass), GFP_KERNEL);
1168c2ecf20Sopenharmony_ci	if (!lpass)
1178c2ecf20Sopenharmony_ci		return -ENOMEM;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1208c2ecf20Sopenharmony_ci	base_top = devm_ioremap_resource(dev, res);
1218c2ecf20Sopenharmony_ci	if (IS_ERR(base_top))
1228c2ecf20Sopenharmony_ci		return PTR_ERR(base_top);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	lpass->sfr0_clk = devm_clk_get(dev, "sfr0_ctrl");
1258c2ecf20Sopenharmony_ci	if (IS_ERR(lpass->sfr0_clk))
1268c2ecf20Sopenharmony_ci		return PTR_ERR(lpass->sfr0_clk);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	lpass->top = regmap_init_mmio(dev, base_top,
1298c2ecf20Sopenharmony_ci					&exynos_lpass_reg_conf);
1308c2ecf20Sopenharmony_ci	if (IS_ERR(lpass->top)) {
1318c2ecf20Sopenharmony_ci		dev_err(dev, "LPASS top regmap initialization failed\n");
1328c2ecf20Sopenharmony_ci		return PTR_ERR(lpass->top);
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, lpass);
1368c2ecf20Sopenharmony_ci	pm_runtime_set_active(dev);
1378c2ecf20Sopenharmony_ci	pm_runtime_enable(dev);
1388c2ecf20Sopenharmony_ci	exynos_lpass_enable(lpass);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	return devm_of_platform_populate(dev);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic int exynos_lpass_remove(struct platform_device *pdev)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	struct exynos_lpass *lpass = platform_get_drvdata(pdev);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	exynos_lpass_disable(lpass);
1488c2ecf20Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
1498c2ecf20Sopenharmony_ci	if (!pm_runtime_status_suspended(&pdev->dev))
1508c2ecf20Sopenharmony_ci		exynos_lpass_disable(lpass);
1518c2ecf20Sopenharmony_ci	regmap_exit(lpass->top);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	return 0;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic int __maybe_unused exynos_lpass_suspend(struct device *dev)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct exynos_lpass *lpass = dev_get_drvdata(dev);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	exynos_lpass_disable(lpass);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return 0;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic int __maybe_unused exynos_lpass_resume(struct device *dev)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct exynos_lpass *lpass = dev_get_drvdata(dev);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	exynos_lpass_enable(lpass);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	return 0;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic const struct dev_pm_ops lpass_pm_ops = {
1758c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(exynos_lpass_suspend, exynos_lpass_resume, NULL)
1768c2ecf20Sopenharmony_ci	SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1778c2ecf20Sopenharmony_ci				     pm_runtime_force_resume)
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistatic const struct of_device_id exynos_lpass_of_match[] = {
1818c2ecf20Sopenharmony_ci	{ .compatible = "samsung,exynos5433-lpass" },
1828c2ecf20Sopenharmony_ci	{ },
1838c2ecf20Sopenharmony_ci};
1848c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, exynos_lpass_of_match);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic struct platform_driver exynos_lpass_driver = {
1878c2ecf20Sopenharmony_ci	.driver = {
1888c2ecf20Sopenharmony_ci		.name		= "exynos-lpass",
1898c2ecf20Sopenharmony_ci		.pm		= &lpass_pm_ops,
1908c2ecf20Sopenharmony_ci		.of_match_table	= exynos_lpass_of_match,
1918c2ecf20Sopenharmony_ci	},
1928c2ecf20Sopenharmony_ci	.probe	= exynos_lpass_probe,
1938c2ecf20Sopenharmony_ci	.remove	= exynos_lpass_remove,
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_cimodule_platform_driver(exynos_lpass_driver);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Samsung Low Power Audio Subsystem driver");
1988c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
199