John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 1 | /* |
| 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 Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012 John Crispin <blogic@openwrt.org> |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/init.h> |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 10 | #include <linux/module.h> |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/gpio.h> |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_gpio.h> |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 17 | #include <linux/io.h> |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 18 | #include <linux/slab.h> |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 19 | |
| 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 Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 31 | struct ltq_mm { |
| 32 | struct of_mm_gpio_chip mmchip; |
| 33 | u16 shadow; /* shadow the latches state */ |
| 34 | }; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 35 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 36 | /** |
| 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 | */ |
| 43 | static void ltq_mm_apply(struct ltq_mm *chip) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 44 | { |
| 45 | unsigned long flags; |
| 46 | |
| 47 | spin_lock_irqsave(&ebu_lock, flags); |
| 48 | ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1); |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 49 | __raw_writew(chip->shadow, chip->mmchip.regs); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 50 | ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1); |
| 51 | spin_unlock_irqrestore(&ebu_lock, flags); |
| 52 | } |
| 53 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 54 | /** |
| 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 | */ |
| 62 | 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] | 63 | { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 64 | struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc); |
| 65 | struct ltq_mm *chip = |
| 66 | container_of(mm_gc, struct ltq_mm, mmchip); |
| 67 | |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 68 | if (value) |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 69 | chip->shadow |= (1 << offset); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 70 | else |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 71 | chip->shadow &= ~(1 << offset); |
| 72 | ltq_mm_apply(chip); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 73 | } |
| 74 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 75 | /** |
| 76 | * ltq_mm_dir_out() - gpio_chip->dir_out - set gpio direction. |
| 77 | * @gc: Pointer to gpio_chip device structure. |
| 78 | * @gpio: GPIO signal number. |
| 79 | * @val: Value to be written to specified signal. |
| 80 | * |
| 81 | * Same as ltq_mm_set, always returns 0. |
| 82 | */ |
| 83 | 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] | 84 | { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 85 | ltq_mm_set(gc, offset, value); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 90 | /** |
| 91 | * ltq_mm_save_regs() - Set initial values of GPIO pins |
| 92 | * @mm_gc: pointer to memory mapped GPIO chip structure |
| 93 | */ |
| 94 | 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] | 95 | { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 96 | struct ltq_mm *chip = |
| 97 | container_of(mm_gc, struct ltq_mm, mmchip); |
| 98 | |
| 99 | /* tell the ebu controller which memory address we will be using */ |
| 100 | ltq_ebu_w32(CPHYSADDR(chip->mmchip.regs) | 0x1, LTQ_EBU_ADDRSEL1); |
| 101 | |
| 102 | ltq_mm_apply(chip); |
| 103 | } |
| 104 | |
| 105 | static int ltq_mm_probe(struct platform_device *pdev) |
| 106 | { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 107 | struct ltq_mm *chip; |
Ricardo Ribalda Delgado | 68a99b1 | 2015-01-19 12:27:24 +0100 | [diff] [blame] | 108 | u32 shadow; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 109 | |
Ricardo Ribalda Delgado | 080440a | 2015-01-18 12:39:27 +0100 | [diff] [blame] | 110 | chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL); |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 111 | if (!chip) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 112 | return -ENOMEM; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 113 | |
Ricardo Ribalda Delgado | da23822 | 2015-01-18 12:39:30 +0100 | [diff] [blame] | 114 | platform_set_drvdata(pdev, chip); |
| 115 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 116 | chip->mmchip.gc.ngpio = 16; |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 117 | chip->mmchip.gc.direction_output = ltq_mm_dir_out; |
| 118 | chip->mmchip.gc.set = ltq_mm_set; |
| 119 | chip->mmchip.save_regs = ltq_mm_save_regs; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 120 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 121 | /* store the shadow value if one was passed by the devicetree */ |
Ricardo Ribalda Delgado | 68a99b1 | 2015-01-19 12:27:24 +0100 | [diff] [blame] | 122 | if (!of_property_read_u32(pdev->dev.of_node, "lantiq,shadow", &shadow)) |
| 123 | chip->shadow = shadow; |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 124 | |
Ricardo Ribalda Delgado | 080440a | 2015-01-18 12:39:27 +0100 | [diff] [blame] | 125 | return of_mm_gpiochip_add(pdev->dev.of_node, &chip->mmchip); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Ricardo Ribalda Delgado | da23822 | 2015-01-18 12:39:30 +0100 | [diff] [blame] | 128 | static int ltq_mm_remove(struct platform_device *pdev) |
| 129 | { |
| 130 | struct ltq_mm *chip = platform_get_drvdata(pdev); |
| 131 | |
| 132 | of_mm_gpiochip_remove(&chip->mmchip); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 137 | static const struct of_device_id ltq_mm_match[] = { |
| 138 | { .compatible = "lantiq,gpio-mm" }, |
| 139 | {}, |
| 140 | }; |
| 141 | MODULE_DEVICE_TABLE(of, ltq_mm_match); |
| 142 | |
| 143 | static struct platform_driver ltq_mm_driver = { |
| 144 | .probe = ltq_mm_probe, |
Ricardo Ribalda Delgado | da23822 | 2015-01-18 12:39:30 +0100 | [diff] [blame] | 145 | .remove = ltq_mm_remove, |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 146 | .driver = { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 147 | .name = "gpio-mm-ltq", |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 148 | .of_match_table = ltq_mm_match, |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 149 | }, |
| 150 | }; |
| 151 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 152 | static int __init ltq_mm_init(void) |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 153 | { |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 154 | return platform_driver_register(<q_mm_driver); |
John Crispin | 935c500 | 2011-03-30 09:27:56 +0200 | [diff] [blame] | 155 | } |
| 156 | |
John Crispin | a36e9a1 | 2012-05-16 22:17:38 +0200 | [diff] [blame] | 157 | subsys_initcall(ltq_mm_init); |
Ricardo Ribalda Delgado | da23822 | 2015-01-18 12:39:30 +0100 | [diff] [blame] | 158 | |
| 159 | static void __exit ltq_mm_exit(void) |
| 160 | { |
| 161 | platform_driver_unregister(<q_mm_driver); |
| 162 | } |
| 163 | module_exit(ltq_mm_exit); |