rtc-v3020.c 9.1 KB
Newer Older
R
Raphael Assenat 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* drivers/rtc/rtc-v3020.c
 *
 * Copyright (C) 2006 8D Technologies inc.
 * Copyright (C) 2004 Compulab Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Driver for the V3020 RTC
 *
 * Changelog:
 *
 *  10-May-2006: Raphael Assenat <raph@8d.com>
 *				- Converted to platform driver
 *				- Use the generic rtc class
 *
 *  ??-???-2004: Someone at Compulab
 *  			- Initial driver creation.
 *
 */
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/rtc.h>
#include <linux/types.h>
#include <linux/bcd.h>
#include <linux/rtc-v3020.h>
M
Mike Rapoport 已提交
29
#include <linux/delay.h>
30
#include <linux/gpio.h>
31
#include <linux/slab.h>
R
Raphael Assenat 已提交
32

M
Mike Rapoport 已提交
33
#include <linux/io.h>
R
Raphael Assenat 已提交
34 35 36

#undef DEBUG

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
struct v3020;

struct v3020_chip_ops {
	int (*map_io)(struct v3020 *chip, struct platform_device *pdev,
		      struct v3020_platform_data *pdata);
	void (*unmap_io)(struct v3020 *chip);
	unsigned char (*read_bit)(struct v3020 *chip);
	void (*write_bit)(struct v3020 *chip, unsigned char bit);
};

#define V3020_CS	0
#define V3020_WR	1
#define V3020_RD	2
#define V3020_IO	3

struct v3020_gpio {
	const char *name;
	unsigned int gpio;
};

R
Raphael Assenat 已提交
57
struct v3020 {
58
	/* MMIO access */
R
Raphael Assenat 已提交
59 60
	void __iomem *ioaddress;
	int leftshift;
61 62 63 64 65 66

	/* GPIO access */
	struct v3020_gpio *gpio;

	struct v3020_chip_ops *ops;

R
Raphael Assenat 已提交
67 68 69
	struct rtc_device *rtc;
};

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

static int v3020_mmio_map(struct v3020 *chip, struct platform_device *pdev,
			  struct v3020_platform_data *pdata)
{
	if (pdev->num_resources != 1)
		return -EBUSY;

	if (pdev->resource[0].flags != IORESOURCE_MEM)
		return -EBUSY;

	chip->leftshift = pdata->leftshift;
	chip->ioaddress = ioremap(pdev->resource[0].start, 1);
	if (chip->ioaddress == NULL)
		return -EBUSY;

	return 0;
}

static void v3020_mmio_unmap(struct v3020 *chip)
{
	iounmap(chip->ioaddress);
}

static void v3020_mmio_write_bit(struct v3020 *chip, unsigned char bit)
{
	writel(bit << chip->leftshift, chip->ioaddress);
}

static unsigned char v3020_mmio_read_bit(struct v3020 *chip)
{
100
	return !!(readl(chip->ioaddress) & (1 << chip->leftshift));
101 102 103 104 105 106 107 108 109 110 111 112 113 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}

static struct v3020_chip_ops v3020_mmio_ops = {
	.map_io		= v3020_mmio_map,
	.unmap_io	= v3020_mmio_unmap,
	.read_bit	= v3020_mmio_read_bit,
	.write_bit	= v3020_mmio_write_bit,
};

static struct v3020_gpio v3020_gpio[] = {
	{ "RTC CS", 0 },
	{ "RTC WR", 0 },
	{ "RTC RD", 0 },
	{ "RTC IO", 0 },
};

static int v3020_gpio_map(struct v3020 *chip, struct platform_device *pdev,
			  struct v3020_platform_data *pdata)
{
	int i, err;

	v3020_gpio[V3020_CS].gpio = pdata->gpio_cs;
	v3020_gpio[V3020_WR].gpio = pdata->gpio_wr;
	v3020_gpio[V3020_RD].gpio = pdata->gpio_rd;
	v3020_gpio[V3020_IO].gpio = pdata->gpio_io;

