pinctrl-at91.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  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/irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/io.h>
  21. #include <linux/gpio.h>
  22. #include <linux/pinctrl/machine.h>
  23. #include <linux/pinctrl/pinconf.h>
  24. #include <linux/pinctrl/pinctrl.h>
  25. #include <linux/pinctrl/pinmux.h>
  26. /* Since we request GPIOs from ourself */
  27. #include <linux/pinctrl/consumer.h>
  28. #include <asm/mach/irq.h>
  29. #include <mach/hardware.h>
  30. #include <mach/at91_pio.h>
  31. #include "core.h"
  32. #define MAX_NB_GPIO_PER_BANK 32
  33. struct at91_pinctrl_mux_ops;
  34. struct at91_gpio_chip {
  35. struct gpio_chip chip;
  36. struct pinctrl_gpio_range range;
  37. struct at91_gpio_chip *next; /* Bank sharing same clock */
  38. int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
  39. int pioc_virq; /* PIO bank Linux virtual interrupt */
  40. int pioc_idx; /* PIO bank index */
  41. void __iomem *regbase; /* PIO bank virtual address */
  42. struct clk *clock; /* associated clock */
  43. struct irq_domain *domain; /* associated irq domain */
  44. struct at91_pinctrl_mux_ops *ops; /* ops */
  45. };
  46. #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
  47. static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
  48. static int gpio_banks;
  49. #define PULL_UP (1 << 0)
  50. #define MULTI_DRIVE (1 << 1)
  51. /**
  52. * struct at91_pmx_func - describes AT91 pinmux functions
  53. * @name: the name of this specific function
  54. * @groups: corresponding pin groups
  55. * @ngroups: the number of groups
  56. */
  57. struct at91_pmx_func {
  58. const char *name;
  59. const char **groups;
  60. unsigned ngroups;
  61. };
  62. enum at91_mux {
  63. AT91_MUX_GPIO = 0,
  64. AT91_MUX_PERIPH_A = 1,
  65. AT91_MUX_PERIPH_B = 2,
  66. AT91_MUX_PERIPH_C = 3,
  67. AT91_MUX_PERIPH_D = 4,
  68. };
  69. /**
  70. * struct at91_pmx_pin - describes an At91 pin mux
  71. * @bank: the bank of the pin
  72. * @pin: the pin number in the @bank
  73. * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
  74. * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
  75. */
  76. struct at91_pmx_pin {
  77. uint32_t bank;
  78. uint32_t pin;
  79. enum at91_mux mux;
  80. unsigned long conf;
  81. };
  82. /**
  83. * struct at91_pin_group - describes an At91 pin group
  84. * @name: the name of this specific pin group
  85. * @pins_conf: the mux mode for each pin in this group. The size of this
  86. * array is the same as pins.
  87. * @pins: an array of discrete physical pins used in this group, taken
  88. * from the driver-local pin enumeration space
  89. * @npins: the number of pins in this group array, i.e. the number of
  90. * elements in .pins so we can iterate over that array
  91. */
  92. struct at91_pin_group {
  93. const char *name;
  94. struct at91_pmx_pin *pins_conf;
  95. unsigned int *pins;
  96. unsigned npins;
  97. };
  98. /**
  99. * struct at91_pinctrl_mux_ops - describes an At91 mux ops group
  100. * on new IP with support for periph C and D the way to mux in
  101. * periph A and B has changed
  102. * So provide the right call back
  103. * if not present means the IP does not support it
  104. * @get_periph: return the periph mode configured
  105. * @mux_A_periph: mux as periph A
  106. * @mux_B_periph: mux as periph B
  107. * @mux_C_periph: mux as periph C
  108. * @mux_D_periph: mux as periph D
  109. * @irq_type: return irq type
  110. */
  111. struct at91_pinctrl_mux_ops {
  112. enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
  113. void (*mux_A_periph)(void __iomem *pio, unsigned mask);
  114. void (*mux_B_periph)(void __iomem *pio, unsigned mask);
  115. void (*mux_C_periph)(void __iomem *pio, unsigned mask);
  116. void (*mux_D_periph)(void __iomem *pio, unsigned mask);
  117. /* irq */
  118. int (*irq_type)(struct irq_data *d, unsigned type);
  119. };
  120. static int gpio_irq_type(struct irq_data *d, unsigned type);
  121. static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
  122. struct at91_pinctrl {
  123. struct device *dev;
  124. struct pinctrl_dev *pctl;
  125. int nbanks;
  126. uint32_t *mux_mask;
  127. int nmux;
  128. struct at91_pmx_func *functions;
  129. int nfunctions;
  130. struct at91_pin_group *groups;
  131. int ngroups;
  132. struct at91_pinctrl_mux_ops *ops;
  133. };
  134. static const inline struct at91_pin_group *at91_pinctrl_find_group_by_name(
  135. const struct at91_pinctrl *info,
  136. const char *name)
  137. {
  138. const struct at91_pin_group *grp = NULL;
  139. int i;
  140. for (i = 0; i < info->ngroups; i++) {
  141. if (strcmp(info->groups[i].name, name))
  142. continue;
  143. grp = &info->groups[i];
  144. dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
  145. break;
  146. }
  147. return grp;
  148. }
  149. static int at91_get_groups_count(struct pinctrl_dev *pctldev)
  150. {
  151. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  152. return info->ngroups;
  153. }
  154. static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
  155. unsigned selector)
  156. {
  157. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  158. return info->groups[selector].name;
  159. }
  160. static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
  161. const unsigned **pins,
  162. unsigned *npins)
  163. {
  164. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  165. if (selector >= info->ngroups)
  166. return -EINVAL;
  167. *pins = info->groups[selector].pins;
  168. *npins = info->groups[selector].npins;
  169. return 0;
  170. }
  171. static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
  172. unsigned offset)
  173. {
  174. seq_printf(s, "%s", dev_name(pctldev->dev));
  175. }
  176. static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
  177. struct device_node *np,
  178. struct pinctrl_map **map, unsigned *num_maps)
  179. {
  180. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  181. const struct at91_pin_group *grp;
  182. struct pinctrl_map *new_map;
  183. struct device_node *parent;
  184. int map_num = 1;
  185. int i;
  186. /*
  187. * first find the group of this node and check if we need create
  188. * config maps for pins
  189. */
  190. grp = at91_pinctrl_find_group_by_name(info, np->name);
  191. if (!grp) {
  192. dev_err(info->dev, "unable to find group for node %s\n",
  193. np->name);
  194. return -EINVAL;
  195. }
  196. map_num += grp->npins;
  197. new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, GFP_KERNEL);
  198. if (!new_map)
  199. return -ENOMEM;
  200. *map = new_map;
  201. *num_maps = map_num;
  202. /* create mux map */
  203. parent = of_get_parent(np);
  204. if (!parent) {
  205. kfree(new_map);
  206. return -EINVAL;
  207. }
  208. new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
  209. new_map[0].data.mux.function = parent->name;
  210. new_map[0].data.mux.group = np->name;
  211. of_node_put(parent);
  212. /* create config map */
  213. new_map++;
  214. for (i = 0; i < grp->npins; i++) {
  215. new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
  216. new_map[i].data.configs.group_or_pin =
  217. pin_get_name(pctldev, grp->pins[i]);
  218. new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
  219. new_map[i].data.configs.num_configs = 1;
  220. }
  221. dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
  222. (*map)->data.mux.function, (*map)->data.mux.group, map_num);
  223. return 0;
  224. }
  225. static void at91_dt_free_map(struct pinctrl_dev *pctldev,
  226. struct pinctrl_map *map, unsigned num_maps)
  227. {
  228. }
  229. static struct pinctrl_ops at91_pctrl_ops = {
  230. .get_groups_count = at91_get_groups_count,
  231. .get_group_name = at91_get_group_name,
  232. .get_group_pins = at91_get_group_pins,
  233. .pin_dbg_show = at91_pin_dbg_show,
  234. .dt_node_to_map = at91_dt_node_to_map,
  235. .dt_free_map = at91_dt_free_map,
  236. };
  237. static void __iomem * pin_to_controller(struct at91_pinctrl *info,
  238. unsigned int bank)
  239. {
  240. return gpio_chips[bank]->regbase;
  241. }
  242. static inline int pin_to_bank(unsigned pin)
  243. {
  244. return pin /= MAX_NB_GPIO_PER_BANK;
  245. }
  246. static unsigned pin_to_mask(unsigned int pin)
  247. {
  248. return 1 << pin;
  249. }
  250. static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
  251. {
  252. writel_relaxed(mask, pio + PIO_IDR);
  253. }
  254. static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
  255. {
  256. return (readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1;
  257. }
  258. static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
  259. {
  260. writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
  261. }
  262. static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
  263. {
  264. return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
  265. }
  266. static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
  267. {
  268. writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
  269. }
  270. static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
  271. {
  272. writel_relaxed(mask, pio + PIO_ASR);
  273. }
  274. static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
  275. {
  276. writel_relaxed(mask, pio + PIO_BSR);
  277. }
  278. static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
  279. {
  280. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
  281. pio + PIO_ABCDSR1);
  282. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
  283. pio + PIO_ABCDSR2);
  284. }
  285. static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
  286. {
  287. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
  288. pio + PIO_ABCDSR1);
  289. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
  290. pio + PIO_ABCDSR2);
  291. }
  292. static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
  293. {
  294. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
  295. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  296. }
  297. static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
  298. {
  299. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
  300. writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
  301. }
  302. static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
  303. {
  304. unsigned select;
  305. if (readl_relaxed(pio + PIO_PSR) & mask)
  306. return AT91_MUX_GPIO;
  307. select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
  308. select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
  309. return select + 1;
  310. }
  311. static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
  312. {
  313. unsigned select;
  314. if (readl_relaxed(pio + PIO_PSR) & mask)
  315. return AT91_MUX_GPIO;
  316. select = readl_relaxed(pio + PIO_ABSR) & mask;
  317. return select + 1;
  318. }
  319. static struct at91_pinctrl_mux_ops at91rm9200_ops = {
  320. .get_periph = at91_mux_get_periph,
  321. .mux_A_periph = at91_mux_set_A_periph,
  322. .mux_B_periph = at91_mux_set_B_periph,
  323. .irq_type = gpio_irq_type,
  324. };
  325. static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
  326. .get_periph = at91_mux_pio3_get_periph,
  327. .mux_A_periph = at91_mux_pio3_set_A_periph,
  328. .mux_B_periph = at91_mux_pio3_set_B_periph,
  329. .mux_C_periph = at91_mux_pio3_set_C_periph,
  330. .mux_D_periph = at91_mux_pio3_set_D_periph,
  331. .irq_type = alt_gpio_irq_type,
  332. };
  333. static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
  334. {
  335. if (pin->mux) {
  336. dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lu\n",
  337. pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
  338. } else {
  339. dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lu\n",
  340. pin->bank + 'A', pin->pin, pin->conf);
  341. }
  342. }
  343. static int pin_check_config(struct at91_pinctrl *info, const char* name,
  344. int index, const struct at91_pmx_pin *pin)
  345. {
  346. int mux;
  347. /* check if it's a valid config */
  348. if (pin->bank >= info->nbanks) {
  349. dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
  350. name, index, pin->bank, info->nbanks);
  351. return -EINVAL;
  352. }
  353. if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
  354. dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
  355. name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
  356. return -EINVAL;
  357. }
  358. if (!pin->mux)
  359. return 0;
  360. mux = pin->mux - 1;
  361. if (mux >= info->nmux) {
  362. dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
  363. name, index, mux, info->nmux);
  364. return -EINVAL;
  365. }
  366. if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
  367. dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
  368. name, index, mux, pin->bank + 'A', pin->pin);
  369. return -EINVAL;
  370. }
  371. return 0;
  372. }
  373. static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
  374. {
  375. writel_relaxed(mask, pio + PIO_PDR);
  376. }
  377. static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
  378. {
  379. writel_relaxed(mask, pio + PIO_PER);
  380. writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
  381. }
  382. static int at91_pmx_enable(struct pinctrl_dev *pctldev, unsigned selector,
  383. unsigned group)
  384. {
  385. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  386. const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
  387. const struct at91_pmx_pin *pin;
  388. uint32_t npins = info->groups[group].npins;
  389. int i, ret;
  390. unsigned mask;
  391. void __iomem *pio;
  392. dev_dbg(info->dev, "enable function %s group %s\n",
  393. info->functions[selector].name, info->groups[group].name);
  394. /* first check that all the pins of the group are valid with a valid
  395. * paramter */
  396. for (i = 0; i < npins; i++) {
  397. pin = &pins_conf[i];
  398. ret = pin_check_config(info, info->groups[group].name, i, pin);
  399. if (ret)
  400. return ret;
  401. }
  402. for (i = 0; i < npins; i++) {
  403. pin = &pins_conf[i];
  404. at91_pin_dbg(info->dev, pin);
  405. pio = pin_to_controller(info, pin->bank);
  406. mask = pin_to_mask(pin->pin);
  407. at91_mux_disable_interrupt(pio, mask);
  408. switch(pin->mux) {
  409. case AT91_MUX_GPIO:
  410. at91_mux_gpio_enable(pio, mask, 1);
  411. break;
  412. case AT91_MUX_PERIPH_A:
  413. info->ops->mux_A_periph(pio, mask);
  414. break;
  415. case AT91_MUX_PERIPH_B:
  416. info->ops->mux_B_periph(pio, mask);
  417. break;
  418. case AT91_MUX_PERIPH_C:
  419. if (!info->ops->mux_C_periph)
  420. return -EINVAL;
  421. info->ops->mux_C_periph(pio, mask);
  422. break;
  423. case AT91_MUX_PERIPH_D:
  424. if (!info->ops->mux_D_periph)
  425. return -EINVAL;
  426. info->ops->mux_D_periph(pio, mask);
  427. break;
  428. }
  429. if (pin->mux)
  430. at91_mux_gpio_disable(pio, mask);
  431. }
  432. return 0;
  433. }
  434. static void at91_pmx_disable(struct pinctrl_dev *pctldev, unsigned selector,
  435. unsigned group)
  436. {
  437. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  438. const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
  439. const struct at91_pmx_pin *pin;
  440. uint32_t npins = info->groups[group].npins;
  441. int i;
  442. unsigned mask;
  443. void __iomem *pio;
  444. for (i = 0; i < npins; i++) {
  445. pin = &pins_conf[i];
  446. at91_pin_dbg(info->dev, pin);
  447. pio = pin_to_controller(info, pin->bank);
  448. mask = pin_to_mask(pin->pin);
  449. at91_mux_gpio_enable(pio, mask, 1);
  450. }
  451. }
  452. static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
  453. {
  454. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  455. return info->nfunctions;
  456. }
  457. static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
  458. unsigned selector)
  459. {
  460. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  461. return info->functions[selector].name;
  462. }
  463. static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
  464. const char * const **groups,
  465. unsigned * const num_groups)
  466. {
  467. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  468. *groups = info->functions[selector].groups;
  469. *num_groups = info->functions[selector].ngroups;
  470. return 0;
  471. }
  472. static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
  473. struct pinctrl_gpio_range *range,
  474. unsigned offset)
  475. {
  476. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  477. struct at91_gpio_chip *at91_chip;
  478. struct gpio_chip *chip;
  479. unsigned mask;
  480. if (!range) {
  481. dev_err(npct->dev, "invalid range\n");
  482. return -EINVAL;
  483. }
  484. if (!range->gc) {
  485. dev_err(npct->dev, "missing GPIO chip in range\n");
  486. return -EINVAL;
  487. }
  488. chip = range->gc;
  489. at91_chip = container_of(chip, struct at91_gpio_chip, chip);
  490. dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
  491. mask = 1 << (offset - chip->base);
  492. dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
  493. offset, 'A' + range->id, offset - chip->base, mask);
  494. writel_relaxed(mask, at91_chip->regbase + PIO_PER);
  495. return 0;
  496. }
  497. static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
  498. struct pinctrl_gpio_range *range,
  499. unsigned offset)
  500. {
  501. struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  502. dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
  503. /* Set the pin to some default state, GPIO is usually default */
  504. }
  505. static struct pinmux_ops at91_pmx_ops = {
  506. .get_functions_count = at91_pmx_get_funcs_count,
  507. .get_function_name = at91_pmx_get_func_name,
  508. .get_function_groups = at91_pmx_get_groups,
  509. .enable = at91_pmx_enable,
  510. .disable = at91_pmx_disable,
  511. .gpio_request_enable = at91_gpio_request_enable,
  512. .gpio_disable_free = at91_gpio_disable_free,
  513. };
  514. static int at91_pinconf_get(struct pinctrl_dev *pctldev,
  515. unsigned pin_id, unsigned long *config)
  516. {
  517. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  518. void __iomem *pio;
  519. unsigned pin;
  520. dev_dbg(info->dev, "%s:%d, pin_id=%d, config=0x%lx", __func__, __LINE__, pin_id, *config);
  521. pio = pin_to_controller(info, pin_to_bank(pin_id));
  522. pin = pin_id % MAX_NB_GPIO_PER_BANK;
  523. if (at91_mux_get_multidrive(pio, pin))
  524. *config |= MULTI_DRIVE;
  525. if (at91_mux_get_pullup(pio, pin))
  526. *config |= PULL_UP;
  527. return 0;
  528. }
  529. static int at91_pinconf_set(struct pinctrl_dev *pctldev,
  530. unsigned pin_id, unsigned long config)
  531. {
  532. struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
  533. unsigned mask;
  534. void __iomem *pio;
  535. dev_dbg(info->dev, "%s:%d, pin_id=%d, config=0x%lx", __func__, __LINE__, pin_id, config);
  536. pio = pin_to_controller(info, pin_to_bank(pin_id));
  537. mask = pin_to_mask(pin_id % MAX_NB_GPIO_PER_BANK);
  538. at91_mux_set_pullup(pio, mask, config & PULL_UP);
  539. at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
  540. return 0;
  541. }
  542. static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
  543. struct seq_file *s, unsigned pin_id)
  544. {
  545. }
  546. static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  547. struct seq_file *s, unsigned group)
  548. {
  549. }
  550. struct pinconf_ops at91_pinconf_ops = {
  551. .pin_config_get = at91_pinconf_get,
  552. .pin_config_set = at91_pinconf_set,
  553. .pin_config_dbg_show = at91_pinconf_dbg_show,
  554. .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
  555. };
  556. static struct pinctrl_desc at91_pinctrl_desc = {
  557. .pctlops = &at91_pctrl_ops,
  558. .pmxops = &at91_pmx_ops,
  559. .confops = &at91_pinconf_ops,
  560. .owner = THIS_MODULE,
  561. };
  562. static const char *gpio_compat = "atmel,at91rm9200-gpio";
  563. static void __devinit at91_pinctrl_child_count(struct at91_pinctrl *info,
  564. struct device_node *np)
  565. {
  566. struct device_node *child;
  567. for_each_child_of_node(np, child) {
  568. if (of_device_is_compatible(child, gpio_compat)) {
  569. info->nbanks++;
  570. } else {
  571. info->nfunctions++;
  572. info->ngroups += of_get_child_count(child);
  573. }
  574. }
  575. }
  576. static int __devinit at91_pinctrl_mux_mask(struct at91_pinctrl *info,
  577. struct device_node *np)
  578. {
  579. int ret = 0;
  580. int size;
  581. const const __be32 *list;
  582. list = of_get_property(np, "atmel,mux-mask", &size);
  583. if (!list) {
  584. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  585. return -EINVAL;
  586. }
  587. size /= sizeof(*list);
  588. if (!size || size % info->nbanks) {
  589. dev_err(info->dev, "wrong mux mask array should be by %d\n", info->nbanks);
  590. return -EINVAL;
  591. }
  592. info->nmux = size / info->nbanks;
  593. info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
  594. if (!info->mux_mask) {
  595. dev_err(info->dev, "could not alloc mux_mask\n");
  596. return -ENOMEM;
  597. }
  598. ret = of_property_read_u32_array(np, "atmel,mux-mask",
  599. info->mux_mask, size);
  600. if (ret)
  601. dev_err(info->dev, "can not read the mux-mask of %d\n", size);
  602. return ret;
  603. }
  604. static int __devinit at91_pinctrl_parse_groups(struct device_node *np,
  605. struct at91_pin_group *grp,
  606. struct at91_pinctrl *info,
  607. u32 index)
  608. {
  609. struct at91_pmx_pin *pin;
  610. int size;
  611. const const __be32 *list;
  612. int i, j;
  613. dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
  614. /* Initialise group */
  615. grp->name = np->name;
  616. /*
  617. * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
  618. * do sanity check and calculate pins number
  619. */
  620. list = of_get_property(np, "atmel,pins", &size);
  621. /* we do not check return since it's safe node passed down */
  622. size /= sizeof(*list);
  623. if (!size || size % 4) {
  624. dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
  625. return -EINVAL;
  626. }
  627. grp->npins = size / 4;
  628. pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct at91_pmx_pin),
  629. GFP_KERNEL);
  630. grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
  631. GFP_KERNEL);
  632. if (!grp->pins_conf || !grp->pins)
  633. return -ENOMEM;
  634. for (i = 0, j = 0; i < size; i += 4, j++) {
  635. pin->bank = be32_to_cpu(*list++);
  636. pin->pin = be32_to_cpu(*list++);
  637. grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
  638. pin->mux = be32_to_cpu(*list++);
  639. pin->conf = be32_to_cpu(*list++);
  640. at91_pin_dbg(info->dev, pin);
  641. pin++;
  642. }
  643. return 0;
  644. }
  645. static int __devinit at91_pinctrl_parse_functions(struct device_node *np,
  646. struct at91_pinctrl *info, u32 index)
  647. {
  648. struct device_node *child;
  649. struct at91_pmx_func *func;
  650. struct at91_pin_group *grp;
  651. int ret;
  652. static u32 grp_index;
  653. u32 i = 0;
  654. dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
  655. func = &info->functions[index];
  656. /* Initialise function */
  657. func->name = np->name;
  658. func->ngroups = of_get_child_count(np);
  659. if (func->ngroups <= 0) {
  660. dev_err(info->dev, "no groups defined\n");
  661. return -EINVAL;
  662. }
  663. func->groups = devm_kzalloc(info->dev,
  664. func->ngroups * sizeof(char *), GFP_KERNEL);
  665. if (!func->groups)
  666. return -ENOMEM;
  667. for_each_child_of_node(np, child) {
  668. func->groups[i] = child->name;
  669. grp = &info->groups[grp_index++];
  670. ret = at91_pinctrl_parse_groups(child, grp, info, i++);
  671. if (ret)
  672. return ret;
  673. }
  674. return 0;
  675. }
  676. static struct of_device_id at91_pinctrl_of_match[] __devinitdata = {
  677. { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
  678. { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
  679. { /* sentinel */ }
  680. };
  681. static int __devinit at91_pinctrl_probe_dt(struct platform_device *pdev,
  682. struct at91_pinctrl *info)
  683. {
  684. int ret = 0;
  685. int i, j;
  686. uint32_t *tmp;
  687. struct device_node *np = pdev->dev.of_node;
  688. struct device_node *child;
  689. if (!np)
  690. return -ENODEV;
  691. info->dev = &pdev->dev;
  692. info->ops = (struct at91_pinctrl_mux_ops*)
  693. of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
  694. at91_pinctrl_child_count(info, np);
  695. if (info->nbanks < 1) {
  696. dev_err(&pdev->dev, "you need to specify atleast one gpio-controller\n");
  697. return -EINVAL;
  698. }
  699. ret = at91_pinctrl_mux_mask(info, np);
  700. if (ret)
  701. return ret;
  702. dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
  703. dev_dbg(&pdev->dev, "mux-mask\n");
  704. tmp = info->mux_mask;
  705. for (i = 0; i < info->nbanks; i++) {
  706. for (j = 0; j < info->nmux; j++, tmp++) {
  707. dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
  708. }
  709. }
  710. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  711. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  712. info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct at91_pmx_func),
  713. GFP_KERNEL);
  714. if (!info->functions)
  715. return -ENOMEM;
  716. info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct at91_pin_group),
  717. GFP_KERNEL);
  718. if (!info->groups)
  719. return -ENOMEM;
  720. dev_dbg(&pdev->dev, "nbanks = %d\n", info->nbanks);
  721. dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
  722. dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
  723. i = 0;
  724. for_each_child_of_node(np, child) {
  725. if (of_device_is_compatible(child, gpio_compat))
  726. continue;
  727. ret = at91_pinctrl_parse_functions(child, info, i++);
  728. if (ret) {
  729. dev_err(&pdev->dev, "failed to parse function\n");
  730. return ret;
  731. }
  732. }
  733. return 0;
  734. }
  735. static int __devinit at91_pinctrl_probe(struct platform_device *pdev)
  736. {
  737. struct at91_pinctrl *info;
  738. struct pinctrl_pin_desc *pdesc;
  739. int ret, i, j ,k;
  740. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  741. if (!info)
  742. return -ENOMEM;
  743. ret = at91_pinctrl_probe_dt(pdev, info);
  744. if (ret)
  745. return ret;
  746. /*
  747. * We need all the GPIO drivers to probe FIRST, or we will not be able
  748. * to obtain references to the struct gpio_chip * for them, and we
  749. * need this to proceed.
  750. */
  751. for (i = 0; i < info->nbanks; i++) {
  752. if (!gpio_chips[i]) {
  753. dev_warn(&pdev->dev, "GPIO chip %d not registered yet\n", i);
  754. devm_kfree(&pdev->dev, info);
  755. return -EPROBE_DEFER;
  756. }
  757. }
  758. at91_pinctrl_desc.name = dev_name(&pdev->dev);
  759. at91_pinctrl_desc.npins = info->nbanks * MAX_NB_GPIO_PER_BANK;
  760. at91_pinctrl_desc.pins = pdesc =
  761. devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL);
  762. if (!at91_pinctrl_desc.pins)
  763. return -ENOMEM;
  764. for (i = 0 , k = 0; i < info->nbanks; i++) {
  765. for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
  766. pdesc->number = k;
  767. pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
  768. pdesc++;
  769. }
  770. }
  771. platform_set_drvdata(pdev, info);
  772. info->pctl = pinctrl_register(&at91_pinctrl_desc, &pdev->dev, info);
  773. if (!info->pctl) {
  774. dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
  775. ret = -EINVAL;
  776. goto err;
  777. }
  778. /* We will handle a range of GPIO pins */
  779. for (i = 0; i < info->nbanks; i++)
  780. pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
  781. dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
  782. return 0;
  783. err:
  784. return ret;
  785. }
  786. static int __devexit at91_pinctrl_remove(struct platform_device *pdev)
  787. {
  788. struct at91_pinctrl *info = platform_get_drvdata(pdev);
  789. pinctrl_unregister(info->pctl);
  790. return 0;
  791. }
  792. static int at91_gpio_request(struct gpio_chip *chip, unsigned offset)
  793. {
  794. /*
  795. * Map back to global GPIO space and request muxing, the direction
  796. * parameter does not matter for this controller.
  797. */
  798. int gpio = chip->base + offset;
  799. int bank = chip->base / chip->ngpio;
  800. dev_dbg(chip->dev, "%s:%d pio%c%d(%d)\n", __func__, __LINE__,
  801. 'A' + bank, offset, gpio);
  802. return pinctrl_request_gpio(gpio);
  803. }
  804. static void at91_gpio_free(struct gpio_chip *chip, unsigned offset)
  805. {
  806. int gpio = chip->base + offset;
  807. pinctrl_free_gpio(gpio);
  808. }
  809. static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  810. {
  811. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  812. void __iomem *pio = at91_gpio->regbase;
  813. unsigned mask = 1 << offset;
  814. writel_relaxed(mask, pio + PIO_ODR);
  815. return 0;
  816. }
  817. static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
  818. {
  819. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  820. void __iomem *pio = at91_gpio->regbase;
  821. unsigned mask = 1 << offset;
  822. u32 pdsr;
  823. pdsr = readl_relaxed(pio + PIO_PDSR);
  824. return (pdsr & mask) != 0;
  825. }
  826. static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
  827. int val)
  828. {
  829. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  830. void __iomem *pio = at91_gpio->regbase;
  831. unsigned mask = 1 << offset;
  832. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  833. }
  834. static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  835. int val)
  836. {
  837. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  838. void __iomem *pio = at91_gpio->regbase;
  839. unsigned mask = 1 << offset;
  840. writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
  841. writel_relaxed(mask, pio + PIO_OER);
  842. return 0;
  843. }
  844. static int at91_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  845. {
  846. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  847. int virq;
  848. if (offset < chip->ngpio)
  849. virq = irq_create_mapping(at91_gpio->domain, offset);
  850. else
  851. virq = -ENXIO;
  852. dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
  853. chip->label, offset + chip->base, virq);
  854. return virq;
  855. }
  856. #ifdef CONFIG_DEBUG_FS
  857. static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  858. {
  859. enum at91_mux mode;
  860. int i;
  861. struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
  862. void __iomem *pio = at91_gpio->regbase;
  863. for (i = 0; i < chip->ngpio; i++) {
  864. unsigned pin = chip->base + i;
  865. unsigned mask = pin_to_mask(pin);
  866. const char *gpio_label;
  867. u32 pdsr;
  868. gpio_label = gpiochip_is_requested(chip, i);
  869. if (!gpio_label)
  870. continue;
  871. mode = at91_gpio->ops->get_periph(pio, mask);
  872. seq_printf(s, "[%s] GPIO%s%d: ",
  873. gpio_label, chip->label, i);
  874. if (mode == AT91_MUX_GPIO) {
  875. pdsr = readl_relaxed(pio + PIO_PDSR);
  876. seq_printf(s, "[gpio] %s\n",
  877. pdsr & mask ?
  878. "set" : "clear");
  879. } else {
  880. seq_printf(s, "[periph %c]\n",
  881. mode + 'A' - 1);
  882. }
  883. }
  884. }
  885. #else
  886. #define at91_gpio_dbg_show NULL
  887. #endif
  888. /* Several AIC controller irqs are dispatched through this GPIO handler.
  889. * To use any AT91_PIN_* as an externally triggered IRQ, first call
  890. * at91_set_gpio_input() then maybe enable its glitch filter.
  891. * Then just request_irq() with the pin ID; it works like any ARM IRQ
  892. * handler.
  893. * First implementation always triggers on rising and falling edges
  894. * whereas the newer PIO3 can be additionally configured to trigger on
  895. * level, edge with any polarity.
  896. *
  897. * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
  898. * configuring them with at91_set_a_periph() or at91_set_b_periph().
  899. * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
  900. */
  901. static void gpio_irq_mask(struct irq_data *d)
  902. {
  903. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  904. void __iomem *pio = at91_gpio->regbase;
  905. unsigned mask = 1 << d->hwirq;
  906. if (pio)
  907. writel_relaxed(mask, pio + PIO_IDR);
  908. }
  909. static void gpio_irq_unmask(struct irq_data *d)
  910. {
  911. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  912. void __iomem *pio = at91_gpio->regbase;
  913. unsigned mask = 1 << d->hwirq;
  914. if (pio)
  915. writel_relaxed(mask, pio + PIO_IER);
  916. }
  917. static int gpio_irq_type(struct irq_data *d, unsigned type)
  918. {
  919. switch (type) {
  920. case IRQ_TYPE_NONE:
  921. case IRQ_TYPE_EDGE_BOTH:
  922. return 0;
  923. default:
  924. return -EINVAL;
  925. }
  926. }
  927. /* Alternate irq type for PIO3 support */
  928. static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
  929. {
  930. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  931. void __iomem *pio = at91_gpio->regbase;
  932. unsigned mask = 1 << d->hwirq;
  933. switch (type) {
  934. case IRQ_TYPE_EDGE_RISING:
  935. writel_relaxed(mask, pio + PIO_ESR);
  936. writel_relaxed(mask, pio + PIO_REHLSR);
  937. break;
  938. case IRQ_TYPE_EDGE_FALLING:
  939. writel_relaxed(mask, pio + PIO_ESR);
  940. writel_relaxed(mask, pio + PIO_FELLSR);
  941. break;
  942. case IRQ_TYPE_LEVEL_LOW:
  943. writel_relaxed(mask, pio + PIO_LSR);
  944. writel_relaxed(mask, pio + PIO_FELLSR);
  945. break;
  946. case IRQ_TYPE_LEVEL_HIGH:
  947. writel_relaxed(mask, pio + PIO_LSR);
  948. writel_relaxed(mask, pio + PIO_REHLSR);
  949. break;
  950. case IRQ_TYPE_EDGE_BOTH:
  951. /*
  952. * disable additional interrupt modes:
  953. * fall back to default behavior
  954. */
  955. writel_relaxed(mask, pio + PIO_AIMDR);
  956. return 0;
  957. case IRQ_TYPE_NONE:
  958. default:
  959. pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
  960. return -EINVAL;
  961. }
  962. /* enable additional interrupt modes */
  963. writel_relaxed(mask, pio + PIO_AIMER);
  964. return 0;
  965. }
  966. #ifdef CONFIG_PM
  967. static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
  968. {
  969. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
  970. unsigned bank = at91_gpio->pioc_idx;
  971. if (unlikely(bank >= MAX_GPIO_BANKS))
  972. return -EINVAL;
  973. irq_set_irq_wake(at91_gpio->pioc_virq, state);
  974. return 0;
  975. }
  976. #else
  977. #define gpio_irq_set_wake NULL
  978. #endif
  979. static struct irq_chip gpio_irqchip = {
  980. .name = "GPIO",
  981. .irq_disable = gpio_irq_mask,
  982. .irq_mask = gpio_irq_mask,
  983. .irq_unmask = gpio_irq_unmask,
  984. /* .irq_set_type is set dynamically */
  985. .irq_set_wake = gpio_irq_set_wake,
  986. };
  987. static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
  988. {
  989. struct irq_chip *chip = irq_desc_get_chip(desc);
  990. struct irq_data *idata = irq_desc_get_irq_data(desc);
  991. struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(idata);
  992. void __iomem *pio = at91_gpio->regbase;
  993. unsigned long isr;
  994. int n;
  995. chained_irq_enter(chip, desc);
  996. for (;;) {
  997. /* Reading ISR acks pending (edge triggered) GPIO interrupts.
  998. * When there none are pending, we're finished unless we need
  999. * to process multiple banks (like ID_PIOCDE on sam9263).
  1000. */
  1001. isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
  1002. if (!isr) {
  1003. if (!at91_gpio->next)
  1004. break;
  1005. at91_gpio = at91_gpio->next;
  1006. pio = at91_gpio->regbase;
  1007. continue;
  1008. }
  1009. for_each_set_bit(n, &isr, BITS_PER_LONG) {
  1010. generic_handle_irq(irq_find_mapping(at91_gpio->domain, n));
  1011. }
  1012. }
  1013. chained_irq_exit(chip, desc);
  1014. /* now it may re-trigger */
  1015. }
  1016. /*
  1017. * This lock class tells lockdep that GPIO irqs are in a different
  1018. * category than their parents, so it won't report false recursion.
  1019. */
  1020. static struct lock_class_key gpio_lock_class;
  1021. static int at91_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  1022. irq_hw_number_t hw)
  1023. {
  1024. struct at91_gpio_chip *at91_gpio = h->host_data;
  1025. irq_set_lockdep_class(virq, &gpio_lock_class);
  1026. /*
  1027. * Can use the "simple" and not "edge" handler since it's
  1028. * shorter, and the AIC handles interrupts sanely.
  1029. */
  1030. irq_set_chip_and_handler(virq, &gpio_irqchip,
  1031. handle_simple_irq);
  1032. set_irq_flags(virq, IRQF_VALID);
  1033. irq_set_chip_data(virq, at91_gpio);
  1034. return 0;
  1035. }
  1036. static int at91_gpio_irq_domain_xlate(struct irq_domain *d,
  1037. struct device_node *ctrlr,
  1038. const u32 *intspec, unsigned int intsize,
  1039. irq_hw_number_t *out_hwirq,
  1040. unsigned int *out_type)
  1041. {
  1042. struct at91_gpio_chip *at91_gpio = d->host_data;
  1043. int ret;
  1044. int pin = at91_gpio->chip.base + intspec[0];
  1045. if (WARN_ON(intsize < 2))
  1046. return -EINVAL;
  1047. *out_hwirq = intspec[0];
  1048. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  1049. ret = gpio_request(pin, ctrlr->full_name);
  1050. if (ret)
  1051. return ret;
  1052. ret = gpio_direction_input(pin);
  1053. if (ret)
  1054. return ret;
  1055. return 0;
  1056. }
  1057. static struct irq_domain_ops at91_gpio_ops = {
  1058. .map = at91_gpio_irq_map,
  1059. .xlate = at91_gpio_irq_domain_xlate,
  1060. };
  1061. static int at91_gpio_of_irq_setup(struct device_node *node,
  1062. struct at91_gpio_chip *at91_gpio)
  1063. {
  1064. struct at91_gpio_chip *prev = NULL;
  1065. struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
  1066. at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
  1067. /* Setup proper .irq_set_type function */
  1068. gpio_irqchip.irq_set_type = at91_gpio->ops->irq_type;
  1069. /* Disable irqs of this PIO controller */
  1070. writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
  1071. /* Setup irq domain */
  1072. at91_gpio->domain = irq_domain_add_linear(node, at91_gpio->chip.ngpio,
  1073. &at91_gpio_ops, at91_gpio);
  1074. if (!at91_gpio->domain)
  1075. panic("at91_gpio.%d: couldn't allocate irq domain (DT).\n",
  1076. at91_gpio->pioc_idx);
  1077. /* Setup chained handler */
  1078. if (at91_gpio->pioc_idx)
  1079. prev = gpio_chips[at91_gpio->pioc_idx - 1];
  1080. /* The toplevel handler handles one bank of GPIOs, except
  1081. * on some SoC it can handles up to three...
  1082. * We only set up the handler for the first of the list.
  1083. */
  1084. if (prev && prev->next == at91_gpio)
  1085. return 0;
  1086. irq_set_chip_data(at91_gpio->pioc_virq, at91_gpio);
  1087. irq_set_chained_handler(at91_gpio->pioc_virq, gpio_irq_handler);
  1088. return 0;
  1089. }
  1090. /* This structure is replicated for each GPIO block allocated at probe time */
  1091. static struct gpio_chip at91_gpio_template = {
  1092. .request = at91_gpio_request,
  1093. .free = at91_gpio_free,
  1094. .direction_input = at91_gpio_direction_input,
  1095. .get = at91_gpio_get,
  1096. .direction_output = at91_gpio_direction_output,
  1097. .set = at91_gpio_set,
  1098. .to_irq = at91_gpio_to_irq,
  1099. .dbg_show = at91_gpio_dbg_show,
  1100. .can_sleep = 0,
  1101. .ngpio = MAX_NB_GPIO_PER_BANK,
  1102. };
  1103. static void __devinit at91_gpio_probe_fixup(void)
  1104. {
  1105. unsigned i;
  1106. struct at91_gpio_chip *at91_gpio, *last = NULL;
  1107. for (i = 0; i < gpio_banks; i++) {
  1108. at91_gpio = gpio_chips[i];
  1109. /*
  1110. * GPIO controller are grouped on some SoC:
  1111. * PIOC, PIOD and PIOE can share the same IRQ line
  1112. */
  1113. if (last && last->pioc_virq == at91_gpio->pioc_virq)
  1114. last->next = at91_gpio;
  1115. last = at91_gpio;
  1116. }
  1117. }
  1118. static struct of_device_id at91_gpio_of_match[] __devinitdata = {
  1119. { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
  1120. { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
  1121. { /* sentinel */ }
  1122. };
  1123. static int __devinit at91_gpio_probe(struct platform_device *pdev)
  1124. {
  1125. struct device_node *np = pdev->dev.of_node;
  1126. struct resource *res;
  1127. struct at91_gpio_chip *at91_chip = NULL;
  1128. struct gpio_chip *chip;
  1129. struct pinctrl_gpio_range *range;
  1130. int ret = 0;
  1131. int irq, i;
  1132. int alias_idx = of_alias_get_id(np, "gpio");
  1133. uint32_t ngpio;
  1134. char **names;
  1135. BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
  1136. if (gpio_chips[alias_idx]) {
  1137. ret = -EBUSY;
  1138. goto err;
  1139. }
  1140. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1141. if (!res) {
  1142. ret = -ENOENT;
  1143. goto err;
  1144. }
  1145. irq = platform_get_irq(pdev, 0);
  1146. if (irq < 0) {
  1147. ret = irq;
  1148. goto err;
  1149. }
  1150. at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
  1151. if (!at91_chip) {
  1152. ret = -ENOMEM;
  1153. goto err;
  1154. }
  1155. at91_chip->regbase = devm_request_and_ioremap(&pdev->dev, res);
  1156. if (!at91_chip->regbase) {
  1157. dev_err(&pdev->dev, "failed to map registers, ignoring.\n");
  1158. ret = -EBUSY;
  1159. goto err;
  1160. }
  1161. at91_chip->ops = (struct at91_pinctrl_mux_ops*)
  1162. of_match_device(at91_gpio_of_match, &pdev->dev)->data;
  1163. at91_chip->pioc_virq = irq;
  1164. at91_chip->pioc_idx = alias_idx;
  1165. at91_chip->clock = clk_get(&pdev->dev, NULL);
  1166. if (IS_ERR(at91_chip->clock)) {
  1167. dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
  1168. goto err;
  1169. }
  1170. if (clk_prepare(at91_chip->clock))
  1171. goto clk_prep_err;
  1172. /* enable PIO controller's clock */
  1173. if (clk_enable(at91_chip->clock)) {
  1174. dev_err(&pdev->dev, "failed to enable clock, ignoring.\n");
  1175. goto clk_err;
  1176. }
  1177. at91_chip->chip = at91_gpio_template;
  1178. chip = &at91_chip->chip;
  1179. chip->of_node = np;
  1180. chip->label = dev_name(&pdev->dev);
  1181. chip->dev = &pdev->dev;
  1182. chip->owner = THIS_MODULE;
  1183. chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
  1184. if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
  1185. if (ngpio >= MAX_NB_GPIO_PER_BANK)
  1186. pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
  1187. alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
  1188. else
  1189. chip->ngpio = ngpio;
  1190. }
  1191. names = devm_kzalloc(&pdev->dev, sizeof(char*) * chip->ngpio, GFP_KERNEL);
  1192. if (!names) {
  1193. ret = -ENOMEM;
  1194. goto clk_err;
  1195. }
  1196. for (i = 0; i < chip->ngpio; i++)
  1197. names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
  1198. chip->names = (const char*const*)names;
  1199. range = &at91_chip->range;
  1200. range->name = chip->label;
  1201. range->id = alias_idx;
  1202. range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
  1203. range->npins = chip->ngpio;
  1204. range->gc = chip;
  1205. ret = gpiochip_add(chip);
  1206. if (ret)
  1207. goto clk_err;
  1208. gpio_chips[alias_idx] = at91_chip;
  1209. gpio_banks = max(gpio_banks, alias_idx + 1);
  1210. at91_gpio_probe_fixup();
  1211. at91_gpio_of_irq_setup(np, at91_chip);
  1212. dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
  1213. return 0;
  1214. clk_err:
  1215. clk_unprepare(at91_chip->clock);
  1216. clk_prep_err:
  1217. clk_put(at91_chip->clock);
  1218. err:
  1219. dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
  1220. return ret;
  1221. }
  1222. static struct platform_driver at91_gpio_driver = {
  1223. .driver = {
  1224. .name = "gpio-at91",
  1225. .owner = THIS_MODULE,
  1226. .of_match_table = of_match_ptr(at91_gpio_of_match),
  1227. },
  1228. .probe = at91_gpio_probe,
  1229. };
  1230. static struct platform_driver at91_pinctrl_driver = {
  1231. .driver = {
  1232. .name = "pinctrl-at91",
  1233. .owner = THIS_MODULE,
  1234. .of_match_table = of_match_ptr(at91_pinctrl_of_match),
  1235. },
  1236. .probe = at91_pinctrl_probe,
  1237. .remove = __devexit_p(at91_pinctrl_remove),
  1238. };
  1239. static int __init at91_pinctrl_init(void)
  1240. {
  1241. int ret;
  1242. ret = platform_driver_register(&at91_gpio_driver);
  1243. if (ret)
  1244. return ret;
  1245. return platform_driver_register(&at91_pinctrl_driver);
  1246. }
  1247. arch_initcall(at91_pinctrl_init);
  1248. static void __exit at91_pinctrl_exit(void)
  1249. {
  1250. platform_driver_unregister(&at91_pinctrl_driver);
  1251. }
  1252. module_exit(at91_pinctrl_exit);
  1253. MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
  1254. MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
  1255. MODULE_LICENSE("GPL v2");