pinctrl-at91.c 38 KB

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