gpio-aspeed.c 22 KB

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