linux/drivers/media/rc/tango-ir.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
   4 */
   5
   6#include <linux/input.h>
   7#include <linux/module.h>
   8#include <linux/platform_device.h>
   9#include <linux/interrupt.h>
  10#include <linux/io.h>
  11#include <linux/clk.h>
  12#include <linux/of.h>
  13#include <media/rc-core.h>
  14
  15#define DRIVER_NAME "tango-ir"
  16
  17#define IR_NEC_CTRL     0x00
  18#define IR_NEC_DATA     0x04
  19#define IR_CTRL         0x08
  20#define IR_RC5_CLK_DIV  0x0c
  21#define IR_RC5_DATA     0x10
  22#define IR_INT          0x14
  23
  24#define NEC_TIME_BASE   560
  25#define RC5_TIME_BASE   1778
  26
  27#define RC6_CTRL        0x00
  28#define RC6_CLKDIV      0x04
  29#define RC6_DATA0       0x08
  30#define RC6_DATA1       0x0c
  31#define RC6_DATA2       0x10
  32#define RC6_DATA3       0x14
  33#define RC6_DATA4       0x18
  34
  35#define RC6_CARRIER     36000
  36#define RC6_TIME_BASE   16
  37
  38#define NEC_CAP(n)      ((n) << 24)
  39#define GPIO_SEL(n)     ((n) << 16)
  40#define DISABLE_NEC     (BIT(4) | BIT(8))
  41#define ENABLE_RC5      (BIT(0) | BIT(9))
  42#define ENABLE_RC6      (BIT(0) | BIT(7))
  43#define ACK_IR_INT      (BIT(0) | BIT(1))
  44#define ACK_RC6_INT     (BIT(31))
  45
  46#define NEC_ANY (RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX | RC_PROTO_BIT_NEC32)
  47
  48struct tango_ir {
  49        void __iomem *rc5_base;
  50        void __iomem *rc6_base;
  51        struct rc_dev *rc;
  52        struct clk *clk;
  53};
  54
  55static void tango_ir_handle_nec(struct tango_ir *ir)
  56{
  57        u32 v, code;
  58        enum rc_proto proto;
  59
  60        v = readl_relaxed(ir->rc5_base + IR_NEC_DATA);
  61        if (!v) {
  62                rc_repeat(ir->rc);
  63                return;
  64        }
  65
  66        code = ir_nec_bytes_to_scancode(v, v >> 8, v >> 16, v >> 24, &proto);
  67        rc_keydown(ir->rc, proto, code, 0);
  68}
  69
  70static void tango_ir_handle_rc5(struct tango_ir *ir)
  71{
  72        u32 data, field, toggle, addr, cmd, code;
  73
  74        data = readl_relaxed(ir->rc5_base + IR_RC5_DATA);
  75        if (data & BIT(31))
  76                return;
  77
  78        field = data >> 12 & 1;
  79        toggle = data >> 11 & 1;
  80        addr = data >> 6 & 0x1f;
  81        cmd = (data & 0x3f) | (field ^ 1) << 6;
  82
  83        code = RC_SCANCODE_RC5(addr, cmd);
  84        rc_keydown(ir->rc, RC_PROTO_RC5, code, toggle);
  85}
  86
  87static void tango_ir_handle_rc6(struct tango_ir *ir)
  88{
  89        u32 data0, data1, toggle, mode, addr, cmd, code;
  90
  91        data0 = readl_relaxed(ir->rc6_base + RC6_DATA0);
  92        data1 = readl_relaxed(ir->rc6_base + RC6_DATA1);
  93
  94        mode = data0 >> 1 & 7;
  95        if (mode != 0)
  96                return;
  97
  98        toggle = data0 & 1;
  99        addr = data0 >> 16;
 100        cmd = data1;
 101
 102        code = RC_SCANCODE_RC6_0(addr, cmd);
 103        rc_keydown(ir->rc, RC_PROTO_RC6_0, code, toggle);
 104}
 105
 106static irqreturn_t tango_ir_irq(int irq, void *dev_id)
 107{
 108        struct tango_ir *ir = dev_id;
 109        unsigned int rc5_stat;
 110        unsigned int rc6_stat;
 111
 112        rc5_stat = readl_relaxed(ir->rc5_base + IR_INT);
 113        writel_relaxed(rc5_stat, ir->rc5_base + IR_INT);
 114
 115        rc6_stat = readl_relaxed(ir->rc6_base + RC6_CTRL);
 116        writel_relaxed(rc6_stat, ir->rc6_base + RC6_CTRL);
 117
 118        if (!(rc5_stat & 3) && !(rc6_stat & BIT(31)))
 119                return IRQ_NONE;
 120
 121        if (rc5_stat & BIT(0))
 122                tango_ir_handle_rc5(ir);
 123
 124        if (rc5_stat & BIT(1))
 125                tango_ir_handle_nec(ir);
 126
 127        if (rc6_stat & BIT(31))
 128                tango_ir_handle_rc6(ir);
 129
 130        return IRQ_HANDLED;
 131}
 132
 133static int tango_change_protocol(struct rc_dev *dev, u64 *rc_type)
 134{
 135        struct tango_ir *ir = dev->priv;
 136        u32 rc5_ctrl = DISABLE_NEC;
 137        u32 rc6_ctrl = 0;
 138
 139        if (*rc_type & NEC_ANY)
 140                rc5_ctrl = 0;
 141
 142        if (*rc_type & RC_PROTO_BIT_RC5)
 143                rc5_ctrl |= ENABLE_RC5;
 144
 145        if (*rc_type & RC_PROTO_BIT_RC6_0)
 146                rc6_ctrl = ENABLE_RC6;
 147
 148        writel_relaxed(rc5_ctrl, ir->rc5_base + IR_CTRL);
 149        writel_relaxed(rc6_ctrl, ir->rc6_base + RC6_CTRL);
 150
 151        return 0;
 152}
 153
 154static int tango_ir_probe(struct platform_device *pdev)
 155{
 156        const char *map_name = RC_MAP_TANGO;
 157        struct device *dev = &pdev->dev;
 158        struct rc_dev *rc;
 159        struct tango_ir *ir;
 160        u64 clkrate, clkdiv;
 161        int irq, err;
 162        u32 val;
 163
 164        irq = platform_get_irq(pdev, 0);
 165        if (irq <= 0)
 166                return -EINVAL;
 167
 168        ir = devm_kzalloc(dev, sizeof(*ir), GFP_KERNEL);
 169        if (!ir)
 170                return -ENOMEM;
 171
 172        ir->rc5_base = devm_platform_ioremap_resource(pdev, 0);
 173        if (IS_ERR(ir->rc5_base))
 174                return PTR_ERR(ir->rc5_base);
 175
 176        ir->rc6_base = devm_platform_ioremap_resource(pdev, 1);
 177        if (IS_ERR(ir->rc6_base))
 178                return PTR_ERR(ir->rc6_base);
 179
 180        ir->clk = devm_clk_get(dev, NULL);
 181        if (IS_ERR(ir->clk))
 182                return PTR_ERR(ir->clk);
 183
 184        rc = devm_rc_allocate_device(dev, RC_DRIVER_SCANCODE);
 185        if (!rc)
 186                return -ENOMEM;
 187
 188        of_property_read_string(dev->of_node, "linux,rc-map-name", &map_name);
 189
 190        rc->device_name = DRIVER_NAME;
 191        rc->driver_name = DRIVER_NAME;
 192        rc->input_phys = DRIVER_NAME "/input0";
 193        rc->map_name = map_name;
 194        rc->allowed_protocols = NEC_ANY | RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_0;
 195        rc->change_protocol = tango_change_protocol;
 196        rc->priv = ir;
 197        ir->rc = rc;
 198
 199        err = clk_prepare_enable(ir->clk);
 200        if (err)
 201                return err;
 202
 203        clkrate = clk_get_rate(ir->clk);
 204
 205        clkdiv = clkrate * NEC_TIME_BASE;
 206        do_div(clkdiv, 1000000);
 207
 208        val = NEC_CAP(31) | GPIO_SEL(12) | clkdiv;
 209        writel_relaxed(val, ir->rc5_base + IR_NEC_CTRL);
 210
 211        clkdiv = clkrate * RC5_TIME_BASE;
 212        do_div(clkdiv, 1000000);
 213
 214        writel_relaxed(DISABLE_NEC, ir->rc5_base + IR_CTRL);
 215        writel_relaxed(clkdiv, ir->rc5_base + IR_RC5_CLK_DIV);
 216        writel_relaxed(ACK_IR_INT, ir->rc5_base + IR_INT);
 217
 218        clkdiv = clkrate * RC6_TIME_BASE;
 219        do_div(clkdiv, RC6_CARRIER);
 220
 221        writel_relaxed(ACK_RC6_INT, ir->rc6_base + RC6_CTRL);
 222        writel_relaxed((clkdiv >> 2) << 18 | clkdiv, ir->rc6_base + RC6_CLKDIV);
 223
 224        err = devm_request_irq(dev, irq, tango_ir_irq, IRQF_SHARED,
 225                               dev_name(dev), ir);
 226        if (err)
 227                goto err_clk;
 228
 229        err = devm_rc_register_device(dev, rc);
 230        if (err)
 231                goto err_clk;
 232
 233        platform_set_drvdata(pdev, ir);
 234        return 0;
 235
 236err_clk:
 237        clk_disable_unprepare(ir->clk);
 238        return err;
 239}
 240
 241static int tango_ir_remove(struct platform_device *pdev)
 242{
 243        struct tango_ir *ir = platform_get_drvdata(pdev);
 244
 245        clk_disable_unprepare(ir->clk);
 246        return 0;
 247}
 248
 249static const struct of_device_id tango_ir_dt_ids[] = {
 250        { .compatible = "sigma,smp8642-ir" },
 251        { }
 252};
 253MODULE_DEVICE_TABLE(of, tango_ir_dt_ids);
 254
 255static struct platform_driver tango_ir_driver = {
 256        .probe  = tango_ir_probe,
 257        .remove = tango_ir_remove,
 258        .driver = {
 259                .name           = DRIVER_NAME,
 260                .of_match_table = tango_ir_dt_ids,
 261        },
 262};
 263module_platform_driver(tango_ir_driver);
 264
 265MODULE_DESCRIPTION("SMP86xx IR decoder driver");
 266MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
 267MODULE_LICENSE("GPL");
 268