pinctrl-cygnus-gpio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Copyright (C) 2014-2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * This file contains the Broadcom Cygnus GPIO driver that supports 3
  14. * GPIO controllers on Cygnus including the ASIU GPIO controller, the
  15. * chipCommonG GPIO controller, and the always-on GPIO controller. Basic
  16. * PINCONF such as bias pull up/down, and drive strength are also supported
  17. * in this driver.
  18. *
  19. * Pins from the ASIU GPIO can be individually muxed to GPIO function,
  20. * through the interaction with the Cygnus IOMUX controller
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/gpio.h>
  27. #include <linux/ioport.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/pinctrl/pinctrl.h>
  31. #include <linux/pinctrl/pinconf.h>
  32. #include <linux/pinctrl/pinconf-generic.h>
  33. #include "../pinctrl-utils.h"
  34. #define CYGNUS_GPIO_DATA_IN_OFFSET 0x00
  35. #define CYGNUS_GPIO_DATA_OUT_OFFSET 0x04
  36. #define CYGNUS_GPIO_OUT_EN_OFFSET 0x08
  37. #define CYGNUS_GPIO_INT_TYPE_OFFSET 0x0c
  38. #define CYGNUS_GPIO_INT_DE_OFFSET 0x10
  39. #define CYGNUS_GPIO_INT_EDGE_OFFSET 0x14
  40. #define CYGNUS_GPIO_INT_MSK_OFFSET 0x18
  41. #define CYGNUS_GPIO_INT_STAT_OFFSET 0x1c
  42. #define CYGNUS_GPIO_INT_MSTAT_OFFSET 0x20
  43. #define CYGNUS_GPIO_INT_CLR_OFFSET 0x24
  44. #define CYGNUS_GPIO_PAD_RES_OFFSET 0x34
  45. #define CYGNUS_GPIO_RES_EN_OFFSET 0x38
  46. /* drive strength control for ASIU GPIO */
  47. #define CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET 0x58
  48. /* drive strength control for CCM/CRMU (AON) GPIO */
  49. #define CYGNUS_GPIO_DRV0_CTRL_OFFSET 0x00
  50. #define GPIO_BANK_SIZE 0x200
  51. #define NGPIOS_PER_BANK 32
  52. #define GPIO_BANK(pin) ((pin) / NGPIOS_PER_BANK)
  53. #define CYGNUS_GPIO_REG(pin, reg) (GPIO_BANK(pin) * GPIO_BANK_SIZE + (reg))
  54. #define CYGNUS_GPIO_SHIFT(pin) ((pin) % NGPIOS_PER_BANK)
  55. #define GPIO_DRV_STRENGTH_BIT_SHIFT 20
  56. #define GPIO_DRV_STRENGTH_BITS 3
  57. #define GPIO_DRV_STRENGTH_BIT_MASK ((1 << GPIO_DRV_STRENGTH_BITS) - 1)
  58. /*
  59. * Cygnus GPIO core
  60. *
  61. * @dev: pointer to device
  62. * @base: I/O register base for Cygnus GPIO controller
  63. * @io_ctrl: I/O register base for certain type of Cygnus GPIO controller that
  64. * has the PINCONF support implemented outside of the GPIO block
  65. * @lock: lock to protect access to I/O registers
  66. * @gc: GPIO chip
  67. * @num_banks: number of GPIO banks, each bank supports up to 32 GPIOs
  68. * @pinmux_is_supported: flag to indicate this GPIO controller contains pins
  69. * that can be individually muxed to GPIO
  70. * @pctl: pointer to pinctrl_dev
  71. * @pctldesc: pinctrl descriptor
  72. */
  73. struct cygnus_gpio {
  74. struct device *dev;
  75. void __iomem *base;
  76. void __iomem *io_ctrl;
  77. spinlock_t lock;
  78. struct gpio_chip gc;
  79. unsigned num_banks;
  80. bool pinmux_is_supported;
  81. struct pinctrl_dev *pctl;
  82. struct pinctrl_desc pctldesc;
  83. };
  84. static inline struct cygnus_gpio *to_cygnus_gpio(struct gpio_chip *gc)
  85. {
  86. return container_of(gc, struct cygnus_gpio, gc);
  87. }
  88. /*
  89. * Mapping from PINCONF pins to GPIO pins is 1-to-1
  90. */
  91. static inline unsigned cygnus_pin_to_gpio(unsigned pin)
  92. {
  93. return pin;
  94. }
  95. /**
  96. * cygnus_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
  97. * Cygnus GPIO register
  98. *
  99. * @cygnus_gpio: Cygnus GPIO device
  100. * @reg: register offset
  101. * @gpio: GPIO pin
  102. * @set: set or clear
  103. */
  104. static inline void cygnus_set_bit(struct cygnus_gpio *chip, unsigned int reg,
  105. unsigned gpio, bool set)
  106. {
  107. unsigned int offset = CYGNUS_GPIO_REG(gpio, reg);
  108. unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
  109. u32 val;
  110. val = readl(chip->base + offset);
  111. if (set)
  112. val |= BIT(shift);
  113. else
  114. val &= ~BIT(shift);
  115. writel(val, chip->base + offset);
  116. }
  117. static inline bool cygnus_get_bit(struct cygnus_gpio *chip, unsigned int reg,
  118. unsigned gpio)
  119. {
  120. unsigned int offset = CYGNUS_GPIO_REG(gpio, reg);
  121. unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
  122. return !!(readl(chip->base + offset) & BIT(shift));
  123. }
  124. static void cygnus_gpio_irq_handler(struct irq_desc *desc)
  125. {
  126. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  127. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  128. struct irq_chip *irq_chip = irq_desc_get_chip(desc);
  129. int i, bit;
  130. chained_irq_enter(irq_chip, desc);
  131. /* go through the entire GPIO banks and handle all interrupts */
  132. for (i = 0; i < chip->num_banks; i++) {
  133. unsigned long val = readl(chip->base + (i * GPIO_BANK_SIZE) +
  134. CYGNUS_GPIO_INT_MSTAT_OFFSET);
  135. for_each_set_bit(bit, &val, NGPIOS_PER_BANK) {
  136. unsigned pin = NGPIOS_PER_BANK * i + bit;
  137. int child_irq = irq_find_mapping(gc->irqdomain, pin);
  138. /*
  139. * Clear the interrupt before invoking the
  140. * handler, so we do not leave any window
  141. */
  142. writel(BIT(bit), chip->base + (i * GPIO_BANK_SIZE) +
  143. CYGNUS_GPIO_INT_CLR_OFFSET);
  144. generic_handle_irq(child_irq);
  145. }
  146. }
  147. chained_irq_exit(irq_chip, desc);
  148. }
  149. static void cygnus_gpio_irq_ack(struct irq_data *d)
  150. {
  151. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  152. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  153. unsigned gpio = d->hwirq;
  154. unsigned int offset = CYGNUS_GPIO_REG(gpio,
  155. CYGNUS_GPIO_INT_CLR_OFFSET);
  156. unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
  157. u32 val = BIT(shift);
  158. writel(val, chip->base + offset);
  159. }
  160. /**
  161. * cygnus_gpio_irq_set_mask - mask/unmask a GPIO interrupt
  162. *
  163. * @d: IRQ chip data
  164. * @unmask: mask/unmask GPIO interrupt
  165. */
  166. static void cygnus_gpio_irq_set_mask(struct irq_data *d, bool unmask)
  167. {
  168. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  169. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  170. unsigned gpio = d->hwirq;
  171. cygnus_set_bit(chip, CYGNUS_GPIO_INT_MSK_OFFSET, gpio, unmask);
  172. }
  173. static void cygnus_gpio_irq_mask(struct irq_data *d)
  174. {
  175. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  176. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  177. unsigned long flags;
  178. spin_lock_irqsave(&chip->lock, flags);
  179. cygnus_gpio_irq_set_mask(d, false);
  180. spin_unlock_irqrestore(&chip->lock, flags);
  181. }
  182. static void cygnus_gpio_irq_unmask(struct irq_data *d)
  183. {
  184. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  185. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  186. unsigned long flags;
  187. spin_lock_irqsave(&chip->lock, flags);
  188. cygnus_gpio_irq_set_mask(d, true);
  189. spin_unlock_irqrestore(&chip->lock, flags);
  190. }
  191. static int cygnus_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  192. {
  193. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  194. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  195. unsigned gpio = d->hwirq;
  196. bool level_triggered = false;
  197. bool dual_edge = false;
  198. bool rising_or_high = false;
  199. unsigned long flags;
  200. switch (type & IRQ_TYPE_SENSE_MASK) {
  201. case IRQ_TYPE_EDGE_RISING:
  202. rising_or_high = true;
  203. break;
  204. case IRQ_TYPE_EDGE_FALLING:
  205. break;
  206. case IRQ_TYPE_EDGE_BOTH:
  207. dual_edge = true;
  208. break;
  209. case IRQ_TYPE_LEVEL_HIGH:
  210. level_triggered = true;
  211. rising_or_high = true;
  212. break;
  213. case IRQ_TYPE_LEVEL_LOW:
  214. level_triggered = true;
  215. break;
  216. default:
  217. dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
  218. type);
  219. return -EINVAL;
  220. }
  221. spin_lock_irqsave(&chip->lock, flags);
  222. cygnus_set_bit(chip, CYGNUS_GPIO_INT_TYPE_OFFSET, gpio,
  223. level_triggered);
  224. cygnus_set_bit(chip, CYGNUS_GPIO_INT_DE_OFFSET, gpio, dual_edge);
  225. cygnus_set_bit(chip, CYGNUS_GPIO_INT_EDGE_OFFSET, gpio,
  226. rising_or_high);
  227. spin_unlock_irqrestore(&chip->lock, flags);
  228. dev_dbg(chip->dev,
  229. "gpio:%u level_triggered:%d dual_edge:%d rising_or_high:%d\n",
  230. gpio, level_triggered, dual_edge, rising_or_high);
  231. return 0;
  232. }
  233. static struct irq_chip cygnus_gpio_irq_chip = {
  234. .name = "bcm-cygnus-gpio",
  235. .irq_ack = cygnus_gpio_irq_ack,
  236. .irq_mask = cygnus_gpio_irq_mask,
  237. .irq_unmask = cygnus_gpio_irq_unmask,
  238. .irq_set_type = cygnus_gpio_irq_set_type,
  239. };
  240. /*
  241. * Request the Cygnus IOMUX pinmux controller to mux individual pins to GPIO
  242. */
  243. static int cygnus_gpio_request(struct gpio_chip *gc, unsigned offset)
  244. {
  245. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  246. unsigned gpio = gc->base + offset;
  247. /* not all Cygnus GPIO pins can be muxed individually */
  248. if (!chip->pinmux_is_supported)
  249. return 0;
  250. return pinctrl_request_gpio(gpio);
  251. }
  252. static void cygnus_gpio_free(struct gpio_chip *gc, unsigned offset)
  253. {
  254. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  255. unsigned gpio = gc->base + offset;
  256. if (!chip->pinmux_is_supported)
  257. return;
  258. pinctrl_free_gpio(gpio);
  259. }
  260. static int cygnus_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
  261. {
  262. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  263. unsigned long flags;
  264. spin_lock_irqsave(&chip->lock, flags);
  265. cygnus_set_bit(chip, CYGNUS_GPIO_OUT_EN_OFFSET, gpio, false);
  266. spin_unlock_irqrestore(&chip->lock, flags);
  267. dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
  268. return 0;
  269. }
  270. static int cygnus_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
  271. int val)
  272. {
  273. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  274. unsigned long flags;
  275. spin_lock_irqsave(&chip->lock, flags);
  276. cygnus_set_bit(chip, CYGNUS_GPIO_OUT_EN_OFFSET, gpio, true);
  277. cygnus_set_bit(chip, CYGNUS_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
  278. spin_unlock_irqrestore(&chip->lock, flags);
  279. dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
  280. return 0;
  281. }
  282. static void cygnus_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
  283. {
  284. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  285. unsigned long flags;
  286. spin_lock_irqsave(&chip->lock, flags);
  287. cygnus_set_bit(chip, CYGNUS_GPIO_DATA_OUT_OFFSET, gpio, !!(val));
  288. spin_unlock_irqrestore(&chip->lock, flags);
  289. dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
  290. }
  291. static int cygnus_gpio_get(struct gpio_chip *gc, unsigned gpio)
  292. {
  293. struct cygnus_gpio *chip = to_cygnus_gpio(gc);
  294. unsigned int offset = CYGNUS_GPIO_REG(gpio,
  295. CYGNUS_GPIO_DATA_IN_OFFSET);
  296. unsigned int shift = CYGNUS_GPIO_SHIFT(gpio);
  297. return !!(readl(chip->base + offset) & BIT(shift));
  298. }
  299. static int cygnus_get_groups_count(struct pinctrl_dev *pctldev)
  300. {
  301. return 1;
  302. }
  303. /*
  304. * Only one group: "gpio_grp", since this local pinctrl device only performs
  305. * GPIO specific PINCONF configurations
  306. */
  307. static const char *cygnus_get_group_name(struct pinctrl_dev *pctldev,
  308. unsigned selector)
  309. {
  310. return "gpio_grp";
  311. }
  312. static const struct pinctrl_ops cygnus_pctrl_ops = {
  313. .get_groups_count = cygnus_get_groups_count,
  314. .get_group_name = cygnus_get_group_name,
  315. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  316. .dt_free_map = pinctrl_utils_dt_free_map,
  317. };
  318. static int cygnus_gpio_set_pull(struct cygnus_gpio *chip, unsigned gpio,
  319. bool disable, bool pull_up)
  320. {
  321. unsigned long flags;
  322. spin_lock_irqsave(&chip->lock, flags);
  323. if (disable) {
  324. cygnus_set_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio, false);
  325. } else {
  326. cygnus_set_bit(chip, CYGNUS_GPIO_PAD_RES_OFFSET, gpio,
  327. pull_up);
  328. cygnus_set_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio, true);
  329. }
  330. spin_unlock_irqrestore(&chip->lock, flags);
  331. dev_dbg(chip->dev, "gpio:%u set pullup:%d\n", gpio, pull_up);
  332. return 0;
  333. }
  334. static void cygnus_gpio_get_pull(struct cygnus_gpio *chip, unsigned gpio,
  335. bool *disable, bool *pull_up)
  336. {
  337. unsigned long flags;
  338. spin_lock_irqsave(&chip->lock, flags);
  339. *disable = !cygnus_get_bit(chip, CYGNUS_GPIO_RES_EN_OFFSET, gpio);
  340. *pull_up = cygnus_get_bit(chip, CYGNUS_GPIO_PAD_RES_OFFSET, gpio);
  341. spin_unlock_irqrestore(&chip->lock, flags);
  342. }
  343. static int cygnus_gpio_set_strength(struct cygnus_gpio *chip, unsigned gpio,
  344. unsigned strength)
  345. {
  346. void __iomem *base;
  347. unsigned int i, offset, shift;
  348. u32 val;
  349. unsigned long flags;
  350. /* make sure drive strength is supported */
  351. if (strength < 2 || strength > 16 || (strength % 2))
  352. return -ENOTSUPP;
  353. if (chip->io_ctrl) {
  354. base = chip->io_ctrl;
  355. offset = CYGNUS_GPIO_DRV0_CTRL_OFFSET;
  356. } else {
  357. base = chip->base;
  358. offset = CYGNUS_GPIO_REG(gpio,
  359. CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET);
  360. }
  361. shift = CYGNUS_GPIO_SHIFT(gpio);
  362. dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
  363. strength);
  364. spin_lock_irqsave(&chip->lock, flags);
  365. strength = (strength / 2) - 1;
  366. for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
  367. val = readl(base + offset);
  368. val &= ~BIT(shift);
  369. val |= ((strength >> i) & 0x1) << shift;
  370. writel(val, base + offset);
  371. offset += 4;
  372. }
  373. spin_unlock_irqrestore(&chip->lock, flags);
  374. return 0;
  375. }
  376. static int cygnus_gpio_get_strength(struct cygnus_gpio *chip, unsigned gpio,
  377. u16 *strength)
  378. {
  379. void __iomem *base;
  380. unsigned int i, offset, shift;
  381. u32 val;
  382. unsigned long flags;
  383. if (chip->io_ctrl) {
  384. base = chip->io_ctrl;
  385. offset = CYGNUS_GPIO_DRV0_CTRL_OFFSET;
  386. } else {
  387. base = chip->base;
  388. offset = CYGNUS_GPIO_REG(gpio,
  389. CYGNUS_GPIO_ASIU_DRV0_CTRL_OFFSET);
  390. }
  391. shift = CYGNUS_GPIO_SHIFT(gpio);
  392. spin_lock_irqsave(&chip->lock, flags);
  393. *strength = 0;
  394. for (i = 0; i < GPIO_DRV_STRENGTH_BITS; i++) {
  395. val = readl(base + offset) & BIT(shift);
  396. val >>= shift;
  397. *strength += (val << i);
  398. offset += 4;
  399. }
  400. /* convert to mA */
  401. *strength = (*strength + 1) * 2;
  402. spin_unlock_irqrestore(&chip->lock, flags);
  403. return 0;
  404. }
  405. static int cygnus_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  406. unsigned long *config)
  407. {
  408. struct cygnus_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  409. enum pin_config_param param = pinconf_to_config_param(*config);
  410. unsigned gpio = cygnus_pin_to_gpio(pin);
  411. u16 arg;
  412. bool disable, pull_up;
  413. int ret;
  414. switch (param) {
  415. case PIN_CONFIG_BIAS_DISABLE:
  416. cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
  417. if (disable)
  418. return 0;
  419. else
  420. return -EINVAL;
  421. case PIN_CONFIG_BIAS_PULL_UP:
  422. cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
  423. if (!disable && pull_up)
  424. return 0;
  425. else
  426. return -EINVAL;
  427. case PIN_CONFIG_BIAS_PULL_DOWN:
  428. cygnus_gpio_get_pull(chip, gpio, &disable, &pull_up);
  429. if (!disable && !pull_up)
  430. return 0;
  431. else
  432. return -EINVAL;
  433. case PIN_CONFIG_DRIVE_STRENGTH:
  434. ret = cygnus_gpio_get_strength(chip, gpio, &arg);
  435. if (ret)
  436. return ret;
  437. else
  438. *config = pinconf_to_config_packed(param, arg);
  439. return 0;
  440. default:
  441. return -ENOTSUPP;
  442. }
  443. return -ENOTSUPP;
  444. }
  445. static int cygnus_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  446. unsigned long *configs, unsigned num_configs)
  447. {
  448. struct cygnus_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  449. enum pin_config_param param;
  450. u16 arg;
  451. unsigned i, gpio = cygnus_pin_to_gpio(pin);
  452. int ret = -ENOTSUPP;
  453. for (i = 0; i < num_configs; i++) {
  454. param = pinconf_to_config_param(configs[i]);
  455. arg = pinconf_to_config_argument(configs[i]);
  456. switch (param) {
  457. case PIN_CONFIG_BIAS_DISABLE:
  458. ret = cygnus_gpio_set_pull(chip, gpio, true, false);
  459. if (ret < 0)
  460. goto out;
  461. break;
  462. case PIN_CONFIG_BIAS_PULL_UP:
  463. ret = cygnus_gpio_set_pull(chip, gpio, false, true);
  464. if (ret < 0)
  465. goto out;
  466. break;
  467. case PIN_CONFIG_BIAS_PULL_DOWN:
  468. ret = cygnus_gpio_set_pull(chip, gpio, false, false);
  469. if (ret < 0)
  470. goto out;
  471. break;
  472. case PIN_CONFIG_DRIVE_STRENGTH:
  473. ret = cygnus_gpio_set_strength(chip, gpio, arg);
  474. if (ret < 0)
  475. goto out;
  476. break;
  477. default:
  478. dev_err(chip->dev, "invalid configuration\n");
  479. return -ENOTSUPP;
  480. }
  481. } /* for each config */
  482. out:
  483. return ret;
  484. }
  485. static const struct pinconf_ops cygnus_pconf_ops = {
  486. .is_generic = true,
  487. .pin_config_get = cygnus_pin_config_get,
  488. .pin_config_set = cygnus_pin_config_set,
  489. };
  490. /*
  491. * Cygnus GPIO controller supports some PINCONF related configurations such as
  492. * pull up, pull down, and drive strength, when the pin is configured to GPIO
  493. *
  494. * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
  495. * local GPIO pins
  496. */
  497. static int cygnus_gpio_register_pinconf(struct cygnus_gpio *chip)
  498. {
  499. struct pinctrl_desc *pctldesc = &chip->pctldesc;
  500. struct pinctrl_pin_desc *pins;
  501. struct gpio_chip *gc = &chip->gc;
  502. int i;
  503. pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
  504. if (!pins)
  505. return -ENOMEM;
  506. for (i = 0; i < gc->ngpio; i++) {
  507. pins[i].number = i;
  508. pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
  509. "gpio-%d", i);
  510. if (!pins[i].name)
  511. return -ENOMEM;
  512. }
  513. pctldesc->name = dev_name(chip->dev);
  514. pctldesc->pctlops = &cygnus_pctrl_ops;
  515. pctldesc->pins = pins;
  516. pctldesc->npins = gc->ngpio;
  517. pctldesc->confops = &cygnus_pconf_ops;
  518. chip->pctl = pinctrl_register(pctldesc, chip->dev, chip);
  519. if (IS_ERR(chip->pctl)) {
  520. dev_err(chip->dev, "unable to register pinctrl device\n");
  521. return PTR_ERR(chip->pctl);
  522. }
  523. return 0;
  524. }
  525. static void cygnus_gpio_unregister_pinconf(struct cygnus_gpio *chip)
  526. {
  527. if (chip->pctl)
  528. pinctrl_unregister(chip->pctl);
  529. }
  530. struct cygnus_gpio_data {
  531. unsigned num_gpios;
  532. };
  533. static const struct cygnus_gpio_data cygnus_cmm_gpio_data = {
  534. .num_gpios = 24,
  535. };
  536. static const struct cygnus_gpio_data cygnus_asiu_gpio_data = {
  537. .num_gpios = 146,
  538. };
  539. static const struct cygnus_gpio_data cygnus_crmu_gpio_data = {
  540. .num_gpios = 6,
  541. };
  542. static const struct of_device_id cygnus_gpio_of_match[] = {
  543. {
  544. .compatible = "brcm,cygnus-ccm-gpio",
  545. .data = &cygnus_cmm_gpio_data,
  546. },
  547. {
  548. .compatible = "brcm,cygnus-asiu-gpio",
  549. .data = &cygnus_asiu_gpio_data,
  550. },
  551. {
  552. .compatible = "brcm,cygnus-crmu-gpio",
  553. .data = &cygnus_crmu_gpio_data,
  554. }
  555. };
  556. static int cygnus_gpio_probe(struct platform_device *pdev)
  557. {
  558. struct device *dev = &pdev->dev;
  559. struct resource *res;
  560. struct cygnus_gpio *chip;
  561. struct gpio_chip *gc;
  562. u32 ngpios;
  563. int irq, ret;
  564. const struct of_device_id *match;
  565. const struct cygnus_gpio_data *gpio_data;
  566. match = of_match_device(cygnus_gpio_of_match, dev);
  567. if (!match)
  568. return -ENODEV;
  569. gpio_data = match->data;
  570. ngpios = gpio_data->num_gpios;
  571. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  572. if (!chip)
  573. return -ENOMEM;
  574. chip->dev = dev;
  575. platform_set_drvdata(pdev, chip);
  576. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  577. chip->base = devm_ioremap_resource(dev, res);
  578. if (IS_ERR(chip->base)) {
  579. dev_err(dev, "unable to map I/O memory\n");
  580. return PTR_ERR(chip->base);
  581. }
  582. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  583. if (res) {
  584. chip->io_ctrl = devm_ioremap_resource(dev, res);
  585. if (IS_ERR(chip->io_ctrl)) {
  586. dev_err(dev, "unable to map I/O memory\n");
  587. return PTR_ERR(chip->io_ctrl);
  588. }
  589. }
  590. spin_lock_init(&chip->lock);
  591. gc = &chip->gc;
  592. gc->base = -1;
  593. gc->ngpio = ngpios;
  594. chip->num_banks = (ngpios + NGPIOS_PER_BANK - 1) / NGPIOS_PER_BANK;
  595. gc->label = dev_name(dev);
  596. gc->dev = dev;
  597. gc->of_node = dev->of_node;
  598. gc->request = cygnus_gpio_request;
  599. gc->free = cygnus_gpio_free;
  600. gc->direction_input = cygnus_gpio_direction_input;
  601. gc->direction_output = cygnus_gpio_direction_output;
  602. gc->set = cygnus_gpio_set;
  603. gc->get = cygnus_gpio_get;
  604. chip->pinmux_is_supported = of_property_read_bool(dev->of_node,
  605. "gpio-ranges");
  606. ret = gpiochip_add(gc);
  607. if (ret < 0) {
  608. dev_err(dev, "unable to add GPIO chip\n");
  609. return ret;
  610. }
  611. ret = cygnus_gpio_register_pinconf(chip);
  612. if (ret) {
  613. dev_err(dev, "unable to register pinconf\n");
  614. goto err_rm_gpiochip;
  615. }
  616. /* optional GPIO interrupt support */
  617. irq = platform_get_irq(pdev, 0);
  618. if (irq) {
  619. ret = gpiochip_irqchip_add(gc, &cygnus_gpio_irq_chip, 0,
  620. handle_simple_irq, IRQ_TYPE_NONE);
  621. if (ret) {
  622. dev_err(dev, "no GPIO irqchip\n");
  623. goto err_unregister_pinconf;
  624. }
  625. gpiochip_set_chained_irqchip(gc, &cygnus_gpio_irq_chip, irq,
  626. cygnus_gpio_irq_handler);
  627. }
  628. return 0;
  629. err_unregister_pinconf:
  630. cygnus_gpio_unregister_pinconf(chip);
  631. err_rm_gpiochip:
  632. gpiochip_remove(gc);
  633. return ret;
  634. }
  635. static struct platform_driver cygnus_gpio_driver = {
  636. .driver = {
  637. .name = "cygnus-gpio",
  638. .of_match_table = cygnus_gpio_of_match,
  639. },
  640. .probe = cygnus_gpio_probe,
  641. };
  642. static int __init cygnus_gpio_init(void)
  643. {
  644. return platform_driver_probe(&cygnus_gpio_driver, cygnus_gpio_probe);
  645. }
  646. arch_initcall_sync(cygnus_gpio_init);