blob: d2da92187d5687db01f18948f88fc1db736bee8e [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Raphael Assenat362600f2006-06-25 05:48:24 -07002/* drivers/rtc/rtc-v3020.c
3 *
4 * Copyright (C) 2006 8D Technologies inc.
5 * Copyright (C) 2004 Compulab Ltd.
6 *
Raphael Assenat362600f2006-06-25 05:48:24 -07007 * Driver for the V3020 RTC
8 *
9 * Changelog:
10 *
11 * 10-May-2006: Raphael Assenat <raph@8d.com>
12 * - Converted to platform driver
13 * - Use the generic rtc class
14 *
15 * ??-???-2004: Someone at Compulab
Sachin Kamatde2edf32013-07-03 15:06:05 -070016 * - Initial driver creation.
Raphael Assenat362600f2006-06-25 05:48:24 -070017 */
18#include <linux/platform_device.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/rtc.h>
22#include <linux/types.h>
23#include <linux/bcd.h>
Alexandre Bellonicd9b5182016-06-27 00:03:03 +020024#include <linux/platform_data/rtc-v3020.h>
Mike Rapoportf3d79b22007-09-11 15:23:45 -070025#include <linux/delay.h>
Mike Rapoport96615842009-04-02 16:57:01 -070026#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Raphael Assenat362600f2006-06-25 05:48:24 -070028
Mike Rapoportc08cf9d2009-03-31 15:24:59 -070029#include <linux/io.h>
Raphael Assenat362600f2006-06-25 05:48:24 -070030
31#undef DEBUG
32
Mike Rapoport96615842009-04-02 16:57:01 -070033struct v3020;
34
35struct v3020_chip_ops {
36 int (*map_io)(struct v3020 *chip, struct platform_device *pdev,
37 struct v3020_platform_data *pdata);
38 void (*unmap_io)(struct v3020 *chip);
39 unsigned char (*read_bit)(struct v3020 *chip);
40 void (*write_bit)(struct v3020 *chip, unsigned char bit);
41};
42
43#define V3020_CS 0
44#define V3020_WR 1
45#define V3020_RD 2
46#define V3020_IO 3
47
Raphael Assenat362600f2006-06-25 05:48:24 -070048struct v3020 {
Mike Rapoport96615842009-04-02 16:57:01 -070049 /* MMIO access */
Raphael Assenat362600f2006-06-25 05:48:24 -070050 void __iomem *ioaddress;
51 int leftshift;
Mike Rapoport96615842009-04-02 16:57:01 -070052
53 /* GPIO access */
Jingoo Han6c95fa82013-02-22 11:19:15 +090054 struct gpio *gpio;
Mike Rapoport96615842009-04-02 16:57:01 -070055
Julia Lawall7432a852015-12-11 18:50:50 +010056 const struct v3020_chip_ops *ops;
Mike Rapoport96615842009-04-02 16:57:01 -070057
Raphael Assenat362600f2006-06-25 05:48:24 -070058 struct rtc_device *rtc;
59};
60
Mike Rapoport96615842009-04-02 16:57:01 -070061
62static int v3020_mmio_map(struct v3020 *chip, struct platform_device *pdev,
63 struct v3020_platform_data *pdata)
64{
65 if (pdev->num_resources != 1)
66 return -EBUSY;
67
68 if (pdev->resource[0].flags != IORESOURCE_MEM)
69 return -EBUSY;
70
71 chip->leftshift = pdata->leftshift;
72 chip->ioaddress = ioremap(pdev->resource[0].start, 1);
73 if (chip->ioaddress == NULL)
74 return -EBUSY;
75
76 return 0;
77}
78
79static void v3020_mmio_unmap(struct v3020 *chip)
80{
81 iounmap(chip->ioaddress);
82}
83
84static void v3020_mmio_write_bit(struct v3020 *chip, unsigned char bit)
85{
86 writel(bit << chip->leftshift, chip->ioaddress);
87}
88
89static unsigned char v3020_mmio_read_bit(struct v3020 *chip)
90{
Scott Valentinebcb3a162009-11-11 14:26:49 -080091 return !!(readl(chip->ioaddress) & (1 << chip->leftshift));
Mike Rapoport96615842009-04-02 16:57:01 -070092}
93
Julia Lawall7432a852015-12-11 18:50:50 +010094static const struct v3020_chip_ops v3020_mmio_ops = {
Mike Rapoport96615842009-04-02 16:57:01 -070095 .map_io = v3020_mmio_map,
96 .unmap_io = v3020_mmio_unmap,
97 .read_bit = v3020_mmio_read_bit,
98 .write_bit = v3020_mmio_write_bit,
99};
100
Jingoo Han6c95fa82013-02-22 11:19:15 +0900101static struct gpio v3020_gpio[] = {
102 { 0, GPIOF_OUT_INIT_HIGH, "RTC CS"},
103 { 0, GPIOF_OUT_INIT_HIGH, "RTC WR"},
104 { 0, GPIOF_OUT_INIT_HIGH, "RTC RD"},
105 { 0, GPIOF_OUT_INIT_HIGH, "RTC IO"},
Mike Rapoport96615842009-04-02 16:57:01 -0700106};
107
108static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
109 struct v3020_platform_data *pdata)
110{
Jingoo Han6c95fa82013-02-22 11:19:15 +0900111 int err;
Mike Rapoport96615842009-04-02 16:57:01 -0700112
113 v3020_gpio[V3020_CS].gpio = pdata->gpio_cs;
114 v3020_gpio[V3020_WR].gpio = pdata->gpio_wr;
115 v3020_gpio[V3020_RD].gpio = pdata->gpio_rd;
116 v3020_gpio[V3020_IO].gpio = pdata->gpio_io;
117
Jingoo Han6c95fa82013-02-22 11:19:15 +0900118 err = gpio_request_array(v3020_gpio, ARRAY_SIZE(v3020_gpio));
Mike Rapoport96615842009-04-02 16:57:01 -0700119
Jingoo Han6c95fa82013-02-22 11:19:15 +0900120 if (!err)
121 chip->gpio = v3020_gpio;
Mike Rapoport96615842009-04-02 16:57:01 -0700122
123 return err;
124}
125
126static void v3020_gpio_unmap(struct v3020 *chip)
127{
Jingoo Han6c95fa82013-02-22 11:19:15 +0900128 gpio_free_array(v3020_gpio, ARRAY_SIZE(v3020_gpio));
Mike Rapoport96615842009-04-02 16:57:01 -0700129}
130
131static void v3020_gpio_write_bit(struct v3020 *chip, unsigned char bit)
132{
133 gpio_direction_output(chip->gpio[V3020_IO].gpio, bit);
134 gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
135 gpio_set_value(chip->gpio[V3020_WR].gpio, 0);
136 udelay(1);
137 gpio_set_value(chip->gpio[V3020_WR].gpio, 1);
138 gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
139}
140
141static unsigned char v3020_gpio_read_bit(struct v3020 *chip)
142{
143 int bit;
144
145 gpio_direction_input(chip->gpio[V3020_IO].gpio);
146 gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
147 gpio_set_value(chip->gpio[V3020_RD].gpio, 0);
148 udelay(1);
149 bit = !!gpio_get_value(chip->gpio[V3020_IO].gpio);
150 udelay(1);
151 gpio_set_value(chip->gpio[V3020_RD].gpio, 1);
152 gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
153
154 return bit;
155}
156
Julia Lawall7432a852015-12-11 18:50:50 +0100157static const struct v3020_chip_ops v3020_gpio_ops = {
Mike Rapoport96615842009-04-02 16:57:01 -0700158 .map_io = v3020_gpio_map,
159 .unmap_io = v3020_gpio_unmap,
160 .read_bit = v3020_gpio_read_bit,
161 .write_bit = v3020_gpio_write_bit,
162};
163
Raphael Assenat362600f2006-06-25 05:48:24 -0700164static void v3020_set_reg(struct v3020 *chip, unsigned char address,
165 unsigned char data)
166{
167 int i;
168 unsigned char tmp;
169
170 tmp = address;
171 for (i = 0; i < 4; i++) {
Mike Rapoport96615842009-04-02 16:57:01 -0700172 chip->ops->write_bit(chip, (tmp & 1));
Raphael Assenat362600f2006-06-25 05:48:24 -0700173 tmp >>= 1;
Mike Rapoportf3d79b22007-09-11 15:23:45 -0700174 udelay(1);
Raphael Assenat362600f2006-06-25 05:48:24 -0700175 }
176
177 /* Commands dont have data */
178 if (!V3020_IS_COMMAND(address)) {
179 for (i = 0; i < 8; i++) {
Mike Rapoport96615842009-04-02 16:57:01 -0700180 chip->ops->write_bit(chip, (data & 1));
Raphael Assenat362600f2006-06-25 05:48:24 -0700181 data >>= 1;
Mike Rapoportf3d79b22007-09-11 15:23:45 -0700182 udelay(1);
Raphael Assenat362600f2006-06-25 05:48:24 -0700183 }
184 }
185}
186
187static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
188{
Mike Rapoportc08cf9d2009-03-31 15:24:59 -0700189 unsigned int data = 0;
Raphael Assenat362600f2006-06-25 05:48:24 -0700190 int i;
191
192 for (i = 0; i < 4; i++) {
Mike Rapoport96615842009-04-02 16:57:01 -0700193 chip->ops->write_bit(chip, (address & 1));
Raphael Assenat362600f2006-06-25 05:48:24 -0700194 address >>= 1;
Mike Rapoportf3d79b22007-09-11 15:23:45 -0700195 udelay(1);
Raphael Assenat362600f2006-06-25 05:48:24 -0700196 }
197
198 for (i = 0; i < 8; i++) {
199 data >>= 1;
Mike Rapoport96615842009-04-02 16:57:01 -0700200 if (chip->ops->read_bit(chip))
Raphael Assenat362600f2006-06-25 05:48:24 -0700201 data |= 0x80;
Mike Rapoportf3d79b22007-09-11 15:23:45 -0700202 udelay(1);
Raphael Assenat362600f2006-06-25 05:48:24 -0700203 }
204
205 return data;
206}
207
208static int v3020_read_time(struct device *dev, struct rtc_time *dt)
209{
210 struct v3020 *chip = dev_get_drvdata(dev);
211 int tmp;
212
213 /* Copy the current time to ram... */
214 v3020_set_reg(chip, V3020_CMD_CLOCK2RAM, 0);
215
216 /* ...and then read constant values. */
217 tmp = v3020_get_reg(chip, V3020_SECONDS);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700218 dt->tm_sec = bcd2bin(tmp);
Raphael Assenat362600f2006-06-25 05:48:24 -0700219 tmp = v3020_get_reg(chip, V3020_MINUTES);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700220 dt->tm_min = bcd2bin(tmp);
Raphael Assenat362600f2006-06-25 05:48:24 -0700221 tmp = v3020_get_reg(chip, V3020_HOURS);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700222 dt->tm_hour = bcd2bin(tmp);
Raphael Assenat362600f2006-06-25 05:48:24 -0700223 tmp = v3020_get_reg(chip, V3020_MONTH_DAY);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700224 dt->tm_mday = bcd2bin(tmp);
Raphael Assenat362600f2006-06-25 05:48:24 -0700225 tmp = v3020_get_reg(chip, V3020_MONTH);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700226 dt->tm_mon = bcd2bin(tmp) - 1;
Raphael Assenat362600f2006-06-25 05:48:24 -0700227 tmp = v3020_get_reg(chip, V3020_WEEK_DAY);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700228 dt->tm_wday = bcd2bin(tmp);
Raphael Assenat362600f2006-06-25 05:48:24 -0700229 tmp = v3020_get_reg(chip, V3020_YEAR);
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700230 dt->tm_year = bcd2bin(tmp)+100;
Raphael Assenat362600f2006-06-25 05:48:24 -0700231
Mike Rapoportc08cf9d2009-03-31 15:24:59 -0700232 dev_dbg(dev, "\n%s : Read RTC values\n", __func__);
233 dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
234 dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
235 dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
236 dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
237 dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
238 dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
239 dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
Raphael Assenat362600f2006-06-25 05:48:24 -0700240
241 return 0;
242}
243
244
245static int v3020_set_time(struct device *dev, struct rtc_time *dt)
246{
247 struct v3020 *chip = dev_get_drvdata(dev);
248
Mike Rapoportc08cf9d2009-03-31 15:24:59 -0700249 dev_dbg(dev, "\n%s : Setting RTC values\n", __func__);
250 dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
251 dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
252 dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
253 dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
254 dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
255 dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
Raphael Assenat362600f2006-06-25 05:48:24 -0700256
257 /* Write all the values to ram... */
Sachin Kamatde2edf32013-07-03 15:06:05 -0700258 v3020_set_reg(chip, V3020_SECONDS, bin2bcd(dt->tm_sec));
259 v3020_set_reg(chip, V3020_MINUTES, bin2bcd(dt->tm_min));
260 v3020_set_reg(chip, V3020_HOURS, bin2bcd(dt->tm_hour));
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700261 v3020_set_reg(chip, V3020_MONTH_DAY, bin2bcd(dt->tm_mday));
Sachin Kamatde2edf32013-07-03 15:06:05 -0700262 v3020_set_reg(chip, V3020_MONTH, bin2bcd(dt->tm_mon + 1));
263 v3020_set_reg(chip, V3020_WEEK_DAY, bin2bcd(dt->tm_wday));
264 v3020_set_reg(chip, V3020_YEAR, bin2bcd(dt->tm_year % 100));
Raphael Assenat362600f2006-06-25 05:48:24 -0700265
266 /* ...and set the clock. */
267 v3020_set_reg(chip, V3020_CMD_RAM2CLOCK, 0);
268
269 /* Compulab used this delay here. I dont know why,
270 * the datasheet does not specify a delay. */
271 /*mdelay(5);*/
272
273 return 0;
274}
275
David Brownellff8371a2006-09-30 23:28:17 -0700276static const struct rtc_class_ops v3020_rtc_ops = {
Raphael Assenat362600f2006-06-25 05:48:24 -0700277 .read_time = v3020_read_time,
278 .set_time = v3020_set_time,
279};
280
281static int rtc_probe(struct platform_device *pdev)
282{
Jingoo Hana65ddce2013-11-12 15:10:53 -0800283 struct v3020_platform_data *pdata = dev_get_platdata(&pdev->dev);
Raphael Assenat362600f2006-06-25 05:48:24 -0700284 struct v3020 *chip;
Raphael Assenat362600f2006-06-25 05:48:24 -0700285 int retval = -EBUSY;
286 int i;
Raphael Assenat362600f2006-06-25 05:48:24 -0700287
Jingoo Han431c6c12013-04-29 16:20:56 -0700288 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
Raphael Assenat362600f2006-06-25 05:48:24 -0700289 if (!chip)
290 return -ENOMEM;
291
Mike Rapoport96615842009-04-02 16:57:01 -0700292 if (pdata->use_gpio)
293 chip->ops = &v3020_gpio_ops;
294 else
295 chip->ops = &v3020_mmio_ops;
296
297 retval = chip->ops->map_io(chip, pdev, pdata);
298 if (retval)
Sachin Kamatd8d52902013-07-03 15:05:41 -0700299 return retval;
Raphael Assenat362600f2006-06-25 05:48:24 -0700300
301 /* Make sure the v3020 expects a communication cycle
302 * by reading 8 times */
303 for (i = 0; i < 8; i++)
Alexandre Belloni42397492019-11-22 11:22:12 +0100304 chip->ops->read_bit(chip);
Raphael Assenat362600f2006-06-25 05:48:24 -0700305
306 /* Test chip by doing a write/read sequence
307 * to the chip ram */
308 v3020_set_reg(chip, V3020_SECONDS, 0x33);
Mike Rapoportc08cf9d2009-03-31 15:24:59 -0700309 if (v3020_get_reg(chip, V3020_SECONDS) != 0x33) {
Raphael Assenat362600f2006-06-25 05:48:24 -0700310 retval = -ENODEV;
311 goto err_io;
312 }
313
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200314 /* Make sure frequency measurement mode, test modes, and lock
Raphael Assenat362600f2006-06-25 05:48:24 -0700315 * are all disabled */
316 v3020_set_reg(chip, V3020_STATUS_0, 0x0);
317
Mike Rapoport96615842009-04-02 16:57:01 -0700318 if (pdata->use_gpio)
319 dev_info(&pdev->dev, "Chip available at GPIOs "
320 "%d, %d, %d, %d\n",
321 chip->gpio[V3020_CS].gpio, chip->gpio[V3020_WR].gpio,
322 chip->gpio[V3020_RD].gpio, chip->gpio[V3020_IO].gpio);
323 else
324 dev_info(&pdev->dev, "Chip available at "
325 "physical address 0x%llx,"
326 "data connected to D%d\n",
327 (unsigned long long)pdev->resource[0].start,
328 chip->leftshift);
Raphael Assenat362600f2006-06-25 05:48:24 -0700329
330 platform_set_drvdata(pdev, chip);
331
Jingoo Han431c6c12013-04-29 16:20:56 -0700332 chip->rtc = devm_rtc_device_register(&pdev->dev, "v3020",
333 &v3020_rtc_ops, THIS_MODULE);
Alessandro Zummob74d2ca2009-12-15 16:45:53 -0800334 if (IS_ERR(chip->rtc)) {
335 retval = PTR_ERR(chip->rtc);
Raphael Assenat362600f2006-06-25 05:48:24 -0700336 goto err_io;
337 }
Raphael Assenat362600f2006-06-25 05:48:24 -0700338
339 return 0;
340
341err_io:
Mike Rapoport96615842009-04-02 16:57:01 -0700342 chip->ops->unmap_io(chip);
Sachin Kamatd8d52902013-07-03 15:05:41 -0700343
Raphael Assenat362600f2006-06-25 05:48:24 -0700344 return retval;
345}
346
347static int rtc_remove(struct platform_device *dev)
348{
349 struct v3020 *chip = platform_get_drvdata(dev);
Raphael Assenat362600f2006-06-25 05:48:24 -0700350
Mike Rapoport96615842009-04-02 16:57:01 -0700351 chip->ops->unmap_io(chip);
Raphael Assenat362600f2006-06-25 05:48:24 -0700352
353 return 0;
354}
355
356static struct platform_driver rtc_device_driver = {
357 .probe = rtc_probe,
358 .remove = rtc_remove,
359 .driver = {
360 .name = "v3020",
Raphael Assenat362600f2006-06-25 05:48:24 -0700361 },
362};
363
Axel Lin0c4eae62012-01-10 15:10:48 -0800364module_platform_driver(rtc_device_driver);
Raphael Assenat362600f2006-06-25 05:48:24 -0700365
366MODULE_DESCRIPTION("V3020 RTC");
367MODULE_AUTHOR("Raphael Assenat");
368MODULE_LICENSE("GPL");
Kay Sieversad28a072008-04-10 21:29:25 -0700369MODULE_ALIAS("platform:v3020");