gpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /* $Id: gpio.c,v 1.16 2005/06/19 17:06:49 starvik Exp $
  2. *
  3. * ETRAX CRISv32 general port I/O device
  4. *
  5. * Copyright (c) 1999, 2000, 2001, 2002, 2003 Axis Communications AB
  6. *
  7. * Authors: Bjorn Wesen (initial version)
  8. * Ola Knutsson (LED handling)
  9. * Johan Adolfsson (read/set directions, write, port G,
  10. * port to ETRAX FS.
  11. *
  12. * $Log: gpio.c,v $
  13. * Revision 1.16 2005/06/19 17:06:49 starvik
  14. * Merge of Linux 2.6.12.
  15. *
  16. * Revision 1.15 2005/05/25 08:22:20 starvik
  17. * Changed GPIO port order to fit packages/devices/axis-2.4.
  18. *
  19. * Revision 1.14 2005/04/24 18:35:08 starvik
  20. * Updated with final register headers.
  21. *
  22. * Revision 1.13 2005/03/15 15:43:00 starvik
  23. * dev_id needs to be supplied for shared IRQs.
  24. *
  25. * Revision 1.12 2005/03/10 17:12:00 starvik
  26. * Protect alarm list with spinlock.
  27. *
  28. * Revision 1.11 2005/01/05 06:08:59 starvik
  29. * No need to do local_irq_disable after local_irq_save.
  30. *
  31. * Revision 1.10 2004/11/19 08:38:31 starvik
  32. * Removed old crap.
  33. *
  34. * Revision 1.9 2004/05/14 07:58:02 starvik
  35. * Merge of changes from 2.4
  36. *
  37. * Revision 1.8 2003/09/11 07:29:50 starvik
  38. * Merge of Linux 2.6.0-test5
  39. *
  40. * Revision 1.7 2003/07/10 13:25:46 starvik
  41. * Compiles for 2.5.74
  42. * Lindented ethernet.c
  43. *
  44. * Revision 1.6 2003/07/04 08:27:46 starvik
  45. * Merge of Linux 2.5.74
  46. *
  47. * Revision 1.5 2003/06/10 08:26:37 johana
  48. * Etrax -> ETRAX CRISv32
  49. *
  50. * Revision 1.4 2003/06/05 14:22:48 johana
  51. * Initialise some_alarms.
  52. *
  53. * Revision 1.3 2003/06/05 10:15:46 johana
  54. * New INTR_VECT macros.
  55. * Enable interrupts in global config.
  56. *
  57. * Revision 1.2 2003/06/03 15:52:50 johana
  58. * Initial CRIS v32 version.
  59. *
  60. * Revision 1.1 2003/06/03 08:53:15 johana
  61. * Copy of os/lx25/arch/cris/arch-v10/drivers/gpio.c version 1.7.
  62. *
  63. */
  64. #include <linux/config.h>
  65. #include <linux/module.h>
  66. #include <linux/sched.h>
  67. #include <linux/slab.h>
  68. #include <linux/ioport.h>
  69. #include <linux/errno.h>
  70. #include <linux/kernel.h>
  71. #include <linux/fs.h>
  72. #include <linux/string.h>
  73. #include <linux/poll.h>
  74. #include <linux/init.h>
  75. #include <linux/interrupt.h>
  76. #include <linux/spinlock.h>
  77. #include <asm/etraxgpio.h>
  78. #include <asm/arch/hwregs/reg_map.h>
  79. #include <asm/arch/hwregs/reg_rdwr.h>
  80. #include <asm/arch/hwregs/gio_defs.h>
  81. #include <asm/arch/hwregs/intr_vect_defs.h>
  82. #include <asm/io.h>
  83. #include <asm/system.h>
  84. #include <asm/irq.h>
  85. /* The following gio ports on ETRAX FS is available:
  86. * pa 8 bits, supports interrupts off, hi, low, set, posedge, negedge anyedge
  87. * pb 18 bits
  88. * pc 18 bits
  89. * pd 18 bits
  90. * pe 18 bits
  91. * each port has a rw_px_dout, r_px_din and rw_px_oe register.
  92. */
  93. #define GPIO_MAJOR 120 /* experimental MAJOR number */
  94. #define D(x)
  95. #if 0
  96. static int dp_cnt;
  97. #define DP(x) do { dp_cnt++; if (dp_cnt % 1000 == 0) x; }while(0)
  98. #else
  99. #define DP(x)
  100. #endif
  101. static char gpio_name[] = "etrax gpio";
  102. #if 0
  103. static wait_queue_head_t *gpio_wq;
  104. #endif
  105. static int gpio_ioctl(struct inode *inode, struct file *file,
  106. unsigned int cmd, unsigned long arg);
  107. static ssize_t gpio_write(struct file * file, const char * buf, size_t count,
  108. loff_t *off);
  109. static int gpio_open(struct inode *inode, struct file *filp);
  110. static int gpio_release(struct inode *inode, struct file *filp);
  111. static unsigned int gpio_poll(struct file *filp, struct poll_table_struct *wait);
  112. /* private data per open() of this driver */
  113. struct gpio_private {
  114. struct gpio_private *next;
  115. /* The IO_CFG_WRITE_MODE_VALUE only support 8 bits: */
  116. unsigned char clk_mask;
  117. unsigned char data_mask;
  118. unsigned char write_msb;
  119. unsigned char pad1;
  120. /* These fields are generic */
  121. unsigned long highalarm, lowalarm;
  122. wait_queue_head_t alarm_wq;
  123. int minor;
  124. };
  125. /* linked list of alarms to check for */
  126. static struct gpio_private *alarmlist = 0;
  127. static int gpio_some_alarms = 0; /* Set if someone uses alarm */
  128. static unsigned long gpio_pa_high_alarms = 0;
  129. static unsigned long gpio_pa_low_alarms = 0;
  130. static DEFINE_SPINLOCK(alarm_lock);
  131. #define NUM_PORTS (GPIO_MINOR_LAST+1)
  132. #define GIO_REG_RD_ADDR(reg) (volatile unsigned long*) (regi_gio + REG_RD_ADDR_gio_##reg )
  133. #define GIO_REG_WR_ADDR(reg) (volatile unsigned long*) (regi_gio + REG_RD_ADDR_gio_##reg )
  134. unsigned long led_dummy;
  135. static volatile unsigned long *data_out[NUM_PORTS] = {
  136. GIO_REG_WR_ADDR(rw_pa_dout),
  137. GIO_REG_WR_ADDR(rw_pb_dout),
  138. &led_dummy,
  139. GIO_REG_WR_ADDR(rw_pc_dout),
  140. GIO_REG_WR_ADDR(rw_pd_dout),
  141. GIO_REG_WR_ADDR(rw_pe_dout),
  142. };
  143. static volatile unsigned long *data_in[NUM_PORTS] = {
  144. GIO_REG_RD_ADDR(r_pa_din),
  145. GIO_REG_RD_ADDR(r_pb_din),
  146. &led_dummy,
  147. GIO_REG_RD_ADDR(r_pc_din),
  148. GIO_REG_RD_ADDR(r_pd_din),
  149. GIO_REG_RD_ADDR(r_pe_din),
  150. };
  151. static unsigned long changeable_dir[NUM_PORTS] = {
  152. CONFIG_ETRAX_PA_CHANGEABLE_DIR,
  153. CONFIG_ETRAX_PB_CHANGEABLE_DIR,
  154. 0,
  155. CONFIG_ETRAX_PC_CHANGEABLE_DIR,
  156. CONFIG_ETRAX_PD_CHANGEABLE_DIR,
  157. CONFIG_ETRAX_PE_CHANGEABLE_DIR,
  158. };
  159. static unsigned long changeable_bits[NUM_PORTS] = {
  160. CONFIG_ETRAX_PA_CHANGEABLE_BITS,
  161. CONFIG_ETRAX_PB_CHANGEABLE_BITS,
  162. 0,
  163. CONFIG_ETRAX_PC_CHANGEABLE_BITS,
  164. CONFIG_ETRAX_PD_CHANGEABLE_BITS,
  165. CONFIG_ETRAX_PE_CHANGEABLE_BITS,
  166. };
  167. static volatile unsigned long *dir_oe[NUM_PORTS] = {
  168. GIO_REG_WR_ADDR(rw_pa_oe),
  169. GIO_REG_WR_ADDR(rw_pb_oe),
  170. &led_dummy,
  171. GIO_REG_WR_ADDR(rw_pc_oe),
  172. GIO_REG_WR_ADDR(rw_pd_oe),
  173. GIO_REG_WR_ADDR(rw_pe_oe),
  174. };
  175. static unsigned int
  176. gpio_poll(struct file *file,
  177. poll_table *wait)
  178. {
  179. unsigned int mask = 0;
  180. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  181. unsigned long data;
  182. poll_wait(file, &priv->alarm_wq, wait);
  183. if (priv->minor == GPIO_MINOR_A) {
  184. reg_gio_rw_intr_cfg intr_cfg;
  185. unsigned long tmp;
  186. unsigned long flags;
  187. local_irq_save(flags);
  188. data = REG_TYPE_CONV(unsigned long, reg_gio_r_pa_din, REG_RD(gio, regi_gio, r_pa_din));
  189. /* PA has support for interrupt
  190. * lets activate high for those low and with highalarm set
  191. */
  192. intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
  193. tmp = ~data & priv->highalarm & 0xFF;
  194. if (tmp & (1 << 0)) {
  195. intr_cfg.pa0 = regk_gio_hi;
  196. }
  197. if (tmp & (1 << 1)) {
  198. intr_cfg.pa1 = regk_gio_hi;
  199. }
  200. if (tmp & (1 << 2)) {
  201. intr_cfg.pa2 = regk_gio_hi;
  202. }
  203. if (tmp & (1 << 3)) {
  204. intr_cfg.pa3 = regk_gio_hi;
  205. }
  206. if (tmp & (1 << 4)) {
  207. intr_cfg.pa4 = regk_gio_hi;
  208. }
  209. if (tmp & (1 << 5)) {
  210. intr_cfg.pa5 = regk_gio_hi;
  211. }
  212. if (tmp & (1 << 6)) {
  213. intr_cfg.pa6 = regk_gio_hi;
  214. }
  215. if (tmp & (1 << 7)) {
  216. intr_cfg.pa7 = regk_gio_hi;
  217. }
  218. /*
  219. * lets activate low for those high and with lowalarm set
  220. */
  221. tmp = data & priv->lowalarm & 0xFF;
  222. if (tmp & (1 << 0)) {
  223. intr_cfg.pa0 = regk_gio_lo;
  224. }
  225. if (tmp & (1 << 1)) {
  226. intr_cfg.pa1 = regk_gio_lo;
  227. }
  228. if (tmp & (1 << 2)) {
  229. intr_cfg.pa2 = regk_gio_lo;
  230. }
  231. if (tmp & (1 << 3)) {
  232. intr_cfg.pa3 = regk_gio_lo;
  233. }
  234. if (tmp & (1 << 4)) {
  235. intr_cfg.pa4 = regk_gio_lo;
  236. }
  237. if (tmp & (1 << 5)) {
  238. intr_cfg.pa5 = regk_gio_lo;
  239. }
  240. if (tmp & (1 << 6)) {
  241. intr_cfg.pa6 = regk_gio_lo;
  242. }
  243. if (tmp & (1 << 7)) {
  244. intr_cfg.pa7 = regk_gio_lo;
  245. }
  246. REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
  247. local_irq_restore(flags);
  248. } else if (priv->minor <= GPIO_MINOR_E)
  249. data = *data_in[priv->minor];
  250. else
  251. return 0;
  252. if ((data & priv->highalarm) ||
  253. (~data & priv->lowalarm)) {
  254. mask = POLLIN|POLLRDNORM;
  255. }
  256. DP(printk("gpio_poll ready: mask 0x%08X\n", mask));
  257. return mask;
  258. }
  259. int etrax_gpio_wake_up_check(void)
  260. {
  261. struct gpio_private *priv = alarmlist;
  262. unsigned long data = 0;
  263. int ret = 0;
  264. while (priv) {
  265. data = *data_in[priv->minor];
  266. if ((data & priv->highalarm) ||
  267. (~data & priv->lowalarm)) {
  268. DP(printk("etrax_gpio_wake_up_check %i\n",priv->minor));
  269. wake_up_interruptible(&priv->alarm_wq);
  270. ret = 1;
  271. }
  272. priv = priv->next;
  273. }
  274. return ret;
  275. }
  276. static irqreturn_t
  277. gpio_poll_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  278. {
  279. if (gpio_some_alarms) {
  280. return IRQ_RETVAL(etrax_gpio_wake_up_check());
  281. }
  282. return IRQ_NONE;
  283. }
  284. static irqreturn_t
  285. gpio_pa_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  286. {
  287. reg_gio_rw_intr_mask intr_mask;
  288. reg_gio_r_masked_intr masked_intr;
  289. reg_gio_rw_ack_intr ack_intr;
  290. unsigned long tmp;
  291. unsigned long tmp2;
  292. /* Find what PA interrupts are active */
  293. masked_intr = REG_RD(gio, regi_gio, r_masked_intr);
  294. tmp = REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr, masked_intr);
  295. /* Find those that we have enabled */
  296. spin_lock(&alarm_lock);
  297. tmp &= (gpio_pa_high_alarms | gpio_pa_low_alarms);
  298. spin_unlock(&alarm_lock);
  299. /* Ack them */
  300. ack_intr = REG_TYPE_CONV(reg_gio_rw_ack_intr, unsigned long, tmp);
  301. REG_WR(gio, regi_gio, rw_ack_intr, ack_intr);
  302. /* Disable those interrupts.. */
  303. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  304. tmp2 = REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask, intr_mask);
  305. tmp2 &= ~tmp;
  306. intr_mask = REG_TYPE_CONV(reg_gio_rw_intr_mask, unsigned long, tmp2);
  307. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  308. if (gpio_some_alarms) {
  309. return IRQ_RETVAL(etrax_gpio_wake_up_check());
  310. }
  311. return IRQ_NONE;
  312. }
  313. static ssize_t gpio_write(struct file * file, const char * buf, size_t count,
  314. loff_t *off)
  315. {
  316. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  317. unsigned char data, clk_mask, data_mask, write_msb;
  318. unsigned long flags;
  319. unsigned long shadow;
  320. volatile unsigned long *port;
  321. ssize_t retval = count;
  322. /* Only bits 0-7 may be used for write operations but allow all
  323. devices except leds... */
  324. if (priv->minor == GPIO_MINOR_LEDS) {
  325. return -EFAULT;
  326. }
  327. if (!access_ok(VERIFY_READ, buf, count)) {
  328. return -EFAULT;
  329. }
  330. clk_mask = priv->clk_mask;
  331. data_mask = priv->data_mask;
  332. /* It must have been configured using the IO_CFG_WRITE_MODE */
  333. /* Perhaps a better error code? */
  334. if (clk_mask == 0 || data_mask == 0) {
  335. return -EPERM;
  336. }
  337. write_msb = priv->write_msb;
  338. D(printk("gpio_write: %lu to data 0x%02X clk 0x%02X msb: %i\n",count, data_mask, clk_mask, write_msb));
  339. port = data_out[priv->minor];
  340. while (count--) {
  341. int i;
  342. data = *buf++;
  343. if (priv->write_msb) {
  344. for (i = 7; i >= 0;i--) {
  345. local_irq_save(flags);
  346. shadow = *port;
  347. *port = shadow &= ~clk_mask;
  348. if (data & 1<<i)
  349. *port = shadow |= data_mask;
  350. else
  351. *port = shadow &= ~data_mask;
  352. /* For FPGA: min 5.0ns (DCC) before CCLK high */
  353. *port = shadow |= clk_mask;
  354. local_irq_restore(flags);
  355. }
  356. } else {
  357. for (i = 0; i <= 7;i++) {
  358. local_irq_save(flags);
  359. shadow = *port;
  360. *port = shadow &= ~clk_mask;
  361. if (data & 1<<i)
  362. *port = shadow |= data_mask;
  363. else
  364. *port = shadow &= ~data_mask;
  365. /* For FPGA: min 5.0ns (DCC) before CCLK high */
  366. *port = shadow |= clk_mask;
  367. local_irq_restore(flags);
  368. }
  369. }
  370. }
  371. return retval;
  372. }
  373. static int
  374. gpio_open(struct inode *inode, struct file *filp)
  375. {
  376. struct gpio_private *priv;
  377. int p = MINOR(inode->i_rdev);
  378. if (p > GPIO_MINOR_LAST)
  379. return -EINVAL;
  380. priv = (struct gpio_private *)kmalloc(sizeof(struct gpio_private),
  381. GFP_KERNEL);
  382. if (!priv)
  383. return -ENOMEM;
  384. priv->minor = p;
  385. /* initialize the io/alarm struct and link it into our alarmlist */
  386. priv->next = alarmlist;
  387. alarmlist = priv;
  388. priv->clk_mask = 0;
  389. priv->data_mask = 0;
  390. priv->highalarm = 0;
  391. priv->lowalarm = 0;
  392. init_waitqueue_head(&priv->alarm_wq);
  393. filp->private_data = (void *)priv;
  394. return 0;
  395. }
  396. static int
  397. gpio_release(struct inode *inode, struct file *filp)
  398. {
  399. struct gpio_private *p = alarmlist;
  400. struct gpio_private *todel = (struct gpio_private *)filp->private_data;
  401. /* local copies while updating them: */
  402. unsigned long a_high, a_low;
  403. unsigned long some_alarms;
  404. /* unlink from alarmlist and free the private structure */
  405. if (p == todel) {
  406. alarmlist = todel->next;
  407. } else {
  408. while (p->next != todel)
  409. p = p->next;
  410. p->next = todel->next;
  411. }
  412. kfree(todel);
  413. /* Check if there are still any alarms set */
  414. p = alarmlist;
  415. some_alarms = 0;
  416. a_high = 0;
  417. a_low = 0;
  418. while (p) {
  419. if (p->minor == GPIO_MINOR_A) {
  420. a_high |= p->highalarm;
  421. a_low |= p->lowalarm;
  422. }
  423. if (p->highalarm | p->lowalarm) {
  424. some_alarms = 1;
  425. }
  426. p = p->next;
  427. }
  428. spin_lock(&alarm_lock);
  429. gpio_some_alarms = some_alarms;
  430. gpio_pa_high_alarms = a_high;
  431. gpio_pa_low_alarms = a_low;
  432. spin_unlock(&alarm_lock);
  433. return 0;
  434. }
  435. /* Main device API. ioctl's to read/set/clear bits, as well as to
  436. * set alarms to wait for using a subsequent select().
  437. */
  438. unsigned long inline setget_input(struct gpio_private *priv, unsigned long arg)
  439. {
  440. /* Set direction 0=unchanged 1=input,
  441. * return mask with 1=input
  442. */
  443. unsigned long flags;
  444. unsigned long dir_shadow;
  445. local_irq_save(flags);
  446. dir_shadow = *dir_oe[priv->minor];
  447. dir_shadow &= ~(arg & changeable_dir[priv->minor]);
  448. *dir_oe[priv->minor] = dir_shadow;
  449. local_irq_restore(flags);
  450. if (priv->minor == GPIO_MINOR_A)
  451. dir_shadow ^= 0xFF; /* Only 8 bits */
  452. else
  453. dir_shadow ^= 0x3FFFF; /* Only 18 bits */
  454. return dir_shadow;
  455. } /* setget_input */
  456. unsigned long inline setget_output(struct gpio_private *priv, unsigned long arg)
  457. {
  458. unsigned long flags;
  459. unsigned long dir_shadow;
  460. local_irq_save(flags);
  461. dir_shadow = *dir_oe[priv->minor];
  462. dir_shadow |= (arg & changeable_dir[priv->minor]);
  463. *dir_oe[priv->minor] = dir_shadow;
  464. local_irq_restore(flags);
  465. return dir_shadow;
  466. } /* setget_output */
  467. static int
  468. gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
  469. static int
  470. gpio_ioctl(struct inode *inode, struct file *file,
  471. unsigned int cmd, unsigned long arg)
  472. {
  473. unsigned long flags;
  474. unsigned long val;
  475. unsigned long shadow;
  476. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  477. if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE) {
  478. return -EINVAL;
  479. }
  480. switch (_IOC_NR(cmd)) {
  481. case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
  482. // read the port
  483. return *data_in[priv->minor];
  484. break;
  485. case IO_SETBITS:
  486. local_irq_save(flags);
  487. if (arg & 0x04)
  488. printk("GPIO SET 2\n");
  489. // set changeable bits with a 1 in arg
  490. shadow = *data_out[priv->minor];
  491. shadow |= (arg & changeable_bits[priv->minor]);
  492. *data_out[priv->minor] = shadow;
  493. local_irq_restore(flags);
  494. break;
  495. case IO_CLRBITS:
  496. local_irq_save(flags);
  497. if (arg & 0x04)
  498. printk("GPIO CLR 2\n");
  499. // clear changeable bits with a 1 in arg
  500. shadow = *data_out[priv->minor];
  501. shadow &= ~(arg & changeable_bits[priv->minor]);
  502. *data_out[priv->minor] = shadow;
  503. local_irq_restore(flags);
  504. break;
  505. case IO_HIGHALARM:
  506. // set alarm when bits with 1 in arg go high
  507. priv->highalarm |= arg;
  508. spin_lock(&alarm_lock);
  509. gpio_some_alarms = 1;
  510. if (priv->minor == GPIO_MINOR_A) {
  511. gpio_pa_high_alarms |= arg;
  512. }
  513. spin_unlock(&alarm_lock);
  514. break;
  515. case IO_LOWALARM:
  516. // set alarm when bits with 1 in arg go low
  517. priv->lowalarm |= arg;
  518. spin_lock(&alarm_lock);
  519. gpio_some_alarms = 1;
  520. if (priv->minor == GPIO_MINOR_A) {
  521. gpio_pa_low_alarms |= arg;
  522. }
  523. spin_unlock(&alarm_lock);
  524. break;
  525. case IO_CLRALARM:
  526. // clear alarm for bits with 1 in arg
  527. priv->highalarm &= ~arg;
  528. priv->lowalarm &= ~arg;
  529. spin_lock(&alarm_lock);
  530. if (priv->minor == GPIO_MINOR_A) {
  531. if (gpio_pa_high_alarms & arg ||
  532. gpio_pa_low_alarms & arg) {
  533. /* Must update the gpio_pa_*alarms masks */
  534. }
  535. }
  536. spin_unlock(&alarm_lock);
  537. break;
  538. case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
  539. /* Read direction 0=input 1=output */
  540. return *dir_oe[priv->minor];
  541. case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
  542. /* Set direction 0=unchanged 1=input,
  543. * return mask with 1=input
  544. */
  545. return setget_input(priv, arg);
  546. break;
  547. case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
  548. /* Set direction 0=unchanged 1=output,
  549. * return mask with 1=output
  550. */
  551. return setget_output(priv, arg);
  552. case IO_CFG_WRITE_MODE:
  553. {
  554. unsigned long dir_shadow;
  555. dir_shadow = *dir_oe[priv->minor];
  556. priv->clk_mask = arg & 0xFF;
  557. priv->data_mask = (arg >> 8) & 0xFF;
  558. priv->write_msb = (arg >> 16) & 0x01;
  559. /* Check if we're allowed to change the bits and
  560. * the direction is correct
  561. */
  562. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  563. (priv->data_mask & changeable_bits[priv->minor]) &&
  564. (priv->clk_mask & dir_shadow) &&
  565. (priv->data_mask & dir_shadow)))
  566. {
  567. priv->clk_mask = 0;
  568. priv->data_mask = 0;
  569. return -EPERM;
  570. }
  571. break;
  572. }
  573. case IO_READ_INBITS:
  574. /* *arg is result of reading the input pins */
  575. val = *data_in[priv->minor];
  576. if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
  577. return -EFAULT;
  578. return 0;
  579. break;
  580. case IO_READ_OUTBITS:
  581. /* *arg is result of reading the output shadow */
  582. val = *data_out[priv->minor];
  583. if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
  584. return -EFAULT;
  585. break;
  586. case IO_SETGET_INPUT:
  587. /* bits set in *arg is set to input,
  588. * *arg updated with current input pins.
  589. */
  590. if (copy_from_user(&val, (unsigned long*)arg, sizeof(val)))
  591. return -EFAULT;
  592. val = setget_input(priv, val);
  593. if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
  594. return -EFAULT;
  595. break;
  596. case IO_SETGET_OUTPUT:
  597. /* bits set in *arg is set to output,
  598. * *arg updated with current output pins.
  599. */
  600. if (copy_from_user(&val, (unsigned long*)arg, sizeof(val)))
  601. return -EFAULT;
  602. val = setget_output(priv, val);
  603. if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
  604. return -EFAULT;
  605. break;
  606. default:
  607. if (priv->minor == GPIO_MINOR_LEDS)
  608. return gpio_leds_ioctl(cmd, arg);
  609. else
  610. return -EINVAL;
  611. } /* switch */
  612. return 0;
  613. }
  614. static int
  615. gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
  616. {
  617. unsigned char green;
  618. unsigned char red;
  619. switch (_IOC_NR(cmd)) {
  620. case IO_LEDACTIVE_SET:
  621. green = ((unsigned char) arg) & 1;
  622. red = (((unsigned char) arg) >> 1) & 1;
  623. LED_ACTIVE_SET_G(green);
  624. LED_ACTIVE_SET_R(red);
  625. break;
  626. default:
  627. return -EINVAL;
  628. } /* switch */
  629. return 0;
  630. }
  631. struct file_operations gpio_fops = {
  632. .owner = THIS_MODULE,
  633. .poll = gpio_poll,
  634. .ioctl = gpio_ioctl,
  635. .write = gpio_write,
  636. .open = gpio_open,
  637. .release = gpio_release,
  638. };
  639. /* main driver initialization routine, called from mem.c */
  640. static __init int
  641. gpio_init(void)
  642. {
  643. int res;
  644. reg_intr_vect_rw_mask intr_mask;
  645. /* do the formalities */
  646. res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
  647. if (res < 0) {
  648. printk(KERN_ERR "gpio: couldn't get a major number.\n");
  649. return res;
  650. }
  651. /* Clear all leds */
  652. LED_NETWORK_SET(0);
  653. LED_ACTIVE_SET(0);
  654. LED_DISK_READ(0);
  655. LED_DISK_WRITE(0);
  656. printk("ETRAX FS GPIO driver v2.5, (c) 2003-2005 Axis Communications AB\n");
  657. /* We call etrax_gpio_wake_up_check() from timer interrupt and
  658. * from cpu_idle() in kernel/process.c
  659. * The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
  660. * in some tests.
  661. */
  662. if (request_irq(TIMER_INTR_VECT, gpio_poll_timer_interrupt,
  663. SA_SHIRQ | SA_INTERRUPT,"gpio poll", &alarmlist)) {
  664. printk("err: timer0 irq for gpio\n");
  665. }
  666. if (request_irq(GEN_IO_INTR_VECT, gpio_pa_interrupt,
  667. SA_SHIRQ | SA_INTERRUPT,"gpio PA", &alarmlist)) {
  668. printk("err: PA irq for gpio\n");
  669. }
  670. /* enable the gio and timer irq in global config */
  671. intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
  672. intr_mask.timer = 1;
  673. intr_mask.gen_io = 1;
  674. REG_WR(intr_vect, regi_irq, rw_mask, intr_mask);
  675. return res;
  676. }
  677. /* this makes sure that gpio_init is called during kernel boot */
  678. module_init(gpio_init);