pinctrl-coh901.c 22 KB

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