Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.12 239 lines 4.9 kB view raw
1/* 2 * linux/drivers/acorn/char/pcf8583.c 3 * 4 * Copyright (C) 2000 Russell King 5 * 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. 9 * 10 * Driver for PCF8583 RTC & RAM chip 11 */ 12#include <linux/i2c.h> 13#include <linux/slab.h> 14#include <linux/string.h> 15#include <linux/mc146818rtc.h> 16#include <linux/init.h> 17#include <linux/errno.h> 18#include <linux/bcd.h> 19 20#include "pcf8583.h" 21 22static struct i2c_driver pcf8583_driver; 23 24static unsigned short ignore[] = { I2C_CLIENT_END }; 25static unsigned short normal_addr[] = { 0x50, I2C_CLIENT_END }; 26 27static struct i2c_client_address_data addr_data = { 28 .normal_i2c = normal_addr, 29 .normal_i2c_range = ignore, 30 .probe = ignore, 31 .probe_range = ignore, 32 .ignore = ignore, 33 .ignore_range = ignore, 34 .force = ignore, 35}; 36 37#define DAT(x) ((unsigned int)(x->dev.driver_data)) 38 39static int 40pcf8583_attach(struct i2c_adapter *adap, int addr, int kind) 41{ 42 struct i2c_client *c; 43 unsigned char buf[1], ad[1] = { 0 }; 44 struct i2c_msg msgs[2] = { 45 { addr, 0, 1, ad }, 46 { addr, I2C_M_RD, 1, buf } 47 }; 48 49 c = kmalloc(sizeof(*c), GFP_KERNEL); 50 if (!c) 51 return -ENOMEM; 52 53 memset(c, 0, sizeof(*c)); 54 c->addr = addr; 55 c->adapter = adap; 56 c->driver = &pcf8583_driver; 57 58 if (i2c_transfer(c->adapter, msgs, 2) == 2) 59 DAT(c) = buf[0]; 60 61 return i2c_attach_client(c); 62} 63 64static int 65pcf8583_probe(struct i2c_adapter *adap) 66{ 67 return i2c_probe(adap, &addr_data, pcf8583_attach); 68} 69 70static int 71pcf8583_detach(struct i2c_client *client) 72{ 73 i2c_detach_client(client); 74 kfree(client); 75 return 0; 76} 77 78static int 79pcf8583_get_datetime(struct i2c_client *client, struct rtc_tm *dt) 80{ 81 unsigned char buf[8], addr[1] = { 1 }; 82 struct i2c_msg msgs[2] = { 83 { client->addr, 0, 1, addr }, 84 { client->addr, I2C_M_RD, 6, buf } 85 }; 86 int ret = -EIO; 87 88 memset(buf, 0, sizeof(buf)); 89 90 ret = i2c_transfer(client->adapter, msgs, 2); 91 if (ret == 2) { 92 dt->year_off = buf[4] >> 6; 93 dt->wday = buf[5] >> 5; 94 95 buf[4] &= 0x3f; 96 buf[5] &= 0x1f; 97 98 dt->cs = BCD_TO_BIN(buf[0]); 99 dt->secs = BCD_TO_BIN(buf[1]); 100 dt->mins = BCD_TO_BIN(buf[2]); 101 dt->hours = BCD_TO_BIN(buf[3]); 102 dt->mday = BCD_TO_BIN(buf[4]); 103 dt->mon = BCD_TO_BIN(buf[5]); 104 105 ret = 0; 106 } 107 108 return ret; 109} 110 111static int 112pcf8583_set_datetime(struct i2c_client *client, struct rtc_tm *dt, int datetoo) 113{ 114 unsigned char buf[8]; 115 int ret, len = 6; 116 117 buf[0] = 0; 118 buf[1] = DAT(client) | 0x80; 119 buf[2] = BIN_TO_BCD(dt->cs); 120 buf[3] = BIN_TO_BCD(dt->secs); 121 buf[4] = BIN_TO_BCD(dt->mins); 122 buf[5] = BIN_TO_BCD(dt->hours); 123 124 if (datetoo) { 125 len = 8; 126 buf[6] = BIN_TO_BCD(dt->mday) | (dt->year_off << 6); 127 buf[7] = BIN_TO_BCD(dt->mon) | (dt->wday << 5); 128 } 129 130 ret = i2c_master_send(client, (char *)buf, len); 131 if (ret == len) 132 ret = 0; 133 134 buf[1] = DAT(client); 135 i2c_master_send(client, (char *)buf, 2); 136 137 return ret; 138} 139 140static int 141pcf8583_get_ctrl(struct i2c_client *client, unsigned char *ctrl) 142{ 143 *ctrl = DAT(client); 144 return 0; 145} 146 147static int 148pcf8583_set_ctrl(struct i2c_client *client, unsigned char *ctrl) 149{ 150 unsigned char buf[2]; 151 152 buf[0] = 0; 153 buf[1] = *ctrl; 154 DAT(client) = *ctrl; 155 156 return i2c_master_send(client, (char *)buf, 2); 157} 158 159static int 160pcf8583_read_mem(struct i2c_client *client, struct mem *mem) 161{ 162 unsigned char addr[1]; 163 struct i2c_msg msgs[2] = { 164 { client->addr, 0, 1, addr }, 165 { client->addr, I2C_M_RD, 0, mem->data } 166 }; 167 168 if (mem->loc < 8) 169 return -EINVAL; 170 171 addr[0] = mem->loc; 172 msgs[1].len = mem->nr; 173 174 return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO; 175} 176 177static int 178pcf8583_write_mem(struct i2c_client *client, struct mem *mem) 179{ 180 unsigned char addr[1]; 181 struct i2c_msg msgs[2] = { 182 { client->addr, 0, 1, addr }, 183 { client->addr, 0, 0, mem->data } 184 }; 185 186 if (mem->loc < 8) 187 return -EINVAL; 188 189 addr[0] = mem->loc; 190 msgs[1].len = mem->nr; 191 192 return i2c_transfer(client->adapter, msgs, 2) == 2 ? 0 : -EIO; 193} 194 195static int 196pcf8583_command(struct i2c_client *client, unsigned int cmd, void *arg) 197{ 198 switch (cmd) { 199 case RTC_GETDATETIME: 200 return pcf8583_get_datetime(client, arg); 201 202 case RTC_SETTIME: 203 return pcf8583_set_datetime(client, arg, 0); 204 205 case RTC_SETDATETIME: 206 return pcf8583_set_datetime(client, arg, 1); 207 208 case RTC_GETCTRL: 209 return pcf8583_get_ctrl(client, arg); 210 211 case RTC_SETCTRL: 212 return pcf8583_set_ctrl(client, arg); 213 214 case MEM_READ: 215 return pcf8583_read_mem(client, arg); 216 217 case MEM_WRITE: 218 return pcf8583_write_mem(client, arg); 219 220 default: 221 return -EINVAL; 222 } 223} 224 225static struct i2c_driver pcf8583_driver = { 226 .name = "PCF8583", 227 .id = I2C_DRIVERID_PCF8583, 228 .flags = I2C_DF_NOTIFY, 229 .attach_adapter = pcf8583_probe, 230 .detach_client = pcf8583_detach, 231 .command = pcf8583_command 232}; 233 234static __init int pcf8583_init(void) 235{ 236 return i2c_add_driver(&pcf8583_driver); 237} 238 239__initcall(pcf8583_init);