aboutsummaryrefslogtreecommitdiffstats
path: root/net/openvswitch
diff options
context:
space:
mode:
Diffstat (limited to 'net/openvswitch')
0 files changed, 0 insertions, 0 deletions
id='n232' href='#n232'>232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
/*
 * Micro Crystal RV-3029C2 rtc class driver
 *
 * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
 *
 * based on previously existing rtc class drivers
 *
 * 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.
 *
 * NOTE: Currently this driver only supports the bare minimum for read
 * and write the RTC and alarms. The extra features provided by this chip
 * (trickle charger, eeprom, T° compensation) are unavailable.
 */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/bcd.h>
#include <linux/rtc.h>

/* Register map */
/* control section */
#define RV3029C2_ONOFF_CTRL		0x00
#define RV3029C2_IRQ_CTRL		0x01
#define RV3029C2_IRQ_CTRL_AIE		(1 << 0)
#define RV3029C2_IRQ_FLAGS		0x02
#define RV3029C2_IRQ_FLAGS_AF		(1 << 0)
#define RV3029C2_STATUS			0x03
#define RV3029C2_STATUS_VLOW1		(1 << 2)
#define RV3029C2_STATUS_VLOW2		(1 << 3)
#define RV3029C2_STATUS_SR		(1 << 4)
#define RV3029C2_STATUS_PON		(1 << 5)
#define RV3029C2_STATUS_EEBUSY		(1 << 7)
#define RV3029C2_RST_CTRL		0x04
#define RV3029C2_CONTROL_SECTION_LEN	0x05

/* watch section */
#define RV3029C2_W_SEC			0x08
#define RV3029C2_W_MINUTES		0x09
#define RV3029C2_W_HOURS		0x0A
#define RV3029C2_REG_HR_12_24		(1<<6)  /* 24h/12h mode */
#define RV3029C2_REG_HR_PM		(1<<5)  /* PM/AM bit in 12h mode */
#define RV3029C2_W_DATE			0x0B
#define RV3029C2_W_DAYS			0x0C
#define RV3029C2_W_MONTHS		0x0D
#define RV3029C2_W_YEARS		0x0E
#define RV3029C2_WATCH_SECTION_LEN	0x07

/* alarm section */
#define RV3029C2_A_SC			0x10
#define RV3029C2_A_MN			0x11
#define RV3029C2_A_HR			0x12
#define RV3029C2_A_DT			0x13
#define RV3029C2_A_DW			0x14
#define RV3029C2_A_MO			0x15
#define RV3029C2_A_YR			0x16
#define RV3029C2_ALARM_SECTION_LEN	0x07

/* timer section */
#define RV3029C2_TIMER_LOW		0x18
#define RV3029C2_TIMER_HIGH		0x19

/* temperature section */
#define RV3029C2_TEMP_PAGE		0x20

/* eeprom data section */
#define RV3029C2_E2P_EEDATA1		0x28
#define RV3029C2_E2P_EEDATA2		0x29

/* eeprom control section */
#define RV3029C2_CONTROL_E2P_EECTRL	0x30
#define RV3029C2_TRICKLE_1K		(1<<0)  /*  1K resistance */
#define RV3029C2_TRICKLE_5K		(1<<1)  /*  5K resistance */
#define RV3029C2_TRICKLE_20K		(1<<2)  /* 20K resistance */
#define RV3029C2_TRICKLE_80K		(1<<3)  /* 80K resistance */
#define RV3029C2_CONTROL_E2P_XTALOFFSET	0x31
#define RV3029C2_CONTROL_E2P_QCOEF	0x32
#define RV3029C2_CONTROL_E2P_TURNOVER	0x33

/* user ram section */
#define RV3029C2_USR1_RAM_PAGE		0x38
#define RV3029C2_USR1_SECTION_LEN	0x04
#define RV3029C2_USR2_RAM_PAGE		0x3C
#define RV3029C2_USR2_SECTION_LEN	0x04

static int
rv3029c2_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
	unsigned len)
{
	int ret;

	if ((reg > RV3029C2_USR1_RAM_PAGE + 7) ||
		(reg + len > RV3029C2_USR1_RAM_PAGE + 8))
		return -EINVAL;

	ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
	if (ret < 0)
		return ret;
	if (ret < len)
		return -EIO;
	return 0;
}

static int
rv3029c2_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[],
			unsigned len)
{
	if ((reg > RV3029C2_USR1_RAM_PAGE + 7) ||
		(reg + len > RV3029C2_USR1_RAM_PAGE + 8))
		return -EINVAL;

	return i2c_smbus_write_i2c_block_data(client, reg, len, buf);
}

static int
rv3029c2_i2c_get_sr(struct i2c_client *client, u8 *buf)
{
	int ret = rv3029c2_i2c_read_regs(client, RV3029C2_STATUS, buf, 1);

	if (ret < 0)
		return -EIO;
	dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
	return 0;
}

static int
rv3029c2_i2c_set_sr(struct i2c_client *client, u8 val)
{
	u8 buf[1];
	int sr;

	buf[0] = val;
	sr = rv3029c2_i2c_write_regs(client, RV3029C2_STATUS, buf, 1);
	dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
	if (sr < 0)
		return -EIO;
	return 0;
}

static int
rv3029c2_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
{
	u8 buf[1];
	int ret;
	u8 regs[RV3029C2_WATCH_SECTION_LEN] = { 0, };

	ret = rv3029c2_i2c_get_sr(client, buf);
	if (ret < 0) {
		dev_err(&client->dev, "%s: reading SR failed\n", __func__);
		return -EIO;
	}

	ret = rv3029c2_i2c_read_regs(client, RV3029C2_W_SEC , regs,
					RV3029C2_WATCH_SECTION_LEN);
	if (ret < 0) {
		dev_err(&client->dev, "%s: reading RTC section failed\n",
			__func__);
		return ret;
	}

	tm->tm_sec = bcd2bin(regs[RV3029C2_W_SEC-RV3029C2_W_SEC]);
	tm->tm_min = bcd2bin(regs[RV3029C2_W_MINUTES-RV3029C2_W_SEC]);

	/* HR field has a more complex interpretation */
	{
		const u8 _hr = regs[RV3029C2_W_HOURS-RV3029C2_W_SEC];
		if (_hr & RV3029C2_REG_HR_12_24) {
			/* 12h format */
			tm->tm_hour = bcd2bin(_hr & 0x1f);
			if (_hr & RV3029C2_REG_HR_PM)	/* PM flag set */
				tm->tm_hour += 12;
		} else /* 24h format */
			tm->tm_hour = bcd2bin(_hr & 0x3f);
	}

	tm->tm_mday = bcd2bin(regs[RV3029C2_W_DATE-RV3029C2_W_SEC]);
	tm->tm_mon = bcd2bin(regs[RV3029C2_W_MONTHS-RV3029C2_W_SEC]) - 1;
	tm->tm_year = bcd2bin(regs[RV3029C2_W_YEARS-RV3029C2_W_SEC]) + 100;
	tm->tm_wday = bcd2bin(regs[RV3029C2_W_DAYS-RV3029C2_W_SEC]) - 1;

	return 0;
}