今天写了个官方i2c驱动,加载出现kernel panic
驱动如下
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/slab.h>
//#include <linux/smp_lock.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <asm/io.h>
//#include <asm/system.h>
#ifndef CONFIG_HISI_SNAPSHOT_BOOT
#include <linux/miscdevice.h>
#endif
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <asm/bitops.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <linux/moduleparam.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <linux/reboot.h>
#include <linux/notifier.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "tlv320aic31.h"
#define CHIP_NUM 1
#define DEV_NAME "tlv320aic31"
static struct i2c_board_info hi_info =
{
I2C_BOARD_INFO("adv7180", 0x21),
};
static struct i2c_client* tlv_client;
int tlv320aic31_write(unsigned char chip_addr, unsigned char reg_addr, unsigned char value)
{
int ret;
unsigned char buf[2];
struct i2c_client* client = tlv_client;
buf[0] = reg_addr;
buf[1] = value;
ret = i2c_master_send(client, buf, 2);
return ret;
}
int tlv320aic31_read(unsigned char chip_addr, unsigned char reg_addr)
{
int ret_data = 0xFF;
int ret;
struct i2c_client* client = tlv_client;
unsigned char buf[2];
buf[0] = reg_addr;
ret = i2c_master_recv(client, buf, 1);
if (ret >= 0)
{
ret_data = buf[0];
}
return ret_data;
}
static int __init tlv320aic31_init(void)
{
struct i2c_adapter* i2c_adap=NULL;
int temp = 0;
printk("matt-tlv320aic31_init\n");
// use i2c2
i2c_adap = i2c_get_adapter(2);
tlv_client = i2c_new_device(i2c_adap, &hi_info);
i2c_put_adapter(i2c_adap);
temp = tlv320aic31_read(0x21, 0x10);
printk("matt-temp=%x\n",temp);
return 0;
}
static void __exit tlv320aic31_exit(void)
{
i2c_unregister_device(tlv_client);
printk("rmmod tlv320aic31.ko for Hi3516A ok!\n");
}
module_init(tlv320aic31_init);
module_exit(tlv320aic31_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Hisilicon");
然后放在driver/gpio下面编译
然后加载之后出现了kernel panic
log如下
Starting kernel ...
Uncompressing Linux... done, booting the kernel.
Booting Linux on physical CPU 0
Linux version 3.4.35 (matt@ubuntu) (gcc version 4.8.3 20131202 (prerelease) (His
ilicon_v300) ) #34 Tue Aug 8 00:09:56 PDT 2017
CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=10c53c7d
CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
Machine: hi3516a
Memory policy: ECC disabled, Data cache writeback
Built 1 zonelists in Zone order, mobility grouping on. Total pages: 32512
Kernel command line: mem=128M console=ttyAMA0,115200n8 mtdparts=hi_sfc:1M(uboot)
,4M(kernel),24M(root),3M(data) root=/dev/mtdblock2 rootfstype=jffs2
PID hash table entries: 512 (order: -1, 2048 bytes)
Dentry cache hash table entries: 16384 (order: 4, 65536 bytes)
Inode-cache hash table entries: 8192 (order: 3, 32768 bytes)
Memory: 128MB = 128MB total
Memory: 124028k/124028k available, 7044k reserved, 0K highmem
Virtual kernel memory layout:
vector : 0xffff0000 - 0xffff1000 ( 4 kB)
fixmap : 0xfff00000 - 0xfffe0000 ( 896 kB)
vmalloc : 0xc8800000 - 0xff000000 ( 872 MB)
lowmem : 0xc0000000 - 0xc8000000 ( 128 MB)
modules : 0xbf000000 - 0xc0000000 ( 16 MB)
.text : 0xc0008000 - 0xc054b000 (5388 kB)
.init : 0xc054b000 - 0xc056d53c ( 138 kB)
.data : 0xc056e000 - 0xc059d940 ( 191 kB)
.bss : 0xc059d964 - 0xc05bc9f8 ( 125 kB)
SLUB: Genslabs=11, HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
NR_IRQS:128
sched_clock: 32 bits at 49MHz, resolution 20ns, wraps every 86767ms
Console: colour dummy device 80x30
Calibrating delay loop... 1196.85 BogoMIPS (lpj=5984256)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 512
Initializing cgroup subsys freezer
CPU: Testing write buffer coherency: ok
Setting up static identity map for 0x8041e9d8 - 0x8041ea30
dummy:
NET: Registered protocol family 16
Serial: AMBA PL011 UART driver
uart:0: ttyAMA0 at MMIO 0x20080000 (irq = 40) is a PL011 rev2
console [ttyAMA0] enabled
uart:1: ttyAMA1 at MMIO 0x20090000 (irq = 41) is a PL011 rev2
bio: create slab <bio-0> at 0
SCSI subsystem initialized
hi-spi-master hi-spi-master.0: with 1 chip select slaves attached
hi-spi-master hi-spi-master.1: with 3 chip select slaves attached
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
Switching to clocksource timer0
NET: Registered protocol family 2
IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
TCP established hash table entries: 4096 (order: 3, 32768 bytes)
TCP bind hash table entries: 4096 (order: 2, 16384 bytes)
TCP: Hash tables configured (established 4096 bind 4096)
TCP: reno registered
UDP hash table entries: 256 (order: 0, 4096 bytes)
UDP-Lite hash table entries: 256 (order: 0, 4096 bytes)
NET: Registered protocol family 1
RPC: Registered named UNIX socket transport module.
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
RPC: Registered tcp NFSv4.1 backchannel transport module.
VFS: Disk quotas dquot_6.5.2
Dquot-cache hash table entries: 1024 (order 0, 4096 bytes)
squashfs: version 4.0 (2009/01/31) Phillip Lougher
NFS: Registering the id_resolver key type
jffs2: version 2.2. (NAND) © 2001-2006 Red Hat, Inc.
fuse init (API version 7.18)
SGI XFS with security attributes, large block/inode numbers, no debug enabled
msgmni has been set to 242
Block layer SCSI generic (bsg) driver version 0.4 loaded (major 254)
io scheduler noop registered
io scheduler deadline registered (default)
io scheduler cfq registered
Unable to handle kernel NULL pointer dereference at virtual address 00000028
pgd = c0004000
[00000028] *pgd=00000000
Internal error: Oops: 5 [#1] ARM
Modules linked in:
CPU: 0 Not tainted (3.4.35 #34)
PC is at i2c_check_addr_busy+0x14/0x6c
LR is at i2c_new_device+0xc8/0x17c
pc : [<c033c168>] lr : [<c033c288>] psr: 80000013
sp : c7827ef8 ip : c7827f18 fp : c7827f14
r10: 00000000 r9 : c055c0e4 r8 : c059e000
r7 : c790b404 r6 : 00000000 r5 : c0586db8 r4 : 00000000
r3 : 0000003f r2 : 80000000 r1 : 00000040 r0 : 00000000
Flags: Nzcv IRQs on FIQs on Mode SVC_32 ISA ARM Segment kernel
Control: 10c53c7d Table: 80004059 DAC: 00000015
PC: 0xc033c0e8:
c0e8 e2800028 e50b1018 e3530000 0a000003 e59f2050 e5931030 e1510002 0a000005
c108 e30b2bec e24b1018 e34c2033 ebfddbc7 e24bd010 e89da810 e30b2bec e24b1018
c128 e34c2033 e2434028 ebfddbc0 e3500000 1afffff6 e1a00004 e51b1018 ebffffe2
c148 e24bd010 e89da810 c059732c e1a0c00d e92dd810 e24cb004 e24dd00c e1a04000
c168 e5900028 e50b1018 e3500000 0a000003 e59f203c e5903030 e1530002 0a000006
c188 e2840028 e24b1018 e30d2d84 e34c2033 ebfddba6 e24bd010 e89da810 e2400028
c1a8 ebffffc9 e3500000 0afffff4 e24bd010 e89da810 c059732c e1a0c00d e92dd8f0
c1c8 e24cb004 e24dd008 e3093ba4 e34c305a e1a06000 e1a05001 e5930024 e3500000
LR: 0xc033c208:
c208 e1a01005 e1a00007 e3a02014 e5843074 e1d531b4 e1c430b0 e1d531b6 e1c430b2
c228 e5953024 e58430f8 ebfcca7d e1d430b0 e1d410b2 e3130010 1a00002e e2413001
c248 e6ff3073 e35300fd 9a00000a e2860028 e3a02007 e1a03001 e30315cc e34c1052
c268 ebfddd71 e1a00004 ebf551e5 e3a00000 e24bd01c e89da8f0 e1a00006 ebffffb2
c288 e2501000 1a000022 e5942018 e3071314 e1d430b0 e34c1059 e2810030 e5841050
c2a8 e584006c e2822028 e5842020 e2133010 e595c020 e2845020 e1d420b2 13a03a0a
c2c8 e1a00005 e30315f0 e584c0c8 e1833002 e34c1052 e5962100 ebfdda51 e1a00005
c2e8 ebfddfe3 e2501000 1a000009 e1a00004 e24bd01c e89da8f0 e3510b01 3affffdd
SP: 0xc7827e78:
7e78 c0585af0 c0586218 c7827e9c c7827e90 c003997c c033c168 80000013 ffffffff
7e98 c7827ee4 c059e000 c7827f14 c7827eb0 c000dcd8 c0008360 00000000 00000040
7eb8 80000000 0000003f 00000000 c0586db8 00000000 c790b404 c059e000 c055c0e4
7ed8 00000000 c7827f14 c7827f18 c7827ef8 c033c288 c033c168 80000013 ffffffff
7ef8 00000000 c790b400 c0586db8 c790b400 c7827f3c c7827f18 c033c288 c033c160
7f18 c7827f3c c7827f28 c0586db8 00000000 c059e000 c059e000 c7827f54 c7827f40
7f38 c055c10c c033c1cc c056d208 00000006 c7827fac c7827f58 c00086a0 c055c0f0
7f58 c0033b68 c054b274 00000000 c7826018 00000006 c06bd57e 00000006 c04e71b4
IP: 0xc7827e98:
7e98 c7827ee4 c059e000 c7827f14 c7827eb0 c000dcd8 c0008360 00000000 00000040
7eb8 80000000 0000003f 00000000 c0586db8 00000000 c790b404 c059e000 c055c0e4
7ed8 00000000 c7827f14 c7827f18 c7827ef8 c033c288 c033c168 80000013 ffffffff
7ef8 00000000 c790b400 c0586db8 c790b400 c7827f3c c7827f18 c033c288 c033c160
7f18 c7827f3c c7827f28 c0586db8 00000000 c059e000 c059e000 c7827f54 c7827f40
7f38 c055c10c c033c1cc c056d208 00000006 c7827fac c7827f58 c00086a0 c055c0f0
7f58 c0033b68 c054b274 00000000 c7826018 00000006 c06bd57e 00000006 c04e71b4
7f78 c0549d80 c00333d4 c0039990 c056d208 00000006 c059e000 c059e000 c054b268
FP: 0xc7827e94:
7e94 ffffffff c7827ee4 c059e000 c7827f14 c7827eb0 c000dcd8 c0008360 00000000
7eb4 00000040 80000000 0000003f 00000000 c0586db8 00000000 c790b404 c059e000
7ed4 c055c0e4 00000000 c7827f14 c7827f18 c7827ef8 c033c288 c033c168 80000013
7ef4 ffffffff 00000000 c790b400 c0586db8 c790b400 c7827f3c c7827f18 c033c288
7f14 c033c160 c7827f3c c7827f28 c0586db8 00000000 c059e000 c059e000 c7827f54
7f34 c7827f40 c055c10c c033c1cc c056d208 00000006 c7827fac c7827f58 c00086a0
7f54 c055c0f0 c0033b68 c054b274 00000000 c7826018 00000006 c06bd57e 00000006
7f74 c04e71b4 c0549d80 c00333d4 c0039990 c056d208 00000006 c059e000 c059e000
R5: 0xc0586d38:
6d38 00000000 00000000 00000000 00000000 28e10cac 4824e523 1152ea39 c05b3eb8
6d58 c0456cc8 00000101 0000011e 0000000f c05b4338 c0456d3c 00000000 0000001e
6d78 0000000f 00000000 c0456dc8 00000000 00000013 00000007 c0509378 00000124
6d98 c0033e34 00000000 00000000 00000000 00000000 c0510398 c05103a0 00000000
6db8 37766461 00303831 00000000 00000000 00000000 00400000 00000000 00000000
6dd8 00000000 00000000 00000040 00000000 c0586de8 c0586de8 00000000 00000001
6df8 c0586df8 c0586df8 00000000 00000000 c0586df4 c0510648 000001a4 c028d354
6e18 c028d4d8 c0510658 000001a4 c028cfa0 c028d7bc c04e6df0 000001a4 c028cfc8
R7: 0xc790b384:
b384 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
b3a4 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
b3c4 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
b3e4 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00400000
b404 37766461 00303831 00000000 00000000 00000000 00000000 00000000 00000000
b424 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
b444 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
b464 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
R8: 0xc059df80:
df80 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
dfa0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
dfc0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
dfe0 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
e000 00000000 00000000 00000000 00000000 00000000 c06bd480 c06bd500 00000000
e020 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
e040 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
e060 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
R9: 0xc055c064:
c064 e34c3058 e3060d48 e34c0058 03a01001 e3520006 e5831000 92821007 81a01002
c084 e51b2010 e5831004 e352000e 9282200f e5832008 ebf4599b e3a00000 e24bd00c
c0a4 e89da800 e1a0c00d e92dd800 e24cb004 e3a01000 e3000208 e1a02001 e34c0051
c0c4 ebf3b0c1 e3033ac4 e34c305b e3700a01 e1a02000 e5832000 93a00000 e89da800
c0e4 e1a0c00d e92dd830 e24cb004 e3a00002 e3064db8 ebf786d6 e34c4058 e1a01004
c104 e1a05000 ebf7802c e30433c0 e34c305b e5830000 e1a00005 ebf786e2 e5d40028
c124 e3a01010 ebf4b8c0 e1a01000 e3000424 e34c0051 ebfaf00a e3000434 e34c0051
c144 ebfaf007 e3e00000 e89da830 e1a0c00d e92dd810 e24cb004 e24dd00c e3a04000
Process swapper (pid: 1, stack limit = 0xc78262e8)
Stack: (0xc7827ef8 to 0xc7828000)
7ee0: 00000000 c790b400
7f00: c0586db8 c790b400 c7827f3c c7827f18 c033c288 c033c160 c7827f3c c7827f28
7f20: c0586db8 00000000 c059e000 c059e000 c7827f54 c7827f40 c055c10c c033c1cc
7f40: c056d208 00000006 c7827fac c7827f58 c00086a0 c055c0f0 c0033b68 c054b274
7f60: 00000000 c7826018 00000006 c06bd57e 00000006 c04e71b4 c0549d80 c00333d4
7f80: c0039990 c056d208 00000006 c059e000 c059e000 c054b268 c0568694 c05686a0
7fa0: c7827ff4 c7827fb0 c054b958 c0008570 00000006 00000006 c054b268 00000000
7fc0: 00000000 00000077 c001f5bc 00000000 c054b82c c001f5bc 00000013 00000000
7fe0: 00000000 00000000 00000000 c7827ff8 c001f5bc c054b838 ffffffff ffffffff
Backtrace:
[<c033c154>] (i2c_check_addr_busy+0x0/0x6c) from [<c033c288>] (i2c_new_device+0x
c8/0x17c)
r4:c790b400
[<c033c1c0>] (i2c_new_device+0x0/0x17c) from [<c055c10c>] (tlv320aic31_init+0x28
/0x6c)
r7:c059e000 r6:c059e000 r5:00000000 r4:c0586db8
[<c055c0e4>] (tlv320aic31_init+0x0/0x6c) from [<c00086a0>] (do_one_initcall+0x13
c/0x1ac)
r5:00000006 r4:c056d208
[<c0008564>] (do_one_initcall+0x0/0x1ac) from [<c054b958>] (kernel_init+0x12c/0x
1c0)
[<c054b82c>] (kernel_init+0x0/0x1c0) from [<c001f5bc>] (do_exit+0x0/0x698)
Code: e92dd810 e24cb004 e24dd00c e1a04000 (e5900028)
---[ end trace dad60e55f5ea98ef ]---
Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b
看起来和i2c相关,但是为啥呢,后来对照正常log一看才发现,这个驱动加载的太早了,i2c bus的驱动都还没有加载,所以出现了错误,然后把代码放到了driver/i2c的目录下d的最后一行
#
# Makefile for the i2c core.
#
obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o
obj-$(CONFIG_I2C) += i2c-core.o
obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o
obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o
obj-$(CONFIG_I2C_MUX) += i2c-mux.o
obj-y += algos/ busses/ muxes/
obj-n += tlv320aic31.o
ccflags-$(CONFIG_I2C_DEBUG_CORE) := -DDEBUG
CFLAGS_i2c-core.o := -Wno-deprecated-declarations
这样编译后运行就正常了,所以设备驱动的加载一定不能早于总线