Merge tag 'trace-3.18-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[cascardo/linux.git] / drivers / media / usb / au0828 / au0828-input.c
1 /*
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 "au0828.h"
21
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/usb.h>
27 #include <linux/slab.h>
28 #include <media/rc-core.h>
29
30 static int disable_ir;
31 module_param(disable_ir,        int, 0444);
32 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
33
34 struct 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
54 static 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
69 static 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
94 static int au8522_rc_andor(struct au0828_rc *ir, u16 reg, u8 mask, u8 value)
95 {
96         int rc;
97         char buf, oldbuf;
98
99         rc = au8522_rc_read(ir, reg, -1, &buf, 1);
100         if (rc < 0)
101                 return rc;
102
103         oldbuf = buf;
104         buf = (buf & ~mask) | (value & mask);
105
106         /* Nothing to do, just return */
107         if (buf == oldbuf)
108                 return 0;
109
110         return au8522_rc_write(ir, reg, buf);
111 }
112
113 #define au8522_rc_set(ir, reg, bit) au8522_rc_andor(ir, (reg), (bit), (bit))
114 #define au8522_rc_clear(ir, reg, bit) au8522_rc_andor(ir, (reg), (bit), 0)
115
116 /* Remote Controller time units */
117
118 #define AU8522_UNIT             200000 /* ns */
119 #define NEC_START_SPACE         (4500000 / AU8522_UNIT)
120 #define NEC_START_PULSE         (562500 * 16)
121 #define RC5_START_SPACE         (4 * AU8522_UNIT)
122 #define RC5_START_PULSE         888888
123
124 static int au0828_get_key_au8522(struct au0828_rc *ir)
125 {
126         unsigned char buf[40];
127         DEFINE_IR_RAW_EVENT(rawir);
128         int i, j, rc;
129         int prv_bit, bit, width;
130         bool first = true;
131
132         /* Check IR int */
133         rc = au8522_rc_read(ir, 0xe1, -1, buf, 1);
134         if (rc < 0 || !(buf[0] & (1 << 4))) {
135                 /* Be sure that IR is enabled */
136                 au8522_rc_set(ir, 0xe0, 1 << 4);
137                 return 0;
138         }
139
140         /* Something arrived. Get the data */
141         rc = au8522_rc_read(ir, 0xe3, 0x11, buf, sizeof(buf));
142
143
144         if (rc < 0)
145                 return rc;
146
147         /* Disable IR */
148         au8522_rc_clear(ir, 0xe0, 1 << 4);
149
150         /* Enable IR */
151         au8522_rc_set(ir, 0xe0, 1 << 4);
152
153         dprintk(16, "RC data received: %*ph\n", 40, buf);
154
155         prv_bit = (buf[0] >> 7) & 0x01;
156         width = 0;
157         for (i = 0; i < sizeof(buf); i++) {
158                 for (j = 7; j >= 0; j--) {
159                         bit = (buf[i] >> j) & 0x01;
160                         if (bit == prv_bit) {
161                                 width++;
162                                 continue;
163                         }
164
165                         /*
166                          * Fix an au8522 bug: the first pulse event
167                          * is lost. So, we need to fake it, based on the
168                          * protocol. That means that not all raw decoders
169                          * will work, as we need to add a hack for each
170                          * protocol, based on the first space.
171                          * So, we only support RC5 and NEC.
172                          */
173
174                         if (first) {
175                                 first = false;
176
177                                 init_ir_raw_event(&rawir);
178                                 rawir.pulse = true;
179                                 if (width > NEC_START_SPACE - 2 &&
180                                     width < NEC_START_SPACE + 2) {
181                                         /* NEC protocol */
182                                         rawir.duration = NEC_START_PULSE;
183                                         dprintk(16, "Storing NEC start %s with duration %d",
184                                                 rawir.pulse ? "pulse" : "space",
185                                                 rawir.duration);
186                                 } else {
187                                         /* RC5 protocol */
188                                         rawir.duration = RC5_START_PULSE;
189                                         dprintk(16, "Storing RC5 start %s with duration %d",
190                                                 rawir.pulse ? "pulse" : "space",
191                                                 rawir.duration);
192                                 }
193                                 ir_raw_event_store(ir->rc, &rawir);
194                         }
195
196                         init_ir_raw_event(&rawir);
197                         rawir.pulse = prv_bit ? false : true;
198                         rawir.duration = AU8522_UNIT * width;
199                         dprintk(16, "Storing %s with duration %d",
200                                 rawir.pulse ? "pulse" : "space",
201                                 rawir.duration);
202                         ir_raw_event_store(ir->rc, &rawir);
203
204                         width = 1;
205                         prv_bit = bit;
206                 }
207         }
208
209         init_ir_raw_event(&rawir);
210         rawir.pulse = prv_bit ? false : true;
211         rawir.duration = AU8522_UNIT * width;
212         dprintk(16, "Storing end %s with duration %d",
213                 rawir.pulse ? "pulse" : "space",
214                 rawir.duration);
215         ir_raw_event_store(ir->rc, &rawir);
216
217         ir_raw_event_handle(ir->rc);
218
219         return 1;
220 }
221
222 /*
223  * Generic IR code
224  */
225
226 static void au0828_rc_work(struct work_struct *work)
227 {
228         struct au0828_rc *ir = container_of(work, struct au0828_rc, work.work);
229         int rc;
230
231         rc = ir->get_key_i2c(ir);
232         if (rc < 0)
233                 pr_info("Error while getting RC scancode\n");
234
235         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
236 }
237
238 static int au0828_rc_start(struct rc_dev *rc)
239 {
240         struct au0828_rc *ir = rc->priv;
241
242         INIT_DELAYED_WORK(&ir->work, au0828_rc_work);
243
244         /* Enable IR */
245         au8522_rc_set(ir, 0xe0, 1 << 4);
246
247         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
248
249         return 0;
250 }
251
252 static void au0828_rc_stop(struct rc_dev *rc)
253 {
254         struct au0828_rc *ir = rc->priv;
255
256         cancel_delayed_work_sync(&ir->work);
257
258         /* Disable IR */
259         au8522_rc_clear(ir, 0xe0, 1 << 4);
260 }
261
262 static int au0828_probe_i2c_ir(struct au0828_dev *dev)
263 {
264         int i = 0;
265         const unsigned short addr_list[] = {
266                  0x47, I2C_CLIENT_END
267         };
268
269         while (addr_list[i] != I2C_CLIENT_END) {
270                 if (i2c_probe_func_quick_read(dev->i2c_client.adapter,
271                                               addr_list[i]) == 1)
272                         return addr_list[i];
273                 i++;
274         }
275
276         return -ENODEV;
277 }
278
279 int au0828_rc_register(struct au0828_dev *dev)
280 {
281         struct au0828_rc *ir;
282         struct rc_dev *rc;
283         int err = -ENOMEM;
284         u16 i2c_rc_dev_addr = 0;
285
286         if (!dev->board.has_ir_i2c || disable_ir)
287                 return 0;
288
289         i2c_rc_dev_addr = au0828_probe_i2c_ir(dev);
290         if (!i2c_rc_dev_addr)
291                 return -ENODEV;
292
293         ir = kzalloc(sizeof(*ir), GFP_KERNEL);
294         rc = rc_allocate_device();
295         if (!ir || !rc)
296                 goto error;
297
298         /* record handles to ourself */
299         ir->dev = dev;
300         dev->ir = ir;
301         ir->rc = rc;
302
303         rc->priv = ir;
304         rc->open = au0828_rc_start;
305         rc->close = au0828_rc_stop;
306
307         if (dev->board.has_ir_i2c) {    /* external i2c device */
308                 switch (dev->boardnr) {
309                 case AU0828_BOARD_HAUPPAUGE_HVR950Q:
310                         rc->map_name = RC_MAP_HAUPPAUGE;
311                         ir->get_key_i2c = au0828_get_key_au8522;
312                         break;
313                 default:
314                         err = -ENODEV;
315                         goto error;
316                 }
317
318                 ir->i2c_dev_addr = i2c_rc_dev_addr;
319         }
320
321         /* This is how often we ask the chip for IR information */
322         ir->polling = 100; /* ms */
323
324         /* init input device */
325         snprintf(ir->name, sizeof(ir->name), "au0828 IR (%s)",
326                  dev->board.name);
327
328         usb_make_path(dev->usbdev, ir->phys, sizeof(ir->phys));
329         strlcat(ir->phys, "/input0", sizeof(ir->phys));
330
331         rc->input_name = ir->name;
332         rc->input_phys = ir->phys;
333         rc->input_id.bustype = BUS_USB;
334         rc->input_id.version = 1;
335         rc->input_id.vendor = le16_to_cpu(dev->usbdev->descriptor.idVendor);
336         rc->input_id.product = le16_to_cpu(dev->usbdev->descriptor.idProduct);
337         rc->dev.parent = &dev->usbdev->dev;
338         rc->driver_name = "au0828-input";
339         rc->driver_type = RC_DRIVER_IR_RAW;
340         rc->allowed_protocols = RC_BIT_NEC | RC_BIT_RC5;
341
342         /* all done */
343         err = rc_register_device(rc);
344         if (err)
345                 goto error;
346
347         pr_info("Remote controller %s initalized\n", ir->name);
348
349         return 0;
350
351 error:
352         dev->ir = NULL;
353         rc_free_device(rc);
354         kfree(ir);
355         return err;
356 }
357
358 void au0828_rc_unregister(struct au0828_dev *dev)
359 {
360         struct au0828_rc *ir = dev->ir;
361
362         /* skip detach on non attached boards */
363         if (!ir)
364                 return;
365
366         if (ir->rc)
367                 rc_unregister_device(ir->rc);
368
369         /* done */
370         kfree(ir);
371         dev->ir = NULL;
372 }
373
374 int au0828_rc_suspend(struct au0828_dev *dev)
375 {
376         struct au0828_rc *ir = dev->ir;
377
378         if (!ir)
379                 return 0;
380
381         pr_info("Stopping RC\n");
382
383         cancel_delayed_work_sync(&ir->work);
384
385         /* Disable IR */
386         au8522_rc_clear(ir, 0xe0, 1 << 4);
387
388         return 0;
389 }
390
391 int au0828_rc_resume(struct au0828_dev *dev)
392 {
393         struct au0828_rc *ir = dev->ir;
394
395         if (!ir)
396                 return 0;
397
398         pr_info("Restarting RC\n");
399
400         /* Enable IR */
401         au8522_rc_set(ir, 0xe0, 1 << 4);
402
403         schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
404
405         return 0;
406 }