1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun // cx231xx IR glue driver
3*4882a593Smuzhiyun //
4*4882a593Smuzhiyun // Copyright (c) 2010 Mauro Carvalho Chehab <mchehab@kernel.org>
5*4882a593Smuzhiyun //
6*4882a593Smuzhiyun // Polaris (cx231xx) has its support for IR's with a design close to MCE.
7*4882a593Smuzhiyun // however, a few designs are using an external I2C chip for IR, instead
8*4882a593Smuzhiyun // of using the one provided by the chip.
9*4882a593Smuzhiyun // This driver provides support for those extra devices
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include "cx231xx.h"
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/bitrev.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define MODULE_NAME "cx231xx-input"
16*4882a593Smuzhiyun
get_key_isdbt(struct IR_i2c * ir,enum rc_proto * protocol,u32 * pscancode,u8 * toggle)17*4882a593Smuzhiyun static int get_key_isdbt(struct IR_i2c *ir, enum rc_proto *protocol,
18*4882a593Smuzhiyun u32 *pscancode, u8 *toggle)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun int rc;
21*4882a593Smuzhiyun u8 cmd, scancode;
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun dev_dbg(&ir->rc->dev, "%s\n", __func__);
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /* poll IR chip */
26*4882a593Smuzhiyun rc = i2c_master_recv(ir->c, &cmd, 1);
27*4882a593Smuzhiyun if (rc < 0)
28*4882a593Smuzhiyun return rc;
29*4882a593Smuzhiyun if (rc != 1)
30*4882a593Smuzhiyun return -EIO;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /* it seems that 0xFE indicates that a button is still hold
33*4882a593Smuzhiyun down, while 0xff indicates that no button is hold
34*4882a593Smuzhiyun down. 0xfe sequences are sometimes interrupted by 0xFF */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun if (cmd == 0xff)
37*4882a593Smuzhiyun return 0;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun scancode = bitrev8(cmd);
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun dev_dbg(&ir->rc->dev, "cmd %02x, scan = %02x\n", cmd, scancode);
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun *protocol = RC_PROTO_OTHER;
44*4882a593Smuzhiyun *pscancode = scancode;
45*4882a593Smuzhiyun *toggle = 0;
46*4882a593Smuzhiyun return 1;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
cx231xx_ir_init(struct cx231xx * dev)49*4882a593Smuzhiyun int cx231xx_ir_init(struct cx231xx *dev)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun struct i2c_board_info info;
52*4882a593Smuzhiyun u8 ir_i2c_bus;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun dev_dbg(dev->dev, "%s\n", __func__);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* Only initialize if a rc keycode map is defined */
57*4882a593Smuzhiyun if (!cx231xx_boards[dev->model].rc_map_name)
58*4882a593Smuzhiyun return -ENODEV;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun request_module("ir-kbd-i2c");
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun memset(&info, 0, sizeof(struct i2c_board_info));
63*4882a593Smuzhiyun memset(&dev->init_data, 0, sizeof(dev->init_data));
64*4882a593Smuzhiyun dev->init_data.rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
65*4882a593Smuzhiyun if (!dev->init_data.rc_dev)
66*4882a593Smuzhiyun return -ENOMEM;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun dev->init_data.name = cx231xx_boards[dev->model].name;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun strscpy(info.type, "ir_video", I2C_NAME_SIZE);
71*4882a593Smuzhiyun info.platform_data = &dev->init_data;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun * Board-dependent values
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * For now, there's just one type of hardware design using
77*4882a593Smuzhiyun * an i2c device.
78*4882a593Smuzhiyun */
79*4882a593Smuzhiyun dev->init_data.get_key = get_key_isdbt;
80*4882a593Smuzhiyun dev->init_data.ir_codes = cx231xx_boards[dev->model].rc_map_name;
81*4882a593Smuzhiyun /* The i2c micro-controller only outputs the cmd part of NEC protocol */
82*4882a593Smuzhiyun dev->init_data.rc_dev->scancode_mask = 0xff;
83*4882a593Smuzhiyun dev->init_data.rc_dev->driver_name = "cx231xx";
84*4882a593Smuzhiyun dev->init_data.type = RC_PROTO_BIT_NEC;
85*4882a593Smuzhiyun info.addr = 0x30;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Load and bind ir-kbd-i2c */
88*4882a593Smuzhiyun ir_i2c_bus = cx231xx_boards[dev->model].ir_i2c_master;
89*4882a593Smuzhiyun dev_dbg(dev->dev, "Trying to bind ir at bus %d, addr 0x%02x\n",
90*4882a593Smuzhiyun ir_i2c_bus, info.addr);
91*4882a593Smuzhiyun dev->ir_i2c_client = i2c_new_client_device(
92*4882a593Smuzhiyun cx231xx_get_i2c_adap(dev, ir_i2c_bus), &info);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun return 0;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
cx231xx_ir_exit(struct cx231xx * dev)97*4882a593Smuzhiyun void cx231xx_ir_exit(struct cx231xx *dev)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun i2c_unregister_device(dev->ir_i2c_client);
100*4882a593Smuzhiyun dev->ir_i2c_client = NULL;
101*4882a593Smuzhiyun }
102