pinctrl-at91.c 44 KB

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