1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
| #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/device.h> #include <asm/io.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/uaccess.h> #include "mpu6050.h" #include <linux/i2c.h>
dev_t devno; unsigned int major; struct class *pcls; struct device *pdevice;
static struct mpu6050_device{ struct i2c_client *client; }; struct mpu6050_device *mpu6050;
static int mpu6050_read_byte(struct i2c_client *client,unsigned char reg) { char txbuf[1] = {reg}; char rxbuf[1]; struct i2c_msg msg[2] = { {client->addr,0,1,txbuf}, {client->addr,1,1,rxbuf}, }; i2c_transfer(client->adapter,msg,ARRAY_SIZE(msg)); return rxbuf[0]; } static int mpu6050_write_byte(struct i2c_client *client,unsigned char reg,unsigned char val ) { char txbuf[2] = {reg,val}; struct i2c_msg msg[1] = { {client->addr,0,2,txbuf}, }; i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); return 0; }
static int mpu6050_init(struct i2c_client *client) {
mpu6050_write_byte(client,PWR_MGMT_1,0x0); mpu6050_write_byte(client,SMPLRT_DIV,0x07); mpu6050_write_byte(client,CONFIG,0x06); mpu6050_write_byte(client,GYRO_CONFIG,0xf8); mpu6050_write_byte(client,ACCEL_CONFIG,0x19); printk("mpu6050 init success \n"); return 0; }
int mpu6050_open(struct inode *inode, struct file *file) { return 0; }
void mpu6050_release(struct inode *inode, struct file *file) { }
long mpu6050_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { union mpu6050_data data; struct i2c_client *client = mpu6050->client;
switch(cmd) { case GET_ACCEL: data.accel.x = mpu6050_read_byte(client,ACCEL_XOUT_L); data.accel.x |= mpu6050_read_byte(client,ACCEL_XOUT_H) << 8;
data.accel.y = mpu6050_read_byte(client,ACCEL_YOUT_L); data.accel.y |= mpu6050_read_byte(client,ACCEL_YOUT_H) << 8;
data.accel.z = mpu6050_read_byte(client,ACCEL_ZOUT_L); data.accel.z |= mpu6050_read_byte(client,ACCEL_ZOUT_H)<< 8; break; case GET_GYRO: data.gyro.x = mpu6050_read_byte(client,GYRO_XOUT_L); data.gyro.x |= mpu6050_read_byte(client,GYRO_XOUT_H)<<8;
data.gyro.y = mpu6050_read_byte(client,GYRO_ZOUT_L); data.gyro.y |= mpu6050_read_byte(client,GYRO_ZOUT_H) << 8; break; default: printk("invaild argument\n"); } copy_to_user((void*)arg,&data,sizeof(data)); return sizeof(data); }
static struct file_operations fops = { .owner = THIS_MODULE, .open = mpu6050_open, .release = mpu6050_release, .unlocked_ioctl = mpu6050_ioctl, };
static int mpu6050_probe(struct i2c_client * client, const struct device * id) { printk("mpu6050_probe\n"); mpu6050 = kzalloc(sizeof(*mpu6050),GFP_KERNEL); mpu6050->client = client; major = register_chrdev(major,"mpu6050",&fops); devno = MKDEV(major,0); printk("create the devno success\n");
pcls = class_create(THIS_MODULE,"mpu6050-auto"); pdevice = device_create(pcls,NULL,devno,NULL,"mpu6050"); printk("create the device node\n");
mpu6050_init(client); printk("mpu6050_probe\n"); return 0; }
static int mpu6050_remove(struct i2c_client * client) {
printk("mpu6050 remove \n"); unregister_chrdev(major,"mpu6050"); device_destroy(pcls,devno);
class_destroy(pcls); return 0; }
int mpu6050_suspend(struct i2c_client *client,pm_message_t state) { printk("fs4412_i2c_suspened\n"); return 0; }
int mpu6050_resume(struct i2c_client *client) { printk("fs4412_i2c_resume\n"); return 0; }
static const struct i2c_device_id mpu6050_id[] = {
{"mpu6050",0}, };
static struct of_device_id mpu6050_dt_match[] = { {.compatible = "invense,mpu6050"},
};
struct i2c_driver mpu6050_driver = { .probe = mpu6050_probe, .remove = mpu6050_remove, .suspend = mpu6050_suspend, .resume = mpu6050_resume, .id_table = mpu6050_id, .driver = { .name = "mpu6050", .owner = THIS_MODULE, .of_match_table = mpu6050_dt_match, }, };
static int __init fs4412_i2c_driver_init(void) { printk("fs4412_i2c_driver_init\n");
i2c_add_driver(&mpu6050_driver);
return 0; } static void __exit fs4412_i2c_driver_exit(void) { printk("fs4412_i2c_driver_exit\n");
i2c_del_driver(&mpu6050_driver); }
MODULE_LICENSE("GPL"); module_init(fs4412_i2c_driver_init); module_exit(fs4412_i2c_driver_exit);
|