]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpio/gpio-qcom-smp2p.c
Merge branch 'tracking-qcomlt-mainline-rpm-smd-pil' into integration-linux-qcomlt
[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 i;
173         u8 tmp_name[SMP2P_MAX_ENTRY_NAME];
174
175         in = smp2p->in;
176
177         /* Acquire smem item, if not already found */
178         if (!in) {
179                 in = qcom_smem_get(pid, smem_id, &size);
180                 if (IS_ERR(in)) {
181                         dev_err(smp2p->dev,
182                                         "Unable to acquire remote smp2p item\n");
183                         return IRQ_HANDLED;
184                 }
185
186                 smp2p->in = in;
187         }
188
189         /* Match newly created entries */
190         for (i = smp2p->valid_entries; i < in->valid_entries; i++) {
191                 list_for_each_entry(entry, &smp2p->inbound, node) {
192                         memcpy_fromio(tmp_name, in->entries[i].name,
193                                         SMP2P_MAX_ENTRY_NAME);
194                         if (!strcmp(tmp_name, entry->name)) {
195                                 entry->value = &in->entries[i].value;
196                                 break;
197                         }
198                 }
199         }
200         smp2p->valid_entries = i;
201
202         /* Fire interrupts based on any value changes */
203         list_for_each_entry(entry, &smp2p->inbound, node) {
204                 /* Ignore entries not yet allocated by the remote side */
205                 if (!entry->value)
206                         continue;
207
208                 val = *entry->value;
209
210                 status = val ^ entry->last_value;
211                 entry->last_value = val;
212
213                 /* No changes of this entry? */
214                 if (!status)
215                         continue;
216
217                 for_each_set_bit(i, entry->irq_enabled, 32) {
218                         if (!(status & BIT(i)))
219                                 continue;
220
221                         if (val & BIT(i)) {
222                                 if (test_bit(i, entry->irq_rising)) {
223                                         irq_pin = irq_find_mapping(entry->domain, i);
224                                         handle_nested_irq(irq_pin);
225                                 }
226                         } else {
227                                 if (test_bit(i, entry->irq_falling)) {
228                                         irq_pin = irq_find_mapping(entry->domain, i);
229                                         handle_nested_irq(irq_pin);
230                                 }
231                         }
232
233                 }
234         }
235
236         return IRQ_HANDLED;
237 }
238
239 static void smp2p_mask_irq(struct irq_data *irqd)
240 {
241         struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
242         irq_hw_number_t irq = irqd_to_hwirq(irqd);
243
244         clear_bit(irq, entry->irq_enabled);
245 }
246
247 static void smp2p_unmask_irq(struct irq_data *irqd)
248 {
249         struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
250         irq_hw_number_t irq = irqd_to_hwirq(irqd);
251
252         set_bit(irq, entry->irq_enabled);
253 }
254
255 static int smp2p_set_irq_type(struct irq_data *irqd, unsigned int type)
256 {
257         struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
258         irq_hw_number_t irq = irqd_to_hwirq(irqd);
259
260         if (!(type & IRQ_TYPE_EDGE_BOTH))
261                 return -EINVAL;
262
263         if (type & IRQ_TYPE_EDGE_RISING)
264                 set_bit(irq, entry->irq_rising);
265         else
266                 clear_bit(irq, entry->irq_rising);
267
268         if (type & IRQ_TYPE_EDGE_FALLING)
269                 set_bit(irq, entry->irq_falling);
270         else
271                 clear_bit(irq, entry->irq_falling);
272
273         return 0;
274 }
275
276 static struct irq_chip smp2p_irq_chip = {
277         .name           = "smp2p",
278         .irq_mask       = smp2p_mask_irq,
279         .irq_unmask     = smp2p_unmask_irq,
280         .irq_set_type   = smp2p_set_irq_type,
281 };
282
283 static int smp2p_irq_map(struct irq_domain *d,
284                          unsigned int irq,
285                          irq_hw_number_t hw)
286 {
287         struct smp2p_entry *entry = d->host_data;
288
289         irq_set_chip_and_handler(irq, &smp2p_irq_chip, handle_level_irq);
290         irq_set_chip_data(irq, entry);
291         irq_set_nested_thread(irq, 1);
292
293         irq_set_noprobe(irq);
294
295         return 0;
296 }
297
298 static const struct irq_domain_ops smp2p_irq_ops = {
299         .map = smp2p_irq_map,
300         .xlate = irq_domain_xlate_twocell,
301 };
302
303 static int smp2p_gpio_direction_output(struct gpio_chip *chip,
304                                        unsigned offset,
305                                        int value)
306 {
307         struct smp2p_entry *entry = container_of(chip, struct smp2p_entry, chip);
308
309         if (value)
310                 *entry->value |= BIT(offset);
311         else
312                 *entry->value &= ~BIT(offset);
313
314         qcom_smp2p_kick(entry->smp2p);
315
316         return 0;
317 }
318
319 static void smp2p_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
320 {
321         smp2p_gpio_direction_output(chip, offset, value);
322 }
323
324 static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
325                                     struct smp2p_entry *entry,
326                                     struct device_node *node)
327 {
328         entry->domain = irq_domain_add_linear(node, 32, &smp2p_irq_ops, entry);
329         if (!entry->domain) {
330                 dev_err(smp2p->dev, "failed to add irq_domain\n");
331                 return -ENOMEM;
332         }
333
334         return 0;
335 }
336
337 static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
338                                      struct smp2p_entry *entry,
339                                      struct device_node *node)
340 {
341         struct smp2p_smem_item *out = smp2p->out;
342         struct gpio_chip *chip;
343         int ret;
344
345         /* Allocate an entry from the smem item */
346         memcpy_toio(out->entries[out->valid_entries].name, entry->name, SMP2P_MAX_ENTRY_NAME);
347         out->valid_entries++;
348
349         /* Make the logical entry reference the physical value */
350         entry->value = &out->entries[out->valid_entries].value;
351
352         chip = &entry->chip;
353         chip->base = -1;
354         chip->ngpio = 32;
355         chip->label = entry->name;
356         chip->dev = smp2p->dev;
357         chip->owner = THIS_MODULE;
358         chip->of_node = node;
359
360         chip->set = smp2p_gpio_set;
361         chip->direction_output = smp2p_gpio_direction_output;
362
363         ret = gpiochip_add(chip);
364         if (ret)
365                 dev_err(smp2p->dev, "failed register gpiochip\n");
366
367         return 0;
368 }
369
370 static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
371 {
372         struct smp2p_smem_item *out;
373         unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
374         unsigned pid = smp2p->remote_pid;
375         int ret;
376
377         ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
378         if (ret < 0 && ret != -EEXIST) {
379                 if (ret != -EPROBE_DEFER)
380                         dev_err(smp2p->dev,
381                                 "unable to allocate local smp2p item\n");
382                 return ret;
383         }
384
385         out = qcom_smem_get(pid, smem_id, NULL);
386         if (IS_ERR(out)) {
387                 dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
388                 return PTR_ERR(out);
389         }
390
391         memset_io(out, 0, sizeof(*out));
392         out->magic = SMP2P_MAGIC;
393         out->local_pid = smp2p->local_pid;
394         out->remote_pid = smp2p->remote_pid;
395         out->total_entries = SMP2P_MAX_ENTRY;
396         out->valid_entries = 0;
397
398         /*
399          * Make sure the rest of the header is written before we validate the
400          * item by writing a valid version number.
401          */
402         wmb();
403         out->version = 1;
404
405         qcom_smp2p_kick(smp2p);
406
407         smp2p->out = out;
408
409         return 0;
410 }
411
412 static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
413 {
414         struct device_node *syscon;
415         struct device *dev = smp2p->dev;
416         const char *key;
417         int ret;
418
419         syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
420         if (!syscon) {
421                 dev_err(dev, "no qcom,ipc node\n");
422                 return -ENODEV;
423         }
424
425         smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
426         if (IS_ERR(smp2p->ipc_regmap))
427                 return PTR_ERR(smp2p->ipc_regmap);
428
429         key = "qcom,ipc";
430         ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
431         if (ret < 0) {
432                 dev_err(dev, "no offset in %s\n", key);
433                 return -EINVAL;
434         }
435
436         ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
437         if (ret < 0) {
438                 dev_err(dev, "no bit in %s\n", key);
439                 return -EINVAL;
440         }
441
442         return 0;
443 }
444
445 static int qcom_smp2p_probe(struct platform_device *pdev)
446 {
447         struct smp2p_entry *entry;
448         struct device_node *node;
449         struct qcom_smp2p *smp2p;
450         const char *key;
451         int irq;
452         int ret;
453
454         smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
455         if (!smp2p)
456                 return -ENOMEM;
457
458         smp2p->dev = &pdev->dev;
459         INIT_LIST_HEAD(&smp2p->inbound);
460         INIT_LIST_HEAD(&smp2p->outbound);
461
462         platform_set_drvdata(pdev, smp2p);
463
464         ret = smp2p_parse_ipc(smp2p);
465         if (ret)
466                 return ret;
467
468         key = "qcom,smem";
469         ret = of_property_read_u32_array(pdev->dev.of_node, key,
470                                          smp2p->smem_items, 2);
471         if (ret)
472                 return ret;
473
474         key = "qcom,local-pid";
475         ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
476         if (ret < 0) {
477                 dev_err(&pdev->dev, "failed to read %s\n", key);
478                 return -EINVAL;
479         }
480
481         key = "qcom,remote-pid";
482         ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
483         if (ret < 0) {
484                 dev_err(&pdev->dev, "failed to read %s\n", key);
485                 return -EINVAL;
486         }
487
488         irq = platform_get_irq(pdev, 0);
489         if (irq < 0) {
490                 dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
491                 return irq;
492         }
493
494         ret = qcom_smp2p_alloc_outbound_item(smp2p);
495         if (ret < 0)
496                 return ret;
497
498         for_each_available_child_of_node(pdev->dev.of_node, node) {
499                 entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
500                 if (!entry) {
501                         ret = -ENOMEM;
502                         goto unwind_interfaces;
503                 }
504
505                 entry->smp2p = smp2p;
506
507                 ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
508                 if (ret < 0)
509                         goto unwind_interfaces;
510
511                 if (of_property_read_bool(node, "qcom,inbound")) {
512                         ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
513                         if (ret < 0)
514                                 goto unwind_interfaces;
515
516                         list_add(&entry->node, &smp2p->inbound);
517                 } else if (of_property_read_bool(node, "qcom,outbound")) {
518                         ret = qcom_smp2p_outbound_entry(smp2p, entry, node);
519                         if (ret < 0)
520                                 goto unwind_interfaces;
521
522                         list_add(&entry->node, &smp2p->outbound);
523                 } else {
524                         dev_err(&pdev->dev, "neither inbound nor outbound\n");
525                         ret = -EINVAL;
526                         goto unwind_interfaces;
527                 }
528         }
529
530         /* Kick the outgoing edge after allocating entries */
531         qcom_smp2p_kick(smp2p);
532
533         ret = devm_request_threaded_irq(&pdev->dev, irq,
534                                         NULL, qcom_smp2p_intr,
535                                         IRQF_ONESHOT,
536                                         "smp2p", (void *)smp2p);
537         if (ret) {
538                 dev_err(&pdev->dev, "failed to request interrupt\n");
539                 goto unwind_interfaces;
540         }
541
542
543         return 0;
544
545 unwind_interfaces:
546         list_for_each_entry(entry, &smp2p->inbound, node)
547                 irq_domain_remove(entry->domain);
548
549         list_for_each_entry(entry, &smp2p->outbound, node)
550                 gpiochip_remove(&entry->chip);
551
552         smp2p->out->valid_entries = 0;
553
554         return ret;
555 }
556
557 static int qcom_smp2p_remove(struct platform_device *pdev)
558 {
559         struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
560         struct smp2p_entry *entry;
561
562         list_for_each_entry(entry, &smp2p->inbound, node)
563                 irq_domain_remove(entry->domain);
564
565         list_for_each_entry(entry, &smp2p->outbound, node)
566                 gpiochip_remove(&entry->chip);
567
568         smp2p->out->valid_entries = 0;
569
570         return 0;
571 }
572
573 static const struct of_device_id qcom_smp2p_of_match[] = {
574         { .compatible = "qcom,smp2p" },
575         {}
576 };
577 MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
578
579 static struct platform_driver qcom_smp2p_driver = {
580         .probe = qcom_smp2p_probe,
581         .remove = qcom_smp2p_remove,
582         .driver  = {
583                 .name  = "qcom_smp2p",
584                 .of_match_table = qcom_smp2p_of_match,
585         },
586 };
587 module_platform_driver(qcom_smp2p_driver);
588
589 MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
590 MODULE_LICENSE("GPL v2");