pinctrl-plgpio.c 19 KB

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