pinctrl-plgpio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * SPEAr platform PLGPIO driver
  3. *
  4. * Copyright (C) 2012 ST Microelectronics
  5. * Viresh Kumar <[email protected]>
  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. int gpio = chip->base + offset;
  176. unsigned long flags;
  177. int ret = 0;
  178. if (offset >= chip->ngpio)
  179. return -EINVAL;
  180. ret = pinctrl_gpio_request(gpio);
  181. if (ret)
  182. return ret;
  183. if (!IS_ERR(plgpio->clk)) {
  184. ret = clk_enable(plgpio->clk);
  185. if (ret)
  186. goto err0;
  187. }
  188. if (plgpio->regs.enb == -1)
  189. return 0;
  190. /*
  191. * put gpio in IN mode before enabling it. This make enabling gpio safe
  192. */
  193. ret = plgpio_direction_input(chip, offset);
  194. if (ret)
  195. goto err1;
  196. /* get correct offset for "offset" pin */
  197. if (plgpio->p2o && (plgpio->p2o_regs & PTO_ENB_REG)) {
  198. offset = plgpio->p2o(offset);
  199. if (offset == -1) {
  200. ret = -EINVAL;
  201. goto err1;
  202. }
  203. }
  204. spin_lock_irqsave(&plgpio->lock, flags);
  205. plgpio_reg_set(plgpio->regmap, offset, plgpio->regs.enb);
  206. spin_unlock_irqrestore(&plgpio->lock, flags);
  207. return 0;
  208. err1:
  209. if (!IS_ERR(plgpio->clk))
  210. clk_disable(plgpio->clk);
  211. err0:
  212. pinctrl_gpio_free(gpio);
  213. return ret;
  214. }
  215. static void plgpio_free(struct gpio_chip *chip, unsigned offset)
  216. {
  217. struct plgpio *plgpio = gpiochip_get_data(chip);
  218. int gpio = chip->base + offset;
  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(gpio);
  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. }
  255. static void plgpio_irq_enable(struct irq_data *d)
  256. {
  257. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  258. struct plgpio *plgpio = gpiochip_get_data(gc);
  259. int offset = d->hwirq;
  260. unsigned long flags;
  261. /* get correct offset for "offset" pin */
  262. if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
  263. offset = plgpio->p2o(offset);
  264. if (offset == -1)
  265. return;
  266. }
  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 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. };
  303. static void plgpio_irq_handler(struct irq_desc *desc)
  304. {
  305. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  306. struct plgpio *plgpio = gpiochip_get_data(gc);
  307. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  308. int regs_count, count, pin, offset, i = 0;
  309. u32 pending;
  310. unsigned long pendingl;
  311. count = plgpio->chip.ngpio;
  312. regs_count = DIV_ROUND_UP(count, MAX_GPIO_PER_REG);
  313. chained_irq_enter(irqchip, desc);
  314. /* check all plgpio MIS registers for a possible interrupt */
  315. for (; i < regs_count; i++) {
  316. regmap_read(plgpio->regmap, plgpio->regs.mis +
  317. i * sizeof(int *), &pending);
  318. if (!pending)
  319. continue;
  320. /* clear interrupts */
  321. regmap_write(plgpio->regmap, plgpio->regs.mis +
  322. i * sizeof(int *), ~pending);
  323. /*
  324. * clear extra bits in last register having gpios < MAX/REG
  325. * ex: Suppose there are max 102 plgpios. then last register
  326. * must have only (102 - MAX_GPIO_PER_REG * 3) = 6 relevant bits
  327. * so, we must not take other 28 bits into consideration for
  328. * checking interrupt. so clear those bits.
  329. */
  330. count = count - i * MAX_GPIO_PER_REG;
  331. if (count < MAX_GPIO_PER_REG)
  332. pending &= (1 << count) - 1;
  333. pendingl = pending;
  334. for_each_set_bit(offset, &pendingl, 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_domain_irq(gc->irq.domain, 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. static 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 *regmap_np;
  442. struct plgpio *plgpio;
  443. int ret, irq;
  444. plgpio = devm_kzalloc(&pdev->dev, sizeof(*plgpio), GFP_KERNEL);
  445. if (!plgpio)
  446. return -ENOMEM;
  447. regmap_np = of_parse_phandle(pdev->dev.of_node, "regmap", 0);
  448. if (regmap_np) {
  449. plgpio->regmap = device_node_to_regmap(regmap_np);
  450. of_node_put(regmap_np);
  451. if (IS_ERR(plgpio->regmap)) {
  452. dev_err(&pdev->dev, "Retrieve regmap failed (%pe)\n",
  453. plgpio->regmap);
  454. return PTR_ERR(plgpio->regmap);
  455. }
  456. } else {
  457. plgpio->regmap = device_node_to_regmap(pdev->dev.of_node);
  458. if (IS_ERR(plgpio->regmap)) {
  459. dev_err(&pdev->dev, "Init regmap failed (%pe)\n",
  460. plgpio->regmap);
  461. return PTR_ERR(plgpio->regmap);
  462. }
  463. }
  464. ret = plgpio_probe_dt(pdev, plgpio);
  465. if (ret) {
  466. dev_err(&pdev->dev, "DT probe failed\n");
  467. return ret;
  468. }
  469. plgpio->clk = devm_clk_get(&pdev->dev, NULL);
  470. if (IS_ERR(plgpio->clk))
  471. dev_warn(&pdev->dev, "clk_get() failed, work without it\n");
  472. #ifdef CONFIG_PM_SLEEP
  473. plgpio->csave_regs = devm_kcalloc(&pdev->dev,
  474. DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG),
  475. sizeof(*plgpio->csave_regs),
  476. GFP_KERNEL);
  477. if (!plgpio->csave_regs)
  478. return -ENOMEM;
  479. #endif
  480. platform_set_drvdata(pdev, plgpio);
  481. spin_lock_init(&plgpio->lock);
  482. plgpio->chip.base = -1;
  483. plgpio->chip.request = plgpio_request;
  484. plgpio->chip.free = plgpio_free;
  485. plgpio->chip.direction_input = plgpio_direction_input;
  486. plgpio->chip.direction_output = plgpio_direction_output;
  487. plgpio->chip.get = plgpio_get_value;
  488. plgpio->chip.set = plgpio_set_value;
  489. plgpio->chip.label = dev_name(&pdev->dev);
  490. plgpio->chip.parent = &pdev->dev;
  491. plgpio->chip.owner = THIS_MODULE;
  492. if (!IS_ERR(plgpio->clk)) {
  493. ret = clk_prepare(plgpio->clk);
  494. if (ret) {
  495. dev_err(&pdev->dev, "clk prepare failed\n");
  496. return ret;
  497. }
  498. }
  499. irq = platform_get_irq(pdev, 0);
  500. if (irq > 0) {
  501. struct gpio_irq_chip *girq;
  502. girq = &plgpio->chip.irq;
  503. girq->chip = &plgpio_irqchip;
  504. girq->parent_handler = plgpio_irq_handler;
  505. girq->num_parents = 1;
  506. girq->parents = devm_kcalloc(&pdev->dev, 1,
  507. sizeof(*girq->parents),
  508. GFP_KERNEL);
  509. if (!girq->parents)
  510. return -ENOMEM;
  511. girq->parents[0] = irq;
  512. girq->default_type = IRQ_TYPE_NONE;
  513. girq->handler = handle_simple_irq;
  514. dev_info(&pdev->dev, "PLGPIO registering with IRQs\n");
  515. } else {
  516. dev_info(&pdev->dev, "PLGPIO registering without IRQs\n");
  517. }
  518. ret = gpiochip_add_data(&plgpio->chip, plgpio);
  519. if (ret) {
  520. dev_err(&pdev->dev, "unable to add gpio chip\n");
  521. goto unprepare_clk;
  522. }
  523. return 0;
  524. unprepare_clk:
  525. if (!IS_ERR(plgpio->clk))
  526. clk_unprepare(plgpio->clk);
  527. return ret;
  528. }
  529. #ifdef CONFIG_PM_SLEEP
  530. static int plgpio_suspend(struct device *dev)
  531. {
  532. struct plgpio *plgpio = dev_get_drvdata(dev);
  533. int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
  534. u32 off;
  535. for (i = 0; i < reg_count; i++) {
  536. off = i * sizeof(int *);
  537. if (plgpio->regs.enb != -1)
  538. regmap_read(plgpio->regmap, plgpio->regs.enb + off,
  539. &plgpio->csave_regs[i].enb);
  540. if (plgpio->regs.eit != -1)
  541. regmap_read(plgpio->regmap, plgpio->regs.eit + off,
  542. &plgpio->csave_regs[i].eit);
  543. regmap_read(plgpio->regmap, plgpio->regs.wdata + off,
  544. &plgpio->csave_regs[i].wdata);
  545. regmap_read(plgpio->regmap, plgpio->regs.dir + off,
  546. &plgpio->csave_regs[i].dir);
  547. regmap_read(plgpio->regmap, plgpio->regs.ie + off,
  548. &plgpio->csave_regs[i].ie);
  549. }
  550. return 0;
  551. }
  552. /*
  553. * This is used to correct the values in end registers. End registers contain
  554. * extra bits that might be used for other purpose in platform. So, we shouldn't
  555. * overwrite these bits. This macro, reads given register again, preserves other
  556. * bit values (non-plgpio bits), and retain captured value (plgpio bits).
  557. */
  558. #define plgpio_prepare_reg(__reg, _off, _mask, _tmp) \
  559. { \
  560. regmap_read(plgpio->regmap, plgpio->regs.__reg + _off, &_tmp); \
  561. _tmp &= ~_mask; \
  562. plgpio->csave_regs[i].__reg = \
  563. _tmp | (plgpio->csave_regs[i].__reg & _mask); \
  564. }
  565. static int plgpio_resume(struct device *dev)
  566. {
  567. struct plgpio *plgpio = dev_get_drvdata(dev);
  568. int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
  569. u32 off;
  570. u32 mask, tmp;
  571. for (i = 0; i < reg_count; i++) {
  572. off = i * sizeof(int *);
  573. if (i == reg_count - 1) {
  574. mask = (1 << (plgpio->chip.ngpio - i *
  575. MAX_GPIO_PER_REG)) - 1;
  576. if (plgpio->regs.enb != -1)
  577. plgpio_prepare_reg(enb, off, mask, tmp);
  578. if (plgpio->regs.eit != -1)
  579. plgpio_prepare_reg(eit, off, mask, tmp);
  580. plgpio_prepare_reg(wdata, off, mask, tmp);
  581. plgpio_prepare_reg(dir, off, mask, tmp);
  582. plgpio_prepare_reg(ie, off, mask, tmp);
  583. }
  584. regmap_write(plgpio->regmap, plgpio->regs.wdata + off,
  585. plgpio->csave_regs[i].wdata);
  586. regmap_write(plgpio->regmap, plgpio->regs.dir + off,
  587. plgpio->csave_regs[i].dir);
  588. if (plgpio->regs.eit != -1)
  589. regmap_write(plgpio->regmap, plgpio->regs.eit + off,
  590. plgpio->csave_regs[i].eit);
  591. regmap_write(plgpio->regmap, plgpio->regs.ie + off,
  592. plgpio->csave_regs[i].ie);
  593. if (plgpio->regs.enb != -1)
  594. regmap_write(plgpio->regmap, plgpio->regs.enb + off,
  595. plgpio->csave_regs[i].enb);
  596. }
  597. return 0;
  598. }
  599. #endif
  600. static SIMPLE_DEV_PM_OPS(plgpio_dev_pm_ops, plgpio_suspend, plgpio_resume);
  601. static const struct of_device_id plgpio_of_match[] = {
  602. { .compatible = "st,spear-plgpio" },
  603. {}
  604. };
  605. static struct platform_driver plgpio_driver = {
  606. .probe = plgpio_probe,
  607. .driver = {
  608. .name = "spear-plgpio",
  609. .pm = &plgpio_dev_pm_ops,
  610. .of_match_table = plgpio_of_match,
  611. },
  612. };
  613. static int __init plgpio_init(void)
  614. {
  615. return platform_driver_register(&plgpio_driver);
  616. }
  617. subsys_initcall(plgpio_init);