gpio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * ETRAX CRISv32 general port I/O device
  3. *
  4. * Copyright (c) 1999-2006 Axis Communications AB
  5. *
  6. * Authors: Bjorn Wesen (initial version)
  7. * Ola Knutsson (LED handling)
  8. * Johan Adolfsson (read/set directions, write, port G,
  9. * port to ETRAX FS.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/ioport.h>
  16. #include <linux/errno.h>
  17. #include <linux/kernel.h>
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/poll.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/spinlock.h>
  24. #include <asm/etraxgpio.h>
  25. #include <hwregs/reg_map.h>
  26. #include <hwregs/reg_rdwr.h>
  27. #include <hwregs/gio_defs.h>
  28. #include <hwregs/intr_vect_defs.h>
  29. #include <asm/io.h>
  30. #include <asm/system.h>
  31. #include <asm/irq.h>
  32. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  33. #include "../i2c.h"
  34. #define VIRT_I2C_ADDR 0x40
  35. #endif
  36. /* The following gio ports on ETRAX FS is available:
  37. * pa 8 bits, supports interrupts off, hi, low, set, posedge, negedge anyedge
  38. * pb 18 bits
  39. * pc 18 bits
  40. * pd 18 bits
  41. * pe 18 bits
  42. * each port has a rw_px_dout, r_px_din and rw_px_oe register.
  43. */
  44. #define GPIO_MAJOR 120 /* experimental MAJOR number */
  45. #define D(x)
  46. #if 0
  47. static int dp_cnt;
  48. #define DP(x) \
  49. do { \
  50. dp_cnt++; \
  51. if (dp_cnt % 1000 == 0) \
  52. x; \
  53. } while (0)
  54. #else
  55. #define DP(x)
  56. #endif
  57. static char gpio_name[] = "etrax gpio";
  58. #if 0
  59. static wait_queue_head_t *gpio_wq;
  60. #endif
  61. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  62. static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
  63. unsigned long arg);
  64. #endif
  65. static int gpio_ioctl(struct inode *inode, struct file *file,
  66. unsigned int cmd, unsigned long arg);
  67. static ssize_t gpio_write(struct file *file, const char *buf, size_t count,
  68. loff_t *off);
  69. static int gpio_open(struct inode *inode, struct file *filp);
  70. static int gpio_release(struct inode *inode, struct file *filp);
  71. static unsigned int gpio_poll(struct file *filp,
  72. struct poll_table_struct *wait);
  73. /* private data per open() of this driver */
  74. struct gpio_private {
  75. struct gpio_private *next;
  76. /* The IO_CFG_WRITE_MODE_VALUE only support 8 bits: */
  77. unsigned char clk_mask;
  78. unsigned char data_mask;
  79. unsigned char write_msb;
  80. unsigned char pad1;
  81. /* These fields are generic */
  82. unsigned long highalarm, lowalarm;
  83. wait_queue_head_t alarm_wq;
  84. int minor;
  85. };
  86. /* linked list of alarms to check for */
  87. static struct gpio_private *alarmlist;
  88. static int gpio_some_alarms; /* Set if someone uses alarm */
  89. static unsigned long gpio_pa_high_alarms;
  90. static unsigned long gpio_pa_low_alarms;
  91. static DEFINE_SPINLOCK(alarm_lock);
  92. #define NUM_PORTS (GPIO_MINOR_LAST+1)
  93. #define GIO_REG_RD_ADDR(reg) \
  94. (volatile unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
  95. #define GIO_REG_WR_ADDR(reg) \
  96. (volatile unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
  97. unsigned long led_dummy;
  98. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  99. static unsigned long virtual_dummy;
  100. static unsigned long virtual_rw_pv_oe = CONFIG_ETRAX_DEF_GIO_PV_OE;
  101. static unsigned short cached_virtual_gpio_read;
  102. #endif
  103. static volatile unsigned long *data_out[NUM_PORTS] = {
  104. GIO_REG_WR_ADDR(rw_pa_dout),
  105. GIO_REG_WR_ADDR(rw_pb_dout),
  106. &led_dummy,
  107. GIO_REG_WR_ADDR(rw_pc_dout),
  108. GIO_REG_WR_ADDR(rw_pd_dout),
  109. GIO_REG_WR_ADDR(rw_pe_dout),
  110. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  111. &virtual_dummy,
  112. #endif
  113. };
  114. static volatile unsigned long *data_in[NUM_PORTS] = {
  115. GIO_REG_RD_ADDR(r_pa_din),
  116. GIO_REG_RD_ADDR(r_pb_din),
  117. &led_dummy,
  118. GIO_REG_RD_ADDR(r_pc_din),
  119. GIO_REG_RD_ADDR(r_pd_din),
  120. GIO_REG_RD_ADDR(r_pe_din),
  121. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  122. &virtual_dummy,
  123. #endif
  124. };
  125. static unsigned long changeable_dir[NUM_PORTS] = {
  126. CONFIG_ETRAX_PA_CHANGEABLE_DIR,
  127. CONFIG_ETRAX_PB_CHANGEABLE_DIR,
  128. 0,
  129. CONFIG_ETRAX_PC_CHANGEABLE_DIR,
  130. CONFIG_ETRAX_PD_CHANGEABLE_DIR,
  131. CONFIG_ETRAX_PE_CHANGEABLE_DIR,
  132. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  133. CONFIG_ETRAX_PV_CHANGEABLE_DIR,
  134. #endif
  135. };
  136. static unsigned long changeable_bits[NUM_PORTS] = {
  137. CONFIG_ETRAX_PA_CHANGEABLE_BITS,
  138. CONFIG_ETRAX_PB_CHANGEABLE_BITS,
  139. 0,
  140. CONFIG_ETRAX_PC_CHANGEABLE_BITS,
  141. CONFIG_ETRAX_PD_CHANGEABLE_BITS,
  142. CONFIG_ETRAX_PE_CHANGEABLE_BITS,
  143. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  144. CONFIG_ETRAX_PV_CHANGEABLE_BITS,
  145. #endif
  146. };
  147. static volatile unsigned long *dir_oe[NUM_PORTS] = {
  148. GIO_REG_WR_ADDR(rw_pa_oe),
  149. GIO_REG_WR_ADDR(rw_pb_oe),
  150. &led_dummy,
  151. GIO_REG_WR_ADDR(rw_pc_oe),
  152. GIO_REG_WR_ADDR(rw_pd_oe),
  153. GIO_REG_WR_ADDR(rw_pe_oe),
  154. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  155. &virtual_rw_pv_oe,
  156. #endif
  157. };
  158. static unsigned int gpio_poll(struct file *file, struct poll_table *wait)
  159. {
  160. unsigned int mask = 0;
  161. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  162. unsigned long data;
  163. poll_wait(file, &priv->alarm_wq, wait);
  164. if (priv->minor == GPIO_MINOR_A) {
  165. reg_gio_rw_intr_cfg intr_cfg;
  166. unsigned long tmp;
  167. unsigned long flags;
  168. local_irq_save(flags);
  169. data = REG_TYPE_CONV(unsigned long, reg_gio_r_pa_din,
  170. REG_RD(gio, regi_gio, r_pa_din));
  171. /* PA has support for interrupt
  172. * lets activate high for those low and with highalarm set
  173. */
  174. intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
  175. tmp = ~data & priv->highalarm & 0xFF;
  176. if (tmp & (1 << 0))
  177. intr_cfg.pa0 = regk_gio_hi;
  178. if (tmp & (1 << 1))
  179. intr_cfg.pa1 = regk_gio_hi;
  180. if (tmp & (1 << 2))
  181. intr_cfg.pa2 = regk_gio_hi;
  182. if (tmp & (1 << 3))
  183. intr_cfg.pa3 = regk_gio_hi;
  184. if (tmp & (1 << 4))
  185. intr_cfg.pa4 = regk_gio_hi;
  186. if (tmp & (1 << 5))
  187. intr_cfg.pa5 = regk_gio_hi;
  188. if (tmp & (1 << 6))
  189. intr_cfg.pa6 = regk_gio_hi;
  190. if (tmp & (1 << 7))
  191. intr_cfg.pa7 = regk_gio_hi;
  192. /*
  193. * lets activate low for those high and with lowalarm set
  194. */
  195. tmp = data & priv->lowalarm & 0xFF;
  196. if (tmp & (1 << 0))
  197. intr_cfg.pa0 = regk_gio_lo;
  198. if (tmp & (1 << 1))
  199. intr_cfg.pa1 = regk_gio_lo;
  200. if (tmp & (1 << 2))
  201. intr_cfg.pa2 = regk_gio_lo;
  202. if (tmp & (1 << 3))
  203. intr_cfg.pa3 = regk_gio_lo;
  204. if (tmp & (1 << 4))
  205. intr_cfg.pa4 = regk_gio_lo;
  206. if (tmp & (1 << 5))
  207. intr_cfg.pa5 = regk_gio_lo;
  208. if (tmp & (1 << 6))
  209. intr_cfg.pa6 = regk_gio_lo;
  210. if (tmp & (1 << 7))
  211. intr_cfg.pa7 = regk_gio_lo;
  212. REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
  213. local_irq_restore(flags);
  214. } else if (priv->minor <= GPIO_MINOR_E)
  215. data = *data_in[priv->minor];
  216. else
  217. return 0;
  218. if ((data & priv->highalarm) || (~data & priv->lowalarm))
  219. mask = POLLIN|POLLRDNORM;
  220. DP(printk(KERN_DEBUG "gpio_poll ready: mask 0x%08X\n", mask));
  221. return mask;
  222. }
  223. int etrax_gpio_wake_up_check(void)
  224. {
  225. struct gpio_private *priv;
  226. unsigned long data = 0;
  227. unsigned long flags;
  228. int ret = 0;
  229. spin_lock_irqsave(&alarm_lock, flags);
  230. priv = alarmlist;
  231. while (priv) {
  232. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  233. if (priv->minor == GPIO_MINOR_V)
  234. data = (unsigned long)cached_virtual_gpio_read;
  235. else {
  236. data = *data_in[priv->minor];
  237. if (priv->minor == GPIO_MINOR_A)
  238. priv->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  239. }
  240. #else
  241. data = *data_in[priv->minor];
  242. #endif
  243. if ((data & priv->highalarm) ||
  244. (~data & priv->lowalarm)) {
  245. DP(printk(KERN_DEBUG
  246. "etrax_gpio_wake_up_check %i\n", priv->minor));
  247. wake_up_interruptible(&priv->alarm_wq);
  248. ret = 1;
  249. }
  250. priv = priv->next;
  251. }
  252. spin_unlock_irqrestore(&alarm_lock, flags);
  253. return ret;
  254. }
  255. static irqreturn_t
  256. gpio_poll_timer_interrupt(int irq, void *dev_id)
  257. {
  258. if (gpio_some_alarms)
  259. return IRQ_RETVAL(etrax_gpio_wake_up_check());
  260. return IRQ_NONE;
  261. }
  262. static irqreturn_t
  263. gpio_pa_interrupt(int irq, void *dev_id)
  264. {
  265. reg_gio_rw_intr_mask intr_mask;
  266. reg_gio_r_masked_intr masked_intr;
  267. reg_gio_rw_ack_intr ack_intr;
  268. unsigned long tmp;
  269. unsigned long tmp2;
  270. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  271. unsigned char enable_gpiov_ack = 0;
  272. #endif
  273. /* Find what PA interrupts are active */
  274. masked_intr = REG_RD(gio, regi_gio, r_masked_intr);
  275. tmp = REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr, masked_intr);
  276. /* Find those that we have enabled */
  277. spin_lock(&alarm_lock);
  278. tmp &= (gpio_pa_high_alarms | gpio_pa_low_alarms);
  279. spin_unlock(&alarm_lock);
  280. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  281. /* Something changed on virtual GPIO. Interrupt is acked by
  282. * reading the device.
  283. */
  284. if (tmp & (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN)) {
  285. i2c_read(VIRT_I2C_ADDR, (void *)&cached_virtual_gpio_read,
  286. sizeof(cached_virtual_gpio_read));
  287. enable_gpiov_ack = 1;
  288. }
  289. #endif
  290. /* Ack them */
  291. ack_intr = REG_TYPE_CONV(reg_gio_rw_ack_intr, unsigned long, tmp);
  292. REG_WR(gio, regi_gio, rw_ack_intr, ack_intr);
  293. /* Disable those interrupts.. */
  294. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  295. tmp2 = REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask, intr_mask);
  296. tmp2 &= ~tmp;
  297. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  298. /* Do not disable interrupt on virtual GPIO. Changes on virtual
  299. * pins are only noticed by an interrupt.
  300. */
  301. if (enable_gpiov_ack)
  302. tmp2 |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  303. #endif
  304. intr_mask = REG_TYPE_CONV(reg_gio_rw_intr_mask, unsigned long, tmp2);
  305. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  306. if (gpio_some_alarms)
  307. return IRQ_RETVAL(etrax_gpio_wake_up_check());
  308. return IRQ_NONE;
  309. }
  310. static ssize_t gpio_write(struct file *file, const char *buf, size_t count,
  311. loff_t *off)
  312. {
  313. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  314. unsigned char data, clk_mask, data_mask, write_msb;
  315. unsigned long flags;
  316. unsigned long shadow;
  317. volatile unsigned long *port;
  318. ssize_t retval = count;
  319. /* Only bits 0-7 may be used for write operations but allow all
  320. devices except leds... */
  321. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  322. if (priv->minor == GPIO_MINOR_V)
  323. return -EFAULT;
  324. #endif
  325. if (priv->minor == GPIO_MINOR_LEDS)
  326. return -EFAULT;
  327. if (!access_ok(VERIFY_READ, buf, count))
  328. return -EFAULT;
  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. write_msb = priv->write_msb;
  336. D(printk(KERN_DEBUG "gpio_write: %lu to data 0x%02X clk 0x%02X "
  337. "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 = kmalloc(sizeof(struct gpio_private), GFP_KERNEL);
  380. if (!priv)
  381. return -ENOMEM;
  382. memset(priv, 0, sizeof(*priv));
  383. priv->minor = p;
  384. /* initialize the io/alarm struct */
  385. priv->clk_mask = 0;
  386. priv->data_mask = 0;
  387. priv->highalarm = 0;
  388. priv->lowalarm = 0;
  389. init_waitqueue_head(&priv->alarm_wq);
  390. filp->private_data = (void *)priv;
  391. /* link it into our alarmlist */
  392. spin_lock_irq(&alarm_lock);
  393. priv->next = alarmlist;
  394. alarmlist = priv;
  395. spin_unlock_irq(&alarm_lock);
  396. return 0;
  397. }
  398. static int
  399. gpio_release(struct inode *inode, struct file *filp)
  400. {
  401. struct gpio_private *p;
  402. struct gpio_private *todel;
  403. /* local copies while updating them: */
  404. unsigned long a_high, a_low;
  405. unsigned long some_alarms;
  406. /* unlink from alarmlist and free the private structure */
  407. spin_lock_irq(&alarm_lock);
  408. p = alarmlist;
  409. todel = (struct gpio_private *)filp->private_data;
  410. if (p == todel) {
  411. alarmlist = todel->next;
  412. } else {
  413. while (p->next != todel)
  414. p = p->next;
  415. p->next = todel->next;
  416. }
  417. kfree(todel);
  418. /* Check if there are still any alarms set */
  419. p = alarmlist;
  420. some_alarms = 0;
  421. a_high = 0;
  422. a_low = 0;
  423. while (p) {
  424. if (p->minor == GPIO_MINOR_A) {
  425. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  426. p->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  427. #endif
  428. a_high |= p->highalarm;
  429. a_low |= p->lowalarm;
  430. }
  431. if (p->highalarm | p->lowalarm)
  432. some_alarms = 1;
  433. p = p->next;
  434. }
  435. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  436. /* Variables 'some_alarms' and 'a_low' needs to be set here again
  437. * to ensure that interrupt for virtual GPIO is handled.
  438. */
  439. some_alarms = 1;
  440. a_low |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  441. #endif
  442. gpio_some_alarms = some_alarms;
  443. gpio_pa_high_alarms = a_high;
  444. gpio_pa_low_alarms = a_low;
  445. spin_unlock_irq(&alarm_lock);
  446. return 0;
  447. }
  448. /* Main device API. ioctl's to read/set/clear bits, as well as to
  449. * set alarms to wait for using a subsequent select().
  450. */
  451. inline unsigned long setget_input(struct gpio_private *priv, unsigned long arg)
  452. {
  453. /* Set direction 0=unchanged 1=input,
  454. * return mask with 1=input
  455. */
  456. unsigned long flags;
  457. unsigned long dir_shadow;
  458. local_irq_save(flags);
  459. dir_shadow = *dir_oe[priv->minor];
  460. dir_shadow &= ~(arg & changeable_dir[priv->minor]);
  461. *dir_oe[priv->minor] = dir_shadow;
  462. local_irq_restore(flags);
  463. if (priv->minor == GPIO_MINOR_A)
  464. dir_shadow ^= 0xFF; /* Only 8 bits */
  465. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  466. else if (priv->minor == GPIO_MINOR_V)
  467. dir_shadow ^= 0xFFFF; /* Only 16 bits */
  468. #endif
  469. else
  470. dir_shadow ^= 0x3FFFF; /* Only 18 bits */
  471. return dir_shadow;
  472. } /* setget_input */
  473. inline unsigned long setget_output(struct gpio_private *priv, unsigned long arg)
  474. {
  475. unsigned long flags;
  476. unsigned long dir_shadow;
  477. local_irq_save(flags);
  478. dir_shadow = *dir_oe[priv->minor];
  479. dir_shadow |= (arg & changeable_dir[priv->minor]);
  480. *dir_oe[priv->minor] = dir_shadow;
  481. local_irq_restore(flags);
  482. return dir_shadow;
  483. } /* setget_output */
  484. static int
  485. gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
  486. static int
  487. gpio_ioctl(struct inode *inode, struct file *file,
  488. unsigned int cmd, unsigned long arg)
  489. {
  490. unsigned long flags;
  491. unsigned long val;
  492. unsigned long shadow;
  493. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  494. if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
  495. return -EINVAL;
  496. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  497. if (priv->minor == GPIO_MINOR_V)
  498. return virtual_gpio_ioctl(file, cmd, arg);
  499. #endif
  500. switch (_IOC_NR(cmd)) {
  501. case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
  502. /* Read the port. */
  503. return *data_in[priv->minor];
  504. break;
  505. case IO_SETBITS:
  506. local_irq_save(flags);
  507. /* Set changeable bits with a 1 in arg. */
  508. shadow = *data_out[priv->minor];
  509. shadow |= (arg & changeable_bits[priv->minor]);
  510. *data_out[priv->minor] = shadow;
  511. local_irq_restore(flags);
  512. break;
  513. case IO_CLRBITS:
  514. local_irq_save(flags);
  515. /* Clear changeable bits with a 1 in arg. */
  516. shadow = *data_out[priv->minor];
  517. shadow &= ~(arg & changeable_bits[priv->minor]);
  518. *data_out[priv->minor] = shadow;
  519. local_irq_restore(flags);
  520. break;
  521. case IO_HIGHALARM:
  522. /* Set alarm when bits with 1 in arg go high. */
  523. priv->highalarm |= arg;
  524. spin_lock_irqsave(&alarm_lock, flags);
  525. gpio_some_alarms = 1;
  526. if (priv->minor == GPIO_MINOR_A)
  527. gpio_pa_high_alarms |= arg;
  528. spin_unlock_irqrestore(&alarm_lock, flags);
  529. break;
  530. case IO_LOWALARM:
  531. /* Set alarm when bits with 1 in arg go low. */
  532. priv->lowalarm |= arg;
  533. spin_lock_irqsave(&alarm_lock, flags);
  534. gpio_some_alarms = 1;
  535. if (priv->minor == GPIO_MINOR_A)
  536. gpio_pa_low_alarms |= arg;
  537. spin_unlock_irqrestore(&alarm_lock, flags);
  538. break;
  539. case IO_CLRALARM:
  540. /* Clear alarm for bits with 1 in arg. */
  541. priv->highalarm &= ~arg;
  542. priv->lowalarm &= ~arg;
  543. spin_lock_irqsave(&alarm_lock, flags);
  544. if (priv->minor == GPIO_MINOR_A) {
  545. if (gpio_pa_high_alarms & arg ||
  546. gpio_pa_low_alarms & arg)
  547. /* Must update the gpio_pa_*alarms masks */
  548. ;
  549. }
  550. spin_unlock_irqrestore(&alarm_lock, flags);
  551. break;
  552. case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
  553. /* Read direction 0=input 1=output */
  554. return *dir_oe[priv->minor];
  555. case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
  556. /* Set direction 0=unchanged 1=input,
  557. * return mask with 1=input
  558. */
  559. return setget_input(priv, arg);
  560. break;
  561. case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
  562. /* Set direction 0=unchanged 1=output,
  563. * return mask with 1=output
  564. */
  565. return setget_output(priv, arg);
  566. case IO_CFG_WRITE_MODE:
  567. {
  568. unsigned long dir_shadow;
  569. dir_shadow = *dir_oe[priv->minor];
  570. priv->clk_mask = arg & 0xFF;
  571. priv->data_mask = (arg >> 8) & 0xFF;
  572. priv->write_msb = (arg >> 16) & 0x01;
  573. /* Check if we're allowed to change the bits and
  574. * the direction is correct
  575. */
  576. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  577. (priv->data_mask & changeable_bits[priv->minor]) &&
  578. (priv->clk_mask & dir_shadow) &&
  579. (priv->data_mask & dir_shadow))) {
  580. priv->clk_mask = 0;
  581. priv->data_mask = 0;
  582. return -EPERM;
  583. }
  584. break;
  585. }
  586. case IO_READ_INBITS:
  587. /* *arg is result of reading the input pins */
  588. val = *data_in[priv->minor];
  589. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  590. return -EFAULT;
  591. return 0;
  592. break;
  593. case IO_READ_OUTBITS:
  594. /* *arg is result of reading the output shadow */
  595. val = *data_out[priv->minor];
  596. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  597. return -EFAULT;
  598. break;
  599. case IO_SETGET_INPUT:
  600. /* bits set in *arg is set to input,
  601. * *arg updated with current input pins.
  602. */
  603. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  604. return -EFAULT;
  605. val = setget_input(priv, val);
  606. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  607. return -EFAULT;
  608. break;
  609. case IO_SETGET_OUTPUT:
  610. /* bits set in *arg is set to output,
  611. * *arg updated with current output pins.
  612. */
  613. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  614. return -EFAULT;
  615. val = setget_output(priv, val);
  616. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  617. return -EFAULT;
  618. break;
  619. default:
  620. if (priv->minor == GPIO_MINOR_LEDS)
  621. return gpio_leds_ioctl(cmd, arg);
  622. else
  623. return -EINVAL;
  624. } /* switch */
  625. return 0;
  626. }
  627. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  628. static int
  629. virtual_gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  630. {
  631. unsigned long flags;
  632. unsigned short val;
  633. unsigned short shadow;
  634. struct gpio_private *priv = (struct gpio_private *)file->private_data;
  635. switch (_IOC_NR(cmd)) {
  636. case IO_SETBITS:
  637. local_irq_save(flags);
  638. /* Set changeable bits with a 1 in arg. */
  639. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  640. shadow |= ~*dir_oe[priv->minor];
  641. shadow |= (arg & changeable_bits[priv->minor]);
  642. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  643. local_irq_restore(flags);
  644. break;
  645. case IO_CLRBITS:
  646. local_irq_save(flags);
  647. /* Clear changeable bits with a 1 in arg. */
  648. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  649. shadow |= ~*dir_oe[priv->minor];
  650. shadow &= ~(arg & changeable_bits[priv->minor]);
  651. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  652. local_irq_restore(flags);
  653. break;
  654. case IO_HIGHALARM:
  655. /* Set alarm when bits with 1 in arg go high. */
  656. priv->highalarm |= arg;
  657. spin_lock(&alarm_lock);
  658. gpio_some_alarms = 1;
  659. spin_unlock(&alarm_lock);
  660. break;
  661. case IO_LOWALARM:
  662. /* Set alarm when bits with 1 in arg go low. */
  663. priv->lowalarm |= arg;
  664. spin_lock(&alarm_lock);
  665. gpio_some_alarms = 1;
  666. spin_unlock(&alarm_lock);
  667. break;
  668. case IO_CLRALARM:
  669. /* Clear alarm for bits with 1 in arg. */
  670. priv->highalarm &= ~arg;
  671. priv->lowalarm &= ~arg;
  672. spin_lock(&alarm_lock);
  673. spin_unlock(&alarm_lock);
  674. break;
  675. case IO_CFG_WRITE_MODE:
  676. {
  677. unsigned long dir_shadow;
  678. dir_shadow = *dir_oe[priv->minor];
  679. priv->clk_mask = arg & 0xFF;
  680. priv->data_mask = (arg >> 8) & 0xFF;
  681. priv->write_msb = (arg >> 16) & 0x01;
  682. /* Check if we're allowed to change the bits and
  683. * the direction is correct
  684. */
  685. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  686. (priv->data_mask & changeable_bits[priv->minor]) &&
  687. (priv->clk_mask & dir_shadow) &&
  688. (priv->data_mask & dir_shadow))) {
  689. priv->clk_mask = 0;
  690. priv->data_mask = 0;
  691. return -EPERM;
  692. }
  693. break;
  694. }
  695. case IO_READ_INBITS:
  696. /* *arg is result of reading the input pins */
  697. val = cached_virtual_gpio_read;
  698. val &= ~*dir_oe[priv->minor];
  699. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  700. return -EFAULT;
  701. return 0;
  702. break;
  703. case IO_READ_OUTBITS:
  704. /* *arg is result of reading the output shadow */
  705. i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
  706. val &= *dir_oe[priv->minor];
  707. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  708. return -EFAULT;
  709. break;
  710. case IO_SETGET_INPUT:
  711. {
  712. /* bits set in *arg is set to input,
  713. * *arg updated with current input pins.
  714. */
  715. unsigned short input_mask = ~*dir_oe[priv->minor];
  716. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  717. return -EFAULT;
  718. val = setget_input(priv, val);
  719. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  720. return -EFAULT;
  721. if ((input_mask & val) != input_mask) {
  722. /* Input pins changed. All ports desired as input
  723. * should be set to logic 1.
  724. */
  725. unsigned short change = input_mask ^ val;
  726. i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
  727. sizeof(shadow));
  728. shadow &= ~change;
  729. shadow |= val;
  730. i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
  731. sizeof(shadow));
  732. }
  733. break;
  734. }
  735. case IO_SETGET_OUTPUT:
  736. /* bits set in *arg is set to output,
  737. * *arg updated with current output pins.
  738. */
  739. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  740. return -EFAULT;
  741. val = setget_output(priv, val);
  742. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  743. return -EFAULT;
  744. break;
  745. default:
  746. return -EINVAL;
  747. } /* switch */
  748. return 0;
  749. }
  750. #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
  751. static int
  752. gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
  753. {
  754. unsigned char green;
  755. unsigned char red;
  756. switch (_IOC_NR(cmd)) {
  757. case IO_LEDACTIVE_SET:
  758. green = ((unsigned char) arg) & 1;
  759. red = (((unsigned char) arg) >> 1) & 1;
  760. LED_ACTIVE_SET_G(green);
  761. LED_ACTIVE_SET_R(red);
  762. break;
  763. default:
  764. return -EINVAL;
  765. } /* switch */
  766. return 0;
  767. }
  768. struct file_operations gpio_fops = {
  769. .owner = THIS_MODULE,
  770. .poll = gpio_poll,
  771. .ioctl = gpio_ioctl,
  772. .write = gpio_write,
  773. .open = gpio_open,
  774. .release = gpio_release,
  775. };
  776. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  777. static void
  778. virtual_gpio_init(void)
  779. {
  780. reg_gio_rw_intr_cfg intr_cfg;
  781. reg_gio_rw_intr_mask intr_mask;
  782. unsigned short shadow;
  783. shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
  784. shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
  785. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  786. /* Set interrupt mask and on what state the interrupt shall trigger.
  787. * For virtual gpio the interrupt shall trigger on logic '0'.
  788. */
  789. intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
  790. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  791. switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
  792. case 0:
  793. intr_cfg.pa0 = regk_gio_lo;
  794. intr_mask.pa0 = regk_gio_yes;
  795. break;
  796. case 1:
  797. intr_cfg.pa1 = regk_gio_lo;
  798. intr_mask.pa1 = regk_gio_yes;
  799. break;
  800. case 2:
  801. intr_cfg.pa2 = regk_gio_lo;
  802. intr_mask.pa2 = regk_gio_yes;
  803. break;
  804. case 3:
  805. intr_cfg.pa3 = regk_gio_lo;
  806. intr_mask.pa3 = regk_gio_yes;
  807. break;
  808. case 4:
  809. intr_cfg.pa4 = regk_gio_lo;
  810. intr_mask.pa4 = regk_gio_yes;
  811. break;
  812. case 5:
  813. intr_cfg.pa5 = regk_gio_lo;
  814. intr_mask.pa5 = regk_gio_yes;
  815. break;
  816. case 6:
  817. intr_cfg.pa6 = regk_gio_lo;
  818. intr_mask.pa6 = regk_gio_yes;
  819. break;
  820. case 7:
  821. intr_cfg.pa7 = regk_gio_lo;
  822. intr_mask.pa7 = regk_gio_yes;
  823. break;
  824. }
  825. REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
  826. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  827. gpio_pa_low_alarms |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  828. gpio_some_alarms = 1;
  829. }
  830. #endif
  831. /* main driver initialization routine, called from mem.c */
  832. static __init int
  833. gpio_init(void)
  834. {
  835. int res;
  836. /* do the formalities */
  837. res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
  838. if (res < 0) {
  839. printk(KERN_ERR "gpio: couldn't get a major number.\n");
  840. return res;
  841. }
  842. /* Clear all leds */
  843. LED_NETWORK_GRP0_SET(0);
  844. LED_NETWORK_GRP1_SET(0);
  845. LED_ACTIVE_SET(0);
  846. LED_DISK_READ(0);
  847. LED_DISK_WRITE(0);
  848. printk(KERN_INFO "ETRAX FS GPIO driver v2.5, (c) 2003-2007 "
  849. "Axis Communications AB\n");
  850. /* We call etrax_gpio_wake_up_check() from timer interrupt and
  851. * from cpu_idle() in kernel/process.c
  852. * The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
  853. * in some tests.
  854. */
  855. if (request_irq(TIMER0_INTR_VECT, gpio_poll_timer_interrupt,
  856. IRQF_SHARED | IRQF_DISABLED, "gpio poll", &alarmlist))
  857. printk(KERN_ERR "timer0 irq for gpio\n");
  858. if (request_irq(GIO_INTR_VECT, gpio_pa_interrupt,
  859. IRQF_SHARED | IRQF_DISABLED, "gpio PA", &alarmlist))
  860. printk(KERN_ERR "PA irq for gpio\n");
  861. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  862. virtual_gpio_init();
  863. #endif
  864. return res;
  865. }
  866. /* this makes sure that gpio_init is called during kernel boot */
  867. module_init(gpio_init);