µÚ33Õ£ºÆ½Ì¨Çý¶¯Ä£ÐÍ
¸ÅÊö£ºÆ½Ì¨Çý¶¯Ä£ÐÍÊÇÓÉÁ½²¿·Ö×é³É£¬Ò»²¿·Ö½Ð×ödeviceÉ豸²ã£¬ÁíÒ»²¿·Ö½Ð×ödriverÇý¶¯²ã¡£É豸²ãÏòÇý¶¯²ã´«µÝÉ豸µÄÐÅÏ¢£¬±ÈÈçGPIO¶Ë¿ÚºÅ£¬irqÖжϺţ¬DMAÏà¹ØµÄÅäÖÃÐÅÏ¢£¬Çý¶¯ÊÕµ½ÕâЩÐÅÏ¢Ö®ºó£¬ÔÙ¸ù¾Ý¾ßÌåµÄϸ½ÚÀ´¶ÔÉ豸½øÐгõʼ»¯£¬É豸²ãÏà¶Ô¼òµ¥£¬Ö»Ð´´«µÝ±àдÇý¶¯ÐèÒªÓõ½µÄÐÅÏ¢£¬¶øÇý¶¯²ã³ýÁ˶ÔÉ豸½øÐгõʼ»¯Ö®Í⣬»¹Òª±àдÈçºÎʹÓÃÉ豸µÄº¯Êý£¬ÈçÉ豸²Ù×÷½Ó¿ÚµÄº¯Êý¾ÍÓ¦¸ÃжÔØÇý¶¯²ãµ±ÖУ¬ÕæÕýʹÓÃÉ豸µÄʱºòËùµ÷Óõĺ¯ÊýÓ¦¸ÃÈ«²¿ÊÇÓÉÇý¶¯²ãÌṩµÄ¡£
ÐéÄâ×ÜÏߣº½«²Ù×÷ÏàËƵÄÒ»ÀàÉ豸ÐéÄâ³ÉΪһ×é×ÜÏߣ¬±ÈÈç¼Ä´æÆ÷ÅäÖ÷½·¨ºÍ²Ù×÷·½·¨ÍêÈ«Ò»ÑùµÄGPIO¶Ë¿Ú»òÕßÊÇI2C,SPIµÈµÈ£¬¾Í¿ÉÒÔÐγÉÒ»×éÐéÄâ×ÜÏߣ¬ÒòΪ²Ù×÷ÏàËÆ£¬ÔÚÑ¡Ôñ»òÇý¶¯²»Í¬É豸µÄʱºò£¬Ö»ÐèÒªÐÞ¸ÄGPIO¶Ë¿ÚºÅ£¬¾Í¿ÉÒÔʵÏÖ¶Ô¶à¸öÉ豸µÄÇý¶¯¡£
ºÃ´¦£ºÈÃÇý¶¯µÄ±àдºÍͬÉ豸µÄÌæ»»±äµÃÊ®·ÖµÄ·½±ã¡£ÔÚLCDÇý¶¯ÒÆÖ²µÄʱºò½«ÌåÏֵĺÜÇå³þ¡£
×¢Ò⣺ƽ̨Çý¶¯Ä£ÐͺÍ֮ǰѧ¹ýµÄÔçÆÚ£¬ÔÓÏ¾µä×Ö·ûÉ豸ע²áÄ£ÐͲ»Ò»Ñù£¬Æ½Ì¨Çý¶¯½ö½öÊÇÒ»¸öƽ̨£¬²»»áÉú³É½Úµã£¬Èç¹ûÐèҪͨ¹ý½ÚµãÀ´²Ù×÷É豸£¬Ó¦¸ÃÔÚƽ̨Çý¶¯ÄÚǶÌ×֮ǰѧ¹ýµÄÈýÖÖÉ豸ע²á·½Ê½£¬»òÕßÊǺóÀ´ÎÒÃÇҪѧϰµÄinput×ÓϵͳµÈµÈ·½Ê½
33-1£ºÍ·Îļþ
#include <linux/platform_device.h>
33-2£º½á¹¹ÌåÔÐÍ
1) É豸²ã£º´Ë½á¹¹ÌåÖ÷ÒªÓÃÀ´ÊÕ¼¯¾ßÌåµÄÉ豸ÐÅÏ¢
a. struct platform_device {
const char * name; //ÓÃÀ´ºÍÇý¶¯²ã½øÐÐÅä¶ÔµÄÃû×Ö£¬Èç¹ûÅã¶Ô²»ÉÏ£¬ÄÇôÇý¶¯
¹¤×÷
int id;//ÓÃÓÚÅúÁ¿×¢²áÉ豸ʱµÄ±àºÅ£¬Ò»°ãÎÒÃÇÌî-1²»Æô¶¯Õâ¸ö¹¦ÄÜ
struct device dev;// ¼û2)
u32 num_resources;//Òª´«µÝµÄÊý¾Ý¾Í½Ð×ö×ÊÔ´£¬ÕâÀïÊÇ×ÊÔ´µÄÊýÁ¿
struct resource *resource; //×ÊÔ´Êý×éµÄµØÖ·
¡£¡£¡£¡£¡£¡£
};
b. struct device{
¡£¡£¡£¡£¡£¡£
void *platform_data;//ƽ̨Êý¾Ý£¬´«µÝһЩÓû§×Ô¶¨ÒåµÄÊý¾Ý£¬×ÔÓɳÉÔ±
void (*release)(structdevice *dev); //É豸²ãжÔصÄʱºò»á×Ô¶¯µ÷ÓÃÕâ¸öº¯Êý£¬ÊÍ
·ÅkzallocÉêÇëµÄ¿Õ¼ä£¬ÊÍ·Å»¥³âËø£¬»¹ÓÐһЩ
Óû§×Ô¶¨ÒåµÄ×¢Ïú»òÕßжÔزÙ×÷
¡£¡£¡£¡£¡£¡£
}
c. struct resource {
resource_size_t start; //¿ªÊ¼µØÖ·,¿ÉÒÔ´«¼Ä´æÆ÷µÄ¿ªÊ¼µØÖ·
resource_size_t end; //½áÊøµØÖ·£¬¿ÉÒÔ´«¼Ä´æÆ÷µÄ½áÊøµØÖ·
const char *name; //¸øÄãµÄ×ÊÔ´Ãü¸öÃû£¬¿ÉÑ¡×é¼þ
unsigned long flags; //´«µÝµÄ×ÊÔ´µÄÀàÐÍÈç¼Ä´æÆ÷×ÊÔ´IORESOURCE_MEM
struct resource *parent, *sibling,*child;
};
×¢Ò⣺×ÊԴĬÈϱêʶ¿ÉÒÔÔÚioprot.hÎļþµ±Öвéѯ£ºÈçÏ£º
2) Çý¶¯²ã£º´Ë½á¹¹Ìå½ÓÊÕÉ豸²ã´«µÝ¹ýÀ´µÄÊý¾Ý²¢½øÐÐÇý¶¯µÄ±àд
a. struct platform_driver {
int (*probe)(struct platform_device *);
int (*remove)(struct platform_device *);
void (*shutdown)(struct platform_device *);
int (*suspend)(struct platform_device *, pm_message_tstate);
int (*resume)(struct platform_device *);
struct device_driver driver;
const struct platform_device_id *id_table;
};
probe£ºÕì²âº¯Êý£¬Ò»µ©É豸²ãºÍÇý¶¯²ãÅä¶Ô³É¹¦£¬´Ëº¯Êý¾Í»áµ÷Óã¬Ï൱ÓÚÄ£¿éÄ£Ð͵Äinitº¯Êý£¬ÆäÖÐÓ¦¸ÃÖ´ÐлñÈ¡É豸ÐÅÏ¢ºÍ³õʼ»¯Ó²¼þµÈµÈµÄ¹¦ÄÜ
remove£ºÐ¶Ôغ¯Êý£¬ÀàËÆÓÚÄ£¿éÄ£Ð͵Äexitº¯Êý£¬ÔÚÇý¶¯²ã½á¹¹ÌåжÔصÄʱºò»áµ÷ÓÃÕâ¸öº¯Êý£¬´Ëº¯ÊýÖÐÓ¦¸Ã¶ÔÓ²¼þ×ÊÔ´½øÐÐÊÍ·Å
shutdown£ºµçÔ´¹ÜÀíÏà¹Ø£¬¹Ø»úµÄʱºò»áµ÷ÓÃÕâ¸öº¯Êý
suspend£ºµçÔ´¹ÜÀíÏà¹Ø£¬ÏµÍ³¹ÒÆð(˯Ãß)µÄʱºòµ÷ÓÃÕâ¸öº¯Êý
resume£ºµß»»¹ÜÀíÏà¹Ø£¬ÏµÍ³´Ó˯Ãßģʽ»Ö¸´µÄʱºòµ÷ÓÃÕâ¸öº¯Êý
driver£ºÇý¶¯½á¹¹Ìå³ÉÔ±£¬ÎÒÃÇҪʹÓÃÆä½á¹¹ÌåÄÚ²¿µÄÁ½¸ö³ÉÔ±
driver.name£ºÓÃÀ´ºÍÉ豸²ã½øÐÐÅä¶ÔµÄÇý¶¯Ãû£¬ÌرðÖØÒª£¬²»ÄÜ´í
driver.owner£ºÌîÈëTHIS_MODULE£¬Äں˶ÔÇý¶¯¼ÆÊýÓÃ
id_table£ºÕâÊÇÓÃÀ´½øÐжà¸öÉ豸²ãÆ¥ÅäµÄÆ¥ÅäÃûÁÐ±í£¬´ËÁÐ±íµ±ÖгöÏÖµÄÉ豸ÃûËù¶ÔÓ¦µÄÉ豸²ã¶¼¿ÉÒÔÓ뵱ǰÇý¶¯²ã½øÐÐÅä¶Ô
33-3£ºº¯ÊýÏà¹Ø
1) É豸²ã£º
a. intplatform_device_register(struct platform_device *pdev)
×÷ÓãºÆ½Ì¨É豸²ã×¢²áº¯Êý£¬´Ëº¯ÊýдÔÚÄ£¿éµÄ×¢²áº¯Êýµ±ÖÐ
²ÎÊý£ºÆ½Ì¨É豸²ã½á¹¹ÌåÖ¸Õë
·µ»Ø³É¹¦£º0
·µ»Øʧ°Ü£ºÐ¡ÓÚ0
b. voidplatform_device_unregister(struct platform_device *pdev)
×÷ÓãºÆ½Ì¨É豸²ãжÔغ¯Êý£¬´Ëº¯ÊýдÔÚÄ£¿éµÄжÔغ¯ÊýÖÐ
²ÎÊý£ºÆ½Ì¨É豸²ã½á¹¹ÌåÖ¸Õë
2) Çý¶¯²ã£º
a. intplatform_driver_register(struct platform_driver *drv)
×÷ÓãºÆ½Ì¨Çý¶¯²ã×¢²áº¯Êý£¬´Ëº¯Ð´ÔÚÔØÄ£¿é×¢²áº¯Êýµ±ÖÐ
²ÎÊý£ºÆ½Ì¨Çý¶¯²ã½á¹¹ÌåÖ¸Õë
·µ»Ø³É¹¦£º0
·µ»Øʧ°Ü£º-1
b. voidplatform_driver_unregister(struct platform_driver *drv)
×÷ÓãºÆ½Ì¨Çý¶¯²ãжÔغ¯Êý£¬´Ëº¯ÊýдÔÚÄ£¿éжÔغ¯Êýµ±ÖÐ
²ÎÊý£ºÆ½Ì¨Çý¶¯²ã½á¹¹ÌåÖ¸Õë
33-4£ºÆ½Ì¨É豸²ãµÄ´úÂë±àд
33-5£ºÆ½Ì¨Çý¶¯²ãµÄ´úÂë±àд
33-6£ºÄ£¿é¼ÓÔغÍжÔؼ¸¸öº¯Êýµ÷ÓõÄÏȺó¹Øϵ
1) É豸²ãºÍÇý¶¯²ã¶¼¼ÓÔØÖ®ºó£¬Èç¹ûÅä¶Ô³É¹¦£¬Õì²âº¯Êýprobe½«Ö´ÐÐ
2) É豸²ã¼ÓÔØ£¬È»ºóжÔØ£¬»áÖ´ÐÐreleaseº¯Êý£¬²»¹ÜÓÐûÓÐÇý¶¯²ã£¬¶¼»áÖ´ÐÐ
3) µ±É豸²ãºÍÇý¶¯²ãÅä¶Ô³É¹¦£¬µ¥¶ÀжÔØÉ豸²ã£¬»áÖ´ÐÐreleaseº¯ÊýºÍremoveº¯Êý
4) µ±É豸²ãºÍÇý¶¯²ãÅä¶Ô³É¹¦£¬µ¥¶ÀжÔØÇý¶¯²ã£¬Ö»»áÖ´ÐÐremoveº¯Êý
×ۺϽáÂÛ£ºÔÚ°²×°ºÍжÔØÁ½¸öÄ£¿éµÄ¹ý³Ìµ±ÖУ¬release probe removeº¯Êý·Ö±ðÖ»Ö´ÐÐÒ»´Î£¬releaseº¯ÊýÊǺÍÉ豸²ãÄ£¿é°ó¶¨ÔÚÒ»ÆðµÄ£¬Ö»ÒªÉ豸²ãÄ£¿éжÔؾͻáÖ´ÐУ¬removeͬʱº¯Êý°ó¶¨É豸²ãÄ£¿éºÍÇý¶¯²ãÄ£¿é£¬Ö»ÒªÐ¶ÔØÆäÖÐÈÎÒâÒ»¸ö£¬¶¼»áÖ´ÐÐremoveº¯Êý£¬Ö»ÓÐÅä¶Ô³É¹¦£¬²Å»áÖ´ÐÐprobeº¯Êý£¬ÏÈ°²×°É豸²ã»òÇý¶¯²ã¶ÔÕû¸ö¹ý³ÌûÓÐÓ°Ïì
33-7£ºÆ½Ì¨Çý¶¯²ãʵÏÖÉ豸Çý¶¯ËùÐèÒªÓõ½µÄº¯Êý
ÄÚºËÓÐרÃÅÌṩ¸ø´úÂ뿪·¢ÈËÔ±ÓÃÀ´»ñÈ¡×ÊÔ´µÄº¯Êý
1) struct resource*platform_get_resource(struct platform_device *dev,
unsigned int type, unsigned int num)
×÷Ó㺻ñÈ¡É豸²ã´«µÝ¹ýÀ´µÄÊý¾Ý
²ÎÊý1£ºÉ豸²ã½á¹¹Ì壬probeº¯ÊýµÄ²ÎÊýÌṩ
²ÎÊý2£ºÌîÈë´ú±í×ÊÔ´ÀàÐ͵ĺêÈçIORESOURCE_MEM
²ÎÊý3£ºÏëÒª»ñµÃµÄ×ÊÔ´ÔÚ×ÊÔ´Êý×éµ±ÖÐͬÀàÐ͵ĵڼ¸¸ö£¬´Ó0¿ªÊ¼£¬×¢Ò⣺²»ÊÇÔÚ×ÊÔ´Êý×éÖÐϱêµÄ±àºÅ£¬¶øÊÇͬÀàÐÍ×ÊÔ´µÄµÚ¼¸¸ö£¬´Ó0¿ªÊ¼
·µ»Ø³É¹¦£º×ÊÔ´½á¹¹ÌåÖ¸Õë
·µ»Øʧ°Ü£ºNULL
2) int platform_get_irq(structplatform_device *dev, unsigned int num)
×÷Óãº×¨ÃÅÓÃÀ´»ñµÃirqÖжÏ×ÊÔ´µÄº¯Êý£¬Ò²¿ÉÒÔ²»Óã¬Ö±½ÓÓÃ1)Ò²ÊÇ¿ÉÒÔµÄ
²ÎÊý1£ºÉ豸²ã½á¹¹ÌåÖ¸Õë
²ÎÊý2£º×ÊÔ´ÔÚͬÀàÐÍ×ÊÔ´µ±ÖеÄ˳Ðò±àºÅ£¬´Ó0¿ªÊ¼
·µ»Ø³É¹¦£º·µ»ØµÄÊÇ´«µÝ¹ýÀ´µÄÖжϱàºÅ
·µ»Øʧ°Ü£ºÐ¡ÓÚ0
3) request_mem_region(start,n,name)
×÷ÓãºÉêÇë¼Ä´æÆ÷µÄÄÚ´æ×ÊÔ´£¬ÅжÏÓÐûÓÐÐéÄâ¼Ä´æÆ÷¿ÉÒÔ·ÖÅäµÄ£¬±¾ÖÊÉÏÊÇ·Àֹͬһ×é¼Ä´æÆ÷±»¶à´ÎÓ³Éäµ½²»Í¬µÄÄÚ´æ
²ÎÊý1£º¼Ä´æÆ÷µØÖ·µÄÆðʼµØÖ·
²ÎÊý2£ºÒªÉêÇëµÄ¼Ä´æÆ÷³¤¶È
²ÎÊý3£ºÉêÇëÕߵǼÇÃû£¬¿ÉÒÔÌîÈëNULL
·µ»Ø³É¹¦£ºstruct resource *Ð͵Ä×ÊÔ´Ö¸Õë
·µ»Øʧ°Ü£ºNULL
4) release_mem_region(start,n)
×÷ÓãºÊÍ·ÅÉêÇëµ½µÄ¼Ä´æÆ÷ÄÚ´æ×ÊÔ´£¬Óë2)ÊÇÒ»¶Ô£¬Ò»¸öÉêÇ룬һ¸öÊÍ·Å
²ÎÊý1£º¼Ä´æÆ÷µØÖ·¿ªÊ¼µÄµØÖ·
²ÎÊý2£ºÒªÊͷŵļĴæÆ÷µØÖ·³¤¶È
33-8£º´úÂ룺°´¼üÖжϿØÖÆLED
Çë²Î¿¼ÊÓƵºÍ´úÂë
×÷Òµ£º×Ô¼ºÊµÏÖƽ̨Çý¶¯µÄÄ£ÐÍ£¬²¢ÇÒÔÚÉ豸²ãºÍÇý¶¯²ãʵÏÖͬһÌ×Çý¶¯¼È¿ÉÒÔÇý¶¯LED£¬ÓпÉÒÔ¿ØÖÆBEEP£¬Í¨¹ý²»Í¬µÄ°´¼üÖжÏÀ´¿ØÖÆ
33-9£ºÇý¶¯²ãºÍÉ豸²ã½øÐÐÆ¥ÅäµÄÁíÍâÒ»ÖÖ·½Ê½
¿ÉÒÔʹÓÃstruct platform_driver½á¹¹ÌåµÄ³ÉÔ±const struct platform_device_id *id_tableÀ´×÷ΪÇý¶¯ÃûÓëÉ豸²ãµÄÉ豸Ãû½øÐÐÅä¶Ô£¬ÏÈ¿´½á¹¹ÌåÔÐÍ£º
struct platform_device_id {
charname[PLATFORM_NAME_SIZE]; //Õâ¾ÍÊÇÓÃÀ´Æ¥ÅäµÄÇý¶¯Ãû
kernel_ulong_tdriver_data //¿ÉÒÔ´«µÝÊý¾Ý£¬Ò»°ãδʹÓÃ
__attribute__((aligned(sizeof(kernel_ulong_t))));
};
×¢Ò⣺µ±ÎÒÃÇʵÏÖÁ˴˽ṹÌåµÄ³ÉÔ±name[PLATFORM_NAME_SIZE]Ö®ºó£¬Ô±¾ÓÃÀ´½øÐÐÆ¥ÅäµÄ½á¹¹Ìå³ÉÔ±driver.name¾ÍʧЧÁË£¬µ«ÊÇÔÚ´úÂë±àдµÄʱºò»¹ÊÇҪȥʵÏÖÕâ¸ödriver.name£¬¿ÉÒÔÎÞЧ£¬µ«ÊDz»ÄÜûÓС£ÁíÍ⣬´Ë½á¹¹ÌåÔÚƽ̨Çý¶¯²ã½á¹¹ÌåÄÚ²¿ÊÇÒԽṹÌåÖ¸ÕëµÄÐÎʽ´æÔڵģ¬ËùÒÔÖ§³Ö¶à¸öÇý¶¯Ãû½øÐÐÆ¥Å䣬ÒÔÊý×éµÄ·½Ê½´øÈë¼´¿É
33-10£ºÆ½Ì¨Êý¾Ý---Ò»¸öºÜÖØÒªµÄÉ豸²ã³ÉÔ±
Struct platform_device mydevice;
´Ë³ÉÔ±ÓëÉ豸²ã½á¹¹ÌåÔÐ͵ĹØϵ£ºmydevice.dev.platform_data
ÆäÔÐÍΪ£ºvoid *platform_data Òò´Ë¿ÉÒÔ´«µÝÈÎÒâÊý¾Ý£¬²¢ÇÒÕâ¸ö³ÉÔ±²»»á±»ÆäËûµÄϵͳ¹¦Äܵ÷Óã¬ÍêÈ«ÊÇÌṩ¸ø´úÂ뿪·¢ÈËԱʹÓõġ£ÔÚʵ¼ÊµÄÇý¶¯¿ª·¢¹ý³ÌÖУ¬Õâ¸ö³ÉÔ±·Ç³£ÓÐÓá£