gpio-mvebu.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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/driver.h>
  39. #include <linux/gpio/consumer.h>
  40. #include <linux/init.h>
  41. #include <linux/io.h>
  42. #include <linux/irq.h>
  43. #include <linux/irqchip/chained_irq.h>
  44. #include <linux/irqdomain.h>
  45. #include <linux/mfd/syscon.h>
  46. #include <linux/of_device.h>
  47. #include <linux/of_irq.h>
  48. #include <linux/pinctrl/consumer.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/pwm.h>
  51. #include <linux/regmap.h>
  52. #include <linux/slab.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 = gpiochip_request_own_desc(&mvchip->chip,
  518. pwm->hwpwm, "mvebu-pwm");
  519. if (IS_ERR(desc)) {
  520. ret = PTR_ERR(desc);
  521. goto out;
  522. }
  523. ret = gpiod_direction_output(desc, 0);
  524. if (ret) {
  525. gpiochip_free_own_desc(desc);
  526. goto out;
  527. }
  528. mvpwm->gpiod = desc;
  529. }
  530. out:
  531. spin_unlock_irqrestore(&mvpwm->lock, flags);
  532. return ret;
  533. }
  534. static void mvebu_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  535. {
  536. struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
  537. unsigned long flags;
  538. spin_lock_irqsave(&mvpwm->lock, flags);
  539. gpiochip_free_own_desc(mvpwm->gpiod);
  540. mvpwm->gpiod = NULL;
  541. spin_unlock_irqrestore(&mvpwm->lock, flags);
  542. }
  543. static void mvebu_pwm_get_state(struct pwm_chip *chip,
  544. struct pwm_device *pwm,
  545. struct pwm_state *state) {
  546. struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
  547. struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
  548. unsigned long long val;
  549. unsigned long flags;
  550. u32 u;
  551. spin_lock_irqsave(&mvpwm->lock, flags);
  552. u = readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
  553. val = (unsigned long long) u * NSEC_PER_SEC;
  554. do_div(val, mvpwm->clk_rate);
  555. if (val > UINT_MAX)
  556. state->duty_cycle = UINT_MAX;
  557. else if (val)
  558. state->duty_cycle = val;
  559. else
  560. state->duty_cycle = 1;
  561. val = (unsigned long long) u; /* on duration */
  562. /* period = on + off duration */
  563. val += readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
  564. val *= NSEC_PER_SEC;
  565. do_div(val, mvpwm->clk_rate);
  566. if (val > UINT_MAX)
  567. state->period = UINT_MAX;
  568. else if (val)
  569. state->period = val;
  570. else
  571. state->period = 1;
  572. regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &u);
  573. if (u)
  574. state->enabled = true;
  575. else
  576. state->enabled = false;
  577. spin_unlock_irqrestore(&mvpwm->lock, flags);
  578. }
  579. static int mvebu_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  580. struct pwm_state *state)
  581. {
  582. struct mvebu_pwm *mvpwm = to_mvebu_pwm(chip);
  583. struct mvebu_gpio_chip *mvchip = mvpwm->mvchip;
  584. unsigned long long val;
  585. unsigned long flags;
  586. unsigned int on, off;
  587. val = (unsigned long long) mvpwm->clk_rate * state->duty_cycle;
  588. do_div(val, NSEC_PER_SEC);
  589. if (val > UINT_MAX)
  590. return -EINVAL;
  591. if (val)
  592. on = val;
  593. else
  594. on = 1;
  595. val = (unsigned long long) mvpwm->clk_rate *
  596. (state->period - state->duty_cycle);
  597. do_div(val, NSEC_PER_SEC);
  598. if (val > UINT_MAX)
  599. return -EINVAL;
  600. if (val)
  601. off = val;
  602. else
  603. off = 1;
  604. spin_lock_irqsave(&mvpwm->lock, flags);
  605. writel_relaxed(on, mvebu_pwmreg_blink_on_duration(mvpwm));
  606. writel_relaxed(off, mvebu_pwmreg_blink_off_duration(mvpwm));
  607. if (state->enabled)
  608. mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 1);
  609. else
  610. mvebu_gpio_blink(&mvchip->chip, pwm->hwpwm, 0);
  611. spin_unlock_irqrestore(&mvpwm->lock, flags);
  612. return 0;
  613. }
  614. static const struct pwm_ops mvebu_pwm_ops = {
  615. .request = mvebu_pwm_request,
  616. .free = mvebu_pwm_free,
  617. .get_state = mvebu_pwm_get_state,
  618. .apply = mvebu_pwm_apply,
  619. .owner = THIS_MODULE,
  620. };
  621. static void __maybe_unused mvebu_pwm_suspend(struct mvebu_gpio_chip *mvchip)
  622. {
  623. struct mvebu_pwm *mvpwm = mvchip->mvpwm;
  624. regmap_read(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
  625. &mvpwm->blink_select);
  626. mvpwm->blink_on_duration =
  627. readl_relaxed(mvebu_pwmreg_blink_on_duration(mvpwm));
  628. mvpwm->blink_off_duration =
  629. readl_relaxed(mvebu_pwmreg_blink_off_duration(mvpwm));
  630. }
  631. static void __maybe_unused mvebu_pwm_resume(struct mvebu_gpio_chip *mvchip)
  632. {
  633. struct mvebu_pwm *mvpwm = mvchip->mvpwm;
  634. regmap_write(mvchip->regs, GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset,
  635. mvpwm->blink_select);
  636. writel_relaxed(mvpwm->blink_on_duration,
  637. mvebu_pwmreg_blink_on_duration(mvpwm));
  638. writel_relaxed(mvpwm->blink_off_duration,
  639. mvebu_pwmreg_blink_off_duration(mvpwm));
  640. }
  641. static int mvebu_pwm_probe(struct platform_device *pdev,
  642. struct mvebu_gpio_chip *mvchip,
  643. int id)
  644. {
  645. struct device *dev = &pdev->dev;
  646. struct mvebu_pwm *mvpwm;
  647. struct resource *res;
  648. u32 set;
  649. if (!of_device_is_compatible(mvchip->chip.of_node,
  650. "marvell,armada-370-gpio"))
  651. return 0;
  652. /*
  653. * There are only two sets of PWM configuration registers for
  654. * all the GPIO lines on those SoCs which this driver reserves
  655. * for the first two GPIO chips. So if the resource is missing
  656. * we can't treat it as an error.
  657. */
  658. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pwm");
  659. if (!res)
  660. return 0;
  661. if (IS_ERR(mvchip->clk))
  662. return PTR_ERR(mvchip->clk);
  663. /*
  664. * Use set A for lines of GPIO chip with id 0, B for GPIO chip
  665. * with id 1. Don't allow further GPIO chips to be used for PWM.
  666. */
  667. if (id == 0)
  668. set = 0;
  669. else if (id == 1)
  670. set = U32_MAX;
  671. else
  672. return -EINVAL;
  673. regmap_write(mvchip->regs,
  674. GPIO_BLINK_CNT_SELECT_OFF + mvchip->offset, set);
  675. mvpwm = devm_kzalloc(dev, sizeof(struct mvebu_pwm), GFP_KERNEL);
  676. if (!mvpwm)
  677. return -ENOMEM;
  678. mvchip->mvpwm = mvpwm;
  679. mvpwm->mvchip = mvchip;
  680. mvpwm->membase = devm_ioremap_resource(dev, res);
  681. if (IS_ERR(mvpwm->membase))
  682. return PTR_ERR(mvpwm->membase);
  683. mvpwm->clk_rate = clk_get_rate(mvchip->clk);
  684. if (!mvpwm->clk_rate) {
  685. dev_err(dev, "failed to get clock rate\n");
  686. return -EINVAL;
  687. }
  688. mvpwm->chip.dev = dev;
  689. mvpwm->chip.ops = &mvebu_pwm_ops;
  690. mvpwm->chip.npwm = mvchip->chip.ngpio;
  691. /*
  692. * There may already be some PWM allocated, so we can't force
  693. * mvpwm->chip.base to a fixed point like mvchip->chip.base.
  694. * So, we let pwmchip_add() do the numbering and take the next free
  695. * region.
  696. */
  697. mvpwm->chip.base = -1;
  698. spin_lock_init(&mvpwm->lock);
  699. return pwmchip_add(&mvpwm->chip);
  700. }
  701. #ifdef CONFIG_DEBUG_FS
  702. #include <linux/seq_file.h>
  703. static void mvebu_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  704. {
  705. struct mvebu_gpio_chip *mvchip = gpiochip_get_data(chip);
  706. u32 out, io_conf, blink, in_pol, data_in, cause, edg_msk, lvl_msk;
  707. int i;
  708. regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset, &out);
  709. regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset, &io_conf);
  710. regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset, &blink);
  711. regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset, &in_pol);
  712. regmap_read(mvchip->regs, GPIO_DATA_IN_OFF + mvchip->offset, &data_in);
  713. cause = mvebu_gpio_read_edge_cause(mvchip);
  714. edg_msk = mvebu_gpio_read_edge_mask(mvchip);
  715. lvl_msk = mvebu_gpio_read_level_mask(mvchip);
  716. for (i = 0; i < chip->ngpio; i++) {
  717. const char *label;
  718. u32 msk;
  719. bool is_out;
  720. label = gpiochip_is_requested(chip, i);
  721. if (!label)
  722. continue;
  723. msk = BIT(i);
  724. is_out = !(io_conf & msk);
  725. seq_printf(s, " gpio-%-3d (%-20.20s)", chip->base + i, label);
  726. if (is_out) {
  727. seq_printf(s, " out %s %s\n",
  728. out & msk ? "hi" : "lo",
  729. blink & msk ? "(blink )" : "");
  730. continue;
  731. }
  732. seq_printf(s, " in %s (act %s) - IRQ",
  733. (data_in ^ in_pol) & msk ? "hi" : "lo",
  734. in_pol & msk ? "lo" : "hi");
  735. if (!((edg_msk | lvl_msk) & msk)) {
  736. seq_puts(s, " disabled\n");
  737. continue;
  738. }
  739. if (edg_msk & msk)
  740. seq_puts(s, " edge ");
  741. if (lvl_msk & msk)
  742. seq_puts(s, " level");
  743. seq_printf(s, " (%s)\n", cause & msk ? "pending" : "clear ");
  744. }
  745. }
  746. #else
  747. #define mvebu_gpio_dbg_show NULL
  748. #endif
  749. static const struct of_device_id mvebu_gpio_of_match[] = {
  750. {
  751. .compatible = "marvell,orion-gpio",
  752. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
  753. },
  754. {
  755. .compatible = "marvell,mv78200-gpio",
  756. .data = (void *) MVEBU_GPIO_SOC_VARIANT_MV78200,
  757. },
  758. {
  759. .compatible = "marvell,armadaxp-gpio",
  760. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ARMADAXP,
  761. },
  762. {
  763. .compatible = "marvell,armada-370-gpio",
  764. .data = (void *) MVEBU_GPIO_SOC_VARIANT_ORION,
  765. },
  766. {
  767. .compatible = "marvell,armada-8k-gpio",
  768. .data = (void *) MVEBU_GPIO_SOC_VARIANT_A8K,
  769. },
  770. {
  771. /* sentinel */
  772. },
  773. };
  774. static int mvebu_gpio_suspend(struct platform_device *pdev, pm_message_t state)
  775. {
  776. struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
  777. int i;
  778. regmap_read(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
  779. &mvchip->out_reg);
  780. regmap_read(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
  781. &mvchip->io_conf_reg);
  782. regmap_read(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
  783. &mvchip->blink_en_reg);
  784. regmap_read(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
  785. &mvchip->in_pol_reg);
  786. switch (mvchip->soc_variant) {
  787. case MVEBU_GPIO_SOC_VARIANT_ORION:
  788. case MVEBU_GPIO_SOC_VARIANT_A8K:
  789. regmap_read(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
  790. &mvchip->edge_mask_regs[0]);
  791. regmap_read(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
  792. &mvchip->level_mask_regs[0]);
  793. break;
  794. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  795. for (i = 0; i < 2; i++) {
  796. regmap_read(mvchip->regs,
  797. GPIO_EDGE_MASK_MV78200_OFF(i),
  798. &mvchip->edge_mask_regs[i]);
  799. regmap_read(mvchip->regs,
  800. GPIO_LEVEL_MASK_MV78200_OFF(i),
  801. &mvchip->level_mask_regs[i]);
  802. }
  803. break;
  804. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  805. for (i = 0; i < 4; i++) {
  806. regmap_read(mvchip->regs,
  807. GPIO_EDGE_MASK_ARMADAXP_OFF(i),
  808. &mvchip->edge_mask_regs[i]);
  809. regmap_read(mvchip->regs,
  810. GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
  811. &mvchip->level_mask_regs[i]);
  812. }
  813. break;
  814. default:
  815. BUG();
  816. }
  817. if (IS_ENABLED(CONFIG_PWM))
  818. mvebu_pwm_suspend(mvchip);
  819. return 0;
  820. }
  821. static int mvebu_gpio_resume(struct platform_device *pdev)
  822. {
  823. struct mvebu_gpio_chip *mvchip = platform_get_drvdata(pdev);
  824. int i;
  825. regmap_write(mvchip->regs, GPIO_OUT_OFF + mvchip->offset,
  826. mvchip->out_reg);
  827. regmap_write(mvchip->regs, GPIO_IO_CONF_OFF + mvchip->offset,
  828. mvchip->io_conf_reg);
  829. regmap_write(mvchip->regs, GPIO_BLINK_EN_OFF + mvchip->offset,
  830. mvchip->blink_en_reg);
  831. regmap_write(mvchip->regs, GPIO_IN_POL_OFF + mvchip->offset,
  832. mvchip->in_pol_reg);
  833. switch (mvchip->soc_variant) {
  834. case MVEBU_GPIO_SOC_VARIANT_ORION:
  835. case MVEBU_GPIO_SOC_VARIANT_A8K:
  836. regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF + mvchip->offset,
  837. mvchip->edge_mask_regs[0]);
  838. regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF + mvchip->offset,
  839. mvchip->level_mask_regs[0]);
  840. break;
  841. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  842. for (i = 0; i < 2; i++) {
  843. regmap_write(mvchip->regs,
  844. GPIO_EDGE_MASK_MV78200_OFF(i),
  845. mvchip->edge_mask_regs[i]);
  846. regmap_write(mvchip->regs,
  847. GPIO_LEVEL_MASK_MV78200_OFF(i),
  848. mvchip->level_mask_regs[i]);
  849. }
  850. break;
  851. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  852. for (i = 0; i < 4; i++) {
  853. regmap_write(mvchip->regs,
  854. GPIO_EDGE_MASK_ARMADAXP_OFF(i),
  855. mvchip->edge_mask_regs[i]);
  856. regmap_write(mvchip->regs,
  857. GPIO_LEVEL_MASK_ARMADAXP_OFF(i),
  858. mvchip->level_mask_regs[i]);
  859. }
  860. break;
  861. default:
  862. BUG();
  863. }
  864. if (IS_ENABLED(CONFIG_PWM))
  865. mvebu_pwm_resume(mvchip);
  866. return 0;
  867. }
  868. static const struct regmap_config mvebu_gpio_regmap_config = {
  869. .reg_bits = 32,
  870. .reg_stride = 4,
  871. .val_bits = 32,
  872. .fast_io = true,
  873. };
  874. static int mvebu_gpio_probe_raw(struct platform_device *pdev,
  875. struct mvebu_gpio_chip *mvchip)
  876. {
  877. struct resource *res;
  878. void __iomem *base;
  879. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  880. base = devm_ioremap_resource(&pdev->dev, res);
  881. if (IS_ERR(base))
  882. return PTR_ERR(base);
  883. mvchip->regs = devm_regmap_init_mmio(&pdev->dev, base,
  884. &mvebu_gpio_regmap_config);
  885. if (IS_ERR(mvchip->regs))
  886. return PTR_ERR(mvchip->regs);
  887. /*
  888. * For the legacy SoCs, the regmap directly maps to the GPIO
  889. * registers, so no offset is needed.
  890. */
  891. mvchip->offset = 0;
  892. /*
  893. * The Armada XP has a second range of registers for the
  894. * per-CPU registers
  895. */
  896. if (mvchip->soc_variant == MVEBU_GPIO_SOC_VARIANT_ARMADAXP) {
  897. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  898. base = devm_ioremap_resource(&pdev->dev, res);
  899. if (IS_ERR(base))
  900. return PTR_ERR(base);
  901. mvchip->percpu_regs =
  902. devm_regmap_init_mmio(&pdev->dev, base,
  903. &mvebu_gpio_regmap_config);
  904. if (IS_ERR(mvchip->percpu_regs))
  905. return PTR_ERR(mvchip->percpu_regs);
  906. }
  907. return 0;
  908. }
  909. static int mvebu_gpio_probe_syscon(struct platform_device *pdev,
  910. struct mvebu_gpio_chip *mvchip)
  911. {
  912. mvchip->regs = syscon_node_to_regmap(pdev->dev.parent->of_node);
  913. if (IS_ERR(mvchip->regs))
  914. return PTR_ERR(mvchip->regs);
  915. if (of_property_read_u32(pdev->dev.of_node, "offset", &mvchip->offset))
  916. return -EINVAL;
  917. return 0;
  918. }
  919. static int mvebu_gpio_probe(struct platform_device *pdev)
  920. {
  921. struct mvebu_gpio_chip *mvchip;
  922. const struct of_device_id *match;
  923. struct device_node *np = pdev->dev.of_node;
  924. struct irq_chip_generic *gc;
  925. struct irq_chip_type *ct;
  926. unsigned int ngpios;
  927. bool have_irqs;
  928. int soc_variant;
  929. int i, cpu, id;
  930. int err;
  931. match = of_match_device(mvebu_gpio_of_match, &pdev->dev);
  932. if (match)
  933. soc_variant = (unsigned long) match->data;
  934. else
  935. soc_variant = MVEBU_GPIO_SOC_VARIANT_ORION;
  936. /* Some gpio controllers do not provide irq support */
  937. have_irqs = of_irq_count(np) != 0;
  938. mvchip = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_gpio_chip),
  939. GFP_KERNEL);
  940. if (!mvchip)
  941. return -ENOMEM;
  942. platform_set_drvdata(pdev, mvchip);
  943. if (of_property_read_u32(pdev->dev.of_node, "ngpios", &ngpios)) {
  944. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  945. return -ENODEV;
  946. }
  947. id = of_alias_get_id(pdev->dev.of_node, "gpio");
  948. if (id < 0) {
  949. dev_err(&pdev->dev, "Couldn't get OF id\n");
  950. return id;
  951. }
  952. mvchip->clk = devm_clk_get(&pdev->dev, NULL);
  953. /* Not all SoCs require a clock.*/
  954. if (!IS_ERR(mvchip->clk))
  955. clk_prepare_enable(mvchip->clk);
  956. mvchip->soc_variant = soc_variant;
  957. mvchip->chip.label = dev_name(&pdev->dev);
  958. mvchip->chip.parent = &pdev->dev;
  959. mvchip->chip.request = gpiochip_generic_request;
  960. mvchip->chip.free = gpiochip_generic_free;
  961. mvchip->chip.direction_input = mvebu_gpio_direction_input;
  962. mvchip->chip.get = mvebu_gpio_get;
  963. mvchip->chip.direction_output = mvebu_gpio_direction_output;
  964. mvchip->chip.set = mvebu_gpio_set;
  965. if (have_irqs)
  966. mvchip->chip.to_irq = mvebu_gpio_to_irq;
  967. mvchip->chip.base = id * MVEBU_MAX_GPIO_PER_BANK;
  968. mvchip->chip.ngpio = ngpios;
  969. mvchip->chip.can_sleep = false;
  970. mvchip->chip.of_node = np;
  971. mvchip->chip.dbg_show = mvebu_gpio_dbg_show;
  972. if (soc_variant == MVEBU_GPIO_SOC_VARIANT_A8K)
  973. err = mvebu_gpio_probe_syscon(pdev, mvchip);
  974. else
  975. err = mvebu_gpio_probe_raw(pdev, mvchip);
  976. if (err)
  977. return err;
  978. /*
  979. * Mask and clear GPIO interrupts.
  980. */
  981. switch (soc_variant) {
  982. case MVEBU_GPIO_SOC_VARIANT_ORION:
  983. case MVEBU_GPIO_SOC_VARIANT_A8K:
  984. regmap_write(mvchip->regs,
  985. GPIO_EDGE_CAUSE_OFF + mvchip->offset, 0);
  986. regmap_write(mvchip->regs,
  987. GPIO_EDGE_MASK_OFF + mvchip->offset, 0);
  988. regmap_write(mvchip->regs,
  989. GPIO_LEVEL_MASK_OFF + mvchip->offset, 0);
  990. break;
  991. case MVEBU_GPIO_SOC_VARIANT_MV78200:
  992. regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
  993. for (cpu = 0; cpu < 2; cpu++) {
  994. regmap_write(mvchip->regs,
  995. GPIO_EDGE_MASK_MV78200_OFF(cpu), 0);
  996. regmap_write(mvchip->regs,
  997. GPIO_LEVEL_MASK_MV78200_OFF(cpu), 0);
  998. }
  999. break;
  1000. case MVEBU_GPIO_SOC_VARIANT_ARMADAXP:
  1001. regmap_write(mvchip->regs, GPIO_EDGE_CAUSE_OFF, 0);
  1002. regmap_write(mvchip->regs, GPIO_EDGE_MASK_OFF, 0);
  1003. regmap_write(mvchip->regs, GPIO_LEVEL_MASK_OFF, 0);
  1004. for (cpu = 0; cpu < 4; cpu++) {
  1005. regmap_write(mvchip->percpu_regs,
  1006. GPIO_EDGE_CAUSE_ARMADAXP_OFF(cpu), 0);
  1007. regmap_write(mvchip->percpu_regs,
  1008. GPIO_EDGE_MASK_ARMADAXP_OFF(cpu), 0);
  1009. regmap_write(mvchip->percpu_regs,
  1010. GPIO_LEVEL_MASK_ARMADAXP_OFF(cpu), 0);
  1011. }
  1012. break;
  1013. default:
  1014. BUG();
  1015. }
  1016. devm_gpiochip_add_data(&pdev->dev, &mvchip->chip, mvchip);
  1017. /* Some MVEBU SoCs have simple PWM support for GPIO lines */
  1018. if (IS_ENABLED(CONFIG_PWM)) {
  1019. err = mvebu_pwm_probe(pdev, mvchip, id);
  1020. if (err)
  1021. return err;
  1022. }
  1023. /* Some gpio controllers do not provide irq support */
  1024. if (!have_irqs)
  1025. return 0;
  1026. mvchip->domain =
  1027. irq_domain_add_linear(np, ngpios, &irq_generic_chip_ops, NULL);
  1028. if (!mvchip->domain) {
  1029. dev_err(&pdev->dev, "couldn't allocate irq domain %s (DT).\n",
  1030. mvchip->chip.label);
  1031. err = -ENODEV;
  1032. goto err_pwm;
  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. return 0;
  1075. err_domain:
  1076. irq_domain_remove(mvchip->domain);
  1077. err_pwm:
  1078. pwmchip_remove(&mvchip->mvpwm->chip);
  1079. return err;
  1080. }
  1081. static struct platform_driver mvebu_gpio_driver = {
  1082. .driver = {
  1083. .name = "mvebu-gpio",
  1084. .of_match_table = mvebu_gpio_of_match,
  1085. },
  1086. .probe = mvebu_gpio_probe,
  1087. .suspend = mvebu_gpio_suspend,
  1088. .resume = mvebu_gpio_resume,
  1089. };
  1090. builtin_platform_driver(mvebu_gpio_driver);