pinctrl-at91.c 52 KB

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