18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for Allwinner sunXi IR controller
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Alexsey Shestacov <wingrime@linux-sunxi.org>
68c2ecf20Sopenharmony_ci * Copyright (C) 2014 Alexander Bersenev <bay@hackerdom.ru>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Based on sun5i-ir.c:
98c2ecf20Sopenharmony_ci * Copyright (C) 2007-2012 Daniel Wang
108c2ecf20Sopenharmony_ci * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
178c2ecf20Sopenharmony_ci#include <linux/reset.h>
188c2ecf20Sopenharmony_ci#include <media/rc-core.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define SUNXI_IR_DEV "sunxi-ir"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* Registers */
238c2ecf20Sopenharmony_ci/* IR Control */
248c2ecf20Sopenharmony_ci#define SUNXI_IR_CTL_REG      0x00
258c2ecf20Sopenharmony_ci/* Global Enable */
268c2ecf20Sopenharmony_ci#define REG_CTL_GEN			BIT(0)
278c2ecf20Sopenharmony_ci/* RX block enable */
288c2ecf20Sopenharmony_ci#define REG_CTL_RXEN			BIT(1)
298c2ecf20Sopenharmony_ci/* CIR mode */
308c2ecf20Sopenharmony_ci#define REG_CTL_MD			(BIT(4) | BIT(5))
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Rx Config */
338c2ecf20Sopenharmony_ci#define SUNXI_IR_RXCTL_REG    0x10
348c2ecf20Sopenharmony_ci/* Pulse Polarity Invert flag */
358c2ecf20Sopenharmony_ci#define REG_RXCTL_RPPI			BIT(2)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* Rx Data */
388c2ecf20Sopenharmony_ci#define SUNXI_IR_RXFIFO_REG   0x20
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/* Rx Interrupt Enable */
418c2ecf20Sopenharmony_ci#define SUNXI_IR_RXINT_REG    0x2C
428c2ecf20Sopenharmony_ci/* Rx FIFO Overflow Interrupt Enable */
438c2ecf20Sopenharmony_ci#define REG_RXINT_ROI_EN		BIT(0)
448c2ecf20Sopenharmony_ci/* Rx Packet End Interrupt Enable */
458c2ecf20Sopenharmony_ci#define REG_RXINT_RPEI_EN		BIT(1)
468c2ecf20Sopenharmony_ci/* Rx FIFO Data Available Interrupt Enable */
478c2ecf20Sopenharmony_ci#define REG_RXINT_RAI_EN		BIT(4)
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* Rx FIFO available byte level */
508c2ecf20Sopenharmony_ci#define REG_RXINT_RAL(val)    ((val) << 8)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* Rx Interrupt Status */
538c2ecf20Sopenharmony_ci#define SUNXI_IR_RXSTA_REG    0x30
548c2ecf20Sopenharmony_ci/* Rx FIFO Overflow */
558c2ecf20Sopenharmony_ci#define REG_RXSTA_ROI			REG_RXINT_ROI_EN
568c2ecf20Sopenharmony_ci/* Rx Packet End */
578c2ecf20Sopenharmony_ci#define REG_RXSTA_RPE			REG_RXINT_RPEI_EN
588c2ecf20Sopenharmony_ci/* Rx FIFO Data Available */
598c2ecf20Sopenharmony_ci#define REG_RXSTA_RA			REG_RXINT_RAI_EN
608c2ecf20Sopenharmony_ci/* RX FIFO Get Available Counter */
618c2ecf20Sopenharmony_ci#define REG_RXSTA_GET_AC(val) (((val) >> 8) & (ir->fifo_size * 2 - 1))
628c2ecf20Sopenharmony_ci/* Clear all interrupt status value */
638c2ecf20Sopenharmony_ci#define REG_RXSTA_CLEARALL    0xff
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/* IR Sample Config */
668c2ecf20Sopenharmony_ci#define SUNXI_IR_CIR_REG      0x34
678c2ecf20Sopenharmony_ci/* CIR_REG register noise threshold */
688c2ecf20Sopenharmony_ci#define REG_CIR_NTHR(val)    (((val) << 2) & (GENMASK(7, 2)))
698c2ecf20Sopenharmony_ci/* CIR_REG register idle threshold */
708c2ecf20Sopenharmony_ci#define REG_CIR_ITHR(val)    (((val) << 8) & (GENMASK(15, 8)))
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* Required frequency for IR0 or IR1 clock in CIR mode (default) */
738c2ecf20Sopenharmony_ci#define SUNXI_IR_BASE_CLK     8000000
748c2ecf20Sopenharmony_ci/* Noise threshold in samples  */
758c2ecf20Sopenharmony_ci#define SUNXI_IR_RXNOISE      1
768c2ecf20Sopenharmony_ci/* Idle Threshold in samples */
778c2ecf20Sopenharmony_ci#define SUNXI_IR_RXIDLE       20
788c2ecf20Sopenharmony_ci/* Time after which device stops sending data in ms */
798c2ecf20Sopenharmony_ci#define SUNXI_IR_TIMEOUT      120
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/**
828c2ecf20Sopenharmony_ci * struct sunxi_ir_quirks - Differences between SoC variants.
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci * @has_reset: SoC needs reset deasserted.
858c2ecf20Sopenharmony_ci * @fifo_size: size of the fifo.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_cistruct sunxi_ir_quirks {
888c2ecf20Sopenharmony_ci	bool		has_reset;
898c2ecf20Sopenharmony_ci	int		fifo_size;
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistruct sunxi_ir {
938c2ecf20Sopenharmony_ci	spinlock_t      ir_lock;
948c2ecf20Sopenharmony_ci	struct rc_dev   *rc;
958c2ecf20Sopenharmony_ci	void __iomem    *base;
968c2ecf20Sopenharmony_ci	int             irq;
978c2ecf20Sopenharmony_ci	int		fifo_size;
988c2ecf20Sopenharmony_ci	struct clk      *clk;
998c2ecf20Sopenharmony_ci	struct clk      *apb_clk;
1008c2ecf20Sopenharmony_ci	struct reset_control *rst;
1018c2ecf20Sopenharmony_ci	const char      *map_name;
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic irqreturn_t sunxi_ir_irq(int irqno, void *dev_id)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	unsigned long status;
1078c2ecf20Sopenharmony_ci	unsigned char dt;
1088c2ecf20Sopenharmony_ci	unsigned int cnt, rc;
1098c2ecf20Sopenharmony_ci	struct sunxi_ir *ir = dev_id;
1108c2ecf20Sopenharmony_ci	struct ir_raw_event rawir = {};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	spin_lock(&ir->ir_lock);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	status = readl(ir->base + SUNXI_IR_RXSTA_REG);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* clean all pending statuses */
1178c2ecf20Sopenharmony_ci	writel(status | REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (status & (REG_RXSTA_RA | REG_RXSTA_RPE)) {
1208c2ecf20Sopenharmony_ci		/* How many messages in fifo */
1218c2ecf20Sopenharmony_ci		rc  = REG_RXSTA_GET_AC(status);
1228c2ecf20Sopenharmony_ci		/* Sanity check */
1238c2ecf20Sopenharmony_ci		rc = rc > ir->fifo_size ? ir->fifo_size : rc;
1248c2ecf20Sopenharmony_ci		/* If we have data */
1258c2ecf20Sopenharmony_ci		for (cnt = 0; cnt < rc; cnt++) {
1268c2ecf20Sopenharmony_ci			/* for each bit in fifo */
1278c2ecf20Sopenharmony_ci			dt = readb(ir->base + SUNXI_IR_RXFIFO_REG);
1288c2ecf20Sopenharmony_ci			rawir.pulse = (dt & 0x80) != 0;
1298c2ecf20Sopenharmony_ci			rawir.duration = ((dt & 0x7f) + 1) *
1308c2ecf20Sopenharmony_ci					 ir->rc->rx_resolution;
1318c2ecf20Sopenharmony_ci			ir_raw_event_store_with_filter(ir->rc, &rawir);
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (status & REG_RXSTA_ROI) {
1368c2ecf20Sopenharmony_ci		ir_raw_event_reset(ir->rc);
1378c2ecf20Sopenharmony_ci	} else if (status & REG_RXSTA_RPE) {
1388c2ecf20Sopenharmony_ci		ir_raw_event_set_idle(ir->rc, true);
1398c2ecf20Sopenharmony_ci		ir_raw_event_handle(ir->rc);
1408c2ecf20Sopenharmony_ci	} else {
1418c2ecf20Sopenharmony_ci		ir_raw_event_handle(ir->rc);
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	spin_unlock(&ir->ir_lock);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic int sunxi_ir_probe(struct platform_device *pdev)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	int ret = 0;
1528c2ecf20Sopenharmony_ci	unsigned long tmp = 0;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
1558c2ecf20Sopenharmony_ci	struct device_node *dn = dev->of_node;
1568c2ecf20Sopenharmony_ci	const struct sunxi_ir_quirks *quirks;
1578c2ecf20Sopenharmony_ci	struct resource *res;
1588c2ecf20Sopenharmony_ci	struct sunxi_ir *ir;
1598c2ecf20Sopenharmony_ci	u32 b_clk_freq = SUNXI_IR_BASE_CLK;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	ir = devm_kzalloc(dev, sizeof(struct sunxi_ir), GFP_KERNEL);
1628c2ecf20Sopenharmony_ci	if (!ir)
1638c2ecf20Sopenharmony_ci		return -ENOMEM;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	quirks = of_device_get_match_data(&pdev->dev);
1668c2ecf20Sopenharmony_ci	if (!quirks) {
1678c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
1688c2ecf20Sopenharmony_ci		return -ENODEV;
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	spin_lock_init(&ir->ir_lock);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	ir->fifo_size = quirks->fifo_size;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* Clock */
1768c2ecf20Sopenharmony_ci	ir->apb_clk = devm_clk_get(dev, "apb");
1778c2ecf20Sopenharmony_ci	if (IS_ERR(ir->apb_clk)) {
1788c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get a apb clock.\n");
1798c2ecf20Sopenharmony_ci		return PTR_ERR(ir->apb_clk);
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci	ir->clk = devm_clk_get(dev, "ir");
1828c2ecf20Sopenharmony_ci	if (IS_ERR(ir->clk)) {
1838c2ecf20Sopenharmony_ci		dev_err(dev, "failed to get a ir clock.\n");
1848c2ecf20Sopenharmony_ci		return PTR_ERR(ir->clk);
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	/* Base clock frequency (optional) */
1888c2ecf20Sopenharmony_ci	of_property_read_u32(dn, "clock-frequency", &b_clk_freq);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	/* Reset */
1918c2ecf20Sopenharmony_ci	if (quirks->has_reset) {
1928c2ecf20Sopenharmony_ci		ir->rst = devm_reset_control_get_exclusive(dev, NULL);
1938c2ecf20Sopenharmony_ci		if (IS_ERR(ir->rst))
1948c2ecf20Sopenharmony_ci			return PTR_ERR(ir->rst);
1958c2ecf20Sopenharmony_ci		ret = reset_control_deassert(ir->rst);
1968c2ecf20Sopenharmony_ci		if (ret)
1978c2ecf20Sopenharmony_ci			return ret;
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	ret = clk_set_rate(ir->clk, b_clk_freq);
2018c2ecf20Sopenharmony_ci	if (ret) {
2028c2ecf20Sopenharmony_ci		dev_err(dev, "set ir base clock failed!\n");
2038c2ecf20Sopenharmony_ci		goto exit_reset_assert;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci	dev_dbg(dev, "set base clock frequency to %d Hz.\n", b_clk_freq);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	if (clk_prepare_enable(ir->apb_clk)) {
2088c2ecf20Sopenharmony_ci		dev_err(dev, "try to enable apb_ir_clk failed\n");
2098c2ecf20Sopenharmony_ci		ret = -EINVAL;
2108c2ecf20Sopenharmony_ci		goto exit_reset_assert;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (clk_prepare_enable(ir->clk)) {
2148c2ecf20Sopenharmony_ci		dev_err(dev, "try to enable ir_clk failed\n");
2158c2ecf20Sopenharmony_ci		ret = -EINVAL;
2168c2ecf20Sopenharmony_ci		goto exit_clkdisable_apb_clk;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	/* IO */
2208c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2218c2ecf20Sopenharmony_ci	ir->base = devm_ioremap_resource(dev, res);
2228c2ecf20Sopenharmony_ci	if (IS_ERR(ir->base)) {
2238c2ecf20Sopenharmony_ci		ret = PTR_ERR(ir->base);
2248c2ecf20Sopenharmony_ci		goto exit_clkdisable_clk;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	ir->rc = rc_allocate_device(RC_DRIVER_IR_RAW);
2288c2ecf20Sopenharmony_ci	if (!ir->rc) {
2298c2ecf20Sopenharmony_ci		dev_err(dev, "failed to allocate device\n");
2308c2ecf20Sopenharmony_ci		ret = -ENOMEM;
2318c2ecf20Sopenharmony_ci		goto exit_clkdisable_clk;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	ir->rc->priv = ir;
2358c2ecf20Sopenharmony_ci	ir->rc->device_name = SUNXI_IR_DEV;
2368c2ecf20Sopenharmony_ci	ir->rc->input_phys = "sunxi-ir/input0";
2378c2ecf20Sopenharmony_ci	ir->rc->input_id.bustype = BUS_HOST;
2388c2ecf20Sopenharmony_ci	ir->rc->input_id.vendor = 0x0001;
2398c2ecf20Sopenharmony_ci	ir->rc->input_id.product = 0x0001;
2408c2ecf20Sopenharmony_ci	ir->rc->input_id.version = 0x0100;
2418c2ecf20Sopenharmony_ci	ir->map_name = of_get_property(dn, "linux,rc-map-name", NULL);
2428c2ecf20Sopenharmony_ci	ir->rc->map_name = ir->map_name ?: RC_MAP_EMPTY;
2438c2ecf20Sopenharmony_ci	ir->rc->dev.parent = dev;
2448c2ecf20Sopenharmony_ci	ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
2458c2ecf20Sopenharmony_ci	/* Frequency after IR internal divider with sample period in ns */
2468c2ecf20Sopenharmony_ci	ir->rc->rx_resolution = (USEC_PER_SEC / (b_clk_freq / 64));
2478c2ecf20Sopenharmony_ci	ir->rc->timeout = MS_TO_US(SUNXI_IR_TIMEOUT);
2488c2ecf20Sopenharmony_ci	ir->rc->driver_name = SUNXI_IR_DEV;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	ret = rc_register_device(ir->rc);
2518c2ecf20Sopenharmony_ci	if (ret) {
2528c2ecf20Sopenharmony_ci		dev_err(dev, "failed to register rc device\n");
2538c2ecf20Sopenharmony_ci		goto exit_free_dev;
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ir);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/* IRQ */
2598c2ecf20Sopenharmony_ci	ir->irq = platform_get_irq(pdev, 0);
2608c2ecf20Sopenharmony_ci	if (ir->irq < 0) {
2618c2ecf20Sopenharmony_ci		ret = ir->irq;
2628c2ecf20Sopenharmony_ci		goto exit_free_dev;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	ret = devm_request_irq(dev, ir->irq, sunxi_ir_irq, 0, SUNXI_IR_DEV, ir);
2668c2ecf20Sopenharmony_ci	if (ret) {
2678c2ecf20Sopenharmony_ci		dev_err(dev, "failed request irq\n");
2688c2ecf20Sopenharmony_ci		goto exit_free_dev;
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	/* Enable CIR Mode */
2728c2ecf20Sopenharmony_ci	writel(REG_CTL_MD, ir->base+SUNXI_IR_CTL_REG);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/* Set noise threshold and idle threshold */
2758c2ecf20Sopenharmony_ci	writel(REG_CIR_NTHR(SUNXI_IR_RXNOISE)|REG_CIR_ITHR(SUNXI_IR_RXIDLE),
2768c2ecf20Sopenharmony_ci	       ir->base + SUNXI_IR_CIR_REG);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	/* Invert Input Signal */
2798c2ecf20Sopenharmony_ci	writel(REG_RXCTL_RPPI, ir->base + SUNXI_IR_RXCTL_REG);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	/* Clear All Rx Interrupt Status */
2828c2ecf20Sopenharmony_ci	writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/*
2858c2ecf20Sopenharmony_ci	 * Enable IRQ on overflow, packet end, FIFO available with trigger
2868c2ecf20Sopenharmony_ci	 * level
2878c2ecf20Sopenharmony_ci	 */
2888c2ecf20Sopenharmony_ci	writel(REG_RXINT_ROI_EN | REG_RXINT_RPEI_EN |
2898c2ecf20Sopenharmony_ci	       REG_RXINT_RAI_EN | REG_RXINT_RAL(ir->fifo_size / 2 - 1),
2908c2ecf20Sopenharmony_ci	       ir->base + SUNXI_IR_RXINT_REG);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	/* Enable IR Module */
2938c2ecf20Sopenharmony_ci	tmp = readl(ir->base + SUNXI_IR_CTL_REG);
2948c2ecf20Sopenharmony_ci	writel(tmp | REG_CTL_GEN | REG_CTL_RXEN, ir->base + SUNXI_IR_CTL_REG);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	dev_info(dev, "initialized sunXi IR driver\n");
2978c2ecf20Sopenharmony_ci	return 0;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ciexit_free_dev:
3008c2ecf20Sopenharmony_ci	rc_free_device(ir->rc);
3018c2ecf20Sopenharmony_ciexit_clkdisable_clk:
3028c2ecf20Sopenharmony_ci	clk_disable_unprepare(ir->clk);
3038c2ecf20Sopenharmony_ciexit_clkdisable_apb_clk:
3048c2ecf20Sopenharmony_ci	clk_disable_unprepare(ir->apb_clk);
3058c2ecf20Sopenharmony_ciexit_reset_assert:
3068c2ecf20Sopenharmony_ci	reset_control_assert(ir->rst);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	return ret;
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic int sunxi_ir_remove(struct platform_device *pdev)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	unsigned long flags;
3148c2ecf20Sopenharmony_ci	struct sunxi_ir *ir = platform_get_drvdata(pdev);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	clk_disable_unprepare(ir->clk);
3178c2ecf20Sopenharmony_ci	clk_disable_unprepare(ir->apb_clk);
3188c2ecf20Sopenharmony_ci	reset_control_assert(ir->rst);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ir->ir_lock, flags);
3218c2ecf20Sopenharmony_ci	/* disable IR IRQ */
3228c2ecf20Sopenharmony_ci	writel(0, ir->base + SUNXI_IR_RXINT_REG);
3238c2ecf20Sopenharmony_ci	/* clear All Rx Interrupt Status */
3248c2ecf20Sopenharmony_ci	writel(REG_RXSTA_CLEARALL, ir->base + SUNXI_IR_RXSTA_REG);
3258c2ecf20Sopenharmony_ci	/* disable IR */
3268c2ecf20Sopenharmony_ci	writel(0, ir->base + SUNXI_IR_CTL_REG);
3278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ir->ir_lock, flags);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	rc_unregister_device(ir->rc);
3308c2ecf20Sopenharmony_ci	return 0;
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic const struct sunxi_ir_quirks sun4i_a10_ir_quirks = {
3348c2ecf20Sopenharmony_ci	.has_reset = false,
3358c2ecf20Sopenharmony_ci	.fifo_size = 16,
3368c2ecf20Sopenharmony_ci};
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic const struct sunxi_ir_quirks sun5i_a13_ir_quirks = {
3398c2ecf20Sopenharmony_ci	.has_reset = false,
3408c2ecf20Sopenharmony_ci	.fifo_size = 64,
3418c2ecf20Sopenharmony_ci};
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic const struct sunxi_ir_quirks sun6i_a31_ir_quirks = {
3448c2ecf20Sopenharmony_ci	.has_reset = true,
3458c2ecf20Sopenharmony_ci	.fifo_size = 64,
3468c2ecf20Sopenharmony_ci};
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic const struct of_device_id sunxi_ir_match[] = {
3498c2ecf20Sopenharmony_ci	{
3508c2ecf20Sopenharmony_ci		.compatible = "allwinner,sun4i-a10-ir",
3518c2ecf20Sopenharmony_ci		.data = &sun4i_a10_ir_quirks,
3528c2ecf20Sopenharmony_ci	},
3538c2ecf20Sopenharmony_ci	{
3548c2ecf20Sopenharmony_ci		.compatible = "allwinner,sun5i-a13-ir",
3558c2ecf20Sopenharmony_ci		.data = &sun5i_a13_ir_quirks,
3568c2ecf20Sopenharmony_ci	},
3578c2ecf20Sopenharmony_ci	{
3588c2ecf20Sopenharmony_ci		.compatible = "allwinner,sun6i-a31-ir",
3598c2ecf20Sopenharmony_ci		.data = &sun6i_a31_ir_quirks,
3608c2ecf20Sopenharmony_ci	},
3618c2ecf20Sopenharmony_ci	{}
3628c2ecf20Sopenharmony_ci};
3638c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sunxi_ir_match);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistatic struct platform_driver sunxi_ir_driver = {
3668c2ecf20Sopenharmony_ci	.probe          = sunxi_ir_probe,
3678c2ecf20Sopenharmony_ci	.remove         = sunxi_ir_remove,
3688c2ecf20Sopenharmony_ci	.driver = {
3698c2ecf20Sopenharmony_ci		.name = SUNXI_IR_DEV,
3708c2ecf20Sopenharmony_ci		.of_match_table = sunxi_ir_match,
3718c2ecf20Sopenharmony_ci	},
3728c2ecf20Sopenharmony_ci};
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cimodule_platform_driver(sunxi_ir_driver);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Allwinner sunXi IR controller driver");
3778c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alexsey Shestacov <wingrime@linux-sunxi.org>");
3788c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
379