pinctrl-coh901.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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_get(struct gpio_chip *chip, unsigned offset)
  207. {
  208. struct u300_gpio *gpio = to_u300_gpio(chip);
  209. return readl(U300_PIN_REG(offset, dir)) & U300_PIN_BIT(offset);
  210. }
  211. static void u300_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  212. {
  213. struct u300_gpio *gpio = to_u300_gpio(chip);
  214. unsigned long flags;
  215. u32 val;
  216. local_irq_save(flags);
  217. val = readl(U300_PIN_REG(offset, dor));
  218. if (value)
  219. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
  220. else
  221. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
  222. local_irq_restore(flags);
  223. }
  224. static int u300_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  225. {
  226. struct u300_gpio *gpio = to_u300_gpio(chip);
  227. unsigned long flags;
  228. u32 val;
  229. local_irq_save(flags);
  230. val = readl(U300_PIN_REG(offset, pcr));
  231. /* Mask out this pin, note 2 bits per setting */
  232. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
  233. writel(val, U300_PIN_REG(offset, pcr));
  234. local_irq_restore(flags);
  235. return 0;
  236. }
  237. static int u300_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  238. int value)
  239. {
  240. struct u300_gpio *gpio = to_u300_gpio(chip);
  241. unsigned long flags;
  242. u32 oldmode;
  243. u32 val;
  244. local_irq_save(flags);
  245. val = readl(U300_PIN_REG(offset, pcr));
  246. /*
  247. * Drive mode must be set by the special mode set function, set
  248. * push/pull mode by default if no mode has been selected.
  249. */
  250. oldmode = val & (U300_GPIO_PXPCR_PIN_MODE_MASK <<
  251. ((offset & 0x07) << 1));
  252. /* mode = 0 means input, else some mode is already set */
  253. if (oldmode == 0) {
  254. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK <<
  255. ((offset & 0x07) << 1));
  256. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  257. << ((offset & 0x07) << 1));
  258. writel(val, U300_PIN_REG(offset, pcr));
  259. }
  260. u300_gpio_set(chip, offset, value);
  261. local_irq_restore(flags);
  262. return 0;
  263. }
  264. /* Returning -EINVAL means "supported but not available" */
  265. int u300_gpio_config_get(struct gpio_chip *chip,
  266. unsigned offset,
  267. unsigned long *config)
  268. {
  269. struct u300_gpio *gpio = to_u300_gpio(chip);
  270. enum pin_config_param param = (enum pin_config_param) *config;
  271. bool biasmode;
  272. u32 drmode;
  273. /* One bit per pin, clamp to bool range */
  274. biasmode = !!(readl(U300_PIN_REG(offset, per)) & U300_PIN_BIT(offset));
  275. /* Mask out the two bits for this pin and shift to bits 0,1 */
  276. drmode = readl(U300_PIN_REG(offset, pcr));
  277. drmode &= (U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
  278. drmode >>= ((offset & 0x07) << 1);
  279. switch (param) {
  280. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  281. *config = 0;
  282. if (biasmode)
  283. return 0;
  284. else
  285. return -EINVAL;
  286. break;
  287. case PIN_CONFIG_BIAS_PULL_UP:
  288. *config = 0;
  289. if (!biasmode)
  290. return 0;
  291. else
  292. return -EINVAL;
  293. break;
  294. case PIN_CONFIG_DRIVE_PUSH_PULL:
  295. *config = 0;
  296. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL)
  297. return 0;
  298. else
  299. return -EINVAL;
  300. break;
  301. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  302. *config = 0;
  303. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN)
  304. return 0;
  305. else
  306. return -EINVAL;
  307. break;
  308. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  309. *config = 0;
  310. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE)
  311. return 0;
  312. else
  313. return -EINVAL;
  314. break;
  315. default:
  316. break;
  317. }
  318. return -ENOTSUPP;
  319. }
  320. int u300_gpio_config_set(struct gpio_chip *chip, unsigned offset,
  321. enum pin_config_param param)
  322. {
  323. struct u300_gpio *gpio = to_u300_gpio(chip);
  324. unsigned long flags;
  325. u32 val;
  326. local_irq_save(flags);
  327. switch (param) {
  328. case PIN_CONFIG_BIAS_DISABLE:
  329. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  330. val = readl(U300_PIN_REG(offset, per));
  331. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  332. break;
  333. case PIN_CONFIG_BIAS_PULL_UP:
  334. val = readl(U300_PIN_REG(offset, per));
  335. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  336. break;
  337. case PIN_CONFIG_DRIVE_PUSH_PULL:
  338. val = readl(U300_PIN_REG(offset, pcr));
  339. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  340. << ((offset & 0x07) << 1));
  341. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  342. << ((offset & 0x07) << 1));
  343. writel(val, U300_PIN_REG(offset, pcr));
  344. break;
  345. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  346. val = readl(U300_PIN_REG(offset, pcr));
  347. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  348. << ((offset & 0x07) << 1));
  349. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN
  350. << ((offset & 0x07) << 1));
  351. writel(val, U300_PIN_REG(offset, pcr));
  352. break;
  353. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  354. val = readl(U300_PIN_REG(offset, pcr));
  355. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  356. << ((offset & 0x07) << 1));
  357. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE
  358. << ((offset & 0x07) << 1));
  359. writel(val, U300_PIN_REG(offset, pcr));
  360. break;
  361. default:
  362. local_irq_restore(flags);
  363. dev_err(gpio->dev, "illegal configuration requested\n");
  364. return -EINVAL;
  365. }
  366. local_irq_restore(flags);
  367. return 0;
  368. }
  369. static struct gpio_chip u300_gpio_chip = {
  370. .label = "u300-gpio-chip",
  371. .owner = THIS_MODULE,
  372. .request = gpiochip_generic_request,
  373. .free = gpiochip_generic_free,
  374. .get = u300_gpio_get,
  375. .set = u300_gpio_set,
  376. .direction_input = u300_gpio_direction_input,
  377. .direction_output = u300_gpio_direction_output,
  378. };
  379. static void u300_toggle_trigger(struct u300_gpio *gpio, unsigned offset)
  380. {
  381. u32 val;
  382. val = readl(U300_PIN_REG(offset, icr));
  383. /* Set mode depending on state */
  384. if (u300_gpio_get(&gpio->chip, offset)) {
  385. /* High now, let's trigger on falling edge next then */
  386. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  387. dev_dbg(gpio->dev, "next IRQ on falling edge on pin %d\n",
  388. offset);
  389. } else {
  390. /* Low now, let's trigger on rising edge next then */
  391. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  392. dev_dbg(gpio->dev, "next IRQ on rising edge on pin %d\n",
  393. offset);
  394. }
  395. }
  396. static int u300_gpio_irq_type(struct irq_data *d, unsigned trigger)
  397. {
  398. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  399. struct u300_gpio *gpio = to_u300_gpio(chip);
  400. struct u300_gpio_port *port = &gpio->ports[d->hwirq >> 3];
  401. int offset = d->hwirq;
  402. u32 val;
  403. if ((trigger & IRQF_TRIGGER_RISING) &&
  404. (trigger & IRQF_TRIGGER_FALLING)) {
  405. /*
  406. * The GPIO block can only trigger on falling OR rising edges,
  407. * not both. So we need to toggle the mode whenever the pin
  408. * goes from one state to the other with a special state flag
  409. */
  410. dev_dbg(gpio->dev,
  411. "trigger on both rising and falling edge on pin %d\n",
  412. offset);
  413. port->toggle_edge_mode |= U300_PIN_BIT(offset);
  414. u300_toggle_trigger(gpio, offset);
  415. } else if (trigger & IRQF_TRIGGER_RISING) {
  416. dev_dbg(gpio->dev, "trigger on rising edge on pin %d\n",
  417. offset);
  418. val = readl(U300_PIN_REG(offset, icr));
  419. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  420. port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  421. } else if (trigger & IRQF_TRIGGER_FALLING) {
  422. dev_dbg(gpio->dev, "trigger on falling edge on pin %d\n",
  423. offset);
  424. val = readl(U300_PIN_REG(offset, icr));
  425. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  426. port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  427. }
  428. return 0;
  429. }
  430. static void u300_gpio_irq_enable(struct irq_data *d)
  431. {
  432. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  433. struct u300_gpio *gpio = to_u300_gpio(chip);
  434. struct u300_gpio_port *port = &gpio->ports[d->hwirq >> 3];
  435. int offset = d->hwirq;
  436. u32 val;
  437. unsigned long flags;
  438. dev_dbg(gpio->dev, "enable IRQ for hwirq %lu on port %s, offset %d\n",
  439. d->hwirq, port->name, offset);
  440. local_irq_save(flags);
  441. val = readl(U300_PIN_REG(offset, ien));
  442. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
  443. local_irq_restore(flags);
  444. }
  445. static void u300_gpio_irq_disable(struct irq_data *d)
  446. {
  447. struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
  448. struct u300_gpio *gpio = to_u300_gpio(chip);
  449. int offset = d->hwirq;
  450. u32 val;
  451. unsigned long flags;
  452. local_irq_save(flags);
  453. val = readl(U300_PIN_REG(offset, ien));
  454. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
  455. local_irq_restore(flags);
  456. }
  457. static struct irq_chip u300_gpio_irqchip = {
  458. .name = "u300-gpio-irqchip",
  459. .irq_enable = u300_gpio_irq_enable,
  460. .irq_disable = u300_gpio_irq_disable,
  461. .irq_set_type = u300_gpio_irq_type,
  462. };
  463. static void u300_gpio_irq_handler(struct irq_desc *desc)
  464. {
  465. unsigned int irq = irq_desc_get_irq(desc);
  466. struct irq_chip *parent_chip = irq_desc_get_chip(desc);
  467. struct gpio_chip *chip = irq_desc_get_handler_data(desc);
  468. struct u300_gpio *gpio = to_u300_gpio(chip);
  469. struct u300_gpio_port *port = &gpio->ports[irq - chip->base];
  470. int pinoffset = port->number << 3; /* get the right stride */
  471. unsigned long val;
  472. chained_irq_enter(parent_chip, desc);
  473. /* Read event register */
  474. val = readl(U300_PIN_REG(pinoffset, iev));
  475. /* Mask relevant bits */
  476. val &= 0xFFU; /* 8 bits per port */
  477. /* ACK IRQ (clear event) */
  478. writel(val, U300_PIN_REG(pinoffset, iev));
  479. /* Call IRQ handler */
  480. if (val != 0) {
  481. int irqoffset;
  482. for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) {
  483. int offset = pinoffset + irqoffset;
  484. int pin_irq = irq_find_mapping(chip->irqdomain, offset);
  485. dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n",
  486. pin_irq, offset);
  487. generic_handle_irq(pin_irq);
  488. /*
  489. * Triggering IRQ on both rising and falling edge
  490. * needs mockery
  491. */
  492. if (port->toggle_edge_mode & U300_PIN_BIT(offset))
  493. u300_toggle_trigger(gpio, offset);
  494. }
  495. }
  496. chained_irq_exit(parent_chip, desc);
  497. }
  498. static void __init u300_gpio_init_pin(struct u300_gpio *gpio,
  499. int offset,
  500. const struct u300_gpio_confdata *conf)
  501. {
  502. /* Set mode: input or output */
  503. if (conf->output) {
  504. u300_gpio_direction_output(&gpio->chip, offset, conf->outval);
  505. /* Deactivate bias mode for output */
  506. u300_gpio_config_set(&gpio->chip, offset,
  507. PIN_CONFIG_BIAS_HIGH_IMPEDANCE);
  508. /* Set drive mode for output */
  509. u300_gpio_config_set(&gpio->chip, offset,
  510. PIN_CONFIG_DRIVE_PUSH_PULL);
  511. dev_dbg(gpio->dev, "set up pin %d as output, value: %d\n",
  512. offset, conf->outval);
  513. } else {
  514. u300_gpio_direction_input(&gpio->chip, offset);
  515. /* Always set output low on input pins */
  516. u300_gpio_set(&gpio->chip, offset, 0);
  517. /* Set bias mode for input */
  518. u300_gpio_config_set(&gpio->chip, offset, conf->bias_mode);
  519. dev_dbg(gpio->dev, "set up pin %d as input, bias: %04x\n",
  520. offset, conf->bias_mode);
  521. }
  522. }
  523. static void __init u300_gpio_init_coh901571(struct u300_gpio *gpio)
  524. {
  525. int i, j;
  526. /* Write default config and values to all pins */
  527. for (i = 0; i < U300_GPIO_NUM_PORTS; i++) {
  528. for (j = 0; j < 8; j++) {
  529. const struct u300_gpio_confdata *conf;
  530. int offset = (i*8) + j;
  531. conf = &bs335_gpio_config[i][j];
  532. u300_gpio_init_pin(gpio, offset, conf);
  533. }
  534. }
  535. }
  536. /*
  537. * Here we map a GPIO in the local gpio_chip pin space to a pin in
  538. * the local pinctrl pin space. The pin controller used is
  539. * pinctrl-u300.
  540. */
  541. struct coh901_pinpair {
  542. unsigned int offset;
  543. unsigned int pin_base;
  544. };
  545. #define COH901_PINRANGE(a, b) { .offset = a, .pin_base = b }
  546. static struct coh901_pinpair coh901_pintable[] = {
  547. COH901_PINRANGE(10, 426),
  548. COH901_PINRANGE(11, 180),
  549. COH901_PINRANGE(12, 165), /* MS/MMC card insertion */
  550. COH901_PINRANGE(13, 179),
  551. COH901_PINRANGE(14, 178),
  552. COH901_PINRANGE(16, 194),
  553. COH901_PINRANGE(17, 193),
  554. COH901_PINRANGE(18, 192),
  555. COH901_PINRANGE(19, 191),
  556. COH901_PINRANGE(20, 186),
  557. COH901_PINRANGE(21, 185),
  558. COH901_PINRANGE(22, 184),
  559. COH901_PINRANGE(23, 183),
  560. COH901_PINRANGE(24, 182),
  561. COH901_PINRANGE(25, 181),
  562. };
  563. static int __init u300_gpio_probe(struct platform_device *pdev)
  564. {
  565. struct u300_gpio *gpio;
  566. struct resource *memres;
  567. int err = 0;
  568. int portno;
  569. u32 val;
  570. u32 ifr;
  571. int i;
  572. gpio = devm_kzalloc(&pdev->dev, sizeof(struct u300_gpio), GFP_KERNEL);
  573. if (gpio == NULL)
  574. return -ENOMEM;
  575. gpio->chip = u300_gpio_chip;
  576. gpio->chip.ngpio = U300_GPIO_NUM_PORTS * U300_GPIO_PINS_PER_PORT;
  577. gpio->chip.dev = &pdev->dev;
  578. gpio->chip.base = 0;
  579. gpio->dev = &pdev->dev;
  580. memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  581. gpio->base = devm_ioremap_resource(&pdev->dev, memres);
  582. if (IS_ERR(gpio->base))
  583. return PTR_ERR(gpio->base);
  584. gpio->clk = devm_clk_get(gpio->dev, NULL);
  585. if (IS_ERR(gpio->clk)) {
  586. err = PTR_ERR(gpio->clk);
  587. dev_err(gpio->dev, "could not get GPIO clock\n");
  588. return err;
  589. }
  590. err = clk_prepare_enable(gpio->clk);
  591. if (err) {
  592. dev_err(gpio->dev, "could not enable GPIO clock\n");
  593. return err;
  594. }
  595. dev_info(gpio->dev,
  596. "initializing GPIO Controller COH 901 571/3\n");
  597. gpio->stride = U300_GPIO_PORT_STRIDE;
  598. gpio->pcr = U300_GPIO_PXPCR;
  599. gpio->dor = U300_GPIO_PXPDOR;
  600. gpio->dir = U300_GPIO_PXPDIR;
  601. gpio->per = U300_GPIO_PXPER;
  602. gpio->icr = U300_GPIO_PXICR;
  603. gpio->ien = U300_GPIO_PXIEN;
  604. gpio->iev = U300_GPIO_PXIEV;
  605. ifr = U300_GPIO_PXIFR;
  606. val = readl(gpio->base + U300_GPIO_CR);
  607. dev_info(gpio->dev, "COH901571/3 block version: %d, " \
  608. "number of cores: %d totalling %d pins\n",
  609. ((val & 0x000001FC) >> 2),
  610. ((val & 0x0000FE00) >> 9),
  611. ((val & 0x0000FE00) >> 9) * 8);
  612. writel(U300_GPIO_CR_BLOCK_CLKRQ_ENABLE,
  613. gpio->base + U300_GPIO_CR);
  614. u300_gpio_init_coh901571(gpio);
  615. #ifdef CONFIG_OF_GPIO
  616. gpio->chip.of_node = pdev->dev.of_node;
  617. #endif
  618. err = gpiochip_add(&gpio->chip);
  619. if (err) {
  620. dev_err(gpio->dev, "unable to add gpiochip: %d\n", err);
  621. goto err_no_chip;
  622. }
  623. err = gpiochip_irqchip_add(&gpio->chip,
  624. &u300_gpio_irqchip,
  625. 0,
  626. handle_simple_irq,
  627. IRQ_TYPE_EDGE_FALLING);
  628. if (err) {
  629. dev_err(gpio->dev, "no GPIO irqchip\n");
  630. goto err_no_irqchip;
  631. }
  632. /* Add each port with its IRQ separately */
  633. for (portno = 0 ; portno < U300_GPIO_NUM_PORTS; portno++) {
  634. struct u300_gpio_port *port = &gpio->ports[portno];
  635. snprintf(port->name, 8, "gpio%d", portno);
  636. port->number = portno;
  637. port->gpio = gpio;
  638. port->irq = platform_get_irq(pdev, portno);
  639. gpiochip_set_chained_irqchip(&gpio->chip,
  640. &u300_gpio_irqchip,
  641. port->irq,
  642. u300_gpio_irq_handler);
  643. /* Turns off irq force (test register) for this port */
  644. writel(0x0, gpio->base + portno * gpio->stride + ifr);
  645. }
  646. dev_dbg(gpio->dev, "initialized %d GPIO ports\n", portno);
  647. /*
  648. * Add pinctrl pin ranges, the pin controller must be registered
  649. * at this point
  650. */
  651. for (i = 0; i < ARRAY_SIZE(coh901_pintable); i++) {
  652. struct coh901_pinpair *p = &coh901_pintable[i];
  653. err = gpiochip_add_pin_range(&gpio->chip, "pinctrl-u300",
  654. p->offset, p->pin_base, 1);
  655. if (err)
  656. goto err_no_range;
  657. }
  658. platform_set_drvdata(pdev, gpio);
  659. return 0;
  660. err_no_range:
  661. err_no_irqchip:
  662. gpiochip_remove(&gpio->chip);
  663. err_no_chip:
  664. clk_disable_unprepare(gpio->clk);
  665. dev_err(&pdev->dev, "module ERROR:%d\n", err);
  666. return err;
  667. }
  668. static int __exit u300_gpio_remove(struct platform_device *pdev)
  669. {
  670. struct u300_gpio *gpio = platform_get_drvdata(pdev);
  671. /* Turn off the GPIO block */
  672. writel(0x00000000U, gpio->base + U300_GPIO_CR);
  673. gpiochip_remove(&gpio->chip);
  674. clk_disable_unprepare(gpio->clk);
  675. return 0;
  676. }
  677. static const struct of_device_id u300_gpio_match[] = {
  678. { .compatible = "stericsson,gpio-coh901" },
  679. {},
  680. };
  681. static struct platform_driver u300_gpio_driver = {
  682. .driver = {
  683. .name = "u300-gpio",
  684. .of_match_table = u300_gpio_match,
  685. },
  686. .remove = __exit_p(u300_gpio_remove),
  687. };
  688. static int __init u300_gpio_init(void)
  689. {
  690. return platform_driver_probe(&u300_gpio_driver, u300_gpio_probe);
  691. }
  692. static void __exit u300_gpio_exit(void)
  693. {
  694. platform_driver_unregister(&u300_gpio_driver);
  695. }
  696. arch_initcall(u300_gpio_init);
  697. module_exit(u300_gpio_exit);
  698. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  699. MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335/COH 901 571/3 GPIO driver");
  700. MODULE_LICENSE("GPL");