pinctrl-plgpio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 void plgpio_set_value(struct gpio_chip *chip, unsigned offset, int value)
  157. {
  158. struct plgpio *plgpio = gpiochip_get_data(chip);
  159. if (offset >= chip->ngpio)
  160. return;
  161. /* get correct offset for "offset" pin */
  162. if (plgpio->p2o && (plgpio->p2o_regs & PTO_WDATA_REG)) {
  163. offset = plgpio->p2o(offset);
  164. if (offset == -1)
  165. return;
  166. }
  167. if (value)
  168. plgpio_reg_set(plgpio->regmap, offset, plgpio->regs.wdata);
  169. else
  170. plgpio_reg_reset(plgpio->regmap, offset, plgpio->regs.wdata);
  171. }
  172. static int plgpio_request(struct gpio_chip *chip, unsigned offset)
  173. {
  174. struct plgpio *plgpio = gpiochip_get_data(chip);
  175. unsigned long flags;
  176. int ret = 0;
  177. if (offset >= chip->ngpio)
  178. return -EINVAL;
  179. ret = pinctrl_gpio_request(chip, offset);
  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->regmap, 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_gpio_free(chip, offset);
  212. return ret;
  213. }
  214. static void plgpio_free(struct gpio_chip *chip, unsigned offset)
  215. {
  216. struct plgpio *plgpio = gpiochip_get_data(chip);
  217. unsigned long flags;
  218. if (offset >= chip->ngpio)
  219. return;
  220. if (plgpio->regs.enb == -1)
  221. goto disable_clk;
  222. /* get correct offset for "offset" pin */
  223. if (plgpio->p2o && (plgpio->p2o_regs & PTO_ENB_REG)) {
  224. offset = plgpio->p2o(offset);
  225. if (offset == -1)
  226. return;
  227. }
  228. spin_lock_irqsave(&plgpio->lock, flags);
  229. plgpio_reg_reset(plgpio->regmap, offset, plgpio->regs.enb);
  230. spin_unlock_irqrestore(&plgpio->lock, flags);
  231. disable_clk:
  232. if (!IS_ERR(plgpio->clk))
  233. clk_disable(plgpio->clk);
  234. pinctrl_gpio_free(chip, offset);
  235. }
  236. /* PLGPIO IRQ */
  237. static void plgpio_irq_disable(struct irq_data *d)
  238. {
  239. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  240. struct plgpio *plgpio = gpiochip_get_data(gc);
  241. int offset = d->hwirq;
  242. unsigned long flags;
  243. /* get correct offset for "offset" pin */
  244. if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
  245. offset = plgpio->p2o(offset);
  246. if (offset == -1)
  247. return;
  248. }
  249. spin_lock_irqsave(&plgpio->lock, flags);
  250. plgpio_reg_set(plgpio->regmap, offset, plgpio->regs.ie);
  251. spin_unlock_irqrestore(&plgpio->lock, flags);
  252. gpiochip_disable_irq(gc, irqd_to_hwirq(d));
  253. }
  254. static void plgpio_irq_enable(struct irq_data *d)
  255. {
  256. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  257. struct plgpio *plgpio = gpiochip_get_data(gc);
  258. int offset = d->hwirq;
  259. unsigned long flags;
  260. /* get correct offset for "offset" pin */
  261. if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
  262. offset = plgpio->p2o(offset);
  263. if (offset == -1)
  264. return;
  265. }
  266. gpiochip_enable_irq(gc, irqd_to_hwirq(d));
  267. spin_lock_irqsave(&plgpio->lock, flags);
  268. plgpio_reg_reset(plgpio->regmap, offset, plgpio->regs.ie);
  269. spin_unlock_irqrestore(&plgpio->lock, flags);
  270. }
  271. static int plgpio_irq_set_type(struct irq_data *d, unsigned trigger)
  272. {
  273. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  274. struct plgpio *plgpio = gpiochip_get_data(gc);
  275. int offset = d->hwirq;
  276. u32 reg_off;
  277. unsigned int supported_type = 0, val;
  278. if (offset >= plgpio->chip.ngpio)
  279. return -EINVAL;
  280. if (plgpio->regs.eit == -1)
  281. supported_type = IRQ_TYPE_LEVEL_HIGH;
  282. else
  283. supported_type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  284. if (!(trigger & supported_type))
  285. return -EINVAL;
  286. if (plgpio->regs.eit == -1)
  287. return 0;
  288. reg_off = REG_OFFSET(0, plgpio->regs.eit, offset);
  289. regmap_read(plgpio->regmap, reg_off, &val);
  290. offset = PIN_OFFSET(offset);
  291. if (trigger & IRQ_TYPE_EDGE_RISING)
  292. regmap_write(plgpio->regmap, reg_off, val | (1 << offset));
  293. else
  294. regmap_write(plgpio->regmap, reg_off, val & ~(1 << offset));
  295. return 0;
  296. }
  297. static const struct irq_chip plgpio_irqchip = {
  298. .name = "PLGPIO",
  299. .irq_enable = plgpio_irq_enable,
  300. .irq_disable = plgpio_irq_disable,
  301. .irq_set_type = plgpio_irq_set_type,
  302. .flags = IRQCHIP_IMMUTABLE,
  303. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  304. };
  305. static void plgpio_irq_handler(struct irq_desc *desc)
  306. {
  307. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  308. struct plgpio *plgpio = gpiochip_get_data(gc);
  309. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  310. int regs_count, count, pin, offset, i = 0;
  311. u32 pending;
  312. unsigned long pendingl;
  313. count = plgpio->chip.ngpio;
  314. regs_count = DIV_ROUND_UP(count, MAX_GPIO_PER_REG);
  315. chained_irq_enter(irqchip, desc);
  316. /* check all plgpio MIS registers for a possible interrupt */
  317. for (; i < regs_count; i++) {
  318. regmap_read(plgpio->regmap, plgpio->regs.mis +
  319. i * sizeof(int *), &pending);
  320. if (!pending)
  321. continue;
  322. /* clear interrupts */
  323. regmap_write(plgpio->regmap, plgpio->regs.mis +
  324. i * sizeof(int *), ~pending);
  325. /*
  326. * clear extra bits in last register having gpios < MAX/REG
  327. * ex: Suppose there are max 102 plgpios. then last register
  328. * must have only (102 - MAX_GPIO_PER_REG * 3) = 6 relevant bits
  329. * so, we must not take other 28 bits into consideration for
  330. * checking interrupt. so clear those bits.
  331. */
  332. count = count - i * MAX_GPIO_PER_REG;
  333. if (count < MAX_GPIO_PER_REG)
  334. pending &= (1 << count) - 1;
  335. pendingl = pending;
  336. for_each_set_bit(offset, &pendingl, MAX_GPIO_PER_REG) {
  337. /* get correct pin for "offset" */
  338. if (plgpio->o2p && (plgpio->p2o_regs & PTO_MIS_REG)) {
  339. pin = plgpio->o2p(offset);
  340. if (pin == -1)
  341. continue;
  342. } else
  343. pin = offset;
  344. /* get correct irq line number */
  345. pin = i * MAX_GPIO_PER_REG + pin;
  346. generic_handle_domain_irq(gc->irq.domain, pin);
  347. }
  348. }
  349. chained_irq_exit(irqchip, desc);
  350. }
  351. /*
  352. * pin to offset and offset to pin converter functions
  353. *
  354. * In spear310 there is inconsistency among bit positions in plgpio regiseters,
  355. * for different plgpio pins. For example: for pin 27, bit offset is 23, pin
  356. * 28-33 are not supported, pin 95 has offset bit 95, bit 100 has offset bit 1
  357. */
  358. static int spear310_p2o(int pin)
  359. {
  360. int offset = pin;
  361. if (pin <= 27)
  362. offset += 4;
  363. else if (pin <= 33)
  364. offset = -1;
  365. else if (pin <= 97)
  366. offset -= 2;
  367. else if (pin <= 101)
  368. offset = 101 - pin;
  369. else
  370. offset = -1;
  371. return offset;
  372. }
  373. static int spear310_o2p(int offset)
  374. {
  375. if (offset <= 3)
  376. return 101 - offset;
  377. else if (offset <= 31)
  378. return offset - 4;
  379. else
  380. return offset + 2;
  381. }
  382. static int plgpio_probe_dt(struct platform_device *pdev, struct plgpio *plgpio)
  383. {
  384. struct device_node *np = pdev->dev.of_node;
  385. int ret = -EINVAL;
  386. u32 val;
  387. if (of_machine_is_compatible("st,spear310")) {
  388. plgpio->p2o = spear310_p2o;
  389. plgpio->o2p = spear310_o2p;
  390. plgpio->p2o_regs = PTO_WDATA_REG | PTO_DIR_REG | PTO_IE_REG |
  391. PTO_RDATA_REG | PTO_MIS_REG;
  392. }
  393. if (!of_property_read_u32(np, "st-plgpio,ngpio", &val)) {
  394. plgpio->chip.ngpio = val;
  395. } else {
  396. dev_err(&pdev->dev, "DT: Invalid ngpio field\n");
  397. goto end;
  398. }
  399. if (!of_property_read_u32(np, "st-plgpio,enb-reg", &val))
  400. plgpio->regs.enb = val;
  401. else
  402. plgpio->regs.enb = -1;
  403. if (!of_property_read_u32(np, "st-plgpio,wdata-reg", &val)) {
  404. plgpio->regs.wdata = val;
  405. } else {
  406. dev_err(&pdev->dev, "DT: Invalid wdata reg\n");
  407. goto end;
  408. }
  409. if (!of_property_read_u32(np, "st-plgpio,dir-reg", &val)) {
  410. plgpio->regs.dir = val;
  411. } else {
  412. dev_err(&pdev->dev, "DT: Invalid dir reg\n");
  413. goto end;
  414. }
  415. if (!of_property_read_u32(np, "st-plgpio,ie-reg", &val)) {
  416. plgpio->regs.ie = val;
  417. } else {
  418. dev_err(&pdev->dev, "DT: Invalid ie reg\n");
  419. goto end;
  420. }
  421. if (!of_property_read_u32(np, "st-plgpio,rdata-reg", &val)) {
  422. plgpio->regs.rdata = val;
  423. } else {
  424. dev_err(&pdev->dev, "DT: Invalid rdata reg\n");
  425. goto end;
  426. }
  427. if (!of_property_read_u32(np, "st-plgpio,mis-reg", &val)) {
  428. plgpio->regs.mis = val;
  429. } else {
  430. dev_err(&pdev->dev, "DT: Invalid mis reg\n");
  431. goto end;
  432. }
  433. if (!of_property_read_u32(np, "st-plgpio,eit-reg", &val))
  434. plgpio->regs.eit = val;
  435. else
  436. plgpio->regs.eit = -1;
  437. return 0;
  438. end:
  439. return ret;
  440. }
  441. static int plgpio_probe(struct platform_device *pdev)
  442. {
  443. struct device_node *regmap_np;
  444. struct plgpio *plgpio;
  445. int ret, irq;
  446. plgpio = devm_kzalloc(&pdev->dev, sizeof(*plgpio), GFP_KERNEL);
  447. if (!plgpio)
  448. return -ENOMEM;
  449. regmap_np = of_parse_phandle(pdev->dev.of_node, "regmap", 0);
  450. if (regmap_np) {
  451. plgpio->regmap = device_node_to_regmap(regmap_np);
  452. of_node_put(regmap_np);
  453. if (IS_ERR(plgpio->regmap)) {
  454. dev_err(&pdev->dev, "Retrieve regmap failed (%pe)\n",
  455. plgpio->regmap);
  456. return PTR_ERR(plgpio->regmap);
  457. }
  458. } else {
  459. plgpio->regmap = device_node_to_regmap(pdev->dev.of_node);
  460. if (IS_ERR(plgpio->regmap)) {
  461. dev_err(&pdev->dev, "Init regmap failed (%pe)\n",
  462. plgpio->regmap);
  463. return PTR_ERR(plgpio->regmap);
  464. }
  465. }
  466. ret = plgpio_probe_dt(pdev, plgpio);
  467. if (ret) {
  468. dev_err(&pdev->dev, "DT probe failed\n");
  469. return ret;
  470. }
  471. plgpio->clk = devm_clk_get(&pdev->dev, NULL);
  472. if (IS_ERR(plgpio->clk))
  473. dev_warn(&pdev->dev, "clk_get() failed, work without it\n");
  474. #ifdef CONFIG_PM_SLEEP
  475. plgpio->csave_regs = devm_kcalloc(&pdev->dev,
  476. DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG),
  477. sizeof(*plgpio->csave_regs),
  478. GFP_KERNEL);
  479. if (!plgpio->csave_regs)
  480. return -ENOMEM;
  481. #endif
  482. platform_set_drvdata(pdev, plgpio);
  483. spin_lock_init(&plgpio->lock);
  484. plgpio->chip.base = -1;
  485. plgpio->chip.request = plgpio_request;
  486. plgpio->chip.free = plgpio_free;
  487. plgpio->chip.direction_input = plgpio_direction_input;
  488. plgpio->chip.direction_output = plgpio_direction_output;
  489. plgpio->chip.get = plgpio_get_value;
  490. plgpio->chip.set = plgpio_set_value;
  491. plgpio->chip.label = dev_name(&pdev->dev);
  492. plgpio->chip.parent = &pdev->dev;
  493. plgpio->chip.owner = THIS_MODULE;
  494. if (!IS_ERR(plgpio->clk)) {
  495. ret = clk_prepare(plgpio->clk);
  496. if (ret) {
  497. dev_err(&pdev->dev, "clk prepare failed\n");
  498. return ret;
  499. }
  500. }
  501. irq = platform_get_irq(pdev, 0);
  502. if (irq > 0) {
  503. struct gpio_irq_chip *girq;
  504. girq = &plgpio->chip.irq;
  505. gpio_irq_chip_set_chip(girq, &plgpio_irqchip);
  506. girq->parent_handler = plgpio_irq_handler;
  507. girq->num_parents = 1;
  508. girq->parents = devm_kcalloc(&pdev->dev, 1,
  509. sizeof(*girq->parents),
  510. GFP_KERNEL);
  511. if (!girq->parents)
  512. return -ENOMEM;
  513. girq->parents[0] = irq;
  514. girq->default_type = IRQ_TYPE_NONE;
  515. girq->handler = handle_simple_irq;
  516. dev_info(&pdev->dev, "PLGPIO registering with IRQs\n");
  517. } else {
  518. dev_info(&pdev->dev, "PLGPIO registering without IRQs\n");
  519. }
  520. ret = gpiochip_add_data(&plgpio->chip, plgpio);
  521. if (ret) {
  522. dev_err(&pdev->dev, "unable to add gpio chip\n");
  523. goto unprepare_clk;
  524. }
  525. return 0;
  526. unprepare_clk:
  527. if (!IS_ERR(plgpio->clk))
  528. clk_unprepare(plgpio->clk);
  529. return ret;
  530. }
  531. #ifdef CONFIG_PM_SLEEP
  532. static int plgpio_suspend(struct device *dev)
  533. {
  534. struct plgpio *plgpio = dev_get_drvdata(dev);
  535. int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
  536. u32 off;
  537. for (i = 0; i < reg_count; i++) {
  538. off = i * sizeof(int *);
  539. if (plgpio->regs.enb != -1)
  540. regmap_read(plgpio->regmap, plgpio->regs.enb + off,
  541. &plgpio->csave_regs[i].enb);
  542. if (plgpio->regs.eit != -1)
  543. regmap_read(plgpio->regmap, plgpio->regs.eit + off,
  544. &plgpio->csave_regs[i].eit);
  545. regmap_read(plgpio->regmap, plgpio->regs.wdata + off,
  546. &plgpio->csave_regs[i].wdata);
  547. regmap_read(plgpio->regmap, plgpio->regs.dir + off,
  548. &plgpio->csave_regs[i].dir);
  549. regmap_read(plgpio->regmap, plgpio->regs.ie + off,
  550. &plgpio->csave_regs[i].ie);
  551. }
  552. return 0;
  553. }
  554. /*
  555. * This is used to correct the values in end registers. End registers contain
  556. * extra bits that might be used for other purpose in platform. So, we shouldn't
  557. * overwrite these bits. This macro, reads given register again, preserves other
  558. * bit values (non-plgpio bits), and retain captured value (plgpio bits).
  559. */
  560. #define plgpio_prepare_reg(__reg, _off, _mask, _tmp) \
  561. { \
  562. regmap_read(plgpio->regmap, plgpio->regs.__reg + _off, &_tmp); \
  563. _tmp &= ~_mask; \
  564. plgpio->csave_regs[i].__reg = \
  565. _tmp | (plgpio->csave_regs[i].__reg & _mask); \
  566. }
  567. static int plgpio_resume(struct device *dev)
  568. {
  569. struct plgpio *plgpio = dev_get_drvdata(dev);
  570. int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
  571. u32 off;
  572. u32 mask, tmp;
  573. for (i = 0; i < reg_count; i++) {
  574. off = i * sizeof(int *);
  575. if (i == reg_count - 1) {
  576. mask = (1 << (plgpio->chip.ngpio - i *
  577. MAX_GPIO_PER_REG)) - 1;
  578. if (plgpio->regs.enb != -1)
  579. plgpio_prepare_reg(enb, off, mask, tmp);
  580. if (plgpio->regs.eit != -1)
  581. plgpio_prepare_reg(eit, off, mask, tmp);
  582. plgpio_prepare_reg(wdata, off, mask, tmp);
  583. plgpio_prepare_reg(dir, off, mask, tmp);
  584. plgpio_prepare_reg(ie, off, mask, tmp);
  585. }
  586. regmap_write(plgpio->regmap, plgpio->regs.wdata + off,
  587. plgpio->csave_regs[i].wdata);
  588. regmap_write(plgpio->regmap, plgpio->regs.dir + off,
  589. plgpio->csave_regs[i].dir);
  590. if (plgpio->regs.eit != -1)
  591. regmap_write(plgpio->regmap, plgpio->regs.eit + off,
  592. plgpio->csave_regs[i].eit);
  593. regmap_write(plgpio->regmap, plgpio->regs.ie + off,
  594. plgpio->csave_regs[i].ie);
  595. if (plgpio->regs.enb != -1)
  596. regmap_write(plgpio->regmap, plgpio->regs.enb + off,
  597. plgpio->csave_regs[i].enb);
  598. }
  599. return 0;
  600. }
  601. #endif
  602. static SIMPLE_DEV_PM_OPS(plgpio_dev_pm_ops, plgpio_suspend, plgpio_resume);
  603. static const struct of_device_id plgpio_of_match[] = {
  604. { .compatible = "st,spear-plgpio" },
  605. {}
  606. };
  607. static struct platform_driver plgpio_driver = {
  608. .probe = plgpio_probe,
  609. .driver = {
  610. .name = "spear-plgpio",
  611. .pm = &plgpio_dev_pm_ops,
  612. .of_match_table = plgpio_of_match,
  613. },
  614. };
  615. static int __init plgpio_init(void)
  616. {
  617. return platform_driver_register(&plgpio_driver);
  618. }
  619. subsys_initcall(plgpio_init);