pinctrl-at91.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. /*
  2. * at91 pinctrl driver based on at91 pinmux core
  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/io.h>
  21. #include <linux/gpio.h>
  22. #include <linux/pinctrl/machine.h>
  23. #include <linux/pinctrl/pinconf.h>
  24. #include <linux/pinctrl/pinctrl.h>
  25. #include <linux/pinctrl/pinmux.h>
  26. /* Since we request GPIOs from ourself */
  27. #include <linux/pinctrl/consumer.h>
  28. #include <asm/mach/irq.h>
  29. #include <mach/hardware.h>
  30. #include <mach/at91_pio.h>
  31. #include "core.h"
  32. #define MAX_NB_GPIO_PER_BANK 32
  33. struct at91_pinctrl_mux_ops;
  34. struct at91_gpio_chip {
  35. struct gpio_chip chip;
  36. struct pinctrl_gpio_range range;
  37. struct at91_gpio_chip *next; /* Bank sharing same clock */
  38. int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
  39. int pioc_virq; /* PIO bank Linux virtual interrupt */
  40. int pioc_idx; /* PIO bank index */
  41. void __iomem *regbase; /* PIO bank virtual address */
  42. struct clk *clock; /* associated clock */
  43. struct irq_domain *domain; /* associated irq domain */
  44. struct at91_pinctrl_mux_ops *ops; /* ops */
  45. };
  46. #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
  47. static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
  48. static int gpio_banks;
  49. #define PULL_UP (1 << 0)
  50. #define MULTI_DRIVE (1 << 1)
  51. #define DEGLITCH (1 << 2)
  52. #define PULL_DOWN (1 << 3)
  53. #define DIS_SCHMIT (1 << 4)
  54. #define DEBOUNCE (1 << 16)
  55. #define DEBOUNCE_VAL_SHIFT 17
  56. #define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
  57. /**
  58. * struct at91_pmx_func - describes AT91 pinmux functions
  59. * @name: the name of this specific function
  60. * @groups: corresponding pin groups
  61. * @ngroups: the number of groups
  62. */
  63. struct at91_pmx_func {
  64. const char *name;
  65. const char **groups;
  66. unsigned ngroups;
  67. };
  68. enum at91_mux {
  69. AT91_MUX_GPIO = 0,
  70. AT91_MUX_PERIPH_A = 1,
  71. AT91_MUX_PERIPH_B = 2,
  72. AT91_MUX_PERIPH_C = 3,
  73. AT91_MUX_PERIPH_D = 4,
  74. };
  75. /**
  76. * struct at91_pmx_pin - describes an At91 pin mux
  77. * @bank: the bank of the pin
  78. * @pin: the pin number in the @bank
  79. * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
  80. * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
  81. */
  82. struct at91_pmx_pin {
  83. uint32_t bank;
  84. uint32_t pin;
  85. enum at91_mux mux;
  86. unsigned long conf;
  87. };
  88. /**
  89. * struct at91_pin_group - describes an At91 pin group
  90. * @name: the name of this specific pin group
  91. * @pins_conf: the mux mode for each pin in this group. The size of this
  92. * array is the same as pins.
  93. * @pins: an array of discrete physical pins used in this group, taken
  94. * from the driver-local pin enumeration space
  95. * @npins: the number of pins in this group array, i.e. the number of
  96. * elements in .pins so we can iterate over that array
  97. */
  98. struct at91_pin_group {
  99. const char *name;
  100. struct at91_pmx_pin *pins_conf;
  101. unsigned int *pins;
  102. unsigned npins;
  103. };
  104. /**
  105. * struct at91_pinctrl_mux_ops - describes an At91 mux ops group
  106. * on new IP with support for periph C and D the way to mux in
  107. * periph A and B has changed
  108. * So provide the right call back
  109. * if not present means the IP does not support it
  110. * @get_periph: return the periph mode configured
  111. * @mux_A_periph: mux as periph A
  112. * @mux_B_periph: mux as periph B
  113. * @mux_C_periph: mux as periph C
  114. * @mux_D_periph: mux as periph D
  115. * @get_deglitch: get deglitch status
  116. * @set_deglitch: enable/disable deglitch
  117. * @get_debounce: get debounce status
  118. * @set_debounce: enable/disable debounce
  119. * @get_pulldown: get pulldown status
  120. * @set_pulldown: enable/disable pulldown
  121. * @get_schmitt_trig: get schmitt trigger status
  122. * @disable_schmitt_trig: disable schmitt trigger
  123. * @irq_type: return irq type
  124. */
  125. struct at91_pinctrl_mux_ops {
  126. enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
  127. void (*mux_A_periph)(void __iomem *pio, unsigned mask);
  128. void (*mux_B_periph)(void __iomem *pio, unsigned mask);
  129. void (*mux_C_periph)(void __iomem *pio, unsigned mask);
  130. void (*mux_D_periph)(void __iomem *pio, unsigned mask);
  131. bool (*get_deglitch)(void __iomem *pio, unsigned pin);
  132. void (*set_deglitch)(void __iomem *pio, unsigned mask, bool in_on);
  133. bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
  134. void (*set_debounce)(void __iomem *pio, unsigned mask, bool in_on, u32 div);
  135. bool (*get_pulldown)(void __iomem *pio, unsigned pin);
  136. void (*set_pulldown)(void __iomem *pio, unsigned mask, bool in_on);
  137. bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
  138. void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
  139. /* irq */
  140. int (*irq_type)(struct irq_data *d, unsigned type);
  141. };
  142. static int gpio_irq_type(struct irq_data *d, unsigned type);
  143. static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
  144. struct at91_pinctrl {
  145. struct device *dev;
  146. struct pinctrl_dev *pctl;
  147. int nbanks;
  148. uint32_t *mux_mask;
  149. int nmux;
  150. struct at91_pmx_func *functions;
  151. int nfunctions;
  152. struct at91_pin_group *groups;
  153. int ngroups;
  154. struct at91_pinctrl_mux_ops *ops;
  155. };
  156. static const inline struct at91_pin_group *at91_pinctrl_find_group_by_name(
  157. const struct at91_pinctrl *info,
  158. const char *name)
  159. {
  160. const struct at91_pin_group *grp = NULL;
  161. int i;
  162. for (i = 0; i < info->ngroups; i++) {
  163. if (strcmp(info->groups[i].name, name))
  164. continue;
  165. grp = &info->groups[i];
  166. dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
  167. break;
  168. }
  169. return grp;
  170. }
  171. static int at91_get_groups_count(struct pinctrl_dev *pctldev)
  172. {
  173. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  174. return info->ngroups;
  175. }
  176. static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
  177. unsigned selector)
  178. {
  179. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  180. return info->groups[selector].name;
  181. }
  182. static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  183. const unsigned **pins,
  184. unsigned *npins)
  185. {
  186. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  187. if (selector >= info->ngroups)
  188. return -EINVAL;
  189. *pins = info->groups[selector].pins;
  190. *npins = info->groups[selector].npins;
  191. return 0;
  192. }
  193. static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  194. unsigned offset)
  195. {
  196. seq_printf(s, "%s", dev_name(pctldev->dev));
  197. }
  198. static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
  199. struct device_node *np,
  200. struct pinctrl_map **map, unsigned *num_maps)
  201. {
  202. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  203. const struct at91_pin_group *grp;
  204. struct pinctrl_map *new_map;
  205. struct device_node *parent;
  206. int map_num = 1;
  207. int i;
  208. /*
  209. * first find the group of this node and check if we need create
  210. * config maps for pins
  211. */
  212. grp = at91_pinctrl_find_group_by_name(info, np->name);
  213. if (!grp) {
  214. dev_err(info->dev, "unable to find group for node %s\n",
  215. np->name);
  216. return -EINVAL;
  217. }
  218. map_num += grp->npins;
  219. new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, GFP_KERNEL);
  220. if (!new_map)
  221. return -ENOMEM;
  222. *map = new_map;
  223. *num_maps = map_num;
  224. /* create mux map */
  225. parent = of_get_parent(np);
  226. if (!parent) {
  227. devm_kfree(pctldev->dev, new_map);
  228. return -EINVAL;
  229. }
  230. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  231. new_map[0].data.mux.function = parent->name;
  232. new_map[0].data.mux.group = np->name;
  233. of_node_put(parent);
  234. /* create config map */
  235. new_map++;
  236. for (i = 0; i < grp->npins; i++) {
  237. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  238. new_map[i].data.configs.group_or_pin =
  239. pin_get_name(pctldev, grp->pins[i]);
  240. new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
  241. new_map[i].data.configs.num_configs = 1;
  242. }
  243. dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
  244. (*map)->data.mux.function, (*map)->data.mux.group, map_num);
  245. return 0;
  246. }
  247. static void at91_dt_free_map(struct pinctrl_dev *pctldev,
  248. struct pinctrl_map *map, unsigned num_maps)
  249. {
  250. }
  251. static struct pinctrl_ops at91_pctrl_ops = {
  252. .get_groups_count = at91_get_groups_count,
  253. .get_group_name = at91_get_group_name,
  254. .get_group_pins = at91_get_group_pins,
  255. .pin_dbg_show = at91_pin_dbg_show,
  256. .dt_node_to_map = at91_dt_node_to_map,
  257. .dt_free_map = at91_dt_free_map,
  258. };
  259. static void __iomem * pin_to_controller(struct at91_pinctrl *info,
  260. unsigned int bank)
  261. {
  262. return gpio_chips[bank]->regbase;
  263. }
  264. static inline int pin_to_bank(unsigned pin)
  265. {
  266. return pin /= MAX_NB_GPIO_PER_BANK;
  267. }
  268. static unsigned pin_to_mask(unsigned int pin)
  269. {
  270. return 1 << pin;
  271. }
  272. static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
  273. {
  274. writel_relaxed(mask, pio + PIO_IDR);
  275. }
  276. static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
  277. {
  278. return (readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1;
  279. }
  280. static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
  281. {
  282. writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
  283. }
  284. static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
  285. {
  286. return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
  287. }
  288. static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
  289. {
  290. writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
  291. }
  292. static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
  293. {
  294. writel_relaxed(mask, pio + PIO_ASR);
  295. }
  296. static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
  297. {
  298. writel_relaxed(mask, pio + PIO_BSR);
  299. }
  300. static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
  301. {
  302. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
  303. pio + PIO_ABCDSR1);
  304. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
  305. pio + PIO_ABCDSR2);
  306. }
  307. static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
  308. {
  309. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
  310. pio + PIO_ABCDSR1);
  311. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
  312. pio + PIO_ABCDSR2);
  313. }
  314. static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
  315. {
  316. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
  317. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  318. }
  319. static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
  320. {
  321. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
  322. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  323. }
  324. static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
  325. {
  326. unsigned select;
  327. if (readl_relaxed(pio + PIO_PSR) & mask)
  328. return AT91_MUX_GPIO;
  329. select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
  330. select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
  331. return select + 1;
  332. }
  333. static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
  334. {
  335. unsigned select;
  336. if (readl_relaxed(pio + PIO_PSR) & mask)
  337. return AT91_MUX_GPIO;
  338. select = readl_relaxed(pio + PIO_ABSR) & mask;
  339. return select + 1;
  340. }
  341. static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin)
  342. {
  343. return (__raw_readl(pio + PIO_IFSR) >> pin) & 0x1;
  344. }
  345. static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
  346. {
  347. __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
  348. }
  349. static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
  350. {
  351. if (is_on)
  352. __raw_writel(mask, pio + PIO_IFSCDR);
  353. at91_mux_set_deglitch(pio, mask, is_on);
  354. }
  355. static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
  356. {
  357. *div = __raw_readl(pio + PIO_SCDR);
  358. return (__raw_readl(pio + PIO_IFSCSR) >> pin) & 0x1;
  359. }
  360. static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask,
  361. bool is_on, u32 div)
  362. {
  363. if (is_on) {
  364. __raw_writel(mask, pio + PIO_IFSCER);
  365. __raw_writel(div & PIO_SCDR_DIV, pio + PIO_SCDR);
  366. __raw_writel(mask, pio + PIO_IFER);
  367. } else {
  368. __raw_writel(mask, pio + PIO_IFDR);
  369. }
  370. }
  371. static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
  372. {
  373. return (__raw_readl(pio + PIO_PPDSR) >> pin) & 0x1;
  374. }
  375. static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on)
  376. {
  377. __raw_writel(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
  378. }
  379. static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask)
  380. {
  381. __raw_writel(__raw_readl(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
  382. }
  383. static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin)
  384. {
  385. return (__raw_readl(pio + PIO_SCHMITT) >> pin) & 0x1;
  386. }
  387. static struct at91_pinctrl_mux_ops at91rm9200_ops = {
  388. .get_periph = at91_mux_get_periph,
  389. .mux_A_periph = at91_mux_set_A_periph,
  390. .mux_B_periph = at91_mux_set_B_periph,
  391. .get_deglitch = at91_mux_get_deglitch,
  392. .set_deglitch = at91_mux_set_deglitch,
  393. .irq_type = gpio_irq_type,
  394. };
  395. static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
  396. .get_periph = at91_mux_pio3_get_periph,
  397. .mux_A_periph = at91_mux_pio3_set_A_periph,
  398. .mux_B_periph = at91_mux_pio3_set_B_periph,
  399. .mux_C_periph = at91_mux_pio3_set_C_periph,
  400. .mux_D_periph = at91_mux_pio3_set_D_periph,
  401. .get_deglitch = at91_mux_get_deglitch,
  402. .set_deglitch = at91_mux_pio3_set_deglitch,
  403. .get_debounce = at91_mux_pio3_get_debounce,
  404. .set_debounce = at91_mux_pio3_set_debounce,
  405. .get_pulldown = at91_mux_pio3_get_pulldown,
  406. .set_pulldown = at91_mux_pio3_set_pulldown,
  407. .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
  408. .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
  409. .irq_type = alt_gpio_irq_type,
  410. };
  411. static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
  412. {
  413. if (pin->mux) {
  414. dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lu\n",
  415. pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
  416. } else {
  417. dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lu\n",
  418. pin->bank + 'A', pin->pin, pin->conf);
  419. }
  420. }
  421. static int pin_check_config(struct at91_pinctrl *info, const char* name,
  422. int index, const struct at91_pmx_pin *pin)
  423. {
  424. int mux;
  425. /* check if it's a valid config */
  426. if (pin->bank >= info->nbanks) {
  427. dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
  428. name, index, pin->bank, info->nbanks);
  429. return -EINVAL;
  430. }
  431. if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
  432. dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
  433. name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
  434. return -EINVAL;
  435. }
  436. if (!pin->mux)
  437. return 0;
  438. mux = pin->mux - 1;
  439. if (mux >= info->nmux) {
  440. dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
  441. name, index, mux, info->nmux);
  442. return -EINVAL;
  443. }
  444. if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
  445. dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
  446. name, index, mux, pin->bank + 'A', pin->pin);
  447. return -EINVAL;
  448. }
  449. return 0;
  450. }
  451. static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
  452. {
  453. writel_relaxed(mask, pio + PIO_PDR);
  454. }
  455. static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
  456. {
  457. writel_relaxed(mask, pio + PIO_PER);
  458. writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
  459. }
  460. static int at91_pmx_enable(struct pinctrl_dev *pctldev, unsigned selector,
  461. unsigned group)
  462. {
  463. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  464. const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
  465. const struct at91_pmx_pin *pin;
  466. uint32_t npins = info->groups[group].npins;
  467. int i, ret;
  468. unsigned mask;
  469. void __iomem *pio;
  470. dev_dbg(info->dev, "enable function %s group %s\n",
  471. info->functions[selector].name, info->groups[group].name);
  472. /* first check that all the pins of the group are valid with a valid
  473. * paramter */
  474. for (i = 0; i < npins; i++) {
  475. pin = &pins_conf[i];
  476. ret = pin_check_config(info, info->groups[group].name, i, pin);
  477. if (ret)
  478. return ret;
  479. }
  480. for (i = 0; i < npins; i++) {
  481. pin = &pins_conf[i];
  482. at91_pin_dbg(info->dev, pin);
  483. pio = pin_to_controller(info, pin->bank);
  484. mask = pin_to_mask(pin->pin);
  485. at91_mux_disable_interrupt(pio, mask);
  486. switch(pin->mux) {
  487. case AT91_MUX_GPIO:
  488. at91_mux_gpio_enable(pio, mask, 1);
  489. break;
  490. case AT91_MUX_PERIPH_A:
  491. info->ops->mux_A_periph(pio, mask);
  492. break;
  493. case AT91_MUX_PERIPH_B:
  494. info->ops->mux_B_periph(pio, mask);
  495. break;
  496. case AT91_MUX_PERIPH_C:
  497. if (!info->ops->mux_C_periph)
  498. return -EINVAL;
  499. info->ops->mux_C_periph(pio, mask);
  500. break;
  501. case AT91_MUX_PERIPH_D:
  502. if (!info->ops->mux_D_periph)
  503. return -EINVAL;
  504. info->ops->mux_D_periph(pio, mask);
  505. break;
  506. }
  507. if (pin->mux)
  508. at91_mux_gpio_disable(pio, mask);
  509. }
  510. return 0;
  511. }
  512. static void at91_pmx_disable(struct pinctrl_dev *pctldev, unsigned selector,
  513. unsigned group)
  514. {
  515. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  516. const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
  517. const struct at91_pmx_pin *pin;
  518. uint32_t npins = info->groups[group].npins;
  519. int i;
  520. unsigned mask;
  521. void __iomem *pio;
  522. for (i = 0; i < npins; i++) {
  523. pin = &pins_conf[i];
  524. at91_pin_dbg(info->dev, pin);
  525. pio = pin_to_controller(info, pin->bank);
  526. mask = pin_to_mask(pin->pin);
  527. at91_mux_gpio_enable(pio, mask, 1);
  528. }
  529. }
  530. static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  531. {
  532. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  533. return info->nfunctions;
  534. }
  535. static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
  536. unsigned selector)
  537. {
  538. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  539. return info->functions[selector].name;
  540. }
  541. static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  542. const char * const **groups,
  543. unsigned * const num_groups)
  544. {
  545. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  546. *groups = info->functions[selector].groups;
  547. *num_groups = info->functions[selector].ngroups;
  548. return 0;
  549. }
  550. static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
  551. struct pinctrl_gpio_range *range,
  552. unsigned offset)
  553. {
  554. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  555. struct at91_gpio_chip *at91_chip;
  556. struct gpio_chip *chip;
  557. unsigned mask;
  558. if (!range) {
  559. dev_err(npct->dev, "invalid range\n");
  560. return -EINVAL;
  561. }
  562. if (!range->gc) {
  563. dev_err(npct->dev, "missing GPIO chip in range\n");
  564. return -EINVAL;
  565. }
  566. chip = range->gc;
  567. at91_chip = container_of(chip, struct at91_gpio_chip, chip);
  568. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  569. mask = 1 << (offset - chip->base);
  570. dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
  571. offset, 'A' + range->id, offset - chip->base, mask);
  572. writel_relaxed(mask, at91_chip->regbase + PIO_PER);
  573. return 0;
  574. }
  575. static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
  576. struct pinctrl_gpio_range *range,
  577. unsigned offset)
  578. {
  579. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  580. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  581. /* Set the pin to some default state, GPIO is usually default */
  582. }
  583. static struct pinmux_ops at91_pmx_ops = {
  584. .get_functions_count = at91_pmx_get_funcs_count,
  585. .get_function_name = at91_pmx_get_func_name,
  586. .get_function_groups = at91_pmx_get_groups,
  587. .enable = at91_pmx_enable,
  588. .disable = at91_pmx_disable,
  589. .gpio_request_enable = at91_gpio_request_enable,
  590. .gpio_disable_free = at91_gpio_disable_free,
  591. };
  592. static int at91_pinconf_get(struct pinctrl_dev *pctldev,
  593. unsigned pin_id, unsigned long *config)
  594. {
  595. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  596. void __iomem *pio;
  597. unsigned pin;
  598. int div;
  599. dev_dbg(info->dev, "%s:%d, pin_id=%d, config=0x%lx", __func__, __LINE__, pin_id, *config);
  600. pio = pin_to_controller(info, pin_to_bank(pin_id));
  601. pin = pin_id % MAX_NB_GPIO_PER_BANK;
  602. if (at91_mux_get_multidrive(pio, pin))
  603. *config |= MULTI_DRIVE;
  604. if (at91_mux_get_pullup(pio, pin))
  605. *config |= PULL_UP;
  606. if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
  607. *config |= DEGLITCH;
  608. if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
  609. *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
  610. if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
  611. *config |= PULL_DOWN;
  612. if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
  613. *config |= DIS_SCHMIT;
  614. return 0;
  615. }
  616. static int at91_pinconf_set(struct pinctrl_dev *pctldev,
  617. unsigned pin_id, unsigned long config)
  618. {
  619. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  620. unsigned mask;
  621. void __iomem *pio;
  622. dev_dbg(info->dev, "%s:%d, pin_id=%d, config=0x%lx", __func__, __LINE__, pin_id, config);
  623. pio = pin_to_controller(info, pin_to_bank(pin_id));
  624. mask = pin_to_mask(pin_id % MAX_NB_GPIO_PER_BANK);
  625. if (config & PULL_UP && config & PULL_DOWN)
  626. return -EINVAL;
  627. at91_mux_set_pullup(pio, mask, config & PULL_UP);
  628. at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
  629. if (info->ops->set_deglitch)
  630. info->ops->set_deglitch(pio, mask, config & DEGLITCH);
  631. if (info->ops->set_debounce)
  632. info->ops->set_debounce(pio, mask, config & DEBOUNCE,
  633. (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
  634. if (info->ops->set_pulldown)
  635. info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
  636. if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
  637. info->ops->disable_schmitt_trig(pio, mask);
  638. return 0;
  639. }
  640. static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  641. struct seq_file *s, unsigned pin_id)
  642. {
  643. }
  644. static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  645. struct seq_file *s, unsigned group)
  646. {
  647. }
  648. static struct pinconf_ops at91_pinconf_ops = {
  649. .pin_config_get = at91_pinconf_get,
  650. .pin_config_set = at91_pinconf_set,
  651. .pin_config_dbg_show = at91_pinconf_dbg_show,
  652. .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
  653. };
  654. static struct pinctrl_desc at91_pinctrl_desc = {
  655. .pctlops = &at91_pctrl_ops,
  656. .pmxops = &at91_pmx_ops,
  657. .confops = &at91_pinconf_ops,
  658. .owner = THIS_MODULE,
  659. };
  660. static const char *gpio_compat = "atmel,at91rm9200-gpio";
  661. static void at91_pinctrl_child_count(struct at91_pinctrl *info,
  662. struct device_node *np)
  663. {
  664. struct device_node *child;
  665. for_each_child_of_node(np, child) {
  666. if (of_device_is_compatible(child, gpio_compat)) {
  667. info->nbanks++;
  668. } else {
  669. info->nfunctions++;
  670. info->ngroups += of_get_child_count(child);
  671. }
  672. }
  673. }
  674. static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
  675. struct device_node *np)
  676. {
  677. int ret = 0;
  678. int size;
  679. const const __be32 *list;
  680. list = of_get_property(np, "atmel,mux-mask", &size);
  681. if (!list) {
  682. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  683. return -EINVAL;
  684. }
  685. size /= sizeof(*list);
  686. if (!size || size % info->nbanks) {
  687. dev_err(info->dev, "wrong mux mask array should be by %d\n", info->nbanks);
  688. return -EINVAL;
  689. }
  690. info->nmux = size / info->nbanks;
  691. info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
  692. if (!info->mux_mask) {
  693. dev_err(info->dev, "could not alloc mux_mask\n");
  694. return -ENOMEM;
  695. }
  696. ret = of_property_read_u32_array(np, "atmel,mux-mask",
  697. info->mux_mask, size);
  698. if (ret)
  699. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  700. return ret;
  701. }
  702. static int at91_pinctrl_parse_groups(struct device_node *np,
  703. struct at91_pin_group *grp,
  704. struct at91_pinctrl *info, u32 index)
  705. {
  706. struct at91_pmx_pin *pin;
  707. int size;
  708. const const __be32 *list;
  709. int i, j;
  710. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  711. /* Initialise group */
  712. grp->name = np->name;
  713. /*
  714. * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
  715. * do sanity check and calculate pins number
  716. */
  717. list = of_get_property(np, "atmel,pins", &size);
  718. /* we do not check return since it's safe node passed down */
  719. size /= sizeof(*list);
  720. if (!size || size % 4) {
  721. dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
  722. return -EINVAL;
  723. }
  724. grp->npins = size / 4;
  725. pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct at91_pmx_pin),
  726. GFP_KERNEL);
  727. grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
  728. GFP_KERNEL);
  729. if (!grp->pins_conf || !grp->pins)
  730. return -ENOMEM;
  731. for (i = 0, j = 0; i < size; i += 4, j++) {
  732. pin->bank = be32_to_cpu(*list++);
  733. pin->pin = be32_to_cpu(*list++);
  734. grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
  735. pin->mux = be32_to_cpu(*list++);
  736. pin->conf = be32_to_cpu(*list++);
  737. at91_pin_dbg(info->dev, pin);
  738. pin++;
  739. }
  740. return 0;
  741. }
  742. static int at91_pinctrl_parse_functions(struct device_node *np,
  743. struct at91_pinctrl *info, u32 index)
  744. {
  745. struct device_node *child;
  746. struct at91_pmx_func *func;
  747. struct at91_pin_group *grp;
  748. int ret;
  749. static u32 grp_index;
  750. u32 i = 0;
  751. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  752. func = &info->functions[index];
  753. /* Initialise function */
  754. func->name = np->name;
  755. func->ngroups = of_get_child_count(np);
  756. if (func->ngroups <= 0) {
  757. dev_err(info->dev, "no groups defined\n");
  758. return -EINVAL;
  759. }
  760. func->groups = devm_kzalloc(info->dev,
  761. func->ngroups * sizeof(char *), GFP_KERNEL);
  762. if (!func->groups)
  763. return -ENOMEM;
  764. for_each_child_of_node(np, child) {
  765. func->groups[i] = child->name;
  766. grp = &info->groups[grp_index++];
  767. ret = at91_pinctrl_parse_groups(child, grp, info, i++);
  768. if (ret)
  769. return ret;
  770. }
  771. return 0;
  772. }
  773. static struct of_device_id at91_pinctrl_of_match[] = {
  774. { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
  775. { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
  776. { /* sentinel */ }
  777. };
  778. static int at91_pinctrl_probe_dt(struct platform_device *pdev,
  779. struct at91_pinctrl *info)
  780. {
  781. int ret = 0;
  782. int i, j;
  783. uint32_t *tmp;
  784. struct device_node *np = pdev->dev.of_node;
  785. struct device_node *child;
  786. if (!np)
  787. return -ENODEV;
  788. info->dev = &pdev->dev;
  789. info->ops = (struct at91_pinctrl_mux_ops*)
  790. of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
  791. at91_pinctrl_child_count(info, np);
  792. if (info->nbanks < 1) {
  793. dev_err(&pdev->dev, "you need to specify atleast one gpio-controller\n");
  794. return -EINVAL;
  795. }
  796. ret = at91_pinctrl_mux_mask(info, np);
  797. if (ret)
  798. return ret;
  799. dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
  800. dev_dbg(&pdev->dev, "mux-mask\n");
  801. tmp = info->mux_mask;
  802. for (i = 0; i < info->nbanks; i++) {
  803. for (j = 0; j < info->nmux; j++, tmp++) {
  804. dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
  805. }
  806. }
  807. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  808. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  809. info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct at91_pmx_func),
  810. GFP_KERNEL);
  811. if (!info->functions)
  812. return -ENOMEM;
  813. info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct at91_pin_group),
  814. GFP_KERNEL);
  815. if (!info->groups)
  816. return -ENOMEM;
  817. dev_dbg(&pdev->dev, "nbanks = %d\n", info->nbanks);
  818. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  819. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  820. i = 0;
  821. for_each_child_of_node(np, child) {
  822. if (of_device_is_compatible(child, gpio_compat))
  823. continue;
  824. ret = at91_pinctrl_parse_functions(child, info, i++);
  825. if (ret) {
  826. dev_err(&pdev->dev, "failed to parse function\n");
  827. return ret;
  828. }
  829. }
  830. return 0;
  831. }
  832. static int at91_pinctrl_probe(struct platform_device *pdev)
  833. {
  834. struct at91_pinctrl *info;
  835. struct pinctrl_pin_desc *pdesc;
  836. int ret, i, j ,k;
  837. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  838. if (!info)
  839. return -ENOMEM;
  840. ret = at91_pinctrl_probe_dt(pdev, info);
  841. if (ret)
  842. return ret;
  843. /*
  844. * We need all the GPIO drivers to probe FIRST, or we will not be able
  845. * to obtain references to the struct gpio_chip * for them, and we
  846. * need this to proceed.
  847. */
  848. for (i = 0; i < info->nbanks; i++) {
  849. if (!gpio_chips[i]) {
  850. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  851. devm_kfree(&pdev->dev, info);
  852. return -EPROBE_DEFER;
  853. }
  854. }
  855. at91_pinctrl_desc.name = dev_name(&pdev->dev);
  856. at91_pinctrl_desc.npins = info->nbanks * MAX_NB_GPIO_PER_BANK;
  857. at91_pinctrl_desc.pins = pdesc =
  858. devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL);
  859. if (!at91_pinctrl_desc.pins)
  860. return -ENOMEM;
  861. for (i = 0 , k = 0; i < info->nbanks; i++) {
  862. for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
  863. pdesc->number = k;
  864. pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
  865. pdesc++;
  866. }
  867. }
  868. platform_set_drvdata(pdev, info);
  869. info->pctl = pinctrl_register(&at91_pinctrl_desc, &pdev->dev, info);
  870. if (!info->pctl) {
  871. dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
  872. ret = -EINVAL;
  873. goto err;
  874. }
  875. /* We will handle a range of GPIO pins */
  876. for (i = 0; i < info->nbanks; i++)
  877. pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
  878. dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
  879. return 0;
  880. err:
  881. return ret;
  882. }
  883. static int at91_pinctrl_remove(struct platform_device *pdev)
  884. {
  885. struct at91_pinctrl *info = platform_get_drvdata(pdev);
  886. pinctrl_unregister(info->pctl);
  887. return 0;
  888. }
  889. static int at91_gpio_request(struct gpio_chip *chip, unsigned offset)
  890. {
  891. /*
  892. * Map back to global GPIO space and request muxing, the direction
  893. * parameter does not matter for this controller.
  894. */
  895. int gpio = chip->base + offset;
  896. int bank = chip->base / chip->ngpio;
  897. dev_dbg(chip->dev, "%s:%d pio%c%d(%d)\n", __func__, __LINE__,
  898. 'A' + bank, offset, gpio);
  899. return pinctrl_request_gpio(gpio);
  900. }
  901. static void at91_gpio_free(struct gpio_chip *chip, unsigned offset)
  902. {
  903. int gpio = chip->base + offset;
  904. pinctrl_free_gpio(gpio);
  905. }
  906. static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  907. {
  908. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  909. void __iomem *pio = at91_gpio->regbase;
  910. unsigned mask = 1 << offset;
  911. writel_relaxed(mask, pio + PIO_ODR);
  912. return 0;
  913. }
  914. static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
  915. {
  916. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  917. void __iomem *pio = at91_gpio->regbase;
  918. unsigned mask = 1 << offset;
  919. u32 pdsr;
  920. pdsr = readl_relaxed(pio + PIO_PDSR);
  921. return (pdsr & mask) != 0;
  922. }
  923. static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
  924. int val)
  925. {
  926. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  927. void __iomem *pio = at91_gpio->regbase;
  928. unsigned mask = 1 << offset;
  929. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  930. }
  931. static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  932. int val)
  933. {
  934. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  935. void __iomem *pio = at91_gpio->regbase;
  936. unsigned mask = 1 << offset;
  937. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  938. writel_relaxed(mask, pio + PIO_OER);
  939. return 0;
  940. }
  941. static int at91_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  942. {
  943. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  944. int virq;
  945. if (offset < chip->ngpio)
  946. virq = irq_create_mapping(at91_gpio->domain, offset);
  947. else
  948. virq = -ENXIO;
  949. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  950. chip->label, offset + chip->base, virq);
  951. return virq;
  952. }
  953. #ifdef CONFIG_DEBUG_FS
  954. static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  955. {
  956. enum at91_mux mode;
  957. int i;
  958. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  959. void __iomem *pio = at91_gpio->regbase;
  960. for (i = 0; i < chip->ngpio; i++) {
  961. unsigned pin = chip->base + i;
  962. unsigned mask = pin_to_mask(pin);
  963. const char *gpio_label;
  964. u32 pdsr;
  965. gpio_label = gpiochip_is_requested(chip, i);
  966. if (!gpio_label)
  967. continue;
  968. mode = at91_gpio->ops->get_periph(pio, mask);
  969. seq_printf(s, "[%s] GPIO%s%d: ",
  970. gpio_label, chip->label, i);
  971. if (mode == AT91_MUX_GPIO) {
  972. pdsr = readl_relaxed(pio + PIO_PDSR);
  973. seq_printf(s, "[gpio] %s\n",
  974. pdsr & mask ?
  975. "set" : "clear");
  976. } else {
  977. seq_printf(s, "[periph %c]\n",
  978. mode + 'A' - 1);
  979. }
  980. }
  981. }
  982. #else
  983. #define at91_gpio_dbg_show NULL
  984. #endif
  985. /* Several AIC controller irqs are dispatched through this GPIO handler.
  986. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  987. * at91_set_gpio_input() then maybe enable its glitch filter.
  988. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  989. * handler.
  990. * First implementation always triggers on rising and falling edges
  991. * whereas the newer PIO3 can be additionally configured to trigger on
  992. * level, edge with any polarity.
  993. *
  994. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  995. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  996. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  997. */
  998. static void gpio_irq_mask(struct irq_data *d)
  999. {
  1000. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1001. void __iomem *pio = at91_gpio->regbase;
  1002. unsigned mask = 1 << d->hwirq;
  1003. if (pio)
  1004. writel_relaxed(mask, pio + PIO_IDR);
  1005. }
  1006. static void gpio_irq_unmask(struct irq_data *d)
  1007. {
  1008. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1009. void __iomem *pio = at91_gpio->regbase;
  1010. unsigned mask = 1 << d->hwirq;
  1011. if (pio)
  1012. writel_relaxed(mask, pio + PIO_IER);
  1013. }
  1014. static int gpio_irq_type(struct irq_data *d, unsigned type)
  1015. {
  1016. switch (type) {
  1017. case IRQ_TYPE_NONE:
  1018. case IRQ_TYPE_EDGE_BOTH:
  1019. return 0;
  1020. default:
  1021. return -EINVAL;
  1022. }
  1023. }
  1024. /* Alternate irq type for PIO3 support */
  1025. static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
  1026. {
  1027. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1028. void __iomem *pio = at91_gpio->regbase;
  1029. unsigned mask = 1 << d->hwirq;
  1030. switch (type) {
  1031. case IRQ_TYPE_EDGE_RISING:
  1032. writel_relaxed(mask, pio + PIO_ESR);
  1033. writel_relaxed(mask, pio + PIO_REHLSR);
  1034. break;
  1035. case IRQ_TYPE_EDGE_FALLING:
  1036. writel_relaxed(mask, pio + PIO_ESR);
  1037. writel_relaxed(mask, pio + PIO_FELLSR);
  1038. break;
  1039. case IRQ_TYPE_LEVEL_LOW:
  1040. writel_relaxed(mask, pio + PIO_LSR);
  1041. writel_relaxed(mask, pio + PIO_FELLSR);
  1042. break;
  1043. case IRQ_TYPE_LEVEL_HIGH:
  1044. writel_relaxed(mask, pio + PIO_LSR);
  1045. writel_relaxed(mask, pio + PIO_REHLSR);
  1046. break;
  1047. case IRQ_TYPE_EDGE_BOTH:
  1048. /*
  1049. * disable additional interrupt modes:
  1050. * fall back to default behavior
  1051. */
  1052. writel_relaxed(mask, pio + PIO_AIMDR);
  1053. return 0;
  1054. case IRQ_TYPE_NONE:
  1055. default:
  1056. pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
  1057. return -EINVAL;
  1058. }
  1059. /* enable additional interrupt modes */
  1060. writel_relaxed(mask, pio + PIO_AIMER);
  1061. return 0;
  1062. }
  1063. #ifdef CONFIG_PM
  1064. static u32 wakeups[MAX_GPIO_BANKS];
  1065. static u32 backups[MAX_GPIO_BANKS];
  1066. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  1067. {
  1068. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1069. unsigned bank = at91_gpio->pioc_idx;
  1070. unsigned mask = 1 << d->hwirq;
  1071. if (unlikely(bank >= MAX_GPIO_BANKS))
  1072. return -EINVAL;
  1073. if (state)
  1074. wakeups[bank] |= mask;
  1075. else
  1076. wakeups[bank] &= ~mask;
  1077. irq_set_irq_wake(at91_gpio->pioc_virq, state);
  1078. return 0;
  1079. }
  1080. void at91_pinctrl_gpio_suspend(void)
  1081. {
  1082. int i;
  1083. for (i = 0; i < gpio_banks; i++) {
  1084. void __iomem *pio;
  1085. if (!gpio_chips[i])
  1086. continue;
  1087. pio = gpio_chips[i]->regbase;
  1088. backups[i] = __raw_readl(pio + PIO_IMR);
  1089. __raw_writel(backups[i], pio + PIO_IDR);
  1090. __raw_writel(wakeups[i], pio + PIO_IER);
  1091. if (!wakeups[i]) {
  1092. clk_unprepare(gpio_chips[i]->clock);
  1093. clk_disable(gpio_chips[i]->clock);
  1094. } else {
  1095. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
  1096. 'A'+i, wakeups[i]);
  1097. }
  1098. }
  1099. }
  1100. void at91_pinctrl_gpio_resume(void)
  1101. {
  1102. int i;
  1103. for (i = 0; i < gpio_banks; i++) {
  1104. void __iomem *pio;
  1105. if (!gpio_chips[i])
  1106. continue;
  1107. pio = gpio_chips[i]->regbase;
  1108. if (!wakeups[i]) {
  1109. if (clk_prepare(gpio_chips[i]->clock) == 0)
  1110. clk_enable(gpio_chips[i]->clock);
  1111. }
  1112. __raw_writel(wakeups[i], pio + PIO_IDR);
  1113. __raw_writel(backups[i], pio + PIO_IER);
  1114. }
  1115. }
  1116. #else
  1117. #define gpio_irq_set_wake NULL
  1118. #endif /* CONFIG_PM */
  1119. static struct irq_chip gpio_irqchip = {
  1120. .name = "GPIO",
  1121. .irq_disable = gpio_irq_mask,
  1122. .irq_mask = gpio_irq_mask,
  1123. .irq_unmask = gpio_irq_unmask,
  1124. /* .irq_set_type is set dynamically */
  1125. .irq_set_wake = gpio_irq_set_wake,
  1126. };
  1127. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  1128. {
  1129. struct irq_chip *chip = irq_desc_get_chip(desc);
  1130. struct irq_data *idata = irq_desc_get_irq_data(desc);
  1131. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
  1132. void __iomem *pio = at91_gpio->regbase;
  1133. unsigned long isr;
  1134. int n;
  1135. chained_irq_enter(chip, desc);
  1136. for (;;) {
  1137. /* Reading ISR acks pending (edge triggered) GPIO interrupts.
  1138. * When there none are pending, we're finished unless we need
  1139. * to process multiple banks (like ID_PIOCDE on sam9263).
  1140. */
  1141. isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
  1142. if (!isr) {
  1143. if (!at91_gpio->next)
  1144. break;
  1145. at91_gpio = at91_gpio->next;
  1146. pio = at91_gpio->regbase;
  1147. continue;
  1148. }
  1149. for_each_set_bit(n, &isr, BITS_PER_LONG) {
  1150. generic_handle_irq(irq_find_mapping(at91_gpio->domain, n));
  1151. }
  1152. }
  1153. chained_irq_exit(chip, desc);
  1154. /* now it may re-trigger */
  1155. }
  1156. /*
  1157. * This lock class tells lockdep that GPIO irqs are in a different
  1158. * category than their parents, so it won't report false recursion.
  1159. */
  1160. static struct lock_class_key gpio_lock_class;
  1161. static int at91_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  1162. irq_hw_number_t hw)
  1163. {
  1164. struct at91_gpio_chip *at91_gpio = h->host_data;
  1165. irq_set_lockdep_class(virq, &gpio_lock_class);
  1166. /*
  1167. * Can use the "simple" and not "edge" handler since it's
  1168. * shorter, and the AIC handles interrupts sanely.
  1169. */
  1170. irq_set_chip_and_handler(virq, &gpio_irqchip,
  1171. handle_simple_irq);
  1172. set_irq_flags(virq, IRQF_VALID);
  1173. irq_set_chip_data(virq, at91_gpio);
  1174. return 0;
  1175. }
  1176. static int at91_gpio_irq_domain_xlate(struct irq_domain *d,
  1177. struct device_node *ctrlr,
  1178. const u32 *intspec, unsigned int intsize,
  1179. irq_hw_number_t *out_hwirq,
  1180. unsigned int *out_type)
  1181. {
  1182. struct at91_gpio_chip *at91_gpio = d->host_data;
  1183. int ret;
  1184. int pin = at91_gpio->chip.base + intspec[0];
  1185. if (WARN_ON(intsize < 2))
  1186. return -EINVAL;
  1187. *out_hwirq = intspec[0];
  1188. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  1189. ret = gpio_request(pin, ctrlr->full_name);
  1190. if (ret)
  1191. return ret;
  1192. ret = gpio_direction_input(pin);
  1193. if (ret)
  1194. return ret;
  1195. return 0;
  1196. }
  1197. static struct irq_domain_ops at91_gpio_ops = {
  1198. .map = at91_gpio_irq_map,
  1199. .xlate = at91_gpio_irq_domain_xlate,
  1200. };
  1201. static int at91_gpio_of_irq_setup(struct device_node *node,
  1202. struct at91_gpio_chip *at91_gpio)
  1203. {
  1204. struct at91_gpio_chip *prev = NULL;
  1205. struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
  1206. at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
  1207. /* Setup proper .irq_set_type function */
  1208. gpio_irqchip.irq_set_type = at91_gpio->ops->irq_type;
  1209. /* Disable irqs of this PIO controller */
  1210. writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
  1211. /* Setup irq domain */
  1212. at91_gpio->domain = irq_domain_add_linear(node, at91_gpio->chip.ngpio,
  1213. &at91_gpio_ops, at91_gpio);
  1214. if (!at91_gpio->domain)
  1215. panic("at91_gpio.%d: couldn't allocate irq domain (DT).\n",
  1216. at91_gpio->pioc_idx);
  1217. /* Setup chained handler */
  1218. if (at91_gpio->pioc_idx)
  1219. prev = gpio_chips[at91_gpio->pioc_idx - 1];
  1220. /* The toplevel handler handles one bank of GPIOs, except
  1221. * on some SoC it can handles up to three...
  1222. * We only set up the handler for the first of the list.
  1223. */
  1224. if (prev && prev->next == at91_gpio)
  1225. return 0;
  1226. irq_set_chip_data(at91_gpio->pioc_virq, at91_gpio);
  1227. irq_set_chained_handler(at91_gpio->pioc_virq, gpio_irq_handler);
  1228. return 0;
  1229. }
  1230. /* This structure is replicated for each GPIO block allocated at probe time */
  1231. static struct gpio_chip at91_gpio_template = {
  1232. .request = at91_gpio_request,
  1233. .free = at91_gpio_free,
  1234. .direction_input = at91_gpio_direction_input,
  1235. .get = at91_gpio_get,
  1236. .direction_output = at91_gpio_direction_output,
  1237. .set = at91_gpio_set,
  1238. .to_irq = at91_gpio_to_irq,
  1239. .dbg_show = at91_gpio_dbg_show,
  1240. .can_sleep = 0,
  1241. .ngpio = MAX_NB_GPIO_PER_BANK,
  1242. };
  1243. static void at91_gpio_probe_fixup(void)
  1244. {
  1245. unsigned i;
  1246. struct at91_gpio_chip *at91_gpio, *last = NULL;
  1247. for (i = 0; i < gpio_banks; i++) {
  1248. at91_gpio = gpio_chips[i];
  1249. /*
  1250. * GPIO controller are grouped on some SoC:
  1251. * PIOC, PIOD and PIOE can share the same IRQ line
  1252. */
  1253. if (last && last->pioc_virq == at91_gpio->pioc_virq)
  1254. last->next = at91_gpio;
  1255. last = at91_gpio;
  1256. }
  1257. }
  1258. static struct of_device_id at91_gpio_of_match[] = {
  1259. { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
  1260. { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
  1261. { /* sentinel */ }
  1262. };
  1263. static int at91_gpio_probe(struct platform_device *pdev)
  1264. {
  1265. struct device_node *np = pdev->dev.of_node;
  1266. struct resource *res;
  1267. struct at91_gpio_chip *at91_chip = NULL;
  1268. struct gpio_chip *chip;
  1269. struct pinctrl_gpio_range *range;
  1270. int ret = 0;
  1271. int irq, i;
  1272. int alias_idx = of_alias_get_id(np, "gpio");
  1273. uint32_t ngpio;
  1274. char **names;
  1275. BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
  1276. if (gpio_chips[alias_idx]) {
  1277. ret = -EBUSY;
  1278. goto err;
  1279. }
  1280. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1281. if (!res) {
  1282. ret = -ENOENT;
  1283. goto err;
  1284. }
  1285. irq = platform_get_irq(pdev, 0);
  1286. if (irq < 0) {
  1287. ret = irq;
  1288. goto err;
  1289. }
  1290. at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
  1291. if (!at91_chip) {
  1292. ret = -ENOMEM;
  1293. goto err;
  1294. }
  1295. at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
  1296. if (IS_ERR(at91_chip->regbase)) {
  1297. ret = PTR_ERR(at91_chip->regbase);
  1298. goto err;
  1299. }
  1300. at91_chip->ops = (struct at91_pinctrl_mux_ops*)
  1301. of_match_device(at91_gpio_of_match, &pdev->dev)->data;
  1302. at91_chip->pioc_virq = irq;
  1303. at91_chip->pioc_idx = alias_idx;
  1304. at91_chip->clock = clk_get(&pdev->dev, NULL);
  1305. if (IS_ERR(at91_chip->clock)) {
  1306. dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
  1307. goto err;
  1308. }
  1309. if (clk_prepare(at91_chip->clock))
  1310. goto clk_prep_err;
  1311. /* enable PIO controller's clock */
  1312. if (clk_enable(at91_chip->clock)) {
  1313. dev_err(&pdev->dev, "failed to enable clock, ignoring.\n");
  1314. goto clk_err;
  1315. }
  1316. at91_chip->chip = at91_gpio_template;
  1317. chip = &at91_chip->chip;
  1318. chip->of_node = np;
  1319. chip->label = dev_name(&pdev->dev);
  1320. chip->dev = &pdev->dev;
  1321. chip->owner = THIS_MODULE;
  1322. chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
  1323. if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
  1324. if (ngpio >= MAX_NB_GPIO_PER_BANK)
  1325. pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
  1326. alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
  1327. else
  1328. chip->ngpio = ngpio;
  1329. }
  1330. names = devm_kzalloc(&pdev->dev, sizeof(char*) * chip->ngpio, GFP_KERNEL);
  1331. if (!names) {
  1332. ret = -ENOMEM;
  1333. goto clk_err;
  1334. }
  1335. for (i = 0; i < chip->ngpio; i++)
  1336. names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
  1337. chip->names = (const char*const*)names;
  1338. range = &at91_chip->range;
  1339. range->name = chip->label;
  1340. range->id = alias_idx;
  1341. range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
  1342. range->npins = chip->ngpio;
  1343. range->gc = chip;
  1344. ret = gpiochip_add(chip);
  1345. if (ret)
  1346. goto clk_err;
  1347. gpio_chips[alias_idx] = at91_chip;
  1348. gpio_banks = max(gpio_banks, alias_idx + 1);
  1349. at91_gpio_probe_fixup();
  1350. at91_gpio_of_irq_setup(np, at91_chip);
  1351. dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
  1352. return 0;
  1353. clk_err:
  1354. clk_unprepare(at91_chip->clock);
  1355. clk_prep_err:
  1356. clk_put(at91_chip->clock);
  1357. err:
  1358. dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
  1359. return ret;
  1360. }
  1361. static struct platform_driver at91_gpio_driver = {
  1362. .driver = {
  1363. .name = "gpio-at91",
  1364. .owner = THIS_MODULE,
  1365. .of_match_table = of_match_ptr(at91_gpio_of_match),
  1366. },
  1367. .probe = at91_gpio_probe,
  1368. };
  1369. static struct platform_driver at91_pinctrl_driver = {
  1370. .driver = {
  1371. .name = "pinctrl-at91",
  1372. .owner = THIS_MODULE,
  1373. .of_match_table = of_match_ptr(at91_pinctrl_of_match),
  1374. },
  1375. .probe = at91_pinctrl_probe,
  1376. .remove = at91_pinctrl_remove,
  1377. };
  1378. static int __init at91_pinctrl_init(void)
  1379. {
  1380. int ret;
  1381. ret = platform_driver_register(&at91_gpio_driver);
  1382. if (ret)
  1383. return ret;
  1384. return platform_driver_register(&at91_pinctrl_driver);
  1385. }
  1386. arch_initcall(at91_pinctrl_init);
  1387. static void __exit at91_pinctrl_exit(void)
  1388. {
  1389. platform_driver_unregister(&at91_pinctrl_driver);
  1390. }
  1391. module_exit(at91_pinctrl_exit);
  1392. MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
  1393. MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
  1394. MODULE_LICENSE("GPL v2");