gpio-mvebu.c 33 KB

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