pinctrl-at91.c 49 KB

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