pinctrl-coh901.c 20 KB

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