blob: 15d8d1b5f05ca73feb8817e3b6c1132d45f17f04 [file] [log] [blame]
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -03001/*
2 * cx231xx IR glue driver
3 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02004 * Copyright (C) 2010 Mauro Carvalho Chehab
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -03005 *
6 * Polaris (cx231xx) has its support for IR's with a design close to MCE.
7 * however, a few designs are using an external I2C chip for IR, instead
8 * of using the one provided by the chip.
9 * This driver provides support for those extra devices
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation version 2.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 */
20
21#include "cx231xx.h"
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030022#include <linux/slab.h>
David Härdeman4dd9bb92014-04-03 20:31:25 -030023#include <linux/bitrev.h>
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030024
25#define MODULE_NAME "cx231xx-input"
26
David Härdeman4dd9bb92014-04-03 20:31:25 -030027static int get_key_isdbt(struct IR_i2c *ir, enum rc_type *protocol,
28 u32 *pscancode, u8 *toggle)
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030029{
Mauro Carvalho Chehab90bf3aa2012-01-09 22:28:13 -020030 int rc;
Mauro Carvalho Chehabe3302892010-12-17 14:22:09 -030031 u8 cmd, scancode;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030032
Mauro Carvalho Chehab141bb0d2010-11-11 08:14:16 -030033 dev_dbg(&ir->rc->input_dev->dev, "%s\n", __func__);
34
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030035 /* poll IR chip */
Mauro Carvalho Chehab90bf3aa2012-01-09 22:28:13 -020036 rc = i2c_master_recv(ir->c, &cmd, 1);
37 if (rc < 0)
38 return rc;
39 if (rc != 1)
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030040 return -EIO;
41
42 /* it seems that 0xFE indicates that a button is still hold
43 down, while 0xff indicates that no button is hold
44 down. 0xfe sequences are sometimes interrupted by 0xFF */
45
46 if (cmd == 0xff)
47 return 0;
48
David Härdeman4dd9bb92014-04-03 20:31:25 -030049 scancode = bitrev8(cmd);
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030050
Mauro Carvalho Chehabe3302892010-12-17 14:22:09 -030051 dev_dbg(&ir->rc->input_dev->dev, "cmd %02x, scan = %02x\n",
52 cmd, scancode);
53
David Härdeman4dd9bb92014-04-03 20:31:25 -030054 *protocol = RC_TYPE_OTHER;
55 *pscancode = scancode;
56 *toggle = 0;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030057 return 1;
58}
59
60int cx231xx_ir_init(struct cx231xx *dev)
61{
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030062 struct i2c_board_info info;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030063 u8 ir_i2c_bus;
64
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -030065 dev_dbg(dev->dev, "%s\n", __func__);
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030066
67 /* Only initialize if a rc keycode map is defined */
Mauro Carvalho Chehab29e3ec12010-11-17 14:12:46 -030068 if (!cx231xx_boards[dev->model].rc_map_name)
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030069 return -ENODEV;
70
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030071 request_module("ir-kbd-i2c");
72
73 memset(&info, 0, sizeof(struct i2c_board_info));
Mauro Carvalho Chehab141bb0d2010-11-11 08:14:16 -030074 memset(&dev->init_data, 0, sizeof(dev->init_data));
75 dev->init_data.rc_dev = rc_allocate_device();
76 if (!dev->init_data.rc_dev)
77 return -ENOMEM;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030078
Mauro Carvalho Chehab141bb0d2010-11-11 08:14:16 -030079 dev->init_data.name = cx231xx_boards[dev->model].name;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030080
81 strlcpy(info.type, "ir_video", I2C_NAME_SIZE);
Mauro Carvalho Chehab141bb0d2010-11-11 08:14:16 -030082 info.platform_data = &dev->init_data;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030083
84 /*
85 * Board-dependent values
86 *
87 * For now, there's just one type of hardware design using
88 * an i2c device.
89 */
Mauro Carvalho Chehab141bb0d2010-11-11 08:14:16 -030090 dev->init_data.get_key = get_key_isdbt;
Mauro Carvalho Chehab29e3ec12010-11-17 14:12:46 -030091 dev->init_data.ir_codes = cx231xx_boards[dev->model].rc_map_name;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030092 /* The i2c micro-controller only outputs the cmd part of NEC protocol */
David Härdeman9d2f1d32014-04-03 20:32:26 -030093 dev->init_data.rc_dev->scancode_mask = 0xff;
Mauro Carvalho Chehab141bb0d2010-11-11 08:14:16 -030094 dev->init_data.rc_dev->driver_name = "cx231xx";
David Härdemanc003ab12012-10-11 19:11:54 -030095 dev->init_data.type = RC_BIT_NEC;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030096 info.addr = 0x30;
97
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -030098 /* Load and bind ir-kbd-i2c */
99 ir_i2c_bus = cx231xx_boards[dev->model].ir_i2c_master;
Mauro Carvalho Chehab336fea92014-11-03 06:07:38 -0300100 dev_dbg(dev->dev, "Trying to bind ir at bus %d, addr 0x%02x\n",
Mauro Carvalho Chehab141bb0d2010-11-11 08:14:16 -0300101 ir_i2c_bus, info.addr);
Matthias Schwarzottc3c3f1a2014-10-02 02:20:59 -0300102 dev->ir_i2c_client = i2c_new_device(
103 cx231xx_get_i2c_adap(dev, ir_i2c_bus), &info);
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -0300104
Mauro Carvalho Chehab141bb0d2010-11-11 08:14:16 -0300105 return 0;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -0300106}
107
108void cx231xx_ir_exit(struct cx231xx *dev)
109{
Mauro Carvalho Chehab7528cd22012-01-10 09:20:01 -0200110 if (dev->ir_i2c_client)
111 i2c_unregister_device(dev->ir_i2c_client);
112 dev->ir_i2c_client = NULL;
Mauro Carvalho Chehab9ab66912010-10-23 13:28:33 -0300113}