pinctrl-at91.c 38 KB

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