diff options
Diffstat (limited to 'drivers/rtc/rtc-ds3232.c')
| -rw-r--r-- | drivers/rtc/rtc-ds3232.c | 172 | 
1 files changed, 90 insertions, 82 deletions
diff --git a/drivers/rtc/rtc-ds3232.c b/drivers/rtc/rtc-ds3232.c index 57063552d3b..adaf06c4147 100644 --- a/drivers/rtc/rtc-ds3232.c +++ b/drivers/rtc/rtc-ds3232.c @@ -1,7 +1,7 @@  /*   * RTC client/driver for the Maxim/Dallas DS3232 Real-Time Clock over I2C   * - * Copyright (C) 2009-2010 Freescale Semiconductor. + * Copyright (C) 2009-2011 Freescale Semiconductor.   * Author: Jack Lan <jack.lan@freescale.com>   *   * This program is free software; you can redistribute  it and/or modify it @@ -57,6 +57,7 @@ struct ds3232 {  	 * in the remove function.  	 */  	struct mutex mutex; +	bool suspended;  	int exiting;  }; @@ -141,9 +142,11 @@ static int ds3232_read_time(struct device *dev, struct rtc_time *time)  		time->tm_hour = bcd2bin(hour);  	} -	time->tm_wday = bcd2bin(week); +	/* Day of the week in linux range is 0~6 while 1~7 in RTC chip */ +	time->tm_wday = bcd2bin(week) - 1;  	time->tm_mday = bcd2bin(day); -	time->tm_mon = bcd2bin(month & 0x7F); +	/* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */ +	time->tm_mon = bcd2bin(month & 0x7F) - 1;  	if (century)  		add_century = 100; @@ -162,9 +165,11 @@ static int ds3232_set_time(struct device *dev, struct rtc_time *time)  	buf[0] = bin2bcd(time->tm_sec);  	buf[1] = bin2bcd(time->tm_min);  	buf[2] = bin2bcd(time->tm_hour); -	buf[3] = bin2bcd(time->tm_wday); /* Day of the week */ +	/* Day of the week in linux range is 0~6 while 1~7 in RTC chip */ +	buf[3] = bin2bcd(time->tm_wday + 1);  	buf[4] = bin2bcd(time->tm_mday); /* Date */ -	buf[5] = bin2bcd(time->tm_mon); +	/* linux tm_mon range:0~11, while month range is 1~12 in RTC chip */ +	buf[5] = bin2bcd(time->tm_mon + 1);  	if (time->tm_year >= 100) {  		buf[5] |= 0x80;  		buf[6] = bin2bcd(time->tm_year - 100); @@ -335,30 +340,21 @@ static int ds3232_alarm_irq_enable(struct device *dev, unsigned int enabled)  	return 0;  } -static int ds3232_update_irq_enable(struct device *dev, unsigned int enabled) -{ -	struct i2c_client *client = to_i2c_client(dev); -	struct ds3232 *ds3232 = i2c_get_clientdata(client); - -	if (client->irq <= 0) -		return -EINVAL; - -	if (enabled) -		ds3232->rtc->irq_data |= RTC_UF; -	else -		ds3232->rtc->irq_data &= ~RTC_UF; - -	ds3232_update_alarm(client); -	return 0; -} -  static irqreturn_t ds3232_irq(int irq, void *dev_id)  {  	struct i2c_client *client = dev_id;  	struct ds3232 *ds3232 = i2c_get_clientdata(client);  	disable_irq_nosync(irq); -	schedule_work(&ds3232->work); + +	/* +	 * If rtc as a wakeup source, can't schedule the work +	 * at system resume flow, because at this time the i2c bus +	 * has not been resumed. +	 */ +	if (!ds3232->suspended) +		schedule_work(&ds3232->work); +  	return IRQ_HANDLED;  } @@ -376,22 +372,26 @@ static void ds3232_work(struct work_struct *work)  	if (stat & DS3232_REG_SR_A1F) {  		control = i2c_smbus_read_byte_data(client, DS3232_REG_CR); -		if (control < 0) -			goto out; -		/* disable alarm1 interrupt */ -		control &= ~(DS3232_REG_CR_A1IE); -		i2c_smbus_write_byte_data(client, DS3232_REG_CR, control); - -		/* clear the alarm pend flag */ -		stat &= ~DS3232_REG_SR_A1F; -		i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat); - -		rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF); +		if (control < 0) { +			pr_warn("Read DS3232 Control Register error." +				"Disable IRQ%d.\n", client->irq); +		} else { +			/* disable alarm1 interrupt */ +			control &= ~(DS3232_REG_CR_A1IE); +			i2c_smbus_write_byte_data(client, DS3232_REG_CR, +						control); + +			/* clear the alarm pend flag */ +			stat &= ~DS3232_REG_SR_A1F; +			i2c_smbus_write_byte_data(client, DS3232_REG_SR, stat); + +			rtc_update_irq(ds3232->rtc, 1, RTC_AF | RTC_IRQF); + +			if (!ds3232->exiting) +				enable_irq(client->irq); +		}  	} -out: -	if (!ds3232->exiting) -		enable_irq(client->irq);  unlock:  	mutex_unlock(&ds3232->mutex);  } @@ -402,16 +402,15 @@ static const struct rtc_class_ops ds3232_rtc_ops = {  	.read_alarm = ds3232_read_alarm,  	.set_alarm = ds3232_set_alarm,  	.alarm_irq_enable = ds3232_alarm_irq_enable, -	.update_irq_enable = ds3232_update_irq_enable,  }; -static int __devinit ds3232_probe(struct i2c_client *client, -		const struct i2c_device_id *id) +static int ds3232_probe(struct i2c_client *client, +			const struct i2c_device_id *id)  {  	struct ds3232 *ds3232;  	int ret; -	ds3232 = kzalloc(sizeof(struct ds3232), GFP_KERNEL); +	ds3232 = devm_kzalloc(&client->dev, sizeof(struct ds3232), GFP_KERNEL);  	if (!ds3232)  		return -ENOMEM; @@ -423,37 +422,22 @@ static int __devinit ds3232_probe(struct i2c_client *client,  	ret = ds3232_check_rtc_status(client);  	if (ret) -		goto out_free; - -	ds3232->rtc = rtc_device_register(client->name, &client->dev, -					  &ds3232_rtc_ops, THIS_MODULE); -	if (IS_ERR(ds3232->rtc)) { -		ret = PTR_ERR(ds3232->rtc); -		dev_err(&client->dev, "unable to register the class device\n"); -		goto out_irq; -	} +		return ret; -	if (client->irq >= 0) { -		ret = request_irq(client->irq, ds3232_irq, 0, -				 "ds3232", client); +	if (client->irq > 0) { +		ret = devm_request_irq(&client->dev, client->irq, ds3232_irq, +				       IRQF_SHARED, "ds3232", client);  		if (ret) {  			dev_err(&client->dev, "unable to request IRQ\n"); -			goto out_free;  		} +		device_init_wakeup(&client->dev, 1);  	} - -	return 0; - -out_irq: -	if (client->irq >= 0) -		free_irq(client->irq, client); - -out_free: -	kfree(ds3232); -	return ret; +	ds3232->rtc = devm_rtc_device_register(&client->dev, client->name, +					  &ds3232_rtc_ops, THIS_MODULE); +	return PTR_ERR_OR_ZERO(ds3232->rtc);  } -static int __devexit ds3232_remove(struct i2c_client *client) +static int ds3232_remove(struct i2c_client *client)  {  	struct ds3232 *ds3232 = i2c_get_clientdata(client); @@ -462,14 +446,48 @@ static int __devexit ds3232_remove(struct i2c_client *client)  		ds3232->exiting = 1;  		mutex_unlock(&ds3232->mutex); -		free_irq(client->irq, client); -		flush_scheduled_work(); +		devm_free_irq(&client->dev, client->irq, client); +		cancel_work_sync(&ds3232->work); +	} + +	return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int ds3232_suspend(struct device *dev) +{ +	struct ds3232 *ds3232 = dev_get_drvdata(dev); +	struct i2c_client *client = to_i2c_client(dev); + +	if (device_can_wakeup(dev)) { +		ds3232->suspended = true; +		irq_set_irq_wake(client->irq, 1); +	} + +	return 0; +} + +static int ds3232_resume(struct device *dev) +{ +	struct ds3232 *ds3232 = dev_get_drvdata(dev); +	struct i2c_client *client = to_i2c_client(dev); + +	if (ds3232->suspended) { +		ds3232->suspended = false; + +		/* Clear the hardware alarm pend flag */ +		schedule_work(&ds3232->work); + +		irq_set_irq_wake(client->irq, 0);  	} -	rtc_device_unregister(ds3232->rtc); -	kfree(ds3232);  	return 0;  } +#endif + +static const struct dev_pm_ops ds3232_pm_ops = { +	SET_SYSTEM_SLEEP_PM_OPS(ds3232_suspend, ds3232_resume) +};  static const struct i2c_device_id ds3232_id[] = {  	{ "ds3232", 0 }, @@ -481,24 +499,14 @@ static struct i2c_driver ds3232_driver = {  	.driver = {  		.name = "rtc-ds3232",  		.owner = THIS_MODULE, +		.pm	= &ds3232_pm_ops,  	},  	.probe = ds3232_probe, -	.remove = __devexit_p(ds3232_remove), +	.remove = ds3232_remove,  	.id_table = ds3232_id,  }; -static int __init ds3232_init(void) -{ -	return i2c_add_driver(&ds3232_driver); -} - -static void __exit ds3232_exit(void) -{ -	i2c_del_driver(&ds3232_driver); -} - -module_init(ds3232_init); -module_exit(ds3232_exit); +module_i2c_driver(ds3232_driver);  MODULE_AUTHOR("Srikanth Srinivasan <srikanth.srinivasan@freescale.com>");  MODULE_DESCRIPTION("Maxim/Dallas DS3232 RTC Driver");  | 
