Linux下SPI总线驱动_第1页
Linux下SPI总线驱动_第2页
Linux下SPI总线驱动_第3页
Linux下SPI总线驱动_第4页
Linux下SPI总线驱动_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Linux下SPI总线驱动有通用接口,一般的SPI设备驱动使用这个驱动接口实现设备驱动。分析驱动最好是先了解核心代码,然后从具体设备分析入手,然后从下至上,了解整个框架,再从上到下分析,理解透彻。以下分析内核根目录均以src代替。内核代码http:/lxr.linux.no/,版本。SPI的核心代码即src/drivers/spi/spi.cSPI 初始化和核心代码src/drivers/spi/spi_gpio.cIO模拟SPI接口代码头文件:src/include/linux/spi/spi.hsrc/include/linux/spi/spi_gpio.hsrc/incl

2、ude/linux/spi/spi_bitbang.h首先,先看核心代码。一步步来,先分析代码,然后看了具体驱动再回过头来看这个核心代码。spi.c- /* 这个函数是驱动模块卸载时使用 */ 32static void spidev_release(struct device *dev) 33 /* 标准设备结构体转换成SPI设备结构体 * 调用container_of函数获取dev设备所在的SPI设备结构体指针 */ 34 struct spi_device *spi = to_spi_device(dev); 35 36 /* spi masters may cleanup for re

3、leased devices */ /* 清空SPI主机申请的内存 */ 37 if (spi-master-cleanup) 38 spi-master-cleanup(spi); 39 /* 减调用次数 */ 40 spi_master_put(spi-master); /* 释放SPI设备节点内存 */ 41 kfree(spi); 42 43 /* 打印模块别名 */ 44static ssize_t 45modalias_show(struct device *dev, struct device_attribute *a, char *buf) 46 47 const struct

4、 spi_device *spi = to_spi_device(dev); 48 49 return sprintf(buf, %sn, spi-modalias); 50 51/* 设置SPI总线属性名称/显示 */ 52static struct device_attribute spi_dev_attrs = 53 _ATTR_RO(modalias), 54 _ATTR_NULL, 55;56 /* 获取设备ID */ 57/* modalias support makes modprobe $MODALIAS new-style hotplug work, 58 * and the

5、 sysfs version makes coldplug work too. 59 */ 60 61static const struct spi_device_id *spi_match_id(const struct spi_device_id *id, 62 const struct spi_device *sdev) 63 /* 判断设备中名称与模块别名相同,则返回该设备ID */ 64 while (id-name0) 65 if (!strcmp(sdev-modalias, id-name) 66 return id; 67 id+; 68 69 return NULL; 70

6、 71 /* 返回设备ID */ 72const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev) 73 74 const struct spi_driver *sdrv = to_spi_driver(sdev-dev.driver); 75 76 return spi_match_id(sdrv-id_table, sdev); 77 78EXPORT_SYMBOL_GPL(spi_get_device_id);接上一个继续看spi.c。-匹配设备 /* 名词解释of: OpenFirmware *

7、 调用层次spi_match_device-of_driver_match_device-of_match_device- * of_match_node * 用于驱动程序检查platform_device是否在其支持列表里 */ 80static int spi_match_device(struct device *dev, struct device_driver *drv) 81 82 const struct spi_device *spi = to_spi_device(dev); 83 const struct spi_driver *sdrv = to_spi_driver(d

8、rv); 84 85 /* Attempt an OF style match */ /* 不匹配返回0;匹配返回非0,指向struct of_device_id类型的指针 * dev:需要查找的设备; drv:驱动程序结构体 */ 86 if (of_driver_match_device(dev, drv) 87 return 1; 88 /* 在驱动查找设备ID,找到返回真,否则假 */ 89 if (sdrv-id_table) 90 return !spi_match_id(sdrv-id_table, spi); 91 /* 比较设备别名和驱动名称,匹配返回真 */ 92 retu

9、rn strcmp(spi-modalias, drv-name) = 0; 93 94-uevent /* struct kobj_uevent_env 是内核用户空间的一个环境参数 * uevent是sysfs向用户空间发出的消息,这里实际上添加的是一串字符串消息。 * 关于uevent参考:/walkingman321/archive/2010/10/01/.aspx */ 95static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) 96 97 const stru

