1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SPI host driver for ICP DAS LP-8841 RTC
5 * Copyright (C) 2016 Sergei Ianovich
9 * Dallas DS1302 RTC Support
10 * Copyright (C) 2002 David McCullough
11 * Copyright (C) 2003 - 2007 Paul Mundt
13 #include <linux/delay.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
18 #include <linux/spi/spi.h>
20 #define DRIVER_NAME "spi_lp8841_rtc"
22 #define SPI_LP8841_RTC_CE 0x01
23 #define SPI_LP8841_RTC_CLK 0x02
24 #define SPI_LP8841_RTC_nWE 0x04
25 #define SPI_LP8841_RTC_MOSI 0x08
26 #define SPI_LP8841_RTC_MISO 0x01
29 * REVISIT If there is support for SPI_3WIRE and SPI_LSB_FIRST in SPI
30 * GPIO driver, this SPI driver can be replaced by a simple GPIO driver
31 * providing 3 GPIO pins.
34 struct spi_lp8841_rtc
{
40 setsck(struct spi_lp8841_rtc
*data
, int is_on
)
43 data
->state
|= SPI_LP8841_RTC_CLK
;
45 data
->state
&= ~SPI_LP8841_RTC_CLK
;
46 writeb(data
->state
, data
->iomem
);
50 setmosi(struct spi_lp8841_rtc
*data
, int is_on
)
53 data
->state
|= SPI_LP8841_RTC_MOSI
;
55 data
->state
&= ~SPI_LP8841_RTC_MOSI
;
56 writeb(data
->state
, data
->iomem
);
60 getmiso(struct spi_lp8841_rtc
*data
)
62 return ioread8(data
->iomem
) & SPI_LP8841_RTC_MISO
;
66 bitbang_txrx_be_cpha0_lsb(struct spi_lp8841_rtc
*data
,
67 unsigned usecs
, unsigned cpol
, unsigned flags
,
70 /* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
72 u32 shift
= 32 - bits
;
73 /* clock starts at inactive polarity */
74 for (; likely(bits
); bits
--) {
76 /* setup LSB (to target) on leading edge */
77 if ((flags
& SPI_CONTROLLER_NO_TX
) == 0)
78 setmosi(data
, (word
& 1));
80 usleep_range(usecs
, usecs
+ 1); /* T(setup) */
82 /* sample LSB (from target) on trailing edge */
84 if ((flags
& SPI_CONTROLLER_NO_RX
) == 0)
85 word
|= (getmiso(data
) << 31);
88 usleep_range(usecs
, usecs
+ 1);
98 spi_lp8841_rtc_transfer_one(struct spi_controller
*host
,
99 struct spi_device
*spi
,
100 struct spi_transfer
*t
)
102 struct spi_lp8841_rtc
*data
= spi_controller_get_devdata(host
);
103 unsigned count
= t
->len
;
104 const u8
*tx
= t
->tx_buf
;
110 data
->state
&= ~SPI_LP8841_RTC_nWE
;
111 writeb(data
->state
, data
->iomem
);
112 while (likely(count
> 0)) {
114 bitbang_txrx_be_cpha0_lsb(data
, 1, 0,
115 SPI_CONTROLLER_NO_RX
, word
, 8);
119 data
->state
|= SPI_LP8841_RTC_nWE
;
120 writeb(data
->state
, data
->iomem
);
121 while (likely(count
> 0)) {
122 word
= bitbang_txrx_be_cpha0_lsb(data
, 1, 0,
123 SPI_CONTROLLER_NO_TX
, word
, 8);
131 spi_finalize_current_transfer(host
);
137 spi_lp8841_rtc_set_cs(struct spi_device
*spi
, bool enable
)
139 struct spi_lp8841_rtc
*data
= spi_controller_get_devdata(spi
->controller
);
142 writeb(data
->state
, data
->iomem
);
145 data
->state
|= SPI_LP8841_RTC_CE
;
146 writeb(data
->state
, data
->iomem
);
152 spi_lp8841_rtc_setup(struct spi_device
*spi
)
154 if ((spi
->mode
& SPI_CS_HIGH
) == 0) {
155 dev_err(&spi
->dev
, "unsupported active low chip select\n");
159 if ((spi
->mode
& SPI_LSB_FIRST
) == 0) {
160 dev_err(&spi
->dev
, "unsupported MSB first mode\n");
164 if ((spi
->mode
& SPI_3WIRE
) == 0) {
165 dev_err(&spi
->dev
, "unsupported wiring. 3 wires required\n");
173 static const struct of_device_id spi_lp8841_rtc_dt_ids
[] = {
174 { .compatible
= "icpdas,lp8841-spi-rtc" },
178 MODULE_DEVICE_TABLE(of
, spi_lp8841_rtc_dt_ids
);
182 spi_lp8841_rtc_probe(struct platform_device
*pdev
)
185 struct spi_controller
*host
;
186 struct spi_lp8841_rtc
*data
;
188 host
= spi_alloc_host(&pdev
->dev
, sizeof(*data
));
191 platform_set_drvdata(pdev
, host
);
193 host
->flags
= SPI_CONTROLLER_HALF_DUPLEX
;
194 host
->mode_bits
= SPI_CS_HIGH
| SPI_3WIRE
| SPI_LSB_FIRST
;
196 host
->bus_num
= pdev
->id
;
197 host
->num_chipselect
= 1;
198 host
->setup
= spi_lp8841_rtc_setup
;
199 host
->set_cs
= spi_lp8841_rtc_set_cs
;
200 host
->transfer_one
= spi_lp8841_rtc_transfer_one
;
201 host
->bits_per_word_mask
= SPI_BPW_MASK(8);
203 host
->dev
.of_node
= pdev
->dev
.of_node
;
206 data
= spi_controller_get_devdata(host
);
208 data
->iomem
= devm_platform_ioremap_resource(pdev
, 0);
209 ret
= PTR_ERR_OR_ZERO(data
->iomem
);
211 dev_err(&pdev
->dev
, "failed to get IO address\n");
215 /* register with the SPI framework */
216 ret
= devm_spi_register_controller(&pdev
->dev
, host
);
218 dev_err(&pdev
->dev
, "cannot register spi host\n");
226 spi_controller_put(host
);
231 MODULE_ALIAS("platform:" DRIVER_NAME
);
233 static struct platform_driver spi_lp8841_rtc_driver
= {
236 .of_match_table
= of_match_ptr(spi_lp8841_rtc_dt_ids
),
238 .probe
= spi_lp8841_rtc_probe
,
240 module_platform_driver(spi_lp8841_rtc_driver
);
242 MODULE_DESCRIPTION("SPI host driver for ICP DAS LP-8841 RTC");
243 MODULE_AUTHOR("Sergei Ianovich");
244 MODULE_LICENSE("GPL");