pinctrl-at91.c 44 KB

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