gpio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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/mutex.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 DEFINE_MUTEX(gpio_mutex);
  59. static char gpio_name[] = "etrax gpio";
  60. #if 0
  61. static wait_queue_head_t *gpio_wq;
  62. #endif
  63. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  64. static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
  65. unsigned long arg);
  66. #endif
  67. static long gpio_ioctl(struct file *file, 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 = 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 = 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. mutex_lock(&gpio_mutex);
  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. mutex_unlock(&gpio_mutex);
  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 = 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 gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
  488. static int
  489. gpio_ioctl_unlocked(struct file *file, unsigned int cmd, unsigned long arg)
  490. {
  491. unsigned long flags;
  492. unsigned long val;
  493. unsigned long shadow;
  494. struct gpio_private *priv = file->private_data;
  495. if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
  496. return -EINVAL;
  497. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  498. if (priv->minor == GPIO_MINOR_V)
  499. return virtual_gpio_ioctl(file, cmd, arg);
  500. #endif
  501. switch (_IOC_NR(cmd)) {
  502. case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
  503. /* Read the port. */
  504. return *data_in[priv->minor];
  505. break;
  506. case IO_SETBITS:
  507. local_irq_save(flags);
  508. /* Set changeable bits with a 1 in arg. */
  509. shadow = *data_out[priv->minor];
  510. shadow |= (arg & changeable_bits[priv->minor]);
  511. *data_out[priv->minor] = shadow;
  512. local_irq_restore(flags);
  513. break;
  514. case IO_CLRBITS:
  515. local_irq_save(flags);
  516. /* Clear changeable bits with a 1 in arg. */
  517. shadow = *data_out[priv->minor];
  518. shadow &= ~(arg & changeable_bits[priv->minor]);
  519. *data_out[priv->minor] = shadow;
  520. local_irq_restore(flags);
  521. break;
  522. case IO_HIGHALARM:
  523. /* Set alarm when bits with 1 in arg go high. */
  524. priv->highalarm |= arg;
  525. spin_lock_irqsave(&alarm_lock, flags);
  526. gpio_some_alarms = 1;
  527. if (priv->minor == GPIO_MINOR_A)
  528. gpio_pa_high_alarms |= arg;
  529. spin_unlock_irqrestore(&alarm_lock, flags);
  530. break;
  531. case IO_LOWALARM:
  532. /* Set alarm when bits with 1 in arg go low. */
  533. priv->lowalarm |= arg;
  534. spin_lock_irqsave(&alarm_lock, flags);
  535. gpio_some_alarms = 1;
  536. if (priv->minor == GPIO_MINOR_A)
  537. gpio_pa_low_alarms |= arg;
  538. spin_unlock_irqrestore(&alarm_lock, flags);
  539. break;
  540. case IO_CLRALARM:
  541. /* Clear alarm for bits with 1 in arg. */
  542. priv->highalarm &= ~arg;
  543. priv->lowalarm &= ~arg;
  544. spin_lock_irqsave(&alarm_lock, flags);
  545. if (priv->minor == GPIO_MINOR_A) {
  546. if (gpio_pa_high_alarms & arg ||
  547. gpio_pa_low_alarms & arg)
  548. /* Must update the gpio_pa_*alarms masks */
  549. ;
  550. }
  551. spin_unlock_irqrestore(&alarm_lock, flags);
  552. break;
  553. case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
  554. /* Read direction 0=input 1=output */
  555. return *dir_oe[priv->minor];
  556. case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
  557. /* Set direction 0=unchanged 1=input,
  558. * return mask with 1=input
  559. */
  560. return setget_input(priv, arg);
  561. break;
  562. case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
  563. /* Set direction 0=unchanged 1=output,
  564. * return mask with 1=output
  565. */
  566. return setget_output(priv, arg);
  567. case IO_CFG_WRITE_MODE:
  568. {
  569. unsigned long dir_shadow;
  570. dir_shadow = *dir_oe[priv->minor];
  571. priv->clk_mask = arg & 0xFF;
  572. priv->data_mask = (arg >> 8) & 0xFF;
  573. priv->write_msb = (arg >> 16) & 0x01;
  574. /* Check if we're allowed to change the bits and
  575. * the direction is correct
  576. */
  577. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  578. (priv->data_mask & changeable_bits[priv->minor]) &&
  579. (priv->clk_mask & dir_shadow) &&
  580. (priv->data_mask & dir_shadow))) {
  581. priv->clk_mask = 0;
  582. priv->data_mask = 0;
  583. return -EPERM;
  584. }
  585. break;
  586. }
  587. case IO_READ_INBITS:
  588. /* *arg is result of reading the input pins */
  589. val = *data_in[priv->minor];
  590. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  591. return -EFAULT;
  592. return 0;
  593. break;
  594. case IO_READ_OUTBITS:
  595. /* *arg is result of reading the output shadow */
  596. val = *data_out[priv->minor];
  597. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  598. return -EFAULT;
  599. break;
  600. case IO_SETGET_INPUT:
  601. /* bits set in *arg is set to input,
  602. * *arg updated with current input pins.
  603. */
  604. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  605. return -EFAULT;
  606. val = setget_input(priv, val);
  607. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  608. return -EFAULT;
  609. break;
  610. case IO_SETGET_OUTPUT:
  611. /* bits set in *arg is set to output,
  612. * *arg updated with current output pins.
  613. */
  614. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  615. return -EFAULT;
  616. val = setget_output(priv, val);
  617. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  618. return -EFAULT;
  619. break;
  620. default:
  621. if (priv->minor == GPIO_MINOR_LEDS)
  622. return gpio_leds_ioctl(cmd, arg);
  623. else
  624. return -EINVAL;
  625. } /* switch */
  626. return 0;
  627. }
  628. static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  629. {
  630. long ret;
  631. mutex_lock(&gpio_mutex);
  632. ret = gpio_ioctl_unlocked(file, cmd, arg);
  633. mutex_unlock(&gpio_mutex);
  634. return ret;
  635. }
  636. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  637. static int
  638. virtual_gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  639. {
  640. unsigned long flags;
  641. unsigned short val;
  642. unsigned short shadow;
  643. struct gpio_private *priv = file->private_data;
  644. switch (_IOC_NR(cmd)) {
  645. case IO_SETBITS:
  646. local_irq_save(flags);
  647. /* Set 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_CLRBITS:
  655. local_irq_save(flags);
  656. /* Clear changeable bits with a 1 in arg. */
  657. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  658. shadow |= ~*dir_oe[priv->minor];
  659. shadow &= ~(arg & changeable_bits[priv->minor]);
  660. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  661. local_irq_restore(flags);
  662. break;
  663. case IO_HIGHALARM:
  664. /* Set alarm when bits with 1 in arg go high. */
  665. priv->highalarm |= arg;
  666. spin_lock(&alarm_lock);
  667. gpio_some_alarms = 1;
  668. spin_unlock(&alarm_lock);
  669. break;
  670. case IO_LOWALARM:
  671. /* Set alarm when bits with 1 in arg go low. */
  672. priv->lowalarm |= arg;
  673. spin_lock(&alarm_lock);
  674. gpio_some_alarms = 1;
  675. spin_unlock(&alarm_lock);
  676. break;
  677. case IO_CLRALARM:
  678. /* Clear alarm for bits with 1 in arg. */
  679. priv->highalarm &= ~arg;
  680. priv->lowalarm &= ~arg;
  681. spin_lock(&alarm_lock);
  682. spin_unlock(&alarm_lock);
  683. break;
  684. case IO_CFG_WRITE_MODE:
  685. {
  686. unsigned long dir_shadow;
  687. dir_shadow = *dir_oe[priv->minor];
  688. priv->clk_mask = arg & 0xFF;
  689. priv->data_mask = (arg >> 8) & 0xFF;
  690. priv->write_msb = (arg >> 16) & 0x01;
  691. /* Check if we're allowed to change the bits and
  692. * the direction is correct
  693. */
  694. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  695. (priv->data_mask & changeable_bits[priv->minor]) &&
  696. (priv->clk_mask & dir_shadow) &&
  697. (priv->data_mask & dir_shadow))) {
  698. priv->clk_mask = 0;
  699. priv->data_mask = 0;
  700. return -EPERM;
  701. }
  702. break;
  703. }
  704. case IO_READ_INBITS:
  705. /* *arg is result of reading the input pins */
  706. val = cached_virtual_gpio_read;
  707. val &= ~*dir_oe[priv->minor];
  708. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  709. return -EFAULT;
  710. return 0;
  711. break;
  712. case IO_READ_OUTBITS:
  713. /* *arg is result of reading the output shadow */
  714. i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
  715. val &= *dir_oe[priv->minor];
  716. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  717. return -EFAULT;
  718. break;
  719. case IO_SETGET_INPUT:
  720. {
  721. /* bits set in *arg is set to input,
  722. * *arg updated with current input pins.
  723. */
  724. unsigned short input_mask = ~*dir_oe[priv->minor];
  725. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  726. return -EFAULT;
  727. val = setget_input(priv, val);
  728. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  729. return -EFAULT;
  730. if ((input_mask & val) != input_mask) {
  731. /* Input pins changed. All ports desired as input
  732. * should be set to logic 1.
  733. */
  734. unsigned short change = input_mask ^ val;
  735. i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
  736. sizeof(shadow));
  737. shadow &= ~change;
  738. shadow |= val;
  739. i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
  740. sizeof(shadow));
  741. }
  742. break;
  743. }
  744. case IO_SETGET_OUTPUT:
  745. /* bits set in *arg is set to output,
  746. * *arg updated with current output pins.
  747. */
  748. if (copy_from_user(&val, (unsigned long *)arg, sizeof(val)))
  749. return -EFAULT;
  750. val = setget_output(priv, val);
  751. if (copy_to_user((unsigned long *)arg, &val, sizeof(val)))
  752. return -EFAULT;
  753. break;
  754. default:
  755. return -EINVAL;
  756. } /* switch */
  757. return 0;
  758. }
  759. #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
  760. static int
  761. gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
  762. {
  763. unsigned char green;
  764. unsigned char red;
  765. switch (_IOC_NR(cmd)) {
  766. case IO_LEDACTIVE_SET:
  767. green = ((unsigned char) arg) & 1;
  768. red = (((unsigned char) arg) >> 1) & 1;
  769. CRIS_LED_ACTIVE_SET_G(green);
  770. CRIS_LED_ACTIVE_SET_R(red);
  771. break;
  772. default:
  773. return -EINVAL;
  774. } /* switch */
  775. return 0;
  776. }
  777. static const struct file_operations gpio_fops = {
  778. .owner = THIS_MODULE,
  779. .poll = gpio_poll,
  780. .unlocked_ioctl = gpio_ioctl,
  781. .write = gpio_write,
  782. .open = gpio_open,
  783. .release = gpio_release,
  784. };
  785. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  786. static void
  787. virtual_gpio_init(void)
  788. {
  789. reg_gio_rw_intr_cfg intr_cfg;
  790. reg_gio_rw_intr_mask intr_mask;
  791. unsigned short shadow;
  792. shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
  793. shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
  794. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  795. /* Set interrupt mask and on what state the interrupt shall trigger.
  796. * For virtual gpio the interrupt shall trigger on logic '0'.
  797. */
  798. intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
  799. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  800. switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
  801. case 0:
  802. intr_cfg.pa0 = regk_gio_lo;
  803. intr_mask.pa0 = regk_gio_yes;
  804. break;
  805. case 1:
  806. intr_cfg.pa1 = regk_gio_lo;
  807. intr_mask.pa1 = regk_gio_yes;
  808. break;
  809. case 2:
  810. intr_cfg.pa2 = regk_gio_lo;
  811. intr_mask.pa2 = regk_gio_yes;
  812. break;
  813. case 3:
  814. intr_cfg.pa3 = regk_gio_lo;
  815. intr_mask.pa3 = regk_gio_yes;
  816. break;
  817. case 4:
  818. intr_cfg.pa4 = regk_gio_lo;
  819. intr_mask.pa4 = regk_gio_yes;
  820. break;
  821. case 5:
  822. intr_cfg.pa5 = regk_gio_lo;
  823. intr_mask.pa5 = regk_gio_yes;
  824. break;
  825. case 6:
  826. intr_cfg.pa6 = regk_gio_lo;
  827. intr_mask.pa6 = regk_gio_yes;
  828. break;
  829. case 7:
  830. intr_cfg.pa7 = regk_gio_lo;
  831. intr_mask.pa7 = regk_gio_yes;
  832. break;
  833. }
  834. REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
  835. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  836. gpio_pa_low_alarms |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  837. gpio_some_alarms = 1;
  838. }
  839. #endif
  840. /* main driver initialization routine, called from mem.c */
  841. static __init int
  842. gpio_init(void)
  843. {
  844. int res;
  845. /* do the formalities */
  846. res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
  847. if (res < 0) {
  848. printk(KERN_ERR "gpio: couldn't get a major number.\n");
  849. return res;
  850. }
  851. /* Clear all leds */
  852. CRIS_LED_NETWORK_GRP0_SET(0);
  853. CRIS_LED_NETWORK_GRP1_SET(0);
  854. CRIS_LED_ACTIVE_SET(0);
  855. CRIS_LED_DISK_READ(0);
  856. CRIS_LED_DISK_WRITE(0);
  857. printk(KERN_INFO "ETRAX FS GPIO driver v2.5, (c) 2003-2007 "
  858. "Axis Communications AB\n");
  859. /* We call etrax_gpio_wake_up_check() from timer interrupt and
  860. * from cpu_idle() in kernel/process.c
  861. * The check in cpu_idle() reduces latency from ~15 ms to ~6 ms
  862. * in some tests.
  863. */
  864. if (request_irq(TIMER0_INTR_VECT, gpio_poll_timer_interrupt,
  865. IRQF_SHARED | IRQF_DISABLED, "gpio poll", &alarmlist))
  866. printk(KERN_ERR "timer0 irq for gpio\n");
  867. if (request_irq(GIO_INTR_VECT, gpio_pa_interrupt,
  868. IRQF_SHARED | IRQF_DISABLED, "gpio PA", &alarmlist))
  869. printk(KERN_ERR "PA irq for gpio\n");
  870. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  871. virtual_gpio_init();
  872. #endif
  873. return res;
  874. }
  875. /* this makes sure that gpio_init is called during kernel boot */
  876. module_init(gpio_init);