gpio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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/smp_lock.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/system.h>
  33. #include <asm/irq.h>
  34. #include <mach/pinmux.h>
  35. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  36. #include "../i2c.h"
  37. #define VIRT_I2C_ADDR 0x40
  38. #endif
  39. /* The following gio ports on ARTPEC-3 is available:
  40. * pa 32 bits
  41. * pb 32 bits
  42. * pc 16 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 I2C_INTERRUPT_BITS 0x300 /* i2c0_done and i2c1_done bits */
  47. #define D(x)
  48. #if 0
  49. static int dp_cnt;
  50. #define DP(x) \
  51. do { \
  52. dp_cnt++; \
  53. if (dp_cnt % 1000 == 0) \
  54. x; \
  55. } while (0)
  56. #else
  57. #define DP(x)
  58. #endif
  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. lock_kernel();
  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. unlock_kernel();
  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. lock_kernel();
  568. ret = gpio_ioctl_unlocked(file, cmd, arg);
  569. unlock_kernel();
  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. };
  776. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  777. static void __init virtual_gpio_init(void)
  778. {
  779. reg_gio_rw_intr_cfg intr_cfg;
  780. reg_gio_rw_intr_mask intr_mask;
  781. unsigned short shadow;
  782. shadow = ~virtual_rw_pv_oe; /* Input ports should be set to logic 1 */
  783. shadow |= CONFIG_ETRAX_DEF_GIO_PV_OUT;
  784. i2c_write(VIRT_I2C_ADDR, (void *)&shadow, sizeof(shadow));
  785. /* Set interrupt mask and on what state the interrupt shall trigger.
  786. * For virtual gpio the interrupt shall trigger on logic '0'.
  787. */
  788. intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
  789. intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
  790. switch (CONFIG_ETRAX_VIRTUAL_GPIO_INTERRUPT_PA_PIN) {
  791. case 0:
  792. intr_cfg.pa0 = regk_gio_lo;
  793. intr_mask.pa0 = regk_gio_yes;
  794. break;
  795. case 1:
  796. intr_cfg.pa1 = regk_gio_lo;
  797. intr_mask.pa1 = regk_gio_yes;
  798. break;
  799. case 2:
  800. intr_cfg.pa2 = regk_gio_lo;
  801. intr_mask.pa2 = regk_gio_yes;
  802. break;
  803. case 3:
  804. intr_cfg.pa3 = regk_gio_lo;
  805. intr_mask.pa3 = regk_gio_yes;
  806. break;
  807. case 4:
  808. intr_cfg.pa4 = regk_gio_lo;
  809. intr_mask.pa4 = regk_gio_yes;
  810. break;
  811. case 5:
  812. intr_cfg.pa5 = regk_gio_lo;
  813. intr_mask.pa5 = regk_gio_yes;
  814. break;
  815. case 6:
  816. intr_cfg.pa6 = regk_gio_lo;
  817. intr_mask.pa6 = regk_gio_yes;
  818. break;
  819. case 7:
  820. intr_cfg.pa7 = regk_gio_lo;
  821. intr_mask.pa7 = regk_gio_yes;
  822. break;
  823. }
  824. REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
  825. REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
  826. }
  827. #endif
  828. /* main driver initialization routine, called from mem.c */
  829. static int __init gpio_init(void)
  830. {
  831. int res;
  832. printk(KERN_INFO "ETRAX FS GPIO driver v2.7, (c) 2003-2008 "
  833. "Axis Communications AB\n");
  834. /* do the formalities */
  835. res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
  836. if (res < 0) {
  837. printk(KERN_ERR "gpio: couldn't get a major number.\n");
  838. return res;
  839. }
  840. /* Clear all leds */
  841. CRIS_LED_NETWORK_GRP0_SET(0);
  842. CRIS_LED_NETWORK_GRP1_SET(0);
  843. CRIS_LED_ACTIVE_SET(0);
  844. CRIS_LED_DISK_READ(0);
  845. CRIS_LED_DISK_WRITE(0);
  846. int res2 = request_irq(GIO_INTR_VECT, gpio_interrupt,
  847. IRQF_SHARED | IRQF_DISABLED, "gpio", &alarmlist);
  848. if (res2) {
  849. printk(KERN_ERR "err: irq for gpio\n");
  850. return res2;
  851. }
  852. /* No IRQs by default. */
  853. REG_WR_INT(gio, regi_gio, rw_intr_pins, 0);
  854. #ifdef CONFIG_ETRAX_VIRTUAL_GPIO
  855. virtual_gpio_init();
  856. #endif
  857. return res;
  858. }
  859. /* this makes sure that gpio_init is called during kernel boot */
  860. module_init(gpio_init);