blob: b1cf76dd84bacb6310616226a3f39dd02d560be4 [file] [log] [blame]
John Crispin935c5002011-03-30 09:27:56 +02001/*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
5 *
John Crispinbaddc7c2016-12-20 19:57:55 +01006 * Copyright (C) 2012 John Crispin <john@phrozen.org>
John Crispin935c5002011-03-30 09:27:56 +02007 */
8
9#include <linux/init.h>
John Crispina36e9a12012-05-16 22:17:38 +020010#include <linux/module.h>
John Crispin935c5002011-03-30 09:27:56 +020011#include <linux/types.h>
12#include <linux/platform_device.h>
13#include <linux/mutex.h>
14#include <linux/gpio.h>
John Crispina36e9a12012-05-16 22:17:38 +020015#include <linux/of.h>
16#include <linux/of_gpio.h>
John Crispin935c5002011-03-30 09:27:56 +020017#include <linux/io.h>
John Crispina36e9a12012-05-16 22:17:38 +020018#include <linux/slab.h>
John Crispin935c5002011-03-30 09:27:56 +020019
20#include <lantiq_soc.h>
21
22/*
23 * By attaching hardware latches to the EBU it is possible to create output
24 * only gpios. This driver configures a special memory address, which when
25 * written to outputs 16 bit to the latches.
26 */
27
28#define LTQ_EBU_BUSCON 0x1e7ff /* 16 bit access, slowest timing */
29#define LTQ_EBU_WP 0x80000000 /* write protect bit */
30
John Crispina36e9a12012-05-16 22:17:38 +020031struct ltq_mm {
32 struct of_mm_gpio_chip mmchip;
33 u16 shadow; /* shadow the latches state */
34};
John Crispin935c5002011-03-30 09:27:56 +020035
John Crispina36e9a12012-05-16 22:17:38 +020036/**
37 * ltq_mm_apply() - write the shadow value to the ebu address.
38 * @chip: Pointer to our private data structure.
39 *
40 * Write the shadow value to the EBU to set the gpios. We need to set the
41 * global EBU lock to make sure that PCI/MTD dont break.
42 */
43static void ltq_mm_apply(struct ltq_mm *chip)
John Crispin935c5002011-03-30 09:27:56 +020044{
45 unsigned long flags;
46
47 spin_lock_irqsave(&ebu_lock, flags);
48 ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1);
John Crispina36e9a12012-05-16 22:17:38 +020049 __raw_writew(chip->shadow, chip->mmchip.regs);
John Crispin935c5002011-03-30 09:27:56 +020050 ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1);
51 spin_unlock_irqrestore(&ebu_lock, flags);
52}
53
John Crispina36e9a12012-05-16 22:17:38 +020054/**
55 * ltq_mm_set() - gpio_chip->set - set gpios.
56 * @gc: Pointer to gpio_chip device structure.
57 * @gpio: GPIO signal number.
58 * @val: Value to be written to specified signal.
59 *
60 * Set the shadow value and call ltq_mm_apply.
61 */
62static void ltq_mm_set(struct gpio_chip *gc, unsigned offset, int value)
John Crispin935c5002011-03-30 09:27:56 +020063{
Linus Walleij6aa7dbf2015-12-07 10:19:29 +010064 struct ltq_mm *chip = gpiochip_get_data(gc);
John Crispina36e9a12012-05-16 22:17:38 +020065
John Crispin935c5002011-03-30 09:27:56 +020066 if (value)
John Crispina36e9a12012-05-16 22:17:38 +020067 chip->shadow |= (1 << offset);
John Crispin935c5002011-03-30 09:27:56 +020068 else
John Crispina36e9a12012-05-16 22:17:38 +020069 chip->shadow &= ~(1 << offset);
70 ltq_mm_apply(chip);
John Crispin935c5002011-03-30 09:27:56 +020071}
72
John Crispina36e9a12012-05-16 22:17:38 +020073/**
74 * ltq_mm_dir_out() - gpio_chip->dir_out - set gpio direction.
75 * @gc: Pointer to gpio_chip device structure.
76 * @gpio: GPIO signal number.
77 * @val: Value to be written to specified signal.
78 *
79 * Same as ltq_mm_set, always returns 0.
80 */
81static int ltq_mm_dir_out(struct gpio_chip *gc, unsigned offset, int value)
John Crispin935c5002011-03-30 09:27:56 +020082{
John Crispina36e9a12012-05-16 22:17:38 +020083 ltq_mm_set(gc, offset, value);
John Crispin935c5002011-03-30 09:27:56 +020084
85 return 0;
86}
87
John Crispina36e9a12012-05-16 22:17:38 +020088/**
89 * ltq_mm_save_regs() - Set initial values of GPIO pins
90 * @mm_gc: pointer to memory mapped GPIO chip structure
91 */
92static void ltq_mm_save_regs(struct of_mm_gpio_chip *mm_gc)
John Crispin935c5002011-03-30 09:27:56 +020093{
Guenter Roeck95c76172016-01-07 08:24:58 -080094 struct ltq_mm *chip =
95 container_of(mm_gc, struct ltq_mm, mmchip);
John Crispina36e9a12012-05-16 22:17:38 +020096
97 /* tell the ebu controller which memory address we will be using */
98 ltq_ebu_w32(CPHYSADDR(chip->mmchip.regs) | 0x1, LTQ_EBU_ADDRSEL1);
99
100 ltq_mm_apply(chip);
101}
102
103static int ltq_mm_probe(struct platform_device *pdev)
104{
John Crispina36e9a12012-05-16 22:17:38 +0200105 struct ltq_mm *chip;
Ricardo Ribalda Delgado68a99b12015-01-19 12:27:24 +0100106 u32 shadow;
John Crispin935c5002011-03-30 09:27:56 +0200107
Ricardo Ribalda Delgado080440a2015-01-18 12:39:27 +0100108 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
John Crispina36e9a12012-05-16 22:17:38 +0200109 if (!chip)
John Crispin935c5002011-03-30 09:27:56 +0200110 return -ENOMEM;
John Crispin935c5002011-03-30 09:27:56 +0200111
Ricardo Ribalda Delgadoda238222015-01-18 12:39:30 +0100112 platform_set_drvdata(pdev, chip);
113
John Crispina36e9a12012-05-16 22:17:38 +0200114 chip->mmchip.gc.ngpio = 16;
John Crispina36e9a12012-05-16 22:17:38 +0200115 chip->mmchip.gc.direction_output = ltq_mm_dir_out;
116 chip->mmchip.gc.set = ltq_mm_set;
117 chip->mmchip.save_regs = ltq_mm_save_regs;
John Crispin935c5002011-03-30 09:27:56 +0200118
John Crispina36e9a12012-05-16 22:17:38 +0200119 /* store the shadow value if one was passed by the devicetree */
Ricardo Ribalda Delgado68a99b12015-01-19 12:27:24 +0100120 if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow))
121 chip->shadow = shadow;
John Crispin935c5002011-03-30 09:27:56 +0200122
Linus Walleij6aa7dbf2015-12-07 10:19:29 +0100123 return of_mm_gpiochip_add_data(pdev->dev.of_node, &chip->mmchip, chip);
John Crispin935c5002011-03-30 09:27:56 +0200124}
125
Ricardo Ribalda Delgadoda238222015-01-18 12:39:30 +0100126static int ltq_mm_remove(struct platform_device *pdev)
127{
128 struct ltq_mm *chip = platform_get_drvdata(pdev);
129
130 of_mm_gpiochip_remove(&chip->mmchip);
131
132 return 0;
133}
134
John Crispina36e9a12012-05-16 22:17:38 +0200135static const struct of_device_id ltq_mm_match[] = {
136 { .compatible = "lantiq,gpio-mm" },
137 {},
138};
139MODULE_DEVICE_TABLE(of, ltq_mm_match);
140
141static struct platform_driver ltq_mm_driver = {
142 .probe = ltq_mm_probe,
Ricardo Ribalda Delgadoda238222015-01-18 12:39:30 +0100143 .remove = ltq_mm_remove,
John Crispin935c5002011-03-30 09:27:56 +0200144 .driver = {
John Crispina36e9a12012-05-16 22:17:38 +0200145 .name = "gpio-mm-ltq",
John Crispina36e9a12012-05-16 22:17:38 +0200146 .of_match_table = ltq_mm_match,
John Crispin935c5002011-03-30 09:27:56 +0200147 },
148};
149
John Crispina36e9a12012-05-16 22:17:38 +0200150static int __init ltq_mm_init(void)
John Crispin935c5002011-03-30 09:27:56 +0200151{
John Crispina36e9a12012-05-16 22:17:38 +0200152 return platform_driver_register(&ltq_mm_driver);
John Crispin935c5002011-03-30 09:27:56 +0200153}
154
John Crispina36e9a12012-05-16 22:17:38 +0200155subsys_initcall(ltq_mm_init);
Ricardo Ribalda Delgadoda238222015-01-18 12:39:30 +0100156
157static void __exit ltq_mm_exit(void)
158{
159 platform_driver_unregister(&ltq_mm_driver);
160}
161module_exit(ltq_mm_exit);