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