mtk-cir.c 12 KB

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