gpio-mvebu.c 33 KB

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