blob: 0d4f9211f9f272d0fec28f6ab5b2fc72df97903b [file] [log] [blame]
James Hogan30dd9e02014-02-28 20:28:54 -03001/*
2 * ImgTec IR Hardware Decoder found in PowerDown Controller.
3 *
4 * Copyright 2010-2014 Imagination Technologies Ltd.
5 *
6 * This ties into the input subsystem using the RC-core. Protocol support is
7 * provided in separate modules which provide the parameters and scancode
8 * translation functions to set up the hardware decoder and interpret the
9 * resulting input.
10 */
11
12#include <linux/bitops.h>
13#include <linux/clk.h>
14#include <linux/interrupt.h>
15#include <linux/spinlock.h>
16#include <linux/timer.h>
17#include <media/rc-core.h>
18#include "img-ir.h"
19
20/* Decoders lock (only modified to preprocess them) */
21static DEFINE_SPINLOCK(img_ir_decoders_lock);
22
James Hogan635abb72014-02-28 20:28:56 -030023extern struct img_ir_decoder img_ir_nec;
James Hogan69336532014-02-28 20:28:57 -030024extern struct img_ir_decoder img_ir_jvc;
James Hogane72b21a2014-02-28 20:28:58 -030025extern struct img_ir_decoder img_ir_sony;
James Hogan635abb72014-02-28 20:28:56 -030026
James Hogan30dd9e02014-02-28 20:28:54 -030027static bool img_ir_decoders_preprocessed;
28static struct img_ir_decoder *img_ir_decoders[] = {
James Hogan635abb72014-02-28 20:28:56 -030029#ifdef CONFIG_IR_IMG_NEC
30 &img_ir_nec,
31#endif
James Hogan69336532014-02-28 20:28:57 -030032#ifdef CONFIG_IR_IMG_JVC
33 &img_ir_jvc,
34#endif
James Hogane72b21a2014-02-28 20:28:58 -030035#ifdef CONFIG_IR_IMG_SONY
36 &img_ir_sony,
37#endif
James Hogan30dd9e02014-02-28 20:28:54 -030038 NULL
39};
40
41#define IMG_IR_F_FILTER BIT(RC_FILTER_NORMAL) /* enable filtering */
42#define IMG_IR_F_WAKE BIT(RC_FILTER_WAKEUP) /* enable waking */
43
44/* code type quirks */
45
46#define IMG_IR_QUIRK_CODE_BROKEN 0x1 /* Decode is broken */
47#define IMG_IR_QUIRK_CODE_LEN_INCR 0x2 /* Bit length needs increment */
48
49/* functions for preprocessing timings, ensuring max is set */
50
51static void img_ir_timing_preprocess(struct img_ir_timing_range *range,
52 unsigned int unit)
53{
54 if (range->max < range->min)
55 range->max = range->min;
56 if (unit) {
57 /* multiply by unit and convert to microseconds */
58 range->min = (range->min*unit)/1000;
59 range->max = (range->max*unit + 999)/1000; /* round up */
60 }
61}
62
63static void img_ir_symbol_timing_preprocess(struct img_ir_symbol_timing *timing,
64 unsigned int unit)
65{
66 img_ir_timing_preprocess(&timing->pulse, unit);
67 img_ir_timing_preprocess(&timing->space, unit);
68}
69
70static void img_ir_timings_preprocess(struct img_ir_timings *timings,
71 unsigned int unit)
72{
73 img_ir_symbol_timing_preprocess(&timings->ldr, unit);
74 img_ir_symbol_timing_preprocess(&timings->s00, unit);
75 img_ir_symbol_timing_preprocess(&timings->s01, unit);
76 img_ir_symbol_timing_preprocess(&timings->s10, unit);
77 img_ir_symbol_timing_preprocess(&timings->s11, unit);
78 /* default s10 and s11 to s00 and s01 if no leader */
79 if (unit)
80 /* multiply by unit and convert to microseconds (round up) */
81 timings->ft.ft_min = (timings->ft.ft_min*unit + 999)/1000;
82}
83
84/* functions for filling empty fields with defaults */
85
86static void img_ir_timing_defaults(struct img_ir_timing_range *range,
87 struct img_ir_timing_range *defaults)
88{
89 if (!range->min)
90 range->min = defaults->min;
91 if (!range->max)
92 range->max = defaults->max;
93}
94
95static void img_ir_symbol_timing_defaults(struct img_ir_symbol_timing *timing,
96 struct img_ir_symbol_timing *defaults)
97{
98 img_ir_timing_defaults(&timing->pulse, &defaults->pulse);
99 img_ir_timing_defaults(&timing->space, &defaults->space);
100}
101
102static void img_ir_timings_defaults(struct img_ir_timings *timings,
103 struct img_ir_timings *defaults)
104{
105 img_ir_symbol_timing_defaults(&timings->ldr, &defaults->ldr);
106 img_ir_symbol_timing_defaults(&timings->s00, &defaults->s00);
107 img_ir_symbol_timing_defaults(&timings->s01, &defaults->s01);
108 img_ir_symbol_timing_defaults(&timings->s10, &defaults->s10);
109 img_ir_symbol_timing_defaults(&timings->s11, &defaults->s11);
110 if (!timings->ft.ft_min)
111 timings->ft.ft_min = defaults->ft.ft_min;
112}
113
114/* functions for converting timings to register values */
115
116/**
117 * img_ir_control() - Convert control struct to control register value.
118 * @control: Control data
119 *
120 * Returns: The control register value equivalent of @control.
121 */
122static u32 img_ir_control(const struct img_ir_control *control)
123{
124 u32 ctrl = control->code_type << IMG_IR_CODETYPE_SHIFT;
125 if (control->decoden)
126 ctrl |= IMG_IR_DECODEN;
127 if (control->hdrtog)
128 ctrl |= IMG_IR_HDRTOG;
129 if (control->ldrdec)
130 ctrl |= IMG_IR_LDRDEC;
131 if (control->decodinpol)
132 ctrl |= IMG_IR_DECODINPOL;
133 if (control->bitorien)
134 ctrl |= IMG_IR_BITORIEN;
135 if (control->d1validsel)
136 ctrl |= IMG_IR_D1VALIDSEL;
137 if (control->bitinv)
138 ctrl |= IMG_IR_BITINV;
139 if (control->decodend2)
140 ctrl |= IMG_IR_DECODEND2;
141 if (control->bitoriend2)
142 ctrl |= IMG_IR_BITORIEND2;
143 if (control->bitinvd2)
144 ctrl |= IMG_IR_BITINVD2;
145 return ctrl;
146}
147
148/**
149 * img_ir_timing_range_convert() - Convert microsecond range.
150 * @out: Output timing range in clock cycles with a shift.
151 * @in: Input timing range in microseconds.
152 * @tolerance: Tolerance as a fraction of 128 (roughly percent).
153 * @clock_hz: IR clock rate in Hz.
154 * @shift: Shift of output units.
155 *
156 * Converts min and max from microseconds to IR clock cycles, applies a
157 * tolerance, and shifts for the register, rounding in the right direction.
158 * Note that in and out can safely be the same object.
159 */
160static void img_ir_timing_range_convert(struct img_ir_timing_range *out,
161 const struct img_ir_timing_range *in,
162 unsigned int tolerance,
163 unsigned long clock_hz,
164 unsigned int shift)
165{
166 unsigned int min = in->min;
167 unsigned int max = in->max;
168 /* add a tolerance */
169 min = min - (min*tolerance >> 7);
170 max = max + (max*tolerance >> 7);
171 /* convert from microseconds into clock cycles */
172 min = min*clock_hz / 1000000;
173 max = (max*clock_hz + 999999) / 1000000; /* round up */
174 /* apply shift and copy to output */
175 out->min = min >> shift;
176 out->max = (max + ((1 << shift) - 1)) >> shift; /* round up */
177}
178
179/**
180 * img_ir_symbol_timing() - Convert symbol timing struct to register value.
181 * @timing: Symbol timing data
182 * @tolerance: Timing tolerance where 0-128 represents 0-100%
183 * @clock_hz: Frequency of source clock in Hz
184 * @pd_shift: Shift to apply to symbol period
185 * @w_shift: Shift to apply to symbol width
186 *
187 * Returns: Symbol timing register value based on arguments.
188 */
189static u32 img_ir_symbol_timing(const struct img_ir_symbol_timing *timing,
190 unsigned int tolerance,
191 unsigned long clock_hz,
192 unsigned int pd_shift,
193 unsigned int w_shift)
194{
195 struct img_ir_timing_range hw_pulse, hw_period;
196 /* we calculate period in hw_period, then convert in place */
197 hw_period.min = timing->pulse.min + timing->space.min;
198 hw_period.max = timing->pulse.max + timing->space.max;
199 img_ir_timing_range_convert(&hw_period, &hw_period,
200 tolerance, clock_hz, pd_shift);
201 img_ir_timing_range_convert(&hw_pulse, &timing->pulse,
202 tolerance, clock_hz, w_shift);
203 /* construct register value */
204 return (hw_period.max << IMG_IR_PD_MAX_SHIFT) |
205 (hw_period.min << IMG_IR_PD_MIN_SHIFT) |
206 (hw_pulse.max << IMG_IR_W_MAX_SHIFT) |
207 (hw_pulse.min << IMG_IR_W_MIN_SHIFT);
208}
209
210/**
211 * img_ir_free_timing() - Convert free time timing struct to register value.
212 * @timing: Free symbol timing data
213 * @clock_hz: Source clock frequency in Hz
214 *
215 * Returns: Free symbol timing register value.
216 */
217static u32 img_ir_free_timing(const struct img_ir_free_timing *timing,
218 unsigned long clock_hz)
219{
220 unsigned int minlen, maxlen, ft_min;
221 /* minlen is only 5 bits, and round minlen to multiple of 2 */
222 if (timing->minlen < 30)
223 minlen = timing->minlen & -2;
224 else
225 minlen = 30;
226 /* maxlen has maximum value of 48, and round maxlen to multiple of 2 */
227 if (timing->maxlen < 48)
228 maxlen = (timing->maxlen + 1) & -2;
229 else
230 maxlen = 48;
231 /* convert and shift ft_min, rounding upwards */
232 ft_min = (timing->ft_min*clock_hz + 999999) / 1000000;
233 ft_min = (ft_min + 7) >> 3;
234 /* construct register value */
235 return (timing->maxlen << IMG_IR_MAXLEN_SHIFT) |
236 (timing->minlen << IMG_IR_MINLEN_SHIFT) |
237 (ft_min << IMG_IR_FT_MIN_SHIFT);
238}
239
240/**
241 * img_ir_free_timing_dynamic() - Update free time register value.
242 * @st_ft: Static free time register value from img_ir_free_timing.
243 * @filter: Current filter which may additionally restrict min/max len.
244 *
245 * Returns: Updated free time register value based on the current filter.
246 */
247static u32 img_ir_free_timing_dynamic(u32 st_ft, struct img_ir_filter *filter)
248{
249 unsigned int minlen, maxlen, newminlen, newmaxlen;
250
251 /* round minlen, maxlen to multiple of 2 */
252 newminlen = filter->minlen & -2;
253 newmaxlen = (filter->maxlen + 1) & -2;
254 /* extract min/max len from register */
255 minlen = (st_ft & IMG_IR_MINLEN) >> IMG_IR_MINLEN_SHIFT;
256 maxlen = (st_ft & IMG_IR_MAXLEN) >> IMG_IR_MAXLEN_SHIFT;
257 /* if the new values are more restrictive, update the register value */
258 if (newminlen > minlen) {
259 st_ft &= ~IMG_IR_MINLEN;
260 st_ft |= newminlen << IMG_IR_MINLEN_SHIFT;
261 }
262 if (newmaxlen < maxlen) {
263 st_ft &= ~IMG_IR_MAXLEN;
264 st_ft |= newmaxlen << IMG_IR_MAXLEN_SHIFT;
265 }
266 return st_ft;
267}
268
269/**
270 * img_ir_timings_convert() - Convert timings to register values
271 * @regs: Output timing register values
272 * @timings: Input timing data
273 * @tolerance: Timing tolerance where 0-128 represents 0-100%
274 * @clock_hz: Source clock frequency in Hz
275 */
276static void img_ir_timings_convert(struct img_ir_timing_regvals *regs,
277 const struct img_ir_timings *timings,
278 unsigned int tolerance,
279 unsigned int clock_hz)
280{
281 /* leader symbol timings are divided by 16 */
282 regs->ldr = img_ir_symbol_timing(&timings->ldr, tolerance, clock_hz,
283 4, 4);
284 /* other symbol timings, pd fields only are divided by 2 */
285 regs->s00 = img_ir_symbol_timing(&timings->s00, tolerance, clock_hz,
286 1, 0);
287 regs->s01 = img_ir_symbol_timing(&timings->s01, tolerance, clock_hz,
288 1, 0);
289 regs->s10 = img_ir_symbol_timing(&timings->s10, tolerance, clock_hz,
290 1, 0);
291 regs->s11 = img_ir_symbol_timing(&timings->s11, tolerance, clock_hz,
292 1, 0);
293 regs->ft = img_ir_free_timing(&timings->ft, clock_hz);
294}
295
296/**
297 * img_ir_decoder_preprocess() - Preprocess timings in decoder.
298 * @decoder: Decoder to be preprocessed.
299 *
300 * Ensures that the symbol timing ranges are valid with respect to ordering, and
301 * does some fixed conversion on them.
302 */
303static void img_ir_decoder_preprocess(struct img_ir_decoder *decoder)
304{
305 /* default tolerance */
306 if (!decoder->tolerance)
307 decoder->tolerance = 10; /* percent */
308 /* and convert tolerance to fraction out of 128 */
309 decoder->tolerance = decoder->tolerance * 128 / 100;
310
311 /* fill in implicit fields */
312 img_ir_timings_preprocess(&decoder->timings, decoder->unit);
313
314 /* do the same for repeat timings if applicable */
315 if (decoder->repeat) {
316 img_ir_timings_preprocess(&decoder->rtimings, decoder->unit);
317 img_ir_timings_defaults(&decoder->rtimings, &decoder->timings);
318 }
319}
320
321/**
322 * img_ir_decoder_convert() - Generate internal timings in decoder.
323 * @decoder: Decoder to be converted to internal timings.
324 * @timings: Timing register values.
325 * @clock_hz: IR clock rate in Hz.
326 *
327 * Fills out the repeat timings and timing register values for a specific clock
328 * rate.
329 */
330static void img_ir_decoder_convert(const struct img_ir_decoder *decoder,
331 struct img_ir_reg_timings *reg_timings,
332 unsigned int clock_hz)
333{
334 /* calculate control value */
335 reg_timings->ctrl = img_ir_control(&decoder->control);
336
337 /* fill in implicit fields and calculate register values */
338 img_ir_timings_convert(&reg_timings->timings, &decoder->timings,
339 decoder->tolerance, clock_hz);
340
341 /* do the same for repeat timings if applicable */
342 if (decoder->repeat)
343 img_ir_timings_convert(&reg_timings->rtimings,
344 &decoder->rtimings, decoder->tolerance,
345 clock_hz);
346}
347
348/**
349 * img_ir_write_timings() - Write timings to the hardware now
350 * @priv: IR private data
351 * @regs: Timing register values to write
352 * @type: RC filter type (RC_FILTER_*)
353 *
354 * Write timing register values @regs to the hardware, taking into account the
355 * current filter which may impose restrictions on the length of the expected
356 * data.
357 */
358static void img_ir_write_timings(struct img_ir_priv *priv,
359 struct img_ir_timing_regvals *regs,
360 enum rc_filter_type type)
361{
362 struct img_ir_priv_hw *hw = &priv->hw;
363
364 /* filter may be more restrictive to minlen, maxlen */
365 u32 ft = regs->ft;
366 if (hw->flags & BIT(type))
367 ft = img_ir_free_timing_dynamic(regs->ft, &hw->filters[type]);
368 /* write to registers */
369 img_ir_write(priv, IMG_IR_LEAD_SYMB_TIMING, regs->ldr);
370 img_ir_write(priv, IMG_IR_S00_SYMB_TIMING, regs->s00);
371 img_ir_write(priv, IMG_IR_S01_SYMB_TIMING, regs->s01);
372 img_ir_write(priv, IMG_IR_S10_SYMB_TIMING, regs->s10);
373 img_ir_write(priv, IMG_IR_S11_SYMB_TIMING, regs->s11);
374 img_ir_write(priv, IMG_IR_FREE_SYMB_TIMING, ft);
375 dev_dbg(priv->dev, "timings: ldr=%#x, s=[%#x, %#x, %#x, %#x], ft=%#x\n",
376 regs->ldr, regs->s00, regs->s01, regs->s10, regs->s11, ft);
377}
378
379static void img_ir_write_filter(struct img_ir_priv *priv,
380 struct img_ir_filter *filter)
381{
382 if (filter) {
383 dev_dbg(priv->dev, "IR filter=%016llx & %016llx\n",
384 (unsigned long long)filter->data,
385 (unsigned long long)filter->mask);
386 img_ir_write(priv, IMG_IR_IRQ_MSG_DATA_LW, (u32)filter->data);
387 img_ir_write(priv, IMG_IR_IRQ_MSG_DATA_UP, (u32)(filter->data
388 >> 32));
389 img_ir_write(priv, IMG_IR_IRQ_MSG_MASK_LW, (u32)filter->mask);
390 img_ir_write(priv, IMG_IR_IRQ_MSG_MASK_UP, (u32)(filter->mask
391 >> 32));
392 } else {
393 dev_dbg(priv->dev, "IR clearing filter\n");
394 img_ir_write(priv, IMG_IR_IRQ_MSG_MASK_LW, 0);
395 img_ir_write(priv, IMG_IR_IRQ_MSG_MASK_UP, 0);
396 }
397}
398
399/* caller must have lock */
400static void _img_ir_set_filter(struct img_ir_priv *priv,
401 struct img_ir_filter *filter)
402{
403 struct img_ir_priv_hw *hw = &priv->hw;
404 u32 irq_en, irq_on;
405
406 irq_en = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
407 if (filter) {
408 /* Only use the match interrupt */
409 hw->filters[RC_FILTER_NORMAL] = *filter;
410 hw->flags |= IMG_IR_F_FILTER;
411 irq_on = IMG_IR_IRQ_DATA_MATCH;
412 irq_en &= ~(IMG_IR_IRQ_DATA_VALID | IMG_IR_IRQ_DATA2_VALID);
413 } else {
414 /* Only use the valid interrupt */
415 hw->flags &= ~IMG_IR_F_FILTER;
416 irq_en &= ~IMG_IR_IRQ_DATA_MATCH;
417 irq_on = IMG_IR_IRQ_DATA_VALID | IMG_IR_IRQ_DATA2_VALID;
418 }
419 irq_en |= irq_on;
420
421 img_ir_write_filter(priv, filter);
422 /* clear any interrupts we're enabling so we don't handle old ones */
423 img_ir_write(priv, IMG_IR_IRQ_CLEAR, irq_on);
424 img_ir_write(priv, IMG_IR_IRQ_ENABLE, irq_en);
425}
426
427/* caller must have lock */
428static void _img_ir_set_wake_filter(struct img_ir_priv *priv,
429 struct img_ir_filter *filter)
430{
431 struct img_ir_priv_hw *hw = &priv->hw;
432 if (filter) {
433 /* Enable wake, and copy filter for later */
434 hw->filters[RC_FILTER_WAKEUP] = *filter;
435 hw->flags |= IMG_IR_F_WAKE;
436 } else {
437 /* Disable wake */
438 hw->flags &= ~IMG_IR_F_WAKE;
439 }
440}
441
442/* Callback for setting scancode filter */
443static int img_ir_set_filter(struct rc_dev *dev, enum rc_filter_type type,
444 struct rc_scancode_filter *sc_filter)
445{
446 struct img_ir_priv *priv = dev->priv;
447 struct img_ir_priv_hw *hw = &priv->hw;
448 struct img_ir_filter filter, *filter_ptr = &filter;
449 int ret = 0;
450
451 dev_dbg(priv->dev, "IR scancode %sfilter=%08x & %08x\n",
452 type == RC_FILTER_WAKEUP ? "wake " : "",
453 sc_filter->data,
454 sc_filter->mask);
455
456 spin_lock_irq(&priv->lock);
457
458 /* filtering can always be disabled */
459 if (!sc_filter->mask) {
460 filter_ptr = NULL;
461 goto set_unlock;
462 }
463
464 /* current decoder must support scancode filtering */
465 if (!hw->decoder || !hw->decoder->filter) {
466 ret = -EINVAL;
467 goto unlock;
468 }
469
470 /* convert scancode filter to raw filter */
471 filter.minlen = 0;
472 filter.maxlen = ~0;
473 ret = hw->decoder->filter(sc_filter, &filter, hw->enabled_protocols);
474 if (ret)
475 goto unlock;
476 dev_dbg(priv->dev, "IR raw %sfilter=%016llx & %016llx\n",
477 type == RC_FILTER_WAKEUP ? "wake " : "",
478 (unsigned long long)filter.data,
479 (unsigned long long)filter.mask);
480
481set_unlock:
482 /* apply raw filters */
483 switch (type) {
484 case RC_FILTER_NORMAL:
485 _img_ir_set_filter(priv, filter_ptr);
486 break;
487 case RC_FILTER_WAKEUP:
488 _img_ir_set_wake_filter(priv, filter_ptr);
489 break;
490 default:
491 ret = -EINVAL;
492 };
493
494unlock:
495 spin_unlock_irq(&priv->lock);
496 return ret;
497}
498
499/**
500 * img_ir_set_decoder() - Set the current decoder.
501 * @priv: IR private data.
502 * @decoder: Decoder to use with immediate effect.
503 * @proto: Protocol bitmap (or 0 to use decoder->type).
504 */
505static void img_ir_set_decoder(struct img_ir_priv *priv,
506 const struct img_ir_decoder *decoder,
507 u64 proto)
508{
509 struct img_ir_priv_hw *hw = &priv->hw;
510 struct rc_dev *rdev = hw->rdev;
511 u32 ir_status, irq_en;
512 spin_lock_irq(&priv->lock);
513
514 /* switch off and disable interrupts */
515 img_ir_write(priv, IMG_IR_CONTROL, 0);
516 irq_en = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
517 img_ir_write(priv, IMG_IR_IRQ_ENABLE, irq_en & IMG_IR_IRQ_EDGE);
518 img_ir_write(priv, IMG_IR_IRQ_CLEAR, IMG_IR_IRQ_ALL & ~IMG_IR_IRQ_EDGE);
519
520 /* ack any data already detected */
521 ir_status = img_ir_read(priv, IMG_IR_STATUS);
522 if (ir_status & (IMG_IR_RXDVAL | IMG_IR_RXDVALD2)) {
523 ir_status &= ~(IMG_IR_RXDVAL | IMG_IR_RXDVALD2);
524 img_ir_write(priv, IMG_IR_STATUS, ir_status);
525 img_ir_read(priv, IMG_IR_DATA_LW);
526 img_ir_read(priv, IMG_IR_DATA_UP);
527 }
528
529 /* stop the end timer and switch back to normal mode */
530 del_timer_sync(&hw->end_timer);
531 hw->mode = IMG_IR_M_NORMAL;
532
533 /* clear the wakeup scancode filter */
534 rdev->scancode_filters[RC_FILTER_WAKEUP].data = 0;
535 rdev->scancode_filters[RC_FILTER_WAKEUP].mask = 0;
536
537 /* clear raw filters */
538 _img_ir_set_filter(priv, NULL);
539 _img_ir_set_wake_filter(priv, NULL);
540
541 /* clear the enabled protocols */
542 hw->enabled_protocols = 0;
543
544 /* switch decoder */
545 hw->decoder = decoder;
546 if (!decoder)
547 goto unlock;
548
549 /* set the enabled protocols */
550 if (!proto)
551 proto = decoder->type;
552 hw->enabled_protocols = proto;
553
554 /* write the new timings */
555 img_ir_decoder_convert(decoder, &hw->reg_timings, hw->clk_hz);
556 img_ir_write_timings(priv, &hw->reg_timings.timings, RC_FILTER_NORMAL);
557
558 /* set up and enable */
559 img_ir_write(priv, IMG_IR_CONTROL, hw->reg_timings.ctrl);
560
561
562unlock:
563 spin_unlock_irq(&priv->lock);
564}
565
566/**
567 * img_ir_decoder_compatable() - Find whether a decoder will work with a device.
568 * @priv: IR private data.
569 * @dec: Decoder to check.
570 *
571 * Returns: true if @dec is compatible with the device @priv refers to.
572 */
573static bool img_ir_decoder_compatible(struct img_ir_priv *priv,
574 const struct img_ir_decoder *dec)
575{
576 unsigned int ct;
577
578 /* don't accept decoders using code types which aren't supported */
579 ct = dec->control.code_type;
580 if (priv->hw.ct_quirks[ct] & IMG_IR_QUIRK_CODE_BROKEN)
581 return false;
582
583 return true;
584}
585
586/**
587 * img_ir_allowed_protos() - Get allowed protocols from global decoder list.
588 * @priv: IR private data.
589 *
590 * Returns: Mask of protocols supported by the device @priv refers to.
591 */
592static u64 img_ir_allowed_protos(struct img_ir_priv *priv)
593{
594 u64 protos = 0;
595 struct img_ir_decoder **decp;
596
597 for (decp = img_ir_decoders; *decp; ++decp) {
598 const struct img_ir_decoder *dec = *decp;
599 if (img_ir_decoder_compatible(priv, dec))
600 protos |= dec->type;
601 }
602 return protos;
603}
604
605/* Callback for changing protocol using sysfs */
606static int img_ir_change_protocol(struct rc_dev *dev, u64 *ir_type)
607{
608 struct img_ir_priv *priv = dev->priv;
609 struct img_ir_priv_hw *hw = &priv->hw;
610 struct rc_dev *rdev = hw->rdev;
611 struct img_ir_decoder **decp;
612 u64 wakeup_protocols;
613
614 if (!*ir_type) {
615 /* disable all protocols */
616 img_ir_set_decoder(priv, NULL, 0);
617 goto success;
618 }
619 for (decp = img_ir_decoders; *decp; ++decp) {
620 const struct img_ir_decoder *dec = *decp;
621 if (!img_ir_decoder_compatible(priv, dec))
622 continue;
623 if (*ir_type & dec->type) {
624 *ir_type &= dec->type;
625 img_ir_set_decoder(priv, dec, *ir_type);
626 goto success;
627 }
628 }
629 return -EINVAL;
630
631success:
632 /*
633 * Only allow matching wakeup protocols for now, and only if filtering
634 * is supported.
635 */
636 wakeup_protocols = *ir_type;
637 if (!hw->decoder || !hw->decoder->filter)
638 wakeup_protocols = 0;
639 rc_set_allowed_wakeup_protocols(rdev, wakeup_protocols);
640 rc_set_enabled_wakeup_protocols(rdev, wakeup_protocols);
641 return 0;
642}
643
644/* Changes ir-core protocol device attribute */
645static void img_ir_set_protocol(struct img_ir_priv *priv, u64 proto)
646{
647 struct rc_dev *rdev = priv->hw.rdev;
648
649 spin_lock_irq(&rdev->rc_map.lock);
650 rdev->rc_map.rc_type = __ffs64(proto);
651 spin_unlock_irq(&rdev->rc_map.lock);
652
653 mutex_lock(&rdev->lock);
654 rc_set_enabled_protocols(rdev, proto);
655 rc_set_allowed_wakeup_protocols(rdev, proto);
656 rc_set_enabled_wakeup_protocols(rdev, proto);
657 mutex_unlock(&rdev->lock);
658}
659
660/* Set up IR decoders */
661static void img_ir_init_decoders(void)
662{
663 struct img_ir_decoder **decp;
664
665 spin_lock(&img_ir_decoders_lock);
666 if (!img_ir_decoders_preprocessed) {
667 for (decp = img_ir_decoders; *decp; ++decp)
668 img_ir_decoder_preprocess(*decp);
669 img_ir_decoders_preprocessed = true;
670 }
671 spin_unlock(&img_ir_decoders_lock);
672}
673
674#ifdef CONFIG_PM_SLEEP
675/**
676 * img_ir_enable_wake() - Switch to wake mode.
677 * @priv: IR private data.
678 *
679 * Returns: non-zero if the IR can wake the system.
680 */
681static int img_ir_enable_wake(struct img_ir_priv *priv)
682{
683 struct img_ir_priv_hw *hw = &priv->hw;
684 int ret = 0;
685
686 spin_lock_irq(&priv->lock);
687 if (hw->flags & IMG_IR_F_WAKE) {
688 /* interrupt only on a match */
689 hw->suspend_irqen = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
690 img_ir_write(priv, IMG_IR_IRQ_ENABLE, IMG_IR_IRQ_DATA_MATCH);
691 img_ir_write_filter(priv, &hw->filters[RC_FILTER_WAKEUP]);
692 img_ir_write_timings(priv, &hw->reg_timings.timings,
693 RC_FILTER_WAKEUP);
694 hw->mode = IMG_IR_M_WAKE;
695 ret = 1;
696 }
697 spin_unlock_irq(&priv->lock);
698 return ret;
699}
700
701/**
702 * img_ir_disable_wake() - Switch out of wake mode.
703 * @priv: IR private data
704 *
705 * Returns: 1 if the hardware should be allowed to wake from a sleep state.
706 * 0 otherwise.
707 */
708static int img_ir_disable_wake(struct img_ir_priv *priv)
709{
710 struct img_ir_priv_hw *hw = &priv->hw;
711 int ret = 0;
712
713 spin_lock_irq(&priv->lock);
714 if (hw->flags & IMG_IR_F_WAKE) {
715 /* restore normal filtering */
716 if (hw->flags & IMG_IR_F_FILTER) {
717 img_ir_write(priv, IMG_IR_IRQ_ENABLE,
718 (hw->suspend_irqen & IMG_IR_IRQ_EDGE) |
719 IMG_IR_IRQ_DATA_MATCH);
720 img_ir_write_filter(priv,
721 &hw->filters[RC_FILTER_NORMAL]);
722 } else {
723 img_ir_write(priv, IMG_IR_IRQ_ENABLE,
724 (hw->suspend_irqen & IMG_IR_IRQ_EDGE) |
725 IMG_IR_IRQ_DATA_VALID |
726 IMG_IR_IRQ_DATA2_VALID);
727 img_ir_write_filter(priv, NULL);
728 }
729 img_ir_write_timings(priv, &hw->reg_timings.timings,
730 RC_FILTER_NORMAL);
731 hw->mode = IMG_IR_M_NORMAL;
732 ret = 1;
733 }
734 spin_unlock_irq(&priv->lock);
735 return ret;
736}
737#endif /* CONFIG_PM_SLEEP */
738
739/* lock must be held */
740static void img_ir_begin_repeat(struct img_ir_priv *priv)
741{
742 struct img_ir_priv_hw *hw = &priv->hw;
743 if (hw->mode == IMG_IR_M_NORMAL) {
744 /* switch to repeat timings */
745 img_ir_write(priv, IMG_IR_CONTROL, 0);
746 hw->mode = IMG_IR_M_REPEATING;
747 img_ir_write_timings(priv, &hw->reg_timings.rtimings,
748 RC_FILTER_NORMAL);
749 img_ir_write(priv, IMG_IR_CONTROL, hw->reg_timings.ctrl);
750 }
751}
752
753/* lock must be held */
754static void img_ir_end_repeat(struct img_ir_priv *priv)
755{
756 struct img_ir_priv_hw *hw = &priv->hw;
757 if (hw->mode == IMG_IR_M_REPEATING) {
758 /* switch to normal timings */
759 img_ir_write(priv, IMG_IR_CONTROL, 0);
760 hw->mode = IMG_IR_M_NORMAL;
761 img_ir_write_timings(priv, &hw->reg_timings.timings,
762 RC_FILTER_NORMAL);
763 img_ir_write(priv, IMG_IR_CONTROL, hw->reg_timings.ctrl);
764 }
765}
766
767/* lock must be held */
768static void img_ir_handle_data(struct img_ir_priv *priv, u32 len, u64 raw)
769{
770 struct img_ir_priv_hw *hw = &priv->hw;
771 const struct img_ir_decoder *dec = hw->decoder;
772 int ret = IMG_IR_SCANCODE;
773 int scancode;
774 if (dec->scancode)
775 ret = dec->scancode(len, raw, &scancode, hw->enabled_protocols);
776 else if (len >= 32)
777 scancode = (u32)raw;
778 else if (len < 32)
779 scancode = (u32)raw & ((1 << len)-1);
780 dev_dbg(priv->dev, "data (%u bits) = %#llx\n",
781 len, (unsigned long long)raw);
782 if (ret == IMG_IR_SCANCODE) {
783 dev_dbg(priv->dev, "decoded scan code %#x\n", scancode);
784 rc_keydown(hw->rdev, scancode, 0);
785 img_ir_end_repeat(priv);
786 } else if (ret == IMG_IR_REPEATCODE) {
787 if (hw->mode == IMG_IR_M_REPEATING) {
788 dev_dbg(priv->dev, "decoded repeat code\n");
789 rc_repeat(hw->rdev);
790 } else {
791 dev_dbg(priv->dev, "decoded unexpected repeat code, ignoring\n");
792 }
793 } else {
794 dev_dbg(priv->dev, "decode failed (%d)\n", ret);
795 return;
796 }
797
798
799 if (dec->repeat) {
800 unsigned long interval;
801
802 img_ir_begin_repeat(priv);
803
804 /* update timer, but allowing for 1/8th tolerance */
805 interval = dec->repeat + (dec->repeat >> 3);
806 mod_timer(&hw->end_timer,
807 jiffies + msecs_to_jiffies(interval));
808 }
809}
810
811/* timer function to end waiting for repeat. */
812static void img_ir_end_timer(unsigned long arg)
813{
814 struct img_ir_priv *priv = (struct img_ir_priv *)arg;
815
816 spin_lock_irq(&priv->lock);
817 img_ir_end_repeat(priv);
818 spin_unlock_irq(&priv->lock);
819}
820
821#ifdef CONFIG_COMMON_CLK
822static void img_ir_change_frequency(struct img_ir_priv *priv,
823 struct clk_notifier_data *change)
824{
825 struct img_ir_priv_hw *hw = &priv->hw;
826
827 dev_dbg(priv->dev, "clk changed %lu HZ -> %lu HZ\n",
828 change->old_rate, change->new_rate);
829
830 spin_lock_irq(&priv->lock);
831 if (hw->clk_hz == change->new_rate)
832 goto unlock;
833 hw->clk_hz = change->new_rate;
834 /* refresh current timings */
835 if (hw->decoder) {
836 img_ir_decoder_convert(hw->decoder, &hw->reg_timings,
837 hw->clk_hz);
838 switch (hw->mode) {
839 case IMG_IR_M_NORMAL:
840 img_ir_write_timings(priv, &hw->reg_timings.timings,
841 RC_FILTER_NORMAL);
842 break;
843 case IMG_IR_M_REPEATING:
844 img_ir_write_timings(priv, &hw->reg_timings.rtimings,
845 RC_FILTER_NORMAL);
846 break;
847#ifdef CONFIG_PM_SLEEP
848 case IMG_IR_M_WAKE:
849 img_ir_write_timings(priv, &hw->reg_timings.timings,
850 RC_FILTER_WAKEUP);
851 break;
852#endif
853 }
854 }
855unlock:
856 spin_unlock_irq(&priv->lock);
857}
858
859static int img_ir_clk_notify(struct notifier_block *self, unsigned long action,
860 void *data)
861{
862 struct img_ir_priv *priv = container_of(self, struct img_ir_priv,
863 hw.clk_nb);
864 switch (action) {
865 case POST_RATE_CHANGE:
866 img_ir_change_frequency(priv, data);
867 break;
868 default:
869 break;
870 }
871 return NOTIFY_OK;
872}
873#endif /* CONFIG_COMMON_CLK */
874
875/* called with priv->lock held */
876void img_ir_isr_hw(struct img_ir_priv *priv, u32 irq_status)
877{
878 struct img_ir_priv_hw *hw = &priv->hw;
879 u32 ir_status, len, lw, up;
880 unsigned int ct;
881
882 /* use the current decoder */
883 if (!hw->decoder)
884 return;
885
886 ir_status = img_ir_read(priv, IMG_IR_STATUS);
887 if (!(ir_status & (IMG_IR_RXDVAL | IMG_IR_RXDVALD2)))
888 return;
889 ir_status &= ~(IMG_IR_RXDVAL | IMG_IR_RXDVALD2);
890 img_ir_write(priv, IMG_IR_STATUS, ir_status);
891
892 len = (ir_status & IMG_IR_RXDLEN) >> IMG_IR_RXDLEN_SHIFT;
893 /* some versions report wrong length for certain code types */
894 ct = hw->decoder->control.code_type;
895 if (hw->ct_quirks[ct] & IMG_IR_QUIRK_CODE_LEN_INCR)
896 ++len;
897
898 lw = img_ir_read(priv, IMG_IR_DATA_LW);
899 up = img_ir_read(priv, IMG_IR_DATA_UP);
900 img_ir_handle_data(priv, len, (u64)up << 32 | lw);
901}
902
903void img_ir_setup_hw(struct img_ir_priv *priv)
904{
905 struct img_ir_decoder **decp;
906
907 if (!priv->hw.rdev)
908 return;
909
910 /* Use the first available decoder (or disable stuff if NULL) */
911 for (decp = img_ir_decoders; *decp; ++decp) {
912 const struct img_ir_decoder *dec = *decp;
913 if (img_ir_decoder_compatible(priv, dec)) {
914 img_ir_set_protocol(priv, dec->type);
915 img_ir_set_decoder(priv, dec, 0);
916 return;
917 }
918 }
919 img_ir_set_decoder(priv, NULL, 0);
920}
921
922/**
923 * img_ir_probe_hw_caps() - Probe capabilities of the hardware.
924 * @priv: IR private data.
925 */
926static void img_ir_probe_hw_caps(struct img_ir_priv *priv)
927{
928 struct img_ir_priv_hw *hw = &priv->hw;
929 /*
930 * When a version of the block becomes available without these quirks,
931 * they'll have to depend on the core revision.
932 */
933 hw->ct_quirks[IMG_IR_CODETYPE_PULSELEN]
934 |= IMG_IR_QUIRK_CODE_LEN_INCR;
935 hw->ct_quirks[IMG_IR_CODETYPE_BIPHASE]
936 |= IMG_IR_QUIRK_CODE_BROKEN;
937 hw->ct_quirks[IMG_IR_CODETYPE_2BITPULSEPOS]
938 |= IMG_IR_QUIRK_CODE_BROKEN;
939}
940
941int img_ir_probe_hw(struct img_ir_priv *priv)
942{
943 struct img_ir_priv_hw *hw = &priv->hw;
944 struct rc_dev *rdev;
945 int error;
946
947 /* Ensure hardware decoders have been preprocessed */
948 img_ir_init_decoders();
949
950 /* Probe hardware capabilities */
951 img_ir_probe_hw_caps(priv);
952
953 /* Set up the end timer */
954 setup_timer(&hw->end_timer, img_ir_end_timer, (unsigned long)priv);
955
956 /* Register a clock notifier */
957 if (!IS_ERR(priv->clk)) {
958 hw->clk_hz = clk_get_rate(priv->clk);
959#ifdef CONFIG_COMMON_CLK
960 hw->clk_nb.notifier_call = img_ir_clk_notify;
961 error = clk_notifier_register(priv->clk, &hw->clk_nb);
962 if (error)
963 dev_warn(priv->dev,
964 "failed to register clock notifier\n");
965#endif
966 } else {
967 hw->clk_hz = 32768;
968 }
969
970 /* Allocate hardware decoder */
971 hw->rdev = rdev = rc_allocate_device();
972 if (!rdev) {
973 dev_err(priv->dev, "cannot allocate input device\n");
974 error = -ENOMEM;
975 goto err_alloc_rc;
976 }
977 rdev->priv = priv;
978 rdev->map_name = RC_MAP_EMPTY;
979 rc_set_allowed_protocols(rdev, img_ir_allowed_protos(priv));
980 rdev->input_name = "IMG Infrared Decoder";
981 rdev->s_filter = img_ir_set_filter;
982
983 /* Register hardware decoder */
984 error = rc_register_device(rdev);
985 if (error) {
986 dev_err(priv->dev, "failed to register IR input device\n");
987 goto err_register_rc;
988 }
989
990 /*
991 * Set this after rc_register_device as no protocols have been
992 * registered yet.
993 */
994 rdev->change_protocol = img_ir_change_protocol;
995
996 device_init_wakeup(priv->dev, 1);
997
998 return 0;
999
1000err_register_rc:
1001 img_ir_set_decoder(priv, NULL, 0);
1002 hw->rdev = NULL;
1003 rc_free_device(rdev);
1004err_alloc_rc:
1005#ifdef CONFIG_COMMON_CLK
1006 if (!IS_ERR(priv->clk))
1007 clk_notifier_unregister(priv->clk, &hw->clk_nb);
1008#endif
1009 return error;
1010}
1011
1012void img_ir_remove_hw(struct img_ir_priv *priv)
1013{
1014 struct img_ir_priv_hw *hw = &priv->hw;
1015 struct rc_dev *rdev = hw->rdev;
1016 if (!rdev)
1017 return;
1018 img_ir_set_decoder(priv, NULL, 0);
1019 hw->rdev = NULL;
1020 rc_unregister_device(rdev);
1021#ifdef CONFIG_COMMON_CLK
1022 if (!IS_ERR(priv->clk))
1023 clk_notifier_unregister(priv->clk, &hw->clk_nb);
1024#endif
1025}
1026
1027#ifdef CONFIG_PM_SLEEP
1028int img_ir_suspend(struct device *dev)
1029{
1030 struct img_ir_priv *priv = dev_get_drvdata(dev);
1031
1032 if (device_may_wakeup(dev) && img_ir_enable_wake(priv))
1033 enable_irq_wake(priv->irq);
1034 return 0;
1035}
1036
1037int img_ir_resume(struct device *dev)
1038{
1039 struct img_ir_priv *priv = dev_get_drvdata(dev);
1040
1041 if (device_may_wakeup(dev) && img_ir_disable_wake(priv))
1042 disable_irq_wake(priv->irq);
1043 return 0;
1044}
1045#endif /* CONFIG_PM_SLEEP */