gpio.c 25 KB

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