blob: 382e2a69f7a7be1c5ce7d1f47df89cfcfea23da2 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +03002/*
Yang Yingliang48815832023-08-23 11:29:44 +08003 * SPI host driver for ICP DAS LP-8841 RTC
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +03004 *
5 * Copyright (C) 2016 Sergei Ianovich
6 *
7 * based on
8 *
9 * Dallas DS1302 RTC Support
10 * Copyright (C) 2002 David McCullough
11 * Copyright (C) 2003 - 2007 Paul Mundt
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +030012 */
13#include <linux/delay.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/of.h>
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +030018#include <linux/spi/spi.h>
19
20#define DRIVER_NAME "spi_lp8841_rtc"
21
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
27
28/*
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.
32 */
33
34struct spi_lp8841_rtc {
35 void *iomem;
36 unsigned long state;
37};
38
39static inline void
40setsck(struct spi_lp8841_rtc *data, int is_on)
41{
42 if (is_on)
43 data->state |= SPI_LP8841_RTC_CLK;
44 else
45 data->state &= ~SPI_LP8841_RTC_CLK;
46 writeb(data->state, data->iomem);
47}
48
49static inline void
50setmosi(struct spi_lp8841_rtc *data, int is_on)
51{
52 if (is_on)
53 data->state |= SPI_LP8841_RTC_MOSI;
54 else
55 data->state &= ~SPI_LP8841_RTC_MOSI;
56 writeb(data->state, data->iomem);
57}
58
59static inline int
60getmiso(struct spi_lp8841_rtc *data)
61{
62 return ioread8(data->iomem) & SPI_LP8841_RTC_MISO;
63}
64
65static inline u32
66bitbang_txrx_be_cpha0_lsb(struct spi_lp8841_rtc *data,
67 unsigned usecs, unsigned cpol, unsigned flags,
68 u32 word, u8 bits)
69{
70 /* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
71
72 u32 shift = 32 - bits;
73 /* clock starts at inactive polarity */
74 for (; likely(bits); bits--) {
75
Yang Yingliang48815832023-08-23 11:29:44 +080076 /* setup LSB (to target) on leading edge */
Andy Shevchenkoc397f092023-07-10 18:49:28 +030077 if ((flags & SPI_CONTROLLER_NO_TX) == 0)
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +030078 setmosi(data, (word & 1));
79
80 usleep_range(usecs, usecs + 1); /* T(setup) */
81
Yang Yingliang48815832023-08-23 11:29:44 +080082 /* sample LSB (from target) on trailing edge */
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +030083 word >>= 1;
Andy Shevchenkoc397f092023-07-10 18:49:28 +030084 if ((flags & SPI_CONTROLLER_NO_RX) == 0)
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +030085 word |= (getmiso(data) << 31);
86
87 setsck(data, !cpol);
88 usleep_range(usecs, usecs + 1);
89
90 setsck(data, cpol);
91 }
92
93 word >>= shift;
94 return word;
95}
96
97static int
Yang Yingliang48815832023-08-23 11:29:44 +080098spi_lp8841_rtc_transfer_one(struct spi_controller *host,
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +030099 struct spi_device *spi,
100 struct spi_transfer *t)
101{
Yang Yingliang48815832023-08-23 11:29:44 +0800102 struct spi_lp8841_rtc *data = spi_controller_get_devdata(host);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300103 unsigned count = t->len;
104 const u8 *tx = t->tx_buf;
105 u8 *rx = t->rx_buf;
106 u8 word = 0;
107 int ret = 0;
108
109 if (tx) {
110 data->state &= ~SPI_LP8841_RTC_nWE;
111 writeb(data->state, data->iomem);
112 while (likely(count > 0)) {
113 word = *tx++;
114 bitbang_txrx_be_cpha0_lsb(data, 1, 0,
Andy Shevchenkoc397f092023-07-10 18:49:28 +0300115 SPI_CONTROLLER_NO_RX, word, 8);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300116 count--;
117 }
118 } else if (rx) {
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,
Andy Shevchenkoc397f092023-07-10 18:49:28 +0300123 SPI_CONTROLLER_NO_TX, word, 8);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300124 *rx++ = word;
125 count--;
126 }
127 } else {
128 ret = -EINVAL;
129 }
130
Yang Yingliang48815832023-08-23 11:29:44 +0800131 spi_finalize_current_transfer(host);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300132
133 return ret;
134}
135
136static void
137spi_lp8841_rtc_set_cs(struct spi_device *spi, bool enable)
138{
Yang Yingliang48815832023-08-23 11:29:44 +0800139 struct spi_lp8841_rtc *data = spi_controller_get_devdata(spi->controller);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300140
141 data->state = 0;
142 writeb(data->state, data->iomem);
143 if (enable) {
144 usleep_range(4, 5);
145 data->state |= SPI_LP8841_RTC_CE;
146 writeb(data->state, data->iomem);
147 usleep_range(4, 5);
148 }
149}
150
151static int
152spi_lp8841_rtc_setup(struct spi_device *spi)
153{
154 if ((spi->mode & SPI_CS_HIGH) == 0) {
155 dev_err(&spi->dev, "unsupported active low chip select\n");
156 return -EINVAL;
157 }
158
159 if ((spi->mode & SPI_LSB_FIRST) == 0) {
160 dev_err(&spi->dev, "unsupported MSB first mode\n");
161 return -EINVAL;
162 }
163
164 if ((spi->mode & SPI_3WIRE) == 0) {
165 dev_err(&spi->dev, "unsupported wiring. 3 wires required\n");
166 return -EINVAL;
167 }
168
169 return 0;
170}
171
172#ifdef CONFIG_OF
173static const struct of_device_id spi_lp8841_rtc_dt_ids[] = {
174 { .compatible = "icpdas,lp8841-spi-rtc" },
175 { }
176};
177
178MODULE_DEVICE_TABLE(of, spi_lp8841_rtc_dt_ids);
179#endif
180
181static int
182spi_lp8841_rtc_probe(struct platform_device *pdev)
183{
184 int ret;
Yang Yingliang48815832023-08-23 11:29:44 +0800185 struct spi_controller *host;
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300186 struct spi_lp8841_rtc *data;
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300187
Yang Yingliang48815832023-08-23 11:29:44 +0800188 host = spi_alloc_host(&pdev->dev, sizeof(*data));
189 if (!host)
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300190 return -ENOMEM;
Yang Yingliang48815832023-08-23 11:29:44 +0800191 platform_set_drvdata(pdev, host);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300192
Yang Yingliang48815832023-08-23 11:29:44 +0800193 host->flags = SPI_CONTROLLER_HALF_DUPLEX;
194 host->mode_bits = SPI_CS_HIGH | SPI_3WIRE | SPI_LSB_FIRST;
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300195
Yang Yingliang48815832023-08-23 11:29:44 +0800196 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);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300202#ifdef CONFIG_OF
Yang Yingliang48815832023-08-23 11:29:44 +0800203 host->dev.of_node = pdev->dev.of_node;
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300204#endif
205
Yang Yingliang48815832023-08-23 11:29:44 +0800206 data = spi_controller_get_devdata(host);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300207
YueHaibing7d2600b2019-09-04 21:58:56 +0800208 data->iomem = devm_platform_ioremap_resource(pdev, 0);
Arnd Bergmann14a20422016-02-25 12:37:40 +0100209 ret = PTR_ERR_OR_ZERO(data->iomem);
210 if (ret) {
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300211 dev_err(&pdev->dev, "failed to get IO address\n");
Yang Yingliang48815832023-08-23 11:29:44 +0800212 goto err_put_host;
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300213 }
214
215 /* register with the SPI framework */
Yang Yingliang48815832023-08-23 11:29:44 +0800216 ret = devm_spi_register_controller(&pdev->dev, host);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300217 if (ret) {
Yang Yingliang48815832023-08-23 11:29:44 +0800218 dev_err(&pdev->dev, "cannot register spi host\n");
219 goto err_put_host;
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300220 }
221
222 return ret;
223
224
Yang Yingliang48815832023-08-23 11:29:44 +0800225err_put_host:
226 spi_controller_put(host);
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300227
228 return ret;
229}
230
231MODULE_ALIAS("platform:" DRIVER_NAME);
232
233static struct platform_driver spi_lp8841_rtc_driver = {
234 .driver = {
235 .name = DRIVER_NAME,
236 .of_match_table = of_match_ptr(spi_lp8841_rtc_dt_ids),
237 },
238 .probe = spi_lp8841_rtc_probe,
239};
240module_platform_driver(spi_lp8841_rtc_driver);
241
Yang Yingliang48815832023-08-23 11:29:44 +0800242MODULE_DESCRIPTION("SPI host driver for ICP DAS LP-8841 RTC");
Sergei Ianovich7ecbfff2016-02-23 13:44:28 +0300243MODULE_AUTHOR("Sergei Ianovich");
244MODULE_LICENSE("GPL");