pinctrl-at91-pio4.c 35 KB

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