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