blob: 72a7bbbf6b1f03eb1393be9e8dd5dc53edb93856 [file] [log] [blame]
Neil Armstrong93904672019-05-20 10:03:43 -04001// SPDX-License-Identifier: GPL-2.0
Beniamino Galvani12ddbad2014-11-18 17:22:34 -03002/*
3 * Driver for Amlogic Meson IR remote receiver
4 *
5 * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
Beniamino Galvani12ddbad2014-11-18 17:22:34 -03006 */
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>
Heiner Kallweite7a937b2017-04-12 16:30:48 -030016#include <linux/bitfield.h>
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030017
18#include <media/rc-core.h>
19
20#define DRIVER_NAME "meson-ir"
21
Neil Armstrong6edf27e2016-08-20 11:54:22 +020022/* valid on all Meson platforms */
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030023#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
Neil Armstrong6edf27e2016-08-20 11:54:22 +020031/* only available on Meson 8b and newer */
32#define IR_DEC_REG2 0x20
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030033
Heiner Kallweite7a937b2017-04-12 16:30:48 -030034#define REG0_RATE_MASK GENMASK(11, 0)
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030035
Neil Armstrong6edf27e2016-08-20 11:54:22 +020036#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
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030046
Heiner Kallweite7a937b2017-04-12 16:30:48 -030047#define REG1_TIME_IV_MASK GENMASK(28, 16)
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030048
Heiner Kallweite7a937b2017-04-12 16:30:48 -030049#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
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030054
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;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030065 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;
Heiner Kallweit137edc02017-04-12 16:33:57 -030082 u32 duration, status;
Sean Young183e19f2018-08-21 15:57:52 -040083 struct ir_raw_event rawir = {};
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030084
85 spin_lock(&ir->lock);
86
Heiner Kallweit137edc02017-04-12 16:33:57 -030087 duration = readl_relaxed(ir->reg + IR_DEC_REG1);
Heiner Kallweite7a937b2017-04-12 16:30:48 -030088 duration = FIELD_GET(REG1_TIME_IV_MASK, duration);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030089 rawir.duration = US_TO_NS(duration * MESON_TRATE);
90
Heiner Kallweit137edc02017-04-12 16:33:57 -030091 status = readl_relaxed(ir->reg + IR_DEC_STATUS);
92 rawir.pulse = !!(status & STATUS_IR_DEC_IN);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030093
Sean Young8d7a77c2018-03-08 09:42:44 -050094 ir_raw_event_store_with_timeout(ir->rc, &rawir);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -030095
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;
105 struct resource *res;
106 const char *map_name;
107 struct meson_ir *ir;
Heiner Kallweit1ffc9312017-04-12 16:28:42 -0300108 int irq, ret;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300109
110 ir = devm_kzalloc(dev, sizeof(struct meson_ir), GFP_KERNEL);
111 if (!ir)
112 return -ENOMEM;
113
114 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
115 ir->reg = devm_ioremap_resource(dev, res);
Ding Xiang9e2e4382019-06-18 05:13:09 -0400116 if (IS_ERR(ir->reg))
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300117 return PTR_ERR(ir->reg);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300118
Heiner Kallweit1ffc9312017-04-12 16:28:42 -0300119 irq = platform_get_irq(pdev, 0);
120 if (irq < 0) {
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300121 dev_err(dev, "no irq resource\n");
Heiner Kallweit1ffc9312017-04-12 16:28:42 -0300122 return irq;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300123 }
124
Heiner Kallweit705aa572017-04-12 16:32:35 -0300125 ir->rc = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300126 if (!ir->rc) {
127 dev_err(dev, "failed to allocate rc device\n");
128 return -ENOMEM;
129 }
130
131 ir->rc->priv = ir;
Sean Young518f4b22017-07-01 12:13:19 -0400132 ir->rc->device_name = DRIVER_NAME;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300133 ir->rc->input_phys = DRIVER_NAME "/input0";
134 ir->rc->input_id.bustype = BUS_HOST;
135 map_name = of_get_property(node, "linux,rc-map-name", NULL);
136 ir->rc->map_name = map_name ? map_name : RC_MAP_EMPTY;
Sean Young6d741bf2017-08-07 16:20:58 -0400137 ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300138 ir->rc->rx_resolution = US_TO_NS(MESON_TRATE);
Sean Youngb358e742018-03-12 17:23:00 -0400139 ir->rc->min_timeout = 1;
140 ir->rc->timeout = IR_DEFAULT_TIMEOUT;
141 ir->rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300142 ir->rc->driver_name = DRIVER_NAME;
143
144 spin_lock_init(&ir->lock);
145 platform_set_drvdata(pdev, ir);
146
Heiner Kallweit705aa572017-04-12 16:32:35 -0300147 ret = devm_rc_register_device(dev, ir->rc);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300148 if (ret) {
149 dev_err(dev, "failed to register rc device\n");
Heiner Kallweit705aa572017-04-12 16:32:35 -0300150 return ret;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300151 }
152
Heiner Kallweit611ee552017-04-12 16:34:50 -0300153 ret = devm_request_irq(dev, irq, meson_ir_irq, 0, NULL, ir);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300154 if (ret) {
155 dev_err(dev, "failed to request irq\n");
Heiner Kallweit705aa572017-04-12 16:32:35 -0300156 return ret;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300157 }
158
159 /* Reset the decoder */
160 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, REG1_RESET);
161 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, 0);
Neil Armstrong6edf27e2016-08-20 11:54:22 +0200162
163 /* Set general operation mode (= raw/software decoding) */
164 if (of_device_is_compatible(node, "amlogic,meson6-ir"))
165 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK,
Heiner Kallweite7a937b2017-04-12 16:30:48 -0300166 FIELD_PREP(REG1_MODE_MASK, DECODE_MODE_RAW));
Neil Armstrong6edf27e2016-08-20 11:54:22 +0200167 else
168 meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK,
Heiner Kallweite7a937b2017-04-12 16:30:48 -0300169 FIELD_PREP(REG2_MODE_MASK, DECODE_MODE_RAW));
Neil Armstrong6edf27e2016-08-20 11:54:22 +0200170
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300171 /* Set rate */
172 meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, MESON_TRATE - 1);
173 /* IRQ on rising and falling edges */
174 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_IRQSEL_MASK,
Heiner Kallweite7a937b2017-04-12 16:30:48 -0300175 FIELD_PREP(REG1_IRQSEL_MASK, REG1_IRQSEL_RISE_FALL));
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300176 /* Enable the decoder */
177 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, REG1_ENABLE);
178
179 dev_info(dev, "receiver initialized\n");
180
181 return 0;
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300182}
183
184static int meson_ir_remove(struct platform_device *pdev)
185{
186 struct meson_ir *ir = platform_get_drvdata(pdev);
187 unsigned long flags;
188
189 /* Disable the decoder */
190 spin_lock_irqsave(&ir->lock, flags);
191 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, 0);
192 spin_unlock_irqrestore(&ir->lock, flags);
193
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300194 return 0;
195}
196
Alex Deryskyba2aa1bd12017-04-25 04:41:09 -0300197static void meson_ir_shutdown(struct platform_device *pdev)
198{
199 struct device *dev = &pdev->dev;
200 struct device_node *node = dev->of_node;
201 struct meson_ir *ir = platform_get_drvdata(pdev);
202 unsigned long flags;
203
204 spin_lock_irqsave(&ir->lock, flags);
205
206 /*
207 * Set operation mode to NEC/hardware decoding to give
208 * bootloader a chance to power the system back on
209 */
210 if (of_device_is_compatible(node, "amlogic,meson6-ir"))
211 meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK,
212 DECODE_MODE_NEC << REG1_MODE_SHIFT);
213 else
214 meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK,
215 DECODE_MODE_NEC << REG2_MODE_SHIFT);
216
217 /* Set rate to default value */
218 meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, 0x13);
219
220 spin_unlock_irqrestore(&ir->lock, flags);
221}
222
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300223static const struct of_device_id meson_ir_match[] = {
224 { .compatible = "amlogic,meson6-ir" },
Neil Armstrong6edf27e2016-08-20 11:54:22 +0200225 { .compatible = "amlogic,meson8b-ir" },
226 { .compatible = "amlogic,meson-gxbb-ir" },
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300227 { },
228};
Javier Martinez Canillas5bb50fe2016-10-17 13:44:10 -0200229MODULE_DEVICE_TABLE(of, meson_ir_match);
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300230
231static struct platform_driver meson_ir_driver = {
232 .probe = meson_ir_probe,
233 .remove = meson_ir_remove,
Alex Deryskyba2aa1bd12017-04-25 04:41:09 -0300234 .shutdown = meson_ir_shutdown,
Beniamino Galvani12ddbad2014-11-18 17:22:34 -0300235 .driver = {
236 .name = DRIVER_NAME,
237 .of_match_table = meson_ir_match,
238 },
239};
240
241module_platform_driver(meson_ir_driver);
242
243MODULE_DESCRIPTION("Amlogic Meson IR remote receiver driver");
244MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
245MODULE_LICENSE("GPL v2");