pinctrl-nsp-gpio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2014-2017 Broadcom
  3. /*
  4. * This file contains the Broadcom Northstar Plus (NSP) GPIO driver that
  5. * supports the chipCommonA GPIO controller. Basic PINCONF such as bias,
  6. * pull up/down, slew and drive strength are also supported in this driver.
  7. *
  8. * Pins from the chipCommonA GPIO can be individually muxed to GPIO function,
  9. * through the interaction with the NSP IOMUX controller.
  10. */
  11. #include <linux/gpio/driver.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/kernel.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_device.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/pinctrl/pinconf.h>
  20. #include <linux/pinctrl/pinconf-generic.h>
  21. #include <linux/pinctrl/pinctrl.h>
  22. #include <linux/slab.h>
  23. #include "../pinctrl-utils.h"
  24. #define NSP_CHIP_A_INT_STATUS 0x00
  25. #define NSP_CHIP_A_INT_MASK 0x04
  26. #define NSP_GPIO_DATA_IN 0x40
  27. #define NSP_GPIO_DATA_OUT 0x44
  28. #define NSP_GPIO_OUT_EN 0x48
  29. #define NSP_GPIO_INT_POLARITY 0x50
  30. #define NSP_GPIO_INT_MASK 0x54
  31. #define NSP_GPIO_EVENT 0x58
  32. #define NSP_GPIO_EVENT_INT_MASK 0x5c
  33. #define NSP_GPIO_EVENT_INT_POLARITY 0x64
  34. #define NSP_CHIP_A_GPIO_INT_BIT 0x01
  35. /* I/O parameters offset for chipcommon A GPIO */
  36. #define NSP_GPIO_DRV_CTRL 0x00
  37. #define NSP_GPIO_HYSTERESIS_EN 0x10
  38. #define NSP_GPIO_SLEW_RATE_EN 0x14
  39. #define NSP_PULL_UP_EN 0x18
  40. #define NSP_PULL_DOWN_EN 0x1c
  41. #define GPIO_DRV_STRENGTH_BITS 0x03
  42. /*
  43. * nsp GPIO core
  44. *
  45. * @dev: pointer to device
  46. * @base: I/O register base for nsp GPIO controller
  47. * @io_ctrl: I/O register base for PINCONF support outside the GPIO block
  48. * @gc: GPIO chip
  49. * @pctl: pointer to pinctrl_dev
  50. * @pctldesc: pinctrl descriptor
  51. * @lock: lock to protect access to I/O registers
  52. */
  53. struct nsp_gpio {
  54. struct device *dev;
  55. void __iomem *base;
  56. void __iomem *io_ctrl;
  57. struct irq_chip irqchip;
  58. struct gpio_chip gc;
  59. struct pinctrl_dev *pctl;
  60. struct pinctrl_desc pctldesc;
  61. raw_spinlock_t lock;
  62. };
  63. enum base_type {
  64. REG,
  65. IO_CTRL
  66. };
  67. /*
  68. * Mapping from PINCONF pins to GPIO pins is 1-to-1
  69. */
  70. static inline unsigned nsp_pin_to_gpio(unsigned pin)
  71. {
  72. return pin;
  73. }
  74. /*
  75. * nsp_set_bit - set or clear one bit (corresponding to the GPIO pin) in a
  76. * nsp GPIO register
  77. *
  78. * @nsp_gpio: nsp GPIO device
  79. * @base_type: reg base to modify
  80. * @reg: register offset
  81. * @gpio: GPIO pin
  82. * @set: set or clear
  83. */
  84. static inline void nsp_set_bit(struct nsp_gpio *chip, enum base_type address,
  85. unsigned int reg, unsigned gpio, bool set)
  86. {
  87. u32 val;
  88. void __iomem *base_address;
  89. if (address == IO_CTRL)
  90. base_address = chip->io_ctrl;
  91. else
  92. base_address = chip->base;
  93. val = readl(base_address + reg);
  94. if (set)
  95. val |= BIT(gpio);
  96. else
  97. val &= ~BIT(gpio);
  98. writel(val, base_address + reg);
  99. }
  100. /*
  101. * nsp_get_bit - get one bit (corresponding to the GPIO pin) in a
  102. * nsp GPIO register
  103. */
  104. static inline bool nsp_get_bit(struct nsp_gpio *chip, enum base_type address,
  105. unsigned int reg, unsigned gpio)
  106. {
  107. if (address == IO_CTRL)
  108. return !!(readl(chip->io_ctrl + reg) & BIT(gpio));
  109. else
  110. return !!(readl(chip->base + reg) & BIT(gpio));
  111. }
  112. static irqreturn_t nsp_gpio_irq_handler(int irq, void *data)
  113. {
  114. struct gpio_chip *gc = (struct gpio_chip *)data;
  115. struct nsp_gpio *chip = gpiochip_get_data(gc);
  116. int bit;
  117. unsigned long int_bits = 0;
  118. u32 int_status;
  119. /* go through the entire GPIOs and handle all interrupts */
  120. int_status = readl(chip->base + NSP_CHIP_A_INT_STATUS);
  121. if (int_status & NSP_CHIP_A_GPIO_INT_BIT) {
  122. unsigned int event, level;
  123. /* Get level and edge interrupts */
  124. event = readl(chip->base + NSP_GPIO_EVENT_INT_MASK) &
  125. readl(chip->base + NSP_GPIO_EVENT);
  126. level = readl(chip->base + NSP_GPIO_DATA_IN) ^
  127. readl(chip->base + NSP_GPIO_INT_POLARITY);
  128. level &= readl(chip->base + NSP_GPIO_INT_MASK);
  129. int_bits = level | event;
  130. for_each_set_bit(bit, &int_bits, gc->ngpio)
  131. generic_handle_domain_irq(gc->irq.domain, bit);
  132. }
  133. return int_bits ? IRQ_HANDLED : IRQ_NONE;
  134. }
  135. static void nsp_gpio_irq_ack(struct irq_data *d)
  136. {
  137. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  138. struct nsp_gpio *chip = gpiochip_get_data(gc);
  139. unsigned gpio = d->hwirq;
  140. u32 val = BIT(gpio);
  141. u32 trigger_type;
  142. trigger_type = irq_get_trigger_type(d->irq);
  143. if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  144. writel(val, chip->base + NSP_GPIO_EVENT);
  145. }
  146. /*
  147. * nsp_gpio_irq_set_mask - mask/unmask a GPIO interrupt
  148. *
  149. * @d: IRQ chip data
  150. * @unmask: mask/unmask GPIO interrupt
  151. */
  152. static void nsp_gpio_irq_set_mask(struct irq_data *d, bool unmask)
  153. {
  154. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  155. struct nsp_gpio *chip = gpiochip_get_data(gc);
  156. unsigned gpio = d->hwirq;
  157. u32 trigger_type;
  158. trigger_type = irq_get_trigger_type(d->irq);
  159. if (trigger_type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  160. nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_MASK, gpio, unmask);
  161. else
  162. nsp_set_bit(chip, REG, NSP_GPIO_INT_MASK, gpio, unmask);
  163. }
  164. static void nsp_gpio_irq_mask(struct irq_data *d)
  165. {
  166. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  167. struct nsp_gpio *chip = gpiochip_get_data(gc);
  168. unsigned long flags;
  169. raw_spin_lock_irqsave(&chip->lock, flags);
  170. nsp_gpio_irq_set_mask(d, false);
  171. raw_spin_unlock_irqrestore(&chip->lock, flags);
  172. }
  173. static void nsp_gpio_irq_unmask(struct irq_data *d)
  174. {
  175. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  176. struct nsp_gpio *chip = gpiochip_get_data(gc);
  177. unsigned long flags;
  178. raw_spin_lock_irqsave(&chip->lock, flags);
  179. nsp_gpio_irq_set_mask(d, true);
  180. raw_spin_unlock_irqrestore(&chip->lock, flags);
  181. }
  182. static int nsp_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  183. {
  184. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  185. struct nsp_gpio *chip = gpiochip_get_data(gc);
  186. unsigned gpio = d->hwirq;
  187. bool level_low;
  188. bool falling;
  189. unsigned long flags;
  190. raw_spin_lock_irqsave(&chip->lock, flags);
  191. falling = nsp_get_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio);
  192. level_low = nsp_get_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio);
  193. switch (type & IRQ_TYPE_SENSE_MASK) {
  194. case IRQ_TYPE_EDGE_RISING:
  195. falling = false;
  196. break;
  197. case IRQ_TYPE_EDGE_FALLING:
  198. falling = true;
  199. break;
  200. case IRQ_TYPE_LEVEL_HIGH:
  201. level_low = false;
  202. break;
  203. case IRQ_TYPE_LEVEL_LOW:
  204. level_low = true;
  205. break;
  206. default:
  207. dev_err(chip->dev, "invalid GPIO IRQ type 0x%x\n",
  208. type);
  209. raw_spin_unlock_irqrestore(&chip->lock, flags);
  210. return -EINVAL;
  211. }
  212. nsp_set_bit(chip, REG, NSP_GPIO_EVENT_INT_POLARITY, gpio, falling);
  213. nsp_set_bit(chip, REG, NSP_GPIO_INT_POLARITY, gpio, level_low);
  214. if (type & IRQ_TYPE_EDGE_BOTH)
  215. irq_set_handler_locked(d, handle_edge_irq);
  216. else
  217. irq_set_handler_locked(d, handle_level_irq);
  218. raw_spin_unlock_irqrestore(&chip->lock, flags);
  219. dev_dbg(chip->dev, "gpio:%u level_low:%s falling:%s\n", gpio,
  220. level_low ? "true" : "false", falling ? "true" : "false");
  221. return 0;
  222. }
  223. static int nsp_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
  224. {
  225. struct nsp_gpio *chip = gpiochip_get_data(gc);
  226. unsigned long flags;
  227. raw_spin_lock_irqsave(&chip->lock, flags);
  228. nsp_set_bit(chip, REG, NSP_GPIO_OUT_EN, gpio, false);
  229. raw_spin_unlock_irqrestore(&chip->lock, flags);
  230. dev_dbg(chip->dev, "gpio:%u set input\n", gpio);
  231. return 0;
  232. }
  233. static int nsp_gpio_direction_output(struct gpio_chip *gc, unsigned gpio,
  234. int val)
  235. {
  236. struct nsp_gpio *chip = gpiochip_get_data(gc);
  237. unsigned long flags;
  238. raw_spin_lock_irqsave(&chip->lock, flags);
  239. nsp_set_bit(chip, REG, NSP_GPIO_OUT_EN, gpio, true);
  240. nsp_set_bit(chip, REG, NSP_GPIO_DATA_OUT, gpio, !!(val));
  241. raw_spin_unlock_irqrestore(&chip->lock, flags);
  242. dev_dbg(chip->dev, "gpio:%u set output, value:%d\n", gpio, val);
  243. return 0;
  244. }
  245. static int nsp_gpio_get_direction(struct gpio_chip *gc, unsigned gpio)
  246. {
  247. struct nsp_gpio *chip = gpiochip_get_data(gc);
  248. unsigned long flags;
  249. int val;
  250. raw_spin_lock_irqsave(&chip->lock, flags);
  251. val = nsp_get_bit(chip, REG, NSP_GPIO_OUT_EN, gpio);
  252. raw_spin_unlock_irqrestore(&chip->lock, flags);
  253. return !val;
  254. }
  255. static void nsp_gpio_set(struct gpio_chip *gc, unsigned gpio, int val)
  256. {
  257. struct nsp_gpio *chip = gpiochip_get_data(gc);
  258. unsigned long flags;
  259. raw_spin_lock_irqsave(&chip->lock, flags);
  260. nsp_set_bit(chip, REG, NSP_GPIO_DATA_OUT, gpio, !!(val));
  261. raw_spin_unlock_irqrestore(&chip->lock, flags);
  262. dev_dbg(chip->dev, "gpio:%u set, value:%d\n", gpio, val);
  263. }
  264. static int nsp_gpio_get(struct gpio_chip *gc, unsigned gpio)
  265. {
  266. struct nsp_gpio *chip = gpiochip_get_data(gc);
  267. return !!(readl(chip->base + NSP_GPIO_DATA_IN) & BIT(gpio));
  268. }
  269. static int nsp_get_groups_count(struct pinctrl_dev *pctldev)
  270. {
  271. return 1;
  272. }
  273. /*
  274. * Only one group: "gpio_grp", since this local pinctrl device only performs
  275. * GPIO specific PINCONF configurations
  276. */
  277. static const char *nsp_get_group_name(struct pinctrl_dev *pctldev,
  278. unsigned selector)
  279. {
  280. return "gpio_grp";
  281. }
  282. static const struct pinctrl_ops nsp_pctrl_ops = {
  283. .get_groups_count = nsp_get_groups_count,
  284. .get_group_name = nsp_get_group_name,
  285. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  286. .dt_free_map = pinctrl_utils_free_map,
  287. };
  288. static int nsp_gpio_set_slew(struct nsp_gpio *chip, unsigned gpio, u32 slew)
  289. {
  290. if (slew)
  291. nsp_set_bit(chip, IO_CTRL, NSP_GPIO_SLEW_RATE_EN, gpio, true);
  292. else
  293. nsp_set_bit(chip, IO_CTRL, NSP_GPIO_SLEW_RATE_EN, gpio, false);
  294. return 0;
  295. }
  296. static int nsp_gpio_set_pull(struct nsp_gpio *chip, unsigned gpio,
  297. bool pull_up, bool pull_down)
  298. {
  299. unsigned long flags;
  300. raw_spin_lock_irqsave(&chip->lock, flags);
  301. nsp_set_bit(chip, IO_CTRL, NSP_PULL_DOWN_EN, gpio, pull_down);
  302. nsp_set_bit(chip, IO_CTRL, NSP_PULL_UP_EN, gpio, pull_up);
  303. raw_spin_unlock_irqrestore(&chip->lock, flags);
  304. dev_dbg(chip->dev, "gpio:%u set pullup:%d pulldown: %d\n",
  305. gpio, pull_up, pull_down);
  306. return 0;
  307. }
  308. static void nsp_gpio_get_pull(struct nsp_gpio *chip, unsigned gpio,
  309. bool *pull_up, bool *pull_down)
  310. {
  311. unsigned long flags;
  312. raw_spin_lock_irqsave(&chip->lock, flags);
  313. *pull_up = nsp_get_bit(chip, IO_CTRL, NSP_PULL_UP_EN, gpio);
  314. *pull_down = nsp_get_bit(chip, IO_CTRL, NSP_PULL_DOWN_EN, gpio);
  315. raw_spin_unlock_irqrestore(&chip->lock, flags);
  316. }
  317. static int nsp_gpio_set_strength(struct nsp_gpio *chip, unsigned gpio,
  318. u32 strength)
  319. {
  320. u32 offset, shift, i;
  321. u32 val;
  322. unsigned long flags;
  323. /* make sure drive strength is supported */
  324. if (strength < 2 || strength > 16 || (strength % 2))
  325. return -ENOTSUPP;
  326. shift = gpio;
  327. offset = NSP_GPIO_DRV_CTRL;
  328. dev_dbg(chip->dev, "gpio:%u set drive strength:%d mA\n", gpio,
  329. strength);
  330. raw_spin_lock_irqsave(&chip->lock, flags);
  331. strength = (strength / 2) - 1;
  332. for (i = GPIO_DRV_STRENGTH_BITS; i > 0; i--) {
  333. val = readl(chip->io_ctrl + offset);
  334. val &= ~BIT(shift);
  335. val |= ((strength >> (i-1)) & 0x1) << shift;
  336. writel(val, chip->io_ctrl + offset);
  337. offset += 4;
  338. }
  339. raw_spin_unlock_irqrestore(&chip->lock, flags);
  340. return 0;
  341. }
  342. static int nsp_gpio_get_strength(struct nsp_gpio *chip, unsigned gpio,
  343. u16 *strength)
  344. {
  345. unsigned int offset, shift;
  346. u32 val;
  347. unsigned long flags;
  348. int i;
  349. offset = NSP_GPIO_DRV_CTRL;
  350. shift = gpio;
  351. raw_spin_lock_irqsave(&chip->lock, flags);
  352. *strength = 0;
  353. for (i = (GPIO_DRV_STRENGTH_BITS - 1); i >= 0; i--) {
  354. val = readl(chip->io_ctrl + offset) & BIT(shift);
  355. val >>= shift;
  356. *strength += (val << i);
  357. offset += 4;
  358. }
  359. /* convert to mA */
  360. *strength = (*strength + 1) * 2;
  361. raw_spin_unlock_irqrestore(&chip->lock, flags);
  362. return 0;
  363. }
  364. static int nsp_pin_config_group_get(struct pinctrl_dev *pctldev,
  365. unsigned selector,
  366. unsigned long *config)
  367. {
  368. return 0;
  369. }
  370. static int nsp_pin_config_group_set(struct pinctrl_dev *pctldev,
  371. unsigned selector,
  372. unsigned long *configs, unsigned num_configs)
  373. {
  374. return 0;
  375. }
  376. static int nsp_pin_config_get(struct pinctrl_dev *pctldev, unsigned pin,
  377. unsigned long *config)
  378. {
  379. struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  380. enum pin_config_param param = pinconf_to_config_param(*config);
  381. unsigned int gpio;
  382. u16 arg = 0;
  383. bool pull_up, pull_down;
  384. int ret;
  385. gpio = nsp_pin_to_gpio(pin);
  386. switch (param) {
  387. case PIN_CONFIG_BIAS_DISABLE:
  388. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  389. if ((pull_up == false) && (pull_down == false))
  390. return 0;
  391. else
  392. return -EINVAL;
  393. case PIN_CONFIG_BIAS_PULL_UP:
  394. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  395. if (pull_up)
  396. return 0;
  397. else
  398. return -EINVAL;
  399. case PIN_CONFIG_BIAS_PULL_DOWN:
  400. nsp_gpio_get_pull(chip, gpio, &pull_up, &pull_down);
  401. if (pull_down)
  402. return 0;
  403. else
  404. return -EINVAL;
  405. case PIN_CONFIG_DRIVE_STRENGTH:
  406. ret = nsp_gpio_get_strength(chip, gpio, &arg);
  407. if (ret)
  408. return ret;
  409. *config = pinconf_to_config_packed(param, arg);
  410. return 0;
  411. default:
  412. return -ENOTSUPP;
  413. }
  414. }
  415. static int nsp_pin_config_set(struct pinctrl_dev *pctldev, unsigned pin,
  416. unsigned long *configs, unsigned num_configs)
  417. {
  418. struct nsp_gpio *chip = pinctrl_dev_get_drvdata(pctldev);
  419. enum pin_config_param param;
  420. u32 arg;
  421. unsigned int i, gpio;
  422. int ret = -ENOTSUPP;
  423. gpio = nsp_pin_to_gpio(pin);
  424. for (i = 0; i < num_configs; i++) {
  425. param = pinconf_to_config_param(configs[i]);
  426. arg = pinconf_to_config_argument(configs[i]);
  427. switch (param) {
  428. case PIN_CONFIG_BIAS_DISABLE:
  429. ret = nsp_gpio_set_pull(chip, gpio, false, false);
  430. if (ret < 0)
  431. goto out;
  432. break;
  433. case PIN_CONFIG_BIAS_PULL_UP:
  434. ret = nsp_gpio_set_pull(chip, gpio, true, false);
  435. if (ret < 0)
  436. goto out;
  437. break;
  438. case PIN_CONFIG_BIAS_PULL_DOWN:
  439. ret = nsp_gpio_set_pull(chip, gpio, false, true);
  440. if (ret < 0)
  441. goto out;
  442. break;
  443. case PIN_CONFIG_DRIVE_STRENGTH:
  444. ret = nsp_gpio_set_strength(chip, gpio, arg);
  445. if (ret < 0)
  446. goto out;
  447. break;
  448. case PIN_CONFIG_SLEW_RATE:
  449. ret = nsp_gpio_set_slew(chip, gpio, arg);
  450. if (ret < 0)
  451. goto out;
  452. break;
  453. default:
  454. dev_err(chip->dev, "invalid configuration\n");
  455. return -ENOTSUPP;
  456. }
  457. }
  458. out:
  459. return ret;
  460. }
  461. static const struct pinconf_ops nsp_pconf_ops = {
  462. .is_generic = true,
  463. .pin_config_get = nsp_pin_config_get,
  464. .pin_config_set = nsp_pin_config_set,
  465. .pin_config_group_get = nsp_pin_config_group_get,
  466. .pin_config_group_set = nsp_pin_config_group_set,
  467. };
  468. /*
  469. * NSP GPIO controller supports some PINCONF related configurations such as
  470. * pull up, pull down, slew and drive strength, when the pin is configured
  471. * to GPIO.
  472. *
  473. * Here a local pinctrl device is created with simple 1-to-1 pin mapping to the
  474. * local GPIO pins
  475. */
  476. static int nsp_gpio_register_pinconf(struct nsp_gpio *chip)
  477. {
  478. struct pinctrl_desc *pctldesc = &chip->pctldesc;
  479. struct pinctrl_pin_desc *pins;
  480. struct gpio_chip *gc = &chip->gc;
  481. int i;
  482. pins = devm_kcalloc(chip->dev, gc->ngpio, sizeof(*pins), GFP_KERNEL);
  483. if (!pins)
  484. return -ENOMEM;
  485. for (i = 0; i < gc->ngpio; i++) {
  486. pins[i].number = i;
  487. pins[i].name = devm_kasprintf(chip->dev, GFP_KERNEL,
  488. "gpio-%d", i);
  489. if (!pins[i].name)
  490. return -ENOMEM;
  491. }
  492. pctldesc->name = dev_name(chip->dev);
  493. pctldesc->pctlops = &nsp_pctrl_ops;
  494. pctldesc->pins = pins;
  495. pctldesc->npins = gc->ngpio;
  496. pctldesc->confops = &nsp_pconf_ops;
  497. chip->pctl = devm_pinctrl_register(chip->dev, pctldesc, chip);
  498. if (IS_ERR(chip->pctl)) {
  499. dev_err(chip->dev, "unable to register pinctrl device\n");
  500. return PTR_ERR(chip->pctl);
  501. }
  502. return 0;
  503. }
  504. static const struct of_device_id nsp_gpio_of_match[] = {
  505. {.compatible = "brcm,nsp-gpio-a",},
  506. {}
  507. };
  508. static int nsp_gpio_probe(struct platform_device *pdev)
  509. {
  510. struct device *dev = &pdev->dev;
  511. struct nsp_gpio *chip;
  512. struct gpio_chip *gc;
  513. u32 val;
  514. int irq, ret;
  515. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &val)) {
  516. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  517. return -ENODEV;
  518. }
  519. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  520. if (!chip)
  521. return -ENOMEM;
  522. chip->dev = dev;
  523. platform_set_drvdata(pdev, chip);
  524. chip->base = devm_platform_ioremap_resource(pdev, 0);
  525. if (IS_ERR(chip->base)) {
  526. dev_err(dev, "unable to map I/O memory\n");
  527. return PTR_ERR(chip->base);
  528. }
  529. chip->io_ctrl = devm_platform_ioremap_resource(pdev, 1);
  530. if (IS_ERR(chip->io_ctrl)) {
  531. dev_err(dev, "unable to map I/O memory\n");
  532. return PTR_ERR(chip->io_ctrl);
  533. }
  534. raw_spin_lock_init(&chip->lock);
  535. gc = &chip->gc;
  536. gc->base = -1;
  537. gc->can_sleep = false;
  538. gc->ngpio = val;
  539. gc->label = dev_name(dev);
  540. gc->parent = dev;
  541. gc->request = gpiochip_generic_request;
  542. gc->free = gpiochip_generic_free;
  543. gc->direction_input = nsp_gpio_direction_input;
  544. gc->direction_output = nsp_gpio_direction_output;
  545. gc->get_direction = nsp_gpio_get_direction;
  546. gc->set = nsp_gpio_set;
  547. gc->get = nsp_gpio_get;
  548. /* optional GPIO interrupt support */
  549. irq = platform_get_irq(pdev, 0);
  550. if (irq > 0) {
  551. struct gpio_irq_chip *girq;
  552. struct irq_chip *irqc;
  553. irqc = &chip->irqchip;
  554. irqc->name = "gpio-a";
  555. irqc->irq_ack = nsp_gpio_irq_ack;
  556. irqc->irq_mask = nsp_gpio_irq_mask;
  557. irqc->irq_unmask = nsp_gpio_irq_unmask;
  558. irqc->irq_set_type = nsp_gpio_irq_set_type;
  559. val = readl(chip->base + NSP_CHIP_A_INT_MASK);
  560. val = val | NSP_CHIP_A_GPIO_INT_BIT;
  561. writel(val, (chip->base + NSP_CHIP_A_INT_MASK));
  562. /* Install ISR for this GPIO controller. */
  563. ret = devm_request_irq(dev, irq, nsp_gpio_irq_handler,
  564. IRQF_SHARED, "gpio-a", &chip->gc);
  565. if (ret) {
  566. dev_err(&pdev->dev, "Unable to request IRQ%d: %d\n",
  567. irq, ret);
  568. return ret;
  569. }
  570. girq = &chip->gc.irq;
  571. girq->chip = irqc;
  572. /* This will let us handle the parent IRQ in the driver */
  573. girq->parent_handler = NULL;
  574. girq->num_parents = 0;
  575. girq->parents = NULL;
  576. girq->default_type = IRQ_TYPE_NONE;
  577. girq->handler = handle_bad_irq;
  578. }
  579. ret = devm_gpiochip_add_data(dev, gc, chip);
  580. if (ret < 0) {
  581. dev_err(dev, "unable to add GPIO chip\n");
  582. return ret;
  583. }
  584. ret = nsp_gpio_register_pinconf(chip);
  585. if (ret) {
  586. dev_err(dev, "unable to register pinconf\n");
  587. return ret;
  588. }
  589. return 0;
  590. }
  591. static struct platform_driver nsp_gpio_driver = {
  592. .driver = {
  593. .name = "nsp-gpio-a",
  594. .of_match_table = nsp_gpio_of_match,
  595. },
  596. .probe = nsp_gpio_probe,
  597. };
  598. static int __init nsp_gpio_init(void)
  599. {
  600. return platform_driver_register(&nsp_gpio_driver);
  601. }
  602. arch_initcall_sync(nsp_gpio_init);