img-ir-hw.c 31 KB

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