USB: serial: iuu_phoenix: simplify init_termios
[linux/fpc-iii.git] / drivers / hwspinlock / sprd_hwspinlock.c
blobdc42bf51f3e6271d97844b3ba4c6a5228a5941ef
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Spreadtrum hardware spinlock driver
4 * Copyright (C) 2017 Spreadtrum - http://www.spreadtrum.com
5 */
7 #include <linux/bitops.h>
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/hwspinlock.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
21 #include "hwspinlock_internal.h"
23 /* hwspinlock registers definition */
24 #define HWSPINLOCK_RECCTRL 0x4
25 #define HWSPINLOCK_MASTERID(_X_) (0x80 + 0x4 * (_X_))
26 #define HWSPINLOCK_TOKEN(_X_) (0x800 + 0x4 * (_X_))
28 /* unlocked value */
29 #define HWSPINLOCK_NOTTAKEN 0x55aa10c5
30 /* bits definition of RECCTRL reg */
31 #define HWSPINLOCK_USER_BITS 0x1
33 /* hwspinlock number */
34 #define SPRD_HWLOCKS_NUM 32
36 struct sprd_hwspinlock_dev {
37 void __iomem *base;
38 struct clk *clk;
39 struct hwspinlock_device bank;
42 /* try to lock the hardware spinlock */
43 static int sprd_hwspinlock_trylock(struct hwspinlock *lock)
45 struct sprd_hwspinlock_dev *sprd_hwlock =
46 dev_get_drvdata(lock->bank->dev);
47 void __iomem *addr = lock->priv;
48 int user_id, lock_id;
50 if (!readl(addr))
51 return 1;
53 lock_id = hwlock_to_id(lock);
54 /* get the hardware spinlock master/user id */
55 user_id = readl(sprd_hwlock->base + HWSPINLOCK_MASTERID(lock_id));
56 dev_warn(sprd_hwlock->bank.dev,
57 "hwspinlock [%d] lock failed and master/user id = %d!\n",
58 lock_id, user_id);
59 return 0;
62 /* unlock the hardware spinlock */
63 static void sprd_hwspinlock_unlock(struct hwspinlock *lock)
65 void __iomem *lock_addr = lock->priv;
67 writel(HWSPINLOCK_NOTTAKEN, lock_addr);
70 /* The specs recommended below number as the retry delay time */
71 static void sprd_hwspinlock_relax(struct hwspinlock *lock)
73 ndelay(10);
76 static const struct hwspinlock_ops sprd_hwspinlock_ops = {
77 .trylock = sprd_hwspinlock_trylock,
78 .unlock = sprd_hwspinlock_unlock,
79 .relax = sprd_hwspinlock_relax,
82 static int sprd_hwspinlock_probe(struct platform_device *pdev)
84 struct sprd_hwspinlock_dev *sprd_hwlock;
85 struct hwspinlock *lock;
86 struct resource *res;
87 int i, ret;
89 if (!pdev->dev.of_node)
90 return -ENODEV;
92 sprd_hwlock = devm_kzalloc(&pdev->dev,
93 sizeof(struct sprd_hwspinlock_dev) +
94 SPRD_HWLOCKS_NUM * sizeof(*lock),
95 GFP_KERNEL);
96 if (!sprd_hwlock)
97 return -ENOMEM;
99 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
100 sprd_hwlock->base = devm_ioremap_resource(&pdev->dev, res);
101 if (IS_ERR(sprd_hwlock->base))
102 return PTR_ERR(sprd_hwlock->base);
104 sprd_hwlock->clk = devm_clk_get(&pdev->dev, "enable");
105 if (IS_ERR(sprd_hwlock->clk)) {
106 dev_err(&pdev->dev, "get hwspinlock clock failed!\n");
107 return PTR_ERR(sprd_hwlock->clk);
110 clk_prepare_enable(sprd_hwlock->clk);
112 /* set the hwspinlock to record user id to identify subsystems */
113 writel(HWSPINLOCK_USER_BITS, sprd_hwlock->base + HWSPINLOCK_RECCTRL);
115 for (i = 0; i < SPRD_HWLOCKS_NUM; i++) {
116 lock = &sprd_hwlock->bank.lock[i];
117 lock->priv = sprd_hwlock->base + HWSPINLOCK_TOKEN(i);
120 platform_set_drvdata(pdev, sprd_hwlock);
121 pm_runtime_enable(&pdev->dev);
123 ret = hwspin_lock_register(&sprd_hwlock->bank, &pdev->dev,
124 &sprd_hwspinlock_ops, 0, SPRD_HWLOCKS_NUM);
125 if (ret) {
126 pm_runtime_disable(&pdev->dev);
127 clk_disable_unprepare(sprd_hwlock->clk);
128 return ret;
131 return 0;
134 static int sprd_hwspinlock_remove(struct platform_device *pdev)
136 struct sprd_hwspinlock_dev *sprd_hwlock = platform_get_drvdata(pdev);
138 hwspin_lock_unregister(&sprd_hwlock->bank);
139 pm_runtime_disable(&pdev->dev);
140 clk_disable_unprepare(sprd_hwlock->clk);
141 return 0;
144 static const struct of_device_id sprd_hwspinlock_of_match[] = {
145 { .compatible = "sprd,hwspinlock-r3p0", },
146 { /* sentinel */ }
148 MODULE_DEVICE_TABLE(of, sprd_hwspinlock_of_match);
150 static struct platform_driver sprd_hwspinlock_driver = {
151 .probe = sprd_hwspinlock_probe,
152 .remove = sprd_hwspinlock_remove,
153 .driver = {
154 .name = "sprd_hwspinlock",
155 .of_match_table = of_match_ptr(sprd_hwspinlock_of_match),
159 static int __init sprd_hwspinlock_init(void)
161 return platform_driver_register(&sprd_hwspinlock_driver);
163 postcore_initcall(sprd_hwspinlock_init);
165 static void __exit sprd_hwspinlock_exit(void)
167 platform_driver_unregister(&sprd_hwspinlock_driver);
169 module_exit(sprd_hwspinlock_exit);
171 MODULE_LICENSE("GPL v2");
172 MODULE_DESCRIPTION("Hardware spinlock driver for Spreadtrum");
173 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
174 MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>");
175 MODULE_AUTHOR("Long Cheng <aiden.cheng@spreadtrum.com>");