⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uda1380-i2c.c

📁 这个源码相信对很多用arm开发板开发的人会有用的
💻 C
字号:
/* *  linux/drivers/i2c/chips/uda1380.c * *  Copyright (C) 2006 yczhao@hhcn.com * * 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. * * Driver for system3's EPSON RTC 8564 chip */#include <linux/module.h>#include <linux/kernel.h>#include <linux/i2c.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/init.h>#include <linux/init.h>#include <linux/delay.h>#define DEBUG#ifdef DEBUG# define _DBG(x, fmt, args...) do{ if (debug>=x) printk(KERN_INFO"%s: " fmt "\n", __FUNCTION__, ##args); } while(0);#else# define _DBG(x, fmt, args...) do { } while(0);#endifstruct uda1380_data {	struct i2c_client client;};struct i2c_client *uda1380_client;#define BCD_TO_BIN(val) (((val)&15) + ((val)>>4)*10)#define BIN_TO_BCD(val) ((((val)/10)<<4) + (val)%10)static int debug;module_param(debug, int, S_IRUGO | S_IWUSR);static struct i2c_driver uda1380_driver;static unsigned short ignore[] = { I2C_CLIENT_END };//static unsigned short force_addr[4] =  {ANY_I2C_BUS, 0x30>>1, I2C_CLIENT_END,I2C_CLIENT_END};static unsigned short force_addr[2][4] = { {ANY_I2C_BUS, 0x30>>1, I2C_CLIENT_END,I2C_CLIENT_END},{I2C_CLIENT_END}};static unsigned short normal_addr[] = { 0x18, I2C_CLIENT_END };//static unsigned short normal_addr[] = { 0x30>>1, I2C_CLIENT_END };static struct i2c_client_address_data addr_data = {	.normal_i2c		= normal_addr,//	.normal_i2c		= ignore,//	.normal_i2c_range	= ignore,	.probe			= ignore,//	.probe_range		= ignore,	.ignore			= ignore,//	.ignore_range		= ignore,//	.forces			= force_addr,};static int uda1380_read(struct i2c_client *client, unsigned char adr,			unsigned char *buf, unsigned char len){	int ret = -EIO;	unsigned char addr[1] = { adr };	struct i2c_msg msgs[2] = {		{client->addr, 0, 1, addr},		{client->addr, I2C_M_RD, len, buf}	};	_DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, buf, len);	if (!buf || !client) {		ret = -EINVAL;		goto done;	}	ret = i2c_transfer(client->adapter, msgs, 2);	if (ret == 2) {		ret = 0;	}done:	return ret;}static int uda1380_write(struct i2c_client *client, unsigned char adr,			 unsigned char *data, unsigned char len){	int ret = 0;	unsigned char _data[16];	struct i2c_msg wr;	int i;	if (!client || !data || len > 15) {		ret = -EINVAL;		goto done;	}	_DBG(1, "client=%p, adr=%d, buf=%p, len=%d", client, adr, data, len);	_data[0] = adr;	for (i = 0; i < len; i++) {		_data[i + 1] = data[i];		_DBG(5, "data[%d] = 0x%02x (%d)", i, data[i], data[i]);	}	wr.addr = client->addr;	wr.flags = 0;	wr.len = len + 1;	wr.buf = _data;	ret = i2c_transfer(client->adapter, &wr, 1);	if (ret == 1) {		ret = 0;	}done:	return ret;}int s3c2410_uda_codec_read(unsigned char adr){	int ret;	unsigned char tmp[2];	ret = uda1380_read(uda1380_client, adr, tmp, 2);	return ret?0xfffe:(((short)tmp[0]<<8) | tmp[1]);  /*a 0xfffe indicate an error*/	}/* * return 0:succeed,else failed * */int s3c2410_uda_codec_write(unsigned char adr, unsigned short value){	int ret;	unsigned char tmp[2];	tmp[0] = (value>>8) & 0xff;	tmp[1] = value & 0xff;	return uda1380_write(uda1380_client, adr, tmp, 2);}static int uda1380_attach(struct i2c_adapter *adap, int addr, int kind){	int ret;	struct uda1380_data *d;	unsigned char data[10];#ifdef DEBUG	unsigned char ad[3] = { 0x1 ,0xff,0xff}; /*regaddr, highbyte, lowbyte*/	struct i2c_msg set_reg[1] = {		{addr, 0, 3, ad} 	};	struct i2c_msg get_reg[2] = {		{addr, 0, 1, ad},		{addr, I2C_M_RD, 2, data}	};#endif	printk("%s:%d\n",__FUNCTION__,__LINE__);	d = kmalloc(sizeof(struct uda1380_data), GFP_KERNEL);	if (!d) {		ret = -ENOMEM;		goto done;	}	memset(d, 0, sizeof(struct uda1380_data));	uda1380_client = &d->client;	strlcpy(uda1380_client->name, "UDA1380", I2C_NAME_SIZE);	i2c_set_clientdata(uda1380_client, d);//	uda1380_client->id = uda1380_driver.id;	uda1380_client->flags = I2C_CLIENT_ALLOW_USE | I2C_DF_NOTIFY;	uda1380_client->addr = addr;	uda1380_client->adapter = adap;	uda1380_client->driver = &uda1380_driver;	_DBG(1, "client=%p", uda1380_client);//	_DBG(1, "client.id=%d", uda1380_client->id);#ifdef DEBUG	data[0] = 0x55;	data[1] = 0xaa;	ret = i2c_transfer(uda1380_client->adapter, set_reg, 1);	if (ret != 1) {		printk(KERN_INFO "uda1380: cant write reg\n");		ret = -ENODEV;		goto done;	}	/* read back ctrl1 and ctrl2 */	ret = i2c_transfer(uda1380_client->adapter, get_reg, 2);	if (ret != 2) {		printk(KERN_INFO "uda1380: cant read reg,ret=%d\n",ret);		ret = -ENODEV;		goto done;	}	printk("register read value = 0x%x, and should be 0x0757\n",data[0]<<8|data[1]);#endif	ret = i2c_attach_client(uda1380_client);done:	if (ret) {		kfree(d);	}	return ret;}static int uda1380_probe(struct i2c_adapter *adap){	printk("%s:%d\n",__FUNCTION__,__LINE__);	return i2c_probe(adap, &addr_data, uda1380_attach);}static int uda1380_detach(struct i2c_client *client){printk("%s:%d\n",__FUNCTION__,__LINE__);	i2c_detach_client(client);	kfree(i2c_get_clientdata(client));	return 0;}static intuda1380_command(struct i2c_client *client, unsigned int cmd, void *arg){	_DBG(1, "cmd=%d", cmd);#if 0	switch (cmd) {	case RTC_GETDATETIME:		return uda1380_get_datetime(client, arg);	case RTC_SETTIME:		return uda1380_set_datetime(client, arg, 0);	case RTC_SETDATETIME:		return uda1380_set_datetime(client, arg, 1);	case RTC_GETCTRL:		return uda1380_get_ctrl(client, arg);	case RTC_SETCTRL:		return uda1380_set_ctrl(client, arg);	case MEM_READ:		return uda1380_read_mem(client, arg);	case MEM_WRITE:		return uda1380_write_mem(client, arg);	default:		return -EINVAL;	}#endif}static struct i2c_driver uda1380_driver = {	.owner		= THIS_MODULE,	.name		= "UDA1380",	.id		= I2C_DRIVERID_UDA1380,	.flags		= I2C_DF_NOTIFY,	.attach_adapter = uda1380_probe,	.detach_client	= uda1380_detach,	.command	= uda1380_command};static __init int uda1380_init(void){	return i2c_add_driver(&uda1380_driver);}static __exit void uda1380_exit(void){	i2c_del_driver(&uda1380_driver);}MODULE_AUTHOR("Stefan Eletzhofer <Stefan.Eletzhofer@eletztrick.de>");MODULE_DESCRIPTION("EPSON RTC8564 Driver");MODULE_LICENSE("GPL");module_init(uda1380_init);module_exit(uda1380_exit);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -