pinctrl-at91.c 49 KB

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