pinctrl-oxnas.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * Oxford Semiconductor OXNAS SoC Family pinctrl driver
  3. *
  4. * Copyright (C) 2016 Neil Armstrong <narmstrong@baylibre.com>
  5. *
  6. * Based on pinctrl-pic32.c
  7. * Joshua Henderson, <joshua.henderson@microchip.com>
  8. * Copyright (C) 2015 Microchip Technology Inc. All rights reserved.
  9. *
  10. * This program is free software; you can distribute it and/or modify it
  11. * under the terms of the GNU General Public License (Version 2) as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. * for more details.
  18. */
  19. #include <linux/gpio/driver.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/irq.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/pinctrl/pinconf.h>
  26. #include <linux/pinctrl/pinconf-generic.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/pinctrl/pinmux.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include <linux/regmap.h>
  32. #include <linux/mfd/syscon.h>
  33. #include "pinctrl-utils.h"
  34. #define PINS_PER_BANK 32
  35. #define GPIO_BANK_START(bank) ((bank) * PINS_PER_BANK)
  36. /* Regmap Offsets */
  37. #define PINMUX_PRIMARY_SEL0 0x0c
  38. #define PINMUX_SECONDARY_SEL0 0x14
  39. #define PINMUX_TERTIARY_SEL0 0x8c
  40. #define PINMUX_PRIMARY_SEL1 0x10
  41. #define PINMUX_SECONDARY_SEL1 0x18
  42. #define PINMUX_TERTIARY_SEL1 0x90
  43. #define PINMUX_PULLUP_CTRL0 0xac
  44. #define PINMUX_PULLUP_CTRL1 0xb0
  45. /* GPIO Registers */
  46. #define INPUT_VALUE 0x00
  47. #define OUTPUT_EN 0x04
  48. #define IRQ_PENDING 0x0c
  49. #define OUTPUT_SET 0x14
  50. #define OUTPUT_CLEAR 0x18
  51. #define OUTPUT_EN_SET 0x1c
  52. #define OUTPUT_EN_CLEAR 0x20
  53. #define RE_IRQ_ENABLE 0x28
  54. #define FE_IRQ_ENABLE 0x2c
  55. struct oxnas_function {
  56. const char *name;
  57. const char * const *groups;
  58. unsigned int ngroups;
  59. };
  60. struct oxnas_pin_group {
  61. const char *name;
  62. unsigned int pin;
  63. unsigned int bank;
  64. struct oxnas_desc_function *functions;
  65. };
  66. struct oxnas_desc_function {
  67. const char *name;
  68. unsigned int fct;
  69. };
  70. struct oxnas_gpio_bank {
  71. void __iomem *reg_base;
  72. struct gpio_chip gpio_chip;
  73. struct irq_chip irq_chip;
  74. unsigned int id;
  75. };
  76. struct oxnas_pinctrl {
  77. struct regmap *regmap;
  78. struct device *dev;
  79. struct pinctrl_dev *pctldev;
  80. const struct pinctrl_pin_desc *pins;
  81. unsigned int npins;
  82. const struct oxnas_function *functions;
  83. unsigned int nfunctions;
  84. const struct oxnas_pin_group *groups;
  85. unsigned int ngroups;
  86. struct oxnas_gpio_bank *gpio_banks;
  87. unsigned int nbanks;
  88. };
  89. static const struct pinctrl_pin_desc oxnas_pins[] = {
  90. PINCTRL_PIN(0, "gpio0"),
  91. PINCTRL_PIN(1, "gpio1"),
  92. PINCTRL_PIN(2, "gpio2"),
  93. PINCTRL_PIN(3, "gpio3"),
  94. PINCTRL_PIN(4, "gpio4"),
  95. PINCTRL_PIN(5, "gpio5"),
  96. PINCTRL_PIN(6, "gpio6"),
  97. PINCTRL_PIN(7, "gpio7"),
  98. PINCTRL_PIN(8, "gpio8"),
  99. PINCTRL_PIN(9, "gpio9"),
  100. PINCTRL_PIN(10, "gpio10"),
  101. PINCTRL_PIN(11, "gpio11"),
  102. PINCTRL_PIN(12, "gpio12"),
  103. PINCTRL_PIN(13, "gpio13"),
  104. PINCTRL_PIN(14, "gpio14"),
  105. PINCTRL_PIN(15, "gpio15"),
  106. PINCTRL_PIN(16, "gpio16"),
  107. PINCTRL_PIN(17, "gpio17"),
  108. PINCTRL_PIN(18, "gpio18"),
  109. PINCTRL_PIN(19, "gpio19"),
  110. PINCTRL_PIN(20, "gpio20"),
  111. PINCTRL_PIN(21, "gpio21"),
  112. PINCTRL_PIN(22, "gpio22"),
  113. PINCTRL_PIN(23, "gpio23"),
  114. PINCTRL_PIN(24, "gpio24"),
  115. PINCTRL_PIN(25, "gpio25"),
  116. PINCTRL_PIN(26, "gpio26"),
  117. PINCTRL_PIN(27, "gpio27"),
  118. PINCTRL_PIN(28, "gpio28"),
  119. PINCTRL_PIN(29, "gpio29"),
  120. PINCTRL_PIN(30, "gpio30"),
  121. PINCTRL_PIN(31, "gpio31"),
  122. PINCTRL_PIN(32, "gpio32"),
  123. PINCTRL_PIN(33, "gpio33"),
  124. PINCTRL_PIN(34, "gpio34"),
  125. };
  126. static const char * const oxnas_fct0_group[] = {
  127. "gpio0", "gpio1", "gpio2", "gpio3",
  128. "gpio4", "gpio5", "gpio6", "gpio7",
  129. "gpio8", "gpio9", "gpio10", "gpio11",
  130. "gpio12", "gpio13", "gpio14", "gpio15",
  131. "gpio16", "gpio17", "gpio18", "gpio19",
  132. "gpio20", "gpio21", "gpio22", "gpio23",
  133. "gpio24", "gpio25", "gpio26", "gpio27",
  134. "gpio28", "gpio29", "gpio30", "gpio31",
  135. "gpio32", "gpio33", "gpio34"
  136. };
  137. static const char * const oxnas_fct3_group[] = {
  138. "gpio0", "gpio1", "gpio2", "gpio3",
  139. "gpio4", "gpio5", "gpio6", "gpio7",
  140. "gpio8", "gpio9",
  141. "gpio20",
  142. "gpio22", "gpio23", "gpio24", "gpio25",
  143. "gpio26", "gpio27", "gpio28", "gpio29",
  144. "gpio30", "gpio31", "gpio32", "gpio33",
  145. "gpio34"
  146. };
  147. #define FUNCTION(_name, _gr) \
  148. { \
  149. .name = #_name, \
  150. .groups = oxnas_##_gr##_group, \
  151. .ngroups = ARRAY_SIZE(oxnas_##_gr##_group), \
  152. }
  153. static const struct oxnas_function oxnas_functions[] = {
  154. FUNCTION(gpio, fct0),
  155. FUNCTION(fct3, fct3),
  156. };
  157. #define OXNAS_PINCTRL_GROUP(_pin, _name, ...) \
  158. { \
  159. .name = #_name, \
  160. .pin = _pin, \
  161. .bank = _pin / PINS_PER_BANK, \
  162. .functions = (struct oxnas_desc_function[]){ \
  163. __VA_ARGS__, { } }, \
  164. }
  165. #define OXNAS_PINCTRL_FUNCTION(_name, _fct) \
  166. { \
  167. .name = #_name, \
  168. .fct = _fct, \
  169. }
  170. static const struct oxnas_pin_group oxnas_groups[] = {
  171. OXNAS_PINCTRL_GROUP(0, gpio0,
  172. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  173. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  174. OXNAS_PINCTRL_GROUP(1, gpio1,
  175. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  176. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  177. OXNAS_PINCTRL_GROUP(2, gpio2,
  178. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  179. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  180. OXNAS_PINCTRL_GROUP(3, gpio3,
  181. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  182. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  183. OXNAS_PINCTRL_GROUP(4, gpio4,
  184. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  185. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  186. OXNAS_PINCTRL_GROUP(5, gpio5,
  187. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  188. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  189. OXNAS_PINCTRL_GROUP(6, gpio6,
  190. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  191. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  192. OXNAS_PINCTRL_GROUP(7, gpio7,
  193. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  194. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  195. OXNAS_PINCTRL_GROUP(8, gpio8,
  196. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  197. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  198. OXNAS_PINCTRL_GROUP(9, gpio9,
  199. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  200. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  201. OXNAS_PINCTRL_GROUP(10, gpio10,
  202. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  203. OXNAS_PINCTRL_GROUP(11, gpio11,
  204. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  205. OXNAS_PINCTRL_GROUP(12, gpio12,
  206. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  207. OXNAS_PINCTRL_GROUP(13, gpio13,
  208. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  209. OXNAS_PINCTRL_GROUP(14, gpio14,
  210. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  211. OXNAS_PINCTRL_GROUP(15, gpio15,
  212. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  213. OXNAS_PINCTRL_GROUP(16, gpio16,
  214. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  215. OXNAS_PINCTRL_GROUP(17, gpio17,
  216. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  217. OXNAS_PINCTRL_GROUP(18, gpio18,
  218. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  219. OXNAS_PINCTRL_GROUP(19, gpio19,
  220. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  221. OXNAS_PINCTRL_GROUP(20, gpio20,
  222. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  223. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  224. OXNAS_PINCTRL_GROUP(21, gpio21,
  225. OXNAS_PINCTRL_FUNCTION(gpio, 0)),
  226. OXNAS_PINCTRL_GROUP(22, gpio22,
  227. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  228. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  229. OXNAS_PINCTRL_GROUP(23, gpio23,
  230. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  231. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  232. OXNAS_PINCTRL_GROUP(24, gpio24,
  233. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  234. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  235. OXNAS_PINCTRL_GROUP(25, gpio25,
  236. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  237. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  238. OXNAS_PINCTRL_GROUP(26, gpio26,
  239. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  240. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  241. OXNAS_PINCTRL_GROUP(27, gpio27,
  242. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  243. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  244. OXNAS_PINCTRL_GROUP(28, gpio28,
  245. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  246. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  247. OXNAS_PINCTRL_GROUP(29, gpio29,
  248. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  249. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  250. OXNAS_PINCTRL_GROUP(30, gpio30,
  251. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  252. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  253. OXNAS_PINCTRL_GROUP(31, gpio31,
  254. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  255. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  256. OXNAS_PINCTRL_GROUP(32, gpio32,
  257. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  258. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  259. OXNAS_PINCTRL_GROUP(33, gpio33,
  260. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  261. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  262. OXNAS_PINCTRL_GROUP(34, gpio34,
  263. OXNAS_PINCTRL_FUNCTION(gpio, 0),
  264. OXNAS_PINCTRL_FUNCTION(fct3, 3)),
  265. };
  266. static inline struct oxnas_gpio_bank *pctl_to_bank(struct oxnas_pinctrl *pctl,
  267. unsigned int pin)
  268. {
  269. return &pctl->gpio_banks[pin / PINS_PER_BANK];
  270. }
  271. static int oxnas_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  272. {
  273. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  274. return pctl->ngroups;
  275. }
  276. static const char *oxnas_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  277. unsigned int group)
  278. {
  279. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  280. return pctl->groups[group].name;
  281. }
  282. static int oxnas_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  283. unsigned int group,
  284. const unsigned int **pins,
  285. unsigned int *num_pins)
  286. {
  287. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  288. *pins = &pctl->groups[group].pin;
  289. *num_pins = 1;
  290. return 0;
  291. }
  292. static const struct pinctrl_ops oxnas_pinctrl_ops = {
  293. .get_groups_count = oxnas_pinctrl_get_groups_count,
  294. .get_group_name = oxnas_pinctrl_get_group_name,
  295. .get_group_pins = oxnas_pinctrl_get_group_pins,
  296. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  297. .dt_free_map = pinctrl_utils_free_map,
  298. };
  299. static int oxnas_pinmux_get_functions_count(struct pinctrl_dev *pctldev)
  300. {
  301. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  302. return pctl->nfunctions;
  303. }
  304. static const char *
  305. oxnas_pinmux_get_function_name(struct pinctrl_dev *pctldev, unsigned int func)
  306. {
  307. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  308. return pctl->functions[func].name;
  309. }
  310. static int oxnas_pinmux_get_function_groups(struct pinctrl_dev *pctldev,
  311. unsigned int func,
  312. const char * const **groups,
  313. unsigned int * const num_groups)
  314. {
  315. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  316. *groups = pctl->functions[func].groups;
  317. *num_groups = pctl->functions[func].ngroups;
  318. return 0;
  319. }
  320. static int oxnas_pinmux_enable(struct pinctrl_dev *pctldev,
  321. unsigned int func, unsigned int group)
  322. {
  323. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  324. const struct oxnas_pin_group *pg = &pctl->groups[group];
  325. const struct oxnas_function *pf = &pctl->functions[func];
  326. const char *fname = pf->name;
  327. struct oxnas_desc_function *functions = pg->functions;
  328. u32 mask = BIT(pg->pin);
  329. while (functions->name) {
  330. if (!strcmp(functions->name, fname)) {
  331. dev_dbg(pctl->dev,
  332. "setting function %s bank %d pin %d fct %d mask %x\n",
  333. fname, pg->bank, pg->pin,
  334. functions->fct, mask);
  335. regmap_write_bits(pctl->regmap,
  336. (pg->bank ?
  337. PINMUX_PRIMARY_SEL1 :
  338. PINMUX_PRIMARY_SEL0),
  339. mask,
  340. (functions->fct == 1 ?
  341. mask : 0));
  342. regmap_write_bits(pctl->regmap,
  343. (pg->bank ?
  344. PINMUX_SECONDARY_SEL1 :
  345. PINMUX_SECONDARY_SEL0),
  346. mask,
  347. (functions->fct == 2 ?
  348. mask : 0));
  349. regmap_write_bits(pctl->regmap,
  350. (pg->bank ?
  351. PINMUX_TERTIARY_SEL1 :
  352. PINMUX_TERTIARY_SEL0),
  353. mask,
  354. (functions->fct == 3 ?
  355. mask : 0));
  356. return 0;
  357. }
  358. functions++;
  359. }
  360. dev_err(pctl->dev, "cannot mux pin %u to function %u\n", group, func);
  361. return -EINVAL;
  362. }
  363. static int oxnas_gpio_request_enable(struct pinctrl_dev *pctldev,
  364. struct pinctrl_gpio_range *range,
  365. unsigned int offset)
  366. {
  367. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  368. struct oxnas_gpio_bank *bank = gpiochip_get_data(range->gc);
  369. u32 mask = BIT(offset - bank->gpio_chip.base);
  370. dev_dbg(pctl->dev, "requesting gpio %d in bank %d (id %d) with mask 0x%x\n",
  371. offset, bank->gpio_chip.base, bank->id, mask);
  372. regmap_write_bits(pctl->regmap,
  373. (bank->id ?
  374. PINMUX_PRIMARY_SEL1 :
  375. PINMUX_PRIMARY_SEL0),
  376. mask, 0);
  377. regmap_write_bits(pctl->regmap,
  378. (bank->id ?
  379. PINMUX_SECONDARY_SEL1 :
  380. PINMUX_SECONDARY_SEL0),
  381. mask, 0);
  382. regmap_write_bits(pctl->regmap,
  383. (bank->id ?
  384. PINMUX_TERTIARY_SEL1 :
  385. PINMUX_TERTIARY_SEL0),
  386. mask, 0);
  387. return 0;
  388. }
  389. static int oxnas_gpio_get_direction(struct gpio_chip *chip,
  390. unsigned int offset)
  391. {
  392. struct oxnas_gpio_bank *bank = gpiochip_get_data(chip);
  393. u32 mask = BIT(offset);
  394. return !(readl_relaxed(bank->reg_base + OUTPUT_EN) & mask);
  395. }
  396. static int oxnas_gpio_direction_input(struct gpio_chip *chip,
  397. unsigned int offset)
  398. {
  399. struct oxnas_gpio_bank *bank = gpiochip_get_data(chip);
  400. u32 mask = BIT(offset);
  401. writel_relaxed(mask, bank->reg_base + OUTPUT_EN_CLEAR);
  402. return 0;
  403. }
  404. static int oxnas_gpio_get(struct gpio_chip *chip, unsigned int offset)
  405. {
  406. struct oxnas_gpio_bank *bank = gpiochip_get_data(chip);
  407. u32 mask = BIT(offset);
  408. return (readl_relaxed(bank->reg_base + INPUT_VALUE) & mask) != 0;
  409. }
  410. static void oxnas_gpio_set(struct gpio_chip *chip, unsigned int offset,
  411. int value)
  412. {
  413. struct oxnas_gpio_bank *bank = gpiochip_get_data(chip);
  414. u32 mask = BIT(offset);
  415. if (value)
  416. writel_relaxed(mask, bank->reg_base + OUTPUT_SET);
  417. else
  418. writel_relaxed(mask, bank->reg_base + OUTPUT_CLEAR);
  419. }
  420. static int oxnas_gpio_direction_output(struct gpio_chip *chip,
  421. unsigned int offset, int value)
  422. {
  423. struct oxnas_gpio_bank *bank = gpiochip_get_data(chip);
  424. u32 mask = BIT(offset);
  425. oxnas_gpio_set(chip, offset, value);
  426. writel_relaxed(mask, bank->reg_base + OUTPUT_EN_SET);
  427. return 0;
  428. }
  429. static int oxnas_gpio_set_direction(struct pinctrl_dev *pctldev,
  430. struct pinctrl_gpio_range *range,
  431. unsigned int offset, bool input)
  432. {
  433. struct gpio_chip *chip = range->gc;
  434. if (input)
  435. oxnas_gpio_direction_input(chip, offset);
  436. else
  437. oxnas_gpio_direction_output(chip, offset, 0);
  438. return 0;
  439. }
  440. static const struct pinmux_ops oxnas_pinmux_ops = {
  441. .get_functions_count = oxnas_pinmux_get_functions_count,
  442. .get_function_name = oxnas_pinmux_get_function_name,
  443. .get_function_groups = oxnas_pinmux_get_function_groups,
  444. .set_mux = oxnas_pinmux_enable,
  445. .gpio_request_enable = oxnas_gpio_request_enable,
  446. .gpio_set_direction = oxnas_gpio_set_direction,
  447. };
  448. static int oxnas_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  449. unsigned long *config)
  450. {
  451. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  452. struct oxnas_gpio_bank *bank = pctl_to_bank(pctl, pin);
  453. unsigned int param = pinconf_to_config_param(*config);
  454. u32 mask = BIT(pin - bank->gpio_chip.base);
  455. int ret;
  456. u32 arg;
  457. switch (param) {
  458. case PIN_CONFIG_BIAS_PULL_UP:
  459. ret = regmap_read(pctl->regmap,
  460. (bank->id ?
  461. PINMUX_PULLUP_CTRL1 :
  462. PINMUX_PULLUP_CTRL0),
  463. &arg);
  464. if (ret)
  465. return ret;
  466. arg = !!(arg & mask);
  467. break;
  468. default:
  469. return -ENOTSUPP;
  470. }
  471. *config = pinconf_to_config_packed(param, arg);
  472. return 0;
  473. }
  474. static int oxnas_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  475. unsigned long *configs, unsigned int num_configs)
  476. {
  477. struct oxnas_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  478. struct oxnas_gpio_bank *bank = pctl_to_bank(pctl, pin);
  479. unsigned int param;
  480. u32 arg;
  481. unsigned int i;
  482. u32 offset = pin - bank->gpio_chip.base;
  483. u32 mask = BIT(offset);
  484. dev_dbg(pctl->dev, "setting pin %d bank %d mask 0x%x\n",
  485. pin, bank->gpio_chip.base, mask);
  486. for (i = 0; i < num_configs; i++) {
  487. param = pinconf_to_config_param(configs[i]);
  488. arg = pinconf_to_config_argument(configs[i]);
  489. switch (param) {
  490. case PIN_CONFIG_BIAS_PULL_UP:
  491. dev_dbg(pctl->dev, " pullup\n");
  492. regmap_write_bits(pctl->regmap,
  493. (bank->id ?
  494. PINMUX_PULLUP_CTRL1 :
  495. PINMUX_PULLUP_CTRL0),
  496. mask, mask);
  497. break;
  498. default:
  499. dev_err(pctl->dev, "Property %u not supported\n",
  500. param);
  501. return -ENOTSUPP;
  502. }
  503. }
  504. return 0;
  505. }
  506. static const struct pinconf_ops oxnas_pinconf_ops = {
  507. .pin_config_get = oxnas_pinconf_get,
  508. .pin_config_set = oxnas_pinconf_set,
  509. .is_generic = true,
  510. };
  511. static struct pinctrl_desc oxnas_pinctrl_desc = {
  512. .name = "oxnas-pinctrl",
  513. .pctlops = &oxnas_pinctrl_ops,
  514. .pmxops = &oxnas_pinmux_ops,
  515. .confops = &oxnas_pinconf_ops,
  516. .owner = THIS_MODULE,
  517. };
  518. static void oxnas_gpio_irq_ack(struct irq_data *data)
  519. {
  520. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  521. struct oxnas_gpio_bank *bank = gpiochip_get_data(chip);
  522. u32 mask = BIT(data->hwirq);
  523. writel(mask, bank->reg_base + IRQ_PENDING);
  524. }
  525. static void oxnas_gpio_irq_mask(struct irq_data *data)
  526. {
  527. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  528. struct oxnas_gpio_bank *bank = gpiochip_get_data(chip);
  529. unsigned int type = irqd_get_trigger_type(data);
  530. u32 mask = BIT(data->hwirq);
  531. if (type & IRQ_TYPE_EDGE_RISING)
  532. writel(readl(bank->reg_base + RE_IRQ_ENABLE) & ~mask,
  533. bank->reg_base + RE_IRQ_ENABLE);
  534. if (type & IRQ_TYPE_EDGE_FALLING)
  535. writel(readl(bank->reg_base + FE_IRQ_ENABLE) & ~mask,
  536. bank->reg_base + FE_IRQ_ENABLE);
  537. }
  538. static void oxnas_gpio_irq_unmask(struct irq_data *data)
  539. {
  540. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  541. struct oxnas_gpio_bank *bank = gpiochip_get_data(chip);
  542. unsigned int type = irqd_get_trigger_type(data);
  543. u32 mask = BIT(data->hwirq);
  544. if (type & IRQ_TYPE_EDGE_RISING)
  545. writel(readl(bank->reg_base + RE_IRQ_ENABLE) | mask,
  546. bank->reg_base + RE_IRQ_ENABLE);
  547. if (type & IRQ_TYPE_EDGE_FALLING)
  548. writel(readl(bank->reg_base + FE_IRQ_ENABLE) | mask,
  549. bank->reg_base + FE_IRQ_ENABLE);
  550. }
  551. static unsigned int oxnas_gpio_irq_startup(struct irq_data *data)
  552. {
  553. struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
  554. oxnas_gpio_direction_input(chip, data->hwirq);
  555. oxnas_gpio_irq_unmask(data);
  556. return 0;
  557. }
  558. static int oxnas_gpio_irq_set_type(struct irq_data *data, unsigned int type)
  559. {
  560. if ((type & (IRQ_TYPE_EDGE_RISING|IRQ_TYPE_EDGE_FALLING)) == 0)
  561. return -EINVAL;
  562. irq_set_handler_locked(data, handle_edge_irq);
  563. return 0;
  564. }
  565. static void oxnas_gpio_irq_handler(struct irq_desc *desc)
  566. {
  567. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  568. struct oxnas_gpio_bank *bank = gpiochip_get_data(gc);
  569. struct irq_chip *chip = irq_desc_get_chip(desc);
  570. unsigned long stat;
  571. unsigned int pin;
  572. chained_irq_enter(chip, desc);
  573. stat = readl(bank->reg_base + IRQ_PENDING);
  574. for_each_set_bit(pin, &stat, BITS_PER_LONG)
  575. generic_handle_irq(irq_linear_revmap(gc->irqdomain, pin));
  576. chained_irq_exit(chip, desc);
  577. }
  578. #define GPIO_BANK(_bank) \
  579. { \
  580. .gpio_chip = { \
  581. .label = "GPIO" #_bank, \
  582. .request = gpiochip_generic_request, \
  583. .free = gpiochip_generic_free, \
  584. .get_direction = oxnas_gpio_get_direction, \
  585. .direction_input = oxnas_gpio_direction_input, \
  586. .direction_output = oxnas_gpio_direction_output, \
  587. .get = oxnas_gpio_get, \
  588. .set = oxnas_gpio_set, \
  589. .ngpio = PINS_PER_BANK, \
  590. .base = GPIO_BANK_START(_bank), \
  591. .owner = THIS_MODULE, \
  592. .can_sleep = 0, \
  593. }, \
  594. .irq_chip = { \
  595. .name = "GPIO" #_bank, \
  596. .irq_startup = oxnas_gpio_irq_startup, \
  597. .irq_ack = oxnas_gpio_irq_ack, \
  598. .irq_mask = oxnas_gpio_irq_mask, \
  599. .irq_unmask = oxnas_gpio_irq_unmask, \
  600. .irq_set_type = oxnas_gpio_irq_set_type, \
  601. }, \
  602. }
  603. static struct oxnas_gpio_bank oxnas_gpio_banks[] = {
  604. GPIO_BANK(0),
  605. GPIO_BANK(1),
  606. };
  607. static int oxnas_pinctrl_probe(struct platform_device *pdev)
  608. {
  609. struct oxnas_pinctrl *pctl;
  610. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  611. if (!pctl)
  612. return -ENOMEM;
  613. pctl->dev = &pdev->dev;
  614. dev_set_drvdata(&pdev->dev, pctl);
  615. pctl->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  616. "oxsemi,sys-ctrl");
  617. if (IS_ERR(pctl->regmap)) {
  618. dev_err(&pdev->dev, "failed to get sys ctrl regmap\n");
  619. return -ENODEV;
  620. }
  621. pctl->pins = oxnas_pins;
  622. pctl->npins = ARRAY_SIZE(oxnas_pins);
  623. pctl->functions = oxnas_functions;
  624. pctl->nfunctions = ARRAY_SIZE(oxnas_functions);
  625. pctl->groups = oxnas_groups;
  626. pctl->ngroups = ARRAY_SIZE(oxnas_groups);
  627. pctl->gpio_banks = oxnas_gpio_banks;
  628. pctl->nbanks = ARRAY_SIZE(oxnas_gpio_banks);
  629. oxnas_pinctrl_desc.pins = pctl->pins;
  630. oxnas_pinctrl_desc.npins = pctl->npins;
  631. pctl->pctldev = pinctrl_register(&oxnas_pinctrl_desc,
  632. &pdev->dev, pctl);
  633. if (IS_ERR(pctl->pctldev)) {
  634. dev_err(&pdev->dev, "Failed to register pinctrl device\n");
  635. return PTR_ERR(pctl->pctldev);
  636. }
  637. return 0;
  638. }
  639. static int oxnas_gpio_probe(struct platform_device *pdev)
  640. {
  641. struct device_node *np = pdev->dev.of_node;
  642. struct of_phandle_args pinspec;
  643. struct oxnas_gpio_bank *bank;
  644. unsigned int id, ngpios;
  645. int irq, ret;
  646. struct resource *res;
  647. if (of_parse_phandle_with_fixed_args(np, "gpio-ranges",
  648. 3, 0, &pinspec)) {
  649. dev_err(&pdev->dev, "gpio-ranges property not found\n");
  650. return -EINVAL;
  651. }
  652. id = pinspec.args[1] / PINS_PER_BANK;
  653. ngpios = pinspec.args[2];
  654. if (id >= ARRAY_SIZE(oxnas_gpio_banks)) {
  655. dev_err(&pdev->dev, "invalid gpio-ranges base arg\n");
  656. return -EINVAL;
  657. }
  658. if (ngpios > PINS_PER_BANK) {
  659. dev_err(&pdev->dev, "invalid gpio-ranges count arg\n");
  660. return -EINVAL;
  661. }
  662. bank = &oxnas_gpio_banks[id];
  663. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  664. bank->reg_base = devm_ioremap_resource(&pdev->dev, res);
  665. if (IS_ERR(bank->reg_base))
  666. return PTR_ERR(bank->reg_base);
  667. irq = platform_get_irq(pdev, 0);
  668. if (irq < 0) {
  669. dev_err(&pdev->dev, "irq get failed\n");
  670. return irq;
  671. }
  672. bank->id = id;
  673. bank->gpio_chip.parent = &pdev->dev;
  674. bank->gpio_chip.of_node = np;
  675. bank->gpio_chip.ngpio = ngpios;
  676. ret = gpiochip_add_data(&bank->gpio_chip, bank);
  677. if (ret < 0) {
  678. dev_err(&pdev->dev, "Failed to add GPIO chip %u: %d\n",
  679. id, ret);
  680. return ret;
  681. }
  682. ret = gpiochip_irqchip_add(&bank->gpio_chip, &bank->irq_chip,
  683. 0, handle_level_irq, IRQ_TYPE_NONE);
  684. if (ret < 0) {
  685. dev_err(&pdev->dev, "Failed to add IRQ chip %u: %d\n",
  686. id, ret);
  687. gpiochip_remove(&bank->gpio_chip);
  688. return ret;
  689. }
  690. gpiochip_set_chained_irqchip(&bank->gpio_chip, &bank->irq_chip,
  691. irq, oxnas_gpio_irq_handler);
  692. return 0;
  693. }
  694. static const struct of_device_id oxnas_pinctrl_of_match[] = {
  695. { .compatible = "oxsemi,ox810se-pinctrl", },
  696. { },
  697. };
  698. static struct platform_driver oxnas_pinctrl_driver = {
  699. .driver = {
  700. .name = "oxnas-pinctrl",
  701. .of_match_table = oxnas_pinctrl_of_match,
  702. .suppress_bind_attrs = true,
  703. },
  704. .probe = oxnas_pinctrl_probe,
  705. };
  706. static const struct of_device_id oxnas_gpio_of_match[] = {
  707. { .compatible = "oxsemi,ox810se-gpio", },
  708. { },
  709. };
  710. static struct platform_driver oxnas_gpio_driver = {
  711. .driver = {
  712. .name = "oxnas-gpio",
  713. .of_match_table = oxnas_gpio_of_match,
  714. .suppress_bind_attrs = true,
  715. },
  716. .probe = oxnas_gpio_probe,
  717. };
  718. static int __init oxnas_gpio_register(void)
  719. {
  720. return platform_driver_register(&oxnas_gpio_driver);
  721. }
  722. arch_initcall(oxnas_gpio_register);
  723. static int __init oxnas_pinctrl_register(void)
  724. {
  725. return platform_driver_register(&oxnas_pinctrl_driver);
  726. }
  727. arch_initcall(oxnas_pinctrl_register);