2 * ImgTec IR Hardware Decoder found in PowerDown Controller.
4 * Copyright 2010-2014 Imagination Technologies Ltd.
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
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>
20 /* Decoders lock (only modified to preprocess them) */
21 static DEFINE_SPINLOCK(img_ir_decoders_lock
);
23 extern struct img_ir_decoder img_ir_nec
;
24 extern struct img_ir_decoder img_ir_jvc
;
25 extern struct img_ir_decoder img_ir_sony
;
26 extern struct img_ir_decoder img_ir_sharp
;
27 extern struct img_ir_decoder img_ir_sanyo
;
29 static bool img_ir_decoders_preprocessed
;
30 static struct img_ir_decoder
*img_ir_decoders
[] = {
31 #ifdef CONFIG_IR_IMG_NEC
34 #ifdef CONFIG_IR_IMG_JVC
37 #ifdef CONFIG_IR_IMG_SONY
40 #ifdef CONFIG_IR_IMG_SHARP
43 #ifdef CONFIG_IR_IMG_SANYO
49 #define IMG_IR_F_FILTER BIT(RC_FILTER_NORMAL) /* enable filtering */
50 #define IMG_IR_F_WAKE BIT(RC_FILTER_WAKEUP) /* enable waking */
52 /* code type quirks */
54 #define IMG_IR_QUIRK_CODE_BROKEN 0x1 /* Decode is broken */
55 #define IMG_IR_QUIRK_CODE_LEN_INCR 0x2 /* Bit length needs increment */
57 /* functions for preprocessing timings, ensuring max is set */
59 static void img_ir_timing_preprocess(struct img_ir_timing_range
*range
,
62 if (range
->max
< range
->min
)
63 range
->max
= range
->min
;
65 /* multiply by unit and convert to microseconds */
66 range
->min
= (range
->min
*unit
)/1000;
67 range
->max
= (range
->max
*unit
+ 999)/1000; /* round up */
71 static void img_ir_symbol_timing_preprocess(struct img_ir_symbol_timing
*timing
,
74 img_ir_timing_preprocess(&timing
->pulse
, unit
);
75 img_ir_timing_preprocess(&timing
->space
, unit
);
78 static void img_ir_timings_preprocess(struct img_ir_timings
*timings
,
81 img_ir_symbol_timing_preprocess(&timings
->ldr
, unit
);
82 img_ir_symbol_timing_preprocess(&timings
->s00
, unit
);
83 img_ir_symbol_timing_preprocess(&timings
->s01
, unit
);
84 img_ir_symbol_timing_preprocess(&timings
->s10
, unit
);
85 img_ir_symbol_timing_preprocess(&timings
->s11
, unit
);
86 /* default s10 and s11 to s00 and s01 if no leader */
88 /* multiply by unit and convert to microseconds (round up) */
89 timings
->ft
.ft_min
= (timings
->ft
.ft_min
*unit
+ 999)/1000;
92 /* functions for filling empty fields with defaults */
94 static void img_ir_timing_defaults(struct img_ir_timing_range
*range
,
95 struct img_ir_timing_range
*defaults
)
98 range
->min
= defaults
->min
;
100 range
->max
= defaults
->max
;
103 static void img_ir_symbol_timing_defaults(struct img_ir_symbol_timing
*timing
,
104 struct img_ir_symbol_timing
*defaults
)
106 img_ir_timing_defaults(&timing
->pulse
, &defaults
->pulse
);
107 img_ir_timing_defaults(&timing
->space
, &defaults
->space
);
110 static void img_ir_timings_defaults(struct img_ir_timings
*timings
,
111 struct img_ir_timings
*defaults
)
113 img_ir_symbol_timing_defaults(&timings
->ldr
, &defaults
->ldr
);
114 img_ir_symbol_timing_defaults(&timings
->s00
, &defaults
->s00
);
115 img_ir_symbol_timing_defaults(&timings
->s01
, &defaults
->s01
);
116 img_ir_symbol_timing_defaults(&timings
->s10
, &defaults
->s10
);
117 img_ir_symbol_timing_defaults(&timings
->s11
, &defaults
->s11
);
118 if (!timings
->ft
.ft_min
)
119 timings
->ft
.ft_min
= defaults
->ft
.ft_min
;
122 /* functions for converting timings to register values */
125 * img_ir_control() - Convert control struct to control register value.
126 * @control: Control data
128 * Returns: The control register value equivalent of @control.
130 static u32
img_ir_control(const struct img_ir_control
*control
)
132 u32 ctrl
= control
->code_type
<< IMG_IR_CODETYPE_SHIFT
;
133 if (control
->decoden
)
134 ctrl
|= IMG_IR_DECODEN
;
136 ctrl
|= IMG_IR_HDRTOG
;
138 ctrl
|= IMG_IR_LDRDEC
;
139 if (control
->decodinpol
)
140 ctrl
|= IMG_IR_DECODINPOL
;
141 if (control
->bitorien
)
142 ctrl
|= IMG_IR_BITORIEN
;
143 if (control
->d1validsel
)
144 ctrl
|= IMG_IR_D1VALIDSEL
;
146 ctrl
|= IMG_IR_BITINV
;
147 if (control
->decodend2
)
148 ctrl
|= IMG_IR_DECODEND2
;
149 if (control
->bitoriend2
)
150 ctrl
|= IMG_IR_BITORIEND2
;
151 if (control
->bitinvd2
)
152 ctrl
|= IMG_IR_BITINVD2
;
157 * img_ir_timing_range_convert() - Convert microsecond range.
158 * @out: Output timing range in clock cycles with a shift.
159 * @in: Input timing range in microseconds.
160 * @tolerance: Tolerance as a fraction of 128 (roughly percent).
161 * @clock_hz: IR clock rate in Hz.
162 * @shift: Shift of output units.
164 * Converts min and max from microseconds to IR clock cycles, applies a
165 * tolerance, and shifts for the register, rounding in the right direction.
166 * Note that in and out can safely be the same object.
168 static void img_ir_timing_range_convert(struct img_ir_timing_range
*out
,
169 const struct img_ir_timing_range
*in
,
170 unsigned int tolerance
,
171 unsigned long clock_hz
,
174 unsigned int min
= in
->min
;
175 unsigned int max
= in
->max
;
176 /* add a tolerance */
177 min
= min
- (min
*tolerance
>> 7);
178 max
= max
+ (max
*tolerance
>> 7);
179 /* convert from microseconds into clock cycles */
180 min
= min
*clock_hz
/ 1000000;
181 max
= (max
*clock_hz
+ 999999) / 1000000; /* round up */
182 /* apply shift and copy to output */
183 out
->min
= min
>> shift
;
184 out
->max
= (max
+ ((1 << shift
) - 1)) >> shift
; /* round up */
188 * img_ir_symbol_timing() - Convert symbol timing struct to register value.
189 * @timing: Symbol timing data
190 * @tolerance: Timing tolerance where 0-128 represents 0-100%
191 * @clock_hz: Frequency of source clock in Hz
192 * @pd_shift: Shift to apply to symbol period
193 * @w_shift: Shift to apply to symbol width
195 * Returns: Symbol timing register value based on arguments.
197 static u32
img_ir_symbol_timing(const struct img_ir_symbol_timing
*timing
,
198 unsigned int tolerance
,
199 unsigned long clock_hz
,
200 unsigned int pd_shift
,
201 unsigned int w_shift
)
203 struct img_ir_timing_range hw_pulse
, hw_period
;
204 /* we calculate period in hw_period, then convert in place */
205 hw_period
.min
= timing
->pulse
.min
+ timing
->space
.min
;
206 hw_period
.max
= timing
->pulse
.max
+ timing
->space
.max
;
207 img_ir_timing_range_convert(&hw_period
, &hw_period
,
208 tolerance
, clock_hz
, pd_shift
);
209 img_ir_timing_range_convert(&hw_pulse
, &timing
->pulse
,
210 tolerance
, clock_hz
, w_shift
);
211 /* construct register value */
212 return (hw_period
.max
<< IMG_IR_PD_MAX_SHIFT
) |
213 (hw_period
.min
<< IMG_IR_PD_MIN_SHIFT
) |
214 (hw_pulse
.max
<< IMG_IR_W_MAX_SHIFT
) |
215 (hw_pulse
.min
<< IMG_IR_W_MIN_SHIFT
);
219 * img_ir_free_timing() - Convert free time timing struct to register value.
220 * @timing: Free symbol timing data
221 * @clock_hz: Source clock frequency in Hz
223 * Returns: Free symbol timing register value.
225 static u32
img_ir_free_timing(const struct img_ir_free_timing
*timing
,
226 unsigned long clock_hz
)
228 unsigned int minlen
, maxlen
, ft_min
;
229 /* minlen is only 5 bits, and round minlen to multiple of 2 */
230 if (timing
->minlen
< 30)
231 minlen
= timing
->minlen
& -2;
234 /* maxlen has maximum value of 48, and round maxlen to multiple of 2 */
235 if (timing
->maxlen
< 48)
236 maxlen
= (timing
->maxlen
+ 1) & -2;
239 /* convert and shift ft_min, rounding upwards */
240 ft_min
= (timing
->ft_min
*clock_hz
+ 999999) / 1000000;
241 ft_min
= (ft_min
+ 7) >> 3;
242 /* construct register value */
243 return (timing
->maxlen
<< IMG_IR_MAXLEN_SHIFT
) |
244 (timing
->minlen
<< IMG_IR_MINLEN_SHIFT
) |
245 (ft_min
<< IMG_IR_FT_MIN_SHIFT
);
249 * img_ir_free_timing_dynamic() - Update free time register value.
250 * @st_ft: Static free time register value from img_ir_free_timing.
251 * @filter: Current filter which may additionally restrict min/max len.
253 * Returns: Updated free time register value based on the current filter.
255 static u32
img_ir_free_timing_dynamic(u32 st_ft
, struct img_ir_filter
*filter
)
257 unsigned int minlen
, maxlen
, newminlen
, newmaxlen
;
259 /* round minlen, maxlen to multiple of 2 */
260 newminlen
= filter
->minlen
& -2;
261 newmaxlen
= (filter
->maxlen
+ 1) & -2;
262 /* extract min/max len from register */
263 minlen
= (st_ft
& IMG_IR_MINLEN
) >> IMG_IR_MINLEN_SHIFT
;
264 maxlen
= (st_ft
& IMG_IR_MAXLEN
) >> IMG_IR_MAXLEN_SHIFT
;
265 /* if the new values are more restrictive, update the register value */
266 if (newminlen
> minlen
) {
267 st_ft
&= ~IMG_IR_MINLEN
;
268 st_ft
|= newminlen
<< IMG_IR_MINLEN_SHIFT
;
270 if (newmaxlen
< maxlen
) {
271 st_ft
&= ~IMG_IR_MAXLEN
;
272 st_ft
|= newmaxlen
<< IMG_IR_MAXLEN_SHIFT
;
278 * img_ir_timings_convert() - Convert timings to register values
279 * @regs: Output timing register values
280 * @timings: Input timing data
281 * @tolerance: Timing tolerance where 0-128 represents 0-100%
282 * @clock_hz: Source clock frequency in Hz
284 static void img_ir_timings_convert(struct img_ir_timing_regvals
*regs
,
285 const struct img_ir_timings
*timings
,
286 unsigned int tolerance
,
287 unsigned int clock_hz
)
289 /* leader symbol timings are divided by 16 */
290 regs
->ldr
= img_ir_symbol_timing(&timings
->ldr
, tolerance
, clock_hz
,
292 /* other symbol timings, pd fields only are divided by 2 */
293 regs
->s00
= img_ir_symbol_timing(&timings
->s00
, tolerance
, clock_hz
,
295 regs
->s01
= img_ir_symbol_timing(&timings
->s01
, tolerance
, clock_hz
,
297 regs
->s10
= img_ir_symbol_timing(&timings
->s10
, tolerance
, clock_hz
,
299 regs
->s11
= img_ir_symbol_timing(&timings
->s11
, tolerance
, clock_hz
,
301 regs
->ft
= img_ir_free_timing(&timings
->ft
, clock_hz
);
305 * img_ir_decoder_preprocess() - Preprocess timings in decoder.
306 * @decoder: Decoder to be preprocessed.
308 * Ensures that the symbol timing ranges are valid with respect to ordering, and
309 * does some fixed conversion on them.
311 static void img_ir_decoder_preprocess(struct img_ir_decoder
*decoder
)
313 /* default tolerance */
314 if (!decoder
->tolerance
)
315 decoder
->tolerance
= 10; /* percent */
316 /* and convert tolerance to fraction out of 128 */
317 decoder
->tolerance
= decoder
->tolerance
* 128 / 100;
319 /* fill in implicit fields */
320 img_ir_timings_preprocess(&decoder
->timings
, decoder
->unit
);
322 /* do the same for repeat timings if applicable */
323 if (decoder
->repeat
) {
324 img_ir_timings_preprocess(&decoder
->rtimings
, decoder
->unit
);
325 img_ir_timings_defaults(&decoder
->rtimings
, &decoder
->timings
);
330 * img_ir_decoder_convert() - Generate internal timings in decoder.
331 * @decoder: Decoder to be converted to internal timings.
332 * @timings: Timing register values.
333 * @clock_hz: IR clock rate in Hz.
335 * Fills out the repeat timings and timing register values for a specific clock
338 static void img_ir_decoder_convert(const struct img_ir_decoder
*decoder
,
339 struct img_ir_reg_timings
*reg_timings
,
340 unsigned int clock_hz
)
342 /* calculate control value */
343 reg_timings
->ctrl
= img_ir_control(&decoder
->control
);
345 /* fill in implicit fields and calculate register values */
346 img_ir_timings_convert(®_timings
->timings
, &decoder
->timings
,
347 decoder
->tolerance
, clock_hz
);
349 /* do the same for repeat timings if applicable */
351 img_ir_timings_convert(®_timings
->rtimings
,
352 &decoder
->rtimings
, decoder
->tolerance
,
357 * img_ir_write_timings() - Write timings to the hardware now
358 * @priv: IR private data
359 * @regs: Timing register values to write
360 * @type: RC filter type (RC_FILTER_*)
362 * Write timing register values @regs to the hardware, taking into account the
363 * current filter which may impose restrictions on the length of the expected
366 static void img_ir_write_timings(struct img_ir_priv
*priv
,
367 struct img_ir_timing_regvals
*regs
,
368 enum rc_filter_type type
)
370 struct img_ir_priv_hw
*hw
= &priv
->hw
;
372 /* filter may be more restrictive to minlen, maxlen */
374 if (hw
->flags
& BIT(type
))
375 ft
= img_ir_free_timing_dynamic(regs
->ft
, &hw
->filters
[type
]);
376 /* write to registers */
377 img_ir_write(priv
, IMG_IR_LEAD_SYMB_TIMING
, regs
->ldr
);
378 img_ir_write(priv
, IMG_IR_S00_SYMB_TIMING
, regs
->s00
);
379 img_ir_write(priv
, IMG_IR_S01_SYMB_TIMING
, regs
->s01
);
380 img_ir_write(priv
, IMG_IR_S10_SYMB_TIMING
, regs
->s10
);
381 img_ir_write(priv
, IMG_IR_S11_SYMB_TIMING
, regs
->s11
);
382 img_ir_write(priv
, IMG_IR_FREE_SYMB_TIMING
, ft
);
383 dev_dbg(priv
->dev
, "timings: ldr=%#x, s=[%#x, %#x, %#x, %#x], ft=%#x\n",
384 regs
->ldr
, regs
->s00
, regs
->s01
, regs
->s10
, regs
->s11
, ft
);
387 static void img_ir_write_filter(struct img_ir_priv
*priv
,
388 struct img_ir_filter
*filter
)
391 dev_dbg(priv
->dev
, "IR filter=%016llx & %016llx\n",
392 (unsigned long long)filter
->data
,
393 (unsigned long long)filter
->mask
);
394 img_ir_write(priv
, IMG_IR_IRQ_MSG_DATA_LW
, (u32
)filter
->data
);
395 img_ir_write(priv
, IMG_IR_IRQ_MSG_DATA_UP
, (u32
)(filter
->data
397 img_ir_write(priv
, IMG_IR_IRQ_MSG_MASK_LW
, (u32
)filter
->mask
);
398 img_ir_write(priv
, IMG_IR_IRQ_MSG_MASK_UP
, (u32
)(filter
->mask
401 dev_dbg(priv
->dev
, "IR clearing filter\n");
402 img_ir_write(priv
, IMG_IR_IRQ_MSG_MASK_LW
, 0);
403 img_ir_write(priv
, IMG_IR_IRQ_MSG_MASK_UP
, 0);
407 /* caller must have lock */
408 static void _img_ir_set_filter(struct img_ir_priv
*priv
,
409 struct img_ir_filter
*filter
)
411 struct img_ir_priv_hw
*hw
= &priv
->hw
;
414 irq_en
= img_ir_read(priv
, IMG_IR_IRQ_ENABLE
);
416 /* Only use the match interrupt */
417 hw
->filters
[RC_FILTER_NORMAL
] = *filter
;
418 hw
->flags
|= IMG_IR_F_FILTER
;
419 irq_on
= IMG_IR_IRQ_DATA_MATCH
;
420 irq_en
&= ~(IMG_IR_IRQ_DATA_VALID
| IMG_IR_IRQ_DATA2_VALID
);
422 /* Only use the valid interrupt */
423 hw
->flags
&= ~IMG_IR_F_FILTER
;
424 irq_en
&= ~IMG_IR_IRQ_DATA_MATCH
;
425 irq_on
= IMG_IR_IRQ_DATA_VALID
| IMG_IR_IRQ_DATA2_VALID
;
429 img_ir_write_filter(priv
, filter
);
430 /* clear any interrupts we're enabling so we don't handle old ones */
431 img_ir_write(priv
, IMG_IR_IRQ_CLEAR
, irq_on
);
432 img_ir_write(priv
, IMG_IR_IRQ_ENABLE
, irq_en
);
435 /* caller must have lock */
436 static void _img_ir_set_wake_filter(struct img_ir_priv
*priv
,
437 struct img_ir_filter
*filter
)
439 struct img_ir_priv_hw
*hw
= &priv
->hw
;
441 /* Enable wake, and copy filter for later */
442 hw
->filters
[RC_FILTER_WAKEUP
] = *filter
;
443 hw
->flags
|= IMG_IR_F_WAKE
;
446 hw
->flags
&= ~IMG_IR_F_WAKE
;
450 /* Callback for setting scancode filter */
451 static int img_ir_set_filter(struct rc_dev
*dev
, enum rc_filter_type type
,
452 struct rc_scancode_filter
*sc_filter
)
454 struct img_ir_priv
*priv
= dev
->priv
;
455 struct img_ir_priv_hw
*hw
= &priv
->hw
;
456 struct img_ir_filter filter
, *filter_ptr
= &filter
;
459 dev_dbg(priv
->dev
, "IR scancode %sfilter=%08x & %08x\n",
460 type
== RC_FILTER_WAKEUP
? "wake " : "",
464 spin_lock_irq(&priv
->lock
);
466 /* filtering can always be disabled */
467 if (!sc_filter
->mask
) {
472 /* current decoder must support scancode filtering */
473 if (!hw
->decoder
|| !hw
->decoder
->filter
) {
478 /* convert scancode filter to raw filter */
481 ret
= hw
->decoder
->filter(sc_filter
, &filter
, hw
->enabled_protocols
);
484 dev_dbg(priv
->dev
, "IR raw %sfilter=%016llx & %016llx\n",
485 type
== RC_FILTER_WAKEUP
? "wake " : "",
486 (unsigned long long)filter
.data
,
487 (unsigned long long)filter
.mask
);
490 /* apply raw filters */
492 case RC_FILTER_NORMAL
:
493 _img_ir_set_filter(priv
, filter_ptr
);
495 case RC_FILTER_WAKEUP
:
496 _img_ir_set_wake_filter(priv
, filter_ptr
);
503 spin_unlock_irq(&priv
->lock
);
508 * img_ir_set_decoder() - Set the current decoder.
509 * @priv: IR private data.
510 * @decoder: Decoder to use with immediate effect.
511 * @proto: Protocol bitmap (or 0 to use decoder->type).
513 static void img_ir_set_decoder(struct img_ir_priv
*priv
,
514 const struct img_ir_decoder
*decoder
,
517 struct img_ir_priv_hw
*hw
= &priv
->hw
;
518 struct rc_dev
*rdev
= hw
->rdev
;
519 u32 ir_status
, irq_en
;
520 spin_lock_irq(&priv
->lock
);
522 /* switch off and disable interrupts */
523 img_ir_write(priv
, IMG_IR_CONTROL
, 0);
524 irq_en
= img_ir_read(priv
, IMG_IR_IRQ_ENABLE
);
525 img_ir_write(priv
, IMG_IR_IRQ_ENABLE
, irq_en
& IMG_IR_IRQ_EDGE
);
526 img_ir_write(priv
, IMG_IR_IRQ_CLEAR
, IMG_IR_IRQ_ALL
& ~IMG_IR_IRQ_EDGE
);
528 /* ack any data already detected */
529 ir_status
= img_ir_read(priv
, IMG_IR_STATUS
);
530 if (ir_status
& (IMG_IR_RXDVAL
| IMG_IR_RXDVALD2
)) {
531 ir_status
&= ~(IMG_IR_RXDVAL
| IMG_IR_RXDVALD2
);
532 img_ir_write(priv
, IMG_IR_STATUS
, ir_status
);
533 img_ir_read(priv
, IMG_IR_DATA_LW
);
534 img_ir_read(priv
, IMG_IR_DATA_UP
);
537 /* stop the end timer and switch back to normal mode */
538 del_timer_sync(&hw
->end_timer
);
539 hw
->mode
= IMG_IR_M_NORMAL
;
541 /* clear the wakeup scancode filter */
542 rdev
->scancode_filters
[RC_FILTER_WAKEUP
].data
= 0;
543 rdev
->scancode_filters
[RC_FILTER_WAKEUP
].mask
= 0;
545 /* clear raw filters */
546 _img_ir_set_filter(priv
, NULL
);
547 _img_ir_set_wake_filter(priv
, NULL
);
549 /* clear the enabled protocols */
550 hw
->enabled_protocols
= 0;
553 hw
->decoder
= decoder
;
557 /* set the enabled protocols */
559 proto
= decoder
->type
;
560 hw
->enabled_protocols
= proto
;
562 /* write the new timings */
563 img_ir_decoder_convert(decoder
, &hw
->reg_timings
, hw
->clk_hz
);
564 img_ir_write_timings(priv
, &hw
->reg_timings
.timings
, RC_FILTER_NORMAL
);
566 /* set up and enable */
567 img_ir_write(priv
, IMG_IR_CONTROL
, hw
->reg_timings
.ctrl
);
571 spin_unlock_irq(&priv
->lock
);
575 * img_ir_decoder_compatable() - Find whether a decoder will work with a device.
576 * @priv: IR private data.
577 * @dec: Decoder to check.
579 * Returns: true if @dec is compatible with the device @priv refers to.
581 static bool img_ir_decoder_compatible(struct img_ir_priv
*priv
,
582 const struct img_ir_decoder
*dec
)
586 /* don't accept decoders using code types which aren't supported */
587 ct
= dec
->control
.code_type
;
588 if (priv
->hw
.ct_quirks
[ct
] & IMG_IR_QUIRK_CODE_BROKEN
)
595 * img_ir_allowed_protos() - Get allowed protocols from global decoder list.
596 * @priv: IR private data.
598 * Returns: Mask of protocols supported by the device @priv refers to.
600 static u64
img_ir_allowed_protos(struct img_ir_priv
*priv
)
603 struct img_ir_decoder
**decp
;
605 for (decp
= img_ir_decoders
; *decp
; ++decp
) {
606 const struct img_ir_decoder
*dec
= *decp
;
607 if (img_ir_decoder_compatible(priv
, dec
))
613 /* Callback for changing protocol using sysfs */
614 static int img_ir_change_protocol(struct rc_dev
*dev
, u64
*ir_type
)
616 struct img_ir_priv
*priv
= dev
->priv
;
617 struct img_ir_priv_hw
*hw
= &priv
->hw
;
618 struct rc_dev
*rdev
= hw
->rdev
;
619 struct img_ir_decoder
**decp
;
620 u64 wakeup_protocols
;
623 /* disable all protocols */
624 img_ir_set_decoder(priv
, NULL
, 0);
627 for (decp
= img_ir_decoders
; *decp
; ++decp
) {
628 const struct img_ir_decoder
*dec
= *decp
;
629 if (!img_ir_decoder_compatible(priv
, dec
))
631 if (*ir_type
& dec
->type
) {
632 *ir_type
&= dec
->type
;
633 img_ir_set_decoder(priv
, dec
, *ir_type
);
641 * Only allow matching wakeup protocols for now, and only if filtering
644 wakeup_protocols
= *ir_type
;
645 if (!hw
->decoder
|| !hw
->decoder
->filter
)
646 wakeup_protocols
= 0;
647 rc_set_allowed_wakeup_protocols(rdev
, wakeup_protocols
);
648 rc_set_enabled_wakeup_protocols(rdev
, wakeup_protocols
);
652 /* Changes ir-core protocol device attribute */
653 static void img_ir_set_protocol(struct img_ir_priv
*priv
, u64 proto
)
655 struct rc_dev
*rdev
= priv
->hw
.rdev
;
657 spin_lock_irq(&rdev
->rc_map
.lock
);
658 rdev
->rc_map
.rc_type
= __ffs64(proto
);
659 spin_unlock_irq(&rdev
->rc_map
.lock
);
661 mutex_lock(&rdev
->lock
);
662 rc_set_enabled_protocols(rdev
, proto
);
663 rc_set_allowed_wakeup_protocols(rdev
, proto
);
664 rc_set_enabled_wakeup_protocols(rdev
, proto
);
665 mutex_unlock(&rdev
->lock
);
668 /* Set up IR decoders */
669 static void img_ir_init_decoders(void)
671 struct img_ir_decoder
**decp
;
673 spin_lock(&img_ir_decoders_lock
);
674 if (!img_ir_decoders_preprocessed
) {
675 for (decp
= img_ir_decoders
; *decp
; ++decp
)
676 img_ir_decoder_preprocess(*decp
);
677 img_ir_decoders_preprocessed
= true;
679 spin_unlock(&img_ir_decoders_lock
);
682 #ifdef CONFIG_PM_SLEEP
684 * img_ir_enable_wake() - Switch to wake mode.
685 * @priv: IR private data.
687 * Returns: non-zero if the IR can wake the system.
689 static int img_ir_enable_wake(struct img_ir_priv
*priv
)
691 struct img_ir_priv_hw
*hw
= &priv
->hw
;
694 spin_lock_irq(&priv
->lock
);
695 if (hw
->flags
& IMG_IR_F_WAKE
) {
696 /* interrupt only on a match */
697 hw
->suspend_irqen
= img_ir_read(priv
, IMG_IR_IRQ_ENABLE
);
698 img_ir_write(priv
, IMG_IR_IRQ_ENABLE
, IMG_IR_IRQ_DATA_MATCH
);
699 img_ir_write_filter(priv
, &hw
->filters
[RC_FILTER_WAKEUP
]);
700 img_ir_write_timings(priv
, &hw
->reg_timings
.timings
,
702 hw
->mode
= IMG_IR_M_WAKE
;
705 spin_unlock_irq(&priv
->lock
);
710 * img_ir_disable_wake() - Switch out of wake mode.
711 * @priv: IR private data
713 * Returns: 1 if the hardware should be allowed to wake from a sleep state.
716 static int img_ir_disable_wake(struct img_ir_priv
*priv
)
718 struct img_ir_priv_hw
*hw
= &priv
->hw
;
721 spin_lock_irq(&priv
->lock
);
722 if (hw
->flags
& IMG_IR_F_WAKE
) {
723 /* restore normal filtering */
724 if (hw
->flags
& IMG_IR_F_FILTER
) {
725 img_ir_write(priv
, IMG_IR_IRQ_ENABLE
,
726 (hw
->suspend_irqen
& IMG_IR_IRQ_EDGE
) |
727 IMG_IR_IRQ_DATA_MATCH
);
728 img_ir_write_filter(priv
,
729 &hw
->filters
[RC_FILTER_NORMAL
]);
731 img_ir_write(priv
, IMG_IR_IRQ_ENABLE
,
732 (hw
->suspend_irqen
& IMG_IR_IRQ_EDGE
) |
733 IMG_IR_IRQ_DATA_VALID
|
734 IMG_IR_IRQ_DATA2_VALID
);
735 img_ir_write_filter(priv
, NULL
);
737 img_ir_write_timings(priv
, &hw
->reg_timings
.timings
,
739 hw
->mode
= IMG_IR_M_NORMAL
;
742 spin_unlock_irq(&priv
->lock
);
745 #endif /* CONFIG_PM_SLEEP */
747 /* lock must be held */
748 static void img_ir_begin_repeat(struct img_ir_priv
*priv
)
750 struct img_ir_priv_hw
*hw
= &priv
->hw
;
751 if (hw
->mode
== IMG_IR_M_NORMAL
) {
752 /* switch to repeat timings */
753 img_ir_write(priv
, IMG_IR_CONTROL
, 0);
754 hw
->mode
= IMG_IR_M_REPEATING
;
755 img_ir_write_timings(priv
, &hw
->reg_timings
.rtimings
,
757 img_ir_write(priv
, IMG_IR_CONTROL
, hw
->reg_timings
.ctrl
);
761 /* lock must be held */
762 static void img_ir_end_repeat(struct img_ir_priv
*priv
)
764 struct img_ir_priv_hw
*hw
= &priv
->hw
;
765 if (hw
->mode
== IMG_IR_M_REPEATING
) {
766 /* switch to normal timings */
767 img_ir_write(priv
, IMG_IR_CONTROL
, 0);
768 hw
->mode
= IMG_IR_M_NORMAL
;
769 img_ir_write_timings(priv
, &hw
->reg_timings
.timings
,
771 img_ir_write(priv
, IMG_IR_CONTROL
, hw
->reg_timings
.ctrl
);
775 /* lock must be held */
776 static void img_ir_handle_data(struct img_ir_priv
*priv
, u32 len
, u64 raw
)
778 struct img_ir_priv_hw
*hw
= &priv
->hw
;
779 const struct img_ir_decoder
*dec
= hw
->decoder
;
780 int ret
= IMG_IR_SCANCODE
;
783 ret
= dec
->scancode(len
, raw
, &scancode
, hw
->enabled_protocols
);
787 scancode
= (u32
)raw
& ((1 << len
)-1);
788 dev_dbg(priv
->dev
, "data (%u bits) = %#llx\n",
789 len
, (unsigned long long)raw
);
790 if (ret
== IMG_IR_SCANCODE
) {
791 dev_dbg(priv
->dev
, "decoded scan code %#x\n", scancode
);
792 rc_keydown(hw
->rdev
, scancode
, 0);
793 img_ir_end_repeat(priv
);
794 } else if (ret
== IMG_IR_REPEATCODE
) {
795 if (hw
->mode
== IMG_IR_M_REPEATING
) {
796 dev_dbg(priv
->dev
, "decoded repeat code\n");
799 dev_dbg(priv
->dev
, "decoded unexpected repeat code, ignoring\n");
802 dev_dbg(priv
->dev
, "decode failed (%d)\n", ret
);
808 unsigned long interval
;
810 img_ir_begin_repeat(priv
);
812 /* update timer, but allowing for 1/8th tolerance */
813 interval
= dec
->repeat
+ (dec
->repeat
>> 3);
814 mod_timer(&hw
->end_timer
,
815 jiffies
+ msecs_to_jiffies(interval
));
819 /* timer function to end waiting for repeat. */
820 static void img_ir_end_timer(unsigned long arg
)
822 struct img_ir_priv
*priv
= (struct img_ir_priv
*)arg
;
824 spin_lock_irq(&priv
->lock
);
825 img_ir_end_repeat(priv
);
826 spin_unlock_irq(&priv
->lock
);
829 #ifdef CONFIG_COMMON_CLK
830 static void img_ir_change_frequency(struct img_ir_priv
*priv
,
831 struct clk_notifier_data
*change
)
833 struct img_ir_priv_hw
*hw
= &priv
->hw
;
835 dev_dbg(priv
->dev
, "clk changed %lu HZ -> %lu HZ\n",
836 change
->old_rate
, change
->new_rate
);
838 spin_lock_irq(&priv
->lock
);
839 if (hw
->clk_hz
== change
->new_rate
)
841 hw
->clk_hz
= change
->new_rate
;
842 /* refresh current timings */
844 img_ir_decoder_convert(hw
->decoder
, &hw
->reg_timings
,
847 case IMG_IR_M_NORMAL
:
848 img_ir_write_timings(priv
, &hw
->reg_timings
.timings
,
851 case IMG_IR_M_REPEATING
:
852 img_ir_write_timings(priv
, &hw
->reg_timings
.rtimings
,
855 #ifdef CONFIG_PM_SLEEP
857 img_ir_write_timings(priv
, &hw
->reg_timings
.timings
,
864 spin_unlock_irq(&priv
->lock
);
867 static int img_ir_clk_notify(struct notifier_block
*self
, unsigned long action
,
870 struct img_ir_priv
*priv
= container_of(self
, struct img_ir_priv
,
873 case POST_RATE_CHANGE
:
874 img_ir_change_frequency(priv
, data
);
881 #endif /* CONFIG_COMMON_CLK */
883 /* called with priv->lock held */
884 void img_ir_isr_hw(struct img_ir_priv
*priv
, u32 irq_status
)
886 struct img_ir_priv_hw
*hw
= &priv
->hw
;
887 u32 ir_status
, len
, lw
, up
;
890 /* use the current decoder */
894 ir_status
= img_ir_read(priv
, IMG_IR_STATUS
);
895 if (!(ir_status
& (IMG_IR_RXDVAL
| IMG_IR_RXDVALD2
)))
897 ir_status
&= ~(IMG_IR_RXDVAL
| IMG_IR_RXDVALD2
);
898 img_ir_write(priv
, IMG_IR_STATUS
, ir_status
);
900 len
= (ir_status
& IMG_IR_RXDLEN
) >> IMG_IR_RXDLEN_SHIFT
;
901 /* some versions report wrong length for certain code types */
902 ct
= hw
->decoder
->control
.code_type
;
903 if (hw
->ct_quirks
[ct
] & IMG_IR_QUIRK_CODE_LEN_INCR
)
906 lw
= img_ir_read(priv
, IMG_IR_DATA_LW
);
907 up
= img_ir_read(priv
, IMG_IR_DATA_UP
);
908 img_ir_handle_data(priv
, len
, (u64
)up
<< 32 | lw
);
911 void img_ir_setup_hw(struct img_ir_priv
*priv
)
913 struct img_ir_decoder
**decp
;
918 /* Use the first available decoder (or disable stuff if NULL) */
919 for (decp
= img_ir_decoders
; *decp
; ++decp
) {
920 const struct img_ir_decoder
*dec
= *decp
;
921 if (img_ir_decoder_compatible(priv
, dec
)) {
922 img_ir_set_protocol(priv
, dec
->type
);
923 img_ir_set_decoder(priv
, dec
, 0);
927 img_ir_set_decoder(priv
, NULL
, 0);
931 * img_ir_probe_hw_caps() - Probe capabilities of the hardware.
932 * @priv: IR private data.
934 static void img_ir_probe_hw_caps(struct img_ir_priv
*priv
)
936 struct img_ir_priv_hw
*hw
= &priv
->hw
;
938 * When a version of the block becomes available without these quirks,
939 * they'll have to depend on the core revision.
941 hw
->ct_quirks
[IMG_IR_CODETYPE_PULSELEN
]
942 |= IMG_IR_QUIRK_CODE_LEN_INCR
;
943 hw
->ct_quirks
[IMG_IR_CODETYPE_BIPHASE
]
944 |= IMG_IR_QUIRK_CODE_BROKEN
;
945 hw
->ct_quirks
[IMG_IR_CODETYPE_2BITPULSEPOS
]
946 |= IMG_IR_QUIRK_CODE_BROKEN
;
949 int img_ir_probe_hw(struct img_ir_priv
*priv
)
951 struct img_ir_priv_hw
*hw
= &priv
->hw
;
955 /* Ensure hardware decoders have been preprocessed */
956 img_ir_init_decoders();
958 /* Probe hardware capabilities */
959 img_ir_probe_hw_caps(priv
);
961 /* Set up the end timer */
962 setup_timer(&hw
->end_timer
, img_ir_end_timer
, (unsigned long)priv
);
964 /* Register a clock notifier */
965 if (!IS_ERR(priv
->clk
)) {
966 hw
->clk_hz
= clk_get_rate(priv
->clk
);
967 #ifdef CONFIG_COMMON_CLK
968 hw
->clk_nb
.notifier_call
= img_ir_clk_notify
;
969 error
= clk_notifier_register(priv
->clk
, &hw
->clk_nb
);
972 "failed to register clock notifier\n");
978 /* Allocate hardware decoder */
979 hw
->rdev
= rdev
= rc_allocate_device();
981 dev_err(priv
->dev
, "cannot allocate input device\n");
986 rdev
->map_name
= RC_MAP_EMPTY
;
987 rc_set_allowed_protocols(rdev
, img_ir_allowed_protos(priv
));
988 rdev
->input_name
= "IMG Infrared Decoder";
989 rdev
->s_filter
= img_ir_set_filter
;
991 /* Register hardware decoder */
992 error
= rc_register_device(rdev
);
994 dev_err(priv
->dev
, "failed to register IR input device\n");
995 goto err_register_rc
;
999 * Set this after rc_register_device as no protocols have been
1002 rdev
->change_protocol
= img_ir_change_protocol
;
1004 device_init_wakeup(priv
->dev
, 1);
1009 img_ir_set_decoder(priv
, NULL
, 0);
1011 rc_free_device(rdev
);
1013 #ifdef CONFIG_COMMON_CLK
1014 if (!IS_ERR(priv
->clk
))
1015 clk_notifier_unregister(priv
->clk
, &hw
->clk_nb
);
1020 void img_ir_remove_hw(struct img_ir_priv
*priv
)
1022 struct img_ir_priv_hw
*hw
= &priv
->hw
;
1023 struct rc_dev
*rdev
= hw
->rdev
;
1026 img_ir_set_decoder(priv
, NULL
, 0);
1028 rc_unregister_device(rdev
);
1029 #ifdef CONFIG_COMMON_CLK
1030 if (!IS_ERR(priv
->clk
))
1031 clk_notifier_unregister(priv
->clk
, &hw
->clk_nb
);
1035 #ifdef CONFIG_PM_SLEEP
1036 int img_ir_suspend(struct device
*dev
)
1038 struct img_ir_priv
*priv
= dev_get_drvdata(dev
);
1040 if (device_may_wakeup(dev
) && img_ir_enable_wake(priv
))
1041 enable_irq_wake(priv
->irq
);
1045 int img_ir_resume(struct device
*dev
)
1047 struct img_ir_priv
*priv
= dev_get_drvdata(dev
);
1049 if (device_may_wakeup(dev
) && img_ir_disable_wake(priv
))
1050 disable_irq_wake(priv
->irq
);
1053 #endif /* CONFIG_PM_SLEEP */