au0828-input.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. handle au0828 IR remotes via linux kernel input layer.
  3. Copyright (C) 2014 Mauro Carvalho Chehab <mchehab@samsung.com>
  4. Copyright (c) 2014 Samsung Electronics Co., Ltd.
  5. Based on em28xx-input.c.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. */
  15. #include "au0828.h"
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/usb.h>
  21. #include <linux/slab.h>
  22. #include <media/rc-core.h>
  23. static int disable_ir;
  24. module_param(disable_ir, int, 0444);
  25. MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
  26. struct au0828_rc {
  27. struct au0828_dev *dev;
  28. struct rc_dev *rc;
  29. char name[32];
  30. char phys[32];
  31. /* poll decoder */
  32. int polling;
  33. struct delayed_work work;
  34. /* i2c slave address of external device (if used) */
  35. u16 i2c_dev_addr;
  36. int (*get_key_i2c)(struct au0828_rc *ir);
  37. };
  38. /*
  39. * AU8522 has a builtin IR receiver. Add functions to get IR from it
  40. */
  41. static int au8522_rc_write(struct au0828_rc *ir, u16 reg, u8 data)
  42. {
  43. int rc;
  44. char buf[] = { (reg >> 8) | 0x80, reg & 0xff, data };
  45. struct i2c_msg msg = { .addr = ir->i2c_dev_addr, .flags = 0,
  46. .buf = buf, .len = sizeof(buf) };
  47. rc = i2c_transfer(ir->dev->i2c_client.adapter, &msg, 1);
  48. if (rc < 0)
  49. return rc;
  50. return (rc == 1) ? 0 : -EIO;
  51. }
  52. static int au8522_rc_read(struct au0828_rc *ir, u16 reg, int val,
  53. char *buf, int size)
  54. {
  55. int rc;
  56. char obuf[3];
  57. struct i2c_msg msg[2] = { { .addr = ir->i2c_dev_addr, .flags = 0,
  58. .buf = obuf, .len = 2 },
  59. { .addr = ir->i2c_dev_addr, .flags = I2C_M_RD,
  60. .buf = buf, .len = size } };
  61. obuf[0] = 0x40 | reg >> 8;
  62. obuf[1] = reg & 0xff;
  63. if (val >= 0) {
  64. obuf[2] = val;
  65. msg[0].len++;
  66. }
  67. rc = i2c_transfer(ir->dev->i2c_client.adapter, msg, 2);
  68. if (rc < 0)
  69. return rc;
  70. return (rc == 2) ? 0 : -EIO;
  71. }
  72. static int au8522_rc_andor(struct au0828_rc *ir, u16 reg, u8 mask, u8 value)
  73. {
  74. int rc;
  75. char buf, oldbuf;
  76. rc = au8522_rc_read(ir, reg, -1, &buf, 1);
  77. if (rc < 0)
  78. return rc;
  79. oldbuf = buf;
  80. buf = (buf & ~mask) | (value & mask);
  81. /* Nothing to do, just return */
  82. if (buf == oldbuf)
  83. return 0;
  84. return au8522_rc_write(ir, reg, buf);
  85. }
  86. #define au8522_rc_set(ir, reg, bit) au8522_rc_andor(ir, (reg), (bit), (bit))
  87. #define au8522_rc_clear(ir, reg, bit) au8522_rc_andor(ir, (reg), (bit), 0)
  88. /* Remote Controller time units */
  89. #define AU8522_UNIT 200000 /* ns */
  90. #define NEC_START_SPACE (4500000 / AU8522_UNIT)
  91. #define NEC_START_PULSE (562500 * 16)
  92. #define RC5_START_SPACE (4 * AU8522_UNIT)
  93. #define RC5_START_PULSE 888888
  94. static int au0828_get_key_au8522(struct au0828_rc *ir)
  95. {
  96. unsigned char buf[40];
  97. DEFINE_IR_RAW_EVENT(rawir);
  98. int i, j, rc;
  99. int prv_bit, bit, width;
  100. bool first = true;
  101. /* Check IR int */
  102. rc = au8522_rc_read(ir, 0xe1, -1, buf, 1);
  103. if (rc < 0 || !(buf[0] & (1 << 4))) {
  104. /* Be sure that IR is enabled */
  105. au8522_rc_set(ir, 0xe0, 1 << 4);
  106. return 0;
  107. }
  108. /* Something arrived. Get the data */
  109. rc = au8522_rc_read(ir, 0xe3, 0x11, buf, sizeof(buf));
  110. if (rc < 0)
  111. return rc;
  112. /* Disable IR */
  113. au8522_rc_clear(ir, 0xe0, 1 << 4);
  114. /* Enable IR */
  115. au8522_rc_set(ir, 0xe0, 1 << 4);
  116. dprintk(16, "RC data received: %*ph\n", 40, buf);
  117. prv_bit = (buf[0] >> 7) & 0x01;
  118. width = 0;
  119. for (i = 0; i < sizeof(buf); i++) {
  120. for (j = 7; j >= 0; j--) {
  121. bit = (buf[i] >> j) & 0x01;
  122. if (bit == prv_bit) {
  123. width++;
  124. continue;
  125. }
  126. /*
  127. * Fix an au8522 bug: the first pulse event
  128. * is lost. So, we need to fake it, based on the
  129. * protocol. That means that not all raw decoders
  130. * will work, as we need to add a hack for each
  131. * protocol, based on the first space.
  132. * So, we only support RC5 and NEC.
  133. */
  134. if (first) {
  135. first = false;
  136. init_ir_raw_event(&rawir);
  137. rawir.pulse = true;
  138. if (width > NEC_START_SPACE - 2 &&
  139. width < NEC_START_SPACE + 2) {
  140. /* NEC protocol */
  141. rawir.duration = NEC_START_PULSE;
  142. dprintk(16, "Storing NEC start %s with duration %d",
  143. rawir.pulse ? "pulse" : "space",
  144. rawir.duration);
  145. } else {
  146. /* RC5 protocol */
  147. rawir.duration = RC5_START_PULSE;
  148. dprintk(16, "Storing RC5 start %s with duration %d",
  149. rawir.pulse ? "pulse" : "space",
  150. rawir.duration);
  151. }
  152. ir_raw_event_store(ir->rc, &rawir);
  153. }
  154. init_ir_raw_event(&rawir);
  155. rawir.pulse = prv_bit ? false : true;
  156. rawir.duration = AU8522_UNIT * width;
  157. dprintk(16, "Storing %s with duration %d",
  158. rawir.pulse ? "pulse" : "space",
  159. rawir.duration);
  160. ir_raw_event_store(ir->rc, &rawir);
  161. width = 1;
  162. prv_bit = bit;
  163. }
  164. }
  165. init_ir_raw_event(&rawir);
  166. rawir.pulse = prv_bit ? false : true;
  167. rawir.duration = AU8522_UNIT * width;
  168. dprintk(16, "Storing end %s with duration %d",
  169. rawir.pulse ? "pulse" : "space",
  170. rawir.duration);
  171. ir_raw_event_store(ir->rc, &rawir);
  172. ir_raw_event_handle(ir->rc);
  173. return 1;
  174. }
  175. /*
  176. * Generic IR code
  177. */
  178. static void au0828_rc_work(struct work_struct *work)
  179. {
  180. struct au0828_rc *ir = container_of(work, struct au0828_rc, work.work);
  181. int rc;
  182. rc = ir->get_key_i2c(ir);
  183. if (rc < 0)
  184. pr_info("Error while getting RC scancode\n");
  185. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  186. }
  187. static int au0828_rc_start(struct rc_dev *rc)
  188. {
  189. struct au0828_rc *ir = rc->priv;
  190. INIT_DELAYED_WORK(&ir->work, au0828_rc_work);
  191. /* Enable IR */
  192. au8522_rc_set(ir, 0xe0, 1 << 4);
  193. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  194. return 0;
  195. }
  196. static void au0828_rc_stop(struct rc_dev *rc)
  197. {
  198. struct au0828_rc *ir = rc->priv;
  199. cancel_delayed_work_sync(&ir->work);
  200. /* Disable IR */
  201. au8522_rc_clear(ir, 0xe0, 1 << 4);
  202. }
  203. static int au0828_probe_i2c_ir(struct au0828_dev *dev)
  204. {
  205. int i = 0;
  206. const unsigned short addr_list[] = {
  207. 0x47, I2C_CLIENT_END
  208. };
  209. while (addr_list[i] != I2C_CLIENT_END) {
  210. if (i2c_probe_func_quick_read(dev->i2c_client.adapter,
  211. addr_list[i]) == 1)
  212. return addr_list[i];
  213. i++;
  214. }
  215. return -ENODEV;
  216. }
  217. int au0828_rc_register(struct au0828_dev *dev)
  218. {
  219. struct au0828_rc *ir;
  220. struct rc_dev *rc;
  221. int err = -ENOMEM;
  222. u16 i2c_rc_dev_addr = 0;
  223. if (!dev->board.has_ir_i2c || disable_ir)
  224. return 0;
  225. i2c_rc_dev_addr = au0828_probe_i2c_ir(dev);
  226. if (!i2c_rc_dev_addr)
  227. return -ENODEV;
  228. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  229. rc = rc_allocate_device();
  230. if (!ir || !rc)
  231. goto error;
  232. /* record handles to ourself */
  233. ir->dev = dev;
  234. dev->ir = ir;
  235. ir->rc = rc;
  236. rc->priv = ir;
  237. rc->open = au0828_rc_start;
  238. rc->close = au0828_rc_stop;
  239. if (dev->board.has_ir_i2c) { /* external i2c device */
  240. switch (dev->boardnr) {
  241. case AU0828_BOARD_HAUPPAUGE_HVR950Q:
  242. rc->map_name = RC_MAP_HAUPPAUGE;
  243. ir->get_key_i2c = au0828_get_key_au8522;
  244. break;
  245. default:
  246. err = -ENODEV;
  247. goto error;
  248. }
  249. ir->i2c_dev_addr = i2c_rc_dev_addr;
  250. }
  251. /* This is how often we ask the chip for IR information */
  252. ir->polling = 100; /* ms */
  253. /* init input device */
  254. snprintf(ir->name, sizeof(ir->name), "au0828 IR (%s)",
  255. dev->board.name);
  256. usb_make_path(dev->usbdev, ir->phys, sizeof(ir->phys));
  257. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  258. rc->input_name = ir->name;
  259. rc->input_phys = ir->phys;
  260. rc->input_id.bustype = BUS_USB;
  261. rc->input_id.version = 1;
  262. rc->input_id.vendor = le16_to_cpu(dev->usbdev->descriptor.idVendor);
  263. rc->input_id.product = le16_to_cpu(dev->usbdev->descriptor.idProduct);
  264. rc->dev.parent = &dev->usbdev->dev;
  265. rc->driver_name = "au0828-input";
  266. rc->driver_type = RC_DRIVER_IR_RAW;
  267. rc->allowed_protocols = RC_BIT_NEC | RC_BIT_RC5;
  268. /* all done */
  269. err = rc_register_device(rc);
  270. if (err)
  271. goto error;
  272. pr_info("Remote controller %s initalized\n", ir->name);
  273. return 0;
  274. error:
  275. dev->ir = NULL;
  276. rc_free_device(rc);
  277. kfree(ir);
  278. return err;
  279. }
  280. void au0828_rc_unregister(struct au0828_dev *dev)
  281. {
  282. struct au0828_rc *ir = dev->ir;
  283. /* skip detach on non attached boards */
  284. if (!ir)
  285. return;
  286. if (ir->rc)
  287. rc_unregister_device(ir->rc);
  288. /* done */
  289. kfree(ir);
  290. dev->ir = NULL;
  291. }
  292. int au0828_rc_suspend(struct au0828_dev *dev)
  293. {
  294. struct au0828_rc *ir = dev->ir;
  295. if (!ir)
  296. return 0;
  297. pr_info("Stopping RC\n");
  298. cancel_delayed_work_sync(&ir->work);
  299. /* Disable IR */
  300. au8522_rc_clear(ir, 0xe0, 1 << 4);
  301. return 0;
  302. }
  303. int au0828_rc_resume(struct au0828_dev *dev)
  304. {
  305. struct au0828_rc *ir = dev->ir;
  306. if (!ir)
  307. return 0;
  308. pr_info("Restarting RC\n");
  309. /* Enable IR */
  310. au8522_rc_set(ir, 0xe0, 1 << 4);
  311. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  312. return 0;
  313. }