pinctrl-coh901.c 21 KB

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