gpio-mvebu.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * GPIO driver for Marvell SoCs
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  7. * Andrew Lunn <andrew@lunn.ch>
  8. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public
  11. * License version 2. This program is licensed "as is" without any
  12. * warranty of any kind, whether express or implied.
  13. *
  14. * This driver is a fairly straightforward GPIO driver for the
  15. * complete family of Marvell EBU SoC platforms (Orion, Dove,
  16. * Kirkwood, Discovery, Armada 370/XP). The only complexity of this
  17. * driver is the different register layout that exists between the
  18. * non-SMP platforms (Orion, Dove, Kirkwood, Armada 370) and the SMP
  19. * platforms (MV78200 from the Discovery family and the Armada
  20. * XP). Therefore, this driver handles three variants of the GPIO
  21. * block:
  22. * - the basic variant, called "orion-gpio", with the simplest
  23. * register set. Used on Orion, Dove, Kirkwoord, Armada 370 and
  24. * non-SMP Discovery systems
  25. * - the mv78200 variant for MV78200 Discovery systems. This variant
  26. * turns the edge mask and level mask registers into CPU0 edge
  27. * mask/level mask registers, and adds CPU1 edge mask/level mask
  28. * registers.
  29. * - the armadaxp variant for Armada XP systems. This variant keeps
  30. * the normal cause/edge mask/level mask registers when the global
  31. * interrupts are used, but adds per-CPU cause/edge mask/level mask
  32. * registers n a separate memory area for the per-CPU GPIO
  33. * interrupts.
  34. */
  35. #include <linux/bitops.h>
  36. #include <linux/clk.h>
  37. #include <linux/err.h>
  38. #include <linux/gpio.h>
  39. #include <linux/init.h>
  40. #include <linux/io.h>
  41. #include <linux/irq.h>
  42. #include <linux/irqchip/chained_irq.h>
  43. #include <linux/irqdomain.h>
  44. #include <linux/mfd/syscon.h>
  45. #include <linux/of_device.h>
  46. #include <linux/of_irq.h>
  47. #include <linux/pinctrl/consumer.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/pwm.h>
  50. #include <linux/regmap.h>
  51. #include <linux/slab.h>
  52. #include "gpiolib.h"
  53. /*
  54. * GPIO unit register offsets.
  55. */
  56. #define GPIO_OUT_OFF 0x0000
  57. #define GPIO_IO_CONF_OFF 0x0004
  58. #define GPIO_BLINK_EN_OFF 0x0008
  59. #define GPIO_IN_POL_OFF 0x000c
  60. #define GPIO_DATA_IN_OFF 0x0010
  61. #define GPIO_EDGE_CAUSE_OFF 0x0014
  62. #define GPIO_EDGE_MASK_OFF 0x0018
  63. #define GPIO_LEVEL_MASK_OFF 0x001c
  64. #define GPIO_BLINK_CNT_SELECT_OFF 0x0020
  65. /*
  66. * PWM register offsets.
  67. */
  68. #define PWM_BLINK_ON_DURATION_OFF 0x0
  69. #define PWM_BLINK_OFF_DURATION_OFF 0x4
  70. /* The MV78200 has per-CPU registers for edge mask and level mask */
  71. #define GPIO_EDGE_MASK_MV78200_OFF(cpu) ((cpu) ? 0x30 : 0x18)
  72. #define GPIO_LEVEL_MASK_MV78200_OFF(cpu) ((cpu) ? 0x34 : 0x1C)
  73. /*
  74. * The Armada XP has per-CPU registers for interrupt cause, interrupt
  75. * mask and interrupt level mask. Those are relative to the
  76. * percpu_membase.
  77. */
  78. #define GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu) ((cpu) * 0x4)
  79. #define GPIO_EDGE_MASK_ARMADAXP_OFF(cpu) (0x10 + (cpu) * 0x4)
  80. #define GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu) (0x20 + (cpu) * 0x4)
  81. #define MVEBU_GPIO_SOC_VARIANT_ORION 0x1
  82. #define MVEBU_GPIO_SOC_VARIANT_MV78200 0x2
  83. #define MVEBU_GPIO_SOC_VARIANT_ARMADAXP 0x3
  84. #define MVEBU_GPIO_SOC_VARIANT_A8K 0x4
  85. #define MVEBU_MAX_GPIO_PER_BANK 32
  86. struct mvebu_pwm {
  87. void __iomem *membase;
  88. unsigned long clk_rate;
  89. struct gpio_desc *gpiod;
  90. struct pwm_chip chip;
  91. spinlock_t lock;
  92. struct mvebu_gpio_chip *mvchip;
  93. /* Used to preserve GPIO/PWM registers across suspend/resume */
  94. u32 blink_select;
  95. u32 blink_on_duration;
  96. u32 blink_off_duration;
  97. };
  98. struct mvebu_gpio_chip {
  99. struct gpio_chip chip;
  100. struct regmap *regs;
  101. u32 offset;
  102. struct regmap *percpu_regs;
  103. int irqbase;
  104. struct irq_domain *domain;
  105. int soc_variant;
  106. /* Used for PWM support */
  107. struct clk *clk;
  108. struct mvebu_pwm *mvpwm;
  109. /* Used to preserve GPIO registers across suspend/resume */
  110. u32 out_reg;
  111. u32 io_conf_reg;
  112. u32 blink_en_reg;
  113. u32 in_pol_reg;
  114. u32 edge_mask_regs[4];
  115. u32 level_mask_regs[4];
  116. };
  117. /*
  118. * Functions returning addresses of individual registers for a given
  119. * GPIO controller.
  120. */
  121. static void mvebu_gpioreg_edge_cause(struct mvebu_gpio_chip *mvchip,
  122. struct regmap **map, unsigned int *offset)
  123. {
  124. int cpu;
  125. switch (mvchip->soc_variant) {
  126. case MVEBU_GPIO_SOC_VARIANT_ORION:
  127. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  128. case MVEBU_GPIO_SOC_VARIANT_A8K:
  129. *map = mvchip->regs;
  130. *offset = GPIO_EDGE_CAUSE_OFF + mvchip->offset;
  131. break;
  132. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  133. cpu = smp_processor_id();
  134. *map = mvchip->percpu_regs;
  135. *offset = GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu);
  136. break;
  137. default:
  138. BUG();
  139. }
  140. }
  141. static u32
  142. mvebu_gpio_read_edge_cause(struct mvebu_gpio_chip *mvchip)
  143. {
  144. struct regmap *map;
  145. unsigned int offset;
  146. u32 val;
  147. mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
  148. regmap_read(map, offset, &val);
  149. return val;
  150. }
  151. static void
  152. mvebu_gpio_write_edge_cause(struct mvebu_gpio_chip *mvchip, u32 val)
  153. {
  154. struct regmap *map;
  155. unsigned int offset;
  156. mvebu_gpioreg_edge_cause(mvchip, &map, &offset);
  157. regmap_write(map, offset, val);
  158. }
  159. static inline void
  160. mvebu_gpioreg_edge_mask(struct mvebu_gpio_chip *mvchip,
  161. struct regmap **map, unsigned int *offset)
  162. {
  163. int cpu;
  164. switch (mvchip->soc_variant) {
  165. case MVEBU_GPIO_SOC_VARIANT_ORION:
  166. case MVEBU_GPIO_SOC_VARIANT_A8K:
  167. *map = mvchip->regs;
  168. *offset = GPIO_EDGE_MASK_OFF + mvchip->offset;
  169. break;
  170. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  171. cpu = smp_processor_id();
  172. *map = mvchip->regs;
  173. *offset = GPIO_EDGE_MASK_MV78200_OFF(cpu);
  174. break;
  175. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  176. cpu = smp_processor_id();
  177. *map = mvchip->percpu_regs;
  178. *offset = GPIO_EDGE_MASK_ARMADAXP_OFF(cpu);
  179. break;
  180. default:
  181. BUG();
  182. }
  183. }
  184. static u32
  185. mvebu_gpio_read_edge_mask(struct mvebu_gpio_chip *mvchip)
  186. {
  187. struct regmap *map;
  188. unsigned int offset;
  189. u32 val;
  190. mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
  191. regmap_read(map, offset, &val);
  192. return val;
  193. }
  194. static void
  195. mvebu_gpio_write_edge_mask(struct mvebu_gpio_chip *mvchip, u32 val)
  196. {
  197. struct regmap *map;
  198. unsigned int offset;
  199. mvebu_gpioreg_edge_mask(mvchip, &map, &offset);
  200. regmap_write(map, offset, val);
  201. }
  202. static void
  203. mvebu_gpioreg_level_mask(struct mvebu_gpio_chip *mvchip,
  204. struct regmap **map, unsigned int *offset)
  205. {
  206. int cpu;
  207. switch (mvchip->soc_variant) {
  208. case MVEBU_GPIO_SOC_VARIANT_ORION:
  209. case MVEBU_GPIO_SOC_VARIANT_A8K:
  210. *map = mvchip->regs;
  211. *offset = GPIO_LEVEL_MASK_OFF + mvchip->offset;
  212. break;
  213. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  214. cpu = smp_processor_id();
  215. *map = mvchip->regs;
  216. *offset = GPIO_LEVEL_MASK_MV78200_OFF(cpu);
  217. break;
  218. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  219. cpu = smp_processor_id();
  220. *map = mvchip->percpu_regs;
  221. *offset = GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu);
  222. break;
  223. default:
  224. BUG();
  225. }
  226. }
  227. static u32
  228. mvebu_gpio_read_level_mask(struct mvebu_gpio_chip *mvchip)
  229. {
  230. struct regmap *map;
  231. unsigned int offset;
  232. u32 val;
  233. mvebu_gpioreg_level_mask(mvchip, &map, &offset);
  234. regmap_read(map, offset, &val);
  235. return val;
  236. }
  237. static void
  238. mvebu_gpio_write_level_mask(struct mvebu_gpio_chip *mvchip, u32 val)
  239. {
  240. struct regmap *map;
  241. unsigned int offset;
  242. mvebu_gpioreg_level_mask(mvchip, &map, &offset);
  243. regmap_write(map, offset, val);
  244. }
  245. /*
  246. * Functions returning addresses of individual registers for a given
  247. * PWM controller.
  248. */
  249. static void __iomem *mvebu_pwmreg_blink_on_duration(struct mvebu_pwm *mvpwm)
  250. {
  251. return mvpwm->membase + PWM_BLINK_ON_DURATION_OFF;
  252. }
  253. static void __iomem *mvebu_pwmreg_blink_off_duration(struct mvebu_pwm *mvpwm)
  254. {
  255. return mvpwm->membase + PWM_BLINK_OFF_DURATION_OFF;
  256. }
  257. /*
  258. * Functions implementing the gpio_chip methods
  259. */
  260. static void mvebu_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
  261. {
  262. struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
  263. regmap_update_bits(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
  264. BIT(pin), value ? BIT(pin) : 0);
  265. }
  266. static int mvebu_gpio_get(struct gpio_chip *chip, unsigned int pin)
  267. {
  268. struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
  269. u32 u;
  270. regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
  271. if (u & BIT(pin)) {
  272. u32 data_in, in_pol;
  273. regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset,
  274. &data_in);
  275. regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
  276. &in_pol);
  277. u = data_in ^ in_pol;
  278. } else {
  279. regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &u);
  280. }
  281. return (u >> pin) & 1;
  282. }
  283. static void mvebu_gpio_blink(struct gpio_chip *chip, unsigned int pin,
  284. int value)
  285. {
  286. struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
  287. regmap_update_bits(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
  288. BIT(pin), value ? BIT(pin) : 0);
  289. }
  290. static int mvebu_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
  291. {
  292. struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
  293. int ret;
  294. /*
  295. * Check with the pinctrl driver whether this pin is usable as
  296. * an input GPIO
  297. */
  298. ret = pinctrl_gpio_direction_input(chip->base + pin);
  299. if (ret)
  300. return ret;
  301. regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
  302. BIT(pin), BIT(pin));
  303. return 0;
  304. }
  305. static int mvebu_gpio_direction_output(struct gpio_chip *chip, unsigned int pin,
  306. int value)
  307. {
  308. struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
  309. int ret;
  310. /*
  311. * Check with the pinctrl driver whether this pin is usable as
  312. * an output GPIO
  313. */
  314. ret = pinctrl_gpio_direction_output(chip->base + pin);
  315. if (ret)
  316. return ret;
  317. mvebu_gpio_blink(chip, pin, 0);
  318. mvebu_gpio_set(chip, pin, value);
  319. regmap_update_bits(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
  320. BIT(pin), 0);
  321. return 0;
  322. }
  323. static int mvebu_gpio_to_irq(struct gpio_chip *chip, unsigned int pin)
  324. {
  325. struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
  326. return irq_create_mapping(mvchip->domain, pin);
  327. }
  328. /*
  329. * Functions implementing the irq_chip methods
  330. */
  331. static void mvebu_gpio_irq_ack(struct irq_data *d)
  332. {
  333. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  334. struct mvebu_gpio_chip *mvchip = gc->private;
  335. u32 mask = d->mask;
  336. irq_gc_lock(gc);
  337. mvebu_gpio_write_edge_cause(mvchip, ~mask);
  338. irq_gc_unlock(gc);
  339. }
  340. static void mvebu_gpio_edge_irq_mask(struct irq_data *d)
  341. {
  342. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  343. struct mvebu_gpio_chip *mvchip = gc->private;
  344. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  345. u32 mask = d->mask;
  346. irq_gc_lock(gc);
  347. ct->mask_cache_priv &= ~mask;
  348. mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
  349. irq_gc_unlock(gc);
  350. }
  351. static void mvebu_gpio_edge_irq_unmask(struct irq_data *d)
  352. {
  353. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  354. struct mvebu_gpio_chip *mvchip = gc->private;
  355. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  356. u32 mask = d->mask;
  357. irq_gc_lock(gc);
  358. ct->mask_cache_priv |= mask;
  359. mvebu_gpio_write_edge_mask(mvchip, ct->mask_cache_priv);
  360. irq_gc_unlock(gc);
  361. }
  362. static void mvebu_gpio_level_irq_mask(struct irq_data *d)
  363. {
  364. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  365. struct mvebu_gpio_chip *mvchip = gc->private;
  366. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  367. u32 mask = d->mask;
  368. irq_gc_lock(gc);
  369. ct->mask_cache_priv &= ~mask;
  370. mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
  371. irq_gc_unlock(gc);
  372. }
  373. static void mvebu_gpio_level_irq_unmask(struct irq_data *d)
  374. {
  375. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  376. struct mvebu_gpio_chip *mvchip = gc->private;
  377. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  378. u32 mask = d->mask;
  379. irq_gc_lock(gc);
  380. ct->mask_cache_priv |= mask;
  381. mvebu_gpio_write_level_mask(mvchip, ct->mask_cache_priv);
  382. irq_gc_unlock(gc);
  383. }
  384. /*****************************************************************************
  385. * MVEBU GPIO IRQ
  386. *
  387. * GPIO_IN_POL register controls whether GPIO_DATA_IN will hold the same
  388. * value of the line or the opposite value.
  389. *
  390. * Level IRQ handlers: DATA_IN is used directly as cause register.
  391. * Interrupt are masked by LEVEL_MASK registers.
  392. * Edge IRQ handlers: Change in DATA_IN are latched in EDGE_CAUSE.
  393. * Interrupt are masked by EDGE_MASK registers.
  394. * Both-edge handlers: Similar to regular Edge handlers, but also swaps
  395. * the polarity to catch the next line transaction.
  396. * This is a race condition that might not perfectly
  397. * work on some use cases.
  398. *
  399. * Every eight GPIO lines are grouped (OR'ed) before going up to main
  400. * cause register.
  401. *
  402. * EDGE cause mask
  403. * data-in /--------| |-----| |----\
  404. * -----| |----- ---- to main cause reg
  405. * X \----------------| |----/
  406. * polarity LEVEL mask
  407. *
  408. ****************************************************************************/
  409. static int mvebu_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  410. {
  411. struct irq_chip_generic *gc = irq_data_get_irq_chip_data(d);
  412. struct irq_chip_type *ct = irq_data_get_chip_type(d);
  413. struct mvebu_gpio_chip *mvchip = gc->private;
  414. int pin;
  415. u32 u;
  416. pin = d->hwirq;
  417. regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &u);
  418. if ((u & BIT(pin)) == 0)
  419. return -EINVAL;
  420. type &= IRQ_TYPE_SENSE_MASK;
  421. if (type == IRQ_TYPE_NONE)
  422. return -EINVAL;
  423. /* Check if we need to change chip and handler */
  424. if (!(ct->type & type))
  425. if (irq_setup_alt_chip(d, type))
  426. return -EINVAL;
  427. /*
  428. * Configure interrupt polarity.
  429. */
  430. switch (type) {
  431. case IRQ_TYPE_EDGE_RISING:
  432. case IRQ_TYPE_LEVEL_HIGH:
  433. regmap_update_bits(mvchip->regs,
  434. GPIO_IN_POL_OFF + mvchip->offset,
  435. BIT(pin), 0);
  436. break;
  437. case IRQ_TYPE_EDGE_FALLING:
  438. case IRQ_TYPE_LEVEL_LOW:
  439. regmap_update_bits(mvchip->regs,
  440. GPIO_IN_POL_OFF + mvchip->offset,
  441. BIT(pin), BIT(pin));
  442. break;
  443. case IRQ_TYPE_EDGE_BOTH: {
  444. u32 data_in, in_pol, val;
  445. regmap_read(mvchip->regs,
  446. GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
  447. regmap_read(mvchip->regs,
  448. GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
  449. /*
  450. * set initial polarity based on current input level
  451. */
  452. if ((data_in ^ in_pol) & BIT(pin))
  453. val = BIT(pin); /* falling */
  454. else
  455. val = 0; /* raising */
  456. regmap_update_bits(mvchip->regs,
  457. GPIO_IN_POL_OFF + mvchip->offset,
  458. BIT(pin), val);
  459. break;
  460. }
  461. }
  462. return 0;
  463. }
  464. static void mvebu_gpio_irq_handler(struct irq_desc *desc)
  465. {
  466. struct mvebu_gpio_chip *mvchip = irq_desc_get_handler_data(desc);
  467. struct irq_chip *chip = irq_desc_get_chip(desc);
  468. u32 cause, type, data_in, level_mask, edge_cause, edge_mask;
  469. int i;
  470. if (mvchip == NULL)
  471. return;
  472. chained_irq_enter(chip, desc);
  473. regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
  474. level_mask = mvebu_gpio_read_level_mask(mvchip);
  475. edge_cause = mvebu_gpio_read_edge_cause(mvchip);
  476. edge_mask = mvebu_gpio_read_edge_mask(mvchip);
  477. cause = (data_in & level_mask) | (edge_cause & edge_mask);
  478. for (i = 0; i < mvchip->chip.ngpio; i++) {
  479. int irq;
  480. irq = irq_find_mapping(mvchip->domain, i);
  481. if (!(cause & BIT(i)))
  482. continue;
  483. type = irq_get_trigger_type(irq);
  484. if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
  485. /* Swap polarity (race with GPIO line) */
  486. u32 polarity;
  487. regmap_read(mvchip->regs,
  488. GPIO_IN_POL_OFF + mvchip->offset,
  489. &polarity);
  490. polarity ^= BIT(i);
  491. regmap_write(mvchip->regs,
  492. GPIO_IN_POL_OFF + mvchip->offset,
  493. polarity);
  494. }
  495. generic_handle_irq(irq);
  496. }
  497. chained_irq_exit(chip, desc);
  498. }
  499. /*
  500. * Functions implementing the pwm_chip methods
  501. */
  502. static struct mvebu_pwm *to_mvebu_pwm(struct pwm_chip *chip)
  503. {
  504. return container_of(chip, struct mvebu_pwm, chip);
  505. }
  506. static int mvebu_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  507. {
  508. struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
  509. struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
  510. struct gpio_desc *desc;
  511. unsigned long flags;
  512. int ret = 0;
  513. spin_lock_irqsave(&mvpwm->lock, flags);
  514. if (mvpwm->gpiod) {
  515. ret = -EBUSY;
  516. } else {
  517. desc = gpio_to_desc(mvchip->chip.base + pwm->hwpwm);
  518. if (!desc) {
  519. ret = -ENODEV;
  520. goto out;
  521. }
  522. ret = gpiod_request(desc, "mvebu-pwm");
  523. if (ret)
  524. goto out;
  525. ret = gpiod_direction_output(desc, 0);
  526. if (ret) {
  527. gpiod_free(desc);
  528. goto out;
  529. }
  530. mvpwm->gpiod = desc;
  531. }
  532. out:
  533. spin_unlock_irqrestore(&mvpwm->lock, flags);
  534. return ret;
  535. }
  536. static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  537. {
  538. struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
  539. unsigned long flags;
  540. spin_lock_irqsave(&mvpwm->lock, flags);
  541. gpiod_free(mvpwm->gpiod);
  542. mvpwm->gpiod = NULL;
  543. spin_unlock_irqrestore(&mvpwm->lock, flags);
  544. }
  545. static void mvebu_pwm_get_state(struct pwm_chip *chip,
  546. struct pwm_device *pwm,
  547. struct pwm_state *state) {
  548. struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
  549. struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
  550. unsigned long long val;
  551. unsigned long flags;
  552. u32 u;
  553. spin_lock_irqsave(&mvpwm->lock, flags);
  554. val = (unsigned long long)
  555. readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
  556. val *= NSEC_PER_SEC;
  557. do_div(val, mvpwm->clk_rate);
  558. if (val > UINT_MAX)
  559. state->duty_cycle = UINT_MAX;
  560. else if (val)
  561. state->duty_cycle = val;
  562. else
  563. state->duty_cycle = 1;
  564. val = (unsigned long long)
  565. readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
  566. val *= NSEC_PER_SEC;
  567. do_div(val, mvpwm->clk_rate);
  568. if (val < state->duty_cycle) {
  569. state->period = 1;
  570. } else {
  571. val -= state->duty_cycle;
  572. if (val > UINT_MAX)
  573. state->period = UINT_MAX;
  574. else if (val)
  575. state->period = val;
  576. else
  577. state->period = 1;
  578. }
  579. regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &u);
  580. if (u)
  581. state->enabled = true;
  582. else
  583. state->enabled = false;
  584. spin_unlock_irqrestore(&mvpwm->lock, flags);
  585. }
  586. static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  587. struct pwm_state *state)
  588. {
  589. struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
  590. struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
  591. unsigned long long val;
  592. unsigned long flags;
  593. unsigned int on, off;
  594. val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle;
  595. do_div(val, NSEC_PER_SEC);
  596. if (val > UINT_MAX)
  597. return -EINVAL;
  598. if (val)
  599. on = val;
  600. else
  601. on = 1;
  602. val = (unsigned long long) mvpwm->clk_rate *
  603. (state->period - state->duty_cycle);
  604. do_div(val, NSEC_PER_SEC);
  605. if (val > UINT_MAX)
  606. return -EINVAL;
  607. if (val)
  608. off = val;
  609. else
  610. off = 1;
  611. spin_lock_irqsave(&mvpwm->lock, flags);
  612. writel_relaxed(on, mvebu_pwmreg_blink_on_duration(mvpwm));
  613. writel_relaxed(off, mvebu_pwmreg_blink_off_duration(mvpwm));
  614. if (state->enabled)
  615. mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 1);
  616. else
  617. mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 0);
  618. spin_unlock_irqrestore(&mvpwm->lock, flags);
  619. return 0;
  620. }
  621. static const struct pwm_ops mvebu_pwm_ops = {
  622. .request = mvebu_pwm_request,
  623. .free = mvebu_pwm_free,
  624. .get_state = mvebu_pwm_get_state,
  625. .apply = mvebu_pwm_apply,
  626. .owner = THIS_MODULE,
  627. };
  628. static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip)
  629. {
  630. struct mvebu_pwm *mvpwm = mvchip->mvpwm;
  631. regmap_read(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
  632. &mvpwm->blink_select);
  633. mvpwm->blink_on_duration =
  634. readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
  635. mvpwm->blink_off_duration =
  636. readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
  637. }
  638. static void __maybe_unused mvebu_pwm_resume(struct mvebu_gpio_chip *mvchip)
  639. {
  640. struct mvebu_pwm *mvpwm = mvchip->mvpwm;
  641. regmap_write(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
  642. mvpwm->blink_select);
  643. writel_relaxed(mvpwm->blink_on_duration,
  644. mvebu_pwmreg_blink_on_duration(mvpwm));
  645. writel_relaxed(mvpwm->blink_off_duration,
  646. mvebu_pwmreg_blink_off_duration(mvpwm));
  647. }
  648. static int mvebu_pwm_probe(struct platform_device *pdev,
  649. struct mvebu_gpio_chip *mvchip,
  650. int id)
  651. {
  652. struct device *dev = &pdev->dev;
  653. struct mvebu_pwm *mvpwm;
  654. struct resource *res;
  655. u32 set;
  656. if (!of_device_is_compatible(mvchip->chip.of_node,
  657. "marvell,armada-370-gpio"))
  658. return 0;
  659. if (IS_ERR(mvchip->clk))
  660. return PTR_ERR(mvchip->clk);
  661. /*
  662. * There are only two sets of PWM configuration registers for
  663. * all the GPIO lines on those SoCs which this driver reserves
  664. * for the first two GPIO chips. So if the resource is missing
  665. * we can't treat it as an error.
  666. */
  667. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm");
  668. if (!res)
  669. return 0;
  670. /*
  671. * Use set A for lines of GPIO chip with id 0, B for GPIO chip
  672. * with id 1. Don't allow further GPIO chips to be used for PWM.
  673. */
  674. if (id == 0)
  675. set = 0;
  676. else if (id == 1)
  677. set = U32_MAX;
  678. else
  679. return -EINVAL;
  680. regmap_write(mvchip->regs,
  681. GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset, set);
  682. mvpwm = devm_kzalloc(dev, sizeof(struct mvebu_pwm), GFP_KERNEL);
  683. if (!mvpwm)
  684. return -ENOMEM;
  685. mvchip->mvpwm = mvpwm;
  686. mvpwm->mvchip = mvchip;
  687. mvpwm->membase = devm_ioremap_resource(dev, res);
  688. if (IS_ERR(mvpwm->membase))
  689. return PTR_ERR(mvpwm->membase);
  690. mvpwm->clk_rate = clk_get_rate(mvchip->clk);
  691. if (!mvpwm->clk_rate) {
  692. dev_err(dev, "failed to get clock rate\n");
  693. return -EINVAL;
  694. }
  695. mvpwm->chip.dev = dev;
  696. mvpwm->chip.ops = &mvebu_pwm_ops;
  697. mvpwm->chip.npwm = mvchip->chip.ngpio;
  698. /*
  699. * There may already be some PWM allocated, so we can't force
  700. * mvpwm->chip.base to a fixed point like mvchip->chip.base.
  701. * So, we let pwmchip_add() do the numbering and take the next free
  702. * region.
  703. */
  704. mvpwm->chip.base = -1;
  705. spin_lock_init(&mvpwm->lock);
  706. return pwmchip_add(&mvpwm->chip);
  707. }
  708. #ifdef CONFIG_DEBUG_FS
  709. #include <linux/seq_file.h>
  710. static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  711. {
  712. struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
  713. u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
  714. int i;
  715. regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &out);
  716. regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &io_conf);
  717. regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &blink);
  718. regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
  719. regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
  720. cause = mvebu_gpio_read_edge_cause(mvchip);
  721. edg_msk = mvebu_gpio_read_edge_mask(mvchip);
  722. lvl_msk = mvebu_gpio_read_level_mask(mvchip);
  723. for (i = 0; i < chip->ngpio; i++) {
  724. const char *label;
  725. u32 msk;
  726. bool is_out;
  727. label = gpiochip_is_requested(chip, i);
  728. if (!label)
  729. continue;
  730. msk = BIT(i);
  731. is_out = !(io_conf & msk);
  732. seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
  733. if (is_out) {
  734. seq_printf(s, " out %s %s\n",
  735. out & msk ? "hi" : "lo",
  736. blink & msk ? "(blink )" : "");
  737. continue;
  738. }
  739. seq_printf(s, " in %s (act %s) - IRQ",
  740. (data_in ^ in_pol) & msk ? "hi" : "lo",
  741. in_pol & msk ? "lo" : "hi");
  742. if (!((edg_msk | lvl_msk) & msk)) {
  743. seq_puts(s, " disabled\n");
  744. continue;
  745. }
  746. if (edg_msk & msk)
  747. seq_puts(s, " edge ");
  748. if (lvl_msk & msk)
  749. seq_puts(s, " level");
  750. seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
  751. }
  752. }
  753. #else
  754. #define mvebu_gpio_dbg_show NULL
  755. #endif
  756. static const struct of_device_id mvebu_gpio_of_match[] = {
  757. {
  758. .compatible = "marvell,orion-gpio",
  759. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
  760. },
  761. {
  762. .compatible = "marvell,mv78200-gpio",
  763. .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
  764. },
  765. {
  766. .compatible = "marvell,armadaxp-gpio",
  767. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  768. },
  769. {
  770. .compatible = "marvell,armada-370-gpio",
  771. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
  772. },
  773. {
  774. .compatible = "marvell,armada-8k-gpio",
  775. .data = (void *) MVEBU_GPIO_SOC_VARIANT_A8K,
  776. },
  777. {
  778. /* sentinel */
  779. },
  780. };
  781. static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
  782. {
  783. struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
  784. int i;
  785. regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
  786. &mvchip->out_reg);
  787. regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
  788. &mvchip->io_conf_reg);
  789. regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
  790. &mvchip->blink_en_reg);
  791. regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
  792. &mvchip->in_pol_reg);
  793. switch (mvchip->soc_variant) {
  794. case MVEBU_GPIO_SOC_VARIANT_ORION:
  795. case MVEBU_GPIO_SOC_VARIANT_A8K:
  796. regmap_read(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
  797. &mvchip->edge_mask_regs[0]);
  798. regmap_read(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
  799. &mvchip->level_mask_regs[0]);
  800. break;
  801. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  802. for (i = 0; i < 2; i++) {
  803. regmap_read(mvchip->regs,
  804. GPIO_EDGE_MASK_MV78200_OFF(i),
  805. &mvchip->edge_mask_regs[i]);
  806. regmap_read(mvchip->regs,
  807. GPIO_LEVEL_MASK_MV78200_OFF(i),
  808. &mvchip->level_mask_regs[i]);
  809. }
  810. break;
  811. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  812. for (i = 0; i < 4; i++) {
  813. regmap_read(mvchip->regs,
  814. GPIO_EDGE_MASK_ARMADAXP_OFF(i),
  815. &mvchip->edge_mask_regs[i]);
  816. regmap_read(mvchip->regs,
  817. GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
  818. &mvchip->level_mask_regs[i]);
  819. }
  820. break;
  821. default:
  822. BUG();
  823. }
  824. if (IS_ENABLED(CONFIG_PWM))
  825. mvebu_pwm_suspend(mvchip);
  826. return 0;
  827. }
  828. static int mvebu_gpio_resume(struct platform_device *pdev)
  829. {
  830. struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
  831. int i;
  832. regmap_write(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
  833. mvchip->out_reg);
  834. regmap_write(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
  835. mvchip->io_conf_reg);
  836. regmap_write(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
  837. mvchip->blink_en_reg);
  838. regmap_write(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
  839. mvchip->in_pol_reg);
  840. switch (mvchip->soc_variant) {
  841. case MVEBU_GPIO_SOC_VARIANT_ORION:
  842. case MVEBU_GPIO_SOC_VARIANT_A8K:
  843. regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
  844. mvchip->edge_mask_regs[0]);
  845. regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
  846. mvchip->level_mask_regs[0]);
  847. break;
  848. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  849. for (i = 0; i < 2; i++) {
  850. regmap_write(mvchip->regs,
  851. GPIO_EDGE_MASK_MV78200_OFF(i),
  852. mvchip->edge_mask_regs[i]);
  853. regmap_write(mvchip->regs,
  854. GPIO_LEVEL_MASK_MV78200_OFF(i),
  855. mvchip->level_mask_regs[i]);
  856. }
  857. break;
  858. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  859. for (i = 0; i < 4; i++) {
  860. regmap_write(mvchip->regs,
  861. GPIO_EDGE_MASK_ARMADAXP_OFF(i),
  862. mvchip->edge_mask_regs[i]);
  863. regmap_write(mvchip->regs,
  864. GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
  865. mvchip->level_mask_regs[i]);
  866. }
  867. break;
  868. default:
  869. BUG();
  870. }
  871. if (IS_ENABLED(CONFIG_PWM))
  872. mvebu_pwm_resume(mvchip);
  873. return 0;
  874. }
  875. static const struct regmap_config mvebu_gpio_regmap_config = {
  876. .reg_bits = 32,
  877. .reg_stride = 4,
  878. .val_bits = 32,
  879. .fast_io = true,
  880. };
  881. static int mvebu_gpio_probe_raw(struct platform_device *pdev,
  882. struct mvebu_gpio_chip *mvchip)
  883. {
  884. struct resource *res;
  885. void __iomem *base;
  886. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  887. base = devm_ioremap_resource(&pdev->dev, res);
  888. if (IS_ERR(base))
  889. return PTR_ERR(base);
  890. mvchip->regs = devm_regmap_init_mmio(&pdev->dev, base,
  891. &mvebu_gpio_regmap_config);
  892. if (IS_ERR(mvchip->regs))
  893. return PTR_ERR(mvchip->regs);
  894. /*
  895. * For the legacy SoCs, the regmap directly maps to the GPIO
  896. * registers, so no offset is needed.
  897. */
  898. mvchip->offset = 0;
  899. /*
  900. * The Armada XP has a second range of registers for the
  901. * per-CPU registers
  902. */
  903. if (mvchip->soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  904. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  905. base = devm_ioremap_resource(&pdev->dev, res);
  906. if (IS_ERR(base))
  907. return PTR_ERR(base);
  908. mvchip->percpu_regs =
  909. devm_regmap_init_mmio(&pdev->dev, base,
  910. &mvebu_gpio_regmap_config);
  911. if (IS_ERR(mvchip->percpu_regs))
  912. return PTR_ERR(mvchip->percpu_regs);
  913. }
  914. return 0;
  915. }
  916. static int mvebu_gpio_probe_syscon(struct platform_device *pdev,
  917. struct mvebu_gpio_chip *mvchip)
  918. {
  919. mvchip->regs = syscon_node_to_regmap(pdev->dev.parent->of_node);
  920. if (IS_ERR(mvchip->regs))
  921. return PTR_ERR(mvchip->regs);
  922. if (of_property_read_u32(pdev->dev.of_node, "offset", &mvchip->offset))
  923. return -EINVAL;
  924. return 0;
  925. }
  926. static int mvebu_gpio_probe(struct platform_device *pdev)
  927. {
  928. struct mvebu_gpio_chip *mvchip;
  929. const struct of_device_id *match;
  930. struct device_node *np = pdev->dev.of_node;
  931. struct irq_chip_generic *gc;
  932. struct irq_chip_type *ct;
  933. unsigned int ngpios;
  934. bool have_irqs;
  935. int soc_variant;
  936. int i, cpu, id;
  937. int err;
  938. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  939. if (match)
  940. soc_variant = (unsigned long) match->data;
  941. else
  942. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  943. /* Some gpio controllers do not provide irq support */
  944. have_irqs = of_irq_count(np) != 0;
  945. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
  946. GFP_KERNEL);
  947. if (!mvchip)
  948. return -ENOMEM;
  949. platform_set_drvdata(pdev, mvchip);
  950. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  951. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  952. return -ENODEV;
  953. }
  954. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  955. if (id < 0) {
  956. dev_err(&pdev->dev, "Couldn't get OF id\n");
  957. return id;
  958. }
  959. mvchip->clk = devm_clk_get(&pdev->dev, NULL);
  960. /* Not all SoCs require a clock.*/
  961. if (!IS_ERR(mvchip->clk))
  962. clk_prepare_enable(mvchip->clk);
  963. mvchip->soc_variant = soc_variant;
  964. mvchip->chip.label = dev_name(&pdev->dev);
  965. mvchip->chip.parent = &pdev->dev;
  966. mvchip->chip.request = gpiochip_generic_request;
  967. mvchip->chip.free = gpiochip_generic_free;
  968. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  969. mvchip->chip.get = mvebu_gpio_get;
  970. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  971. mvchip->chip.set = mvebu_gpio_set;
  972. if (have_irqs)
  973. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  974. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  975. mvchip->chip.ngpio = ngpios;
  976. mvchip->chip.can_sleep = false;
  977. mvchip->chip.of_node = np;
  978. mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
  979. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_A8K)
  980. err = mvebu_gpio_probe_syscon(pdev, mvchip);
  981. else
  982. err = mvebu_gpio_probe_raw(pdev, mvchip);
  983. if (err)
  984. return err;
  985. /*
  986. * Mask and clear GPIO interrupts.
  987. */
  988. switch (soc_variant) {
  989. case MVEBU_GPIO_SOC_VARIANT_ORION:
  990. case MVEBU_GPIO_SOC_VARIANT_A8K:
  991. regmap_write(mvchip->regs,
  992. GPIO_EDGE_CAUSE_OFF + mvchip->offset, 0);
  993. regmap_write(mvchip->regs,
  994. GPIO_EDGE_MASK_OFF + mvchip->offset, 0);
  995. regmap_write(mvchip->regs,
  996. GPIO_LEVEL_MASK_OFF + mvchip->offset, 0);
  997. break;
  998. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  999. regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
  1000. for (cpu = 0; cpu < 2; cpu++) {
  1001. regmap_write(mvchip->regs,
  1002. GPIO_EDGE_MASK_MV78200_OFF(cpu), 0);
  1003. regmap_write(mvchip->regs,
  1004. GPIO_LEVEL_MASK_MV78200_OFF(cpu), 0);
  1005. }
  1006. break;
  1007. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  1008. regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
  1009. regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF, 0);
  1010. regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF, 0);
  1011. for (cpu = 0; cpu < 4; cpu++) {
  1012. regmap_write(mvchip->percpu_regs,
  1013. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu), 0);
  1014. regmap_write(mvchip->percpu_regs,
  1015. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu), 0);
  1016. regmap_write(mvchip->percpu_regs,
  1017. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu), 0);
  1018. }
  1019. break;
  1020. default:
  1021. BUG();
  1022. }
  1023. devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip);
  1024. /* Some gpio controllers do not provide irq support */
  1025. if (!have_irqs)
  1026. return 0;
  1027. mvchip->domain =
  1028. irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL);
  1029. if (!mvchip->domain) {
  1030. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  1031. mvchip->chip.label);
  1032. return -ENODEV;
  1033. }
  1034. err = irq_alloc_domain_generic_chips(
  1035. mvchip->domain, ngpios, 2, np->name, handle_level_irq,
  1036. IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_LEVEL, 0, 0);
  1037. if (err) {
  1038. dev_err(&pdev->dev, "couldn't allocate irq chips %s (DT).\n",
  1039. mvchip->chip.label);
  1040. goto err_domain;
  1041. }
  1042. /*
  1043. * NOTE: The common accessors cannot be used because of the percpu
  1044. * access to the mask registers
  1045. */
  1046. gc = irq_get_domain_generic_chip(mvchip->domain, 0);
  1047. gc->private = mvchip;
  1048. ct = &gc->chip_types[0];
  1049. ct->type = IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW;
  1050. ct->chip.irq_mask = mvebu_gpio_level_irq_mask;
  1051. ct->chip.irq_unmask = mvebu_gpio_level_irq_unmask;
  1052. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  1053. ct->chip.name = mvchip->chip.label;
  1054. ct = &gc->chip_types[1];
  1055. ct->type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
  1056. ct->chip.irq_ack = mvebu_gpio_irq_ack;
  1057. ct->chip.irq_mask = mvebu_gpio_edge_irq_mask;
  1058. ct->chip.irq_unmask = mvebu_gpio_edge_irq_unmask;
  1059. ct->chip.irq_set_type = mvebu_gpio_irq_set_type;
  1060. ct->handler = handle_edge_irq;
  1061. ct->chip.name = mvchip->chip.label;
  1062. /*
  1063. * Setup the interrupt handlers. Each chip can have up to 4
  1064. * interrupt handlers, with each handler dealing with 8 GPIO
  1065. * pins.
  1066. */
  1067. for (i = 0; i < 4; i++) {
  1068. int irq = platform_get_irq(pdev, i);
  1069. if (irq < 0)
  1070. continue;
  1071. irq_set_chained_handler_and_data(irq, mvebu_gpio_irq_handler,
  1072. mvchip);
  1073. }
  1074. /* Some MVEBU SoCs have simple PWM support for GPIO lines */
  1075. if (IS_ENABLED(CONFIG_PWM))
  1076. return mvebu_pwm_probe(pdev, mvchip, id);
  1077. return 0;
  1078. err_domain:
  1079. irq_domain_remove(mvchip->domain);
  1080. return err;
  1081. }
  1082. static struct platform_driver mvebu_gpio_driver = {
  1083. .driver = {
  1084. .name = "mvebu-gpio",
  1085. .of_match_table = mvebu_gpio_of_match,
  1086. },
  1087. .probe = mvebu_gpio_probe,
  1088. .suspend = mvebu_gpio_suspend,
  1089. .resume = mvebu_gpio_resume,
  1090. };
  1091. builtin_platform_driver(mvebu_gpio_driver);