pinctrl-at91.c 52 KB

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