gpio.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * Artec-3 general port I/O device
  3. *
  4. * Copyright (c) 2007 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. * Ricard Wanderlof (PWM for Artpec-3)
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/ioport.h>
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/fs.h>
  20. #include <linux/string.h>
  21. #include <linux/poll.h>
  22. #include <linux/init.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/mutex.h>
  26. #include <asm/etraxgpio.h>
  27. #include <hwregs/reg_map.h>
  28. #include <hwregs/reg_rdwr.h>
  29. #include <hwregs/gio_defs.h>
  30. #include <hwregs/intr_vect_defs.h>
  31. #include <asm/io.h>
  32. #include <asm/irq.h>
  33. #include <mach/pinmux.h>
  34. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  35. #include "../i2c.h"
  36. #define VIRT_I2C_ADDR 0x40
  37. #endif
  38. /* The following gio ports on ARTPEC-3 is available:
  39. * pa 32 bits
  40. * pb 32 bits
  41. * pc 16 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 I2C_INTERRUPT_BITS 0x300 /* i2c0_done and i2c1_done bits */
  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. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  61. static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
  62. unsigned long arg);
  63. #endif
  64. static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  65. static ssize_t gpio_write(struct file *file, const char __user *buf,
  66. size_t count, loff_t *off);
  67. static int gpio_open(struct inode *inode, struct file *filp);
  68. static int gpio_release(struct inode *inode, struct file *filp);
  69. static unsigned int gpio_poll(struct file *filp,
  70. struct poll_table_struct *wait);
  71. /* private data per open() of this driver */
  72. struct gpio_private {
  73. struct gpio_private *next;
  74. /* The IO_CFG_WRITE_MODE_VALUE only support 8 bits: */
  75. unsigned char clk_mask;
  76. unsigned char data_mask;
  77. unsigned char write_msb;
  78. unsigned char pad1;
  79. /* These fields are generic */
  80. unsigned long highalarm, lowalarm;
  81. wait_queue_head_t alarm_wq;
  82. int minor;
  83. };
  84. static void gpio_set_alarm(struct gpio_private *priv);
  85. static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
  86. static int gpio_pwm_ioctl(struct gpio_private *priv, unsigned int cmd,
  87. unsigned long arg);
  88. /* linked list of alarms to check for */
  89. static struct gpio_private *alarmlist;
  90. static int wanted_interrupts;
  91. static DEFINE_SPINLOCK(gpio_lock);
  92. #define NUM_PORTS (GPIO_MINOR_LAST+1)
  93. #define GIO_REG_RD_ADDR(reg) \
  94. (unsigned long *)(regi_gio + REG_RD_ADDR_gio_##reg)
  95. #define GIO_REG_WR_ADDR(reg) \
  96. (unsigned long *)(regi_gio + REG_WR_ADDR_gio_##reg)
  97. static unsigned long led_dummy;
  98. static unsigned long port_d_dummy; /* Only input on Artpec-3 */
  99. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  100. static unsigned long port_e_dummy; /* Non existent on Artpec-3 */
  101. static unsigned long virtual_dummy;
  102. static unsigned long virtual_rw_pv_oe = CONFIG_ETRAX_DEF_GIO_PV_OE;
  103. static unsigned short cached_virtual_gpio_read;
  104. #endif
  105. static unsigned long *data_out[NUM_PORTS] = {
  106. GIO_REG_WR_ADDR(rw_pa_dout),
  107. GIO_REG_WR_ADDR(rw_pb_dout),
  108. &led_dummy,
  109. GIO_REG_WR_ADDR(rw_pc_dout),
  110. &port_d_dummy,
  111. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  112. &port_e_dummy,
  113. &virtual_dummy,
  114. #endif
  115. };
  116. static unsigned long *data_in[NUM_PORTS] = {
  117. GIO_REG_RD_ADDR(r_pa_din),
  118. GIO_REG_RD_ADDR(r_pb_din),
  119. &led_dummy,
  120. GIO_REG_RD_ADDR(r_pc_din),
  121. GIO_REG_RD_ADDR(r_pd_din),
  122. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  123. &port_e_dummy,
  124. &virtual_dummy,
  125. #endif
  126. };
  127. static unsigned long changeable_dir[NUM_PORTS] = {
  128. CONFIG_ETRAX_PA_CHANGEABLE_DIR,
  129. CONFIG_ETRAX_PB_CHANGEABLE_DIR,
  130. 0,
  131. CONFIG_ETRAX_PC_CHANGEABLE_DIR,
  132. 0,
  133. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  134. 0,
  135. CONFIG_ETRAX_PV_CHANGEABLE_DIR,
  136. #endif
  137. };
  138. static unsigned long changeable_bits[NUM_PORTS] = {
  139. CONFIG_ETRAX_PA_CHANGEABLE_BITS,
  140. CONFIG_ETRAX_PB_CHANGEABLE_BITS,
  141. 0,
  142. CONFIG_ETRAX_PC_CHANGEABLE_BITS,
  143. 0,
  144. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  145. 0,
  146. CONFIG_ETRAX_PV_CHANGEABLE_BITS,
  147. #endif
  148. };
  149. static unsigned long *dir_oe[NUM_PORTS] = {
  150. GIO_REG_WR_ADDR(rw_pa_oe),
  151. GIO_REG_WR_ADDR(rw_pb_oe),
  152. &led_dummy,
  153. GIO_REG_WR_ADDR(rw_pc_oe),
  154. &port_d_dummy,
  155. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  156. &port_e_dummy,
  157. &virtual_rw_pv_oe,
  158. #endif
  159. };
  160. static void gpio_set_alarm(struct gpio_private *priv)
  161. {
  162. int bit;
  163. int intr_cfg;
  164. int mask;
  165. int pins;
  166. unsigned long flags;
  167. spin_lock_irqsave(&gpio_lock, flags);
  168. intr_cfg = REG_RD_INT(gio, regi_gio, rw_intr_cfg);
  169. pins = REG_RD_INT(gio, regi_gio, rw_intr_pins);
  170. mask = REG_RD_INT(gio, regi_gio, rw_intr_mask) & I2C_INTERRUPT_BITS;
  171. for (bit = 0; bit < 32; bit++) {
  172. int intr = bit % 8;
  173. int pin = bit / 8;
  174. if (priv->minor < GPIO_MINOR_LEDS)
  175. pin += priv->minor * 4;
  176. else
  177. pin += (priv->minor - 1) * 4;
  178. if (priv->highalarm & (1<<bit)) {
  179. intr_cfg |= (regk_gio_hi << (intr * 3));
  180. mask |= 1 << intr;
  181. wanted_interrupts = mask & 0xff;
  182. pins |= pin << (intr * 4);
  183. } else if (priv->lowalarm & (1<<bit)) {
  184. intr_cfg |= (regk_gio_lo << (intr * 3));
  185. mask |= 1 << intr;
  186. wanted_interrupts = mask & 0xff;
  187. pins |= pin << (intr * 4);
  188. }
  189. }
  190. REG_WR_INT(gio, regi_gio, rw_intr_cfg, intr_cfg);
  191. REG_WR_INT(gio, regi_gio, rw_intr_pins, pins);
  192. REG_WR_INT(gio, regi_gio, rw_intr_mask, mask);
  193. spin_unlock_irqrestore(&gpio_lock, flags);
  194. }
  195. static unsigned int gpio_poll(struct file *file, struct poll_table_struct *wait)
  196. {
  197. unsigned int mask = 0;
  198. struct gpio_private *priv = file->private_data;
  199. unsigned long data;
  200. unsigned long tmp;
  201. if (priv->minor >= GPIO_MINOR_PWM0 &&
  202. priv->minor <= GPIO_MINOR_LAST_PWM)
  203. return 0;
  204. poll_wait(file, &priv->alarm_wq, wait);
  205. if (priv->minor <= GPIO_MINOR_D) {
  206. data = readl(data_in[priv->minor]);
  207. REG_WR_INT(gio, regi_gio, rw_ack_intr, wanted_interrupts);
  208. tmp = REG_RD_INT(gio, regi_gio, rw_intr_mask);
  209. tmp &= I2C_INTERRUPT_BITS;
  210. tmp |= wanted_interrupts;
  211. REG_WR_INT(gio, regi_gio, rw_intr_mask, tmp);
  212. } else
  213. return 0;
  214. if ((data & priv->highalarm) || (~data & priv->lowalarm))
  215. mask = POLLIN|POLLRDNORM;
  216. DP(printk(KERN_DEBUG "gpio_poll ready: mask 0x%08X\n", mask));
  217. return mask;
  218. }
  219. static irqreturn_t gpio_interrupt(int irq, void *dev_id)
  220. {
  221. reg_gio_rw_intr_mask intr_mask;
  222. reg_gio_r_masked_intr masked_intr;
  223. reg_gio_rw_ack_intr ack_intr;
  224. unsigned long flags;
  225. unsigned long tmp;
  226. unsigned long tmp2;
  227. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  228. unsigned char enable_gpiov_ack = 0;
  229. #endif
  230. /* Find what PA interrupts are active */
  231. masked_intr = REG_RD(gio, regi_gio, r_masked_intr);
  232. tmp = REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr, masked_intr);
  233. /* Find those that we have enabled */
  234. spin_lock_irqsave(&gpio_lock, flags);
  235. tmp &= wanted_interrupts;
  236. spin_unlock_irqrestore(&gpio_lock, flags);
  237. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  238. /* Something changed on virtual GPIO. Interrupt is acked by
  239. * reading the device.
  240. */
  241. if (tmp & (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN)) {
  242. i2c_read(VIRT_I2C_ADDR, (void *)&cached_virtual_gpio_read,
  243. sizeof(cached_virtual_gpio_read));
  244. enable_gpiov_ack = 1;
  245. }
  246. #endif
  247. /* Ack them */
  248. ack_intr = REG_TYPE_CONV(reg_gio_rw_ack_intr, unsigned long, tmp);
  249. REG_WR(gio, regi_gio, rw_ack_intr, ack_intr);
  250. /* Disable those interrupts.. */
  251. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  252. tmp2 = REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask, intr_mask);
  253. tmp2 &= ~tmp;
  254. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  255. /* Do not disable interrupt on virtual GPIO. Changes on virtual
  256. * pins are only noticed by an interrupt.
  257. */
  258. if (enable_gpiov_ack)
  259. tmp2 |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  260. #endif
  261. intr_mask = REG_TYPE_CONV(reg_gio_rw_intr_mask, unsigned long, tmp2);
  262. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  263. return IRQ_RETVAL(tmp);
  264. }
  265. static void gpio_write_bit(unsigned long *port, unsigned char data, int bit,
  266. unsigned char clk_mask, unsigned char data_mask)
  267. {
  268. unsigned long shadow = readl(port) & ~clk_mask;
  269. writel(shadow, port);
  270. if (data & 1 << bit)
  271. shadow |= data_mask;
  272. else
  273. shadow &= ~data_mask;
  274. writel(shadow, port);
  275. /* For FPGA: min 5.0ns (DCC) before CCLK high */
  276. shadow |= clk_mask;
  277. writel(shadow, port);
  278. }
  279. static void gpio_write_byte(struct gpio_private *priv, unsigned long *port,
  280. unsigned char data)
  281. {
  282. int i;
  283. if (priv->write_msb)
  284. for (i = 7; i >= 0; i--)
  285. gpio_write_bit(port, data, i, priv->clk_mask,
  286. priv->data_mask);
  287. else
  288. for (i = 0; i <= 7; i++)
  289. gpio_write_bit(port, data, i, priv->clk_mask,
  290. priv->data_mask);
  291. }
  292. static ssize_t gpio_write(struct file *file, const char __user *buf,
  293. size_t count, loff_t *off)
  294. {
  295. struct gpio_private *priv = file->private_data;
  296. unsigned long flags;
  297. ssize_t retval = count;
  298. /* Only bits 0-7 may be used for write operations but allow all
  299. devices except leds... */
  300. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  301. if (priv->minor == GPIO_MINOR_V)
  302. return -EFAULT;
  303. #endif
  304. if (priv->minor == GPIO_MINOR_LEDS)
  305. return -EFAULT;
  306. if (priv->minor >= GPIO_MINOR_PWM0 &&
  307. priv->minor <= GPIO_MINOR_LAST_PWM)
  308. return -EFAULT;
  309. if (!access_ok(VERIFY_READ, buf, count))
  310. return -EFAULT;
  311. /* It must have been configured using the IO_CFG_WRITE_MODE */
  312. /* Perhaps a better error code? */
  313. if (priv->clk_mask == 0 || priv->data_mask == 0)
  314. return -EPERM;
  315. D(printk(KERN_DEBUG "gpio_write: %lu to data 0x%02X clk 0x%02X "
  316. "msb: %i\n",
  317. count, priv->data_mask, priv->clk_mask, priv->write_msb));
  318. spin_lock_irqsave(&gpio_lock, flags);
  319. while (count--)
  320. gpio_write_byte(priv, data_out[priv->minor], *buf++);
  321. spin_unlock_irqrestore(&gpio_lock, flags);
  322. return retval;
  323. }
  324. static int gpio_open(struct inode *inode, struct file *filp)
  325. {
  326. struct gpio_private *priv;
  327. int p = iminor(inode);
  328. if (p > GPIO_MINOR_LAST_PWM ||
  329. (p > GPIO_MINOR_LAST && p < GPIO_MINOR_PWM0))
  330. return -EINVAL;
  331. priv = kmalloc(sizeof(struct gpio_private), GFP_KERNEL);
  332. if (!priv)
  333. return -ENOMEM;
  334. mutex_lock(&gpio_mutex);
  335. memset(priv, 0, sizeof(*priv));
  336. priv->minor = p;
  337. filp->private_data = priv;
  338. /* initialize the io/alarm struct, not for PWM ports though */
  339. if (p <= GPIO_MINOR_LAST) {
  340. priv->clk_mask = 0;
  341. priv->data_mask = 0;
  342. priv->highalarm = 0;
  343. priv->lowalarm = 0;
  344. init_waitqueue_head(&priv->alarm_wq);
  345. /* link it into our alarmlist */
  346. spin_lock_irq(&gpio_lock);
  347. priv->next = alarmlist;
  348. alarmlist = priv;
  349. spin_unlock_irq(&gpio_lock);
  350. }
  351. mutex_unlock(&gpio_mutex);
  352. return 0;
  353. }
  354. static int gpio_release(struct inode *inode, struct file *filp)
  355. {
  356. struct gpio_private *p;
  357. struct gpio_private *todel;
  358. /* local copies while updating them: */
  359. unsigned long a_high, a_low;
  360. /* prepare to free private structure */
  361. todel = filp->private_data;
  362. /* unlink from alarmlist - only for non-PWM ports though */
  363. if (todel->minor <= GPIO_MINOR_LAST) {
  364. spin_lock_irq(&gpio_lock);
  365. p = alarmlist;
  366. if (p == todel)
  367. alarmlist = todel->next;
  368. else {
  369. while (p->next != todel)
  370. p = p->next;
  371. p->next = todel->next;
  372. }
  373. /* Check if there are still any alarms set */
  374. p = alarmlist;
  375. a_high = 0;
  376. a_low = 0;
  377. while (p) {
  378. if (p->minor == GPIO_MINOR_A) {
  379. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  380. p->lowalarm |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  381. #endif
  382. a_high |= p->highalarm;
  383. a_low |= p->lowalarm;
  384. }
  385. p = p->next;
  386. }
  387. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  388. /* Variable 'a_low' needs to be set here again
  389. * to ensure that interrupt for virtual GPIO is handled.
  390. */
  391. a_low |= (1 << CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN);
  392. #endif
  393. spin_unlock_irq(&gpio_lock);
  394. }
  395. kfree(todel);
  396. return 0;
  397. }
  398. /* Main device API. ioctl's to read/set/clear bits, as well as to
  399. * set alarms to wait for using a subsequent select().
  400. */
  401. inline unsigned long setget_input(struct gpio_private *priv, unsigned long arg)
  402. {
  403. /* Set direction 0=unchanged 1=input,
  404. * return mask with 1=input
  405. */
  406. unsigned long flags;
  407. unsigned long dir_shadow;
  408. spin_lock_irqsave(&gpio_lock, flags);
  409. dir_shadow = readl(dir_oe[priv->minor]) &
  410. ~(arg & changeable_dir[priv->minor]);
  411. writel(dir_shadow, dir_oe[priv->minor]);
  412. spin_unlock_irqrestore(&gpio_lock, flags);
  413. if (priv->minor == GPIO_MINOR_C)
  414. dir_shadow ^= 0xFFFF; /* Only 16 bits */
  415. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  416. else if (priv->minor == GPIO_MINOR_V)
  417. dir_shadow ^= 0xFFFF; /* Only 16 bits */
  418. #endif
  419. else
  420. dir_shadow ^= 0xFFFFFFFF; /* PA, PB and PD 32 bits */
  421. return dir_shadow;
  422. } /* setget_input */
  423. static inline unsigned long setget_output(struct gpio_private *priv,
  424. unsigned long arg)
  425. {
  426. unsigned long flags;
  427. unsigned long dir_shadow;
  428. spin_lock_irqsave(&gpio_lock, flags);
  429. dir_shadow = readl(dir_oe[priv->minor]) |
  430. (arg & changeable_dir[priv->minor]);
  431. writel(dir_shadow, dir_oe[priv->minor]);
  432. spin_unlock_irqrestore(&gpio_lock, flags);
  433. return dir_shadow;
  434. } /* setget_output */
  435. static long gpio_ioctl_unlocked(struct file *file,
  436. unsigned int cmd, unsigned long arg)
  437. {
  438. unsigned long flags;
  439. unsigned long val;
  440. unsigned long shadow;
  441. struct gpio_private *priv = file->private_data;
  442. if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE)
  443. return -ENOTTY;
  444. /* Check for special ioctl handlers first */
  445. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  446. if (priv->minor == GPIO_MINOR_V)
  447. return virtual_gpio_ioctl(file, cmd, arg);
  448. #endif
  449. if (priv->minor == GPIO_MINOR_LEDS)
  450. return gpio_leds_ioctl(cmd, arg);
  451. if (priv->minor >= GPIO_MINOR_PWM0 &&
  452. priv->minor <= GPIO_MINOR_LAST_PWM)
  453. return gpio_pwm_ioctl(priv, cmd, arg);
  454. switch (_IOC_NR(cmd)) {
  455. case IO_READBITS: /* Use IO_READ_INBITS and IO_READ_OUTBITS instead */
  456. /* Read the port. */
  457. return readl(data_in[priv->minor]);
  458. case IO_SETBITS:
  459. spin_lock_irqsave(&gpio_lock, flags);
  460. /* Set changeable bits with a 1 in arg. */
  461. shadow = readl(data_out[priv->minor]) |
  462. (arg & changeable_bits[priv->minor]);
  463. writel(shadow, data_out[priv->minor]);
  464. spin_unlock_irqrestore(&gpio_lock, flags);
  465. break;
  466. case IO_CLRBITS:
  467. spin_lock_irqsave(&gpio_lock, flags);
  468. /* Clear changeable bits with a 1 in arg. */
  469. shadow = readl(data_out[priv->minor]) &
  470. ~(arg & changeable_bits[priv->minor]);
  471. writel(shadow, data_out[priv->minor]);
  472. spin_unlock_irqrestore(&gpio_lock, flags);
  473. break;
  474. case IO_HIGHALARM:
  475. /* Set alarm when bits with 1 in arg go high. */
  476. priv->highalarm |= arg;
  477. gpio_set_alarm(priv);
  478. break;
  479. case IO_LOWALARM:
  480. /* Set alarm when bits with 1 in arg go low. */
  481. priv->lowalarm |= arg;
  482. gpio_set_alarm(priv);
  483. break;
  484. case IO_CLRALARM:
  485. /* Clear alarm for bits with 1 in arg. */
  486. priv->highalarm &= ~arg;
  487. priv->lowalarm &= ~arg;
  488. gpio_set_alarm(priv);
  489. break;
  490. case IO_READDIR: /* Use IO_SETGET_INPUT/OUTPUT instead! */
  491. /* Read direction 0=input 1=output */
  492. return readl(dir_oe[priv->minor]);
  493. case IO_SETINPUT: /* Use IO_SETGET_INPUT instead! */
  494. /* Set direction 0=unchanged 1=input,
  495. * return mask with 1=input
  496. */
  497. return setget_input(priv, arg);
  498. case IO_SETOUTPUT: /* Use IO_SETGET_OUTPUT instead! */
  499. /* Set direction 0=unchanged 1=output,
  500. * return mask with 1=output
  501. */
  502. return setget_output(priv, arg);
  503. case IO_CFG_WRITE_MODE:
  504. {
  505. int res = -EPERM;
  506. unsigned long dir_shadow, clk_mask, data_mask, write_msb;
  507. clk_mask = arg & 0xFF;
  508. data_mask = (arg >> 8) & 0xFF;
  509. write_msb = (arg >> 16) & 0x01;
  510. /* Check if we're allowed to change the bits and
  511. * the direction is correct
  512. */
  513. spin_lock_irqsave(&gpio_lock, flags);
  514. dir_shadow = readl(dir_oe[priv->minor]);
  515. if ((clk_mask & changeable_bits[priv->minor]) &&
  516. (data_mask & changeable_bits[priv->minor]) &&
  517. (clk_mask & dir_shadow) &&
  518. (data_mask & dir_shadow)) {
  519. priv->clk_mask = clk_mask;
  520. priv->data_mask = data_mask;
  521. priv->write_msb = write_msb;
  522. res = 0;
  523. }
  524. spin_unlock_irqrestore(&gpio_lock, flags);
  525. return res;
  526. }
  527. case IO_READ_INBITS:
  528. /* *arg is result of reading the input pins */
  529. val = readl(data_in[priv->minor]);
  530. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  531. return -EFAULT;
  532. return 0;
  533. case IO_READ_OUTBITS:
  534. /* *arg is result of reading the output shadow */
  535. val = *data_out[priv->minor];
  536. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  537. return -EFAULT;
  538. break;
  539. case IO_SETGET_INPUT:
  540. /* bits set in *arg is set to input,
  541. * *arg updated with current input pins.
  542. */
  543. if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
  544. return -EFAULT;
  545. val = setget_input(priv, val);
  546. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  547. return -EFAULT;
  548. break;
  549. case IO_SETGET_OUTPUT:
  550. /* bits set in *arg is set to output,
  551. * *arg updated with current output pins.
  552. */
  553. if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
  554. return -EFAULT;
  555. val = setget_output(priv, val);
  556. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  557. return -EFAULT;
  558. break;
  559. default:
  560. return -EINVAL;
  561. } /* switch */
  562. return 0;
  563. }
  564. static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  565. {
  566. long ret;
  567. mutex_lock(&gpio_mutex);
  568. ret = gpio_ioctl_unlocked(file, cmd, arg);
  569. mutex_unlock(&gpio_mutex);
  570. return ret;
  571. }
  572. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  573. static int virtual_gpio_ioctl(struct file *file, unsigned int cmd,
  574. unsigned long arg)
  575. {
  576. unsigned long flags;
  577. unsigned short val;
  578. unsigned short shadow;
  579. struct gpio_private *priv = file->private_data;
  580. switch (_IOC_NR(cmd)) {
  581. case IO_SETBITS:
  582. spin_lock_irqsave(&gpio_lock, flags);
  583. /* Set changeable bits with a 1 in arg. */
  584. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  585. shadow |= ~readl(dir_oe[priv->minor]) |
  586. (arg & changeable_bits[priv->minor]);
  587. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  588. spin_unlock_irqrestore(&gpio_lock, flags);
  589. break;
  590. case IO_CLRBITS:
  591. spin_lock_irqsave(&gpio_lock, flags);
  592. /* Clear changeable bits with a 1 in arg. */
  593. i2c_read(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  594. shadow |= ~readl(dir_oe[priv->minor]) &
  595. ~(arg & changeable_bits[priv->minor]);
  596. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  597. spin_unlock_irqrestore(&gpio_lock, flags);
  598. break;
  599. case IO_HIGHALARM:
  600. /* Set alarm when bits with 1 in arg go high. */
  601. priv->highalarm |= arg;
  602. break;
  603. case IO_LOWALARM:
  604. /* Set alarm when bits with 1 in arg go low. */
  605. priv->lowalarm |= arg;
  606. break;
  607. case IO_CLRALARM:
  608. /* Clear alarm for bits with 1 in arg. */
  609. priv->highalarm &= ~arg;
  610. priv->lowalarm &= ~arg;
  611. break;
  612. case IO_CFG_WRITE_MODE:
  613. {
  614. unsigned long dir_shadow;
  615. dir_shadow = readl(dir_oe[priv->minor]);
  616. priv->clk_mask = arg & 0xFF;
  617. priv->data_mask = (arg >> 8) & 0xFF;
  618. priv->write_msb = (arg >> 16) & 0x01;
  619. /* Check if we're allowed to change the bits and
  620. * the direction is correct
  621. */
  622. if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
  623. (priv->data_mask & changeable_bits[priv->minor]) &&
  624. (priv->clk_mask & dir_shadow) &&
  625. (priv->data_mask & dir_shadow))) {
  626. priv->clk_mask = 0;
  627. priv->data_mask = 0;
  628. return -EPERM;
  629. }
  630. break;
  631. }
  632. case IO_READ_INBITS:
  633. /* *arg is result of reading the input pins */
  634. val = cached_virtual_gpio_read & ~readl(dir_oe[priv->minor]);
  635. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  636. return -EFAULT;
  637. return 0;
  638. case IO_READ_OUTBITS:
  639. /* *arg is result of reading the output shadow */
  640. i2c_read(VIRT_I2C_ADDR, (void *)&val, sizeof(val));
  641. val &= readl(dir_oe[priv->minor]);
  642. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  643. return -EFAULT;
  644. break;
  645. case IO_SETGET_INPUT:
  646. {
  647. /* bits set in *arg is set to input,
  648. * *arg updated with current input pins.
  649. */
  650. unsigned short input_mask = ~readl(dir_oe[priv->minor]);
  651. if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
  652. return -EFAULT;
  653. val = setget_input(priv, val);
  654. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  655. return -EFAULT;
  656. if ((input_mask & val) != input_mask) {
  657. /* Input pins changed. All ports desired as input
  658. * should be set to logic 1.
  659. */
  660. unsigned short change = input_mask ^ val;
  661. i2c_read(VIRT_I2C_ADDR, (void *)&shadow,
  662. sizeof(shadow));
  663. shadow &= ~change;
  664. shadow |= val;
  665. i2c_write(VIRT_I2C_ADDR, (void *)&shadow,
  666. sizeof(shadow));
  667. }
  668. break;
  669. }
  670. case IO_SETGET_OUTPUT:
  671. /* bits set in *arg is set to output,
  672. * *arg updated with current output pins.
  673. */
  674. if (copy_from_user(&val, (void __user *)arg, sizeof(val)))
  675. return -EFAULT;
  676. val = setget_output(priv, val);
  677. if (copy_to_user((void __user *)arg, &val, sizeof(val)))
  678. return -EFAULT;
  679. break;
  680. default:
  681. return -EINVAL;
  682. } /* switch */
  683. return 0;
  684. }
  685. #endif /* CONFIG_ETRAX_VIRTUAL_GPIO */
  686. static int gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
  687. {
  688. unsigned char green;
  689. unsigned char red;
  690. switch (_IOC_NR(cmd)) {
  691. case IO_LEDACTIVE_SET:
  692. green = ((unsigned char) arg) & 1;
  693. red = (((unsigned char) arg) >> 1) & 1;
  694. CRIS_LED_ACTIVE_SET_G(green);
  695. CRIS_LED_ACTIVE_SET_R(red);
  696. break;
  697. default:
  698. return -EINVAL;
  699. } /* switch */
  700. return 0;
  701. }
  702. static int gpio_pwm_set_mode(unsigned long arg, int pwm_port)
  703. {
  704. int pinmux_pwm = pinmux_pwm0 + pwm_port;
  705. int mode;
  706. reg_gio_rw_pwm0_ctrl rw_pwm_ctrl = {
  707. .ccd_val = 0,
  708. .ccd_override = regk_gio_no,
  709. .mode = regk_gio_no
  710. };
  711. int allocstatus;
  712. if (get_user(mode, &((struct io_pwm_set_mode *) arg)->mode))
  713. return -EFAULT;
  714. rw_pwm_ctrl.mode = mode;
  715. if (mode != PWM_OFF)
  716. allocstatus = crisv32_pinmux_alloc_fixed(pinmux_pwm);
  717. else
  718. allocstatus = crisv32_pinmux_dealloc_fixed(pinmux_pwm);
  719. if (allocstatus)
  720. return allocstatus;
  721. REG_WRITE(reg_gio_rw_pwm0_ctrl, REG_ADDR(gio, regi_gio, rw_pwm0_ctrl) +
  722. 12 * pwm_port, rw_pwm_ctrl);
  723. return 0;
  724. }
  725. static int gpio_pwm_set_period(unsigned long arg, int pwm_port)
  726. {
  727. struct io_pwm_set_period periods;
  728. reg_gio_rw_pwm0_var rw_pwm_widths;
  729. if (copy_from_user(&periods, (void __user *)arg, sizeof(periods)))
  730. return -EFAULT;
  731. if (periods.lo > 8191 || periods.hi > 8191)
  732. return -EINVAL;
  733. rw_pwm_widths.lo = periods.lo;
  734. rw_pwm_widths.hi = periods.hi;
  735. REG_WRITE(reg_gio_rw_pwm0_var, REG_ADDR(gio, regi_gio, rw_pwm0_var) +
  736. 12 * pwm_port, rw_pwm_widths);
  737. return 0;
  738. }
  739. static int gpio_pwm_set_duty(unsigned long arg, int pwm_port)
  740. {
  741. unsigned int duty;
  742. reg_gio_rw_pwm0_data rw_pwm_duty;
  743. if (get_user(duty, &((struct io_pwm_set_duty *) arg)->duty))
  744. return -EFAULT;
  745. if (duty > 255)
  746. return -EINVAL;
  747. rw_pwm_duty.data = duty;
  748. REG_WRITE(reg_gio_rw_pwm0_data, REG_ADDR(gio, regi_gio, rw_pwm0_data) +
  749. 12 * pwm_port, rw_pwm_duty);
  750. return 0;
  751. }
  752. static int gpio_pwm_ioctl(struct gpio_private *priv, unsigned int cmd,
  753. unsigned long arg)
  754. {
  755. int pwm_port = priv->minor - GPIO_MINOR_PWM0;
  756. switch (_IOC_NR(cmd)) {
  757. case IO_PWM_SET_MODE:
  758. return gpio_pwm_set_mode(arg, pwm_port);
  759. case IO_PWM_SET_PERIOD:
  760. return gpio_pwm_set_period(arg, pwm_port);
  761. case IO_PWM_SET_DUTY:
  762. return gpio_pwm_set_duty(arg, pwm_port);
  763. default:
  764. return -EINVAL;
  765. }
  766. return 0;
  767. }
  768. static const struct file_operations gpio_fops = {
  769. .owner = THIS_MODULE,
  770. .poll = gpio_poll,
  771. .unlocked_ioctl = gpio_ioctl,
  772. .write = gpio_write,
  773. .open = gpio_open,
  774. .release = gpio_release,
  775. .llseek = noop_llseek,
  776. };
  777. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  778. static void __init 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. }
  828. #endif
  829. /* main driver initialization routine, called from mem.c */
  830. static int __init gpio_init(void)
  831. {
  832. int res;
  833. printk(KERN_INFO "ETRAX FS GPIO driver v2.7, (c) 2003-2008 "
  834. "Axis Communications AB\n");
  835. /* do the formalities */
  836. res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
  837. if (res < 0) {
  838. printk(KERN_ERR "gpio: couldn't get a major number.\n");
  839. return res;
  840. }
  841. /* Clear all leds */
  842. CRIS_LED_NETWORK_GRP0_SET(0);
  843. CRIS_LED_NETWORK_GRP1_SET(0);
  844. CRIS_LED_ACTIVE_SET(0);
  845. CRIS_LED_DISK_READ(0);
  846. CRIS_LED_DISK_WRITE(0);
  847. int res2 = request_irq(GIO_INTR_VECT, gpio_interrupt,
  848. IRQF_SHARED | IRQF_DISABLED, "gpio", &alarmlist);
  849. if (res2) {
  850. printk(KERN_ERR "err: irq for gpio\n");
  851. return res2;
  852. }
  853. /* No IRQs by default. */
  854. REG_WR_INT(gio, regi_gio, rw_intr_pins, 0);
  855. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  856. virtual_gpio_init();
  857. #endif
  858. return res;
  859. }
  860. /* this makes sure that gpio_init is called during kernel boot */
  861. module_init(gpio_init);