pinctrl-at91.c 49 KB

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