pinctrl-coh901.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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/irqdomain.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/gpio.h>
  21. #include <linux/list.h>
  22. #include <linux/slab.h>
  23. #include <linux/pinctrl/consumer.h>
  24. #include <linux/pinctrl/pinconf-generic.h>
  25. #include <linux/platform_data/pinctrl-coh901.h>
  26. #include "pinctrl-coh901.h"
  27. #define U300_GPIO_PORT_STRIDE (0x30)
  28. /*
  29. * Control Register 32bit (R/W)
  30. * bit 15-9 (mask 0x0000FE00) contains the number of cores. 8*cores
  31. * gives the number of GPIO pins.
  32. * bit 8-2 (mask 0x000001FC) contains the core version ID.
  33. */
  34. #define U300_GPIO_CR (0x00)
  35. #define U300_GPIO_CR_SYNC_SEL_ENABLE (0x00000002UL)
  36. #define U300_GPIO_CR_BLOCK_CLKRQ_ENABLE (0x00000001UL)
  37. #define U300_GPIO_PXPDIR (0x04)
  38. #define U300_GPIO_PXPDOR (0x08)
  39. #define U300_GPIO_PXPCR (0x0C)
  40. #define U300_GPIO_PXPCR_ALL_PINS_MODE_MASK (0x0000FFFFUL)
  41. #define U300_GPIO_PXPCR_PIN_MODE_MASK (0x00000003UL)
  42. #define U300_GPIO_PXPCR_PIN_MODE_SHIFT (0x00000002UL)
  43. #define U300_GPIO_PXPCR_PIN_MODE_INPUT (0x00000000UL)
  44. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL (0x00000001UL)
  45. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN (0x00000002UL)
  46. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE (0x00000003UL)
  47. #define U300_GPIO_PXPER (0x10)
  48. #define U300_GPIO_PXPER_ALL_PULL_UP_DISABLE_MASK (0x000000FFUL)
  49. #define U300_GPIO_PXPER_PULL_UP_DISABLE (0x00000001UL)
  50. #define U300_GPIO_PXIEV (0x14)
  51. #define U300_GPIO_PXIEN (0x18)
  52. #define U300_GPIO_PXIFR (0x1C)
  53. #define U300_GPIO_PXICR (0x20)
  54. #define U300_GPIO_PXICR_ALL_IRQ_CONFIG_MASK (0x000000FFUL)
  55. #define U300_GPIO_PXICR_IRQ_CONFIG_MASK (0x00000001UL)
  56. #define U300_GPIO_PXICR_IRQ_CONFIG_FALLING_EDGE (0x00000000UL)
  57. #define U300_GPIO_PXICR_IRQ_CONFIG_RISING_EDGE (0x00000001UL)
  58. /* 8 bits per port, no version has more than 7 ports */
  59. #define U300_GPIO_PINS_PER_PORT 8
  60. #define U300_GPIO_MAX (U300_GPIO_PINS_PER_PORT * 7)
  61. struct u300_gpio {
  62. struct gpio_chip chip;
  63. struct list_head port_list;
  64. struct clk *clk;
  65. void __iomem *base;
  66. struct device *dev;
  67. u32 stride;
  68. /* Register offsets */
  69. u32 pcr;
  70. u32 dor;
  71. u32 dir;
  72. u32 per;
  73. u32 icr;
  74. u32 ien;
  75. u32 iev;
  76. };
  77. struct u300_gpio_port {
  78. struct list_head node;
  79. struct u300_gpio *gpio;
  80. char name[8];
  81. struct irq_domain *domain;
  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 portno = offset >> 3;
  289. struct u300_gpio_port *port = NULL;
  290. struct list_head *p;
  291. int retirq;
  292. bool found = false;
  293. list_for_each(p, &gpio->port_list) {
  294. port = list_entry(p, struct u300_gpio_port, node);
  295. if (port->number == portno) {
  296. found = true;
  297. break;
  298. }
  299. }
  300. if (!found) {
  301. dev_err(gpio->dev, "could not locate port for GPIO %d IRQ\n",
  302. offset);
  303. return -EINVAL;
  304. }
  305. /*
  306. * The local hwirqs on the port are the lower three bits, there
  307. * are exactly 8 IRQs per port since they are 8-bit
  308. */
  309. retirq = irq_find_mapping(port->domain, (offset & 0x7));
  310. dev_dbg(gpio->dev, "request IRQ for GPIO %d, return %d from port %d\n",
  311. offset, retirq, port->number);
  312. return retirq;
  313. }
  314. /* Returning -EINVAL means "supported but not available" */
  315. int u300_gpio_config_get(struct gpio_chip *chip,
  316. unsigned offset,
  317. unsigned long *config)
  318. {
  319. struct u300_gpio *gpio = to_u300_gpio(chip);
  320. enum pin_config_param param = (enum pin_config_param) *config;
  321. bool biasmode;
  322. u32 drmode;
  323. /* One bit per pin, clamp to bool range */
  324. biasmode = !!(readl(U300_PIN_REG(offset, per)) & U300_PIN_BIT(offset));
  325. /* Mask out the two bits for this pin and shift to bits 0,1 */
  326. drmode = readl(U300_PIN_REG(offset, pcr));
  327. drmode &= (U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
  328. drmode >>= ((offset & 0x07) << 1);
  329. switch (param) {
  330. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  331. *config = 0;
  332. if (biasmode)
  333. return 0;
  334. else
  335. return -EINVAL;
  336. break;
  337. case PIN_CONFIG_BIAS_PULL_UP:
  338. *config = 0;
  339. if (!biasmode)
  340. return 0;
  341. else
  342. return -EINVAL;
  343. break;
  344. case PIN_CONFIG_DRIVE_PUSH_PULL:
  345. *config = 0;
  346. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL)
  347. return 0;
  348. else
  349. return -EINVAL;
  350. break;
  351. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  352. *config = 0;
  353. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN)
  354. return 0;
  355. else
  356. return -EINVAL;
  357. break;
  358. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  359. *config = 0;
  360. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE)
  361. return 0;
  362. else
  363. return -EINVAL;
  364. break;
  365. default:
  366. break;
  367. }
  368. return -ENOTSUPP;
  369. }
  370. int u300_gpio_config_set(struct gpio_chip *chip, unsigned offset,
  371. enum pin_config_param param)
  372. {
  373. struct u300_gpio *gpio = to_u300_gpio(chip);
  374. unsigned long flags;
  375. u32 val;
  376. local_irq_save(flags);
  377. switch (param) {
  378. case PIN_CONFIG_BIAS_DISABLE:
  379. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  380. val = readl(U300_PIN_REG(offset, per));
  381. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  382. break;
  383. case PIN_CONFIG_BIAS_PULL_UP:
  384. val = readl(U300_PIN_REG(offset, per));
  385. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  386. break;
  387. case PIN_CONFIG_DRIVE_PUSH_PULL:
  388. val = readl(U300_PIN_REG(offset, pcr));
  389. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  390. << ((offset & 0x07) << 1));
  391. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  392. << ((offset & 0x07) << 1));
  393. writel(val, U300_PIN_REG(offset, pcr));
  394. break;
  395. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  396. val = readl(U300_PIN_REG(offset, pcr));
  397. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  398. << ((offset & 0x07) << 1));
  399. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN
  400. << ((offset & 0x07) << 1));
  401. writel(val, U300_PIN_REG(offset, pcr));
  402. break;
  403. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  404. val = readl(U300_PIN_REG(offset, pcr));
  405. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  406. << ((offset & 0x07) << 1));
  407. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE
  408. << ((offset & 0x07) << 1));
  409. writel(val, U300_PIN_REG(offset, pcr));
  410. break;
  411. default:
  412. local_irq_restore(flags);
  413. dev_err(gpio->dev, "illegal configuration requested\n");
  414. return -EINVAL;
  415. }
  416. local_irq_restore(flags);
  417. return 0;
  418. }
  419. static struct gpio_chip u300_gpio_chip = {
  420. .label = "u300-gpio-chip",
  421. .owner = THIS_MODULE,
  422. .request = u300_gpio_request,
  423. .free = u300_gpio_free,
  424. .get = u300_gpio_get,
  425. .set = u300_gpio_set,
  426. .direction_input = u300_gpio_direction_input,
  427. .direction_output = u300_gpio_direction_output,
  428. .to_irq = u300_gpio_to_irq,
  429. };
  430. static void u300_toggle_trigger(struct u300_gpio *gpio, unsigned offset)
  431. {
  432. u32 val;
  433. val = readl(U300_PIN_REG(offset, icr));
  434. /* Set mode depending on state */
  435. if (u300_gpio_get(&gpio->chip, offset)) {
  436. /* High now, let's trigger on falling edge next then */
  437. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  438. dev_dbg(gpio->dev, "next IRQ on falling edge on pin %d\n",
  439. offset);
  440. } else {
  441. /* Low now, let's trigger on rising edge next then */
  442. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  443. dev_dbg(gpio->dev, "next IRQ on rising edge on pin %d\n",
  444. offset);
  445. }
  446. }
  447. static int u300_gpio_irq_type(struct irq_data *d, unsigned trigger)
  448. {
  449. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  450. struct u300_gpio *gpio = port->gpio;
  451. int offset = (port->number << 3) + d->hwirq;
  452. u32 val;
  453. if ((trigger & IRQF_TRIGGER_RISING) &&
  454. (trigger & IRQF_TRIGGER_FALLING)) {
  455. /*
  456. * The GPIO block can only trigger on falling OR rising edges,
  457. * not both. So we need to toggle the mode whenever the pin
  458. * goes from one state to the other with a special state flag
  459. */
  460. dev_dbg(gpio->dev,
  461. "trigger on both rising and falling edge on pin %d\n",
  462. offset);
  463. port->toggle_edge_mode |= U300_PIN_BIT(offset);
  464. u300_toggle_trigger(gpio, offset);
  465. } else if (trigger & IRQF_TRIGGER_RISING) {
  466. dev_dbg(gpio->dev, "trigger on rising edge on pin %d\n",
  467. offset);
  468. val = readl(U300_PIN_REG(offset, icr));
  469. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  470. port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  471. } else if (trigger & IRQF_TRIGGER_FALLING) {
  472. dev_dbg(gpio->dev, "trigger on falling edge on pin %d\n",
  473. offset);
  474. val = readl(U300_PIN_REG(offset, icr));
  475. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  476. port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  477. }
  478. return 0;
  479. }
  480. static void u300_gpio_irq_enable(struct irq_data *d)
  481. {
  482. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  483. struct u300_gpio *gpio = port->gpio;
  484. int offset = (port->number << 3) + d->hwirq;
  485. u32 val;
  486. unsigned long flags;
  487. dev_dbg(gpio->dev, "enable IRQ for hwirq %lu on port %s, offset %d\n",
  488. d->hwirq, port->name, offset);
  489. local_irq_save(flags);
  490. val = readl(U300_PIN_REG(offset, ien));
  491. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
  492. local_irq_restore(flags);
  493. }
  494. static void u300_gpio_irq_disable(struct irq_data *d)
  495. {
  496. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  497. struct u300_gpio *gpio = port->gpio;
  498. int offset = (port->number << 3) + d->hwirq;
  499. u32 val;
  500. unsigned long flags;
  501. local_irq_save(flags);
  502. val = readl(U300_PIN_REG(offset, ien));
  503. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
  504. local_irq_restore(flags);
  505. }
  506. static struct irq_chip u300_gpio_irqchip = {
  507. .name = "u300-gpio-irqchip",
  508. .irq_enable = u300_gpio_irq_enable,
  509. .irq_disable = u300_gpio_irq_disable,
  510. .irq_set_type = u300_gpio_irq_type,
  511. };
  512. static void u300_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  513. {
  514. struct u300_gpio_port *port = irq_get_handler_data(irq);
  515. struct u300_gpio *gpio = port->gpio;
  516. int pinoffset = port->number << 3; /* get the right stride */
  517. unsigned long val;
  518. desc->irq_data.chip->irq_ack(&desc->irq_data);
  519. /* Read event register */
  520. val = readl(U300_PIN_REG(pinoffset, iev));
  521. /* Mask relevant bits */
  522. val &= 0xFFU; /* 8 bits per port */
  523. /* ACK IRQ (clear event) */
  524. writel(val, U300_PIN_REG(pinoffset, iev));
  525. /* Call IRQ handler */
  526. if (val != 0) {
  527. int irqoffset;
  528. for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) {
  529. int pin_irq = irq_find_mapping(port->domain, irqoffset);
  530. int offset = pinoffset + irqoffset;
  531. dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n",
  532. pin_irq, offset);
  533. generic_handle_irq(pin_irq);
  534. /*
  535. * Triggering IRQ on both rising and falling edge
  536. * needs mockery
  537. */
  538. if (port->toggle_edge_mode & U300_PIN_BIT(offset))
  539. u300_toggle_trigger(gpio, offset);
  540. }
  541. }
  542. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  543. }
  544. static void __init u300_gpio_init_pin(struct u300_gpio *gpio,
  545. int offset,
  546. const struct u300_gpio_confdata *conf)
  547. {
  548. /* Set mode: input or output */
  549. if (conf->output) {
  550. u300_gpio_direction_output(&gpio->chip, offset, conf->outval);
  551. /* Deactivate bias mode for output */
  552. u300_gpio_config_set(&gpio->chip, offset,
  553. PIN_CONFIG_BIAS_HIGH_IMPEDANCE);
  554. /* Set drive mode for output */
  555. u300_gpio_config_set(&gpio->chip, offset,
  556. PIN_CONFIG_DRIVE_PUSH_PULL);
  557. dev_dbg(gpio->dev, "set up pin %d as output, value: %d\n",
  558. offset, conf->outval);
  559. } else {
  560. u300_gpio_direction_input(&gpio->chip, offset);
  561. /* Always set output low on input pins */
  562. u300_gpio_set(&gpio->chip, offset, 0);
  563. /* Set bias mode for input */
  564. u300_gpio_config_set(&gpio->chip, offset, conf->bias_mode);
  565. dev_dbg(gpio->dev, "set up pin %d as input, bias: %04x\n",
  566. offset, conf->bias_mode);
  567. }
  568. }
  569. static void __init u300_gpio_init_coh901571(struct u300_gpio *gpio,
  570. struct u300_gpio_platform *plat)
  571. {
  572. int i, j;
  573. /* Write default config and values to all pins */
  574. for (i = 0; i < plat->ports; i++) {
  575. for (j = 0; j < 8; j++) {
  576. const struct u300_gpio_confdata *conf;
  577. int offset = (i*8) + j;
  578. conf = &bs335_gpio_config[i][j];
  579. u300_gpio_init_pin(gpio, offset, conf);
  580. }
  581. }
  582. }
  583. static inline void u300_gpio_free_ports(struct u300_gpio *gpio)
  584. {
  585. struct u300_gpio_port *port;
  586. struct list_head *p, *n;
  587. list_for_each_safe(p, n, &gpio->port_list) {
  588. port = list_entry(p, struct u300_gpio_port, node);
  589. list_del(&port->node);
  590. if (port->domain)
  591. irq_domain_remove(port->domain);
  592. kfree(port);
  593. }
  594. }
  595. /*
  596. * Here we map a GPIO in the local gpio_chip pin space to a pin in
  597. * the local pinctrl pin space. The pin controller used is
  598. * pinctrl-u300.
  599. */
  600. struct coh901_pinpair {
  601. unsigned int offset;
  602. unsigned int pin_base;
  603. };
  604. #define COH901_PINRANGE(a, b) { .offset = a, .pin_base = b }
  605. static struct coh901_pinpair coh901_pintable[] = {
  606. COH901_PINRANGE(10, 426),
  607. COH901_PINRANGE(11, 180),
  608. COH901_PINRANGE(12, 165), /* MS/MMC card insertion */
  609. COH901_PINRANGE(13, 179),
  610. COH901_PINRANGE(14, 178),
  611. COH901_PINRANGE(16, 194),
  612. COH901_PINRANGE(17, 193),
  613. COH901_PINRANGE(18, 192),
  614. COH901_PINRANGE(19, 191),
  615. COH901_PINRANGE(20, 186),
  616. COH901_PINRANGE(21, 185),
  617. COH901_PINRANGE(22, 184),
  618. COH901_PINRANGE(23, 183),
  619. COH901_PINRANGE(24, 182),
  620. COH901_PINRANGE(25, 181),
  621. };
  622. static int __init u300_gpio_probe(struct platform_device *pdev)
  623. {
  624. struct u300_gpio_platform *plat = dev_get_platdata(&pdev->dev);
  625. struct u300_gpio *gpio;
  626. struct resource *memres;
  627. int err = 0;
  628. int portno;
  629. u32 val;
  630. u32 ifr;
  631. int i;
  632. gpio = devm_kzalloc(&pdev->dev, sizeof(struct u300_gpio), GFP_KERNEL);
  633. if (gpio == NULL)
  634. return -ENOMEM;
  635. gpio->chip = u300_gpio_chip;
  636. gpio->chip.ngpio = plat->ports * U300_GPIO_PINS_PER_PORT;
  637. gpio->chip.dev = &pdev->dev;
  638. gpio->chip.base = plat->gpio_base;
  639. gpio->dev = &pdev->dev;
  640. memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  641. if (!memres) {
  642. dev_err(gpio->dev, "could not get GPIO memory resource\n");
  643. return -ENODEV;
  644. }
  645. gpio->base = devm_ioremap_resource(&pdev->dev, memres);
  646. if (IS_ERR(gpio->base))
  647. return PTR_ERR(gpio->base);
  648. gpio->clk = devm_clk_get(gpio->dev, NULL);
  649. if (IS_ERR(gpio->clk)) {
  650. err = PTR_ERR(gpio->clk);
  651. dev_err(gpio->dev, "could not get GPIO clock\n");
  652. return err;
  653. }
  654. err = clk_prepare_enable(gpio->clk);
  655. if (err) {
  656. dev_err(gpio->dev, "could not enable GPIO clock\n");
  657. return err;
  658. }
  659. dev_info(gpio->dev,
  660. "initializing GPIO Controller COH 901 571/3\n");
  661. gpio->stride = U300_GPIO_PORT_STRIDE;
  662. gpio->pcr = U300_GPIO_PXPCR;
  663. gpio->dor = U300_GPIO_PXPDOR;
  664. gpio->dir = U300_GPIO_PXPDIR;
  665. gpio->per = U300_GPIO_PXPER;
  666. gpio->icr = U300_GPIO_PXICR;
  667. gpio->ien = U300_GPIO_PXIEN;
  668. gpio->iev = U300_GPIO_PXIEV;
  669. ifr = U300_GPIO_PXIFR;
  670. val = readl(gpio->base + U300_GPIO_CR);
  671. dev_info(gpio->dev, "COH901571/3 block version: %d, " \
  672. "number of cores: %d totalling %d pins\n",
  673. ((val & 0x000001FC) >> 2),
  674. ((val & 0x0000FE00) >> 9),
  675. ((val & 0x0000FE00) >> 9) * 8);
  676. writel(U300_GPIO_CR_BLOCK_CLKRQ_ENABLE,
  677. gpio->base + U300_GPIO_CR);
  678. u300_gpio_init_coh901571(gpio, plat);
  679. /* Add each port with its IRQ separately */
  680. INIT_LIST_HEAD(&gpio->port_list);
  681. for (portno = 0 ; portno < plat->ports; portno++) {
  682. struct u300_gpio_port *port =
  683. kmalloc(sizeof(struct u300_gpio_port), GFP_KERNEL);
  684. if (!port) {
  685. dev_err(gpio->dev, "out of memory\n");
  686. err = -ENOMEM;
  687. goto err_no_port;
  688. }
  689. snprintf(port->name, 8, "gpio%d", portno);
  690. port->number = portno;
  691. port->gpio = gpio;
  692. port->irq = platform_get_irq_byname(pdev,
  693. port->name);
  694. dev_dbg(gpio->dev, "register IRQ %d for port %s\n", port->irq,
  695. port->name);
  696. port->domain = irq_domain_add_linear(pdev->dev.of_node,
  697. U300_GPIO_PINS_PER_PORT,
  698. &irq_domain_simple_ops,
  699. port);
  700. if (!port->domain) {
  701. err = -ENOMEM;
  702. goto err_no_domain;
  703. }
  704. irq_set_chained_handler(port->irq, u300_gpio_irq_handler);
  705. irq_set_handler_data(port->irq, port);
  706. /* For each GPIO pin set the unique IRQ handler */
  707. for (i = 0; i < U300_GPIO_PINS_PER_PORT; i++) {
  708. int irqno = irq_create_mapping(port->domain, i);
  709. dev_dbg(gpio->dev, "GPIO%d on port %s gets IRQ %d\n",
  710. gpio->chip.base + (port->number << 3) + i,
  711. port->name, irqno);
  712. irq_set_chip_and_handler(irqno, &u300_gpio_irqchip,
  713. handle_simple_irq);
  714. set_irq_flags(irqno, IRQF_VALID);
  715. irq_set_chip_data(irqno, port);
  716. }
  717. /* Turns off irq force (test register) for this port */
  718. writel(0x0, gpio->base + portno * gpio->stride + ifr);
  719. list_add_tail(&port->node, &gpio->port_list);
  720. }
  721. dev_dbg(gpio->dev, "initialized %d GPIO ports\n", portno);
  722. err = gpiochip_add(&gpio->chip);
  723. if (err) {
  724. dev_err(gpio->dev, "unable to add gpiochip: %d\n", err);
  725. goto err_no_chip;
  726. }
  727. /*
  728. * Add pinctrl pin ranges, the pin controller must be registered
  729. * at this point
  730. */
  731. for (i = 0; i < ARRAY_SIZE(coh901_pintable); i++) {
  732. struct coh901_pinpair *p = &coh901_pintable[i];
  733. err = gpiochip_add_pin_range(&gpio->chip, "pinctrl-u300",
  734. p->offset, p->pin_base, 1);
  735. if (err)
  736. goto err_no_range;
  737. }
  738. platform_set_drvdata(pdev, gpio);
  739. return 0;
  740. err_no_range:
  741. err = gpiochip_remove(&gpio->chip);
  742. err_no_chip:
  743. err_no_domain:
  744. err_no_port:
  745. u300_gpio_free_ports(gpio);
  746. clk_disable_unprepare(gpio->clk);
  747. dev_err(&pdev->dev, "module ERROR:%d\n", err);
  748. return err;
  749. }
  750. static int __exit u300_gpio_remove(struct platform_device *pdev)
  751. {
  752. struct u300_gpio *gpio = platform_get_drvdata(pdev);
  753. int err;
  754. /* Turn off the GPIO block */
  755. writel(0x00000000U, gpio->base + U300_GPIO_CR);
  756. err = gpiochip_remove(&gpio->chip);
  757. if (err < 0) {
  758. dev_err(gpio->dev, "unable to remove gpiochip: %d\n", err);
  759. return err;
  760. }
  761. u300_gpio_free_ports(gpio);
  762. clk_disable_unprepare(gpio->clk);
  763. platform_set_drvdata(pdev, NULL);
  764. return 0;
  765. }
  766. static struct platform_driver u300_gpio_driver = {
  767. .driver = {
  768. .name = "u300-gpio",
  769. },
  770. .remove = __exit_p(u300_gpio_remove),
  771. };
  772. static int __init u300_gpio_init(void)
  773. {
  774. return platform_driver_probe(&u300_gpio_driver, u300_gpio_probe);
  775. }
  776. static void __exit u300_gpio_exit(void)
  777. {
  778. platform_driver_unregister(&u300_gpio_driver);
  779. }
  780. arch_initcall(u300_gpio_init);
  781. module_exit(u300_gpio_exit);
  782. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  783. MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335/COH 901 571/3 GPIO driver");
  784. MODULE_LICENSE("GPL");