Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * dm355evm_keys.c - support buttons and IR remote on DM355 EVM board
0004  *
0005  * Copyright (c) 2008 by David Brownell
0006  */
0007 #include <linux/kernel.h>
0008 #include <linux/slab.h>
0009 #include <linux/input.h>
0010 #include <linux/input/sparse-keymap.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/interrupt.h>
0013 
0014 #include <linux/mfd/dm355evm_msp.h>
0015 #include <linux/module.h>
0016 
0017 
0018 /*
0019  * The MSP430 firmware on the DM355 EVM monitors on-board pushbuttons
0020  * and an IR receptor used for the remote control.  When any key is
0021  * pressed, or its autorepeat kicks in, an event is sent.  This driver
0022  * read those events from the small (32 event) queue and reports them.
0023  *
0024  * Note that physically there can only be one of these devices.
0025  *
0026  * This driver was tested with firmware revision A4.
0027  */
0028 struct dm355evm_keys {
0029     struct input_dev    *input;
0030     struct device       *dev;
0031 };
0032 
0033 /* These initial keycodes can be remapped */
0034 static const struct key_entry dm355evm_keys[] = {
0035     /*
0036      * Pushbuttons on the EVM board ... note that the labels for these
0037      * are SW10/SW11/etc on the PC board.  The left/right orientation
0038      * comes only from the firmware's documentation, and presumes the
0039      * power connector is immediately in front of you and the IR sensor
0040      * is to the right.  (That is, rotate the board counter-clockwise
0041      * by 90 degrees from the SW10/etc and "DM355 EVM" labels.)
0042      */
0043     { KE_KEY, 0x00d8, { KEY_OK } },     /* SW12 */
0044     { KE_KEY, 0x00b8, { KEY_UP } },     /* SW13 */
0045     { KE_KEY, 0x00e8, { KEY_DOWN } },   /* SW11 */
0046     { KE_KEY, 0x0078, { KEY_LEFT } },   /* SW14 */
0047     { KE_KEY, 0x00f0, { KEY_RIGHT } },  /* SW10 */
0048 
0049     /*
0050      * IR buttons ... codes assigned to match the universal remote
0051      * provided with the EVM (Philips PM4S) using DVD code 0020.
0052      *
0053      * These event codes match firmware documentation, but other
0054      * remote controls could easily send more RC5-encoded events.
0055      * The PM4S manual was used in several cases to help select
0056      * a keycode reflecting the intended usage.
0057      *
0058      * RC5 codes are 14 bits, with two start bits (0x3 prefix)
0059      * and a toggle bit (masked out below).
0060      */
0061     { KE_KEY, 0x300c, { KEY_POWER } },  /* NOTE: docs omit this */
0062     { KE_KEY, 0x3000, { KEY_NUMERIC_0 } },
0063     { KE_KEY, 0x3001, { KEY_NUMERIC_1 } },
0064     { KE_KEY, 0x3002, { KEY_NUMERIC_2 } },
0065     { KE_KEY, 0x3003, { KEY_NUMERIC_3 } },
0066     { KE_KEY, 0x3004, { KEY_NUMERIC_4 } },
0067     { KE_KEY, 0x3005, { KEY_NUMERIC_5 } },
0068     { KE_KEY, 0x3006, { KEY_NUMERIC_6 } },
0069     { KE_KEY, 0x3007, { KEY_NUMERIC_7 } },
0070     { KE_KEY, 0x3008, { KEY_NUMERIC_8 } },
0071     { KE_KEY, 0x3009, { KEY_NUMERIC_9 } },
0072     { KE_KEY, 0x3022, { KEY_ENTER } },
0073     { KE_KEY, 0x30ec, { KEY_MODE } },   /* "tv/vcr/..." */
0074     { KE_KEY, 0x300f, { KEY_SELECT } }, /* "info" */
0075     { KE_KEY, 0x3020, { KEY_CHANNELUP } },  /* "up" */
0076     { KE_KEY, 0x302e, { KEY_MENU } },   /* "in/out" */
0077     { KE_KEY, 0x3011, { KEY_VOLUMEDOWN } }, /* "left" */
0078     { KE_KEY, 0x300d, { KEY_MUTE } },   /* "ok" */
0079     { KE_KEY, 0x3010, { KEY_VOLUMEUP } },   /* "right" */
0080     { KE_KEY, 0x301e, { KEY_SUBTITLE } },   /* "cc" */
0081     { KE_KEY, 0x3021, { KEY_CHANNELDOWN } },/* "down" */
0082     { KE_KEY, 0x3022, { KEY_PREVIOUS } },
0083     { KE_KEY, 0x3026, { KEY_SLEEP } },
0084     { KE_KEY, 0x3172, { KEY_REWIND } }, /* NOTE: docs wrongly say 0x30ca */
0085     { KE_KEY, 0x3175, { KEY_PLAY } },
0086     { KE_KEY, 0x3174, { KEY_FASTFORWARD } },
0087     { KE_KEY, 0x3177, { KEY_RECORD } },
0088     { KE_KEY, 0x3176, { KEY_STOP } },
0089     { KE_KEY, 0x3169, { KEY_PAUSE } },
0090 };
0091 
0092 /*
0093  * Because we communicate with the MSP430 using I2C, and all I2C calls
0094  * in Linux sleep, we use a threaded IRQ handler.  The IRQ itself is
0095  * active low, but we go through the GPIO controller so we can trigger
0096  * on falling edges and not worry about enabling/disabling the IRQ in
0097  * the keypress handling path.
0098  */
0099 static irqreturn_t dm355evm_keys_irq(int irq, void *_keys)
0100 {
0101     static u16 last_event;
0102     struct dm355evm_keys *keys = _keys;
0103     const struct key_entry *ke;
0104     unsigned int keycode;
0105     int status;
0106     u16 event;
0107 
0108     /* For simplicity we ignore INPUT_COUNT and just read
0109      * events until we get the "queue empty" indicator.
0110      * Reading INPUT_LOW decrements the count.
0111      */
0112     for (;;) {
0113         status = dm355evm_msp_read(DM355EVM_MSP_INPUT_HIGH);
0114         if (status < 0) {
0115             dev_dbg(keys->dev, "input high err %d\n",
0116                     status);
0117             break;
0118         }
0119         event = status << 8;
0120 
0121         status = dm355evm_msp_read(DM355EVM_MSP_INPUT_LOW);
0122         if (status < 0) {
0123             dev_dbg(keys->dev, "input low err %d\n",
0124                     status);
0125             break;
0126         }
0127         event |= status;
0128         if (event == 0xdead)
0129             break;
0130 
0131         /* Press and release a button:  two events, same code.
0132          * Press and hold (autorepeat), then release: N events
0133          * (N > 2), same code.  For RC5 buttons the toggle bits
0134          * distinguish (for example) "1-autorepeat" from "1 1";
0135          * but PCB buttons don't support that bit.
0136          *
0137          * So we must synthesize release events.  We do that by
0138          * mapping events to a press/release event pair; then
0139          * to avoid adding extra events, skip the second event
0140          * of each pair.
0141          */
0142         if (event == last_event) {
0143             last_event = 0;
0144             continue;
0145         }
0146         last_event = event;
0147 
0148         /* ignore the RC5 toggle bit */
0149         event &= ~0x0800;
0150 
0151         /* find the key, or report it as unknown */
0152         ke = sparse_keymap_entry_from_scancode(keys->input, event);
0153         keycode = ke ? ke->keycode : KEY_UNKNOWN;
0154         dev_dbg(keys->dev,
0155             "input event 0x%04x--> keycode %d\n",
0156             event, keycode);
0157 
0158         /* report press + release */
0159         input_report_key(keys->input, keycode, 1);
0160         input_sync(keys->input);
0161         input_report_key(keys->input, keycode, 0);
0162         input_sync(keys->input);
0163     }
0164 
0165     return IRQ_HANDLED;
0166 }
0167 
0168 /*----------------------------------------------------------------------*/
0169 
0170 static int dm355evm_keys_probe(struct platform_device *pdev)
0171 {
0172     struct dm355evm_keys    *keys;
0173     struct input_dev    *input;
0174     int         irq;
0175     int         error;
0176 
0177     keys = devm_kzalloc(&pdev->dev, sizeof (*keys), GFP_KERNEL);
0178     if (!keys)
0179         return -ENOMEM;
0180 
0181     input = devm_input_allocate_device(&pdev->dev);
0182     if (!input)
0183         return -ENOMEM;
0184 
0185     keys->dev = &pdev->dev;
0186     keys->input = input;
0187 
0188     input->name = "DM355 EVM Controls";
0189     input->phys = "dm355evm/input0";
0190 
0191     input->id.bustype = BUS_I2C;
0192     input->id.product = 0x0355;
0193     input->id.version = dm355evm_msp_read(DM355EVM_MSP_FIRMREV);
0194 
0195     error = sparse_keymap_setup(input, dm355evm_keys, NULL);
0196     if (error)
0197         return error;
0198 
0199     /* REVISIT:  flush the event queue? */
0200 
0201     /* set up "threaded IRQ handler" */
0202     irq = platform_get_irq(pdev, 0);
0203     if (irq < 0)
0204         return irq;
0205 
0206     error = devm_request_threaded_irq(&pdev->dev, irq,
0207                       NULL, dm355evm_keys_irq,
0208                       IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
0209                       dev_name(&pdev->dev), keys);
0210     if (error)
0211         return error;
0212 
0213     /* register */
0214     error = input_register_device(input);
0215     if (error)
0216         return error;
0217 
0218     return 0;
0219 }
0220 
0221 /* REVISIT:  add suspend/resume when DaVinci supports it.  The IRQ should
0222  * be able to wake up the system.  When device_may_wakeup(&pdev->dev), call
0223  * enable_irq_wake() on suspend, and disable_irq_wake() on resume.
0224  */
0225 
0226 /*
0227  * I2C is used to talk to the MSP430, but this platform device is
0228  * exposed by an MFD driver that manages I2C communications.
0229  */
0230 static struct platform_driver dm355evm_keys_driver = {
0231     .probe      = dm355evm_keys_probe,
0232     .driver     = {
0233         .name   = "dm355evm_keys",
0234     },
0235 };
0236 module_platform_driver(dm355evm_keys_driver);
0237 
0238 MODULE_LICENSE("GPL");