pinctrl-at91-pio4.c 30 KB

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