pinctrl-coh901.c 25 KB

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