Blame view

drivers/spi/spi-sprd-adi.c 14.6 KB
7e2903cb9   Baolin Wang   spi: Add ADI driv...
1
2
3
4
5
  /*
   * Copyright (C) 2017 Spreadtrum Communications Inc.
   *
   * SPDX-License-Identifier: GPL-2.0
   */
ac1775012   Baolin Wang   spi: sprd: Add th...
6
  #include <linux/delay.h>
7e2903cb9   Baolin Wang   spi: Add ADI driv...
7
8
9
10
11
12
13
14
  #include <linux/hwspinlock.h>
  #include <linux/init.h>
  #include <linux/io.h>
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/of_device.h>
  #include <linux/platform_device.h>
ac1775012   Baolin Wang   spi: sprd: Add th...
15
  #include <linux/reboot.h>
7e2903cb9   Baolin Wang   spi: Add ADI driv...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include <linux/spi/spi.h>
  #include <linux/sizes.h>
  
  /* Registers definitions for ADI controller */
  #define REG_ADI_CTRL0			0x4
  #define REG_ADI_CHN_PRIL		0x8
  #define REG_ADI_CHN_PRIH		0xc
  #define REG_ADI_INT_EN			0x10
  #define REG_ADI_INT_RAW			0x14
  #define REG_ADI_INT_MASK		0x18
  #define REG_ADI_INT_CLR			0x1c
  #define REG_ADI_GSSI_CFG0		0x20
  #define REG_ADI_GSSI_CFG1		0x24
  #define REG_ADI_RD_CMD			0x28
  #define REG_ADI_RD_DATA			0x2c
  #define REG_ADI_ARM_FIFO_STS		0x30
  #define REG_ADI_STS			0x34
  #define REG_ADI_EVT_FIFO_STS		0x38
  #define REG_ADI_ARM_CMD_STS		0x3c
  #define REG_ADI_CHN_EN			0x40
  #define REG_ADI_CHN_ADDR(id)		(0x44 + (id - 2) * 4)
  #define REG_ADI_CHN_EN1			0x20c
  
  /* Bits definitions for register REG_ADI_GSSI_CFG0 */
  #define BIT_CLK_ALL_ON			BIT(30)
  
  /* Bits definitions for register REG_ADI_RD_DATA */
  #define BIT_RD_CMD_BUSY			BIT(31)
  #define RD_ADDR_SHIFT			16
  #define RD_VALUE_MASK			GENMASK(15, 0)
  #define RD_ADDR_MASK			GENMASK(30, 16)
  
  /* Bits definitions for register REG_ADI_ARM_FIFO_STS */
  #define BIT_FIFO_FULL			BIT(11)
  #define BIT_FIFO_EMPTY			BIT(10)
  
  /*
   * ADI slave devices include RTC, ADC, regulator, charger, thermal and so on.
   * The slave devices address offset is always 0x8000 and size is 4K.
   */
  #define ADI_SLAVE_ADDR_SIZE		SZ_4K
  #define ADI_SLAVE_OFFSET		0x8000
  
  /* Timeout (ms) for the trylock of hardware spinlocks */
  #define ADI_HWSPINLOCK_TIMEOUT		5000
  /*
   * ADI controller has 50 channels including 2 software channels
   * and 48 hardware channels.
   */
  #define ADI_HW_CHNS			50
  
  #define ADI_FIFO_DRAIN_TIMEOUT		1000
  #define ADI_READ_TIMEOUT		2000
  #define REG_ADDR_LOW_MASK		GENMASK(11, 0)
ac1775012   Baolin Wang   spi: sprd: Add th...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  /* Registers definitions for PMIC watchdog controller */
  #define REG_WDG_LOAD_LOW		0x80
  #define REG_WDG_LOAD_HIGH		0x84
  #define REG_WDG_CTRL			0x88
  #define REG_WDG_LOCK			0xa0
  
  /* Bits definitions for register REG_WDG_CTRL */
  #define BIT_WDG_RUN			BIT(1)
  #define BIT_WDG_RST			BIT(3)
  
  /* Registers definitions for PMIC */
  #define PMIC_RST_STATUS			0xee8
  #define PMIC_MODULE_EN			0xc08
  #define PMIC_CLK_EN			0xc18
  #define BIT_WDG_EN			BIT(2)
  
  /* Definition of PMIC reset status register */
