pinctrl-at91-pio4.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. /*
  2. * Driver for the Atmel PIO4 controller
  3. *
  4. * Copyright (C) 2015 Atmel,
  5. * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <dt-bindings/pinctrl/at91.h>
  17. #include <linux/clk.h>
  18. #include <linux/gpio/driver.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/init.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pinctrl/pinconf.h>
  25. #include <linux/pinctrl/pinconf-generic.h>
  26. #include <linux/pinctrl/pinctrl.h>
  27. #include <linux/pinctrl/pinmux.h>
  28. #include <linux/slab.h>
  29. #include "core.h"
  30. #include "pinconf.h"
  31. #include "pinctrl-utils.h"
  32. /*
  33. * Warning:
  34. * In order to not introduce confusion between Atmel PIO groups and pinctrl
  35. * framework groups, Atmel PIO groups will be called banks, line is kept to
  36. * designed the pin id into this bank.
  37. */
  38. #define ATMEL_PIO_MSKR 0x0000
  39. #define ATMEL_PIO_CFGR 0x0004
  40. #define ATMEL_PIO_CFGR_FUNC_MASK GENMASK(2, 0)
  41. #define ATMEL_PIO_DIR_MASK BIT(8)
  42. #define ATMEL_PIO_PUEN_MASK BIT(9)
  43. #define ATMEL_PIO_PDEN_MASK BIT(10)
  44. #define ATMEL_PIO_IFEN_MASK BIT(12)
  45. #define ATMEL_PIO_IFSCEN_MASK BIT(13)
  46. #define ATMEL_PIO_OPD_MASK BIT(14)
  47. #define ATMEL_PIO_SCHMITT_MASK BIT(15)
  48. #define ATMEL_PIO_DRVSTR_MASK GENMASK(17, 16)
  49. #define ATMEL_PIO_DRVSTR_OFFSET 16
  50. #define ATMEL_PIO_CFGR_EVTSEL_MASK GENMASK(26, 24)
  51. #define ATMEL_PIO_CFGR_EVTSEL_FALLING (0 << 24)
  52. #define ATMEL_PIO_CFGR_EVTSEL_RISING (1 << 24)
  53. #define ATMEL_PIO_CFGR_EVTSEL_BOTH (2 << 24)
  54. #define ATMEL_PIO_CFGR_EVTSEL_LOW (3 << 24)
  55. #define ATMEL_PIO_CFGR_EVTSEL_HIGH (4 << 24)
  56. #define ATMEL_PIO_PDSR 0x0008
  57. #define ATMEL_PIO_LOCKSR 0x000C
  58. #define ATMEL_PIO_SODR 0x0010
  59. #define ATMEL_PIO_CODR 0x0014
  60. #define ATMEL_PIO_ODSR 0x0018
  61. #define ATMEL_PIO_IER 0x0020
  62. #define ATMEL_PIO_IDR 0x0024
  63. #define ATMEL_PIO_IMR 0x0028
  64. #define ATMEL_PIO_ISR 0x002C
  65. #define ATMEL_PIO_IOFR 0x003C
  66. #define ATMEL_PIO_NPINS_PER_BANK 32
  67. #define ATMEL_PIO_BANK(pin_id) (pin_id / ATMEL_PIO_NPINS_PER_BANK)
  68. #define ATMEL_PIO_LINE(pin_id) (pin_id % ATMEL_PIO_NPINS_PER_BANK)
  69. #define ATMEL_PIO_BANK_OFFSET 0x40
  70. #define ATMEL_GET_PIN_NO(pinfunc) ((pinfunc) & 0xff)
  71. #define ATMEL_GET_PIN_FUNC(pinfunc) ((pinfunc >> 16) & 0xf)
  72. #define ATMEL_GET_PIN_IOSET(pinfunc) ((pinfunc >> 20) & 0xf)
  73. /* Custom pinconf parameters */
  74. #define ATMEL_PIN_CONFIG_DRIVE_STRENGTH (PIN_CONFIG_END + 1)
  75. struct atmel_pioctrl_data {
  76. unsigned nbanks;
  77. };
  78. struct atmel_group {
  79. const char *name;
  80. u32 pin;
  81. };
  82. struct atmel_pin {
  83. unsigned pin_id;
  84. unsigned mux;
  85. unsigned ioset;
  86. unsigned bank;
  87. unsigned line;
  88. const char *device;
  89. };
  90. /**
  91. * struct atmel_pioctrl - Atmel PIO controller (pinmux + gpio)
  92. * @reg_base: base address of the controller.
  93. * @clk: clock of the controller.
  94. * @nbanks: number of PIO groups, it can vary depending on the SoC.
  95. * @pinctrl_dev: pinctrl device registered.
  96. * @groups: groups table to provide group name and pin in the group to pinctrl.
  97. * @group_names: group names table to provide all the group/pin names to
  98. * pinctrl or gpio.
  99. * @pins: pins table used for both pinctrl and gpio. pin_id, bank and line
  100. * fields are set at probe time. Other ones are set when parsing dt
  101. * pinctrl.
  102. * @npins: number of pins.
  103. * @gpio_chip: gpio chip registered.
  104. * @irq_domain: irq domain for the gpio controller.
  105. * @irqs: table containing the hw irq number of the bank. The index of the
  106. * table is the bank id.
  107. * @dev: device entry for the Atmel PIO controller.
  108. * @node: node of the Atmel PIO controller.
  109. */
  110. struct atmel_pioctrl {
  111. void __iomem *reg_base;
  112. struct clk *clk;
  113. unsigned nbanks;
  114. struct pinctrl_dev *pinctrl_dev;
  115. struct atmel_group *groups;
  116. const char * const *group_names;
  117. struct atmel_pin **pins;
  118. unsigned npins;
  119. struct gpio_chip *gpio_chip;
  120. struct irq_domain *irq_domain;
  121. int *irqs;
  122. unsigned *pm_wakeup_sources;
  123. struct {
  124. u32 imr;
  125. u32 odsr;
  126. u32 cfgr[ATMEL_PIO_NPINS_PER_BANK];
  127. } *pm_suspend_backup;
  128. struct device *dev;
  129. struct device_node *node;
  130. };
  131. static const char * const atmel_functions[] = {
  132. "GPIO", "A", "B", "C", "D", "E", "F", "G"
  133. };
  134. static const struct pinconf_generic_params atmel_custom_bindings[] = {
  135. {"atmel,drive-strength", ATMEL_PIN_CONFIG_DRIVE_STRENGTH, 0},
  136. };
  137. /* --- GPIO --- */
  138. static unsigned int atmel_gpio_read(struct atmel_pioctrl *atmel_pioctrl,
  139. unsigned int bank, unsigned int reg)
  140. {
  141. return readl_relaxed(atmel_pioctrl->reg_base
  142. + ATMEL_PIO_BANK_OFFSET * bank + reg);
  143. }
  144. static void atmel_gpio_write(struct atmel_pioctrl *atmel_pioctrl,
  145. unsigned int bank, unsigned int reg,
  146. unsigned int val)
  147. {
  148. writel_relaxed(val, atmel_pioctrl->reg_base
  149. + ATMEL_PIO_BANK_OFFSET * bank + reg);
  150. }
  151. static void atmel_gpio_irq_ack(struct irq_data *d)
  152. {
  153. /*
  154. * Nothing to do, interrupt is cleared when reading the status
  155. * register.
  156. */
  157. }
  158. static int atmel_gpio_irq_set_type(struct irq_data *d, unsigned type)
  159. {
  160. struct atmel_pioctrl *atmel_pioctrl = irq_data_get_irq_chip_data(d);
  161. struct atmel_pin *pin = atmel_pioctrl->pins[d->hwirq];
  162. unsigned reg;
  163. atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_MSKR,
  164. BIT(pin->line));
  165. reg = atmel_gpio_read(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR);
  166. reg &= (~ATMEL_PIO_CFGR_EVTSEL_MASK);
  167. switch (type) {
  168. case IRQ_TYPE_EDGE_RISING:
  169. irq_set_handler_locked(d, handle_edge_irq);
  170. reg |= ATMEL_PIO_CFGR_EVTSEL_RISING;
  171. break;
  172. case IRQ_TYPE_EDGE_FALLING:
  173. irq_set_handler_locked(d, handle_edge_irq);
  174. reg |= ATMEL_PIO_CFGR_EVTSEL_FALLING;
  175. break;
  176. case IRQ_TYPE_EDGE_BOTH:
  177. irq_set_handler_locked(d, handle_edge_irq);
  178. reg |= ATMEL_PIO_CFGR_EVTSEL_BOTH;
  179. break;
  180. case IRQ_TYPE_LEVEL_LOW:
  181. irq_set_handler_locked(d, handle_level_irq);
  182. reg |= ATMEL_PIO_CFGR_EVTSEL_LOW;
  183. break;
  184. case IRQ_TYPE_LEVEL_HIGH:
  185. irq_set_handler_locked(d, handle_level_irq);
  186. reg |= ATMEL_PIO_CFGR_EVTSEL_HIGH;
  187. break;
  188. case IRQ_TYPE_NONE:
  189. default:
  190. return -EINVAL;
  191. }
  192. atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR, reg);
  193. return 0;
  194. }
  195. static void atmel_gpio_irq_mask(struct irq_data *d)
  196. {
  197. struct atmel_pioctrl *atmel_pioctrl = irq_data_get_irq_chip_data(d);
  198. struct atmel_pin *pin = atmel_pioctrl->pins[d->hwirq];
  199. atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_IDR,
  200. BIT(pin->line));
  201. }
  202. static void atmel_gpio_irq_unmask(struct irq_data *d)
  203. {
  204. struct atmel_pioctrl *atmel_pioctrl = irq_data_get_irq_chip_data(d);
  205. struct atmel_pin *pin = atmel_pioctrl->pins[d->hwirq];
  206. atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_IER,
  207. BIT(pin->line));
  208. }
  209. #ifdef CONFIG_PM_SLEEP
  210. static int atmel_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  211. {
  212. struct atmel_pioctrl *atmel_pioctrl = irq_data_get_irq_chip_data(d);
  213. int bank = ATMEL_PIO_BANK(d->hwirq);
  214. int line = ATMEL_PIO_LINE(d->hwirq);
  215. /* The gpio controller has one interrupt line per bank. */
  216. irq_set_irq_wake(atmel_pioctrl->irqs[bank], on);
  217. if (on)
  218. atmel_pioctrl->pm_wakeup_sources[bank] |= BIT(line);
  219. else
  220. atmel_pioctrl->pm_wakeup_sources[bank] &= ~(BIT(line));
  221. return 0;
  222. }
  223. #else
  224. #define atmel_gpio_irq_set_wake NULL
  225. #endif /* CONFIG_PM_SLEEP */
  226. static struct irq_chip atmel_gpio_irq_chip = {
  227. .name = "GPIO",
  228. .irq_ack = atmel_gpio_irq_ack,
  229. .irq_mask = atmel_gpio_irq_mask,
  230. .irq_unmask = atmel_gpio_irq_unmask,
  231. .irq_set_type = atmel_gpio_irq_set_type,
  232. .irq_set_wake = atmel_gpio_irq_set_wake,
  233. };
  234. static int atmel_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  235. {
  236. struct atmel_pioctrl *atmel_pioctrl = gpiochip_get_data(chip);
  237. return irq_find_mapping(atmel_pioctrl->irq_domain, offset);
  238. }
  239. static void atmel_gpio_irq_handler(struct irq_desc *desc)
  240. {
  241. unsigned int irq = irq_desc_get_irq(desc);
  242. struct atmel_pioctrl *atmel_pioctrl = irq_desc_get_handler_data(desc);
  243. struct irq_chip *chip = irq_desc_get_chip(desc);
  244. unsigned long isr;
  245. int n, bank = -1;
  246. /* Find from which bank is the irq received. */
  247. for (n = 0; n < atmel_pioctrl->nbanks; n++) {
  248. if (atmel_pioctrl->irqs[n] == irq) {
  249. bank = n;
  250. break;
  251. }
  252. }
  253. if (bank < 0) {
  254. dev_err(atmel_pioctrl->dev,
  255. "no bank associated to irq %u\n", irq);
  256. return;
  257. }
  258. chained_irq_enter(chip, desc);
  259. for (;;) {
  260. isr = (unsigned long)atmel_gpio_read(atmel_pioctrl, bank,
  261. ATMEL_PIO_ISR);
  262. isr &= (unsigned long)atmel_gpio_read(atmel_pioctrl, bank,
  263. ATMEL_PIO_IMR);
  264. if (!isr)
  265. break;
  266. for_each_set_bit(n, &isr, BITS_PER_LONG)
  267. generic_handle_irq(atmel_gpio_to_irq(
  268. atmel_pioctrl->gpio_chip,
  269. bank * ATMEL_PIO_NPINS_PER_BANK + n));
  270. }
  271. chained_irq_exit(chip, desc);
  272. }
  273. static int atmel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  274. {
  275. struct atmel_pioctrl *atmel_pioctrl = gpiochip_get_data(chip);
  276. struct atmel_pin *pin = atmel_pioctrl->pins[offset];
  277. unsigned reg;
  278. atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_MSKR,
  279. BIT(pin->line));
  280. reg = atmel_gpio_read(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR);
  281. reg &= ~ATMEL_PIO_DIR_MASK;
  282. atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR, reg);
  283. return 0;
  284. }
  285. static int atmel_gpio_get(struct gpio_chip *chip, unsigned offset)
  286. {
  287. struct atmel_pioctrl *atmel_pioctrl = gpiochip_get_data(chip);
  288. struct atmel_pin *pin = atmel_pioctrl->pins[offset];
  289. unsigned reg;
  290. reg = atmel_gpio_read(atmel_pioctrl, pin->bank, ATMEL_PIO_PDSR);
  291. return !!(reg & BIT(pin->line));
  292. }
  293. static int atmel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  294. int value)
  295. {
  296. struct atmel_pioctrl *atmel_pioctrl = gpiochip_get_data(chip);
  297. struct atmel_pin *pin = atmel_pioctrl->pins[offset];
  298. unsigned reg;
  299. atmel_gpio_write(atmel_pioctrl, pin->bank,
  300. value ? ATMEL_PIO_SODR : ATMEL_PIO_CODR,
  301. BIT(pin->line));
  302. atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_MSKR,
  303. BIT(pin->line));
  304. reg = atmel_gpio_read(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR);
  305. reg |= ATMEL_PIO_DIR_MASK;
  306. atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR, reg);
  307. return 0;
  308. }
  309. static void atmel_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  310. {
  311. struct atmel_pioctrl *atmel_pioctrl = gpiochip_get_data(chip);
  312. struct atmel_pin *pin = atmel_pioctrl->pins[offset];
  313. atmel_gpio_write(atmel_pioctrl, pin->bank,
  314. val ? ATMEL_PIO_SODR : ATMEL_PIO_CODR,
  315. BIT(pin->line));
  316. }
  317. static struct gpio_chip atmel_gpio_chip = {
  318. .direction_input = atmel_gpio_direction_input,
  319. .get = atmel_gpio_get,
  320. .direction_output = atmel_gpio_direction_output,
  321. .set = atmel_gpio_set,
  322. .to_irq = atmel_gpio_to_irq,
  323. .base = 0,
  324. };
  325. /* --- PINCTRL --- */
  326. static unsigned int atmel_pin_config_read(struct pinctrl_dev *pctldev,
  327. unsigned pin_id)
  328. {
  329. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  330. unsigned bank = atmel_pioctrl->pins[pin_id]->bank;
  331. unsigned line = atmel_pioctrl->pins[pin_id]->line;
  332. void __iomem *addr = atmel_pioctrl->reg_base
  333. + bank * ATMEL_PIO_BANK_OFFSET;
  334. writel_relaxed(BIT(line), addr + ATMEL_PIO_MSKR);
  335. /* Have to set MSKR first, to access the right pin CFGR. */
  336. wmb();
  337. return readl_relaxed(addr + ATMEL_PIO_CFGR);
  338. }
  339. static void atmel_pin_config_write(struct pinctrl_dev *pctldev,
  340. unsigned pin_id, u32 conf)
  341. {
  342. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  343. unsigned bank = atmel_pioctrl->pins[pin_id]->bank;
  344. unsigned line = atmel_pioctrl->pins[pin_id]->line;
  345. void __iomem *addr = atmel_pioctrl->reg_base
  346. + bank * ATMEL_PIO_BANK_OFFSET;
  347. writel_relaxed(BIT(line), addr + ATMEL_PIO_MSKR);
  348. /* Have to set MSKR first, to access the right pin CFGR. */
  349. wmb();
  350. writel_relaxed(conf, addr + ATMEL_PIO_CFGR);
  351. }
  352. static int atmel_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  353. {
  354. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  355. return atmel_pioctrl->npins;
  356. }
  357. static const char *atmel_pctl_get_group_name(struct pinctrl_dev *pctldev,
  358. unsigned selector)
  359. {
  360. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  361. return atmel_pioctrl->groups[selector].name;
  362. }
  363. static int atmel_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  364. unsigned selector, const unsigned **pins,
  365. unsigned *num_pins)
  366. {
  367. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  368. *pins = (unsigned *)&atmel_pioctrl->groups[selector].pin;
  369. *num_pins = 1;
  370. return 0;
  371. }
  372. static struct atmel_group *
  373. atmel_pctl_find_group_by_pin(struct pinctrl_dev *pctldev, unsigned pin)
  374. {
  375. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  376. int i;
  377. for (i = 0; i < atmel_pioctrl->npins; i++) {
  378. struct atmel_group *grp = atmel_pioctrl->groups + i;
  379. if (grp->pin == pin)
  380. return grp;
  381. }
  382. return NULL;
  383. }
  384. static int atmel_pctl_xlate_pinfunc(struct pinctrl_dev *pctldev,
  385. struct device_node *np,
  386. u32 pinfunc, const char **grp_name,
  387. const char **func_name)
  388. {
  389. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  390. unsigned pin_id, func_id;
  391. struct atmel_group *grp;
  392. pin_id = ATMEL_GET_PIN_NO(pinfunc);
  393. func_id = ATMEL_GET_PIN_FUNC(pinfunc);
  394. if (func_id >= ARRAY_SIZE(atmel_functions))
  395. return -EINVAL;
  396. *func_name = atmel_functions[func_id];
  397. grp = atmel_pctl_find_group_by_pin(pctldev, pin_id);
  398. if (!grp)
  399. return -EINVAL;
  400. *grp_name = grp->name;
  401. atmel_pioctrl->pins[pin_id]->mux = func_id;
  402. atmel_pioctrl->pins[pin_id]->ioset = ATMEL_GET_PIN_IOSET(pinfunc);
  403. /* Want the device name not the group one. */
  404. if (np->parent == atmel_pioctrl->node)
  405. atmel_pioctrl->pins[pin_id]->device = np->name;
  406. else
  407. atmel_pioctrl->pins[pin_id]->device = np->parent->name;
  408. return 0;
  409. }
  410. static int atmel_pctl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  411. struct device_node *np,
  412. struct pinctrl_map **map,
  413. unsigned *reserved_maps,
  414. unsigned *num_maps)
  415. {
  416. unsigned num_pins, num_configs, reserve;
  417. unsigned long *configs;
  418. struct property *pins;
  419. u32 pinfunc;
  420. int ret, i;
  421. pins = of_find_property(np, "pinmux", NULL);
  422. if (!pins)
  423. return -EINVAL;
  424. ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
  425. &num_configs);
  426. if (ret < 0) {
  427. dev_err(pctldev->dev, "%pOF: could not parse node property\n",
  428. np);
  429. return ret;
  430. }
  431. num_pins = pins->length / sizeof(u32);
  432. if (!num_pins) {
  433. dev_err(pctldev->dev, "no pins found in node %pOF\n", np);
  434. ret = -EINVAL;
  435. goto exit;
  436. }
  437. /*
  438. * Reserve maps, at least there is a mux map and an optional conf
  439. * map for each pin.
  440. */
  441. reserve = 1;
  442. if (num_configs)
  443. reserve++;
  444. reserve *= num_pins;
  445. ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps,
  446. reserve);
  447. if (ret < 0)
  448. goto exit;
  449. for (i = 0; i < num_pins; i++) {
  450. const char *group, *func;
  451. ret = of_property_read_u32_index(np, "pinmux", i, &pinfunc);
  452. if (ret)
  453. goto exit;
  454. ret = atmel_pctl_xlate_pinfunc(pctldev, np, pinfunc, &group,
  455. &func);
  456. if (ret)
  457. goto exit;
  458. pinctrl_utils_add_map_mux(pctldev, map, reserved_maps, num_maps,
  459. group, func);
  460. if (num_configs) {
  461. ret = pinctrl_utils_add_map_configs(pctldev, map,
  462. reserved_maps, num_maps, group,
  463. configs, num_configs,
  464. PIN_MAP_TYPE_CONFIGS_GROUP);
  465. if (ret < 0)
  466. goto exit;
  467. }
  468. }
  469. exit:
  470. kfree(configs);
  471. return ret;
  472. }
  473. static int atmel_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  474. struct device_node *np_config,
  475. struct pinctrl_map **map,
  476. unsigned *num_maps)
  477. {
  478. struct device_node *np;
  479. unsigned reserved_maps;
  480. int ret;
  481. *map = NULL;
  482. *num_maps = 0;
  483. reserved_maps = 0;
  484. /*
  485. * If all the pins of a device have the same configuration (or no one),
  486. * it is useless to add a subnode, so directly parse node referenced by
  487. * phandle.
  488. */
  489. ret = atmel_pctl_dt_subnode_to_map(pctldev, np_config, map,
  490. &reserved_maps, num_maps);
  491. if (ret) {
  492. for_each_child_of_node(np_config, np) {
  493. ret = atmel_pctl_dt_subnode_to_map(pctldev, np, map,
  494. &reserved_maps, num_maps);
  495. if (ret < 0) {
  496. of_node_put(np);
  497. break;
  498. }
  499. }
  500. }
  501. if (ret < 0) {
  502. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  503. dev_err(pctldev->dev, "can't create maps for node %pOF\n",
  504. np_config);
  505. }
  506. return ret;
  507. }
  508. static const struct pinctrl_ops atmel_pctlops = {
  509. .get_groups_count = atmel_pctl_get_groups_count,
  510. .get_group_name = atmel_pctl_get_group_name,
  511. .get_group_pins = atmel_pctl_get_group_pins,
  512. .dt_node_to_map = atmel_pctl_dt_node_to_map,
  513. .dt_free_map = pinctrl_utils_free_map,
  514. };
  515. static int atmel_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  516. {
  517. return ARRAY_SIZE(atmel_functions);
  518. }
  519. static const char *atmel_pmx_get_function_name(struct pinctrl_dev *pctldev,
  520. unsigned selector)
  521. {
  522. return atmel_functions[selector];
  523. }
  524. static int atmel_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  525. unsigned selector,
  526. const char * const **groups,
  527. unsigned * const num_groups)
  528. {
  529. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  530. *groups = atmel_pioctrl->group_names;
  531. *num_groups = atmel_pioctrl->npins;
  532. return 0;
  533. }
  534. static int atmel_pmx_set_mux(struct pinctrl_dev *pctldev,
  535. unsigned function,
  536. unsigned group)
  537. {
  538. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  539. unsigned pin;
  540. u32 conf;
  541. dev_dbg(pctldev->dev, "enable function %s group %s\n",
  542. atmel_functions[function], atmel_pioctrl->groups[group].name);
  543. pin = atmel_pioctrl->groups[group].pin;
  544. conf = atmel_pin_config_read(pctldev, pin);
  545. conf &= (~ATMEL_PIO_CFGR_FUNC_MASK);
  546. conf |= (function & ATMEL_PIO_CFGR_FUNC_MASK);
  547. dev_dbg(pctldev->dev, "pin: %u, conf: 0x%08x\n", pin, conf);
  548. atmel_pin_config_write(pctldev, pin, conf);
  549. return 0;
  550. }
  551. static const struct pinmux_ops atmel_pmxops = {
  552. .get_functions_count = atmel_pmx_get_functions_count,
  553. .get_function_name = atmel_pmx_get_function_name,
  554. .get_function_groups = atmel_pmx_get_function_groups,
  555. .set_mux = atmel_pmx_set_mux,
  556. };
  557. static int atmel_conf_pin_config_group_get(struct pinctrl_dev *pctldev,
  558. unsigned group,
  559. unsigned long *config)
  560. {
  561. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  562. unsigned param = pinconf_to_config_param(*config), arg = 0;
  563. struct atmel_group *grp = atmel_pioctrl->groups + group;
  564. unsigned pin_id = grp->pin;
  565. u32 res;
  566. res = atmel_pin_config_read(pctldev, pin_id);
  567. switch (param) {
  568. case PIN_CONFIG_BIAS_PULL_UP:
  569. if (!(res & ATMEL_PIO_PUEN_MASK))
  570. return -EINVAL;
  571. arg = 1;
  572. break;
  573. case PIN_CONFIG_BIAS_PULL_DOWN:
  574. if ((res & ATMEL_PIO_PUEN_MASK) ||
  575. (!(res & ATMEL_PIO_PDEN_MASK)))
  576. return -EINVAL;
  577. arg = 1;
  578. break;
  579. case PIN_CONFIG_BIAS_DISABLE:
  580. if ((res & ATMEL_PIO_PUEN_MASK) ||
  581. ((res & ATMEL_PIO_PDEN_MASK)))
  582. return -EINVAL;
  583. arg = 1;
  584. break;
  585. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  586. if (!(res & ATMEL_PIO_OPD_MASK))
  587. return -EINVAL;
  588. arg = 1;
  589. break;
  590. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  591. if (!(res & ATMEL_PIO_SCHMITT_MASK))
  592. return -EINVAL;
  593. arg = 1;
  594. break;
  595. case ATMEL_PIN_CONFIG_DRIVE_STRENGTH:
  596. if (!(res & ATMEL_PIO_DRVSTR_MASK))
  597. return -EINVAL;
  598. arg = (res & ATMEL_PIO_DRVSTR_MASK) >> ATMEL_PIO_DRVSTR_OFFSET;
  599. break;
  600. default:
  601. return -ENOTSUPP;
  602. }
  603. *config = pinconf_to_config_packed(param, arg);
  604. return 0;
  605. }
  606. static int atmel_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
  607. unsigned group,
  608. unsigned long *configs,
  609. unsigned num_configs)
  610. {
  611. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  612. struct atmel_group *grp = atmel_pioctrl->groups + group;
  613. unsigned bank, pin, pin_id = grp->pin;
  614. u32 mask, conf = 0;
  615. int i;
  616. conf = atmel_pin_config_read(pctldev, pin_id);
  617. for (i = 0; i < num_configs; i++) {
  618. unsigned param = pinconf_to_config_param(configs[i]);
  619. unsigned arg = pinconf_to_config_argument(configs[i]);
  620. dev_dbg(pctldev->dev, "%s: pin=%u, config=0x%lx\n",
  621. __func__, pin_id, configs[i]);
  622. switch (param) {
  623. case PIN_CONFIG_BIAS_DISABLE:
  624. conf &= (~ATMEL_PIO_PUEN_MASK);
  625. conf &= (~ATMEL_PIO_PDEN_MASK);
  626. break;
  627. case PIN_CONFIG_BIAS_PULL_UP:
  628. conf |= ATMEL_PIO_PUEN_MASK;
  629. conf &= (~ATMEL_PIO_PDEN_MASK);
  630. break;
  631. case PIN_CONFIG_BIAS_PULL_DOWN:
  632. conf |= ATMEL_PIO_PDEN_MASK;
  633. conf &= (~ATMEL_PIO_PUEN_MASK);
  634. break;
  635. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  636. if (arg == 0)
  637. conf &= (~ATMEL_PIO_OPD_MASK);
  638. else
  639. conf |= ATMEL_PIO_OPD_MASK;
  640. break;
  641. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  642. if (arg == 0)
  643. conf |= ATMEL_PIO_SCHMITT_MASK;
  644. else
  645. conf &= (~ATMEL_PIO_SCHMITT_MASK);
  646. break;
  647. case PIN_CONFIG_INPUT_DEBOUNCE:
  648. if (arg == 0) {
  649. conf &= (~ATMEL_PIO_IFEN_MASK);
  650. conf &= (~ATMEL_PIO_IFSCEN_MASK);
  651. } else {
  652. /*
  653. * We don't care about the debounce value for several reasons:
  654. * - can't have different debounce periods inside a same group,
  655. * - the register to configure this period is a secure register.
  656. * The debouncing filter can filter a pulse with a duration of less
  657. * than 1/2 slow clock period.
  658. */
  659. conf |= ATMEL_PIO_IFEN_MASK;
  660. conf |= ATMEL_PIO_IFSCEN_MASK;
  661. }
  662. break;
  663. case PIN_CONFIG_OUTPUT:
  664. conf |= ATMEL_PIO_DIR_MASK;
  665. bank = ATMEL_PIO_BANK(pin_id);
  666. pin = ATMEL_PIO_LINE(pin_id);
  667. mask = 1 << pin;
  668. if (arg == 0) {
  669. writel_relaxed(mask, atmel_pioctrl->reg_base +
  670. bank * ATMEL_PIO_BANK_OFFSET +
  671. ATMEL_PIO_CODR);
  672. } else {
  673. writel_relaxed(mask, atmel_pioctrl->reg_base +
  674. bank * ATMEL_PIO_BANK_OFFSET +
  675. ATMEL_PIO_SODR);
  676. }
  677. break;
  678. case ATMEL_PIN_CONFIG_DRIVE_STRENGTH:
  679. switch (arg) {
  680. case ATMEL_PIO_DRVSTR_LO:
  681. case ATMEL_PIO_DRVSTR_ME:
  682. case ATMEL_PIO_DRVSTR_HI:
  683. conf &= (~ATMEL_PIO_DRVSTR_MASK);
  684. conf |= arg << ATMEL_PIO_DRVSTR_OFFSET;
  685. break;
  686. default:
  687. dev_warn(pctldev->dev, "drive strength not updated (incorrect value)\n");
  688. }
  689. break;
  690. default:
  691. dev_warn(pctldev->dev,
  692. "unsupported configuration parameter: %u\n",
  693. param);
  694. continue;
  695. }
  696. }
  697. dev_dbg(pctldev->dev, "%s: reg=0x%08x\n", __func__, conf);
  698. atmel_pin_config_write(pctldev, pin_id, conf);
  699. return 0;
  700. }
  701. static void atmel_conf_pin_config_dbg_show(struct pinctrl_dev *pctldev,
  702. struct seq_file *s, unsigned pin_id)
  703. {
  704. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  705. u32 conf;
  706. if (!atmel_pioctrl->pins[pin_id]->device)
  707. return;
  708. if (atmel_pioctrl->pins[pin_id])
  709. seq_printf(s, " (%s, ioset %u) ",
  710. atmel_pioctrl->pins[pin_id]->device,
  711. atmel_pioctrl->pins[pin_id]->ioset);
  712. conf = atmel_pin_config_read(pctldev, pin_id);
  713. if (conf & ATMEL_PIO_PUEN_MASK)
  714. seq_printf(s, "%s ", "pull-up");
  715. if (conf & ATMEL_PIO_PDEN_MASK)
  716. seq_printf(s, "%s ", "pull-down");
  717. if (conf & ATMEL_PIO_IFEN_MASK)
  718. seq_printf(s, "%s ", "debounce");
  719. if (conf & ATMEL_PIO_OPD_MASK)
  720. seq_printf(s, "%s ", "open-drain");
  721. if (conf & ATMEL_PIO_SCHMITT_MASK)
  722. seq_printf(s, "%s ", "schmitt");
  723. if (conf & ATMEL_PIO_DRVSTR_MASK) {
  724. switch ((conf & ATMEL_PIO_DRVSTR_MASK) >> ATMEL_PIO_DRVSTR_OFFSET) {
  725. case ATMEL_PIO_DRVSTR_ME:
  726. seq_printf(s, "%s ", "medium-drive");
  727. break;
  728. case ATMEL_PIO_DRVSTR_HI:
  729. seq_printf(s, "%s ", "high-drive");
  730. break;
  731. /* ATMEL_PIO_DRVSTR_LO and 0 which is the default value at reset */
  732. default:
  733. seq_printf(s, "%s ", "low-drive");
  734. }
  735. }
  736. }
  737. static const struct pinconf_ops atmel_confops = {
  738. .pin_config_group_get = atmel_conf_pin_config_group_get,
  739. .pin_config_group_set = atmel_conf_pin_config_group_set,
  740. .pin_config_dbg_show = atmel_conf_pin_config_dbg_show,
  741. };
  742. static struct pinctrl_desc atmel_pinctrl_desc = {
  743. .name = "atmel_pinctrl",
  744. .confops = &atmel_confops,
  745. .pctlops = &atmel_pctlops,
  746. .pmxops = &atmel_pmxops,
  747. };
  748. static int __maybe_unused atmel_pctrl_suspend(struct device *dev)
  749. {
  750. struct platform_device *pdev = to_platform_device(dev);
  751. struct atmel_pioctrl *atmel_pioctrl = platform_get_drvdata(pdev);
  752. int i, j;
  753. /*
  754. * For each bank, save IMR to restore it later and disable all GPIO
  755. * interrupts excepting the ones marked as wakeup sources.
  756. */
  757. for (i = 0; i < atmel_pioctrl->nbanks; i++) {
  758. atmel_pioctrl->pm_suspend_backup[i].imr =
  759. atmel_gpio_read(atmel_pioctrl, i, ATMEL_PIO_IMR);
  760. atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_IDR,
  761. ~atmel_pioctrl->pm_wakeup_sources[i]);
  762. atmel_pioctrl->pm_suspend_backup[i].odsr =
  763. atmel_gpio_read(atmel_pioctrl, i, ATMEL_PIO_ODSR);
  764. for (j = 0; j < ATMEL_PIO_NPINS_PER_BANK; j++) {
  765. atmel_gpio_write(atmel_pioctrl, i,
  766. ATMEL_PIO_MSKR, BIT(j));
  767. atmel_pioctrl->pm_suspend_backup[i].cfgr[j] =
  768. atmel_gpio_read(atmel_pioctrl, i,
  769. ATMEL_PIO_CFGR);
  770. }
  771. }
  772. return 0;
  773. }
  774. static int __maybe_unused atmel_pctrl_resume(struct device *dev)
  775. {
  776. struct platform_device *pdev = to_platform_device(dev);
  777. struct atmel_pioctrl *atmel_pioctrl = platform_get_drvdata(pdev);
  778. int i, j;
  779. for (i = 0; i < atmel_pioctrl->nbanks; i++) {
  780. atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_IER,
  781. atmel_pioctrl->pm_suspend_backup[i].imr);
  782. atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_SODR,
  783. atmel_pioctrl->pm_suspend_backup[i].odsr);
  784. for (j = 0; j < ATMEL_PIO_NPINS_PER_BANK; j++) {
  785. atmel_gpio_write(atmel_pioctrl, i,
  786. ATMEL_PIO_MSKR, BIT(j));
  787. atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_CFGR,
  788. atmel_pioctrl->pm_suspend_backup[i].cfgr[j]);
  789. }
  790. }
  791. return 0;
  792. }
  793. static const struct dev_pm_ops atmel_pctrl_pm_ops = {
  794. SET_SYSTEM_SLEEP_PM_OPS(atmel_pctrl_suspend, atmel_pctrl_resume)
  795. };
  796. /*
  797. * The number of banks can be different from a SoC to another one.
  798. * We can have up to 16 banks.
  799. */
  800. static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
  801. .nbanks = 4,
  802. };
  803. static const struct of_device_id atmel_pctrl_of_match[] = {
  804. {
  805. .compatible = "atmel,sama5d2-pinctrl",
  806. .data = &atmel_sama5d2_pioctrl_data,
  807. }, {
  808. /* sentinel */
  809. }
  810. };
  811. static int atmel_pinctrl_probe(struct platform_device *pdev)
  812. {
  813. struct device *dev = &pdev->dev;
  814. struct pinctrl_pin_desc *pin_desc;
  815. const char **group_names;
  816. const struct of_device_id *match;
  817. int i, ret;
  818. struct resource *res;
  819. struct atmel_pioctrl *atmel_pioctrl;
  820. const struct atmel_pioctrl_data *atmel_pioctrl_data;
  821. atmel_pioctrl = devm_kzalloc(dev, sizeof(*atmel_pioctrl), GFP_KERNEL);
  822. if (!atmel_pioctrl)
  823. return -ENOMEM;
  824. atmel_pioctrl->dev = dev;
  825. atmel_pioctrl->node = dev->of_node;
  826. platform_set_drvdata(pdev, atmel_pioctrl);
  827. match = of_match_node(atmel_pctrl_of_match, dev->of_node);
  828. if (!match) {
  829. dev_err(dev, "unknown compatible string\n");
  830. return -ENODEV;
  831. }
  832. atmel_pioctrl_data = match->data;
  833. atmel_pioctrl->nbanks = atmel_pioctrl_data->nbanks;
  834. atmel_pioctrl->npins = atmel_pioctrl->nbanks * ATMEL_PIO_NPINS_PER_BANK;
  835. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  836. atmel_pioctrl->reg_base = devm_ioremap_resource(dev, res);
  837. if (IS_ERR(atmel_pioctrl->reg_base))
  838. return -EINVAL;
  839. atmel_pioctrl->clk = devm_clk_get(dev, NULL);
  840. if (IS_ERR(atmel_pioctrl->clk)) {
  841. dev_err(dev, "failed to get clock\n");
  842. return PTR_ERR(atmel_pioctrl->clk);
  843. }
  844. atmel_pioctrl->pins = devm_kcalloc(dev,
  845. atmel_pioctrl->npins,
  846. sizeof(*atmel_pioctrl->pins),
  847. GFP_KERNEL);
  848. if (!atmel_pioctrl->pins)
  849. return -ENOMEM;
  850. pin_desc = devm_kcalloc(dev, atmel_pioctrl->npins, sizeof(*pin_desc),
  851. GFP_KERNEL);
  852. if (!pin_desc)
  853. return -ENOMEM;
  854. atmel_pinctrl_desc.pins = pin_desc;
  855. atmel_pinctrl_desc.npins = atmel_pioctrl->npins;
  856. atmel_pinctrl_desc.num_custom_params = ARRAY_SIZE(atmel_custom_bindings);
  857. atmel_pinctrl_desc.custom_params = atmel_custom_bindings;
  858. /* One pin is one group since a pin can achieve all functions. */
  859. group_names = devm_kcalloc(dev,
  860. atmel_pioctrl->npins, sizeof(*group_names),
  861. GFP_KERNEL);
  862. if (!group_names)
  863. return -ENOMEM;
  864. atmel_pioctrl->group_names = group_names;
  865. atmel_pioctrl->groups = devm_kcalloc(&pdev->dev,
  866. atmel_pioctrl->npins, sizeof(*atmel_pioctrl->groups),
  867. GFP_KERNEL);
  868. if (!atmel_pioctrl->groups)
  869. return -ENOMEM;
  870. for (i = 0 ; i < atmel_pioctrl->npins; i++) {
  871. struct atmel_group *group = atmel_pioctrl->groups + i;
  872. unsigned bank = ATMEL_PIO_BANK(i);
  873. unsigned line = ATMEL_PIO_LINE(i);
  874. atmel_pioctrl->pins[i] = devm_kzalloc(dev,
  875. sizeof(**atmel_pioctrl->pins), GFP_KERNEL);
  876. if (!atmel_pioctrl->pins[i])
  877. return -ENOMEM;
  878. atmel_pioctrl->pins[i]->pin_id = i;
  879. atmel_pioctrl->pins[i]->bank = bank;
  880. atmel_pioctrl->pins[i]->line = line;
  881. pin_desc[i].number = i;
  882. /* Pin naming convention: P(bank_name)(bank_pin_number). */
  883. pin_desc[i].name = kasprintf(GFP_KERNEL, "P%c%d",
  884. bank + 'A', line);
  885. group->name = group_names[i] = pin_desc[i].name;
  886. group->pin = pin_desc[i].number;
  887. dev_dbg(dev, "pin_id=%u, bank=%u, line=%u", i, bank, line);
  888. }
  889. atmel_pioctrl->gpio_chip = &atmel_gpio_chip;
  890. atmel_pioctrl->gpio_chip->of_node = dev->of_node;
  891. atmel_pioctrl->gpio_chip->ngpio = atmel_pioctrl->npins;
  892. atmel_pioctrl->gpio_chip->label = dev_name(dev);
  893. atmel_pioctrl->gpio_chip->parent = dev;
  894. atmel_pioctrl->gpio_chip->names = atmel_pioctrl->group_names;
  895. atmel_pioctrl->pm_wakeup_sources = devm_kcalloc(dev,
  896. atmel_pioctrl->nbanks,
  897. sizeof(*atmel_pioctrl->pm_wakeup_sources),
  898. GFP_KERNEL);
  899. if (!atmel_pioctrl->pm_wakeup_sources)
  900. return -ENOMEM;
  901. atmel_pioctrl->pm_suspend_backup = devm_kcalloc(dev,
  902. atmel_pioctrl->nbanks,
  903. sizeof(*atmel_pioctrl->pm_suspend_backup),
  904. GFP_KERNEL);
  905. if (!atmel_pioctrl->pm_suspend_backup)
  906. return -ENOMEM;
  907. atmel_pioctrl->irqs = devm_kcalloc(dev,
  908. atmel_pioctrl->nbanks,
  909. sizeof(*atmel_pioctrl->irqs),
  910. GFP_KERNEL);
  911. if (!atmel_pioctrl->irqs)
  912. return -ENOMEM;
  913. /* There is one controller but each bank has its own irq line. */
  914. for (i = 0; i < atmel_pioctrl->nbanks; i++) {
  915. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  916. if (!res) {
  917. dev_err(dev, "missing irq resource for group %c\n",
  918. 'A' + i);
  919. return -EINVAL;
  920. }
  921. atmel_pioctrl->irqs[i] = res->start;
  922. irq_set_chained_handler(res->start, atmel_gpio_irq_handler);
  923. irq_set_handler_data(res->start, atmel_pioctrl);
  924. dev_dbg(dev, "bank %i: irq=%pr\n", i, res);
  925. }
  926. atmel_pioctrl->irq_domain = irq_domain_add_linear(dev->of_node,
  927. atmel_pioctrl->gpio_chip->ngpio,
  928. &irq_domain_simple_ops, NULL);
  929. if (!atmel_pioctrl->irq_domain) {
  930. dev_err(dev, "can't add the irq domain\n");
  931. return -ENODEV;
  932. }
  933. atmel_pioctrl->irq_domain->name = "atmel gpio";
  934. for (i = 0; i < atmel_pioctrl->npins; i++) {
  935. int irq = irq_create_mapping(atmel_pioctrl->irq_domain, i);
  936. irq_set_chip_and_handler(irq, &atmel_gpio_irq_chip,
  937. handle_simple_irq);
  938. irq_set_chip_data(irq, atmel_pioctrl);
  939. dev_dbg(dev,
  940. "atmel gpio irq domain: hwirq: %d, linux irq: %d\n",
  941. i, irq);
  942. }
  943. ret = clk_prepare_enable(atmel_pioctrl->clk);
  944. if (ret) {
  945. dev_err(dev, "failed to prepare and enable clock\n");
  946. goto clk_prepare_enable_error;
  947. }
  948. atmel_pioctrl->pinctrl_dev = devm_pinctrl_register(&pdev->dev,
  949. &atmel_pinctrl_desc,
  950. atmel_pioctrl);
  951. if (IS_ERR(atmel_pioctrl->pinctrl_dev)) {
  952. ret = PTR_ERR(atmel_pioctrl->pinctrl_dev);
  953. dev_err(dev, "pinctrl registration failed\n");
  954. goto clk_unprep;
  955. }
  956. ret = gpiochip_add_data(atmel_pioctrl->gpio_chip, atmel_pioctrl);
  957. if (ret) {
  958. dev_err(dev, "failed to add gpiochip\n");
  959. goto clk_unprep;
  960. }
  961. ret = gpiochip_add_pin_range(atmel_pioctrl->gpio_chip, dev_name(dev),
  962. 0, 0, atmel_pioctrl->gpio_chip->ngpio);
  963. if (ret) {
  964. dev_err(dev, "failed to add gpio pin range\n");
  965. goto gpiochip_add_pin_range_error;
  966. }
  967. dev_info(&pdev->dev, "atmel pinctrl initialized\n");
  968. return 0;
  969. gpiochip_add_pin_range_error:
  970. gpiochip_remove(atmel_pioctrl->gpio_chip);
  971. clk_unprep:
  972. clk_disable_unprepare(atmel_pioctrl->clk);
  973. clk_prepare_enable_error:
  974. irq_domain_remove(atmel_pioctrl->irq_domain);
  975. return ret;
  976. }
  977. static struct platform_driver atmel_pinctrl_driver = {
  978. .driver = {
  979. .name = "pinctrl-at91-pio4",
  980. .of_match_table = atmel_pctrl_of_match,
  981. .pm = &atmel_pctrl_pm_ops,
  982. .suppress_bind_attrs = true,
  983. },
  984. .probe = atmel_pinctrl_probe,
  985. };
  986. builtin_platform_driver(atmel_pinctrl_driver);