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