2  *  linux/drivers/acorn/char/pcf8583.c
 
   4  *  Copyright (C) 2000 Russell King
 
   6  * This program is free software; you can redistribute it and/or modify
 
   7  * it under the terms of the GNU General Public License version 2 as
 
   8  * published by the Free Software Foundation.
 
  10  *  Driver for PCF8583 RTC & RAM chip
 
  12 #include <linux/module.h>
 
  13 #include <linux/i2c.h>
 
  14 #include <linux/slab.h>
 
  15 #include <linux/string.h>
 
  16 #include <linux/mc146818rtc.h>
 
  17 #include <linux/init.h>
 
  18 #include <linux/errno.h>
 
  19 #include <linux/bcd.h>
 
  23 static struct i2c_driver pcf8583_driver;
 
  25 static unsigned short ignore[] = { I2C_CLIENT_END };
 
  26 static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END };
 
  27 static unsigned short *forces[] = { NULL };
 
  29 static struct i2c_client_address_data addr_data = {
 
  30         .normal_i2c             = normal_addr,
 
  36 #define set_ctrl(x, v) i2c_set_clientdata(x, (void *)(unsigned int)(v))
 
  37 #define get_ctrl(x)    ((unsigned int)i2c_get_clientdata(x))
 
  40 pcf8583_attach(struct i2c_adapter *adap, int addr, int kind)
 
  43         unsigned char buf[1], ad[1] = { 0 };
 
  44         struct i2c_msg msgs[2] = {
 
  58         c = kmalloc(sizeof(*c), GFP_KERNEL);
 
  62         memset(c, 0, sizeof(*c));
 
  65         c->driver       = &pcf8583_driver;
 
  67         if (i2c_transfer(c->adapter, msgs, 2) == 2)
 
  70         return i2c_attach_client(c);
 
  74 pcf8583_probe(struct i2c_adapter *adap)
 
  76         return i2c_probe(adap, &addr_data, pcf8583_attach);
 
  80 pcf8583_detach(struct i2c_client *client)
 
  82         i2c_detach_client(client);
 
  88 pcf8583_get_datetime(struct i2c_client *client, struct rtc_tm *dt)
 
  90         unsigned char buf[8], addr[1] = { 1 };
 
  91         struct i2c_msg msgs[2] = {
 
 106         memset(buf, 0, sizeof(buf));
 
 108         ret = i2c_transfer(client->adapter, msgs, 2);
 
 110                 dt->year_off = buf[4] >> 6;
 
 111                 dt->wday     = buf[5] >> 5;
 
 116                 dt->cs       = BCD_TO_BIN(buf[0]);
 
 117                 dt->secs     = BCD_TO_BIN(buf[1]);
 
 118                 dt->mins     = BCD_TO_BIN(buf[2]);
 
 119                 dt->hours    = BCD_TO_BIN(buf[3]);
 
 120                 dt->mday     = BCD_TO_BIN(buf[4]);
 
 121                 dt->mon      = BCD_TO_BIN(buf[5]);
 
 130 pcf8583_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo)
 
 132         unsigned char buf[8];
 
 136         buf[1] = get_ctrl(client) | 0x80;
 
 137         buf[2] = BIN_TO_BCD(dt->cs);
 
 138         buf[3] = BIN_TO_BCD(dt->secs);
 
 139         buf[4] = BIN_TO_BCD(dt->mins);
 
 140         buf[5] = BIN_TO_BCD(dt->hours);
 
 144                 buf[6] = BIN_TO_BCD(dt->mday) | (dt->year_off << 6);
 
 145                 buf[7] = BIN_TO_BCD(dt->mon)  | (dt->wday << 5);
 
 148         ret = i2c_master_send(client, (char *)buf, len);
 
 152         buf[1] = get_ctrl(client);
 
 153         i2c_master_send(client, (char *)buf, 2);
 
 159 pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl)
 
 161         *ctrl = get_ctrl(client);
 
 166 pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl)
 
 168         unsigned char buf[2];
 
 172         set_ctrl(client, *ctrl);
 
 174         return i2c_master_send(client, (char *)buf, 2);
 
 178 pcf8583_read_mem(struct i2c_client *client, struct mem *mem)
 
 180         unsigned char addr[1];
 
 181         struct i2c_msg msgs[2] = {
 
 183                         .addr = client->addr,
 
 188                         .addr = client->addr,
 
 200         return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
 
 204 pcf8583_write_mem(struct i2c_client *client, struct mem *mem)
 
 206         unsigned char addr[1];
 
 207         struct i2c_msg msgs[2] = {
 
 209                         .addr = client->addr,
 
 214                         .addr = client->addr,
 
 215                         .flags = I2C_M_NOSTART,
 
 226         return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO;
 
 230 pcf8583_command(struct i2c_client *client, unsigned int cmd, void *arg)
 
 233         case RTC_GETDATETIME:
 
 234                 return pcf8583_get_datetime(client, arg);
 
 237                 return pcf8583_set_datetime(client, arg, 0);
 
 239         case RTC_SETDATETIME:
 
 240                 return pcf8583_set_datetime(client, arg, 1);
 
 243                 return pcf8583_get_ctrl(client, arg);
 
 246                 return pcf8583_set_ctrl(client, arg);
 
 249                 return pcf8583_read_mem(client, arg);
 
 252                 return pcf8583_write_mem(client, arg);
 
 259 static struct i2c_driver pcf8583_driver = {
 
 261         .id             = I2C_DRIVERID_PCF8583,
 
 262         .flags          = I2C_DF_NOTIFY,
 
 263         .attach_adapter = pcf8583_probe,
 
 264         .detach_client  = pcf8583_detach,
 
 265         .command        = pcf8583_command
 
 268 static __init int pcf8583_init(void)
 
 270         return i2c_add_driver(&pcf8583_driver);
 
 273 static __exit void pcf8583_exit(void)
 
 275         i2c_del_driver(&pcf8583_driver);
 
 278 module_init(pcf8583_init);
 
 279 module_exit(pcf8583_exit);
 
 281 MODULE_AUTHOR("Russell King");
 
 282 MODULE_DESCRIPTION("PCF8583 I2C RTC driver");
 
 283 MODULE_LICENSE("GPL");