pinctrl-at91.c 45 KB

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