blob: 590f99d02c93a07f4c0b81b9ef23169c9374f76e [file] [log] [blame]
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -03001/*
2 handle au0828 IR remotes via linux kernel input layer.
3
4 Copyright (C) 2014 Mauro Carvalho Chehab <mchehab@samsung.com>
5 Copyright (c) 2014 Samsung Electronics Co., Ltd.
6
7 Based on em28xx-input.c.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 */
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
24#include <linux/usb.h>
25#include <linux/slab.h>
26#include <media/rc-core.h>
27
Mauro Carvalho Chehabff346b02014-08-07 13:10:25 -030028static int disable_ir;
29module_param(disable_ir, int, 0444);
30MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
31
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -030032#include "au0828.h"
33
34struct au0828_rc {
35 struct au0828_dev *dev;
36 struct rc_dev *rc;
37 char name[32];
38 char phys[32];
39
40 /* poll decoder */
41 int polling;
42 struct delayed_work work;
43
44 /* i2c slave address of external device (if used) */
45 u16 i2c_dev_addr;
46
47 int (*get_key_i2c)(struct au0828_rc *ir);
48};
49
50/*
51 * AU8522 has a builtin IR receiver. Add functions to get IR from it
52 */
53
54static int au8522_rc_write(struct au0828_rc *ir, u16 reg, u8 data)
55{
56 int rc;
57 char buf[] = { (reg >> 8) | 0x80, reg & 0xff, data };
58 struct i2c_msg msg = { .addr = ir->i2c_dev_addr, .flags = 0,
59 .buf = buf, .len = sizeof(buf) };
60
61 rc = i2c_transfer(ir->dev->i2c_client.adapter, &msg, 1);
62
63 if (rc < 0)
64 return rc;
65
66 return (rc == 1) ? 0 : -EIO;
67}
68
69static int au8522_rc_read(struct au0828_rc *ir, u16 reg, int val,
70 char *buf, int size)
71{
72 int rc;
73 char obuf[3];
74 struct i2c_msg msg[2] = { { .addr = ir->i2c_dev_addr, .flags = 0,
75 .buf = obuf, .len = 2 },
76 { .addr = ir->i2c_dev_addr, .flags = I2C_M_RD,
77 .buf = buf, .len = size } };
78
79 obuf[0] = 0x40 | reg >> 8;
80 obuf[1] = reg & 0xff;
81 if (val >= 0) {
82 obuf[2] = val;
83 msg[0].len++;
84 }
85
86 rc = i2c_transfer(ir->dev->i2c_client.adapter, msg, 2);
87
88 if (rc < 0)
89 return rc;
90
91 return (rc == 2) ? 0 : -EIO;
92}
93
94static int au8522_rc_andor(struct au0828_rc *ir, u16 reg, u8 mask, u8 value)
95{
96 int rc;
97 char buf;
98
99 rc = au8522_rc_read(ir, reg, -1, &buf, 1);
100 if (rc < 0)
101 return rc;
102
103 buf = (buf & ~mask) | (value & mask);
104
105 return au8522_rc_write(ir, reg, buf);
106}
107
108#define au8522_rc_set(ir, reg, bit) au8522_rc_andor(ir, (reg), (bit), (bit))
109#define au8522_rc_clear(ir, reg, bit) au8522_rc_andor(ir, (reg), (bit), 0)
110
111/* Remote Controller time units */
112
113#define AU8522_UNIT 200000 /* ns */
114#define NEC_START_SPACE (4500000 / AU8522_UNIT)
115#define NEC_START_PULSE (562500 * 16)
116#define RC5_START_SPACE (4 * AU8522_UNIT)
117#define RC5_START_PULSE 888888
118
119static int au0828_get_key_au8522(struct au0828_rc *ir)
120{
121 unsigned char buf[40];
122 DEFINE_IR_RAW_EVENT(rawir);
123 int i, j, rc;
124 int prv_bit, bit, width;
125 bool first = true;
126
127 /* Check IR int */
128 rc = au8522_rc_read(ir, 0xe1, -1, buf, 1);
129 if (rc < 0 || !(buf[0] & (1 << 4)))
130 return 0;
131
132 /* Something arrived. Get the data */
133 rc = au8522_rc_read(ir, 0xe3, 0x11, buf, sizeof(buf));
134
135
136 if (rc < 0)
137 return rc;
138
139 /* Disable IR */
140 au8522_rc_clear(ir, 0xe0, 1 << 4);
141
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300142 /* Enable IR */
143 au8522_rc_set(ir, 0xe0, 1 << 4);
144
145 dprintk(16, "RC data received: %*ph\n", 40, buf);
146
147 prv_bit = (buf[0] >> 7) & 0x01;
148 width = 0;
149 for (i = 0; i < sizeof(buf); i++) {
150 for (j = 7; j >= 0; j--) {
151 bit = (buf[i] >> j) & 0x01;
152 if (bit == prv_bit) {
153 width++;
154 continue;
155 }
156
157 /*
158 * Fix an au8522 bug: the first pulse event
159 * is lost. So, we need to fake it, based on the
160 * protocol. That means that not all raw decoders
161 * will work, as we need to add a hack for each
162 * protocol, based on the first space.
163 * So, we only support RC5 and NEC.
164 */
165
166 if (first) {
167 first = false;
168
169 init_ir_raw_event(&rawir);
170 rawir.pulse = true;
171 if (width > NEC_START_SPACE - 2 &&
172 width < NEC_START_SPACE + 2) {
173 /* NEC protocol */
174 rawir.duration = NEC_START_PULSE;
175 dprintk(16, "Storing NEC start %s with duration %d",
176 rawir.pulse ? "pulse" : "space",
177 rawir.duration);
178 } else {
179 /* RC5 protocol */
180 rawir.duration = RC5_START_PULSE;
181 dprintk(16, "Storing RC5 start %s with duration %d",
182 rawir.pulse ? "pulse" : "space",
183 rawir.duration);
184 }
185 ir_raw_event_store(ir->rc, &rawir);
186 }
187
188 init_ir_raw_event(&rawir);
189 rawir.pulse = prv_bit ? false : true;
190 rawir.duration = AU8522_UNIT * width;
191 dprintk(16, "Storing %s with duration %d",
192 rawir.pulse ? "pulse" : "space",
193 rawir.duration);
194 ir_raw_event_store(ir->rc, &rawir);
195
196 width = 1;
197 prv_bit = bit;
198 }
199 }
200
201 init_ir_raw_event(&rawir);
202 rawir.pulse = prv_bit ? false : true;
203 rawir.duration = AU8522_UNIT * width;
204 dprintk(16, "Storing end %s with duration %d",
205 rawir.pulse ? "pulse" : "space",
206 rawir.duration);
207 ir_raw_event_store(ir->rc, &rawir);
208
209 ir_raw_event_handle(ir->rc);
210
211 return 1;
212}
213
214/*
215 * Generic IR code
216 */
217
218static void au0828_rc_work(struct work_struct *work)
219{
220 struct au0828_rc *ir = container_of(work, struct au0828_rc, work.work);
221 int rc;
222
223 rc = ir->get_key_i2c(ir);
224 if (rc < 0)
225 pr_info("Error while getting RC scancode\n");
226
227 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
228}
229
230static int au0828_rc_start(struct rc_dev *rc)
231{
232 struct au0828_rc *ir = rc->priv;
233
234 INIT_DELAYED_WORK(&ir->work, au0828_rc_work);
235
236 /* Enable IR */
237 au8522_rc_set(ir, 0xe0, 1 << 4);
238
239 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
240
241 return 0;
242}
243
244static void au0828_rc_stop(struct rc_dev *rc)
245{
246 struct au0828_rc *ir = rc->priv;
247
248 /* Disable IR */
249 au8522_rc_clear(ir, 0xe0, 1 << 4);
250
251 cancel_delayed_work_sync(&ir->work);
252}
253
254static int au0828_probe_i2c_ir(struct au0828_dev *dev)
255{
256 int i = 0;
257 const unsigned short addr_list[] = {
258 0x47, I2C_CLIENT_END
259 };
260
261 while (addr_list[i] != I2C_CLIENT_END) {
262 if (i2c_probe_func_quick_read(dev->i2c_client.adapter,
263 addr_list[i]) == 1)
264 return addr_list[i];
265 i++;
266 }
267
268 return -ENODEV;
269}
270
271int au0828_rc_register(struct au0828_dev *dev)
272{
273 struct au0828_rc *ir;
274 struct rc_dev *rc;
275 int err = -ENOMEM;
276 u16 i2c_rc_dev_addr = 0;
277
Mauro Carvalho Chehabff346b02014-08-07 13:10:25 -0300278 if (!dev->board.has_ir_i2c || disable_ir)
Mauro Carvalho Chehab2fcfd312014-07-24 22:49:02 -0300279 return 0;
280
281 i2c_rc_dev_addr = au0828_probe_i2c_ir(dev);
282 if (!i2c_rc_dev_addr)
283 return -ENODEV;
284
285 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
286 rc = rc_allocate_device();
287 if (!ir || !rc)
288 goto error;
289
290 /* record handles to ourself */
291 ir->dev = dev;
292 dev->ir = ir;
293 ir->rc = rc;
294
295 rc->priv = ir;
296 rc->open = au0828_rc_start;
297 rc->close = au0828_rc_stop;
298
299 if (dev->board.has_ir_i2c) { /* external i2c device */
300 switch (dev->boardnr) {
301 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
302 rc->map_name = RC_MAP_HAUPPAUGE;
303 ir->get_key_i2c = au0828_get_key_au8522;
304 break;
305 default:
306 err = -ENODEV;
307 goto error;
308 }
309
310 ir->i2c_dev_addr = i2c_rc_dev_addr;
311 }
312
313 /* This is how often we ask the chip for IR information */
314 ir->polling = 100; /* ms */
315
316 /* init input device */
317 snprintf(ir->name, sizeof(ir->name), "au0828 IR (%s)",
318 dev->board.name);
319
320 usb_make_path(dev->usbdev, ir->phys, sizeof(ir->phys));
321 strlcat(ir->phys, "/input0", sizeof(ir->phys));
322
323 rc->input_name = ir->name;
324 rc->input_phys = ir->phys;
325 rc->input_id.bustype = BUS_USB;
326 rc->input_id.version = 1;
327 rc->input_id.vendor = le16_to_cpu(dev->usbdev->descriptor.idVendor);
328 rc->input_id.product = le16_to_cpu(dev->usbdev->descriptor.idProduct);
329 rc->dev.parent = &dev->usbdev->dev;
330 rc->driver_name = "au0828-input";
331 rc->driver_type = RC_DRIVER_IR_RAW;
332 rc->allowed_protocols = RC_BIT_NEC | RC_BIT_RC5;
333
334 /* all done */
335 err = rc_register_device(rc);
336 if (err)
337 goto error;
338
339 pr_info("Remote controller %s initalized\n", ir->name);
340
341 return 0;
342
343error:
344 dev->ir = NULL;
345 rc_free_device(rc);
346 kfree(ir);
347 return err;
348}
349
350void au0828_rc_unregister(struct au0828_dev *dev)
351{
352 struct au0828_rc *ir = dev->ir;
353
354 /* skip detach on non attached boards */
355 if (!ir)
356 return;
357
358 if (ir->rc)
359 rc_unregister_device(ir->rc);
360
361 /* done */
362 kfree(ir);
363 dev->ir = NULL;
364}
365
366int au0828_rc_suspend(struct au0828_dev *dev)
367{
368 struct au0828_rc *ir = dev->ir;
369
370 if (!ir)
371 return 0;
372
373 cancel_delayed_work_sync(&ir->work);
374
375 return 0;
376}
377
378int au0828_rc_resume(struct au0828_dev *dev)
379{
380 struct au0828_rc *ir = dev->ir;
381
382 if (!ir)
383 return 0;
384
385 schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
386
387 return 0;
388}