Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 2 | /* |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 3 | * |
John Crispin | baddc7c | 2016-12-20 19:57:55 +0100 | [diff] [blame] | 4 | * Copyright (C) 2012 John Crispin <john@phrozen.org> |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/init.h> |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 9 | #include <linux/types.h> |
| 10 | #include <linux/platform_device.h> |
| 11 | #include <linux/mutex.h> |
Linus Walleij | 0cbbdcf | 2018-04-13 15:21:15 +0200 | [diff] [blame] | 12 | #include <linux/gpio/driver.h> |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 13 | #include <linux/of.h> |
| 14 | #include <linux/of_gpio.h> |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 15 | #include <linux/io.h> |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 17 | |
| 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 Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 29 | struct ltq_mm { |
| 30 | struct of_mm_gpio_chip mmchip; |
| 31 | u16 shadow; /* shadow the latches state */ |
| 32 | }; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 33 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 34 | /** |
| 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 | */ |
| 41 | static void ltq_mm_apply(struct ltq_mm *chip) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 42 | { |
| 43 | unsigned long flags; |
| 44 | |
| 45 | spin_lock_irqsave(&ebu_lock, flags); |
| 46 | ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1); |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 47 | __raw_writew(chip->shadow, chip->mmchip.regs); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 48 | ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1); |
| 49 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 50 | } |
| 51 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 52 | /** |
| 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 | */ |
| 60 | static void ltq_mm_set(struct gpio_chip *gc, unsigned offset, int value) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 61 | { |
Linus Walleij | 6aa7dbf | 2015-12-07 10:19:29 +0100 | [diff] [blame] | 62 | struct ltq_mm *chip = gpiochip_get_data(gc); |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 63 | |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 64 | if (value) |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 65 | chip->shadow |= (1 << offset); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 66 | else |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 67 | chip->shadow &= ~(1 << offset); |
| 68 | ltq_mm_apply(chip); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 69 | } |
| 70 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 71 | /** |
| 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 | */ |
| 79 | static int ltq_mm_dir_out(struct gpio_chip *gc, unsigned offset, int value) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 80 | { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 81 | ltq_mm_set(gc, offset, value); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 86 | /** |
| 87 | * ltq_mm_save_regs() - Set initial values of GPIO pins |
| 88 | * @mm_gc: pointer to memory mapped GPIO chip structure |
| 89 | */ |
| 90 | static void ltq_mm_save_regs(struct of_mm_gpio_chip *mm_gc) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 91 | { |
Guenter Roeck | 95c7617 | 2016-01-07 08:24:58 -0800 | [diff] [blame] | 92 | struct ltq_mm *chip = |
| 93 | container_of(mm_gc, struct ltq_mm, mmchip); |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 94 | |
| 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 | |
| 101 | static int ltq_mm_probe(struct platform_device *pdev) |
| 102 | { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 103 | struct ltq_mm *chip; |
Ricardo Ribalda Delgado | 68a99b1 | 2015-01-19 12:27:24 +0100 | [diff] [blame] | 104 | u32 shadow; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 105 | |
Ricardo Ribalda Delgado | 080440a | 2015-01-18 12:39:27 +0100 | [diff] [blame] | 106 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 107 | if (!chip) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 108 | return -ENOMEM; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 109 | |
Ricardo Ribalda Delgado | da23822 | 2015-01-18 12:39:30 +0100 | [diff] [blame] | 110 | platform_set_drvdata(pdev, chip); |
| 111 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 112 | chip->mmchip.gc.ngpio = 16; |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 113 | 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 Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 116 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 117 | /* store the shadow value if one was passed by the devicetree */ |
Ricardo Ribalda Delgado | 68a99b1 | 2015-01-19 12:27:24 +0100 | [diff] [blame] | 118 | if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow)) |
| 119 | chip->shadow = shadow; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 120 | |
Linus Walleij | 6aa7dbf | 2015-12-07 10:19:29 +0100 | [diff] [blame] | 121 | return of_mm_gpiochip_add_data(pdev->dev.of_node, &chip->mmchip, chip); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Ricardo Ribalda Delgado | da23822 | 2015-01-18 12:39:30 +0100 | [diff] [blame] | 124 | static 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 Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 133 | static const struct of_device_id ltq_mm_match[] = { |
| 134 | { .compatible = "lantiq,gpio-mm" }, |
| 135 | {}, |
| 136 | }; |
| 137 | MODULE_DEVICE_TABLE(of, ltq_mm_match); |
| 138 | |
| 139 | static struct platform_driver ltq_mm_driver = { |
| 140 | .probe = ltq_mm_probe, |
Ricardo Ribalda Delgado | da23822 | 2015-01-18 12:39:30 +0100 | [diff] [blame] | 141 | .remove = ltq_mm_remove, |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 142 | .driver = { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 143 | .name = "gpio-mm-ltq", |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 144 | .of_match_table = ltq_mm_match, |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 145 | }, |
| 146 | }; |
| 147 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 148 | static int __init ltq_mm_init(void) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 149 | { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 150 | return platform_driver_register(<q_mm_driver); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 151 | } |
| 152 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 153 | subsys_initcall(ltq_mm_init); |
Ricardo Ribalda Delgado | da23822 | 2015-01-18 12:39:30 +0100 | [diff] [blame] | 154 | |
| 155 | static void __exit ltq_mm_exit(void) |
| 156 | { |
| 157 | platform_driver_unregister(<q_mm_driver); |
| 158 | } |
| 159 | module_exit(ltq_mm_exit); |