pinctrl-nsp-gpio.c 18 KB

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