pinctrl-oxnas.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. /*
  2. * oxnas pinctrl driver based on at91 pinctrl driver
  3. *
  4. * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  5. *
  6. * Under GPLv2 only
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/err.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. #include <linux/io.h>
  22. #include <linux/gpio.h>
  23. #include <linux/pinctrl/machine.h>
  24. #include <linux/pinctrl/pinconf.h>
  25. #include <linux/pinctrl/pinctrl.h>
  26. #include <linux/pinctrl/pinmux.h>
  27. /* Since we request GPIOs from ourself */
  28. #include <linux/pinctrl/consumer.h>
  29. #include <linux/spinlock.h>
  30. #include "core.h"
  31. #include <mach/utils.h>
  32. #define MAX_NB_GPIO_PER_BANK 32
  33. #define MAX_GPIO_BANKS 2
  34. struct oxnas_gpio_chip {
  35. struct gpio_chip chip;
  36. struct pinctrl_gpio_range range;
  37. void __iomem *regbase; /* GPIOA/B virtual address */
  38. void __iomem *ctrlbase; /* SYS/SEC_CTRL virtual address */
  39. struct irq_domain *domain; /* associated irq domain */
  40. spinlock_t lock;
  41. };
  42. #define to_oxnas_gpio_chip(c) container_of(c, struct oxnas_gpio_chip, chip)
  43. static struct oxnas_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
  44. static int gpio_banks;
  45. #define PULL_UP (1 << 0)
  46. #define PULL_DOWN (1 << 1)
  47. #define DEBOUNCE (1 << 2)
  48. /**
  49. * struct oxnas_pmx_func - describes pinmux functions
  50. * @name: the name of this specific function
  51. * @groups: corresponding pin groups
  52. * @ngroups: the number of groups
  53. */
  54. struct oxnas_pmx_func {
  55. const char *name;
  56. const char **groups;
  57. unsigned ngroups;
  58. };
  59. enum oxnas_mux {
  60. OXNAS_PINMUX_GPIO,
  61. OXNAS_PINMUX_FUNC2,
  62. OXNAS_PINMUX_FUNC3,
  63. OXNAS_PINMUX_FUNC4,
  64. OXNAS_PINMUX_DEBUG,
  65. OXNAS_PINMUX_ALT,
  66. };
  67. enum {
  68. INPUT_VALUE = 0,
  69. OUTPUT_ENABLE = 4,
  70. IRQ_PENDING = 0xC,
  71. OUTPUT_VALUE = 0x10,
  72. OUTPUT_SET = 0x14,
  73. OUTPUT_CLEAR = 0x18,
  74. OUTPUT_EN_SET = 0x1C,
  75. OUTPUT_EN_CLEAR = 0x20,
  76. DEBOUNCE_ENABLE = 0x24,
  77. RE_IRQ_ENABLE = 0x28, /* rising edge */
  78. FE_IRQ_ENABLE = 0x2C, /* falling edge */
  79. RE_IRQ_PENDING = 0x30, /* rising edge */
  80. FE_IRQ_PENDING = 0x34, /* falling edge */
  81. CLOCK_DIV = 0x48,
  82. PULL_ENABLE = 0x50,
  83. PULL_SENSE = 0x54, /* 1 up, 0 down */
  84. DEBOUNCE_MASK = 0x3FFF0000,
  85. /* put hw debounce and soft config at same bit position*/
  86. DEBOUNCE_SHIFT = 16
  87. };
  88. enum {
  89. PINMUX_SECONDARY_SEL = 0x14,
  90. PINMUX_TERTIARY_SEL = 0x8c,
  91. PINMUX_QUATERNARY_SEL = 0x94,
  92. PINMUX_DEBUG_SEL = 0x9c,
  93. PINMUX_ALTERNATIVE_SEL = 0xa4,
  94. PINMUX_PULLUP_SEL = 0xac,
  95. };
  96. /**
  97. * struct oxnas_pmx_pin - describes an pin mux
  98. * @bank: the bank of the pin
  99. * @pin: the pin number in the @bank
  100. * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
  101. * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
  102. */
  103. struct oxnas_pmx_pin {
  104. uint32_t bank;
  105. uint32_t pin;
  106. enum oxnas_mux mux;
  107. unsigned long conf;
  108. };
  109. /**
  110. * struct oxnas_pin_group - describes an pin group
  111. * @name: the name of this specific pin group
  112. * @pins_conf: the mux mode for each pin in this group. The size of this
  113. * array is the same as pins.
  114. * @pins: an array of discrete physical pins used in this group, taken
  115. * from the driver-local pin enumeration space
  116. * @npins: the number of pins in this group array, i.e. the number of
  117. * elements in .pins so we can iterate over that array
  118. */
  119. struct oxnas_pin_group {
  120. const char *name;
  121. struct oxnas_pmx_pin *pins_conf;
  122. unsigned int *pins;
  123. unsigned npins;
  124. };
  125. struct oxnas_pinctrl {
  126. struct device *dev;
  127. struct pinctrl_dev *pctl;
  128. int nbanks;
  129. uint32_t *mux_mask;
  130. int nmux;
  131. struct oxnas_pmx_func *functions;
  132. int nfunctions;
  133. struct oxnas_pin_group *groups;
  134. int ngroups;
  135. };
  136. static const inline struct oxnas_pin_group *oxnas_pinctrl_find_group_by_name(
  137. const struct oxnas_pinctrl *info,
  138. const char *name)
  139. {
  140. const struct oxnas_pin_group *grp = NULL;
  141. int i;
  142. for (i = 0; i < info->ngroups; i++) {
  143. if (strcmp(info->groups[i].name, name))
  144. continue;
  145. grp = &info->groups[i];
  146. dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins,
  147. grp->pins[0]);
  148. break;
  149. }
  150. return grp;
  151. }
  152. static int oxnas_get_groups_count(struct pinctrl_dev *pctldev)
  153. {
  154. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  155. return info->ngroups;
  156. }
  157. static const char *oxnas_get_group_name(struct pinctrl_dev *pctldev,
  158. unsigned selector)
  159. {
  160. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  161. return info->groups[selector].name;
  162. }
  163. static int oxnas_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  164. const unsigned **pins,
  165. unsigned *npins)
  166. {
  167. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  168. if (selector >= info->ngroups)
  169. return -EINVAL;
  170. *pins = info->groups[selector].pins;
  171. *npins = info->groups[selector].npins;
  172. return 0;
  173. }
  174. static void oxnas_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  175. unsigned offset)
  176. {
  177. seq_printf(s, "%s", dev_name(pctldev->dev));
  178. }
  179. static int oxnas_dt_node_to_map(struct pinctrl_dev *pctldev,
  180. struct device_node *np,
  181. struct pinctrl_map **map, unsigned *num_maps)
  182. {
  183. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  184. const struct oxnas_pin_group *grp;
  185. struct pinctrl_map *new_map;
  186. struct device_node *parent;
  187. int map_num = 1;
  188. int i;
  189. /*
  190. * first find the group of this node and check if we need create
  191. * config maps for pins
  192. */
  193. grp = oxnas_pinctrl_find_group_by_name(info, np->name);
  194. if (!grp) {
  195. dev_err(info->dev, "unable to find group for node %s\n",
  196. np->name);
  197. return -EINVAL;
  198. }
  199. map_num += grp->npins;
  200. new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num,
  201. GFP_KERNEL);
  202. if (!new_map)
  203. return -ENOMEM;
  204. *map = new_map;
  205. *num_maps = map_num;
  206. /* create mux map */
  207. parent = of_get_parent(np);
  208. if (!parent) {
  209. devm_kfree(pctldev->dev, new_map);
  210. return -EINVAL;
  211. }
  212. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  213. new_map[0].data.mux.function = parent->name;
  214. new_map[0].data.mux.group = np->name;
  215. of_node_put(parent);
  216. /* create config map */
  217. new_map++;
  218. for (i = 0; i < grp->npins; i++) {
  219. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  220. new_map[i].data.configs.group_or_pin =
  221. pin_get_name(pctldev, grp->pins[i]);
  222. new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
  223. new_map[i].data.configs.num_configs = 1;
  224. }
  225. dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
  226. (*map)->data.mux.function, (*map)->data.mux.group, map_num);
  227. return 0;
  228. }
  229. static void oxnas_dt_free_map(struct pinctrl_dev *pctldev,
  230. struct pinctrl_map *map, unsigned num_maps)
  231. {
  232. }
  233. static const struct pinctrl_ops oxnas_pctrl_ops = {
  234. .get_groups_count = oxnas_get_groups_count,
  235. .get_group_name = oxnas_get_group_name,
  236. .get_group_pins = oxnas_get_group_pins,
  237. .pin_dbg_show = oxnas_pin_dbg_show,
  238. .dt_node_to_map = oxnas_dt_node_to_map,
  239. .dt_free_map = oxnas_dt_free_map,
  240. };
  241. static void __iomem *pin_to_gpioctrl(struct oxnas_pinctrl *info,
  242. unsigned int bank)
  243. {
  244. return gpio_chips[bank]->regbase;
  245. }
  246. static void __iomem *pin_to_muxctrl(struct oxnas_pinctrl *info,
  247. unsigned int bank)
  248. {
  249. return gpio_chips[bank]->ctrlbase;
  250. }
  251. static inline int pin_to_bank(unsigned pin)
  252. {
  253. return pin / MAX_NB_GPIO_PER_BANK;
  254. }
  255. static unsigned pin_to_mask(unsigned int pin)
  256. {
  257. return 1 << pin;
  258. }
  259. static void oxnas_mux_disable_interrupt(void __iomem *pio, unsigned mask)
  260. {
  261. oxnas_register_clear_mask(pio + RE_IRQ_ENABLE, mask);
  262. oxnas_register_clear_mask(pio + FE_IRQ_ENABLE, mask);
  263. }
  264. static unsigned oxnas_mux_get_pullup(void __iomem *pio, unsigned pin)
  265. {
  266. return (readl_relaxed(pio + PULL_ENABLE) & BIT(pin)) &&
  267. (readl_relaxed(pio + PULL_SENSE) & BIT(pin));
  268. }
  269. static void oxnas_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
  270. {
  271. if (on) {
  272. oxnas_register_set_mask(pio + PULL_SENSE, mask);
  273. oxnas_register_set_mask(pio + PULL_ENABLE, mask);
  274. } else {
  275. oxnas_register_clear_mask(pio + PULL_ENABLE, mask);
  276. }
  277. }
  278. static bool oxnas_mux_get_pulldown(void __iomem *pio, unsigned pin)
  279. {
  280. return (readl_relaxed(pio + PULL_ENABLE) & BIT(pin)) &&
  281. (!(readl_relaxed(pio + PULL_SENSE) & BIT(pin)));
  282. }
  283. static void oxnas_mux_set_pulldown(void __iomem *pio, unsigned mask, bool on)
  284. {
  285. if (on) {
  286. oxnas_register_clear_mask(pio + PULL_SENSE, mask);
  287. oxnas_register_set_mask(pio + PULL_ENABLE, mask);
  288. } else {
  289. oxnas_register_clear_mask(pio + PULL_ENABLE, mask);
  290. };
  291. }
  292. /* unfortunately debounce control are shared */
  293. static bool oxnas_mux_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
  294. {
  295. *div = __raw_readl(pio + CLOCK_DIV) & DEBOUNCE_MASK;
  296. return __raw_readl(pio + DEBOUNCE_ENABLE) & BIT(pin);
  297. }
  298. static void oxnas_mux_set_debounce(void __iomem *pio, unsigned mask,
  299. bool is_on, u32 div)
  300. {
  301. if (is_on) {
  302. oxnas_register_value_mask(pio + CLOCK_DIV, DEBOUNCE_MASK, div);
  303. oxnas_register_set_mask(pio + DEBOUNCE_ENABLE, mask);
  304. } else {
  305. oxnas_register_clear_mask(pio + DEBOUNCE_ENABLE, mask);
  306. }
  307. }
  308. static void oxnas_mux_set_func2(void __iomem *cio, unsigned mask)
  309. {
  310. /* in fact, SECONDARY takes precedence, so clear others is not necessary */
  311. oxnas_register_set_mask(cio + PINMUX_SECONDARY_SEL, mask);
  312. oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
  313. oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
  314. oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
  315. oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
  316. }
  317. static void oxnas_mux_set_func3(void __iomem *cio, unsigned mask)
  318. {
  319. oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
  320. oxnas_register_set_mask(cio + PINMUX_TERTIARY_SEL, mask);
  321. oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
  322. oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
  323. oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
  324. }
  325. static void oxnas_mux_set_func4(void __iomem *cio, unsigned mask)
  326. {
  327. oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
  328. oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
  329. oxnas_register_set_mask(cio + PINMUX_QUATERNARY_SEL, mask);
  330. oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
  331. oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
  332. }
  333. static void oxnas_mux_set_func_dbg(void __iomem *cio, unsigned mask)
  334. {
  335. oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
  336. oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
  337. oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
  338. oxnas_register_set_mask(cio + PINMUX_DEBUG_SEL, mask);
  339. oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
  340. }
  341. static void oxnas_mux_set_func_alt(void __iomem *cio, unsigned mask)
  342. {
  343. oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
  344. oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
  345. oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
  346. oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
  347. oxnas_register_set_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
  348. }
  349. static void oxnas_mux_set_gpio(void __iomem *cio, unsigned mask)
  350. {
  351. oxnas_register_clear_mask(cio + PINMUX_SECONDARY_SEL, mask);
  352. oxnas_register_clear_mask(cio + PINMUX_TERTIARY_SEL, mask);
  353. oxnas_register_clear_mask(cio + PINMUX_QUATERNARY_SEL, mask);
  354. oxnas_register_clear_mask(cio + PINMUX_DEBUG_SEL, mask);
  355. oxnas_register_clear_mask(cio + PINMUX_ALTERNATIVE_SEL, mask);
  356. }
  357. static enum oxnas_mux oxnas_mux_get_func(void __iomem *cio, unsigned mask)
  358. {
  359. if (readl_relaxed(cio + PINMUX_SECONDARY_SEL) & mask)
  360. return OXNAS_PINMUX_FUNC2;
  361. if (readl_relaxed(cio + PINMUX_TERTIARY_SEL) & mask)
  362. return OXNAS_PINMUX_FUNC3;
  363. if (readl_relaxed(cio + PINMUX_QUATERNARY_SEL) & mask)
  364. return OXNAS_PINMUX_FUNC4;
  365. if (readl_relaxed(cio + PINMUX_DEBUG_SEL) & mask)
  366. return OXNAS_PINMUX_DEBUG;
  367. if (readl_relaxed(cio + PINMUX_ALTERNATIVE_SEL) & mask)
  368. return OXNAS_PINMUX_ALT;
  369. return OXNAS_PINMUX_GPIO;
  370. }
  371. static void oxnas_pin_dbg(const struct device *dev,
  372. const struct oxnas_pmx_pin *pin)
  373. {
  374. if (pin->mux) {
  375. dev_dbg(dev,
  376. "MF_%c%d configured as periph%c with conf = 0x%lu\n",
  377. pin->bank + 'A', pin->pin, pin->mux - 1 + 'A',
  378. pin->conf);
  379. } else {
  380. dev_dbg(dev, "MF_%c%d configured as gpio with conf = 0x%lu\n",
  381. pin->bank + 'A', pin->pin, pin->conf);
  382. }
  383. }
  384. static int pin_check_config(struct oxnas_pinctrl *info, const char *name,
  385. int index, const struct oxnas_pmx_pin *pin)
  386. {
  387. int mux;
  388. /* check if it's a valid config */
  389. if (pin->bank >= info->nbanks) {
  390. dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
  391. name, index, pin->bank, info->nbanks);
  392. return -EINVAL;
  393. }
  394. if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
  395. dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
  396. name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
  397. return -EINVAL;
  398. }
  399. /* gpio always allowed */
  400. if (!pin->mux)
  401. return 0;
  402. mux = pin->mux - 1;
  403. if (mux >= info->nmux) {
  404. dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
  405. name, index, mux, info->nmux);
  406. return -EINVAL;
  407. }
  408. if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
  409. dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for MF_%c%d\n",
  410. name, index, mux, pin->bank + 'A', pin->pin);
  411. return -EINVAL;
  412. }
  413. return 0;
  414. }
  415. static void oxnas_mux_gpio_enable(void __iomem *cio, void __iomem *pio,
  416. unsigned mask, bool input)
  417. {
  418. oxnas_mux_set_gpio(cio, mask);
  419. if (input)
  420. writel_relaxed(mask, pio + OUTPUT_EN_CLEAR);
  421. else
  422. writel_relaxed(mask, pio + OUTPUT_EN_SET);
  423. }
  424. static void oxnas_mux_gpio_disable(void __iomem *cio, void __iomem *pio,
  425. unsigned mask)
  426. {
  427. /* when switch to other function, gpio is disabled automatically */
  428. return;
  429. }
  430. static int oxnas_pmx_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
  431. unsigned group)
  432. {
  433. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  434. const struct oxnas_pmx_pin *pins_conf = info->groups[group].pins_conf;
  435. const struct oxnas_pmx_pin *pin;
  436. uint32_t npins = info->groups[group].npins;
  437. int i, ret;
  438. unsigned mask;
  439. void __iomem *pio;
  440. void __iomem *cio;
  441. dev_dbg(info->dev, "enable function %s group %s\n",
  442. info->functions[selector].name, info->groups[group].name);
  443. /* first check that all the pins of the group are valid with a valid
  444. * paramter */
  445. for (i = 0; i < npins; i++) {
  446. pin = &pins_conf[i];
  447. ret = pin_check_config(info, info->groups[group].name, i, pin);
  448. if (ret)
  449. return ret;
  450. }
  451. for (i = 0; i < npins; i++) {
  452. pin = &pins_conf[i];
  453. oxnas_pin_dbg(info->dev, pin);
  454. pio = pin_to_gpioctrl(info, pin->bank);
  455. cio = pin_to_muxctrl(info, pin->bank);
  456. mask = pin_to_mask(pin->pin);
  457. oxnas_mux_disable_interrupt(pio, mask);
  458. switch (pin->mux) {
  459. case OXNAS_PINMUX_GPIO:
  460. oxnas_mux_gpio_enable(cio, pio, mask, 1);
  461. break;
  462. case OXNAS_PINMUX_FUNC2:
  463. oxnas_mux_set_func2(cio, mask);
  464. break;
  465. case OXNAS_PINMUX_FUNC3:
  466. oxnas_mux_set_func3(cio, mask);
  467. break;
  468. case OXNAS_PINMUX_FUNC4:
  469. oxnas_mux_set_func4(cio, mask);
  470. break;
  471. case OXNAS_PINMUX_DEBUG:
  472. oxnas_mux_set_func_dbg(cio, mask);
  473. break;
  474. case OXNAS_PINMUX_ALT:
  475. oxnas_mux_set_func_alt(cio, mask);
  476. break;
  477. }
  478. if (pin->mux)
  479. oxnas_mux_gpio_disable(cio, pio, mask);
  480. }
  481. return 0;
  482. }
  483. static int oxnas_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  484. {
  485. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  486. return info->nfunctions;
  487. }
  488. static const char *oxnas_pmx_get_func_name(struct pinctrl_dev *pctldev,
  489. unsigned selector)
  490. {
  491. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  492. return info->functions[selector].name;
  493. }
  494. static int oxnas_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  495. const char * const **groups,
  496. unsigned * const num_groups)
  497. {
  498. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  499. *groups = info->functions[selector].groups;
  500. *num_groups = info->functions[selector].ngroups;
  501. return 0;
  502. }
  503. static int oxnas_gpio_request_enable(struct pinctrl_dev *pctldev,
  504. struct pinctrl_gpio_range *range,
  505. unsigned offset)
  506. {
  507. struct oxnas_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  508. struct oxnas_gpio_chip *oxnas_chip;
  509. struct gpio_chip *chip;
  510. unsigned mask;
  511. if (!range) {
  512. dev_err(npct->dev, "invalid range\n");
  513. return -EINVAL;
  514. }
  515. if (!range->gc) {
  516. dev_err(npct->dev, "missing GPIO chip in range\n");
  517. return -EINVAL;
  518. }
  519. chip = range->gc;
  520. oxnas_chip = container_of(chip, struct oxnas_gpio_chip, chip);
  521. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  522. mask = 1 << (offset - chip->base);
  523. dev_dbg(npct->dev, "enable pin %u as MF_%c%d 0x%x\n",
  524. offset, 'A' + range->id, offset - chip->base, mask);
  525. oxnas_mux_set_gpio(oxnas_chip->ctrlbase, mask);
  526. return 0;
  527. }
  528. static void oxnas_gpio_disable_free(struct pinctrl_dev *pctldev,
  529. struct pinctrl_gpio_range *range,
  530. unsigned offset)
  531. {
  532. struct oxnas_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  533. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  534. /* Set the pin to some default state, GPIO is usually default */
  535. }
  536. static const struct pinmux_ops oxnas_pmx_ops = {
  537. .get_functions_count = oxnas_pmx_get_funcs_count,
  538. .get_function_name = oxnas_pmx_get_func_name,
  539. .get_function_groups = oxnas_pmx_get_groups,
  540. .set_mux = oxnas_pmx_set_mux,
  541. .gpio_request_enable = oxnas_gpio_request_enable,
  542. .gpio_disable_free = oxnas_gpio_disable_free,
  543. };
  544. static int oxnas_pinconf_get(struct pinctrl_dev *pctldev,
  545. unsigned pin_id, unsigned long *config)
  546. {
  547. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  548. void __iomem *pio;
  549. unsigned pin;
  550. int div;
  551. dev_dbg(info->dev, "%s:%d, pin_id=%d, config=0x%lx", __func__,
  552. __LINE__, pin_id, *config);
  553. pio = pin_to_gpioctrl(info, pin_to_bank(pin_id));
  554. pin = pin_id % MAX_NB_GPIO_PER_BANK;
  555. if (oxnas_mux_get_pullup(pio, pin))
  556. *config |= PULL_UP;
  557. if (oxnas_mux_get_pulldown(pio, pin))
  558. *config |= PULL_DOWN;
  559. if (oxnas_mux_get_debounce(pio, pin, &div))
  560. *config |= DEBOUNCE | div;
  561. return 0;
  562. }
  563. static int oxnas_pinconf_set(struct pinctrl_dev *pctldev,
  564. unsigned pin_id, unsigned long *configs,
  565. unsigned num_configs)
  566. {
  567. struct oxnas_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  568. unsigned mask;
  569. void __iomem *pio;
  570. int i;
  571. unsigned long config;
  572. pio = pin_to_gpioctrl(info, pin_to_bank(pin_id));
  573. mask = pin_to_mask(pin_id % MAX_NB_GPIO_PER_BANK);
  574. for (i = 0; i < num_configs; i++) {
  575. config = configs[i];
  576. dev_dbg(info->dev,
  577. "%s:%d, pin_id=%d, config=0x%lx",
  578. __func__, __LINE__, pin_id, config);
  579. if ((config & PULL_UP) && (config & PULL_DOWN))
  580. return -EINVAL;
  581. oxnas_mux_set_pullup(pio, mask, config & PULL_UP);
  582. oxnas_mux_set_pulldown(pio, mask, config & PULL_DOWN);
  583. oxnas_mux_set_debounce(pio, mask, config & DEBOUNCE,
  584. config & DEBOUNCE_MASK);
  585. } /* for each config */
  586. return 0;
  587. }
  588. static void oxnas_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  589. struct seq_file *s, unsigned pin_id)
  590. {
  591. }
  592. static void oxnas_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  593. struct seq_file *s, unsigned group)
  594. {
  595. }
  596. static const struct pinconf_ops oxnas_pinconf_ops = {
  597. .pin_config_get = oxnas_pinconf_get,
  598. .pin_config_set = oxnas_pinconf_set,
  599. .pin_config_dbg_show = oxnas_pinconf_dbg_show,
  600. .pin_config_group_dbg_show = oxnas_pinconf_group_dbg_show,
  601. };
  602. static struct pinctrl_desc oxnas_pinctrl_desc = {
  603. .pctlops = &oxnas_pctrl_ops,
  604. .pmxops = &oxnas_pmx_ops,
  605. .confops = &oxnas_pinconf_ops,
  606. .owner = THIS_MODULE,
  607. };
  608. static const char *gpio_compat = "plxtech,nas782x-gpio";
  609. static void oxnas_pinctrl_child_count(struct oxnas_pinctrl *info,
  610. struct device_node *np)
  611. {
  612. struct device_node *child;
  613. for_each_child_of_node(np, child) {
  614. if (of_device_is_compatible(child, gpio_compat)) {
  615. info->nbanks++;
  616. } else {
  617. info->nfunctions++;
  618. info->ngroups += of_get_child_count(child);
  619. }
  620. }
  621. }
  622. static int oxnas_pinctrl_mux_mask(struct oxnas_pinctrl *info,
  623. struct device_node *np)
  624. {
  625. int ret = 0;
  626. int size;
  627. const __be32 *list;
  628. list = of_get_property(np, "plxtech,mux-mask", &size);
  629. if (!list) {
  630. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  631. return -EINVAL;
  632. }
  633. size /= sizeof(*list);
  634. if (!size || size % info->nbanks) {
  635. dev_err(info->dev, "wrong mux mask array should be by %d\n",
  636. info->nbanks);
  637. return -EINVAL;
  638. }
  639. info->nmux = size / info->nbanks;
  640. info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
  641. if (!info->mux_mask) {
  642. dev_err(info->dev, "could not alloc mux_mask\n");
  643. return -ENOMEM;
  644. }
  645. ret = of_property_read_u32_array(np, "plxtech,mux-mask",
  646. info->mux_mask, size);
  647. if (ret)
  648. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  649. return ret;
  650. }
  651. static int oxnas_pinctrl_parse_groups(struct device_node *np,
  652. struct oxnas_pin_group *grp,
  653. struct oxnas_pinctrl *info, u32 index)
  654. {
  655. struct oxnas_pmx_pin *pin;
  656. int size;
  657. const __be32 *list;
  658. int i, j;
  659. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  660. /* Initialise group */
  661. grp->name = np->name;
  662. /*
  663. * the binding format is plxtech,pins = <bank pin mux CONFIG ...>,
  664. * do sanity check and calculate pins number
  665. */
  666. list = of_get_property(np, "plxtech,pins", &size);
  667. /* we do not check return since it's safe node passed down */
  668. size /= sizeof(*list);
  669. if (!size || size % 4) {
  670. dev_err(info->dev, "wrong pins number or pins and configs"
  671. " should be divisible by 4\n");
  672. return -EINVAL;
  673. }
  674. grp->npins = size / 4;
  675. pin = grp->pins_conf = devm_kzalloc(info->dev,
  676. grp->npins * sizeof(struct oxnas_pmx_pin),
  677. GFP_KERNEL);
  678. grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
  679. GFP_KERNEL);
  680. if (!grp->pins_conf || !grp->pins)
  681. return -ENOMEM;
  682. for (i = 0, j = 0; i < size; i += 4, j++) {
  683. pin->bank = be32_to_cpu(*list++);
  684. pin->pin = be32_to_cpu(*list++);
  685. grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
  686. pin->mux = be32_to_cpu(*list++);
  687. pin->conf = be32_to_cpu(*list++);
  688. oxnas_pin_dbg(info->dev, pin);
  689. pin++;
  690. }
  691. return 0;
  692. }
  693. static int oxnas_pinctrl_parse_functions(struct device_node *np,
  694. struct oxnas_pinctrl *info, u32 index)
  695. {
  696. struct device_node *child;
  697. struct oxnas_pmx_func *func;
  698. struct oxnas_pin_group *grp;
  699. int ret;
  700. static u32 grp_index;
  701. u32 i = 0;
  702. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  703. func = &info->functions[index];
  704. /* Initialise function */
  705. func->name = np->name;
  706. func->ngroups = of_get_child_count(np);
  707. if (func->ngroups <= 0) {
  708. dev_err(info->dev, "no groups defined\n");
  709. return -EINVAL;
  710. }
  711. func->groups = devm_kzalloc(info->dev,
  712. func->ngroups * sizeof(char *), GFP_KERNEL);
  713. if (!func->groups)
  714. return -ENOMEM;
  715. for_each_child_of_node(np, child) {
  716. func->groups[i] = child->name;
  717. grp = &info->groups[grp_index++];
  718. ret = oxnas_pinctrl_parse_groups(child, grp, info, i++);
  719. if (ret)
  720. return ret;
  721. }
  722. return 0;
  723. }
  724. static struct of_device_id oxnas_pinctrl_of_match[] = {
  725. { .compatible = "plxtech,nas782x-pinctrl"},
  726. { /* sentinel */ }
  727. };
  728. static int oxnas_pinctrl_probe_dt(struct platform_device *pdev,
  729. struct oxnas_pinctrl *info)
  730. {
  731. int ret = 0;
  732. int i, j;
  733. uint32_t *tmp;
  734. struct device_node *np = pdev->dev.of_node;
  735. struct device_node *child;
  736. if (!np)
  737. return -ENODEV;
  738. info->dev = &pdev->dev;
  739. oxnas_pinctrl_child_count(info, np);
  740. if (info->nbanks < 1) {
  741. dev_err(&pdev->dev, "you need to specify atleast one gpio-controller\n");
  742. return -EINVAL;
  743. }
  744. ret = oxnas_pinctrl_mux_mask(info, np);
  745. if (ret)
  746. return ret;
  747. dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
  748. dev_dbg(&pdev->dev, "mux-mask\n");
  749. tmp = info->mux_mask;
  750. for (i = 0; i < info->nbanks; i++)
  751. for (j = 0; j < info->nmux; j++, tmp++)
  752. dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
  753. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  754. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  755. info->functions = devm_kzalloc(&pdev->dev, info->nfunctions *
  756. sizeof(struct oxnas_pmx_func),
  757. GFP_KERNEL);
  758. if (!info->functions)
  759. return -ENOMEM;
  760. info->groups = devm_kzalloc(&pdev->dev, info->ngroups *
  761. sizeof(struct oxnas_pin_group),
  762. GFP_KERNEL);
  763. if (!info->groups)
  764. return -ENOMEM;
  765. dev_dbg(&pdev->dev, "nbanks = %d\n", info->nbanks);
  766. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  767. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  768. i = 0;
  769. for_each_child_of_node(np, child) {
  770. if (of_device_is_compatible(child, gpio_compat))
  771. continue;
  772. ret = oxnas_pinctrl_parse_functions(child, info, i++);
  773. if (ret) {
  774. dev_err(&pdev->dev, "failed to parse function\n");
  775. return ret;
  776. }
  777. }
  778. return 0;
  779. }
  780. static int oxnas_pinctrl_probe(struct platform_device *pdev)
  781. {
  782. struct oxnas_pinctrl *info;
  783. struct pinctrl_pin_desc *pdesc;
  784. int ret, i, j, k;
  785. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  786. if (!info)
  787. return -ENOMEM;
  788. ret = oxnas_pinctrl_probe_dt(pdev, info);
  789. if (ret)
  790. return ret;
  791. /*
  792. * We need all the GPIO drivers to probe FIRST, or we will not be able
  793. * to obtain references to the struct gpio_chip * for them, and we
  794. * need this to proceed.
  795. */
  796. for (i = 0; i < info->nbanks; i++) {
  797. if (!gpio_chips[i]) {
  798. dev_warn(&pdev->dev,
  799. "GPIO chip %d not registered yet\n", i);
  800. devm_kfree(&pdev->dev, info);
  801. return -EPROBE_DEFER;
  802. }
  803. }
  804. oxnas_pinctrl_desc.name = dev_name(&pdev->dev);
  805. oxnas_pinctrl_desc.npins = info->nbanks * MAX_NB_GPIO_PER_BANK;
  806. oxnas_pinctrl_desc.pins = pdesc =
  807. devm_kzalloc(&pdev->dev, sizeof(*pdesc) *
  808. oxnas_pinctrl_desc.npins, GFP_KERNEL);
  809. if (!oxnas_pinctrl_desc.pins)
  810. return -ENOMEM;
  811. for (i = 0 , k = 0; i < info->nbanks; i++) {
  812. for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
  813. pdesc->number = k;
  814. pdesc->name = kasprintf(GFP_KERNEL, "MF_%c%d", i + 'A',
  815. j);
  816. pdesc++;
  817. }
  818. }
  819. platform_set_drvdata(pdev, info);
  820. info->pctl = pinctrl_register(&oxnas_pinctrl_desc, &pdev->dev, info);
  821. if (!info->pctl) {
  822. dev_err(&pdev->dev, "could not register OX820 pinctrl driver\n");
  823. ret = -EINVAL;
  824. goto err;
  825. }
  826. /* We will handle a range of GPIO pins */
  827. for (i = 0; i < info->nbanks; i++)
  828. pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
  829. dev_info(&pdev->dev, "initialized OX820 pinctrl driver\n");
  830. return 0;
  831. err:
  832. return ret;
  833. }
  834. static int oxnas_pinctrl_remove(struct platform_device *pdev)
  835. {
  836. struct oxnas_pinctrl *info = platform_get_drvdata(pdev);
  837. pinctrl_unregister(info->pctl);
  838. return 0;
  839. }
  840. static int oxnas_gpio_request(struct gpio_chip *chip, unsigned offset)
  841. {
  842. /*
  843. * Map back to global GPIO space and request muxing, the direction
  844. * parameter does not matter for this controller.
  845. */
  846. int gpio = chip->base + offset;
  847. int bank = chip->base / chip->ngpio;
  848. dev_dbg(chip->dev, "%s:%d MF_%c%d(%d)\n", __func__, __LINE__,
  849. 'A' + bank, offset, gpio);
  850. return pinctrl_request_gpio(gpio);
  851. }
  852. static void oxnas_gpio_free(struct gpio_chip *chip, unsigned offset)
  853. {
  854. int gpio = chip->base + offset;
  855. pinctrl_free_gpio(gpio);
  856. }
  857. static int oxnas_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  858. {
  859. struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
  860. void __iomem *pio = oxnas_gpio->regbase;
  861. writel_relaxed(BIT(offset), pio + OUTPUT_EN_CLEAR);
  862. return 0;
  863. }
  864. static int oxnas_gpio_get(struct gpio_chip *chip, unsigned offset)
  865. {
  866. struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
  867. void __iomem *pio = oxnas_gpio->regbase;
  868. unsigned mask = 1 << offset;
  869. u32 pdsr;
  870. pdsr = readl_relaxed(pio + INPUT_VALUE);
  871. return (pdsr & mask) != 0;
  872. }
  873. static void oxnas_gpio_set(struct gpio_chip *chip, unsigned offset,
  874. int val)
  875. {
  876. struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
  877. void __iomem *pio = oxnas_gpio->regbase;
  878. if (val)
  879. writel_relaxed(BIT(offset), pio + OUTPUT_SET);
  880. else
  881. writel_relaxed(BIT(offset), pio + OUTPUT_CLEAR);
  882. }
  883. static int oxnas_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  884. int val)
  885. {
  886. struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
  887. void __iomem *pio = oxnas_gpio->regbase;
  888. if (val)
  889. writel_relaxed(BIT(offset), pio + OUTPUT_SET);
  890. else
  891. writel_relaxed(BIT(offset), pio + OUTPUT_CLEAR);
  892. writel_relaxed(BIT(offset), pio + OUTPUT_EN_SET);
  893. return 0;
  894. }
  895. static int oxnas_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  896. {
  897. struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
  898. int virq;
  899. if (offset < chip->ngpio)
  900. virq = irq_create_mapping(oxnas_gpio->domain, offset);
  901. else
  902. virq = -ENXIO;
  903. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  904. chip->label, offset + chip->base, virq);
  905. return virq;
  906. }
  907. #ifdef CONFIG_DEBUG_FS
  908. static void oxnas_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  909. {
  910. enum oxnas_mux mode;
  911. int i;
  912. struct oxnas_gpio_chip *oxnas_gpio = to_oxnas_gpio_chip(chip);
  913. void __iomem *pio = oxnas_gpio->regbase;
  914. void __iomem *cio = oxnas_gpio->ctrlbase;
  915. for (i = 0; i < chip->ngpio; i++) {
  916. unsigned pin = chip->base + i;
  917. unsigned mask = pin_to_mask(pin);
  918. const char *gpio_label;
  919. u32 pdsr;
  920. gpio_label = gpiochip_is_requested(chip, i);
  921. if (!gpio_label)
  922. continue;
  923. /* FIXME */
  924. mode = oxnas_mux_get_func(cio, mask);
  925. seq_printf(s, "[%s] GPIO%s%d: ",
  926. gpio_label, chip->label, i);
  927. if (mode == OXNAS_PINMUX_GPIO) {
  928. pdsr = readl_relaxed(pio + INPUT_VALUE);
  929. seq_printf(s, "[gpio] %s\n",
  930. pdsr & mask ?
  931. "set" : "clear");
  932. } else {
  933. seq_printf(s, "[periph %c]\n",
  934. mode + 'A' - 1);
  935. }
  936. }
  937. }
  938. #else
  939. #define oxnas_gpio_dbg_show NULL
  940. #endif
  941. /* Several AIC controller irqs are dispatched through this GPIO handler.
  942. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  943. * oxnas_set_gpio_input() then maybe enable its glitch filter.
  944. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  945. * handler.
  946. */
  947. static void gpio_irq_mask(struct irq_data *d)
  948. {
  949. struct oxnas_gpio_chip *oxnas_gpio = irq_data_get_irq_chip_data(d);
  950. void __iomem *pio = oxnas_gpio->regbase;
  951. unsigned mask = 1 << d->hwirq;
  952. unsigned type = irqd_get_trigger_type(d);
  953. unsigned long flags;
  954. if (!(type & IRQ_TYPE_EDGE_BOTH))
  955. return;
  956. spin_lock_irqsave(&oxnas_gpio->lock, flags);
  957. if (type & IRQ_TYPE_EDGE_RISING)
  958. oxnas_register_clear_mask(pio + RE_IRQ_ENABLE, mask);
  959. if (type & IRQ_TYPE_EDGE_FALLING)
  960. oxnas_register_clear_mask(pio + FE_IRQ_ENABLE, mask);
  961. spin_unlock_irqrestore(&oxnas_gpio->lock, flags);
  962. }
  963. static void gpio_irq_unmask(struct irq_data *d)
  964. {
  965. struct oxnas_gpio_chip *oxnas_gpio = irq_data_get_irq_chip_data(d);
  966. void __iomem *pio = oxnas_gpio->regbase;
  967. unsigned mask = 1 << d->hwirq;
  968. unsigned type = irqd_get_trigger_type(d);
  969. unsigned long flags;
  970. if (!(type & IRQ_TYPE_EDGE_BOTH))
  971. return;
  972. spin_lock_irqsave(&oxnas_gpio->lock, flags);
  973. if (type & IRQ_TYPE_EDGE_RISING)
  974. oxnas_register_set_mask(pio + RE_IRQ_ENABLE, mask);
  975. if (type & IRQ_TYPE_EDGE_FALLING)
  976. oxnas_register_set_mask(pio + FE_IRQ_ENABLE, mask);
  977. spin_unlock_irqrestore(&oxnas_gpio->lock, flags);
  978. }
  979. static int gpio_irq_type(struct irq_data *d, unsigned type)
  980. {
  981. if ((type & IRQ_TYPE_EDGE_BOTH) == 0) {
  982. pr_warn("OX820: Unsupported type for irq %d\n",
  983. gpio_to_irq(d->irq));
  984. return -EINVAL;
  985. }
  986. /* seems no way to set trigger type without enable irq, so leave it to unmask time */
  987. return 0;
  988. }
  989. static struct irq_chip gpio_irqchip = {
  990. .name = "GPIO",
  991. .irq_disable = gpio_irq_mask,
  992. .irq_mask = gpio_irq_mask,
  993. .irq_unmask = gpio_irq_unmask,
  994. .irq_set_type = gpio_irq_type,
  995. };
  996. static void gpio_irq_handler(struct irq_desc *desc)
  997. {
  998. struct irq_chip *chip = irq_desc_get_chip(desc);
  999. struct irq_data *idata = irq_desc_get_irq_data(desc);
  1000. struct oxnas_gpio_chip *oxnas_gpio = irq_data_get_irq_chip_data(idata);
  1001. void __iomem *pio = oxnas_gpio->regbase;
  1002. unsigned long isr;
  1003. int n;
  1004. chained_irq_enter(chip, desc);
  1005. for (;;) {
  1006. /* TODO: see if it works */
  1007. isr = readl_relaxed(pio + IRQ_PENDING);
  1008. if (!isr)
  1009. break;
  1010. /* acks pending interrupts */
  1011. writel_relaxed(isr, pio + IRQ_PENDING);
  1012. for_each_set_bit(n, &isr, BITS_PER_LONG) {
  1013. generic_handle_irq(irq_find_mapping(oxnas_gpio->domain,
  1014. n));
  1015. }
  1016. }
  1017. chained_irq_exit(chip, desc);
  1018. /* now it may re-trigger */
  1019. }
  1020. /*
  1021. * This lock class tells lockdep that GPIO irqs are in a different
  1022. * category than their parents, so it won't report false recursion.
  1023. */
  1024. static struct lock_class_key gpio_lock_class;
  1025. static int oxnas_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  1026. irq_hw_number_t hw)
  1027. {
  1028. struct oxnas_gpio_chip *oxnas_gpio = h->host_data;
  1029. irq_set_lockdep_class(virq, &gpio_lock_class);
  1030. irq_set_chip_and_handler(virq, &gpio_irqchip, handle_edge_irq);
  1031. irq_set_chip_data(virq, oxnas_gpio);
  1032. return 0;
  1033. }
  1034. static int oxnas_gpio_irq_domain_xlate(struct irq_domain *d,
  1035. struct device_node *ctrlr,
  1036. const u32 *intspec,
  1037. unsigned int intsize,
  1038. irq_hw_number_t *out_hwirq,
  1039. unsigned int *out_type)
  1040. {
  1041. struct oxnas_gpio_chip *oxnas_gpio = d->host_data;
  1042. int ret;
  1043. int pin = oxnas_gpio->chip.base + intspec[0];
  1044. if (WARN_ON(intsize < 2))
  1045. return -EINVAL;
  1046. *out_hwirq = intspec[0];
  1047. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  1048. ret = gpio_request(pin, ctrlr->full_name);
  1049. if (ret)
  1050. return ret;
  1051. ret = gpio_direction_input(pin);
  1052. if (ret)
  1053. return ret;
  1054. return 0;
  1055. }
  1056. static struct irq_domain_ops oxnas_gpio_ops = {
  1057. .map = oxnas_gpio_irq_map,
  1058. .xlate = oxnas_gpio_irq_domain_xlate,
  1059. };
  1060. static int oxnas_gpio_of_irq_setup(struct device_node *node,
  1061. struct oxnas_gpio_chip *oxnas_gpio,
  1062. unsigned int irq)
  1063. {
  1064. /* Disable irqs of this controller */
  1065. writel_relaxed(0, oxnas_gpio->regbase + RE_IRQ_ENABLE);
  1066. writel_relaxed(0, oxnas_gpio->regbase + FE_IRQ_ENABLE);
  1067. /* Setup irq domain */
  1068. oxnas_gpio->domain = irq_domain_add_linear(node, oxnas_gpio->chip.ngpio,
  1069. &oxnas_gpio_ops, oxnas_gpio);
  1070. if (!oxnas_gpio->domain)
  1071. panic("oxnas_gpio: couldn't allocate irq domain (DT).\n");
  1072. irq_set_chip_data(irq, oxnas_gpio);
  1073. irq_set_chained_handler(irq, gpio_irq_handler);
  1074. return 0;
  1075. }
  1076. /* This structure is replicated for each GPIO block allocated at probe time */
  1077. static struct gpio_chip oxnas_gpio_template = {
  1078. .request = oxnas_gpio_request,
  1079. .free = oxnas_gpio_free,
  1080. .direction_input = oxnas_gpio_direction_input,
  1081. .get = oxnas_gpio_get,
  1082. .direction_output = oxnas_gpio_direction_output,
  1083. .set = oxnas_gpio_set,
  1084. .to_irq = oxnas_gpio_to_irq,
  1085. .dbg_show = oxnas_gpio_dbg_show,
  1086. .can_sleep = 0,
  1087. .ngpio = MAX_NB_GPIO_PER_BANK,
  1088. };
  1089. static struct of_device_id oxnas_gpio_of_match[] = {
  1090. { .compatible = "plxtech,nas782x-gpio"},
  1091. { /* sentinel */ }
  1092. };
  1093. static int oxnas_gpio_probe(struct platform_device *pdev)
  1094. {
  1095. struct device_node *np = pdev->dev.of_node;
  1096. struct resource *res;
  1097. struct oxnas_gpio_chip *oxnas_chip = NULL;
  1098. struct gpio_chip *chip;
  1099. struct pinctrl_gpio_range *range;
  1100. int ret = 0;
  1101. int irq, i;
  1102. int alias_idx = of_alias_get_id(np, "gpio");
  1103. uint32_t ngpio;
  1104. char **names;
  1105. BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
  1106. if (gpio_chips[alias_idx]) {
  1107. ret = -EBUSY;
  1108. goto err;
  1109. }
  1110. irq = platform_get_irq(pdev, 0);
  1111. if (irq < 0) {
  1112. ret = irq;
  1113. goto err;
  1114. }
  1115. oxnas_chip = devm_kzalloc(&pdev->dev, sizeof(*oxnas_chip), GFP_KERNEL);
  1116. if (!oxnas_chip) {
  1117. ret = -ENOMEM;
  1118. goto err;
  1119. }
  1120. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1121. oxnas_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
  1122. if (IS_ERR(oxnas_chip->regbase)) {
  1123. ret = PTR_ERR(oxnas_chip->regbase);
  1124. goto err;
  1125. }
  1126. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1127. oxnas_chip->ctrlbase = devm_ioremap_resource(&pdev->dev, res);
  1128. if (IS_ERR(oxnas_chip->ctrlbase)) {
  1129. ret = PTR_ERR(oxnas_chip->ctrlbase);
  1130. goto err;
  1131. }
  1132. oxnas_chip->chip = oxnas_gpio_template;
  1133. spin_lock_init(&oxnas_chip->lock);
  1134. chip = &oxnas_chip->chip;
  1135. chip->of_node = np;
  1136. chip->label = dev_name(&pdev->dev);
  1137. chip->dev = &pdev->dev;
  1138. chip->owner = THIS_MODULE;
  1139. chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
  1140. if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
  1141. if (ngpio > MAX_NB_GPIO_PER_BANK)
  1142. pr_err("oxnas_gpio.%d, gpio-nb >= %d failback to %d\n",
  1143. alias_idx, MAX_NB_GPIO_PER_BANK,
  1144. MAX_NB_GPIO_PER_BANK);
  1145. else
  1146. chip->ngpio = ngpio;
  1147. }
  1148. names = devm_kzalloc(&pdev->dev, sizeof(char *) * chip->ngpio,
  1149. GFP_KERNEL);
  1150. if (!names) {
  1151. ret = -ENOMEM;
  1152. goto err;
  1153. }
  1154. for (i = 0; i < chip->ngpio; i++)
  1155. names[i] = kasprintf(GFP_KERNEL, "MF_%c%d", alias_idx + 'A', i);
  1156. chip->names = (const char *const *)names;
  1157. range = &oxnas_chip->range;
  1158. range->name = chip->label;
  1159. range->id = alias_idx;
  1160. range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
  1161. range->npins = chip->ngpio;
  1162. range->gc = chip;
  1163. ret = gpiochip_add(chip);
  1164. if (ret)
  1165. goto err;
  1166. gpio_chips[alias_idx] = oxnas_chip;
  1167. gpio_banks = max(gpio_banks, alias_idx + 1);
  1168. oxnas_gpio_of_irq_setup(np, oxnas_chip, irq);
  1169. dev_info(&pdev->dev, "at address %p\n", oxnas_chip->regbase);
  1170. return 0;
  1171. err:
  1172. dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
  1173. return ret;
  1174. }
  1175. static struct platform_driver oxnas_gpio_driver = {
  1176. .driver = {
  1177. .name = "gpio-oxnas",
  1178. .owner = THIS_MODULE,
  1179. .of_match_table = of_match_ptr(oxnas_gpio_of_match),
  1180. },
  1181. .probe = oxnas_gpio_probe,
  1182. };
  1183. static struct platform_driver oxnas_pinctrl_driver = {
  1184. .driver = {
  1185. .name = "pinctrl-oxnas",
  1186. .owner = THIS_MODULE,
  1187. .of_match_table = of_match_ptr(oxnas_pinctrl_of_match),
  1188. },
  1189. .probe = oxnas_pinctrl_probe,
  1190. .remove = oxnas_pinctrl_remove,
  1191. };
  1192. static int __init oxnas_pinctrl_init(void)
  1193. {
  1194. int ret;
  1195. ret = platform_driver_register(&oxnas_gpio_driver);
  1196. if (ret)
  1197. return ret;
  1198. return platform_driver_register(&oxnas_pinctrl_driver);
  1199. }
  1200. arch_initcall(oxnas_pinctrl_init);
  1201. static void __exit oxnas_pinctrl_exit(void)
  1202. {
  1203. platform_driver_unregister(&oxnas_pinctrl_driver);
  1204. }
  1205. module_exit(oxnas_pinctrl_exit);
  1206. MODULE_AUTHOR("Ma Hajun <mahaijuns@gmail.com>");
  1207. MODULE_DESCRIPTION("Plxtech Nas782x pinctrl driver");
  1208. MODULE_LICENSE("GPL v2");