pinctrl-at91.c 49 KB

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