gpio-mvebu.c 34 KB

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