pinctrl-at91.c 42 KB

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