	for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++) {
		err = gpio_request(v3020_gpio[i].gpio, v3020_gpio[i].name);
		if (err)
			goto err_request;

		gpio_direction_output(v3020_gpio[i].gpio, 1);
	}

	chip->gpio = v3020_gpio;

	return 0;

err_request:
	while (--i >= 0)
		gpio_free(v3020_gpio[i].gpio);

	return err;
}

static void v3020_gpio_unmap(struct v3020 *chip)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(v3020_gpio); i++)
		gpio_free(v3020_gpio[i].gpio);
}

static void v3020_gpio_write_bit(struct v3020 *chip, unsigned char bit)
{
	gpio_direction_output(chip->gpio[V3020_IO].gpio, bit);
	gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
	gpio_set_value(chip->gpio[V3020_WR].gpio, 0);
	udelay(1);
	gpio_set_value(chip->gpio[V3020_WR].gpio, 1);
	gpio_set_value(chip->gpio[V3020_CS].gpio, 1);
}

static unsigned char v3020_gpio_read_bit(struct v3020 *chip)
{
	int bit;

	gpio_direction_input(chip->gpio[V3020_IO].gpio);
	gpio_set_value(chip->gpio[V3020_CS].gpio, 0);
	gpio_set_value(chip->gpio[V3020_RD].gpio, 0);
	udelay(1);
	bit = !!gpio_get_value(chip->gpio[V3020_IO].gpio);
	udelay(1);
	gpio_set_value(chip->gpio[V3020_RD].gpio, 1);
	gpio_set_value(chip->gpio[V3020_CS].gpio, 1);

	return bit;
}

static struct v3020_chip_ops v3020_gpio_ops = {
	.map_io		= v3020_gpio_map,
	.unmap_io	= v3020_gpio_unmap,
	.read_bit	= v3020_gpio_read_bit,
	.write_bit	= v3020_gpio_write_bit,
};

R
Raphael Assenat 已提交
187 188 189 190 191 192 193 194
static void v3020_set_reg(struct v3020 *chip, unsigned char address,
			unsigned char data)
{
	int i;
	unsigned char tmp;

	tmp = address;
	for (i = 0; i < 4; i++) {
195
		chip->ops->write_bit(chip, (tmp & 1));
R
Raphael Assenat 已提交
196
		tmp >>= 1;
M
Mike Rapoport 已提交
197
		udelay(1);
R
Raphael Assenat 已提交
198 199 200 201 202
	}

	/* Commands dont have data */
	if (!V3020_IS_COMMAND(address)) {
		for (i = 0; i < 8; i++) {
203
			chip->ops->write_bit(chip, (data & 1));
R
Raphael Assenat 已提交
204
			data >>= 1;
M
Mike Rapoport 已提交
205
			udelay(1);
R
Raphael Assenat 已提交
206 207 208 209 210 211
		}
	}
}

static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
{
M
Mike Rapoport 已提交
212
	unsigned int data = 0;
R
Raphael Assenat 已提交
213 214 215
	int i;

	for (i = 0; i < 4; i++) {
216
		chip->ops->write_bit(chip, (address & 1));
R
Raphael Assenat 已提交
217
		address >>= 1;
M
Mike Rapoport 已提交
218
		udelay(1);
R
Raphael Assenat 已提交
219 220 221 222
	}

	for (i = 0; i < 8; i++) {
		data >>= 1;
223
		if (chip->ops->read_bit(chip))
R
Raphael Assenat 已提交
224
			data |= 0x80;
M
Mike Rapoport 已提交
225
		udelay(1);
R
Raphael Assenat 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	}

	return data;
}

