pinctrl-coh901.c 23 KB

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