pinctrl-at91-pio4.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  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 void 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. }
  343. static void atmel_gpio_set_multiple(struct gpio_chip *chip, unsigned long *mask,
  344. unsigned long *bits)
  345. {
  346. struct atmel_pioctrl *atmel_pioctrl = gpiochip_get_data(chip);
  347. unsigned int bank;
  348. for (bank = 0; bank < atmel_pioctrl->nbanks; bank++) {
  349. unsigned int bitmask;
  350. unsigned int word = bank;
  351. /*
  352. * On a 64-bit platform, BITS_PER_LONG is 64 so it is necessary to iterate over
  353. * two 32bit words to handle the whole bitmask
  354. */
  355. #if ATMEL_PIO_NPINS_PER_BANK != BITS_PER_LONG
  356. word = BIT_WORD(bank * ATMEL_PIO_NPINS_PER_BANK);
  357. #endif
  358. if (!mask[word])
  359. continue;
  360. bitmask = mask[word] & bits[word];
  361. atmel_gpio_write(atmel_pioctrl, bank, ATMEL_PIO_SODR, bitmask);
  362. bitmask = mask[word] & ~bits[word];
  363. atmel_gpio_write(atmel_pioctrl, bank, ATMEL_PIO_CODR, bitmask);
  364. #if ATMEL_PIO_NPINS_PER_BANK != BITS_PER_LONG
  365. mask[word] >>= ATMEL_PIO_NPINS_PER_BANK;
  366. bits[word] >>= ATMEL_PIO_NPINS_PER_BANK;
  367. #endif
  368. }
  369. }
  370. static struct gpio_chip atmel_gpio_chip = {
  371. .direction_input = atmel_gpio_direction_input,
  372. .get = atmel_gpio_get,
  373. .get_multiple = atmel_gpio_get_multiple,
  374. .direction_output = atmel_gpio_direction_output,
  375. .set = atmel_gpio_set,
  376. .set_multiple = atmel_gpio_set_multiple,
  377. .to_irq = atmel_gpio_to_irq,
  378. .base = 0,
  379. };
  380. /* --- PINCTRL --- */
  381. static unsigned int atmel_pin_config_read(struct pinctrl_dev *pctldev,
  382. unsigned int pin_id)
  383. {
  384. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  385. unsigned int bank = atmel_pioctrl->pins[pin_id]->bank;
  386. unsigned int line = atmel_pioctrl->pins[pin_id]->line;
  387. void __iomem *addr = atmel_pioctrl->reg_base
  388. + bank * ATMEL_PIO_BANK_OFFSET;
  389. writel_relaxed(BIT(line), addr + ATMEL_PIO_MSKR);
  390. /* Have to set MSKR first, to access the right pin CFGR. */
  391. wmb();
  392. return readl_relaxed(addr + ATMEL_PIO_CFGR);
  393. }
  394. static void atmel_pin_config_write(struct pinctrl_dev *pctldev,
  395. unsigned int pin_id, u32 conf)
  396. {
  397. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  398. unsigned int bank = atmel_pioctrl->pins[pin_id]->bank;
  399. unsigned int line = atmel_pioctrl->pins[pin_id]->line;
  400. void __iomem *addr = atmel_pioctrl->reg_base
  401. + bank * ATMEL_PIO_BANK_OFFSET;
  402. writel_relaxed(BIT(line), addr + ATMEL_PIO_MSKR);
  403. /* Have to set MSKR first, to access the right pin CFGR. */
  404. wmb();
  405. writel_relaxed(conf, addr + ATMEL_PIO_CFGR);
  406. }
  407. static int atmel_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  408. {
  409. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  410. return atmel_pioctrl->npins;
  411. }
  412. static const char *atmel_pctl_get_group_name(struct pinctrl_dev *pctldev,
  413. unsigned int selector)
  414. {
  415. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  416. return atmel_pioctrl->groups[selector].name;
  417. }
  418. static int atmel_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  419. unsigned int selector,
  420. const unsigned int **pins,
  421. unsigned int *num_pins)
  422. {
  423. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  424. *pins = (unsigned int *)&atmel_pioctrl->groups[selector].pin;
  425. *num_pins = 1;
  426. return 0;
  427. }
  428. static struct atmel_group *
  429. atmel_pctl_find_group_by_pin(struct pinctrl_dev *pctldev, unsigned int pin)
  430. {
  431. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  432. int i;
  433. for (i = 0; i < atmel_pioctrl->npins; i++) {
  434. struct atmel_group *grp = atmel_pioctrl->groups + i;
  435. if (grp->pin == pin)
  436. return grp;
  437. }
  438. return NULL;
  439. }
  440. static int atmel_pctl_xlate_pinfunc(struct pinctrl_dev *pctldev,
  441. struct device_node *np,
  442. u32 pinfunc, const char **grp_name,
  443. const char **func_name)
  444. {
  445. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  446. unsigned int pin_id, func_id;
  447. struct atmel_group *grp;
  448. pin_id = ATMEL_GET_PIN_NO(pinfunc);
  449. func_id = ATMEL_GET_PIN_FUNC(pinfunc);
  450. if (func_id >= ARRAY_SIZE(atmel_functions))
  451. return -EINVAL;
  452. *func_name = atmel_functions[func_id];
  453. grp = atmel_pctl_find_group_by_pin(pctldev, pin_id);
  454. if (!grp)
  455. return -EINVAL;
  456. *grp_name = grp->name;
  457. atmel_pioctrl->pins[pin_id]->mux = func_id;
  458. atmel_pioctrl->pins[pin_id]->ioset = ATMEL_GET_PIN_IOSET(pinfunc);
  459. /* Want the device name not the group one. */
  460. if (np->parent == atmel_pioctrl->node)
  461. atmel_pioctrl->pins[pin_id]->device = np->name;
  462. else
  463. atmel_pioctrl->pins[pin_id]->device = np->parent->name;
  464. return 0;
  465. }
  466. static int atmel_pctl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  467. struct device_node *np,
  468. struct pinctrl_map **map,
  469. unsigned int *reserved_maps,
  470. unsigned int *num_maps)
  471. {
  472. unsigned int num_pins, num_configs, reserve;
  473. unsigned long *configs;
  474. struct property *pins;
  475. u32 pinfunc;
  476. int ret, i;
  477. pins = of_find_property(np, "pinmux", NULL);
  478. if (!pins)
  479. return -EINVAL;
  480. ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
  481. &num_configs);
  482. if (ret < 0) {
  483. dev_err(pctldev->dev, "%pOF: could not parse node property\n",
  484. np);
  485. return ret;
  486. }
  487. num_pins = pins->length / sizeof(u32);
  488. if (!num_pins) {
  489. dev_err(pctldev->dev, "no pins found in node %pOF\n", np);
  490. ret = -EINVAL;
  491. goto exit;
  492. }
  493. /*
  494. * Reserve maps, at least there is a mux map and an optional conf
  495. * map for each pin.
  496. */
  497. reserve = 1;
  498. if (num_configs)
  499. reserve++;
  500. reserve *= num_pins;
  501. ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps,
  502. reserve);
  503. if (ret < 0)
  504. goto exit;
  505. for (i = 0; i < num_pins; i++) {
  506. const char *group, *func;
  507. ret = of_property_read_u32_index(np, "pinmux", i, &pinfunc);
  508. if (ret)
  509. goto exit;
  510. ret = atmel_pctl_xlate_pinfunc(pctldev, np, pinfunc, &group,
  511. &func);
  512. if (ret)
  513. goto exit;
  514. pinctrl_utils_add_map_mux(pctldev, map, reserved_maps, num_maps,
  515. group, func);
  516. if (num_configs) {
  517. ret = pinctrl_utils_add_map_configs(pctldev, map,
  518. reserved_maps, num_maps, group,
  519. configs, num_configs,
  520. PIN_MAP_TYPE_CONFIGS_GROUP);
  521. if (ret < 0)
  522. goto exit;
  523. }
  524. }
  525. exit:
  526. kfree(configs);
  527. return ret;
  528. }
  529. static int atmel_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  530. struct device_node *np_config,
  531. struct pinctrl_map **map,
  532. unsigned int *num_maps)
  533. {
  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_scoped(np_config, np) {
  548. ret = atmel_pctl_dt_subnode_to_map(pctldev, np, map,
  549. &reserved_maps, num_maps);
  550. if (ret < 0)
  551. break;
  552. }
  553. }
  554. if (ret < 0) {
  555. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  556. dev_err(pctldev->dev, "can't create maps for node %pOF\n",
  557. np_config);
  558. }
  559. return ret;
  560. }
  561. static const struct pinctrl_ops atmel_pctlops = {
  562. .get_groups_count = atmel_pctl_get_groups_count,
  563. .get_group_name = atmel_pctl_get_group_name,
  564. .get_group_pins = atmel_pctl_get_group_pins,
  565. .dt_node_to_map = atmel_pctl_dt_node_to_map,
  566. .dt_free_map = pinctrl_utils_free_map,
  567. };
  568. static int atmel_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  569. {
  570. return ARRAY_SIZE(atmel_functions);
  571. }
  572. static const char *atmel_pmx_get_function_name(struct pinctrl_dev *pctldev,
  573. unsigned int selector)
  574. {
  575. return atmel_functions[selector];
  576. }
  577. static int atmel_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  578. unsigned int selector,
  579. const char * const **groups,
  580. unsigned * const num_groups)
  581. {
  582. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  583. *groups = atmel_pioctrl->group_names;
  584. *num_groups = atmel_pioctrl->npins;
  585. return 0;
  586. }
  587. static int atmel_pmx_set_mux(struct pinctrl_dev *pctldev,
  588. unsigned int function,
  589. unsigned int group)
  590. {
  591. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  592. unsigned int pin;
  593. u32 conf;
  594. dev_dbg(pctldev->dev, "enable function %s group %s\n",
  595. atmel_functions[function], atmel_pioctrl->groups[group].name);
  596. pin = atmel_pioctrl->groups[group].pin;
  597. conf = atmel_pin_config_read(pctldev, pin);
  598. conf &= (~ATMEL_PIO_CFGR_FUNC_MASK);
  599. conf |= (function & ATMEL_PIO_CFGR_FUNC_MASK);
  600. dev_dbg(pctldev->dev, "pin: %u, conf: 0x%08x\n", pin, conf);
  601. atmel_pin_config_write(pctldev, pin, conf);
  602. return 0;
  603. }
  604. static const struct pinmux_ops atmel_pmxops = {
  605. .get_functions_count = atmel_pmx_get_functions_count,
  606. .get_function_name = atmel_pmx_get_function_name,
  607. .get_function_groups = atmel_pmx_get_function_groups,
  608. .set_mux = atmel_pmx_set_mux,
  609. };
  610. static int atmel_conf_pin_config_group_get(struct pinctrl_dev *pctldev,
  611. unsigned int group,
  612. unsigned long *config)
  613. {
  614. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  615. unsigned int param = pinconf_to_config_param(*config), arg = 0;
  616. struct atmel_group *grp = atmel_pioctrl->groups + group;
  617. unsigned int pin_id = grp->pin;
  618. u32 res;
  619. res = atmel_pin_config_read(pctldev, pin_id);
  620. switch (param) {
  621. case PIN_CONFIG_BIAS_PULL_UP:
  622. if (!(res & ATMEL_PIO_PUEN_MASK))
  623. return -EINVAL;
  624. arg = 1;
  625. break;
  626. case PIN_CONFIG_BIAS_PULL_DOWN:
  627. if ((res & ATMEL_PIO_PUEN_MASK) ||
  628. (!(res & ATMEL_PIO_PDEN_MASK)))
  629. return -EINVAL;
  630. arg = 1;
  631. break;
  632. case PIN_CONFIG_BIAS_DISABLE:
  633. if ((res & ATMEL_PIO_PUEN_MASK) ||
  634. ((res & ATMEL_PIO_PDEN_MASK)))
  635. return -EINVAL;
  636. arg = 1;
  637. break;
  638. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  639. if (!(res & ATMEL_PIO_OPD_MASK))
  640. return -EINVAL;
  641. arg = 1;
  642. break;
  643. case PIN_CONFIG_DRIVE_PUSH_PULL:
  644. if (res & ATMEL_PIO_OPD_MASK)
  645. return -EINVAL;
  646. arg = 1;
  647. break;
  648. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  649. if (!(res & ATMEL_PIO_SCHMITT_MASK))
  650. return -EINVAL;
  651. arg = 1;
  652. break;
  653. case PIN_CONFIG_SLEW_RATE:
  654. if (!atmel_pioctrl->slew_rate_support)
  655. return -EOPNOTSUPP;
  656. if (!(res & ATMEL_PIO_SR_MASK))
  657. return -EINVAL;
  658. arg = 1;
  659. break;
  660. case ATMEL_PIN_CONFIG_DRIVE_STRENGTH:
  661. if (!(res & ATMEL_PIO_DRVSTR_MASK))
  662. return -EINVAL;
  663. arg = (res & ATMEL_PIO_DRVSTR_MASK) >> ATMEL_PIO_DRVSTR_OFFSET;
  664. break;
  665. case PIN_CONFIG_PERSIST_STATE:
  666. return -ENOTSUPP;
  667. default:
  668. return -ENOTSUPP;
  669. }
  670. *config = pinconf_to_config_packed(param, arg);
  671. return 0;
  672. }
  673. static int atmel_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
  674. unsigned int group,
  675. unsigned long *configs,
  676. unsigned int num_configs)
  677. {
  678. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  679. struct atmel_group *grp = atmel_pioctrl->groups + group;
  680. unsigned int bank, pin, pin_id = grp->pin;
  681. u32 mask, conf = 0;
  682. int i;
  683. conf = atmel_pin_config_read(pctldev, pin_id);
  684. /* Keep slew rate enabled by default. */
  685. if (atmel_pioctrl->slew_rate_support)
  686. conf |= ATMEL_PIO_SR_MASK;
  687. for (i = 0; i < num_configs; i++) {
  688. unsigned int param = pinconf_to_config_param(configs[i]);
  689. unsigned int arg = pinconf_to_config_argument(configs[i]);
  690. dev_dbg(pctldev->dev, "%s: pin=%u, config=0x%lx\n",
  691. __func__, pin_id, configs[i]);
  692. switch (param) {
  693. case PIN_CONFIG_BIAS_DISABLE:
  694. conf &= (~ATMEL_PIO_PUEN_MASK);
  695. conf &= (~ATMEL_PIO_PDEN_MASK);
  696. break;
  697. case PIN_CONFIG_BIAS_PULL_UP:
  698. conf |= ATMEL_PIO_PUEN_MASK;
  699. conf &= (~ATMEL_PIO_PDEN_MASK);
  700. break;
  701. case PIN_CONFIG_BIAS_PULL_DOWN:
  702. conf |= ATMEL_PIO_PDEN_MASK;
  703. conf &= (~ATMEL_PIO_PUEN_MASK);
  704. break;
  705. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  706. conf |= ATMEL_PIO_OPD_MASK;
  707. break;
  708. case PIN_CONFIG_DRIVE_PUSH_PULL:
  709. conf &= ~ATMEL_PIO_OPD_MASK;
  710. break;
  711. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  712. if (arg == 0)
  713. conf |= ATMEL_PIO_SCHMITT_MASK;
  714. else
  715. conf &= (~ATMEL_PIO_SCHMITT_MASK);
  716. break;
  717. case PIN_CONFIG_INPUT_DEBOUNCE:
  718. if (arg == 0) {
  719. conf &= (~ATMEL_PIO_IFEN_MASK);
  720. conf &= (~ATMEL_PIO_IFSCEN_MASK);
  721. } else {
  722. /*
  723. * We don't care about the debounce value for several reasons:
  724. * - can't have different debounce periods inside a same group,
  725. * - the register to configure this period is a secure register.
  726. * The debouncing filter can filter a pulse with a duration of less
  727. * than 1/2 slow clock period.
  728. */
  729. conf |= ATMEL_PIO_IFEN_MASK;
  730. conf |= ATMEL_PIO_IFSCEN_MASK;
  731. }
  732. break;
  733. case PIN_CONFIG_OUTPUT:
  734. conf |= ATMEL_PIO_DIR_MASK;
  735. bank = ATMEL_PIO_BANK(pin_id);
  736. pin = ATMEL_PIO_LINE(pin_id);
  737. mask = 1 << pin;
  738. if (arg == 0) {
  739. writel_relaxed(mask, atmel_pioctrl->reg_base +
  740. bank * ATMEL_PIO_BANK_OFFSET +
  741. ATMEL_PIO_CODR);
  742. } else {
  743. writel_relaxed(mask, atmel_pioctrl->reg_base +
  744. bank * ATMEL_PIO_BANK_OFFSET +
  745. ATMEL_PIO_SODR);
  746. }
  747. break;
  748. case PIN_CONFIG_SLEW_RATE:
  749. if (!atmel_pioctrl->slew_rate_support)
  750. break;
  751. /* And remove it if explicitly requested. */
  752. if (arg == 0)
  753. conf &= ~ATMEL_PIO_SR_MASK;
  754. break;
  755. case ATMEL_PIN_CONFIG_DRIVE_STRENGTH:
  756. switch (arg) {
  757. case ATMEL_PIO_DRVSTR_LO:
  758. case ATMEL_PIO_DRVSTR_ME:
  759. case ATMEL_PIO_DRVSTR_HI:
  760. conf &= (~ATMEL_PIO_DRVSTR_MASK);
  761. conf |= arg << ATMEL_PIO_DRVSTR_OFFSET;
  762. break;
  763. default:
  764. dev_warn(pctldev->dev, "drive strength not updated (incorrect value)\n");
  765. }
  766. break;
  767. case PIN_CONFIG_PERSIST_STATE:
  768. return -ENOTSUPP;
  769. default:
  770. dev_warn(pctldev->dev,
  771. "unsupported configuration parameter: %u\n",
  772. param);
  773. continue;
  774. }
  775. }
  776. dev_dbg(pctldev->dev, "%s: reg=0x%08x\n", __func__, conf);
  777. atmel_pin_config_write(pctldev, pin_id, conf);
  778. return 0;
  779. }
  780. static int atmel_conf_pin_config_set(struct pinctrl_dev *pctldev,
  781. unsigned pin,
  782. unsigned long *configs,
  783. unsigned num_configs)
  784. {
  785. struct atmel_group *grp = atmel_pctl_find_group_by_pin(pctldev, pin);
  786. return atmel_conf_pin_config_group_set(pctldev, grp->pin, configs, num_configs);
  787. }
  788. static int atmel_conf_pin_config_get(struct pinctrl_dev *pctldev,
  789. unsigned pin,
  790. unsigned long *configs)
  791. {
  792. struct atmel_group *grp = atmel_pctl_find_group_by_pin(pctldev, pin);
  793. return atmel_conf_pin_config_group_get(pctldev, grp->pin, configs);
  794. }
  795. static void atmel_conf_pin_config_dbg_show(struct pinctrl_dev *pctldev,
  796. struct seq_file *s,
  797. unsigned int pin_id)
  798. {
  799. struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
  800. u32 conf;
  801. if (!atmel_pioctrl->pins[pin_id]->device)
  802. return;
  803. seq_printf(s, " (%s, ioset %u) ",
  804. atmel_pioctrl->pins[pin_id]->device,
  805. atmel_pioctrl->pins[pin_id]->ioset);
  806. conf = atmel_pin_config_read(pctldev, pin_id);
  807. if (conf & ATMEL_PIO_PUEN_MASK)
  808. seq_printf(s, "%s ", "pull-up");
  809. if (conf & ATMEL_PIO_PDEN_MASK)
  810. seq_printf(s, "%s ", "pull-down");
  811. if (conf & ATMEL_PIO_IFEN_MASK)
  812. seq_printf(s, "%s ", "debounce");
  813. if (conf & ATMEL_PIO_OPD_MASK)
  814. seq_printf(s, "%s ", "open-drain");
  815. else
  816. seq_printf(s, "%s ", "push-pull");
  817. if (conf & ATMEL_PIO_SCHMITT_MASK)
  818. seq_printf(s, "%s ", "schmitt");
  819. if (atmel_pioctrl->slew_rate_support && (conf & ATMEL_PIO_SR_MASK))
  820. seq_printf(s, "%s ", "slew-rate");
  821. if (conf & ATMEL_PIO_DRVSTR_MASK) {
  822. switch ((conf & ATMEL_PIO_DRVSTR_MASK) >> ATMEL_PIO_DRVSTR_OFFSET) {
  823. case ATMEL_PIO_DRVSTR_ME:
  824. seq_printf(s, "%s ", "medium-drive");
  825. break;
  826. case ATMEL_PIO_DRVSTR_HI:
  827. seq_printf(s, "%s ", "high-drive");
  828. break;
  829. /* ATMEL_PIO_DRVSTR_LO and 0 which is the default value at reset */
  830. default:
  831. seq_printf(s, "%s ", "low-drive");
  832. }
  833. }
  834. }
  835. static const struct pinconf_ops atmel_confops = {
  836. .pin_config_group_get = atmel_conf_pin_config_group_get,
  837. .pin_config_group_set = atmel_conf_pin_config_group_set,
  838. .pin_config_dbg_show = atmel_conf_pin_config_dbg_show,
  839. .pin_config_set = atmel_conf_pin_config_set,
  840. .pin_config_get = atmel_conf_pin_config_get,
  841. };
  842. static struct pinctrl_desc atmel_pinctrl_desc = {
  843. .name = "atmel_pinctrl",
  844. .confops = &atmel_confops,
  845. .pctlops = &atmel_pctlops,
  846. .pmxops = &atmel_pmxops,
  847. };
  848. static int __maybe_unused atmel_pctrl_suspend(struct device *dev)
  849. {
  850. struct atmel_pioctrl *atmel_pioctrl = dev_get_drvdata(dev);
  851. int i, j;
  852. /*
  853. * For each bank, save IMR to restore it later and disable all GPIO
  854. * interrupts excepting the ones marked as wakeup sources.
  855. */
  856. for (i = 0; i < atmel_pioctrl->nbanks; i++) {
  857. atmel_pioctrl->pm_suspend_backup[i].imr =
  858. atmel_gpio_read(atmel_pioctrl, i, ATMEL_PIO_IMR);
  859. atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_IDR,
  860. ~atmel_pioctrl->pm_wakeup_sources[i]);
  861. atmel_pioctrl->pm_suspend_backup[i].odsr =
  862. atmel_gpio_read(atmel_pioctrl, i, ATMEL_PIO_ODSR);
  863. for (j = 0; j < ATMEL_PIO_NPINS_PER_BANK; j++) {
  864. atmel_gpio_write(atmel_pioctrl, i,
  865. ATMEL_PIO_MSKR, BIT(j));
  866. atmel_pioctrl->pm_suspend_backup[i].cfgr[j] =
  867. atmel_gpio_read(atmel_pioctrl, i,
  868. ATMEL_PIO_CFGR);
  869. }
  870. }
  871. return 0;
  872. }
  873. static int __maybe_unused atmel_pctrl_resume(struct device *dev)
  874. {
  875. struct atmel_pioctrl *atmel_pioctrl = dev_get_drvdata(dev);
  876. int i, j;
  877. for (i = 0; i < atmel_pioctrl->nbanks; i++) {
  878. atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_IER,
  879. atmel_pioctrl->pm_suspend_backup[i].imr);
  880. atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_SODR,
  881. atmel_pioctrl->pm_suspend_backup[i].odsr);
  882. for (j = 0; j < ATMEL_PIO_NPINS_PER_BANK; j++) {
  883. atmel_gpio_write(atmel_pioctrl, i,
  884. ATMEL_PIO_MSKR, BIT(j));
  885. atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_CFGR,
  886. atmel_pioctrl->pm_suspend_backup[i].cfgr[j]);
  887. }
  888. }
  889. return 0;
  890. }
  891. static const struct dev_pm_ops atmel_pctrl_pm_ops = {
  892. SET_SYSTEM_SLEEP_PM_OPS(atmel_pctrl_suspend, atmel_pctrl_resume)
  893. };
  894. /*
  895. * The number of banks can be different from a SoC to another one.
  896. * We can have up to 16 banks.
  897. */
  898. static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
  899. .nbanks = 4,
  900. .last_bank_count = ATMEL_PIO_NPINS_PER_BANK,
  901. };
  902. static const struct atmel_pioctrl_data microchip_sama7g5_pioctrl_data = {
  903. .nbanks = 5,
  904. .last_bank_count = 8, /* sama7g5 has only PE0 to PE7 */
  905. .slew_rate_support = 1,
  906. };
  907. static const struct of_device_id atmel_pctrl_of_match[] = {
  908. {
  909. .compatible = "atmel,sama5d2-pinctrl",
  910. .data = &atmel_sama5d2_pioctrl_data,
  911. }, {
  912. .compatible = "microchip,sama7g5-pinctrl",
  913. .data = &microchip_sama7g5_pioctrl_data,
  914. }, {
  915. /* sentinel */
  916. }
  917. };
  918. /*
  919. * This lock class allows to tell lockdep that parent IRQ and children IRQ do
  920. * not share the same class so it does not raise false positive
  921. */
  922. static struct lock_class_key atmel_lock_key;
  923. static struct lock_class_key atmel_request_key;
  924. static int atmel_pinctrl_probe(struct platform_device *pdev)
  925. {
  926. struct device *dev = &pdev->dev;
  927. struct pinctrl_pin_desc *pin_desc;
  928. const char **group_names;
  929. int i, ret;
  930. struct atmel_pioctrl *atmel_pioctrl;
  931. const struct atmel_pioctrl_data *atmel_pioctrl_data;
  932. atmel_pioctrl = devm_kzalloc(dev, sizeof(*atmel_pioctrl), GFP_KERNEL);
  933. if (!atmel_pioctrl)
  934. return -ENOMEM;
  935. atmel_pioctrl->dev = dev;
  936. atmel_pioctrl->node = dev->of_node;
  937. platform_set_drvdata(pdev, atmel_pioctrl);
  938. atmel_pioctrl_data = device_get_match_data(dev);
  939. if (!atmel_pioctrl_data)
  940. return dev_err_probe(dev, -ENODEV, "Invalid device data\n");
  941. atmel_pioctrl->nbanks = atmel_pioctrl_data->nbanks;
  942. atmel_pioctrl->npins = atmel_pioctrl->nbanks * ATMEL_PIO_NPINS_PER_BANK;
  943. /* if last bank has limited number of pins, adjust accordingly */
  944. if (atmel_pioctrl_data->last_bank_count != ATMEL_PIO_NPINS_PER_BANK) {
  945. atmel_pioctrl->npins -= ATMEL_PIO_NPINS_PER_BANK;
  946. atmel_pioctrl->npins += atmel_pioctrl_data->last_bank_count;
  947. }
  948. atmel_pioctrl->slew_rate_support = atmel_pioctrl_data->slew_rate_support;
  949. atmel_pioctrl->reg_base = devm_platform_ioremap_resource(pdev, 0);
  950. if (IS_ERR(atmel_pioctrl->reg_base))
  951. return PTR_ERR(atmel_pioctrl->reg_base);
  952. atmel_pioctrl->clk = devm_clk_get_enabled(dev, NULL);
  953. if (IS_ERR(atmel_pioctrl->clk))
  954. return dev_err_probe(dev, PTR_ERR(atmel_pioctrl->clk), "failed to get clock\n");
  955. atmel_pioctrl->pins = devm_kcalloc(dev,
  956. atmel_pioctrl->npins,
  957. sizeof(*atmel_pioctrl->pins),
  958. GFP_KERNEL);
  959. if (!atmel_pioctrl->pins)
  960. return -ENOMEM;
  961. pin_desc = devm_kcalloc(dev, atmel_pioctrl->npins, sizeof(*pin_desc),
  962. GFP_KERNEL);
  963. if (!pin_desc)
  964. return -ENOMEM;
  965. atmel_pinctrl_desc.pins = pin_desc;
  966. atmel_pinctrl_desc.npins = atmel_pioctrl->npins;
  967. atmel_pinctrl_desc.num_custom_params = ARRAY_SIZE(atmel_custom_bindings);
  968. atmel_pinctrl_desc.custom_params = atmel_custom_bindings;
  969. /* One pin is one group since a pin can achieve all functions. */
  970. group_names = devm_kcalloc(dev,
  971. atmel_pioctrl->npins, sizeof(*group_names),
  972. GFP_KERNEL);
  973. if (!group_names)
  974. return -ENOMEM;
  975. atmel_pioctrl->group_names = group_names;
  976. atmel_pioctrl->groups = devm_kcalloc(&pdev->dev,
  977. atmel_pioctrl->npins, sizeof(*atmel_pioctrl->groups),
  978. GFP_KERNEL);
  979. if (!atmel_pioctrl->groups)
  980. return -ENOMEM;
  981. for (i = 0 ; i < atmel_pioctrl->npins; i++) {
  982. struct atmel_group *group = atmel_pioctrl->groups + i;
  983. unsigned int bank = ATMEL_PIO_BANK(i);
  984. unsigned int line = ATMEL_PIO_LINE(i);
  985. atmel_pioctrl->pins[i] = devm_kzalloc(dev,
  986. sizeof(**atmel_pioctrl->pins), GFP_KERNEL);
  987. if (!atmel_pioctrl->pins[i])
  988. return -ENOMEM;
  989. atmel_pioctrl->pins[i]->pin_id = i;
  990. atmel_pioctrl->pins[i]->bank = bank;
  991. atmel_pioctrl->pins[i]->line = line;
  992. pin_desc[i].number = i;
  993. /* Pin naming convention: P(bank_name)(bank_pin_number). */
  994. pin_desc[i].name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "P%c%u",
  995. bank + 'A', line);
  996. if (!pin_desc[i].name)
  997. return -ENOMEM;
  998. group->name = group_names[i] = pin_desc[i].name;
  999. group->pin = pin_desc[i].number;
  1000. dev_dbg(dev, "pin_id=%u, bank=%u, line=%u", i, bank, line);
  1001. }
  1002. atmel_pioctrl->gpio_chip = &atmel_gpio_chip;
  1003. atmel_pioctrl->gpio_chip->ngpio = atmel_pioctrl->npins;
  1004. atmel_pioctrl->gpio_chip->label = dev_name(dev);
  1005. atmel_pioctrl->gpio_chip->parent = dev;
  1006. atmel_pioctrl->gpio_chip->names = atmel_pioctrl->group_names;
  1007. atmel_pioctrl->gpio_chip->set_config = gpiochip_generic_config;
  1008. atmel_pioctrl->pm_wakeup_sources = devm_kcalloc(dev,
  1009. atmel_pioctrl->nbanks,
  1010. sizeof(*atmel_pioctrl->pm_wakeup_sources),
  1011. GFP_KERNEL);
  1012. if (!atmel_pioctrl->pm_wakeup_sources)
  1013. return -ENOMEM;
  1014. atmel_pioctrl->pm_suspend_backup = devm_kcalloc(dev,
  1015. atmel_pioctrl->nbanks,
  1016. sizeof(*atmel_pioctrl->pm_suspend_backup),
  1017. GFP_KERNEL);
  1018. if (!atmel_pioctrl->pm_suspend_backup)
  1019. return -ENOMEM;
  1020. atmel_pioctrl->irqs = devm_kcalloc(dev,
  1021. atmel_pioctrl->nbanks,
  1022. sizeof(*atmel_pioctrl->irqs),
  1023. GFP_KERNEL);
  1024. if (!atmel_pioctrl->irqs)
  1025. return -ENOMEM;
  1026. /* There is one controller but each bank has its own irq line. */
  1027. for (i = 0; i < atmel_pioctrl->nbanks; i++) {
  1028. ret = platform_get_irq(pdev, i);
  1029. if (ret < 0) {
  1030. dev_dbg(dev, "missing irq resource for group %c\n",
  1031. 'A' + i);
  1032. return ret;
  1033. }
  1034. atmel_pioctrl->irqs[i] = ret;
  1035. irq_set_chained_handler_and_data(ret, atmel_gpio_irq_handler, atmel_pioctrl);
  1036. dev_dbg(dev, "bank %i: irq=%d\n", i, ret);
  1037. }
  1038. atmel_pioctrl->irq_domain = irq_domain_add_linear(dev->of_node,
  1039. atmel_pioctrl->gpio_chip->ngpio,
  1040. &irq_domain_simple_ops, NULL);
  1041. if (!atmel_pioctrl->irq_domain)
  1042. return dev_err_probe(dev, -ENODEV, "can't add the irq domain\n");
  1043. for (i = 0; i < atmel_pioctrl->npins; i++) {
  1044. int irq = irq_create_mapping(atmel_pioctrl->irq_domain, i);
  1045. irq_set_chip_and_handler(irq, &atmel_gpio_irq_chip,
  1046. handle_simple_irq);
  1047. irq_set_chip_data(irq, atmel_pioctrl);
  1048. irq_set_lockdep_class(irq, &atmel_lock_key, &atmel_request_key);
  1049. dev_dbg(dev,
  1050. "atmel gpio irq domain: hwirq: %d, linux irq: %d\n",
  1051. i, irq);
  1052. }
  1053. atmel_pioctrl->pinctrl_dev = devm_pinctrl_register(&pdev->dev,
  1054. &atmel_pinctrl_desc,
  1055. atmel_pioctrl);
  1056. if (IS_ERR(atmel_pioctrl->pinctrl_dev)) {
  1057. ret = PTR_ERR(atmel_pioctrl->pinctrl_dev);
  1058. dev_err(dev, "pinctrl registration failed\n");
  1059. goto irq_domain_remove_error;
  1060. }
  1061. ret = gpiochip_add_data(atmel_pioctrl->gpio_chip, atmel_pioctrl);
  1062. if (ret) {
  1063. dev_err(dev, "failed to add gpiochip\n");
  1064. goto irq_domain_remove_error;
  1065. }
  1066. ret = gpiochip_add_pin_range(atmel_pioctrl->gpio_chip, dev_name(dev),
  1067. 0, 0, atmel_pioctrl->gpio_chip->ngpio);
  1068. if (ret) {
  1069. dev_err(dev, "failed to add gpio pin range\n");
  1070. goto gpiochip_add_pin_range_error;
  1071. }
  1072. dev_info(&pdev->dev, "atmel pinctrl initialized\n");
  1073. return 0;
  1074. gpiochip_add_pin_range_error:
  1075. gpiochip_remove(atmel_pioctrl->gpio_chip);
  1076. irq_domain_remove_error:
  1077. irq_domain_remove(atmel_pioctrl->irq_domain);
  1078. return ret;
  1079. }
  1080. static struct platform_driver atmel_pinctrl_driver = {
  1081. .driver = {
  1082. .name = "pinctrl-at91-pio4",
  1083. .of_match_table = atmel_pctrl_of_match,
  1084. .pm = &atmel_pctrl_pm_ops,
  1085. .suppress_bind_attrs = true,
  1086. },
  1087. .probe = atmel_pinctrl_probe,
  1088. };
  1089. builtin_platform_driver(atmel_pinctrl_driver);