当前位置:   article > 正文

如何在mtk andorid6.0上添加一个I2C驱动(这里是添加一个FM 芯片驱动)_cubeide i2c驱动hs6760

cubeide i2c驱动hs6760
平台:mt6737 android 6.0
#include <linux/init.h>
#include <linux/module.h>
//#include <stdlib.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/wait.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/pinctrl/consumer.h>
#include <linux/of_gpio.h>
#include <linux/delay.h>










static void fm_set_gpio_output(unsigned int GPIO, unsigned int output);
static unsigned int GPIO_FM_PWR_EN;
static unsigned int GPIO_SPK_PWR_EN;
#define HS6760_I2C_ID 0x18
//#define Clock_12M // Clock_7v6M//Clock_12M//Clock_24M//Clock_32v768K//选择时钟频率 

static struct i2c_client *HS6760_client = NULL;
static struct class *cls = NULL;
static unsigned int major;
static char *name = "fm_hs6760";

typedef enum 
{
hs6760_normal = 0,
hs6760_mute = 1,
hs6760_sleep = 2,
MODE_NULL
}MODE;


typedef enum
{
_75K = 0,
_50K = 1,
_22v5K = 2,
DEV_NULL
}DEV;

typedef enum
{
disable = 0,
enable = 1,
STATE_NULL
}STATE;

void HS6760_Initial(void);
void HS6760_SetFreq(uint16_t curFreq);
void HS6760_SetPow(uint8_t power);
void HS6760_Reset(void);
void HS6760_Fre_dev(DEV deviation);
void HS6760_Sel_mode(MODE mode);
void HS6760_Stereo(STATE flag);


static int Delayms(u32 data)
{
printk("delay %dms\n", data);
mdelay(data);//msleep(data);
return 0;
}


void ext_spkamp_enable(void)
{
printk(" fm  --- ext_spkamp_enable!\n");
fm_set_gpio_output(GPIO_SPK_PWR_EN , 1);
}


void ext_spkamp_disable(void)
{
printk(" fm  --- ext_spkamp_disable!\n");
fm_set_gpio_output(GPIO_SPK_PWR_EN , 0);
}


void HS6760_FM3V3_enable(void) //打开HS6760
{
printk(" fm  --- HS6760_FM3V3_enable!\n");
fm_set_gpio_output(GPIO_FM_PWR_EN, 1);
}


void HS6760_FM3V3_disable(void) //关闭HS6760
{
printk(" fm  --- HS6760_FM3V3_disable!\n");
fm_set_gpio_output(GPIO_FM_PWR_EN, 0);
}


static int HS6760_i2c_read(u8 reg)
{
unsigned char val[1] = {0};
int ret = 0;
val[0] = reg;
ret = i2c_master_send(HS6760_client, val, 1);
if (ret < 0) 
{
printk(" fm  --- HS6760_i2c_read I2C i/o error ret = %d\n", ret);
return ret;
}
mdelay(10);
ret = i2c_master_recv(HS6760_client, val, 1);
if (ret < 0) 
{
printk(" fm  --- HS6760_i2c_read I2C read error ret = %d\n", ret);
return ret;
}
return val[0];
}


static int HS6760_i2c_write(u8 reg, u8 writedata)
{
u8 databuf[2] = {0};
int ret = 0;
databuf[0] = reg;
databuf[1] = writedata;
ret = i2c_master_send(HS6760_client, databuf, 2);
printk(" fm  --- HS6760_i2c_write ret=%d, databuf[0]=%d, databuf[1]=%d\n", ret, databuf[0], databuf[1]);
if(ret < 0)
{
printk(" fm  --- HS6760_i2c_write send data failed !\n");
return -1;
}
return ret;
}


void HS6760_Initial(void)
{
uint8_t RegData;

#ifdef Clock_24M
RegData = 0x36; //PGA12dB,24M晶振
printk(" fm  ---HS6760 : HS6760_Initial Clock_24M !!\n");
#else

#ifdef Clock_12M
RegData = 0x34; //PGA12dB,12M晶
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/266560?site
推荐阅读