pinctrl-oxnas.c 37 KB

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