pinctrl-oxnas.c 37 KB

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