950-0045-lirc-added-support-for-RaspberryPi-GPIO.patch 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. From 277c109833c1d78543bec0723ab42f4c79937df9 Mon Sep 17 00:00:00 2001
  2. From: Aron Szabo <[email protected]>
  3. Date: Sat, 16 Jun 2012 12:15:55 +0200
  4. Subject: [PATCH] lirc: added support for RaspberryPi GPIO
  5. lirc_rpi: Use read_current_timer to determine transmitter delay. Thanks to jjmz and others
  6. See: https://github.com/raspberrypi/linux/issues/525
  7. lirc: Remove restriction on gpio pins that can be used with lirc
  8. Compute Module, for example could use different pins
  9. lirc_rpi: Add parameter to specify input pin pull
  10. Depending on the connected IR circuitry it might be desirable to change the
  11. gpios internal pull from it pull-down default behaviour. Add a module
  12. parameter to allow the user to set it explicitly.
  13. Signed-off-by: Julian Scheel <[email protected]>
  14. lirc-rpi: Use the higher-level irq control functions
  15. This module used to access the irq_chip methods of the
  16. gpio controller directly, rather than going through the
  17. standard enable_irq/irq_set_irq_type functions. This
  18. caused problems on pinctrl-bcm2835 which only implements
  19. the irq_enable/disable methods and not irq_unmask/mask.
  20. lirc-rpi: Correct the interrupt usage
  21. 1) Correct the use of enable_irq (i.e. don't call it so often)
  22. 2) Correct the shutdown sequence.
  23. 3) Avoid a bcm2708_gpio driver quirk by setting the irq flags earlier
  24. lirc-rpi: use getnstimeofday instead of read_current_timer
  25. read_current_timer isn't guaranteed to return values in
  26. microseconds, and indeed it doesn't on a Pi2.
  27. Issue: linux#827
  28. lirc-rpi: Add device tree support, and a suitable overlay
  29. The overlay supports DT parameters that match the old module
  30. parameters, except that gpio_in_pull should be set using the
  31. strings "up", "down" or "off".
  32. lirc-rpi: Also support pinctrl-bcm2835 in non-DT mode
  33. fix auto-sense in lirc_rpi driver
  34. On a Raspberry Pi 2, the lirc_rpi driver might receive spurious
  35. interrupts and change it's low-active / high-active setting.
  36. When this happens, the IR remote control stops working.
  37. This patch disables this auto-detection if the 'sense' parameter
  38. was set in the device tree, making the driver robust to such
  39. spurious interrupts.
  40. ---
  41. drivers/staging/media/lirc/Kconfig | 6 +
  42. drivers/staging/media/lirc/Makefile | 1 +
  43. drivers/staging/media/lirc/lirc_rpi.c | 734 ++++++++++++++++++++++++++++++++++
  44. include/linux/platform_data/bcm2708.h | 23 ++
  45. 4 files changed, 764 insertions(+)
  46. create mode 100644 drivers/staging/media/lirc/lirc_rpi.c
  47. create mode 100644 include/linux/platform_data/bcm2708.h
  48. --- a/drivers/staging/media/lirc/Kconfig
  49. +++ b/drivers/staging/media/lirc/Kconfig
  50. @@ -32,6 +32,12 @@ config LIRC_PARALLEL
  51. help
  52. Driver for Homebrew Parallel Port Receivers
  53. +config LIRC_RPI
  54. + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
  55. + depends on LIRC
  56. + help
  57. + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
  58. +
  59. config LIRC_SASEM
  60. tristate "Sasem USB IR Remote"
  61. depends on LIRC && USB
  62. --- a/drivers/staging/media/lirc/Makefile
  63. +++ b/drivers/staging/media/lirc/Makefile
  64. @@ -6,6 +6,7 @@
  65. obj-$(CONFIG_LIRC_BT829) += lirc_bt829.o
  66. obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
  67. obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
  68. +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
  69. obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
  70. obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
  71. obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
  72. --- /dev/null
  73. +++ b/drivers/staging/media/lirc/lirc_rpi.c
  74. @@ -0,0 +1,734 @@
  75. +/*
  76. + * lirc_rpi.c
  77. + *
  78. + * lirc_rpi - Device driver that records pulse- and pause-lengths
  79. + * (space-lengths) (just like the lirc_serial driver does)
  80. + * between GPIO interrupt events on the Raspberry Pi.
  81. + * Lots of code has been taken from the lirc_serial module,
  82. + * so I would like say thanks to the authors.
  83. + *
  84. + * Copyright (C) 2012 Aron Robert Szabo <[email protected]>,
  85. + * Michael Bishop <[email protected]>
  86. + * This program is free software; you can redistribute it and/or modify
  87. + * it under the terms of the GNU General Public License as published by
  88. + * the Free Software Foundation; either version 2 of the License, or
  89. + * (at your option) any later version.
  90. + *
  91. + * This program is distributed in the hope that it will be useful,
  92. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  93. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  94. + * GNU General Public License for more details.
  95. + *
  96. + * You should have received a copy of the GNU General Public License
  97. + * along with this program; if not, write to the Free Software
  98. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  99. + */
  100. +
  101. +#include <linux/module.h>
  102. +#include <linux/errno.h>
  103. +#include <linux/interrupt.h>
  104. +#include <linux/sched.h>
  105. +#include <linux/kernel.h>
  106. +#include <linux/time.h>
  107. +#include <linux/timex.h>
  108. +#include <linux/timekeeping.h>
  109. +#include <linux/string.h>
  110. +#include <linux/delay.h>
  111. +#include <linux/platform_device.h>
  112. +#include <linux/irq.h>
  113. +#include <linux/spinlock.h>
  114. +#include <media/lirc.h>
  115. +#include <media/lirc_dev.h>
  116. +#include <linux/gpio.h>
  117. +#include <linux/of_platform.h>
  118. +#include <linux/platform_data/bcm2708.h>
  119. +
  120. +#define LIRC_DRIVER_NAME "lirc_rpi"
  121. +#define RBUF_LEN 256
  122. +#define LIRC_TRANSMITTER_LATENCY 50
  123. +
  124. +#ifndef MAX_UDELAY_MS
  125. +#define MAX_UDELAY_US 5000
  126. +#else
  127. +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
  128. +#endif
  129. +
  130. +#define dprintk(fmt, args...) \
  131. + do { \
  132. + if (debug) \
  133. + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
  134. + fmt, ## args); \
  135. + } while (0)
  136. +
  137. +/* module parameters */
  138. +
  139. +/* set the default GPIO input pin */
  140. +static int gpio_in_pin = 18;
  141. +/* set the default pull behaviour for input pin */
  142. +static int gpio_in_pull = BCM2708_PULL_DOWN;
  143. +/* set the default GPIO output pin */
  144. +static int gpio_out_pin = 17;
  145. +/* enable debugging messages */
  146. +static bool debug;
  147. +/* -1 = auto, 0 = active high, 1 = active low */
  148. +static int sense = -1;
  149. +/* use softcarrier by default */
  150. +static bool softcarrier = 1;
  151. +/* 0 = do not invert output, 1 = invert output */
  152. +static bool invert = 0;
  153. +
  154. +struct gpio_chip *gpiochip;
  155. +static int irq_num;
  156. +static int auto_sense = 1;
  157. +
  158. +/* forward declarations */
  159. +static long send_pulse(unsigned long length);
  160. +static void send_space(long length);
  161. +static void lirc_rpi_exit(void);
  162. +
  163. +static struct platform_device *lirc_rpi_dev;
  164. +static struct timeval lasttv = { 0, 0 };
  165. +static struct lirc_buffer rbuf;
  166. +static spinlock_t lock;
  167. +
  168. +/* initialized/set in init_timing_params() */
  169. +static unsigned int freq = 38000;
  170. +static unsigned int duty_cycle = 50;
  171. +static unsigned long period;
  172. +static unsigned long pulse_width;
  173. +static unsigned long space_width;
  174. +
  175. +static void safe_udelay(unsigned long usecs)
  176. +{
  177. + while (usecs > MAX_UDELAY_US) {
  178. + udelay(MAX_UDELAY_US);
  179. + usecs -= MAX_UDELAY_US;
  180. + }
  181. + udelay(usecs);
  182. +}
  183. +
  184. +static unsigned long read_current_us(void)
  185. +{
  186. + struct timespec now;
  187. + getnstimeofday(&now);
  188. + return (now.tv_sec * 1000000) + (now.tv_nsec/1000);
  189. +}
  190. +
  191. +static int init_timing_params(unsigned int new_duty_cycle,
  192. + unsigned int new_freq)
  193. +{
  194. + if (1000 * 1000000L / new_freq * new_duty_cycle / 100 <=
  195. + LIRC_TRANSMITTER_LATENCY)
  196. + return -EINVAL;
  197. + if (1000 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
  198. + LIRC_TRANSMITTER_LATENCY)
  199. + return -EINVAL;
  200. + duty_cycle = new_duty_cycle;
  201. + freq = new_freq;
  202. + period = 1000 * 1000000L / freq;
  203. + pulse_width = period * duty_cycle / 100;
  204. + space_width = period - pulse_width;
  205. + dprintk("in init_timing_params, freq=%d pulse=%ld, "
  206. + "space=%ld\n", freq, pulse_width, space_width);
  207. + return 0;
  208. +}
  209. +
  210. +static long send_pulse_softcarrier(unsigned long length)
  211. +{
  212. + int flag;
  213. + unsigned long actual, target;
  214. + unsigned long actual_us, initial_us, target_us;
  215. +
  216. + length *= 1000;
  217. +
  218. + actual = 0; target = 0; flag = 0;
  219. + actual_us = read_current_us();
  220. +
  221. + while (actual < length) {
  222. + if (flag) {
  223. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  224. + target += space_width;
  225. + } else {
  226. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  227. + target += pulse_width;
  228. + }
  229. + initial_us = actual_us;
  230. + target_us = actual_us + (target - actual) / 1000;
  231. + /*
  232. + * Note - we've checked in ioctl that the pulse/space
  233. + * widths are big enough so that d is > 0
  234. + */
  235. + if ((int)(target_us - actual_us) > 0)
  236. + udelay(target_us - actual_us);
  237. + actual_us = read_current_us();
  238. + actual += (actual_us - initial_us) * 1000;
  239. + flag = !flag;
  240. + }
  241. + return (actual-length) / 1000;
  242. +}
  243. +
  244. +static long send_pulse(unsigned long length)
  245. +{
  246. + if (length <= 0)
  247. + return 0;
  248. +
  249. + if (softcarrier) {
  250. + return send_pulse_softcarrier(length);
  251. + } else {
  252. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  253. + safe_udelay(length);
  254. + return 0;
  255. + }
  256. +}
  257. +
  258. +static void send_space(long length)
  259. +{
  260. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  261. + if (length <= 0)
  262. + return;
  263. + safe_udelay(length);
  264. +}
  265. +
  266. +static void rbwrite(int l)
  267. +{
  268. + if (lirc_buffer_full(&rbuf)) {
  269. + /* no new signals will be accepted */
  270. + dprintk("Buffer overrun\n");
  271. + return;
  272. + }
  273. + lirc_buffer_write(&rbuf, (void *)&l);
  274. +}
  275. +
  276. +static void frbwrite(int l)
  277. +{
  278. + /* simple noise filter */
  279. + static int pulse, space;
  280. + static unsigned int ptr;
  281. +
  282. + if (ptr > 0 && (l & PULSE_BIT)) {
  283. + pulse += l & PULSE_MASK;
  284. + if (pulse > 250) {
  285. + rbwrite(space);
  286. + rbwrite(pulse | PULSE_BIT);
  287. + ptr = 0;
  288. + pulse = 0;
  289. + }
  290. + return;
  291. + }
  292. + if (!(l & PULSE_BIT)) {
  293. + if (ptr == 0) {
  294. + if (l > 20000) {
  295. + space = l;
  296. + ptr++;
  297. + return;
  298. + }
  299. + } else {
  300. + if (l > 20000) {
  301. + space += pulse;
  302. + if (space > PULSE_MASK)
  303. + space = PULSE_MASK;
  304. + space += l;
  305. + if (space > PULSE_MASK)
  306. + space = PULSE_MASK;
  307. + pulse = 0;
  308. + return;
  309. + }
  310. + rbwrite(space);
  311. + rbwrite(pulse | PULSE_BIT);
  312. + ptr = 0;
  313. + pulse = 0;
  314. + }
  315. + }
  316. + rbwrite(l);
  317. +}
  318. +
  319. +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
  320. +{
  321. + struct timeval tv;
  322. + long deltv;
  323. + int data;
  324. + int signal;
  325. +
  326. + /* use the GPIO signal level */
  327. + signal = gpiochip->get(gpiochip, gpio_in_pin);
  328. +
  329. + if (sense != -1) {
  330. + /* get current time */
  331. + do_gettimeofday(&tv);
  332. +
  333. + /* calc time since last interrupt in microseconds */
  334. + deltv = tv.tv_sec-lasttv.tv_sec;
  335. + if (tv.tv_sec < lasttv.tv_sec ||
  336. + (tv.tv_sec == lasttv.tv_sec &&
  337. + tv.tv_usec < lasttv.tv_usec)) {
  338. + printk(KERN_WARNING LIRC_DRIVER_NAME
  339. + ": AIEEEE: your clock just jumped backwards\n");
  340. + printk(KERN_WARNING LIRC_DRIVER_NAME
  341. + ": %d %d %lx %lx %lx %lx\n", signal, sense,
  342. + tv.tv_sec, lasttv.tv_sec,
  343. + tv.tv_usec, lasttv.tv_usec);
  344. + data = PULSE_MASK;
  345. + } else if (deltv > 15) {
  346. + data = PULSE_MASK; /* really long time */
  347. + if (!(signal^sense)) {
  348. + /* sanity check */
  349. + printk(KERN_DEBUG LIRC_DRIVER_NAME
  350. + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
  351. + signal, sense, tv.tv_sec, lasttv.tv_sec,
  352. + tv.tv_usec, lasttv.tv_usec);
  353. + /*
  354. + * detecting pulse while this
  355. + * MUST be a space!
  356. + */
  357. + if (auto_sense) {
  358. + sense = sense ? 0 : 1;
  359. + }
  360. + }
  361. + } else {
  362. + data = (int) (deltv*1000000 +
  363. + (tv.tv_usec - lasttv.tv_usec));
  364. + }
  365. + frbwrite(signal^sense ? data : (data|PULSE_BIT));
  366. + lasttv = tv;
  367. + wake_up_interruptible(&rbuf.wait_poll);
  368. + }
  369. +
  370. + return IRQ_HANDLED;
  371. +}
  372. +
  373. +static int is_right_chip(struct gpio_chip *chip, void *data)
  374. +{
  375. + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
  376. +
  377. + if (strcmp(data, chip->label) == 0)
  378. + return 1;
  379. + return 0;
  380. +}
  381. +
  382. +static inline int read_bool_property(const struct device_node *np,
  383. + const char *propname,
  384. + bool *out_value)
  385. +{
  386. + u32 value = 0;
  387. + int err = of_property_read_u32(np, propname, &value);
  388. + if (err == 0)
  389. + *out_value = (value != 0);
  390. + return err;
  391. +}
  392. +
  393. +static void read_pin_settings(struct device_node *node)
  394. +{
  395. + u32 pin;
  396. + int index;
  397. +
  398. + for (index = 0;
  399. + of_property_read_u32_index(
  400. + node,
  401. + "brcm,pins",
  402. + index,
  403. + &pin) == 0;
  404. + index++) {
  405. + u32 function;
  406. + int err;
  407. + err = of_property_read_u32_index(
  408. + node,
  409. + "brcm,function",
  410. + index,
  411. + &function);
  412. + if (err == 0) {
  413. + if (function == 1) /* Output */
  414. + gpio_out_pin = pin;
  415. + else if (function == 0) /* Input */
  416. + gpio_in_pin = pin;
  417. + }
  418. + }
  419. +}
  420. +
  421. +static int init_port(void)
  422. +{
  423. + int i, nlow, nhigh;
  424. + struct device_node *node;
  425. +
  426. + node = lirc_rpi_dev->dev.of_node;
  427. +
  428. + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
  429. +
  430. + /*
  431. + * Because of the lack of a setpull function, only support
  432. + * pinctrl-bcm2835 if using device tree.
  433. + */
  434. + if (!gpiochip && node)
  435. + gpiochip = gpiochip_find("pinctrl-bcm2835", is_right_chip);
  436. +
  437. + if (!gpiochip) {
  438. + pr_err(LIRC_DRIVER_NAME ": gpio chip not found!\n");
  439. + return -ENODEV;
  440. + }
  441. +
  442. + if (node) {
  443. + struct device_node *pins_node;
  444. +
  445. + pins_node = of_parse_phandle(node, "pinctrl-0", 0);
  446. + if (!pins_node) {
  447. + printk(KERN_ERR LIRC_DRIVER_NAME
  448. + ": pinctrl settings not found!\n");
  449. + return -EINVAL;
  450. + }
  451. +
  452. + read_pin_settings(pins_node);
  453. +
  454. + of_property_read_u32(node, "rpi,sense", &sense);
  455. +
  456. + read_bool_property(node, "rpi,softcarrier", &softcarrier);
  457. +
  458. + read_bool_property(node, "rpi,invert", &invert);
  459. +
  460. + read_bool_property(node, "rpi,debug", &debug);
  461. +
  462. + } else {
  463. + return -EINVAL;
  464. + }
  465. +
  466. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  467. +
  468. + irq_num = gpiochip->to_irq(gpiochip, gpio_in_pin);
  469. + dprintk("to_irq %d\n", irq_num);
  470. +
  471. + /* if pin is high, then this must be an active low receiver. */
  472. + if (sense == -1) {
  473. + /* wait 1/2 sec for the power supply */
  474. + msleep(500);
  475. +
  476. + /*
  477. + * probe 9 times every 0.04s, collect "votes" for
  478. + * active high/low
  479. + */
  480. + nlow = 0;
  481. + nhigh = 0;
  482. + for (i = 0; i < 9; i++) {
  483. + if (gpiochip->get(gpiochip, gpio_in_pin))
  484. + nlow++;
  485. + else
  486. + nhigh++;
  487. + msleep(40);
  488. + }
  489. + sense = (nlow >= nhigh ? 1 : 0);
  490. + printk(KERN_INFO LIRC_DRIVER_NAME
  491. + ": auto-detected active %s receiver on GPIO pin %d\n",
  492. + sense ? "low" : "high", gpio_in_pin);
  493. + } else {
  494. + printk(KERN_INFO LIRC_DRIVER_NAME
  495. + ": manually using active %s receiver on GPIO pin %d\n",
  496. + sense ? "low" : "high", gpio_in_pin);
  497. + auto_sense = 0;
  498. + }
  499. +
  500. + return 0;
  501. +}
  502. +
  503. +// called when the character device is opened
  504. +static int set_use_inc(void *data)
  505. +{
  506. + int result;
  507. +
  508. + /* initialize timestamp */
  509. + do_gettimeofday(&lasttv);
  510. +
  511. + result = request_irq(irq_num,
  512. + (irq_handler_t) irq_handler,
  513. + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING,
  514. + LIRC_DRIVER_NAME, (void*) 0);
  515. +
  516. + switch (result) {
  517. + case -EBUSY:
  518. + printk(KERN_ERR LIRC_DRIVER_NAME
  519. + ": IRQ %d is busy\n",
  520. + irq_num);
  521. + return -EBUSY;
  522. + case -EINVAL:
  523. + printk(KERN_ERR LIRC_DRIVER_NAME
  524. + ": Bad irq number or handler\n");
  525. + return -EINVAL;
  526. + default:
  527. + dprintk("Interrupt %d obtained\n",
  528. + irq_num);
  529. + break;
  530. + };
  531. +
  532. + /* initialize pulse/space widths */
  533. + init_timing_params(duty_cycle, freq);
  534. +
  535. + return 0;
  536. +}
  537. +
  538. +static void set_use_dec(void *data)
  539. +{
  540. + /* GPIO Pin Falling/Rising Edge Detect Disable */
  541. + irq_set_irq_type(irq_num, 0);
  542. + disable_irq(irq_num);
  543. +
  544. + free_irq(irq_num, (void *) 0);
  545. +
  546. + dprintk(KERN_INFO LIRC_DRIVER_NAME
  547. + ": freed IRQ %d\n", irq_num);
  548. +}
  549. +
  550. +static ssize_t lirc_write(struct file *file, const char *buf,
  551. + size_t n, loff_t *ppos)
  552. +{
  553. + int i, count;
  554. + unsigned long flags;
  555. + long delta = 0;
  556. + int *wbuf;
  557. +
  558. + count = n / sizeof(int);
  559. + if (n % sizeof(int) || count % 2 == 0)
  560. + return -EINVAL;
  561. + wbuf = memdup_user(buf, n);
  562. + if (IS_ERR(wbuf))
  563. + return PTR_ERR(wbuf);
  564. + spin_lock_irqsave(&lock, flags);
  565. +
  566. + for (i = 0; i < count; i++) {
  567. + if (i%2)
  568. + send_space(wbuf[i] - delta);
  569. + else
  570. + delta = send_pulse(wbuf[i]);
  571. + }
  572. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  573. +
  574. + spin_unlock_irqrestore(&lock, flags);
  575. + kfree(wbuf);
  576. + return n;
  577. +}
  578. +
  579. +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  580. +{
  581. + int result;
  582. + __u32 value;
  583. +
  584. + switch (cmd) {
  585. + case LIRC_GET_SEND_MODE:
  586. + return -ENOIOCTLCMD;
  587. + break;
  588. +
  589. + case LIRC_SET_SEND_MODE:
  590. + result = get_user(value, (__u32 *) arg);
  591. + if (result)
  592. + return result;
  593. + /* only LIRC_MODE_PULSE supported */
  594. + if (value != LIRC_MODE_PULSE)
  595. + return -ENOSYS;
  596. + break;
  597. +
  598. + case LIRC_GET_LENGTH:
  599. + return -ENOSYS;
  600. + break;
  601. +
  602. + case LIRC_SET_SEND_DUTY_CYCLE:
  603. + dprintk("SET_SEND_DUTY_CYCLE\n");
  604. + result = get_user(value, (__u32 *) arg);
  605. + if (result)
  606. + return result;
  607. + if (value <= 0 || value > 100)
  608. + return -EINVAL;
  609. + return init_timing_params(value, freq);
  610. + break;
  611. +
  612. + case LIRC_SET_SEND_CARRIER:
  613. + dprintk("SET_SEND_CARRIER\n");
  614. + result = get_user(value, (__u32 *) arg);
  615. + if (result)
  616. + return result;
  617. + if (value > 500000 || value < 20000)
  618. + return -EINVAL;
  619. + return init_timing_params(duty_cycle, value);
  620. + break;
  621. +
  622. + default:
  623. + return lirc_dev_fop_ioctl(filep, cmd, arg);
  624. + }
  625. + return 0;
  626. +}
  627. +
  628. +static const struct file_operations lirc_fops = {
  629. + .owner = THIS_MODULE,
  630. + .write = lirc_write,
  631. + .unlocked_ioctl = lirc_ioctl,
  632. + .read = lirc_dev_fop_read,
  633. + .poll = lirc_dev_fop_poll,
  634. + .open = lirc_dev_fop_open,
  635. + .release = lirc_dev_fop_close,
  636. + .llseek = no_llseek,
  637. +};
  638. +
  639. +static struct lirc_driver driver = {
  640. + .name = LIRC_DRIVER_NAME,
  641. + .minor = -1,
  642. + .code_length = 1,
  643. + .sample_rate = 0,
  644. + .data = NULL,
  645. + .add_to_buf = NULL,
  646. + .rbuf = &rbuf,
  647. + .set_use_inc = set_use_inc,
  648. + .set_use_dec = set_use_dec,
  649. + .fops = &lirc_fops,
  650. + .dev = NULL,
  651. + .owner = THIS_MODULE,
  652. +};
  653. +
  654. +static const struct of_device_id lirc_rpi_of_match[] = {
  655. + { .compatible = "rpi,lirc-rpi", },
  656. + {},
  657. +};
  658. +MODULE_DEVICE_TABLE(of, lirc_rpi_of_match);
  659. +
  660. +static struct platform_driver lirc_rpi_driver = {
  661. + .driver = {
  662. + .name = LIRC_DRIVER_NAME,
  663. + .owner = THIS_MODULE,
  664. + .of_match_table = of_match_ptr(lirc_rpi_of_match),
  665. + },
  666. +};
  667. +
  668. +static int __init lirc_rpi_init(void)
  669. +{
  670. + struct device_node *node;
  671. + int result;
  672. +
  673. + /* Init read buffer. */
  674. + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
  675. + if (result < 0)
  676. + return -ENOMEM;
  677. +
  678. + result = platform_driver_register(&lirc_rpi_driver);
  679. + if (result) {
  680. + printk(KERN_ERR LIRC_DRIVER_NAME
  681. + ": lirc register returned %d\n", result);
  682. + goto exit_buffer_free;
  683. + }
  684. +
  685. + node = of_find_compatible_node(NULL, NULL,
  686. + lirc_rpi_of_match[0].compatible);
  687. +
  688. + if (node) {
  689. + /* DT-enabled */
  690. + lirc_rpi_dev = of_find_device_by_node(node);
  691. + WARN_ON(lirc_rpi_dev->dev.of_node != node);
  692. + of_node_put(node);
  693. + }
  694. + else {
  695. + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
  696. + if (!lirc_rpi_dev) {
  697. + result = -ENOMEM;
  698. + goto exit_driver_unregister;
  699. + }
  700. +
  701. + result = platform_device_add(lirc_rpi_dev);
  702. + if (result)
  703. + goto exit_device_put;
  704. + }
  705. +
  706. + return 0;
  707. +
  708. + exit_device_put:
  709. + platform_device_put(lirc_rpi_dev);
  710. +
  711. + exit_driver_unregister:
  712. + platform_driver_unregister(&lirc_rpi_driver);
  713. +
  714. + exit_buffer_free:
  715. + lirc_buffer_free(&rbuf);
  716. +
  717. + return result;
  718. +}
  719. +
  720. +static void lirc_rpi_exit(void)
  721. +{
  722. + if (!lirc_rpi_dev->dev.of_node)
  723. + platform_device_unregister(lirc_rpi_dev);
  724. + platform_driver_unregister(&lirc_rpi_driver);
  725. + lirc_buffer_free(&rbuf);
  726. +}
  727. +
  728. +static int __init lirc_rpi_init_module(void)
  729. +{
  730. + int result;
  731. +
  732. + result = lirc_rpi_init();
  733. + if (result)
  734. + return result;
  735. +
  736. + result = init_port();
  737. + if (result < 0)
  738. + goto exit_rpi;
  739. +
  740. + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
  741. + LIRC_CAN_SET_SEND_CARRIER |
  742. + LIRC_CAN_SEND_PULSE |
  743. + LIRC_CAN_REC_MODE2;
  744. +
  745. + driver.dev = &lirc_rpi_dev->dev;
  746. + driver.minor = lirc_register_driver(&driver);
  747. +
  748. + if (driver.minor < 0) {
  749. + printk(KERN_ERR LIRC_DRIVER_NAME
  750. + ": device registration failed with %d\n", result);
  751. + result = -EIO;
  752. + goto exit_rpi;
  753. + }
  754. +
  755. + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
  756. +
  757. + return 0;
  758. +
  759. + exit_rpi:
  760. + lirc_rpi_exit();
  761. +
  762. + return result;
  763. +}
  764. +
  765. +static void __exit lirc_rpi_exit_module(void)
  766. +{
  767. + lirc_unregister_driver(driver.minor);
  768. +
  769. + gpio_free(gpio_out_pin);
  770. + gpio_free(gpio_in_pin);
  771. +
  772. + lirc_rpi_exit();
  773. +
  774. + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
  775. +}
  776. +
  777. +module_init(lirc_rpi_init_module);
  778. +module_exit(lirc_rpi_exit_module);
  779. +
  780. +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
  781. +MODULE_AUTHOR("Aron Robert Szabo <[email protected]>");
  782. +MODULE_AUTHOR("Michael Bishop <[email protected]>");
  783. +MODULE_LICENSE("GPL");
  784. +
  785. +module_param(gpio_out_pin, int, S_IRUGO);
  786. +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
  787. + " processor. (default 17");
  788. +
  789. +module_param(gpio_in_pin, int, S_IRUGO);
  790. +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
  791. + " (default 18");
  792. +
  793. +module_param(gpio_in_pull, int, S_IRUGO);
  794. +MODULE_PARM_DESC(gpio_in_pull, "GPIO input pin pull configuration."
  795. + " (0 = off, 1 = up, 2 = down, default down)");
  796. +
  797. +module_param(sense, int, S_IRUGO);
  798. +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
  799. + " (0 = active high, 1 = active low )");
  800. +
  801. +module_param(softcarrier, bool, S_IRUGO);
  802. +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
  803. +
  804. +module_param(invert, bool, S_IRUGO);
  805. +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
  806. +
  807. +module_param(debug, bool, S_IRUGO | S_IWUSR);
  808. +MODULE_PARM_DESC(debug, "Enable debugging messages");
  809. --- /dev/null
  810. +++ b/include/linux/platform_data/bcm2708.h
  811. @@ -0,0 +1,23 @@
  812. +/*
  813. + * include/linux/platform_data/bcm2708.h
  814. + *
  815. + * This program is free software; you can redistribute it and/or modify
  816. + * it under the terms of the GNU General Public License version 2 as
  817. + * published by the Free Software Foundation.
  818. + *
  819. + * (C) 2014 Julian Scheel <[email protected]>
  820. + *
  821. + */
  822. +#ifndef __BCM2708_H_
  823. +#define __BCM2708_H_
  824. +
  825. +typedef enum {
  826. + BCM2708_PULL_OFF,
  827. + BCM2708_PULL_UP,
  828. + BCM2708_PULL_DOWN
  829. +} bcm2708_gpio_pull_t;
  830. +
  831. +extern int bcm2708_gpio_setpull(struct gpio_chip *gc, unsigned offset,
  832. + bcm2708_gpio_pull_t value);
  833. +
  834. +#endif