pinctrl-at91.c 48 KB

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