gpio-aspeed.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * Copyright 2015 IBM Corp.
  3. *
  4. * Joel Stanley <joel@jms.id.au>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <asm/div64.h>
  12. #include <linux/clk.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/hashtable.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pinctrl/consumer.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/string.h>
  23. struct aspeed_bank_props {
  24. unsigned int bank;
  25. u32 input;
  26. u32 output;
  27. };
  28. struct aspeed_gpio_config {
  29. unsigned int nr_gpios;
  30. const struct aspeed_bank_props *props;
  31. };
  32. /*
  33. * @offset_timer: Maps an offset to an @timer_users index, or zero if disabled
  34. * @timer_users: Tracks the number of users for each timer
  35. *
  36. * The @timer_users has four elements but the first element is unused. This is
  37. * to simplify accounting and indexing, as a zero value in @offset_timer
  38. * represents disabled debouncing for the GPIO. Any other value for an element
  39. * of @offset_timer is used as an index into @timer_users. This behaviour of
  40. * the zero value aligns with the behaviour of zero built from the timer
  41. * configuration registers (i.e. debouncing is disabled).
  42. */
  43. struct aspeed_gpio {
  44. struct gpio_chip chip;
  45. spinlock_t lock;
  46. void __iomem *base;
  47. int irq;
  48. const struct aspeed_gpio_config *config;
  49. u8 *offset_timer;
  50. unsigned int timer_users[4];
  51. struct clk *clk;
  52. };
  53. struct aspeed_gpio_bank {
  54. uint16_t val_regs;
  55. uint16_t irq_regs;
  56. uint16_t debounce_regs;
  57. const char names[4][3];
  58. };
  59. static const int debounce_timers[4] = { 0x00, 0x50, 0x54, 0x58 };
  60. static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
  61. {
  62. .val_regs = 0x0000,
  63. .irq_regs = 0x0008,
  64. .debounce_regs = 0x0040,
  65. .names = { "A", "B", "C", "D" },
  66. },
  67. {
  68. .val_regs = 0x0020,
  69. .irq_regs = 0x0028,
  70. .debounce_regs = 0x0048,
  71. .names = { "E", "F", "G", "H" },
  72. },
  73. {
  74. .val_regs = 0x0070,
  75. .irq_regs = 0x0098,
  76. .debounce_regs = 0x00b0,
  77. .names = { "I", "J", "K", "L" },
  78. },
  79. {
  80. .val_regs = 0x0078,
  81. .irq_regs = 0x00e8,
  82. .debounce_regs = 0x0100,
  83. .names = { "M", "N", "O", "P" },
  84. },
  85. {
  86. .val_regs = 0x0080,
  87. .irq_regs = 0x0118,
  88. .debounce_regs = 0x0130,
  89. .names = { "Q", "R", "S", "T" },
  90. },
  91. {
  92. .val_regs = 0x0088,
  93. .irq_regs = 0x0148,
  94. .debounce_regs = 0x0160,
  95. .names = { "U", "V", "W", "X" },
  96. },
  97. {
  98. .val_regs = 0x01E0,
  99. .irq_regs = 0x0178,
  100. .debounce_regs = 0x0190,
  101. .names = { "Y", "Z", "AA", "AB" },
  102. },
  103. {
  104. .val_regs = 0x01E8,
  105. .irq_regs = 0x01A8,
  106. .debounce_regs = 0x01c0,
  107. .names = { "AC", "", "", "" },
  108. },
  109. };
  110. #define GPIO_BANK(x) ((x) >> 5)
  111. #define GPIO_OFFSET(x) ((x) & 0x1f)
  112. #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
  113. #define GPIO_DATA 0x00
  114. #define GPIO_DIR 0x04
  115. #define GPIO_IRQ_ENABLE 0x00
  116. #define GPIO_IRQ_TYPE0 0x04
  117. #define GPIO_IRQ_TYPE1 0x08
  118. #define GPIO_IRQ_TYPE2 0x0c
  119. #define GPIO_IRQ_STATUS 0x10
  120. #define GPIO_DEBOUNCE_SEL1 0x00
  121. #define GPIO_DEBOUNCE_SEL2 0x04
  122. #define _GPIO_SET_DEBOUNCE(t, o, i) ((!!((t) & BIT(i))) << GPIO_OFFSET(o))
  123. #define GPIO_SET_DEBOUNCE1(t, o) _GPIO_SET_DEBOUNCE(t, o, 1)
  124. #define GPIO_SET_DEBOUNCE2(t, o) _GPIO_SET_DEBOUNCE(t, o, 0)
  125. static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
  126. {
  127. unsigned int bank = GPIO_BANK(offset);
  128. WARN_ON(bank > ARRAY_SIZE(aspeed_gpio_banks));
  129. return &aspeed_gpio_banks[bank];
  130. }
  131. static inline bool is_bank_props_sentinel(const struct aspeed_bank_props *props)
  132. {
  133. return !(props->input || props->output);
  134. }
  135. static inline const struct aspeed_bank_props *find_bank_props(
  136. struct aspeed_gpio *gpio, unsigned int offset)
  137. {
  138. const struct aspeed_bank_props *props = gpio->config->props;
  139. while (!is_bank_props_sentinel(props)) {
  140. if (props->bank == GPIO_BANK(offset))
  141. return props;
  142. props++;
  143. }
  144. return NULL;
  145. }
  146. static inline bool have_gpio(struct aspeed_gpio *gpio, unsigned int offset)
  147. {
  148. const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
  149. const struct aspeed_gpio_bank *bank = to_bank(offset);
  150. unsigned int group = GPIO_OFFSET(offset) / 8;
  151. return bank->names[group][0] != '\0' &&
  152. (!props || ((props->input | props->output) & GPIO_BIT(offset)));
  153. }
  154. static inline bool have_input(struct aspeed_gpio *gpio, unsigned int offset)
  155. {
  156. const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
  157. return !props || (props->input & GPIO_BIT(offset));
  158. }
  159. #define have_irq(g, o) have_input((g), (o))
  160. #define have_debounce(g, o) have_input((g), (o))
  161. static inline bool have_output(struct aspeed_gpio *gpio, unsigned int offset)
  162. {
  163. const struct aspeed_bank_props *props = find_bank_props(gpio, offset);
  164. return !props || (props->output & GPIO_BIT(offset));
  165. }
  166. static void __iomem *bank_val_reg(struct aspeed_gpio *gpio,
  167. const struct aspeed_gpio_bank *bank,
  168. unsigned int reg)
  169. {
  170. return gpio->base + bank->val_regs + reg;
  171. }
  172. static void __iomem *bank_irq_reg(struct aspeed_gpio *gpio,
  173. const struct aspeed_gpio_bank *bank,
  174. unsigned int reg)
  175. {
  176. return gpio->base + bank->irq_regs + reg;
  177. }
  178. static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
  179. {
  180. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  181. const struct aspeed_gpio_bank *bank = to_bank(offset);
  182. return !!(ioread32(bank_val_reg(gpio, bank, GPIO_DATA))
  183. & GPIO_BIT(offset));
  184. }
  185. static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
  186. int val)
  187. {
  188. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  189. const struct aspeed_gpio_bank *bank = to_bank(offset);
  190. void __iomem *addr;
  191. u32 reg;
  192. addr = bank_val_reg(gpio, bank, GPIO_DATA);
  193. reg = ioread32(addr);
  194. if (val)
  195. reg |= GPIO_BIT(offset);
  196. else
  197. reg &= ~GPIO_BIT(offset);
  198. iowrite32(reg, addr);
  199. }
  200. static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
  201. int val)
  202. {
  203. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  204. unsigned long flags;
  205. spin_lock_irqsave(&gpio->lock, flags);
  206. __aspeed_gpio_set(gc, offset, val);
  207. spin_unlock_irqrestore(&gpio->lock, flags);
  208. }
  209. static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
  210. {
  211. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  212. const struct aspeed_gpio_bank *bank = to_bank(offset);
  213. unsigned long flags;
  214. u32 reg;
  215. if (!have_input(gpio, offset))
  216. return -ENOTSUPP;
  217. spin_lock_irqsave(&gpio->lock, flags);
  218. reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
  219. iowrite32(reg & ~GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
  220. spin_unlock_irqrestore(&gpio->lock, flags);
  221. return 0;
  222. }
  223. static int aspeed_gpio_dir_out(struct gpio_chip *gc,
  224. unsigned int offset, int val)
  225. {
  226. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  227. const struct aspeed_gpio_bank *bank = to_bank(offset);
  228. unsigned long flags;
  229. u32 reg;
  230. if (!have_output(gpio, offset))
  231. return -ENOTSUPP;
  232. spin_lock_irqsave(&gpio->lock, flags);
  233. reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
  234. iowrite32(reg | GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
  235. __aspeed_gpio_set(gc, offset, val);
  236. spin_unlock_irqrestore(&gpio->lock, flags);
  237. return 0;
  238. }
  239. static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  240. {
  241. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  242. const struct aspeed_gpio_bank *bank = to_bank(offset);
  243. unsigned long flags;
  244. u32 val;
  245. if (!have_input(gpio, offset))
  246. return 0;
  247. if (!have_output(gpio, offset))
  248. return 1;
  249. spin_lock_irqsave(&gpio->lock, flags);
  250. val = ioread32(bank_val_reg(gpio, bank, GPIO_DIR)) & GPIO_BIT(offset);
  251. spin_unlock_irqrestore(&gpio->lock, flags);
  252. return !val;
  253. }
  254. static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
  255. struct aspeed_gpio **gpio,
  256. const struct aspeed_gpio_bank **bank,
  257. u32 *bit)
  258. {
  259. int offset;
  260. struct aspeed_gpio *internal;
  261. offset = irqd_to_hwirq(d);
  262. internal = irq_data_get_irq_chip_data(d);
  263. /* This might be a bit of a questionable place to check */
  264. if (!have_irq(internal, offset))
  265. return -ENOTSUPP;
  266. *gpio = internal;
  267. *bank = to_bank(offset);
  268. *bit = GPIO_BIT(offset);
  269. return 0;
  270. }
  271. static void aspeed_gpio_irq_ack(struct irq_data *d)
  272. {
  273. const struct aspeed_gpio_bank *bank;
  274. struct aspeed_gpio *gpio;
  275. unsigned long flags;
  276. void __iomem *status_addr;
  277. u32 bit;
  278. int rc;
  279. rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
  280. if (rc)
  281. return;
  282. status_addr = bank_irq_reg(gpio, bank, GPIO_IRQ_STATUS);
  283. spin_lock_irqsave(&gpio->lock, flags);
  284. iowrite32(bit, status_addr);
  285. spin_unlock_irqrestore(&gpio->lock, flags);
  286. }
  287. static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
  288. {
  289. const struct aspeed_gpio_bank *bank;
  290. struct aspeed_gpio *gpio;
  291. unsigned long flags;
  292. u32 reg, bit;
  293. void __iomem *addr;
  294. int rc;
  295. rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
  296. if (rc)
  297. return;
  298. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_ENABLE);
  299. spin_lock_irqsave(&gpio->lock, flags);
  300. reg = ioread32(addr);
  301. if (set)
  302. reg |= bit;
  303. else
  304. reg &= bit;
  305. iowrite32(reg, addr);
  306. spin_unlock_irqrestore(&gpio->lock, flags);
  307. }
  308. static void aspeed_gpio_irq_mask(struct irq_data *d)
  309. {
  310. aspeed_gpio_irq_set_mask(d, false);
  311. }
  312. static void aspeed_gpio_irq_unmask(struct irq_data *d)
  313. {
  314. aspeed_gpio_irq_set_mask(d, true);
  315. }
  316. static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
  317. {
  318. u32 type0 = 0;
  319. u32 type1 = 0;
  320. u32 type2 = 0;
  321. u32 bit, reg;
  322. const struct aspeed_gpio_bank *bank;
  323. irq_flow_handler_t handler;
  324. struct aspeed_gpio *gpio;
  325. unsigned long flags;
  326. void __iomem *addr;
  327. int rc;
  328. rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
  329. if (rc)
  330. return -EINVAL;
  331. switch (type & IRQ_TYPE_SENSE_MASK) {
  332. case IRQ_TYPE_EDGE_BOTH:
  333. type2 |= bit;
  334. case IRQ_TYPE_EDGE_RISING:
  335. type0 |= bit;
  336. case IRQ_TYPE_EDGE_FALLING:
  337. handler = handle_edge_irq;
  338. break;
  339. case IRQ_TYPE_LEVEL_HIGH:
  340. type0 |= bit;
  341. case IRQ_TYPE_LEVEL_LOW:
  342. type1 |= bit;
  343. handler = handle_level_irq;
  344. break;
  345. default:
  346. return -EINVAL;
  347. }
  348. spin_lock_irqsave(&gpio->lock, flags);
  349. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE0);
  350. reg = ioread32(addr);
  351. reg = (reg & ~bit) | type0;
  352. iowrite32(reg, addr);
  353. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE1);
  354. reg = ioread32(addr);
  355. reg = (reg & ~bit) | type1;
  356. iowrite32(reg, addr);
  357. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE2);
  358. reg = ioread32(addr);
  359. reg = (reg & ~bit) | type2;
  360. iowrite32(reg, addr);
  361. spin_unlock_irqrestore(&gpio->lock, flags);
  362. irq_set_handler_locked(d, handler);
  363. return 0;
  364. }
  365. static void aspeed_gpio_irq_handler(struct irq_desc *desc)
  366. {
  367. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  368. struct irq_chip *ic = irq_desc_get_chip(desc);
  369. struct aspeed_gpio *data = gpiochip_get_data(gc);
  370. unsigned int i, p, girq;
  371. unsigned long reg;
  372. chained_irq_enter(ic, desc);
  373. for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
  374. const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
  375. reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS));
  376. for_each_set_bit(p, &reg, 32) {
  377. girq = irq_find_mapping(gc->irqdomain, i * 32 + p);
  378. generic_handle_irq(girq);
  379. }
  380. }
  381. chained_irq_exit(ic, desc);
  382. }
  383. static struct irq_chip aspeed_gpio_irqchip = {
  384. .name = "aspeed-gpio",
  385. .irq_ack = aspeed_gpio_irq_ack,
  386. .irq_mask = aspeed_gpio_irq_mask,
  387. .irq_unmask = aspeed_gpio_irq_unmask,
  388. .irq_set_type = aspeed_gpio_set_type,
  389. };
  390. static void set_irq_valid_mask(struct aspeed_gpio *gpio)
  391. {
  392. const struct aspeed_bank_props *props = gpio->config->props;
  393. while (!is_bank_props_sentinel(props)) {
  394. unsigned int offset;
  395. const unsigned long int input = props->input;
  396. /* Pretty crummy approach, but similar to GPIO core */
  397. for_each_clear_bit(offset, &input, 32) {
  398. unsigned int i = props->bank * 32 + offset;
  399. if (i >= gpio->config->nr_gpios)
  400. break;
  401. clear_bit(i, gpio->chip.irq_valid_mask);
  402. }
  403. props++;
  404. }
  405. }
  406. static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio,
  407. struct platform_device *pdev)
  408. {
  409. int rc;
  410. rc = platform_get_irq(pdev, 0);
  411. if (rc < 0)
  412. return rc;
  413. gpio->irq = rc;
  414. set_irq_valid_mask(gpio);
  415. rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip,
  416. 0, handle_bad_irq, IRQ_TYPE_NONE);
  417. if (rc) {
  418. dev_info(&pdev->dev, "Could not add irqchip\n");
  419. return rc;
  420. }
  421. gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip,
  422. gpio->irq, aspeed_gpio_irq_handler);
  423. return 0;
  424. }
  425. static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
  426. {
  427. if (!have_gpio(gpiochip_get_data(chip), offset))
  428. return -ENODEV;
  429. return pinctrl_gpio_request(chip->base + offset);
  430. }
  431. static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
  432. {
  433. pinctrl_gpio_free(chip->base + offset);
  434. }
  435. static inline void __iomem *bank_debounce_reg(struct aspeed_gpio *gpio,
  436. const struct aspeed_gpio_bank *bank,
  437. unsigned int reg)
  438. {
  439. return gpio->base + bank->debounce_regs + reg;
  440. }
  441. static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
  442. u32 *cycles)
  443. {
  444. u64 rate;
  445. u64 n;
  446. u32 r;
  447. rate = clk_get_rate(gpio->clk);
  448. if (!rate)
  449. return -ENOTSUPP;
  450. n = rate * usecs;
  451. r = do_div(n, 1000000);
  452. if (n >= U32_MAX)
  453. return -ERANGE;
  454. /* At least as long as the requested time */
  455. *cycles = n + (!!r);
  456. return 0;
  457. }
  458. /* Call under gpio->lock */
  459. static int register_allocated_timer(struct aspeed_gpio *gpio,
  460. unsigned int offset, unsigned int timer)
  461. {
  462. if (WARN(gpio->offset_timer[offset] != 0,
  463. "Offset %d already allocated timer %d\n",
  464. offset, gpio->offset_timer[offset]))
  465. return -EINVAL;
  466. if (WARN(gpio->timer_users[timer] == UINT_MAX,
  467. "Timer user count would overflow\n"))
  468. return -EPERM;
  469. gpio->offset_timer[offset] = timer;
  470. gpio->timer_users[timer]++;
  471. return 0;
  472. }
  473. /* Call under gpio->lock */
  474. static int unregister_allocated_timer(struct aspeed_gpio *gpio,
  475. unsigned int offset)
  476. {
  477. if (WARN(gpio->offset_timer[offset] == 0,
  478. "No timer allocated to offset %d\n", offset))
  479. return -EINVAL;
  480. if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
  481. "No users recorded for timer %d\n",
  482. gpio->offset_timer[offset]))
  483. return -EINVAL;
  484. gpio->timer_users[gpio->offset_timer[offset]]--;
  485. gpio->offset_timer[offset] = 0;
  486. return 0;
  487. }
  488. /* Call under gpio->lock */
  489. static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
  490. unsigned int offset)
  491. {
  492. return gpio->offset_timer[offset] > 0;
  493. }
  494. /* Call under gpio->lock */
  495. static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
  496. unsigned int timer)
  497. {
  498. const struct aspeed_gpio_bank *bank = to_bank(offset);
  499. const u32 mask = GPIO_BIT(offset);
  500. void __iomem *addr;
  501. u32 val;
  502. addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL1);
  503. val = ioread32(addr);
  504. iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
  505. addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL2);
  506. val = ioread32(addr);
  507. iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
  508. }
  509. static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
  510. unsigned long usecs)
  511. {
  512. struct aspeed_gpio *gpio = gpiochip_get_data(chip);
  513. u32 requested_cycles;
  514. unsigned long flags;
  515. int rc;
  516. int i;
  517. if (!gpio->clk)
  518. return -EINVAL;
  519. rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
  520. if (rc < 0) {
  521. dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
  522. usecs, clk_get_rate(gpio->clk), rc);
  523. return rc;
  524. }
  525. spin_lock_irqsave(&gpio->lock, flags);
  526. if (timer_allocation_registered(gpio, offset)) {
  527. rc = unregister_allocated_timer(gpio, offset);
  528. if (rc < 0)
  529. goto out;
  530. }
  531. /* Try to find a timer already configured for the debounce period */
  532. for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
  533. u32 cycles;
  534. cycles = ioread32(gpio->base + debounce_timers[i]);
  535. if (requested_cycles == cycles)
  536. break;
  537. }
  538. if (i == ARRAY_SIZE(debounce_timers)) {
  539. int j;
  540. /*
  541. * As there are no timers configured for the requested debounce
  542. * period, find an unused timer instead
  543. */
  544. for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
  545. if (gpio->timer_users[j] == 0)
  546. break;
  547. }
  548. if (j == ARRAY_SIZE(gpio->timer_users)) {
  549. dev_warn(chip->parent,
  550. "Debounce timers exhausted, cannot debounce for period %luus\n",
  551. usecs);
  552. rc = -EPERM;
  553. /*
  554. * We already adjusted the accounting to remove @offset
  555. * as a user of its previous timer, so also configure
  556. * the hardware so @offset has timers disabled for
  557. * consistency.
  558. */
  559. configure_timer(gpio, offset, 0);
  560. goto out;
  561. }
  562. i = j;
  563. iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
  564. }
  565. if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
  566. rc = -EINVAL;
  567. goto out;
  568. }
  569. register_allocated_timer(gpio, offset, i);
  570. configure_timer(gpio, offset, i);
  571. out:
  572. spin_unlock_irqrestore(&gpio->lock, flags);
  573. return rc;
  574. }
  575. static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
  576. {
  577. struct aspeed_gpio *gpio = gpiochip_get_data(chip);
  578. unsigned long flags;
  579. int rc;
  580. spin_lock_irqsave(&gpio->lock, flags);
  581. rc = unregister_allocated_timer(gpio, offset);
  582. if (!rc)
  583. configure_timer(gpio, offset, 0);
  584. spin_unlock_irqrestore(&gpio->lock, flags);
  585. return rc;
  586. }
  587. static int set_debounce(struct gpio_chip *chip, unsigned int offset,
  588. unsigned long usecs)
  589. {
  590. struct aspeed_gpio *gpio = gpiochip_get_data(chip);
  591. if (!have_debounce(gpio, offset))
  592. return -ENOTSUPP;
  593. if (usecs)
  594. return enable_debounce(chip, offset, usecs);
  595. return disable_debounce(chip, offset);
  596. }
  597. static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  598. unsigned long config)
  599. {
  600. unsigned long param = pinconf_to_config_param(config);
  601. u32 arg = pinconf_to_config_argument(config);
  602. if (param == PIN_CONFIG_INPUT_DEBOUNCE)
  603. return set_debounce(chip, offset, arg);
  604. else if (param == PIN_CONFIG_BIAS_DISABLE ||
  605. param == PIN_CONFIG_BIAS_PULL_DOWN ||
  606. param == PIN_CONFIG_DRIVE_STRENGTH)
  607. return pinctrl_gpio_set_config(offset, config);
  608. else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
  609. param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
  610. /* Return -ENOTSUPP to trigger emulation, as per datasheet */
  611. return -ENOTSUPP;
  612. return -ENOTSUPP;
  613. }
  614. /*
  615. * Any banks not specified in a struct aspeed_bank_props array are assumed to
  616. * have the properties:
  617. *
  618. * { .input = 0xffffffff, .output = 0xffffffff }
  619. */
  620. static const struct aspeed_bank_props ast2400_bank_props[] = {
  621. /* input output */
  622. { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
  623. { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
  624. { },
  625. };
  626. static const struct aspeed_gpio_config ast2400_config =
  627. /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
  628. { .nr_gpios = 220, .props = ast2400_bank_props, };
  629. static const struct aspeed_bank_props ast2500_bank_props[] = {
  630. /* input output */
  631. { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
  632. { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
  633. { 7, 0x000000ff, 0x000000ff }, /* AC */
  634. { },
  635. };
  636. static const struct aspeed_gpio_config ast2500_config =
  637. /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
  638. { .nr_gpios = 232, .props = ast2500_bank_props, };
  639. static const struct of_device_id aspeed_gpio_of_table[] = {
  640. { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
  641. { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
  642. {}
  643. };
  644. MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
  645. static int __init aspeed_gpio_probe(struct platform_device *pdev)
  646. {
  647. const struct of_device_id *gpio_id;
  648. struct aspeed_gpio *gpio;
  649. struct resource *res;
  650. int rc;
  651. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  652. if (!gpio)
  653. return -ENOMEM;
  654. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  655. gpio->base = devm_ioremap_resource(&pdev->dev, res);
  656. if (IS_ERR(gpio->base))
  657. return PTR_ERR(gpio->base);
  658. spin_lock_init(&gpio->lock);
  659. gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
  660. if (!gpio_id)
  661. return -EINVAL;
  662. gpio->clk = of_clk_get(pdev->dev.of_node, 0);
  663. if (IS_ERR(gpio->clk)) {
  664. dev_warn(&pdev->dev,
  665. "Failed to get clock from devicetree, debouncing disabled\n");
  666. gpio->clk = NULL;
  667. }
  668. gpio->config = gpio_id->data;
  669. gpio->chip.parent = &pdev->dev;
  670. gpio->chip.ngpio = gpio->config->nr_gpios;
  671. gpio->chip.parent = &pdev->dev;
  672. gpio->chip.direction_input = aspeed_gpio_dir_in;
  673. gpio->chip.direction_output = aspeed_gpio_dir_out;
  674. gpio->chip.get_direction = aspeed_gpio_get_direction;
  675. gpio->chip.request = aspeed_gpio_request;
  676. gpio->chip.free = aspeed_gpio_free;
  677. gpio->chip.get = aspeed_gpio_get;
  678. gpio->chip.set = aspeed_gpio_set;
  679. gpio->chip.set_config = aspeed_gpio_set_config;
  680. gpio->chip.label = dev_name(&pdev->dev);
  681. gpio->chip.base = -1;
  682. gpio->chip.irq_need_valid_mask = true;
  683. rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  684. if (rc < 0)
  685. return rc;
  686. gpio->offset_timer =
  687. devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
  688. return aspeed_gpio_setup_irqs(gpio, pdev);
  689. }
  690. static struct platform_driver aspeed_gpio_driver = {
  691. .driver = {
  692. .name = KBUILD_MODNAME,
  693. .of_match_table = aspeed_gpio_of_table,
  694. },
  695. };
  696. module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
  697. MODULE_DESCRIPTION("Aspeed GPIO Driver");
  698. MODULE_LICENSE("GPL");