au0828-input.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <mchehab@samsung.com>
  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 200000 /* ns */
  83. #define NEC_START_SPACE (4500000 / AU8522_UNIT)
  84. #define NEC_START_PULSE (562500 * 16)
  85. #define RC5_START_SPACE (4 * AU8522_UNIT)
  86. #define RC5_START_PULSE 888888
  87. static int au0828_get_key_au8522(struct au0828_rc *ir)
  88. {
  89. unsigned char buf[40];
  90. DEFINE_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. init_ir_raw_event(&rawir);
  133. rawir.pulse = true;
  134. if (width > NEC_START_SPACE - 2 &&
  135. width < NEC_START_SPACE + 2) {
  136. /* NEC protocol */
  137. rawir.duration = NEC_START_PULSE;
  138. dprintk(16, "Storing NEC start %s with duration %d",
  139. rawir.pulse ? "pulse" : "space",
  140. rawir.duration);
  141. } else {
  142. /* RC5 protocol */
  143. rawir.duration = RC5_START_PULSE;
  144. dprintk(16, "Storing RC5 start %s with duration %d",
  145. rawir.pulse ? "pulse" : "space",
  146. rawir.duration);
  147. }
  148. ir_raw_event_store(ir->rc, &rawir);
  149. }
  150. init_ir_raw_event(&rawir);
  151. rawir.pulse = prv_bit ? false : true;
  152. rawir.duration = AU8522_UNIT * width;
  153. dprintk(16, "Storing %s with duration %d",
  154. rawir.pulse ? "pulse" : "space",
  155. rawir.duration);
  156. ir_raw_event_store(ir->rc, &rawir);
  157. width = 1;
  158. prv_bit = bit;
  159. }
  160. }
  161. init_ir_raw_event(&rawir);
  162. rawir.pulse = prv_bit ? false : true;
  163. rawir.duration = AU8522_UNIT * width;
  164. dprintk(16, "Storing end %s with duration %d",
  165. rawir.pulse ? "pulse" : "space",
  166. rawir.duration);
  167. ir_raw_event_store(ir->rc, &rawir);
  168. ir_raw_event_handle(ir->rc);
  169. return 1;
  170. }
  171. /*
  172. * Generic IR code
  173. */
  174. static void au0828_rc_work(struct work_struct *work)
  175. {
  176. struct au0828_rc *ir = container_of(work, struct au0828_rc, work.work);
  177. int rc;
  178. rc = ir->get_key_i2c(ir);
  179. if (rc < 0)
  180. pr_info("Error while getting RC scancode\n");
  181. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  182. }
  183. static int au0828_rc_start(struct rc_dev *rc)
  184. {
  185. struct au0828_rc *ir = rc->priv;
  186. INIT_DELAYED_WORK(&ir->work, au0828_rc_work);
  187. /* Enable IR */
  188. au8522_rc_set(ir, 0xe0, 1 << 4);
  189. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  190. return 0;
  191. }
  192. static void au0828_rc_stop(struct rc_dev *rc)
  193. {
  194. struct au0828_rc *ir = rc->priv;
  195. cancel_delayed_work_sync(&ir->work);
  196. /* do nothing if device is disconnected */
  197. if (!test_bit(DEV_DISCONNECTED, &ir->dev->dev_state)) {
  198. /* Disable IR */
  199. au8522_rc_clear(ir, 0xe0, 1 << 4);
  200. }
  201. }
  202. static int au0828_probe_i2c_ir(struct au0828_dev *dev)
  203. {
  204. int i = 0;
  205. static const unsigned short addr_list[] = {
  206. 0x47, I2C_CLIENT_END
  207. };
  208. while (addr_list[i] != I2C_CLIENT_END) {
  209. if (i2c_probe_func_quick_read(dev->i2c_client.adapter,
  210. addr_list[i]) == 1)
  211. return addr_list[i];
  212. i++;
  213. }
  214. return -ENODEV;
  215. }
  216. int au0828_rc_register(struct au0828_dev *dev)
  217. {
  218. struct au0828_rc *ir;
  219. struct rc_dev *rc;
  220. int err = -ENOMEM;
  221. u16 i2c_rc_dev_addr = 0;
  222. if (!dev->board.has_ir_i2c || disable_ir)
  223. return 0;
  224. i2c_rc_dev_addr = au0828_probe_i2c_ir(dev);
  225. if (!i2c_rc_dev_addr)
  226. return -ENODEV;
  227. ir = kzalloc(sizeof(*ir), GFP_KERNEL);
  228. rc = rc_allocate_device(RC_DRIVER_IR_RAW);
  229. if (!ir || !rc)
  230. goto error;
  231. /* record handles to ourself */
  232. ir->dev = dev;
  233. dev->ir = ir;
  234. ir->rc = rc;
  235. rc->priv = ir;
  236. rc->open = au0828_rc_start;
  237. rc->close = au0828_rc_stop;
  238. if (dev->board.has_ir_i2c) { /* external i2c device */
  239. switch (dev->boardnr) {
  240. case AU0828_BOARD_HAUPPAUGE_HVR950Q:
  241. rc->map_name = RC_MAP_HAUPPAUGE;
  242. ir->get_key_i2c = au0828_get_key_au8522;
  243. break;
  244. default:
  245. err = -ENODEV;
  246. goto error;
  247. }
  248. ir->i2c_dev_addr = i2c_rc_dev_addr;
  249. }
  250. /* This is how often we ask the chip for IR information */
  251. ir->polling = 100; /* ms */
  252. /* init input device */
  253. snprintf(ir->name, sizeof(ir->name), "au0828 IR (%s)",
  254. dev->board.name);
  255. usb_make_path(dev->usbdev, ir->phys, sizeof(ir->phys));
  256. strlcat(ir->phys, "/input0", sizeof(ir->phys));
  257. rc->device_name = ir->name;
  258. rc->input_phys = ir->phys;
  259. rc->input_id.bustype = BUS_USB;
  260. rc->input_id.version = 1;
  261. rc->input_id.vendor = le16_to_cpu(dev->usbdev->descriptor.idVendor);
  262. rc->input_id.product = le16_to_cpu(dev->usbdev->descriptor.idProduct);
  263. rc->dev.parent = &dev->usbdev->dev;
  264. rc->driver_name = "au0828-input";
  265. rc->allowed_protocols = RC_PROTO_BIT_NEC | RC_PROTO_BIT_NECX |
  266. RC_PROTO_BIT_NEC32 | RC_PROTO_BIT_RC5;
  267. /* all done */
  268. err = rc_register_device(rc);
  269. if (err)
  270. goto error;
  271. pr_info("Remote controller %s initialized\n", ir->name);
  272. return 0;
  273. error:
  274. dev->ir = NULL;
  275. rc_free_device(rc);
  276. kfree(ir);
  277. return err;
  278. }
  279. void au0828_rc_unregister(struct au0828_dev *dev)
  280. {
  281. struct au0828_rc *ir = dev->ir;
  282. /* skip detach on non attached boards */
  283. if (!ir)
  284. return;
  285. rc_unregister_device(ir->rc);
  286. /* done */
  287. kfree(ir);
  288. dev->ir = NULL;
  289. }
  290. int au0828_rc_suspend(struct au0828_dev *dev)
  291. {
  292. struct au0828_rc *ir = dev->ir;
  293. if (!ir)
  294. return 0;
  295. pr_info("Stopping RC\n");
  296. cancel_delayed_work_sync(&ir->work);
  297. /* Disable IR */
  298. au8522_rc_clear(ir, 0xe0, 1 << 4);
  299. return 0;
  300. }
  301. int au0828_rc_resume(struct au0828_dev *dev)
  302. {
  303. struct au0828_rc *ir = dev->ir;
  304. if (!ir)
  305. return 0;
  306. pr_info("Restarting RC\n");
  307. /* Enable IR */
  308. au8522_rc_set(ir, 0xe0, 1 << 4);
  309. schedule_delayed_work(&ir->work, msecs_to_jiffies(ir->polling));
  310. return 0;
  311. }