blob: f460d71b0c92b379b79371ab6bbf6f3611515487 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
John Crispin935c5002011-03-30 09:27:56 +02002/*
John Crispin935c5002011-03-30 09:27:56 +02003 *
John Crispinbaddc7c2016-12-20 19:57:55 +01004 * Copyright (C) 2012 John Crispin <john@phrozen.org>
John Crispin935c5002011-03-30 09:27:56 +02005 */
6
7#include <linux/init.h>
John Crispina36e9a12012-05-16 22:17:38 +02008#include <linux/module.h>
John Crispin935c5002011-03-30 09:27:56 +02009#include <linux/types.h>
10#include <linux/platform_device.h>
11#include <linux/mutex.h>
Linus Walleij0cbbdcf2018-04-13 15:21:15 +020012#include <linux/gpio/driver.h>
John Crispina36e9a12012-05-16 22:17:38 +020013#include <linux/of.h>
14#include <linux/of_gpio.h>
John Crispin935c5002011-03-30 09:27:56 +020015#include <linux/io.h>
John Crispina36e9a12012-05-16 22:17:38 +020016#include <linux/slab.h>
John Crispin935c5002011-03-30 09:27:56 +020017
18#include <lantiq_soc.h>
19
20/*
21 * By attaching hardware latches to the EBU it is possible to create output
22 * only gpios. This driver configures a special memory address, which when
23 * written to outputs 16 bit to the latches.
24 */
25
26#define LTQ_EBU_BUSCON 0x1e7ff /* 16 bit access, slowest timing */
27#define LTQ_EBU_WP 0x80000000 /* write protect bit */
28
John Crispina36e9a12012-05-16 22:17:38 +020029struct ltq_mm {
30 struct of_mm_gpio_chip mmchip;
31 u16 shadow; /* shadow the latches state */
32};
John Crispin935c5002011-03-30 09:27:56 +020033
John Crispina36e9a12012-05-16 22:17:38 +020034/**
35 * ltq_mm_apply() - write the shadow value to the ebu address.
36 * @chip: Pointer to our private data structure.
37 *
38 * Write the shadow value to the EBU to set the gpios. We need to set the
39 * global EBU lock to make sure that PCI/MTD dont break.
40 */
41static void ltq_mm_apply(struct ltq_mm *chip)
John Crispin935c5002011-03-30 09:27:56 +020042{
43 unsigned long flags;
44
45 spin_lock_irqsave(&ebu_lock, flags);
46 ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1);
John Crispina36e9a12012-05-16 22:17:38 +020047 __raw_writew(chip->shadow, chip->mmchip.regs);
John Crispin935c5002011-03-30 09:27:56 +020048 ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1);
49 spin_unlock_irqrestore(&ebu_lock, flags);
50}
51
John Crispina36e9a12012-05-16 22:17:38 +020052/**
53 * ltq_mm_set() - gpio_chip->set - set gpios.
54 * @gc: Pointer to gpio_chip device structure.
55 * @gpio: GPIO signal number.
56 * @val: Value to be written to specified signal.
57 *
58 * Set the shadow value and call ltq_mm_apply.
59 */
60static void ltq_mm_set(struct gpio_chip *gc, unsigned offset, int value)
John Crispin935c5002011-03-30 09:27:56 +020061{
Linus Walleij6aa7dbf2015-12-07 10:19:29 +010062 struct ltq_mm *chip = gpiochip_get_data(gc);
John Crispina36e9a12012-05-16 22:17:38 +020063
John Crispin935c5002011-03-30 09:27:56 +020064 if (value)
John Crispina36e9a12012-05-16 22:17:38 +020065 chip->shadow |= (1 << offset);
John Crispin935c5002011-03-30 09:27:56 +020066 else
John Crispina36e9a12012-05-16 22:17:38 +020067 chip->shadow &= ~(1 << offset);
68 ltq_mm_apply(chip);
John Crispin935c5002011-03-30 09:27:56 +020069}
70
John Crispina36e9a12012-05-16 22:17:38 +020071/**
72 * ltq_mm_dir_out() - gpio_chip->dir_out - set gpio direction.
73 * @gc: Pointer to gpio_chip device structure.
74 * @gpio: GPIO signal number.
75 * @val: Value to be written to specified signal.
76 *
77 * Same as ltq_mm_set, always returns 0.
78 */
79static int ltq_mm_dir_out(struct gpio_chip *gc, unsigned offset, int value)
John Crispin935c5002011-03-30 09:27:56 +020080{
John Crispina36e9a12012-05-16 22:17:38 +020081 ltq_mm_set(gc, offset, value);
John Crispin935c5002011-03-30 09:27:56 +020082
83 return 0;
84}
85
John Crispina36e9a12012-05-16 22:17:38 +020086/**
87 * ltq_mm_save_regs() - Set initial values of GPIO pins
88 * @mm_gc: pointer to memory mapped GPIO chip structure
89 */
90static void ltq_mm_save_regs(struct of_mm_gpio_chip *mm_gc)
John Crispin935c5002011-03-30 09:27:56 +020091{
Guenter Roeck95c76172016-01-07 08:24:58 -080092 struct ltq_mm *chip =
93 container_of(mm_gc, struct ltq_mm, mmchip);
John Crispina36e9a12012-05-16 22:17:38 +020094
95 /* tell the ebu controller which memory address we will be using */
96 ltq_ebu_w32(CPHYSADDR(chip->mmchip.regs) | 0x1, LTQ_EBU_ADDRSEL1);
97
98 ltq_mm_apply(chip);
99}
100
101static int ltq_mm_probe(struct platform_device *pdev)
102{
John Crispina36e9a12012-05-16 22:17:38 +0200103 struct ltq_mm *chip;
Ricardo Ribalda Delgado68a99b12015-01-19 12:27:24 +0100104 u32 shadow;
John Crispin935c5002011-03-30 09:27:56 +0200105
Ricardo Ribalda Delgado080440a2015-01-18 12:39:27 +0100106 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
John Crispina36e9a12012-05-16 22:17:38 +0200107 if (!chip)
John Crispin935c5002011-03-30 09:27:56 +0200108 return -ENOMEM;
John Crispin935c5002011-03-30 09:27:56 +0200109
Ricardo Ribalda Delgadoda238222015-01-18 12:39:30 +0100110 platform_set_drvdata(pdev, chip);
111
John Crispina36e9a12012-05-16 22:17:38 +0200112 chip->mmchip.gc.ngpio = 16;
John Crispina36e9a12012-05-16 22:17:38 +0200113 chip->mmchip.gc.direction_output = ltq_mm_dir_out;
114 chip->mmchip.gc.set = ltq_mm_set;
115 chip->mmchip.save_regs = ltq_mm_save_regs;
John Crispin935c5002011-03-30 09:27:56 +0200116
John Crispina36e9a12012-05-16 22:17:38 +0200117 /* store the shadow value if one was passed by the devicetree */
Ricardo Ribalda Delgado68a99b12015-01-19 12:27:24 +0100118 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
119 chip->shadow = shadow;
John Crispin935c5002011-03-30 09:27:56 +0200120
Linus Walleij6aa7dbf2015-12-07 10:19:29 +0100121 return of_mm_gpiochip_add_data(pdev->dev.of_node, &chip->mmchip, chip);
John Crispin935c5002011-03-30 09:27:56 +0200122}
123
Ricardo Ribalda Delgadoda238222015-01-18 12:39:30 +0100124static int ltq_mm_remove(struct platform_device *pdev)
125{
126 struct ltq_mm *chip = platform_get_drvdata(pdev);
127
128 of_mm_gpiochip_remove(&chip->mmchip);
129
130 return 0;
131}
132
John Crispina36e9a12012-05-16 22:17:38 +0200133static const struct of_device_id ltq_mm_match[] = {
134 { .compatible = "lantiq,gpio-mm" },
135 {},
136};
137MODULE_DEVICE_TABLE(of, ltq_mm_match);
138
139static struct platform_driver ltq_mm_driver = {
140 .probe = ltq_mm_probe,
Ricardo Ribalda Delgadoda238222015-01-18 12:39:30 +0100141 .remove = ltq_mm_remove,
John Crispin935c5002011-03-30 09:27:56 +0200142 .driver = {
John Crispina36e9a12012-05-16 22:17:38 +0200143 .name = "gpio-mm-ltq",
John Crispina36e9a12012-05-16 22:17:38 +0200144 .of_match_table = ltq_mm_match,
John Crispin935c5002011-03-30 09:27:56 +0200145 },
146};
147
John Crispina36e9a12012-05-16 22:17:38 +0200148static int __init ltq_mm_init(void)
John Crispin935c5002011-03-30 09:27:56 +0200149{
John Crispina36e9a12012-05-16 22:17:38 +0200150 return platform_driver_register(&ltq_mm_driver);
John Crispin935c5002011-03-30 09:27:56 +0200151}
152
John Crispina36e9a12012-05-16 22:17:38 +0200153subsys_initcall(ltq_mm_init);
Ricardo Ribalda Delgadoda238222015-01-18 12:39:30 +0100154
155static void __exit ltq_mm_exit(void)
156{
157 platform_driver_unregister(&ltq_mm_driver);
158}
159module_exit(ltq_mm_exit);