10、ct spi_device *spi = to_spi_device(dev); 98 99 add_uevent_var(env, MODALIAS=%s%s, SPI_MODULE_PREFIX, spi-modalias); 100 return 0; 101-电源管理 /* 配置了电源管理 * 现在不清楚suspend和resume函数哪里实现,等找到了再说 */ 103#ifdef CONFIG_PM 104 /* 挂起 */ 105static int spi_suspend(struct device *dev, pm_message_t message) 106 107 int

11、 value = 0; 108 struct spi_driver *drv = to_spi_driver(dev-driver); 109 110 /* suspend will stop irqs and dma; no more i/o */ /* 挂起将定制终端和DMA,没有输入输出 */ 111 if (drv) /* 驱动实现了挂起操作函数 */ 112 if (drv-suspend) 113 value = drv-suspend(to_spi_device(dev), message); 114 else 115 dev_dbg(dev, . cant suspendn);

12、 116 117 return value; 118 119 /* 恢复 */ 120static int spi_resume(struct device *dev) 121 122 int value = 0; 123 struct spi_driver *drv = to_spi_driver(dev-driver); 124 125 /* resume may restart the i/o queue */ /* 重新开始输入输出队列 */ 126 if (drv) 127 if (drv-resume) 128 value = drv-resume(to_spi_device(de

13、v); 129 else 130 dev_dbg(dev, . cant resumen); 131 132 return value; 133 134 135#else /* 没有电源管理 */ 136#define spi_suspend NULL 137#define spi_resume NULL 138#endif-总线 /* 总线 参考:/xingzuzi/blog/item/d12c03f473b3c2a0a50f5260.html */ 140struct bus_type spi_bus_type = 141 .name = spi, 14

14、2 .dev_attrs = spi_dev_attrs, 143 .match = spi_match_device, 144 .uevent = spi_uevent, 145 .suspend = spi_suspend, 146 .resume = spi_resume, 147; 148EXPORT_SYMBOL_GPL(spi_bus_type);-驱动注册、删除/* 驱动注册 */ 151static int spi_drv_probe(struct device *dev) 152 153 const struct spi_driver *sdrv = to_spi_drive

15、r(dev-driver); 154 155 return sdrv-probe(to_spi_device(dev); 156 157 /* 驱动删除 */ 158static int spi_drv_remove(struct device *dev) 159 160 const struct spi_driver *sdrv = to_spi_driver(dev-driver); 161 162 return sdrv-remove(to_spi_device(dev); 163 164 /* 关闭 */ 165static void spi_drv_shutdown(struct d

16、evice *dev) 166 167 const struct spi_driver *sdrv = to_spi_driver(dev-driver); 168 169 sdrv-shutdown(to_spi_device(dev); 170-注册SPI总线驱动 /* 注册SPI驱动 */ 172/* 173 * spi_register_driver - register a SPI driver 174 * sdrv: the driver to register 175 * Context: can sleep 176 */ 177int spi_register_driver(s

17、truct spi_driver *sdrv) 178 /* 初始化总线结构体 */ 179 sdrv-driver.bus = &spi_bus_type; /* 初始化驱动相关函数 */ 180 if (sdrv-probe) 181 be = spi_drv_probe; 182 if (sdrv-remove) 183 sdrv-driver.remove = spi_drv_remove; 184 if (sdrv-shutdown) 185 sdrv-driver.shutdown = spi_drv_shutdown; /* 驱动注册 * 添加驱动到

18、总线 * sysfs、uevent等创建、初始化 */ 186 return driver_register(&sdrv-driver); 187 188EXPORT_SYMBOL_GPL(spi_register_driver);接下去看spi.c特殊的板级相关设备添加方法 /* 神奇的分割线 */ 190/*-*/ 191 192/* SPI devices should normally not be created by SPI device drivers; that 193 * would make them board-specific. Similarly with SPI m

19、aster drivers. 194 * Device registration normally goes into like arch/./mach./board-YYY.c 195 * with other readonly (flashable) information about mainboard devices. 196 */ 197 /* 板级相关信息链表 */ 198struct boardinfo 199 struct list_head list; 200 struct spi_board_info board_info; 201; 202 203static LIST_

20、HEAD(board_list); 204static LIST_HEAD(spi_master_list); 206/* 207 * Used to protect add/del opertion for board_info list and 208 * spi_master list, and their matching process 209 */ /* 链表操作锁 */ 210static DEFINE_MUTEX(board_lock); 211 212/* 213 * spi_alloc_device - Allocate a new SPI device 214 * mas

21、ter: Controller to which device is connected 215 * Context: can sleep 216 * 217 * Allows a driver to allocate and initialize a spi_device without 218 * registering it immediately. This allows a driver to directly 219 * fill the spi_device with device parameters before calling 220 * spi_add_device()

22、on it. 221 * 222 * Caller is responsible to call spi_add_device() on the returned 223 * spi_device structure to add it to the SPI master. If the caller 224 * needs to discard the spi_device without adding it, then it should 225 * call spi_dev_put() on it. 226 * 227 * Returns a pointer to the new dev

23、ice, or NULL. 228 */ /* 为申请SPI设备结构体空间而不注册设备,必须调用spi_add_device,如要丢弃这个设备 * 则必须调用spi_dev_put */ 229struct spi_device *spi_alloc_device(struct spi_master *master) 230 231 struct spi_device *spi; 232 struct device *dev = master-dev.parent; 233 /* 增加设备引用次数,相反操作spi_master_put */ 234 if (!spi_master_get(ma

24、ster) 235 return NULL; 236 /* 申请内核空间内存 */ 237 spi = kzalloc(sizeof *spi, GFP_KERNEL); 238 if (!spi) 239 dev_err(dev, cannot alloc spi_devicen); 240 spi_master_put(master); 241 return NULL; 242 243 /* SPI主机控制结构体 */ 244 spi-master = master; /* 设备 */ 245 spi-dev.parent = dev; /* 总线 */ 246 spi-dev.bus =

25、 &spi_bus_type; /* 删除方法 */ 247 spi-dev.release = spidev_release; /* 设备初始化 * 初始化kobject,dma池链表,设备互斥锁,自旋锁,设备对象的电源相关部分 * src/drivers/base/core.c */ 248 device_initialize(&spi-dev); /* 返回spi设备结构体指针 */ 249 return spi; 250 251EXPORT_SYMBOL_GPL(spi_alloc_device); 252 253/* 254 * spi_add_device - Add spi_de

26、vice allocated with spi_alloc_device 255 * spi: spi_device to register 256 * 257 * Companion function to spi_alloc_device. Devices allocated with 258 * spi_alloc_device can be added onto the spi bus with this function. 259 * 260 * Returns 0 on success; negative errno on failure 261 */ /* 与spi_alloc_

27、device配合使用,将设备结构体添加到SPI总线 */ 262int spi_add_device(struct spi_device *spi) 263 264 static DEFINE_MUTEX(spi_add_lock); 265 struct device *dev = spi-master-dev.parent; 266 struct device *d; 267 int status; 268 269 /* Chipselects are numbered 0.max; validate. */ /* 设备片选编号比SPI主控制器的片选大,出错 */ 270 if (spi-

28、chip_select = spi-master-num_chipselect) 271 dev_err(dev, cs%d = max %dn, 272 spi-chip_select, 273 spi-master-num_chipselect); 274 return -EINVAL; 275 276 277 /* Set the bus ID string */ /* 设置设备名称,调用kobject的设置设备名称参数方法 */ 278 dev_set_name(&spi-dev, %s.%u, dev_name(&spi-master-dev), 279 spi-chip_selec

29、t); 280 281 282 /* We need to make sure theres no other device with this 283 * chipselect *BEFORE* we call setup(), else well trash 284 * its configuration. Lock against concurrent add() calls. 285 */ /* 互斥锁,保证设置时,没有其他设备用这个片选,防止并发添加add() */ 286 mutex_lock(&spi_add_lock); 287 /* 按照名称字符查找设备结构体,设备名称由de

30、v_name查找kobject返回 * 能够找到说明设备已经在用 */ 288 d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi-dev); 289 if (d != NULL) 290 dev_err(dev, chipselect %d already in usen, 291 spi-chip_select); 292 put_device(d); 293 status = -EBUSY; 294 goto done; 295 296 297 /* Drivers may modify this initial

31、i/o setup, but will 298 * normally rely on the device being setup. Devices 299 * using SPI_CS_HIGH cant coexist well otherwise. 300 */ /* /* 设置SPI模式和时钟频率 * 驱动可以初始化IO,当设备被设置的时候 * 取消片选没有用 * 详细见后面驱动核心方法spi_setup解析 */ 301 status = spi_setup(spi); 302 if (status dev), status); 305 goto done; 306 307 308

32、/* Device may be bound to an active driver when this returns */ /* 将设备添加到驱动层次结构,添加到kobject层次结构,添加到其他子系统 */ 309 status = device_add(&spi-dev); 310 if (status dev), status); 313 else 314 dev_dbg(dev, registered child %sn, dev_name(&spi-dev); 315 316done: 317 mutex_unlock(&spi_add_lock); 318 return sta

33、tus; 319 320EXPORT_SYMBOL_GPL(spi_add_device); 321 322/* 323 * spi_new_device - instantiate one new SPI device 324 * master: Controller to which device is connected 325 * chip: Describes the SPI device 326 * Context: can sleep 327 * 328 * On typical mainboards, this is purely internal; and its not n

34、eeded 329 * after board init creates the hard-wired devices. Some development 330 * platforms may not be able to use spi_register_board_info though, and 331 * this is exported so that for example a USB or parport based adapter 332 * driver could add devices (which it would learn about out-of-band).

35、333 * 334 * Returns the new device, or NULL. 335 */ /* 有些开发平台可能不能通过spi_register_board_info添加设备 * 这个用来让一些比如基于USB适配的驱动添加设备 */ 336struct spi_device *spi_new_device(struct spi_master *master, 337 struct spi_board_info *chip) 338 339 struct spi_device *proxy; 340 int status; 341 342 /* NOTE: caller did a

36、ny chip-bus_num checks necessary. 343 * 344 * Also, unless we change the return value convention to use 345 * error-or-pointer (not NULL-or-pointer), troubleshootability 346 * suggests syslogged diagnostics are best here (ugh). 347 */ 348 /* 分配设备结构体内存空间,并初始化 */ 349 proxy = spi_alloc_device(master);

37、350 if (!proxy) 351 return NULL; 352 353 WARN_ON(strlen(chip-modalias) = sizeof(proxy-modalias); 354 355 proxy-chip_select = chip-chip_select; 356 proxy-max_speed_hz = chip-max_speed_hz; 357 proxy-mode = chip-mode; 358 proxy-irq = chip-irq; 359 strlcpy(proxy-modalias, chip-modalias, sizeof(proxy-mod

38、alias); 360 proxy-dev.platform_data = (void *) chip-platform_data; 361 proxy-controller_data = chip-controller_data; 362 proxy-controller_state = NULL; 363 /* 添加设备 */ 364 status = spi_add_device(proxy); 365 if (status bus_num != bi-bus_num) 380 return; 381 382 dev = spi_new_device(master, bi); 383 i

39、f (!dev) 384 dev_err(master-dev.parent, cant create new device for %sn, 385 bi-modalias); 386 387 388/* 389 * spi_register_board_info - register SPI devices for a given board 390 * info: array of chip descriptors 391 * n: how many descriptors are provided 392 * Context: can sleep 393 * 394 * Board-s

40、pecific early init code calls this (probably during arch_initcall) 395 * with segments of the SPI device table. Any device nodes are created later, 396 * after the relevant parent SPI controller (bus_num) is defined. We keep 397 * this table of devices forever, so that reloading a controller driver

41、will 398 * not make Linux forget about these hard-wired devices. 399 * 400 * Other code can also call this, e.g. a particular add-on board might provide 401 * SPI devices through its expansion connector, so code initializing that board 402 * would naturally declare its SPI devices. 403 * 404 * The b

42、oard info passed can safely be _initdata . but be careful of 405 * any embedded pointers (platform_data, etc), theyre copied as-is. 406 */ /* 使用一系列板级描述信息初始化设备 */ 407int _init 408spi_register_board_info(struct spi_board_info const *info, unsigned n) 409 410 struct boardinfo *bi; 411 int i; 412 413 bi

43、 = kzalloc(n * sizeof(*bi), GFP_KERNEL); 414 if (!bi) 415 return -ENOMEM; 416 417 for (i = 0; i board_info, info, sizeof(*info); 421 mutex_lock(&board_lock); /* 添加到板级描述符链表 */ 422 list_add_tail(&bi-list, &board_list); /* 将主机控制类链表所有的节点匹配板级信息的设备初始化 */ 423 list_for_each_entry(master, &spi_master_list, list) 424 spi_match_master_to_boardinfo(master, &bi-board_info); 425 mutex_unlock(&board_lock); 4

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论