gpio-aspeed.c 22 KB

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