Commit 9edae7bc authored by Alessandro Zummo's avatar Alessandro Zummo Committed by Linus Torvalds

rtc-isl1208: new style conversion and minor bug fixes

[akpm@linux-foundation.org: coding-style fixes]
Signed-off-by: default avatarAlessandro Zummo <a.zummo@towertech.it>
Cc: Herbert Valerio Riedel <hvr@gnu.org>
Cc: David Brownell <david-b@pacbell.net>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent c7500900
...@@ -15,16 +15,15 @@ ...@@ -15,16 +15,15 @@
#include <linux/bcd.h> #include <linux/bcd.h>
#include <linux/rtc.h> #include <linux/rtc.h>
#define DRV_NAME "isl1208" #define DRV_VERSION "0.3"
#define DRV_VERSION "0.2"
/* Register map */ /* Register map */
/* rtc section */ /* rtc section */
#define ISL1208_REG_SC 0x00 #define ISL1208_REG_SC 0x00
#define ISL1208_REG_MN 0x01 #define ISL1208_REG_MN 0x01
#define ISL1208_REG_HR 0x02 #define ISL1208_REG_HR 0x02
#define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */ #define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */
#define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */ #define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
#define ISL1208_REG_DT 0x03 #define ISL1208_REG_DT 0x03
#define ISL1208_REG_MO 0x04 #define ISL1208_REG_MO 0x04
#define ISL1208_REG_YR 0x05 #define ISL1208_REG_YR 0x05
...@@ -33,14 +32,14 @@ ...@@ -33,14 +32,14 @@
/* control/status section */ /* control/status section */
#define ISL1208_REG_SR 0x07 #define ISL1208_REG_SR 0x07
#define ISL1208_REG_SR_ARST (1<<7) /* auto reset */ #define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
#define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */ #define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
#define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */ #define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
#define ISL1208_REG_SR_ALM (1<<2) /* alarm */ #define ISL1208_REG_SR_ALM (1<<2) /* alarm */
#define ISL1208_REG_SR_BAT (1<<1) /* battery */ #define ISL1208_REG_SR_BAT (1<<1) /* battery */
#define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */ #define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
#define ISL1208_REG_INT 0x08 #define ISL1208_REG_INT 0x08
#define ISL1208_REG_09 0x09 /* reserved */ #define ISL1208_REG_09 0x09 /* reserved */
#define ISL1208_REG_ATR 0x0a #define ISL1208_REG_ATR 0x0a
#define ISL1208_REG_DTR 0x0b #define ISL1208_REG_DTR 0x0b
...@@ -58,39 +57,21 @@ ...@@ -58,39 +57,21 @@
#define ISL1208_REG_USR2 0x13 #define ISL1208_REG_USR2 0x13
#define ISL1208_USR_SECTION_LEN 2 #define ISL1208_USR_SECTION_LEN 2
/* i2c configuration */ static struct i2c_driver isl1208_driver;
#define ISL1208_I2C_ADDR 0xde
static const unsigned short normal_i2c[] = {
ISL1208_I2C_ADDR>>1, I2C_CLIENT_END
};
I2C_CLIENT_INSMOD; /* defines addr_data */
static int isl1208_attach_adapter(struct i2c_adapter *adapter);
static int isl1208_detach_client(struct i2c_client *client);
static struct i2c_driver isl1208_driver = {
.driver = {
.name = DRV_NAME,
},
.id = I2C_DRIVERID_ISL1208,
.attach_adapter = &isl1208_attach_adapter,
.detach_client = &isl1208_detach_client,
};
/* block read */ /* block read */
static int static int
isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[], isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
unsigned len) unsigned len)
{ {
u8 reg_addr[1] = { reg }; u8 reg_addr[1] = { reg };
struct i2c_msg msgs[2] = { struct i2c_msg msgs[2] = {
{ client->addr, client->flags, sizeof(reg_addr), reg_addr }, {client->addr, 0, sizeof(reg_addr), reg_addr}
{ client->addr, client->flags | I2C_M_RD, len, buf } ,
{client->addr, I2C_M_RD, len, buf}
}; };
int ret; int ret;
BUG_ON(len == 0);
BUG_ON(reg > ISL1208_REG_USR2); BUG_ON(reg > ISL1208_REG_USR2);
BUG_ON(reg + len > ISL1208_REG_USR2 + 1); BUG_ON(reg + len > ISL1208_REG_USR2 + 1);
...@@ -103,15 +84,14 @@ isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[], ...@@ -103,15 +84,14 @@ isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
/* block write */ /* block write */
static int static int
isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[], isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
unsigned len) unsigned len)
{ {
u8 i2c_buf[ISL1208_REG_USR2 + 2]; u8 i2c_buf[ISL1208_REG_USR2 + 2];
struct i2c_msg msgs[1] = { struct i2c_msg msgs[1] = {
{ client->addr, client->flags, len + 1, i2c_buf } {client->addr, 0, len + 1, i2c_buf}
}; };
int ret; int ret;
BUG_ON(len == 0);
BUG_ON(reg > ISL1208_REG_USR2); BUG_ON(reg > ISL1208_REG_USR2);
BUG_ON(reg + len > ISL1208_REG_USR2 + 1); BUG_ON(reg + len > ISL1208_REG_USR2 + 1);
...@@ -125,7 +105,8 @@ isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[], ...@@ -125,7 +105,8 @@ isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
} }
/* simple check to see wether we have a isl1208 */ /* simple check to see wether we have a isl1208 */
static int isl1208_i2c_validate_client(struct i2c_client *client) static int
isl1208_i2c_validate_client(struct i2c_client *client)
{ {
u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, }; u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
u8 zero_mask[ISL1208_RTC_SECTION_LEN] = { u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
...@@ -139,24 +120,29 @@ static int isl1208_i2c_validate_client(struct i2c_client *client) ...@@ -139,24 +120,29 @@ static int isl1208_i2c_validate_client(struct i2c_client *client)
return ret; return ret;
for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) { for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
if (regs[i] & zero_mask[i]) /* check if bits are cleared */ if (regs[i] & zero_mask[i]) /* check if bits are cleared */
return -ENODEV; return -ENODEV;
} }
return 0; return 0;
} }
static int isl1208_i2c_get_sr(struct i2c_client *client) static int
isl1208_i2c_get_sr(struct i2c_client *client)
{ {
return i2c_smbus_read_byte_data(client, ISL1208_REG_SR) == -1 ? -EIO:0; int sr = i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
if (sr < 0)
return -EIO;
return sr;
} }
static int isl1208_i2c_get_atr(struct i2c_client *client) static int
isl1208_i2c_get_atr(struct i2c_client *client)
{ {
int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR); int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
if (atr < 0) if (atr < 0)
return -EIO; return atr;
/* The 6bit value in the ATR register controls the load /* The 6bit value in the ATR register controls the load
* capacitance C_load * in steps of 0.25pF * capacitance C_load * in steps of 0.25pF
...@@ -169,51 +155,54 @@ static int isl1208_i2c_get_atr(struct i2c_client *client) ...@@ -169,51 +155,54 @@ static int isl1208_i2c_get_atr(struct i2c_client *client)
* *
*/ */
atr &= 0x3f; /* mask out lsb */ atr &= 0x3f; /* mask out lsb */
atr ^= 1<<5; /* invert 6th bit */ atr ^= 1 << 5; /* invert 6th bit */
atr += 2*9; /* add offset of 4.5pF; unit[atr] = 0.25pF */ atr += 2 * 9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
return atr; return atr;
} }
static int isl1208_i2c_get_dtr(struct i2c_client *client) static int
isl1208_i2c_get_dtr(struct i2c_client *client)
{ {
int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR); int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
if (dtr < 0) if (dtr < 0)
return -EIO; return -EIO;
/* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */ /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
dtr = ((dtr & 0x3) * 20) * (dtr & (1<<2) ? -1 : 1); dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
return dtr; return dtr;
} }
static int isl1208_i2c_get_usr(struct i2c_client *client) static int
isl1208_i2c_get_usr(struct i2c_client *client)
{ {
u8 buf[ISL1208_USR_SECTION_LEN] = { 0, }; u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
int ret; int ret;
ret = isl1208_i2c_read_regs (client, ISL1208_REG_USR1, buf, ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
ISL1208_USR_SECTION_LEN); ISL1208_USR_SECTION_LEN);
if (ret < 0) if (ret < 0)
return ret; return ret;
return (buf[1] << 8) | buf[0]; return (buf[1] << 8) | buf[0];
} }
static int isl1208_i2c_set_usr(struct i2c_client *client, u16 usr) static int
isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
{ {
u8 buf[ISL1208_USR_SECTION_LEN]; u8 buf[ISL1208_USR_SECTION_LEN];
buf[0] = usr & 0xff; buf[0] = usr & 0xff;
buf[1] = (usr >> 8) & 0xff; buf[1] = (usr >> 8) & 0xff;
return isl1208_i2c_set_regs (client, ISL1208_REG_USR1, buf, return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
ISL1208_USR_SECTION_LEN); ISL1208_USR_SECTION_LEN);
} }
static int isl1208_rtc_proc(struct device *dev, struct seq_file *seq) static int
isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
{ {
struct i2c_client *const client = to_i2c_client(dev); struct i2c_client *const client = to_i2c_client(dev);
int sr, dtr, atr, usr; int sr, dtr, atr, usr;
...@@ -230,20 +219,19 @@ static int isl1208_rtc_proc(struct device *dev, struct seq_file *seq) ...@@ -230,20 +219,19 @@ static int isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
(sr & ISL1208_REG_SR_ALM) ? " ALM" : "", (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
(sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "", (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
(sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "", (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
(sr & ISL1208_REG_SR_ARST) ? " ARST" : "", (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
sr);
seq_printf(seq, "batt_status\t: %s\n", seq_printf(seq, "batt_status\t: %s\n",
(sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay"); (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
dtr = isl1208_i2c_get_dtr(client); dtr = isl1208_i2c_get_dtr(client);
if (dtr >= 0 -1) if (dtr >= 0 - 1)
seq_printf(seq, "digital_trim\t: %d ppm\n", dtr); seq_printf(seq, "digital_trim\t: %d ppm\n", dtr);
atr = isl1208_i2c_get_atr(client); atr = isl1208_i2c_get_atr(client);
if (atr >= 0) if (atr >= 0)
seq_printf(seq, "analog_trim\t: %d.%.2d pF\n", seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
atr>>2, (atr&0x3)*25); atr >> 2, (atr & 0x3) * 25);
usr = isl1208_i2c_get_usr(client); usr = isl1208_i2c_get_usr(client);
if (usr >= 0) if (usr >= 0)
...@@ -252,9 +240,8 @@ static int isl1208_rtc_proc(struct device *dev, struct seq_file *seq) ...@@ -252,9 +240,8 @@ static int isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
return 0; return 0;
} }
static int
static int isl1208_i2c_read_time(struct i2c_client *client, isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
struct rtc_time *tm)
{ {
int sr; int sr;
u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, }; u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
...@@ -274,27 +261,30 @@ static int isl1208_i2c_read_time(struct i2c_client *client, ...@@ -274,27 +261,30 @@ static int isl1208_i2c_read_time(struct i2c_client *client,
tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SC]); tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SC]);
tm->tm_min = BCD2BIN(regs[ISL1208_REG_MN]); tm->tm_min = BCD2BIN(regs[ISL1208_REG_MN]);
{ /* HR field has a more complex interpretation */
/* HR field has a more complex interpretation */
{
const u8 _hr = regs[ISL1208_REG_HR]; const u8 _hr = regs[ISL1208_REG_HR];
if (_hr & ISL1208_REG_HR_MIL) /* 24h format */ if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
tm->tm_hour = BCD2BIN(_hr & 0x3f); tm->tm_hour = BCD2BIN(_hr & 0x3f);
else { // 12h format else {
/* 12h format */
tm->tm_hour = BCD2BIN(_hr & 0x1f); tm->tm_hour = BCD2BIN(_hr & 0x1f);
if (_hr & ISL1208_REG_HR_PM) /* PM flag set */ if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
tm->tm_hour += 12; tm->tm_hour += 12;
} }
} }
tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DT]); tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DT]);
tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */ tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
tm->tm_year = BCD2BIN(regs[ISL1208_REG_YR]) + 100; tm->tm_year = BCD2BIN(regs[ISL1208_REG_YR]) + 100;
tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DW]); tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DW]);
return 0; return 0;
} }
static int isl1208_i2c_read_alarm(struct i2c_client *client, static int
struct rtc_wkalrm *alarm) isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
{ {
struct rtc_time *const tm = &alarm->time; struct rtc_time *const tm = &alarm->time;
u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, }; u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
...@@ -307,7 +297,7 @@ static int isl1208_i2c_read_alarm(struct i2c_client *client, ...@@ -307,7 +297,7 @@ static int isl1208_i2c_read_alarm(struct i2c_client *client,
} }
sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs, sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
ISL1208_ALARM_SECTION_LEN); ISL1208_ALARM_SECTION_LEN);
if (sr < 0) { if (sr < 0) {
dev_err(&client->dev, "%s: reading alarm section failed\n", dev_err(&client->dev, "%s: reading alarm section failed\n",
__func__); __func__);
...@@ -315,23 +305,25 @@ static int isl1208_i2c_read_alarm(struct i2c_client *client, ...@@ -315,23 +305,25 @@ static int isl1208_i2c_read_alarm(struct i2c_client *client,
} }
/* MSB of each alarm register is an enable bit */ /* MSB of each alarm register is an enable bit */
tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SCA-ISL1208_REG_SCA] & 0x7f); tm->tm_sec = BCD2BIN(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
tm->tm_min = BCD2BIN(regs[ISL1208_REG_MNA-ISL1208_REG_SCA] & 0x7f); tm->tm_min = BCD2BIN(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
tm->tm_hour = BCD2BIN(regs[ISL1208_REG_HRA-ISL1208_REG_SCA] & 0x3f); tm->tm_hour = BCD2BIN(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DTA-ISL1208_REG_SCA] & 0x3f); tm->tm_mday = BCD2BIN(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
tm->tm_mon = BCD2BIN(regs[ISL1208_REG_MOA-ISL1208_REG_SCA] & 0x1f)-1; tm->tm_mon =
tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DWA-ISL1208_REG_SCA] & 0x03); BCD2BIN(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
tm->tm_wday = BCD2BIN(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
return 0; return 0;
} }
static int isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm) static int
isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
{ {
return isl1208_i2c_read_time(to_i2c_client(dev), tm); return isl1208_i2c_read_time(to_i2c_client(dev), tm);
} }
static int isl1208_i2c_set_time(struct i2c_client *client, static int
struct rtc_time const *tm) isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
{ {
int sr; int sr;
u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, }; u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
...@@ -353,7 +345,7 @@ static int isl1208_i2c_set_time(struct i2c_client *client, ...@@ -353,7 +345,7 @@ static int isl1208_i2c_set_time(struct i2c_client *client,
} }
/* set WRTC */ /* set WRTC */
sr = i2c_smbus_write_byte_data (client, ISL1208_REG_SR, sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
sr | ISL1208_REG_SR_WRTC); sr | ISL1208_REG_SR_WRTC);
if (sr < 0) { if (sr < 0) {
dev_err(&client->dev, "%s: writing SR failed\n", __func__); dev_err(&client->dev, "%s: writing SR failed\n", __func__);
...@@ -369,7 +361,7 @@ static int isl1208_i2c_set_time(struct i2c_client *client, ...@@ -369,7 +361,7 @@ static int isl1208_i2c_set_time(struct i2c_client *client,
} }
/* clear WRTC again */ /* clear WRTC again */
sr = i2c_smbus_write_byte_data (client, ISL1208_REG_SR, sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
sr & ~ISL1208_REG_SR_WRTC); sr & ~ISL1208_REG_SR_WRTC);
if (sr < 0) { if (sr < 0) {
dev_err(&client->dev, "%s: writing SR failed\n", __func__); dev_err(&client->dev, "%s: writing SR failed\n", __func__);
...@@ -380,70 +372,69 @@ static int isl1208_i2c_set_time(struct i2c_client *client, ...@@ -380,70 +372,69 @@ static int isl1208_i2c_set_time(struct i2c_client *client,
} }
static int isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm) static int
isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
{ {
return isl1208_i2c_set_time(to_i2c_client(dev), tm); return isl1208_i2c_set_time(to_i2c_client(dev), tm);
} }
static int isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) static int
isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
{ {
return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm); return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
} }
static const struct rtc_class_ops isl1208_rtc_ops = { static const struct rtc_class_ops isl1208_rtc_ops = {
.proc = isl1208_rtc_proc, .proc = isl1208_rtc_proc,
.read_time = isl1208_rtc_read_time, .read_time = isl1208_rtc_read_time,
.set_time = isl1208_rtc_set_time, .set_time = isl1208_rtc_set_time,
.read_alarm = isl1208_rtc_read_alarm, .read_alarm = isl1208_rtc_read_alarm,
//.set_alarm = isl1208_rtc_set_alarm, /*.set_alarm = isl1208_rtc_set_alarm, */
}; };
/* sysfs interface */ /* sysfs interface */
static ssize_t isl1208_sysfs_show_atrim(struct device *dev, static ssize_t
struct device_attribute *attr, isl1208_sysfs_show_atrim(struct device *dev,
char *buf) struct device_attribute *attr, char *buf)
{ {
int atr; int atr = isl1208_i2c_get_atr(to_i2c_client(dev));
atr = isl1208_i2c_get_atr(to_i2c_client(dev));
if (atr < 0) if (atr < 0)
return atr; return atr;
return sprintf(buf, "%d.%.2d pF\n", atr>>2, (atr&0x3)*25); return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
} }
static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL); static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);
static ssize_t isl1208_sysfs_show_dtrim(struct device *dev, static ssize_t
struct device_attribute *attr, isl1208_sysfs_show_dtrim(struct device *dev,
char *buf) struct device_attribute *attr, char *buf)
{ {
int dtr; int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev));
dtr = isl1208_i2c_get_dtr(to_i2c_client(dev));
if (dtr < 0) if (dtr < 0)
return dtr; return dtr;
return sprintf(buf, "%d ppm\n", dtr); return sprintf(buf, "%d ppm\n", dtr);
} }
static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL); static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
static ssize_t isl1208_sysfs_show_usr(struct device *dev, static ssize_t
struct device_attribute *attr, isl1208_sysfs_show_usr(struct device *dev,
char *buf) struct device_attribute *attr, char *buf)
{ {
int usr; int usr = isl1208_i2c_get_usr(to_i2c_client(dev));
usr = isl1208_i2c_get_usr(to_i2c_client(dev));
if (usr < 0) if (usr < 0)
return usr; return usr;
return sprintf(buf, "0x%.4x\n", usr); return sprintf(buf, "0x%.4x\n", usr);
} }
static ssize_t isl1208_sysfs_store_usr(struct device *dev, static ssize_t
struct device_attribute *attr, isl1208_sysfs_store_usr(struct device *dev,
const char *buf, size_t count) struct device_attribute *attr,
const char *buf, size_t count)
{ {
int usr = -1; int usr = -1;
...@@ -460,124 +451,116 @@ static ssize_t isl1208_sysfs_store_usr(struct device *dev, ...@@ -460,124 +451,116 @@ static ssize_t isl1208_sysfs_store_usr(struct device *dev,
return isl1208_i2c_set_usr(to_i2c_client(dev), usr) ? -EIO : count; return isl1208_i2c_set_usr(to_i2c_client(dev), usr) ? -EIO : count;
} }
static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr, static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
isl1208_sysfs_store_usr); isl1208_sysfs_store_usr);
static int static int
isl1208_probe(struct i2c_adapter *adapter, int addr, int kind) isl1208_sysfs_register(struct device *dev)
{ {
int rc = 0; int err;
struct i2c_client *new_client = NULL;
struct rtc_device *rtc = NULL; err = device_create_file(dev, &dev_attr_atrim);
if (err)
return err;
if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) { err = device_create_file(dev, &dev_attr_dtrim);
rc = -ENODEV; if (err) {
goto failout; device_remove_file(dev, &dev_attr_atrim);
return err;
} }
new_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); err = device_create_file(dev, &dev_attr_usr);
if (new_client == NULL) { if (err) {
rc = -ENOMEM; device_remove_file(dev, &dev_attr_atrim);
goto failout; device_remove_file(dev, &dev_attr_dtrim);
} }
new_client->addr = addr; return 0;
new_client->adapter = adapter; }
new_client->driver = &isl1208_driver;
new_client->flags = 0;
strcpy(new_client->name, DRV_NAME);
if (kind < 0) { static int
rc = isl1208_i2c_validate_client(new_client); isl1208_sysfs_unregister(struct device *dev)
if (rc < 0) {
goto failout; device_remove_file(dev, &dev_attr_atrim);
} device_remove_file(dev, &dev_attr_atrim);
device_remove_file(dev, &dev_attr_usr);
return 0;
}
static int
isl1208_probe(struct i2c_client *client)
{
int rc = 0;
struct rtc_device *rtc;
rc = i2c_attach_client(new_client); if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
if (rc < 0) return -ENODEV;
goto failout;
dev_info(&new_client->dev, if (isl1208_i2c_validate_client(client) < 0)
return -ENODEV;
dev_info(&client->dev,
"chip found, driver version " DRV_VERSION "\n"); "chip found, driver version " DRV_VERSION "\n");
rtc = rtc_device_register(isl1208_driver.driver.name, rtc = rtc_device_register(isl1208_driver.driver.name,
&new_client->dev, &client->dev, &isl1208_rtc_ops,
&isl1208_rtc_ops, THIS_MODULE); THIS_MODULE);
if (IS_ERR(rtc))
if (IS_ERR(rtc)) { return PTR_ERR(rtc);
rc = PTR_ERR(rtc);
goto failout_detach;
}
i2c_set_clientdata(new_client, rtc); i2c_set_clientdata(client, rtc);
rc = isl1208_i2c_get_sr(new_client); rc = isl1208_i2c_get_sr(client);
if (rc < 0) { if (rc < 0) {
dev_err(&new_client->dev, "reading status failed\n"); dev_err(&client->dev, "reading status failed\n");
goto failout_unregister; goto exit_unregister;
} }
if (rc & ISL1208_REG_SR_RTCF) if (rc & ISL1208_REG_SR_RTCF)
dev_warn(&new_client->dev, "rtc power failure detected, " dev_warn(&client->dev, "rtc power failure detected, "
"please set clock.\n"); "please set clock.\n");
rc = device_create_file(&new_client->dev, &dev_attr_atrim); rc = isl1208_sysfs_register(&client->dev);
if (rc < 0) if (rc)
goto failout_unregister; goto exit_unregister;
rc = device_create_file(&new_client->dev, &dev_attr_dtrim);
if (rc < 0)
goto failout_atrim;
rc = device_create_file(&new_client->dev, &dev_attr_usr);
if (rc < 0)
goto failout_dtrim;
return 0; return 0;
failout_dtrim: exit_unregister:
device_remove_file(&new_client->dev, &dev_attr_dtrim);
failout_atrim:
device_remove_file(&new_client->dev, &dev_attr_atrim);
failout_unregister:
rtc_device_unregister(rtc); rtc_device_unregister(rtc);
failout_detach:
i2c_detach_client(new_client);
failout:
kfree(new_client);
return rc;
}
static int return rc;
isl1208_attach_adapter (struct i2c_adapter *adapter)
{
return i2c_probe(adapter, &addr_data, isl1208_probe);
} }
static int static int
isl1208_detach_client(struct i2c_client *client) isl1208_remove(struct i2c_client *client)
{ {
int rc; struct rtc_device *rtc = i2c_get_clientdata(client);
struct rtc_device *const rtc = i2c_get_clientdata(client);
if (rtc)
rtc_device_unregister(rtc); /* do we need to kfree? */
rc = i2c_detach_client(client);
if (rc)
return rc;
kfree(client); isl1208_sysfs_unregister(&client->dev);
rtc_device_unregister(rtc);
return 0; return 0;
} }
/* module management */ static struct i2c_driver isl1208_driver = {
.driver = {
.name = "rtc-isl1208",
},
.probe = isl1208_probe,
.remove = isl1208_remove,
};
static int __init isl1208_init(void) static int __init
isl1208_init(void)
{ {
return i2c_add_driver(&isl1208_driver); return i2c_add_driver(&isl1208_driver);
} }
static void __exit isl1208_exit(void) static void __exit
isl1208_exit(void)
{ {
i2c_del_driver(&isl1208_driver); i2c_del_driver(&isl1208_driver);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment