gpio-u300.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * U300 GPIO module.
  3. *
  4. * Copyright (C) 2007-2011 ST-Ericsson AB
  5. * License terms: GNU General Public License (GPL) version 2
  6. * This can driver either of the two basic GPIO cores
  7. * available in the U300 platforms:
  8. * COH 901 335 - Used in DB3150 (U300 1.0) and DB3200 (U330 1.0)
  9. * COH 901 571/3 - Used in DB3210 (U365 2.0) and DB3350 (U335 1.0)
  10. * Author: Linus Walleij <linus.walleij@linaro.org>
  11. * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/io.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/gpio.h>
  23. #include <linux/list.h>
  24. #include <linux/slab.h>
  25. #include <mach/gpio-u300.h>
  26. /*
  27. * Bias modes for U300 GPIOs
  28. *
  29. * GPIO_U300_CONFIG_BIAS_UNKNOWN: this bias mode is not known to us
  30. * GPIO_U300_CONFIG_BIAS_FLOAT: no specific bias, the GPIO will float or state
  31. * is not controlled by software
  32. * GPIO_U300_CONFIG_BIAS_PULL_UP: the GPIO will be pulled up (usually with high
  33. * impedance to VDD)
  34. */
  35. #define GPIO_U300_CONFIG_BIAS_UNKNOWN 0x1000
  36. #define GPIO_U300_CONFIG_BIAS_FLOAT 0x1001
  37. #define GPIO_U300_CONFIG_BIAS_PULL_UP 0x1002
  38. /*
  39. * Drive modes for U300 GPIOs (output)
  40. *
  41. * GPIO_U300_CONFIG_DRIVE_PUSH_PULL: the GPIO will be driven actively high and
  42. * low, this is the most typical case and is typically achieved with two
  43. * active transistors on the output
  44. * GPIO_U300_CONFIG_DRIVE_OPEN_DRAIN: the GPIO will be driven with open drain
  45. * (open collector) which means it is usually wired with other output
  46. * ports which are then pulled up with an external resistor
  47. * GPIO_U300_CONFIG_DRIVE_OPEN_SOURCE: the GPIO will be driven with open drain
  48. * (open emitter) which is the same as open drain mutatis mutandis but
  49. * pulled to ground
  50. */
  51. #define GPIO_U300_CONFIG_DRIVE_PUSH_PULL 0x2000
  52. #define GPIO_U300_CONFIG_DRIVE_OPEN_DRAIN 0x2001
  53. #define GPIO_U300_CONFIG_DRIVE_OPEN_SOURCE 0x2002
  54. /*
  55. * Register definitions for COH 901 335 variant
  56. */
  57. #define U300_335_PORT_STRIDE (0x1C)
  58. /* Port X Pin Data Register 32bit, this is both input and output (R/W) */
  59. #define U300_335_PXPDIR (0x00)
  60. #define U300_335_PXPDOR (0x00)
  61. /* Port X Pin Config Register 32bit (R/W) */
  62. #define U300_335_PXPCR (0x04)
  63. /* This register layout is the same in both blocks */
  64. #define U300_GPIO_PXPCR_ALL_PINS_MODE_MASK (0x0000FFFFUL)
  65. #define U300_GPIO_PXPCR_PIN_MODE_MASK (0x00000003UL)
  66. #define U300_GPIO_PXPCR_PIN_MODE_SHIFT (0x00000002UL)
  67. #define U300_GPIO_PXPCR_PIN_MODE_INPUT (0x00000000UL)
  68. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL (0x00000001UL)
  69. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN (0x00000002UL)
  70. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE (0x00000003UL)
  71. /* Port X Interrupt Event Register 32bit (R/W) */
  72. #define U300_335_PXIEV (0x08)
  73. /* Port X Interrupt Enable Register 32bit (R/W) */
  74. #define U300_335_PXIEN (0x0C)
  75. /* Port X Interrupt Force Register 32bit (R/W) */
  76. #define U300_335_PXIFR (0x10)
  77. /* Port X Interrupt Config Register 32bit (R/W) */
  78. #define U300_335_PXICR (0x14)
  79. /* This register layout is the same in both blocks */
  80. #define U300_GPIO_PXICR_ALL_IRQ_CONFIG_MASK (0x000000FFUL)
  81. #define U300_GPIO_PXICR_IRQ_CONFIG_MASK (0x00000001UL)
  82. #define U300_GPIO_PXICR_IRQ_CONFIG_FALLING_EDGE (0x00000000UL)
  83. #define U300_GPIO_PXICR_IRQ_CONFIG_RISING_EDGE (0x00000001UL)
  84. /* Port X Pull-up Enable Register 32bit (R/W) */
  85. #define U300_335_PXPER (0x18)
  86. /* This register layout is the same in both blocks */
  87. #define U300_GPIO_PXPER_ALL_PULL_UP_DISABLE_MASK (0x000000FFUL)
  88. #define U300_GPIO_PXPER_PULL_UP_DISABLE (0x00000001UL)
  89. /* Control Register 32bit (R/W) */
  90. #define U300_335_CR (0x54)
  91. #define U300_335_CR_BLOCK_CLOCK_ENABLE (0x00000001UL)
  92. /*
  93. * Register definitions for COH 901 571 / 3 variant
  94. */
  95. #define U300_571_PORT_STRIDE (0x30)
  96. /*
  97. * Control Register 32bit (R/W)
  98. * bit 15-9 (mask 0x0000FE00) contains the number of cores. 8*cores
  99. * gives the number of GPIO pins.
  100. * bit 8-2 (mask 0x000001FC) contains the core version ID.
  101. */
  102. #define U300_571_CR (0x00)
  103. #define U300_571_CR_SYNC_SEL_ENABLE (0x00000002UL)
  104. #define U300_571_CR_BLOCK_CLKRQ_ENABLE (0x00000001UL)
  105. /*
  106. * These registers have the same layout and function as the corresponding
  107. * COH 901 335 registers, just at different offset.
  108. */
  109. #define U300_571_PXPDIR (0x04)
  110. #define U300_571_PXPDOR (0x08)
  111. #define U300_571_PXPCR (0x0C)
  112. #define U300_571_PXPER (0x10)
  113. #define U300_571_PXIEV (0x14)
  114. #define U300_571_PXIEN (0x18)
  115. #define U300_571_PXIFR (0x1C)
  116. #define U300_571_PXICR (0x20)
  117. /* 8 bits per port, no version has more than 7 ports */
  118. #define U300_GPIO_PINS_PER_PORT 8
  119. #define U300_GPIO_MAX (U300_GPIO_PINS_PER_PORT * 7)
  120. struct u300_gpio {
  121. struct gpio_chip chip;
  122. struct list_head port_list;
  123. struct clk *clk;
  124. struct resource *memres;
  125. void __iomem *base;
  126. struct device *dev;
  127. int irq_base;
  128. u32 stride;
  129. /* Register offsets */
  130. u32 pcr;
  131. u32 dor;
  132. u32 dir;
  133. u32 per;
  134. u32 icr;
  135. u32 ien;
  136. u32 iev;
  137. };
  138. struct u300_gpio_port {
  139. struct list_head node;
  140. struct u300_gpio *gpio;
  141. char name[8];
  142. int irq;
  143. int number;
  144. u8 toggle_edge_mode;
  145. };
  146. /*
  147. * Macro to expand to read a specific register found in the "gpio"
  148. * struct. It requires the struct u300_gpio *gpio variable to exist in
  149. * its context. It calculates the port offset from the given pin
  150. * offset, muliplies by the port stride and adds the register offset
  151. * so it provides a pointer to the desired register.
  152. */
  153. #define U300_PIN_REG(pin, reg) \
  154. (gpio->base + (pin >> 3) * gpio->stride + gpio->reg)
  155. /*
  156. * Provides a bitmask for a specific gpio pin inside an 8-bit GPIO
  157. * register.
  158. */
  159. #define U300_PIN_BIT(pin) \
  160. (1 << (pin & 0x07))
  161. struct u300_gpio_confdata {
  162. u16 bias_mode;
  163. bool output;
  164. int outval;
  165. };
  166. /* BS335 has seven ports of 8 bits each = GPIO pins 0..55 */
  167. #define BS335_GPIO_NUM_PORTS 7
  168. /* BS365 has five ports of 8 bits each = GPIO pins 0..39 */
  169. #define BS365_GPIO_NUM_PORTS 5
  170. #define U300_FLOATING_INPUT { \
  171. .bias_mode = GPIO_U300_CONFIG_BIAS_FLOAT, \
  172. .output = false, \
  173. }
  174. #define U300_PULL_UP_INPUT { \
  175. .bias_mode = GPIO_U300_CONFIG_BIAS_PULL_UP, \
  176. .output = false, \
  177. }
  178. #define U300_OUTPUT_LOW { \
  179. .output = true, \
  180. .outval = 0, \
  181. }
  182. #define U300_OUTPUT_HIGH { \
  183. .output = true, \
  184. .outval = 1, \
  185. }
  186. /* Initial configuration */
  187. static const struct __initdata u300_gpio_confdata
  188. bs335_gpio_config[BS335_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
  189. /* Port 0, pins 0-7 */
  190. {
  191. U300_FLOATING_INPUT,
  192. U300_OUTPUT_HIGH,
  193. U300_FLOATING_INPUT,
  194. U300_OUTPUT_LOW,
  195. U300_OUTPUT_LOW,
  196. U300_OUTPUT_LOW,
  197. U300_OUTPUT_LOW,
  198. U300_OUTPUT_LOW,
  199. },
  200. /* Port 1, pins 0-7 */
  201. {
  202. U300_OUTPUT_LOW,
  203. U300_OUTPUT_LOW,
  204. U300_OUTPUT_LOW,
  205. U300_PULL_UP_INPUT,
  206. U300_FLOATING_INPUT,
  207. U300_OUTPUT_HIGH,
  208. U300_OUTPUT_LOW,
  209. U300_OUTPUT_LOW,
  210. },
  211. /* Port 2, pins 0-7 */
  212. {
  213. U300_FLOATING_INPUT,
  214. U300_FLOATING_INPUT,
  215. U300_FLOATING_INPUT,
  216. U300_FLOATING_INPUT,
  217. U300_OUTPUT_LOW,
  218. U300_PULL_UP_INPUT,
  219. U300_OUTPUT_LOW,
  220. U300_PULL_UP_INPUT,
  221. },
  222. /* Port 3, pins 0-7 */
  223. {
  224. U300_PULL_UP_INPUT,
  225. U300_OUTPUT_LOW,
  226. U300_FLOATING_INPUT,
  227. U300_FLOATING_INPUT,
  228. U300_FLOATING_INPUT,
  229. U300_FLOATING_INPUT,
  230. U300_FLOATING_INPUT,
  231. U300_FLOATING_INPUT,
  232. },
  233. /* Port 4, pins 0-7 */
  234. {
  235. U300_FLOATING_INPUT,
  236. U300_FLOATING_INPUT,
  237. U300_FLOATING_INPUT,
  238. U300_FLOATING_INPUT,
  239. U300_FLOATING_INPUT,
  240. U300_FLOATING_INPUT,
  241. U300_FLOATING_INPUT,
  242. U300_FLOATING_INPUT,
  243. },
  244. /* Port 5, pins 0-7 */
  245. {
  246. U300_FLOATING_INPUT,
  247. U300_FLOATING_INPUT,
  248. U300_FLOATING_INPUT,
  249. U300_FLOATING_INPUT,
  250. U300_FLOATING_INPUT,
  251. U300_FLOATING_INPUT,
  252. U300_FLOATING_INPUT,
  253. U300_FLOATING_INPUT,
  254. },
  255. /* Port 6, pind 0-7 */
  256. {
  257. U300_FLOATING_INPUT,
  258. U300_FLOATING_INPUT,
  259. U300_FLOATING_INPUT,
  260. U300_FLOATING_INPUT,
  261. U300_FLOATING_INPUT,
  262. U300_FLOATING_INPUT,
  263. U300_FLOATING_INPUT,
  264. U300_FLOATING_INPUT,
  265. }
  266. };
  267. static const struct __initdata u300_gpio_confdata
  268. bs365_gpio_config[BS365_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
  269. /* Port 0, pins 0-7 */
  270. {
  271. U300_FLOATING_INPUT,
  272. U300_OUTPUT_LOW,
  273. U300_FLOATING_INPUT,
  274. U300_OUTPUT_LOW,
  275. U300_OUTPUT_LOW,
  276. U300_OUTPUT_LOW,
  277. U300_PULL_UP_INPUT,
  278. U300_FLOATING_INPUT,
  279. },
  280. /* Port 1, pins 0-7 */
  281. {
  282. U300_OUTPUT_LOW,
  283. U300_FLOATING_INPUT,
  284. U300_OUTPUT_LOW,
  285. U300_FLOATING_INPUT,
  286. U300_FLOATING_INPUT,
  287. U300_OUTPUT_HIGH,
  288. U300_OUTPUT_LOW,
  289. U300_OUTPUT_LOW,
  290. },
  291. /* Port 2, pins 0-7 */
  292. {
  293. U300_FLOATING_INPUT,
  294. U300_PULL_UP_INPUT,
  295. U300_OUTPUT_LOW,
  296. U300_OUTPUT_LOW,
  297. U300_PULL_UP_INPUT,
  298. U300_PULL_UP_INPUT,
  299. U300_PULL_UP_INPUT,
  300. U300_PULL_UP_INPUT,
  301. },
  302. /* Port 3, pins 0-7 */
  303. {
  304. U300_PULL_UP_INPUT,
  305. U300_PULL_UP_INPUT,
  306. U300_PULL_UP_INPUT,
  307. U300_PULL_UP_INPUT,
  308. U300_PULL_UP_INPUT,
  309. U300_PULL_UP_INPUT,
  310. U300_PULL_UP_INPUT,
  311. U300_PULL_UP_INPUT,
  312. },
  313. /* Port 4, pins 0-7 */
  314. {
  315. U300_PULL_UP_INPUT,
  316. U300_PULL_UP_INPUT,
  317. U300_PULL_UP_INPUT,
  318. U300_PULL_UP_INPUT,
  319. /* These 4 pins doesn't exist on DB3210 */
  320. U300_OUTPUT_LOW,
  321. U300_OUTPUT_LOW,
  322. U300_OUTPUT_LOW,
  323. U300_OUTPUT_LOW,
  324. }
  325. };
  326. /**
  327. * to_u300_gpio() - get the pointer to u300_gpio
  328. * @chip: the gpio chip member of the structure u300_gpio
  329. */
  330. static inline struct u300_gpio *to_u300_gpio(struct gpio_chip *chip)
  331. {
  332. return container_of(chip, struct u300_gpio, chip);
  333. }
  334. static int u300_gpio_get(struct gpio_chip *chip, unsigned offset)
  335. {
  336. struct u300_gpio *gpio = to_u300_gpio(chip);
  337. return readl(U300_PIN_REG(offset, dir)) & U300_PIN_BIT(offset);
  338. }
  339. static void u300_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  340. {
  341. struct u300_gpio *gpio = to_u300_gpio(chip);
  342. unsigned long flags;
  343. u32 val;
  344. local_irq_save(flags);
  345. val = readl(U300_PIN_REG(offset, dor));
  346. if (value)
  347. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
  348. else
  349. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
  350. local_irq_restore(flags);
  351. }
  352. static int u300_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  353. {
  354. struct u300_gpio *gpio = to_u300_gpio(chip);
  355. unsigned long flags;
  356. u32 val;
  357. local_irq_save(flags);
  358. val = readl(U300_PIN_REG(offset, pcr));
  359. /* Mask out this pin, note 2 bits per setting */
  360. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
  361. writel(val, U300_PIN_REG(offset, pcr));
  362. local_irq_restore(flags);
  363. return 0;
  364. }
  365. static int u300_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  366. int value)
  367. {
  368. struct u300_gpio *gpio = to_u300_gpio(chip);
  369. unsigned long flags;
  370. u32 oldmode;
  371. u32 val;
  372. local_irq_save(flags);
  373. val = readl(U300_PIN_REG(offset, pcr));
  374. /*
  375. * Drive mode must be set by the special mode set function, set
  376. * push/pull mode by default if no mode has been selected.
  377. */
  378. oldmode = val & (U300_GPIO_PXPCR_PIN_MODE_MASK <<
  379. ((offset & 0x07) << 1));
  380. /* mode = 0 means input, else some mode is already set */
  381. if (oldmode == 0) {
  382. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK <<
  383. ((offset & 0x07) << 1));
  384. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  385. << ((offset & 0x07) << 1));
  386. writel(val, U300_PIN_REG(offset, pcr));
  387. }
  388. u300_gpio_set(chip, offset, value);
  389. local_irq_restore(flags);
  390. return 0;
  391. }
  392. static int u300_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  393. {
  394. struct u300_gpio *gpio = to_u300_gpio(chip);
  395. int retirq = gpio->irq_base + offset;
  396. dev_dbg(gpio->dev, "request IRQ for GPIO %d, return %d\n", offset,
  397. retirq);
  398. return retirq;
  399. }
  400. static int u300_gpio_config(struct gpio_chip *chip, unsigned offset,
  401. u16 param, unsigned long *data)
  402. {
  403. struct u300_gpio *gpio = to_u300_gpio(chip);
  404. unsigned long flags;
  405. u32 val;
  406. local_irq_save(flags);
  407. switch (param) {
  408. case GPIO_U300_CONFIG_BIAS_UNKNOWN:
  409. case GPIO_U300_CONFIG_BIAS_FLOAT:
  410. val = readl(U300_PIN_REG(offset, per));
  411. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  412. break;
  413. case GPIO_U300_CONFIG_BIAS_PULL_UP:
  414. val = readl(U300_PIN_REG(offset, per));
  415. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  416. break;
  417. case GPIO_U300_CONFIG_DRIVE_PUSH_PULL:
  418. val = readl(U300_PIN_REG(offset, pcr));
  419. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  420. << ((offset & 0x07) << 1));
  421. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  422. << ((offset & 0x07) << 1));
  423. writel(val, U300_PIN_REG(offset, pcr));
  424. break;
  425. case GPIO_U300_CONFIG_DRIVE_OPEN_DRAIN:
  426. val = readl(U300_PIN_REG(offset, pcr));
  427. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  428. << ((offset & 0x07) << 1));
  429. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN
  430. << ((offset & 0x07) << 1));
  431. writel(val, U300_PIN_REG(offset, pcr));
  432. break;
  433. case GPIO_U300_CONFIG_DRIVE_OPEN_SOURCE:
  434. val = readl(U300_PIN_REG(offset, pcr));
  435. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  436. << ((offset & 0x07) << 1));
  437. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE
  438. << ((offset & 0x07) << 1));
  439. writel(val, U300_PIN_REG(offset, pcr));
  440. break;
  441. default:
  442. local_irq_restore(flags);
  443. dev_err(gpio->dev, "illegal configuration requested\n");
  444. return -EINVAL;
  445. }
  446. local_irq_restore(flags);
  447. return 0;
  448. }
  449. static struct gpio_chip u300_gpio_chip = {
  450. .label = "u300-gpio-chip",
  451. .owner = THIS_MODULE,
  452. .get = u300_gpio_get,
  453. .set = u300_gpio_set,
  454. .direction_input = u300_gpio_direction_input,
  455. .direction_output = u300_gpio_direction_output,
  456. .to_irq = u300_gpio_to_irq,
  457. };
  458. static void u300_toggle_trigger(struct u300_gpio *gpio, unsigned offset)
  459. {
  460. u32 val;
  461. val = readl(U300_PIN_REG(offset, icr));
  462. /* Set mode depending on state */
  463. if (u300_gpio_get(&gpio->chip, offset)) {
  464. /* High now, let's trigger on falling edge next then */
  465. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  466. dev_dbg(gpio->dev, "next IRQ on falling edge on pin %d\n",
  467. offset);
  468. } else {
  469. /* Low now, let's trigger on rising edge next then */
  470. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  471. dev_dbg(gpio->dev, "next IRQ on rising edge on pin %d\n",
  472. offset);
  473. }
  474. }
  475. static int u300_gpio_irq_type(struct irq_data *d, unsigned trigger)
  476. {
  477. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  478. struct u300_gpio *gpio = port->gpio;
  479. int offset = d->irq - gpio->irq_base;
  480. u32 val;
  481. if ((trigger & IRQF_TRIGGER_RISING) &&
  482. (trigger & IRQF_TRIGGER_FALLING)) {
  483. /*
  484. * The GPIO block can only trigger on falling OR rising edges,
  485. * not both. So we need to toggle the mode whenever the pin
  486. * goes from one state to the other with a special state flag
  487. */
  488. dev_dbg(gpio->dev,
  489. "trigger on both rising and falling edge on pin %d\n",
  490. offset);
  491. port->toggle_edge_mode |= U300_PIN_BIT(offset);
  492. u300_toggle_trigger(gpio, offset);
  493. } else if (trigger & IRQF_TRIGGER_RISING) {
  494. dev_dbg(gpio->dev, "trigger on rising edge on pin %d\n",
  495. offset);
  496. val = readl(U300_PIN_REG(offset, icr));
  497. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  498. port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  499. } else if (trigger & IRQF_TRIGGER_FALLING) {
  500. dev_dbg(gpio->dev, "trigger on falling edge on pin %d\n",
  501. offset);
  502. val = readl(U300_PIN_REG(offset, icr));
  503. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  504. port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  505. }
  506. return 0;
  507. }
  508. static void u300_gpio_irq_enable(struct irq_data *d)
  509. {
  510. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  511. struct u300_gpio *gpio = port->gpio;
  512. int offset = d->irq - gpio->irq_base;
  513. u32 val;
  514. unsigned long flags;
  515. local_irq_save(flags);
  516. val = readl(U300_PIN_REG(offset, ien));
  517. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
  518. local_irq_restore(flags);
  519. }
  520. static void u300_gpio_irq_disable(struct irq_data *d)
  521. {
  522. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  523. struct u300_gpio *gpio = port->gpio;
  524. int offset = d->irq - gpio->irq_base;
  525. u32 val;
  526. unsigned long flags;
  527. local_irq_save(flags);
  528. val = readl(U300_PIN_REG(offset, ien));
  529. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
  530. local_irq_restore(flags);
  531. }
  532. static struct irq_chip u300_gpio_irqchip = {
  533. .name = "u300-gpio-irqchip",
  534. .irq_enable = u300_gpio_irq_enable,
  535. .irq_disable = u300_gpio_irq_disable,
  536. .irq_set_type = u300_gpio_irq_type,
  537. };
  538. static void u300_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  539. {
  540. struct u300_gpio_port *port = irq_get_handler_data(irq);
  541. struct u300_gpio *gpio = port->gpio;
  542. int pinoffset = port->number << 3; /* get the right stride */
  543. unsigned long val;
  544. desc->irq_data.chip->irq_ack(&desc->irq_data);
  545. /* Read event register */
  546. val = readl(U300_PIN_REG(pinoffset, iev));
  547. /* Mask relevant bits */
  548. val &= 0xFFU; /* 8 bits per port */
  549. /* ACK IRQ (clear event) */
  550. writel(val, U300_PIN_REG(pinoffset, iev));
  551. /* Call IRQ handler */
  552. if (val != 0) {
  553. int irqoffset;
  554. for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) {
  555. int pin_irq = gpio->irq_base + (port->number << 3)
  556. + irqoffset;
  557. int offset = pinoffset + irqoffset;
  558. dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n",
  559. pin_irq, offset);
  560. generic_handle_irq(pin_irq);
  561. /*
  562. * Triggering IRQ on both rising and falling edge
  563. * needs mockery
  564. */
  565. if (port->toggle_edge_mode & U300_PIN_BIT(offset))
  566. u300_toggle_trigger(gpio, offset);
  567. }
  568. }
  569. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  570. }
  571. static void __init u300_gpio_init_pin(struct u300_gpio *gpio,
  572. int offset,
  573. const struct u300_gpio_confdata *conf)
  574. {
  575. /* Set mode: input or output */
  576. if (conf->output) {
  577. u300_gpio_direction_output(&gpio->chip, offset, conf->outval);
  578. /* Deactivate bias mode for output */
  579. u300_gpio_config(&gpio->chip, offset,
  580. GPIO_U300_CONFIG_BIAS_FLOAT,
  581. NULL);
  582. /* Set drive mode for output */
  583. u300_gpio_config(&gpio->chip, offset,
  584. GPIO_U300_CONFIG_DRIVE_PUSH_PULL, NULL);
  585. dev_dbg(gpio->dev, "set up pin %d as output, value: %d\n",
  586. offset, conf->outval);
  587. } else {
  588. u300_gpio_direction_input(&gpio->chip, offset);
  589. /* Always set output low on input pins */
  590. u300_gpio_set(&gpio->chip, offset, 0);
  591. /* Set bias mode for input */
  592. u300_gpio_config(&gpio->chip, offset, conf->bias_mode, NULL);
  593. dev_dbg(gpio->dev, "set up pin %d as input, bias: %04x\n",
  594. offset, conf->bias_mode);
  595. }
  596. }
  597. static void __init u300_gpio_init_coh901571(struct u300_gpio *gpio,
  598. struct u300_gpio_platform *plat)
  599. {
  600. int i, j;
  601. /* Write default config and values to all pins */
  602. for (i = 0; i < plat->ports; i++) {
  603. for (j = 0; j < 8; j++) {
  604. const struct u300_gpio_confdata *conf;
  605. int offset = (i*8) + j;
  606. if (plat->variant == U300_GPIO_COH901571_3_BS335)
  607. conf = &bs335_gpio_config[i][j];
  608. else if (plat->variant == U300_GPIO_COH901571_3_BS365)
  609. conf = &bs365_gpio_config[i][j];
  610. else
  611. break;
  612. u300_gpio_init_pin(gpio, offset, conf);
  613. }
  614. }
  615. }
  616. static inline void u300_gpio_free_ports(struct u300_gpio *gpio)
  617. {
  618. struct u300_gpio_port *port;
  619. struct list_head *p, *n;
  620. list_for_each_safe(p, n, &gpio->port_list) {
  621. port = list_entry(p, struct u300_gpio_port, node);
  622. list_del(&port->node);
  623. free_irq(port->irq, port);
  624. kfree(port);
  625. }
  626. }
  627. static int __init u300_gpio_probe(struct platform_device *pdev)
  628. {
  629. struct u300_gpio_platform *plat = dev_get_platdata(&pdev->dev);
  630. struct u300_gpio *gpio;
  631. int err = 0;
  632. int portno;
  633. u32 val;
  634. u32 ifr;
  635. int i;
  636. gpio = kzalloc(sizeof(struct u300_gpio), GFP_KERNEL);
  637. if (gpio == NULL) {
  638. dev_err(&pdev->dev, "failed to allocate memory\n");
  639. return -ENOMEM;
  640. }
  641. gpio->chip = u300_gpio_chip;
  642. gpio->chip.ngpio = plat->ports * U300_GPIO_PINS_PER_PORT;
  643. gpio->irq_base = plat->gpio_irq_base;
  644. gpio->chip.dev = &pdev->dev;
  645. gpio->chip.base = plat->gpio_base;
  646. gpio->dev = &pdev->dev;
  647. /* Get GPIO clock */
  648. gpio->clk = clk_get(gpio->dev, NULL);
  649. if (IS_ERR(gpio->clk)) {
  650. err = PTR_ERR(gpio->clk);
  651. dev_err(gpio->dev, "could not get GPIO clock\n");
  652. goto err_no_clk;
  653. }
  654. err = clk_enable(gpio->clk);
  655. if (err) {
  656. dev_err(gpio->dev, "could not enable GPIO clock\n");
  657. goto err_no_clk_enable;
  658. }
  659. gpio->memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  660. if (!gpio->memres) {
  661. dev_err(gpio->dev, "could not get GPIO memory resource\n");
  662. err = -ENODEV;
  663. goto err_no_resource;
  664. }
  665. if (!request_mem_region(gpio->memres->start,
  666. resource_size(gpio->memres),
  667. "GPIO Controller")) {
  668. err = -ENODEV;
  669. goto err_no_ioregion;
  670. }
  671. gpio->base = ioremap(gpio->memres->start, resource_size(gpio->memres));
  672. if (!gpio->base) {
  673. err = -ENOMEM;
  674. goto err_no_ioremap;
  675. }
  676. if (plat->variant == U300_GPIO_COH901335) {
  677. dev_info(gpio->dev,
  678. "initializing GPIO Controller COH 901 335\n");
  679. gpio->stride = U300_335_PORT_STRIDE;
  680. gpio->pcr = U300_335_PXPCR;
  681. gpio->dor = U300_335_PXPDOR;
  682. gpio->dir = U300_335_PXPDIR;
  683. gpio->per = U300_335_PXPER;
  684. gpio->icr = U300_335_PXICR;
  685. gpio->ien = U300_335_PXIEN;
  686. gpio->iev = U300_335_PXIEV;
  687. ifr = U300_335_PXIFR;
  688. /* Turn on the GPIO block */
  689. writel(U300_335_CR_BLOCK_CLOCK_ENABLE,
  690. gpio->base + U300_335_CR);
  691. } else if (plat->variant == U300_GPIO_COH901571_3_BS335 ||
  692. plat->variant == U300_GPIO_COH901571_3_BS365) {
  693. dev_info(gpio->dev,
  694. "initializing GPIO Controller COH 901 571/3\n");
  695. gpio->stride = U300_571_PORT_STRIDE;
  696. gpio->pcr = U300_571_PXPCR;
  697. gpio->dor = U300_571_PXPDOR;
  698. gpio->dir = U300_571_PXPDIR;
  699. gpio->per = U300_571_PXPER;
  700. gpio->icr = U300_571_PXICR;
  701. gpio->ien = U300_571_PXIEN;
  702. gpio->iev = U300_571_PXIEV;
  703. ifr = U300_571_PXIFR;
  704. val = readl(gpio->base + U300_571_CR);
  705. dev_info(gpio->dev, "COH901571/3 block version: %d, " \
  706. "number of cores: %d totalling %d pins\n",
  707. ((val & 0x000001FC) >> 2),
  708. ((val & 0x0000FE00) >> 9),
  709. ((val & 0x0000FE00) >> 9) * 8);
  710. writel(U300_571_CR_BLOCK_CLKRQ_ENABLE,
  711. gpio->base + U300_571_CR);
  712. u300_gpio_init_coh901571(gpio, plat);
  713. } else {
  714. dev_err(gpio->dev, "unknown block variant\n");
  715. err = -ENODEV;
  716. goto err_unknown_variant;
  717. }
  718. /* Add each port with its IRQ separately */
  719. INIT_LIST_HEAD(&gpio->port_list);
  720. for (portno = 0 ; portno < plat->ports; portno++) {
  721. struct u300_gpio_port *port =
  722. kmalloc(sizeof(struct u300_gpio_port), GFP_KERNEL);
  723. if (!port) {
  724. dev_err(gpio->dev, "out of memory\n");
  725. err = -ENOMEM;
  726. goto err_no_port;
  727. }
  728. snprintf(port->name, 8, "gpio%d", portno);
  729. port->number = portno;
  730. port->gpio = gpio;
  731. port->irq = platform_get_irq_byname(pdev,
  732. port->name);
  733. dev_dbg(gpio->dev, "register IRQ %d for %s\n", port->irq,
  734. port->name);
  735. irq_set_chained_handler(port->irq, u300_gpio_irq_handler);
  736. irq_set_handler_data(port->irq, port);
  737. /* For each GPIO pin set the unique IRQ handler */
  738. for (i = 0; i < U300_GPIO_PINS_PER_PORT; i++) {
  739. int irqno = gpio->irq_base + (portno << 3) + i;
  740. dev_dbg(gpio->dev, "handler for IRQ %d on %s\n",
  741. irqno, port->name);
  742. irq_set_chip_and_handler(irqno, &u300_gpio_irqchip,
  743. handle_simple_irq);
  744. set_irq_flags(irqno, IRQF_VALID);
  745. irq_set_chip_data(irqno, port);
  746. }
  747. /* Turns off irq force (test register) for this port */
  748. writel(0x0, gpio->base + portno * gpio->stride + ifr);
  749. list_add_tail(&port->node, &gpio->port_list);
  750. }
  751. dev_dbg(gpio->dev, "initialized %d GPIO ports\n", portno);
  752. err = gpiochip_add(&gpio->chip);
  753. if (err) {
  754. dev_err(gpio->dev, "unable to add gpiochip: %d\n", err);
  755. goto err_no_chip;
  756. }
  757. platform_set_drvdata(pdev, gpio);
  758. return 0;
  759. err_no_chip:
  760. err_no_port:
  761. u300_gpio_free_ports(gpio);
  762. err_unknown_variant:
  763. iounmap(gpio->base);
  764. err_no_ioremap:
  765. release_mem_region(gpio->memres->start, resource_size(gpio->memres));
  766. err_no_ioregion:
  767. err_no_resource:
  768. clk_disable(gpio->clk);
  769. err_no_clk_enable:
  770. clk_put(gpio->clk);
  771. err_no_clk:
  772. kfree(gpio);
  773. dev_info(&pdev->dev, "module ERROR:%d\n", err);
  774. return err;
  775. }
  776. static int __exit u300_gpio_remove(struct platform_device *pdev)
  777. {
  778. struct u300_gpio_platform *plat = dev_get_platdata(&pdev->dev);
  779. struct u300_gpio *gpio = platform_get_drvdata(pdev);
  780. int err;
  781. /* Turn off the GPIO block */
  782. if (plat->variant == U300_GPIO_COH901335)
  783. writel(0x00000000U, gpio->base + U300_335_CR);
  784. if (plat->variant == U300_GPIO_COH901571_3_BS335 ||
  785. plat->variant == U300_GPIO_COH901571_3_BS365)
  786. writel(0x00000000U, gpio->base + U300_571_CR);
  787. err = gpiochip_remove(&gpio->chip);
  788. if (err < 0) {
  789. dev_err(gpio->dev, "unable to remove gpiochip: %d\n", err);
  790. return err;
  791. }
  792. u300_gpio_free_ports(gpio);
  793. iounmap(gpio->base);
  794. release_mem_region(gpio->memres->start,
  795. resource_size(gpio->memres));
  796. clk_disable(gpio->clk);
  797. clk_put(gpio->clk);
  798. platform_set_drvdata(pdev, NULL);
  799. kfree(gpio);
  800. return 0;
  801. }
  802. static struct platform_driver u300_gpio_driver = {
  803. .driver = {
  804. .name = "u300-gpio",
  805. },
  806. .remove = __exit_p(u300_gpio_remove),
  807. };
  808. static int __init u300_gpio_init(void)
  809. {
  810. return platform_driver_probe(&u300_gpio_driver, u300_gpio_probe);
  811. }
  812. static void __exit u300_gpio_exit(void)
  813. {
  814. platform_driver_unregister(&u300_gpio_driver);
  815. }
  816. arch_initcall(u300_gpio_init);
  817. module_exit(u300_gpio_exit);
  818. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  819. MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335/COH 901 571/3 GPIO driver");
  820. MODULE_LICENSE("GPL");