img-ir-hw.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. * @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. ret = hw->decoder->filter(sc_filter, &filter, hw->enabled_protocols);
  451. if (ret)
  452. goto unlock;
  453. dev_dbg(priv->dev, "IR raw %sfilter=%016llx & %016llx\n",
  454. type == RC_FILTER_WAKEUP ? "wake " : "",
  455. (unsigned long long)filter.data,
  456. (unsigned long long)filter.mask);
  457. set_unlock:
  458. /* apply raw filters */
  459. switch (type) {
  460. case RC_FILTER_NORMAL:
  461. _img_ir_set_filter(priv, filter_ptr);
  462. break;
  463. case RC_FILTER_WAKEUP:
  464. _img_ir_set_wake_filter(priv, filter_ptr);
  465. break;
  466. default:
  467. ret = -EINVAL;
  468. }
  469. unlock:
  470. spin_unlock_irq(&priv->lock);
  471. return ret;
  472. }
  473. static int img_ir_set_normal_filter(struct rc_dev *dev,
  474. struct rc_scancode_filter *sc_filter)
  475. {
  476. return img_ir_set_filter(dev, RC_FILTER_NORMAL, sc_filter);
  477. }
  478. static int img_ir_set_wakeup_filter(struct rc_dev *dev,
  479. struct rc_scancode_filter *sc_filter)
  480. {
  481. return img_ir_set_filter(dev, RC_FILTER_WAKEUP, sc_filter);
  482. }
  483. /**
  484. * img_ir_set_decoder() - Set the current decoder.
  485. * @priv: IR private data.
  486. * @decoder: Decoder to use with immediate effect.
  487. * @proto: Protocol bitmap (or 0 to use decoder->type).
  488. */
  489. static void img_ir_set_decoder(struct img_ir_priv *priv,
  490. const struct img_ir_decoder *decoder,
  491. u64 proto)
  492. {
  493. struct img_ir_priv_hw *hw = &priv->hw;
  494. struct rc_dev *rdev = hw->rdev;
  495. u32 ir_status, irq_en;
  496. spin_lock_irq(&priv->lock);
  497. /*
  498. * First record that the protocol is being stopped so that the end timer
  499. * isn't restarted while we're trying to stop it.
  500. */
  501. hw->stopping = true;
  502. /*
  503. * Release the lock to stop the end timer, since the end timer handler
  504. * acquires the lock and we don't want to deadlock waiting for it.
  505. */
  506. spin_unlock_irq(&priv->lock);
  507. del_timer_sync(&hw->end_timer);
  508. del_timer_sync(&hw->suspend_timer);
  509. spin_lock_irq(&priv->lock);
  510. hw->stopping = false;
  511. /* switch off and disable interrupts */
  512. img_ir_write(priv, IMG_IR_CONTROL, 0);
  513. irq_en = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
  514. img_ir_write(priv, IMG_IR_IRQ_ENABLE, irq_en & IMG_IR_IRQ_EDGE);
  515. img_ir_write(priv, IMG_IR_IRQ_CLEAR, IMG_IR_IRQ_ALL & ~IMG_IR_IRQ_EDGE);
  516. /* ack any data already detected */
  517. ir_status = img_ir_read(priv, IMG_IR_STATUS);
  518. if (ir_status & (IMG_IR_RXDVAL | IMG_IR_RXDVALD2)) {
  519. ir_status &= ~(IMG_IR_RXDVAL | IMG_IR_RXDVALD2);
  520. img_ir_write(priv, IMG_IR_STATUS, ir_status);
  521. }
  522. /* always read data to clear buffer if IR wakes the device */
  523. img_ir_read(priv, IMG_IR_DATA_LW);
  524. img_ir_read(priv, IMG_IR_DATA_UP);
  525. /* switch back to normal mode */
  526. hw->mode = IMG_IR_M_NORMAL;
  527. /* clear the wakeup scancode filter */
  528. rdev->scancode_wakeup_filter.data = 0;
  529. rdev->scancode_wakeup_filter.mask = 0;
  530. /* clear raw filters */
  531. _img_ir_set_filter(priv, NULL);
  532. _img_ir_set_wake_filter(priv, NULL);
  533. /* clear the enabled protocols */
  534. hw->enabled_protocols = 0;
  535. /* switch decoder */
  536. hw->decoder = decoder;
  537. if (!decoder)
  538. goto unlock;
  539. /* set the enabled protocols */
  540. if (!proto)
  541. proto = decoder->type;
  542. hw->enabled_protocols = proto;
  543. /* write the new timings */
  544. img_ir_decoder_convert(decoder, &hw->reg_timings, hw->clk_hz);
  545. img_ir_write_timings(priv, &hw->reg_timings.timings, RC_FILTER_NORMAL);
  546. /* set up and enable */
  547. img_ir_write(priv, IMG_IR_CONTROL, hw->reg_timings.ctrl);
  548. unlock:
  549. spin_unlock_irq(&priv->lock);
  550. }
  551. /**
  552. * img_ir_decoder_compatable() - Find whether a decoder will work with a device.
  553. * @priv: IR private data.
  554. * @dec: Decoder to check.
  555. *
  556. * Returns: true if @dec is compatible with the device @priv refers to.
  557. */
  558. static bool img_ir_decoder_compatible(struct img_ir_priv *priv,
  559. const struct img_ir_decoder *dec)
  560. {
  561. unsigned int ct;
  562. /* don't accept decoders using code types which aren't supported */
  563. ct = dec->control.code_type;
  564. if (priv->hw.ct_quirks[ct] & IMG_IR_QUIRK_CODE_BROKEN)
  565. return false;
  566. return true;
  567. }
  568. /**
  569. * img_ir_allowed_protos() - Get allowed protocols from global decoder list.
  570. * @priv: IR private data.
  571. *
  572. * Returns: Mask of protocols supported by the device @priv refers to.
  573. */
  574. static u64 img_ir_allowed_protos(struct img_ir_priv *priv)
  575. {
  576. u64 protos = 0;
  577. struct img_ir_decoder **decp;
  578. for (decp = img_ir_decoders; *decp; ++decp) {
  579. const struct img_ir_decoder *dec = *decp;
  580. if (img_ir_decoder_compatible(priv, dec))
  581. protos |= dec->type;
  582. }
  583. return protos;
  584. }
  585. /* Callback for changing protocol using sysfs */
  586. static int img_ir_change_protocol(struct rc_dev *dev, u64 *ir_type)
  587. {
  588. struct img_ir_priv *priv = dev->priv;
  589. struct img_ir_priv_hw *hw = &priv->hw;
  590. struct rc_dev *rdev = hw->rdev;
  591. struct img_ir_decoder **decp;
  592. u64 wakeup_protocols;
  593. if (!*ir_type) {
  594. /* disable all protocols */
  595. img_ir_set_decoder(priv, NULL, 0);
  596. goto success;
  597. }
  598. for (decp = img_ir_decoders; *decp; ++decp) {
  599. const struct img_ir_decoder *dec = *decp;
  600. if (!img_ir_decoder_compatible(priv, dec))
  601. continue;
  602. if (*ir_type & dec->type) {
  603. *ir_type &= dec->type;
  604. img_ir_set_decoder(priv, dec, *ir_type);
  605. goto success;
  606. }
  607. }
  608. return -EINVAL;
  609. success:
  610. /*
  611. * Only allow matching wakeup protocols for now, and only if filtering
  612. * is supported.
  613. */
  614. wakeup_protocols = *ir_type;
  615. if (!hw->decoder || !hw->decoder->filter)
  616. wakeup_protocols = 0;
  617. rdev->allowed_wakeup_protocols = wakeup_protocols;
  618. rdev->enabled_wakeup_protocols = wakeup_protocols;
  619. return 0;
  620. }
  621. /* Changes ir-core protocol device attribute */
  622. static void img_ir_set_protocol(struct img_ir_priv *priv, u64 proto)
  623. {
  624. struct rc_dev *rdev = priv->hw.rdev;
  625. spin_lock_irq(&rdev->rc_map.lock);
  626. rdev->rc_map.rc_type = __ffs64(proto);
  627. spin_unlock_irq(&rdev->rc_map.lock);
  628. mutex_lock(&rdev->lock);
  629. rdev->enabled_protocols = proto;
  630. rdev->allowed_wakeup_protocols = proto;
  631. rdev->enabled_wakeup_protocols = proto;
  632. mutex_unlock(&rdev->lock);
  633. }
  634. /* Set up IR decoders */
  635. static void img_ir_init_decoders(void)
  636. {
  637. struct img_ir_decoder **decp;
  638. spin_lock(&img_ir_decoders_lock);
  639. if (!img_ir_decoders_preprocessed) {
  640. for (decp = img_ir_decoders; *decp; ++decp)
  641. img_ir_decoder_preprocess(*decp);
  642. img_ir_decoders_preprocessed = true;
  643. }
  644. spin_unlock(&img_ir_decoders_lock);
  645. }
  646. #ifdef CONFIG_PM_SLEEP
  647. /**
  648. * img_ir_enable_wake() - Switch to wake mode.
  649. * @priv: IR private data.
  650. *
  651. * Returns: non-zero if the IR can wake the system.
  652. */
  653. static int img_ir_enable_wake(struct img_ir_priv *priv)
  654. {
  655. struct img_ir_priv_hw *hw = &priv->hw;
  656. int ret = 0;
  657. spin_lock_irq(&priv->lock);
  658. if (hw->flags & IMG_IR_F_WAKE) {
  659. /* interrupt only on a match */
  660. hw->suspend_irqen = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
  661. img_ir_write(priv, IMG_IR_IRQ_ENABLE, IMG_IR_IRQ_DATA_MATCH);
  662. img_ir_write_filter(priv, &hw->filters[RC_FILTER_WAKEUP]);
  663. img_ir_write_timings(priv, &hw->reg_timings.timings,
  664. RC_FILTER_WAKEUP);
  665. hw->mode = IMG_IR_M_WAKE;
  666. ret = 1;
  667. }
  668. spin_unlock_irq(&priv->lock);
  669. return ret;
  670. }
  671. /**
  672. * img_ir_disable_wake() - Switch out of wake mode.
  673. * @priv: IR private data
  674. *
  675. * Returns: 1 if the hardware should be allowed to wake from a sleep state.
  676. * 0 otherwise.
  677. */
  678. static int img_ir_disable_wake(struct img_ir_priv *priv)
  679. {
  680. struct img_ir_priv_hw *hw = &priv->hw;
  681. int ret = 0;
  682. spin_lock_irq(&priv->lock);
  683. if (hw->flags & IMG_IR_F_WAKE) {
  684. /* restore normal filtering */
  685. if (hw->flags & IMG_IR_F_FILTER) {
  686. img_ir_write(priv, IMG_IR_IRQ_ENABLE,
  687. (hw->suspend_irqen & IMG_IR_IRQ_EDGE) |
  688. IMG_IR_IRQ_DATA_MATCH);
  689. img_ir_write_filter(priv,
  690. &hw->filters[RC_FILTER_NORMAL]);
  691. } else {
  692. img_ir_write(priv, IMG_IR_IRQ_ENABLE,
  693. (hw->suspend_irqen & IMG_IR_IRQ_EDGE) |
  694. IMG_IR_IRQ_DATA_VALID |
  695. IMG_IR_IRQ_DATA2_VALID);
  696. img_ir_write_filter(priv, NULL);
  697. }
  698. img_ir_write_timings(priv, &hw->reg_timings.timings,
  699. RC_FILTER_NORMAL);
  700. hw->mode = IMG_IR_M_NORMAL;
  701. ret = 1;
  702. }
  703. spin_unlock_irq(&priv->lock);
  704. return ret;
  705. }
  706. #endif /* CONFIG_PM_SLEEP */
  707. /* lock must be held */
  708. static void img_ir_begin_repeat(struct img_ir_priv *priv)
  709. {
  710. struct img_ir_priv_hw *hw = &priv->hw;
  711. if (hw->mode == IMG_IR_M_NORMAL) {
  712. /* switch to repeat timings */
  713. img_ir_write(priv, IMG_IR_CONTROL, 0);
  714. hw->mode = IMG_IR_M_REPEATING;
  715. img_ir_write_timings(priv, &hw->reg_timings.rtimings,
  716. RC_FILTER_NORMAL);
  717. img_ir_write(priv, IMG_IR_CONTROL, hw->reg_timings.ctrl);
  718. }
  719. }
  720. /* lock must be held */
  721. static void img_ir_end_repeat(struct img_ir_priv *priv)
  722. {
  723. struct img_ir_priv_hw *hw = &priv->hw;
  724. if (hw->mode == IMG_IR_M_REPEATING) {
  725. /* switch to normal timings */
  726. img_ir_write(priv, IMG_IR_CONTROL, 0);
  727. hw->mode = IMG_IR_M_NORMAL;
  728. img_ir_write_timings(priv, &hw->reg_timings.timings,
  729. RC_FILTER_NORMAL);
  730. img_ir_write(priv, IMG_IR_CONTROL, hw->reg_timings.ctrl);
  731. }
  732. }
  733. /* lock must be held */
  734. static void img_ir_handle_data(struct img_ir_priv *priv, u32 len, u64 raw)
  735. {
  736. struct img_ir_priv_hw *hw = &priv->hw;
  737. const struct img_ir_decoder *dec = hw->decoder;
  738. int ret = IMG_IR_SCANCODE;
  739. struct img_ir_scancode_req request;
  740. request.protocol = RC_TYPE_UNKNOWN;
  741. request.toggle = 0;
  742. if (dec->scancode)
  743. ret = dec->scancode(len, raw, hw->enabled_protocols, &request);
  744. else if (len >= 32)
  745. request.scancode = (u32)raw;
  746. else if (len < 32)
  747. request.scancode = (u32)raw & ((1 << len)-1);
  748. dev_dbg(priv->dev, "data (%u bits) = %#llx\n",
  749. len, (unsigned long long)raw);
  750. if (ret == IMG_IR_SCANCODE) {
  751. dev_dbg(priv->dev, "decoded scan code %#x, toggle %u\n",
  752. request.scancode, request.toggle);
  753. rc_keydown(hw->rdev, request.protocol, request.scancode,
  754. request.toggle);
  755. img_ir_end_repeat(priv);
  756. } else if (ret == IMG_IR_REPEATCODE) {
  757. if (hw->mode == IMG_IR_M_REPEATING) {
  758. dev_dbg(priv->dev, "decoded repeat code\n");
  759. rc_repeat(hw->rdev);
  760. } else {
  761. dev_dbg(priv->dev, "decoded unexpected repeat code, ignoring\n");
  762. }
  763. } else {
  764. dev_dbg(priv->dev, "decode failed (%d)\n", ret);
  765. return;
  766. }
  767. /* we mustn't update the end timer while trying to stop it */
  768. if (dec->repeat && !hw->stopping) {
  769. unsigned long interval;
  770. img_ir_begin_repeat(priv);
  771. /* update timer, but allowing for 1/8th tolerance */
  772. interval = dec->repeat + (dec->repeat >> 3);
  773. mod_timer(&hw->end_timer,
  774. jiffies + msecs_to_jiffies(interval));
  775. }
  776. }
  777. /* timer function to end waiting for repeat. */
  778. static void img_ir_end_timer(unsigned long arg)
  779. {
  780. struct img_ir_priv *priv = (struct img_ir_priv *)arg;
  781. spin_lock_irq(&priv->lock);
  782. img_ir_end_repeat(priv);
  783. spin_unlock_irq(&priv->lock);
  784. }
  785. /*
  786. * Timer function to re-enable the current protocol after it had been
  787. * cleared when invalid interrupts were generated due to a quirk in the
  788. * img-ir decoder.
  789. */
  790. static void img_ir_suspend_timer(unsigned long arg)
  791. {
  792. struct img_ir_priv *priv = (struct img_ir_priv *)arg;
  793. spin_lock_irq(&priv->lock);
  794. /*
  795. * Don't overwrite enabled valid/match IRQs if they have already been
  796. * changed by e.g. a filter change.
  797. */
  798. if ((priv->hw.quirk_suspend_irq & IMG_IR_IRQ_EDGE) ==
  799. img_ir_read(priv, IMG_IR_IRQ_ENABLE))
  800. img_ir_write(priv, IMG_IR_IRQ_ENABLE,
  801. priv->hw.quirk_suspend_irq);
  802. /* enable */
  803. img_ir_write(priv, IMG_IR_CONTROL, priv->hw.reg_timings.ctrl);
  804. spin_unlock_irq(&priv->lock);
  805. }
  806. #ifdef CONFIG_COMMON_CLK
  807. static void img_ir_change_frequency(struct img_ir_priv *priv,
  808. struct clk_notifier_data *change)
  809. {
  810. struct img_ir_priv_hw *hw = &priv->hw;
  811. dev_dbg(priv->dev, "clk changed %lu HZ -> %lu HZ\n",
  812. change->old_rate, change->new_rate);
  813. spin_lock_irq(&priv->lock);
  814. if (hw->clk_hz == change->new_rate)
  815. goto unlock;
  816. hw->clk_hz = change->new_rate;
  817. /* refresh current timings */
  818. if (hw->decoder) {
  819. img_ir_decoder_convert(hw->decoder, &hw->reg_timings,
  820. hw->clk_hz);
  821. switch (hw->mode) {
  822. case IMG_IR_M_NORMAL:
  823. img_ir_write_timings(priv, &hw->reg_timings.timings,
  824. RC_FILTER_NORMAL);
  825. break;
  826. case IMG_IR_M_REPEATING:
  827. img_ir_write_timings(priv, &hw->reg_timings.rtimings,
  828. RC_FILTER_NORMAL);
  829. break;
  830. #ifdef CONFIG_PM_SLEEP
  831. case IMG_IR_M_WAKE:
  832. img_ir_write_timings(priv, &hw->reg_timings.timings,
  833. RC_FILTER_WAKEUP);
  834. break;
  835. #endif
  836. }
  837. }
  838. unlock:
  839. spin_unlock_irq(&priv->lock);
  840. }
  841. static int img_ir_clk_notify(struct notifier_block *self, unsigned long action,
  842. void *data)
  843. {
  844. struct img_ir_priv *priv = container_of(self, struct img_ir_priv,
  845. hw.clk_nb);
  846. switch (action) {
  847. case POST_RATE_CHANGE:
  848. img_ir_change_frequency(priv, data);
  849. break;
  850. default:
  851. break;
  852. }
  853. return NOTIFY_OK;
  854. }
  855. #endif /* CONFIG_COMMON_CLK */
  856. /* called with priv->lock held */
  857. void img_ir_isr_hw(struct img_ir_priv *priv, u32 irq_status)
  858. {
  859. struct img_ir_priv_hw *hw = &priv->hw;
  860. u32 ir_status, len, lw, up;
  861. unsigned int ct;
  862. /* use the current decoder */
  863. if (!hw->decoder)
  864. return;
  865. ct = hw->decoder->control.code_type;
  866. ir_status = img_ir_read(priv, IMG_IR_STATUS);
  867. if (!(ir_status & (IMG_IR_RXDVAL | IMG_IR_RXDVALD2))) {
  868. if (!(priv->hw.ct_quirks[ct] & IMG_IR_QUIRK_CODE_IRQ) ||
  869. hw->stopping)
  870. return;
  871. /*
  872. * The below functionality is added as a work around to stop
  873. * multiple Interrupts generated when an incomplete IR code is
  874. * received by the decoder.
  875. * The decoder generates rapid interrupts without actually
  876. * having received any new data. After a single interrupt it's
  877. * expected to clear up, but instead multiple interrupts are
  878. * rapidly generated. only way to get out of this loop is to
  879. * reset the control register after a short delay.
  880. */
  881. img_ir_write(priv, IMG_IR_CONTROL, 0);
  882. hw->quirk_suspend_irq = img_ir_read(priv, IMG_IR_IRQ_ENABLE);
  883. img_ir_write(priv, IMG_IR_IRQ_ENABLE,
  884. hw->quirk_suspend_irq & IMG_IR_IRQ_EDGE);
  885. /* Timer activated to re-enable the protocol. */
  886. mod_timer(&hw->suspend_timer,
  887. jiffies + msecs_to_jiffies(5));
  888. return;
  889. }
  890. ir_status &= ~(IMG_IR_RXDVAL | IMG_IR_RXDVALD2);
  891. img_ir_write(priv, IMG_IR_STATUS, ir_status);
  892. len = (ir_status & IMG_IR_RXDLEN) >> IMG_IR_RXDLEN_SHIFT;
  893. /* some versions report wrong length for certain code types */
  894. if (hw->ct_quirks[ct] & IMG_IR_QUIRK_CODE_LEN_INCR)
  895. ++len;
  896. lw = img_ir_read(priv, IMG_IR_DATA_LW);
  897. up = img_ir_read(priv, IMG_IR_DATA_UP);
  898. img_ir_handle_data(priv, len, (u64)up << 32 | lw);
  899. }
  900. void img_ir_setup_hw(struct img_ir_priv *priv)
  901. {
  902. struct img_ir_decoder **decp;
  903. if (!priv->hw.rdev)
  904. return;
  905. /* Use the first available decoder (or disable stuff if NULL) */
  906. for (decp = img_ir_decoders; *decp; ++decp) {
  907. const struct img_ir_decoder *dec = *decp;
  908. if (img_ir_decoder_compatible(priv, dec)) {
  909. img_ir_set_protocol(priv, dec->type);
  910. img_ir_set_decoder(priv, dec, 0);
  911. return;
  912. }
  913. }
  914. img_ir_set_decoder(priv, NULL, 0);
  915. }
  916. /**
  917. * img_ir_probe_hw_caps() - Probe capabilities of the hardware.
  918. * @priv: IR private data.
  919. */
  920. static void img_ir_probe_hw_caps(struct img_ir_priv *priv)
  921. {
  922. struct img_ir_priv_hw *hw = &priv->hw;
  923. /*
  924. * When a version of the block becomes available without these quirks,
  925. * they'll have to depend on the core revision.
  926. */
  927. hw->ct_quirks[IMG_IR_CODETYPE_PULSELEN]
  928. |= IMG_IR_QUIRK_CODE_LEN_INCR;
  929. hw->ct_quirks[IMG_IR_CODETYPE_BIPHASE]
  930. |= IMG_IR_QUIRK_CODE_IRQ;
  931. hw->ct_quirks[IMG_IR_CODETYPE_2BITPULSEPOS]
  932. |= IMG_IR_QUIRK_CODE_BROKEN;
  933. }
  934. int img_ir_probe_hw(struct img_ir_priv *priv)
  935. {
  936. struct img_ir_priv_hw *hw = &priv->hw;
  937. struct rc_dev *rdev;
  938. int error;
  939. /* Ensure hardware decoders have been preprocessed */
  940. img_ir_init_decoders();
  941. /* Probe hardware capabilities */
  942. img_ir_probe_hw_caps(priv);
  943. /* Set up the end timer */
  944. setup_timer(&hw->end_timer, img_ir_end_timer, (unsigned long)priv);
  945. setup_timer(&hw->suspend_timer, img_ir_suspend_timer,
  946. (unsigned long)priv);
  947. /* Register a clock notifier */
  948. if (!IS_ERR(priv->clk)) {
  949. hw->clk_hz = clk_get_rate(priv->clk);
  950. #ifdef CONFIG_COMMON_CLK
  951. hw->clk_nb.notifier_call = img_ir_clk_notify;
  952. error = clk_notifier_register(priv->clk, &hw->clk_nb);
  953. if (error)
  954. dev_warn(priv->dev,
  955. "failed to register clock notifier\n");
  956. #endif
  957. } else {
  958. hw->clk_hz = 32768;
  959. }
  960. /* Allocate hardware decoder */
  961. hw->rdev = rdev = rc_allocate_device();
  962. if (!rdev) {
  963. dev_err(priv->dev, "cannot allocate input device\n");
  964. error = -ENOMEM;
  965. goto err_alloc_rc;
  966. }
  967. rdev->priv = priv;
  968. rdev->map_name = RC_MAP_EMPTY;
  969. rdev->allowed_protocols = img_ir_allowed_protos(priv);
  970. rdev->input_name = "IMG Infrared Decoder";
  971. rdev->s_filter = img_ir_set_normal_filter;
  972. rdev->s_wakeup_filter = img_ir_set_wakeup_filter;
  973. /* Register hardware decoder */
  974. error = rc_register_device(rdev);
  975. if (error) {
  976. dev_err(priv->dev, "failed to register IR input device\n");
  977. goto err_register_rc;
  978. }
  979. /*
  980. * Set this after rc_register_device as no protocols have been
  981. * registered yet.
  982. */
  983. rdev->change_protocol = img_ir_change_protocol;
  984. device_init_wakeup(priv->dev, 1);
  985. return 0;
  986. err_register_rc:
  987. img_ir_set_decoder(priv, NULL, 0);
  988. hw->rdev = NULL;
  989. rc_free_device(rdev);
  990. err_alloc_rc:
  991. #ifdef CONFIG_COMMON_CLK
  992. if (!IS_ERR(priv->clk))
  993. clk_notifier_unregister(priv->clk, &hw->clk_nb);
  994. #endif
  995. return error;
  996. }
  997. void img_ir_remove_hw(struct img_ir_priv *priv)
  998. {
  999. struct img_ir_priv_hw *hw = &priv->hw;
  1000. struct rc_dev *rdev = hw->rdev;
  1001. if (!rdev)
  1002. return;
  1003. img_ir_set_decoder(priv, NULL, 0);
  1004. hw->rdev = NULL;
  1005. rc_unregister_device(rdev);
  1006. #ifdef CONFIG_COMMON_CLK
  1007. if (!IS_ERR(priv->clk))
  1008. clk_notifier_unregister(priv->clk, &hw->clk_nb);
  1009. #endif
  1010. }
  1011. #ifdef CONFIG_PM_SLEEP
  1012. int img_ir_suspend(struct device *dev)
  1013. {
  1014. struct img_ir_priv *priv = dev_get_drvdata(dev);
  1015. if (device_may_wakeup(dev) && img_ir_enable_wake(priv))
  1016. enable_irq_wake(priv->irq);
  1017. return 0;
  1018. }
  1019. int img_ir_resume(struct device *dev)
  1020. {
  1021. struct img_ir_priv *priv = dev_get_drvdata(dev);
  1022. if (device_may_wakeup(dev) && img_ir_disable_wake(priv))
  1023. disable_irq_wake(priv->irq);
  1024. return 0;
  1025. }
  1026. #endif /* CONFIG_PM_SLEEP */