gpio.c 19 KB

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