Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2021 Pengutronix, Oleksij Rempel <kernel@pengutronix.de> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/counter.h> |
| 7 | #include <linux/gpio/consumer.h> |
| 8 | #include <linux/interrupt.h> |
| 9 | #include <linux/irq.h> |
| 10 | #include <linux/mod_devicetable.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/platform_device.h> |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 13 | #include <linux/types.h> |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 14 | |
| 15 | #define INTERRUPT_CNT_NAME "interrupt-cnt" |
| 16 | |
| 17 | struct interrupt_cnt_priv { |
| 18 | atomic_t count; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 19 | struct gpio_desc *gpio; |
| 20 | int irq; |
| 21 | bool enabled; |
| 22 | struct counter_signal signals; |
| 23 | struct counter_synapse synapses; |
| 24 | struct counter_count cnts; |
| 25 | }; |
| 26 | |
| 27 | static irqreturn_t interrupt_cnt_isr(int irq, void *dev_id) |
| 28 | { |
| 29 | struct interrupt_cnt_priv *priv = dev_id; |
| 30 | |
| 31 | atomic_inc(&priv->count); |
| 32 | |
| 33 | return IRQ_HANDLED; |
| 34 | } |
| 35 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 36 | static int interrupt_cnt_enable_read(struct counter_device *counter, |
| 37 | struct counter_count *count, u8 *enable) |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 38 | { |
Uwe Kleine-König | 63f0e2b | 2021-12-30 16:02:43 +0100 | [diff] [blame] | 39 | struct interrupt_cnt_priv *priv = counter_priv(counter); |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 40 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 41 | *enable = priv->enabled; |
| 42 | |
| 43 | return 0; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 44 | } |
| 45 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 46 | static int interrupt_cnt_enable_write(struct counter_device *counter, |
| 47 | struct counter_count *count, u8 enable) |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 48 | { |
Uwe Kleine-König | 63f0e2b | 2021-12-30 16:02:43 +0100 | [diff] [blame] | 49 | struct interrupt_cnt_priv *priv = counter_priv(counter); |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 50 | |
| 51 | if (priv->enabled == enable) |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 52 | return 0; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 53 | |
| 54 | if (enable) { |
| 55 | priv->enabled = true; |
| 56 | enable_irq(priv->irq); |
| 57 | } else { |
| 58 | disable_irq(priv->irq); |
| 59 | priv->enabled = false; |
| 60 | } |
| 61 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 62 | return 0; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 63 | } |
| 64 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 65 | static struct counter_comp interrupt_cnt_ext[] = { |
| 66 | COUNTER_COMP_ENABLE(interrupt_cnt_enable_read, |
| 67 | interrupt_cnt_enable_write), |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
William Breathitt Gray | bc84957 | 2021-06-10 10:36:42 +0900 | [diff] [blame] | 70 | static const enum counter_synapse_action interrupt_cnt_synapse_actions[] = { |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 71 | COUNTER_SYNAPSE_ACTION_RISING_EDGE, |
| 72 | }; |
| 73 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 74 | static int interrupt_cnt_action_read(struct counter_device *counter, |
| 75 | struct counter_count *count, |
| 76 | struct counter_synapse *synapse, |
| 77 | enum counter_synapse_action *action) |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 78 | { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 79 | *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int interrupt_cnt_read(struct counter_device *counter, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 85 | struct counter_count *count, u64 *val) |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 86 | { |
Uwe Kleine-König | 63f0e2b | 2021-12-30 16:02:43 +0100 | [diff] [blame] | 87 | struct interrupt_cnt_priv *priv = counter_priv(counter); |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 88 | |
| 89 | *val = atomic_read(&priv->count); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int interrupt_cnt_write(struct counter_device *counter, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 95 | struct counter_count *count, const u64 val) |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 96 | { |
Uwe Kleine-König | 63f0e2b | 2021-12-30 16:02:43 +0100 | [diff] [blame] | 97 | struct interrupt_cnt_priv *priv = counter_priv(counter); |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 98 | |
William Breathitt Gray | e2ff319 | 2021-08-03 21:06:13 +0900 | [diff] [blame] | 99 | if (val != (typeof(priv->count.counter))val) |
| 100 | return -ERANGE; |
| 101 | |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 102 | atomic_set(&priv->count, val); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
William Breathitt Gray | 394a015 | 2021-08-03 21:06:15 +0900 | [diff] [blame] | 107 | static const enum counter_function interrupt_cnt_functions[] = { |
| 108 | COUNTER_FUNCTION_INCREASE, |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 111 | static int interrupt_cnt_function_read(struct counter_device *counter, |
| 112 | struct counter_count *count, |
| 113 | enum counter_function *function) |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 114 | { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 115 | *function = COUNTER_FUNCTION_INCREASE; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static int interrupt_cnt_signal_read(struct counter_device *counter, |
| 121 | struct counter_signal *signal, |
William Breathitt Gray | 493b938 | 2021-08-03 21:06:14 +0900 | [diff] [blame] | 122 | enum counter_signal_level *level) |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 123 | { |
Uwe Kleine-König | 63f0e2b | 2021-12-30 16:02:43 +0100 | [diff] [blame] | 124 | struct interrupt_cnt_priv *priv = counter_priv(counter); |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 125 | int ret; |
| 126 | |
| 127 | if (!priv->gpio) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | ret = gpiod_get_value(priv->gpio); |
| 131 | if (ret < 0) |
| 132 | return ret; |
| 133 | |
William Breathitt Gray | 493b938 | 2021-08-03 21:06:14 +0900 | [diff] [blame] | 134 | *level = ret ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static const struct counter_ops interrupt_cnt_ops = { |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 140 | .action_read = interrupt_cnt_action_read, |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 141 | .count_read = interrupt_cnt_read, |
| 142 | .count_write = interrupt_cnt_write, |
William Breathitt Gray | aaec1a0 | 2021-08-27 12:47:47 +0900 | [diff] [blame] | 143 | .function_read = interrupt_cnt_function_read, |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 144 | .signal_read = interrupt_cnt_signal_read, |
| 145 | }; |
| 146 | |
| 147 | static int interrupt_cnt_probe(struct platform_device *pdev) |
| 148 | { |
| 149 | struct device *dev = &pdev->dev; |
Uwe Kleine-König | aefc7e1 | 2021-12-30 16:02:53 +0100 | [diff] [blame] | 150 | struct counter_device *counter; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 151 | struct interrupt_cnt_priv *priv; |
| 152 | int ret; |
| 153 | |
Uwe Kleine-König | aefc7e1 | 2021-12-30 16:02:53 +0100 | [diff] [blame] | 154 | counter = devm_counter_alloc(dev, sizeof(*priv)); |
| 155 | if (!counter) |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 156 | return -ENOMEM; |
Uwe Kleine-König | aefc7e1 | 2021-12-30 16:02:53 +0100 | [diff] [blame] | 157 | priv = counter_priv(counter); |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 158 | |
| 159 | priv->irq = platform_get_irq_optional(pdev, 0); |
| 160 | if (priv->irq == -ENXIO) |
| 161 | priv->irq = 0; |
| 162 | else if (priv->irq < 0) |
| 163 | return dev_err_probe(dev, priv->irq, "failed to get IRQ\n"); |
| 164 | |
| 165 | priv->gpio = devm_gpiod_get_optional(dev, NULL, GPIOD_IN); |
| 166 | if (IS_ERR(priv->gpio)) |
| 167 | return dev_err_probe(dev, PTR_ERR(priv->gpio), "failed to get GPIO\n"); |
| 168 | |
| 169 | if (!priv->irq && !priv->gpio) { |
| 170 | dev_err(dev, "IRQ and GPIO are not found. At least one source should be provided\n"); |
| 171 | return -ENODEV; |
| 172 | } |
| 173 | |
| 174 | if (!priv->irq) { |
| 175 | int irq = gpiod_to_irq(priv->gpio); |
| 176 | |
| 177 | if (irq < 0) |
| 178 | return dev_err_probe(dev, irq, "failed to get IRQ from GPIO\n"); |
| 179 | |
| 180 | priv->irq = irq; |
| 181 | } |
| 182 | |
| 183 | priv->signals.name = devm_kasprintf(dev, GFP_KERNEL, "IRQ %d", |
| 184 | priv->irq); |
| 185 | if (!priv->signals.name) |
| 186 | return -ENOMEM; |
| 187 | |
Uwe Kleine-König | aefc7e1 | 2021-12-30 16:02:53 +0100 | [diff] [blame] | 188 | counter->signals = &priv->signals; |
| 189 | counter->num_signals = 1; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 190 | |
William Breathitt Gray | bc84957 | 2021-06-10 10:36:42 +0900 | [diff] [blame] | 191 | priv->synapses.actions_list = interrupt_cnt_synapse_actions; |
| 192 | priv->synapses.num_actions = ARRAY_SIZE(interrupt_cnt_synapse_actions); |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 193 | priv->synapses.signal = &priv->signals; |
| 194 | |
| 195 | priv->cnts.name = "Channel 0 Count"; |
| 196 | priv->cnts.functions_list = interrupt_cnt_functions; |
| 197 | priv->cnts.num_functions = ARRAY_SIZE(interrupt_cnt_functions); |
| 198 | priv->cnts.synapses = &priv->synapses; |
| 199 | priv->cnts.num_synapses = 1; |
| 200 | priv->cnts.ext = interrupt_cnt_ext; |
| 201 | priv->cnts.num_ext = ARRAY_SIZE(interrupt_cnt_ext); |
| 202 | |
Uwe Kleine-König | aefc7e1 | 2021-12-30 16:02:53 +0100 | [diff] [blame] | 203 | counter->name = dev_name(dev); |
| 204 | counter->parent = dev; |
| 205 | counter->ops = &interrupt_cnt_ops; |
| 206 | counter->counts = &priv->cnts; |
| 207 | counter->num_counts = 1; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 208 | |
| 209 | irq_set_status_flags(priv->irq, IRQ_NOAUTOEN); |
| 210 | ret = devm_request_irq(dev, priv->irq, interrupt_cnt_isr, |
| 211 | IRQF_TRIGGER_RISING | IRQF_NO_THREAD, |
| 212 | dev_name(dev), priv); |
| 213 | if (ret) |
| 214 | return ret; |
| 215 | |
Uwe Kleine-König | aefc7e1 | 2021-12-30 16:02:53 +0100 | [diff] [blame] | 216 | ret = devm_counter_add(dev, counter); |
| 217 | if (ret < 0) |
| 218 | return dev_err_probe(dev, ret, "Failed to add counter\n"); |
| 219 | |
| 220 | return 0; |
Oleksij Rempel | a55ebd4 | 2021-03-01 09:04:01 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static const struct of_device_id interrupt_cnt_of_match[] = { |
| 224 | { .compatible = "interrupt-counter", }, |
| 225 | {} |
| 226 | }; |
| 227 | MODULE_DEVICE_TABLE(of, interrupt_cnt_of_match); |
| 228 | |
| 229 | static struct platform_driver interrupt_cnt_driver = { |
| 230 | .probe = interrupt_cnt_probe, |
| 231 | .driver = { |
| 232 | .name = INTERRUPT_CNT_NAME, |
| 233 | .of_match_table = interrupt_cnt_of_match, |
| 234 | }, |
| 235 | }; |
| 236 | module_platform_driver(interrupt_cnt_driver); |
| 237 | |
| 238 | MODULE_ALIAS("platform:interrupt-counter"); |
| 239 | MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>"); |
| 240 | MODULE_DESCRIPTION("Interrupt counter driver"); |
| 241 | MODULE_LICENSE("GPL v2"); |