1 /*
2  * ImgTec IR Hardware Decoder found in PowerDown Controller.
3  *
4  * Copyright 2010-2014 Imagination Technologies Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  */
11 
12 #ifndef _IMG_IR_HW_H_
13 #define _IMG_IR_HW_H_
14 
15 #include <linux/kernel.h>
16 #include <media/rc-core.h>
17 
18 /* constants */
19 
20 #define IMG_IR_CODETYPE_PULSELEN	0x0	/* Sony */
21 #define IMG_IR_CODETYPE_PULSEDIST	0x1	/* NEC, Toshiba, Micom, Sharp */
22 #define IMG_IR_CODETYPE_BIPHASE		0x2	/* RC-5/6 */
23 #define IMG_IR_CODETYPE_2BITPULSEPOS	0x3	/* RC-MM */
24 
25 
26 /* Timing information */
27 
28 /**
29  * struct img_ir_control - Decoder control settings
30  * @decoden:	Primary decoder enable
31  * @code_type:	Decode type (see IMG_IR_CODETYPE_*)
32  * @hdrtog:	Detect header toggle symbol after leader symbol
33  * @ldrdec:	Don't discard leader if maximum width reached
34  * @decodinpol:	Decoder input polarity (1=active high)
35  * @bitorien:	Bit orientation (1=MSB first)
36  * @d1validsel:	Decoder 2 takes over if it detects valid data
37  * @bitinv:	Bit inversion switch (1=don't invert)
38  * @decodend2:	Secondary decoder enable (no leader symbol)
39  * @bitoriend2:	Bit orientation (1=MSB first)
40  * @bitinvd2:	Secondary decoder bit inversion switch (1=don't invert)
41  */
42 struct img_ir_control {
43 	unsigned decoden:1;
44 	unsigned code_type:2;
45 	unsigned hdrtog:1;
46 	unsigned ldrdec:1;
47 	unsigned decodinpol:1;
48 	unsigned bitorien:1;
49 	unsigned d1validsel:1;
50 	unsigned bitinv:1;
51 	unsigned decodend2:1;
52 	unsigned bitoriend2:1;
53 	unsigned bitinvd2:1;
54 };
55 
56 /**
57  * struct img_ir_timing_range - range of timing values
58  * @min:	Minimum timing value
59  * @max:	Maximum timing value (if < @min, this will be set to @min during
60  *		preprocessing step, so it is normally not explicitly initialised
61  *		and is taken care of by the tolerance)
62  */
63 struct img_ir_timing_range {
64 	u16 min;
65 	u16 max;
66 };
67 
68 /**
69  * struct img_ir_symbol_timing - timing data for a symbol
70  * @pulse:	Timing range for the length of the pulse in this symbol
71  * @space:	Timing range for the length of the space in this symbol
72  */
73 struct img_ir_symbol_timing {
74 	struct img_ir_timing_range pulse;
75 	struct img_ir_timing_range space;
76 };
77 
78 /**
79  * struct img_ir_free_timing - timing data for free time symbol
80  * @minlen:	Minimum number of bits of data
81  * @maxlen:	Maximum number of bits of data
82  * @ft_min:	Minimum free time after message
83  */
84 struct img_ir_free_timing {
85 	/* measured in bits */
86 	u8 minlen;
87 	u8 maxlen;
88 	u16 ft_min;
89 };
90 
91 /**
92  * struct img_ir_timings - Timing values.
93  * @ldr:	Leader symbol timing data
94  * @s00:	Zero symbol timing data for primary decoder
95  * @s01:	One symbol timing data for primary decoder
96  * @s10:	Zero symbol timing data for secondary (no leader symbol) decoder
97  * @s11:	One symbol timing data for secondary (no leader symbol) decoder
98  * @ft:		Free time symbol timing data
99  */
100 struct img_ir_timings {
101 	struct img_ir_symbol_timing ldr, s00, s01, s10, s11;
102 	struct img_ir_free_timing ft;
103 };
104 
105 /**
106  * struct img_ir_filter - Filter IR events.
107  * @data:	Data to match.
108  * @mask:	Mask of bits to compare.
109  * @minlen:	Additional minimum number of bits.
110  * @maxlen:	Additional maximum number of bits.
111  */
112 struct img_ir_filter {
113 	u64 data;
114 	u64 mask;
115 	u8 minlen;
116 	u8 maxlen;
117 };
118 
119 /**
120  * struct img_ir_timing_regvals - Calculated timing register values.
121  * @ldr:	Leader symbol timing register value
122  * @s00:	Zero symbol timing register value for primary decoder
123  * @s01:	One symbol timing register value for primary decoder
124  * @s10:	Zero symbol timing register value for secondary decoder
125  * @s11:	One symbol timing register value for secondary decoder
126  * @ft:		Free time symbol timing register value
127  */
128 struct img_ir_timing_regvals {
129 	u32 ldr, s00, s01, s10, s11, ft;
130 };
131 
132 #define IMG_IR_SCANCODE		0	/* new scancode */
133 #define IMG_IR_REPEATCODE	1	/* repeat the previous code */
134 
135 /**
136  * struct img_ir_decoder - Decoder settings for an IR protocol.
137  * @type:	Protocol types bitmap.
138  * @tolerance:	Timing tolerance as a percentage (default 10%).
139  * @unit:	Unit of timings in nanoseconds (default 1 us).
140  * @timings:	Primary timings
141  * @rtimings:	Additional override timings while waiting for repeats.
142  * @repeat:	Maximum repeat interval (always in milliseconds).
143  * @control:	Control flags.
144  *
145  * @scancode:	Pointer to function to convert the IR data into a scancode (it
146  *		must be safe to execute in interrupt context).
147  *		Returns IMG_IR_SCANCODE to emit new scancode.
148  *		Returns IMG_IR_REPEATCODE to repeat previous code.
149  *		Returns -errno (e.g. -EINVAL) on error.
150  * @filter:	Pointer to function to convert scancode filter to raw hardware
151  *		filter. The minlen and maxlen fields will have been initialised
152  *		to the maximum range.
153  */
154 struct img_ir_decoder {
155 	/* core description */
156 	u64				type;
157 	unsigned int			tolerance;
158 	unsigned int			unit;
159 	struct img_ir_timings		timings;
160 	struct img_ir_timings		rtimings;
161 	unsigned int			repeat;
162 	struct img_ir_control		control;
163 
164 	/* scancode logic */
165 	int (*scancode)(int len, u64 raw, enum rc_type *protocol,
166 			u32 *scancode, u64 enabled_protocols);
167 	int (*filter)(const struct rc_scancode_filter *in,
168 		      struct img_ir_filter *out, u64 protocols);
169 };
170 
171 /**
172  * struct img_ir_reg_timings - Reg values for decoder timings at clock rate.
173  * @ctrl:	Processed control register value.
174  * @timings:	Processed primary timings.
175  * @rtimings:	Processed repeat timings.
176  */
177 struct img_ir_reg_timings {
178 	u32				ctrl;
179 	struct img_ir_timing_regvals	timings;
180 	struct img_ir_timing_regvals	rtimings;
181 };
182 
183 int img_ir_register_decoder(struct img_ir_decoder *dec);
184 void img_ir_unregister_decoder(struct img_ir_decoder *dec);
185 
186 struct img_ir_priv;
187 
188 #ifdef CONFIG_IR_IMG_HW
189 
190 enum img_ir_mode {
191 	IMG_IR_M_NORMAL,
192 	IMG_IR_M_REPEATING,
193 #ifdef CONFIG_PM_SLEEP
194 	IMG_IR_M_WAKE,
195 #endif
196 };
197 
198 /**
199  * struct img_ir_priv_hw - Private driver data for hardware decoder.
200  * @ct_quirks:		Quirk bits for each code type.
201  * @rdev:		Remote control device
202  * @clk_nb:		Notifier block for clock notify events.
203  * @end_timer:		Timer until repeat timeout.
204  * @decoder:		Current decoder settings.
205  * @enabled_protocols:	Currently enabled protocols.
206  * @clk_hz:		Current core clock rate in Hz.
207  * @reg_timings:	Timing reg values for decoder at clock rate.
208  * @flags:		IMG_IR_F_*.
209  * @filters:		HW filters (derived from scancode filters).
210  * @mode:		Current decode mode.
211  * @suspend_irqen:	Saved IRQ enable mask over suspend.
212  */
213 struct img_ir_priv_hw {
214 	unsigned int			ct_quirks[4];
215 	struct rc_dev			*rdev;
216 	struct notifier_block		clk_nb;
217 	struct timer_list		end_timer;
218 	const struct img_ir_decoder	*decoder;
219 	u64				enabled_protocols;
220 	unsigned long			clk_hz;
221 	struct img_ir_reg_timings	reg_timings;
222 	unsigned int			flags;
223 	struct img_ir_filter		filters[RC_FILTER_MAX];
224 
225 	enum img_ir_mode		mode;
226 	u32				suspend_irqen;
227 };
228 
229 static inline bool img_ir_hw_enabled(struct img_ir_priv_hw *hw)
230 {
231 	return hw->rdev;
232 };
233 
234 void img_ir_isr_hw(struct img_ir_priv *priv, u32 irq_status);
235 void img_ir_setup_hw(struct img_ir_priv *priv);
236 int img_ir_probe_hw(struct img_ir_priv *priv);
237 void img_ir_remove_hw(struct img_ir_priv *priv);
238 
239 #ifdef CONFIG_PM_SLEEP
240 int img_ir_suspend(struct device *dev);
241 int img_ir_resume(struct device *dev);
242 #else
243 #define img_ir_suspend NULL
244 #define img_ir_resume NULL
245 #endif
246 
247 #else
248 
249 struct img_ir_priv_hw {
250 };
251 
252 static inline bool img_ir_hw_enabled(struct img_ir_priv_hw *hw)
253 {
254 	return false;
255 };
256 static inline void img_ir_isr_hw(struct img_ir_priv *priv, u32 irq_status)
257 {
258 }
259 static inline void img_ir_setup_hw(struct img_ir_priv *priv)
260 {
261 }
262 static inline int img_ir_probe_hw(struct img_ir_priv *priv)
263 {
264 	return -ENODEV;
265 }
266 static inline void img_ir_remove_hw(struct img_ir_priv *priv)
267 {
268 }
269 
270 #define img_ir_suspend NULL
271 #define img_ir_resume NULL
272 
273 #endif /* CONFIG_IR_IMG_HW */
274 
275 #endif /* _IMG_IR_HW_H_ */
276