static int v3020_read_time(struct device *dev, struct rtc_time *dt)
{
	struct v3020 *chip = dev_get_drvdata(dev);
	int tmp;

	/* Copy the current time to ram... */
	v3020_set_reg(chip, V3020_CMD_CLOCK2RAM, 0);

	/* ...and then read constant values. */
	tmp = v3020_get_reg(chip, V3020_SECONDS);
A
Adrian Bunk 已提交
241
	dt->tm_sec	= bcd2bin(tmp);
R
Raphael Assenat 已提交
242
	tmp = v3020_get_reg(chip, V3020_MINUTES);
A
Adrian Bunk 已提交
243
	dt->tm_min	= bcd2bin(tmp);
R
Raphael Assenat 已提交
244
	tmp = v3020_get_reg(chip, V3020_HOURS);
A
Adrian Bunk 已提交
245
	dt->tm_hour	= bcd2bin(tmp);
R
Raphael Assenat 已提交
246
	tmp = v3020_get_reg(chip, V3020_MONTH_DAY);
A
Adrian Bunk 已提交
247
	dt->tm_mday	= bcd2bin(tmp);
R
Raphael Assenat 已提交
248
	tmp = v3020_get_reg(chip, V3020_MONTH);
A
Adrian Bunk 已提交
249
	dt->tm_mon    = bcd2bin(tmp) - 1;
R
Raphael Assenat 已提交
250
	tmp = v3020_get_reg(chip, V3020_WEEK_DAY);
A
Adrian Bunk 已提交
251
	dt->tm_wday	= bcd2bin(tmp);
R
Raphael Assenat 已提交
252
	tmp = v3020_get_reg(chip, V3020_YEAR);
A
Adrian Bunk 已提交
253
	dt->tm_year = bcd2bin(tmp)+100;
R
Raphael Assenat 已提交
254

M
Mike Rapoport 已提交
255 256 257 258 259 260 261 262
	dev_dbg(dev, "\n%s : Read RTC values\n", __func__);
	dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
	dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
	dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
	dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
	dev_dbg(dev, "tm_mon : %i\n", dt->tm_mon);
	dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
	dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
R
Raphael Assenat 已提交
263 264 265 266 267 268 269 270 271

	return 0;
}


static int v3020_set_time(struct device *dev, struct rtc_time *dt)
{
	struct v3020 *chip = dev_get_drvdata(dev);

M
Mike Rapoport 已提交
272 273 274 275 276 277 278
	dev_dbg(dev, "\n%s : Setting RTC values\n", __func__);
	dev_dbg(dev, "tm_sec : %i\n", dt->tm_sec);
	dev_dbg(dev, "tm_min : %i\n", dt->tm_min);
	dev_dbg(dev, "tm_hour: %i\n", dt->tm_hour);
	dev_dbg(dev, "tm_mday: %i\n", dt->tm_mday);
	dev_dbg(dev, "tm_wday: %i\n", dt->tm_wday);
	dev_dbg(dev, "tm_year: %i\n", dt->tm_year);
R
Raphael Assenat 已提交
279 280

	/* Write all the values to ram... */
A
Adrian Bunk 已提交
281 282 283 284 285 286 287
	v3020_set_reg(chip, V3020_SECONDS, 	bin2bcd(dt->tm_sec));
	v3020_set_reg(chip, V3020_MINUTES, 	bin2bcd(dt->tm_min));
	v3020_set_reg(chip, V3020_HOURS, 	bin2bcd(dt->tm_hour));
	v3020_set_reg(chip, V3020_MONTH_DAY,	bin2bcd(dt->tm_mday));
	v3020_set_reg(chip, V3020_MONTH,     bin2bcd(dt->tm_mon + 1));
	v3020_set_reg(chip, V3020_WEEK_DAY, 	bin2bcd(dt->tm_wday));
	v3020_set_reg(chip, V3020_YEAR, 	bin2bcd(dt->tm_year % 100));
R
Raphael Assenat 已提交
288 289 290 291 292 293 294 295 296 297 298

	/* ...and set the clock. */
	v3020_set_reg(chip, V3020_CMD_RAM2CLOCK, 0);

	/* Compulab used this delay here. I dont know why,
	 * the datasheet does not specify a delay. */
	/*mdelay(5);*/

	return 0;
}

