meson-ir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for Amlogic Meson IR remote receiver
  4. *
  5. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  6. */
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/bitfield.h>
  16. #include <linux/regmap.h>
  17. #include <media/rc-core.h>
  18. #define DRIVER_NAME "meson-ir"
  19. #define IR_DEC_LDR_ACTIVE 0x00
  20. #define IR_DEC_LDR_ACTIVE_MAX GENMASK(28, 16)
  21. #define IR_DEC_LDR_ACTIVE_MIN GENMASK(12, 0)
  22. #define IR_DEC_LDR_IDLE 0x04
  23. #define IR_DEC_LDR_IDLE_MAX GENMASK(28, 16)
  24. #define IR_DEC_LDR_IDLE_MIN GENMASK(12, 0)
  25. #define IR_DEC_LDR_REPEAT 0x08
  26. #define IR_DEC_LDR_REPEAT_MAX GENMASK(25, 16)
  27. #define IR_DEC_LDR_REPEAT_MIN GENMASK(9, 0)
  28. #define IR_DEC_BIT_0 0x0c
  29. #define IR_DEC_BIT_0_MAX GENMASK(25, 16)
  30. #define IR_DEC_BIT_0_MIN GENMASK(9, 0)
  31. #define IR_DEC_REG0 0x10
  32. #define IR_DEC_REG0_FILTER GENMASK(30, 28)
  33. #define IR_DEC_REG0_FRAME_TIME_MAX GENMASK(24, 12)
  34. #define IR_DEC_REG0_BASE_TIME GENMASK(11, 0)
  35. #define IR_DEC_FRAME 0x14
  36. #define IR_DEC_STATUS 0x18
  37. #define IR_DEC_STATUS_BIT_1_ENABLE BIT(30)
  38. #define IR_DEC_STATUS_BIT_1_MAX GENMASK(29, 20)
  39. #define IR_DEC_STATUS_BIT_1_MIN GENMASK(19, 10)
  40. #define IR_DEC_STATUS_PULSE BIT(8)
  41. #define IR_DEC_STATUS_BUSY BIT(7)
  42. #define IR_DEC_STATUS_FRAME_STATUS GENMASK(3, 0)
  43. #define IR_DEC_REG1 0x1c
  44. #define IR_DEC_REG1_TIME_IV GENMASK(28, 16)
  45. #define IR_DEC_REG1_FRAME_LEN GENMASK(13, 8)
  46. #define IR_DEC_REG1_ENABLE BIT(15)
  47. #define IR_DEC_REG1_HOLD_CODE BIT(6)
  48. #define IR_DEC_REG1_IRQSEL GENMASK(3, 2)
  49. #define IR_DEC_REG1_RESET BIT(0)
  50. /* Meson 6b uses REG1 to configure IR mode */
  51. #define IR_DEC_REG1_MODE GENMASK(8, 7)
  52. /* The following registers are only available on Meson 8b and newer */
  53. #define IR_DEC_REG2 0x20
  54. #define IR_DEC_REG2_TICK_MODE BIT(15)
  55. #define IR_DEC_REG2_REPEAT_COUNTER BIT(13)
  56. #define IR_DEC_REG2_REPEAT_TIME BIT(12)
  57. #define IR_DEC_REG2_COMPARE_FRAME BIT(11)
  58. #define IR_DEC_REG2_BIT_ORDER BIT(8)
  59. /* Meson 8b / GXBB use REG2 to configure IR mode */
  60. #define IR_DEC_REG2_MODE GENMASK(3, 0)
  61. #define IR_DEC_DURATN2 0x24
  62. #define IR_DEC_DURATN2_MAX GENMASK(25, 16)
  63. #define IR_DEC_DURATN2_MIN GENMASK(9, 0)
  64. #define IR_DEC_DURATN3 0x28
  65. #define IR_DEC_DURATN3_MAX GENMASK(25, 16)
  66. #define IR_DEC_DURATN3_MIN GENMASK(9, 0)
  67. #define IR_DEC_FRAME1 0x2c
  68. #define FRAME_MSB_FIRST true
  69. #define FRAME_LSB_FIRST false
  70. #define DEC_MODE_NEC 0x0
  71. #define DEC_MODE_RAW 0x2
  72. #define DEC_MODE_RC6 0x9
  73. #define DEC_MODE_XMP 0xE
  74. #define DEC_MODE_UNKNOW 0xFF
  75. #define DEC_STATUS_VALID BIT(3)
  76. #define DEC_STATUS_DATA_CODE_ERR BIT(2)
  77. #define DEC_STATUS_CUSTOM_CODE_ERR BIT(1)
  78. #define DEC_STATUS_REPEAT BIT(0)
  79. #define IRQSEL_DEC_MODE 0
  80. #define IRQSEL_RISE_FALL 1
  81. #define IRQSEL_FALL 2
  82. #define IRQSEL_RISE 3
  83. #define MESON_RAW_TRATE 10 /* us */
  84. #define MESON_HW_TRATE 20 /* us */
  85. /**
  86. * struct meson_ir_protocol - describe IR Protocol parameter
  87. *
  88. * @hw_protocol: select IR Protocol from IR Controller
  89. * @repeat_counter_enable: enable frame-to-frame time counter, it should work
  90. * with @repeat_compare_enable to detect the repeat frame
  91. * @repeat_check_enable: enable repeat time check for repeat detection
  92. * @repeat_compare_enable: enable to compare frame for repeat frame detection.
  93. * Some IR Protocol send the same data as repeat frame.
  94. * In this case, it should work with
  95. * @repeat_counter_enable to detect the repeat frame.
  96. * @bit_order: bit order, LSB or MSB
  97. * @bit1_match_enable: enable to check bit 1
  98. * @hold_code_enable: hold frame code in register IR_DEC_FRAME1, the new one
  99. * frame code will not be store in IR_DEC_FRAME1.
  100. * until IR_DEC_FRAME1 has been read
  101. * @count_tick_mode: increasing time unit of frame-to-frame time counter.
  102. * 0 = 100us, 1 = 10us
  103. * @code_length: length (N-1) of data frame
  104. * @frame_time_max: max time for whole frame. Unit: MESON_HW_TRATE
  105. * @leader_active_max: max time for NEC/RC6 leader active part. Unit: MESON_HW_TRATE
  106. * @leader_active_min: min time for NEC/RC6 leader active part. Unit: MESON_HW_TRATE
  107. * @leader_idle_max: max time for NEC/RC6 leader idle part. Unit: MESON_HW_TRATE
  108. * @leader_idle_min: min time for NEC/RC6 leader idle part. Unit: MESON_HW_TRATE
  109. * @repeat_leader_max: max time for NEC repeat leader idle part. Unit: MESON_HW_TRATE
  110. * @repeat_leader_min: min time for NEC repeat leader idle part. Unit: MESON_HW_TRATE
  111. * @bit0_max: max time for NEC Logic '0', half of RC6 trailer bit, XMP Logic '00'
  112. * @bit0_min: min time for NEC Logic '0', half of RC6 trailer bit, XMP Logic '00'
  113. * @bit1_max: max time for NEC Logic '1', whole of RC6 trailer bit, XMP Logic '01'
  114. * @bit1_min: min time for NEC Logic '1', whole of RC6 trailer bit, XMP Logic '01'
  115. * @duration2_max: max time for half of RC6 normal bit, XMP Logic '10'
  116. * @duration2_min: min time for half of RC6 normal bit, XMP Logic '10'
  117. * @duration3_max: max time for whole of RC6 normal bit, XMP Logic '11'
  118. * @duration3_min: min time for whole of RC6 normal bit, XMP Logic '11'
  119. */
  120. struct meson_ir_protocol {
  121. u8 hw_protocol;
  122. bool repeat_counter_enable;
  123. bool repeat_check_enable;
  124. bool repeat_compare_enable;
  125. bool bit_order;
  126. bool bit1_match_enable;
  127. bool hold_code_enable;
  128. bool count_tick_mode;
  129. u8 code_length;
  130. u16 frame_time_max;
  131. u16 leader_active_max;
  132. u16 leader_active_min;
  133. u16 leader_idle_max;
  134. u16 leader_idle_min;
  135. u16 repeat_leader_max;
  136. u16 repeat_leader_min;
  137. u16 bit0_max;
  138. u16 bit0_min;
  139. u16 bit1_max;
  140. u16 bit1_min;
  141. u16 duration2_max;
  142. u16 duration2_min;
  143. u16 duration3_max;
  144. u16 duration3_min;
  145. };
  146. struct meson_ir_param {
  147. bool support_hw_decoder;
  148. unsigned int max_register;
  149. };
  150. struct meson_ir {
  151. const struct meson_ir_param *param;
  152. struct regmap *reg;
  153. struct rc_dev *rc;
  154. spinlock_t lock;
  155. };
  156. static struct regmap_config meson_ir_regmap_config = {
  157. .reg_bits = 32,
  158. .val_bits = 32,
  159. .reg_stride = 4,
  160. };
  161. static const struct meson_ir_protocol protocol_timings[] = {
  162. /* protocol, repeat counter, repeat check, repeat compare, order */
  163. {DEC_MODE_NEC, false, false, false, FRAME_LSB_FIRST,
  164. /* bit 1 match, hold code, count tick, len, frame time */
  165. true, false, false, 32, 4000,
  166. /* leader active max/min, leader idle max/min, repeat leader max/min */
  167. 500, 400, 300, 200, 150, 80,
  168. /* bit0 max/min, bit1 max/min, duration2 max/min, duration3 max/min */
  169. 72, 40, 134, 90, 0, 0, 0, 0}
  170. };
  171. static void meson_ir_nec_handler(struct meson_ir *ir)
  172. {
  173. u32 code = 0;
  174. u32 status = 0;
  175. enum rc_proto proto;
  176. regmap_read(ir->reg, IR_DEC_STATUS, &status);
  177. if (status & DEC_STATUS_REPEAT) {
  178. rc_repeat(ir->rc);
  179. } else {
  180. regmap_read(ir->reg, IR_DEC_FRAME, &code);
  181. code = ir_nec_bytes_to_scancode(code, code >> 8,
  182. code >> 16, code >> 24, &proto);
  183. rc_keydown(ir->rc, proto, code, 0);
  184. }
  185. }
  186. static void meson_ir_hw_handler(struct meson_ir *ir)
  187. {
  188. if (ir->rc->enabled_protocols & RC_PROTO_BIT_NEC)
  189. meson_ir_nec_handler(ir);
  190. }
  191. static irqreturn_t meson_ir_irq(int irqno, void *dev_id)
  192. {
  193. struct meson_ir *ir = dev_id;
  194. u32 duration, status;
  195. struct ir_raw_event rawir = {};
  196. spin_lock(&ir->lock);
  197. regmap_read(ir->reg, IR_DEC_STATUS, &status);
  198. if (ir->rc->driver_type == RC_DRIVER_IR_RAW) {
  199. rawir.pulse = !!(status & IR_DEC_STATUS_PULSE);
  200. regmap_read(ir->reg, IR_DEC_REG1, &duration);
  201. duration = FIELD_GET(IR_DEC_REG1_TIME_IV, duration);
  202. rawir.duration = duration * MESON_RAW_TRATE;
  203. ir_raw_event_store_with_timeout(ir->rc, &rawir);
  204. } else if (ir->rc->driver_type == RC_DRIVER_SCANCODE) {
  205. if (status & DEC_STATUS_VALID)
  206. meson_ir_hw_handler(ir);
  207. }
  208. spin_unlock(&ir->lock);
  209. return IRQ_HANDLED;
  210. }
  211. static int meson_ir_hw_decoder_init(struct rc_dev *dev, u64 *rc_type)
  212. {
  213. u8 protocol;
  214. u32 regval;
  215. int i;
  216. unsigned long flags;
  217. const struct meson_ir_protocol *timings;
  218. struct meson_ir *ir = dev->priv;
  219. if (*rc_type & RC_PROTO_BIT_NEC)
  220. protocol = DEC_MODE_NEC;
  221. else
  222. return 0;
  223. for (i = 0; i < ARRAY_SIZE(protocol_timings); i++)
  224. if (protocol_timings[i].hw_protocol == protocol)
  225. break;
  226. if (i == ARRAY_SIZE(protocol_timings)) {
  227. dev_err(&dev->dev, "hw protocol isn't supported: %d\n",
  228. protocol);
  229. return -EINVAL;
  230. }
  231. timings = &protocol_timings[i];
  232. spin_lock_irqsave(&ir->lock, flags);
  233. /* Clear controller status */
  234. regmap_read(ir->reg, IR_DEC_STATUS, &regval);
  235. regmap_read(ir->reg, IR_DEC_FRAME, &regval);
  236. /* Reset ir decoder and disable decoder */
  237. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_ENABLE, 0);
  238. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_RESET,
  239. IR_DEC_REG1_RESET);
  240. /* Base time resolution, (19+1)*1us=20us */
  241. regval = FIELD_PREP(IR_DEC_REG0_BASE_TIME, MESON_HW_TRATE - 1);
  242. regmap_update_bits(ir->reg, IR_DEC_REG0, IR_DEC_REG0_BASE_TIME, regval);
  243. /* Monitor timing for input filter */
  244. regmap_update_bits(ir->reg, IR_DEC_REG0, IR_DEC_REG0_FILTER,
  245. FIELD_PREP(IR_DEC_REG0_FILTER, 7));
  246. /* HW protocol */
  247. regval = FIELD_PREP(IR_DEC_REG2_MODE, timings->hw_protocol);
  248. regmap_update_bits(ir->reg, IR_DEC_REG2, IR_DEC_REG2_MODE, regval);
  249. /* Hold frame data until register was read */
  250. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_HOLD_CODE,
  251. timings->hold_code_enable ?
  252. IR_DEC_REG1_HOLD_CODE : 0);
  253. /* Bit order */
  254. regmap_update_bits(ir->reg, IR_DEC_REG2, IR_DEC_REG2_BIT_ORDER,
  255. timings->bit_order ? IR_DEC_REG2_BIT_ORDER : 0);
  256. /* Select tick mode */
  257. regmap_update_bits(ir->reg, IR_DEC_REG2, IR_DEC_REG2_TICK_MODE,
  258. timings->count_tick_mode ?
  259. IR_DEC_REG2_TICK_MODE : 0);
  260. /*
  261. * Some protocols transmit the same data frame as repeat frame
  262. * when the key is pressing. In this case, it could be detected as
  263. * repeat frame if the repeat checker was enabled.
  264. */
  265. regmap_update_bits(ir->reg, IR_DEC_REG2, IR_DEC_REG2_REPEAT_COUNTER,
  266. timings->repeat_counter_enable ?
  267. IR_DEC_REG2_REPEAT_COUNTER : 0);
  268. regmap_update_bits(ir->reg, IR_DEC_REG2, IR_DEC_REG2_REPEAT_TIME,
  269. timings->repeat_check_enable ?
  270. IR_DEC_REG2_REPEAT_TIME : 0);
  271. regmap_update_bits(ir->reg, IR_DEC_REG2, IR_DEC_REG2_COMPARE_FRAME,
  272. timings->repeat_compare_enable ?
  273. IR_DEC_REG2_COMPARE_FRAME : 0);
  274. /*
  275. * FRAME_TIME_MAX should be larger than the time between
  276. * data frame and repeat frame
  277. */
  278. regval = FIELD_PREP(IR_DEC_REG0_FRAME_TIME_MAX,
  279. timings->frame_time_max);
  280. regmap_update_bits(ir->reg, IR_DEC_REG0, IR_DEC_REG0_FRAME_TIME_MAX,
  281. regval);
  282. /* Length(N-1) of data frame */
  283. regval = FIELD_PREP(IR_DEC_REG1_FRAME_LEN, timings->code_length - 1);
  284. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_FRAME_LEN, regval);
  285. /* Time for leader active part */
  286. regval = FIELD_PREP(IR_DEC_LDR_ACTIVE_MAX,
  287. timings->leader_active_max) |
  288. FIELD_PREP(IR_DEC_LDR_ACTIVE_MIN,
  289. timings->leader_active_min);
  290. regmap_update_bits(ir->reg, IR_DEC_LDR_ACTIVE, IR_DEC_LDR_ACTIVE_MAX |
  291. IR_DEC_LDR_ACTIVE_MIN, regval);
  292. /* Time for leader idle part */
  293. regval = FIELD_PREP(IR_DEC_LDR_IDLE_MAX, timings->leader_idle_max) |
  294. FIELD_PREP(IR_DEC_LDR_IDLE_MIN, timings->leader_idle_min);
  295. regmap_update_bits(ir->reg, IR_DEC_LDR_IDLE,
  296. IR_DEC_LDR_IDLE_MAX | IR_DEC_LDR_IDLE_MIN, regval);
  297. /* Time for repeat leader idle part */
  298. regval = FIELD_PREP(IR_DEC_LDR_REPEAT_MAX, timings->repeat_leader_max) |
  299. FIELD_PREP(IR_DEC_LDR_REPEAT_MIN, timings->repeat_leader_min);
  300. regmap_update_bits(ir->reg, IR_DEC_LDR_REPEAT, IR_DEC_LDR_REPEAT_MAX |
  301. IR_DEC_LDR_REPEAT_MIN, regval);
  302. /*
  303. * NEC: Time for logic '0'
  304. * RC6: Time for half of trailer bit
  305. */
  306. regval = FIELD_PREP(IR_DEC_BIT_0_MAX, timings->bit0_max) |
  307. FIELD_PREP(IR_DEC_BIT_0_MIN, timings->bit0_min);
  308. regmap_update_bits(ir->reg, IR_DEC_BIT_0,
  309. IR_DEC_BIT_0_MAX | IR_DEC_BIT_0_MIN, regval);
  310. /*
  311. * NEC: Time for logic '1'
  312. * RC6: Time for whole of trailer bit
  313. */
  314. regval = FIELD_PREP(IR_DEC_STATUS_BIT_1_MAX, timings->bit1_max) |
  315. FIELD_PREP(IR_DEC_STATUS_BIT_1_MIN, timings->bit1_min);
  316. regmap_update_bits(ir->reg, IR_DEC_STATUS, IR_DEC_STATUS_BIT_1_MAX |
  317. IR_DEC_STATUS_BIT_1_MIN, regval);
  318. /* Enable to match logic '1' */
  319. regmap_update_bits(ir->reg, IR_DEC_STATUS, IR_DEC_STATUS_BIT_1_ENABLE,
  320. timings->bit1_match_enable ?
  321. IR_DEC_STATUS_BIT_1_ENABLE : 0);
  322. /*
  323. * NEC: Unused
  324. * RC6: Time for halt of logic 0/1
  325. */
  326. regval = FIELD_PREP(IR_DEC_DURATN2_MAX, timings->duration2_max) |
  327. FIELD_PREP(IR_DEC_DURATN2_MIN, timings->duration2_min);
  328. regmap_update_bits(ir->reg, IR_DEC_DURATN2,
  329. IR_DEC_DURATN2_MAX | IR_DEC_DURATN2_MIN, regval);
  330. /*
  331. * NEC: Unused
  332. * RC6: Time for whole logic 0/1
  333. */
  334. regval = FIELD_PREP(IR_DEC_DURATN3_MAX, timings->duration3_max) |
  335. FIELD_PREP(IR_DEC_DURATN3_MIN, timings->duration3_min);
  336. regmap_update_bits(ir->reg, IR_DEC_DURATN3,
  337. IR_DEC_DURATN3_MAX | IR_DEC_DURATN3_MIN, regval);
  338. /* Reset ir decoder and enable decode */
  339. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_RESET,
  340. IR_DEC_REG1_RESET);
  341. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_RESET, 0);
  342. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_ENABLE,
  343. IR_DEC_REG1_ENABLE);
  344. spin_unlock_irqrestore(&ir->lock, flags);
  345. dev_info(&dev->dev, "hw decoder init, protocol: %d\n", protocol);
  346. return 0;
  347. }
  348. static void meson_ir_sw_decoder_init(struct rc_dev *dev)
  349. {
  350. unsigned long flags;
  351. struct meson_ir *ir = dev->priv;
  352. spin_lock_irqsave(&ir->lock, flags);
  353. /* Reset the decoder */
  354. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_RESET,
  355. IR_DEC_REG1_RESET);
  356. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_RESET, 0);
  357. /* Set general operation mode (= raw/software decoding) */
  358. if (of_device_is_compatible(dev->dev.of_node, "amlogic,meson6-ir"))
  359. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_MODE,
  360. FIELD_PREP(IR_DEC_REG1_MODE,
  361. DEC_MODE_RAW));
  362. else
  363. regmap_update_bits(ir->reg, IR_DEC_REG2, IR_DEC_REG2_MODE,
  364. FIELD_PREP(IR_DEC_REG2_MODE,
  365. DEC_MODE_RAW));
  366. /* Set rate */
  367. regmap_update_bits(ir->reg, IR_DEC_REG0, IR_DEC_REG0_BASE_TIME,
  368. FIELD_PREP(IR_DEC_REG0_BASE_TIME,
  369. MESON_RAW_TRATE - 1));
  370. /* IRQ on rising and falling edges */
  371. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_IRQSEL,
  372. FIELD_PREP(IR_DEC_REG1_IRQSEL, IRQSEL_RISE_FALL));
  373. /* Enable the decoder */
  374. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_ENABLE,
  375. IR_DEC_REG1_ENABLE);
  376. spin_unlock_irqrestore(&ir->lock, flags);
  377. dev_info(&dev->dev, "sw decoder init\n");
  378. }
  379. static int meson_ir_probe(struct platform_device *pdev)
  380. {
  381. const struct meson_ir_param *match_data;
  382. struct device *dev = &pdev->dev;
  383. struct device_node *node = dev->of_node;
  384. void __iomem *res_start;
  385. const char *map_name;
  386. struct meson_ir *ir;
  387. int irq, ret;
  388. ir = devm_kzalloc(dev, sizeof(struct meson_ir), GFP_KERNEL);
  389. if (!ir)
  390. return -ENOMEM;
  391. match_data = of_device_get_match_data(dev);
  392. if (!match_data)
  393. return dev_err_probe(dev, -ENODEV, "failed to get match data\n");
  394. ir->param = match_data;
  395. res_start = devm_platform_ioremap_resource(pdev, 0);
  396. if (IS_ERR(res_start))
  397. return PTR_ERR(res_start);
  398. meson_ir_regmap_config.max_register = ir->param->max_register;
  399. ir->reg = devm_regmap_init_mmio(&pdev->dev, res_start,
  400. &meson_ir_regmap_config);
  401. if (IS_ERR(ir->reg))
  402. return PTR_ERR(ir->reg);
  403. irq = platform_get_irq(pdev, 0);
  404. if (irq < 0)
  405. return irq;
  406. if (ir->param->support_hw_decoder)
  407. ir->rc = devm_rc_allocate_device(&pdev->dev,
  408. RC_DRIVER_SCANCODE);
  409. else
  410. ir->rc = devm_rc_allocate_device(&pdev->dev, RC_DRIVER_IR_RAW);
  411. if (!ir->rc) {
  412. dev_err(dev, "failed to allocate rc device\n");
  413. return -ENOMEM;
  414. }
  415. if (ir->rc->driver_type == RC_DRIVER_IR_RAW) {
  416. ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  417. ir->rc->rx_resolution = MESON_RAW_TRATE;
  418. ir->rc->min_timeout = 1;
  419. ir->rc->timeout = IR_DEFAULT_TIMEOUT;
  420. ir->rc->max_timeout = 10 * IR_DEFAULT_TIMEOUT;
  421. } else if (ir->rc->driver_type == RC_DRIVER_SCANCODE) {
  422. ir->rc->allowed_protocols = RC_PROTO_BIT_NEC;
  423. ir->rc->change_protocol = meson_ir_hw_decoder_init;
  424. }
  425. ir->rc->priv = ir;
  426. ir->rc->device_name = DRIVER_NAME;
  427. ir->rc->input_phys = DRIVER_NAME "/input0";
  428. ir->rc->input_id.bustype = BUS_HOST;
  429. map_name = of_get_property(node, "linux,rc-map-name", NULL);
  430. ir->rc->map_name = map_name ? map_name : RC_MAP_EMPTY;
  431. ir->rc->driver_name = DRIVER_NAME;
  432. spin_lock_init(&ir->lock);
  433. platform_set_drvdata(pdev, ir);
  434. ret = devm_rc_register_device(dev, ir->rc);
  435. if (ret) {
  436. dev_err(dev, "failed to register rc device\n");
  437. return ret;
  438. }
  439. if (ir->rc->driver_type == RC_DRIVER_IR_RAW)
  440. meson_ir_sw_decoder_init(ir->rc);
  441. ret = devm_request_irq(dev, irq, meson_ir_irq, 0, "meson_ir", ir);
  442. if (ret) {
  443. dev_err(dev, "failed to request irq\n");
  444. return ret;
  445. }
  446. dev_info(dev, "receiver initialized\n");
  447. return 0;
  448. }
  449. static void meson_ir_remove(struct platform_device *pdev)
  450. {
  451. struct meson_ir *ir = platform_get_drvdata(pdev);
  452. unsigned long flags;
  453. /* Disable the decoder */
  454. spin_lock_irqsave(&ir->lock, flags);
  455. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_ENABLE, 0);
  456. spin_unlock_irqrestore(&ir->lock, flags);
  457. }
  458. static void meson_ir_shutdown(struct platform_device *pdev)
  459. {
  460. struct device *dev = &pdev->dev;
  461. struct device_node *node = dev->of_node;
  462. struct meson_ir *ir = platform_get_drvdata(pdev);
  463. unsigned long flags;
  464. spin_lock_irqsave(&ir->lock, flags);
  465. /*
  466. * Set operation mode to NEC/hardware decoding to give
  467. * bootloader a chance to power the system back on
  468. */
  469. if (of_device_is_compatible(node, "amlogic,meson6-ir"))
  470. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_MODE,
  471. FIELD_PREP(IR_DEC_REG1_MODE, DEC_MODE_NEC));
  472. else
  473. regmap_update_bits(ir->reg, IR_DEC_REG2, IR_DEC_REG2_MODE,
  474. FIELD_PREP(IR_DEC_REG2_MODE, DEC_MODE_NEC));
  475. /* Set rate to default value */
  476. regmap_update_bits(ir->reg, IR_DEC_REG0, IR_DEC_REG0_BASE_TIME,
  477. FIELD_PREP(IR_DEC_REG0_BASE_TIME,
  478. MESON_HW_TRATE - 1));
  479. spin_unlock_irqrestore(&ir->lock, flags);
  480. }
  481. static __maybe_unused int meson_ir_resume(struct device *dev)
  482. {
  483. struct meson_ir *ir = dev_get_drvdata(dev);
  484. if (ir->param->support_hw_decoder)
  485. meson_ir_hw_decoder_init(ir->rc, &ir->rc->enabled_protocols);
  486. else
  487. meson_ir_sw_decoder_init(ir->rc);
  488. return 0;
  489. }
  490. static __maybe_unused int meson_ir_suspend(struct device *dev)
  491. {
  492. struct meson_ir *ir = dev_get_drvdata(dev);
  493. unsigned long flags;
  494. spin_lock_irqsave(&ir->lock, flags);
  495. regmap_update_bits(ir->reg, IR_DEC_REG1, IR_DEC_REG1_ENABLE, 0);
  496. spin_unlock_irqrestore(&ir->lock, flags);
  497. return 0;
  498. }
  499. static SIMPLE_DEV_PM_OPS(meson_ir_pm_ops, meson_ir_suspend, meson_ir_resume);
  500. static const struct meson_ir_param meson6_ir_param = {
  501. .support_hw_decoder = false,
  502. .max_register = IR_DEC_REG1,
  503. };
  504. static const struct meson_ir_param meson8b_ir_param = {
  505. .support_hw_decoder = false,
  506. .max_register = IR_DEC_REG2,
  507. };
  508. static const struct meson_ir_param meson_s4_ir_param = {
  509. .support_hw_decoder = true,
  510. .max_register = IR_DEC_FRAME1,
  511. };
  512. static const struct of_device_id meson_ir_match[] = {
  513. {
  514. .compatible = "amlogic,meson6-ir",
  515. .data = &meson6_ir_param,
  516. }, {
  517. .compatible = "amlogic,meson8b-ir",
  518. .data = &meson8b_ir_param,
  519. }, {
  520. .compatible = "amlogic,meson-gxbb-ir",
  521. .data = &meson8b_ir_param,
  522. }, {
  523. .compatible = "amlogic,meson-s4-ir",
  524. .data = &meson_s4_ir_param,
  525. },
  526. {},
  527. };
  528. MODULE_DEVICE_TABLE(of, meson_ir_match);
  529. static struct platform_driver meson_ir_driver = {
  530. .probe = meson_ir_probe,
  531. .remove_new = meson_ir_remove,
  532. .shutdown = meson_ir_shutdown,
  533. .driver = {
  534. .name = DRIVER_NAME,
  535. .of_match_table = meson_ir_match,
  536. .pm = pm_ptr(&meson_ir_pm_ops),
  537. },
  538. };
  539. module_platform_driver(meson_ir_driver);
  540. MODULE_DESCRIPTION("Amlogic Meson IR remote receiver driver");
  541. MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
  542. MODULE_LICENSE("GPL v2");