概述
调试MCP3421
I2C 通道2
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/kthread.h>
#include <linux/input.h>
#include <linux/miscdevice.h>
#include <linux/i2c.h>
#include <asm/uaccess.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/wait.h>
struct i2c_client *mcp3421_i2c_client;
#define ADC_LOG pr_debug
#define MCP3421_I2C_MAX_LOOP 3
#ifndef BOOL
typedef unsigned char BOOL;
#endif
static BOOL mcp3421_i2c_write_reg_len(unsigned char data)
{
BOOL ack=0;
unsigned char ret;
unsigned char wrbuf[2];
wrbuf[0] = data;
ret = i2c_master_send(mcp3421_i2c_client, wrbuf, 1);
if (ret != 1)
{
ADC_LOG("leo_code i2c_master_send failed %s rn", __func__);
//dev_err(&mcp3421_i2c_client->dev,"%s: i2c_master_recv() failed, ret=%dn",__func__, ret);
ack = 1;
}
ADC_LOG("leo_code mcp3421_i2c_write_reg_len() data=0x%x OKrn", data);
return ack;
}
static BOOL mcp3421_i2c_write_reg_org(unsigned char reg,unsigned char data)
{
BOOL ack=0;
unsigned char ret;
unsigned char wrbuf[2];
wrbuf[0] = reg;
wrbuf[1] = data;
ret = i2c_master_send(mcp3421_i2c_client, wrbuf, 2);
if (ret != 2)
{
ADC_LOG("leo_code i2c_master_send failed %s rn", __func__);
//dev_err(&mcp3421_i2c_client->dev,"%s: i2c_master_recv() failed, ret=%dn",__func__, ret);
ack = 1;
}
return ack;
}
BOOL mcp3421_i2c_write_reg(unsigned char reg,unsigned char data)
{
BOOL ack=0;
unsigned char i;
for (i=0; i<MCP3421_I2C_MAX_LOOP; i++)
{
ack = mcp3421_i2c_write_reg_org(reg,data);
if (ack == 0) // ack success
break;
}
return ack;
}
unsigned short mcp3421_i2c_read_reg(unsigned char regaddr)
{
unsigned char rdbuf[5], wrbuf[1], ret;
unsigned short res=0;
static int init_do=0;
if(!init_do)
{
mcp3421_i2c_write_reg_len(0x14);
init_do=1;
}
wrbuf[0] = regaddr;
/*
int i;
for (i=0; i<MCP3421_I2C_MAX_LOOP; i++)
{
ret = i2c_master_send(mcp3421_i2c_client, wrbuf, 1);
if (ret == 1)
break;
}
*/
ret = i2c_master_recv(mcp3421_i2c_client, rdbuf, 5);
if (ret <=0)
{
ADC_LOG("leo_code i2c_master_recv() error rn");
//dev_err(&mcp3421_i2c_client->dev,"%s: i2c_master_recv() failed, ret=%dn",__func__, ret);
}
res = (unsigned short)rdbuf[0]<<8 |(unsigned short)rdbuf[1];
{
int debg_res=(int)res;
ADC_LOG("leo_code i2c_master_recv() ret=%d : %x,%x,%x,%x: %d rn", ret,rdbuf[0], rdbuf[1], rdbuf[2], rdbuf[3],debg_res);
}
return res;
}
static int mcp3421_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
ADC_LOG("leo_code mcp3421_i2c_probe:OKn");
mcp3421_i2c_client = client;
return 0;
}
static int mcp3421_i2c_remove(struct i2c_client *client)
{
mcp3421_i2c_client = NULL;
return 0;
}
static const struct i2c_device_id mcp3421_i2c_id[] = {
{ "adc_i2c", 0 },
{ }
};
static const struct of_device_id adc_of_match[] = {
{.compatible = "mediatek,adc_i2c"},
{},
};
static struct i2c_driver mcp3421_i2c_driver = {
.driver =
{
.name = "adc_i2c",
.of_match_table = adc_of_match,
},
.probe = mcp3421_i2c_probe,
.remove = (mcp3421_i2c_remove),
.id_table = mcp3421_i2c_id,
};
//end-Bee-20140417
static int __init hwctl_driver_init(void)
{
int ret;
ADC_LOG("leo_code hwctl_driver_init()n");
// #err
// i2c_register_board_info(1, &mcp3421_i2c_hw, 1);
ret = i2c_add_driver(&mcp3421_i2c_driver);
ADC_LOG("hwctl_driver_init:start n");
return 0;
}
/* should never be called */
static void __exit hwctl_driver_exit(void)
{
i2c_del_driver(&mcp3421_i2c_driver);
}
module_init(hwctl_driver_init);
module_exit(hwctl_driver_exit);
MODULE_DESCRIPTION("mcp3421 Linux HW direct control driver");
MODULE_AUTHOR("leo@com.cn");
MODULE_LICENSE("GPL");
最后
以上就是洁净大地为你收集整理的MTK6580M 调试ADC mcp3421 I2C接口的全部内容,希望文章能够帮你解决MTK6580M 调试ADC mcp3421 I2C接口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复