pinctrl-at91.c 44 KB

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