cc6b3431b   Chenxu Wei   spi: sprd: adi: A...
87
  #define HWRST_STATUS_SECURITY		0x02
ac1775012   Baolin Wang   spi: sprd: Add th...
88
89
90
91
92
93
94
95
96
97
98
  #define HWRST_STATUS_RECOVERY		0x20
  #define HWRST_STATUS_NORMAL		0x40
  #define HWRST_STATUS_ALARM		0x50
  #define HWRST_STATUS_SLEEP		0x60
  #define HWRST_STATUS_FASTBOOT		0x30
  #define HWRST_STATUS_SPECIAL		0x70
  #define HWRST_STATUS_PANIC		0x80
  #define HWRST_STATUS_CFTREBOOT		0x90
  #define HWRST_STATUS_AUTODLOADER	0xa0
  #define HWRST_STATUS_IQMODE		0xb0
  #define HWRST_STATUS_SPRDISK		0xc0
9d9aa1cc9   Sherry Zong   spi: sprd: adi: A...
99
  #define HWRST_STATUS_FACTORYTEST	0xe0
e6d722ca0   Sherry Zong   spi: sprd: adi: A...
100
  #define HWRST_STATUS_WATCHDOG		0xf0
ac1775012   Baolin Wang   spi: sprd: Add th...
101
102
103
104
105
  
  /* Use default timeout 50 ms that converts to watchdog values */
  #define WDG_LOAD_VAL			((50 * 1000) / 32768)
  #define WDG_LOAD_MASK			GENMASK(15, 0)
  #define WDG_UNLOCK_KEY			0xe551
