au0828-input.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /* do nothing if device is disconnected */
  102. if (test_bit(DEV_DISCONNECTED, &ir->dev->dev_state))
  103. return 0;
  104. /* Check IR int */
  105. rc = au8522_rc_read(ir, 0xe1, -1, buf, 1);
  106. if (rc < 0 || !(buf[0] & (1 << 4))) {
  107. /* Be sure that IR is enabled */
  108. au8522_rc_set(ir, 0xe0, 1 << 4);
  109. return 0;
  110. }
  111. /* Something arrived. Get the data */
  112. rc = au8522_rc_read(ir, 0xe3, 0x11, buf, sizeof(buf));
  113. if (rc < 0)
  114. return rc;
  115. /* Disable IR */
  116. au8522_rc_clear(ir, 0xe0, 1 << 4);
  117. /* Enable IR */
  118. au8522_rc_set(ir, 0xe0, 1 << 4);
  119. dprintk(16, "RC data received: %*ph\n", 40, buf);
  120. prv_bit = (buf[0] >> 7) & 0x01;
  121. width = 0;
  122. for (i = 0; i < sizeof(buf); i++) {
  123. for (j = 7; j >= 0; j--) {
  124. bit = (buf[i] >> j) & 0x01;
  125. if (bit == prv_bit) {
  126. width++;
  127. continue;
  128. }
  129. /*
  130. * Fix an au8522 bug: the first pulse event
  131. * is lost. So, we need to fake it, based on the
  132. * protocol. That means that not all raw decoders
  133. * will work, as we need to add a hack for each
  134. * protocol, based on the first space.
  135. * So, we only support RC5 and NEC.
  136. */
  137. if (first) {
  138. first = false;
  139. init_ir_raw_event(&rawir);
  140. rawir.pulse = true;
  141. if (width > NEC_START_SPACE - 2 &&
  142. width < NEC_START_SPACE + 2) {
  143. /* NEC protocol */
  144. rawir.duration = NEC_START_PULSE;
  145. dprintk(16, "Storing NEC start %s with duration %d",
  146. rawir.pulse ? "pulse" : "space",
  147. rawir.duration);
  148. } else {
  149. /* RC5 protocol */
  150. rawir.duration = RC5_START_PULSE;
  151. dprintk(16, "Storing RC5 start %s with duration %d",
  152. rawir.pulse ? "pulse" : "space",
  153. rawir.duration);
  154. }
  155. ir_raw_event_store(ir->rc, &rawir);
  156. }
  157. init_ir_raw_event(&rawir);
  158. rawir.pulse = prv_bit ? false : true;
  159. rawir.duration = AU8522_UNIT * width;
  160. dprintk(16, "Storing %s with duration %d",
  161. rawir.pulse ? "pulse" : "space",
  162. rawir.duration);
  163. ir_raw_event_store(ir->rc, &rawir);
  164. width = 1;
  165. prv_bit = bit;
  166. }
  167. }
  168. init_ir_raw_event(&rawir);
  169. rawir.pulse = prv_bit ? false : true;
  170. rawir.duration = AU8522_UNIT * width;
  171. dprintk(16, "Storing end %s with duration %d",
  172. rawir.pulse ? "pulse" : "space",
  173. rawir.duration);
  174. ir_raw_event_store(ir->rc, &rawir);
  175. ir_raw_event_handle(ir->rc);
  176. return 1;
  177. }
  178. /*
  179. * Generic IR code
  180. */
  181. static void au0828_rc_work(struct work_struct *work)
  182. {
  183. struct au0828_rc *ir = container_of(work, struct au0828_rc, work.work);
  184. int rc;
  185. rc = ir->get_key_i2c(ir);
  186. if (rc < 0)
  187. pr_info("Error while getting RC scancode\n");
  188. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  189. }
  190. static int au0828_rc_start(struct rc_dev *rc)
  191. {
  192. struct au0828_rc *ir = rc->priv;
  193. INIT_DELAYED_WORK(&ir->work, au0828_rc_work);
  194. /* Enable IR */
  195. au8522_rc_set(ir, 0xe0, 1 << 4);
  196. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  197. return 0;
  198. }
  199. static void au0828_rc_stop(struct rc_dev *rc)
  200. {
  201. struct au0828_rc *ir = rc->priv;
  202. cancel_delayed_work_sync(&ir->work);
  203. /* do nothing if device is disconnected */
  204. if (!test_bit(DEV_DISCONNECTED, &ir->dev->dev_state)) {
  205. /* Disable IR */
  206. au8522_rc_clear(ir, 0xe0, 1 << 4);
  207. }
  208. }
  209. static int au0828_probe_i2c_ir(struct au0828_dev *dev)
  210. {
  211. int i = 0;
  212. const unsigned short addr_list[] = {
  213. 0x47, I2C_CLIENT_END
  214. };
  215. while (addr_list[i] != I2C_CLIENT_END) {
  216. if (i2c_probe_func_quick_read(dev->i2c_client.adapter,
  217. addr_list[i]) == 1)
  218. return addr_list[i];
  219. i++;
  220. }
  221. return -ENODEV;
  222. }
  223. int au0828_rc_register(struct au0828_dev *dev)
  224. {
  225. struct au0828_rc *ir;
  226. struct rc_dev *rc;
  227. int err = -ENOMEM;
  228. u16 i2c_rc_dev_addr = 0;
  229. if (!dev->board.has_ir_i2c || disable_ir)
  230. return 0;
  231. i2c_rc_dev_addr = au0828_probe_i2c_ir(dev);
  232. if (!i2c_rc_dev_addr)
  233. return -ENODEV;
  234. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  235. rc = rc_allocate_device();
  236. if (!ir || !rc)
  237. goto error;
  238. /* record handles to ourself */
  239. ir->dev = dev;
  240. dev->ir = ir;
  241. ir->rc = rc;
  242. rc->priv = ir;
  243. rc->open = au0828_rc_start;
  244. rc->close = au0828_rc_stop;
  245. if (dev->board.has_ir_i2c) { /* external i2c device */
  246. switch (dev->boardnr) {
  247. case AU0828_BOARD_HAUPPAUGE_HVR950Q:
  248. rc->map_name = RC_MAP_HAUPPAUGE;
  249. ir->get_key_i2c = au0828_get_key_au8522;
  250. break;
  251. default:
  252. err = -ENODEV;
  253. goto error;
  254. }
  255. ir->i2c_dev_addr = i2c_rc_dev_addr;
  256. }
  257. /* This is how often we ask the chip for IR information */
  258. ir->polling = 100; /* ms */
  259. /* init input device */
  260. snprintf(ir->name, sizeof(ir->name), "au0828 IR (%s)",
  261. dev->board.name);
  262. usb_make_path(dev->usbdev, ir->phys, sizeof(ir->phys));
  263. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  264. rc->input_name = ir->name;
  265. rc->input_phys = ir->phys;
  266. rc->input_id.bustype = BUS_USB;
  267. rc->input_id.version = 1;
  268. rc->input_id.vendor = le16_to_cpu(dev->usbdev->descriptor.idVendor);
  269. rc->input_id.product = le16_to_cpu(dev->usbdev->descriptor.idProduct);
  270. rc->dev.parent = &dev->usbdev->dev;
  271. rc->driver_name = "au0828-input";
  272. rc->driver_type = RC_DRIVER_IR_RAW;
  273. rc->allowed_protocols = RC_BIT_NEC | RC_BIT_RC5;
  274. /* all done */
  275. err = rc_register_device(rc);
  276. if (err)
  277. goto error;
  278. pr_info("Remote controller %s initalized\n", ir->name);
  279. return 0;
  280. error:
  281. dev->ir = NULL;
  282. rc_free_device(rc);
  283. kfree(ir);
  284. return err;
  285. }
  286. void au0828_rc_unregister(struct au0828_dev *dev)
  287. {
  288. struct au0828_rc *ir = dev->ir;
  289. /* skip detach on non attached boards */
  290. if (!ir)
  291. return;
  292. rc_unregister_device(ir->rc);
  293. /* done */
  294. kfree(ir);
  295. dev->ir = NULL;
  296. }
  297. int au0828_rc_suspend(struct au0828_dev *dev)
  298. {
  299. struct au0828_rc *ir = dev->ir;
  300. if (!ir)
  301. return 0;
  302. pr_info("Stopping RC\n");
  303. cancel_delayed_work_sync(&ir->work);
  304. /* Disable IR */
  305. au8522_rc_clear(ir, 0xe0, 1 << 4);
  306. return 0;
  307. }
  308. int au0828_rc_resume(struct au0828_dev *dev)
  309. {
  310. struct au0828_rc *ir = dev->ir;
  311. if (!ir)
  312. return 0;
  313. pr_info("Restarting RC\n");
  314. /* Enable IR */
  315. au8522_rc_set(ir, 0xe0, 1 << 4);
  316. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  317. return 0;
  318. }