au0828-input.c 8.4 KB

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