pinctrl-at91.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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%lx\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%lx\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 int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  516. {
  517. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  518. return info->nfunctions;
  519. }
  520. static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
  521. unsigned selector)
  522. {
  523. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  524. return info->functions[selector].name;
  525. }
  526. static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  527. const char * const **groups,
  528. unsigned * const num_groups)
  529. {
  530. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  531. *groups = info->functions[selector].groups;
  532. *num_groups = info->functions[selector].ngroups;
  533. return 0;
  534. }
  535. static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
  536. struct pinctrl_gpio_range *range,
  537. unsigned offset)
  538. {
  539. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  540. struct at91_gpio_chip *at91_chip;
  541. struct gpio_chip *chip;
  542. unsigned mask;
  543. if (!range) {
  544. dev_err(npct->dev, "invalid range\n");
  545. return -EINVAL;
  546. }
  547. if (!range->gc) {
  548. dev_err(npct->dev, "missing GPIO chip in range\n");
  549. return -EINVAL;
  550. }
  551. chip = range->gc;
  552. at91_chip = container_of(chip, struct at91_gpio_chip, chip);
  553. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  554. mask = 1 << (offset - chip->base);
  555. dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
  556. offset, 'A' + range->id, offset - chip->base, mask);
  557. writel_relaxed(mask, at91_chip->regbase + PIO_PER);
  558. return 0;
  559. }
  560. static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
  561. struct pinctrl_gpio_range *range,
  562. unsigned offset)
  563. {
  564. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  565. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  566. /* Set the pin to some default state, GPIO is usually default */
  567. }
  568. static const struct pinmux_ops at91_pmx_ops = {
  569. .get_functions_count = at91_pmx_get_funcs_count,
  570. .get_function_name = at91_pmx_get_func_name,
  571. .get_function_groups = at91_pmx_get_groups,
  572. .enable = at91_pmx_enable,
  573. .gpio_request_enable = at91_gpio_request_enable,
  574. .gpio_disable_free = at91_gpio_disable_free,
  575. };
  576. static int at91_pinconf_get(struct pinctrl_dev *pctldev,
  577. unsigned pin_id, unsigned long *config)
  578. {
  579. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  580. void __iomem *pio;
  581. unsigned pin;
  582. int div;
  583. *config = 0;
  584. dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
  585. pio = pin_to_controller(info, pin_to_bank(pin_id));
  586. pin = pin_id % MAX_NB_GPIO_PER_BANK;
  587. if (at91_mux_get_multidrive(pio, pin))
  588. *config |= MULTI_DRIVE;
  589. if (at91_mux_get_pullup(pio, pin))
  590. *config |= PULL_UP;
  591. if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
  592. *config |= DEGLITCH;
  593. if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
  594. *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
  595. if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
  596. *config |= PULL_DOWN;
  597. if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
  598. *config |= DIS_SCHMIT;
  599. return 0;
  600. }
  601. static int at91_pinconf_set(struct pinctrl_dev *pctldev,
  602. unsigned pin_id, unsigned long *configs,
  603. unsigned num_configs)
  604. {
  605. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  606. unsigned mask;
  607. void __iomem *pio;
  608. int i;
  609. unsigned long config;
  610. for (i = 0; i < num_configs; i++) {
  611. config = configs[i];
  612. dev_dbg(info->dev,
  613. "%s:%d, pin_id=%d, config=0x%lx",
  614. __func__, __LINE__, pin_id, config);
  615. pio = pin_to_controller(info, pin_to_bank(pin_id));
  616. mask = pin_to_mask(pin_id % MAX_NB_GPIO_PER_BANK);
  617. if (config & PULL_UP && config & PULL_DOWN)
  618. return -EINVAL;
  619. at91_mux_set_pullup(pio, mask, config & PULL_UP);
  620. at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
  621. if (info->ops->set_deglitch)
  622. info->ops->set_deglitch(pio, mask, config & DEGLITCH);
  623. if (info->ops->set_debounce)
  624. info->ops->set_debounce(pio, mask, config & DEBOUNCE,
  625. (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
  626. if (info->ops->set_pulldown)
  627. info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
  628. if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
  629. info->ops->disable_schmitt_trig(pio, mask);
  630. } /* for each config */
  631. return 0;
  632. }
  633. #define DBG_SHOW_FLAG(flag) do { \
  634. if (config & flag) { \
  635. if (num_conf) \
  636. seq_puts(s, "|"); \
  637. seq_puts(s, #flag); \
  638. num_conf++; \
  639. } \
  640. } while (0)
  641. static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  642. struct seq_file *s, unsigned pin_id)
  643. {
  644. unsigned long config;
  645. int val, num_conf = 0;
  646. at91_pinconf_get(pctldev, pin_id, &config);
  647. DBG_SHOW_FLAG(MULTI_DRIVE);
  648. DBG_SHOW_FLAG(PULL_UP);
  649. DBG_SHOW_FLAG(PULL_DOWN);
  650. DBG_SHOW_FLAG(DIS_SCHMIT);
  651. DBG_SHOW_FLAG(DEGLITCH);
  652. DBG_SHOW_FLAG(DEBOUNCE);
  653. if (config & DEBOUNCE) {
  654. val = config >> DEBOUNCE_VAL_SHIFT;
  655. seq_printf(s, "(%d)", val);
  656. }
  657. return;
  658. }
  659. static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  660. struct seq_file *s, unsigned group)
  661. {
  662. }
  663. static const struct pinconf_ops at91_pinconf_ops = {
  664. .pin_config_get = at91_pinconf_get,
  665. .pin_config_set = at91_pinconf_set,
  666. .pin_config_dbg_show = at91_pinconf_dbg_show,
  667. .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
  668. };
  669. static struct pinctrl_desc at91_pinctrl_desc = {
  670. .pctlops = &at91_pctrl_ops,
  671. .pmxops = &at91_pmx_ops,
  672. .confops = &at91_pinconf_ops,
  673. .owner = THIS_MODULE,
  674. };
  675. static const char *gpio_compat = "atmel,at91rm9200-gpio";
  676. static void at91_pinctrl_child_count(struct at91_pinctrl *info,
  677. struct device_node *np)
  678. {
  679. struct device_node *child;
  680. for_each_child_of_node(np, child) {
  681. if (of_device_is_compatible(child, gpio_compat)) {
  682. info->nbanks++;
  683. } else {
  684. info->nfunctions++;
  685. info->ngroups += of_get_child_count(child);
  686. }
  687. }
  688. }
  689. static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
  690. struct device_node *np)
  691. {
  692. int ret = 0;
  693. int size;
  694. const __be32 *list;
  695. list = of_get_property(np, "atmel,mux-mask", &size);
  696. if (!list) {
  697. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  698. return -EINVAL;
  699. }
  700. size /= sizeof(*list);
  701. if (!size || size % info->nbanks) {
  702. dev_err(info->dev, "wrong mux mask array should be by %d\n", info->nbanks);
  703. return -EINVAL;
  704. }
  705. info->nmux = size / info->nbanks;
  706. info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
  707. if (!info->mux_mask) {
  708. dev_err(info->dev, "could not alloc mux_mask\n");
  709. return -ENOMEM;
  710. }
  711. ret = of_property_read_u32_array(np, "atmel,mux-mask",
  712. info->mux_mask, size);
  713. if (ret)
  714. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  715. return ret;
  716. }
  717. static int at91_pinctrl_parse_groups(struct device_node *np,
  718. struct at91_pin_group *grp,
  719. struct at91_pinctrl *info, u32 index)
  720. {
  721. struct at91_pmx_pin *pin;
  722. int size;
  723. const __be32 *list;
  724. int i, j;
  725. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  726. /* Initialise group */
  727. grp->name = np->name;
  728. /*
  729. * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
  730. * do sanity check and calculate pins number
  731. */
  732. list = of_get_property(np, "atmel,pins", &size);
  733. /* we do not check return since it's safe node passed down */
  734. size /= sizeof(*list);
  735. if (!size || size % 4) {
  736. dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
  737. return -EINVAL;
  738. }
  739. grp->npins = size / 4;
  740. pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct at91_pmx_pin),
  741. GFP_KERNEL);
  742. grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
  743. GFP_KERNEL);
  744. if (!grp->pins_conf || !grp->pins)
  745. return -ENOMEM;
  746. for (i = 0, j = 0; i < size; i += 4, j++) {
  747. pin->bank = be32_to_cpu(*list++);
  748. pin->pin = be32_to_cpu(*list++);
  749. grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
  750. pin->mux = be32_to_cpu(*list++);
  751. pin->conf = be32_to_cpu(*list++);
  752. at91_pin_dbg(info->dev, pin);
  753. pin++;
  754. }
  755. return 0;
  756. }
  757. static int at91_pinctrl_parse_functions(struct device_node *np,
  758. struct at91_pinctrl *info, u32 index)
  759. {
  760. struct device_node *child;
  761. struct at91_pmx_func *func;
  762. struct at91_pin_group *grp;
  763. int ret;
  764. static u32 grp_index;
  765. u32 i = 0;
  766. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  767. func = &info->functions[index];
  768. /* Initialise function */
  769. func->name = np->name;
  770. func->ngroups = of_get_child_count(np);
  771. if (func->ngroups == 0) {
  772. dev_err(info->dev, "no groups defined\n");
  773. return -EINVAL;
  774. }
  775. func->groups = devm_kzalloc(info->dev,
  776. func->ngroups * sizeof(char *), GFP_KERNEL);
  777. if (!func->groups)
  778. return -ENOMEM;
  779. for_each_child_of_node(np, child) {
  780. func->groups[i] = child->name;
  781. grp = &info->groups[grp_index++];
  782. ret = at91_pinctrl_parse_groups(child, grp, info, i++);
  783. if (ret)
  784. return ret;
  785. }
  786. return 0;
  787. }
  788. static struct of_device_id at91_pinctrl_of_match[] = {
  789. { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
  790. { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
  791. { /* sentinel */ }
  792. };
  793. static int at91_pinctrl_probe_dt(struct platform_device *pdev,
  794. struct at91_pinctrl *info)
  795. {
  796. int ret = 0;
  797. int i, j;
  798. uint32_t *tmp;
  799. struct device_node *np = pdev->dev.of_node;
  800. struct device_node *child;
  801. if (!np)
  802. return -ENODEV;
  803. info->dev = &pdev->dev;
  804. info->ops = (struct at91_pinctrl_mux_ops *)
  805. of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
  806. at91_pinctrl_child_count(info, np);
  807. if (info->nbanks < 1) {
  808. dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
  809. return -EINVAL;
  810. }
  811. ret = at91_pinctrl_mux_mask(info, np);
  812. if (ret)
  813. return ret;
  814. dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
  815. dev_dbg(&pdev->dev, "mux-mask\n");
  816. tmp = info->mux_mask;
  817. for (i = 0; i < info->nbanks; i++) {
  818. for (j = 0; j < info->nmux; j++, tmp++) {
  819. dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
  820. }
  821. }
  822. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  823. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  824. info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct at91_pmx_func),
  825. GFP_KERNEL);
  826. if (!info->functions)
  827. return -ENOMEM;
  828. info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct at91_pin_group),
  829. GFP_KERNEL);
  830. if (!info->groups)
  831. return -ENOMEM;
  832. dev_dbg(&pdev->dev, "nbanks = %d\n", info->nbanks);
  833. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  834. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  835. i = 0;
  836. for_each_child_of_node(np, child) {
  837. if (of_device_is_compatible(child, gpio_compat))
  838. continue;
  839. ret = at91_pinctrl_parse_functions(child, info, i++);
  840. if (ret) {
  841. dev_err(&pdev->dev, "failed to parse function\n");
  842. return ret;
  843. }
  844. }
  845. return 0;
  846. }
  847. static int at91_pinctrl_probe(struct platform_device *pdev)
  848. {
  849. struct at91_pinctrl *info;
  850. struct pinctrl_pin_desc *pdesc;
  851. int ret, i, j, k;
  852. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  853. if (!info)
  854. return -ENOMEM;
  855. ret = at91_pinctrl_probe_dt(pdev, info);
  856. if (ret)
  857. return ret;
  858. /*
  859. * We need all the GPIO drivers to probe FIRST, or we will not be able
  860. * to obtain references to the struct gpio_chip * for them, and we
  861. * need this to proceed.
  862. */
  863. for (i = 0; i < info->nbanks; i++) {
  864. if (!gpio_chips[i]) {
  865. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  866. devm_kfree(&pdev->dev, info);
  867. return -EPROBE_DEFER;
  868. }
  869. }
  870. at91_pinctrl_desc.name = dev_name(&pdev->dev);
  871. at91_pinctrl_desc.npins = info->nbanks * MAX_NB_GPIO_PER_BANK;
  872. at91_pinctrl_desc.pins = pdesc =
  873. devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL);
  874. if (!at91_pinctrl_desc.pins)
  875. return -ENOMEM;
  876. for (i = 0 , k = 0; i < info->nbanks; i++) {
  877. for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
  878. pdesc->number = k;
  879. pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
  880. pdesc++;
  881. }
  882. }
  883. platform_set_drvdata(pdev, info);
  884. info->pctl = pinctrl_register(&at91_pinctrl_desc, &pdev->dev, info);
  885. if (!info->pctl) {
  886. dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
  887. ret = -EINVAL;
  888. goto err;
  889. }
  890. /* We will handle a range of GPIO pins */
  891. for (i = 0; i < info->nbanks; i++)
  892. pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
  893. dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
  894. return 0;
  895. err:
  896. return ret;
  897. }
  898. static int at91_pinctrl_remove(struct platform_device *pdev)
  899. {
  900. struct at91_pinctrl *info = platform_get_drvdata(pdev);
  901. pinctrl_unregister(info->pctl);
  902. return 0;
  903. }
  904. static int at91_gpio_request(struct gpio_chip *chip, unsigned offset)
  905. {
  906. /*
  907. * Map back to global GPIO space and request muxing, the direction
  908. * parameter does not matter for this controller.
  909. */
  910. int gpio = chip->base + offset;
  911. int bank = chip->base / chip->ngpio;
  912. dev_dbg(chip->dev, "%s:%d pio%c%d(%d)\n", __func__, __LINE__,
  913. 'A' + bank, offset, gpio);
  914. return pinctrl_request_gpio(gpio);
  915. }
  916. static void at91_gpio_free(struct gpio_chip *chip, unsigned offset)
  917. {
  918. int gpio = chip->base + offset;
  919. pinctrl_free_gpio(gpio);
  920. }
  921. static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  922. {
  923. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  924. void __iomem *pio = at91_gpio->regbase;
  925. unsigned mask = 1 << offset;
  926. u32 osr;
  927. osr = readl_relaxed(pio + PIO_OSR);
  928. return !(osr & mask);
  929. }
  930. static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  931. {
  932. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  933. void __iomem *pio = at91_gpio->regbase;
  934. unsigned mask = 1 << offset;
  935. writel_relaxed(mask, pio + PIO_ODR);
  936. return 0;
  937. }
  938. static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
  939. {
  940. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  941. void __iomem *pio = at91_gpio->regbase;
  942. unsigned mask = 1 << offset;
  943. u32 pdsr;
  944. pdsr = readl_relaxed(pio + PIO_PDSR);
  945. return (pdsr & mask) != 0;
  946. }
  947. static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
  948. int val)
  949. {
  950. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  951. void __iomem *pio = at91_gpio->regbase;
  952. unsigned mask = 1 << offset;
  953. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  954. }
  955. static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  956. int val)
  957. {
  958. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  959. void __iomem *pio = at91_gpio->regbase;
  960. unsigned mask = 1 << offset;
  961. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  962. writel_relaxed(mask, pio + PIO_OER);
  963. return 0;
  964. }
  965. #ifdef CONFIG_DEBUG_FS
  966. static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  967. {
  968. enum at91_mux mode;
  969. int i;
  970. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  971. void __iomem *pio = at91_gpio->regbase;
  972. for (i = 0; i < chip->ngpio; i++) {
  973. unsigned mask = pin_to_mask(i);
  974. const char *gpio_label;
  975. u32 pdsr;
  976. gpio_label = gpiochip_is_requested(chip, i);
  977. if (!gpio_label)
  978. continue;
  979. mode = at91_gpio->ops->get_periph(pio, mask);
  980. seq_printf(s, "[%s] GPIO%s%d: ",
  981. gpio_label, chip->label, i);
  982. if (mode == AT91_MUX_GPIO) {
  983. pdsr = readl_relaxed(pio + PIO_PDSR);
  984. seq_printf(s, "[gpio] %s\n",
  985. pdsr & mask ?
  986. "set" : "clear");
  987. } else {
  988. seq_printf(s, "[periph %c]\n",
  989. mode + 'A' - 1);
  990. }
  991. }
  992. }
  993. #else
  994. #define at91_gpio_dbg_show NULL
  995. #endif
  996. /* Several AIC controller irqs are dispatched through this GPIO handler.
  997. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  998. * at91_set_gpio_input() then maybe enable its glitch filter.
  999. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  1000. * handler.
  1001. * First implementation always triggers on rising and falling edges
  1002. * whereas the newer PIO3 can be additionally configured to trigger on
  1003. * level, edge with any polarity.
  1004. *
  1005. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  1006. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  1007. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  1008. */
  1009. static void gpio_irq_mask(struct irq_data *d)
  1010. {
  1011. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1012. void __iomem *pio = at91_gpio->regbase;
  1013. unsigned mask = 1 << d->hwirq;
  1014. if (pio)
  1015. writel_relaxed(mask, pio + PIO_IDR);
  1016. }
  1017. static void gpio_irq_unmask(struct irq_data *d)
  1018. {
  1019. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1020. void __iomem *pio = at91_gpio->regbase;
  1021. unsigned mask = 1 << d->hwirq;
  1022. if (pio)
  1023. writel_relaxed(mask, pio + PIO_IER);
  1024. }
  1025. static int gpio_irq_type(struct irq_data *d, unsigned type)
  1026. {
  1027. switch (type) {
  1028. case IRQ_TYPE_NONE:
  1029. case IRQ_TYPE_EDGE_BOTH:
  1030. return 0;
  1031. default:
  1032. return -EINVAL;
  1033. }
  1034. }
  1035. /* Alternate irq type for PIO3 support */
  1036. static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
  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. switch (type) {
  1042. case IRQ_TYPE_EDGE_RISING:
  1043. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1044. writel_relaxed(mask, pio + PIO_ESR);
  1045. writel_relaxed(mask, pio + PIO_REHLSR);
  1046. break;
  1047. case IRQ_TYPE_EDGE_FALLING:
  1048. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1049. writel_relaxed(mask, pio + PIO_ESR);
  1050. writel_relaxed(mask, pio + PIO_FELLSR);
  1051. break;
  1052. case IRQ_TYPE_LEVEL_LOW:
  1053. __irq_set_handler_locked(d->irq, handle_level_irq);
  1054. writel_relaxed(mask, pio + PIO_LSR);
  1055. writel_relaxed(mask, pio + PIO_FELLSR);
  1056. break;
  1057. case IRQ_TYPE_LEVEL_HIGH:
  1058. __irq_set_handler_locked(d->irq, handle_level_irq);
  1059. writel_relaxed(mask, pio + PIO_LSR);
  1060. writel_relaxed(mask, pio + PIO_REHLSR);
  1061. break;
  1062. case IRQ_TYPE_EDGE_BOTH:
  1063. /*
  1064. * disable additional interrupt modes:
  1065. * fall back to default behavior
  1066. */
  1067. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1068. writel_relaxed(mask, pio + PIO_AIMDR);
  1069. return 0;
  1070. case IRQ_TYPE_NONE:
  1071. default:
  1072. pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
  1073. return -EINVAL;
  1074. }
  1075. /* enable additional interrupt modes */
  1076. writel_relaxed(mask, pio + PIO_AIMER);
  1077. return 0;
  1078. }
  1079. static void gpio_irq_ack(struct irq_data *d)
  1080. {
  1081. /* the interrupt is already cleared before by reading ISR */
  1082. }
  1083. static unsigned int gpio_irq_startup(struct irq_data *d)
  1084. {
  1085. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1086. unsigned pin = d->hwirq;
  1087. int ret;
  1088. ret = gpio_lock_as_irq(&at91_gpio->chip, pin);
  1089. if (ret) {
  1090. dev_err(at91_gpio->chip.dev, "unable to lock pind %lu IRQ\n",
  1091. d->hwirq);
  1092. return ret;
  1093. }
  1094. gpio_irq_unmask(d);
  1095. return 0;
  1096. }
  1097. static void gpio_irq_shutdown(struct irq_data *d)
  1098. {
  1099. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1100. unsigned pin = d->hwirq;
  1101. gpio_irq_mask(d);
  1102. gpio_unlock_as_irq(&at91_gpio->chip, pin);
  1103. }
  1104. #ifdef CONFIG_PM
  1105. static u32 wakeups[MAX_GPIO_BANKS];
  1106. static u32 backups[MAX_GPIO_BANKS];
  1107. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  1108. {
  1109. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1110. unsigned bank = at91_gpio->pioc_idx;
  1111. unsigned mask = 1 << d->hwirq;
  1112. if (unlikely(bank >= MAX_GPIO_BANKS))
  1113. return -EINVAL;
  1114. if (state)
  1115. wakeups[bank] |= mask;
  1116. else
  1117. wakeups[bank] &= ~mask;
  1118. irq_set_irq_wake(at91_gpio->pioc_virq, state);
  1119. return 0;
  1120. }
  1121. void at91_pinctrl_gpio_suspend(void)
  1122. {
  1123. int i;
  1124. for (i = 0; i < gpio_banks; i++) {
  1125. void __iomem *pio;
  1126. if (!gpio_chips[i])
  1127. continue;
  1128. pio = gpio_chips[i]->regbase;
  1129. backups[i] = __raw_readl(pio + PIO_IMR);
  1130. __raw_writel(backups[i], pio + PIO_IDR);
  1131. __raw_writel(wakeups[i], pio + PIO_IER);
  1132. if (!wakeups[i])
  1133. clk_disable_unprepare(gpio_chips[i]->clock);
  1134. else
  1135. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
  1136. 'A'+i, wakeups[i]);
  1137. }
  1138. }
  1139. void at91_pinctrl_gpio_resume(void)
  1140. {
  1141. int i;
  1142. for (i = 0; i < gpio_banks; i++) {
  1143. void __iomem *pio;
  1144. if (!gpio_chips[i])
  1145. continue;
  1146. pio = gpio_chips[i]->regbase;
  1147. if (!wakeups[i])
  1148. clk_prepare_enable(gpio_chips[i]->clock);
  1149. __raw_writel(wakeups[i], pio + PIO_IDR);
  1150. __raw_writel(backups[i], pio + PIO_IER);
  1151. }
  1152. }
  1153. #else
  1154. #define gpio_irq_set_wake NULL
  1155. #endif /* CONFIG_PM */
  1156. static struct irq_chip gpio_irqchip = {
  1157. .name = "GPIO",
  1158. .irq_ack = gpio_irq_ack,
  1159. .irq_startup = gpio_irq_startup,
  1160. .irq_shutdown = gpio_irq_shutdown,
  1161. .irq_disable = gpio_irq_mask,
  1162. .irq_mask = gpio_irq_mask,
  1163. .irq_unmask = gpio_irq_unmask,
  1164. /* .irq_set_type is set dynamically */
  1165. .irq_set_wake = gpio_irq_set_wake,
  1166. };
  1167. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  1168. {
  1169. struct irq_chip *chip = irq_get_chip(irq);
  1170. struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
  1171. struct at91_gpio_chip *at91_gpio = container_of(gpio_chip,
  1172. struct at91_gpio_chip, chip);
  1173. void __iomem *pio = at91_gpio->regbase;
  1174. unsigned long isr;
  1175. int n;
  1176. chained_irq_enter(chip, desc);
  1177. for (;;) {
  1178. /* Reading ISR acks pending (edge triggered) GPIO interrupts.
  1179. * When there are none pending, we're finished unless we need
  1180. * to process multiple banks (like ID_PIOCDE on sam9263).
  1181. */
  1182. isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
  1183. if (!isr) {
  1184. if (!at91_gpio->next)
  1185. break;
  1186. at91_gpio = at91_gpio->next;
  1187. pio = at91_gpio->regbase;
  1188. gpio_chip = &at91_gpio->chip;
  1189. continue;
  1190. }
  1191. for_each_set_bit(n, &isr, BITS_PER_LONG) {
  1192. generic_handle_irq(irq_find_mapping(
  1193. gpio_chip->irqdomain, n));
  1194. }
  1195. }
  1196. chained_irq_exit(chip, desc);
  1197. /* now it may re-trigger */
  1198. }
  1199. static int at91_gpio_of_irq_setup(struct device_node *node,
  1200. struct at91_gpio_chip *at91_gpio)
  1201. {
  1202. struct at91_gpio_chip *prev = NULL;
  1203. struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
  1204. int ret;
  1205. at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
  1206. /* Setup proper .irq_set_type function */
  1207. gpio_irqchip.irq_set_type = at91_gpio->ops->irq_type;
  1208. /* Disable irqs of this PIO controller */
  1209. writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
  1210. /*
  1211. * Let the generic code handle this edge IRQ, the the chained
  1212. * handler will perform the actual work of handling the parent
  1213. * interrupt.
  1214. */
  1215. ret = gpiochip_irqchip_add(&at91_gpio->chip,
  1216. &gpio_irqchip,
  1217. 0,
  1218. handle_edge_irq,
  1219. IRQ_TYPE_EDGE_BOTH);
  1220. if (ret)
  1221. panic("at91_gpio.%d: couldn't allocate irq domain (DT).\n",
  1222. at91_gpio->pioc_idx);
  1223. /* Setup chained handler */
  1224. if (at91_gpio->pioc_idx)
  1225. prev = gpio_chips[at91_gpio->pioc_idx - 1];
  1226. /* The top level handler handles one bank of GPIOs, except
  1227. * on some SoC it can handle up to three...
  1228. * We only set up the handler for the first of the list.
  1229. */
  1230. if (prev && prev->next == at91_gpio)
  1231. return 0;
  1232. /* Then register the chain on the parent IRQ */
  1233. gpiochip_set_chained_irqchip(&at91_gpio->chip,
  1234. &gpio_irqchip,
  1235. at91_gpio->pioc_virq,
  1236. gpio_irq_handler);
  1237. return 0;
  1238. }
  1239. /* This structure is replicated for each GPIO block allocated at probe time */
  1240. static struct gpio_chip at91_gpio_template = {
  1241. .request = at91_gpio_request,
  1242. .free = at91_gpio_free,
  1243. .get_direction = at91_gpio_get_direction,
  1244. .direction_input = at91_gpio_direction_input,
  1245. .get = at91_gpio_get,
  1246. .direction_output = at91_gpio_direction_output,
  1247. .set = at91_gpio_set,
  1248. .dbg_show = at91_gpio_dbg_show,
  1249. .can_sleep = false,
  1250. .ngpio = MAX_NB_GPIO_PER_BANK,
  1251. };
  1252. static void at91_gpio_probe_fixup(void)
  1253. {
  1254. unsigned i;
  1255. struct at91_gpio_chip *at91_gpio, *last = NULL;
  1256. for (i = 0; i < gpio_banks; i++) {
  1257. at91_gpio = gpio_chips[i];
  1258. /*
  1259. * GPIO controller are grouped on some SoC:
  1260. * PIOC, PIOD and PIOE can share the same IRQ line
  1261. */
  1262. if (last && last->pioc_virq == at91_gpio->pioc_virq)
  1263. last->next = at91_gpio;
  1264. last = at91_gpio;
  1265. }
  1266. }
  1267. static struct of_device_id at91_gpio_of_match[] = {
  1268. { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
  1269. { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
  1270. { /* sentinel */ }
  1271. };
  1272. static int at91_gpio_probe(struct platform_device *pdev)
  1273. {
  1274. struct device_node *np = pdev->dev.of_node;
  1275. struct resource *res;
  1276. struct at91_gpio_chip *at91_chip = NULL;
  1277. struct gpio_chip *chip;
  1278. struct pinctrl_gpio_range *range;
  1279. int ret = 0;
  1280. int irq, i;
  1281. int alias_idx = of_alias_get_id(np, "gpio");
  1282. uint32_t ngpio;
  1283. char **names;
  1284. BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
  1285. if (gpio_chips[alias_idx]) {
  1286. ret = -EBUSY;
  1287. goto err;
  1288. }
  1289. irq = platform_get_irq(pdev, 0);
  1290. if (irq < 0) {
  1291. ret = irq;
  1292. goto err;
  1293. }
  1294. at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
  1295. if (!at91_chip) {
  1296. ret = -ENOMEM;
  1297. goto err;
  1298. }
  1299. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1300. at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
  1301. if (IS_ERR(at91_chip->regbase)) {
  1302. ret = PTR_ERR(at91_chip->regbase);
  1303. goto err;
  1304. }
  1305. at91_chip->ops = (struct at91_pinctrl_mux_ops *)
  1306. of_match_device(at91_gpio_of_match, &pdev->dev)->data;
  1307. at91_chip->pioc_virq = irq;
  1308. at91_chip->pioc_idx = alias_idx;
  1309. at91_chip->clock = clk_get(&pdev->dev, NULL);
  1310. if (IS_ERR(at91_chip->clock)) {
  1311. dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
  1312. goto err;
  1313. }
  1314. if (clk_prepare(at91_chip->clock))
  1315. goto clk_prep_err;
  1316. /* enable PIO controller's clock */
  1317. if (clk_enable(at91_chip->clock)) {
  1318. dev_err(&pdev->dev, "failed to enable clock, ignoring.\n");
  1319. goto clk_err;
  1320. }
  1321. at91_chip->chip = at91_gpio_template;
  1322. chip = &at91_chip->chip;
  1323. chip->of_node = np;
  1324. chip->label = dev_name(&pdev->dev);
  1325. chip->dev = &pdev->dev;
  1326. chip->owner = THIS_MODULE;
  1327. chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
  1328. if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
  1329. if (ngpio >= MAX_NB_GPIO_PER_BANK)
  1330. pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
  1331. alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
  1332. else
  1333. chip->ngpio = ngpio;
  1334. }
  1335. names = devm_kzalloc(&pdev->dev, sizeof(char *) * chip->ngpio,
  1336. GFP_KERNEL);
  1337. if (!names) {
  1338. ret = -ENOMEM;
  1339. goto clk_err;
  1340. }
  1341. for (i = 0; i < chip->ngpio; i++)
  1342. names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
  1343. chip->names = (const char *const *)names;
  1344. range = &at91_chip->range;
  1345. range->name = chip->label;
  1346. range->id = alias_idx;
  1347. range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
  1348. range->npins = chip->ngpio;
  1349. range->gc = chip;
  1350. ret = gpiochip_add(chip);
  1351. if (ret)
  1352. goto clk_err;
  1353. gpio_chips[alias_idx] = at91_chip;
  1354. gpio_banks = max(gpio_banks, alias_idx + 1);
  1355. at91_gpio_probe_fixup();
  1356. at91_gpio_of_irq_setup(np, at91_chip);
  1357. dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
  1358. return 0;
  1359. clk_err:
  1360. clk_unprepare(at91_chip->clock);
  1361. clk_prep_err:
  1362. clk_put(at91_chip->clock);
  1363. err:
  1364. dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
  1365. return ret;
  1366. }
  1367. static struct platform_driver at91_gpio_driver = {
  1368. .driver = {
  1369. .name = "gpio-at91",
  1370. .owner = THIS_MODULE,
  1371. .of_match_table = at91_gpio_of_match,
  1372. },
  1373. .probe = at91_gpio_probe,
  1374. };
  1375. static struct platform_driver at91_pinctrl_driver = {
  1376. .driver = {
  1377. .name = "pinctrl-at91",
  1378. .owner = THIS_MODULE,
  1379. .of_match_table = at91_pinctrl_of_match,
  1380. },
  1381. .probe = at91_pinctrl_probe,
  1382. .remove = at91_pinctrl_remove,
  1383. };
  1384. static int __init at91_pinctrl_init(void)
  1385. {
  1386. int ret;
  1387. ret = platform_driver_register(&at91_gpio_driver);
  1388. if (ret)
  1389. return ret;
  1390. return platform_driver_register(&at91_pinctrl_driver);
  1391. }
  1392. arch_initcall(at91_pinctrl_init);
  1393. static void __exit at91_pinctrl_exit(void)
  1394. {
  1395. platform_driver_unregister(&at91_pinctrl_driver);
  1396. }
  1397. module_exit(at91_pinctrl_exit);
  1398. MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
  1399. MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
  1400. MODULE_LICENSE("GPL v2");