Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Amlogic Meson IR remote receiver |
| 3 | * |
| 4 | * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * You should have received a copy of the GNU General Public License |
| 11 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_platform.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/spinlock.h> |
Heiner Kallweit | e7a937b | 2017-04-12 16:30:48 -0300 | [diff] [blame] | 22 | #include <linux/bitfield.h> |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 23 | |
| 24 | #include <media/rc-core.h> |
| 25 | |
| 26 | #define DRIVER_NAME "meson-ir" |
| 27 | |
Neil Armstrong | 6edf27e | 2016-08-20 11:54:22 +0200 | [diff] [blame] | 28 | /* valid on all Meson platforms */ |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 29 | #define IR_DEC_LDR_ACTIVE 0x00 |
| 30 | #define IR_DEC_LDR_IDLE 0x04 |
| 31 | #define IR_DEC_LDR_REPEAT 0x08 |
| 32 | #define IR_DEC_BIT_0 0x0c |
| 33 | #define IR_DEC_REG0 0x10 |
| 34 | #define IR_DEC_FRAME 0x14 |
| 35 | #define IR_DEC_STATUS 0x18 |
| 36 | #define IR_DEC_REG1 0x1c |
Neil Armstrong | 6edf27e | 2016-08-20 11:54:22 +0200 | [diff] [blame] | 37 | /* only available on Meson 8b and newer */ |
| 38 | #define IR_DEC_REG2 0x20 |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 39 | |
Heiner Kallweit | e7a937b | 2017-04-12 16:30:48 -0300 | [diff] [blame] | 40 | #define REG0_RATE_MASK GENMASK(11, 0) |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 41 | |
Neil Armstrong | 6edf27e | 2016-08-20 11:54:22 +0200 | [diff] [blame] | 42 | #define DECODE_MODE_NEC 0x0 |
| 43 | #define DECODE_MODE_RAW 0x2 |
| 44 | |
| 45 | /* Meson 6b uses REG1 to configure the mode */ |
| 46 | #define REG1_MODE_MASK GENMASK(8, 7) |
| 47 | #define REG1_MODE_SHIFT 7 |
| 48 | |
| 49 | /* Meson 8b / GXBB use REG2 to configure the mode */ |
| 50 | #define REG2_MODE_MASK GENMASK(3, 0) |
| 51 | #define REG2_MODE_SHIFT 0 |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 52 | |
Heiner Kallweit | e7a937b | 2017-04-12 16:30:48 -0300 | [diff] [blame] | 53 | #define REG1_TIME_IV_MASK GENMASK(28, 16) |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 54 | |
Heiner Kallweit | e7a937b | 2017-04-12 16:30:48 -0300 | [diff] [blame] | 55 | #define REG1_IRQSEL_MASK GENMASK(3, 2) |
| 56 | #define REG1_IRQSEL_NEC_MODE 0 |
| 57 | #define REG1_IRQSEL_RISE_FALL 1 |
| 58 | #define REG1_IRQSEL_FALL 2 |
| 59 | #define REG1_IRQSEL_RISE 3 |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 60 | |
| 61 | #define REG1_RESET BIT(0) |
| 62 | #define REG1_ENABLE BIT(15) |
| 63 | |
| 64 | #define STATUS_IR_DEC_IN BIT(8) |
| 65 | |
| 66 | #define MESON_TRATE 10 /* us */ |
| 67 | |
| 68 | struct meson_ir { |
| 69 | void __iomem *reg; |
| 70 | struct rc_dev *rc; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 71 | spinlock_t lock; |
| 72 | }; |
| 73 | |
| 74 | static void meson_ir_set_mask(struct meson_ir *ir, unsigned int reg, |
| 75 | u32 mask, u32 value) |
| 76 | { |
| 77 | u32 data; |
| 78 | |
| 79 | data = readl(ir->reg + reg); |
| 80 | data &= ~mask; |
| 81 | data |= (value & mask); |
| 82 | writel(data, ir->reg + reg); |
| 83 | } |
| 84 | |
| 85 | static irqreturn_t meson_ir_irq(int irqno, void *dev_id) |
| 86 | { |
| 87 | struct meson_ir *ir = dev_id; |
Heiner Kallweit | 137edc0 | 2017-04-12 16:33:57 -0300 | [diff] [blame] | 88 | u32 duration, status; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 89 | DEFINE_IR_RAW_EVENT(rawir); |
| 90 | |
| 91 | spin_lock(&ir->lock); |
| 92 | |
Heiner Kallweit | 137edc0 | 2017-04-12 16:33:57 -0300 | [diff] [blame] | 93 | duration = readl_relaxed(ir->reg + IR_DEC_REG1); |
Heiner Kallweit | e7a937b | 2017-04-12 16:30:48 -0300 | [diff] [blame] | 94 | duration = FIELD_GET(REG1_TIME_IV_MASK, duration); |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 95 | rawir.duration = US_TO_NS(duration * MESON_TRATE); |
| 96 | |
Heiner Kallweit | 137edc0 | 2017-04-12 16:33:57 -0300 | [diff] [blame] | 97 | status = readl_relaxed(ir->reg + IR_DEC_STATUS); |
| 98 | rawir.pulse = !!(status & STATUS_IR_DEC_IN); |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 99 | |
Jonas Karlman | 48b0a69 | 2017-04-25 04:40:48 -0300 | [diff] [blame] | 100 | ir_raw_event_store(ir->rc, &rawir); |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 101 | ir_raw_event_handle(ir->rc); |
| 102 | |
| 103 | spin_unlock(&ir->lock); |
| 104 | |
| 105 | return IRQ_HANDLED; |
| 106 | } |
| 107 | |
| 108 | static int meson_ir_probe(struct platform_device *pdev) |
| 109 | { |
| 110 | struct device *dev = &pdev->dev; |
| 111 | struct device_node *node = dev->of_node; |
| 112 | struct resource *res; |
| 113 | const char *map_name; |
| 114 | struct meson_ir *ir; |
Heiner Kallweit | 1ffc931 | 2017-04-12 16:28:42 -0300 | [diff] [blame] | 115 | int irq, ret; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 116 | |
| 117 | ir = devm_kzalloc(dev, sizeof(struct meson_ir), GFP_KERNEL); |
| 118 | if (!ir) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 122 | ir->reg = devm_ioremap_resource(dev, res); |
| 123 | if (IS_ERR(ir->reg)) { |
| 124 | dev_err(dev, "failed to map registers\n"); |
| 125 | return PTR_ERR(ir->reg); |
| 126 | } |
| 127 | |
Heiner Kallweit | 1ffc931 | 2017-04-12 16:28:42 -0300 | [diff] [blame] | 128 | irq = platform_get_irq(pdev, 0); |
| 129 | if (irq < 0) { |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 130 | dev_err(dev, "no irq resource\n"); |
Heiner Kallweit | 1ffc931 | 2017-04-12 16:28:42 -0300 | [diff] [blame] | 131 | return irq; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 132 | } |
| 133 | |
Heiner Kallweit | 705aa57 | 2017-04-12 16:32:35 -0300 | [diff] [blame] | 134 | ir->rc = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW); |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 135 | if (!ir->rc) { |
| 136 | dev_err(dev, "failed to allocate rc device\n"); |
| 137 | return -ENOMEM; |
| 138 | } |
| 139 | |
| 140 | ir->rc->priv = ir; |
Sean Young | 518f4b2 | 2017-07-01 12:13:19 -0400 | [diff] [blame] | 141 | ir->rc->device_name = DRIVER_NAME; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 142 | ir->rc->input_phys = DRIVER_NAME "/input0"; |
| 143 | ir->rc->input_id.bustype = BUS_HOST; |
| 144 | map_name = of_get_property(node, "linux,rc-map-name", NULL); |
| 145 | ir->rc->map_name = map_name ? map_name : RC_MAP_EMPTY; |
Sean Young | 6d741bf | 2017-08-07 16:20:58 -0400 | [diff] [blame] | 146 | ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 147 | ir->rc->rx_resolution = US_TO_NS(MESON_TRATE); |
| 148 | ir->rc->timeout = MS_TO_NS(200); |
| 149 | ir->rc->driver_name = DRIVER_NAME; |
| 150 | |
| 151 | spin_lock_init(&ir->lock); |
| 152 | platform_set_drvdata(pdev, ir); |
| 153 | |
Heiner Kallweit | 705aa57 | 2017-04-12 16:32:35 -0300 | [diff] [blame] | 154 | ret = devm_rc_register_device(dev, ir->rc); |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 155 | if (ret) { |
| 156 | dev_err(dev, "failed to register rc device\n"); |
Heiner Kallweit | 705aa57 | 2017-04-12 16:32:35 -0300 | [diff] [blame] | 157 | return ret; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 158 | } |
| 159 | |
Heiner Kallweit | 611ee55 | 2017-04-12 16:34:50 -0300 | [diff] [blame] | 160 | ret = devm_request_irq(dev, irq, meson_ir_irq, 0, NULL, ir); |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 161 | if (ret) { |
| 162 | dev_err(dev, "failed to request irq\n"); |
Heiner Kallweit | 705aa57 | 2017-04-12 16:32:35 -0300 | [diff] [blame] | 163 | return ret; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* Reset the decoder */ |
| 167 | meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, REG1_RESET); |
| 168 | meson_ir_set_mask(ir, IR_DEC_REG1, REG1_RESET, 0); |
Neil Armstrong | 6edf27e | 2016-08-20 11:54:22 +0200 | [diff] [blame] | 169 | |
| 170 | /* Set general operation mode (= raw/software decoding) */ |
| 171 | if (of_device_is_compatible(node, "amlogic,meson6-ir")) |
| 172 | meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK, |
Heiner Kallweit | e7a937b | 2017-04-12 16:30:48 -0300 | [diff] [blame] | 173 | FIELD_PREP(REG1_MODE_MASK, DECODE_MODE_RAW)); |
Neil Armstrong | 6edf27e | 2016-08-20 11:54:22 +0200 | [diff] [blame] | 174 | else |
| 175 | meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK, |
Heiner Kallweit | e7a937b | 2017-04-12 16:30:48 -0300 | [diff] [blame] | 176 | FIELD_PREP(REG2_MODE_MASK, DECODE_MODE_RAW)); |
Neil Armstrong | 6edf27e | 2016-08-20 11:54:22 +0200 | [diff] [blame] | 177 | |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 178 | /* Set rate */ |
| 179 | meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, MESON_TRATE - 1); |
| 180 | /* IRQ on rising and falling edges */ |
| 181 | meson_ir_set_mask(ir, IR_DEC_REG1, REG1_IRQSEL_MASK, |
Heiner Kallweit | e7a937b | 2017-04-12 16:30:48 -0300 | [diff] [blame] | 182 | FIELD_PREP(REG1_IRQSEL_MASK, REG1_IRQSEL_RISE_FALL)); |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 183 | /* Enable the decoder */ |
| 184 | meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, REG1_ENABLE); |
| 185 | |
| 186 | dev_info(dev, "receiver initialized\n"); |
| 187 | |
| 188 | return 0; |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int meson_ir_remove(struct platform_device *pdev) |
| 192 | { |
| 193 | struct meson_ir *ir = platform_get_drvdata(pdev); |
| 194 | unsigned long flags; |
| 195 | |
| 196 | /* Disable the decoder */ |
| 197 | spin_lock_irqsave(&ir->lock, flags); |
| 198 | meson_ir_set_mask(ir, IR_DEC_REG1, REG1_ENABLE, 0); |
| 199 | spin_unlock_irqrestore(&ir->lock, flags); |
| 200 | |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Alex Deryskyba | 2aa1bd1 | 2017-04-25 04:41:09 -0300 | [diff] [blame] | 204 | static void meson_ir_shutdown(struct platform_device *pdev) |
| 205 | { |
| 206 | struct device *dev = &pdev->dev; |
| 207 | struct device_node *node = dev->of_node; |
| 208 | struct meson_ir *ir = platform_get_drvdata(pdev); |
| 209 | unsigned long flags; |
| 210 | |
| 211 | spin_lock_irqsave(&ir->lock, flags); |
| 212 | |
| 213 | /* |
| 214 | * Set operation mode to NEC/hardware decoding to give |
| 215 | * bootloader a chance to power the system back on |
| 216 | */ |
| 217 | if (of_device_is_compatible(node, "amlogic,meson6-ir")) |
| 218 | meson_ir_set_mask(ir, IR_DEC_REG1, REG1_MODE_MASK, |
| 219 | DECODE_MODE_NEC << REG1_MODE_SHIFT); |
| 220 | else |
| 221 | meson_ir_set_mask(ir, IR_DEC_REG2, REG2_MODE_MASK, |
| 222 | DECODE_MODE_NEC << REG2_MODE_SHIFT); |
| 223 | |
| 224 | /* Set rate to default value */ |
| 225 | meson_ir_set_mask(ir, IR_DEC_REG0, REG0_RATE_MASK, 0x13); |
| 226 | |
| 227 | spin_unlock_irqrestore(&ir->lock, flags); |
| 228 | } |
| 229 | |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 230 | static const struct of_device_id meson_ir_match[] = { |
| 231 | { .compatible = "amlogic,meson6-ir" }, |
Neil Armstrong | 6edf27e | 2016-08-20 11:54:22 +0200 | [diff] [blame] | 232 | { .compatible = "amlogic,meson8b-ir" }, |
| 233 | { .compatible = "amlogic,meson-gxbb-ir" }, |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 234 | { }, |
| 235 | }; |
Javier Martinez Canillas | 5bb50fe | 2016-10-17 13:44:10 -0200 | [diff] [blame] | 236 | MODULE_DEVICE_TABLE(of, meson_ir_match); |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 237 | |
| 238 | static struct platform_driver meson_ir_driver = { |
| 239 | .probe = meson_ir_probe, |
| 240 | .remove = meson_ir_remove, |
Alex Deryskyba | 2aa1bd1 | 2017-04-25 04:41:09 -0300 | [diff] [blame] | 241 | .shutdown = meson_ir_shutdown, |
Beniamino Galvani | 12ddbad | 2014-11-18 17:22:34 -0300 | [diff] [blame] | 242 | .driver = { |
| 243 | .name = DRIVER_NAME, |
| 244 | .of_match_table = meson_ir_match, |
| 245 | }, |
| 246 | }; |
| 247 | |
| 248 | module_platform_driver(meson_ir_driver); |
| 249 | |
| 250 | MODULE_DESCRIPTION("Amlogic Meson IR remote receiver driver"); |
| 251 | MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>"); |
| 252 | MODULE_LICENSE("GPL v2"); |