gpio-aspeed.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. /* fall through */
  335. case IRQ_TYPE_EDGE_RISING:
  336. type0 |= bit;
  337. /* fall through */
  338. case IRQ_TYPE_EDGE_FALLING:
  339. handler = handle_edge_irq;
  340. break;
  341. case IRQ_TYPE_LEVEL_HIGH:
  342. type0 |= bit;
  343. /* fall through */
  344. case IRQ_TYPE_LEVEL_LOW:
  345. type1 |= bit;
  346. handler = handle_level_irq;
  347. break;
  348. default:
  349. return -EINVAL;
  350. }
  351. spin_lock_irqsave(&gpio->lock, flags);
  352. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE0);
  353. reg = ioread32(addr);
  354. reg = (reg & ~bit) | type0;
  355. iowrite32(reg, addr);
  356. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE1);
  357. reg = ioread32(addr);
  358. reg = (reg & ~bit) | type1;
  359. iowrite32(reg, addr);
  360. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE2);
  361. reg = ioread32(addr);
  362. reg = (reg & ~bit) | type2;
  363. iowrite32(reg, addr);
  364. spin_unlock_irqrestore(&gpio->lock, flags);
  365. irq_set_handler_locked(d, handler);
  366. return 0;
  367. }
  368. static void aspeed_gpio_irq_handler(struct irq_desc *desc)
  369. {
  370. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  371. struct irq_chip *ic = irq_desc_get_chip(desc);
  372. struct aspeed_gpio *data = gpiochip_get_data(gc);
  373. unsigned int i, p, girq;
  374. unsigned long reg;
  375. chained_irq_enter(ic, desc);
  376. for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
  377. const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
  378. reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS));
  379. for_each_set_bit(p, &reg, 32) {
  380. girq = irq_find_mapping(gc->irq.domain, i * 32 + p);
  381. generic_handle_irq(girq);
  382. }
  383. }
  384. chained_irq_exit(ic, desc);
  385. }
  386. static struct irq_chip aspeed_gpio_irqchip = {
  387. .name = "aspeed-gpio",
  388. .irq_ack = aspeed_gpio_irq_ack,
  389. .irq_mask = aspeed_gpio_irq_mask,
  390. .irq_unmask = aspeed_gpio_irq_unmask,
  391. .irq_set_type = aspeed_gpio_set_type,
  392. };
  393. static void set_irq_valid_mask(struct aspeed_gpio *gpio)
  394. {
  395. const struct aspeed_bank_props *props = gpio->config->props;
  396. while (!is_bank_props_sentinel(props)) {
  397. unsigned int offset;
  398. const unsigned long int input = props->input;
  399. /* Pretty crummy approach, but similar to GPIO core */
  400. for_each_clear_bit(offset, &input, 32) {
  401. unsigned int i = props->bank * 32 + offset;
  402. if (i >= gpio->config->nr_gpios)
  403. break;
  404. clear_bit(i, gpio->chip.irq_valid_mask);
  405. }
  406. props++;
  407. }
  408. }
  409. static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio,
  410. struct platform_device *pdev)
  411. {
  412. int rc;
  413. rc = platform_get_irq(pdev, 0);
  414. if (rc < 0)
  415. return rc;
  416. gpio->irq = rc;
  417. set_irq_valid_mask(gpio);
  418. rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip,
  419. 0, handle_bad_irq, IRQ_TYPE_NONE);
  420. if (rc) {
  421. dev_info(&pdev->dev, "Could not add irqchip\n");
  422. return rc;
  423. }
  424. gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip,
  425. gpio->irq, aspeed_gpio_irq_handler);
  426. return 0;
  427. }
  428. static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
  429. {
  430. if (!have_gpio(gpiochip_get_data(chip), offset))
  431. return -ENODEV;
  432. return pinctrl_request_gpio(chip->base + offset);
  433. }
  434. static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
  435. {
  436. pinctrl_free_gpio(chip->base + offset);
  437. }
  438. static inline void __iomem *bank_debounce_reg(struct aspeed_gpio *gpio,
  439. const struct aspeed_gpio_bank *bank,
  440. unsigned int reg)
  441. {
  442. return gpio->base + bank->debounce_regs + reg;
  443. }
  444. static int usecs_to_cycles(struct aspeed_gpio *gpio, unsigned long usecs,
  445. u32 *cycles)
  446. {
  447. u64 rate;
  448. u64 n;
  449. u32 r;
  450. rate = clk_get_rate(gpio->clk);
  451. if (!rate)
  452. return -ENOTSUPP;
  453. n = rate * usecs;
  454. r = do_div(n, 1000000);
  455. if (n >= U32_MAX)
  456. return -ERANGE;
  457. /* At least as long as the requested time */
  458. *cycles = n + (!!r);
  459. return 0;
  460. }
  461. /* Call under gpio->lock */
  462. static int register_allocated_timer(struct aspeed_gpio *gpio,
  463. unsigned int offset, unsigned int timer)
  464. {
  465. if (WARN(gpio->offset_timer[offset] != 0,
  466. "Offset %d already allocated timer %d\n",
  467. offset, gpio->offset_timer[offset]))
  468. return -EINVAL;
  469. if (WARN(gpio->timer_users[timer] == UINT_MAX,
  470. "Timer user count would overflow\n"))
  471. return -EPERM;
  472. gpio->offset_timer[offset] = timer;
  473. gpio->timer_users[timer]++;
  474. return 0;
  475. }
  476. /* Call under gpio->lock */
  477. static int unregister_allocated_timer(struct aspeed_gpio *gpio,
  478. unsigned int offset)
  479. {
  480. if (WARN(gpio->offset_timer[offset] == 0,
  481. "No timer allocated to offset %d\n", offset))
  482. return -EINVAL;
  483. if (WARN(gpio->timer_users[gpio->offset_timer[offset]] == 0,
  484. "No users recorded for timer %d\n",
  485. gpio->offset_timer[offset]))
  486. return -EINVAL;
  487. gpio->timer_users[gpio->offset_timer[offset]]--;
  488. gpio->offset_timer[offset] = 0;
  489. return 0;
  490. }
  491. /* Call under gpio->lock */
  492. static inline bool timer_allocation_registered(struct aspeed_gpio *gpio,
  493. unsigned int offset)
  494. {
  495. return gpio->offset_timer[offset] > 0;
  496. }
  497. /* Call under gpio->lock */
  498. static void configure_timer(struct aspeed_gpio *gpio, unsigned int offset,
  499. unsigned int timer)
  500. {
  501. const struct aspeed_gpio_bank *bank = to_bank(offset);
  502. const u32 mask = GPIO_BIT(offset);
  503. void __iomem *addr;
  504. u32 val;
  505. addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL1);
  506. val = ioread32(addr);
  507. iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE1(timer, offset), addr);
  508. addr = bank_debounce_reg(gpio, bank, GPIO_DEBOUNCE_SEL2);
  509. val = ioread32(addr);
  510. iowrite32((val & ~mask) | GPIO_SET_DEBOUNCE2(timer, offset), addr);
  511. }
  512. static int enable_debounce(struct gpio_chip *chip, unsigned int offset,
  513. unsigned long usecs)
  514. {
  515. struct aspeed_gpio *gpio = gpiochip_get_data(chip);
  516. u32 requested_cycles;
  517. unsigned long flags;
  518. int rc;
  519. int i;
  520. if (!gpio->clk)
  521. return -EINVAL;
  522. rc = usecs_to_cycles(gpio, usecs, &requested_cycles);
  523. if (rc < 0) {
  524. dev_warn(chip->parent, "Failed to convert %luus to cycles at %luHz: %d\n",
  525. usecs, clk_get_rate(gpio->clk), rc);
  526. return rc;
  527. }
  528. spin_lock_irqsave(&gpio->lock, flags);
  529. if (timer_allocation_registered(gpio, offset)) {
  530. rc = unregister_allocated_timer(gpio, offset);
  531. if (rc < 0)
  532. goto out;
  533. }
  534. /* Try to find a timer already configured for the debounce period */
  535. for (i = 1; i < ARRAY_SIZE(debounce_timers); i++) {
  536. u32 cycles;
  537. cycles = ioread32(gpio->base + debounce_timers[i]);
  538. if (requested_cycles == cycles)
  539. break;
  540. }
  541. if (i == ARRAY_SIZE(debounce_timers)) {
  542. int j;
  543. /*
  544. * As there are no timers configured for the requested debounce
  545. * period, find an unused timer instead
  546. */
  547. for (j = 1; j < ARRAY_SIZE(gpio->timer_users); j++) {
  548. if (gpio->timer_users[j] == 0)
  549. break;
  550. }
  551. if (j == ARRAY_SIZE(gpio->timer_users)) {
  552. dev_warn(chip->parent,
  553. "Debounce timers exhausted, cannot debounce for period %luus\n",
  554. usecs);
  555. rc = -EPERM;
  556. /*
  557. * We already adjusted the accounting to remove @offset
  558. * as a user of its previous timer, so also configure
  559. * the hardware so @offset has timers disabled for
  560. * consistency.
  561. */
  562. configure_timer(gpio, offset, 0);
  563. goto out;
  564. }
  565. i = j;
  566. iowrite32(requested_cycles, gpio->base + debounce_timers[i]);
  567. }
  568. if (WARN(i == 0, "Cannot register index of disabled timer\n")) {
  569. rc = -EINVAL;
  570. goto out;
  571. }
  572. register_allocated_timer(gpio, offset, i);
  573. configure_timer(gpio, offset, i);
  574. out:
  575. spin_unlock_irqrestore(&gpio->lock, flags);
  576. return rc;
  577. }
  578. static int disable_debounce(struct gpio_chip *chip, unsigned int offset)
  579. {
  580. struct aspeed_gpio *gpio = gpiochip_get_data(chip);
  581. unsigned long flags;
  582. int rc;
  583. spin_lock_irqsave(&gpio->lock, flags);
  584. rc = unregister_allocated_timer(gpio, offset);
  585. if (!rc)
  586. configure_timer(gpio, offset, 0);
  587. spin_unlock_irqrestore(&gpio->lock, flags);
  588. return rc;
  589. }
  590. static int set_debounce(struct gpio_chip *chip, unsigned int offset,
  591. unsigned long usecs)
  592. {
  593. struct aspeed_gpio *gpio = gpiochip_get_data(chip);
  594. if (!have_debounce(gpio, offset))
  595. return -ENOTSUPP;
  596. if (usecs)
  597. return enable_debounce(chip, offset, usecs);
  598. return disable_debounce(chip, offset);
  599. }
  600. static int aspeed_gpio_set_config(struct gpio_chip *chip, unsigned int offset,
  601. unsigned long config)
  602. {
  603. unsigned long param = pinconf_to_config_param(config);
  604. u32 arg = pinconf_to_config_argument(config);
  605. if (param == PIN_CONFIG_INPUT_DEBOUNCE)
  606. return set_debounce(chip, offset, arg);
  607. else if (param == PIN_CONFIG_BIAS_DISABLE ||
  608. param == PIN_CONFIG_BIAS_PULL_DOWN ||
  609. param == PIN_CONFIG_DRIVE_STRENGTH)
  610. return pinctrl_gpio_set_config(offset, config);
  611. else if (param == PIN_CONFIG_DRIVE_OPEN_DRAIN ||
  612. param == PIN_CONFIG_DRIVE_OPEN_SOURCE)
  613. /* Return -ENOTSUPP to trigger emulation, as per datasheet */
  614. return -ENOTSUPP;
  615. return -ENOTSUPP;
  616. }
  617. /*
  618. * Any banks not specified in a struct aspeed_bank_props array are assumed to
  619. * have the properties:
  620. *
  621. * { .input = 0xffffffff, .output = 0xffffffff }
  622. */
  623. static const struct aspeed_bank_props ast2400_bank_props[] = {
  624. /* input output */
  625. { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
  626. { 6, 0x0000000f, 0x0fffff0f }, /* Y/Z/AA/AB, two 4-GPIO holes */
  627. { },
  628. };
  629. static const struct aspeed_gpio_config ast2400_config =
  630. /* 220 for simplicity, really 216 with two 4-GPIO holes, four at end */
  631. { .nr_gpios = 220, .props = ast2400_bank_props, };
  632. static const struct aspeed_bank_props ast2500_bank_props[] = {
  633. /* input output */
  634. { 5, 0xffffffff, 0x0000ffff }, /* U/V/W/X */
  635. { 6, 0x0fffffff, 0x0fffffff }, /* Y/Z/AA/AB, 4-GPIO hole */
  636. { 7, 0x000000ff, 0x000000ff }, /* AC */
  637. { },
  638. };
  639. static const struct aspeed_gpio_config ast2500_config =
  640. /* 232 for simplicity, actual number is 228 (4-GPIO hole in GPIOAB) */
  641. { .nr_gpios = 232, .props = ast2500_bank_props, };
  642. static const struct of_device_id aspeed_gpio_of_table[] = {
  643. { .compatible = "aspeed,ast2400-gpio", .data = &ast2400_config, },
  644. { .compatible = "aspeed,ast2500-gpio", .data = &ast2500_config, },
  645. {}
  646. };
  647. MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
  648. static int __init aspeed_gpio_probe(struct platform_device *pdev)
  649. {
  650. const struct of_device_id *gpio_id;
  651. struct aspeed_gpio *gpio;
  652. struct resource *res;
  653. int rc;
  654. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  655. if (!gpio)
  656. return -ENOMEM;
  657. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  658. gpio->base = devm_ioremap_resource(&pdev->dev, res);
  659. if (IS_ERR(gpio->base))
  660. return PTR_ERR(gpio->base);
  661. spin_lock_init(&gpio->lock);
  662. gpio_id = of_match_node(aspeed_gpio_of_table, pdev->dev.of_node);
  663. if (!gpio_id)
  664. return -EINVAL;
  665. gpio->clk = of_clk_get(pdev->dev.of_node, 0);
  666. if (IS_ERR(gpio->clk)) {
  667. dev_warn(&pdev->dev,
  668. "Failed to get clock from devicetree, debouncing disabled\n");
  669. gpio->clk = NULL;
  670. }
  671. gpio->config = gpio_id->data;
  672. gpio->chip.parent = &pdev->dev;
  673. gpio->chip.ngpio = gpio->config->nr_gpios;
  674. gpio->chip.parent = &pdev->dev;
  675. gpio->chip.direction_input = aspeed_gpio_dir_in;
  676. gpio->chip.direction_output = aspeed_gpio_dir_out;
  677. gpio->chip.get_direction = aspeed_gpio_get_direction;
  678. gpio->chip.request = aspeed_gpio_request;
  679. gpio->chip.free = aspeed_gpio_free;
  680. gpio->chip.get = aspeed_gpio_get;
  681. gpio->chip.set = aspeed_gpio_set;
  682. gpio->chip.set_config = aspeed_gpio_set_config;
  683. gpio->chip.label = dev_name(&pdev->dev);
  684. gpio->chip.base = -1;
  685. gpio->chip.irq_need_valid_mask = true;
  686. rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  687. if (rc < 0)
  688. return rc;
  689. gpio->offset_timer =
  690. devm_kzalloc(&pdev->dev, gpio->chip.ngpio, GFP_KERNEL);
  691. return aspeed_gpio_setup_irqs(gpio, pdev);
  692. }
  693. static struct platform_driver aspeed_gpio_driver = {
  694. .driver = {
  695. .name = KBUILD_MODNAME,
  696. .of_match_table = aspeed_gpio_of_table,
  697. },
  698. };
  699. module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
  700. MODULE_DESCRIPTION("Aspeed GPIO Driver");
  701. MODULE_LICENSE("GPL");