pinctrl-coh901.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * U300 GPIO module.
  3. *
  4. * Copyright (C) 2007-2011 ST-Ericsson AB
  5. * License terms: GNU General Public License (GPL) version 2
  6. * This can driver either of the two basic GPIO cores
  7. * available in the U300 platforms:
  8. * COH 901 335 - Used in DB3150 (U300 1.0) and DB3200 (U330 1.0)
  9. * COH 901 571/3 - Used in DB3210 (U365 2.0) and DB3350 (U335 1.0)
  10. * Author: Linus Walleij <linus.walleij@linaro.org>
  11. * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/irq.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/errno.h>
  18. #include <linux/io.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/gpio.h>
  23. #include <linux/list.h>
  24. #include <linux/slab.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/pinctrl/pinconf-generic.h>
  27. #include <mach/gpio-u300.h>
  28. #include "pinctrl-coh901.h"
  29. /*
  30. * Register definitions for COH 901 335 variant
  31. */
  32. #define U300_335_PORT_STRIDE (0x1C)
  33. /* Port X Pin Data Register 32bit, this is both input and output (R/W) */
  34. #define U300_335_PXPDIR (0x00)
  35. #define U300_335_PXPDOR (0x00)
  36. /* Port X Pin Config Register 32bit (R/W) */
  37. #define U300_335_PXPCR (0x04)
  38. /* This register layout is the same in both blocks */
  39. #define U300_GPIO_PXPCR_ALL_PINS_MODE_MASK (0x0000FFFFUL)
  40. #define U300_GPIO_PXPCR_PIN_MODE_MASK (0x00000003UL)
  41. #define U300_GPIO_PXPCR_PIN_MODE_SHIFT (0x00000002UL)
  42. #define U300_GPIO_PXPCR_PIN_MODE_INPUT (0x00000000UL)
  43. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL (0x00000001UL)
  44. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN (0x00000002UL)
  45. #define U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE (0x00000003UL)
  46. /* Port X Interrupt Event Register 32bit (R/W) */
  47. #define U300_335_PXIEV (0x08)
  48. /* Port X Interrupt Enable Register 32bit (R/W) */
  49. #define U300_335_PXIEN (0x0C)
  50. /* Port X Interrupt Force Register 32bit (R/W) */
  51. #define U300_335_PXIFR (0x10)
  52. /* Port X Interrupt Config Register 32bit (R/W) */
  53. #define U300_335_PXICR (0x14)
  54. /* This register layout is the same in both blocks */
  55. #define U300_GPIO_PXICR_ALL_IRQ_CONFIG_MASK (0x000000FFUL)
  56. #define U300_GPIO_PXICR_IRQ_CONFIG_MASK (0x00000001UL)
  57. #define U300_GPIO_PXICR_IRQ_CONFIG_FALLING_EDGE (0x00000000UL)
  58. #define U300_GPIO_PXICR_IRQ_CONFIG_RISING_EDGE (0x00000001UL)
  59. /* Port X Pull-up Enable Register 32bit (R/W) */
  60. #define U300_335_PXPER (0x18)
  61. /* This register layout is the same in both blocks */
  62. #define U300_GPIO_PXPER_ALL_PULL_UP_DISABLE_MASK (0x000000FFUL)
  63. #define U300_GPIO_PXPER_PULL_UP_DISABLE (0x00000001UL)
  64. /* Control Register 32bit (R/W) */
  65. #define U300_335_CR (0x54)
  66. #define U300_335_CR_BLOCK_CLOCK_ENABLE (0x00000001UL)
  67. /*
  68. * Register definitions for COH 901 571 / 3 variant
  69. */
  70. #define U300_571_PORT_STRIDE (0x30)
  71. /*
  72. * Control Register 32bit (R/W)
  73. * bit 15-9 (mask 0x0000FE00) contains the number of cores. 8*cores
  74. * gives the number of GPIO pins.
  75. * bit 8-2 (mask 0x000001FC) contains the core version ID.
  76. */
  77. #define U300_571_CR (0x00)
  78. #define U300_571_CR_SYNC_SEL_ENABLE (0x00000002UL)
  79. #define U300_571_CR_BLOCK_CLKRQ_ENABLE (0x00000001UL)
  80. /*
  81. * These registers have the same layout and function as the corresponding
  82. * COH 901 335 registers, just at different offset.
  83. */
  84. #define U300_571_PXPDIR (0x04)
  85. #define U300_571_PXPDOR (0x08)
  86. #define U300_571_PXPCR (0x0C)
  87. #define U300_571_PXPER (0x10)
  88. #define U300_571_PXIEV (0x14)
  89. #define U300_571_PXIEN (0x18)
  90. #define U300_571_PXIFR (0x1C)
  91. #define U300_571_PXICR (0x20)
  92. /* 8 bits per port, no version has more than 7 ports */
  93. #define U300_GPIO_PINS_PER_PORT 8
  94. #define U300_GPIO_MAX (U300_GPIO_PINS_PER_PORT * 7)
  95. struct u300_gpio {
  96. struct gpio_chip chip;
  97. struct list_head port_list;
  98. struct clk *clk;
  99. struct resource *memres;
  100. void __iomem *base;
  101. struct device *dev;
  102. int irq_base;
  103. u32 stride;
  104. /* Register offsets */
  105. u32 pcr;
  106. u32 dor;
  107. u32 dir;
  108. u32 per;
  109. u32 icr;
  110. u32 ien;
  111. u32 iev;
  112. };
  113. struct u300_gpio_port {
  114. struct list_head node;
  115. struct u300_gpio *gpio;
  116. char name[8];
  117. int irq;
  118. int number;
  119. u8 toggle_edge_mode;
  120. };
  121. /*
  122. * Macro to expand to read a specific register found in the "gpio"
  123. * struct. It requires the struct u300_gpio *gpio variable to exist in
  124. * its context. It calculates the port offset from the given pin
  125. * offset, muliplies by the port stride and adds the register offset
  126. * so it provides a pointer to the desired register.
  127. */
  128. #define U300_PIN_REG(pin, reg) \
  129. (gpio->base + (pin >> 3) * gpio->stride + gpio->reg)
  130. /*
  131. * Provides a bitmask for a specific gpio pin inside an 8-bit GPIO
  132. * register.
  133. */
  134. #define U300_PIN_BIT(pin) \
  135. (1 << (pin & 0x07))
  136. struct u300_gpio_confdata {
  137. u16 bias_mode;
  138. bool output;
  139. int outval;
  140. };
  141. /* BS335 has seven ports of 8 bits each = GPIO pins 0..55 */
  142. #define BS335_GPIO_NUM_PORTS 7
  143. /* BS365 has five ports of 8 bits each = GPIO pins 0..39 */
  144. #define BS365_GPIO_NUM_PORTS 5
  145. #define U300_FLOATING_INPUT { \
  146. .bias_mode = PIN_CONFIG_BIAS_HIGH_IMPEDANCE, \
  147. .output = false, \
  148. }
  149. #define U300_PULL_UP_INPUT { \
  150. .bias_mode = PIN_CONFIG_BIAS_PULL_UP, \
  151. .output = false, \
  152. }
  153. #define U300_OUTPUT_LOW { \
  154. .output = true, \
  155. .outval = 0, \
  156. }
  157. #define U300_OUTPUT_HIGH { \
  158. .output = true, \
  159. .outval = 1, \
  160. }
  161. /* Initial configuration */
  162. static const struct __initdata u300_gpio_confdata
  163. bs335_gpio_config[BS335_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
  164. /* Port 0, pins 0-7 */
  165. {
  166. U300_FLOATING_INPUT,
  167. U300_OUTPUT_HIGH,
  168. U300_FLOATING_INPUT,
  169. U300_OUTPUT_LOW,
  170. U300_OUTPUT_LOW,
  171. U300_OUTPUT_LOW,
  172. U300_OUTPUT_LOW,
  173. U300_OUTPUT_LOW,
  174. },
  175. /* Port 1, pins 0-7 */
  176. {
  177. U300_OUTPUT_LOW,
  178. U300_OUTPUT_LOW,
  179. U300_OUTPUT_LOW,
  180. U300_PULL_UP_INPUT,
  181. U300_FLOATING_INPUT,
  182. U300_OUTPUT_HIGH,
  183. U300_OUTPUT_LOW,
  184. U300_OUTPUT_LOW,
  185. },
  186. /* Port 2, pins 0-7 */
  187. {
  188. U300_FLOATING_INPUT,
  189. U300_FLOATING_INPUT,
  190. U300_FLOATING_INPUT,
  191. U300_FLOATING_INPUT,
  192. U300_OUTPUT_LOW,
  193. U300_PULL_UP_INPUT,
  194. U300_OUTPUT_LOW,
  195. U300_PULL_UP_INPUT,
  196. },
  197. /* Port 3, pins 0-7 */
  198. {
  199. U300_PULL_UP_INPUT,
  200. U300_OUTPUT_LOW,
  201. U300_FLOATING_INPUT,
  202. U300_FLOATING_INPUT,
  203. U300_FLOATING_INPUT,
  204. U300_FLOATING_INPUT,
  205. U300_FLOATING_INPUT,
  206. U300_FLOATING_INPUT,
  207. },
  208. /* Port 4, pins 0-7 */
  209. {
  210. U300_FLOATING_INPUT,
  211. U300_FLOATING_INPUT,
  212. U300_FLOATING_INPUT,
  213. U300_FLOATING_INPUT,
  214. U300_FLOATING_INPUT,
  215. U300_FLOATING_INPUT,
  216. U300_FLOATING_INPUT,
  217. U300_FLOATING_INPUT,
  218. },
  219. /* Port 5, pins 0-7 */
  220. {
  221. U300_FLOATING_INPUT,
  222. U300_FLOATING_INPUT,
  223. U300_FLOATING_INPUT,
  224. U300_FLOATING_INPUT,
  225. U300_FLOATING_INPUT,
  226. U300_FLOATING_INPUT,
  227. U300_FLOATING_INPUT,
  228. U300_FLOATING_INPUT,
  229. },
  230. /* Port 6, pind 0-7 */
  231. {
  232. U300_FLOATING_INPUT,
  233. U300_FLOATING_INPUT,
  234. U300_FLOATING_INPUT,
  235. U300_FLOATING_INPUT,
  236. U300_FLOATING_INPUT,
  237. U300_FLOATING_INPUT,
  238. U300_FLOATING_INPUT,
  239. U300_FLOATING_INPUT,
  240. }
  241. };
  242. static const struct __initdata u300_gpio_confdata
  243. bs365_gpio_config[BS365_GPIO_NUM_PORTS][U300_GPIO_PINS_PER_PORT] = {
  244. /* Port 0, pins 0-7 */
  245. {
  246. U300_FLOATING_INPUT,
  247. U300_OUTPUT_LOW,
  248. U300_FLOATING_INPUT,
  249. U300_OUTPUT_LOW,
  250. U300_OUTPUT_LOW,
  251. U300_OUTPUT_LOW,
  252. U300_PULL_UP_INPUT,
  253. U300_FLOATING_INPUT,
  254. },
  255. /* Port 1, pins 0-7 */
  256. {
  257. U300_OUTPUT_LOW,
  258. U300_FLOATING_INPUT,
  259. U300_OUTPUT_LOW,
  260. U300_FLOATING_INPUT,
  261. U300_FLOATING_INPUT,
  262. U300_OUTPUT_HIGH,
  263. U300_OUTPUT_LOW,
  264. U300_OUTPUT_LOW,
  265. },
  266. /* Port 2, pins 0-7 */
  267. {
  268. U300_FLOATING_INPUT,
  269. U300_PULL_UP_INPUT,
  270. U300_OUTPUT_LOW,
  271. U300_OUTPUT_LOW,
  272. U300_PULL_UP_INPUT,
  273. U300_PULL_UP_INPUT,
  274. U300_PULL_UP_INPUT,
  275. U300_PULL_UP_INPUT,
  276. },
  277. /* Port 3, pins 0-7 */
  278. {
  279. U300_PULL_UP_INPUT,
  280. U300_PULL_UP_INPUT,
  281. U300_PULL_UP_INPUT,
  282. U300_PULL_UP_INPUT,
  283. U300_PULL_UP_INPUT,
  284. U300_PULL_UP_INPUT,
  285. U300_PULL_UP_INPUT,
  286. U300_PULL_UP_INPUT,
  287. },
  288. /* Port 4, pins 0-7 */
  289. {
  290. U300_PULL_UP_INPUT,
  291. U300_PULL_UP_INPUT,
  292. U300_PULL_UP_INPUT,
  293. U300_PULL_UP_INPUT,
  294. /* These 4 pins doesn't exist on DB3210 */
  295. U300_OUTPUT_LOW,
  296. U300_OUTPUT_LOW,
  297. U300_OUTPUT_LOW,
  298. U300_OUTPUT_LOW,
  299. }
  300. };
  301. /**
  302. * to_u300_gpio() - get the pointer to u300_gpio
  303. * @chip: the gpio chip member of the structure u300_gpio
  304. */
  305. static inline struct u300_gpio *to_u300_gpio(struct gpio_chip *chip)
  306. {
  307. return container_of(chip, struct u300_gpio, chip);
  308. }
  309. static int u300_gpio_request(struct gpio_chip *chip, unsigned offset)
  310. {
  311. /*
  312. * Map back to global GPIO space and request muxing, the direction
  313. * parameter does not matter for this controller.
  314. */
  315. int gpio = chip->base + offset;
  316. return pinctrl_request_gpio(gpio);
  317. }
  318. static void u300_gpio_free(struct gpio_chip *chip, unsigned offset)
  319. {
  320. int gpio = chip->base + offset;
  321. pinctrl_free_gpio(gpio);
  322. }
  323. static int u300_gpio_get(struct gpio_chip *chip, unsigned offset)
  324. {
  325. struct u300_gpio *gpio = to_u300_gpio(chip);
  326. return readl(U300_PIN_REG(offset, dir)) & U300_PIN_BIT(offset);
  327. }
  328. static void u300_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  329. {
  330. struct u300_gpio *gpio = to_u300_gpio(chip);
  331. unsigned long flags;
  332. u32 val;
  333. local_irq_save(flags);
  334. val = readl(U300_PIN_REG(offset, dor));
  335. if (value)
  336. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
  337. else
  338. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, dor));
  339. local_irq_restore(flags);
  340. }
  341. static int u300_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  342. {
  343. struct u300_gpio *gpio = to_u300_gpio(chip);
  344. unsigned long flags;
  345. u32 val;
  346. local_irq_save(flags);
  347. val = readl(U300_PIN_REG(offset, pcr));
  348. /* Mask out this pin, note 2 bits per setting */
  349. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
  350. writel(val, U300_PIN_REG(offset, pcr));
  351. local_irq_restore(flags);
  352. return 0;
  353. }
  354. static int u300_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  355. int value)
  356. {
  357. struct u300_gpio *gpio = to_u300_gpio(chip);
  358. unsigned long flags;
  359. u32 oldmode;
  360. u32 val;
  361. local_irq_save(flags);
  362. val = readl(U300_PIN_REG(offset, pcr));
  363. /*
  364. * Drive mode must be set by the special mode set function, set
  365. * push/pull mode by default if no mode has been selected.
  366. */
  367. oldmode = val & (U300_GPIO_PXPCR_PIN_MODE_MASK <<
  368. ((offset & 0x07) << 1));
  369. /* mode = 0 means input, else some mode is already set */
  370. if (oldmode == 0) {
  371. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK <<
  372. ((offset & 0x07) << 1));
  373. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  374. << ((offset & 0x07) << 1));
  375. writel(val, U300_PIN_REG(offset, pcr));
  376. }
  377. u300_gpio_set(chip, offset, value);
  378. local_irq_restore(flags);
  379. return 0;
  380. }
  381. static int u300_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  382. {
  383. struct u300_gpio *gpio = to_u300_gpio(chip);
  384. int retirq = gpio->irq_base + offset;
  385. dev_dbg(gpio->dev, "request IRQ for GPIO %d, return %d\n", offset,
  386. retirq);
  387. return retirq;
  388. }
  389. /* Returning -EINVAL means "supported but not available" */
  390. int u300_gpio_config_get(struct gpio_chip *chip,
  391. unsigned offset,
  392. unsigned long *config)
  393. {
  394. struct u300_gpio *gpio = to_u300_gpio(chip);
  395. enum pin_config_param param = (enum pin_config_param) *config;
  396. bool biasmode;
  397. u32 drmode;
  398. /* One bit per pin, clamp to bool range */
  399. biasmode = !!(readl(U300_PIN_REG(offset, per)) & U300_PIN_BIT(offset));
  400. /* Mask out the two bits for this pin and shift to bits 0,1 */
  401. drmode = readl(U300_PIN_REG(offset, pcr));
  402. drmode &= (U300_GPIO_PXPCR_PIN_MODE_MASK << ((offset & 0x07) << 1));
  403. drmode >>= ((offset & 0x07) << 1);
  404. switch(param) {
  405. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  406. *config = 0;
  407. if (biasmode)
  408. return 0;
  409. else
  410. return -EINVAL;
  411. break;
  412. case PIN_CONFIG_BIAS_PULL_UP:
  413. *config = 0;
  414. if (!biasmode)
  415. return 0;
  416. else
  417. return -EINVAL;
  418. break;
  419. case PIN_CONFIG_DRIVE_PUSH_PULL:
  420. *config = 0;
  421. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL)
  422. return 0;
  423. else
  424. return -EINVAL;
  425. break;
  426. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  427. *config = 0;
  428. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN)
  429. return 0;
  430. else
  431. return -EINVAL;
  432. break;
  433. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  434. *config = 0;
  435. if (drmode == U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE)
  436. return 0;
  437. else
  438. return -EINVAL;
  439. break;
  440. default:
  441. break;
  442. }
  443. return -ENOTSUPP;
  444. }
  445. int u300_gpio_config_set(struct gpio_chip *chip, unsigned offset,
  446. enum pin_config_param param)
  447. {
  448. struct u300_gpio *gpio = to_u300_gpio(chip);
  449. unsigned long flags;
  450. u32 val;
  451. local_irq_save(flags);
  452. switch (param) {
  453. case PIN_CONFIG_BIAS_DISABLE:
  454. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  455. val = readl(U300_PIN_REG(offset, per));
  456. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  457. break;
  458. case PIN_CONFIG_BIAS_PULL_UP:
  459. val = readl(U300_PIN_REG(offset, per));
  460. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, per));
  461. break;
  462. case PIN_CONFIG_DRIVE_PUSH_PULL:
  463. val = readl(U300_PIN_REG(offset, pcr));
  464. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  465. << ((offset & 0x07) << 1));
  466. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_PUSH_PULL
  467. << ((offset & 0x07) << 1));
  468. writel(val, U300_PIN_REG(offset, pcr));
  469. break;
  470. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  471. val = readl(U300_PIN_REG(offset, pcr));
  472. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  473. << ((offset & 0x07) << 1));
  474. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_DRAIN
  475. << ((offset & 0x07) << 1));
  476. writel(val, U300_PIN_REG(offset, pcr));
  477. break;
  478. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  479. val = readl(U300_PIN_REG(offset, pcr));
  480. val &= ~(U300_GPIO_PXPCR_PIN_MODE_MASK
  481. << ((offset & 0x07) << 1));
  482. val |= (U300_GPIO_PXPCR_PIN_MODE_OUTPUT_OPEN_SOURCE
  483. << ((offset & 0x07) << 1));
  484. writel(val, U300_PIN_REG(offset, pcr));
  485. break;
  486. default:
  487. local_irq_restore(flags);
  488. dev_err(gpio->dev, "illegal configuration requested\n");
  489. return -EINVAL;
  490. }
  491. local_irq_restore(flags);
  492. return 0;
  493. }
  494. static struct gpio_chip u300_gpio_chip = {
  495. .label = "u300-gpio-chip",
  496. .owner = THIS_MODULE,
  497. .request = u300_gpio_request,
  498. .free = u300_gpio_free,
  499. .get = u300_gpio_get,
  500. .set = u300_gpio_set,
  501. .direction_input = u300_gpio_direction_input,
  502. .direction_output = u300_gpio_direction_output,
  503. .to_irq = u300_gpio_to_irq,
  504. };
  505. static void u300_toggle_trigger(struct u300_gpio *gpio, unsigned offset)
  506. {
  507. u32 val;
  508. val = readl(U300_PIN_REG(offset, icr));
  509. /* Set mode depending on state */
  510. if (u300_gpio_get(&gpio->chip, offset)) {
  511. /* High now, let's trigger on falling edge next then */
  512. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  513. dev_dbg(gpio->dev, "next IRQ on falling edge on pin %d\n",
  514. offset);
  515. } else {
  516. /* Low now, let's trigger on rising edge next then */
  517. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  518. dev_dbg(gpio->dev, "next IRQ on rising edge on pin %d\n",
  519. offset);
  520. }
  521. }
  522. static int u300_gpio_irq_type(struct irq_data *d, unsigned trigger)
  523. {
  524. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  525. struct u300_gpio *gpio = port->gpio;
  526. int offset = d->irq - gpio->irq_base;
  527. u32 val;
  528. if ((trigger & IRQF_TRIGGER_RISING) &&
  529. (trigger & IRQF_TRIGGER_FALLING)) {
  530. /*
  531. * The GPIO block can only trigger on falling OR rising edges,
  532. * not both. So we need to toggle the mode whenever the pin
  533. * goes from one state to the other with a special state flag
  534. */
  535. dev_dbg(gpio->dev,
  536. "trigger on both rising and falling edge on pin %d\n",
  537. offset);
  538. port->toggle_edge_mode |= U300_PIN_BIT(offset);
  539. u300_toggle_trigger(gpio, offset);
  540. } else if (trigger & IRQF_TRIGGER_RISING) {
  541. dev_dbg(gpio->dev, "trigger on rising edge on pin %d\n",
  542. offset);
  543. val = readl(U300_PIN_REG(offset, icr));
  544. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  545. port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  546. } else if (trigger & IRQF_TRIGGER_FALLING) {
  547. dev_dbg(gpio->dev, "trigger on falling edge on pin %d\n",
  548. offset);
  549. val = readl(U300_PIN_REG(offset, icr));
  550. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, icr));
  551. port->toggle_edge_mode &= ~U300_PIN_BIT(offset);
  552. }
  553. return 0;
  554. }
  555. static void u300_gpio_irq_enable(struct irq_data *d)
  556. {
  557. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  558. struct u300_gpio *gpio = port->gpio;
  559. int offset = d->irq - gpio->irq_base;
  560. u32 val;
  561. unsigned long flags;
  562. local_irq_save(flags);
  563. val = readl(U300_PIN_REG(offset, ien));
  564. writel(val | U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
  565. local_irq_restore(flags);
  566. }
  567. static void u300_gpio_irq_disable(struct irq_data *d)
  568. {
  569. struct u300_gpio_port *port = irq_data_get_irq_chip_data(d);
  570. struct u300_gpio *gpio = port->gpio;
  571. int offset = d->irq - gpio->irq_base;
  572. u32 val;
  573. unsigned long flags;
  574. local_irq_save(flags);
  575. val = readl(U300_PIN_REG(offset, ien));
  576. writel(val & ~U300_PIN_BIT(offset), U300_PIN_REG(offset, ien));
  577. local_irq_restore(flags);
  578. }
  579. static struct irq_chip u300_gpio_irqchip = {
  580. .name = "u300-gpio-irqchip",
  581. .irq_enable = u300_gpio_irq_enable,
  582. .irq_disable = u300_gpio_irq_disable,
  583. .irq_set_type = u300_gpio_irq_type,
  584. };
  585. static void u300_gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  586. {
  587. struct u300_gpio_port *port = irq_get_handler_data(irq);
  588. struct u300_gpio *gpio = port->gpio;
  589. int pinoffset = port->number << 3; /* get the right stride */
  590. unsigned long val;
  591. desc->irq_data.chip->irq_ack(&desc->irq_data);
  592. /* Read event register */
  593. val = readl(U300_PIN_REG(pinoffset, iev));
  594. /* Mask relevant bits */
  595. val &= 0xFFU; /* 8 bits per port */
  596. /* ACK IRQ (clear event) */
  597. writel(val, U300_PIN_REG(pinoffset, iev));
  598. /* Call IRQ handler */
  599. if (val != 0) {
  600. int irqoffset;
  601. for_each_set_bit(irqoffset, &val, U300_GPIO_PINS_PER_PORT) {
  602. int pin_irq = gpio->irq_base + (port->number << 3)
  603. + irqoffset;
  604. int offset = pinoffset + irqoffset;
  605. dev_dbg(gpio->dev, "GPIO IRQ %d on pin %d\n",
  606. pin_irq, offset);
  607. generic_handle_irq(pin_irq);
  608. /*
  609. * Triggering IRQ on both rising and falling edge
  610. * needs mockery
  611. */
  612. if (port->toggle_edge_mode & U300_PIN_BIT(offset))
  613. u300_toggle_trigger(gpio, offset);
  614. }
  615. }
  616. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  617. }
  618. static void __init u300_gpio_init_pin(struct u300_gpio *gpio,
  619. int offset,
  620. const struct u300_gpio_confdata *conf)
  621. {
  622. /* Set mode: input or output */
  623. if (conf->output) {
  624. u300_gpio_direction_output(&gpio->chip, offset, conf->outval);
  625. /* Deactivate bias mode for output */
  626. u300_gpio_config_set(&gpio->chip, offset,
  627. PIN_CONFIG_BIAS_HIGH_IMPEDANCE);
  628. /* Set drive mode for output */
  629. u300_gpio_config_set(&gpio->chip, offset,
  630. PIN_CONFIG_DRIVE_PUSH_PULL);
  631. dev_dbg(gpio->dev, "set up pin %d as output, value: %d\n",
  632. offset, conf->outval);
  633. } else {
  634. u300_gpio_direction_input(&gpio->chip, offset);
  635. /* Always set output low on input pins */
  636. u300_gpio_set(&gpio->chip, offset, 0);
  637. /* Set bias mode for input */
  638. u300_gpio_config_set(&gpio->chip, offset, conf->bias_mode);
  639. dev_dbg(gpio->dev, "set up pin %d as input, bias: %04x\n",
  640. offset, conf->bias_mode);
  641. }
  642. }
  643. static void __init u300_gpio_init_coh901571(struct u300_gpio *gpio,
  644. struct u300_gpio_platform *plat)
  645. {
  646. int i, j;
  647. /* Write default config and values to all pins */
  648. for (i = 0; i < plat->ports; i++) {
  649. for (j = 0; j < 8; j++) {
  650. const struct u300_gpio_confdata *conf;
  651. int offset = (i*8) + j;
  652. if (plat->variant == U300_GPIO_COH901571_3_BS335)
  653. conf = &bs335_gpio_config[i][j];
  654. else if (plat->variant == U300_GPIO_COH901571_3_BS365)
  655. conf = &bs365_gpio_config[i][j];
  656. else
  657. break;
  658. u300_gpio_init_pin(gpio, offset, conf);
  659. }
  660. }
  661. }
  662. static inline void u300_gpio_free_ports(struct u300_gpio *gpio)
  663. {
  664. struct u300_gpio_port *port;
  665. struct list_head *p, *n;
  666. list_for_each_safe(p, n, &gpio->port_list) {
  667. port = list_entry(p, struct u300_gpio_port, node);
  668. list_del(&port->node);
  669. kfree(port);
  670. }
  671. }
  672. static int __init u300_gpio_probe(struct platform_device *pdev)
  673. {
  674. struct u300_gpio_platform *plat = dev_get_platdata(&pdev->dev);
  675. struct u300_gpio *gpio;
  676. int err = 0;
  677. int portno;
  678. u32 val;
  679. u32 ifr;
  680. int i;
  681. gpio = kzalloc(sizeof(struct u300_gpio), GFP_KERNEL);
  682. if (gpio == NULL) {
  683. dev_err(&pdev->dev, "failed to allocate memory\n");
  684. return -ENOMEM;
  685. }
  686. gpio->chip = u300_gpio_chip;
  687. gpio->chip.ngpio = plat->ports * U300_GPIO_PINS_PER_PORT;
  688. gpio->irq_base = plat->gpio_irq_base;
  689. gpio->chip.dev = &pdev->dev;
  690. gpio->chip.base = plat->gpio_base;
  691. gpio->dev = &pdev->dev;
  692. /* Get GPIO clock */
  693. gpio->clk = clk_get(gpio->dev, NULL);
  694. if (IS_ERR(gpio->clk)) {
  695. err = PTR_ERR(gpio->clk);
  696. dev_err(gpio->dev, "could not get GPIO clock\n");
  697. goto err_no_clk;
  698. }
  699. err = clk_enable(gpio->clk);
  700. if (err) {
  701. dev_err(gpio->dev, "could not enable GPIO clock\n");
  702. goto err_no_clk_enable;
  703. }
  704. gpio->memres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  705. if (!gpio->memres) {
  706. dev_err(gpio->dev, "could not get GPIO memory resource\n");
  707. err = -ENODEV;
  708. goto err_no_resource;
  709. }
  710. if (!request_mem_region(gpio->memres->start,
  711. resource_size(gpio->memres),
  712. "GPIO Controller")) {
  713. err = -ENODEV;
  714. goto err_no_ioregion;
  715. }
  716. gpio->base = ioremap(gpio->memres->start, resource_size(gpio->memres));
  717. if (!gpio->base) {
  718. err = -ENOMEM;
  719. goto err_no_ioremap;
  720. }
  721. if (plat->variant == U300_GPIO_COH901335) {
  722. dev_info(gpio->dev,
  723. "initializing GPIO Controller COH 901 335\n");
  724. gpio->stride = U300_335_PORT_STRIDE;
  725. gpio->pcr = U300_335_PXPCR;
  726. gpio->dor = U300_335_PXPDOR;
  727. gpio->dir = U300_335_PXPDIR;
  728. gpio->per = U300_335_PXPER;
  729. gpio->icr = U300_335_PXICR;
  730. gpio->ien = U300_335_PXIEN;
  731. gpio->iev = U300_335_PXIEV;
  732. ifr = U300_335_PXIFR;
  733. /* Turn on the GPIO block */
  734. writel(U300_335_CR_BLOCK_CLOCK_ENABLE,
  735. gpio->base + U300_335_CR);
  736. } else if (plat->variant == U300_GPIO_COH901571_3_BS335 ||
  737. plat->variant == U300_GPIO_COH901571_3_BS365) {
  738. dev_info(gpio->dev,
  739. "initializing GPIO Controller COH 901 571/3\n");
  740. gpio->stride = U300_571_PORT_STRIDE;
  741. gpio->pcr = U300_571_PXPCR;
  742. gpio->dor = U300_571_PXPDOR;
  743. gpio->dir = U300_571_PXPDIR;
  744. gpio->per = U300_571_PXPER;
  745. gpio->icr = U300_571_PXICR;
  746. gpio->ien = U300_571_PXIEN;
  747. gpio->iev = U300_571_PXIEV;
  748. ifr = U300_571_PXIFR;
  749. val = readl(gpio->base + U300_571_CR);
  750. dev_info(gpio->dev, "COH901571/3 block version: %d, " \
  751. "number of cores: %d totalling %d pins\n",
  752. ((val & 0x000001FC) >> 2),
  753. ((val & 0x0000FE00) >> 9),
  754. ((val & 0x0000FE00) >> 9) * 8);
  755. writel(U300_571_CR_BLOCK_CLKRQ_ENABLE,
  756. gpio->base + U300_571_CR);
  757. u300_gpio_init_coh901571(gpio, plat);
  758. } else {
  759. dev_err(gpio->dev, "unknown block variant\n");
  760. err = -ENODEV;
  761. goto err_unknown_variant;
  762. }
  763. /* Add each port with its IRQ separately */
  764. INIT_LIST_HEAD(&gpio->port_list);
  765. for (portno = 0 ; portno < plat->ports; portno++) {
  766. struct u300_gpio_port *port =
  767. kmalloc(sizeof(struct u300_gpio_port), GFP_KERNEL);
  768. if (!port) {
  769. dev_err(gpio->dev, "out of memory\n");
  770. err = -ENOMEM;
  771. goto err_no_port;
  772. }
  773. snprintf(port->name, 8, "gpio%d", portno);
  774. port->number = portno;
  775. port->gpio = gpio;
  776. port->irq = platform_get_irq_byname(pdev,
  777. port->name);
  778. dev_dbg(gpio->dev, "register IRQ %d for %s\n", port->irq,
  779. port->name);
  780. irq_set_chained_handler(port->irq, u300_gpio_irq_handler);
  781. irq_set_handler_data(port->irq, port);
  782. /* For each GPIO pin set the unique IRQ handler */
  783. for (i = 0; i < U300_GPIO_PINS_PER_PORT; i++) {
  784. int irqno = gpio->irq_base + (portno << 3) + i;
  785. dev_dbg(gpio->dev, "handler for IRQ %d on %s\n",
  786. irqno, port->name);
  787. irq_set_chip_and_handler(irqno, &u300_gpio_irqchip,
  788. handle_simple_irq);
  789. set_irq_flags(irqno, IRQF_VALID);
  790. irq_set_chip_data(irqno, port);
  791. }
  792. /* Turns off irq force (test register) for this port */
  793. writel(0x0, gpio->base + portno * gpio->stride + ifr);
  794. list_add_tail(&port->node, &gpio->port_list);
  795. }
  796. dev_dbg(gpio->dev, "initialized %d GPIO ports\n", portno);
  797. err = gpiochip_add(&gpio->chip);
  798. if (err) {
  799. dev_err(gpio->dev, "unable to add gpiochip: %d\n", err);
  800. goto err_no_chip;
  801. }
  802. /* Spawn pin controller device as child of the GPIO, pass gpio chip */
  803. plat->pinctrl_device->dev.platform_data = &gpio->chip;
  804. err = platform_device_register(plat->pinctrl_device);
  805. if (err)
  806. goto err_no_pinctrl;
  807. platform_set_drvdata(pdev, gpio);
  808. return 0;
  809. err_no_pinctrl:
  810. err = gpiochip_remove(&gpio->chip);
  811. err_no_chip:
  812. err_no_port:
  813. u300_gpio_free_ports(gpio);
  814. err_unknown_variant:
  815. iounmap(gpio->base);
  816. err_no_ioremap:
  817. release_mem_region(gpio->memres->start, resource_size(gpio->memres));
  818. err_no_ioregion:
  819. err_no_resource:
  820. clk_disable(gpio->clk);
  821. err_no_clk_enable:
  822. clk_put(gpio->clk);
  823. err_no_clk:
  824. kfree(gpio);
  825. dev_info(&pdev->dev, "module ERROR:%d\n", err);
  826. return err;
  827. }
  828. static int __exit u300_gpio_remove(struct platform_device *pdev)
  829. {
  830. struct u300_gpio_platform *plat = dev_get_platdata(&pdev->dev);
  831. struct u300_gpio *gpio = platform_get_drvdata(pdev);
  832. int err;
  833. /* Turn off the GPIO block */
  834. if (plat->variant == U300_GPIO_COH901335)
  835. writel(0x00000000U, gpio->base + U300_335_CR);
  836. if (plat->variant == U300_GPIO_COH901571_3_BS335 ||
  837. plat->variant == U300_GPIO_COH901571_3_BS365)
  838. writel(0x00000000U, gpio->base + U300_571_CR);
  839. err = gpiochip_remove(&gpio->chip);
  840. if (err < 0) {
  841. dev_err(gpio->dev, "unable to remove gpiochip: %d\n", err);
  842. return err;
  843. }
  844. u300_gpio_free_ports(gpio);
  845. iounmap(gpio->base);
  846. release_mem_region(gpio->memres->start,
  847. resource_size(gpio->memres));
  848. clk_disable(gpio->clk);
  849. clk_put(gpio->clk);
  850. platform_set_drvdata(pdev, NULL);
  851. kfree(gpio);
  852. return 0;
  853. }
  854. static struct platform_driver u300_gpio_driver = {
  855. .driver = {
  856. .name = "u300-gpio",
  857. },
  858. .remove = __exit_p(u300_gpio_remove),
  859. };
  860. static int __init u300_gpio_init(void)
  861. {
  862. return platform_driver_probe(&u300_gpio_driver, u300_gpio_probe);
  863. }
  864. static void __exit u300_gpio_exit(void)
  865. {
  866. platform_driver_unregister(&u300_gpio_driver);
  867. }
  868. arch_initcall(u300_gpio_init);
  869. module_exit(u300_gpio_exit);
  870. MODULE_AUTHOR("Linus Walleij <linus.walleij@stericsson.com>");
  871. MODULE_DESCRIPTION("ST-Ericsson AB COH 901 335/COH 901 571/3 GPIO driver");
  872. MODULE_LICENSE("GPL");