pinctrl-at91.c 42 KB

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