pinctrl-at91.c 48 KB

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