pinctrl-nsp-gpio.c 19 KB

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