pinctrl-at91.c 49 KB

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