]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpio/gpio-qcom-smp2p.c
gpio:smp2p:qcom: kill set_irq_flags and use genirq
[karo-tx-linux.git] / drivers / gpio / gpio-qcom-smp2p.c
1 /*
2  * Copyright (c) 2015, Sony Mobile Communications AB.
3  * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/of_platform.h>
18 #include <linux/io.h>
19 #include <linux/interrupt.h>
20 #include <linux/memblock.h>
21 #include <linux/slab.h>
22 #include <linux/of_address.h>
23 #include <linux/of_irq.h>
24 #include <linux/regmap.h>
25 #include <linux/list.h>
26 #include <linux/gpio.h>
27 #include <linux/mfd/syscon.h>
28
29 #include <linux/delay.h>
30
31 #include <linux/soc/qcom/smem.h>
32
33 /*
34  * The Shared Memory Point to Point (SMP2P) protocol facilitates communication
35  * of a single 32-bit value between two processors.  Each value has a single
36  * writer (the local side) and a single reader (the remote side). Values are
37  * uniquely identified in the system by the directed edge (local processor ID
38  * to remote processor ID) and a string identifier.
39  *
40  * Each processor is responsible for creating the outgoing SMEM items and each
41  * item is writable by the local processor and readable by the remote
42  * processor.  By using two separate SMEM items that are single-reader and
43  * single-writer, SMP2P does not require any remote locking mechanisms.
44  *
45  * The driver uses the Linux GPIO and interrupt framework to expose a virtual
46  * GPIO for each outbound entry and a virtual interrupt controller for each
47  * inbound entry.
48  */
49
50 #define SMP2P_MAX_ENTRY 16
51 #define SMP2P_MAX_ENTRY_NAME 16
52
53 #define SMP2P_FEATURE_SSR_ACK 0x1
54
55 #define SMP2P_MAGIC 0x504d5324
56
57 /**
58  * struct smp2p_smem_item - in memory communication structure
59  * @magic:              magic number
60  * @version:            version - must be 1
61  * @features:           features flag - currently unused
62  * @local_pid:          processor id of sending end
63  * @remote_pid:         processor id of receiving end
64  * @total_entries:      number of entries - always SMP2P_MAX_ENTRY
65  * @valid_entries:      number of allocated entries
66  * @flags:
67  * @entries:            individual communication entries
68  *     @name:           name of the entry
69  *     @value:          content of the entry
70  */
71 struct smp2p_smem_item {
72         u32 magic;
73         u8 version;
74         unsigned features:24;
75         u16 local_pid;
76         u16 remote_pid;
77         u16 total_entries;
78         u16 valid_entries;
79         u32 flags;
80
81         struct {
82                 u8 name[SMP2P_MAX_ENTRY_NAME];
83                 u32 value;
84         } entries[SMP2P_MAX_ENTRY];
85 } __packed;
86
87 /**
88  * struct smp2p_entry - driver context matching one entry
89  * @node:       list entry to keep track of allocated entries
90  * @smp2p:      reference to the device driver context
91  * @name:       name of the entry, to match against smp2p_smem_item
92  * @value:      pointer to smp2p_smem_item entry value
93  * @last_value: last handled value
94  * @domain:     irq_domain for inbound entries
95  * @irq_enabled:bitmap to track enabled irq bits
96  * @irq_rising: bitmap to mark irq bits for rising detection
97  * @irq_falling:bitmap to mark irq bits for falling detection
98  * @chip:       gpio_chip for outbound entries
99  */
100 struct smp2p_entry {
101         struct list_head node;
102         struct qcom_smp2p *smp2p;
103
104         const char *name;
105         u32 *value;
106         u32 last_value;
107
108         struct irq_domain *domain;
109         DECLARE_BITMAP(irq_enabled, 32);
110         DECLARE_BITMAP(irq_rising, 32);
111         DECLARE_BITMAP(irq_falling, 32);
112
113         struct gpio_chip chip;
114 };
115
116 #define SMP2P_INBOUND   0
117 #define SMP2P_OUTBOUND  1
118
119 /**
120  * struct qcom_smp2p - device driver context
121  * @dev:        device driver handle
122  * @in:         pointer to the inbound smem item
123  * @smem_items: ids of the two smem items
124  * @valid_entries: already scanned inbound entries
125  * @local_pid:  processor id of the inbound edge
126  * @remote_pid: processor id of the outbound edge
127  * @ipc_regmap: regmap for the outbound ipc
128  * @ipc_offset: offset within the regmap
129  * @ipc_bit:    bit in regmap@offset to kick to signal remote processor
130  * @inbound:    list of inbound entries
131  * @outbound:   list of outbound entries
132  */
133 struct qcom_smp2p {
134         struct device *dev;
135
136         struct smp2p_smem_item *in;
137         struct smp2p_smem_item *out;
138
139         unsigned smem_items[SMP2P_OUTBOUND + 1];
140
141         unsigned valid_entries;
142
143         unsigned local_pid;
144         unsigned remote_pid;
145
146         struct regmap *ipc_regmap;
147         int ipc_offset;
148         int ipc_bit;
149
150         struct list_head inbound;
151         struct list_head outbound;
152 };
153
154 static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
155 {
156         /* Make sure any updated data is written before the kick */
157         wmb();
158         regmap_write(smp2p->ipc_regmap, smp2p->ipc_offset, BIT(smp2p->ipc_bit));
159 }
160
161 static irqreturn_t qcom_smp2p_intr(int irq, void *data)
162 {
163         struct smp2p_smem_item *in;
164         struct smp2p_entry *entry;
165         struct qcom_smp2p *smp2p = data;
166         unsigned smem_id = smp2p->smem_items[SMP2P_INBOUND];
167         unsigned pid = smp2p->remote_pid;
168         size_t size;
169         int irq_pin;
170         u32 status;
171         u32 val;
172         int ret;
173         int i;
174         u8 tmp_name[SMP2P_MAX_ENTRY_NAME];
175
176         in = smp2p->in;
177
178         /* Acquire smem item, if not already found */
179         if (!in) {
180                 ret = qcom_smem_get(pid, smem_id, (void **)&in, &size);
181                 if (ret < 0) {
182                         dev_err(smp2p->dev,
183                                         "Unable to acquire remote smp2p item\n");
184                         return IRQ_HANDLED;
185                 }
186
187                 smp2p->in = in;
188         }
189
190         /* Match newly created entries */
191         for (i = smp2p->valid_entries; i < in->valid_entries; i++) {
192                 list_for_each_entry(entry, &smp2p->inbound, node) {
193                         memcpy_fromio(tmp_name, in->entries[i].name,
194                                         SMP2P_MAX_ENTRY_NAME);
195                         if (!strcmp(tmp_name, entry->name)) {
196                                 entry->value = &in->entries[i].value;
197                                 break;
198                         }
199                 }
200         }
201         smp2p->valid_entries = i;
202
203         /* Fire interrupts based on any value changes */
204         list_for_each_entry(entry, &smp2p->inbound, node) {
205                 /* Ignore entries not yet allocated by the remote side */
206                 if (!entry->value)
207                         continue;
208
209                 val = *entry->value;
210
211                 status = val ^ entry->last_value;
212                 entry->last_value = val;
213
214                 /* No changes of this entry? */
215                 if (!status)
216                         continue;
217
218                 for_each_set_bit(i, entry->irq_enabled, 32) {
219                         if (!(status & BIT(i)))
220                                 continue;
221
222                         if (val & BIT(i)) {
223                                 if (test_bit(i, entry->irq_rising)) {
224                                         irq_pin = irq_find_mapping(entry->domain, i);
225                                         handle_nested_irq(irq_pin);
226                                 }
227                         } else {
228                                 if (test_bit(i, entry->irq_falling)) {
229                                         irq_pin = irq_find_mapping(entry->domain, i);
230                                         handle_nested_irq(irq_pin);
231                                 }
232                         }
233
234                 }
235         }
236
237         return IRQ_HANDLED;
238 }
239
240 static void smp2p_mask_irq(struct irq_data *irqd)
241 {
242         struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
243         irq_hw_number_t irq = irqd_to_hwirq(irqd);
244
245         clear_bit(irq, entry->irq_enabled);
246 }
247
248 static void smp2p_unmask_irq(struct irq_data *irqd)
249 {
250         struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
251         irq_hw_number_t irq = irqd_to_hwirq(irqd);
252
253         set_bit(irq, entry->irq_enabled);
254 }
255
256 static int smp2p_set_irq_type(struct irq_data *irqd, unsigned int type)
257 {
258         struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
259         irq_hw_number_t irq = irqd_to_hwirq(irqd);
260
261         if (!(type & IRQ_TYPE_EDGE_BOTH))
262                 return -EINVAL;
263
264         if (type & IRQ_TYPE_EDGE_RISING)
265                 set_bit(irq, entry->irq_rising);
266         else
267                 clear_bit(irq, entry->irq_rising);
268
269         if (type & IRQ_TYPE_EDGE_FALLING)
270                 set_bit(irq, entry->irq_falling);
271         else
272                 clear_bit(irq, entry->irq_falling);
273
274         return 0;
275 }
276
277 static struct irq_chip smp2p_irq_chip = {
278         .name           = "smp2p",
279         .irq_mask       = smp2p_mask_irq,
280         .irq_unmask     = smp2p_unmask_irq,
281         .irq_set_type   = smp2p_set_irq_type,
282 };
283
284 static int smp2p_irq_map(struct irq_domain *d,
285                          unsigned int irq,
286                          irq_hw_number_t hw)
287 {
288         struct smp2p_entry *entry = d->host_data;
289
290         irq_set_chip_and_handler(irq, &smp2p_irq_chip, handle_level_irq);
291         irq_set_chip_data(irq, entry);
292         irq_set_nested_thread(irq, 1);
293
294         irq_set_noprobe(irq);
295
296         return 0;
297 }
298
299 static const struct irq_domain_ops smp2p_irq_ops = {
300         .map = smp2p_irq_map,
301         .xlate = irq_domain_xlate_twocell,
302 };
303
304 static int smp2p_gpio_direction_output(struct gpio_chip *chip,
305                                        unsigned offset,
306                                        int value)
307 {
308         struct smp2p_entry *entry = container_of(chip, struct smp2p_entry, chip);
309
310         if (value)
311                 *entry->value |= BIT(offset);
312         else
313                 *entry->value &= ~BIT(offset);
314
315         qcom_smp2p_kick(entry->smp2p);
316
317         return 0;
318 }
319
320 static void smp2p_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
321 {
322         smp2p_gpio_direction_output(chip, offset, value);
323 }
324
325 static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
326                                     struct smp2p_entry *entry,
327                                     struct device_node *node)
328 {
329         entry->domain = irq_domain_add_linear(node, 32, &smp2p_irq_ops, entry);
330         if (!entry->domain) {
331                 dev_err(smp2p->dev, "failed to add irq_domain\n");
332                 return -ENOMEM;
333         }
334
335         return 0;
336 }
337
338 static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
339                                      struct smp2p_entry *entry,
340                                      struct device_node *node)
341 {
342         struct smp2p_smem_item *out = smp2p->out;
343         struct gpio_chip *chip;
344         int ret;
345
346         /* Allocate an entry from the smem item */
347         memcpy_toio(out->entries[out->valid_entries].name, entry->name, SMP2P_MAX_ENTRY_NAME);
348         out->valid_entries++;
349
350         /* Make the logical entry reference the physical value */
351         entry->value = &out->entries[out->valid_entries].value;
352
353         chip = &entry->chip;
354         chip->base = -1;
355         chip->ngpio = 32;
356         chip->label = entry->name;
357         chip->dev = smp2p->dev;
358         chip->owner = THIS_MODULE;
359         chip->of_node = node;
360
361         chip->set = smp2p_gpio_set;
362         chip->direction_output = smp2p_gpio_direction_output;
363
364         ret = gpiochip_add(chip);
365         if (ret)
366                 dev_err(smp2p->dev, "failed register gpiochip\n");
367
368         return 0;
369 }
370
371 static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
372 {
373         struct smp2p_smem_item *out;
374         unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
375         unsigned pid = smp2p->remote_pid;
376         int ret;
377
378         ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
379         if (ret < 0 && ret != -EEXIST) {
380                 if (ret != -EPROBE_DEFER)
381                         dev_err(smp2p->dev,
382                                 "unable to allocate local smp2p item\n");
383                 return ret;
384         }
385
386         ret = qcom_smem_get(pid, smem_id, (void **)&out, NULL);
387         if (ret < 0) {
388                 dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
389                 return ret;
390         }
391
392         memset_io(out, 0, sizeof(*out));
393         out->magic = SMP2P_MAGIC;
394         out->local_pid = smp2p->local_pid;
395         out->remote_pid = smp2p->remote_pid;
396         out->total_entries = SMP2P_MAX_ENTRY;
397         out->valid_entries = 0;
398
399         /*
400          * Make sure the rest of the header is written before we validate the
401          * item by writing a valid version number.
402          */
403         wmb();
404         out->version = 1;
405
406         qcom_smp2p_kick(smp2p);
407
408         smp2p->out = out;
409
410         return 0;
411 }
412
413 static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
414 {
415         struct device_node *syscon;
416         struct device *dev = smp2p->dev;
417         const char *key;
418         int ret;
419
420         syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
421         if (!syscon) {
422                 dev_err(dev, "no qcom,ipc node\n");
423                 return -ENODEV;
424         }
425
426         smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
427         if (IS_ERR(smp2p->ipc_regmap))
428                 return PTR_ERR(smp2p->ipc_regmap);
429
430         key = "qcom,ipc";
431         ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
432         if (ret < 0) {
433                 dev_err(dev, "no offset in %s\n", key);
434                 return -EINVAL;
435         }
436
437         ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
438         if (ret < 0) {
439                 dev_err(dev, "no bit in %s\n", key);
440                 return -EINVAL;
441         }
442
443         return 0;
444 }
445
446 static int qcom_smp2p_probe(struct platform_device *pdev)
447 {
448         struct smp2p_entry *entry;
449         struct device_node *node;
450         struct qcom_smp2p *smp2p;
451         const char *key;
452         int irq;
453         int ret;
454
455         smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
456         if (!smp2p)
457                 return -ENOMEM;
458
459         smp2p->dev = &pdev->dev;
460         INIT_LIST_HEAD(&smp2p->inbound);
461         INIT_LIST_HEAD(&smp2p->outbound);
462
463         platform_set_drvdata(pdev, smp2p);
464
465         ret = smp2p_parse_ipc(smp2p);
466         if (ret)
467                 return ret;
468
469         key = "qcom,smem";
470         ret = of_property_read_u32_array(pdev->dev.of_node, key,
471                                          smp2p->smem_items, 2);
472         if (ret)
473                 return ret;
474
475         key = "qcom,local-pid";
476         ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
477         if (ret < 0) {
478                 dev_err(&pdev->dev, "failed to read %s\n", key);
479                 return -EINVAL;
480         }
481
482         key = "qcom,remote-pid";
483         ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
484         if (ret < 0) {
485                 dev_err(&pdev->dev, "failed to read %s\n", key);
486                 return -EINVAL;
487         }
488
489         irq = platform_get_irq(pdev, 0);
490         if (irq < 0) {
491                 dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
492                 return irq;
493         }
494
495         ret = qcom_smp2p_alloc_outbound_item(smp2p);
496         if (ret < 0)
497                 return ret;
498
499         for_each_available_child_of_node(pdev->dev.of_node, node) {
500                 entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
501                 if (!entry) {
502                         ret = -ENOMEM;
503                         goto unwind_interfaces;
504                 }
505
506                 entry->smp2p = smp2p;
507
508                 ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
509                 if (ret < 0)
510                         goto unwind_interfaces;
511
512                 if (of_property_read_bool(node, "qcom,inbound")) {
513                         ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
514                         if (ret < 0)
515                                 goto unwind_interfaces;
516
517                         list_add(&entry->node, &smp2p->inbound);
518                 } else if (of_property_read_bool(node, "qcom,outbound")) {
519                         ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
520                         if (ret < 0)
521                                 goto unwind_interfaces;
522
523                         list_add(&entry->node, &smp2p->outbound);
524                 } else {
525                         dev_err(&pdev->dev, "neither inbound nor outbound\n");
526                         ret = -EINVAL;
527                         goto unwind_interfaces;
528                 }
529         }
530
531         /* Kick the outgoing edge after allocating entries */
532         qcom_smp2p_kick(smp2p);
533
534         ret = devm_request_threaded_irq(&pdev->dev, irq,
535                                         NULL, qcom_smp2p_intr,
536                                         IRQF_ONESHOT,
537                                         "smp2p", (void *)smp2p);
538         if (ret) {
539                 dev_err(&pdev->dev, "failed to request interrupt\n");
540                 goto unwind_interfaces;
541         }
542
543
544         return 0;
545
546 unwind_interfaces:
547         list_for_each_entry(entry, &smp2p->inbound, node)
548                 irq_domain_remove(entry->domain);
549
550         list_for_each_entry(entry, &smp2p->outbound, node)
551                 gpiochip_remove(&entry->chip);
552
553         smp2p->out->valid_entries = 0;
554
555         return ret;
556 }
557
558 static int qcom_smp2p_remove(struct platform_device *pdev)
559 {
560         struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
561         struct smp2p_entry *entry;
562
563         list_for_each_entry(entry, &smp2p->inbound, node)
564                 irq_domain_remove(entry->domain);
565
566         list_for_each_entry(entry, &smp2p->outbound, node)
567                 gpiochip_remove(&entry->chip);
568
569         smp2p->out->valid_entries = 0;
570
571         return 0;
572 }
573
574 static const struct of_device_id qcom_smp2p_of_match[] = {
575         { .compatible = "qcom,smp2p" },
576         {}
577 };
578 MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
579
580 static struct platform_driver qcom_smp2p_driver = {
581         .probe = qcom_smp2p_probe,
582         .remove = qcom_smp2p_remove,
583         .driver  = {
584                 .name  = "qcom_smp2p",
585                 .of_match_table = qcom_smp2p_of_match,
586         },
587 };
588 module_platform_driver(qcom_smp2p_driver);
589
590 MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
591 MODULE_LICENSE("GPL v2");