7e2903cb9   Baolin Wang   spi: Add ADI driv...
106
107
108
109
110
111
112
  struct sprd_adi {
  	struct spi_controller	*ctlr;
  	struct device		*dev;
  	void __iomem		*base;
  	struct hwspinlock	*hwlock;
  	unsigned long		slave_vbase;
  	unsigned long		slave_pbase;
ac1775012   Baolin Wang   spi: sprd: Add th...
113
  	struct notifier_block	restart_handler;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  };
  
  static int sprd_adi_check_paddr(struct sprd_adi *sadi, u32 paddr)
  {
  	if (paddr < sadi->slave_pbase || paddr >
  	    (sadi->slave_pbase + ADI_SLAVE_ADDR_SIZE)) {
  		dev_err(sadi->dev,
  			"slave physical address is incorrect, addr = 0x%x
  ",
  			paddr);
  		return -EINVAL;
  	}
  
  	return 0;
  }
  
  static unsigned long sprd_adi_to_vaddr(struct sprd_adi *sadi, u32 paddr)
  {
  	return (paddr - sadi->slave_pbase + sadi->slave_vbase);
  }
  
  static int sprd_adi_drain_fifo(struct sprd_adi *sadi)
  {
  	u32 timeout = ADI_FIFO_DRAIN_TIMEOUT;
  	u32 sts;
  
  	do {
  		sts = readl_relaxed(sadi->base + REG_ADI_ARM_FIFO_STS);
  		if (sts & BIT_FIFO_EMPTY)
  			break;
  
  		cpu_relax();
  	} while (--timeout);
  
  	if (timeout == 0) {
  		dev_err(sadi->dev, "drain write fifo timeout
  ");
  		return -EBUSY;
  	}
  
  	return 0;
  }
  
  static int sprd_adi_fifo_is_full(struct sprd_adi *sadi)
  {
  	return readl_relaxed(sadi->base + REG_ADI_ARM_FIFO_STS) & BIT_FIFO_FULL;
  }
  
  static int sprd_adi_read(struct sprd_adi *sadi, u32 reg_paddr, u32 *read_val)
  {
  	int read_timeout = ADI_READ_TIMEOUT;
a61aa6836   Baolin Wang   spi: sprd: Simpli...
165
  	unsigned long flags;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
166
  	u32 val, rd_addr;
f9adf61e9   Baolin Wang   spi: sprd: adi: C...
167
168
169
170
171
172
173
174
175
176
177
  	int ret = 0;
  
  	if (sadi->hwlock) {
  		ret = hwspin_lock_timeout_irqsave(sadi->hwlock,
  						  ADI_HWSPINLOCK_TIMEOUT,
  						  &flags);
  		if (ret) {
  			dev_err(sadi->dev, "get the hw lock failed
  ");
  			return ret;
  		}
a61aa6836   Baolin Wang   spi: sprd: Simpli...
178
  	}
7e2903cb9   Baolin Wang   spi: Add ADI driv...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  
  	/*
  	 * Set the physical register address need to read into RD_CMD register,
  	 * then ADI controller will start to transfer automatically.
  	 */
  	writel_relaxed(reg_paddr, sadi->base + REG_ADI_RD_CMD);
  
  	/*
  	 * Wait read operation complete, the BIT_RD_CMD_BUSY will be set
  	 * simultaneously when writing read command to register, and the
  	 * BIT_RD_CMD_BUSY will be cleared after the read operation is
  	 * completed.
  	 */
  	do {
  		val = readl_relaxed(sadi->base + REG_ADI_RD_DATA);
  		if (!(val & BIT_RD_CMD_BUSY))
  			break;
  
  		cpu_relax();
  	} while (--read_timeout);
  
  	if (read_timeout == 0) {
  		dev_err(sadi->dev, "ADI read timeout
  ");
a61aa6836   Baolin Wang   spi: sprd: Simpli...
203
204
  		ret = -EBUSY;
  		goto out;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  	}
  
  	/*
  	 * The return value includes data and read register address, from bit 0
  	 * to bit 15 are data, and from bit 16 to bit 30 are read register
  	 * address. Then we can check the returned register address to validate
  	 * data.
  	 */
  	rd_addr = (val & RD_ADDR_MASK ) >> RD_ADDR_SHIFT;
  
  	if (rd_addr != (reg_paddr & REG_ADDR_LOW_MASK)) {
  		dev_err(sadi->dev, "read error, reg addr = 0x%x, val = 0x%x
  ",
  			reg_paddr, val);
a61aa6836   Baolin Wang   spi: sprd: Simpli...
219
220
  		ret = -EIO;
  		goto out;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
221
222
223
  	}
  
  	*read_val = val & RD_VALUE_MASK;
a61aa6836   Baolin Wang   spi: sprd: Simpli...
224
225
  
  out:
f9adf61e9   Baolin Wang   spi: sprd: adi: C...
226
227
  	if (sadi->hwlock)
  		hwspin_unlock_irqrestore(sadi->hwlock, &flags);
a61aa6836   Baolin Wang   spi: sprd: Simpli...
228
  	return ret;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
229
  }
a61aa6836   Baolin Wang   spi: sprd: Simpli...
230
  static int sprd_adi_write(struct sprd_adi *sadi, u32 reg_paddr, u32 val)
7e2903cb9   Baolin Wang   spi: Add ADI driv...
231
  {
a61aa6836   Baolin Wang   spi: sprd: Simpli...
232
  	unsigned long reg = sprd_adi_to_vaddr(sadi, reg_paddr);
7e2903cb9   Baolin Wang   spi: Add ADI driv...
233
  	u32 timeout = ADI_FIFO_DRAIN_TIMEOUT;
a61aa6836   Baolin Wang   spi: sprd: Simpli...
234
  	unsigned long flags;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
235
  	int ret;
f9adf61e9   Baolin Wang   spi: sprd: adi: C...
236
237
238
239
240
241
242
243
244
  	if (sadi->hwlock) {
  		ret = hwspin_lock_timeout_irqsave(sadi->hwlock,
  						  ADI_HWSPINLOCK_TIMEOUT,
  						  &flags);
  		if (ret) {
  			dev_err(sadi->dev, "get the hw lock failed
  ");
  			return ret;
  		}
a61aa6836   Baolin Wang   spi: sprd: Simpli...
245
  	}
7e2903cb9   Baolin Wang   spi: Add ADI driv...
246
247
  	ret = sprd_adi_drain_fifo(sadi);
  	if (ret < 0)
a61aa6836   Baolin Wang   spi: sprd: Simpli...
248
  		goto out;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  
  	/*
  	 * we should wait for write fifo is empty before writing data to PMIC
  	 * registers.
  	 */
  	do {
  		if (!sprd_adi_fifo_is_full(sadi)) {
  			writel_relaxed(val, (void __iomem *)reg);
  			break;
  		}
  
  		cpu_relax();
  	} while (--timeout);
  
  	if (timeout == 0) {
  		dev_err(sadi->dev, "write fifo is full
  ");
a61aa6836   Baolin Wang   spi: sprd: Simpli...
266
  		ret = -EBUSY;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
267
  	}
a61aa6836   Baolin Wang   spi: sprd: Simpli...
268
  out:
f9adf61e9   Baolin Wang   spi: sprd: adi: C...
269
270
  	if (sadi->hwlock)
  		hwspin_unlock_irqrestore(sadi->hwlock, &flags);
a61aa6836   Baolin Wang   spi: sprd: Simpli...
271
  	return ret;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
272
273
274
275
276
277
278
  }
  
  static int sprd_adi_transfer_one(struct spi_controller *ctlr,
  				 struct spi_device *spi_dev,
  				 struct spi_transfer *t)
  {
  	struct sprd_adi *sadi = spi_controller_get_devdata(ctlr);
7e2903cb9   Baolin Wang   spi: Add ADI driv...
279
280
281
282
283
284
285
286
287
  	u32 phy_reg, val;
  	int ret;
  
  	if (t->rx_buf) {
  		phy_reg = *(u32 *)t->rx_buf + sadi->slave_pbase;
  
  		ret = sprd_adi_check_paddr(sadi, phy_reg);
  		if (ret)
  			return ret;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
288
  		ret = sprd_adi_read(sadi, phy_reg, &val);
7e2903cb9   Baolin Wang   spi: Add ADI driv...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
  		if (ret)
  			return ret;
  
  		*(u32 *)t->rx_buf = val;
  	} else if (t->tx_buf) {
  		u32 *p = (u32 *)t->tx_buf;
  
  		/*
  		 * Get the physical register address need to write and convert
  		 * the physical address to virtual address. Since we need
  		 * virtual register address to write.
  		 */
  		phy_reg = *p++ + sadi->slave_pbase;
  		ret = sprd_adi_check_paddr(sadi, phy_reg);
  		if (ret)
  			return ret;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
305
  		val = *p;
a61aa6836   Baolin Wang   spi: sprd: Simpli...
306
  		ret = sprd_adi_write(sadi, phy_reg, val);
7e2903cb9   Baolin Wang   spi: Add ADI driv...
307
308
309
310
311
312
313
314
315
316
  		if (ret)
  			return ret;
  	} else {
  		dev_err(sadi->dev, "no buffer for transfer
  ");
  		return -EINVAL;
  	}
  
  	return 0;
  }
e6d722ca0   Sherry Zong   spi: sprd: adi: A...
317
318
319
320
321
322
323
324
325
326
327
  static void sprd_adi_set_wdt_rst_mode(struct sprd_adi *sadi)
  {
  #ifdef CONFIG_SPRD_WATCHDOG
  	u32 val;
  
  	/* Set default watchdog reboot mode */
  	sprd_adi_read(sadi, sadi->slave_pbase + PMIC_RST_STATUS, &val);
  	val |= HWRST_STATUS_WATCHDOG;
  	sprd_adi_write(sadi, sadi->slave_pbase + PMIC_RST_STATUS, val);
  #endif
  }
ac1775012   Baolin Wang   spi: sprd: Add th...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  static int sprd_adi_restart_handler(struct notifier_block *this,
  				    unsigned long mode, void *cmd)
  {
  	struct sprd_adi *sadi = container_of(this, struct sprd_adi,
  					     restart_handler);
  	u32 val, reboot_mode = 0;
  
  	if (!cmd)
  		reboot_mode = HWRST_STATUS_NORMAL;
  	else if (!strncmp(cmd, "recovery", 8))
  		reboot_mode = HWRST_STATUS_RECOVERY;
  	else if (!strncmp(cmd, "alarm", 5))
  		reboot_mode = HWRST_STATUS_ALARM;
  	else if (!strncmp(cmd, "fastsleep", 9))
  		reboot_mode = HWRST_STATUS_SLEEP;
  	else if (!strncmp(cmd, "bootloader", 10))
  		reboot_mode = HWRST_STATUS_FASTBOOT;
  	else if (!strncmp(cmd, "panic", 5))
  		reboot_mode = HWRST_STATUS_PANIC;
  	else if (!strncmp(cmd, "special", 7))
  		reboot_mode = HWRST_STATUS_SPECIAL;
  	else if (!strncmp(cmd, "cftreboot", 9))
  		reboot_mode = HWRST_STATUS_CFTREBOOT;
  	else if (!strncmp(cmd, "autodloader", 11))
  		reboot_mode = HWRST_STATUS_AUTODLOADER;
  	else if (!strncmp(cmd, "iqmode", 6))
  		reboot_mode = HWRST_STATUS_IQMODE;
  	else if (!strncmp(cmd, "sprdisk", 7))
  		reboot_mode = HWRST_STATUS_SPRDISK;
cc6b3431b   Chenxu Wei   spi: sprd: adi: A...
357
358
  	else if (!strncmp(cmd, "tospanic", 8))
  		reboot_mode = HWRST_STATUS_SECURITY;
9d9aa1cc9   Sherry Zong   spi: sprd: adi: A...
359
360
  	else if (!strncmp(cmd, "factorytest", 11))
  		reboot_mode = HWRST_STATUS_FACTORYTEST;
ac1775012   Baolin Wang   spi: sprd: Add th...
361
362
363
364
365
  	else
  		reboot_mode = HWRST_STATUS_NORMAL;
  
  	/* Record the reboot mode */
  	sprd_adi_read(sadi, sadi->slave_pbase + PMIC_RST_STATUS, &val);
e6d722ca0   Sherry Zong   spi: sprd: adi: A...
366
  	val &= ~HWRST_STATUS_WATCHDOG;
ac1775012   Baolin Wang   spi: sprd: Add th...
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  	val |= reboot_mode;
  	sprd_adi_write(sadi, sadi->slave_pbase + PMIC_RST_STATUS, val);
  
  	/* Enable the interface clock of the watchdog */
  	sprd_adi_read(sadi, sadi->slave_pbase + PMIC_MODULE_EN, &val);
  	val |= BIT_WDG_EN;
  	sprd_adi_write(sadi, sadi->slave_pbase + PMIC_MODULE_EN, val);
  
  	/* Enable the work clock of the watchdog */
  	sprd_adi_read(sadi, sadi->slave_pbase + PMIC_CLK_EN, &val);
  	val |= BIT_WDG_EN;
  	sprd_adi_write(sadi, sadi->slave_pbase + PMIC_CLK_EN, val);
  
  	/* Unlock the watchdog */
  	sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_LOCK, WDG_UNLOCK_KEY);
  
  	/* Load the watchdog timeout value, 50ms is always enough. */
5d7e2852d   Lingling Xu   spi: sprd: switch...
384
  	sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_LOAD_HIGH, 0);
ac1775012   Baolin Wang   spi: sprd: Add th...
385
386
  	sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_LOAD_LOW,
  		       WDG_LOAD_VAL & WDG_LOAD_MASK);
ac1775012   Baolin Wang   spi: sprd: Add th...
387
388
389
390
391
  
  	/* Start the watchdog to reset system */
  	sprd_adi_read(sadi, sadi->slave_pbase + REG_WDG_CTRL, &val);
  	val |= BIT_WDG_RUN | BIT_WDG_RST;
  	sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_CTRL, val);
60e315db4   Lingling Xu   spi: sprd: adi: A...
392
393
  	/* Lock the watchdog */
  	sprd_adi_write(sadi, sadi->slave_pbase + REG_WDG_LOCK, ~WDG_UNLOCK_KEY);
ac1775012   Baolin Wang   spi: sprd: Add th...
394
395
396
397
398
399
  	mdelay(1000);
  
  	dev_emerg(sadi->dev, "Unable to restart system
  ");
  	return NOTIFY_DONE;
  }
7e2903cb9   Baolin Wang   spi: Add ADI driv...
400
401
402
403
404
405
  static void sprd_adi_hw_init(struct sprd_adi *sadi)
  {
  	struct device_node *np = sadi->dev->of_node;
  	int i, size, chn_cnt;
  	const __be32 *list;
  	u32 tmp;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
406
407
408
409
410
411
412
413
414
415
416
  	/* Set all channels as default priority */
  	writel_relaxed(0, sadi->base + REG_ADI_CHN_PRIL);
  	writel_relaxed(0, sadi->base + REG_ADI_CHN_PRIH);
  
  	/* Set clock auto gate mode */
  	tmp = readl_relaxed(sadi->base + REG_ADI_GSSI_CFG0);
  	tmp &= ~BIT_CLK_ALL_ON;
  	writel_relaxed(tmp, sadi->base + REG_ADI_GSSI_CFG0);
  
  	/* Set hardware channels setting */
  	list = of_get_property(np, "sprd,hw-channels", &size);
b0d6e097b   Dan Carpenter   spi: sprd-adi: si...
417
  	if (!list || !size) {
7e2903cb9   Baolin Wang   spi: Add ADI driv...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
  		dev_info(sadi->dev, "no hw channels setting in node
  ");
  		return;
  	}
  
  	chn_cnt = size / 8;
  	for (i = 0; i < chn_cnt; i++) {
  		u32 value;
  		u32 chn_id = be32_to_cpu(*list++);
  		u32 chn_config = be32_to_cpu(*list++);
  
  		/* Channel 0 and 1 are software channels */
  		if (chn_id < 2)
  			continue;
  
  		writel_relaxed(chn_config, sadi->base +
  			       REG_ADI_CHN_ADDR(chn_id));
54e2fc28d   Baolin Wang   spi: sprd: Fix th...
435
  		if (chn_id < 32) {
7e2903cb9   Baolin Wang   spi: Add ADI driv...
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
  			value = readl_relaxed(sadi->base + REG_ADI_CHN_EN);
  			value |= BIT(chn_id);
  			writel_relaxed(value, sadi->base + REG_ADI_CHN_EN);
  		} else if (chn_id < ADI_HW_CHNS) {
  			value = readl_relaxed(sadi->base + REG_ADI_CHN_EN1);
  			value |= BIT(chn_id - 32);
  			writel_relaxed(value, sadi->base + REG_ADI_CHN_EN1);
  		}
  	}
  }
  
  static int sprd_adi_probe(struct platform_device *pdev)
  {
  	struct device_node *np = pdev->dev.of_node;
  	struct spi_controller *ctlr;
  	struct sprd_adi *sadi;
  	struct resource *res;
  	u32 num_chipselect;
  	int ret;
  
  	if (!np) {
  		dev_err(&pdev->dev, "can not find the adi bus node
  ");
  		return -ENODEV;
  	}
  
  	pdev->id = of_alias_get_id(np, "spi");
  	num_chipselect = of_get_child_count(np);
  
  	ctlr = spi_alloc_master(&pdev->dev, sizeof(struct sprd_adi));
  	if (!ctlr)
  		return -ENOMEM;
  
  	dev_set_drvdata(&pdev->dev, ctlr);
  	sadi = spi_controller_get_devdata(ctlr);
  
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	sadi->base = devm_ioremap_resource(&pdev->dev, res);
04063a011   Dan Carpenter   spi: sprd-adi: ch...
474
475
  	if (IS_ERR(sadi->base)) {
  		ret = PTR_ERR(sadi->base);
7e2903cb9   Baolin Wang   spi: Add ADI driv...
476
477
478
479
480
481
482
  		goto put_ctlr;
  	}
  
  	sadi->slave_vbase = (unsigned long)sadi->base + ADI_SLAVE_OFFSET;
  	sadi->slave_pbase = res->start + ADI_SLAVE_OFFSET;
  	sadi->ctlr = ctlr;
  	sadi->dev = &pdev->dev;
f9adf61e9   Baolin Wang   spi: sprd: adi: C...
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
  	ret = of_hwspin_lock_get_id(np, 0);
  	if (ret > 0 || (IS_ENABLED(CONFIG_HWSPINLOCK) && ret == 0)) {
  		sadi->hwlock =
  			devm_hwspin_lock_request_specific(&pdev->dev, ret);
  		if (!sadi->hwlock) {
  			ret = -ENXIO;
  			goto put_ctlr;
  		}
  	} else {
  		switch (ret) {
  		case -ENOENT:
  			dev_info(&pdev->dev, "no hardware spinlock supplied
  ");
  			break;
  		default:
  			dev_err(&pdev->dev,
  				"failed to find hwlock id, %d
  ", ret);
  			/* fall-through */
  		case -EPROBE_DEFER:
  			goto put_ctlr;
  		}
7e2903cb9   Baolin Wang   spi: Add ADI driv...
505
506
507
  	}
  
  	sprd_adi_hw_init(sadi);
e6d722ca0   Sherry Zong   spi: sprd: adi: A...
508
  	sprd_adi_set_wdt_rst_mode(sadi);
7e2903cb9   Baolin Wang   spi: Add ADI driv...
509
510
511
512
513
514
515
516
517
518
519
520
  
  	ctlr->dev.of_node = pdev->dev.of_node;
  	ctlr->bus_num = pdev->id;
  	ctlr->num_chipselect = num_chipselect;
  	ctlr->flags = SPI_MASTER_HALF_DUPLEX;
  	ctlr->bits_per_word_mask = 0;
  	ctlr->transfer_one = sprd_adi_transfer_one;
  
  	ret = devm_spi_register_controller(&pdev->dev, ctlr);
  	if (ret) {
  		dev_err(&pdev->dev, "failed to register SPI controller
  ");
c8d049896   Baolin Wang   spi: sprd: Change...
521
  		goto put_ctlr;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
522
  	}
ac1775012   Baolin Wang   spi: sprd: Add th...
523
524
525
526
527
528
  	sadi->restart_handler.notifier_call = sprd_adi_restart_handler;
  	sadi->restart_handler.priority = 128;
  	ret = register_restart_handler(&sadi->restart_handler);
  	if (ret) {
  		dev_err(&pdev->dev, "can not register restart handler
  ");
c8d049896   Baolin Wang   spi: sprd: Change...
529
  		goto put_ctlr;
ac1775012   Baolin Wang   spi: sprd: Add th...
530
  	}
7e2903cb9   Baolin Wang   spi: Add ADI driv...
531
  	return 0;
7e2903cb9   Baolin Wang   spi: Add ADI driv...
532
533
534
535
536
537
538
539
540
  put_ctlr:
  	spi_controller_put(ctlr);
  	return ret;
  }
  
  static int sprd_adi_remove(struct platform_device *pdev)
  {
  	struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev);
  	struct sprd_adi *sadi = spi_controller_get_devdata(ctlr);
ac1775012   Baolin Wang   spi: sprd: Add th...
541
  	unregister_restart_handler(&sadi->restart_handler);
7e2903cb9   Baolin Wang   spi: Add ADI driv...
542
543
544
545
546
547
548
549
550
551
552
553
554
555
  	return 0;
  }
  
  static const struct of_device_id sprd_adi_of_match[] = {
  	{
  		.compatible = "sprd,sc9860-adi",
  	},
  	{ },
  };
  MODULE_DEVICE_TABLE(of, sprd_adi_of_match);
  
  static struct platform_driver sprd_adi_driver = {
  	.driver = {
  		.name = "sprd-adi",
7e2903cb9   Baolin Wang   spi: Add ADI driv...
556
557
558
559
560
561
562
563
564
565
  		.of_match_table = sprd_adi_of_match,
  	},
  	.probe = sprd_adi_probe,
  	.remove = sprd_adi_remove,
  };
  module_platform_driver(sprd_adi_driver);
  
  MODULE_DESCRIPTION("Spreadtrum ADI Controller Driver");
  MODULE_AUTHOR("Baolin Wang <Baolin.Wang@spreadtrum.com>");
  MODULE_LICENSE("GPL v2");