2 * SPI master driver for ICP DAS LP-8841 RTC
4 * Copyright (C) 2016 Sergei Ianovich
8 * Dallas DS1302 RTC Support
9 * Copyright (C) 2002 David McCullough
10 * Copyright (C) 2003 - 2007 Paul Mundt
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 #include <linux/delay.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
27 #include <linux/of_device.h>
28 #include <linux/spi/spi.h>
30 #define DRIVER_NAME "spi_lp8841_rtc"
32 #define SPI_LP8841_RTC_CE 0x01
33 #define SPI_LP8841_RTC_CLK 0x02
34 #define SPI_LP8841_RTC_nWE 0x04
35 #define SPI_LP8841_RTC_MOSI 0x08
36 #define SPI_LP8841_RTC_MISO 0x01
39 * REVISIT If there is support for SPI_3WIRE and SPI_LSB_FIRST in SPI
40 * GPIO driver, this SPI driver can be replaced by a simple GPIO driver
41 * providing 3 GPIO pins.
44 struct spi_lp8841_rtc
{
50 setsck(struct spi_lp8841_rtc
*data
, int is_on
)
53 data
->state
|= SPI_LP8841_RTC_CLK
;
55 data
->state
&= ~SPI_LP8841_RTC_CLK
;
56 writeb(data
->state
, data
->iomem
);
60 setmosi(struct spi_lp8841_rtc
*data
, int is_on
)
63 data
->state
|= SPI_LP8841_RTC_MOSI
;
65 data
->state
&= ~SPI_LP8841_RTC_MOSI
;
66 writeb(data
->state
, data
->iomem
);
70 getmiso(struct spi_lp8841_rtc
*data
)
72 return ioread8(data
->iomem
) & SPI_LP8841_RTC_MISO
;
76 bitbang_txrx_be_cpha0_lsb(struct spi_lp8841_rtc
*data
,
77 unsigned usecs
, unsigned cpol
, unsigned flags
,
80 /* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
82 u32 shift
= 32 - bits
;
83 /* clock starts at inactive polarity */
84 for (; likely(bits
); bits
--) {
86 /* setup LSB (to slave) on leading edge */
87 if ((flags
& SPI_MASTER_NO_TX
) == 0)
88 setmosi(data
, (word
& 1));
90 usleep_range(usecs
, usecs
+ 1); /* T(setup) */
92 /* sample LSB (from slave) on trailing edge */
94 if ((flags
& SPI_MASTER_NO_RX
) == 0)
95 word
|= (getmiso(data
) << 31);
98 usleep_range(usecs
, usecs
+ 1);
108 spi_lp8841_rtc_transfer_one(struct spi_master
*master
,
109 struct spi_device
*spi
,
110 struct spi_transfer
*t
)
112 struct spi_lp8841_rtc
*data
= spi_master_get_devdata(master
);
113 unsigned count
= t
->len
;
114 const u8
*tx
= t
->tx_buf
;
120 data
->state
&= ~SPI_LP8841_RTC_nWE
;
121 writeb(data
->state
, data
->iomem
);
122 while (likely(count
> 0)) {
124 bitbang_txrx_be_cpha0_lsb(data
, 1, 0,
125 SPI_MASTER_NO_RX
, word
, 8);
129 data
->state
|= SPI_LP8841_RTC_nWE
;
130 writeb(data
->state
, data
->iomem
);
131 while (likely(count
> 0)) {
132 word
= bitbang_txrx_be_cpha0_lsb(data
, 1, 0,
133 SPI_MASTER_NO_TX
, word
, 8);
141 spi_finalize_current_transfer(master
);
147 spi_lp8841_rtc_set_cs(struct spi_device
*spi
, bool enable
)
149 struct spi_lp8841_rtc
*data
= spi_master_get_devdata(spi
->master
);
152 writeb(data
->state
, data
->iomem
);
155 data
->state
|= SPI_LP8841_RTC_CE
;
156 writeb(data
->state
, data
->iomem
);
162 spi_lp8841_rtc_setup(struct spi_device
*spi
)
164 if ((spi
->mode
& SPI_CS_HIGH
) == 0) {
165 dev_err(&spi
->dev
, "unsupported active low chip select\n");
169 if ((spi
->mode
& SPI_LSB_FIRST
) == 0) {
170 dev_err(&spi
->dev
, "unsupported MSB first mode\n");
174 if ((spi
->mode
& SPI_3WIRE
) == 0) {
175 dev_err(&spi
->dev
, "unsupported wiring. 3 wires required\n");
183 static const struct of_device_id spi_lp8841_rtc_dt_ids
[] = {
184 { .compatible
= "icpdas,lp8841-spi-rtc" },
188 MODULE_DEVICE_TABLE(of
, spi_lp8841_rtc_dt_ids
);
192 spi_lp8841_rtc_probe(struct platform_device
*pdev
)
195 struct spi_master
*master
;
196 struct spi_lp8841_rtc
*data
;
199 master
= spi_alloc_master(&pdev
->dev
, sizeof(*data
));
202 platform_set_drvdata(pdev
, master
);
204 master
->flags
= SPI_MASTER_HALF_DUPLEX
;
205 master
->mode_bits
= SPI_CS_HIGH
| SPI_3WIRE
| SPI_LSB_FIRST
;
207 master
->bus_num
= pdev
->id
;
208 master
->num_chipselect
= 1;
209 master
->setup
= spi_lp8841_rtc_setup
;
210 master
->set_cs
= spi_lp8841_rtc_set_cs
;
211 master
->transfer_one
= spi_lp8841_rtc_transfer_one
;
212 master
->bits_per_word_mask
= SPI_BPW_MASK(8);
214 master
->dev
.of_node
= pdev
->dev
.of_node
;
217 data
= spi_master_get_devdata(master
);
219 iomem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
220 data
->iomem
= devm_ioremap_resource(&pdev
->dev
, iomem
);
221 ret
= PTR_ERR_OR_ZERO(data
->iomem
);
223 dev_err(&pdev
->dev
, "failed to get IO address\n");
227 /* register with the SPI framework */
228 ret
= devm_spi_register_master(&pdev
->dev
, master
);
230 dev_err(&pdev
->dev
, "cannot register spi master\n");
238 spi_master_put(master
);
243 MODULE_ALIAS("platform:" DRIVER_NAME
);
245 static struct platform_driver spi_lp8841_rtc_driver
= {
248 .of_match_table
= of_match_ptr(spi_lp8841_rtc_dt_ids
),
250 .probe
= spi_lp8841_rtc_probe
,
252 module_platform_driver(spi_lp8841_rtc_driver
);
254 MODULE_DESCRIPTION("SPI master driver for ICP DAS LP-8841 RTC");
255 MODULE_AUTHOR("Sergei Ianovich");
256 MODULE_LICENSE("GPL");