2 * Copyright (c) 2015, Sony Mobile Communications AB.
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
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.
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.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/of_platform.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>
29 #include <linux/delay.h>
31 #include <linux/soc/qcom/smem.h>
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.
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.
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
50 #define SMP2P_MAX_ENTRY 16
51 #define SMP2P_MAX_ENTRY_NAME 16
53 #define SMP2P_FEATURE_SSR_ACK 0x1
55 #define SMP2P_MAGIC 0x504d5324
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
67 * @entries: individual communication entries
68 * @name: name of the entry
69 * @value: content of the entry
71 struct smp2p_smem_item {
82 u8 name[SMP2P_MAX_ENTRY_NAME];
84 } entries[SMP2P_MAX_ENTRY];
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
101 struct list_head node;
102 struct qcom_smp2p *smp2p;
108 struct irq_domain *domain;
109 DECLARE_BITMAP(irq_enabled, 32);
110 DECLARE_BITMAP(irq_rising, 32);
111 DECLARE_BITMAP(irq_falling, 32);
113 struct gpio_chip chip;
116 #define SMP2P_INBOUND 0
117 #define SMP2P_OUTBOUND 1
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
136 struct smp2p_smem_item *in;
137 struct smp2p_smem_item *out;
139 unsigned smem_items[SMP2P_OUTBOUND + 1];
141 unsigned valid_entries;
146 struct regmap *ipc_regmap;
150 struct list_head inbound;
151 struct list_head outbound;
154 static void qcom_smp2p_kick(struct qcom_smp2p *smp2p)
156 /* Make sure any updated data is written before the kick */
158 regmap_write(smp2p->ipc_regmap, smp2p->ipc_offset, BIT(smp2p->ipc_bit));
161 static irqreturn_t qcom_smp2p_intr(int irq, void *data)
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;
173 u8 tmp_name[SMP2P_MAX_ENTRY_NAME];
177 /* Acquire smem item, if not already found */
179 in = qcom_smem_get(pid, smem_id, &size);
182 "Unable to acquire remote smp2p item\n");
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;
200 smp2p->valid_entries = i;
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 */
210 status = val ^ entry->last_value;
211 entry->last_value = val;
213 /* No changes of this entry? */
217 for_each_set_bit(i, entry->irq_enabled, 32) {
218 if (!(status & 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);
227 if (test_bit(i, entry->irq_falling)) {
228 irq_pin = irq_find_mapping(entry->domain, i);
229 handle_nested_irq(irq_pin);
239 static void smp2p_mask_irq(struct irq_data *irqd)
241 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
242 irq_hw_number_t irq = irqd_to_hwirq(irqd);
244 clear_bit(irq, entry->irq_enabled);
247 static void smp2p_unmask_irq(struct irq_data *irqd)
249 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
250 irq_hw_number_t irq = irqd_to_hwirq(irqd);
252 set_bit(irq, entry->irq_enabled);
255 static int smp2p_set_irq_type(struct irq_data *irqd, unsigned int type)
257 struct smp2p_entry *entry = irq_data_get_irq_chip_data(irqd);
258 irq_hw_number_t irq = irqd_to_hwirq(irqd);
260 if (!(type & IRQ_TYPE_EDGE_BOTH))
263 if (type & IRQ_TYPE_EDGE_RISING)
264 set_bit(irq, entry->irq_rising);
266 clear_bit(irq, entry->irq_rising);
268 if (type & IRQ_TYPE_EDGE_FALLING)
269 set_bit(irq, entry->irq_falling);
271 clear_bit(irq, entry->irq_falling);
276 static struct irq_chip smp2p_irq_chip = {
278 .irq_mask = smp2p_mask_irq,
279 .irq_unmask = smp2p_unmask_irq,
280 .irq_set_type = smp2p_set_irq_type,
283 static int smp2p_irq_map(struct irq_domain *d,
287 struct smp2p_entry *entry = d->host_data;
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);
293 irq_set_noprobe(irq);
298 static const struct irq_domain_ops smp2p_irq_ops = {
299 .map = smp2p_irq_map,
300 .xlate = irq_domain_xlate_twocell,
303 static int smp2p_gpio_direction_output(struct gpio_chip *chip,
307 struct smp2p_entry *entry = container_of(chip, struct smp2p_entry, chip);
310 *entry->value |= BIT(offset);
312 *entry->value &= ~BIT(offset);
314 qcom_smp2p_kick(entry->smp2p);
319 static void smp2p_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
321 smp2p_gpio_direction_output(chip, offset, value);
324 static int qcom_smp2p_inbound_entry(struct qcom_smp2p *smp2p,
325 struct smp2p_entry *entry,
326 struct device_node *node)
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");
337 static int qcom_smp2p_outbound_entry(struct qcom_smp2p *smp2p,
338 struct smp2p_entry *entry,
339 struct device_node *node)
341 struct smp2p_smem_item *out = smp2p->out;
342 struct gpio_chip *chip;
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++;
349 /* Make the logical entry reference the physical value */
350 entry->value = &out->entries[out->valid_entries].value;
355 chip->label = entry->name;
356 chip->dev = smp2p->dev;
357 chip->owner = THIS_MODULE;
358 chip->of_node = node;
360 chip->set = smp2p_gpio_set;
361 chip->direction_output = smp2p_gpio_direction_output;
363 ret = gpiochip_add(chip);
365 dev_err(smp2p->dev, "failed register gpiochip\n");
370 static int qcom_smp2p_alloc_outbound_item(struct qcom_smp2p *smp2p)
372 struct smp2p_smem_item *out;
373 unsigned smem_id = smp2p->smem_items[SMP2P_OUTBOUND];
374 unsigned pid = smp2p->remote_pid;
377 ret = qcom_smem_alloc(pid, smem_id, sizeof(*out));
378 if (ret < 0 && ret != -EEXIST) {
379 if (ret != -EPROBE_DEFER)
381 "unable to allocate local smp2p item\n");
385 out = qcom_smem_get(pid, smem_id, NULL);
387 dev_err(smp2p->dev, "Unable to acquire local smp2p item\n");
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;
399 * Make sure the rest of the header is written before we validate the
400 * item by writing a valid version number.
405 qcom_smp2p_kick(smp2p);
412 static int smp2p_parse_ipc(struct qcom_smp2p *smp2p)
414 struct device_node *syscon;
415 struct device *dev = smp2p->dev;
419 syscon = of_parse_phandle(dev->of_node, "qcom,ipc", 0);
421 dev_err(dev, "no qcom,ipc node\n");
425 smp2p->ipc_regmap = syscon_node_to_regmap(syscon);
426 if (IS_ERR(smp2p->ipc_regmap))
427 return PTR_ERR(smp2p->ipc_regmap);
430 ret = of_property_read_u32_index(dev->of_node, key, 1, &smp2p->ipc_offset);
432 dev_err(dev, "no offset in %s\n", key);
436 ret = of_property_read_u32_index(dev->of_node, key, 2, &smp2p->ipc_bit);
438 dev_err(dev, "no bit in %s\n", key);
445 static int qcom_smp2p_probe(struct platform_device *pdev)
447 struct smp2p_entry *entry;
448 struct device_node *node;
449 struct qcom_smp2p *smp2p;
454 smp2p = devm_kzalloc(&pdev->dev, sizeof(*smp2p), GFP_KERNEL);
458 smp2p->dev = &pdev->dev;
459 INIT_LIST_HEAD(&smp2p->inbound);
460 INIT_LIST_HEAD(&smp2p->outbound);
462 platform_set_drvdata(pdev, smp2p);
464 ret = smp2p_parse_ipc(smp2p);
469 ret = of_property_read_u32_array(pdev->dev.of_node, key,
470 smp2p->smem_items, 2);
474 key = "qcom,local-pid";
475 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->local_pid);
477 dev_err(&pdev->dev, "failed to read %s\n", key);
481 key = "qcom,remote-pid";
482 ret = of_property_read_u32(pdev->dev.of_node, key, &smp2p->remote_pid);
484 dev_err(&pdev->dev, "failed to read %s\n", key);
488 irq = platform_get_irq(pdev, 0);
490 dev_err(&pdev->dev, "unable to acquire smp2p interrupt\n");
494 ret = qcom_smp2p_alloc_outbound_item(smp2p);
498 for_each_available_child_of_node(pdev->dev.of_node, node) {
499 entry = devm_kzalloc(&pdev->dev, sizeof(*entry), GFP_KERNEL);
502 goto unwind_interfaces;
505 entry->smp2p = smp2p;
507 ret = of_property_read_string(node, "qcom,entry-name", &entry->name);
509 goto unwind_interfaces;
511 if (of_property_read_bool(node, "qcom,inbound")) {
512 ret = qcom_smp2p_inbound_entry(smp2p, entry, node);
514 goto unwind_interfaces;
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);
520 goto unwind_interfaces;
522 list_add(&entry->node, &smp2p->outbound);
524 dev_err(&pdev->dev, "neither inbound nor outbound\n");
526 goto unwind_interfaces;
530 /* Kick the outgoing edge after allocating entries */
531 qcom_smp2p_kick(smp2p);
533 ret = devm_request_threaded_irq(&pdev->dev, irq,
534 NULL, qcom_smp2p_intr,
536 "smp2p", (void *)smp2p);
538 dev_err(&pdev->dev, "failed to request interrupt\n");
539 goto unwind_interfaces;
546 list_for_each_entry(entry, &smp2p->inbound, node)
547 irq_domain_remove(entry->domain);
549 list_for_each_entry(entry, &smp2p->outbound, node)
550 gpiochip_remove(&entry->chip);
552 smp2p->out->valid_entries = 0;
557 static int qcom_smp2p_remove(struct platform_device *pdev)
559 struct qcom_smp2p *smp2p = platform_get_drvdata(pdev);
560 struct smp2p_entry *entry;
562 list_for_each_entry(entry, &smp2p->inbound, node)
563 irq_domain_remove(entry->domain);
565 list_for_each_entry(entry, &smp2p->outbound, node)
566 gpiochip_remove(&entry->chip);
568 smp2p->out->valid_entries = 0;
573 static const struct of_device_id qcom_smp2p_of_match[] = {
574 { .compatible = "qcom,smp2p" },
577 MODULE_DEVICE_TABLE(of, qcom_smp2p_of_match);
579 static struct platform_driver qcom_smp2p_driver = {
580 .probe = qcom_smp2p_probe,
581 .remove = qcom_smp2p_remove,
583 .name = "qcom_smp2p",
584 .of_match_table = qcom_smp2p_of_match,
587 module_platform_driver(qcom_smp2p_driver);
589 MODULE_DESCRIPTION("Qualcomm Shared Memory Point to Point driver");
590 MODULE_LICENSE("GPL v2");