]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpio/gpio-qcom-smp2p.c
gpio: qcom-smp2p: Fix compile issues
[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 #if defined(CONFIG_ARM64) || defined(CONFIG_ARM)
295         set_irq_flags(irq, IRQF_VALID);
296 #else
297         irq_set_noprobe(virq);
298 #endif
299
300         return 0;
301 }
302
303 static const struct irq_domain_ops smp2p_irq_ops = {
304         .map = smp2p_irq_map,
305         .xlate = irq_domain_xlate_twocell,
306 };
307
308 static int smp2p_gpio_direction_output(struct gpio_chip *chip,
309                                        unsigned offset,
310                                        int value)
311 {
312         struct smp2p_entry *entry = container_of(chip, struct smp2p_entry, chip);
313
314         if (value)
315                 *entry->value |= BIT(offset);
316         else
317                 *entry->value &= ~BIT(offset);
318
319         qcom_smp2p_kick(entry->smp2p);
320
321         return 0;
322 }
323
324 static void smp2p_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
325 {
326         smp2p_gpio_direction_output(chip, offset, value);
327 }
328
329 static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
330                                     struct smp2p_entry *entry,
331                                     struct device_node *node)
332 {
333         entry->domain = irq_domain_add_linear(node, 32, &smp2p_irq_ops, entry);
334         if (!entry->domain) {
335                 dev_err(smp2p->dev, "failed to add irq_domain\n");
336                 return -ENOMEM;
337         }
338
339         return 0;
340 }
341
342 static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
343                                      struct smp2p_entry *entry,
344                                      struct device_node *node)
345 {
346         struct smp2p_smem_item *out = smp2p->out;
347         struct gpio_chip *chip;
348         int ret;
349
350         /* Allocate an entry from the smem item */
351         memcpy_toio(out->entries[out->valid_entries].name, entry->name, SMP2P_MAX_ENTRY_NAME);
352         out->valid_entries++;
353
354         /* Make the logical entry reference the physical value */
355         entry->value = &out->entries[out->valid_entries].value;
356
357         chip = &entry->chip;
358         chip->base = -1;
359         chip->ngpio = 32;
360         chip->label = entry->name;
361         chip->dev = smp2p->dev;
362         chip->owner = THIS_MODULE;
363         chip->of_node = node;
364
365         chip->set = smp2p_gpio_set;
366         chip->direction_output = smp2p_gpio_direction_output;
367
368         ret = gpiochip_add(chip);
369         if (ret)
370                 dev_err(smp2p->dev, "failed register gpiochip\n");
371
372         return 0;
373 }
374
375 static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
376 {
377         struct smp2p_smem_item *out;
378         unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
379         unsigned pid = smp2p->remote_pid;
380         int ret;
381
382         ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
383         if (ret < 0 && ret != -EEXIST) {
384                 if (ret != -EPROBE_DEFER)
385                         dev_err(smp2p->dev,
386                                 "unable to allocate local smp2p item\n");
387                 return ret;
388         }
389
390         ret = qcom_smem_get(pid, smem_id, (void **)&out, NULL);
391         if (ret < 0) {
392                 dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
393                 return ret;
394         }
395
396         memset_io(out, 0, sizeof(*out));
397         out->magic = SMP2P_MAGIC;
398         out->local_pid = smp2p->local_pid;
399         out->remote_pid = smp2p->remote_pid;
400         out->total_entries = SMP2P_MAX_ENTRY;
401         out->valid_entries = 0;
402
403         /*
404          * Make sure the rest of the header is written before we validate the
405          * item by writing a valid version number.
406          */
407         wmb();
408         out->version = 1;
409
410         qcom_smp2p_kick(smp2p);
411
412         smp2p->out = out;
413
414         return 0;
415 }
416
417 static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
418 {
419         struct device_node *syscon;
420         struct device *dev = smp2p->dev;
421         const char *key;
422         int ret;
423
424         syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
425         if (!syscon) {
426                 dev_err(dev, "no qcom,ipc node\n");
427                 return -ENODEV;
428         }
429
430         smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
431         if (IS_ERR(smp2p->ipc_regmap))
432                 return PTR_ERR(smp2p->ipc_regmap);
433
434         key = "qcom,ipc";
435         ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
436         if (ret < 0) {
437                 dev_err(dev, "no offset in %s\n", key);
438                 return -EINVAL;
439         }
440
441         ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
442         if (ret < 0) {
443                 dev_err(dev, "no bit in %s\n", key);
444                 return -EINVAL;
445         }
446
447         return 0;
448 }
449
450 static int qcom_smp2p_probe(struct platform_device *pdev)
451 {
452         struct smp2p_entry *entry;
453         struct device_node *node;
454         struct qcom_smp2p *smp2p;
455         const char *key;
456         int irq;
457         int ret;
458
459         smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
460         if (!smp2p)
461                 return -ENOMEM;
462
463         smp2p->dev = &pdev->dev;
464         INIT_LIST_HEAD(&smp2p->inbound);
465         INIT_LIST_HEAD(&smp2p->outbound);
466
467         platform_set_drvdata(pdev, smp2p);
468
469         ret = smp2p_parse_ipc(smp2p);
470         if (ret)
471                 return ret;
472
473         key = "qcom,smem";
474         ret = of_property_read_u32_array(pdev->dev.of_node, key,
475                                          smp2p->smem_items, 2);
476         if (ret)
477                 return ret;
478
479         key = "qcom,local-pid";
480         ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
481         if (ret < 0) {
482                 dev_err(&pdev->dev, "failed to read %s\n", key);
483                 return -EINVAL;
484         }
485
486         key = "qcom,remote-pid";
487         ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
488         if (ret < 0) {
489                 dev_err(&pdev->dev, "failed to read %s\n", key);
490                 return -EINVAL;
491         }
492
493         irq = platform_get_irq(pdev, 0);
494         if (irq < 0) {
495                 dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
496                 return irq;
497         }
498
499         ret = qcom_smp2p_alloc_outbound_item(smp2p);
500         if (ret < 0)
501                 return ret;
502
503         for_each_available_child_of_node(pdev->dev.of_node, node) {
504                 entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
505                 if (!entry) {
506                         ret = -ENOMEM;
507                         goto unwind_interfaces;
508                 }
509
510                 entry->smp2p = smp2p;
511
512                 ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
513                 if (ret < 0)
514                         goto unwind_interfaces;
515
516                 if (of_property_read_bool(node, "qcom,inbound")) {
517                         ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
518                         if (ret < 0)
519                                 goto unwind_interfaces;
520
521                         list_add(&entry->node, &smp2p->inbound);
522                 } else if (of_property_read_bool(node, "qcom,outbound")) {
523                         ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
524                         if (ret < 0)
525                                 goto unwind_interfaces;
526
527                         list_add(&entry->node, &smp2p->outbound);
528                 } else {
529                         dev_err(&pdev->dev, "neither inbound nor outbound\n");
530                         ret = -EINVAL;
531                         goto unwind_interfaces;
532                 }
533         }
534
535         /* Kick the outgoing edge after allocating entries */
536         qcom_smp2p_kick(smp2p);
537
538         ret = devm_request_threaded_irq(&pdev->dev, irq,
539                                         NULL, qcom_smp2p_intr,
540                                         IRQF_ONESHOT,
541                                         "smp2p", (void *)smp2p);
542         if (ret) {
543                 dev_err(&pdev->dev, "failed to request interrupt\n");
544                 goto unwind_interfaces;
545         }
546
547
548         return 0;
549
550 unwind_interfaces:
551         list_for_each_entry(entry, &smp2p->inbound, node)
552                 irq_domain_remove(entry->domain);
553
554         list_for_each_entry(entry, &smp2p->outbound, node)
555                 gpiochip_remove(&entry->chip);
556
557         smp2p->out->valid_entries = 0;
558
559         return ret;
560 }
561
562 static int qcom_smp2p_remove(struct platform_device *pdev)
563 {
564         struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
565         struct smp2p_entry *entry;
566
567         list_for_each_entry(entry, &smp2p->inbound, node)
568                 irq_domain_remove(entry->domain);
569
570         list_for_each_entry(entry, &smp2p->outbound, node)
571                 gpiochip_remove(&entry->chip);
572
573         smp2p->out->valid_entries = 0;
574
575         return 0;
576 }
577
578 static const struct of_device_id qcom_smp2p_of_match[] = {
579         { .compatible = "qcom,smp2p" },
580         {}
581 };
582 MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
583
584 static struct platform_driver qcom_smp2p_driver = {
585         .probe = qcom_smp2p_probe,
586         .remove = qcom_smp2p_remove,
587         .driver  = {
588                 .name  = "qcom_smp2p",
589                 .of_match_table = qcom_smp2p_of_match,
590         },
591 };
592 module_platform_driver(qcom_smp2p_driver);
593
594 MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
595 MODULE_LICENSE("GPL v2");