gpio.c 25 KB

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