mtk-cir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Mediatek IR Receiver Controller
  4. *
  5. * Copyright (C) 2017 Sean Wang <[email protected]>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/reset.h>
  12. #include <media/rc-core.h>
  13. #define MTK_IR_DEV KBUILD_MODNAME
  14. /* Register to enable PWM and IR */
  15. #define MTK_CONFIG_HIGH_REG 0x0c
  16. /* Bit to enable IR pulse width detection */
  17. #define MTK_PWM_EN BIT(13)
  18. /*
  19. * Register to setting ok count whose unit based on hardware sampling period
  20. * indicating IR receiving completion and then making IRQ fires
  21. */
  22. #define MTK_OK_COUNT_MASK (GENMASK(22, 16))
  23. #define MTK_OK_COUNT(x) ((x) << 16)
  24. /* Bit to enable IR hardware function */
  25. #define MTK_IR_EN BIT(0)
  26. /* Bit to restart IR receiving */
  27. #define MTK_IRCLR BIT(0)
  28. /* Fields containing pulse width data */
  29. #define MTK_WIDTH_MASK (GENMASK(7, 0))
  30. /* IR threshold */
  31. #define MTK_IRTHD 0x14
  32. #define MTK_DG_CNT_MASK (GENMASK(12, 8))
  33. #define MTK_DG_CNT(x) ((x) << 8)
  34. /* Bit to enable interrupt */
  35. #define MTK_IRINT_EN BIT(0)
  36. /* Bit to clear interrupt status */
  37. #define MTK_IRINT_CLR BIT(0)
  38. /* Maximum count of samples */
  39. #define MTK_MAX_SAMPLES 0xff
  40. /* Indicate the end of IR message */
  41. #define MTK_IR_END(v, p) ((v) == MTK_MAX_SAMPLES && (p) == 0)
  42. /* Number of registers to record the pulse width */
  43. #define MTK_CHKDATA_SZ 17
  44. /* Sample period in us */
  45. #define MTK_IR_SAMPLE 46
  46. enum mtk_fields {
  47. /* Register to setting software sampling period */
  48. MTK_CHK_PERIOD,
  49. /* Register to setting hardware sampling period */
  50. MTK_HW_PERIOD,
  51. };
  52. enum mtk_regs {
  53. /* Register to clear state of state machine */
  54. MTK_IRCLR_REG,
  55. /* Register containing pulse width data */
  56. MTK_CHKDATA_REG,
  57. /* Register to enable IR interrupt */
  58. MTK_IRINT_EN_REG,
  59. /* Register to ack IR interrupt */
  60. MTK_IRINT_CLR_REG
  61. };
  62. static const u32 mt7623_regs[] = {
  63. [MTK_IRCLR_REG] = 0x20,
  64. [MTK_CHKDATA_REG] = 0x88,
  65. [MTK_IRINT_EN_REG] = 0xcc,
  66. [MTK_IRINT_CLR_REG] = 0xd0,
  67. };
  68. static const u32 mt7622_regs[] = {
  69. [MTK_IRCLR_REG] = 0x18,
  70. [MTK_CHKDATA_REG] = 0x30,
  71. [MTK_IRINT_EN_REG] = 0x1c,
  72. [MTK_IRINT_CLR_REG] = 0x20,
  73. };
  74. struct mtk_field_type {
  75. u32 reg;
  76. u8 offset;
  77. u32 mask;
  78. };
  79. /*
  80. * struct mtk_ir_data - This is the structure holding all differences among
  81. various hardwares
  82. * @regs: The pointer to the array holding registers offset
  83. * @fields: The pointer to the array holding fields location
  84. * @div: The internal divisor for the based reference clock
  85. * @ok_count: The count indicating the completion of IR data
  86. * receiving when count is reached
  87. * @hw_period: The value indicating the hardware sampling period
  88. */
  89. struct mtk_ir_data {
  90. const u32 *regs;
  91. const struct mtk_field_type *fields;
  92. u8 div;
  93. u8 ok_count;
  94. u32 hw_period;
  95. };
  96. static const struct mtk_field_type mt7623_fields[] = {
  97. [MTK_CHK_PERIOD] = {0x10, 8, GENMASK(20, 8)},
  98. [MTK_HW_PERIOD] = {0x10, 0, GENMASK(7, 0)},
  99. };
  100. static const struct mtk_field_type mt7622_fields[] = {
  101. [MTK_CHK_PERIOD] = {0x24, 0, GENMASK(24, 0)},
  102. [MTK_HW_PERIOD] = {0x10, 0, GENMASK(24, 0)},
  103. };
  104. /*
  105. * struct mtk_ir - This is the main datasructure for holding the state
  106. * of the driver
  107. * @dev: The device pointer
  108. * @rc: The rc instrance
  109. * @base: The mapped register i/o base
  110. * @irq: The IRQ that we are using
  111. * @clk: The clock that IR internal is using
  112. * @bus: The clock that software decoder is using
  113. * @data: Holding specific data for vaious platform
  114. */
  115. struct mtk_ir {
  116. struct device *dev;
  117. struct rc_dev *rc;
  118. void __iomem *base;
  119. int irq;
  120. struct clk *clk;
  121. struct clk *bus;
  122. const struct mtk_ir_data *data;
  123. };
  124. static inline u32 mtk_chkdata_reg(struct mtk_ir *ir, u32 i)
  125. {
  126. return ir->data->regs[MTK_CHKDATA_REG] + 4 * i;
  127. }
  128. static inline u32 mtk_chk_period(struct mtk_ir *ir)
  129. {
  130. u32 val;
  131. /*
  132. * Period for software decoder used in the
  133. * unit of raw software sampling
  134. */
  135. val = DIV_ROUND_CLOSEST(clk_get_rate(ir->bus),
  136. USEC_PER_SEC * ir->data->div / MTK_IR_SAMPLE);
  137. dev_dbg(ir->dev, "@pwm clk = \t%lu\n",
  138. clk_get_rate(ir->bus) / ir->data->div);
  139. dev_dbg(ir->dev, "@chkperiod = %08x\n", val);
  140. return val;
  141. }
  142. static void mtk_w32_mask(struct mtk_ir *ir, u32 val, u32 mask, unsigned int reg)
  143. {
  144. u32 tmp;
  145. tmp = __raw_readl(ir->base + reg);
  146. tmp = (tmp & ~mask) | val;
  147. __raw_writel(tmp, ir->base + reg);
  148. }
  149. static void mtk_w32(struct mtk_ir *ir, u32 val, unsigned int reg)
  150. {
  151. __raw_writel(val, ir->base + reg);
  152. }
  153. static u32 mtk_r32(struct mtk_ir *ir, unsigned int reg)
  154. {
  155. return __raw_readl(ir->base + reg);
  156. }
  157. static inline void mtk_irq_disable(struct mtk_ir *ir, u32 mask)
  158. {
  159. u32 val;
  160. val = mtk_r32(ir, ir->data->regs[MTK_IRINT_EN_REG]);
  161. mtk_w32(ir, val & ~mask, ir->data->regs[MTK_IRINT_EN_REG]);
  162. }
  163. static inline void mtk_irq_enable(struct mtk_ir *ir, u32 mask)
  164. {
  165. u32 val;
  166. val = mtk_r32(ir, ir->data->regs[MTK_IRINT_EN_REG]);
  167. mtk_w32(ir, val | mask, ir->data->regs[MTK_IRINT_EN_REG]);
  168. }
  169. static irqreturn_t mtk_ir_irq(int irqno, void *dev_id)
  170. {
  171. struct ir_raw_event rawir = {};
  172. struct mtk_ir *ir = dev_id;
  173. u32 i, j, val;
  174. u8 wid;
  175. /*
  176. * Each pulse and space is encoded as a single byte, each byte
  177. * alternating between pulse and space. If a pulse or space is longer
  178. * than can be encoded in a single byte, it is encoded as the maximum
  179. * value 0xff.
  180. *
  181. * If a space is longer than ok_count (about 23ms), the value is
  182. * encoded as zero, and all following bytes are zero. Any IR that
  183. * follows will be presented in the next interrupt.
  184. *
  185. * If there are more than 68 (=MTK_CHKDATA_SZ * 4) pulses and spaces,
  186. * then the only the first 68 will be presented; the rest is lost.
  187. */
  188. /* Handle all pulse and space IR controller captures */
  189. for (i = 0 ; i < MTK_CHKDATA_SZ ; i++) {
  190. val = mtk_r32(ir, mtk_chkdata_reg(ir, i));
  191. dev_dbg(ir->dev, "@reg%d=0x%08x\n", i, val);
  192. for (j = 0 ; j < 4 ; j++) {
  193. wid = val & MTK_WIDTH_MASK;
  194. val >>= 8;
  195. rawir.pulse = !rawir.pulse;
  196. rawir.duration = wid * (MTK_IR_SAMPLE + 1);
  197. ir_raw_event_store_with_filter(ir->rc, &rawir);
  198. }
  199. }
  200. /*
  201. * The maximum number of edges the IR controller can
  202. * hold is MTK_CHKDATA_SZ * 4. So if received IR messages
  203. * is over the limit, the last incomplete IR message would
  204. * be appended trailing space and still would be sent into
  205. * ir-rc-raw to decode. That helps it is possible that it
  206. * has enough information to decode a scancode even if the
  207. * trailing end of the message is missing.
  208. */
  209. if (!MTK_IR_END(wid, rawir.pulse)) {
  210. rawir.pulse = false;
  211. rawir.duration = MTK_MAX_SAMPLES * (MTK_IR_SAMPLE + 1);
  212. ir_raw_event_store_with_filter(ir->rc, &rawir);
  213. }
  214. ir_raw_event_handle(ir->rc);
  215. /*
  216. * Restart controller for the next receive that would
  217. * clear up all CHKDATA registers
  218. */
  219. mtk_w32_mask(ir, 0x1, MTK_IRCLR, ir->data->regs[MTK_IRCLR_REG]);
  220. /* Clear interrupt status */
  221. mtk_w32_mask(ir, 0x1, MTK_IRINT_CLR,
  222. ir->data->regs[MTK_IRINT_CLR_REG]);
  223. return IRQ_HANDLED;
  224. }
  225. static const struct mtk_ir_data mt7623_data = {
  226. .regs = mt7623_regs,
  227. .fields = mt7623_fields,
  228. .ok_count = 3,
  229. .hw_period = 0xff,
  230. .div = 4,
  231. };
  232. static const struct mtk_ir_data mt7622_data = {
  233. .regs = mt7622_regs,
  234. .fields = mt7622_fields,
  235. .ok_count = 3,
  236. .hw_period = 0xffff,
  237. .div = 32,
  238. };
  239. static const struct of_device_id mtk_ir_match[] = {
  240. { .compatible = "mediatek,mt7623-cir", .data = &mt7623_data},
  241. { .compatible = "mediatek,mt7622-cir", .data = &mt7622_data},
  242. {},
  243. };
  244. MODULE_DEVICE_TABLE(of, mtk_ir_match);
  245. static int mtk_ir_probe(struct platform_device *pdev)
  246. {
  247. struct device *dev = &pdev->dev;
  248. struct device_node *dn = dev->of_node;
  249. struct mtk_ir *ir;
  250. u32 val;
  251. int ret = 0;
  252. const char *map_name;
  253. ir = devm_kzalloc(dev, sizeof(struct mtk_ir), GFP_KERNEL);
  254. if (!ir)
  255. return -ENOMEM;
  256. ir->dev = dev;
  257. ir->data = of_device_get_match_data(dev);
  258. ir->clk = devm_clk_get(dev, "clk");
  259. if (IS_ERR(ir->clk)) {
  260. dev_err(dev, "failed to get a ir clock.\n");
  261. return PTR_ERR(ir->clk);
  262. }
  263. ir->bus = devm_clk_get(dev, "bus");
  264. if (IS_ERR(ir->bus)) {
  265. /*
  266. * For compatibility with older device trees try unnamed
  267. * ir->bus uses the same clock as ir->clock.
  268. */
  269. ir->bus = ir->clk;
  270. }
  271. ir->base = devm_platform_ioremap_resource(pdev, 0);
  272. if (IS_ERR(ir->base))
  273. return PTR_ERR(ir->base);
  274. ir->rc = devm_rc_allocate_device(dev, RC_DRIVER_IR_RAW);
  275. if (!ir->rc) {
  276. dev_err(dev, "failed to allocate device\n");
  277. return -ENOMEM;
  278. }
  279. ir->rc->priv = ir;
  280. ir->rc->device_name = MTK_IR_DEV;
  281. ir->rc->input_phys = MTK_IR_DEV "/input0";
  282. ir->rc->input_id.bustype = BUS_HOST;
  283. ir->rc->input_id.vendor = 0x0001;
  284. ir->rc->input_id.product = 0x0001;
  285. ir->rc->input_id.version = 0x0001;
  286. map_name = of_get_property(dn, "linux,rc-map-name", NULL);
  287. ir->rc->map_name = map_name ?: RC_MAP_EMPTY;
  288. ir->rc->dev.parent = dev;
  289. ir->rc->driver_name = MTK_IR_DEV;
  290. ir->rc->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  291. ir->rc->rx_resolution = MTK_IR_SAMPLE;
  292. ir->rc->timeout = MTK_MAX_SAMPLES * (MTK_IR_SAMPLE + 1);
  293. ret = devm_rc_register_device(dev, ir->rc);
  294. if (ret) {
  295. dev_err(dev, "failed to register rc device\n");
  296. return ret;
  297. }
  298. platform_set_drvdata(pdev, ir);
  299. ir->irq = platform_get_irq(pdev, 0);
  300. if (ir->irq < 0)
  301. return -ENODEV;
  302. if (clk_prepare_enable(ir->clk)) {
  303. dev_err(dev, "try to enable ir_clk failed\n");
  304. return -EINVAL;
  305. }
  306. if (clk_prepare_enable(ir->bus)) {
  307. dev_err(dev, "try to enable ir_clk failed\n");
  308. ret = -EINVAL;
  309. goto exit_clkdisable_clk;
  310. }
  311. /*
  312. * Enable interrupt after proper hardware
  313. * setup and IRQ handler registration
  314. */
  315. mtk_irq_disable(ir, MTK_IRINT_EN);
  316. ret = devm_request_irq(dev, ir->irq, mtk_ir_irq, 0, MTK_IR_DEV, ir);
  317. if (ret) {
  318. dev_err(dev, "failed request irq\n");
  319. goto exit_clkdisable_bus;
  320. }
  321. /*
  322. * Setup software sample period as the reference of software decoder
  323. */
  324. val = (mtk_chk_period(ir) << ir->data->fields[MTK_CHK_PERIOD].offset) &
  325. ir->data->fields[MTK_CHK_PERIOD].mask;
  326. mtk_w32_mask(ir, val, ir->data->fields[MTK_CHK_PERIOD].mask,
  327. ir->data->fields[MTK_CHK_PERIOD].reg);
  328. /*
  329. * Setup hardware sampling period used to setup the proper timeout for
  330. * indicating end of IR receiving completion
  331. */
  332. val = (ir->data->hw_period << ir->data->fields[MTK_HW_PERIOD].offset) &
  333. ir->data->fields[MTK_HW_PERIOD].mask;
  334. mtk_w32_mask(ir, val, ir->data->fields[MTK_HW_PERIOD].mask,
  335. ir->data->fields[MTK_HW_PERIOD].reg);
  336. /* Set de-glitch counter */
  337. mtk_w32_mask(ir, MTK_DG_CNT(1), MTK_DG_CNT_MASK, MTK_IRTHD);
  338. /* Enable IR and PWM */
  339. val = mtk_r32(ir, MTK_CONFIG_HIGH_REG) & ~MTK_OK_COUNT_MASK;
  340. val |= MTK_OK_COUNT(ir->data->ok_count) | MTK_PWM_EN | MTK_IR_EN;
  341. mtk_w32(ir, val, MTK_CONFIG_HIGH_REG);
  342. mtk_irq_enable(ir, MTK_IRINT_EN);
  343. dev_info(dev, "Initialized MT7623 IR driver, sample period = %dus\n",
  344. MTK_IR_SAMPLE);
  345. return 0;
  346. exit_clkdisable_bus:
  347. clk_disable_unprepare(ir->bus);
  348. exit_clkdisable_clk:
  349. clk_disable_unprepare(ir->clk);
  350. return ret;
  351. }
  352. static int mtk_ir_remove(struct platform_device *pdev)
  353. {
  354. struct mtk_ir *ir = platform_get_drvdata(pdev);
  355. /*
  356. * Avoid contention between remove handler and
  357. * IRQ handler so that disabling IR interrupt and
  358. * waiting for pending IRQ handler to complete
  359. */
  360. mtk_irq_disable(ir, MTK_IRINT_EN);
  361. synchronize_irq(ir->irq);
  362. clk_disable_unprepare(ir->bus);
  363. clk_disable_unprepare(ir->clk);
  364. return 0;
  365. }
  366. static struct platform_driver mtk_ir_driver = {
  367. .probe = mtk_ir_probe,
  368. .remove = mtk_ir_remove,
  369. .driver = {
  370. .name = MTK_IR_DEV,
  371. .of_match_table = mtk_ir_match,
  372. },
  373. };
  374. module_platform_driver(mtk_ir_driver);
  375. MODULE_DESCRIPTION("Mediatek IR Receiver Controller Driver");
  376. MODULE_AUTHOR("Sean Wang <[email protected]>");
  377. MODULE_LICENSE("GPL");