mtk-cir.c 12 KB

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