299
static const struct rtc_class_ops v3020_rtc_ops = {
R
Raphael Assenat 已提交
300 301 302 303 304 305 306 307 308 309 310 311
	.read_time	= v3020_read_time,
	.set_time	= v3020_set_time,
};

static int rtc_probe(struct platform_device *pdev)
{
	struct v3020_platform_data *pdata = pdev->dev.platform_data;
	struct v3020 *chip;
	int retval = -EBUSY;
	int i;
	int temp;

312
	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
R
Raphael Assenat 已提交
313 314 315
	if (!chip)
		return -ENOMEM;

316 317 318 319 320 321 322
	if (pdata->use_gpio)
		chip->ops = &v3020_gpio_ops;
	else
		chip->ops = &v3020_mmio_ops;

	retval = chip->ops->map_io(chip, pdev, pdata);
	if (retval)
323
		return retval;
R
Raphael Assenat 已提交
324 325 326 327

	/* Make sure the v3020 expects a communication cycle
	 * by reading 8 times */
	for (i = 0; i < 8; i++)
328
		temp = chip->ops->read_bit(chip);
R
Raphael Assenat 已提交
329 330 331 332

	/* Test chip by doing a write/read sequence
	 * to the chip ram */
	v3020_set_reg(chip, V3020_SECONDS, 0x33);
M
Mike Rapoport 已提交
333
	if (v3020_get_reg(chip, V3020_SECONDS) != 0x33) {
R
Raphael Assenat 已提交
334 335 336 337
		retval = -ENODEV;
		goto err_io;
	}

338
	/* Make sure frequency measurement mode, test modes, and lock
R
Raphael Assenat 已提交
339 340 341
	 * are all disabled */
	v3020_set_reg(chip, V3020_STATUS_0, 0x0);

342 343 344 345 346 347 348 349 350 351 352
	if (pdata->use_gpio)
		dev_info(&pdev->dev, "Chip available at GPIOs "
			 "%d, %d, %d, %d\n",
			 chip->gpio[V3020_CS].gpio, chip->gpio[V3020_WR].gpio,
			 chip->gpio[V3020_RD].gpio, chip->gpio[V3020_IO].gpio);
	else
		dev_info(&pdev->dev, "Chip available at "
			 "physical address 0x%llx,"
			 "data connected to D%d\n",
			 (unsigned long long)pdev->resource[0].start,
			 chip->leftshift);
R
Raphael Assenat 已提交
353 354 355

	platform_set_drvdata(pdev, chip);

356 357
	chip->rtc = devm_rtc_device_register(&pdev->dev, "v3020",
					&v3020_rtc_ops, THIS_MODULE);
358 359
	if (IS_ERR(chip->rtc)) {
		retval = PTR_ERR(chip->rtc);
R
Raphael Assenat 已提交
360 361 362 363 364 365
		goto err_io;
	}

	return 0;

err_io:
366
	chip->ops->unmap_io(chip);
367

R
Raphael Assenat 已提交
368 369 370 371 372 373 374
	return retval;
}

static int rtc_remove(struct platform_device *dev)
{
	struct v3020 *chip = platform_get_drvdata(dev);

375
	chip->ops->unmap_io(chip);
R
Raphael Assenat 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388

	return 0;
}

static struct platform_driver rtc_device_driver = {
	.probe	= rtc_probe,
	.remove = rtc_remove,
	.driver = {
		.name	= "v3020",
		.owner	= THIS_MODULE,
	},
};

389
module_platform_driver(rtc_device_driver);
R
Raphael Assenat 已提交
390 391 392 393

MODULE_DESCRIPTION("V3020 RTC");
MODULE_AUTHOR("Raphael Assenat");
MODULE_LICENSE("GPL");
394
MODULE_ALIAS("platform:v3020");