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