pinctrl-at91.c 49 KB

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