img-ir-hw.c 33 KB

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