pinctrl-plgpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * SPEAr platform PLGPIO driver
  3. *
  4. * Copyright (C) 2012 ST Microelectronics
  5. * Viresh Kumar <viresh.kumar@linaro.org>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/err.h>
  13. #include <linux/gpio.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/module.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm.h>
  21. #include <linux/spinlock.h>
  22. #include <asm/mach/irq.h>
  23. #define MAX_GPIO_PER_REG 32
  24. #define PIN_OFFSET(pin) (pin % MAX_GPIO_PER_REG)
  25. #define REG_OFFSET(base, reg, pin) (base + reg + (pin / MAX_GPIO_PER_REG) \
  26. * sizeof(int *))
  27. /*
  28. * plgpio pins in all machines are not one to one mapped, bitwise with registers
  29. * bits. These set of macros define register masks for which below functions
  30. * (pin_to_offset and offset_to_pin) are required to be called.
  31. */
  32. #define PTO_ENB_REG 0x001
  33. #define PTO_WDATA_REG 0x002
  34. #define PTO_DIR_REG 0x004
  35. #define PTO_IE_REG 0x008
  36. #define PTO_RDATA_REG 0x010
  37. #define PTO_MIS_REG 0x020
  38. struct plgpio_regs {
  39. u32 enb; /* enable register */
  40. u32 wdata; /* write data register */
  41. u32 dir; /* direction set register */
  42. u32 rdata; /* read data register */
  43. u32 ie; /* interrupt enable register */
  44. u32 mis; /* mask interrupt status register */
  45. u32 eit; /* edge interrupt type */
  46. };
  47. /*
  48. * struct plgpio: plgpio driver specific structure
  49. *
  50. * lock: lock for guarding gpio registers
  51. * base: base address of plgpio block
  52. * irq_base: irq number of plgpio0
  53. * chip: gpio framework specific chip information structure
  54. * p2o: function ptr for pin to offset conversion. This is required only for
  55. * machines where mapping b/w pin and offset is not 1-to-1.
  56. * o2p: function ptr for offset to pin conversion. This is required only for
  57. * machines where mapping b/w pin and offset is not 1-to-1.
  58. * p2o_regs: mask of registers for which p2o and o2p are applicable
  59. * regs: register offsets
  60. * csave_regs: context save registers for standby/sleep/hibernate cases
  61. */
  62. struct plgpio {
  63. spinlock_t lock;
  64. void __iomem *base;
  65. struct clk *clk;
  66. unsigned irq_base;
  67. struct irq_domain *irq_domain;
  68. struct gpio_chip chip;
  69. int (*p2o)(int pin); /* pin_to_offset */
  70. int (*o2p)(int offset); /* offset_to_pin */
  71. u32 p2o_regs;
  72. struct plgpio_regs regs;
  73. #ifdef CONFIG_PM
  74. struct plgpio_regs *csave_regs;
  75. #endif
  76. };
  77. /* register manipulation inline functions */
  78. static inline u32 is_plgpio_set(void __iomem *base, u32 pin, u32 reg)
  79. {
  80. u32 offset = PIN_OFFSET(pin);
  81. void __iomem *reg_off = REG_OFFSET(base, reg, pin);
  82. u32 val = readl_relaxed(reg_off);
  83. return !!(val & (1 << offset));
  84. }
  85. static inline void plgpio_reg_set(void __iomem *base, u32 pin, u32 reg)
  86. {
  87. u32 offset = PIN_OFFSET(pin);
  88. void __iomem *reg_off = REG_OFFSET(base, reg, pin);
  89. u32 val = readl_relaxed(reg_off);
  90. writel_relaxed(val | (1 << offset), reg_off);
  91. }
  92. static inline void plgpio_reg_reset(void __iomem *base, u32 pin, u32 reg)
  93. {
  94. u32 offset = PIN_OFFSET(pin);
  95. void __iomem *reg_off = REG_OFFSET(base, reg, pin);
  96. u32 val = readl_relaxed(reg_off);
  97. writel_relaxed(val & ~(1 << offset), reg_off);
  98. }
  99. /* gpio framework specific routines */
  100. static int plgpio_direction_input(struct gpio_chip *chip, unsigned offset)
  101. {
  102. struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
  103. unsigned long flags;
  104. /* get correct offset for "offset" pin */
  105. if (plgpio->p2o && (plgpio->p2o_regs & PTO_DIR_REG)) {
  106. offset = plgpio->p2o(offset);
  107. if (offset == -1)
  108. return -EINVAL;
  109. }
  110. spin_lock_irqsave(&plgpio->lock, flags);
  111. plgpio_reg_set(plgpio->base, offset, plgpio->regs.dir);
  112. spin_unlock_irqrestore(&plgpio->lock, flags);
  113. return 0;
  114. }
  115. static int plgpio_direction_output(struct gpio_chip *chip, unsigned offset,
  116. int value)
  117. {
  118. struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
  119. unsigned long flags;
  120. unsigned dir_offset = offset, wdata_offset = offset, tmp;
  121. /* get correct offset for "offset" pin */
  122. if (plgpio->p2o && (plgpio->p2o_regs & (PTO_DIR_REG | PTO_WDATA_REG))) {
  123. tmp = plgpio->p2o(offset);
  124. if (tmp == -1)
  125. return -EINVAL;
  126. if (plgpio->p2o_regs & PTO_DIR_REG)
  127. dir_offset = tmp;
  128. if (plgpio->p2o_regs & PTO_WDATA_REG)
  129. wdata_offset = tmp;
  130. }
  131. spin_lock_irqsave(&plgpio->lock, flags);
  132. if (value)
  133. plgpio_reg_set(plgpio->base, wdata_offset,
  134. plgpio->regs.wdata);
  135. else
  136. plgpio_reg_reset(plgpio->base, wdata_offset,
  137. plgpio->regs.wdata);
  138. plgpio_reg_reset(plgpio->base, dir_offset, plgpio->regs.dir);
  139. spin_unlock_irqrestore(&plgpio->lock, flags);
  140. return 0;
  141. }
  142. static int plgpio_get_value(struct gpio_chip *chip, unsigned offset)
  143. {
  144. struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
  145. if (offset >= chip->ngpio)
  146. return -EINVAL;
  147. /* get correct offset for "offset" pin */
  148. if (plgpio->p2o && (plgpio->p2o_regs & PTO_RDATA_REG)) {
  149. offset = plgpio->p2o(offset);
  150. if (offset == -1)
  151. return -EINVAL;
  152. }
  153. return is_plgpio_set(plgpio->base, offset, plgpio->regs.rdata);
  154. }
  155. static void plgpio_set_value(struct gpio_chip *chip, unsigned offset, int value)
  156. {
  157. struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
  158. if (offset >= chip->ngpio)
  159. return;
  160. /* get correct offset for "offset" pin */
  161. if (plgpio->p2o && (plgpio->p2o_regs & PTO_WDATA_REG)) {
  162. offset = plgpio->p2o(offset);
  163. if (offset == -1)
  164. return;
  165. }
  166. if (value)
  167. plgpio_reg_set(plgpio->base, offset, plgpio->regs.wdata);
  168. else
  169. plgpio_reg_reset(plgpio->base, offset, plgpio->regs.wdata);
  170. }
  171. static int plgpio_request(struct gpio_chip *chip, unsigned offset)
  172. {
  173. struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
  174. int gpio = chip->base + offset;
  175. unsigned long flags;
  176. int ret = 0;
  177. if (offset >= chip->ngpio)
  178. return -EINVAL;
  179. ret = pinctrl_request_gpio(gpio);
  180. if (ret)
  181. return ret;
  182. if (!IS_ERR(plgpio->clk)) {
  183. ret = clk_enable(plgpio->clk);
  184. if (ret)
  185. goto err0;
  186. }
  187. if (plgpio->regs.enb == -1)
  188. return 0;
  189. /*
  190. * put gpio in IN mode before enabling it. This make enabling gpio safe
  191. */
  192. ret = plgpio_direction_input(chip, offset);
  193. if (ret)
  194. goto err1;
  195. /* get correct offset for "offset" pin */
  196. if (plgpio->p2o && (plgpio->p2o_regs & PTO_ENB_REG)) {
  197. offset = plgpio->p2o(offset);
  198. if (offset == -1) {
  199. ret = -EINVAL;
  200. goto err1;
  201. }
  202. }
  203. spin_lock_irqsave(&plgpio->lock, flags);
  204. plgpio_reg_set(plgpio->base, offset, plgpio->regs.enb);
  205. spin_unlock_irqrestore(&plgpio->lock, flags);
  206. return 0;
  207. err1:
  208. if (!IS_ERR(plgpio->clk))
  209. clk_disable(plgpio->clk);
  210. err0:
  211. pinctrl_free_gpio(gpio);
  212. return ret;
  213. }
  214. static void plgpio_free(struct gpio_chip *chip, unsigned offset)
  215. {
  216. struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
  217. int gpio = chip->base + offset;
  218. unsigned long flags;
  219. if (offset >= chip->ngpio)
  220. return;
  221. if (plgpio->regs.enb == -1)
  222. goto disable_clk;
  223. /* get correct offset for "offset" pin */
  224. if (plgpio->p2o && (plgpio->p2o_regs & PTO_ENB_REG)) {
  225. offset = plgpio->p2o(offset);
  226. if (offset == -1)
  227. return;
  228. }
  229. spin_lock_irqsave(&plgpio->lock, flags);
  230. plgpio_reg_reset(plgpio->base, offset, plgpio->regs.enb);
  231. spin_unlock_irqrestore(&plgpio->lock, flags);
  232. disable_clk:
  233. if (!IS_ERR(plgpio->clk))
  234. clk_disable(plgpio->clk);
  235. pinctrl_free_gpio(gpio);
  236. }
  237. static int plgpio_to_irq(struct gpio_chip *chip, unsigned offset)
  238. {
  239. struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
  240. if (IS_ERR_VALUE(plgpio->irq_base))
  241. return -EINVAL;
  242. return irq_find_mapping(plgpio->irq_domain, offset);
  243. }
  244. /* PLGPIO IRQ */
  245. static void plgpio_irq_disable(struct irq_data *d)
  246. {
  247. struct plgpio *plgpio = irq_data_get_irq_chip_data(d);
  248. int offset = d->irq - plgpio->irq_base;
  249. unsigned long flags;
  250. /* get correct offset for "offset" pin */
  251. if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
  252. offset = plgpio->p2o(offset);
  253. if (offset == -1)
  254. return;
  255. }
  256. spin_lock_irqsave(&plgpio->lock, flags);
  257. plgpio_reg_set(plgpio->base, offset, plgpio->regs.ie);
  258. spin_unlock_irqrestore(&plgpio->lock, flags);
  259. }
  260. static void plgpio_irq_enable(struct irq_data *d)
  261. {
  262. struct plgpio *plgpio = irq_data_get_irq_chip_data(d);
  263. int offset = d->irq - plgpio->irq_base;
  264. unsigned long flags;
  265. /* get correct offset for "offset" pin */
  266. if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
  267. offset = plgpio->p2o(offset);
  268. if (offset == -1)
  269. return;
  270. }
  271. spin_lock_irqsave(&plgpio->lock, flags);
  272. plgpio_reg_reset(plgpio->base, offset, plgpio->regs.ie);
  273. spin_unlock_irqrestore(&plgpio->lock, flags);
  274. }
  275. static int plgpio_irq_set_type(struct irq_data *d, unsigned trigger)
  276. {
  277. struct plgpio *plgpio = irq_data_get_irq_chip_data(d);
  278. int offset = d->irq - plgpio->irq_base;
  279. void __iomem *reg_off;
  280. unsigned int supported_type = 0, val;
  281. if (offset >= plgpio->chip.ngpio)
  282. return -EINVAL;
  283. if (plgpio->regs.eit == -1)
  284. supported_type = IRQ_TYPE_LEVEL_HIGH;
  285. else
  286. supported_type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  287. if (!(trigger & supported_type))
  288. return -EINVAL;
  289. if (plgpio->regs.eit == -1)
  290. return 0;
  291. reg_off = REG_OFFSET(plgpio->base, plgpio->regs.eit, offset);
  292. val = readl_relaxed(reg_off);
  293. offset = PIN_OFFSET(offset);
  294. if (trigger & IRQ_TYPE_EDGE_RISING)
  295. writel_relaxed(val | (1 << offset), reg_off);
  296. else
  297. writel_relaxed(val & ~(1 << offset), reg_off);
  298. return 0;
  299. }
  300. static struct irq_chip plgpio_irqchip = {
  301. .name = "PLGPIO",
  302. .irq_enable = plgpio_irq_enable,
  303. .irq_disable = plgpio_irq_disable,
  304. .irq_set_type = plgpio_irq_set_type,
  305. };
  306. static void plgpio_irq_handler(unsigned irq, struct irq_desc *desc)
  307. {
  308. struct plgpio *plgpio = irq_get_handler_data(irq);
  309. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  310. int regs_count, count, pin, offset, i = 0;
  311. unsigned long pending;
  312. count = plgpio->chip.ngpio;
  313. regs_count = DIV_ROUND_UP(count, MAX_GPIO_PER_REG);
  314. chained_irq_enter(irqchip, desc);
  315. /* check all plgpio MIS registers for a possible interrupt */
  316. for (; i < regs_count; i++) {
  317. pending = readl_relaxed(plgpio->base + plgpio->regs.mis +
  318. i * sizeof(int *));
  319. if (!pending)
  320. continue;
  321. /* clear interrupts */
  322. writel_relaxed(~pending, plgpio->base + plgpio->regs.mis +
  323. i * sizeof(int *));
  324. /*
  325. * clear extra bits in last register having gpios < MAX/REG
  326. * ex: Suppose there are max 102 plgpios. then last register
  327. * must have only (102 - MAX_GPIO_PER_REG * 3) = 6 relevant bits
  328. * so, we must not take other 28 bits into consideration for
  329. * checking interrupt. so clear those bits.
  330. */
  331. count = count - i * MAX_GPIO_PER_REG;
  332. if (count < MAX_GPIO_PER_REG)
  333. pending &= (1 << count) - 1;
  334. for_each_set_bit(offset, &pending, MAX_GPIO_PER_REG) {
  335. /* get correct pin for "offset" */
  336. if (plgpio->o2p && (plgpio->p2o_regs & PTO_MIS_REG)) {
  337. pin = plgpio->o2p(offset);
  338. if (pin == -1)
  339. continue;
  340. } else
  341. pin = offset;
  342. /* get correct irq line number */
  343. pin = i * MAX_GPIO_PER_REG + pin;
  344. generic_handle_irq(plgpio_to_irq(&plgpio->chip, pin));
  345. }
  346. }
  347. chained_irq_exit(irqchip, desc);
  348. }
  349. /*
  350. * pin to offset and offset to pin converter functions
  351. *
  352. * In spear310 there is inconsistency among bit positions in plgpio regiseters,
  353. * for different plgpio pins. For example: for pin 27, bit offset is 23, pin
  354. * 28-33 are not supported, pin 95 has offset bit 95, bit 100 has offset bit 1
  355. */
  356. static int spear310_p2o(int pin)
  357. {
  358. int offset = pin;
  359. if (pin <= 27)
  360. offset += 4;
  361. else if (pin <= 33)
  362. offset = -1;
  363. else if (pin <= 97)
  364. offset -= 2;
  365. else if (pin <= 101)
  366. offset = 101 - pin;
  367. else
  368. offset = -1;
  369. return offset;
  370. }
  371. int spear310_o2p(int offset)
  372. {
  373. if (offset <= 3)
  374. return 101 - offset;
  375. else if (offset <= 31)
  376. return offset - 4;
  377. else
  378. return offset + 2;
  379. }
  380. static int plgpio_probe_dt(struct platform_device *pdev, struct plgpio *plgpio)
  381. {
  382. struct device_node *np = pdev->dev.of_node;
  383. int ret = -EINVAL;
  384. u32 val;
  385. if (of_machine_is_compatible("st,spear310")) {
  386. plgpio->p2o = spear310_p2o;
  387. plgpio->o2p = spear310_o2p;
  388. plgpio->p2o_regs = PTO_WDATA_REG | PTO_DIR_REG | PTO_IE_REG |
  389. PTO_RDATA_REG | PTO_MIS_REG;
  390. }
  391. if (!of_property_read_u32(np, "st-plgpio,ngpio", &val)) {
  392. plgpio->chip.ngpio = val;
  393. } else {
  394. dev_err(&pdev->dev, "DT: Invalid ngpio field\n");
  395. goto end;
  396. }
  397. if (!of_property_read_u32(np, "st-plgpio,enb-reg", &val))
  398. plgpio->regs.enb = val;
  399. else
  400. plgpio->regs.enb = -1;
  401. if (!of_property_read_u32(np, "st-plgpio,wdata-reg", &val)) {
  402. plgpio->regs.wdata = val;
  403. } else {
  404. dev_err(&pdev->dev, "DT: Invalid wdata reg\n");
  405. goto end;
  406. }
  407. if (!of_property_read_u32(np, "st-plgpio,dir-reg", &val)) {
  408. plgpio->regs.dir = val;
  409. } else {
  410. dev_err(&pdev->dev, "DT: Invalid dir reg\n");
  411. goto end;
  412. }
  413. if (!of_property_read_u32(np, "st-plgpio,ie-reg", &val)) {
  414. plgpio->regs.ie = val;
  415. } else {
  416. dev_err(&pdev->dev, "DT: Invalid ie reg\n");
  417. goto end;
  418. }
  419. if (!of_property_read_u32(np, "st-plgpio,rdata-reg", &val)) {
  420. plgpio->regs.rdata = val;
  421. } else {
  422. dev_err(&pdev->dev, "DT: Invalid rdata reg\n");
  423. goto end;
  424. }
  425. if (!of_property_read_u32(np, "st-plgpio,mis-reg", &val)) {
  426. plgpio->regs.mis = val;
  427. } else {
  428. dev_err(&pdev->dev, "DT: Invalid mis reg\n");
  429. goto end;
  430. }
  431. if (!of_property_read_u32(np, "st-plgpio,eit-reg", &val))
  432. plgpio->regs.eit = val;
  433. else
  434. plgpio->regs.eit = -1;
  435. return 0;
  436. end:
  437. return ret;
  438. }
  439. static int plgpio_probe(struct platform_device *pdev)
  440. {
  441. struct device_node *np = pdev->dev.of_node;
  442. struct plgpio *plgpio;
  443. struct resource *res;
  444. int ret, irq, i;
  445. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  446. if (!res) {
  447. dev_err(&pdev->dev, "invalid IORESOURCE_MEM\n");
  448. return -EBUSY;
  449. }
  450. plgpio = devm_kzalloc(&pdev->dev, sizeof(*plgpio), GFP_KERNEL);
  451. if (!plgpio) {
  452. dev_err(&pdev->dev, "memory allocation fail\n");
  453. return -ENOMEM;
  454. }
  455. plgpio->base = devm_ioremap_resource(&pdev->dev, res);
  456. if (IS_ERR(plgpio->base))
  457. return PTR_ERR(plgpio->base);
  458. ret = plgpio_probe_dt(pdev, plgpio);
  459. if (ret) {
  460. dev_err(&pdev->dev, "DT probe failed\n");
  461. return ret;
  462. }
  463. plgpio->clk = devm_clk_get(&pdev->dev, NULL);
  464. if (IS_ERR(plgpio->clk))
  465. dev_warn(&pdev->dev, "clk_get() failed, work without it\n");
  466. #ifdef CONFIG_PM
  467. plgpio->csave_regs = devm_kzalloc(&pdev->dev,
  468. sizeof(*plgpio->csave_regs) *
  469. DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG),
  470. GFP_KERNEL);
  471. if (!plgpio->csave_regs) {
  472. dev_err(&pdev->dev, "csave registers memory allocation fail\n");
  473. return -ENOMEM;
  474. }
  475. #endif
  476. platform_set_drvdata(pdev, plgpio);
  477. spin_lock_init(&plgpio->lock);
  478. plgpio->irq_base = -1;
  479. plgpio->chip.base = -1;
  480. plgpio->chip.request = plgpio_request;
  481. plgpio->chip.free = plgpio_free;
  482. plgpio->chip.direction_input = plgpio_direction_input;
  483. plgpio->chip.direction_output = plgpio_direction_output;
  484. plgpio->chip.get = plgpio_get_value;
  485. plgpio->chip.set = plgpio_set_value;
  486. plgpio->chip.to_irq = plgpio_to_irq;
  487. plgpio->chip.label = dev_name(&pdev->dev);
  488. plgpio->chip.dev = &pdev->dev;
  489. plgpio->chip.owner = THIS_MODULE;
  490. if (!IS_ERR(plgpio->clk)) {
  491. ret = clk_prepare(plgpio->clk);
  492. if (ret) {
  493. dev_err(&pdev->dev, "clk prepare failed\n");
  494. return ret;
  495. }
  496. }
  497. ret = gpiochip_add(&plgpio->chip);
  498. if (ret) {
  499. dev_err(&pdev->dev, "unable to add gpio chip\n");
  500. goto unprepare_clk;
  501. }
  502. irq = platform_get_irq(pdev, 0);
  503. if (irq < 0) {
  504. dev_info(&pdev->dev, "irqs not supported\n");
  505. return 0;
  506. }
  507. plgpio->irq_base = irq_alloc_descs(-1, 0, plgpio->chip.ngpio, 0);
  508. if (IS_ERR_VALUE(plgpio->irq_base)) {
  509. /* we would not support irq for gpio */
  510. dev_warn(&pdev->dev, "couldn't allocate irq base\n");
  511. return 0;
  512. }
  513. plgpio->irq_domain = irq_domain_add_legacy(np, plgpio->chip.ngpio,
  514. plgpio->irq_base, 0, &irq_domain_simple_ops, NULL);
  515. if (WARN_ON(!plgpio->irq_domain)) {
  516. dev_err(&pdev->dev, "irq domain init failed\n");
  517. irq_free_descs(plgpio->irq_base, plgpio->chip.ngpio);
  518. ret = -ENXIO;
  519. goto remove_gpiochip;
  520. }
  521. irq_set_chained_handler(irq, plgpio_irq_handler);
  522. for (i = 0; i < plgpio->chip.ngpio; i++) {
  523. irq_set_chip_and_handler(i + plgpio->irq_base, &plgpio_irqchip,
  524. handle_simple_irq);
  525. set_irq_flags(i + plgpio->irq_base, IRQF_VALID);
  526. irq_set_chip_data(i + plgpio->irq_base, plgpio);
  527. }
  528. irq_set_handler_data(irq, plgpio);
  529. dev_info(&pdev->dev, "PLGPIO registered with IRQs\n");
  530. return 0;
  531. remove_gpiochip:
  532. dev_info(&pdev->dev, "Remove gpiochip\n");
  533. if (gpiochip_remove(&plgpio->chip))
  534. dev_err(&pdev->dev, "unable to remove gpiochip\n");
  535. unprepare_clk:
  536. if (!IS_ERR(plgpio->clk))
  537. clk_unprepare(plgpio->clk);
  538. return ret;
  539. }
  540. #ifdef CONFIG_PM
  541. static int plgpio_suspend(struct device *dev)
  542. {
  543. struct plgpio *plgpio = dev_get_drvdata(dev);
  544. int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
  545. void __iomem *off;
  546. for (i = 0; i < reg_count; i++) {
  547. off = plgpio->base + i * sizeof(int *);
  548. if (plgpio->regs.enb != -1)
  549. plgpio->csave_regs[i].enb =
  550. readl_relaxed(plgpio->regs.enb + off);
  551. if (plgpio->regs.eit != -1)
  552. plgpio->csave_regs[i].eit =
  553. readl_relaxed(plgpio->regs.eit + off);
  554. plgpio->csave_regs[i].wdata = readl_relaxed(plgpio->regs.wdata +
  555. off);
  556. plgpio->csave_regs[i].dir = readl_relaxed(plgpio->regs.dir +
  557. off);
  558. plgpio->csave_regs[i].ie = readl_relaxed(plgpio->regs.ie + off);
  559. }
  560. return 0;
  561. }
  562. /*
  563. * This is used to correct the values in end registers. End registers contain
  564. * extra bits that might be used for other purpose in platform. So, we shouldn't
  565. * overwrite these bits. This macro, reads given register again, preserves other
  566. * bit values (non-plgpio bits), and retain captured value (plgpio bits).
  567. */
  568. #define plgpio_prepare_reg(__reg, _off, _mask, _tmp) \
  569. { \
  570. _tmp = readl_relaxed(plgpio->regs.__reg + _off); \
  571. _tmp &= ~_mask; \
  572. plgpio->csave_regs[i].__reg = \
  573. _tmp | (plgpio->csave_regs[i].__reg & _mask); \
  574. }
  575. static int plgpio_resume(struct device *dev)
  576. {
  577. struct plgpio *plgpio = dev_get_drvdata(dev);
  578. int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
  579. void __iomem *off;
  580. u32 mask, tmp;
  581. for (i = 0; i < reg_count; i++) {
  582. off = plgpio->base + i * sizeof(int *);
  583. if (i == reg_count - 1) {
  584. mask = (1 << (plgpio->chip.ngpio - i *
  585. MAX_GPIO_PER_REG)) - 1;
  586. if (plgpio->regs.enb != -1)
  587. plgpio_prepare_reg(enb, off, mask, tmp);
  588. if (plgpio->regs.eit != -1)
  589. plgpio_prepare_reg(eit, off, mask, tmp);
  590. plgpio_prepare_reg(wdata, off, mask, tmp);
  591. plgpio_prepare_reg(dir, off, mask, tmp);
  592. plgpio_prepare_reg(ie, off, mask, tmp);
  593. }
  594. writel_relaxed(plgpio->csave_regs[i].wdata, plgpio->regs.wdata +
  595. off);
  596. writel_relaxed(plgpio->csave_regs[i].dir, plgpio->regs.dir +
  597. off);
  598. if (plgpio->regs.eit != -1)
  599. writel_relaxed(plgpio->csave_regs[i].eit,
  600. plgpio->regs.eit + off);
  601. writel_relaxed(plgpio->csave_regs[i].ie, plgpio->regs.ie + off);
  602. if (plgpio->regs.enb != -1)
  603. writel_relaxed(plgpio->csave_regs[i].enb,
  604. plgpio->regs.enb + off);
  605. }
  606. return 0;
  607. }
  608. #endif
  609. static SIMPLE_DEV_PM_OPS(plgpio_dev_pm_ops, plgpio_suspend, plgpio_resume);
  610. static const struct of_device_id plgpio_of_match[] = {
  611. { .compatible = "st,spear-plgpio" },
  612. {}
  613. };
  614. MODULE_DEVICE_TABLE(of, plgpio_of_match);
  615. static struct platform_driver plgpio_driver = {
  616. .probe = plgpio_probe,
  617. .driver = {
  618. .owner = THIS_MODULE,
  619. .name = "spear-plgpio",
  620. .pm = &plgpio_dev_pm_ops,
  621. .of_match_table = of_match_ptr(plgpio_of_match),
  622. },
  623. };
  624. static int __init plgpio_init(void)
  625. {
  626. return platform_driver_register(&plgpio_driver);
  627. }
  628. subsys_initcall(plgpio_init);
  629. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
  630. MODULE_DESCRIPTION("ST Microlectronics SPEAr PLGPIO driver");
  631. MODULE_LICENSE("GPL");