SI4703_I2C.c
资源名称:fmradio.zip [点击查看]
上传用户:ledjyj
上传日期:2014-08-27
资源大小:2639k
文件大小:8k
源码类别:
驱动编程
开发平台:
Unix_Linux
- /*
- si4703.c - Part of lm_sensors, Linux kernel modules for hardware
- monitoring
- Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl>,
- Philip Edelbrock <phil@netroedge.com>,
- Dan Eaton <dan.eaton@rocketlogix.com>
- Ported to Linux 2.6 by Aurelien Jarno <aurel32@debian.org> with
- the help of Jean Delvare <khali@linux-fr.org>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- /* A few notes about the SI4703:
- * The SI4703 is an 8-bit I/O expander for the I2C bus produced by
- Philips Semiconductors. It is designed to provide a byte I2C
- interface to up to 8 separate devices.
- * The SI4703 appears as a very simple SMBus device which can be
- read from or written to with SMBUS byte read/write accesses.
- --Dan
- */
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/slab.h>
- #include <linux/i2c.h>
- /* Addresses to scan */
- static unsigned short normal_i2c[] = { 0x2a,
- I2C_CLIENT_END };
- /* Insmod parameters */
- I2C_CLIENT_INSMOD_2(si4703, si4703a);
- /* Initial values */
- #define SI4703_INIT 255 /* All outputs on (input mode) */
- /* Each client has this additional data */
- struct si4703_data {
- struct i2c_client client;
- u8 write; /* Remember last written value */
- };
- static int si4703_attach_adapter(struct i2c_adapter *adapter);
- static int si4703_detect(struct i2c_adapter *adapter, int address, int kind);
- static int si4703_detach_client(struct i2c_client *client);
- static void si4703_init_client(struct i2c_client *client);
- /* This is the driver that will be inserted */
- static struct i2c_driver si4703_driver = {
- .driver = {
- .name = "si4703",
- },
- .id = I2C_DRIVERID_SI4703,
- .attach_adapter = si4703_attach_adapter,
- .detach_client = si4703_detach_client,
- };
- static struct i2c_client *g_client = NULL;
- /* following are the sysfs callback functions */
- static ssize_t show_read(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct i2c_client *client = to_i2c_client(dev);
- return sprintf(buf, "%un", i2c_smbus_read_byte(client));
- }
- static DEVICE_ATTR(read, S_IRUGO, show_read, NULL);
- static ssize_t show_write(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct si4703_data *data = i2c_get_clientdata(to_i2c_client(dev));
- return sprintf(buf, "%un", data->write);
- }
- static ssize_t set_write(struct device *dev, struct device_attribute *attr, const char *buf,
- size_t count)
- {
- struct i2c_client *client = to_i2c_client(dev);
- struct si4703_data *data = i2c_get_clientdata(client);
- unsigned long val = simple_strtoul(buf, NULL, 10);
- if (val > 0xff)
- return -EINVAL;
- data->write = val;
- i2c_smbus_write_byte(client, data->write);
- return count;
- }
- static DEVICE_ATTR(write, S_IWUSR | S_IRUGO, show_write, set_write);
- /*
- * Real code
- */
- //beck add it for si4703 int SI4703_I2C_Write( u8 *value, u8 size)
- {
- int res=0;
- // printk("%sn",__FUNCTION__);
- if( g_client == NULL )
- return -1;
- struct i2c_msg msgs[1] =
- { g_client->addr, 0, size, value };
- res=i2c_transfer(g_client->adapter,msgs,1);
- if (res<=0)
- goto out;
- //res = i2c_master_send(g_client, buf, 2);
- //if (res >0)
- // res =0;
- //else
- // res =-1;
- printk(KERN_INFO "In funtion %s addr:%x value:%xreturn %d n", __FUNCTION__, g_client->addr,value,res);
- return res;
- out:
- printk(KERN_INFO "In funtion %s addr:%x,value=%xn", __FUNCTION__, g_client->addr,value);
- if (res<=0) printk("res = %d n",res);
- return res;
- }
- //beck add int SI4703_I2C_Read( u8 *pvalue, u8 size)
- {
- //printk("%sn",__FUNCTION__);
- int res=0;
- // char buf=0;
- // char buffer=0x0;
- struct i2c_msg msgs[1] ={
- // { addr, 0, 1, &buffer },
- { g_client->addr, 1, size, pvalue}};
- printk("%sn",__FUNCTION__);
- if( g_client == NULL )
- return -1;
- // i2c_ov9640_inc_use(g_client);
- // msgs[0].addr=msgs[1].addr=g_client->addr;
- //msgs[0].addr=g_client->addr;
- //res=i2c_transfer(g_client->adapter,&msgs[0],1);
- //if (res<=0)
- // goto out;
- res=i2c_transfer(g_client->adapter,msgs,1);
- if (res<=0)
- goto out;
- // printk("%s--buf value=%xn",__FUNCTION__, buf);
- // *pvalue=buf;
- printk("%s-- pvalue=%xn",__FUNCTION__,*pvalue);
- return 0;
- // i2c_ov9640_dec_use(g_client);
- out:
- printk(KERN_INFO "In funtion %s addr:%x,value=%xn", __FUNCTION__, g_client->addr,*pvalue);
- if (res<=0) printk("res = %d n",res);
- return res;
- }
- static int si4703_attach_adapter(struct i2c_adapter *adapter)
- {
- return i2c_probe(adapter, &addr_data, si4703_detect);
- }
- /* This function is called by i2c_probe */
- static int si4703_detect(struct i2c_adapter *adapter, int address, int kind)
- {
- struct i2c_client *new_client;
- struct si4703_data *data;
- int err = 0;
- const char *client_name = "";
- if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
- goto exit;
- /* OK. For now, we presume we have a valid client. We now create the
- client structure, even though we cannot fill it completely yet. */
- if (!(data = kzalloc(sizeof(struct si4703_data), GFP_KERNEL))) {
- err = -ENOMEM;
- goto exit;
- }
- new_client = &data->client;
- i2c_set_clientdata(new_client, data);
- new_client->addr = address;
- new_client->adapter = adapter;
- new_client->driver = &si4703_driver;
- new_client->flags = 0; g_client = new_client;
- /* Now, we would do the remaining detection. But the SI4703 is plainly
- impossible to detect! Stupid chip. */
- /* Determine the chip type */
- //if (kind <= 0) {
- //if (address >= 0x38 && address <= 0x3f)
- // kind = si4703a;
- // else
- // kind = si4703;
- //}
- //if (kind == si4703a)
- // client_name = "si4703a";
- //else
- client_name = "si4703";
- /* Fill in the remaining client fields and put it into the global list */
- strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
- /* Tell the I2C layer a new client has arrived */
- if ((err = i2c_attach_client(new_client)))
- {printk("si4703 has been registered failed!n");goto exit_free;}
- else{printk("si4703 has been registered success!n");}
- /* Initialize the SI4703 chip */
- //si4703_init_client(new_client);
- /* Register sysfs hooks */
- device_create_file(&new_client->dev, &dev_attr_read);
- device_create_file(&new_client->dev, &dev_attr_write);
- return 0;
- /* OK, this is not exactly good programming practice, usually. But it is
- very code-efficient in this case. */
- exit_free:
- kfree(data);
- exit:
- return err;
- }
- static int si4703_detach_client(struct i2c_client *client)
- {
- int err;
- if ((err = i2c_detach_client(client)))
- return err;
- kfree(i2c_get_clientdata(client));
- return 0;
- }
- /* Called when we have found a new SI4703. */
- static void si4703_init_client(struct i2c_client *client)
- {
- struct si4703_data *data = i2c_get_clientdata(client);
- data->write = SI4703_INIT;
- i2c_smbus_write_byte(client, data->write);
- }
- static int __init si4703_init(void)
- { int res=0;
- if ( (res = i2c_add_driver(&si4703_driver)) ) {
- printk("pcf8574: Driver registration failed, module not inserted.n");
- i2c_del_driver(&si4703_driver);
- return res;
- }
- if(g_client != NULL)
- printk("I2C: driver for device %s registed!.n", g_client->name);
- else
- printk("I2C: driver for device si4703 unregisted!.n");
- //return i2c_add_driver(&si4703_driver);
- }
- static void __exit si4703_exit(void)
- {
- i2c_del_driver(&si4703_driver);
- }
- MODULE_AUTHOR
- ("Beck He<beck.he@quantacn.com>");
- MODULE_DESCRIPTION("SI4703 driver");
- MODULE_LICENSE("GPL"); EXPORT_SYMBOL(SI4703_I2C_Write);
- EXPORT_SYMBOL(SI4703_I2C_Read);
- module_init(si4703_init);
- module_exit(si4703_exit);