xref: /linux/drivers/media/rc/img-ir/img-ir-core.c (revision 3932b9ca55b0be314a36d3e84faff3e823c081f5)
1 /*
2  * ImgTec IR Decoder found in PowerDown Controller.
3  *
4  * Copyright 2010-2014 Imagination Technologies Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This contains core img-ir code for setting up the driver. The two interfaces
12  * (raw and hardware decode) are handled separately.
13  */
14 
15 #include <linux/clk.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include "img-ir.h"
24 
25 static irqreturn_t img_ir_isr(int irq, void *dev_id)
26 {
27 	struct img_ir_priv *priv = dev_id;
28 	u32 irq_status;
29 
30 	spin_lock(&priv->lock);
31 	/* we have to clear irqs before reading */
32 	irq_status = img_ir_read(priv, IMG_IR_IRQ_STATUS);
33 	img_ir_write(priv, IMG_IR_IRQ_CLEAR, irq_status);
34 
35 	/* don't handle valid data irqs if we're only interested in matches */
36 	irq_status &= img_ir_read(priv, IMG_IR_IRQ_ENABLE);
37 
38 	/* hand off edge interrupts to raw decode handler */
39 	if (irq_status & IMG_IR_IRQ_EDGE && img_ir_raw_enabled(&priv->raw))
40 		img_ir_isr_raw(priv, irq_status);
41 
42 	/* hand off hardware match interrupts to hardware decode handler */
43 	if (irq_status & (IMG_IR_IRQ_DATA_MATCH |
44 			  IMG_IR_IRQ_DATA_VALID |
45 			  IMG_IR_IRQ_DATA2_VALID) &&
46 	    img_ir_hw_enabled(&priv->hw))
47 		img_ir_isr_hw(priv, irq_status);
48 
49 	spin_unlock(&priv->lock);
50 	return IRQ_HANDLED;
51 }
52 
53 static void img_ir_setup(struct img_ir_priv *priv)
54 {
55 	/* start off with interrupts disabled */
56 	img_ir_write(priv, IMG_IR_IRQ_ENABLE, 0);
57 
58 	img_ir_setup_raw(priv);
59 	img_ir_setup_hw(priv);
60 
61 	if (!IS_ERR(priv->clk))
62 		clk_prepare_enable(priv->clk);
63 }
64 
65 static void img_ir_ident(struct img_ir_priv *priv)
66 {
67 	u32 core_rev = img_ir_read(priv, IMG_IR_CORE_REV);
68 
69 	dev_info(priv->dev,
70 		 "IMG IR Decoder (%d.%d.%d.%d) probed successfully\n",
71 		 (core_rev & IMG_IR_DESIGNER) >> IMG_IR_DESIGNER_SHIFT,
72 		 (core_rev & IMG_IR_MAJOR_REV) >> IMG_IR_MAJOR_REV_SHIFT,
73 		 (core_rev & IMG_IR_MINOR_REV) >> IMG_IR_MINOR_REV_SHIFT,
74 		 (core_rev & IMG_IR_MAINT_REV) >> IMG_IR_MAINT_REV_SHIFT);
75 	dev_info(priv->dev, "Modes:%s%s\n",
76 		 img_ir_hw_enabled(&priv->hw) ? " hardware" : "",
77 		 img_ir_raw_enabled(&priv->raw) ? " raw" : "");
78 }
79 
80 static int img_ir_probe(struct platform_device *pdev)
81 {
82 	struct img_ir_priv *priv;
83 	struct resource *res_regs;
84 	int irq, error, error2;
85 
86 	/* Get resources from platform device */
87 	irq = platform_get_irq(pdev, 0);
88 	if (irq < 0) {
89 		dev_err(&pdev->dev, "cannot find IRQ resource\n");
90 		return irq;
91 	}
92 
93 	/* Private driver data */
94 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
95 	if (!priv) {
96 		dev_err(&pdev->dev, "cannot allocate device data\n");
97 		return -ENOMEM;
98 	}
99 	platform_set_drvdata(pdev, priv);
100 	priv->dev = &pdev->dev;
101 	spin_lock_init(&priv->lock);
102 
103 	/* Ioremap the registers */
104 	res_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105 	priv->reg_base = devm_ioremap_resource(&pdev->dev, res_regs);
106 	if (IS_ERR(priv->reg_base))
107 		return PTR_ERR(priv->reg_base);
108 
109 	/* Get core clock */
110 	priv->clk = devm_clk_get(&pdev->dev, "core");
111 	if (IS_ERR(priv->clk))
112 		dev_warn(&pdev->dev, "cannot get core clock resource\n");
113 	/*
114 	 * The driver doesn't need to know about the system ("sys") or power
115 	 * modulation ("mod") clocks yet
116 	 */
117 
118 	/* Set up raw & hw decoder */
119 	error = img_ir_probe_raw(priv);
120 	error2 = img_ir_probe_hw(priv);
121 	if (error && error2)
122 		return (error == -ENODEV) ? error2 : error;
123 
124 	/* Get the IRQ */
125 	priv->irq = irq;
126 	error = request_irq(priv->irq, img_ir_isr, 0, "img-ir", priv);
127 	if (error) {
128 		dev_err(&pdev->dev, "cannot register IRQ %u\n",
129 			priv->irq);
130 		error = -EIO;
131 		goto err_irq;
132 	}
133 
134 	img_ir_ident(priv);
135 	img_ir_setup(priv);
136 
137 	return 0;
138 
139 err_irq:
140 	img_ir_remove_hw(priv);
141 	img_ir_remove_raw(priv);
142 	return error;
143 }
144 
145 static int img_ir_remove(struct platform_device *pdev)
146 {
147 	struct img_ir_priv *priv = platform_get_drvdata(pdev);
148 
149 	free_irq(priv->irq, img_ir_isr);
150 	img_ir_remove_hw(priv);
151 	img_ir_remove_raw(priv);
152 
153 	if (!IS_ERR(priv->clk))
154 		clk_disable_unprepare(priv->clk);
155 	return 0;
156 }
157 
158 static SIMPLE_DEV_PM_OPS(img_ir_pmops, img_ir_suspend, img_ir_resume);
159 
160 static const struct of_device_id img_ir_match[] = {
161 	{ .compatible = "img,ir-rev1" },
162 	{}
163 };
164 MODULE_DEVICE_TABLE(of, img_ir_match);
165 
166 static struct platform_driver img_ir_driver = {
167 	.driver = {
168 		.name = "img-ir",
169 		.owner	= THIS_MODULE,
170 		.of_match_table	= img_ir_match,
171 		.pm = &img_ir_pmops,
172 	},
173 	.probe = img_ir_probe,
174 	.remove = img_ir_remove,
175 };
176 
177 module_platform_driver(img_ir_driver);
178 
179 MODULE_AUTHOR("Imagination Technologies Ltd.");
180 MODULE_DESCRIPTION("ImgTec IR");
181 MODULE_LICENSE("GPL");
182