Merge tag 'locking_urgent_for_v6.4_rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / drivers / media / rc / meson-ir.c
CommitLineData
9390467c 1// SPDX-License-Identifier: GPL-2.0
12ddbadf
BG
2/*
3 * Driver for Amlogic Meson IR remote receiver
4 *
5 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
12ddbadf
BG
6 */
7
8#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/interrupt.h>
11#include <linux/io.h>
12#include <linux/module.h>
13#include <linux/of_platform.h>
14#include <linux/platform_device.h>
15#include <linux/spinlock.h>
e7a937b5 16#include <linux/bitfield.h>
12ddbadf
BG
17
18#include <media/rc-core.h>
19
20#define DRIVER_NAME "meson-ir"
21
6edf27ee 22/* valid on all Meson platforms */
12ddbadf
BG
23#define IR_DEC_LDR_ACTIVE 0x00
24#define IR_DEC_LDR_IDLE 0x04
25#define IR_DEC_LDR_REPEAT 0x08
26#define IR_DEC_BIT_0 0x0c
27#define IR_DEC_REG0 0x10
28#define IR_DEC_FRAME 0x14
29#define IR_DEC_STATUS 0x18
30#define IR_DEC_REG1 0x1c
6edf27ee
NA
31/* only available on Meson 8b and newer */
32#define IR_DEC_REG2 0x20
12ddbadf 33
e7a937b5 34#define REG0_RATE_MASK GENMASK(11, 0)
12ddbadf 35
6edf27ee
NA
36#define DECODE_MODE_NEC 0x0
37#define DECODE_MODE_RAW 0x2
38
39/* Meson 6b uses REG1 to configure the mode */
40#define REG1_MODE_MASK GENMASK(8, 7)
41#define REG1_MODE_SHIFT 7
42
43/* Meson 8b / GXBB use REG2 to configure the mode */
44#define REG2_MODE_MASK GENMASK(3, 0)
45#define REG2_MODE_SHIFT 0
12ddbadf 46
e7a937b5 47#define REG1_TIME_IV_MASK GENMASK(28, 16)
12ddbadf 48
e7a937b5
HK
49#define REG1_IRQSEL_MASK GENMASK(3, 2)
50#define REG1_IRQSEL_NEC_MODE 0
51#define REG1_IRQSEL_RISE_FALL 1
52#define REG1_IRQSEL_FALL 2
53#define REG1_IRQSEL_RISE 3
12ddbadf
BG
54
55#define REG1_RESET BIT(0)
56#define REG1_ENABLE BIT(15)
57
58#define STATUS_IR_DEC_IN BIT(8)
59
60#define MESON_TRATE 10 /* us */
61
62struct meson_ir {
63 void __iomem *reg;
64 struct rc_dev *rc;
12ddbadf
BG
65 spinlock_t lock;
66};
67
68static void meson_ir_set_mask(struct meson_ir *ir, unsigned int reg,
69 u32 mask, u32 value)
70{
71 u32 data;
72
73 data = readl(ir->reg + reg);
74 data &= ~mask;
75 data |= (value & mask);
76 writel(data, ir->reg + reg);
77}
78
79static irqreturn_t meson_ir_irq(int irqno, void *dev_id)
80{
81 struct meson_ir *ir = dev_id;
137edc02 82 u32 duration, status;
183e19f5 83 struct ir_raw_event rawir = {};
12ddbadf
BG
84
85 spin_lock(&ir->lock);
86
137edc02 87 duration = readl_relaxed(ir->reg + IR_DEC_REG1);
e7a937b5 88 duration = FIELD_GET(REG1_TIME_IV_MASK, duration);
528222d8 89 rawir.duration = duration * MESON_TRATE;
12ddbadf 90
137edc02
HK
91 status = readl_relaxed(ir->reg + IR_DEC_STATUS);
92 rawir.pulse = !!(status & STATUS_IR_DEC_IN);
12ddbadf 93
8d7a77ce 94 ir_raw_event_store_with_timeout(ir->rc, &rawir);
12ddbadf
BG
95
96 spin_unlock(&ir->lock);
97
98 return IRQ_HANDLED;
99}
100
101static int meson_ir_probe(struct platform_device *pdev)
102{
103 struct device *dev = &pdev->dev;
104 struct device_node *node = dev->of_node;
12ddbadf
BG
105 const char *map_name;
106 struct meson_ir *ir;
1ffc931c 107 int irq, ret;
12ddbadf
BG
108
109 ir = devm_kzalloc(dev, sizeof(struct meson_ir), GFP_KERNEL);
110 if (!ir)
111 return -ENOMEM;
112
c533dabe 113 ir->reg = devm_platform_ioremap_resource(pdev, 0);
9e2e4382 114 if (IS_ERR(ir->reg))
12ddbadf 115 return PTR_ERR(ir->reg);
12ddbadf 116
1ffc931c 117 irq = platform_get_irq(pdev, 0);
97299a30 118 if (irq < 0)
1ffc931c 119 return irq;
12ddbadf 120
705aa578 121 ir->rc = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
12ddbadf
BG
122 if (!ir->rc) {
123 dev_err(dev, "failed to allocate rc device\n");
124 return -ENOMEM;
125 }
126
127 ir->rc->priv = ir;
518f4b26 128 ir->rc->device_name = DRIVER_NAME;
12ddbadf
BG
129 ir->rc->input_phys = DRIVER_NAME "/input0";
130 ir->rc->input_id.bustype = BUS_HOST;
131 map_name = of_get_property(node, "linux,rc-map-name", NULL);
132 ir->rc->map_name = map_name ? map_name : RC_MAP_EMPTY;
6d741bfe 133 ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
528222d8 134 ir->rc->rx_resolution = MESON_TRATE;
b358e747
SY
135 ir->rc->min_timeout = 1;
136 ir->rc->timeout = IR_DEFAULT_TIMEOUT;
137 ir->rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
12ddbadf
BG
138 ir->rc->driver_name = DRIVER_NAME;
139
140 spin_lock_init(&ir->lock);
141 platform_set_drvdata(pdev, ir);
142
705aa578 143 ret = devm_rc_register_device(dev, ir->rc);
12ddbadf
BG
144 if (ret) {
145 dev_err(dev, "failed to register rc device\n");
705aa578 146 return ret;
12ddbadf
BG
147 }
148
611ee552 149 ret = devm_request_irq(dev, irq, meson_ir_irq, 0, NULL, ir);
12ddbadf
BG
150 if (ret) {
151 dev_err(dev, "failed to request irq\n");
705aa578 152 return ret;
12ddbadf
BG
153 }
154
155 /* Reset the decoder */
156 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, REG1_RESET);
157 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, 0);
6edf27ee
NA
158
159 /* Set general operation mode (= raw/software decoding) */
160 if (of_device_is_compatible(node, "amlogic,meson6-ir"))
161 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK,
e7a937b5 162 FIELD_PREP(REG1_MODE_MASK, DECODE_MODE_RAW));
6edf27ee
NA
163 else
164 meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK,
e7a937b5 165 FIELD_PREP(REG2_MODE_MASK, DECODE_MODE_RAW));
6edf27ee 166
12ddbadf
BG
167 /* Set rate */
168 meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, MESON_TRATE - 1);
169 /* IRQ on rising and falling edges */
170 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_IRQSEL_MASK,
e7a937b5 171 FIELD_PREP(REG1_IRQSEL_MASK, REG1_IRQSEL_RISE_FALL));
12ddbadf
BG
172 /* Enable the decoder */
173 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, REG1_ENABLE);
174
175 dev_info(dev, "receiver initialized\n");
176
177 return 0;
12ddbadf
BG
178}
179
e6c04474 180static void meson_ir_remove(struct platform_device *pdev)
12ddbadf
BG
181{
182 struct meson_ir *ir = platform_get_drvdata(pdev);
183 unsigned long flags;
184
185 /* Disable the decoder */
186 spin_lock_irqsave(&ir->lock, flags);
187 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, 0);
188 spin_unlock_irqrestore(&ir->lock, flags);
12ddbadf
BG
189}
190
2aa1bd1c
AD
191static void meson_ir_shutdown(struct platform_device *pdev)
192{
193 struct device *dev = &pdev->dev;
194 struct device_node *node = dev->of_node;
195 struct meson_ir *ir = platform_get_drvdata(pdev);
196 unsigned long flags;
197
198 spin_lock_irqsave(&ir->lock, flags);
199
200 /*
201 * Set operation mode to NEC/hardware decoding to give
202 * bootloader a chance to power the system back on
203 */
204 if (of_device_is_compatible(node, "amlogic,meson6-ir"))
205 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK,
206 DECODE_MODE_NEC << REG1_MODE_SHIFT);
207 else
208 meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK,
209 DECODE_MODE_NEC << REG2_MODE_SHIFT);
210
211 /* Set rate to default value */
212 meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, 0x13);
213
214 spin_unlock_irqrestore(&ir->lock, flags);
215}
216
12ddbadf
BG
217static const struct of_device_id meson_ir_match[] = {
218 { .compatible = "amlogic,meson6-ir" },
6edf27ee
NA
219 { .compatible = "amlogic,meson8b-ir" },
220 { .compatible = "amlogic,meson-gxbb-ir" },
12ddbadf
BG
221 { },
222};
5bb50fe7 223MODULE_DEVICE_TABLE(of, meson_ir_match);
12ddbadf
BG
224
225static struct platform_driver meson_ir_driver = {
226 .probe = meson_ir_probe,
e6c04474 227 .remove_new = meson_ir_remove,
2aa1bd1c 228 .shutdown = meson_ir_shutdown,
12ddbadf
BG
229 .driver = {
230 .name = DRIVER_NAME,
231 .of_match_table = meson_ir_match,
232 },
233};
234
235module_platform_driver(meson_ir_driver);
236
237MODULE_DESCRIPTION("Amlogic Meson IR remote receiver driver");
238MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
239MODULE_LICENSE("GPL v2");