pinctrl-at91.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  1. /*
  2. * at91 pinctrl driver based on at91 pinmux core
  3. *
  4. * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
  5. *
  6. * Under GPLv2 only
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/err.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/gpio.h>
  20. #include <linux/pinctrl/machine.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include <linux/pinctrl/pinctrl.h>
  23. #include <linux/pinctrl/pinmux.h>
  24. /* Since we request GPIOs from ourself */
  25. #include <linux/pinctrl/consumer.h>
  26. #include "pinctrl-at91.h"
  27. #include "core.h"
  28. #define MAX_GPIO_BANKS 5
  29. #define MAX_NB_GPIO_PER_BANK 32
  30. struct at91_pinctrl_mux_ops;
  31. struct at91_gpio_chip {
  32. struct gpio_chip chip;
  33. struct pinctrl_gpio_range range;
  34. struct at91_gpio_chip *next; /* Bank sharing same clock */
  35. int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
  36. int pioc_virq; /* PIO bank Linux virtual interrupt */
  37. int pioc_idx; /* PIO bank index */
  38. void __iomem *regbase; /* PIO bank virtual address */
  39. struct clk *clock; /* associated clock */
  40. struct at91_pinctrl_mux_ops *ops; /* ops */
  41. };
  42. #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
  43. static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
  44. static int gpio_banks;
  45. #define PULL_UP (1 << 0)
  46. #define MULTI_DRIVE (1 << 1)
  47. #define DEGLITCH (1 << 2)
  48. #define PULL_DOWN (1 << 3)
  49. #define DIS_SCHMIT (1 << 4)
  50. #define DRIVE_STRENGTH_SHIFT 5
  51. #define DRIVE_STRENGTH_MASK 0x3
  52. #define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
  53. #define DEBOUNCE (1 << 16)
  54. #define DEBOUNCE_VAL_SHIFT 17
  55. #define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
  56. /**
  57. * These defines will translated the dt binding settings to our internal
  58. * settings. They are not necessarily the same value as the register setting.
  59. * The actual drive strength current of low, medium and high must be looked up
  60. * from the corresponding device datasheet. This value is different for pins
  61. * that are even in the same banks. It is also dependent on VCC.
  62. * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
  63. * strength when there is no dt config for it.
  64. */
  65. #define DRIVE_STRENGTH_DEFAULT (0 << DRIVE_STRENGTH_SHIFT)
  66. #define DRIVE_STRENGTH_LOW (1 << DRIVE_STRENGTH_SHIFT)
  67. #define DRIVE_STRENGTH_MED (2 << DRIVE_STRENGTH_SHIFT)
  68. #define DRIVE_STRENGTH_HI (3 << DRIVE_STRENGTH_SHIFT)
  69. /**
  70. * struct at91_pmx_func - describes AT91 pinmux functions
  71. * @name: the name of this specific function
  72. * @groups: corresponding pin groups
  73. * @ngroups: the number of groups
  74. */
  75. struct at91_pmx_func {
  76. const char *name;
  77. const char **groups;
  78. unsigned ngroups;
  79. };
  80. enum at91_mux {
  81. AT91_MUX_GPIO = 0,
  82. AT91_MUX_PERIPH_A = 1,
  83. AT91_MUX_PERIPH_B = 2,
  84. AT91_MUX_PERIPH_C = 3,
  85. AT91_MUX_PERIPH_D = 4,
  86. };
  87. /**
  88. * struct at91_pmx_pin - describes an At91 pin mux
  89. * @bank: the bank of the pin
  90. * @pin: the pin number in the @bank
  91. * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
  92. * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
  93. */
  94. struct at91_pmx_pin {
  95. uint32_t bank;
  96. uint32_t pin;
  97. enum at91_mux mux;
  98. unsigned long conf;
  99. };
  100. /**
  101. * struct at91_pin_group - describes an At91 pin group
  102. * @name: the name of this specific pin group
  103. * @pins_conf: the mux mode for each pin in this group. The size of this
  104. * array is the same as pins.
  105. * @pins: an array of discrete physical pins used in this group, taken
  106. * from the driver-local pin enumeration space
  107. * @npins: the number of pins in this group array, i.e. the number of
  108. * elements in .pins so we can iterate over that array
  109. */
  110. struct at91_pin_group {
  111. const char *name;
  112. struct at91_pmx_pin *pins_conf;
  113. unsigned int *pins;
  114. unsigned npins;
  115. };
  116. /**
  117. * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
  118. * on new IP with support for periph C and D the way to mux in
  119. * periph A and B has changed
  120. * So provide the right call back
  121. * if not present means the IP does not support it
  122. * @get_periph: return the periph mode configured
  123. * @mux_A_periph: mux as periph A
  124. * @mux_B_periph: mux as periph B
  125. * @mux_C_periph: mux as periph C
  126. * @mux_D_periph: mux as periph D
  127. * @get_deglitch: get deglitch status
  128. * @set_deglitch: enable/disable deglitch
  129. * @get_debounce: get debounce status
  130. * @set_debounce: enable/disable debounce
  131. * @get_pulldown: get pulldown status
  132. * @set_pulldown: enable/disable pulldown
  133. * @get_schmitt_trig: get schmitt trigger status
  134. * @disable_schmitt_trig: disable schmitt trigger
  135. * @irq_type: return irq type
  136. */
  137. struct at91_pinctrl_mux_ops {
  138. enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
  139. void (*mux_A_periph)(void __iomem *pio, unsigned mask);
  140. void (*mux_B_periph)(void __iomem *pio, unsigned mask);
  141. void (*mux_C_periph)(void __iomem *pio, unsigned mask);
  142. void (*mux_D_periph)(void __iomem *pio, unsigned mask);
  143. bool (*get_deglitch)(void __iomem *pio, unsigned pin);
  144. void (*set_deglitch)(void __iomem *pio, unsigned mask, bool is_on);
  145. bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
  146. void (*set_debounce)(void __iomem *pio, unsigned mask, bool is_on, u32 div);
  147. bool (*get_pulldown)(void __iomem *pio, unsigned pin);
  148. void (*set_pulldown)(void __iomem *pio, unsigned mask, bool is_on);
  149. bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
  150. void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
  151. unsigned (*get_drivestrength)(void __iomem *pio, unsigned pin);
  152. void (*set_drivestrength)(void __iomem *pio, unsigned pin,
  153. u32 strength);
  154. /* irq */
  155. int (*irq_type)(struct irq_data *d, unsigned type);
  156. };
  157. static int gpio_irq_type(struct irq_data *d, unsigned type);
  158. static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
  159. struct at91_pinctrl {
  160. struct device *dev;
  161. struct pinctrl_dev *pctl;
  162. int nactive_banks;
  163. uint32_t *mux_mask;
  164. int nmux;
  165. struct at91_pmx_func *functions;
  166. int nfunctions;
  167. struct at91_pin_group *groups;
  168. int ngroups;
  169. struct at91_pinctrl_mux_ops *ops;
  170. };
  171. static const inline struct at91_pin_group *at91_pinctrl_find_group_by_name(
  172. const struct at91_pinctrl *info,
  173. const char *name)
  174. {
  175. const struct at91_pin_group *grp = NULL;
  176. int i;
  177. for (i = 0; i < info->ngroups; i++) {
  178. if (strcmp(info->groups[i].name, name))
  179. continue;
  180. grp = &info->groups[i];
  181. dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
  182. break;
  183. }
  184. return grp;
  185. }
  186. static int at91_get_groups_count(struct pinctrl_dev *pctldev)
  187. {
  188. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  189. return info->ngroups;
  190. }
  191. static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
  192. unsigned selector)
  193. {
  194. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  195. return info->groups[selector].name;
  196. }
  197. static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  198. const unsigned **pins,
  199. unsigned *npins)
  200. {
  201. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  202. if (selector >= info->ngroups)
  203. return -EINVAL;
  204. *pins = info->groups[selector].pins;
  205. *npins = info->groups[selector].npins;
  206. return 0;
  207. }
  208. static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  209. unsigned offset)
  210. {
  211. seq_printf(s, "%s", dev_name(pctldev->dev));
  212. }
  213. static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
  214. struct device_node *np,
  215. struct pinctrl_map **map, unsigned *num_maps)
  216. {
  217. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  218. const struct at91_pin_group *grp;
  219. struct pinctrl_map *new_map;
  220. struct device_node *parent;
  221. int map_num = 1;
  222. int i;
  223. /*
  224. * first find the group of this node and check if we need to create
  225. * config maps for pins
  226. */
  227. grp = at91_pinctrl_find_group_by_name(info, np->name);
  228. if (!grp) {
  229. dev_err(info->dev, "unable to find group for node %s\n",
  230. np->name);
  231. return -EINVAL;
  232. }
  233. map_num += grp->npins;
  234. new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, GFP_KERNEL);
  235. if (!new_map)
  236. return -ENOMEM;
  237. *map = new_map;
  238. *num_maps = map_num;
  239. /* create mux map */
  240. parent = of_get_parent(np);
  241. if (!parent) {
  242. devm_kfree(pctldev->dev, new_map);
  243. return -EINVAL;
  244. }
  245. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  246. new_map[0].data.mux.function = parent->name;
  247. new_map[0].data.mux.group = np->name;
  248. of_node_put(parent);
  249. /* create config map */
  250. new_map++;
  251. for (i = 0; i < grp->npins; i++) {
  252. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  253. new_map[i].data.configs.group_or_pin =
  254. pin_get_name(pctldev, grp->pins[i]);
  255. new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
  256. new_map[i].data.configs.num_configs = 1;
  257. }
  258. dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
  259. (*map)->data.mux.function, (*map)->data.mux.group, map_num);
  260. return 0;
  261. }
  262. static void at91_dt_free_map(struct pinctrl_dev *pctldev,
  263. struct pinctrl_map *map, unsigned num_maps)
  264. {
  265. }
  266. static const struct pinctrl_ops at91_pctrl_ops = {
  267. .get_groups_count = at91_get_groups_count,
  268. .get_group_name = at91_get_group_name,
  269. .get_group_pins = at91_get_group_pins,
  270. .pin_dbg_show = at91_pin_dbg_show,
  271. .dt_node_to_map = at91_dt_node_to_map,
  272. .dt_free_map = at91_dt_free_map,
  273. };
  274. static void __iomem *pin_to_controller(struct at91_pinctrl *info,
  275. unsigned int bank)
  276. {
  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. mask = pin_to_mask(pin->pin);
  612. at91_mux_disable_interrupt(pio, mask);
  613. switch (pin->mux) {
  614. case AT91_MUX_GPIO:
  615. at91_mux_gpio_enable(pio, mask, 1);
  616. break;
  617. case AT91_MUX_PERIPH_A:
  618. info->ops->mux_A_periph(pio, mask);
  619. break;
  620. case AT91_MUX_PERIPH_B:
  621. info->ops->mux_B_periph(pio, mask);
  622. break;
  623. case AT91_MUX_PERIPH_C:
  624. if (!info->ops->mux_C_periph)
  625. return -EINVAL;
  626. info->ops->mux_C_periph(pio, mask);
  627. break;
  628. case AT91_MUX_PERIPH_D:
  629. if (!info->ops->mux_D_periph)
  630. return -EINVAL;
  631. info->ops->mux_D_periph(pio, mask);
  632. break;
  633. }
  634. if (pin->mux)
  635. at91_mux_gpio_disable(pio, mask);
  636. }
  637. return 0;
  638. }
  639. static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  640. {
  641. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  642. return info->nfunctions;
  643. }
  644. static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
  645. unsigned selector)
  646. {
  647. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  648. return info->functions[selector].name;
  649. }
  650. static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  651. const char * const **groups,
  652. unsigned * const num_groups)
  653. {
  654. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  655. *groups = info->functions[selector].groups;
  656. *num_groups = info->functions[selector].ngroups;
  657. return 0;
  658. }
  659. static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
  660. struct pinctrl_gpio_range *range,
  661. unsigned offset)
  662. {
  663. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  664. struct at91_gpio_chip *at91_chip;
  665. struct gpio_chip *chip;
  666. unsigned mask;
  667. if (!range) {
  668. dev_err(npct->dev, "invalid range\n");
  669. return -EINVAL;
  670. }
  671. if (!range->gc) {
  672. dev_err(npct->dev, "missing GPIO chip in range\n");
  673. return -EINVAL;
  674. }
  675. chip = range->gc;
  676. at91_chip = container_of(chip, struct at91_gpio_chip, chip);
  677. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  678. mask = 1 << (offset - chip->base);
  679. dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
  680. offset, 'A' + range->id, offset - chip->base, mask);
  681. writel_relaxed(mask, at91_chip->regbase + PIO_PER);
  682. return 0;
  683. }
  684. static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
  685. struct pinctrl_gpio_range *range,
  686. unsigned offset)
  687. {
  688. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  689. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  690. /* Set the pin to some default state, GPIO is usually default */
  691. }
  692. static const struct pinmux_ops at91_pmx_ops = {
  693. .get_functions_count = at91_pmx_get_funcs_count,
  694. .get_function_name = at91_pmx_get_func_name,
  695. .get_function_groups = at91_pmx_get_groups,
  696. .set_mux = at91_pmx_set,
  697. .gpio_request_enable = at91_gpio_request_enable,
  698. .gpio_disable_free = at91_gpio_disable_free,
  699. };
  700. static int at91_pinconf_get(struct pinctrl_dev *pctldev,
  701. unsigned pin_id, unsigned long *config)
  702. {
  703. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  704. void __iomem *pio;
  705. unsigned pin;
  706. int div;
  707. *config = 0;
  708. dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
  709. pio = pin_to_controller(info, pin_to_bank(pin_id));
  710. pin = pin_id % MAX_NB_GPIO_PER_BANK;
  711. if (at91_mux_get_multidrive(pio, pin))
  712. *config |= MULTI_DRIVE;
  713. if (at91_mux_get_pullup(pio, pin))
  714. *config |= PULL_UP;
  715. if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
  716. *config |= DEGLITCH;
  717. if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
  718. *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
  719. if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
  720. *config |= PULL_DOWN;
  721. if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
  722. *config |= DIS_SCHMIT;
  723. if (info->ops->get_drivestrength)
  724. *config |= (info->ops->get_drivestrength(pio, pin)
  725. << DRIVE_STRENGTH_SHIFT);
  726. return 0;
  727. }
  728. static int at91_pinconf_set(struct pinctrl_dev *pctldev,
  729. unsigned pin_id, unsigned long *configs,
  730. unsigned num_configs)
  731. {
  732. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  733. unsigned mask;
  734. void __iomem *pio;
  735. int i;
  736. unsigned long config;
  737. unsigned pin;
  738. for (i = 0; i < num_configs; i++) {
  739. config = configs[i];
  740. dev_dbg(info->dev,
  741. "%s:%d, pin_id=%d, config=0x%lx",
  742. __func__, __LINE__, pin_id, config);
  743. pio = pin_to_controller(info, pin_to_bank(pin_id));
  744. pin = pin_id % MAX_NB_GPIO_PER_BANK;
  745. mask = pin_to_mask(pin);
  746. if (config & PULL_UP && config & PULL_DOWN)
  747. return -EINVAL;
  748. at91_mux_set_pullup(pio, mask, config & PULL_UP);
  749. at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
  750. if (info->ops->set_deglitch)
  751. info->ops->set_deglitch(pio, mask, config & DEGLITCH);
  752. if (info->ops->set_debounce)
  753. info->ops->set_debounce(pio, mask, config & DEBOUNCE,
  754. (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
  755. if (info->ops->set_pulldown)
  756. info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
  757. if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
  758. info->ops->disable_schmitt_trig(pio, mask);
  759. if (info->ops->set_drivestrength)
  760. info->ops->set_drivestrength(pio, pin,
  761. (config & DRIVE_STRENGTH)
  762. >> DRIVE_STRENGTH_SHIFT);
  763. } /* for each config */
  764. return 0;
  765. }
  766. #define DBG_SHOW_FLAG(flag) do { \
  767. if (config & flag) { \
  768. if (num_conf) \
  769. seq_puts(s, "|"); \
  770. seq_puts(s, #flag); \
  771. num_conf++; \
  772. } \
  773. } while (0)
  774. #define DBG_SHOW_FLAG_MASKED(mask,flag) do { \
  775. if ((config & mask) == flag) { \
  776. if (num_conf) \
  777. seq_puts(s, "|"); \
  778. seq_puts(s, #flag); \
  779. num_conf++; \
  780. } \
  781. } while (0)
  782. static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  783. struct seq_file *s, unsigned pin_id)
  784. {
  785. unsigned long config;
  786. int val, num_conf = 0;
  787. at91_pinconf_get(pctldev, pin_id, &config);
  788. DBG_SHOW_FLAG(MULTI_DRIVE);
  789. DBG_SHOW_FLAG(PULL_UP);
  790. DBG_SHOW_FLAG(PULL_DOWN);
  791. DBG_SHOW_FLAG(DIS_SCHMIT);
  792. DBG_SHOW_FLAG(DEGLITCH);
  793. DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_LOW);
  794. DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_MED);
  795. DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_HI);
  796. DBG_SHOW_FLAG(DEBOUNCE);
  797. if (config & DEBOUNCE) {
  798. val = config >> DEBOUNCE_VAL_SHIFT;
  799. seq_printf(s, "(%d)", val);
  800. }
  801. return;
  802. }
  803. static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  804. struct seq_file *s, unsigned group)
  805. {
  806. }
  807. static const struct pinconf_ops at91_pinconf_ops = {
  808. .pin_config_get = at91_pinconf_get,
  809. .pin_config_set = at91_pinconf_set,
  810. .pin_config_dbg_show = at91_pinconf_dbg_show,
  811. .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
  812. };
  813. static struct pinctrl_desc at91_pinctrl_desc = {
  814. .pctlops = &at91_pctrl_ops,
  815. .pmxops = &at91_pmx_ops,
  816. .confops = &at91_pinconf_ops,
  817. .owner = THIS_MODULE,
  818. };
  819. static const char *gpio_compat = "atmel,at91rm9200-gpio";
  820. static void at91_pinctrl_child_count(struct at91_pinctrl *info,
  821. struct device_node *np)
  822. {
  823. struct device_node *child;
  824. for_each_child_of_node(np, child) {
  825. if (of_device_is_compatible(child, gpio_compat)) {
  826. if (of_device_is_available(child))
  827. info->nactive_banks++;
  828. } else {
  829. info->nfunctions++;
  830. info->ngroups += of_get_child_count(child);
  831. }
  832. }
  833. }
  834. static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
  835. struct device_node *np)
  836. {
  837. int ret = 0;
  838. int size;
  839. const __be32 *list;
  840. list = of_get_property(np, "atmel,mux-mask", &size);
  841. if (!list) {
  842. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  843. return -EINVAL;
  844. }
  845. size /= sizeof(*list);
  846. if (!size || size % gpio_banks) {
  847. dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
  848. return -EINVAL;
  849. }
  850. info->nmux = size / gpio_banks;
  851. info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
  852. if (!info->mux_mask) {
  853. dev_err(info->dev, "could not alloc mux_mask\n");
  854. return -ENOMEM;
  855. }
  856. ret = of_property_read_u32_array(np, "atmel,mux-mask",
  857. info->mux_mask, size);
  858. if (ret)
  859. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  860. return ret;
  861. }
  862. static int at91_pinctrl_parse_groups(struct device_node *np,
  863. struct at91_pin_group *grp,
  864. struct at91_pinctrl *info, u32 index)
  865. {
  866. struct at91_pmx_pin *pin;
  867. int size;
  868. const __be32 *list;
  869. int i, j;
  870. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  871. /* Initialise group */
  872. grp->name = np->name;
  873. /*
  874. * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
  875. * do sanity check and calculate pins number
  876. */
  877. list = of_get_property(np, "atmel,pins", &size);
  878. /* we do not check return since it's safe node passed down */
  879. size /= sizeof(*list);
  880. if (!size || size % 4) {
  881. dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
  882. return -EINVAL;
  883. }
  884. grp->npins = size / 4;
  885. pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct at91_pmx_pin),
  886. GFP_KERNEL);
  887. grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
  888. GFP_KERNEL);
  889. if (!grp->pins_conf || !grp->pins)
  890. return -ENOMEM;
  891. for (i = 0, j = 0; i < size; i += 4, j++) {
  892. pin->bank = be32_to_cpu(*list++);
  893. pin->pin = be32_to_cpu(*list++);
  894. grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
  895. pin->mux = be32_to_cpu(*list++);
  896. pin->conf = be32_to_cpu(*list++);
  897. at91_pin_dbg(info->dev, pin);
  898. pin++;
  899. }
  900. return 0;
  901. }
  902. static int at91_pinctrl_parse_functions(struct device_node *np,
  903. struct at91_pinctrl *info, u32 index)
  904. {
  905. struct device_node *child;
  906. struct at91_pmx_func *func;
  907. struct at91_pin_group *grp;
  908. int ret;
  909. static u32 grp_index;
  910. u32 i = 0;
  911. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  912. func = &info->functions[index];
  913. /* Initialise function */
  914. func->name = np->name;
  915. func->ngroups = of_get_child_count(np);
  916. if (func->ngroups == 0) {
  917. dev_err(info->dev, "no groups defined\n");
  918. return -EINVAL;
  919. }
  920. func->groups = devm_kzalloc(info->dev,
  921. func->ngroups * sizeof(char *), GFP_KERNEL);
  922. if (!func->groups)
  923. return -ENOMEM;
  924. for_each_child_of_node(np, child) {
  925. func->groups[i] = child->name;
  926. grp = &info->groups[grp_index++];
  927. ret = at91_pinctrl_parse_groups(child, grp, info, i++);
  928. if (ret)
  929. return ret;
  930. }
  931. return 0;
  932. }
  933. static const struct of_device_id at91_pinctrl_of_match[] = {
  934. { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
  935. { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
  936. { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
  937. { /* sentinel */ }
  938. };
  939. static int at91_pinctrl_probe_dt(struct platform_device *pdev,
  940. struct at91_pinctrl *info)
  941. {
  942. int ret = 0;
  943. int i, j;
  944. uint32_t *tmp;
  945. struct device_node *np = pdev->dev.of_node;
  946. struct device_node *child;
  947. if (!np)
  948. return -ENODEV;
  949. info->dev = &pdev->dev;
  950. info->ops = (struct at91_pinctrl_mux_ops *)
  951. of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
  952. at91_pinctrl_child_count(info, np);
  953. if (gpio_banks < 1) {
  954. dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
  955. return -EINVAL;
  956. }
  957. ret = at91_pinctrl_mux_mask(info, np);
  958. if (ret)
  959. return ret;
  960. dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
  961. dev_dbg(&pdev->dev, "mux-mask\n");
  962. tmp = info->mux_mask;
  963. for (i = 0; i < gpio_banks; i++) {
  964. for (j = 0; j < info->nmux; j++, tmp++) {
  965. dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
  966. }
  967. }
  968. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  969. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  970. info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct at91_pmx_func),
  971. GFP_KERNEL);
  972. if (!info->functions)
  973. return -ENOMEM;
  974. info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct at91_pin_group),
  975. GFP_KERNEL);
  976. if (!info->groups)
  977. return -ENOMEM;
  978. dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
  979. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  980. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  981. i = 0;
  982. for_each_child_of_node(np, child) {
  983. if (of_device_is_compatible(child, gpio_compat))
  984. continue;
  985. ret = at91_pinctrl_parse_functions(child, info, i++);
  986. if (ret) {
  987. dev_err(&pdev->dev, "failed to parse function\n");
  988. return ret;
  989. }
  990. }
  991. return 0;
  992. }
  993. static int at91_pinctrl_probe(struct platform_device *pdev)
  994. {
  995. struct at91_pinctrl *info;
  996. struct pinctrl_pin_desc *pdesc;
  997. int ret, i, j, k, ngpio_chips_enabled = 0;
  998. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  999. if (!info)
  1000. return -ENOMEM;
  1001. ret = at91_pinctrl_probe_dt(pdev, info);
  1002. if (ret)
  1003. return ret;
  1004. /*
  1005. * We need all the GPIO drivers to probe FIRST, or we will not be able
  1006. * to obtain references to the struct gpio_chip * for them, and we
  1007. * need this to proceed.
  1008. */
  1009. for (i = 0; i < gpio_banks; i++)
  1010. if (gpio_chips[i])
  1011. ngpio_chips_enabled++;
  1012. if (ngpio_chips_enabled < info->nactive_banks) {
  1013. dev_warn(&pdev->dev,
  1014. "All GPIO chips are not registered yet (%d/%d)\n",
  1015. ngpio_chips_enabled, info->nactive_banks);
  1016. devm_kfree(&pdev->dev, info);
  1017. return -EPROBE_DEFER;
  1018. }
  1019. at91_pinctrl_desc.name = dev_name(&pdev->dev);
  1020. at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
  1021. at91_pinctrl_desc.pins = pdesc =
  1022. devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL);
  1023. if (!at91_pinctrl_desc.pins)
  1024. return -ENOMEM;
  1025. for (i = 0, k = 0; i < gpio_banks; i++) {
  1026. for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
  1027. pdesc->number = k;
  1028. pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
  1029. pdesc++;
  1030. }
  1031. }
  1032. platform_set_drvdata(pdev, info);
  1033. info->pctl = pinctrl_register(&at91_pinctrl_desc, &pdev->dev, info);
  1034. if (!info->pctl) {
  1035. dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
  1036. return -EINVAL;
  1037. }
  1038. /* We will handle a range of GPIO pins */
  1039. for (i = 0; i < gpio_banks; i++)
  1040. if (gpio_chips[i])
  1041. pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
  1042. dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
  1043. return 0;
  1044. }
  1045. static int at91_pinctrl_remove(struct platform_device *pdev)
  1046. {
  1047. struct at91_pinctrl *info = platform_get_drvdata(pdev);
  1048. pinctrl_unregister(info->pctl);
  1049. return 0;
  1050. }
  1051. static int at91_gpio_request(struct gpio_chip *chip, unsigned offset)
  1052. {
  1053. /*
  1054. * Map back to global GPIO space and request muxing, the direction
  1055. * parameter does not matter for this controller.
  1056. */
  1057. int gpio = chip->base + offset;
  1058. int bank = chip->base / chip->ngpio;
  1059. dev_dbg(chip->dev, "%s:%d pio%c%d(%d)\n", __func__, __LINE__,
  1060. 'A' + bank, offset, gpio);
  1061. return pinctrl_request_gpio(gpio);
  1062. }
  1063. static void at91_gpio_free(struct gpio_chip *chip, unsigned offset)
  1064. {
  1065. int gpio = chip->base + offset;
  1066. pinctrl_free_gpio(gpio);
  1067. }
  1068. static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  1069. {
  1070. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1071. void __iomem *pio = at91_gpio->regbase;
  1072. unsigned mask = 1 << offset;
  1073. u32 osr;
  1074. osr = readl_relaxed(pio + PIO_OSR);
  1075. return !(osr & mask);
  1076. }
  1077. static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  1078. {
  1079. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1080. void __iomem *pio = at91_gpio->regbase;
  1081. unsigned mask = 1 << offset;
  1082. writel_relaxed(mask, pio + PIO_ODR);
  1083. return 0;
  1084. }
  1085. static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
  1086. {
  1087. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1088. void __iomem *pio = at91_gpio->regbase;
  1089. unsigned mask = 1 << offset;
  1090. u32 pdsr;
  1091. pdsr = readl_relaxed(pio + PIO_PDSR);
  1092. return (pdsr & mask) != 0;
  1093. }
  1094. static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
  1095. int val)
  1096. {
  1097. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1098. void __iomem *pio = at91_gpio->regbase;
  1099. unsigned mask = 1 << offset;
  1100. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  1101. }
  1102. static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  1103. int val)
  1104. {
  1105. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1106. void __iomem *pio = at91_gpio->regbase;
  1107. unsigned mask = 1 << offset;
  1108. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  1109. writel_relaxed(mask, pio + PIO_OER);
  1110. return 0;
  1111. }
  1112. #ifdef CONFIG_DEBUG_FS
  1113. static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  1114. {
  1115. enum at91_mux mode;
  1116. int i;
  1117. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  1118. void __iomem *pio = at91_gpio->regbase;
  1119. for (i = 0; i < chip->ngpio; i++) {
  1120. unsigned mask = pin_to_mask(i);
  1121. const char *gpio_label;
  1122. gpio_label = gpiochip_is_requested(chip, i);
  1123. if (!gpio_label)
  1124. continue;
  1125. mode = at91_gpio->ops->get_periph(pio, mask);
  1126. seq_printf(s, "[%s] GPIO%s%d: ",
  1127. gpio_label, chip->label, i);
  1128. if (mode == AT91_MUX_GPIO) {
  1129. seq_printf(s, "[gpio] ");
  1130. seq_printf(s, "%s ",
  1131. readl_relaxed(pio + PIO_OSR) & mask ?
  1132. "output" : "input");
  1133. seq_printf(s, "%s\n",
  1134. readl_relaxed(pio + PIO_PDSR) & mask ?
  1135. "set" : "clear");
  1136. } else {
  1137. seq_printf(s, "[periph %c]\n",
  1138. mode + 'A' - 1);
  1139. }
  1140. }
  1141. }
  1142. #else
  1143. #define at91_gpio_dbg_show NULL
  1144. #endif
  1145. /* Several AIC controller irqs are dispatched through this GPIO handler.
  1146. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  1147. * at91_set_gpio_input() then maybe enable its glitch filter.
  1148. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  1149. * handler.
  1150. * First implementation always triggers on rising and falling edges
  1151. * whereas the newer PIO3 can be additionally configured to trigger on
  1152. * level, edge with any polarity.
  1153. *
  1154. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  1155. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  1156. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  1157. */
  1158. static void gpio_irq_mask(struct irq_data *d)
  1159. {
  1160. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1161. void __iomem *pio = at91_gpio->regbase;
  1162. unsigned mask = 1 << d->hwirq;
  1163. if (pio)
  1164. writel_relaxed(mask, pio + PIO_IDR);
  1165. }
  1166. static void gpio_irq_unmask(struct irq_data *d)
  1167. {
  1168. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1169. void __iomem *pio = at91_gpio->regbase;
  1170. unsigned mask = 1 << d->hwirq;
  1171. if (pio)
  1172. writel_relaxed(mask, pio + PIO_IER);
  1173. }
  1174. static int gpio_irq_type(struct irq_data *d, unsigned type)
  1175. {
  1176. switch (type) {
  1177. case IRQ_TYPE_NONE:
  1178. case IRQ_TYPE_EDGE_BOTH:
  1179. return 0;
  1180. default:
  1181. return -EINVAL;
  1182. }
  1183. }
  1184. /* Alternate irq type for PIO3 support */
  1185. static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
  1186. {
  1187. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1188. void __iomem *pio = at91_gpio->regbase;
  1189. unsigned mask = 1 << d->hwirq;
  1190. switch (type) {
  1191. case IRQ_TYPE_EDGE_RISING:
  1192. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1193. writel_relaxed(mask, pio + PIO_ESR);
  1194. writel_relaxed(mask, pio + PIO_REHLSR);
  1195. break;
  1196. case IRQ_TYPE_EDGE_FALLING:
  1197. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1198. writel_relaxed(mask, pio + PIO_ESR);
  1199. writel_relaxed(mask, pio + PIO_FELLSR);
  1200. break;
  1201. case IRQ_TYPE_LEVEL_LOW:
  1202. __irq_set_handler_locked(d->irq, handle_level_irq);
  1203. writel_relaxed(mask, pio + PIO_LSR);
  1204. writel_relaxed(mask, pio + PIO_FELLSR);
  1205. break;
  1206. case IRQ_TYPE_LEVEL_HIGH:
  1207. __irq_set_handler_locked(d->irq, handle_level_irq);
  1208. writel_relaxed(mask, pio + PIO_LSR);
  1209. writel_relaxed(mask, pio + PIO_REHLSR);
  1210. break;
  1211. case IRQ_TYPE_EDGE_BOTH:
  1212. /*
  1213. * disable additional interrupt modes:
  1214. * fall back to default behavior
  1215. */
  1216. __irq_set_handler_locked(d->irq, handle_simple_irq);
  1217. writel_relaxed(mask, pio + PIO_AIMDR);
  1218. return 0;
  1219. case IRQ_TYPE_NONE:
  1220. default:
  1221. pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
  1222. return -EINVAL;
  1223. }
  1224. /* enable additional interrupt modes */
  1225. writel_relaxed(mask, pio + PIO_AIMER);
  1226. return 0;
  1227. }
  1228. static void gpio_irq_ack(struct irq_data *d)
  1229. {
  1230. /* the interrupt is already cleared before by reading ISR */
  1231. }
  1232. static int gpio_irq_request_res(struct irq_data *d)
  1233. {
  1234. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1235. unsigned pin = d->hwirq;
  1236. int ret;
  1237. ret = gpiochip_lock_as_irq(&at91_gpio->chip, pin);
  1238. if (ret)
  1239. dev_err(at91_gpio->chip.dev, "unable to lock pind %lu IRQ\n",
  1240. d->hwirq);
  1241. return ret;
  1242. }
  1243. static void gpio_irq_release_res(struct irq_data *d)
  1244. {
  1245. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1246. unsigned pin = d->hwirq;
  1247. gpiochip_unlock_as_irq(&at91_gpio->chip, pin);
  1248. }
  1249. #ifdef CONFIG_PM
  1250. static u32 wakeups[MAX_GPIO_BANKS];
  1251. static u32 backups[MAX_GPIO_BANKS];
  1252. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  1253. {
  1254. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  1255. unsigned bank = at91_gpio->pioc_idx;
  1256. unsigned mask = 1 << d->hwirq;
  1257. if (unlikely(bank >= MAX_GPIO_BANKS))
  1258. return -EINVAL;
  1259. if (state)
  1260. wakeups[bank] |= mask;
  1261. else
  1262. wakeups[bank] &= ~mask;
  1263. irq_set_irq_wake(at91_gpio->pioc_virq, state);
  1264. return 0;
  1265. }
  1266. void at91_pinctrl_gpio_suspend(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. backups[i] = readl_relaxed(pio + PIO_IMR);
  1275. writel_relaxed(backups[i], pio + PIO_IDR);
  1276. writel_relaxed(wakeups[i], pio + PIO_IER);
  1277. if (!wakeups[i])
  1278. clk_disable_unprepare(gpio_chips[i]->clock);
  1279. else
  1280. printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
  1281. 'A'+i, wakeups[i]);
  1282. }
  1283. }
  1284. void at91_pinctrl_gpio_resume(void)
  1285. {
  1286. int i;
  1287. for (i = 0; i < gpio_banks; i++) {
  1288. void __iomem *pio;
  1289. if (!gpio_chips[i])
  1290. continue;
  1291. pio = gpio_chips[i]->regbase;
  1292. if (!wakeups[i])
  1293. clk_prepare_enable(gpio_chips[i]->clock);
  1294. writel_relaxed(wakeups[i], pio + PIO_IDR);
  1295. writel_relaxed(backups[i], pio + PIO_IER);
  1296. }
  1297. }
  1298. #else
  1299. #define gpio_irq_set_wake NULL
  1300. #endif /* CONFIG_PM */
  1301. static struct irq_chip gpio_irqchip = {
  1302. .name = "GPIO",
  1303. .irq_ack = gpio_irq_ack,
  1304. .irq_request_resources = gpio_irq_request_res,
  1305. .irq_release_resources = gpio_irq_release_res,
  1306. .irq_disable = gpio_irq_mask,
  1307. .irq_mask = gpio_irq_mask,
  1308. .irq_unmask = gpio_irq_unmask,
  1309. /* .irq_set_type is set dynamically */
  1310. .irq_set_wake = gpio_irq_set_wake,
  1311. };
  1312. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  1313. {
  1314. struct irq_chip *chip = irq_get_chip(irq);
  1315. struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
  1316. struct at91_gpio_chip *at91_gpio = container_of(gpio_chip,
  1317. struct at91_gpio_chip, chip);
  1318. void __iomem *pio = at91_gpio->regbase;
  1319. unsigned long isr;
  1320. int n;
  1321. chained_irq_enter(chip, desc);
  1322. for (;;) {
  1323. /* Reading ISR acks pending (edge triggered) GPIO interrupts.
  1324. * When there are none pending, we're finished unless we need
  1325. * to process multiple banks (like ID_PIOCDE on sam9263).
  1326. */
  1327. isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
  1328. if (!isr) {
  1329. if (!at91_gpio->next)
  1330. break;
  1331. at91_gpio = at91_gpio->next;
  1332. pio = at91_gpio->regbase;
  1333. gpio_chip = &at91_gpio->chip;
  1334. continue;
  1335. }
  1336. for_each_set_bit(n, &isr, BITS_PER_LONG) {
  1337. generic_handle_irq(irq_find_mapping(
  1338. gpio_chip->irqdomain, n));
  1339. }
  1340. }
  1341. chained_irq_exit(chip, desc);
  1342. /* now it may re-trigger */
  1343. }
  1344. static int at91_gpio_of_irq_setup(struct platform_device *pdev,
  1345. struct at91_gpio_chip *at91_gpio)
  1346. {
  1347. struct gpio_chip *gpiochip_prev = NULL;
  1348. struct at91_gpio_chip *prev = NULL;
  1349. struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
  1350. int ret, i;
  1351. at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
  1352. /* Setup proper .irq_set_type function */
  1353. gpio_irqchip.irq_set_type = at91_gpio->ops->irq_type;
  1354. /* Disable irqs of this PIO controller */
  1355. writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
  1356. /*
  1357. * Let the generic code handle this edge IRQ, the the chained
  1358. * handler will perform the actual work of handling the parent
  1359. * interrupt.
  1360. */
  1361. ret = gpiochip_irqchip_add(&at91_gpio->chip,
  1362. &gpio_irqchip,
  1363. 0,
  1364. handle_edge_irq,
  1365. IRQ_TYPE_EDGE_BOTH);
  1366. if (ret) {
  1367. dev_err(&pdev->dev, "at91_gpio.%d: Couldn't add irqchip to gpiochip.\n",
  1368. at91_gpio->pioc_idx);
  1369. return ret;
  1370. }
  1371. /* The top level handler handles one bank of GPIOs, except
  1372. * on some SoC it can handle up to three...
  1373. * We only set up the handler for the first of the list.
  1374. */
  1375. gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
  1376. if (!gpiochip_prev) {
  1377. /* Then register the chain on the parent IRQ */
  1378. gpiochip_set_chained_irqchip(&at91_gpio->chip,
  1379. &gpio_irqchip,
  1380. at91_gpio->pioc_virq,
  1381. gpio_irq_handler);
  1382. return 0;
  1383. }
  1384. prev = container_of(gpiochip_prev, struct at91_gpio_chip, chip);
  1385. /* we can only have 2 banks before */
  1386. for (i = 0; i < 2; i++) {
  1387. if (prev->next) {
  1388. prev = prev->next;
  1389. } else {
  1390. prev->next = at91_gpio;
  1391. return 0;
  1392. }
  1393. }
  1394. return -EINVAL;
  1395. }
  1396. /* This structure is replicated for each GPIO block allocated at probe time */
  1397. static struct gpio_chip at91_gpio_template = {
  1398. .request = at91_gpio_request,
  1399. .free = at91_gpio_free,
  1400. .get_direction = at91_gpio_get_direction,
  1401. .direction_input = at91_gpio_direction_input,
  1402. .get = at91_gpio_get,
  1403. .direction_output = at91_gpio_direction_output,
  1404. .set = at91_gpio_set,
  1405. .dbg_show = at91_gpio_dbg_show,
  1406. .can_sleep = false,
  1407. .ngpio = MAX_NB_GPIO_PER_BANK,
  1408. };
  1409. static const struct of_device_id at91_gpio_of_match[] = {
  1410. { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
  1411. { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
  1412. { /* sentinel */ }
  1413. };
  1414. static int at91_gpio_probe(struct platform_device *pdev)
  1415. {
  1416. struct device_node *np = pdev->dev.of_node;
  1417. struct resource *res;
  1418. struct at91_gpio_chip *at91_chip = NULL;
  1419. struct gpio_chip *chip;
  1420. struct pinctrl_gpio_range *range;
  1421. int ret = 0;
  1422. int irq, i;
  1423. int alias_idx = of_alias_get_id(np, "gpio");
  1424. uint32_t ngpio;
  1425. char **names;
  1426. BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
  1427. if (gpio_chips[alias_idx]) {
  1428. ret = -EBUSY;
  1429. goto err;
  1430. }
  1431. irq = platform_get_irq(pdev, 0);
  1432. if (irq < 0) {
  1433. ret = irq;
  1434. goto err;
  1435. }
  1436. at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
  1437. if (!at91_chip) {
  1438. ret = -ENOMEM;
  1439. goto err;
  1440. }
  1441. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1442. at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
  1443. if (IS_ERR(at91_chip->regbase)) {
  1444. ret = PTR_ERR(at91_chip->regbase);
  1445. goto err;
  1446. }
  1447. at91_chip->ops = (struct at91_pinctrl_mux_ops *)
  1448. of_match_device(at91_gpio_of_match, &pdev->dev)->data;
  1449. at91_chip->pioc_virq = irq;
  1450. at91_chip->pioc_idx = alias_idx;
  1451. at91_chip->clock = devm_clk_get(&pdev->dev, NULL);
  1452. if (IS_ERR(at91_chip->clock)) {
  1453. dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
  1454. ret = PTR_ERR(at91_chip->clock);
  1455. goto err;
  1456. }
  1457. ret = clk_prepare(at91_chip->clock);
  1458. if (ret)
  1459. goto clk_prepare_err;
  1460. /* enable PIO controller's clock */
  1461. ret = clk_enable(at91_chip->clock);
  1462. if (ret) {
  1463. dev_err(&pdev->dev, "failed to enable clock, ignoring.\n");
  1464. goto clk_enable_err;
  1465. }
  1466. at91_chip->chip = at91_gpio_template;
  1467. chip = &at91_chip->chip;
  1468. chip->of_node = np;
  1469. chip->label = dev_name(&pdev->dev);
  1470. chip->dev = &pdev->dev;
  1471. chip->owner = THIS_MODULE;
  1472. chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
  1473. if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
  1474. if (ngpio >= MAX_NB_GPIO_PER_BANK)
  1475. pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
  1476. alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
  1477. else
  1478. chip->ngpio = ngpio;
  1479. }
  1480. names = devm_kzalloc(&pdev->dev, sizeof(char *) * chip->ngpio,
  1481. GFP_KERNEL);
  1482. if (!names) {
  1483. ret = -ENOMEM;
  1484. goto clk_enable_err;
  1485. }
  1486. for (i = 0; i < chip->ngpio; i++)
  1487. names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
  1488. chip->names = (const char *const *)names;
  1489. range = &at91_chip->range;
  1490. range->name = chip->label;
  1491. range->id = alias_idx;
  1492. range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
  1493. range->npins = chip->ngpio;
  1494. range->gc = chip;
  1495. ret = gpiochip_add(chip);
  1496. if (ret)
  1497. goto gpiochip_add_err;
  1498. gpio_chips[alias_idx] = at91_chip;
  1499. gpio_banks = max(gpio_banks, alias_idx + 1);
  1500. ret = at91_gpio_of_irq_setup(pdev, at91_chip);
  1501. if (ret)
  1502. goto irq_setup_err;
  1503. dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
  1504. return 0;
  1505. irq_setup_err:
  1506. gpiochip_remove(chip);
  1507. gpiochip_add_err:
  1508. clk_disable(at91_chip->clock);
  1509. clk_enable_err:
  1510. clk_unprepare(at91_chip->clock);
  1511. clk_prepare_err:
  1512. err:
  1513. dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
  1514. return ret;
  1515. }
  1516. static struct platform_driver at91_gpio_driver = {
  1517. .driver = {
  1518. .name = "gpio-at91",
  1519. .of_match_table = at91_gpio_of_match,
  1520. },
  1521. .probe = at91_gpio_probe,
  1522. };
  1523. static struct platform_driver at91_pinctrl_driver = {
  1524. .driver = {
  1525. .name = "pinctrl-at91",
  1526. .of_match_table = at91_pinctrl_of_match,
  1527. },
  1528. .probe = at91_pinctrl_probe,
  1529. .remove = at91_pinctrl_remove,
  1530. };
  1531. static int __init at91_pinctrl_init(void)
  1532. {
  1533. int ret;
  1534. ret = platform_driver_register(&at91_gpio_driver);
  1535. if (ret)
  1536. return ret;
  1537. return platform_driver_register(&at91_pinctrl_driver);
  1538. }
  1539. arch_initcall(at91_pinctrl_init);
  1540. static void __exit at91_pinctrl_exit(void)
  1541. {
  1542. platform_driver_unregister(&at91_pinctrl_driver);
  1543. }
  1544. module_exit(at91_pinctrl_exit);
  1545. MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
  1546. MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
  1547. MODULE_LICENSE("GPL v2");