pinctrl-at91.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  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 nactive_banks;
  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 (readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1;
  382. }
  383. static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
  384. {
  385. writel_relaxed(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 ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1)
  390. return !((readl_relaxed(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. writel_relaxed(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 = readl_relaxed(pio + PIO_SCDR);
  402. return ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1) &&
  403. ((readl_relaxed(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. writel_relaxed(mask, pio + PIO_IFSCER);
  410. writel_relaxed(div & PIO_SCDR_DIV, pio + PIO_SCDR);
  411. writel_relaxed(mask, pio + PIO_IFER);
  412. } else
  413. writel_relaxed(mask, pio + PIO_IFSCDR);
  414. }
  415. static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
  416. {
  417. return !((readl_relaxed(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. writel_relaxed(mask, pio + PIO_PUDR);
  423. writel_relaxed(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. writel_relaxed(readl_relaxed(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 (readl_relaxed(pio + PIO_SCHMITT) >> pin) & 0x1;
  432. }
  433. static inline u32 read_drive_strength(void __iomem *reg, unsigned pin)
  434. {
  435. unsigned tmp = readl_relaxed(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 = readl_relaxed(reg);
  463. unsigned shift = two_bit_pin_value_shift_amount(pin);
  464. tmp &= ~(DRIVE_STRENGTH_MASK << shift);
  465. tmp |= strength << shift;
  466. writel_relaxed(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 >= gpio_banks) {
  549. dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
  550. name, index, pin->bank, gpio_banks);
  551. return -EINVAL;
  552. }
  553. if (!gpio_chips[pin->bank]) {
  554. dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
  555. name, index, pin->bank);
  556. return -ENXIO;
  557. }
  558. if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
  559. dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
  560. name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
  561. return -EINVAL;
  562. }
  563. if (!pin->mux)
  564. return 0;
  565. mux = pin->mux - 1;
  566. if (mux >= info->nmux) {
  567. dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
  568. name, index, mux, info->nmux);
  569. return -EINVAL;
  570. }
  571. if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
  572. dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
  573. name, index, mux, pin->bank + 'A', pin->pin);
  574. return -EINVAL;
  575. }
  576. return 0;
  577. }
  578. static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
  579. {
  580. writel_relaxed(mask, pio + PIO_PDR);
  581. }
  582. static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
  583. {
  584. writel_relaxed(mask, pio + PIO_PER);
  585. writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
  586. }
  587. static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
  588. unsigned group)
  589. {
  590. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  591. const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
  592. const struct at91_pmx_pin *pin;
  593. uint32_t npins = info->groups[group].npins;
  594. int i, ret;
  595. unsigned mask;
  596. void __iomem *pio;
  597. dev_dbg(info->dev, "enable function %s group %s\n",
  598. info->functions[selector].name, info->groups[group].name);
  599. /* first check that all the pins of the group are valid with a valid
  600. * parameter */
  601. for (i = 0; i < npins; i++) {
  602. pin = &pins_conf[i];
  603. ret = pin_check_config(info, info->groups[group].name, i, pin);
  604. if (ret)
  605. return ret;
  606. }
  607. for (i = 0; i < npins; i++) {
  608. pin = &pins_conf[i];
  609. at91_pin_dbg(info->dev, pin);
  610. pio = pin_to_controller(info, pin->bank);
  611. mask = pin_to_mask(pin->pin);
  612. at91_mux_disable_interrupt(pio, mask);
  613. switch (pin->mux) {
  614. case AT91_MUX_GPIO:
  615. at91_mux_gpio_enable(pio, mask, 1);
  616. break;
  617. case AT91_MUX_PERIPH_A:
  618. info->ops->mux_A_periph(pio, mask);
  619. break;
  620. case AT91_MUX_PERIPH_B:
  621. info->ops->mux_B_periph(pio, mask);
  622. break;
  623. case AT91_MUX_PERIPH_C:
  624. if (!info->ops->mux_C_periph)
  625. return -EINVAL;
  626. info->ops->mux_C_periph(pio, mask);
  627. break;
  628. case AT91_MUX_PERIPH_D:
  629. if (!info->ops->mux_D_periph)
  630. return -EINVAL;
  631. info->ops->mux_D_periph(pio, mask);
  632. break;
  633. }
  634. if (pin->mux)
  635. at91_mux_gpio_disable(pio, mask);
  636. }
  637. return 0;
  638. }
  639. static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  640. {
  641. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  642. return info->nfunctions;
  643. }
  644. static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
  645. unsigned selector)
  646. {
  647. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  648. return info->functions[selector].name;
  649. }
  650. static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  651. const char * const **groups,
  652. unsigned * const num_groups)
  653. {
  654. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  655. *groups = info->functions[selector].groups;
  656. *num_groups = info->functions[selector].ngroups;
  657. return 0;
  658. }
  659. static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
  660. struct pinctrl_gpio_range *range,
  661. unsigned offset)
  662. {
  663. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  664. struct at91_gpio_chip *at91_chip;
  665. struct gpio_chip *chip;
  666. unsigned mask;
  667. if (!range) {
  668. dev_err(npct->dev, "invalid range\n");
  669. return -EINVAL;
  670. }
  671. if (!range->gc) {
  672. dev_err(npct->dev, "missing GPIO chip in range\n");
  673. return -EINVAL;
  674. }
  675. chip = range->gc;
  676. at91_chip = container_of(chip, struct at91_gpio_chip, chip);
  677. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  678. mask = 1 << (offset - chip->base);
  679. dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
  680. offset, 'A' + range->id, offset - chip->base, mask);
  681. writel_relaxed(mask, at91_chip->regbase + PIO_PER);
  682. return 0;
  683. }
  684. static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
  685. struct pinctrl_gpio_range *range,
  686. unsigned offset)
  687. {
  688. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  689. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  690. /* Set the pin to some default state, GPIO is usually default */
  691. }
  692. static const struct pinmux_ops at91_pmx_ops = {
  693. .get_functions_count = at91_pmx_get_funcs_count,
  694. .get_function_name = at91_pmx_get_func_name,
  695. .get_function_groups = at91_pmx_get_groups,
  696. .set_mux = at91_pmx_set,
  697. .gpio_request_enable = at91_gpio_request_enable,
  698. .gpio_disable_free = at91_gpio_disable_free,
  699. };
  700. static int at91_pinconf_get(struct pinctrl_dev *pctldev,
  701. unsigned pin_id, unsigned long *config)
  702. {
  703. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  704. void __iomem *pio;
  705. unsigned pin;
  706. int div;
  707. *config = 0;
  708. dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
  709. pio = pin_to_controller(info, pin_to_bank(pin_id));
  710. pin = pin_id % MAX_NB_GPIO_PER_BANK;
  711. if (at91_mux_get_multidrive(pio, pin))
  712. *config |= MULTI_DRIVE;
  713. if (at91_mux_get_pullup(pio, pin))
  714. *config |= PULL_UP;
  715. if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
  716. *config |= DEGLITCH;
  717. if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
  718. *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
  719. if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
  720. *config |= PULL_DOWN;
  721. if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
  722. *config |= DIS_SCHMIT;
  723. if (info->ops->get_drivestrength)
  724. *config |= (info->ops->get_drivestrength(pio, pin)
  725. << DRIVE_STRENGTH_SHIFT);
  726. return 0;
  727. }
  728. static int at91_pinconf_set(struct pinctrl_dev *pctldev,
  729. unsigned pin_id, unsigned long *configs,
  730. unsigned num_configs)
  731. {
  732. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  733. unsigned mask;
  734. void __iomem *pio;
  735. int i;
  736. unsigned long config;
  737. unsigned pin;
  738. for (i = 0; i < num_configs; i++) {
  739. config = configs[i];
  740. dev_dbg(info->dev,
  741. "%s:%d, pin_id=%d, config=0x%lx",
  742. __func__, __LINE__, pin_id, config);
  743. pio = pin_to_controller(info, pin_to_bank(pin_id));
  744. pin = pin_id % MAX_NB_GPIO_PER_BANK;
  745. mask = pin_to_mask(pin);
  746. if (config & PULL_UP && config & PULL_DOWN)
  747. return -EINVAL;
  748. at91_mux_set_pullup(pio, mask, config & PULL_UP);
  749. at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
  750. if (info->ops->set_deglitch)
  751. info->ops->set_deglitch(pio, mask, config & DEGLITCH);
  752. if (info->ops->set_debounce)
  753. info->ops->set_debounce(pio, mask, config & DEBOUNCE,
  754. (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
  755. if (info->ops->set_pulldown)
  756. info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
  757. if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
  758. info->ops->disable_schmitt_trig(pio, mask);
  759. if (info->ops->set_drivestrength)
  760. info->ops->set_drivestrength(pio, pin,
  761. (config & DRIVE_STRENGTH)
  762. >> DRIVE_STRENGTH_SHIFT);
  763. } /* for each config */
  764. return 0;
  765. }
  766. #define DBG_SHOW_FLAG(flag) do { \
  767. if (config & flag) { \
  768. if (num_conf) \
  769. seq_puts(s, "|"); \
  770. seq_puts(s, #flag); \
  771. num_conf++; \
  772. } \
  773. } while (0)
  774. #define DBG_SHOW_FLAG_MASKED(mask,flag) do { \
  775. if ((config & mask) == flag) { \
  776. if (num_conf) \
  777. seq_puts(s, "|"); \
  778. seq_puts(s, #flag); \
  779. num_conf++; \
  780. } \
  781. } while (0)
  782. static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  783. struct seq_file *s, unsigned pin_id)
  784. {
  785. unsigned long config;
  786. int val, num_conf = 0;
  787. at91_pinconf_get(pctldev, pin_id, &config);
  788. DBG_SHOW_FLAG(MULTI_DRIVE);
  789. DBG_SHOW_FLAG(PULL_UP);
  790. DBG_SHOW_FLAG(PULL_DOWN);
  791. DBG_SHOW_FLAG(DIS_SCHMIT);
  792. DBG_SHOW_FLAG(DEGLITCH);
  793. DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_LOW);
  794. DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_MED);
  795. DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_HI);
  796. DBG_SHOW_FLAG(DEBOUNCE);
  797. if (config & DEBOUNCE) {
  798. val = config >> DEBOUNCE_VAL_SHIFT;
  799. seq_printf(s, "(%d)", val);
  800. }
  801. return;
  802. }
  803. static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  804. struct seq_file *s, unsigned group)
  805. {
  806. }
  807. static const struct pinconf_ops at91_pinconf_ops = {
  808. .pin_config_get = at91_pinconf_get,
  809. .pin_config_set = at91_pinconf_set,
  810. .pin_config_dbg_show = at91_pinconf_dbg_show,
  811. .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
  812. };
  813. static struct pinctrl_desc at91_pinctrl_desc = {
  814. .pctlops = &at91_pctrl_ops,
  815. .pmxops = &at91_pmx_ops,
  816. .confops = &at91_pinconf_ops,
  817. .owner = THIS_MODULE,
  818. };
  819. static const char *gpio_compat = "atmel,at91rm9200-gpio";
  820. static void at91_pinctrl_child_count(struct at91_pinctrl *info,
  821. struct device_node *np)
  822. {
  823. struct device_node *child;
  824. for_each_child_of_node(np, child) {
  825. if (of_device_is_compatible(child, gpio_compat)) {
  826. if (of_device_is_available(child))
  827. info->nactive_banks++;
  828. } else {
  829. info->nfunctions++;
  830. info->ngroups += of_get_child_count(child);
  831. }
  832. }
  833. }
  834. static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
  835. struct device_node *np)
  836. {
  837. int ret = 0;
  838. int size;
  839. const __be32 *list;
  840. list = of_get_property(np, "atmel,mux-mask", &size);
  841. if (!list) {
  842. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  843. return -EINVAL;
  844. }
  845. size /= sizeof(*list);
  846. if (!size || size % gpio_banks) {
  847. dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
  848. return -EINVAL;
  849. }
  850. info->nmux = size / gpio_banks;
  851. info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
  852. if (!info->mux_mask) {
  853. dev_err(info->dev, "could not alloc mux_mask\n");
  854. return -ENOMEM;
  855. }
  856. ret = of_property_read_u32_array(np, "atmel,mux-mask",
  857. info->mux_mask, size);
  858. if (ret)
  859. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  860. return ret;
  861. }
  862. static int at91_pinctrl_parse_groups(struct device_node *np,
  863. struct at91_pin_group *grp,
  864. struct at91_pinctrl *info, u32 index)
  865. {
  866. struct at91_pmx_pin *pin;
  867. int size;
  868. const __be32 *list;
  869. int i, j;
  870. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  871. /* Initialise group */
  872. grp->name = np->name;
  873. /*
  874. * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
  875. * do sanity check and calculate pins number
  876. */
  877. list = of_get_property(np, "atmel,pins", &size);
  878. /* we do not check return since it's safe node passed down */
  879. size /= sizeof(*list);
  880. if (!size || size % 4) {
  881. dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
  882. return -EINVAL;
  883. }
  884. grp->npins = size / 4;
  885. pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct at91_pmx_pin),
  886. GFP_KERNEL);
  887. grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
  888. GFP_KERNEL);
  889. if (!grp->pins_conf || !grp->pins)
  890. return -ENOMEM;
  891. for (i = 0, j = 0; i < size; i += 4, j++) {
  892. pin->bank = be32_to_cpu(*list++);
  893. pin->pin = be32_to_cpu(*list++);
  894. grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
  895. pin->mux = be32_to_cpu(*list++);
  896. pin->conf = be32_to_cpu(*list++);
  897. at91_pin_dbg(info->dev, pin);
  898. pin++;
  899. }
  900. return 0;
  901. }
  902. static int at91_pinctrl_parse_functions(struct device_node *np,
  903. struct at91_pinctrl *info, u32 index)
  904. {
  905. struct device_node *child;
  906. struct at91_pmx_func *func;
  907. struct at91_pin_group *grp;
  908. int ret;
  909. static u32 grp_index;
  910. u32 i = 0;
  911. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  912. func = &info->functions[index];
  913. /* Initialise function */
  914. func->name = np->name;
  915. func->ngroups = of_get_child_count(np);
  916. if (func->ngroups == 0) {
  917. dev_err(info->dev, "no groups defined\n");
  918. return -EINVAL;
  919. }
  920. func->groups = devm_kzalloc(info->dev,
  921. func->ngroups * sizeof(char *), GFP_KERNEL);
  922. if (!func->groups)
  923. return -ENOMEM;
  924. for_each_child_of_node(np, child) {
  925. func->groups[i] = child->name;
  926. grp = &info->groups[grp_index++];
  927. ret = at91_pinctrl_parse_groups(child, grp, info, i++);
  928. if (ret)
  929. return ret;
  930. }
  931. return 0;
  932. }
  933. static const struct of_device_id at91_pinctrl_of_match[] = {
  934. { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
  935. { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
  936. { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
  937. { /* sentinel */ }
  938. };
  939. static int at91_pinctrl_probe_dt(struct platform_device *pdev,
  940. struct at91_pinctrl *info)
  941. {
  942. int ret = 0;
  943. int i, j;
  944. uint32_t *tmp;
  945. struct device_node *np = pdev->dev.of_node;
  946. struct device_node *child;
  947. if (!np)
  948. return -ENODEV;
  949. info->dev = &pdev->dev;
  950. info->ops = (struct at91_pinctrl_mux_ops *)
  951. of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
  952. at91_pinctrl_child_count(info, np);
  953. if (gpio_banks < 1) {
  954. dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
  955. return -EINVAL;
  956. }
  957. ret = at91_pinctrl_mux_mask(info, np);
  958. if (ret)
  959. return ret;
  960. dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
  961. dev_dbg(&pdev->dev, "mux-mask\n");
  962. tmp = info->mux_mask;
  963. for (i = 0; i < gpio_banks; i++) {
  964. for (j = 0; j < info->nmux; j++, tmp++) {
  965. dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
  966. }
  967. }
  968. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  969. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  970. info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct at91_pmx_func),
  971. GFP_KERNEL);
  972. if (!info->functions)
  973. return -ENOMEM;
  974. info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct at91_pin_group),
  975. GFP_KERNEL);
  976. if (!info->groups)
  977. return -ENOMEM;
  978. dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
  979. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  980. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  981. i = 0;
  982. for_each_child_of_node(np, child) {
  983. if (of_device_is_compatible(child, gpio_compat))
  984. continue;
  985. ret = at91_pinctrl_parse_functions(child, info, i++);
  986. if (ret) {
  987. dev_err(&pdev->dev, "failed to parse function\n");
  988. return ret;
  989. }
  990. }
  991. return 0;
  992. }
  993. static int at91_pinctrl_probe(struct platform_device *pdev)
  994. {
  995. struct at91_pinctrl *info;
  996. struct pinctrl_pin_desc *pdesc;
  997. int ret, i, j, k, ngpio_chips_enabled = 0;
  998. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  999. if (!info)
  1000. return -ENOMEM;
  1001. ret = at91_pinctrl_probe_dt(pdev, info);
  1002. if (ret)
  1003. return ret;
  1004. /*
  1005. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1006. * to obtain references to the struct gpio_chip * for them, and we
  1007. * need this to proceed.
  1008. */
  1009. for (i = 0; i < gpio_banks; i++)
  1010. if (gpio_chips[i])
  1011. ngpio_chips_enabled++;
  1012. if (ngpio_chips_enabled < info->nactive_banks) {
  1013. dev_warn(&pdev->dev,
  1014. "All GPIO chips are not registered yet (%d/%d)\n",
  1015. ngpio_chips_enabled, info->nactive_banks);
  1016. devm_kfree(&pdev->dev, info);
  1017. return -EPROBE_DEFER;
  1018. }
  1019. at91_pinctrl_desc.name = dev_name(&pdev->dev);
  1020. at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
  1021. at91_pinctrl_desc.pins = pdesc =
  1022. devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL);
  1023. if (!at91_pinctrl_desc.pins)
  1024. return -ENOMEM;
  1025. for (i = 0, k = 0; i < gpio_banks; i++) {
  1026. for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
  1027. pdesc->number = k;
  1028. pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
  1029. pdesc++;
  1030. }
  1031. }
  1032. platform_set_drvdata(pdev, info);
  1033. info->pctl = pinctrl_register(&at91_pinctrl_desc, &pdev->dev, info);
  1034. if (IS_ERR(info->pctl)) {
  1035. dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
  1036. return PTR_ERR(info->pctl);
  1037. }
  1038. /* We will handle a range of GPIO pins */
  1039. for (i = 0; i < gpio_banks; i++)
  1040. if (gpio_chips[i])
  1041. pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
  1042. dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
  1043. return 0;
  1044. }
  1045. static int at91_pinctrl_remove(struct platform_device *pdev)
  1046. {
  1047. struct at91_pinctrl *info = platform_get_drvdata(pdev);
  1048. pinctrl_unregister(info->pctl);
  1049. return 0;
  1050. }
  1051. static int at91_gpio_request(struct gpio_chip *chip, unsigned offset)
  1052. {
  1053. /*
  1054. * Map back to global GPIO space and request muxing, the direction
  1055. * parameter does not matter for this controller.
  1056. */
  1057. int gpio = chip->base + offset;
  1058. int bank = chip->base / chip->ngpio;
  1059. dev_dbg(chip->dev, "%s:%d pio%c%d(%d)\n", __func__, __LINE__,
  1060. 'A' + bank, offset, gpio);
  1061. return pinctrl_request_gpio(gpio);
  1062. }
  1063. static void at91_gpio_free(struct gpio_chip *chip, unsigned offset)
  1064. {
  1065. int gpio = chip->base + offset;
  1066. pinctrl_free_gpio(gpio);
  1067. }
  1068. static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  1069. {
  1070. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1071. void __iomem *pio = at91_gpio->regbase;
  1072. unsigned mask = 1 << offset;
  1073. u32 osr;
  1074. osr = readl_relaxed(pio + PIO_OSR);
  1075. return !(osr & mask);
  1076. }
  1077. static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  1078. {
  1079. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1080. void __iomem *pio = at91_gpio->regbase;
  1081. unsigned mask = 1 << offset;
  1082. writel_relaxed(mask, pio + PIO_ODR);
  1083. return 0;
  1084. }
  1085. static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
  1086. {
  1087. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1088. void __iomem *pio = at91_gpio->regbase;
  1089. unsigned mask = 1 << offset;
  1090. u32 pdsr;
  1091. pdsr = readl_relaxed(pio + PIO_PDSR);
  1092. return (pdsr & mask) != 0;
  1093. }
  1094. static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
  1095. int val)
  1096. {
  1097. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1098. void __iomem *pio = at91_gpio->regbase;
  1099. unsigned mask = 1 << offset;
  1100. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  1101. }
  1102. static void at91_gpio_set_multiple(struct gpio_chip *chip,
  1103. unsigned long *mask, unsigned long *bits)
  1104. {
  1105. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1106. void __iomem *pio = at91_gpio->regbase;
  1107. #define BITS_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
  1108. /* Mask additionally to ngpio as not all GPIO controllers have 32 pins */
  1109. uint32_t set_mask = (*mask & *bits) & BITS_MASK(chip->ngpio);
  1110. uint32_t clear_mask = (*mask & ~(*bits)) & BITS_MASK(chip->ngpio);
  1111. writel_relaxed(set_mask, pio + PIO_SODR);
  1112. writel_relaxed(clear_mask, pio + PIO_CODR);
  1113. }
  1114. static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  1115. int val)
  1116. {
  1117. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1118. void __iomem *pio = at91_gpio->regbase;
  1119. unsigned mask = 1 << offset;
  1120. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  1121. writel_relaxed(mask, pio + PIO_OER);
  1122. return 0;
  1123. }
  1124. #ifdef CONFIG_DEBUG_FS
  1125. static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1126. {
  1127. enum at91_mux mode;
  1128. int i;
  1129. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1130. void __iomem *pio = at91_gpio->regbase;
  1131. for (i = 0; i < chip->ngpio; i++) {
  1132. unsigned mask = pin_to_mask(i);
  1133. const char *gpio_label;
  1134. gpio_label = gpiochip_is_requested(chip, i);
  1135. if (!gpio_label)
  1136. continue;
  1137. mode = at91_gpio->ops->get_periph(pio, mask);
  1138. seq_printf(s, "[%s] GPIO%s%d: ",
  1139. gpio_label, chip->label, i);
  1140. if (mode == AT91_MUX_GPIO) {
  1141. seq_printf(s, "[gpio] ");
  1142. seq_printf(s, "%s ",
  1143. readl_relaxed(pio + PIO_OSR) & mask ?
  1144. "output" : "input");
  1145. seq_printf(s, "%s\n",
  1146. readl_relaxed(pio + PIO_PDSR) & mask ?
  1147. "set" : "clear");
  1148. } else {
  1149. seq_printf(s, "[periph %c]\n",
  1150. mode + 'A' - 1);
  1151. }
  1152. }
  1153. }
  1154. #else
  1155. #define at91_gpio_dbg_show NULL
  1156. #endif
  1157. /* Several AIC controller irqs are dispatched through this GPIO handler.
  1158. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  1159. * at91_set_gpio_input() then maybe enable its glitch filter.
  1160. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  1161. * handler.
  1162. * First implementation always triggers on rising and falling edges
  1163. * whereas the newer PIO3 can be additionally configured to trigger on
  1164. * level, edge with any polarity.
  1165. *
  1166. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  1167. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  1168. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  1169. */
  1170. static void gpio_irq_mask(struct irq_data *d)
  1171. {
  1172. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1173. void __iomem *pio = at91_gpio->regbase;
  1174. unsigned mask = 1 << d->hwirq;
  1175. if (pio)
  1176. writel_relaxed(mask, pio + PIO_IDR);
  1177. }
  1178. static void gpio_irq_unmask(struct irq_data *d)
  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. if (pio)
  1184. writel_relaxed(mask, pio + PIO_IER);
  1185. }
  1186. static int gpio_irq_type(struct irq_data *d, unsigned type)
  1187. {
  1188. switch (type) {
  1189. case IRQ_TYPE_NONE:
  1190. case IRQ_TYPE_EDGE_BOTH:
  1191. return 0;
  1192. default:
  1193. return -EINVAL;
  1194. }
  1195. }
  1196. /* Alternate irq type for PIO3 support */
  1197. static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
  1198. {
  1199. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1200. void __iomem *pio = at91_gpio->regbase;
  1201. unsigned mask = 1 << d->hwirq;
  1202. switch (type) {
  1203. case IRQ_TYPE_EDGE_RISING:
  1204. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1205. writel_relaxed(mask, pio + PIO_ESR);
  1206. writel_relaxed(mask, pio + PIO_REHLSR);
  1207. break;
  1208. case IRQ_TYPE_EDGE_FALLING:
  1209. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1210. writel_relaxed(mask, pio + PIO_ESR);
  1211. writel_relaxed(mask, pio + PIO_FELLSR);
  1212. break;
  1213. case IRQ_TYPE_LEVEL_LOW:
  1214. __irq_set_handler_locked(d->irq, handle_level_irq);
  1215. writel_relaxed(mask, pio + PIO_LSR);
  1216. writel_relaxed(mask, pio + PIO_FELLSR);
  1217. break;
  1218. case IRQ_TYPE_LEVEL_HIGH:
  1219. __irq_set_handler_locked(d->irq, handle_level_irq);
  1220. writel_relaxed(mask, pio + PIO_LSR);
  1221. writel_relaxed(mask, pio + PIO_REHLSR);
  1222. break;
  1223. case IRQ_TYPE_EDGE_BOTH:
  1224. /*
  1225. * disable additional interrupt modes:
  1226. * fall back to default behavior
  1227. */
  1228. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1229. writel_relaxed(mask, pio + PIO_AIMDR);
  1230. return 0;
  1231. case IRQ_TYPE_NONE:
  1232. default:
  1233. pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
  1234. return -EINVAL;
  1235. }
  1236. /* enable additional interrupt modes */
  1237. writel_relaxed(mask, pio + PIO_AIMER);
  1238. return 0;
  1239. }
  1240. static void gpio_irq_ack(struct irq_data *d)
  1241. {
  1242. /* the interrupt is already cleared before by reading ISR */
  1243. }
  1244. static int gpio_irq_request_res(struct irq_data *d)
  1245. {
  1246. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1247. unsigned pin = d->hwirq;
  1248. int ret;
  1249. ret = gpiochip_lock_as_irq(&at91_gpio->chip, pin);
  1250. if (ret)
  1251. dev_err(at91_gpio->chip.dev, "unable to lock pind %lu IRQ\n",
  1252. d->hwirq);
  1253. return ret;
  1254. }
  1255. static void gpio_irq_release_res(struct irq_data *d)
  1256. {
  1257. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1258. unsigned pin = d->hwirq;
  1259. gpiochip_unlock_as_irq(&at91_gpio->chip, pin);
  1260. }
  1261. #ifdef CONFIG_PM
  1262. static u32 wakeups[MAX_GPIO_BANKS];
  1263. static u32 backups[MAX_GPIO_BANKS];
  1264. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  1265. {
  1266. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1267. unsigned bank = at91_gpio->pioc_idx;
  1268. unsigned mask = 1 << d->hwirq;
  1269. if (unlikely(bank >= MAX_GPIO_BANKS))
  1270. return -EINVAL;
  1271. if (state)
  1272. wakeups[bank] |= mask;
  1273. else
  1274. wakeups[bank] &= ~mask;
  1275. irq_set_irq_wake(at91_gpio->pioc_virq, state);
  1276. return 0;
  1277. }
  1278. void at91_pinctrl_gpio_suspend(void)
  1279. {
  1280. int i;
  1281. for (i = 0; i < gpio_banks; i++) {
  1282. void __iomem *pio;
  1283. if (!gpio_chips[i])
  1284. continue;
  1285. pio = gpio_chips[i]->regbase;
  1286. backups[i] = readl_relaxed(pio + PIO_IMR);
  1287. writel_relaxed(backups[i], pio + PIO_IDR);
  1288. writel_relaxed(wakeups[i], pio + PIO_IER);
  1289. if (!wakeups[i])
  1290. clk_disable_unprepare(gpio_chips[i]->clock);
  1291. else
  1292. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
  1293. 'A'+i, wakeups[i]);
  1294. }
  1295. }
  1296. void at91_pinctrl_gpio_resume(void)
  1297. {
  1298. int i;
  1299. for (i = 0; i < gpio_banks; i++) {
  1300. void __iomem *pio;
  1301. if (!gpio_chips[i])
  1302. continue;
  1303. pio = gpio_chips[i]->regbase;
  1304. if (!wakeups[i])
  1305. clk_prepare_enable(gpio_chips[i]->clock);
  1306. writel_relaxed(wakeups[i], pio + PIO_IDR);
  1307. writel_relaxed(backups[i], pio + PIO_IER);
  1308. }
  1309. }
  1310. #else
  1311. #define gpio_irq_set_wake NULL
  1312. #endif /* CONFIG_PM */
  1313. static struct irq_chip gpio_irqchip = {
  1314. .name = "GPIO",
  1315. .irq_ack = gpio_irq_ack,
  1316. .irq_request_resources = gpio_irq_request_res,
  1317. .irq_release_resources = gpio_irq_release_res,
  1318. .irq_disable = gpio_irq_mask,
  1319. .irq_mask = gpio_irq_mask,
  1320. .irq_unmask = gpio_irq_unmask,
  1321. /* .irq_set_type is set dynamically */
  1322. .irq_set_wake = gpio_irq_set_wake,
  1323. };
  1324. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  1325. {
  1326. struct irq_chip *chip = irq_get_chip(irq);
  1327. struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
  1328. struct at91_gpio_chip *at91_gpio = container_of(gpio_chip,
  1329. struct at91_gpio_chip, chip);
  1330. void __iomem *pio = at91_gpio->regbase;
  1331. unsigned long isr;
  1332. int n;
  1333. chained_irq_enter(chip, desc);
  1334. for (;;) {
  1335. /* Reading ISR acks pending (edge triggered) GPIO interrupts.
  1336. * When there are none pending, we're finished unless we need
  1337. * to process multiple banks (like ID_PIOCDE on sam9263).
  1338. */
  1339. isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
  1340. if (!isr) {
  1341. if (!at91_gpio->next)
  1342. break;
  1343. at91_gpio = at91_gpio->next;
  1344. pio = at91_gpio->regbase;
  1345. gpio_chip = &at91_gpio->chip;
  1346. continue;
  1347. }
  1348. for_each_set_bit(n, &isr, BITS_PER_LONG) {
  1349. generic_handle_irq(irq_find_mapping(
  1350. gpio_chip->irqdomain, n));
  1351. }
  1352. }
  1353. chained_irq_exit(chip, desc);
  1354. /* now it may re-trigger */
  1355. }
  1356. static int at91_gpio_of_irq_setup(struct platform_device *pdev,
  1357. struct at91_gpio_chip *at91_gpio)
  1358. {
  1359. struct gpio_chip *gpiochip_prev = NULL;
  1360. struct at91_gpio_chip *prev = NULL;
  1361. struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
  1362. int ret, i;
  1363. at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
  1364. /* Setup proper .irq_set_type function */
  1365. gpio_irqchip.irq_set_type = at91_gpio->ops->irq_type;
  1366. /* Disable irqs of this PIO controller */
  1367. writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
  1368. /*
  1369. * Let the generic code handle this edge IRQ, the the chained
  1370. * handler will perform the actual work of handling the parent
  1371. * interrupt.
  1372. */
  1373. ret = gpiochip_irqchip_add(&at91_gpio->chip,
  1374. &gpio_irqchip,
  1375. 0,
  1376. handle_edge_irq,
  1377. IRQ_TYPE_EDGE_BOTH);
  1378. if (ret) {
  1379. dev_err(&pdev->dev, "at91_gpio.%d: Couldn't add irqchip to gpiochip.\n",
  1380. at91_gpio->pioc_idx);
  1381. return ret;
  1382. }
  1383. /* The top level handler handles one bank of GPIOs, except
  1384. * on some SoC it can handle up to three...
  1385. * We only set up the handler for the first of the list.
  1386. */
  1387. gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
  1388. if (!gpiochip_prev) {
  1389. /* Then register the chain on the parent IRQ */
  1390. gpiochip_set_chained_irqchip(&at91_gpio->chip,
  1391. &gpio_irqchip,
  1392. at91_gpio->pioc_virq,
  1393. gpio_irq_handler);
  1394. return 0;
  1395. }
  1396. prev = container_of(gpiochip_prev, struct at91_gpio_chip, chip);
  1397. /* we can only have 2 banks before */
  1398. for (i = 0; i < 2; i++) {
  1399. if (prev->next) {
  1400. prev = prev->next;
  1401. } else {
  1402. prev->next = at91_gpio;
  1403. return 0;
  1404. }
  1405. }
  1406. return -EINVAL;
  1407. }
  1408. /* This structure is replicated for each GPIO block allocated at probe time */
  1409. static struct gpio_chip at91_gpio_template = {
  1410. .request = at91_gpio_request,
  1411. .free = at91_gpio_free,
  1412. .get_direction = at91_gpio_get_direction,
  1413. .direction_input = at91_gpio_direction_input,
  1414. .get = at91_gpio_get,
  1415. .direction_output = at91_gpio_direction_output,
  1416. .set = at91_gpio_set,
  1417. .set_multiple = at91_gpio_set_multiple,
  1418. .dbg_show = at91_gpio_dbg_show,
  1419. .can_sleep = false,
  1420. .ngpio = MAX_NB_GPIO_PER_BANK,
  1421. };
  1422. static const struct of_device_id at91_gpio_of_match[] = {
  1423. { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
  1424. { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
  1425. { /* sentinel */ }
  1426. };
  1427. static int at91_gpio_probe(struct platform_device *pdev)
  1428. {
  1429. struct device_node *np = pdev->dev.of_node;
  1430. struct resource *res;
  1431. struct at91_gpio_chip *at91_chip = NULL;
  1432. struct gpio_chip *chip;
  1433. struct pinctrl_gpio_range *range;
  1434. int ret = 0;
  1435. int irq, i;
  1436. int alias_idx = of_alias_get_id(np, "gpio");
  1437. uint32_t ngpio;
  1438. char **names;
  1439. BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
  1440. if (gpio_chips[alias_idx]) {
  1441. ret = -EBUSY;
  1442. goto err;
  1443. }
  1444. irq = platform_get_irq(pdev, 0);
  1445. if (irq < 0) {
  1446. ret = irq;
  1447. goto err;
  1448. }
  1449. at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
  1450. if (!at91_chip) {
  1451. ret = -ENOMEM;
  1452. goto err;
  1453. }
  1454. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1455. at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
  1456. if (IS_ERR(at91_chip->regbase)) {
  1457. ret = PTR_ERR(at91_chip->regbase);
  1458. goto err;
  1459. }
  1460. at91_chip->ops = (struct at91_pinctrl_mux_ops *)
  1461. of_match_device(at91_gpio_of_match, &pdev->dev)->data;
  1462. at91_chip->pioc_virq = irq;
  1463. at91_chip->pioc_idx = alias_idx;
  1464. at91_chip->clock = devm_clk_get(&pdev->dev, NULL);
  1465. if (IS_ERR(at91_chip->clock)) {
  1466. dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
  1467. ret = PTR_ERR(at91_chip->clock);
  1468. goto err;
  1469. }
  1470. ret = clk_prepare(at91_chip->clock);
  1471. if (ret)
  1472. goto clk_prepare_err;
  1473. /* enable PIO controller's clock */
  1474. ret = clk_enable(at91_chip->clock);
  1475. if (ret) {
  1476. dev_err(&pdev->dev, "failed to enable clock, ignoring.\n");
  1477. goto clk_enable_err;
  1478. }
  1479. at91_chip->chip = at91_gpio_template;
  1480. chip = &at91_chip->chip;
  1481. chip->of_node = np;
  1482. chip->label = dev_name(&pdev->dev);
  1483. chip->dev = &pdev->dev;
  1484. chip->owner = THIS_MODULE;
  1485. chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
  1486. if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
  1487. if (ngpio >= MAX_NB_GPIO_PER_BANK)
  1488. pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
  1489. alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
  1490. else
  1491. chip->ngpio = ngpio;
  1492. }
  1493. names = devm_kzalloc(&pdev->dev, sizeof(char *) * chip->ngpio,
  1494. GFP_KERNEL);
  1495. if (!names) {
  1496. ret = -ENOMEM;
  1497. goto clk_enable_err;
  1498. }
  1499. for (i = 0; i < chip->ngpio; i++)
  1500. names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
  1501. chip->names = (const char *const *)names;
  1502. range = &at91_chip->range;
  1503. range->name = chip->label;
  1504. range->id = alias_idx;
  1505. range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
  1506. range->npins = chip->ngpio;
  1507. range->gc = chip;
  1508. ret = gpiochip_add(chip);
  1509. if (ret)
  1510. goto gpiochip_add_err;
  1511. gpio_chips[alias_idx] = at91_chip;
  1512. gpio_banks = max(gpio_banks, alias_idx + 1);
  1513. ret = at91_gpio_of_irq_setup(pdev, at91_chip);
  1514. if (ret)
  1515. goto irq_setup_err;
  1516. dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
  1517. return 0;
  1518. irq_setup_err:
  1519. gpiochip_remove(chip);
  1520. gpiochip_add_err:
  1521. clk_disable(at91_chip->clock);
  1522. clk_enable_err:
  1523. clk_unprepare(at91_chip->clock);
  1524. clk_prepare_err:
  1525. err:
  1526. dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
  1527. return ret;
  1528. }
  1529. static struct platform_driver at91_gpio_driver = {
  1530. .driver = {
  1531. .name = "gpio-at91",
  1532. .of_match_table = at91_gpio_of_match,
  1533. },
  1534. .probe = at91_gpio_probe,
  1535. };
  1536. static struct platform_driver at91_pinctrl_driver = {
  1537. .driver = {
  1538. .name = "pinctrl-at91",
  1539. .of_match_table = at91_pinctrl_of_match,
  1540. },
  1541. .probe = at91_pinctrl_probe,
  1542. .remove = at91_pinctrl_remove,
  1543. };
  1544. static int __init at91_pinctrl_init(void)
  1545. {
  1546. int ret;
  1547. ret = platform_driver_register(&at91_gpio_driver);
  1548. if (ret)
  1549. return ret;
  1550. return platform_driver_register(&at91_pinctrl_driver);
  1551. }
  1552. arch_initcall(at91_pinctrl_init);
  1553. static void __exit at91_pinctrl_exit(void)
  1554. {
  1555. platform_driver_unregister(&at91_pinctrl_driver);
  1556. }
  1557. module_exit(at91_pinctrl_exit);
  1558. MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
  1559. MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
  1560. MODULE_LICENSE("GPL v2");