ir-hix5hd2.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 2014 Linaro Ltd.
  3. * Copyright (c) 2014 Hisilicon Limited.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/regmap.h>
  16. #include <media/rc-core.h>
  17. #define IR_ENABLE 0x00
  18. #define IR_CONFIG 0x04
  19. #define CNT_LEADS 0x08
  20. #define CNT_LEADE 0x0c
  21. #define CNT_SLEADE 0x10
  22. #define CNT0_B 0x14
  23. #define CNT1_B 0x18
  24. #define IR_BUSY 0x1c
  25. #define IR_DATAH 0x20
  26. #define IR_DATAL 0x24
  27. #define IR_INTM 0x28
  28. #define IR_INTS 0x2c
  29. #define IR_INTC 0x30
  30. #define IR_START 0x34
  31. /* interrupt mask */
  32. #define INTMS_SYMBRCV (BIT(24) | BIT(8))
  33. #define INTMS_TIMEOUT (BIT(25) | BIT(9))
  34. #define INTMS_OVERFLOW (BIT(26) | BIT(10))
  35. #define INT_CLR_OVERFLOW BIT(18)
  36. #define INT_CLR_TIMEOUT BIT(17)
  37. #define INT_CLR_RCV BIT(16)
  38. #define INT_CLR_RCVTIMEOUT (BIT(16) | BIT(17))
  39. #define IR_CLK 0x48
  40. #define IR_CLK_ENABLE BIT(4)
  41. #define IR_CLK_RESET BIT(5)
  42. #define IR_CFG_WIDTH_MASK 0xffff
  43. #define IR_CFG_WIDTH_SHIFT 16
  44. #define IR_CFG_FORMAT_MASK 0x3
  45. #define IR_CFG_FORMAT_SHIFT 14
  46. #define IR_CFG_INT_LEVEL_MASK 0x3f
  47. #define IR_CFG_INT_LEVEL_SHIFT 8
  48. /* only support raw mode */
  49. #define IR_CFG_MODE_RAW BIT(7)
  50. #define IR_CFG_FREQ_MASK 0x7f
  51. #define IR_CFG_FREQ_SHIFT 0
  52. #define IR_CFG_INT_THRESHOLD 1
  53. /* symbol start from low to high, symbol stream end at high*/
  54. #define IR_CFG_SYMBOL_FMT 0
  55. #define IR_CFG_SYMBOL_MAXWIDTH 0x3e80
  56. #define IR_HIX5HD2_NAME "hix5hd2-ir"
  57. struct hix5hd2_ir_priv {
  58. int irq;
  59. void __iomem *base;
  60. struct device *dev;
  61. struct rc_dev *rdev;
  62. struct regmap *regmap;
  63. struct clk *clock;
  64. unsigned long rate;
  65. };
  66. static int hix5hd2_ir_enable(struct hix5hd2_ir_priv *dev, bool on)
  67. {
  68. u32 val;
  69. int ret = 0;
  70. if (dev->regmap) {
  71. regmap_read(dev->regmap, IR_CLK, &val);
  72. if (on) {
  73. val &= ~IR_CLK_RESET;
  74. val |= IR_CLK_ENABLE;
  75. } else {
  76. val &= ~IR_CLK_ENABLE;
  77. val |= IR_CLK_RESET;
  78. }
  79. regmap_write(dev->regmap, IR_CLK, val);
  80. } else {
  81. if (on)
  82. ret = clk_prepare_enable(dev->clock);
  83. else
  84. clk_disable_unprepare(dev->clock);
  85. }
  86. return ret;
  87. }
  88. static int hix5hd2_ir_config(struct hix5hd2_ir_priv *priv)
  89. {
  90. int timeout = 10000;
  91. u32 val, rate;
  92. writel_relaxed(0x01, priv->base + IR_ENABLE);
  93. while (readl_relaxed(priv->base + IR_BUSY)) {
  94. if (timeout--) {
  95. udelay(1);
  96. } else {
  97. dev_err(priv->dev, "IR_BUSY timeout\n");
  98. return -ETIMEDOUT;
  99. }
  100. }
  101. /* Now only support raw mode, with symbol start from low to high */
  102. rate = DIV_ROUND_CLOSEST(priv->rate, 1000000);
  103. val = IR_CFG_SYMBOL_MAXWIDTH & IR_CFG_WIDTH_MASK << IR_CFG_WIDTH_SHIFT;
  104. val |= IR_CFG_SYMBOL_FMT & IR_CFG_FORMAT_MASK << IR_CFG_FORMAT_SHIFT;
  105. val |= (IR_CFG_INT_THRESHOLD - 1) & IR_CFG_INT_LEVEL_MASK
  106. << IR_CFG_INT_LEVEL_SHIFT;
  107. val |= IR_CFG_MODE_RAW;
  108. val |= (rate - 1) & IR_CFG_FREQ_MASK << IR_CFG_FREQ_SHIFT;
  109. writel_relaxed(val, priv->base + IR_CONFIG);
  110. writel_relaxed(0x00, priv->base + IR_INTM);
  111. /* write arbitrary value to start */
  112. writel_relaxed(0x01, priv->base + IR_START);
  113. return 0;
  114. }
  115. static int hix5hd2_ir_open(struct rc_dev *rdev)
  116. {
  117. struct hix5hd2_ir_priv *priv = rdev->priv;
  118. int ret;
  119. ret = hix5hd2_ir_enable(priv, true);
  120. if (ret)
  121. return ret;
  122. ret = hix5hd2_ir_config(priv);
  123. if (ret) {
  124. hix5hd2_ir_enable(priv, false);
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. static void hix5hd2_ir_close(struct rc_dev *rdev)
  130. {
  131. struct hix5hd2_ir_priv *priv = rdev->priv;
  132. hix5hd2_ir_enable(priv, false);
  133. }
  134. static irqreturn_t hix5hd2_ir_rx_interrupt(int irq, void *data)
  135. {
  136. u32 symb_num, symb_val, symb_time;
  137. u32 data_l, data_h;
  138. u32 irq_sr, i;
  139. struct hix5hd2_ir_priv *priv = data;
  140. irq_sr = readl_relaxed(priv->base + IR_INTS);
  141. if (irq_sr & INTMS_OVERFLOW) {
  142. /*
  143. * we must read IR_DATAL first, then we can clean up
  144. * IR_INTS availably since logic would not clear
  145. * fifo when overflow, drv do the job
  146. */
  147. ir_raw_event_reset(priv->rdev);
  148. symb_num = readl_relaxed(priv->base + IR_DATAH);
  149. for (i = 0; i < symb_num; i++)
  150. readl_relaxed(priv->base + IR_DATAL);
  151. writel_relaxed(INT_CLR_OVERFLOW, priv->base + IR_INTC);
  152. dev_info(priv->dev, "overflow, level=%d\n",
  153. IR_CFG_INT_THRESHOLD);
  154. }
  155. if ((irq_sr & INTMS_SYMBRCV) || (irq_sr & INTMS_TIMEOUT)) {
  156. DEFINE_IR_RAW_EVENT(ev);
  157. symb_num = readl_relaxed(priv->base + IR_DATAH);
  158. for (i = 0; i < symb_num; i++) {
  159. symb_val = readl_relaxed(priv->base + IR_DATAL);
  160. data_l = ((symb_val & 0xffff) * 10);
  161. data_h = ((symb_val >> 16) & 0xffff) * 10;
  162. symb_time = (data_l + data_h) / 10;
  163. ev.duration = US_TO_NS(data_l);
  164. ev.pulse = true;
  165. ir_raw_event_store(priv->rdev, &ev);
  166. if (symb_time < IR_CFG_SYMBOL_MAXWIDTH) {
  167. ev.duration = US_TO_NS(data_h);
  168. ev.pulse = false;
  169. ir_raw_event_store(priv->rdev, &ev);
  170. } else {
  171. ir_raw_event_set_idle(priv->rdev, true);
  172. }
  173. }
  174. if (irq_sr & INTMS_SYMBRCV)
  175. writel_relaxed(INT_CLR_RCV, priv->base + IR_INTC);
  176. if (irq_sr & INTMS_TIMEOUT)
  177. writel_relaxed(INT_CLR_TIMEOUT, priv->base + IR_INTC);
  178. }
  179. /* Empty software fifo */
  180. ir_raw_event_handle(priv->rdev);
  181. return IRQ_HANDLED;
  182. }
  183. static int hix5hd2_ir_probe(struct platform_device *pdev)
  184. {
  185. struct rc_dev *rdev;
  186. struct device *dev = &pdev->dev;
  187. struct resource *res;
  188. struct hix5hd2_ir_priv *priv;
  189. struct device_node *node = pdev->dev.of_node;
  190. const char *map_name;
  191. int ret;
  192. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  193. if (!priv)
  194. return -ENOMEM;
  195. priv->regmap = syscon_regmap_lookup_by_phandle(node,
  196. "hisilicon,power-syscon");
  197. if (IS_ERR(priv->regmap)) {
  198. dev_info(dev, "no power-reg\n");
  199. priv->regmap = NULL;
  200. }
  201. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  202. priv->base = devm_ioremap_resource(dev, res);
  203. if (IS_ERR(priv->base))
  204. return PTR_ERR(priv->base);
  205. priv->irq = platform_get_irq(pdev, 0);
  206. if (priv->irq < 0) {
  207. dev_err(dev, "irq can not get\n");
  208. return priv->irq;
  209. }
  210. rdev = rc_allocate_device(RC_DRIVER_IR_RAW);
  211. if (!rdev)
  212. return -ENOMEM;
  213. priv->clock = devm_clk_get(dev, NULL);
  214. if (IS_ERR(priv->clock)) {
  215. dev_err(dev, "clock not found\n");
  216. ret = PTR_ERR(priv->clock);
  217. goto err;
  218. }
  219. ret = clk_prepare_enable(priv->clock);
  220. if (ret)
  221. goto err;
  222. priv->rate = clk_get_rate(priv->clock);
  223. rdev->allowed_protocols = RC_PROTO_BIT_ALL_IR_DECODER;
  224. rdev->priv = priv;
  225. rdev->open = hix5hd2_ir_open;
  226. rdev->close = hix5hd2_ir_close;
  227. rdev->driver_name = IR_HIX5HD2_NAME;
  228. map_name = of_get_property(node, "linux,rc-map-name", NULL);
  229. rdev->map_name = map_name ?: RC_MAP_EMPTY;
  230. rdev->device_name = IR_HIX5HD2_NAME;
  231. rdev->input_phys = IR_HIX5HD2_NAME "/input0";
  232. rdev->input_id.bustype = BUS_HOST;
  233. rdev->input_id.vendor = 0x0001;
  234. rdev->input_id.product = 0x0001;
  235. rdev->input_id.version = 0x0100;
  236. rdev->rx_resolution = US_TO_NS(10);
  237. rdev->timeout = US_TO_NS(IR_CFG_SYMBOL_MAXWIDTH * 10);
  238. ret = rc_register_device(rdev);
  239. if (ret < 0)
  240. goto clkerr;
  241. if (devm_request_irq(dev, priv->irq, hix5hd2_ir_rx_interrupt,
  242. 0, pdev->name, priv) < 0) {
  243. dev_err(dev, "IRQ %d register failed\n", priv->irq);
  244. ret = -EINVAL;
  245. goto regerr;
  246. }
  247. priv->rdev = rdev;
  248. priv->dev = dev;
  249. platform_set_drvdata(pdev, priv);
  250. return ret;
  251. regerr:
  252. rc_unregister_device(rdev);
  253. rdev = NULL;
  254. clkerr:
  255. clk_disable_unprepare(priv->clock);
  256. err:
  257. rc_free_device(rdev);
  258. dev_err(dev, "Unable to register device (%d)\n", ret);
  259. return ret;
  260. }
  261. static int hix5hd2_ir_remove(struct platform_device *pdev)
  262. {
  263. struct hix5hd2_ir_priv *priv = platform_get_drvdata(pdev);
  264. clk_disable_unprepare(priv->clock);
  265. rc_unregister_device(priv->rdev);
  266. return 0;
  267. }
  268. #ifdef CONFIG_PM_SLEEP
  269. static int hix5hd2_ir_suspend(struct device *dev)
  270. {
  271. struct hix5hd2_ir_priv *priv = dev_get_drvdata(dev);
  272. clk_disable_unprepare(priv->clock);
  273. hix5hd2_ir_enable(priv, false);
  274. return 0;
  275. }
  276. static int hix5hd2_ir_resume(struct device *dev)
  277. {
  278. struct hix5hd2_ir_priv *priv = dev_get_drvdata(dev);
  279. int ret;
  280. ret = hix5hd2_ir_enable(priv, true);
  281. if (ret)
  282. return ret;
  283. ret = clk_prepare_enable(priv->clock);
  284. if (ret) {
  285. hix5hd2_ir_enable(priv, false);
  286. return ret;
  287. }
  288. writel_relaxed(0x01, priv->base + IR_ENABLE);
  289. writel_relaxed(0x00, priv->base + IR_INTM);
  290. writel_relaxed(0xff, priv->base + IR_INTC);
  291. writel_relaxed(0x01, priv->base + IR_START);
  292. return 0;
  293. }
  294. #endif
  295. static SIMPLE_DEV_PM_OPS(hix5hd2_ir_pm_ops, hix5hd2_ir_suspend,
  296. hix5hd2_ir_resume);
  297. static const struct of_device_id hix5hd2_ir_table[] = {
  298. { .compatible = "hisilicon,hix5hd2-ir", },
  299. {},
  300. };
  301. MODULE_DEVICE_TABLE(of, hix5hd2_ir_table);
  302. static struct platform_driver hix5hd2_ir_driver = {
  303. .driver = {
  304. .name = IR_HIX5HD2_NAME,
  305. .of_match_table = hix5hd2_ir_table,
  306. .pm = &hix5hd2_ir_pm_ops,
  307. },
  308. .probe = hix5hd2_ir_probe,
  309. .remove = hix5hd2_ir_remove,
  310. };
  311. module_platform_driver(hix5hd2_ir_driver);
  312. MODULE_DESCRIPTION("IR controller driver for hix5hd2 platforms");
  313. MODULE_AUTHOR("Guoxiong Yan <yanguoxiong@huawei.com>");
  314. MODULE_LICENSE("GPL v2");
  315. MODULE_ALIAS("platform:hix5hd2-ir");