pinctrl-at91-pio4.c 36 KB

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