pinctrl-at91.c 44 KB

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