pinctrl-coh901.c 24 KB

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