1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2015, Sony Mobile Communications AB
7 #include <linux/hwspinlock.h>
9 #include <linux/kernel.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
17 #include "hwspinlock_internal.h"
19 #define QCOM_MUTEX_APPS_PROC_ID 1
20 #define QCOM_MUTEX_NUM_LOCKS 32
22 static int qcom_hwspinlock_trylock(struct hwspinlock
*lock
)
24 struct regmap_field
*field
= lock
->priv
;
28 ret
= regmap_field_write(field
, QCOM_MUTEX_APPS_PROC_ID
);
32 ret
= regmap_field_read(field
, &lock_owner
);
36 return lock_owner
== QCOM_MUTEX_APPS_PROC_ID
;
39 static void qcom_hwspinlock_unlock(struct hwspinlock
*lock
)
41 struct regmap_field
*field
= lock
->priv
;
45 ret
= regmap_field_read(field
, &lock_owner
);
47 pr_err("%s: unable to query spinlock owner\n", __func__
);
51 if (lock_owner
!= QCOM_MUTEX_APPS_PROC_ID
) {
52 pr_err("%s: spinlock not owned by us (actual owner is %d)\n",
53 __func__
, lock_owner
);
56 ret
= regmap_field_write(field
, 0);
58 pr_err("%s: failed to unlock spinlock\n", __func__
);
61 static const struct hwspinlock_ops qcom_hwspinlock_ops
= {
62 .trylock
= qcom_hwspinlock_trylock
,
63 .unlock
= qcom_hwspinlock_unlock
,
66 static const struct of_device_id qcom_hwspinlock_of_match
[] = {
67 { .compatible
= "qcom,sfpb-mutex" },
68 { .compatible
= "qcom,tcsr-mutex" },
71 MODULE_DEVICE_TABLE(of
, qcom_hwspinlock_of_match
);
73 static int qcom_hwspinlock_probe(struct platform_device
*pdev
)
75 struct hwspinlock_device
*bank
;
76 struct device_node
*syscon
;
77 struct reg_field field
;
78 struct regmap
*regmap
;
85 syscon
= of_parse_phandle(pdev
->dev
.of_node
, "syscon", 0);
87 dev_err(&pdev
->dev
, "no syscon property\n");
91 regmap
= syscon_node_to_regmap(syscon
);
94 return PTR_ERR(regmap
);
96 ret
= of_property_read_u32_index(pdev
->dev
.of_node
, "syscon", 1, &base
);
98 dev_err(&pdev
->dev
, "no offset in syscon\n");
102 ret
= of_property_read_u32_index(pdev
->dev
.of_node
, "syscon", 2, &stride
);
104 dev_err(&pdev
->dev
, "no stride syscon\n");
108 array_size
= QCOM_MUTEX_NUM_LOCKS
* sizeof(struct hwspinlock
);
109 bank
= devm_kzalloc(&pdev
->dev
, sizeof(*bank
) + array_size
, GFP_KERNEL
);
113 platform_set_drvdata(pdev
, bank
);
115 for (i
= 0; i
< QCOM_MUTEX_NUM_LOCKS
; i
++) {
116 field
.reg
= base
+ i
* stride
;
120 bank
->lock
[i
].priv
= devm_regmap_field_alloc(&pdev
->dev
,
124 return devm_hwspin_lock_register(&pdev
->dev
, bank
, &qcom_hwspinlock_ops
,
125 0, QCOM_MUTEX_NUM_LOCKS
);
128 static struct platform_driver qcom_hwspinlock_driver
= {
129 .probe
= qcom_hwspinlock_probe
,
131 .name
= "qcom_hwspinlock",
132 .of_match_table
= qcom_hwspinlock_of_match
,
136 static int __init
qcom_hwspinlock_init(void)
138 return platform_driver_register(&qcom_hwspinlock_driver
);
140 /* board init code might need to reserve hwspinlocks for predefined purposes */
141 postcore_initcall(qcom_hwspinlock_init
);
143 static void __exit
qcom_hwspinlock_exit(void)
145 platform_driver_unregister(&qcom_hwspinlock_driver
);
147 module_exit(qcom_hwspinlock_exit
);
149 MODULE_LICENSE("GPL v2");
150 MODULE_DESCRIPTION("Hardware spinlock driver for Qualcomm SoCs");