pinctrl-at91.c 48 KB

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